mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
107 lines
4.0 KiB
TypeScript
107 lines
4.0 KiB
TypeScript
import { readOpenCodeNativeChatTranscriptFull } from './transcript-history/opencode/transcript-opencode'
|
||
import { isENOENT } from '../ipc/filesystem/access/filesystem-path-containment'
|
||
import type { AgentType } from '../../shared/agent-status/contracts/agent-status-types'
|
||
import type {
|
||
NativeChatMessage,
|
||
NativeChatTurnLifecycle
|
||
} from '../../shared/native-chat/native-chat-types'
|
||
import { resolveNativeChatTranscriptAgent } from '../../shared/native-chat/native-chat-agent-support'
|
||
import { errorMessage } from '../../shared/error-message'
|
||
import { resolveSessionFilePath, type ResolveSessionFileOptions } from './session-file-resolver'
|
||
import { openTranscriptReadStream } from './transcript-filesystem/wsl-transcript-fs-access'
|
||
import { wslTranscriptFsRefusal } from './transcript-filesystem/wsl-transcript-fs-error'
|
||
import { decodeClaudeTranscriptLine } from './transcript-decoding/transcript-line-decoders-claude'
|
||
import { decodeCodexTranscriptLine } from './transcript-decoding/transcript-line-decoders-codex'
|
||
import { decodeGrokTranscriptLine } from './transcript-decoding/transcript-line-decoders-grok'
|
||
import { decodeOmpTranscriptLine } from './transcript-decoding/transcript-line-decoders-omp'
|
||
import { decodeTranscriptStream } from './transcript-streaming/transcript-stream-lines'
|
||
|
||
export type ReadTranscriptResult =
|
||
| {
|
||
messages: NativeChatMessage[]
|
||
lifecycle?: NativeChatTurnLifecycle
|
||
sourceEmpty?: boolean
|
||
forkTipItemIds?: string[]
|
||
}
|
||
// notFound is retryable: the agent may not have flushed its transcript yet.
|
||
| { error: string; notFound?: true }
|
||
|
||
export type ReadTranscriptOptions = ResolveSessionFileOptions & {
|
||
filePath?: string
|
||
sourceOrder?: boolean
|
||
}
|
||
|
||
/** Read the full transcript with no message cap; skip malformed/unknown records using the tailer’s decoders. */
|
||
export async function readNativeChatTranscript(
|
||
agent: AgentType,
|
||
sessionId: string,
|
||
options: ReadTranscriptOptions = {}
|
||
): Promise<ReadTranscriptResult> {
|
||
if (resolveNativeChatTranscriptAgent(agent) === 'opencode') {
|
||
return readOpenCodeNativeChatTranscriptFull(
|
||
sessionId,
|
||
{},
|
||
undefined,
|
||
agent === 'zcode' ? 'zcode' : undefined
|
||
)
|
||
}
|
||
let filePath: string | null
|
||
try {
|
||
filePath = options.filePath ?? (await resolveSessionFilePath(agent, sessionId, options))
|
||
} catch (err) {
|
||
// Gate refusal is unavailable, never evidence that the transcript is missing.
|
||
return { error: wslTranscriptFsRefusal(err).message }
|
||
}
|
||
if (!filePath) {
|
||
return { error: `No transcript found for ${agent} session ${sessionId}`, notFound: true }
|
||
}
|
||
try {
|
||
const transcriptAgent = resolveNativeChatTranscriptAgent(agent)
|
||
if (transcriptAgent === 'claude') {
|
||
return {
|
||
messages: await readTranscript(filePath, decodeClaudeTranscriptLine, options.sourceOrder)
|
||
}
|
||
}
|
||
if (transcriptAgent === 'codex') {
|
||
return {
|
||
messages: await readTranscript(filePath, decodeCodexTranscriptLine, options.sourceOrder)
|
||
}
|
||
}
|
||
if (transcriptAgent === 'grok') {
|
||
return {
|
||
messages: await readTranscript(filePath, decodeGrokTranscriptLine, options.sourceOrder)
|
||
}
|
||
}
|
||
if (transcriptAgent === 'omp') {
|
||
return {
|
||
messages: await readTranscript(filePath, decodeOmpTranscriptLine, options.sourceOrder)
|
||
}
|
||
}
|
||
return { error: `Unsupported agent for Chat UI transcript: ${agent}` }
|
||
} catch (err) {
|
||
// First flush/rotation can race successful resolution; ENOENT remains retryable.
|
||
if (isENOENT(err)) {
|
||
return { error: errorMessage(err), notFound: true }
|
||
}
|
||
return { error: errorMessage(err) }
|
||
}
|
||
}
|
||
|
||
async function readTranscript(
|
||
filePath: string,
|
||
decode: (line: string, fallbackId: string) => NativeChatMessage | null,
|
||
sourceOrder = false
|
||
): Promise<NativeChatMessage[]> {
|
||
const stream = openTranscriptReadStream(filePath, { encoding: 'utf-8' }, 'exact')
|
||
const { messages } = await decodeTranscriptStream(
|
||
stream,
|
||
filePath,
|
||
0,
|
||
decode,
|
||
true,
|
||
Infinity,
|
||
sourceOrder
|
||
)
|
||
return messages
|
||
}
|