@agentionai/agents 1.6.0 → 1.8.0-beta-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/agents/Agent.d.ts +9 -2
- package/dist/agents/Agent.js +4 -0
- package/dist/agents/AgentConfig.d.ts +76 -2
- package/dist/agents/BaseAgent.d.ts +24 -2
- package/dist/agents/BaseAgent.js +17 -0
- package/dist/agents/anthropic/ClaudeAgent.d.ts +4 -3
- package/dist/agents/anthropic/ClaudeAgent.js +47 -17
- package/dist/agents/cancellation.d.ts +55 -0
- package/dist/agents/cancellation.js +72 -0
- package/dist/agents/errors/AgentError.d.ts +50 -2
- package/dist/agents/errors/AgentError.js +57 -1
- package/dist/agents/google/GeminiAgent.d.ts +3 -2
- package/dist/agents/google/GeminiAgent.js +34 -11
- package/dist/agents/mistral/MistralAgent.d.ts +3 -2
- package/dist/agents/mistral/MistralAgent.js +33 -13
- package/dist/agents/ollama/OllamaAgent.d.ts +17 -3
- package/dist/agents/ollama/OllamaAgent.js +69 -19
- package/dist/agents/openai/OpenAiAgent.d.ts +4 -3
- package/dist/agents/openai/OpenAiAgent.js +52 -17
- package/dist/agents/openai-compatible/OpenAICompatibleAgent.d.ts +4 -3
- package/dist/agents/openai-compatible/OpenAICompatibleAgent.js +48 -19
- package/dist/agents/openrouter/OpenRouterAgent.d.ts +234 -0
- package/dist/agents/openrouter/OpenRouterAgent.js +711 -0
- package/dist/agents/openrouter/types.d.ts +164 -0
- package/dist/agents/openrouter/types.js +15 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/history/transformers.d.ts +80 -0
- package/dist/history/transformers.js +156 -1
- package/dist/history/types.d.ts +22 -2
- package/dist/history/types.js +13 -2
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/mcp/MCPClient.js +4 -2
- package/dist/openrouter.d.ts +6 -0
- package/dist/openrouter.js +24 -0
- package/dist/tools/Tool.d.ts +13 -3
- package/dist/tools/Tool.js +18 -4
- package/dist/viz/types.d.ts +1 -1
- package/package.json +10 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { BaseAgent, BaseAgentConfig, ModelInfo, TokenUsage } from "../BaseAgent";
|
|
2
|
+
import { OpenRouterSpecificConfig } from "../AgentConfig";
|
|
3
|
+
import { ExecuteOptions } from "../cancellation";
|
|
4
|
+
import { History, MessageContent } from "../../history/History";
|
|
5
|
+
import type { OpenRouterGenerationInfo } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* A single chunk yielded by `executeStream()`.
|
|
8
|
+
* - `"text"` — visible output token
|
|
9
|
+
* - `"reasoning"` — internal reasoning token
|
|
10
|
+
*/
|
|
11
|
+
export type StreamChunk = {
|
|
12
|
+
type: "text" | "reasoning";
|
|
13
|
+
content: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Build a `beforeRequest` hook that adds custom headers to every request.
|
|
17
|
+
*
|
|
18
|
+
* `@openrouter/sdk` has no `defaultHeaders` option like the Anthropic and
|
|
19
|
+
* OpenAI clients, so headers are injected at the HTTP layer instead. They
|
|
20
|
+
* overwrite headers the SDK already set, so that `defaultHeaders` means the
|
|
21
|
+
* same thing on every provider — see `CommonAgentConfig.defaultHeaders`.
|
|
22
|
+
*
|
|
23
|
+
* `httpReferer` / `appTitle` are a separate OpenRouter attribution path
|
|
24
|
+
* (`HTTP-Referer` / `X-Title`). They are not a substitute for tracing or
|
|
25
|
+
* gateway headers.
|
|
26
|
+
*/
|
|
27
|
+
export declare function defaultHeadersHook(headers: Record<string, string>): (request: Request) => void;
|
|
28
|
+
export type OpenRouterConfig = BaseAgentConfig & OpenRouterSpecificConfig & {
|
|
29
|
+
model?: string;
|
|
30
|
+
maxTokens?: number;
|
|
31
|
+
/** Override the API base URL — for an OpenRouter-compatible gateway. */
|
|
32
|
+
baseURL?: string;
|
|
33
|
+
/** Vendor-nested form of the same options, for `AgentConfig` compatibility. */
|
|
34
|
+
vendorConfig?: {
|
|
35
|
+
openrouter?: OpenRouterSpecificConfig;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* A model as OpenRouter's `/models` endpoint reports it.
|
|
40
|
+
*
|
|
41
|
+
* Richer than any single upstream provider's listing: OpenRouter publishes
|
|
42
|
+
* per-token pricing, the context window, and the exact parameter names each
|
|
43
|
+
* model accepts.
|
|
44
|
+
*/
|
|
45
|
+
export type OpenRouterModelCard = {
|
|
46
|
+
id: string;
|
|
47
|
+
canonicalSlug?: string | null;
|
|
48
|
+
name?: string;
|
|
49
|
+
created?: number;
|
|
50
|
+
description?: string;
|
|
51
|
+
contextLength?: number | null;
|
|
52
|
+
architecture?: {
|
|
53
|
+
inputModalities?: string[];
|
|
54
|
+
outputModalities?: string[];
|
|
55
|
+
tokenizer?: string;
|
|
56
|
+
instructType?: string | null;
|
|
57
|
+
};
|
|
58
|
+
pricing?: {
|
|
59
|
+
prompt?: string;
|
|
60
|
+
completion?: string;
|
|
61
|
+
request?: string;
|
|
62
|
+
image?: string;
|
|
63
|
+
webSearch?: string;
|
|
64
|
+
internalReasoning?: string;
|
|
65
|
+
};
|
|
66
|
+
topProvider?: {
|
|
67
|
+
contextLength?: number | null;
|
|
68
|
+
maxCompletionTokens?: number | null;
|
|
69
|
+
isModerated?: boolean;
|
|
70
|
+
};
|
|
71
|
+
/** Parameter names the model accepts, e.g. `"tools"`, `"reasoning"`, `"seed"`. */
|
|
72
|
+
supportedParameters?: string[] | null;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Agent backed by [OpenRouter](https://openrouter.ai) via the official
|
|
77
|
+
* `@openrouter/sdk`, giving one API key access to models from every provider it
|
|
78
|
+
* fronts.
|
|
79
|
+
*
|
|
80
|
+
* Beyond what an OpenAI-compatible endpoint offers, this agent exposes
|
|
81
|
+
* OpenRouter's routing controls — `models` fallbacks, `provider` preferences —
|
|
82
|
+
* reports the credit cost of each run on {@link lastGeneration}, and round-trips
|
|
83
|
+
* `reasoning_details` so multi-turn tool calls work on reasoning models whose
|
|
84
|
+
* thinking blocks are signed.
|
|
85
|
+
*
|
|
86
|
+
* @requires @openrouter/sdk - Install as a peer dependency:
|
|
87
|
+
* ```bash
|
|
88
|
+
* npm install @openrouter/sdk
|
|
89
|
+
* ```
|
|
90
|
+
* The SDK is ESM-only, so it is loaded through a dynamic import. On CommonJS
|
|
91
|
+
* that needs Node 20.19+ or 22.12+, where `require()` of an ES module works.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const agent = new OpenRouterAgent({
|
|
96
|
+
* id: "router",
|
|
97
|
+
* name: "Router",
|
|
98
|
+
* description: "Answers questions",
|
|
99
|
+
* apiKey: process.env.OPENROUTER_API_KEY!,
|
|
100
|
+
* model: "anthropic/claude-opus-4-20250514",
|
|
101
|
+
* models: ["openai/gpt-5.6"], // used if the primary is rate limited
|
|
102
|
+
* provider: { sort: "throughput" },
|
|
103
|
+
* });
|
|
104
|
+
*
|
|
105
|
+
* const answer = await agent.execute("Explain recursion");
|
|
106
|
+
* console.log(agent.lastGeneration?.cost, "credits");
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare class OpenRouterAgent extends BaseAgent {
|
|
110
|
+
protected config: OpenRouterConfig;
|
|
111
|
+
/**
|
|
112
|
+
* Cost and routing facts for the most recent `execute()` / `executeStream()`.
|
|
113
|
+
* Reset at the start of each run, alongside `lastTokenUsage`.
|
|
114
|
+
*/
|
|
115
|
+
lastGeneration?: OpenRouterGenerationInfo;
|
|
116
|
+
private clientPromise?;
|
|
117
|
+
private vizEventId?;
|
|
118
|
+
private currentToolCallCount;
|
|
119
|
+
constructor(config: OpenRouterConfig, history?: History);
|
|
120
|
+
/**
|
|
121
|
+
* Load `@openrouter/sdk` and construct the client, once per agent.
|
|
122
|
+
*
|
|
123
|
+
* The specifier goes through a variable so TypeScript does not resolve it at
|
|
124
|
+
* build time, which keeps the dependency genuinely optional — the same
|
|
125
|
+
* approach `MCPClient` uses. The promise is memoized including its rejection,
|
|
126
|
+
* so a missing package reports the install hint on every call rather than
|
|
127
|
+
* retrying the import.
|
|
128
|
+
*/
|
|
129
|
+
private getClient;
|
|
130
|
+
private createClient;
|
|
131
|
+
protected getToolDefinitions(): Array<Record<string, unknown>>;
|
|
132
|
+
protected process(_input: string): Promise<string>;
|
|
133
|
+
/**
|
|
134
|
+
* List the models OpenRouter offers, following pagination to the end.
|
|
135
|
+
*
|
|
136
|
+
* Fills `contextLength`, `maxOutputTokens` and `capabilities` from
|
|
137
|
+
* OpenRouter's own metadata: `supported_parameters` says whether a model takes
|
|
138
|
+
* `tools` and `reasoning`, and `architecture.input_modalities` whether it
|
|
139
|
+
* accepts images. Per-token pricing is on `raw.pricing`.
|
|
140
|
+
*/
|
|
141
|
+
listModels(): Promise<ModelInfo<OpenRouterModelCard>[]>;
|
|
142
|
+
execute(input: string | MessageContent[], options?: ExecuteOptions): Promise<string>;
|
|
143
|
+
/**
|
|
144
|
+
* Stream a response as an async generator of {@link StreamChunk} objects.
|
|
145
|
+
*
|
|
146
|
+
* Tool calls are executed transparently — the generator keeps streaming after
|
|
147
|
+
* each tool-call round trip.
|
|
148
|
+
*/
|
|
149
|
+
executeStream(input: string | MessageContent[], options?: ExecuteOptions): AsyncGenerator<StreamChunk>;
|
|
150
|
+
/** Shared setup for `execute()` and `executeStream()`. */
|
|
151
|
+
private beginRun;
|
|
152
|
+
/**
|
|
153
|
+
* Map whatever a run threw onto this library's error types, emit it, and close
|
|
154
|
+
* any open visualization event. Returns the error for the caller to throw.
|
|
155
|
+
*/
|
|
156
|
+
private failRun;
|
|
157
|
+
/**
|
|
158
|
+
* Turn an `@openrouter/sdk` error into an {@link AgentError}.
|
|
159
|
+
*
|
|
160
|
+
* The SDK throws one class per status code, all extending `OpenRouterError`
|
|
161
|
+
* with `statusCode`, `headers` and `body`. Rather than importing those classes
|
|
162
|
+
* — which would make the optional peer dependency mandatory — this reads the
|
|
163
|
+
* shape structurally.
|
|
164
|
+
*/
|
|
165
|
+
private mapProviderError;
|
|
166
|
+
/**
|
|
167
|
+
* Build a {@link RateLimitError} from a 429, lifting OpenRouter's rate-limit
|
|
168
|
+
* headers onto it. They are only present on OpenRouter's own platform limits —
|
|
169
|
+
* a 429 passed through from an upstream provider carries neither, which is why
|
|
170
|
+
* every field is optional.
|
|
171
|
+
*/
|
|
172
|
+
private rateLimitError;
|
|
173
|
+
private closeViz;
|
|
174
|
+
/**
|
|
175
|
+
* Wrap a `ChatRequest` in the envelope `@openrouter/sdk` `chat.send()` expects.
|
|
176
|
+
* Passing the body bare fails Speakeasy validation (`Input validation failed`).
|
|
177
|
+
*/
|
|
178
|
+
private sendRequest;
|
|
179
|
+
/** The `ChatRequest` body, identical for the streaming and buffered paths. */
|
|
180
|
+
private buildRequest;
|
|
181
|
+
/**
|
|
182
|
+
* Per-request options: the cancellation signal plus the retry policy.
|
|
183
|
+
*
|
|
184
|
+
* `retryCodes` has to be passed on every call — the SDK reads it only from the
|
|
185
|
+
* call options, never from the client's, so setting it once at construction
|
|
186
|
+
* would silently do nothing.
|
|
187
|
+
*/
|
|
188
|
+
private requestOptions;
|
|
189
|
+
private callProvider;
|
|
190
|
+
protected handleResponse(response: any, options?: ExecuteOptions): Promise<string>;
|
|
191
|
+
private streamTurn;
|
|
192
|
+
private handleToolCalls;
|
|
193
|
+
/**
|
|
194
|
+
* Fold one API call's cost and routing facts into {@link lastGeneration}.
|
|
195
|
+
* Cost is summed — a tool loop bills once per hop — while the id and model
|
|
196
|
+
* describe the most recent call.
|
|
197
|
+
*/
|
|
198
|
+
private recordGeneration;
|
|
199
|
+
protected parseUsage(response: unknown): TokenUsage;
|
|
200
|
+
private parseUsageObject;
|
|
201
|
+
private completeViz;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Milliseconds to wait from a `Retry-After` header.
|
|
205
|
+
*
|
|
206
|
+
* RFC 9110 allows two forms — delay-seconds (`120`) and an HTTP-date
|
|
207
|
+
* (`Wed, 21 Oct 2026 07:28:00 GMT`). OpenRouter sends the first; the second is
|
|
208
|
+
* accepted because it is legal and cheap to support. A date already in the past
|
|
209
|
+
* yields `0` rather than a negative wait.
|
|
210
|
+
*
|
|
211
|
+
* @returns The delay in milliseconds, or `undefined` when the header is absent
|
|
212
|
+
* or unparseable.
|
|
213
|
+
*/
|
|
214
|
+
export declare function parseRetryAfter(raw: string | null | undefined): number | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* The instant an `X-RateLimit-Reset` header points at.
|
|
217
|
+
*
|
|
218
|
+
* OpenRouter documents that the header exists but not what is in it, and the
|
|
219
|
+
* three encodings in common use across APIs are indistinguishable by type — so
|
|
220
|
+
* they are told apart by magnitude, taking "the answer is somewhere near now" as
|
|
221
|
+
* the tiebreaker:
|
|
222
|
+
*
|
|
223
|
+
* - below `10^9` — a duration in seconds from now (a literal epoch would be
|
|
224
|
+
* before 2001, which no live API means)
|
|
225
|
+
* - below `10^11` — Unix **seconds** (`10^11` seconds is the year 5138, so
|
|
226
|
+
* anything under it is a plausible timestamp and anything over it is not)
|
|
227
|
+
* - otherwise — Unix **milliseconds**
|
|
228
|
+
*
|
|
229
|
+
* Returns `undefined` for a missing or non-finite value, so callers see "not
|
|
230
|
+
* reported" rather than a date in 1970. Prefer
|
|
231
|
+
* {@link RateLimitError.retryAfterMs} when both are present: it is unambiguous.
|
|
232
|
+
*/
|
|
233
|
+
export declare function parseResetAt(value: number | undefined): Date | undefined;
|
|
234
|
+
//# sourceMappingURL=OpenRouterAgent.d.ts.map
|