MCPcopy Create free account
hub / github.com/TanStack/ai / streamModelResponse

Method streamModelResponse

packages/ai/src/activities/chat/index.ts:990–1141  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

988 }
989
990 private async *streamModelResponse(): AsyncGenerator<StreamChunk> {
991 const { metadata, modelOptions } = this.params
992 const tools = this.tools
993
994 // Convert tool schemas to JSON Schema before passing to adapter
995 const toolsWithJsonSchemas = tools.map((tool) => ({
996 ...tool,
997 inputSchema: tool.inputSchema
998 ? convertSchemaToJsonSchema(tool.inputSchema)
999 : undefined,
1000 outputSchema: tool.outputSchema
1001 ? convertSchemaToJsonSchema(tool.outputSchema)
1002 : undefined,
1003 }))
1004
1005 this.middlewareCtx.phase = 'modelStream'
1006
1007 const providerName =
1008 (this.adapter as { provider?: string }).provider ?? this.adapter.name
1009 this.logger.request(
1010 `activity=chat provider=${providerName} model=${this.params.model} messages=${this.messages.length} tools=${this.tools.length} stream=true`,
1011 {
1012 provider: providerName,
1013 model: this.params.model,
1014 messageCount: this.messages.length,
1015 toolCount: this.tools.length,
1016 },
1017 )
1018
1019 // When the adapter declared `supportsCombinedToolsAndSchema()`, the
1020 // activity layer set `nativeCombined: true` and we forward the
1021 // pre-converted JSON Schema into the regular chatStream call. The
1022 // adapter wires it into the upstream request (e.g. `response_format`,
1023 // `text.format`, `output_format`) so the model's final-turn text is
1024 // schema-constrained and the engine can harvest it from the agent loop
1025 // without a separate finalization round-trip.
1026 const combinedSchema =
1027 this.finalStructuredOutput?.nativeCombined === true
1028 ? this.finalStructuredOutput.jsonSchema
1029 : undefined
1030
1031 for await (const chunk of this.adapter.chatStream({
1032 model: this.params.model,
1033 messages: this.messages,
1034 tools: toolsWithJsonSchemas,
1035 metadata,
1036 request: this.effectiveRequest,
1037 modelOptions,
1038 systemPrompts: this.systemPrompts,
1039 logger: this.logger,
1040 threadId: this.threadId,
1041 runId: this.runIdOverride,
1042 parentRunId: this.parentRunIdOverride,
1043 // Expose provided capabilities (e.g. sandbox) to harness adapters.
1044 capabilities: this.middlewareCtx,
1045 // Client approval decisions, for harness interactive-approval resolution.
1046 approvals: this.initialApprovals,
1047 ...(combinedSchema ? { outputSchema: combinedSchema } : {}),

Callers 1

runMethod · 0.95

Calls 11

isCancelledMethod · 0.95
handleStreamChunkMethod · 0.95
drainSandboxFileQueueMethod · 0.95
generateMessageIdFunction · 0.90
nowMethod · 0.80
runOnChunkMethod · 0.80
runOnUsageMethod · 0.80
outputMethod · 0.65
requestMethod · 0.45
chatStreamMethod · 0.45

Tested by

no test coverage detected