The open-source agent harness - the runtime layer that turns an LLM into a working agent.
Use Vercel AI SDK for LLM interface (#16)
Chirag Jainb3a8d5de7a14
diff --git a/AGENTS.md b/AGENTS.mdindex 3ffbcac04..5237ebb2c 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -10,3 +10,4 @@- A change that makes code unused MUST remove that dead code in the same change; it MUST NOT leave stale exports, files, documentation, duplicates, or “just in case” shims.- Comments MUST explain intent, trade-offs, or constraints and remain concise; they MUST NOT restate the code or include issue-tracker IDs.- Every nested `AGENTS.md` MUST have a sibling `CLAUDE.md` containing only `@AGENTS.md`, so Cursor and Claude Code load the same scoped rules.+- Code under `packages/sdk` is autogenerated and MUST NOT be edited manually; changes belong in the generator or its source, not in the generated output.diff --git a/fern/openapi/openapi.json b/fern/openapi/openapi.jsonindex 1c142ec67..6986be705 100644--- a/fern/openapi/openapi.json+++ b/fern/openapi/openapi.json@@ -128,6 +128,58 @@],"type": "object"},+ "AnthropicProviderModelEntry": {+ "additionalProperties": false,+ "properties": {+ "api_key": {+ "minLength": 1,+ "type": "string"+ },+ "base_url": {+ "format": "uri",+ "type": "string"+ },+ "headers": {+ "additionalProperties": {+ "type": "string"+ },+ "type": "object"+ },+ "max_output_tokens": {+ "exclusiveMinimum": 0,+ "type": "integer"+ },+ "model_id": {+ "minLength": 1,+ "type": "string"+ },+ "name": {+ "minLength": 1,+ "pattern": "^[a-z0-9][a-z0-9.-]*$",+ "type": "string"+ },+ "provider": {+ "enum": [+ "anthropic"+ ],+ "type": "string"+ },+ "reasoning_efforts": {+ "items": {+ "minLength": 1,+ "type": "string"+ },+ "minItems": 1,+ "type": "array"+ }+ },+ "required": [+ "name",+ "max_output_tokens",+ "provider"+ ],+ "type": "object"+ },"ApprovalAllow": {"properties": {"status": {@@ -467,6 +519,65 @@},"type": "object"},+ "GenericProviderModelEntry": {+ "additionalProperties": false,+ "properties": {+ "api_format": {+ "enum": [+ "openai-chat-completions"+ ],+ "type": "string"+ },+ "api_key": {+ "minLength": 1,+ "type": "string"+ },+ "base_url": {+ "format": "uri",+ "type": "string"+ },+ "headers": {+ "additionalProperties": {+ "type": "string"+ },+ "type": "object"+ },+ "max_output_tokens": {+ "exclusiveMinimum": 0,+ "type": "integer"+ },+ "model_id": {+ "minLength": 1,+ "type": "string"+ },+ "name": {+ "minLength": 1,+ "pattern": "^[a-z0-9][a-z0-9.-]*$",+ "type": "string"+ },+ "provider": {+ "enum": [+ "generic"+ ],+ "type": "string"+ },+ "reasoning_efforts": {+ "items": {+ "minLength": 1,+ "type": "string"+ },+ "minItems": 1,+ "type": "array"+ }+ },+ "required": [+ "name",+ "max_output_tokens",+ "provider",+ "base_url"+ ],+ "type": "object"+ },"GetCapabilitiesResponse": {"properties": {"data": {@@ -517,6 +628,58 @@],"type": "object"},+ "GoogleGeminiProviderModelEntry": {+ "additionalProperties": false,+ "properties": {+ "api_key": {+ "minLength": 1,+ "type": "string"+ },+ "base_url": {+ "format": "uri",+ "type": "string"+ },+ "headers": {+ "additionalProperties": {+ "type": "string"+ },+ "type": "object"+ },+ "max_output_tokens": {+ "exclusiveMinimum": 0,+ "type": "integer"+ },+ "model_id": {+ "minLength": 1,+ "type": "string"+ },+ "name": {+ "minLength": 1,+ "pattern": "^[a-z0-9][a-z0-9.-]*$",+ "type": "string"+ },+ "provider": {+ "enum": [+ "google-gemini"+ ],+ "type": "string"+ },+ "reasoning_efforts": {+ "items": {+ "minLength": 1,+ "type": "string"+ },+ "minItems": 1,+ "type": "array"+ }+ },+ "required": [+ "name",+ "max_output_tokens",+ "provider"+ ],+ "type": "object"+ },"LargeToolResponseConfig": {"default": {"enabled": true@@ -992,30 +1155,29 @@"type": "object"},"ModelEntry": {- "additionalProperties": false,- "properties": {- "max_output_tokens": {- "exclusiveMinimum": 0,- "type": "integer"+ "discriminator": {+ "mapping": {+ "anthropic": "#/components/schemas/AnthropicProviderModelEntry",+ "generic": "#/components/schemas/GenericProviderModelEntry",+ "google-gemini": "#/components/schemas/GoogleGeminiProviderModelEntry",+ "openai": "#/components/schemas/OpenAIProviderModelEntry"},- "name": {- "minLength": 1,- "type": "string"+ "propertyName": "provider"+ },+ "oneOf": [+ {+ "$ref": "#/components/schemas/OpenAIProviderModelEntry"},- "reasoning_efforts": {- "items": {- "minLength": 1,- "type": "string"- },- "minItems": 1,- "type": "array"+ {+ "$ref": "#/components/schemas/AnthropicProviderModelEntry"+ },+ {+ "$ref": "#/components/schemas/GoogleGeminiProviderModelEntry"+ },+ {+ "$ref": "#/components/schemas/GenericProviderModelEntry"}- },- "required": [- "name",- "max_output_tokens"- ],- "type": "object"+ ]},"ModelMessageDeltaEvent": {"properties": {@@ -1225,6 +1387,58 @@},"type": "object"},+ "OpenAIProviderModelEntry": {+ "additionalProperties": false,+ "properties": {+ "api_key": {+ "minLength": 1,+ "type": "string"+ },+ "base_url": {+ "format": "uri",+ "type": "string"+ },+ "headers": {+ "additionalProperties": {+ "type": "string"+ },+ "type": "object"+ },+ "max_output_tokens": {+ "exclusiveMinimum": 0,+ "type": "integer"+ },+ "model_id": {+ "minLength": 1,+ "type": "string"+ },+ "name": {+ "minLength": 1,+ "pattern": "^[a-z0-9][a-z0-9.-]*$",+ "type": "string"+ },+ "provider": {+ "enum": [+ "openai"+ ],+ "type": "string"+ },+ "reasoning_efforts": {+ "items": {+ "minLength": 1,+ "type": "string"+ },+ "minItems": 1,+ "type": "array"+ }+ },+ "required": [+ "name",+ "max_output_tokens",+ "provider"+ ],+ "type": "object"+ },"PreviousTurnIdInput": {"anyOf": [{diff --git a/packages/frontend/README.md b/packages/frontend/README.mdindex 1060d3d92..8b0845471 100644--- a/packages/frontend/README.md+++ b/packages/frontend/README.md@@ -63,13 +63,13 @@ Skills: catalog UI lists `GET /api/v1/skills` (empty state when none). Selection## Gaps-| Item | Status |-| ------------------------------------- | ---------------------------------------- |-| Subscribe turn SSE | Deferred (route defined, not registered) |-| Skills | Deferred |-| Attachments / sandbox download | Off |-| Session `type` / `created_by_subject` | Soft — not required for draft FE |-| `turn.created.created_by` | Soft — stream adopt tolerates missing |+| Item | Status |+| ------------------------------------- | --------------------------------------------- |+| Subscribe turn SSE | Deferred (route defined, not registered) |+| Skills | Deferred |+| Attachments / sandbox download | On (adapter wired; sandbox download deferred) |+| Session `type` / `created_by_subject` | Soft — not required for draft FE |+| `turn.created.created_by` | Soft — stream adopt tolerates missing |## Scriptsdiff --git a/packages/frontend/src/App.tsx b/packages/frontend/src/App.tsxindex 572f23217..51af9aff4 100644--- a/packages/frontend/src/App.tsx+++ b/packages/frontend/src/App.tsx@@ -1,6 +1,10 @@import { AssistantRuntimeProvider, useAui, useAuiState } from '@assistant-ui/react';import { ErrorToasterProvider, SlotsProvider, Thread } from '@truefoundry/agent-ui-sdk';-import { useTrueFoundryAgentRuntime, type AgentSpec } from '@truefoundry/assistant-ui-runtime';+import {+ trueFoundryAttachmentAdapter,+ useTrueFoundryAgentRuntime,+ type AgentSpec,+} from '@truefoundry/assistant-ui-runtime';import { useEffect, useMemo, useRef, useState } from 'react';import { ThemeProvider } from 'tfy-web-components/components/theme/useTheme';import { AgentSessionClient } from 'truefoundry-gateway-sdk/agents';@@ -69,6 +73,7 @@ function ChatApp({mode: 'draft',defaultAgentSpec,},+ adapters: { attachments: trueFoundryAttachmentAdapter },});return (diff --git a/packages/frontend/src/ComposerCatalogControls.tsx b/packages/frontend/src/ComposerCatalogControls.tsxindex c9b303e02..0219c3abf 100644--- a/packages/frontend/src/ComposerCatalogControls.tsx+++ b/packages/frontend/src/ComposerCatalogControls.tsx@@ -22,6 +22,7 @@ interface Props {isRunning?: boolean;initialTab?: CatalogTab;onAttach?: () => void;+ onClosePanel?: () => void;onOpenPanel?: (tab: CatalogTab) => void;}@@ -43,6 +44,7 @@ export function ComposerCatalogControls({disabled = false,initialTab = 'connectors',onAttach,+ onClosePanel,onOpenPanel,}: Props) {const capabilities = useServerCapabilities();@@ -198,8 +200,12 @@ export function ComposerCatalogControls({{models.map(entry => {const nextReasoningEffort = entry.reasoning_efforts?.[0];const nextParams = { ...agentSpec?.model.params };- if (nextReasoningEffort) nextParams.reasoningEffort = nextReasoningEffort;- else delete nextParams.reasoningEffort;+ // Assign undefined (not delete) so the spread in mergeAgentSpec+ // overrides the base value; a deleted key leaves the base intact.+ // TODO (chiragjn): look into SDK generation, if we can have a more robust way to do this.+ // @ts-expect-error -- ModelParams.reasoningEffort is `string?` (exactOptionalPropertyTypes),+ // but we intentionally write undefined here so the JS spread overwrites any prior value.+ nextParams.reasoningEffort = nextReasoningEffort;return (<buttonkey={entry.name}@@ -327,6 +333,7 @@ export function ComposerCatalogControls({disabled={!onAttach}onClick={() => {setTab('attachment');+ onClosePanel?.();onAttach?.();}}>diff --git a/packages/frontend/src/ComposerShell.tsx b/packages/frontend/src/ComposerShell.tsxindex 47e264450..0277dcf8c 100644--- a/packages/frontend/src/ComposerShell.tsx+++ b/packages/frontend/src/ComposerShell.tsx@@ -109,6 +109,9 @@ export function AppComposerShell({initialTab={panelTab}disabled={disabled}isRunning={isRunning}+ onClosePanel={() => {+ setPanelOpen(false);+ }}{...(onAttach ? { onAttach } : {})}/></div>diff --git a/packages/harness/jest.config.cjs b/packages/harness/jest.config.cjsindex 2a9cf8b0c..935b21b7e 100644--- a/packages/harness/jest.config.cjs+++ b/packages/harness/jest.config.cjs@@ -12,7 +12,22 @@ module.exports = {module: { type: 'commonjs' },},],+ // Vercel AI SDK and provider packages ship as ESM — compile them to CJS for Jest.+ '^.+\\.js$': [+ '@swc/jest',+ {+ jsc: {+ parser: { syntax: 'ecmascript' },+ target: 'es2022',+ },+ module: { type: 'commonjs' },+ },+ ],},+ // Do not exclude AI SDK packages from Jest transformation (they ship as ESM).+ // pnpm stores packages under node_modules/.pnpm/, and the AI SDK + its transitive+ // dependencies all ship as ESM — transform every .js file in node_modules.+ transformIgnorePatterns: [],moduleNameMapper: {'^(\\.{1,2}/.*)\\.js$': '$1',},diff --git a/packages/harness/package.json b/packages/harness/package.jsonindex 01416eb3a..d4cd0879a 100644--- a/packages/harness/package.json+++ b/packages/harness/package.json@@ -82,12 +82,17 @@"pack:dry": "cd dist && pnpm pack --dry-run"},"dependencies": {+ "@ai-sdk/anthropic": "^4.0.25",+ "@ai-sdk/google": "^4.0.29",+ "@ai-sdk/openai": "^4.0.25",+ "@ai-sdk/openai-compatible": "^3.0.18","@daytona/sdk": "^0.190.0","@hono/zod-openapi": "^0.19.10","@modelcontextprotocol/sdk": "^1.29.0","@nats-io/nats-core": "3.0.2","@opentelemetry/api": "^1.9.0","@opentelemetry/core": "^2.9.0",+ "ai": "^7.0.44","dedent": "^1.7.2","openai": "^4.104.0","ulid": "^3.0.2",@@ -100,7 +105,6 @@"redis": "^5.10.0"},"devDependencies": {- "redis": "^5.10.0","@swc/core": "^1.11.0","@swc/jest": "^0.2.37","@types/jest": "^29.5.14",@@ -108,6 +112,7 @@"@types/ws": "^8.5.10","esbuild-plugin-file-path-extensions": "^2.1.4","jest": "^29.7.0",+ "redis": "^5.10.0","tsup": "^8.5.0","typescript": "^7.0.2"}diff --git a/packages/harness/src/core/index.ts b/packages/harness/src/core/index.tsindex 3e59525a1..71254f4b8 100644--- a/packages/harness/src/core/index.ts+++ b/packages/harness/src/core/index.ts@@ -82,9 +82,10 @@ export { ToolSet } from './mcp/ToolSet';// LLM contractsexport type { AgentMetadata, ILLM } from './llm/ILLM';-export { OpenAILLM } from './llm/OpenAILLM';export { ResponseFormatSchema, toOpenAIResponseFormat } from './llm/responseFormat';export type { ResponseFormat } from './llm/responseFormat';+export { VercelAILLM } from './llm/VercelAILLM';+export type { VercelAILLMConfig, VercelAIProviderConfig, VercelAIProviderName } from './llm/VercelAILLM';// Event contractsexport {diff --git a/packages/harness/src/core/llm/LLMTypes.ts b/packages/harness/src/core/llm/LLMTypes.tsindex 038953fe6..87168e332 100644--- a/packages/harness/src/core/llm/LLMTypes.ts+++ b/packages/harness/src/core/llm/LLMTypes.ts@@ -182,7 +182,7 @@ interface ExtendedChoice extends Omit<ChatCompletionChunk.Choice, 'delta'> {/*** Extended chat completion chunk that includes thought_signature in tool calls.- * `usage` is harness-normalized; OpenAILLM maps the gateway wire `usage` payload at the adapter boundary.+ * `usage` is harness-normalized; the LLM adapter maps the wire `usage` payload at the adapter boundary.*/export interface ExtendedChatCompletionChunk extends Omit<ChatCompletionChunk, 'choices' | 'usage'> {/**diff --git a/packages/harness/src/core/llm/OpenAILLM.ts b/packages/harness/src/core/llm/OpenAILLM.tsdeleted file mode 100644index e9f8b1263..000000000--- a/packages/harness/src/core/llm/OpenAILLM.ts+++ /dev/null@@ -1,286 +0,0 @@-import { z } from '@hono/zod-openapi';-import OpenAI from 'openai';-import type { RequestOptions } from 'openai/core';-import type {- ChatCompletionChunk,- ChatCompletionCreateParams,- ChatCompletionCreateParamsStreaming,-} from 'openai/resources/chat';-import type { Logger } from 'winston';-import { extractErrorLogFields } from '../util/errorLogFields';-import type { ILLM } from './ILLM';-import {- type CompletionUsage,- type ExtendedChatCompletionChunk,- getEmptyUsage,- type RawAssistantMessage,- type RawAssistantMessageWithUsage,-} from './LLMTypes';--/** Gateway wire usage. Private; normalized once into CompletionUsage. */-const OptionalGatewayTokenCountSchema = z.number().int().nonnegative().optional().catch(undefined);-const OptionalGatewayCostSchema = z.number().nonnegative().optional().catch(undefined);--const GatewayChatCompletionUsageSchema = z.object({- completion_tokens: OptionalGatewayTokenCountSchema,- prompt_tokens: OptionalGatewayTokenCountSchema,- total_tokens: OptionalGatewayTokenCountSchema,- completion_tokens_details: z- .object({- reasoning_tokens: OptionalGatewayTokenCountSchema,- })- .optional()- .catch(undefined),- prompt_tokens_details: z- .object({- cached_tokens: OptionalGatewayTokenCountSchema,- })- .optional()- .catch(undefined),- cache_read_input_tokens: OptionalGatewayTokenCountSchema,- cache_creation_input_tokens: OptionalGatewayTokenCountSchema,- costInUSD: OptionalGatewayCostSchema,-});--type GatewayChatCompletionUsage = z.infer<typeof GatewayChatCompletionUsageSchema>;--/** One-shot translation: gateway wire → canonical harness CompletionUsage. */-function normalizeGatewayUsage(usage: unknown): CompletionUsage {- const result = GatewayChatCompletionUsageSchema.safeParse(usage);- const parsed: GatewayChatCompletionUsage = result.success ? result.data : {};- return {- input_tokens: parsed.prompt_tokens ?? 0,- output_tokens: parsed.completion_tokens ?? 0,- total_tokens: parsed.total_tokens ?? 0,- cache_read_tokens: parsed.cache_read_input_tokens ?? parsed.prompt_tokens_details?.cached_tokens,- cache_write_tokens: parsed.cache_creation_input_tokens,- reasoning_tokens: parsed.completion_tokens_details?.reasoning_tokens,- cost_in_usd: parsed.costInUSD,- };-}--function toHarnessChunk(event: ChatCompletionChunk): ExtendedChatCompletionChunk {- const { usage, ...rest } = event;- if (usage == null) {- return rest;- }- return {- ...rest,- usage: normalizeGatewayUsage(usage),- };-}--/** Static headers, or a resolver invoked on every request. */-export type LLMHeaders = Record<string, string> | (() => Record<string, string>);--export interface OpenAILLMConfig {- /** Base URL of an OpenAI-compatible chat completions API. */- baseURL: string;- /** API key, sent as `Authorization: Bearer <key>`. */- apiKey: string;- /** Extra per-request headers (take precedence over client defaults). */- headers?: LLMHeaders | undefined;- logger: Logger;- signal?: AbortSignal | undefined;- /** Response header carrying the served model's context length, if the API exposes one. */- servedContextLengthHeader?: string | undefined;- onServedModelContextLength?: ((contextLength: number) => void) | undefined;-}--/** ILLM implementation for any OpenAI-compatible chat completions API. */-export class OpenAILLM implements ILLM {- private readonly client: OpenAI;- private readonly headers?: LLMHeaders | undefined;- private readonly logger: Logger;- private readonly signal?: AbortSignal | undefined;- private readonly servedContextLengthHeader?: string | undefined;- private readonly onServedModelContextLength?: ((contextLength: number) => void) | undefined;-- constructor(config: OpenAILLMConfig) {- this.client = new OpenAI({- baseURL: config.baseURL,- apiKey: config.apiKey,- });- this.headers = config.headers;- this.logger = config.logger;- this.signal = config.signal;- this.servedContextLengthHeader = config.servedContextLengthHeader;- this.onServedModelContextLength = config.onServedModelContextLength;- }-- private buildRequestOptions(): RequestOptions {- return {- headers: typeof this.headers === 'function' ? this.headers() : this.headers,- signal: this.signal,- };- }-- async *create(- body: ChatCompletionCreateParamsStreaming,- ): AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown> {- const newMessage: Partial<RawAssistantMessageWithUsage> & Pick<RawAssistantMessageWithUsage, 'output'> = {- output: {- role: 'assistant',- content: '',- },- };- let response;- let servedModelContextLengthHeader: string | null = null;-- try {- const { data, response: httpResponse } = await this.client.chat.completions- .create(body, this.buildRequestOptions())- .withResponse();- response = data;- if (this.servedContextLengthHeader) {- servedModelContextLengthHeader = httpResponse.headers.get(this.servedContextLengthHeader);- }- } catch (error) {- if (this.signal?.aborted) {- this.logger.debug(`LLM call aborted`, extractErrorLogFields(error));- } else {- this.logger.error(`Error creating chat completion`, extractErrorLogFields(error));- }- throw error;- }-- for await (const event of response) {- const chunk = toHarnessChunk(event);- yield chunk;- accumulateTokensFromChunk(chunk, newMessage, this.logger);- }- // Record after the stream drains so the last completed LLM call wins.- const servedModelContextLength = Number(servedModelContextLengthHeader);- if (servedModelContextLength > 0) {- this.onServedModelContextLength?.(servedModelContextLength);- }-- return {- usage: newMessage.usage ?? estimateTokensForAssistantMessage(body, newMessage.output, this.logger),- output: newMessage.output,- finish_reason: newMessage.finish_reason ?? null,- };- }-- async createNonStream(body: ChatCompletionCreateParams): Promise<RawAssistantMessageWithUsage> {- // TODO(agent): this is not really efficient. Rewrite this.- const llmStream = this.create({ ...body, stream: true });- let result = await llmStream.next();- while (!result.done) {- result = await llmStream.next();- }-- return result.value;- }-}--function estimateTokensForAssistantMessage(- _request: ChatCompletionCreateParamsStreaming,- _response: RawAssistantMessage,- logger: Logger,-): CompletionUsage {- // TODO(usage): fill this.- logger.error(`Did not recieve usage from LLM, estimation is not implemented.`);- return getEmptyUsage();-}--/**- * Accumulates tokens from a streaming chunk into the messages buffer.- * Handles content and tool calls accumulation for each choice in the chunk.- * Supports thought_signature for models like gemini-3-pro-preview.- */-function accumulateTokensFromChunk(- chunk: ExtendedChatCompletionChunk,- message: Partial<RawAssistantMessageWithUsage> & Pick<RawAssistantMessageWithUsage, 'output'>,- logger: Logger,-): void {- for (const choice of chunk.choices) {- const delta = choice.delta;- // Validate role only if it's explicitly present (typically only in first chunk)- // Most chunks won't have role in delta, which is normal- if (delta.role !== undefined && delta.role !== 'assistant') {- logger.warn(`Unexpected role: ${delta.role}`);- throw Error(`Unexpected role: ${delta.role}`);- }-- if (delta.content) {- const existing = typeof message.output.content === 'string' ? message.output.content : '';- message.output.content = existing + delta.content;- }- // ToDo (handle refusal in the message, currently not handled)-- if (delta.thinking_blocks) {- message.output.thinking_blocks ??= [];- for (const tb of delta.thinking_blocks) {- if (tb.type === 'thinking') {- const blocks = message.output.thinking_blocks;- const last = blocks[blocks.length - 1];- // Last thinking block without signature is still in-progress — append to it.- // Otherwise start a new block.- let current;- if (last?.type === 'thinking' && !last.signature) {- current = last;- } else {- current = { type: 'thinking' as const, thinking: '' };- blocks.push(current);- }- if (tb.thinking) {- current.thinking += tb.thinking;- }- if (tb.signature) {- current.signature = tb.signature;- }- } else {- // redacted_thinking- message.output.thinking_blocks.push(tb);- }- }- }-- if (delta.tool_calls) {- message.output.tool_calls ??= [];-- for (const toolCallDelta of delta.tool_calls) {- const toolCallIndex = toolCallDelta.index;- const functionDelta = toolCallDelta.function;-- // Initialize tool call if it doesn't exist- if (!message.output.tool_calls[toolCallIndex]) {- message.output.tool_calls[toolCallIndex] = {- id: toolCallDelta.id ?? '',- type: 'function',- function: {- name: functionDelta?.name ?? '',- arguments: functionDelta?.arguments ?? '',- },- ...(toolCallDelta.provider_specific_fields && {- provider_specific_fields: toolCallDelta.provider_specific_fields,- }),- };- } else {- const existingToolCall = message.output.tool_calls[toolCallIndex];- if (functionDelta?.name) {- existingToolCall.function.name = functionDelta.name;- }- if (functionDelta?.arguments) {- existingToolCall.function.arguments += functionDelta.arguments;- }- if (toolCallDelta.id) {- existingToolCall.id = toolCallDelta.id;- }- if (toolCallDelta.provider_specific_fields) {- existingToolCall.provider_specific_fields =- existingToolCall.provider_specific_fields ?? toolCallDelta.provider_specific_fields;- }- }- }- }- }- if (chunk.usage) {- message.usage = chunk.usage;- }- const finishReason = chunk.choices[0]?.finish_reason;- if (finishReason) {- message.finish_reason = finishReason;- }-}diff --git a/packages/harness/src/core/llm/VercelAILLM.ts b/packages/harness/src/core/llm/VercelAILLM.tsnew file mode 100644index 000000000..75b0c87c3--- /dev/null+++ b/packages/harness/src/core/llm/VercelAILLM.ts@@ -0,0 +1,1082 @@+import { createAnthropic } from '@ai-sdk/anthropic';+import { createGoogle } from '@ai-sdk/google';+import { createOpenAI } from '@ai-sdk/openai';+import { createOpenAICompatible } from '@ai-sdk/openai-compatible';+import type {+ AssistantContent,+ FilePart,+ FinishReason,+ JSONValue,+ LanguageModel,+ LanguageModelCallOptions,+ ModelMessage,+ ProviderMetadata,+ TextPart,+ TextStreamPart,+ ToolCallPart,+ ToolContent,+ ToolSet,+ UserContent,+} from 'ai';+import { jsonSchema, Output, streamText } from 'ai';+import type {+ ChatCompletionContentPart,+ ChatCompletionCreateParams,+ ChatCompletionCreateParamsStreaming,+ ChatCompletionMessageParam,+ ChatCompletionTool,+} from 'openai/resources/chat';+import type { Logger } from 'winston';+import { extractErrorLogFields } from '../util/errorLogFields';+import type { ILLM } from './ILLM';+import {+ type CompletionUsage,+ type ExtendedChatCompletionChunk,+ getEmptyUsage,+ type RawAssistantMessage,+ type RawAssistantMessageWithUsage,+ type ThinkingBlock,+} from './LLMTypes';++/** Structurally identical to ProviderOptions from @ai-sdk/provider-utils, not re-exported by `ai`. */+type ProviderOptions = ProviderMetadata;++/** Returns true for any non-null, non-array object — narrows to an indexable record. */+function isPlainObject(v: unknown): v is Record<string, unknown> {+ return typeof v === 'object' && v !== null && !Array.isArray(v);+}++/** Structurally matches ReasoningPart from @ai-sdk/provider-utils. */+interface ReasoningPart {+ type: 'reasoning';+ text: string;+ providerOptions?: ProviderOptions;+}++/** Providers with a dedicated code path in {@link buildLanguageModel}. */+export type VercelAIProviderName = 'openai' | 'anthropic' | 'google-gemini' | 'generic';++/** Structural config accepted by VercelAILLM; compatible with server's ProviderConfig. */+export interface VercelAIProviderConfig {+ provider: VercelAIProviderName;+ /** Display name / alias. Also used as the provider model identifier when `model_id` is absent. */+ name: string;+ /** Provider-facing model identifier, sent instead of `name` when present. */+ model_id?: string | undefined;+ /** Optional base URL override. Explicitly includes `undefined` for Zod-derived type compat. */+ base_url?: string | undefined;+ apiKey: string;+ headers: Record<string, string>;+ /** API format for the `generic` provider. Only option today; defaults when absent. */+ api_format?: 'openai-chat-completions' | undefined;+}++export interface VercelAILLMConfig {+ providerConfig: VercelAIProviderConfig;+ logger: Logger;+ signal?: AbortSignal;+}++export function buildLanguageModel(config: VercelAIProviderConfig): LanguageModel {+ const { provider, name, model_id, base_url, apiKey, headers } = config;+ const modelId = model_id ?? name;+ const extraHeaders = Object.keys(headers).length > 0 ? headers : undefined;++ switch (provider) {+ case 'openai': {+ const client = createOpenAI({+ apiKey,+ ...(base_url !== undefined ? { baseURL: base_url } : {}),+ ...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),+ });+ return client.responses(modelId);+ }+ case 'anthropic': {+ const client = createAnthropic({+ apiKey,+ ...(base_url !== undefined ? { baseURL: base_url } : {}),+ ...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),+ });+ return client(modelId);+ }+ case 'google-gemini': {+ const client = createGoogle({+ apiKey,+ ...(base_url !== undefined ? { baseURL: base_url } : {}),+ ...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),+ });+ return client(modelId);+ }+ case 'generic': {+ if (base_url === undefined) {+ throw new Error('Provider "generic" requires a base_url in models.yaml');+ }+ const client = createOpenAICompatible({+ name: 'generic',+ baseURL: base_url,+ apiKey,+ // Without this, @ai-sdk/openai-compatible silently downgrades json_schema requests+ // to a schema-less json_object, dropping the schema and strictJsonSchema.+ supportsStructuredOutputs: true,+ ...(extraHeaders !== undefined ? { headers: extraHeaders } : {}),+ });+ return client(modelId);+ }+ default: {+ const _exhaustive: never = provider;+ throw new Error(`Unknown provider "${String(_exhaustive)}"`);+ }+ }+}++/** Maps Vercel AI SDK LanguageModelUsage → harness CompletionUsage. */+export function normalizeUsage(usage: {+ inputTokens: number | undefined;+ outputTokens: number | undefined;+ inputTokenDetails: {+ cacheReadTokens: number | undefined;+ cacheWriteTokens: number | undefined;+ noCacheTokens: number | undefined;+ };+ outputTokenDetails: {+ reasoningTokens: number | undefined;+ textTokens: number | undefined;+ };+}): CompletionUsage {+ const input = usage.inputTokens ?? 0;+ const output = usage.outputTokens ?? 0;+ return {+ input_tokens: input,+ output_tokens: output,+ total_tokens: input + output,+ cache_read_tokens: usage.inputTokenDetails.cacheReadTokens ?? undefined,+ cache_write_tokens: usage.inputTokenDetails.cacheWriteTokens ?? undefined,+ reasoning_tokens: usage.outputTokenDetails.reasoningTokens ?? undefined,+ };+}++/**+ * The SDK's standardized cross-provider `streamText({ reasoning })` setting, distinct from the+ * `providerOptions` bucket. Derived from `LanguageModelCallOptions` so it stays in sync with the+ * SDK's own definition rather than being a hand-maintained mirror.+ */+type ReasoningLevel = NonNullable<LanguageModelCallOptions['reasoning']>;++const REASONING_LEVELS: readonly ReasoningLevel[] = [+ 'provider-default',+ 'none',+ 'minimal',+ 'low',+ 'medium',+ 'high',+ 'xhigh',+];++function isReasoningLevel(v: string): v is ReasoningLevel {+ return (REASONING_LEVELS as readonly string[]).includes(v);+}++/**+ * Maps a harness `reasoning_effort` to the SDK's standardized `reasoning` setting. Only+ * `google-gemini` needs this — `@ai-sdk/google` derives its `thinkingConfig` from it, since it has+ * no `providerOptions.google.reasoningEffort` lever like openai/anthropic/generic do.+ */+export function toReasoningLevel({+ provider,+ reasoningEffort,+}: {+ provider: VercelAIProviderName;+ reasoningEffort: string | undefined;+}): ReasoningLevel | undefined {+ if (provider !== 'google-gemini' || reasoningEffort === undefined || !isReasoningLevel(reasoningEffort)) {+ return undefined;+ }+ return reasoningEffort;+}++export function mapFinishReason(reason: FinishReason): RawAssistantMessageWithUsage['finish_reason'] {+ switch (reason) {+ case 'stop':+ return 'stop';+ case 'length':+ return 'length';+ case 'tool-calls':+ return 'tool_calls';+ case 'content-filter':+ return 'content_filter';+ // No harness equivalent for these — fall back to 'stop'.+ case 'error':+ case 'other':+ return 'stop';+ default: {+ const _exhaustive: never = reason;+ throw new Error(`Unknown finish reason: ${String(_exhaustive)}`);+ }+ }+}++/** Opaque alias — each provider bucket is an arbitrary JSON object from the request body. */+type JSONObject = Record<string, JSONValue | undefined>;++/**+ * Reads a single top-level field from rawBody by snake_case key and returns it as-is.+ * rawBody originates from a parsed JSON request body, so any value present is a valid JSONValue.+ */+function readBodyField(rawBody: unknown, key: string): JSONValue | undefined {+ const val: unknown = Reflect.get(Object(rawBody), key);+ return val !== undefined ? (val as JSONValue) : undefined;+}++const ANTHROPIC_BUDGET_BY_EFFORT: Record<string, number> = { low: 1024, medium: 8192, high: 32768 };++function openaiProviderOptions(+ rawBody: unknown,+ reasoningEffort: string | undefined,+ strictJsonSchema: boolean | undefined,+): JSONObject {+ const serviceTier = readBodyField(rawBody, 'service_tier');+ const user = readBodyField(rawBody, 'user');+ const promptCacheKey = readBodyField(rawBody, 'prompt_cache_key');+ const parallelToolCalls = readBodyField(rawBody, 'parallel_tool_calls');+ // Disables server-side storage and requests the encrypted reasoning token so multi-turn+ // conversations can replay reasoning statelessly; harmless no-op for non-reasoning models.+ return {+ store: false,+ include: ['reasoning.encrypted_content'],+ ...(reasoningEffort !== undefined ? { reasoningEffort } : {}),+ ...(strictJsonSchema !== undefined ? { strictJsonSchema } : {}),+ ...(serviceTier !== undefined ? { serviceTier } : {}),+ ...(user !== undefined ? { user } : {}),+ ...(promptCacheKey !== undefined ? { promptCacheKey } : {}),+ ...(parallelToolCalls !== undefined ? { parallelToolCalls } : {}),+ };+}++function anthropicProviderOptions(rawBody: unknown, reasoningEffort: string | undefined): JSONObject | undefined {+ const cacheControl = readBodyField(rawBody, 'cache_control');+ const disableParallelToolUse = readBodyField(rawBody, 'disable_parallel_tool_use');+ const opts: JSONObject = {+ ...(reasoningEffort !== undefined+ ? { thinking: { type: 'enabled', budgetTokens: ANTHROPIC_BUDGET_BY_EFFORT[reasoningEffort] ?? 8192 } }+ : {}),+ ...(cacheControl !== undefined ? { cacheControl } : {}),+ ...(disableParallelToolUse !== undefined ? { disableParallelToolUse } : {}),+ };+ return Object.keys(opts).length > 0 ? opts : undefined;+}++function googleProviderOptions(rawBody: unknown): JSONObject | undefined {+ const safetySettings = readBodyField(rawBody, 'safety_settings');+ const thinkingConfig = readBodyField(rawBody, 'thinking_config');+ const cachedContent = readBodyField(rawBody, 'cached_content');+ const opts: JSONObject = {+ ...(safetySettings !== undefined ? { safetySettings } : {}),+ ...(thinkingConfig !== undefined ? { thinkingConfig } : {}),+ ...(cachedContent !== undefined ? { cachedContent } : {}),+ };+ return Object.keys(opts).length > 0 ? opts : undefined;+}++function genericProviderOptions(+ rawBody: unknown,+ reasoningEffort: string | undefined,+ strictJsonSchema: boolean | undefined,+): JSONObject | undefined {+ const parallelToolCalls = readBodyField(rawBody, 'parallel_tool_calls');+ const opts: JSONObject = {+ ...(reasoningEffort !== undefined ? { reasoningEffort } : {}),+ ...(strictJsonSchema !== undefined ? { strictJsonSchema } : {}),+ ...(parallelToolCalls !== undefined ? { parallelToolCalls } : {}),+ };+ return Object.keys(opts).length > 0 ? opts : undefined;+}++/**+ * Builds provider-specific options for reasoning/thinking budget, structured-output strictness,+ * and any extra fields passed through from the request body via `rawBody`.+ *+ * `rawBody` is the raw request body; provider-specific fields (e.g. `cache_control`,+ * `service_tier`, `safety_settings`) are forwarded by snake_case key with no further validation.+ */+export function buildProviderOptions({+ config,+ reasoningEffort,+ structuredOutputSpec,+ rawBody,+}: {+ config: VercelAIProviderConfig;+ reasoningEffort: string | undefined;+ structuredOutputSpec: StructuredOutputSpec;+ rawBody: unknown;+}): ProviderOptions {+ const strictJsonSchema = structuredOutputSpec.mode === 'json_schema' ? structuredOutputSpec.strict : undefined;+ if (config.provider === 'openai') {+ return { openai: openaiProviderOptions(rawBody, reasoningEffort, strictJsonSchema) };+ } else if (config.provider === 'anthropic') {+ const anthropic = anthropicProviderOptions(rawBody, reasoningEffort);+ return anthropic !== undefined ? { anthropic } : {};+ } else if (config.provider === 'google-gemini') {+ const google = googleProviderOptions(rawBody);+ return google !== undefined ? { google } : {};+ } else {+ // 'generic' — @ai-sdk/openai-compatible registers under the name 'generic'.+ // this will have to become api_format specific in the future.+ const generic = genericProviderOptions(rawBody, reasoningEffort, strictJsonSchema);+ return generic !== undefined ? { generic } : {};+ }+}++/**+ * Prefers `max_completion_tokens`, falling back to the deprecated `max_tokens` (not part of the+ * OpenAI SDK type, read via `Reflect.get` to avoid an unsafe-member-access lint error).+ */+export function resolveMaxOutputTokens(+ body: Pick<ChatCompletionCreateParamsStreaming, 'max_completion_tokens'>,+): number | undefined {+ const limit: unknown = body.max_completion_tokens ?? Reflect.get(body, 'max_tokens');+ return typeof limit === 'number' ? limit : undefined;+}++/**+ * Parses the MIME type from a data URI (`data:<mime>;base64,...`).+ * Returns `undefined` when the URI is not a data URI or has no MIME segment.+ */+export function parseMimeFromDataUri(uri: string): string | undefined {+ const match = /^data:([^;,]+)/.exec(uri);+ return match?.[1];+}++/**+ * Builds a Vercel AI SDK FilePart from an OpenAI-format file content part.+ * Returns `undefined` when the part carries no usable data (e.g. file_id only,+ * which requires a server-side lookup we don't support here).+ */+export function toFilePart(file: {+ file_data?: string | undefined;+ filename?: string | undefined;+}): FilePart | undefined {+ const { file_data, filename } = file;+ if (!file_data) return undefined;+ const mediaType = parseMimeFromDataUri(file_data) ?? 'application/octet-stream';+ return {+ type: 'file',+ data: file_data,+ mediaType,+ ...(filename !== undefined ? { filename } : {}),+ };+}++/** Converts a user content array from OpenAI format to Vercel AI SDK UserContent. */+export function toUserContent(content: string | ChatCompletionContentPart[]): UserContent {+ if (typeof content === 'string') {+ return content;+ }+ const parts: UserContent = [];+ for (const part of content) {+ if (part.type === 'text') {+ parts.push({ type: 'text', text: part.text });+ continue;+ }+ if (part.type === 'image_url') {+ // Map image URLs/data-URIs as FilePart with image/* mediaType.+ // ImagePart ({ type: 'image' }) is deprecated in favour of FilePart.+ const url = part.image_url.url;+ const mediaType = parseMimeFromDataUri(url) ?? 'image/*';+ parts.push({ type: 'file', data: url, mediaType });+ continue;+ }+ if (part.type === 'file') {+ const filePart = toFilePart(part.file);+ if (filePart !== undefined) {+ parts.push(filePart);+ }+ // file_id-only parts are unsupported (require server-side lookup); skip.+ continue;+ }+ // input_audio — not universally supported across providers; omit.+ }+ return parts;+}++/**+ * Converts an assistant ChatCompletionMessageParam → AssistantModelMessage for multi-turn replay.+ *+ * The harness attaches `thinking_blocks` to assistant messages at runtime (via Object.assign in+ * AgentThread) for reasoning replay. Since `ChatCompletionMessageParam` does not declare this+ * field in its TypeScript type, we read it via Reflect.get so no assertion is needed.+ *+ * `provider` is used to place the opaque reasoning-replay token under the correct providerOptions key:+ * - `'openai'` / `'generic'` → `providerOptions[provider].encryptedContent` (Responses API)+ * - `'anthropic'` → `providerOptions.anthropic.signature`+ * - `'google-gemini'` → no standalone reasoning block; replay token is per-tool-call (see below)+ *+ * Independently of `provider`, a tool call's own `provider_specific_fields.thought_signature`+ * (Gemini attaches its thinking signature to the function-call part itself, not a separate+ * reasoning block) is placed at `providerOptions.google.thoughtSignature` on the `ToolCallPart` —+ * the convention `@ai-sdk/google` reads back out when replaying a tool call. Harmless no-op for+ * other providers, since this field is never populated for them.+ */+export function toAssistantModelMessage({+ msg,+ provider,+}: {+ msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }>;+ provider: VercelAIProviderName;+}): ModelMessage {+ const parts: AssistantContent = [];++ const rawThinking: unknown = Reflect.get(msg, 'thinking_blocks');+ if (Array.isArray(rawThinking)) {+ // Widen any[] → unknown[] so TypeScript's in-guards can narrow safely+ // without triggering no-unsafe-member-access on every property access.+ const blocks: unknown[] = rawThinking;+ for (const tb of blocks) {+ if (+ typeof tb === 'object' &&+ tb !== null &&+ 'type' in tb &&+ 'thinking' in tb &&+ tb.type === 'thinking' &&+ typeof tb.thinking === 'string'+ ) {+ const signature = 'signature' in tb && typeof tb.signature === 'string' ? tb.signature : undefined;+ let reasoningProviderOptions: ProviderOptions | undefined;+ if (signature !== undefined && provider !== 'google-gemini') {+ if (provider === 'openai' || provider === 'generic') {+ reasoningProviderOptions = { [provider]: { encryptedContent: signature } };+ } else {+ reasoningProviderOptions = { anthropic: { signature } };+ }+ }+ const reasoningPart: ReasoningPart = {+ type: 'reasoning',+ text: tb.thinking,+ ...(reasoningProviderOptions !== undefined ? { providerOptions: reasoningProviderOptions } : {}),+ };+ parts.push(reasoningPart);+ }+ }+ }++ const rawContent = msg.content;+ if (rawContent) {+ const text =+ typeof rawContent === 'string'+ ? rawContent+ : rawContent+ .filter((p): p is { type: 'text'; text: string } => p.type === 'text')+ .map(p => p.text)+ .join('');+ if (text) {+ const textPart: TextPart = { type: 'text', text };+ parts.push(textPart);+ }+ }++ if (msg.tool_calls?.length) {+ for (const tc of msg.tool_calls) {+ let input: Record<string, unknown> = {};+ try {+ const parsed: unknown = JSON.parse(tc.function.arguments);+ if (isPlainObject(parsed)) {+ input = parsed;+ }+ } catch {+ // malformed arguments — leave empty record+ }+ const rawProviderFields: unknown = Reflect.get(tc, 'provider_specific_fields');+ const thoughtSignature =+ isPlainObject(rawProviderFields) && typeof rawProviderFields['thought_signature'] === 'string'+ ? rawProviderFields['thought_signature']+ : undefined;+ const toolCallPart: ToolCallPart = {+ type: 'tool-call',+ toolCallId: tc.id,+ toolName: tc.function.name,+ input,+ ...(thoughtSignature !== undefined+ ? { providerOptions: { google: { thoughtSignature } } satisfies ProviderOptions }+ : {}),+ };+ parts.push(toolCallPart);+ }+ }++ return {+ role: 'assistant',+ content: parts.length > 0 ? parts : [{ type: 'text', text: '' }],+ };+}++/**+ * Converts OpenAI-format messages to Vercel AI SDK ModelMessage[].+ *+ * Builds a toolCallId → toolName lookup from assistant messages first so that+ * tool result messages (which only carry toolCallId) can include the name+ * required by ToolResultPart.+ *+ * `provider` is threaded into assistant message conversion so that reasoning+ * replay tokens are placed under the correct providerOptions key.+ */+export interface ConvertedMessages {+ instructions: string | undefined;+ messages: ModelMessage[];+}++/**+ * Splits an OpenAI message list into Vercel AI SDK format. System messages are extracted into+ * `instructions` since the SDK v7 no longer accepts `{ role: 'system' }` in the messages array.+ * Builds a toolCallId → toolName lookup first, since tool result messages only carry the id.+ */+export function convertMessages({+ messages,+ provider,+}: {+ messages: ChatCompletionMessageParam[];+ provider: VercelAIProviderName;+}): ConvertedMessages {+ const toolNameById = new Map<string, string>();+ for (const msg of messages) {+ if (msg.role === 'assistant' && msg.tool_calls) {+ for (const tc of msg.tool_calls) {+ toolNameById.set(tc.id, tc.function.name);+ }+ }+ }++ const systemParts: string[] = [];+ const result: ModelMessage[] = [];+ for (const msg of messages) {+ if (msg.role === 'system') {+ const content = typeof msg.content === 'string' ? msg.content : msg.content.map(p => p.text).join('');+ systemParts.push(content);+ continue;+ }+ if (msg.role === 'user') {+ result.push({ role: 'user', content: toUserContent(msg.content) });+ continue;+ }+ if (msg.role === 'assistant') {+ result.push(toAssistantModelMessage({ msg, provider }));+ continue;+ }+ if (msg.role === 'tool') {+ const toolName = toolNameById.get(msg.tool_call_id) ?? '';+ const toolContent: ToolContent = [+ {+ type: 'tool-result',+ toolCallId: msg.tool_call_id,+ toolName,+ output: {+ type: 'text',+ value: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content),+ },+ },+ ];+ result.push({ role: 'tool', content: toolContent });+ continue;+ }+ // 'developer' / 'function' roles — not used in the harness pipeline; skip.+ }+ return { instructions: systemParts.length > 0 ? systemParts.join('\n\n') : undefined, messages: result };+}++/** Converts OpenAI tool definitions to the Vercel AI SDK ToolSet. */+export function convertTools(tools: ChatCompletionTool[] | undefined): ToolSet | undefined {+ if (!tools?.length) {+ return undefined;+ }+ const toolSet: ToolSet = {};+ for (const t of tools) {+ toolSet[t.function.name] = {+ inputSchema: jsonSchema(t.function.parameters ?? { type: 'object', properties: {} }),+ ...(t.function.description !== undefined ? { description: t.function.description } : {}),+ };+ }+ return Object.keys(toolSet).length > 0 ? toolSet : undefined;+}++/**+ * Normalized structured-response mode, derived from an OpenAI-format `response_format`. Kept as+ * plain data rather than a Vercel AI SDK `Output` instance, since `Output` isn't a nameable+ * exported type from `ai` — see `buildOutput`.+ */+export type StructuredOutputSpec =+ | { mode: 'text' }+ | { mode: 'json' }+ | {+ mode: 'json_schema';+ schema: Record<string, unknown>;+ name: string;+ description: string | undefined;+ /** Mirrors `response_format.json_schema.strict`; consumed by `buildProviderOptions`. */+ strict: boolean | undefined;+ };++/** Converts an OpenAI-format `response_format` into a normalized `StructuredOutputSpec`. */+export function toStructuredOutputSpec(+ responseFormat: ChatCompletionCreateParamsStreaming['response_format'],+): StructuredOutputSpec {+ if (responseFormat === undefined || responseFormat.type === 'text') {+ return { mode: 'text' };+ }+ if (responseFormat.type === 'json_object') {+ return { mode: 'json' };+ }+ const { json_schema } = responseFormat;+ return {+ mode: 'json_schema',+ schema: json_schema.schema ?? { type: 'object', properties: {} },+ name: json_schema.name,+ description: json_schema.description,+ strict: json_schema.strict ?? undefined,+ };+}++/**+ * Builds the Vercel AI SDK `Output` spec for `streamText`. Not exported: `Output`'s type isn't+ * nameable outside `ai`, so an exported function can't declare it as a return type.+ */+function buildOutput(spec: StructuredOutputSpec) {+ switch (spec.mode) {+ case 'text':+ return undefined;+ case 'json':+ return Output.json();+ case 'json_schema':+ return Output.object({+ schema: jsonSchema(spec.schema),+ name: spec.name,+ ...(spec.description !== undefined ? { description: spec.description } : {}),+ });+ default: {+ const _exhaustive: never = spec;+ throw new Error(`Unknown structured output mode: ${JSON.stringify(_exhaustive)}`);+ }+ }+}++/**+ * Pure-data subset of `streamText(...)`'s call args. Composes `LanguageModelCallOptions` for the+ * model settings so new SDK settings are picked up automatically. Uses `?` (omitted keys) rather+ * than this file's usual explicit `| undefined`, since these fields cross directly into+ * `streamText`'s own optional-property call signature under `exactOptionalPropertyTypes`.+ */+export type StreamTextArgs = LanguageModelCallOptions & {+ model: LanguageModel;+ instructions?: string;+ messages: ModelMessage[];+ tools?: ToolSet;+ providerOptions?: ProviderOptions;+ abortSignal?: AbortSignal;+ maxRetries: number;+};++/**+ * Builds the pure-data subset of `streamText(...)`'s call args from already-computed+ * translations. Exported so the final wiring itself — not just the sub-functions that feed it+ * (`convertMessages`, `convertTools`, `buildProviderOptions`, `toReasoningLevel`,+ * `resolveMaxOutputTokens`) — is directly unit-testable: a value computed correctly but never+ * spread into this object wouldn't be caught by testing those sub-functions alone.+ *+ * `output` (from `buildOutput`) is deliberately excluded and spread separately by the caller:+ * its type isn't nameable outside `ai` (see `buildOutput`), so including it here would make this+ * exported function's return type unnameable too.+ */+export function buildStreamTextArgs(input: {+ model: LanguageModel;+ instructions: string | undefined;+ messages: ModelMessage[];+ tools: ToolSet | undefined;+ reasoning: ReasoningLevel | undefined;+ providerOptions: ProviderOptions;+ maxOutputTokens: number | undefined;+ temperature: number | null | undefined;+ topP: number | null | undefined;+ topK: number | null | undefined;+ presencePenalty: number | null | undefined;+ frequencyPenalty: number | null | undefined;+ stopSequences: string[] | null | undefined;+ seed: number | null | undefined;+ abortSignal: AbortSignal | undefined;+}): StreamTextArgs {+ const {+ model,+ instructions,+ messages,+ tools,+ reasoning,+ providerOptions,+ maxOutputTokens,+ temperature,+ topP,+ topK,+ presencePenalty,+ frequencyPenalty,+ stopSequences,+ seed,+ abortSignal,+ } = input;+ return {+ model,+ ...(instructions !== undefined ? { instructions } : {}),+ messages,+ ...(tools !== undefined ? { tools } : {}),+ ...(reasoning !== undefined ? { reasoning } : {}),+ ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),+ ...(temperature != null ? { temperature } : {}),+ ...(topP != null ? { topP } : {}),+ ...(topK != null ? { topK } : {}),+ ...(presencePenalty != null ? { presencePenalty } : {}),+ ...(frequencyPenalty != null ? { frequencyPenalty } : {}),+ ...(stopSequences != null ? { stopSequences } : {}),+ ...(seed != null ? { seed } : {}),+ ...(Object.keys(providerOptions).length > 0 ? { providerOptions } : {}),+ ...(abortSignal !== undefined ? { abortSignal } : {}),+ maxRetries: 0,+ };+}++interface ToolCallState {+ index: number;+ id: string;+ name: string;+ arguments: string;+ /** Gemini `thoughtSignature` from `providerMetadata.google` on the `tool-input-start` part. */+ thoughtSignature: string | undefined;+}++/** Synthetic chunk fields shared by every chunk emitted for a single stream. */+interface ChunkMeta {+ id: string;+ created: number;+ model: string;+}++/**+ * Converts a Vercel AI SDK stream of `TextStreamPart`s into harness-format+ * `ExtendedChatCompletionChunk`s, returning the final aggregated assistant message on completion.+ * Takes a plain `AsyncIterable` so hand-built fixtures work in tests too.+ */+export async function* mapStreamToChunks({+ stream,+ chunkMeta,+}: {+ stream: AsyncIterable<TextStreamPart<ToolSet>>;+ chunkMeta: ChunkMeta;+}): AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown> {+ const makeBase = (): Omit<ExtendedChatCompletionChunk, 'choices' | 'usage'> => ({+ id: chunkMeta.id,+ object: 'chat.completion.chunk' as const,+ created: chunkMeta.created,+ model: chunkMeta.model,+ });++ const toolCallStates = new Map<string, ToolCallState>();+ let nextToolIndex = 0;+ let accumulatedText = '';+ const accumulatedThinking: ThinkingBlock[] = [];+ let currentThinkingBlock: ThinkingBlock | null = null;+ let finalUsage: CompletionUsage = getEmptyUsage();+ let finalFinishReason: RawAssistantMessageWithUsage['finish_reason'] = 'stop';++ for await (const part of stream) {+ switch (part.type) {+ case 'text-delta': {+ accumulatedText += part.text;+ yield {+ ...makeBase(),+ choices: [+ {+ index: 0,+ delta: { content: part.text, role: 'assistant' },+ finish_reason: null,+ logprobs: null,+ },+ ],+ };+ break;+ }++ case 'reasoning-start': {+ currentThinkingBlock = { type: 'thinking', thinking: '' };+ accumulatedThinking.push(currentThinkingBlock);+ break;+ }++ case 'reasoning-delta': {+ if (currentThinkingBlock !== null) {+ currentThinkingBlock.thinking += part.text;+ }+ yield {+ ...makeBase(),+ choices: [+ {+ index: 0,+ delta: {+ thinking_blocks: [{ type: 'thinking', thinking: part.text }],+ reasoning_content: part.text,+ } satisfies ExtendedChatCompletionChunk['choices'][0]['delta'],+ finish_reason: null,+ logprobs: null,+ },+ ],+ };+ break;+ }++ case 'reasoning-end': {+ if (currentThinkingBlock !== null && part.providerMetadata !== undefined) {+ // Scan all provider namespace values — only the active provider's key will+ // be populated, so this is provider-agnostic without coupling to replayKey.+ // OpenAI Responses API → meta.reasoningEncryptedContent+ // Anthropic → meta.signature+ for (const meta of Object.values(part.providerMetadata)) {+ if ('reasoningEncryptedContent' in meta && typeof meta['reasoningEncryptedContent'] === 'string') {+ currentThinkingBlock.signature = meta['reasoningEncryptedContent'];+ break;+ }+ if ('signature' in meta && typeof meta['signature'] === 'string') {+ currentThinkingBlock.signature = meta['signature'];+ break;+ }+ }+ }+ currentThinkingBlock = null;+ break;+ }++ case 'tool-input-start': {+ const idx = nextToolIndex++;+ // @ai-sdk/google sets providerMetadata.google.thoughtSignature on tool-input-start for+ // Gemini thinking models. Capture it here so the finalized tool_call can carry+ // provider_specific_fields.thought_signature — the field toAssistantModelMessage reads+ // when replaying tool calls in multi-turn conversations.+ const googleMeta = part.providerMetadata?.['google'];+ const thoughtSignature =+ googleMeta !== undefined && typeof googleMeta['thoughtSignature'] === 'string'+ ? googleMeta['thoughtSignature']+ : undefined;+ toolCallStates.set(part.id, {+ index: idx,+ id: part.id,+ name: part.toolName,+ arguments: '',+ thoughtSignature,+ });+ yield {+ ...makeBase(),+ choices: [+ {+ index: 0,+ delta: {+ tool_calls: [+ {+ index: idx,+ id: part.id,+ type: 'function',+ function: { name: part.toolName, arguments: '' },+ },+ ],+ },+ finish_reason: null,+ logprobs: null,+ },+ ],+ };+ break;+ }++ case 'tool-input-delta': {+ const state = toolCallStates.get(part.id);+ if (state !== undefined) {+ state.arguments += part.delta;+ }+ const idx = state?.index ?? 0;+ yield {+ ...makeBase(),+ choices: [+ {+ index: 0,+ delta: {+ tool_calls: [{ index: idx, function: { arguments: part.delta } }],+ },+ finish_reason: null,+ logprobs: null,+ },+ ],+ };+ break;+ }++ case 'finish-step': {+ finalUsage = normalizeUsage(part.usage);+ finalFinishReason = mapFinishReason(part.finishReason);+ yield {+ ...makeBase(),+ choices: [+ {+ index: 0,+ delta: {},+ finish_reason: finalFinishReason,+ logprobs: null,+ },+ ],+ usage: finalUsage,+ };+ break;+ }++ case 'error': {+ const raw = part.error;+ const err = raw instanceof Error ? raw : new Error(String(raw));+ throw new Error('LLM stream error', { cause: err });+ }++ case 'start':+ case 'text-start':+ case 'text-end':+ case 'custom':+ case 'tool-input-end':+ case 'source':+ case 'file':+ case 'reasoning-file':+ case 'tool-call':+ case 'tool-result':+ case 'tool-error':+ case 'tool-output-denied':+ case 'tool-approval-request':+ case 'tool-approval-response':+ case 'start-step':+ case 'finish':+ case 'abort':+ case 'raw':+ // Structural/bookkeeping events — no harness chunk is emitted for these.+ break;++ default: {+ const _exhaustive: never = part;+ throw new Error(`Unknown stream part type: ${JSON.stringify(_exhaustive)}`);+ }+ }+ }++ const toolCalls = [...toolCallStates.values()]+ .sort((a, b) => a.index - b.index)+ .map(tc => ({+ id: tc.id,+ type: 'function' as const,+ function: { name: tc.name, arguments: tc.arguments },+ ...(tc.thoughtSignature !== undefined+ ? { provider_specific_fields: { thought_signature: tc.thoughtSignature } }+ : {}),+ }));++ const output: RawAssistantMessage = {+ role: 'assistant',+ content: accumulatedText || null,+ ...(accumulatedThinking.length > 0 ? { thinking_blocks: accumulatedThinking } : {}),+ ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),+ };++ return { usage: finalUsage, output, finish_reason: finalFinishReason };+}++/** ILLM implementation backed by Vercel AI SDK, supporting multiple LLM providers. */+export class VercelAILLM implements ILLM {+ private readonly config: VercelAILLMConfig;+ private readonly logger: Logger;+ private readonly signal: AbortSignal | undefined;++ constructor(config: VercelAILLMConfig) {+ this.config = config;+ this.logger = config.logger;+ this.signal = config.signal;+ }++ async *create(+ body: ChatCompletionCreateParamsStreaming,+ ): AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown> {+ const { providerConfig } = this.config;+ const model = buildLanguageModel(providerConfig);++ const { instructions, messages } = convertMessages({ messages: body.messages, provider: providerConfig.provider });+ const tools = convertTools(body.tools ?? undefined);+ const structuredOutputSpec = toStructuredOutputSpec(body.response_format);+ const output = buildOutput(structuredOutputSpec);++ // reasoning_effort is injected by AgentThread via Object.assign; not in the SDK type.+ const rawReasoningEffort: unknown = Reflect.get(body, 'reasoning_effort');+ const reasoningEffort = typeof rawReasoningEffort === 'string' ? rawReasoningEffort : undefined;+ const providerOptions = buildProviderOptions({+ config: providerConfig,+ reasoningEffort,+ structuredOutputSpec,+ rawBody: body,+ });+ const reasoning = toReasoningLevel({ provider: providerConfig.provider, reasoningEffort });++ // top_k is injected by AgentThread via Object.assign; not in the SDK type.+ const rawTopK: unknown = Reflect.get(body, 'top_k');+ const streamTextArgs = buildStreamTextArgs({+ model,+ instructions,+ messages,+ tools,+ reasoning,+ providerOptions,+ maxOutputTokens: resolveMaxOutputTokens(body),+ temperature: body.temperature,+ topP: body.top_p,+ topK: typeof rawTopK === 'number' ? rawTopK : null,+ presencePenalty: body.presence_penalty,+ frequencyPenalty: body.frequency_penalty,+ stopSequences: typeof body.stop === 'string' ? [body.stop] : (body.stop ?? null),+ seed: body.seed,+ abortSignal: this.signal,+ });++ let streamResult;+ try {+ streamResult = streamText({+ ...streamTextArgs,+ ...(output !== undefined ? { output } : {}),+ });+ } catch (error) {+ if (this.signal?.aborted) {+ this.logger.debug('LLM call aborted', extractErrorLogFields(error));+ } else {+ this.logger.error('Error creating streaming chat completion', extractErrorLogFields(error));+ }+ throw error;+ }++ // Synthetic chunk fields; id is unique per stream instance.+ const chunkMeta: ChunkMeta = {+ id: `vc-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 7)}`,+ created: Math.floor(Date.now() / 1000),+ model: body.model,+ };++ try {+ return yield* mapStreamToChunks({ stream: streamResult.stream, chunkMeta });+ } catch (error) {+ if (this.signal?.aborted) {+ this.logger.debug('LLM stream aborted', extractErrorLogFields(error));+ } else {+ this.logger.error('Error reading LLM stream', extractErrorLogFields(error));+ }+ throw error;+ }+ }++ async createNonStream(body: ChatCompletionCreateParams): Promise<RawAssistantMessageWithUsage> {+ // Drain the streaming generator — reuses the same accumulation and error handling path.+ const llmStream = this.create({ ...body, stream: true });+ let result = await llmStream.next();+ while (!result.done) {+ result = await llmStream.next();+ }+ return result.value;+ }+}diff --git a/packages/harness/tests/core/llm/VercelAILLM.messages.test.ts b/packages/harness/tests/core/llm/VercelAILLM.messages.test.tsnew file mode 100644index 000000000..43f19a14c--- /dev/null+++ b/packages/harness/tests/core/llm/VercelAILLM.messages.test.ts@@ -0,0 +1,388 @@+/**+ * Conversion of OpenAI-format messages → Vercel AI SDK ModelMessage[]:+ * user content parts, assistant messages (text + tool_calls + reasoning replay),+ * message list assembly, and tool definitions.+ */+import type { ChatCompletionMessageParam, ChatCompletionTool } from 'openai/resources/chat';+import {+ convertMessages,+ convertTools,+ parseMimeFromDataUri,+ toAssistantModelMessage,+ toFilePart,+ toUserContent,+} from '../../../src/core/llm/VercelAILLM';++// ─────────── parseMimeFromDataUri ───────────++describe('parseMimeFromDataUri', () => {+ it('returns the MIME type from a data URI', () => {+ expect(parseMimeFromDataUri('data:image/png;base64,abc')).toBe('image/png');+ expect(parseMimeFromDataUri('data:application/pdf;base64,xyz')).toBe('application/pdf');+ });++ it('returns undefined for a non-data URI', () => {+ expect(parseMimeFromDataUri('https://example.com/img.png')).toBeUndefined();+ expect(parseMimeFromDataUri('')).toBeUndefined();+ });++ it('returns undefined when there is no MIME segment', () => {+ expect(parseMimeFromDataUri('data:;base64,abc')).toBeUndefined();+ });+});++// ─────────── toFilePart ───────────++describe('toFilePart', () => {+ it('returns undefined when file_data is absent', () => {+ expect(toFilePart({})).toBeUndefined();+ expect(toFilePart({ filename: 'doc.pdf' })).toBeUndefined();+ });++ it('parses mediaType from a data URI', () => {+ const part = toFilePart({ file_data: 'data:application/pdf;base64,abc', filename: 'doc.pdf' });+ expect(part).toEqual({+ type: 'file',+ data: 'data:application/pdf;base64,abc',+ mediaType: 'application/pdf',+ filename: 'doc.pdf',+ });+ });++ it('falls back to application/octet-stream when no MIME in data URI', () => {+ const part = toFilePart({ file_data: 'data:;base64,abc' });+ expect(part?.mediaType).toBe('application/octet-stream');+ });++ it('omits filename when not provided', () => {+ const part = toFilePart({ file_data: 'data:image/jpeg;base64,abc' });+ expect(part).not.toHaveProperty('filename');+ });+});++// ─────────── toUserContent ───────────++describe('toUserContent', () => {+ it('passes a string through unchanged', () => {+ expect(toUserContent('hello')).toBe('hello');+ });++ it('converts text parts', () => {+ const result = toUserContent([{ type: 'text', text: 'hi' }]);+ expect(result).toEqual([{ type: 'text', text: 'hi' }]);+ });++ it('converts image_url with a data URI (parses mediaType)', () => {+ const result = toUserContent([{ type: 'image_url', image_url: { url: 'data:image/png;base64,abc' } }]);+ expect(result).toEqual([{ type: 'file', data: 'data:image/png;base64,abc', mediaType: 'image/png' }]);+ });++ it('falls back to image/* for non-data image URLs', () => {+ const result = toUserContent([{ type: 'image_url', image_url: { url: 'https://example.com/img.png' } }]);+ expect(result).toEqual([{ type: 'file', data: 'https://example.com/img.png', mediaType: 'image/*' }]);+ });++ it('converts file parts with file_data', () => {+ const result = toUserContent([+ { type: 'file', file: { file_data: 'data:application/pdf;base64,xyz', filename: 'a.pdf' } },+ ]);+ expect(result).toEqual([+ { type: 'file', data: 'data:application/pdf;base64,xyz', mediaType: 'application/pdf', filename: 'a.pdf' },+ ]);+ });++ it('skips file parts with only file_id (no file_data)', () => {+ const result = toUserContent([{ type: 'file', file: { file_id: 'file-123' } }]);+ expect(result).toEqual([]);+ });++ it('skips input_audio parts', () => {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- test fixture for an unsupported content part type+ const result = toUserContent([{ type: 'input_audio', input_audio: { data: 'abc', format: 'mp3' } } as any]);+ expect(result).toEqual([]);+ });++ it('handles mixed content parts in order', () => {+ const result = toUserContent([+ { type: 'text', text: 'caption' },+ { type: 'image_url', image_url: { url: 'https://example.com/img.png' } },+ ]);+ expect(result).toHaveLength(2);+ expect((result as Array<{ type: string }>)[0]?.type).toBe('text');+ expect((result as Array<{ type: string }>)[1]?.type).toBe('file');+ });+});++// ─────────── toAssistantModelMessage ───────────++describe('toAssistantModelMessage', () => {+ it('produces a text part for string content', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: 'hello',+ };+ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ expect(result.role).toBe('assistant');+ expect(result.content).toEqual([{ type: 'text', text: 'hello' }]);+ });++ it('joins array text content parts into one text part', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: [+ { type: 'text', text: 'a' },+ { type: 'text', text: 'b' },+ ],+ };+ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ expect(result.content).toEqual([{ type: 'text', text: 'ab' }]);+ });++ it('falls back to empty text part when content is null/empty', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ expect(result.content).toEqual([{ type: 'text', text: '' }]);+ });++ it('converts tool_calls with parsed JSON arguments', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ tool_calls: [{ id: 'call-1', type: 'function', function: { name: 'my_tool', arguments: '{"x":1}' } }],+ };+ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ const toolCallPart = (result.content as Array<{ type: string }>).find(p => p.type === 'tool-call');+ expect(toolCallPart).toMatchObject({+ type: 'tool-call',+ toolCallId: 'call-1',+ toolName: 'my_tool',+ input: { x: 1 },+ });+ });++ it('falls back to empty input object for malformed tool_call arguments', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ tool_calls: [{ id: 'call-2', type: 'function', function: { name: 'bad', arguments: 'not-json' } }],+ };+ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ const toolCallPart = (result.content as Array<{ type: string; input?: unknown }>).find(p => p.type === 'tool-call');+ expect(toolCallPart?.input).toEqual({});+ });++ it('places thinking_blocks as reasoning parts — openai provider → encryptedContent', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(msg, 'thinking_blocks', [{ type: 'thinking', thinking: 'step one', signature: 'sig-openai' }]);++ const result = toAssistantModelMessage({ msg: msg, provider: 'openai' });+ const reasoningPart = (result.content as Array<{ type: string; text?: string; providerOptions?: unknown }>).find(+ p => p.type === 'reasoning',+ );+ expect(reasoningPart?.text).toBe('step one');+ expect(reasoningPart?.providerOptions).toEqual({ openai: { encryptedContent: 'sig-openai' } });+ });++ it('places thinking_blocks as reasoning parts — anthropic provider → signature', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(msg, 'thinking_blocks', [{ type: 'thinking', thinking: 'step two', signature: 'sig-ant' }]);++ const result = toAssistantModelMessage({ msg: msg, provider: 'anthropic' });+ const reasoningPart = (result.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'reasoning',+ );+ expect(reasoningPart?.providerOptions).toEqual({ anthropic: { signature: 'sig-ant' } });+ });++ it('places thinking_blocks as reasoning parts — generic provider → encryptedContent', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(msg, 'thinking_blocks', [{ type: 'thinking', thinking: 'step', signature: 'sig-gen' }]);++ const result = toAssistantModelMessage({ msg: msg, provider: 'generic' });+ const reasoningPart = (result.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'reasoning',+ );+ expect(reasoningPart?.providerOptions).toEqual({ generic: { encryptedContent: 'sig-gen' } });+ });++ it('omits providerOptions on reasoning part when provider is google-gemini', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(msg, 'thinking_blocks', [{ type: 'thinking', thinking: 'step', signature: 'sig' }]);++ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ const reasoningPart = (result.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'reasoning',+ );+ expect(reasoningPart).toBeDefined();+ expect(reasoningPart).not.toHaveProperty('providerOptions');+ });++ it('places provider_specific_fields.thought_signature → providerOptions.google.thoughtSignature on tool-call', () => {+ const toolCall = { id: 'call-1', type: 'function' as const, function: { name: 'fn', arguments: '{}' } };+ Reflect.set(toolCall, 'provider_specific_fields', { thought_signature: 'gemini-sig' });+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ tool_calls: [toolCall],+ };++ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ const toolCallPart = (result.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'tool-call',+ );+ expect(toolCallPart?.providerOptions).toEqual({ google: { thoughtSignature: 'gemini-sig' } });+ });++ it('omits providerOptions on tool-call when no thought_signature', () => {+ const msg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ tool_calls: [{ id: 'call-1', type: 'function', function: { name: 'fn', arguments: '{}' } }],+ };++ const result = toAssistantModelMessage({ msg: msg, provider: 'google-gemini' });+ const toolCallPart = (result.content as Array<{ type: string }>).find(p => p.type === 'tool-call');+ expect(toolCallPart).not.toHaveProperty('providerOptions');+ });+});++// ─────────── convertMessages ───────────++describe('convertMessages', () => {+ it('extracts system messages as instructions (joined with double newlines)', () => {+ const messages: ChatCompletionMessageParam[] = [+ { role: 'system', content: 'Prompt A' },+ { role: 'system', content: 'Prompt B' },+ { role: 'user', content: 'hi' },+ ];+ const { instructions, messages: result } = convertMessages({ messages: messages, provider: 'google-gemini' });+ expect(instructions).toBe('Prompt A\n\nPrompt B');+ expect(result).toHaveLength(1);+ expect(result[0]).toMatchObject({ role: 'user' });+ });++ it('returns undefined instructions when there are no system messages', () => {+ const { instructions } = convertMessages({+ messages: [{ role: 'user', content: 'hi' }],+ provider: 'google-gemini',+ });+ expect(instructions).toBeUndefined();+ });++ it('preserves user/assistant/tool message ordering', () => {+ const messages: ChatCompletionMessageParam[] = [+ { role: 'user', content: 'q1' },+ {+ role: 'assistant',+ content: 'a1',+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'fn', arguments: '{}' } }],+ },+ { role: 'tool', tool_call_id: 'c1', content: 'result' },+ ];+ const { messages: result } = convertMessages({ messages: messages, provider: 'google-gemini' });+ expect(result).toHaveLength(3);+ expect(result[0]?.role).toBe('user');+ expect(result[1]?.role).toBe('assistant');+ expect(result[2]?.role).toBe('tool');+ });++ it('looks up toolName for tool result messages from assistant tool_calls', () => {+ const messages: ChatCompletionMessageParam[] = [+ {+ role: 'assistant',+ content: null,+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'search', arguments: '{}' } }],+ },+ { role: 'tool', tool_call_id: 'c1', content: 'found it' },+ ];+ const { messages: result } = convertMessages({ messages: messages, provider: 'google-gemini' });+ const toolMsg = result.find(m => m.role === 'tool');+ expect(toolMsg?.content).toEqual([expect.objectContaining({ toolName: 'search' })]);+ });++ it('falls back to empty string toolName for unresolvable tool_call_id', () => {+ const messages: ChatCompletionMessageParam[] = [{ role: 'tool', tool_call_id: 'unknown-id', content: 'data' }];+ const { messages: result } = convertMessages({ messages: messages, provider: 'google-gemini' });+ expect(result[0]?.content).toEqual([expect.objectContaining({ toolName: '' })]);+ });++ it('skips developer and function role messages', () => {+ const messages: ChatCompletionMessageParam[] = [+ { role: 'user', content: 'x' },+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- simulating unrecognised roles at runtime+ { role: 'developer', content: 'internal' } as any,+ ];+ const { messages: result } = convertMessages({ messages: messages, provider: 'google-gemini' });+ expect(result).toHaveLength(1);+ expect(result[0]?.role).toBe('user');+ });++ it('threads the provider into assistant message reasoning parts', () => {+ const msg: ChatCompletionMessageParam = { role: 'assistant', content: null };+ Reflect.set(msg, 'thinking_blocks', [{ type: 'thinking', thinking: 'thought', signature: 'sig' }]);++ const { messages: result } = convertMessages({ messages: [msg], provider: 'anthropic' });+ const assistantMsg = result.find(m => m.role === 'assistant');+ const reasoningPart = (+ assistantMsg?.content as Array<{ type: string; providerOptions?: unknown }> | undefined+ )?.find(p => p.type === 'reasoning');+ expect(reasoningPart?.providerOptions).toEqual({ anthropic: { signature: 'sig' } });+ });+});++// ─────────── convertTools ───────────++describe('convertTools', () => {+ it('returns undefined for undefined input', () => {+ expect(convertTools(undefined)).toBeUndefined();+ });++ it('returns undefined for an empty array', () => {+ expect(convertTools([])).toBeUndefined();+ });++ it('converts a valid tool definition', () => {+ const tools: ChatCompletionTool[] = [+ {+ type: 'function',+ function: {+ name: 'search',+ description: 'Search the web',+ parameters: { type: 'object', properties: { q: { type: 'string' } } },+ },+ },+ ];+ const toolSet = convertTools(tools);+ expect(toolSet).toBeDefined();+ expect(toolSet?.['search']).toBeDefined();+ expect(toolSet?.['search']?.description).toBe('Search the web');+ expect(toolSet?.['search']?.inputSchema).toBeDefined();+ });++ it('falls back to empty object schema when parameters is absent', () => {+ const tools: ChatCompletionTool[] = [{ type: 'function', function: { name: 'noop' } }];+ const toolSet = convertTools(tools);+ expect(toolSet?.['noop']?.inputSchema).toBeDefined();+ });++ it('omits description when not provided', () => {+ const tools: ChatCompletionTool[] = [{ type: 'function', function: { name: 'silent' } }];+ const toolSet = convertTools(tools);+ expect(toolSet?.['silent']).not.toHaveProperty('description');+ });+});diff --git a/packages/harness/tests/core/llm/VercelAILLM.model.test.ts b/packages/harness/tests/core/llm/VercelAILLM.model.test.tsnew file mode 100644index 000000000..e08d41951--- /dev/null+++ b/packages/harness/tests/core/llm/VercelAILLM.model.test.ts@@ -0,0 +1,221 @@+/**+ * LanguageModel construction and streamText call-args assembly:+ * buildLanguageModel (per-provider construction + capability flags),+ * resolveMaxOutputTokens, and buildStreamTextArgs (key presence under+ * exactOptionalPropertyTypes).+ */+import type { VercelAIProviderConfig } from '../../../src/core/llm/VercelAILLM';+import {+ buildLanguageModel,+ buildStreamTextArgs,+ convertTools,+ resolveMaxOutputTokens,+} from '../../../src/core/llm/VercelAILLM';++function makeConfig(+ overrides: Partial<VercelAIProviderConfig> & { provider: VercelAIProviderConfig['provider'] },+): VercelAIProviderConfig {+ return { name: 'test', apiKey: 'sk-test', headers: {}, ...overrides };+}++// ─────────── buildLanguageModel ───────────++describe('buildLanguageModel', () => {+ it('throws for generic provider without base_url', () => {+ const config = makeConfig({ provider: 'generic' });+ expect(() => buildLanguageModel(config)).toThrow('base_url');+ });++ it('throws for unknown provider string (exhaustive default branch)', () => {+ const badConfig = JSON.parse('{"provider":"unknown-provider","name":"x","apiKey":"k","headers":{}}');+ expect(() => buildLanguageModel(badConfig)).toThrow('Unknown provider');+ });++ it('constructs openai model without throwing', () => {+ const model = buildLanguageModel(makeConfig({ provider: 'openai' }));+ expect(model).toBeDefined();+ });++ it('constructs anthropic model without throwing', () => {+ const model = buildLanguageModel(makeConfig({ provider: 'anthropic' }));+ expect(model).toBeDefined();+ });++ it('constructs google-gemini model without throwing', () => {+ const model = buildLanguageModel(makeConfig({ provider: 'google-gemini' }));+ expect(model).toBeDefined();+ });++ it('constructs generic model without throwing when base_url is provided', () => {+ const model = buildLanguageModel(makeConfig({ provider: 'generic', base_url: 'http://localhost:11434/v1' }));+ expect(model).toBeDefined();+ });++ it('sets supportsStructuredOutputs:true on the generic model instance', () => {+ const model: unknown = buildLanguageModel(+ makeConfig({ provider: 'generic', base_url: 'http://localhost:11434/v1' }),+ );+ if (typeof model !== 'object' || model === null) throw new Error('Expected model to be an object');+ expect(Reflect.get(model, 'supportsStructuredOutputs')).toBe(true);+ });++ it('uses model_id over name when provided', () => {+ const model: unknown = buildLanguageModel(+ makeConfig({ provider: 'anthropic', name: 'alias', model_id: 'claude-sonnet-5' }),+ );+ if (typeof model !== 'object' || model === null) throw new Error('Expected model to be an object');+ expect(Reflect.get(model, 'modelId')).toBe('claude-sonnet-5');+ });+});++// ─────────── resolveMaxOutputTokens ───────────++describe('resolveMaxOutputTokens', () => {+ it('returns max_completion_tokens when set', () => {+ expect(resolveMaxOutputTokens({ max_completion_tokens: 512 })).toBe(512);+ });++ it('falls back to max_tokens when max_completion_tokens is absent', () => {+ const body = JSON.parse('{"max_tokens":256}');+ expect(resolveMaxOutputTokens(body)).toBe(256);+ });++ it('prefers max_completion_tokens over max_tokens', () => {+ const body = JSON.parse('{"max_completion_tokens":512,"max_tokens":256}');+ expect(resolveMaxOutputTokens(body)).toBe(512);+ });++ it('returns undefined when neither is set', () => {+ expect(resolveMaxOutputTokens({})).toBeUndefined();+ });++ it('returns undefined for non-numeric values', () => {+ const body = JSON.parse('{"max_completion_tokens":"not-a-number"}');+ expect(resolveMaxOutputTokens(body)).toBeUndefined();+ });+});++// ─────────── buildStreamTextArgs ───────────++describe('buildStreamTextArgs', () => {+ const model = buildLanguageModel(makeConfig({ provider: 'anthropic' }));+ const messages = [{ role: 'user' as const, content: 'hi' }];++ it('includes all provided optional fields as present keys', () => {+ const tools = convertTools([{ type: 'function', function: { name: 'search', description: 'Search' } }]);+ const result = buildStreamTextArgs({+ model,+ instructions: 'You are helpful.',+ messages,+ tools,+ reasoning: 'high',+ providerOptions: { anthropic: { thinking: { type: 'enabled', budgetTokens: 8192 } } },+ maxOutputTokens: 1024,+ temperature: 0.7,+ topP: 0.9,+ topK: 40,+ presencePenalty: 0.1,+ frequencyPenalty: 0.2,+ stopSequences: ['STOP'],+ seed: 42,+ abortSignal: new AbortController().signal,+ });++ expect('instructions' in result).toBe(true);+ expect('tools' in result).toBe(true);+ expect('reasoning' in result).toBe(true);+ expect('maxOutputTokens' in result).toBe(true);+ expect('temperature' in result).toBe(true);+ expect('topP' in result).toBe(true);+ expect('topK' in result).toBe(true);+ expect('presencePenalty' in result).toBe(true);+ expect('frequencyPenalty' in result).toBe(true);+ expect('stopSequences' in result).toBe(true);+ expect('seed' in result).toBe(true);+ expect('providerOptions' in result).toBe(true);+ expect('abortSignal' in result).toBe(true);+ expect(result.maxRetries).toBe(0);+ });++ it('omits optional keys (not just undefined-valued) when input is absent', () => {+ const result = buildStreamTextArgs({+ model,+ instructions: undefined,+ messages,+ tools: undefined,+ reasoning: undefined,+ providerOptions: {},+ maxOutputTokens: undefined,+ temperature: undefined,+ topP: undefined,+ topK: undefined,+ presencePenalty: undefined,+ frequencyPenalty: undefined,+ stopSequences: undefined,+ seed: undefined,+ abortSignal: undefined,+ });++ expect('instructions' in result).toBe(false);+ expect('tools' in result).toBe(false);+ expect('reasoning' in result).toBe(false);+ expect('maxOutputTokens' in result).toBe(false);+ expect('temperature' in result).toBe(false);+ expect('topP' in result).toBe(false);+ expect('topK' in result).toBe(false);+ expect('presencePenalty' in result).toBe(false);+ expect('frequencyPenalty' in result).toBe(false);+ expect('stopSequences' in result).toBe(false);+ expect('seed' in result).toBe(false);+ expect('providerOptions' in result).toBe(false);+ expect('abortSignal' in result).toBe(false);+ });++ it('omits providerOptions when the object is empty', () => {+ const result = buildStreamTextArgs({+ model,+ instructions: undefined,+ messages,+ tools: undefined,+ reasoning: undefined,+ providerOptions: {},+ maxOutputTokens: undefined,+ temperature: undefined,+ topP: undefined,+ topK: undefined,+ presencePenalty: undefined,+ frequencyPenalty: undefined,+ stopSequences: undefined,+ seed: undefined,+ abortSignal: undefined,+ });+ expect('providerOptions' in result).toBe(false);+ });++ it('omits temperature/topP/topK/presencePenalty/frequencyPenalty/stopSequences/seed when null', () => {+ const result = buildStreamTextArgs({+ model,+ instructions: undefined,+ messages,+ tools: undefined,+ reasoning: undefined,+ providerOptions: {},+ maxOutputTokens: undefined,+ temperature: null,+ topP: null,+ topK: null,+ presencePenalty: null,+ frequencyPenalty: null,+ stopSequences: null,+ seed: null,+ abortSignal: undefined,+ });+ expect('temperature' in result).toBe(false);+ expect('topP' in result).toBe(false);+ expect('topK' in result).toBe(false);+ expect('presencePenalty' in result).toBe(false);+ expect('frequencyPenalty' in result).toBe(false);+ expect('stopSequences' in result).toBe(false);+ expect('seed' in result).toBe(false);+ });+});diff --git a/packages/harness/tests/core/llm/VercelAILLM.output.test.ts b/packages/harness/tests/core/llm/VercelAILLM.output.test.tsnew file mode 100644index 000000000..2cac89e02--- /dev/null+++ b/packages/harness/tests/core/llm/VercelAILLM.output.test.ts@@ -0,0 +1,420 @@+/**+ * Conversion of OpenAI response_format → StructuredOutputSpec, and assembly of+ * provider-specific options (reasoning effort, strictJsonSchema) for each provider.+ */+import type { StructuredOutputSpec, VercelAIProviderConfig } from '../../../src/core/llm/VercelAILLM';+import { buildProviderOptions, toReasoningLevel, toStructuredOutputSpec } from '../../../src/core/llm/VercelAILLM';++function makeConfig(+ overrides: Partial<VercelAIProviderConfig> & { provider: VercelAIProviderConfig['provider'] },+): VercelAIProviderConfig {+ return { name: 'test', apiKey: 'sk-test', headers: {}, ...overrides };+}++// ─────────── toStructuredOutputSpec ───────────++describe('toStructuredOutputSpec', () => {+ it('returns text mode when response_format is undefined', () => {+ expect(toStructuredOutputSpec(undefined)).toEqual({ mode: 'text' });+ });++ it('returns text mode for type:text', () => {+ expect(toStructuredOutputSpec({ type: 'text' })).toEqual({ mode: 'text' });+ });++ it('returns json mode for type:json_object', () => {+ expect(toStructuredOutputSpec({ type: 'json_object' })).toEqual({ mode: 'json' });+ });++ it('returns json_schema mode with full spec', () => {+ const result = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: {+ name: 'MySchema',+ description: 'desc',+ schema: { type: 'object', properties: { x: { type: 'string' } } },+ strict: true,+ },+ });+ expect(result).toEqual({+ mode: 'json_schema',+ name: 'MySchema',+ description: 'desc',+ schema: { type: 'object', properties: { x: { type: 'string' } } },+ strict: true,+ });+ });++ it('maps strict: false to false', () => {+ const result = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: { name: 'S', schema: {}, strict: false },+ });+ expect((result as { strict?: boolean | null }).strict).toBe(false);+ });++ it('maps strict: null / omitted to undefined', () => {+ const withNull = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: { name: 'S', schema: {}, strict: null },+ });+ expect((withNull as { strict?: boolean | null }).strict).toBeUndefined();++ const withOmitted = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: { name: 'S', schema: {} },+ });+ expect((withOmitted as { strict?: boolean | null }).strict).toBeUndefined();+ });++ it('falls back to empty object schema when json_schema.schema is absent', () => {+ const result = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: { name: 'S' },+ });+ expect((result as { schema?: unknown }).schema).toEqual({ type: 'object', properties: {} });+ });++ it('omits description when not provided', () => {+ const result = toStructuredOutputSpec({+ type: 'json_schema',+ json_schema: { name: 'S', schema: {} },+ });+ expect((result as { description?: unknown }).description).toBeUndefined();+ });+});++// ─────────── toReasoningLevel ───────────++describe('toReasoningLevel', () => {+ it('returns undefined for non-google-gemini providers regardless of effort', () => {+ expect(toReasoningLevel({ provider: 'openai', reasoningEffort: 'high' })).toBeUndefined();+ expect(toReasoningLevel({ provider: 'anthropic', reasoningEffort: 'medium' })).toBeUndefined();+ expect(toReasoningLevel({ provider: 'generic', reasoningEffort: 'low' })).toBeUndefined();+ });++ it('returns undefined when reasoningEffort is undefined', () => {+ expect(toReasoningLevel({ provider: 'google-gemini', reasoningEffort: undefined })).toBeUndefined();+ });++ it('returns undefined when reasoningEffort is not a valid ReasoningLevel', () => {+ expect(toReasoningLevel({ provider: 'google-gemini', reasoningEffort: 'ultra' })).toBeUndefined();+ expect(toReasoningLevel({ provider: 'google-gemini', reasoningEffort: '' })).toBeUndefined();+ });++ it('returns the level for all valid ReasoningLevel values on google-gemini', () => {+ const validLevels = ['provider-default', 'none', 'minimal', 'low', 'medium', 'high', 'xhigh'] as const;+ for (const level of validLevels) {+ expect(toReasoningLevel({ provider: 'google-gemini', reasoningEffort: level })).toBe(level);+ }+ });+});++// ─────────── buildProviderOptions ───────────++describe('buildProviderOptions', () => {+ const textSpec: StructuredOutputSpec = { mode: 'text' };+ const jsonSpec: StructuredOutputSpec = { mode: 'json' };+ const schemaSpecStrict: StructuredOutputSpec = {+ mode: 'json_schema',+ name: 'S',+ description: undefined,+ schema: {},+ strict: true,+ };+ const schemaSpecNoStrict: StructuredOutputSpec = {+ mode: 'json_schema',+ name: 'S',+ description: undefined,+ schema: {},+ strict: undefined,+ };++ describe('openai provider', () => {+ const config = makeConfig({ provider: 'openai' });++ it('always includes store:false and include array', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['openai']).toMatchObject({ store: false, include: ['reasoning.encrypted_content'] });+ });++ it('includes reasoningEffort when present', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: 'high',+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['openai']).toMatchObject({ reasoningEffort: 'high' });+ });++ it('omits reasoningEffort key when undefined', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['openai']).not.toHaveProperty('reasoningEffort');+ });++ it('includes strictJsonSchema:true when spec is json_schema with strict:true', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: schemaSpecStrict,+ rawBody: {},+ });+ expect(opts['openai']).toMatchObject({ strictJsonSchema: true });+ });++ it('omits strictJsonSchema for non-json_schema modes', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: jsonSpec,+ rawBody: {},+ });+ expect(opts['openai']).not.toHaveProperty('strictJsonSchema');+ });++ it('omits strictJsonSchema when strict is undefined in spec', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: schemaSpecNoStrict,+ rawBody: {},+ });+ expect(opts['openai']).not.toHaveProperty('strictJsonSchema');+ });++ it('forwards service_tier, user, and prompt_cache_key from rawBody', () => {+ const opts = buildProviderOptions({+ config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: { service_tier: 'auto', user: 'u-123', prompt_cache_key: 'key-abc' },+ });+ expect(opts['openai']).toMatchObject({ serviceTier: 'auto', user: 'u-123', promptCacheKey: 'key-abc' });+ });++ it('omits service_tier/user/prompt_cache_key when absent from rawBody', () => {+ const opts = buildProviderOptions({+ config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['openai']).not.toHaveProperty('serviceTier');+ expect(opts['openai']).not.toHaveProperty('user');+ expect(opts['openai']).not.toHaveProperty('promptCacheKey');+ });+ });++ describe('anthropic provider', () => {+ const config = makeConfig({ provider: 'anthropic' });++ it('returns empty options when reasoningEffort is absent', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts).toEqual({});+ });++ it('maps low → 1024, medium → 8192, high → 32768', () => {+ expect(+ buildProviderOptions({ config: config, reasoningEffort: 'low', structuredOutputSpec: textSpec, rawBody: {} })[+ 'anthropic'+ ],+ ).toEqual({ thinking: { type: 'enabled', budgetTokens: 1024 } });+ expect(+ buildProviderOptions({+ config: config,+ reasoningEffort: 'medium',+ structuredOutputSpec: textSpec,+ rawBody: {},+ })['anthropic'],+ ).toEqual({ thinking: { type: 'enabled', budgetTokens: 8192 } });+ expect(+ buildProviderOptions({ config: config, reasoningEffort: 'high', structuredOutputSpec: textSpec, rawBody: {} })[+ 'anthropic'+ ],+ ).toEqual({ thinking: { type: 'enabled', budgetTokens: 32768 } });+ });++ it('falls back to 8192 for an unrecognised effort level', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: 'ultra',+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['anthropic']).toEqual({ thinking: { type: 'enabled', budgetTokens: 8192 } });+ });++ it('ignores strictJsonSchema (no anthropic key for structured-output strictness)', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: schemaSpecStrict,+ rawBody: {},+ });+ expect(opts).toEqual({});+ });++ it('forwards cache_control and disable_parallel_tool_use from rawBody', () => {+ const opts = buildProviderOptions({+ config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: { cache_control: { type: 'ephemeral' }, disable_parallel_tool_use: true },+ });+ expect(opts['anthropic']).toMatchObject({+ cacheControl: { type: 'ephemeral' },+ disableParallelToolUse: true,+ });+ });++ it('omits anthropic key when rawBody fields are absent and no reasoningEffort', () => {+ const opts = buildProviderOptions({+ config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts).not.toHaveProperty('anthropic');+ });+ });++ describe('generic provider', () => {+ const config = makeConfig({ provider: 'generic', base_url: 'http://localhost/v1' });++ it('returns empty options when both reasoningEffort and strictJsonSchema are absent', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts).toEqual({});+ });++ it('passes reasoningEffort through', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: 'medium',+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts['generic']).toMatchObject({ reasoningEffort: 'medium' });+ });++ it('passes strictJsonSchema for json_schema mode', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: schemaSpecStrict,+ rawBody: {},+ });+ expect(opts['generic']).toMatchObject({ strictJsonSchema: true });+ });++ it('includes both when both are present', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: 'low',+ structuredOutputSpec: schemaSpecStrict,+ rawBody: {},+ });+ expect(opts['generic']).toEqual({ reasoningEffort: 'low', strictJsonSchema: true });+ });++ it('omits the generic key when the resulting object would be empty', () => {+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ expect(opts).not.toHaveProperty('generic');+ });+ });++ describe('google-gemini provider', () => {+ const config = makeConfig({ provider: 'google-gemini' });++ it('returns empty providerOptions when rawBody carries no google-specific fields', () => {+ expect(+ buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {},+ }),+ ).toEqual({});+ expect(+ buildProviderOptions({ config: config, reasoningEffort: 'high', structuredOutputSpec: textSpec, rawBody: {} }),+ ).toEqual({});+ expect(+ buildProviderOptions({+ config: config,+ reasoningEffort: undefined,+ structuredOutputSpec: schemaSpecStrict,+ rawBody: {},+ }),+ ).toEqual({});+ });++ it('forwards safety_settings, thinking_config, cached_content from rawBody', () => {+ const opts = buildProviderOptions({+ config,+ reasoningEffort: undefined,+ structuredOutputSpec: textSpec,+ rawBody: {+ safety_settings: [{ category: 'HARM_CATEGORY_HATE_SPEECH', threshold: 'BLOCK_NONE' }],+ thinking_config: { thinkingBudget: 1024 },+ cached_content: 'cachedContents/my-cache',+ },+ });+ expect(opts['google']).toMatchObject({+ safetySettings: [{ category: 'HARM_CATEGORY_HATE_SPEECH', threshold: 'BLOCK_NONE' }],+ thinkingConfig: { thinkingBudget: 1024 },+ cachedContent: 'cachedContents/my-cache',+ });+ });+ });++ describe('cross-provider completeness: every provider must surface reasoningEffort somewhere', () => {+ const providers: VercelAIProviderConfig['provider'][] = ['openai', 'anthropic', 'generic', 'google-gemini'];+ const effort = 'high';++ it.each(providers)('%s: reasoningEffort reaches providerOptions or toReasoningLevel', provider => {+ const config = makeConfig({+ provider,+ ...(provider === 'generic' ? { base_url: 'http://localhost/v1' } : {}),+ });+ const opts = buildProviderOptions({+ config: config,+ reasoningEffort: effort,+ structuredOutputSpec: textSpec,+ rawBody: {},+ });+ const reasoningLevel = toReasoningLevel({ provider: provider, reasoningEffort: effort });++ const inProviderOptions =+ (opts['openai'] !== undefined && 'reasoningEffort' in opts['openai']) ||+ opts['anthropic'] !== undefined ||+ (opts['generic'] !== undefined && 'reasoningEffort' in opts['generic']);++ expect(inProviderOptions || reasoningLevel !== undefined).toBe(true);+ });+ });+});diff --git a/packages/harness/tests/core/llm/VercelAILLM.replay.test.ts b/packages/harness/tests/core/llm/VercelAILLM.replay.test.tsnew file mode 100644index 000000000..6668a3961--- /dev/null+++ b/packages/harness/tests/core/llm/VercelAILLM.replay.test.ts@@ -0,0 +1,239 @@+/**+ * End-to-end replay forwarding: stream accumulation → message replay.+ *+ * Each test pipes `mapStreamToChunks` output into `toAssistantModelMessage` and asserts that the+ * provider-specific replay token (reasoning signature / tool thoughtSignature) survives intact.+ *+ * These tests exist because the two layers were previously tested in isolation:+ * - VercelAILLM.stream.test.ts verified chunk emission but not provider_specific_fields on tool_calls+ * - VercelAILLM.messages.test.ts verified replay with hand-crafted inputs that bypassed accumulation+ *+ * A bug in either boundary would not be caught by unit tests alone; these cross-layer tests close+ * that gap for every provider that has a replay token.+ */+import type { LanguageModelUsage, ProviderMetadata, StepResultPerformance, TextStreamPart, ToolSet } from 'ai';+import type { ChatCompletionMessageParam } from 'openai/resources/chat';+import type { ExtendedChatCompletionChunk, RawAssistantMessageWithUsage } from '../../../src/core/llm/LLMTypes';+import {+ mapStreamToChunks,+ toAssistantModelMessage,+ type VercelAIProviderName,+} from '../../../src/core/llm/VercelAILLM';++// ─────────── Shared helpers ───────────++function makeUsage(): LanguageModelUsage {+ return {+ inputTokens: 10,+ outputTokens: 5,+ totalTokens: 15,+ inputTokenDetails: { cacheReadTokens: undefined, cacheWriteTokens: undefined, noCacheTokens: undefined },+ outputTokenDetails: { reasoningTokens: undefined, textTokens: undefined },+ };+}++function makeFinishStep(finishReason: 'stop' | 'tool-calls'): TextStreamPart<ToolSet> {+ const performance: StepResultPerformance = {+ effectiveOutputTokensPerSecond: 0,+ outputTokensPerSecond: undefined,+ inputTokensPerSecond: undefined,+ effectiveTotalTokensPerSecond: 0,+ stepTimeMs: 0,+ responseTimeMs: 0,+ toolExecutionMs: {},+ timeToFirstOutputMs: undefined,+ };+ return {+ type: 'finish-step',+ response: { id: 'resp', timestamp: new Date(0), modelId: 'test-model' },+ usage: makeUsage(),+ performance,+ finishReason,+ rawFinishReason: undefined,+ providerMetadata: undefined,+ };+}++async function* makeStream(parts: TextStreamPart<ToolSet>[]): AsyncGenerator<TextStreamPart<ToolSet>> {+ for (const part of parts) yield part;+}++async function drainStream(+ gen: AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown>,+): Promise<RawAssistantMessageWithUsage> {+ let result = await gen.next();+ while (!result.done) {+ result = await gen.next();+ }+ return result.value;+}++const CHUNK_META = { id: 'test-id', created: 0, model: 'test-model' };++// ─────────── Tool thoughtSignature round-trip (Gemini) ───────────++describe('Gemini tool thoughtSignature replay round-trip', () => {+ it('carries thoughtSignature from stream through provider_specific_fields to replay providerOptions', async () => {+ const toolMetadata: ProviderMetadata = { google: { thoughtSignature: 'gemini-sig-xyz' } };++ const { output } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-g1', toolName: 'search', providerMetadata: toolMetadata },+ { type: 'tool-input-delta', id: 'call-g1', delta: '{"q":"hello"}' },+ makeFinishStep('tool-calls'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ // The stream output must carry thought_signature so the next turn can replay it.+ expect(output.tool_calls?.[0]).toMatchObject({+ provider_specific_fields: { thought_signature: 'gemini-sig-xyz' },+ });++ // Simulate how AgentThread reconstructs the assistant message for the next-turn request.+ const assistantMsg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ ...(output.tool_calls !== undefined ? { tool_calls: output.tool_calls } : {}),+ };+ const replayMsg = toAssistantModelMessage({ msg: assistantMsg, provider: 'google-gemini' });+ const toolCallPart = (replayMsg.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'tool-call',+ );+ expect(toolCallPart?.providerOptions).toEqual({ google: { thoughtSignature: 'gemini-sig-xyz' } });+ });++ it('produces no providerOptions on replayed tool-call when no thoughtSignature was in the stream', async () => {+ const { output } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-plain', toolName: 'noop' },+ { type: 'tool-input-delta', id: 'call-plain', delta: '{}' },+ makeFinishStep('tool-calls'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(output.tool_calls?.[0]).not.toHaveProperty('provider_specific_fields');++ const assistantMsg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ ...(output.tool_calls !== undefined ? { tool_calls: output.tool_calls } : {}),+ };+ const replayMsg = toAssistantModelMessage({ msg: assistantMsg, provider: 'google-gemini' });+ const toolCallPart = (replayMsg.content as Array<{ type: string }>).find(p => p.type === 'tool-call');+ expect(toolCallPart).not.toHaveProperty('providerOptions');+ });+});++// ─────────── Reasoning signature round-trips ───────────++type ReasoningRoundTripCase = {+ provider: VercelAIProviderName;+ metadataKey: string;+ signatureField: string;+ signatureValue: string;+ expectedProviderOptions: Record<string, unknown>;+};++const REASONING_CASES: ReasoningRoundTripCase[] = [+ {+ provider: 'openai',+ metadataKey: 'openai',+ signatureField: 'reasoningEncryptedContent',+ signatureValue: 'enc-content-openai',+ expectedProviderOptions: { openai: { encryptedContent: 'enc-content-openai' } },+ },+ {+ provider: 'anthropic',+ metadataKey: 'anthropic',+ signatureField: 'signature',+ signatureValue: 'ant-sig-value',+ expectedProviderOptions: { anthropic: { signature: 'ant-sig-value' } },+ },+ {+ provider: 'generic',+ metadataKey: 'generic',+ signatureField: 'reasoningEncryptedContent',+ signatureValue: 'enc-content-generic',+ expectedProviderOptions: { generic: { encryptedContent: 'enc-content-generic' } },+ },+];++describe.each(REASONING_CASES)(+ 'reasoning signature round-trip ($provider)',+ ({ provider, metadataKey, signatureField, signatureValue, expectedProviderOptions }) => {+ it('carries reasoning signature from stream through thinking_blocks to replay providerOptions', async () => {+ const reasoningMetadata: ProviderMetadata = { [metadataKey]: { [signatureField]: signatureValue } };++ const { output } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'reasoning-start', id: 'r1' },+ { type: 'reasoning-delta', id: 'r1', text: 'some thought' },+ { type: 'reasoning-end', id: 'r1', providerMetadata: reasoningMetadata },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ // The signature must survive into thinking_blocks so the next turn can replay it.+ expect(output.thinking_blocks?.[0]).toMatchObject({+ type: 'thinking',+ thinking: 'some thought',+ signature: signatureValue,+ });++ // Simulate AgentThread reconstructing the assistant message for the next-turn request.+ const assistantMsg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(assistantMsg, 'thinking_blocks', output.thinking_blocks);++ const replayMsg = toAssistantModelMessage({ msg: assistantMsg, provider });+ const reasoningPart = (replayMsg.content as Array<{ type: string; providerOptions?: unknown }>).find(+ p => p.type === 'reasoning',+ );+ expect(reasoningPart?.providerOptions).toEqual(expectedProviderOptions);+ });+ },+);++describe('google-gemini reasoning replay round-trip', () => {+ it('emits a reasoning part with no providerOptions (signature is per-tool-call for Gemini)', async () => {+ // Gemini does not attach a standalone reasoning block signature; thoughtSignature is per+ // tool-call (tested in the Gemini tool round-trip suite above). The reasoning block is still+ // accumulated but carries no providerOptions on replay — @ai-sdk/google ignores it.+ const { output } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'reasoning-start', id: 'r1' },+ { type: 'reasoning-delta', id: 'r1', text: 'gemini thought' },+ { type: 'reasoning-end', id: 'r1' },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(output.thinking_blocks?.[0]).toMatchObject({ type: 'thinking', thinking: 'gemini thought' });+ expect(output.thinking_blocks?.[0]).not.toHaveProperty('signature');++ const assistantMsg: Extract<ChatCompletionMessageParam, { role: 'assistant' }> = {+ role: 'assistant',+ content: null,+ };+ Reflect.set(assistantMsg, 'thinking_blocks', output.thinking_blocks);++ const replayMsg = toAssistantModelMessage({ msg: assistantMsg, provider: 'google-gemini' });+ const reasoningPart = (replayMsg.content as Array<{ type: string }>).find(p => p.type === 'reasoning');+ expect(reasoningPart).toBeDefined();+ expect(reasoningPart).not.toHaveProperty('providerOptions');+ });+});diff --git a/packages/harness/tests/core/llm/VercelAILLM.stream.test.ts b/packages/harness/tests/core/llm/VercelAILLM.stream.test.tsnew file mode 100644index 000000000..f23281b29--- /dev/null+++ b/packages/harness/tests/core/llm/VercelAILLM.stream.test.ts@@ -0,0 +1,350 @@+/**+ * Stream processing: usage/finish-reason normalisation and the+ * TextStreamPart → ExtendedChatCompletionChunk mapper.+ */+import type {+ FinishReason,+ LanguageModelUsage,+ ProviderMetadata,+ StepResultPerformance,+ TextStreamPart,+ ToolSet,+} from 'ai';+import type { ExtendedChatCompletionChunk, RawAssistantMessageWithUsage } from '../../../src/core/llm/LLMTypes';+import { mapFinishReason, mapStreamToChunks, normalizeUsage } from '../../../src/core/llm/VercelAILLM';++// ─────────── Helpers ───────────++function makeUsage(inputTokens = 10, outputTokens = 5): LanguageModelUsage {+ return {+ inputTokens,+ outputTokens,+ totalTokens: inputTokens + outputTokens,+ inputTokenDetails: { cacheReadTokens: undefined, cacheWriteTokens: undefined, noCacheTokens: undefined },+ outputTokenDetails: { reasoningTokens: undefined, textTokens: undefined },+ };+}++function makeFinishStep(+ finishReason: 'stop' | 'length' | 'tool-calls' | 'content-filter' | 'error' | 'other',+ usage: LanguageModelUsage = makeUsage(),+): TextStreamPart<ToolSet> {+ const performance: StepResultPerformance = {+ effectiveOutputTokensPerSecond: 0,+ outputTokensPerSecond: undefined,+ inputTokensPerSecond: undefined,+ effectiveTotalTokensPerSecond: 0,+ stepTimeMs: 0,+ responseTimeMs: 0,+ toolExecutionMs: {},+ timeToFirstOutputMs: undefined,+ };+ return {+ type: 'finish-step',+ response: { id: 'resp', timestamp: new Date(0), modelId: 'test-model' },+ usage,+ performance,+ finishReason,+ rawFinishReason: undefined,+ providerMetadata: undefined,+ };+}++async function* makeStream(parts: TextStreamPart<ToolSet>[]): AsyncGenerator<TextStreamPart<ToolSet>> {+ for (const part of parts) yield part;+}++async function drainStream(+ gen: AsyncGenerator<ExtendedChatCompletionChunk, RawAssistantMessageWithUsage, unknown>,+): Promise<{ chunks: ExtendedChatCompletionChunk[]; final: RawAssistantMessageWithUsage }> {+ const chunks: ExtendedChatCompletionChunk[] = [];+ let result = await gen.next();+ while (!result.done) {+ chunks.push(result.value);+ result = await gen.next();+ }+ return { chunks, final: result.value };+}++const CHUNK_META = { id: 'test-id', created: 0, model: 'test-model' };++// ─────────── normalizeUsage ───────────++describe('normalizeUsage', () => {+ it('sums input and output tokens', () => {+ const result = normalizeUsage(makeUsage(10, 5));+ expect(result).toMatchObject({ input_tokens: 10, output_tokens: 5, total_tokens: 15 });+ });++ it('treats undefined token counts as 0', () => {+ const usage = makeUsage(0, 0);+ // Overwrite at runtime to simulate the SDK returning undefined for token counts.+ Reflect.set(usage, 'inputTokens', undefined);+ Reflect.set(usage, 'outputTokens', undefined);+ const result = normalizeUsage(usage);+ expect(result).toMatchObject({ input_tokens: 0, output_tokens: 0, total_tokens: 0 });+ });++ it('maps cache and reasoning detail fields', () => {+ const usage = makeUsage(20, 10);+ usage.inputTokenDetails.cacheReadTokens = 5;+ usage.inputTokenDetails.cacheWriteTokens = 3;+ usage.outputTokenDetails.reasoningTokens = 4;+ const result = normalizeUsage(usage);+ expect(result.cache_read_tokens).toBe(5);+ expect(result.cache_write_tokens).toBe(3);+ expect(result.reasoning_tokens).toBe(4);+ });++ it('leaves optional fields as undefined when source values are absent', () => {+ const result = normalizeUsage(makeUsage());+ expect(result.cache_read_tokens).toBeUndefined();+ expect(result.cache_write_tokens).toBeUndefined();+ expect(result.reasoning_tokens).toBeUndefined();+ });+});++// ─────────── mapFinishReason ───────────++describe('mapFinishReason', () => {+ const cases: Array<[FinishReason, string]> = [+ ['stop', 'stop'],+ ['length', 'length'],+ ['tool-calls', 'tool_calls'],+ ['content-filter', 'content_filter'],+ ['error', 'stop'],+ ['other', 'stop'],+ ];++ it.each(cases)('maps %s → %s', (sdkReason, harnessReason) => {+ expect(mapFinishReason(sdkReason)).toBe(harnessReason);+ });+});++// ─────────── mapStreamToChunks ───────────++describe('mapStreamToChunks', () => {+ it('emits text-delta chunks and accumulates content in the final message', async () => {+ const { chunks, final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'text-delta', id: 't1', text: 'hel' },+ { type: 'text-delta', id: 't1', text: 'lo' },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ const textChunks = chunks.filter(c => c.choices[0]?.delta.content !== undefined);+ expect(textChunks).toHaveLength(2);+ expect(textChunks[0]?.choices[0]?.delta.content).toBe('hel');+ expect(textChunks[1]?.choices[0]?.delta.content).toBe('lo');+ expect(final.output.content).toBe('hello');+ });++ it('emits reasoning-delta chunks and accumulates thinking_blocks', async () => {+ const { chunks, final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'reasoning-start', id: 'r1' },+ { type: 'reasoning-delta', id: 'r1', text: 'step one' },+ { type: 'reasoning-end', id: 'r1' },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ const reasoningChunks = chunks.filter(c => c.choices[0]?.delta.reasoning_content !== undefined);+ expect(reasoningChunks).toHaveLength(1);+ expect(reasoningChunks[0]?.choices[0]?.delta.reasoning_content).toBe('step one');+ expect(final.output.thinking_blocks).toEqual([{ type: 'thinking', thinking: 'step one' }]);+ });++ it('attaches signature from providerMetadata.*.reasoningEncryptedContent on reasoning-end (OpenAI)', async () => {+ const providerMetadata: ProviderMetadata = { openai: { reasoningEncryptedContent: 'enc-sig' } };+ const { final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'reasoning-start', id: 'r1' },+ { type: 'reasoning-delta', id: 'r1', text: 'thought' },+ { type: 'reasoning-end', id: 'r1', providerMetadata },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(final.output.thinking_blocks?.[0]).toMatchObject({+ type: 'thinking',+ thinking: 'thought',+ signature: 'enc-sig',+ });+ });++ it('attaches signature from providerMetadata.*.signature on reasoning-end (Anthropic)', async () => {+ const providerMetadata: ProviderMetadata = { anthropic: { signature: 'ant-sig' } };+ const { final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'reasoning-start', id: 'r1' },+ { type: 'reasoning-delta', id: 'r1', text: 'thought' },+ { type: 'reasoning-end', id: 'r1', providerMetadata },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(final.output.thinking_blocks?.[0]).toMatchObject({+ type: 'thinking',+ thinking: 'thought',+ signature: 'ant-sig',+ });+ });++ it('assigns unique ascending indices to interleaved tool-input-* parts', async () => {+ const { chunks } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-a', toolName: 'tool_a' },+ { type: 'tool-input-start', id: 'call-b', toolName: 'tool_b' },+ { type: 'tool-input-delta', id: 'call-a', delta: '{"k"' },+ { type: 'tool-input-delta', id: 'call-b', delta: '{}' },+ { type: 'tool-input-delta', id: 'call-a', delta: ':1}' },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ const startChunks = chunks.filter(c => c.choices[0]?.delta.tool_calls?.[0]?.id !== undefined);+ expect(startChunks).toHaveLength(2);+ const indices = startChunks.map(c => c.choices[0]?.delta.tool_calls?.[0]?.index);+ expect(indices[0]).toBe(0);+ expect(indices[1]).toBe(1);+ });++ it('accumulates tool_calls arguments in the final message', async () => {+ const { final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-1', toolName: 'search' },+ { type: 'tool-input-delta', id: 'call-1', delta: '{"q"' },+ { type: 'tool-input-delta', id: 'call-1', delta: ':"hi"}' },+ makeFinishStep('tool-calls'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(final.output.tool_calls).toEqual([+ { id: 'call-1', type: 'function', function: { name: 'search', arguments: '{"q":"hi"}' } },+ ]);+ expect(final.finish_reason).toBe('tool_calls');+ });++ it('captures google thoughtSignature from tool-input-start providerMetadata into provider_specific_fields', async () => {+ const providerMetadata: ProviderMetadata = { google: { thoughtSignature: 'google-sig-abc' } };+ const { final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-g', toolName: 'search_tool', providerMetadata },+ { type: 'tool-input-delta', id: 'call-g', delta: '{"q":"test"}' },+ makeFinishStep('tool-calls'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(final.output.tool_calls?.[0]).toMatchObject({+ id: 'call-g',+ type: 'function',+ function: { name: 'search_tool', arguments: '{"q":"test"}' },+ provider_specific_fields: { thought_signature: 'google-sig-abc' },+ });+ });++ it('omits provider_specific_fields when no thoughtSignature is present on tool-input-start', async () => {+ const { final } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'tool-input-start', id: 'call-x', toolName: 'noop' },+ { type: 'tool-input-delta', id: 'call-x', delta: '{}' },+ makeFinishStep('tool-calls'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );++ expect(final.output.tool_calls?.[0]).not.toHaveProperty('provider_specific_fields');+ });++ it('emits a finish chunk with usage and sets the final message finish_reason', async () => {+ const usage = makeUsage(20, 10);+ const { chunks, final } = await drainStream(+ mapStreamToChunks({ stream: makeStream([makeFinishStep('length', usage)]), chunkMeta: CHUNK_META }),+ );++ const finishChunk = chunks.find(c => c.choices[0]?.finish_reason !== null);+ expect(finishChunk?.choices[0]?.finish_reason).toBe('length');+ expect(finishChunk?.usage).toMatchObject({ input_tokens: 20, output_tokens: 10, total_tokens: 30 });+ expect(final.finish_reason).toBe('length');+ });++ it('rejects (with cause) on an error part', async () => {+ const cause = new Error('upstream error');+ await expect(+ drainStream(+ mapStreamToChunks({+ stream: makeStream([{ type: 'error', error: cause }]),+ chunkMeta: CHUNK_META,+ }),+ ),+ ).rejects.toMatchObject({ message: 'LLM stream error', cause });+ });++ it('wraps non-Error error values in an Error with cause', async () => {+ await expect(+ drainStream(+ mapStreamToChunks({+ stream: makeStream([{ type: 'error', error: 'string error' }]),+ chunkMeta: CHUNK_META,+ }),+ ),+ ).rejects.toMatchObject({ message: 'LLM stream error' });+ });++ it('sets chunk metadata (id, model, object) on emitted chunks', async () => {+ const meta = { id: 'vc-test-001', created: 1000, model: 'my-model' };+ const { chunks } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([{ type: 'text-delta', id: 't1', text: 'x' }, makeFinishStep('stop')]),+ chunkMeta: meta,+ }),+ );+ const chunk = chunks[0];+ expect(chunk?.id).toBe('vc-test-001');+ expect(chunk?.model).toBe('my-model');+ expect(chunk?.object).toBe('chat.completion.chunk');+ });++ it('ignores structural/bookkeeping stream parts without emitting chunks', async () => {+ const { chunks } = await drainStream(+ mapStreamToChunks({+ stream: makeStream([+ { type: 'start' },+ { type: 'text-start', id: 't1' },+ { type: 'text-end', id: 't1' },+ { type: 'tool-input-end', id: 'c1' },+ makeFinishStep('stop'),+ ]),+ chunkMeta: CHUNK_META,+ }),+ );+ // Only the finish-step chunk is emitted (no text-delta chunks for structural parts).+ const nonFinishChunks = chunks.filter(c => c.choices[0]?.finish_reason === null);+ expect(nonFinishChunks).toHaveLength(0);+ });+});diff --git a/packages/sdk/.fern/metadata.json b/packages/sdk/.fern/metadata.jsonindex 8547fb5bc..4cbe38c6b 100644--- a/packages/sdk/.fern/metadata.json+++ b/packages/sdk/.fern/metadata.json@@ -26,7 +26,7 @@"streamType": "web","useDefaultRequestParameterValues": true},- "originGitCommit": "2bf85043c33eb3621f5a106baf173a70e21daf7f",+ "originGitCommit": "c4bd4f7de2af782b3f6b977060eaae948d76d75d","originGitCommitIsDirty": true,"invokedBy": "ci","requestedVersion": "0.0.0",diff --git a/packages/sdk/src/api/types/AnthropicProviderModelEntry.ts b/packages/sdk/src/api/types/AnthropicProviderModelEntry.tsnew file mode 100644index 000000000..b5fb26519--- /dev/null+++ b/packages/sdk/src/api/types/AnthropicProviderModelEntry.ts@@ -0,0 +1,12 @@+// This file was auto-generated by Fern from our API Definition.++export interface AnthropicProviderModelEntry {+ apiKey?: string;+ baseUrl?: string;+ headers?: Record<string, string>;+ maxOutputTokens: number;+ modelId?: string;+ name: string;+ provider: "anthropic";+ reasoningEfforts?: string[];+}diff --git a/packages/sdk/src/api/types/GenericProviderModelEntry.ts b/packages/sdk/src/api/types/GenericProviderModelEntry.tsnew file mode 100644index 000000000..b12af83ca--- /dev/null+++ b/packages/sdk/src/api/types/GenericProviderModelEntry.ts@@ -0,0 +1,13 @@+// This file was auto-generated by Fern from our API Definition.++export interface GenericProviderModelEntry {+ apiFormat?: "openai-chat-completions";+ apiKey?: string;+ baseUrl: string;+ headers?: Record<string, string>;+ maxOutputTokens: number;+ modelId?: string;+ name: string;+ provider: "generic";+ reasoningEfforts?: string[];+}diff --git a/packages/sdk/src/api/types/GoogleGeminiProviderModelEntry.ts b/packages/sdk/src/api/types/GoogleGeminiProviderModelEntry.tsnew file mode 100644index 000000000..6179f02ec--- /dev/null+++ b/packages/sdk/src/api/types/GoogleGeminiProviderModelEntry.ts@@ -0,0 +1,12 @@+// This file was auto-generated by Fern from our API Definition.++export interface GoogleGeminiProviderModelEntry {+ apiKey?: string;+ baseUrl?: string;+ headers?: Record<string, string>;+ maxOutputTokens: number;+ modelId?: string;+ name: string;+ provider: "google-gemini";+ reasoningEfforts?: string[];+}diff --git a/packages/sdk/src/api/types/ModelEntry.ts b/packages/sdk/src/api/types/ModelEntry.tsindex a853fcd13..68aa6d4d3 100644--- a/packages/sdk/src/api/types/ModelEntry.ts+++ b/packages/sdk/src/api/types/ModelEntry.ts@@ -1,7 +1,9 @@// This file was auto-generated by Fern from our API Definition.-export interface ModelEntry {- maxOutputTokens: number;- name: string;- reasoningEfforts?: string[];-}+import type * as TrueHarness from "../index.js";++export type ModelEntry =+ | TrueHarness.AnthropicProviderModelEntry+ | TrueHarness.GenericProviderModelEntry+ | TrueHarness.GoogleGeminiProviderModelEntry+ | TrueHarness.OpenAiProviderModelEntry;diff --git a/packages/sdk/src/api/types/OpenAiProviderModelEntry.ts b/packages/sdk/src/api/types/OpenAiProviderModelEntry.tsnew file mode 100644index 000000000..0292f66cc--- /dev/null+++ b/packages/sdk/src/api/types/OpenAiProviderModelEntry.ts@@ -0,0 +1,12 @@+// This file was auto-generated by Fern from our API Definition.++export interface OpenAiProviderModelEntry {+ apiKey?: string;+ baseUrl?: string;+ headers?: Record<string, string>;+ maxOutputTokens: number;+ modelId?: string;+ name: string;+ provider: "openai";+ reasoningEfforts?: string[];+}diff --git a/packages/sdk/src/api/types/index.ts b/packages/sdk/src/api/types/index.tsindex 4b6ec142f..b2304233c 100644--- a/packages/sdk/src/api/types/index.ts+++ b/packages/sdk/src/api/types/index.ts@@ -3,6 +3,7 @@ export * from "./AgentInfo.js";export * from "./AgentParent.js";export * from "./AgentSpec.js";export * from "./AgentSpecUserMessage.js";+export * from "./AnthropicProviderModelEntry.js";export * from "./ApprovalAllow.js";export * from "./ApprovalDecision.js";export * from "./ApprovalDeny.js";@@ -23,11 +24,13 @@ export * from "./ExtendedChunkDeltaToolCall.js";export * from "./FileContent.js";export * from "./FinishReason.js";export * from "./GenerativeUiConfig.js";+export * from "./GenericProviderModelEntry.js";export * from "./GetCapabilitiesResponse.js";export * from "./GetCapabilitiesResponseData.js";export * from "./GetCapabilitiesResponseDataSandbox.js";export * from "./GetSessionResponse.js";export * from "./GetTurnResponse.js";+export * from "./GoogleGeminiProviderModelEntry.js";export * from "./LargeToolResponseConfig.js";export * from "./ListMcpServersResponse.js";export * from "./ListMcpToolsResponse.js";@@ -64,6 +67,7 @@ export * from "./ModelMessageEventContentOneItem.js";export * from "./ModelMessageUsage.js";export * from "./ModelMessageUsageInputTokensBreakdown.js";export * from "./ModelParams.js";+export * from "./OpenAiProviderModelEntry.js";export * from "./PreviousTurnIdInput.js";export * from "./RawToolCall.js";export * from "./RequestErrorResponse.js";diff --git a/packages/sdk/src/serialization/types/AnthropicProviderModelEntry.ts b/packages/sdk/src/serialization/types/AnthropicProviderModelEntry.tsnew file mode 100644index 000000000..1cff9cf18--- /dev/null+++ b/packages/sdk/src/serialization/types/AnthropicProviderModelEntry.ts@@ -0,0 +1,35 @@+// This file was auto-generated by Fern from our API Definition.++import type * as TrueHarness from "../../api/index.js";+import * as core from "../../core/index.js";+import type * as serializers from "../index.js";++export const AnthropicProviderModelEntry: core.serialization.ObjectSchema<+ serializers.AnthropicProviderModelEntry.Raw,+ TrueHarness.AnthropicProviderModelEntry+> = core.serialization.object({+ apiKey: core.serialization.property("api_key", core.serialization.string().optional()),+ baseUrl: core.serialization.property("base_url", core.serialization.string().optional()),+ headers: core.serialization.record(core.serialization.string(), core.serialization.string()).optional(),+ maxOutputTokens: core.serialization.property("max_output_tokens", core.serialization.number()),+ modelId: core.serialization.property("model_id", core.serialization.string().optional()),+ name: core.serialization.string(),+ provider: core.serialization.stringLiteral("anthropic"),+ reasoningEfforts: core.serialization.property(+ "reasoning_efforts",+ core.serialization.list(core.serialization.string()).optional(),+ ),+});++export declare namespace AnthropicProviderModelEntry {+ export interface Raw {+ api_key?: string | null;+ base_url?: string | null;+ headers?: Record<string, string> | null;+ max_output_tokens: number;+ model_id?: string | null;+ name: string;+ provider: "anthropic";+ reasoning_efforts?: string[] | null;+ }+}diff --git a/packages/sdk/src/serialization/types/GenericProviderModelEntry.ts b/packages/sdk/src/serialization/types/GenericProviderModelEntry.tsnew file mode 100644index 000000000..13148cc50--- /dev/null+++ b/packages/sdk/src/serialization/types/GenericProviderModelEntry.ts@@ -0,0 +1,40 @@+// This file was auto-generated by Fern from our API Definition.++import type * as TrueHarness from "../../api/index.js";+import * as core from "../../core/index.js";+import type * as serializers from "../index.js";++export const GenericProviderModelEntry: core.serialization.ObjectSchema<+ serializers.GenericProviderModelEntry.Raw,+ TrueHarness.GenericProviderModelEntry+> = core.serialization.object({+ apiFormat: core.serialization.property(+ "api_format",+ core.serialization.stringLiteral("openai-chat-completions").optional(),+ ),+ apiKey: core.serialization.property("api_key", core.serialization.string().optional()),+ baseUrl: core.serialization.property("base_url", core.serialization.string()),+ headers: core.serialization.record(core.serialization.string(), core.serialization.string()).optional(),+ maxOutputTokens: core.serialization.property("max_output_tokens", core.serialization.number()),+ modelId: core.serialization.property("model_id", core.serialization.string().optional()),+ name: core.serialization.string(),+ provider: core.serialization.stringLiteral("generic"),+ reasoningEfforts: core.serialization.property(+ "reasoning_efforts",+ core.serialization.list(core.serialization.string()).optional(),+ ),+});++export declare namespace GenericProviderModelEntry {+ export interface Raw {+ api_format?: "openai-chat-completions" | null;+ api_key?: string | null;+ base_url: string;+ headers?: Record<string, string> | null;+ max_output_tokens: number;+ model_id?: string | null;+ name: string;+ provider: "generic";+ reasoning_efforts?: string[] | null;+ }+}diff --git a/packages/sdk/src/serialization/types/GoogleGeminiProviderModelEntry.ts b/packages/sdk/src/serialization/types/GoogleGeminiProviderModelEntry.tsnew file mode 100644index 000000000..619b9f41b--- /dev/null+++ b/packages/sdk/src/serialization/types/GoogleGeminiProviderModelEntry.ts@@ -0,0 +1,35 @@+// This file was auto-generated by Fern from our API Definition.++import type * as TrueHarness from "../../api/index.js";+import * as core from "../../core/index.js";+import type * as serializers from "../index.js";++export const GoogleGeminiProviderModelEntry: core.serialization.ObjectSchema<+ serializers.GoogleGeminiProviderModelEntry.Raw,+ TrueHarness.GoogleGeminiProviderModelEntry+> = core.serialization.object({+ apiKey: core.serialization.property("api_key", core.serialization.string().optional()),+ baseUrl: core.serialization.property("base_url", core.serialization.string().optional()),+ headers: core.serialization.record(core.serialization.string(), core.serialization.string()).optional(),+ maxOutputTokens: core.serialization.property("max_output_tokens", core.serialization.number()),+ modelId: core.serialization.property("model_id", core.serialization.string().optional()),+ name: core.serialization.string(),+ provider: core.serialization.stringLiteral("google-gemini"),+ reasoningEfforts: core.serialization.property(+ "reasoning_efforts",+ core.serialization.list(core.serialization.string()).optional(),+ ),+});++export declare namespace GoogleGeminiProviderModelEntry {+ export interface Raw {+ api_key?: string | null;+ base_url?: string | null;+ headers?: Record<string, string> | null;+ max_output_tokens: number;+ model_id?: string | null;+ name: string;+ provider: "google-gemini";+ reasoning_efforts?: string[] | null;+ }+}diff --git a/packages/sdk/src/serialization/types/ModelEntry.ts b/packages/sdk/src/serialization/types/ModelEntry.tsindex 44a4c85e1..8b188db33 100644--- a/packages/sdk/src/serialization/types/ModelEntry.ts+++ b/packages/sdk/src/serialization/types/ModelEntry.ts@@ -3,21 +3,23 @@import type * as TrueHarness from "../../api/index.js";import * as core from "../../core/index.js";import type * as serializers from "../index.js";+import { AnthropicProviderModelEntry } from "./AnthropicProviderModelEntry.js";+import { GenericProviderModelEntry } from "./GenericProviderModelEntry.js";+import { GoogleGeminiProviderModelEntry } from "./GoogleGeminiProviderModelEntry.js";+import { OpenAiProviderModelEntry } from "./OpenAiProviderModelEntry.js";-export const ModelEntry: core.serialization.ObjectSchema<serializers.ModelEntry.Raw, TrueHarness.ModelEntry> =- core.serialization.object({- maxOutputTokens: core.serialization.property("max_output_tokens", core.serialization.number()),- name: core.serialization.string(),- reasoningEfforts: core.serialization.property(- "reasoning_efforts",- core.serialization.list(core.serialization.string()).optional(),- ),- });+export const ModelEntry: core.serialization.Schema<serializers.ModelEntry.Raw, TrueHarness.ModelEntry> =+ core.serialization.undiscriminatedUnion([+ AnthropicProviderModelEntry,+ GenericProviderModelEntry,+ GoogleGeminiProviderModelEntry,+ OpenAiProviderModelEntry,+ ]);export declare namespace ModelEntry {- export interface Raw {- max_output_tokens: number;- name: string;- reasoning_efforts?: string[] | null;- }+ export type Raw =+ | AnthropicProviderModelEntry.Raw+ | GenericProviderModelEntry.Raw+ | GoogleGeminiProviderModelEntry.Raw+ | OpenAiProviderModelEntry.Raw;}diff --git a/packages/sdk/src/serialization/types/OpenAiProviderModelEntry.ts b/packages/sdk/src/serialization/types/OpenAiProviderModelEntry.tsnew file mode 100644index 000000000..04cbc5288--- /dev/null+++ b/packages/sdk/src/serialization/types/OpenAiProviderModelEntry.ts@@ -0,0 +1,35 @@+// This file was auto-generated by Fern from our API Definition.++import type * as TrueHarness from "../../api/index.js";+import * as core from "../../core/index.js";+import type * as serializers from "../index.js";++export const OpenAiProviderModelEntry: core.serialization.ObjectSchema<+ serializers.OpenAiProviderModelEntry.Raw,+ TrueHarness.OpenAiProviderModelEntry+> = core.serialization.object({+ apiKey: core.serialization.property("api_key", core.serialization.string().optional()),+ baseUrl: core.serialization.property("base_url", core.serialization.string().optional()),+ headers: core.serialization.record(core.serialization.string(), core.serialization.string()).optional(),+ maxOutputTokens: core.serialization.property("max_output_tokens", core.serialization.number()),+ modelId: core.serialization.property("model_id", core.serialization.string().optional()),+ name: core.serialization.string(),+ provider: core.serialization.stringLiteral("openai"),+ reasoningEfforts: core.serialization.property(+ "reasoning_efforts",+ core.serialization.list(core.serialization.string()).optional(),+ ),+});++export declare namespace OpenAiProviderModelEntry {+ export interface Raw {+ api_key?: string | null;+ base_url?: string | null;+ headers?: Record<string, string> | null;+ max_output_tokens: number;+ model_id?: string | null;+ name: string;+ provider: "openai";+ reasoning_efforts?: string[] | null;+ }+}diff --git a/packages/sdk/src/serialization/types/index.ts b/packages/sdk/src/serialization/types/index.tsindex 4b6ec142f..b2304233c 100644--- a/packages/sdk/src/serialization/types/index.ts+++ b/packages/sdk/src/serialization/types/index.ts@@ -3,6 +3,7 @@ export * from "./AgentInfo.js";export * from "./AgentParent.js";export * from "./AgentSpec.js";export * from "./AgentSpecUserMessage.js";+export * from "./AnthropicProviderModelEntry.js";export * from "./ApprovalAllow.js";export * from "./ApprovalDecision.js";export * from "./ApprovalDeny.js";@@ -23,11 +24,13 @@ export * from "./ExtendedChunkDeltaToolCall.js";export * from "./FileContent.js";export * from "./FinishReason.js";export * from "./GenerativeUiConfig.js";+export * from "./GenericProviderModelEntry.js";export * from "./GetCapabilitiesResponse.js";export * from "./GetCapabilitiesResponseData.js";export * from "./GetCapabilitiesResponseDataSandbox.js";export * from "./GetSessionResponse.js";export * from "./GetTurnResponse.js";+export * from "./GoogleGeminiProviderModelEntry.js";export * from "./LargeToolResponseConfig.js";export * from "./ListMcpServersResponse.js";export * from "./ListMcpToolsResponse.js";@@ -64,6 +67,7 @@ export * from "./ModelMessageEventContentOneItem.js";export * from "./ModelMessageUsage.js";export * from "./ModelMessageUsageInputTokensBreakdown.js";export * from "./ModelParams.js";+export * from "./OpenAiProviderModelEntry.js";export * from "./PreviousTurnIdInput.js";export * from "./RawToolCall.js";export * from "./RequestErrorResponse.js";diff --git a/packages/sdk/tests/wire/models.test.ts b/packages/sdk/tests/wire/models.test.tsindex 65936b9f7..9c1ba0c13 100644--- a/packages/sdk/tests/wire/models.test.ts+++ b/packages/sdk/tests/wire/models.test.ts@@ -9,7 +9,18 @@ describe("ModelsClient", () => {const client = new TrueHarness({ maxRetries: 0, environment: server.baseUrl });const rawResponseBody = {- data: [{ max_output_tokens: 1, name: "name", reasoning_efforts: ["reasoning_efforts"] }],+ data: [+ {+ api_key: "api_key",+ base_url: "base_url",+ headers: { key: "value" },+ max_output_tokens: 1,+ model_id: "model_id",+ name: "name",+ provider: "anthropic",+ reasoning_efforts: ["reasoning_efforts"],+ },+ ],};server.mockEndpoint().get("/api/v1/models").respondWith().statusCode(200).jsonBody(rawResponseBody).build();@@ -18,8 +29,15 @@ describe("ModelsClient", () => {expect(response).toEqual({data: [{+ apiKey: "api_key",+ baseUrl: "base_url",+ headers: {+ key: "value",+ },maxOutputTokens: 1,+ modelId: "model_id",name: "name",+ provider: "anthropic",reasoningEfforts: ["reasoning_efforts"],},],diff --git a/packages/server/registry-example/models.yaml b/packages/server/registry-example/models.yamlindex 9290e5313..2fd8c33ca 100644--- a/packages/server/registry-example/models.yaml+++ b/packages/server/registry-example/models.yaml@@ -1,19 +1,88 @@-# Models reachable through the OpenAI-compatible API at base_url.-#-# Auth headers are configured via env vars, never in this file:-# MODEL_API_KEY - default API key for base_url (required)-# MODEL_{NAME}_API_KEY - per-model API key override-# MODEL_HEADERS - extra default headers for every model-# MODEL_{NAME}_HEADERS - per-model overrides ({NAME} = model name,-# uppercased, non-alphanumerics replaced by "_")-# TODO(oss): replace internal endpoint with a placeholder before the repo-# goes public.-base_url: https://internal.devtest.truefoundry.tech/api/llm+# Each model declares its own provider so direct vendors and gateways can be+# mixed freely in the same registry.+#+# name — display name / alias used in the API and env var lookups.+# Must be lowercase a-z, 0-9, hyphens, and dots (e.g. "gpt-4o").+# model_id — the identifier sent to the provider (e.g. "anthropic/claude-sonnet-4-6"+# for a gateway). Omit when name already matches the provider's model ID.+#+# Auth — two ways to configure credentials (first match wins per model):+#+# 1. Inline api_key / headers in this file using ${ENV_VAR} substitution:+# api_key: "${ANTHROPIC_API_KEY}"+# headers:+# Authorization: "Bearer ${MY_TOKEN}"+# The server resolves the env var at startup. Missing vars raise an error.+#+# 2. Env var conventions (fallback when api_key is absent):+# MODEL_API_KEY — global default key for all models+# MODEL_{NAME}_API_KEY — per-model override (keyed by name, not model_id)+# MODEL_HEADERS — global extra headers+# MODEL_{NAME}_HEADERS — per-model header overrides (keyed by name)+# ({NAME} = name uppercased, non-alphanumerics → "_")+#+# Supported providers:+# First-party (Vercel AI SDK): openai | anthropic | google-gemini+# Generic (requires base_url): generic+#+# base_url is optional for first-party providers (overrides the default endpoint)+# and required for the generic provider.+#+# api_format selects the API surface for the generic provider:+# openai-chat-completions - OpenAI Chat Completions API (default).+#+# openai always uses the Responses API (supports o-series reasoning models,+# store: false, and stateless multi-turn via reasoning.encrypted_content).models:- - name: anthropic/claude-sonnet-4-6+ # --- Direct Anthropic ---+ - name: claude-sonnet-4-6+ provider: anthropic+ api_key: "${ANTHROPIC_API_KEY}"+ max_output_tokens: 32768+ reasoning_efforts:+ - low+ - medium+ - high++ # --- Direct OpenAI (Responses API) ---+ - name: gpt-4o+ provider: openai+ api_key: "${OPENAI_API_KEY}"+ max_output_tokens: 16384++ - name: o4-mini+ provider: openai+ api_key: "${OPENAI_API_KEY}"+ max_output_tokens: 32768+ reasoning_efforts:+ - low+ - medium+ - high++ - name: o3+ provider: openai+ api_key: "${OPENAI_API_KEY}"+ max_output_tokens: 32768reasoning_efforts:- low- medium- high++ # --- Direct Google ---+ - name: gemini-2.5-flash+ provider: google-gemini+ api_key: "${GOOGLE_GEMINI_API_KEY}"+ max_output_tokens: 8192++ # --- Generic gateway (e.g. LiteLLM / TrueFoundry / any OpenAI-compat proxy) ---+ - name: claude-sonnet-4-6-gateway+ model_id: anthropic/claude-sonnet-4-6+ provider: generic+ api_key: "${LLM_GATEWAY_API_KEY}"+ base_url: https://your-gateway.example.com/api/llmmax_output_tokens: 32768+ reasoning_efforts:+ - low+ - medium+ - highdiff --git a/packages/server/src/apis/turns.ts b/packages/server/src/apis/turns.tsindex 1938c6207..c95821033 100644--- a/packages/server/src/apis/turns.ts+++ b/packages/server/src/apis/turns.ts@@ -15,7 +15,7 @@ import {isAgentInputUserMessage,isFileContentPart,McpConnectionError,- OpenAILLM,+ VercelAILLM,} from '@truefoundry/utils/core';import { streamSSE } from 'hono/streaming';import type { Logger } from 'winston';@@ -50,8 +50,8 @@ export interface TurnsRouterDeps {/*** Per-run resource wiring: maps the YAML catalogs onto the agentSession- * TurnResourceResolver. Models are served by one OpenAI-compatible API- * (models.yaml base_url); MCP servers resolve to url + env-configured headers;+ * TurnResourceResolver. Each model carries its own provider config from+ * models.yaml; MCP servers resolve to url + env-configured headers;* the sandbox factory (when configured) creates/reattaches the run's Sandbox.*/function createTurnResolver(deps: {@@ -64,10 +64,8 @@ function createTurnResolver(deps: {const { modelStore, mcpStore, logger, signal } = deps;return new TurnResourceResolver({llm: name =>- new OpenAILLM({- baseURL: modelStore.baseUrl,- apiKey: modelStore.getApiKey(name),- headers: modelStore.getHeaders(name),+ new VercelAILLM({+ providerConfig: modelStore.getProviderConfig(name),logger,signal,}),diff --git a/packages/server/src/store/ModelStore.ts b/packages/server/src/store/ModelStore.tsindex 7bd1f9aa5..9318b2bc8 100644--- a/packages/server/src/store/ModelStore.ts+++ b/packages/server/src/store/ModelStore.ts@@ -1,27 +1,47 @@/**- * Catalog of models from models.yaml. Every model is reached through the- * OpenAI-compatible API at the file's base_url, authenticated with- * MODEL_API_KEY (or a per-model MODEL_{NAME}_API_KEY override);- * MODEL_HEADERS / MODEL_{NAME}_HEADERS add extra headers. Credentials are- * never exposed in list output.+ * Catalog of models from models.yaml. Each model declares its own provider so+ * direct vendors (openai, anthropic, google, mistral, …) and gateways+ * (truefoundry, litellm, openrouter, …) can be mixed freely.+ *+ * Credentials are injected at runtime from env vars and are never exposed in+ * list output.*/import configuration, { CONFIG_FILES, normalizeEnvName } from '../config';import { loadYamlFile } from './loadYaml';import { ModelsFileSchema, type ModelEntry } from './schemas';+/**+ * Resolves `${VAR_NAME}` placeholders in a template string using `process.env`.+ * Throws clearly if a referenced variable is not set so misconfiguration is+ * caught at startup rather than silently producing empty credentials.+ */+function substituteEnvVars(template: string): string {+ return template.replace(/\$\{([^}]+)\}/g, (_, varName: string) => {+ const value = process.env[varName];+ if (value === undefined) {+ throw new Error(`Environment variable "${varName}" is not set (referenced in models.yaml api_key or headers)`);+ }+ return value;+ });+}++/** ModelEntry merged with the runtime credentials for that model. */+export type ProviderConfig = ModelEntry & {+ apiKey: string;+ headers: Record<string, string>;+};+export class ModelStore {- readonly baseUrl: string;private readonly models: ModelEntry[];- constructor(baseUrl: string, models: ModelEntry[]) {- this.baseUrl = baseUrl;+ constructor(models: ModelEntry[]) {this.models = models;}/** Loads and validates models.yaml. Throws on any error. */static load(): ModelStore {const file = loadYamlFile(CONFIG_FILES.models, ModelsFileSchema);- return new ModelStore(file.base_url, file.models);+ return new ModelStore(file.models);}list(): ModelEntry[] {@@ -32,20 +52,59 @@ export class ModelStore {return this.models.find(model => model.name === name);}- /** API key for the given model: MODEL_{NAME}_API_KEY override, else MODEL_API_KEY. */+ /**+ * API key for the given model.+ *+ * Resolution order (first match wins):+ * 1. `api_key` field in models.yaml (supports `${VAR}` substitution)+ * 2. `MODEL_{NAME}_API_KEY` env var+ * 3. `MODEL_API_KEY` env var (global default)+ */getApiKey(name: string): string {+ const entry = this.get(name);+ if (entry?.api_key !== undefined) {+ return substituteEnvVars(entry.api_key);+ }return configuration.MODEL_API_KEY_BY_NAME[normalizeEnvName(name)] ?? configuration.MODEL_API_KEY;}/**- * Extra headers for requests to the given model: MODEL_HEADERS plus- * per-model MODEL_{NAME}_HEADERS overrides. Auth is not included — the API- * key from getApiKey() is passed to the LLM client separately.+ * Extra headers for requests to the given model.+ *+ * Merged in ascending priority order:+ * 1. `MODEL_HEADERS` env var (global)+ * 2. `MODEL_{NAME}_HEADERS` env var (per-model)+ * 3. `headers` field in models.yaml (supports `${VAR}` substitution in values)*/getHeaders(name: string): Record<string, string> {+ const entry = this.get(name);+ const inlineHeaders: Record<string, string> = {};+ if (entry?.headers !== undefined) {+ for (const [key, value] of Object.entries(entry.headers)) {+ inlineHeaders[key] = substituteEnvVars(value);+ }+ }return {...configuration.MODEL_HEADERS,...configuration.MODEL_HEADERS_BY_NAME[normalizeEnvName(name)],+ ...inlineHeaders,+ };+ }++ /**+ * Full provider config for a model: entry fields (provider, base_url, …)+ * merged with the resolved API key and headers for use by VercelAILLM.+ * Throws if the model is not registered.+ */+ getProviderConfig(name: string): ProviderConfig {+ const entry = this.get(name);+ if (!entry) {+ throw new Error(`Model not registered in models.yaml: ${name}`);+ }+ return {+ ...entry,+ apiKey: this.getApiKey(name),+ headers: this.getHeaders(name),};}}diff --git a/packages/server/src/store/schemas.ts b/packages/server/src/store/schemas.tsindex eb8e220b7..a70a265e3 100644--- a/packages/server/src/store/schemas.ts+++ b/packages/server/src/store/schemas.ts@@ -23,10 +23,9 @@ function uniqueNames(entries: { name: string }[], ctx: z.RefinementCtx): void {}/**- * Adds a validation issue if two distinct names normalize to the same- * `{NAME}` env var segment (e.g. "foo-bar" and "foo_bar"), since they could- * not be given separate per-name overrides (headers, API keys). Only applied- * to entries with per-name env vars (models, MCP servers).+ * Adds a validation issue if two names normalize to the same `{NAME}` env var+ * segment (e.g. "foo-bar" and "foo_bar"), since they couldn't get separate+ * per-name overrides.*/function uniqueEnvNames(entries: { name: string }[], ctx: z.RefinementCtx): void {const seenByEnvName = new Map<string, string>();@@ -44,21 +43,89 @@ function uniqueEnvNames(entries: { name: string }[], ctx: z.RefinementCtx): void}}-export const ModelEntrySchema = z- .object({- name: z.string().min(1),- reasoning_efforts: z.array(z.string().min(1)).min(1).optional(),- max_output_tokens: z.number().int().positive(),- })+const ModelEntryBaseSchema = z.object({+ /**+ * Display name / alias used in the API, env vars, and the registry.+ * Also sent to the provider as the model identifier when `model_id` is absent.+ */+ name: z+ .string()+ .min(1)+ .regex(+ /^[a-z0-9][a-z0-9.-]*$/,+ 'name must start with a letter or digit and contain only lowercase a-z, 0-9, hyphens, and dots',+ ),+ /**+ * The model identifier sent to the provider (e.g. `anthropic/claude-sonnet-4-6`+ * for a gateway). Use when the provider-facing ID isn't a valid `name`.+ */+ model_id: z.string().min(1).optional(),+ max_output_tokens: z.number().int().positive(),+ reasoning_efforts: z.array(z.string().min(1)).min(1).optional(),+ /**+ * API key for this model, supports `${ENV_VAR}` substitution.+ * Falls back to MODEL_{NAME}_API_KEY, then MODEL_API_KEY when absent.+ */+ api_key: z.string().min(1).optional(),+ /**+ * Extra HTTP headers for requests to this model, supports `${ENV_VAR}` substitution.+ * Merged on top of MODEL_HEADERS and MODEL_{NAME}_HEADERS env var headers.+ */+ headers: z.record(z.string()).optional(),+});++/** OpenAI provider entry — always uses the Responses API. */+const OpenAIProviderEntrySchema = ModelEntryBaseSchema.extend({+ provider: z.literal('openai'),+ /** Override the default OpenAI base URL (e.g. Azure OpenAI endpoint). */+ base_url: z.string().url().optional(),+})+ .strict()+ .openapi('OpenAIProviderModelEntry');++/** Anthropic provider entry — backed by the dedicated Vercel AI SDK adapter. */+const AnthropicProviderEntrySchema = ModelEntryBaseSchema.extend({+ provider: z.literal('anthropic'),+ /** Override the provider's default base URL (e.g. point at a local proxy). */+ base_url: z.string().url().optional(),+}).strict()+ .openapi('AnthropicProviderModelEntry');++/** Google Gemini provider entry — backed by the dedicated Vercel AI SDK adapter. */+const GoogleGeminiProviderEntrySchema = ModelEntryBaseSchema.extend({+ provider: z.literal('google-gemini'),+ /** Override the provider's default base URL (e.g. point at a local proxy). */+ base_url: z.string().url().optional(),+})+ .strict()+ .openapi('GoogleGeminiProviderModelEntry');++/**+ * Generic OpenAI-compatible provider. Requires an explicit `base_url` since+ * there is no canonical endpoint.+ */+const GenericProviderEntrySchema = ModelEntryBaseSchema.extend({+ provider: z.literal('generic'),+ /** Deployment-specific base URL — required. */+ base_url: z.string().url(),+ /** API format used by this endpoint. Only option today; defaults when absent. */+ api_format: z.literal('openai-chat-completions').optional(),+})+ .strict()+ .openapi('GenericProviderModelEntry');++export const ModelEntrySchema = z+ .discriminatedUnion('provider', [+ OpenAIProviderEntrySchema,+ AnthropicProviderEntrySchema,+ GoogleGeminiProviderEntrySchema,+ GenericProviderEntrySchema,+ ]).openapi('ModelEntry');export const ModelsFileSchema = z.object({- base_url: z- .string()- .url()- .transform(url => url.replace(/\/$/, '')),models: z.array(ModelEntrySchema),}).strict()diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c9d1a9ee9..dcc2419dd 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -109,6 +109,18 @@ importers:packages/harness:dependencies:+ '@ai-sdk/anthropic':+ specifier: ^4.0.25+ version: 4.0.25([email protected])+ '@ai-sdk/google':+ specifier: ^4.0.29+ version: 4.0.29([email protected])+ '@ai-sdk/openai':+ specifier: ^4.0.25+ version: 4.0.25([email protected])+ '@ai-sdk/openai-compatible':+ specifier: ^3.0.18+ version: 3.0.18([email protected])'@daytona/sdk':specifier: ^0.190.0@@ -127,6 +139,9 @@ importers:'@opentelemetry/core':specifier: ^2.9.0version: 2.10.0(@opentelemetry/[email protected])+ ai:+ specifier: ^7.0.44+ version: 7.0.44([email protected])dedent:specifier: ^1.7.2version: 1.7.2([email protected])@@ -261,6 +276,46 @@ packages:'@ag-ui/[email protected]':resolution: {integrity: sha512-11UocR7fFdMWw503bWCX2IOK15vbWfxT11Mn9xOiPBVO/UVcn57ywGrlLL4UaBlPgmUTvuzr2yYR2ElSqiN2wQ==}+ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-cU61cAdOfgiuJngCPq61FI9pD7KB+gTQm1KQRIwvsf09gGJmliTR8UQdw9x9UWOOqvEYaeuE6NrtXUUCLOjsbQ==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-YFkZhWH1ksIoSO2b0S/nAboiYBYRhkrmbC/6vOBsKZz6Y0FfBBaIjWBu3lW2n2x7cXEH776RUKGOiEzJYmu+Pw==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-JpFlWrQUJ2xJY4j1NWVZlcPlK9TEPyNwy5n0blWa8mW1juqasyEdHva3yBGD9iFFJ0enChtDUr+FB6ZurRr4Sg==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-IKoElyW52LakNTmqiwM2yXZYTvOnA8XCzP+yUoV5Pf+pudqOL8rsXToXiuLlJapMcWpTpXH0EoWK42zNJd9Dew==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-iUQSY1EfAyyA8nFAm1UyiHjVyt93i29CVo+8gjT8oxaVjqZIZOlP2Ni4pIHrQTQFrTUD//B83r9NTw0u90/6RQ==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-LWDrmYWkZoAJwMbToUglpR6AmgvtnNtCCGeU9MKlXiSV3Yiu4u73/pVA18sQAAe3kZtCRBY9P31lAfnPekWWyg==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8++ '@ai-sdk/[email protected]':+ resolution: {integrity: sha512-tbHKNLirllUNF3ZlkCsXnwab2ZV1Sl4b1H/Cp9ruCce15IBmskE8Gwkk0yo9xDWY+jho2of7lVXtwSsyrq7cwQ==}+ engines: {node: '>=22'}+'@alloc/[email protected]':resolution: {integrity: sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==}engines: {node: '>=10'}@@ -3208,12 +3263,19 @@ packages:'@ungap/[email protected]':resolution: {integrity: sha512-60YRaenCQcVjYEKOcG824+DRGGIQ3VKErcBoAEDJZz5bKIs2ZG+X/H9Nk+Q6EVkwJk5QNApxbrc5QtBSwtrXAg==}+ '@vercel/[email protected]':+ resolution: {integrity: sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug==}+ engines: {node: '>= 20'}+'@vitejs/[email protected]':resolution: {integrity: sha512-gUu9hwfWvvEDBBmgtAowQCojwZmJ5mcLn3aufeCsitijs3+f2NsrPtlAWIR6OPiqljl96GVCUbLe0HyqIpVaoA==}engines: {node: ^14.18.0 || >=16.0.0}peerDependencies:vite: ^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0+ '@workflow/[email protected]':+ resolution: {integrity: sha512-pav4F2BoirECWR7Nf1TKt+2eETcBj7jj4cBefQ8VXQCA6NPkaKeLfj/zMgi+3zYV5ZIBT4GuUiphsj0/b9hPQQ==}+resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}engines: {node: '>=6.5'}@@ -3240,6 +3302,12 @@ packages:resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==}engines: {node: '>= 8.0.0'}+ resolution: {integrity: sha512-OMJ8v3ZzMIt90wIZRluD8c/VgvmX8SvAajGNYEbCeCAiCbr62yhCgXC7MvZRrQ0pOTfLZRohztUVrt5A08gKNg==}+ engines: {node: '>=22'}+ peerDependencies:+ zod: ^3.25.76 || ^4.1.8+resolution: {integrity: sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==}peerDependencies:@@ -4634,6 +4702,9 @@ packages:resolution: {integrity: sha512-fQhoXdcvc3V28x7C7BMs4P5+kNlgUURe2jmUT1T//oBRMDrqy1QPelJimwZGo7Hg9VPV3EQV5Bnq4hbFy2vetA==}+ resolution: {integrity: sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==}+resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==}@@ -6020,6 +6091,10 @@ packages:resolution: {integrity: sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==}+ resolution: {integrity: sha512-IDxfleLmmbSskfWSUATiN1nfn2rDuvnMOqb5CWR92iIfojA0Ud+ulOAAEQ57LPr9rWmsreUyf5lwyao+7GNNVw==}+ engines: {node: '>=20.18.1'}+resolution: {integrity: sha512-yLSH4py7oFH3oG/9K+XWrz1pSi3dfUrWEnInbxMfArOfc1+33BlGPQtLsOYwvdMy11AwUBetYuaRxSPqgkq+8g==}engines: {node: '>=4'}@@ -6334,6 +6409,50 @@ snapshots:dependencies:zod: 3.25.76+ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ '@vercel/oidc': 3.2.0+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ zod: 3.25.76++ '@ai-sdk/[email protected]([email protected])':+ dependencies:+ '@ai-sdk/provider': 4.0.4+ '@standard-schema/spec': 1.1.0+ '@workflow/serde': 4.1.0+ eventsource-parser: 3.1.0+ undici: 7.29.0+ zod: 3.25.76++ '@ai-sdk/[email protected]':+ dependencies:+ json-schema: 0.4.0+'@alloc/[email protected]': {}'@assistant-ui/[email protected](@assistant-ui/[email protected](@assistant-ui/[email protected](@types/[email protected])([email protected]))(@types/[email protected])([email protected]))(@assistant-ui/[email protected](@types/[email protected])([email protected]))(@types/[email protected])([email protected]([email protected](@opentelemetry/[email protected])))([email protected])([email protected](@opentelemetry/[email protected]))([email protected](@types/[email protected])([email protected])([email protected]([email protected])))':@@ -9486,6 +9605,8 @@ snapshots:'@ungap/[email protected]': {}+ '@vercel/[email protected]': {}+'@vitejs/[email protected]([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected]))':dependencies:'@babel/core': 7.29.7([email protected])@@ -9498,6 +9619,8 @@ snapshots:transitivePeerDependencies:- supports-color+ '@workflow/[email protected]': {}+dependencies:event-target-shim: 5.0.1@@ -9523,6 +9646,13 @@ snapshots:dependencies:humanize-ms: 1.2.1+ dependencies:+ '@ai-sdk/gateway': 4.0.33([email protected])+ '@ai-sdk/provider': 4.0.4+ '@ai-sdk/provider-utils': 5.0.16([email protected])+ zod: 3.25.76+optionalDependencies:ajv: 8.20.0@@ -11235,6 +11365,8 @@ snapshots:+ [email protected]: {}+@@ -13007,6 +13139,8 @@ snapshots:+ [email protected]: {}+