dsh-plugin-doubao 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/adapter.js ADDED
@@ -0,0 +1,851 @@
1
+ /**
2
+ * The Doubao provider adapter.
3
+ *
4
+ * This is a plain object rather than a `LlmAdapter` subclass on purpose: the
5
+ * harness only ever calls the methods below, and `@deepseek-ai/dsh-llm` is not
6
+ * resolvable from a profile's `node_modules` (only `cosmokit`/`schemastery`
7
+ * live there). Duck typing is what lets the plugin be installed from a GitHub
8
+ * URL without shipping a copy of the harness.
9
+ *
10
+ * Stream chunks follow the harness vocabulary the pi-ai adapter emits:
11
+ * `{type:'text-delta',index,text}`, `{type:'usage',usage:{...}}`, then
12
+ * `{type:'finish',reason:{kind:'stop'}}` — usage before finish, or the token
13
+ * meter never sees the call.
14
+ */
15
+ import { Pacer, streamChat, MODELS, RISK_CODE, resolveModel as resolveWireModel } from './doubao.js'
16
+ import { createRiskMemory } from './risk-memory.js'
17
+ import { FALLBACK_RATES, fetchModelRates } from './rates.js'
18
+
19
+ // Doubao reports a 128K context for the chat surface and 131072 for the routed
20
+ // models. The smaller number is the one that is true for all of them.
21
+ const CONTEXT_WINDOW = 131_072
22
+ const DEFAULT_MAX_TOKENS = 32_768
23
+
24
+ /**
25
+ * Separator between a model's name and its billing rate.
26
+ *
27
+ * A middle dot, not a hyphen or a colon: the names already contain hyphens
28
+ * (`GLM 5.3 Flash`, `Kimi K3.1 Pro 高速`), so anything hyphen-like would be
29
+ * ambiguous about where the name ends.
30
+ */
31
+ export const RATE_SEPARATOR = ' · '
32
+
33
+ /**
34
+ * The rate has to ride the *name*, not the description: the harness model seat
35
+ * renders `name` only, so a rate placed in `description` is invisible exactly
36
+ * where the user picks a model. Nothing resolves a model by name — the wire
37
+ * request is built from `id` — so this stays display-only.
38
+ */
39
+ export function withRate(name, rate) {
40
+ return !rate ? name : `${name}${RATE_SEPARATOR}${rate}`
41
+ }
42
+
43
+ /**
44
+ * Strip the host's runtime injections out of a message before it goes on the
45
+ * wire. DSH appends `<system-reminder>` blocks (the operating-mode preamble,
46
+ * AGENTS.md, the project memory) to the messages it hands an adapter. Those
47
+ * blocks are instructions for *this* agent, not conversation content, and
48
+ * shipping them to a consumer chat endpoint is what earned the account this
49
+ * reply:
50
+ *
51
+ * 该内容涉嫌违反[豆包使用规范](...),若有误判,请长按本条消息后点击"反馈与举报"
52
+ *
53
+ * The refusal arrives as a normal text delta with a `stop` finish, so the user
54
+ * sees a model that "cannot chat". Length is the primary signal: a real system
55
+ * hint (title generation, a short persona) is a few hundred characters, while
56
+ * the host preamble is tens of thousands.
57
+ */
58
+ export function cleanForWire(text) {
59
+ return String(text ?? '')
60
+ .replace(/<system-reminder\b[^>]*>[\s\S]*?<\/system-reminder>/gi, '')
61
+ .replace(/<\/?system-reminder\b[^>]*>/gi, '')
62
+ .trim()
63
+ }
64
+
65
+ function hasHostMarkers(text) {
66
+ return /Instructions from:|AGENTS\.md|无限制模式|operator-authorized|without refusal|soft-refuse|hard-refuse/i.test(text)
67
+ }
68
+
69
+ /**
70
+ * The host preamble can arrive as a `system`/`developer` message *or* be
71
+ * appended to a `user` turn inside `<system-reminder>` tags (the archive shows
72
+ * both shapes). `cleanForWire` removes the tags; this catches any unwrapped
73
+ * remnant. Long system blocks are dropped outright; for a user turn we require
74
+ * the explicit markers as well, so pasting a long document is never discarded.
75
+ */
76
+ function isHostPreamble(text, role) {
77
+ if (role === 'system' || role === 'developer') return text.length > 2000 || hasHostMarkers(text)
78
+ // assistant/tool turns are agent-generated: if one carries the harness's own
79
+ // markers it is meta-discussion about DSH, not something the user said. Keep
80
+ // user turns unless they are BOTH long and marked, so a pasted document or a
81
+ // short question about AGENTS.md is never silently discarded.
82
+ if (role === 'assistant' || role === 'tool') return hasHostMarkers(text)
83
+ return text.length > 2000 && hasHostMarkers(text)
84
+ }
85
+
86
+ // The moderation notice the account actually received (session archive,
87
+ // 2026-11-14): 该内容涉嫌违反[豆包使用规范](https://www.doubao.com/legal/
88
+ // community_guidelines),若有误判,请长按本条消息后点击"反馈与举报"进行反馈。
89
+ // The short lead is deliberate: any wording variant that starts this way is
90
+ // caught on its first chunk, before a single character reaches the UI. Holding
91
+ // only deltas that are still a prefix of it means every normal answer starts
92
+ // streaming on its first delta — there is no perceptible delay.
93
+ const REFUSAL_LEAD = '该内容涉嫌违反'
94
+
95
+ /**
96
+ * Doubao's content filter answers with a canned notice instead of an error.
97
+ * Anchored on the exact lead (or the embedded guidelines URL) so a user asking
98
+ * about the rules in ordinary words is not mistaken for a refusal.
99
+ */
100
+ export function isRefusal(text) {
101
+ const s = String(text ?? '').replace(/^\s+/, '')
102
+ return s.startsWith(REFUSAL_LEAD) || s.includes('doubao.com/legal/community_guidelines')
103
+ }
104
+
105
+ /**
106
+ * Hard cap on the flattened prompt, in characters.
107
+ *
108
+ * Doubao's `/chat/completion` is a stateless single-turn endpoint: the harness
109
+ * hands us EVERY visible message (including compressed-block summaries, tool
110
+ * results and injected time/runtime context) and `toPrompt` joins them into one
111
+ * `text_block.text`. A long-lived session measured 2,236,328 characters across
112
+ * 1,497 blocks; the endpoint answers that with `common invalid param` at the
113
+ * parameter-validation layer, before any model or risk logic runs. A fresh
114
+ * session is ~900 characters and answers normally.
115
+ *
116
+ * The workbuddy connect plugin solves the same class of failure with
117
+ * `applyContextBudgets` / `keepPromptLast`: keep the recent turns, keep the
118
+ * system head, drop the middle. 32K characters is ~16K tokens at the Chinese
119
+ * 2-chars-per-token estimate, comfortably inside every model's window and far
120
+ * below whatever body limit the gateway enforces.
121
+ */
122
+ export const MAX_PROMPT_CHARS = 32_000
123
+
124
+ /**
125
+ * Messages the harness injects into the transcript but which are not a human
126
+ * turn: time sampling, runtime-context snapshots, billion-context compression
127
+ * records and efficiency nudges. DSH already renders them separately; shipping
128
+ * them inside the prompt only wastes the budget and (for the operating-mode
129
+ * preamble) trips content moderation. The patterns are measured from the
130
+ * session archive, matching workbuddy's `INJECTED_BUILT_IN_PREFIXES`.
131
+ */
132
+ const INJECTED_TURN = /^(?:Time sampled while preparing turn \d|Current runtime context\.|<system-reminder>|\[Compressed conversation section\]|This is an automatically generated checkpoint|This is an efficiency nudge|\[model changed:|\[ACP status:|\[ACP )/
133
+
134
+ function isInjectedTurn(text) {
135
+ return INJECTED_TURN.test(text)
136
+ }
137
+
138
+ // ---------------------------------------------------------------- tool calling
139
+ //
140
+ // Doubao's web `/chat/completion` has no native function-calling field: the
141
+ // request body carries a single text block and the SSE stream only ever yields
142
+ // text/reasoning deltas. Function Calling on Doubao lives on the separate
143
+ // (paid) Volcengine Ark API, which this plugin deliberately does not use.
144
+ //
145
+ // So tool support is a prompt-injection bridge: serialize the harness tool
146
+ // schemas into the prompt, ask the model to answer tool calls with a strict
147
+ // `[TOOL_CALL]{...}[/TOOL_CALL]` marker, then translate those markers back into
148
+ // the harness's `tool-call` chunk protocol. The harness agent loop executes the
149
+ // call and feeds `role:'tool'` results into the next turn; `toPrompt` already
150
+ // renders those as `[Tool result]` text, so the loop closes without a wire
151
+ // protocol for tools.
152
+
153
+ export const TOOL_CALL_OPEN = '[TOOL_CALL]'
154
+ export const TOOL_CALL_CLOSE = '[/TOOL_CALL]'
155
+
156
+ /** Active tools only: deferLoading entries are not available this turn. */
157
+ export function activeTools(tools) {
158
+ return (Array.isArray(tools) ? tools : []).filter(
159
+ (tool) => tool && typeof tool.name === 'string' && tool.name.length > 0 && tool.deferLoading !== true,
160
+ )
161
+ }
162
+
163
+ /** The protocol preamble prepended to the prompt when tools are available. */
164
+ export function toolInstructions(tools) {
165
+ const defs = tools.map((tool) => ({
166
+ name: tool.name,
167
+ description: typeof tool.description === 'string' ? tool.description : '',
168
+ parameters: tool.parameters && typeof tool.parameters === 'object' ? tool.parameters : { type: 'object', properties: {} },
169
+ }))
170
+ return [
171
+ '你可以使用下面的工具完成任务(例如执行终端命令、读写文件、查询信息)。You have the tools below; use them whenever the task needs one.',
172
+ 'TOOL DEFINITIONS (JSON):',
173
+ JSON.stringify(defs),
174
+ '',
175
+ `To call a tool, emit exactly one block in this form, with nothing else inside it:`,
176
+ `${TOOL_CALL_OPEN}{"name":"<exact tool name>","arguments":{...JSON matching that tool's parameters...}}${TOOL_CALL_CLOSE}`,
177
+ '',
178
+ 'Rules:',
179
+ '- "name" must be copied exactly from the definitions.',
180
+ '- "arguments" must be a JSON object valid against that tool\'s "parameters" schema.',
181
+ '- Do not wrap the marker in markdown code fences and do not add commentary inside it.',
182
+ '- Emit one marker per call; several calls may appear one after another.',
183
+ '- If no tool is needed, answer normally with no marker at all.',
184
+ ].join('\n')
185
+ }
186
+
187
+ /**
188
+ * With dozens of tools the JSON definitions are tens of thousands of chars long.
189
+ * Doubao reads the *end* of the prompt most attentively, so a marker protocol
190
+ * buried before that wall of JSON gets ignored — the model answers "I can't
191
+ * access your files" in prose instead of calling anything. Re-state the one
192
+ * rule that matters immediately after the user's question. Measured against the
193
+ * live 59-tool prompt: without this tail the model emits no marker; with it the
194
+ * model emits a well-formed call (choosing an appropriate tool on its own).
195
+ */
196
+ export const TOOL_CALL_REMINDER = [
197
+ '',
198
+ '[System: act, do not explain] You have real executable tools listed above.',
199
+ `If this task needs an action, emit exactly one ${TOOL_CALL_OPEN}{"name":"<exact name>","arguments":{...}}${TOOL_CALL_CLOSE} block now.`,
200
+ 'Do NOT claim you lack access or ask the user to run it themselves. Use plain prose only when the task truly needs no tool.',
201
+ ].join('\n')
202
+
203
+ /**
204
+ * Longest suffix of `text` that is also a prefix of `marker`. While streaming,
205
+ * this is the piece of text we must hold back: it could still grow into the
206
+ * tool-call marker with the next delta. Everything before it is safe to emit.
207
+ */
208
+ export function heldMarkerPrefix(text, marker) {
209
+ const max = Math.min(text.length, marker.length - 1)
210
+ for (let n = max; n > 0; n--) {
211
+ if (text.slice(text.length - n) === marker.slice(0, n)) return n
212
+ }
213
+ return 0
214
+ }
215
+
216
+ function parseToolCallPayload(jsonText) {
217
+ let payload
218
+ try {
219
+ payload = JSON.parse(jsonText)
220
+ } catch {
221
+ return undefined
222
+ }
223
+ if (!payload || typeof payload !== 'object') return undefined
224
+ const name = typeof payload.name === 'string' ? payload.name.trim() : ''
225
+ if (!name) return undefined
226
+ let args = payload.arguments
227
+ if (args === undefined || args === null) args = {}
228
+ let argumentsString
229
+ if (typeof args === 'string') argumentsString = args
230
+ else {
231
+ try {
232
+ argumentsString = JSON.stringify(args)
233
+ } catch {
234
+ argumentsString = '{}'
235
+ }
236
+ }
237
+ return { name, arguments: argumentsString }
238
+ }
239
+
240
+ /**
241
+ * Incremental translator from Doubao's plain text stream to harness tool-call
242
+ * chunks. Text outside `[TOOL_CALL]…[/TOOL_CALL]` is emitted unchanged; a closed
243
+ * marker becomes a complete tool-call block (start + delta + end). When no tools
244
+ * are active it is a transparent passthrough, so ordinary turns are unaffected.
245
+ *
246
+ * `allocIndex(key)` reserves a distinct, stable block index per emitted block.
247
+ */
248
+ export class ToolCallStream {
249
+ constructor({ tools = [], allocIndex = () => 0 } = {}) {
250
+ this.active = activeTools(tools).length > 0
251
+ this.allocIndex = allocIndex
252
+ this.buffer = ''
253
+ this.callCount = 0
254
+ }
255
+
256
+ get hasCalls() {
257
+ return this.callCount > 0
258
+ }
259
+
260
+ *feed(text) {
261
+ if (!this.active) {
262
+ if (text) yield { type: 'text', text }
263
+ return
264
+ }
265
+ if (text) this.buffer += text
266
+ yield* this.#drain(false)
267
+ }
268
+
269
+ *end() {
270
+ if (!this.active) return
271
+ yield* this.#drain(true)
272
+ }
273
+
274
+ *#drain(final) {
275
+ for (;;) {
276
+ if (!this.buffer) return
277
+ const open = this.buffer.indexOf(TOOL_CALL_OPEN)
278
+ if (open < 0) {
279
+ if (final) {
280
+ const rest = this.buffer
281
+ this.buffer = ''
282
+ if (rest) yield { type: 'text', text: rest }
283
+ return
284
+ }
285
+ // Hold back a partial marker prefix — the next delta may complete it.
286
+ const held = heldMarkerPrefix(this.buffer, TOOL_CALL_OPEN)
287
+ const cut = this.buffer.length - held
288
+ if (cut > 0) {
289
+ const text = this.buffer.slice(0, cut)
290
+ this.buffer = this.buffer.slice(cut)
291
+ yield { type: 'text', text }
292
+ }
293
+ return
294
+ }
295
+ // Everything before a complete marker is plain text.
296
+ if (open > 0) {
297
+ const text = this.buffer.slice(0, open)
298
+ this.buffer = this.buffer.slice(open)
299
+ yield { type: 'text', text }
300
+ }
301
+ const bodyStart = TOOL_CALL_OPEN.length
302
+ const close = this.buffer.indexOf(TOOL_CALL_CLOSE, bodyStart)
303
+ if (close < 0) {
304
+ if (final) {
305
+ // An unclosed marker is not a tool call: show the model's words rather
306
+ // than silently dropping them.
307
+ const rest = this.buffer
308
+ this.buffer = ''
309
+ yield { type: 'text', text: rest }
310
+ }
311
+ return
312
+ }
313
+ const jsonText = this.buffer.slice(bodyStart, close)
314
+ this.buffer = this.buffer.slice(close + TOOL_CALL_CLOSE.length)
315
+ const call = parseToolCallPayload(jsonText)
316
+ if (call) {
317
+ const index = this.allocIndex(`tool-${this.callCount}`)
318
+ const id = `call-${index}`
319
+ this.callCount++
320
+ yield { type: 'block-start', index, blockType: 'tool-call' }
321
+ yield { type: 'tool-call-delta', index, id, name: call.name, argumentsDelta: call.arguments }
322
+ yield { type: 'block-end', index, block: { type: 'tool-call', id, name: call.name, arguments: call.arguments } }
323
+ } else {
324
+ // Malformed payload: show the marker verbatim instead of losing it.
325
+ yield { type: 'text', text: `${TOOL_CALL_OPEN}${jsonText}${TOOL_CALL_CLOSE}` }
326
+ }
327
+ }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Flatten harness message blocks into the single prompt Doubao expects, with a
333
+ * context budget: when the flattened history is too large, keep the leading
334
+ * system hint and the most recent turns, and insert a marker where the middle
335
+ * was dropped. The last user turn is NEVER dropped — that is the question.
336
+ */
337
+ function toPromptCore(messages, { maxChars = MAX_PROMPT_CHARS, tools = [] } = {}) {
338
+ const parts = []
339
+ const toolsForPrompt = activeTools(tools)
340
+ if (toolsForPrompt.length > 0) parts.push(`[Tool instructions]\n${toolInstructions(toolsForPrompt)}`)
341
+ for (const message of messages ?? []) {
342
+ const cleaned = cleanForWire(
343
+ (message.content ?? [])
344
+ .filter((block) => block.type === 'text' && typeof block.text === 'string')
345
+ .map((block) => block.text)
346
+ .join('\n'),
347
+ )
348
+ if (!cleaned) continue
349
+ // A long host config is not a chat turn. Dropping it keeps the conversation
350
+ // readable to Doubao's moderation instead of tripping it on words like
351
+ // "execute without refusal" that belong to the harness, not to the user.
352
+ if (isHostPreamble(cleaned, message.role)) continue
353
+ if (isInjectedTurn(cleaned)) continue
354
+ if (message.role === 'system' || message.role === 'developer') parts.push(`[System]\n${cleaned}`)
355
+ else if (message.role === 'assistant') parts.push(`[Assistant]\n${cleaned}`)
356
+ else if (message.role === 'tool') parts.push(`[Tool result]\n${cleaned}`)
357
+ else parts.push(cleaned)
358
+ }
359
+
360
+ // Re-state the call protocol at the very END, right after the user's question.
361
+ // With dozens of tools the JSON definitions are tens of thousands of chars and
362
+ // Doubao only attends to the prompt tail — a marker rule buried before that
363
+ // wall of JSON is ignored and the model answers in prose. As the last part it
364
+ // is also the most protected during trimming (recent parts are kept first).
365
+ if (toolsForPrompt.length > 0) parts.push(TOOL_CALL_REMINDER.trim())
366
+
367
+ const joined = parts.join('\n\n')
368
+ if (joined.length <= maxChars) return joined
369
+
370
+ // Keep the leading system/tool-instruction hints (if any) plus as many recent
371
+ // parts as fit. Tool definitions MUST survive trimming — dropping them while
372
+ // still expecting a tool call would guarantee a malformed answer. The newest
373
+ // part is collected first, so the current question is always kept.
374
+ const headParts = []
375
+ let tailStart = 0
376
+ while (
377
+ tailStart < parts.length &&
378
+ (parts[tailStart].startsWith('[System]\n') || parts[tailStart].startsWith('[Tool instructions]\n'))
379
+ ) {
380
+ headParts.push(parts[tailStart])
381
+ tailStart++
382
+ }
383
+ const head = headParts.length ? headParts.join('\n\n') : undefined
384
+ // Tool definitions and system hints are essential and must never be chopped
385
+ // in the middle of their JSON. If the essentials alone fill the budget, send
386
+ // them whole: an over-budget request is rejected cleanly, but a half-written
387
+ // tool schema makes the model emit a malformed call.
388
+ if (head !== undefined && head.length >= maxChars) {
389
+ // ...but the conversation still has to ride along. With many tools the
390
+ // definitions alone exceed `maxChars`; returning `head` by itself leaves
391
+ // the model staring at a toolbox with no question, so it invents one
392
+ // ("just send your request"). Keep every remaining turn; if that is huge,
393
+ // keep its tail so the newest user message survives. The call reminder is
394
+ // separated and re-appended last: it must stay at the very end regardless
395
+ // of how the conversation tail is trimmed.
396
+ const tailParts = parts.slice(tailStart)
397
+ const reminder = toolsForPrompt.length > 0 && tailParts.at(-1) === TOOL_CALL_REMINDER.trim()
398
+ ? tailParts.pop()
399
+ : undefined
400
+ const rest = tailParts.join('\n\n')
401
+ // Reserve space for the reminder, but the *last user message* wins over it:
402
+ // a cut must end on the question, with the (short) reminder appended after.
403
+ const room = reminder && reminder.length < maxChars ? maxChars - reminder.length - 2 : maxChars
404
+ const keptRest = rest.length > room ? `…${rest.slice(rest.length - room)}` : rest
405
+ const tail = [keptRest, reminder && reminder.length < maxChars ? reminder : undefined]
406
+ .filter(Boolean)
407
+ .join('\n\n')
408
+ return tail ? `${head}\n\n${tail}` : head
409
+ }
410
+ const marker = '[… earlier turns omitted to fit the Doubao request limit …]'
411
+ const fixed = (head === undefined ? 0 : head.length + 2) + marker.length + 2
412
+
413
+ // The call reminder is protocol glue, not conversation. Keep it OUT of the
414
+ // recent-first collection below and reserve its space up front, so it can
415
+ // never displace the user's question — and is never itself squeezed out when
416
+ // the tail budget is tight. Without this tail reminder, Doubao ignores a rule
417
+ // buried behind tens of thousands of chars of tool JSON.
418
+ let reminder
419
+ let last = parts.length - 1
420
+ if (toolsForPrompt.length > 0 && parts[last] === TOOL_CALL_REMINDER.trim()) {
421
+ reminder = parts[last]
422
+ last--
423
+ }
424
+ const reminderRoom = reminder && reminder.length + 2 <= maxChars ? reminder.length + 2 : 0
425
+ const budget = Math.max(1, maxChars - fixed - 2 - reminderRoom)
426
+
427
+ const kept = []
428
+ let used = 0
429
+ for (let i = last; i >= tailStart; i--) {
430
+ let part = parts[i]
431
+ const size = part.length + 2
432
+ if (kept.length && used + size > budget) break
433
+ if (!kept.length && size > budget) {
434
+ // A single turn (the question, or a pasted document) is bigger than the
435
+ // whole budget. Trim its HEAD and keep its TAIL — the instruction the user
436
+ // actually cares about lives at the end. A global slice would cut that off.
437
+ part = '…' + part.slice(part.length - (budget - 3))
438
+ }
439
+ kept.unshift(part)
440
+ used += part.length + 2
441
+ }
442
+ const core = [head, marker, ...kept].filter((p) => p !== undefined).join('\n\n')
443
+ if (reminderRoom) return `${core}\n\n${reminder}`
444
+ return core.slice(0, maxChars)
445
+ }
446
+
447
+ /**
448
+ * Build the prompt sent on the wire. Re-state the call protocol at the very end
449
+ * when tools are active: the definitions are far longer than Doubao's attention
450
+ * to the middle of the prompt, and a marker rule that is not last gets ignored.
451
+ */
452
+ export function toPrompt(messages, options = {}) {
453
+ return toPromptCore(messages, options)
454
+ }
455
+
456
+ /**
457
+ * Normalize a thrown Doubao failure into the harness finish reason.
458
+ *
459
+ * A risk-control refusal is reported as `stop` with a note appended, not as an
460
+ * exception: the transport worked and the model answered, it just answered with
461
+ * a refusal. Turning it into a hard error would make the harness retry, and
462
+ * retrying a rate limit is what deepens it.
463
+ */
464
+ export function finishReasonFor(error) {
465
+ if (!error) return { kind: 'stop' }
466
+ if (error.code === 'ABORTED' || error.name === 'AbortError') {
467
+ return { kind: 'aborted', failure: { message: String(error.message ?? 'aborted'), code: 'ABORTED' } }
468
+ }
469
+ const isAuth = error.code === 710012001 || error.code === 710012002
470
+ const code =
471
+ error.code === 710022004
472
+ ? 'RATE_LIMIT'
473
+ : isAuth
474
+ ? 'AUTH'
475
+ : // A bridge fault is local (the client page went away or refused), so it
476
+ // is safe to retry: no risk-control state is involved and a fresh
477
+ // connection usually clears it.
478
+ error.code === 'BRIDGE'
479
+ ? 'TRANSPORT'
480
+ : 'DOUBAO_ERROR'
481
+ // The service answers an expired cookie with a bare numeric code. Carry the
482
+ // raw detail but add the one action that actually fixes it, otherwise the
483
+ // user sees "710012001" and has no idea the login needs refreshing.
484
+ const raw = String(error.message ?? error)
485
+ const message = isAuth
486
+ ? `${raw}(Doubao 登录态已失效,请在插件目录运行 \`node bin/login.js\` 重新扫码登录,见 README。)`
487
+ : raw
488
+ return { kind: 'error', failure: { message, code } }
489
+ }
490
+
491
+ /**
492
+ * Provider id. Not "doubao-local": the plugin no longer runs a local proxy, so
493
+ * calling it local would be a lie, and the name is also what a stale
494
+ * `doubao-local` block in a profile would collide with (DUPLICATE_ADAPTER).
495
+ */
496
+ export const DEFAULT_PROVIDER = 'doubao'
497
+
498
+ /**
499
+ * Retry policy owned by this provider.
500
+ *
501
+ * `RATE_LIMIT` is deliberately absent from `retryableCodes`. The harness default
502
+ * retries it (DEFAULT_RETRYABLE_CODES in @deepseek-ai/dsh-llm), which is exactly
503
+ * backwards here: 710022004 is a risk-control escalation, so the second attempt
504
+ * arrives while the account is already flagged and the streak is what turns a
505
+ * short throttle into a long one. The pacer already serializes turns, so the
506
+ * only failures worth a second attempt are transport hiccups.
507
+ */
508
+ export const RETRY_POLICY = Object.freeze({
509
+ mode: 'normal',
510
+ maxRetries: 2,
511
+ retryableCodes: Object.freeze(['TRANSPORT', 'TIMEOUT', 'SERVER']),
512
+ backoff: Object.freeze({ initialDelayMs: 2_000, maxDelayMs: 15_000, jitterRatio: 0.2 }),
513
+ })
514
+
515
+ export function createAdapter({
516
+ loadCredentials,
517
+ logger,
518
+ pacer,
519
+ providerId = DEFAULT_PROVIDER,
520
+ fetchRates = fetchModelRates,
521
+ bridgePort,
522
+ // Per-adapter memory by default so direct createAdapter callers (tests, and
523
+ // anyone embedding the adapter) do not share risk state through the module's
524
+ // live `streamChat`. The plugin passes a persistent one from index.js.
525
+ riskMemory = createRiskMemory(),
526
+ // true (default): direct first, fall back to the signed bridge on 710022004.
527
+ // false: direct only, never launch or probe the Doubao client — for people who
528
+ // would rather switch models than have the client started for them.
529
+ allowBridge = true,
530
+ } = {}) {
531
+ // Matches DEFAULTS.minIntervalMs in index.js: the plugin always passes its own
532
+ // pacer, so this fallback only matters for direct callers of createAdapter.
533
+ const gate = pacer ?? new Pacer({ minIntervalMs: 1500 })
534
+
535
+ // The rate table starts as the last reading taken from the client and is
536
+ // replaced once a live fetch answers. Names are built from it on every call,
537
+ // so a refreshed table shows up in the picker without a restart.
538
+ let rates = { ...FALLBACK_RATES }
539
+ let rateFetchedAt = 0
540
+ let inFlight
541
+
542
+ async function refreshRates() {
543
+ let credentials
544
+ try {
545
+ credentials = await loadCredentials()
546
+ } catch {
547
+ return rates
548
+ }
549
+ // No credentials yet: leave the clock alone so a login is picked up on the
550
+ // next picker open. Reading the file is cheap; no network call is made.
551
+ if (!credentials?.cookies || !credentials?.identity?.deviceId) return rates
552
+ // Mark the attempt before starting. A failed lookup must not be retried on
553
+ // every picker open or every turn — that would be the one behaviour this
554
+ // plugin cannot afford, and the last known table is still useful.
555
+ rateFetchedAt = Date.now()
556
+ const fresh = await fetchRates({ identity: credentials.identity, cookies: credentials.cookies, logger })
557
+ if (fresh && Object.keys(fresh).length > 0) rates = fresh
558
+ return rates
559
+ }
560
+
561
+ /** Refresh at most once every ten minutes, deduplicating concurrent calls. */
562
+ function ensureRates() {
563
+ const stale = Date.now() - rateFetchedAt > 10 * 60_000
564
+ if (inFlight) return inFlight
565
+ if (!stale) return Promise.resolve(rates)
566
+ inFlight = refreshRates()
567
+ .catch(() => rates)
568
+ .finally(() => {
569
+ inFlight = undefined
570
+ })
571
+ return inFlight
572
+ }
573
+
574
+ function nameFor(model) {
575
+ return withRate(model.name ?? model.id, rates[model.key])
576
+ }
577
+
578
+ function catalogFor() {
579
+ return MODELS.map((model) => ({
580
+ provider: providerId,
581
+ id: model.id,
582
+ name: nameFor(model),
583
+ description: model.description,
584
+ inputModalities: ['text'],
585
+ }))
586
+ }
587
+
588
+ function modelEntry(id) {
589
+ const model = MODELS.find((entry) => entry.id === id)
590
+ if (!model) {
591
+ return { provider: providerId, id, name: id, inputModalities: ['text'] }
592
+ }
593
+ return {
594
+ provider: providerId,
595
+ id: model.id,
596
+ name: nameFor(model),
597
+ description: model.description,
598
+ inputModalities: ['text'],
599
+ }
600
+ }
601
+
602
+ /**
603
+ * Reasoning controls only change something on the chat surface (`modeId==='1'`):
604
+ * there `need_deep_think` is toggled by the effort, so Off genuinely skips the
605
+ * thinking pass and High asks for one. The routed models (`modeId==='3'`)
606
+ * carry their thinking depth as part of the model identity — `deepThink` is a
607
+ * fixed id, and buildBody ignores the user's thinking flag for them. Offering
608
+ * a High choice there would be a switch that does nothing, so those models
609
+ * expose only the default Off effort. Keeping the field (rather than dropping
610
+ * `reasoning` entirely) means a saved call that still passes `off` validates
611
+ * instead of failing with UNSUPPORTED_REASONING_EFFORT.
612
+ */
613
+ function reasoningFor(model) {
614
+ const off = { id: 'off', name: 'Off', description: 'Answer without extended thinking.' }
615
+ if (model?.modeId !== '1') {
616
+ return { efforts: [off], defaultEffort: 'off' }
617
+ }
618
+ return {
619
+ efforts: [off, { id: 'high', name: 'High', description: 'Think before answering.' }],
620
+ defaultEffort: 'off',
621
+ }
622
+ }
623
+
624
+ function resolvedInfo(id) {
625
+ return {
626
+ ...modelEntry(id),
627
+ context: { contextWindow: CONTEXT_WINDOW },
628
+ defaultMaxTokens: DEFAULT_MAX_TOKENS,
629
+ reasoning: reasoningFor(MODELS.find((entry) => entry.id === id)),
630
+ }
631
+ }
632
+
633
+ return {
634
+ pacer: gate,
635
+
636
+ providerInfo(provider) {
637
+ return { id: provider, name: 'Doubao' }
638
+ },
639
+
640
+ providerRetryPolicy() {
641
+ return RETRY_POLICY
642
+ },
643
+
644
+ async listModels() {
645
+ await ensureRates()
646
+ return catalogFor()
647
+ },
648
+
649
+ async resolveModel(provider, model) {
650
+ await ensureRates()
651
+ return resolvedInfo(model)
652
+ },
653
+
654
+ async prepareCall(provider, model) {
655
+ // Deliberately not awaited: a stale rate table costs one `brief_list`
656
+ // round trip, and this method sits between "user pressed enter" and the
657
+ // request going out. The picker still gets an accurate table from
658
+ // listModels/resolveModel, which do await; here the rate only decorates
659
+ // the name on the turn record, so a background refresh is enough.
660
+ ensureRates().catch(() => rates)
661
+ return { model: resolvedInfo(model), stream: (options) => this.stream({ ...options, provider, model }) }
662
+ },
663
+
664
+ /** Test seam: the table the picker is currently showing. */
665
+ currentRates() {
666
+ return { ...rates }
667
+ },
668
+
669
+ async *stream(options) {
670
+ const modelId = options.model
671
+ const wire = resolveWireModel(modelId)?.id ?? modelId
672
+ const tools = Array.isArray(options.tools) ? options.tools : []
673
+ const prompt = toPrompt(options.messages, { tools })
674
+ const started = Date.now()
675
+
676
+ let credentials
677
+ try {
678
+ credentials = await loadCredentials()
679
+ } catch (error) {
680
+ yield { type: 'finish', reason: finishReasonFor(error) }
681
+ return
682
+ }
683
+ if (!credentials?.cookies || !credentials?.identity?.deviceId) {
684
+ yield {
685
+ type: 'finish',
686
+ reason: {
687
+ kind: 'error',
688
+ failure: {
689
+ message: 'Doubao is not signed in. Run `node bin/login.js` from the plugin directory and scan the QR with the Douyin app (see README).',
690
+ code: 'AUTH_MISSING',
691
+ },
692
+ },
693
+ }
694
+ return
695
+ }
696
+
697
+ const think = options.reasoningEffort === 'high' || options.reasoningEffort === 'max'
698
+ let chars = 0
699
+ // One block per kind of text, numbered in the order they first appear. The
700
+ // reasoning block is opened as soon as Doubao starts thinking, which is
701
+ // seconds before the answer exists — that early chunk is what makes the
702
+ // turn look alive instead of stalled.
703
+ const blockIndex = new Map()
704
+ function indexFor(kind) {
705
+ if (!blockIndex.has(kind)) blockIndex.set(kind, blockIndex.size)
706
+ return blockIndex.get(kind)
707
+ }
708
+ const opened = new Set()
709
+ function* begin(kind) {
710
+ if (opened.has(kind)) return
711
+ opened.add(kind)
712
+ yield { type: 'block-start', index: indexFor(kind), blockType: kind === 'reasoning' ? 'reasoning' : 'text' }
713
+ }
714
+ // Tool calls must own their block indexes. text/reasoning blocks claiming
715
+ // indexes lazily while a call is emitted would otherwise reuse the same
716
+ // number and collapse two content blocks into one in the assembler.
717
+ let toolIndexCursor = 1000
718
+ const toolStream = new ToolCallStream({
719
+ tools,
720
+ allocIndex: () => toolIndexCursor++,
721
+ })
722
+ function* emitText(text) {
723
+ if (!text) return
724
+ // The live reminder restates the protocol at the very end of the prompt;
725
+ // the model sometimes echoes it back in prose. Strip that echo so a text
726
+ // answer never carries the marker syntax as visible text.
727
+ text = text.replace(/\[System:\s*act,\s*do not explain\][^\n]*\n?/gi, '')
728
+ .replace(/^If this task needs an action, emit exactly one .*$/gim, '')
729
+ .replace(/^Do NOT claim you lack access.*$/gim, '')
730
+ if (!text) return
731
+ chars += text.length
732
+ yield* begin('answer')
733
+ yield { type: 'text-delta', index: indexFor('answer'), text }
734
+ }
735
+ function* consumeTranslator(events) {
736
+ for (const piece of events) {
737
+ if (piece.type === 'text') yield* emitText(piece.text)
738
+ else yield piece
739
+ }
740
+ }
741
+ // Doubao's moderation refusal is a normal text stream, not an error frame.
742
+ // Hold deltas only while they are still a strict prefix of the canned
743
+ // notice's lead; any real answer diverges on its first delta and flushes
744
+ // immediately, so there is no perceptible buffering.
745
+ let pending = ''
746
+ let refused = false
747
+ let holding = true
748
+ try {
749
+ for await (const event of gate.runIterable(
750
+ () =>
751
+ streamChat({
752
+ identity: credentials.identity,
753
+ cookies: credentials.cookies,
754
+ text: prompt,
755
+ model: wire,
756
+ think,
757
+ signal: options.signal,
758
+ bridgePort,
759
+ riskMemory,
760
+ bridge: allowBridge,
761
+ }),
762
+ // The lock is per turn, the cooldown is per model: a penalty taken here
763
+ // must not be paid by whichever model the user picks next.
764
+ { key: modelId, signal: options.signal },
765
+ )) {
766
+ if (event.type === 'reasoning' && event.text) {
767
+ // Reasoning is never the canned notice: pass it straight through.
768
+ yield* begin('reasoning')
769
+ chars += event.text.length
770
+ yield { type: 'reasoning-delta', index: indexFor('reasoning'), text: event.text }
771
+ } else if (event.type === 'delta' && event.text) {
772
+ if (holding) {
773
+ pending += event.text
774
+ if (isRefusal(pending)) {
775
+ // Caught before a single character reached the UI.
776
+ refused = true
777
+ pending = ''
778
+ holding = false
779
+ break
780
+ }
781
+ if (pending.length >= REFUSAL_LEAD.length || !REFUSAL_LEAD.startsWith(pending)) {
782
+ // Not a refusal: flush everything held and switch to passthrough.
783
+ holding = false
784
+ const first = pending
785
+ pending = ''
786
+ yield* consumeTranslator(toolStream.feed(first))
787
+ }
788
+ } else {
789
+ yield* consumeTranslator(toolStream.feed(event.text))
790
+ }
791
+ } else if (event.type === 'error') {
792
+ // pending can only be a truncated refusal prefix here; dropping it is
793
+ // more honest than showing a stray fragment alongside the error.
794
+ pending = ''
795
+ const failure = new Error(event.message ?? 'doubao error')
796
+ // `common invalid param` is the gateway's parameter-validation reply
797
+ // to an oversized single text_block; it carries no error_code. Make
798
+ // any recurrence self-diagnosing instead of an opaque message.
799
+ if (/common invalid param|invalid param/i.test(failure.message)) {
800
+ failure.message = `Doubao rejected the request: ${failure.message} (wire prompt was ${prompt.length} chars, budget ${MAX_PROMPT_CHARS})`
801
+ }
802
+ failure.code = event.code
803
+ // The refusal arrives in the stream body, not as a thrown error, so the
804
+ // cooldown has to be extended here too — otherwise this model walks
805
+ // straight back into the same limit. It stays scoped to this model.
806
+ if (event.code === RISK_CODE) gate.penalize(60_000, modelId)
807
+ yield { type: 'finish', reason: finishReasonFor(failure) }
808
+ return
809
+ }
810
+ }
811
+ // A stream that ended still holding a prefix was truncated mid-refusal;
812
+ // there is nothing worth rendering.
813
+ pending = ''
814
+ // Flush any held marker prefix or complete marker the model left in the
815
+ // final delta, so a tool call split across the last chunk is not lost.
816
+ yield* consumeTranslator(toolStream.end())
817
+ } catch (error) {
818
+ if (error.code === RISK_CODE) gate.penalize(60_000, modelId)
819
+ logger?.warn?.(`doubao: ${modelId} failed: ${error.message}`)
820
+ yield { type: 'finish', reason: finishReasonFor(error) }
821
+ return
822
+ }
823
+
824
+ if (refused) {
825
+ // A visible explanation, not a `stop` that would make the harness show a
826
+ // blank turn. No retryable error: retrying a moderation flag just deepens
827
+ // it. Host runtime injections are stripped by toPrompt, but if the user's
828
+ // own text still trips the filter, telling them why is the useful answer.
829
+ const explanation = 'Doubao 内容审核拦截了本次请求(不是模型故障)。已自动剥离宿主注入的系统提示;若仍出现,请换一种表述或减少附件内容后重试。'
830
+ yield* begin('answer')
831
+ yield { type: 'text-delta', index: indexFor('answer'), text: explanation }
832
+ chars += explanation.length
833
+ gate.penalize(30_000, modelId)
834
+ }
835
+ // Roughly a token per two characters for Chinese and per four for English;
836
+ // Doubao does not return usage on the web endpoint, so this is an estimate
837
+ // and the honest thing is to say so in the field names rather than lie.
838
+ const outputTokens = Math.max(1, Math.ceil(chars / 2))
839
+ const inputTokens = Math.max(1, Math.ceil(prompt.length / 2))
840
+ yield {
841
+ type: 'usage',
842
+ usage: { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens },
843
+ }
844
+ // A turn that produced at least one tool call must report `tool-calls`,
845
+ // otherwise the agent loop treats the parsed blocks as plain prose and
846
+ // never executes them. No tools configured → unchanged `stop` behaviour.
847
+ const finishReason = toolStream.hasCalls ? { kind: 'tool-calls' } : { kind: 'stop' }
848
+ yield { type: 'finish', reason: finishReason, durationMs: Date.now() - started }
849
+ },
850
+ }
851
+ }