Represent tool results as tool-role messages and migrate released wrappers without losing outer own JSON fields, call identity, error state, or writer field order. Refuse nested or wrapper-only information that has no lossless current representation before successor publication. Validate current event relationships directly instead of projecting V4 data back into V3. Keep lifecycle, PTC, title/command, compaction, system-field and retired-syntax checks on catalog restoration and ordinary JSONL opens. The Worker JSON equality helper must inspect own keys so it cannot admit PTC arguments that Node rejects. Keep the source role change, required native admission and consumer fixture updates in one complete review layer. Explain shared-type changes in the persistence record and the native reader rationale in its existing note. Use checked branded fixture identities and discriminant narrowing instead of unknown double casts; fixture values and assertions remain intact. This recomposes the original tool-role work by _Kerman with the independently validated migration and admission repairs by Tianyi Cui. Frozen generations and snapshot input version selection are retained. Co-authored-by: Tianyi Cui <[email protected]>
88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
/** First-party semantic text extraction for session-query consumers. */
|
|
|
|
import type { SessionEvent } from '@deepseek-ai/dsh-session'
|
|
// Type-only: includes the first-party todo event consumed below.
|
|
import type {} from '@deepseek-ai/dsh-tool-todo'
|
|
|
|
/**
|
|
* Extract searchable semantic text from one first-party session event.
|
|
*
|
|
* Structural boundaries, embedded raw streams, request envelopes, and unknown
|
|
* declaration-merged events contribute no text.
|
|
* @param event - event to inspect.
|
|
* @returns newline-joined semantic text, or an empty string when non-searchable.
|
|
*/
|
|
export function extractSessionEventText(event: SessionEvent): string {
|
|
switch (event.type) {
|
|
case 'user/message':
|
|
return contentText(event.data.content)
|
|
case 'assistant/message':
|
|
return contentText(event.data.message.content)
|
|
case 'tool/call':
|
|
return joinText([event.data.name, event.data.arguments])
|
|
case 'tool/result':
|
|
return joinText([
|
|
contentText(event.data.message.content),
|
|
event.data.error?.name ?? '',
|
|
event.data.error?.code ?? '',
|
|
])
|
|
case 'todo/write':
|
|
return joinText(event.data.todos.flatMap(todo => [todo.status, todo.content]))
|
|
case 'turn/end':
|
|
return turnEndText(event.data.reason)
|
|
case 'turn/start':
|
|
case 'step/start':
|
|
case 'step/end':
|
|
case 'assistant/attempt':
|
|
case 'request/header':
|
|
return ''
|
|
// SessionEventMap is merge-extensible. Unknown events remain
|
|
// non-searchable until a concrete first-party consumer defines semantics.
|
|
default:
|
|
return ''
|
|
}
|
|
}
|
|
|
|
function turnEndText(reason: SessionEvent<'turn/end'>['data']['reason']): string {
|
|
switch (reason.kind) {
|
|
case 'error':
|
|
return joinText(['error', reason.error.message])
|
|
case 'aborted':
|
|
return 'aborted'
|
|
case 'max-tokens':
|
|
case 'interrupted':
|
|
return reason.kind
|
|
case 'completed':
|
|
return ''
|
|
// TurnEndReasonMap is merge-extensible. Unknown outcomes stay out until
|
|
// their owner defines which detail is semantic rather than structural.
|
|
default:
|
|
return ''
|
|
}
|
|
}
|
|
|
|
type SessionContentBlock = SessionEvent<'user/message'>['data']['content'][number]
|
|
|
|
function contentText(content: readonly SessionContentBlock[]): string {
|
|
return joinText(content.flatMap(blockText))
|
|
}
|
|
|
|
function blockText(block: SessionContentBlock): string[] {
|
|
switch (block.type) {
|
|
case 'text':
|
|
return [block.text]
|
|
case 'reasoning':
|
|
return []
|
|
case 'tool-call':
|
|
return [block.name, block.arguments]
|
|
// ContentBlockMap is merge-extensible. Unknown blocks do not become
|
|
// searchable merely because their payload happens to contain strings.
|
|
default:
|
|
return []
|
|
}
|
|
}
|
|
|
|
function joinText(parts: readonly string[]): string {
|
|
return parts.map(part => part.trim()).filter(Boolean).join('\n')
|
|
}
|