(
model: LanguageModel,
modelString: string,
messages: ModelMessage[],
system: string,
tools?: Record<string, Tool>,
providerOptions?: Record<string, unknown>,
maxOutputTokens?: number,
callSettingsOverrides?: ResolvedCallSettingsOverrides,
toolPolicy?: ToolPolicy,
hasQueuedMessages?: (dispatchMode?: "tool-end" | "turn-end") => boolean,
headers?: Record<string, string | undefined>,
anthropicCacheTtlOverride?: AnthropicCacheTtl,
onChunk?: StreamTextOnChunk,
onStepMessages?: (messages: ModelMessage[]) => void
)
| 1383 | } |
| 1384 | |
| 1385 | private buildStreamRequestConfig( |
| 1386 | model: LanguageModel, |
| 1387 | modelString: string, |
| 1388 | messages: ModelMessage[], |
| 1389 | system: string, |
| 1390 | tools?: Record<string, Tool>, |
| 1391 | providerOptions?: Record<string, unknown>, |
| 1392 | maxOutputTokens?: number, |
| 1393 | callSettingsOverrides?: ResolvedCallSettingsOverrides, |
| 1394 | toolPolicy?: ToolPolicy, |
| 1395 | hasQueuedMessages?: (dispatchMode?: "tool-end" | "turn-end") => boolean, |
| 1396 | headers?: Record<string, string | undefined>, |
| 1397 | anthropicCacheTtlOverride?: AnthropicCacheTtl, |
| 1398 | onChunk?: StreamTextOnChunk, |
| 1399 | onStepMessages?: (messages: ModelMessage[]) => void |
| 1400 | ): StreamRequestConfig { |
| 1401 | const finalProviderOptions = providerOptions; |
| 1402 | |
| 1403 | // Apply cache control for Anthropic models |
| 1404 | let finalMessages = messages; |
| 1405 | let finalTools = tools; |
| 1406 | let finalSystem: string | undefined = system; |
| 1407 | const anthropicCacheTtl = |
| 1408 | anthropicCacheTtlOverride ?? getAnthropicCacheTtl(finalProviderOptions); |
| 1409 | |
| 1410 | // For Anthropic models, convert system message to a cached message at the start |
| 1411 | const cachedSystemMessage = createCachedSystemMessage(system, modelString, anthropicCacheTtl); |
| 1412 | if (cachedSystemMessage) { |
| 1413 | // Prepend cached system message and set system parameter to undefined |
| 1414 | // Note: Must be undefined, not empty string, to avoid Anthropic API error |
| 1415 | finalMessages = [cachedSystemMessage, ...messages]; |
| 1416 | finalSystem = undefined; |
| 1417 | } |
| 1418 | |
| 1419 | // Apply cache control to tools for Anthropic models |
| 1420 | if (tools) { |
| 1421 | finalTools = applyCacheControlToTools(tools, modelString, anthropicCacheTtl); |
| 1422 | } |
| 1423 | |
| 1424 | // Use the runtime model's max_output_tokens if available and caller didn't |
| 1425 | // specify. This must be the runtime model (not the mapped metadata model) |
| 1426 | // because max_output_tokens is a request parameter sent to the provider — |
| 1427 | // a custom model's provider may not support the mapped model's output cap. |
| 1428 | // If no metadata exists, omit the parameter to let the provider use its |
| 1429 | // default (Anthropic requires this but has low defaults). |
| 1430 | const { maxOutputTokens: configMaxOutputTokens, ...streamCallSettings } = |
| 1431 | callSettingsOverrides ?? {}; |
| 1432 | |
| 1433 | const runtimeModelStats = getModelStats(modelString); |
| 1434 | // Fall back to resolved stats for custom aliases (e.g., provider alias mappedToModel). |
| 1435 | const resolvedModelStats = |
| 1436 | runtimeModelStats ?? getModelStatsResolved(modelString, this.getProvidersConfig()); |
| 1437 | const effectiveMaxOutputTokens = |
| 1438 | maxOutputTokens ?? configMaxOutputTokens ?? resolvedModelStats?.max_output_tokens; |
| 1439 | |
| 1440 | return { |
| 1441 | model, |
| 1442 | messages: finalMessages, |
no test coverage detected