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
2 changes: 1 addition & 1 deletion packages/foundry-effect-ai/docs/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Order: 1 → {2, 6} → {3, 4, 5, 7}. Braced phases run in parallel worktrees. A
| 1 | `FoundryClient` with `json` only, a finished `internal/errors.ts`, fixture recording, `OpenAiDecisionModel`, and the phase 7 probe. `errors.ts` maps status to reason, the Foundry error envelope (vendor bodies arrive in `parameters.responseBody` as `Optional[...]`), and a lenient read of `error.{message,type,code,status}`; in-stream errors stay in each protocol folder. | Done: the `FoundryClient` and `OpenAiDecisionModel` rows pass offline and live, with a 401 (bad token) and a 404 `LanguageModelNotAvailable` (unknown model) recorded from each vendor route. |
| 2 | `OpenAiLanguageModel` (Responses, written directly), `FoundryClient.sse` and `test/capabilities.ts` | Done: the OpenAI row passes offline and live on `gpt-6-luna`, with a recorded `reasoning_summary_part` stream and a `tools` replay of encrypted reasoning. Foundry's `reasoning_summary_part.added` carries only `type`, so reasoning blocks open from the summary deltas. |
| 3 | `XaiLanguageModel`. Extract the Responses dialect from phase 2 and pass it as a value from each vendor module. | Done: the xAI row passes offline and live on `grok-4-6`. Foundry's xAI route takes assistant message IDs and streams OpenAI's events, so the dialect differs only in route, namespace, `system` role and always asking for encrypted reasoning. `json-tools` is flaky on Grok, which skips the tool in about 1 of 3 first turns. |
| 4 | `AnthropicLanguageModel` | Anthropic row has no todos, covering adaptive thinking and both structured-output modes; live passes. |
| 4 | `AnthropicLanguageModel` | Done: passes offline and live on `claude-haiku-4.5` (budget thinking, JSON tool) and `claude-sonnet-5.5` (adaptive thinking, `output_config.format`). Forced tool choices fail locally with thinking or on 5.5 models. Out of scope: `cache_control` and content prepopulated in `message_start`. |
| 5 | `GoogleLanguageModel` | Gemini row has no todos; live passes. Highest risk: `effect/ai` has no Gemini structured-output codec. |
| 6 | `OpenAiEmbeddingModel`. Depends only on phase 1, so it runs beside phase 2. Embedding RIDs equal their IDs (`text-embedding-*`). | Embedding row has no todos; all three catalog embedding models pass live. |
| 7 | `OpenAiChatLanguageModel` and `internal/openai-chat-completions/`, on `/openai/v1/chat/completions` | Done: the Chat row passes offline and live on `gpt-6-luna`. The route takes `max_completion_tokens`, not `max_tokens`; streams end without `[DONE]`; OpenAI models return no reasoning text; GPT-6 Luna, Sol and 6.1 Terra accept tools only with `reasoning_effort: 'none'`. Anthropic, Google and xAI have no working Chat route. |
Expand Down
94 changes: 84 additions & 10 deletions packages/foundry-effect-ai/src/AnthropicLanguageModel.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,99 @@
// Adapted from @effect/ai-anthropic (MIT), effect@4.1.4, 82904fe.
/** Anthropic language models through Foundry's `/anthropic/v1/messages` route. */
import { toCodecAnthropic } from 'effect/ai/AnthropicStructuredOutput';
import * as LanguageModel from 'effect/ai/LanguageModel';
import * as AiModel from 'effect/ai/Model';
import type * as Effect from 'effect/Effect';
import * as Effect from 'effect/Effect';
import * as Layer from 'effect/Layer';
import type { FoundryClient } from './FoundryClient.ts';
import { notImplemented } from './internal/notImplemented.ts';
import * as Stream from 'effect/Stream';
import { FoundryClient } from './FoundryClient.ts';
import { makeRequest, type Target } from './internal/anthropic-messages/request.ts';
import { decodeResponse } from './internal/anthropic-messages/response.ts';
import * as Wire from './internal/anthropic-messages/schema.ts';
import { decodeStream } from './internal/anthropic-messages/stream.ts';
import { models } from './internal/models/anthropic.ts';

export const providerName = 'foundry-anthropic';

/** A catalog alias or a raw Foundry model RID. */
export type ModelId = keyof typeof models | (string & Record<never, never>);

/** Request-wide settings, sent as the Messages fields of the same name. */
export interface Config {
/** Defaults to the model's output limit, or 4096 for a target outside the catalog. */
readonly max_tokens?: number | undefined;
/**
* For example `{ type: 'adaptive' }`, or `{ type: 'enabled', budget_tokens: 2048 }` on models
* before Claude 5.5. Thinking blocks replay with their signatures on later turns.
*/
readonly thinking?: Wire.Thinking | undefined;
}

const known: Readonly<
Record<
string,
{
readonly rid: string;
readonly maxOutputTokens: number;
readonly rejectsForcedToolUse?: boolean;
}
>
> = models;

/** Known aliases and RIDs get their catalog flags; any other target passes through as is. */
const resolve = (modelId: string): Target => {
const entry =
known[modelId] ?? Object.values(known).find((candidate) => candidate.rid === modelId);
return {
rid: entry?.rid ?? modelId,
maxOutputTokens: entry?.maxOutputTokens ?? 4096,
rejectsForcedToolUse: entry?.rejectsForcedToolUse === true,
};
};

const path = '/anthropic/v1/messages';
const headers = { 'anthropic-version': '2023-06-01' };

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('AnthropicLanguageModel.make');
/**
* Claude Opus and Sonnet 5.5 reject forced tool use, so their structured output is native
* `output_config.format`; other models answer through a forced JSON tool. Tool input is never
* streamed eagerly: Foundry does not support it.
*/
export const make = Effect.fnUntraced(function* (options: {
readonly model: ModelId;
readonly config?: Config | undefined;
}) {
const client = yield* FoundryClient;
const target = resolve(options.model);
const config = options.config;
return yield* LanguageModel.make({
codecTransformer: toCodecAnthropic,
generateText: Effect.fnUntraced(function* (providerOptions) {
const request = yield* makeRequest(providerOptions, target, config);
const body = yield* client.json({ path, headers, body: request, schema: Wire.Body });
return yield* decodeResponse(body, providerOptions, target);
}),
streamText: (providerOptions) =>
makeRequest(providerOptions, target, config).pipe(
Effect.map((request) =>
decodeStream(
client.sse({ path, headers, body: { ...request, stream: true } }),
providerOptions,
),
),
Stream.unwrap,
),
});
});

export const layer = (options: {
readonly model: string;
readonly model: ModelId;
readonly config?: Config | undefined;
}): Layer.Layer<LanguageModel.LanguageModel, never, FoundryClient> =>
Layer.effect(LanguageModel.LanguageModel, make(options));
Loading