@hugesb/dsh-stream-agent 0.3.1 → 0.3.2
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/CHANGELOG.md +125 -0
- package/README.en.md +3 -3
- package/README.md +3 -3
- package/dist/entry/loop.js +332 -21
- package/dist/entry/mock-provider.js +20 -7
- package/dist/kernel/parser.js +488 -39
- package/dist/kernel/render.js +25 -3
- package/dist/version.js +1 -1
- package/package.json +2 -2
- package/scripts/release.mjs +20 -7
- package/scripts/repair-session-logs.mjs +105 -10
- package/src/entry/loop.ts +332 -21
- package/src/entry/mock-provider.ts +20 -7
- package/src/kernel/parser.ts +488 -39
- package/src/kernel/render.ts +25 -3
- package/src/version.ts +1 -1
- package/test/host-conformance.test.ts +418 -0
- package/test/kernel/parser.test.ts +3 -2
- package/test/v032-conflict.test.ts +191 -0
- package/test/v032-truncation.test.ts +291 -0
package/dist/kernel/render.js
CHANGED
|
@@ -26,9 +26,23 @@ import { createHash } from 'node:crypto';
|
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
|
|
29
|
+
/** v0.3.2:assistant 消息的宿主来源元数据。宿主 LlmRuntime.forAdapter(dsh-llm
|
|
30
|
+
* lib/index.js:2277;GitHub master packages/llm/llm/src/index.ts 同源无防护)对请求里的
|
|
31
|
+
* 每条 assistant 消息读 `message.source.replayState`——source 缺失 = TypeError
|
|
32
|
+
* "Cannot read properties of undefined (reading 'replayState')",流以 error finish 收场,
|
|
33
|
+
* 回合中断(真机 session-8f22c7a0 seq 121 实证:正文首次落追加区后的下一请求必崩)。
|
|
34
|
+
* replayState 本身可选(dsh-llm types.d.ts:15),缺失时宿主按"无原生回放"处理。 */
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
29
41
|
|
|
30
42
|
|
|
31
43
|
|
|
44
|
+
|
|
45
|
+
|
|
32
46
|
|
|
33
47
|
|
|
34
48
|
/** 与 03 号分派 obs.ts 的 directoryDelta(sinceSeq) 返回结构兼容(结构化类型,不 import)。 */
|
|
@@ -161,6 +175,8 @@ export function renderHash(messages ) {
|
|
|
161
175
|
|
|
162
176
|
|
|
163
177
|
|
|
178
|
+
|
|
179
|
+
|
|
164
180
|
|
|
165
181
|
|
|
166
182
|
|
|
@@ -241,7 +257,8 @@ export class ContextRenderer {
|
|
|
241
257
|
if (msg.role === 'system') {
|
|
242
258
|
throw new Error('appendHistory 不接受 system 消息:system 属于稳定前缀');
|
|
243
259
|
}
|
|
244
|
-
|
|
260
|
+
// v0.3.2:source 必须保留(assistant 块不带 source = 请求在宿主 forAdapter 崩溃)。
|
|
261
|
+
this.#appendBlocks.push({ kind: 'history', role: msg.role, content: msg.content, ...(msg.source === undefined ? {} : { source: msg.source }) });
|
|
245
262
|
}
|
|
246
263
|
|
|
247
264
|
/**
|
|
@@ -317,11 +334,16 @@ export class ContextRenderer {
|
|
|
317
334
|
|
|
318
335
|
// ------------------------------------------------------------------- 装配
|
|
319
336
|
|
|
320
|
-
/** 稳定段 = 稳定前缀 ++ 追加区(不含易变尾)。
|
|
337
|
+
/** 稳定段 = 稳定前缀 ++ 追加区(不含易变尾)。v0.3.2:assistant 块的 source 随装配
|
|
338
|
+
* 透传(宿主 forAdapter 读 assistant 消息的 source.replayState,缺失即崩)。 */
|
|
321
339
|
stableMessages() {
|
|
322
340
|
return [
|
|
323
341
|
...this.#prefix,
|
|
324
|
-
...this.#appendBlocks.map((b) => (
|
|
342
|
+
...this.#appendBlocks.map((b) => (
|
|
343
|
+
b.source === undefined
|
|
344
|
+
? { role: b.role, content: b.content }
|
|
345
|
+
: { role: b.role, content: b.content, source: b.source }
|
|
346
|
+
)),
|
|
325
347
|
];
|
|
326
348
|
}
|
|
327
349
|
|
package/dist/version.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 为什么单独一个文件:控制栏在 stderr 与 --json 里都要报版本,而 kernel 不读 package.json
|
|
4
4
|
// (内核零 IO 纪律),entry 也不该各自硬编码字符串。
|
|
5
5
|
|
|
6
|
-
export const VERSION = '0.3.
|
|
6
|
+
export const VERSION = '0.3.2';
|
|
7
7
|
|
|
8
8
|
/** 版本来源:npm 源未上线时引导用户用本地路径安装的提示会用到。 */
|
|
9
9
|
export const PACKAGE_NAME = '@hugesb/dsh-stream-agent';
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hugesb/dsh-stream-agent",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"description": "单流协议 DSH 插件(独立模式):专用 profile + 计量治理 + tf-code 收口 + 编辑/搜索/计划/恢复/段边界(含会话日志 v4
|
|
6
|
+
"description": "单流协议 DSH 插件(独立模式):专用 profile + 计量治理 + tf-code 收口 + 编辑/搜索/计划/恢复/段边界(含会话日志 v4 工具生命周期与 model source 修复)",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"dsh",
|
|
9
9
|
"dsh-plugin",
|
package/scripts/release.mjs
CHANGED
|
@@ -25,6 +25,8 @@ const WIN = process.platform === 'win32';
|
|
|
25
25
|
const NPM = WIN ? 'npm.cmd' : 'npm';
|
|
26
26
|
const DRY = has('dry-run');
|
|
27
27
|
const YES = has('yes');
|
|
28
|
+
/** 一次性验证码(账号 2FA=auth-and-writes 时 npm publish 需要):NPM_OTP=123456 npm run release */
|
|
29
|
+
const OTP = (process.env.NPM_OTP ?? '').trim();
|
|
28
30
|
|
|
29
31
|
/* ── 输出 ─────────────────────────────────────────────── */
|
|
30
32
|
const C = process.stdout.isTTY
|
|
@@ -348,6 +350,7 @@ if (!has('no-npm')) {
|
|
|
348
350
|
}
|
|
349
351
|
const distTag = targetVer.pre ? 'alpha' : 'latest';
|
|
350
352
|
const pubArgs = ['publish', '--access', 'public', '--tag', distTag];
|
|
353
|
+
if (OTP !== '') pubArgs.push(`--otp=${OTP}`);
|
|
351
354
|
if (DRY) {
|
|
352
355
|
warn(`dry-run:本节将执行 npm ${pubArgs.join(' ')}`);
|
|
353
356
|
try {
|
|
@@ -360,16 +363,26 @@ if (!has('no-npm')) {
|
|
|
360
363
|
npmRun(pubArgs, { inherit: true });
|
|
361
364
|
} catch (e) {
|
|
362
365
|
const out = String(e.stdout || '') + String(e.stderr || '');
|
|
363
|
-
|
|
366
|
+
// 2FA 墙的三种表现(npm 版本不同):E403 two-factor / E401 otp / E404 …/-/v1/done?authId=
|
|
367
|
+
// (11.x 的 otplease 会先开网页鉴权流程轮询 /-/v1/done,超时后把 authId 失效报成 404——
|
|
368
|
+
// 这个 404 是症状不是原因,真正的因是账号 2FA=auth-and-writes 且本次 PUT 没带 OTP)
|
|
369
|
+
const otpWall =
|
|
370
|
+
/EOTP|two-factor|2fa/i.test(out) ||
|
|
371
|
+
/\/v1\/done|authId/i.test(out) ||
|
|
372
|
+
(/E40[13]/.test(out) && /otp/i.test(out));
|
|
373
|
+
if (otpWall) {
|
|
364
374
|
die(
|
|
365
|
-
'npm
|
|
375
|
+
'npm 拒绝了发布:账号 2FA=auth-and-writes,本次 PUT 没带一次性验证码',
|
|
366
376
|
[
|
|
367
|
-
'
|
|
368
|
-
'
|
|
369
|
-
'
|
|
370
|
-
'
|
|
377
|
+
'(若报的是 E404 GET /-/v1/done?authId=…,那是网页鉴权流程超时的症状,不是包名/权限问题)',
|
|
378
|
+
'三个办法:',
|
|
379
|
+
' 1. 本次直接带码(30 秒一换):NPM_OTP=123456 npm run release',
|
|
380
|
+
' —— 或不经脚本:npm publish --access public --otp=123456',
|
|
381
|
+
' 2. 一劳永逸:建 Granular Access Token 并勾选 “Bypass two-factor authentication”',
|
|
382
|
+
' https://www.npmjs.com/settings/' + (pkg.name.split('/')[0].slice(1) || '') + '/tokens → Generate New Token',
|
|
383
|
+
' 权限 Read and write、Packages 选 All packages,然后写入:',
|
|
371
384
|
' npm config set //registry.npmjs.org/:_authToken=npm_xxx',
|
|
372
|
-
'
|
|
385
|
+
' 3. 不想每次带码也不建新令牌:把账号 2FA 降为 auth-only(写操作免码,安全性下降,不建议)',
|
|
373
386
|
].join('\n'),
|
|
374
387
|
);
|
|
375
388
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// scripts/repair-session-logs.mjs —— 存量会话日志的 v4
|
|
1
|
+
// scripts/repair-session-logs.mjs —— 存量会话日志的 v4 形状修复(0.3.0 广告缺失 / 0.3.2 model source)
|
|
2
2
|
//
|
|
3
3
|
// 背景:0.3.0 及更早版本的插件在派发 tf-* 命令时只让宿主管线落 `tool/call` +
|
|
4
4
|
// `tool/result`,从未写 v4 格式要求的 `assistant/message`(content 带 `tool-call` 块)。
|
|
@@ -6,13 +6,22 @@
|
|
|
6
6
|
// corrupt——桌面端表现为"历史加载失败"。本脚本为存量日志补入缺失的广告事件,并重排
|
|
7
7
|
// seq 与全部 seq 引用;写入前先备份,写入后用本地复核规则再验一遍。
|
|
8
8
|
//
|
|
9
|
+
// 0.3.2 新增第三类 corrupt(model source 缺失):0.3.1 及更早版本写 assistant/message
|
|
10
|
+
// 时 source 只有 `{kind:'model'}`,缺 `provider`/`model` 非空字符串——宿主校验器
|
|
11
|
+
// (dsh-session assertMessageEventShape → hasProviderModel)读取时抛
|
|
12
|
+
// `message must have model source`,整条会话同样被判 corrupt。本脚本按命令行指定的
|
|
13
|
+
// provider/model(缺省 deepseek-account / deepseek-flash,即本机桌面端默认路由)补齐
|
|
14
|
+
// 全部缺失的 model source;已有 provider/model 的事件一个字节不动。
|
|
15
|
+
//
|
|
9
16
|
// 用法:
|
|
10
17
|
// node scripts/repair-session-logs.mjs # dry-run:只报告哪些会话需要修
|
|
11
18
|
// node scripts/repair-session-logs.mjs --write # 落盘(.bak-<时间戳> 备份 + 删投影缓存)
|
|
12
19
|
// node scripts/repair-session-logs.mjs --session <id> # 只处理一个会话
|
|
13
20
|
// node scripts/repair-session-logs.mjs --root <dir> # 指定 dsh 状态根(缺省 ~/.dsh)
|
|
21
|
+
// node scripts/repair-session-logs.mjs --model-provider <p> --model <m>
|
|
22
|
+
// # 补齐 source 用的 provider/model(缺省 deepseek-account / deepseek-flash)
|
|
14
23
|
//
|
|
15
|
-
// 纪律:零依赖;只改被判定为"
|
|
24
|
+
// 纪律:零依赖;只改被判定为"缺广告或缺 model source"的会话;形状完好的会话一个字节都不动。
|
|
16
25
|
|
|
17
26
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
18
27
|
import { homedir } from 'node:os';
|
|
@@ -34,6 +43,9 @@ const valueOf = (name, fallback) => {
|
|
|
34
43
|
const WRITE = has('--write');
|
|
35
44
|
const STATE_ROOT = resolve(valueOf('--root', join(homedir(), '.dsh')));
|
|
36
45
|
const ONLY_SESSION = valueOf('--session', undefined);
|
|
46
|
+
// model source 补齐值(0.3.2):缺省 = 本机桌面端默认路由(与实测健康日志一致)。
|
|
47
|
+
const MODEL_PROVIDER = valueOf('--model-provider', 'deepseek-account');
|
|
48
|
+
const MODEL_NAME = valueOf('--model', 'deepseek-flash');
|
|
37
49
|
const SESSIONS_ROOT = join(STATE_ROOT, 'sessions');
|
|
38
50
|
const PROJ_CACHE = join(STATE_ROOT, 'storages', 'session_projcache', 'sessions');
|
|
39
51
|
|
|
@@ -134,11 +146,37 @@ function reorderClosedStepMessages(events) {
|
|
|
134
146
|
return out;
|
|
135
147
|
}
|
|
136
148
|
|
|
149
|
+
/** assistant/message 的 model source 是否已满足宿主校验(kind=model + provider/model 非空字符串)。 */
|
|
150
|
+
function hasValidModelSource(message) {
|
|
151
|
+
const source = message?.source;
|
|
152
|
+
if (source === null || typeof source !== 'object') return false;
|
|
153
|
+
if (source.kind !== 'model') return false;
|
|
154
|
+
return typeof source.provider === 'string' && source.provider !== ''
|
|
155
|
+
&& typeof source.model === 'string' && source.model !== '';
|
|
156
|
+
}
|
|
157
|
+
|
|
137
158
|
function repairedRows(rows, sessionId) {
|
|
138
159
|
const [headerRow, ...rawEvents] = rows;
|
|
139
160
|
if (headerRow?.type !== 'session') throw new Error('首行不是 session header');
|
|
140
161
|
const events = reorderClosedStepMessages(rawEvents);
|
|
141
162
|
|
|
163
|
+
// 0.3.2:model source 补齐(原位改写,不动 seq 与顺序)。只补缺失,已有值不覆盖——
|
|
164
|
+
// 万一存量日志里有宿主/旧版本写的真实 provider/model,保留原值。
|
|
165
|
+
let fixedSources = 0;
|
|
166
|
+
for (const event of events) {
|
|
167
|
+
if (event.type !== 'assistant/message') continue;
|
|
168
|
+
const message = event.data?.message;
|
|
169
|
+
if (message === null || typeof message !== 'object') continue;
|
|
170
|
+
if (hasValidModelSource(message)) continue;
|
|
171
|
+
const source = (message.source !== null && typeof message.source === 'object' && message.source.kind === 'model')
|
|
172
|
+
? message.source
|
|
173
|
+
: { kind: 'model' };
|
|
174
|
+
source.provider = MODEL_PROVIDER;
|
|
175
|
+
source.model = MODEL_NAME;
|
|
176
|
+
message.source = source;
|
|
177
|
+
fixedSources += 1;
|
|
178
|
+
}
|
|
179
|
+
|
|
142
180
|
/** @type {Map<number, Array<{type:'tool-call', id:string, name:string, arguments:string}>>} */
|
|
143
181
|
const insertBefore = new Map();
|
|
144
182
|
let turn = null;
|
|
@@ -197,9 +235,9 @@ function repairedRows(rows, sessionId) {
|
|
|
197
235
|
};
|
|
198
236
|
|
|
199
237
|
if (insertBefore.size === 0) {
|
|
200
|
-
if (!reordered) return { changed: false, rows, inserted: 0, unadvertised: 0, reordered: false };
|
|
238
|
+
if (!reordered && fixedSources === 0) return { changed: false, rows, inserted: 0, unadvertised: 0, reordered: false, fixedSources: 0 };
|
|
201
239
|
const out = rebuild([...events]);
|
|
202
|
-
return { changed: true, rows: [headerRow, ...out], inserted: 0, unadvertised: 0, reordered
|
|
240
|
+
return { changed: true, rows: [headerRow, ...out], inserted: 0, unadvertised: 0, reordered, fixedSources };
|
|
203
241
|
}
|
|
204
242
|
|
|
205
243
|
const out = [];
|
|
@@ -213,7 +251,8 @@ function repairedRows(rows, sessionId) {
|
|
|
213
251
|
data: {
|
|
214
252
|
turn: first.data?.turn,
|
|
215
253
|
step: first.data?.step,
|
|
216
|
-
|
|
254
|
+
// 0.3.2:补出的广告同样要带非空 provider/model(宿主 hasProviderModel 校验)。
|
|
255
|
+
message: { id: `repaired-ad-${randomUUID()}`, role: 'assistant', content: blocks, source: { kind: 'model', provider: MODEL_PROVIDER, model: MODEL_NAME } },
|
|
217
256
|
stream: [],
|
|
218
257
|
},
|
|
219
258
|
surfaceOp: 'append',
|
|
@@ -223,7 +262,7 @@ function repairedRows(rows, sessionId) {
|
|
|
223
262
|
}
|
|
224
263
|
rebuild(out);
|
|
225
264
|
|
|
226
|
-
return { changed: true, rows: [headerRow, ...out], inserted: insertBefore.size, unadvertised, reordered };
|
|
265
|
+
return { changed: true, rows: [headerRow, ...out], inserted: insertBefore.size, unadvertised, reordered, fixedSources };
|
|
227
266
|
}
|
|
228
267
|
|
|
229
268
|
function remapReferences(event, map) {
|
|
@@ -248,9 +287,52 @@ function remapReferences(event, map) {
|
|
|
248
287
|
}
|
|
249
288
|
|
|
250
289
|
// ---------------------------------------------------------------------------
|
|
251
|
-
// 复核:与 dsh-session-format-v3-to-v4 的 tool()/closeTools 同规则
|
|
290
|
+
// 复核:与 dsh-session-format-v3-to-v4 的 tool()/closeTools 同规则 + 消息形状同规则
|
|
252
291
|
// ---------------------------------------------------------------------------
|
|
253
292
|
|
|
293
|
+
/**
|
|
294
|
+
* 消息形状复核(0.3.2):与 dsh-session/lib/index.js 的 assertMessageEventShape 同规则
|
|
295
|
+
* (本地镜像,插件仓库不依赖宿主包)。五类 message 事件的 role/source/content 不合法
|
|
296
|
+
* = 整条日志被判 corrupt。DSM 之外的类型(assistant/attempt、plugin:* 等)不查。
|
|
297
|
+
*/
|
|
298
|
+
function assertMessageShapes(rows) {
|
|
299
|
+
const expectedRole = {
|
|
300
|
+
'system/message': 'system',
|
|
301
|
+
'developer/message': 'developer',
|
|
302
|
+
'user/message': 'user',
|
|
303
|
+
'assistant/message': 'assistant',
|
|
304
|
+
'tool/result': 'tool',
|
|
305
|
+
};
|
|
306
|
+
for (const event of rows) {
|
|
307
|
+
const type = event.type;
|
|
308
|
+
if (!(type in expectedRole)) continue;
|
|
309
|
+
const data = event.data ?? {};
|
|
310
|
+
const message = type === 'user/message' ? data : data.message;
|
|
311
|
+
if (message === null || typeof message !== 'object') throw new Error(`${type}#${event.seq} 缺 message`);
|
|
312
|
+
if (typeof message.id !== 'string' || message.id === '') throw new Error(`${type}#${event.seq} 缺非空 id`);
|
|
313
|
+
if (message.role !== expectedRole[type]) throw new Error(`${type}#${event.seq} role 必须是 "${expectedRole[type]}"`);
|
|
314
|
+
const source = message.source;
|
|
315
|
+
if (source === null || typeof source !== 'object' || typeof source.kind !== 'string' || source.kind === '') {
|
|
316
|
+
throw new Error(`${type}#${event.seq} source 非法`);
|
|
317
|
+
}
|
|
318
|
+
if (!Array.isArray(message.content)) throw new Error(`${type}#${event.seq} content 必须是数组`);
|
|
319
|
+
if (type === 'system/message') {
|
|
320
|
+
if (source.kind !== 'system-prompt') throw new Error(`${type}#${event.seq} source 必须是 system-prompt`);
|
|
321
|
+
} else if (type === 'assistant/message') {
|
|
322
|
+
if (source.kind !== 'model' || typeof source.provider !== 'string' || source.provider === ''
|
|
323
|
+
|| typeof source.model !== 'string' || source.model === '') {
|
|
324
|
+
throw new Error(`${type}#${event.seq} message 必须有 model source(非空 provider/model)`);
|
|
325
|
+
}
|
|
326
|
+
} else if (type === 'tool/result') {
|
|
327
|
+
if (source.kind !== 'tool' || typeof source.callId !== 'string' || source.callId === '') {
|
|
328
|
+
throw new Error(`${type}#${event.seq} message 必须有 tool source`);
|
|
329
|
+
}
|
|
330
|
+
if (message.toolCallId !== source.callId) throw new Error(`${type}#${event.seq} toolCallId 与 source.callId 不一致`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
254
336
|
function assertLifecycle(rows) {
|
|
255
337
|
const events = rows.filter((row) => row.type !== 'session');
|
|
256
338
|
const pending = new Map();
|
|
@@ -373,13 +455,26 @@ for (const { sessionId, file } of findSessionFiles(SESSIONS_ROOT)) {
|
|
|
373
455
|
console.log(`[skip] ${sessionId}:${error.message}`);
|
|
374
456
|
continue;
|
|
375
457
|
}
|
|
376
|
-
|
|
377
|
-
|
|
458
|
+
// 0.3.2:先对**原日志**跑消息形状复核——形状已坏的会话无论能否补广告都需要修。
|
|
459
|
+
let shapeBroken = false;
|
|
460
|
+
try {
|
|
461
|
+
assertMessageShapes(rows);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
shapeBroken = true;
|
|
464
|
+
}
|
|
465
|
+
if (!result.changed && !shapeBroken) {
|
|
466
|
+
console.log(`[ok] ${sessionId}:无缺失广告,model source 完好`);
|
|
378
467
|
continue;
|
|
379
468
|
}
|
|
380
469
|
needRepair += 1;
|
|
381
|
-
|
|
470
|
+
const fixes = [
|
|
471
|
+
result.inserted > 0 ? `补 ${result.inserted} 条广告(覆盖 ${result.unadvertised} 个 tool/call)` : '',
|
|
472
|
+
result.fixedSources > 0 ? `补齐 ${result.fixedSources} 条 model source(${MODEL_PROVIDER}/${MODEL_NAME})` : '',
|
|
473
|
+
shapeBroken && !result.changed ? '消息形状损坏(见修复后复核)' : '',
|
|
474
|
+
].filter(Boolean).join(';');
|
|
475
|
+
console.log(`[fix] ${sessionId}:${fixes}`);
|
|
382
476
|
try {
|
|
477
|
+
assertMessageShapes(result.rows);
|
|
383
478
|
assertLifecycle(result.rows);
|
|
384
479
|
hostValidate?.(result.rows);
|
|
385
480
|
} catch (error) {
|