run: prewarm interactive transport in background

This commit is contained in:
Simon Klee
2026-04-17 15:49:52 +02:00
parent 01159082de
commit 52c196c723
2 changed files with 67 additions and 16 deletions

View File

@@ -221,7 +221,7 @@ export async function createRuntimeLifecycle(input: LifecycleInput): Promise<Lif
process.on("SIGINT", sigint)
let closed = false
const close = async (next: { showExit: boolean; sessionTitle?: string }) => {
const close = async (next: { showExit: boolean; sessionTitle?: string; sessionID?: string }) => {
if (closed) {
return
}

View File

@@ -272,24 +272,65 @@ async function runInteractiveRuntime(input: RunRuntimeInput): Promise<void> {
handle: Awaited<ReturnType<Awaited<typeof import("./stream.transport")>["createSessionTransport"]>>
}
| undefined
const ensureStream = async () => {
let loading:
| Promise<{
mod: Awaited<typeof import("./stream.transport")>
handle: Awaited<ReturnType<Awaited<typeof import("./stream.transport")>["createSessionTransport"]>>
}>
| undefined
const ensureStream = () => {
if (stream) {
return stream
return Promise.resolve(stream)
}
await ensureSession()
const mod = await streamTask
const handle = await mod.createSessionTransport({
sdk: ctx.sdk,
sessionID,
thinking: input.thinking,
limits: () => limits,
footer,
trace: log,
})
selectSubagent = handle.selectSubagent
stream = { mod, handle }
return stream
if (loading) {
return loading
}
const task = (async () => {
await ensureSession()
if (footer.isClosed) {
throw new Error("runtime closed")
}
const mod = await streamTask
if (footer.isClosed) {
throw new Error("runtime closed")
}
const handle = await mod.createSessionTransport({
sdk: ctx.sdk,
sessionID,
thinking: input.thinking,
limits: () => limits,
footer,
trace: log,
})
if (footer.isClosed) {
await handle.close()
throw new Error("runtime closed")
}
selectSubagent = handle.selectSubagent
const next = { mod, handle }
stream = next
return next
})()
loading = task
task.then(
() => {
if (loading === task) {
loading = undefined
}
},
() => {
if (loading === task) {
loading = undefined
}
},
)
return task
}
try {
@@ -299,6 +340,16 @@ async function runInteractiveRuntime(input: RunRuntimeInput): Promise<void> {
await ensureStream()
}
if (!eager && input.resolveSession) {
queueMicrotask(() => {
if (footer.isClosed) {
return
}
void ensureStream().catch(() => {})
})
}
try {
if (demo) {
await demo.start()