@tangle-network/agent-runtime 0.84.0 → 0.85.0
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.js +1 -1
- package/dist/{chunk-UJW6EVNY.js → chunk-DLAEEF26.js} +817 -2
- package/dist/chunk-DLAEEF26.js.map +1 -0
- package/dist/{chunk-XBSWWF6A.js → chunk-QSYPMMWS.js} +2 -2
- package/dist/index.js +14 -623
- package/dist/index.js.map +1 -1
- package/dist/loop-runner-bin.js +2 -2
- package/dist/loops.d.ts +132 -2
- package/dist/loops.js +5 -1
- package/dist/mcp/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-UJW6EVNY.js.map +0 -1
- /package/dist/{chunk-XBSWWF6A.js.map → chunk-QSYPMMWS.js.map} +0 -0
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
definePersona,
|
|
6
6
|
runPersonified,
|
|
7
7
|
worktreeFanout
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-DLAEEF26.js";
|
|
9
9
|
import {
|
|
10
10
|
createExecutorRegistry
|
|
11
11
|
} from "./chunk-XN5TIJGP.js";
|
|
@@ -196,4 +196,4 @@ export {
|
|
|
196
196
|
runLoopRunnerCli,
|
|
197
197
|
parseLoopRunnerArgv
|
|
198
198
|
};
|
|
199
|
-
//# sourceMappingURL=chunk-
|
|
199
|
+
//# sourceMappingURL=chunk-QSYPMMWS.js.map
|
package/dist/index.js
CHANGED
|
@@ -20,9 +20,18 @@ import {
|
|
|
20
20
|
runLoopRunnerCli,
|
|
21
21
|
selfImproveLoopRunner,
|
|
22
22
|
worktreeLoopRunner
|
|
23
|
-
} from "./chunk-
|
|
23
|
+
} from "./chunk-QSYPMMWS.js";
|
|
24
24
|
import "./chunk-SGKPNBXE.js";
|
|
25
|
-
import
|
|
25
|
+
import {
|
|
26
|
+
InMemoryRuntimeSessionStore,
|
|
27
|
+
createIterableBackend,
|
|
28
|
+
createOpenAICompatibleBackend,
|
|
29
|
+
createSandboxPromptBackend,
|
|
30
|
+
newRuntimeSession,
|
|
31
|
+
normalizeBackendStreamEvent,
|
|
32
|
+
nowIso,
|
|
33
|
+
touchSession
|
|
34
|
+
} from "./chunk-DLAEEF26.js";
|
|
26
35
|
import {
|
|
27
36
|
assertModelAllowed,
|
|
28
37
|
composeRuntimeHooks,
|
|
@@ -63,624 +72,6 @@ import {
|
|
|
63
72
|
import "./chunk-DPEUKJRO.js";
|
|
64
73
|
import "./chunk-DGUM43GV.js";
|
|
65
74
|
|
|
66
|
-
// src/sessions.ts
|
|
67
|
-
function newRuntimeSession(backend, requestedId, metadata) {
|
|
68
|
-
const now = nowIso();
|
|
69
|
-
return {
|
|
70
|
-
id: requestedId || crypto.randomUUID(),
|
|
71
|
-
backend,
|
|
72
|
-
status: "active",
|
|
73
|
-
createdAt: now,
|
|
74
|
-
updatedAt: now,
|
|
75
|
-
metadata
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
function touchSession(session) {
|
|
79
|
-
return { ...session, updatedAt: nowIso() };
|
|
80
|
-
}
|
|
81
|
-
function nowIso() {
|
|
82
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
83
|
-
}
|
|
84
|
-
var InMemoryRuntimeSessionStore = class {
|
|
85
|
-
sessions = /* @__PURE__ */ new Map();
|
|
86
|
-
events = /* @__PURE__ */ new Map();
|
|
87
|
-
get(sessionId) {
|
|
88
|
-
return this.sessions.get(sessionId);
|
|
89
|
-
}
|
|
90
|
-
put(session) {
|
|
91
|
-
this.sessions.set(session.id, session);
|
|
92
|
-
}
|
|
93
|
-
appendEvent(sessionId, event) {
|
|
94
|
-
const existing = this.events.get(sessionId) ?? [];
|
|
95
|
-
existing.push(event);
|
|
96
|
-
this.events.set(sessionId, existing);
|
|
97
|
-
}
|
|
98
|
-
listEvents(sessionId) {
|
|
99
|
-
return [...this.events.get(sessionId) ?? []];
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// src/backends.ts
|
|
104
|
-
function createIterableBackend(options) {
|
|
105
|
-
return options;
|
|
106
|
-
}
|
|
107
|
-
function createSandboxPromptBackend(options) {
|
|
108
|
-
const kind = options.kind ?? "sandbox";
|
|
109
|
-
return {
|
|
110
|
-
kind,
|
|
111
|
-
async start(input, context) {
|
|
112
|
-
const box = await options.getBox(input, context);
|
|
113
|
-
return newRuntimeSession(
|
|
114
|
-
kind,
|
|
115
|
-
options.getSessionId?.(box, input) ?? context.requestedSessionId,
|
|
116
|
-
{ resumable: true }
|
|
117
|
-
);
|
|
118
|
-
},
|
|
119
|
-
resume(session) {
|
|
120
|
-
return touchSession({ ...session, status: "active" });
|
|
121
|
-
},
|
|
122
|
-
async *stream(input, context) {
|
|
123
|
-
const box = await options.getBox(input, context);
|
|
124
|
-
const message = input.message ?? input.messages?.at(-1)?.content ?? context.task.intent;
|
|
125
|
-
for await (const event of options.streamPrompt(box, message, context)) {
|
|
126
|
-
const mapped = options.mapEvent?.(event, context) ?? mapCommonBackendEvent(event, context);
|
|
127
|
-
if (mapped) yield mapped;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
var DEFAULT_RETRY_STATUSES = [408, 425, 429, 500, 502, 503, 504];
|
|
133
|
-
function pickRetryDelayMs(attempt, policy) {
|
|
134
|
-
const exp = policy.initialBackoffMs * 2 ** (attempt - 1);
|
|
135
|
-
const capped = Math.min(exp, policy.maxBackoffMs);
|
|
136
|
-
const jitter = capped * policy.jitter * (Math.random() * 2 - 1);
|
|
137
|
-
return Math.max(0, Math.round(capped + jitter));
|
|
138
|
-
}
|
|
139
|
-
function withTimeout(callerSignal, timeoutMs) {
|
|
140
|
-
if (timeoutMs <= 0) {
|
|
141
|
-
return { signal: callerSignal ?? new AbortController().signal, dispose: () => void 0 };
|
|
142
|
-
}
|
|
143
|
-
const controller = new AbortController();
|
|
144
|
-
const timer = setTimeout(
|
|
145
|
-
() => controller.abort(new Error(`request timed out after ${timeoutMs}ms`)),
|
|
146
|
-
timeoutMs
|
|
147
|
-
);
|
|
148
|
-
if (typeof timer.unref === "function") {
|
|
149
|
-
;
|
|
150
|
-
timer.unref();
|
|
151
|
-
}
|
|
152
|
-
const onCallerAbort = () => controller.abort(callerSignal?.reason ?? new Error("aborted"));
|
|
153
|
-
if (callerSignal) {
|
|
154
|
-
if (callerSignal.aborted) onCallerAbort();
|
|
155
|
-
else callerSignal.addEventListener("abort", onCallerAbort, { once: true });
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
signal: controller.signal,
|
|
159
|
-
dispose: () => {
|
|
160
|
-
clearTimeout(timer);
|
|
161
|
-
callerSignal?.removeEventListener("abort", onCallerAbort);
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
function sleep(ms, signal) {
|
|
166
|
-
return new Promise((resolve, reject) => {
|
|
167
|
-
if (signal?.aborted) {
|
|
168
|
-
reject(signal.reason ?? new Error("aborted"));
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
const t = setTimeout(() => {
|
|
172
|
-
signal?.removeEventListener("abort", onAbort);
|
|
173
|
-
resolve();
|
|
174
|
-
}, ms);
|
|
175
|
-
const onAbort = () => {
|
|
176
|
-
clearTimeout(t);
|
|
177
|
-
reject(signal?.reason ?? new Error("aborted"));
|
|
178
|
-
};
|
|
179
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
function createOpenAICompatibleBackend(options) {
|
|
183
|
-
const fetcher = options.fetchImpl ?? fetch;
|
|
184
|
-
const kind = options.kind ?? "tcloud";
|
|
185
|
-
const retryPolicy = {
|
|
186
|
-
maxAttempts: options.retry?.maxAttempts ?? 5,
|
|
187
|
-
initialBackoffMs: options.retry?.initialBackoffMs ?? 1e3,
|
|
188
|
-
maxBackoffMs: options.retry?.maxBackoffMs ?? 3e4,
|
|
189
|
-
jitter: options.retry?.jitter ?? 0.25,
|
|
190
|
-
retryStatuses: options.retry?.retryStatuses ?? DEFAULT_RETRY_STATUSES,
|
|
191
|
-
requestTimeoutMs: options.retry?.requestTimeoutMs ?? 12e4
|
|
192
|
-
};
|
|
193
|
-
return {
|
|
194
|
-
kind,
|
|
195
|
-
start(_input, context) {
|
|
196
|
-
return newRuntimeSession(kind, context.requestedSessionId);
|
|
197
|
-
},
|
|
198
|
-
async *stream(input, context) {
|
|
199
|
-
const url = `${options.baseUrl.replace(/\/$/, "")}/chat/completions`;
|
|
200
|
-
const bodyPayload = {
|
|
201
|
-
model: options.model,
|
|
202
|
-
stream: true,
|
|
203
|
-
stream_options: { include_usage: true },
|
|
204
|
-
messages: input.messages ?? [
|
|
205
|
-
{ role: "user", content: input.message ?? context.task.intent }
|
|
206
|
-
]
|
|
207
|
-
};
|
|
208
|
-
if (options.tools && options.tools.length > 0) {
|
|
209
|
-
bodyPayload.tools = options.tools;
|
|
210
|
-
if (options.toolChoice !== void 0) bodyPayload.tool_choice = options.toolChoice;
|
|
211
|
-
}
|
|
212
|
-
if (options.responseFormat !== void 0) {
|
|
213
|
-
bodyPayload.response_format = options.responseFormat;
|
|
214
|
-
}
|
|
215
|
-
const requestBody = JSON.stringify(bodyPayload);
|
|
216
|
-
let response;
|
|
217
|
-
let lastStatus = 0;
|
|
218
|
-
let lastThrown;
|
|
219
|
-
for (let attempt = 1; attempt <= retryPolicy.maxAttempts; attempt++) {
|
|
220
|
-
lastThrown = void 0;
|
|
221
|
-
const attemptSignal = withTimeout(context.signal, retryPolicy.requestTimeoutMs);
|
|
222
|
-
try {
|
|
223
|
-
response = await fetcher(url, {
|
|
224
|
-
method: "POST",
|
|
225
|
-
headers: {
|
|
226
|
-
Authorization: `Bearer ${options.apiKey}`,
|
|
227
|
-
"Content-Type": "application/json",
|
|
228
|
-
// Cross-gateway forwarding: when this call is part of a
|
|
229
|
-
// multi-agent conversation, the runner stamps run/turn/
|
|
230
|
-
// depth/forwarded-auth headers onto the context. They flow
|
|
231
|
-
// through to the downstream gateway verbatim so the original
|
|
232
|
-
// user gets billed, the recursion depth stays bounded, and
|
|
233
|
-
// the trace correlates across hops.
|
|
234
|
-
...context.propagatedHeaders ?? {}
|
|
235
|
-
},
|
|
236
|
-
body: requestBody,
|
|
237
|
-
signal: attemptSignal.signal
|
|
238
|
-
});
|
|
239
|
-
} catch (err) {
|
|
240
|
-
attemptSignal.dispose();
|
|
241
|
-
if (context.signal?.aborted) throw err;
|
|
242
|
-
lastThrown = err;
|
|
243
|
-
response = void 0;
|
|
244
|
-
if (attempt === retryPolicy.maxAttempts) break;
|
|
245
|
-
await sleep(pickRetryDelayMs(attempt, retryPolicy), context.signal);
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
attemptSignal.dispose();
|
|
249
|
-
if (response.ok) break;
|
|
250
|
-
lastStatus = response.status;
|
|
251
|
-
if (!retryPolicy.retryStatuses.includes(response.status)) break;
|
|
252
|
-
if (attempt === retryPolicy.maxAttempts) break;
|
|
253
|
-
try {
|
|
254
|
-
await response.body?.cancel();
|
|
255
|
-
} catch {
|
|
256
|
-
}
|
|
257
|
-
const delayMs = pickRetryDelayMs(attempt, retryPolicy);
|
|
258
|
-
await sleep(delayMs, context.signal);
|
|
259
|
-
}
|
|
260
|
-
if (!response) {
|
|
261
|
-
const reason = lastThrown instanceof Error ? lastThrown.message : String(lastThrown);
|
|
262
|
-
throw new BackendTransportError(
|
|
263
|
-
kind,
|
|
264
|
-
`chat backend unreachable after ${retryPolicy.maxAttempts} attempts: ${reason}`,
|
|
265
|
-
{ status: 0 }
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
if (!response.ok) {
|
|
269
|
-
let body;
|
|
270
|
-
try {
|
|
271
|
-
const raw = await response.text();
|
|
272
|
-
body = raw.length > MAX_ERROR_BODY_BYTES ? `${raw.slice(0, MAX_ERROR_BODY_BYTES)}\u2026` : raw;
|
|
273
|
-
} catch {
|
|
274
|
-
body = void 0;
|
|
275
|
-
}
|
|
276
|
-
throw new BackendTransportError(kind, `chat backend returned ${lastStatus || "unknown"}`, {
|
|
277
|
-
status: lastStatus || 0,
|
|
278
|
-
body
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
yield* streamResponseEvents(response, context, options.model);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
var MAX_ERROR_BODY_BYTES = 2048;
|
|
286
|
-
function normalizeBackendStreamEvent(event, task, session) {
|
|
287
|
-
if ("task" in event && event.task && "session" in event && event.session && "timestamp" in event && event.timestamp) {
|
|
288
|
-
return event;
|
|
289
|
-
}
|
|
290
|
-
return {
|
|
291
|
-
...event,
|
|
292
|
-
task: "task" in event && event.task ? event.task : task,
|
|
293
|
-
session: "session" in event && event.session ? event.session : session,
|
|
294
|
-
timestamp: "timestamp" in event && event.timestamp ? event.timestamp : nowIso()
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
function mapCommonBackendEvent(event, context) {
|
|
298
|
-
if (!event || typeof event !== "object") return void 0;
|
|
299
|
-
const record = event;
|
|
300
|
-
const type = String(record.type ?? "");
|
|
301
|
-
const data = record.data && typeof record.data === "object" ? record.data : record;
|
|
302
|
-
if (type === "message.part.updated" || type === "text_delta" || type === "delta") {
|
|
303
|
-
const part = data.part;
|
|
304
|
-
const partText = part !== void 0 && typeof part === "object" && (part.type === "text" || part.type === void 0) ? stringValue(part.text) : void 0;
|
|
305
|
-
const text = stringValue(data.text) ?? stringValue(data.delta) ?? stringValue(record.text) ?? partText;
|
|
306
|
-
return text ? {
|
|
307
|
-
type: "text_delta",
|
|
308
|
-
task: context.task,
|
|
309
|
-
session: context.session,
|
|
310
|
-
text,
|
|
311
|
-
timestamp: nowIso()
|
|
312
|
-
} : void 0;
|
|
313
|
-
}
|
|
314
|
-
if (type === "reasoning_delta") {
|
|
315
|
-
const text = stringValue(data.text) ?? stringValue(record.text);
|
|
316
|
-
return text ? {
|
|
317
|
-
type: "reasoning_delta",
|
|
318
|
-
task: context.task,
|
|
319
|
-
session: context.session,
|
|
320
|
-
text,
|
|
321
|
-
timestamp: nowIso()
|
|
322
|
-
} : void 0;
|
|
323
|
-
}
|
|
324
|
-
if (type === "tool_call") {
|
|
325
|
-
return {
|
|
326
|
-
type: "tool_call",
|
|
327
|
-
task: context.task,
|
|
328
|
-
session: context.session,
|
|
329
|
-
toolName: stringValue(data.name) ?? stringValue(record.toolName) ?? "tool",
|
|
330
|
-
toolCallId: stringValue(data.id) ?? stringValue(record.toolCallId),
|
|
331
|
-
args: data.args ?? data.input ?? record.args,
|
|
332
|
-
timestamp: nowIso()
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
if (type === "tool_result") {
|
|
336
|
-
return {
|
|
337
|
-
type: "tool_result",
|
|
338
|
-
task: context.task,
|
|
339
|
-
session: context.session,
|
|
340
|
-
toolName: stringValue(data.name) ?? stringValue(record.toolName) ?? "tool",
|
|
341
|
-
toolCallId: stringValue(data.id) ?? stringValue(record.toolCallId),
|
|
342
|
-
result: data.result ?? data.output ?? record.result,
|
|
343
|
-
timestamp: nowIso()
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
if (type === "artifact") {
|
|
347
|
-
const artifactId = stringValue(data.artifactId) ?? stringValue(data.id) ?? stringValue(record.artifactId);
|
|
348
|
-
if (!artifactId) return void 0;
|
|
349
|
-
return {
|
|
350
|
-
type: "artifact",
|
|
351
|
-
task: context.task,
|
|
352
|
-
session: context.session,
|
|
353
|
-
artifactId,
|
|
354
|
-
name: stringValue(data.name) ?? stringValue(record.name),
|
|
355
|
-
mimeType: stringValue(data.mimeType) ?? stringValue(record.mimeType),
|
|
356
|
-
uri: stringValue(data.uri) ?? stringValue(record.uri),
|
|
357
|
-
content: stringValue(data.content) ?? stringValue(data.body) ?? stringValue(record.content),
|
|
358
|
-
metadata: data.metadata && typeof data.metadata === "object" ? data.metadata : void 0,
|
|
359
|
-
timestamp: nowIso()
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
if (type === "proposal_created" || type === "proposal" || type === "filing") {
|
|
363
|
-
const proposalId = stringValue(data.proposalId) ?? stringValue(data.id) ?? stringValue(record.proposalId);
|
|
364
|
-
if (!proposalId) return void 0;
|
|
365
|
-
const status = stringValue(data.status) ?? stringValue(record.status);
|
|
366
|
-
return {
|
|
367
|
-
type: "proposal_created",
|
|
368
|
-
task: context.task,
|
|
369
|
-
session: context.session,
|
|
370
|
-
proposalId,
|
|
371
|
-
title: stringValue(data.title) ?? stringValue(record.title) ?? proposalId,
|
|
372
|
-
status: status === "pending" || status === "approved" || status === "rejected" ? status : void 0,
|
|
373
|
-
content: stringValue(data.content) ?? stringValue(data.body) ?? stringValue(record.content),
|
|
374
|
-
timestamp: nowIso()
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
if (type === "result" || type === "final") {
|
|
378
|
-
const text = stringValue(data.finalText) ?? stringValue(data.text) ?? stringValue(record.text);
|
|
379
|
-
return text ? {
|
|
380
|
-
type: "text_delta",
|
|
381
|
-
task: context.task,
|
|
382
|
-
session: context.session,
|
|
383
|
-
text,
|
|
384
|
-
timestamp: nowIso()
|
|
385
|
-
} : void 0;
|
|
386
|
-
}
|
|
387
|
-
return void 0;
|
|
388
|
-
}
|
|
389
|
-
async function* streamResponseEvents(response, context, requestedModel) {
|
|
390
|
-
const body = response.body;
|
|
391
|
-
if (!body) return;
|
|
392
|
-
const reader = body.getReader();
|
|
393
|
-
const decoder = new TextDecoder();
|
|
394
|
-
let buffer = "";
|
|
395
|
-
const usage = { saw: false };
|
|
396
|
-
const toolCalls = /* @__PURE__ */ new Map();
|
|
397
|
-
const startedAt = Date.now();
|
|
398
|
-
for (; ; ) {
|
|
399
|
-
const { done, value } = await reader.read();
|
|
400
|
-
if (done) break;
|
|
401
|
-
buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n");
|
|
402
|
-
for (const event of drainStreamBuffer(false)) yield event;
|
|
403
|
-
}
|
|
404
|
-
buffer += decoder.decode().replace(/\r\n/g, "\n");
|
|
405
|
-
for (const event of drainStreamBuffer(true)) yield event;
|
|
406
|
-
if (buffer.trim()) {
|
|
407
|
-
for (const event of parseStreamChunk(buffer, context, usage, toolCalls)) yield event;
|
|
408
|
-
}
|
|
409
|
-
for (const event of flushPendingToolCalls(toolCalls, context)) yield event;
|
|
410
|
-
if (usage.saw) {
|
|
411
|
-
yield {
|
|
412
|
-
type: "llm_call",
|
|
413
|
-
task: context.task,
|
|
414
|
-
session: context.session,
|
|
415
|
-
model: usage.model ?? requestedModel,
|
|
416
|
-
tokensIn: usage.tokensIn,
|
|
417
|
-
tokensOut: usage.tokensOut,
|
|
418
|
-
// `costUsd` is intentionally absent — pricing tables live in consumers
|
|
419
|
-
// (agent-eval's `estimateCost`, MetricsCollector). Emitting a wrong
|
|
420
|
-
// number here is worse than emitting none.
|
|
421
|
-
latencyMs: Date.now() - startedAt,
|
|
422
|
-
finishReason: usage.finishReason,
|
|
423
|
-
timestamp: nowIso()
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
function* drainStreamBuffer(flush) {
|
|
427
|
-
for (; ; ) {
|
|
428
|
-
const sseBoundary = buffer.indexOf("\n\n");
|
|
429
|
-
if (sseBoundary >= 0) {
|
|
430
|
-
const chunk = buffer.slice(0, sseBoundary);
|
|
431
|
-
buffer = buffer.slice(sseBoundary + 2);
|
|
432
|
-
for (const event of parseStreamChunk(chunk, context, usage, toolCalls)) yield event;
|
|
433
|
-
continue;
|
|
434
|
-
}
|
|
435
|
-
const newline = buffer.indexOf("\n");
|
|
436
|
-
if (newline >= 0 && !buffer.slice(0, newline).startsWith("data:")) {
|
|
437
|
-
const line = buffer.slice(0, newline);
|
|
438
|
-
buffer = buffer.slice(newline + 1);
|
|
439
|
-
for (const event of parseStreamChunk(line, context, usage, toolCalls)) yield event;
|
|
440
|
-
continue;
|
|
441
|
-
}
|
|
442
|
-
if (flush && buffer.trim() && !buffer.trimStart().startsWith("data:")) {
|
|
443
|
-
const line = buffer;
|
|
444
|
-
buffer = "";
|
|
445
|
-
for (const event of parseStreamChunk(line, context, usage, toolCalls)) yield event;
|
|
446
|
-
continue;
|
|
447
|
-
}
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
function* parseStreamChunk(chunk, context, usage, toolCalls) {
|
|
453
|
-
const lines = chunk.split(/\r?\n/);
|
|
454
|
-
const dataLines = lines.filter((line) => line.startsWith("data:"));
|
|
455
|
-
if (dataLines.length === 0 && lines.every((line) => {
|
|
456
|
-
const trimmed = line.trim();
|
|
457
|
-
return trimmed.length === 0 || trimmed.startsWith(":");
|
|
458
|
-
})) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
const data = dataLines.length > 0 ? dataLines.map((line) => line.slice(5).trimStart()).join("\n") : chunk.trim();
|
|
462
|
-
if (!data || data === "[DONE]") return;
|
|
463
|
-
let parsed;
|
|
464
|
-
try {
|
|
465
|
-
parsed = JSON.parse(data);
|
|
466
|
-
} catch {
|
|
467
|
-
yield {
|
|
468
|
-
type: "text_delta",
|
|
469
|
-
task: context.task,
|
|
470
|
-
session: context.session,
|
|
471
|
-
text: data,
|
|
472
|
-
timestamp: nowIso()
|
|
473
|
-
};
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
captureStreamUsage(parsed, usage);
|
|
477
|
-
const choices = parsed.choices;
|
|
478
|
-
const choice = Array.isArray(choices) ? choices[0] : void 0;
|
|
479
|
-
const delta = choice?.delta;
|
|
480
|
-
const message = choice?.message;
|
|
481
|
-
const deltaToolCalls = delta?.tool_calls;
|
|
482
|
-
if (Array.isArray(deltaToolCalls)) {
|
|
483
|
-
for (const tc of deltaToolCalls) {
|
|
484
|
-
if (!tc || typeof tc !== "object") continue;
|
|
485
|
-
const rec = tc;
|
|
486
|
-
const idx = numberValue(rec.index) ?? 0;
|
|
487
|
-
const key = `openai:${idx}`;
|
|
488
|
-
const acc = toolCalls.get(key) ?? { argsRaw: "", source: "openai", finalized: false };
|
|
489
|
-
const id = stringValue(rec.id);
|
|
490
|
-
if (id) acc.id = id;
|
|
491
|
-
const fn = rec.function;
|
|
492
|
-
const name = stringValue(fn?.name);
|
|
493
|
-
if (name) acc.name = name;
|
|
494
|
-
const args = stringValue(fn?.arguments);
|
|
495
|
-
if (args) acc.argsRaw += args;
|
|
496
|
-
toolCalls.set(key, acc);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
const messageToolCalls = message?.tool_calls;
|
|
500
|
-
if (Array.isArray(messageToolCalls)) {
|
|
501
|
-
for (const tc of messageToolCalls) {
|
|
502
|
-
if (!tc || typeof tc !== "object") continue;
|
|
503
|
-
const rec = tc;
|
|
504
|
-
const fn = rec.function;
|
|
505
|
-
const idx = numberValue(rec.index) ?? messageToolCalls.indexOf(tc);
|
|
506
|
-
const key = `openai:${idx}`;
|
|
507
|
-
const acc = toolCalls.get(key) ?? { argsRaw: "", source: "openai", finalized: false };
|
|
508
|
-
const id = stringValue(rec.id);
|
|
509
|
-
if (id) acc.id = id;
|
|
510
|
-
const name = stringValue(fn?.name);
|
|
511
|
-
if (name) acc.name = name;
|
|
512
|
-
const args = stringValue(fn?.arguments);
|
|
513
|
-
if (args) acc.argsRaw += args;
|
|
514
|
-
acc.finalized = true;
|
|
515
|
-
toolCalls.set(key, acc);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
const finishReason = stringValue(choice?.finish_reason);
|
|
519
|
-
if (finishReason === "tool_calls") {
|
|
520
|
-
for (const [key, acc] of toolCalls) {
|
|
521
|
-
if (acc.source === "openai" && !acc.finalized) acc.finalized = true;
|
|
522
|
-
toolCalls.set(key, acc);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
const eventType = stringValue(parsed.type);
|
|
526
|
-
if (eventType === "content_block_start") {
|
|
527
|
-
const block = parsed.content_block;
|
|
528
|
-
if (block && stringValue(block.type) === "tool_use") {
|
|
529
|
-
const idx = numberValue(parsed.index) ?? 0;
|
|
530
|
-
const key = `anthropic:${idx}`;
|
|
531
|
-
toolCalls.set(key, {
|
|
532
|
-
id: stringValue(block.id),
|
|
533
|
-
name: stringValue(block.name),
|
|
534
|
-
argsRaw: "",
|
|
535
|
-
source: "anthropic",
|
|
536
|
-
finalized: false
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
if (eventType === "content_block_delta") {
|
|
541
|
-
const d = parsed.delta;
|
|
542
|
-
const dType = stringValue(d?.type);
|
|
543
|
-
if (dType === "input_json_delta") {
|
|
544
|
-
const idx = numberValue(parsed.index) ?? 0;
|
|
545
|
-
const key = `anthropic:${idx}`;
|
|
546
|
-
const acc = toolCalls.get(key);
|
|
547
|
-
if (acc) {
|
|
548
|
-
const partial = stringValue(d?.partial_json) ?? "";
|
|
549
|
-
acc.argsRaw += partial;
|
|
550
|
-
toolCalls.set(key, acc);
|
|
551
|
-
}
|
|
552
|
-
} else {
|
|
553
|
-
const text2 = stringValue(d?.text);
|
|
554
|
-
if (text2) {
|
|
555
|
-
yield {
|
|
556
|
-
type: "text_delta",
|
|
557
|
-
task: context.task,
|
|
558
|
-
session: context.session,
|
|
559
|
-
text: text2,
|
|
560
|
-
timestamp: nowIso()
|
|
561
|
-
};
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
if (eventType === "content_block_stop") {
|
|
566
|
-
const idx = numberValue(parsed.index) ?? 0;
|
|
567
|
-
const key = `anthropic:${idx}`;
|
|
568
|
-
const acc = toolCalls.get(key);
|
|
569
|
-
if (acc) {
|
|
570
|
-
acc.finalized = true;
|
|
571
|
-
toolCalls.set(key, acc);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
for (const event of drainFinalizedToolCalls(toolCalls, context)) yield event;
|
|
575
|
-
const text = stringValue(delta?.content) ?? stringValue(message?.content) ?? stringValue(parsed.text);
|
|
576
|
-
if (text) {
|
|
577
|
-
yield {
|
|
578
|
-
type: "text_delta",
|
|
579
|
-
task: context.task,
|
|
580
|
-
session: context.session,
|
|
581
|
-
text,
|
|
582
|
-
timestamp: nowIso()
|
|
583
|
-
};
|
|
584
|
-
return;
|
|
585
|
-
}
|
|
586
|
-
const mapped = mapCommonBackendEvent(parsed, context);
|
|
587
|
-
if (mapped) yield mapped;
|
|
588
|
-
}
|
|
589
|
-
function* drainFinalizedToolCalls(toolCalls, context) {
|
|
590
|
-
for (const [key, acc] of toolCalls) {
|
|
591
|
-
if (!acc.finalized) continue;
|
|
592
|
-
toolCalls.delete(key);
|
|
593
|
-
yield buildToolCallEvent(acc, context);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
function* flushPendingToolCalls(toolCalls, context) {
|
|
597
|
-
for (const [key, acc] of toolCalls) {
|
|
598
|
-
toolCalls.delete(key);
|
|
599
|
-
yield buildToolCallEvent(acc, context);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
function buildToolCallEvent(acc, context) {
|
|
603
|
-
let args = acc.argsRaw;
|
|
604
|
-
if (acc.argsRaw.length > 0) {
|
|
605
|
-
try {
|
|
606
|
-
args = JSON.parse(acc.argsRaw);
|
|
607
|
-
} catch {
|
|
608
|
-
args = acc.argsRaw;
|
|
609
|
-
}
|
|
610
|
-
} else {
|
|
611
|
-
args = {};
|
|
612
|
-
}
|
|
613
|
-
return {
|
|
614
|
-
type: "tool_call",
|
|
615
|
-
task: context.task,
|
|
616
|
-
session: context.session,
|
|
617
|
-
toolName: acc.name ?? "tool",
|
|
618
|
-
toolCallId: acc.id,
|
|
619
|
-
args,
|
|
620
|
-
timestamp: nowIso()
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
function captureStreamUsage(parsed, usage) {
|
|
624
|
-
const model = stringValue(parsed.model);
|
|
625
|
-
if (model && !usage.model) usage.model = model;
|
|
626
|
-
const openAiUsage = parsed.usage;
|
|
627
|
-
if (openAiUsage && typeof openAiUsage === "object") {
|
|
628
|
-
const promptTokens = numberValue(openAiUsage.prompt_tokens);
|
|
629
|
-
const completionTokens = numberValue(openAiUsage.completion_tokens);
|
|
630
|
-
const inputTokens = numberValue(openAiUsage.input_tokens);
|
|
631
|
-
const outputTokens = numberValue(openAiUsage.output_tokens);
|
|
632
|
-
if (promptTokens !== void 0) {
|
|
633
|
-
usage.tokensIn = promptTokens;
|
|
634
|
-
usage.saw = true;
|
|
635
|
-
} else if (inputTokens !== void 0) {
|
|
636
|
-
usage.tokensIn = (usage.tokensIn ?? 0) + inputTokens;
|
|
637
|
-
usage.saw = true;
|
|
638
|
-
}
|
|
639
|
-
if (completionTokens !== void 0) {
|
|
640
|
-
usage.tokensOut = completionTokens;
|
|
641
|
-
usage.saw = true;
|
|
642
|
-
} else if (outputTokens !== void 0) {
|
|
643
|
-
usage.tokensOut = (usage.tokensOut ?? 0) + outputTokens;
|
|
644
|
-
usage.saw = true;
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
const type = stringValue(parsed.type);
|
|
648
|
-
if (type === "message_start") {
|
|
649
|
-
const message = parsed.message;
|
|
650
|
-
const messageModel = stringValue(message?.model);
|
|
651
|
-
if (messageModel && !usage.model) usage.model = messageModel;
|
|
652
|
-
const messageUsage = message?.usage;
|
|
653
|
-
const inputTokens = numberValue(messageUsage?.input_tokens);
|
|
654
|
-
if (inputTokens !== void 0) {
|
|
655
|
-
usage.tokensIn = inputTokens;
|
|
656
|
-
usage.saw = true;
|
|
657
|
-
}
|
|
658
|
-
const outputTokens = numberValue(messageUsage?.output_tokens);
|
|
659
|
-
if (outputTokens !== void 0) {
|
|
660
|
-
usage.tokensOut = (usage.tokensOut ?? 0) + outputTokens;
|
|
661
|
-
usage.saw = true;
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
if (type === "message_delta") {
|
|
665
|
-
const delta = parsed.delta;
|
|
666
|
-
const stopReason = stringValue(delta?.stop_reason);
|
|
667
|
-
if (stopReason) usage.finishReason = stopReason;
|
|
668
|
-
}
|
|
669
|
-
const choices = parsed.choices;
|
|
670
|
-
if (Array.isArray(choices)) {
|
|
671
|
-
const finishReason = stringValue(
|
|
672
|
-
choices[0]?.finish_reason
|
|
673
|
-
);
|
|
674
|
-
if (finishReason) usage.finishReason = finishReason;
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
function numberValue(value) {
|
|
678
|
-
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
679
|
-
}
|
|
680
|
-
function stringValue(value) {
|
|
681
|
-
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
75
|
// src/conversation/call-policy.ts
|
|
685
76
|
var CircuitOpenError = class extends Error {
|
|
686
77
|
constructor(participant, retryAfterMs) {
|
|
@@ -779,7 +170,7 @@ function computeBackoff(spec, attempt) {
|
|
|
779
170
|
if (typeof spec === "function") return Math.max(0, spec(attempt));
|
|
780
171
|
return Math.max(0, spec);
|
|
781
172
|
}
|
|
782
|
-
function
|
|
173
|
+
function sleep(ms) {
|
|
783
174
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
784
175
|
}
|
|
785
176
|
|
|
@@ -1069,7 +460,7 @@ async function* runConversationStream(conversation, options) {
|
|
|
1069
460
|
if (attempt >= totalAttempts || !isRetryable(lastError)) {
|
|
1070
461
|
break;
|
|
1071
462
|
}
|
|
1072
|
-
await
|
|
463
|
+
await sleep(computeBackoff(callPolicy?.retryBackoffMs, attempt));
|
|
1073
464
|
}
|
|
1074
465
|
}
|
|
1075
466
|
if (!aggregator) {
|
|
@@ -3558,7 +2949,7 @@ export {
|
|
|
3558
2949
|
sanitizeKnowledgeReadinessReport,
|
|
3559
2950
|
sanitizeRuntimeStreamEvent,
|
|
3560
2951
|
selfImproveLoopRunner,
|
|
3561
|
-
|
|
2952
|
+
sleep,
|
|
3562
2953
|
slugifySpeaker,
|
|
3563
2954
|
startRuntimeRun,
|
|
3564
2955
|
streamToolLoop,
|