Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/foundry-effect-ai/NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions packages/foundry-effect-ai/docs/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<vendor>.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-<x> (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-<x> (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 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

Expand Down Expand Up @@ -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)). |
Expand Down Expand Up @@ -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.
80 changes: 70 additions & 10 deletions packages/foundry-effect-ai/src/GoogleLanguageModel.ts
Original file line number Diff line number Diff line change
@@ -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<never, never>);

/** 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<Record<string, { readonly rid: string }>> = 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<typeof providerName, LanguageModel.LanguageModel, FoundryClient> =>
AiModel.make(providerName, modelId, layer({ model: modelId }));
AiModel.make(providerName, modelId, layer({ model: modelId, config }));

export const make = (_options: {
readonly model: string;
}): Effect.Effect<LanguageModel.LanguageModel, never, FoundryClient> =>
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<LanguageModel.LanguageModel, never, FoundryClient> =>
Layer.effect(LanguageModel.LanguageModel, make(options));
Loading