@compose-market/sdk 0.8.3 → 0.8.5
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/.speakeasy/a2a.arazzo.yaml +1 -1
- package/.speakeasy/memory.arazzo.yaml +1 -1
- package/.speakeasy/tests.arazzo.yaml +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/resources/agent.d.ts +10 -22
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/resources/agent.js +201 -523
- package/dist/resources/agent.js.map +1 -1
- package/dist/resources/workflow.d.ts +11 -17
- package/dist/resources/workflow.d.ts.map +1 -1
- package/dist/resources/workflow.js +94 -174
- package/dist/resources/workflow.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/generated/inference/esm/lib/config.d.ts +3 -3
- package/generated/inference/esm/lib/config.js +3 -3
- package/generated/inference/package.json +1 -1
- package/generated/inference/src/lib/config.ts +3 -3
- package/generated/manowar/esm/lib/config.d.ts +3 -3
- package/generated/manowar/esm/lib/config.js +3 -3
- package/generated/manowar/package.json +1 -1
- package/generated/manowar/src/lib/config.ts +3 -3
- package/generated/memory/esm/lib/config.d.ts +3 -3
- package/generated/memory/esm/lib/config.js +3 -3
- package/generated/memory/package.json +1 -1
- package/generated/memory/src/lib/config.ts +3 -3
- package/generated/x402/esm/lib/config.d.ts +3 -3
- package/generated/x402/esm/lib/config.js +3 -3
- package/generated/x402/package.json +1 -1
- package/generated/x402/src/lib/config.ts +3 -3
- package/package.json +5 -5
- package/specs/inference.openapi.yaml +1 -1
- package/specs/manowar.openapi.yaml +1 -1
- package/specs/memory.openapi.yaml +1 -1
- package/specs/x402.openapi.yaml +1 -1
package/dist/resources/agent.js
CHANGED
|
@@ -1,28 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Compose agent runtime stream resource.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* (
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* execution lifecycle. This resource keeps those domains separate.
|
|
4
|
+
* SDK responsibilities ONLY:
|
|
5
|
+
* - HTTP request with payment/auth/abort
|
|
6
|
+
* - Response header extraction (budget, receipt, session)
|
|
7
|
+
* - EventBus emission (toolCallStart/End, childAgent*, planProposed, etc.)
|
|
8
|
+
* - Text accumulation for final result
|
|
9
|
+
* - Stream lifecycle (agentStreamStart/End)
|
|
11
10
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* runtime tool vocabulary fires, so UI strips can react uniformly
|
|
15
|
-
* regardless of whether the source is agent, workflow, or chat.
|
|
16
|
-
* - `sdk.events.agentStreamStart` / `agentStreamEnd` — lifecycle notifications.
|
|
17
|
-
* - `instrumentBillableResponse` — budget/receipt/sessionInvalid surfaced
|
|
18
|
-
* exactly as on every other billable SDK call.
|
|
11
|
+
* All SSE protocol, decoding, and event type routing is delegated to
|
|
12
|
+
* @compose-market/core/sse/stream — the single source of truth.
|
|
19
13
|
*/
|
|
20
14
|
import { Error } from "../errors.js";
|
|
21
|
-
import {
|
|
22
|
-
import { extractReceiptFromResponse
|
|
15
|
+
import { streamEvents } from "@compose-market/core/sse/stream";
|
|
16
|
+
import { extractReceiptFromResponse } from "@compose-market/core/sse/receipt";
|
|
23
17
|
import { extractSessionBudgetFromResponse } from "@compose-market/core/sse/budget";
|
|
24
|
-
import { decode as decodeActivityEvent } from "@compose-market/core/activity";
|
|
25
|
-
import { decode as decodeModelEvent } from "@compose-market/core/model";
|
|
26
18
|
import { buildCallHeaders, StreamIterator, requestResponseWithPayment, } from "./inference.js";
|
|
27
19
|
export class AgentResource {
|
|
28
20
|
ctx;
|
|
@@ -32,11 +24,6 @@ export class AgentResource {
|
|
|
32
24
|
stream(params, options = {}) {
|
|
33
25
|
return new StreamIterator(driveAgentStream(this.ctx, params, options));
|
|
34
26
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Abort an in-flight stream for (agentWallet, runId). Conversation/CoT/memory
|
|
37
|
-
* for the thread are preserved by the LangGraph checkpoint and can be resumed
|
|
38
|
-
* by issuing a new stream call with the same threadId.
|
|
39
|
-
*/
|
|
40
27
|
async stop(params) {
|
|
41
28
|
const wallet = this.ctx.getWalletMaybe();
|
|
42
29
|
const token = this.ctx.getTokenMaybe();
|
|
@@ -74,7 +61,7 @@ export class AgentResource {
|
|
|
74
61
|
if (wallet.chainId !== null)
|
|
75
62
|
headers["x-chain-id"] = String(wallet.chainId);
|
|
76
63
|
const path = `/agent/${encodeURIComponent(params.agentWallet)}/runs/${encodeURIComponent(params.runId)}/approval`;
|
|
77
|
-
|
|
64
|
+
const response = await this.ctx.http.request({
|
|
78
65
|
method: "POST",
|
|
79
66
|
path,
|
|
80
67
|
headers,
|
|
@@ -84,12 +71,16 @@ export class AgentResource {
|
|
|
84
71
|
decision: params.decision,
|
|
85
72
|
...(params.approvalId ? { approvalId: params.approvalId } : {}),
|
|
86
73
|
...(params.approver ? { approver: params.approver } : {}),
|
|
87
|
-
...(params.reason ? { reason: params.reason } : {}),
|
|
88
74
|
...(params.feedback ? { feedback: params.feedback } : {}),
|
|
75
|
+
...(params.reason ? { reason: params.reason } : {}),
|
|
89
76
|
},
|
|
90
|
-
});
|
|
77
|
+
}).withResponse();
|
|
78
|
+
return { approved: Boolean(response.data?.approved) };
|
|
91
79
|
}
|
|
92
80
|
}
|
|
81
|
+
// =============================================================================
|
|
82
|
+
// Stream Driver — HTTP + EventBus + text accumulation only
|
|
83
|
+
// =============================================================================
|
|
93
84
|
async function* driveAgentStream(ctx, params, options) {
|
|
94
85
|
const path = `/agent/${encodeURIComponent(params.agentWallet)}/stream`;
|
|
95
86
|
const wallet = ctx.getWalletMaybe();
|
|
@@ -203,285 +194,32 @@ async function* driveAgentStream(ctx, params, options) {
|
|
|
203
194
|
}
|
|
204
195
|
let text = "";
|
|
205
196
|
let streamReceipt = null;
|
|
206
|
-
let emittedDone = false;
|
|
207
|
-
const postDoneTimeoutMs = options.paymentMode === "x402" ? 90_000 : 250;
|
|
208
197
|
const toolCalls = [];
|
|
209
|
-
const
|
|
210
|
-
|
|
198
|
+
const stream = streamEvents(response.body, {
|
|
199
|
+
runId: params.runId,
|
|
200
|
+
signal: options.signal,
|
|
201
|
+
});
|
|
211
202
|
try {
|
|
212
|
-
|
|
203
|
+
let result;
|
|
213
204
|
while (true) {
|
|
214
|
-
const next = await
|
|
215
|
-
if (next.done)
|
|
205
|
+
const next = await stream.next();
|
|
206
|
+
if (next.done) {
|
|
207
|
+
result = next.value;
|
|
216
208
|
break;
|
|
217
|
-
const frame = next.value;
|
|
218
|
-
if (frame.data === "[DONE]") {
|
|
219
|
-
if (!emittedDone) {
|
|
220
|
-
emittedDone = true;
|
|
221
|
-
}
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
if (isReceiptEvent(frame.event)) {
|
|
225
|
-
try {
|
|
226
|
-
streamReceipt = parseReceiptEvent(frame.data);
|
|
227
|
-
ctx.events.emit("receipt", {
|
|
228
|
-
userAddress: wallet.address,
|
|
229
|
-
chainId: wallet.chainId,
|
|
230
|
-
receipt: streamReceipt,
|
|
231
|
-
requestId,
|
|
232
|
-
source: "stream",
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
catch { /* skip malformed */ }
|
|
236
|
-
if (emittedDone)
|
|
237
|
-
break;
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
if (frame.event === "compose.error") {
|
|
241
|
-
const decoded = decodeActivityEvent(frame, decodeOptions)
|
|
242
|
-
?? decodeActivityEvent({ type: "error", message: frame.data }, decodeOptions);
|
|
243
|
-
if (decoded)
|
|
244
|
-
yield decoded;
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
if (!frame.data)
|
|
248
|
-
continue;
|
|
249
|
-
if (emittedDone)
|
|
250
|
-
continue;
|
|
251
|
-
let payload = null;
|
|
252
|
-
try {
|
|
253
|
-
payload = JSON.parse(frame.data);
|
|
254
|
-
}
|
|
255
|
-
catch {
|
|
256
|
-
const delta = frame.data;
|
|
257
|
-
text += delta;
|
|
258
|
-
const decoded = decodeModelEvent(delta, decodeOptions);
|
|
259
|
-
if (decoded)
|
|
260
|
-
yield decoded;
|
|
261
|
-
continue;
|
|
262
|
-
}
|
|
263
|
-
if (payload.domain === "model") {
|
|
264
|
-
const decoded = decodeModelEvent(payload, decodeOptions);
|
|
265
|
-
if (decoded) {
|
|
266
|
-
if (decoded.type === "model.text.delta" && decoded.delta)
|
|
267
|
-
text += decoded.delta;
|
|
268
|
-
if (decoded.type === "model.text.done" && decoded.text && !text)
|
|
269
|
-
text = decoded.text;
|
|
270
|
-
yield decoded;
|
|
271
|
-
}
|
|
272
|
-
continue;
|
|
273
|
-
}
|
|
274
|
-
if (payload.domain === "activity") {
|
|
275
|
-
const decoded = decodeActivityEvent(payload, decodeOptions);
|
|
276
|
-
if (decoded)
|
|
277
|
-
yield decoded;
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
// OpenAI chat.completion.chunk passthrough — runtime forwards these
|
|
281
|
-
// for streamed assistant text.
|
|
282
|
-
const choices = Array.isArray(payload.choices) ? payload.choices : null;
|
|
283
|
-
if (choices && choices.length > 0) {
|
|
284
|
-
const delta = choices[0].delta;
|
|
285
|
-
const reasoningChunk = typeof delta?.reasoning_content === "string"
|
|
286
|
-
? delta.reasoning_content
|
|
287
|
-
: null;
|
|
288
|
-
if (reasoningChunk) {
|
|
289
|
-
const decoded = decodeModelEvent({ type: "reasoning-delta", delta: reasoningChunk }, decodeOptions);
|
|
290
|
-
if (decoded)
|
|
291
|
-
yield decoded;
|
|
292
|
-
}
|
|
293
|
-
const streamedChunk = typeof delta?.content === "string"
|
|
294
|
-
? delta.content
|
|
295
|
-
: null;
|
|
296
|
-
if (streamedChunk) {
|
|
297
|
-
text += streamedChunk;
|
|
298
|
-
const decoded = decodeModelEvent({ type: "text-delta", delta: streamedChunk }, decodeOptions);
|
|
299
|
-
if (decoded)
|
|
300
|
-
yield decoded;
|
|
301
|
-
}
|
|
302
|
-
continue;
|
|
303
|
-
}
|
|
304
|
-
const type = typeof payload.type === "string" ? payload.type : "";
|
|
305
|
-
const proposalFrame = proposal(payload, type);
|
|
306
|
-
if (proposalFrame) {
|
|
307
|
-
emitProposal(ctx.events, wallet, requestId, proposalFrame);
|
|
308
|
-
const decoded = decodeActivityEvent(proposalFrame, decodeOptions);
|
|
309
|
-
if (decoded)
|
|
310
|
-
yield decoded;
|
|
311
|
-
continue;
|
|
312
|
-
}
|
|
313
|
-
const childFrame = child(payload, type);
|
|
314
|
-
if (childFrame) {
|
|
315
|
-
emitChild(ctx.events, wallet, requestId, childFrame);
|
|
316
|
-
const decoded = decodeActivityEvent(childFrame, decodeOptions);
|
|
317
|
-
if (decoded)
|
|
318
|
-
yield decoded;
|
|
319
|
-
continue;
|
|
320
|
-
}
|
|
321
|
-
const traceFrame = trace(payload, type);
|
|
322
|
-
if (traceFrame) {
|
|
323
|
-
const decoded = decodeActivityEvent(traceFrame, decodeOptions);
|
|
324
|
-
if (decoded)
|
|
325
|
-
yield decoded;
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
const conclaveFrame = conclave(payload, type);
|
|
329
|
-
if (conclaveFrame) {
|
|
330
|
-
const decoded = decodeActivityEvent(conclaveFrame, decodeOptions);
|
|
331
|
-
if (decoded)
|
|
332
|
-
yield decoded;
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
if (type === "thinking_start") {
|
|
336
|
-
const decoded = decodeActivityEvent(payload, decodeOptions);
|
|
337
|
-
if (decoded)
|
|
338
|
-
yield decoded;
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
if (type === "thinking_end") {
|
|
342
|
-
const decoded = decodeActivityEvent(payload, decodeOptions);
|
|
343
|
-
if (decoded)
|
|
344
|
-
yield decoded;
|
|
345
|
-
continue;
|
|
346
|
-
}
|
|
347
|
-
if (type === "reasoning_delta") {
|
|
348
|
-
const delta = typeof payload.delta === "string"
|
|
349
|
-
? payload.delta
|
|
350
|
-
: typeof payload.content === "string"
|
|
351
|
-
? payload.content
|
|
352
|
-
: "";
|
|
353
|
-
if (delta) {
|
|
354
|
-
const decoded = decodeModelEvent({ type: "reasoning-delta", delta }, decodeOptions);
|
|
355
|
-
if (decoded)
|
|
356
|
-
yield decoded;
|
|
357
|
-
}
|
|
358
|
-
continue;
|
|
359
|
-
}
|
|
360
|
-
if (type === "tool_args_delta") {
|
|
361
|
-
const argsDelta = typeof payload.argsDelta === "string"
|
|
362
|
-
? payload.argsDelta
|
|
363
|
-
: typeof payload.delta === "string"
|
|
364
|
-
? payload.delta
|
|
365
|
-
: "";
|
|
366
|
-
if (argsDelta) {
|
|
367
|
-
const id = typeof payload.id === "string" ? payload.id : undefined;
|
|
368
|
-
const toolName = typeof payload.toolName === "string" ? payload.toolName : undefined;
|
|
369
|
-
const decoded = decodeModelEvent({ type: "tool_args_delta", id, toolName, argsDelta }, decodeOptions);
|
|
370
|
-
if (decoded)
|
|
371
|
-
yield decoded;
|
|
372
|
-
}
|
|
373
|
-
continue;
|
|
374
209
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const summary = typeof payload.content === "string" ? payload.content : undefined;
|
|
385
|
-
const content = typeof payload.content === "string" ? payload.content : undefined;
|
|
386
|
-
const meta = view(payload.display);
|
|
387
|
-
const shown = {
|
|
388
|
-
...(meta?.name ? { displayName: meta.name } : {}),
|
|
389
|
-
...(meta ? { display: meta, targetKind: meta.kind } : {}),
|
|
390
|
-
...(meta?.target ? { target: meta.target } : {}),
|
|
391
|
-
};
|
|
392
|
-
activeTools.set(toolName, { summary });
|
|
393
|
-
ctx.events.emit("toolCallStart", {
|
|
394
|
-
userAddress: wallet.address,
|
|
395
|
-
chainId: wallet.chainId,
|
|
396
|
-
requestId,
|
|
397
|
-
source: "agent",
|
|
398
|
-
toolCallId: toolName,
|
|
399
|
-
toolName,
|
|
400
|
-
...shown,
|
|
401
|
-
summary,
|
|
402
|
-
});
|
|
403
|
-
const decoded = decodeActivityEvent({ type: "tool-start", toolName, display: meta, summary, content, input: payload.input }, decodeOptions);
|
|
404
|
-
if (decoded)
|
|
405
|
-
yield decoded;
|
|
406
|
-
continue;
|
|
407
|
-
}
|
|
408
|
-
if (type === "tool_end") {
|
|
409
|
-
const toolName = typeof payload.toolName === "string" ? payload.toolName : "tool";
|
|
410
|
-
const summary = typeof payload.message === "string" ? payload.message : undefined;
|
|
411
|
-
const failed = typeof payload.error === "string" && payload.error.length > 0;
|
|
412
|
-
const error = failed ? payload.error : undefined;
|
|
413
|
-
const meta = view(payload.display);
|
|
414
|
-
const displayName = meta?.name ?? display(toolName, payload);
|
|
415
|
-
const shown = {
|
|
416
|
-
...(displayName ? { displayName } : {}),
|
|
417
|
-
...(meta ? { display: meta, targetKind: meta.kind } : {}),
|
|
418
|
-
...(meta?.target ? { target: meta.target } : {}),
|
|
419
|
-
};
|
|
420
|
-
toolCalls.push({ toolName, ...shown, summary, failed, error });
|
|
421
|
-
activeTools.delete(toolName);
|
|
422
|
-
ctx.events.emit("toolCallEnd", {
|
|
423
|
-
userAddress: wallet.address,
|
|
424
|
-
chainId: wallet.chainId,
|
|
425
|
-
requestId,
|
|
426
|
-
source: "agent",
|
|
427
|
-
toolCallId: toolName,
|
|
428
|
-
toolName,
|
|
429
|
-
...shown,
|
|
430
|
-
summary,
|
|
431
|
-
failed,
|
|
432
|
-
error,
|
|
433
|
-
});
|
|
434
|
-
const decoded = decodeActivityEvent({
|
|
435
|
-
type: "tool-end",
|
|
436
|
-
toolName,
|
|
437
|
-
display: meta,
|
|
438
|
-
summary,
|
|
439
|
-
failed,
|
|
440
|
-
error,
|
|
441
|
-
output: payload.output ?? payload.message,
|
|
442
|
-
}, decodeOptions);
|
|
443
|
-
if (decoded)
|
|
444
|
-
yield decoded;
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
if (type === "error") {
|
|
448
|
-
const code = typeof payload.code === "string" ? payload.code : undefined;
|
|
449
|
-
const message = typeof payload.content === "string"
|
|
450
|
-
? payload.content
|
|
451
|
-
: typeof payload.error === "string"
|
|
452
|
-
? payload.error
|
|
453
|
-
: typeof payload.message === "string"
|
|
454
|
-
? payload.message
|
|
455
|
-
: "Agent stream failed";
|
|
456
|
-
const decoded = decodeActivityEvent({ type: "error", code, message }, decodeOptions);
|
|
457
|
-
if (decoded)
|
|
458
|
-
yield decoded;
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
if (type === "done") {
|
|
462
|
-
if (!emittedDone) {
|
|
463
|
-
emittedDone = true;
|
|
464
|
-
const decoded = decodeActivityEvent(payload, decodeOptions);
|
|
465
|
-
if (decoded)
|
|
466
|
-
yield decoded;
|
|
467
|
-
}
|
|
468
|
-
continue;
|
|
469
|
-
}
|
|
470
|
-
if (typeof payload.content === "string") {
|
|
471
|
-
text += payload.content;
|
|
472
|
-
const decoded = decodeModelEvent({ type: "text-delta", delta: payload.content }, decodeOptions);
|
|
473
|
-
if (decoded)
|
|
474
|
-
yield decoded;
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
|
-
if (typeof payload.text === "string") {
|
|
478
|
-
text += payload.text;
|
|
479
|
-
const decoded = decodeModelEvent({ type: "text-delta", delta: payload.text }, decodeOptions);
|
|
480
|
-
if (decoded)
|
|
481
|
-
yield decoded;
|
|
482
|
-
continue;
|
|
210
|
+
const event = next.value;
|
|
211
|
+
yield event;
|
|
212
|
+
handleEvent(event, ctx.events, wallet, requestId, { text, toolCalls });
|
|
213
|
+
if (event.domain === "model") {
|
|
214
|
+
const me = event;
|
|
215
|
+
if (me.type === "model.text.delta" && me.delta)
|
|
216
|
+
text += me.delta;
|
|
217
|
+
if (me.type === "model.text.done" && me.text && !text)
|
|
218
|
+
text = me.text;
|
|
483
219
|
}
|
|
484
220
|
}
|
|
221
|
+
if (result?.receipt)
|
|
222
|
+
streamReceipt = result.receipt;
|
|
485
223
|
}
|
|
486
224
|
finally {
|
|
487
225
|
try {
|
|
@@ -497,221 +235,182 @@ async function* driveAgentStream(ctx, params, options) {
|
|
|
497
235
|
runId: params.runId,
|
|
498
236
|
});
|
|
499
237
|
}
|
|
500
|
-
const finalReceipt = streamReceipt ?? headerReceipt;
|
|
501
238
|
return {
|
|
502
239
|
text,
|
|
503
240
|
toolCalls,
|
|
504
241
|
requestId,
|
|
505
|
-
receipt:
|
|
242
|
+
receipt: streamReceipt ?? headerReceipt,
|
|
506
243
|
budget,
|
|
507
244
|
sessionInvalidReason,
|
|
508
245
|
};
|
|
509
246
|
}
|
|
510
|
-
function
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
function obj(value) {
|
|
514
|
-
return value && typeof value === "object" && !Array.isArray(value)
|
|
515
|
-
? value
|
|
516
|
-
: null;
|
|
517
|
-
}
|
|
518
|
-
function str(value) {
|
|
519
|
-
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
520
|
-
}
|
|
521
|
-
function num(value) {
|
|
522
|
-
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
523
|
-
}
|
|
524
|
-
function strings(value) {
|
|
525
|
-
return Array.isArray(value) && value.every((item) => typeof item === "string")
|
|
526
|
-
? value
|
|
527
|
-
: undefined;
|
|
528
|
-
}
|
|
529
|
-
function childKind(type) {
|
|
530
|
-
switch (type) {
|
|
531
|
-
case "swarm_child_start":
|
|
532
|
-
return "start";
|
|
533
|
-
case "swarm_child_delta":
|
|
534
|
-
return "delta";
|
|
535
|
-
case "swarm_child_tool_start":
|
|
536
|
-
return "tool-start";
|
|
537
|
-
case "swarm_child_tool_end":
|
|
538
|
-
return "tool-end";
|
|
539
|
-
case "swarm_child_done":
|
|
540
|
-
return "done";
|
|
541
|
-
case "swarm_child_error":
|
|
542
|
-
return "error";
|
|
543
|
-
default:
|
|
544
|
-
return null;
|
|
247
|
+
function handleEvent(event, events, wallet, requestId, ctx) {
|
|
248
|
+
if (event.domain === "activity") {
|
|
249
|
+
handleActivityEvent(event, events, wallet, requestId, ctx);
|
|
545
250
|
}
|
|
546
251
|
}
|
|
547
|
-
function
|
|
548
|
-
const
|
|
549
|
-
if (
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
252
|
+
function handleActivityEvent(event, events, wallet, requestId, ctx) {
|
|
253
|
+
const rawType = rawString(event.raw, "type") ?? "";
|
|
254
|
+
if (event.type === "activity.tool") {
|
|
255
|
+
handleToolEvent(event, events, wallet, requestId, ctx, rawType);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (event.type === "activity.child") {
|
|
259
|
+
handleChildEvent(event, events, wallet, requestId, rawType);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (event.type === "activity.plan") {
|
|
263
|
+
handlePlanEvent(event, events, wallet, requestId, rawType);
|
|
264
|
+
return;
|
|
554
265
|
}
|
|
555
|
-
const details = obj(record.details);
|
|
556
|
-
return {
|
|
557
|
-
kind: kind,
|
|
558
|
-
...(str(record.id) ? { id: str(record.id) } : {}),
|
|
559
|
-
...(str(record.name) ? { name: str(record.name) } : {}),
|
|
560
|
-
...(str(record.target) ? { target: str(record.target) } : {}),
|
|
561
|
-
...(str(record.summary) ? { summary: str(record.summary) } : {}),
|
|
562
|
-
...(details ? { details } : {}),
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
function decision(value) {
|
|
566
|
-
return value === "approved" || value === "rejected" || value === "changes_requested"
|
|
567
|
-
? value
|
|
568
|
-
: undefined;
|
|
569
|
-
}
|
|
570
|
-
function proposal(payload, type) {
|
|
571
|
-
if (type !== "plan.proposed" && type !== "approval.requested" && type !== "approval.decided" && type !== "plan.feedback_requested")
|
|
572
|
-
return null;
|
|
573
|
-
const proposalId = str(payload.proposalId);
|
|
574
|
-
const version = num(payload.version);
|
|
575
|
-
const state = str(payload.state);
|
|
576
|
-
if (!proposalId || version === undefined || !state)
|
|
577
|
-
return null;
|
|
578
|
-
const details = obj(payload.details);
|
|
579
|
-
return {
|
|
580
|
-
type,
|
|
581
|
-
proposalId,
|
|
582
|
-
version,
|
|
583
|
-
state,
|
|
584
|
-
...(decision(payload.decision) ? { decision: decision(payload.decision) } : {}),
|
|
585
|
-
...(str(payload.rootRunId) ? { rootRunId: str(payload.rootRunId) } : {}),
|
|
586
|
-
...(str(payload.runId) ? { runId: str(payload.runId) } : {}),
|
|
587
|
-
...(str(payload.requestedBy) ? { requestedBy: str(payload.requestedBy) } : {}),
|
|
588
|
-
...("proposal" in payload ? { proposal: payload.proposal } : {}),
|
|
589
|
-
...(str(payload.markdown) ? { markdown: str(payload.markdown) } : {}),
|
|
590
|
-
...(num(payload.ts) !== undefined ? { ts: num(payload.ts) } : {}),
|
|
591
|
-
...(num(payload.updatedAt) !== undefined ? { updatedAt: num(payload.updatedAt) } : {}),
|
|
592
|
-
...(str(payload.approver) ? { approver: str(payload.approver) } : {}),
|
|
593
|
-
...(str(payload.reason) ? { reason: str(payload.reason) } : {}),
|
|
594
|
-
...(str(payload.feedback) ? { feedback: str(payload.feedback) } : {}),
|
|
595
|
-
...(view(payload.display) ? { display: view(payload.display) } : {}),
|
|
596
|
-
...(details ? { details } : {}),
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
function child(payload, type) {
|
|
600
|
-
const event = childKind(type);
|
|
601
|
-
if (!event)
|
|
602
|
-
return null;
|
|
603
|
-
const display = view(payload.display);
|
|
604
|
-
return {
|
|
605
|
-
type: "child",
|
|
606
|
-
event,
|
|
607
|
-
...(str(payload.rootRunId) ? { rootRunId: str(payload.rootRunId) } : {}),
|
|
608
|
-
...(str(payload.parentRunId) ? { parentRunId: str(payload.parentRunId) } : {}),
|
|
609
|
-
...(str(payload.subId) ? { subId: str(payload.subId) } : {}),
|
|
610
|
-
...(num(payload.depth) !== undefined ? { depth: num(payload.depth) } : {}),
|
|
611
|
-
...(str(payload.agentWallet) ? { agentWallet: str(payload.agentWallet) } : {}),
|
|
612
|
-
...(str(payload.userAddress) ? { userAddress: str(payload.userAddress) } : {}),
|
|
613
|
-
...(str(payload.runKey) ? { runKey: str(payload.runKey) } : {}),
|
|
614
|
-
...(strings(payload.runKeyChain) ? { runKeyChain: strings(payload.runKeyChain) } : {}),
|
|
615
|
-
...(typeof payload.delta === "string" ? { delta: payload.delta } : {}),
|
|
616
|
-
...(str(payload.toolName) ? { toolName: str(payload.toolName) } : {}),
|
|
617
|
-
...("input" in payload ? { input: payload.input } : {}),
|
|
618
|
-
...("output" in payload ? { output: payload.output } : {}),
|
|
619
|
-
...(typeof payload.failed === "boolean" ? { failed: payload.failed } : {}),
|
|
620
|
-
...(str(payload.error) ? { error: str(payload.error) } : {}),
|
|
621
|
-
...(obj(payload.usage) ? { usage: obj(payload.usage) } : {}),
|
|
622
|
-
...(num(payload.toolBatches) !== undefined ? { toolBatches: num(payload.toolBatches) } : {}),
|
|
623
|
-
...(str(payload.stopReason) ? { stopReason: str(payload.stopReason) } : {}),
|
|
624
|
-
...(num(payload.wallMs) !== undefined ? { wallMs: num(payload.wallMs) } : {}),
|
|
625
|
-
...(display ? { display } : {}),
|
|
626
|
-
...(num(payload.ts) !== undefined ? { ts: num(payload.ts) } : {}),
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
function trace(payload, type) {
|
|
630
|
-
if (type !== "trace")
|
|
631
|
-
return null;
|
|
632
|
-
const source = str(payload.source);
|
|
633
|
-
if (!source || !["capability", "model", "tool", "agent", "harness", "swarm", "route"].includes(source))
|
|
634
|
-
return null;
|
|
635
|
-
const details = obj(payload.details);
|
|
636
|
-
return {
|
|
637
|
-
type: "trace",
|
|
638
|
-
source: source,
|
|
639
|
-
...(str(payload.stage) ? { stage: str(payload.stage) } : {}),
|
|
640
|
-
...(str(payload.action) ? { action: str(payload.action) } : {}),
|
|
641
|
-
...(str(payload.message) ? { message: str(payload.message) } : {}),
|
|
642
|
-
...(view(payload.display) ? { display: view(payload.display) } : {}),
|
|
643
|
-
...(num(payload.ts) !== undefined ? { ts: num(payload.ts) } : {}),
|
|
644
|
-
...(details ? { details } : {}),
|
|
645
|
-
};
|
|
646
266
|
}
|
|
647
|
-
function
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
const
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
...(
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
267
|
+
function handleToolEvent(event, events, wallet, requestId, ctx, rawType) {
|
|
268
|
+
const isStart = rawType === "tool_start" || rawType === "tool-start" || event.status === "running";
|
|
269
|
+
const toolName = event.name ?? "tool";
|
|
270
|
+
const target = event.target;
|
|
271
|
+
const display = target ? {
|
|
272
|
+
kind: target.kind,
|
|
273
|
+
...(target.id ? { id: target.id } : {}),
|
|
274
|
+
...(target.name ? { name: target.name } : {}),
|
|
275
|
+
...(target.target ? { target: target.target } : {}),
|
|
276
|
+
...(target.summary ? { summary: target.summary } : {}),
|
|
277
|
+
...(target.details ? { details: target.details } : {}),
|
|
278
|
+
} : undefined;
|
|
279
|
+
const summary = target?.summary ?? rawString(event.raw, "message") ?? rawString(event.raw, "content");
|
|
280
|
+
const failed = event.status === "failed";
|
|
281
|
+
const error = rawString(event.raw, "error");
|
|
282
|
+
if (isStart) {
|
|
283
|
+
events.emit("toolCallStart", {
|
|
284
|
+
userAddress: wallet.address,
|
|
285
|
+
chainId: wallet.chainId,
|
|
286
|
+
requestId,
|
|
287
|
+
source: "agent",
|
|
288
|
+
toolCallId: toolName,
|
|
289
|
+
toolName,
|
|
290
|
+
...(display?.name ? { displayName: display.name } : {}),
|
|
291
|
+
...(display ? { display, targetKind: display.kind } : {}),
|
|
292
|
+
...(display?.target ? { target: display.target } : {}),
|
|
293
|
+
summary,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
ctx.toolCalls.push({
|
|
298
|
+
toolName,
|
|
299
|
+
...(display?.name ? { displayName: display.name } : {}),
|
|
300
|
+
...(display ? { display, targetKind: display.kind } : {}),
|
|
301
|
+
...(display?.target ? { target: display.target } : {}),
|
|
302
|
+
summary,
|
|
303
|
+
failed,
|
|
304
|
+
...(error ? { error } : {}),
|
|
305
|
+
});
|
|
306
|
+
events.emit("toolCallEnd", {
|
|
307
|
+
userAddress: wallet.address,
|
|
308
|
+
chainId: wallet.chainId,
|
|
309
|
+
requestId,
|
|
310
|
+
source: "agent",
|
|
311
|
+
toolCallId: toolName,
|
|
312
|
+
toolName,
|
|
313
|
+
...(display?.name ? { displayName: display.name } : {}),
|
|
314
|
+
...(display ? { display, targetKind: display.kind } : {}),
|
|
315
|
+
...(display?.target ? { target: display.target } : {}),
|
|
316
|
+
summary,
|
|
317
|
+
failed,
|
|
318
|
+
...(error ? { error } : {}),
|
|
319
|
+
});
|
|
320
|
+
}
|
|
661
321
|
}
|
|
662
|
-
function
|
|
663
|
-
|
|
322
|
+
function handleChildEvent(event, events, wallet, requestId, rawType) {
|
|
323
|
+
const childEvent = mapChildEventType(rawType);
|
|
324
|
+
if (!childEvent)
|
|
664
325
|
return;
|
|
326
|
+
const target = event.target;
|
|
327
|
+
const display = target ? {
|
|
328
|
+
kind: target.kind,
|
|
329
|
+
...(target.id ? { id: target.id } : {}),
|
|
330
|
+
...(target.name ? { name: target.name } : {}),
|
|
331
|
+
...(target.target ? { target: target.target } : {}),
|
|
332
|
+
...(target.summary ? { summary: target.summary } : {}),
|
|
333
|
+
...(target.details ? { details: target.details } : {}),
|
|
334
|
+
} : undefined;
|
|
335
|
+
const details = target?.details;
|
|
336
|
+
const raw = event.raw;
|
|
665
337
|
const payload = {
|
|
666
338
|
userAddress: wallet.address,
|
|
667
339
|
chainId: wallet.chainId,
|
|
668
340
|
requestId,
|
|
669
341
|
source: "agent",
|
|
670
|
-
event:
|
|
671
|
-
...(
|
|
672
|
-
...(
|
|
673
|
-
...(
|
|
674
|
-
...(
|
|
675
|
-
...(
|
|
676
|
-
...(
|
|
677
|
-
...(event.
|
|
678
|
-
...(
|
|
679
|
-
...("input" in
|
|
680
|
-
...("output" in
|
|
681
|
-
...(event.
|
|
682
|
-
...(
|
|
683
|
-
...(
|
|
684
|
-
...(
|
|
685
|
-
...(
|
|
686
|
-
...(
|
|
687
|
-
...(
|
|
688
|
-
...(
|
|
342
|
+
event: childEvent,
|
|
343
|
+
...(rawString(raw, "rootRunId") ? { rootRunId: rawString(raw, "rootRunId") } : {}),
|
|
344
|
+
...(rawString(raw, "parentRunId") ? { parentRunId: rawString(raw, "parentRunId") } : {}),
|
|
345
|
+
...(rawString(raw, "subId") ? { subId: rawString(raw, "subId") } : {}),
|
|
346
|
+
...(typeof details?.depth === "number" ? { depth: details.depth } : {}),
|
|
347
|
+
...(rawString(raw, "agentWallet") ? { agentWallet: rawString(raw, "agentWallet") } : {}),
|
|
348
|
+
...(rawString(raw, "runKey") ? { runKey: rawString(raw, "runKey") } : {}),
|
|
349
|
+
...(Array.isArray(event.path) ? { runKeyChain: event.path } : {}),
|
|
350
|
+
...(rawString(raw, "toolName") ? { toolName: rawString(raw, "toolName") } : {}),
|
|
351
|
+
...(raw && "input" in raw ? { input: raw.input } : {}),
|
|
352
|
+
...(raw && "output" in raw ? { output: raw.output } : {}),
|
|
353
|
+
...(event.status === "failed" ? { failed: true } : {}),
|
|
354
|
+
...(rawString(raw, "error") ? { error: rawString(raw, "error") } : {}),
|
|
355
|
+
...(raw && typeof raw.usage === "object" && raw.usage ? { usage: raw.usage } : {}),
|
|
356
|
+
...(typeof details?.toolBatches === "number" ? { toolBatches: details.toolBatches } : {}),
|
|
357
|
+
...(rawString(raw, "stopReason") ? { stopReason: rawString(raw, "stopReason") } : {}),
|
|
358
|
+
...(typeof details?.wallMs === "number" ? { wallMs: details.wallMs } : {}),
|
|
359
|
+
...(display ? { display } : {}),
|
|
360
|
+
...(typeof details?.ts === "number" ? { ts: details.ts } : {}),
|
|
689
361
|
};
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
362
|
+
switch (childEvent) {
|
|
363
|
+
case "start":
|
|
364
|
+
events.emit("childAgentStart", payload);
|
|
365
|
+
break;
|
|
366
|
+
case "tool-start":
|
|
367
|
+
events.emit("childAgentToolStart", payload);
|
|
368
|
+
break;
|
|
369
|
+
case "tool-end":
|
|
370
|
+
events.emit("childAgentToolEnd", payload);
|
|
371
|
+
break;
|
|
372
|
+
case "done":
|
|
373
|
+
events.emit("childAgentDone", payload);
|
|
374
|
+
break;
|
|
375
|
+
case "error":
|
|
376
|
+
events.emit("childAgentError", payload);
|
|
377
|
+
break;
|
|
704
378
|
}
|
|
705
379
|
}
|
|
706
|
-
function
|
|
380
|
+
function handlePlanEvent(event, events, wallet, requestId, rawType) {
|
|
381
|
+
const p = event.payload ?? {};
|
|
382
|
+
const display = event.target ? {
|
|
383
|
+
kind: event.target.kind,
|
|
384
|
+
...(event.target.id ? { id: event.target.id } : {}),
|
|
385
|
+
...(event.target.name ? { name: event.target.name } : {}),
|
|
386
|
+
...(event.target.target ? { target: event.target.target } : {}),
|
|
387
|
+
...(event.target.summary ? { summary: event.target.summary } : {}),
|
|
388
|
+
...(event.target.details ? { details: event.target.details } : {}),
|
|
389
|
+
} : undefined;
|
|
707
390
|
const payload = {
|
|
708
|
-
|
|
391
|
+
type: rawType,
|
|
392
|
+
proposalId: String(p.proposalId ?? event.id),
|
|
393
|
+
version: Number(p.version ?? 1),
|
|
394
|
+
...(typeof p.approvalId === "string" ? { approvalId: p.approvalId } : {}),
|
|
395
|
+
state: String(p.state ?? event.status ?? "pending"),
|
|
396
|
+
...(p.decision ? { decision: p.decision } : {}),
|
|
397
|
+
...(typeof p.rootRunId === "string" ? { rootRunId: p.rootRunId } : {}),
|
|
398
|
+
...(event.runId ? { runId: event.runId } : {}),
|
|
399
|
+
...(typeof p.requestedBy === "string" ? { requestedBy: p.requestedBy } : {}),
|
|
400
|
+
...(p.proposal !== undefined ? { proposal: p.proposal } : {}),
|
|
401
|
+
...(typeof p.markdown === "string" ? { markdown: p.markdown } : {}),
|
|
402
|
+
...(typeof p.updatedAt === "number" ? { updatedAt: p.updatedAt } : {}),
|
|
403
|
+
...(typeof p.approver === "string" ? { approver: p.approver } : {}),
|
|
404
|
+
...(typeof p.reason === "string" ? { reason: p.reason } : {}),
|
|
405
|
+
...(typeof p.feedback === "string" ? { feedback: p.feedback } : {}),
|
|
406
|
+
...(display ? { display } : {}),
|
|
407
|
+
...(event.target?.details ? { details: event.target.details } : {}),
|
|
709
408
|
userAddress: wallet.address,
|
|
710
409
|
chainId: wallet.chainId,
|
|
711
410
|
requestId,
|
|
712
411
|
source: "agent",
|
|
713
412
|
};
|
|
714
|
-
switch (
|
|
413
|
+
switch (rawType) {
|
|
715
414
|
case "plan.proposed":
|
|
716
415
|
events.emit("planProposed", payload);
|
|
717
416
|
break;
|
|
@@ -726,31 +425,25 @@ function emitProposal(events, wallet, requestId, event) {
|
|
|
726
425
|
break;
|
|
727
426
|
}
|
|
728
427
|
}
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
428
|
+
// =============================================================================
|
|
429
|
+
// Utilities
|
|
430
|
+
// =============================================================================
|
|
431
|
+
function rawString(value, key) {
|
|
432
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
433
|
+
return undefined;
|
|
434
|
+
const record = value;
|
|
435
|
+
const v = record[key];
|
|
436
|
+
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
736
437
|
}
|
|
737
|
-
function
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
return
|
|
741
|
-
|
|
742
|
-
return
|
|
438
|
+
function mapChildEventType(rawType) {
|
|
439
|
+
switch (rawType) {
|
|
440
|
+
case "swarm_child_start": return "start";
|
|
441
|
+
case "swarm_child_tool_start": return "tool-start";
|
|
442
|
+
case "swarm_child_tool_end": return "tool-end";
|
|
443
|
+
case "swarm_child_done": return "done";
|
|
444
|
+
case "swarm_child_error": return "error";
|
|
445
|
+
default: return null;
|
|
743
446
|
}
|
|
744
|
-
catch {
|
|
745
|
-
return null;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
function display(toolName, payload) {
|
|
749
|
-
if (toolName !== "models_call")
|
|
750
|
-
return undefined;
|
|
751
|
-
const parsed = json(payload.output) ?? json(payload.message);
|
|
752
|
-
const model = obj(parsed?.model);
|
|
753
|
-
return str(model?.name) ?? str(model?.id) ?? str(model?.modelId);
|
|
754
447
|
}
|
|
755
448
|
function mergeSignals(a, b) {
|
|
756
449
|
if (!a)
|
|
@@ -769,19 +462,4 @@ function mergeSignals(a, b) {
|
|
|
769
462
|
},
|
|
770
463
|
};
|
|
771
464
|
}
|
|
772
|
-
async function readAgentStreamFrame(iterator, timeoutMs) {
|
|
773
|
-
const pending = iterator.next();
|
|
774
|
-
if (!timeoutMs) {
|
|
775
|
-
return pending;
|
|
776
|
-
}
|
|
777
|
-
let timer;
|
|
778
|
-
const timeout = new Promise((resolve) => {
|
|
779
|
-
timer = setTimeout(() => resolve({ done: true, value: undefined }), timeoutMs);
|
|
780
|
-
});
|
|
781
|
-
pending.catch(() => undefined);
|
|
782
|
-
const result = await Promise.race([pending, timeout]);
|
|
783
|
-
if (timer)
|
|
784
|
-
clearTimeout(timer);
|
|
785
|
-
return result;
|
|
786
|
-
}
|
|
787
465
|
//# sourceMappingURL=agent.js.map
|