Files
archived-orca/src/main/opencode/transcripts/opencode-transcript-import.ts
T

72 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { stat } from 'node:fs/promises'
import { admitTranscriptPath } from '../../native-chat/transcript-discovery/transcript-path-admission'
import { dirname } from 'node:path'
import { homedir } from 'node:os'
import { isLegacyAgentSessionAccountHome } from '../../../shared/agent-session/lifecycle/agent-session-account-home'
import { getManagedDataAccountService } from '../../managed-data-accounts/service'
import {
absoluteConversationStore,
type StructuredAgentTranscriptImport
} from '../../native-chat/conversation-adoption/structured-agent-cli-conversations'
import { resolveOpenCodeDatabasePath } from '../accounts/opencode-data-directory'
import { environmentForStructuredOpenCodeAccountHome } from '../accounts/opencode-structured-account-home'
function accountDatabase(
accountHome: Parameters<StructuredAgentTranscriptImport['conversationStore']>[0]['accountHome'],
launchEnv: NodeJS.ProcessEnv
) {
if (isLegacyAgentSessionAccountHome(accountHome)) {
return null
}
const env = environmentForStructuredOpenCodeAccountHome(accountHome, {
managedAccounts: getManagedDataAccountService(),
baseEnvironment: launchEnv
})
return absoluteConversationStore(
resolveOpenCodeDatabasePath(
env,
(process.platform === 'win32' ? env.USERPROFILE : env.HOME) || homedir()
)
)
}
export const OPENCODE_TRANSCRIPT_IMPORT: StructuredAgentTranscriptImport = {
readableContext: { kind: 'container' },
// Restrict imports to the exact database the selected account’s chat can load.
conversationStore: ({ accountHome, launchEnv }) => accountDatabase(accountHome, launchEnv),
admitTranscriptPath: async ({ store: dbPath, transcriptPath }) => {
const [expected, actual] = await Promise.all([
admitTranscriptPath(dirname(dbPath), dbPath),
admitTranscriptPath(dirname(dbPath), transcriptPath)
])
return expected && actual && expected.path === actual.path ? actual.path : null
},
accountHomeCandidates: ({ selectedAccountHome }) => [
selectedAccountHome,
{ kind: 'opencode', locator: { kind: 'unmanaged' } }
],
findTranscript: async ({ providerSessionId, store: dbPath }) => {
try {
if (!(await stat(dbPath)).isFile()) {
return null
}
} catch (error) {
if (
error instanceof Error &&
'code' in error &&
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
) {
return null
}
throw error
}
const { readOpenCodeTranscriptSignalViaWorker } =
await import('../../ai-vault/scanners/opencode/reader-transport/session-scanner-opencode-sqlite-worker-spawn')
const signal = await readOpenCodeTranscriptSignalViaWorker({
dbPath,
sessionId: providerSessionId
})
return signal ? dbPath : null
}
}