@opperai/agents 0.1.3 → 0.2.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 +32 -0
- package/dist/index.cjs +665 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +179 -1
- package/dist/index.d.ts +179 -1
- package/dist/index.js +661 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -508,6 +508,10 @@ declare const HookEvents: {
|
|
|
508
508
|
readonly MemoryRead: "memory:read";
|
|
509
509
|
readonly MemoryWrite: "memory:write";
|
|
510
510
|
readonly MemoryError: "memory:error";
|
|
511
|
+
readonly StreamStart: "stream:start";
|
|
512
|
+
readonly StreamChunk: "stream:chunk";
|
|
513
|
+
readonly StreamEnd: "stream:end";
|
|
514
|
+
readonly StreamError: "stream:error";
|
|
511
515
|
};
|
|
512
516
|
type HookEventName = (typeof HookEvents)[keyof typeof HookEvents];
|
|
513
517
|
interface HookPayloadMap {
|
|
@@ -533,6 +537,7 @@ interface HookPayloadMap {
|
|
|
533
537
|
context: AgentContext;
|
|
534
538
|
callType: string;
|
|
535
539
|
response: unknown;
|
|
540
|
+
parsed?: unknown;
|
|
536
541
|
};
|
|
537
542
|
[HookEvents.ThinkEnd]: {
|
|
538
543
|
context: AgentContext;
|
|
@@ -570,6 +575,31 @@ interface HookPayloadMap {
|
|
|
570
575
|
operation: "read" | "write" | "delete" | "clear";
|
|
571
576
|
error: unknown;
|
|
572
577
|
};
|
|
578
|
+
[HookEvents.StreamStart]: {
|
|
579
|
+
context: AgentContext;
|
|
580
|
+
callType: string;
|
|
581
|
+
};
|
|
582
|
+
[HookEvents.StreamChunk]: {
|
|
583
|
+
context: AgentContext;
|
|
584
|
+
callType: string;
|
|
585
|
+
chunkData: {
|
|
586
|
+
delta: unknown;
|
|
587
|
+
jsonPath?: string | null;
|
|
588
|
+
chunkType?: string | null;
|
|
589
|
+
};
|
|
590
|
+
accumulated: string;
|
|
591
|
+
fieldBuffers: Record<string, string>;
|
|
592
|
+
};
|
|
593
|
+
[HookEvents.StreamEnd]: {
|
|
594
|
+
context: AgentContext;
|
|
595
|
+
callType: string;
|
|
596
|
+
fieldBuffers: Record<string, string>;
|
|
597
|
+
};
|
|
598
|
+
[HookEvents.StreamError]: {
|
|
599
|
+
context: AgentContext;
|
|
600
|
+
callType: string;
|
|
601
|
+
error: unknown;
|
|
602
|
+
};
|
|
573
603
|
}
|
|
574
604
|
type HookPayload<E extends HookEventName> = HookPayloadMap[E];
|
|
575
605
|
type HookHandler<E extends HookEventName> = (payload: HookPayload<E>) => void | Promise<void>;
|
|
@@ -592,6 +622,28 @@ declare class HookManager {
|
|
|
592
622
|
}
|
|
593
623
|
declare const createHookManager: (logger?: AgentLogger) => HookManager;
|
|
594
624
|
|
|
625
|
+
declare const AgentEvents: {
|
|
626
|
+
readonly StreamStart: "stream:start";
|
|
627
|
+
readonly StreamChunk: "stream:chunk";
|
|
628
|
+
readonly StreamEnd: "stream:end";
|
|
629
|
+
readonly StreamError: "stream:error";
|
|
630
|
+
};
|
|
631
|
+
type AgentEventName = (typeof AgentEvents)[keyof typeof AgentEvents];
|
|
632
|
+
type AgentEventPayloadMap = Pick<HookPayloadMap, AgentEventName>;
|
|
633
|
+
type AgentEventPayload<E extends AgentEventName> = AgentEventPayloadMap[E];
|
|
634
|
+
type AgentEventListener<E extends AgentEventName> = (payload: AgentEventPayload<E>) => void;
|
|
635
|
+
declare class AgentEventEmitter {
|
|
636
|
+
private readonly registry;
|
|
637
|
+
private readonly logger;
|
|
638
|
+
constructor(logger?: AgentLogger);
|
|
639
|
+
on<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
640
|
+
once<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
641
|
+
off<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): void;
|
|
642
|
+
emit<E extends AgentEventName>(event: E, payload: AgentEventPayload<E>): void;
|
|
643
|
+
removeAllListeners(event?: AgentEventName): void;
|
|
644
|
+
listenerCount(event?: AgentEventName): number;
|
|
645
|
+
}
|
|
646
|
+
|
|
595
647
|
/**
|
|
596
648
|
* Options for generating agent flow visualization
|
|
597
649
|
*/
|
|
@@ -825,6 +877,27 @@ interface BaseAgentConfig<TInput, TOutput> {
|
|
|
825
877
|
* Output schema for validation (Zod schema)
|
|
826
878
|
*/
|
|
827
879
|
outputSchema?: ZodType<TOutput>;
|
|
880
|
+
/**
|
|
881
|
+
* Enable Opper streaming APIs for LLM calls
|
|
882
|
+
* @default false
|
|
883
|
+
*/
|
|
884
|
+
enableStreaming?: boolean;
|
|
885
|
+
/**
|
|
886
|
+
* Register a handler invoked when a streaming call starts.
|
|
887
|
+
*/
|
|
888
|
+
onStreamStart?: AgentEventListener<typeof HookEvents.StreamStart>;
|
|
889
|
+
/**
|
|
890
|
+
* Register a handler invoked for each streaming chunk.
|
|
891
|
+
*/
|
|
892
|
+
onStreamChunk?: AgentEventListener<typeof HookEvents.StreamChunk>;
|
|
893
|
+
/**
|
|
894
|
+
* Register a handler invoked when a streaming call ends.
|
|
895
|
+
*/
|
|
896
|
+
onStreamEnd?: AgentEventListener<typeof HookEvents.StreamEnd>;
|
|
897
|
+
/**
|
|
898
|
+
* Register a handler invoked when streaming encounters an error.
|
|
899
|
+
*/
|
|
900
|
+
onStreamError?: AgentEventListener<typeof HookEvents.StreamError>;
|
|
828
901
|
/**
|
|
829
902
|
* Enable memory subsystem
|
|
830
903
|
* @default false
|
|
@@ -900,6 +973,10 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
900
973
|
* Whether memory is enabled
|
|
901
974
|
*/
|
|
902
975
|
readonly enableMemory: boolean;
|
|
976
|
+
/**
|
|
977
|
+
* Whether streaming is enabled
|
|
978
|
+
*/
|
|
979
|
+
readonly enableStreaming: boolean;
|
|
903
980
|
/**
|
|
904
981
|
* Memory instance for persistent storage (null if disabled or initialization failed)
|
|
905
982
|
*/
|
|
@@ -912,6 +989,10 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
912
989
|
* Hook manager for lifecycle events
|
|
913
990
|
*/
|
|
914
991
|
protected readonly hooks: HookManager;
|
|
992
|
+
/**
|
|
993
|
+
* Event dispatcher for runtime events (notably streaming)
|
|
994
|
+
*/
|
|
995
|
+
protected readonly events: AgentEventEmitter;
|
|
915
996
|
/**
|
|
916
997
|
* Registry of available tools
|
|
917
998
|
*/
|
|
@@ -1022,6 +1103,29 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1022
1103
|
* @returns Cleanup function to unregister the hook
|
|
1023
1104
|
*/
|
|
1024
1105
|
registerHook<E extends HookEventName>(event: E, handler: HookHandler<E>): () => void;
|
|
1106
|
+
/**
|
|
1107
|
+
* Register an event listener.
|
|
1108
|
+
*
|
|
1109
|
+
* @param event - Event name
|
|
1110
|
+
* @param listener - Listener callback
|
|
1111
|
+
* @returns Cleanup function to unregister the listener
|
|
1112
|
+
*/
|
|
1113
|
+
on<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
1114
|
+
/**
|
|
1115
|
+
* Register a one-time event listener that removes itself after the first call.
|
|
1116
|
+
*
|
|
1117
|
+
* @param event - Event name
|
|
1118
|
+
* @param listener - Listener callback
|
|
1119
|
+
* @returns Cleanup function (no-op once listener fires)
|
|
1120
|
+
*/
|
|
1121
|
+
once<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
1122
|
+
/**
|
|
1123
|
+
* Remove a previously registered event listener.
|
|
1124
|
+
*
|
|
1125
|
+
* @param event - Event name
|
|
1126
|
+
* @param listener - Listener callback to remove
|
|
1127
|
+
*/
|
|
1128
|
+
off<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): void;
|
|
1025
1129
|
/**
|
|
1026
1130
|
* Trigger a hook event with a payload.
|
|
1027
1131
|
* Swallows errors to prevent hook failures from breaking agent execution.
|
|
@@ -1030,6 +1134,13 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1030
1134
|
* @param payload - Event payload
|
|
1031
1135
|
*/
|
|
1032
1136
|
protected triggerHook<E extends HookEventName>(event: E, payload: Parameters<HookHandler<E>>[0]): Promise<void>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Emit a runtime event to listeners.
|
|
1139
|
+
*
|
|
1140
|
+
* @param event - Event name
|
|
1141
|
+
* @param payload - Event payload
|
|
1142
|
+
*/
|
|
1143
|
+
protected emitAgentEvent<E extends AgentEventName>(event: E, payload: AgentEventPayload<E>): void;
|
|
1033
1144
|
/**
|
|
1034
1145
|
* Execute a tool with proper context, hooks, and error handling.
|
|
1035
1146
|
*
|
|
@@ -1070,6 +1181,31 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1070
1181
|
visualizeFlow(options?: VisualizationOptions): Promise<string>;
|
|
1071
1182
|
}
|
|
1072
1183
|
|
|
1184
|
+
/**
|
|
1185
|
+
* Streaming chunk payload from Opper SSE responses.
|
|
1186
|
+
*/
|
|
1187
|
+
interface OpperStreamChunk {
|
|
1188
|
+
delta?: string | number | boolean | null | undefined;
|
|
1189
|
+
jsonPath?: string | null | undefined;
|
|
1190
|
+
spanId?: string | null | undefined;
|
|
1191
|
+
chunkType?: string | null | undefined;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Server-sent event emitted during streaming calls.
|
|
1195
|
+
*/
|
|
1196
|
+
interface OpperStreamEvent {
|
|
1197
|
+
id?: string;
|
|
1198
|
+
event?: string;
|
|
1199
|
+
retry?: number;
|
|
1200
|
+
data?: OpperStreamChunk;
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Structured response returned by Opper stream endpoints.
|
|
1204
|
+
*/
|
|
1205
|
+
interface OpperStreamResponse {
|
|
1206
|
+
headers: Record<string, string[]>;
|
|
1207
|
+
result: AsyncIterable<OpperStreamEvent>;
|
|
1208
|
+
}
|
|
1073
1209
|
/**
|
|
1074
1210
|
* Opper call response
|
|
1075
1211
|
*/
|
|
@@ -1133,6 +1269,10 @@ interface OpperCallOptions<TInput = unknown, TOutput = unknown> {
|
|
|
1133
1269
|
* Parent span ID for tracing
|
|
1134
1270
|
*/
|
|
1135
1271
|
parentSpanId?: string;
|
|
1272
|
+
/**
|
|
1273
|
+
* Abort signal used to cancel the underlying HTTP request.
|
|
1274
|
+
*/
|
|
1275
|
+
signal?: AbortSignal;
|
|
1136
1276
|
}
|
|
1137
1277
|
/**
|
|
1138
1278
|
* Span information
|
|
@@ -1191,6 +1331,10 @@ declare class OpperClient {
|
|
|
1191
1331
|
* Make a call to Opper with retry logic
|
|
1192
1332
|
*/
|
|
1193
1333
|
call<TInput = unknown, TOutput = unknown>(options: OpperCallOptions<TInput, TOutput>): Promise<OpperCallResponse<TOutput>>;
|
|
1334
|
+
/**
|
|
1335
|
+
* Stream a call to Opper with retry logic
|
|
1336
|
+
*/
|
|
1337
|
+
stream<TInput = unknown, TOutput = unknown>(options: OpperCallOptions<TInput, TOutput>): Promise<OpperStreamResponse>;
|
|
1194
1338
|
/**
|
|
1195
1339
|
* Create a span for tracing
|
|
1196
1340
|
*/
|
|
@@ -1268,6 +1412,7 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
|
|
|
1268
1412
|
* Think step: Call LLM to decide next action
|
|
1269
1413
|
*/
|
|
1270
1414
|
private think;
|
|
1415
|
+
private thinkStreaming;
|
|
1271
1416
|
/**
|
|
1272
1417
|
* Build static instructions for the think step
|
|
1273
1418
|
*/
|
|
@@ -1289,6 +1434,8 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
|
|
|
1289
1434
|
* Generate final result based on execution history
|
|
1290
1435
|
*/
|
|
1291
1436
|
private generateFinalResult;
|
|
1437
|
+
private generateFinalResultStreaming;
|
|
1438
|
+
private trackStreamingUsageBySpan;
|
|
1292
1439
|
/**
|
|
1293
1440
|
* Log helper
|
|
1294
1441
|
*/
|
|
@@ -1793,6 +1940,37 @@ declare const schemaToJson: (schema: ZodTypeAny, options?: JsonSchemaOptions) =>
|
|
|
1793
1940
|
declare const getSchemaDefault: <T>(schema: Schema<T>) => T | undefined;
|
|
1794
1941
|
declare const mergeSchemaDefaults: <T extends Record<string, unknown>>(schema: Schema<T>, value: Partial<T>) => T;
|
|
1795
1942
|
|
|
1943
|
+
declare const STREAM_ROOT_PATH = "_root";
|
|
1944
|
+
interface StreamAssemblerOptions {
|
|
1945
|
+
schema?: ZodTypeAny;
|
|
1946
|
+
}
|
|
1947
|
+
interface StreamFeedResult {
|
|
1948
|
+
path: string;
|
|
1949
|
+
accumulated: string;
|
|
1950
|
+
snapshot: Record<string, string>;
|
|
1951
|
+
}
|
|
1952
|
+
interface StreamFinalizeResult {
|
|
1953
|
+
type: "root" | "structured" | "empty";
|
|
1954
|
+
rootText?: string;
|
|
1955
|
+
structured?: unknown;
|
|
1956
|
+
}
|
|
1957
|
+
declare class StreamAssembler {
|
|
1958
|
+
private readonly displayBuffers;
|
|
1959
|
+
private readonly valueBuffers;
|
|
1960
|
+
private readonly schema;
|
|
1961
|
+
constructor(options?: StreamAssemblerOptions);
|
|
1962
|
+
feed(chunk: {
|
|
1963
|
+
delta?: unknown;
|
|
1964
|
+
jsonPath?: string | null | undefined;
|
|
1965
|
+
}): StreamFeedResult | null;
|
|
1966
|
+
snapshot(): Record<string, string>;
|
|
1967
|
+
hasStructuredFields(): boolean;
|
|
1968
|
+
finalize(): StreamFinalizeResult;
|
|
1969
|
+
getFieldBuffers(): Map<string, string>;
|
|
1970
|
+
private reconstructStructured;
|
|
1971
|
+
}
|
|
1972
|
+
declare const createStreamAssembler: (options?: StreamAssemblerOptions) => StreamAssembler;
|
|
1973
|
+
|
|
1796
1974
|
/**
|
|
1797
1975
|
* Options for creating a tool from a function
|
|
1798
1976
|
*/
|
|
@@ -1978,4 +2156,4 @@ declare class ToolRunner {
|
|
|
1978
2156
|
};
|
|
1979
2157
|
}
|
|
1980
2158
|
|
|
1981
|
-
export { Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentLogger, BaseAgent, type BaseAgentConfig, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, LogLevel, 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 MemoryUpdate, MemoryUpdateSchema, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, Result, type RetryConfig, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, type Success, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, 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, coerceToolDefinition, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createToolCallRecord, err, extractTools, generateAgentFlowDiagram, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput };
|
|
2159
|
+
export { Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, AgentEventEmitter, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, BaseAgent, type BaseAgentConfig, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, LogLevel, 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 MemoryUpdate, MemoryUpdateSchema, 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 StreamFeedResult, type StreamFinalizeResult, type Success, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, 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, coerceToolDefinition, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createStreamAssembler, createToolCallRecord, err, extractTools, generateAgentFlowDiagram, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -508,6 +508,10 @@ declare const HookEvents: {
|
|
|
508
508
|
readonly MemoryRead: "memory:read";
|
|
509
509
|
readonly MemoryWrite: "memory:write";
|
|
510
510
|
readonly MemoryError: "memory:error";
|
|
511
|
+
readonly StreamStart: "stream:start";
|
|
512
|
+
readonly StreamChunk: "stream:chunk";
|
|
513
|
+
readonly StreamEnd: "stream:end";
|
|
514
|
+
readonly StreamError: "stream:error";
|
|
511
515
|
};
|
|
512
516
|
type HookEventName = (typeof HookEvents)[keyof typeof HookEvents];
|
|
513
517
|
interface HookPayloadMap {
|
|
@@ -533,6 +537,7 @@ interface HookPayloadMap {
|
|
|
533
537
|
context: AgentContext;
|
|
534
538
|
callType: string;
|
|
535
539
|
response: unknown;
|
|
540
|
+
parsed?: unknown;
|
|
536
541
|
};
|
|
537
542
|
[HookEvents.ThinkEnd]: {
|
|
538
543
|
context: AgentContext;
|
|
@@ -570,6 +575,31 @@ interface HookPayloadMap {
|
|
|
570
575
|
operation: "read" | "write" | "delete" | "clear";
|
|
571
576
|
error: unknown;
|
|
572
577
|
};
|
|
578
|
+
[HookEvents.StreamStart]: {
|
|
579
|
+
context: AgentContext;
|
|
580
|
+
callType: string;
|
|
581
|
+
};
|
|
582
|
+
[HookEvents.StreamChunk]: {
|
|
583
|
+
context: AgentContext;
|
|
584
|
+
callType: string;
|
|
585
|
+
chunkData: {
|
|
586
|
+
delta: unknown;
|
|
587
|
+
jsonPath?: string | null;
|
|
588
|
+
chunkType?: string | null;
|
|
589
|
+
};
|
|
590
|
+
accumulated: string;
|
|
591
|
+
fieldBuffers: Record<string, string>;
|
|
592
|
+
};
|
|
593
|
+
[HookEvents.StreamEnd]: {
|
|
594
|
+
context: AgentContext;
|
|
595
|
+
callType: string;
|
|
596
|
+
fieldBuffers: Record<string, string>;
|
|
597
|
+
};
|
|
598
|
+
[HookEvents.StreamError]: {
|
|
599
|
+
context: AgentContext;
|
|
600
|
+
callType: string;
|
|
601
|
+
error: unknown;
|
|
602
|
+
};
|
|
573
603
|
}
|
|
574
604
|
type HookPayload<E extends HookEventName> = HookPayloadMap[E];
|
|
575
605
|
type HookHandler<E extends HookEventName> = (payload: HookPayload<E>) => void | Promise<void>;
|
|
@@ -592,6 +622,28 @@ declare class HookManager {
|
|
|
592
622
|
}
|
|
593
623
|
declare const createHookManager: (logger?: AgentLogger) => HookManager;
|
|
594
624
|
|
|
625
|
+
declare const AgentEvents: {
|
|
626
|
+
readonly StreamStart: "stream:start";
|
|
627
|
+
readonly StreamChunk: "stream:chunk";
|
|
628
|
+
readonly StreamEnd: "stream:end";
|
|
629
|
+
readonly StreamError: "stream:error";
|
|
630
|
+
};
|
|
631
|
+
type AgentEventName = (typeof AgentEvents)[keyof typeof AgentEvents];
|
|
632
|
+
type AgentEventPayloadMap = Pick<HookPayloadMap, AgentEventName>;
|
|
633
|
+
type AgentEventPayload<E extends AgentEventName> = AgentEventPayloadMap[E];
|
|
634
|
+
type AgentEventListener<E extends AgentEventName> = (payload: AgentEventPayload<E>) => void;
|
|
635
|
+
declare class AgentEventEmitter {
|
|
636
|
+
private readonly registry;
|
|
637
|
+
private readonly logger;
|
|
638
|
+
constructor(logger?: AgentLogger);
|
|
639
|
+
on<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
640
|
+
once<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
641
|
+
off<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): void;
|
|
642
|
+
emit<E extends AgentEventName>(event: E, payload: AgentEventPayload<E>): void;
|
|
643
|
+
removeAllListeners(event?: AgentEventName): void;
|
|
644
|
+
listenerCount(event?: AgentEventName): number;
|
|
645
|
+
}
|
|
646
|
+
|
|
595
647
|
/**
|
|
596
648
|
* Options for generating agent flow visualization
|
|
597
649
|
*/
|
|
@@ -825,6 +877,27 @@ interface BaseAgentConfig<TInput, TOutput> {
|
|
|
825
877
|
* Output schema for validation (Zod schema)
|
|
826
878
|
*/
|
|
827
879
|
outputSchema?: ZodType<TOutput>;
|
|
880
|
+
/**
|
|
881
|
+
* Enable Opper streaming APIs for LLM calls
|
|
882
|
+
* @default false
|
|
883
|
+
*/
|
|
884
|
+
enableStreaming?: boolean;
|
|
885
|
+
/**
|
|
886
|
+
* Register a handler invoked when a streaming call starts.
|
|
887
|
+
*/
|
|
888
|
+
onStreamStart?: AgentEventListener<typeof HookEvents.StreamStart>;
|
|
889
|
+
/**
|
|
890
|
+
* Register a handler invoked for each streaming chunk.
|
|
891
|
+
*/
|
|
892
|
+
onStreamChunk?: AgentEventListener<typeof HookEvents.StreamChunk>;
|
|
893
|
+
/**
|
|
894
|
+
* Register a handler invoked when a streaming call ends.
|
|
895
|
+
*/
|
|
896
|
+
onStreamEnd?: AgentEventListener<typeof HookEvents.StreamEnd>;
|
|
897
|
+
/**
|
|
898
|
+
* Register a handler invoked when streaming encounters an error.
|
|
899
|
+
*/
|
|
900
|
+
onStreamError?: AgentEventListener<typeof HookEvents.StreamError>;
|
|
828
901
|
/**
|
|
829
902
|
* Enable memory subsystem
|
|
830
903
|
* @default false
|
|
@@ -900,6 +973,10 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
900
973
|
* Whether memory is enabled
|
|
901
974
|
*/
|
|
902
975
|
readonly enableMemory: boolean;
|
|
976
|
+
/**
|
|
977
|
+
* Whether streaming is enabled
|
|
978
|
+
*/
|
|
979
|
+
readonly enableStreaming: boolean;
|
|
903
980
|
/**
|
|
904
981
|
* Memory instance for persistent storage (null if disabled or initialization failed)
|
|
905
982
|
*/
|
|
@@ -912,6 +989,10 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
912
989
|
* Hook manager for lifecycle events
|
|
913
990
|
*/
|
|
914
991
|
protected readonly hooks: HookManager;
|
|
992
|
+
/**
|
|
993
|
+
* Event dispatcher for runtime events (notably streaming)
|
|
994
|
+
*/
|
|
995
|
+
protected readonly events: AgentEventEmitter;
|
|
915
996
|
/**
|
|
916
997
|
* Registry of available tools
|
|
917
998
|
*/
|
|
@@ -1022,6 +1103,29 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1022
1103
|
* @returns Cleanup function to unregister the hook
|
|
1023
1104
|
*/
|
|
1024
1105
|
registerHook<E extends HookEventName>(event: E, handler: HookHandler<E>): () => void;
|
|
1106
|
+
/**
|
|
1107
|
+
* Register an event listener.
|
|
1108
|
+
*
|
|
1109
|
+
* @param event - Event name
|
|
1110
|
+
* @param listener - Listener callback
|
|
1111
|
+
* @returns Cleanup function to unregister the listener
|
|
1112
|
+
*/
|
|
1113
|
+
on<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
1114
|
+
/**
|
|
1115
|
+
* Register a one-time event listener that removes itself after the first call.
|
|
1116
|
+
*
|
|
1117
|
+
* @param event - Event name
|
|
1118
|
+
* @param listener - Listener callback
|
|
1119
|
+
* @returns Cleanup function (no-op once listener fires)
|
|
1120
|
+
*/
|
|
1121
|
+
once<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): () => void;
|
|
1122
|
+
/**
|
|
1123
|
+
* Remove a previously registered event listener.
|
|
1124
|
+
*
|
|
1125
|
+
* @param event - Event name
|
|
1126
|
+
* @param listener - Listener callback to remove
|
|
1127
|
+
*/
|
|
1128
|
+
off<E extends AgentEventName>(event: E, listener: AgentEventListener<E>): void;
|
|
1025
1129
|
/**
|
|
1026
1130
|
* Trigger a hook event with a payload.
|
|
1027
1131
|
* Swallows errors to prevent hook failures from breaking agent execution.
|
|
@@ -1030,6 +1134,13 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1030
1134
|
* @param payload - Event payload
|
|
1031
1135
|
*/
|
|
1032
1136
|
protected triggerHook<E extends HookEventName>(event: E, payload: Parameters<HookHandler<E>>[0]): Promise<void>;
|
|
1137
|
+
/**
|
|
1138
|
+
* Emit a runtime event to listeners.
|
|
1139
|
+
*
|
|
1140
|
+
* @param event - Event name
|
|
1141
|
+
* @param payload - Event payload
|
|
1142
|
+
*/
|
|
1143
|
+
protected emitAgentEvent<E extends AgentEventName>(event: E, payload: AgentEventPayload<E>): void;
|
|
1033
1144
|
/**
|
|
1034
1145
|
* Execute a tool with proper context, hooks, and error handling.
|
|
1035
1146
|
*
|
|
@@ -1070,6 +1181,31 @@ declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> {
|
|
|
1070
1181
|
visualizeFlow(options?: VisualizationOptions): Promise<string>;
|
|
1071
1182
|
}
|
|
1072
1183
|
|
|
1184
|
+
/**
|
|
1185
|
+
* Streaming chunk payload from Opper SSE responses.
|
|
1186
|
+
*/
|
|
1187
|
+
interface OpperStreamChunk {
|
|
1188
|
+
delta?: string | number | boolean | null | undefined;
|
|
1189
|
+
jsonPath?: string | null | undefined;
|
|
1190
|
+
spanId?: string | null | undefined;
|
|
1191
|
+
chunkType?: string | null | undefined;
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Server-sent event emitted during streaming calls.
|
|
1195
|
+
*/
|
|
1196
|
+
interface OpperStreamEvent {
|
|
1197
|
+
id?: string;
|
|
1198
|
+
event?: string;
|
|
1199
|
+
retry?: number;
|
|
1200
|
+
data?: OpperStreamChunk;
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Structured response returned by Opper stream endpoints.
|
|
1204
|
+
*/
|
|
1205
|
+
interface OpperStreamResponse {
|
|
1206
|
+
headers: Record<string, string[]>;
|
|
1207
|
+
result: AsyncIterable<OpperStreamEvent>;
|
|
1208
|
+
}
|
|
1073
1209
|
/**
|
|
1074
1210
|
* Opper call response
|
|
1075
1211
|
*/
|
|
@@ -1133,6 +1269,10 @@ interface OpperCallOptions<TInput = unknown, TOutput = unknown> {
|
|
|
1133
1269
|
* Parent span ID for tracing
|
|
1134
1270
|
*/
|
|
1135
1271
|
parentSpanId?: string;
|
|
1272
|
+
/**
|
|
1273
|
+
* Abort signal used to cancel the underlying HTTP request.
|
|
1274
|
+
*/
|
|
1275
|
+
signal?: AbortSignal;
|
|
1136
1276
|
}
|
|
1137
1277
|
/**
|
|
1138
1278
|
* Span information
|
|
@@ -1191,6 +1331,10 @@ declare class OpperClient {
|
|
|
1191
1331
|
* Make a call to Opper with retry logic
|
|
1192
1332
|
*/
|
|
1193
1333
|
call<TInput = unknown, TOutput = unknown>(options: OpperCallOptions<TInput, TOutput>): Promise<OpperCallResponse<TOutput>>;
|
|
1334
|
+
/**
|
|
1335
|
+
* Stream a call to Opper with retry logic
|
|
1336
|
+
*/
|
|
1337
|
+
stream<TInput = unknown, TOutput = unknown>(options: OpperCallOptions<TInput, TOutput>): Promise<OpperStreamResponse>;
|
|
1194
1338
|
/**
|
|
1195
1339
|
* Create a span for tracing
|
|
1196
1340
|
*/
|
|
@@ -1268,6 +1412,7 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
|
|
|
1268
1412
|
* Think step: Call LLM to decide next action
|
|
1269
1413
|
*/
|
|
1270
1414
|
private think;
|
|
1415
|
+
private thinkStreaming;
|
|
1271
1416
|
/**
|
|
1272
1417
|
* Build static instructions for the think step
|
|
1273
1418
|
*/
|
|
@@ -1289,6 +1434,8 @@ declare class Agent<TInput = unknown, TOutput = unknown> extends BaseAgent<TInpu
|
|
|
1289
1434
|
* Generate final result based on execution history
|
|
1290
1435
|
*/
|
|
1291
1436
|
private generateFinalResult;
|
|
1437
|
+
private generateFinalResultStreaming;
|
|
1438
|
+
private trackStreamingUsageBySpan;
|
|
1292
1439
|
/**
|
|
1293
1440
|
* Log helper
|
|
1294
1441
|
*/
|
|
@@ -1793,6 +1940,37 @@ declare const schemaToJson: (schema: ZodTypeAny, options?: JsonSchemaOptions) =>
|
|
|
1793
1940
|
declare const getSchemaDefault: <T>(schema: Schema<T>) => T | undefined;
|
|
1794
1941
|
declare const mergeSchemaDefaults: <T extends Record<string, unknown>>(schema: Schema<T>, value: Partial<T>) => T;
|
|
1795
1942
|
|
|
1943
|
+
declare const STREAM_ROOT_PATH = "_root";
|
|
1944
|
+
interface StreamAssemblerOptions {
|
|
1945
|
+
schema?: ZodTypeAny;
|
|
1946
|
+
}
|
|
1947
|
+
interface StreamFeedResult {
|
|
1948
|
+
path: string;
|
|
1949
|
+
accumulated: string;
|
|
1950
|
+
snapshot: Record<string, string>;
|
|
1951
|
+
}
|
|
1952
|
+
interface StreamFinalizeResult {
|
|
1953
|
+
type: "root" | "structured" | "empty";
|
|
1954
|
+
rootText?: string;
|
|
1955
|
+
structured?: unknown;
|
|
1956
|
+
}
|
|
1957
|
+
declare class StreamAssembler {
|
|
1958
|
+
private readonly displayBuffers;
|
|
1959
|
+
private readonly valueBuffers;
|
|
1960
|
+
private readonly schema;
|
|
1961
|
+
constructor(options?: StreamAssemblerOptions);
|
|
1962
|
+
feed(chunk: {
|
|
1963
|
+
delta?: unknown;
|
|
1964
|
+
jsonPath?: string | null | undefined;
|
|
1965
|
+
}): StreamFeedResult | null;
|
|
1966
|
+
snapshot(): Record<string, string>;
|
|
1967
|
+
hasStructuredFields(): boolean;
|
|
1968
|
+
finalize(): StreamFinalizeResult;
|
|
1969
|
+
getFieldBuffers(): Map<string, string>;
|
|
1970
|
+
private reconstructStructured;
|
|
1971
|
+
}
|
|
1972
|
+
declare const createStreamAssembler: (options?: StreamAssemblerOptions) => StreamAssembler;
|
|
1973
|
+
|
|
1796
1974
|
/**
|
|
1797
1975
|
* Options for creating a tool from a function
|
|
1798
1976
|
*/
|
|
@@ -1978,4 +2156,4 @@ declare class ToolRunner {
|
|
|
1978
2156
|
};
|
|
1979
2157
|
}
|
|
1980
2158
|
|
|
1981
|
-
export { Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, type AgentLogger, BaseAgent, type BaseAgentConfig, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, LogLevel, 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 MemoryUpdate, MemoryUpdateSchema, type OpperCallOptions, type OpperCallResponse, OpperClient, type OpperClientConfig, type OpperSpan, Result, type RetryConfig, type Schema, SchemaValidationError, type SchemaValidationOptions, SilentLogger, type Success, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, 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, coerceToolDefinition, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createToolCallRecord, err, extractTools, generateAgentFlowDiagram, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput };
|
|
2159
|
+
export { Agent, type AgentConfig, AgentContext, type AgentContextOptions, type AgentContextSnapshot, type AgentDecision, AgentDecisionSchema, AgentEventEmitter, type AgentEventListener, type AgentEventName, type AgentEventPayload, type AgentEventPayloadMap, AgentEvents, type AgentLogger, BaseAgent, type BaseAgentConfig, ConsoleLogger, type CreateSpanOptions, DEFAULT_MODEL, DEFAULT_RETRY_CONFIG, type ExecutionCycle, ExecutionCycleSchema, type Failure, type HookEventName, HookEvents, type HookHandler, HookManager, type HookPayload, type HookPayloadMap, type HookRegistration, InMemoryStore, type IterationSummary, type JsonSchemaOptions, LogLevel, 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 MemoryUpdate, MemoryUpdateSchema, 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 StreamFeedResult, type StreamFinalizeResult, type Success, type Thought, ThoughtSchema, type Tool, type ToolCall, type ToolCallRecord, ToolCallRecordSchema, ToolCallSchema, type ToolDefinition, 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, coerceToolDefinition, createFunctionTool, createHookManager, createInMemoryStore, createMCPServerConfig, createOpperClient, createStreamAssembler, createToolCallRecord, err, extractTools, generateAgentFlowDiagram, getDefaultLogger, getSchemaDefault, isSchemaValid, isToolProvider, mcp, mergeSchemaDefaults, normalizeToolEntries, ok, schemaToJson, setDefaultLogger, tool, validateSchema, validateToolInput };
|