@opperai/agents 0.6.0 → 0.7.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.d.cts CHANGED
@@ -561,6 +561,15 @@ interface IterationSummary {
561
561
  }>;
562
562
  results: unknown[];
563
563
  }
564
+ interface PendingSpanUpdate {
565
+ spanId: string;
566
+ output?: unknown;
567
+ error?: string;
568
+ startTime?: Date;
569
+ endTime?: Date;
570
+ meta?: Record<string, unknown>;
571
+ name?: string;
572
+ }
564
573
  declare class AgentContext {
565
574
  readonly agentName: string;
566
575
  readonly sessionId: string;
@@ -573,6 +582,7 @@ declare class AgentContext {
573
582
  metadata: Record<string, unknown>;
574
583
  readonly startedAt: number;
575
584
  updatedAt: number;
585
+ pendingSpanUpdates: PendingSpanUpdate[];
576
586
  constructor(options: AgentContextOptions);
577
587
  updateUsage(delta: Usage): void;
578
588
  /**
@@ -1786,6 +1796,8 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
1786
1796
  * Serialize input for passing to LLM or spans
1787
1797
  */
1788
1798
  private serializeInput;
1799
+ private queueSpanUpdate;
1800
+ private flushPendingSpanUpdates;
1789
1801
  /**
1790
1802
  * Main agent loop: think → tool execution → memory handling → repeat until complete
1791
1803
  */
@@ -2587,4 +2599,4 @@ declare class ToolRunner {
2587
2599
  };
2588
2600
  }
2589
2601
 
2590
- export { type AfterToolPayload, Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentEndPayload, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, type AgentStartPayload, type AgentThought, BaseAgent, type BaseAgentConfig, type BaseUsage, BaseUsageSchema, type BeforeToolPayload, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type ExecutionThought, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, type LlmCallPayload, type LlmCallType, type LlmResponsePayload, LogLevel, type LoopEndPayload, type LoopStartPayload, MCPClient, type MCPClientOptions, type MCPServerConfig, type MCPServerConfigInput, MCPServerConfigSchema, type MCPTool, MCPToolProvider, type MCPToolProviderOptions, MCPconfig, type MaybePromise, type Memory, type MemoryCatalogEntry, type MemoryEntry, type MemoryEntryMetadata, MemoryEntryMetadataSchema, MemoryEntrySchema, type MemoryErrorPayload, type MemoryReadPayload, type MemoryUpdate, MemoryUpdateSchema, type MemoryWritePayload, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, type OpperStreamChunk, type OpperStreamEvent, type OpperStreamResponse, Result, type RetryConfig, STREAM_ROOT_PATH, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, StreamAssembler, type StreamAssemblerOptions, type StreamChunkPayload, type StreamEndPayload, type StreamErrorPayload, type StreamFeedResult, type StreamFinalizeResult, type StreamStartPayload, type Success, type ThinkEndPayload, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, type ToolErrorPayload, type ToolExample, type ToolExecutionContext, type ToolExecutionSummary, ToolExecutionSummarySchema, type ToolFailure, type ToolFunction, ToolMetadataSchema, type ToolOptions, type ToolProvider, type ToolResult, type ToolResultData, ToolResultFactory, ToolResultFailureSchema, type ToolResultInit, ToolResultSchema, ToolResultSuccessSchema, type ToolRunOptions, ToolRunner, type ToolSuccess, type UnregisterHook, type Usage, UsageSchema, type VisualizationOptions, 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 };
2602
+ export { type AfterToolPayload, Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentEndPayload, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, type AgentStartPayload, type AgentThought, BaseAgent, type BaseAgentConfig, type BaseUsage, BaseUsageSchema, type BeforeToolPayload, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type ExecutionThought, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, type LlmCallPayload, type LlmCallType, type LlmResponsePayload, LogLevel, type LoopEndPayload, type LoopStartPayload, MCPClient, type MCPClientOptions, type MCPServerConfig, type MCPServerConfigInput, MCPServerConfigSchema, type MCPTool, MCPToolProvider, type MCPToolProviderOptions, MCPconfig, type MaybePromise, type Memory, type MemoryCatalogEntry, type MemoryEntry, type MemoryEntryMetadata, MemoryEntryMetadataSchema, MemoryEntrySchema, type MemoryErrorPayload, type MemoryReadPayload, type MemoryUpdate, MemoryUpdateSchema, type MemoryWritePayload, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, type OpperStreamChunk, type OpperStreamEvent, type OpperStreamResponse, type PendingSpanUpdate, Result, type RetryConfig, STREAM_ROOT_PATH, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, StreamAssembler, type StreamAssemblerOptions, type StreamChunkPayload, type StreamEndPayload, type StreamErrorPayload, type StreamFeedResult, type StreamFinalizeResult, type StreamStartPayload, type Success, type ThinkEndPayload, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, type ToolErrorPayload, type ToolExample, type ToolExecutionContext, type ToolExecutionSummary, ToolExecutionSummarySchema, type ToolFailure, type ToolFunction, ToolMetadataSchema, type ToolOptions, type ToolProvider, type ToolResult, type ToolResultData, ToolResultFactory, ToolResultFailureSchema, type ToolResultInit, ToolResultSchema, ToolResultSuccessSchema, type ToolRunOptions, ToolRunner, type ToolSuccess, type UnregisterHook, type Usage, UsageSchema, type VisualizationOptions, 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 };
package/dist/index.d.ts CHANGED
@@ -561,6 +561,15 @@ interface IterationSummary {
561
561
  }>;
562
562
  results: unknown[];
563
563
  }
564
+ interface PendingSpanUpdate {
565
+ spanId: string;
566
+ output?: unknown;
567
+ error?: string;
568
+ startTime?: Date;
569
+ endTime?: Date;
570
+ meta?: Record<string, unknown>;
571
+ name?: string;
572
+ }
564
573
  declare class AgentContext {
565
574
  readonly agentName: string;
566
575
  readonly sessionId: string;
@@ -573,6 +582,7 @@ declare class AgentContext {
573
582
  metadata: Record<string, unknown>;
574
583
  readonly startedAt: number;
575
584
  updatedAt: number;
585
+ pendingSpanUpdates: PendingSpanUpdate[];
576
586
  constructor(options: AgentContextOptions);
577
587
  updateUsage(delta: Usage): void;
578
588
  /**
@@ -1786,6 +1796,8 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
1786
1796
  * Serialize input for passing to LLM or spans
1787
1797
  */
1788
1798
  private serializeInput;
1799
+ private queueSpanUpdate;
1800
+ private flushPendingSpanUpdates;
1789
1801
  /**
1790
1802
  * Main agent loop: think → tool execution → memory handling → repeat until complete
1791
1803
  */
@@ -2587,4 +2599,4 @@ declare class ToolRunner {
2587
2599
  };
2588
2600
  }
2589
2601
 
2590
- export { type AfterToolPayload, Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentEndPayload, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, type AgentStartPayload, type AgentThought, BaseAgent, type BaseAgentConfig, type BaseUsage, BaseUsageSchema, type BeforeToolPayload, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type ExecutionThought, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, type LlmCallPayload, type LlmCallType, type LlmResponsePayload, LogLevel, type LoopEndPayload, type LoopStartPayload, MCPClient, type MCPClientOptions, type MCPServerConfig, type MCPServerConfigInput, MCPServerConfigSchema, type MCPTool, MCPToolProvider, type MCPToolProviderOptions, MCPconfig, type MaybePromise, type Memory, type MemoryCatalogEntry, type MemoryEntry, type MemoryEntryMetadata, MemoryEntryMetadataSchema, MemoryEntrySchema, type MemoryErrorPayload, type MemoryReadPayload, type MemoryUpdate, MemoryUpdateSchema, type MemoryWritePayload, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, type OpperStreamChunk, type OpperStreamEvent, type OpperStreamResponse, Result, type RetryConfig, STREAM_ROOT_PATH, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, StreamAssembler, type StreamAssemblerOptions, type StreamChunkPayload, type StreamEndPayload, type StreamErrorPayload, type StreamFeedResult, type StreamFinalizeResult, type StreamStartPayload, type Success, type ThinkEndPayload, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, type ToolErrorPayload, type ToolExample, type ToolExecutionContext, type ToolExecutionSummary, ToolExecutionSummarySchema, type ToolFailure, type ToolFunction, ToolMetadataSchema, type ToolOptions, type ToolProvider, type ToolResult, type ToolResultData, ToolResultFactory, ToolResultFailureSchema, type ToolResultInit, ToolResultSchema, ToolResultSuccessSchema, type ToolRunOptions, ToolRunner, type ToolSuccess, type UnregisterHook, type Usage, UsageSchema, type VisualizationOptions, 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 };
2602
+ export { type AfterToolPayload, Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentEndPayload, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, type AgentStartPayload, type AgentThought, BaseAgent, type BaseAgentConfig, type BaseUsage, BaseUsageSchema, type BeforeToolPayload, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type ExecutionThought, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, type LlmCallPayload, type LlmCallType, type LlmResponsePayload, LogLevel, type LoopEndPayload, type LoopStartPayload, MCPClient, type MCPClientOptions, type MCPServerConfig, type MCPServerConfigInput, MCPServerConfigSchema, type MCPTool, MCPToolProvider, type MCPToolProviderOptions, MCPconfig, type MaybePromise, type Memory, type MemoryCatalogEntry, type MemoryEntry, type MemoryEntryMetadata, MemoryEntryMetadataSchema, MemoryEntrySchema, type MemoryErrorPayload, type MemoryReadPayload, type MemoryUpdate, MemoryUpdateSchema, type MemoryWritePayload, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, type OpperStreamChunk, type OpperStreamEvent, type OpperStreamResponse, type PendingSpanUpdate, Result, type RetryConfig, STREAM_ROOT_PATH, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, StreamAssembler, type StreamAssemblerOptions, type StreamChunkPayload, type StreamEndPayload, type StreamErrorPayload, type StreamFeedResult, type StreamFinalizeResult, type StreamStartPayload, type Success, type ThinkEndPayload, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, type ToolErrorPayload, type ToolExample, type ToolExecutionContext, type ToolExecutionSummary, ToolExecutionSummarySchema, type ToolFailure, type ToolFunction, ToolMetadataSchema, type ToolOptions, type ToolProvider, type ToolResult, type ToolResultData, ToolResultFactory, ToolResultFailureSchema, type ToolResultInit, ToolResultSchema, ToolResultSuccessSchema, type ToolRunOptions, ToolRunner, type ToolSuccess, type UnregisterHook, type Usage, UsageSchema, type VisualizationOptions, 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 };
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.0"};
1638
1639
 
1639
1640
  // src/utils/version.ts
1640
1641
  var SDK_NAME = "@opperai/agents";
@@ -2144,6 +2145,30 @@ var Agent = class extends BaseAgent {
2144
2145
  }
2145
2146
  return String(input);
2146
2147
  }
