From cc49544a263050a93413110f799094d508b527cc Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:36:20 -0600 Subject: [PATCH 1/4] feat(effect-ai): GoogleLanguageModel over generateContent with recorded fixtures --- .../src/GoogleLanguageModel.ts | 80 +++++- .../src/internal/google-gemini/request.ts | 249 ++++++++++++++++++ .../src/internal/google-gemini/response.ts | 208 +++++++++++++++ .../src/internal/google-gemini/schema.ts | 119 +++++++++ .../src/internal/google-gemini/stream.ts | 156 +++++++++++ .../src/internal/models/google.ts | 16 ++ .../test/cells/GoogleLanguageModel.ts | 22 ++ .../fixtures/GoogleLanguageModel/image.json | 32 +++ .../GoogleLanguageModel/json-tools.json | 177 +++++++++++++ .../fixtures/GoogleLanguageModel/json.json | 45 ++++ .../GoogleLanguageModel/reasoning.json | 32 +++ .../stream-error.synthetic.json | 26 ++ .../GoogleLanguageModel/stream-tools.json | 153 +++++++++++ .../stream-truncated.synthetic.json | 26 ++ .../fixtures/GoogleLanguageModel/stream.json | 26 ++ .../fixtures/GoogleLanguageModel/text.json | 26 ++ .../fixtures/GoogleLanguageModel/tools.json | 153 +++++++++++ 17 files changed, 1536 insertions(+), 10 deletions(-) create mode 100644 packages/foundry-effect-ai/src/internal/google-gemini/request.ts create mode 100644 packages/foundry-effect-ai/src/internal/google-gemini/response.ts create mode 100644 packages/foundry-effect-ai/src/internal/google-gemini/schema.ts create mode 100644 packages/foundry-effect-ai/src/internal/google-gemini/stream.ts create mode 100644 packages/foundry-effect-ai/src/internal/models/google.ts create mode 100644 packages/foundry-effect-ai/test/cells/GoogleLanguageModel.ts create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/image.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json-tools.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/reasoning.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-error.synthetic.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-tools.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-truncated.synthetic.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/text.json create mode 100644 packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/tools.json diff --git a/packages/foundry-effect-ai/src/GoogleLanguageModel.ts b/packages/foundry-effect-ai/src/GoogleLanguageModel.ts index 7d2478f..5b170ac 100644 --- a/packages/foundry-effect-ai/src/GoogleLanguageModel.ts +++ b/packages/foundry-effect-ai/src/GoogleLanguageModel.ts @@ -1,25 +1,85 @@ /** Google Gemini language models through Foundry's `/google/v1/models/{model}:generateContent` route. */ 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 { codecTransformer, makeRequest } from './internal/google-gemini/request.ts'; +import { decodeResponse } from './internal/google-gemini/response.ts'; +import * as Wire from './internal/google-gemini/schema.ts'; +import { decodeStream } from './internal/google-gemini/stream.ts'; +import { models } from './internal/models/google.ts'; export const providerName = 'foundry-google'; /** A catalog alias or a raw Foundry model RID. */ +export type ModelId = keyof typeof models | (string & Record); + +/** Request-wide settings, sent as the `generationConfig` fields of the same name. */ +export interface Config { + /** + * For example `{ includeThoughts: true, thinkingLevel: 'HIGH' }` to receive thought + * summaries. Foundry rejects lower-case levels. + */ + readonly thinkingConfig?: Wire.ThinkingConfig | undefined; +} + +const known: Readonly> = models; + +/** Known aliases resolve to their RIDs; a known RID or any other target passes through. */ +const resolve = (modelId: string): string => known[modelId]?.rid ?? modelId; + 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('GoogleLanguageModel.make'); +/** + * Every request replays the full prompt with each part's thought signature, which Gemini + * requires on replayed function calls. Structured output sends Effect's default JSON Schema as + * `responseJsonSchema`, or, beside function tools, as a synthetic `json` function that the + * model is forced to call; that call comes back as the answer text. + */ +export const make = Effect.fnUntraced(function* (options: { + readonly model: ModelId; + readonly config?: Config | undefined; +}) { + const client = yield* FoundryClient; + const path = `/google/v1/models/${resolve(options.model)}`; + const thinkingConfig = options.config?.thinkingConfig; + return yield* LanguageModel.make({ + codecTransformer, + generateText: Effect.fnUntraced(function* (providerOptions) { + const { request, jsonTool } = yield* makeRequest(providerOptions, thinkingConfig); + const body = yield* client.json({ + path: `${path}:generateContent`, + body: request, + schema: Wire.Response, + }); + return yield* decodeResponse(body, providerOptions, jsonTool); + }), + streamText: (providerOptions) => + makeRequest(providerOptions, thinkingConfig).pipe( + Effect.map(({ request, jsonTool }) => + decodeStream( + client.sse({ + path: `${path}:streamGenerateContent`, + urlParams: { alt: 'sse' }, + body: request, + }), + providerOptions, + jsonTool, + ), + ), + 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/google-gemini/request.ts b/packages/foundry-effect-ai/src/internal/google-gemini/request.ts new file mode 100644 index 0000000..d8a79b9 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/google-gemini/request.ts @@ -0,0 +1,249 @@ +// Adapted from @ai-sdk/google (Apache-2.0), 4.0.122. +/** + * Effect AI prompt, tools and response format → a `generateContent` request. Every turn replays + * the whole prompt, and each part goes back with the thought signature it arrived with: Gemini + * rejects a replayed function call that lost its signature. + */ +import * as AiError from 'effect/ai/AiError'; +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 Schema from 'effect/Schema'; +import type * as Wire from './schema.ts'; + +/** Metadata and prompt-option namespace for thought signatures. */ +export const metadataKey = 'foundry-google'; + +/** + * Gemini accepts Effect's default JSON Schema as `responseJsonSchema`, including + * `additionalProperties`, `anyOf`, bounds and `$defs`. It ignores `const`, which Effect never + * emits: literals become `enum`. + */ +export const codecTransformer = LanguageModel.defaultCodecTransformer; + +const fail = (reason: AiError.AiErrorReason) => + AiError.make({ module: 'GoogleLanguageModel', method: 'makeRequest', reason }); + +const signature = (options: Prompt.ProviderOptions): { thoughtSignature?: string } => { + const value = options[metadataKey]; + const raw = Predicate.isObject(value) ? value['thoughtSignature'] : undefined; + return typeof raw === 'string' ? { thoughtSignature: raw } : {}; +}; + +const base64DataUrl = /^data:[^,]*;base64,/u; + +const filePart = (part: Prompt.FilePart): Wire.Part => { + const { mediaType: mimeType, data } = part; + if (!(data instanceof URL)) { + return { + inlineData: { mimeType, data: typeof data === 'string' ? data : Base64.encode(data) }, + }; + } + return base64DataUrl.test(data.href) + ? { inlineData: { mimeType, data: data.href.replace(base64DataUrl, '') } } + : { fileData: { mimeType, fileUri: data.href } }; +}; + +/** A tool result as Gemini's object `response`: the text of its model output, or the result. */ +const toolResponse = Effect.fnUntraced(function* (part: Prompt.ToolResultPart) { + if (part.isFailure) { + return { name: part.name, error: part.result ?? null }; + } + if (part.modelOutput === undefined) { + return { name: part.name, content: part.result ?? null }; + } + const texts: Array = []; + for (const output of part.modelOutput) { + if (output.type !== 'text') { + return yield* fail( + new AiError.InvalidUserInputError({ + description: `Tool results cannot carry files (${output.mediaType}) on this route`, + }), + ); + } + texts.push(output.text); + } + return { name: part.name, content: texts.join('\n') }; +}); + +const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { + const system: Array<{ text: string }> = []; + const contents: Array = []; + for (const message of prompt.content) { + const parts: Array = []; + switch (message.role) { + case 'system': + if (contents.length > 0) { + return yield* fail( + new AiError.InvalidUserInputError({ + description: 'System messages are supported only at the start of the prompt', + }), + ); + } + system.push({ text: message.content }); + break; + case 'user': + for (const part of message.content) { + parts.push(part.type === 'text' ? { text: part.text } : filePart(part)); + } + contents.push({ role: 'user', parts }); + break; + case 'assistant': + for (const part of message.content) { + const signed = signature(part.options); + if ( + (part.type === 'text' || part.type === 'reasoning') && + (part.text !== '' || signed.thoughtSignature !== undefined) + ) { + parts.push({ + text: part.text, + ...(part.type === 'reasoning' ? { thought: true } : undefined), + ...signed, + }); + } else if (part.type === 'tool-call' && !part.providerExecuted) { + parts.push({ + functionCall: { id: part.id, name: part.name, args: part.params }, + ...signed, + }); + } + } + if (parts.length > 0) { + contents.push({ role: 'model', parts }); + } + break; + case 'tool': + for (const part of message.content) { + if (part.type === 'tool-result') { + parts.push({ + functionResponse: { + id: part.id, + name: part.name, + response: yield* toolResponse(part), + }, + }); + } + } + if (parts.length > 0) { + contents.push({ role: 'user', parts }); + } + break; + } + } + return { + contents, + ...(system.length === 0 ? undefined : { systemInstruction: { parts: system } }), + }; +}); + +const unsupportedSchema = (error: unknown) => + fail( + new AiError.UnsupportedSchemaError({ + description: error instanceof Error ? error.message : String(error), + }), + ); + +const responseSchema = (schema: Schema.Constraint) => + Effect.try({ + try: () => Tool.getJsonSchemaFromSchema(schema, { transformer: codecTransformer }), + catch: unsupportedSchema, + }); + +/** + * Function declarations and the calling mode. JSON output beside tools becomes a synthetic + * `json` function under forced calling: with `responseJsonSchema` and tools both set, Gemini + * 3.1 Flash-Lite calls the tool again after its result instead of answering. + */ +const encodeTools = Effect.fnUntraced(function* ( + options: LanguageModel.ProviderOptions, + jsonSchema: unknown, +) { + const choice = options.toolChoice; + const required = + choice === 'required' || + (typeof choice === 'object' && ('tool' in choice || choice.mode === 'required')); + if (options.tools.length === 0) { + 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 allowed = + typeof choice === 'object' && 'oneOf' in choice + ? options.tools.filter((tool) => choice.oneOf.includes(tool.name)) + : options.tools; + const functionDeclarations: Array = []; + for (const tool of allowed) { + if (Tool.isProviderDefined(tool)) { + return yield* fail( + new AiError.InvalidRequestError({ + description: `Provider-defined tool '${tool.name}' is not supported`, + }), + ); + } + const parametersJsonSchema = yield* Effect.try({ + try: () => Tool.getJsonSchema(tool, { transformer: codecTransformer }), + catch: unsupportedSchema, + }); + const description = Tool.getDescription(tool); + functionDeclarations.push({ + name: String(tool.name), + parametersJsonSchema, + ...(description === undefined ? undefined : { description }), + }); + } + let jsonTool: string | undefined; + if (jsonSchema !== undefined) { + jsonTool = 'json'; + for (let suffix = 1; options.tools.some((tool) => tool.name === jsonTool); suffix++) { + jsonTool = `json_${suffix}`; + } + functionDeclarations.push({ + name: jsonTool, + description: 'Respond with a JSON object.', + parametersJsonSchema: jsonSchema, + }); + } + let functionCallingConfig: Wire.FunctionCallingConfig; + if (typeof choice === 'object' && 'tool' in choice) { + functionCallingConfig = { mode: 'ANY', allowedFunctionNames: [String(choice.tool)] }; + } else if (jsonTool !== undefined) { + functionCallingConfig = + choice === 'none' ? { mode: 'ANY', allowedFunctionNames: [jsonTool] } : { mode: 'ANY' }; + } else if (choice === 'none') { + functionCallingConfig = { mode: 'NONE' }; + } else { + functionCallingConfig = { mode: required ? 'ANY' : 'AUTO' }; + } + return { tools: [{ functionDeclarations }], toolConfig: { functionCallingConfig }, jsonTool }; +}); + +/** The request, and the synthetic JSON function whose call is the answer, if one was added. */ +export const makeRequest = Effect.fnUntraced(function* ( + options: LanguageModel.ProviderOptions, + thinkingConfig: Wire.ThinkingConfig | undefined, +) { + const prompt = yield* encodePrompt(options.prompt); + const format = options.responseFormat; + const jsonSchema = format.type === 'json' ? yield* responseSchema(format.schema) : undefined; + const { jsonTool, ...tools } = yield* encodeTools(options, jsonSchema); + const generationConfig = { + ...(jsonSchema === undefined || jsonTool !== undefined + ? undefined + : { responseMimeType: 'application/json' as const, responseJsonSchema: jsonSchema }), + ...(thinkingConfig === undefined ? undefined : { thinkingConfig }), + }; + const request: Wire.Request = { + ...prompt, + ...tools, + ...(Object.keys(generationConfig).length === 0 ? undefined : { generationConfig }), + }; + return { request, jsonTool }; +}); diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/response.ts b/packages/foundry-effect-ai/src/internal/google-gemini/response.ts new file mode 100644 index 0000000..0093433 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/google-gemini/response.ts @@ -0,0 +1,208 @@ +// Adapted from @ai-sdk/google (Apache-2.0), 4.0.122. +/** A `generateContent` body → Effect AI response parts, plus helpers the stream shares. */ +import * as AiError from 'effect/ai/AiError'; +import * as IdGenerator from 'effect/ai/IdGenerator'; +import type * as LanguageModel from 'effect/ai/LanguageModel'; +import type * as Response from 'effect/ai/Response'; +import * as DateTime from 'effect/DateTime'; +import * as Effect from 'effect/Effect'; +import * as Option from 'effect/Option'; +import * as Schema from 'effect/Schema'; +import { metadataKey } from './request.ts'; +import type * as Wire from './schema.ts'; + +const module = 'GoogleLanguageModel'; + +export const invalidResponse = (method: string, description: string): AiError.AiError => + AiError.make({ module, method, reason: new AiError.InvalidResponseError({ description }) }); + +const encodeJson = Schema.encodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); + +/** JSON text for call arguments: a tool's streamed parameters, or the synthetic JSON answer. */ +export const jsonText = (method: string, value: unknown): Effect.Effect => + Effect.mapError(encodeJson(value), (error) => invalidResponse(method, error.message)); + +/** + * A part's text: its own, or the arguments of a call to the synthetic JSON function, which is + * the answer rather than a tool call. + */ +export const partText = ( + method: string, + part: typeof Wire.ResponsePart.Type, +): Effect.Effect => + part.functionCall === undefined + ? Effect.succeed(part.text) + : jsonText(method, part.functionCall.args ?? {}); + +/** + * An error reported inside a successful response or stream. `RESOURCE_EXHAUSTED` is throttling; + * anything else is a provider failure. Code and status stay in metadata. + */ +export const providerError = ( + method: string, + error: typeof Wire.ErrorObject.Type, +): AiError.AiError => { + const metadata = { [metadataKey]: { code: error.code ?? null, status: error.status ?? null } }; + const reason = + error.status === 'RESOURCE_EXHAUSTED' || error.code === 429 + ? new AiError.RateLimitError({ metadata }) + : new AiError.InternalProviderError({ + description: + [error.status ?? error.code, error.message].filter((text) => text != null).join(': ') || + 'Response failed', + metadata, + }); + return AiError.make({ module, method, reason }); +}; + +export const responseMetadata = ( + body: typeof Wire.Response.Type, +): Response.ResponseMetadataPartEncoded => ({ + type: 'response-metadata', + id: body.responseId, + modelId: body.modelVersion, + timestamp: Option.getOrUndefined( + Option.map(DateTime.make(body.createTime ?? ''), DateTime.formatIso), + ), +}); + +/** The opaque signature Gemini needs back on the part that carried it. */ +export const signed = ( + thoughtSignature: string | undefined, +): { metadata?: Record } => + thoughtSignature === undefined ? {} : { metadata: { [metadataKey]: { thoughtSignature } } }; + +/** A prompt block reason other than the unspecified placeholder. */ +export const blocked = (body: typeof Wire.Response.Type): boolean => { + const reason = body.promptFeedback?.blockReason; + return ( + reason !== undefined && + reason !== '' && + reason !== 'BLOCK_REASON_UNSPECIFIED' && + reason !== 'BLOCKED_REASON_UNSPECIFIED' + ); +}; + +const filtered = new Set([ + 'SAFETY', + 'BLOCKLIST', + 'PROHIBITED_CONTENT', + 'SPII', + 'RECITATION', + 'IMAGE_SAFETY', +]); + +/** A blocked prompt is filtered; `CONTINUATION` is Gemini 4's per-request output cap. */ +export const finishReason = (outcome: { + readonly reason: string | undefined; + readonly blocked: boolean; + readonly hasToolCalls: boolean; +}): Response.FinishReason => { + const { reason } = outcome; + if (reason === undefined) { + return outcome.blocked ? 'content-filter' : 'other'; + } + if (reason === 'STOP') { + return outcome.hasToolCalls ? 'tool-calls' : 'stop'; + } + if (reason === 'MAX_TOKENS' || reason === 'CONTINUATION') { + return 'length'; + } + if (filtered.has(reason)) { + return 'content-filter'; + } + return reason === 'MALFORMED_FUNCTION_CALL' ? 'error' : 'other'; +}; + +/** A snapshot with counts; Gemini sends `{}` on chunks before the last. */ +export const hasUsage = (wire: typeof Wire.UsageMetadata.Type | undefined): boolean => + wire?.promptTokenCount !== undefined || wire?.totalTokenCount !== undefined; + +/** Unreported usage stays undefined; inside a snapshot, proto3 omits zero counts. */ +export const usage = ( + wire: typeof Wire.UsageMetadata.Type | undefined, +): typeof Response.Usage.Encoded => { + if (wire === undefined || !hasUsage(wire)) { + return { inputTokens: {}, outputTokens: {} }; + } + const input = (wire.promptTokenCount ?? 0) + (wire.toolUsePromptTokenCount ?? 0); + const cacheRead = wire.cachedContentTokenCount ?? 0; + const text = wire.candidatesTokenCount ?? 0; + const reasoning = wire.thoughtsTokenCount ?? 0; + return { + inputTokens: { total: input, uncached: input - cacheRead, cacheRead }, + outputTokens: { total: text + reasoning, text, reasoning }, + }; +}; + +/** + * A function call with its wire ID, or a generated one if Gemini sent none. Arguments arrive + * as an object and are passed through; Toolkit validates them. + */ +export const toolCall = Effect.fnUntraced(function* ( + method: string, + options: LanguageModel.ProviderOptions, + functionCall: typeof Wire.FunctionCall.Type, + thoughtSignature: string | undefined, +) { + const { name } = functionCall; + if ( + options.toolCallValidation !== 'deferred' && + !options.tools.some((tool) => tool.name === name) + ) { + return yield* AiError.make({ + module, + method, + reason: new AiError.ToolNotFoundError({ + toolName: name, + availableTools: options.tools.map((tool) => String(tool.name)), + }), + }); + } + const ids = yield* IdGenerator.IdGenerator; + const call: Response.ToolCallPartEncoded = { + type: 'tool-call', + id: functionCall.id ?? (yield* ids.generateId()), + name, + params: functionCall.args ?? {}, + ...signed(thoughtSignature), + }; + return call; +}); + +export const decodeResponse = Effect.fnUntraced(function* ( + body: typeof Wire.Response.Type, + options: LanguageModel.ProviderOptions, + jsonTool: string | undefined, +) { + if (body.error !== undefined) { + return yield* providerError('generateText', body.error); + } + const parts: Array = [responseMetadata(body)]; + const candidate = body.candidates?.[0]; + let hasToolCalls = false; + for (const part of candidate?.content?.parts ?? []) { + if (part.functionCall !== undefined && part.functionCall.name !== jsonTool) { + hasToolCalls = true; + parts.push( + yield* toolCall('generateText', options, part.functionCall, part.thoughtSignature), + ); + continue; + } + const text = yield* partText('generateText', part); + // A signature-only part stays a part of its own, so replay puts it back in place. + if (text !== undefined && (text !== '' || part.thoughtSignature !== undefined)) { + parts.push({ + type: part.thought === true ? 'reasoning' : 'text', + text, + ...signed(part.thoughtSignature), + }); + } + } + parts.push({ + type: 'finish', + reason: finishReason({ reason: candidate?.finishReason, blocked: blocked(body), hasToolCalls }), + usage: usage(body.usageMetadata), + }); + return parts; +}); diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts b/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts new file mode 100644 index 0000000..8a17fc7 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts @@ -0,0 +1,119 @@ +// Adapted from @ai-sdk/google (Apache-2.0), 4.0.122. +/** + * Wire shapes for `/google/v1/models/{model}:generateContent` and `:streamGenerateContent`. + * Requests are plain interfaces; the response schema decodes only the fields the adapter reads, + * and each stream event is a whole response chunk of the same shape. + */ +import * as Schema from 'effect/Schema'; + +/** A part the adapter sends. A signature goes back on the part that carried it. */ +export type Part = + | { + readonly text: string; + readonly thought?: true; + readonly thoughtSignature?: string; + } + | { readonly inlineData: { readonly mimeType: string; readonly data: string } } + | { readonly fileData: { readonly mimeType: string; readonly fileUri: string } } + | { + readonly functionCall: { readonly id: string; readonly name: string; readonly args: unknown }; + readonly thoughtSignature?: string; + } + | { + readonly functionResponse: { + readonly id: string; + readonly name: string; + readonly response: + | { readonly name: string; readonly content: unknown } + | { readonly name: string; readonly error: unknown }; + }; + }; + +export interface Content { + readonly role: 'user' | 'model'; + readonly parts: ReadonlyArray; +} + +/** Request-wide thinking settings, sent as given. Foundry accepts upper-case levels only. */ +export interface ThinkingConfig { + readonly includeThoughts?: boolean; + readonly thinkingBudget?: number; + readonly thinkingLevel?: 'MINIMAL' | 'LOW' | 'MEDIUM' | 'HIGH'; +} + +export interface FunctionDeclaration { + readonly name: string; + readonly description?: string; + readonly parametersJsonSchema: unknown; +} + +export interface FunctionCallingConfig { + readonly mode: 'AUTO' | 'NONE' | 'ANY'; + readonly allowedFunctionNames?: ReadonlyArray; +} + +export interface Request { + readonly contents: ReadonlyArray; + readonly systemInstruction?: { readonly parts: ReadonlyArray<{ readonly text: string }> }; + readonly generationConfig?: { + readonly responseMimeType?: 'application/json'; + readonly responseJsonSchema?: unknown; + readonly thinkingConfig?: ThinkingConfig; + }; + readonly tools?: ReadonlyArray<{ + readonly functionDeclarations: ReadonlyArray; + }>; + readonly toolConfig?: { readonly functionCallingConfig: FunctionCallingConfig }; +} + +const Count = Schema.optional(Schema.Finite); + +/** Proto3 JSON omits zero counts, so a present snapshot reads missing fields as zero. */ +export const UsageMetadata = Schema.Struct({ + promptTokenCount: Count, + toolUsePromptTokenCount: Count, + cachedContentTokenCount: Count, + candidatesTokenCount: Count, + thoughtsTokenCount: Count, + totalTokenCount: Count, +}); + +/** `{ code, status, message }`, read leniently: every field may be absent. */ +export const ErrorObject = Schema.Struct({ + code: Schema.optional(Schema.NullOr(Schema.Finite)), + status: Schema.optional(Schema.NullOr(Schema.String)), + message: Schema.optional(Schema.NullOr(Schema.String)), +}); + +export const FunctionCall = Schema.Struct({ + id: Schema.optional(Schema.String), + name: Schema.String, + args: Schema.optional(Schema.Unknown), +}); + +export const ResponsePart = Schema.Struct({ + text: Schema.optional(Schema.String), + thought: Schema.optional(Schema.Boolean), + thoughtSignature: Schema.optional(Schema.String), + functionCall: Schema.optional(FunctionCall), +}); + +/** A response, or one stream chunk. A 2xx body or event may instead carry `error`. */ +export const Response = Schema.Struct({ + responseId: Schema.optional(Schema.String), + modelVersion: Schema.optional(Schema.String), + createTime: Schema.optional(Schema.String), + candidates: Schema.optional( + Schema.Array( + Schema.Struct({ + content: Schema.optional( + Schema.Struct({ parts: Schema.optional(Schema.Array(ResponsePart)) }), + ), + finishReason: Schema.optional(Schema.String), + }), + ), + ), + promptFeedback: Schema.optional(Schema.Struct({ blockReason: Schema.optional(Schema.String) })), + usageMetadata: Schema.optional(UsageMetadata), + error: Schema.optional(ErrorObject), +}); diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts b/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts new file mode 100644 index 0000000..b2ddeb1 --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts @@ -0,0 +1,156 @@ +// Adapted from @ai-sdk/google (Apache-2.0), 4.0.122. +/** + * `streamGenerateContent?alt=sse` events → Effect AI stream parts. Each event is a response + * chunk. Text and thoughts open a block on their first delta and close on a switch, a function + * call or the end. Gemini sends no terminal event: the finish comes at end of stream, and only + * after a chunk reported a finish reason or a prompt block; otherwise the stream was cut off and + * fails with `InvalidResponseError`. An in-band error fails the stream. + */ +import type * as AiError from 'effect/ai/AiError'; +import type * as IdGenerator from 'effect/ai/IdGenerator'; +import type * as LanguageModel from 'effect/ai/LanguageModel'; +import type * as Response from 'effect/ai/Response'; +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 { + blocked, + finishReason, + hasUsage, + invalidResponse, + jsonText, + partText, + providerError, + responseMetadata, + signed, + toolCall, + usage, +} from './response.ts'; +import * as Wire from './schema.ts'; + +const method = 'streamText'; +const decodeChunk = Schema.decodeUnknownEffect(Schema.fromJsonString(Wire.Response)); + +/** An open text or reasoning block, and the signature that closed it. */ +interface Block { + readonly kind: 'text' | 'reasoning'; + readonly id: string; + signature?: string; +} + +export const decodeStream = ( + events: Stream.Stream, + options: LanguageModel.ProviderOptions, + jsonTool: string | undefined, +): Stream.Stream => { + let started = false; + let blocks = 0; + let open: Block | undefined; + let reason: string | undefined; + let isBlocked = false; + let hasToolCalls = false; + let lastUsage: typeof Wire.UsageMetadata.Type | undefined; + + const end = (): Array => { + if (open === undefined) { + return []; + } + const { kind, id, signature } = open; + open = undefined; + return [ + kind === 'text' + ? { type: 'text-end', id, ...signed(signature) } + : { type: 'reasoning-end', id, ...signed(signature) }, + ]; + }; + + /** The open block of this kind, opening one (and closing any other) if needed. */ + const ensure = (kind: Block['kind'], parts: Array): Block => { + if (open?.kind === kind) { + return open; + } + parts.push(...end()); + const block: Block = { kind, id: String(blocks++) }; + open = block; + parts.push( + kind === 'text' + ? { type: 'text-start', id: block.id } + : { type: 'reasoning-start', id: block.id }, + ); + return block; + }; + + const handle = Effect.fnUntraced(function* (event: Sse.Event) { + const chunk = yield* Effect.mapError(decodeChunk(event.data), (error) => + invalidResponse(method, `Malformed event data: ${error.message}`), + ); + if (chunk.error !== undefined) { + return yield* providerError(method, chunk.error); + } + const parts: Array = []; + if (!started) { + started = true; + parts.push(responseMetadata(chunk)); + } + const candidate = chunk.candidates?.[0]; + for (const part of candidate?.content?.parts ?? []) { + if (part.functionCall !== undefined && part.functionCall.name !== jsonTool) { + // Gemini sends a call's arguments whole, in one part. + parts.push(...end()); + hasToolCalls = true; + const call = yield* toolCall(method, options, part.functionCall, part.thoughtSignature); + const delta = yield* jsonText(method, call.params); + parts.push( + { type: 'tool-params-start', id: call.id, name: call.name }, + { type: 'tool-params-delta', id: call.id, delta }, + { type: 'tool-params-end', id: call.id }, + call, + ); + continue; + } + const text = yield* partText(method, part); + if (text === undefined || (text === '' && part.thoughtSignature === undefined)) { + continue; + } + const block = ensure(part.thought === true ? 'reasoning' : 'text', parts); + if (text !== '') { + parts.push( + block.kind === 'text' + ? { type: 'text-delta', id: block.id, delta: text } + : { type: 'reasoning-delta', id: block.id, delta: text }, + ); + } + // A signature ends its wire part, so it closes the block and replays in place. + if (part.thoughtSignature !== undefined) { + block.signature = part.thoughtSignature; + parts.push(...end()); + } + } + reason = candidate?.finishReason ?? reason; + isBlocked ||= blocked(chunk); + if (hasUsage(chunk.usageMetadata)) { + lastUsage = chunk.usageMetadata; + } + return parts; + }); + + return events.pipe( + Stream.mapEffect(handle), + Stream.flattenIterable, + Stream.concat( + Stream.suspend(() => + reason === undefined && !isBlocked + ? Stream.fail(invalidResponse(method, 'Stream ended before a finish reason')) + : Stream.fromIterable([ + ...end(), + { + type: 'finish', + reason: finishReason({ reason, blocked: isBlocked, hasToolCalls }), + usage: usage(lastUsage), + }, + ]), + ), + ), + ); +}; diff --git a/packages/foundry-effect-ai/src/internal/models/google.ts b/packages/foundry-effect-ai/src/internal/models/google.ts new file mode 100644 index 0000000..d891ebf --- /dev/null +++ b/packages/foundry-effect-ai/src/internal/models/google.ts @@ -0,0 +1,16 @@ +/** Gemini models on Foundry, by catalog alias. The adapter reads no flags yet. */ +export const models = { + 'gemini-4-argon': { rid: 'ri.language-model-service..language-model.gemini-4-argon' }, + 'gemini-4-carbon': { rid: 'ri.language-model-service..language-model.gemini-4-carbon' }, + 'gemini-4-hydrogen': { rid: 'ri.language-model-service..language-model.gemini-4-hydrogen' }, + 'gemini-3.8-flash': { rid: 'ri.language-model-service..language-model.gemini-3-8-flash' }, + 'gemini-3-flash': { rid: 'ri.language-model-service..language-model.gemini-3-flash' }, + 'gemini-3.1-pro': { rid: 'ri.language-model-service..language-model.gemini-3-1-pro' }, + 'gemini-3.1-flash-lite': { + rid: 'ri.language-model-service..language-model.gemini-3-1-flash-lite', + }, + 'gemini-3.5-flash': { rid: 'ri.language-model-service..language-model.gemini-3-5-flash' }, + 'gemini-3.5-flash-lite': { + rid: 'ri.language-model-service..language-model.gemini-3-5-flash-lite', + }, +} as const; diff --git a/packages/foundry-effect-ai/test/cells/GoogleLanguageModel.ts b/packages/foundry-effect-ai/test/cells/GoogleLanguageModel.ts new file mode 100644 index 0000000..e55dc88 --- /dev/null +++ b/packages/foundry-effect-ai/test/cells/GoogleLanguageModel.ts @@ -0,0 +1,22 @@ +import { expect } from '@effect/vitest'; +import type * as Response from 'effect/ai/Response'; +import * as GoogleLanguageModel from '../../src/GoogleLanguageModel.ts'; +import { languageCells } from '../capabilities.ts'; + +const namespace = 'foundry-google'; + +/** Gemini rejects a replayed function call without the thought signature it arrived with. */ +function expectReplay(parts: ReadonlyArray): void { + const call = parts.find((part) => part.type === 'tool-call'); + expect(call?.metadata[namespace]).toMatchObject({ thoughtSignature: expect.any(String) }); +} + +export const cells = languageCells({ + model: ({ reasoning }) => + GoogleLanguageModel.model( + 'gemini-3.1-flash-lite', + // Thought summaries need a level: flash-lite's default thinks too little to report any. + reasoning ? { thinkingConfig: { includeThoughts: true, thinkingLevel: 'HIGH' } } : undefined, + ), + replay: expectReplay, +}); diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/image.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/image.json new file mode 100644 index 0000000..9738afe --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/image.json @@ -0,0 +1,32 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a1924QIUJ6PlQ7Nwpx0O32aCWSM6fAyLtb0txxgcVx8YT9gdGYeSBW4YAfcOwxWYm4mKf8z4eJiEnruSrDA/y274tngyWV6kwLs4mzPsMdF5Le11SDiNtq1QYP3M/4ZYzX00WVBFRxhdLQfX5aw7z2B6gR93euRTnTpZKWAAWod7\",\"text\":\"Red\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":1100,\"candidatesTokenCount\":1,\"totalTokenCount\":1101,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":11},{\"modality\":\"IMAGE\",\"tokenCount\":1089}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":1}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:47.808325Z\",\"responseId\":\"redacted-id-1\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "What color fills this image? Answer with one word." + }, + { + "inlineData": { + "mimeType": "image/png", + "data": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAAxElEQVR42u3RMQ0AAAjAsPk3DTLgaDIFa1M6zAIAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABAADAAgAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAACAEAAAAjAixYgaMOy89oM6gAAAABJRU5ErkJggg==" + } + } + ] + } + ] + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json-tools.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json-tools.json new file mode 100644 index 0000000..7c99f0b --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json-tools.json @@ -0,0 +1,177 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a19mOfILCXwLocxyEAWdyF78xw7G0aN7KrsYuDP3P20byRFTfSdVCaeA8K0OjERuXjXiBT9LovQYD6mUvT/2BF/ZhiYvYSAIjVsLySSZ4dODDWjWzEfVKrhFyzTfZJjSOQe7FA1RcGNOz3LzzTmoJUvsFpupD3rLF+qFFPZ4+MCL\",\"functionCall\":{\"id\":\"redacted-id-1\",\"name\":\"GetWeather\",\"args\":{\"city\":\"Paris\"}}}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":103,\"candidatesTokenCount\":15,\"totalTokenCount\":118,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":103}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":15}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:46.407050Z\",\"responseId\":\"redacted-id-2\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + }, + { + "name": "json", + "description": "Respond with a JSON object.", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "temperatureC": { + "type": "number" + } + }, + "required": [ + "city", + "temperatureC" + ], + "additionalProperties": true + } + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "ANY" + } + } + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a1+MX1vl4Ok3UFOZo8e9SAKUGIyMTU0HB0JJIJAH21GnSoQajclbLIft7QUy9rbQaIl80geca78Ra24/ggn4TlM+WSyH6GJpSc0VRhPxKzA/bO/0PQZIiBD+kqQuJPb4E3nSgpdvuuWs+UH9orM1hBZkiI7Vt0Q66UBPuR4X9Pt7\",\"functionCall\":{\"id\":\"redacted-id-3\",\"name\":\"json\",\"args\":{\"city\":\"Paris\",\"temperatureC\":21}}}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":127,\"candidatesTokenCount\":20,\"totalTokenCount\":147,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":127}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":20}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:47.148893Z\",\"responseId\":\"redacted-id-4\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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": "model", + "parts": [ + { + "functionCall": { + "id": "redacted-id-1", + "name": "GetWeather", + "args": { + "city": "Paris" + } + }, + "thoughtSignature": "AY89a19mOfILCXwLocxyEAWdyF78xw7G0aN7KrsYuDP3P20byRFTfSdVCaeA8K0OjERuXjXiBT9LovQYD6mUvT/2BF/ZhiYvYSAIjVsLySSZ4dODDWjWzEfVKrhFyzTfZJjSOQe7FA1RcGNOz3LzzTmoJUvsFpupD3rLF+qFFPZ4+MCL" + } + ] + }, + { + "role": "user", + "parts": [ + { + "functionResponse": { + "id": "redacted-id-1", + "name": "GetWeather", + "response": { + "name": "GetWeather", + "content": { + "temperatureC": 21, + "conditions": "sunny" + } + } + } + } + ] + } + ], + "tools": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + }, + { + "name": "json", + "description": "Respond with a JSON object.", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "temperatureC": { + "type": "number" + } + }, + "required": [ + "city", + "temperatureC" + ], + "additionalProperties": true + } + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "ANY" + } + } + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json.json new file mode 100644 index 0000000..76dadb1 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/json.json @@ -0,0 +1,45 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a1/qUzb+DhKanUXSiu78Lq9zskE9YeFSMzJ8RO3xiEtuEfkMyvB/fwjpTy5Zyu9vCZiW4mGICZK6Il8HkJmwtdalhhGLrecZmxbInflMCuK6+BSeleZ+Coe2Ukbv0WrqH0kuW0L4v302W1Ne8ArELkbhuB21gFL2/cjdkOM33sw/\",\"text\":\"{\\n \\\"city\\\": \\\"Paris\\\",\\n \\\"country\\\": \\\"France\\\"\\n}\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":74,\"candidatesTokenCount\":19,\"totalTokenCount\":93,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":74}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":19}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:38.443110Z\",\"responseId\":\"redacted-id-1\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "Name the capital of France and its country." + } + ] + } + ], + "generationConfig": { + "responseMimeType": "application/json", + "responseJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "city", + "country" + ], + "additionalProperties": true + } + } + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/reasoning.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/reasoning.json new file mode 100644 index 0000000..97246f6 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/reasoning.json @@ -0,0 +1,32 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thought\":true,\"text\":\"**Considering Bat and Ball Costs**\\n\\nI'm refining the calculation for the bat and ball pricing. My focus is now on the incremental cost of the bat relative to the ball, ensuring the total sum aligns with the given 110 cents.\\n\\n\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thought\":true,\"text\":\"**Solving for Ball Cost**\\n\\nI've just finalized the algebraic solution. By setting up and solving a system of equations, I've determined the cost of the ball, which then allows me to find the bat's cost.\\n\\n\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Let $x$ be the cost of the ball.\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"\\nThen the cost of the bat is $x + 100$.\\n\\nTogether, their cost is:\\n$x + (x + \"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"100) = 110$\\n\\nCombine the $x$ terms:\\n$2x + 100 = \"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"110$\\n\\nSubtract 100 from both sides:\\n$2x = 10$\\n\\n\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Divide by 2:\\n$x = 5$\\n\\n5\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a184VMh9X6iFNKEyd872IaUns+WZA616JLXObK4eyo/aQ+X9WvA6kBa3NCn+xPuYgLiPhR4yiGM9XsII4CLL9Z9RFkz8f6DD0EumJZ1uSCfJ/zvMTd7zMpTHH/18AMSUXUhKaeZ/i3M8OmtSnlE5YNFs6a4gQz7QXbdfmmMCD+/b4vo7KmekwD6vmSr1yr9clfmjOXoJ3kSAVtRrw5Sa1lqwibGrBEak13sSt3BzWEtVQNDPx0CHdZJzbe1DY40rvne0bLwsG+xzDcvIw9fLzdg8VWMU/c3kYxwDrJ8InivHornfZfvf7Ar+m/FOCAOeLdUbIXKWXRpFUB+RUJJHaVbf5RlwrOj9FISeTsMa94JBh2IWsGBBeSBqx4EnqT0O5hsMa0BxjBv7xBlCeBZhFtVyY8FXKg8wtPTOX4Iu/3R3rpvM9f5bCan0sKSRoW8VNJ1z6wY75U+SanbMAckZMR5lvmtYggPwI/7j0PgFLhq4ETudHuNgTDre8BbfhqKiNG/s2tMVQLw5EzWubpeRG9K2xRFUhamgUZjZO++bDtaJ/KE/FUzOgVsvqvdu3kKQv1x9I7tObY+3ovauncuj5/u2cH/GNy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YEqk4eGoQOKyaBvrZ1Dvh78UP5lkrBwsRHKPF3xgbI1X3ipNOhZZOAi15GvAPBr13ViAIlCY+BAtpJb/Xt5/YNHhaeizj6/zoRt4TGqVr21GGLBLMlUzy2dkT7g5BJPFj2oOQRtkrqVbSDHEZm/eCfZZ6R64KjCn8PiqMowpvbRSWyW0nYgaDpizp6QWjNXW4fwiBmJQbGRKWEdui6U+Re5kMotb7mjPZNUIGMvFxJ6rV9MM9r2sEP4pqeZJuhGQgTVMZvRuOe6z8XldUuWUwT+7Q+TFo9Du2/rY4R/OYP3mtzqf7V3G1LspLBaGpbq/NWv2YVzfgvwJxltT7UkHcEiuUY6GYnAHuHjrH498UMweCmlxfNfGvyh4U17SzN1QLpSD+inWWl2gFy1BHFKLleMDtJYrDGMzGEqgFD2qoue3Tuvo19uMFJgKAFc1jcQ7F0n7+nL3hlJjULMJigEtcwxver1XWkC3CUxFBhoSlnK0UM29CJjGSBsQkh1gWZKGu6odzw8waeazAtQ5sxLcj/ZuCkvk1lvbRMZAQxBiEVPZ8xjsSI9vUWicOXUsd2tkS7KXWfVa9kkE1oZJNSEvzekHH/nPCktgrpxuDfvjUACjfBh1QYnhOGdPx7ymEZ73YiTBj5OyE6eU/pw0MqyDVNBikZSUfqT5z9W/X3JJDSOpuA/1gfX9PdfRfrUJqvgbW3mhgeb22iHV4fglnceVQuKZp5RcYFmqYyCH/qDSXENcivrqhD7Aq8VwJ4I2m9IorrVJLpMR6fXXilzv00nQ+wGOyo3itGioQlmwf5TPEp0h9dDbQZ2hJL7BDseA8Iq+\",\"text\":\"\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":47,\"candidatesTokenCount\":109,\"thoughtsTokenCount\":761,\"totalTokenCount\":917,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":47}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":109}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:48.980404Z\",\"responseId\":\"redacted-id-1\"}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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." + } + ] + } + ], + "generationConfig": { + "thinkingConfig": { + "includeThoughts": true, + "thinkingLevel": "HIGH" + } + } + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-error.synthetic.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-error.synthetic.json new file mode 100644 index 0000000..4fac9f2 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-error.synthetic.json @@ -0,0 +1,26 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Hello\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:37.446350Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"error\":{\"code\":500,\"message\":\"Internal error encountered.\",\"status\":\"INTERNAL\"}}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "Reply with exactly one word: hello" + } + ] + } + ] + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-tools.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-tools.json new file mode 100644 index 0000000..4b621fd --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-tools.json @@ -0,0 +1,153 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thought\":true,\"text\":\"**Recalling Olympic Host Cities**\\n\\nI've successfully identified Paris as the European capital that hosted the Summer Olympics in both 1900 and 1924. The next step involves calling the `GetWeather` function for Paris to retrieve the necessary meteorological data for my response.\\n\\n\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:43.896225Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a185E8mpfSeE0LHHeGypDiLHmHpgNl3hXbmDM8R3WdC1YzKK5c+nn8j3Nkk8uaxLlYRcqyLIctFpb+lbOW6QN/nv19mU3wDLKEsGjWlZFH7iNesgJQFsVfZi9rdP2zbdz8xFCqO8B0/OQ9aLzfK0YfXHlj3T2X4WytcwamRQ5ut4zPhHq2fxQfbJ1/Mc8+QLX0KnDiC7rbMXdik2FBjbPNuhKqyXn4TA/dTVEKp6m7yzra1Xb+fQjzIksK2x41LtTJqUK52IkgCrAQHJEgC2J6oZDyZDLTvNm1u2LMxLuo9gs9z1o3OSFo1FSIZWqc0w/XNFpNLwTg+kgqSTufQZAZ4Rb3Uzc4SnK0FwXM+1oOz1HrSFWgxv/DXPe3KadD3tOtRIgWQ37dHxlINLcEwyoKAvDpznC3B0+qzzWUGil9YKCMyWKs7jM8Iez6C5UCrpoulfnml0rkqbnCmtrol+Gzjb9eer9R69G81bM+E5iPmZbvJNigamcDVW0KYttI5V3+oRwnRIwXybrLGa5cMwaVMdNcWsbJPFRqV+xNj6XdB6L/X/lPG18reNpgQCDt5Zq/vi2r1Ch+xvOH+2GgS3FJjLy1l6gHslbKeHqLYCTL2XPOb9XkqJLu7BQzsRozND6EzmbW4IxgKnhc0BTKKQbCEF6C+zEXBAGzgMu9emAp5kWcFuGQkzx2ccZUcNf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Y/wy+5SQE+w5LKPw1KfUUSnvMvyM5AKxOHE7jK8QtEbdWF29z+8d/Si647IzVFbWumQX2WHx0Q4kOIWtXHHIBNCNruqiU68H9n+08tlrB8kRJXxrC5QWBBUQWHurMZHvBZa6GpkaF1otx1Msuvukip9+apw+FQ9hXz1s/9kH3xKjl45hTUmD+NnkYhkHdFkUbaav3JLci1kZNA8PwGJo7YTsK0GZ4kSbz5NB+T2Te5EwuLnDc14PUu4HPALSvrDLcKJyd0CmNzTLXke0KZRaH+eZUcOM7a2Vn2spMiH90sd1KdSZwoMiyFd3g/hiXvZHzWBf66MNFH2caz4jCtmOdZFhsl3nrvuzFH7Tc7KvdzZWleV3siR/pNt08+qjDhp4seIU9Kn89DRlbDGFnV/rZBCUBqoFVOJsrBok6gHuzHj86WT7SWRYuk5kTV7vHyEXyULo/TyDOyLa/O7La1ewC69gZsOco7r1YmTGL+BWc1YxiC7UWVSrZlF2zIcSiwvBM+TMXj3+z0Ekd65CfRFybwOA+EQ+szJ/jWjPqToy0GSwRjwpC4gP389EKxN4ePiN3BX4Gcg\",\"functionCall\":{\"id\":\"redacted-id-2\",\"name\":\"GetWeather\",\"args\":{\"city\":\"Paris\"}}}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:43.896225Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":67,\"candidatesTokenCount\":15,\"thoughtsTokenCount\":372,\"totalTokenCount\":454,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":67}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":15}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:43.896225Z\",\"responseId\":\"redacted-id-1\"}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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." + } + ] + } + ], + "tools": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "AUTO" + } + }, + "generationConfig": { + "thinkingConfig": { + "includeThoughts": true, + "thinkingLevel": "HIGH" + } + } + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Paris, which hosted the Summer Olympics in\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:45.437408Z\",\"responseId\":\"redacted-id-3\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\" 1900 and 1924, is currently experiencing sunny conditions with a temperature of 21°\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:45.437408Z\",\"responseId\":\"redacted-id-3\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"C.\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:45.437408Z\",\"responseId\":\"redacted-id-3\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a194c+YfmGVe5F9Bf4kMZHKBux36++ilN6wOm07LSSkXpLHqZVNoESbocNeVNBDIolyLs6D2wPYyUcGfwzELg9bS2Mo2B19sUiP3u7FrregXfFZE2TOtBaQgVaShRYGfHED0DpCXW5qFeI8O4AWxa9o0XRod8LirH8ZIgXw1u5tUELqQuOUFPcWJDQS2UVvLXziHOar6ov3YFp6dGvC2XicZx24z6EBB1fzZGvmUD9Y5uQ2BHbUm1mwWIrv5vLNlmFnu2Z0WCCPMPOOr1CGlRp7wbE9TRWsBmqGcclvSl7eb9SnGqznnsc7Iv1930DcGFDp5waJatn2EUt/1tmb/XAsx3gbtcg54cd12YqBGwIuIiep0QjxUXogKFyJnttsNwBbA3pgARFEoaJzLXVaVpd9nXUfjWIjGrvPJ5j1ElI8+M5+IKN4orphERm8mm3eU/vFRp1biiWISNyfOmm97V1Bhfdzb4KkHYPD1TfsYZIvfmtzccO6uwesCZzdYKS3TvgV/cCccBJdZjfx/y4f8HaEVxxIGxQz4T+9biev/AR6Lrhb+60Vq3w92QQt3N/Lsp47osmf8R8yTTJm4WwqYucEXPxAZKeHbhv+jmtFAemAXQn/J7Z25l2T5Hj5ohB95hQ==\",\"text\":\"\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":463,\"candidatesTokenCount\":35,\"thoughtsTokenCount\":93,\"totalTokenCount\":591,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":463}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":35}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:45.437408Z\",\"responseId\":\"redacted-id-3\"}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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": "model", + "parts": [ + { + "text": "**Recalling Olympic Host Cities**\n\nI've successfully identified Paris as the European capital that hosted the Summer Olympics in both 1900 and 1924. The next step involves calling the `GetWeather` function for Paris to retrieve the necessary meteorological data for my response.\n\n", + "thought": true + }, + { + "functionCall": { + "id": "redacted-id-2", + "name": "GetWeather", + "args": { + "city": "Paris" + } + }, + "thoughtSignature": "AY89a185E8mpfSeE0LHHeGypDiLHmHpgNl3hXbmDM8R3WdC1YzKK5c+nn8j3Nkk8uaxLlYRcqyLIctFpb+lbOW6QN/nv19mU3wDLKEsGjWlZFH7iNesgJQFsVfZi9rdP2zbdz8xFCqO8B0/OQ9aLzfK0YfXHlj3T2X4WytcwamRQ5ut4zPhHq2fxQfbJ1/Mc8+QLX0KnDiC7rbMXdik2FBjbPNuhKqyXn4TA/dTVEKp6m7yzra1Xb+fQjzIksK2x41LtTJqUK52IkgCrAQHJEgC2J6oZDyZDLTvNm1u2LMxLuo9gs9z1o3OSFo1FSIZWqc0w/XNFpNLwTg+kgqSTufQZAZ4Rb3Uzc4SnK0FwXM+1oOz1HrSFWgxv/DXPe3KadD3tOtRIgWQ37dHxlINLcEwyoKAvDpznC3B0+qzzWUGil9YKCMyWKs7jM8Iez6C5UCrpoulfnml0rkqbnCmtrol+Gzjb9eer9R69G81bM+E5iPmZbvJNigamcDVW0KYttI5V3+oRwnRIwXybrLGa5cMwaVMdNcWsbJPFRqV+xNj6XdB6L/X/lPG18reNpgQCDt5Zq/vi2r1Ch+xvOH+2GgS3FJjLy1l6gHslbKeHqLYCTL2XPOb9XkqJLu7BQzsRozND6EzmbW4IxgKnhc0BTKKQbCEF6C+zEXBAGzgMu9emAp5kWcFuGQkzx2ccZUcNfdvwblI0I59hH9u0gjNzCdxF29ymKv15s90bULvSbqVjE9AqJH7Ycd5KULzfzaw0xXPx7hfgai0Z8luPs7kxXRGmKBDDBwfR/dBu9z3au7zXblpYzTs3TyXaKO1T8TLU0i0T5PhfqS6uh9NLEW8crzZgLx9szLkGfzL6NmLBEA7n91FwXkgHurP9hTKDSk/NP8v/APc9YnYRW4R7BL84wnzj9BkcLBLm48TFViPtDWz6SK75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5EwuLnDc14PUu4HPALSvrDLcKJyd0CmNzTLXke0KZRaH+eZUcOM7a2Vn2spMiH90sd1KdSZwoMiyFd3g/hiXvZHzWBf66MNFH2caz4jCtmOdZFhsl3nrvuzFH7Tc7KvdzZWleV3siR/pNt08+qjDhp4seIU9Kn89DRlbDGFnV/rZBCUBqoFVOJsrBok6gHuzHj86WT7SWRYuk5kTV7vHyEXyULo/TyDOyLa/O7La1ewC69gZsOco7r1YmTGL+BWc1YxiC7UWVSrZlF2zIcSiwvBM+TMXj3+z0Ekd65CfRFybwOA+EQ+szJ/jWjPqToy0GSwRjwpC4gP389EKxN4ePiN3BX4Gcg" + } + ] + }, + { + "role": "user", + "parts": [ + { + "functionResponse": { + "id": "redacted-id-2", + "name": "GetWeather", + "response": { + "name": "GetWeather", + "content": { + "temperatureC": 21, + "conditions": "sunny" + } + } + } + } + ] + } + ], + "tools": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "AUTO" + } + }, + "generationConfig": { + "thinkingConfig": { + "includeThoughts": true, + "thinkingLevel": "HIGH" + } + } + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-truncated.synthetic.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-truncated.synthetic.json new file mode 100644 index 0000000..03628b3 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream-truncated.synthetic.json @@ -0,0 +1,26 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Hello\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:37.446350Z\",\"responseId\":\"redacted-id-1\"}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "Reply with exactly one word: hello" + } + ] + } + ] + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream.json new file mode 100644 index 0000000..c953dbd --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/stream.json @@ -0,0 +1,26 @@ +[ + { + "status": 200, + "contentType": "text/event-stream", + "body": "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Hello\"}]}}],\"usageMetadata\":{},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:37.446350Z\",\"responseId\":\"redacted-id-1\"}\n\ndata: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a1+8UgbORPmTgmDsgGBMw3EXnZ6u4x1X9vjv6tSS8Q7OHII6cv9YnpB0UWu6U5/zuTKV/4Is7u2t/RBH7O/ilZ8gXsbrI3ZLRct30msrkXI/RBiv/XGApWw/NQ/hHD/dxotYIMny5OZMVx8ZFAOslQYmggs01cPQ4LIw1v21QwK/\",\"text\":\"\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":7,\"candidatesTokenCount\":1,\"totalTokenCount\":8,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":7}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":1}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:37.446350Z\",\"responseId\":\"redacted-id-1\"}\n\n", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:streamGenerateContent?alt=sse", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "Reply with exactly one word: hello" + } + ] + } + ] + }, + "headers": { + "accept": "text/event-stream", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/text.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/text.json new file mode 100644 index 0000000..ef263b1 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/text.json @@ -0,0 +1,26 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thoughtSignature\":\"AY89a1/a4thU5Tqdoovyp4sGFCW5Eo50qd2/TM+UVR+guAyJ69idgKqC7C0YEt8GacohGu4pikhG3afjxTkop+kjXY1/S27LNbCnObPT8T4Y94lzhn417sgdhjJcWM9rtotbxm8pj8vAqDMC4OC7gvtJsy8gU96erf8E0PkZTL/gAc8e\",\"text\":\"Hello\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":7,\"candidatesTokenCount\":1,\"totalTokenCount\":8,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":7}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":1}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:36.765030Z\",\"responseId\":\"redacted-id-1\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "text": "Reply with exactly one word: hello" + } + ] + } + ] + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + } +] diff --git a/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/tools.json b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/tools.json new file mode 100644 index 0000000..36e09e8 --- /dev/null +++ b/packages/foundry-effect-ai/test/fixtures/GoogleLanguageModel/tools.json @@ -0,0 +1,153 @@ +[ + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thought\":true,\"text\":\"**My Thought Process for Identifying the Olympic Host City and Preparing to Retrieve Weather Information**\\n\\nMy primary objective here is to pinpoint the European capital that served as the host for both the 1900 and 1924 Summer Olympic Games. A crucial condition is that I must only invoke the `GetWeather` tool for this identified city *after* I've successfully identified it. Furthermore, the final answer needs to be a single, concise sentence.\\n\\nLet's break down the identification process:\\n\\nFirst, I need to recall or deduce which city hosted the 1900 Summer Olympics. My knowledge base indicates this was Paris, France.\\nNext, I must do the same for the 1924 Summer Olympics. Again, my information points to Paris, France.\\n\\nTherefore, the city that hosted both events is Paris.\\n\\nNow, I need to consider the tool invocation. The instructions explicitly state to call the `GetWeather` tool for the identified city *before* answering. This means my immediate next step should be to execute `GetWeather(city='Paris')`.\\n\\nI understand that I must wait for the output from this tool call. Once I have the weather data, I will then synthesize all the information – the Olympic host city and the retrieved weather – into a single, well-formed sentence as the final answer.\\n\\nUpon reflection, I initially considered a slightly different interpretation of the prompt's sequence. However, re-reading the instruction \\\"Then call the GetWeather tool for it and answer in one sentence\\\" clarifies that the tool call should happen *now*, as part of this current turn, and the answering will follow in a subsequent turn after the tool's execution.\\n\\nA quick check confirms that Paris is indeed a European capital, so it meets that implicit requirement.\\n\\nMy intended action is therefore to proceed with calling the `GetWeather` tool for Paris.\\n\\n\"},{\"thoughtSignature\":\"AY89a1/lVuJuuOUTfTM3++Erg65dtubk0jGLSuaCDdPPrkFMESczkDaS4wrcnglvKA6zf75A/zzRrXIsONFEm1CUccSzhg5gEdepNTd7XUapZWtulEXs+VJMFGxvWE3Zbqsn0FPR6NdHgKOHaPCDLhovUrCyulMKK2ZMi9QSQlhct4W2jBI3XNa3nRA0wXrUBpbkJQDal3Lh2Be5zUmbA2q+JRgbnOZkDTwMmyDBM9QCtC5XwX8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3/ZTxURLSB2bCJxAFPt4p47lkORVk5HcaxmBNBuYwKwARqdqTlzT3yhBeSSXtFduU33GCf/DokmKaYEOJmrTJVyPcc8hnzUiNJYDSwpOp1NKU+e28h6sKfoeNpqMpJJTrUdltGbyECzG80W4JfT9M37f7WcQi/gpzgRbR\",\"functionCall\":{\"id\":\"redacted-id-1\",\"name\":\"GetWeather\",\"args\":{\"city\":\"Paris\"}}}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":67,\"candidatesTokenCount\":15,\"thoughtsTokenCount\":262,\"totalTokenCount\":344,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":67}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":15}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:39.242122Z\",\"responseId\":\"redacted-id-2\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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." + } + ] + } + ], + "tools": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "AUTO" + } + }, + "generationConfig": { + "thinkingConfig": { + "includeThoughts": true, + "thinkingLevel": "HIGH" + } + } + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + }, + { + "status": 200, + "contentType": "application/json", + "body": "{\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"thought\":true,\"text\":\"**My Train of Thought**\\n\\nAh, I see I need to summarize this information about Paris. First, I'll note the core facts: the city is Paris, and it's famous for hosting the Summer Olympics twice, in 1900 and 1924. Then, I'll add the current weather conditions: it's sunny, and the temperature is 21°C. Now, the crucial constraint: I must present this as a single, cohesive sentence. So, I'll weave these elements together logically. I'll start by identifying Paris and its Olympic history, then transition to the present-day weather. Combining these, I arrive at: \\\"Paris, which hosted the Summer Olympics in 1900 and 1924, currently has sunny weather with a temperature of 21°C.\\\"\\n\\n\"},{\"thoughtSignature\":\"AY89a18zhD+6PmYrX4LIwSubZOVYnhYVOrq/U64DClDiRcSRlaI/BT9SWP/HHOrn2cVRtefYMIZKp5cS4D+8lBXTBJdsI2fS8CRMV4j2HNGFOS++lYwHcUQJgPuFQKoRrNi1YImSrSqC8L9/0lZMac1pHLb0CxmqDX+VZjKnYGyL0BVowuQvd9RISPK9pcBbnN1II19WUMt18JJAJPT6+3WV1sCEGUHUV0paiAB/jYF4aiCNhb0hqy/1t8WrBdloEDuCx2Xo82gxrHdeM9ZnbHpYO0HCsv8L6pKiKSj6GbgpfbB3NI2NzvAAeW6MO3xX6/ODJ1FL1136/ljGt+OSaC9tZFJVqVmUFPAQpDJTLqethelCaQAGgOu9GBlqPjlQ1uHsMlzqT0kYXIjv7SvKKteQK0B/4o0URCqswUBLJqw1PmABY8WokBhin2b31sjW++2G6stEIh6s5PnDu3rDCS2gljXMhbP+UYDDt3o0tBclNdXZLuCsqJ5spe+lzK/9D54vaZXjLqLYpATM1DwuqaarxBvYzrtpVria\",\"text\":\"Paris, which hosted the Summer Olympics in 1900 and 1924, currently has sunny weather with a temperature of 21°C.\"}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":353,\"candidatesTokenCount\":34,\"thoughtsTokenCount\":88,\"totalTokenCount\":475,\"promptTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":353}],\"candidatesTokensDetails\":[{\"modality\":\"TEXT\",\"tokenCount\":34}]},\"modelVersion\":\"gemini-3.1-flash-lite\",\"createTime\":\"2026-11-20T02:35:42.106413Z\",\"responseId\":\"redacted-id-3\"}", + "request": { + "path": "/google/v1/models/ri.language-model-service..language-model.gemini-3-1-flash-lite:generateContent", + "body": { + "contents": [ + { + "role": "user", + "parts": [ + { + "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": "model", + "parts": [ + { + "text": "**My Thought Process for Identifying the Olympic Host City and Preparing to Retrieve Weather Information**\n\nMy primary objective here is to pinpoint the European capital that served as the host for both the 1900 and 1924 Summer Olympic Games. A crucial condition is that I must only invoke the `GetWeather` tool for this identified city *after* I've successfully identified it. Furthermore, the final answer needs to be a single, concise sentence.\n\nLet's break down the identification process:\n\nFirst, I need to recall or deduce which city hosted the 1900 Summer Olympics. My knowledge base indicates this was Paris, France.\nNext, I must do the same for the 1924 Summer Olympics. Again, my information points to Paris, France.\n\nTherefore, the city that hosted both events is Paris.\n\nNow, I need to consider the tool invocation. The instructions explicitly state to call the `GetWeather` tool for the identified city *before* answering. This means my immediate next step should be to execute `GetWeather(city='Paris')`.\n\nI understand that I must wait for the output from this tool call. Once I have the weather data, I will then synthesize all the information – the Olympic host city and the retrieved weather – into a single, well-formed sentence as the final answer.\n\nUpon reflection, I initially considered a slightly different interpretation of the prompt's sequence. However, re-reading the instruction \"Then call the GetWeather tool for it and answer in one sentence\" clarifies that the tool call should happen *now*, as part of this current turn, and the answering will follow in a subsequent turn after the tool's execution.\n\nA quick check confirms that Paris is indeed a European capital, so it meets that implicit requirement.\n\nMy intended action is therefore to proceed with calling the `GetWeather` tool for Paris.\n\n", + "thought": true + }, + { + "functionCall": { + "id": "redacted-id-1", + "name": "GetWeather", + "args": { + "city": "Paris" + } + }, + "thoughtSignature": "AY89a1/lVuJuuOUTfTM3++Erg65dtubk0jGLSuaCDdPPrkFMESczkDaS4wrcnglvKA6zf75A/zzRrXIsONFEm1CUccSzhg5gEdepNTd7XUapZWtulEXs+VJMFGxvWE3Zbqsn0FPR6NdHgKOHaPCDLhovUrCyulMKK2ZMi9QSQlhct4W2jBI3XNa3nRA0wXrUBp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Axe4i4ZS8EwXmVjf4BoqnB4rtO1eNbx8y+5ZOwf4+cIxFlf3/ZTxURLSB2bCJxAFPt4p47lkORVk5HcaxmBNBuYwKwARqdqTlzT3yhBeSSXtFduU33GCf/DokmKaYEOJmrTJVyPcc8hnzUiNJYDSwpOp1NKU+e28h6sKfoeNpqMpJJTrUdltGbyECzG80W4JfT9M37f7WcQi/gpzgRbR" + } + ] + }, + { + "role": "user", + "parts": [ + { + "functionResponse": { + "id": "redacted-id-1", + "name": "GetWeather", + "response": { + "name": "GetWeather", + "content": { + "temperatureC": 21, + "conditions": "sunny" + } + } + } + } + ] + } + ], + "tools": [ + { + "functionDeclarations": [ + { + "name": "GetWeather", + "parametersJsonSchema": { + "type": "object", + "properties": { + "city": { + "type": "string" + } + }, + "required": [ + "city" + ], + "additionalProperties": true + }, + "description": "Get the current weather in a city" + } + ] + } + ], + "toolConfig": { + "functionCallingConfig": { + "mode": "AUTO" + } + }, + "generationConfig": { + "thinkingConfig": { + "includeThoughts": true, + "thinkingLevel": "HIGH" + } + } + }, + "headers": { + "accept": "application/json", + "content-type": "application/json" + } + } + } +] From eeb55bd1fffd61378482ace3753ddfdf66a14eaa Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:37:24 -0600 Subject: [PATCH 2/4] docs(effect-ai): record Gemini structured output, budget and attribution (phase 5) --- packages/foundry-effect-ai/NOTICE | 4 ++++ packages/foundry-effect-ai/docs/PLAN.md | 6 +++--- scripts/check-tree-shaking.mjs | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/foundry-effect-ai/NOTICE b/packages/foundry-effect-ai/NOTICE index f606bd4..f651748 100644 --- a/packages/foundry-effect-ai/NOTICE +++ b/packages/foundry-effect-ai/NOTICE @@ -27,3 +27,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +The Gemini adapter (src/internal/google-gemini/) adapts code from the Vercel AI SDK's +@ai-sdk/google 4.0.122 (https://github.com/vercel/ai), Copyright 2023 Vercel, Inc., under the +Apache License, Version 2.0. diff --git a/packages/foundry-effect-ai/docs/PLAN.md b/packages/foundry-effect-ai/docs/PLAN.md index 6e1c965..a529a44 100644 --- a/packages/foundry-effect-ai/docs/PLAN.md +++ b/packages/foundry-effect-ai/docs/PLAN.md @@ -19,7 +19,8 @@ Release candidates publish to npm's `next` tag as phases land; phase 8 cuts the | xAI | `XaiLanguageModel` reuses the Responses codec, passing its xAI dialect as a value. | Foundry serves Grok through Responses; the AI SDK adapter's `xai-compat.ts` rewrites are unnecessary with lenient decoding. Vendor branches inside `openai-responses/` would be invisible to the tree-shaking check, since both modules own that folder. | | Build | `tsc` emits ESM, one file per module, `sideEffects: false`. | Effect 4 is ESM-only; per-file output lets bundlers drop unused modules without a pure-annotation pass. | | Model lists | Each vendor has its own plain list in `src/internal/models/.ts` (`openai`, `anthropic`, `google`, `xai`): a const object of alias → `{ rid }` plus only the flags the adapter reads. The phase that first needs a list adds it, and `model(id)` is typed from its keys. `packages/foundry-ai` is untouched. | Duplication is clearer than one source compiled into both packages, which would couple their builds, caches and releases. Depending on `@shpitdev/foundry-ai` would install the `ai` peer. | -| Upstream code | Port logic (prompt conversion, stream state machines), not generated schemas. Each derived file starts with `// Adapted from @effect/ai- (MIT), effect@4.1.4, 82904fe.`; the MIT text is in `NOTICE`. | Keeps attribution exact and auditable. | +| Upstream code | Port logic (prompt conversion, stream state machines), not generated schemas. Each derived file starts with `// Adapted from @effect/ai- (MIT), effect@4.1.4, 82904fe.`; the MIT text is in `NOTICE`. Gemini has no Effect provider, so `internal/google-gemini/` adapts `@ai-sdk/google` (Apache-2.0) 4.0.122 and says so. | Keeps attribution exact and auditable. | +| Gemini JSON output | `responseJsonSchema` from `LanguageModel.defaultCodecTransformer`. Beside function tools, the schema becomes a synthetic `json` function under `ANY` mode instead, and its call returns as the answer text, not a tool call. | Live check (November 19, 2026, `gemini-3.1-flash-lite`): Foundry accepted the default schema (`anyOf`, bounds, `enum`, `$defs`) and ignored `const`, which Effect never emits. With JSON and tools, the model re-called the tool after each result. | ## Target layout @@ -94,7 +95,7 @@ Order: 1 → {2, 6} → {3, 4, 5, 7}. Braced phases run in parallel worktrees. A | 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: 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. | +| 5 | `GoogleLanguageModel` | Done: the Gemini row passes offline and live on `gemini-3.1-flash-lite`. Foundry rejects a lower-case `thinkingLevel` (422); flash-lite reports thought summaries only at `MEDIUM` or above, so reasoning cells send `HIGH`. Replaying a function call without its thought signature fails with 400. | | 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. | | 8 | Stable release | Full-catalog live sweep recorded in a package `docs/README.md`; `pnpm run release foundry-effect-ai` (see [RELEASING.md](../../../docs/RELEASING.md)). | @@ -123,5 +124,4 @@ The wire has only predicate, choice and score, so there is no Locate. Settled ga ## Open questions - Which Foundry response deviations need lenient decoding beyond those already known; capture fixtures before relaxing a schema. -- Gemini structured output: the default codec transformer or a Gemini-specific JSON Schema subset (phase 5). - What a decision refusal without a name means; the adapter treats it as refusing every unanswered decision. diff --git a/scripts/check-tree-shaking.mjs b/scripts/check-tree-shaking.mjs index d8e9099..609d55e 100644 --- a/scripts/check-tree-shaking.mjs +++ b/scripts/check-tree-shaking.mjs @@ -35,6 +35,7 @@ const owners = { const budgets = { AnthropicLanguageModel: 25_000, FoundryClient: 6_800, + GoogleLanguageModel: 19_400, OpenAiDecisionModel: 13_600, OpenAiEmbeddingModel: 8_400, OpenAiChatLanguageModel: 21_500, From e665750ca5a87ee6538d6e3481148b4849df2ba4 Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:54:16 -0600 Subject: [PATCH 3/4] fix(effect-ai): hold Gemini stream calls until finish, replay synthetic JSON and local IDs, map in-band errors --- packages/foundry-effect-ai/docs/PLAN.md | 2 +- .../src/internal/google-gemini/request.ts | 42 +++++- .../src/internal/google-gemini/response.ts | 140 +++++++++++++----- .../src/internal/google-gemini/schema.ts | 10 +- .../src/internal/google-gemini/stream.ts | 91 +++++++----- 5 files changed, 194 insertions(+), 91 deletions(-) diff --git a/packages/foundry-effect-ai/docs/PLAN.md b/packages/foundry-effect-ai/docs/PLAN.md index a529a44..67067b2 100644 --- a/packages/foundry-effect-ai/docs/PLAN.md +++ b/packages/foundry-effect-ai/docs/PLAN.md @@ -20,7 +20,7 @@ Release candidates publish to npm's `next` tag as phases land; phase 8 cuts the | Build | `tsc` emits ESM, one file per module, `sideEffects: false`. | Effect 4 is ESM-only; per-file output lets bundlers drop unused modules without a pure-annotation pass. | | Model lists | Each vendor has its own plain list in `src/internal/models/.ts` (`openai`, `anthropic`, `google`, `xai`): a const object of alias → `{ rid }` plus only the flags the adapter reads. The phase that first needs a list adds it, and `model(id)` is typed from its keys. `packages/foundry-ai` is untouched. | Duplication is clearer than one source compiled into both packages, which would couple their builds, caches and releases. Depending on `@shpitdev/foundry-ai` would install the `ai` peer. | | Upstream code | Port logic (prompt conversion, stream state machines), not generated schemas. Each derived file starts with `// Adapted from @effect/ai- (MIT), effect@4.1.4, 82904fe.`; the MIT text is in `NOTICE`. Gemini has no Effect provider, so `internal/google-gemini/` adapts `@ai-sdk/google` (Apache-2.0) 4.0.122 and says so. | Keeps attribution exact and auditable. | -| Gemini JSON output | `responseJsonSchema` from `LanguageModel.defaultCodecTransformer`. Beside function tools, the schema becomes a synthetic `json` function under `ANY` mode instead, and its call returns as the answer text, not a tool call. | Live check (November 19, 2026, `gemini-3.1-flash-lite`): Foundry accepted the default schema (`anyOf`, bounds, `enum`, `$defs`) and ignored `const`, which Effect never emits. With JSON and tools, the model re-called the tool after each result. | +| Gemini JSON output | `responseJsonSchema` from `LanguageModel.defaultCodecTransformer`. Beside tools, the schema is a synthetic `json` function under `ANY` mode, and its call is the answer text and replays as that call. Keywords outside Gemini's subset and types with no JSON form pass through unchecked; the codec still validates the answer. | Live check (November 19, 2026, `gemini-3.1-flash-lite`): Foundry accepted the default schema (`anyOf`, bounds, `enum`, `$defs`) and ignored `const`, which Effect never emits. With JSON and tools, the model re-called the tool after each result. | ## Target layout diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/request.ts b/packages/foundry-effect-ai/src/internal/google-gemini/request.ts index d8a79b9..38ff837 100644 --- a/packages/foundry-effect-ai/src/internal/google-gemini/request.ts +++ b/packages/foundry-effect-ai/src/internal/google-gemini/request.ts @@ -20,17 +20,32 @@ export const metadataKey = 'foundry-google'; /** * Gemini accepts Effect's default JSON Schema as `responseJsonSchema`, including * `additionalProperties`, `anyOf`, bounds and `$defs`. It ignores `const`, which Effect never - * emits: literals become `enum`. + * emits: literals become `enum`. Keywords outside Gemini's subset, and types with no JSON form, + * pass through unchecked; the codec still validates the answer. */ export const codecTransformer = LanguageModel.defaultCodecTransformer; const fail = (reason: AiError.AiErrorReason) => AiError.make({ module: 'GoogleLanguageModel', method: 'makeRequest', reason }); -const signature = (options: Prompt.ProviderOptions): { thoughtSignature?: string } => { +/** What an earlier response part stored for replay under `metadataKey`. */ +const replayed = (options: Prompt.ProviderOptions) => { const value = options[metadataKey]; - const raw = Predicate.isObject(value) ? value['thoughtSignature'] : undefined; - return typeof raw === 'string' ? { thoughtSignature: raw } : {}; + const fields = Predicate.isObject(value) ? value : {}; + const { thoughtSignature, functionCall } = fields; + return { + signed: typeof thoughtSignature === 'string' ? { thoughtSignature } : {}, + localId: fields['localId'] === true, + // The synthetic JSON call whose arguments became answer text. + functionCall: + Predicate.isObject(functionCall) && typeof functionCall['name'] === 'string' + ? { + name: functionCall['name'], + args: functionCall['args'] ?? {}, + ...(typeof functionCall['id'] === 'string' ? { id: functionCall['id'] } : undefined), + } + : undefined, + }; }; const base64DataUrl = /^data:[^,]*;base64,/u; @@ -72,6 +87,8 @@ const toolResponse = Effect.fnUntraced(function* (part: Prompt.ToolResultPart) { const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { const system: Array<{ text: string }> = []; const contents: Array = []; + // Call IDs generated locally because Gemini sent none; they stay off the wire. + const localIds = new Set(); for (const message of prompt.content) { const parts: Array = []; switch (message.role) { @@ -93,8 +110,10 @@ const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { break; case 'assistant': for (const part of message.content) { - const signed = signature(part.options); - if ( + const { signed, localId, functionCall } = replayed(part.options); + if (part.type === 'text' && functionCall !== undefined) { + parts.push({ functionCall, ...signed }); + } else if ( (part.type === 'text' || part.type === 'reasoning') && (part.text !== '' || signed.thoughtSignature !== undefined) ) { @@ -104,8 +123,15 @@ const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { ...signed, }); } else if (part.type === 'tool-call' && !part.providerExecuted) { + if (localId) { + localIds.add(part.id); + } parts.push({ - functionCall: { id: part.id, name: part.name, args: part.params }, + functionCall: { + ...(localId ? undefined : { id: part.id }), + name: part.name, + args: part.params, + }, ...signed, }); } @@ -119,7 +145,7 @@ const encodePrompt = Effect.fnUntraced(function* (prompt: Prompt.Prompt) { if (part.type === 'tool-result') { parts.push({ functionResponse: { - id: part.id, + ...(localIds.has(part.id) ? undefined : { id: part.id }), name: part.name, response: yield* toolResponse(part), }, diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/response.ts b/packages/foundry-effect-ai/src/internal/google-gemini/response.ts index 0093433..0a3cc06 100644 --- a/packages/foundry-effect-ai/src/internal/google-gemini/response.ts +++ b/packages/foundry-effect-ai/src/internal/google-gemini/response.ts @@ -22,36 +22,55 @@ const encodeJson = Schema.encodeUnknownEffect(Schema.fromJsonString(Schema.Unkno export const jsonText = (method: string, value: unknown): Effect.Effect => Effect.mapError(encodeJson(value), (error) => invalidResponse(method, error.message)); -/** - * A part's text: its own, or the arguments of a call to the synthetic JSON function, which is - * the answer rather than a tool call. - */ -export const partText = ( - method: string, - part: typeof Wire.ResponsePart.Type, -): Effect.Effect => - part.functionCall === undefined - ? Effect.succeed(part.text) - : jsonText(method, part.functionCall.args ?? {}); +/** HTTP status for each Google RPC status, for errors that carry no numeric code. */ +const rpcStatuses: Readonly> = { + INVALID_ARGUMENT: 400, + FAILED_PRECONDITION: 400, + OUT_OF_RANGE: 400, + UNAUTHENTICATED: 401, + PERMISSION_DENIED: 403, + NOT_FOUND: 404, + ALREADY_EXISTS: 409, + ABORTED: 409, + RESOURCE_EXHAUSTED: 429, + CANCELLED: 499, + UNKNOWN: 500, + INTERNAL: 500, + DATA_LOSS: 500, + UNIMPLEMENTED: 501, + UNAVAILABLE: 503, + DEADLINE_EXCEEDED: 504, +}; /** - * An error reported inside a successful response or stream. `RESOURCE_EXHAUSTED` is throttling; - * anything else is a provider failure. Code and status stay in metadata. + * An error reported inside a successful response or stream, classified by its numeric code or + * RPC status as `FoundryClient` classifies HTTP statuses. Code and status stay in metadata. */ export const providerError = ( method: string, error: typeof Wire.ErrorObject.Type, ): AiError.AiError => { - const metadata = { [metadataKey]: { code: error.code ?? null, status: error.status ?? null } }; - const reason = - error.status === 'RESOURCE_EXHAUSTED' || error.code === 429 - ? new AiError.RateLimitError({ metadata }) - : new AiError.InternalProviderError({ - description: - [error.status ?? error.code, error.message].filter((text) => text != null).join(': ') || - 'Response failed', - metadata, - }); + const status = error.code ?? (error.status == null ? undefined : rpcStatuses[error.status]); + const common = { + description: + [error.status ?? error.code, error.message].filter((text) => text != null).join(': ') || + 'Response failed', + metadata: { [metadataKey]: { code: error.code ?? null, status: error.status ?? null } }, + }; + let reason: AiError.AiErrorReason; + if (status === 400 || status === 404 || status === 409) { + reason = new AiError.InvalidRequestError(common); + } else if (status === 401) { + reason = new AiError.AuthenticationError({ kind: 'InvalidKey', ...common }); + } else if (status === 403) { + reason = new AiError.AuthenticationError({ kind: 'InsufficientPermissions', ...common }); + } else if (status === 429) { + reason = new AiError.RateLimitError({ metadata: common.metadata }); + } else if (status === undefined || status >= 500) { + reason = new AiError.InternalProviderError(common); + } else { + reason = new AiError.UnknownError(common); + } return AiError.make({ module, method, reason }); }; @@ -66,11 +85,32 @@ export const responseMetadata = ( ), }); -/** The opaque signature Gemini needs back on the part that carried it. */ -export const signed = ( - thoughtSignature: string | undefined, -): { metadata?: Record } => - thoughtSignature === undefined ? {} : { metadata: { [metadataKey]: { thoughtSignature } } }; +/** + * What replay needs under `metadataKey`: the part's thought signature, `localId` for a call ID + * Gemini did not send, and `functionCall` for answer text that came from the synthetic JSON call. + */ +export const replayMetadata = (fields: { + readonly thoughtSignature?: string | undefined; + readonly localId?: boolean | undefined; + readonly functionCall?: typeof Wire.FunctionCall.Type | undefined; +}): { metadata?: Response.ProviderMetadata } => { + const replay: Record = {}; + if (fields.thoughtSignature !== undefined) { + replay['thoughtSignature'] = fields.thoughtSignature; + } + if (fields.localId === true) { + replay['localId'] = true; + } + const call = fields.functionCall; + if (call !== undefined) { + replay['functionCall'] = { + name: call.name, + args: call.args ?? {}, + ...(call.id === undefined ? undefined : { id: call.id }), + }; + } + return Object.keys(replay).length === 0 ? {} : { metadata: { [metadataKey]: replay } }; +}; /** A prompt block reason other than the unspecified placeholder. */ export const blocked = (body: typeof Wire.Response.Type): boolean => { @@ -136,8 +176,8 @@ export const usage = ( }; /** - * A function call with its wire ID, or a generated one if Gemini sent none. Arguments arrive - * as an object and are passed through; Toolkit validates them. + * A function call with its wire ID, or a generated local one if Gemini sent none. Arguments + * arrive as an object and are passed through; Toolkit validates them. */ export const toolCall = Effect.fnUntraced(function* ( method: string, @@ -165,11 +205,36 @@ export const toolCall = Effect.fnUntraced(function* ( id: functionCall.id ?? (yield* ids.generateId()), name, params: functionCall.args ?? {}, - ...signed(thoughtSignature), + ...replayMetadata({ thoughtSignature, localId: functionCall.id === undefined }), }; return call; }); +/** + * The text or reasoning a part carries, if any. A call to the synthetic JSON function is the + * answer text, and in that mode other text is not. A signature-only fragment is empty text that + * keeps its signature, with or without a `text` field. + */ +export const partContent = Effect.fnUntraced(function* ( + method: string, + part: typeof Wire.ResponsePart.Type, + jsonTool: string | undefined, +) { + const call = part.functionCall; + const skipped = call === undefined && jsonTool !== undefined && part.thought !== true; + let text = part.text ?? ''; + if (call !== undefined) { + text = yield* jsonText(method, call.args ?? {}); + } + return skipped || (text === '' && part.thoughtSignature === undefined) + ? undefined + : { + kind: part.thought === true ? ('reasoning' as const) : ('text' as const), + text, + ...replayMetadata({ thoughtSignature: part.thoughtSignature, functionCall: call }), + }; +}); + export const decodeResponse = Effect.fnUntraced(function* ( body: typeof Wire.Response.Type, options: LanguageModel.ProviderOptions, @@ -189,14 +254,11 @@ export const decodeResponse = Effect.fnUntraced(function* ( ); continue; } - const text = yield* partText('generateText', part); - // A signature-only part stays a part of its own, so replay puts it back in place. - if (text !== undefined && (text !== '' || part.thoughtSignature !== undefined)) { - parts.push({ - type: part.thought === true ? 'reasoning' : 'text', - text, - ...signed(part.thoughtSignature), - }); + const content = yield* partContent('generateText', part, jsonTool); + if (content !== undefined) { + // A signature-only part stays a part of its own, so replay puts it back in place. + const { kind, ...rest } = content; + parts.push({ type: kind, ...rest }); } } parts.push({ diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts b/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts index 8a17fc7..684ba06 100644 --- a/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts +++ b/packages/foundry-effect-ai/src/internal/google-gemini/schema.ts @@ -16,12 +16,16 @@ export type Part = | { readonly inlineData: { readonly mimeType: string; readonly data: string } } | { readonly fileData: { readonly mimeType: string; readonly fileUri: string } } | { - readonly functionCall: { readonly id: string; readonly name: string; readonly args: unknown }; + readonly functionCall: { + readonly id?: string; + readonly name: string; + readonly args: unknown; + }; readonly thoughtSignature?: string; } | { readonly functionResponse: { - readonly id: string; + readonly id?: string; readonly name: string; readonly response: | { readonly name: string; readonly content: unknown } @@ -88,7 +92,7 @@ export const ErrorObject = Schema.Struct({ export const FunctionCall = Schema.Struct({ id: Schema.optional(Schema.String), name: Schema.String, - args: Schema.optional(Schema.Unknown), + args: Schema.optional(Schema.Json), }); export const ResponsePart = Schema.Struct({ diff --git a/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts b/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts index b2ddeb1..f07a0b1 100644 --- a/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts +++ b/packages/foundry-effect-ai/src/internal/google-gemini/stream.ts @@ -2,9 +2,12 @@ /** * `streamGenerateContent?alt=sse` events → Effect AI stream parts. Each event is a response * chunk. Text and thoughts open a block on their first delta and close on a switch, a function - * call or the end. Gemini sends no terminal event: the finish comes at end of stream, and only - * after a chunk reported a finish reason or a prompt block; otherwise the stream was cut off and - * fails with `InvalidResponseError`. An in-band error fails the stream. + * call, a part with replay metadata or the end. Gemini sends no terminal event: the finish comes + * at end of stream, and only after a chunk reported a finish reason or a prompt block; otherwise + * the stream was cut off and fails with `InvalidResponseError`. An in-band error fails the + * stream. Function call parameters stream as they arrive, but the calls themselves, and any + * content after them, wait for the finish: core starts a handler once the stream moves past its + * call, so a call sent before a `length` or `content-filter` finish would already be running. */ import type * as AiError from 'effect/ai/AiError'; import type * as IdGenerator from 'effect/ai/IdGenerator'; @@ -20,10 +23,9 @@ import { hasUsage, invalidResponse, jsonText, - partText, + partContent, providerError, responseMetadata, - signed, toolCall, usage, } from './response.ts'; @@ -32,11 +34,11 @@ import * as Wire from './schema.ts'; const method = 'streamText'; const decodeChunk = Schema.decodeUnknownEffect(Schema.fromJsonString(Wire.Response)); -/** An open text or reasoning block, and the signature that closed it. */ +/** An open text or reasoning block, and the replay metadata of the part that closed it. */ interface Block { readonly kind: 'text' | 'reasoning'; readonly id: string; - signature?: string; + metadata?: Response.ProviderMetadata; } export const decodeStream = ( @@ -51,29 +53,30 @@ export const decodeStream = ( let isBlocked = false; let hasToolCalls = false; let lastUsage: typeof Wire.UsageMetadata.Type | undefined; + // Tool calls, and content after the first of them, until the finish. + const held: Array = []; const end = (): Array => { if (open === undefined) { return []; } - const { kind, id, signature } = open; + const { kind, id, metadata } = open; open = undefined; + const meta = metadata === undefined ? {} : { metadata }; return [ - kind === 'text' - ? { type: 'text-end', id, ...signed(signature) } - : { type: 'reasoning-end', id, ...signed(signature) }, + kind === 'text' ? { type: 'text-end', id, ...meta } : { type: 'reasoning-end', id, ...meta }, ]; }; /** The open block of this kind, opening one (and closing any other) if needed. */ - const ensure = (kind: Block['kind'], parts: Array): Block => { + const ensure = (kind: Block['kind'], out: Array): Block => { if (open?.kind === kind) { return open; } - parts.push(...end()); + out.push(...end()); const block: Block = { kind, id: String(blocks++) }; open = block; - parts.push( + out.push( kind === 'text' ? { type: 'text-start', id: block.id } : { type: 'reasoning-start', id: block.id }, @@ -95,9 +98,10 @@ export const decodeStream = ( } const candidate = chunk.candidates?.[0]; for (const part of candidate?.content?.parts ?? []) { + const out = held.length > 0 ? held : parts; if (part.functionCall !== undefined && part.functionCall.name !== jsonTool) { // Gemini sends a call's arguments whole, in one part. - parts.push(...end()); + out.push(...end()); hasToolCalls = true; const call = yield* toolCall(method, options, part.functionCall, part.thoughtSignature); const delta = yield* jsonText(method, call.params); @@ -105,29 +109,33 @@ export const decodeStream = ( { type: 'tool-params-start', id: call.id, name: call.name }, { type: 'tool-params-delta', id: call.id, delta }, { type: 'tool-params-end', id: call.id }, - call, ); + held.push(call); continue; } - const text = yield* partText(method, part); - if (text === undefined || (text === '' && part.thoughtSignature === undefined)) { + const content = yield* partContent(method, part, jsonTool); + if (content === undefined) { continue; } - const block = ensure(part.thought === true ? 'reasoning' : 'text', parts); - if (text !== '') { - parts.push( + const block = ensure(content.kind, out); + if (content.text !== '') { + out.push( block.kind === 'text' - ? { type: 'text-delta', id: block.id, delta: text } - : { type: 'reasoning-delta', id: block.id, delta: text }, + ? { type: 'text-delta', id: block.id, delta: content.text } + : { type: 'reasoning-delta', id: block.id, delta: content.text }, ); } - // A signature ends its wire part, so it closes the block and replays in place. - if (part.thoughtSignature !== undefined) { - block.signature = part.thoughtSignature; - parts.push(...end()); + // Replay metadata ends its wire part, so it closes the block and replays in place. + if (content.metadata !== undefined) { + block.metadata = content.metadata; + out.push(...end()); } } - reason = candidate?.finishReason ?? reason; + // An empty or unspecified reason means generation has not stopped. + const reported = candidate?.finishReason; + if (reported !== undefined && reported !== '' && reported !== 'FINISH_REASON_UNSPECIFIED') { + reason = reported; + } isBlocked ||= blocked(chunk); if (hasUsage(chunk.usageMetadata)) { lastUsage = chunk.usageMetadata; @@ -139,18 +147,21 @@ export const decodeStream = ( Stream.mapEffect(handle), Stream.flattenIterable, Stream.concat( - Stream.suspend(() => - reason === undefined && !isBlocked - ? Stream.fail(invalidResponse(method, 'Stream ended before a finish reason')) - : Stream.fromIterable([ - ...end(), - { - type: 'finish', - reason: finishReason({ reason, blocked: isBlocked, hasToolCalls }), - usage: usage(lastUsage), - }, - ]), - ), + Stream.suspend(() => { + if (reason === undefined && !isBlocked) { + return Stream.fail(invalidResponse(method, 'Stream ended before a finish reason')); + } + // One array, so core sees the calls and an incomplete finish together. + return Stream.fromArray([ + ...held, + ...end(), + { + type: 'finish', + reason: finishReason({ reason, blocked: isBlocked, hasToolCalls }), + usage: usage(lastUsage), + }, + ]); + }), ), ); }; From b85bb41b805cf76deacfdaf4ae6aec7e7fd42e44 Mon Sep 17 00:00:00 2001 From: Anand Pant Date: Thu, 19 Nov 2026 20:55:07 -0600 Subject: [PATCH 4/4] chore(effect-ai): reset GoogleLanguageModel budget to 21,000 bytes --- scripts/check-tree-shaking.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/check-tree-shaking.mjs b/scripts/check-tree-shaking.mjs index 609d55e..cc1a37b 100644 --- a/scripts/check-tree-shaking.mjs +++ b/scripts/check-tree-shaking.mjs @@ -35,7 +35,7 @@ const owners = { const budgets = { AnthropicLanguageModel: 25_000, FoundryClient: 6_800, - GoogleLanguageModel: 19_400, + GoogleLanguageModel: 21_000, OpenAiDecisionModel: 13_600, OpenAiEmbeddingModel: 8_400, OpenAiChatLanguageModel: 21_500,