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

Function stream

packages/opencode/src/session/llm/native-runtime.ts:74–146  ·  view source on GitHub ↗
(input: StreamInput)

Source from the content-addressed store, hash-verified

72}
73
74export function stream(input: StreamInput): StreamResult {
75 const fetch = providerFetch(input)
76 const current = statusWithFetch(input, fetch)
77 if (current.type === "unsupported") return current
78
79 // Integration point with @opencode-ai/llm: native-request lowers session data
80 // into an LLMRequest, then LLMClient handles route selection and transport.
81 //
82 // ProviderTransform.providerOptions builds AI-SDK-shaped options for the
83 // selected SDK key (e.g. "openai") and the native LLM SDK reads the same
84 // keys via OpenAIOptions.* (store, reasoningEffort, reasoningSummary,
85 // include, textVerbosity, promptCacheKey). Both sides intentionally use
86 // OpenAI's official wire field names, so this is identity, not translation
87 // — if a field ever needs to differ between the two surfaces, the
88 // translation belongs here, not split across both packages.
89 const tools = nativeTools(input.tools, input)
90 const request = LLMNative.request({
91 model: input.model,
92 apiKey: current.apiKey,
93 baseURL: current.baseURL,
94 messages: ProviderTransform.message(input.messages, input.model, input.providerOptions ?? {}),
95 toolChoice: input.toolChoice,
96 temperature: input.temperature,
97 topP: input.topP,
98 topK: input.topK,
99 maxOutputTokens: input.maxOutputTokens,
100 providerOptions: ProviderTransform.providerOptions(input.model, input.providerOptions ?? {}),
101 headers: { ...providerHeaders(input.provider.options.headers), ...input.headers },
102 })
103 const stream = Stream.scoped(
104 Stream.unwrap(
105 Effect.gen(function* () {
106 const settlements = yield* FiberSet.make<void>()
107 const results = yield* Queue.unbounded<LLMEvent, Cause.Done>()
108 const provider = input.llmClient
109 .stream(
110 LLMRequest.update(request, {
111 tools: [...request.tools, ...toDefinitions(tools)],
112 }),
113 )
114 .pipe(
115 Stream.flatMap((event) =>
116 event.type !== "tool-call" || event.providerExecuted
117 ? Stream.make(event)
118 : Stream.make(event).pipe(
119 Stream.concat(
120 Stream.fromEffectDrain(
121 ToolRuntime.dispatch(tools, event).pipe(
122 Effect.flatMap((dispatched) => Queue.offerAll(results, dispatched.events)),
123 Effect.catchCause((cause) => Queue.failCause(results, cause)),
124 Effect.asVoid,
125 FiberSet.run(settlements, { startImmediately: true }),
126 ),
127 ),
128 ),
129 ),
130 ),
131 Stream.concat(

Callers

nothing calls this directly

Calls 11

toDefinitionsFunction · 0.90
providerFetchFunction · 0.85
statusWithFetchFunction · 0.85
nativeToolsFunction · 0.85
providerHeadersFunction · 0.85
streamMethod · 0.80
updateMethod · 0.65
requestMethod · 0.45
messageMethod · 0.45
makeMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected