@~lyre/ai-agents 0.3.0 → 0.4.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.
Files changed (2) hide show
  1. package/dist/index.js +24 -10
  2. 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 { generateObject, generateText, streamText, tool } from "ai";
49
+ import { generateText, streamText, tool, Output } from "ai";
50
50
 
51
51
  // src/providers.ts
52
52
  import { createAnthropic } from "@ai-sdk/anthropic";
@@ -149,11 +149,21 @@ async function* runStream(registry, params, defaults) {
149
149
  stopWhen: ({ steps }) => steps.length >= (params.maxSteps ?? 8),
150
150
  providerOptions: agent.providerOptions
151
151
  });
152
+ const accumulatedUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
153
+ const addUsage = (u) => {
154
+ if (!u) return;
155
+ accumulatedUsage.inputTokens += u.inputTokens ?? 0;
156
+ accumulatedUsage.outputTokens += u.outputTokens ?? 0;
157
+ accumulatedUsage.totalTokens += u.totalTokens ?? 0;
158
+ };
152
159
  for await (const part of result.fullStream) {
153
160
  switch (part.type) {
154
161
  case "text-delta":
155
162
  yield { type: "text-delta", text: part.text ?? part.delta ?? "" };
156
163
  break;
164
+ case "finish-step":
165
+ addUsage(part.usage);
166
+ break;
157
167
  case "tool-call":
158
168
  yield {
159
169
  type: "tool-call",
@@ -178,17 +188,19 @@ async function* runStream(registry, params, defaults) {
178
188
  error: part.error
179
189
  };
180
190
  break;
181
- case "finish":
191
+ case "finish": {
192
+ const finalUsage = part.totalUsage ?? part.usage;
182
193
  yield {
183
194
  type: "finish",
184
195
  finishReason: part.finishReason,
185
196
  usage: {
186
- inputTokens: part.usage?.inputTokens ?? 0,
187
- outputTokens: part.usage?.outputTokens ?? 0,
188
- totalTokens: part.usage?.totalTokens ?? 0
197
+ inputTokens: finalUsage?.inputTokens ?? accumulatedUsage.inputTokens,
198
+ outputTokens: finalUsage?.outputTokens ?? accumulatedUsage.outputTokens,
199
+ totalTokens: finalUsage?.totalTokens ?? accumulatedUsage.totalTokens
189
200
  }
190
201
  };
191
202
  break;
203
+ }
192
204
  case "error":
193
205
  yield { type: "error", error: part.error };
194
206
  break;
@@ -198,18 +210,20 @@ async function* runStream(registry, params, defaults) {
198
210
  async function runObject(registry, params, defaults) {
199
211
  const agent = registry.resolveAgent(params.agent);
200
212
  const messages = buildMessages(agent, params);
201
- const result = await generateObject({
213
+ const result = await generateText({
202
214
  model: resolveModel(agent, defaults),
203
215
  messages,
204
- schema: params.inputSchema,
205
- schemaName: params.schemaName,
206
- schemaDescription: params.schemaDescription,
216
+ output: Output.object({
217
+ schema: params.inputSchema,
218
+ name: params.schemaName,
219
+ description: params.schemaDescription
220
+ }),
207
221
  temperature: params.temperature ?? agent.temperature,
208
222
  maxOutputTokens: params.maxOutputTokens ?? agent.maxOutputTokens,
209
223
  providerOptions: agent.providerOptions
210
224
  });
211
225
  return {
212
- object: result.object,
226
+ object: result.output,
213
227
  finishReason: result.finishReason ?? "stop",
214
228
  usage: {
215
229
  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.0",
3
+ "version": "0.4.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,