From 103cd021870e14e4ac4141af17f1b5b3bae9d6ee Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:33:50 -0600 Subject: [PATCH 1/3] feat(effect-ai): AnthropicLanguageModel over /anthropic/v1/messages --- .../src/AnthropicLanguageModel.ts | 90 ++++- .../internal/anthropic-messages/request.ts | 325 ++++++++++++++++++ .../internal/anthropic-messages/response.ts | 218 ++++++++++++ .../src/internal/anthropic-messages/schema.ts | 177 ++++++++++ .../src/internal/anthropic-messages/stream.ts | 224 ++++++++++++ .../src/internal/models/anthropic.ts | 57 +++ .../test/cells/AnthropicLanguageModel.ts | 51 +++ .../AnthropicLanguageModel/image.json | 38 ++ .../json-native-claude-sonnet-5.5.json | 54 +++ .../AnthropicLanguageModel/json-tools.json | 166 +++++++++ .../fixtures/AnthropicLanguageModel/json.json | 57 +++ .../AnthropicLanguageModel/reasoning.json | 35 ++ .../stream-error.synthetic.json | 31 ++ .../AnthropicLanguageModel/stream-tools.json | 143 ++++++++ .../stream-truncated.synthetic.json | 31 ++ .../AnthropicLanguageModel/stream.json | 31 ++ .../fixtures/AnthropicLanguageModel/text.json | 30 ++ .../AnthropicLanguageModel/tools.json | 141 ++++++++ 18 files changed, 1889 insertions(+), 10 deletions(-) create mode 100644 packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts create mode 100644 packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts create mode 100644 packages/foundry-effect-ai/src/internal/anthropic-messages/schema.ts create mode 100644 packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts create mode 100644 packages/foundry-effect-ai/src/internal/models/anthropic.ts create mode 100644 packages/foundry-effect-ai/test/cells/AnthropicLanguageModel.ts create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/image.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-native-claude-sonnet-5.5.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-tools.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/reasoning.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-error.synthetic.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-tools.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-truncated.synthetic.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/text.json create mode 100644 packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/tools.json diff --git a/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts b/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts index 06516e0..32958cd 100644 --- a/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts +++ b/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts @@ -1,25 +1,95 @@ +// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe. /** Anthropic language models through Foundry's `/anthropic/v1/messages` route. */ +import { toCodecAnthropic } from 'effect/ai/AnthropicStructuredOutput'; import * as LanguageModel from 'effect/ai/LanguageModel'; import * as AiModel from 'effect/ai/Model'; -import type * as Effect from 'effect/Effect'; +import * as Effect from 'effect/Effect'; import * as Layer from 'effect/Layer'; -import type { FoundryClient } from './FoundryClient.ts'; -import { notImplemented } from './internal/notImplemented.ts'; +import * as Stream from 'effect/Stream'; +import { FoundryClient } from './FoundryClient.ts'; +import { makeRequest, type Target } from './internal/anthropic-messages/request.ts'; +import { decodeResponse } from './internal/anthropic-messages/response.ts'; +import * as Wire from './internal/anthropic-messages/schema.ts'; +import { decodeStream } from './internal/anthropic-messages/stream.ts'; +import { models } from './internal/models/anthropic.ts'; export const providerName = 'foundry-anthropic'; /** A catalog alias or a raw Foundry model RID. */ +export type ModelId = keyof typeof models | (string & Record); + +/** Request-wide settings, sent as the Messages fields of the same name. */ +export interface Config { + /** Defaults to the model's output limit, or 4096 for a target outside the catalog. */ + readonly max_tokens?: number | undefined; + /** + * For example `{ type: 'adaptive' }`, or `{ type: 'enabled', budget_tokens: 2048 }` on models + * before Claude 5.5. Thinking blocks replay with their signatures on later turns. + */ + readonly thinking?: Wire.Thinking | undefined; +} + +const known: Readonly< + Record< + string, + { readonly rid: string; readonly maxOutputTokens: number; readonly nativeJson?: boolean } + > +> = models; + +/** Known aliases and RIDs get their catalog flags; any other target passes through as is. */ +const resolve = (modelId: string): Target => { + const entry = + known[modelId] ?? Object.values(known).find((candidate) => candidate.rid === modelId); + return { + rid: entry?.rid ?? modelId, + maxOutputTokens: entry?.maxOutputTokens ?? 4096, + nativeJson: entry?.nativeJson === true, + }; +}; + +const path = '/anthropic/v1/messages'; +const headers = { 'anthropic-version': '2023-06-01' }; + export const model = ( - modelId: string, + modelId: ModelId, + config?: Config, ): AiModel.Model => - AiModel.make(providerName, modelId, layer({ model: modelId })); + AiModel.make(providerName, modelId, layer({ model: modelId, config })); -export const make = (_options: { - readonly model: string; -}): Effect.Effect => - notImplemented('AnthropicLanguageModel.make'); +/** + * Claude Opus and Sonnet 5.5 reject forced tool use, so their structured output is native + * `output_config.format`; other models answer through a forced JSON tool. Tool input is never + * streamed eagerly: Foundry does not support it. + */ +export const make = Effect.fnUntraced(function* (options: { + readonly model: ModelId; + readonly config?: Config | undefined; +}) { + const client = yield* FoundryClient; + const target = resolve(options.model); + const config = options.config; + return yield* LanguageModel.make({ + codecTransformer: toCodecAnthropic, + generateText: Effect.fnUntraced(function* (providerOptions) { + const request = yield* makeRequest(providerOptions, target, config); + const body = yield* client.json({ path, headers, body: request, schema: Wire.Body }); + return yield* decodeResponse(body, providerOptions, target); + }), + streamText: (providerOptions) => + makeRequest(providerOptions, target, config).pipe( + Effect.map((request) => + decodeStream( + client.sse({ path, headers, body: { ...request, stream: true } }), + providerOptions, + ), + ), + Stream.unwrap, + ), + }); +}); export const layer = (options: { - readonly model: string; + readonly model: ModelId; + readonly config?: Config | undefined; }): Layer.Layer => Layer.effect(LanguageModel.LanguageModel, make(options)); diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts new file mode 100644 index 0000000..62f6cf0 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts @@ -0,0 +1,325 @@ +// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe. +/** + * Effect AI prompt, tools and response format → a Messages request. Every system message goes + * to the top-level `system`; consecutive user and tool messages form one user turn, tool results + * first. Thinking blocks replay with their signatures. Structured output is native + * `output_config.format` on models that reject forced tool use, and a forced JSON tool otherwise. + */ +import * as AiError from 'effect/ai/AiError'; +import { toCodecAnthropic } from 'effect/ai/AnthropicStructuredOutput'; +import * as LanguageModel from 'effect/ai/LanguageModel'; +import type * as Prompt from 'effect/ai/Prompt'; +import * as Tool from 'effect/ai/Tool'; +import * as Effect from 'effect/Effect'; +import * as Base64 from 'effect/encoding/Base64'; +import * as Predicate from 'effect/Predicate'; +import type * as Wire from './schema.ts'; + +/** Metadata and prompt-option namespace for thinking signatures and raw tool input. */ +export const metadataKey = 'foundry-anthropic'; + +export interface Target { + /** The wire model: a catalog RID or a raw target. */ + readonly rid: string; + /** The default `max_tokens`. */ + readonly maxOutputTokens: number; + /** Structured output through `output_config.format` instead of a forced JSON tool. */ + readonly nativeJson: boolean; +} + +/** The public module's request-wide settings. */ +export interface Settings { + readonly max_tokens?: number | undefined; + readonly thinking?: Wire.Thinking | undefined; +} + +const fail = (reason: AiError.AiErrorReason) => + AiError.make({ module: 'AnthropicLanguageModel', method: 'makeRequest', reason }); + +/** A field this adapter stored under `metadataKey` on an earlier response part. */ +export const option = (options: Prompt.ProviderOptions, field: string): unknown => { + const value = options[metadataKey]; + return Predicate.isObject(value) ? value[field] : undefined; +}; + +const text = (options: Prompt.ProviderOptions, field: string): string | undefined => { + const value = option(options, field); + return typeof value === 'string' ? value : undefined; +}; + +type JsonFormat = Extract; + +/** The response format when structured output goes through an injected JSON tool. */ +export const jsonTool = ( + options: LanguageModel.ProviderOptions, + target: Target, +): JsonFormat | undefined => + options.responseFormat.type === 'json' && !target.nativeJson ? options.responseFormat : undefined; + +/** Strict tools are sent the Anthropic codec's schema; others the default one. */ +export const isStrict = (tool: Tool.Any): boolean => Tool.getStrictMode(tool) === true; + +const image = Effect.fnUntraced(function* (part: Prompt.FilePart) { + if (!part.mediaType.startsWith('image/')) { + return yield* fail( + new AiError.InvalidUserInputError({ + description: `Unsupported file type ${part.mediaType}; send images only`, + }), + ); + } + const block: Wire.ImageBlock = { + type: 'image', + source: + part.data instanceof URL + ? { type: 'url', url: part.data.href } + : { + type: 'base64', + media_type: part.mediaType, + data: typeof part.data === 'string' ? part.data : Base64.encode(part.data), + }, + }; + return block; +}); + +/** JSON text for wire values; a void result still serializes as `null`. */ +export const stringify = (value: unknown): string => + typeof value === 'string' ? value : JSON.stringify(value ?? null); + +const toolResult = Effect.fnUntraced(function* (part: Prompt.ToolResultPart) { + let content: string | Array; + if (part.modelOutput === undefined) { + content = stringify(part.result); + } else { + content = []; + for (const output of part.modelOutput) { + content.push( + output.type === 'text' ? { type: 'text', text: output.text } : yield* image(output), + ); + } + } + const block: Wire.UserBlock = { + type: 'tool_result', + tool_use_id: part.id, + content, + is_error: part.isFailure, + }; + return block; +}); + +const assistantBlocks = (message: Prompt.AssistantMessage): Array => + message.content.flatMap((part): Array => { + if (part.type === 'text') { + return [{ type: 'text', text: part.text }]; + } + if (part.type === 'reasoning') { + // Only signed or redacted thinking can be replayed. + const signature = text(part.options, 'signature'); + const redacted = text(part.options, 'redactedData'); + if (redacted !== undefined) { + return [{ type: 'redacted_thinking', data: redacted }]; + } + return signature === undefined ? [] : [{ type: 'thinking', thinking: part.text, signature }]; + } + if (part.type === 'tool-call' && !part.providerExecuted) { + // Strict calls replay the input the model produced, not the normalized parameters. + const input = option(part.options, 'input') ?? part.params; + return [{ type: 'tool_use', id: part.id, name: part.name, input }]; + } + // Assistant files, provider-executed tools and approvals have no wire block here. + return []; + }); + +const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { + const system: Array = []; + const messages: Array = []; + // Consecutive user and tool messages share one user turn, with tool results first. + let results: Array = []; + let user: Array = []; + const flushUser = () => { + if (results.length + user.length > 0) { + messages.push({ role: 'user', content: [...results, ...user] }); + results = []; + user = []; + } + }; + for (const message of prompt.content) { + switch (message.role) { + case 'system': + system.push({ type: 'text', text: message.content }); + break; + case 'user': + for (const part of message.content) { + user.push(part.type === 'text' ? { type: 'text', text: part.text } : yield* image(part)); + } + break; + case 'tool': + for (const part of message.content) { + if (part.type === 'tool-result') { + results.push(yield* toolResult(part)); + } + } + break; + case 'assistant': { + flushUser(); + const content = assistantBlocks(message); + const last = messages.at(-1); + if (last?.role === 'assistant') { + last.content.push(...content); + } else if (content.length > 0) { + messages.push({ role: 'assistant', content }); + } + break; + } + } + } + flushUser(); + // Anthropic rejects trailing whitespace in a final assistant prefill. + const last = messages.at(-1); + const final = last?.role === 'assistant' ? last.content.at(-1) : undefined; + if (last?.role === 'assistant' && final?.type === 'text') { + last.content[last.content.length - 1] = { type: 'text', text: final.text.trimEnd() }; + } + return { system, messages }; +}); + +const unsupportedSchema = (error: unknown) => + fail( + new AiError.UnsupportedSchemaError({ + description: error instanceof Error ? error.message : String(error), + }), + ); + +const encodeTool = Effect.fnUntraced(function* (tool: Tool.Any) { + if (Tool.isProviderDefined(tool)) { + return yield* fail( + new AiError.InvalidRequestError({ + description: `Provider-defined tool '${tool.name}' is not supported`, + }), + ); + } + const strict = Tool.getStrictMode(tool); + const inputSchema = yield* Effect.try({ + try: () => + Tool.getJsonSchema(tool, { + transformer: strict === true ? toCodecAnthropic : LanguageModel.defaultCodecTransformer, + }), + catch: unsupportedSchema, + }); + const description = Tool.getDescription(tool); + const encoded: Wire.Tool = { + name: String(tool.name), + input_schema: inputSchema, + ...(description === undefined ? undefined : { description }), + ...(strict === undefined ? undefined : { strict }), + }; + return encoded; +}); + +/** + * In JSON-tool mode the application tools allowed by the choice travel with the injected tool, + * and the model must call one of them, one at a time: the JSON tool alone is forced by name. + */ +const encodeTools = Effect.fnUntraced(function* ( + options: LanguageModel.ProviderOptions, + json: JsonFormat | undefined, +) { + const choice = options.toolChoice; + if (json !== undefined) { + const { objectName, schema } = json; + if (options.tools.some((tool) => tool.name === objectName)) { + return yield* fail( + new AiError.ToolConfigurationError({ + toolName: objectName, + description: `Tool '${objectName}' collides with the structured output tool; pass another objectName`, + }), + ); + } + const inputSchema = yield* Effect.try({ + try: () => Tool.getJsonSchemaFromSchema(schema, { transformer: toCodecAnthropic }), + catch: unsupportedSchema, + }); + const allowed = options.tools.filter( + (tool) => + choice !== 'none' && + (typeof choice !== 'object' || !('tool' in choice) || tool.name === choice.tool), + ); + const tools: Array = []; + for (const tool of allowed) { + tools.push(yield* encodeTool(tool)); + } + tools.push({ + name: objectName, + description: 'Respond with a JSON object.', + input_schema: inputSchema, + }); + const toolChoice: Wire.ToolChoice = + allowed.length === 0 + ? { type: 'tool', name: objectName, disable_parallel_tool_use: true } + : { type: 'any', disable_parallel_tool_use: true }; + return { tools, tool_choice: toolChoice }; + } + if (options.tools.length === 0) { + const required = + choice === 'required' || + (typeof choice === 'object' && ('tool' in choice || choice.mode === 'required')); + if (required) { + return yield* fail( + new AiError.ToolConfigurationError({ + toolName: typeof choice === 'object' && 'tool' in choice ? String(choice.tool) : '', + description: 'The tool choice requires a tool call, but no tools are available', + }), + ); + } + return {}; + } + const tools: Array = []; + for (const tool of options.tools) { + tools.push(yield* encodeTool(tool)); + } + let toolChoice: Wire.ToolChoice; + if (choice === 'required') { + toolChoice = { type: 'any' }; + } else if (typeof choice === 'string') { + toolChoice = { type: choice }; + } else if ('oneOf' in choice) { + toolChoice = { type: choice.mode === 'required' ? 'any' : 'auto' }; + } else { + toolChoice = { type: 'tool', name: String(choice.tool) }; + } + return { tools, tool_choice: toolChoice }; +}); + +const encodeFormat = Effect.fnUntraced(function* ( + options: LanguageModel.ProviderOptions, + target: Target, +) { + if (options.responseFormat.type !== 'json' || !target.nativeJson) { + return {}; + } + const { schema } = options.responseFormat; + const jsonSchema = yield* Effect.try({ + try: () => Tool.getJsonSchemaFromSchema(schema, { transformer: toCodecAnthropic }), + catch: unsupportedSchema, + }); + return { output_config: { format: { type: 'json_schema', schema: jsonSchema } } } as const; +}); + +export const makeRequest = Effect.fnUntraced(function* ( + options: LanguageModel.ProviderOptions, + target: Target, + settings: Settings | undefined, +) { + const { system, messages } = yield* encodePrompt(options.prompt); + const tools = yield* encodeTools(options, jsonTool(options, target)); + const format = yield* encodeFormat(options, target); + const request: Wire.Request = { + model: target.rid, + max_tokens: settings?.max_tokens ?? target.maxOutputTokens, + messages, + ...(system.length === 0 ? undefined : { system }), + ...(settings?.thinking === undefined ? undefined : { thinking: settings.thinking }), + ...tools, + ...format, + }; + return request; +}); diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts new file mode 100644 index 0000000..15d7781 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts @@ -0,0 +1,218 @@ +// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe. +/** A Messages JSON body → Effect AI response parts, plus helpers the stream shares. */ +import * as AiError from 'effect/ai/AiError'; +import { toCodecAnthropic } from 'effect/ai/AnthropicStructuredOutput'; +import type * as LanguageModel from 'effect/ai/LanguageModel'; +import type * as Response from 'effect/ai/Response'; +import * as Tool from 'effect/ai/Tool'; +import * as Effect from 'effect/Effect'; +import * as Predicate from 'effect/Predicate'; +import * as Schema from 'effect/Schema'; +import { isStrict, jsonTool, metadataKey, stringify, type Target } from './request.ts'; +import * as Wire from './schema.ts'; + +const module = 'AnthropicLanguageModel'; +const isJson = Schema.is(Schema.Json); + +export const invalidResponse = (method: string, description: string): AiError.AiError => + AiError.make({ module, method, reason: new AiError.InvalidResponseError({ description }) }); + +/** + * An Anthropic error envelope inside a successful HTTP response or stream. Rate limits, billing + * and invalid requests keep their own reasons; anything else, such as `overloaded_error` or + * `api_error`, is a provider failure. The error type stays in metadata. + */ +export const providerError = ( + method: string, + error: typeof Wire.ErrorObject.Type, +): AiError.AiError => { + const type = error.type ?? undefined; + const description = [type, error.message].filter(Boolean).join(': ') || 'Response failed'; + const metadata = { [metadataKey]: { type: type ?? null } }; + let reason: AiError.AiErrorReason; + if (type === 'rate_limit_error') { + reason = new AiError.RateLimitError({ metadata }); + } else if (type === 'billing_error') { + reason = new AiError.QuotaExhaustedError({ metadata }); + } else if (type === 'invalid_request_error') { + reason = new AiError.InvalidRequestError({ description, metadata }); + } else { + reason = new AiError.InternalProviderError({ description, metadata }); + } + return AiError.make({ module, method, reason }); +}; + +const finishReasons: Readonly> = { + end_turn: 'stop', + stop_sequence: 'stop', + tool_use: 'tool-calls', + max_tokens: 'length', + model_context_window_exceeded: 'length', + pause_turn: 'pause', + refusal: 'content-filter', +}; + +/** A structured-output tool call is the answer, not a tool call to run. */ +export const finishReason = ( + stopReason: string | null | undefined, + structured = false, +): Response.FinishReason => { + const reason = stopReason == null ? undefined : finishReasons[stopReason]; + if (reason === undefined) { + return 'unknown'; + } + return structured && reason === 'tool-calls' ? 'stop' : reason; +}; + +/** Total input adds cache reads and writes to the uncached count; unreported counts stay undefined. */ +export const usage = ( + wire: typeof Wire.Usage.Type | null | undefined, +): typeof Response.Usage.Encoded => { + const uncached = wire?.input_tokens ?? undefined; + const cacheRead = wire?.cache_read_input_tokens ?? undefined; + const cacheWrite = wire?.cache_creation_input_tokens ?? undefined; + return { + inputTokens: { + total: uncached === undefined ? undefined : uncached + (cacheRead ?? 0) + (cacheWrite ?? 0), + uncached, + cacheRead, + cacheWrite, + }, + outputTokens: { + total: wire?.output_tokens ?? undefined, + text: undefined, + reasoning: undefined, + }, + }; +}; + +const decodeSchema = + >(schema: S) => + (method: string, value: unknown) => + Effect.mapError(Schema.decodeUnknownEffect(schema)(value), (error) => + AiError.make({ module, method, reason: AiError.InvalidResponseError.fromSchemaError(error) }), + ); + +export const decodeBlock = decodeSchema(Wire.ContentBlock); +export const decodeDelta = decodeSchema(Wire.Delta); + +/** Whether `value` has a string `type` in `known`, so it should be decoded rather than skipped. */ +export const isKnown = (value: unknown, known: ReadonlySet): boolean => + Predicate.hasProperty(value, 'type') && typeof value.type === 'string' && known.has(value.type); + +/** Thinking signatures and redacted thinking, kept for replay under `metadataKey`. */ +export const metadata = ( + fields: Readonly>, +): Record> => ({ + [metadataKey]: Object.fromEntries( + Object.entries(fields).flatMap(([key, value]): Array<[string, string]> => + value === undefined ? [] : [[key, value]], + ), + ), +}); + +/** + * A completed tool call. Strict tools were sent the Anthropic codec's schema, so valid input is + * decoded with it and re-encoded for the tool's own schema; Toolkit validates. The input the + * model produced then stays in metadata, since replay must send it rather than that shape. + */ +export const toolCall = Effect.fnUntraced(function* ( + method: string, + options: LanguageModel.ProviderOptions, + call: { readonly id: string; readonly name: string; readonly input: unknown }, +) { + const tool = options.tools.find((candidate) => candidate.name === call.name); + if (tool === undefined && options.toolCallValidation !== 'deferred') { + return yield* AiError.make({ + module, + method, + reason: new AiError.ToolNotFoundError({ + toolName: call.name, + availableTools: options.tools.map((candidate) => String(candidate.name)), + }), + }); + } + let params = call.input; + if (tool !== undefined && isStrict(tool) && !(Tool.isDynamic(tool) && tool.jsonSchema)) { + type Codec = (input: unknown) => Effect.Effect; + const normalized = Effect.suspend(() => { + // Tool parameter schemas need no services: Toolkit decodes them without any. + /* oxlint-disable typescript/no-unsafe-type-assertion, typescript/no-unsafe-argument */ + const decode = Schema.decodeUnknownEffect( + toCodecAnthropic(tool.parametersSchema).codec, + ) as Codec; + const encode = Schema.encodeUnknownEffect(Schema.toCodecJson(tool.parametersSchema)) as Codec; + /* oxlint-enable typescript/no-unsafe-type-assertion, typescript/no-unsafe-argument */ + return Effect.flatMap(decode(call.input), encode); + }); + params = yield* Effect.orElseSucceed(normalized, () => call.input); + } + const part: Response.ToolCallPartEncoded = { + type: 'tool-call', + id: call.id, + name: call.name, + params, + ...(params === call.input || !isJson(call.input) + ? undefined + : { metadata: { [metadataKey]: { input: call.input } } }), + }; + return part; +}); + +export const decodeResponse = Effect.fnUntraced(function* ( + body: typeof Wire.Body.Type, + options: LanguageModel.ProviderOptions, + target: Target, +) { + if (!('content' in body)) { + return yield* providerError('generateText', body.error); + } + const parts: Array = [ + { type: 'response-metadata', id: body.id, modelId: body.model }, + ]; + const blocks: Array = []; + for (const raw of body.content) { + if (isKnown(raw, Wire.contentBlockTypes)) { + blocks.push(yield* decodeBlock('generateText', raw)); + } + } + // The JSON tool's input is the answer; prose beside it would break JSON decoding. + const json = jsonTool(options, target)?.objectName; + const structured = blocks.some((block) => block.type === 'tool_use' && block.name === json); + for (const block of blocks) { + switch (block.type) { + case 'text': + if (!structured) { + parts.push({ type: 'text', text: block.text }); + } + break; + case 'thinking': + parts.push({ + type: 'reasoning', + text: block.thinking, + metadata: metadata({ signature: block.signature }), + }); + break; + case 'redacted_thinking': + parts.push({ + type: 'reasoning', + text: '', + metadata: metadata({ redactedData: block.data }), + }); + break; + case 'tool_use': + parts.push( + block.name === json + ? { type: 'text', text: stringify(block.input) } + : yield* toolCall('generateText', options, block), + ); + break; + } + } + parts.push({ + type: 'finish', + reason: finishReason(body.stop_reason, structured), + usage: usage(body.usage), + }); + return parts; +}); diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/schema.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/schema.ts new file mode 100644 index 0000000..496d884 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/schema.ts @@ -0,0 +1,177 @@ +// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe. +/** + * Wire shapes for `/anthropic/v1/messages`. Requests are plain interfaces; response schemas + * decode only the fields the adapter reads. Content blocks, deltas and stream events are + * dispatched on `type`: unknown types are skipped, and a known type with a malformed payload + * fails. + */ +import * as Schema from 'effect/Schema'; + +export interface TextBlock { + readonly type: 'text'; + readonly text: string; +} + +export interface ImageBlock { + readonly type: 'image'; + readonly source: + | { readonly type: 'base64'; readonly media_type: string; readonly data: string } + | { readonly type: 'url'; readonly url: string }; +} + +export type UserBlock = + | TextBlock + | ImageBlock + | { + readonly type: 'tool_result'; + readonly tool_use_id: string; + readonly content: string | ReadonlyArray; + readonly is_error: boolean; + }; + +export type AssistantBlock = + | TextBlock + | { readonly type: 'thinking'; readonly thinking: string; readonly signature: string } + | { readonly type: 'redacted_thinking'; readonly data: string } + | { + readonly type: 'tool_use'; + readonly id: string; + readonly name: string; + readonly input: unknown; + }; + +export type Message = + | { readonly role: 'user'; readonly content: Array } + | { readonly role: 'assistant'; readonly content: Array }; + +export interface Tool { + readonly name: string; + readonly description?: string; + readonly input_schema: unknown; + readonly strict?: boolean; +} + +export type ToolChoice = ( + | { readonly type: 'auto' | 'any' | 'none' } + | { readonly type: 'tool'; readonly name: string } +) & { readonly disable_parallel_tool_use?: boolean }; + +/** Extended thinking, sent as given. Claude 5.5 models accept only `adaptive`. */ +export type Thinking = + | { readonly type: 'adaptive' } + | { readonly type: 'enabled'; readonly budget_tokens: number } + | { readonly type: 'disabled' }; + +export interface Request { + readonly model: string; + readonly max_tokens: number; + readonly messages: ReadonlyArray; + readonly system?: ReadonlyArray; + readonly tools?: ReadonlyArray; + readonly tool_choice?: ToolChoice; + readonly thinking?: Thinking; + readonly output_config?: { + readonly format: { readonly type: 'json_schema'; readonly schema: unknown }; + }; + readonly stream?: true; +} + +const Count = Schema.optional(Schema.NullOr(Schema.Finite)); + +export const Usage = Schema.Struct({ + input_tokens: Count, + output_tokens: Count, + cache_read_input_tokens: Count, + cache_creation_input_tokens: Count, +}); + +const Text = Schema.optional(Schema.NullOr(Schema.String)); + +/** `{ type, message }`, read leniently: either field may be absent or null. */ +export const ErrorObject = Schema.Struct({ type: Text, message: Text }); + +export const ToolUseBlock = Schema.Struct({ + type: Schema.Literal('tool_use'), + id: Schema.String, + name: Schema.String, + input: Schema.Unknown, +}); + +export const ContentBlock = Schema.Union([ + Schema.Struct({ type: Schema.Literal('text'), text: Schema.String }), + Schema.Struct({ + type: Schema.Literal('thinking'), + thinking: Schema.String, + signature: Schema.optional(Schema.String), + }), + Schema.Struct({ type: Schema.Literal('redacted_thinking'), data: Schema.String }), + ToolUseBlock, +]); +export const contentBlockTypes: ReadonlySet = new Set([ + 'text', + 'thinking', + 'redacted_thinking', + 'tool_use', +]); + +export const Response = Schema.Struct({ + id: Schema.String, + model: Schema.String, + content: Schema.Array(Schema.Unknown), + stop_reason: Text, + usage: Schema.optional(Schema.NullOr(Usage)), +}); + +/** A 2xx body: a message, or an error envelope. */ +export const Body = Schema.Union([Response, Schema.Struct({ error: ErrorObject })]); + +export const Delta = Schema.Union([ + Schema.Struct({ type: Schema.Literal('text_delta'), text: Schema.String }), + Schema.Struct({ type: Schema.Literal('thinking_delta'), thinking: Schema.String }), + Schema.Struct({ type: Schema.Literal('signature_delta'), signature: Schema.String }), + Schema.Struct({ type: Schema.Literal('input_json_delta'), partial_json: Schema.String }), +]); +export const deltaTypes: ReadonlySet = new Set([ + 'text_delta', + 'thinking_delta', + 'signature_delta', + 'input_json_delta', +]); + +export const Event = Schema.Union([ + Schema.Struct({ + type: Schema.Literal('message_start'), + message: Schema.Struct({ + id: Schema.String, + model: Schema.String, + usage: Schema.optional(Schema.NullOr(Usage)), + }), + }), + Schema.Struct({ + type: Schema.Literal('content_block_start'), + index: Schema.Finite, + content_block: Schema.Unknown, + }), + Schema.Struct({ + type: Schema.Literal('content_block_delta'), + index: Schema.Finite, + delta: Schema.Unknown, + }), + Schema.Struct({ type: Schema.Literal('content_block_stop'), index: Schema.Finite }), + Schema.Struct({ + type: Schema.Literal('message_delta'), + delta: Schema.Struct({ stop_reason: Text }), + usage: Schema.optional(Schema.NullOr(Usage)), + }), + Schema.Struct({ type: Schema.Literal('message_stop') }), + Schema.Struct({ type: Schema.Literal('error'), error: ErrorObject }), +]); +export const eventTypes: ReadonlySet = new Set([ + 'message_start', + 'content_block_start', + 'content_block_delta', + 'content_block_stop', + 'message_delta', + 'message_stop', + 'error', +]); diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts new file mode 100644 index 0000000..db0359a --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts @@ -0,0 +1,224 @@ +// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe. +/** + * Messages server-sent events → Effect AI stream parts. Blocks are tracked by `index`; a + * thinking block's signature arrives as a delta and is attached to its end part. `message_stop` + * closes any open blocks, emits the finish and stops reading. An in-band `error` fails the + * stream, and a stream that ends before `message_stop` fails with `InvalidResponseError`. + */ +import * as AiError from 'effect/ai/AiError'; +import type * as LanguageModel from 'effect/ai/LanguageModel'; +import type * as Response from 'effect/ai/Response'; +import * as Tool from 'effect/ai/Tool'; +import * as Effect from 'effect/Effect'; +import type * as Sse from 'effect/encoding/Sse'; +import * as Schema from 'effect/Schema'; +import * as Stream from 'effect/Stream'; +import { stringify } from './request.ts'; +import { + decodeBlock, + decodeDelta, + finishReason, + invalidResponse, + isKnown, + metadata, + providerError, + toolCall, + usage, +} from './response.ts'; +import * as Wire from './schema.ts'; + +const method = 'streamText'; +const decodeData = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); +const decodeEvent = Schema.decodeUnknownEffect(Wire.Event); + +type Block = + | { readonly type: 'text' } + | { readonly type: 'reasoning'; signature?: string; readonly redactedData?: string } + | { readonly type: 'tool'; readonly id: string; readonly name: string; input: string }; + +export const decodeStream = ( + events: Stream.Stream, + options: LanguageModel.ProviderOptions, +): Stream.Stream => { + let finished = false; + let stopReason: string | null | undefined; + // `message_delta` usage is cumulative: each reported count replaces the earlier one. + const counts: Record = {}; + const blocks = new Map(); + + const mergeUsage = (wire: typeof Wire.Usage.Type | null | undefined) => { + for (const [key, value] of Object.entries(wire ?? {})) { + if (typeof value === 'number') { + counts[key] = value; + } + } + }; + + const end = Effect.fnUntraced(function* (index: number) { + const block = blocks.get(index); + blocks.delete(index); + const id = String(index); + if (block === undefined) { + return []; + } + if (block.type === 'text') { + return [{ type: 'text-end', id } as const]; + } + if (block.type === 'reasoning') { + return [ + { + type: 'reasoning-end', + id, + metadata: metadata({ signature: block.signature, redactedData: block.redactedData }), + } as const, + ]; + } + const input = yield* Effect.try({ + try: () => Tool.unsafeSecureJsonParse(block.input === '' ? '{}' : block.input), + catch: (cause) => + AiError.make({ + module: 'AnthropicLanguageModel', + method, + reason: new AiError.ToolParameterValidationError({ + toolName: block.name, + description: `Tool input is not valid JSON: ${String(cause)}`, + }), + }), + }); + return [ + { type: 'tool-params-end', id: block.id } as const, + yield* toolCall(method, options, { id: block.id, name: block.name, input }), + ]; + }); + + const handle = Effect.fnUntraced(function* (event: Sse.Event) { + const parts: Array = []; + const data = yield* Effect.mapError(decodeData(event.data), (error) => + invalidResponse(method, `Malformed event data: ${error.message}`), + ); + if (!isKnown(data, Wire.eventTypes)) { + return parts; + } + const decoded = yield* Effect.mapError(decodeEvent(data), (error) => + invalidResponse(method, error.message), + ); + switch (decoded.type) { + case 'message_start': + mergeUsage(decoded.message.usage); + parts.push({ + type: 'response-metadata', + id: decoded.message.id, + modelId: decoded.message.model, + }); + break; + case 'content_block_start': { + if (!isKnown(decoded.content_block, Wire.contentBlockTypes)) { + break; + } + const block = yield* decodeBlock(method, decoded.content_block); + const id = String(decoded.index); + switch (block.type) { + case 'text': + blocks.set(decoded.index, { type: 'text' }); + parts.push({ type: 'text-start', id }); + if (block.text !== '') { + parts.push({ type: 'text-delta', id, delta: block.text }); + } + break; + case 'thinking': + blocks.set(decoded.index, { + type: 'reasoning', + ...(block.signature === undefined ? undefined : { signature: block.signature }), + }); + parts.push({ type: 'reasoning-start', id }); + if (block.thinking !== '') { + parts.push({ type: 'reasoning-delta', id, delta: block.thinking }); + } + break; + case 'redacted_thinking': + blocks.set(decoded.index, { type: 'reasoning', redactedData: block.data }); + parts.push({ type: 'reasoning-start', id }); + break; + case 'tool_use': { + // Input usually streams as deltas after an empty placeholder object. + const seeded = + typeof block.input === 'object' && + block.input !== null && + Object.keys(block.input).length > 0 + ? stringify(block.input) + : ''; + blocks.set(decoded.index, { + type: 'tool', + id: block.id, + name: block.name, + input: seeded, + }); + parts.push({ type: 'tool-params-start', id: block.id, name: block.name }); + if (seeded !== '') { + parts.push({ type: 'tool-params-delta', id: block.id, delta: seeded }); + } + break; + } + } + break; + } + case 'content_block_delta': { + const block = blocks.get(decoded.index); + if (block === undefined || !isKnown(decoded.delta, Wire.deltaTypes)) { + break; + } + const delta = yield* decodeDelta(method, decoded.delta); + const id = String(decoded.index); + if (delta.type === 'text_delta' && block.type === 'text') { + parts.push({ type: 'text-delta', id, delta: delta.text }); + } else if (delta.type === 'thinking_delta' && block.type === 'reasoning') { + parts.push({ type: 'reasoning-delta', id, delta: delta.thinking }); + } else if (delta.type === 'signature_delta' && block.type === 'reasoning') { + block.signature = delta.signature; + } else if (delta.type === 'input_json_delta' && block.type === 'tool') { + if (delta.partial_json !== '') { + block.input += delta.partial_json; + parts.push({ type: 'tool-params-delta', id: block.id, delta: delta.partial_json }); + } + } else { + return yield* invalidResponse( + method, + `Delta ${delta.type} does not match content block ${decoded.index}`, + ); + } + break; + } + case 'content_block_stop': + parts.push(...(yield* end(decoded.index))); + break; + case 'message_delta': + mergeUsage(decoded.usage); + stopReason = decoded.delta.stop_reason ?? stopReason; + break; + case 'message_stop': + finished = true; + for (const index of blocks.keys()) { + parts.push(...(yield* end(index))); + } + parts.push({ type: 'finish', reason: finishReason(stopReason), usage: usage(counts) }); + break; + case 'error': + return yield* providerError(method, decoded.error); + } + return parts; + }); + + return events.pipe( + Stream.mapEffect(handle), + // Stop reading at the terminal event, even if the connection stays open. + Stream.takeUntil(() => finished), + Stream.flattenIterable, + Stream.concat( + Stream.suspend(() => + finished + ? Stream.empty + : Stream.fail(invalidResponse(method, 'Stream ended before message_stop')), + ), + ), + ); +}; diff --git a/packages/foundry-effect-ai/src/internal/models/anthropic.ts b/packages/foundry-effect-ai/src/internal/models/anthropic.ts new file mode 100644 index 0000000..0134ae1 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/models/anthropic.ts @@ -0,0 +1,57 @@ +/** + * Anthropic models on Foundry, by catalog alias. `maxOutputTokens` is the default `max_tokens`; + * `nativeJson` marks models that reject forced tool use, so structured output is sent as + * `output_config.format` instead of a forced JSON tool. + */ +export const models = { + 'claude-haiku-4.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-5-haiku', + maxOutputTokens: 64_000, + }, + 'claude-haiku-5.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-5-5-haiku', + maxOutputTokens: 128_000, + }, + 'claude-opus-4.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-5-opus', + maxOutputTokens: 64_000, + }, + 'claude-opus-4.6': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-6-opus', + maxOutputTokens: 128_000, + }, + 'claude-opus-4.7': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-7-opus', + maxOutputTokens: 128_000, + }, + 'claude-opus-4.8': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-8-opus', + maxOutputTokens: 128_000, + }, + 'claude-opus-5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-5-opus', + maxOutputTokens: 128_000, + }, + 'claude-opus-5.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-5-5-opus', + maxOutputTokens: 128_000, + nativeJson: true, + }, + 'claude-sonnet-4.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-5-sonnet', + maxOutputTokens: 64_000, + }, + 'claude-sonnet-4.6': { + rid: 'ri.language-model-service..language-model.anthropic-claude-4-6-sonnet', + maxOutputTokens: 128_000, + }, + 'claude-sonnet-5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-5-sonnet', + maxOutputTokens: 128_000, + }, + 'claude-sonnet-5.5': { + rid: 'ri.language-model-service..language-model.anthropic-claude-5-5-sonnet', + maxOutputTokens: 128_000, + nativeJson: true, + }, +} as const; diff --git a/packages/foundry-effect-ai/test/cells/AnthropicLanguageModel.ts b/packages/foundry-effect-ai/test/cells/AnthropicLanguageModel.ts new file mode 100644 index 0000000..4467bc1 --- /dev/null +++ b/packages/foundry-effect-ai/test/cells/AnthropicLanguageModel.ts @@ -0,0 +1,51 @@ +import { expect } from '@effect/vitest'; +import type * as Response from 'effect/ai/Response'; +import * as Effect from 'effect/Effect'; +import * as AnthropicLanguageModel from '../../src/AnthropicLanguageModel.ts'; +import { languageCells } from '../capabilities.ts'; +import type { Env } from '../harness.ts'; + +const namespace = 'foundry-anthropic'; + +/** Thinking replays only with its signature, ahead of the call. */ +function expectReplay(parts: ReadonlyArray): void { + const reasoning = parts.flatMap((part) => + part.type === 'reasoning' || part.type === 'reasoning-end' ? [part.metadata[namespace]] : [], + ); + expect(reasoning).toContainEqual(expect.objectContaining({ signature: expect.any(String) })); +} + +// Claude Haiku 4.5 predates adaptive thinking, so reasoning cells set a thinking budget. +const shared = languageCells({ + model: ({ reasoning }) => + AnthropicLanguageModel.model( + 'claude-haiku-4.5', + reasoning ? { thinking: { type: 'enabled', budget_tokens: 1024 } } : undefined, + ), + replay: expectReplay, +}); + +// Claude Sonnet 5.5 rejects forced tool use and budget thinking: the shared `json` program runs +// with native `output_config.format` and adaptive thinking. +const nativeJson = languageCells({ + model: () => + AnthropicLanguageModel.model('claude-sonnet-5.5', { thinking: { type: 'adaptive' } }), + replay: expectReplay, +})['json']; + +export const cells = { + ...shared, + 'json-native-claude-sonnet-5.5': (env: Env) => + Effect.andThen( + nativeJson?.(env) ?? Effect.void, + Effect.sync(() => { + if (!env.live) { + expect(env.requests[0]?.body).toMatchObject({ + thinking: { type: 'adaptive' }, + output_config: { format: { type: 'json_schema' } }, + }); + expect(env.requests[0]?.body).not.toHaveProperty('tool_choice'); + } + }), + ), +}; diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/image.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/image.json new file mode 100644 index 0000000..b6463f0 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/image.json @@ -0,0 +1,38 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"text\":\"Red\",\"type\":\"text\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":46,\"output_tokens\":4,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What color fills this image? Answer with one word." + }, + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/png", + "data": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAAxElEQVR42u3RMQ0AAAjAsPk3DTLgaDIFa1M6zAIAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABAADAAgAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAjAixYgaMOy89oM6gAAAABJRU5ErkJggg==" + } + } + ] + } + ] + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-native-claude-sonnet-5.5.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-native-claude-sonnet-5.5.json new file mode 100644 index 0000000..7ffcfef --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-native-claude-sonnet-5.5.json @@ -0,0 +1,54 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"claude-sonnet-5-5\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"text\":\"{\\\"city\\\":\\\"Paris\\\",\\\"country\\\":\\\"France\\\"}\",\"type\":\"text\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":225,\"output_tokens\":17,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-5-5-sonnet", + "max_tokens": 128000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Name the capital of France and its country." + } + ] + } + ], + "thinking": { + "type": "adaptive" + }, + "output_config": { + "format": { + "type": "json_schema", + "schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "city", + "country" + ], + "additionalProperties": false + } + } + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-tools.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-tools.json new file mode 100644 index 0000000..ae7c1f8 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json-tools.json @@ -0,0 +1,166 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"id\":\"redacted-id-2\",\"name\":\"GetWeather\",\"input\":{\"city\":\"Paris\"},\"type\":\"tool_use\"}],\"stop_reason\":\"tool_use\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":780,\"output_tokens\":29,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence. Report the city and temperature." + } + ] + } + ], + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + }, + { + "name": "generateObject", + "description": "Respond with a JSON object.", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "temperatureC": { + "type": "number" + } + }, + "required": [ + "city", + "temperatureC" + ], + "additionalProperties": false + } + } + ], + "tool_choice": { + "type": "any", + "disable_parallel_tool_use": true + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-3\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"id\":\"redacted-id-4\",\"name\":\"generateObject\",\"input\":{\"city\":\"Paris\",\"temperatureC\":21},\"type\":\"tool_use\"}],\"stop_reason\":\"tool_use\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":856,\"output_tokens\":47,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence. Report the city and temperature." + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "tool_use", + "id": "redacted-id-2", + "name": "GetWeather", + "input": { + "city": "Paris" + } + } + ] + }, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "redacted-id-2", + "content": "{\"temperatureC\":21,\"conditions\":\"sunny\"}", + "is_error": false + } + ] + } + ], + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + }, + { + "name": "generateObject", + "description": "Respond with a JSON object.", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "temperatureC": { + "type": "number" + } + }, + "required": [ + "city", + "temperatureC" + ], + "additionalProperties": false + } + } + ], + "tool_choice": { + "type": "any", + "disable_parallel_tool_use": true + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json.json new file mode 100644 index 0000000..a3de6d3 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/json.json @@ -0,0 +1,57 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"id\":\"redacted-id-2\",\"name\":\"generateObject\",\"input\":{\"city\":\"Paris\",\"country\":\"France\"},\"type\":\"tool_use\"}],\"stop_reason\":\"tool_use\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":682,\"output_tokens\":41,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Name the capital of France and its country." + } + ] + } + ], + "tools": [ + { + "name": "generateObject", + "description": "Respond with a JSON object.", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "city", + "country" + ], + "additionalProperties": false + } + } + ], + "tool_choice": { + "type": "tool", + "name": "generateObject", + "disable_parallel_tool_use": true + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/reasoning.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/reasoning.json new file mode 100644 index 0000000..00b24b6 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/reasoning.json @@ -0,0 +1,35 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":82,\"output_tokens\":3,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}},\"type\":\"message_start\"}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"index\":0,\"content_block\":{\"thinking\":\"\",\"type\":\"thinking\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"Let me den\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"ote:\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"\\n- b\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" = cost of the ball in\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" cents\\n- t\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" = cost of the bat in\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" cents\\n\\nFrom the problem,\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" I have\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" two equations:\\n1.\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" t +\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" b = 110 (\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"bat\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" and\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" ball cost 110 cents\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" total)\\n2. t\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" = b + 100 \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"(bat costs 100 \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"cents more than the\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" ball)\\n\\nSubstituting\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" equation 2 into equation\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" 1:\\n(b\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" + 100) + b\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" = 110\\n2b\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" + 100 = \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"110\\n2b = \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"10\\nb = 5\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"\\n\\nLet\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" me verify:\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"\\n- Ball costs 5\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" cents\\n- Bat\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" costs 5 + \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"100 = 105 \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"cents\\n- Total:\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" 5 + 105\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" = 110 cents\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" ✓\\n-\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" Bat costs 100 more\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" than ball: 105 \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"- 5 = \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"100 \",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"✓\\n\\nThe\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" ball\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" costs 5 cents.\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"signature\":\"EtUFCoQBCBQQAhgCKkD7fCTH8UnKrs6ZLp1znaf9JFqUH+qe/gjlJnaK3yOLRoQtgVSzVHgs79njjwwB6+tsVqzLrtWiv6utw1Op5/tJMhljbGF1ZGUtaGFpa3UtNC01LTIwMjUxMDAxOABCCHRoaW5raW5nWgw3MTQzMjc0Njc3NDWoAcji/tcGEgxJGTjH9wW0b4bEwwIaDEADTTfGzFPsvrSpPCIw21OGfxQ9CStTXZsN9nybi6N7R0m2V3feE/NwO7scVlgXqtQU6dkX1ncFK8oTO9flKv0Dn2OmwKOTQU7v7q8N9gpTB2JhMToZ0CYsLFUDCgi9GN9JH7ylVK88xe0cYHQDNl1UXjOAeL9zXJSD4Re/Gce5KZhMcIcye3yhOuMyfJhUvLKvftjmBJo5OT9LUD3L9/HGQegONGZGo6XBYPqxXl6WjwTxokF6+yVWvRUX9+ESxLLjEgzryKctp7bYbztVWoMGAH3XL4rYT9Xs6+v1U4XOnXXXBorXu7SmeKd/XJs5qtfDd4qoZ04hnfLIyx+AriEZ4Zi+jXyB7hDmXz4OAuUp6cB64YUNf8ocUFGnklx8mZAGaaE7pxgzL3TZNvYMbCPd5Sp1r+61IPioI9fd+0WSqfnQj8kXtTnVRnU6wAk4ZXkADjr/QhUAbqEAupgF1eqwXUO4bV7pkAXkK8dhPbsGr0rxQ6k9Zj/CJK/9R5FF8HhJz30KiRUt70huLEdz/yYfzKEGjovWucGNvRjveBrqXq6+lfiKLE+vbfCtqBI5UM1qDLTFntnsbajcd2A4sBiAvodv2QIiFWtyWEA779ObZy0Q/myP3h6Xv5QY4N+fNUOHrsj5k8qU8SAbodACtPam/9W4MTnpWdlCBJ6fsmlx+EBVVRxbkRxGZVdSPfxhdC4vYQ0QXpgTq8PKa6K8tup7Bz0MCdT6x226SpAl7tSTmRaDOD+4Pfs29vUhPVcYAQ==\",\"type\":\"signature_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":0,\"type\":\"content_block_stop\"}\n\nevent: content_block_start\ndata: {\"index\":1,\"content_block\":{\"text\":\"\",\"type\":\"text\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"#\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" Step-\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"by-step\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" reasoning\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\":\\n\\nLet b\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" = cost of the ball in\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" cents\\n\\n**\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"Set\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" up equations\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\":**\\n- Equation\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" 1: bat\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" + ball = 110\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"\\n- Equation 2:\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" bat = ball + 100\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"\\n\\n**Substitute equation \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"2 into equation 1\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\":**\\n-\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" (b\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" + 100) + b\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" = 110\\n- \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"2b + 100 \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"= 110\\n- \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"2b = 10\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"\\n- b = 5\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"\\n\\n**Verify:**\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"\\n- Ball:\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" 5 cents\\n-\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" Bat: 105\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" cents\\n- Total:\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" 5 + 105\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" = 110 \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"✓\\n- Difference\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\": 105 - \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"5 = 100 \",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"✓\\n\\n**5\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"**\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":1,\"type\":\"content_block_stop\"}\n\nevent: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null},\"usage\":{\"output_tokens\":384,\"input_tokens\":82,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"server_tool_use\":null},\"type\":\"message_delta\"}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "A bat and a ball cost 110 cents in total. The bat costs 100 cents more than the ball. How many cents does the ball cost? Reason step by step, then answer with just the number." + } + ] + } + ], + "thinking": { + "type": "enabled", + "budget_tokens": 1024 + }, + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-error.synthetic.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-error.synthetic.json new file mode 100644 index 0000000..0b8b305 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-error.synthetic.json @@ -0,0 +1,31 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":14,\"output_tokens\":1,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}},\"type\":\"message_start\"}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: error\ndata: {\"type\":\"error\",\"error\":{\"type\":\"overloaded_error\",\"message\":\"Overloaded\"}}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Reply with exactly one word: hello" + } + ] + } + ], + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-tools.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-tools.json new file mode 100644 index 0000000..2d557d3 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-tools.json @@ -0,0 +1,143 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-1\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":634,\"output_tokens\":7,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}},\"type\":\"message_start\"}\n\nevent: content_block_start\ndata: {\"index\":0,\"content_block\":{\"thinking\":\"\",\"type\":\"thinking\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"The user is asking me to first\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" figure\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" out which European capital hosted the Summer Olympics\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" in both\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" 1900 and 1924\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\", then\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" call the GetWeather tool for that\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" city.\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"\\n\\nLet me think\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" about this:\\n-\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" 1900 Summer Olympics:\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" Paris, France\\n- 1924\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" Summer Olympics: Paris\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\", France\\n\\nSo Paris\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" is\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" the European\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" capital that hosted the Summer Olympics in both\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" 1900 and 1924\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\".\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"\\n\\nNow\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\" I need to call Get\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"thinking\":\"Weather for Paris.\",\"type\":\"thinking_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"signature\":\"EogFCpwBCBQQARgCKkBRhMCZj4bMBMbmbI4SIVTPdqLeEV8PdQNRap+03cT2BIy2MRbsR5vajVfn7NyYuoncGma6WYOTRz8Z3I50KGncMhljbGF1ZGUtaGFpa3UtNC01LTIwMjUxMDAxOABCCHRoaW5raW5nWgwxOTY3MzM3OTYwMjlyENzLeG9s7AgIhP8RRMel8tCIAQGoAb7i/tcGsAECEgymF5QUyc+Vzm25IM8aDHBp9s1e4OOIdXvQkyIwZl/0hR1xCnV0/KeOVP8UyVHljvJDinmqXaGTvwZrTRrgWkjfX2/Vxps4QOekFYj1KpgD1GSbA89oGXzptZkdnrdFjq/8P10ec5VCxrVZb74HvomKjJqiTbub70XwhWZE8NEU9Im/4ZshbkczdpgYSfvUsB671YKvybsFm7x7a1cvlw3EU0RWbIGxijRWSDtqvaOoPQNF98moRR8T5iPguvzS7fPelrCB3GO+/1Jbw9YEfcZCTO1a/iALmFnYsfwsAvRisi9AqioHcjARQLsK25HZ899FyA3ApWF2v8O8ODfAxMJM4s8A76EXccxs9DE8APQbeQzxEuv2GX04uXV6LdftUjuQn97DqssxuSx0vqc0jxxQT7ED+fEbHSN+0/k1A2NPLzXEc6ALS0Gv2FlXyWzAZTJTtjuXK1n/AWMqiu+9lWYcsFLhz7mXkBzHhqEN2DBCkuHWQ1Xmrs1qgxM3HmyhWC/SXYJQwfxLPQ1or45xnTRHS6IcJH1kT4ogumDMzUQ3lD8KXo5gYoOqHiRIelq5LO1Y3IfBQ45dpS3fHZsgLc0UiPbuTzIa+iC/+NTiToMqJipujUDvtUsTOwp7xzILgt7kU6qlHaAJGAE=\",\"type\":\"signature_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":0,\"type\":\"content_block_stop\"}\n\nevent: content_block_start\ndata: {\"index\":1,\"content_block\":{\"text\":\"\",\"type\":\"text\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\"The\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" European capital that hosted the Summer Olympics in\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" both\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" 1900 and 1924\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" is\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" **Paris, France**.\\n\\nNow\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" let\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" me get\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" the current\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" weather for\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":1,\"delta\":{\"text\":\" Paris:\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":1,\"type\":\"content_block_stop\"}\n\nevent: content_block_start\ndata: {\"index\":2,\"content_block\":{\"id\":\"redacted-id-2\",\"name\":\"GetWeather\",\"input\":{},\"type\":\"tool_use\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":2,\"delta\":{\"partial_json\":\"\",\"type\":\"input_json_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":2,\"delta\":{\"partial_json\":\"{\\\"city\\\"\",\"type\":\"input_json_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":2,\"delta\":{\"partial_json\":\": \\\"Pari\",\"type\":\"input_json_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":2,\"delta\":{\"partial_json\":\"s\\\"}\",\"type\":\"input_json_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":2,\"type\":\"content_block_stop\"}\n\nevent: message_delta\ndata: {\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null,\"stop_details\":null},\"usage\":{\"output_tokens\":202,\"input_tokens\":634,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"server_tool_use\":null},\"type\":\"message_delta\"}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence." + } + ] + } + ], + "thinking": { + "type": "enabled", + "budget_tokens": 1024 + }, + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ], + "tool_choice": { + "type": "auto" + }, + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-3\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":859,\"output_tokens\":1,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}},\"type\":\"message_start\"}\n\nevent: content_block_start\ndata: {\"index\":0,\"content_block\":{\"text\":\"\",\"type\":\"text\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\"Paris\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\", the\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" city\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" that hosted the 1900 and\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" 1924 Summer Olympics, is\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" currently experiencing sunny\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" weather\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\" with a temperature of 21°C\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\".\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":0,\"type\":\"content_block_stop\"}\n\nevent: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null},\"usage\":{\"output_tokens\":36,\"input_tokens\":859,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"server_tool_use\":null},\"type\":\"message_delta\"}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence." + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "The user is asking me to first figure out which European capital hosted the Summer Olympics in both 1900 and 1924, then call the GetWeather tool for that city.\n\nLet me think about this:\n- 1900 Summer Olympics: Paris, France\n- 1924 Summer Olympics: Paris, France\n\nSo Paris is the European capital that hosted the Summer Olympics in both 1900 and 1924.\n\nNow I need to call GetWeather for Paris.", + "signature": "EogFCpwBCBQQARgCKkBRhMCZj4bMBMbmbI4SIVTPdqLeEV8PdQNRap+03cT2BIy2MRbsR5vajVfn7NyYuoncGma6WYOTRz8Z3I50KGncMhljbGF1ZGUtaGFpa3UtNC01LTIwMjUxMDAxOABCCHRoaW5raW5nWgwxOTY3MzM3OTYwMjlyENzLeG9s7AgIhP8RRMel8tCIAQGoAb7i/tcGsAECEgymF5QUyc+Vzm25IM8aDHBp9s1e4OOIdXvQkyIwZl/0hR1xCnV0/KeOVP8UyVHljvJDinmqXaGTvwZrTRrgWkjfX2/Vxps4QOekFYj1KpgD1GSbA89oGXzptZkdnrdFjq/8P10ec5VCxrVZb74HvomKjJqiTbub70XwhWZE8NEU9Im/4ZshbkczdpgYSfvUsB671YKvybsFm7x7a1cvlw3EU0RWbIGxijRWSDtqvaOoPQNF98moRR8T5iPguvzS7fPelrCB3GO+/1Jbw9YEfcZCTO1a/iALmFnYsfwsAvRisi9AqioHcjARQLsK25HZ899FyA3ApWF2v8O8ODfAxMJM4s8A76EXccxs9DE8APQbeQzxEuv2GX04uXV6LdftUjuQn97DqssxuSx0vqc0jxxQT7ED+fEbHSN+0/k1A2NPLzXEc6ALS0Gv2FlXyWzAZTJTtjuXK1n/AWMqiu+9lWYcsFLhz7mXkBzHhqEN2DBCkuHWQ1Xmrs1qgxM3HmyhWC/SXYJQwfxLPQ1or45xnTRHS6IcJH1kT4ogumDMzUQ3lD8KXo5gYoOqHiRIelq5LO1Y3IfBQ45dpS3fHZsgLc0UiPbuTzIa+iC/+NTiToMqJipujUDvtUsTOwp7xzILgt7kU6qlHaAJGAE=" + }, + { + "type": "text", + "text": "The European capital that hosted the Summer Olympics in both 1900 and 1924 is **Paris, France**.\n\nNow let me get the current weather for Paris:" + }, + { + "type": "tool_use", + "id": "redacted-id-2", + "name": "GetWeather", + "input": { + "city": "Paris" + } + } + ] + }, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "redacted-id-2", + "content": "{\"temperatureC\":21,\"conditions\":\"sunny\"}", + "is_error": false + } + ] + } + ], + "thinking": { + "type": "enabled", + "budget_tokens": 1024 + }, + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ], + "tool_choice": { + "type": "auto" + }, + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-truncated.synthetic.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-truncated.synthetic.json new file mode 100644 index 0000000..86c34be --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream-truncated.synthetic.json @@ -0,0 +1,31 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":14,\"output_tokens\":1,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}},\"type\":\"message_start\"}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"index\":0,\"content_block\":{\"text\":\"\",\"type\":\"text\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\"Hello\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":0,\"type\":\"content_block_stop\"}\n\nevent: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null},\"usage\":{\"output_tokens\":4,\"input_tokens\":14,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"server_tool_use\":null},\"type\":\"message_delta\"}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Reply with exactly one word: hello" + } + ] + } + ], + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream.json new file mode 100644 index 0000000..cee0437 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/stream.json @@ -0,0 +1,31 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "event: message_start\ndata: {\"message\":{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":14,\"output_tokens\":1,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}},\"type\":\"message_start\"}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"index\":0,\"content_block\":{\"text\":\"\",\"type\":\"text\"},\"type\":\"content_block_start\"}\n\nevent: content_block_delta\ndata: {\"index\":0,\"delta\":{\"text\":\"Hello\",\"type\":\"text_delta\"},\"type\":\"content_block_delta\"}\n\nevent: content_block_stop\ndata: {\"index\":0,\"type\":\"content_block_stop\"}\n\nevent: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null},\"usage\":{\"output_tokens\":4,\"input_tokens\":14,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"server_tool_use\":null},\"type\":\"message_delta\"}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Reply with exactly one word: hello" + } + ] + } + ], + "stream": true + }, + "headers": { + "accept": "text/event-stream", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/text.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/text.json new file mode 100644 index 0000000..7428647 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/text.json @@ -0,0 +1,30 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"text\":\"Hello\",\"type\":\"text\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":14,\"output_tokens\":4,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Reply with exactly one word: hello" + } + ] + } + ] + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/tools.json b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/tools.json new file mode 100644 index 0000000..0f30e78 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/AnthropicLanguageModel/tools.json @@ -0,0 +1,141 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-1\",\"model\":\"claude-haiku-4-5-20251001\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"thinking\":\"Let me think about which European capital hosted the Summer Olympics in 1900 and 1924.\\n\\nThe 1900 Summer Olympics were held in Paris, France.\\nThe 1924 Summer Olympics were also held in Paris, France.\\n\\nSo Paris is the European capital that hosted the Summer Olympics in both 1900 and 1924.\\n\\nNow I need to call the GetWeather tool for Paris.\",\"signature\":\"EtIECpwBCBQQAhgCKkAmeTwWJNAD1qiX6JM61QwWx9ZD1mllLXgamo3ZKL4vsS5xktyZ6a7nVW7Q9AdFYm6xsPAORpttC1ChYV04Bql6MhljbGF1ZGUtaGFpa3UtNC01LTIwMjUxMDAxOABCCHRoaW5raW5nWgw3MTQzMjc0Njc3NDVyEIlqrJXg6eN31UAbgdyx0RaIAQGoAbzi/tcGsAECEgxAEO9jp946RugBSRUaDE5pg90pckVEUINjiyIwyejuoxJ1HHWH+CTIiYr8bozPE/o7d1bSENHUvYjN9a9SkiVDm11BcFu9v95yZBSbKuICFYh3oaGsjjARRbZ9zBRWgW8knSViIx6l9pCvdcUi4LLCNWTkNqRZYgPXMNeZlrRl0tjB59Vs5eRz71/cHlQ3P6NXSH6nSNHGh4ElVq/+xiZH3nYZZtuPsh3LGoVvaVImZBFwRaR89tgfo9aGsBRFQf7Iv/6g7U+1rpyUUy2LYh+fQcjkTn7C11OmWK7m6xaA5at3gWM39DFEirdJuyqEOa7f3hFwAskCJxeMYSNQ5Z9bvuHWDJH3HjrgdKmDE8TPWlp9lBBVmFEhA0RjRDGUPj/TncPtwXD6pXIoAU+fCkJGOKi0lMF5hKkg9f9Nm5OSLNAuAok4gv4CtoJvdkVuefrz1G/jr7JoOOJk4LB5Rki5vCj8kKsiif0hglmbHoRlBD4Wjr0h/mzA0JwO1VawL7o52dh9Yw/cwCHAVwtICK/3GJOVss2cQEm/8UXGr8BOrud9Cc2A1iyo2qRQwoc2NvNpGAE=\",\"type\":\"thinking\"},{\"text\":\"Paris, France hosted the Summer Olympics in both 1900 and 1924. Let me get the current weather there.\",\"type\":\"text\"},{\"id\":\"redacted-id-2\",\"name\":\"GetWeather\",\"input\":{\"city\":\"Paris\"},\"type\":\"tool_use\"}],\"stop_reason\":\"tool_use\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":634,\"output_tokens\":177,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":\"standard\"}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence." + } + ] + } + ], + "thinking": { + "type": "enabled", + "budget_tokens": 1024 + }, + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ], + "tool_choice": { + "type": "auto" + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "application/json", + "body": "{\"id\":\"redacted-id-3\",\"model\":\"anthropic.claude-haiku-4-5-20251001-v1:0\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"text\":\"The current weather in Paris is sunny with a temperature of 21°C.\",\"type\":\"text\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"stop_details\":null,\"usage\":{\"input_tokens\":834,\"output_tokens\":19,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"speed\":null}}", + "request": { + "path": "/anthropic/v1/messages", + "body": { + "model": "ri.language-model-service..language-model.anthropic-claude-4-5-haiku", + "max_tokens": 64000, + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Before calling any tool, work out which European capital hosted the Summer Olympics in 1900 and 1924. Then call the GetWeather tool for it and answer in one sentence." + } + ] + }, + { + "role": "assistant", + "content": [ + { + "type": "thinking", + "thinking": "Let me think about which European capital hosted the Summer Olympics in 1900 and 1924.\n\nThe 1900 Summer Olympics were held in Paris, France.\nThe 1924 Summer Olympics were also held in Paris, France.\n\nSo Paris is the European capital that hosted the Summer Olympics in both 1900 and 1924.\n\nNow I need to call the GetWeather tool for Paris.", + "signature": "EtIECpwBCBQQAhgCKkAmeTwWJNAD1qiX6JM61QwWx9ZD1mllLXgamo3ZKL4vsS5xktyZ6a7nVW7Q9AdFYm6xsPAORpttC1ChYV04Bql6MhljbGF1ZGUtaGFpa3UtNC01LTIwMjUxMDAxOABCCHRoaW5raW5nWgw3MTQzMjc0Njc3NDVyEIlqrJXg6eN31UAbgdyx0RaIAQGoAbzi/tcGsAECEgxAEO9jp946RugBSRUaDE5pg90pckVEUINjiyIwyejuoxJ1HHWH+CTIiYr8bozPE/o7d1bSENHUvYjN9a9SkiVDm11BcFu9v95yZBSbKuICFYh3oaGsjjARRbZ9zBRWgW8knSViIx6l9pCvdcUi4LLCNWTkNqRZYgPXMNeZlrRl0tjB59Vs5eRz71/cHlQ3P6NXSH6nSNHGh4ElVq/+xiZH3nYZZtuPsh3LGoVvaVImZBFwRaR89tgfo9aGsBRFQf7Iv/6g7U+1rpyUUy2LYh+fQcjkTn7C11OmWK7m6xaA5at3gWM39DFEirdJuyqEOa7f3hFwAskCJxeMYSNQ5Z9bvuHWDJH3HjrgdKmDE8TPWlp9lBBVmFEhA0RjRDGUPj/TncPtwXD6pXIoAU+fCkJGOKi0lMF5hKkg9f9Nm5OSLNAuAok4gv4CtoJvdkVuefrz1G/jr7JoOOJk4LB5Rki5vCj8kKsiif0hglmbHoRlBD4Wjr0h/mzA0JwO1VawL7o52dh9Yw/cwCHAVwtICK/3GJOVss2cQEm/8UXGr8BOrud9Cc2A1iyo2qRQwoc2NvNpGAE=" + }, + { + "type": "text", + "text": "Paris, France hosted the Summer Olympics in both 1900 and 1924. Let me get the current weather there." + }, + { + "type": "tool_use", + "id": "redacted-id-2", + "name": "GetWeather", + "input": { + "city": "Paris" + } + } + ] + }, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "redacted-id-2", + "content": "{\"temperatureC\":21,\"conditions\":\"sunny\"}", + "is_error": false + } + ] + } + ], + "thinking": { + "type": "enabled", + "budget_tokens": 1024 + }, + "tools": [ + { + "name": "GetWeather", + "input_schema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ], + "tool_choice": { + "type": "auto" + } + }, + "headers": { + "accept": "application/json", + "anthropic-version": "2023-06-01", + "content-type": "application/json" + } + } + } +] From f0446bee8cfa2654094ea3b415470ca3d2014f3b Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:36:02 -0600 Subject: [PATCH 2/3] fix(effect-ai): drop Anthropic tool calls cut off by max_tokens; no forced JSON tool with thinking --- packages/foundry-effect-ai/docs/PLAN.md | 2 +- .../internal/anthropic-messages/request.ts | 18 +++++--- .../internal/anthropic-messages/response.ts | 4 ++ .../src/internal/anthropic-messages/stream.ts | 42 ++++++++++++------- scripts/check-tree-shaking.mjs | 1 + 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/packages/foundry-effect-ai/docs/PLAN.md b/packages/foundry-effect-ai/docs/PLAN.md index 2cc32f7..d08e3af 100644 --- a/packages/foundry-effect-ai/docs/PLAN.md +++ b/packages/foundry-effect-ai/docs/PLAN.md @@ -93,7 +93,7 @@ Order: 1 → {2, 6} → {3, 4, 5, 7}. Braced phases run in parallel worktrees. A | 1 | `FoundryClient` with `json` only, a finished `internal/errors.ts`, fixture recording, `OpenAiDecisionModel`, and the phase 7 probe. `errors.ts` maps status to reason, the Foundry error envelope (vendor bodies arrive in `parameters.responseBody` as `Optional[...]`), and a lenient read of `error.{message,type,code,status}`; in-stream errors stay in each protocol folder. | Done: the `FoundryClient` and `OpenAiDecisionModel` rows pass offline and live, with a 401 (bad token) and a 404 `LanguageModelNotAvailable` (unknown model) recorded from each vendor route. | | 2 | `OpenAiLanguageModel` (Responses, written directly), `FoundryClient.sse` and `test/capabilities.ts` | Done: the OpenAI row passes offline and live on `gpt-6-luna`, with a recorded `reasoning_summary_part` stream and a `tools` replay of encrypted reasoning. Foundry's `reasoning_summary_part.added` carries only `type`, so reasoning blocks open from the summary deltas. | | 3 | `XaiLanguageModel`. Extract the Responses dialect from phase 2 and pass it as a value from each vendor module. | Done: the xAI row passes offline and live on `grok-4-6`. Foundry's xAI route takes assistant message IDs and streams OpenAI's events, so the dialect differs only in route, namespace, `system` role and always asking for encrypted reasoning. `json-tools` is flaky on Grok, which skips the tool in about 1 of 3 first turns. | -| 4 | `AnthropicLanguageModel` | Anthropic row has no todos, covering adaptive thinking and both structured-output modes; live passes. | +| 4 | `AnthropicLanguageModel` | Done: the Anthropic row passes offline and live on `claude-haiku-4.5` (budget thinking, forced JSON tool) and `claude-sonnet-5.5` (adaptive thinking, native `output_config.format`). With thinking on, the JSON tool is offered with `auto`, since thinking rejects forced tool use. | | 5 | `GoogleLanguageModel` | Gemini row has no todos; live passes. Highest risk: `effect/ai` has no Gemini structured-output codec. | | 6 | `OpenAiEmbeddingModel`. Depends only on phase 1, so it runs beside phase 2. Embedding RIDs equal their IDs (`text-embedding-*`). | Embedding row has no todos; all three catalog embedding models pass live. | | 7 | `OpenAiChatLanguageModel` and `internal/openai-chat-completions/`, on `/openai/v1/chat/completions` | Done: the Chat row passes offline and live on `gpt-6-luna`. The route takes `max_completion_tokens`, not `max_tokens`; streams end without `[DONE]`; OpenAI models return no reasoning text; GPT-6 Luna, Sol and 6.1 Terra accept tools only with `reasoning_effort: 'none'`. Anthropic, Google and xAI have no working Chat route. | diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts index 62f6cf0..3079428 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts @@ -218,10 +218,12 @@ const encodeTool = Effect.fnUntraced(function* (tool: Tool.Any) { /** * In JSON-tool mode the application tools allowed by the choice travel with the injected tool, * and the model must call one of them, one at a time: the JSON tool alone is forced by name. + * With thinking on, the choice stays `auto`, and a JSON text answer decodes as well. */ const encodeTools = Effect.fnUntraced(function* ( options: LanguageModel.ProviderOptions, json: JsonFormat | undefined, + thinking: boolean, ) { const choice = options.toolChoice; if (json !== undefined) { @@ -252,10 +254,15 @@ const encodeTools = Effect.fnUntraced(function* ( description: 'Respond with a JSON object.', input_schema: inputSchema, }); - const toolChoice: Wire.ToolChoice = - allowed.length === 0 - ? { type: 'tool', name: objectName, disable_parallel_tool_use: true } - : { type: 'any', disable_parallel_tool_use: true }; + let toolChoice: Wire.ToolChoice; + if (thinking) { + // Thinking rejects forced tool use, so a thinking model is asked to call a tool instead. + toolChoice = { type: 'auto', disable_parallel_tool_use: true }; + } else if (allowed.length === 0) { + toolChoice = { type: 'tool', name: objectName, disable_parallel_tool_use: true }; + } else { + toolChoice = { type: 'any', disable_parallel_tool_use: true }; + } return { tools, tool_choice: toolChoice }; } if (options.tools.length === 0) { @@ -310,7 +317,8 @@ export const makeRequest = Effect.fnUntraced(function* ( settings: Settings | undefined, ) { const { system, messages } = yield* encodePrompt(options.prompt); - const tools = yield* encodeTools(options, jsonTool(options, target)); + const thinking = settings?.thinking !== undefined && settings.thinking.type !== 'disabled'; + const tools = yield* encodeTools(options, jsonTool(options, target), thinking); const format = yield* encodeFormat(options, target); const request: Wire.Request = { model: target.rid, diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts index 15d7781..9ddaffb 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts @@ -179,6 +179,10 @@ export const decodeResponse = Effect.fnUntraced(function* ( // The JSON tool's input is the answer; prose beside it would break JSON decoding. const json = jsonTool(options, target)?.objectName; const structured = blocks.some((block) => block.type === 'tool_use' && block.name === json); + // A last tool call cut off by the output limit has partial input; the finish reports it. + if (body.stop_reason === 'max_tokens' && blocks.at(-1)?.type === 'tool_use') { + blocks.pop(); + } for (const block of blocks) { switch (block.type) { case 'text': diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts index db0359a..1761620 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts @@ -2,7 +2,8 @@ /** * Messages server-sent events → Effect AI stream parts. Blocks are tracked by `index`; a * thinking block's signature arrives as a delta and is attached to its end part. `message_stop` - * closes any open blocks, emits the finish and stops reading. An in-band `error` fails the + * closes any open blocks, emits the finish and stops reading; a tool call whose input the output + * limit cut off is dropped, and the finish reports `length`. An in-band `error` fails the * stream, and a stream that ends before `message_stop` fails with `InvalidResponseError`. */ import * as AiError from 'effect/ai/AiError'; @@ -11,6 +12,7 @@ import type * as Response from 'effect/ai/Response'; import * as Tool from 'effect/ai/Tool'; import * as Effect from 'effect/Effect'; import type * as Sse from 'effect/encoding/Sse'; +import * as Result from 'effect/Result'; import * as Schema from 'effect/Schema'; import * as Stream from 'effect/Stream'; import { stringify } from './request.ts'; @@ -45,6 +47,8 @@ export const decodeStream = ( // `message_delta` usage is cumulative: each reported count replaces the earlier one. const counts: Record = {}; const blocks = new Map(); + // Input cut off by the output limit is not a call; any other unparsable input fails at the end. + let invalidInput: AiError.AiError | undefined; const mergeUsage = (wire: typeof Wire.Usage.Type | null | undefined) => { for (const [key, value] of Object.entries(wire ?? {})) { @@ -73,21 +77,28 @@ export const decodeStream = ( } as const, ]; } - const input = yield* Effect.try({ - try: () => Tool.unsafeSecureJsonParse(block.input === '' ? '{}' : block.input), - catch: (cause) => - AiError.make({ - module: 'AnthropicLanguageModel', - method, - reason: new AiError.ToolParameterValidationError({ - toolName: block.name, - description: `Tool input is not valid JSON: ${String(cause)}`, + const paramsEnd = { type: 'tool-params-end', id: block.id } as const; + const input = yield* Effect.result( + Effect.try({ + try: () => Tool.unsafeSecureJsonParse(block.input === '' ? '{}' : block.input), + catch: (cause) => + AiError.make({ + module: 'AnthropicLanguageModel', + method, + reason: new AiError.ToolParameterValidationError({ + toolName: block.name, + description: `Tool input is not valid JSON: ${String(cause)}`, + }), }), - }), - }); + }), + ); + if (Result.isFailure(input)) { + invalidInput ??= input.failure; + return [paramsEnd]; + } return [ - { type: 'tool-params-end', id: block.id } as const, - yield* toolCall(method, options, { id: block.id, name: block.name, input }), + paramsEnd, + yield* toolCall(method, options, { id: block.id, name: block.name, input: input.success }), ]; }); @@ -200,6 +211,9 @@ export const decodeStream = ( for (const index of blocks.keys()) { parts.push(...(yield* end(index))); } + if (invalidInput !== undefined && finishReason(stopReason) !== 'length') { + return yield* invalidInput; + } parts.push({ type: 'finish', reason: finishReason(stopReason), usage: usage(counts) }); break; case 'error': diff --git a/scripts/check-tree-shaking.mjs b/scripts/check-tree-shaking.mjs index 65bcf57..d8e9099 100644 --- a/scripts/check-tree-shaking.mjs +++ b/scripts/check-tree-shaking.mjs @@ -33,6 +33,7 @@ const owners = { // Minified bytes with effect external. Set each module's real budget when its phase lands. const budgets = { + AnthropicLanguageModel: 25_000, FoundryClient: 6_800, OpenAiDecisionModel: 13_600, OpenAiEmbeddingModel: 8_400, From 066b79d1d0747839abc813f4710b2fe543ae1756 Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:51:07 -0600 Subject: [PATCH 3/3] fix(effect-ai): hold the last streamed Anthropic tool call, drop JSON-tool prose, reject unsupported forced tool choices --- packages/foundry-effect-ai/docs/PLAN.md | 2 +- .../src/AnthropicLanguageModel.ts | 8 +- .../internal/anthropic-messages/request.ts | 58 +++++++---- .../internal/anthropic-messages/response.ts | 10 +- .../src/internal/anthropic-messages/stream.ts | 97 +++++++++++-------- .../src/internal/models/anthropic.ts | 8 +- 6 files changed, 111 insertions(+), 72 deletions(-) diff --git a/packages/foundry-effect-ai/docs/PLAN.md b/packages/foundry-effect-ai/docs/PLAN.md index d08e3af..6e1c965 100644 --- a/packages/foundry-effect-ai/docs/PLAN.md +++ b/packages/foundry-effect-ai/docs/PLAN.md @@ -93,7 +93,7 @@ Order: 1 → {2, 6} → {3, 4, 5, 7}. Braced phases run in parallel worktrees. A | 1 | `FoundryClient` with `json` only, a finished `internal/errors.ts`, fixture recording, `OpenAiDecisionModel`, and the phase 7 probe. `errors.ts` maps status to reason, the Foundry error envelope (vendor bodies arrive in `parameters.responseBody` as `Optional[...]`), and a lenient read of `error.{message,type,code,status}`; in-stream errors stay in each protocol folder. | Done: the `FoundryClient` and `OpenAiDecisionModel` rows pass offline and live, with a 401 (bad token) and a 404 `LanguageModelNotAvailable` (unknown model) recorded from each vendor route. | | 2 | `OpenAiLanguageModel` (Responses, written directly), `FoundryClient.sse` and `test/capabilities.ts` | Done: the OpenAI row passes offline and live on `gpt-6-luna`, with a recorded `reasoning_summary_part` stream and a `tools` replay of encrypted reasoning. Foundry's `reasoning_summary_part.added` carries only `type`, so reasoning blocks open from the summary deltas. | | 3 | `XaiLanguageModel`. Extract the Responses dialect from phase 2 and pass it as a value from each vendor module. | Done: the xAI row passes offline and live on `grok-4-6`. Foundry's xAI route takes assistant message IDs and streams OpenAI's events, so the dialect differs only in route, namespace, `system` role and always asking for encrypted reasoning. `json-tools` is flaky on Grok, which skips the tool in about 1 of 3 first turns. | -| 4 | `AnthropicLanguageModel` | Done: the Anthropic row passes offline and live on `claude-haiku-4.5` (budget thinking, forced JSON tool) and `claude-sonnet-5.5` (adaptive thinking, native `output_config.format`). With thinking on, the JSON tool is offered with `auto`, since thinking rejects forced tool use. | +| 4 | `AnthropicLanguageModel` | Done: passes offline and live on `claude-haiku-4.5` (budget thinking, JSON tool) and `claude-sonnet-5.5` (adaptive thinking, `output_config.format`). Forced tool choices fail locally with thinking or on 5.5 models. Out of scope: `cache_control` and content prepopulated in `message_start`. | | 5 | `GoogleLanguageModel` | Gemini row has no todos; live passes. Highest risk: `effect/ai` has no Gemini structured-output codec. | | 6 | `OpenAiEmbeddingModel`. Depends only on phase 1, so it runs beside phase 2. Embedding RIDs equal their IDs (`text-embedding-*`). | Embedding row has no todos; all three catalog embedding models pass live. | | 7 | `OpenAiChatLanguageModel` and `internal/openai-chat-completions/`, on `/openai/v1/chat/completions` | Done: the Chat row passes offline and live on `gpt-6-luna`. The route takes `max_completion_tokens`, not `max_tokens`; streams end without `[DONE]`; OpenAI models return no reasoning text; GPT-6 Luna, Sol and 6.1 Terra accept tools only with `reasoning_effort: 'none'`. Anthropic, Google and xAI have no working Chat route. | diff --git a/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts b/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts index 32958cd..53a6056 100644 --- a/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts +++ b/packages/foundry-effect-ai/src/AnthropicLanguageModel.ts @@ -32,7 +32,11 @@ export interface Config { const known: Readonly< Record< string, - { readonly rid: string; readonly maxOutputTokens: number; readonly nativeJson?: boolean } + { + readonly rid: string; + readonly maxOutputTokens: number; + readonly rejectsForcedToolUse?: boolean; + } > > = models; @@ -43,7 +47,7 @@ const resolve = (modelId: string): Target => { return { rid: entry?.rid ?? modelId, maxOutputTokens: entry?.maxOutputTokens ?? 4096, - nativeJson: entry?.nativeJson === true, + rejectsForcedToolUse: entry?.rejectsForcedToolUse === true, }; }; diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts index 3079428..7341a31 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/request.ts @@ -23,8 +23,8 @@ export interface Target { readonly rid: string; /** The default `max_tokens`. */ readonly maxOutputTokens: number; - /** Structured output through `output_config.format` instead of a forced JSON tool. */ - readonly nativeJson: boolean; + /** Forced tool choices fail, and structured output uses `output_config.format`. */ + readonly rejectsForcedToolUse: boolean; } /** The public module's request-wide settings. */ @@ -54,7 +54,9 @@ export const jsonTool = ( options: LanguageModel.ProviderOptions, target: Target, ): JsonFormat | undefined => - options.responseFormat.type === 'json' && !target.nativeJson ? options.responseFormat : undefined; + options.responseFormat.type === 'json' && !target.rejectsForcedToolUse + ? options.responseFormat + : undefined; /** Strict tools are sent the Anthropic codec's schema; others the default one. */ export const isStrict = (tool: Tool.Any): boolean => Tool.getStrictMode(tool) === true; @@ -218,14 +220,35 @@ const encodeTool = Effect.fnUntraced(function* (tool: Tool.Any) { /** * In JSON-tool mode the application tools allowed by the choice travel with the injected tool, * and the model must call one of them, one at a time: the JSON tool alone is forced by name. - * With thinking on, the choice stays `auto`, and a JSON text answer decodes as well. + * Where forced tool use is unavailable (`unforced` says why), a forced choice fails before + * sending, and the JSON tool is offered with `auto`, so a JSON text answer decodes as well. */ const encodeTools = Effect.fnUntraced(function* ( options: LanguageModel.ProviderOptions, json: JsonFormat | undefined, - thinking: boolean, + unforced: string | undefined, ) { const choice = options.toolChoice; + const forced = + choice === 'required' || + (typeof choice === 'object' && ('tool' in choice || choice.mode === 'required')); + const toolName = typeof choice === 'object' && 'tool' in choice ? String(choice.tool) : ''; + if (forced && options.tools.length === 0 && json === undefined) { + return yield* fail( + new AiError.ToolConfigurationError({ + toolName, + description: 'The tool choice requires a tool call, but no tools are available', + }), + ); + } + if (forced && unforced !== undefined) { + return yield* fail( + new AiError.ToolConfigurationError({ + toolName, + description: `${unforced}; use toolChoice 'auto'`, + }), + ); + } if (json !== undefined) { const { objectName, schema } = json; if (options.tools.some((tool) => tool.name === objectName)) { @@ -255,8 +278,7 @@ const encodeTools = Effect.fnUntraced(function* ( input_schema: inputSchema, }); let toolChoice: Wire.ToolChoice; - if (thinking) { - // Thinking rejects forced tool use, so a thinking model is asked to call a tool instead. + if (unforced !== undefined) { toolChoice = { type: 'auto', disable_parallel_tool_use: true }; } else if (allowed.length === 0) { toolChoice = { type: 'tool', name: objectName, disable_parallel_tool_use: true }; @@ -266,17 +288,6 @@ const encodeTools = Effect.fnUntraced(function* ( return { tools, tool_choice: toolChoice }; } if (options.tools.length === 0) { - const required = - choice === 'required' || - (typeof choice === 'object' && ('tool' in choice || choice.mode === 'required')); - if (required) { - return yield* fail( - new AiError.ToolConfigurationError({ - toolName: typeof choice === 'object' && 'tool' in choice ? String(choice.tool) : '', - description: 'The tool choice requires a tool call, but no tools are available', - }), - ); - } return {}; } const tools: Array = []; @@ -300,7 +311,7 @@ const encodeFormat = Effect.fnUntraced(function* ( options: LanguageModel.ProviderOptions, target: Target, ) { - if (options.responseFormat.type !== 'json' || !target.nativeJson) { + if (options.responseFormat.type !== 'json' || !target.rejectsForcedToolUse) { return {}; } const { schema } = options.responseFormat; @@ -317,8 +328,13 @@ export const makeRequest = Effect.fnUntraced(function* ( settings: Settings | undefined, ) { const { system, messages } = yield* encodePrompt(options.prompt); - const thinking = settings?.thinking !== undefined && settings.thinking.type !== 'disabled'; - const tools = yield* encodeTools(options, jsonTool(options, target), thinking); + let unforced: string | undefined; + if (target.rejectsForcedToolUse) { + unforced = `${target.rid} rejects forced tool use`; + } else if (settings?.thinking !== undefined && settings.thinking.type !== 'disabled') { + unforced = 'Thinking rejects forced tool use'; + } + const tools = yield* encodeTools(options, jsonTool(options, target), unforced); const format = yield* encodeFormat(options, target); const request: Wire.Request = { model: target.rid, diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts index 9ddaffb..ca52805 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/response.ts @@ -176,17 +176,19 @@ export const decodeResponse = Effect.fnUntraced(function* ( blocks.push(yield* decodeBlock('generateText', raw)); } } - // The JSON tool's input is the answer; prose beside it would break JSON decoding. - const json = jsonTool(options, target)?.objectName; - const structured = blocks.some((block) => block.type === 'tool_use' && block.name === json); // A last tool call cut off by the output limit has partial input; the finish reports it. if (body.stop_reason === 'max_tokens' && blocks.at(-1)?.type === 'tool_use') { blocks.pop(); } + // In JSON-tool mode a tool call's input is the answer or the turn is an intermediate tool + // call; either way, prose beside it would break JSON decoding. + const json = jsonTool(options, target)?.objectName; + const structured = blocks.some((block) => block.type === 'tool_use' && block.name === json); + const prose = json === undefined || !blocks.some((block) => block.type === 'tool_use'); for (const block of blocks) { switch (block.type) { case 'text': - if (!structured) { + if (prose) { parts.push({ type: 'text', text: block.text }); } break; diff --git a/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts index 1761620..9606cf3 100644 --- a/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts +++ b/packages/foundry-effect-ai/src/internal/anthropic-messages/stream.ts @@ -2,9 +2,10 @@ /** * Messages server-sent events → Effect AI stream parts. Blocks are tracked by `index`; a * thinking block's signature arrives as a delta and is attached to its end part. `message_stop` - * closes any open blocks, emits the finish and stops reading; a tool call whose input the output - * limit cut off is dropped, and the finish reports `length`. An in-band `error` fails the - * stream, and a stream that ends before `message_stop` fails with `InvalidResponseError`. + * closes any open blocks, emits the finish and stops reading. The last tool call is held until + * the next block starts or the stop reason is known: one cut off by the output limit is dropped, + * and the finish reports `length`. An in-band `error` fails the stream, and a stream that ends + * before `message_stop` fails with `InvalidResponseError`. */ import * as AiError from 'effect/ai/AiError'; import type * as LanguageModel from 'effect/ai/LanguageModel'; @@ -12,7 +13,6 @@ import type * as Response from 'effect/ai/Response'; import * as Tool from 'effect/ai/Tool'; import * as Effect from 'effect/Effect'; import type * as Sse from 'effect/encoding/Sse'; -import * as Result from 'effect/Result'; import * as Schema from 'effect/Schema'; import * as Stream from 'effect/Stream'; import { stringify } from './request.ts'; @@ -33,10 +33,17 @@ const method = 'streamText'; const decodeData = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); const decodeEvent = Schema.decodeUnknownEffect(Wire.Event); +interface ToolBlock { + readonly type: 'tool'; + readonly id: string; + readonly name: string; + input: string; +} + type Block = | { readonly type: 'text' } | { readonly type: 'reasoning'; signature?: string; readonly redactedData?: string } - | { readonly type: 'tool'; readonly id: string; readonly name: string; input: string }; + | ToolBlock; export const decodeStream = ( events: Stream.Stream, @@ -47,8 +54,10 @@ export const decodeStream = ( // `message_delta` usage is cumulative: each reported count replaces the earlier one. const counts: Record = {}; const blocks = new Map(); - // Input cut off by the output limit is not a call; any other unparsable input fails at the end. - let invalidInput: AiError.AiError | undefined; + // Blocks of types this adapter skips; their deltas are skipped too. + const skipped = new Set(); + // The last finished tool call, which the output limit may have cut off. + let held: ToolBlock | undefined; const mergeUsage = (wire: typeof Wire.Usage.Type | null | undefined) => { for (const [key, value] of Object.entries(wire ?? {})) { @@ -58,7 +67,7 @@ export const decodeStream = ( } }; - const end = Effect.fnUntraced(function* (index: number) { + const end = (index: number): Array => { const block = blocks.get(index); blocks.delete(index); const id = String(index); @@ -66,7 +75,7 @@ export const decodeStream = ( return []; } if (block.type === 'text') { - return [{ type: 'text-end', id } as const]; + return [{ type: 'text-end', id }]; } if (block.type === 'reasoning') { return [ @@ -74,32 +83,33 @@ export const decodeStream = ( type: 'reasoning-end', id, metadata: metadata({ signature: block.signature, redactedData: block.redactedData }), - } as const, + }, ]; } - const paramsEnd = { type: 'tool-params-end', id: block.id } as const; - const input = yield* Effect.result( - Effect.try({ - try: () => Tool.unsafeSecureJsonParse(block.input === '' ? '{}' : block.input), - catch: (cause) => - AiError.make({ - module: 'AnthropicLanguageModel', - method, - reason: new AiError.ToolParameterValidationError({ - toolName: block.name, - description: `Tool input is not valid JSON: ${String(cause)}`, - }), - }), - }), - ); - if (Result.isFailure(input)) { - invalidInput ??= input.failure; - return [paramsEnd]; + held = block; + return [{ type: 'tool-params-end', id: block.id }]; + }; + + /** Emits the held tool call, which is complete once another block starts or the turn ends. */ + const release = Effect.fnUntraced(function* () { + const block = held; + held = undefined; + if (block === undefined) { + return []; } - return [ - paramsEnd, - yield* toolCall(method, options, { id: block.id, name: block.name, input: input.success }), - ]; + const input = yield* Effect.try({ + try: () => Tool.unsafeSecureJsonParse(block.input === '' ? '{}' : block.input), + catch: (cause) => + AiError.make({ + module: 'AnthropicLanguageModel', + method, + reason: new AiError.ToolParameterValidationError({ + toolName: block.name, + description: `Tool input is not valid JSON: ${String(cause)}`, + }), + }), + }); + return [yield* toolCall(method, options, { id: block.id, name: block.name, input })]; }); const handle = Effect.fnUntraced(function* (event: Sse.Event) { @@ -123,7 +133,9 @@ export const decodeStream = ( }); break; case 'content_block_start': { + parts.push(...(yield* release())); if (!isKnown(decoded.content_block, Wire.contentBlockTypes)) { + skipped.add(decoded.index); break; } const block = yield* decodeBlock(method, decoded.content_block); @@ -174,12 +186,15 @@ export const decodeStream = ( break; } case 'content_block_delta': { - const block = blocks.get(decoded.index); - if (block === undefined || !isKnown(decoded.delta, Wire.deltaTypes)) { + if (skipped.has(decoded.index) || !isKnown(decoded.delta, Wire.deltaTypes)) { break; } const delta = yield* decodeDelta(method, decoded.delta); + const block = blocks.get(decoded.index); const id = String(decoded.index); + if (block === undefined) { + return yield* invalidResponse(method, `Delta ${delta.type} for unopened block ${id}`); + } if (delta.type === 'text_delta' && block.type === 'text') { parts.push({ type: 'text-delta', id, delta: delta.text }); } else if (delta.type === 'thinking_delta' && block.type === 'reasoning') { @@ -200,22 +215,24 @@ export const decodeStream = ( break; } case 'content_block_stop': - parts.push(...(yield* end(decoded.index))); + parts.push(...end(decoded.index)); break; case 'message_delta': mergeUsage(decoded.usage); stopReason = decoded.delta.stop_reason ?? stopReason; break; - case 'message_stop': + case 'message_stop': { finished = true; for (const index of blocks.keys()) { - parts.push(...(yield* end(index))); + parts.push(...end(index)); } - if (invalidInput !== undefined && finishReason(stopReason) !== 'length') { - return yield* invalidInput; + const reason = finishReason(stopReason); + if (reason === 'length') { + held = undefined; } - parts.push({ type: 'finish', reason: finishReason(stopReason), usage: usage(counts) }); + parts.push(...(yield* release()), { type: 'finish', reason, usage: usage(counts) }); break; + } case 'error': return yield* providerError(method, decoded.error); } diff --git a/packages/foundry-effect-ai/src/internal/models/anthropic.ts b/packages/foundry-effect-ai/src/internal/models/anthropic.ts index 0134ae1..b8cd6df 100644 --- a/packages/foundry-effect-ai/src/internal/models/anthropic.ts +++ b/packages/foundry-effect-ai/src/internal/models/anthropic.ts @@ -1,7 +1,7 @@ /** * Anthropic models on Foundry, by catalog alias. `maxOutputTokens` is the default `max_tokens`; - * `nativeJson` marks models that reject forced tool use, so structured output is sent as - * `output_config.format` instead of a forced JSON tool. + * `rejectsForcedToolUse` marks models that reject forced tool use: structured output is sent as + * `output_config.format` instead of a forced JSON tool, and forced tool choices fail locally. */ export const models = { 'claude-haiku-4.5': { @@ -35,7 +35,7 @@ export const models = { 'claude-opus-5.5': { rid: 'ri.language-model-service..language-model.anthropic-claude-5-5-opus', maxOutputTokens: 128_000, - nativeJson: true, + rejectsForcedToolUse: true, }, 'claude-sonnet-4.5': { rid: 'ri.language-model-service..language-model.anthropic-claude-4-5-sonnet', @@ -52,6 +52,6 @@ export const models = { 'claude-sonnet-5.5': { rid: 'ri.language-model-service..language-model.anthropic-claude-5-5-sonnet', maxOutputTokens: 128_000, - nativeJson: true, + rejectsForcedToolUse: true, }, } as const;