@opperai/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.
- package/README.md +9 -5
- package/dist/index.cjs +173 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +270 -50
- package/dist/index.d.ts +270 -50
- package/dist/index.js +172 -141
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -100,7 +100,8 @@ var init_tool = __esm({
|
|
|
100
100
|
output,
|
|
101
101
|
...init.startedAt !== void 0 && { startedAt: init.startedAt },
|
|
102
102
|
finishedAt: init.finishedAt ?? Date.now(),
|
|
103
|
-
metadata: init.metadata ?? {}
|
|
103
|
+
metadata: init.metadata ?? {},
|
|
104
|
+
...init.usage !== void 0 && { usage: init.usage }
|
|
104
105
|
};
|
|
105
106
|
ToolResultSuccessSchema.parse(result);
|
|
106
107
|
return result;
|
|
@@ -112,7 +113,8 @@ var init_tool = __esm({
|
|
|
112
113
|
error,
|
|
113
114
|
...init.startedAt !== void 0 && { startedAt: init.startedAt },
|
|
114
115
|
finishedAt: init.finishedAt ?? Date.now(),
|
|
115
|
-
metadata: init.metadata ?? {}
|
|
116
|
+
metadata: init.metadata ?? {},
|
|
117
|
+
...init.usage !== void 0 && { usage: init.usage }
|
|
116
118
|
};
|
|
117
119
|
ToolResultFailureSchema.parse(result);
|
|
118
120
|
return result;
|
|
@@ -167,14 +169,43 @@ var init_tool = __esm({
|
|
|
167
169
|
var context_exports = {};
|
|
168
170
|
__export(context_exports, {
|
|
169
171
|
AgentContext: () => AgentContext,
|
|
172
|
+
BaseUsageSchema: () => BaseUsageSchema,
|
|
170
173
|
ExecutionCycleSchema: () => ExecutionCycleSchema,
|
|
171
|
-
UsageSchema: () => UsageSchema
|
|
174
|
+
UsageSchema: () => UsageSchema,
|
|
175
|
+
addUsage: () => addUsage,
|
|
176
|
+
createEmptyUsage: () => createEmptyUsage
|
|
172
177
|
});
|
|
173
|
-
|
|
178
|
+
function createEmptyUsage() {
|
|
179
|
+
return {
|
|
180
|
+
requests: 0,
|
|
181
|
+
inputTokens: 0,
|
|
182
|
+
outputTokens: 0,
|
|
183
|
+
totalTokens: 0,
|
|
184
|
+
cost: {
|
|
185
|
+
generation: 0,
|
|
186
|
+
platform: 0,
|
|
187
|
+
total: 0
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function addUsage(a, b) {
|
|
192
|
+
return {
|
|
193
|
+
requests: a.requests + b.requests,
|
|
194
|
+
inputTokens: a.inputTokens + b.inputTokens,
|
|
195
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
196
|
+
totalTokens: a.totalTokens + b.totalTokens,
|
|
197
|
+
cost: {
|
|
198
|
+
generation: a.cost.generation + b.cost.generation,
|
|
199
|
+
platform: a.cost.platform + b.cost.platform,
|
|
200
|
+
total: a.cost.total + b.cost.total
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
var BaseUsageSchema, UsageSchema, ExecutionCycleSchema, AgentContext;
|
|
174
205
|
var init_context = __esm({
|
|
175
206
|
"src/base/context.ts"() {
|
|
176
207
|
init_tool();
|
|
177
|
-
|
|
208
|
+
BaseUsageSchema = z.object({
|
|
178
209
|
requests: z.number().int().nonnegative().default(0),
|
|
179
210
|
inputTokens: z.number().int().nonnegative().default(0),
|
|
180
211
|
outputTokens: z.number().int().nonnegative().default(0),
|
|
@@ -185,6 +216,13 @@ var init_context = __esm({
|
|
|
185
216
|
total: z.number().nonnegative().default(0)
|
|
186
217
|
}).default({ generation: 0, platform: 0, total: 0 })
|
|
187
218
|
});
|
|
219
|
+
UsageSchema = BaseUsageSchema.extend({
|
|
220
|
+
/**
|
|
221
|
+
* Optional breakdown of usage by source (agent name).
|
|
222
|
+
* Only present when nested agents are used.
|
|
223
|
+
*/
|
|
224
|
+
breakdown: z.record(z.string(), z.lazy(() => BaseUsageSchema)).optional()
|
|
225
|
+
});
|
|
188
226
|
ExecutionCycleSchema = z.object({
|
|
189
227
|
iteration: z.number().int().nonnegative(),
|
|
190
228
|
thought: z.unknown().nullable().optional(),
|
|
@@ -226,10 +264,42 @@ var init_context = __esm({
|
|
|
226
264
|
generation: this.usage.cost.generation + next.cost.generation,
|
|
227
265
|
platform: this.usage.cost.platform + next.cost.platform,
|
|
228
266
|
total: this.usage.cost.total + next.cost.total
|
|
229
|
-
}
|
|
267
|
+
},
|
|
268
|
+
// Preserve existing breakdown
|
|
269
|
+
...this.usage.breakdown && { breakdown: this.usage.breakdown }
|
|
230
270
|
};
|
|
231
271
|
this.touch();
|
|
232
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Update usage with source tracking for nested agent aggregation.
|
|
275
|
+
* This method updates the total usage AND tracks it by source in the breakdown.
|
|
276
|
+
*
|
|
277
|
+
* @param source - The source name (typically the agent/tool name)
|
|
278
|
+
* @param delta - The usage to add
|
|
279
|
+
*/
|
|
280
|
+
updateUsageWithSource(source, delta) {
|
|
281
|
+
this.updateUsage(delta);
|
|
282
|
+
if (!this.usage.breakdown) {
|
|
283
|
+
this.usage.breakdown = {};
|
|
284
|
+
}
|
|
285
|
+
const existing = this.usage.breakdown[source] ?? createEmptyUsage();
|
|
286
|
+
this.usage.breakdown[source] = addUsage(existing, delta);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Clean up the usage breakdown if it only contains the parent agent.
|
|
290
|
+
* This ensures breakdown is only present when there are nested agents.
|
|
291
|
+
*
|
|
292
|
+
* @param parentAgentName - The name of the parent agent to check for
|
|
293
|
+
*/
|
|
294
|
+
cleanupBreakdownIfOnlyParent(parentAgentName) {
|
|
295
|
+
if (!this.usage.breakdown) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const sources = Object.keys(this.usage.breakdown);
|
|
299
|
+
if (sources.length === 1 && sources[0] === parentAgentName) {
|
|
300
|
+
delete this.usage.breakdown;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
233
303
|
addCycle(cycle) {
|
|
234
304
|
const parsed = ExecutionCycleSchema.parse(cycle);
|
|
235
305
|
this.executionHistory.push(parsed);
|
|
@@ -275,6 +345,18 @@ var init_context = __esm({
|
|
|
275
345
|
this.touch();
|
|
276
346
|
}
|
|
277
347
|
snapshot() {
|
|
348
|
+
const usageCopy = {
|
|
349
|
+
...this.usage,
|
|
350
|
+
cost: { ...this.usage.cost },
|
|
351
|
+
...this.usage.breakdown && {
|
|
352
|
+
breakdown: Object.fromEntries(
|
|
353
|
+
Object.entries(this.usage.breakdown).map(([key, value]) => [
|
|
354
|
+
key,
|
|
355
|
+
{ ...value, cost: { ...value.cost } }
|
|
356
|
+
])
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
};
|
|
278
360
|
return {
|
|
279
361
|
agentName: this.agentName,
|
|
280
362
|
sessionId: this.sessionId,
|
|
@@ -282,7 +364,7 @@ var init_context = __esm({
|
|
|
282
364
|
iteration: this.iteration,
|
|
283
365
|
goal: this.goal,
|
|
284
366
|
executionHistory: [...this.executionHistory],
|
|
285
|
-
usage:
|
|
367
|
+
usage: usageCopy,
|
|
286
368
|
toolCalls: [...this.toolCalls],
|
|
287
369
|
metadata: { ...this.metadata },
|
|
288
370
|
startedAt: this.startedAt,
|
|
@@ -469,82 +551,6 @@ var HookManager = class {
|
|
|
469
551
|
};
|
|
470
552
|
var createHookManager = (logger) => new HookManager(logger);
|
|
471
553
|
|
|
472
|
-
// src/base/events.ts
|
|
473
|
-
var AgentEvents = {
|
|
474
|
-
StreamStart: HookEvents.StreamStart,
|
|
475
|
-
StreamChunk: HookEvents.StreamChunk,
|
|
476
|
-
StreamEnd: HookEvents.StreamEnd,
|
|
477
|
-
StreamError: HookEvents.StreamError
|
|
478
|
-
};
|
|
479
|
-
var AgentEventEmitter = class {
|
|
480
|
-
registry = /* @__PURE__ */ new Map();
|
|
481
|
-
logger;
|
|
482
|
-
constructor(logger) {
|
|
483
|
-
this.logger = logger ?? getDefaultLogger();
|
|
484
|
-
}
|
|
485
|
-
on(event, listener) {
|
|
486
|
-
const listeners = this.registry.get(event) ?? /* @__PURE__ */ new Set();
|
|
487
|
-
listeners.add(listener);
|
|
488
|
-
this.registry.set(event, listeners);
|
|
489
|
-
return () => this.off(event, listener);
|
|
490
|
-
}
|
|
491
|
-
once(event, listener) {
|
|
492
|
-
const wrapper = (payload) => {
|
|
493
|
-
try {
|
|
494
|
-
listener(payload);
|
|
495
|
-
} finally {
|
|
496
|
-
this.off(event, wrapper);
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
return this.on(event, wrapper);
|
|
500
|
-
}
|
|
501
|
-
off(event, listener) {
|
|
502
|
-
const listeners = this.registry.get(event);
|
|
503
|
-
if (!listeners) {
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
listeners.delete(listener);
|
|
507
|
-
if (listeners.size === 0) {
|
|
508
|
-
this.registry.delete(event);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
emit(event, payload) {
|
|
512
|
-
const listeners = Array.from(
|
|
513
|
-
this.registry.get(event) ?? /* @__PURE__ */ new Set()
|
|
514
|
-
);
|
|
515
|
-
if (listeners.length === 0) {
|
|
516
|
-
return;
|
|
517
|
-
}
|
|
518
|
-
for (const listener of listeners) {
|
|
519
|
-
try {
|
|
520
|
-
listener(payload);
|
|
521
|
-
} catch (error) {
|
|
522
|
-
this.logger.warn(`Agent event listener failed for "${event}"`, {
|
|
523
|
-
event,
|
|
524
|
-
error: error instanceof Error ? error.message : String(error)
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
removeAllListeners(event) {
|
|
530
|
-
if (event) {
|
|
531
|
-
this.registry.delete(event);
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
this.registry.clear();
|
|
535
|
-
}
|
|
536
|
-
listenerCount(event) {
|
|
537
|
-
if (event) {
|
|
538
|
-
return this.registry.get(event)?.size ?? 0;
|
|
539
|
-
}
|
|
540
|
-
let total = 0;
|
|
541
|
-
for (const listeners of this.registry.values()) {
|
|
542
|
-
total += listeners.size;
|
|
543
|
-
}
|
|
544
|
-
return total;
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
|
|
548
554
|
// src/base/agent.ts
|
|
549
555
|
init_tool();
|
|
550
556
|
function sanitizeId(name) {
|
|
@@ -929,10 +935,6 @@ var BaseAgent = class {
|
|
|
929
935
|
* Hook manager for lifecycle events
|
|
930
936
|
*/
|
|
931
937
|
hooks;
|
|
932
|
-
/**
|
|
933
|
-
* Event dispatcher for runtime events (notably streaming)
|
|
934
|
-
*/
|
|
935
|
-
events;
|
|
936
938
|
/**
|
|
937
939
|
* Registry of available tools
|
|
938
940
|
*/
|
|
@@ -987,7 +989,6 @@ var BaseAgent = class {
|
|
|
987
989
|
this.enableStreaming = config.enableStreaming ?? false;
|
|
988
990
|
this.metadata = { ...config.metadata ?? {} };
|
|
989
991
|
this.hooks = new HookManager();
|
|
990
|
-
this.events = new AgentEventEmitter();
|
|
991
992
|
this.tools = /* @__PURE__ */ new Map();
|
|
992
993
|
this.baseTools = /* @__PURE__ */ new Map();
|
|
993
994
|
this.toolProviders = /* @__PURE__ */ new Set();
|
|
@@ -1038,11 +1039,37 @@ var BaseAgent = class {
|
|
|
1038
1039
|
* Main entry point for agent execution.
|
|
1039
1040
|
* Orchestrates lifecycle: context initialization, hook triggering, loop execution, teardown.
|
|
1040
1041
|
*
|
|
1042
|
+
* **Note:** This method will be removed in a future update.
|
|
1043
|
+
* Consider using {@link run} instead, which returns both result and usage statistics.
|
|
1044
|
+
*
|
|
1041
1045
|
* @param input - Input to process
|
|
1042
1046
|
* @param parentSpanId - Optional parent span ID for tracing
|
|
1043
1047
|
* @returns Processed output
|
|
1044
1048
|
*/
|
|
1045
1049
|
async process(input, parentSpanId) {
|
|
1050
|
+
const { result } = await this.executeProcess(input, parentSpanId);
|
|
1051
|
+
return result;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Process input and return both result and usage statistics.
|
|
1055
|
+
* This is the recommended method for agent execution.
|
|
1056
|
+
*
|
|
1057
|
+
* @param input - Input to process
|
|
1058
|
+
* @param parentSpanId - Optional parent span ID for tracing
|
|
1059
|
+
* @returns Object containing the result and usage statistics
|
|
1060
|
+
*/
|
|
1061
|
+
async run(input, parentSpanId) {
|
|
1062
|
+
return this.executeProcess(input, parentSpanId);
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Internal method that executes the process and returns both result and usage.
|
|
1066
|
+
* This is the core implementation shared by process() and run().
|
|
1067
|
+
*
|
|
1068
|
+
* @param input - Input to process
|
|
1069
|
+
* @param parentSpanId - Optional parent span ID for tracing
|
|
1070
|
+
* @returns Object containing the result and usage statistics
|
|
1071
|
+
*/
|
|
1072
|
+
async executeProcess(input, parentSpanId) {
|
|
1046
1073
|
const validatedInput = this.inputSchema ? this.inputSchema.parse(input) : input;
|
|
1047
1074
|
const context = await this.initializeContext(validatedInput, parentSpanId);
|
|
1048
1075
|
try {
|
|
@@ -1054,7 +1081,8 @@ var BaseAgent = class {
|
|
|
1054
1081
|
context,
|
|
1055
1082
|
result: validatedOutput
|
|
1056
1083
|
});
|
|
1057
|
-
|
|
1084
|
+
context.cleanupBreakdownIfOnlyParent(this.name);
|
|
1085
|
+
return { result: validatedOutput, usage: context.usage };
|
|
1058
1086
|
} catch (error) {
|
|
1059
1087
|
await this.triggerHook(HookEvents.AgentEnd, {
|
|
1060
1088
|
context,
|
|
@@ -1144,6 +1172,8 @@ var BaseAgent = class {
|
|
|
1144
1172
|
}
|
|
1145
1173
|
/**
|
|
1146
1174
|
* Convert this agent into a tool that can be used by other agents.
|
|
1175
|
+
* When used as a tool, the nested agent's usage statistics are propagated
|
|
1176
|
+
* back to the parent agent for aggregation.
|
|
1147
1177
|
*
|
|
1148
1178
|
* @param toolName - Optional custom name for the tool (defaults to agent name)
|
|
1149
1179
|
* @param toolDescription - Optional custom description (defaults to agent description)
|
|
@@ -1163,8 +1193,8 @@ var BaseAgent = class {
|
|
|
1163
1193
|
execute: async (input, executionContext) => {
|
|
1164
1194
|
try {
|
|
1165
1195
|
const parentSpanId = executionContext.spanId ?? executionContext.agentContext.parentSpanId ?? void 0;
|
|
1166
|
-
const result = await this.
|
|
1167
|
-
return ToolResultFactory.success(tool2.name, result);
|
|
1196
|
+
const { result, usage } = await this.run(input, parentSpanId);
|
|
1197
|
+
return ToolResultFactory.success(tool2.name, result, { usage });
|
|
1168
1198
|
} catch (error) {
|
|
1169
1199
|
return ToolResultFactory.failure(
|
|
1170
1200
|
tool2.name,
|
|
@@ -1187,32 +1217,34 @@ var BaseAgent = class {
|
|
|
1187
1217
|
}
|
|
1188
1218
|
/**
|
|
1189
1219
|
* Register an event listener.
|
|
1220
|
+
* This is equivalent to registerHook() - both methods use the unified hook system.
|
|
1190
1221
|
*
|
|
1191
|
-
* @param event - Event name
|
|
1222
|
+
* @param event - Event name (any HookEvents value)
|
|
1192
1223
|
* @param listener - Listener callback
|
|
1193
1224
|
* @returns Cleanup function to unregister the listener
|
|
1194
1225
|
*/
|
|
1195
1226
|
on(event, listener) {
|
|
1196
|
-
return this.
|
|
1227
|
+
return this.hooks.on(event, listener);
|
|
1197
1228
|
}
|
|
1198
1229
|
/**
|
|
1199
1230
|
* Register a one-time event listener that removes itself after the first call.
|
|
1231
|
+
* This is equivalent to calling registerHook() with a self-removing handler.
|
|
1200
1232
|
*
|
|
1201
|
-
* @param event - Event name
|
|
1233
|
+
* @param event - Event name (any HookEvents value)
|
|
1202
1234
|
* @param listener - Listener callback
|
|
1203
1235
|
* @returns Cleanup function (no-op once listener fires)
|
|
1204
1236
|
*/
|
|
1205
1237
|
once(event, listener) {
|
|
1206
|
-
return this.
|
|
1238
|
+
return this.hooks.once(event, listener);
|
|
1207
1239
|
}
|
|
1208
1240
|
/**
|
|
1209
1241
|
* Remove a previously registered event listener.
|
|
1210
1242
|
*
|
|
1211
|
-
* @param event - Event name
|
|
1243
|
+
* @param event - Event name (any HookEvents value)
|
|
1212
1244
|
* @param listener - Listener callback to remove
|
|
1213
1245
|
*/
|
|
1214
1246
|
off(event, listener) {
|
|
1215
|
-
this.
|
|
1247
|
+
this.hooks.off(event, listener);
|
|
1216
1248
|
}
|
|
1217
1249
|
/**
|
|
1218
1250
|
* Trigger a hook event with a payload.
|
|
@@ -1228,15 +1260,6 @@ var BaseAgent = class {
|
|
|
1228
1260
|
console.warn(`Hook error for event ${event}:`, error);
|
|
1229
1261
|
}
|
|
1230
1262
|
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Emit a runtime event to listeners.
|
|
1233
|
-
*
|
|
1234
|
-
* @param event - Event name
|
|
1235
|
-
* @param payload - Event payload
|
|
1236
|
-
*/
|
|
1237
|
-
emitAgentEvent(event, payload) {
|
|
1238
|
-
this.events.emit(event, payload);
|
|
1239
|
-
}
|
|
1240
1263
|
/**
|
|
1241
1264
|
* Execute a tool with proper context, hooks, and error handling.
|
|
1242
1265
|
*
|
|
@@ -1422,6 +1445,11 @@ var BaseAgent = class {
|
|
|
1422
1445
|
|
|
1423
1446
|
// src/index.ts
|
|
1424
1447
|
init_context();
|
|
1448
|
+
|
|
1449
|
+
// src/base/events.ts
|
|
1450
|
+
var AgentEvents = HookEvents;
|
|
1451
|
+
|
|
1452
|
+
// src/index.ts
|
|
1425
1453
|
init_result();
|
|
1426
1454
|
init_tool();
|
|
1427
1455
|
var ThoughtSchema = z.object({
|
|
@@ -1590,7 +1618,7 @@ var mergeSchemaDefaults = (schema, value) => {
|
|
|
1590
1618
|
|
|
1591
1619
|
// package.json
|
|
1592
1620
|
var package_default = {
|
|
1593
|
-
version: "0.
|
|
1621
|
+
version: "0.4.0"};
|
|
1594
1622
|
|
|
1595
1623
|
// src/utils/version.ts
|
|
1596
1624
|
var SDK_NAME = "@opperai/agents";
|
|
@@ -2260,7 +2288,7 @@ var Agent = class extends BaseAgent {
|
|
|
2260
2288
|
model: this.model,
|
|
2261
2289
|
...context.parentSpanId && { parentSpanId: context.parentSpanId }
|
|
2262
2290
|
});
|
|
2263
|
-
context.
|
|
2291
|
+
context.updateUsageWithSource(this.name, {
|
|
2264
2292
|
requests: 1,
|
|
2265
2293
|
inputTokens: response.usage.inputTokens,
|
|
2266
2294
|
outputTokens: response.usage.outputTokens,
|
|
@@ -2312,10 +2340,6 @@ var Agent = class extends BaseAgent {
|
|
|
2312
2340
|
context,
|
|
2313
2341
|
callType: "think"
|
|
2314
2342
|
});
|
|
2315
|
-
this.emitAgentEvent(HookEvents.StreamStart, {
|
|
2316
|
-
context,
|
|
2317
|
-
callType: "think"
|
|
2318
|
-
});
|
|
2319
2343
|
try {
|
|
2320
2344
|
const streamResponse = await this.opperClient.stream({
|
|
2321
2345
|
name: spanName,
|
|
@@ -2352,7 +2376,6 @@ var Agent = class extends BaseAgent {
|
|
|
2352
2376
|
fieldBuffers: feedResult.snapshot
|
|
2353
2377
|
};
|
|
2354
2378
|
await this.triggerHook(HookEvents.StreamChunk, chunkPayload);
|
|
2355
|
-
this.emitAgentEvent(HookEvents.StreamChunk, chunkPayload);
|
|
2356
2379
|
}
|
|
2357
2380
|
const fieldBuffers = assembler.snapshot();
|
|
2358
2381
|
const endPayload = {
|
|
@@ -2361,7 +2384,6 @@ var Agent = class extends BaseAgent {
|
|
|
2361
2384
|
fieldBuffers
|
|
2362
2385
|
};
|
|
2363
2386
|
await this.triggerHook(HookEvents.StreamEnd, endPayload);
|
|
2364
|
-
this.emitAgentEvent(HookEvents.StreamEnd, endPayload);
|
|
2365
2387
|
const finalize = assembler.finalize();
|
|
2366
2388
|
let decision;
|
|
2367
2389
|
if (finalize.type === "structured" && finalize.structured) {
|
|
@@ -2378,7 +2400,7 @@ var Agent = class extends BaseAgent {
|
|
|
2378
2400
|
streamSpanId
|
|
2379
2401
|
);
|
|
2380
2402
|
if (!usageTracked) {
|
|
2381
|
-
context.
|
|
2403
|
+
context.updateUsageWithSource(this.name, {
|
|
2382
2404
|
requests: 1,
|
|
2383
2405
|
inputTokens: 0,
|
|
2384
2406
|
outputTokens: 0,
|
|
@@ -2423,11 +2445,6 @@ var Agent = class extends BaseAgent {
|
|
|
2423
2445
|
callType: "think",
|
|
2424
2446
|
error
|
|
2425
2447
|
});
|
|
2426
|
-
this.emitAgentEvent(HookEvents.StreamError, {
|
|
2427
|
-
context,
|
|
2428
|
-
callType: "think",
|
|
2429
|
-
error
|
|
2430
|
-
});
|
|
2431
2448
|
this.logger.error("Think step failed", error);
|
|
2432
2449
|
throw new Error(
|
|
2433
2450
|
`Think step failed: ${error instanceof Error ? error.message : String(error)}`
|
|
@@ -2485,12 +2502,30 @@ The memory you write persists across all process() calls on this agent.`;
|
|
|
2485
2502
|
* Build dynamic context for the think step
|
|
2486
2503
|
*/
|
|
2487
2504
|
async buildThinkContext(input, context) {
|
|
2488
|
-
const availableTools = Array.from(this.tools.values()).map((tool2) =>
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2505
|
+
const availableTools = Array.from(this.tools.values()).map((tool2) => {
|
|
2506
|
+
let parameters = {};
|
|
2507
|
+
if (tool2.schema) {
|
|
2508
|
+
parameters = schemaToJson(tool2.schema);
|
|
2509
|
+
} else if (tool2.metadata?.["parameters"] && typeof tool2.metadata["parameters"] === "object") {
|
|
2510
|
+
parameters = tool2.metadata["parameters"];
|
|
2511
|
+
}
|
|
2512
|
+
let returns = void 0;
|
|
2513
|
+
if (tool2.outputSchema) {
|
|
2514
|
+
returns = schemaToJson(tool2.outputSchema);
|
|
2515
|
+
} else if (tool2.metadata?.["outputSchema"] && typeof tool2.metadata["outputSchema"] === "object") {
|
|
2516
|
+
returns = tool2.metadata["outputSchema"];
|
|
2517
|
+
}
|
|
2518
|
+
return {
|
|
2519
|
+
name: tool2.name,
|
|
2520
|
+
description: tool2.description || "",
|
|
2521
|
+
// Include parameters (from Zod schema or MCP metadata)
|
|
2522
|
+
parameters,
|
|
2523
|
+
// Include output schema if defined (helps LLM understand what tool returns)
|
|
2524
|
+
...returns !== void 0 ? { returns } : {},
|
|
2525
|
+
// Include examples if defined (helps LLM understand expected behavior)
|
|
2526
|
+
...tool2.examples ? { examples: tool2.examples } : {}
|
|
2527
|
+
};
|
|
2528
|
+
});
|
|
2494
2529
|
const executionHistory = context.getLastNCycles(3).map((cycle) => {
|
|
2495
2530
|
const thought = typeof cycle.thought === "object" && cycle.thought !== null ? cycle.thought["reasoning"] || "" : String(cycle.thought || "");
|
|
2496
2531
|
const results = Array.isArray(cycle.results) ? cycle.results.map((r) => {
|
|
@@ -2560,6 +2595,9 @@ The memory you write persists across all process() calls on this agent.`;
|
|
|
2560
2595
|
context,
|
|
2561
2596
|
{ spanId: toolSpan.id }
|
|
2562
2597
|
);
|
|
2598
|
+
if (result.usage) {
|
|
2599
|
+
context.updateUsageWithSource(toolCall.toolName, result.usage);
|
|
2600
|
+
}
|
|
2563
2601
|
const endTime = /* @__PURE__ */ new Date();
|
|
2564
2602
|
const durationMs = endTime.getTime() - startTime.getTime();
|
|
2565
2603
|
if (result.success) {
|
|
@@ -2818,7 +2856,7 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2818
2856
|
callOptions.outputSchema = this.outputSchema;
|
|
2819
2857
|
}
|
|
2820
2858
|
const response = await this.opperClient.call(callOptions);
|
|
2821
|
-
context.
|
|
2859
|
+
context.updateUsageWithSource(this.name, {
|
|
2822
2860
|
requests: 1,
|
|
2823
2861
|
inputTokens: response.usage.inputTokens,
|
|
2824
2862
|
outputTokens: response.usage.outputTokens,
|
|
@@ -2848,10 +2886,6 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2848
2886
|
context,
|
|
2849
2887
|
callType: "final_result"
|
|
2850
2888
|
});
|
|
2851
|
-
this.emitAgentEvent(HookEvents.StreamStart, {
|
|
2852
|
-
context,
|
|
2853
|
-
callType: "final_result"
|
|
2854
|
-
});
|
|
2855
2889
|
try {
|
|
2856
2890
|
const sanitizedName = this.name.toLowerCase().replace(/[\s-]/g, "_");
|
|
2857
2891
|
const functionName = `generate_final_result_${sanitizedName}`;
|
|
@@ -2890,7 +2924,6 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2890
2924
|
fieldBuffers: feedResult.snapshot
|
|
2891
2925
|
};
|
|
2892
2926
|
await this.triggerHook(HookEvents.StreamChunk, chunkPayload);
|
|
2893
|
-
this.emitAgentEvent(HookEvents.StreamChunk, chunkPayload);
|
|
2894
2927
|
}
|
|
2895
2928
|
const fieldBuffers = assembler.snapshot();
|
|
2896
2929
|
const endPayload = {
|
|
@@ -2899,7 +2932,6 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2899
2932
|
fieldBuffers
|
|
2900
2933
|
};
|
|
2901
2934
|
await this.triggerHook(HookEvents.StreamEnd, endPayload);
|
|
2902
|
-
this.emitAgentEvent(HookEvents.StreamEnd, endPayload);
|
|
2903
2935
|
const finalize = assembler.finalize();
|
|
2904
2936
|
let result;
|
|
2905
2937
|
if (this.outputSchema) {
|
|
@@ -2923,7 +2955,7 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2923
2955
|
streamSpanId
|
|
2924
2956
|
);
|
|
2925
2957
|
if (!usageTracked) {
|
|
2926
|
-
context.
|
|
2958
|
+
context.updateUsageWithSource(this.name, {
|
|
2927
2959
|
requests: 1,
|
|
2928
2960
|
inputTokens: 0,
|
|
2929
2961
|
outputTokens: 0,
|
|
@@ -2947,11 +2979,6 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2947
2979
|
callType: "final_result",
|
|
2948
2980
|
error
|
|
2949
2981
|
});
|
|
2950
|
-
this.emitAgentEvent(HookEvents.StreamError, {
|
|
2951
|
-
context,
|
|
2952
|
-
callType: "final_result",
|
|
2953
|
-
error
|
|
2954
|
-
});
|
|
2955
2982
|
this.logger.error("Failed to generate final result", error);
|
|
2956
2983
|
throw new Error(
|
|
2957
2984
|
`Failed to generate final result: ${error instanceof Error ? error.message : String(error)}`
|
|
@@ -2987,7 +3014,7 @@ Follow any instructions provided for formatting and style.`;
|
|
|
2987
3014
|
const fallbackTotal = record["total_tokens"];
|
|
2988
3015
|
const totalTokensRaw = typeof primaryTotal === "number" && Number.isFinite(primaryTotal) ? primaryTotal : typeof fallbackTotal === "number" && Number.isFinite(fallbackTotal) ? fallbackTotal : void 0;
|
|
2989
3016
|
if (totalTokensRaw !== void 0) {
|
|
2990
|
-
context.
|
|
3017
|
+
context.updateUsageWithSource(this.name, {
|
|
2991
3018
|
requests: 1,
|
|
2992
3019
|
inputTokens: 0,
|
|
2993
3020
|
outputTokens: 0,
|
|
@@ -3439,6 +3466,8 @@ function createToolDefinition(options, methodName, propertyKey, method, callTarg
|
|
|
3439
3466
|
name,
|
|
3440
3467
|
description,
|
|
3441
3468
|
...options.schema && { schema: options.schema },
|
|
3469
|
+
...options.outputSchema && { outputSchema: options.outputSchema },
|
|
3470
|
+
...options.examples && { examples: options.examples },
|
|
3442
3471
|
...options.timeoutMs !== void 0 && { timeoutMs: options.timeoutMs },
|
|
3443
3472
|
metadata: {
|
|
3444
3473
|
...options.metadata,
|
|
@@ -3455,6 +3484,8 @@ function createFunctionTool(fn, options = {}) {
|
|
|
3455
3484
|
name,
|
|
3456
3485
|
description,
|
|
3457
3486
|
...options.schema && { schema: options.schema },
|
|
3487
|
+
...options.outputSchema && { outputSchema: options.outputSchema },
|
|
3488
|
+
...options.examples && { examples: options.examples },
|
|
3458
3489
|
...options.timeoutMs !== void 0 && { timeoutMs: options.timeoutMs },
|
|
3459
3490
|
metadata: {
|
|
3460
3491
|
...options.metadata,
|
|
@@ -3770,6 +3801,6 @@ var ToolRunner = class {
|
|
|
3770
3801
|
}
|
|
3771
3802
|
};
|
|
3772
3803
|
|
|
3773
|
-
export { Agent, AgentContext, AgentDecisionSchema,
|
|
3804
|
+
export { Agent, AgentContext, AgentDecisionSchema, AgentEvents, BaseAgent, BaseUsageSchema, ConsoleLogger, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, ExecutionCycleSchema, HookEvents, HookManager, InMemoryStore, LogLevel, MCPClient, MCPServerConfigSchema, MCPToolProvider, MCPconfig, MemoryEntryMetadataSchema, MemoryEntrySchema, MemoryUpdateSchema, OpperClient, Result, STREAM_ROOT_PATH, SchemaValidationError, SilentLogger, StreamAssembler, ThoughtSchema, ToolCallRecordSchema, ToolCallSchema, ToolExecutionSummarySchema, ToolMetadataSchema, ToolResultFactory, ToolResultFailureSchema, ToolResultSchema, ToolResultSuccessSchema, ToolRunner, UsageSchema, addUsage, coerceToolDefinition, createAgentDecisionWithOutputSchema, createEmptyUsage, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createStreamAssembler, createToolCallRecord, err, extractTools, generateAgentFlowDiagram, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput, zodSchemaToJsonSchema };
|
|
3774
3805
|
//# sourceMappingURL=index.js.map
|
|
3775
3806
|
//# sourceMappingURL=index.js.map
|