98e40dac97
CLI Smoke Test / smoke-test-linux (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-linux (24) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (24) (push) Has been cancelled
Expo App TypeScript typecheck / typecheck (push) Has been cancelled
70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import type {
|
|
AgentEvent,
|
|
AgentStartOptions,
|
|
} from '../shared/agent-protocol'
|
|
|
|
/* ─────────────────────────────────────────────────────────────────────────
|
|
* agent-bridge — renderer-side handle to a worker-hosted Agent SDK session.
|
|
*
|
|
* One `AgentSession` per chat. The session id is a UUID generated by the
|
|
* caller (see chat/store.ts) and round-trips through the SDK as its own
|
|
* session id, so resuming after a process restart is just `start({ resume:
|
|
* true })` with the same id.
|
|
*
|
|
* Events arrive on a dedicated IPC channel; the bridge fans them out to a
|
|
* single callback so the chat runner can update its store without tracking
|
|
* subscribers itself.
|
|
* ──────────────────────────────────────────────────────────────────────── */
|
|
|
|
export interface AgentSession {
|
|
readonly id: string
|
|
/** Send a follow-up user message into a live session. Queues if the
|
|
* assistant is mid-turn. */
|
|
send(text: string): void
|
|
/** Interrupt the current assistant turn; the session stays alive. */
|
|
interrupt(): void
|
|
/** End the session and release the worker-side state. Once stopped,
|
|
* any future messages must `start()` again with `resume: true`. */
|
|
stop(): void
|
|
}
|
|
|
|
export interface OpenSessionArgs {
|
|
sessionId: string
|
|
/** First user message to send. */
|
|
prompt: string
|
|
/** When true, load history for `sessionId` from the SDK's session store
|
|
* before sending `prompt`. */
|
|
resume: boolean
|
|
options: AgentStartOptions
|
|
onEvent(ev: AgentEvent): void
|
|
/** Fired once the worker has cleaned up the session (after `stop()` or
|
|
* when the SDK exits naturally). */
|
|
onClosed?: () => void
|
|
}
|
|
|
|
export function openAgentSession(args: OpenSessionArgs): AgentSession {
|
|
const offEvent = window.agent.onEvent(args.sessionId, args.onEvent)
|
|
const offClosed = window.agent.onClosed(args.sessionId, () => {
|
|
try {
|
|
args.onClosed?.()
|
|
} finally {
|
|
offEvent()
|
|
offClosed()
|
|
}
|
|
})
|
|
|
|
window.agent.start({
|
|
sessionId: args.sessionId,
|
|
prompt: args.prompt,
|
|
resume: args.resume,
|
|
options: args.options,
|
|
})
|
|
|
|
return {
|
|
id: args.sessionId,
|
|
send: (text) => window.agent.send(args.sessionId, text),
|
|
interrupt: () => window.agent.interrupt(args.sessionId),
|
|
stop: () => window.agent.stop(args.sessionId),
|
|
}
|
|
}
|