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/doubao.js ADDED
@@ -0,0 +1,746 @@
1
+ /**
2
+ * The Doubao chat protocol, in plain Node.
3
+ *
4
+ * Verified against the live service: a POST to /chat/completion carrying the
5
+ * account's own device_id plus a valid session cookie streams SSE back. The
6
+ * two things that matter are the identity pair (device_id and fp, where fp is
7
+ * literally "verify_" + device_id) and the session cookie — everything else in
8
+ * the payload is cosmetic as far as the risk engine is concerned.
9
+ *
10
+ * One caveat, learned the hard way: "cosmetic" holds only while the account is
11
+ * in good standing. Once the semantic risk model takes an interest, an unsigned
12
+ * request is refused with 710022004 regardless of how the query or body is
13
+ * shaped; the only thing that satisfies it is the `a_bogus`/`msToken` pair the
14
+ * desktop client mints internally. See ./bridge.js for how we borrow it.
15
+ */
16
+
17
+ import {
18
+ ensureBridge,
19
+ bridgeAvailable,
20
+ bridgeStream,
21
+ DEFAULT_PORT as BRIDGE_PORT,
22
+ } from './bridge.js'
23
+ import { createRiskMemory } from './risk-memory.js'
24
+
25
+ export const CHAT_AID = '582478'
26
+ export const BOT_ID = '7338286299411103781'
27
+ // The verified-good values are the ones the Windows client sends. They are
28
+ // cosmetic while the account is in good standing, and under the bridge the page
29
+ // supplies its own UA and signature. But a client on macOS/Linux should not be
30
+ // forced to claim Windows if the risk engine ever looks at these, so they can be
31
+ // overridden without a code change. Defaults stay byte-for-byte as verified.
32
+ export const DEFAULT_UA =
33
+ process.env.DOUBAO_UA ??
34
+ ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' +
35
+ 'Chrome/147.0.7727.149 Safari/537.36 SamanthaDoubao/2.35.12')
36
+
37
+ const ALNUM = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
38
+
39
+ export function randomId(len = 10) {
40
+ let out = ''
41
+ for (let i = 0; i < len; i += 1) out += ALNUM[Math.floor(Math.random() * ALNUM.length)]
42
+ return out
43
+ }
44
+
45
+ export function randomDigits(len, lead) {
46
+ let out = String(lead)
47
+ for (let i = 1; i < len; i += 1) out += String(Math.floor(Math.random() * 10))
48
+ return out
49
+ }
50
+
51
+ /** A brand new device identity. Only useful if the account will accept it. */
52
+ export function mintIdentity() {
53
+ const deviceId = randomDigits(16, '7')
54
+ return { deviceId, fp: `verify_${deviceId}`, webId: randomDigits(19, '7') }
55
+ }
56
+
57
+ /**
58
+ * Every chat model the Doubao client can route to.
59
+ *
60
+ * Picking a model is not a single field. The client sends the same five-tuple
61
+ * in three places — `conversation_init_ext`, `model_config` and
62
+ * `aggregate_params` — and mirrors two of them onto `option` and `ext`. The
63
+ * server reads whichever copy it wants, so leaving any of them at the default
64
+ * silently downgrades the request to the plain chat model: the answer comes
65
+ * back, just from the wrong model. That is why the tuple lives here instead of
66
+ * being hard-coded in one place.
67
+ *
68
+ * modeId 1 = chat surface, 3 = office/task surface
69
+ * agentMode 2 = chat, 1 = task
70
+ * convMode conversation_mode, 1 for chat and 3 for the task surface
71
+ * effort reasoning_effort; 3 normal, 4 raised
72
+ * deepThink need_deep_think / use_deep_think, the client's thinking-mode id
73
+ *
74
+ * Verified against the live account: all twelve answer with real content.
75
+ */
76
+ export const MODELS = [
77
+ { id: 'doubao', key: '0', name: 'Doubao', modeId: '1', agentMode: 2, convMode: 1, effort: 3, deepThink: 0,
78
+ description: 'Doubao 2.1, the everyday chat model.' },
79
+ { id: 'doubao-expert', key: '3', name: 'Doubao Expert', modeId: '1', agentMode: 2, convMode: 1, effort: 4, deepThink: 1,
80
+ description: 'Doubao with expert reasoning turned on.' },
81
+ { id: 'doubao-auto', key: '9', name: 'Doubao Auto', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 9,
82
+ description: 'Doubao decides per request how much to think.' },
83
+ { id: 'doubao-2.1-lite', key: 'seed-lite-7b', name: 'Doubao 2.1 Lite', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
84
+ description: 'Small, fast Doubao 2.1.' },
85
+ { id: 'doubao-2.1-pro', key: '5', name: 'Doubao 2.1 Pro', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 5,
86
+ description: 'Full-size Doubao 2.1.' },
87
+ { id: 'deepseek-v4.1-flash', key: 'person-deepseek-v4-1-flash-261017', name: 'DeepSeek V4.1 Flash', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
88
+ description: 'DeepSeek V4.1 Flash, served through Doubao.' },
89
+ { id: 'deepseek-v4-pro', key: 'person-deepseek-v4-pro-ga-260813', name: 'DeepSeek V4 Pro', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
90
+ description: 'DeepSeek V4 Pro, served through Doubao.' },
91
+ { id: 'kimi-k3.1-pro', key: 'person-kimi-k3-1-pro-261111', name: 'Kimi K3.1 Pro', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
92
+ description: 'Kimi K3.1 Pro, served through Doubao.' },
93
+ { id: 'kimi-k3.1-pro-highspeed', key: 'person-kimi-k3-1-pro-highspeed-261111', name: 'Kimi K3.1 Pro Highspeed', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
94
+ description: 'Kimi K3.1 Pro on the low-latency tier.' },
95
+ { id: 'kimi-k3', key: 'person-kimi-k3-261111', name: 'Kimi K3', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 4,
96
+ description: 'Kimi K3, served through Doubao.' },
97
+ { id: 'glm-5.3', key: 'person-glm-5-3-261111', name: 'GLM 5.3', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 8,
98
+ description: 'GLM 5.3, served through Doubao.' },
99
+ { id: 'glm-5.3-flash', key: 'person-glm-5-3-flash-261111', name: 'GLM 5.3 Flash', modeId: '3', agentMode: 1, convMode: 3, effort: 3, deepThink: 8,
100
+ description: 'GLM 5.3 on the fast tier.' },
101
+ ]
102
+
103
+ /**
104
+ * Older names, kept so a saved session or a hand-written config does not turn
105
+ * into a different model without saying so.
106
+ */
107
+ export const MODEL_ALIASES = {
108
+ 'doubao-pro': 'doubao',
109
+ 'doubao-think': 'doubao-expert',
110
+ }
111
+
112
+ /** The routing entry for an id, or undefined when the id is unknown. */
113
+ export function findModel(id) {
114
+ const canonical = MODEL_ALIASES[id] ?? id
115
+ return MODELS.find((m) => m.id === canonical)
116
+ }
117
+
118
+ /**
119
+ * Same lookup, but forgiving: a session saved against a model this build no
120
+ * longer knows about keeps working instead of failing to answer at all.
121
+ */
122
+ export function resolveModel(id) {
123
+ return findModel(id) ?? MODELS[0]
124
+ }
125
+
126
+ export function buildQuery(identity) {
127
+ // Verified against a live session: this exact shape streams, and all five
128
+ // models answered in one run. `pc_version` mirrors the client version the
129
+ // device reported; the web_platform/device_platform pair stays "web" because
130
+ // that is what the working capture carried.
131
+ const params = new URLSearchParams({
132
+ aid: CHAT_AID,
133
+ channel: process.env.DOUBAO_CHANNEL ?? 'win',
134
+ client_platform: 'pc_client',
135
+ device_platform: 'web',
136
+ doubao_device_platform: 'web',
137
+ pc_version: identity.clientVersion ?? '2.35.12',
138
+ runtime: 'web',
139
+ runtime_version: '3.41.0',
140
+ web_platform: 'web',
141
+ language: 'zh',
142
+ pkg_type: 'release_version',
143
+ real_aid: CHAT_AID,
144
+ region: 'CN',
145
+ sys_region: 'CN',
146
+ samantha_web: '1',
147
+ 'use-olympus-account': '1',
148
+ version_code: '20800',
149
+ device_id: identity.deviceId,
150
+ fp: identity.fp,
151
+ web_id: identity.webId ?? identity.deviceId,
152
+ web_tab_id: crypto.randomUUID(),
153
+ tz_name: process.env.DOUBAO_TZ ?? Intl.DateTimeFormat().resolvedOptions().timeZone ?? 'Etc/GMT-8',
154
+ chromium_version: identity.chromiumVersion ?? '147.0.7727.149',
155
+ tea_uuid: identity.deviceId,
156
+ })
157
+ return params.toString()
158
+ }
159
+
160
+ export function buildBody(text, { model = 'doubao', think, conversationId = '' } = {}) {
161
+ const cfg = resolveModel(model)
162
+ // `think` is the harness reasoning control, and it only makes sense for the
163
+ // chat-surface models that have a thinking switch. For the routed models the
164
+ // model itself already implies the depth, so honouring a stray "off" would
165
+ // quietly downgrade e.g. GLM 5.3 to its non-thinking path.
166
+ const deepThink = cfg.modeId === '1' && think !== undefined ? (think ? 1 : 0) : cfg.deepThink
167
+ const effort = String(cfg.effort)
168
+ return {
169
+ client_meta: {
170
+ local_conversation_id: conversationId || 'local_' + randomDigits(16, '1'),
171
+ conversation_id: conversationId,
172
+ bot_id: BOT_ID,
173
+ last_section_id: '',
174
+ last_message_index: null,
175
+ },
176
+ messages: [
177
+ {
178
+ local_message_id: crypto.randomUUID(),
179
+ content_block: [
180
+ {
181
+ block_type: 10000,
182
+ content: {
183
+ text_block: { text, icon_url: '', icon_url_dark: '', summary: '' },
184
+ pc_event_block: '',
185
+ },
186
+ block_id: crypto.randomUUID(),
187
+ parent_id: '',
188
+ meta_info: [],
189
+ extra_info: {},
190
+ append_fields: [],
191
+ },
192
+ ],
193
+ message_status: 0,
194
+ },
195
+ ],
196
+ option: {
197
+ agent_mode: cfg.agentMode,
198
+ need_deep_think: deepThink,
199
+ scene_type: 0,
200
+ need_create_conversation: !conversationId,
201
+ conversation_init_option: { need_ack_conversation: true },
202
+ conversation_init_ext: { model_item_key: cfg.key, reasoning_effort: effort, mode_id: cfg.modeId },
203
+ sse_recv_event_options: { support_chunk_delta: true },
204
+ support_lazy_fetch_stream: true,
205
+ is_old_user: true,
206
+ recovery_option: {
207
+ is_recovery: false,
208
+ req_create_time_sec: Math.floor(Date.now() / 1000),
209
+ append_sse_event_scene: 0,
210
+ },
211
+ model_config: { model_item_key: cfg.key, model_extra_params: {}, reasoning_effort: cfg.effort },
212
+ aggregate_params: {
213
+ mention_skill_list: '[]',
214
+ mention_plugin_list: '[]',
215
+ mention_ext: '[{}]',
216
+ conversation_mode: String(cfg.convMode),
217
+ mode_id: cfg.modeId,
218
+ model_item_key: cfg.key,
219
+ agent_mode: String(cfg.agentMode),
220
+ reasoning_effort: effort,
221
+ provider_id: '',
222
+ },
223
+ conversation_mode: cfg.convMode,
224
+ unique_key: crypto.randomUUID(),
225
+ },
226
+ user_context: [],
227
+ ext: {
228
+ agent_mode: String(cfg.agentMode),
229
+ use_deep_think: String(deepThink),
230
+ sub_conv_firstmet_type: '1',
231
+ collection_id: '',
232
+ is_finish: '1',
233
+ conversation_init_option: '{"need_ack_conversation":true}',
234
+ commerce_credit_config_enable: '0',
235
+ },
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Walk one SSE frame by frame. Doubao interleaves heartbeats, an ack, a full
241
+ * message snapshot and then deltas, and any of those can carry the risk-control
242
+ * error instead of the text — so the parser reports errors rather than
243
+ * assuming a well-formed stream.
244
+ */
245
+ export function* parseFrames(chunk) {
246
+ const lines = chunk.split('\n')
247
+ let event = ''
248
+ const data = []
249
+ for (const raw of lines) {
250
+ const line = raw.replace(/\r$/, '')
251
+ if (line === '') {
252
+ if (data.length) yield { event, data: data.join('\n') }
253
+ event = ''
254
+ data.length = 0
255
+ continue
256
+ }
257
+ if (line.startsWith('event:')) event = line.slice(6).trim()
258
+ else if (line.startsWith('data:')) data.push(line.slice(5).trim())
259
+ }
260
+ if (data.length) yield { event, data: data.join('\n') }
261
+ }
262
+
263
+ /**
264
+ * Attribute the text in each frame to the block that produced it.
265
+ *
266
+ * Doubao pushes two kinds of text down the same pipe: the answer, and — when
267
+ * thinking is on — the reasoning that precedes it. The reasoning body is an
268
+ * ordinary `block_type` 10000 text block whose `parent_id` points at the 10040
269
+ * thinking-block header (which itself carries no text, only the "正在思考" /
270
+ * "已完成思考" titles). `CHUNK_DELTA` frames carry no block identity at all, so a
271
+ * delta belongs to whichever text block was declared last.
272
+ *
273
+ * Treating every delta as answer text is what used to glue the reasoning onto
274
+ * the front of the reply — measured on a real turn, a 259-character 思考 stream
275
+ * arrived 6 seconds before the answer and was rendered as part of it — and it
276
+ * also threw away the only stream the UI could show while the model was busy.
277
+ *
278
+ * The tracker is stateful because a delta cannot be classified on its own; one
279
+ * instance belongs to one stream.
280
+ */
281
+ export function createTextTracker() {
282
+ const thinkingBlocks = new Set()
283
+ let kind = 'answer'
284
+ return {
285
+ /** The kind the next `CHUNK_DELTA` will be attributed to. */
286
+ get kind() {
287
+ return kind
288
+ },
289
+ /**
290
+ * Read one SSE frame.
291
+ * @returns {{kind: 'answer'|'reasoning', text: string}[]} text segments, in order.
292
+ */
293
+ absorb(event, payload) {
294
+ if (!payload) return []
295
+ const blocks = []
296
+ if (event === 'STREAM_MSG_NOTIFY' || event === 'FULL_MSG_NOTIFY') {
297
+ const message = payload?.message
298
+ const content = message?.content ?? payload?.content
299
+ blocks.push(...(content?.content_block ?? []))
300
+ } else if (event === 'STREAM_CHUNK') {
301
+ for (const op of payload?.patch_op ?? []) {
302
+ if (op?.patch_object === 111) continue // text-to-speech mirror of the same text
303
+ blocks.push(...(op?.patch_value?.content_block ?? []))
304
+ }
305
+ }
306
+ const out = []
307
+ for (const b of blocks) {
308
+ const type = Number(b?.block_type)
309
+ if (type === 10040) {
310
+ // Header only: remember the id so its child text block can be spotted.
311
+ const id = b?.block_id
312
+ if (id !== undefined && id !== null && id !== '') thinkingBlocks.add(String(id))
313
+ kind = 'reasoning'
314
+ continue
315
+ }
316
+ if (type !== 10000) continue
317
+ const parent = b?.parent_id
318
+ const isThinkingChild =
319
+ parent !== undefined && parent !== null && parent !== '' && parent !== 0 && thinkingBlocks.has(String(parent))
320
+ kind = isThinkingChild ? 'reasoning' : 'answer'
321
+ const text = b?.content?.text_block?.text
322
+ if (typeof text === 'string' && text !== '') out.push({ kind, text })
323
+ }
324
+ // The server sends the bulk of the answer as CHUNK_DELTA once
325
+ // `support_chunk_delta` is on. Missing this event silently truncates every
326
+ // reply to its first frame — verified against the live stream, where the
327
+ // three sources concatenate exactly to the message brief.
328
+ if (event === 'CHUNK_DELTA' && typeof payload.text === 'string' && payload.text !== '') {
329
+ out.push({ kind, text: payload.text })
330
+ }
331
+ return out
332
+ },
333
+ }
334
+ }
335
+
336
+ /** Pull the answer text out of one self-contained SSE event. */
337
+ export function textOf(event, payload) {
338
+ return createTextTracker()
339
+ .absorb(event, payload)
340
+ .filter((segment) => segment.kind === 'answer')
341
+ .map((segment) => segment.text)
342
+ .join('')
343
+ }
344
+
345
+ export const RISK_CODES = new Set([710022002, 710022004])
346
+ export const AUTH_CODES = new Set([710012001, 710012002])
347
+
348
+ /** Cooldown key used when a penalty is not tied to one model. */
349
+ const GLOBAL_COOLDOWN = '*'
350
+
351
+ /**
352
+ * Serialize outbound turns and keep a minimum gap between them.
353
+ *
354
+ * The account is shared and the risk engine reacts to bursts, not to volume: five
355
+ * quick calls in one probe run were enough to earn a cooldown that outlasted the
356
+ * run. So the lock spans the whole turn (request *and* stream) — releasing it
357
+ * after the POST would let several streams overlap, which is the exact pattern
358
+ * that trips 710022004.
359
+ *
360
+ * Two properties matter for switching models mid-conversation:
361
+ *
362
+ * - The lock cannot outlive the turn that took it. Holding it in a generator's
363
+ * `finally` is not enough: when a consumer drops an async iterator without
364
+ * calling `return()` (which is what an aborted turn does), that block never
365
+ * runs and every later turn waits on a promise nobody will resolve. Each lease
366
+ * therefore carries a deadline, is extended while the stream keeps producing,
367
+ * and ends early when the turn's abort signal fires.
368
+ * - A cooldown belongs to the model that earned it. 710022004 is answered by a
369
+ * fresh request to a *different* backend without incident, so a penalty on one
370
+ * model must not make every other model unavailable for the next minute.
371
+ */
372
+ export class Pacer {
373
+ constructor({ minIntervalMs = 1500, leaseMs = 45_000, now = () => Date.now(), sleep } = {}) {
374
+ this.minIntervalMs = minIntervalMs
375
+ this.leaseMs = leaseMs
376
+ this.now = now
377
+ this.sleep = sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)))
378
+ this.lastAt = 0
379
+ this.tail = Promise.resolve()
380
+ this.cooldowns = new Map()
381
+ }
382
+
383
+ /**
384
+ * Take a lease on the turn lock.
385
+ *
386
+ * `done` resolves exactly once — on `end()` or when the deadline passes with no
387
+ * sign of life. `touch()` pushes the deadline out; the watchdog only fires for
388
+ * a turn that went silent.
389
+ */
390
+ openLease() {
391
+ let release
392
+ const done = new Promise((r) => {
393
+ release = r
394
+ })
395
+ let released = false
396
+ let timer = null
397
+ const end = () => {
398
+ if (released) return
399
+ released = true
400
+ if (timer !== null) {
401
+ clearTimeout(timer)
402
+ timer = null
403
+ }
404
+ release()
405
+ }
406
+ const touch = () => {
407
+ if (released) return
408
+ if (timer !== null) clearTimeout(timer)
409
+ timer = setTimeout(end, this.leaseMs)
410
+ timer.unref?.()
411
+ }
412
+ touch()
413
+ return { done, touch, end }
414
+ }
415
+
416
+ /** Wait until this caller is allowed to send. Call only inside a lease. */
417
+ async wait(key) {
418
+ const cooldown = this.cooldownFor(key)
419
+ if (cooldown > 0) await this.sleep(cooldown)
420
+ const gap = this.minIntervalMs - (this.now() - this.lastAt)
421
+ if (gap > 0) await this.sleep(gap)
422
+ this.lastAt = this.now()
423
+ }
424
+
425
+ /** Run one turn exclusively; everything else queues behind it. */
426
+ async run(fn, { key, signal } = {}) {
427
+ const previous = this.tail
428
+ const lease = this.openLease()
429
+ this.tail = lease.done
430
+ const onAbort = () => lease.end()
431
+ if (signal) {
432
+ if (signal.aborted) lease.end()
433
+ else signal.addEventListener?.('abort', onAbort, { once: true })
434
+ }
435
+ await previous
436
+ try {
437
+ await this.wait(key)
438
+ return await fn()
439
+ } finally {
440
+ signal?.removeEventListener?.('abort', onAbort)
441
+ lease.end()
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Same exclusive turn, but for work that yields as it goes.
447
+ *
448
+ * `run` cannot be used for this: `for await` does not await a promise before
449
+ * looking for `Symbol.asyncIterator`, so `for await (x of run(() => gen()))`
450
+ * throws "is not a function or its return value is not async iterable". Here the
451
+ * generator yields through, and the lock is released when it finishes, when the
452
+ * caller aborts, or when it goes silent long enough for the lease to expire —
453
+ * which is the whole point of holding it across the stream.
454
+ */
455
+ async *runIterable(makeIterable, { key, signal } = {}) {
456
+ const previous = this.tail
457
+ const lease = this.openLease()
458
+ this.tail = lease.done
459
+ const onAbort = () => lease.end()
460
+ if (signal) {
461
+ if (signal.aborted) lease.end()
462
+ else signal.addEventListener?.('abort', onAbort, { once: true })
463
+ }
464
+ await previous
465
+ try {
466
+ await this.wait(key)
467
+ for await (const value of makeIterable()) {
468
+ // A stream that is still producing is alive; only silence expires.
469
+ lease.touch()
470
+ yield value
471
+ }
472
+ } finally {
473
+ signal?.removeEventListener?.('abort', onAbort)
474
+ lease.end()
475
+ }
476
+ }
477
+
478
+ /**
479
+ * Cool the given key (a model id, or GLOBAL when omitted) down.
480
+ *
481
+ * Waiters ask for their own key, so a penalty taken by one model is not paid by
482
+ * the next one the user picks.
483
+ */
484
+ penalize(ms, key = GLOBAL_COOLDOWN) {
485
+ const until = this.now() + ms
486
+ this.cooldowns.set(key, Math.max(this.cooldowns.get(key) ?? 0, until))
487
+ }
488
+
489
+ /** How long a caller for `key` would have to wait, in ms. */
490
+ cooldownFor(key) {
491
+ const now = this.now()
492
+ const own = key === undefined ? 0 : (this.cooldowns.get(key) ?? 0) - now
493
+ const global = (this.cooldowns.get(GLOBAL_COOLDOWN) ?? 0) - now
494
+ return Math.max(0, own, global)
495
+ }
496
+
497
+ get coolingDownMs() {
498
+ const now = this.now()
499
+ let longest = 0
500
+ for (const until of this.cooldowns.values()) longest = Math.max(longest, until - now)
501
+ return Math.max(0, longest)
502
+ }
503
+ }
504
+
505
+ /**
506
+ * Turn a stream of SSE text into events.
507
+ *
508
+ * Transport-agnostic on purpose: the bridge hands over strings and a direct
509
+ * fetch hands over bytes, and both have to frame identically or the two paths
510
+ * would drift apart in behaviour.
511
+ */
512
+ async function* readEvents(chunks) {
513
+ const decoder = new TextDecoder()
514
+ let buffer = ''
515
+ let sawError = false
516
+ const tracker = createTextTracker()
517
+
518
+ function* drain(block) {
519
+ for (const frame of parseFrames(block)) {
520
+ const payload = tryJson(frame.data)
521
+ if (frame.event === 'SSE_ACK') {
522
+ const meta = payload?.ack_client_meta ?? {}
523
+ if (meta.conversation_id) yield { type: 'meta', conversationId: meta.conversation_id }
524
+ continue
525
+ }
526
+ if (frame.event === 'STREAM_ERROR' || frame.event === 'SSE_ERROR') {
527
+ sawError = true
528
+ const code = Number(payload?.error_code ?? payload?.code ?? 0)
529
+ yield {
530
+ type: 'error',
531
+ code,
532
+ message: payload?.error_msg ?? payload?.message ?? 'stream error',
533
+ raw: payload,
534
+ }
535
+ continue
536
+ }
537
+ if (frame.event === 'FULL_MSG_NOTIFY') {
538
+ // full snapshot arrives before the deltas; ignore it and let STREAM_CHUNK drive
539
+ const meta = payload?.message?.conversation_id
540
+ if (meta) yield { type: 'meta', conversationId: meta }
541
+ continue
542
+ }
543
+ if (frame.event === 'SSE_REPLY_END') {
544
+ yield { type: 'end', endType: payload?.end_type, brief: payload?.msg_finish_attr?.brief }
545
+ continue
546
+ }
547
+ for (const segment of tracker.absorb(frame.event, payload)) {
548
+ yield { type: segment.kind === 'reasoning' ? 'reasoning' : 'delta', text: segment.text }
549
+ }
550
+ }
551
+ }
552
+
553
+ for await (const chunk of chunks) {
554
+ buffer += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true })
555
+ // frames are separated by a blank line; keep the tail for the next read
556
+ const cut = buffer.lastIndexOf('\n\n')
557
+ if (cut < 0) continue
558
+ const block = buffer.slice(0, cut + 2)
559
+ buffer = buffer.slice(cut + 2)
560
+ yield* drain(block)
561
+ }
562
+ if (!sawError) {
563
+ // flush a trailing frame that lacked its blank-line terminator
564
+ yield* drain(buffer)
565
+ }
566
+ }
567
+
568
+ /** The risk-control code that means "this account now needs a signed request". */
569
+ export const RISK_CODE = 710022004
570
+
571
+ /**
572
+ * Stream parsed events from the live endpoint.
573
+ *
574
+ * Two transports exist. A plain Node request (`fetchDirect`) carries no
575
+ * `a_bogus`/`msToken` — the request shape the current front end itself uses —
576
+ * and it is enough while the account is in good standing. Once the account is
577
+ * flagged, the service answers 710022004 and only the desktop client's own
578
+ * preload-signed request (over the debugger bridge) gets through.
579
+ *
580
+ * Order matters for the "no needless client launch" goal. The old code probed
581
+ * `ensureBridge` on *every* turn, which launches the client even for a clean
582
+ * account that would have answered over plain HTTP. Now the direct request is
583
+ * tried first:
584
+ *
585
+ * - clean account -> direct succeeds, the client is never touched, and DSH
586
+ * restarts change nothing;
587
+ * - 710022004 -> the account is marked in `riskMemory` (so the next turns
588
+ * skip the doomed direct probe), then `ensure` brings up
589
+ * the signed bridge as a fallback;
590
+ * - flagged on entry -> go straight to the bridge, no direct probe at all;
591
+ * - `bridge: false` -> direct only, no memory, no fallback.
592
+ *
593
+ * The flag expires (see risk-memory.js) so a recovered account stops using the
594
+ * bridge on its own. Every transport call is injected so tests can exercise all
595
+ * four paths without a real client; `streamChat` binds the live implementations.
596
+ */
597
+ export function createStreamChat({
598
+ available = bridgeAvailable, // probe only: is a client already reachable?
599
+ ensure = ensureBridge, // bring a client up for the fallback path
600
+ stream = bridgeStream,
601
+ riskMemory = createRiskMemory(),
602
+ // Live lookup, not `fetch` captured at factory time: the exported streamChat is
603
+ // built at module load, and a bound reference would bypass tests (and any host
604
+ // that installs a fetch shim after import) forever.
605
+ fetchDirect = (...args) => fetch(...args),
606
+ } = {}) {
607
+ return async function* streamChat({
608
+ identity,
609
+ cookies,
610
+ text,
611
+ model,
612
+ think,
613
+ conversationId,
614
+ signal,
615
+ bridge = true,
616
+ bridgePort,
617
+ riskKey,
618
+ riskMemory: callRiskMemory,
619
+ }) {
620
+ const url = 'https://www.doubao.com/chat/completion?' + buildQuery(identity)
621
+ const body = JSON.stringify(buildBody(text, { model, think, conversationId }))
622
+ const memory = callRiskMemory ?? riskMemory
623
+ // The device id is stable across DSH restarts, so persisting flags under it
624
+ // survives a restart as intended. Fall back to a process-wide key when a
625
+ // caller has no identity (older tests and direct callers).
626
+ const flagKey = riskKey ?? identity?.deviceId ?? 'default'
627
+
628
+ async function* directEvents() {
629
+ const res = await fetchDirect(url, {
630
+ method: 'POST',
631
+ headers: {
632
+ 'Content-Type': 'application/json',
633
+ 'agw-js-conv': 'str',
634
+ Cookie: cookies,
635
+ 'User-Agent': DEFAULT_UA,
636
+ Origin: 'https://www.doubao.com',
637
+ Referer: 'https://www.doubao.com/chat/',
638
+ },
639
+ body,
640
+ signal,
641
+ })
642
+ if (!res.ok || !res.body) {
643
+ const detail = await res.text().catch(() => '')
644
+ const isRisk = res.status === RISK_CODE || new RegExp(String(RISK_CODE)).test(detail)
645
+ yield isRisk
646
+ ? { type: 'error', code: RISK_CODE, message: detail.slice(0, 300) }
647
+ : { type: 'error', code: res.status, message: `http ${res.status} ${detail.slice(0, 300)}` }
648
+ return
649
+ }
650
+ yield* readEvents(res.body)
651
+ }
652
+
653
+ async function* viaBridge() {
654
+ try {
655
+ yield* readEvents(stream({ url, body, port: bridgePort ?? BRIDGE_PORT, signal }))
656
+ } catch (error) {
657
+ if (signal?.aborted) throw error
658
+ // The client was there but refused mid-flight. Calling this a rate limit
659
+ // would send the host down a retry path that only deepens the problem.
660
+ yield { type: 'error', code: 'BRIDGE', message: `client bridge failed: ${error.message}` }
661
+ }
662
+ }
663
+
664
+ // Opt-out: no memory, no fallback, no client probing of any kind.
665
+ if (bridge === false) {
666
+ yield* directEvents()
667
+ return
668
+ }
669
+
670
+ // Already flagged: skip the doomed direct request and go straight to the
671
+ // signed path, bringing a client up only if one is not already reachable.
672
+ if (memory.isFlagged(flagKey)) {
673
+ const ready = await ensure(bridgePort).catch(() => false)
674
+ if (ready) {
675
+ yield* viaBridge()
676
+ return
677
+ }
678
+ // No client could be brought up. Try direct once anyway: the flag may have
679
+ // expired in the real world even if our clock has not caught up, and a hard
680
+ // failure here would leave the user with no answer at all.
681
+ yield* directEvents()
682
+ return
683
+ }
684
+
685
+ // Direct first. Risk refusals arrive before any content, so buffer frames
686
+ // until the first real chunk: if 710022004 shows up we fall back without
687
+ // having leaked a partial answer; otherwise we flush and stream as normal.
688
+ const buffered = []
689
+ let started = false
690
+ let risk = false
691
+ for await (const ev of directEvents()) {
692
+ if (ev.type === 'error' && ev.code === RISK_CODE && !started) {
693
+ risk = true
694
+ break
695
+ }
696
+ if (!started && (ev.type === 'delta' || ev.type === 'reasoning' || ev.type === 'end')) {
697
+ started = true
698
+ for (const b of buffered) yield b
699
+ buffered.length = 0
700
+ }
701
+ if (started) yield ev
702
+ else buffered.push(ev)
703
+ }
704
+ if (!started && !risk) {
705
+ for (const b of buffered) yield b
706
+ return
707
+ }
708
+ if (!risk) return
709
+
710
+ // Risk control hit on the direct path: remember it, then retry signed.
711
+ memory.mark(flagKey)
712
+ const ready = await ensure(bridgePort).catch(() => false)
713
+ if (ready) {
714
+ yield* viaBridge()
715
+ return
716
+ }
717
+ yield {
718
+ type: 'error',
719
+ code: RISK_CODE,
720
+ message: `Doubao risk control (${RISK_CODE}) and no signed client bridge could be started`,
721
+ }
722
+ }
723
+ }
724
+
725
+ /** The live transport: direct request first, signed bridge only when needed. */
726
+ export const streamChat = createStreamChat()
727
+
728
+ function tryJson(s) {
729
+ try {
730
+ return JSON.parse(s)
731
+ } catch {
732
+ return undefined
733
+ }
734
+ }
735
+
736
+ /** Non-streaming convenience wrapper. */
737
+ export async function completeChat(options) {
738
+ let text = ''
739
+ let conversationId
740
+ for await (const ev of streamChat(options)) {
741
+ if (ev.type === 'delta') text += ev.text
742
+ else if (ev.type === 'meta') conversationId = ev.conversationId
743
+ else if (ev.type === 'error') throw Object.assign(new Error(ev.message), { code: ev.code, raw: ev.raw })
744
+ }
745
+ return { text, conversationId }
746
+ }