@kjerneverk/agentic 1.0.6 → 1.0.7-dev.20260212012257.575e470
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +388 -0
- package/dist/index.js +685 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,243 @@
|
|
|
11
11
|
|
|
12
12
|
import { ZodSchema } from 'zod';
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* A chunk yielded during agent execution
|
|
16
|
+
*/
|
|
17
|
+
export declare interface AgentChunk {
|
|
18
|
+
type: AgentChunkType;
|
|
19
|
+
/** Text content (for type='text') */
|
|
20
|
+
text?: string;
|
|
21
|
+
/** Tool information (for tool_start/tool_result) */
|
|
22
|
+
tool?: {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
arguments?: Record<string, any>;
|
|
26
|
+
result?: string;
|
|
27
|
+
error?: string;
|
|
28
|
+
duration?: number;
|
|
29
|
+
};
|
|
30
|
+
/** Error information (for type='error') */
|
|
31
|
+
error?: {
|
|
32
|
+
message: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
};
|
|
35
|
+
/** Metadata */
|
|
36
|
+
meta?: {
|
|
37
|
+
iteration?: number;
|
|
38
|
+
tokenUsage?: {
|
|
39
|
+
input: number;
|
|
40
|
+
output: number;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Streaming chunk types from the agent loop
|
|
47
|
+
*/
|
|
48
|
+
export declare type AgentChunkType = 'text' | 'tool_start' | 'tool_result' | 'turn_complete' | 'done' | 'error';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Execution options
|
|
52
|
+
*/
|
|
53
|
+
export declare interface AgentExecutionOptions {
|
|
54
|
+
apiKey?: string;
|
|
55
|
+
model?: string;
|
|
56
|
+
temperature?: number;
|
|
57
|
+
maxTokens?: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* AgentLoop orchestrates multi-turn LLM conversations with tool use.
|
|
62
|
+
*
|
|
63
|
+
* The loop:
|
|
64
|
+
* 1. Sends messages + tool definitions to LLM
|
|
65
|
+
* 2. Parses response for tool calls
|
|
66
|
+
* 3. Executes tool calls via ToolRegistry
|
|
67
|
+
* 4. Formats tool results as messages
|
|
68
|
+
* 5. Feeds back to LLM
|
|
69
|
+
* 6. Repeats until LLM responds with text (no tool calls)
|
|
70
|
+
*/
|
|
71
|
+
export declare class AgentLoop {
|
|
72
|
+
private provider;
|
|
73
|
+
private toolRegistry;
|
|
74
|
+
private conversation;
|
|
75
|
+
private model;
|
|
76
|
+
private maxIterations;
|
|
77
|
+
private events;
|
|
78
|
+
private logger;
|
|
79
|
+
private executionOptions;
|
|
80
|
+
private cancelled;
|
|
81
|
+
private constructor();
|
|
82
|
+
/**
|
|
83
|
+
* Create a new AgentLoop instance
|
|
84
|
+
*/
|
|
85
|
+
static create(options: AgentLoopOptions): AgentLoop;
|
|
86
|
+
/**
|
|
87
|
+
* Set the system prompt
|
|
88
|
+
*/
|
|
89
|
+
setSystemPrompt(prompt: string): void;
|
|
90
|
+
/**
|
|
91
|
+
* Get the conversation manager
|
|
92
|
+
*/
|
|
93
|
+
getConversation(): ConversationManager;
|
|
94
|
+
/**
|
|
95
|
+
* Cancel the current execution
|
|
96
|
+
*/
|
|
97
|
+
cancel(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Reset cancellation state
|
|
100
|
+
*/
|
|
101
|
+
resetCancellation(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Run the agent loop with a user message (non-streaming)
|
|
104
|
+
*/
|
|
105
|
+
run(userMessage: string): Promise<AgentLoopResult>;
|
|
106
|
+
/**
|
|
107
|
+
* Run the agent loop with streaming output
|
|
108
|
+
*/
|
|
109
|
+
runStream(userMessage: string): AsyncIterable<AgentChunk>;
|
|
110
|
+
/**
|
|
111
|
+
* Execute tool calls and yield streaming results
|
|
112
|
+
*/
|
|
113
|
+
private executeToolCallsStreaming;
|
|
114
|
+
/**
|
|
115
|
+
* Build a request object for the provider
|
|
116
|
+
*/
|
|
117
|
+
private buildRequest;
|
|
118
|
+
/**
|
|
119
|
+
* Get tool definitions in provider-agnostic format
|
|
120
|
+
*/
|
|
121
|
+
private getToolDefinitions;
|
|
122
|
+
/**
|
|
123
|
+
* Build ToolCall array from accumulated streaming data
|
|
124
|
+
*/
|
|
125
|
+
private buildToolCalls;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Events emitted by the agent loop
|
|
130
|
+
*/
|
|
131
|
+
export declare interface AgentLoopEvents {
|
|
132
|
+
onToolCallStart?: (toolName: string, args: Record<string, any>) => void;
|
|
133
|
+
onToolCallComplete?: (toolName: string, result: any, duration: number) => void;
|
|
134
|
+
onToolCallError?: (toolName: string, error: Error) => void;
|
|
135
|
+
onLLMRequest?: (messages: ConversationMessage[]) => void;
|
|
136
|
+
onLLMResponse?: (content: string | null, toolCalls?: ToolCall[]) => void;
|
|
137
|
+
onIterationStart?: (iteration: number) => void;
|
|
138
|
+
onIterationComplete?: (iteration: number) => void;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Options for creating an AgentLoop
|
|
143
|
+
*/
|
|
144
|
+
export declare interface AgentLoopOptions {
|
|
145
|
+
/** The LLM provider to use */
|
|
146
|
+
provider: AgentProvider;
|
|
147
|
+
/** Tool registry with available tools */
|
|
148
|
+
toolRegistry: ToolRegistry;
|
|
149
|
+
/** Conversation manager (optional, will create one if not provided) */
|
|
150
|
+
conversation?: ConversationManager;
|
|
151
|
+
/** Model to use */
|
|
152
|
+
model?: string;
|
|
153
|
+
/** Maximum iterations before stopping (default: 10) */
|
|
154
|
+
maxIterations?: number;
|
|
155
|
+
/** Event handlers */
|
|
156
|
+
events?: AgentLoopEvents;
|
|
157
|
+
/** Logger */
|
|
158
|
+
logger?: Logger;
|
|
159
|
+
/** Execution options passed to provider */
|
|
160
|
+
executionOptions?: AgentExecutionOptions;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Result of running the agent loop
|
|
165
|
+
*/
|
|
166
|
+
export declare interface AgentLoopResult {
|
|
167
|
+
/** Final text response from the agent */
|
|
168
|
+
response: string;
|
|
169
|
+
/** Number of iterations executed */
|
|
170
|
+
iterations: number;
|
|
171
|
+
/** Tool calls made during execution */
|
|
172
|
+
toolCalls: Array<{
|
|
173
|
+
name: string;
|
|
174
|
+
arguments: Record<string, any>;
|
|
175
|
+
result: any;
|
|
176
|
+
duration: number;
|
|
177
|
+
}>;
|
|
178
|
+
/** Total token usage */
|
|
179
|
+
tokenUsage?: {
|
|
180
|
+
input: number;
|
|
181
|
+
output: number;
|
|
182
|
+
};
|
|
183
|
+
/** Whether the loop was cancelled */
|
|
184
|
+
cancelled: boolean;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Provider interface for the agent loop
|
|
189
|
+
* Compatible with execution package providers
|
|
190
|
+
*/
|
|
191
|
+
export declare interface AgentProvider {
|
|
192
|
+
readonly name: string;
|
|
193
|
+
execute(request: AgentRequest, options?: AgentExecutionOptions): Promise<AgentProviderResponse>;
|
|
194
|
+
executeStream?(request: AgentRequest, options?: AgentExecutionOptions): AsyncIterable<AgentStreamChunk>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Response from the provider
|
|
199
|
+
*/
|
|
200
|
+
export declare interface AgentProviderResponse {
|
|
201
|
+
content: string;
|
|
202
|
+
model: string;
|
|
203
|
+
usage?: {
|
|
204
|
+
inputTokens: number;
|
|
205
|
+
outputTokens: number;
|
|
206
|
+
};
|
|
207
|
+
toolCalls?: ToolCall[];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Request to the provider
|
|
212
|
+
*/
|
|
213
|
+
export declare interface AgentRequest {
|
|
214
|
+
messages: ConversationMessage[];
|
|
215
|
+
model: string;
|
|
216
|
+
tools?: AgentToolDefinition[];
|
|
217
|
+
addMessage(message: ConversationMessage): void;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Streaming chunk from provider
|
|
222
|
+
*/
|
|
223
|
+
export declare interface AgentStreamChunk {
|
|
224
|
+
type: 'text' | 'tool_call_start' | 'tool_call_delta' | 'tool_call_end' | 'usage' | 'done';
|
|
225
|
+
text?: string;
|
|
226
|
+
toolCall?: {
|
|
227
|
+
id?: string;
|
|
228
|
+
index?: number;
|
|
229
|
+
name?: string;
|
|
230
|
+
argumentsDelta?: string;
|
|
231
|
+
};
|
|
232
|
+
usage?: {
|
|
233
|
+
inputTokens: number;
|
|
234
|
+
outputTokens: number;
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Tool definition for the agent
|
|
240
|
+
*/
|
|
241
|
+
export declare interface AgentToolDefinition {
|
|
242
|
+
name: string;
|
|
243
|
+
description: string;
|
|
244
|
+
parameters: {
|
|
245
|
+
type: 'object';
|
|
246
|
+
properties: Record<string, any>;
|
|
247
|
+
required?: string[];
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
14
251
|
/**
|
|
15
252
|
* Anthropic-compatible tool format
|
|
16
253
|
*/
|
|
@@ -118,6 +355,144 @@ export declare interface ContextStats {
|
|
|
118
355
|
newestTimestamp?: Date;
|
|
119
356
|
}
|
|
120
357
|
|
|
358
|
+
/**
|
|
359
|
+
* ConversationManager manages message history for agent conversations.
|
|
360
|
+
*
|
|
361
|
+
* Features:
|
|
362
|
+
* - Add/retrieve messages
|
|
363
|
+
* - Handle provider-specific format differences
|
|
364
|
+
* - Serialize/deserialize for persistence
|
|
365
|
+
* - Token budget awareness and compression
|
|
366
|
+
*/
|
|
367
|
+
export declare class ConversationManager {
|
|
368
|
+
private messages;
|
|
369
|
+
private logger;
|
|
370
|
+
private tokenBudget?;
|
|
371
|
+
private tokenCounter?;
|
|
372
|
+
private createdAt;
|
|
373
|
+
private updatedAt;
|
|
374
|
+
private constructor();
|
|
375
|
+
/**
|
|
376
|
+
* Create a new ConversationManager instance
|
|
377
|
+
*/
|
|
378
|
+
static create(options?: ConversationManagerOptions): ConversationManager;
|
|
379
|
+
/**
|
|
380
|
+
* Create from serialized state
|
|
381
|
+
*/
|
|
382
|
+
static fromSerialized(data: SerializedConversation, options?: ConversationManagerOptions): ConversationManager;
|
|
383
|
+
/**
|
|
384
|
+
* Add a system message (will be placed at the beginning)
|
|
385
|
+
*/
|
|
386
|
+
addSystemMessage(content: string): void;
|
|
387
|
+
/**
|
|
388
|
+
* Add a user message
|
|
389
|
+
*/
|
|
390
|
+
addUserMessage(content: string): void;
|
|
391
|
+
/**
|
|
392
|
+
* Add an assistant message (text response)
|
|
393
|
+
*/
|
|
394
|
+
addAssistantMessage(content: string): void;
|
|
395
|
+
/**
|
|
396
|
+
* Add an assistant message with tool calls
|
|
397
|
+
*/
|
|
398
|
+
addAssistantToolCalls(toolCalls: ToolCall[], content?: string): void;
|
|
399
|
+
/**
|
|
400
|
+
* Add a tool result message
|
|
401
|
+
*/
|
|
402
|
+
addToolResult(toolCallId: string, result: string): void;
|
|
403
|
+
/**
|
|
404
|
+
* Add a raw message (any role)
|
|
405
|
+
*/
|
|
406
|
+
addMessage(message: ConversationMessage): void;
|
|
407
|
+
/**
|
|
408
|
+
* Get all messages
|
|
409
|
+
*/
|
|
410
|
+
getMessages(): ConversationMessage[];
|
|
411
|
+
/**
|
|
412
|
+
* Get messages in a format suitable for the provider
|
|
413
|
+
* This handles any provider-specific transformations
|
|
414
|
+
*/
|
|
415
|
+
getMessagesForProvider(_provider: 'anthropic' | 'openai'): ConversationMessage[];
|
|
416
|
+
/**
|
|
417
|
+
* Get the system prompt (if any)
|
|
418
|
+
*/
|
|
419
|
+
getSystemPrompt(): string | undefined;
|
|
420
|
+
/**
|
|
421
|
+
* Get the last message
|
|
422
|
+
*/
|
|
423
|
+
getLastMessage(): ConversationMessage | undefined;
|
|
424
|
+
/**
|
|
425
|
+
* Get the last assistant message
|
|
426
|
+
*/
|
|
427
|
+
getLastAssistantMessage(): ConversationMessage | undefined;
|
|
428
|
+
/**
|
|
429
|
+
* Get message count
|
|
430
|
+
*/
|
|
431
|
+
count(): number;
|
|
432
|
+
/**
|
|
433
|
+
* Check if conversation is empty
|
|
434
|
+
*/
|
|
435
|
+
isEmpty(): boolean;
|
|
436
|
+
/**
|
|
437
|
+
* Clear all messages except system prompt
|
|
438
|
+
*/
|
|
439
|
+
clearHistory(): void;
|
|
440
|
+
/**
|
|
441
|
+
* Clear everything including system prompt
|
|
442
|
+
*/
|
|
443
|
+
clear(): void;
|
|
444
|
+
/**
|
|
445
|
+
* Serialize conversation for persistence
|
|
446
|
+
*/
|
|
447
|
+
serialize(): SerializedConversation;
|
|
448
|
+
/**
|
|
449
|
+
* Get token usage information
|
|
450
|
+
*/
|
|
451
|
+
getTokenUsage(): TokenUsage | undefined;
|
|
452
|
+
/**
|
|
453
|
+
* Check token budget and compress if needed
|
|
454
|
+
*/
|
|
455
|
+
private checkTokenBudget;
|
|
456
|
+
/**
|
|
457
|
+
* Handle budget exceeded based on configuration
|
|
458
|
+
*/
|
|
459
|
+
private handleBudgetExceeded;
|
|
460
|
+
/**
|
|
461
|
+
* Compress conversation to fit within budget
|
|
462
|
+
*/
|
|
463
|
+
private compressConversation;
|
|
464
|
+
/**
|
|
465
|
+
* Truncate conversation to fit within budget
|
|
466
|
+
*/
|
|
467
|
+
private truncateConversation;
|
|
468
|
+
/**
|
|
469
|
+
* Check if currently over budget
|
|
470
|
+
*/
|
|
471
|
+
private isOverBudget;
|
|
472
|
+
/**
|
|
473
|
+
* Get a summary of the conversation for debugging
|
|
474
|
+
*/
|
|
475
|
+
getSummary(): {
|
|
476
|
+
messageCount: number;
|
|
477
|
+
roles: Record<string, number>;
|
|
478
|
+
hasSystemPrompt: boolean;
|
|
479
|
+
lastRole?: string;
|
|
480
|
+
tokenUsage?: TokenUsage;
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Options for creating a ConversationManager
|
|
486
|
+
*/
|
|
487
|
+
export declare interface ConversationManagerOptions {
|
|
488
|
+
/** Optional logger */
|
|
489
|
+
logger?: Logger;
|
|
490
|
+
/** Token budget configuration for context window management */
|
|
491
|
+
tokenBudget?: TokenBudgetConfig;
|
|
492
|
+
/** Function to count tokens in a message */
|
|
493
|
+
tokenCounter?: (message: ConversationMessage) => number;
|
|
494
|
+
}
|
|
495
|
+
|
|
121
496
|
export declare interface ConversationMessage {
|
|
122
497
|
role: 'system' | 'user' | 'assistant' | 'tool' | 'developer';
|
|
123
498
|
content: string | null;
|
|
@@ -291,6 +666,19 @@ export declare interface SandboxOptions {
|
|
|
291
666
|
onAfterExecution?: (tool: Tool, result: unknown, error?: Error) => void | Promise<void>;
|
|
292
667
|
}
|
|
293
668
|
|
|
669
|
+
/**
|
|
670
|
+
* Serialized conversation state for persistence
|
|
671
|
+
*/
|
|
672
|
+
export declare interface SerializedConversation {
|
|
673
|
+
messages: ConversationMessage[];
|
|
674
|
+
metadata: {
|
|
675
|
+
createdAt: string;
|
|
676
|
+
updatedAt: string;
|
|
677
|
+
messageCount: number;
|
|
678
|
+
systemPrompt?: string;
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
|
|
294
682
|
/**
|
|
295
683
|
* Current state of strategy execution
|
|
296
684
|
*/
|