agentbox-sdk 0.1.310 → 0.1.311

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.
@@ -2,8 +2,8 @@ import {
2
2
  Agent,
3
3
  agentboxRoot,
4
4
  getAgentLayout
5
- } from "../chunk-6MDOW3GK.js";
6
- import "../chunk-PNONJAGY.js";
5
+ } from "../chunk-BNBEOIGX.js";
6
+ import "../chunk-764MJCER.js";
7
7
  import {
8
8
  AGENT_RESERVED_PORTS,
9
9
  collectAllAgentReservedPorts
@@ -299,6 +299,21 @@ var OpenCodeLogAssembler = class {
299
299
  userMessageIds = /* @__PURE__ */ new Set();
300
300
  textByPartId = /* @__PURE__ */ new Map();
301
301
  byPartId = /* @__PURE__ */ new Map();
302
+ // Sub-agent (`task` tool) linkage. opencode multiplexes the parent task
303
+ // tool's `state.metadata.sessionId` update and the child session's first
304
+ // stream events onto the same SSE bus with no ordering guarantee, so
305
+ // child events frequently arrive before the parent has published its
306
+ // child sessionID. We bind FIFO instead: every task tool we see goes
307
+ // onto a queue; the first non-main sessionID that lacks an explicit
308
+ // binding gets paired with the queue head. Once a binding is established
309
+ // we stamp every emitted snapshot for that child session with
310
+ // `parentTaskCallId` on the part so consumers don't have to re-derive
311
+ // the linkage from snapshots.
312
+ mainSessionID = null;
313
+ pendingTaskCallIds = [];
314
+ childSessionToTaskCallId = /* @__PURE__ */ new Map();
315
+ childMessageIdToTaskCallId = /* @__PURE__ */ new Map();
316
+ messageIdToSessionId = /* @__PURE__ */ new Map();
302
317
  process(event) {
303
318
  if (!isRecord(event)) {
304
319
  return [];
@@ -306,10 +321,48 @@ var OpenCodeLogAssembler = class {
306
321
  const type = typeof event.type === "string" ? event.type : "";
307
322
  const properties = isRecord(event.properties) ? event.properties : {};
308
323
  const info = isRecord(properties.info) ? properties.info : null;
324
+ const eventPart = isRecord(properties.part) ? properties.part : isRecord(event.part) ? event.part : null;
325
+ if (this.mainSessionID === null) {
326
+ const candidate = (typeof info?.sessionID === "string" ? info.sessionID : null) ?? (eventPart && typeof eventPart.sessionID === "string" ? eventPart.sessionID : null) ?? (typeof properties.sessionID === "string" ? properties.sessionID : null);
327
+ if (candidate) this.mainSessionID = candidate;
328
+ }
329
+ if (info && typeof info.id === "string" && typeof info.sessionID === "string") {
330
+ this.messageIdToSessionId.set(info.id, info.sessionID);
331
+ }
332
+ if (eventPart && typeof eventPart.id === "string" && typeof eventPart.messageID === "string" && typeof eventPart.sessionID === "string") {
333
+ this.messageIdToSessionId.set(eventPart.messageID, eventPart.sessionID);
334
+ }
335
+ if (eventPart && this.isTaskToolPart(eventPart)) {
336
+ const callId = typeof eventPart.callID === "string" ? eventPart.callID : typeof eventPart.id === "string" ? eventPart.id : null;
337
+ if (callId) {
338
+ const metaSid = this.extractTaskMetadataSessionId(eventPart);
339
+ if (metaSid && !this.childSessionToTaskCallId.has(metaSid)) {
340
+ this.childSessionToTaskCallId.set(metaSid, callId);
341
+ this.pendingTaskCallIds = this.pendingTaskCallIds.filter(
342
+ (id) => id !== callId
343
+ );
344
+ } else if (!metaSid) {
345
+ const alreadyBound = this.taskCallIdAlreadyBound(callId);
346
+ if (!alreadyBound && !this.pendingTaskCallIds.includes(callId)) {
347
+ this.pendingTaskCallIds.push(callId);
348
+ }
349
+ }
350
+ }
351
+ }
352
+ const effectiveSid = this.extractSessionID(properties, eventPart, info);
353
+ if (effectiveSid && effectiveSid !== this.mainSessionID && !this.childSessionToTaskCallId.has(effectiveSid) && this.pendingTaskCallIds.length > 0) {
354
+ const taskCallId = this.pendingTaskCallIds.shift();
355
+ this.childSessionToTaskCallId.set(effectiveSid, taskCallId);
356
+ }
309
357
  if (type === "message.updated" && typeof info?.id === "string" && info.role === "user") {
310
358
  this.userMessageIds.add(info.id);
311
359
  return [clone(event)];
312
360
  }
361
+ const parentTaskCallId = this.resolveParentTaskCallId(
362
+ effectiveSid,
363
+ eventPart,
364
+ info
365
+ );
313
366
  if (type === "message.part.delta") {
314
367
  const partId = typeof properties.partID === "string" ? properties.partID : null;
315
368
  const messageId = typeof properties.messageID === "string" ? properties.messageID : null;
@@ -317,32 +370,50 @@ var OpenCodeLogAssembler = class {
317
370
  if (!partId || !delta || properties.field !== "text" || messageId && this.userMessageIds.has(messageId)) {
318
371
  return [];
319
372
  }
373
+ if (messageId && effectiveSid) {
374
+ this.messageIdToSessionId.set(messageId, effectiveSid);
375
+ }
320
376
  const text = (this.textByPartId.get(partId) ?? "") + delta;
321
377
  this.textByPartId.set(partId, text);
322
- return [
323
- this.upsertPart(partId, {
324
- id: partId,
325
- messageID: messageId ?? void 0,
326
- type: "text",
327
- text
328
- })
329
- ];
378
+ const part = {
379
+ id: partId,
380
+ messageID: messageId ?? void 0,
381
+ type: "text",
382
+ text
383
+ };
384
+ if (parentTaskCallId) {
385
+ part.parentTaskCallId = parentTaskCallId;
386
+ if (messageId) {
387
+ this.childMessageIdToTaskCallId.set(messageId, parentTaskCallId);
388
+ }
389
+ }
390
+ return [this.upsertPart(partId, part)];
330
391
  }
331
- const part = isRecord(properties.part) ? properties.part : isRecord(event.part) ? event.part : null;
332
- if (part && typeof part.id === "string") {
333
- if (part.messageID && this.userMessageIds.has(String(part.messageID))) {
392
+ if (eventPart && typeof eventPart.id === "string") {
393
+ if (eventPart.messageID && this.userMessageIds.has(String(eventPart.messageID))) {
334
394
  return [];
335
395
  }
336
- const previous = this.byPartId.get(part.id);
337
- if (part.type === "text" && previous && isRecord(previous.properties?.part)) {
396
+ const previous = this.byPartId.get(eventPart.id);
397
+ if (eventPart.type === "text" && previous && isRecord(previous.properties?.part)) {
338
398
  const previousPart = previous.properties.part;
339
399
  const previousText = typeof previousPart.text === "string" ? previousPart.text : "";
340
- const nextText = typeof part.text === "string" ? part.text : "";
400
+ const nextText = typeof eventPart.text === "string" ? eventPart.text : "";
341
401
  if (nextText.length < previousText.length) {
342
402
  return [clone(previous)];
343
403
  }
344
404
  }
345
- this.byPartId.set(part.id, clone(event));
405
+ const enriched = clone(event);
406
+ if (parentTaskCallId) {
407
+ this.stampParentTaskCallId(enriched, parentTaskCallId);
408
+ if (typeof eventPart.messageID === "string") {
409
+ this.childMessageIdToTaskCallId.set(
410
+ eventPart.messageID,
411
+ parentTaskCallId
412
+ );
413
+ }
414
+ }
415
+ this.byPartId.set(eventPart.id, enriched);
416
+ return [clone(enriched)];
346
417
  }
347
418
  return [clone(event)];
348
419
  }
@@ -350,11 +421,23 @@ var OpenCodeLogAssembler = class {
350
421
  this.userMessageIds.clear();
351
422
  this.textByPartId.clear();
352
423
  this.byPartId.clear();
424
+ this.mainSessionID = null;
425
+ this.pendingTaskCallIds = [];
426
+ this.childSessionToTaskCallId.clear();
427
+ this.childMessageIdToTaskCallId.clear();
428
+ this.messageIdToSessionId.clear();
353
429
  for (const snapshot of snapshots) {
354
430
  if (!isRecord(snapshot)) continue;
355
431
  const type = typeof snapshot.type === "string" ? snapshot.type : "";
356
432
  const properties = isRecord(snapshot.properties) ? snapshot.properties : {};
357
433
  const info = isRecord(properties.info) ? properties.info : null;
434
+ if (this.mainSessionID === null) {
435
+ const candidate = (typeof info?.sessionID === "string" ? info.sessionID : null) ?? (typeof properties.sessionID === "string" ? properties.sessionID : null);
436
+ if (candidate) this.mainSessionID = candidate;
437
+ }
438
+ if (info && typeof info.id === "string" && typeof info.sessionID === "string") {
439
+ this.messageIdToSessionId.set(info.id, info.sessionID);
440
+ }
358
441
  if (type === "message.updated" && typeof info?.id === "string" && info.role === "user") {
359
442
  this.userMessageIds.add(info.id);
360
443
  continue;
@@ -365,9 +448,88 @@ var OpenCodeLogAssembler = class {
365
448
  if (typeof part.text === "string") {
366
449
  this.textByPartId.set(part.id, part.text);
367
450
  }
451
+ if (typeof part.messageID === "string" && typeof part.sessionID === "string") {
452
+ this.messageIdToSessionId.set(part.messageID, part.sessionID);
453
+ }
454
+ if (typeof part.parentTaskCallId === "string") {
455
+ if (typeof part.sessionID === "string" && !this.childSessionToTaskCallId.has(part.sessionID)) {
456
+ this.childSessionToTaskCallId.set(
457
+ part.sessionID,
458
+ part.parentTaskCallId
459
+ );
460
+ }
461
+ if (typeof part.messageID === "string") {
462
+ this.childMessageIdToTaskCallId.set(
463
+ part.messageID,
464
+ part.parentTaskCallId
465
+ );
466
+ }
467
+ }
468
+ if (this.isTaskToolPart(part)) {
469
+ const callId = typeof part.callID === "string" ? part.callID : typeof part.id === "string" ? part.id : null;
470
+ const metaSid = this.extractTaskMetadataSessionId(part);
471
+ if (callId && metaSid && !this.childSessionToTaskCallId.has(metaSid)) {
472
+ this.childSessionToTaskCallId.set(metaSid, callId);
473
+ }
474
+ }
368
475
  }
369
476
  }
370
477
  }
478
+ isTaskToolPart(part) {
479
+ if (part.type !== "tool") return false;
480
+ const tool = typeof part.tool === "string" ? part.tool.toLowerCase() : "";
481
+ return tool === "task";
482
+ }
483
+ taskCallIdAlreadyBound(callId) {
484
+ for (const v of this.childSessionToTaskCallId.values()) {
485
+ if (v === callId) return true;
486
+ }
487
+ return false;
488
+ }
489
+ extractTaskMetadataSessionId(part) {
490
+ const state = isRecord(part.state) ? part.state : null;
491
+ const metadata = state && isRecord(state.metadata) ? state.metadata : null;
492
+ if (!metadata) return null;
493
+ if (typeof metadata.sessionId === "string") return metadata.sessionId;
494
+ if (typeof metadata.sessionID === "string") return metadata.sessionID;
495
+ return null;
496
+ }
497
+ extractSessionID(properties, part, info) {
498
+ if (typeof properties.sessionID === "string") return properties.sessionID;
499
+ if (part && typeof part.sessionID === "string") return part.sessionID;
500
+ if (info && typeof info.sessionID === "string") return info.sessionID;
501
+ return null;
502
+ }
503
+ resolveParentTaskCallId(sessionID, part, info) {
504
+ if (sessionID && this.childSessionToTaskCallId.has(sessionID)) {
505
+ return this.childSessionToTaskCallId.get(sessionID) ?? null;
506
+ }
507
+ const candidateMessageIds = [];
508
+ if (part && typeof part.messageID === "string") {
509
+ candidateMessageIds.push(part.messageID);
510
+ }
511
+ if (info && typeof info.id === "string") {
512
+ candidateMessageIds.push(info.id);
513
+ }
514
+ for (const messageId of candidateMessageIds) {
515
+ const cached = this.childMessageIdToTaskCallId.get(messageId);
516
+ if (cached) return cached;
517
+ const mappedSid = this.messageIdToSessionId.get(messageId);
518
+ if (mappedSid && this.childSessionToTaskCallId.has(mappedSid)) {
519
+ const tcid = this.childSessionToTaskCallId.get(mappedSid);
520
+ this.childMessageIdToTaskCallId.set(messageId, tcid);
521
+ return tcid;
522
+ }
523
+ }
524
+ return null;
525
+ }
526
+ stampParentTaskCallId(snapshot, parentTaskCallId) {
527
+ const properties = isRecord(snapshot.properties) ? snapshot.properties : null;
528
+ const part = properties && isRecord(properties.part) ? properties.part : isRecord(snapshot.part) ? snapshot.part : null;
529
+ if (part) {
530
+ part.parentTaskCallId = parentTaskCallId;
531
+ }
532
+ }
371
533
  upsertPart(partId, part) {
372
534
  const next = {
373
535
  type: "message.part.updated",
@@ -2,7 +2,7 @@ import {
2
2
  createNormalizedEvent,
3
3
  normalizeRawAgentEvent,
4
4
  toAISDKStream
5
- } from "./chunk-PNONJAGY.js";
5
+ } from "./chunk-764MJCER.js";
6
6
  import {
7
7
  AgentBoxError,
8
8
  AsyncQueue,
@@ -4,7 +4,7 @@ import {
4
4
  normalizeRawAgentEvent,
5
5
  toAISDKEvent,
6
6
  toAISDKStream
7
- } from "../chunk-PNONJAGY.js";
7
+ } from "../chunk-764MJCER.js";
8
8
  import "../chunk-GOFJNFAD.js";
9
9
  export {
10
10
  ProviderLogAssembler,
package/dist/index.js CHANGED
@@ -2,14 +2,14 @@ import {
2
2
  Agent,
3
3
  agentboxRoot,
4
4
  getAgentLayout
5
- } from "./chunk-6MDOW3GK.js";
5
+ } from "./chunk-BNBEOIGX.js";
6
6
  import {
7
7
  ProviderLogAssembler,
8
8
  createNormalizedEvent,
9
9
  normalizeRawAgentEvent,
10
10
  toAISDKEvent,
11
11
  toAISDKStream
12
- } from "./chunk-PNONJAGY.js";
12
+ } from "./chunk-764MJCER.js";
13
13
  import {
14
14
  Sandbox,
15
15
  SandboxAdapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentbox-sdk",
3
- "version": "0.1.310",
3
+ "version": "0.1.311",
4
4
  "description": "Swappable coding agents and sandbox providers for Bun and TypeScript.",
5
5
  "license": "MIT",
6
6
  "repository": {