@~lyre/ai-agents 0.3.0 → 0.5.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/dist/index.js +34 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -46,7 +46,7 @@ var Registry = class {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
// src/run.ts
|
|
49
|
-
import {
|
|
49
|
+
import { generateText, streamText, tool, Output } from "ai";
|
|
50
50
|
|
|
51
51
|
// src/providers.ts
|
|
52
52
|
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
@@ -60,6 +60,12 @@ var PROVIDERS = {
|
|
|
60
60
|
// Grok
|
|
61
61
|
google: (apiKey, id) => createGoogleGenerativeAI({ apiKey })(id)
|
|
62
62
|
};
|
|
63
|
+
var PROVIDER_ENV = {
|
|
64
|
+
anthropic: "ANTHROPIC_API_KEY",
|
|
65
|
+
openai: "OPENAI_API_KEY",
|
|
66
|
+
xai: "XAI_API_KEY",
|
|
67
|
+
google: "GOOGLE_GENERATIVE_AI_API_KEY"
|
|
68
|
+
};
|
|
63
69
|
var SUPPORTED_PROVIDERS = Object.keys(PROVIDERS);
|
|
64
70
|
function resolveModelString(model, apiKey) {
|
|
65
71
|
const slash = model.indexOf("/");
|
|
@@ -67,7 +73,10 @@ function resolveModelString(model, apiKey) {
|
|
|
67
73
|
const provider = model.slice(0, slash);
|
|
68
74
|
const modelId = model.slice(slash + 1);
|
|
69
75
|
const factory = PROVIDERS[provider];
|
|
70
|
-
if (factory
|
|
76
|
+
if (!factory || !modelId) return model;
|
|
77
|
+
const envKey = typeof process !== "undefined" ? process.env?.[PROVIDER_ENV[provider]] : void 0;
|
|
78
|
+
const key = envKey || apiKey;
|
|
79
|
+
if (key) return factory(key, modelId);
|
|
71
80
|
return model;
|
|
72
81
|
}
|
|
73
82
|
|
|
@@ -149,11 +158,21 @@ async function* runStream(registry, params, defaults) {
|
|
|
149
158
|
stopWhen: ({ steps }) => steps.length >= (params.maxSteps ?? 8),
|
|
150
159
|
providerOptions: agent.providerOptions
|
|
151
160
|
});
|
|
161
|
+
const accumulatedUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
162
|
+
const addUsage = (u) => {
|
|
163
|
+
if (!u) return;
|
|
164
|
+
accumulatedUsage.inputTokens += u.inputTokens ?? 0;
|
|
165
|
+
accumulatedUsage.outputTokens += u.outputTokens ?? 0;
|
|
166
|
+
accumulatedUsage.totalTokens += u.totalTokens ?? 0;
|
|
167
|
+
};
|
|
152
168
|
for await (const part of result.fullStream) {
|
|
153
169
|
switch (part.type) {
|
|
154
170
|
case "text-delta":
|
|
155
171
|
yield { type: "text-delta", text: part.text ?? part.delta ?? "" };
|
|
156
172
|
break;
|
|
173
|
+
case "finish-step":
|
|
174
|
+
addUsage(part.usage);
|
|
175
|
+
break;
|
|
157
176
|
case "tool-call":
|
|
158
177
|
yield {
|
|
159
178
|
type: "tool-call",
|
|
@@ -178,17 +197,19 @@ async function* runStream(registry, params, defaults) {
|
|
|
178
197
|
error: part.error
|
|
179
198
|
};
|
|
180
199
|
break;
|
|
181
|
-
case "finish":
|
|
200
|
+
case "finish": {
|
|
201
|
+
const finalUsage = part.totalUsage ?? part.usage;
|
|
182
202
|
yield {
|
|
183
203
|
type: "finish",
|
|
184
204
|
finishReason: part.finishReason,
|
|
185
205
|
usage: {
|
|
186
|
-
inputTokens:
|
|
187
|
-
outputTokens:
|
|
188
|
-
totalTokens:
|
|
206
|
+
inputTokens: finalUsage?.inputTokens ?? accumulatedUsage.inputTokens,
|
|
207
|
+
outputTokens: finalUsage?.outputTokens ?? accumulatedUsage.outputTokens,
|
|
208
|
+
totalTokens: finalUsage?.totalTokens ?? accumulatedUsage.totalTokens
|
|
189
209
|
}
|
|
190
210
|
};
|
|
191
211
|
break;
|
|
212
|
+
}
|
|
192
213
|
case "error":
|
|
193
214
|
yield { type: "error", error: part.error };
|
|
194
215
|
break;
|
|
@@ -198,18 +219,20 @@ async function* runStream(registry, params, defaults) {
|
|
|
198
219
|
async function runObject(registry, params, defaults) {
|
|
199
220
|
const agent = registry.resolveAgent(params.agent);
|
|
200
221
|
const messages = buildMessages(agent, params);
|
|
201
|
-
const result = await
|
|
222
|
+
const result = await generateText({
|
|
202
223
|
model: resolveModel(agent, defaults),
|
|
203
224
|
messages,
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
225
|
+
output: Output.object({
|
|
226
|
+
schema: params.inputSchema,
|
|
227
|
+
name: params.schemaName,
|
|
228
|
+
description: params.schemaDescription
|
|
229
|
+
}),
|
|
207
230
|
temperature: params.temperature ?? agent.temperature,
|
|
208
231
|
maxOutputTokens: params.maxOutputTokens ?? agent.maxOutputTokens,
|
|
209
232
|
providerOptions: agent.providerOptions
|
|
210
233
|
});
|
|
211
234
|
return {
|
|
212
|
-
object: result.
|
|
235
|
+
object: result.output,
|
|
213
236
|
finishReason: result.finishReason ?? "stop",
|
|
214
237
|
usage: {
|
|
215
238
|
inputTokens: result.usage?.inputTokens ?? 0,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@~lyre/ai-agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Multi-provider AI agents SDK for SvelteKit + Node. Thin agent/tool/run/runStream surface over the Vercel AI SDK — OpenAI, Anthropic, Google Gemini, Mistral, Cohere. Zod-typed tools, full streaming event surface, optional HTML sanitizer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|