@mysterycode-liwen/dsh-mail 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tools.js ADDED
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Model-facing mailbox query tool.
3
+ *
4
+ * This tool is intentionally READ-ONLY: it never sends, deletes, or changes
5
+ * mail, and it never exposes credentials (auth codes, passwords). Every call
6
+ * is gated by the `agentMailAccess` setting — when off, the tool returns an
7
+ * explicit "disabled" status and performs no mailbox access at all.
8
+ *
9
+ * All mailbox work is delegated to the plugin's existing request handler via a
10
+ * synthetic Request, so the agent and the sidebar UI share one code path.
11
+ *
12
+ * NOTE: The definition below uses raw JSON Schema directly (the subset that
13
+ * `ctx.tools.register` accepts). We deliberately avoid importing `defineTool`
14
+ * from `@deepseek-ai/dsh-tools` because that package pulls in a deep dependency
15
+ * chain (cordis, dsh-llm, dsh-scope, dsh-util-values, ...) that is not resolvable
16
+ * from a local-link plugin. `defineTool` is only a DSL-to-JSON-Schema converter;
17
+ * a plain ToolDefinition is registered identically.
18
+ */
19
+ export function mailQueryTool(ctx, handleRequest, isEnabled) {
20
+ // Gate is asynchronous: the authoritative switch lives in the credential
21
+ // store (same source as the Settings UI), so we read it on every call through
22
+ // the provided predicate. No access is granted unless it returns true.
23
+ const gate = async () => (typeof isEnabled === 'function' && (await isEnabled()) === true);
24
+
25
+ function disabledValue() {
26
+ return {
27
+ status: 'disabled',
28
+ message: '邮箱查询未开启。请到插件设置中打开“允许 AI 助手查询邮箱内容”开关后再试。/ Mailbox query is disabled. Enable "Allow the AI assistant to query mailbox content" in the plugin settings first.',
29
+ };
30
+ }
31
+
32
+ async function call(op, extra) {
33
+ const url = 'http://dsh-mail.local/api/dsh-mail?op=' + encodeURIComponent(op);
34
+ const request = new Request(url, {
35
+ method: 'POST',
36
+ headers: { 'content-type': 'application/json' },
37
+ body: JSON.stringify({ op, ...extra }),
38
+ });
39
+ const response = await handleRequest(ctx, request);
40
+ return response.json();
41
+ }
42
+
43
+ function render(_args, value) {
44
+ if (!value || typeof value !== 'object') {
45
+ return [{ type: 'text', text: String(value) }];
46
+ }
47
+ if (value.status === 'disabled' || value.status === 'error') {
48
+ return [{ type: 'text', text: value.message || String(value.status) }];
49
+ }
50
+ return [{ type: 'text', text: JSON.stringify(value, null, 2) }];
51
+ }
52
+
53
+ return {
54
+ name: 'mail_query',
55
+ description: 'Query the configured mailboxes (read-only). Requires the plugin setting "Allow the AI assistant to query mailbox content" to be enabled; otherwise returns a disabled status. Actions: list_accounts (list configured accounts; no secrets), list_messages (list recent messages in a folder), get_message (read one message by uid), sent_messages (recent sent mail). Never sends, deletes, or modifies mail and never returns passwords or authorization codes.',
56
+ parameters: {
57
+ type: 'object',
58
+ properties: {
59
+ action: {
60
+ type: 'string',
61
+ description: 'Query to run: list_accounts | list_messages | get_message | sent_messages.',
62
+ enum: ['list_accounts', 'list_messages', 'get_message', 'sent_messages'],
63
+ },
64
+ accountId: {
65
+ type: 'string',
66
+ description: 'Account id from list_accounts. Required for list_messages, get_message, and sent_messages.',
67
+ },
68
+ folder: {
69
+ type: 'string',
70
+ description: 'IMAP folder to read. Defaults to INBOX. Only used by list_messages and get_message.',
71
+ },
72
+ limit: {
73
+ type: 'integer',
74
+ description: 'Maximum number of messages to return (1-100). Defaults to 20. Only used by list_messages and sent_messages.',
75
+ },
76
+ uid: {
77
+ type: 'string',
78
+ description: 'Message uid to read, from list_messages. Required for get_message.',
79
+ },
80
+ },
81
+ required: ['action'],
82
+ },
83
+ output: {
84
+ schema: {
85
+ type: 'object',
86
+ properties: {
87
+ status: { type: 'string', description: 'disabled | error | ok' },
88
+ message: { type: 'string', description: 'Human-readable message when status is disabled or error.' },
89
+ data: { description: 'Query payload when status is ok.' },
90
+ },
91
+ },
92
+ render,
93
+ },
94
+ async execute(args, exec) {
95
+ if (!(await gate())) return disabledValue();
96
+
97
+ try {
98
+ const a = args || {};
99
+ let data;
100
+ switch (a.action) {
101
+ case 'list_accounts': {
102
+ data = await call('listAccounts', {});
103
+ break;
104
+ }
105
+ case 'list_messages': {
106
+ if (!a.accountId) {
107
+ return { status: 'error', message: 'accountId is required for list_messages.' };
108
+ }
109
+ const limit = Math.max(1, Math.min(100, Number(a.limit) || 20));
110
+ data = await call('listMessages', {
111
+ id: a.accountId,
112
+ folder: a.folder || 'INBOX',
113
+ limit,
114
+ });
115
+ break;
116
+ }
117
+ case 'get_message': {
118
+ if (!a.accountId) {
119
+ return { status: 'error', message: 'accountId is required for get_message.' };
120
+ }
121
+ if (!a.uid) {
122
+ return { status: 'error', message: 'uid is required for get_message.' };
123
+ }
124
+ data = await call('getMessage', {
125
+ id: a.accountId,
126
+ uid: a.uid,
127
+ folder: a.folder || 'INBOX',
128
+ });
129
+ break;
130
+ }
131
+ case 'sent_messages': {
132
+ if (!a.accountId) {
133
+ return { status: 'error', message: 'accountId is required for sent_messages.' };
134
+ }
135
+ data = await call('sentMessages', { id: a.accountId });
136
+ break;
137
+ }
138
+ default:
139
+ return { status: 'error', message: 'Unknown action: ' + a.action };
140
+ }
141
+
142
+ if (!data || data.ok === false) {
143
+ const message = data && data.error ? String(data.error) : 'Mailbox query failed.';
144
+ return { status: 'error', message };
145
+ }
146
+ // Strip server envelope; keep only the payload the UI would receive.
147
+ const { ok, ...payload } = data;
148
+ return { status: 'ok', data: payload };
149
+ } catch (error) {
150
+ if (exec && exec.signal && exec.signal.aborted) throw error;
151
+ return {
152
+ status: 'error',
153
+ message: error instanceof Error ? error.message : String(error),
154
+ };
155
+ }
156
+ },
157
+ };
158
+ }