@wrongstack/core 0.1.9 → 0.1.10
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/agent-bridge-6KPqsFx6.d.ts +33 -0
- package/dist/compactor-B4mQZXf2.d.ts +17 -0
- package/dist/config-BU9f_5yH.d.ts +193 -0
- package/dist/{provider-txgB0Oq9.d.ts → context-BmM2xGUZ.d.ts} +532 -472
- package/dist/coordination/index.d.ts +694 -0
- package/dist/coordination/index.js +1995 -0
- package/dist/coordination/index.js.map +1 -0
- package/dist/defaults/index.d.ts +34 -2309
- package/dist/defaults/index.js +3893 -3803
- package/dist/defaults/index.js.map +1 -1
- package/dist/events-BMNaEFZl.d.ts +218 -0
- package/dist/execution/index.d.ts +260 -0
- package/dist/execution/index.js +1625 -0
- package/dist/execution/index.js.map +1 -0
- package/dist/index.d.ts +47 -10
- package/dist/index.js +6617 -6093
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/index.d.ts +10 -0
- package/dist/infrastructure/index.js +575 -0
- package/dist/infrastructure/index.js.map +1 -0
- package/dist/input-reader-E-ffP2ee.d.ts +12 -0
- package/dist/kernel/index.d.ts +15 -4
- package/dist/kernel/index.js.map +1 -1
- package/dist/logger-BH6AE0W9.d.ts +24 -0
- package/dist/logger-BMQgxvdy.d.ts +12 -0
- package/dist/mcp-servers-Dzgg4x1w.d.ts +100 -0
- package/dist/memory-CEXuo7sz.d.ts +16 -0
- package/dist/mode-CV077NjV.d.ts +27 -0
- package/dist/models/index.d.ts +60 -0
- package/dist/models/index.js +621 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models-registry-DqzwpBQy.d.ts +46 -0
- package/dist/models-registry-Y2xbog0E.d.ts +95 -0
- package/dist/multi-agent-fmkRHtof.d.ts +283 -0
- package/dist/observability/index.d.ts +353 -0
- package/dist/observability/index.js +691 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability-BhnVLBLS.d.ts +67 -0
- package/dist/path-resolver-CPRj4bFY.d.ts +10 -0
- package/dist/path-resolver-DBjaoXFq.d.ts +54 -0
- package/dist/plugin-DJk6LL8B.d.ts +434 -0
- package/dist/renderer-rk_1Swwc.d.ts +158 -0
- package/dist/sdd/index.d.ts +206 -0
- package/dist/sdd/index.js +864 -0
- package/dist/sdd/index.js.map +1 -0
- package/dist/secret-scrubber-CicHLN4G.d.ts +31 -0
- package/dist/secret-scrubber-DF88luOe.d.ts +54 -0
- package/dist/secret-vault-DoISxaKO.d.ts +19 -0
- package/dist/security/index.d.ts +30 -0
- package/dist/security/index.js +524 -0
- package/dist/security/index.js.map +1 -0
- package/dist/selector-BbJqiEP4.d.ts +51 -0
- package/dist/session-reader-Drq8RvJu.d.ts +150 -0
- package/dist/skill-DhfSizKv.d.ts +72 -0
- package/dist/storage/index.d.ts +382 -0
- package/dist/storage/index.js +1530 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/{system-prompt-vAB0F54-.d.ts → system-prompt-BC_8ypCG.d.ts} +1 -1
- package/dist/task-graph-BITvWt4t.d.ts +160 -0
- package/dist/tool-executor-CpuJPYm9.d.ts +97 -0
- package/dist/types/index.d.ts +26 -4
- package/dist/types/index.js +1787 -4
- package/dist/types/index.js.map +1 -1
- package/dist/utils/index.d.ts +49 -2
- package/dist/utils/index.js +100 -2
- package/dist/utils/index.js.map +1 -1
- package/package.json +34 -2
- package/dist/mode-Pjt5vMS6.d.ts +0 -815
- package/dist/session-reader-9sOTgmeC.d.ts +0 -1087
|
@@ -46,11 +46,34 @@ interface ImageBlock {
|
|
|
46
46
|
url?: string;
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Chain-of-thought / extended-thinking content emitted by the model.
|
|
51
|
+
*
|
|
52
|
+
* Both Anthropic extended thinking (`{type:'thinking', thinking, signature}`)
|
|
53
|
+
* and DeepSeek reasoning mode (top-level `reasoning_content` on the assistant
|
|
54
|
+
* message) require this content to be echoed back verbatim on the next
|
|
55
|
+
* request, otherwise the provider returns 400:
|
|
56
|
+
* - Anthropic: "The `content[].thinking` in the thinking mode must be passed back"
|
|
57
|
+
* - DeepSeek: "The `reasoning_content` in the thinking mode must be passed back"
|
|
58
|
+
*
|
|
59
|
+
* `signature` is Anthropic-specific (an opaque integrity blob). DeepSeek
|
|
60
|
+
* doesn't issue a signature — the field is absent for that provider.
|
|
61
|
+
*
|
|
62
|
+
* Per Anthropic, thinking blocks MUST appear before any text/tool_use blocks
|
|
63
|
+
* in an assistant message. Stream builders preserve that order.
|
|
64
|
+
*/
|
|
65
|
+
interface ThinkingBlock {
|
|
66
|
+
type: 'thinking';
|
|
67
|
+
thinking: string;
|
|
68
|
+
signature?: string;
|
|
69
|
+
providerMeta?: Record<string, unknown>;
|
|
70
|
+
}
|
|
71
|
+
type ContentBlock = TextBlock | ToolUseBlock | ToolResultBlock | ImageBlock | ThinkingBlock;
|
|
50
72
|
declare function isTextBlock(b: ContentBlock): b is TextBlock;
|
|
51
73
|
declare function isToolUseBlock(b: ContentBlock): b is ToolUseBlock;
|
|
52
74
|
declare function isToolResultBlock(b: ContentBlock): b is ToolResultBlock;
|
|
53
75
|
declare function isImageBlock(b: ContentBlock): b is ImageBlock;
|
|
76
|
+
declare function isThinkingBlock(b: ContentBlock): b is ThinkingBlock;
|
|
54
77
|
|
|
55
78
|
type MessageRole = 'user' | 'assistant' | 'system';
|
|
56
79
|
interface Message {
|
|
@@ -60,379 +83,115 @@ interface Message {
|
|
|
60
83
|
declare function asBlocks(content: string | ContentBlock[]): ContentBlock[];
|
|
61
84
|
declare function asText(content: string | ContentBlock[]): string;
|
|
62
85
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
type
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
ts: string;
|
|
90
|
-
model: string;
|
|
91
|
-
messageCount: number;
|
|
92
|
-
} | {
|
|
93
|
-
type: 'llm_response';
|
|
94
|
-
ts: string;
|
|
95
|
-
content: ContentBlock[];
|
|
96
|
-
stopReason: string;
|
|
97
|
-
usage: Usage;
|
|
98
|
-
} | {
|
|
99
|
-
type: 'tool_use';
|
|
100
|
-
ts: string;
|
|
101
|
-
name: string;
|
|
102
|
-
id: string;
|
|
103
|
-
input: unknown;
|
|
104
|
-
} | {
|
|
105
|
-
type: 'tool_result';
|
|
106
|
-
ts: string;
|
|
107
|
-
id: string;
|
|
108
|
-
content: unknown;
|
|
109
|
-
isError: boolean;
|
|
110
|
-
} | {
|
|
111
|
-
type: 'compaction';
|
|
112
|
-
ts: string;
|
|
113
|
-
before: number;
|
|
114
|
-
after: number;
|
|
115
|
-
} | {
|
|
116
|
-
type: 'error';
|
|
117
|
-
ts: string;
|
|
118
|
-
message: string;
|
|
119
|
-
phase: string;
|
|
120
|
-
} | {
|
|
121
|
-
type: 'session_end';
|
|
122
|
-
ts: string;
|
|
123
|
-
usage: Usage;
|
|
124
|
-
} | {
|
|
125
|
-
type: 'mode_changed';
|
|
126
|
-
ts: string;
|
|
127
|
-
from: string;
|
|
128
|
-
to: string;
|
|
129
|
-
} | {
|
|
130
|
-
type: 'task_created';
|
|
131
|
-
ts: string;
|
|
132
|
-
taskId: string;
|
|
133
|
-
title: string;
|
|
134
|
-
} | {
|
|
135
|
-
type: 'task_updated';
|
|
136
|
-
ts: string;
|
|
137
|
-
taskId: string;
|
|
138
|
-
status: string;
|
|
139
|
-
} | {
|
|
140
|
-
type: 'task_completed';
|
|
141
|
-
ts: string;
|
|
142
|
-
taskId: string;
|
|
143
|
-
title: string;
|
|
144
|
-
} | {
|
|
145
|
-
type: 'task_failed';
|
|
146
|
-
ts: string;
|
|
147
|
-
taskId: string;
|
|
148
|
-
title: string;
|
|
149
|
-
error: string;
|
|
150
|
-
} | {
|
|
151
|
-
type: 'agent_spawned';
|
|
152
|
-
ts: string;
|
|
153
|
-
agentId: string;
|
|
154
|
-
role: string;
|
|
155
|
-
} | {
|
|
156
|
-
type: 'agent_stopped';
|
|
157
|
-
ts: string;
|
|
158
|
-
agentId: string;
|
|
159
|
-
} | {
|
|
160
|
-
type: 'agent_error';
|
|
161
|
-
ts: string;
|
|
162
|
-
agentId: string;
|
|
163
|
-
error: string;
|
|
164
|
-
} | {
|
|
165
|
-
type: 'spec_parsed';
|
|
166
|
-
ts: string;
|
|
167
|
-
specId: string;
|
|
168
|
-
title: string;
|
|
169
|
-
completeness: number;
|
|
170
|
-
} | {
|
|
171
|
-
type: 'spec_analyzed';
|
|
172
|
-
ts: string;
|
|
173
|
-
specId: string;
|
|
174
|
-
gaps: string[];
|
|
175
|
-
} | {
|
|
176
|
-
type: 'skill_activated';
|
|
177
|
-
ts: string;
|
|
178
|
-
skillName: string;
|
|
179
|
-
} | {
|
|
180
|
-
type: 'skill_deactivated';
|
|
181
|
-
ts: string;
|
|
182
|
-
skillName: string;
|
|
183
|
-
} | {
|
|
184
|
-
type: 'tool_call_start';
|
|
185
|
-
ts: string;
|
|
186
|
-
name: string;
|
|
187
|
-
id: string;
|
|
188
|
-
input: unknown;
|
|
189
|
-
} | {
|
|
190
|
-
type: 'tool_call_end';
|
|
191
|
-
ts: string;
|
|
192
|
-
name: string;
|
|
193
|
-
id: string;
|
|
194
|
-
durationMs: number;
|
|
195
|
-
outputSize: number;
|
|
196
|
-
} | {
|
|
197
|
-
type: 'message_truncated';
|
|
198
|
-
ts: string;
|
|
199
|
-
before: number;
|
|
200
|
-
after: number;
|
|
201
|
-
};
|
|
202
|
-
interface SessionSummary {
|
|
203
|
-
id: string;
|
|
204
|
-
title: string;
|
|
205
|
-
startedAt: string;
|
|
206
|
-
model: string;
|
|
207
|
-
provider: string;
|
|
208
|
-
tokenTotal: number;
|
|
209
|
-
}
|
|
210
|
-
interface SessionData {
|
|
211
|
-
metadata: SessionMetadata;
|
|
212
|
-
events: SessionEvent[];
|
|
213
|
-
messages: Message[];
|
|
214
|
-
usage: Usage;
|
|
215
|
-
}
|
|
216
|
-
interface ResumedSession {
|
|
217
|
-
writer: SessionWriter;
|
|
218
|
-
data: SessionData;
|
|
219
|
-
}
|
|
220
|
-
interface SessionStore {
|
|
221
|
-
create(meta: Omit<SessionMetadata, 'startedAt'>): Promise<SessionWriter>;
|
|
222
|
-
load(id: string): Promise<SessionData>;
|
|
86
|
+
/**
|
|
87
|
+
* WrongStack error hierarchy.
|
|
88
|
+
*
|
|
89
|
+
* Every error thrown by the framework is a `WrongStackError` with a
|
|
90
|
+
* machine-readable `code`, a `subsystem` tag, and a `severity` level.
|
|
91
|
+
* This lets consumers (CLI, TUI, plugins, tests) branch on structured
|
|
92
|
+
* data instead of parsing error messages.
|
|
93
|
+
*/
|
|
94
|
+
type ErrorCode = 'PROVIDER_RATE_LIMITED' | 'PROVIDER_AUTH_FAILED' | 'PROVIDER_OVERLOADED' | 'PROVIDER_INVALID_REQUEST' | 'PROVIDER_SERVER_ERROR' | 'PROVIDER_NETWORK_ERROR' | 'PROVIDER_CONTEXT_OVERFLOW' | 'TOOL_NOT_FOUND' | 'TOOL_PERMISSION_DENIED' | 'TOOL_EXECUTION_FAILED' | 'TOOL_TIMEOUT' | 'TOOL_INPUT_INVALID' | 'CONFIG_INVALID' | 'CONFIG_NOT_FOUND' | 'CONFIG_PARSE_FAILED' | 'CONFIG_MIGRATION_NEEDED' | 'PLUGIN_LOAD_FAILED' | 'PLUGIN_API_MISMATCH' | 'PLUGIN_MISSING_DEPENDENCY' | 'AGENT_ITERATION_LIMIT' | 'AGENT_CONTEXT_OVERFLOW' | 'AGENT_ABORTED' | 'AGENT_RUN_FAILED' | 'SESSION_NOT_FOUND' | 'SESSION_CORRUPTED' | 'SESSION_WRITE_FAILED' | 'CONTAINER_TOKEN_ALREADY_BOUND' | 'CONTAINER_TOKEN_NOT_BOUND' | 'REGISTRY_DUPLICATE' | 'REGISTRY_NOT_FOUND' | 'UNKNOWN';
|
|
95
|
+
type ErrorSubsystem = 'provider' | 'tool' | 'config' | 'plugin' | 'agent' | 'session' | 'container' | 'general';
|
|
96
|
+
type ErrorSeverity = 'fatal' | 'error' | 'warning';
|
|
97
|
+
declare class WrongStackError extends Error {
|
|
98
|
+
readonly code: ErrorCode;
|
|
99
|
+
readonly subsystem: ErrorSubsystem;
|
|
100
|
+
readonly severity: ErrorSeverity;
|
|
101
|
+
readonly recoverable: boolean;
|
|
102
|
+
readonly context?: Record<string, unknown>;
|
|
103
|
+
constructor(opts: {
|
|
104
|
+
message: string;
|
|
105
|
+
code: ErrorCode;
|
|
106
|
+
subsystem: ErrorSubsystem;
|
|
107
|
+
severity?: ErrorSeverity;
|
|
108
|
+
recoverable?: boolean;
|
|
109
|
+
context?: Record<string, unknown>;
|
|
110
|
+
cause?: unknown;
|
|
111
|
+
});
|
|
223
112
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
* (messages + usage) so the caller can hydrate a Context. A
|
|
227
|
-
* `session_resumed` marker is appended for audit.
|
|
113
|
+
* Render a one-line user-facing description.
|
|
114
|
+
* Subclasses should override for domain-specific formatting.
|
|
228
115
|
*/
|
|
229
|
-
|
|
230
|
-
list(limit?: number): Promise<SessionSummary[]>;
|
|
231
|
-
delete(id: string): Promise<void>;
|
|
232
|
-
}
|
|
233
|
-
interface SessionWriter {
|
|
234
|
-
readonly id: string;
|
|
235
|
-
append(event: SessionEvent): Promise<void>;
|
|
236
|
-
close(): Promise<void>;
|
|
116
|
+
describe(): string;
|
|
237
117
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Tool execution errors — thrown by ToolExecutor and individual tools.
|
|
120
|
+
*/
|
|
121
|
+
declare class ToolError extends WrongStackError {
|
|
122
|
+
readonly toolName: string;
|
|
123
|
+
constructor(opts: {
|
|
124
|
+
message: string;
|
|
125
|
+
code: Extract<ErrorCode, 'TOOL_NOT_FOUND' | 'TOOL_PERMISSION_DENIED' | 'TOOL_EXECUTION_FAILED' | 'TOOL_TIMEOUT' | 'TOOL_INPUT_INVALID'>;
|
|
126
|
+
toolName: string;
|
|
127
|
+
recoverable?: boolean;
|
|
128
|
+
context?: Record<string, unknown>;
|
|
129
|
+
cause?: unknown;
|
|
130
|
+
});
|
|
246
131
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
reset(): void;
|
|
132
|
+
/**
|
|
133
|
+
* Config loading / validation errors.
|
|
134
|
+
*/
|
|
135
|
+
declare class ConfigError extends WrongStackError {
|
|
136
|
+
constructor(opts: {
|
|
137
|
+
message: string;
|
|
138
|
+
code: Extract<ErrorCode, 'CONFIG_INVALID' | 'CONFIG_NOT_FOUND' | 'CONFIG_PARSE_FAILED' | 'CONFIG_MIGRATION_NEEDED'>;
|
|
139
|
+
context?: Record<string, unknown>;
|
|
140
|
+
cause?: unknown;
|
|
141
|
+
});
|
|
258
142
|
}
|
|
259
|
-
|
|
260
143
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
* `Context` today doubles as both a DI bag (provider, session, tokenCounter,
|
|
264
|
-
* cwd, …) and a mutable state container (messages, todos, meta). That makes
|
|
265
|
-
* it hard to test (every test reconstructs the full bag) and easy to abuse
|
|
266
|
-
* (any tool can swap the provider mid-run).
|
|
267
|
-
*
|
|
268
|
-
* `RunEnv` is the immutable half: a read-only projection that subsystems
|
|
269
|
-
* can hold instead of the whole `Context`. It's a view, not a copy — pulling
|
|
270
|
-
* a `RunEnv` from a `Context` is O(1) and reflects the same underlying
|
|
271
|
-
* references. The opposite direction (set things on Context) still works,
|
|
272
|
-
* and `extractRunEnv` rebuilds the view if you need a snapshot.
|
|
273
|
-
*
|
|
274
|
-
* Migration path: new APIs accept `RunEnv` instead of `Context` when they
|
|
275
|
-
* only need read access. Existing APIs continue to accept `Context` until
|
|
276
|
-
* a full split is scheduled.
|
|
144
|
+
* Plugin loading / lifecycle errors.
|
|
277
145
|
*/
|
|
278
|
-
|
|
279
|
-
readonly
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
readonly tools: readonly Tool[];
|
|
146
|
+
declare class PluginError extends WrongStackError {
|
|
147
|
+
readonly pluginName: string;
|
|
148
|
+
constructor(opts: {
|
|
149
|
+
message: string;
|
|
150
|
+
code: Extract<ErrorCode, 'PLUGIN_LOAD_FAILED' | 'PLUGIN_API_MISMATCH' | 'PLUGIN_MISSING_DEPENDENCY'>;
|
|
151
|
+
pluginName: string;
|
|
152
|
+
context?: Record<string, unknown>;
|
|
153
|
+
cause?: unknown;
|
|
154
|
+
});
|
|
288
155
|
}
|
|
289
156
|
/**
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* references), but the view itself can't be mutated.
|
|
293
|
-
*
|
|
294
|
-
* Use this in subsystems that want to declare "I only need read access to
|
|
295
|
-
* the env" without rewriting their signature to accept the full Context.
|
|
296
|
-
*/
|
|
297
|
-
declare function extractRunEnv(ctx: Context): RunEnv;
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Observable wrapper for mutable conversation state. Production code should
|
|
301
|
-
* mutate messages, todos, and meta through this API so subscribers see a
|
|
302
|
-
* deterministic change stream. The underlying Context arrays are still
|
|
303
|
-
* exposed for read compatibility and legacy tests.
|
|
157
|
+
* Agent runtime errors — thrown by Agent.run when a non-WrongStackError
|
|
158
|
+
* escapes the inner loop, so callers always see a structured error.
|
|
304
159
|
*/
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
todos: readonly TodoItem[];
|
|
314
|
-
} | {
|
|
315
|
-
kind: 'meta_set';
|
|
316
|
-
key: string;
|
|
317
|
-
value: unknown;
|
|
318
|
-
} | {
|
|
319
|
-
kind: 'meta_deleted';
|
|
320
|
-
key: string;
|
|
321
|
-
} | {
|
|
322
|
-
kind: 'meta_cleared';
|
|
323
|
-
};
|
|
324
|
-
type StateChangeHandler = (change: StateChange, state: ConversationState) => void;
|
|
325
|
-
interface ReadonlyConversationState {
|
|
326
|
-
readonly messages: readonly Message[];
|
|
327
|
-
readonly todos: readonly TodoItem[];
|
|
328
|
-
readonly meta: Readonly<Record<string, unknown>>;
|
|
329
|
-
}
|
|
330
|
-
declare class ConversationState {
|
|
331
|
-
private readonly ctx;
|
|
332
|
-
private readonly listeners;
|
|
333
|
-
constructor(ctx: Context);
|
|
334
|
-
get messages(): readonly Message[];
|
|
335
|
-
get todos(): readonly TodoItem[];
|
|
336
|
-
get meta(): Readonly<Record<string, unknown>>;
|
|
337
|
-
/**
|
|
338
|
-
* Cheap immutable snapshot. Useful for tests and for compaction passes
|
|
339
|
-
* that need a stable view across an async boundary.
|
|
340
|
-
*/
|
|
341
|
-
snapshot(): ReadonlyConversationState;
|
|
342
|
-
appendMessage(message: Message): void;
|
|
343
|
-
replaceMessages(messages: Message[]): void;
|
|
344
|
-
replaceTodos(todos: TodoItem[]): void;
|
|
345
|
-
setMeta(key: string, value: unknown): void;
|
|
346
|
-
deleteMeta(key: string): void;
|
|
347
|
-
clearMeta(): void;
|
|
348
|
-
/**
|
|
349
|
-
* Subscribe to mutations that go through this wrapper. Direct mutations of
|
|
350
|
-
* the compatibility arrays are intentionally not observed.
|
|
351
|
-
*/
|
|
352
|
-
onChange(listener: StateChangeHandler): () => void;
|
|
353
|
-
private emit;
|
|
160
|
+
declare class AgentError extends WrongStackError {
|
|
161
|
+
constructor(opts: {
|
|
162
|
+
message: string;
|
|
163
|
+
code: Extract<ErrorCode, 'AGENT_ITERATION_LIMIT' | 'AGENT_CONTEXT_OVERFLOW' | 'AGENT_ABORTED' | 'AGENT_RUN_FAILED'>;
|
|
164
|
+
recoverable?: boolean;
|
|
165
|
+
context?: Record<string, unknown>;
|
|
166
|
+
cause?: unknown;
|
|
167
|
+
});
|
|
354
168
|
}
|
|
355
169
|
/**
|
|
356
|
-
*
|
|
170
|
+
* Wrap an arbitrary thrown value into a `WrongStackError` so the caller
|
|
171
|
+
* always gets a structured error. Pass-throughs WrongStackError instances
|
|
172
|
+
* unchanged; raw `Error`s and primitives get an `AGENT_RUN_FAILED` wrapper
|
|
173
|
+
* with the original preserved as `cause`.
|
|
357
174
|
*/
|
|
358
|
-
declare function
|
|
359
|
-
|
|
360
|
-
interface TodoItem {
|
|
361
|
-
id: string;
|
|
362
|
-
content: string;
|
|
363
|
-
status: 'pending' | 'in_progress' | 'completed';
|
|
364
|
-
activeForm?: string;
|
|
365
|
-
}
|
|
366
|
-
interface RunOptions {
|
|
367
|
-
signal?: AbortSignal;
|
|
368
|
-
model?: string;
|
|
369
|
-
executionStrategy?: 'parallel' | 'sequential' | 'smart';
|
|
370
|
-
maxIterations?: number;
|
|
371
|
-
}
|
|
372
|
-
interface ContextInit {
|
|
373
|
-
systemPrompt: TextBlock[];
|
|
374
|
-
provider: Provider;
|
|
375
|
-
session: SessionWriter;
|
|
376
|
-
signal: AbortSignal;
|
|
377
|
-
tokenCounter: TokenCounter;
|
|
378
|
-
cwd: string;
|
|
379
|
-
projectRoot: string;
|
|
380
|
-
model: string;
|
|
381
|
-
tools?: Tool[];
|
|
382
|
-
}
|
|
175
|
+
declare function toWrongStackError(err: unknown, code?: Extract<ErrorCode, 'AGENT_RUN_FAILED' | 'AGENT_ABORTED' | 'UNKNOWN'>): WrongStackError;
|
|
383
176
|
/**
|
|
384
|
-
*
|
|
385
|
-
* shape is exposed by the `RunEnv` interface (every field below the
|
|
386
|
-
* conversation state) and its mutable shape by `ConversationState` (the
|
|
387
|
-
* `state` accessor). New code should declare the narrower type at its
|
|
388
|
-
* parameter — pass `ctx` for it. Existing tools that accept `Context`
|
|
389
|
-
* still work because `Context` structurally satisfies both.
|
|
177
|
+
* Session storage errors.
|
|
390
178
|
*/
|
|
391
|
-
declare class
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
tokenCounter: TokenCounter;
|
|
401
|
-
cwd: string;
|
|
402
|
-
projectRoot: string;
|
|
403
|
-
model: string;
|
|
404
|
-
tools: Tool[];
|
|
405
|
-
meta: Record<string, unknown>;
|
|
406
|
-
constructor(init: ContextInit);
|
|
407
|
-
/**
|
|
408
|
-
* Observable wrapper over the mutable conversation state. Lazy so
|
|
409
|
-
* subsystems that don't subscribe pay nothing. Mutations made directly
|
|
410
|
-
* on `ctx.messages` / `ctx.todos` are still visible through this
|
|
411
|
-
* wrapper's read API (it holds a reference, not a copy) but only
|
|
412
|
-
* mutations that go through `state.appendMessage()` etc. fire
|
|
413
|
-
* `onChange`. New code should prefer the wrapper API.
|
|
414
|
-
*/
|
|
415
|
-
private _state;
|
|
416
|
-
get state(): ConversationState;
|
|
417
|
-
/**
|
|
418
|
-
* Register a teardown hook tied to the current run's abort signal. The
|
|
419
|
-
* hook fires when the run aborts OR ends normally — Agent.run wires
|
|
420
|
-
* this through a RunController. When no run is active the hook fires
|
|
421
|
-
* immediately so callers don't leak resources.
|
|
422
|
-
*
|
|
423
|
-
* **Scope:** these hooks fire on the **whole agent run's** abort, not on
|
|
424
|
-
* an individual tool call. For per-tool teardown of resources owned by
|
|
425
|
-
* the tool author (child processes, handles), prefer `Tool.cleanup` —
|
|
426
|
-
* see its JSDoc for the full rule.
|
|
427
|
-
*/
|
|
428
|
-
private abortHooks;
|
|
429
|
-
registerAbortHook(fn: () => void | Promise<void>): () => void;
|
|
430
|
-
drainAbortHooks(): Promise<void>;
|
|
431
|
-
recordRead(absPath: string, mtimeMs: number): void;
|
|
432
|
-
hasRead(absPath: string): boolean;
|
|
433
|
-
lastReadMtime(absPath: string): number | undefined;
|
|
434
|
-
usage(): Usage;
|
|
179
|
+
declare class SessionError extends WrongStackError {
|
|
180
|
+
readonly sessionId?: string;
|
|
181
|
+
constructor(opts: {
|
|
182
|
+
message: string;
|
|
183
|
+
code: Extract<ErrorCode, 'SESSION_NOT_FOUND' | 'SESSION_CORRUPTED' | 'SESSION_WRITE_FAILED'>;
|
|
184
|
+
sessionId?: string;
|
|
185
|
+
context?: Record<string, unknown>;
|
|
186
|
+
cause?: unknown;
|
|
187
|
+
});
|
|
435
188
|
}
|
|
189
|
+
declare function isWrongStackError(err: unknown): err is WrongStackError;
|
|
190
|
+
declare function isToolError(err: unknown): err is ToolError;
|
|
191
|
+
declare function isConfigError(err: unknown): err is ConfigError;
|
|
192
|
+
declare function isPluginError(err: unknown): err is PluginError;
|
|
193
|
+
declare function isSessionError(err: unknown): err is SessionError;
|
|
194
|
+
declare function isAgentError(err: unknown): err is AgentError;
|
|
436
195
|
|
|
437
196
|
type Permission = 'auto' | 'confirm' | 'deny';
|
|
438
197
|
interface JSONSchema {
|
|
@@ -569,120 +328,30 @@ interface ToolCallContext {
|
|
|
569
328
|
}
|
|
570
329
|
|
|
571
330
|
/**
|
|
572
|
-
*
|
|
331
|
+
* Token usage for a single provider call, normalized across providers.
|
|
573
332
|
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
code: ErrorCode;
|
|
591
|
-
subsystem: ErrorSubsystem;
|
|
592
|
-
severity?: ErrorSeverity;
|
|
593
|
-
recoverable?: boolean;
|
|
594
|
-
context?: Record<string, unknown>;
|
|
595
|
-
cause?: unknown;
|
|
596
|
-
});
|
|
597
|
-
/**
|
|
598
|
-
* Render a one-line user-facing description.
|
|
599
|
-
* Subclasses should override for domain-specific formatting.
|
|
600
|
-
*/
|
|
601
|
-
describe(): string;
|
|
602
|
-
}
|
|
603
|
-
/**
|
|
604
|
-
* Tool execution errors — thrown by ToolExecutor and individual tools.
|
|
333
|
+
* Disjoint semantics: the four fields never overlap. `input` is the count
|
|
334
|
+
* of FRESH input tokens (billed at the full input rate); `cacheRead` and
|
|
335
|
+
* `cacheWrite` are separate cached subsets each priced at their own rate.
|
|
336
|
+
* The total context the model loaded for this turn is
|
|
337
|
+
* `input + (cacheRead ?? 0) + (cacheWrite ?? 0)`.
|
|
338
|
+
*
|
|
339
|
+
* Provider quirks normalized at the adapter layer:
|
|
340
|
+
* - Anthropic: returns `input_tokens` already disjoint from cache fields.
|
|
341
|
+
* - OpenAI / OpenAI-compatible: `prompt_tokens` is the TOTAL including
|
|
342
|
+
* cached portion; the adapter subtracts `cached_tokens` to stay disjoint.
|
|
343
|
+
* - Google: `promptTokenCount` likewise includes cache; adapter subtracts
|
|
344
|
+
* `cachedContentTokenCount`.
|
|
345
|
+
*
|
|
346
|
+
* Cost math and the context-fullness chip both depend on the disjoint
|
|
347
|
+
* invariant — a TOTAL `input` plus a separate `cacheRead` count would bill
|
|
348
|
+
* cached tokens twice and skew cache-hit-ratio reporting.
|
|
605
349
|
*/
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
toolName: string;
|
|
612
|
-
recoverable?: boolean;
|
|
613
|
-
context?: Record<string, unknown>;
|
|
614
|
-
cause?: unknown;
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
/**
|
|
618
|
-
* Config loading / validation errors.
|
|
619
|
-
*/
|
|
620
|
-
declare class ConfigError extends WrongStackError {
|
|
621
|
-
constructor(opts: {
|
|
622
|
-
message: string;
|
|
623
|
-
code: Extract<ErrorCode, 'CONFIG_INVALID' | 'CONFIG_NOT_FOUND' | 'CONFIG_PARSE_FAILED' | 'CONFIG_MIGRATION_NEEDED'>;
|
|
624
|
-
context?: Record<string, unknown>;
|
|
625
|
-
cause?: unknown;
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Plugin loading / lifecycle errors.
|
|
630
|
-
*/
|
|
631
|
-
declare class PluginError extends WrongStackError {
|
|
632
|
-
readonly pluginName: string;
|
|
633
|
-
constructor(opts: {
|
|
634
|
-
message: string;
|
|
635
|
-
code: Extract<ErrorCode, 'PLUGIN_LOAD_FAILED' | 'PLUGIN_API_MISMATCH' | 'PLUGIN_MISSING_DEPENDENCY'>;
|
|
636
|
-
pluginName: string;
|
|
637
|
-
context?: Record<string, unknown>;
|
|
638
|
-
cause?: unknown;
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
/**
|
|
642
|
-
* Agent runtime errors — thrown by Agent.run when a non-WrongStackError
|
|
643
|
-
* escapes the inner loop, so callers always see a structured error.
|
|
644
|
-
*/
|
|
645
|
-
declare class AgentError extends WrongStackError {
|
|
646
|
-
constructor(opts: {
|
|
647
|
-
message: string;
|
|
648
|
-
code: Extract<ErrorCode, 'AGENT_ITERATION_LIMIT' | 'AGENT_CONTEXT_OVERFLOW' | 'AGENT_ABORTED' | 'AGENT_RUN_FAILED'>;
|
|
649
|
-
recoverable?: boolean;
|
|
650
|
-
context?: Record<string, unknown>;
|
|
651
|
-
cause?: unknown;
|
|
652
|
-
});
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Wrap an arbitrary thrown value into a `WrongStackError` so the caller
|
|
656
|
-
* always gets a structured error. Pass-throughs WrongStackError instances
|
|
657
|
-
* unchanged; raw `Error`s and primitives get an `AGENT_RUN_FAILED` wrapper
|
|
658
|
-
* with the original preserved as `cause`.
|
|
659
|
-
*/
|
|
660
|
-
declare function toWrongStackError(err: unknown, code?: Extract<ErrorCode, 'AGENT_RUN_FAILED' | 'AGENT_ABORTED' | 'UNKNOWN'>): WrongStackError;
|
|
661
|
-
/**
|
|
662
|
-
* Session storage errors.
|
|
663
|
-
*/
|
|
664
|
-
declare class SessionError extends WrongStackError {
|
|
665
|
-
readonly sessionId?: string;
|
|
666
|
-
constructor(opts: {
|
|
667
|
-
message: string;
|
|
668
|
-
code: Extract<ErrorCode, 'SESSION_NOT_FOUND' | 'SESSION_CORRUPTED' | 'SESSION_WRITE_FAILED'>;
|
|
669
|
-
sessionId?: string;
|
|
670
|
-
context?: Record<string, unknown>;
|
|
671
|
-
cause?: unknown;
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
declare function isWrongStackError(err: unknown): err is WrongStackError;
|
|
675
|
-
declare function isToolError(err: unknown): err is ToolError;
|
|
676
|
-
declare function isConfigError(err: unknown): err is ConfigError;
|
|
677
|
-
declare function isPluginError(err: unknown): err is PluginError;
|
|
678
|
-
declare function isSessionError(err: unknown): err is SessionError;
|
|
679
|
-
declare function isAgentError(err: unknown): err is AgentError;
|
|
680
|
-
|
|
681
|
-
interface Usage {
|
|
682
|
-
input: number;
|
|
683
|
-
output: number;
|
|
684
|
-
cacheRead?: number;
|
|
685
|
-
cacheWrite?: number;
|
|
350
|
+
interface Usage {
|
|
351
|
+
input: number;
|
|
352
|
+
output: number;
|
|
353
|
+
cacheRead?: number;
|
|
354
|
+
cacheWrite?: number;
|
|
686
355
|
}
|
|
687
356
|
interface Capabilities {
|
|
688
357
|
tools: boolean;
|
|
@@ -721,7 +390,7 @@ type StreamEvent = {
|
|
|
721
390
|
model: string;
|
|
722
391
|
} | {
|
|
723
392
|
type: 'content_block_start';
|
|
724
|
-
kind: 'text' | 'tool_use';
|
|
393
|
+
kind: 'text' | 'tool_use' | 'thinking';
|
|
725
394
|
id?: string;
|
|
726
395
|
name?: string;
|
|
727
396
|
} | {
|
|
@@ -743,6 +412,17 @@ type StreamEvent = {
|
|
|
743
412
|
id: string;
|
|
744
413
|
input: unknown;
|
|
745
414
|
providerMeta?: Record<string, unknown>;
|
|
415
|
+
} | {
|
|
416
|
+
type: 'thinking_start';
|
|
417
|
+
providerMeta?: Record<string, unknown>;
|
|
418
|
+
} | {
|
|
419
|
+
type: 'thinking_delta';
|
|
420
|
+
text: string;
|
|
421
|
+
} | {
|
|
422
|
+
type: 'thinking_signature';
|
|
423
|
+
signature: string;
|
|
424
|
+
} | {
|
|
425
|
+
type: 'thinking_stop';
|
|
746
426
|
} | {
|
|
747
427
|
type: 'message_stop';
|
|
748
428
|
stopReason: StopReason;
|
|
@@ -807,4 +487,384 @@ declare class ProviderError extends WrongStackError {
|
|
|
807
487
|
describe(): string;
|
|
808
488
|
}
|
|
809
489
|
|
|
810
|
-
|
|
490
|
+
interface SessionMetadata {
|
|
491
|
+
id: string;
|
|
492
|
+
title?: string;
|
|
493
|
+
model?: string;
|
|
494
|
+
provider?: string;
|
|
495
|
+
startedAt: string;
|
|
496
|
+
endedAt?: string;
|
|
497
|
+
}
|
|
498
|
+
type SessionEvent = {
|
|
499
|
+
type: 'session_start';
|
|
500
|
+
ts: string;
|
|
501
|
+
id: string;
|
|
502
|
+
model: string;
|
|
503
|
+
provider: string;
|
|
504
|
+
} | {
|
|
505
|
+
type: 'session_resumed';
|
|
506
|
+
ts: string;
|
|
507
|
+
id: string;
|
|
508
|
+
model: string;
|
|
509
|
+
provider: string;
|
|
510
|
+
} | {
|
|
511
|
+
type: 'user_input';
|
|
512
|
+
ts: string;
|
|
513
|
+
content: string | ContentBlock[];
|
|
514
|
+
} | {
|
|
515
|
+
type: 'llm_request';
|
|
516
|
+
ts: string;
|
|
517
|
+
model: string;
|
|
518
|
+
messageCount: number;
|
|
519
|
+
} | {
|
|
520
|
+
type: 'llm_response';
|
|
521
|
+
ts: string;
|
|
522
|
+
content: ContentBlock[];
|
|
523
|
+
stopReason: string;
|
|
524
|
+
usage: Usage;
|
|
525
|
+
} | {
|
|
526
|
+
type: 'tool_use';
|
|
527
|
+
ts: string;
|
|
528
|
+
name: string;
|
|
529
|
+
id: string;
|
|
530
|
+
input: unknown;
|
|
531
|
+
} | {
|
|
532
|
+
type: 'tool_result';
|
|
533
|
+
ts: string;
|
|
534
|
+
id: string;
|
|
535
|
+
content: unknown;
|
|
536
|
+
isError: boolean;
|
|
537
|
+
} | {
|
|
538
|
+
type: 'compaction';
|
|
539
|
+
ts: string;
|
|
540
|
+
before: number;
|
|
541
|
+
after: number;
|
|
542
|
+
} | {
|
|
543
|
+
type: 'error';
|
|
544
|
+
ts: string;
|
|
545
|
+
message: string;
|
|
546
|
+
phase: string;
|
|
547
|
+
} | {
|
|
548
|
+
type: 'session_end';
|
|
549
|
+
ts: string;
|
|
550
|
+
usage: Usage;
|
|
551
|
+
} | {
|
|
552
|
+
type: 'mode_changed';
|
|
553
|
+
ts: string;
|
|
554
|
+
from: string;
|
|
555
|
+
to: string;
|
|
556
|
+
} | {
|
|
557
|
+
type: 'task_created';
|
|
558
|
+
ts: string;
|
|
559
|
+
taskId: string;
|
|
560
|
+
title: string;
|
|
561
|
+
} | {
|
|
562
|
+
type: 'task_updated';
|
|
563
|
+
ts: string;
|
|
564
|
+
taskId: string;
|
|
565
|
+
status: string;
|
|
566
|
+
} | {
|
|
567
|
+
type: 'task_completed';
|
|
568
|
+
ts: string;
|
|
569
|
+
taskId: string;
|
|
570
|
+
title: string;
|
|
571
|
+
} | {
|
|
572
|
+
type: 'task_failed';
|
|
573
|
+
ts: string;
|
|
574
|
+
taskId: string;
|
|
575
|
+
title: string;
|
|
576
|
+
error: string;
|
|
577
|
+
} | {
|
|
578
|
+
type: 'agent_spawned';
|
|
579
|
+
ts: string;
|
|
580
|
+
agentId: string;
|
|
581
|
+
role: string;
|
|
582
|
+
} | {
|
|
583
|
+
type: 'agent_stopped';
|
|
584
|
+
ts: string;
|
|
585
|
+
agentId: string;
|
|
586
|
+
} | {
|
|
587
|
+
type: 'agent_error';
|
|
588
|
+
ts: string;
|
|
589
|
+
agentId: string;
|
|
590
|
+
error: string;
|
|
591
|
+
} | {
|
|
592
|
+
type: 'spec_parsed';
|
|
593
|
+
ts: string;
|
|
594
|
+
specId: string;
|
|
595
|
+
title: string;
|
|
596
|
+
completeness: number;
|
|
597
|
+
} | {
|
|
598
|
+
type: 'spec_analyzed';
|
|
599
|
+
ts: string;
|
|
600
|
+
specId: string;
|
|
601
|
+
gaps: string[];
|
|
602
|
+
} | {
|
|
603
|
+
type: 'skill_activated';
|
|
604
|
+
ts: string;
|
|
605
|
+
skillName: string;
|
|
606
|
+
} | {
|
|
607
|
+
type: 'skill_deactivated';
|
|
608
|
+
ts: string;
|
|
609
|
+
skillName: string;
|
|
610
|
+
} | {
|
|
611
|
+
type: 'tool_call_start';
|
|
612
|
+
ts: string;
|
|
613
|
+
name: string;
|
|
614
|
+
id: string;
|
|
615
|
+
input: unknown;
|
|
616
|
+
} | {
|
|
617
|
+
type: 'tool_call_end';
|
|
618
|
+
ts: string;
|
|
619
|
+
name: string;
|
|
620
|
+
id: string;
|
|
621
|
+
durationMs: number;
|
|
622
|
+
outputSize: number;
|
|
623
|
+
} | {
|
|
624
|
+
type: 'message_truncated';
|
|
625
|
+
ts: string;
|
|
626
|
+
before: number;
|
|
627
|
+
after: number;
|
|
628
|
+
};
|
|
629
|
+
interface SessionSummary {
|
|
630
|
+
id: string;
|
|
631
|
+
title: string;
|
|
632
|
+
startedAt: string;
|
|
633
|
+
model: string;
|
|
634
|
+
provider: string;
|
|
635
|
+
tokenTotal: number;
|
|
636
|
+
}
|
|
637
|
+
interface SessionData {
|
|
638
|
+
metadata: SessionMetadata;
|
|
639
|
+
events: SessionEvent[];
|
|
640
|
+
messages: Message[];
|
|
641
|
+
usage: Usage;
|
|
642
|
+
}
|
|
643
|
+
interface ResumedSession {
|
|
644
|
+
writer: SessionWriter;
|
|
645
|
+
data: SessionData;
|
|
646
|
+
}
|
|
647
|
+
interface SessionStore {
|
|
648
|
+
create(meta: Omit<SessionMetadata, 'startedAt'>): Promise<SessionWriter>;
|
|
649
|
+
load(id: string): Promise<SessionData>;
|
|
650
|
+
/**
|
|
651
|
+
* Open an existing session for append, returning both a writer that
|
|
652
|
+
* continues writing to the same JSONL file and the replayed state
|
|
653
|
+
* (messages + usage) so the caller can hydrate a Context. A
|
|
654
|
+
* `session_resumed` marker is appended for audit.
|
|
655
|
+
*/
|
|
656
|
+
resume(id: string): Promise<ResumedSession>;
|
|
657
|
+
list(limit?: number): Promise<SessionSummary[]>;
|
|
658
|
+
delete(id: string): Promise<void>;
|
|
659
|
+
}
|
|
660
|
+
interface SessionWriter {
|
|
661
|
+
readonly id: string;
|
|
662
|
+
append(event: SessionEvent): Promise<void>;
|
|
663
|
+
close(): Promise<void>;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
interface CacheStats {
|
|
667
|
+
/** Tokens served from cache (cheaper). */
|
|
668
|
+
readTokens: number;
|
|
669
|
+
/** Tokens written into the cache (more expensive than input on first hit). */
|
|
670
|
+
writeTokens: number;
|
|
671
|
+
/** Hit ratio: cacheRead / (cacheRead + input). 0 when nothing cached. */
|
|
672
|
+
hitRatio: number;
|
|
673
|
+
}
|
|
674
|
+
interface TokenCounter {
|
|
675
|
+
account(usage: Usage, model?: string): void;
|
|
676
|
+
total(): Usage;
|
|
677
|
+
estimateCost(): {
|
|
678
|
+
input: number;
|
|
679
|
+
output: number;
|
|
680
|
+
total: number;
|
|
681
|
+
currency: 'USD';
|
|
682
|
+
};
|
|
683
|
+
cacheStats(): CacheStats;
|
|
684
|
+
reset(): void;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Observable wrapper for mutable conversation state. Production code should
|
|
689
|
+
* mutate messages, todos, and meta through this API so subscribers see a
|
|
690
|
+
* deterministic change stream. The underlying Context arrays are still
|
|
691
|
+
* exposed for read compatibility and legacy tests.
|
|
692
|
+
*
|
|
693
|
+
* L1-A invariant: direct mutations of `ctx.messages` / `ctx.todos` bypass
|
|
694
|
+
* the observer layer. Prefer `ctx.state.appendMessage()` etc. to keep
|
|
695
|
+
* subscribers in sync. The compatibility arrays exist so existing code
|
|
696
|
+
* that reads `ctx.messages` directly still works — they are NOT safe for
|
|
697
|
+
* external writes.
|
|
698
|
+
*/
|
|
699
|
+
type StateChange = {
|
|
700
|
+
kind: 'message_appended';
|
|
701
|
+
message: Message;
|
|
702
|
+
} | {
|
|
703
|
+
kind: 'messages_replaced';
|
|
704
|
+
messages: readonly Message[];
|
|
705
|
+
} | {
|
|
706
|
+
kind: 'todos_replaced';
|
|
707
|
+
todos: readonly TodoItem[];
|
|
708
|
+
} | {
|
|
709
|
+
kind: 'meta_set';
|
|
710
|
+
key: string;
|
|
711
|
+
value: unknown;
|
|
712
|
+
} | {
|
|
713
|
+
kind: 'meta_deleted';
|
|
714
|
+
key: string;
|
|
715
|
+
} | {
|
|
716
|
+
kind: 'meta_cleared';
|
|
717
|
+
};
|
|
718
|
+
type StateChangeHandler = (change: StateChange, state: ConversationState) => void;
|
|
719
|
+
interface ReadonlyConversationState {
|
|
720
|
+
readonly messages: readonly Message[];
|
|
721
|
+
readonly todos: readonly TodoItem[];
|
|
722
|
+
readonly meta: Readonly<Record<string, unknown>>;
|
|
723
|
+
}
|
|
724
|
+
declare class ConversationState {
|
|
725
|
+
private readonly ctx;
|
|
726
|
+
private readonly listeners;
|
|
727
|
+
constructor(ctx: Context);
|
|
728
|
+
get messages(): readonly Message[];
|
|
729
|
+
get todos(): readonly TodoItem[];
|
|
730
|
+
get meta(): Readonly<Record<string, unknown>>;
|
|
731
|
+
/**
|
|
732
|
+
* Cheap immutable snapshot. Useful for tests and for compaction passes
|
|
733
|
+
* that need a stable view across an async boundary.
|
|
734
|
+
*/
|
|
735
|
+
snapshot(): ReadonlyConversationState;
|
|
736
|
+
appendMessage(message: Message): void;
|
|
737
|
+
replaceMessages(messages: Message[]): void;
|
|
738
|
+
replaceTodos(todos: TodoItem[]): void;
|
|
739
|
+
setMeta(key: string, value: unknown): void;
|
|
740
|
+
deleteMeta(key: string): void;
|
|
741
|
+
clearMeta(): void;
|
|
742
|
+
/**
|
|
743
|
+
* Subscribe to mutations that go through this wrapper. Direct mutations of
|
|
744
|
+
* the compatibility arrays are intentionally not observed.
|
|
745
|
+
*/
|
|
746
|
+
onChange(listener: StateChangeHandler): () => void;
|
|
747
|
+
private emit;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Convenience constructor. The wrapper holds a reference, not a copy.
|
|
751
|
+
*/
|
|
752
|
+
declare function wrapAsState(ctx: Context): ConversationState;
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Immutable run environment — the set-once dependencies for an agent run.
|
|
756
|
+
*
|
|
757
|
+
* `Context` today doubles as both a DI bag (provider, session, tokenCounter,
|
|
758
|
+
* cwd, …) and a mutable state container (messages, todos, meta). That makes
|
|
759
|
+
* it hard to test (every test reconstructs the full bag) and easy to abuse
|
|
760
|
+
* (any tool can swap the provider mid-run).
|
|
761
|
+
*
|
|
762
|
+
* `RunEnv` is the immutable half: a read-only projection that subsystems
|
|
763
|
+
* can hold instead of the whole `Context`. It's a view, not a copy — pulling
|
|
764
|
+
* a `RunEnv` from a `Context` is O(1) and reflects the same underlying
|
|
765
|
+
* references. The opposite direction (set things on Context) still works,
|
|
766
|
+
* and `extractRunEnv` rebuilds the view if you need a snapshot.
|
|
767
|
+
*
|
|
768
|
+
* Migration path: new APIs accept `RunEnv` instead of `Context` when they
|
|
769
|
+
* only need read access. Existing APIs continue to accept `Context` until
|
|
770
|
+
* a full split is scheduled.
|
|
771
|
+
*/
|
|
772
|
+
interface RunEnv {
|
|
773
|
+
readonly provider: Provider;
|
|
774
|
+
readonly session: SessionWriter;
|
|
775
|
+
readonly signal: AbortSignal;
|
|
776
|
+
readonly tokenCounter: TokenCounter;
|
|
777
|
+
readonly cwd: string;
|
|
778
|
+
readonly projectRoot: string;
|
|
779
|
+
readonly model: string;
|
|
780
|
+
readonly systemPrompt: readonly TextBlock[];
|
|
781
|
+
readonly tools: readonly Tool[];
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Build a `RunEnv` view from a Context. The returned object is a shallow
|
|
785
|
+
* frozen view — mutations to `Context` are visible (it's the same
|
|
786
|
+
* references), but the view itself can't be mutated.
|
|
787
|
+
*
|
|
788
|
+
* Use this in subsystems that want to declare "I only need read access to
|
|
789
|
+
* the env" without rewriting their signature to accept the full Context.
|
|
790
|
+
*/
|
|
791
|
+
declare function extractRunEnv(ctx: Context): RunEnv;
|
|
792
|
+
|
|
793
|
+
interface TodoItem {
|
|
794
|
+
id: string;
|
|
795
|
+
content: string;
|
|
796
|
+
status: 'pending' | 'in_progress' | 'completed';
|
|
797
|
+
activeForm?: string;
|
|
798
|
+
}
|
|
799
|
+
interface RunOptions {
|
|
800
|
+
signal?: AbortSignal;
|
|
801
|
+
model?: string;
|
|
802
|
+
executionStrategy?: 'parallel' | 'sequential' | 'smart';
|
|
803
|
+
maxIterations?: number;
|
|
804
|
+
}
|
|
805
|
+
interface ContextInit {
|
|
806
|
+
systemPrompt: TextBlock[];
|
|
807
|
+
provider: Provider;
|
|
808
|
+
session: SessionWriter;
|
|
809
|
+
signal: AbortSignal;
|
|
810
|
+
tokenCounter: TokenCounter;
|
|
811
|
+
cwd: string;
|
|
812
|
+
projectRoot: string;
|
|
813
|
+
model: string;
|
|
814
|
+
tools?: Tool[];
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* L1-A: `Context` is the live agent-run object. Its read-only environment
|
|
818
|
+
* shape is exposed by the `RunEnv` interface (every field below the
|
|
819
|
+
* conversation state) and its mutable shape by `ConversationState` (the
|
|
820
|
+
* `state` accessor). New code should declare the narrower type at its
|
|
821
|
+
* parameter — pass `ctx` for it. Existing tools that accept `Context`
|
|
822
|
+
* still work because `Context` structurally satisfies both.
|
|
823
|
+
*/
|
|
824
|
+
declare class Context implements RunEnv {
|
|
825
|
+
messages: Message[];
|
|
826
|
+
todos: TodoItem[];
|
|
827
|
+
readFiles: Set<string>;
|
|
828
|
+
fileMtimes: Map<string, number>;
|
|
829
|
+
systemPrompt: TextBlock[];
|
|
830
|
+
provider: Provider;
|
|
831
|
+
session: SessionWriter;
|
|
832
|
+
signal: AbortSignal;
|
|
833
|
+
tokenCounter: TokenCounter;
|
|
834
|
+
cwd: string;
|
|
835
|
+
projectRoot: string;
|
|
836
|
+
model: string;
|
|
837
|
+
tools: Tool[];
|
|
838
|
+
meta: Record<string, unknown>;
|
|
839
|
+
constructor(init: ContextInit);
|
|
840
|
+
/**
|
|
841
|
+
* Observable wrapper over the mutable conversation state. Lazy so
|
|
842
|
+
* subsystems that don't subscribe pay nothing. Mutations made directly
|
|
843
|
+
* on `ctx.messages` / `ctx.todos` are still visible through this
|
|
844
|
+
* wrapper's read API (it holds a reference, not a copy) but only
|
|
845
|
+
* mutations that go through `state.appendMessage()` etc. fire
|
|
846
|
+
* `onChange`. New code should prefer the wrapper API.
|
|
847
|
+
*/
|
|
848
|
+
private _state;
|
|
849
|
+
get state(): ConversationState;
|
|
850
|
+
/**
|
|
851
|
+
* Register a teardown hook tied to the current run's abort signal. The
|
|
852
|
+
* hook fires when the run aborts OR ends normally — Agent.run wires
|
|
853
|
+
* this through a RunController. When no run is active the hook fires
|
|
854
|
+
* immediately so callers don't leak resources.
|
|
855
|
+
*
|
|
856
|
+
* **Scope:** these hooks fire on the **whole agent run's** abort, not on
|
|
857
|
+
* an individual tool call. For per-tool teardown of resources owned by
|
|
858
|
+
* the tool author (child processes, handles), prefer `Tool.cleanup` —
|
|
859
|
+
* see its JSDoc for the full rule.
|
|
860
|
+
*/
|
|
861
|
+
private abortHooks;
|
|
862
|
+
registerAbortHook(fn: () => void | Promise<void>): () => void;
|
|
863
|
+
drainAbortHooks(): Promise<void>;
|
|
864
|
+
recordRead(absPath: string, mtimeMs: number): void;
|
|
865
|
+
hasRead(absPath: string): boolean;
|
|
866
|
+
lastReadMtime(absPath: string): number | undefined;
|
|
867
|
+
usage(): Usage;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
export { toWrongStackError as $, AgentError as A, type ToolStreamEvent as B, type Capabilities as C, type ToolUseBlock as D, type ErrorCode as E, asBlocks as F, asText as G, isAgentError as H, type ImageBlock as I, type JSONSchema as J, isConfigError as K, isImageBlock as L, type Message as M, isPluginError as N, isSessionError as O, type Permission as P, isTextBlock as Q, type Request as R, type SessionData as S, type TextBlock as T, type Usage as U, isThinkingBlock as V, WrongStackError as W, isToolError as X, isToolResultBlock as Y, isToolUseBlock as Z, isWrongStackError as _, ConfigError as a, Context as a0, type TokenCounter as a1, type CacheStats as a2, type ContextInit as a3, ConversationState as a4, type ReadonlyConversationState as a5, type RunEnv as a6, type RunOptions as a7, type StateChange as a8, type StateChangeHandler as a9, type TodoItem as aa, extractRunEnv as ab, wrapAsState as ac, type ContentBlock as b, type ErrorSeverity as c, type ErrorSubsystem as d, type MessageRole as e, PluginError as f, type Provider as g, ProviderError as h, type ProviderErrorBody as i, type Response as j, type ResumedSession as k, SessionError as l, type SessionEvent as m, type SessionMetadata as n, type SessionStore as o, type SessionSummary as p, type SessionWriter as q, type StopReason as r, type StreamEvent as s, type ThinkingBlock as t, type Tool as u, type ToolCallContext as v, ToolError as w, type ToolFinalEvent as x, type ToolProgressEvent as y, type ToolResultBlock as z };
|