@devxiyang/agent-kernel 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +525 -0
- package/dist/core/agent/agent.d.ts +132 -0
- package/dist/core/agent/agent.d.ts.map +1 -0
- package/dist/core/agent/agent.js +301 -0
- package/dist/core/agent/agent.js.map +1 -0
- package/dist/core/agent/index.d.ts +13 -0
- package/dist/core/agent/index.d.ts.map +1 -0
- package/dist/core/agent/index.js +10 -0
- package/dist/core/agent/index.js.map +1 -0
- package/dist/core/agent/loop.d.ts +30 -0
- package/dist/core/agent/loop.d.ts.map +1 -0
- package/dist/core/agent/loop.js +378 -0
- package/dist/core/agent/loop.js.map +1 -0
- package/dist/core/agent/types.d.ts +231 -0
- package/dist/core/agent/types.d.ts.map +1 -0
- package/dist/core/agent/types.js +9 -0
- package/dist/core/agent/types.js.map +1 -0
- package/dist/core/agent/wrap-tool.d.ts +12 -0
- package/dist/core/agent/wrap-tool.d.ts.map +1 -0
- package/dist/core/agent/wrap-tool.js +37 -0
- package/dist/core/agent/wrap-tool.js.map +1 -0
- package/dist/core/kernel/index.d.ts +12 -0
- package/dist/core/kernel/index.d.ts.map +1 -0
- package/dist/core/kernel/index.js +10 -0
- package/dist/core/kernel/index.js.map +1 -0
- package/dist/core/kernel/kernel.d.ts +15 -0
- package/dist/core/kernel/kernel.d.ts.map +1 -0
- package/dist/core/kernel/kernel.js +320 -0
- package/dist/core/kernel/kernel.js.map +1 -0
- package/dist/core/kernel/session-store.d.ts +24 -0
- package/dist/core/kernel/session-store.d.ts.map +1 -0
- package/dist/core/kernel/session-store.js +90 -0
- package/dist/core/kernel/session-store.js.map +1 -0
- package/dist/core/kernel/types.d.ts +215 -0
- package/dist/core/kernel/types.d.ts.map +1 -0
- package/dist/core/kernel/types.js +11 -0
- package/dist/core/kernel/types.js.map +1 -0
- package/dist/event-stream.d.ts +25 -0
- package/dist/event-stream.d.ts.map +1 -0
- package/dist/event-stream.js +58 -0
- package/dist/event-stream.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent — stateful wrapper around kernel + runLoop.
|
|
3
|
+
*
|
|
4
|
+
* Owns:
|
|
5
|
+
* - AbortController lifecycle (per-prompt)
|
|
6
|
+
* - Steering / follow-up message queues
|
|
7
|
+
* - Event fan-out to subscribers
|
|
8
|
+
* - Mutable config (stream, tools, maxSteps)
|
|
9
|
+
* - Runtime state (streaming message, pending tool calls, error)
|
|
10
|
+
*
|
|
11
|
+
* Does not own:
|
|
12
|
+
* - Kernel lifecycle (caller creates and passes in)
|
|
13
|
+
* - Conversation log (kernel handles both kernel.jsonl and log.jsonl)
|
|
14
|
+
* - Compaction decisions (caller checks kernel.contextSize and calls kernel.compact())
|
|
15
|
+
*/
|
|
16
|
+
import { createKernel } from '../kernel';
|
|
17
|
+
import { runLoop } from './loop';
|
|
18
|
+
// ─── Agent ───────────────────────────────────────────────────────────────────
|
|
19
|
+
/**
|
|
20
|
+
* High-level stateful agent.
|
|
21
|
+
*
|
|
22
|
+
* Owns the AbortController lifecycle, steering/follow-up queues, event fan-out
|
|
23
|
+
* to subscribers, and mutable config. The underlying kernel (conversation store)
|
|
24
|
+
* is created externally and injected — the Agent never touches persistence directly.
|
|
25
|
+
*/
|
|
26
|
+
export class Agent {
|
|
27
|
+
_kernel;
|
|
28
|
+
_stream;
|
|
29
|
+
_tools;
|
|
30
|
+
_maxSteps;
|
|
31
|
+
_transformContext;
|
|
32
|
+
_onStepEnd;
|
|
33
|
+
_steeringMode;
|
|
34
|
+
_followUpMode;
|
|
35
|
+
_parallelTools;
|
|
36
|
+
_onContextFull;
|
|
37
|
+
_toolTimeout;
|
|
38
|
+
_retryOnError;
|
|
39
|
+
_abortController = null;
|
|
40
|
+
_runningPromise = null;
|
|
41
|
+
_steeringQueue = [];
|
|
42
|
+
_followUpQueue = [];
|
|
43
|
+
_listeners = new Set();
|
|
44
|
+
// ── Runtime state ──────────────────────────────────────────────────────
|
|
45
|
+
_streamEntry = null;
|
|
46
|
+
_streamText = '';
|
|
47
|
+
_streamReasoning = '';
|
|
48
|
+
_streamToolCalls = [];
|
|
49
|
+
_pendingToolCalls = new Set();
|
|
50
|
+
_error = null;
|
|
51
|
+
constructor(kernel, options) {
|
|
52
|
+
this._kernel = kernel;
|
|
53
|
+
this._stream = options.stream;
|
|
54
|
+
this._tools = options.tools;
|
|
55
|
+
this._maxSteps = options.maxSteps;
|
|
56
|
+
this._transformContext = options.transformContext;
|
|
57
|
+
this._onStepEnd = options.onStepEnd;
|
|
58
|
+
this._steeringMode = options.steeringMode ?? 'one-at-a-time';
|
|
59
|
+
this._followUpMode = options.followUpMode ?? 'one-at-a-time';
|
|
60
|
+
this._parallelTools = options.parallelTools;
|
|
61
|
+
this._onContextFull = options.onContextFull;
|
|
62
|
+
this._toolTimeout = options.toolTimeout;
|
|
63
|
+
this._retryOnError = options.retryOnError;
|
|
64
|
+
}
|
|
65
|
+
// ── State ──────────────────────────────────────────────────────────────
|
|
66
|
+
/** The underlying kernel that owns conversation history and persistence. */
|
|
67
|
+
get kernel() { return this._kernel; }
|
|
68
|
+
/** Snapshot of the current runtime state (read-only). */
|
|
69
|
+
get state() {
|
|
70
|
+
return {
|
|
71
|
+
isRunning: this._abortController !== null,
|
|
72
|
+
streamEntry: this._streamEntry,
|
|
73
|
+
pendingToolCalls: this._pendingToolCalls,
|
|
74
|
+
error: this._error,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// ── Mutators ─────────────────────────────────────────────────────────────
|
|
78
|
+
/** Replace the LLM streaming function (takes effect on the next run). */
|
|
79
|
+
setStream(stream) { this._stream = stream; }
|
|
80
|
+
/** Replace the tool set (takes effect on the next run). */
|
|
81
|
+
setTools(tools) { this._tools = tools; }
|
|
82
|
+
/** Update the maximum number of loop steps (takes effect on the next run). */
|
|
83
|
+
setMaxSteps(maxSteps) { this._maxSteps = maxSteps; }
|
|
84
|
+
/** Change how many steering messages are dequeued per check. */
|
|
85
|
+
setSteeringMode(mode) { this._steeringMode = mode; }
|
|
86
|
+
/** Change how many follow-up messages are dequeued per check. */
|
|
87
|
+
setFollowUpMode(mode) { this._followUpMode = mode; }
|
|
88
|
+
// ── Event subscription ──────────────────────────────────────────────────
|
|
89
|
+
/**
|
|
90
|
+
* Register a listener for all agent events.
|
|
91
|
+
* Returns an unsubscribe function — call it to stop receiving events.
|
|
92
|
+
*/
|
|
93
|
+
subscribe(fn) {
|
|
94
|
+
this._listeners.add(fn);
|
|
95
|
+
return () => { this._listeners.delete(fn); };
|
|
96
|
+
}
|
|
97
|
+
// ── Prompt ──────────────────────────────────────────────────────────────
|
|
98
|
+
/**
|
|
99
|
+
* Append one or more user entries to the kernel and start a new agent run.
|
|
100
|
+
* Throws if the agent is already running — use steer() or followUp() instead.
|
|
101
|
+
*/
|
|
102
|
+
prompt(entries) {
|
|
103
|
+
if (this.state.isRunning) {
|
|
104
|
+
throw new Error('Agent is already running. Use steer() or followUp() to queue messages.');
|
|
105
|
+
}
|
|
106
|
+
for (const entry of Array.isArray(entries) ? entries : [entries]) {
|
|
107
|
+
this._kernel.append(entry);
|
|
108
|
+
}
|
|
109
|
+
this._error = null;
|
|
110
|
+
this._run();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resume execution after an error, abort, or when steering/follow-up messages
|
|
114
|
+
* are queued but the loop has already exited.
|
|
115
|
+
* Throws if already running or if there is nothing to continue from.
|
|
116
|
+
*/
|
|
117
|
+
continue() {
|
|
118
|
+
if (this.state.isRunning) {
|
|
119
|
+
throw new Error('Agent is already running.');
|
|
120
|
+
}
|
|
121
|
+
const lastEntry = this._kernel.peek();
|
|
122
|
+
if (!lastEntry) {
|
|
123
|
+
throw new Error('No conversation to continue from. Use prompt() to start.');
|
|
124
|
+
}
|
|
125
|
+
const hasQueued = this._steeringQueue.length > 0 || this._followUpQueue.length > 0;
|
|
126
|
+
if (lastEntry.type === 'assistant') {
|
|
127
|
+
const { stopReason } = lastEntry.payload;
|
|
128
|
+
const isRetriable = stopReason === 'error' || stopReason === 'aborted';
|
|
129
|
+
if (!isRetriable && !hasQueued) {
|
|
130
|
+
throw new Error('Nothing to continue from. Use prompt() to start a new turn.');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
this._error = null;
|
|
134
|
+
this._run();
|
|
135
|
+
}
|
|
136
|
+
// ── Steering / Follow-up ────────────────────────────────────────────────
|
|
137
|
+
/**
|
|
138
|
+
* Queue a steering message that interrupts the current run between tool calls.
|
|
139
|
+
* Safe to call while the agent is running. The loop picks it up on the next
|
|
140
|
+
* steering check and skips any remaining tool calls in the current batch.
|
|
141
|
+
*/
|
|
142
|
+
steer(entries) {
|
|
143
|
+
this._steeringQueue.push(...(Array.isArray(entries) ? entries : [entries]));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Queue a follow-up message to be processed after the current run completes.
|
|
147
|
+
* Causes the outer loop to continue rather than stop when the agent would
|
|
148
|
+
* otherwise go idle.
|
|
149
|
+
*/
|
|
150
|
+
followUp(entries) {
|
|
151
|
+
this._followUpQueue.push(...(Array.isArray(entries) ? entries : [entries]));
|
|
152
|
+
}
|
|
153
|
+
// ── Abort ───────────────────────────────────────────────────────────────
|
|
154
|
+
/** Cancel the current run. No-op if not running. */
|
|
155
|
+
abort() {
|
|
156
|
+
this._abortController?.abort();
|
|
157
|
+
}
|
|
158
|
+
// ── Reset ────────────────────────────────────────────────────────────────
|
|
159
|
+
/**
|
|
160
|
+
* Clear all queues and transient runtime state (stream entry, pending tool calls, error).
|
|
161
|
+
* Does NOT touch the kernel or conversation history.
|
|
162
|
+
* Throws if called while running — abort() first.
|
|
163
|
+
*/
|
|
164
|
+
reset() {
|
|
165
|
+
if (this.state.isRunning) {
|
|
166
|
+
throw new Error('Cannot reset while running. Call abort() first.');
|
|
167
|
+
}
|
|
168
|
+
this._steeringQueue.splice(0);
|
|
169
|
+
this._followUpQueue.splice(0);
|
|
170
|
+
this._streamEntry = null;
|
|
171
|
+
this._streamText = '';
|
|
172
|
+
this._streamReasoning = '';
|
|
173
|
+
this._streamToolCalls = [];
|
|
174
|
+
this._pendingToolCalls = new Set();
|
|
175
|
+
this._error = null;
|
|
176
|
+
}
|
|
177
|
+
// ── Wait ────────────────────────────────────────────────────────────────
|
|
178
|
+
/** Resolves when the agent finishes its current run (or immediately if idle). */
|
|
179
|
+
async waitForIdle() {
|
|
180
|
+
if (this._runningPromise) {
|
|
181
|
+
try {
|
|
182
|
+
await this._runningPromise;
|
|
183
|
+
}
|
|
184
|
+
catch { /* caller handles errors via subscribe */ }
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// ── Private ─────────────────────────────────────────────────────────────
|
|
188
|
+
_run() {
|
|
189
|
+
this._abortController = new AbortController();
|
|
190
|
+
const eventStream = runLoop(this._kernel, {
|
|
191
|
+
stream: this._stream,
|
|
192
|
+
tools: this._tools,
|
|
193
|
+
maxSteps: this._maxSteps,
|
|
194
|
+
signal: this._abortController.signal,
|
|
195
|
+
transformContext: this._transformContext,
|
|
196
|
+
onStepEnd: this._onStepEnd,
|
|
197
|
+
getSteeringMessages: () => this._drainSteering(),
|
|
198
|
+
getFollowUpMessages: () => this._drainFollowUp(),
|
|
199
|
+
parallelTools: this._parallelTools,
|
|
200
|
+
onContextFull: this._onContextFull,
|
|
201
|
+
toolTimeout: this._toolTimeout,
|
|
202
|
+
retryOnError: this._retryOnError,
|
|
203
|
+
});
|
|
204
|
+
this._runningPromise = this._consume(eventStream);
|
|
205
|
+
}
|
|
206
|
+
async _consume(stream) {
|
|
207
|
+
try {
|
|
208
|
+
for await (const event of stream) {
|
|
209
|
+
this._handleEvent(event);
|
|
210
|
+
for (const fn of this._listeners) {
|
|
211
|
+
fn(event);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return await stream.result();
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
this._abortController = null;
|
|
221
|
+
this._runningPromise = null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
_handleEvent(event) {
|
|
225
|
+
switch (event.type) {
|
|
226
|
+
case 'turn_start':
|
|
227
|
+
this._streamText = '';
|
|
228
|
+
this._streamReasoning = '';
|
|
229
|
+
this._streamToolCalls = [];
|
|
230
|
+
this._streamEntry = null;
|
|
231
|
+
break;
|
|
232
|
+
case 'text_delta':
|
|
233
|
+
this._streamText += event.delta;
|
|
234
|
+
this._updateStreamEntry();
|
|
235
|
+
break;
|
|
236
|
+
case 'reasoning_delta':
|
|
237
|
+
this._streamReasoning += event.delta;
|
|
238
|
+
this._updateStreamEntry();
|
|
239
|
+
break;
|
|
240
|
+
case 'tool_call':
|
|
241
|
+
this._streamToolCalls.push({
|
|
242
|
+
toolCallId: event.toolCallId,
|
|
243
|
+
toolName: event.toolName,
|
|
244
|
+
input: event.input,
|
|
245
|
+
});
|
|
246
|
+
this._pendingToolCalls.add(event.toolCallId);
|
|
247
|
+
this._updateStreamEntry();
|
|
248
|
+
break;
|
|
249
|
+
case 'tool_result':
|
|
250
|
+
this._pendingToolCalls.delete(event.toolCallId);
|
|
251
|
+
break;
|
|
252
|
+
case 'message_end':
|
|
253
|
+
this._streamEntry = null;
|
|
254
|
+
break;
|
|
255
|
+
case 'turn_end':
|
|
256
|
+
this._streamEntry = null;
|
|
257
|
+
break;
|
|
258
|
+
case 'agent_end':
|
|
259
|
+
this._streamEntry = null;
|
|
260
|
+
this._pendingToolCalls = new Set();
|
|
261
|
+
if (event.error) {
|
|
262
|
+
this._error = event.error;
|
|
263
|
+
}
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
_updateStreamEntry() {
|
|
268
|
+
this._streamEntry = {
|
|
269
|
+
type: 'assistant',
|
|
270
|
+
payload: {
|
|
271
|
+
text: this._streamText,
|
|
272
|
+
reasoning: this._streamReasoning || undefined,
|
|
273
|
+
toolCalls: this._streamToolCalls,
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
async _drainSteering() {
|
|
278
|
+
if (this._steeringQueue.length === 0)
|
|
279
|
+
return [];
|
|
280
|
+
return this._steeringMode === 'one-at-a-time'
|
|
281
|
+
? this._steeringQueue.splice(0, 1)
|
|
282
|
+
: this._steeringQueue.splice(0);
|
|
283
|
+
}
|
|
284
|
+
async _drainFollowUp() {
|
|
285
|
+
if (this._followUpQueue.length === 0)
|
|
286
|
+
return [];
|
|
287
|
+
return this._followUpMode === 'one-at-a-time'
|
|
288
|
+
? this._followUpQueue.splice(0, 1)
|
|
289
|
+
: this._followUpQueue.splice(0);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Convenience factory that creates a kernel (optionally with persistence) and
|
|
294
|
+
* wraps it in an Agent. Prefer this over constructing Agent directly.
|
|
295
|
+
*/
|
|
296
|
+
export function createAgent(options) {
|
|
297
|
+
const { session, ...agentOptions } = options;
|
|
298
|
+
const kernel = createKernel(session);
|
|
299
|
+
return new Agent(kernel, agentOptions);
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../../src/core/agent/agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAyBhC,gFAAgF;AAEhF;;;;;;GAMG;AACH,MAAM,OAAO,KAAK;IACC,OAAO,CAAa;IAE7B,OAAO,CAAoB;IAC3B,MAAM,CAAwB;IAC9B,SAAS,CAAgB;IACzB,iBAAiB,CAAkC;IACnD,UAAU,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AAG,YAAY,CAAC,OAAO,CAAC,CAAA;IACpC,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;AACxC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module agent-kernel/agent
|
|
3
|
+
*
|
|
4
|
+
* Public surface of the Agent module. Exports all types, the Agent class,
|
|
5
|
+
* the createAgent factory, the low-level runLoop function, and the wrapTool helper.
|
|
6
|
+
*/
|
|
7
|
+
export type { Usage, AgentEntry, AgentMessage, StopReason, ToolCallInfo, } from '../kernel';
|
|
8
|
+
export type { ToolContent, ToolResult, ToolResultInfo, LLMStreamEvent, LLMStopReason, LLMStepResult, StreamFn, AgentTool, AgentConfig, AgentEvent, AgentResult, AgentOptions, QueueMode, BlockResult, BeforeToolCallResult, BeforeToolCallHook, AfterToolCallResult, AfterToolCallHook, ToolWrapHooks, } from './types';
|
|
9
|
+
export { Agent, createAgent } from './agent';
|
|
10
|
+
export type { AgentState, AgentSessionOptions } from './agent';
|
|
11
|
+
export { runLoop } from './loop';
|
|
12
|
+
export { wrapTool } from './wrap-tool';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/agent/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,YAAY,EACV,KAAK,EACL,UAAU,EACV,YAAY,EACZ,UAAU,EACV,YAAY,GACb,MAAM,WAAW,CAAA;AAElB,YAAY,EACV,WAAW,EACX,UAAU,EACV,cAAc,EACd,cAAc,EACd,aAAa,EACb,aAAa,EACb,QAAQ,EACR,SAAS,EACT,WAAW,EACX,UAAU,EACV,WAAW,EACX,YAAY,EACZ,SAAS,EACT,WAAW,EACX,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,aAAa,GACd,MAAM,SAAS,CAAA;AAEhB,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAC5C,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module agent-kernel/agent
|
|
3
|
+
*
|
|
4
|
+
* Public surface of the Agent module. Exports all types, the Agent class,
|
|
5
|
+
* the createAgent factory, the low-level runLoop function, and the wrapTool helper.
|
|
6
|
+
*/
|
|
7
|
+
export { Agent, createAgent } from './agent';
|
|
8
|
+
export { runLoop } from './loop';
|
|
9
|
+
export { wrapTool } from './wrap-tool';
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/agent/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgCH,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop — the "userspace shell" around the kernel.
|
|
3
|
+
*
|
|
4
|
+
* Design:
|
|
5
|
+
* - No dependency on any LLM provider (AI SDK, OpenAI, etc.).
|
|
6
|
+
* - Provider is injected as StreamFn; tools are injected as AgentTool[].
|
|
7
|
+
* - The kernel is the single source of truth for conversation history.
|
|
8
|
+
* - Nested loop pattern: inner loop handles tool calls
|
|
9
|
+
* and steering; outer loop handles follow-up messages.
|
|
10
|
+
*
|
|
11
|
+
* Each inner iteration (one turn):
|
|
12
|
+
* 1. Write pending entries to kernel.
|
|
13
|
+
* 2. Emit turn_start.
|
|
14
|
+
* 3. buildContext → transformContext? → AgentMessage[]
|
|
15
|
+
* 4. stream(messages) → LLMStepResult, emitting LLMStreamEvents to UI.
|
|
16
|
+
* 5. Write assistant entry to kernel.
|
|
17
|
+
* 6. Execute tool calls (write each result to kernel, check steering between calls).
|
|
18
|
+
* 7. Emit turn_end { toolResults }.
|
|
19
|
+
* 8. If tool calls were made → repeat from 1 (LLM processes results).
|
|
20
|
+
* 9. Else → check for follow-up messages (outer loop) or stop.
|
|
21
|
+
*/
|
|
22
|
+
import { EventStream } from '../../event-stream';
|
|
23
|
+
import type { AgentKernel } from '../kernel';
|
|
24
|
+
import type { AgentConfig, AgentEvent, AgentResult } from './types';
|
|
25
|
+
/**
|
|
26
|
+
* Start the agent loop. Returns an EventStream immediately.
|
|
27
|
+
* Consumer iterates events with `for await` and awaits stream.result().
|
|
28
|
+
*/
|
|
29
|
+
export declare function runLoop(kernel: AgentKernel, config: AgentConfig): EventStream<AgentEvent, AgentResult>;
|
|
30
|
+
//# sourceMappingURL=loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../../../src/core/agent/loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAC5C,OAAO,KAAK,EAEV,WAAW,EACX,UAAU,EACV,WAAW,EAOZ,MAAM,SAAS,CAAA;AAIhB;;;GAGG;AACH,wBAAgB,OAAO,CACrB,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,GAClB,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,CAItC"}
|