@hugesb/dsh-stream-agent 0.0.0-stage → 0.3.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/CHANGELOG.md +497 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +202 -0
- package/README.en.md +349 -0
- package/README.md +300 -3
- package/cordis.patch.yml +77 -0
- package/dist/entry/action-gate.js +206 -0
- package/dist/entry/adapter-kit.js +192 -0
- package/dist/entry/context-render.js +119 -0
- package/dist/entry/control-bar-cli.js +289 -0
- package/dist/entry/control-bar.js +123 -0
- package/dist/entry/control-session.js +115 -0
- package/dist/entry/control-state.js +125 -0
- package/dist/entry/cordis-compat.js +95 -0
- package/dist/entry/eval-probe.js +112 -0
- package/dist/entry/intent-log.js +121 -0
- package/dist/entry/json-persistence.js +306 -0
- package/dist/entry/loop.js +3852 -0
- package/dist/entry/mock-provider.js +536 -0
- package/dist/entry/obs-broker.js +178 -0
- package/dist/entry/ptc-bridge.js +405 -0
- package/dist/entry/state-handle.js +217 -0
- package/dist/entry/takeover-registry.js +60 -0
- package/dist/kernel/control-bar.js +559 -0
- package/dist/kernel/final.js +62 -0
- package/dist/kernel/gate.js +464 -0
- package/dist/kernel/modes.js +253 -0
- package/dist/kernel/obs.js +517 -0
- package/dist/kernel/parser.js +632 -0
- package/dist/kernel/render.js +444 -0
- package/dist/kernel/state.js +416 -0
- package/dist/version.js +9 -0
- package/docs/TEST-PROPOSITIONS.md +139 -0
- package/docs/adr-001-segment-boundary.md +61 -0
- package/docs/architecture.md +104 -0
- package/docs/desktop-ui-feasibility.md +277 -0
- package/docs/disabled-lines-audit.md +53 -0
- package/docs/limitations.md +181 -0
- package/docs/protocol.md +99 -0
- package/docs/v0.4.0-plan.md +50 -0
- package/eval/METRICS.md +128 -0
- package/eval/RESULTS.md +246 -0
- package/eval/tasks/fixtures/big-report.md +298 -0
- package/eval/tasks/fixtures/build-manifest.txt +5 -0
- package/eval/tasks/fixtures/deploy.yaml +4 -0
- package/eval/tasks/fixtures/error.log +200 -0
- package/eval/tasks/fixtures/features.yaml +4 -0
- package/eval/tasks/fixtures/pipeline.yaml +4 -0
- package/eval/tasks/fixtures/sales-q1.csv +61 -0
- package/eval/tasks/fixtures/settings.yaml +4 -0
- package/eval/tasks/fixtures/users-roster.json +304 -0
- package/eval/tasks/fixtures/users-scores.json +20 -0
- package/eval/tasks/gen-fixtures.cjs +80 -0
- package/eval/tasks/tasks.json +150 -0
- package/eval/tools/f6-kill-drill.cjs +176 -0
- package/eval/tools/f6-kill-drill.worker.cjs +131 -0
- package/eval/tools/gen-results.cjs +191 -0
- package/eval/tools/machine-test.cjs +200 -0
- package/eval/tools/manual-code-scenarios.md +59 -0
- package/eval/tools/replay-check.cjs +311 -0
- package/eval/tools/runner.cjs +219 -0
- package/eval/tools/session-log.cjs +106 -0
- package/eval/tools/summarize.cjs +185 -0
- package/eval/tools/t3-cells.cjs +173 -0
- package/package.json +73 -6
- package/patches/adapters-smoke.yml +41 -0
- package/patches/eval-account.yml +36 -0
- package/patches/eval-baseline.yml +33 -0
- package/patches/eval-stream-code-off.yml +46 -0
- package/patches/eval-stream-code-on.yml +47 -0
- package/patches/eval-stream.yml +54 -0
- package/patches/takeover.yml +42 -0
- package/patches/test-preset.yml +23 -0
- package/preset/stream/cordis.patch.yml +39 -0
- package/preset/stream/package.json +13 -0
- package/preset.yml +56 -0
- package/profiles/stream/README.md +66 -0
- package/profiles/stream/cordis.patch.yml +12 -0
- package/profiles/stream/package.json +17 -0
- package/profiles/stream-headless/package.json +16 -0
- package/scripts/build.mjs +103 -0
- package/scripts/check-consistency.mjs +146 -0
- package/scripts/gate.mjs +138 -0
- package/scripts/install-check.mjs +116 -0
- package/scripts/install-cycle.mjs +88 -0
- package/scripts/release.mjs +415 -0
- package/scripts/repair-session-logs.mjs +403 -0
- package/src/entry/action-gate.ts +206 -0
- package/src/entry/adapter-kit.ts +192 -0
- package/src/entry/context-render.ts +119 -0
- package/src/entry/control-bar-cli.ts +289 -0
- package/src/entry/control-bar.ts +123 -0
- package/src/entry/control-session.ts +115 -0
- package/src/entry/control-state.ts +125 -0
- package/src/entry/cordis-compat.ts +95 -0
- package/src/entry/eval-probe.ts +112 -0
- package/src/entry/intent-log.ts +121 -0
- package/src/entry/json-persistence.ts +306 -0
- package/src/entry/loop.ts +3852 -0
- package/src/entry/mock-provider.ts +536 -0
- package/src/entry/obs-broker.ts +178 -0
- package/src/entry/ptc-bridge.ts +405 -0
- package/src/entry/state-handle.ts +217 -0
- package/src/entry/takeover-registry.ts +60 -0
- package/src/kernel/control-bar.ts +559 -0
- package/src/kernel/final.ts +62 -0
- package/src/kernel/gate.ts +464 -0
- package/src/kernel/modes.ts +253 -0
- package/src/kernel/obs.ts +517 -0
- package/src/kernel/parser.ts +632 -0
- package/src/kernel/render.ts +444 -0
- package/src/kernel/state.ts +416 -0
- package/src/version.ts +9 -0
- package/test/activation.test.ts +187 -0
- package/test/adapters.test.ts +676 -0
- package/test/control-bar.test.ts +486 -0
- package/test/desktop-ui.test.ts +359 -0
- package/test/eval-probe.test.ts +139 -0
- package/test/f5-plan.test.ts +221 -0
- package/test/f6-intent.test.ts +168 -0
- package/test/f7-segment.test.ts +190 -0
- package/test/fix-limits.test.ts +599 -0
- package/test/gate.test.ts +380 -0
- package/test/helpers/stub-loop.ts +240 -0
- package/test/kernel/obs.test.ts +329 -0
- package/test/kernel/parser.test.ts +450 -0
- package/test/loop.test.ts +1393 -0
- package/test/mock-provider.test.ts +386 -0
- package/test/ptc-bridge.test.ts +356 -0
- package/test/render.test.ts +418 -0
- package/test/s2-install-check.test.ts +46 -0
- package/test/scenarios/3-file-basic.json +13 -0
- package/test/scenarios/5-file-basic.json +22 -0
- package/test/scenarios/burn-more.json +9 -0
- package/test/scenarios/degraded-final.json +10 -0
- package/test/scenarios/fault-abort-xml.json +11 -0
- package/test/scenarios/fault-malformed-command.json +12 -0
- package/test/scenarios/fault-midstream-abort.json +11 -0
- package/test/scenarios/fault-saturating-output.json +9 -0
- package/test/scenarios/fault-timeout.json +9 -0
- package/test/scenarios/peek-first.json +11 -0
- package/test/scenarios/peek-second.json +11 -0
- package/test/scenarios/peek-third.json +10 -0
- package/test/scenarios/recover-final.json +10 -0
- package/test/scenarios/reroute-first.json +17 -0
- package/test/scenarios/reroute-second.json +16 -0
- package/test/scenarios/starvation-final.json +10 -0
- package/test/scenarios/starvation-promote.json +11 -0
- package/test/scenarios/wait-tools-continue.json +10 -0
- package/test/scenarios/wait-tools-xml.json +22 -0
- package/test/scenarios/wait-tools.json +15 -0
- package/test/session-format.test.ts +234 -0
- package/test/state.test.ts +335 -0
- package/test/stream-v02.test.ts +264 -0
- package/test/v031-display.test.ts +333 -0
- package/ui/client.js +576 -0
- package/ui/index.js +282 -0
- package/ui/package.json +26 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// entry/mock-provider.ts —— 离线假模型适配器(分派 05)
|
|
2
|
+
//
|
|
3
|
+
// 按剧本(scenario)吐 StreamChunk 的假模型,注册进 ctx.llm,
|
|
4
|
+
// 让 单流协议链路(loop → parser → gate → blob → ledger → 重入)零 API 成本完成 E2E。
|
|
5
|
+
//
|
|
6
|
+
// 上游契约(全部镜像自 npm @deepseek-ai/dsh@0.2.2-rc.1 的 .d.ts,file:line 见各类型注释):
|
|
7
|
+
// - 流以 finish chunk 终止;adapter 可抛错,LlmRuntime.stream() 把异常归一为
|
|
8
|
+
// 终止 error/aborted finish(types.d.ts:456 注释原文)。
|
|
9
|
+
// - adapter 经 ctx.llm.registerAdapter(providers, adapter) 注册(index.d.ts:262);
|
|
10
|
+
// registerConfigurableProviders 只登记设置目录条目(index.d.ts:290),
|
|
11
|
+
// 真正的流路由走 registerAdapter(dsh-llm-deepseek/lib/index.js:2735 实证)。
|
|
12
|
+
//
|
|
13
|
+
// 纪律(红线):
|
|
14
|
+
// - 零运行时依赖:不 import 任何 @deepseek-ai/*(本地插件 bare import 不走宿主解析,
|
|
15
|
+
// 见 results/01-micro-spike-results.md §0),也不 import 生产 entry(loop.ts)——
|
|
16
|
+
// mock 只依赖 llm 流契约,谁被测都一样。
|
|
17
|
+
// - fail-fast:剧本非法、waitTools 等不到信号、消费方从不 abort 一律快速抛清晰错误,
|
|
18
|
+
// 绝不挂死(红线 5 的反面)。
|
|
19
|
+
// - 只挂测试 preset(patches/test-preset.yml);生产 preset.yml 不得引用本 entry。
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// 流契约镜像(勿改语义;来源 file:line 见注释)
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/** 镜像 dsh-llm/lib/types/types.d.ts:33-51(本 mock 只用子集) */
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 镜像 dsh-llm/lib/types/types.d.ts:458-488 的 StreamChunk。
|
|
34
|
+
* mock 只会吐:block-start / text-delta / block-end / usage / finish。
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/** 镜像 dsh-llm/lib/types/types.d.ts:538-580 的 GenerateOptions(mock 只读子集) */
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// 剧本 DSL
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
/** 五种步骤类型(分派 05 硬性要求 1) */
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
export class MockScenarioError extends Error {}
|
|
95
|
+
export class MockStreamAbortError extends Error {}
|
|
96
|
+
|
|
97
|
+
const FAIL_MODES = new Set(['midstream_abort', 'timeout', 'malformed_command', 'saturating_output']);
|
|
98
|
+
|
|
99
|
+
/** 剧本全量校验(fail-fast:任何非法字段立即抛 MockScenarioError,绝不静默修正) */
|
|
100
|
+
export function validateScenario(raw , source ) {
|
|
101
|
+
const fail = (msg ) => {
|
|
102
|
+
throw new MockScenarioError(`[mock-provider] 剧本非法(${source}):${msg}`);
|
|
103
|
+
};
|
|
104
|
+
if (typeof raw !== 'object' || raw === null) fail('顶层必须是对象');
|
|
105
|
+
const s = raw ;
|
|
106
|
+
if (typeof s.name !== 'string' || s.name.length === 0) fail('name 必须是非空字符串');
|
|
107
|
+
if (!Array.isArray(s.steps) || s.steps.length === 0) fail('steps 必须是非空数组');
|
|
108
|
+
if (s.chunking !== undefined) {
|
|
109
|
+
if (typeof s.chunking !== 'object' || s.chunking === null) fail('chunking 必须是对象');
|
|
110
|
+
const c = s.chunking ;
|
|
111
|
+
if (c.charsPerChunk !== undefined && (typeof c.charsPerChunk !== 'number' || c.charsPerChunk < 0 || !Number.isInteger(c.charsPerChunk)))
|
|
112
|
+
fail('chunking.charsPerChunk 必须是非负整数');
|
|
113
|
+
if (c.delayMs !== undefined && (typeof c.delayMs !== 'number' || c.delayMs < 0)) fail('chunking.delayMs 必须是非负数');
|
|
114
|
+
}
|
|
115
|
+
let hasFinish = false;
|
|
116
|
+
let lastFailTerminal = false; // fail(midstream_abort|timeout|saturating_output) 本身就是流的终止,不强制要求 finish 步骤
|
|
117
|
+
const steps = s.steps.map((step, i) => {
|
|
118
|
+
if (typeof step !== 'object' || step === null) fail(`steps[${i}] 必须是对象`);
|
|
119
|
+
const st = step ;
|
|
120
|
+
switch (st.type) {
|
|
121
|
+
case 'text':
|
|
122
|
+
if (typeof st.text !== 'string') fail(`steps[${i}] text.text 必须是字符串`);
|
|
123
|
+
return { type: 'text', text: st.text } ;
|
|
124
|
+
case 'commands': {
|
|
125
|
+
if (!Array.isArray(st.commands) || st.commands.length === 0) fail(`steps[${i}] commands.commands 必须是非空数组`);
|
|
126
|
+
for (const c of st.commands) if (typeof c !== 'string' || c.length === 0) fail(`steps[${i}].commands 每项必须是非空字符串`);
|
|
127
|
+
return { type: 'commands', commands: [...st.commands] } ;
|
|
128
|
+
}
|
|
129
|
+
case 'waitTools': {
|
|
130
|
+
if (typeof st.count !== 'number' || !Number.isInteger(st.count) || st.count < 1) fail(`steps[${i}] waitTools.count 必须是正整数`);
|
|
131
|
+
if (st.timeoutMs !== undefined && (typeof st.timeoutMs !== 'number' || st.timeoutMs < 0)) fail(`steps[${i}] waitTools.timeoutMs 必须是非负数`);
|
|
132
|
+
return { type: 'waitTools', count: st.count, timeoutMs: st.timeoutMs } ;
|
|
133
|
+
}
|
|
134
|
+
case 'fail': {
|
|
135
|
+
if (typeof st.mode !== 'string' || !FAIL_MODES.has(st.mode)) {
|
|
136
|
+
fail(`steps[${i}] fail.mode 必须是 midstream_abort|timeout|malformed_command|saturating_output`);
|
|
137
|
+
}
|
|
138
|
+
if (st.text !== undefined && typeof st.text !== 'string') fail(`steps[${i}] fail.text 必须是字符串`);
|
|
139
|
+
if (st.delayMs !== undefined && (typeof st.delayMs !== 'number' || st.delayMs < 0)) fail(`steps[${i}] fail.delayMs 必须是非负数`);
|
|
140
|
+
if (st.maxChars !== undefined && (typeof st.maxChars !== 'number' || !Number.isInteger(st.maxChars) || st.maxChars < 1))
|
|
141
|
+
fail(`steps[${i}] fail.maxChars 必须是正整数`);
|
|
142
|
+
lastFailTerminal = st.mode === 'midstream_abort' || st.mode === 'timeout' || st.mode === 'saturating_output';
|
|
143
|
+
return { type: 'fail', mode: st.mode, text: st.text, delayMs: st.delayMs, maxChars: st.maxChars } ;
|
|
144
|
+
}
|
|
145
|
+
case 'finish':
|
|
146
|
+
if (st.reason !== undefined && st.reason !== 'stop' && st.reason !== 'max-tokens') fail(`steps[${i}] finish.reason 只支持 stop|max-tokens`);
|
|
147
|
+
hasFinish = true;
|
|
148
|
+
return { type: 'finish', reason: st.reason ?? 'stop' } ;
|
|
149
|
+
default:
|
|
150
|
+
fail(`steps[${i}] type 必须是 text|commands|waitTools|fail|finish,实际 ${String(st.type)}`);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
if (!hasFinish && !lastFailTerminal) fail('缺少 finish 步骤(流必须以 finish chunk 终止)');
|
|
154
|
+
const match = s.match ;
|
|
155
|
+
if (match !== undefined) {
|
|
156
|
+
if (typeof match !== 'object' || match === null) fail('match 必须是对象');
|
|
157
|
+
if (match.model !== undefined && typeof match.model !== 'string') fail('match.model 必须是字符串');
|
|
158
|
+
if (match.promptIncludes !== undefined && typeof match.promptIncludes !== 'string') fail('match.promptIncludes 必须是字符串');
|
|
159
|
+
if (match.model === undefined && match.promptIncludes === undefined) fail('match 至少声明 model 或 promptIncludes 之一');
|
|
160
|
+
}
|
|
161
|
+
return { name: s.name , match, chunking: s.chunking , steps };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// MockModel
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
/** 每次 stream 调用的记录(供调用次数断言与 08 号重放比对) */
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
const DEFAULT_CHUNKING = { charsPerChunk: 8, delayMs: 20 };
|
|
213
|
+
|
|
214
|
+
export function createMockModel(config ) {
|
|
215
|
+
if (!Array.isArray(config.scenarios) || config.scenarios.length === 0) {
|
|
216
|
+
throw new MockScenarioError('[mock-provider] config.scenarios 不能为空');
|
|
217
|
+
}
|
|
218
|
+
const provider = config.provider ?? 'mock';
|
|
219
|
+
const calls = [];
|
|
220
|
+
let toolCompletions = 0;
|
|
221
|
+
let callSeq = 0;
|
|
222
|
+
|
|
223
|
+
const model = {
|
|
224
|
+
provider,
|
|
225
|
+
calls,
|
|
226
|
+
notifyToolResult() {
|
|
227
|
+
toolCompletions += 1;
|
|
228
|
+
},
|
|
229
|
+
resetSignals() {
|
|
230
|
+
toolCompletions = 0;
|
|
231
|
+
},
|
|
232
|
+
adapter: {
|
|
233
|
+
providerInfo(p) {
|
|
234
|
+
return { id: p, name: 'Mock Scenario Model' };
|
|
235
|
+
},
|
|
236
|
+
providerRetryPolicy() {
|
|
237
|
+
return undefined;
|
|
238
|
+
},
|
|
239
|
+
imageRequestPricing() {
|
|
240
|
+
return undefined;
|
|
241
|
+
},
|
|
242
|
+
async listModels() {
|
|
243
|
+
return config.scenarios.map((s) => ({ provider, id: s.name, name: `mock:${s.name}` }));
|
|
244
|
+
},
|
|
245
|
+
async resolveModel(_p, id) {
|
|
246
|
+
return { provider, id, name: `mock:${id}`, context: { contextWindow: 131072 } };
|
|
247
|
+
},
|
|
248
|
+
stream(options) {
|
|
249
|
+
return runScenario(options);
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
function selectScenario(options ) {
|
|
255
|
+
const prompt = options.messages
|
|
256
|
+
.filter((m) => m.role === 'user')
|
|
257
|
+
.flatMap((m) => m.content)
|
|
258
|
+
.map((b) => b.text ?? '')
|
|
259
|
+
.join('\n');
|
|
260
|
+
const matched = config.scenarios.find((s) => {
|
|
261
|
+
if (!s.match) return false;
|
|
262
|
+
if (s.match.model !== undefined && s.match.model !== options.model) return false;
|
|
263
|
+
if (s.match.promptIncludes !== undefined && !prompt.includes(s.match.promptIncludes)) return false;
|
|
264
|
+
return true;
|
|
265
|
+
});
|
|
266
|
+
return matched ?? config.scenarios[0] ; // 无 match 的首个剧本作兜底
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function* chunkify(text , charsPerChunk ) {
|
|
270
|
+
if (charsPerChunk <= 0) {
|
|
271
|
+
yield text;
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
for (let i = 0; i < text.length; i += charsPerChunk) {
|
|
275
|
+
yield text.slice(i, i + charsPerChunk);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const sleep = (ms , signal ) =>
|
|
280
|
+
new Promise ((resolve) => {
|
|
281
|
+
if (ms <= 0) return resolve(signal?.aborted ? 'aborted' : 'ok');
|
|
282
|
+
const timer = setTimeout(done, ms);
|
|
283
|
+
function done() {
|
|
284
|
+
clearTimeout(timer);
|
|
285
|
+
signal?.removeEventListener('abort', onAbort);
|
|
286
|
+
resolve(signal?.aborted ? 'aborted' : 'ok');
|
|
287
|
+
}
|
|
288
|
+
function onAbort() {
|
|
289
|
+
done();
|
|
290
|
+
}
|
|
291
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const waitToolCompletions = (count , timeoutMs , signal ) =>
|
|
295
|
+
new Promise ((resolve) => {
|
|
296
|
+
if (toolCompletions >= count) return resolve('ok');
|
|
297
|
+
const timer = setTimeout(done, timeoutMs);
|
|
298
|
+
function settle(v ) {
|
|
299
|
+
clearInterval(poll);
|
|
300
|
+
clearTimeout(timer);
|
|
301
|
+
signal?.removeEventListener('abort', onAbort);
|
|
302
|
+
resolve(v);
|
|
303
|
+
}
|
|
304
|
+
function done() {
|
|
305
|
+
settle('timeout');
|
|
306
|
+
}
|
|
307
|
+
function onAbort() {
|
|
308
|
+
settle('aborted');
|
|
309
|
+
}
|
|
310
|
+
// 轮询信号计数(notifyToolResult 是同步计数器,无需事件系统)
|
|
311
|
+
const poll = setInterval(() => {
|
|
312
|
+
if (toolCompletions >= count) settle('ok');
|
|
313
|
+
}, 1);
|
|
314
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
async function* runScenario(options ) {
|
|
318
|
+
const scenario = selectScenario(options);
|
|
319
|
+
const chunking = { ...DEFAULT_CHUNKING, ...config.defaultChunking, ...scenario.chunking };
|
|
320
|
+
const record = {
|
|
321
|
+
seq: callSeq++,
|
|
322
|
+
model: options.model,
|
|
323
|
+
scenario: scenario.name,
|
|
324
|
+
chunkTypes: [],
|
|
325
|
+
emittedText: '',
|
|
326
|
+
finished: false,
|
|
327
|
+
};
|
|
328
|
+
calls.push(record);
|
|
329
|
+
const push = (chunk ) => {
|
|
330
|
+
record.chunkTypes.push(chunk.type);
|
|
331
|
+
if (chunk.type === 'text-delta') record.emittedText += chunk.text;
|
|
332
|
+
return chunk;
|
|
333
|
+
};
|
|
334
|
+
const abortedFinish = () => {
|
|
335
|
+
record.finished = true;
|
|
336
|
+
record.finishKind = 'aborted';
|
|
337
|
+
return {
|
|
338
|
+
type: 'finish',
|
|
339
|
+
reason: { kind: 'aborted', message: 'mock scenario aborted by consumer signal', code: 'MOCK_ABORTED' },
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
try {
|
|
344
|
+
let opened = false; // text block 只开一次,全程 index=0
|
|
345
|
+
let fullText = '';
|
|
346
|
+
const openBlock = function* () {
|
|
347
|
+
if (!opened) {
|
|
348
|
+
opened = true;
|
|
349
|
+
yield push({ type: 'block-start', index: 0, blockType: 'text' });
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const emitText = async function* (text ) {
|
|
353
|
+
yield* openBlock();
|
|
354
|
+
for (const piece of chunkify(text, chunking.charsPerChunk)) {
|
|
355
|
+
if (options.signal?.aborted) {
|
|
356
|
+
yield abortedFinish();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const idle = await sleep(chunking.delayMs, options.signal);
|
|
360
|
+
if (idle === 'aborted') {
|
|
361
|
+
yield abortedFinish();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
fullText += piece;
|
|
365
|
+
yield push({ type: 'text-delta', index: 0, text: piece });
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
for (const step of scenario.steps) {
|
|
370
|
+
switch (step.type) {
|
|
371
|
+
case 'text': {
|
|
372
|
+
yield* emitText(step.text);
|
|
373
|
+
if (record.finished) return;
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case 'commands': {
|
|
377
|
+
yield* emitText(`${step.commands.join('\n')}\n`);
|
|
378
|
+
if (record.finished) return;
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
case 'waitTools': {
|
|
382
|
+
const v = await waitToolCompletions(step.count, step.timeoutMs ?? 5000, options.signal);
|
|
383
|
+
if (v === 'aborted') {
|
|
384
|
+
yield abortedFinish();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (v === 'timeout') {
|
|
388
|
+
// fail-fast:等不到回灌信号绝不挂死
|
|
389
|
+
const msg = `[mock-provider] waitTools(${step.count}) 超时:剧本 "${scenario.name}" 等待 ${step.timeoutMs ?? 5000}ms 内未收到工具完成信号`;
|
|
390
|
+
record.finished = true;
|
|
391
|
+
record.finishKind = 'error';
|
|
392
|
+
yield { type: 'finish', reason: { kind: 'error', message: msg, code: 'MOCK_WAITTOOLS_TIMEOUT' } };
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
case 'fail': {
|
|
398
|
+
switch (step.mode) {
|
|
399
|
+
case 'midstream_abort': {
|
|
400
|
+
// 适配器抛错 → 上层(LlmRuntime / 测试 shim)归一为终止 error finish
|
|
401
|
+
throw new MockStreamAbortError(`[mock-provider] 剧本 "${scenario.name}" 注入 midstream_abort`);
|
|
402
|
+
}
|
|
403
|
+
case 'timeout': {
|
|
404
|
+
const v = await sleep(step.delayMs ?? 30000, options.signal);
|
|
405
|
+
if (v === 'aborted') {
|
|
406
|
+
yield abortedFinish();
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
// fail-fast:消费方从不 abort → 快速抛错而非无限挂起
|
|
410
|
+
throw new MockScenarioError(
|
|
411
|
+
`[mock-provider] 剧本 "${scenario.name}" timeout 注入未被消费方 abort(delayMs=${step.delayMs ?? 30000} 已耗尽)——消费方缺少超时处理路径`,
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
case 'malformed_command': {
|
|
415
|
+
yield* emitText(step.text ?? 'tf-frobnicate x\n');
|
|
416
|
+
if (record.finished) return;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
case 'saturating_output': {
|
|
420
|
+
yield* openBlock();
|
|
421
|
+
const maxChars = step.maxChars ?? 4_000_000;
|
|
422
|
+
const filler = 'x'.repeat(Math.max(chunking.charsPerChunk, 64));
|
|
423
|
+
let sent = 0;
|
|
424
|
+
while (sent < maxChars) {
|
|
425
|
+
if (options.signal?.aborted) {
|
|
426
|
+
yield abortedFinish();
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const idle = await sleep(chunking.delayMs, options.signal);
|
|
430
|
+
if (idle === 'aborted') {
|
|
431
|
+
yield abortedFinish();
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const piece = filler.slice(0, Math.min(filler.length, maxChars - sent));
|
|
435
|
+
fullText += piece;
|
|
436
|
+
sent += piece.length;
|
|
437
|
+
yield push({ type: 'text-delta', index: 0, text: piece });
|
|
438
|
+
}
|
|
439
|
+
record.finished = true;
|
|
440
|
+
record.finishKind = 'max-tokens';
|
|
441
|
+
yield { type: 'finish', reason: { kind: 'max-tokens' } };
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case 'finish': {
|
|
448
|
+
yield push({ type: 'block-end', index: 0, block: { type: 'text', text: fullText } });
|
|
449
|
+
yield push({ type: 'usage', usage: { inputTokens: Math.ceil(promptCharsOf(options) / 4), outputTokens: Math.ceil(fullText.length / 4) } });
|
|
450
|
+
record.finished = true;
|
|
451
|
+
record.finishKind = step.reason ?? 'stop';
|
|
452
|
+
yield push({ type: 'finish', reason: { kind: step.reason ?? 'stop' } });
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// 剧本走完没有触发 finish 步骤(validateScenario 保证存在 finish,防御性兜底)
|
|
458
|
+
throw new MockScenarioError(`[mock-provider] 剧本 "${scenario.name}" 步骤耗尽仍未 finish(校验漏洞,请报告)`);
|
|
459
|
+
} catch (error) {
|
|
460
|
+
// fail-open 开关(红线 5 反面):mock 自身异常快速失败,绝不挂死。
|
|
461
|
+
// 已发过 finish 后不再重复发。
|
|
462
|
+
if (!record.finished) {
|
|
463
|
+
record.finished = true;
|
|
464
|
+
record.finishKind = 'error';
|
|
465
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
466
|
+
yield { type: 'finish', reason: { kind: 'error', message, code: 'MOCK_ADAPTER_THROWN' } };
|
|
467
|
+
}
|
|
468
|
+
throw error;
|
|
469
|
+
} finally {
|
|
470
|
+
// 宿主提前退出迭代(消费方 break/return)也如实记录
|
|
471
|
+
if (!record.finished) {
|
|
472
|
+
record.finished = true;
|
|
473
|
+
record.finishKind = 'consumer-early-exit';
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function promptCharsOf(options ) {
|
|
479
|
+
return options.messages.reduce((n, m) => n + m.content.reduce((k, b) => k + (b.text?.length ?? 0), 0), 0);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return model;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// ---------------------------------------------------------------------------
|
|
486
|
+
// Cordis entry(只挂测试 preset)
|
|
487
|
+
// ---------------------------------------------------------------------------
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* 函数插件入口。挂载后:
|
|
491
|
+
* 1. ctx.llm.registerAdapter(['mock'], adapter) —— 流路由;
|
|
492
|
+
* 2. ctx.llm.registerConfigurableProviders([...]) —— 设置目录条目(F10,deepseek-account:136 同款);
|
|
493
|
+
* 3. ctx.mockModel = model —— 06/07 号 loop 的工具完成信号源(notifyToolResult)。
|
|
494
|
+
*
|
|
495
|
+
* config: { scenarios?: Scenario[](或 JSON 数组), provider?: string }
|
|
496
|
+
* 剧本文件放 test/scenarios/*.json,代码与数据分离(红线)。
|
|
497
|
+
*/
|
|
498
|
+
export default function apply(ctx
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
, config ) {
|
|
507
|
+
const provider = config?.provider ?? 'mock';
|
|
508
|
+
const scenarios = (config?.scenarios ?? []).map((s, i) => validateScenario(s, `config.scenarios[${i}]`));
|
|
509
|
+
const model = createMockModel({ scenarios, provider });
|
|
510
|
+
|
|
511
|
+
const llm = ctx.llm ?? (ctx.get?.('llm') );
|
|
512
|
+
if (!llm || typeof (llm ).registerAdapter !== 'function') {
|
|
513
|
+
console.error('[mock-provider] ctx.llm 不可用或缺少 registerAdapter——entry 挂载失败(fail-fast)');
|
|
514
|
+
return undefined;
|
|
515
|
+
}
|
|
516
|
+
const disposeAdapter = (llm ).registerAdapter([provider], model.adapter);
|
|
517
|
+
// 设置目录条目仅在有该 API 的宿主上登记(离线桩环境可没有)
|
|
518
|
+
if (typeof (llm ).registerConfigurableProviders === 'function') {
|
|
519
|
+
(llm ).registerConfigurableProviders([
|
|
520
|
+
{
|
|
521
|
+
provider,
|
|
522
|
+
displayName: 'Mock Scenario Model',
|
|
523
|
+
settingsNs: ctx.fiber?.entry?.options?.id ?? 'mock-provider',
|
|
524
|
+
settingsPath: [],
|
|
525
|
+
},
|
|
526
|
+
]);
|
|
527
|
+
}
|
|
528
|
+
(ctx ).mockModel = model;
|
|
529
|
+
ctx.effect?.(
|
|
530
|
+
() => () => {
|
|
531
|
+
disposeAdapter?.();
|
|
532
|
+
},
|
|
533
|
+
'mock-provider.registerAdapter()',
|
|
534
|
+
);
|
|
535
|
+
return model;
|
|
536
|
+
}
|