@morgan9668/dsh-task-notify 0.0.0-stage → 1.1.0
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/LICENSE +21 -0
- package/README.md +126 -2
- package/__tests__/client.mjs +274 -0
- package/__tests__/e2e.mjs +192 -0
- package/__tests__/settings.mjs +455 -0
- package/__tests__/unit.mjs +432 -0
- package/client.js +402 -0
- package/cordis.patch.yml +63 -0
- package/docs/DETAIL.md +76 -0
- package/index.js +450 -0
- package/locale/en.json +10 -0
- package/locale/zh.json +10 -0
- package/package.json +56 -4
- package/src/channels.js +239 -0
- package/src/render.js +207 -0
package/index.js
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @morgan9668/dsh-task-notify —— 任务结束通知。
|
|
3
|
+
*
|
|
4
|
+
* 监听 DSH 的会话事件流,在每个回合结束时把「概要 + 耗时 + token 消耗」
|
|
5
|
+
* 推送到用户配置的群机器人(企业微信 / 飞书 / 钉钉),微信通道预留。
|
|
6
|
+
*
|
|
7
|
+
* 三条硬约束:
|
|
8
|
+
* 1. fire-and-forget —— 任何发送失败只写日志,绝不向上抛,绝不阻塞 agent。
|
|
9
|
+
* 2. 不猜数据 —— token 用量只认 DSH 已提交的 `assistant/message.data.usage`;
|
|
10
|
+
* 上游没给就写「未提供」,不伪造 0。
|
|
11
|
+
* 3. 不给未实现的东西留假象 —— 微信通道没有凭据时如实回报「未配置」。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import z from '@deepseek-ai/schemastery';
|
|
15
|
+
|
|
16
|
+
import { isChannelReady, sendToChannel } from './src/channels.js';
|
|
17
|
+
import { renderNotification } from './src/render.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 不声明 inject。
|
|
21
|
+
*
|
|
22
|
+
* 插件唯一的挂载点是 `ctx.on('session/event')`,模型名只是锦上添花,
|
|
23
|
+
* 从 `session.requestHeader()` 读,读不到就留空。声明成必需服务会让
|
|
24
|
+
* 插件在缺少该服务的 profile 里停在 pending,那是自找的故障面。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const DEFAULT_REASONS = ['completed', 'error', 'blocked'];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 读会话的完整事件日志(按发生顺序,旧 → 新)。
|
|
31
|
+
*
|
|
32
|
+
* DSH 的 `Session` 类上真正的读法是 `snapshotEvents(fromSeq?, toSeqExclusive?)`;
|
|
33
|
+
* 更早的版本(0.1.0 / 0.1.1)只有 `events` getter。两条都要试,否则在新版
|
|
34
|
+
* harness 上会静默拿到 undefined——表现就是通知里概要为空、token 未提供。
|
|
35
|
+
*
|
|
36
|
+
* 任何读取失败都退化成空数组:内容降级可以接受,把异常抛给 harness 不行。
|
|
37
|
+
*/
|
|
38
|
+
export function sessionEvents(session) {
|
|
39
|
+
try {
|
|
40
|
+
if (typeof session?.snapshotEvents === 'function') {
|
|
41
|
+
const snapshot = session.snapshotEvents();
|
|
42
|
+
if (Array.isArray(snapshot)) return snapshot;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(session?.events)) return session.events;
|
|
45
|
+
} catch {
|
|
46
|
+
/* 落到空日志 */
|
|
47
|
+
}
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Config 字段**故意不用** `.volatile()`。
|
|
53
|
+
*
|
|
54
|
+
* 这不是偷懒,是被 DSH 的实现逼出来的选择:`configEditor.edit()` 的第一件事是
|
|
55
|
+
*
|
|
56
|
+
* const current = structuredClone(entry.options.config ?? {})
|
|
57
|
+
*
|
|
58
|
+
* 而 `.volatile()` 产出的是带 `get()` 函数的活引用对象,函数不可结构化克隆,
|
|
59
|
+
* 所以配置里只要有一个 volatile 字段,`structuredClone` 就抛
|
|
60
|
+
* `DataCloneError: () => v could not be cloned.`——**设置页任何写入都会失败**,
|
|
61
|
+
* 表现就是「勾选框点不动、保存没反应」。
|
|
62
|
+
*
|
|
63
|
+
* 代价:非 volatile 字段改完要重启 DSH 才生效。这个代价可以接受,
|
|
64
|
+
* 因为一个存不进去的即时生效毫无意义。
|
|
65
|
+
*/
|
|
66
|
+
export const Config = z.object({
|
|
67
|
+
enabled: z.boolean().default(true),
|
|
68
|
+
reasons: z.array(z.string()).default(DEFAULT_REASONS),
|
|
69
|
+
notifySubagents: z.boolean().default(false),
|
|
70
|
+
maxExcerptChars: z.number().min(50).max(2000).default(400),
|
|
71
|
+
timeoutMs: z.number().min(1000).max(30000).default(8000),
|
|
72
|
+
channels: z.object({
|
|
73
|
+
wechat: z.object({
|
|
74
|
+
enabled: z.boolean().default(false),
|
|
75
|
+
serverchanSendKey: z.string().default(''),
|
|
76
|
+
wxpusherAppToken: z.string().default(''),
|
|
77
|
+
}),
|
|
78
|
+
wecom: z.object({
|
|
79
|
+
enabled: z.boolean().default(false),
|
|
80
|
+
webhook: z.string().default(''),
|
|
81
|
+
}),
|
|
82
|
+
feishu: z.object({
|
|
83
|
+
enabled: z.boolean().default(false),
|
|
84
|
+
webhook: z.string().default(''),
|
|
85
|
+
secret: z.string().default(''),
|
|
86
|
+
}),
|
|
87
|
+
dingtalk: z.object({
|
|
88
|
+
enabled: z.boolean().default(false),
|
|
89
|
+
webhook: z.string().default(''),
|
|
90
|
+
secret: z.string().default(''),
|
|
91
|
+
}),
|
|
92
|
+
}),
|
|
93
|
+
}).description('任务完成通知');
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 兼容旧的调用点:字段已不再是活引用,这里直接原样返回。
|
|
97
|
+
*
|
|
98
|
+
* 保留这个函数而不是删掉它,是因为测试与路由都按名字调用它;
|
|
99
|
+
* 删掉会让"解引用"这件事在代码里失去唯一的落点,将来有人再加
|
|
100
|
+
* volatile 字段时不知道要在这里处理。
|
|
101
|
+
*/
|
|
102
|
+
export function resolveConfig(raw) {
|
|
103
|
+
return raw;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 归一化配置:把 patch 里可能缺省/写错的字段收敛成可用形状。 */
|
|
107
|
+
export function normalizeConfig(raw) {
|
|
108
|
+
const config = raw && typeof raw === 'object' ? raw : {};
|
|
109
|
+
const channels = config.channels && typeof config.channels === 'object' ? config.channels : {};
|
|
110
|
+
|
|
111
|
+
const pickString = (value) => (typeof value === 'string' ? value.trim() : '');
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
enabled: config.enabled !== false,
|
|
115
|
+
reasons: Array.isArray(config.reasons) && config.reasons.length > 0
|
|
116
|
+
? config.reasons.filter((entry) => typeof entry === 'string')
|
|
117
|
+
: DEFAULT_REASONS,
|
|
118
|
+
notifySubagents: config.notifySubagents === true,
|
|
119
|
+
maxExcerptChars: Number.isSafeInteger(config.maxExcerptChars) && config.maxExcerptChars > 0
|
|
120
|
+
? config.maxExcerptChars
|
|
121
|
+
: 400,
|
|
122
|
+
timeoutMs: Number.isSafeInteger(config.timeoutMs) && config.timeoutMs > 0
|
|
123
|
+
? config.timeoutMs
|
|
124
|
+
: 8000,
|
|
125
|
+
channels: {
|
|
126
|
+
wechat: {
|
|
127
|
+
enabled: channels.wechat?.enabled === true,
|
|
128
|
+
serverchanSendKey: pickString(channels.wechat?.serverchanSendKey),
|
|
129
|
+
wxpusherAppToken: pickString(channels.wechat?.wxpusherAppToken),
|
|
130
|
+
},
|
|
131
|
+
wecom: {
|
|
132
|
+
enabled: channels.wecom?.enabled === true,
|
|
133
|
+
webhook: pickString(channels.wecom?.webhook),
|
|
134
|
+
},
|
|
135
|
+
feishu: {
|
|
136
|
+
enabled: channels.feishu?.enabled === true,
|
|
137
|
+
webhook: pickString(channels.feishu?.webhook),
|
|
138
|
+
secret: pickString(channels.feishu?.secret),
|
|
139
|
+
},
|
|
140
|
+
dingtalk: {
|
|
141
|
+
enabled: channels.dingtalk?.enabled === true,
|
|
142
|
+
webhook: pickString(channels.dingtalk?.webhook),
|
|
143
|
+
secret: pickString(channels.dingtalk?.secret),
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 当前真正可发送的渠道列表。 */
|
|
150
|
+
export function readyChannels(config) {
|
|
151
|
+
const kinds = ['wecom', 'feishu', 'dingtalk', 'wechat'];
|
|
152
|
+
return kinds.filter((kind) => isChannelReady(config.channels[kind], kind));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function apply(ctx, rawConfig) {
|
|
156
|
+
const log = (message) => {
|
|
157
|
+
// 桌面端把 stderr 收进自己的日志;不依赖 ctx.logger 这个未承诺的字段。
|
|
158
|
+
try {
|
|
159
|
+
ctx.logger?.info?.(message);
|
|
160
|
+
} catch {
|
|
161
|
+
/* logger 不可用就退回 stderr */
|
|
162
|
+
}
|
|
163
|
+
process.stderr.write(`[task-notify] ${message}\n`);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 每次都重新读配置,而不是在 apply 时快照成常量。
|
|
168
|
+
*
|
|
169
|
+
* 字段不是 volatile,所以设置页写入后不会原地改变已加载的配置对象;
|
|
170
|
+
* 但每次读 `rawConfig` 仍然有意义:DSH 在 reconcile 时会更新这一层,
|
|
171
|
+
* 而「读的那一刻取值」总比「加载时取一次」更接近用户刚保存的内容。
|
|
172
|
+
*
|
|
173
|
+
* 真正让改动生效的是 DSH 的 reconcile:普通字段会重启插件,`apply`
|
|
174
|
+
* 拿到新的 config 重新挂载。这一层不需要自己缓存。
|
|
175
|
+
*/
|
|
176
|
+
const currentConfig = () => normalizeConfig(resolveConfig(rawConfig));
|
|
177
|
+
|
|
178
|
+
{
|
|
179
|
+
const config = currentConfig();
|
|
180
|
+
if (!config.enabled) {
|
|
181
|
+
log('已禁用(enabled: false),不发送任何通知');
|
|
182
|
+
} else {
|
|
183
|
+
const ready = readyChannels(config);
|
|
184
|
+
if (ready.length === 0) {
|
|
185
|
+
log('未配置任何可用渠道;插件已激活但不发送通知。请在设置里填写企业微信 / 飞书 / 钉钉的 Webhook。');
|
|
186
|
+
} else {
|
|
187
|
+
log(`已启用,可用渠道:${ready.join('、')}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** 每个会话的回合开始时刻与 turn 号,用于算耗时。 */
|
|
193
|
+
const turnStarts = new Map();
|
|
194
|
+
/** 已发送过的事件指纹,避免同一回合重复推送。 */
|
|
195
|
+
const delivered = new Set();
|
|
196
|
+
|
|
197
|
+
const modelLabelOf = (session) => {
|
|
198
|
+
try {
|
|
199
|
+
const selection = session?.requestHeader?.()?.config;
|
|
200
|
+
if (selection?.provider && selection?.model) return `${selection.provider}/${selection.model}`;
|
|
201
|
+
} catch {
|
|
202
|
+
/* 调用方折叠失败就退回空串 */
|
|
203
|
+
}
|
|
204
|
+
return '';
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/** 把一条已渲染的消息发到当前所有可用渠道。 */
|
|
208
|
+
const deliver = async (rendered) => {
|
|
209
|
+
const config = currentConfig();
|
|
210
|
+
if (!config.enabled) return [];
|
|
211
|
+
const ready = readyChannels(config);
|
|
212
|
+
if (ready.length === 0) return [];
|
|
213
|
+
|
|
214
|
+
const results = await Promise.all(
|
|
215
|
+
ready.map((kind) =>
|
|
216
|
+
sendToChannel(config.channels[kind], kind, rendered, {
|
|
217
|
+
timeoutMs: config.timeoutMs,
|
|
218
|
+
log,
|
|
219
|
+
}),
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
for (const result of results) {
|
|
224
|
+
if (result.ok) log(`${result.channel} 通知已送达`);
|
|
225
|
+
else log(`${result.channel} 通知未送达:${result.detail ?? '未知原因'}`);
|
|
226
|
+
}
|
|
227
|
+
return results;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const dispatch = async ({ session, events, reasonKind, durationMs, turn }) => {
|
|
231
|
+
const config = currentConfig();
|
|
232
|
+
const rendered = renderNotification({
|
|
233
|
+
reasonKind,
|
|
234
|
+
durationMs,
|
|
235
|
+
events,
|
|
236
|
+
// turn 由调用方显式传入。之前这里从 turnStarts 里再取一次,
|
|
237
|
+
// 而调用方在 dispatch 之前已经把它删了,于是恒为 undefined,
|
|
238
|
+
// 回合过滤失效 → 统计了整场会话的累计 token。
|
|
239
|
+
turn,
|
|
240
|
+
maxExcerptChars: config.maxExcerptChars,
|
|
241
|
+
modelLabel: modelLabelOf(session),
|
|
242
|
+
});
|
|
243
|
+
await deliver(rendered);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// 事件订阅:整个插件唯一的外部挂载点。
|
|
247
|
+
const dispose = ctx.on('session/event', (session, event) => {
|
|
248
|
+
if (event?.type === 'turn/start') {
|
|
249
|
+
turnStarts.set(session?.id, { at: Date.now(), turn: event?.data?.turn });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (event?.type !== 'turn/end') return;
|
|
254
|
+
|
|
255
|
+
// 每个事件都读最新配置,设置卡一改即生效。
|
|
256
|
+
const config = currentConfig();
|
|
257
|
+
if (!config.enabled) return;
|
|
258
|
+
|
|
259
|
+
const depth = session?.header?.delegationDepth ?? 0;
|
|
260
|
+
if (!config.notifySubagents && depth > 0) return;
|
|
261
|
+
|
|
262
|
+
const reasonKind = event?.data?.reason?.kind;
|
|
263
|
+
if (!config.reasons.includes(reasonKind)) return;
|
|
264
|
+
|
|
265
|
+
const key = `${session?.id}\u0000${event?.data?.turn ?? ''}\u0000${reasonKind}`;
|
|
266
|
+
if (delivered.has(key)) return;
|
|
267
|
+
delivered.add(key);
|
|
268
|
+
// 上限保护:长会话不无限增长。
|
|
269
|
+
if (delivered.size > 512) {
|
|
270
|
+
const oldest = delivered.values().next().value;
|
|
271
|
+
delivered.delete(oldest);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const started = turnStarts.get(session?.id);
|
|
275
|
+
const durationMs = started?.at ? Date.now() - started.at : Number.NaN;
|
|
276
|
+
// turn 号优先取 turn/start 记下的那个;没有就退回 turn/end 自带的值,
|
|
277
|
+
// 保证「只统计本轮」这条过滤永远有值可用。
|
|
278
|
+
const turn = started?.turn ?? event?.data?.turn;
|
|
279
|
+
turnStarts.delete(session?.id);
|
|
280
|
+
|
|
281
|
+
// 读本会话的完整事件日志。这是整个插件最容易出错的一处:
|
|
282
|
+
// · `collectEvents()` 是我想当然写的方法,Session 上根本不存在,
|
|
283
|
+
// 之前因此恒返回空数组,通知里的概要与 token 全是「未提供」。
|
|
284
|
+
// · 真实 API 是 `snapshotEvents(fromSeq?, toSeqExclusive?)`,返回
|
|
285
|
+
// 只读事件数组(旧 → 新)。
|
|
286
|
+
// · 老版本 DSH(0.1.0/0.1.1)只有 `events` getter,所以两条都试。
|
|
287
|
+
// 读日志失败按「空日志」处理:内容降级可以,把异常抛给 harness 不行。
|
|
288
|
+
const events = sessionEvents(session);
|
|
289
|
+
|
|
290
|
+
// fire-and-forget
|
|
291
|
+
void dispatch({ session, events, reasonKind, durationMs, turn });
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 设置页通过这三条同源路由读写配置、发测试通知。
|
|
296
|
+
*
|
|
297
|
+
* 为什么用 HTTP 而不是 configForms 的 scope:our-free-model 用的就是这套
|
|
298
|
+
* (`/api/our-free-model/*`),路由由同一个 host 进程提供,不依赖客户端
|
|
299
|
+
* 是否绑定到 scope。代价是保存时要自己调 configEditor。
|
|
300
|
+
*
|
|
301
|
+
* 三条路由都用**精确路径**,不用前缀:web server 按最长前缀匹配,一个
|
|
302
|
+
* 覆盖 `/api/task-notify` 的前缀会把它下面所有路径(含未来的)一次性吃掉,
|
|
303
|
+
* 排查起来比多写两行还贵。
|
|
304
|
+
*/
|
|
305
|
+
const API_PREFIX = '/api/task-notify';
|
|
306
|
+
const sendTest = async () => {
|
|
307
|
+
return await deliver({
|
|
308
|
+
title: '任务通知测试',
|
|
309
|
+
body: [
|
|
310
|
+
'**✅ 这是一条测试通知**',
|
|
311
|
+
'',
|
|
312
|
+
'**📋 任务概要**',
|
|
313
|
+
'> 收到这条消息,说明渠道配置正确。',
|
|
314
|
+
'',
|
|
315
|
+
'**⏱️ 耗时**',
|
|
316
|
+
'> 3分12秒',
|
|
317
|
+
'',
|
|
318
|
+
'**🔢 token 消耗**',
|
|
319
|
+
'> 输入 12,340 输出 2,891',
|
|
320
|
+
'> 合计 **15,231**',
|
|
321
|
+
].join('\n'),
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 把一个 { path, value } 写回配置。
|
|
327
|
+
*
|
|
328
|
+
* path 形如 `['channels','wecom','webhook']`。写入走 configEditor,
|
|
329
|
+
* 它保留文件其余内容、校验候选值、并与其他配置改动串行化。
|
|
330
|
+
*
|
|
331
|
+
* `current` 已经是 configEditor 自己 `structuredClone` 过的普通对象,
|
|
332
|
+
* 这里直接改它再返回即可。**不要**再克隆一次:这一层多一次克隆没有任何
|
|
333
|
+
* 收益,而一旦将来有人给 Config 加回 volatile 字段,它就会变成新的
|
|
334
|
+
* DataCloneError 来源——那正是之前「勾选框点不动」的根因。
|
|
335
|
+
*/
|
|
336
|
+
const writeSetting = async (path, value) => {
|
|
337
|
+
if (!Array.isArray(path) || path.length === 0) {
|
|
338
|
+
throw new Error('path 必须是非空数组');
|
|
339
|
+
}
|
|
340
|
+
const entry = ctx.fiber?.entry;
|
|
341
|
+
const editor = typeof ctx.get === 'function' ? ctx.get('configEditor') : undefined;
|
|
342
|
+
if (!entry || !editor || typeof editor.edit !== 'function') {
|
|
343
|
+
throw new Error('当前 profile 无法保存配置(找不到 configEditor)');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
await editor.edit(entry, (current) => {
|
|
347
|
+
const next = current && typeof current === 'object' && !Array.isArray(current) ? current : {};
|
|
348
|
+
// 逐层建对象,最后落值。
|
|
349
|
+
let cursor = next;
|
|
350
|
+
for (let index = 0; index < path.length - 1; index += 1) {
|
|
351
|
+
const key = path[index];
|
|
352
|
+
if (!cursor[key] || typeof cursor[key] !== 'object' || Array.isArray(cursor[key])) {
|
|
353
|
+
cursor[key] = {};
|
|
354
|
+
}
|
|
355
|
+
cursor = cursor[key];
|
|
356
|
+
}
|
|
357
|
+
cursor[path[path.length - 1]] = value;
|
|
358
|
+
return next;
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
return currentConfig();
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const readBody = async (request) => {
|
|
365
|
+
const chunks = [];
|
|
366
|
+
for await (const chunk of request) chunks.push(chunk);
|
|
367
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
368
|
+
if (text.trim() === '') return {};
|
|
369
|
+
return JSON.parse(text);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const json = (response, status, payload) => {
|
|
373
|
+
response.writeHead(status, {
|
|
374
|
+
'content-type': 'application/json; charset=utf-8',
|
|
375
|
+
'cache-control': 'no-store',
|
|
376
|
+
});
|
|
377
|
+
response.end(JSON.stringify(payload));
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// 用 ctx.get 而不是 ctx.webServer 直取:直取需要声明 `inject = ['webServer']`,
|
|
381
|
+
// 那会让插件在没有 web 服务的 profile 里停在 pending。
|
|
382
|
+
const server = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined;
|
|
383
|
+
if (server && typeof server.register === 'function') {
|
|
384
|
+
const registerRoute = (path, handler) => {
|
|
385
|
+
try {
|
|
386
|
+
ctx.effect?.(() =>
|
|
387
|
+
server.register({
|
|
388
|
+
kind: 'exact',
|
|
389
|
+
path,
|
|
390
|
+
handler: (request, response) => {
|
|
391
|
+
void (async () => {
|
|
392
|
+
try {
|
|
393
|
+
await handler(request, response);
|
|
394
|
+
} catch (error) {
|
|
395
|
+
json(response, 500, {
|
|
396
|
+
error: error instanceof Error ? error.message : String(error),
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
})();
|
|
400
|
+
},
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
|
+
} catch (error) {
|
|
404
|
+
log(`路由 ${path} 注册失败:${error instanceof Error ? error.message : String(error)}`);
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// 读配置
|
|
409
|
+
registerRoute(`${API_PREFIX}/settings`, async (request, response) => {
|
|
410
|
+
json(response, 200, { settings: currentConfig() });
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// 写一个字段
|
|
414
|
+
registerRoute(`${API_PREFIX}/settings`, async (request, response) => {
|
|
415
|
+
if (request.method !== 'POST') {
|
|
416
|
+
json(response, 405, { error: '只接受 POST' });
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const body = await readBody(request);
|
|
420
|
+
const settings = await writeSetting(body.path, body.value);
|
|
421
|
+
log(`设置已更新:${Array.isArray(body.path) ? body.path.join('.') : '?'} = ${
|
|
422
|
+
typeof body.value === 'string' ? (body.value === '' ? '(空)' : '(已填)') : body.value
|
|
423
|
+
}`);
|
|
424
|
+
json(response, 200, { ok: true, settings });
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
// 发送测试通知
|
|
428
|
+
registerRoute(`${API_PREFIX}/test`, async (request, response) => {
|
|
429
|
+
let results = [];
|
|
430
|
+
try {
|
|
431
|
+
results = await sendTest();
|
|
432
|
+
} catch (error) {
|
|
433
|
+
results = [
|
|
434
|
+
{ ok: false, channel: 'host', detail: error instanceof Error ? error.message : String(error) },
|
|
435
|
+
];
|
|
436
|
+
}
|
|
437
|
+
json(response, 200, { ok: results.every((entry) => entry.ok), results });
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
dispose,
|
|
443
|
+
/** 返回当前可用渠道(宿主侧可读状态)。 */
|
|
444
|
+
async refresh() {
|
|
445
|
+
return readyChannels(currentConfig());
|
|
446
|
+
},
|
|
447
|
+
/** 发送一条测试通知,用于验证 Webhook 是否配通。 */
|
|
448
|
+
sendTest,
|
|
449
|
+
};
|
|
450
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Task completion notifier",
|
|
4
|
+
"description": "After each turn ends, pushes a task summary, elapsed time and token usage to a WeCom, Feishu or DingTalk group bot. The WeChat channel keeps ServerChan and WxPusher credential slots.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
},
|
|
7
|
+
"taskNotify": {
|
|
8
|
+
"title": "Task completion notifier"
|
|
9
|
+
}
|
|
10
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morgan9668/dsh-task-notify",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "任务完成通知:每个回合结束后,把任务概要、耗时与 token 消耗推送到飞书 / 钉钉 / 企业微信群机器人(微信通道预留)。配置页内嵌在 DSH 设置里。",
|
|
6
|
+
"author": "chenzhao",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./client": "./client.js",
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
"./locale/*.json": "./locale/*.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"client.js",
|
|
17
|
+
"src",
|
|
18
|
+
"cordis.patch.yml",
|
|
19
|
+
"locale",
|
|
20
|
+
"docs",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"__tests__"
|
|
24
|
+
],
|
|
25
|
+
"dsh": {
|
|
26
|
+
"bundle": {
|
|
27
|
+
"patch": "./cordis.patch.yml"
|
|
28
|
+
},
|
|
29
|
+
"client": {
|
|
30
|
+
"platform": "web"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": "^22.19.0 || >=24.0.0",
|
|
35
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0-0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@deepseek-ai/schemastery": "^3.18.4"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"test": "node __tests__/unit.mjs && node __tests__/settings.mjs && node __tests__/client.mjs && node __tests__/e2e.mjs"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"dsh-plugin",
|
|
45
|
+
"deepseek-harness",
|
|
46
|
+
"notification",
|
|
47
|
+
"webhook",
|
|
48
|
+
"feishu",
|
|
49
|
+
"dingtalk",
|
|
50
|
+
"wecom",
|
|
51
|
+
"token-usage"
|
|
52
|
+
],
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
}
|
|
58
|
+
}
|