dsh-zcode-startplan 0.5.2 → 1.0.3
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 +1963 -672
- package/README.md +92 -472
- package/bin/cli.mjs +509 -450
- package/client.js +2459 -1002
- package/cordis.patch.yml +4 -2
- package/index.js +109 -11
- package/lib/adapter.js +476 -449
- package/lib/anthropic-body.js +513 -469
- package/lib/assemble.js +808 -542
- package/lib/auto-claim.js +229 -0
- package/lib/captcha/minter.js +61 -7
- package/lib/distribution-keys.js +202 -0
- package/lib/errors.js +281 -125
- package/lib/fallback.js +120 -0
- package/lib/local-endpoint.js +367 -352
- package/lib/manage-routes.js +1245 -652
- package/lib/model-catalog.js +57 -0
- package/lib/openai-gateway.js +776 -0
- package/lib/plan-client.js +942 -532
- package/lib/pool.js +771 -478
- package/lib/sse.js +429 -317
- package/lib/upstream-api.js +948 -611
- package/lib/upstream.js +294 -134
- package/lib/version-probe.js +110 -0
- package/package.json +84 -81
- package/scripts/analyze-grant-intervals.mjs +171 -0
- package/scripts/check-compat-rc3.mjs +21 -11
- package/scripts/debug-claimable.mjs +130 -0
- package/scripts/probe-marketing.mjs +203 -0
- package/scripts/restart-dsh.ps1 +119 -116
- package/scripts/test-claimed-pending.mjs +152 -0
- package/scripts/verify_compat.mjs +75 -64
- package/scripts/watch-marketing.mjs +155 -0
- package/tests/auto-claim.test.mjs +318 -0
- package/tests/claim-routes.test.mjs +438 -293
- package/tests/client-panel.test.mjs +1141 -356
- package/tests/distribution-keys.test.mjs +123 -0
- package/tests/e2e.test.mjs +388 -353
- package/tests/fallback-scrub.test.mjs +105 -0
- package/tests/launch-command.test.mjs +68 -0
- package/tests/openai-gateway.test.mjs +454 -0
- package/tests/pinned-models.test.mjs +505 -497
- package/tests/plan-client.test.mjs +582 -222
- package/tests/pool.test.mjs +319 -93
- package/tests/quota-blackhole.test.mjs +409 -0
- package/tests/restart-script.test.mjs +53 -0
- package/tests/routing-quota.test.mjs +251 -0
- package/tests/support/hanging-stream.mjs +48 -0
- package/tests/upstream-error-200.test.mjs +164 -125
- package/tests/upstream.test.mjs +330 -250
- package/tests/version-probe.test.mjs +172 -0
package/lib/anthropic-body.js
CHANGED
|
@@ -1,469 +1,513 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 请求体组装:把 DSH 的 `GenerateOptions`(provider-neutral 词汇)翻成
|
|
3
|
-
* zcode-plan 端点要求的 Anthropic Messages 形态。
|
|
4
|
-
*
|
|
5
|
-
* ## 三道准入闸门(缺一即 3012 / 3007,且 3012 有账号冷却惩罚)
|
|
6
|
-
*
|
|
7
|
-
* 1. **官方身份块**:`system` 必须以官方身份块结构**开头**(cliPrefix + stable 段),
|
|
8
|
-
* 否则 `3012 unusual activity`。实测矩阵:无 system → 3012;只有 cliPrefix → 3012;
|
|
9
|
-
* cliPrefix + stable → 200。判据是「身份块是否存在」,与 HTTP 头 / 运行时无关。
|
|
10
|
-
* 调用方(DSH)的 system 内容**追加在最后**,不得前置。
|
|
11
|
-
* 2. **首轮日期块**:第一条 user 消息的 content **数组**最前面插一个
|
|
12
|
-
* `<system-reminder>` 日期块(纯文本 content 会被转成块数组)。幂等:已以此开头则跳过。
|
|
13
|
-
* 3. **tools 缓存断点**:只在**最后一个** tool 上打 `cache_control` —— Anthropic 缓存是
|
|
14
|
-
* 前缀式的,一个断点即覆盖 system + 全部 tools;单请求最多 4 个断点。
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 第一块:CLI 身份前缀(官方以此开头,42 字符)。
|
|
19
|
-
* ⚠ 不要"优化"这段文本:上游的准入检查看的是前缀结构。
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*/
|
|
31
|
-
export const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
'
|
|
43
|
-
'
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (typeof
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 请求体组装:把 DSH 的 `GenerateOptions`(provider-neutral 词汇)翻成
|
|
3
|
+
* zcode-plan 端点要求的 Anthropic Messages 形态。
|
|
4
|
+
*
|
|
5
|
+
* ## 三道准入闸门(缺一即 3012 / 3007,且 3012 有账号冷却惩罚)
|
|
6
|
+
*
|
|
7
|
+
* 1. **官方身份块**:`system` 必须以官方身份块结构**开头**(cliPrefix + stable 段),
|
|
8
|
+
* 否则 `3012 unusual activity`。实测矩阵:无 system → 3012;只有 cliPrefix → 3012;
|
|
9
|
+
* cliPrefix + stable → 200。判据是「身份块是否存在」,与 HTTP 头 / 运行时无关。
|
|
10
|
+
* 调用方(DSH)的 system 内容**追加在最后**,不得前置。
|
|
11
|
+
* 2. **首轮日期块**:第一条 user 消息的 content **数组**最前面插一个
|
|
12
|
+
* `<system-reminder>` 日期块(纯文本 content 会被转成块数组)。幂等:已以此开头则跳过。
|
|
13
|
+
* 3. **tools 缓存断点**:只在**最后一个** tool 上打 `cache_control` —— Anthropic 缓存是
|
|
14
|
+
* 前缀式的,一个断点即覆盖 system + 全部 tools;单请求最多 4 个断点。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 第一块:CLI 身份前缀(官方以此开头,42 字符)。
|
|
19
|
+
* ⚠ 不要"优化"这段文本:上游的准入检查看的是前缀结构。
|
|
20
|
+
*
|
|
21
|
+
* ## 文本出处(Apache-2.0,可放心逐字使用)
|
|
22
|
+
*
|
|
23
|
+
* 身份块文本与开源 ZCode 仓库**逐字一致**:
|
|
24
|
+
* [zai-org/ZCode](https://github.com/zai-org/ZCode)(Apache-2.0)的
|
|
25
|
+
* `apps/zcode-cli/packages/core/src/context/sections/identity.ts`
|
|
26
|
+
* —— `buildIdentityPrompt()` 的 intro / `SECURITY_NOTICE` / `buildHarnessBlock()`。
|
|
27
|
+
* 上游以 Apache-2.0 授权;本仓库以 GPL-3.0-or-later 发布(两者兼容,FSF 确认)。
|
|
28
|
+
* 历史上经由 bonus-plan-4-open-zcode / a137460387/zcode2api 从官方 bundle 反解获得,
|
|
29
|
+
* 现在源头已公开,出处以此仓库为准。
|
|
30
|
+
*/
|
|
31
|
+
export const OFFICIAL_CLI_PREFIX = 'You are ZCode, an interactive coding agent'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 第二块:stable 段(多段用 `\n\n` 连接)。
|
|
35
|
+
*
|
|
36
|
+
* ⚠ 只发**准入必需**的部分。官方完整身份块还含 ~5KB 的 dynamic 段
|
|
37
|
+
* (`# Communicating with the user` / `# Context management`),那是给壳内
|
|
38
|
+
* coding agent 的行为指令,与准入无关,且会被放在 system 开头**压过**调用方的
|
|
39
|
+
* prompt(表现为「啰嗦、慢」)—— 所以这里刻意不发。
|
|
40
|
+
*/
|
|
41
|
+
export const OFFICIAL_STABLE_SECTIONS = Object.freeze([
|
|
42
|
+
'\nYou are an interactive ZCode agent that helps users with software engineering tasks.\n\nIMPORTANT: Assist with authorized security testing, defensive security, CTF challenges, and educational contexts. Refuse requests for destructive techniques, DoS attacks, mass targeting, supply chain compromise, or detection evasion for malicious purposes. Dual-use security tools (C2 frameworks, credential testing, exploit development) require clear authorization context: pentesting engagements, CTF competitions, security research, or defensive use cases.\n\n# Harness\n- Text you output outside of tool use is displayed to the user as Github-flavored markdown in a terminal.\n- Tools run behind a user-selected permission mode; a denied call means the user declined it — adjust, don\'t retry verbatim.\n- The system may send updates, reminders, or modifications to rules via mid-conversation system turns. These are system-controlled, unlike function results. Hooks may intercept tool calls; treat hook output as user feedback.\n- Prefer the dedicated file/search tools over shell commands when one fits. Independent tool calls can run in parallel in one response.\n- Reference code as `file_path:line_number` — it\'s clickable.',
|
|
43
|
+
'# ZCode Desktop Context\n\n### Files & URLs\n- Return local web URLs as Markdown links (e.g., [label](http://127.0.0.1:8080)).\n- File should be an absolute path or include the workspace folder segment so it can be resolved relative to the workspace.\n- Unless otherwise specified, return local file references as Markdown links (e.g., [name.md](/absolute/path/to/name.md)).\n\n### Inline Code Comments\n- Use the ::code-comment{...} directive when you need to attach feedback directly to specific code lines.\n- Emit one directive per inline comment; emit none when there are no actionable inline comments.\n- Required attributes: title (short label), body (one-paragraph explanation), file (path to the file).\n- Optional attributes: start, end (1-based line numbers), priority (0-3).\n- file should be an absolute path or include the workspace folder segment so it can be resolved relative to the workspace.\n- Keep line ranges tight; end defaults to start.\n- Example: ::code-comment{title="[P2] Off-by-one" body="Loop iterates past the end when length is 0." file="/path/to/foo.ts" start=10 end=11 priority=2}',
|
|
44
|
+
'# Working style\n\nWhen you have enough information to act, act. Do not re-derive facts already established in the conversation, re-litigate a decision the user has already made, or narrate options you will not pursue. If you are weighing a choice, give a recommendation, not an exhaustive survey. Prefer reading the actual file or running the actual command over reasoning about what it probably contains. When a signal pattern-matches to a known failure, check that the evidence actually supports that specific diagnosis before acting on it.',
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
/** `<system-reminder>` 日期块的固定文案(官方逐字)。 */
|
|
48
|
+
const CONTEXT_PREFIX_INTRO = "As you answer the user's questions, you can use the following context:"
|
|
49
|
+
|
|
50
|
+
/** 结尾段(⚠ 前有 **6 个空格**缩进,官方逐字如此)。 */
|
|
51
|
+
const CONTEXT_PREFIX_OUTRO =
|
|
52
|
+
' IMPORTANT: this context may or may not be relevant to your tasks. ' +
|
|
53
|
+
'You should not respond to this context unless it is highly relevant to your task.'
|
|
54
|
+
|
|
55
|
+
/** 本地时区的 ISO 日期(官方用本地日期,**不是** UTC)。 */
|
|
56
|
+
export function formatLocalIsoDate(date = new Date()) {
|
|
57
|
+
const pad = (value) => String(value).padStart(2, '0')
|
|
58
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 构造 `<system-reminder>` 上下文块(整块是**一个** text 块)。 */
|
|
62
|
+
export function buildContextPrefixBlock(now = new Date()) {
|
|
63
|
+
const body = [
|
|
64
|
+
CONTEXT_PREFIX_INTRO,
|
|
65
|
+
`# currentDate\nToday's date is ${formatLocalIsoDate(now)}.`,
|
|
66
|
+
'',
|
|
67
|
+
CONTEXT_PREFIX_OUTRO,
|
|
68
|
+
].join('\n')
|
|
69
|
+
return { type: 'text', text: `<system-reminder>${body}</system-reminder>` }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 判断一条消息的 content 是否已经以日期块开头(幂等判据)。 */
|
|
73
|
+
function startsWithSystemReminder(content) {
|
|
74
|
+
if (typeof content === 'string') return content.startsWith('<system-reminder>')
|
|
75
|
+
if (Array.isArray(content)) {
|
|
76
|
+
const first = content[0]
|
|
77
|
+
return typeof first?.text === 'string' && first.text.startsWith('<system-reminder>')
|
|
78
|
+
}
|
|
79
|
+
return false
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 给**首轮 user 消息**的 content 数组最前面插入日期块。
|
|
84
|
+
*
|
|
85
|
+
* 规则(官方行为):只处理第一条消息且必须是 user;幂等;纯文本 content 转块数组。
|
|
86
|
+
*/
|
|
87
|
+
export function withContextPrefix(messages, now = new Date()) {
|
|
88
|
+
const out = messages.map((message) => ({ ...message }))
|
|
89
|
+
const first = out[0]
|
|
90
|
+
if (first === undefined || first.role !== 'user') return out
|
|
91
|
+
if (startsWithSystemReminder(first.content)) return out
|
|
92
|
+
|
|
93
|
+
const prefix = buildContextPrefixBlock(now)
|
|
94
|
+
if (Array.isArray(first.content)) first.content = [prefix, ...first.content]
|
|
95
|
+
else if (typeof first.content === 'string') first.content = [prefix, { type: 'text', text: first.content }]
|
|
96
|
+
else first.content = [prefix]
|
|
97
|
+
return out
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** # Environment 段。 */
|
|
101
|
+
export function buildEnvironmentSection(options) {
|
|
102
|
+
const cwd = typeof options.cwd === 'string' && options.cwd.trim().length > 0 ? options.cwd : '.'
|
|
103
|
+
const provider = options.provider ?? 'zcode'
|
|
104
|
+
const model = options.model ?? 'glm-5.3-flash'
|
|
105
|
+
const platform = options.platform ?? process.platform
|
|
106
|
+
return [
|
|
107
|
+
'# Environment',
|
|
108
|
+
'You have been invoked in the following environment:',
|
|
109
|
+
` - Primary working directory: ${cwd}`,
|
|
110
|
+
` - Platform: ${platform}`,
|
|
111
|
+
` - Shell: ${platform === 'win32' ? 'powershell' : 'bash'}`,
|
|
112
|
+
` - You are powered by the model named ${provider}/${model}.`,
|
|
113
|
+
].join('\n')
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 构造官方形态的 system 块数组。
|
|
118
|
+
*
|
|
119
|
+
* 结构(逐字复刻官方,**不要优化**):
|
|
120
|
+
* block[0] = cliPrefix ← 准入必需
|
|
121
|
+
* block[1] = stable ← 准入必需
|
|
122
|
+
* block[2] = # Environment
|
|
123
|
+
* block[3..] = 调用方 system(追加在最后)
|
|
124
|
+
*
|
|
125
|
+
* ⚠ 只在**最后一块**打 cache_control(前缀式缓存,一个断点覆盖面最大)。
|
|
126
|
+
*/
|
|
127
|
+
export function buildZcodeSystemBlocks(callerSystem, options = {}) {
|
|
128
|
+
const stable = OFFICIAL_STABLE_SECTIONS.join('\n\n')
|
|
129
|
+
const blocks = [
|
|
130
|
+
{ type: 'text', text: OFFICIAL_CLI_PREFIX },
|
|
131
|
+
{ type: 'text', text: stable },
|
|
132
|
+
{ type: 'text', text: buildEnvironmentSection(options) },
|
|
133
|
+
]
|
|
134
|
+
if (typeof callerSystem === 'string' && callerSystem.trim().length > 0) {
|
|
135
|
+
blocks.push({ type: 'text', text: callerSystem })
|
|
136
|
+
}
|
|
137
|
+
const last = blocks[blocks.length - 1]
|
|
138
|
+
if (last !== undefined) last.cache_control = { type: 'ephemeral' }
|
|
139
|
+
return blocks
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 把任意形态的 system 归一成块数组(Anthropic 允许字符串或块数组)。 */
|
|
143
|
+
export function normalizeSystem(system) {
|
|
144
|
+
if (typeof system === 'string') {
|
|
145
|
+
return system.trim().length > 0 ? [{ type: 'text', text: system }] : []
|
|
146
|
+
}
|
|
147
|
+
if (!Array.isArray(system)) return []
|
|
148
|
+
const blocks = []
|
|
149
|
+
for (const item of system) {
|
|
150
|
+
if (typeof item !== 'object' || item === null) continue
|
|
151
|
+
if (item.type !== 'text' || typeof item.text !== 'string') continue
|
|
152
|
+
const block = { type: 'text', text: item.text }
|
|
153
|
+
if (item.cache_control?.type === 'ephemeral') block.cache_control = { type: 'ephemeral' }
|
|
154
|
+
blocks.push(block)
|
|
155
|
+
}
|
|
156
|
+
return blocks
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 给工具表的**最后一个**工具打缓存断点。 */
|
|
160
|
+
export function withToolCacheBreakpoint(tools) {
|
|
161
|
+
if (!Array.isArray(tools) || tools.length === 0) return []
|
|
162
|
+
return tools.map((tool, index) =>
|
|
163
|
+
index === tools.length - 1 ? { ...tool, cache_control: { type: 'ephemeral' } } : { ...tool },
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* DSH 工具定义 → Anthropic 工具声明。
|
|
169
|
+
*
|
|
170
|
+
* @param {readonly any[]} tools DSH 的 ToolDefinition 列表
|
|
171
|
+
* @returns {Array<any>}
|
|
172
|
+
*/
|
|
173
|
+
export function convertTools(tools) {
|
|
174
|
+
if (!Array.isArray(tools) || tools.length === 0) return []
|
|
175
|
+
const out = []
|
|
176
|
+
for (const tool of tools) {
|
|
177
|
+
if (typeof tool?.name !== 'string' || typeof tool?.parameters !== 'object' || tool.parameters === null) continue
|
|
178
|
+
const schema = { ...tool.parameters }
|
|
179
|
+
/** Anthropic 要求顶层 type=object;DSH 的 schema 本来就满足,缺失时补上。 */
|
|
180
|
+
if (schema.type === undefined) schema.type = 'object'
|
|
181
|
+
/** Anthropic 不接受 `$schema` 之类的键。 */
|
|
182
|
+
delete schema['$schema']
|
|
183
|
+
delete schema['additionalProperties']
|
|
184
|
+
out.push({
|
|
185
|
+
name: tool.name,
|
|
186
|
+
...(typeof tool.description === 'string' && tool.description.length > 0 ? { description: tool.description } : {}),
|
|
187
|
+
input_schema: schema,
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
return withToolCacheBreakpoint(out)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** DSH 保存的是原始 JSON 字符串;Anthropic tool_use.input 要求对象。 */
|
|
194
|
+
function parseToolArguments(raw) {
|
|
195
|
+
if (typeof raw === 'object' && raw !== null && !Array.isArray(raw)) return raw
|
|
196
|
+
if (typeof raw !== 'string' || raw.length === 0) return {}
|
|
197
|
+
try {
|
|
198
|
+
const value = JSON.parse(raw)
|
|
199
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {}
|
|
200
|
+
} catch {
|
|
201
|
+
return {}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** 单值取第一个字符串字段(工具表列出的键按序尝试)。 */
|
|
206
|
+
function pickString(value, ...keys) {
|
|
207
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
208
|
+
for (const key of keys) {
|
|
209
|
+
const item = value[key]
|
|
210
|
+
if (typeof item === 'string' && item.length > 0) return item
|
|
211
|
+
}
|
|
212
|
+
return undefined
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 把 DSH 的 `ImageAttachmentRef`(宿主 `ImageBlock.attachment`,持久引用)转成
|
|
217
|
+
* Anthropic 的 base64 image 块。
|
|
218
|
+
*
|
|
219
|
+
* ## 宿主契约(rc.3 实测,这是「图片被静默丢弃」的根因)
|
|
220
|
+
*
|
|
221
|
+
* DSH 传给适配器的 image 块是 `{ type: 'image', attachment: ImageAttachmentRef }`
|
|
222
|
+
* (`dsh-llm/lib/types` 的 `ImageBlock`),**不是** base64。引用形态(实测
|
|
223
|
+
* `attachment-local` 后端):
|
|
224
|
+
*
|
|
225
|
+
* ```
|
|
226
|
+
* { attachmentId: 'sha256:<hex>', mediaType: 'image/png', width, height, bytes, name? }
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* 字节要经附件服务 `readImageRequest(ref, target)` 解析(见 `resolveImages`);
|
|
230
|
+
* 这里只做**已解析字节 → wire 块**的纯转换。
|
|
231
|
+
*
|
|
232
|
+
* @param {any} ref DSH 的 ImageAttachmentRef
|
|
233
|
+
* @param {Uint8Array} data 请求图片字节
|
|
234
|
+
* @returns {Record<string, unknown>} Anthropic base64 image 块
|
|
235
|
+
*/
|
|
236
|
+
export function imageBlockFromAttachment(ref, data) {
|
|
237
|
+
const base64 =
|
|
238
|
+
typeof Buffer !== 'undefined' && typeof Buffer.from === 'function'
|
|
239
|
+
? Buffer.from(data).toString('base64')
|
|
240
|
+
: bytesToBase64(data)
|
|
241
|
+
return {
|
|
242
|
+
type: 'image',
|
|
243
|
+
source: {
|
|
244
|
+
type: 'base64',
|
|
245
|
+
media_type: typeof ref?.mediaType === 'string' ? ref.mediaType : 'image/png',
|
|
246
|
+
data: base64,
|
|
247
|
+
},
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 纯 JS base64(无 Node Buffer 的环境兜底)。 */
|
|
252
|
+
function bytesToBase64(bytes) {
|
|
253
|
+
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
254
|
+
let out = ''
|
|
255
|
+
for (let index = 0; index < bytes.length; index += 3) {
|
|
256
|
+
const remaining = bytes.length - index
|
|
257
|
+
const b0 = bytes[index]
|
|
258
|
+
const b1 = remaining > 1 ? bytes[index + 1] : 0
|
|
259
|
+
const b2 = remaining > 2 ? bytes[index + 2] : 0
|
|
260
|
+
out += table[b0 >> 2]
|
|
261
|
+
out += table[((b0 & 0x03) << 4) | (b1 >> 4)]
|
|
262
|
+
out += remaining > 1 ? table[((b1 & 0x0f) << 2) | (b2 >> 6)] : '='
|
|
263
|
+
out += remaining > 2 ? table[b2 & 0x3f] : '='
|
|
264
|
+
}
|
|
265
|
+
return out
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 图片块的宿主 attachment 引用 id(`pickString` 依次试三个字段名)。
|
|
270
|
+
* `imageOccurrences` 与 `convertContent` 共用同一份判据 —— 两处各写一份曾在
|
|
271
|
+
* 字段名集合漂移时产生过「预扫描到了、转换时丢图」的隐患。
|
|
272
|
+
*/
|
|
273
|
+
const attachmentIdOf = (block) => pickString(block.attachment, 'attachmentId', 'attachment_id', 'id')
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 遍历一条消息 content 里的图片引用(宿主 `ImageBlock` 形态优先,兼容内联 base64)。
|
|
277
|
+
*
|
|
278
|
+
* @param {readonly any[]} blocks
|
|
279
|
+
* @returns {Array<{ index: number, attachmentId: string, ref: any, inline?: { mediaType: string, data: string } }>}
|
|
280
|
+
*/
|
|
281
|
+
export function imageOccurrences(blocks) {
|
|
282
|
+
if (!Array.isArray(blocks)) return []
|
|
283
|
+
const found = []
|
|
284
|
+
for (let index = 0; index < blocks.length; index += 1) {
|
|
285
|
+
const block = blocks[index]
|
|
286
|
+
if (block === null || typeof block !== 'object' || block.type !== 'image') continue
|
|
287
|
+
/** 宿主流派:持久引用(attachment + attachmentId)。 */
|
|
288
|
+
const attachmentId = attachmentIdOf(block)
|
|
289
|
+
if (typeof attachmentId === 'string' && attachmentId.length > 0) {
|
|
290
|
+
found.push({ index, attachmentId, ref: block.attachment })
|
|
291
|
+
continue
|
|
292
|
+
}
|
|
293
|
+
/** 防御:内联 base64(旧测试/其他宿主形态)原样携带,不需要解析。 */
|
|
294
|
+
if (block.source?.type === 'base64' && typeof block.source.data === 'string') {
|
|
295
|
+
found.push({
|
|
296
|
+
index,
|
|
297
|
+
attachmentId: `inline:${index}`,
|
|
298
|
+
ref: null,
|
|
299
|
+
inline: { mediaType: block.source.media_type ?? 'image/png', data: block.source.data },
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return found
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* DSH content blocks → Anthropic content blocks。
|
|
308
|
+
*
|
|
309
|
+
* 支持 text / image / tool-call / tool-result;
|
|
310
|
+
* 其余块(file 等)在 DSH 侧已被投影成文本或占位,这里保守丢弃。
|
|
311
|
+
*
|
|
312
|
+
* ⚠ **丢弃必须留痕**(0.10.0 审计 P6):此前 default 分支静默 `break`,注释说
|
|
313
|
+
* 「记一条提示」但实际一个字节都没输出 —— 用户看到的是「发出去的东西模型看不见」,
|
|
314
|
+
* 却无从判断是哪一类块被丢。现在通过 `options.onDrop(type)` 把块类型报给调用方
|
|
315
|
+
* (`plan-client` 打一条 warn;同一请求内相同类型去重,避免刷屏)。
|
|
316
|
+
*
|
|
317
|
+
* ## 图片的两种形态
|
|
318
|
+
*
|
|
319
|
+
* 1. **宿主流派(实际形态)**:`{ type: 'image', attachment: ImageAttachmentRef }`
|
|
320
|
+
* —— 持久引用。字节由调用方先经 `resolveImages()` 从附件服务解析好,
|
|
321
|
+
* 以 `resolvedImages: Map<attachmentId, {data, ref}>` 传入;查不到的引用
|
|
322
|
+
* 以占位文本替换(不再静默丢弃——那会让用户以为发了图,模型却什么都看不到)。
|
|
323
|
+
* 2. **内联形态(防御性兼容)**:`{ type: 'image', source: { type: 'base64'|'url', … } }`
|
|
324
|
+
* —— 旧测试与其他宿主形态,原样转换。
|
|
325
|
+
*
|
|
326
|
+
* @param {readonly any[]} blocks
|
|
327
|
+
* @param {{
|
|
328
|
+
* supportsImages?: boolean,
|
|
329
|
+
* resolvedImages?: Map<string, { data: Uint8Array, ref: any }>,
|
|
330
|
+
* onDrop?: (type: string, block: any) => void,
|
|
331
|
+
* }} [options]
|
|
332
|
+
* @returns {Array<any>}
|
|
333
|
+
*/
|
|
334
|
+
export function convertContent(blocks, options = {}) {
|
|
335
|
+
if (typeof blocks === 'string') return [{ type: 'text', text: blocks }]
|
|
336
|
+
if (!Array.isArray(blocks)) return []
|
|
337
|
+
const onDrop = typeof options.onDrop === 'function' ? options.onDrop : undefined
|
|
338
|
+
const out = []
|
|
339
|
+
for (const block of blocks) {
|
|
340
|
+
if (block === null || typeof block !== 'object') continue
|
|
341
|
+
switch (block.type) {
|
|
342
|
+
case 'text':
|
|
343
|
+
if (typeof block.text === 'string' && block.text.length > 0) out.push({ type: 'text', text: block.text })
|
|
344
|
+
break
|
|
345
|
+
case 'image': {
|
|
346
|
+
if (options.supportsImages === false) {
|
|
347
|
+
out.push({ type: 'text', text: '[image omitted: this model does not accept image input]' })
|
|
348
|
+
break
|
|
349
|
+
}
|
|
350
|
+
/** ① 宿主流派:持久引用 → 已解析字节。 */
|
|
351
|
+
const attachmentId = attachmentIdOf(block)
|
|
352
|
+
if (typeof attachmentId === 'string' && attachmentId.length > 0) {
|
|
353
|
+
const resolved = options.resolvedImages?.get(attachmentId)
|
|
354
|
+
if (resolved !== undefined) {
|
|
355
|
+
out.push(imageBlockFromAttachment(resolved.ref ?? block.attachment, resolved.data))
|
|
356
|
+
} else {
|
|
357
|
+
out.push({
|
|
358
|
+
type: 'text',
|
|
359
|
+
text: `[image omitted: attachment ${attachmentId.slice(0, 15)}… could not be loaded from the attachment store]`,
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
break
|
|
363
|
+
}
|
|
364
|
+
/** ② 防御:内联 base64 / url。 */
|
|
365
|
+
const source = block.source
|
|
366
|
+
if (source?.type === 'base64' && typeof source.data === 'string') {
|
|
367
|
+
out.push({
|
|
368
|
+
type: 'image',
|
|
369
|
+
source: { type: 'base64', media_type: source.media_type ?? 'image/png', data: source.data },
|
|
370
|
+
})
|
|
371
|
+
} else if (source?.type === 'url' && typeof source.url === 'string') {
|
|
372
|
+
out.push({ type: 'image', source: { type: 'url', url: source.url } })
|
|
373
|
+
}
|
|
374
|
+
break
|
|
375
|
+
}
|
|
376
|
+
case 'tool-call':
|
|
377
|
+
out.push({
|
|
378
|
+
type: 'tool_use',
|
|
379
|
+
id: block.id ?? `toolu_${Math.random().toString(36).slice(2)}`,
|
|
380
|
+
name: block.name,
|
|
381
|
+
input: parseToolArguments(block.arguments ?? block.args),
|
|
382
|
+
})
|
|
383
|
+
break
|
|
384
|
+
case 'tool-result':
|
|
385
|
+
out.push({
|
|
386
|
+
type: 'tool_result',
|
|
387
|
+
tool_use_id: block.toolCallId ?? block.id,
|
|
388
|
+
content:
|
|
389
|
+
typeof block.output === 'string'
|
|
390
|
+
? block.output
|
|
391
|
+
: JSON.stringify(block.output ?? block.content ?? ''),
|
|
392
|
+
...(block.isError === true ? { is_error: true } : {}),
|
|
393
|
+
})
|
|
394
|
+
break
|
|
395
|
+
case 'thinking':
|
|
396
|
+
case 'reasoning':
|
|
397
|
+
/** 不向历史回放思考块:上游不接受无签名的 thinking(**刻意**丢弃,不算未知块)。 */
|
|
398
|
+
break
|
|
399
|
+
default:
|
|
400
|
+
/** 未知块:丢弃但留痕(见函数头「丢弃必须留痕」)。 */
|
|
401
|
+
onDrop?.(String(block.type ?? 'unknown'), block)
|
|
402
|
+
break
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return out
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* 收集一次请求里所有消息的图片引用,并经附件服务解析成请求字节。
|
|
410
|
+
*
|
|
411
|
+
* 这是官方 `dsh-llm-deepseek` 的同款链路:`attachments.readImageRequest(ref, target)`
|
|
412
|
+
* 返回确定性的「请求版本」(`{ data, mediaType, width, height, … }`,超出 target 时
|
|
413
|
+
* 由后端缩放/重编码)。解析失败的引用跳过(`convertContent` 会给出占位文本),
|
|
414
|
+
* 不让一张坏图让整个请求失败。
|
|
415
|
+
*
|
|
416
|
+
* @param {readonly any[]} messages DSH GenerateOptions.messages
|
|
417
|
+
* @param {{ resolveAttachments?: () => any, signal?: AbortSignal }} [deps]
|
|
418
|
+
* @returns {Promise<Map<string, { data: Uint8Array, ref: any }>>} attachmentId → 请求字节
|
|
419
|
+
*/
|
|
420
|
+
export async function resolveImages(messages, deps = {}) {
|
|
421
|
+
const attachments = deps.resolveAttachments?.()
|
|
422
|
+
if (attachments === undefined || attachments === null) return new Map()
|
|
423
|
+
const refs = new Map()
|
|
424
|
+
for (const message of Array.isArray(messages) ? messages : []) {
|
|
425
|
+
const blocks = Array.isArray(message?.content) ? message.content : []
|
|
426
|
+
for (const occurrence of imageOccurrences(blocks)) {
|
|
427
|
+
if (occurrence.inline !== undefined) continue
|
|
428
|
+
if (!refs.has(occurrence.attachmentId)) refs.set(occurrence.attachmentId, occurrence.ref)
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const resolved = new Map()
|
|
432
|
+
for (const [attachmentId, ref] of refs) {
|
|
433
|
+
try {
|
|
434
|
+
/** 默认保留归一化尺寸(基类 imageRequestTarget 的语义);不放大也不强制缩。 */
|
|
435
|
+
const target = {
|
|
436
|
+
width: typeof ref.width === 'number' && ref.width > 0 ? ref.width : 1568,
|
|
437
|
+
height: typeof ref.height === 'number' && ref.height > 0 ? ref.height : 1568,
|
|
438
|
+
maxBytes: typeof ref.bytes === 'number' && ref.bytes > 0 ? ref.bytes : 4_194_304,
|
|
439
|
+
}
|
|
440
|
+
const version = await attachments.readImageRequest(ref, target, deps.signal)
|
|
441
|
+
if (version?.data instanceof Uint8Array && version.data.byteLength > 0) {
|
|
442
|
+
resolved.set(attachmentId, { data: version.data, ref: version.attachment ?? ref })
|
|
443
|
+
}
|
|
444
|
+
} catch {
|
|
445
|
+
/** 单张图解析失败只降级为占位文本,不拖垮整条请求。 */
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return resolved
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* 把 DSH 的 `GenerateOptions` 组装成上游请求体(**纯函数**)。
|
|
453
|
+
*
|
|
454
|
+
* @param {{
|
|
455
|
+
* options: any,
|
|
456
|
+
* cwd?: string,
|
|
457
|
+
* provider?: string,
|
|
458
|
+
* now?: Date,
|
|
459
|
+
* maxTokens?: number,
|
|
460
|
+
* supportsImages?: boolean,
|
|
461
|
+
* resolvedImages?: Map<string, { data: Uint8Array, ref: any }>,
|
|
462
|
+
* onDrop?: (type: string) => void,
|
|
463
|
+
* }} input
|
|
464
|
+
* `onDrop` 收到的是**去重后**的未知块类型(同一请求里同样的类型只报一次)。
|
|
465
|
+
* @returns {Record<string, unknown>}
|
|
466
|
+
*/
|
|
467
|
+
export function buildUpstreamBody(input) {
|
|
468
|
+
const options = input.options
|
|
469
|
+
const model = options.model
|
|
470
|
+
const callerBlocks = normalizeSystem(options.system)
|
|
471
|
+
const callerText = callerBlocks.map((block) => block.text).join('\n\n')
|
|
472
|
+
/**
|
|
473
|
+
* 未知块去重(0.10.0 审计 P6):一次请求可能带几十条同一类型的未知块,
|
|
474
|
+
* 逐条 warn 会把日志刷穿,反而看不出「到底是哪一类被丢了」。
|
|
475
|
+
*/
|
|
476
|
+
const dropped = new Set()
|
|
477
|
+
const onDrop = typeof input.onDrop === 'function'
|
|
478
|
+
? (type) => {
|
|
479
|
+
if (dropped.has(type)) return
|
|
480
|
+
dropped.add(type)
|
|
481
|
+
input.onDrop(type)
|
|
482
|
+
}
|
|
483
|
+
: undefined
|
|
484
|
+
|
|
485
|
+
const messages = (Array.isArray(options.messages) ? options.messages : [])
|
|
486
|
+
.filter((message) => message?.role !== 'system')
|
|
487
|
+
.map((message) => ({
|
|
488
|
+
role: message.role === 'assistant' ? 'assistant' : 'user',
|
|
489
|
+
content: convertContent(message.content, {
|
|
490
|
+
supportsImages: input.supportsImages,
|
|
491
|
+
resolvedImages: input.resolvedImages,
|
|
492
|
+
onDrop,
|
|
493
|
+
}),
|
|
494
|
+
}))
|
|
495
|
+
.filter((message) => message.content.length > 0)
|
|
496
|
+
|
|
497
|
+
const body = {
|
|
498
|
+
model,
|
|
499
|
+
max_tokens: input.maxTokens ?? 32_768,
|
|
500
|
+
stream: true,
|
|
501
|
+
system: buildZcodeSystemBlocks(callerText.length > 0 ? callerText : undefined, {
|
|
502
|
+
cwd: input.cwd ?? '.',
|
|
503
|
+
provider: input.provider ?? 'zcode',
|
|
504
|
+
model,
|
|
505
|
+
}),
|
|
506
|
+
messages: withContextPrefix(messages, input.now ?? new Date()),
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const tools = convertTools(options.tools)
|
|
510
|
+
if (tools.length > 0) body.tools = tools
|
|
511
|
+
if (typeof options.temperature === 'number') body.temperature = options.temperature
|
|
512
|
+
return body
|
|
513
|
+
}
|