Skip to content
Open
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
11 changes: 8 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ MetaAgent (optimizer loop)
| `HarnessSpec.ts` | Optimizer state, harness manifest, diagnosis output |
| `MetaAgent.ts` | Meta-agent optimizer (assisted + autonomous modes) |
| `Metrics.ts` | Token/cost metrics extraction + trajectory JSON |
| `Providers.ts` | OpenAI + Anthropic LanguageModel layers |
| `Providers.ts` | OpenAI-compatible LanguageModel layers |
| `ShellTool.ts` | Low-level shell execution effect |
| `TaskSpec.ts` | Task directory reader with TOML parsing |
| `TrajectoryConverter.ts` | OpenAI/Claude SDK messages → ATIF conversion |
Expand Down Expand Up @@ -164,10 +164,15 @@ All errors extend `Schema.TaggedErrorClass` and are recoverable via
```ts
openAiModel('gpt-5.4'); // Layer<LanguageModel, never, never>
anthropicModel('claude-sonnet-4-6'); // Layer<LanguageModel, never, never>
openRouterModel('openai/gpt-4o'); // OpenAI-compatible Chat Completions
openCodeModel('kimi-k2.5'); // OpenCode Zen
openCodeGoModel('kimi-k3'); // OpenCode Go
```

Both are fully wired (include HttpClient + API key config). Pass directly
to `AgentExecutor.layer` via `Layer.provide`.
All bundled layers include the HTTP client and API key config. OpenRouter,
OpenCode Zen, and OpenCode Go use their documented OpenAI-compatible endpoints;
pass `protocol: 'responses'` to an OpenCode helper for a Responses model. Pass
the selected layer directly to `AgentExecutor.layer` via `Layer.provide`.

## CLI Usage

Expand Down
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tools, orchestration strategy, and constraints.
## Quick start

