@kabxx/dsh-btw 0.0.0-stage → 0.1.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.
@@ -0,0 +1,783 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { realpathSync } from 'node:fs'
3
+ import { createRequire } from 'node:module'
4
+ import { resolve } from 'node:path'
5
+ import { pathToFileURL } from 'node:url'
6
+ import {
7
+ CONTEXT_OVERFLOW, PACKAGE_NAME, TOKEN_FIELDS, contextDescriptor, parseDetail, parseQuestion, parseSessionId, parseThreadId, parseVersion,
8
+ } from '../shared/wire.js'
9
+ import { sessionThreadStore } from './session-store.js'
10
+
11
+ const requireHere = createRequire(import.meta.url)
12
+ const requireFrom = []
13
+ if (process.env.DSH_INSTALL_DIR) {
14
+ requireFrom.push(createRequire(resolve(process.env.DSH_INSTALL_DIR, 'package.json')))
15
+ }
16
+ if (process.argv[1]) {
17
+ try {
18
+ // The Web Host starts from the CLI entry, even when its shell-tool-only
19
+ // environment variables are absent and the bundle is linked elsewhere.
20
+ requireFrom.push(createRequire(realpathSync(process.argv[1])))
21
+ } catch {
22
+ // Inline or embedded launchers may provide only the package-local resolver.
23
+ }
24
+ }
25
+ requireFrom.push(requireHere)
26
+ function fromDsh(name) {
27
+ for (const locate of requireFrom) {
28
+ let entry
29
+ try {
30
+ entry = locate.resolve(name)
31
+ } catch (error) {
32
+ if (error?.code !== 'MODULE_NOT_FOUND') throw error
33
+ continue
34
+ }
35
+ return import(pathToFileURL(entry).href)
36
+ }
37
+ throw new Error(`BTW could not resolve installed Host package ${name}`)
38
+ }
39
+ const [
40
+ { Service }, { RemoteError, TypertRemoteService }, { z }, { estimateMessage, estimateToolsTokens },
41
+ { BlockAssembler, CONTEXT_WINDOW_EXCEEDED_CODE, isAgentLoopRequest, isTokenDelta },
42
+ { PiAiAdapter },
43
+ ] = await Promise.all([
44
+ fromDsh('@deepseek-ai/cordis'),
45
+ fromDsh('@deepseek-ai/dsh-typert-protocol'),
46
+ fromDsh('zod'),
47
+ fromDsh('@deepseek-ai/dsh-token-meter/estimate'),
48
+ fromDsh('@deepseek-ai/dsh-llm'),
49
+ fromDsh('@deepseek-ai/dsh-llm-pi-ai'),
50
+ ])
51
+
52
+ export const inject = ['agents', 'llm', 'sessionPersistence', 'typert']
53
+
54
+ const CHECKPOINT_INTERVAL_MS = 500
55
+ const PREVIEW_INTERVAL_MS = 45
56
+ const NO_TOOLS = 'Reply in text only. Do not call tools. Treat the preceding conversation as read-only reference context, and state what is missing if answering requires inspecting files or running commands.'
57
+ const INTERRUPTION = '回复已中断。'
58
+ const FAILURE = '回复生成失败。'
59
+ const NO_TEXT = '模型未返回文本内容。'
60
+ const ARCHIVE_ERROR = 'archive-write-failed'
61
+ const tokenCount = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)
62
+ const timestamp = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)
63
+ const providerKey = z.string().min(1)
64
+ const routeSchema = z.strictObject({
65
+ provider: providerKey,
66
+ model: providerKey,
67
+ reasoningEffort: providerKey.optional(),
68
+ })
69
+ const nativeBlock = z.looseObject({ type: z.string().min(1) })
70
+ const nativeMessage = z.looseObject({
71
+ id: z.string().min(1),
72
+ role: z.enum(['system', 'developer', 'user', 'assistant', 'tool']),
73
+ content: z.array(nativeBlock),
74
+ source: z.looseObject({ kind: z.string().min(1) }),
75
+ })
76
+ const toolSchema = z.looseObject({
77
+ name: z.string(), description: z.string(), parameters: z.record(z.string(), z.json()),
78
+ })
79
+ const nativeSnapshot = z.strictObject({
80
+ mode: z.literal('native'),
81
+ anchor: timestamp.nullable(),
82
+ messages: z.array(nativeMessage),
83
+ system: z.string().optional(),
84
+ tools: z.array(toolSchema).optional(),
85
+ toolHistory: z.strictObject({
86
+ tools: z.array(toolSchema),
87
+ updates: z.array(z.strictObject({ messageId: z.string(), additions: z.array(toolSchema) })),
88
+ }).optional(),
89
+ temperature: z.number().optional(),
90
+ stop: z.array(z.string()).optional(),
91
+ measuredPrefix: tokenCount.optional(),
92
+ })
93
+ const turnSchema = z.strictObject({
94
+ question: z.string().min(1),
95
+ reply: z.string(),
96
+ status: z.enum(['generating', 'completed', 'failed', 'interrupted']),
97
+ createdAt: timestamp,
98
+ finishedAt: timestamp.optional(),
99
+ error: z.string().optional(),
100
+ saveError: z.string().optional(),
101
+ modelReply: nativeMessage.optional(),
102
+ usage: z.strictObject(Object.fromEntries(TOKEN_FIELDS.map(key => [key, tokenCount.optional()]))).optional(),
103
+ timing: z.strictObject({ requestMs: timestamp, firstTokenMs: timestamp.optional() }).optional(),
104
+ })
105
+ export const threadSchema = z.strictObject({
106
+ id: z.string(),
107
+ sessionId: z.string(),
108
+ question: z.string().min(1),
109
+ createdAt: timestamp,
110
+ updatedAt: timestamp,
111
+ version: z.number().int().min(1).max(Number.MAX_SAFE_INTEGER),
112
+ turns: z.array(turnSchema).min(1),
113
+ route: routeSchema,
114
+ snapshot: nativeSnapshot,
115
+ }).superRefine((record, context) => {
116
+ try {
117
+ parseDetail(toDetailShape(record))
118
+ if (record.question !== record.turns[0].question || record.turns.slice(0, -1).some(turn => turn.status === 'generating')) {
119
+ throw new Error('invalid thread history')
120
+ }
121
+ } catch {
122
+ context.addIssue({ code: 'custom', message: 'Invalid BTW thread record' })
123
+ }
124
+ })
125
+
126
+ function fault(code, message) {
127
+ return new RemoteError(`btw/${code}`, message)
128
+ }
129
+
130
+ function newest(record) {
131
+ return record.turns.at(-1)
132
+ }
133
+
134
+ function publicTurn({ modelReply, ...fields }) {
135
+ return fields
136
+ }
137
+
138
+ function toDetailShape({ route, snapshot, turns, ...publicFields }) {
139
+ return { ...publicFields, turns: turns.map(publicTurn) }
140
+ }
141
+
142
+ function toDetail(record) {
143
+ return parseDetail(toDetailShape(record))
144
+ }
145
+
146
+ function toSummary({ id, sessionId, question, createdAt, updatedAt, version, turns }) {
147
+ return { id, sessionId, question, createdAt, updatedAt, version, status: turns.at(-1).status }
148
+ }
149
+
150
+ function nextRevision(record, turns, time = Date.now()) {
151
+ return { ...record, turns, updatedAt: Math.max(time, record.updatedAt), version: record.version + 1 }
152
+ }
153
+
154
+ function nextTurn(question, createdAt) {
155
+ return { question, reply: '', status: 'generating', createdAt }
156
+ }
157
+
158
+ function withLatest(record, turn, time) {
159
+ return nextRevision(record, [...record.turns.slice(0, -1), turn], time)
160
+ }
161
+
162
+ function isDone(record) {
163
+ return newest(record).status !== 'generating'
164
+ }
165
+
166
+ // A side request can start while the main loop is executing tools. Only a fully
167
+ // answered tool batch belongs to the frozen prefix. Leave that pending tail in
168
+ // the main loop and retain every preceding committed block unchanged.
169
+ function completeToolBoundary(messages) {
170
+ let complete = 0
171
+ const pending = new Set()
172
+ for (const [index, message] of messages.entries()) {
173
+ if (message.role === 'assistant') {
174
+ if (pending.size) break
175
+ for (const block of message.content) {
176
+ if (block.type === 'tool-call') pending.add(block.id)
177
+ }
178
+ } else if (message.role === 'tool') {
179
+ if (!pending.delete(message.toolCallId)) break
180
+ } else if (message.role === 'user' && pending.size) {
181
+ break
182
+ }
183
+ if (!pending.size) complete = index + 1
184
+ }
185
+ return complete
186
+ }
187
+
188
+ function sameRoute(left, right) {
189
+ return left?.provider === right.provider && left.model === right.model
190
+ }
191
+
192
+ function freezeSnapshot(session, selected, observed, tokenMeter) {
193
+ const derived = session.deriveMessages()
194
+ const end = completeToolBoundary(derived)
195
+ const matchesSurface = observed?.request.messages.length === end &&
196
+ observed.request.messages.every((message, index) => message === derived[index])
197
+ const exact = matchesSurface && sameRoute(observed.request, selected) ? observed : undefined
198
+ const header = session.requestHeader?.()
199
+ const controls = exact?.request ?? (sameRoute(header?.config, selected) ? header.config : selected)
200
+ const route = { ...selected, reasoningEffort: selected.reasoningEffort ?? controls.reasoningEffort }
201
+ const messages = exact?.request.messages ?? derived.slice(0, end)
202
+ const tools = exact ? exact.request.tools : header?.tools
203
+ let toolHistory = exact ? exact.request.toolHistory : session.toolHistory?.()
204
+ if (!exact && toolHistory && end < derived.length) {
205
+ const messageIds = new Set(messages.map(message => message.id))
206
+ toolHistory = { ...toolHistory, updates: toolHistory.updates.filter(update => messageIds.has(update.messageId)) }
207
+ }
208
+ let measuredPrefix
209
+ if (end === derived.length && tokenMeter) {
210
+ const { temperature, stop, maxTokens } = controls
211
+ const envelope = { config: { ...route, temperature, stop, maxTokens }, ...(tools && { tools }) }
212
+ try { measuredPrefix = tokenMeter.measure(session, envelope).totalTokens } catch { /* Estimate below. */ }
213
+ }
214
+ // Persist only lossless-JSON model inputs; the session identity, call signal,
215
+ // caller, and prepared dispatch belong exclusively to the original request.
216
+ const snapshot = JSON.parse(JSON.stringify({
217
+ mode: 'native',
218
+ anchor: exact?.anchor ?? (end === derived.length ? session.surface?.nodes?.at(-1) ?? null : null),
219
+ messages,
220
+ system: exact?.request.system,
221
+ tools,
222
+ toolHistory,
223
+ temperature: controls.temperature,
224
+ stop: controls.stop,
225
+ measuredPrefix: Number.isSafeInteger(measuredPrefix) && measuredPrefix >= 0 ? measuredPrefix : undefined,
226
+ }))
227
+ return { route, snapshot }
228
+ }
229
+
230
+ function userInput(text) {
231
+ return { role: 'user', content: [{ type: 'text', text }] }
232
+ }
233
+
234
+ function assistantInput(id, text, route) {
235
+ return {
236
+ id,
237
+ role: 'assistant',
238
+ content: [{ type: 'text', text }],
239
+ source: { kind: 'model', provider: route.provider, model: route.model },
240
+ }
241
+ }
242
+
243
+ function questionInput(question) {
244
+ return userInput(`${NO_TOOLS}\n\nQuestion:\n${question}`)
245
+ }
246
+
247
+ function requestMessages(record, includeSettledLastReply = false) {
248
+ const { snapshot } = record
249
+ const messages = [...snapshot.messages]
250
+ for (const [index, turn] of record.turns.entries()) {
251
+ messages.push(questionInput(turn.question))
252
+ if (index === record.turns.length - 1 && !includeSettledLastReply) break
253
+ // Native history includes user input and committed assistant messages.
254
+ // Failed provider attempts and recovered checkpoints are UI history only.
255
+ if (turn.modelReply) messages.push(turn.modelReply)
256
+ else if (turn.status === 'completed' && turn.reply.trim()) {
257
+ messages.push(assistantInput(`${record.id}/reply/${index + 1}`, turn.reply, record.route))
258
+ }
259
+ }
260
+ return messages
261
+ }
262
+
263
+ function requestFields(snapshot) {
264
+ const { system, tools, toolHistory, temperature, stop } = snapshot
265
+ return {
266
+ ...(system !== undefined && { system }), ...(tools !== undefined && { tools }),
267
+ ...(toolHistory !== undefined && { toolHistory }), ...(temperature !== undefined && { temperature }),
268
+ ...(stop !== undefined && { stop }),
269
+ }
270
+ }
271
+
272
+ function responsesRegistration(llm, { provider, model }) {
273
+ // DSH 0.2.2-rc.5 exposes no transport-only cache hint. Qualify the exact
274
+ // registered adapter and model before using its internal prepared dispatch.
275
+ if (typeof llm.registration !== 'function' || typeof llm.normalizeModelInfo !== 'function' ||
276
+ typeof llm.resolveCallWithInfo !== 'function' || typeof llm.streamWithRegistration !== 'function') return
277
+ try {
278
+ const registration = llm.registration(provider)
279
+ const { adapter } = registration
280
+ if (Object.getPrototypeOf(adapter) !== PiAiAdapter.prototype) return
281
+ const descriptor = adapter.modelOf(adapter.current(), provider, model)
282
+ if (descriptor.api === 'openai-responses' && descriptor.compat?.promptCacheKeyMode !== 'disabled') return registration
283
+ } catch {
284
+ // Let the ordinary runtime report errors for unknown routes and models.
285
+ }
286
+ }
287
+
288
+ async function* streamSideQuestion(llm, options, record) {
289
+ const registration = responsesRegistration(llm, options)
290
+ if (!registration) {
291
+ yield* llm.stream(options)
292
+ return
293
+ }
294
+
295
+ // The stock Pi adapter captures the same model snapshot synchronously at
296
+ // prepareCall entry. Late-bind the session ID only to this pinned adapter
297
+ // dispatch. Generic llm/stream listeners must keep seeing the side request
298
+ // without an ID, including listeners that flush or mutate a main session.
299
+ const { provider, model, reasoningEffort, temperature, maxTokens, stop, signal } = options
300
+ const prepared = await registration.adapter.prepareCall(provider, model, signal)
301
+ const modelInfo = llm.normalizeModelInfo(registration, model, prepared.model)
302
+ const { config } = llm.resolveCallWithInfo({ provider, model, reasoningEffort, temperature, maxTokens, stop }, modelInfo)
303
+ yield* llm.streamWithRegistration({ ...options, ...config }, {
304
+ registration, config, modelInfo,
305
+ dispatch: request => prepared.stream({ ...request, sessionId: record.sessionId }),
306
+ })
307
+ }
308
+
309
+ function modelReplyFor(record, assembler, interrupted) {
310
+ const received = interrupted ? assembler.interruptedBlocks() : assembler.blocks()
311
+ const content = received.filter(block => block.type === 'text' || block.type === 'reasoning')
312
+ if (!content.length) return undefined
313
+ const replayState = interrupted || content.length !== received.length ? undefined : assembler.replayState
314
+ return {
315
+ ...assistantInput(`${record.id}/reply/${record.turns.length}`, '', record.route), content,
316
+ ...(replayState !== undefined && { source: { kind: 'model', provider: record.route.provider, model: record.route.model, replayState } }),
317
+ }
318
+ }
319
+
320
+ function readUsage(usage) {
321
+ const counts = {}
322
+ for (const key of TOKEN_FIELDS) {
323
+ const count = usage?.[key]
324
+ if (Number.isSafeInteger(count) && count >= 0) counts[key] = count
325
+ }
326
+ return Object.keys(counts).length > 0 ? counts : undefined
327
+ }
328
+
329
+ // Coalesce UI wakeups: each watcher reads the newest immutable revision, so a slow
330
+ // browser does not accumulate a queue of full answers. There is no async gap between
331
+ // checking the version and subscribing to the next pulse.
332
+ function waitForPulse(listeners, id, signal) {
333
+ signal.throwIfAborted()
334
+ return new Promise((resolvePulse, rejectPulse) => {
335
+ const current = listeners.get(id) ?? new Set()
336
+ listeners.set(id, current)
337
+ function cleanup() {
338
+ current.delete(wake)
339
+ if (current.size === 0) listeners.delete(id)
340
+ signal.removeEventListener('abort', onAbort)
341
+ }
342
+ function wake() {
343
+ cleanup()
344
+ resolvePulse()
345
+ }
346
+ function onAbort() {
347
+ cleanup()
348
+ rejectPulse(signal.reason)
349
+ }
350
+ current.add(wake)
351
+ signal.addEventListener('abort', onAbort, { once: true })
352
+ })
353
+ }
354
+
355
+ export class BtwHost {
356
+ constructor({ store, llm, logger, tokenMeter }) {
357
+ this.store = store
358
+ this.llm = llm
359
+ this.logger = logger
360
+ this.tokenMeter = tokenMeter
361
+ this.mainRequests = new WeakMap()
362
+ this.runs = new Map()
363
+ this.listeners = new Map()
364
+ this.sessions = new Map()
365
+ this.opening = new Map()
366
+ this.admissions = new Set()
367
+ this.lifetime = new AbortController()
368
+ }
369
+
370
+ async prepare(sessionId, signal) {
371
+ signal?.throwIfAborted()
372
+ this.lifetime.signal.throwIfAborted()
373
+ if (this.opening.has(sessionId) || !this.sessions.has(sessionId)) {
374
+ let opening = this.opening.get(sessionId)
375
+ if (!opening) {
376
+ opening = this.openSession(sessionId).finally(() => this.opening.delete(sessionId))
377
+ this.opening.set(sessionId, opening)
378
+ }
379
+ await opening
380
+ }
381
+ signal?.throwIfAborted()
382
+ this.lifetime.signal.throwIfAborted()
383
+ return this.sessions.has(sessionId)
384
+ }
385
+
386
+ async openSession(sessionId) {
387
+ const archive = await this.store.open(sessionId, this.lifetime.signal)
388
+ if (!archive) return
389
+ this.sessions.set(sessionId, { archive, records: new Map() })
390
+ for (const record of archive.records) {
391
+ this.publish(record, false)
392
+ if (isDone(record)) continue
393
+ const time = Math.max(Date.now(), record.updatedAt)
394
+ const terminal = withLatest(record, {
395
+ ...newest(record), status: 'interrupted', finishedAt: time, error: INTERRUPTION,
396
+ }, time)
397
+ this.publish(terminal, false)
398
+ try {
399
+ await this.save(terminal)
400
+ } catch {
401
+ this.logger?.error('BTW could not save a recovered reply')
402
+ this.publish(withLatest(terminal, { ...newest(terminal), saveError: ARCHIVE_ERROR }, time), false)
403
+ }
404
+ }
405
+ }
406
+
407
+ observeMain(session, request) {
408
+ this.mainRequests.set(session, { request, anchor: session.surface?.nodes?.at(-1) ?? null })
409
+ }
410
+
411
+ async save(record) {
412
+ const turns = record.turns.map(({ saveError, ...turn }) => turn)
413
+ await this.sessions.get(record.sessionId).archive.put(threadSchema.parse({ ...record, turns }))
414
+ }
415
+
416
+ read(sessionId, id) {
417
+ parseThreadId(id)
418
+ const record = this.sessions.get(sessionId)?.records.get(id)
419
+ if (!record) throw fault('not-found', '找不到这条侧问。')
420
+ return record
421
+ }
422
+
423
+ list(sessionId) {
424
+ return [...(this.sessions.get(sessionId)?.records.values() ?? [])]
425
+ .map(toSummary)
426
+ .sort((left, right) => right.createdAt - left.createdAt || right.id.localeCompare(left.id))
427
+ }
428
+
429
+ get(sessionId, id) {
430
+ return toDetail(this.read(sessionId, id))
431
+ }
432
+
433
+ async contextWindow(sessionId, id, requestSignal) {
434
+ const signal = requestSignal
435
+ ? AbortSignal.any([requestSignal, this.lifetime.signal])
436
+ : this.lifetime.signal
437
+ signal.throwIfAborted()
438
+ const { provider, model } = this.read(sessionId, id).route
439
+ let capacity
440
+ try {
441
+ const info = await this.llm.resolveModelInfo(provider, model, signal)
442
+ capacity = info.context?.contextWindow
443
+ } catch {
444
+ // A disconnected provider cannot supply the optional context meter.
445
+ }
446
+ signal.throwIfAborted()
447
+ if (!Number.isSafeInteger(capacity) || capacity <= 0) return { contextWindow: null }
448
+ const record = this.read(sessionId, id)
449
+ const price = messages => messages.reduce((sum, message) => sum + estimateMessage(message), 0)
450
+ const snapshot = record.snapshot
451
+ const schemaTokens = estimateToolsTokens({ tools: snapshot.tools })
452
+ const systemTokens = snapshot.system
453
+ ? estimateMessage({ role: 'system', content: [{ type: 'text', text: snapshot.system }] }) : 0
454
+ const overhead = schemaTokens + systemTokens
455
+ const fullEstimate = price(requestMessages(record, true)) + overhead
456
+ const promptFields = ['inputTokens', 'cacheReadTokens', 'cacheWriteTokens']
457
+ const anchor = record.turns.findIndex(({ usage }) => promptFields.some(field => usage?.[field] !== undefined))
458
+ const nativeAdjustment = snapshot.measuredPrefix !== undefined
459
+ ? snapshot.measuredPrefix - (price(snapshot.messages) + overhead) : 0
460
+ let usageAdjustment = 0
461
+ if (anchor !== -1) {
462
+ const reported = promptFields.reduce((sum, field) => sum + (record.turns[anchor].usage[field] ?? 0), 0)
463
+ const promptEstimate = price(requestMessages({ ...record, turns: record.turns.slice(0, anchor + 1) })) + overhead
464
+ usageAdjustment = reported - promptEstimate
465
+ }
466
+ // Fixed native measurements and assistant-context deltas coexist with the
467
+ // first side-request sample. Newer billing reports do not reset the meter.
468
+ return { contextWindow: capacity, estimatedTokens: fullEstimate + Math.max(0, nativeAdjustment, usageAdjustment) }
469
+ }
470
+
471
+ async create(agent, question, signal) {
472
+ parseQuestion(question)
473
+ const sessionId = parseSessionId(agent.session.id)
474
+ if (!await this.prepare(sessionId, signal)) {
475
+ throw fault('session-not-saved', '请先发送一条消息开始侧问')
476
+ }
477
+ const selected = agent.model.current()
478
+ const model = routeSchema.safeParse(selected && {
479
+ provider: selected.provider,
480
+ model: selected.model,
481
+ reasoningEffort: selected.reasoningEffort,
482
+ })
483
+ if (!model.success) throw fault('model-unavailable', '当前会话未配置可用模型。')
484
+ const time = Date.now()
485
+ const { route, snapshot } = freezeSnapshot(agent.session, model.data, this.mainRequests.get(agent.session), this.tokenMeter)
486
+ return this.admit({
487
+ id: `btw-${randomUUID()}`, sessionId, question, createdAt: time, updatedAt: time, version: 1,
488
+ turns: [nextTurn(question, time)], route, snapshot,
489
+ })
490
+ }
491
+
492
+ followup(sessionId, id, question, signal) {
493
+ signal?.throwIfAborted()
494
+ this.lifetime.signal.throwIfAborted()
495
+ parseQuestion(question)
496
+ const record = this.read(sessionId, id)
497
+ if (this.runs.has(id) || !isDone(record)) throw fault('generating', '上一条回复仍在生成。')
498
+ if (newest(record).error === CONTEXT_OVERFLOW) throw fault('context-overflow', '已达上下文上限。')
499
+ const time = Math.max(Date.now(), record.updatedAt)
500
+ return this.admit(nextRevision(record, [...record.turns, nextTurn(question, time)], time), admitted => {
501
+ const { usage, ...turn } = publicTurn(newest(admitted))
502
+ return { id, sessionId, updatedAt: admitted.updatedAt, version: admitted.version, turnIndex: admitted.turns.length - 1, turn }
503
+ })
504
+ }
505
+
506
+ admit(record, result = toDetail) {
507
+ const controller = new AbortController()
508
+ const run = { controller, promise: undefined }
509
+ this.runs.set(record.id, run)
510
+ const admission = this.save(record).then(() => {
511
+ this.publish(record)
512
+ // Start only after durable admission. No caller/transport signal is inherited.
513
+ run.promise = this.generate(record, controller.signal, () => {
514
+ if (this.runs.get(record.id) === run) this.runs.delete(record.id)
515
+ })
516
+ return result(record)
517
+ }).catch(error => {
518
+ this.runs.delete(record.id)
519
+ this.logger?.error('BTW could not admit a question')
520
+ throw fault('save-failed', '保存侧问失败。')
521
+ })
522
+ this.admissions.add(admission)
523
+ admission.then(() => this.admissions.delete(admission), () => this.admissions.delete(admission))
524
+ return admission
525
+ }
526
+
527
+ publish(record, wake = true) {
528
+ this.sessions.get(record.sessionId).records.set(record.id, record)
529
+ if (wake) this.notify(record.id)
530
+ }
531
+
532
+ notify(id) {
533
+ for (const listener of this.listeners.get(id) ?? []) listener()
534
+ }
535
+
536
+ async generate(record, runSignal, releaseRun) {
537
+ const signal = AbortSignal.any([runSignal, this.lifetime.signal])
538
+ const assembler = new BlockAssembler()
539
+ let current = record
540
+ let checkpointVersion = record.version
541
+ let pendingSave
542
+ let previewTimer
543
+ let lastNotified = 0
544
+ let finish
545
+ let failureCode
546
+ let firstTokenMs
547
+ const checkpointTimer = setInterval(() => {
548
+ if (pendingSave || checkpointVersion === current.version || signal.aborted) return
549
+ const checkpoint = current
550
+ checkpointVersion = checkpoint.version
551
+ pendingSave = this.save(checkpoint).catch(() => {
552
+ this.logger?.error('BTW could not save a reply checkpoint')
553
+ }).finally(() => { pendingSave = undefined })
554
+ }, CHECKPOINT_INTERVAL_MS)
555
+ const sendPreview = () => {
556
+ previewTimer = undefined
557
+ lastNotified = Date.now()
558
+ this.notify(record.id)
559
+ }
560
+ const startedAt = performance.now()
561
+ try {
562
+ signal.throwIfAborted()
563
+ for await (const chunk of streamSideQuestion(this.llm, {
564
+ ...record.route,
565
+ ...requestFields(record.snapshot),
566
+ messages: requestMessages(record),
567
+ purpose: 'btw',
568
+ signal,
569
+ }, record)) {
570
+ signal.throwIfAborted()
571
+ if (firstTokenMs === undefined && isTokenDelta(chunk)) {
572
+ firstTokenMs = Math.round(performance.now() - startedAt)
573
+ }
574
+ assembler.push(chunk)
575
+ if (chunk.type === 'finish') {
576
+ finish = chunk.reason.kind
577
+ if (finish === 'error') failureCode = chunk.reason.failure?.code
578
+ break
579
+ }
580
+ if (chunk.type !== 'text-delta' && !(chunk.type === 'block-end' && chunk.block.type === 'text')) continue
581
+ const reply = assembler.interruptedBlocks().filter(block => block.type === 'text').map(block => block.text).join('')
582
+ const time = Date.now()
583
+ current = withLatest(current, { ...newest(current), reply }, time)
584
+ this.publish(current, false)
585
+ if (time - lastNotified >= PREVIEW_INTERVAL_MS) {
586
+ clearTimeout(previewTimer)
587
+ sendPreview()
588
+ } else if (previewTimer === undefined) {
589
+ previewTimer = setTimeout(sendPreview, PREVIEW_INTERVAL_MS - (time - lastNotified))
590
+ }
591
+ }
592
+ } catch (error) {
593
+ // Only the canonical overflow code crosses the wire, never provider details.
594
+ failureCode = error?.code
595
+ }
596
+ clearInterval(checkpointTimer)
597
+ clearTimeout(previewTimer)
598
+ const timing = { requestMs: Math.round(performance.now() - startedAt),
599
+ ...(firstTokenMs !== undefined && { firstTokenMs }) }
600
+ await pendingSave
601
+ const time = Math.max(Date.now(), current.updatedAt)
602
+ const turn = newest(current)
603
+ const interrupted = signal.aborted || finish === 'aborted'
604
+ let error
605
+ if (interrupted) error = INTERRUPTION
606
+ else if (failureCode === CONTEXT_WINDOW_EXCEEDED_CODE) error = CONTEXT_OVERFLOW
607
+ else if (!finish || finish === 'error') error = FAILURE
608
+ else if (!turn.reply.trim()) error = NO_TEXT
609
+ let modelReply
610
+ if (signal.aborted || !error) {
611
+ try { modelReply = modelReplyFor(record, assembler, signal.aborted) } catch { error = FAILURE }
612
+ }
613
+ const usage = readUsage(assembler.usage)
614
+ const terminal = withLatest(current, {
615
+ ...turn,
616
+ status: interrupted ? 'interrupted' : error ? 'failed' : 'completed',
617
+ finishedAt: time,
618
+ ...(error ? { error } : {}),
619
+ ...(modelReply ? { modelReply } : {}),
620
+ ...(usage ? { usage } : {}),
621
+ timing,
622
+ }, time)
623
+ try {
624
+ await this.save(terminal)
625
+ releaseRun()
626
+ this.publish(terminal)
627
+ } catch {
628
+ this.logger?.error('BTW could not save a terminal reply')
629
+ releaseRun()
630
+ this.publish(withLatest(terminal, { ...newest(terminal), saveError: ARCHIVE_ERROR }, time))
631
+ }
632
+ }
633
+
634
+ async *watch(sessionId, id, version, assertAccess, requestSignal) {
635
+ if (version != null) parseVersion(version)
636
+ const signal = requestSignal
637
+ ? AbortSignal.any([requestSignal, this.lifetime.signal])
638
+ : this.lifetime.signal
639
+ while (!signal.aborted) {
640
+ assertAccess()
641
+ const record = this.read(sessionId, id)
642
+ if (record.version !== version) {
643
+ version = record.version
644
+ yield toDetail(record)
645
+ }
646
+ if (isDone(record)) return
647
+ // If a revision arrived while the generator was yielding, reread promptly.
648
+ if (this.read(sessionId, id).version !== version) continue
649
+ try {
650
+ await waitForPulse(this.listeners, id, signal)
651
+ } catch (error) {
652
+ if (signal.aborted) return
653
+ throw error
654
+ }
655
+ }
656
+ }
657
+
658
+ async *watchReply(sessionId, id, turnIndex, version, assertAccess, requestSignal) {
659
+ const signal = requestSignal
660
+ ? AbortSignal.any([requestSignal, this.lifetime.signal])
661
+ : this.lifetime.signal
662
+ let previousText
663
+ while (!signal.aborted) {
664
+ assertAccess()
665
+ const record = this.read(sessionId, id)
666
+ const turn = record.turns[turnIndex]
667
+ if (!turn) throw fault('not-found', '找不到这轮侧问。')
668
+ if (record.version !== version || turn.status !== 'generating') {
669
+ // A changed baseline may have reordered text blocks before we began watching.
670
+ const start = previousText !== undefined && turn.reply.startsWith(previousText) ? previousText.length : 0
671
+ const { status, finishedAt, error, saveError, usage, timing } = turn
672
+ version = record.version
673
+ previousText = turn.reply
674
+ yield {
675
+ version, updatedAt: record.updatedAt, offset: start, text: turn.reply.slice(start), status,
676
+ ...(finishedAt !== undefined && { finishedAt }), ...(error !== undefined && { error }),
677
+ ...(saveError !== undefined && { saveError }), ...(usage !== undefined && { usage }),
678
+ ...(timing !== undefined && { timing }),
679
+ }
680
+ } else if (previousText === undefined) {
681
+ previousText = turn.reply
682
+ }
683
+ if (turn.status !== 'generating') return
684
+ if (this.read(sessionId, id).version !== version) continue
685
+ try {
686
+ await waitForPulse(this.listeners, id, signal)
687
+ } catch (error) {
688
+ if (signal.aborted) return
689
+ throw error
690
+ }
691
+ }
692
+ }
693
+
694
+ async close() {
695
+ this.lifetime.abort()
696
+ await Promise.allSettled([...this.admissions])
697
+ await Promise.allSettled([...this.runs.values()].map(run => run.promise))
698
+ await Promise.allSettled([...this.opening.values()])
699
+ }
700
+ }
701
+
702
+ export default class BtwService extends TypertRemoteService {
703
+ static inject = inject
704
+
705
+ constructor(ctx) {
706
+ super(ctx, 'btw')
707
+ }
708
+
709
+ async [Service.init]() {
710
+ const store = sessionThreadStore(this.ctx.sessionPersistence, record => threadSchema.parse(record))
711
+ const host = new BtwHost({ store, llm: this.ctx.llm, logger: this.ctx.logger })
712
+ this.ctx.effect(() => () => host.close(), 'btw.close')
713
+ this.ctx.on('llm/stream', (request, next) => {
714
+ if (isAgentLoopRequest(request) && request.sessionId) {
715
+ const session = this.ctx.agents.get(request.sessionId)?.session
716
+ if (session) host.observeMain(session, request)
717
+ }
718
+ return next()
719
+ })
720
+ this.ctx.inject(['tokenMeter'], scope => {
721
+ host.tokenMeter = scope.tokenMeter
722
+ scope.effect(() => () => { host.tokenMeter = undefined }, 'btw.tokenMeter')
723
+ })
724
+ this.host = host
725
+ // The loader snapshots ./typert once per process. Register this hand-written
726
+ // contribution with the live plugin so an installed update exposes it immediately.
727
+ this.ctx.typert.register({
728
+ package: `${PACKAGE_NAME}-meter`, face: 'host', schemas: [],
729
+ model: { services: [], events: [], objects: [] }, invocations: [contextDescriptor],
730
+ })
731
+ }
732
+
733
+ sessionFor(agent) {
734
+ const peer = this.ctx.invocation?.peer
735
+ if (peer?.principal.kind !== 'owner' || peer.signal.aborted) throw fault('denied', '无权访问侧问。')
736
+ const sessionId = parseSessionId(agent?.session?.id)
737
+ if (this.ctx.agents.get(sessionId) !== agent) throw fault('denied', '当前会话不可用。')
738
+ return sessionId
739
+ }
740
+
741
+ async prepare(agent, signal) {
742
+ const sessionId = this.sessionFor(agent)
743
+ const available = await this.host.prepare(sessionId, signal)
744
+ this.sessionFor(agent)
745
+ return { sessionId, available }
746
+ }
747
+
748
+ async create(agent, question, signal) {
749
+ const { available } = await this.prepare(agent, signal)
750
+ if (!available) throw fault('session-not-saved', '请先发送一条消息开始侧问')
751
+ return this.host.create(agent, question, signal)
752
+ }
753
+
754
+ async list(agent, signal) {
755
+ const { sessionId } = await this.prepare(agent, signal)
756
+ return this.host.list(sessionId)
757
+ }
758
+
759
+ async get(agent, id, signal) {
760
+ const { sessionId } = await this.prepare(agent, signal)
761
+ return this.host.get(sessionId, id)
762
+ }
763
+
764
+ async contextCapacity(agent, id, signal) {
765
+ const { sessionId } = await this.prepare(agent, signal)
766
+ return this.host.contextWindow(sessionId, id, signal)
767
+ }
768
+
769
+ async followup(agent, id, question, signal) {
770
+ const { sessionId } = await this.prepare(agent, signal)
771
+ return this.host.followup(sessionId, id, question, signal)
772
+ }
773
+
774
+ async *watch(agent, id, version, signal) {
775
+ const { sessionId } = await this.prepare(agent, signal)
776
+ yield* this.host.watch(sessionId, id, version, () => this.sessionFor(agent), signal)
777
+ }
778
+
779
+ async *watchReply(agent, id, turnIndex, version, signal) {
780
+ const { sessionId } = await this.prepare(agent, signal)
781
+ yield* this.host.watchReply(sessionId, id, turnIndex, version, () => this.sessionFor(agent), signal)
782
+ }
783
+ }