MCPcopy Index your code
hub / github.com/anomalyco/opencode / ensureStream

Function ensureStream

packages/opencode/src/cli/cmd/run/runtime.ts:457–502  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

455
456 const streamTask = deps.streamTransport ?? import("./stream.transport")
457 const ensureStream = () => {
458 if (state.stream) {
459 return state.stream
460 }
461
462 // Share eager prewarm and first-turn boot through one in-flight promise,
463 // but clear it if transport creation fails so a later prompt can retry.
464 const next = (async () => {
465 await ensureSession()
466 if (footer.isClosed) {
467 throw new Error("runtime closed")
468 }
469
470 const mod = await streamTask
471 if (footer.isClosed) {
472 throw new Error("runtime closed")
473 }
474
475 const handle = await mod.createSessionTransport({
476 sdk: ctx.sdk,
477 directory: ctx.directory,
478 sessionID: state.sessionID,
479 thinking: input.thinking,
480 replay: input.replay,
481 replayLimit: input.replayLimit,
482 limits: () => state.limits,
483 providers: () => state.providers,
484 footer,
485 trace: log,
486 })
487 if (footer.isClosed) {
488 await handle.close()
489 throw new Error("runtime closed")
490 }
491
492 state.selectSubagent = (sessionID) => handle.selectSubagent(sessionID)
493 return { mod, handle }
494 })()
495 state.stream = next
496 void next.catch(() => {
497 if (state.stream === next) {
498 state.stream = undefined
499 }
500 })
501 return next
502 }
503
504 let resizeTimer: ReturnType<typeof setTimeout> | undefined
505 const offResize = shell.onResize(() => {

Callers 2

runQueueFunction · 0.85
runInteractiveRuntimeFunction · 0.85

Calls 2

ensureSessionFunction · 0.85
closeMethod · 0.45

Tested by

no test coverage detected