@falai/agent 0.1.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 +516 -0
- package/dist/constants/index.d.ts +5 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +5 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/core/Agent.d.ts +98 -0
- package/dist/core/Agent.d.ts.map +1 -0
- package/dist/core/Agent.js +248 -0
- package/dist/core/Agent.js.map +1 -0
- package/dist/core/DomainRegistry.d.ts +26 -0
- package/dist/core/DomainRegistry.d.ts.map +1 -0
- package/dist/core/DomainRegistry.js +41 -0
- package/dist/core/DomainRegistry.js.map +1 -0
- package/dist/core/Events.d.ts +19 -0
- package/dist/core/Events.d.ts.map +1 -0
- package/dist/core/Events.js +79 -0
- package/dist/core/Events.js.map +1 -0
- package/dist/core/Observation.d.ts +24 -0
- package/dist/core/Observation.d.ts.map +1 -0
- package/dist/core/Observation.js +35 -0
- package/dist/core/Observation.js.map +1 -0
- package/dist/core/PromptBuilder.d.ts +121 -0
- package/dist/core/PromptBuilder.d.ts.map +1 -0
- package/dist/core/PromptBuilder.js +339 -0
- package/dist/core/PromptBuilder.js.map +1 -0
- package/dist/core/Route.d.ts +46 -0
- package/dist/core/Route.d.ts.map +1 -0
- package/dist/core/Route.js +113 -0
- package/dist/core/Route.js.map +1 -0
- package/dist/core/State.d.ts +50 -0
- package/dist/core/State.d.ts.map +1 -0
- package/dist/core/State.js +110 -0
- package/dist/core/State.js.map +1 -0
- package/dist/core/Tool.d.ts +31 -0
- package/dist/core/Tool.d.ts.map +1 -0
- package/dist/core/Tool.js +33 -0
- package/dist/core/Tool.js.map +1 -0
- package/dist/core/Transition.d.ts +32 -0
- package/dist/core/Transition.d.ts.map +1 -0
- package/dist/core/Transition.js +59 -0
- package/dist/core/Transition.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/GeminiProvider.d.ts +40 -0
- package/dist/providers/GeminiProvider.d.ts.map +1 -0
- package/dist/providers/GeminiProvider.js +126 -0
- package/dist/providers/GeminiProvider.js.map +1 -0
- package/dist/providers/OpenAIProvider.d.ts +42 -0
- package/dist/providers/OpenAIProvider.d.ts.map +1 -0
- package/dist/providers/OpenAIProvider.js +164 -0
- package/dist/providers/OpenAIProvider.js.map +1 -0
- package/dist/providers/OpenRouterProvider.d.ts +46 -0
- package/dist/providers/OpenRouterProvider.d.ts.map +1 -0
- package/dist/providers/OpenRouterProvider.js +171 -0
- package/dist/providers/OpenRouterProvider.js.map +1 -0
- package/dist/types/agent.d.ts +105 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +18 -0
- package/dist/types/agent.js.map +1 -0
- package/dist/types/ai.d.ts +78 -0
- package/dist/types/ai.d.ts.map +1 -0
- package/dist/types/ai.js +5 -0
- package/dist/types/ai.js.map +1 -0
- package/dist/types/history.d.ts +112 -0
- package/dist/types/history.d.ts.map +1 -0
- package/dist/types/history.js +34 -0
- package/dist/types/history.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/observation.d.ts +25 -0
- package/dist/types/observation.d.ts.map +1 -0
- package/dist/types/observation.js +5 -0
- package/dist/types/observation.js.map +1 -0
- package/dist/types/prompt.d.ts +46 -0
- package/dist/types/prompt.d.ts.map +1 -0
- package/dist/types/prompt.js +16 -0
- package/dist/types/prompt.js.map +1 -0
- package/dist/types/route.d.ts +59 -0
- package/dist/types/route.d.ts.map +1 -0
- package/dist/types/route.js +5 -0
- package/dist/types/route.js.map +1 -0
- package/dist/types/tool.d.ts +46 -0
- package/dist/types/tool.d.ts.map +1 -0
- package/dist/types/tool.js +5 -0
- package/dist/types/tool.js.map +1 -0
- package/dist/utils/retry.d.ts +13 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +70 -0
- package/dist/utils/retry.js.map +1 -0
- package/docs/API_REFERENCE.md +517 -0
- package/docs/CONSTRUCTOR_OPTIONS.md +256 -0
- package/docs/CONTRIBUTING.md +481 -0
- package/docs/GETTING_STARTED.md +328 -0
- package/docs/PROVIDERS.md +472 -0
- package/docs/PUBLISHING.md +174 -0
- package/docs/README.md +68 -0
- package/docs/STRUCTURE.md +32 -0
- package/examples/declarative-agent.ts +217 -0
- package/examples/healthcare-agent.ts +283 -0
- package/examples/openai-agent.ts +167 -0
- package/examples/travel-agent.ts +342 -0
- package/package.json +73 -0
- package/src/constants/index.ts +5 -0
- package/src/core/Agent.ts +307 -0
- package/src/core/DomainRegistry.ts +50 -0
- package/src/core/Events.ts +101 -0
- package/src/core/Observation.ts +46 -0
- package/src/core/PromptBuilder.ts +511 -0
- package/src/core/Route.ts +136 -0
- package/src/core/State.ts +153 -0
- package/src/core/Tool.ts +54 -0
- package/src/core/Transition.ts +66 -0
- package/src/index.ts +83 -0
- package/src/providers/GeminiProvider.ts +220 -0
- package/src/providers/OpenAIProvider.ts +272 -0
- package/src/providers/OpenRouterProvider.ts +282 -0
- package/src/types/agent.ts +112 -0
- package/src/types/ai.ts +85 -0
- package/src/types/history.ts +125 -0
- package/src/types/index.ts +56 -0
- package/src/types/observation.ts +27 -0
- package/src/types/prompt.ts +49 -0
- package/src/types/route.ts +68 -0
- package/src/types/tool.ts +53 -0
- package/src/utils/retry.ts +96 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI provider implementation with retry and backup models
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import OpenAI from "openai";
|
|
6
|
+
import type { ChatCompletionCreateParamsNonStreaming } from "openai/resources/chat/completions";
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
AiProvider,
|
|
10
|
+
GenerateMessageInput,
|
|
11
|
+
GenerateMessageOutput,
|
|
12
|
+
} from "@/types/ai";
|
|
13
|
+
import { withTimeoutAndRetry } from "@/utils/retry";
|
|
14
|
+
|
|
15
|
+
const DEFAULT_RETRY_CONFIG = {
|
|
16
|
+
timeout: 60000,
|
|
17
|
+
retries: 3,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Configuration options for OpenAI provider
|
|
22
|
+
* Uses types from openai package
|
|
23
|
+
*/
|
|
24
|
+
export interface OpenAIProviderOptions {
|
|
25
|
+
/** OpenAI API key */
|
|
26
|
+
apiKey: string;
|
|
27
|
+
/** Organization ID (optional) */
|
|
28
|
+
organization?: string;
|
|
29
|
+
/** Model to use (required) - e.g., "gpt-5", "gpt-5-mini" */
|
|
30
|
+
model: string;
|
|
31
|
+
/** Backup models to try if primary fails (default: []) */
|
|
32
|
+
backupModels?: string[];
|
|
33
|
+
/** Default parameters - uses ChatCompletionCreateParamsNonStreaming from openai package */
|
|
34
|
+
config?: Partial<
|
|
35
|
+
Omit<ChatCompletionCreateParamsNonStreaming, "model" | "messages">
|
|
36
|
+
>;
|
|
37
|
+
/** Retry configuration */
|
|
38
|
+
retryConfig?: {
|
|
39
|
+
timeout?: number;
|
|
40
|
+
retries?: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Type guard for errors with status/code properties
|
|
46
|
+
*/
|
|
47
|
+
interface ErrorWithStatus {
|
|
48
|
+
status?: number;
|
|
49
|
+
code?: string;
|
|
50
|
+
message?: string;
|
|
51
|
+
type?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Type guard to check if error is ErrorWithStatus
|
|
56
|
+
*/
|
|
57
|
+
function isErrorWithStatus(error: unknown): error is ErrorWithStatus {
|
|
58
|
+
return (
|
|
59
|
+
typeof error === "object" &&
|
|
60
|
+
error !== null &&
|
|
61
|
+
("status" in error || "code" in error || "message" in error)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Safely extract error message
|
|
67
|
+
*/
|
|
68
|
+
function getErrorMessage(error: unknown): string {
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
return error.message;
|
|
71
|
+
}
|
|
72
|
+
if (isErrorWithStatus(error) && error.message) {
|
|
73
|
+
return error.message;
|
|
74
|
+
}
|
|
75
|
+
return String(error);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Determines if an error should trigger backup model usage
|
|
80
|
+
*/
|
|
81
|
+
const shouldUseBackupModel = (error: unknown): boolean => {
|
|
82
|
+
if (!isErrorWithStatus(error)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Server errors
|
|
87
|
+
if (error.status === 500 || error.status === 503) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Rate limiting
|
|
92
|
+
if (error.status === 429) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Model overloaded or unavailable
|
|
97
|
+
if (error.code === "model_not_found" || error.code === "model_overloaded") {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const message = getErrorMessage(error);
|
|
102
|
+
if (
|
|
103
|
+
message.includes("overloaded") ||
|
|
104
|
+
message.includes("unavailable") ||
|
|
105
|
+
message.includes("internal error") ||
|
|
106
|
+
message.includes("Internal error")
|
|
107
|
+
) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* OpenAI provider implementation with backup models and retry logic
|
|
116
|
+
*/
|
|
117
|
+
export class OpenAIProvider implements AiProvider {
|
|
118
|
+
public readonly name = "openai";
|
|
119
|
+
private client: OpenAI;
|
|
120
|
+
private primaryModel: string;
|
|
121
|
+
private backupModels: string[];
|
|
122
|
+
private config?: Partial<
|
|
123
|
+
Omit<ChatCompletionCreateParamsNonStreaming, "model" | "messages">
|
|
124
|
+
>;
|
|
125
|
+
private retryConfig: { timeout: number; retries: number };
|
|
126
|
+
|
|
127
|
+
constructor(options: OpenAIProviderOptions) {
|
|
128
|
+
const {
|
|
129
|
+
apiKey,
|
|
130
|
+
organization,
|
|
131
|
+
model,
|
|
132
|
+
backupModels = [],
|
|
133
|
+
config,
|
|
134
|
+
retryConfig,
|
|
135
|
+
} = options;
|
|
136
|
+
|
|
137
|
+
if (!apiKey) {
|
|
138
|
+
throw new Error("OpenAI API key is required");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!model) {
|
|
142
|
+
throw new Error("Model is required. Example: 'gpt-5' or 'gpt-5-mini'");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Dynamic import to avoid bundling issues
|
|
146
|
+
|
|
147
|
+
this.client = new OpenAI({
|
|
148
|
+
apiKey,
|
|
149
|
+
organization,
|
|
150
|
+
});
|
|
151
|
+
this.primaryModel = model;
|
|
152
|
+
this.backupModels = backupModels;
|
|
153
|
+
this.config = config;
|
|
154
|
+
this.retryConfig = {
|
|
155
|
+
timeout: retryConfig?.timeout || DEFAULT_RETRY_CONFIG.timeout,
|
|
156
|
+
retries: retryConfig?.retries || DEFAULT_RETRY_CONFIG.retries,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async generateMessage<TContext = unknown>(
|
|
161
|
+
input: GenerateMessageInput<TContext>
|
|
162
|
+
): Promise<GenerateMessageOutput> {
|
|
163
|
+
return this.generateWithBackup(input);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async generateWithBackup<TContext = unknown>(
|
|
167
|
+
input: GenerateMessageInput<TContext>
|
|
168
|
+
): Promise<GenerateMessageOutput> {
|
|
169
|
+
// Try primary model first
|
|
170
|
+
try {
|
|
171
|
+
return await this.generateWithModel(this.primaryModel, input);
|
|
172
|
+
} catch (primaryError: unknown) {
|
|
173
|
+
const primaryErrMsg = getErrorMessage(primaryError);
|
|
174
|
+
console.warn(
|
|
175
|
+
`[OPENAI] Primary model ${this.primaryModel} failed: ${primaryErrMsg}`
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (!shouldUseBackupModel(primaryError)) {
|
|
179
|
+
throw primaryError;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
console.log(`[OPENAI] Trying backup models`);
|
|
183
|
+
|
|
184
|
+
let lastBackupError: unknown = primaryError;
|
|
185
|
+
|
|
186
|
+
for (let i = 0; i < this.backupModels.length; i++) {
|
|
187
|
+
const backupModel = this.backupModels[i];
|
|
188
|
+
console.log(
|
|
189
|
+
`[OPENAI] Trying backup model ${i + 1}/${
|
|
190
|
+
this.backupModels.length
|
|
191
|
+
}: ${backupModel}`
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
try {
|
|
195
|
+
const result = await this.generateWithModel(backupModel, input);
|
|
196
|
+
console.log(`[OPENAI] Backup model ${backupModel} succeeded`);
|
|
197
|
+
return result;
|
|
198
|
+
} catch (backupError: unknown) {
|
|
199
|
+
const backupErrMsg = getErrorMessage(backupError);
|
|
200
|
+
console.warn(
|
|
201
|
+
`[OPENAI] Backup model ${backupModel} failed: ${backupErrMsg}`
|
|
202
|
+
);
|
|
203
|
+
lastBackupError = backupError;
|
|
204
|
+
|
|
205
|
+
if (
|
|
206
|
+
!shouldUseBackupModel(backupError) &&
|
|
207
|
+
i < this.backupModels.length - 1
|
|
208
|
+
) {
|
|
209
|
+
console.log(
|
|
210
|
+
`[OPENAI] Backup model error doesn't qualify for further attempts`
|
|
211
|
+
);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const lastBackupErrMsg = getErrorMessage(lastBackupError);
|
|
218
|
+
console.error(
|
|
219
|
+
`[OPENAI] All models failed. Primary: ${primaryErrMsg}, Last backup: ${lastBackupErrMsg}`
|
|
220
|
+
);
|
|
221
|
+
throw lastBackupError;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private async generateWithModel<TContext = unknown>(
|
|
226
|
+
model: string,
|
|
227
|
+
input: GenerateMessageInput<TContext>
|
|
228
|
+
): Promise<GenerateMessageOutput> {
|
|
229
|
+
const operation = async (): Promise<GenerateMessageOutput> => {
|
|
230
|
+
const params: ChatCompletionCreateParamsNonStreaming = {
|
|
231
|
+
model,
|
|
232
|
+
messages: [
|
|
233
|
+
{
|
|
234
|
+
role: "user",
|
|
235
|
+
content: input.prompt,
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
...this.config,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// Override with input parameters if provided
|
|
242
|
+
if (input.parameters?.maxOutputTokens !== undefined) {
|
|
243
|
+
params.max_tokens = input.parameters.maxOutputTokens;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const response = await this.client.chat.completions.create(params);
|
|
247
|
+
|
|
248
|
+
const message = response.choices[0]?.message?.content;
|
|
249
|
+
if (!message) {
|
|
250
|
+
throw new Error("No response from OpenAI");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
message,
|
|
255
|
+
metadata: {
|
|
256
|
+
model: response.model,
|
|
257
|
+
finishReason: response.choices[0]?.finish_reason,
|
|
258
|
+
tokensUsed: response.usage?.total_tokens,
|
|
259
|
+
promptTokens: response.usage?.prompt_tokens,
|
|
260
|
+
completionTokens: response.usage?.completion_tokens,
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
return withTimeoutAndRetry(
|
|
266
|
+
operation,
|
|
267
|
+
this.retryConfig.timeout,
|
|
268
|
+
this.retryConfig.retries,
|
|
269
|
+
`OpenAI ${model}`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenRouter provider implementation
|
|
3
|
+
* OpenRouter provides access to multiple AI models through a unified OpenAI-compatible API
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import OpenAI from "openai";
|
|
7
|
+
import type { ChatCompletionCreateParamsNonStreaming } from "openai/resources/chat/completions";
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
AiProvider,
|
|
11
|
+
GenerateMessageInput,
|
|
12
|
+
GenerateMessageOutput,
|
|
13
|
+
} from "@/types/ai";
|
|
14
|
+
import { withTimeoutAndRetry } from "@/utils/retry";
|
|
15
|
+
|
|
16
|
+
const DEFAULT_RETRY_CONFIG = {
|
|
17
|
+
timeout: 60000,
|
|
18
|
+
retries: 3,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Configuration options for OpenRouter provider
|
|
23
|
+
* Uses types from openai package (OpenRouter is OpenAI-compatible)
|
|
24
|
+
*/
|
|
25
|
+
export interface OpenRouterProviderOptions {
|
|
26
|
+
/** OpenRouter API key */
|
|
27
|
+
apiKey: string;
|
|
28
|
+
/** Model to use (required) - see https://openrouter.ai/models */
|
|
29
|
+
model: string;
|
|
30
|
+
/** Backup models to try if primary fails (default: []) */
|
|
31
|
+
backupModels?: string[];
|
|
32
|
+
/** Optional site URL for OpenRouter rankings */
|
|
33
|
+
siteUrl?: string;
|
|
34
|
+
/** Optional app name for OpenRouter rankings */
|
|
35
|
+
siteName?: string;
|
|
36
|
+
/** Default parameters - uses ChatCompletionCreateParamsNonStreaming from openai package */
|
|
37
|
+
config?: Partial<
|
|
38
|
+
Omit<ChatCompletionCreateParamsNonStreaming, "model" | "messages">
|
|
39
|
+
>;
|
|
40
|
+
/** Retry configuration */
|
|
41
|
+
retryConfig?: {
|
|
42
|
+
timeout?: number;
|
|
43
|
+
retries?: number;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Type guard for errors with status/code properties
|
|
49
|
+
*/
|
|
50
|
+
interface ErrorWithStatus {
|
|
51
|
+
status?: number;
|
|
52
|
+
code?: string;
|
|
53
|
+
message?: string;
|
|
54
|
+
type?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Type guard to check if error is ErrorWithStatus
|
|
59
|
+
*/
|
|
60
|
+
function isErrorWithStatus(error: unknown): error is ErrorWithStatus {
|
|
61
|
+
return (
|
|
62
|
+
typeof error === "object" &&
|
|
63
|
+
error !== null &&
|
|
64
|
+
("status" in error || "code" in error || "message" in error)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Safely extract error message
|
|
70
|
+
*/
|
|
71
|
+
function getErrorMessage(error: unknown): string {
|
|
72
|
+
if (error instanceof Error) {
|
|
73
|
+
return error.message;
|
|
74
|
+
}
|
|
75
|
+
if (isErrorWithStatus(error) && error.message) {
|
|
76
|
+
return error.message;
|
|
77
|
+
}
|
|
78
|
+
return String(error);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Determines if an error should trigger backup model usage
|
|
83
|
+
*/
|
|
84
|
+
const shouldUseBackupModel = (error: unknown): boolean => {
|
|
85
|
+
if (!isErrorWithStatus(error)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Server errors
|
|
90
|
+
if (error.status === 500 || error.status === 503) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Rate limiting
|
|
95
|
+
if (error.status === 429) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Model not available or overloaded
|
|
100
|
+
if (error.code === "model_not_found" || error.code === "model_overloaded") {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const message = getErrorMessage(error);
|
|
105
|
+
if (
|
|
106
|
+
message.includes("overloaded") ||
|
|
107
|
+
message.includes("unavailable") ||
|
|
108
|
+
message.includes("internal error") ||
|
|
109
|
+
message.includes("Internal error") ||
|
|
110
|
+
message.includes("capacity")
|
|
111
|
+
) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* OpenRouter provider implementation
|
|
120
|
+
* Provides access to multiple AI models through OpenRouter's unified API
|
|
121
|
+
*/
|
|
122
|
+
export class OpenRouterProvider implements AiProvider {
|
|
123
|
+
public readonly name = "openrouter";
|
|
124
|
+
private client: OpenAI;
|
|
125
|
+
private primaryModel: string;
|
|
126
|
+
private backupModels: string[];
|
|
127
|
+
private config?: Partial<
|
|
128
|
+
Omit<ChatCompletionCreateParamsNonStreaming, "model" | "messages">
|
|
129
|
+
>;
|
|
130
|
+
private retryConfig: { timeout: number; retries: number };
|
|
131
|
+
|
|
132
|
+
constructor(options: OpenRouterProviderOptions) {
|
|
133
|
+
const {
|
|
134
|
+
apiKey,
|
|
135
|
+
model,
|
|
136
|
+
backupModels = [],
|
|
137
|
+
siteUrl,
|
|
138
|
+
siteName,
|
|
139
|
+
config,
|
|
140
|
+
retryConfig,
|
|
141
|
+
} = options;
|
|
142
|
+
|
|
143
|
+
if (!apiKey) {
|
|
144
|
+
throw new Error("OpenRouter API key is required");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!model) {
|
|
148
|
+
throw new Error("Model is required. See https://openrouter.ai/models");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Initialize OpenAI client with OpenRouter base URL
|
|
152
|
+
this.client = new OpenAI({
|
|
153
|
+
apiKey,
|
|
154
|
+
baseURL: "https://openrouter.ai/api/v1",
|
|
155
|
+
defaultHeaders: {
|
|
156
|
+
"HTTP-Referer": siteUrl || "",
|
|
157
|
+
"X-Title": siteName || "",
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
this.primaryModel = model;
|
|
162
|
+
this.backupModels = backupModels;
|
|
163
|
+
this.config = config;
|
|
164
|
+
this.retryConfig = {
|
|
165
|
+
timeout: retryConfig?.timeout || DEFAULT_RETRY_CONFIG.timeout,
|
|
166
|
+
retries: retryConfig?.retries || DEFAULT_RETRY_CONFIG.retries,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async generateMessage<TContext = unknown>(
|
|
171
|
+
input: GenerateMessageInput<TContext>
|
|
172
|
+
): Promise<GenerateMessageOutput> {
|
|
173
|
+
return this.generateWithBackup(input);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private async generateWithBackup<TContext = unknown>(
|
|
177
|
+
input: GenerateMessageInput<TContext>
|
|
178
|
+
): Promise<GenerateMessageOutput> {
|
|
179
|
+
// Try primary model first
|
|
180
|
+
try {
|
|
181
|
+
return await this.generateWithModel(this.primaryModel, input);
|
|
182
|
+
} catch (primaryError: unknown) {
|
|
183
|
+
const primaryErrMsg = getErrorMessage(primaryError);
|
|
184
|
+
console.warn(
|
|
185
|
+
`[OPENROUTER] Primary model ${this.primaryModel} failed: ${primaryErrMsg}`
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
if (!shouldUseBackupModel(primaryError)) {
|
|
189
|
+
throw primaryError;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
console.log(`[OPENROUTER] Trying backup models`);
|
|
193
|
+
|
|
194
|
+
let lastBackupError: unknown = primaryError;
|
|
195
|
+
|
|
196
|
+
for (let i = 0; i < this.backupModels.length; i++) {
|
|
197
|
+
const backupModel = this.backupModels[i];
|
|
198
|
+
console.log(
|
|
199
|
+
`[OPENROUTER] Trying backup model ${i + 1}/${
|
|
200
|
+
this.backupModels.length
|
|
201
|
+
}: ${backupModel}`
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const result = await this.generateWithModel(backupModel, input);
|
|
206
|
+
console.log(`[OPENROUTER] Backup model ${backupModel} succeeded`);
|
|
207
|
+
return result;
|
|
208
|
+
} catch (backupError: unknown) {
|
|
209
|
+
const backupErrMsg = getErrorMessage(backupError);
|
|
210
|
+
console.warn(
|
|
211
|
+
`[OPENROUTER] Backup model ${backupModel} failed: ${backupErrMsg}`
|
|
212
|
+
);
|
|
213
|
+
lastBackupError = backupError;
|
|
214
|
+
|
|
215
|
+
if (
|
|
216
|
+
!shouldUseBackupModel(backupError) &&
|
|
217
|
+
i < this.backupModels.length - 1
|
|
218
|
+
) {
|
|
219
|
+
console.log(
|
|
220
|
+
`[OPENROUTER] Backup model error doesn't qualify for further attempts`
|
|
221
|
+
);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const lastBackupErrMsg = getErrorMessage(lastBackupError);
|
|
228
|
+
console.error(
|
|
229
|
+
`[OPENROUTER] All models failed. Primary: ${primaryErrMsg}, Last backup: ${lastBackupErrMsg}`
|
|
230
|
+
);
|
|
231
|
+
throw lastBackupError;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private async generateWithModel<TContext = unknown>(
|
|
236
|
+
model: string,
|
|
237
|
+
input: GenerateMessageInput<TContext>
|
|
238
|
+
): Promise<GenerateMessageOutput> {
|
|
239
|
+
const operation = async (): Promise<GenerateMessageOutput> => {
|
|
240
|
+
const params: ChatCompletionCreateParamsNonStreaming = {
|
|
241
|
+
model,
|
|
242
|
+
messages: [
|
|
243
|
+
{
|
|
244
|
+
role: "user",
|
|
245
|
+
content: input.prompt,
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
...this.config,
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Override with input parameters if provided
|
|
252
|
+
if (input.parameters?.maxOutputTokens !== undefined) {
|
|
253
|
+
params.max_tokens = input.parameters.maxOutputTokens;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const response = await this.client.chat.completions.create(params);
|
|
257
|
+
|
|
258
|
+
const message = response.choices[0]?.message?.content;
|
|
259
|
+
if (!message) {
|
|
260
|
+
throw new Error("No response from OpenRouter");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
message,
|
|
265
|
+
metadata: {
|
|
266
|
+
model: response.model,
|
|
267
|
+
finishReason: response.choices[0]?.finish_reason,
|
|
268
|
+
tokensUsed: response.usage?.total_tokens,
|
|
269
|
+
promptTokens: response.usage?.prompt_tokens,
|
|
270
|
+
completionTokens: response.usage?.completion_tokens,
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
return withTimeoutAndRetry(
|
|
276
|
+
operation,
|
|
277
|
+
this.retryConfig.timeout,
|
|
278
|
+
this.retryConfig.retries,
|
|
279
|
+
`OpenRouter ${model}`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-related type definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { AiProvider } from "./ai";
|
|
6
|
+
import type { ToolRef } from "./tool";
|
|
7
|
+
import type { RouteOptions } from "./route";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Composition mode determines how the agent processes and structures responses
|
|
11
|
+
*/
|
|
12
|
+
export enum CompositionMode {
|
|
13
|
+
/** Fluid, natural conversation without strict structure */
|
|
14
|
+
FLUID = "fluid",
|
|
15
|
+
/** Canned responses with fluid fallback */
|
|
16
|
+
CANNED_FLUID = "canned_fluid",
|
|
17
|
+
/** Composited canned responses */
|
|
18
|
+
CANNED_COMPOSITED = "composited_canned",
|
|
19
|
+
/** Strict canned responses only */
|
|
20
|
+
CANNED_STRICT = "strict_canned",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Forward declare observation types
|
|
25
|
+
*/
|
|
26
|
+
import type { ObservationOptions } from "./observation";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Options for creating an Agent
|
|
30
|
+
*/
|
|
31
|
+
export interface AgentOptions<TContext = unknown> {
|
|
32
|
+
/** Display name of the agent */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Detailed description of the agent's purpose and personality */
|
|
35
|
+
description?: string;
|
|
36
|
+
/** The agent's primary goal or objective */
|
|
37
|
+
goal?: string;
|
|
38
|
+
/** Default context data available to the agent */
|
|
39
|
+
context?: TContext;
|
|
40
|
+
/** AI provider strategy for generating responses */
|
|
41
|
+
ai: AiProvider;
|
|
42
|
+
/** Maximum number of processing iterations per request */
|
|
43
|
+
maxEngineIterations?: number;
|
|
44
|
+
/** Composition mode for response generation */
|
|
45
|
+
compositionMode?: CompositionMode;
|
|
46
|
+
/** Initial terms for domain glossary */
|
|
47
|
+
terms?: Term[];
|
|
48
|
+
/** Initial guidelines for agent behavior */
|
|
49
|
+
guidelines?: Guideline[];
|
|
50
|
+
/** Initial capabilities */
|
|
51
|
+
capabilities?: Capability[];
|
|
52
|
+
/** Initial routes (will be instantiated as Route objects) */
|
|
53
|
+
routes?: RouteOptions[];
|
|
54
|
+
/** Initial observations for disambiguation */
|
|
55
|
+
observations?: ObservationOptions[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A term in the domain glossary
|
|
60
|
+
*/
|
|
61
|
+
export interface Term {
|
|
62
|
+
/** Name of the term */
|
|
63
|
+
name: string;
|
|
64
|
+
/** Description/definition of the term */
|
|
65
|
+
description: string;
|
|
66
|
+
/** Alternative names or synonyms */
|
|
67
|
+
synonyms?: string[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A behavioral guideline for the agent
|
|
72
|
+
*/
|
|
73
|
+
export interface Guideline {
|
|
74
|
+
/** Unique identifier */
|
|
75
|
+
id?: string;
|
|
76
|
+
/** Condition that triggers this guideline (optional for always-active guidelines) */
|
|
77
|
+
condition?: string;
|
|
78
|
+
/** Action the agent should take when the condition is met */
|
|
79
|
+
action: string;
|
|
80
|
+
/** Whether this guideline is currently enabled */
|
|
81
|
+
enabled?: boolean;
|
|
82
|
+
/** Tags for organizing and filtering guidelines */
|
|
83
|
+
tags?: string[];
|
|
84
|
+
/** Tools available when following this guideline */
|
|
85
|
+
tools?: ToolRef<unknown, unknown[], unknown>[];
|
|
86
|
+
/** Additional metadata */
|
|
87
|
+
metadata?: Record<string, unknown>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A capability the agent can perform
|
|
92
|
+
*/
|
|
93
|
+
export interface Capability {
|
|
94
|
+
/** Unique identifier */
|
|
95
|
+
id?: string;
|
|
96
|
+
/** Title of the capability */
|
|
97
|
+
title: string;
|
|
98
|
+
/** Description of what the capability does */
|
|
99
|
+
description: string;
|
|
100
|
+
/** Tools used by this capability */
|
|
101
|
+
tools?: ToolRef<unknown, unknown[], unknown>[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Guideline match with rationale
|
|
106
|
+
*/
|
|
107
|
+
export interface GuidelineMatch {
|
|
108
|
+
/** The matched guideline */
|
|
109
|
+
guideline: Guideline;
|
|
110
|
+
/** Explanation of why this guideline was matched */
|
|
111
|
+
rationale?: string;
|
|
112
|
+
}
|