@cuylabs/agent-core 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/{builder-BKkipazh.d.ts → builder-BRvqCcIk.d.ts} +2 -2
- package/dist/{resolver-DOfZ-xuk.d.ts → capability-resolver-CgRGsWVX.d.ts} +1 -1
- package/dist/{chunk-3C4VKG4P.js → chunk-3HNO5SVI.js} +273 -807
- package/dist/chunk-5K7AQVOU.js +619 -0
- package/dist/{chunk-QAQADS4X.js → chunk-BNSHUWCV.js} +1 -0
- package/dist/{chunk-O2ZCFQL6.js → chunk-CDTV2UYU.js} +86 -1
- package/dist/chunk-IEFIQENH.js +73 -0
- package/dist/chunk-N7P4PN3O.js +84 -0
- package/dist/{chunk-QWFMX226.js → chunk-QGOGIP7T.js} +148 -15
- package/dist/chunk-VNQBHPCT.js +398 -0
- package/dist/{chunk-X635CM2F.js → chunk-ZPMACVZK.js} +1 -1
- package/dist/context/index.js +1 -1
- package/dist/host/index.d.ts +45 -0
- package/dist/host/index.js +8 -0
- package/dist/{index-DZQJD_hp.d.ts → index-C33hlD6H.d.ts} +12 -7
- package/dist/{index-ipP3_ztp.d.ts → index-CfBGYrpd.d.ts} +121 -2
- package/dist/index.d.ts +107 -126
- package/dist/index.js +321 -601
- package/dist/inference/index.d.ts +59 -0
- package/dist/inference/index.js +25 -0
- package/dist/middleware/index.d.ts +7 -4
- package/dist/middleware/index.js +5 -3
- package/dist/models/index.d.ts +104 -2
- package/dist/models/index.js +40 -6
- package/dist/prompt/index.d.ts +9 -6
- package/dist/reasoning/index.d.ts +54 -8
- package/dist/reasoning/index.js +2 -3
- package/dist/{registry-CuRWWtcT.d.ts → registry-BDLIHOQB.d.ts} +1 -1
- package/dist/{runner-G1wxEgac.d.ts → runner-DSKaEz3z.d.ts} +35 -8
- package/dist/runtime/index.d.ts +41 -7
- package/dist/runtime/index.js +15 -6
- package/dist/scope/index.d.ts +10 -0
- package/dist/scope/index.js +14 -0
- package/dist/{session-manager-Uawm2Le7.d.ts → session-manager-B_CWGTsl.d.ts} +1 -1
- package/dist/skill/index.d.ts +7 -5
- package/dist/storage/index.d.ts +2 -2
- package/dist/sub-agent/index.d.ts +12 -8
- package/dist/tool/index.d.ts +7 -4
- package/dist/tool/index.js +4 -3
- package/dist/{tool-pFAnJc5Y.d.ts → tool-Db1Ue-1U.d.ts} +1 -1
- package/dist/{tool-DYp6-cC3.d.ts → tool-HUtkiVBx.d.ts} +5 -99
- package/dist/tracking/index.d.ts +3 -1
- package/dist/types-9jGQUjqW.d.ts +29 -0
- package/dist/types-CHiPh8U2.d.ts +100 -0
- package/dist/types-CqDZTh4d.d.ts +335 -0
- package/dist/types-FRpzzg_9.d.ts +355 -0
- package/package.json +19 -8
- package/dist/capabilities/index.d.ts +0 -97
- package/dist/capabilities/index.js +0 -46
- package/dist/chunk-6TDTQJ4P.js +0 -116
- package/dist/chunk-DWYX7ASF.js +0 -26
- package/dist/chunk-FG4MD5MU.js +0 -54
- package/dist/config-D2xeGEHK.d.ts +0 -52
- package/dist/identifiers-BLUxFqV_.d.ts +0 -12
- package/dist/network-D76DS5ot.d.ts +0 -5
- package/dist/types-BWo810L_.d.ts +0 -648
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { LanguageModel, TelemetrySettings, ModelMessage, ToolSet } from 'ai';
|
|
2
|
+
import { T as Tool } from './tool-Db1Ue-1U.js';
|
|
3
|
+
import { T as ToolHost } from './types-CHiPh8U2.js';
|
|
4
|
+
import { f as StepProcessingResult, A as AgentEvent, g as AgentTurnBoundaryKind, d as StreamProvider, M as MiddlewareRunner } from './runner-DSKaEz3z.js';
|
|
5
|
+
import { g as InterventionController, T as ToolExecutionMode, I as InferenceStreamInput } from './types-FRpzzg_9.js';
|
|
6
|
+
import { R as ReasoningLevel } from './types-CQaXbRsS.js';
|
|
7
|
+
import { N as NormalizedToolReplayPolicy, d as TurnTrackerContext } from './tool-HUtkiVBx.js';
|
|
8
|
+
import { T as TokenUsage, M as Message } from './messages-BYWGn8TY.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Doom-loop handling contracts for repeated tool invocations.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Response from a doom-loop handler.
|
|
15
|
+
*/
|
|
16
|
+
type DoomLoopAction = "allow" | "deny" | "remember";
|
|
17
|
+
/**
|
|
18
|
+
* Doom-loop detection request.
|
|
19
|
+
*/
|
|
20
|
+
interface DoomLoopRequest {
|
|
21
|
+
/** The tool being called repeatedly */
|
|
22
|
+
tool: string;
|
|
23
|
+
/** How many times it has been called with the same input */
|
|
24
|
+
repeatCount: number;
|
|
25
|
+
/** The repeated input */
|
|
26
|
+
input: unknown;
|
|
27
|
+
/** Session ID */
|
|
28
|
+
sessionId: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Handler for doom-loop situations.
|
|
32
|
+
*/
|
|
33
|
+
type DoomLoopHandler = (request: DoomLoopRequest) => Promise<DoomLoopAction>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Options for processing a streamed model result.
|
|
37
|
+
*/
|
|
38
|
+
interface StepProcessingOptions {
|
|
39
|
+
/** Session ID (optional - for tracking purposes) */
|
|
40
|
+
sessionID?: string;
|
|
41
|
+
/** Abort signal */
|
|
42
|
+
abort: AbortSignal;
|
|
43
|
+
/** Event callback */
|
|
44
|
+
onEvent: (event: AgentEvent) => void | Promise<void>;
|
|
45
|
+
/** Doom loop threshold (default: 3) */
|
|
46
|
+
doomLoopThreshold?: number;
|
|
47
|
+
/** Enforce doom loop (throw error vs warn) when no handler is provided */
|
|
48
|
+
enforceDoomLoop?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Handler for doom loop detection.
|
|
51
|
+
* Allows user choice: allow, deny, or remember.
|
|
52
|
+
*/
|
|
53
|
+
onDoomLoop?: DoomLoopHandler;
|
|
54
|
+
/** Tools that are "remembered" (allowed without asking) */
|
|
55
|
+
rememberedDoomLoopTools?: Set<string>;
|
|
56
|
+
/** Token limit for context overflow detection */
|
|
57
|
+
contextTokenLimit?: number;
|
|
58
|
+
/** Callback for context overflow */
|
|
59
|
+
onContextOverflow?: (tokens: number, limit: number) => void | Promise<void>;
|
|
60
|
+
/** Current step number */
|
|
61
|
+
currentStep?: number;
|
|
62
|
+
/** Max steps */
|
|
63
|
+
maxSteps?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Result of stream processing.
|
|
67
|
+
*/
|
|
68
|
+
interface StepProcessingOutput {
|
|
69
|
+
/** Processing result */
|
|
70
|
+
result: StepProcessingResult;
|
|
71
|
+
/** Accumulated text */
|
|
72
|
+
text: string;
|
|
73
|
+
/** Tool results */
|
|
74
|
+
toolResults: Array<{
|
|
75
|
+
toolName: string;
|
|
76
|
+
toolCallId: string;
|
|
77
|
+
result: unknown;
|
|
78
|
+
}>;
|
|
79
|
+
/** Final usage */
|
|
80
|
+
usage?: TokenUsage;
|
|
81
|
+
/** Finish reason reported by the most recent completed step */
|
|
82
|
+
finishReason?: string;
|
|
83
|
+
/** Error if any */
|
|
84
|
+
error?: Error;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @deprecated Use `StepProcessingOptions`.
|
|
88
|
+
*/
|
|
89
|
+
type ProcessorOptions = StepProcessingOptions;
|
|
90
|
+
/**
|
|
91
|
+
* @deprecated Use `StepProcessingOutput`.
|
|
92
|
+
*/
|
|
93
|
+
type ProcessorOutput = StepProcessingOutput;
|
|
94
|
+
|
|
95
|
+
interface AgentTurnEngineOptions {
|
|
96
|
+
sessionId: string;
|
|
97
|
+
startedAt: string;
|
|
98
|
+
getToolReplayPolicy?: (toolName: string) => NormalizedToolReplayPolicy | undefined;
|
|
99
|
+
}
|
|
100
|
+
interface AgentTurnBoundaryMetadata {
|
|
101
|
+
step?: number;
|
|
102
|
+
messageRole?: Message["role"];
|
|
103
|
+
pendingToolCallCount?: number;
|
|
104
|
+
}
|
|
105
|
+
interface AgentTurnStepCommitToolCall {
|
|
106
|
+
toolCallId: string;
|
|
107
|
+
toolName: string;
|
|
108
|
+
args: unknown;
|
|
109
|
+
replayPolicy?: NormalizedToolReplayPolicy;
|
|
110
|
+
}
|
|
111
|
+
interface AgentTurnStepCommitToolResult {
|
|
112
|
+
toolCallId: string;
|
|
113
|
+
toolName: string;
|
|
114
|
+
result: unknown;
|
|
115
|
+
replayPolicy?: NormalizedToolReplayPolicy;
|
|
116
|
+
}
|
|
117
|
+
interface AgentTurnStepCommitSnapshot {
|
|
118
|
+
toolCalls: AgentTurnStepCommitToolCall[];
|
|
119
|
+
toolResults: AgentTurnStepCommitToolResult[];
|
|
120
|
+
}
|
|
121
|
+
interface CreateAgentTurnStepCommitBatchOptions {
|
|
122
|
+
assistantMessageId?: string;
|
|
123
|
+
toolMessageIds?: Record<string, string>;
|
|
124
|
+
createdAt?: Date;
|
|
125
|
+
}
|
|
126
|
+
interface AgentTurnCommitBatch {
|
|
127
|
+
startBoundary: AgentEvent & {
|
|
128
|
+
type: "turn-boundary";
|
|
129
|
+
};
|
|
130
|
+
finishBoundary: AgentEvent & {
|
|
131
|
+
type: "turn-boundary";
|
|
132
|
+
};
|
|
133
|
+
messages: Message[];
|
|
134
|
+
}
|
|
135
|
+
interface AgentTurnOutputCommitOptions {
|
|
136
|
+
text: string;
|
|
137
|
+
usage?: TokenUsage;
|
|
138
|
+
createdAt?: Date;
|
|
139
|
+
id?: string;
|
|
140
|
+
}
|
|
141
|
+
type AgentTurnBoundaryEvent = AgentEvent & {
|
|
142
|
+
type: "turn-boundary";
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
type AgentTurnPhase = "initializing" | "committing-input" | "running-model" | "running-tools" | "committing-step" | "committing-output" | "completed" | "failed";
|
|
146
|
+
interface AgentTurnBoundarySnapshot {
|
|
147
|
+
kind: AgentTurnBoundaryKind;
|
|
148
|
+
createdAt: string;
|
|
149
|
+
step?: number;
|
|
150
|
+
messageRole?: "system" | "user" | "assistant" | "tool";
|
|
151
|
+
pendingToolCallCount?: number;
|
|
152
|
+
}
|
|
153
|
+
interface AgentTurnActiveToolCall {
|
|
154
|
+
toolCallId: string;
|
|
155
|
+
toolName: string;
|
|
156
|
+
input: unknown;
|
|
157
|
+
startedAt: string;
|
|
158
|
+
replayPolicy?: NormalizedToolReplayPolicy;
|
|
159
|
+
}
|
|
160
|
+
interface AgentTurnResolvedToolCall {
|
|
161
|
+
toolCallId: string;
|
|
162
|
+
toolName: string;
|
|
163
|
+
outcome: "result" | "error";
|
|
164
|
+
value: unknown;
|
|
165
|
+
resolvedAt: string;
|
|
166
|
+
replayPolicy?: NormalizedToolReplayPolicy;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Neutral state model for a single in-flight agent turn.
|
|
170
|
+
*
|
|
171
|
+
* This is intentionally infrastructure-agnostic. Runtime adapters can persist
|
|
172
|
+
* or inspect it without coupling `agent-core` to Dapr, queues, or workflow
|
|
173
|
+
* engines. The current shape is suitable for durable tracking and forms the
|
|
174
|
+
* basis for future workflow-level resume support.
|
|
175
|
+
*/
|
|
176
|
+
interface AgentTurnState {
|
|
177
|
+
sessionId: string;
|
|
178
|
+
phase: AgentTurnPhase;
|
|
179
|
+
step: number;
|
|
180
|
+
maxSteps?: number;
|
|
181
|
+
response: string;
|
|
182
|
+
usage: TokenUsage;
|
|
183
|
+
eventCount: number;
|
|
184
|
+
activeToolCalls: AgentTurnActiveToolCall[];
|
|
185
|
+
resolvedToolCalls: AgentTurnResolvedToolCall[];
|
|
186
|
+
lastFinishReason?: string;
|
|
187
|
+
lastBoundary?: AgentTurnBoundarySnapshot;
|
|
188
|
+
lastEvent?: AgentEvent;
|
|
189
|
+
error?: string;
|
|
190
|
+
startedAt: string;
|
|
191
|
+
updatedAt: string;
|
|
192
|
+
}
|
|
193
|
+
interface CreateAgentTurnStateOptions {
|
|
194
|
+
sessionId: string;
|
|
195
|
+
startedAt: string;
|
|
196
|
+
}
|
|
197
|
+
interface AgentTurnStateAdvanceOptions {
|
|
198
|
+
toolReplayPolicy?: NormalizedToolReplayPolicy;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Create the initial neutral state for a single agent turn.
|
|
202
|
+
*/
|
|
203
|
+
declare function createAgentTurnState(options: CreateAgentTurnStateOptions): AgentTurnState;
|
|
204
|
+
/**
|
|
205
|
+
* Advance neutral turn state from a streamed `AgentEvent`.
|
|
206
|
+
*
|
|
207
|
+
* This reducer deliberately models coarse but deterministic boundaries:
|
|
208
|
+
* step start, tool activity, step finish, completion, and failure. Runtime
|
|
209
|
+
* adapters can persist the resulting state now, and future resume-capable
|
|
210
|
+
* engines can build on the same state transitions.
|
|
211
|
+
*/
|
|
212
|
+
declare function advanceAgentTurnState(state: AgentTurnState, event: AgentEvent, updatedAt: string, options?: AgentTurnStateAdvanceOptions): AgentTurnState;
|
|
213
|
+
/**
|
|
214
|
+
* Mark the turn as failed when execution aborts without a terminal `error`
|
|
215
|
+
* event. This covers exceptions thrown by the stream itself or runtime
|
|
216
|
+
* wrappers around the loop.
|
|
217
|
+
*/
|
|
218
|
+
declare function failAgentTurnState(state: AgentTurnState, error: Error, updatedAt: string): AgentTurnState;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Neutral internal turn engine for one in-flight agent turn.
|
|
222
|
+
*/
|
|
223
|
+
declare class AgentTurnEngine {
|
|
224
|
+
private turnState;
|
|
225
|
+
private readonly pendingToolCalls;
|
|
226
|
+
private readonly pendingToolResults;
|
|
227
|
+
private readonly getToolReplayPolicy?;
|
|
228
|
+
constructor(options: AgentTurnEngineOptions);
|
|
229
|
+
getState(): AgentTurnState;
|
|
230
|
+
hasPendingToolCalls(): boolean;
|
|
231
|
+
createStepCommitSnapshot(): AgentTurnStepCommitSnapshot | undefined;
|
|
232
|
+
recordEvent(event: AgentEvent, updatedAt: string): AgentTurnState;
|
|
233
|
+
createBoundaryEvent(boundary: AgentTurnBoundaryKind, metadata?: AgentTurnBoundaryMetadata): AgentTurnBoundaryEvent;
|
|
234
|
+
createInputCommit(options: {
|
|
235
|
+
content: string;
|
|
236
|
+
system?: string;
|
|
237
|
+
createdAt?: Date;
|
|
238
|
+
id?: string;
|
|
239
|
+
}): AgentTurnCommitBatch;
|
|
240
|
+
createInterventionCommit(options: {
|
|
241
|
+
id: string;
|
|
242
|
+
content: string;
|
|
243
|
+
createdAt?: Date;
|
|
244
|
+
}): AgentTurnCommitBatch;
|
|
245
|
+
consumeStepCommit(step: number): AgentTurnCommitBatch | undefined;
|
|
246
|
+
createOutputCommit(options: AgentTurnOutputCommitOptions): AgentTurnCommitBatch | undefined;
|
|
247
|
+
}
|
|
248
|
+
declare function createAgentTurnEngine(options: AgentTurnEngineOptions): AgentTurnEngine;
|
|
249
|
+
|
|
250
|
+
interface AgentTurnCommitOptions {
|
|
251
|
+
emitMessages?: boolean;
|
|
252
|
+
}
|
|
253
|
+
type AgentTurnCommitApplier = (batch: AgentTurnCommitBatch, options?: AgentTurnCommitOptions) => AsyncGenerator<AgentEvent>;
|
|
254
|
+
interface AgentTurnStepRuntimeConfig {
|
|
255
|
+
model: LanguageModel;
|
|
256
|
+
cwd: string;
|
|
257
|
+
temperature?: number;
|
|
258
|
+
topP?: number;
|
|
259
|
+
maxOutputTokens?: number;
|
|
260
|
+
maxSteps: number;
|
|
261
|
+
doomLoopThreshold?: number;
|
|
262
|
+
enforceDoomLoop?: boolean;
|
|
263
|
+
onDoomLoop?: DoomLoopHandler;
|
|
264
|
+
contextWindow?: number;
|
|
265
|
+
streamProvider?: StreamProvider;
|
|
266
|
+
telemetry?: TelemetrySettings;
|
|
267
|
+
}
|
|
268
|
+
interface PrepareModelStepOptions {
|
|
269
|
+
sessionId: string;
|
|
270
|
+
step: number;
|
|
271
|
+
systemPrompts: string[];
|
|
272
|
+
messages: Message[];
|
|
273
|
+
toModelMessages: (messages: Message[]) => ModelMessage[];
|
|
274
|
+
abort: AbortSignal;
|
|
275
|
+
tools: Record<string, Tool.Info>;
|
|
276
|
+
mcpTools?: ToolSet;
|
|
277
|
+
config: AgentTurnStepRuntimeConfig;
|
|
278
|
+
host?: ToolHost;
|
|
279
|
+
turnTracker?: TurnTrackerContext;
|
|
280
|
+
intervention?: InterventionController;
|
|
281
|
+
middleware?: MiddlewareRunner;
|
|
282
|
+
reasoningLevel?: ReasoningLevel;
|
|
283
|
+
toolExecutionMode?: ToolExecutionMode;
|
|
284
|
+
}
|
|
285
|
+
interface PreparedAgentModelStep {
|
|
286
|
+
step: number;
|
|
287
|
+
messages: Message[];
|
|
288
|
+
modelMessages: ModelMessage[];
|
|
289
|
+
inferenceInput: InferenceStreamInput;
|
|
290
|
+
stepProcessing: {
|
|
291
|
+
maxSteps: number;
|
|
292
|
+
doomLoopThreshold?: number;
|
|
293
|
+
enforceDoomLoop?: boolean;
|
|
294
|
+
onDoomLoop?: DoomLoopHandler;
|
|
295
|
+
contextTokenLimit?: number;
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
interface RunModelStepOptions {
|
|
299
|
+
preparedStep: PreparedAgentModelStep;
|
|
300
|
+
turnEngine: AgentTurnEngine;
|
|
301
|
+
applyCommitBatch: AgentTurnCommitApplier;
|
|
302
|
+
rememberedDoomLoopTools?: Set<string>;
|
|
303
|
+
}
|
|
304
|
+
interface CommitStepOptions {
|
|
305
|
+
step: number;
|
|
306
|
+
finishReason?: string;
|
|
307
|
+
turnEngine: AgentTurnEngine;
|
|
308
|
+
applyCommitBatch: AgentTurnCommitApplier;
|
|
309
|
+
}
|
|
310
|
+
interface CommitOutputOptions {
|
|
311
|
+
text: string;
|
|
312
|
+
usage?: StepProcessingOutput["usage"];
|
|
313
|
+
turnEngine: AgentTurnEngine;
|
|
314
|
+
applyCommitBatch: AgentTurnCommitApplier;
|
|
315
|
+
}
|
|
316
|
+
interface RunToolBatchOptions {
|
|
317
|
+
sessionId: string;
|
|
318
|
+
snapshot: AgentTurnStepCommitSnapshot;
|
|
319
|
+
tools: Record<string, Tool.Info>;
|
|
320
|
+
cwd: string;
|
|
321
|
+
abort: AbortSignal;
|
|
322
|
+
host?: ToolHost;
|
|
323
|
+
turnTracker?: TurnTrackerContext;
|
|
324
|
+
middleware?: MiddlewareRunner;
|
|
325
|
+
turnState?: AgentTurnState;
|
|
326
|
+
}
|
|
327
|
+
interface RunToolBatchResult {
|
|
328
|
+
snapshot: AgentTurnStepCommitSnapshot;
|
|
329
|
+
turnState?: AgentTurnState;
|
|
330
|
+
events: Array<Extract<AgentEvent, {
|
|
331
|
+
type: "tool-result" | "tool-error";
|
|
332
|
+
}>>;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export { type AgentTurnActiveToolCall as A, advanceAgentTurnState as B, type CommitOutputOptions as C, type DoomLoopAction as D, createAgentTurnEngine as E, createAgentTurnState as F, failAgentTurnState as G, type PrepareModelStepOptions as P, type RunModelStepOptions as R, type StepProcessingOptions as S, type AgentTurnBoundaryMetadata as a, type AgentTurnBoundarySnapshot as b, type AgentTurnCommitApplier as c, type AgentTurnCommitBatch as d, type AgentTurnCommitOptions as e, AgentTurnEngine as f, type AgentTurnEngineOptions as g, type AgentTurnPhase as h, type AgentTurnResolvedToolCall as i, type AgentTurnState as j, type AgentTurnStateAdvanceOptions as k, type AgentTurnStepCommitSnapshot as l, type AgentTurnStepCommitToolCall as m, type AgentTurnStepCommitToolResult as n, type AgentTurnStepRuntimeConfig as o, type CommitStepOptions as p, type CreateAgentTurnStateOptions as q, type CreateAgentTurnStepCommitBatchOptions as r, type DoomLoopHandler as s, type DoomLoopRequest as t, type PreparedAgentModelStep as u, type ProcessorOptions as v, type ProcessorOutput as w, type RunToolBatchOptions as x, type RunToolBatchResult as y, type StepProcessingOutput as z };
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { StreamTextResult, ToolSet, Output, LanguageModel, ModelMessage, TelemetrySettings } from 'ai';
|
|
2
|
+
import { T as Tool } from './tool-Db1Ue-1U.js';
|
|
3
|
+
import { T as ToolHost } from './types-CHiPh8U2.js';
|
|
4
|
+
import { S as StreamChunk, d as StreamProvider, M as MiddlewareRunner, e as ModelCallInput } from './runner-DSKaEz3z.js';
|
|
5
|
+
import { R as ReasoningLevel } from './types-CQaXbRsS.js';
|
|
6
|
+
import { d as TurnTrackerContext } from './tool-HUtkiVBx.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Intervention Controller for @cuylabs/agent-core
|
|
10
|
+
*
|
|
11
|
+
* Manages mid-turn message injection into running agent turns.
|
|
12
|
+
* Uses Vercel AI SDK v6's `prepareStep` hook for zero-overhead
|
|
13
|
+
* integration at step boundaries (between LLM calls in a multi-step turn).
|
|
14
|
+
*
|
|
15
|
+
* Instead of polling a queue after each tool execution, this leverages
|
|
16
|
+
* the SDK's native step lifecycle. The
|
|
17
|
+
* `prepareStep` callback fires before every LLM call, giving us a
|
|
18
|
+
* natural injection point with no custom loop machinery.
|
|
19
|
+
*
|
|
20
|
+
* Two injection modes:
|
|
21
|
+
* - **Immediate**: `intervene(msg)` — injected at the next step boundary
|
|
22
|
+
* - **Deferred**: `queueNext(msg)` — held until the turn completes
|
|
23
|
+
*/
|
|
24
|
+
/** A queued intervention message waiting to be applied */
|
|
25
|
+
interface PendingIntervention {
|
|
26
|
+
/** Unique identifier for tracking */
|
|
27
|
+
readonly id: string;
|
|
28
|
+
/** The user message to inject */
|
|
29
|
+
readonly message: string;
|
|
30
|
+
/** When the intervention was created */
|
|
31
|
+
readonly createdAt: Date;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Callback fired when an intervention is applied to a step.
|
|
35
|
+
*
|
|
36
|
+
* The agent uses this to push `intervention-applied` events into
|
|
37
|
+
* the streaming event queue so they reach the consumer in the
|
|
38
|
+
* correct order relative to other events.
|
|
39
|
+
*/
|
|
40
|
+
type OnInterventionApplied = (intervention: PendingIntervention) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Manages mid-turn message injection for agents.
|
|
43
|
+
*
|
|
44
|
+
* This is the core primitive that connects user redirection intent
|
|
45
|
+
* to the AI SDK's multi-step loop. The flow:
|
|
46
|
+
*
|
|
47
|
+
* 1. Consumer calls `agent.intervene("focus on auth.ts")` from any async context
|
|
48
|
+
* 2. The message is queued as a `PendingIntervention`
|
|
49
|
+
* 3. At the next step boundary, `prepareStep` drains the queue
|
|
50
|
+
* 4. The message is appended to the LLM's input messages
|
|
51
|
+
* 5. The LLM responds to the redirect naturally
|
|
52
|
+
*
|
|
53
|
+
* Thread safety: All operations are synchronous and run on the
|
|
54
|
+
* same event loop. No locks needed — Node.js guarantees ordering.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const ctrl = new InterventionController();
|
|
59
|
+
*
|
|
60
|
+
* // Queue an intervention (from a UI handler, WebSocket, etc.)
|
|
61
|
+
* const id = ctrl.intervene("stop refactoring, fix the failing test first");
|
|
62
|
+
*
|
|
63
|
+
* // Later, in prepareStep:
|
|
64
|
+
* const pending = ctrl.drainImmediate();
|
|
65
|
+
* // → [{ id, message: "stop refactoring...", createdAt }]
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare class InterventionController {
|
|
69
|
+
/** Immediate interventions — applied at the next step boundary */
|
|
70
|
+
private immediate;
|
|
71
|
+
/** Deferred messages — held until the turn completes */
|
|
72
|
+
private deferred;
|
|
73
|
+
/**
|
|
74
|
+
* Callback fired when an intervention is wired into a step.
|
|
75
|
+
* Set by the Agent before starting a chat turn, cleared after.
|
|
76
|
+
*/
|
|
77
|
+
onApplied?: OnInterventionApplied;
|
|
78
|
+
/**
|
|
79
|
+
* Inject a message at the next LLM step boundary.
|
|
80
|
+
*
|
|
81
|
+
* The message is appended as a user message to the conversation
|
|
82
|
+
* before the next LLM call in the current multi-step turn. The
|
|
83
|
+
* LLM will see it and can adjust its behavior accordingly.
|
|
84
|
+
*
|
|
85
|
+
* Safe to call from any async context while `chat()` is running.
|
|
86
|
+
* If called when no turn is active, the message will be picked up
|
|
87
|
+
* by the first step of the next `chat()` call.
|
|
88
|
+
*
|
|
89
|
+
* @param message - The user message to inject
|
|
90
|
+
* @returns Intervention ID for tracking
|
|
91
|
+
*/
|
|
92
|
+
intervene(message: string): string;
|
|
93
|
+
/**
|
|
94
|
+
* Drain and return all pending immediate interventions.
|
|
95
|
+
* The internal queue is cleared atomically.
|
|
96
|
+
*
|
|
97
|
+
* @internal Called by the LLM stream's `prepareStep` hook
|
|
98
|
+
*/
|
|
99
|
+
drainImmediate(): PendingIntervention[];
|
|
100
|
+
/** Whether there are pending immediate interventions */
|
|
101
|
+
get hasPending(): boolean;
|
|
102
|
+
/** Number of pending immediate interventions */
|
|
103
|
+
get pendingCount(): number;
|
|
104
|
+
/**
|
|
105
|
+
* Queue a message for after the current turn completes.
|
|
106
|
+
*
|
|
107
|
+
* Unlike `intervene()`, this does **not** inject mid-turn. The
|
|
108
|
+
* message is held and available via `drainDeferred()` after
|
|
109
|
+
* `chat()` finishes. The consumer decides whether to send it
|
|
110
|
+
* as a new turn.
|
|
111
|
+
*
|
|
112
|
+
* @param message - The message to queue
|
|
113
|
+
* @returns Intervention ID for tracking
|
|
114
|
+
*/
|
|
115
|
+
queueNext(message: string): string;
|
|
116
|
+
/**
|
|
117
|
+
* Drain and return all deferred messages.
|
|
118
|
+
* The internal queue is cleared atomically.
|
|
119
|
+
*/
|
|
120
|
+
drainDeferred(): PendingIntervention[];
|
|
121
|
+
/** Whether there are deferred messages */
|
|
122
|
+
get hasDeferred(): boolean;
|
|
123
|
+
/** Number of deferred messages */
|
|
124
|
+
get deferredCount(): number;
|
|
125
|
+
/** Clear all queues (immediate + deferred) */
|
|
126
|
+
clear(): void;
|
|
127
|
+
/** Reset the controller for a new turn (clears onApplied, keeps queues) */
|
|
128
|
+
resetCallbacks(): void;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
type ErrorCategory = "rate_limit" | "overloaded" | "auth" | "invalid_request" | "context_overflow" | "content_filter" | "network" | "timeout" | "cancelled" | "unknown";
|
|
132
|
+
interface ResponseHeaders {
|
|
133
|
+
"retry-after"?: string;
|
|
134
|
+
"retry-after-ms"?: string;
|
|
135
|
+
"x-ratelimit-remaining"?: string;
|
|
136
|
+
"x-ratelimit-reset"?: string;
|
|
137
|
+
[key: string]: string | undefined;
|
|
138
|
+
}
|
|
139
|
+
interface LLMErrorOptions {
|
|
140
|
+
message: string;
|
|
141
|
+
category?: ErrorCategory;
|
|
142
|
+
status?: number;
|
|
143
|
+
headers?: ResponseHeaders;
|
|
144
|
+
cause?: Error;
|
|
145
|
+
provider?: string;
|
|
146
|
+
model?: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
declare class LLMError extends Error {
|
|
150
|
+
readonly category: ErrorCategory;
|
|
151
|
+
readonly status?: number;
|
|
152
|
+
readonly headers?: ResponseHeaders;
|
|
153
|
+
readonly provider?: string;
|
|
154
|
+
readonly model?: string;
|
|
155
|
+
readonly isRetryable: boolean;
|
|
156
|
+
readonly retryDelayMs?: number;
|
|
157
|
+
constructor(options: LLMErrorOptions);
|
|
158
|
+
static from(error: unknown, context?: Partial<LLMErrorOptions>): LLMError;
|
|
159
|
+
get description(): string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Retry logic with exponential backoff for @cuylabs/agent-core
|
|
164
|
+
*
|
|
165
|
+
* Provides configurable retry behavior with header-aware delays,
|
|
166
|
+
* exponential backoff, and abort signal support.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Retry configuration
|
|
171
|
+
*/
|
|
172
|
+
interface RetryConfig {
|
|
173
|
+
/** Maximum number of retry attempts (default: 3) */
|
|
174
|
+
maxAttempts?: number;
|
|
175
|
+
/** Initial delay in ms (default: 2000) */
|
|
176
|
+
initialDelayMs?: number;
|
|
177
|
+
/** Backoff multiplier (default: 2) */
|
|
178
|
+
backoffFactor?: number;
|
|
179
|
+
/** Maximum delay without headers in ms (default: 30000) */
|
|
180
|
+
maxDelayMs?: number;
|
|
181
|
+
/** Whether to jitter delays (default: true) */
|
|
182
|
+
jitter?: boolean;
|
|
183
|
+
/** Callback when retrying */
|
|
184
|
+
onRetry?: (attempt: number, delayMs: number, error: LLMError) => void;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Default retry configuration
|
|
188
|
+
*/
|
|
189
|
+
declare const DEFAULT_RETRY_CONFIG: Required<Omit<RetryConfig, "onRetry">>;
|
|
190
|
+
/**
|
|
191
|
+
* Tracks retry state across attempts
|
|
192
|
+
*/
|
|
193
|
+
interface RetryState {
|
|
194
|
+
/** Current attempt number (1-indexed) */
|
|
195
|
+
attempt: number;
|
|
196
|
+
/** Total errors encountered */
|
|
197
|
+
errors: LLMError[];
|
|
198
|
+
/** Whether more retries are available */
|
|
199
|
+
canRetry: boolean;
|
|
200
|
+
/** Delay until next retry (if applicable) */
|
|
201
|
+
nextDelayMs?: number;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Creates initial retry state
|
|
205
|
+
*/
|
|
206
|
+
declare function createRetryState(): RetryState;
|
|
207
|
+
/**
|
|
208
|
+
* Calculate delay for a retry attempt
|
|
209
|
+
*/
|
|
210
|
+
declare function calculateDelay(attempt: number, error: LLMError | undefined, config: Required<Omit<RetryConfig, "onRetry">>): number;
|
|
211
|
+
/**
|
|
212
|
+
* Sleep for a duration, respecting abort signal
|
|
213
|
+
*/
|
|
214
|
+
declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Execute a function with retry logic
|
|
217
|
+
*/
|
|
218
|
+
declare function withRetry<T>(fn: (attempt: number) => Promise<T>, config?: RetryConfig, signal?: AbortSignal): Promise<T>;
|
|
219
|
+
/**
|
|
220
|
+
* Options for retry handler
|
|
221
|
+
*/
|
|
222
|
+
interface RetryHandlerOptions extends RetryConfig {
|
|
223
|
+
/** Abort signal */
|
|
224
|
+
signal?: AbortSignal;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Creates a retry handler that wraps stream creation
|
|
228
|
+
*/
|
|
229
|
+
declare function createRetryHandler(options?: RetryHandlerOptions): <T>(createStream: (attempt: number) => Promise<T>) => Promise<T>;
|
|
230
|
+
/**
|
|
231
|
+
* Checks if more retries should be attempted
|
|
232
|
+
*/
|
|
233
|
+
declare function shouldRetry(error: unknown, attempt: number, maxAttempts?: number): boolean;
|
|
234
|
+
|
|
235
|
+
/** Stream result type - uses default Output for text streaming. */
|
|
236
|
+
type InferenceStreamResult = StreamTextResult<ToolSet, Output.Output<string, string, never>>;
|
|
237
|
+
/**
|
|
238
|
+
* @deprecated Use `InferenceStreamResult`.
|
|
239
|
+
*/
|
|
240
|
+
type LLMStreamResult = InferenceStreamResult;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Custom stream result type - compatible shape from external providers.
|
|
244
|
+
*/
|
|
245
|
+
type InferenceCustomResult = {
|
|
246
|
+
fullStream: AsyncIterable<StreamChunk>;
|
|
247
|
+
text: Promise<string>;
|
|
248
|
+
usage: Promise<{
|
|
249
|
+
inputTokens: number;
|
|
250
|
+
outputTokens: number;
|
|
251
|
+
totalTokens: number;
|
|
252
|
+
}>;
|
|
253
|
+
finishReason: Promise<string>;
|
|
254
|
+
};
|
|
255
|
+
/** Union type for stream results - either AI SDK or custom. */
|
|
256
|
+
type AnyInferenceResult = InferenceStreamResult | InferenceCustomResult;
|
|
257
|
+
/**
|
|
258
|
+
* @deprecated Use `InferenceCustomResult`.
|
|
259
|
+
*/
|
|
260
|
+
type CustomStreamResult = InferenceCustomResult;
|
|
261
|
+
/**
|
|
262
|
+
* @deprecated Use `AnyInferenceResult`.
|
|
263
|
+
*/
|
|
264
|
+
type AnyStreamResult = AnyInferenceResult;
|
|
265
|
+
/** Default max output tokens. */
|
|
266
|
+
declare const DEFAULT_MAX_OUTPUT_TOKENS = 32000;
|
|
267
|
+
/**
|
|
268
|
+
* @deprecated Use `DEFAULT_MAX_OUTPUT_TOKENS`.
|
|
269
|
+
*/
|
|
270
|
+
declare const OUTPUT_TOKEN_MAX = 32000;
|
|
271
|
+
/**
|
|
272
|
+
* @deprecated Use StreamProvider from types instead.
|
|
273
|
+
*/
|
|
274
|
+
type CustomStreamProvider = StreamProvider;
|
|
275
|
+
/** Control whether AI SDK tool definitions auto-execute or only expose calls. */
|
|
276
|
+
type ToolExecutionMode = "auto" | "plan";
|
|
277
|
+
/**
|
|
278
|
+
* Input for model inference stream creation.
|
|
279
|
+
*/
|
|
280
|
+
interface InferenceStreamInput {
|
|
281
|
+
/** Session ID */
|
|
282
|
+
sessionID: string;
|
|
283
|
+
/** Step number within the current turn */
|
|
284
|
+
step?: number;
|
|
285
|
+
/** Model to use */
|
|
286
|
+
model: LanguageModel;
|
|
287
|
+
/** System prompt parts */
|
|
288
|
+
system: string[];
|
|
289
|
+
/** Messages to send */
|
|
290
|
+
messages: ModelMessage[];
|
|
291
|
+
/** Abort signal */
|
|
292
|
+
abort: AbortSignal;
|
|
293
|
+
/** Tools to use */
|
|
294
|
+
tools: Record<string, Tool.Info>;
|
|
295
|
+
/** Whether tools should auto-execute or only be surfaced as tool calls. */
|
|
296
|
+
toolExecutionMode?: ToolExecutionMode;
|
|
297
|
+
/** Working directory */
|
|
298
|
+
cwd: string;
|
|
299
|
+
/** Execution environment for tools */
|
|
300
|
+
host?: ToolHost;
|
|
301
|
+
/** Temperature */
|
|
302
|
+
temperature?: number;
|
|
303
|
+
/** Top-p */
|
|
304
|
+
topP?: number;
|
|
305
|
+
/** Max output tokens */
|
|
306
|
+
maxOutputTokens?: number;
|
|
307
|
+
/** Max steps (tool iterations) */
|
|
308
|
+
maxSteps?: number;
|
|
309
|
+
/** Reasoning level for extended-thinking models */
|
|
310
|
+
reasoningLevel?: ReasoningLevel;
|
|
311
|
+
/** Retry configuration */
|
|
312
|
+
retry?: RetryConfig;
|
|
313
|
+
/** Callback for step completion */
|
|
314
|
+
onStepFinish?: (step: InferenceStepInfo) => void | Promise<void>;
|
|
315
|
+
/** Custom stream provider */
|
|
316
|
+
customStreamProvider?: StreamProvider;
|
|
317
|
+
/** Turn tracker for automatic file baseline capture */
|
|
318
|
+
turnTracker?: TurnTrackerContext;
|
|
319
|
+
/** Pre-built MCP tools */
|
|
320
|
+
mcpTools?: ToolSet;
|
|
321
|
+
/** Intervention controller for mid-turn message injection */
|
|
322
|
+
intervention?: InterventionController;
|
|
323
|
+
/** Middleware runner for lifecycle hooks */
|
|
324
|
+
middleware?: MiddlewareRunner;
|
|
325
|
+
/** AI SDK telemetry settings */
|
|
326
|
+
telemetry?: TelemetrySettings;
|
|
327
|
+
/** Internal snapshot of the resolved model request after middleware input hooks. */
|
|
328
|
+
activeModelCall?: ModelCallInput;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Step information surfaced to callers.
|
|
332
|
+
*/
|
|
333
|
+
interface InferenceStepInfo {
|
|
334
|
+
toolResults?: Array<{
|
|
335
|
+
toolName: string;
|
|
336
|
+
toolCallId: string;
|
|
337
|
+
output: unknown;
|
|
338
|
+
}>;
|
|
339
|
+
usage?: {
|
|
340
|
+
inputTokens?: number;
|
|
341
|
+
outputTokens?: number;
|
|
342
|
+
totalTokens?: number;
|
|
343
|
+
};
|
|
344
|
+
finishReason?: string;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* @deprecated Use `InferenceStreamInput`.
|
|
348
|
+
*/
|
|
349
|
+
type LLMStreamInput = InferenceStreamInput;
|
|
350
|
+
/**
|
|
351
|
+
* @deprecated Use `InferenceStepInfo`.
|
|
352
|
+
*/
|
|
353
|
+
type StepInfo = InferenceStepInfo;
|
|
354
|
+
|
|
355
|
+
export { type AnyInferenceResult as A, type CustomStreamProvider as C, DEFAULT_MAX_OUTPUT_TOKENS as D, type ErrorCategory as E, type InferenceStreamInput as I, type LLMStreamInput as L, OUTPUT_TOKEN_MAX as O, type PendingIntervention as P, type ResponseHeaders as R, type StepInfo as S, type ToolExecutionMode as T, type AnyStreamResult as a, type CustomStreamResult as b, type InferenceCustomResult as c, type InferenceStepInfo as d, type InferenceStreamResult as e, type LLMStreamResult as f, InterventionController as g, DEFAULT_RETRY_CONFIG as h, LLMError as i, type LLMErrorOptions as j, type OnInterventionApplied as k, type RetryConfig as l, type RetryHandlerOptions as m, type RetryState as n, calculateDelay as o, createRetryHandler as p, createRetryState as q, sleep as r, shouldRetry as s, withRetry as w };
|