[feat] log

This commit is contained in:
zhhe committed 2026-11-17 07:58:03 +08:00
1 parent 1fcf56e3dd
commit 44e203d868
14 files changed
+305 -210

No files matched your search

+55 -20
View File
@@ -1,5 +1,5 @@
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { StatusCard } from './StatusCard.js'; import { StatusCard } from './StatusCard.js';
const table = { const table = {
width: '100%', width: '100%',
@@ -8,7 +8,7 @@ const table = {
}; };
const cell = { const cell = {
borderBottom: '1px solid var(--dsh-border, #e5e7eb)', borderBottom: '1px solid var(--dsh-border, #e5e7eb)',
padding: '8px 10px', padding: '6px 8px',
verticalAlign: 'middle', verticalAlign: 'middle',
textAlign: 'left', textAlign: 'left',
}; };
@@ -38,8 +38,22 @@ const primary = {
color: '#fff', color: '#fff',
}; };
const muted = { color: '#6b7280', fontSize: 12 }; const muted = { color: '#6b7280', fontSize: 12 };
const section = { display: 'grid', gap: 16, maxWidth: 760 }; const section = { display: 'grid', gap: 16, maxWidth: 640 };
const row = { display: 'flex', gap: 8, alignItems: 'center' }; const row = { display: 'flex', gap: 8, alignItems: 'center' };
const logBox = {
margin: 0,
padding: '8px 10px',
maxHeight: 220,
overflow: 'auto',
border: '1px solid var(--dsh-border, #e5e7eb)',
borderRadius: 6,
background: 'var(--dsh-code-bg, rgba(127,127,127,0.08))',
fontSize: 12,
lineHeight: 1.6,
fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
whiteSpace: 'pre-wrap',
wordBreak: 'break-all',
};
export function MailGatewaySection({ api }) { export function MailGatewaySection({ api }) {
const [snapshot, setSnapshot] = useState(); const [snapshot, setSnapshot] = useState();
const [mailbox, setMailbox] = useState(); const [mailbox, setMailbox] = useState();
@@ -49,6 +63,38 @@ export function MailGatewaySection({ api }) {
const [test, setTest] = useState(); const [test, setTest] = useState();
const [busy, setBusy] = useState(null); const [busy, setBusy] = useState(null);
const [notice, setNotice] = useState(); const [notice, setNotice] = useState();
const logRef = useRef(null);
// 首次读取填充表单;之后每 3 秒只刷新状态与日志,不覆盖正在编辑的表单。
useEffect(() => {
let alive = true;
const load = async (initial) => {
const result = await api.get();
if (!alive)
return;
if (!result.ok) {
if (initial)
setNotice({ kind: 'error', text: result.error.message });
return;
}
setSnapshot(result.value);
if (initial) {
setMailbox({ ...result.value.mailbox });
setAllowed([...result.value.allowedAddresses]);
setPassword('');
}
};
void load(true);
const timer = setInterval(() => void load(false), 3000);
return () => {
alive = false;
clearInterval(timer);
};
}, [api]);
useEffect(() => {
const box = logRef.current;
if (box)
box.scrollTop = box.scrollHeight;
}, [snapshot?.logs]);
const applySnapshot = (next, resetMailbox) => { const applySnapshot = (next, resetMailbox) => {
setSnapshot(next); setSnapshot(next);
setAllowed([...next.allowedAddresses]); setAllowed([...next.allowedAddresses]);
@@ -57,20 +103,6 @@ export function MailGatewaySection({ api }) {
setPassword(''); setPassword('');
} }
}; };
useEffect(() => {
let alive = true;
void api.get().then(result => {
if (!alive)
return;
if (result.ok)
applySnapshot(result.value, true);
else
setNotice({ kind: 'error', text: result.error.message });
});
return () => {
alive = false;
};
}, [api]);
const setField = (key, value) => { const setField = (key, value) => {
setMailbox(current => (current ? { ...current, [key]: value } : current)); setMailbox(current => (current ? { ...current, [key]: value } : current));
}; };
@@ -88,7 +120,7 @@ export function MailGatewaySection({ api }) {
if (result.ok) { if (result.ok) {
applySnapshot(result.value, true); applySnapshot(result.value, true);
setTest(undefined); setTest(undefined);
setNotice({ kind: 'ok', text: '邮箱绑定已保存,连接已按新配置重建。' }); setNotice({ kind: 'ok', text: '已保存,按新配置重连。' });
} }
else { else {
setNotice({ kind: 'error', text: result.error.message }); setNotice({ kind: 'error', text: result.error.message });
@@ -131,7 +163,7 @@ export function MailGatewaySection({ api }) {
setBusy(null); setBusy(null);
if (result.ok) { if (result.ok) {
applySnapshot(result.value, false); applySnapshot(result.value, false);
setNotice({ kind: 'ok', text: '白名单已保存,收信策略立即生效,连接保持不变。' }); setNotice({ kind: 'ok', text: '白名单已保存。' });
} }
else { else {
setNotice({ kind: 'error', text: result.error.message }); setNotice({ kind: 'error', text: result.error.message });
@@ -146,5 +178,8 @@ export function MailGatewaySection({ api }) {
const boolField = (key) => (event) => { const boolField = (key) => (event) => {
setField(key, event.target.checked); setField(key, event.target.checked);
}; };
return (_jsxs("div", { style: section, children: [_jsx(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test: test }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }), _jsxs("table", { style: table, children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { style: { ...cell, ...muted, width: 140 }, children: "\u5B57\u6BB5" }), _jsx("th", { style: { ...cell, ...muted }, children: "\u503C" }), _jsx("th", { style: { ...cell, ...muted, width: 240 }, children: "\u8BF4\u660E" })] }) }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u90AE\u7BB1\u5730\u5740" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.email ?? '', onChange: textField('email') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u63A5\u6536\u90AE\u4EF6\u4E0E\u56DE\u4FE1\u4F7F\u7528\u7684\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u663E\u793A\u540D" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.displayName ?? '', onChange: textField('displayName') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u56DE\u4FE1\u65F6\u7684\u53D1\u4EF6\u4EBA\u540D\u79F0\uFF0C\u53EF\u7559\u7A7A\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP \u4E3B\u673A" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.imapHost ?? '', onChange: textField('imapHost') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 IMAP \u670D\u52A1\u5668\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP \u7AEF\u53E3" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.imapPort ?? 993, onChange: numberField('imapPort') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 993\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP TLS" }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.imapSecure ?? true, onChange: boolField('imapSecure') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP \u4E3B\u673A" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.smtpHost ?? '', onChange: textField('smtpHost') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 SMTP \u670D\u52A1\u5668\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP \u7AEF\u53E3" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.smtpPort ?? 465, onChange: numberField('smtpPort') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 465\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP TLS" }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.smtpSecure ?? true, onChange: boolField('smtpSecure') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u767B\u5F55\u7528\u6237\u540D" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.username ?? '', onChange: textField('username') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u7559\u7A7A\u65F6\u4F7F\u7528\u90AE\u7BB1\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u5BC6\u7801" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, type: "password", value: password, placeholder: snapshot?.passwordSet ? '已设置,留空表示不修改' : '未设置', onChange: event => setPassword(event.target.value) }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u4FDD\u5B58\u540E\u53EA\u663E\u793A\u201C\u5DF2\u8BBE\u7F6E\u201D\uFF0C\u4E0D\u4F1A\u56DE\u663E\u660E\u6587\u3002" })] })] })] }), _jsxs("div", { style: { ...row, marginTop: 10 }, children: [_jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === 'mailbox' ? '保存中…' : '保存邮箱绑定' }), _jsx("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === 'test' ? '测试中…' : '测试连接' }), _jsx("span", { style: muted, children: "\u4FDD\u5B58\u90AE\u7BB1\u7ED1\u5B9A\u65F6\u4F1A\u6309\u65B0\u914D\u7F6E\u91CD\u5EFA\u8FDE\u63A5\u3002" })] })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u767D\u540D\u5355" }), _jsxs("div", { style: { ...row, marginBottom: 8 }, children: [_jsx("input", { style: { ...input,Line truncated const textRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.[key] ?? '', onChange: textField(key) }) })] }));
const portRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.[key] ?? 0, onChange: numberField(key) }) })] }));
const tlsRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.[key] ?? true, onChange: boolField(key) }) })] }));
return (_jsxs("div", { style: section, children: [_jsx(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test: test }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }), _jsx("table", { style: table, children: _jsxs("tbody", { children: [textRow('邮箱地址', 'email'), textRow('显示名', 'displayName'), textRow('IMAP 主机', 'imapHost'), portRow('IMAP 端口', 'imapPort'), tlsRow('IMAP TLS', 'imapSecure'), textRow('SMTP 主机', 'smtpHost'), portRow('SMTP 端口', 'smtpPort'), tlsRow('SMTP TLS', 'smtpSecure'), textRow('登录用户名', 'username'), _jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: "\u5BC6\u7801" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, type: "password", value: password, placeholder: snapshot?.passwordSet ? '已设置,留空表示不修改' : '未设置', onChange: event => setPassword(event.target.value) }) })] })] }) }), _jsxs("div", { style: { ...row, marginTop: 10 }, children: [_jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === 'mailbox' ? '保存中…' : '保存邮箱绑定' }), _jsx("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === 'test' ? '测试中…' : '测试连接' })] })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u767D\u540D\u5355" }), _jsxs("div", { style: { ...row, marginBottom: 8 }, children: [_jsx("input", { style: { ...input, maxWidth: 320 }, value: entry, placeholder: "\u8F93\u5165\u90AE\u7BB1\u5730\u5740\u540E\u56DE\u8F66\u6DFB\u52A0", onChange: event => setEntry(event.target.value), onKeyDown: onEntryKey }), _jsx("button", { style: button, onClick: addAddress, children: "\u6DFB\u52A0" })] }), _jsx("table", { style: table, children: _jsx("tbody", { children: allowed.length === 0 ? (_jsx("tr", { children: _jsx("td", { style: { ...cell, ...muted }, children: "\u6682\u65E0\u5730\u5740\uFF0C\u767D\u540D\u5355\u4E3A\u7A7A\u65F6\u4E0D\u4F1A\u63A5\u6536\u4EFB\u4F55\u90AE\u4EF6\u3002" }) })) : (allowed.map(address => (_jsxs("tr", { children: [_jsx("td", { style: cell, children: address }), _jsx("td", { style: { ...cell, width: 80, textAlign: 'right' }, children: _jsx("button", { style: button, onClick: () => removeAddress(address), children: "\u5220\u9664" }) })] }, address)))) }) }), _jsx("div", { style: { ...row, marginTop: 10 }, children: _jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveWhitelist(), children: busy === 'whitelist' ? '保存中…' : '保存白名单' }) })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u8FD0\u884C\u65E5\u5FD7" }), _jsx("pre", { ref: logRef, style: logBox, children: snapshot?.logs?.length ? snapshot.logs.join('\n') : '暂无日志' })] }), notice ? (_jsx("div", { style: { color: notice.kind === 'ok' ? '#047857' : '#b91c1c', fontSize: 13 }, children: notice.text })) : null] }));
} }
+89 -89
View File
@@ -19915,7 +19915,8 @@ var snapshotSchema = () => external_exports.object({
mailbox: mailboxSchema(), mailbox: mailboxSchema(),
passwordSet: external_exports.boolean(), passwordSet: external_exports.boolean(),
allowedAddresses: external_exports.array(external_exports.string()), allowedAddresses: external_exports.array(external_exports.string()),
status: statusSchema() status: statusSchema(),
logs: external_exports.array(external_exports.string())
}); });
var patchSchema = () => external_exports.object({ var patchSchema = () => external_exports.object({
email: external_exports.string().optional(), email: external_exports.string().optional(),
@@ -20048,7 +20049,7 @@ var table = {
}; };
var cell = { var cell = {
borderBottom: "1px solid var(--dsh-border, #e5e7eb)", borderBottom: "1px solid var(--dsh-border, #e5e7eb)",
padding: "8px 10px", padding: "6px 8px",
verticalAlign: "middle", verticalAlign: "middle",
textAlign: "left" textAlign: "left"
}; };
@@ -20078,8 +20079,22 @@ var primary = {
color: "#fff" color: "#fff"
}; };
var muted = { color: "#6b7280", fontSize: 12 }; var muted = { color: "#6b7280", fontSize: 12 };
var section = { display: "grid", gap: 16, maxWidth: 760 }; var section = { display: "grid", gap: 16, maxWidth: 640 };
var row = { display: "flex", gap: 8, alignItems: "center" }; var row = { display: "flex", gap: 8, alignItems: "center" };
var logBox = {
margin: 0,
padding: "8px 10px",
maxHeight: 220,
overflow: "auto",
border: "1px solid var(--dsh-border, #e5e7eb)",
borderRadius: 6,
background: "var(--dsh-code-bg, rgba(127,127,127,0.08))",
fontSize: 12,
lineHeight: 1.6,
fontFamily: "ui-monospace, SFMono-Regular, Consolas, monospace",
whiteSpace: "pre-wrap",
wordBreak: "break-all"
};
function MailGatewaySection({ api }) { function MailGatewaySection({ api }) {
const [snapshot, setSnapshot] = (0, import_react.useState)(); const [snapshot, setSnapshot] = (0, import_react.useState)();
const [mailbox, setMailbox] = (0, import_react.useState)(); const [mailbox, setMailbox] = (0, import_react.useState)();
@@ -20089,6 +20104,34 @@ function MailGatewaySection({ api }) {
const [test, setTest] = (0, import_react.useState)(); const [test, setTest] = (0, import_react.useState)();
const [busy, setBusy] = (0, import_react.useState)(null); const [busy, setBusy] = (0, import_react.useState)(null);
const [notice, setNotice] = (0, import_react.useState)(); const [notice, setNotice] = (0, import_react.useState)();
const logRef = (0, import_react.useRef)(null);
(0, import_react.useEffect)(() => {
let alive = true;
const load = async (initial) => {
const result = await api.get();
if (!alive) return;
if (!result.ok) {
if (initial) setNotice({ kind: "error", text: result.error.message });
return;
}
setSnapshot(result.value);
if (initial) {
setMailbox({ ...result.value.mailbox });
setAllowed([...result.value.allowedAddresses]);
setPassword("");
}
};
void load(true);
const timer = setInterval(() => void load(false), 3e3);
return () => {
alive = false;
clearInterval(timer);
};
}, [api]);
(0, import_react.useEffect)(() => {
const box = logRef.current;
if (box) box.scrollTop = box.scrollHeight;
}, [snapshot?.logs]);
const applySnapshot = (next, resetMailbox) => { const applySnapshot = (next, resetMailbox) => {
setSnapshot(next); setSnapshot(next);
setAllowed([...next.allowedAddresses]); setAllowed([...next.allowedAddresses]);
@@ -20097,17 +20140,6 @@ function MailGatewaySection({ api }) {
setPassword(""); setPassword("");
} }
}; };
(0, import_react.useEffect)(() => {
let alive = true;
void api.get().then((result) => {
if (!alive) return;
if (result.ok) applySnapshot(result.value, true);
else setNotice({ kind: "error", text: result.error.message });
});
return () => {
alive = false;
};
}, [api]);
const setField = (key, value) => { const setField = (key, value) => {
setMailbox((current) => current ? { ...current, [key]: value } : current); setMailbox((current) => current ? { ...current, [key]: value } : current);
}; };
@@ -20124,7 +20156,7 @@ function MailGatewaySection({ api }) {
if (result.ok) { if (result.ok) {
applySnapshot(result.value, true); applySnapshot(result.value, true);
setTest(void 0); setTest(void 0);
setNotice({ kind: "ok", text: "\u90AE\u7BB1\u7ED1\u5B9A\u5DF2\u4FDD\u5B58\uFF0C\u8FDE\u63A5\u5DF2\u6309\u65B0\u914D\u7F6E\u91CD\u5EFA\u3002" }); setNotice({ kind: "ok", text: "\u5DF2\u4FDD\u5B58\uFF0C\u6309\u65B0\u914D\u7F6E\u91CD\u8FDE\u3002" });
} else { } else {
setNotice({ kind: "error", text: result.error.message }); setNotice({ kind: "error", text: result.error.message });
} }
@@ -20162,7 +20194,7 @@ function MailGatewaySection({ api }) {
setBusy(null); setBusy(null);
if (result.ok) { if (result.ok) {
applySnapshot(result.value, false); applySnapshot(result.value, false);
setNotice({ kind: "ok", text: "\u767D\u540D\u5355\u5DF2\u4FDD\u5B58\uFF0C\u6536\u4FE1\u7B56\u7565\u7ACB\u5373\u751F\u6548\uFF0C\u8FDE\u63A5\u4FDD\u6301\u4E0D\u53D8\u3002" }); setNotice({ kind: "ok", text: "\u767D\u540D\u5355\u5DF2\u4FDD\u5B58\u3002" });
} else { } else {
setNotice({ kind: "error", text: result.error.message }); setNotice({ kind: "error", text: result.error.message });
} }
@@ -20176,82 +20208,49 @@ function MailGatewaySection({ api }) {
const boolField = (key) => (event) => { const boolField = (key) => (event) => {
setField(key, event.target.checked); setField(key, event.target.checked);
}; };
const textRow = (label, key) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 110 }, children: label }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.[key] ?? "", onChange: textField(key) }) })
] });
const portRow = (label, key) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 110 }, children: label }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.[key] ?? 0, onChange: numberField(key) }) })
] });
const tlsRow = (label, key) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 110 }, children: label }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { type: "checkbox", checked: mailbox?.[key] ?? true, onChange: boolField(key) }) })
] });
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: section, children: [ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: section, children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { style: { margin: "0 0 8px", fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { style: { margin: "0 0 8px", fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("table", { style: table, children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("table", { style: table, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tbody", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("thead", { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [ textRow("\u90AE\u7BB1\u5730\u5740", "email"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("th", { style: { ...cell, ...muted, width: 140 }, children: "\u5B57\u6BB5" }), textRow("\u663E\u793A\u540D", "displayName"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("th", { style: { ...cell, ...muted }, children: "\u503C" }), textRow("IMAP \u4E3B\u673A", "imapHost"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("th", { style: { ...cell, ...muted, width: 240 }, children: "\u8BF4\u660E" }) portRow("IMAP \u7AEF\u53E3", "imapPort"),
] }) }), tlsRow("IMAP TLS", "imapSecure"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tbody", { children: [ textRow("SMTP \u4E3B\u673A", "smtpHost"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [ portRow("SMTP \u7AEF\u53E3", "smtpPort"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "\u90AE\u7BB1\u5730\u5740" }), tlsRow("SMTP TLS", "smtpSecure"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.email ?? "", onChange: textField("email") }) }), textRow("\u767B\u5F55\u7528\u6237\u540D", "username"),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u63A5\u6536\u90AE\u4EF6\u4E0E\u56DE\u4FE1\u4F7F\u7528\u7684\u5730\u5740\u3002" }) /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
] }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 110 }, children: "\u5BC6\u7801" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "\u663E\u793A\u540D" }), "input",
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.displayName ?? "", onChange: textField("displayName") }) }), {
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u56DE\u4FE1\u65F6\u7684\u53D1\u4EF6\u4EBA\u540D\u79F0\uFF0C\u53EF\u7559\u7A7A\u3002" }) style: input2,
] }), type: "password",
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [ value: password,
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "IMAP \u4E3B\u673A" }), placeholder: snapshot?.passwordSet ? "\u5DF2\u8BBE\u7F6E\uFF0C\u7559\u7A7A\u8868\u793A\u4E0D\u4FEE\u6539" : "\u672A\u8BBE\u7F6E",
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.imapHost ?? "", onChange: textField("imapHost") }) }), onChange: (event) => setPassword(event.target.value)
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 IMAP \u670D\u52A1\u5668\u5730\u5740\u3002" }) }
] }), ) })
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "IMAP \u7AEF\u53E3" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.imapPort ?? 993, onChange: numberField("imapPort") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 993\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "IMAP TLS" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { type: "checkbox", checked: mailbox?.imapSecure ?? true, onChange: boolField("imapSecure") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "SMTP \u4E3B\u673A" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.smtpHost ?? "", onChange: textField("smtpHost") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 SMTP \u670D\u52A1\u5668\u5730\u5740\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "SMTP \u7AEF\u53E3" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.smtpPort ?? 465, onChange: numberField("smtpPort") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 465\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "SMTP TLS" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { type: "checkbox", checked: mailbox?.smtpSecure ?? true, onChange: boolField("smtpSecure") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "\u767B\u5F55\u7528\u6237\u540D" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("input", { style: input2, value: mailbox?.username ?? "", onChange: textField("username") }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u7559\u7A7A\u65F6\u4F7F\u7528\u90AE\u7BB1\u5730\u5740\u3002" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("tr", { children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: "\u5BC6\u7801" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"input",
{
style: input2,
type: "password",
value: password,
placeholder: snapshot?.passwordSet ? "\u5DF2\u8BBE\u7F6E\uFF0C\u7559\u7A7A\u8868\u793A\u4E0D\u4FEE\u6539" : "\u672A\u8BBE\u7F6E",
onChange: (event) => setPassword(event.target.value)
}
) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, ...muted }, children: "\u4FDD\u5B58\u540E\u53EA\u663E\u793A\u201C\u5DF2\u8BBE\u7F6E\u201D\uFF0C\u4E0D\u4F1A\u56DE\u663E\u660E\u6587\u3002" })
] })
] }) ] })
] }), ] }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { ...row, marginTop: 10 }, children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { ...row, marginTop: 10 }, children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === "mailbox" ? "\u4FDD\u5B58\u4E2D\u2026" : "\u4FDD\u5B58\u90AE\u7BB1\u7ED1\u5B9A" }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === "mailbox" ? "\u4FDD\u5B58\u4E2D\u2026" : "\u4FDD\u5B58\u90AE\u7BB1\u7ED1\u5B9A" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === "test" ? "\u6D4B\u8BD5\u4E2D\u2026" : "\u6D4B\u8BD5\u8FDE\u63A5" }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === "test" ? "\u6D4B\u8BD5\u4E2D\u2026" : "\u6D4B\u8BD5\u8FDE\u63A5" })
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: muted, children: "\u4FDD\u5B58\u90AE\u7BB1\u7ED1\u5B9A\u65F6\u4F1A\u6309\u65B0\u914D\u7F6E\u91CD\u5EFA\u8FDE\u63A5\u3002" })
] }) ] })
] }), ] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
@@ -20273,10 +20272,11 @@ function MailGatewaySection({ api }) {
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: address }), /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: cell, children: address }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 80, textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: button, onClick: () => removeAddress(address), children: "\u5220\u9664" }) }) /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("td", { style: { ...cell, width: 80, textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: button, onClick: () => removeAddress(address), children: "\u5220\u9664" }) })
] }, address)) }) }), ] }, address)) }) }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { ...row, marginTop: 10 }, children: [ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { ...row, marginTop: 10 }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: primary, disabled: busy !== null, onClick: () => void saveWhitelist(), children: busy === "whitelist" ? "\u4FDD\u5B58\u4E2D\u2026" : "\u4FDD\u5B58\u767D\u540D\u5355" }) })
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { style: primary, disabled: busy !== null, onClick: () => void saveWhitelist(), children: busy === "whitelist" ? "\u4FDD\u5B58\u4E2D\u2026" : "\u4FDD\u5B58\u767D\u540D\u5355" }), ] }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: muted, children: "\u540C\u4E00\u4EFD\u767D\u540D\u5355\u540C\u65F6\u6821\u9A8C\u5165\u7AD9\u53D1\u4EF6\u5730\u5740\u4E0E\u51FA\u7AD9\u56DE\u590D\u5730\u5740\u3002" }) /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
] }) /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { style: { margin: "0 0 8px", fontSize: 15 }, children: "\u8FD0\u884C\u65E5\u5FD7" }),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("pre", { ref: logRef, style: logBox, children: snapshot?.logs?.length ? snapshot.logs.join("\n") : "\u6682\u65E0\u65E5\u5FD7" })
] }), ] }),
notice ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { color: notice.kind === "ok" ? "#047857" : "#b91c1c", fontSize: 13 }, children: notice.text }) : null notice ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { color: notice.kind === "ok" ? "#047857" : "#b91c1c", fontSize: 13 }, children: notice.text }) : null
] }); ] });
+8 -1
View File
@@ -33,6 +33,7 @@ export class MailDeliverer {
content: [{ type: 'text', text: job.text }], content: [{ type: 'text', text: job.text }],
source: { kind: 'user' }, source: { kind: 'user' },
})); }));
this.onLog?.(`[mail] ${job.sessionId} 已交给模型,等待回复`);
} }
dispose() { dispose() {
this.offRoot(); this.offRoot();
@@ -61,12 +62,15 @@ export class MailDeliverer {
return borrowed; return borrowed;
} }
let handle; let handle;
let created = false;
try { try {
handle = await this.ctx.agents.resume({ resumeSessionId: id }); handle = await this.ctx.agents.resume({ resumeSessionId: id });
} }
catch { catch {
handle = await this.ctx.agents.create({ sessionId: id }); handle = await this.ctx.agents.create({ sessionId: id });
created = true;
} }
this.onLog?.(`[mail] ${sessionId} 会话已${created ? '创建' : '恢复'}`);
const owned = { const owned = {
agent: handle.agent, agent: handle.agent,
off: this.watch(handle.agent), off: this.watch(handle.agent),
@@ -114,8 +118,11 @@ export class MailDeliverer {
this.pending.delete(sessionId); this.pending.delete(sessionId);
if (!job) if (!job)
return; return;
if (event.data.reason.kind !== 'completed' || !text) if (event.data.reason.kind !== 'completed' || !text) {
this.onLog?.(`[mail] ${sessionId} 本轮未产生可回复文本(${event.data.reason.kind})`);
return; return;
}
this.onLog?.(`[mail] ${sessionId} 模型已回复,准备回信 ${job.from}`);
void this.onReply(job, text).catch(error => { void this.onReply(job, text).catch(error => {
this.onLog?.(`[mail] 回信失败: ${error instanceof Error ? error.message : String(error)}`); this.onLog?.(`[mail] 回信失败: ${error instanceof Error ? error.message : String(error)}`);
}); });
+3
View File
@@ -16,8 +16,11 @@ export declare class MailGateway {
private allowed; private allowed;
private accountKey; private accountKey;
private generation; private generation;
private readonly logs;
private status; private status;
constructor(ctx: Context, mailbox: MailboxSettings, allowed: readonly string[], onLog?: (message: string) => void); constructor(ctx: Context, mailbox: MailboxSettings, allowed: readonly string[], onLog?: (message: string) => void);
/** 所有运行日志都进内存环形缓冲,设置页可直接查看,同时保留控制台输出。 */
private record;
start(): () => Promise<void>; start(): () => Promise<void>;
/** 只接受新的绑定与白名单;ctx 不参与重载。 */ /** 只接受新的绑定与白名单;ctx 不参与重载。 */
reload(mailbox: MailboxSettings, allowed: readonly string[]): void; reload(mailbox: MailboxSettings, allowed: readonly string[]): void;
+16 -4
View File
@@ -23,6 +23,7 @@ export class MailGateway {
allowed; allowed;
accountKey = ''; accountKey = '';
generation = 0; generation = 0;
logs = [];
status = { status = {
phase: 'disabled', phase: 'disabled',
supportsIdle: false, supportsIdle: false,
@@ -37,7 +38,16 @@ export class MailGateway {
if (!this.sender) if (!this.sender)
throw new Error('SMTP 尚未配置'); throw new Error('SMTP 尚未配置');
await this.sender.send(job, text); await this.sender.send(job, text);
}, onLog); this.record(`[mail] 已回信 ${job.from}`);
}, message => this.record(message));
}
/** 所有运行日志都进内存环形缓冲,设置页可直接查看,同时保留控制台输出。 */
record(message) {
const time = new Date().toLocaleTimeString('zh-CN', { hour12: false });
this.logs.push(`${time} ${message}`);
if (this.logs.length > 200)
this.logs.splice(0, this.logs.length - 200);
this.onLog(message);
} }
start() { start() {
this.restart(); this.restart();
@@ -72,6 +82,7 @@ export class MailGateway {
passwordSet: Boolean(this.mailbox.password), passwordSet: Boolean(this.mailbox.password),
allowedAddresses: [...this.allowed], allowedAddresses: [...this.allowed],
status: this.status, status: this.status,
logs: [...this.logs],
}; };
} }
restart() { restart() {
@@ -83,9 +94,10 @@ export class MailGateway {
if (!isMailboxConfigured(this.mailbox)) { if (!isMailboxConfigured(this.mailbox)) {
this.accountKey = ''; this.accountKey = '';
this.status = { phase: 'disabled', supportsIdle: false, pollIntervalMs: POLL_INTERVAL_MS }; this.status = { phase: 'disabled', supportsIdle: false, pollIntervalMs: POLL_INTERVAL_MS };
this.onLog('[mail] 未配置完整邮箱,收信入口保持关闭'); this.record('[mail] 未配置完整邮箱,收信入口保持关闭');
return; return;
} }
this.record(`[mail] 开始连接 ${this.mailbox.imapHost}:${this.mailbox.imapPort}`);
this.accountKey = mailboxKey(this.mailbox); this.accountKey = mailboxKey(this.mailbox);
this.sender = new SmtpSender(this.mailbox); this.sender = new SmtpSender(this.mailbox);
this.receiver = new ImapReceiver(this.mailbox, () => this.policy, new CursorStore(`${this.mailbox.email}@${this.mailbox.imapHost}`), { this.receiver = new ImapReceiver(this.mailbox, () => this.policy, new CursorStore(`${this.mailbox.email}@${this.mailbox.imapHost}`), {
@@ -94,7 +106,7 @@ export class MailGateway {
if (generation === this.generation) if (generation === this.generation)
this.status = status; this.status = status;
}, },
onLog: this.onLog, onLog: message => this.record(message),
}); });
this.receiver.start(); this.receiver.start();
} }
@@ -114,7 +126,7 @@ export class MailGateway {
await this.deliverer.deliver(job); await this.deliverer.deliver(job);
} }
catch (error) { catch (error) {
this.onLog(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`); this.record(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`);
} }
} }
} }
+4
View File
@@ -183,6 +183,10 @@ export class ImapReceiver {
this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`); this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`);
this.hooks.onJob(job); this.hooks.onJob(job);
} }
else {
const sender = parsed.from?.value?.[0]?.address ?? '未知发件人';
this.hooks.onLog?.(`[mail] 忽略 ${sender} 的邮件:不在白名单或缺少发件地址`);
}
} }
catch (error) { catch (error) {
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`); this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`);
+1
View File
@@ -24,6 +24,7 @@ const snapshotSchema = () => z.object({
passwordSet: z.boolean(), passwordSet: z.boolean(),
allowedAddresses: z.array(z.string()), allowedAddresses: z.array(z.string()),
status: statusSchema(), status: statusSchema(),
logs: z.array(z.string()),
}); });
const patchSchema = () => z.object({ const patchSchema = () => z.object({
email: z.string().optional(), email: z.string().optional(),
+2
View File
@@ -47,6 +47,8 @@ export interface MailSnapshot {
readonly passwordSet: boolean; readonly passwordSet: boolean;
readonly allowedAddresses: readonly string[]; readonly allowedAddresses: readonly string[];
readonly status: MailStatus; readonly status: MailStatus;
/** 最近的运行日志(最新的在最后),用于设置页排查收信/回信问题。 */
readonly logs: readonly string[];
} }
export interface ConnectionTestResult { export interface ConnectionTestResult {
readonly ok: boolean; readonly ok: boolean;
+95 -91
View File
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import type { ChangeEvent, CSSProperties, KeyboardEvent } from 'react' import type { ChangeEvent, CSSProperties, KeyboardEvent } from 'react'
import type { MailGatewayApi } from './api.js' import type { MailGatewayApi } from './api.js'
import { StatusCard } from './StatusCard.js' import { StatusCard } from './StatusCard.js'
@@ -23,7 +23,7 @@ const table: CSSProperties = {
const cell: CSSProperties = { const cell: CSSProperties = {
borderBottom: '1px solid var(--dsh-border, #e5e7eb)', borderBottom: '1px solid var(--dsh-border, #e5e7eb)',
padding: '8px 10px', padding: '6px 8px',
verticalAlign: 'middle', verticalAlign: 'middle',
textAlign: 'left', textAlign: 'left',
} }
@@ -57,9 +57,24 @@ const primary: CSSProperties = {
} }
const muted: CSSProperties = { color: '#6b7280', fontSize: 12 } const muted: CSSProperties = { color: '#6b7280', fontSize: 12 }
const section: CSSProperties = { display: 'grid', gap: 16, maxWidth: 760 } const section: CSSProperties = { display: 'grid', gap: 16, maxWidth: 640 }
const row: CSSProperties = { display: 'flex', gap: 8, alignItems: 'center' } const row: CSSProperties = { display: 'flex', gap: 8, alignItems: 'center' }
const logBox: CSSProperties = {
margin: 0,
padding: '8px 10px',
maxHeight: 220,
overflow: 'auto',
border: '1px solid var(--dsh-border, #e5e7eb)',
borderRadius: 6,
background: 'var(--dsh-code-bg, rgba(127,127,127,0.08))',
fontSize: 12,
lineHeight: 1.6,
fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
whiteSpace: 'pre-wrap',
wordBreak: 'break-all',
}
export function MailGatewaySection({ api }: MailGatewaySectionProps) { export function MailGatewaySection({ api }: MailGatewaySectionProps) {
const [snapshot, setSnapshot] = useState<MailSnapshot>() const [snapshot, setSnapshot] = useState<MailSnapshot>()
const [mailbox, setMailbox] = useState<MailboxSettings>() const [mailbox, setMailbox] = useState<MailboxSettings>()
@@ -69,6 +84,37 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
const [test, setTest] = useState<ConnectionTestResult>() const [test, setTest] = useState<ConnectionTestResult>()
const [busy, setBusy] = useState<Busy>(null) const [busy, setBusy] = useState<Busy>(null)
const [notice, setNotice] = useState<{ kind: 'ok' | 'error'; text: string }>() const [notice, setNotice] = useState<{ kind: 'ok' | 'error'; text: string }>()
const logRef = useRef<HTMLPreElement>(null)
// 首次读取填充表单;之后每 3 秒只刷新状态与日志,不覆盖正在编辑的表单。
useEffect(() => {
let alive = true
const load = async (initial: boolean) => {
const result = await api.get()
if (!alive) return
if (!result.ok) {
if (initial) setNotice({ kind: 'error', text: result.error.message })
return
}
setSnapshot(result.value)
if (initial) {
setMailbox({ ...result.value.mailbox })
setAllowed([...result.value.allowedAddresses])
setPassword('')
}
}
void load(true)
const timer = setInterval(() => void load(false), 3000)
return () => {
alive = false
clearInterval(timer)
}
}, [api])
useEffect(() => {
const box = logRef.current
if (box) box.scrollTop = box.scrollHeight
}, [snapshot?.logs])
const applySnapshot = (next: MailSnapshot, resetMailbox: boolean) => { const applySnapshot = (next: MailSnapshot, resetMailbox: boolean) => {
setSnapshot(next) setSnapshot(next)
@@ -79,18 +125,6 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
} }
} }
useEffect(() => {
let alive = true
void api.get().then(result => {
if (!alive) return
if (result.ok) applySnapshot(result.value, true)
else setNotice({ kind: 'error', text: result.error.message })
})
return () => {
alive = false
}
}, [api])
const setField = <K extends keyof MailboxSettings>(key: K, value: MailboxSettings[K]) => { const setField = <K extends keyof MailboxSettings>(key: K, value: MailboxSettings[K]) => {
setMailbox(current => (current ? { ...current, [key]: value } : current)) setMailbox(current => (current ? { ...current, [key]: value } : current))
} }
@@ -109,7 +143,7 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
if (result.ok) { if (result.ok) {
applySnapshot(result.value, true) applySnapshot(result.value, true)
setTest(undefined) setTest(undefined)
setNotice({ kind: 'ok', text: '邮箱绑定已保存,连接已按新配置重建。' }) setNotice({ kind: 'ok', text: '已保存,按新配置重连。' })
} else { } else {
setNotice({ kind: 'error', text: result.error.message }) setNotice({ kind: 'error', text: result.error.message })
} }
@@ -152,7 +186,7 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
setBusy(null) setBusy(null)
if (result.ok) { if (result.ok) {
applySnapshot(result.value, false) applySnapshot(result.value, false)
setNotice({ kind: 'ok', text: '白名单已保存,收信策略立即生效,连接保持不变。' }) setNotice({ kind: 'ok', text: '白名单已保存。' })
} else { } else {
setNotice({ kind: 'error', text: result.error.message }) setNotice({ kind: 'error', text: result.error.message })
} }
@@ -172,6 +206,33 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
setField(key, event.target.checked) setField(key, event.target.checked)
} }
const textRow = (label: string, key: 'email' | 'displayName' | 'imapHost' | 'smtpHost' | 'username') => (
<tr>
<td style={{ ...cell, width: 110 }}>{label}</td>
<td style={cell}>
<input style={input} value={mailbox?.[key] ?? ''} onChange={textField(key)} />
</td>
</tr>
)
const portRow = (label: string, key: 'imapPort' | 'smtpPort') => (
<tr>
<td style={{ ...cell, width: 110 }}>{label}</td>
<td style={cell}>
<input style={input} value={mailbox?.[key] ?? 0} onChange={numberField(key)} />
</td>
</tr>
)
const tlsRow = (label: string, key: 'imapSecure' | 'smtpSecure') => (
<tr>
<td style={{ ...cell, width: 110 }}>{label}</td>
<td style={cell}>
<input type="checkbox" checked={mailbox?.[key] ?? true} onChange={boolField(key)} />
</td>
</tr>
)
return ( return (
<div style={section}> <div style={section}>
<StatusCard status={snapshot?.status} allowedCount={allowed.length} test={test} /> <StatusCard status={snapshot?.status} allowedCount={allowed.length} test={test} />
@@ -179,79 +240,18 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
<div> <div>
<h3 style={{ margin: '0 0 8px', fontSize: 15 }}>邮箱绑定</h3> <h3 style={{ margin: '0 0 8px', fontSize: 15 }}>邮箱绑定</h3>
<table style={table}> <table style={table}>
<thead>
<tr>
<th style={{ ...cell, ...muted, width: 140 }}>字段</th>
<th style={{ ...cell, ...muted }}>值</th>
<th style={{ ...cell, ...muted, width: 240 }}>说明</th>
</tr>
</thead>
<tbody> <tbody>
{textRow('邮箱地址', 'email')}
{textRow('显示名', 'displayName')}
{textRow('IMAP 主机', 'imapHost')}
{portRow('IMAP 端口', 'imapPort')}
{tlsRow('IMAP TLS', 'imapSecure')}
{textRow('SMTP 主机', 'smtpHost')}
{portRow('SMTP 端口', 'smtpPort')}
{tlsRow('SMTP TLS', 'smtpSecure')}
{textRow('登录用户名', 'username')}
<tr> <tr>
<td style={cell}>邮箱地址</td> <td style={{ ...cell, width: 110 }}>密码</td>
<td style={cell}>
<input style={input} value={mailbox?.email ?? ''} onChange={textField('email')} />
</td>
<td style={{ ...cell, ...muted }}>接收邮件与回信使用的地址。</td>
</tr>
<tr>
<td style={cell}>显示名</td>
<td style={cell}>
<input style={input} value={mailbox?.displayName ?? ''} onChange={textField('displayName')} />
</td>
<td style={{ ...cell, ...muted }}>回信时的发件人名称,可留空。</td>
</tr>
<tr>
<td style={cell}>IMAP 主机</td>
<td style={cell}>
<input style={input} value={mailbox?.imapHost ?? ''} onChange={textField('imapHost')} />
</td>
<td style={{ ...cell, ...muted }}>标准 IMAP 服务器地址。</td>
</tr>
<tr>
<td style={cell}>IMAP 端口</td>
<td style={cell}>
<input style={input} value={mailbox?.imapPort ?? 993} onChange={numberField('imapPort')} />
</td>
<td style={{ ...cell, ...muted }}>SSL 一般为 993。</td>
</tr>
<tr>
<td style={cell}>IMAP TLS</td>
<td style={cell}>
<input type="checkbox" checked={mailbox?.imapSecure ?? true} onChange={boolField('imapSecure')} />
</td>
<td style={{ ...cell, ...muted }}>勾选表示使用 SSL/TLS。</td>
</tr>
<tr>
<td style={cell}>SMTP 主机</td>
<td style={cell}>
<input style={input} value={mailbox?.smtpHost ?? ''} onChange={textField('smtpHost')} />
</td>
<td style={{ ...cell, ...muted }}>标准 SMTP 服务器地址。</td>
</tr>
<tr>
<td style={cell}>SMTP 端口</td>
<td style={cell}>
<input style={input} value={mailbox?.smtpPort ?? 465} onChange={numberField('smtpPort')} />
</td>
<td style={{ ...cell, ...muted }}>SSL 一般为 465。</td>
</tr>
<tr>
<td style={cell}>SMTP TLS</td>
<td style={cell}>
<input type="checkbox" checked={mailbox?.smtpSecure ?? true} onChange={boolField('smtpSecure')} />
</td>
<td style={{ ...cell, ...muted }}>勾选表示使用 SSL/TLS。</td>
</tr>
<tr>
<td style={cell}>登录用户名</td>
<td style={cell}>
<input style={input} value={mailbox?.username ?? ''} onChange={textField('username')} />
</td>
<td style={{ ...cell, ...muted }}>留空时使用邮箱地址。</td>
</tr>
<tr>
<td style={cell}>密码</td>
<td style={cell}> <td style={cell}>
<input <input
style={input} style={input}
@@ -261,7 +261,6 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
onChange={event => setPassword(event.target.value)} onChange={event => setPassword(event.target.value)}
/> />
</td> </td>
<td style={{ ...cell, ...muted }}>保存后只显示“已设置”,不会回显明文。</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@@ -272,7 +271,6 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
<button style={button} disabled={busy !== null} onClick={() => void testConnection()}> <button style={button} disabled={busy !== null} onClick={() => void testConnection()}>
{busy === 'test' ? '测试中…' : '测试连接'} {busy === 'test' ? '测试中…' : '测试连接'}
</button> </button>
<span style={muted}>保存邮箱绑定时会按新配置重建连接。</span>
</div> </div>
</div> </div>
@@ -314,10 +312,16 @@ export function MailGatewaySection({ api }: MailGatewaySectionProps) {
<button style={primary} disabled={busy !== null} onClick={() => void saveWhitelist()}> <button style={primary} disabled={busy !== null} onClick={() => void saveWhitelist()}>
{busy === 'whitelist' ? '保存中…' : '保存白名单'} {busy === 'whitelist' ? '保存中…' : '保存白名单'}
</button> </button>
<span style={muted}>同一份白名单同时校验入站发件地址与出站回复地址。</span>
</div> </div>
</div> </div>
<div>
<h3 style={{ margin: '0 0 8px', fontSize: 15 }}>运行日志</h3>
<pre ref={logRef} style={logBox}>
{snapshot?.logs?.length ? snapshot.logs.join('\n') : '暂无日志'}
</pre>
</div>
{notice ? ( {notice ? (
<div style={{ color: notice.kind === 'ok' ? '#047857' : '#b91c1c', fontSize: 13 }}> <div style={{ color: notice.kind === 'ok' ? '#047857' : '#b91c1c', fontSize: 13 }}>
{notice.text} {notice.text}
+9 -1
View File
@@ -47,6 +47,7 @@ export class MailDeliverer {
source: { kind: 'user' }, source: { kind: 'user' },
}), }),
) )
this.onLog?.(`[mail] ${job.sessionId} 已交给模型,等待回复`)
} }
dispose(): void { dispose(): void {
@@ -78,11 +79,14 @@ export class MailDeliverer {
} }
let handle: AgentHandle let handle: AgentHandle
let created = false
try { try {
handle = await this.ctx.agents.resume({ resumeSessionId: id }) handle = await this.ctx.agents.resume({ resumeSessionId: id })
} catch { } catch {
handle = await this.ctx.agents.create({ sessionId: id }) handle = await this.ctx.agents.create({ sessionId: id })
created = true
} }
this.onLog?.(`[mail] ${sessionId} 会话已${created ? '创建' : '恢复'}`)
const owned: DriveHandle = { const owned: DriveHandle = {
agent: handle.agent, agent: handle.agent,
@@ -131,7 +135,11 @@ export class MailDeliverer {
if (queue && queue.length === 0) this.pending.delete(sessionId) if (queue && queue.length === 0) this.pending.delete(sessionId)
if (!job) return if (!job) return
if (event.data.reason.kind !== 'completed' || !text) return if (event.data.reason.kind !== 'completed' || !text) {
this.onLog?.(`[mail] ${sessionId} 本轮未产生可回复文本(${event.data.reason.kind})`)
return
}
this.onLog?.(`[mail] ${sessionId} 模型已回复,准备回信 ${job.from}`)
void this.onReply(job, text).catch(error => { void this.onReply(job, text).catch(error => {
this.onLog?.(`[mail] 回信失败: ${error instanceof Error ? error.message : String(error)}`) this.onLog?.(`[mail] 回信失败: ${error instanceof Error ? error.message : String(error)}`)
}) })
+17 -4
View File
@@ -27,6 +27,7 @@ export class MailGateway {
private allowed: string[] private allowed: string[]
private accountKey = '' private accountKey = ''
private generation = 0 private generation = 0
private readonly logs: string[] = []
private status: MailStatus = { private status: MailStatus = {
phase: 'disabled', phase: 'disabled',
supportsIdle: false, supportsIdle: false,
@@ -47,11 +48,20 @@ export class MailGateway {
async (job, text) => { async (job, text) => {
if (!this.sender) throw new Error('SMTP 尚未配置') if (!this.sender) throw new Error('SMTP 尚未配置')
await this.sender.send(job, text) await this.sender.send(job, text)
this.record(`[mail] 已回信 ${job.from}`)
}, },
onLog, message => this.record(message),
) )
} }
/** 所有运行日志都进内存环形缓冲,设置页可直接查看,同时保留控制台输出。 */
private record(message: string): void {
const time = new Date().toLocaleTimeString('zh-CN', { hour12: false })
this.logs.push(`${time} ${message}`)
if (this.logs.length > 200) this.logs.splice(0, this.logs.length - 200)
this.onLog(message)
}
start(): () => Promise<void> { start(): () => Promise<void> {
this.restart() this.restart()
return async () => { return async () => {
@@ -90,6 +100,7 @@ export class MailGateway {
passwordSet: Boolean(this.mailbox.password), passwordSet: Boolean(this.mailbox.password),
allowedAddresses: [...this.allowed], allowedAddresses: [...this.allowed],
status: this.status, status: this.status,
logs: [...this.logs],
} }
} }
@@ -103,10 +114,12 @@ export class MailGateway {
if (!isMailboxConfigured(this.mailbox)) { if (!isMailboxConfigured(this.mailbox)) {
this.accountKey = '' this.accountKey = ''
this.status = { phase: 'disabled', supportsIdle: false, pollIntervalMs: POLL_INTERVAL_MS } this.status = { phase: 'disabled', supportsIdle: false, pollIntervalMs: POLL_INTERVAL_MS }
this.onLog('[mail] 未配置完整邮箱,收信入口保持关闭') this.record('[mail] 未配置完整邮箱,收信入口保持关闭')
return return
} }
this.record(`[mail] 开始连接 ${this.mailbox.imapHost}:${this.mailbox.imapPort}`)
this.accountKey = mailboxKey(this.mailbox) this.accountKey = mailboxKey(this.mailbox)
this.sender = new SmtpSender(this.mailbox) this.sender = new SmtpSender(this.mailbox)
this.receiver = new ImapReceiver( this.receiver = new ImapReceiver(
@@ -118,7 +131,7 @@ export class MailGateway {
onStatus: status => { onStatus: status => {
if (generation === this.generation) this.status = status if (generation === this.generation) this.status = status
}, },
onLog: this.onLog, onLog: message => this.record(message),
}, },
) )
this.receiver.start() this.receiver.start()
@@ -139,7 +152,7 @@ export class MailGateway {
try { try {
await this.deliverer.deliver(job) await this.deliverer.deliver(job)
} catch (error) { } catch (error) {
this.onLog(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`) this.record(`[mail] 投递 ${job.from} 失败: ${errorMessage(error)}`)
} }
} }
} finally { } finally {
+3
View File
@@ -200,6 +200,9 @@ export class ImapReceiver {
if (job) { if (job) {
this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`) this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`)
this.hooks.onJob(job) this.hooks.onJob(job)
} else {
const sender = parsed.from?.value?.[0]?.address ?? '未知发件人'
this.hooks.onLog?.(`[mail] 忽略 ${sender} 的邮件:不在白名单或缺少发件地址`)
} }
} catch (error) { } catch (error) {
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`) this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`)
+1
View File
@@ -32,6 +32,7 @@ const snapshotSchema = () =>
passwordSet: z.boolean(), passwordSet: z.boolean(),
allowedAddresses: z.array(z.string()), allowedAddresses: z.array(z.string()),
status: statusSchema(), status: statusSchema(),
logs: z.array(z.string()),
}) })
const patchSchema = () => const patchSchema = () =>
+2
View File
@@ -52,6 +52,8 @@ export interface MailSnapshot {
readonly passwordSet: boolean readonly passwordSet: boolean
readonly allowedAddresses: readonly string[] readonly allowedAddresses: readonly string[]
readonly status: MailStatus readonly status: MailStatus
/** 最近的运行日志(最新的在最后),用于设置页排查收信/回信问题。 */
readonly logs: readonly string[]
} }
export interface ConnectionTestResult { export interface ConnectionTestResult {