@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
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 设置页机制测试:验证「声明了 Config」「配置改了立刻生效」这两件事。
|
|
4
|
+
*
|
|
5
|
+
* 这两点是设置卡能出现、并且不会出现「保存了但没生效」的前提。
|
|
6
|
+
*
|
|
7
|
+
* node __tests__/settings.mjs
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
|
|
12
|
+
import { Config, resolveConfig, normalizeConfig, readyChannels, apply } from '../index.js';
|
|
13
|
+
|
|
14
|
+
let failures = 0;
|
|
15
|
+
const queue = [];
|
|
16
|
+
const test = (name, fn) => queue.push({ name, fn });
|
|
17
|
+
|
|
18
|
+
async function run() {
|
|
19
|
+
for (const { name, fn } of queue) {
|
|
20
|
+
try {
|
|
21
|
+
await fn();
|
|
22
|
+
console.log(`✓ ${name}`);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
failures += 1;
|
|
25
|
+
console.log(`✗ ${name}`);
|
|
26
|
+
console.log(` ${error instanceof Error ? error.message : String(error)}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
console.log(failures === 0 ? '\n全部通过' : `\n${failures} 项失败`);
|
|
30
|
+
process.exit(failures === 0 ? 0 : 1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* ------------------------- Config 声明 ------------------------- */
|
|
34
|
+
|
|
35
|
+
test('导出了 Config(设置卡存在的前提)', () => {
|
|
36
|
+
assert.ok(Config, '必须导出 Config');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('Config 能被 schemastery 校验并给出默认值', () => {
|
|
40
|
+
// 字段是 volatile 活引用,所以先解引用再断言默认值。
|
|
41
|
+
const parsed = resolveConfig(Config({}));
|
|
42
|
+
assert.equal(parsed.enabled, true);
|
|
43
|
+
assert.deepEqual(parsed.reasons, ['completed', 'error', 'blocked']);
|
|
44
|
+
assert.equal(parsed.notifySubagents, false);
|
|
45
|
+
assert.equal(parsed.maxExcerptChars, 400);
|
|
46
|
+
assert.equal(parsed.timeoutMs, 8000);
|
|
47
|
+
assert.ok(parsed.channels, 'channels 必须有默认值');
|
|
48
|
+
assert.equal(parsed.channels.wecom.enabled, false);
|
|
49
|
+
assert.equal(parsed.channels.wecom.webhook, '');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('Config 覆盖四个渠道,微信也占位', () => {
|
|
53
|
+
const parsed = resolveConfig(Config({}));
|
|
54
|
+
for (const kind of ['wechat', 'wecom', 'feishu', 'dingtalk']) {
|
|
55
|
+
assert.ok(parsed.channels[kind], `缺渠道 ${kind}`);
|
|
56
|
+
assert.equal(typeof parsed.channels[kind].enabled, 'boolean');
|
|
57
|
+
}
|
|
58
|
+
assert.equal(typeof parsed.channels.wechat.serverchanSendKey, 'string');
|
|
59
|
+
assert.equal(typeof parsed.channels.wechat.wxpusherAppToken, 'string');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('Config 会拒绝超范围的数值', () => {
|
|
63
|
+
assert.throws(() => Config({ maxExcerptChars: 5 }), '过小的摘要上限应被拒绝');
|
|
64
|
+
assert.throws(() => Config({ timeoutMs: 100 }), '过小的超时应被拒绝');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/* ----------------------- 活引用解引用 ----------------------- */
|
|
68
|
+
|
|
69
|
+
/** 造一个假的 volatile 活引用,行为与 cosmokit 一致。 */
|
|
70
|
+
function makeLiveRef(initial) {
|
|
71
|
+
const key = Symbol.for('cosmokit.volatile.write');
|
|
72
|
+
const holder = { [key]: { path: ['x'] }, __value: initial };
|
|
73
|
+
holder.get = () => holder.__value;
|
|
74
|
+
holder.set = (next) => {
|
|
75
|
+
holder.__value = next;
|
|
76
|
+
};
|
|
77
|
+
return holder;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
test('Config 产物必须是可 structuredClone 的纯对象(这是设置页能存的前提)', () => {
|
|
81
|
+
// configEditor.edit 第一件事就是 structuredClone(entry.options.config)。
|
|
82
|
+
// 只要 Config 里有 volatile 活引用(含 get 函数),这一步就抛
|
|
83
|
+
// DataCloneError,表现是「勾选框点不动」。这条测试守住那个回归。
|
|
84
|
+
const raw = Config({});
|
|
85
|
+
assert.doesNotThrow(() => structuredClone(raw), 'Config 产物必须可克隆');
|
|
86
|
+
assert.doesNotThrow(() => structuredClone({ ...raw, channels: { ...raw.channels } }), '嵌套也要可克隆');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('Config 里不出现带函数的活引用', () => {
|
|
90
|
+
const raw = Config({});
|
|
91
|
+
const walk = (value, path) => {
|
|
92
|
+
if (value === null || typeof value !== 'object') return;
|
|
93
|
+
assert.notEqual(
|
|
94
|
+
typeof value.get,
|
|
95
|
+
'function',
|
|
96
|
+
`${path} 看起来是 volatile 活引用,会让设置写入抛 DataCloneError`,
|
|
97
|
+
);
|
|
98
|
+
for (const [key, child] of Object.entries(value)) walk(child, `${path}.${key}`);
|
|
99
|
+
};
|
|
100
|
+
walk(raw, 'config');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('resolveConfig 对普通对象原样返回', () => {
|
|
104
|
+
const raw = { enabled: true, channels: { wecom: { enabled: true, webhook: 'https://h' } } };
|
|
105
|
+
assert.deepEqual(resolveConfig(raw), raw);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/* ------------------- 配置改完不重启也生效 ------------------- */
|
|
109
|
+
|
|
110
|
+
test('关总开关后 turn/end 不再发送', async () => {
|
|
111
|
+
const calls = [];
|
|
112
|
+
const originalFetch = globalThis.fetch;
|
|
113
|
+
globalThis.fetch = async (url, init) => {
|
|
114
|
+
calls.push({ url, body: init?.body });
|
|
115
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
116
|
+
};
|
|
117
|
+
try {
|
|
118
|
+
const handlers = [];
|
|
119
|
+
const ctx = {
|
|
120
|
+
on: (event, handler) => {
|
|
121
|
+
if (event === 'session/event') handlers.push(handler);
|
|
122
|
+
return () => {};
|
|
123
|
+
},
|
|
124
|
+
logger: { info: () => {}, warn: () => {} },
|
|
125
|
+
get: () => undefined,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const session = {
|
|
129
|
+
id: 's1',
|
|
130
|
+
header: { delegationDepth: 0 },
|
|
131
|
+
requestHeader: () => ({ config: { provider: 'p', model: 'm' } }),
|
|
132
|
+
collectEvents: () => [],
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// 启用的配置:应当发送
|
|
136
|
+
apply(ctx, {
|
|
137
|
+
enabled: true,
|
|
138
|
+
reasons: ['completed'],
|
|
139
|
+
channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
140
|
+
});
|
|
141
|
+
handlers[0](session, { type: 'turn/start', data: { turn: 1 } });
|
|
142
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } });
|
|
143
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
144
|
+
assert.equal(calls.length, 1, '启用时应发送');
|
|
145
|
+
|
|
146
|
+
// 关掉总开关:不发送
|
|
147
|
+
handlers.length = 0;
|
|
148
|
+
apply(ctx, {
|
|
149
|
+
enabled: false,
|
|
150
|
+
reasons: ['completed'],
|
|
151
|
+
channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
152
|
+
});
|
|
153
|
+
handlers[0](session, { type: 'turn/start', data: { turn: 2 } });
|
|
154
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } } });
|
|
155
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
156
|
+
assert.equal(calls.length, 1, '关闭后不应再发');
|
|
157
|
+
} finally {
|
|
158
|
+
globalThis.fetch = originalFetch;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('渠道未启用时即使有 webhook 也不发', async () => {
|
|
163
|
+
const calls = [];
|
|
164
|
+
const originalFetch = globalThis.fetch;
|
|
165
|
+
globalThis.fetch = async () => {
|
|
166
|
+
calls.push(1);
|
|
167
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
168
|
+
};
|
|
169
|
+
try {
|
|
170
|
+
const handlers = [];
|
|
171
|
+
const ctx = {
|
|
172
|
+
on: (event, handler) => {
|
|
173
|
+
if (event === 'session/event') handlers.push(handler);
|
|
174
|
+
return () => {};
|
|
175
|
+
},
|
|
176
|
+
logger: { info: () => {}, warn: () => {} },
|
|
177
|
+
get: () => undefined,
|
|
178
|
+
};
|
|
179
|
+
apply(ctx, {
|
|
180
|
+
enabled: true,
|
|
181
|
+
reasons: ['completed'],
|
|
182
|
+
channels: { wecom: { enabled: false, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
183
|
+
});
|
|
184
|
+
const session = {
|
|
185
|
+
id: 's2',
|
|
186
|
+
header: { delegationDepth: 0 },
|
|
187
|
+
collectEvents: () => [],
|
|
188
|
+
};
|
|
189
|
+
handlers[0](session, { type: 'turn/start', data: { turn: 1 } });
|
|
190
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } });
|
|
191
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
192
|
+
assert.equal(calls.length, 0, '渠道未启用时不应发送');
|
|
193
|
+
} finally {
|
|
194
|
+
globalThis.fetch = originalFetch;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('设置卡调用 sendTest 能真的发出测试通知', async () => {
|
|
199
|
+
const calls = [];
|
|
200
|
+
const originalFetch = globalThis.fetch;
|
|
201
|
+
globalThis.fetch = async (url, init) => {
|
|
202
|
+
calls.push({ url, body: init?.body });
|
|
203
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
204
|
+
};
|
|
205
|
+
try {
|
|
206
|
+
const ctx = {
|
|
207
|
+
on: () => () => {},
|
|
208
|
+
logger: { info: () => {}, warn: () => {} },
|
|
209
|
+
};
|
|
210
|
+
const handle = apply(ctx, {
|
|
211
|
+
channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const results = await handle.sendTest();
|
|
215
|
+
assert.equal(calls.length, 1, '应发出 1 条测试通知');
|
|
216
|
+
assert.equal(results[0].ok, true);
|
|
217
|
+
|
|
218
|
+
const payload = JSON.parse(calls[0].body);
|
|
219
|
+
assert.ok(payload.markdown.content.includes('测试通知'), '测试通知应标明是测试');
|
|
220
|
+
} finally {
|
|
221
|
+
globalThis.fetch = originalFetch;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test('没有可用渠道时 sendTest 返回空数组而不是抛错', async () => {
|
|
226
|
+
const ctx = { on: () => () => {}, logger: { info: () => {}, warn: () => {} } };
|
|
227
|
+
const handle = apply(ctx, { channels: {} });
|
|
228
|
+
const results = await handle.sendTest();
|
|
229
|
+
assert.deepEqual(results, []);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
/* ------------------- 设置卡与 Host 的路由接线 ------------------- */
|
|
233
|
+
|
|
234
|
+
test('Host 注册了三条精确的 API 路由(settings 读写 + test)', async () => {
|
|
235
|
+
const registrations = [];
|
|
236
|
+
const ctx = {
|
|
237
|
+
on: () => () => {},
|
|
238
|
+
logger: { info: () => {}, warn: () => {} },
|
|
239
|
+
get: (name) => {
|
|
240
|
+
if (name !== 'webServer') return undefined;
|
|
241
|
+
return {
|
|
242
|
+
register: (options) => {
|
|
243
|
+
registrations.push(options);
|
|
244
|
+
return () => {};
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
},
|
|
248
|
+
effect: (fn) => fn(),
|
|
249
|
+
};
|
|
250
|
+
apply(ctx, { channels: {} });
|
|
251
|
+
|
|
252
|
+
assert.ok(registrations.length >= 3, `至少三条路由,实际 ${registrations.length}`);
|
|
253
|
+
for (const route of registrations) {
|
|
254
|
+
assert.equal(route.kind, 'exact', `路由 ${route.path} 必须是精确匹配`);
|
|
255
|
+
assert.ok(route.path.startsWith('/api/task-notify'), `路由前缀不对:${route.path}`);
|
|
256
|
+
assert.equal(typeof route.handler, 'function');
|
|
257
|
+
}
|
|
258
|
+
const paths = registrations.map((r) => r.path);
|
|
259
|
+
assert.ok(paths.includes('/api/task-notify/settings'), '缺 settings 路由');
|
|
260
|
+
assert.ok(paths.includes('/api/task-notify/test'), '缺 test 路由');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('GET /api/task-notify/settings 返回当前配置', async () => {
|
|
264
|
+
const registrations = [];
|
|
265
|
+
const ctx = {
|
|
266
|
+
on: () => () => {},
|
|
267
|
+
logger: { info: () => {}, warn: () => {} },
|
|
268
|
+
get: (name) =>
|
|
269
|
+
name === 'webServer'
|
|
270
|
+
? { register: (o) => { registrations.push(o); return () => {}; } }
|
|
271
|
+
: undefined,
|
|
272
|
+
effect: (fn) => fn(),
|
|
273
|
+
};
|
|
274
|
+
apply(ctx, { channels: { wecom: { enabled: true, webhook: 'https://x' } } });
|
|
275
|
+
|
|
276
|
+
const route = registrations.find((r) => r.path === '/api/task-notify/settings');
|
|
277
|
+
let body = '';
|
|
278
|
+
const res = { writeHead: () => {}, end: (t) => { body = t; } };
|
|
279
|
+
await route.handler({ method: 'GET' }, res);
|
|
280
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
281
|
+
|
|
282
|
+
const payload = JSON.parse(body);
|
|
283
|
+
assert.ok(payload.settings, '应返回 settings');
|
|
284
|
+
assert.equal(payload.settings.channels.wecom.webhook, 'https://x');
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('POST /api/task-notify/test 真的发通知并回报', async () => {
|
|
288
|
+
const calls = [];
|
|
289
|
+
const originalFetch = globalThis.fetch;
|
|
290
|
+
globalThis.fetch = async (url) => {
|
|
291
|
+
calls.push(url);
|
|
292
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
293
|
+
};
|
|
294
|
+
try {
|
|
295
|
+
const registrations = [];
|
|
296
|
+
const ctx = {
|
|
297
|
+
on: () => () => {},
|
|
298
|
+
logger: { info: () => {}, warn: () => {} },
|
|
299
|
+
get: (name) =>
|
|
300
|
+
name === 'webServer'
|
|
301
|
+
? { register: (o) => { registrations.push(o); return () => {}; } }
|
|
302
|
+
: undefined,
|
|
303
|
+
effect: (fn) => fn(),
|
|
304
|
+
};
|
|
305
|
+
apply(ctx, { channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } } });
|
|
306
|
+
|
|
307
|
+
const route = registrations.find((r) => r.path === '/api/task-notify/test');
|
|
308
|
+
let body = '';
|
|
309
|
+
const res = { writeHead: () => {}, end: (t) => { body = t; } };
|
|
310
|
+
await route.handler({ method: 'POST' }, res);
|
|
311
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
312
|
+
|
|
313
|
+
assert.equal(calls.length, 1, '应发出 1 条测试通知');
|
|
314
|
+
const payload = JSON.parse(body);
|
|
315
|
+
assert.equal(payload.ok, true, body);
|
|
316
|
+
assert.equal(payload.results[0].channel, 'wecom');
|
|
317
|
+
} finally {
|
|
318
|
+
globalThis.fetch = originalFetch;
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('没有 webServer 时插件照常工作,只是少了测试路由', () => {
|
|
323
|
+
const ctx = { on: () => () => {}, logger: { info: () => {}, warn: () => {} } };
|
|
324
|
+
const handle = apply(ctx, { channels: {} });
|
|
325
|
+
assert.equal(typeof handle.sendTest, 'function');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
/* ------------------- 真实 Session API:读日志 ------------------- */
|
|
329
|
+
|
|
330
|
+
test('sessionEvents 用 snapshotEvents 读日志(不是 collectEvents)', async () => {
|
|
331
|
+
const { sessionEvents } = await import('../index.js');
|
|
332
|
+
|
|
333
|
+
const events = [{ type: 'turn/start', data: { turn: 1 } }];
|
|
334
|
+
const session = { snapshotEvents: () => events };
|
|
335
|
+
assert.deepEqual(sessionEvents(session), events);
|
|
336
|
+
|
|
337
|
+
// 老版本 DSH 只有 events getter
|
|
338
|
+
assert.deepEqual(sessionEvents({ events }), events);
|
|
339
|
+
|
|
340
|
+
// 什么都没有时给空数组,不抛
|
|
341
|
+
assert.deepEqual(sessionEvents({}), []);
|
|
342
|
+
assert.deepEqual(sessionEvents(undefined), []);
|
|
343
|
+
// snapshotEvents 抛错也不外泄
|
|
344
|
+
assert.deepEqual(sessionEvents({ snapshotEvents: () => { throw new Error('boom') } }), []);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test('通知的概要与 token 只算本轮,不掺历史轮次', async () => {
|
|
348
|
+
const received = [];
|
|
349
|
+
const originalFetch = globalThis.fetch;
|
|
350
|
+
globalThis.fetch = async (url, init) => {
|
|
351
|
+
received.push(JSON.parse(init.body));
|
|
352
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
353
|
+
};
|
|
354
|
+
try {
|
|
355
|
+
const events = [
|
|
356
|
+
{ type: 'turn/start', data: { turn: 1 } },
|
|
357
|
+
{ type: 'session/title', data: { title: '测试会话' } },
|
|
358
|
+
{ type: 'assistant/message', data: {
|
|
359
|
+
turn: 1,
|
|
360
|
+
usage: { inputTokens: 50000, outputTokens: 9000, totalTokens: 59000 },
|
|
361
|
+
message: { content: [{ type: 'text', text: '历史回复不应出现' }] },
|
|
362
|
+
} },
|
|
363
|
+
{ type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } },
|
|
364
|
+
{ type: 'turn/start', data: { turn: 2 } },
|
|
365
|
+
{ type: 'assistant/message', data: {
|
|
366
|
+
turn: 2,
|
|
367
|
+
usage: { inputTokens: 1234, outputTokens: 567, totalTokens: 1801 },
|
|
368
|
+
message: { content: [{ type: 'text', text: '本轮的真实回复' }] },
|
|
369
|
+
} },
|
|
370
|
+
];
|
|
371
|
+
|
|
372
|
+
const handlers = [];
|
|
373
|
+
const ctx = {
|
|
374
|
+
on: (ev, h) => { if (ev === 'session/event') handlers.push(h); return () => {}; },
|
|
375
|
+
logger: { info: () => {}, warn: () => {} },
|
|
376
|
+
get: () => undefined,
|
|
377
|
+
};
|
|
378
|
+
apply(ctx, {
|
|
379
|
+
enabled: true,
|
|
380
|
+
reasons: ['completed'],
|
|
381
|
+
channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const session = {
|
|
385
|
+
id: 'sess-filter',
|
|
386
|
+
header: { delegationDepth: 0 },
|
|
387
|
+
snapshotEvents: () => events,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// 第 1 轮走完(不发通知:reasons 只配了本轮将触发的 completed,
|
|
391
|
+
// 但 delivered 去重会挡住重复,所以这里只推进到第 2 轮)
|
|
392
|
+
handlers[0](session, events[0]);
|
|
393
|
+
handlers[0](session, events[2]);
|
|
394
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } });
|
|
395
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
396
|
+
|
|
397
|
+
// 第 2 轮
|
|
398
|
+
handlers[0](session, events[4]);
|
|
399
|
+
handlers[0](session, events[5]);
|
|
400
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } } });
|
|
401
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
402
|
+
|
|
403
|
+
const last = received[received.length - 1];
|
|
404
|
+
const content = last?.markdown?.content ?? '';
|
|
405
|
+
assert.ok(content.includes('本轮的真实回复'), `缺本轮概要:${content}`);
|
|
406
|
+
assert.ok(!content.includes('历史回复不应出现'), '不应包含历史轮次的回复');
|
|
407
|
+
assert.ok(content.includes('1,801'), `token 应是本轮的 1,801:${content}`);
|
|
408
|
+
assert.ok(!content.includes('59,000'), '不应把历史 token 算进来');
|
|
409
|
+
} finally {
|
|
410
|
+
globalThis.fetch = originalFetch;
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test('turn 号缺失时退回 turn/end 自带的值,过滤不会失效', async () => {
|
|
415
|
+
const received = [];
|
|
416
|
+
const originalFetch = globalThis.fetch;
|
|
417
|
+
globalThis.fetch = async (url, init) => {
|
|
418
|
+
received.push(JSON.parse(init.body));
|
|
419
|
+
return { ok: true, status: 200, text: async () => JSON.stringify({ errcode: 0 }) };
|
|
420
|
+
};
|
|
421
|
+
try {
|
|
422
|
+
const events = [
|
|
423
|
+
{ type: 'assistant/message', data: {
|
|
424
|
+
turn: 7,
|
|
425
|
+
usage: { inputTokens: 100, outputTokens: 20, totalTokens: 120 },
|
|
426
|
+
message: { content: [{ type: 'text', text: '只有 turn/end 没有 turn/start 的场景' }] },
|
|
427
|
+
} },
|
|
428
|
+
];
|
|
429
|
+
const handlers = [];
|
|
430
|
+
const ctx = {
|
|
431
|
+
on: (ev, h) => { if (ev === 'session/event') handlers.push(h); return () => {}; },
|
|
432
|
+
logger: { info: () => {}, warn: () => {} },
|
|
433
|
+
get: () => undefined,
|
|
434
|
+
};
|
|
435
|
+
apply(ctx, {
|
|
436
|
+
enabled: true,
|
|
437
|
+
reasons: ['completed'],
|
|
438
|
+
channels: { wecom: { enabled: true, webhook: 'https://qyapi.weixin.qq.com/hook' } },
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const session = { id: 'sess-nostart', header: { delegationDepth: 0 }, snapshotEvents: () => events };
|
|
442
|
+
// 故意不发 turn/start
|
|
443
|
+
handlers[0](session, { type: 'turn/end', data: { turn: 7, reason: { kind: 'completed' } } });
|
|
444
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
445
|
+
|
|
446
|
+
const content = received[received.length - 1]?.markdown?.content ?? '';
|
|
447
|
+
assert.ok(content.includes('只有 turn/end'), `缺概要:${content}`);
|
|
448
|
+
assert.ok(content.includes('120'), `token 应取到本轮的 120:${content}`);
|
|
449
|
+
} finally {
|
|
450
|
+
globalThis.fetch = originalFetch;
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
await run();
|