**Requirements:** [Bun](https://bun.sh), Docker, and API credentials for your
chosen provider (OpenAI or Anthropic).
chosen provider (OpenAI, Anthropic, OpenRouter, or OpenCode).

```bash
# 1. Install dependencies
Expand All @@ -62,6 +62,10 @@ bun install
# 2. Set up environment variables
cat > .env << 'EOF'
OPENAI_API_KEY=...
# Use the key matching the selected compatible provider.
OPENROUTER_API_KEY=...
OPENCODE_API_KEY=...
OPENCODE_GO_API_KEY=...
EOF

# 3. Verify the harness compiles
Expand Down Expand Up @@ -131,7 +135,7 @@ src/
HarnessSpec.ts — optimizer state, harness manifest, diagnosis output
MetaAgent.ts — meta-agent optimizer (assisted + autonomous modes)
Metrics.ts — token/cost metrics + trajectory JSON serialization
Providers.ts — OpenAI + Anthropic LanguageModel layers
Providers.ts — OpenAI-compatible LanguageModel layers
ShellTool.ts — low-level shell execution effect
TaskSpec.ts — task directory reader with TOML parsing
TrajectoryConverter.ts — OpenAI/Claude SDK messages → ATIF conversion
Expand Down Expand Up @@ -202,7 +206,8 @@ MetaAgent (optimizer loop)

## Providers

Both OpenAI and Anthropic are supported as fully-wired `LanguageModel` layers:
OpenAI, Anthropic, OpenRouter, and OpenCode are supported as fully-wired
`LanguageModel` layers:

```ts
import { openAiModel, anthropicModel } from 'effect-autoagent';
Expand All @@ -211,7 +216,26 @@ openAiModel('gpt-5.4'); // Layer<LanguageModel>
anthropicModel('claude-sonnet-4-6'); // Layer<LanguageModel>
```

API keys are read from config (`OPENAI_API_KEY` / `ANTHROPIC_API_KEY`).
OpenAI-compatible providers use the published Chat Completions package:

```ts
import {
openCodeGoModel,
openCodeModel,
openRouterModel
} from 'effect-autoagent';

openRouterModel('openai/gpt-4o'); // OPENROUTER_API_KEY
openCodeModel('kimi-k2.5'); // OPENCODE_API_KEY (Zen)
openCodeGoModel('kimi-k3'); // OPENCODE_GO_API_KEY (or OPENCODE_API_KEY)
openCodeModel('gpt-5.4', { protocol: 'responses' });
```

API keys are read from config (`OPENAI_API_KEY`, `ANTHROPIC_API_KEY`,
`OPENROUTER_API_KEY`, `OPENCODE_API_KEY`, and `OPENCODE_GO_API_KEY`). Endpoint
defaults can be overridden with `OPENROUTER_API_URL`, `OPENCODE_API_URL`, and
`OPENCODE_GO_API_URL`. The CLI accepts `--provider openrouter`,
`--provider opencode`, and `--provider opencode-go`.

## Library usage

Expand Down
3 changes: 3 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"dependencies": {
"@effect/ai-anthropic": "4.0.0-beta.46",
"@effect/ai-openai": "4.0.0-beta.46",
"@effect/ai-openai-compat": "4.0.0-beta.46",
"@effect/platform-bun": "4.0.0-beta.46",
"smol-toml": "^1.6.1"
},
Expand Down
8 changes: 7 additions & 1 deletion src/AgentBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import { defaultToolSpecs, ToolSpec } from './ToolSpec.js';
*
* @since 0.3.0
*/
export const ModelProvider = Schema.Literals(['openai', 'anthropic']).annotate({
export const ModelProvider = Schema.Literals([
'openai',
'anthropic',
'openrouter',
'opencode',
'opencode-go'
]).annotate({
title: 'ModelProvider',
description: 'LLM provider for the agent.'
});
Expand Down
39 changes: 34 additions & 5 deletions src/AgentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as Str from 'effect/String';
import { Chat, LanguageModel, Prompt } from 'effect/unstable/ai';
import type * as Tool from 'effect/unstable/ai/Tool';

import { type AgentBlueprint } from './AgentBlueprint.js';
import { type AgentBlueprint, type ModelProvider } from './AgentBlueprint.js';
import { AgentRunResult, type ExitReason } from './AgentRunResult.js';
import { AgentInfo, AtifStep, buildTrajectory, FinalMetrics } from './Atif.js';
import {
Expand All @@ -31,7 +31,13 @@ import type {
PlanAndExecute,
WithVerifier
} from './OrchestrationSpec.js';
import { anthropicModel, openAiModel } from './Providers.js';
import {
anthropicModel,
openAiModel,
openCodeGoModel,
openCodeModel,
openRouterModel
} from './Providers.js';
import { ToolFactory, type BuiltToolkit } from './ToolFactory.js';
import { UsageSnapshot } from './UsageMetrics.js';

Expand Down Expand Up @@ -883,16 +889,32 @@ const runWithVerifier = Effect.fn('AgentFactory.withVerifier')(function* (
* Infer the provider from a model name using prefix heuristics.
*
* - `claude-*` → Anthropic
* - `openrouter/*` → OpenRouter
* - `opencode/*` → OpenCode Zen
* - `opencode-go/*` → OpenCode Go
* - Everything else → OpenAI (safest fallback for unknown models)
*
* @internal
*/
const inferProvider = (modelName: string): 'openai' | 'anthropic' =>
const inferProvider = (modelName: string): ModelProvider =>
Match.value(modelName).pipe(
Match.when(Str.startsWith('claude'), () => 'anthropic' as const),
Match.when(Str.startsWith('openrouter/'), () => 'openrouter' as const),
Match.when(
Str.startsWith('opencode-go/'),
() => 'opencode-go' as const
),
Match.when(Str.startsWith('opencode/'), () => 'opencode' as const),
Match.orElse(() => 'openai' as const)
);

const stripProviderPrefix = (modelName: string, provider: ModelProvider) => {
const prefix = `${provider}/`;
return modelName.startsWith(prefix)
? modelName.slice(prefix.length)
: modelName;
};

/**
* Model fallback strategy.
*
Expand Down Expand Up @@ -927,11 +949,18 @@ const runFallbackModels = Effect.fn('AgentFactory.fallbackModels')(function* (
);

const provider = inferProvider(modelName);
const providerModel = stripProviderPrefix(modelName, provider);
// ConfigError from API key lookup is unrecoverable — orDie per EF-31
const modelLayer = Layer.orDie(
provider === 'openai'
? openAiModel(modelName)
: anthropicModel(modelName)
? openAiModel(providerModel)
: provider === 'anthropic'
? anthropicModel(providerModel)
: provider === 'openrouter'
? openRouterModel(providerModel)
: provider === 'opencode'
? openCodeModel(providerModel)
: openCodeGoModel(providerModel)
);

const loopLayer = Layer.mergeAll(modelLayer, envLayer).pipe(
Expand Down
Loading