2148
+ queueSpanUpdate(context, update) {
2149
+ context.pendingSpanUpdates.push(update);
2150
+ }
2151
+ async flushPendingSpanUpdates(context) {
2152
+ const updates = context.pendingSpanUpdates.splice(
2153
+ 0,
2154
+ context.pendingSpanUpdates.length
2155
+ );
2156
+ if (updates.length === 0) {
2157
+ return;
2158
+ }
2159
+ await Promise.allSettled(
2160
+ updates.map((update) => {
2161
+ const options = {
2162
+ ...update.error !== void 0 && { error: update.error },
2163
+ ...update.startTime && { startTime: update.startTime },
2164
+ ...update.endTime && { endTime: update.endTime },
2165
+ ...update.meta && { meta: update.meta },
2166
+ ...update.name && { name: update.name }
2167
+ };
2168
+ return this.opperClient.updateSpan(update.spanId, update.output, options);
2169
+ })
2170
+ );
2171
+ }
2147
2172
  /**
2148
2173
  * Main agent loop: think → tool execution → memory handling → repeat until complete
2149
2174
  */
@@ -2205,7 +2230,9 @@ var Agent = class extends BaseAgent {
2205
2230
  }
2206
2231
  }
2207
2232
  const executionEndTime2 = /* @__PURE__ */ new Date();
