A side-chat fork physically duplicated the parent's entire event log into the child session (measured: a 32,674-event child on this deployment, 303k duplicated rows across 36 seeded threads). After a summarizing compaction the checkpoint user/message already replaces the shadowed span on the model-visible surface, so the child only needs the surviving pre-cut surface nodes, the checkpoint, and the post-compaction tail. The rebased sequence is verified with the real fold (foldSurface) before use: identical visible nodes to the parent, hence identical model context and provider context-cache prefix. Any tail that cannot be proven equivalent (dangling back-references, reordering replaces, no compaction) falls back to today's verbatim full copy.
1064 lines
45 KiB
TypeScript
1064 lines
45 KiB
TypeScript
/**
|
|
* Pure side-conversation ("Side Chat") logic shared by the host routes and
|
|
* the client tab. Framework-free (no React, no Node) so both halves and the
|
|
* node test environment can import it.
|
|
*
|
|
* A side thread is a child session the plugin creates ITSELF with a custom
|
|
* seed — the parent session's FULL event log up to the click moment
|
|
* (completed turns, the unanswered user message, and — when the parent is
|
|
* mid-turn — the in-progress assistant output and tool activity). The log
|
|
* model forbids open-turn seeds, so an in-flight parent turn is copied
|
|
* verbatim and CLOSED with synthetic `step/end` + `turn/end{reason:
|
|
* 'interrupted'}` events: the child sees the partial turn as honestly
|
|
* frozen ("cut off"), never as a completed answer. The one case that cannot
|
|
* be closed honestly — a tool call still executing (no `tool/result` yet;
|
|
* providers reject dangling assistant calls) — falls back to cutting before
|
|
* the open turn and carrying the partial content as a structured text
|
|
* snapshot inside the boundary prompt.
|
|
*/
|
|
import type { SidebarHistoryEntry, SidebarSessionSummary } from './context-types.ts'
|
|
import type { AssistantLiveChunk } from './assistant-live.ts'
|
|
import { foldSurface } from '@deepseek-ai/dsh-session/surface'
|
|
|
|
/** The durable thread-label prefix (also the row filter in the client list). */
|
|
export const SIDE_LABEL_PREFIX = 'Side: '
|
|
|
|
/** The pinned label of a freshly created thread that no prompt has reached
|
|
* yet (Codex-style immediate create: the tab opens an EMPTY thread, the
|
|
* first composer message carries the boundary and earns the real label).
|
|
* The client renders it localized; the prefix keeps the row filter honest. */
|
|
export const SIDE_NEW_THREAD_TITLE = 'Side: New thread'
|
|
|
|
/** Maximum code points kept in a durable thread label (matches subagent labels). */
|
|
export const LABEL_MAX_CHARS = 48
|
|
|
|
/** The boundary message's opening line — the transcript mapping drops user
|
|
* rows starting with it (same first line as dsh-sidechain's boundary, so
|
|
* the two plugins' threads render consistently in either UI). */
|
|
export const SIDE_BOUNDARY_PREFIX = 'Side conversation boundary'
|
|
|
|
/** The plugin's producer-owned source kind, stamped on the source of
|
|
* context-injection messages (boundary prompt + parked snapshot) so the
|
|
* transcript recognizes them structurally — not by text prefix. Session
|
|
* format v4 retired the bare `kind: 'plugin'` + `plugin` pair; a plugin is
|
|
* now identified by its own `plugin:<name>` kind, which is exactly what
|
|
* DSH's own v3→v4 migration derives for rows this plugin wrote earlier, so
|
|
* both generations read back under one shape. */
|
|
export const SIDE_INJECTION_SOURCE_KIND = 'plugin:dsh-better-sidebar'
|
|
|
|
/**
|
|
* The boundary prompt delivered as the thread's first user message: the
|
|
* inherited seed is reference context only, never active instruction.
|
|
* Model-facing contract — change only with intent, tests pin the sentences.
|
|
*/
|
|
export const SIDE_BOUNDARY_PROMPT = `Side conversation boundary.
|
|
|
|
Everything before this boundary is inherited history from the parent session: its completed turns, its pending question, and — if the parent was mid-turn — its in-progress output frozen at the moment this side conversation started. It is reference context only. It is not your current task.
|
|
|
|
Do not continue, execute, or complete any instructions, plans, tool calls, approvals, edits, or requests from before this boundary. Only messages submitted after this boundary are active user instructions for this side conversation.
|
|
|
|
Mode: this is a continuable side conversation. Your answers stay in this side thread and are viewed in the side panel; they are never delivered into the parent session.`
|
|
|
|
/** One seed event (structural mirror of the durable SessionEvent). The
|
|
* envelope fields are preserved verbatim: surface-eligible events
|
|
* (user/message, assistant/message, tool/result) REQUIRE the `surfaceOp`
|
|
* marker (and may carry `sourceEventSeqs`) — the seed validator rejects
|
|
* them without it. */
|
|
export interface SeedEvent {
|
|
type: string
|
|
seq: number
|
|
time: number
|
|
data: Record<string, unknown>
|
|
/** Surface marker of message-producing events ('append' | replace op). */
|
|
surfaceOp?: unknown
|
|
/** Seq numbers of earlier events this event cites as sources. */
|
|
sourceEventSeqs?: unknown
|
|
/** Reader-skip marker of purely informational events. */
|
|
ignorable?: true
|
|
}
|
|
|
|
/** The minimal structural face of a session-log event this module reads
|
|
* (loose enough to accept both the host's real SessionEvent and the
|
|
* client's SidebarSessionEvent mirror). */
|
|
export interface SidechatLogEvent {
|
|
type: string
|
|
seq: number
|
|
time: number
|
|
data: unknown
|
|
}
|
|
|
|
/**
|
|
* One live assistant delta on the plugin's wire, mirroring DSH 0.1.5's
|
|
* client-only `assistant/live-chunk` presentation row.
|
|
*
|
|
* DSH 0.1.5 no longer logs `assistant/chunk`: an in-flight attempt's deltas
|
|
* are process-local frames (`agent/assistant-stream`, folded by
|
|
* {@link ./assistant-live.ts}) and reach the transcript through the plugin's
|
|
* own `sidechat.events` route as these rows. They are NOT durable — the
|
|
* route returns the current attempt's rows on every poll and the client
|
|
* replaces its live set each time; the durable `assistant/message` settles
|
|
* them by `turn:step`.
|
|
*/
|
|
export interface SidechatLiveEvent {
|
|
type: 'assistant/live-chunk'
|
|
/** Ordering key among live rows only; durable seqs stay authoritative. */
|
|
seq: number
|
|
time: number
|
|
data: {
|
|
attemptId: string
|
|
turn: number
|
|
step: number
|
|
/** Dense zero-based position within the attempt. */
|
|
index: number
|
|
/** The raw model stream chunk. */
|
|
chunk: Record<string, unknown>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Project buffered live chunks into wire rows.
|
|
* @param chunks - the session's active-attempt chunks, in index order.
|
|
* @param tailSeq - the session's last durable seq (live rows order after it).
|
|
* @returns the rows to append to the transcript feed.
|
|
*/
|
|
export function liveEventsOf(chunks: readonly AssistantLiveChunk[], tailSeq: number): SidechatLiveEvent[] {
|
|
return chunks.map((delta, position) => ({
|
|
type: 'assistant/live-chunk',
|
|
seq: tailSeq + 1 + position,
|
|
time: delta.time,
|
|
data: {
|
|
attemptId: delta.attemptId,
|
|
turn: delta.turn,
|
|
step: delta.step,
|
|
index: delta.index,
|
|
chunk: delta.chunk,
|
|
},
|
|
}))
|
|
}
|
|
|
|
/** The result of cutting a parent log into a side-thread inheritance. */
|
|
export interface SidechatInheritance {
|
|
/** The child seed: contiguous from seq 0, ends outside any open turn. */
|
|
seed: SeedEvent[]
|
|
/**
|
|
* Structured snapshot of the parent's in-progress turn when it could NOT
|
|
* be included as events (a tool call was still executing); null when the
|
|
* seed already carries the whole picture.
|
|
*/
|
|
snapshot: string | null
|
|
}
|
|
|
|
/** The data record of one event (narrowed from the loose face). */
|
|
function dataOf(event: SidechatLogEvent): Record<string, unknown> {
|
|
return event.data as Record<string, unknown>
|
|
}
|
|
|
|
/** Copy parent events verbatim (their live seq === array index contract).
|
|
* The FULL envelope is preserved — stripping `surfaceOp` would make the
|
|
* seed validator reject every surface-eligible message event. */
|
|
function copyEvents(events: readonly SidechatLogEvent[]): SeedEvent[] {
|
|
return events.map(event => {
|
|
const source = event as SidechatLogEvent & {
|
|
surfaceOp?: unknown
|
|
sourceEventSeqs?: unknown
|
|
ignorable?: true
|
|
}
|
|
return {
|
|
type: source.type,
|
|
seq: source.seq,
|
|
time: source.time,
|
|
data: dataOf(source),
|
|
...(source.surfaceOp === undefined ? {} : { surfaceOp: source.surfaceOp }),
|
|
...(source.sourceEventSeqs === undefined ? {} : { sourceEventSeqs: source.sourceEventSeqs }),
|
|
...(source.ignorable === undefined ? {} : { ignorable: source.ignorable }),
|
|
}
|
|
})
|
|
}
|
|
|
|
/** Index of the last `turn/start` or `turn/end`, or -1. */
|
|
function lastTurnBoundary(events: readonly SidechatLogEvent[]): number {
|
|
for (let index = events.length - 1; index >= 0; index--) {
|
|
const type = events[index]?.type
|
|
if (type === 'turn/start' || type === 'turn/end') return index
|
|
}
|
|
return -1
|
|
}
|
|
|
|
/** Numeric field of an event's data (turn / step numbers). */
|
|
function numberAt(data: Record<string, unknown>, key: string): number {
|
|
const value = data[key]
|
|
return typeof value === 'number' && Number.isSafeInteger(value) ? value : 0
|
|
}
|
|
|
|
/** The step number still open at the log tail inside the turn starting at
|
|
* `turnStart` (undefined when no step is open). */
|
|
function openStepInTurn(events: readonly SidechatLogEvent[], turnStart: number): number | undefined {
|
|
let open: number | undefined
|
|
for (let index = turnStart + 1; index < events.length; index++) {
|
|
const event = events[index]
|
|
if (event === undefined) continue
|
|
if (event.type === 'step/start') open = numberAt(dataOf(event), 'step')
|
|
else if (event.type === 'step/end') open = undefined
|
|
}
|
|
return open
|
|
}
|
|
|
|
/**
|
|
* Whether the open turn ending the log has a `tool/call` without its paired
|
|
* `tool/result` in the CURRENT open step. Providers reject dangling
|
|
* assistant calls, so such a turn cannot be honestly closed and the
|
|
* inheritance must fall back to the snapshot.
|
|
*/
|
|
export function hasDanglingToolCall(events: readonly SidechatLogEvent[], turnStart: number): boolean {
|
|
const pending = new Set<string>()
|
|
for (let index = turnStart + 1; index < events.length; index++) {
|
|
const event = events[index]
|
|
if (event === undefined) continue
|
|
const data = dataOf(event)
|
|
if (event.type === 'step/end') {
|
|
pending.clear()
|
|
continue
|
|
}
|
|
if (event.type === 'tool/call') {
|
|
const callId = data.callId
|
|
if (typeof callId === 'string') pending.add(callId)
|
|
continue
|
|
}
|
|
if (event.type === 'tool/result') {
|
|
const source = data.message as { source?: { callId?: unknown } } | undefined
|
|
const callId = source?.source?.callId
|
|
if (typeof callId === 'string') pending.delete(callId)
|
|
}
|
|
}
|
|
return pending.size > 0
|
|
}
|
|
|
|
/**
|
|
* The result content blocks of one tool/result message under BOTH logged
|
|
* shapes: 0.1.6 wrapped them in a single `type: 'tool-result'` content block
|
|
* on a user-role message, 0.1.7's first-class tool-role message carries them
|
|
* at the message's own top level. Historical logs keep the old shape forever,
|
|
* so both are read. Undefined when the message carries no block array.
|
|
*/
|
|
function resultBlocks(content: unknown): readonly unknown[] | undefined {
|
|
if (!Array.isArray(content)) return undefined
|
|
for (const block of content) {
|
|
if (block === null || typeof block !== 'object') continue
|
|
const wrapper = block as { type?: unknown; content?: unknown }
|
|
if (wrapper.type === 'tool-result' && Array.isArray(wrapper.content)) {
|
|
return wrapper.content as readonly unknown[]
|
|
}
|
|
}
|
|
return content as readonly unknown[]
|
|
}
|
|
|
|
/** The plain text of one tool/result message (its text blocks, under either
|
|
* of the two shapes {@link resultBlocks} reads). */
|
|
function toolResultText(data: Record<string, unknown>): string {
|
|
const message = data.message as { content?: unknown } | undefined
|
|
const blocks = resultBlocks(message?.content)
|
|
if (blocks === undefined) return ''
|
|
const parts: string[] = []
|
|
for (const item of blocks) {
|
|
if (item === null || typeof item !== 'object') continue
|
|
const textItem = item as { type?: unknown; text?: unknown }
|
|
if (textItem.type === 'text' && typeof textItem.text === 'string') {
|
|
parts.push(textItem.text)
|
|
}
|
|
}
|
|
return parts.join('\n')
|
|
}
|
|
|
|
/** Cap applied to one tool-result's text inside a snapshot (prompt budget). */
|
|
const SNAPSHOT_RESULT_CAP = 2000
|
|
/** Cap applied to the whole snapshot (prompt budget). */
|
|
const SNAPSHOT_TOTAL_CAP = 8000
|
|
|
|
/**
|
|
* Rebase the parent's log onto its last summarizing compaction: the
|
|
* checkpoint `user/message` already REPLACES the shadowed span on the
|
|
* model-visible surface, so a child needs only the surviving pre-cut surface
|
|
* nodes, the checkpoint itself, and the post-compaction tail — the shadowed
|
|
* bulk never has to be physically copied again. The result is verified with
|
|
* the real fold before it is used: the rebased sequence must produce the
|
|
* SAME visible nodes as the parent, which keeps the child's model context
|
|
* (and provider context-cache prefix) identical to a full-log fork.
|
|
*
|
|
* Returns a seq-contiguous event array, or undefined to fall back to the
|
|
* verbatim full copy — no compaction in the log, a tail whose back
|
|
* references cannot be rebased, or any fold the construction cannot prove
|
|
* equivalent. Failures degrade to today's behavior, never to a wrong child.
|
|
* @param events - the parent's log (live or persisted).
|
|
* @returns the rebased log, or undefined when the full copy is required.
|
|
*/
|
|
function compactionTail(events: readonly SidechatLogEvent[]): SidechatLogEvent[] | undefined {
|
|
const cut = lastCheckpointIndex(events)
|
|
if (cut === undefined) return undefined
|
|
const checkpoint = events[cut]
|
|
if (checkpoint === undefined) return undefined
|
|
let head: SidechatLogEvent[]
|
|
let parentNodes: readonly number[]
|
|
try {
|
|
const fold = foldSurface(events as unknown as Parameters<typeof foldSurface>[0])
|
|
parentNodes = fold.nodes
|
|
const surfaceIndex = parentNodes.indexOf(checkpoint.seq)
|
|
// The checkpoint must sit on the surface, and every node before its
|
|
// surface slot must predate the cut (a post-cut node reordered ahead of
|
|
// the checkpoint would need the very shadowing we are removing).
|
|
if (surfaceIndex === -1) return undefined
|
|
const before: SidechatLogEvent[] = []
|
|
const after: SidechatLogEvent[] = []
|
|
let sawTailNode = false
|
|
for (let index = 0; index < parentNodes.length; index++) {
|
|
if (index === surfaceIndex) continue
|
|
const node = events[parentNodes[index] as number]
|
|
if (node === undefined || node.seq >= cut) {
|
|
if (index > surfaceIndex) {
|
|
sawTailNode = true
|
|
continue
|
|
}
|
|
return undefined
|
|
}
|
|
// Surface order is [pre-cut before the checkpoint's slot][checkpoint]
|
|
// [pre-cut after its slot][tail in log order]; anything else cannot be
|
|
// reproduced by appending and degrades to the full copy.
|
|
if (sawTailNode) return undefined
|
|
if (index < surfaceIndex) before.push(node)
|
|
else after.push(node)
|
|
}
|
|
head = [...before, checkpoint, ...after]
|
|
} catch {
|
|
return undefined
|
|
}
|
|
// Surviving pre-cut tool/results are surface nodes; their PAIRED calls
|
|
// are log-only events the fold never kept. Emit each call right before
|
|
// its result so the child's pairs stay complete.
|
|
const headWithCalls: SidechatLogEvent[] = []
|
|
for (const event of head) {
|
|
const callId = toolResultCallId(event)
|
|
if (callId !== undefined) {
|
|
const call = findPairedCall(events, cut, callId)
|
|
if (call !== undefined) headWithCalls.push(call)
|
|
}
|
|
headWithCalls.push(event)
|
|
}
|
|
// Positional renumbering: head (with calls) keeps surface order, the
|
|
// post-checkpoint tail keeps log order; both map old seqs onto 0..n-1.
|
|
// Head events are visible nodes re-emitted as plain appends — any replace
|
|
// they carried is already baked into WHICH nodes survive (the surfaceOp
|
|
// marker itself is required by the seed validator, while stale citations
|
|
// into the dropped head must not survive).
|
|
const rebase = new Map<number, number>()
|
|
const seed: SeedEvent[] = []
|
|
for (const event of headWithCalls) {
|
|
rebase.set(event.seq, seed.length)
|
|
seed.push(
|
|
event.type === 'tool/call'
|
|
? { type: event.type, seq: seed.length, time: event.time, data: dataOf(event) }
|
|
: { type: event.type, seq: seed.length, time: event.time, data: dataOf(event), surfaceOp: 'append' },
|
|
)
|
|
}
|
|
for (const event of events.slice(cut + 1)) {
|
|
const rebased = rebaseTailEvent(event, seed.length, rebase)
|
|
if (rebased === undefined) return undefined
|
|
rebase.set(event.seq, seed.length)
|
|
seed.push(rebased)
|
|
}
|
|
// Prove equivalence before the caller relies on it: the rebased sequence
|
|
// must fold without error and to the same visible nodes as the parent.
|
|
try {
|
|
const child = foldSurface(seed as unknown as Parameters<typeof foldSurface>[0])
|
|
if (!sameVisibleSequence(events, parentNodes, child.nodes, seed)) return undefined
|
|
} catch {
|
|
return undefined
|
|
}
|
|
return seed
|
|
}
|
|
|
|
/** Index of the checkpoint user/message of the last summarizing compaction:
|
|
* the event right after the last `compaction/summary` — that adjacency is
|
|
* the shadow-price contract. Undefined without a matching shape. */
|
|
function lastCheckpointIndex(events: readonly SidechatLogEvent[]): number | undefined {
|
|
for (let index = events.length - 1; index >= 0; index--) {
|
|
if (events[index]?.type !== 'compaction/summary') continue
|
|
return events[index + 1]?.type === 'user/message' ? index + 1 : undefined
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
/** The callId a tool/result event cites, when its message carries one. */
|
|
function toolResultCallId(event: SidechatLogEvent): string | undefined {
|
|
if (event.type !== 'tool/result') return undefined
|
|
const message = dataOf(event).message as { source?: { callId?: unknown } } | undefined
|
|
const callId = message?.source?.callId
|
|
return typeof callId === 'string' && callId !== '' ? callId : undefined
|
|
}
|
|
|
|
/** The last log-only tool/call with this callId before the cut, when present. */
|
|
function findPairedCall(events: readonly SidechatLogEvent[], cut: number, callId: string): SidechatLogEvent | undefined {
|
|
for (let index = cut - 1; index >= 0; index--) {
|
|
const event = events[index]
|
|
if (event?.type === 'tool/call' && dataOf(event).callId === callId) return event
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
/** One `type` + message-content token per visible node, for comparing folds.
|
|
* `headerSeq` is envelope metadata the rebase rewrites, not content. */
|
|
function visibleToken(event: SidechatLogEvent | undefined): string {
|
|
if (event === undefined) return 'missing'
|
|
const data = { ...dataOf(event) }
|
|
delete data.headerSeq
|
|
return `${event.type}:${JSON.stringify(data)}`
|
|
}
|
|
|
|
/** Compare the parent's visible nodes with the rebased child's by content. */
|
|
function sameVisibleSequence(
|
|
parent: readonly SidechatLogEvent[],
|
|
parentNodes: readonly number[],
|
|
childNodes: readonly number[],
|
|
child: readonly SidechatLogEvent[],
|
|
): boolean {
|
|
if (parentNodes.length !== childNodes.length) return false
|
|
for (let index = 0; index < parentNodes.length; index++) {
|
|
if (visibleToken(parent[parentNodes[index] as number]) !== visibleToken(child[childNodes[index] as number])) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* Copy one post-checkpoint tail event into the rebased sequence: envelope
|
|
* seq references are remapped through the rebase map, and a reference the
|
|
* map cannot serve (it points into the dropped head) degrades the event
|
|
* honestly — a replace whose shadowed nodes are gone becomes an append,
|
|
* dropped citations vanish — except `developer/message.headerSeq`, whose
|
|
* referent cannot be degraded: that aborts the whole tail.
|
|
* @param event - the tail event to copy.
|
|
* @param seq - the event's position in the rebased sequence.
|
|
* @param rebase - old seq → rebased position over the kept set.
|
|
* @returns the rebased event, or undefined when the tail is not provable.
|
|
*/
|
|
function rebaseTailEvent(event: SidechatLogEvent, seq: number, rebase: Map<number, number>): SeedEvent | undefined {
|
|
const source = event as SidechatLogEvent & { ignorable?: true }
|
|
const seed: SeedEvent = { type: source.type, seq, time: source.time, data: dataOf(source) }
|
|
if (source.ignorable === true) seed.ignorable = true
|
|
const sourceData = dataOf(event)
|
|
if (event.type === 'developer/message' && typeof sourceData.headerSeq === 'number') {
|
|
const mapped = rebase.get(sourceData.headerSeq)
|
|
if (mapped === undefined) return undefined
|
|
seed.data = { ...seed.data, headerSeq: mapped }
|
|
}
|
|
const op = (event as { surfaceOp?: unknown }).surfaceOp
|
|
if (op === 'append') {
|
|
seed.surfaceOp = 'append'
|
|
} else if (op !== null && typeof op === 'object' && 'op' in op && (op as { op: unknown }).op === 'replace') {
|
|
const replace = op as { startSeq?: unknown; endSeq?: unknown }
|
|
const start = typeof replace.startSeq === 'number' ? rebase.get(replace.startSeq) : undefined
|
|
const end = typeof replace.endSeq === 'number' ? rebase.get(replace.endSeq) : undefined
|
|
// A range the rebase cannot resolve lost its shadowed nodes with the
|
|
// head; appending the replacement is exactly what remains visible.
|
|
seed.surfaceOp = start !== undefined && end !== undefined && start <= end
|
|
? { op: 'replace', startSeq: start, endSeq: end }
|
|
: 'append'
|
|
}
|
|
const cited = (event as { sourceEventSeqs?: unknown }).sourceEventSeqs
|
|
if (Array.isArray(cited)) {
|
|
const mapped = cited.map(item => typeof item === 'number' ? rebase.get(item) : undefined)
|
|
// Citations the rebase cannot serve referenced the dropped head; the
|
|
// field is optional and only has to cover a live replace range.
|
|
if (!mapped.includes(undefined)) seed.sourceEventSeqs = mapped as number[]
|
|
}
|
|
return seed
|
|
}
|
|
|
|
/**
|
|
* Build the side-thread inheritance for one parent log: the full event log
|
|
* up to the click moment, honestly closed when it ends inside an open turn.
|
|
* When the parent has compacted, the log is first rebased onto its last
|
|
* summarizing compaction (`compactionTail`) — same model-visible surface, a
|
|
* fraction of the durable copy.
|
|
* @param events - the parent's log (live or persisted).
|
|
* @param live - the parent's in-flight stream chunks (DSH 0.1.5+ publishes
|
|
* them outside the log); used only by the snapshot fallback.
|
|
*/
|
|
export function buildSidechatInheritance(
|
|
events: readonly SidechatLogEvent[],
|
|
live: readonly AssistantLiveChunk[] = [],
|
|
): SidechatInheritance {
|
|
if (events.length === 0) return { seed: [], snapshot: null }
|
|
const trimmed = compactionTail(events)
|
|
const log = trimmed ?? events
|
|
const boundary = lastTurnBoundary(log)
|
|
if (boundary < 0 || log[boundary]?.type === 'turn/end') {
|
|
// Ends outside any turn (or has no turns at all): the whole log is a
|
|
// valid, balanced seed — possibly ending with a pending user message.
|
|
return { seed: copyEvents(log), snapshot: null }
|
|
}
|
|
// Ends inside the open turn starting at `boundary`.
|
|
if (hasDanglingToolCall(log, boundary)) {
|
|
// Cannot close honestly: cut before the open turn; the caller attaches
|
|
// the structured snapshot to the boundary prompt instead.
|
|
return {
|
|
seed: copyEvents(log.slice(0, boundary)),
|
|
snapshot: buildOpenTurnSnapshot(log, live),
|
|
}
|
|
}
|
|
const seed = copyEvents(log)
|
|
const last = log[log.length - 1]
|
|
const turn = numberAt(dataOf(log[boundary]!), 'turn')
|
|
const now = last?.time ?? 0
|
|
const openStep = openStepInTurn(log, boundary)
|
|
if (openStep !== undefined) {
|
|
seed.push({ type: 'step/end', seq: seed.length, time: now, data: { turn, step: openStep } })
|
|
}
|
|
seed.push({
|
|
type: 'turn/end',
|
|
seq: seed.length,
|
|
time: now,
|
|
data: { turn, reason: { kind: 'interrupted' } },
|
|
})
|
|
return { seed, snapshot: null }
|
|
}
|
|
|
|
/** One assistant content block reduced to the text the snapshot shows. */
|
|
function messageTexts(message: unknown): { text: string; reasoning: string } {
|
|
const content = (message as { content?: unknown } | undefined)?.content
|
|
let text = ''
|
|
let reasoning = ''
|
|
if (!Array.isArray(content)) return { text, reasoning }
|
|
for (const block of content) {
|
|
if (block === null || typeof block !== 'object') continue
|
|
const candidate = block as { type?: unknown; text?: unknown }
|
|
if (typeof candidate.text !== 'string' || candidate.text === '') continue
|
|
if (candidate.type === 'text') text += candidate.text
|
|
else if (candidate.type === 'reasoning') reasoning += candidate.text
|
|
}
|
|
return { text, reasoning }
|
|
}
|
|
|
|
/**
|
|
* Expand one attempt's compact `AssistantStreamRecord[]` (the durable stream
|
|
* DSH 0.1.5 embeds in `assistant/message.stream` and `assistant/attempt.stream`)
|
|
* into the text/reasoning it carried. Tool-call records contribute no text.
|
|
*/
|
|
function streamTexts(stream: unknown): { text: string; reasoning: string } {
|
|
let text = ''
|
|
let reasoning = ''
|
|
if (!Array.isArray(stream)) return { text, reasoning }
|
|
for (const record of stream) {
|
|
if (record === null || typeof record !== 'object') continue
|
|
const entry = record as { type?: unknown; texts?: unknown; chunk?: unknown }
|
|
if (entry.type === 'text-chunks' || entry.type === 'reasoning-chunks') {
|
|
if (!Array.isArray(entry.texts)) continue
|
|
const joined = entry.texts.filter((part): part is string => typeof part === 'string').join('')
|
|
if (entry.type === 'text-chunks') text += joined
|
|
else reasoning += joined
|
|
continue
|
|
}
|
|
if (entry.type === 'chunk') {
|
|
const chunk = entry.chunk as { type?: unknown; text?: unknown } | undefined
|
|
if (chunk === null || typeof chunk !== 'object' || typeof chunk.text !== 'string') continue
|
|
if (chunk.type === 'text-delta') text += chunk.text
|
|
else if (chunk.type === 'reasoning-delta') reasoning += chunk.text
|
|
}
|
|
}
|
|
return { text, reasoning }
|
|
}
|
|
|
|
/** One live delta's contribution to the snapshot. */
|
|
function liveTexts(chunk: Record<string, unknown>): { text: string; reasoning: string } {
|
|
if (typeof chunk.text !== 'string' || chunk.text === '') return { text: '', reasoning: '' }
|
|
if (chunk.type === 'text-delta') return { text: chunk.text, reasoning: '' }
|
|
if (chunk.type === 'reasoning-delta') return { text: '', reasoning: chunk.text }
|
|
return { text: '', reasoning: '' }
|
|
}
|
|
|
|
/**
|
|
* Structured text snapshot of the parent's OPEN turn (from its `turn/start`
|
|
* to the log tail): the assistant/reasoning output so far and the tool
|
|
* activity — executed tools with their result text, the still-executing one
|
|
* marked. Returns null when there is no open turn or nothing to show.
|
|
*
|
|
* The in-flight step's text is NOT in the log on DSH 0.1.5 (the model stream
|
|
* is process-local until it settles), so it comes from `live`; settled steps
|
|
* read their durable `assistant/message` content, and a failed attempt reads
|
|
* its embedded `assistant/attempt.stream`.
|
|
* @param events - the parent's log.
|
|
* @param live - the parent's in-flight stream chunks, in index order.
|
|
*/
|
|
export function buildOpenTurnSnapshot(
|
|
events: readonly SidechatLogEvent[],
|
|
live: readonly AssistantLiveChunk[] = [],
|
|
): string | null {
|
|
const boundary = lastTurnBoundary(events)
|
|
if (boundary < 0 || events[boundary]?.type !== 'turn/start') return null
|
|
const openTurn = numberAt(dataOf(events[boundary]!), 'turn')
|
|
let text = ''
|
|
let reasoning = ''
|
|
const tools: string[] = []
|
|
const pendingCalls = new Map<string, { name: string; args: string }>()
|
|
for (let index = boundary + 1; index < events.length; index++) {
|
|
const event = events[index]
|
|
if (event === undefined) continue
|
|
const data = dataOf(event)
|
|
if (event.type === 'step/end') {
|
|
pendingCalls.clear()
|
|
continue
|
|
}
|
|
if (event.type === 'assistant/message') {
|
|
const settled = messageTexts(data.message)
|
|
text += settled.text
|
|
reasoning += settled.reasoning
|
|
continue
|
|
}
|
|
if (event.type === 'assistant/attempt') {
|
|
const attempt = streamTexts(data.stream)
|
|
text += attempt.text
|
|
reasoning += attempt.reasoning
|
|
continue
|
|
}
|
|
if (event.type === 'tool/call') {
|
|
const callId = data.callId
|
|
if (typeof callId === 'string') {
|
|
pendingCalls.set(callId, {
|
|
name: typeof data.name === 'string' ? data.name : 'tool',
|
|
args: typeof data.arguments === 'string' ? data.arguments : '',
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
if (event.type === 'tool/result') {
|
|
const source = data.message as { source?: { callId?: unknown } } | undefined
|
|
const callId = typeof source?.source?.callId === 'string' ? source.source.callId : undefined
|
|
const name = callId !== undefined ? pendingCalls.get(callId)?.name : undefined
|
|
const args = callId !== undefined ? pendingCalls.get(callId)?.args : undefined
|
|
if (callId !== undefined) pendingCalls.delete(callId)
|
|
const result = toolResultText(data).slice(0, SNAPSHOT_RESULT_CAP)
|
|
const failed = data.error !== undefined
|
|
const line = [
|
|
`- \`${name ?? 'tool'}\`${failed ? ' (failed)' : ''}`
|
|
+ (args !== undefined && args !== '' ? ` — arguments: \`${args}\`` : ''),
|
|
...(result === '' ? [] : [` Result: ${result}`]),
|
|
].join('\n')
|
|
tools.push(line)
|
|
}
|
|
}
|
|
for (const [, call] of pendingCalls) {
|
|
const line = `- \`${call.name}\` (executing) — arguments: \`${call.args}\``
|
|
tools.push(line)
|
|
}
|
|
// The in-flight step: its deltas never reached the log.
|
|
for (const delta of live) {
|
|
if (delta.turn !== openTurn) continue
|
|
const contribution = liveTexts(delta.chunk)
|
|
text += contribution.text
|
|
reasoning += contribution.reasoning
|
|
}
|
|
const sections: string[] = []
|
|
if (text.trim() !== '') sections.push(`Assistant output so far:\n\n${text}`)
|
|
if (reasoning.trim() !== '') sections.push(`Reasoning so far:\n\n${reasoning}`)
|
|
if (tools.length > 0) sections.push(`Tool activity:\n${tools.join('\n')}`)
|
|
if (sections.length === 0) return null
|
|
const body = sections.join('\n\n')
|
|
return body.length > SNAPSHOT_TOTAL_CAP
|
|
? `Parent session in-progress turn (reference only):\n\n${body.slice(0, SNAPSHOT_TOTAL_CAP)}…`
|
|
: `Parent session in-progress turn (reference only):\n\n${body}`
|
|
}
|
|
|
|
/** One side-thread row in the client's thread list. */
|
|
export interface SideThreadRow {
|
|
id: string
|
|
/** The durable thread title ('Side: …'). */
|
|
title: string
|
|
/** Whether the thread's agent is currently running. */
|
|
running: boolean
|
|
}
|
|
|
|
/**
|
|
* Derive the side threads of one parent session from the client session list:
|
|
* durable `origin: 'subagent'` children of the parent whose pinned title
|
|
* carries the thread label prefix (our creation path pins it via
|
|
* sessionTitle.rename; dsh-sidechain threads share the convention, so they
|
|
* are visible here too).
|
|
*/
|
|
export function sideThreadRows(
|
|
byId: Readonly<Record<string, SidebarSessionSummary>>,
|
|
sessionId: string,
|
|
): SideThreadRow[] {
|
|
const rows: SideThreadRow[] = []
|
|
for (const summary of Object.values(byId)) {
|
|
if (summary.origin !== 'subagent' || summary.parentId !== sessionId) continue
|
|
if (!summary.displayTitle.startsWith(SIDE_LABEL_PREFIX)) continue
|
|
rows.push({ id: summary.id, title: summary.displayTitle, running: summary.running === true })
|
|
}
|
|
return rows
|
|
}
|
|
|
|
/** Truncate + prefix a question into a durable thread label. */
|
|
export function sideLabel(question: string): string {
|
|
const flat = question.replace(/\s+/g, ' ').trim()
|
|
const max = Math.max(1, LABEL_MAX_CHARS - SIDE_LABEL_PREFIX.length)
|
|
const body = flat.length > max ? `${flat.slice(0, max - 1)}…` : flat
|
|
return `${SIDE_LABEL_PREFIX}${body}`
|
|
}
|
|
|
|
/**
|
|
* Whether the thread log already carries the side boundary message — i.e.
|
|
* the first prompt was delivered. Tolerant to the content shape (block
|
|
* array or bare string) and to inherited seed messages (only an OWN
|
|
* boundary message starts with the prefix; seed messages came from the
|
|
* parent's log, which never contains one).
|
|
*/
|
|
export function boundaryDelivered(events: readonly SidechatLogEvent[]): boolean {
|
|
for (const event of events) {
|
|
if (event.type !== 'user/message') continue
|
|
if (messageLeadText(dataOf(event)).startsWith(SIDE_BOUNDARY_PREFIX)) return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
/** The leading text of a user/message's content (block array or bare string). */
|
|
function messageLeadText(data: Record<string, unknown>): string {
|
|
const content = data.content
|
|
const first = Array.isArray(content) ? content[0] : content
|
|
return typeof first === 'string'
|
|
? first
|
|
: (typeof first === 'object' && first !== null && 'text' in first
|
|
? String((first as { text: unknown }).text)
|
|
: '')
|
|
}
|
|
|
|
/**
|
|
* Whether a logged user/message is a CONTEXT INJECTION (the boundary prompt
|
|
* plus the parked in-progress snapshot) rather than a real user message.
|
|
* New threads deliver the injection via `agent.inject` stamped with a
|
|
* non-'user' source kind; threads created before that split carry
|
|
* boundary+question in ONE 'user' message, recognized by the boundary
|
|
* prefix. Both render as one collapsible injection row — never as a user
|
|
* bubble.
|
|
*/
|
|
export function isContextInjectionMessage(data: Record<string, unknown>): boolean {
|
|
const source = data.source as { kind?: unknown } | null | undefined
|
|
if (source?.kind !== undefined && source.kind !== 'user') return true
|
|
return messageLeadText(data).startsWith(SIDE_BOUNDARY_PREFIX)
|
|
}
|
|
|
|
/** The info the thread header shows (live runtime state + agent identity). */
|
|
export interface SidechatThreadInfo {
|
|
/** A live agent drives the thread right now (false = cold/persisted). */
|
|
live: boolean
|
|
/** Live lifecycle state; absent on cold threads. */
|
|
status?: 'idle' | 'running'
|
|
/** Provider route the thread actually requests with (see
|
|
* {@link parentModelSelection} — NOT the frozen creation-time default). */
|
|
provider?: string
|
|
/** Model id the thread actually requests with. */
|
|
model?: string
|
|
/** Reasoning effort of the effective selection, when the log records one. */
|
|
reasoningEffort?: string
|
|
/** The recorded agent preset (live header, or persisted on cold reads). */
|
|
preset?: string
|
|
}
|
|
|
|
/**
|
|
* One UNCLAIMED inbox message of a live side thread — the queue the next step
|
|
* or turn will claim. The transcript renders these as pending user bubbles
|
|
* (with a spinner and a dequeue affordance) instead of leaving accepted input
|
|
* invisible until a claim logs it as a durable `user/message`.
|
|
*/
|
|
export interface SidechatPendingInput {
|
|
/** Durable message identity — the `sidechat.dequeue` key. */
|
|
id: string
|
|
/** Which boundary will claim it; `next-step` entries are claimed first. */
|
|
target: 'next-turn' | 'next-step'
|
|
/** Flattened text of the message content ('' when it carries no text). */
|
|
text: string
|
|
}
|
|
|
|
/** One routable model in the host model catalog (a structural slice of the
|
|
* session controller's own `modelCatalog` rows). */
|
|
export interface SidechatModelInfo {
|
|
id: string
|
|
name?: string
|
|
description?: string
|
|
/** Provider-declared reasoning efforts, when the model has any. */
|
|
reasoning?: {
|
|
efforts: readonly { id: string; name?: string; description?: string }[]
|
|
defaultEffort?: string
|
|
}
|
|
}
|
|
|
|
/** One provider group of the host model catalog. */
|
|
export interface SidechatModelGroup {
|
|
id: string
|
|
name?: string
|
|
models: readonly SidechatModelInfo[]
|
|
}
|
|
|
|
/**
|
|
* The host model catalog as the side-chat model picker consumes it. Sourced
|
|
* from the host's own `sessionController.modelCatalog()` (the exact catalog
|
|
* the main composer lists), degraded to `available: false` when that service
|
|
* is absent — the picker then offers only "inherit the parent".
|
|
*/
|
|
export interface SidechatModelCatalog {
|
|
/** Whether a host catalog was readable at all. */
|
|
available: boolean
|
|
/** The deployment default selection, when readable. */
|
|
default?: SidechatModelSelection
|
|
/** Provider groups with at least one routable model. */
|
|
groups: readonly SidechatModelGroup[]
|
|
/** Providers whose catalog read failed (kept so the picker can say so). */
|
|
failures: readonly { id: string; name?: string; message: string }[]
|
|
}
|
|
|
|
/** Flatten one pending message's content into the text the bubble shows. */
|
|
function pendingText(message: unknown): string {
|
|
const content = (message as { content?: unknown } | undefined)?.content
|
|
if (typeof content === 'string') return content
|
|
if (!Array.isArray(content)) return ''
|
|
const parts: string[] = []
|
|
for (const block of content) {
|
|
if (block === null || typeof block !== 'object') continue
|
|
const item = block as { type?: unknown; text?: unknown }
|
|
if (item.type === 'text' && typeof item.text === 'string') parts.push(item.text)
|
|
}
|
|
return parts.join('\n')
|
|
}
|
|
|
|
/** The identity one inbox message carries (`MessageId`, a branded string). */
|
|
export function pendingInputId(message: unknown): string | undefined {
|
|
const id = (message as { id?: unknown } | undefined)?.id
|
|
return typeof id === 'string' && id !== '' ? id : undefined
|
|
}
|
|
|
|
/**
|
|
* Project one agent's inbox into wire rows, in CLAIM order: every `next-step`
|
|
* entry first, then the queued turns (the host's `Inbox.claim` drains exactly
|
|
* that way). Messages without an identity are skipped — they could not be
|
|
* dequeued, so showing them as pending would offer a dead button.
|
|
*/
|
|
export function pendingInputs(
|
|
nextStep: readonly unknown[],
|
|
nextTurn: readonly unknown[],
|
|
): SidechatPendingInput[] {
|
|
const rows: SidechatPendingInput[] = []
|
|
const add = (message: unknown, target: SidechatPendingInput['target']): void => {
|
|
const id = pendingInputId(message)
|
|
if (id === undefined) return
|
|
rows.push({ id, target, text: pendingText(message) })
|
|
}
|
|
for (const message of nextStep) add(message, 'next-step')
|
|
for (const message of nextTurn) add(message, 'next-turn')
|
|
return rows
|
|
}
|
|
|
|
/**
|
|
* Fold a log's `agent/inbox/spliced` events into the message ids STILL pending
|
|
* after it — the durable record of the inbox (the host's own
|
|
* `inboxProjectionDefinition` semantics: apply every splice in order).
|
|
*
|
|
* This is the reading the ghost sweep needs: a seeded thread's inherited
|
|
* prefix can leave messages "pending" that the parent had already claimed
|
|
* (the claim splice sits inside the parent's open turn, which the seed
|
|
* truncation cuts away), and those must never be delivered as the child's
|
|
* own input.
|
|
* @param events - the log (or any prefix of it), contiguous from seq 0.
|
|
* @param limit - exclusive upper bound of event count to fold (the inherited
|
|
* prefix length, or the whole log).
|
|
* @returns the ids of messages still pending after the folded events.
|
|
*/
|
|
export function pendingIdsInLog(events: readonly SidechatLogEvent[], limit?: number): string[] {
|
|
const nextTurn: unknown[] = []
|
|
const nextStep: unknown[] = []
|
|
const end = limit === undefined ? events.length : Math.min(limit, events.length)
|
|
for (let index = 0; index < end; index++) {
|
|
const event = events[index]
|
|
if (event === undefined || event.type !== 'agent/inbox/spliced') continue
|
|
const data = dataOf(event)
|
|
const target = data.target === 'next-step' ? nextStep : data.target === 'next-turn' ? nextTurn : undefined
|
|
if (target === undefined) continue
|
|
const start = typeof data.start === 'number' && Number.isSafeInteger(data.start) ? data.start : 0
|
|
const removed = typeof data.removedCount === 'number' && Number.isSafeInteger(data.removedCount) ? data.removedCount : 0
|
|
const inserted = Array.isArray(data.inserted) ? data.inserted : []
|
|
target.splice(Math.max(0, Math.min(start, target.length)), Math.max(0, removed), ...inserted)
|
|
}
|
|
const ids: string[] = []
|
|
for (const message of [...nextStep, ...nextTurn]) {
|
|
const id = pendingInputId(message)
|
|
if (id !== undefined) ids.push(id)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
/**
|
|
* The message ids a seeded thread INHERITED as still-pending but never
|
|
* actually owned: fold only the inherited prefix (`[0, inheritedEventCount)`),
|
|
* which is exactly the region the parent's own claim splices may have been
|
|
* cut out of. The caller removes precisely these from the live inbox, so a
|
|
* user's genuinely queued side-thread input is never touched.
|
|
* @param events - the thread's log.
|
|
* @param inheritedEventCount - the thread's inherited prefix length.
|
|
* @returns the ghost message ids present in the prefix fold.
|
|
*/
|
|
export function inheritedPendingIds(
|
|
events: readonly SidechatLogEvent[],
|
|
inheritedEventCount: number,
|
|
): string[] {
|
|
return pendingIdsInLog(events, Math.max(0, Math.trunc(inheritedEventCount)))
|
|
}
|
|
|
|
/**
|
|
* The thread's effective model selection: `pending ?? lastUsed` of its own
|
|
* `model/selection` / `request/header` fold (see {@link parentModelSelection},
|
|
* which this is the same projection as). Used by the routes to report what the
|
|
* thread will actually request with.
|
|
*/
|
|
export function effectiveModelSelection(
|
|
events: readonly SidechatLogEvent[],
|
|
): SidechatModelSelection | undefined {
|
|
return parentModelSelection(events)
|
|
}
|
|
|
|
/** The events a thread produced itself: everything after the LAST
|
|
* `session/end-seed` marker (the fork-seed boundary). A log with no marker
|
|
* (a thread created before seeding existed) is returned whole. */
|
|
export function threadOwnLogEvents(events: readonly SidechatLogEvent[]): SidechatLogEvent[] {
|
|
for (let index = events.length - 1; index >= 0; index--) {
|
|
if (events[index]?.type === 'session/end-seed') return events.slice(index + 1)
|
|
}
|
|
return [...events]
|
|
}
|
|
|
|
/** {@link threadOwnLogEvents} over history rows (the client cache shape). */
|
|
export function threadOwnEvents(entries: readonly SidebarHistoryEntry[]): SidechatLogEvent[] {
|
|
return threadOwnLogEvents(entries.map(entry => entry.event))
|
|
}
|
|
|
|
/**
|
|
* Whether the thread has at least one completed turn — the save-as-new-
|
|
* session precondition (`session.fork` refuses to fork before the first
|
|
* `turn/end`).
|
|
*/
|
|
export function threadHasCompletedTurn(entries: readonly SidebarHistoryEntry[]): boolean {
|
|
return threadOwnEvents(entries).some(event => event.type === 'turn/end')
|
|
}
|
|
|
|
/** Whether the thread ends with a user message that no completed turn
|
|
* answered yet — such a pending follow-up is NOT carried into the saved
|
|
* session (the fork cut is the last `turn/end`). */
|
|
export function threadTrailingPending(entries: readonly SidebarHistoryEntry[]): boolean {
|
|
const own = threadOwnEvents(entries)
|
|
let lastUser = -1
|
|
let lastTurnEnd = -1
|
|
own.forEach((event, index) => {
|
|
if (event.type === 'user/message') lastUser = index
|
|
if (event.type === 'turn/end') lastTurnEnd = index
|
|
})
|
|
return lastUser > lastTurnEnd
|
|
}
|
|
|
|
/**
|
|
* The agent preset a session actually runs: newest `agent-preset/selected`
|
|
* event wins, else the creation header (mirror of the dsh-agent-presets
|
|
* resolveSessionPreset helper — replicated here to avoid a host dependency
|
|
* on that package).
|
|
*/
|
|
export function resolvePresetId(
|
|
header: { agentPreset?: string },
|
|
events: readonly SidechatLogEvent[],
|
|
): string | undefined {
|
|
for (let index = events.length - 1; index >= 0; index--) {
|
|
const event = events[index]
|
|
if (event?.type !== 'agent-preset/selected') continue
|
|
const preset = dataOf(event).agentPreset
|
|
if (typeof preset === 'string') return preset
|
|
}
|
|
return header.agentPreset
|
|
}
|
|
|
|
/** One model route as the durable log records it (the shape of a
|
|
* `model/selection` event payload and of the core `modelSelection`
|
|
* projection rows). */
|
|
export interface SidechatModelSelection {
|
|
provider: string
|
|
model: string
|
|
reasoningEffort?: string
|
|
}
|
|
|
|
/**
|
|
* The model selection a session CURRENTLY runs, projected from its durable
|
|
* log (mirror of the core `modelSelection` projection — replicated here
|
|
* like resolvePresetId to avoid a host dependency). An explicit
|
|
* `model/selection` event stays PENDING until a same-route `request/header`
|
|
* consumes it; the value the composer's model selector shows is
|
|
* `pending ?? lastUsed`.
|
|
*
|
|
* This is NOT what `agent.options.provider/model` holds: those freeze the
|
|
* creation-time deployment default and are never rewritten by a UI model
|
|
* switch — DSH 0.1.5 routes requests through the session-local selection
|
|
* and its `agent/request` waterfall instead. Anyone inheriting "the
|
|
* parent's current model" must read this projection; inheriting
|
|
* `parent.options` verbatim silently ships the stale creation default
|
|
* (issue #368).
|
|
*
|
|
* A `request/header` contributes provider/model/effort, EXCEPT when its
|
|
* effort is marked as an adapter default (`adapterDefaults.reasoningEffort`)
|
|
* which the core selection getter deliberately does not adopt as a sticky pick.
|
|
*/
|
|
export function parentModelSelection(
|
|
events: readonly SidechatLogEvent[],
|
|
): SidechatModelSelection | undefined {
|
|
let pending: SidechatModelSelection | undefined
|
|
let lastUsed: SidechatModelSelection | undefined
|
|
for (const event of events) {
|
|
if (event.type === 'model/selection') {
|
|
const selection = looseSelection(dataOf(event))
|
|
if (selection !== undefined) pending = selection
|
|
continue
|
|
}
|
|
if (event.type !== 'request/header') continue
|
|
const header = (dataOf(event).header as {
|
|
config?: unknown
|
|
adapterDefaults?: { reasoningEffort?: boolean }
|
|
} | undefined)
|
|
const config = header?.config
|
|
if (config === null || typeof config !== 'object') continue
|
|
const selection = looseSelection(config as Record<string, unknown>)
|
|
if (selection === undefined) continue
|
|
const isDefaultEffort = header?.adapterDefaults?.reasoningEffort === true
|
|
lastUsed = {
|
|
provider: selection.provider,
|
|
model: selection.model,
|
|
...(!isDefaultEffort && selection.reasoningEffort !== undefined ? { reasoningEffort: selection.reasoningEffort } : {}),
|
|
}
|
|
if (sameSelection(pending, lastUsed)) pending = undefined
|
|
}
|
|
return pending ?? lastUsed
|
|
}
|
|
|
|
function sameSelection(
|
|
left: SidechatModelSelection | undefined,
|
|
right: SidechatModelSelection | undefined,
|
|
): boolean {
|
|
if (left === right) return true
|
|
if (left === undefined || right === undefined) return false
|
|
return left.provider === right.provider
|
|
&& left.model === right.model
|
|
&& left.reasoningEffort === right.reasoningEffort
|
|
}
|
|
|
|
/** Narrow one loose record into a model selection (non-empty strings only). */
|
|
function looseSelection(data: Record<string, unknown>): SidechatModelSelection | undefined {
|
|
const { provider, model, reasoningEffort } = data
|
|
if (typeof provider !== 'string' || provider === '') return undefined
|
|
if (typeof model !== 'string' || model === '') return undefined
|
|
return {
|
|
provider,
|
|
model,
|
|
...(typeof reasoningEffort === 'string' && reasoningEffort !== '' ? { reasoningEffort } : {}),
|
|
}
|
|
}
|