@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
|
@@ -257,13 +257,26 @@ export function createMockModel(config ) {
|
|
|
257
257
|
.flatMap((m) => m.content)
|
|
258
258
|
.map((b) => b.text ?? '')
|
|
259
259
|
.join('\n');
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
260
|
+
// v0.3.2:按 marker 在提示中的**最后出现位置**选剧本(等价于"响应最新注入")。
|
|
261
|
+
// 0.3.1 取首个匹配;驱动器把历史观测转正进追加区后,旧 marker 会留在历史里,
|
|
262
|
+
// 首匹配会让同一个剧本无限重演(G1/D1 的假失败就是这么来的)。
|
|
263
|
+
let best ;
|
|
264
|
+
let bestAt = -1;
|
|
265
|
+
for (const s of config.scenarios) {
|
|
266
|
+
if (!s.match) continue;
|
|
267
|
+
if (s.match.model !== undefined && s.match.model !== options.model) continue;
|
|
268
|
+
if (s.match.promptIncludes !== undefined) {
|
|
269
|
+
const at = prompt.lastIndexOf(s.match.promptIncludes);
|
|
270
|
+
if (at === -1) continue;
|
|
271
|
+
if (at > bestAt) {
|
|
272
|
+
best = s;
|
|
273
|
+
bestAt = at;
|
|
274
|
+
}
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (best === undefined) best = s;
|
|
278
|
+
}
|
|
279
|
+
return best ?? config.scenarios[0] ; // 无 match 的首个剧本作兜底
|
|
267
280
|
}
|
|
268
281
|
|
|
269
282
|
function* chunkify(text , charsPerChunk ) {
|
package/dist/kernel/parser.js
CHANGED
|
@@ -46,6 +46,19 @@
|
|
|
46
46
|
* 典型是模型把自闭合命令又"关闭"了一次)一律按 malformed(stray-close-tag) 记账后丢弃,
|
|
47
47
|
* 绝不作 prose 透传——0.3.1 真机证据:这类残片会以纯文本落进 assistant/message,
|
|
48
48
|
* 桌面端正文里出现裸的 `</tf-bash>`。
|
|
49
|
+
* - DSML 参数式调用(v0.3.2 截断修复):模型原生调用格式还有"父标签 + parameter 子标签"
|
|
50
|
+
* 形态(真机取证 session-e4b9a89c 第 6/9 步):
|
|
51
|
+
* <||DSML|| invoke name="tf-read">
|
|
52
|
+
* <||DSML|| parameter name="path" string="true">README.md</||DSML|| parameter>
|
|
53
|
+
* </||DSML|| invoke>
|
|
54
|
+
* 0.3.1 只认「invoke 头部内联属性」形态,parameter 子标签被逐条判 malformed,而标签之间的
|
|
55
|
+
* 属性值(README.md / r10 / 命令行)作为 prose 漏进正文——用户侧看到的就是"截断的正文 +
|
|
56
|
+
* `README.md r10` 这种残片",且命令根本没执行。本版把整个 invoke 块缓冲到闭合,按
|
|
57
|
+
* parameter 归集成属性后一次性发命令;块内文本不再作为 prose 透传。块未闭合即流终止时,
|
|
58
|
+
* 非正文命令(属性已齐)照常发命令(能救则救),正文命令按 truncated 丢弃(不写半截文件)。
|
|
59
|
+
* - 非正文命令的配对开标签(v0.3.2):`<tf-read ...>` 少了 `/` 时,0.3.1 会进入 body 模式
|
|
60
|
+
* 一直吞到流末尾(整个后续输出消失)。现在非正文命令一旦出现下一个 `<tf-` / `</tf-`
|
|
61
|
+
* 即视为已闭合(命令照常派发),流末尾仍未闭合也发命令并把剩余文本按 prose 归还。
|
|
49
62
|
*
|
|
50
63
|
* 事件类型:text / command / malformed / truncated / cycle / done。
|
|
51
64
|
* 命令闭合立即 emit,不等流结束;"当前可执行集合"用 readyCommands() 查询
|
|
@@ -120,7 +133,7 @@ const COMMAND_NAMES =
|
|
|
120
133
|
// 流式解析器
|
|
121
134
|
// ---------------------------------------------------------------------------
|
|
122
135
|
|
|
123
|
-
|
|
136
|
+
|
|
124
137
|
|
|
125
138
|
|
|
126
139
|
export class TfStreamParser {
|
|
@@ -128,6 +141,10 @@ export class TfStreamParser {
|
|
|
128
141
|
mode = 'ground';
|
|
129
142
|
ended = false;
|
|
130
143
|
|
|
144
|
+
/** DSML 参数式调用块缓冲(v0.3.2):从无内联属性的 invoke 开标签起,缓冲到块闭合。 */
|
|
145
|
+
dsmlBuf = '';
|
|
146
|
+
dsmlName = null;
|
|
147
|
+
|
|
131
148
|
// body 中间态
|
|
132
149
|
bodyName = null;
|
|
133
150
|
bodyAttrs = null;
|
|
@@ -160,7 +177,24 @@ export class TfStreamParser {
|
|
|
160
177
|
if (this.ended) return [];
|
|
161
178
|
this.ended = true;
|
|
162
179
|
const events = [];
|
|
163
|
-
if (this.mode === '
|
|
180
|
+
if (this.mode === 'dsml') {
|
|
181
|
+
// v0.3.2:DSML 参数式块未闭合即流终止。非正文命令只要属性齐全就照常发(能救则救);
|
|
182
|
+
// 正文命令(tf-write/tf-edit/tf-append/tf-code)不写半截内容——按 truncated 上报。
|
|
183
|
+
const block = this.dsmlBuf + this.buf;
|
|
184
|
+
const expected = this.dsmlName;
|
|
185
|
+
this.dsmlBuf = '';
|
|
186
|
+
this.dsmlName = null;
|
|
187
|
+
this.buf = '';
|
|
188
|
+
this.mode = 'ground';
|
|
189
|
+
const parsed = parseDsmlBlock(block, expected, { allowIncompleteBody: false });
|
|
190
|
+
for (const bad of parsed.malformed) {
|
|
191
|
+
events.push({ type: 'malformed', raw: bad.raw, reason: bad.reason });
|
|
192
|
+
}
|
|
193
|
+
for (const cmd of parsed.commands) {
|
|
194
|
+
this.emitCommand(events, cmd.name, cmd.attrs, cmd.dependsOn, cmd.body, cmd.raw);
|
|
195
|
+
}
|
|
196
|
+
events.push({ type: 'truncated', raw: block, reason: 'unclosed-dsml-invoke' });
|
|
197
|
+
} else if (this.mode === 'header') {
|
|
164
198
|
if (isDsmlTag(this.buf)) {
|
|
165
199
|
// DSML 尾巴(feed 期间已进 header 模式但流终止):malformed 上报,不进 prose
|
|
166
200
|
events.push({ type: 'malformed', raw: this.buf, reason: 'dsml-wrapper' });
|
|
@@ -169,11 +203,33 @@ export class TfStreamParser {
|
|
|
169
203
|
events.push({ type: 'truncated', raw: this.buf, reason: 'unclosed-command-tag' });
|
|
170
204
|
}
|
|
171
205
|
} else if (this.mode === 'body') {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
206
|
+
if (!COMMAND_SCHEMAS[this.bodyName ].hasBody) {
|
|
207
|
+
// v0.3.2:非正文命令的配对开标签缺闭合——命令本身是完整的(属性齐),
|
|
208
|
+
// 不能因为模型少写一个 '/' 就把整条命令连同后续输出一起丢掉。
|
|
209
|
+
const name = this.bodyName ;
|
|
210
|
+
const attrs = this.bodyAttrs ;
|
|
211
|
+
const deps = this.bodyDeps ;
|
|
212
|
+
const rawOpen = this.bodyRawOpen;
|
|
213
|
+
const trailing = this.bodyBuf + this.buf;
|
|
214
|
+
this.buf = '';
|
|
215
|
+
this.bodyName = null;
|
|
216
|
+
this.bodyAttrs = null;
|
|
217
|
+
this.bodyDeps = null;
|
|
218
|
+
this.bodyRawOpen = '';
|
|
219
|
+
this.bodyBuf = '';
|
|
220
|
+
this.mode = 'ground';
|
|
221
|
+
if (trailing.trim() !== '') {
|
|
222
|
+
events.push({ type: 'malformed', raw: trailing, reason: 'unexpected-body' });
|
|
223
|
+
}
|
|
224
|
+
this.emitCommand(events, name, attrs, deps, undefined, rawOpen + trailing);
|
|
225
|
+
if (trailing.trim() !== '') events.push({ type: 'text', text: trailing });
|
|
226
|
+
} else {
|
|
227
|
+
events.push({
|
|
228
|
+
type: 'truncated',
|
|
229
|
+
raw: this.bodyRawOpen + this.bodyBuf + this.buf,
|
|
230
|
+
reason: `unclosed-command-body:${this.bodyName}`,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
177
233
|
} else {
|
|
178
234
|
// ground:残留的 "<tf-" 片段(尚无法判定是否标签)→ truncated;
|
|
179
235
|
// DSML 尾巴(协议违规,v0.3.1)→ malformed 上报后丢弃,不作为 prose 泄漏;
|
|
@@ -224,6 +280,7 @@ export class TfStreamParser {
|
|
|
224
280
|
step(events ) {
|
|
225
281
|
if (this.mode === 'ground') return this.stepGround(events);
|
|
226
282
|
if (this.mode === 'header') return this.stepHeader(events);
|
|
283
|
+
if (this.mode === 'dsml') return this.stepDsml(events);
|
|
227
284
|
return this.stepBody(events);
|
|
228
285
|
}
|
|
229
286
|
|
|
@@ -269,12 +326,32 @@ export class TfStreamParser {
|
|
|
269
326
|
// DSML 噪声(v0.3.1):先于普通解析处理——`invoke name="tf-xxx" ...` 形态改写回
|
|
270
327
|
// 等价 tf-* 头部(命令不丢);其余包装行按 malformed 上报后丢弃(不进 prose)。
|
|
271
328
|
if (isDsmlTag(raw)) {
|
|
272
|
-
|
|
273
|
-
|
|
329
|
+
// v0.3.2:参数式 invoke → 进入块缓冲,按 parameter 归集属性。
|
|
330
|
+
// 0.3.1 只认内联属性形态,parameter 子标签之间的值会作为 prose 泄漏(真机残片
|
|
331
|
+
// `README.md r10` / `Get-ChildItem ... b902` 均由此产生)。
|
|
332
|
+
// v0.3.2 补充:带内联属性的 invoke 同样缓冲(parseDsmlBlock 本就合并内联属性与
|
|
333
|
+
// parameter——parameter 覆盖同名内联键)。此前内联属性走 parseDsmlHeader 进 body
|
|
334
|
+
// 模式,非正文命令的 DSML 包装残片会被吞到流末才以 malformed 露出
|
|
335
|
+
// (真机 session-8f22c7a0 step 2:三条闭合标签残片)。
|
|
336
|
+
const dsml = parseDsmlTag(raw, close.selfClosing);
|
|
337
|
+
if (
|
|
338
|
+
dsml !== null
|
|
339
|
+
&& dsml.name === 'invoke'
|
|
340
|
+
&& !dsml.closing
|
|
341
|
+
&& !close.selfClosing
|
|
342
|
+
&& dsml.commandName !== null
|
|
343
|
+
) {
|
|
344
|
+
this.dsmlBuf = raw;
|
|
345
|
+
this.dsmlName = dsml.commandName;
|
|
346
|
+
this.mode = 'dsml';
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
const inline = parseDsmlHeader(raw, close.selfClosing);
|
|
350
|
+
if (inline === null) {
|
|
274
351
|
events.push({ type: 'malformed', raw, reason: 'dsml-wrapper' });
|
|
275
352
|
return true;
|
|
276
353
|
}
|
|
277
|
-
const parsed = parseHeader(
|
|
354
|
+
const parsed = parseHeader(inline.synthetic);
|
|
278
355
|
if ('error' in parsed || parsed.name === null) {
|
|
279
356
|
events.push({ type: 'malformed', raw, reason: 'dsml-wrapper' });
|
|
280
357
|
return true;
|
|
@@ -318,8 +395,35 @@ export class TfStreamParser {
|
|
|
318
395
|
|
|
319
396
|
stepBody(events ) {
|
|
320
397
|
const name = this.bodyName ;
|
|
398
|
+
const schema = COMMAND_SCHEMAS[name];
|
|
321
399
|
const closeTag = `</${name}>`;
|
|
322
400
|
const closeIdx = this.buf.indexOf(closeTag);
|
|
401
|
+
// v0.3.2:非正文命令的配对开标签(少写 '/')不得吞掉后续输出——遇到下一个
|
|
402
|
+
// `<tf-` / `</tf-` 即视为本命令结束,命令照常派发,剩余文本回到 ground 继续解析。
|
|
403
|
+
// v0.3.2 补充:DSML 标签同样是边界(包装残片立即露出,不吞到流末)。
|
|
404
|
+
if (!schema.hasBody) {
|
|
405
|
+
const boundary = findNonBodyBoundaryWithDsml(this.buf);
|
|
406
|
+
if (boundary !== -1 && (closeIdx === -1 || boundary < closeIdx)) {
|
|
407
|
+
const body = this.bodyBuf + this.buf.slice(0, boundary);
|
|
408
|
+
const raw = this.bodyRawOpen + body;
|
|
409
|
+
this.buf = this.buf.slice(boundary);
|
|
410
|
+
this.mode = 'ground';
|
|
411
|
+
const attrs = this.bodyAttrs ;
|
|
412
|
+
const deps = this.bodyDeps ;
|
|
413
|
+
this.bodyName = null;
|
|
414
|
+
this.bodyAttrs = null;
|
|
415
|
+
this.bodyDeps = null;
|
|
416
|
+
this.bodyRawOpen = '';
|
|
417
|
+
this.bodyBuf = '';
|
|
418
|
+
if (body.trim() !== '') {
|
|
419
|
+
events.push({ type: 'malformed', raw: body, reason: 'unexpected-body' });
|
|
420
|
+
// 非正文命令后面的文本是模型 prose(不是命令输入)——归还正文,避免"输出变少"。
|
|
421
|
+
events.push({ type: 'text', text: body });
|
|
422
|
+
}
|
|
423
|
+
this.emitCommand(events, name, attrs, deps, undefined, raw);
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
323
427
|
if (closeIdx === -1) {
|
|
324
428
|
// 闭合标签可能被拆在 chunk 边界上:扣留末尾至多 closeTag.length-1 个字符
|
|
325
429
|
const keep = Math.min(closeTag.length - 1, this.buf.length);
|
|
@@ -339,11 +443,43 @@ export class TfStreamParser {
|
|
|
339
443
|
this.bodyRawOpen = '';
|
|
340
444
|
this.bodyBuf = '';
|
|
341
445
|
|
|
342
|
-
if (!
|
|
446
|
+
if (!schema.hasBody && body !== '') {
|
|
447
|
+
// 非正文命令带正文:命令本身仍有效(属性齐),正文只记账不当作命令输入。
|
|
448
|
+
// v0.3.2 起不再丢弃命令——0.3.1 会整条判 malformed,模型侧表现为"命令没执行"。
|
|
343
449
|
events.push({ type: 'malformed', raw, reason: 'unexpected-body' });
|
|
450
|
+
this.emitCommand(events, name, attrs, deps, undefined, raw);
|
|
344
451
|
return true;
|
|
345
452
|
}
|
|
346
|
-
this.emitCommand(events, name, attrs, deps,
|
|
453
|
+
this.emitCommand(events, name, attrs, deps, schema.hasBody ? body : undefined, raw);
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* v0.3.2:DSML 参数式 invoke 块。缓冲到块闭合后一次性归集为命令:
|
|
459
|
+
* 块内 `<||DSML|| parameter name="k">value</...>` 的 value 归入属性 k,
|
|
460
|
+
* 不再作为 prose 泄漏;块内裸文本按命令正文处理(hasBody 命令)。
|
|
461
|
+
*/
|
|
462
|
+
stepDsml(events ) {
|
|
463
|
+
if (this.buf !== '') {
|
|
464
|
+
this.dsmlBuf += this.buf;
|
|
465
|
+
this.buf = '';
|
|
466
|
+
}
|
|
467
|
+
const term = findDsmlInvokeEnd(this.dsmlBuf, this.dsmlName);
|
|
468
|
+
if (term === null) return false;
|
|
469
|
+
const block = this.dsmlBuf.slice(0, term.end);
|
|
470
|
+
const rest = this.dsmlBuf.slice(term.end);
|
|
471
|
+
const expected = this.dsmlName;
|
|
472
|
+
this.dsmlBuf = '';
|
|
473
|
+
this.dsmlName = null;
|
|
474
|
+
this.mode = 'ground';
|
|
475
|
+
this.buf = rest;
|
|
476
|
+
const parsed = parseDsmlBlock(block, expected, { allowIncompleteBody: true });
|
|
477
|
+
for (const bad of parsed.malformed) {
|
|
478
|
+
events.push({ type: 'malformed', raw: bad.raw, reason: bad.reason });
|
|
479
|
+
}
|
|
480
|
+
for (const cmd of parsed.commands) {
|
|
481
|
+
this.emitCommand(events, cmd.name, cmd.attrs, cmd.dependsOn, cmd.body, cmd.raw);
|
|
482
|
+
}
|
|
347
483
|
return true;
|
|
348
484
|
}
|
|
349
485
|
|
|
@@ -471,6 +607,341 @@ function parseDsmlHeader(raw , selfClosing )
|
|
|
471
607
|
return { synthetic: `<${name}${attrsPart}${selfClosing ? '/>' : '>'}` };
|
|
472
608
|
}
|
|
473
609
|
|
|
610
|
+
// ---------------------------------------------------------------------------
|
|
611
|
+
// DSML 参数式 invoke(v0.3.2)
|
|
612
|
+
// ---------------------------------------------------------------------------
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
/** 解析单个 DSML 标签原文(不含前后空白)。非 DSML 形态返回 null。 */
|
|
626
|
+
function parseDsmlTag(raw , selfClosing ) {
|
|
627
|
+
if (!isDsmlTag(raw)) return null;
|
|
628
|
+
let inner = raw.slice(1);
|
|
629
|
+
const closing = inner.startsWith('/');
|
|
630
|
+
if (closing) inner = inner.slice(1);
|
|
631
|
+
if (inner.endsWith('/>')) inner = inner.slice(0, -2);
|
|
632
|
+
else if (inner.endsWith('>')) inner = inner.slice(0, -1);
|
|
633
|
+
const m = inner.match(/^|+DSML|*\s*([a-zA-Z][a-zA-Z0-9_-]*)([\s\S]*)$/);
|
|
634
|
+
if (m === null) return null;
|
|
635
|
+
const name = m[1] ;
|
|
636
|
+
const attrs = parseAttrPairs(m[2] );
|
|
637
|
+
const commandName = attrs['name'] !== undefined && COMMAND_NAMES.includes(attrs['name'] )
|
|
638
|
+
? (attrs['name'] )
|
|
639
|
+
: null;
|
|
640
|
+
return { name, closing, selfClosing, attrs, commandName };
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** v0.3.2:属性值的 XML 实体解码。模型带 XML 习惯输出属性(真机 session-8f22c7a0
|
|
644
|
+
* step 5:`command="... Write-Output "EXIT=$LASTEXITCODE""`——实体原样进了
|
|
645
|
+
* PowerShell,命令解析报错,白白烧一步)。只解码有界实体集 + 数字引用;`&` 本身
|
|
646
|
+
* 不在实体里的原样保留(不猜)。 */
|
|
647
|
+
function decodeXmlEntities(value ) {
|
|
648
|
+
return value
|
|
649
|
+
.replace(/&(lt|gt|quot|apos|amp);/g, (_, name ) => (
|
|
650
|
+
name === 'lt' ? '<' : name === 'gt' ? '>' : name === 'quot' ? '"' : name === 'apos' ? '\'' : '&'
|
|
651
|
+
))
|
|
652
|
+
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex ) => String.fromCodePoint(parseInt(hex, 16)))
|
|
653
|
+
.replace(/&#(\d+);/g, (_, dec ) => String.fromCodePoint(parseInt(dec, 10)));
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** 从 `k="v" k2="v2"` 属性段提取键值(引号内允许 '>';重复键后者覆盖,交由 schema 兜底)。 */
|
|
657
|
+
function parseAttrPairs(segment ) {
|
|
658
|
+
const attrs = {};
|
|
659
|
+
const re = /([a-zA-Z][a-zA-Z0-9_-]*)\s*=\s*"([^"]*)"/g;
|
|
660
|
+
let m ;
|
|
661
|
+
while ((m = re.exec(segment)) !== null) attrs[m[1] ] = decodeXmlEntities(m[2] );
|
|
662
|
+
return attrs;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* 非正文命令配对开标签的恢复边界(基础版,仅 tf 标签):下一个 `<tf-` / `</tf-` 的
|
|
667
|
+
* 位置(-1 = 没有)。DSML 块缓冲的消费(findDsmlInvokeEnd)也用它——缓冲块内部的
|
|
668
|
+
* DSML 标签属于块本身,绝不能当边界(否则 `<|` 即块结束 = 解析死循环)。
|
|
669
|
+
*/
|
|
670
|
+
function findNonBodyBoundary(buf ) {
|
|
671
|
+
const a = buf.indexOf('<tf-');
|
|
672
|
+
const b = buf.indexOf('</tf-');
|
|
673
|
+
if (a === -1) return b;
|
|
674
|
+
if (b === -1) return a;
|
|
675
|
+
return Math.min(a, b);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* v0.3.2:body 模式的扩展边界(tf 标签 + DSML 标签)。只在 stepBody 的非正文分支
|
|
680
|
+
* 使用——真机(session-8f22c7a0 step 2)里 DSML invoke 走完内联路径后,parameter/
|
|
681
|
+
* invoke/closes 包装残片被 body 模式整体吞掉、到流末才以 malformed 露出;DSML 标签
|
|
682
|
+
* 出现即视为本命令结束(残片按既有 malformed 纪律处理,绝不作为 prose 透传)。
|
|
683
|
+
*/
|
|
684
|
+
function findNonBodyBoundaryWithDsml(buf ) {
|
|
685
|
+
const base = findNonBodyBoundary(buf);
|
|
686
|
+
const c = buf.indexOf('<|');
|
|
687
|
+
const d = buf.indexOf('</|');
|
|
688
|
+
let min = base;
|
|
689
|
+
for (const v of [c, d]) {
|
|
690
|
+
if (v === -1) continue;
|
|
691
|
+
if (min === -1 || v < min) min = v;
|
|
692
|
+
}
|
|
693
|
+
return min;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** DSML 段结束位置(含结束标签本身):`</invoke>` / `</tf-name>` / (非正文)下一个 tf 标签。 */
|
|
697
|
+
function findDsmlInvokeEnd(buf , name ) {
|
|
698
|
+
let end = -1;
|
|
699
|
+
const invokeClose = /<\/\s*|+DSML|*\s*invoke\s*>/g;
|
|
700
|
+
const m = invokeClose.exec(buf);
|
|
701
|
+
if (m !== null) end = m.index + m[0].length;
|
|
702
|
+
if (name !== null) {
|
|
703
|
+
const tagClose = buf.indexOf(`</${name}>`);
|
|
704
|
+
if (tagClose !== -1 && (end === -1 || tagClose + name.length + 3 < end)) {
|
|
705
|
+
end = tagClose + name.length + 3;
|
|
706
|
+
}
|
|
707
|
+
if (!COMMAND_SCHEMAS[name].hasBody) {
|
|
708
|
+
const boundary = findNonBodyBoundary(buf);
|
|
709
|
+
if (boundary !== -1 && (end === -1 || boundary < end)) end = boundary;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return end === -1 ? null : { end };
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
/** 把 DSML 块切成「DSML 标签 / 文本」两类 token;非 DSML 的 '<' 留在文本里。 */
|
|
728
|
+
function tokenizeDsmlBlock(block ) {
|
|
729
|
+
const out = [];
|
|
730
|
+
let i = 0;
|
|
731
|
+
while (i < block.length) {
|
|
732
|
+
const lt = block.indexOf('<', i);
|
|
733
|
+
if (lt === -1) {
|
|
734
|
+
out.push({ kind: 'text', text: block.slice(i) });
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
if (lt > i) out.push({ kind: 'text', text: block.slice(i, lt) });
|
|
738
|
+
const after = block.slice(lt);
|
|
739
|
+
if (!/^<\/?|/.test(after)) {
|
|
740
|
+
out.push({ kind: 'text', text: '<' });
|
|
741
|
+
i = lt + 1;
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
const close = scanHeaderClose(after);
|
|
745
|
+
if (close === null) {
|
|
746
|
+
out.push({ kind: 'text', text: after });
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
const raw = after.slice(0, close.end + 1);
|
|
750
|
+
const tag = parseDsmlTag(raw, close.selfClosing);
|
|
751
|
+
if (tag === null) out.push({ kind: 'text', text: raw });
|
|
752
|
+
else out.push({ kind: 'tag', tag, raw });
|
|
753
|
+
i = lt + close.end + 1;
|
|
754
|
+
}
|
|
755
|
+
return out;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* 解析一个 DSML invoke 块(可能含 parameter 子标签)。
|
|
760
|
+
*
|
|
761
|
+
* - `parameter name="k"` 的标签间文本 → 属性 k(多行原样,去掉首尾各一个换行);
|
|
762
|
+
* - 正文类参数(content/body/code/text)→ 命令 body(hasBody 命令);
|
|
763
|
+
* - 块内裸文本 → hasBody 命令的 body;tf-final 无 summary 时兜底为最终答案;
|
|
764
|
+
* - 非正文命令带正文 → malformed(unexpected-body) 但命令仍发(不丢工作);
|
|
765
|
+
* - `allowIncompleteBody=false` 时,未闭合的正文命令不回放(避免写半截文件)。
|
|
766
|
+
*/
|
|
767
|
+
function parseDsmlBlock(
|
|
768
|
+
block ,
|
|
769
|
+
expected ,
|
|
770
|
+
opts ,
|
|
771
|
+
) {
|
|
772
|
+
const commands = [];
|
|
773
|
+
const malformed = [];
|
|
774
|
+
let cur
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
= null;
|
|
784
|
+
|
|
785
|
+
const flush = (complete ) => {
|
|
786
|
+
if (cur === null) return;
|
|
787
|
+
const item = cur;
|
|
788
|
+
cur = null;
|
|
789
|
+
if (item.paramName !== null) item.params[item.paramName] = trimParamValue(item.paramValue);
|
|
790
|
+
const name = item.name ?? expected;
|
|
791
|
+
if (name === null) {
|
|
792
|
+
malformed.push({ raw: item.raw, reason: 'dsml-unknown-invoke' });
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
const schema = COMMAND_SCHEMAS[name];
|
|
796
|
+
const attrs = { ...item.attrs };
|
|
797
|
+
const bodyKeys = ['content', 'body', 'code', 'text'];
|
|
798
|
+
let bodyFromParam ;
|
|
799
|
+
for (const key of bodyKeys) {
|
|
800
|
+
if (item.params[key] !== undefined) {
|
|
801
|
+
bodyFromParam = item.params[key];
|
|
802
|
+
delete item.params[key];
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
for (const [k, v] of Object.entries(item.params)) attrs[k] = v;
|
|
806
|
+
if (!complete && schema.hasBody && !opts.allowIncompleteBody) {
|
|
807
|
+
malformed.push({ raw: item.raw + item.text, reason: 'unclosed-dsml-body' });
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
let body ;
|
|
811
|
+
if (schema.hasBody) {
|
|
812
|
+
// v0.3.2:块以命令自身的配对闭合(</name>)终止时,闭合标签会被 tokenizer 当作
|
|
813
|
+
// 块内文本累积——剥掉尾部闭合,正文才干净(与 body 模式的消费语义一致)。
|
|
814
|
+
let text = item.text;
|
|
815
|
+
if (bodyFromParam === undefined) {
|
|
816
|
+
const closeTag = `</${name}>`;
|
|
817
|
+
const closeIdx = text.lastIndexOf(closeTag);
|
|
818
|
+
if (closeIdx !== -1 && text.slice(closeIdx).trim() === closeTag) {
|
|
819
|
+
text = text.slice(0, closeIdx);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
body = bodyFromParam ?? text.replace(/^\r?\n/, '').replace(/\r?\n$/, '');
|
|
823
|
+
} else if (bodyFromParam !== undefined || item.text.trim() !== '') {
|
|
824
|
+
malformed.push({ raw: item.raw + item.text, reason: 'unexpected-body' });
|
|
825
|
+
if (name === 'tf-final' && attrs['summary'] === undefined) {
|
|
826
|
+
attrs['summary'] = (bodyFromParam ?? item.text).trim();
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const validated = validateCommandAttrs(name, attrs);
|
|
830
|
+
if ('error' in validated) {
|
|
831
|
+
malformed.push({ raw: item.raw, reason: `dsml-invoke:${validated.error}` });
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
commands.push({ name, attrs: validated.attrs, dependsOn: validated.dependsOn, body, raw: item.raw });
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
for (const token of tokenizeDsmlBlock(block)) {
|
|
838
|
+
if (token.kind === 'text') {
|
|
839
|
+
if (cur === null) {
|
|
840
|
+
if (token.text.trim() !== '') malformed.push({ raw: token.text, reason: 'dsml-text' });
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
if (cur.paramName !== null) cur.paramValue += token.text;
|
|
844
|
+
else cur.text += token.text;
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const tag = token.tag;
|
|
848
|
+
if (tag.name === 'calls') continue;
|
|
849
|
+
if (tag.name === 'invoke') {
|
|
850
|
+
if (tag.closing) {
|
|
851
|
+
flush(true);
|
|
852
|
+
continue;
|
|
853
|
+
}
|
|
854
|
+
flush(true);
|
|
855
|
+
if (tag.commandName === null) {
|
|
856
|
+
malformed.push({ raw: token.raw, reason: 'dsml-unknown-invoke' });
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
const { name: _n, ...rest } = tag.attrs;
|
|
860
|
+
cur = {
|
|
861
|
+
name: tag.commandName,
|
|
862
|
+
attrs: rest,
|
|
863
|
+
text: '',
|
|
864
|
+
params: {},
|
|
865
|
+
paramName: null,
|
|
866
|
+
paramValue: '',
|
|
867
|
+
raw: token.raw,
|
|
868
|
+
complete: false,
|
|
869
|
+
};
|
|
870
|
+
if (tag.selfClosing) flush(true);
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (tag.name === 'parameter') {
|
|
874
|
+
if (tag.closing) {
|
|
875
|
+
if (cur !== null && cur.paramName !== null) {
|
|
876
|
+
cur.params[cur.paramName] = trimParamValue(cur.paramValue);
|
|
877
|
+
cur.paramName = null;
|
|
878
|
+
cur.paramValue = '';
|
|
879
|
+
}
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
if (cur === null) {
|
|
883
|
+
malformed.push({ raw: token.raw, reason: 'dsml-stray-parameter' });
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
const key = tag.attrs['name'];
|
|
887
|
+
if (key === undefined) {
|
|
888
|
+
malformed.push({ raw: token.raw, reason: 'dsml-parameter-without-name' });
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
cur.paramName = key;
|
|
892
|
+
cur.paramValue = '';
|
|
893
|
+
if (tag.selfClosing) {
|
|
894
|
+
cur.params[key] = '';
|
|
895
|
+
cur.paramName = null;
|
|
896
|
+
}
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
malformed.push({ raw: token.raw, reason: `dsml-unknown-tag:${tag.name}` });
|
|
900
|
+
}
|
|
901
|
+
flush(false);
|
|
902
|
+
return { commands, malformed };
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/** parameter 值:去掉首尾各一个换行(模型习惯把值写在标签下一行)。 */
|
|
906
|
+
function trimParamValue(value ) {
|
|
907
|
+
return value.replace(/^\r?\n/, '').replace(/\r?\n$/, '');
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** 属性 schema 校验(parseHeader 与 DSML 块共用;校验失败返回结构化 error)。 */
|
|
911
|
+
function validateCommandAttrs(
|
|
912
|
+
name ,
|
|
913
|
+
attrs ,
|
|
914
|
+
) {
|
|
915
|
+
const schema = COMMAND_SCHEMAS[name];
|
|
916
|
+
const allowed = new Set([...COMMON_ATTRS, ...schema.required, ...schema.optional]);
|
|
917
|
+
for (const key of Object.keys(attrs)) {
|
|
918
|
+
if (!allowed.has(key)) return { error: `unknown-attribute:${key}` };
|
|
919
|
+
}
|
|
920
|
+
for (const req of schema.required) {
|
|
921
|
+
if (attrs[req] === undefined) return { error: `missing-attribute:${req}` };
|
|
922
|
+
}
|
|
923
|
+
for (const key of ['from_line', 'to_line']) {
|
|
924
|
+
if (attrs[key] !== undefined && !/^\d+$/.test(attrs[key])) {
|
|
925
|
+
return { error: `bad-integer-attribute:${key}` };
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
return { attrs, dependsOn: buildDependsOn(attrs) };
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/** depends_on:逗号分隔,去空、去重、保序。 */
|
|
932
|
+
function buildDependsOn(attrs ) {
|
|
933
|
+
const dependsOn = [];
|
|
934
|
+
if (attrs['depends_on'] === undefined) return dependsOn;
|
|
935
|
+
const seen = new Set ();
|
|
936
|
+
for (const part of attrs['depends_on'].split(',')) {
|
|
937
|
+
const d = part.trim();
|
|
938
|
+
if (d === '' || seen.has(d)) continue;
|
|
939
|
+
seen.add(d);
|
|
940
|
+
dependsOn.push(d);
|
|
941
|
+
}
|
|
942
|
+
return dependsOn;
|
|
943
|
+
}
|
|
944
|
+
|
|
474
945
|
|
|
475
946
|
|
|
476
947
|
|
|
@@ -504,40 +975,18 @@ function parseHeader(raw ) {
|
|
|
504
975
|
if (!attr) return { error: `bad-attribute-syntax:${rest.slice(0, 20)}` };
|
|
505
976
|
const key = attr[1];
|
|
506
977
|
if (key in attrs) return { error: `duplicate-attribute:${key}` };
|
|
507
|
-
attrs[key] = attr[2];
|
|
978
|
+
attrs[key] = decodeXmlEntities(attr[2]);
|
|
508
979
|
rest = rest.slice(attr[0].length);
|
|
509
980
|
}
|
|
510
981
|
|
|
511
982
|
// depends_on:逗号分隔,去空、去重、保序
|
|
512
|
-
const dependsOn
|
|
513
|
-
if (attrs['depends_on'] !== undefined) {
|
|
514
|
-
const seen = new Set ();
|
|
515
|
-
for (const part of attrs['depends_on'].split(',')) {
|
|
516
|
-
const d = part.trim();
|
|
517
|
-
if (d === '') continue;
|
|
518
|
-
if (!seen.has(d)) {
|
|
519
|
-
seen.add(d);
|
|
520
|
-
dependsOn.push(d);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
983
|
+
const dependsOn = buildDependsOn(attrs);
|
|
524
984
|
|
|
525
985
|
if (known === null) return { name: null, attrs, dependsOn };
|
|
526
986
|
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
if (!allowed.has(key)) return { error: `unknown-attribute:${key}` };
|
|
531
|
-
}
|
|
532
|
-
for (const req of schema.required) {
|
|
533
|
-
if (attrs[req] === undefined) return { error: `missing-attribute:${req}` };
|
|
534
|
-
}
|
|
535
|
-
for (const key of ['from_line', 'to_line']) {
|
|
536
|
-
if (attrs[key] !== undefined && !/^\d+$/.test(attrs[key])) {
|
|
537
|
-
return { error: `bad-integer-attribute:${key}` };
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return { name: known, attrs, dependsOn };
|
|
987
|
+
const validated = validateCommandAttrs(known, attrs);
|
|
988
|
+
if ('error' in validated) return { error: validated.error };
|
|
989
|
+
return { name: known, attrs: validated.attrs, dependsOn: validated.dependsOn };
|
|
541
990
|
}
|
|
542
991
|
|
|
543
992
|
/**
|