@opperai/agents 0.6.0 → 0.7.1

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 CHANGED
@@ -242,6 +242,7 @@ var init_context = __esm({
242
242
  metadata;
243
243
  startedAt;
244
244
  updatedAt;
245
+ pendingSpanUpdates = [];
245
246
  constructor(options) {
246
247
  const now = Date.now();
247
248
  this.agentName = options.agentName;
@@ -1634,7 +1635,7 @@ var mergeSchemaDefaults = (schema, value) => {
1634
1635
 
1635
1636
  // package.json
1636
1637
  var package_default = {
1637
- version: "0.6.0"};
1638
+ version: "0.7.1"};
1638
1639
 
1639
1640
  // src/utils/version.ts
1640
1641
  var SDK_NAME = "@opperai/agents";
@@ -1670,7 +1671,8 @@ var extractCost = (response) => {
1670
1671
  };
1671
1672
  };
1672
1673
  var DEFAULT_RETRY_CONFIG = {
1673
- maxRetries: 3,
1674
+ maxRetries: 0,
1675
+ // Retries are handled in the opper-node sdk
1674
1676
  initialDelayMs: 1e3,
1675
1677
  backoffMultiplier: 2,
1676
1678
  maxDelayMs: 1e4
@@ -1683,7 +1685,8 @@ var OpperClient = class {
1683
1685
  this.client = new Opper({
1684
1686
  httpBearer: apiKey ?? process.env["OPPER_HTTP_BEARER"] ?? "",
1685
1687
  userAgent: getUserAgent(),
1686
- ...options.baseUrl && { serverURL: options.baseUrl }
1688
+ ...options.baseUrl && { serverURL: options.baseUrl },
1689
+ retryConfig: { strategy: "backoff" }
1687
1690
  });
1688
1691
  this.logger = options.logger ?? getDefaultLogger();
1689
1692
  this.retryConfig = {
@@ -2144,6 +2147,30 @@ var Agent = class extends BaseAgent {
2144
2147
  }
2145
2148
  return String(input);
2146
2149
  }
2150
+ queueSpanUpdate(context, update) {
2151
+ context.pendingSpanUpdates.push(update);
2152
+ }
2153
+ async flushPendingSpanUpdates(context) {
2154
+ const updates = context.pendingSpanUpdates.splice(
2155
+ 0,
2156
+ context.pendingSpanUpdates.length
2157
+ );
2158
+ if (updates.length === 0) {
2159
+ return;
2160
+ }
2161
+ await Promise.allSettled(
2162
+ updates.map((update) => {
2163
+ const options = {
2164
+ ...update.error !== void 0 && { error: update.error },
2165
+ ...update.startTime && { startTime: update.startTime },
2166
+ ...update.endTime && { endTime: update.endTime },
2167
+ ...update.meta && { meta: update.meta },
2168
+ ...update.name && { name: update.name }
2169
+ };
2170
+ return this.opperClient.updateSpan(update.spanId, update.output, options);
2171
+ })
2172
+ );
2173
+ }
2147
2174
  /**
2148
2175
  * Main agent loop: think → tool execution → memory handling → repeat until complete
2149
2176
  */
@@ -2205,7 +2232,9 @@ var Agent = class extends BaseAgent {
2205
2232
  }
2206
2233
  }
2207
2234
  const executionEndTime2 = /* @__PURE__ */ new Date();
2208
- await this.opperClient.updateSpan(parentSpan.id, finalResult, {
2235
+ this.queueSpanUpdate(context, {
2236
+ spanId: parentSpan.id,
2237
+ output: finalResult,
2209
2238
  startTime: executionStartTime,
2210
2239
  endTime: executionEndTime2,
2211
2240
  meta: {
@@ -2260,7 +2289,9 @@ var Agent = class extends BaseAgent {
2260
2289
  }
2261
2290
  const result = await this.generateFinalResult(input, context);
2262
2291
  const executionEndTime = /* @__PURE__ */ new Date();
2263
- await this.opperClient.updateSpan(parentSpan.id, result, {
2292
+ this.queueSpanUpdate(context, {
2293
+ spanId: parentSpan.id,
2294
+ output: result,
2264
2295
  startTime: executionStartTime,
2265
2296
  endTime: executionEndTime,
2266
2297
  meta: {
@@ -2270,7 +2301,8 @@ var Agent = class extends BaseAgent {
2270
2301
  return result;
2271
2302
  } catch (error) {
2272
2303
  const executionEndTime = /* @__PURE__ */ new Date();
2273
- await this.opperClient.updateSpan(parentSpan.id, void 0, {
2304
+ this.queueSpanUpdate(context, {
2305
+ spanId: parentSpan.id,
2274
2306
  error: error instanceof Error ? error.message : String(error),
2275
2307
  startTime: executionStartTime,
2276
2308
  endTime: executionEndTime,
@@ -2279,6 +2311,8 @@ var Agent = class extends BaseAgent {
2279
2311
  }
2280
2312
  });
2281
2313
  throw error;
2314
+ } finally {
2315
+ await this.flushPendingSpanUpdates(context);
2282
2316
  }
2283
2317
  }
2284
2318
  /**
@@ -2322,7 +2356,8 @@ var Agent = class extends BaseAgent {
2322
2356
  response
2323
2357
  });
2324
2358
  if (response.spanId) {
2325
- await this.opperClient.updateSpan(response.spanId, void 0, {
2359
+ this.queueSpanUpdate(context, {
2360
+ spanId: response.spanId,
2326
2361
  name: "think"
2327
2362
  });
2328
2363
  }
@@ -2433,7 +2468,8 @@ var Agent = class extends BaseAgent {
2433
2468
  parsed: decision
2434
2469
  });
2435
2470
  if (streamSpanId) {
2436
- await this.opperClient.updateSpan(streamSpanId, void 0, {
2471
+ this.queueSpanUpdate(context, {
2472
+ spanId: streamSpanId,
2437
2473
  name: "think"
2438
2474
  });
2439
2475
  }
@@ -2619,13 +2655,16 @@ The memory you write persists across all process() calls on this agent.`;
2619
2655
  const endTime = /* @__PURE__ */ new Date();
2620
2656
  const durationMs = endTime.getTime() - startTime.getTime();
2621
2657
  if (result.success) {
2622
- await this.opperClient.updateSpan(toolSpan.id, result.output, {
2658
+ this.queueSpanUpdate(context, {
2659
+ spanId: toolSpan.id,
2660
+ output: result.output,
2623
2661
  startTime,
2624
2662
  endTime,
2625
2663
  meta: { durationMs }
2626
2664
  });
2627
2665
  } else {
2628
- await this.opperClient.updateSpan(toolSpan.id, void 0, {
2666
+ this.queueSpanUpdate(context, {
2667
+ spanId: toolSpan.id,
2629
2668
  error: result.error instanceof Error ? result.error.message : String(result.error),
2630
2669
  startTime,
2631
2670
  endTime,
@@ -2651,7 +2690,8 @@ The memory you write persists across all process() calls on this agent.`;
2651
2690
  } catch (error) {
2652
2691
  const endTime = /* @__PURE__ */ new Date();
2653
2692
  const durationMs = endTime.getTime() - startTime.getTime();
2654
- await this.opperClient.updateSpan(toolSpan.id, void 0, {
2693
+ this.queueSpanUpdate(context, {
2694
+ spanId: toolSpan.id,
2655
2695
  error: error instanceof Error ? error.message : String(error),
2656
2696
  startTime,
2657
2697
  endTime,
@@ -2712,7 +2752,9 @@ The memory you write persists across all process() calls on this agent.`;
2712
2752
  const memoryData = await this.memory.read(keys);
2713
2753
  const endTime = /* @__PURE__ */ new Date();
2714
2754
  const durationMs = endTime.getTime() - startTime.getTime();
2715
- await this.opperClient.updateSpan(memoryReadSpan.id, memoryData, {
2755
+ this.queueSpanUpdate(context, {
2756
+ spanId: memoryReadSpan.id,
2757
+ output: memoryData,
2716
2758
  startTime,
2717
2759
  endTime,
2718
2760
  meta: { durationMs }
@@ -2779,15 +2821,13 @@ The memory you write persists across all process() calls on this agent.`;
2779
2821
  }
2780
2822
  const endTime = /* @__PURE__ */ new Date();
2781
2823
  const durationMs = endTime.getTime() - startTime.getTime();
2782
- await this.opperClient.updateSpan(
2783
- memoryWriteSpan.id,
2784
- `Successfully wrote ${updateEntries.length} keys`,
2785
- {
2786
- startTime,
2787
- endTime,
2788
- meta: { durationMs }
2789
- }
2790
- );
2824
+ this.queueSpanUpdate(context, {
2825
+ spanId: memoryWriteSpan.id,
2826
+ output: `Successfully wrote ${updateEntries.length} keys`,
2827
+ startTime,
2828
+ endTime,
2829
+ meta: { durationMs }
2830
+ });
2791
2831
  this.log(`Wrote ${updateEntries.length} memory entries`);
2792
2832
  summaries.push(
2793
2833
  ToolExecutionSummarySchema.parse({