@prestyj/agent 5.28.1 → 5.29.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +488 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +145 -5
- package/dist/index.d.ts +145 -5
- package/dist/index.js +460 -55
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Message, StreamOptions, Tool, ToolResultContent, ServerToolDefinition, Usage, StopReason, AssistantMessage } from '@prestyj/ai';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Stream rules: regex guards matched against a response WHILE it streams.
|
|
6
|
+
*
|
|
7
|
+
* A rule costs zero prompt tokens until the model actually breaks it. On a
|
|
8
|
+
* match the agent loop aborts the in-flight response, discards the partial
|
|
9
|
+
* assistant message (nothing is persisted, no partial tool call runs), appends
|
|
10
|
+
* the rule's reminder as a hidden runtime note, and retries the same step.
|
|
11
|
+
*
|
|
12
|
+
* Matching is incremental: each stream (assistant text, or one tool call's
|
|
13
|
+
* argument JSON) keeps a bounded rolling window, so a delta is scanned together
|
|
14
|
+
* with only the last `windowChars` characters before it — never the whole
|
|
15
|
+
* output. A pattern whose match spans more than the window is not detected.
|
|
16
|
+
*
|
|
17
|
+
* Tool-call arguments arrive as JSON text; they are unescaped incrementally so
|
|
18
|
+
* rules see `\n`, `"`, etc. as the model meant them, not as JSON escapes.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Which streamed output a rule watches. Thinking is never matched. */
|
|
22
|
+
type StreamRuleScope = "text" | "tool" | "both";
|
|
23
|
+
interface StreamRule {
|
|
24
|
+
/** Stable identifier; also the once-per-run dedupe key. */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Pattern tested against the rolling window. `g`/`y` flags are tolerated. */
|
|
27
|
+
pattern: RegExp;
|
|
28
|
+
scope: StreamRuleScope;
|
|
29
|
+
/** Reminder appended to the context when the rule fires. */
|
|
30
|
+
reminder: string;
|
|
31
|
+
/** Restrict tool-scope matching to these tool names. Unset = every tool. */
|
|
32
|
+
tools?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
interface StreamRulesConfig {
|
|
35
|
+
rules: readonly StreamRule[];
|
|
36
|
+
/** Rule-triggered retries allowed per agent run (one user turn). Default 3. */
|
|
37
|
+
maxRetries?: number;
|
|
38
|
+
/** Characters of prior output kept per stream for cross-delta matches. Default 1024. */
|
|
39
|
+
windowChars?: number;
|
|
40
|
+
}
|
|
41
|
+
type StreamRuleSource = "text" | "tool";
|
|
42
|
+
interface StreamRuleMatch {
|
|
43
|
+
rules: StreamRule[];
|
|
44
|
+
source: StreamRuleSource;
|
|
45
|
+
toolName?: string;
|
|
46
|
+
}
|
|
47
|
+
declare const DEFAULT_STREAM_RULE_MAX_RETRIES = 3;
|
|
48
|
+
declare const DEFAULT_STREAM_RULE_WINDOW_CHARS = 1024;
|
|
49
|
+
/** Incremental JSON string-escape decoder that tolerates escapes split across chunks. */
|
|
50
|
+
declare class JsonEscapeDecoder {
|
|
51
|
+
private pending;
|
|
52
|
+
push(chunk: string): string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Per-run rule state. Create one per `agentLoop` run; call `beginAttempt()`
|
|
56
|
+
* before every provider attempt so windows never leak across attempts.
|
|
57
|
+
*/
|
|
58
|
+
declare class StreamRuleMonitor {
|
|
59
|
+
readonly maxRetries: number;
|
|
60
|
+
private readonly rules;
|
|
61
|
+
private readonly windowChars;
|
|
62
|
+
private readonly fired;
|
|
63
|
+
private retries;
|
|
64
|
+
private textWindow;
|
|
65
|
+
private readonly toolStreams;
|
|
66
|
+
constructor(config: StreamRulesConfig);
|
|
67
|
+
/** False once the retry cap is spent or every rule has fired — skip all matching. */
|
|
68
|
+
get active(): boolean;
|
|
69
|
+
get retriesUsed(): number;
|
|
70
|
+
beginAttempt(): void;
|
|
71
|
+
checkText(delta: string): StreamRuleMatch | null;
|
|
72
|
+
checkToolArgs(id: string, toolName: string, argsDelta: string): StreamRuleMatch | null;
|
|
73
|
+
/** Consume one retry and mark the rules fired. Returns the retry ordinal (1-based). */
|
|
74
|
+
recordTrigger(rules: readonly StreamRule[]): number;
|
|
75
|
+
private match;
|
|
76
|
+
}
|
|
77
|
+
/** The hidden runtime note appended before the retry. */
|
|
78
|
+
declare function buildStreamRuleReminder(rules: readonly StreamRule[]): Message;
|
|
79
|
+
|
|
4
80
|
interface StructuredToolResult {
|
|
5
81
|
content: ToolResultContent;
|
|
6
82
|
details?: unknown;
|
|
@@ -27,6 +103,12 @@ interface AgentTool<T extends z.ZodType = z.ZodType> extends Tool {
|
|
|
27
103
|
* message — becomes unreachable.
|
|
28
104
|
*/
|
|
29
105
|
timeoutMs?: number;
|
|
106
|
+
/**
|
|
107
|
+
* Whether a mid-run steering message may preempt this tool. Defaults to
|
|
108
|
+
* true, except for atomic file mutators (`edit`, `write`, …) which always
|
|
109
|
+
* run to completion so they are never left half-applied.
|
|
110
|
+
*/
|
|
111
|
+
interruptible?: boolean;
|
|
30
112
|
execute: (args: z.infer<T>, context: ToolContext) => ToolExecuteResult | Promise<ToolExecuteResult>;
|
|
31
113
|
}
|
|
32
114
|
interface AgentTextDeltaEvent {
|
|
@@ -146,7 +228,7 @@ interface AgentTruncatedEvent {
|
|
|
146
228
|
}
|
|
147
229
|
interface AgentRetryEvent {
|
|
148
230
|
type: "retry";
|
|
149
|
-
reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch" | "runaway_toolcall";
|
|
231
|
+
reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch" | "runaway_toolcall" | "stream_rule";
|
|
150
232
|
attempt: number;
|
|
151
233
|
maxAttempts: number;
|
|
152
234
|
delayMs: number;
|
|
@@ -164,6 +246,29 @@ interface AgentRetryEvent {
|
|
|
164
246
|
*/
|
|
165
247
|
preservedChars?: number;
|
|
166
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* A stream rule matched mid-response. The attempt was aborted and discarded
|
|
251
|
+
* (no partial message persisted, no partial tool call executed), the rule's
|
|
252
|
+
* reminder was appended to the context, and the step is retried. Always
|
|
253
|
+
* followed by a silent `retry` (reason `stream_rule`) so UIs roll back the
|
|
254
|
+
* streamed partial exactly as for any other replayed attempt.
|
|
255
|
+
*/
|
|
256
|
+
interface AgentStreamRuleTriggeredEvent {
|
|
257
|
+
type: "stream_rule_triggered";
|
|
258
|
+
/** Names of the rules that matched (usually one). */
|
|
259
|
+
rules: string[];
|
|
260
|
+
source: "text" | "tool";
|
|
261
|
+
/** Tool whose streamed arguments matched, for `source: "tool"`. */
|
|
262
|
+
toolName?: string;
|
|
263
|
+
attempt: number;
|
|
264
|
+
maxAttempts: number;
|
|
265
|
+
/**
|
|
266
|
+
* ESTIMATED usage of the aborted attempt (providers report none for an
|
|
267
|
+
* aborted stream): prompt chars/4 in, streamed chars/4 out. Already added to
|
|
268
|
+
* the run's `totalUsage`.
|
|
269
|
+
*/
|
|
270
|
+
usage: Usage;
|
|
271
|
+
}
|
|
167
272
|
interface AgentToolCallDeltaEvent {
|
|
168
273
|
type: "toolcall_delta";
|
|
169
274
|
chars: number;
|
|
@@ -192,7 +297,7 @@ interface AgentFollowUpMessageEvent {
|
|
|
192
297
|
type: "follow_up_message";
|
|
193
298
|
content: Message["content"];
|
|
194
299
|
}
|
|
195
|
-
type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentCheckpointEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTurnBudgetExtendedEvent | AgentTruncatedEvent | AgentErrorEvent;
|
|
300
|
+
type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentStreamRuleTriggeredEvent | AgentTurnEndEvent | AgentCheckpointEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTurnBudgetExtendedEvent | AgentTruncatedEvent | AgentErrorEvent;
|
|
196
301
|
interface TransformContextOptions {
|
|
197
302
|
/** Force a transform after the provider reports context overflow. */
|
|
198
303
|
force?: boolean;
|
|
@@ -244,6 +349,7 @@ interface AgentOptions {
|
|
|
244
349
|
transportSessionId?: StreamOptions["transportSessionId"];
|
|
245
350
|
projectId?: StreamOptions["projectId"];
|
|
246
351
|
cacheRetention?: StreamOptions["cacheRetention"];
|
|
352
|
+
onContextPrepared?: StreamOptions["onContextPrepared"];
|
|
247
353
|
/** Stable per-session cache routing key for providers that support it. */
|
|
248
354
|
promptCacheKey?: StreamOptions["promptCacheKey"];
|
|
249
355
|
/** Override the User-Agent sent with OAuth-authenticated Anthropic requests. */
|
|
@@ -253,6 +359,10 @@ interface AgentOptions {
|
|
|
253
359
|
defaultHeaders?: StreamOptions["defaultHeaders"];
|
|
254
360
|
/** OpenAI service tier for latency-sensitive first-party API requests. */
|
|
255
361
|
serviceTier?: StreamOptions["serviceTier"];
|
|
362
|
+
/** Codex Responses-Lite request shape override (see StreamOptions). */
|
|
363
|
+
responsesLite?: StreamOptions["responsesLite"];
|
|
364
|
+
/** Codex strict tool schema override (see StreamOptions). */
|
|
365
|
+
strictTools?: StreamOptions["strictTools"];
|
|
256
366
|
/** Whether the target model supports image input. When false, image blocks
|
|
257
367
|
* in messages/tool_results are downgraded to text placeholders. Default: true. */
|
|
258
368
|
supportsImages?: boolean;
|
|
@@ -271,6 +381,14 @@ interface AgentOptions {
|
|
|
271
381
|
* against parallel fan-outs injecting huge uncached context in one turn;
|
|
272
382
|
* the largest results are trimmed (water-filling) with a re-run notice. */
|
|
273
383
|
maxTurnToolResultChars?: number;
|
|
384
|
+
/** Optional post-processing of a SUCCESSFUL tool result (after redaction,
|
|
385
|
+
* before the tool_call_end event and the provider context). Return the
|
|
386
|
+
* content unchanged to leave it alone. A throw is ignored (original kept).
|
|
387
|
+
* Used e.g. to append a warning to untrusted-content results. */
|
|
388
|
+
transformToolResult?: (call: {
|
|
389
|
+
name: string;
|
|
390
|
+
args: Record<string, unknown>;
|
|
391
|
+
}, content: ToolResultContent) => ToolResultContent;
|
|
274
392
|
/** Max consecutive pause_turn continuations before stopping (default: 5).
|
|
275
393
|
* Prevents infinite loops when server-side tools keep pausing. */
|
|
276
394
|
maxContinuations?: number;
|
|
@@ -292,6 +410,15 @@ interface AgentOptions {
|
|
|
292
410
|
* on read.
|
|
293
411
|
*/
|
|
294
412
|
getSteeringMessages?: () => Promise<Message[] | null> | Message[] | null;
|
|
413
|
+
/**
|
|
414
|
+
* Instant interrupt (codex `instant_interrupt`): subscribe to "a steering
|
|
415
|
+
* message just arrived". While tools are running, the listener preempts
|
|
416
|
+
* interruptible tools (their AbortSignal fires; unfinished calls get an
|
|
417
|
+
* "Interrupted" error result) and the loop drains steering and continues.
|
|
418
|
+
* Distinct from `signal` (the Stop button), which ends the run. Returns an
|
|
419
|
+
* unsubscribe function.
|
|
420
|
+
*/
|
|
421
|
+
onSteeringAvailable?: (listener: () => void) => () => void;
|
|
295
422
|
/**
|
|
296
423
|
* Polled when the agent would otherwise stop (no tool calls, no steering).
|
|
297
424
|
* Returns messages to inject and continue the loop. Lower priority than
|
|
@@ -312,6 +439,13 @@ interface AgentOptions {
|
|
|
312
439
|
maxTurns: number;
|
|
313
440
|
extension: number;
|
|
314
441
|
}) => Promise<boolean> | boolean;
|
|
442
|
+
/**
|
|
443
|
+
* Regex rules matched against streamed assistant text and tool-call
|
|
444
|
+
* arguments. A match aborts the attempt, discards it, appends the rule's
|
|
445
|
+
* reminder and retries the step. Each rule fires at most once per run;
|
|
446
|
+
* `maxRetries` (default 3) caps rule retries per run. Unset = no matching.
|
|
447
|
+
*/
|
|
448
|
+
streamRules?: StreamRulesConfig;
|
|
315
449
|
}
|
|
316
450
|
interface AgentResult {
|
|
317
451
|
message: AssistantMessage;
|
|
@@ -349,6 +483,7 @@ declare class Agent {
|
|
|
349
483
|
private options;
|
|
350
484
|
private steeringQueue;
|
|
351
485
|
private followUpQueue;
|
|
486
|
+
private steeringListeners;
|
|
352
487
|
constructor(options: AgentOptions);
|
|
353
488
|
/** Snapshot of the current message history. Used for session persistence. */
|
|
354
489
|
getMessages(): Message[];
|
|
@@ -359,7 +494,11 @@ declare class Agent {
|
|
|
359
494
|
*/
|
|
360
495
|
setSignal(signal: AbortSignal | undefined): void;
|
|
361
496
|
get running(): boolean;
|
|
362
|
-
/**
|
|
497
|
+
/**
|
|
498
|
+
* Queue a steering message. Instant interrupt: if tools are running, the
|
|
499
|
+
* interruptible ones are preempted right away and the message is injected
|
|
500
|
+
* before the next model call (the turn continues — unlike abort/Stop).
|
|
501
|
+
*/
|
|
363
502
|
steer(msg: Message): void;
|
|
364
503
|
/** Queue a follow-up message for injection when the agent would otherwise stop. */
|
|
365
504
|
followUp(msg: Message): void;
|
|
@@ -415,6 +554,7 @@ declare function cancelledBeforeStartText(name: string): string;
|
|
|
415
554
|
* something never ran when it did.
|
|
416
555
|
*/
|
|
417
556
|
declare function indeterminateOutcomeText(name: string): string;
|
|
557
|
+
declare function repairToolPairingAdjacent(messages: Message[]): void;
|
|
418
558
|
|
|
419
559
|
/**
|
|
420
560
|
* A local inference server (llama.cpp, vLLM, Ollama, LM Studio) can spend
|
|
@@ -425,4 +565,4 @@ declare function indeterminateOutcomeText(name: string): string;
|
|
|
425
565
|
*/
|
|
426
566
|
declare function isLocalBackendUrl(baseUrl?: string): boolean;
|
|
427
567
|
|
|
428
|
-
export { Agent, type AgentDoneEvent, type AgentErrorEvent, type AgentEvent, type AgentFollowUpMessageEvent, type AgentOptions, type AgentResult, type AgentRetryEvent, type AgentServerToolCallEvent, type AgentServerToolResultEvent, type AgentSteeringMessageEvent, AgentStream, type AgentTextDeltaEvent, type AgentThinkingDeltaEvent, type AgentTool, type AgentToolCallDeltaEvent, type AgentToolCallEndEvent, type AgentToolCallStartEvent, type AgentToolCallUpdateEvent, type AgentTurnEndEvent, type AgentTurnTiming, type StreamDiagnosticFn, type StructuredToolResult, type ToolContext, type ToolExecuteResult, type ToolExecutionMode, type TransformContextOptions, agentLoop, cancelledBeforeStartText, indeterminateOutcomeText, isAbortError, isBillingError, isContextOverflow, isLocalBackendUrl, isUsageLimitError, setStreamDiagnostic };
|
|
568
|
+
export { Agent, type AgentDoneEvent, type AgentErrorEvent, type AgentEvent, type AgentFollowUpMessageEvent, type AgentOptions, type AgentResult, type AgentRetryEvent, type AgentServerToolCallEvent, type AgentServerToolResultEvent, type AgentSteeringMessageEvent, AgentStream, type AgentStreamRuleTriggeredEvent, type AgentTextDeltaEvent, type AgentThinkingDeltaEvent, type AgentTool, type AgentToolCallDeltaEvent, type AgentToolCallEndEvent, type AgentToolCallStartEvent, type AgentToolCallUpdateEvent, type AgentTurnEndEvent, type AgentTurnTiming, DEFAULT_STREAM_RULE_MAX_RETRIES, DEFAULT_STREAM_RULE_WINDOW_CHARS, JsonEscapeDecoder, type StreamDiagnosticFn, type StreamRule, type StreamRuleMatch, StreamRuleMonitor, type StreamRuleScope, type StreamRuleSource, type StreamRulesConfig, type StructuredToolResult, type ToolContext, type ToolExecuteResult, type ToolExecutionMode, type TransformContextOptions, agentLoop, buildStreamRuleReminder, cancelledBeforeStartText, indeterminateOutcomeText, isAbortError, isBillingError, isContextOverflow, isLocalBackendUrl, isUsageLimitError, repairToolPairingAdjacent, setStreamDiagnostic };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Message, StreamOptions, Tool, ToolResultContent, ServerToolDefinition, Usage, StopReason, AssistantMessage } from '@prestyj/ai';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Stream rules: regex guards matched against a response WHILE it streams.
|
|
6
|
+
*
|
|
7
|
+
* A rule costs zero prompt tokens until the model actually breaks it. On a
|
|
8
|
+
* match the agent loop aborts the in-flight response, discards the partial
|
|
9
|
+
* assistant message (nothing is persisted, no partial tool call runs), appends
|
|
10
|
+
* the rule's reminder as a hidden runtime note, and retries the same step.
|
|
11
|
+
*
|
|
12
|
+
* Matching is incremental: each stream (assistant text, or one tool call's
|
|
13
|
+
* argument JSON) keeps a bounded rolling window, so a delta is scanned together
|
|
14
|
+
* with only the last `windowChars` characters before it — never the whole
|
|
15
|
+
* output. A pattern whose match spans more than the window is not detected.
|
|
16
|
+
*
|
|
17
|
+
* Tool-call arguments arrive as JSON text; they are unescaped incrementally so
|
|
18
|
+
* rules see `\n`, `"`, etc. as the model meant them, not as JSON escapes.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Which streamed output a rule watches. Thinking is never matched. */
|
|
22
|
+
type StreamRuleScope = "text" | "tool" | "both";
|
|
23
|
+
interface StreamRule {
|
|
24
|
+
/** Stable identifier; also the once-per-run dedupe key. */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Pattern tested against the rolling window. `g`/`y` flags are tolerated. */
|
|
27
|
+
pattern: RegExp;
|
|
28
|
+
scope: StreamRuleScope;
|
|
29
|
+
/** Reminder appended to the context when the rule fires. */
|
|
30
|
+
reminder: string;
|
|
31
|
+
/** Restrict tool-scope matching to these tool names. Unset = every tool. */
|
|
32
|
+
tools?: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
interface StreamRulesConfig {
|
|
35
|
+
rules: readonly StreamRule[];
|
|
36
|
+
/** Rule-triggered retries allowed per agent run (one user turn). Default 3. */
|
|
37
|
+
maxRetries?: number;
|
|
38
|
+
/** Characters of prior output kept per stream for cross-delta matches. Default 1024. */
|
|
39
|
+
windowChars?: number;
|
|
40
|
+
}
|
|
41
|
+
type StreamRuleSource = "text" | "tool";
|
|
42
|
+
interface StreamRuleMatch {
|
|
43
|
+
rules: StreamRule[];
|
|
44
|
+
source: StreamRuleSource;
|
|
45
|
+
toolName?: string;
|
|
46
|
+
}
|
|
47
|
+
declare const DEFAULT_STREAM_RULE_MAX_RETRIES = 3;
|
|
48
|
+
declare const DEFAULT_STREAM_RULE_WINDOW_CHARS = 1024;
|
|
49
|
+
/** Incremental JSON string-escape decoder that tolerates escapes split across chunks. */
|
|
50
|
+
declare class JsonEscapeDecoder {
|
|
51
|
+
private pending;
|
|
52
|
+
push(chunk: string): string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Per-run rule state. Create one per `agentLoop` run; call `beginAttempt()`
|
|
56
|
+
* before every provider attempt so windows never leak across attempts.
|
|
57
|
+
*/
|
|
58
|
+
declare class StreamRuleMonitor {
|
|
59
|
+
readonly maxRetries: number;
|
|
60
|
+
private readonly rules;
|
|
61
|
+
private readonly windowChars;
|
|
62
|
+
private readonly fired;
|
|
63
|
+
private retries;
|
|
64
|
+
private textWindow;
|
|
65
|
+
private readonly toolStreams;
|
|
66
|
+
constructor(config: StreamRulesConfig);
|
|
67
|
+
/** False once the retry cap is spent or every rule has fired — skip all matching. */
|
|
68
|
+
get active(): boolean;
|
|
69
|
+
get retriesUsed(): number;
|
|
70
|
+
beginAttempt(): void;
|
|
71
|
+
checkText(delta: string): StreamRuleMatch | null;
|
|
72
|
+
checkToolArgs(id: string, toolName: string, argsDelta: string): StreamRuleMatch | null;
|
|
73
|
+
/** Consume one retry and mark the rules fired. Returns the retry ordinal (1-based). */
|
|
74
|
+
recordTrigger(rules: readonly StreamRule[]): number;
|
|
75
|
+
private match;
|
|
76
|
+
}
|
|
77
|
+
/** The hidden runtime note appended before the retry. */
|
|
78
|
+
declare function buildStreamRuleReminder(rules: readonly StreamRule[]): Message;
|
|
79
|
+
|
|
4
80
|
interface StructuredToolResult {
|
|
5
81
|
content: ToolResultContent;
|
|
6
82
|
details?: unknown;
|
|
@@ -27,6 +103,12 @@ interface AgentTool<T extends z.ZodType = z.ZodType> extends Tool {
|
|
|
27
103
|
* message — becomes unreachable.
|
|
28
104
|
*/
|
|
29
105
|
timeoutMs?: number;
|
|
106
|
+
/**
|
|
107
|
+
* Whether a mid-run steering message may preempt this tool. Defaults to
|
|
108
|
+
* true, except for atomic file mutators (`edit`, `write`, …) which always
|
|
109
|
+
* run to completion so they are never left half-applied.
|
|
110
|
+
*/
|
|
111
|
+
interruptible?: boolean;
|
|
30
112
|
execute: (args: z.infer<T>, context: ToolContext) => ToolExecuteResult | Promise<ToolExecuteResult>;
|
|
31
113
|
}
|
|
32
114
|
interface AgentTextDeltaEvent {
|
|
@@ -146,7 +228,7 @@ interface AgentTruncatedEvent {
|
|
|
146
228
|
}
|
|
147
229
|
interface AgentRetryEvent {
|
|
148
230
|
type: "retry";
|
|
149
|
-
reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch" | "runaway_toolcall";
|
|
231
|
+
reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch" | "runaway_toolcall" | "stream_rule";
|
|
150
232
|
attempt: number;
|
|
151
233
|
maxAttempts: number;
|
|
152
234
|
delayMs: number;
|
|
@@ -164,6 +246,29 @@ interface AgentRetryEvent {
|
|
|
164
246
|
*/
|
|
165
247
|
preservedChars?: number;
|
|
166
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* A stream rule matched mid-response. The attempt was aborted and discarded
|
|
251
|
+
* (no partial message persisted, no partial tool call executed), the rule's
|
|
252
|
+
* reminder was appended to the context, and the step is retried. Always
|
|
253
|
+
* followed by a silent `retry` (reason `stream_rule`) so UIs roll back the
|
|
254
|
+
* streamed partial exactly as for any other replayed attempt.
|
|
255
|
+
*/
|
|
256
|
+
interface AgentStreamRuleTriggeredEvent {
|
|
257
|
+
type: "stream_rule_triggered";
|
|
258
|
+
/** Names of the rules that matched (usually one). */
|
|
259
|
+
rules: string[];
|
|
260
|
+
source: "text" | "tool";
|
|
261
|
+
/** Tool whose streamed arguments matched, for `source: "tool"`. */
|
|
262
|
+
toolName?: string;
|
|
263
|
+
attempt: number;
|
|
264
|
+
maxAttempts: number;
|
|
265
|
+
/**
|
|
266
|
+
* ESTIMATED usage of the aborted attempt (providers report none for an
|
|
267
|
+
* aborted stream): prompt chars/4 in, streamed chars/4 out. Already added to
|
|
268
|
+
* the run's `totalUsage`.
|
|
269
|
+
*/
|
|
270
|
+
usage: Usage;
|
|
271
|
+
}
|
|
167
272
|
interface AgentToolCallDeltaEvent {
|
|
168
273
|
type: "toolcall_delta";
|
|
169
274
|
chars: number;
|
|
@@ -192,7 +297,7 @@ interface AgentFollowUpMessageEvent {
|
|
|
192
297
|
type: "follow_up_message";
|
|
193
298
|
content: Message["content"];
|
|
194
299
|
}
|
|
195
|
-
type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentCheckpointEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTurnBudgetExtendedEvent | AgentTruncatedEvent | AgentErrorEvent;
|
|
300
|
+
type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentStreamRuleTriggeredEvent | AgentTurnEndEvent | AgentCheckpointEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTurnBudgetExtendedEvent | AgentTruncatedEvent | AgentErrorEvent;
|
|
196
301
|
interface TransformContextOptions {
|
|
197
302
|
/** Force a transform after the provider reports context overflow. */
|
|
198
303
|
force?: boolean;
|
|
@@ -244,6 +349,7 @@ interface AgentOptions {
|
|
|
244
349
|
transportSessionId?: StreamOptions["transportSessionId"];
|
|
245
350
|
projectId?: StreamOptions["projectId"];
|
|
246
351
|
cacheRetention?: StreamOptions["cacheRetention"];
|
|
352
|
+
onContextPrepared?: StreamOptions["onContextPrepared"];
|
|
247
353
|
/** Stable per-session cache routing key for providers that support it. */
|
|
248
354
|
promptCacheKey?: StreamOptions["promptCacheKey"];
|
|
249
355
|
/** Override the User-Agent sent with OAuth-authenticated Anthropic requests. */
|
|
@@ -253,6 +359,10 @@ interface AgentOptions {
|
|
|
253
359
|
defaultHeaders?: StreamOptions["defaultHeaders"];
|
|
254
360
|
/** OpenAI service tier for latency-sensitive first-party API requests. */
|
|
255
361
|
serviceTier?: StreamOptions["serviceTier"];
|
|
362
|
+
/** Codex Responses-Lite request shape override (see StreamOptions). */
|
|
363
|
+
responsesLite?: StreamOptions["responsesLite"];
|
|
364
|
+
/** Codex strict tool schema override (see StreamOptions). */
|
|
365
|
+
strictTools?: StreamOptions["strictTools"];
|
|
256
366
|
/** Whether the target model supports image input. When false, image blocks
|
|
257
367
|
* in messages/tool_results are downgraded to text placeholders. Default: true. */
|
|
258
368
|
supportsImages?: boolean;
|
|
@@ -271,6 +381,14 @@ interface AgentOptions {
|
|
|
271
381
|
* against parallel fan-outs injecting huge uncached context in one turn;
|
|
272
382
|
* the largest results are trimmed (water-filling) with a re-run notice. */
|
|
273
383
|
maxTurnToolResultChars?: number;
|
|
384
|
+
/** Optional post-processing of a SUCCESSFUL tool result (after redaction,
|
|
385
|
+
* before the tool_call_end event and the provider context). Return the
|
|
386
|
+
* content unchanged to leave it alone. A throw is ignored (original kept).
|
|
387
|
+
* Used e.g. to append a warning to untrusted-content results. */
|
|
388
|
+
transformToolResult?: (call: {
|
|
389
|
+
name: string;
|
|
390
|
+
args: Record<string, unknown>;
|
|
391
|
+
}, content: ToolResultContent) => ToolResultContent;
|
|
274
392
|
/** Max consecutive pause_turn continuations before stopping (default: 5).
|
|
275
393
|
* Prevents infinite loops when server-side tools keep pausing. */
|
|
276
394
|
maxContinuations?: number;
|
|
@@ -292,6 +410,15 @@ interface AgentOptions {
|
|
|
292
410
|
* on read.
|
|
293
411
|
*/
|
|
294
412
|
getSteeringMessages?: () => Promise<Message[] | null> | Message[] | null;
|
|
413
|
+
/**
|
|
414
|
+
* Instant interrupt (codex `instant_interrupt`): subscribe to "a steering
|
|
415
|
+
* message just arrived". While tools are running, the listener preempts
|
|
416
|
+
* interruptible tools (their AbortSignal fires; unfinished calls get an
|
|
417
|
+
* "Interrupted" error result) and the loop drains steering and continues.
|
|
418
|
+
* Distinct from `signal` (the Stop button), which ends the run. Returns an
|
|
419
|
+
* unsubscribe function.
|
|
420
|
+
*/
|
|
421
|
+
onSteeringAvailable?: (listener: () => void) => () => void;
|
|
295
422
|
/**
|
|
296
423
|
* Polled when the agent would otherwise stop (no tool calls, no steering).
|
|
297
424
|
* Returns messages to inject and continue the loop. Lower priority than
|
|
@@ -312,6 +439,13 @@ interface AgentOptions {
|
|
|
312
439
|
maxTurns: number;
|
|
313
440
|
extension: number;
|
|
314
441
|
}) => Promise<boolean> | boolean;
|
|
442
|
+
/**
|
|
443
|
+
* Regex rules matched against streamed assistant text and tool-call
|
|
444
|
+
* arguments. A match aborts the attempt, discards it, appends the rule's
|
|
445
|
+
* reminder and retries the step. Each rule fires at most once per run;
|
|
446
|
+
* `maxRetries` (default 3) caps rule retries per run. Unset = no matching.
|
|
447
|
+
*/
|
|
448
|
+
streamRules?: StreamRulesConfig;
|
|
315
449
|
}
|
|
316
450
|
interface AgentResult {
|
|
317
451
|
message: AssistantMessage;
|
|
@@ -349,6 +483,7 @@ declare class Agent {
|
|
|
349
483
|
private options;
|
|
350
484
|
private steeringQueue;
|
|
351
485
|
private followUpQueue;
|
|
486
|
+
private steeringListeners;
|
|
352
487
|
constructor(options: AgentOptions);
|
|
353
488
|
/** Snapshot of the current message history. Used for session persistence. */
|
|
354
489
|
getMessages(): Message[];
|
|
@@ -359,7 +494,11 @@ declare class Agent {
|
|
|
359
494
|
*/
|
|
360
495
|
setSignal(signal: AbortSignal | undefined): void;
|
|
361
496
|
get running(): boolean;
|
|
362
|
-
/**
|
|
497
|
+
/**
|
|
498
|
+
* Queue a steering message. Instant interrupt: if tools are running, the
|
|
499
|
+
* interruptible ones are preempted right away and the message is injected
|
|
500
|
+
* before the next model call (the turn continues — unlike abort/Stop).
|
|
501
|
+
*/
|
|
363
502
|
steer(msg: Message): void;
|
|
364
503
|
/** Queue a follow-up message for injection when the agent would otherwise stop. */
|
|
365
504
|
followUp(msg: Message): void;
|
|
@@ -415,6 +554,7 @@ declare function cancelledBeforeStartText(name: string): string;
|
|
|
415
554
|
* something never ran when it did.
|
|
416
555
|
*/
|
|
417
556
|
declare function indeterminateOutcomeText(name: string): string;
|
|
557
|
+
declare function repairToolPairingAdjacent(messages: Message[]): void;
|
|
418
558
|
|
|
419
559
|
/**
|
|
420
560
|
* A local inference server (llama.cpp, vLLM, Ollama, LM Studio) can spend
|
|
@@ -425,4 +565,4 @@ declare function indeterminateOutcomeText(name: string): string;
|
|
|
425
565
|
*/
|
|
426
566
|
declare function isLocalBackendUrl(baseUrl?: string): boolean;
|
|
427
567
|
|
|
428
|
-
export { Agent, type AgentDoneEvent, type AgentErrorEvent, type AgentEvent, type AgentFollowUpMessageEvent, type AgentOptions, type AgentResult, type AgentRetryEvent, type AgentServerToolCallEvent, type AgentServerToolResultEvent, type AgentSteeringMessageEvent, AgentStream, type AgentTextDeltaEvent, type AgentThinkingDeltaEvent, type AgentTool, type AgentToolCallDeltaEvent, type AgentToolCallEndEvent, type AgentToolCallStartEvent, type AgentToolCallUpdateEvent, type AgentTurnEndEvent, type AgentTurnTiming, type StreamDiagnosticFn, type StructuredToolResult, type ToolContext, type ToolExecuteResult, type ToolExecutionMode, type TransformContextOptions, agentLoop, cancelledBeforeStartText, indeterminateOutcomeText, isAbortError, isBillingError, isContextOverflow, isLocalBackendUrl, isUsageLimitError, setStreamDiagnostic };
|
|
568
|
+
export { Agent, type AgentDoneEvent, type AgentErrorEvent, type AgentEvent, type AgentFollowUpMessageEvent, type AgentOptions, type AgentResult, type AgentRetryEvent, type AgentServerToolCallEvent, type AgentServerToolResultEvent, type AgentSteeringMessageEvent, AgentStream, type AgentStreamRuleTriggeredEvent, type AgentTextDeltaEvent, type AgentThinkingDeltaEvent, type AgentTool, type AgentToolCallDeltaEvent, type AgentToolCallEndEvent, type AgentToolCallStartEvent, type AgentToolCallUpdateEvent, type AgentTurnEndEvent, type AgentTurnTiming, DEFAULT_STREAM_RULE_MAX_RETRIES, DEFAULT_STREAM_RULE_WINDOW_CHARS, JsonEscapeDecoder, type StreamDiagnosticFn, type StreamRule, type StreamRuleMatch, StreamRuleMonitor, type StreamRuleScope, type StreamRuleSource, type StreamRulesConfig, type StructuredToolResult, type ToolContext, type ToolExecuteResult, type ToolExecutionMode, type TransformContextOptions, agentLoop, buildStreamRuleReminder, cancelledBeforeStartText, indeterminateOutcomeText, isAbortError, isBillingError, isContextOverflow, isLocalBackendUrl, isUsageLimitError, repairToolPairingAdjacent, setStreamDiagnostic };
|