MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / createMessage

Function createMessage

src/api/providers/openai-compatible.ts:153–195  ·  view source on GitHub ↗

* Create a message stream using the AI SDK.

(
		systemPrompt: string,
		messages: Anthropic.Messages.MessageParam[],
		metadata?: ApiHandlerCreateMessageMetadata,
	)

Source from the content-addressed store, hash-verified

151 * Create a message stream using the AI SDK.
152 */
153 override async *createMessage(
154 systemPrompt: string,
155 messages: Anthropic.Messages.MessageParam[],
156 metadata?: ApiHandlerCreateMessageMetadata,
157 ): ApiStream {
158 const model = this.getModel()
159 const languageModel = this.getLanguageModel()
160
161 // Convert messages to AI SDK format
162 const aiSdkMessages = convertToAiSdkMessages(messages)
163
164 // Convert tools to OpenAI format first, then to AI SDK format
165 const openAiTools = this.convertToolsForOpenAI(metadata?.tools)
166 const aiSdkTools = convertToolsForAiSdk(openAiTools) as ToolSet | undefined
167
168 // Build the request options
169 const requestOptions: Parameters<typeof streamText>[0] = {
170 model: languageModel,
171 system: systemPrompt,
172 messages: aiSdkMessages,
173 temperature: model.temperature ?? this.config.temperature ?? 0,
174 maxOutputTokens: this.getMaxOutputTokens(),
175 tools: aiSdkTools,
176 toolChoice: this.mapToolChoice(metadata?.tool_choice),
177 }
178
179 // Use streamText for streaming responses
180 const result = streamText(requestOptions)
181
182 // Process the full stream to get all events
183 for await (const part of result.fullStream) {
184 // Use the processAiSdkStreamPart utility to convert stream parts
185 for (const chunk of processAiSdkStreamPart(part)) {
186 yield chunk
187 }
188 }
189
190 // Yield usage metrics at the end
191 const usage = await result.usage
192 if (usage) {
193 yield this.processUsageMetrics(usage)
194 }
195 }
196
197 /**
198 * Complete a prompt using the AI SDK generateText.

Callers

nothing calls this directly

Calls 6

convertToAiSdkMessagesFunction · 0.90
convertToolsForAiSdkFunction · 0.90
processAiSdkStreamPartFunction · 0.90
getMaxOutputTokensMethod · 0.80
getModelMethod · 0.65
processUsageMetricsMethod · 0.45

Tested by

no test coverage detected