@aragon-agent/core 0.2.92 → 0.2.94
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/engine/accept-steering.js +1 -15
- package/dist/engine/agent-loop.js +1 -533
- package/dist/engine/agent.js +1 -538
- package/dist/engine/compaction.js +1 -268
- package/dist/engine/context-manager.js +1 -13
- package/dist/engine/message-manager.js +1 -39
- package/dist/engine/steering.js +1 -72
- package/dist/engine/tool-result-content.js +1 -39
- package/dist/engine/watchdog.js +1 -73
- package/dist/index.js +1 -99
- package/dist/llm/context-window.js +1 -46
- package/dist/llm/image-tokens.js +1 -11
- package/dist/llm/model-discovery.js +1 -18
- package/dist/llm/model-registry.js +1 -370
- package/dist/llm/output-limit-recovery.js +1 -263
- package/dist/llm/output-limits.js +1 -283
- package/dist/llm/provider.js +1 -171
- package/dist/llm/providers/anthropic.js +1 -514
- package/dist/llm/providers/google.js +1 -464
- package/dist/llm/providers/index.js +1 -205
- package/dist/llm/providers/openai.js +1 -520
- package/dist/llm/providers/tool-result-images.js +1 -52
- package/dist/llm/retry.js +1 -948
- package/dist/llm/sse-parser.js +1 -103
- package/dist/llm/stream-utils.js +1 -118
- package/dist/llm/types.js +1 -17
- package/dist/sandbox/console-capture.js +1 -56
- package/dist/sandbox/http-bridge.js +1 -143
- package/dist/sandbox/isolated-vm-sandbox.js +1 -226
- package/dist/sandbox/tool-bridge.js +1 -98
- package/dist/sandbox/types.js +1 -8
- package/dist/skills/constants.js +1 -179
- package/dist/skills/disclosure.js +1 -788
- package/dist/skills/frontmatter.js +1 -177
- package/dist/skills/index.js +1 -18
- package/dist/skills/skill-find-tool.js +1 -117
- package/dist/skills/skill-registry.js +1 -208
- package/dist/skills/skill-tool.js +1 -96
- package/dist/skills/tool-policy.js +1 -272
- package/dist/skills/types.js +1 -8
- package/dist/skills/validate.js +1 -264
- package/dist/tools/executor.js +1 -281
- package/dist/tools/helpers.js +1 -87
- package/dist/tools/registry.js +1 -119
- package/dist/tools/types.js +1 -7
- package/dist/tools/validator.js +1 -210
- package/dist/types.js +1 -7
- package/package.json +3 -2
package/dist/engine/agent.js
CHANGED
|
@@ -1,538 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent — the main class for the Agent engine.
|
|
3
|
-
*
|
|
4
|
-
* Wraps the agent loop, message management, steering/follow-up queues,
|
|
5
|
-
* tool registry/executor, and idle watchdog into a single cohesive API
|
|
6
|
-
* that provides a full agentic LLM execution engine.
|
|
7
|
-
*/
|
|
8
|
-
import { ToolRegistry } from '../tools/registry.js';
|
|
9
|
-
import { ToolExecutor } from '../tools/executor.js';
|
|
10
|
-
import { MessageManager } from './message-manager.js';
|
|
11
|
-
import { MessageQueueManager } from './steering.js';
|
|
12
|
-
import { IdleWatchdog } from './watchdog.js';
|
|
13
|
-
import { runAgentLoop } from './agent-loop.js';
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
// Default timeouts
|
|
16
|
-
// ---------------------------------------------------------------------------
|
|
17
|
-
const DEFAULT_LLM_CALL_TIMEOUT = 300_000;
|
|
18
|
-
const DEFAULT_IDLE_TIMEOUT = 60_000;
|
|
19
|
-
const DEFAULT_TOOL_TIMEOUT = 120_000;
|
|
20
|
-
const DEFAULT_CODE_TIMEOUT = 30_000;
|
|
21
|
-
function freshActivity() {
|
|
22
|
-
return {
|
|
23
|
-
lastEvent: 'none',
|
|
24
|
-
phase: 'awaiting_response',
|
|
25
|
-
toolName: undefined,
|
|
26
|
-
monotonicAt: performance.now(),
|
|
27
|
-
wallAt: Date.now(),
|
|
28
|
-
eventsSeen: 0,
|
|
29
|
-
toolsInFlight: 0,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
/** `message_update` and friends read better in a log as `message_update:text_delta`. */
|
|
33
|
-
function describeEvent(event) {
|
|
34
|
-
return event.type === 'message_update'
|
|
35
|
-
? `message_update:${event.streamEvent.type}`
|
|
36
|
-
: event.type;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Fold one event into the activity record. Mutates `a`; allocation-free because it
|
|
40
|
-
* runs for every streamed delta of every run, which is the only reason this is
|
|
41
|
-
* not written as a pure `(a, event) => a`.
|
|
42
|
-
*/
|
|
43
|
-
function recordActivity(a, event) {
|
|
44
|
-
a.lastEvent = describeEvent(event);
|
|
45
|
-
a.monotonicAt = performance.now();
|
|
46
|
-
a.wallAt = Date.now();
|
|
47
|
-
a.eventsSeen += 1;
|
|
48
|
-
switch (event.type) {
|
|
49
|
-
case 'agent_start':
|
|
50
|
-
a.toolsInFlight = 0;
|
|
51
|
-
a.phase = 'awaiting_response';
|
|
52
|
-
a.toolName = undefined;
|
|
53
|
-
return;
|
|
54
|
-
case 'turn_start':
|
|
55
|
-
a.phase = 'awaiting_response';
|
|
56
|
-
a.toolName = undefined;
|
|
57
|
-
return;
|
|
58
|
-
case 'turn_end':
|
|
59
|
-
a.phase = 'between_turns';
|
|
60
|
-
return;
|
|
61
|
-
case 'tool_execution_start':
|
|
62
|
-
case 'code_execution_start':
|
|
63
|
-
a.toolsInFlight += 1;
|
|
64
|
-
a.phase = 'tool_running';
|
|
65
|
-
a.toolName = event.type === 'tool_execution_start' ? event.toolName : 'code';
|
|
66
|
-
return;
|
|
67
|
-
case 'tool_execution_end':
|
|
68
|
-
case 'code_execution_end':
|
|
69
|
-
a.toolsInFlight = Math.max(0, a.toolsInFlight - 1);
|
|
70
|
-
if (a.toolsInFlight === 0) {
|
|
71
|
-
a.phase = 'awaiting_response';
|
|
72
|
-
a.toolName = undefined;
|
|
73
|
-
}
|
|
74
|
-
return;
|
|
75
|
-
case 'message_update':
|
|
76
|
-
break;
|
|
77
|
-
default:
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
switch (event.streamEvent.type) {
|
|
81
|
-
case 'thinking_start':
|
|
82
|
-
case 'thinking_delta':
|
|
83
|
-
a.phase = 'thinking';
|
|
84
|
-
return;
|
|
85
|
-
case 'text_delta':
|
|
86
|
-
a.phase = 'text';
|
|
87
|
-
return;
|
|
88
|
-
case 'tool_call_start':
|
|
89
|
-
a.phase = 'tool_input';
|
|
90
|
-
a.toolName = event.streamEvent.toolName;
|
|
91
|
-
return;
|
|
92
|
-
case 'tool_call_delta':
|
|
93
|
-
a.phase = 'tool_input';
|
|
94
|
-
return;
|
|
95
|
-
case 'retry_attempt':
|
|
96
|
-
case 'stream_restart':
|
|
97
|
-
// A new request is going out: the silence that follows is before ITS first byte.
|
|
98
|
-
a.phase = 'awaiting_response';
|
|
99
|
-
a.toolName = undefined;
|
|
100
|
-
return;
|
|
101
|
-
default:
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// ---------------------------------------------------------------------------
|
|
106
|
-
// Agent
|
|
107
|
-
// ---------------------------------------------------------------------------
|
|
108
|
-
export class Agent {
|
|
109
|
-
// --- Internal components ---
|
|
110
|
-
messageManager = new MessageManager();
|
|
111
|
-
messageQueueManager = new MessageQueueManager();
|
|
112
|
-
toolRegistry = new ToolRegistry();
|
|
113
|
-
toolExecutor;
|
|
114
|
-
watchdog;
|
|
115
|
-
listeners = new Set();
|
|
116
|
-
// --- Configuration (mutable via setters) ---
|
|
117
|
-
systemPrompt;
|
|
118
|
-
model;
|
|
119
|
-
thinkingLevel;
|
|
120
|
-
currentTools;
|
|
121
|
-
providerRegistry;
|
|
122
|
-
getApiKeyFn;
|
|
123
|
-
sandbox;
|
|
124
|
-
contextManager;
|
|
125
|
-
projectMessages;
|
|
126
|
-
/** Tiered reconnect: read once per run, so the host's config stays live (§4.4). */
|
|
127
|
-
resolveEscalation;
|
|
128
|
-
// --- LLM generation parameters ---
|
|
129
|
-
maxTokens;
|
|
130
|
-
// --- Timeouts ---
|
|
131
|
-
llmCallTimeout;
|
|
132
|
-
idleTimeout;
|
|
133
|
-
toolTimeout;
|
|
134
|
-
codeTimeout;
|
|
135
|
-
compactionHardTimeout;
|
|
136
|
-
// --- Runtime state ---
|
|
137
|
-
abortController = null;
|
|
138
|
-
/**
|
|
139
|
-
* Set by the idle watchdog immediately before it aborts, because the abort
|
|
140
|
-
* path produces no error event at all: without this bit an engine-interrupted
|
|
141
|
-
* run reaches every host indistinguishable from an answered one
|
|
142
|
-
* (`llm-network-error-dead-run` F).
|
|
143
|
-
*/
|
|
144
|
-
watchdogFired = false;
|
|
145
|
-
/** Where the run was when the watchdog fired; captured THEN, before the abort unwinds. */
|
|
146
|
-
stall = null;
|
|
147
|
-
activity = freshActivity();
|
|
148
|
-
/** Failure facts for the NEXT `agent_end`; cleared as they are emitted. */
|
|
149
|
-
endFailure = null;
|
|
150
|
-
running = false;
|
|
151
|
-
idlePromiseResolvers = [];
|
|
152
|
-
constructor(config) {
|
|
153
|
-
this.systemPrompt = config.systemPrompt;
|
|
154
|
-
this.model = { ...config.model };
|
|
155
|
-
this.thinkingLevel = config.thinkingLevel ?? 'off';
|
|
156
|
-
this.currentTools = [...config.tools];
|
|
157
|
-
this.providerRegistry = config.providerRegistry;
|
|
158
|
-
this.getApiKeyFn = config.getApiKey;
|
|
159
|
-
this.sandbox = config.sandbox;
|
|
160
|
-
this.contextManager = config.contextManager;
|
|
161
|
-
this.projectMessages = config.projectMessages;
|
|
162
|
-
this.resolveEscalation = config.resolveEscalation;
|
|
163
|
-
this.maxTokens = config.maxTokens;
|
|
164
|
-
// Timeouts
|
|
165
|
-
this.llmCallTimeout = config.timeouts?.llmCallTimeout ?? DEFAULT_LLM_CALL_TIMEOUT;
|
|
166
|
-
this.idleTimeout = config.timeouts?.idleTimeout ?? DEFAULT_IDLE_TIMEOUT;
|
|
167
|
-
this.toolTimeout = config.timeouts?.toolTimeout ?? DEFAULT_TOOL_TIMEOUT;
|
|
168
|
-
this.codeTimeout = config.timeouts?.codeTimeout ?? DEFAULT_CODE_TIMEOUT;
|
|
169
|
-
this.compactionHardTimeout = config.timeouts?.compactionHardTimeout;
|
|
170
|
-
// Register initial tools
|
|
171
|
-
this.syncToolRegistry(config.tools);
|
|
172
|
-
// Tool executor
|
|
173
|
-
this.toolExecutor = new ToolExecutor(this.toolRegistry, {
|
|
174
|
-
defaultTimeout: this.toolTimeout,
|
|
175
|
-
...(config.timeouts?.toolTimeoutOverrides
|
|
176
|
-
? { timeoutOverrides: config.timeouts.toolTimeoutOverrides }
|
|
177
|
-
: {}),
|
|
178
|
-
});
|
|
179
|
-
// Watchdog — on timeout, abort the agent
|
|
180
|
-
this.watchdog = new IdleWatchdog(this.idleTimeout, () => {
|
|
181
|
-
console.error('[Agent] idle watchdog fired — aborting');
|
|
182
|
-
// BEFORE the abort, so the fact survives the unwind this call starts.
|
|
183
|
-
this.watchdogFired = true;
|
|
184
|
-
// CAPTURED NOW, NOT IN `finally`: the abort makes the loop emit events of its
|
|
185
|
-
// own while it unwinds, and each of them would move `lastEvent` to something
|
|
186
|
-
// that is not where the silence began.
|
|
187
|
-
this.stall = this.describeStall();
|
|
188
|
-
this.abort();
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
/** The watchdog's own evidence: what was last seen, and for how long nothing was. */
|
|
192
|
-
describeStall() {
|
|
193
|
-
const a = this.activity;
|
|
194
|
-
return {
|
|
195
|
-
phase: a.phase,
|
|
196
|
-
lastEvent: a.lastEvent,
|
|
197
|
-
msSinceLastEvent: Math.round(performance.now() - a.monotonicAt),
|
|
198
|
-
wallMsSinceLastEvent: Date.now() - a.wallAt,
|
|
199
|
-
eventsSeen: a.eventsSeen,
|
|
200
|
-
idleTimeoutMs: this.idleTimeout,
|
|
201
|
-
...(a.toolName !== undefined ? { toolName: a.toolName } : {}),
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
// =========================================================================
|
|
205
|
-
// Public readonly state
|
|
206
|
-
// =========================================================================
|
|
207
|
-
get state() {
|
|
208
|
-
return {
|
|
209
|
-
systemPrompt: this.systemPrompt,
|
|
210
|
-
model: { ...this.model },
|
|
211
|
-
thinkingLevel: this.thinkingLevel,
|
|
212
|
-
tools: [...this.currentTools],
|
|
213
|
-
messages: this.messageManager.getAll(),
|
|
214
|
-
isRunning: this.running,
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
// =========================================================================
|
|
218
|
-
// Core methods
|
|
219
|
-
// =========================================================================
|
|
220
|
-
/**
|
|
221
|
-
* Send a user message and run the Agent loop until completion.
|
|
222
|
-
* Always emits `agent_start` at the beginning and `agent_end` at the end,
|
|
223
|
-
* even if an error occurs.
|
|
224
|
-
*/
|
|
225
|
-
async prompt(message) {
|
|
226
|
-
if (this.running) {
|
|
227
|
-
throw new Error('Agent is already running. Call abort() first or wait for idle.');
|
|
228
|
-
}
|
|
229
|
-
const userMessage = typeof message === 'string'
|
|
230
|
-
? { role: 'user', content: message, timestamp: Date.now() }
|
|
231
|
-
: message;
|
|
232
|
-
this.messageManager.push(userMessage);
|
|
233
|
-
await this.runLoopWithLifecycle();
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Resume the Agent loop from the current message state.
|
|
237
|
-
* Useful for retrying after an error without re-sending the user message.
|
|
238
|
-
*/
|
|
239
|
-
async continue() {
|
|
240
|
-
if (this.running) {
|
|
241
|
-
throw new Error('Agent is already running. Call abort() first or wait for idle.');
|
|
242
|
-
}
|
|
243
|
-
await this.runLoopWithLifecycle();
|
|
244
|
-
}
|
|
245
|
-
/** Abort the currently running Agent loop. */
|
|
246
|
-
abort() {
|
|
247
|
-
if (this.abortController) {
|
|
248
|
-
this.abortController.abort();
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Wait until the Agent is idle (not running).
|
|
253
|
-
* Resolves immediately if the Agent is already idle.
|
|
254
|
-
*/
|
|
255
|
-
waitForIdle() {
|
|
256
|
-
if (!this.running) {
|
|
257
|
-
return Promise.resolve();
|
|
258
|
-
}
|
|
259
|
-
return new Promise((resolve) => {
|
|
260
|
-
this.idlePromiseResolvers.push(resolve);
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
// =========================================================================
|
|
264
|
-
// Event subscription
|
|
265
|
-
// =========================================================================
|
|
266
|
-
/**
|
|
267
|
-
* Subscribe to Agent events.
|
|
268
|
-
* @returns An unsubscribe function.
|
|
269
|
-
*/
|
|
270
|
-
subscribe(listener) {
|
|
271
|
-
this.listeners.add(listener);
|
|
272
|
-
return () => {
|
|
273
|
-
this.listeners.delete(listener);
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
// =========================================================================
|
|
277
|
-
// Message queues
|
|
278
|
-
// =========================================================================
|
|
279
|
-
/** Queue high-priority text; an optional ID is echoed only after history acceptance. */
|
|
280
|
-
steer(message, id, parts) {
|
|
281
|
-
this.messageQueueManager.pushSteering(message, id, parts);
|
|
282
|
-
}
|
|
283
|
-
/** Withdraw a queued steering message by its receipt ID; false if the engine already took it. */
|
|
284
|
-
removeQueuedSteering(id) {
|
|
285
|
-
return this.messageQueueManager.removeSteeringById(id);
|
|
286
|
-
}
|
|
287
|
-
/** Queue a low-priority follow-up message (consumed after end_turn). */
|
|
288
|
-
followUp(message) {
|
|
289
|
-
this.messageQueueManager.pushFollowUp(message);
|
|
290
|
-
}
|
|
291
|
-
// =========================================================================
|
|
292
|
-
// State mutation
|
|
293
|
-
// =========================================================================
|
|
294
|
-
setModel(model) {
|
|
295
|
-
this.model = { ...model };
|
|
296
|
-
}
|
|
297
|
-
setSystemPrompt(prompt) {
|
|
298
|
-
this.systemPrompt = prompt;
|
|
299
|
-
}
|
|
300
|
-
setThinkingLevel(level) {
|
|
301
|
-
this.thinkingLevel = level;
|
|
302
|
-
}
|
|
303
|
-
setMaxTokens(value) {
|
|
304
|
-
this.maxTokens = value;
|
|
305
|
-
}
|
|
306
|
-
setTools(tools) {
|
|
307
|
-
this.currentTools = [...tools];
|
|
308
|
-
this.syncToolRegistry(tools);
|
|
309
|
-
}
|
|
310
|
-
clearMessages() {
|
|
311
|
-
this.messageManager.clear();
|
|
312
|
-
}
|
|
313
|
-
/** Append a single message to the conversation history (used by direct-mode flows). */
|
|
314
|
-
appendMessage(message) {
|
|
315
|
-
this.messageManager.push(message);
|
|
316
|
-
}
|
|
317
|
-
/** Replace the entire message history (used when loading a conversation from DB). */
|
|
318
|
-
replaceMessages(messages) {
|
|
319
|
-
this.messageManager.restore(messages);
|
|
320
|
-
}
|
|
321
|
-
/** Clear both steering and follow-up message queues. */
|
|
322
|
-
clearAllQueues() {
|
|
323
|
-
this.messageQueueManager.clearAll();
|
|
324
|
-
}
|
|
325
|
-
// =========================================================================
|
|
326
|
-
// Idle watchdog control
|
|
327
|
-
//
|
|
328
|
-
// Public because only the HOST knows that a tool is blocked on a human. The
|
|
329
|
-
// engine sees an ordinary long-running tool call and would abort the run
|
|
330
|
-
// after `idleTimeout` — which is right for a wedged network call and wrong
|
|
331
|
-
// for a person reading a plan. Always pair these in a `try/finally`.
|
|
332
|
-
// =========================================================================
|
|
333
|
-
/** Suspend the idle watchdog (e.g. while a tool waits on a human). Idempotent. */
|
|
334
|
-
pauseIdleWatchdog() {
|
|
335
|
-
this.watchdog.pause();
|
|
336
|
-
}
|
|
337
|
-
/** Resume the idle watchdog and restart its window from now. Idempotent. */
|
|
338
|
-
resumeIdleWatchdog() {
|
|
339
|
-
this.watchdog.resume();
|
|
340
|
-
}
|
|
341
|
-
// =========================================================================
|
|
342
|
-
// Internal
|
|
343
|
-
// =========================================================================
|
|
344
|
-
/**
|
|
345
|
-
* Emit an event to all listeners and kick the watchdog.
|
|
346
|
-
* Listener errors are caught and logged to prevent crashing the loop.
|
|
347
|
-
*/
|
|
348
|
-
emit(event) {
|
|
349
|
-
// BEFORE THE LISTENERS. A listener that throws is caught below, but the
|
|
350
|
-
// policy still has to have been applied — otherwise a bad subscriber leaves
|
|
351
|
-
// the watchdog armed across a 30-second backoff and the run is aborted for
|
|
352
|
-
// being "idle" while it is provably waiting on purpose.
|
|
353
|
-
this.applyWatchdogPolicy(event);
|
|
354
|
-
recordActivity(this.activity, event);
|
|
355
|
-
for (const listener of this.listeners) {
|
|
356
|
-
try {
|
|
357
|
-
listener(event);
|
|
358
|
-
}
|
|
359
|
-
catch (err) {
|
|
360
|
-
console.error('[Agent] listener error:', err);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
this.watchdog.kick();
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Suspend the idle watchdog across a wait the ENGINE ITSELF scheduled, and
|
|
367
|
-
* resume it when work moves again.
|
|
368
|
-
*
|
|
369
|
-
* TWO PAIRS, ONE POLICY (context-auto-compaction §3.3). Retry backoff was the
|
|
370
|
-
* first (llm-api-retry-backoff §4.6); context compaction is the second, and it
|
|
371
|
-
* is the reason this method lost the `Retry` in its name. Both are cases where
|
|
372
|
-
* the run is provably waiting on purpose and the watchdog cannot tell that
|
|
373
|
-
* apart from a wedged network call.
|
|
374
|
-
*
|
|
375
|
-
* `compaction_end` IS EMITTED FROM A `finally` IN `runCompaction`, on every
|
|
376
|
-
* exit path including a throw, precisely so this `resume()` is unconditional.
|
|
377
|
-
* A compaction whose `end` never arrived would leave the watchdog paused for
|
|
378
|
-
* the rest of the run, with nothing anywhere reporting it (P1-3 / R-13).
|
|
379
|
-
*
|
|
380
|
-
* Both halves are idempotent, and `runLoopWithLifecycle`'s `finally` calls
|
|
381
|
-
* `watchdog.stop()`, which clears `paused` — that is what bounds an abort
|
|
382
|
-
* landing mid-wait.
|
|
383
|
-
*
|
|
384
|
-
* `withRetry` waits up to `maxDelayMs` between attempts and emits nothing while
|
|
385
|
-
* it does. That is indistinguishable from a wedged network call to the
|
|
386
|
-
* watchdog, which is right for a wedged call and wrong for a wait the engine
|
|
387
|
-
* itself scheduled. `pause()` swallows the subsequent `kick()` by design and
|
|
388
|
-
* `resume()` restarts the window from now; both are idempotent.
|
|
389
|
-
*
|
|
390
|
-
* A `retry_scheduled` never followed by a `retry_attempt` — an abort during the
|
|
391
|
-
* wait — leaves the watchdog deaf, and that is bounded rather than leaked:
|
|
392
|
-
* `runLoopWithLifecycle`'s `finally` calls `watchdog.stop()`, and `stop()`
|
|
393
|
-
* clears `paused` for exactly this reason. DO NOT "simplify" that line out of
|
|
394
|
-
* `stop()`.
|
|
395
|
-
*/
|
|
396
|
-
applyWatchdogPolicy(event) {
|
|
397
|
-
if (event.type === 'compaction_start') {
|
|
398
|
-
this.watchdog.pause();
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
if (event.type === 'compaction_end') {
|
|
402
|
-
this.watchdog.resume();
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
if (event.type !== 'message_update')
|
|
406
|
-
return;
|
|
407
|
-
const t = event.streamEvent.type;
|
|
408
|
-
if (t === 'retry_scheduled')
|
|
409
|
-
this.watchdog.pause();
|
|
410
|
-
else if (t === 'retry_attempt')
|
|
411
|
-
this.watchdog.resume();
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Synchronize the ToolRegistry with the provided tool array.
|
|
415
|
-
* Removes tools no longer present and adds new ones.
|
|
416
|
-
*/
|
|
417
|
-
syncToolRegistry(tools) {
|
|
418
|
-
const newNames = new Set(tools.map((t) => t.name));
|
|
419
|
-
// Remove tools no longer in the set.
|
|
420
|
-
for (const existing of this.toolRegistry.getAll()) {
|
|
421
|
-
if (!newNames.has(existing.name)) {
|
|
422
|
-
this.toolRegistry.unregister(existing.name);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
// Add or update tools.
|
|
426
|
-
for (const tool of tools) {
|
|
427
|
-
if (this.toolRegistry.has(tool.name)) {
|
|
428
|
-
// Unregister and re-register to update the definition.
|
|
429
|
-
this.toolRegistry.unregister(tool.name);
|
|
430
|
-
}
|
|
431
|
-
this.toolRegistry.register(tool);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Run the agent loop wrapped with lifecycle events and error handling.
|
|
436
|
-
* Guarantees: agent_start is emitted first, agent_end is always emitted
|
|
437
|
-
* in the finally block, and the watchdog is always stopped.
|
|
438
|
-
*/
|
|
439
|
-
async runLoopWithLifecycle() {
|
|
440
|
-
this.running = true;
|
|
441
|
-
this.watchdogFired = false;
|
|
442
|
-
this.stall = null;
|
|
443
|
-
this.activity = freshActivity();
|
|
444
|
-
this.abortController = new AbortController();
|
|
445
|
-
this.watchdog.start();
|
|
446
|
-
this.emit({ type: 'agent_start' });
|
|
447
|
-
// Tiered reconnect (§4.4): ONE read per run. `null` (no resolver, or a null
|
|
448
|
-
// policy) leaves the loop context untouched, so a host that never opted in is
|
|
449
|
-
// indistinguishable from a pre-feature build.
|
|
450
|
-
const resolvedEscalation = this.resolveEscalation?.() ?? null;
|
|
451
|
-
try {
|
|
452
|
-
await runAgentLoop({
|
|
453
|
-
providerRegistry: this.providerRegistry,
|
|
454
|
-
toolRegistry: this.toolRegistry,
|
|
455
|
-
toolExecutor: this.toolExecutor,
|
|
456
|
-
messageManager: this.messageManager,
|
|
457
|
-
messageQueueManager: this.messageQueueManager,
|
|
458
|
-
getApiKey: this.getApiKeyFn,
|
|
459
|
-
emit: (event) => this.emit(event),
|
|
460
|
-
sandbox: this.sandbox,
|
|
461
|
-
model: this.model,
|
|
462
|
-
systemPrompt: this.systemPrompt,
|
|
463
|
-
thinkingLevel: this.thinkingLevel,
|
|
464
|
-
maxTokens: this.maxTokens,
|
|
465
|
-
// SPREAD, never `contextManager: this.contextManager`. With no manager the
|
|
466
|
-
// loop context has no such key, so `if (!ctx.contextManager)` tests a field
|
|
467
|
-
// that is genuinely absent rather than a property holding `undefined` —
|
|
468
|
-
// which is what makes the byte-identity claim in AC-1 provable.
|
|
469
|
-
...(this.contextManager ? { contextManager: this.contextManager } : {}),
|
|
470
|
-
...(this.projectMessages ? { projectMessages: this.projectMessages } : {}),
|
|
471
|
-
// Read ONCE per run, and spread only when a policy is actually resolved:
|
|
472
|
-
// with the feature off, the loop context has no `escalation` key at all
|
|
473
|
-
// (D-9 / AC-1, the same discipline as `contextManager` above).
|
|
474
|
-
...(resolvedEscalation !== null ? { escalation: resolvedEscalation } : {}),
|
|
475
|
-
signal: this.abortController.signal,
|
|
476
|
-
timeouts: {
|
|
477
|
-
llmCallTimeout: this.llmCallTimeout,
|
|
478
|
-
toolTimeout: this.toolTimeout,
|
|
479
|
-
idleTimeout: this.idleTimeout,
|
|
480
|
-
codeTimeout: this.codeTimeout,
|
|
481
|
-
...(this.compactionHardTimeout !== undefined
|
|
482
|
-
? { compactionHardTimeout: this.compactionHardTimeout }
|
|
483
|
-
: {}),
|
|
484
|
-
},
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
catch (err) {
|
|
488
|
-
console.error('[Agent] loop error:', err);
|
|
489
|
-
// THE ONE PLACE THE LOOP'S THROW BECOMES A FACT A HOST CAN READ
|
|
490
|
-
// (llm-network-error-dead-run D). `runAgentLoop` re-throws the provider's
|
|
491
|
-
// own error, so the retry ladder's verdict is still on it: a retryable
|
|
492
|
-
// `ECONNRESET` arrives here with `retryable: true` and `errorType:
|
|
493
|
-
// 'network_error'`, which is exactly the pair the host needs to say
|
|
494
|
-
// "resume" instead of "ask the user for a key". Read STRUCTURALLY, never
|
|
495
|
-
// through `instanceof`: the engine may be wrapped by a provider adapter.
|
|
496
|
-
const facts = err;
|
|
497
|
-
this.endFailure = {
|
|
498
|
-
errored: true,
|
|
499
|
-
reason: 'llm_error',
|
|
500
|
-
...(typeof facts?.errorType === 'string' ? { errorType: facts.errorType } : {}),
|
|
501
|
-
...(typeof facts?.retryable === 'boolean' ? { retryable: facts.retryable } : {}),
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
finally {
|
|
505
|
-
this.watchdog.stop();
|
|
506
|
-
this.running = false;
|
|
507
|
-
this.abortController = null;
|
|
508
|
-
// THE WATCHDOG'S VERDICT, decided here because the abort path emits no
|
|
509
|
-
// error: a request that went quiet for `idleTimeout` is a TIMEOUT, and it
|
|
510
|
-
// is retryable in the only sense that matters to a host - starting the next
|
|
511
|
-
// turn is safe. `endFailure` wins when both could apply: the loop reached
|
|
512
|
-
// an error of its own, which is the more specific fact.
|
|
513
|
-
const failure = this.watchdogFired && !this.endFailure
|
|
514
|
-
? {
|
|
515
|
-
errored: true,
|
|
516
|
-
reason: 'idle_watchdog',
|
|
517
|
-
errorType: 'timeout',
|
|
518
|
-
retryable: true,
|
|
519
|
-
...(this.stall ? { stall: this.stall } : {}),
|
|
520
|
-
}
|
|
521
|
-
: (this.endFailure ?? {});
|
|
522
|
-
this.endFailure = null;
|
|
523
|
-
this.stall = null;
|
|
524
|
-
this.emit({
|
|
525
|
-
type: 'agent_end',
|
|
526
|
-
messages: this.messageManager.getAll(),
|
|
527
|
-
// ABSENT ON THE HAPPY PATH, so an answered run's `agent_end` keeps the
|
|
528
|
-
// exact shape it had before this feature: `['messages', 'type']`.
|
|
529
|
-
...failure,
|
|
530
|
-
});
|
|
531
|
-
// Resolve all pending waitForIdle() callers.
|
|
532
|
-
for (const resolve of this.idlePromiseResolvers) {
|
|
533
|
-
resolve();
|
|
534
|
-
}
|
|
535
|
-
this.idlePromiseResolvers = [];
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
1
|
+
const _0x5f0c3f=_0x566f;function _0x566f(_0x48e51c,_0x47e4b4){_0x48e51c=_0x48e51c-0x1e1;const _0x49dccf=_0x49dc();let _0x566f06=_0x49dccf[_0x48e51c];if(_0x566f['PBJmlA']===undefined){var _0x96db1c=function(_0x5cfc79){const _0x35658e='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0x2bfc53='',_0x266e12='';for(let _0x1167b1=0x0,_0x27f3a6,_0x5eb84e,_0x33dd95=0x0;_0x5eb84e=_0x5cfc79['charAt'](_0x33dd95++);~_0x5eb84e&&(_0x27f3a6=_0x1167b1%0x4?_0x27f3a6*0x40+_0x5eb84e:_0x5eb84e,_0x1167b1++%0x4)?_0x2bfc53+=String['fromCharCode'](0xff&_0x27f3a6>>(-0x2*_0x1167b1&0x6)):0x0){_0x5eb84e=_0x35658e['indexOf'](_0x5eb84e);}for(let _0x33a460=0x0,_0x10f690=_0x2bfc53['length'];_0x33a460<_0x10f690;_0x33a460++){_0x266e12+='%'+('00'+_0x2bfc53['charCodeAt'](_0x33a460)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(_0x266e12);};_0x566f['aogwEG']=_0x96db1c,_0x566f['kDoMOi']={},_0x566f['PBJmlA']=!![];}const _0x443efb=_0x49dccf[0x0];_0x566f['HcQzwC']!==_0x443efb&&(_0x566f['kDoMOi']={},_0x566f['HcQzwC']=_0x443efb);const _0x1f1d7b=_0x566f['kDoMOi'][_0x48e51c];return _0x1f1d7b===undefined?(_0x566f06=_0x566f['aogwEG'](_0x566f06),_0x566f['kDoMOi'][_0x48e51c]=_0x566f06):_0x566f06=_0x1f1d7b,_0x566f06;}(function(_0x4656af,_0x3c492c){const _0x20e9d8=_0x566f,_0x2a315b=_0x4656af();while(!![]){try{const _0x384b5a=parseInt(_0x20e9d8(0x1ff))/0x1+parseInt(_0x20e9d8(0x240))/0x2+parseInt(_0x20e9d8(0x23b))/0x3+parseInt(_0x20e9d8(0x247))/0x4+parseInt(_0x20e9d8(0x1e7))/0x5+-parseInt(_0x20e9d8(0x248))/0x6+-parseInt(_0x20e9d8(0x25b))/0x7;if(_0x384b5a===_0x3c492c)break;else _0x2a315b['push'](_0x2a315b['shift']());}catch(_0x504d63){_0x2a315b['push'](_0x2a315b['shift']());}}}(_0x49dc,0x36471));import{ToolRegistry}from'../tools/registry.js';import{ToolExecutor}from'../tools/executor.js';import{MessageManager}from'./message-manager.js';import{MessageQueueManager}from'./steering.js';import{IdleWatchdog}from'./watchdog.js';function _0x49dc(){const _0x1b8ce6=['CMvZDw1LswrSzvDHDgnOzg9N','AwrSzv93yxrJAgrVzW','ywDLBNrFzw5K','Bwf4vg9Rzw5Z','Bw9UB3rVBMLJqxq','D2fSBef0','ChjVDMLKzxjszwDPC3rYEq','y29KzvrPBwvVDxq','CMvTB3zLu3rLzxjPBMDcEuLK','yxbWBhLxyxrJAgrVz1bVBgLJEq','ywjVCNq','BgLZDgvUzxjZ','DgHPBMTPBMDFzgvSDge','w0fNzw50xsbPzgXLihDHDgnOzg9NigzPCMvKiokaLcbHyM9YDgLUzW','Dg9VBfjLz2LZDhj5','zw5KrMfPBhvYzq','C2fUzgjVEa','AgfZ','CMv0CNLFC2nOzwr1BgvK','CMv0CNLFyxr0zw1WDa','DgHPBMTPBMDmzxzLBa','CMvZB2X2zuvZy2fSyxrPB24','z2v0qwXS','mZK2mZC5z3PvEKfR','y29Kzv9LEgvJDxrPB25FC3rHCNq','Bw9KzwW','Dg9VBfrPBwvVDxrpDMvYCMLKzxm','Dg9VBe5HBwu','Dg9VBf9YDw5UAw5N','zxjYB3juExbL','y29UDgv4De1HBMfNzxi','Dg9VBf9LEgvJDxrPB25FC3rHCNq','y29UDgLUDwu','Cgf1C2vjzgXLv2f0y2HKB2C','C3rYzwfTx3jLC3rHCNq','yxDHAxrPBMDFCMvZCg9UC2u','DxnLCG','Dg9VBhm','C2v0vg9VBhm','ChvZAezVBgXVD1vW','DgHPBMTPBMDFC3rHCNq','ywn0AxzPDhK','BgfZDev2zw50','ChjVAMvJDe1LC3nHz2vZ','CMvTB3zLuxvLDwvKu3rLzxjPBMC','D2f0y2HKB2DgAxjLza','y29TCgfJDgLVBL9LBMq','ywrK','BMfTzq','DgLTzw91Dhm','Bwf4','D2f0y2HKB2C','ChjVBxb0','w0fNzw50xsbSB29WigvYCM9YoG','Dg9VBfrPBwvVDxq','zgvZy3jPyMvtDgfSBa','C3rYzwfTrxzLBNq','C3rHCNq','y29Uy2f0','zw1PDa','C2v0vgHPBMTPBMDmzxzLBa','y29TCgfJDgLVBKHHCMruAw1LB3v0','zxzLBNrZu2vLBG','BgXTq2fSBfrPBwvVDxq','CMvZB2X2zq','Dg9VBf9LEgvJDxrPB25Fzw5K','Dg9VBhnjBKzSAwDODa','C3vIC2nYAwjL','CMvWBgfJzu1LC3nHz2vZ','y29Kzq','ChvZAa','Dgv4Df9KzwX0yq','BwfW','Dw5YzwDPC3rLCG','Dgv4Da','C3LZDgvTuhjVBxb0','CM91BMq','AwrSzvbYB21PC2vszxnVBhzLCNm','CNvUBMLUzW','DhvYBL9LBMq','CMvNAxn0zxi','C3rVCa','zM9SBg93vxa','ndqYmdq3CLLjAMXW','Dg9VBev4zwn1Dg9Y','CgHHC2u','BwvZC2fNzv91CgrHDgu6','DhLWzq','mJm2mZi0vMjyDwr0','yxbWzw5KtwvZC2fNzq','DhvYBL9ZDgfYDa','DgLTzw91Da','C3rLzxi','y3vYCMvUDfrVB2XZ','ywjVCNrdB250CM9SBgvY','ndK2mdK2EwvRugrv','mtC2oda5mK1WD2Pnsa','C3rHBgW','qwDLBNqGAxmGywXYzwfKEsbYDw5UAw5NlIbdywXSigfIB3j0kcKGzMLYC3qGB3iGD2fPDcbMB3iGAwrSzs4','CMvZDw1L','y29TCgfJDgLVBL9ZDgfYDa','Dg9VBf9JywXSx3n0yxj0','AwrSzvrPBwvVDxq','D2fPDezVCKLKBgu','yM9VBgvHBG','CMv0CNLHyMXL','y2XLyxjnzxnZywDLCW','BM93','C3rHDgu','CMvZDg9Yzq','BgXTx2vYCM9Y','BwvZC2fNzv91CgrHDgu','Dg9VBf9PBNb1Da','CNvUtg9VCfDPDgHmAwzLy3LJBgu','y29Kzv9LEgvJDxrPB25Fzw5K','mZm3nJC4nMrtr1jSvq','z2v0qxbPs2v5','zxjYB3i','BM9Uzq','Cgf1C2u','Dg9VBf9JywXSx2rLBhrH','yMv0D2vLBL90DxjUCW','BwvZC2fNzvf1zxvLtwfUywDLCG','z2v0qxbPs2v5rM4','BwvZC2fNzu1HBMfNzxi','C3LUy1rVB2XszwDPC3rYEq','mta2nZqZnuDKB1ryra'];_0x49dc=function(){return _0x1b8ce6;};return _0x49dc();}import{runAgentLoop}from'./agent-loop.js';const DEFAULT_LLM_CALL_TIMEOUT=0x493e0,DEFAULT_IDLE_TIMEOUT=0xea60,DEFAULT_TOOL_TIMEOUT=0x1d4c0,DEFAULT_CODE_TIMEOUT=0x7530;function freshActivity(){const _0xc3618b=_0x566f;return{'lastEvent':_0xc3618b(0x25e),'phase':_0xc3618b(0x20b),'toolName':undefined,'monotonicAt':performance[_0xc3618b(0x253)](),'wallAt':Date[_0xc3618b(0x253)](),'eventsSeen':0x0,'toolsInFlight':0x0};}function describeEvent(_0x266e12){const _0x9ca72f=_0x566f;return _0x266e12[_0x9ca72f(0x23f)]==='message_update'?_0x9ca72f(0x23e)[_0x9ca72f(0x222)](_0x266e12['streamEvent']['type']):_0x266e12['type'];}function recordActivity(_0x1167b1,_0x27f3a6){const _0x1aaa58=_0x566f;_0x1167b1[_0x1aaa58(0x212)]=describeEvent(_0x27f3a6),_0x1167b1[_0x1aaa58(0x1ec)]=performance[_0x1aaa58(0x253)](),_0x1167b1[_0x1aaa58(0x1ed)]=Date['now'](),_0x1167b1[_0x1aaa58(0x226)]+=0x1;switch(_0x27f3a6['type']){case'agent_start':_0x1167b1[_0x1aaa58(0x22a)]=0x0,_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x20b),_0x1167b1[_0x1aaa58(0x203)]=undefined;return;case _0x1aaa58(0x242):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x20b),_0x1167b1['toolName']=undefined;return;case _0x1aaa58(0x237):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x1e2);return;case _0x1aaa58(0x207):case _0x1aaa58(0x200):_0x1167b1[_0x1aaa58(0x22a)]+=0x1,_0x1167b1['phase']=_0x1aaa58(0x204),_0x1167b1['toolName']=_0x27f3a6[_0x1aaa58(0x23f)]===_0x1aaa58(0x207)?_0x27f3a6[_0x1aaa58(0x203)]:_0x1aaa58(0x22d);return;case _0x1aaa58(0x229):case _0x1aaa58(0x25a):_0x1167b1[_0x1aaa58(0x22a)]=Math[_0x1aaa58(0x21a)](0x0,_0x1167b1[_0x1aaa58(0x22a)]-0x1);_0x1167b1['toolsInFlight']===0x0&&(_0x1167b1['phase']=_0x1aaa58(0x20b),_0x1167b1['toolName']=undefined);return;case _0x1aaa58(0x257):break;default:return;}switch(_0x27f3a6['streamEvent'][_0x1aaa58(0x23f)]){case _0x1aaa58(0x210):case _0x1aaa58(0x1f4):_0x1167b1['phase']='thinking';return;case _0x1aaa58(0x22f):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x232);return;case _0x1aaa58(0x24d):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x258),_0x1167b1[_0x1aaa58(0x203)]=_0x27f3a6['streamEvent'][_0x1aaa58(0x203)];return;case _0x1aaa58(0x1e1):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x258);return;case _0x1aaa58(0x1fb):case _0x1aaa58(0x20a):_0x1167b1[_0x1aaa58(0x23d)]=_0x1aaa58(0x20b),_0x1167b1[_0x1aaa58(0x203)]=undefined;return;default:return;}}export class Agent{[_0x5f0c3f(0x1e5)]=new MessageManager();[_0x5f0c3f(0x1e3)]=new MessageQueueManager();[_0x5f0c3f(0x1f6)]=new ToolRegistry();[_0x5f0c3f(0x23c)];[_0x5f0c3f(0x21b)];[_0x5f0c3f(0x1f3)]=new Set();[_0x5f0c3f(0x233)];[_0x5f0c3f(0x201)];[_0x5f0c3f(0x1fc)];[_0x5f0c3f(0x245)];[_0x5f0c3f(0x1ee)];[_0x5f0c3f(0x1e4)];[_0x5f0c3f(0x1f8)];[_0x5f0c3f(0x206)];[_0x5f0c3f(0x213)];[_0x5f0c3f(0x1fd)];[_0x5f0c3f(0x1eb)];[_0x5f0c3f(0x227)];[_0x5f0c3f(0x24e)];['toolTimeout'];[_0x5f0c3f(0x1ef)];[_0x5f0c3f(0x225)];[_0x5f0c3f(0x246)]=null;['watchdogFired']=![];[_0x5f0c3f(0x249)]=null;[_0x5f0c3f(0x211)]=freshActivity();['endFailure']=null;[_0x5f0c3f(0x236)]=![];['idlePromiseResolvers']=[];constructor(_0x5eb84e){const _0x14008e=_0x5f0c3f;this[_0x14008e(0x233)]=_0x5eb84e[_0x14008e(0x233)],this[_0x14008e(0x201)]={..._0x5eb84e[_0x14008e(0x201)]},this[_0x14008e(0x1fc)]=_0x5eb84e[_0x14008e(0x1fc)]??'off',this[_0x14008e(0x245)]=[..._0x5eb84e[_0x14008e(0x20d)]],this['providerRegistry']=_0x5eb84e[_0x14008e(0x1ee)],this[_0x14008e(0x1e4)]=_0x5eb84e[_0x14008e(0x25c)],this['sandbox']=_0x5eb84e[_0x14008e(0x1f8)],this[_0x14008e(0x206)]=_0x5eb84e[_0x14008e(0x206)],this[_0x14008e(0x213)]=_0x5eb84e[_0x14008e(0x213)],this['resolveEscalation']=_0x5eb84e[_0x14008e(0x1fd)],this[_0x14008e(0x1eb)]=_0x5eb84e[_0x14008e(0x1eb)],this['llmCallTimeout']=_0x5eb84e[_0x14008e(0x219)]?.['llmCallTimeout']??DEFAULT_LLM_CALL_TIMEOUT,this[_0x14008e(0x24e)]=_0x5eb84e[_0x14008e(0x219)]?.[_0x14008e(0x24e)]??DEFAULT_IDLE_TIMEOUT,this[_0x14008e(0x21e)]=_0x5eb84e[_0x14008e(0x219)]?.[_0x14008e(0x21e)]??DEFAULT_TOOL_TIMEOUT,this['codeTimeout']=_0x5eb84e[_0x14008e(0x219)]?.[_0x14008e(0x1ef)]??DEFAULT_CODE_TIMEOUT,this[_0x14008e(0x225)]=_0x5eb84e[_0x14008e(0x219)]?.[_0x14008e(0x225)],this['syncToolRegistry'](_0x5eb84e[_0x14008e(0x20d)]),this[_0x14008e(0x23c)]=new ToolExecutor(this['toolRegistry'],{'defaultTimeout':this[_0x14008e(0x21e)],..._0x5eb84e[_0x14008e(0x219)]?.['toolTimeoutOverrides']?{'timeoutOverrides':_0x5eb84e['timeouts'][_0x14008e(0x202)]}:{}}),this[_0x14008e(0x21b)]=new IdleWatchdog(this['idleTimeout'],()=>{const _0x3c8b1b=_0x14008e;console[_0x3c8b1b(0x25d)](_0x3c8b1b(0x1f5)),this[_0x3c8b1b(0x215)]=!![],this[_0x3c8b1b(0x249)]=this[_0x3c8b1b(0x21f)](),this[_0x3c8b1b(0x1f2)]();});}[_0x5f0c3f(0x21f)](){const _0x43e58d=_0x5f0c3f,_0x33dd95=this[_0x43e58d(0x211)];return{'phase':_0x33dd95[_0x43e58d(0x23d)],'lastEvent':_0x33dd95['lastEvent'],'msSinceLastEvent':Math[_0x43e58d(0x234)](performance[_0x43e58d(0x253)]()-_0x33dd95['monotonicAt']),'wallMsSinceLastEvent':Date[_0x43e58d(0x253)]()-_0x33dd95[_0x43e58d(0x1ed)],'eventsSeen':_0x33dd95[_0x43e58d(0x226)],'idleTimeoutMs':this['idleTimeout'],..._0x33dd95[_0x43e58d(0x203)]!==undefined?{'toolName':_0x33dd95[_0x43e58d(0x203)]}:{}};}get[_0x5f0c3f(0x254)](){const _0x4ad03e=_0x5f0c3f;return{'systemPrompt':this[_0x4ad03e(0x233)],'model':{...this[_0x4ad03e(0x201)]},'thinkingLevel':this[_0x4ad03e(0x1fc)],'tools':[...this[_0x4ad03e(0x245)]],'messages':this[_0x4ad03e(0x1e5)][_0x4ad03e(0x1fe)](),'isRunning':this[_0x4ad03e(0x236)]};}async[_0x5f0c3f(0x21c)](_0x33a460){const _0x3baa85=_0x5f0c3f;if(this[_0x3baa85(0x236)])throw new Error('Agent\x20is\x20already\x20running.\x20Call\x20abort()\x20first\x20or\x20wait\x20for\x20idle.');const _0x10f690=typeof _0x33a460==='string'?{'role':_0x3baa85(0x20c),'content':_0x33a460,'timestamp':Date['now']()}:_0x33a460;this[_0x3baa85(0x1e5)][_0x3baa85(0x22e)](_0x10f690),await this[_0x3baa85(0x259)]();}async[_0x5f0c3f(0x208)](){const _0x10bc4c=_0x5f0c3f;if(this[_0x10bc4c(0x236)])throw new Error(_0x10bc4c(0x24a));await this['runLoopWithLifecycle']();}['abort'](){const _0x20ed9a=_0x5f0c3f;this[_0x20ed9a(0x246)]&&this['abortController'][_0x20ed9a(0x1f2)]();}[_0x5f0c3f(0x24f)](){const _0x1bbda0=_0x5f0c3f;if(!this[_0x1bbda0(0x236)])return Promise[_0x1bbda0(0x228)]();return new Promise(_0x1c5827=>{const _0x17d28f=_0x1bbda0;this[_0x17d28f(0x235)][_0x17d28f(0x22e)](_0x1c5827);});}[_0x5f0c3f(0x22b)](_0x25164f){const _0x1c8902=_0x5f0c3f;return this[_0x1c8902(0x1f3)][_0x1c8902(0x217)](_0x25164f),()=>{const _0x4bdf14=_0x1c8902;this[_0x4bdf14(0x1f3)]['delete'](_0x25164f);};}[_0x5f0c3f(0x244)](_0x20d646,_0x25d125,_0x517e64){const _0x438295=_0x5f0c3f;this[_0x438295(0x1e3)]['pushSteering'](_0x20d646,_0x25d125,_0x517e64);}[_0x5f0c3f(0x214)](_0x86e372){const _0x298091=_0x5f0c3f;return this['messageQueueManager'][_0x298091(0x1f0)](_0x86e372);}[_0x5f0c3f(0x23a)](_0x4a4a6b){const _0xf6f16e=_0x5f0c3f;this[_0xf6f16e(0x1e3)][_0xf6f16e(0x20f)](_0x4a4a6b);}['setModel'](_0x25a450){this['model']={..._0x25a450};}['setSystemPrompt'](_0x5d5459){const _0x38090a=_0x5f0c3f;this[_0x38090a(0x233)]=_0x5d5459;}[_0x5f0c3f(0x224)](_0xa945a0){this['thinkingLevel']=_0xa945a0;}['setMaxTokens'](_0x485d95){const _0x49d343=_0x5f0c3f;this[_0x49d343(0x1eb)]=_0x485d95;}[_0x5f0c3f(0x20e)](_0x53e3d7){const _0x5a40dc=_0x5f0c3f;this['currentTools']=[..._0x53e3d7],this[_0x5a40dc(0x1e6)](_0x53e3d7);}[_0x5f0c3f(0x252)](){const _0x125177=_0x5f0c3f;this[_0x125177(0x1e5)]['clear']();}[_0x5f0c3f(0x241)](_0x2bb534){const _0x117e02=_0x5f0c3f;this[_0x117e02(0x1e5)][_0x117e02(0x22e)](_0x2bb534);}[_0x5f0c3f(0x22c)](_0x27c66f){const _0x40dbfd=_0x5f0c3f;this['messageManager'][_0x40dbfd(0x255)](_0x27c66f);}['clearAllQueues'](){const _0x5bd731=_0x5f0c3f;this[_0x5bd731(0x1e3)]['clearAll']();}[_0x5f0c3f(0x209)](){const _0x5af74b=_0x5f0c3f;this[_0x5af74b(0x21b)][_0x5af74b(0x25f)]();}[_0x5f0c3f(0x1e8)](){const _0x4a9872=_0x5f0c3f;this['watchdog'][_0x4a9872(0x24b)]();}[_0x5f0c3f(0x223)](_0x119923){const _0x50ce26=_0x5f0c3f;this[_0x50ce26(0x1f1)](_0x119923),recordActivity(this[_0x50ce26(0x211)],_0x119923);for(const _0x223762 of this['listeners']){try{_0x223762(_0x119923);}catch(_0x3a88d0){console[_0x50ce26(0x25d)]('[Agent]\x20listener\x20error:',_0x3a88d0);}}this['watchdog']['kick']();}['applyWatchdogPolicy'](_0x593b00){const _0x114043=_0x5f0c3f;if(_0x593b00[_0x114043(0x23f)]===_0x114043(0x24c)){this[_0x114043(0x21b)][_0x114043(0x25f)]();return;}if(_0x593b00[_0x114043(0x23f)]===_0x114043(0x216)){this[_0x114043(0x21b)][_0x114043(0x24b)]();return;}if(_0x593b00[_0x114043(0x23f)]!==_0x114043(0x257))return;const _0x1d7c26=_0x593b00[_0x114043(0x220)][_0x114043(0x23f)];if(_0x1d7c26===_0x114043(0x1fa))this[_0x114043(0x21b)]['pause']();else{if(_0x1d7c26===_0x114043(0x1fb))this[_0x114043(0x21b)][_0x114043(0x24b)]();}}[_0x5f0c3f(0x1e6)](_0x52486b){const _0x4f9e3a=_0x5f0c3f,_0x3c8600=new Set(_0x52486b[_0x4f9e3a(0x230)](_0x4b64c3=>_0x4b64c3[_0x4f9e3a(0x218)]));for(const _0xdedbe of this[_0x4f9e3a(0x1f6)][_0x4f9e3a(0x1fe)]()){!_0x3c8600[_0x4f9e3a(0x1f9)](_0xdedbe[_0x4f9e3a(0x218)])&&this[_0x4f9e3a(0x1f6)][_0x4f9e3a(0x231)](_0xdedbe[_0x4f9e3a(0x218)]);}for(const _0x324c34 of _0x52486b){this[_0x4f9e3a(0x1f6)]['has'](_0x324c34[_0x4f9e3a(0x218)])&&this[_0x4f9e3a(0x1f6)]['unregister'](_0x324c34[_0x4f9e3a(0x218)]),this[_0x4f9e3a(0x1f6)][_0x4f9e3a(0x238)](_0x324c34);}}async[_0x5f0c3f(0x259)](){const _0x2a25ae=_0x5f0c3f;this['running']=!![],this['watchdogFired']=![],this['stall']=null,this[_0x2a25ae(0x211)]=freshActivity(),this[_0x2a25ae(0x246)]=new AbortController(),this['watchdog'][_0x2a25ae(0x221)](),this['emit']({'type':'agent_start'});const _0x5746b6=this[_0x2a25ae(0x1fd)]?.()??null;try{await runAgentLoop({'providerRegistry':this[_0x2a25ae(0x1ee)],'toolRegistry':this['toolRegistry'],'toolExecutor':this[_0x2a25ae(0x23c)],'messageManager':this[_0x2a25ae(0x1e5)],'messageQueueManager':this['messageQueueManager'],'getApiKey':this[_0x2a25ae(0x1e4)],'emit':_0x35a567=>this[_0x2a25ae(0x223)](_0x35a567),'sandbox':this[_0x2a25ae(0x1f8)],'model':this[_0x2a25ae(0x201)],'systemPrompt':this[_0x2a25ae(0x233)],'thinkingLevel':this[_0x2a25ae(0x1fc)],'maxTokens':this[_0x2a25ae(0x1eb)],...this['contextManager']?{'contextManager':this['contextManager']}:{},...this[_0x2a25ae(0x213)]?{'projectMessages':this['projectMessages']}:{},..._0x5746b6!==null?{'escalation':_0x5746b6}:{},'signal':this[_0x2a25ae(0x246)]['signal'],'timeouts':{'llmCallTimeout':this[_0x2a25ae(0x227)],'toolTimeout':this['toolTimeout'],'idleTimeout':this[_0x2a25ae(0x24e)],'codeTimeout':this[_0x2a25ae(0x1ef)],...this[_0x2a25ae(0x225)]!==undefined?{'compactionHardTimeout':this[_0x2a25ae(0x225)]}:{}}});}catch(_0x1d265d){console['error'](_0x2a25ae(0x21d),_0x1d265d);const _0x1d51db=_0x1d265d;this['endFailure']={'errored':!![],'reason':_0x2a25ae(0x256),...typeof _0x1d51db?.['errorType']==='string'?{'errorType':_0x1d51db[_0x2a25ae(0x205)]}:{},...typeof _0x1d51db?.['retryable']===_0x2a25ae(0x250)?{'retryable':_0x1d51db[_0x2a25ae(0x251)]}:{}};}finally{this[_0x2a25ae(0x21b)][_0x2a25ae(0x239)](),this[_0x2a25ae(0x236)]=![],this[_0x2a25ae(0x246)]=null;const _0x1c92b3=this['watchdogFired']&&!this[_0x2a25ae(0x1f7)]?{'errored':!![],'reason':_0x2a25ae(0x1e9),'errorType':_0x2a25ae(0x243),'retryable':!![],...this[_0x2a25ae(0x249)]?{'stall':this[_0x2a25ae(0x249)]}:{}}:this['endFailure']??{};this[_0x2a25ae(0x1f7)]=null,this[_0x2a25ae(0x249)]=null,this[_0x2a25ae(0x223)]({'type':_0x2a25ae(0x1ea),'messages':this[_0x2a25ae(0x1e5)][_0x2a25ae(0x1fe)](),..._0x1c92b3});for(const _0x30c799 of this[_0x2a25ae(0x235)]){_0x30c799();}this['idlePromiseResolvers']=[];}}}
|