@simulacra-ai/core 0.0.2 → 0.0.4
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 +87 -108
- package/dist/index.cjs +2293 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1631 -0
- package/dist/index.d.ts +1631 -10
- package/dist/index.js +2232 -8
- package/dist/index.js.map +1 -1
- package/package.json +10 -4
- package/dist/checkpoints/default-summarization-strategy.d.ts +0 -13
- package/dist/checkpoints/default-summarization-strategy.d.ts.map +0 -1
- package/dist/checkpoints/default-summarization-strategy.js +0 -63
- package/dist/checkpoints/default-summarization-strategy.js.map +0 -1
- package/dist/checkpoints/index.d.ts +0 -3
- package/dist/checkpoints/index.d.ts.map +0 -1
- package/dist/checkpoints/index.js +0 -3
- package/dist/checkpoints/index.js.map +0 -1
- package/dist/checkpoints/types.d.ts +0 -32
- package/dist/checkpoints/types.d.ts.map +0 -1
- package/dist/checkpoints/types.js +0 -2
- package/dist/checkpoints/types.js.map +0 -1
- package/dist/context-transformers/checkpoint-context-transformer.d.ts +0 -13
- package/dist/context-transformers/checkpoint-context-transformer.d.ts.map +0 -1
- package/dist/context-transformers/checkpoint-context-transformer.js +0 -32
- package/dist/context-transformers/checkpoint-context-transformer.js.map +0 -1
- package/dist/context-transformers/composite-context-transformer.d.ts +0 -31
- package/dist/context-transformers/composite-context-transformer.d.ts.map +0 -1
- package/dist/context-transformers/composite-context-transformer.js +0 -41
- package/dist/context-transformers/composite-context-transformer.js.map +0 -1
- package/dist/context-transformers/index.d.ts +0 -6
- package/dist/context-transformers/index.d.ts.map +0 -1
- package/dist/context-transformers/index.js +0 -6
- package/dist/context-transformers/index.js.map +0 -1
- package/dist/context-transformers/noop-context-transformer.d.ts +0 -24
- package/dist/context-transformers/noop-context-transformer.d.ts.map +0 -1
- package/dist/context-transformers/noop-context-transformer.js +0 -26
- package/dist/context-transformers/noop-context-transformer.js.map +0 -1
- package/dist/context-transformers/tool-context-transformer.d.ts +0 -26
- package/dist/context-transformers/tool-context-transformer.d.ts.map +0 -1
- package/dist/context-transformers/tool-context-transformer.js +0 -52
- package/dist/context-transformers/tool-context-transformer.js.map +0 -1
- package/dist/context-transformers/types.d.ts +0 -65
- package/dist/context-transformers/types.d.ts.map +0 -1
- package/dist/context-transformers/types.js +0 -2
- package/dist/context-transformers/types.js.map +0 -1
- package/dist/conversations/conversation.d.ts +0 -146
- package/dist/conversations/conversation.d.ts.map +0 -1
- package/dist/conversations/conversation.js +0 -642
- package/dist/conversations/conversation.js.map +0 -1
- package/dist/conversations/index.d.ts +0 -5
- package/dist/conversations/index.d.ts.map +0 -1
- package/dist/conversations/index.js +0 -5
- package/dist/conversations/index.js.map +0 -1
- package/dist/conversations/stream-listener.d.ts +0 -37
- package/dist/conversations/stream-listener.d.ts.map +0 -1
- package/dist/conversations/stream-listener.js +0 -68
- package/dist/conversations/stream-listener.js.map +0 -1
- package/dist/conversations/token-tracker.d.ts +0 -59
- package/dist/conversations/token-tracker.d.ts.map +0 -1
- package/dist/conversations/token-tracker.js +0 -98
- package/dist/conversations/token-tracker.js.map +0 -1
- package/dist/conversations/types.d.ts +0 -346
- package/dist/conversations/types.d.ts.map +0 -1
- package/dist/conversations/types.js +0 -2
- package/dist/conversations/types.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/policies/composite-policy.d.ts +0 -29
- package/dist/policies/composite-policy.d.ts.map +0 -1
- package/dist/policies/composite-policy.js +0 -77
- package/dist/policies/composite-policy.js.map +0 -1
- package/dist/policies/default-policy.d.ts +0 -7
- package/dist/policies/default-policy.d.ts.map +0 -1
- package/dist/policies/default-policy.js +0 -14
- package/dist/policies/default-policy.js.map +0 -1
- package/dist/policies/index.d.ts +0 -8
- package/dist/policies/index.d.ts.map +0 -1
- package/dist/policies/index.js +0 -8
- package/dist/policies/index.js.map +0 -1
- package/dist/policies/noop-policy.d.ts +0 -21
- package/dist/policies/noop-policy.d.ts.map +0 -1
- package/dist/policies/noop-policy.js +0 -36
- package/dist/policies/noop-policy.js.map +0 -1
- package/dist/policies/rate-limit-policy.d.ts +0 -44
- package/dist/policies/rate-limit-policy.d.ts.map +0 -1
- package/dist/policies/rate-limit-policy.js +0 -98
- package/dist/policies/rate-limit-policy.js.map +0 -1
- package/dist/policies/retry-policy.d.ts +0 -42
- package/dist/policies/retry-policy.d.ts.map +0 -1
- package/dist/policies/retry-policy.js +0 -114
- package/dist/policies/retry-policy.js.map +0 -1
- package/dist/policies/token-limit-policy.d.ts +0 -76
- package/dist/policies/token-limit-policy.d.ts.map +0 -1
- package/dist/policies/token-limit-policy.js +0 -134
- package/dist/policies/token-limit-policy.js.map +0 -1
- package/dist/policies/types.d.ts +0 -49
- package/dist/policies/types.d.ts.map +0 -1
- package/dist/policies/types.js +0 -9
- package/dist/policies/types.js.map +0 -1
- package/dist/tools/index.d.ts +0 -2
- package/dist/tools/index.d.ts.map +0 -1
- package/dist/tools/index.js +0 -2
- package/dist/tools/index.js.map +0 -1
- package/dist/tools/types.d.ts +0 -180
- package/dist/tools/types.d.ts.map +0 -1
- package/dist/tools/types.js +0 -2
- package/dist/tools/types.js.map +0 -1
- package/dist/utils/async.d.ts +0 -125
- package/dist/utils/async.d.ts.map +0 -1
- package/dist/utils/async.js +0 -217
- package/dist/utils/async.js.map +0 -1
- package/dist/utils/index.d.ts +0 -4
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -4
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/object.d.ts +0 -80
- package/dist/utils/object.d.ts.map +0 -1
- package/dist/utils/object.js +0 -189
- package/dist/utils/object.js.map +0 -1
- package/dist/utils/types.d.ts +0 -17
- package/dist/utils/types.d.ts.map +0 -1
- package/dist/utils/types.js +0 -2
- package/dist/utils/types.js.map +0 -1
- package/dist/workflows/index.d.ts +0 -4
- package/dist/workflows/index.d.ts.map +0 -1
- package/dist/workflows/index.js +0 -4
- package/dist/workflows/index.js.map +0 -1
- package/dist/workflows/types.d.ts +0 -70
- package/dist/workflows/types.d.ts.map +0 -1
- package/dist/workflows/types.js +0 -2
- package/dist/workflows/types.js.map +0 -1
- package/dist/workflows/workflow-manager.d.ts +0 -74
- package/dist/workflows/workflow-manager.d.ts.map +0 -1
- package/dist/workflows/workflow-manager.js +0 -165
- package/dist/workflows/workflow-manager.js.map +0 -1
- package/dist/workflows/workflow.d.ts +0 -116
- package/dist/workflows/workflow.d.ts.map +0 -1
- package/dist/workflows/workflow.js +0 -351
- package/dist/workflows/workflow.js.map +0 -1
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1631 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages an agentic workflow with automatic tool execution.
|
|
3
|
+
*
|
|
4
|
+
* A Workflow wraps a Conversation and automatically executes tools requested
|
|
5
|
+
* by the model, continuing the conversation until completion or error.
|
|
6
|
+
*/
|
|
7
|
+
declare class Workflow {
|
|
8
|
+
#private;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new workflow instance.
|
|
11
|
+
*
|
|
12
|
+
* @param conversation - The conversation to manage.
|
|
13
|
+
* @param options - Optional configuration.
|
|
14
|
+
* @param options.context_data - Custom data to pass to tools via ToolContext.
|
|
15
|
+
* @param options.parent - Parent workflow if this is a child workflow.
|
|
16
|
+
*/
|
|
17
|
+
constructor(conversation: Conversation, options?: {
|
|
18
|
+
context_data?: Record<string, unknown>;
|
|
19
|
+
parent?: Workflow;
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* The unique identifier for this workflow.
|
|
23
|
+
*/
|
|
24
|
+
get id(): string;
|
|
25
|
+
set id(value: string);
|
|
26
|
+
/**
|
|
27
|
+
* The current state of the workflow.
|
|
28
|
+
*/
|
|
29
|
+
get state(): WorkflowState;
|
|
30
|
+
/**
|
|
31
|
+
* The conversation being managed by this workflow.
|
|
32
|
+
*/
|
|
33
|
+
get conversation(): Conversation;
|
|
34
|
+
/**
|
|
35
|
+
* The parent workflow, if this is a child workflow.
|
|
36
|
+
*/
|
|
37
|
+
get parent(): Workflow | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The message history tracked by this workflow.
|
|
40
|
+
*/
|
|
41
|
+
get messages(): readonly Readonly<Message>[];
|
|
42
|
+
/**
|
|
43
|
+
* Messages queued for sending after the current response completes.
|
|
44
|
+
*/
|
|
45
|
+
get queued_messages(): string[];
|
|
46
|
+
/**
|
|
47
|
+
* Disposes the workflow and releases all resources.
|
|
48
|
+
*
|
|
49
|
+
* This method is called automatically when using the `using` keyword.
|
|
50
|
+
*/
|
|
51
|
+
[Symbol.dispose](): void;
|
|
52
|
+
/**
|
|
53
|
+
* Registers a one-time event listener.
|
|
54
|
+
*
|
|
55
|
+
* @template E - The event name type.
|
|
56
|
+
* @param event - The name of the event to listen for.
|
|
57
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
58
|
+
* @returns This workflow instance for chaining.
|
|
59
|
+
*/
|
|
60
|
+
once<E extends keyof WorkflowEvents>(event: E, listener: E extends keyof WorkflowEvents ? (...args: WorkflowEvents[E]) => void : never): this;
|
|
61
|
+
/**
|
|
62
|
+
* Registers a persistent event listener.
|
|
63
|
+
*
|
|
64
|
+
* @template E - The event name type.
|
|
65
|
+
* @param event - The name of the event to listen for.
|
|
66
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
67
|
+
* @returns This workflow instance for chaining.
|
|
68
|
+
*/
|
|
69
|
+
on<E extends keyof WorkflowEvents>(event: E, listener: E extends keyof WorkflowEvents ? (...args: WorkflowEvents[E]) => void : never): this;
|
|
70
|
+
/**
|
|
71
|
+
* Removes an event listener.
|
|
72
|
+
*
|
|
73
|
+
* @template E - The event name type.
|
|
74
|
+
* @param event - The name of the event.
|
|
75
|
+
* @param listener - The callback function to remove.
|
|
76
|
+
* @returns This workflow instance for chaining.
|
|
77
|
+
*/
|
|
78
|
+
off<E extends keyof WorkflowEvents>(event: E, listener: E extends keyof WorkflowEvents ? (...args: WorkflowEvents[E]) => void : never): this;
|
|
79
|
+
/**
|
|
80
|
+
* Starts the workflow execution.
|
|
81
|
+
*
|
|
82
|
+
* @param message - Optional initial user message to begin the workflow.
|
|
83
|
+
*/
|
|
84
|
+
start(message?: UserMessage): void;
|
|
85
|
+
/**
|
|
86
|
+
* Cancels the workflow execution.
|
|
87
|
+
*
|
|
88
|
+
* This will cancel any in-progress model response and emit a workflow_end event.
|
|
89
|
+
*/
|
|
90
|
+
cancel(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a child workflow with a different conversation.
|
|
93
|
+
*
|
|
94
|
+
* Child events are propagated to the parent as "child_workflow_event" events.
|
|
95
|
+
*
|
|
96
|
+
* @param conversation - The conversation for the child workflow.
|
|
97
|
+
* @param id - Optional custom ID for the child workflow.
|
|
98
|
+
* @param context_data - Additional context data to merge with this workflow's context data.
|
|
99
|
+
* @returns The newly created child workflow.
|
|
100
|
+
*/
|
|
101
|
+
spawn_child(conversation: Conversation, id?: string, context_data?: Record<string, unknown>): Workflow;
|
|
102
|
+
/**
|
|
103
|
+
* Adds a message to the queue for sending after the current response completes.
|
|
104
|
+
*
|
|
105
|
+
* @param message - The text message to queue.
|
|
106
|
+
*/
|
|
107
|
+
queue_message(message: string): void;
|
|
108
|
+
/**
|
|
109
|
+
* Clears all queued messages.
|
|
110
|
+
*/
|
|
111
|
+
clear_queue(): void;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Automatically manages workflow lifecycle for a conversation.
|
|
116
|
+
*
|
|
117
|
+
* The WorkflowManager creates a new Workflow instance whenever a message is sent
|
|
118
|
+
* to its associated conversation, eliminating the need for manual workflow management.
|
|
119
|
+
*/
|
|
120
|
+
declare class WorkflowManager {
|
|
121
|
+
#private;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a new workflow manager instance.
|
|
124
|
+
*
|
|
125
|
+
* @param conversation - The conversation to manage workflows for.
|
|
126
|
+
* @param options - Optional configuration.
|
|
127
|
+
* @param options.context_data - Custom data to pass to tools via ToolContext.
|
|
128
|
+
*/
|
|
129
|
+
constructor(conversation: Conversation, options?: {
|
|
130
|
+
context_data?: Record<string, unknown>;
|
|
131
|
+
});
|
|
132
|
+
/**
|
|
133
|
+
* The current state of the workflow manager.
|
|
134
|
+
*/
|
|
135
|
+
get state(): WorkflowState;
|
|
136
|
+
/**
|
|
137
|
+
* The conversation being managed.
|
|
138
|
+
*/
|
|
139
|
+
get conversation(): Conversation;
|
|
140
|
+
/**
|
|
141
|
+
* The currently active workflow, if any.
|
|
142
|
+
*/
|
|
143
|
+
get current_workflow(): Workflow | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* Disposes the workflow manager and releases all resources.
|
|
146
|
+
*
|
|
147
|
+
* This method is called automatically when using the `using` keyword.
|
|
148
|
+
*/
|
|
149
|
+
[Symbol.dispose](): void;
|
|
150
|
+
/**
|
|
151
|
+
* Registers a one-time event listener.
|
|
152
|
+
*
|
|
153
|
+
* @template E - The event name type.
|
|
154
|
+
* @param event - The name of the event to listen for.
|
|
155
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
156
|
+
* @returns This workflow manager instance for chaining.
|
|
157
|
+
*/
|
|
158
|
+
once<E extends keyof WorkflowManagerEvents>(event: E, listener: E extends keyof WorkflowManagerEvents ? (...args: WorkflowManagerEvents[E]) => void : never): this;
|
|
159
|
+
/**
|
|
160
|
+
* Registers a persistent event listener.
|
|
161
|
+
*
|
|
162
|
+
* @template E - The event name type.
|
|
163
|
+
* @param event - The name of the event to listen for.
|
|
164
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
165
|
+
* @returns This workflow manager instance for chaining.
|
|
166
|
+
*/
|
|
167
|
+
on<E extends keyof WorkflowManagerEvents>(event: E, listener: E extends keyof WorkflowManagerEvents ? (...args: WorkflowManagerEvents[E]) => void : never): this;
|
|
168
|
+
/**
|
|
169
|
+
* Removes an event listener.
|
|
170
|
+
*
|
|
171
|
+
* @template E - The event name type.
|
|
172
|
+
* @param event - The name of the event.
|
|
173
|
+
* @param listener - The callback function to remove.
|
|
174
|
+
* @returns This workflow manager instance for chaining.
|
|
175
|
+
*/
|
|
176
|
+
off<E extends keyof WorkflowManagerEvents>(event: E, listener: E extends keyof WorkflowManagerEvents ? (...args: WorkflowManagerEvents[E]) => void : never): this;
|
|
177
|
+
/**
|
|
178
|
+
* Manually starts a workflow.
|
|
179
|
+
*
|
|
180
|
+
* This is typically not needed as workflows are started automatically when messages are sent.
|
|
181
|
+
*/
|
|
182
|
+
start_workflow(): void;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Possible states of a workflow.
|
|
187
|
+
*/
|
|
188
|
+
type WorkflowState = "idle" | "busy" | "disposed";
|
|
189
|
+
/**
|
|
190
|
+
* Event data describing why a workflow ended.
|
|
191
|
+
*/
|
|
192
|
+
type WorkflowEndEvent = {
|
|
193
|
+
reason: "complete" | "cancel" | "error";
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Events emitted by a Workflow instance.
|
|
197
|
+
*/
|
|
198
|
+
interface WorkflowEvents {
|
|
199
|
+
/** Emitted when the workflow state changes. */
|
|
200
|
+
state_change: [ChangeEvent<WorkflowState>, Workflow];
|
|
201
|
+
/** Emitted when the workflow is updated with new messages. */
|
|
202
|
+
workflow_update: [Workflow];
|
|
203
|
+
/** Emitted when the workflow ends. */
|
|
204
|
+
workflow_end: [WorkflowEndEvent, Workflow];
|
|
205
|
+
/** Emitted when a child workflow is created. */
|
|
206
|
+
child_workflow_begin: [Workflow, Workflow];
|
|
207
|
+
/** Emitted when a child workflow emits an event. */
|
|
208
|
+
child_workflow_event: [
|
|
209
|
+
{
|
|
210
|
+
[E in keyof WorkflowEvents]: {
|
|
211
|
+
event_name: E;
|
|
212
|
+
event_args: WorkflowEvents[E];
|
|
213
|
+
};
|
|
214
|
+
}[keyof WorkflowEvents],
|
|
215
|
+
Workflow
|
|
216
|
+
];
|
|
217
|
+
/** Emitted when a message is added to the queue. */
|
|
218
|
+
message_queued: [string, Workflow];
|
|
219
|
+
/** Emitted when a message is removed from the queue for sending. */
|
|
220
|
+
message_dequeued: [string, Workflow];
|
|
221
|
+
/** Emitted when the message queue is cleared. */
|
|
222
|
+
queue_cleared: [Workflow];
|
|
223
|
+
/** Emitted when an infrastructure or lifecycle operation fails. */
|
|
224
|
+
lifecycle_error: [LifecycleErrorEvent, Workflow];
|
|
225
|
+
/** Emitted when the workflow is disposed. */
|
|
226
|
+
dispose: [Workflow];
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Events emitted by a WorkflowManager instance.
|
|
230
|
+
*/
|
|
231
|
+
interface WorkflowManagerEvents {
|
|
232
|
+
/** Emitted when the workflow manager state changes. */
|
|
233
|
+
state_change: [ChangeEvent<WorkflowState>, WorkflowManager];
|
|
234
|
+
/** Emitted when a new workflow begins. */
|
|
235
|
+
workflow_begin: [Workflow, WorkflowManager];
|
|
236
|
+
/** Emitted when a managed workflow emits an event. */
|
|
237
|
+
workflow_event: [
|
|
238
|
+
{
|
|
239
|
+
[E in keyof WorkflowEvents]: {
|
|
240
|
+
event_name: E;
|
|
241
|
+
event_args: WorkflowEvents[E];
|
|
242
|
+
};
|
|
243
|
+
}[keyof WorkflowEvents],
|
|
244
|
+
WorkflowManager
|
|
245
|
+
];
|
|
246
|
+
/** Emitted when an infrastructure or lifecycle operation fails. */
|
|
247
|
+
lifecycle_error: [LifecycleErrorEvent, WorkflowManager];
|
|
248
|
+
/** Emitted when the workflow manager is disposed. */
|
|
249
|
+
dispose: [WorkflowManager];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Context provided to tools when they are executed.
|
|
254
|
+
*
|
|
255
|
+
* This interface can be extended with custom properties by passing
|
|
256
|
+
* additional data through the workflow's context_data.
|
|
257
|
+
*/
|
|
258
|
+
interface ToolContext {
|
|
259
|
+
/** The conversation instance where the tool is being executed. */
|
|
260
|
+
conversation: Conversation;
|
|
261
|
+
/** The workflow instance managing the tool execution. */
|
|
262
|
+
workflow: Workflow;
|
|
263
|
+
/** Additional custom context properties. */
|
|
264
|
+
[key: string]: unknown;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Result indicating successful tool execution.
|
|
268
|
+
*/
|
|
269
|
+
interface ToolSuccessResult {
|
|
270
|
+
result: true;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Result indicating failed tool execution.
|
|
274
|
+
*/
|
|
275
|
+
interface ToolErrorResult {
|
|
276
|
+
result: false;
|
|
277
|
+
/** Description of what went wrong. */
|
|
278
|
+
message: string;
|
|
279
|
+
/** Optional underlying error object. */
|
|
280
|
+
error?: unknown;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Union of all possible tool execution results.
|
|
284
|
+
*/
|
|
285
|
+
type ToolResult = ToolSuccessResult | ToolErrorResult;
|
|
286
|
+
/**
|
|
287
|
+
* String parameter type definition.
|
|
288
|
+
*/
|
|
289
|
+
type StringParameterType = {
|
|
290
|
+
type: "string";
|
|
291
|
+
required: true;
|
|
292
|
+
default?: never;
|
|
293
|
+
description?: string;
|
|
294
|
+
} | {
|
|
295
|
+
type: "string";
|
|
296
|
+
required?: false;
|
|
297
|
+
default?: string;
|
|
298
|
+
description?: string;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Enumeration parameter type definition.
|
|
302
|
+
*/
|
|
303
|
+
type EnumParameterType = {
|
|
304
|
+
type: "string";
|
|
305
|
+
required: true;
|
|
306
|
+
enum: string[];
|
|
307
|
+
default?: never;
|
|
308
|
+
description?: string;
|
|
309
|
+
} | {
|
|
310
|
+
type: "string";
|
|
311
|
+
required?: false;
|
|
312
|
+
enum: string[];
|
|
313
|
+
default?: string;
|
|
314
|
+
description?: string;
|
|
315
|
+
};
|
|
316
|
+
/**
|
|
317
|
+
* Number parameter type definition.
|
|
318
|
+
*/
|
|
319
|
+
type NumberParameterType = {
|
|
320
|
+
type: "number";
|
|
321
|
+
required: true;
|
|
322
|
+
default?: never;
|
|
323
|
+
description?: string;
|
|
324
|
+
} | {
|
|
325
|
+
type: "number";
|
|
326
|
+
required?: false;
|
|
327
|
+
default?: number;
|
|
328
|
+
description?: string;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Boolean parameter type definition.
|
|
332
|
+
*/
|
|
333
|
+
type BooleanParameterType = {
|
|
334
|
+
type: "boolean";
|
|
335
|
+
required: true;
|
|
336
|
+
default?: never;
|
|
337
|
+
description?: string;
|
|
338
|
+
} | {
|
|
339
|
+
type: "boolean";
|
|
340
|
+
required?: false;
|
|
341
|
+
default?: boolean;
|
|
342
|
+
description?: string;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Union of all primitive parameter types.
|
|
346
|
+
*/
|
|
347
|
+
type PrimitiveParameterType = StringParameterType | EnumParameterType | NumberParameterType | BooleanParameterType;
|
|
348
|
+
/**
|
|
349
|
+
* Object parameter type definition with nested properties.
|
|
350
|
+
*/
|
|
351
|
+
interface ObjectParameterType {
|
|
352
|
+
type: "object";
|
|
353
|
+
required?: boolean;
|
|
354
|
+
properties: Record<string, ParameterType>;
|
|
355
|
+
default?: never;
|
|
356
|
+
description?: string;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Array parameter type definition.
|
|
360
|
+
*/
|
|
361
|
+
interface ArrayParameterType {
|
|
362
|
+
type: "array";
|
|
363
|
+
required?: boolean;
|
|
364
|
+
items: ParameterType;
|
|
365
|
+
default?: never;
|
|
366
|
+
description?: string;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Union of all parameter types.
|
|
370
|
+
*/
|
|
371
|
+
type ParameterType = PrimitiveParameterType | ObjectParameterType | ArrayParameterType;
|
|
372
|
+
/**
|
|
373
|
+
* A named parameter with its type definition and optional description.
|
|
374
|
+
*/
|
|
375
|
+
type ToolParameterDefinition = ParameterType & {
|
|
376
|
+
name: string;
|
|
377
|
+
description?: string;
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Complete definition of a tool including its name, description, and parameters.
|
|
381
|
+
*/
|
|
382
|
+
interface ToolDefinition {
|
|
383
|
+
/** The unique name of the tool. */
|
|
384
|
+
name: string;
|
|
385
|
+
/** Description of what the tool does. */
|
|
386
|
+
description: string;
|
|
387
|
+
/** The parameters the tool accepts. */
|
|
388
|
+
parameters: ToolParameterDefinition[];
|
|
389
|
+
/** Whether this tool can be executed in parallel with other tools. */
|
|
390
|
+
parallelizable?: boolean;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Constructor interface for tool classes.
|
|
394
|
+
*
|
|
395
|
+
* @template TParams - The parameter type for the tool.
|
|
396
|
+
* @template TSuccessResult - The success result type.
|
|
397
|
+
* @template TErrorResult - The error result type.
|
|
398
|
+
*/
|
|
399
|
+
interface ToolClass<TParams extends Record<string, unknown> = Record<string, unknown>, TSuccessResult extends ToolSuccessResult = ToolSuccessResult, TErrorResult extends ToolErrorResult = ToolErrorResult> {
|
|
400
|
+
/**
|
|
401
|
+
* Constructs a new tool instance.
|
|
402
|
+
*
|
|
403
|
+
* @param context - The context for tool execution.
|
|
404
|
+
*/
|
|
405
|
+
new (context: ToolContext): Tool<TParams, TSuccessResult, TErrorResult>;
|
|
406
|
+
/**
|
|
407
|
+
* Gets the static definition of this tool.
|
|
408
|
+
*
|
|
409
|
+
* @returns The tool definition.
|
|
410
|
+
*/
|
|
411
|
+
get_definition(): ToolDefinition;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Interface that all tools must implement.
|
|
415
|
+
*
|
|
416
|
+
* @template TParams - The parameter type for the tool.
|
|
417
|
+
* @template TSuccessResult - The success result type.
|
|
418
|
+
* @template TErrorResult - The error result type.
|
|
419
|
+
*/
|
|
420
|
+
interface Tool<TParams extends Record<string, unknown> = Record<string, unknown>, TSuccessResult extends ToolSuccessResult = ToolSuccessResult, TErrorResult extends ToolErrorResult = ToolErrorResult> {
|
|
421
|
+
/**
|
|
422
|
+
* Executes the tool with the provided parameters.
|
|
423
|
+
*
|
|
424
|
+
* @param params - The parameters for tool execution.
|
|
425
|
+
* @returns A promise that resolves to either success or error result.
|
|
426
|
+
*/
|
|
427
|
+
execute(params: TParams): Promise<TSuccessResult | TErrorResult>;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Token for monitoring and responding to cancellation requests.
|
|
432
|
+
*
|
|
433
|
+
* Provides a way to check if an operation has been cancelled and to
|
|
434
|
+
* register callbacks for when cancellation occurs.
|
|
435
|
+
*/
|
|
436
|
+
declare class CancellationToken {
|
|
437
|
+
#private;
|
|
438
|
+
/**
|
|
439
|
+
* Creates a new cancellation token.
|
|
440
|
+
*
|
|
441
|
+
* @param source - Optional source that controls this token.
|
|
442
|
+
*/
|
|
443
|
+
constructor(source?: CancellationTokenSource);
|
|
444
|
+
/**
|
|
445
|
+
* Whether cancellation has been requested.
|
|
446
|
+
*/
|
|
447
|
+
get is_cancellation_requested(): boolean;
|
|
448
|
+
/**
|
|
449
|
+
* Registers a one-time handler for cancellation.
|
|
450
|
+
*
|
|
451
|
+
* @param event - The event name (always "cancel").
|
|
452
|
+
* @param handler - The callback to invoke when cancelled.
|
|
453
|
+
*/
|
|
454
|
+
once(event: "cancel", handler: () => void): void;
|
|
455
|
+
/**
|
|
456
|
+
* Removes a cancellation handler.
|
|
457
|
+
*
|
|
458
|
+
* @param event - The event name (always "cancel").
|
|
459
|
+
* @param handler - The callback to remove.
|
|
460
|
+
*/
|
|
461
|
+
off(event: "cancel", handler: () => void): void;
|
|
462
|
+
/**
|
|
463
|
+
* Throws an error if cancellation has been requested.
|
|
464
|
+
*/
|
|
465
|
+
throw_if_cancellation_requested(): void;
|
|
466
|
+
/**
|
|
467
|
+
* Creates a promise that rejects when cancellation is requested.
|
|
468
|
+
*
|
|
469
|
+
* @returns A promise that never resolves but rejects on cancellation.
|
|
470
|
+
*/
|
|
471
|
+
await_cancellation(): Promise<never>;
|
|
472
|
+
/**
|
|
473
|
+
* Creates an empty cancellation token that can never be cancelled.
|
|
474
|
+
*
|
|
475
|
+
* @returns A new cancellation token with no source.
|
|
476
|
+
*/
|
|
477
|
+
static empty(): CancellationToken;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Source for creating and controlling cancellation tokens.
|
|
481
|
+
*
|
|
482
|
+
* Allows triggering cancellation for associated tokens.
|
|
483
|
+
*/
|
|
484
|
+
declare class CancellationTokenSource {
|
|
485
|
+
#private;
|
|
486
|
+
/**
|
|
487
|
+
* Disposes the cancellation token source.
|
|
488
|
+
*
|
|
489
|
+
* This method is called automatically when using the `using` keyword.
|
|
490
|
+
*/
|
|
491
|
+
[Symbol.dispose](): void;
|
|
492
|
+
/**
|
|
493
|
+
* Gets a cancellation token associated with this source.
|
|
494
|
+
*/
|
|
495
|
+
get token(): CancellationToken;
|
|
496
|
+
/**
|
|
497
|
+
* Whether cancellation has been triggered.
|
|
498
|
+
*/
|
|
499
|
+
get is_cancelled(): boolean;
|
|
500
|
+
/**
|
|
501
|
+
* Registers a one-time handler for cancellation.
|
|
502
|
+
*
|
|
503
|
+
* @param event - The event name (always "cancel").
|
|
504
|
+
* @param handler - The callback to invoke when cancelled.
|
|
505
|
+
*/
|
|
506
|
+
once(event: "cancel", handler: () => void): void;
|
|
507
|
+
/**
|
|
508
|
+
* Removes a cancellation handler.
|
|
509
|
+
*
|
|
510
|
+
* @param event - The event name (always "cancel").
|
|
511
|
+
* @param handler - The callback to remove.
|
|
512
|
+
*/
|
|
513
|
+
off(event: "cancel", handler: () => void): void;
|
|
514
|
+
/**
|
|
515
|
+
* Triggers cancellation for all associated tokens.
|
|
516
|
+
*/
|
|
517
|
+
cancel(): void;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Error thrown when an operation is cancelled.
|
|
521
|
+
*/
|
|
522
|
+
declare class OperationCanceledError extends Error {
|
|
523
|
+
#private;
|
|
524
|
+
/**
|
|
525
|
+
* Creates a new operation cancelled error.
|
|
526
|
+
*
|
|
527
|
+
* @param token - The cancellation token associated with this error.
|
|
528
|
+
*/
|
|
529
|
+
constructor(token: CancellationToken);
|
|
530
|
+
/**
|
|
531
|
+
* The cancellation token associated with this error.
|
|
532
|
+
*/
|
|
533
|
+
get token(): CancellationToken;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Delays execution for a specified time period with cancellation support.
|
|
537
|
+
*
|
|
538
|
+
* @param ms - The number of milliseconds to sleep.
|
|
539
|
+
* @param cancellation_token - Optional token for cancelling the sleep.
|
|
540
|
+
* @returns A promise that resolves after the delay or rejects if cancelled.
|
|
541
|
+
*/
|
|
542
|
+
declare function sleep(ms: number, cancellation_token?: CancellationToken): Promise<void>;
|
|
543
|
+
/**
|
|
544
|
+
* Peeks at the first value from an async generator without consuming it.
|
|
545
|
+
*
|
|
546
|
+
* @template T - The type of values yielded by the generator.
|
|
547
|
+
* @param generator - The async generator to peek.
|
|
548
|
+
* @returns An object containing the peeked value and a new generator that includes it.
|
|
549
|
+
*/
|
|
550
|
+
declare function peek_generator<T>(generator: AsyncGenerator<T>): Promise<{
|
|
551
|
+
peeked_value: T;
|
|
552
|
+
generator: AsyncGenerator<Awaited<T>, void, unknown>;
|
|
553
|
+
}>;
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Represents a successful policy execution.
|
|
557
|
+
*
|
|
558
|
+
* @template T - The type of the result value.
|
|
559
|
+
*/
|
|
560
|
+
interface PolicySuccessResult<T> {
|
|
561
|
+
result: true;
|
|
562
|
+
/** Metadata about the policy execution. */
|
|
563
|
+
metadata: object;
|
|
564
|
+
/** The value returned by the wrapped function. */
|
|
565
|
+
value: T;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Represents a failed policy execution.
|
|
569
|
+
*/
|
|
570
|
+
interface PolicyErrorResult {
|
|
571
|
+
result: false;
|
|
572
|
+
/** Metadata about the policy execution. */
|
|
573
|
+
metadata: object;
|
|
574
|
+
/** The error that occurred. */
|
|
575
|
+
error: unknown;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Union of all possible policy execution results.
|
|
579
|
+
*
|
|
580
|
+
* @template T - The type of the success result value.
|
|
581
|
+
*/
|
|
582
|
+
type PolicyResult<T> = PolicySuccessResult<T> | PolicyErrorResult;
|
|
583
|
+
/**
|
|
584
|
+
* Base class for all policies.
|
|
585
|
+
*
|
|
586
|
+
* Policies wrap function execution to provide cross-cutting concerns like
|
|
587
|
+
* retries, rate limiting, and token budget management.
|
|
588
|
+
*/
|
|
589
|
+
declare abstract class Policy {
|
|
590
|
+
/**
|
|
591
|
+
* Executes a function with this policy applied.
|
|
592
|
+
*
|
|
593
|
+
* @template TResult - The return type of the function.
|
|
594
|
+
* @template TArgs - The argument types of the function.
|
|
595
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
596
|
+
* @param fn - The function to execute.
|
|
597
|
+
* @param args - Arguments to pass to the function.
|
|
598
|
+
* @returns A promise that resolves to the policy result.
|
|
599
|
+
*/
|
|
600
|
+
abstract execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Manages a conversation with a language model.
|
|
605
|
+
*
|
|
606
|
+
* The Conversation class handles message exchange, tool execution coordination,
|
|
607
|
+
* event emission, and state management for interactions with language models.
|
|
608
|
+
*/
|
|
609
|
+
declare class Conversation {
|
|
610
|
+
#private;
|
|
611
|
+
/**
|
|
612
|
+
* Creates a new conversation instance.
|
|
613
|
+
*
|
|
614
|
+
* @param provider - The model provider for executing requests.
|
|
615
|
+
* @param policy - The policy for controlling request execution (default: retry + rate limiting).
|
|
616
|
+
* @param context_transformer - Transformer for modifying messages before sending (default: ToolContextTransformer + CheckpointContextTransformer).
|
|
617
|
+
* @param summarization_strategy - Strategy for generating checkpoint summaries (default: DefaultSummarizationStrategy).
|
|
618
|
+
*/
|
|
619
|
+
constructor(provider: ModelProvider, policy?: Policy, context_transformer?: ContextTransformer, summarization_strategy?: SummarizationStrategy);
|
|
620
|
+
/**
|
|
621
|
+
* The unique identifier for this conversation.
|
|
622
|
+
*/
|
|
623
|
+
get id(): string;
|
|
624
|
+
set id(value: string);
|
|
625
|
+
/**
|
|
626
|
+
* The current state of the conversation.
|
|
627
|
+
*/
|
|
628
|
+
get state(): ConversationState;
|
|
629
|
+
/**
|
|
630
|
+
* The system prompt for this conversation.
|
|
631
|
+
*/
|
|
632
|
+
get system(): string | undefined;
|
|
633
|
+
set system(value: string | undefined);
|
|
634
|
+
/**
|
|
635
|
+
* The collection of tools available to the model in this conversation.
|
|
636
|
+
*/
|
|
637
|
+
get toolkit(): Readonly<ToolClass[]>;
|
|
638
|
+
set toolkit(value: ToolClass[]);
|
|
639
|
+
/**
|
|
640
|
+
* The conversation message history.
|
|
641
|
+
*/
|
|
642
|
+
get messages(): Readonly<Readonly<Message>[]>;
|
|
643
|
+
/**
|
|
644
|
+
* The most recent checkpoint state, containing the summary text and the ID
|
|
645
|
+
* of the last message included in the checkpoint. Available after a
|
|
646
|
+
* successful call to `checkpoint()`. Used by CheckpointContextTransformer
|
|
647
|
+
* to replace pre-checkpoint messages with the summary on subsequent prompts.
|
|
648
|
+
*/
|
|
649
|
+
get checkpoint_state(): Readonly<CheckpointState> | undefined;
|
|
650
|
+
set checkpoint_state(value: CheckpointState | undefined);
|
|
651
|
+
/**
|
|
652
|
+
* True when this conversation is an ephemeral child spawned to generate a
|
|
653
|
+
* checkpoint summary. Checkpoint sessions are short-lived, receive the
|
|
654
|
+
* conversation history as a summarization prompt, and are disposed after
|
|
655
|
+
* the summary is produced.
|
|
656
|
+
*/
|
|
657
|
+
get is_checkpoint(): boolean;
|
|
658
|
+
/**
|
|
659
|
+
* Disposes the conversation and releases all resources.
|
|
660
|
+
*
|
|
661
|
+
* This method is called automatically when using the `using` keyword.
|
|
662
|
+
* Emits a dispose event and transitions to the disposed state.
|
|
663
|
+
*/
|
|
664
|
+
[Symbol.dispose](): void;
|
|
665
|
+
/**
|
|
666
|
+
* Sends a text prompt to the model.
|
|
667
|
+
*
|
|
668
|
+
* @param prompt - The text prompt to send.
|
|
669
|
+
* @returns A promise that resolves with the request data, or undefined if cancelled.
|
|
670
|
+
*/
|
|
671
|
+
prompt(prompt: string): Promise<PromptRequestData | undefined>;
|
|
672
|
+
/**
|
|
673
|
+
* Sends a message with custom content to the model.
|
|
674
|
+
*
|
|
675
|
+
* @param contents - The content blocks to include in the message.
|
|
676
|
+
* @returns A promise that resolves with the request data, or undefined if cancelled.
|
|
677
|
+
*/
|
|
678
|
+
send_message(contents: UserContent[]): Promise<PromptRequestData | undefined>;
|
|
679
|
+
/**
|
|
680
|
+
* Registers a one-time event listener.
|
|
681
|
+
*
|
|
682
|
+
* @template E - The event name type.
|
|
683
|
+
* @param event_name - The name of the event to listen for.
|
|
684
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
685
|
+
*/
|
|
686
|
+
once<E extends keyof ConversationEvents>(event_name: E, listener: E extends keyof ConversationEvents ? (...arg: ConversationEvents[E]) => void : never): void;
|
|
687
|
+
/**
|
|
688
|
+
* Registers a persistent event listener.
|
|
689
|
+
*
|
|
690
|
+
* @template E - The event name type.
|
|
691
|
+
* @param event_name - The name of the event to listen for.
|
|
692
|
+
* @param listener - The callback function to invoke when the event occurs.
|
|
693
|
+
*/
|
|
694
|
+
on<E extends keyof ConversationEvents>(event_name: E, listener: E extends keyof ConversationEvents ? (...arg: ConversationEvents[E]) => void : never): void;
|
|
695
|
+
/**
|
|
696
|
+
* Removes an event listener.
|
|
697
|
+
*
|
|
698
|
+
* @template E - The event name type.
|
|
699
|
+
* @param event_name - The name of the event.
|
|
700
|
+
* @param listener - The callback function to remove.
|
|
701
|
+
*/
|
|
702
|
+
off<E extends keyof ConversationEvents>(event_name: E, listener: E extends keyof ConversationEvents ? (...arg: ConversationEvents[E]) => void : never): void;
|
|
703
|
+
/**
|
|
704
|
+
* Cancels an in-progress model response.
|
|
705
|
+
*
|
|
706
|
+
* Transitions the conversation to the "stopping" state, which triggers
|
|
707
|
+
* cancellation of the underlying request.
|
|
708
|
+
*/
|
|
709
|
+
cancel_response(): void;
|
|
710
|
+
/**
|
|
711
|
+
* Clears all messages from the conversation history.
|
|
712
|
+
*
|
|
713
|
+
* Can only be called when the conversation is idle.
|
|
714
|
+
*/
|
|
715
|
+
clear(): void;
|
|
716
|
+
/**
|
|
717
|
+
* Loads messages into the conversation history.
|
|
718
|
+
*
|
|
719
|
+
* @param messages - The messages to load.
|
|
720
|
+
*/
|
|
721
|
+
load(messages: Message[]): void;
|
|
722
|
+
/**
|
|
723
|
+
* Creates a child conversation that inherits configuration from this conversation.
|
|
724
|
+
*
|
|
725
|
+
* Child events are propagated to the parent as "child_event" events.
|
|
726
|
+
*
|
|
727
|
+
* @param fork_session - Whether to copy the current message history to the child.
|
|
728
|
+
* @param id - Optional custom ID for the child conversation.
|
|
729
|
+
* @param system_prompt - Optional system prompt override for the child.
|
|
730
|
+
* @param is_checkpoint - Whether this child is a checkpoint summarization session.
|
|
731
|
+
* @returns The newly created child conversation.
|
|
732
|
+
*/
|
|
733
|
+
spawn_child(fork_session?: boolean, id?: string, system_prompt?: string, is_checkpoint?: boolean): Conversation;
|
|
734
|
+
/**
|
|
735
|
+
* Creates a checkpoint summarizing the conversation so far.
|
|
736
|
+
*
|
|
737
|
+
* Spawns an ephemeral child conversation, sends the checkpoint prompt,
|
|
738
|
+
* and stores the resulting summary as the new checkpoint state.
|
|
739
|
+
*
|
|
740
|
+
* Requires a CheckpointContextTransformer (or composite that includes one)
|
|
741
|
+
* in the context transformer pipeline for the checkpoint state to take effect
|
|
742
|
+
* on subsequent prompts.
|
|
743
|
+
*
|
|
744
|
+
* @param config - Optional checkpoint configuration.
|
|
745
|
+
* @returns The new checkpoint state.
|
|
746
|
+
*/
|
|
747
|
+
checkpoint(config?: CheckpointConfig): Promise<CheckpointState>;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Discriminated union of all possible stream events and their payloads.
|
|
752
|
+
*/
|
|
753
|
+
type StreamListenerPayload = {
|
|
754
|
+
[E in keyof StreamReceiver]: {
|
|
755
|
+
event: E;
|
|
756
|
+
payload: Parameters<StreamReceiver[E]>;
|
|
757
|
+
};
|
|
758
|
+
}[keyof StreamReceiver];
|
|
759
|
+
/**
|
|
760
|
+
* Internal utility for converting StreamReceiver callbacks into a single event handler.
|
|
761
|
+
*
|
|
762
|
+
* This class is used internally by Conversation to manage streaming events.
|
|
763
|
+
*/
|
|
764
|
+
declare class StreamListener {
|
|
765
|
+
#private;
|
|
766
|
+
/**
|
|
767
|
+
* Creates a new stream listener.
|
|
768
|
+
*
|
|
769
|
+
* @param listener - The callback to invoke with stream events.
|
|
770
|
+
*/
|
|
771
|
+
constructor(listener: (payload: StreamListenerPayload) => void | Promise<void>);
|
|
772
|
+
/**
|
|
773
|
+
* Disposes the stream listener.
|
|
774
|
+
*
|
|
775
|
+
* This method is called automatically when using the `using` keyword.
|
|
776
|
+
*/
|
|
777
|
+
[Symbol.dispose](): void;
|
|
778
|
+
/**
|
|
779
|
+
* Creates a StreamReceiver that forwards all events to the listener.
|
|
780
|
+
*
|
|
781
|
+
* @returns A new stream receiver instance.
|
|
782
|
+
*/
|
|
783
|
+
create_receiver(): StreamReceiver;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Statistics about token usage.
|
|
788
|
+
*/
|
|
789
|
+
interface TokenStats {
|
|
790
|
+
/** Token usage from the most recent request. */
|
|
791
|
+
last_request: {
|
|
792
|
+
/** Input tokens in the last request. */
|
|
793
|
+
input: number;
|
|
794
|
+
/** Output tokens in the last request. */
|
|
795
|
+
output: number;
|
|
796
|
+
};
|
|
797
|
+
/** Cumulative token usage across all requests. */
|
|
798
|
+
total: {
|
|
799
|
+
/** Total input tokens consumed. */
|
|
800
|
+
input: number;
|
|
801
|
+
/** Total output tokens generated. */
|
|
802
|
+
output: number;
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Tracks token usage for a conversation and its children.
|
|
807
|
+
*
|
|
808
|
+
* Automatically monitors conversation events to accumulate token statistics.
|
|
809
|
+
*/
|
|
810
|
+
declare class TokenTracker {
|
|
811
|
+
#private;
|
|
812
|
+
/**
|
|
813
|
+
* Creates a new token tracker.
|
|
814
|
+
*
|
|
815
|
+
* @param conversation - The conversation to track.
|
|
816
|
+
*/
|
|
817
|
+
constructor(conversation: Conversation);
|
|
818
|
+
/**
|
|
819
|
+
* Disposes the token tracker and removes event listeners.
|
|
820
|
+
*
|
|
821
|
+
* This method is called automatically when using the `using` keyword.
|
|
822
|
+
*/
|
|
823
|
+
[Symbol.dispose](): void;
|
|
824
|
+
/**
|
|
825
|
+
* The current token statistics.
|
|
826
|
+
*/
|
|
827
|
+
get stats(): Readonly<TokenStats>;
|
|
828
|
+
/**
|
|
829
|
+
* Registers an event listener for statistics updates.
|
|
830
|
+
*
|
|
831
|
+
* @param event - The event name (always "stats_update").
|
|
832
|
+
* @param listener - The callback to invoke when statistics update.
|
|
833
|
+
*/
|
|
834
|
+
on(event: "stats_update", listener: (stats: TokenStats) => void): void;
|
|
835
|
+
/**
|
|
836
|
+
* Removes an event listener for statistics updates.
|
|
837
|
+
*
|
|
838
|
+
* @param event - The event name (always "stats_update").
|
|
839
|
+
* @param listener - The callback to remove.
|
|
840
|
+
*/
|
|
841
|
+
off(event: "stats_update", listener: (stats: TokenStats) => void): void;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* The active checkpoint state for a conversation.
|
|
846
|
+
*/
|
|
847
|
+
interface CheckpointState {
|
|
848
|
+
/** The ID of the last message included in the checkpoint. */
|
|
849
|
+
message_id: string;
|
|
850
|
+
/** The condensed summary produced by the checkpoint model call. */
|
|
851
|
+
summary: string;
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Additional context passed to transformers alongside messages.
|
|
855
|
+
*/
|
|
856
|
+
interface TransformContext {
|
|
857
|
+
/** The active checkpoint, if any. */
|
|
858
|
+
checkpoint?: CheckpointState;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Interface for transforming conversation context before and after model requests.
|
|
862
|
+
*
|
|
863
|
+
* Context transformers can modify messages before they are sent to the model
|
|
864
|
+
* and transform assistant responses before they are added to conversation history.
|
|
865
|
+
*/
|
|
866
|
+
interface ContextTransformer {
|
|
867
|
+
/**
|
|
868
|
+
* Transforms the prompt messages before sending to the model.
|
|
869
|
+
*
|
|
870
|
+
* @param messages - The messages to transform.
|
|
871
|
+
* @param context - Additional context such as checkpoint state.
|
|
872
|
+
* @returns A promise that resolves to the transformed messages.
|
|
873
|
+
*/
|
|
874
|
+
transform_prompt(messages: Message[], context?: TransformContext): Promise<Message[]>;
|
|
875
|
+
/**
|
|
876
|
+
* Transforms the assistant's completion message before adding to history.
|
|
877
|
+
*
|
|
878
|
+
* @param message - The assistant message to transform.
|
|
879
|
+
* @returns A promise that resolves to the transformed message.
|
|
880
|
+
*/
|
|
881
|
+
transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Interface for provider-level context transformers.
|
|
885
|
+
*
|
|
886
|
+
* Provider transformers normalize provider-specific quirks at the wire level.
|
|
887
|
+
* They run before conversation-level transformers and do not receive
|
|
888
|
+
* conversation context (checkpoints, etc.). Both methods are optional —
|
|
889
|
+
* most provider transformers only need one direction.
|
|
890
|
+
*/
|
|
891
|
+
interface ProviderContextTransformer {
|
|
892
|
+
/**
|
|
893
|
+
* Transforms prompt messages before sending to the model.
|
|
894
|
+
*
|
|
895
|
+
* @param messages - The messages to transform.
|
|
896
|
+
* @returns A promise that resolves to the transformed messages.
|
|
897
|
+
*/
|
|
898
|
+
transform_prompt?(messages: Message[]): Promise<Message[]>;
|
|
899
|
+
/**
|
|
900
|
+
* Transforms the assistant's completion message before adding to history.
|
|
901
|
+
*
|
|
902
|
+
* @param message - The assistant message to transform.
|
|
903
|
+
* @returns A promise that resolves to the transformed message.
|
|
904
|
+
*/
|
|
905
|
+
transform_completion?(message: AssistantMessage): Promise<AssistantMessage>;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Base interface for all content types.
|
|
910
|
+
*/
|
|
911
|
+
interface BaseContent {
|
|
912
|
+
/** Optional unique identifier for this content block. */
|
|
913
|
+
id?: string;
|
|
914
|
+
/** Optional timestamp when this content was created. */
|
|
915
|
+
timestamp?: number;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Raw content that contains provider-specific data.
|
|
919
|
+
*/
|
|
920
|
+
interface RawContent extends BaseContent {
|
|
921
|
+
type: "raw";
|
|
922
|
+
/** The kind of model provider this raw data belongs to. */
|
|
923
|
+
model_kind: string;
|
|
924
|
+
/** The raw serialized data from the provider. */
|
|
925
|
+
data: string;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Base interface for content that can be extended with custom properties.
|
|
929
|
+
*/
|
|
930
|
+
interface ExtendableContent extends BaseContent {
|
|
931
|
+
/** Optional custom properties that can be attached to this content. */
|
|
932
|
+
extended?: Record<string, unknown>;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Text content block.
|
|
936
|
+
*/
|
|
937
|
+
interface TextContent extends ExtendableContent {
|
|
938
|
+
type: "text";
|
|
939
|
+
/** The text content. */
|
|
940
|
+
text: string;
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Tool execution result content.
|
|
944
|
+
*
|
|
945
|
+
* @template TTool - The type of tool that produced this result.
|
|
946
|
+
*/
|
|
947
|
+
interface ToolResultContent<TTool extends Tool = Tool> extends ExtendableContent {
|
|
948
|
+
type: "tool_result";
|
|
949
|
+
/** The name of the tool that was executed. */
|
|
950
|
+
tool: string;
|
|
951
|
+
/** The unique identifier matching the tool request. */
|
|
952
|
+
tool_request_id: string;
|
|
953
|
+
/** The result returned by the tool execution. */
|
|
954
|
+
result: Awaited<ReturnType<TTool["execute"]>>;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Content types that can appear in user messages.
|
|
958
|
+
*/
|
|
959
|
+
type UserContent = RawContent | TextContent | ToolResultContent;
|
|
960
|
+
/**
|
|
961
|
+
* Thinking/reasoning content from the assistant.
|
|
962
|
+
*/
|
|
963
|
+
interface ThinkingMessageContent extends ExtendableContent {
|
|
964
|
+
type: "thinking";
|
|
965
|
+
/** The assistant's internal reasoning. */
|
|
966
|
+
thought: string;
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Tool invocation request from the assistant.
|
|
970
|
+
*
|
|
971
|
+
* @template TTool - The type of tool being invoked.
|
|
972
|
+
*/
|
|
973
|
+
interface ToolContent<TTool extends Tool = Tool> extends ExtendableContent {
|
|
974
|
+
type: "tool";
|
|
975
|
+
/** Unique identifier for this tool request. */
|
|
976
|
+
tool_request_id: string;
|
|
977
|
+
/** The name of the tool to invoke. */
|
|
978
|
+
tool: string;
|
|
979
|
+
/** The parameters to pass to the tool. */
|
|
980
|
+
params: Parameters<TTool["execute"]>[0];
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Content types that can appear in assistant messages.
|
|
984
|
+
*/
|
|
985
|
+
type AssistantContent = RawContent | TextContent | ThinkingMessageContent | ToolContent;
|
|
986
|
+
/**
|
|
987
|
+
* Union of all content types.
|
|
988
|
+
*/
|
|
989
|
+
type Content = UserContent | AssistantContent;
|
|
990
|
+
/**
|
|
991
|
+
* A message from the user.
|
|
992
|
+
*/
|
|
993
|
+
interface UserMessage {
|
|
994
|
+
/** Optional unique identifier for this message. */
|
|
995
|
+
id?: string;
|
|
996
|
+
/** Optional timestamp when this message was created. */
|
|
997
|
+
timestamp?: number;
|
|
998
|
+
/** The role of the message sender. */
|
|
999
|
+
role: "user";
|
|
1000
|
+
/** The content blocks in this message. */
|
|
1001
|
+
content: UserContent[];
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* A message from the assistant.
|
|
1005
|
+
*/
|
|
1006
|
+
interface AssistantMessage {
|
|
1007
|
+
/** Optional unique identifier for this message. */
|
|
1008
|
+
id?: string;
|
|
1009
|
+
/** Optional timestamp when this message was created. */
|
|
1010
|
+
timestamp?: number;
|
|
1011
|
+
/** The role of the message sender. */
|
|
1012
|
+
role: "assistant";
|
|
1013
|
+
/** The content blocks in this message. */
|
|
1014
|
+
content: AssistantContent[];
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Union of all message types.
|
|
1018
|
+
*/
|
|
1019
|
+
type Message = UserMessage | AssistantMessage;
|
|
1020
|
+
/**
|
|
1021
|
+
* Token usage information for a model request/response.
|
|
1022
|
+
*/
|
|
1023
|
+
interface Usage {
|
|
1024
|
+
/** Number of tokens used to create cache. */
|
|
1025
|
+
cache_creation_input_tokens?: number | null;
|
|
1026
|
+
/** Number of cached input tokens read. */
|
|
1027
|
+
cache_read_input_tokens?: number | null;
|
|
1028
|
+
/** Number of input tokens consumed. */
|
|
1029
|
+
input_tokens?: number | null;
|
|
1030
|
+
/** Number of output tokens generated. */
|
|
1031
|
+
output_tokens?: number | null;
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Base data for any request.
|
|
1035
|
+
*/
|
|
1036
|
+
type RequestData = {
|
|
1037
|
+
request_id?: string;
|
|
1038
|
+
};
|
|
1039
|
+
/**
|
|
1040
|
+
* Data for a prompt request that includes the user message.
|
|
1041
|
+
*/
|
|
1042
|
+
type PromptRequestData = RequestData & {
|
|
1043
|
+
message: UserMessage;
|
|
1044
|
+
};
|
|
1045
|
+
/**
|
|
1046
|
+
* Streaming response data that includes usage information.
|
|
1047
|
+
*/
|
|
1048
|
+
type CompletionStreamingResponseData = RequestData & {
|
|
1049
|
+
usage: Usage;
|
|
1050
|
+
};
|
|
1051
|
+
/**
|
|
1052
|
+
* Complete response data that includes stop reason and details.
|
|
1053
|
+
*/
|
|
1054
|
+
type CompletionResponseData = CompletionStreamingResponseData & {
|
|
1055
|
+
/** The reason why the model stopped generating. */
|
|
1056
|
+
stop_reason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use" | "error" | "other";
|
|
1057
|
+
/** Optional additional details about why the model stopped. */
|
|
1058
|
+
stop_details?: string;
|
|
1059
|
+
};
|
|
1060
|
+
/**
|
|
1061
|
+
* Represents a state change event.
|
|
1062
|
+
*
|
|
1063
|
+
* @template T - The type of state being changed.
|
|
1064
|
+
*/
|
|
1065
|
+
type ChangeEvent<T> = {
|
|
1066
|
+
current: T;
|
|
1067
|
+
previous: T;
|
|
1068
|
+
};
|
|
1069
|
+
/**
|
|
1070
|
+
* Event containing raw data from the provider.
|
|
1071
|
+
*/
|
|
1072
|
+
type RawEvent = {
|
|
1073
|
+
data: unknown;
|
|
1074
|
+
};
|
|
1075
|
+
/**
|
|
1076
|
+
* Raw event with an associated request ID.
|
|
1077
|
+
*/
|
|
1078
|
+
type RawRequestEvent = RawEvent & RequestData;
|
|
1079
|
+
/**
|
|
1080
|
+
* Event containing error information for a failed request.
|
|
1081
|
+
*/
|
|
1082
|
+
type ErrorRequestEvent = PromptRequestData & {
|
|
1083
|
+
error: unknown;
|
|
1084
|
+
};
|
|
1085
|
+
/**
|
|
1086
|
+
* Event emitted when an infrastructure or lifecycle operation fails.
|
|
1087
|
+
*
|
|
1088
|
+
* Unlike {@link ErrorRequestEvent} (which is specific to model provider request failures),
|
|
1089
|
+
* this event covers operational failures such as session save errors, fork failures,
|
|
1090
|
+
* or cleanup failures.
|
|
1091
|
+
*/
|
|
1092
|
+
interface LifecycleErrorEvent {
|
|
1093
|
+
/** The error that occurred. */
|
|
1094
|
+
error: unknown;
|
|
1095
|
+
/** A short identifier for the operation that failed (e.g., "save", "fork", "disconnect"). */
|
|
1096
|
+
operation: string;
|
|
1097
|
+
/** Optional additional context about the failure. */
|
|
1098
|
+
context?: Record<string, unknown>;
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Partial message completion event during streaming.
|
|
1102
|
+
*/
|
|
1103
|
+
type PartialMessageCompletionEvent = CompletionStreamingResponseData & {
|
|
1104
|
+
message: Partial<AssistantMessage>;
|
|
1105
|
+
};
|
|
1106
|
+
/**
|
|
1107
|
+
* Complete message event when streaming finishes.
|
|
1108
|
+
*/
|
|
1109
|
+
type FullMessageCompletionEvent = CompletionResponseData & {
|
|
1110
|
+
message: AssistantMessage;
|
|
1111
|
+
};
|
|
1112
|
+
/**
|
|
1113
|
+
* Partial content completion event during streaming.
|
|
1114
|
+
*/
|
|
1115
|
+
type PartialContentCompletionEvent = PartialMessageCompletionEvent & {
|
|
1116
|
+
content: Partial<AssistantContent>;
|
|
1117
|
+
};
|
|
1118
|
+
/**
|
|
1119
|
+
* Complete content event when a content block finishes streaming.
|
|
1120
|
+
*/
|
|
1121
|
+
type FullContentCompletionEvent = PartialMessageCompletionEvent & {
|
|
1122
|
+
content: AssistantContent;
|
|
1123
|
+
};
|
|
1124
|
+
/**
|
|
1125
|
+
* Events emitted by a Conversation instance.
|
|
1126
|
+
*/
|
|
1127
|
+
interface ConversationEvents {
|
|
1128
|
+
/** Emitted when the conversation state changes. */
|
|
1129
|
+
state_change: [ChangeEvent<ConversationState>, Conversation];
|
|
1130
|
+
/** Emitted when a prompt is sent to the model. */
|
|
1131
|
+
prompt_send: [PromptRequestData, Conversation];
|
|
1132
|
+
/** Emitted when the assistant starts generating a message. */
|
|
1133
|
+
message_start: [PartialMessageCompletionEvent, Conversation];
|
|
1134
|
+
/** Emitted during message generation with partial updates. */
|
|
1135
|
+
message_update: [PartialMessageCompletionEvent, Conversation];
|
|
1136
|
+
/** Emitted when message generation completes. */
|
|
1137
|
+
message_complete: [FullMessageCompletionEvent, Conversation];
|
|
1138
|
+
/** Emitted when a content block starts streaming. */
|
|
1139
|
+
content_start: [PartialContentCompletionEvent, Conversation];
|
|
1140
|
+
/** Emitted during content block streaming with updates. */
|
|
1141
|
+
content_update: [PartialContentCompletionEvent, Conversation];
|
|
1142
|
+
/** Emitted when a content block completes. */
|
|
1143
|
+
content_complete: [FullContentCompletionEvent, Conversation];
|
|
1144
|
+
/** Emitted when a request succeeds. */
|
|
1145
|
+
request_success: [PromptRequestData, object, Conversation];
|
|
1146
|
+
/** Emitted when a request fails. */
|
|
1147
|
+
request_error: [ErrorRequestEvent, object, Conversation];
|
|
1148
|
+
/** Emitted before a request is sent to the provider. */
|
|
1149
|
+
before_request: [RawEvent, Conversation];
|
|
1150
|
+
/** Emitted with raw request data. */
|
|
1151
|
+
raw_request: [RawRequestEvent, Conversation];
|
|
1152
|
+
/** Emitted with raw response data. */
|
|
1153
|
+
raw_response: [RawRequestEvent, Conversation];
|
|
1154
|
+
/** Emitted with raw streaming data. */
|
|
1155
|
+
raw_stream: [RawRequestEvent, Conversation];
|
|
1156
|
+
/** Emitted when a checkpoint operation begins. The payload is the child conversation used for summarization. */
|
|
1157
|
+
checkpoint_begin: [Conversation, Conversation];
|
|
1158
|
+
/** Emitted when a checkpoint operation completes. The payload is the new checkpoint state. */
|
|
1159
|
+
checkpoint_complete: [CheckpointState, Conversation];
|
|
1160
|
+
/** Emitted when a child conversation is created. */
|
|
1161
|
+
create_child: [Conversation, Conversation];
|
|
1162
|
+
/** Emitted when a child conversation emits an event. */
|
|
1163
|
+
child_event: [
|
|
1164
|
+
{
|
|
1165
|
+
[E in keyof ConversationEvents]: {
|
|
1166
|
+
event_name: E;
|
|
1167
|
+
event_args: ConversationEvents[E];
|
|
1168
|
+
};
|
|
1169
|
+
}[keyof ConversationEvents],
|
|
1170
|
+
Conversation
|
|
1171
|
+
];
|
|
1172
|
+
/** Emitted when an infrastructure or lifecycle operation fails. */
|
|
1173
|
+
lifecycle_error: [LifecycleErrorEvent, Conversation];
|
|
1174
|
+
/** Emitted when the conversation is disposed. */
|
|
1175
|
+
dispose: [Conversation];
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Interface for receiving streaming events from model providers.
|
|
1179
|
+
*/
|
|
1180
|
+
interface StreamReceiver {
|
|
1181
|
+
/** Called when a message starts streaming. */
|
|
1182
|
+
start_message: (event: Omit<PartialMessageCompletionEvent, "request_id">) => void;
|
|
1183
|
+
/** Called with message updates during streaming. */
|
|
1184
|
+
update_message: (event: Omit<PartialMessageCompletionEvent, "request_id">) => void;
|
|
1185
|
+
/** Called when a message completes. */
|
|
1186
|
+
complete_message: (event: Omit<FullMessageCompletionEvent, "request_id">) => void;
|
|
1187
|
+
/** Called when a content block starts. */
|
|
1188
|
+
start_content: (event: Omit<PartialContentCompletionEvent, "request_id">) => void;
|
|
1189
|
+
/** Called with content block updates. */
|
|
1190
|
+
update_content: (event: Omit<PartialContentCompletionEvent, "request_id">) => void;
|
|
1191
|
+
/** Called when a content block completes. */
|
|
1192
|
+
complete_content: (event: Omit<FullContentCompletionEvent, "request_id">) => void;
|
|
1193
|
+
/** Called when an error occurs. */
|
|
1194
|
+
error: (error: unknown) => void;
|
|
1195
|
+
/** Called before a request is sent. */
|
|
1196
|
+
before_request: (data: unknown) => void;
|
|
1197
|
+
/** Called with raw request data. */
|
|
1198
|
+
request_raw: (data: unknown) => void;
|
|
1199
|
+
/** Called with raw response data. */
|
|
1200
|
+
response_raw: (data: unknown) => void;
|
|
1201
|
+
/** Called with raw stream chunks. */
|
|
1202
|
+
stream_raw: (data: unknown) => void;
|
|
1203
|
+
/** Called when the stream is cancelled. */
|
|
1204
|
+
cancel: () => void;
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* Possible states of a conversation.
|
|
1208
|
+
*/
|
|
1209
|
+
type ConversationState = "idle" | "awaiting_response" | "streaming_response" | "stopping" | "disposed";
|
|
1210
|
+
/**
|
|
1211
|
+
* Request data sent to a model provider.
|
|
1212
|
+
*/
|
|
1213
|
+
interface ModelRequest {
|
|
1214
|
+
/** The conversation messages. */
|
|
1215
|
+
messages: Message[];
|
|
1216
|
+
/** Available tool definitions. */
|
|
1217
|
+
tools: ToolDefinition[];
|
|
1218
|
+
/** Optional system prompt. */
|
|
1219
|
+
system?: string;
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Interface that model providers must implement.
|
|
1223
|
+
*/
|
|
1224
|
+
interface ModelProvider {
|
|
1225
|
+
/**
|
|
1226
|
+
* Provider-level context transformers that normalize provider-specific quirks.
|
|
1227
|
+
*
|
|
1228
|
+
* These run before conversation-level transformers and are read fresh on
|
|
1229
|
+
* every request, allowing model routing providers to swap them at runtime.
|
|
1230
|
+
*/
|
|
1231
|
+
context_transformers?: ProviderContextTransformer[];
|
|
1232
|
+
/**
|
|
1233
|
+
* Executes a request against the model.
|
|
1234
|
+
*
|
|
1235
|
+
* @param request - The model request containing messages and tools.
|
|
1236
|
+
* @param receiver - The stream receiver for handling response events.
|
|
1237
|
+
* @param cancellation - Token for cancelling the request.
|
|
1238
|
+
* @returns A promise that resolves when the request completes.
|
|
1239
|
+
*/
|
|
1240
|
+
execute_request(request: ModelRequest, receiver: StreamReceiver, cancellation: CancellationToken): Promise<void>;
|
|
1241
|
+
/**
|
|
1242
|
+
* Creates a clone of this provider.
|
|
1243
|
+
*
|
|
1244
|
+
* @returns A new instance of the provider with the same configuration.
|
|
1245
|
+
*/
|
|
1246
|
+
clone(): ModelProvider;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* Configuration for a checkpoint operation.
|
|
1251
|
+
*/
|
|
1252
|
+
interface CheckpointConfig {
|
|
1253
|
+
/** Arbitrary context passed through to the prompt strategy. */
|
|
1254
|
+
context?: Record<string, unknown>;
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Strategy for generating a checkpoint summary of a conversation.
|
|
1258
|
+
*/
|
|
1259
|
+
interface SummarizationStrategy {
|
|
1260
|
+
/** Builds the messages for the checkpoint conversation from the parent's context. */
|
|
1261
|
+
build_prompt(context: SummarizationContext): Message[];
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Context provided to the summarization strategy.
|
|
1265
|
+
*/
|
|
1266
|
+
interface SummarizationContext {
|
|
1267
|
+
/** The session ID of the conversation being checkpointed. */
|
|
1268
|
+
session_id: string;
|
|
1269
|
+
/** The conversation messages since the last checkpoint (or all if first checkpoint). */
|
|
1270
|
+
messages: readonly Message[];
|
|
1271
|
+
/** The previous checkpoint state, if this is an incremental checkpoint. */
|
|
1272
|
+
previous_checkpoint?: CheckpointState;
|
|
1273
|
+
/** The system prompt of the conversation being checkpointed. */
|
|
1274
|
+
system?: string;
|
|
1275
|
+
/** Arbitrary context forwarded from CheckpointConfig. */
|
|
1276
|
+
context?: Record<string, unknown>;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/**
|
|
1280
|
+
* Default summarization strategy for checkpoints.
|
|
1281
|
+
*
|
|
1282
|
+
* Serializes the conversation context into a structured text block and appends
|
|
1283
|
+
* an instruction asking the model to produce a condensed summary.
|
|
1284
|
+
*/
|
|
1285
|
+
declare class DefaultSummarizationStrategy implements SummarizationStrategy {
|
|
1286
|
+
#private;
|
|
1287
|
+
/**
|
|
1288
|
+
* Builds the summarization prompt from the conversation context. Assembles
|
|
1289
|
+
* the previous checkpoint summary, system prompt, and recent messages into a
|
|
1290
|
+
* structured text block, then appends instructions asking the model to produce
|
|
1291
|
+
* a condensed summary suitable for continuing the conversation.
|
|
1292
|
+
*/
|
|
1293
|
+
build_prompt(context: SummarizationContext): Message[];
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* Context transformer that trims pre-checkpoint messages and inserts
|
|
1298
|
+
* the checkpoint summary as a synthetic first user message. The boundary
|
|
1299
|
+
* message (an assistant response) is retained as the natural response to
|
|
1300
|
+
* the summary, maintaining proper message alternation.
|
|
1301
|
+
*/
|
|
1302
|
+
declare class CheckpointContextTransformer implements ContextTransformer {
|
|
1303
|
+
/**
|
|
1304
|
+
* Replaces all messages before the checkpoint boundary with a synthetic user
|
|
1305
|
+
* message containing the checkpoint summary. Messages after the boundary are
|
|
1306
|
+
* kept intact. Returns messages unchanged if no checkpoint is active.
|
|
1307
|
+
*/
|
|
1308
|
+
transform_prompt(messages: Message[], context?: TransformContext): Promise<Message[]>;
|
|
1309
|
+
/** No-op. Returns the message unchanged. */
|
|
1310
|
+
transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/**
|
|
1314
|
+
* Combines multiple context transformers into a single transformation pipeline.
|
|
1315
|
+
*
|
|
1316
|
+
* Transformers are applied in the order provided.
|
|
1317
|
+
*/
|
|
1318
|
+
declare class CompositeContextTransformer implements ContextTransformer {
|
|
1319
|
+
#private;
|
|
1320
|
+
/**
|
|
1321
|
+
* Creates a new composite context transformer.
|
|
1322
|
+
*
|
|
1323
|
+
* @param transformers - The transformers to compose.
|
|
1324
|
+
*/
|
|
1325
|
+
constructor(transformers: ContextTransformer[]);
|
|
1326
|
+
/**
|
|
1327
|
+
* Transforms prompt messages by applying all transformers in sequence.
|
|
1328
|
+
*
|
|
1329
|
+
* @param messages - The messages to transform.
|
|
1330
|
+
* @returns A promise that resolves to the transformed messages.
|
|
1331
|
+
*/
|
|
1332
|
+
transform_prompt(messages: Message[], context?: TransformContext): Promise<Message[]>;
|
|
1333
|
+
/**
|
|
1334
|
+
* Transforms a completion message by applying all transformers in sequence.
|
|
1335
|
+
*
|
|
1336
|
+
* @param message - The assistant message to transform.
|
|
1337
|
+
* @returns A promise that resolves to the transformed message.
|
|
1338
|
+
*/
|
|
1339
|
+
transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Context transformer that performs no transformations.
|
|
1344
|
+
*
|
|
1345
|
+
* Returns messages unchanged.
|
|
1346
|
+
*/
|
|
1347
|
+
declare class NoopContextTransformer implements ContextTransformer {
|
|
1348
|
+
/**
|
|
1349
|
+
* Returns the prompt messages unchanged.
|
|
1350
|
+
*
|
|
1351
|
+
* @param messages - The messages to pass through.
|
|
1352
|
+
* @returns A promise that resolves to the same messages.
|
|
1353
|
+
*/
|
|
1354
|
+
transform_prompt(messages: Message[]): Promise<Message[]>;
|
|
1355
|
+
/**
|
|
1356
|
+
* Returns the completion message unchanged.
|
|
1357
|
+
*
|
|
1358
|
+
* @param message - The assistant message to pass through.
|
|
1359
|
+
* @returns A promise that resolves to the same message.
|
|
1360
|
+
*/
|
|
1361
|
+
transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* Context transformer that removes unreferenced tool invocations from message history.
|
|
1366
|
+
*
|
|
1367
|
+
* Filters out tool content blocks that don't have a corresponding tool_result,
|
|
1368
|
+
* reducing context size when tools were invoked but their results are no longer
|
|
1369
|
+
* relevant to the conversation.
|
|
1370
|
+
*/
|
|
1371
|
+
declare class ToolContextTransformer implements ContextTransformer {
|
|
1372
|
+
/**
|
|
1373
|
+
* Transforms prompt messages by removing unreferenced tool invocations.
|
|
1374
|
+
*
|
|
1375
|
+
* @param messages - The messages to transform.
|
|
1376
|
+
* @returns A promise that resolves to the transformed messages.
|
|
1377
|
+
*/
|
|
1378
|
+
transform_prompt(messages: Message[]): Promise<Message[]>;
|
|
1379
|
+
/**
|
|
1380
|
+
* Returns the completion message unchanged.
|
|
1381
|
+
*
|
|
1382
|
+
* @param message - The assistant message to pass through.
|
|
1383
|
+
* @returns A promise that resolves to the same message.
|
|
1384
|
+
*/
|
|
1385
|
+
transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
/**
|
|
1389
|
+
* Policy that combines multiple policies into a single execution chain.
|
|
1390
|
+
*
|
|
1391
|
+
* Policies are applied in the order provided, with each policy wrapping
|
|
1392
|
+
* the execution of the next.
|
|
1393
|
+
*/
|
|
1394
|
+
declare class CompositePolicy extends Policy {
|
|
1395
|
+
#private;
|
|
1396
|
+
/**
|
|
1397
|
+
* Creates a new composite policy.
|
|
1398
|
+
*
|
|
1399
|
+
* @param policies - The policies to compose. If empty, uses a NoopPolicy.
|
|
1400
|
+
*/
|
|
1401
|
+
constructor(...policies: Policy[]);
|
|
1402
|
+
/**
|
|
1403
|
+
* Executes a function with all composed policies applied.
|
|
1404
|
+
*
|
|
1405
|
+
* @template TResult - The return type of the function.
|
|
1406
|
+
* @template TArgs - The argument types of the function.
|
|
1407
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
1408
|
+
* @param fn - The function to execute.
|
|
1409
|
+
* @param args - Arguments to pass to the function.
|
|
1410
|
+
* @returns A promise that resolves to the policy result.
|
|
1411
|
+
*/
|
|
1412
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
/**
|
|
1416
|
+
* The default policy used when none is passed to Conversation.
|
|
1417
|
+
* A composite that includes retry with exponential backoff. Rate and token limits require attach(conversation) so are not in the default.
|
|
1418
|
+
*/
|
|
1419
|
+
declare function getDefaultPolicy(): Policy;
|
|
1420
|
+
|
|
1421
|
+
/**
|
|
1422
|
+
* Policy that performs no special operations.
|
|
1423
|
+
*
|
|
1424
|
+
* Simply executes the provided function with cancellation support.
|
|
1425
|
+
*/
|
|
1426
|
+
declare class NoopPolicy extends Policy {
|
|
1427
|
+
/**
|
|
1428
|
+
* Executes a function with no additional policy logic.
|
|
1429
|
+
*
|
|
1430
|
+
* @template TResult - The return type of the function.
|
|
1431
|
+
* @template TArgs - The argument types of the function.
|
|
1432
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
1433
|
+
* @param fn - The function to execute.
|
|
1434
|
+
* @param args - Arguments to pass to the function.
|
|
1435
|
+
* @returns A promise that resolves to the policy result.
|
|
1436
|
+
*/
|
|
1437
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* Configuration options for rate limiting.
|
|
1442
|
+
*/
|
|
1443
|
+
type RateLimitPolicyOptions = {
|
|
1444
|
+
/** Maximum number of requests allowed in the time period. */
|
|
1445
|
+
limit: number;
|
|
1446
|
+
/** Time period in milliseconds. */
|
|
1447
|
+
period_ms: number;
|
|
1448
|
+
};
|
|
1449
|
+
/**
|
|
1450
|
+
* Policy that enforces rate limits on operations.
|
|
1451
|
+
*
|
|
1452
|
+
* Tracks request timestamps and delays execution when the rate limit is exceeded.
|
|
1453
|
+
*/
|
|
1454
|
+
declare class RateLimitPolicy extends Policy {
|
|
1455
|
+
#private;
|
|
1456
|
+
/**
|
|
1457
|
+
* Creates a new rate limit policy.
|
|
1458
|
+
*
|
|
1459
|
+
* @param options - The rate limiting configuration.
|
|
1460
|
+
*/
|
|
1461
|
+
constructor(options: RateLimitPolicyOptions);
|
|
1462
|
+
/**
|
|
1463
|
+
* Attaches this policy to a conversation to track its requests.
|
|
1464
|
+
*
|
|
1465
|
+
* @param conversation - The conversation to monitor.
|
|
1466
|
+
*/
|
|
1467
|
+
attach(conversation: Conversation): void;
|
|
1468
|
+
/**
|
|
1469
|
+
* Executes a function with rate limiting applied.
|
|
1470
|
+
*
|
|
1471
|
+
* @template TResult - The return type of the function.
|
|
1472
|
+
* @template TArgs - The argument types of the function.
|
|
1473
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
1474
|
+
* @param fn - The function to execute.
|
|
1475
|
+
* @param args - Arguments to pass to the function.
|
|
1476
|
+
* @returns A promise that resolves to the policy result.
|
|
1477
|
+
*/
|
|
1478
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
/**
|
|
1482
|
+
* Configuration options for retry behavior.
|
|
1483
|
+
*/
|
|
1484
|
+
interface RetryPolicyOptions {
|
|
1485
|
+
/** Maximum number of attempts (including the initial attempt). */
|
|
1486
|
+
max_attempts: number;
|
|
1487
|
+
/** Initial backoff delay in milliseconds. */
|
|
1488
|
+
initial_backoff_ms: number;
|
|
1489
|
+
/** Factor by which to multiply the backoff after each retry. */
|
|
1490
|
+
backoff_factor: number;
|
|
1491
|
+
/** Optional function to determine if an error is retryable. */
|
|
1492
|
+
retryable?: (result: PolicyErrorResult) => boolean;
|
|
1493
|
+
}
|
|
1494
|
+
declare function defaultRetryable(result: PolicyErrorResult): boolean;
|
|
1495
|
+
/**
|
|
1496
|
+
* Policy that retries failed operations with exponential backoff.
|
|
1497
|
+
*
|
|
1498
|
+
* @template _TResult - The result type of the operation.
|
|
1499
|
+
*/
|
|
1500
|
+
declare class RetryPolicy<_TResult> extends Policy {
|
|
1501
|
+
#private;
|
|
1502
|
+
/**
|
|
1503
|
+
* Creates a new retry policy.
|
|
1504
|
+
*
|
|
1505
|
+
* @param options - The retry configuration options.
|
|
1506
|
+
*/
|
|
1507
|
+
constructor(options: RetryPolicyOptions);
|
|
1508
|
+
/**
|
|
1509
|
+
* Executes a function with retry logic applied.
|
|
1510
|
+
*
|
|
1511
|
+
* @template TResult - The return type of the function.
|
|
1512
|
+
* @template TArgs - The argument types of the function.
|
|
1513
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
1514
|
+
* @param fn - The function to execute.
|
|
1515
|
+
* @param args - Arguments to pass to the function.
|
|
1516
|
+
* @returns A promise that resolves to the policy result.
|
|
1517
|
+
*/
|
|
1518
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
/**
|
|
1522
|
+
* Record of token usage for a single request.
|
|
1523
|
+
*/
|
|
1524
|
+
interface TokenRecord {
|
|
1525
|
+
/** When this usage occurred. */
|
|
1526
|
+
timestamp: number;
|
|
1527
|
+
/** Number of input tokens consumed. */
|
|
1528
|
+
input_tokens: number;
|
|
1529
|
+
/** Number of output tokens generated. */
|
|
1530
|
+
output_tokens: number;
|
|
1531
|
+
}
|
|
1532
|
+
/**
|
|
1533
|
+
* Base options for token limit policy.
|
|
1534
|
+
*/
|
|
1535
|
+
type BaseOptions = {
|
|
1536
|
+
/** Time period in milliseconds for the token budget. */
|
|
1537
|
+
period_ms: number;
|
|
1538
|
+
};
|
|
1539
|
+
/**
|
|
1540
|
+
* Options for separate input and output token limits.
|
|
1541
|
+
*/
|
|
1542
|
+
type InputOutputTokenOptions = {
|
|
1543
|
+
/** Maximum input tokens allowed per period. */
|
|
1544
|
+
input_tokens_per_period: number;
|
|
1545
|
+
/** Maximum output tokens allowed per period. */
|
|
1546
|
+
output_tokens_per_period: number;
|
|
1547
|
+
};
|
|
1548
|
+
/**
|
|
1549
|
+
* Options for a combined total token limit.
|
|
1550
|
+
*/
|
|
1551
|
+
type TotalTokenOptions = {
|
|
1552
|
+
/** Maximum total tokens (input + output) allowed per period. */
|
|
1553
|
+
total_tokens_per_period: number;
|
|
1554
|
+
};
|
|
1555
|
+
/**
|
|
1556
|
+
* Configuration options for token limit policy.
|
|
1557
|
+
*
|
|
1558
|
+
* Can specify either separate input/output limits or a combined total limit.
|
|
1559
|
+
*/
|
|
1560
|
+
type TokenLimitPolicyOptions = BaseOptions & (InputOutputTokenOptions | TotalTokenOptions);
|
|
1561
|
+
/**
|
|
1562
|
+
* Policy that enforces token usage limits over a time period.
|
|
1563
|
+
*
|
|
1564
|
+
* Tracks token consumption and delays execution when limits are exceeded.
|
|
1565
|
+
*/
|
|
1566
|
+
declare class TokenLimitPolicy extends Policy {
|
|
1567
|
+
#private;
|
|
1568
|
+
/**
|
|
1569
|
+
* Creates a new token limit policy.
|
|
1570
|
+
*
|
|
1571
|
+
* @param options - The token limit configuration.
|
|
1572
|
+
*/
|
|
1573
|
+
constructor(options: TokenLimitPolicyOptions);
|
|
1574
|
+
/**
|
|
1575
|
+
* Executes a function with token limit enforcement applied.
|
|
1576
|
+
*
|
|
1577
|
+
* @template TResult - The return type of the function.
|
|
1578
|
+
* @template TArgs - The argument types of the function.
|
|
1579
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
1580
|
+
* @param fn - The function to execute.
|
|
1581
|
+
* @param args - Arguments to pass to the function.
|
|
1582
|
+
* @returns A promise that resolves to the policy result.
|
|
1583
|
+
*/
|
|
1584
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
1585
|
+
/**
|
|
1586
|
+
* Attaches this policy to a conversation to track its token usage.
|
|
1587
|
+
*
|
|
1588
|
+
* @param conversation - The conversation to monitor.
|
|
1589
|
+
*/
|
|
1590
|
+
attach(conversation: Conversation): void;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
/**
|
|
1594
|
+
* Returns undefined if the value is empty (empty array or object with no defined keys).
|
|
1595
|
+
*
|
|
1596
|
+
* @template T - The type of the value.
|
|
1597
|
+
* @param obj - The value to check.
|
|
1598
|
+
* @returns The value if non-empty, otherwise undefined.
|
|
1599
|
+
*/
|
|
1600
|
+
declare function undefined_if_empty<T = unknown>(obj: T): T | undefined;
|
|
1601
|
+
/**
|
|
1602
|
+
* Recursively merges two values together.
|
|
1603
|
+
*
|
|
1604
|
+
* For objects, properties are merged recursively. For arrays, elements are concatenated.
|
|
1605
|
+
* For primitives, the supplemental value replaces the original.
|
|
1606
|
+
*
|
|
1607
|
+
* @template T - The type of values being merged.
|
|
1608
|
+
* @param original - The original value.
|
|
1609
|
+
* @param supplemental - The value to merge in.
|
|
1610
|
+
* @returns The merged result.
|
|
1611
|
+
*/
|
|
1612
|
+
declare function deep_merge<T = unknown>(original: T, supplemental: T): T;
|
|
1613
|
+
|
|
1614
|
+
/**
|
|
1615
|
+
* Makes all properties of an object and their nested properties optional.
|
|
1616
|
+
*
|
|
1617
|
+
* @template T - The object type to make deeply partial.
|
|
1618
|
+
*/
|
|
1619
|
+
type DeepPartial<T> = Partial<{
|
|
1620
|
+
[K in keyof T]: Partial<T[K]>;
|
|
1621
|
+
}>;
|
|
1622
|
+
/**
|
|
1623
|
+
* Flattens intersection types for better IDE display.
|
|
1624
|
+
*
|
|
1625
|
+
* @template T - The type to prettify.
|
|
1626
|
+
*/
|
|
1627
|
+
type Prettify<T> = {
|
|
1628
|
+
[K in keyof T]: T[K];
|
|
1629
|
+
} & {};
|
|
1630
|
+
|
|
1631
|
+
export { type ArrayParameterType, type AssistantContent, type AssistantMessage, type BooleanParameterType, CancellationToken, CancellationTokenSource, type ChangeEvent, type CheckpointConfig, CheckpointContextTransformer, type CheckpointState, type CompletionResponseData, type CompletionStreamingResponseData, CompositeContextTransformer, CompositePolicy, type Content, type ContextTransformer, Conversation, type ConversationEvents, type ConversationState, type DeepPartial, DefaultSummarizationStrategy, type EnumParameterType, type ErrorRequestEvent, type ExtendableContent, type FullContentCompletionEvent, type FullMessageCompletionEvent, type LifecycleErrorEvent, type Message, type ModelProvider, type ModelRequest, NoopContextTransformer, NoopPolicy, type NumberParameterType, type ObjectParameterType, OperationCanceledError, type ParameterType, type PartialContentCompletionEvent, type PartialMessageCompletionEvent, Policy, type PolicyErrorResult, type PolicyResult, type PolicySuccessResult, type Prettify, type PrimitiveParameterType, type PromptRequestData, type ProviderContextTransformer, RateLimitPolicy, type RateLimitPolicyOptions, type RawContent, type RawEvent, type RawRequestEvent, type RequestData, RetryPolicy, type RetryPolicyOptions, StreamListener, type StreamListenerPayload, type StreamReceiver, type StringParameterType, type SummarizationContext, type SummarizationStrategy, type TextContent, type ThinkingMessageContent, TokenLimitPolicy, type TokenLimitPolicyOptions, type TokenRecord, type TokenStats, TokenTracker, type Tool, type ToolClass, type ToolContent, type ToolContext, ToolContextTransformer, type ToolDefinition, type ToolErrorResult, type ToolParameterDefinition, type ToolResult, type ToolResultContent, type ToolSuccessResult, type TransformContext, type Usage, type UserContent, type UserMessage, Workflow, type WorkflowEndEvent, type WorkflowEvents, WorkflowManager, type WorkflowManagerEvents, type WorkflowState, deep_merge, defaultRetryable, getDefaultPolicy, peek_generator, sleep, undefined_if_empty };
|