@axlsdk/axl 0.1.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 +363 -0
- package/dist/chunk-EE2BCC37.js +79 -0
- package/dist/chunk-EE2BCC37.js.map +1 -0
- package/dist/index.cjs +5410 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1576 -0
- package/dist/index.d.ts +1576 -0
- package/dist/index.js +5265 -0
- package/dist/index.js.map +1 -0
- package/dist/span-manager-LGX7QHZ7.js +7 -0
- package/dist/span-manager-LGX7QHZ7.js.map +1 -0
- package/package.json +85 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1576 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
|
|
5
|
+
/** Result type for concurrent operations (spawn, map) */
|
|
6
|
+
type Result<T> = {
|
|
7
|
+
ok: true;
|
|
8
|
+
value: T;
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
error: string;
|
|
12
|
+
};
|
|
13
|
+
/** Budget execution result */
|
|
14
|
+
type BudgetResult<T> = {
|
|
15
|
+
value: T | null;
|
|
16
|
+
budgetExceeded: boolean;
|
|
17
|
+
totalCost: number;
|
|
18
|
+
};
|
|
19
|
+
/** Human decision from awaitHuman */
|
|
20
|
+
type HumanDecision = {
|
|
21
|
+
approved: true;
|
|
22
|
+
data?: string;
|
|
23
|
+
} | {
|
|
24
|
+
approved: false;
|
|
25
|
+
reason?: string;
|
|
26
|
+
};
|
|
27
|
+
/** Budget options */
|
|
28
|
+
type BudgetOptions = {
|
|
29
|
+
cost: string;
|
|
30
|
+
onExceed?: 'finish_and_stop' | 'hard_stop' | 'warn';
|
|
31
|
+
};
|
|
32
|
+
/** Map options */
|
|
33
|
+
type MapOptions = {
|
|
34
|
+
concurrency?: number;
|
|
35
|
+
quorum?: number;
|
|
36
|
+
};
|
|
37
|
+
/** Spawn options */
|
|
38
|
+
type SpawnOptions = {
|
|
39
|
+
quorum?: number;
|
|
40
|
+
};
|
|
41
|
+
/** Vote strategies */
|
|
42
|
+
type VoteStrategy = 'majority' | 'unanimous' | 'highest' | 'lowest' | 'mean' | 'median' | 'custom';
|
|
43
|
+
/** Vote options */
|
|
44
|
+
type VoteOptions<T> = {
|
|
45
|
+
strategy: VoteStrategy;
|
|
46
|
+
key?: string;
|
|
47
|
+
scorer?: (value: T) => number | Promise<number>;
|
|
48
|
+
reducer?: (values: T[]) => T | Promise<T>;
|
|
49
|
+
};
|
|
50
|
+
/** Verify options */
|
|
51
|
+
type VerifyOptions<T> = {
|
|
52
|
+
retries?: number;
|
|
53
|
+
fallback?: T;
|
|
54
|
+
};
|
|
55
|
+
/** AwaitHuman options */
|
|
56
|
+
type AwaitHumanOptions = {
|
|
57
|
+
channel: string;
|
|
58
|
+
prompt: string;
|
|
59
|
+
metadata?: Record<string, unknown>;
|
|
60
|
+
};
|
|
61
|
+
/** Ask options */
|
|
62
|
+
type AskOptions<T = unknown> = {
|
|
63
|
+
schema?: z.ZodType<T>;
|
|
64
|
+
retries?: number;
|
|
65
|
+
/** Per-call metadata passed to dynamic model/system selector functions. */
|
|
66
|
+
metadata?: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
/** Race options */
|
|
69
|
+
type RaceOptions<T = unknown> = {
|
|
70
|
+
/** Schema to validate each result. Invalid results are discarded and the race continues. */
|
|
71
|
+
schema?: z.ZodType<T>;
|
|
72
|
+
};
|
|
73
|
+
/** Execution status */
|
|
74
|
+
type ExecutionStatus = 'running' | 'completed' | 'failed' | 'waiting';
|
|
75
|
+
/** Trace event */
|
|
76
|
+
type TraceEvent = {
|
|
77
|
+
executionId: string;
|
|
78
|
+
step: number;
|
|
79
|
+
type: 'agent_call' | 'tool_call' | 'verify' | 'handoff' | 'tool_denied' | 'log' | 'workflow_start' | 'workflow_end' | 'guardrail';
|
|
80
|
+
workflow?: string;
|
|
81
|
+
agent?: string;
|
|
82
|
+
tool?: string;
|
|
83
|
+
promptVersion?: string;
|
|
84
|
+
model?: string;
|
|
85
|
+
cost?: number;
|
|
86
|
+
duration?: number;
|
|
87
|
+
data?: unknown;
|
|
88
|
+
timestamp: number;
|
|
89
|
+
};
|
|
90
|
+
/** Result of a guardrail check. */
|
|
91
|
+
type GuardrailResult = {
|
|
92
|
+
block: boolean;
|
|
93
|
+
reason?: string;
|
|
94
|
+
};
|
|
95
|
+
/** Input guardrail function. Runs before the LLM call. */
|
|
96
|
+
type InputGuardrail = (prompt: string, ctx: {
|
|
97
|
+
metadata: Record<string, unknown>;
|
|
98
|
+
}) => GuardrailResult | Promise<GuardrailResult>;
|
|
99
|
+
/** Output guardrail function. Runs after the LLM response. */
|
|
100
|
+
type OutputGuardrail = (response: string, ctx: {
|
|
101
|
+
metadata: Record<string, unknown>;
|
|
102
|
+
}) => GuardrailResult | Promise<GuardrailResult>;
|
|
103
|
+
/** Handler for when a guardrail blocks. */
|
|
104
|
+
type GuardrailBlockHandler = 'retry' | 'throw' | ((reason: string, ctx: {
|
|
105
|
+
metadata: Record<string, unknown>;
|
|
106
|
+
}) => string | Promise<string>);
|
|
107
|
+
/** Full guardrails configuration for an agent. */
|
|
108
|
+
type GuardrailsConfig = {
|
|
109
|
+
input?: InputGuardrail;
|
|
110
|
+
output?: OutputGuardrail;
|
|
111
|
+
onBlock?: GuardrailBlockHandler;
|
|
112
|
+
maxRetries?: number;
|
|
113
|
+
};
|
|
114
|
+
/** Execution info */
|
|
115
|
+
type ExecutionInfo = {
|
|
116
|
+
executionId: string;
|
|
117
|
+
workflow: string;
|
|
118
|
+
status: ExecutionStatus;
|
|
119
|
+
steps: TraceEvent[];
|
|
120
|
+
totalCost: number;
|
|
121
|
+
startedAt: number;
|
|
122
|
+
completedAt?: number;
|
|
123
|
+
duration: number;
|
|
124
|
+
error?: string;
|
|
125
|
+
};
|
|
126
|
+
type StreamEvent = {
|
|
127
|
+
type: 'token';
|
|
128
|
+
data: string;
|
|
129
|
+
} | {
|
|
130
|
+
type: 'tool_call';
|
|
131
|
+
name: string;
|
|
132
|
+
args: unknown;
|
|
133
|
+
} | {
|
|
134
|
+
type: 'tool_result';
|
|
135
|
+
name: string;
|
|
136
|
+
result: unknown;
|
|
137
|
+
} | {
|
|
138
|
+
type: 'tool_approval';
|
|
139
|
+
name: string;
|
|
140
|
+
args: unknown;
|
|
141
|
+
approved: boolean;
|
|
142
|
+
reason?: string;
|
|
143
|
+
} | {
|
|
144
|
+
type: 'agent_start';
|
|
145
|
+
agent: string;
|
|
146
|
+
model?: string;
|
|
147
|
+
} | {
|
|
148
|
+
type: 'agent_end';
|
|
149
|
+
agent: string;
|
|
150
|
+
cost?: number;
|
|
151
|
+
duration?: number;
|
|
152
|
+
} | {
|
|
153
|
+
type: 'handoff';
|
|
154
|
+
source: string;
|
|
155
|
+
target: string;
|
|
156
|
+
mode?: 'oneway' | 'roundtrip';
|
|
157
|
+
} | {
|
|
158
|
+
type: 'step';
|
|
159
|
+
step: number;
|
|
160
|
+
data: unknown;
|
|
161
|
+
} | {
|
|
162
|
+
type: 'done';
|
|
163
|
+
data: unknown;
|
|
164
|
+
} | {
|
|
165
|
+
type: 'error';
|
|
166
|
+
error: Error;
|
|
167
|
+
};
|
|
168
|
+
/** Record of an agent handoff event (persisted in session metadata). */
|
|
169
|
+
type HandoffRecord = {
|
|
170
|
+
source: string;
|
|
171
|
+
target: string;
|
|
172
|
+
mode: 'oneway' | 'roundtrip';
|
|
173
|
+
timestamp: number;
|
|
174
|
+
duration?: number;
|
|
175
|
+
};
|
|
176
|
+
/** Chat message types for provider communication */
|
|
177
|
+
type ChatRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
178
|
+
type ChatMessage = {
|
|
179
|
+
role: ChatRole;
|
|
180
|
+
content: string;
|
|
181
|
+
name?: string;
|
|
182
|
+
tool_calls?: ToolCallMessage[];
|
|
183
|
+
tool_call_id?: string;
|
|
184
|
+
};
|
|
185
|
+
type ToolCallMessage = {
|
|
186
|
+
id: string;
|
|
187
|
+
type: 'function';
|
|
188
|
+
function: {
|
|
189
|
+
name: string;
|
|
190
|
+
arguments: string;
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
/** Provider response */
|
|
194
|
+
type ProviderResponse = {
|
|
195
|
+
content: string;
|
|
196
|
+
tool_calls?: ToolCallMessage[];
|
|
197
|
+
usage?: {
|
|
198
|
+
prompt_tokens: number;
|
|
199
|
+
completion_tokens: number;
|
|
200
|
+
total_tokens: number;
|
|
201
|
+
reasoning_tokens?: number;
|
|
202
|
+
cached_tokens?: number;
|
|
203
|
+
};
|
|
204
|
+
cost?: number;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/** Descriptor for a handoff target agent with optional description. */
|
|
208
|
+
type HandoffDescriptor = {
|
|
209
|
+
agent: Agent;
|
|
210
|
+
description?: string;
|
|
211
|
+
/** Handoff mode: 'oneway' (default) exits source loop, 'roundtrip' returns result to source. */
|
|
212
|
+
mode?: 'oneway' | 'roundtrip';
|
|
213
|
+
};
|
|
214
|
+
/** Agent configuration */
|
|
215
|
+
type AgentConfig = {
|
|
216
|
+
name?: string;
|
|
217
|
+
model: string | ((ctx: {
|
|
218
|
+
metadata?: Record<string, unknown>;
|
|
219
|
+
}) => string);
|
|
220
|
+
system: string | ((ctx: {
|
|
221
|
+
metadata?: Record<string, unknown>;
|
|
222
|
+
}) => string);
|
|
223
|
+
tools?: Tool[];
|
|
224
|
+
handoffs?: HandoffDescriptor[];
|
|
225
|
+
mcp?: string[];
|
|
226
|
+
mcpTools?: string[];
|
|
227
|
+
temperature?: number;
|
|
228
|
+
maxTurns?: number;
|
|
229
|
+
timeout?: string;
|
|
230
|
+
maxContext?: number;
|
|
231
|
+
version?: string;
|
|
232
|
+
guardrails?: GuardrailsConfig;
|
|
233
|
+
};
|
|
234
|
+
/** A defined agent instance */
|
|
235
|
+
type Agent = {
|
|
236
|
+
readonly _config: AgentConfig;
|
|
237
|
+
readonly _name: string;
|
|
238
|
+
/** Direct invocation for prototyping (no workflow context) */
|
|
239
|
+
ask<T = string>(prompt: string, options?: AskOptions<T>): Promise<T>;
|
|
240
|
+
/** Resolve model string for given context */
|
|
241
|
+
resolveModel(ctx?: {
|
|
242
|
+
metadata?: Record<string, unknown>;
|
|
243
|
+
}): string;
|
|
244
|
+
/** Resolve system prompt for given context */
|
|
245
|
+
resolveSystem(ctx?: {
|
|
246
|
+
metadata?: Record<string, unknown>;
|
|
247
|
+
}): string;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Define an agent with a model, system prompt, tools, and optional handoffs.
|
|
251
|
+
* Agents are inert definitions until invoked via `ctx.ask()` or `agent.ask()`.
|
|
252
|
+
* @param config - Agent configuration: model URI, system prompt (static or dynamic), tools, temperature, etc.
|
|
253
|
+
* @returns An Agent instance that can be used with `ctx.ask()` inside workflows or called directly for prototyping.
|
|
254
|
+
*/
|
|
255
|
+
declare function agent(config: AgentConfig): Agent;
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Tool definition in OpenAI-compatible format.
|
|
259
|
+
* All providers normalize to this format internally.
|
|
260
|
+
*/
|
|
261
|
+
type ToolDefinition = {
|
|
262
|
+
type: 'function';
|
|
263
|
+
function: {
|
|
264
|
+
name: string;
|
|
265
|
+
description: string;
|
|
266
|
+
parameters: unknown;
|
|
267
|
+
strict?: boolean;
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Options passed to provider chat/stream calls.
|
|
272
|
+
*/
|
|
273
|
+
type ChatOptions = {
|
|
274
|
+
model: string;
|
|
275
|
+
temperature?: number;
|
|
276
|
+
tools?: ToolDefinition[];
|
|
277
|
+
maxTokens?: number;
|
|
278
|
+
responseFormat?: ResponseFormat;
|
|
279
|
+
stop?: string[];
|
|
280
|
+
signal?: AbortSignal;
|
|
281
|
+
reasoningEffort?: 'low' | 'medium' | 'high';
|
|
282
|
+
toolChoice?: 'auto' | 'none' | 'required' | {
|
|
283
|
+
type: 'function';
|
|
284
|
+
function: {
|
|
285
|
+
name: string;
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
/**
|
|
290
|
+
* Response format for structured output (JSON mode).
|
|
291
|
+
*/
|
|
292
|
+
type ResponseFormat = {
|
|
293
|
+
type: 'text';
|
|
294
|
+
} | {
|
|
295
|
+
type: 'json_object';
|
|
296
|
+
} | {
|
|
297
|
+
type: 'json_schema';
|
|
298
|
+
json_schema: {
|
|
299
|
+
name: string;
|
|
300
|
+
strict?: boolean;
|
|
301
|
+
schema: unknown;
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Chunks emitted during streaming.
|
|
306
|
+
*/
|
|
307
|
+
type StreamChunk = {
|
|
308
|
+
type: 'text_delta';
|
|
309
|
+
content: string;
|
|
310
|
+
} | {
|
|
311
|
+
type: 'tool_call_delta';
|
|
312
|
+
id: string;
|
|
313
|
+
name?: string;
|
|
314
|
+
arguments?: string;
|
|
315
|
+
} | {
|
|
316
|
+
type: 'done';
|
|
317
|
+
usage?: {
|
|
318
|
+
prompt_tokens: number;
|
|
319
|
+
completion_tokens: number;
|
|
320
|
+
total_tokens: number;
|
|
321
|
+
reasoning_tokens?: number;
|
|
322
|
+
cached_tokens?: number;
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* Core provider interface. Every LLM adapter must implement this.
|
|
327
|
+
*/
|
|
328
|
+
interface Provider {
|
|
329
|
+
/** Human-readable name for the provider (e.g. "openai", "anthropic") */
|
|
330
|
+
readonly name?: string;
|
|
331
|
+
/**
|
|
332
|
+
* Send a chat completion request and return the full response.
|
|
333
|
+
*/
|
|
334
|
+
chat(messages: ChatMessage[], options: ChatOptions): Promise<ProviderResponse>;
|
|
335
|
+
/**
|
|
336
|
+
* Stream a chat completion, yielding chunks as they arrive.
|
|
337
|
+
*/
|
|
338
|
+
stream(messages: ChatMessage[], options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Alias for Provider. Used for backward compatibility with index.ts exports.
|
|
342
|
+
*/
|
|
343
|
+
type ProviderAdapter = Provider;
|
|
344
|
+
|
|
345
|
+
/** Configuration for OpenTelemetry integration. */
|
|
346
|
+
type TelemetryConfig = {
|
|
347
|
+
/** Whether telemetry is enabled. Defaults to false. */
|
|
348
|
+
enabled?: boolean;
|
|
349
|
+
/** Custom TracerProvider. If not provided, uses the global OTel provider. */
|
|
350
|
+
tracerProvider?: unknown;
|
|
351
|
+
/** Tracer name. Defaults to 'axl'. */
|
|
352
|
+
serviceName?: string;
|
|
353
|
+
};
|
|
354
|
+
/** A handle to an active span for adding events and attributes. */
|
|
355
|
+
interface SpanHandle {
|
|
356
|
+
setAttribute(key: string, value: string | number | boolean): void;
|
|
357
|
+
addEvent(name: string, attributes?: Record<string, string | number | boolean>): void;
|
|
358
|
+
setStatus(code: 'ok' | 'error', message?: string): void;
|
|
359
|
+
end(): void;
|
|
360
|
+
}
|
|
361
|
+
/** Manages span creation and context propagation. */
|
|
362
|
+
interface SpanManager {
|
|
363
|
+
/** Wrap an async function in a span. Child spans created inside auto-nest. */
|
|
364
|
+
withSpanAsync<T>(name: string, attributes: Record<string, string | number | boolean>, fn: (span: SpanHandle) => Promise<T>): Promise<T>;
|
|
365
|
+
/** Add an event to the currently active span (if any). */
|
|
366
|
+
addEventToActiveSpan(name: string, attributes?: Record<string, string | number | boolean>): void;
|
|
367
|
+
/** Gracefully shut down (flush pending spans). */
|
|
368
|
+
shutdown(): Promise<void>;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** A single vector entry in the store. */
|
|
372
|
+
type VectorEntry = {
|
|
373
|
+
id: string;
|
|
374
|
+
content: string;
|
|
375
|
+
embedding: number[];
|
|
376
|
+
metadata?: Record<string, unknown>;
|
|
377
|
+
};
|
|
378
|
+
/** A result from a vector similarity search. */
|
|
379
|
+
type VectorResult = {
|
|
380
|
+
id: string;
|
|
381
|
+
content: string;
|
|
382
|
+
score: number;
|
|
383
|
+
metadata?: Record<string, unknown>;
|
|
384
|
+
};
|
|
385
|
+
/** Options for ctx.remember(). */
|
|
386
|
+
type RememberOptions = {
|
|
387
|
+
/** 'session' (default) scopes to current session, 'global' scopes to all sessions. */
|
|
388
|
+
scope?: 'session' | 'global';
|
|
389
|
+
/** Arbitrary metadata stored alongside the value. */
|
|
390
|
+
metadata?: Record<string, unknown>;
|
|
391
|
+
/** When true and a vector store is configured, also embed for semantic search. */
|
|
392
|
+
embed?: boolean;
|
|
393
|
+
};
|
|
394
|
+
/** Options for ctx.recall(). */
|
|
395
|
+
type RecallOptions = {
|
|
396
|
+
/** 'session' (default) scopes to current session, 'global' scopes to all sessions. */
|
|
397
|
+
scope?: 'session' | 'global';
|
|
398
|
+
/** When provided, performs semantic similarity search instead of exact key lookup. */
|
|
399
|
+
query?: string;
|
|
400
|
+
/** Number of results for semantic search. Defaults to 5. */
|
|
401
|
+
topK?: number;
|
|
402
|
+
};
|
|
403
|
+
/** Vector store interface for semantic memory. */
|
|
404
|
+
interface VectorStore {
|
|
405
|
+
upsert(entries: VectorEntry[]): Promise<void>;
|
|
406
|
+
search(embedding: number[], topK: number): Promise<VectorResult[]>;
|
|
407
|
+
delete(ids: string[]): Promise<void>;
|
|
408
|
+
close?(): Promise<void>;
|
|
409
|
+
}
|
|
410
|
+
/** Embedder interface for converting text to vectors. */
|
|
411
|
+
interface Embedder {
|
|
412
|
+
embed(texts: string[]): Promise<number[][]>;
|
|
413
|
+
readonly dimensions: number;
|
|
414
|
+
}
|
|
415
|
+
/** Memory configuration. */
|
|
416
|
+
type MemoryConfig = {
|
|
417
|
+
/** Vector store for semantic memory (optional). */
|
|
418
|
+
vectorStore?: VectorStore;
|
|
419
|
+
/** Embedder for converting text to vectors (required if vectorStore is set). */
|
|
420
|
+
embedder?: Embedder;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/** Provider configuration */
|
|
424
|
+
type ProviderConfig = {
|
|
425
|
+
apiKey?: string;
|
|
426
|
+
baseUrl?: string;
|
|
427
|
+
};
|
|
428
|
+
/** MCP server configuration */
|
|
429
|
+
type McpServerConfig$1 = {
|
|
430
|
+
name: string;
|
|
431
|
+
command?: string;
|
|
432
|
+
uri?: string;
|
|
433
|
+
env?: Record<string, string>;
|
|
434
|
+
};
|
|
435
|
+
/** Trace configuration */
|
|
436
|
+
type TraceConfig = {
|
|
437
|
+
enabled?: boolean;
|
|
438
|
+
level?: 'off' | 'steps' | 'full';
|
|
439
|
+
output?: 'console' | 'json' | 'file';
|
|
440
|
+
/** When true, redact prompt/response data from agent_call trace events to prevent PII leakage. */
|
|
441
|
+
redact?: boolean;
|
|
442
|
+
};
|
|
443
|
+
/** State store configuration */
|
|
444
|
+
type StateConfig = {
|
|
445
|
+
store?: 'memory' | 'sqlite' | 'redis';
|
|
446
|
+
sqlite?: {
|
|
447
|
+
path: string;
|
|
448
|
+
};
|
|
449
|
+
redis?: {
|
|
450
|
+
url: string;
|
|
451
|
+
};
|
|
452
|
+
};
|
|
453
|
+
/** Global defaults */
|
|
454
|
+
type DefaultsConfig = {
|
|
455
|
+
timeout?: string;
|
|
456
|
+
maxRetries?: number;
|
|
457
|
+
budgetPolicy?: 'finish_and_stop' | 'hard_stop' | 'warn';
|
|
458
|
+
};
|
|
459
|
+
/** Context window management configuration */
|
|
460
|
+
type ContextManagementConfig = {
|
|
461
|
+
summaryModel?: string;
|
|
462
|
+
reserveTokens?: number;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
/** Full Axl configuration */
|
|
466
|
+
type AxlConfig = {
|
|
467
|
+
providers?: Record<string, ProviderConfig>;
|
|
468
|
+
defaultProvider?: string;
|
|
469
|
+
defaultModel?: string;
|
|
470
|
+
mcp?: {
|
|
471
|
+
servers?: McpServerConfig$1[];
|
|
472
|
+
};
|
|
473
|
+
state?: StateConfig;
|
|
474
|
+
trace?: TraceConfig;
|
|
475
|
+
defaults?: DefaultsConfig;
|
|
476
|
+
contextManagement?: ContextManagementConfig;
|
|
477
|
+
memory?: MemoryConfig;
|
|
478
|
+
telemetry?: TelemetryConfig;
|
|
479
|
+
};
|
|
480
|
+
/**
|
|
481
|
+
* Create a type-safe Axl configuration object for providers, state, tracing, and defaults.
|
|
482
|
+
* @param config - The full Axl configuration (providers, state store, tracing, defaults, context management).
|
|
483
|
+
* @returns The same configuration object, validated at the type level.
|
|
484
|
+
*/
|
|
485
|
+
declare function defineConfig(config: AxlConfig): AxlConfig;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Resolved result from a provider:model URI.
|
|
489
|
+
*/
|
|
490
|
+
type ResolvedProvider = {
|
|
491
|
+
provider: Provider;
|
|
492
|
+
model: string;
|
|
493
|
+
};
|
|
494
|
+
type ProviderFactory = (config: AxlConfig) => Provider;
|
|
495
|
+
/**
|
|
496
|
+
* Registry for LLM providers. Holds cached provider instances and supports
|
|
497
|
+
* custom provider registration.
|
|
498
|
+
*
|
|
499
|
+
* Usage:
|
|
500
|
+
* const registry = new ProviderRegistry();
|
|
501
|
+
* registry.register('custom', (config) => new MyProvider(config));
|
|
502
|
+
* const { provider, model } = registry.resolve('openai:gpt-4o', config);
|
|
503
|
+
*/
|
|
504
|
+
declare class ProviderRegistry {
|
|
505
|
+
/** Cached provider instances, keyed by provider name */
|
|
506
|
+
private instances;
|
|
507
|
+
/** Factory functions, keyed by provider name */
|
|
508
|
+
private factories;
|
|
509
|
+
/** Fallback provider returned when no factory or instance matches */
|
|
510
|
+
private fallbackInstance?;
|
|
511
|
+
constructor();
|
|
512
|
+
/**
|
|
513
|
+
* Register a custom provider factory.
|
|
514
|
+
* If a provider with this name already exists, it is replaced and
|
|
515
|
+
* any cached instance is evicted.
|
|
516
|
+
*/
|
|
517
|
+
register(name: string, factory: ProviderFactory): void;
|
|
518
|
+
/**
|
|
519
|
+
* Register a pre-instantiated provider directly.
|
|
520
|
+
*/
|
|
521
|
+
registerInstance(name: string, provider: Provider): void;
|
|
522
|
+
/**
|
|
523
|
+
* Check whether a provider with the given name is registered.
|
|
524
|
+
*/
|
|
525
|
+
has(name: string): boolean;
|
|
526
|
+
/**
|
|
527
|
+
* List all registered provider names.
|
|
528
|
+
*/
|
|
529
|
+
list(): string[];
|
|
530
|
+
/**
|
|
531
|
+
* Set a fallback provider returned when no factory or instance matches.
|
|
532
|
+
* Useful for testing where a single mock provider covers all agents.
|
|
533
|
+
*/
|
|
534
|
+
setFallback(provider: Provider): void;
|
|
535
|
+
/**
|
|
536
|
+
* Get a provider instance by name, creating it lazily via its factory.
|
|
537
|
+
*/
|
|
538
|
+
get(name: string, config?: AxlConfig): Provider;
|
|
539
|
+
/**
|
|
540
|
+
* Resolve a "provider:model" URI string into a Provider instance and model name.
|
|
541
|
+
*
|
|
542
|
+
* Supported formats:
|
|
543
|
+
* - "openai:gpt-4o" -> provider=openai, model=gpt-4o
|
|
544
|
+
* - "anthropic:claude-3" -> provider=anthropic, model=claude-3
|
|
545
|
+
* - "gpt-4o" -> uses defaultProvider from config, model=gpt-4o
|
|
546
|
+
* - undefined / empty -> uses defaultProvider and defaultModel from config
|
|
547
|
+
*
|
|
548
|
+
* @param uri Provider:model string, or just a model name
|
|
549
|
+
* @param config Axl configuration for provider options and defaults
|
|
550
|
+
*/
|
|
551
|
+
resolve(uri: string | undefined, config?: AxlConfig): ResolvedProvider;
|
|
552
|
+
/**
|
|
553
|
+
* Clear all cached provider instances. Useful for testing or reconfiguration.
|
|
554
|
+
*/
|
|
555
|
+
clearCache(): void;
|
|
556
|
+
/**
|
|
557
|
+
* Clear all registered factories (including built-ins).
|
|
558
|
+
* Useful for test runtimes where only explicitly registered instances should be used.
|
|
559
|
+
*/
|
|
560
|
+
clearFactories(): void;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** A pending human decision awaiting resolution. */
|
|
564
|
+
type PendingDecision = {
|
|
565
|
+
executionId: string;
|
|
566
|
+
channel: string;
|
|
567
|
+
prompt: string;
|
|
568
|
+
metadata?: Record<string, unknown>;
|
|
569
|
+
createdAt: string;
|
|
570
|
+
};
|
|
571
|
+
/** Persisted execution state for suspend/resume. */
|
|
572
|
+
type ExecutionState = {
|
|
573
|
+
workflow: string;
|
|
574
|
+
input: unknown;
|
|
575
|
+
step: number;
|
|
576
|
+
status: 'waiting' | 'running';
|
|
577
|
+
metadata?: Record<string, unknown>;
|
|
578
|
+
};
|
|
579
|
+
/**
|
|
580
|
+
* Pluggable state persistence interface.
|
|
581
|
+
*
|
|
582
|
+
* Built-in implementations: MemoryStore (testing), SQLiteStore (file-based),
|
|
583
|
+
* Redis (production).
|
|
584
|
+
*/
|
|
585
|
+
interface StateStore {
|
|
586
|
+
saveCheckpoint(executionId: string, step: number, data: unknown): Promise<void>;
|
|
587
|
+
getCheckpoint(executionId: string, step: number): Promise<unknown | null>;
|
|
588
|
+
getLatestCheckpoint(executionId: string): Promise<{
|
|
589
|
+
step: number;
|
|
590
|
+
data: unknown;
|
|
591
|
+
} | null>;
|
|
592
|
+
saveSession(sessionId: string, history: ChatMessage[]): Promise<void>;
|
|
593
|
+
getSession(sessionId: string): Promise<ChatMessage[]>;
|
|
594
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
595
|
+
saveSessionMeta(sessionId: string, key: string, value: unknown): Promise<void>;
|
|
596
|
+
getSessionMeta(sessionId: string, key: string): Promise<unknown | null>;
|
|
597
|
+
savePendingDecision(executionId: string, decision: PendingDecision): Promise<void>;
|
|
598
|
+
getPendingDecisions(): Promise<PendingDecision[]>;
|
|
599
|
+
resolveDecision(executionId: string, result: HumanDecision): Promise<void>;
|
|
600
|
+
saveExecutionState(executionId: string, state: ExecutionState): Promise<void>;
|
|
601
|
+
getExecutionState(executionId: string): Promise<ExecutionState | null>;
|
|
602
|
+
listPendingExecutions(): Promise<string[]>;
|
|
603
|
+
/** Save a memory entry (key-value). */
|
|
604
|
+
saveMemory?(scope: string, key: string, value: unknown): Promise<void>;
|
|
605
|
+
/** Get a memory entry by key. */
|
|
606
|
+
getMemory?(scope: string, key: string): Promise<unknown | null>;
|
|
607
|
+
/** Get all memory entries for a scope. */
|
|
608
|
+
getAllMemory?(scope: string): Promise<Array<{
|
|
609
|
+
key: string;
|
|
610
|
+
value: unknown;
|
|
611
|
+
}>>;
|
|
612
|
+
/** Delete a memory entry by key. */
|
|
613
|
+
deleteMemory?(scope: string, key: string): Promise<void>;
|
|
614
|
+
close?(): Promise<void>;
|
|
615
|
+
deleteCheckpoints?(executionId: string): Promise<void>;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* MCP (Model Context Protocol) types for tool discovery and execution.
|
|
620
|
+
*/
|
|
621
|
+
/** A tool definition discovered from an MCP server. */
|
|
622
|
+
type McpToolDefinition = {
|
|
623
|
+
name: string;
|
|
624
|
+
description: string;
|
|
625
|
+
inputSchema: unknown;
|
|
626
|
+
};
|
|
627
|
+
/** Configuration for connecting to an MCP server. */
|
|
628
|
+
type McpServerConfig = {
|
|
629
|
+
name: string;
|
|
630
|
+
/** Command to spawn for stdio transport (e.g. "npx -y @modelcontextprotocol/server-fs") */
|
|
631
|
+
command?: string;
|
|
632
|
+
/** Arguments for the command */
|
|
633
|
+
args?: string[];
|
|
634
|
+
/** HTTP/SSE endpoint URI for HTTP transport */
|
|
635
|
+
uri?: string;
|
|
636
|
+
/** Environment variables to pass to the spawned process */
|
|
637
|
+
env?: Record<string, string>;
|
|
638
|
+
};
|
|
639
|
+
/** Result from calling an MCP tool. */
|
|
640
|
+
type McpToolResult = {
|
|
641
|
+
content: Array<{
|
|
642
|
+
type: 'text';
|
|
643
|
+
text: string;
|
|
644
|
+
} | {
|
|
645
|
+
type: 'image';
|
|
646
|
+
data: string;
|
|
647
|
+
mimeType: string;
|
|
648
|
+
}>;
|
|
649
|
+
isError?: boolean;
|
|
650
|
+
};
|
|
651
|
+
/** A connected MCP server with its discovered tools. */
|
|
652
|
+
type McpServer = {
|
|
653
|
+
name: string;
|
|
654
|
+
tools: McpToolDefinition[];
|
|
655
|
+
callTool(toolName: string, args: unknown): Promise<McpToolResult>;
|
|
656
|
+
close(): Promise<void>;
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Manages connections to multiple MCP servers.
|
|
661
|
+
* Discovers tools, filters by agent config, and routes tool calls.
|
|
662
|
+
*
|
|
663
|
+
* MCP tools are namespaced internally as "server:tool_name" for disambiguation
|
|
664
|
+
* and trace output, but presented to the LLM with their original names
|
|
665
|
+
* (no prefix) to avoid confusing the model.
|
|
666
|
+
*/
|
|
667
|
+
declare class McpManager {
|
|
668
|
+
private servers;
|
|
669
|
+
private initPromise;
|
|
670
|
+
/**
|
|
671
|
+
* Initialize connections to all configured MCP servers.
|
|
672
|
+
* Safe to call multiple times — subsequent calls return the original promise.
|
|
673
|
+
*/
|
|
674
|
+
initialize(configs: McpServerConfig[]): Promise<void>;
|
|
675
|
+
/**
|
|
676
|
+
* Get all tools from all connected servers.
|
|
677
|
+
*/
|
|
678
|
+
getAllTools(): Array<{
|
|
679
|
+
server: string;
|
|
680
|
+
tool: McpToolDefinition;
|
|
681
|
+
}>;
|
|
682
|
+
/**
|
|
683
|
+
* Get the qualified name for an MCP tool: "server:tool_name".
|
|
684
|
+
* Used in traces and ACL declarations.
|
|
685
|
+
*/
|
|
686
|
+
getQualifiedName(toolName: string): string | undefined;
|
|
687
|
+
/**
|
|
688
|
+
* Get tool definitions filtered for a specific agent's MCP configuration.
|
|
689
|
+
*
|
|
690
|
+
* @param agentMcp - Server names the agent has access to (undefined = all)
|
|
691
|
+
* @param agentMcpTools - Specific tool names the agent can use.
|
|
692
|
+
* Accepts both plain names ("read_file") and qualified names ("fs-server:read_file").
|
|
693
|
+
* Qualified names enable disambiguation when multiple servers expose same-named tools.
|
|
694
|
+
*/
|
|
695
|
+
getToolsForAgent(agentMcp?: string[], agentMcpTools?: string[]): Array<{
|
|
696
|
+
server: string;
|
|
697
|
+
tool: McpToolDefinition;
|
|
698
|
+
}>;
|
|
699
|
+
/**
|
|
700
|
+
* Convert MCP tools to ToolDefinition format for sending to LLMs.
|
|
701
|
+
* Tools are presented with their original names (no server prefix)
|
|
702
|
+
* to avoid confusing the LLM.
|
|
703
|
+
*/
|
|
704
|
+
getToolDefinitions(agentMcp?: string[], agentMcpTools?: string[]): ToolDefinition[];
|
|
705
|
+
/**
|
|
706
|
+
* Call a tool by name. Finds the correct server automatically.
|
|
707
|
+
*
|
|
708
|
+
* @param toolName - The tool name
|
|
709
|
+
* @param args - Arguments for the tool
|
|
710
|
+
* @param serverHint - Optional server name if known (for disambiguation)
|
|
711
|
+
*/
|
|
712
|
+
callTool(toolName: string, args: unknown, serverHint?: string): Promise<McpToolResult>;
|
|
713
|
+
/**
|
|
714
|
+
* Check if a tool name belongs to an MCP server.
|
|
715
|
+
*/
|
|
716
|
+
isMcpTool(toolName: string): boolean;
|
|
717
|
+
/**
|
|
718
|
+
* Shut down all MCP server connections.
|
|
719
|
+
*/
|
|
720
|
+
shutdown(): Promise<void>;
|
|
721
|
+
/**
|
|
722
|
+
* Get the number of connected servers.
|
|
723
|
+
*/
|
|
724
|
+
get serverCount(): number;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Coordinates key-value memory and vector store for semantic search.
|
|
729
|
+
* All key-value operations delegate to the StateStore.
|
|
730
|
+
* Vector operations use the configured VectorStore + Embedder.
|
|
731
|
+
*/
|
|
732
|
+
declare class MemoryManager {
|
|
733
|
+
private vectorStore?;
|
|
734
|
+
private embedder?;
|
|
735
|
+
constructor(options?: {
|
|
736
|
+
vectorStore?: VectorStore;
|
|
737
|
+
embedder?: Embedder;
|
|
738
|
+
});
|
|
739
|
+
/**
|
|
740
|
+
* Store a key-value pair in memory.
|
|
741
|
+
* If a vector store and embedder are configured and embed is true,
|
|
742
|
+
* also embeds the value for semantic search.
|
|
743
|
+
*/
|
|
744
|
+
remember(key: string, value: unknown, stateStore: StateStore, sessionId: string | undefined, options?: RememberOptions): Promise<void>;
|
|
745
|
+
/**
|
|
746
|
+
* Recall a value from memory by key, or perform semantic search if query is provided.
|
|
747
|
+
*/
|
|
748
|
+
recall(key: string, stateStore: StateStore, sessionId: string | undefined, options?: RecallOptions): Promise<unknown | VectorResult[] | null>;
|
|
749
|
+
/** Delete a memory entry. If a vector store is configured, also removes the embedding. */
|
|
750
|
+
forget(key: string, stateStore: StateStore, sessionId: string | undefined, options?: {
|
|
751
|
+
scope?: 'session' | 'global';
|
|
752
|
+
}): Promise<void>;
|
|
753
|
+
/** Shut down the vector store if it has a close method. */
|
|
754
|
+
close(): Promise<void>;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
type WorkflowContextInit = {
|
|
758
|
+
input: unknown;
|
|
759
|
+
executionId: string;
|
|
760
|
+
metadata?: Record<string, unknown>;
|
|
761
|
+
config: AxlConfig;
|
|
762
|
+
providerRegistry: ProviderRegistry;
|
|
763
|
+
sessionHistory?: ChatMessage[];
|
|
764
|
+
onTrace?: (event: TraceEvent) => void;
|
|
765
|
+
onToken?: (token: string) => void;
|
|
766
|
+
onToolCall?: (call: {
|
|
767
|
+
name: string;
|
|
768
|
+
args: unknown;
|
|
769
|
+
}) => void;
|
|
770
|
+
pendingDecisions?: Map<string, (d: HumanDecision) => void>;
|
|
771
|
+
budgetContext?: {
|
|
772
|
+
totalCost: number;
|
|
773
|
+
limit: number;
|
|
774
|
+
exceeded: boolean;
|
|
775
|
+
policy: string;
|
|
776
|
+
abortController?: AbortController;
|
|
777
|
+
};
|
|
778
|
+
stateStore?: StateStore;
|
|
779
|
+
signal?: AbortSignal;
|
|
780
|
+
workflowName?: string;
|
|
781
|
+
mcpManager?: McpManager;
|
|
782
|
+
/** SpanManager for OpenTelemetry instrumentation. */
|
|
783
|
+
spanManager?: SpanManager;
|
|
784
|
+
/** MemoryManager for ctx.remember() / ctx.recall() operations. */
|
|
785
|
+
memoryManager?: MemoryManager;
|
|
786
|
+
/** When true, the context replays from checkpoints before executing. */
|
|
787
|
+
resumeMode?: boolean;
|
|
788
|
+
/** Override tool handlers by name. Bypasses normal tool lookup in executeAgentCall. */
|
|
789
|
+
toolOverrides?: Map<string, (args: unknown) => Promise<unknown>>;
|
|
790
|
+
/** Handler for awaitHuman — when set, returns immediately instead of waiting for pendingDecisions. */
|
|
791
|
+
awaitHumanHandler?: (options: AwaitHumanOptions) => HumanDecision | Promise<HumanDecision>;
|
|
792
|
+
/** Callback fired when an agent LLM call is about to start. */
|
|
793
|
+
onAgentStart?: (info: {
|
|
794
|
+
agent: string;
|
|
795
|
+
model: string;
|
|
796
|
+
}) => void;
|
|
797
|
+
/** Callback fired after each ctx.ask() completes (once per ask invocation). */
|
|
798
|
+
onAgentCallComplete?: (call: {
|
|
799
|
+
agent: string;
|
|
800
|
+
prompt: string;
|
|
801
|
+
response: string;
|
|
802
|
+
model: string;
|
|
803
|
+
cost: number;
|
|
804
|
+
duration: number;
|
|
805
|
+
promptVersion?: string;
|
|
806
|
+
}) => void;
|
|
807
|
+
};
|
|
808
|
+
/**
|
|
809
|
+
* The central coordination object for all Axl primitives.
|
|
810
|
+
* Carries execution state, tracing, budget tracking, and session history.
|
|
811
|
+
*/
|
|
812
|
+
declare class WorkflowContext<TInput = unknown> {
|
|
813
|
+
readonly input: TInput;
|
|
814
|
+
readonly executionId: string;
|
|
815
|
+
readonly metadata: Record<string, unknown>;
|
|
816
|
+
private config;
|
|
817
|
+
private providerRegistry;
|
|
818
|
+
private sessionHistory;
|
|
819
|
+
private onTrace?;
|
|
820
|
+
private onToken?;
|
|
821
|
+
private onToolCall?;
|
|
822
|
+
private pendingDecisions?;
|
|
823
|
+
private budgetContext?;
|
|
824
|
+
private stateStore?;
|
|
825
|
+
private stepCounter;
|
|
826
|
+
private checkpointCounter;
|
|
827
|
+
private signal?;
|
|
828
|
+
private summaryCache?;
|
|
829
|
+
private workflowName?;
|
|
830
|
+
private mcpManager?;
|
|
831
|
+
private spanManager?;
|
|
832
|
+
private memoryManager?;
|
|
833
|
+
private resumeMode;
|
|
834
|
+
private toolOverrides?;
|
|
835
|
+
private awaitHumanHandler?;
|
|
836
|
+
private onAgentStart?;
|
|
837
|
+
private onAgentCallComplete?;
|
|
838
|
+
constructor(init: WorkflowContextInit);
|
|
839
|
+
/**
|
|
840
|
+
* Resolve the current abort signal.
|
|
841
|
+
* Branch-scoped signals (from race/spawn/map/budget) in AsyncLocalStorage
|
|
842
|
+
* take priority over the instance-level signal.
|
|
843
|
+
*/
|
|
844
|
+
private get currentSignal();
|
|
845
|
+
ask<T = string>(agent: Agent, prompt: string, options?: AskOptions<T>): Promise<T>;
|
|
846
|
+
private executeAgentCall;
|
|
847
|
+
private buildToolDefs;
|
|
848
|
+
/**
|
|
849
|
+
* Summarize old messages to fit within context window.
|
|
850
|
+
* Keeps recent messages intact, summarizes older ones.
|
|
851
|
+
*/
|
|
852
|
+
private summarizeHistory;
|
|
853
|
+
/**
|
|
854
|
+
* Execute a function with checkpoint-replay semantics.
|
|
855
|
+
*
|
|
856
|
+
* On first execution, runs `fn()`, saves the result, and returns it.
|
|
857
|
+
* On replay (resume after restart), returns the saved result without re-executing.
|
|
858
|
+
* This prevents duplicate side effects (double API calls, double refunds, etc.).
|
|
859
|
+
*/
|
|
860
|
+
checkpoint<T>(fn: () => Promise<T>): Promise<T>;
|
|
861
|
+
/**
|
|
862
|
+
* Internal checkpoint implementation shared by both the public checkpoint()
|
|
863
|
+
* and the automatic checkpointing in ask/spawn/race/parallel/map.
|
|
864
|
+
*/
|
|
865
|
+
private _checkpoint;
|
|
866
|
+
spawn<T>(n: number, fn: (index: number) => Promise<T>, options?: SpawnOptions): Promise<Result<T>[]>;
|
|
867
|
+
private _spawnImpl;
|
|
868
|
+
vote<T>(results: Result<T>[], options: VoteOptions<T>): T | Promise<T>;
|
|
869
|
+
private _voteImpl;
|
|
870
|
+
private asyncVote;
|
|
871
|
+
private majorityVote;
|
|
872
|
+
private unanimousVote;
|
|
873
|
+
private numericVote;
|
|
874
|
+
private meanVote;
|
|
875
|
+
private medianVote;
|
|
876
|
+
verify<T>(fn: (lastOutput?: unknown, errorMessage?: string) => Promise<unknown>, schema: z.ZodType<T>, options?: VerifyOptions<T>): Promise<T>;
|
|
877
|
+
budget<T>(options: BudgetOptions, fn: () => Promise<T>): Promise<BudgetResult<T>>;
|
|
878
|
+
/** Get the current budget status, or null if not inside a budget block. */
|
|
879
|
+
getBudgetStatus(): {
|
|
880
|
+
spent: number;
|
|
881
|
+
limit: number;
|
|
882
|
+
remaining: number;
|
|
883
|
+
} | null;
|
|
884
|
+
race<T>(fns: Array<() => Promise<T>>, options?: RaceOptions<T>): Promise<T>;
|
|
885
|
+
private _raceImpl;
|
|
886
|
+
parallel<T extends unknown[]>(fns: {
|
|
887
|
+
[K in keyof T]: () => Promise<T[K]>;
|
|
888
|
+
}): Promise<T>;
|
|
889
|
+
map<T, U>(items: T[], fn: (item: T, index: number) => Promise<U>, options?: MapOptions): Promise<Result<U>[]>;
|
|
890
|
+
private _mapImpl;
|
|
891
|
+
awaitHuman(options: AwaitHumanOptions): Promise<HumanDecision>;
|
|
892
|
+
private _awaitHumanImpl;
|
|
893
|
+
log(event: string, data?: unknown): void;
|
|
894
|
+
/**
|
|
895
|
+
* Store a value in memory, scoped to the current session (default) or globally.
|
|
896
|
+
* When a vector store is configured, the value is also embedded for semantic recall.
|
|
897
|
+
*/
|
|
898
|
+
remember(key: string, value: unknown, options?: RememberOptions): Promise<void>;
|
|
899
|
+
/**
|
|
900
|
+
* Recall a value from memory by key, or perform semantic search if query option is provided.
|
|
901
|
+
*/
|
|
902
|
+
recall(key: string, options?: RecallOptions): Promise<unknown | VectorResult[] | null>;
|
|
903
|
+
/** Delete a memory entry by key. */
|
|
904
|
+
forget(key: string, options?: {
|
|
905
|
+
scope?: 'session' | 'global';
|
|
906
|
+
}): Promise<void>;
|
|
907
|
+
private emitTrace;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** Retry policy for tool handlers */
|
|
911
|
+
type RetryPolicy = {
|
|
912
|
+
attempts?: number;
|
|
913
|
+
backoff?: 'none' | 'linear' | 'exponential';
|
|
914
|
+
on?: (error: Error & {
|
|
915
|
+
status?: number;
|
|
916
|
+
}) => boolean;
|
|
917
|
+
};
|
|
918
|
+
/** Lifecycle hooks for tool execution. */
|
|
919
|
+
type ToolHooks<TInput = unknown, TOutput = unknown> = {
|
|
920
|
+
/** Transform input before the handler runs. Receives parsed input and workflow context. */
|
|
921
|
+
before?: (input: TInput, ctx: WorkflowContext) => TInput | Promise<TInput>;
|
|
922
|
+
/** Transform output after the handler runs. Receives handler result and workflow context. */
|
|
923
|
+
after?: (output: TOutput, ctx: WorkflowContext) => TOutput | Promise<TOutput>;
|
|
924
|
+
};
|
|
925
|
+
/** Tool configuration */
|
|
926
|
+
type ToolConfig<TInput extends z.ZodTypeAny, TOutput = unknown> = {
|
|
927
|
+
name: string;
|
|
928
|
+
description: string;
|
|
929
|
+
input: TInput;
|
|
930
|
+
handler: (input: z.infer<TInput>) => TOutput | Promise<TOutput>;
|
|
931
|
+
retry?: RetryPolicy;
|
|
932
|
+
sensitive?: boolean;
|
|
933
|
+
/** Maximum string length for any string argument. Default: 10000. Set to 0 to disable. */
|
|
934
|
+
maxStringLength?: number;
|
|
935
|
+
/** When true, agent-initiated calls trigger ctx.awaitHuman() before execution. */
|
|
936
|
+
requireApproval?: boolean;
|
|
937
|
+
/** Lifecycle hooks: before/after the handler. */
|
|
938
|
+
hooks?: ToolHooks<z.infer<TInput>, TOutput>;
|
|
939
|
+
};
|
|
940
|
+
/** A defined tool instance */
|
|
941
|
+
type Tool<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput = unknown> = {
|
|
942
|
+
readonly name: string;
|
|
943
|
+
readonly description: string;
|
|
944
|
+
readonly inputSchema: TInput;
|
|
945
|
+
readonly sensitive: boolean;
|
|
946
|
+
readonly retry: RetryPolicy;
|
|
947
|
+
readonly requireApproval: boolean;
|
|
948
|
+
readonly hooks?: ToolHooks<z.infer<TInput>, TOutput>;
|
|
949
|
+
/** Run the tool directly from workflow code */
|
|
950
|
+
run(ctx: WorkflowContext, input: z.infer<TInput>): Promise<TOutput>;
|
|
951
|
+
/** Execute the handler (internal use — includes retry logic) */
|
|
952
|
+
_execute(input: z.infer<TInput>): Promise<TOutput>;
|
|
953
|
+
};
|
|
954
|
+
/**
|
|
955
|
+
* Define a tool with Zod-validated input, a handler function, and optional retry policy.
|
|
956
|
+
* @param config - Tool configuration: name, description, input schema, handler, retry, and sensitivity options.
|
|
957
|
+
* @returns A Tool instance that can be attached to agents and invoked via `tool.run()` or agent tool calling.
|
|
958
|
+
*/
|
|
959
|
+
declare function tool<TInput extends z.ZodTypeAny, TOutput = unknown>(config: ToolConfig<TInput, TOutput>): Tool<TInput, TOutput>;
|
|
960
|
+
|
|
961
|
+
/** Workflow configuration */
|
|
962
|
+
type WorkflowConfig<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny> = {
|
|
963
|
+
name: string;
|
|
964
|
+
input: TInput;
|
|
965
|
+
output?: TOutput;
|
|
966
|
+
handler: (ctx: WorkflowContext<z.infer<TInput>>) => Promise<z.infer<TOutput>>;
|
|
967
|
+
};
|
|
968
|
+
/** A defined workflow instance */
|
|
969
|
+
type Workflow<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny> = {
|
|
970
|
+
readonly name: string;
|
|
971
|
+
readonly inputSchema: TInput;
|
|
972
|
+
readonly outputSchema: TOutput | undefined;
|
|
973
|
+
readonly handler: (ctx: WorkflowContext<z.infer<TInput>>) => Promise<z.infer<TOutput>>;
|
|
974
|
+
};
|
|
975
|
+
/**
|
|
976
|
+
* Define a named workflow with Zod-validated input/output and an async handler.
|
|
977
|
+
* Register workflows with `AxlRuntime.register()` to execute them.
|
|
978
|
+
* @param config - Workflow configuration: name, input schema, optional output schema, and async handler receiving a WorkflowContext.
|
|
979
|
+
* @returns A Workflow instance ready to be registered with an AxlRuntime.
|
|
980
|
+
*/
|
|
981
|
+
declare function workflow<TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny>(config: WorkflowConfig<TInput, TOutput>): Workflow<TInput, TOutput>;
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* A streamable workflow execution.
|
|
985
|
+
*
|
|
986
|
+
* Extends Node's Readable and implements AsyncIterable<StreamEvent>.
|
|
987
|
+
* Supports .on() events, for-await-of, .text iterator, and .pipe().
|
|
988
|
+
*/
|
|
989
|
+
declare class AxlStream extends Readable {
|
|
990
|
+
private bus;
|
|
991
|
+
private tokens;
|
|
992
|
+
private result;
|
|
993
|
+
private finished;
|
|
994
|
+
readonly promise: Promise<unknown>;
|
|
995
|
+
private eventQueue;
|
|
996
|
+
private waiters;
|
|
997
|
+
constructor();
|
|
998
|
+
private static readonly STREAM_EVENTS;
|
|
999
|
+
on(event: string, handler: (...args: unknown[]) => void): this;
|
|
1000
|
+
off(event: string, handler: (...args: unknown[]) => void): this;
|
|
1001
|
+
[Symbol.asyncIterator](): {
|
|
1002
|
+
next: () => Promise<IteratorResult<StreamEvent>>;
|
|
1003
|
+
[Symbol.asyncIterator](): /*elided*/ any;
|
|
1004
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
1005
|
+
};
|
|
1006
|
+
get text(): AsyncIterable<string>;
|
|
1007
|
+
get steps(): AsyncIterable<StreamEvent>;
|
|
1008
|
+
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
|
|
1009
|
+
end?: boolean;
|
|
1010
|
+
}): T;
|
|
1011
|
+
/** Push a stream event. Called by the runtime. */
|
|
1012
|
+
_push(event: StreamEvent): void;
|
|
1013
|
+
/** Signal successful completion. */
|
|
1014
|
+
_done(result: unknown): void;
|
|
1015
|
+
/** Signal an error. */
|
|
1016
|
+
_error(error: Error): void;
|
|
1017
|
+
get fullText(): string;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/** Options for configuring a session. */
|
|
1021
|
+
type SessionOptions = {
|
|
1022
|
+
/** History management options. */
|
|
1023
|
+
history?: {
|
|
1024
|
+
/** Maximum number of messages to keep in history. Older messages are trimmed (or summarized if summarize is true). */
|
|
1025
|
+
maxMessages?: number;
|
|
1026
|
+
/** When true and maxMessages is exceeded, summarize old messages instead of dropping them. Requires summaryModel to be set. Default: false. */
|
|
1027
|
+
summarize?: boolean;
|
|
1028
|
+
/** Model URI to use for summarization (e.g., 'openai:gpt-4o-mini'). Required when summarize is true. */
|
|
1029
|
+
summaryModel?: string;
|
|
1030
|
+
};
|
|
1031
|
+
/** Whether to persist session history to the state store. Default: true. */
|
|
1032
|
+
persist?: boolean;
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* A stateful conversation session.
|
|
1036
|
+
* Persists message history across multiple interactions.
|
|
1037
|
+
*/
|
|
1038
|
+
declare class Session {
|
|
1039
|
+
private sessionId;
|
|
1040
|
+
private runtime;
|
|
1041
|
+
private store;
|
|
1042
|
+
private closed;
|
|
1043
|
+
private options;
|
|
1044
|
+
constructor(sessionId: string, runtime: AxlRuntime, store: StateStore, options?: SessionOptions);
|
|
1045
|
+
get id(): string;
|
|
1046
|
+
send(workflowName: string, input: unknown): Promise<unknown>;
|
|
1047
|
+
stream(workflowName: string, input: unknown): Promise<AxlStream>;
|
|
1048
|
+
history(): Promise<ChatMessage[]>;
|
|
1049
|
+
/** Get the handoff history for this session. */
|
|
1050
|
+
handoffs(): Promise<HandoffRecord[]>;
|
|
1051
|
+
end(): Promise<void>;
|
|
1052
|
+
fork(newId: string): Promise<Session>;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
type ExecuteOptions = {
|
|
1056
|
+
metadata?: Record<string, unknown>;
|
|
1057
|
+
};
|
|
1058
|
+
/**
|
|
1059
|
+
* The main entry point for executing Axl workflows.
|
|
1060
|
+
* Manages workflow registration, provider resolution, state storage, tracing, MCP servers,
|
|
1061
|
+
* and human-in-the-loop decision handling. Supports both synchronous (`execute`) and
|
|
1062
|
+
* streaming (`stream`) execution modes, as well as multi-turn sessions.
|
|
1063
|
+
*/
|
|
1064
|
+
declare class AxlRuntime extends EventEmitter {
|
|
1065
|
+
private config;
|
|
1066
|
+
private workflows;
|
|
1067
|
+
private providerRegistry;
|
|
1068
|
+
private stateStore;
|
|
1069
|
+
private executions;
|
|
1070
|
+
private pendingDecisionResolvers;
|
|
1071
|
+
private abortControllers;
|
|
1072
|
+
private mcpManager?;
|
|
1073
|
+
private memoryManager?;
|
|
1074
|
+
private spanManager;
|
|
1075
|
+
constructor(config?: AxlConfig);
|
|
1076
|
+
/**
|
|
1077
|
+
* Initialize MCP servers configured in the config.
|
|
1078
|
+
* Call this before executing workflows that use MCP tools.
|
|
1079
|
+
*/
|
|
1080
|
+
initializeMcp(): Promise<void>;
|
|
1081
|
+
/**
|
|
1082
|
+
* Initialize OpenTelemetry telemetry based on config.
|
|
1083
|
+
* Call this before executing workflows to enable span creation.
|
|
1084
|
+
*/
|
|
1085
|
+
initializeTelemetry(): Promise<void>;
|
|
1086
|
+
/** Get the MCP manager (if initialized). */
|
|
1087
|
+
getMcpManager(): McpManager | undefined;
|
|
1088
|
+
private createStateStore;
|
|
1089
|
+
/** Register a workflow with the runtime. */
|
|
1090
|
+
register(workflow: Workflow): void;
|
|
1091
|
+
/** Register a custom provider instance. */
|
|
1092
|
+
registerProvider(name: string, provider: Provider): void;
|
|
1093
|
+
/** Execute a workflow and return the result. */
|
|
1094
|
+
execute(name: string, input: unknown, options?: ExecuteOptions): Promise<unknown>;
|
|
1095
|
+
/** Execute a workflow and return a stream. */
|
|
1096
|
+
stream(name: string, input: unknown, options?: ExecuteOptions): AxlStream;
|
|
1097
|
+
/** Create or resume a session. */
|
|
1098
|
+
session(id: string, options?: SessionOptions): Session;
|
|
1099
|
+
/** Gracefully shut down the runtime, aborting in-flight executions and closing state stores and MCP servers. */
|
|
1100
|
+
shutdown(): Promise<void>;
|
|
1101
|
+
/** Abort a running execution by its ID. */
|
|
1102
|
+
abort(executionId: string): void;
|
|
1103
|
+
/** Get execution details by ID. */
|
|
1104
|
+
getExecution(executionId: string): Promise<ExecutionInfo | undefined>;
|
|
1105
|
+
/** List pending human decisions. */
|
|
1106
|
+
getPendingDecisions(): Promise<PendingDecision[]>;
|
|
1107
|
+
/** Resolve a pending human decision. */
|
|
1108
|
+
resolveDecision(executionId: string, decision: HumanDecision): Promise<void>;
|
|
1109
|
+
/**
|
|
1110
|
+
* Resume a specific execution that was waiting for a human decision.
|
|
1111
|
+
* Re-runs the workflow from scratch; the workflow should use checkpoint-replay
|
|
1112
|
+
* to skip already-completed steps and pick up from the awaitHuman point.
|
|
1113
|
+
*/
|
|
1114
|
+
resumeExecution(executionId: string): Promise<unknown>;
|
|
1115
|
+
/**
|
|
1116
|
+
* Resume all pending executions that were waiting for human decisions.
|
|
1117
|
+
* Call this on startup to resume workflows that were interrupted by a restart.
|
|
1118
|
+
* Returns the execution IDs that were resumed.
|
|
1119
|
+
*/
|
|
1120
|
+
resumePending(): Promise<string[]>;
|
|
1121
|
+
/**
|
|
1122
|
+
* Summarize a list of chat messages into a concise summary string.
|
|
1123
|
+
* Used by Session to summarize dropped messages when history.summarize is enabled.
|
|
1124
|
+
*/
|
|
1125
|
+
summarizeMessages(messages: ChatMessage[], modelUri: string): Promise<string>;
|
|
1126
|
+
/** Get the state store (for testing and advanced use cases). */
|
|
1127
|
+
getStateStore(): StateStore;
|
|
1128
|
+
/**
|
|
1129
|
+
* Run an evaluation against a registered workflow.
|
|
1130
|
+
* Requires `axl-eval` as a peer dependency.
|
|
1131
|
+
*
|
|
1132
|
+
* @see Spec Section 13.5
|
|
1133
|
+
*/
|
|
1134
|
+
eval(config: {
|
|
1135
|
+
workflow: string;
|
|
1136
|
+
dataset: any;
|
|
1137
|
+
scorers: any[];
|
|
1138
|
+
concurrency?: number;
|
|
1139
|
+
budget?: string;
|
|
1140
|
+
metadata?: Record<string, any>;
|
|
1141
|
+
}): Promise<any>;
|
|
1142
|
+
/**
|
|
1143
|
+
* Compare two evaluation results to detect regressions and improvements.
|
|
1144
|
+
* Requires `axl-eval` as a peer dependency.
|
|
1145
|
+
*
|
|
1146
|
+
* @see Spec Section 13.6
|
|
1147
|
+
*/
|
|
1148
|
+
evalCompare(baseline: any, candidate: any): Promise<any>;
|
|
1149
|
+
/**
|
|
1150
|
+
* Handle trace event output based on configuration.
|
|
1151
|
+
*
|
|
1152
|
+
* When trace is disabled or level is 'off', events are still emitted via
|
|
1153
|
+
* EventEmitter (for programmatic subscribers) but nothing is logged to console.
|
|
1154
|
+
* The emit('trace', event) call happens before this method is called, so
|
|
1155
|
+
* programmatic subscribers always receive events regardless of trace config.
|
|
1156
|
+
*/
|
|
1157
|
+
private outputTraceEvent;
|
|
1158
|
+
private logTraceEvent;
|
|
1159
|
+
/**
|
|
1160
|
+
* Append a handoff record to session metadata.
|
|
1161
|
+
* Note: The read-modify-write is not atomic. Concurrent handoffs in the same
|
|
1162
|
+
* session could lose a record. In practice, trace events within a single
|
|
1163
|
+
* execution are sequential (same event loop), so this is only a concern for
|
|
1164
|
+
* cross-execution concurrency on the same session, which is unlikely.
|
|
1165
|
+
*/
|
|
1166
|
+
private appendHandoffRecord;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
/** Base error class for all Axl errors */
|
|
1170
|
+
declare class AxlError extends Error {
|
|
1171
|
+
readonly code: string;
|
|
1172
|
+
constructor(code: string, message: string);
|
|
1173
|
+
}
|
|
1174
|
+
/** Thrown when schema validation fails after all retries exhausted */
|
|
1175
|
+
declare class VerifyError extends AxlError {
|
|
1176
|
+
readonly lastOutput: unknown;
|
|
1177
|
+
readonly zodError: ZodError;
|
|
1178
|
+
readonly retries: number;
|
|
1179
|
+
constructor(lastOutput: unknown, zodError: ZodError, retries: number);
|
|
1180
|
+
}
|
|
1181
|
+
/** Thrown when quorum is not met in spawn */
|
|
1182
|
+
declare class QuorumNotMet extends AxlError {
|
|
1183
|
+
readonly results: Result<unknown>[];
|
|
1184
|
+
constructor(required: number, actual: number, results: Result<unknown>[]);
|
|
1185
|
+
}
|
|
1186
|
+
/** Thrown when vote cannot reach consensus */
|
|
1187
|
+
declare class NoConsensus extends AxlError {
|
|
1188
|
+
constructor(reason: string);
|
|
1189
|
+
}
|
|
1190
|
+
/** Thrown when an operation exceeds its timeout */
|
|
1191
|
+
declare class TimeoutError extends AxlError {
|
|
1192
|
+
constructor(operation: string, timeoutMs: number);
|
|
1193
|
+
}
|
|
1194
|
+
/** Thrown when a budget limit is exceeded */
|
|
1195
|
+
declare class BudgetExceededError extends AxlError {
|
|
1196
|
+
readonly limit: number;
|
|
1197
|
+
readonly spent: number;
|
|
1198
|
+
readonly policy: string;
|
|
1199
|
+
constructor(limit: number, spent: number, policy: string);
|
|
1200
|
+
}
|
|
1201
|
+
/** Thrown when an agent exceeds its maximum number of tool-calling turns */
|
|
1202
|
+
declare class MaxTurnsError extends AxlError {
|
|
1203
|
+
readonly maxTurns: number;
|
|
1204
|
+
constructor(operation: string, maxTurns: number);
|
|
1205
|
+
}
|
|
1206
|
+
/** Thrown when a guardrail blocks a request/response and the policy is 'throw'. */
|
|
1207
|
+
declare class GuardrailError extends AxlError {
|
|
1208
|
+
readonly guardrailType: 'input' | 'output';
|
|
1209
|
+
readonly reason: string;
|
|
1210
|
+
constructor(guardrailType: 'input' | 'output', reason: string);
|
|
1211
|
+
}
|
|
1212
|
+
/** Internal: thrown when an agent tries to call a tool not in its ACL */
|
|
1213
|
+
declare class ToolDenied extends AxlError {
|
|
1214
|
+
readonly toolName: string;
|
|
1215
|
+
readonly agentName: string;
|
|
1216
|
+
constructor(toolName: string, agentName: string);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* OpenAI-compatible provider using raw fetch (no SDK dependency).
|
|
1221
|
+
*
|
|
1222
|
+
* Supports:
|
|
1223
|
+
* - Chat completions
|
|
1224
|
+
* - Tool calling
|
|
1225
|
+
* - Streaming via SSE
|
|
1226
|
+
* - Structured output via response_format (JSON mode / JSON schema)
|
|
1227
|
+
* - Reasoning models (o1/o3/o4-mini) with developer role and reasoning_effort
|
|
1228
|
+
*/
|
|
1229
|
+
declare class OpenAIProvider implements Provider {
|
|
1230
|
+
readonly name = "openai";
|
|
1231
|
+
private baseUrl;
|
|
1232
|
+
private apiKey;
|
|
1233
|
+
constructor(options?: {
|
|
1234
|
+
apiKey?: string;
|
|
1235
|
+
baseUrl?: string;
|
|
1236
|
+
});
|
|
1237
|
+
chat(messages: ChatMessage[], options: ChatOptions): Promise<ProviderResponse>;
|
|
1238
|
+
stream(messages: ChatMessage[], options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
1239
|
+
private buildRequestBody;
|
|
1240
|
+
/** Extract a human-readable message from an API error response body. */
|
|
1241
|
+
private extractErrorMessage;
|
|
1242
|
+
private formatMessage;
|
|
1243
|
+
private parseSSEStream;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
/**
|
|
1247
|
+
* OpenAI Responses API provider using raw fetch (no SDK dependency).
|
|
1248
|
+
*
|
|
1249
|
+
* Maps the standard Provider interface to OpenAI's Responses API (`POST /v1/responses`).
|
|
1250
|
+
* The Responses API is OpenAI's recommended path forward with better caching,
|
|
1251
|
+
* built-in tools, and native reasoning support.
|
|
1252
|
+
*/
|
|
1253
|
+
declare class OpenAIResponsesProvider implements Provider {
|
|
1254
|
+
readonly name = "openai-responses";
|
|
1255
|
+
private baseUrl;
|
|
1256
|
+
private apiKey;
|
|
1257
|
+
constructor(options?: {
|
|
1258
|
+
apiKey?: string;
|
|
1259
|
+
baseUrl?: string;
|
|
1260
|
+
});
|
|
1261
|
+
chat(messages: ChatMessage[], options: ChatOptions): Promise<ProviderResponse>;
|
|
1262
|
+
stream(messages: ChatMessage[], options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
1263
|
+
private buildRequestBody;
|
|
1264
|
+
private buildInput;
|
|
1265
|
+
/**
|
|
1266
|
+
* The Responses API uses `text.format` instead of `response_format`.
|
|
1267
|
+
* For `json_schema`, the schema fields are flattened into the format object
|
|
1268
|
+
* rather than nested under a `json_schema` key.
|
|
1269
|
+
*
|
|
1270
|
+
* Chat Completions: `{ type: "json_schema", json_schema: { name, strict, schema } }`
|
|
1271
|
+
* Responses API: `{ type: "json_schema", name, strict, schema }`
|
|
1272
|
+
*/
|
|
1273
|
+
private mapResponseFormat;
|
|
1274
|
+
private parseResponse;
|
|
1275
|
+
private parseSSEStream;
|
|
1276
|
+
private handleStreamEvent;
|
|
1277
|
+
private extractErrorMessage;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* Anthropic provider using raw fetch (no SDK dependency).
|
|
1282
|
+
*
|
|
1283
|
+
* Supports:
|
|
1284
|
+
* - Chat completions via /v1/messages
|
|
1285
|
+
* - Tool calling (tool_use / tool_result content blocks)
|
|
1286
|
+
* - Streaming via SSE
|
|
1287
|
+
*
|
|
1288
|
+
* Message mapping:
|
|
1289
|
+
* - "system" role messages are extracted and sent as the top-level `system` param
|
|
1290
|
+
* - "tool" role messages are mapped to user messages with tool_result content blocks
|
|
1291
|
+
* - "assistant" messages with tool_calls are mapped to tool_use content blocks
|
|
1292
|
+
*/
|
|
1293
|
+
declare class AnthropicProvider implements Provider {
|
|
1294
|
+
readonly name = "anthropic";
|
|
1295
|
+
private baseUrl;
|
|
1296
|
+
private apiKey;
|
|
1297
|
+
private currentModel?;
|
|
1298
|
+
constructor(options?: {
|
|
1299
|
+
apiKey?: string;
|
|
1300
|
+
baseUrl?: string;
|
|
1301
|
+
});
|
|
1302
|
+
chat(messages: ChatMessage[], options: ChatOptions): Promise<ProviderResponse>;
|
|
1303
|
+
stream(messages: ChatMessage[], options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
1304
|
+
private buildHeaders;
|
|
1305
|
+
/** Extract a human-readable message from an API error response body. */
|
|
1306
|
+
private extractErrorMessage;
|
|
1307
|
+
private buildRequestBody;
|
|
1308
|
+
/**
|
|
1309
|
+
* Map OpenAI-format ChatMessages to Anthropic message format.
|
|
1310
|
+
*
|
|
1311
|
+
* Key transformations:
|
|
1312
|
+
* - assistant messages with tool_calls -> assistant with tool_use content blocks
|
|
1313
|
+
* - tool messages (tool results) -> user messages with tool_result content blocks
|
|
1314
|
+
*/
|
|
1315
|
+
private mapMessages;
|
|
1316
|
+
/**
|
|
1317
|
+
* Merge consecutive messages with the same role into a single message.
|
|
1318
|
+
* This handles cases where multiple tool_result blocks need to be in one user message.
|
|
1319
|
+
*/
|
|
1320
|
+
private mergeConsecutiveRoles;
|
|
1321
|
+
private toContentBlocks;
|
|
1322
|
+
/**
|
|
1323
|
+
* Map an OpenAI-format ToolDefinition to Anthropic's tool format.
|
|
1324
|
+
*/
|
|
1325
|
+
private mapToolDefinition;
|
|
1326
|
+
private parseResponse;
|
|
1327
|
+
private parseSSEStream;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* Google Gemini provider using raw fetch (no SDK dependency).
|
|
1332
|
+
*
|
|
1333
|
+
* Supports:
|
|
1334
|
+
* - Chat completions via generateContent
|
|
1335
|
+
* - Tool calling (functionCall / functionResponse)
|
|
1336
|
+
* - Streaming via SSE (streamGenerateContent)
|
|
1337
|
+
* - Structured output via responseMimeType / responseSchema
|
|
1338
|
+
*
|
|
1339
|
+
* Message mapping:
|
|
1340
|
+
* - "system" role messages are extracted into the top-level `system_instruction` param
|
|
1341
|
+
* - "assistant" role is mapped to "model" role
|
|
1342
|
+
* - "tool" role messages are mapped to user messages with functionResponse parts
|
|
1343
|
+
* - assistant messages with tool_calls are mapped to functionCall parts
|
|
1344
|
+
*/
|
|
1345
|
+
declare class GeminiProvider implements Provider {
|
|
1346
|
+
readonly name = "google";
|
|
1347
|
+
private baseUrl;
|
|
1348
|
+
private apiKey;
|
|
1349
|
+
private callCounter;
|
|
1350
|
+
constructor(options?: {
|
|
1351
|
+
apiKey?: string;
|
|
1352
|
+
baseUrl?: string;
|
|
1353
|
+
});
|
|
1354
|
+
chat(messages: ChatMessage[], options: ChatOptions): Promise<ProviderResponse>;
|
|
1355
|
+
stream(messages: ChatMessage[], options: ChatOptions): AsyncGenerator<StreamChunk>;
|
|
1356
|
+
private buildHeaders;
|
|
1357
|
+
private extractErrorMessage;
|
|
1358
|
+
private buildRequestBody;
|
|
1359
|
+
/**
|
|
1360
|
+
* Map OpenAI-format ChatMessages to Gemini content format.
|
|
1361
|
+
*
|
|
1362
|
+
* Key transformations:
|
|
1363
|
+
* - assistant role -> model role
|
|
1364
|
+
* - assistant messages with tool_calls -> model messages with functionCall parts
|
|
1365
|
+
* - tool messages -> user messages with functionResponse parts
|
|
1366
|
+
*
|
|
1367
|
+
* Two-pass approach: first build a tool_call_id -> function name mapping
|
|
1368
|
+
* from assistant messages, then use it when mapping tool result messages.
|
|
1369
|
+
*/
|
|
1370
|
+
private mapMessages;
|
|
1371
|
+
/**
|
|
1372
|
+
* Merge consecutive messages with the same role into a single message.
|
|
1373
|
+
* Gemini requires alternating user/model turns.
|
|
1374
|
+
*/
|
|
1375
|
+
private mergeConsecutiveRoles;
|
|
1376
|
+
private mapToolDefinition;
|
|
1377
|
+
private parseResponse;
|
|
1378
|
+
private parseSSEStream;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* In-memory implementation of StateStore.
|
|
1383
|
+
* Fast for development and testing, but lost on process restart.
|
|
1384
|
+
*
|
|
1385
|
+
* Exception: awaitHuman state (pending decisions + execution states with
|
|
1386
|
+
* status "waiting") is persisted to a temporary file so it survives restarts.
|
|
1387
|
+
* This is the one exception to "memory means ephemeral" — because a human
|
|
1388
|
+
* could take hours to respond, and losing that state is unacceptable.
|
|
1389
|
+
*/
|
|
1390
|
+
declare class MemoryStore implements StateStore {
|
|
1391
|
+
private checkpoints;
|
|
1392
|
+
private sessions;
|
|
1393
|
+
private sessionMeta;
|
|
1394
|
+
private decisions;
|
|
1395
|
+
private executionStates;
|
|
1396
|
+
private memories;
|
|
1397
|
+
constructor();
|
|
1398
|
+
saveCheckpoint(executionId: string, step: number, data: unknown): Promise<void>;
|
|
1399
|
+
getCheckpoint(executionId: string, step: number): Promise<unknown | null>;
|
|
1400
|
+
getLatestCheckpoint(executionId: string): Promise<{
|
|
1401
|
+
step: number;
|
|
1402
|
+
data: unknown;
|
|
1403
|
+
} | null>;
|
|
1404
|
+
saveSession(sessionId: string, history: ChatMessage[]): Promise<void>;
|
|
1405
|
+
getSession(sessionId: string): Promise<ChatMessage[]>;
|
|
1406
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
1407
|
+
saveSessionMeta(sessionId: string, key: string, value: unknown): Promise<void>;
|
|
1408
|
+
getSessionMeta(sessionId: string, key: string): Promise<unknown | null>;
|
|
1409
|
+
savePendingDecision(executionId: string, decision: PendingDecision): Promise<void>;
|
|
1410
|
+
getPendingDecisions(): Promise<PendingDecision[]>;
|
|
1411
|
+
resolveDecision(executionId: string, _result: HumanDecision): Promise<void>;
|
|
1412
|
+
saveExecutionState(executionId: string, state: ExecutionState): Promise<void>;
|
|
1413
|
+
getExecutionState(executionId: string): Promise<ExecutionState | null>;
|
|
1414
|
+
listPendingExecutions(): Promise<string[]>;
|
|
1415
|
+
saveMemory(scope: string, key: string, value: unknown): Promise<void>;
|
|
1416
|
+
getMemory(scope: string, key: string): Promise<unknown | null>;
|
|
1417
|
+
getAllMemory(scope: string): Promise<Array<{
|
|
1418
|
+
key: string;
|
|
1419
|
+
value: unknown;
|
|
1420
|
+
}>>;
|
|
1421
|
+
deleteMemory(scope: string, key: string): Promise<void>;
|
|
1422
|
+
close(): Promise<void>;
|
|
1423
|
+
deleteCheckpoints(executionId: string): Promise<void>;
|
|
1424
|
+
private persistAwaitHumanState;
|
|
1425
|
+
private loadPersistedState;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* SQLite-backed StateStore using better-sqlite3.
|
|
1430
|
+
*
|
|
1431
|
+
* Zero-config, file-based persistence suitable for single-process production.
|
|
1432
|
+
* Uses prepared statements for all operations.
|
|
1433
|
+
*
|
|
1434
|
+
* Requires `better-sqlite3` as a peer dependency. If not installed,
|
|
1435
|
+
* the constructor throws a clear error message.
|
|
1436
|
+
*/
|
|
1437
|
+
declare class SQLiteStore implements StateStore {
|
|
1438
|
+
private db;
|
|
1439
|
+
constructor(filePath: string);
|
|
1440
|
+
private initTables;
|
|
1441
|
+
saveCheckpoint(executionId: string, step: number, data: unknown): Promise<void>;
|
|
1442
|
+
getCheckpoint(executionId: string, step: number): Promise<unknown | null>;
|
|
1443
|
+
getLatestCheckpoint(executionId: string): Promise<{
|
|
1444
|
+
step: number;
|
|
1445
|
+
data: unknown;
|
|
1446
|
+
} | null>;
|
|
1447
|
+
saveSession(sessionId: string, history: ChatMessage[]): Promise<void>;
|
|
1448
|
+
getSession(sessionId: string): Promise<ChatMessage[]>;
|
|
1449
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
1450
|
+
saveSessionMeta(sessionId: string, key: string, value: unknown): Promise<void>;
|
|
1451
|
+
getSessionMeta(sessionId: string, key: string): Promise<unknown | null>;
|
|
1452
|
+
savePendingDecision(executionId: string, decision: PendingDecision): Promise<void>;
|
|
1453
|
+
getPendingDecisions(): Promise<PendingDecision[]>;
|
|
1454
|
+
resolveDecision(executionId: string, _result: HumanDecision): Promise<void>;
|
|
1455
|
+
saveExecutionState(executionId: string, state: ExecutionState): Promise<void>;
|
|
1456
|
+
getExecutionState(executionId: string): Promise<ExecutionState | null>;
|
|
1457
|
+
listPendingExecutions(): Promise<string[]>;
|
|
1458
|
+
saveMemory(scope: string, key: string, value: unknown): Promise<void>;
|
|
1459
|
+
getMemory(scope: string, key: string): Promise<unknown | null>;
|
|
1460
|
+
getAllMemory(scope: string): Promise<Array<{
|
|
1461
|
+
key: string;
|
|
1462
|
+
value: unknown;
|
|
1463
|
+
}>>;
|
|
1464
|
+
deleteMemory(scope: string, key: string): Promise<void>;
|
|
1465
|
+
/** Close the database connection. */
|
|
1466
|
+
close(): Promise<void>;
|
|
1467
|
+
deleteCheckpoints(executionId: string): Promise<void>;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
/**
|
|
1471
|
+
* Redis-backed StateStore using ioredis.
|
|
1472
|
+
*
|
|
1473
|
+
* Designed for multi-process and sidecar deployments where
|
|
1474
|
+
* multiple runtime instances need shared state.
|
|
1475
|
+
*
|
|
1476
|
+
* Requires `ioredis` as a peer dependency. If not installed,
|
|
1477
|
+
* the constructor throws a clear error message.
|
|
1478
|
+
*/
|
|
1479
|
+
declare class RedisStore implements StateStore {
|
|
1480
|
+
private client;
|
|
1481
|
+
constructor(url?: string);
|
|
1482
|
+
private checkpointKey;
|
|
1483
|
+
private sessionKey;
|
|
1484
|
+
private sessionMetaKey;
|
|
1485
|
+
private decisionsKey;
|
|
1486
|
+
private executionStateKey;
|
|
1487
|
+
private pendingExecSetKey;
|
|
1488
|
+
saveCheckpoint(executionId: string, step: number, data: unknown): Promise<void>;
|
|
1489
|
+
getCheckpoint(executionId: string, step: number): Promise<unknown | null>;
|
|
1490
|
+
getLatestCheckpoint(executionId: string): Promise<{
|
|
1491
|
+
step: number;
|
|
1492
|
+
data: unknown;
|
|
1493
|
+
} | null>;
|
|
1494
|
+
saveSession(sessionId: string, history: ChatMessage[]): Promise<void>;
|
|
1495
|
+
getSession(sessionId: string): Promise<ChatMessage[]>;
|
|
1496
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
1497
|
+
saveSessionMeta(sessionId: string, key: string, value: unknown): Promise<void>;
|
|
1498
|
+
getSessionMeta(sessionId: string, key: string): Promise<unknown | null>;
|
|
1499
|
+
savePendingDecision(executionId: string, decision: PendingDecision): Promise<void>;
|
|
1500
|
+
getPendingDecisions(): Promise<PendingDecision[]>;
|
|
1501
|
+
resolveDecision(executionId: string, _result: HumanDecision): Promise<void>;
|
|
1502
|
+
saveExecutionState(executionId: string, state: ExecutionState): Promise<void>;
|
|
1503
|
+
getExecutionState(executionId: string): Promise<ExecutionState | null>;
|
|
1504
|
+
listPendingExecutions(): Promise<string[]>;
|
|
1505
|
+
/** Close the Redis connection. */
|
|
1506
|
+
close(): Promise<void>;
|
|
1507
|
+
deleteCheckpoints(executionId: string): Promise<void>;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
/**
|
|
1511
|
+
* OpenAI embeddings via raw fetch (zero SDK dependency).
|
|
1512
|
+
* Uses the /v1/embeddings endpoint.
|
|
1513
|
+
*/
|
|
1514
|
+
declare class OpenAIEmbedder implements Embedder {
|
|
1515
|
+
private apiKey;
|
|
1516
|
+
private model;
|
|
1517
|
+
private baseUrl;
|
|
1518
|
+
readonly dimensions: number;
|
|
1519
|
+
constructor(options?: {
|
|
1520
|
+
apiKey?: string;
|
|
1521
|
+
model?: string;
|
|
1522
|
+
baseUrl?: string;
|
|
1523
|
+
dimensions?: number;
|
|
1524
|
+
});
|
|
1525
|
+
embed(texts: string[]): Promise<number[][]>;
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
/**
|
|
1529
|
+
* In-memory vector store using brute-force cosine similarity.
|
|
1530
|
+
* Suitable for testing and small datasets only.
|
|
1531
|
+
*/
|
|
1532
|
+
declare class InMemoryVectorStore implements VectorStore {
|
|
1533
|
+
private entries;
|
|
1534
|
+
upsert(entries: VectorEntry[]): Promise<void>;
|
|
1535
|
+
search(embedding: number[], topK: number): Promise<VectorResult[]>;
|
|
1536
|
+
delete(ids: string[]): Promise<void>;
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* SQLite-backed vector store using better-sqlite3.
|
|
1541
|
+
* Uses a simple serialized embedding column with brute-force cosine similarity in JS.
|
|
1542
|
+
* For production workloads, consider a dedicated vector database.
|
|
1543
|
+
*
|
|
1544
|
+
* This avoids dependency on sqlite-vec extension by computing similarity in JS
|
|
1545
|
+
* after retrieving all rows (brute-force scan). Suitable for small-to-medium datasets.
|
|
1546
|
+
*/
|
|
1547
|
+
declare class SqliteVectorStore implements VectorStore {
|
|
1548
|
+
private db;
|
|
1549
|
+
constructor(options?: {
|
|
1550
|
+
path?: string;
|
|
1551
|
+
});
|
|
1552
|
+
private initTables;
|
|
1553
|
+
upsert(entries: VectorEntry[]): Promise<void>;
|
|
1554
|
+
search(embedding: number[], topK: number): Promise<VectorResult[]>;
|
|
1555
|
+
delete(ids: string[]): Promise<void>;
|
|
1556
|
+
close(): Promise<void>;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
/**
|
|
1560
|
+
* No-op span manager. Zero overhead when OTel is not configured.
|
|
1561
|
+
* All methods are synchronous no-ops that return immediately.
|
|
1562
|
+
*/
|
|
1563
|
+
declare class NoopSpanManager implements SpanManager {
|
|
1564
|
+
withSpanAsync<T>(_name: string, _attributes: Record<string, string | number | boolean>, fn: (span: SpanHandle) => Promise<T>): Promise<T>;
|
|
1565
|
+
addEventToActiveSpan(): void;
|
|
1566
|
+
shutdown(): Promise<void>;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
/**
|
|
1570
|
+
* Create a SpanManager based on configuration.
|
|
1571
|
+
* Returns NoopSpanManager when telemetry is disabled (zero overhead).
|
|
1572
|
+
* Dynamically imports OTelSpanManager when enabled (avoids hard dep).
|
|
1573
|
+
*/
|
|
1574
|
+
declare function createSpanManager(config?: TelemetryConfig): Promise<SpanManager>;
|
|
1575
|
+
|
|
1576
|
+
export { type Agent, type AgentConfig, AnthropicProvider, type AskOptions, type AwaitHumanOptions, type AxlConfig, AxlError, AxlRuntime, AxlStream, BudgetExceededError, type BudgetOptions, type BudgetResult, type ChatMessage, type ChatOptions, type Embedder, type ExecutionInfo, type ExecutionState, GeminiProvider, type GuardrailBlockHandler, GuardrailError, type GuardrailResult, type GuardrailsConfig, type HandoffDescriptor, type HandoffRecord, type HumanDecision, InMemoryVectorStore, type InputGuardrail, type MapOptions, MaxTurnsError, McpManager, type McpServer, type McpServerConfig, type McpToolDefinition, type McpToolResult, type MemoryConfig, MemoryManager, MemoryStore, NoConsensus, NoopSpanManager, OpenAIEmbedder, OpenAIProvider, OpenAIResponsesProvider, type OutputGuardrail, type PendingDecision, type Provider, type ProviderAdapter, ProviderRegistry, type ProviderResponse, QuorumNotMet, type RaceOptions, type RecallOptions, RedisStore, type RememberOptions, type Result, type RetryPolicy, SQLiteStore, Session, type SessionOptions, type SpanHandle, type SpanManager, type SpawnOptions, SqliteVectorStore, type StateStore, type StreamChunk, type StreamEvent, type TelemetryConfig, TimeoutError, type Tool, type ToolCallMessage, type ToolConfig, ToolDenied, type ToolHooks, type TraceEvent, type VectorEntry, type VectorResult, type VectorStore, VerifyError, type VerifyOptions, type VoteOptions, type Workflow, type WorkflowConfig, WorkflowContext, type WorkflowContextInit, agent, createSpanManager, defineConfig, tool, workflow };
|