2208
- await this.opperClient.updateSpan(parentSpan.id, finalResult, {
2233
+ this.queueSpanUpdate(context, {
2234
+ spanId: parentSpan.id,
2235
+ output: finalResult,
2209
2236
  startTime: executionStartTime,
2210
2237
  endTime: executionEndTime2,
2211
2238
  meta: {
@@ -2260,7 +2287,9 @@ var Agent = class extends BaseAgent {
2260
2287
  }
2261
2288
  const result = await this.generateFinalResult(input, context);
2262
2289
  const executionEndTime = /* @__PURE__ */ new Date();
2263
- await this.opperClient.updateSpan(parentSpan.id, result, {
2290
+ this.queueSpanUpdate(context, {
2291
+ spanId: parentSpan.id,
2292
+ output: result,
2264
2293
  startTime: executionStartTime,
2265
2294
  endTime: executionEndTime,
2266
2295
  meta: {
@@ -2270,7 +2299,8 @@ var Agent = class extends BaseAgent {
2270
2299
  return result;
2271
2300
  } catch (error) {
2272
2301
  const executionEndTime = /* @__PURE__ */ new Date();
2273
- await this.opperClient.updateSpan(parentSpan.id, void 0, {
2302
+ this.queueSpanUpdate(context, {
2303
+ spanId: parentSpan.id,
2274
2304
  error: error instanceof Error ? error.message : String(error),
2275
2305
  startTime: executionStartTime,
2276
2306
  endTime: executionEndTime,
@@ -2279,6 +2309,8 @@ var Agent = class extends BaseAgent {
2279
2309
  }
2280
2310
  });
2281
2311
  throw error;
2312
+ } finally {
2313
+ await this.flushPendingSpanUpdates(context);
2282
2314
  }
2283
2315
  }
2284
2316
  /**
@@ -2322,7 +2354,8 @@ var Agent = class extends BaseAgent {
2322
2354
  response
2323
2355
  });
2324
2356
  if (response.spanId) {
2325
- await this.opperClient.updateSpan(response.spanId, void 0, {
2357
+ this.queueSpanUpdate(context, {
2358
+ spanId: response.spanId,
2326
2359
  name: "think"
2327
2360
  });
2328
2361
  }
@@ -2433,7 +2466,8 @@ var Agent = class extends BaseAgent {
2433
2466
  parsed: decision
2434
2467
  });
2435
2468
  if (streamSpanId) {
2436
- await this.opperClient.updateSpan(streamSpanId, void 0, {
2469
+ this.queueSpanUpdate(context, {
2470
+ spanId: streamSpanId,
2437
2471
  name: "think"
2438
2472
  });
2439
2473
  }
@@ -2619,13 +2653,16 @@ The memory you write persists across all process() calls on this agent.`;
2619
2653
  const endTime = /* @__PURE__ */ new Date();
2620
2654
  const durationMs = endTime.getTime() - startTime.getTime();
2621
2655
  if (result.success) {
2622
- await this.opperClient.updateSpan(toolSpan.id, result.output, {
2656
+ this.queueSpanUpdate(context, {
2657
+ spanId: toolSpan.id,
2658
+ output: result.output,
2623
2659
  startTime,
2624
2660
  endTime,
2625
2661
  meta: { durationMs }
2626
2662
  });
2627
2663
  } else {
2628
- await this.opperClient.updateSpan(toolSpan.id, void 0, {
2664
+ this.queueSpanUpdate(context, {
2665
+ spanId: toolSpan.id,
2629
2666
  error: result.error instanceof Error ? result.error.message : String(result.error),
2630
2667
  startTime,
2631
2668
  endTime,
@@ -2651,7 +2688,8 @@ The memory you write persists across all process() calls on this agent.`;
2651
2688
  } catch (error) {
2652
2689
  const endTime = /* @__PURE__ */ new Date();
2653
2690
  const durationMs = endTime.getTime() - startTime.getTime();
2654
- await this.opperClient.updateSpan(toolSpan.id, void 0, {
2691
+ this.queueSpanUpdate(context, {
2692
+ spanId: toolSpan.id,
2655
2693
  error: error instanceof Error ? error.message : String(error),
2656
2694
  startTime,
2657
2695
  endTime,
@@ -2712,7 +2750,9 @@ The memory you write persists across all process() calls on this agent.`;
2712
2750
  const memoryData = await this.memory.read(keys);
2713
2751
  const endTime = /* @__PURE__ */ new Date();
2714
2752
  const durationMs = endTime.getTime() - startTime.getTime();
2715
- await this.opperClient.updateSpan(memoryReadSpan.id, memoryData, {
2753
+ this.queueSpanUpdate(context, {
2754
+ spanId: memoryReadSpan.id,
2755
+ output: memoryData,
2716
2756
  startTime,
2717
2757
  endTime,
2718
2758
  meta: { durationMs }
@@ -2779,15 +2819,13 @@ The memory you write persists across all process() calls on this agent.`;
2779
2819
  }
2780
2820
  const endTime = /* @__PURE__ */ new Date();
2781
2821
  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
- );
2822
+ this.queueSpanUpdate(context, {
2823
+ spanId: memoryWriteSpan.id,
2824
+ output: `Successfully wrote ${updateEntries.length} keys`,
2825
+ startTime,
2826
+ endTime,
2827
+ meta: { durationMs }
2828
+ });
2791
2829
  this.log(`Wrote ${updateEntries.length} memory entries`);
2792
2830
  summaries.push(
2793
2831
  ToolExecutionSummarySchema.parse({