laohuang 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -141
- package/dist/bin.js +12028 -0
- package/dist/bin.js.map +7 -0
- package/package.json +28 -18
- package/dist/agent.d.ts +0 -166
- package/dist/agent.js +0 -858
- package/dist/agent.js.map +0 -1
- package/dist/bash-runner.d.ts +0 -79
- package/dist/bash-runner.js +0 -464
- package/dist/bash-runner.js.map +0 -1
- package/dist/cancellation.d.ts +0 -36
- package/dist/cancellation.js +0 -123
- package/dist/cancellation.js.map +0 -1
- package/dist/cli.d.ts +0 -117
- package/dist/cli.js +0 -1307
- package/dist/cli.js.map +0 -1
- package/dist/client.d.ts +0 -21
- package/dist/client.js +0 -17
- package/dist/client.js.map +0 -1
- package/dist/commands.d.ts +0 -123
- package/dist/commands.js +0 -660
- package/dist/commands.js.map +0 -1
- package/dist/config.d.ts +0 -51
- package/dist/config.js +0 -183
- package/dist/config.js.map +0 -1
- package/dist/credentials.d.ts +0 -12
- package/dist/credentials.js +0 -102
- package/dist/credentials.js.map +0 -1
- package/dist/events.d.ts +0 -288
- package/dist/events.js +0 -838
- package/dist/events.js.map +0 -1
- package/dist/model-adapter.d.ts +0 -138
- package/dist/model-adapter.js +0 -244
- package/dist/model-adapter.js.map +0 -1
- package/dist/model-selection.d.ts +0 -67
- package/dist/model-selection.js +0 -148
- package/dist/model-selection.js.map +0 -1
- package/dist/model-stream.d.ts +0 -128
- package/dist/model-stream.js +0 -582
- package/dist/model-stream.js.map +0 -1
- package/dist/project-instructions.d.ts +0 -99
- package/dist/project-instructions.js +0 -348
- package/dist/project-instructions.js.map +0 -1
- package/dist/providers.d.ts +0 -9
- package/dist/providers.js +0 -26
- package/dist/providers.js.map +0 -1
- package/dist/routing.d.ts +0 -170
- package/dist/routing.js +0 -669
- package/dist/routing.js.map +0 -1
- package/dist/semantic-classifier.d.ts +0 -66
- package/dist/semantic-classifier.js +0 -86
- package/dist/semantic-classifier.js.map +0 -1
- package/dist/session.d.ts +0 -162
- package/dist/session.js +0 -871
- package/dist/session.js.map +0 -1
- package/dist/system-prompt.d.ts +0 -16
- package/dist/system-prompt.js +0 -42
- package/dist/system-prompt.js.map +0 -1
- package/dist/terminal/editor.d.ts +0 -161
- package/dist/terminal/editor.js +0 -1060
- package/dist/terminal/editor.js.map +0 -1
- package/dist/terminal/input.d.ts +0 -61
- package/dist/terminal/input.js +0 -276
- package/dist/terminal/input.js.map +0 -1
- package/dist/terminal/markdown.d.ts +0 -20
- package/dist/terminal/markdown.js +0 -621
- package/dist/terminal/markdown.js.map +0 -1
- package/dist/terminal/screen.d.ts +0 -66
- package/dist/terminal/screen.js +0 -624
- package/dist/terminal/screen.js.map +0 -1
- package/dist/terminal/theme.d.ts +0 -23
- package/dist/terminal/theme.js +0 -101
- package/dist/terminal/theme.js.map +0 -1
- package/dist/terminal/ui.d.ts +0 -287
- package/dist/terminal/ui.js +0 -1819
- package/dist/terminal/ui.js.map +0 -1
- package/dist/tools.d.ts +0 -95
- package/dist/tools.js +0 -444
- package/dist/tools.js.map +0 -1
- package/dist/ui-state.d.ts +0 -51
- package/dist/ui-state.js +0 -194
- package/dist/ui-state.js.map +0 -1
- package/dist/web.d.ts +0 -56
- package/dist/web.js +0 -247
- package/dist/web.js.map +0 -1
package/dist/routing.js
DELETED
|
@@ -1,669 +0,0 @@
|
|
|
1
|
-
/** Deterministic routing, task registry, and runtime input queues. */
|
|
2
|
-
import { CancelToken } from "./cancellation.js";
|
|
3
|
-
import { EventKind, EventSource, } from "./events.js";
|
|
4
|
-
// ---------------------------------------------------------------------------
|
|
5
|
-
// Task registry
|
|
6
|
-
// ---------------------------------------------------------------------------
|
|
7
|
-
export const TaskState = {
|
|
8
|
-
RunningModel: "running_model",
|
|
9
|
-
RunningTools: "running_tools",
|
|
10
|
-
Cancelling: "cancelling",
|
|
11
|
-
Cancelled: "cancelled",
|
|
12
|
-
Completed: "completed",
|
|
13
|
-
Failed: "failed",
|
|
14
|
-
};
|
|
15
|
-
const TERMINAL_TASK_STATES = new Set([
|
|
16
|
-
TaskState.Cancelled,
|
|
17
|
-
TaskState.Completed,
|
|
18
|
-
TaskState.Failed,
|
|
19
|
-
]);
|
|
20
|
-
const ALLOWED_TASK_TRANSITIONS = new Map([
|
|
21
|
-
[
|
|
22
|
-
TaskState.RunningModel,
|
|
23
|
-
new Set([
|
|
24
|
-
TaskState.RunningTools,
|
|
25
|
-
TaskState.Cancelling,
|
|
26
|
-
TaskState.Completed,
|
|
27
|
-
TaskState.Failed,
|
|
28
|
-
]),
|
|
29
|
-
],
|
|
30
|
-
[
|
|
31
|
-
TaskState.RunningTools,
|
|
32
|
-
new Set([
|
|
33
|
-
TaskState.RunningModel,
|
|
34
|
-
TaskState.Cancelling,
|
|
35
|
-
TaskState.Completed,
|
|
36
|
-
TaskState.Failed,
|
|
37
|
-
]),
|
|
38
|
-
],
|
|
39
|
-
[TaskState.Cancelling, new Set([TaskState.Cancelled])],
|
|
40
|
-
[TaskState.Cancelled, new Set()],
|
|
41
|
-
[TaskState.Completed, new Set()],
|
|
42
|
-
[TaskState.Failed, new Set()],
|
|
43
|
-
]);
|
|
44
|
-
/** Task records with at most one active task. */
|
|
45
|
-
export class TaskRegistry {
|
|
46
|
-
tasks = new Map();
|
|
47
|
-
activeTaskIdValue = null;
|
|
48
|
-
get activeTaskId() {
|
|
49
|
-
return this.activeTaskIdValue;
|
|
50
|
-
}
|
|
51
|
-
register(taskId, options = {}) {
|
|
52
|
-
const state = options.state ?? TaskState.RunningModel;
|
|
53
|
-
const activate = options.activate ?? true;
|
|
54
|
-
if (this.tasks.has(taskId)) {
|
|
55
|
-
throw new Error(`task already exists: ${taskId}`);
|
|
56
|
-
}
|
|
57
|
-
if (activate && this.activeTaskIdValue !== null) {
|
|
58
|
-
throw new Error("another task is already active");
|
|
59
|
-
}
|
|
60
|
-
const record = {
|
|
61
|
-
taskId,
|
|
62
|
-
state,
|
|
63
|
-
cancelToken: options.cancelToken ?? new CancelToken(),
|
|
64
|
-
result: null,
|
|
65
|
-
error: null,
|
|
66
|
-
};
|
|
67
|
-
this.tasks.set(taskId, record);
|
|
68
|
-
if (activate && !TERMINAL_TASK_STATES.has(state)) {
|
|
69
|
-
this.activeTaskIdValue = taskId;
|
|
70
|
-
}
|
|
71
|
-
return { ...record };
|
|
72
|
-
}
|
|
73
|
-
get(taskId) {
|
|
74
|
-
const record = this.tasks.get(taskId);
|
|
75
|
-
return record === undefined ? null : { ...record };
|
|
76
|
-
}
|
|
77
|
-
active() {
|
|
78
|
-
if (this.activeTaskIdValue === null) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
const record = this.tasks.get(this.activeTaskIdValue);
|
|
82
|
-
return record === undefined ? null : { ...record };
|
|
83
|
-
}
|
|
84
|
-
transition(taskId, state, options = {}) {
|
|
85
|
-
const record = this.tasks.get(taskId);
|
|
86
|
-
if (record === undefined) {
|
|
87
|
-
throw new Error(`unknown task: ${taskId}`);
|
|
88
|
-
}
|
|
89
|
-
if (state !== record.state &&
|
|
90
|
-
!ALLOWED_TASK_TRANSITIONS.get(record.state)?.has(state)) {
|
|
91
|
-
throw new Error(`invalid task transition: ${record.state} -> ${state}`);
|
|
92
|
-
}
|
|
93
|
-
const next = {
|
|
94
|
-
...record,
|
|
95
|
-
state,
|
|
96
|
-
result: options.result !== undefined ? options.result : record.result,
|
|
97
|
-
error: options.error !== undefined ? options.error : record.error,
|
|
98
|
-
};
|
|
99
|
-
if (TERMINAL_TASK_STATES.has(state)) {
|
|
100
|
-
if (this.activeTaskIdValue === taskId) {
|
|
101
|
-
this.activeTaskIdValue = null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
const active = this.activeTaskIdValue;
|
|
106
|
-
if (active !== null && active !== taskId) {
|
|
107
|
-
throw new Error("another task is already active");
|
|
108
|
-
}
|
|
109
|
-
this.activeTaskIdValue = taskId;
|
|
110
|
-
}
|
|
111
|
-
this.tasks.set(taskId, next);
|
|
112
|
-
return { ...next };
|
|
113
|
-
}
|
|
114
|
-
records() {
|
|
115
|
-
return [...this.tasks.values()].map((record) => ({ ...record }));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// ---------------------------------------------------------------------------
|
|
119
|
-
// Token estimates
|
|
120
|
-
// ---------------------------------------------------------------------------
|
|
121
|
-
/** Provider-neutral conservative token estimate without a tokenizer. */
|
|
122
|
-
export function estimateTokens(text) {
|
|
123
|
-
if (!text) {
|
|
124
|
-
return 0;
|
|
125
|
-
}
|
|
126
|
-
return Math.max(1, Math.floor((new TextEncoder().encode(text).length + 3) / 4));
|
|
127
|
-
}
|
|
128
|
-
function eventTokens(routed) {
|
|
129
|
-
const content = routed.event.payload["content"];
|
|
130
|
-
return estimateTokens(typeof content === "string" ? content : String(content ?? ""));
|
|
131
|
-
}
|
|
132
|
-
/** Bounded inspection queue for events rejected by routing or capacity. */
|
|
133
|
-
export class DeadLetterQueue {
|
|
134
|
-
maxItems;
|
|
135
|
-
items = [];
|
|
136
|
-
constructor(maxItems = 100) {
|
|
137
|
-
if (maxItems <= 0) {
|
|
138
|
-
throw new RangeError("maxItems must be positive");
|
|
139
|
-
}
|
|
140
|
-
this.maxItems = maxItems;
|
|
141
|
-
}
|
|
142
|
-
put(event, reason) {
|
|
143
|
-
if (this.items.length >= this.maxItems) {
|
|
144
|
-
this.items.shift();
|
|
145
|
-
}
|
|
146
|
-
this.items.push({ event, reason });
|
|
147
|
-
}
|
|
148
|
-
drain() {
|
|
149
|
-
const items = this.items;
|
|
150
|
-
this.items = [];
|
|
151
|
-
return items;
|
|
152
|
-
}
|
|
153
|
-
snapshot() {
|
|
154
|
-
return [...this.items];
|
|
155
|
-
}
|
|
156
|
-
clear() {
|
|
157
|
-
return this.drain().length;
|
|
158
|
-
}
|
|
159
|
-
get size() {
|
|
160
|
-
return this.items.length;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
function isRouteDecision(verdict) {
|
|
164
|
-
return typeof verdict === "object" && verdict !== null;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Four-layer router with deterministic safety as the final arbiter.
|
|
168
|
-
*
|
|
169
|
-
* Layers one through three short-circuit after obtaining a decision. Layer
|
|
170
|
-
* four always runs, so an injected semantic classifier can never override
|
|
171
|
-
* cancellation, invalid task ownership, or a custom safety policy.
|
|
172
|
-
*/
|
|
173
|
-
export class EventRouter {
|
|
174
|
-
taskRegistry;
|
|
175
|
-
semanticClassifier;
|
|
176
|
-
safetyPolicy;
|
|
177
|
-
constructor(taskRegistry, options = {}) {
|
|
178
|
-
this.taskRegistry = taskRegistry;
|
|
179
|
-
this.semanticClassifier = options.semanticClassifier ?? null;
|
|
180
|
-
this.safetyPolicy = options.safetyPolicy ?? null;
|
|
181
|
-
}
|
|
182
|
-
async route(event) {
|
|
183
|
-
const active = this.taskRegistry.active();
|
|
184
|
-
let decision = this.metadataMatch(event, active) ??
|
|
185
|
-
this.deterministicRules(event, active) ??
|
|
186
|
-
(await this.semanticClassification(event, active)) ??
|
|
187
|
-
EventRouter.defaultDecision(event, active);
|
|
188
|
-
decision = this.safetyArbiter(event, active, decision);
|
|
189
|
-
return { event, decision };
|
|
190
|
-
}
|
|
191
|
-
metadataMatch(event, active) {
|
|
192
|
-
if (event.task_id === null) {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
const target = this.taskRegistry.get(event.task_id);
|
|
196
|
-
if (target === null) {
|
|
197
|
-
return {
|
|
198
|
-
taskId: event.task_id,
|
|
199
|
-
destination: "drop",
|
|
200
|
-
timing: "immediate",
|
|
201
|
-
strategy: "reject",
|
|
202
|
-
confidence: 1.0,
|
|
203
|
-
reason: "metadata references an unknown task",
|
|
204
|
-
layer: 1,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
if (event.source !== EventSource.User && event.source !== EventSource.Cli) {
|
|
208
|
-
if (active === null ||
|
|
209
|
-
target.taskId !== active.taskId ||
|
|
210
|
-
TERMINAL_TASK_STATES.has(target.state)) {
|
|
211
|
-
return {
|
|
212
|
-
taskId: target.taskId,
|
|
213
|
-
destination: "drop",
|
|
214
|
-
timing: "immediate",
|
|
215
|
-
strategy: "reject",
|
|
216
|
-
confidence: 1.0,
|
|
217
|
-
reason: "stale internal callback targeted an inactive task",
|
|
218
|
-
layer: 1,
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
return {
|
|
222
|
-
taskId: target.taskId,
|
|
223
|
-
destination: "current_task",
|
|
224
|
-
timing: "immediate",
|
|
225
|
-
strategy: "execute",
|
|
226
|
-
confidence: 1.0,
|
|
227
|
-
reason: "internal callback matched task metadata",
|
|
228
|
-
layer: 1,
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
if (active !== null && target.taskId === active.taskId) {
|
|
232
|
-
const held = active.state === TaskState.Cancelling;
|
|
233
|
-
return {
|
|
234
|
-
taskId: target.taskId,
|
|
235
|
-
destination: held ? "held" : "pending",
|
|
236
|
-
timing: held ? "after_cancel" : "safe_point",
|
|
237
|
-
strategy: "steer",
|
|
238
|
-
confidence: 1.0,
|
|
239
|
-
reason: "user input explicitly matched the active task",
|
|
240
|
-
layer: 1,
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
return {
|
|
244
|
-
taskId: target.taskId,
|
|
245
|
-
destination: "held",
|
|
246
|
-
timing: "after_cancel",
|
|
247
|
-
strategy: "follow_up",
|
|
248
|
-
confidence: 1.0,
|
|
249
|
-
reason: "user input targets an inactive task",
|
|
250
|
-
layer: 1,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
deterministicRules(event, active) {
|
|
254
|
-
const rawContent = event.payload["content"];
|
|
255
|
-
const content = String(rawContent ?? "").trim();
|
|
256
|
-
if (event.kind === EventKind.InputCancelRequested ||
|
|
257
|
-
content === "/cancel") {
|
|
258
|
-
return {
|
|
259
|
-
taskId: active?.taskId ?? null,
|
|
260
|
-
destination: "control",
|
|
261
|
-
timing: "immediate",
|
|
262
|
-
strategy: "cancel",
|
|
263
|
-
confidence: 1.0,
|
|
264
|
-
reason: "cancel is an immediate control event",
|
|
265
|
-
layer: 2,
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
if (event.kind === EventKind.InputSlashCommand) {
|
|
269
|
-
return {
|
|
270
|
-
taskId: active?.taskId ?? null,
|
|
271
|
-
destination: "control",
|
|
272
|
-
timing: "immediate",
|
|
273
|
-
strategy: "execute",
|
|
274
|
-
confidence: 1.0,
|
|
275
|
-
reason: "slash commands are deterministic local control input",
|
|
276
|
-
layer: 2,
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
if (event.kind !== EventKind.InputReceived) {
|
|
280
|
-
return {
|
|
281
|
-
taskId: event.task_id || active?.taskId || null,
|
|
282
|
-
destination: "current_task",
|
|
283
|
-
timing: "immediate",
|
|
284
|
-
strategy: "execute",
|
|
285
|
-
confidence: 1.0,
|
|
286
|
-
reason: "non-input event follows deterministic callback routing",
|
|
287
|
-
layer: 2,
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
if (active === null) {
|
|
291
|
-
return {
|
|
292
|
-
taskId: null,
|
|
293
|
-
destination: "new_task",
|
|
294
|
-
timing: "immediate",
|
|
295
|
-
strategy: "execute",
|
|
296
|
-
confidence: 1.0,
|
|
297
|
-
reason: "no active task",
|
|
298
|
-
layer: 2,
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
if (active.state === TaskState.Cancelling) {
|
|
302
|
-
return {
|
|
303
|
-
taskId: active.taskId,
|
|
304
|
-
destination: "held",
|
|
305
|
-
timing: "after_cancel",
|
|
306
|
-
strategy: "follow_up",
|
|
307
|
-
confidence: 1.0,
|
|
308
|
-
reason: "ordinary input is held while cancellation settles",
|
|
309
|
-
layer: 2,
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
const requested = event.payload["strategy"];
|
|
313
|
-
if (requested === "steer") {
|
|
314
|
-
return {
|
|
315
|
-
taskId: active.taskId,
|
|
316
|
-
destination: "pending",
|
|
317
|
-
timing: "safe_point",
|
|
318
|
-
strategy: "steer",
|
|
319
|
-
confidence: 1.0,
|
|
320
|
-
reason: "explicit steering strategy",
|
|
321
|
-
layer: 2,
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
if (requested === "follow_up") {
|
|
325
|
-
return {
|
|
326
|
-
taskId: active.taskId,
|
|
327
|
-
destination: "pending",
|
|
328
|
-
timing: "safe_point",
|
|
329
|
-
strategy: "follow_up",
|
|
330
|
-
confidence: 1.0,
|
|
331
|
-
reason: "explicit follow-up strategy",
|
|
332
|
-
layer: 2,
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
return null;
|
|
336
|
-
}
|
|
337
|
-
async semanticClassification(event, active) {
|
|
338
|
-
if (this.semanticClassifier === null || active === null) {
|
|
339
|
-
return null;
|
|
340
|
-
}
|
|
341
|
-
const classifier = this.semanticClassifier;
|
|
342
|
-
const classified = typeof classifier === "function"
|
|
343
|
-
? await classifier(event, active)
|
|
344
|
-
: await classifier.classify(event, active);
|
|
345
|
-
if (classified === null || classified === undefined) {
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
if (isRouteDecision(classified)) {
|
|
349
|
-
return classified;
|
|
350
|
-
}
|
|
351
|
-
const strategy = String(classified);
|
|
352
|
-
if (strategy !== "steer" && strategy !== "follow_up") {
|
|
353
|
-
return null;
|
|
354
|
-
}
|
|
355
|
-
return {
|
|
356
|
-
taskId: active.taskId,
|
|
357
|
-
destination: "pending",
|
|
358
|
-
timing: "safe_point",
|
|
359
|
-
strategy,
|
|
360
|
-
confidence: 0.75,
|
|
361
|
-
reason: "semantic classifier resolved ambiguous input",
|
|
362
|
-
layer: 3,
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
static defaultDecision(event, active) {
|
|
366
|
-
if (active === null) {
|
|
367
|
-
return {
|
|
368
|
-
taskId: null,
|
|
369
|
-
destination: "new_task",
|
|
370
|
-
timing: "immediate",
|
|
371
|
-
strategy: "execute",
|
|
372
|
-
confidence: 0.5,
|
|
373
|
-
reason: "defaulted to a new task",
|
|
374
|
-
layer: 3,
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
return {
|
|
378
|
-
taskId: active.taskId,
|
|
379
|
-
destination: "pending",
|
|
380
|
-
timing: "safe_point",
|
|
381
|
-
strategy: "follow_up",
|
|
382
|
-
confidence: 0.5,
|
|
383
|
-
reason: "ambiguous input defaults to a safe follow-up",
|
|
384
|
-
layer: 3,
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
safetyArbiter(event, active, decision) {
|
|
388
|
-
let current = decision;
|
|
389
|
-
if (this.safetyPolicy !== null) {
|
|
390
|
-
const verdict = this.safetyPolicy(event, current);
|
|
391
|
-
if (isRouteDecision(verdict)) {
|
|
392
|
-
current = verdict;
|
|
393
|
-
}
|
|
394
|
-
else if (verdict === false) {
|
|
395
|
-
current = {
|
|
396
|
-
taskId: current.taskId,
|
|
397
|
-
destination: "drop",
|
|
398
|
-
timing: "immediate",
|
|
399
|
-
strategy: "reject",
|
|
400
|
-
confidence: 1.0,
|
|
401
|
-
reason: "custom safety policy rejected the event",
|
|
402
|
-
layer: 4,
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
// These invariants run after the custom policy, so neither a classifier
|
|
407
|
-
// nor a policy callback can downgrade cancellation or target an unknown
|
|
408
|
-
// task.
|
|
409
|
-
if (event.kind === EventKind.InputCancelRequested) {
|
|
410
|
-
return {
|
|
411
|
-
taskId: active?.taskId ?? null,
|
|
412
|
-
destination: "control",
|
|
413
|
-
timing: "immediate",
|
|
414
|
-
strategy: "cancel",
|
|
415
|
-
confidence: 1.0,
|
|
416
|
-
reason: "safety arbiter preserves immediate cancellation",
|
|
417
|
-
layer: 4,
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
if (current.taskId !== null &&
|
|
421
|
-
this.taskRegistry.get(current.taskId) === null &&
|
|
422
|
-
current.destination !== "new_task") {
|
|
423
|
-
return {
|
|
424
|
-
taskId: current.taskId,
|
|
425
|
-
destination: "drop",
|
|
426
|
-
timing: "immediate",
|
|
427
|
-
strategy: "reject",
|
|
428
|
-
confidence: 1.0,
|
|
429
|
-
reason: "safety arbiter rejected an unknown task",
|
|
430
|
-
layer: 4,
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
return current;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
// ---------------------------------------------------------------------------
|
|
437
|
-
// Bounded runtime queues
|
|
438
|
-
// ---------------------------------------------------------------------------
|
|
439
|
-
/** Raised when a bounded queue exceeds its item or token budget. */
|
|
440
|
-
export class QueueOverflowError extends Error {
|
|
441
|
-
constructor(message) {
|
|
442
|
-
super(message);
|
|
443
|
-
this.name = "QueueOverflowError";
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
function compatibilityKey(routed) {
|
|
447
|
-
return JSON.stringify([routed.event.session_id, routed.decision.taskId]);
|
|
448
|
-
}
|
|
449
|
-
/** FIFO queue with atomic compatible-batch snapshots. */
|
|
450
|
-
export class PendingQueue {
|
|
451
|
-
maxItems;
|
|
452
|
-
maxEstimatedTokens;
|
|
453
|
-
items = [];
|
|
454
|
-
estimatedTokensValue = 0;
|
|
455
|
-
constructor(maxItems = 100, options = {}) {
|
|
456
|
-
const maxEstimatedTokens = options.maxEstimatedTokens ?? 8_000;
|
|
457
|
-
if (maxItems <= 0) {
|
|
458
|
-
throw new RangeError("maxItems must be positive");
|
|
459
|
-
}
|
|
460
|
-
if (maxEstimatedTokens <= 0) {
|
|
461
|
-
throw new RangeError("maxEstimatedTokens must be positive");
|
|
462
|
-
}
|
|
463
|
-
this.maxItems = maxItems;
|
|
464
|
-
this.maxEstimatedTokens = maxEstimatedTokens;
|
|
465
|
-
}
|
|
466
|
-
put(event) {
|
|
467
|
-
if (event.decision.destination !== "pending") {
|
|
468
|
-
throw new Error("PendingQueue only accepts pending events");
|
|
469
|
-
}
|
|
470
|
-
if (this.items.length >= this.maxItems) {
|
|
471
|
-
throw new QueueOverflowError("pending queue is full");
|
|
472
|
-
}
|
|
473
|
-
const tokens = eventTokens(event);
|
|
474
|
-
if (this.estimatedTokensValue + tokens > this.maxEstimatedTokens) {
|
|
475
|
-
throw new QueueOverflowError("pending queue token budget is full");
|
|
476
|
-
}
|
|
477
|
-
this.items.push(event);
|
|
478
|
-
this.estimatedTokensValue += tokens;
|
|
479
|
-
}
|
|
480
|
-
/** Atomically remove all currently eligible events in source order. */
|
|
481
|
-
drainCompatible(options = {}) {
|
|
482
|
-
const taskId = options.taskId ?? null;
|
|
483
|
-
const strategy = options.strategy ?? null;
|
|
484
|
-
const timing = options.timing ?? null;
|
|
485
|
-
if (this.items.length === 0) {
|
|
486
|
-
return [];
|
|
487
|
-
}
|
|
488
|
-
const pivot = this.items.find((item) => taskId === null || item.decision.taskId === taskId);
|
|
489
|
-
if (pivot === undefined) {
|
|
490
|
-
return [];
|
|
491
|
-
}
|
|
492
|
-
const key = compatibilityKey(pivot);
|
|
493
|
-
const drained = [];
|
|
494
|
-
const retained = [];
|
|
495
|
-
for (const item of this.items) {
|
|
496
|
-
let compatible = compatibilityKey(item) === key;
|
|
497
|
-
if (strategy !== null) {
|
|
498
|
-
compatible = compatible && item.decision.strategy === strategy;
|
|
499
|
-
}
|
|
500
|
-
if (timing !== null) {
|
|
501
|
-
compatible = compatible && item.decision.timing === timing;
|
|
502
|
-
}
|
|
503
|
-
if (compatible) {
|
|
504
|
-
drained.push(item);
|
|
505
|
-
}
|
|
506
|
-
else {
|
|
507
|
-
retained.push(item);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
this.items = retained;
|
|
511
|
-
this.estimatedTokensValue = retained.reduce((sum, item) => sum + eventTokens(item), 0);
|
|
512
|
-
return drained;
|
|
513
|
-
}
|
|
514
|
-
/** Atomically remove every strategy bucket belonging to a task. */
|
|
515
|
-
drainTask(taskId) {
|
|
516
|
-
const drained = [];
|
|
517
|
-
const retained = [];
|
|
518
|
-
for (const item of this.items) {
|
|
519
|
-
if (item.decision.taskId === taskId) {
|
|
520
|
-
drained.push(item);
|
|
521
|
-
}
|
|
522
|
-
else {
|
|
523
|
-
retained.push(item);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
this.items = retained;
|
|
527
|
-
this.estimatedTokensValue = retained.reduce((sum, item) => sum + eventTokens(item), 0);
|
|
528
|
-
return drained;
|
|
529
|
-
}
|
|
530
|
-
get estimatedTokens() {
|
|
531
|
-
return this.estimatedTokensValue;
|
|
532
|
-
}
|
|
533
|
-
snapshot() {
|
|
534
|
-
return [...this.items];
|
|
535
|
-
}
|
|
536
|
-
clear() {
|
|
537
|
-
const count = this.items.length;
|
|
538
|
-
this.items = [];
|
|
539
|
-
this.estimatedTokensValue = 0;
|
|
540
|
-
return count;
|
|
541
|
-
}
|
|
542
|
-
get size() {
|
|
543
|
-
return this.items.length;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
/** Inputs retained while the active task is cancelling. */
|
|
547
|
-
export class HeldQueue {
|
|
548
|
-
maxItems;
|
|
549
|
-
maxEstimatedTokens;
|
|
550
|
-
items = [];
|
|
551
|
-
estimatedTokensValue = 0;
|
|
552
|
-
constructor(maxItems = 100, options = {}) {
|
|
553
|
-
const maxEstimatedTokens = options.maxEstimatedTokens ?? 8_000;
|
|
554
|
-
if (maxItems <= 0) {
|
|
555
|
-
throw new RangeError("maxItems must be positive");
|
|
556
|
-
}
|
|
557
|
-
if (maxEstimatedTokens <= 0) {
|
|
558
|
-
throw new RangeError("maxEstimatedTokens must be positive");
|
|
559
|
-
}
|
|
560
|
-
this.maxItems = maxItems;
|
|
561
|
-
this.maxEstimatedTokens = maxEstimatedTokens;
|
|
562
|
-
}
|
|
563
|
-
put(event) {
|
|
564
|
-
if (event.decision.destination !== "held") {
|
|
565
|
-
throw new Error("HeldQueue only accepts held events");
|
|
566
|
-
}
|
|
567
|
-
if (this.items.length >= this.maxItems) {
|
|
568
|
-
throw new QueueOverflowError("held queue is full");
|
|
569
|
-
}
|
|
570
|
-
const tokens = eventTokens(event);
|
|
571
|
-
if (this.estimatedTokensValue + tokens > this.maxEstimatedTokens) {
|
|
572
|
-
throw new QueueOverflowError("held queue token budget is full");
|
|
573
|
-
}
|
|
574
|
-
this.items.push(event);
|
|
575
|
-
this.estimatedTokensValue += tokens;
|
|
576
|
-
}
|
|
577
|
-
drain(options = {}) {
|
|
578
|
-
const taskId = options.taskId ?? null;
|
|
579
|
-
if (taskId === null) {
|
|
580
|
-
const items = this.items;
|
|
581
|
-
this.items = [];
|
|
582
|
-
this.estimatedTokensValue = 0;
|
|
583
|
-
return items;
|
|
584
|
-
}
|
|
585
|
-
const drained = [];
|
|
586
|
-
const retained = [];
|
|
587
|
-
for (const item of this.items) {
|
|
588
|
-
if (item.decision.taskId === taskId) {
|
|
589
|
-
drained.push(item);
|
|
590
|
-
}
|
|
591
|
-
else {
|
|
592
|
-
retained.push(item);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
this.items = retained;
|
|
596
|
-
this.estimatedTokensValue = retained.reduce((sum, item) => sum + eventTokens(item), 0);
|
|
597
|
-
return drained;
|
|
598
|
-
}
|
|
599
|
-
get estimatedTokens() {
|
|
600
|
-
return this.estimatedTokensValue;
|
|
601
|
-
}
|
|
602
|
-
clear() {
|
|
603
|
-
return this.drain().length;
|
|
604
|
-
}
|
|
605
|
-
get size() {
|
|
606
|
-
return this.items.length;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
export class Scheduler {
|
|
610
|
-
pending;
|
|
611
|
-
held;
|
|
612
|
-
deadLetters;
|
|
613
|
-
constructor(options = {}) {
|
|
614
|
-
this.pending = options.pending ?? new PendingQueue();
|
|
615
|
-
this.held = options.held ?? new HeldQueue();
|
|
616
|
-
this.deadLetters = options.deadLetters ?? new DeadLetterQueue();
|
|
617
|
-
}
|
|
618
|
-
schedule(event) {
|
|
619
|
-
const destination = event.decision.destination;
|
|
620
|
-
try {
|
|
621
|
-
if (destination === "pending") {
|
|
622
|
-
this.pending.put(event);
|
|
623
|
-
return { immediate: [], queued: true, rejected: false, reason: "" };
|
|
624
|
-
}
|
|
625
|
-
if (destination === "held") {
|
|
626
|
-
this.held.put(event);
|
|
627
|
-
return { immediate: [], queued: true, rejected: false, reason: "" };
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
catch (error) {
|
|
631
|
-
if (!(error instanceof QueueOverflowError)) {
|
|
632
|
-
throw error;
|
|
633
|
-
}
|
|
634
|
-
const reason = error.message;
|
|
635
|
-
this.deadLetters.put(event.event, reason);
|
|
636
|
-
return { immediate: [], queued: false, rejected: true, reason };
|
|
637
|
-
}
|
|
638
|
-
if (destination === "drop") {
|
|
639
|
-
this.deadLetters.put(event.event, event.decision.reason);
|
|
640
|
-
return {
|
|
641
|
-
immediate: [],
|
|
642
|
-
queued: false,
|
|
643
|
-
rejected: true,
|
|
644
|
-
reason: event.decision.reason,
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
return { immediate: [event], queued: false, rejected: false, reason: "" };
|
|
648
|
-
}
|
|
649
|
-
safePoint(taskId) {
|
|
650
|
-
return this.pending.drainCompatible({ taskId });
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
/** Combine a pending snapshot into one model input without losing IDs. */
|
|
654
|
-
export function combineInput(events) {
|
|
655
|
-
const parts = [];
|
|
656
|
-
for (const item of events) {
|
|
657
|
-
const raw = item.event.payload["content"];
|
|
658
|
-
const content = String(raw ?? "").trim();
|
|
659
|
-
if (content) {
|
|
660
|
-
parts.push(`[event_id=${item.event.event_id}]\n${content}`);
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
if (parts.length === 0) {
|
|
664
|
-
return "";
|
|
665
|
-
}
|
|
666
|
-
return ("Please handle all of these pending messages in one response:\n\n" +
|
|
667
|
-
parts.join("\n\n"));
|
|
668
|
-
}
|
|
669
|
-
//# sourceMappingURL=routing.js.map
|