@genesislcap/ai-assistant 15.81.0 → 15.83.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.
Files changed (110) hide show
  1. package/dist/ai-assistant.api.json +2800 -116
  2. package/dist/ai-assistant.d.ts +887 -109
  3. package/dist/chat-driver.cjs +468 -107
  4. package/dist/chat-driver.cjs.map +4 -4
  5. package/dist/chat-driver.mjs +468 -107
  6. package/dist/chat-driver.mjs.map +4 -4
  7. package/dist/custom-elements.json +1108 -61
  8. package/dist/dts/autopilot/autopilot-driver.d.ts +117 -11
  9. package/dist/dts/autopilot/autopilot-driver.d.ts.map +1 -1
  10. package/dist/dts/autopilot/autopilot-session.d.ts +55 -7
  11. package/dist/dts/autopilot/autopilot-session.d.ts.map +1 -1
  12. package/dist/dts/autopilot/autopilot-tools.d.ts +3 -2
  13. package/dist/dts/autopilot/autopilot-tools.d.ts.map +1 -1
  14. package/dist/dts/autopilot/decision-message.d.ts +35 -3
  15. package/dist/dts/autopilot/decision-message.d.ts.map +1 -1
  16. package/dist/dts/autopilot/hand-back/hand-back.controller.d.ts +2 -2
  17. package/dist/dts/autopilot/hand-back/hand-back.controller.d.ts.map +1 -1
  18. package/dist/dts/autopilot/hand-back/hand-back.d.ts +2 -2
  19. package/dist/dts/autopilot/hand-back/hand-back.d.ts.map +1 -1
  20. package/dist/dts/autopilot/index.d.ts +1 -0
  21. package/dist/dts/autopilot/index.d.ts.map +1 -1
  22. package/dist/dts/autopilot/widget-tools.d.ts +95 -0
  23. package/dist/dts/autopilot/widget-tools.d.ts.map +1 -0
  24. package/dist/dts/channel/ai-activity-channel.d.ts +10 -0
  25. package/dist/dts/channel/ai-activity-channel.d.ts.map +1 -1
  26. package/dist/dts/components/agent-session/agent-session.d.ts +2 -1
  27. package/dist/dts/components/agent-session/agent-session.d.ts.map +1 -1
  28. package/dist/dts/components/ai-driver/ai-driver.d.ts +25 -6
  29. package/dist/dts/components/ai-driver/ai-driver.d.ts.map +1 -1
  30. package/dist/dts/components/ai-driver/base-driver-decorator.d.ts +2 -0
  31. package/dist/dts/components/ai-driver/base-driver-decorator.d.ts.map +1 -1
  32. package/dist/dts/components/chat-driver/chat-driver.d.ts +14 -2
  33. package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
  34. package/dist/dts/components/chat-driver/interaction-controller.d.ts +61 -17
  35. package/dist/dts/components/chat-driver/interaction-controller.d.ts.map +1 -1
  36. package/dist/dts/components/orchestrating-driver/orchestrating-driver.d.ts +6 -0
  37. package/dist/dts/components/orchestrating-driver/orchestrating-driver.d.ts.map +1 -1
  38. package/dist/dts/config/config.d.ts +31 -6
  39. package/dist/dts/config/config.d.ts.map +1 -1
  40. package/dist/dts/config/define-stateful-agent.d.ts +5 -1
  41. package/dist/dts/config/define-stateful-agent.d.ts.map +1 -1
  42. package/dist/dts/index.d.ts +4 -2
  43. package/dist/dts/index.d.ts.map +1 -1
  44. package/dist/dts/main/main.d.ts +33 -16
  45. package/dist/dts/main/main.d.ts.map +1 -1
  46. package/dist/dts/main/main.template.d.ts.map +1 -1
  47. package/dist/dts/main/main.types.d.ts +15 -0
  48. package/dist/dts/main/main.types.d.ts.map +1 -1
  49. package/dist/dts/notifications/agent-notifications.d.ts +6 -2
  50. package/dist/dts/notifications/agent-notifications.d.ts.map +1 -1
  51. package/dist/dts/notifications/agent-notifier.d.ts +26 -4
  52. package/dist/dts/notifications/agent-notifier.d.ts.map +1 -1
  53. package/dist/dts/state/decision-surface.d.ts +22 -4
  54. package/dist/dts/state/decision-surface.d.ts.map +1 -1
  55. package/dist/dts/state/interaction-context.d.ts +23 -4
  56. package/dist/dts/state/interaction-context.d.ts.map +1 -1
  57. package/dist/dts/state/interaction-registry.d.ts +55 -6
  58. package/dist/dts/state/interaction-registry.d.ts.map +1 -1
  59. package/dist/dts/state/live-interactions.d.ts +28 -0
  60. package/dist/dts/state/live-interactions.d.ts.map +1 -0
  61. package/dist/dts/state/persistence/session-persister.d.ts +6 -0
  62. package/dist/dts/state/persistence/session-persister.d.ts.map +1 -1
  63. package/dist/dts/state/persistence/session-snapshot.d.ts +16 -5
  64. package/dist/dts/state/persistence/session-snapshot.d.ts.map +1 -1
  65. package/dist/dts/state/test-interaction-context.d.ts +93 -0
  66. package/dist/dts/state/test-interaction-context.d.ts.map +1 -0
  67. package/dist/dts/types/ai-chat-widget.d.ts +7 -6
  68. package/dist/dts/types/ai-chat-widget.d.ts.map +1 -1
  69. package/dist/dts/types/interaction-context.d.ts +128 -17
  70. package/dist/dts/types/interaction-context.d.ts.map +1 -1
  71. package/dist/dts/types/interaction-interceptor.d.ts +48 -6
  72. package/dist/dts/types/interaction-interceptor.d.ts.map +1 -1
  73. package/dist/dts/types/interaction-surface.d.ts +145 -23
  74. package/dist/dts/types/interaction-surface.d.ts.map +1 -1
  75. package/dist/dts/utils/delegated-message.d.ts +7 -0
  76. package/dist/dts/utils/delegated-message.d.ts.map +1 -1
  77. package/dist/esm/autopilot/autopilot-driver.js +313 -60
  78. package/dist/esm/autopilot/autopilot-session.js +219 -40
  79. package/dist/esm/autopilot/autopilot-tools.js +11 -7
  80. package/dist/esm/autopilot/decision-message.js +105 -26
  81. package/dist/esm/autopilot/hand-back/hand-back.controller.js +2 -1
  82. package/dist/esm/autopilot/index.js +1 -0
  83. package/dist/esm/autopilot/widget-tools.js +39 -0
  84. package/dist/esm/components/agent-session/agent-session.js +3 -2
  85. package/dist/esm/components/ai-driver/base-driver-decorator.js +17 -0
  86. package/dist/esm/components/chat-driver/chat-driver.js +18 -2
  87. package/dist/esm/components/chat-driver/interaction-controller.js +232 -71
  88. package/dist/esm/components/orchestrating-driver/orchestrating-driver.js +14 -6
  89. package/dist/esm/components/settings-modal/settings-modal.template.js +2 -2
  90. package/dist/esm/config/define-stateful-agent.js +22 -1
  91. package/dist/esm/index.js +2 -0
  92. package/dist/esm/main/main.js +88 -35
  93. package/dist/esm/main/main.template.js +4 -1
  94. package/dist/esm/notifications/agent-notifications.js +11 -1
  95. package/dist/esm/notifications/agent-notifier.js +41 -7
  96. package/dist/esm/state/debug-event-log.js +6 -6
  97. package/dist/esm/state/decision-surface.js +137 -54
  98. package/dist/esm/state/interaction-context.js +41 -8
  99. package/dist/esm/state/interaction-registry.js +11 -0
  100. package/dist/esm/state/live-interactions.js +36 -0
  101. package/dist/esm/state/persistence/session-persister.js +55 -18
  102. package/dist/esm/state/persistence/session-snapshot.js +2 -2
  103. package/dist/esm/state/test-interaction-context.js +99 -0
  104. package/docs/notifications.md +55 -5
  105. package/docs/styling.md +33 -0
  106. package/package.json +19 -19
  107. package/sandbox/README.md +3 -2
  108. package/sandbox/autopilot.ts +26 -6
  109. package/sandbox/sandbox.ts +2 -0
  110. /package/dist/{tsdoc-metadata.json → dts/tsdoc-metadata.json} +0 -0
@@ -4117,8 +4117,8 @@ var META_EVENT_IMPORTANCE = {
4117
4117
  "interaction.outcome-dropped": "high",
4118
4118
  // A long action's step starting, finishing, or skipped on resume because it was already done.
4119
4119
  "interaction.step": "low",
4120
- // A cut checkpoint taken over after a reload, refused (by sameWork, or as malformed), or dropped
4121
- // at flow end.
4120
+ // A cut checkpoint taken over after a reload, refused (by sameWork, and kept for a later request;
4121
+ // or as malformed), or dropped at flow end, as is the live one.
4122
4122
  "interaction.checkpoint": "normal",
4123
4123
  // A claimant handing a pending interaction to the person, or taking it back.
4124
4124
  "interaction.held": "normal",
@@ -5020,9 +5020,10 @@ var AgentSession = class {
5020
5020
  /**
5021
5021
  * The name routing is locked to, flow lock first. A flow in progress outranks the user's
5022
5022
  * sticky pick for the duration of the flow; neither being set means automatic routing.
5023
+ * `pin`, when given, stands in for the sticky pick: one message routed its own way.
5023
5024
  */
5024
- getLockedName() {
5025
- return this.flowOwner ?? this.pinned;
5025
+ getLockedName(pin = this.pinned) {
5026
+ return this.flowOwner ?? pin;
5026
5027
  }
5027
5028
  /** Set the sticky user pin. `null` returns to automatic routing. */
5028
5029
  setPinned(name) {
@@ -5356,14 +5357,22 @@ var FoldController = class {
5356
5357
  // src/state/decision-surface.ts
5357
5358
  var SELECT_ACTION = "select";
5358
5359
  var REPLY_ACTION = "reply";
5359
- var normalise = (option) => typeof option === "string" ? { id: option, label: option } : { id: option.id ?? option.label, label: option.label, enabledWhen: option.enabledWhen };
5360
- var isEnabled = (option) => {
5360
+ var normalise = (option) => typeof option === "string" ? { id: option, label: option } : { id: option.id ?? option.label, label: option.label, unavailable: option.unavailable };
5361
+ var GENERIC_UNAVAILABLE = "not available in this state";
5362
+ function unavailableReason(guard, ...args) {
5363
+ if (!guard) return void 0;
5361
5364
  try {
5362
- return option.enabledWhen?.() ?? true;
5365
+ const reason = guard(...args);
5366
+ if (reason === void 0) return void 0;
5367
+ return reason.trim().replace(/\.+$/, "") || GENERIC_UNAVAILABLE;
5363
5368
  } catch {
5364
- return false;
5369
+ return GENERIC_UNAVAILABLE;
5365
5370
  }
5366
- };
5371
+ }
5372
+ var reasonFor = (option) => unavailableReason(option.unavailable);
5373
+ var isEnabled = (option) => reasonFor(option) === void 0;
5374
+ var describeViews = (views) => Object.fromEntries(Object.entries(views).map(([name, v]) => [name, v.description]));
5375
+ var readView = (views, view) => Object.prototype.hasOwnProperty.call(views, view) ? views[view].read() : void 0;
5367
5376
  function buildDecisionSurface(spec) {
5368
5377
  const options = spec.options.map(normalise);
5369
5378
  const ids = /* @__PURE__ */ new Set();
@@ -5378,12 +5387,16 @@ function buildDecisionSurface(spec) {
5378
5387
  const actions = () => {
5379
5388
  const enabled = options.filter(isEnabled);
5380
5389
  return [
5381
- ...options.map((o) => ({
5382
- name: o.id,
5383
- label: o.label,
5384
- finishes: true,
5385
- enabled: isEnabled(o)
5386
- })),
5390
+ ...options.map((o) => {
5391
+ const unavailable = reasonFor(o);
5392
+ return {
5393
+ name: o.id,
5394
+ label: o.label,
5395
+ finishes: true,
5396
+ enabled: unavailable === void 0,
5397
+ ...unavailable !== void 0 ? { unavailable } : {}
5398
+ };
5399
+ }),
5387
5400
  ...spec.multi ? [
5388
5401
  {
5389
5402
  name: SELECT_ACTION,
@@ -5392,13 +5405,32 @@ function buildDecisionSurface(spec) {
5392
5405
  args: "options"
5393
5406
  }
5394
5407
  ] : [],
5395
- ...spec.freeText ? [{ name: REPLY_ACTION, finishes: true, enabled: true, args: "text" }] : []
5408
+ ...spec.freeText ? [replyAction()] : []
5396
5409
  ];
5397
5410
  };
5411
+ const replyReason = () => unavailableReason(spec.freeTextUnavailable);
5412
+ const replyAction = () => {
5413
+ const unavailable = replyReason();
5414
+ return {
5415
+ name: REPLY_ACTION,
5416
+ finishes: true,
5417
+ enabled: unavailable === void 0,
5418
+ ...unavailable !== void 0 ? { unavailable } : {},
5419
+ args: "text",
5420
+ refines: true
5421
+ };
5422
+ };
5398
5423
  const answer = (a) => () => spec.choose(a);
5424
+ const facts = () => spec.facts?.() ?? {};
5425
+ const views = spec.views && Object.keys(spec.views).length ? spec.views : void 0;
5426
+ const readable = views ? {
5427
+ views: describeViews(views),
5428
+ read: (view) => view === void 0 ? facts() : readView(views, view)
5429
+ } : {};
5399
5430
  return {
5400
5431
  prompt: spec.prompt,
5401
- facts: () => spec.facts?.() ?? {},
5432
+ facts,
5433
+ ...readable,
5402
5434
  actions,
5403
5435
  isReady: () => actions().some((a) => a.finishes && a.enabled),
5404
5436
  prepare(name, args) {
@@ -5419,7 +5451,7 @@ function buildDecisionSurface(spec) {
5419
5451
  if (disabled.length) {
5420
5452
  return {
5421
5453
  error: "disabled",
5422
- message: `Not available right now: ${disabled.map((o) => o.label).join(", ")}.`
5454
+ message: `Not available right now: ${disabled.map((o) => `${o.label} (${reasonFor(o)})`).join(", ")}.`
5423
5455
  };
5424
5456
  }
5425
5457
  const picks = chosen;
@@ -5430,6 +5462,10 @@ function buildDecisionSurface(spec) {
5430
5462
  };
5431
5463
  }
5432
5464
  if (spec.freeText && name === REPLY_ACTION) {
5465
+ const why2 = replyReason();
5466
+ if (why2 !== void 0) {
5467
+ return { error: "disabled", message: `"reply" is not available right now: ${why2}.` };
5468
+ }
5433
5469
  const text = args?.text;
5434
5470
  if (typeof text !== "string" || !text.trim()) {
5435
5471
  return { error: "invalid-args", message: "`reply` takes { text: string }, not empty." };
@@ -5440,8 +5476,12 @@ function buildDecisionSurface(spec) {
5440
5476
  if (!option) {
5441
5477
  return { error: "unknown-action", message: `No action "${name}" here.` };
5442
5478
  }
5443
- if (!isEnabled(option)) {
5444
- return { error: "disabled", message: `"${option.label}" is not available right now.` };
5479
+ const why = reasonFor(option);
5480
+ if (why !== void 0) {
5481
+ return {
5482
+ error: "disabled",
5483
+ message: `"${option.label}" is not available right now: ${why}.`
5484
+ };
5445
5485
  }
5446
5486
  return {
5447
5487
  choice: option.label,
@@ -5481,33 +5521,65 @@ function checkArgs(args, schema) {
5481
5521
  return wrong.length ? `${wrong.join("; ")}.` : void 0;
5482
5522
  }
5483
5523
  function buildFullSurface(spec) {
5484
- const guard = (enabledWhen) => {
5485
- try {
5486
- return enabledWhen?.() ?? true;
5487
- } catch {
5488
- return false;
5524
+ for (const [name, action] of Object.entries(spec.actions)) {
5525
+ if (action.text && action.argsSchema) {
5526
+ throw new Error(
5527
+ `exposeSurface: action "${name}" takes \`text\` or \`argsSchema\`, not both.`
5528
+ );
5489
5529
  }
5490
- };
5491
- const actions = () => Object.entries(spec.actions).map(([name, a]) => ({
5492
- name,
5493
- finishes: !!a.finishes,
5494
- enabled: guard(a.enabledWhen),
5495
- description: a.description,
5496
- ...a.argsSchema ? { args: "fields", argsSchema: a.argsSchema } : {}
5497
- }));
5530
+ }
5531
+ const views = spec.views && Object.keys(spec.views).length ? spec.views : void 0;
5532
+ if (spec.preview !== void 0 && !(views && Object.prototype.hasOwnProperty.call(views, spec.preview))) {
5533
+ throw new Error(`exposeSurface: the preview "${spec.preview}" is not one of its views.`);
5534
+ }
5535
+ const actions = () => Object.entries(spec.actions).map(([name, a]) => {
5536
+ const unavailable = unavailableReason(a.unavailable);
5537
+ return {
5538
+ name,
5539
+ finishes: !!a.finishes,
5540
+ enabled: unavailable === void 0,
5541
+ ...unavailable !== void 0 ? { unavailable } : {},
5542
+ description: a.description,
5543
+ ...a.text ? { args: "text" } : {},
5544
+ ...a.argsSchema ? { args: "fields", argsSchema: a.argsSchema } : {},
5545
+ ...a.refines ? { refines: true } : {}
5546
+ };
5547
+ });
5498
5548
  return {
5499
5549
  prompt: spec.prompt,
5500
5550
  facts: () => ({}),
5501
5551
  describe: () => spec.describe(),
5502
- views: spec.views,
5503
- read: (view) => spec.read(view),
5552
+ ...views ? {
5553
+ views: describeViews(views),
5554
+ ...spec.preview !== void 0 ? { preview: spec.preview } : {},
5555
+ read: (view) => {
5556
+ const name = view ?? spec.preview;
5557
+ return name === void 0 ? void 0 : readView(views, name);
5558
+ }
5559
+ } : {},
5504
5560
  actions,
5505
5561
  isReady: () => actions().some((a) => a.enabled),
5506
5562
  prepare(name, args) {
5507
5563
  const action = Object.prototype.hasOwnProperty.call(spec.actions, name) ? spec.actions[name] : void 0;
5508
5564
  if (!action) return { error: "unknown-action", message: `No action "${name}" here.` };
5509
- if (!guard(action.enabledWhen)) {
5510
- return { error: "disabled", message: `"${name}" is not available right now.` };
5565
+ const why = unavailableReason(action.unavailable);
5566
+ if (why !== void 0) {
5567
+ return { error: "disabled", message: `"${name}" is not available right now: ${why}.` };
5568
+ }
5569
+ if (action.text) {
5570
+ const text = args?.text;
5571
+ if (typeof text !== "string" || !text.trim()) {
5572
+ return {
5573
+ error: "invalid-args",
5574
+ message: `"${name}" takes its argument as \`text\`, not empty.`
5575
+ };
5576
+ }
5577
+ const trimmed = text.trim();
5578
+ return {
5579
+ choice: `${name}: ${trimmed}`.slice(0, CHOICE_CHARS),
5580
+ finishes: !!action.finishes,
5581
+ run: () => action.run(trimmed)
5582
+ };
5511
5583
  }
5512
5584
  const problem = checkArgs(args ?? {}, action.argsSchema);
5513
5585
  if (problem) return { error: "invalid-args", message: `"${name}": ${problem}` };
@@ -5535,15 +5607,21 @@ function createInteractionContext(interactionId, deps = {}) {
5535
5607
  let surface;
5536
5608
  let lastActivityAt = Date.now();
5537
5609
  let checkpoint = deps.checkpoint;
5610
+ let working;
5538
5611
  let offered = false;
5539
5612
  let attention;
5540
5613
  let attentionDone = false;
5614
+ let attentionTaker;
5541
5615
  const reportAttention = () => {
5542
5616
  if (!offered || !attention || attentionDone || disposed || settled) return;
5543
5617
  attentionDone = true;
5544
5618
  if (heldBy !== "user") return;
5545
- deps.requestAttention?.(attention.message);
5619
+ (attentionTaker ?? deps.requestAttention)?.(attention.message);
5546
5620
  };
5621
+ const reporting = (built) => ({
5622
+ ...built,
5623
+ isReady: () => working === false || working !== true && built.isReady()
5624
+ });
5547
5625
  const checkReady = () => {
5548
5626
  if (disposed || !surface || readyListeners.size === 0 || !surface.isReady()) return;
5549
5627
  const waiting = [...readyListeners];
@@ -5574,6 +5652,14 @@ function createInteractionContext(interactionId, deps = {}) {
5574
5652
  get checkpoint() {
5575
5653
  return checkpoint;
5576
5654
  },
5655
+ get working() {
5656
+ return working;
5657
+ },
5658
+ setWorking(next) {
5659
+ if (disposed || next === working) return;
5660
+ working = next;
5661
+ context.notify();
5662
+ },
5577
5663
  saveCheckpoint(next) {
5578
5664
  if (disposed) return;
5579
5665
  checkpoint = next;
@@ -5616,19 +5702,19 @@ function createInteractionContext(interactionId, deps = {}) {
5616
5702
  }
5617
5703
  disposers.add(fn);
5618
5704
  },
5619
- resolve(result) {
5705
+ resolve(result, options) {
5620
5706
  if (settled || disposed) return;
5621
5707
  settled = true;
5622
- deps.resolve?.(result);
5708
+ deps.resolve?.(result, options);
5623
5709
  },
5624
5710
  exposeDecision(decision) {
5625
5711
  if (disposed) return;
5626
- surface = buildDecisionSurface(decision);
5712
+ surface = reporting(buildDecisionSurface(decision));
5627
5713
  context.notify();
5628
5714
  },
5629
5715
  exposeSurface(spec) {
5630
5716
  if (disposed) return;
5631
- surface = buildFullSurface(spec);
5717
+ surface = reporting(buildFullSurface(spec));
5632
5718
  context.notify();
5633
5719
  },
5634
5720
  onSurfaceReady(fn) {
@@ -5682,6 +5768,16 @@ function createInteractionContext(interactionId, deps = {}) {
5682
5768
  offered = true;
5683
5769
  reportAttention();
5684
5770
  },
5771
+ get attentionMessage() {
5772
+ return attention?.message;
5773
+ },
5774
+ takeAttention(onRequest) {
5775
+ if (attentionDone || disposed || settled || attentionTaker) return void 0;
5776
+ attentionTaker = onRequest;
5777
+ return () => {
5778
+ if (attentionTaker === onRequest) attentionTaker = void 0;
5779
+ };
5780
+ },
5685
5781
  get surface() {
5686
5782
  return surface;
5687
5783
  }
@@ -5694,6 +5790,15 @@ function getInteractionRegistration(name) {
5694
5790
  return registry2.get(name);
5695
5791
  }
5696
5792
 
5793
+ // src/state/live-interactions.ts
5794
+ var live = /* @__PURE__ */ new Map();
5795
+ function trackInteractionContext(context) {
5796
+ live.set(context.interactionId, context);
5797
+ return () => {
5798
+ if (live.get(context.interactionId) === context) live.delete(context.interactionId);
5799
+ };
5800
+ }
5801
+
5697
5802
  // src/state/run-steps.ts
5698
5803
  var recorders = /* @__PURE__ */ new WeakMap();
5699
5804
  function bindStepRecorder(ctx, record, refused) {
@@ -5742,6 +5847,19 @@ function doneOnUsersBehalf(interaction, answeredBy) {
5742
5847
  }
5743
5848
  return {};
5744
5849
  }
5850
+ function stampAnsweredBy(result, answeredBy) {
5851
+ if (result === null || typeof result !== "object" || Array.isArray(result)) return result;
5852
+ const copy = { ...result };
5853
+ if (answeredBy) copy.answeredBy = answeredBy;
5854
+ else delete copy.answeredBy;
5855
+ return copy;
5856
+ }
5857
+ function whoAnswered(interaction, result, how) {
5858
+ const unanswered = result?.status === "timeout";
5859
+ const resolvedItself = !unanswered && !interaction.settledBy && (!!interaction.resolvesItself || how?.resolvedItself === true);
5860
+ if (unanswered || resolvedItself) return { resolvedItself };
5861
+ return { answeredBy: interaction.settledBy ?? "user", resolvedItself };
5862
+ }
5745
5863
  var STOPPED = {
5746
5864
  status: "timeout",
5747
5865
  message: "The turn was stopped before this was answered."
@@ -5764,58 +5882,118 @@ var InteractionController = class {
5764
5882
  * the root's are ever consulted: a sub-agent's request is opened by the root.
5765
5883
  */
5766
5884
  this.interceptors = [];
5885
+ /**
5886
+ * Checkpoints a reload cut, oldest first, each waiting for a request of its own widget type that
5887
+ * `sameWork` agrees is the same work. Held apart from `live`, so neither the work a resumed flow
5888
+ * does first nor a request `sameWork` refuses can lose one: a flow may open other long widgets
5889
+ * before the one a checkpoint belongs to asks for it. Each leaves when it is taken over, and all
5890
+ * of them when the flow ends.
5891
+ */
5892
+ this.cut = [];
5767
5893
  }
5768
- /** See `ChatDriver.getInteractionCheckpoint`. */
5894
+ /**
5895
+ * See `ChatDriver.getInteractionCheckpoints`: every checkpoint to save with the session — those a
5896
+ * reload cut that nothing has taken yet, oldest first, then the live interaction's. A reload cuts
5897
+ * them all, so all of them are restored as cut.
5898
+ */
5899
+ getCheckpoints() {
5900
+ const live2 = this.live;
5901
+ return [
5902
+ ...this.cut.map(({ componentName, value }) => ({ componentName, value })),
5903
+ ...live2 ? [{ componentName: live2.componentName, value: live2.value }] : []
5904
+ ];
5905
+ }
5906
+ /**
5907
+ * See `ChatDriver.getInteractionCheckpoint` (deprecated): one checkpoint, the live interaction's,
5908
+ * else the newest a reload cut.
5909
+ */
5769
5910
  getCheckpoint() {
5770
- const cp = this.checkpoint;
5771
- return cp ? { componentName: cp.componentName, value: cp.value } : void 0;
5911
+ const all = this.getCheckpoints();
5912
+ return all[all.length - 1];
5772
5913
  }
5773
- /** See `ChatDriver.restoreInteractionCheckpoint`. Restored means cut: its interaction is gone. */
5914
+ /**
5915
+ * See `ChatDriver.restoreInteractionCheckpoints`. Restored means cut: their interactions are gone.
5916
+ * Replaces whatever was held.
5917
+ */
5918
+ restoreCheckpoints(saved) {
5919
+ this.live = void 0;
5920
+ this.cut = (saved ?? []).map(({ componentName, value }) => ({ componentName, value }));
5921
+ }
5922
+ /** See `ChatDriver.restoreInteractionCheckpoint` (deprecated): one, or none. */
5774
5923
  restoreCheckpoint(saved) {
5775
- this.checkpoint = saved ? { componentName: saved.componentName, value: saved.value, cut: true } : void 0;
5924
+ this.restoreCheckpoints(saved ? [saved] : []);
5776
5925
  }
5777
- /** Drop the checkpoint, used or not, when its flow ends. */
5926
+ /** Drop every checkpoint, used or not, cut or live, when its flow ends. */
5778
5927
  clearCheckpoint(why) {
5779
- const cp = this.checkpoint;
5780
- if (!cp) return;
5781
- this.checkpoint = void 0;
5782
- recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5783
- action: "cleared",
5784
- component: cp.componentName,
5785
- why,
5786
- ...cp.interactionId ? { interactionId: cp.interactionId } : {}
5787
- });
5928
+ const live2 = this.live;
5929
+ const cut = this.cut;
5930
+ this.live = void 0;
5931
+ this.cut = [];
5932
+ for (const cp of cut) {
5933
+ recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5934
+ action: "cleared",
5935
+ component: cp.componentName,
5936
+ why,
5937
+ cut: true
5938
+ });
5939
+ }
5940
+ if (live2) {
5941
+ recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5942
+ action: "cleared",
5943
+ component: live2.componentName,
5944
+ why,
5945
+ interactionId: live2.interactionId
5946
+ });
5947
+ }
5788
5948
  }
5789
5949
  /**
5790
- * Hand a cut checkpoint to a new request of the same widget type, if its registration's
5791
- * `sameWork` agrees. Only the first such request is asked: a refused checkpoint is dropped, and
5792
- * the work starts fresh as it would have without one.
5950
+ * Offer the checkpoints a reload cut to a new request of their widget type, newest first, and
5951
+ * hand it the first its registration's `sameWork` agrees is the same work (any, without one). A
5952
+ * checkpoint `sameWork` refuses stays where it is, for a later request of the same type: a flow
5953
+ * may ask for other work of that type first. Each refusal is recorded, with the identity kept.
5793
5954
  */
5794
5955
  takeOverCheckpoint(componentName, data, interactionId) {
5795
- const cut = this.checkpoint;
5796
- if (!cut?.cut || cut.componentName !== componentName) return void 0;
5797
- this.checkpoint = void 0;
5798
5956
  const sameWork = getInteractionRegistration(componentName)?.sameWork;
5799
- let agrees = true;
5800
- if (sameWork) {
5801
- try {
5802
- agrees = sameWork(cut.value, data);
5803
- } catch (e) {
5804
- logger2.warn(`InteractionController: "${componentName}" sameWork threw; starting fresh:`, e);
5805
- agrees = false;
5957
+ for (let i = this.cut.length - 1; i >= 0; i -= 1) {
5958
+ const cut = this.cut[i];
5959
+ if (cut.componentName !== componentName) continue;
5960
+ let agrees = true;
5961
+ if (sameWork) {
5962
+ try {
5963
+ agrees = sameWork(cut.value, data);
5964
+ } catch (e) {
5965
+ logger2.warn(
5966
+ `InteractionController: "${componentName}" sameWork threw; not taking it over:`,
5967
+ e
5968
+ );
5969
+ agrees = false;
5970
+ }
5806
5971
  }
5972
+ if (!agrees) {
5973
+ recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5974
+ action: "refused",
5975
+ component: componentName,
5976
+ interactionId,
5977
+ saved: cut.value,
5978
+ kept: true
5979
+ });
5980
+ continue;
5981
+ }
5982
+ this.cut.splice(i, 1);
5983
+ recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5984
+ action: "resumed",
5985
+ component: componentName,
5986
+ interactionId
5987
+ });
5988
+ this.live = { componentName, value: cut.value, interactionId };
5989
+ return cut.value;
5807
5990
  }
5808
- recordMetaEvent(this.deps.sessionKey, "interaction.checkpoint", {
5809
- action: agrees ? "resumed" : "refused",
5810
- component: componentName,
5811
- interactionId,
5812
- ...agrees ? {} : { saved: cut.value }
5813
- });
5814
- if (!agrees) return void 0;
5815
- this.checkpoint = { componentName, value: cut.value, interactionId };
5816
- return cut.value;
5991
+ return void 0;
5817
5992
  }
5818
- /** A controller's `ctx.saveCheckpoint`: kept as JSON, the form it is persisted in. */
5993
+ /**
5994
+ * A controller's `ctx.saveCheckpoint`: kept as JSON, the form it is persisted in. Replaces the
5995
+ * live interaction's only; the checkpoints a reload cut are left waiting for their own widgets.
5996
+ */
5819
5997
  saveCheckpoint(interactionId, componentName, value) {
5820
5998
  if (!this.pending.has(interactionId)) return;
5821
5999
  let copy;
@@ -5828,7 +6006,7 @@ var InteractionController = class {
5828
6006
  );
5829
6007
  return;
5830
6008
  }
5831
- this.checkpoint = { componentName, value: copy, interactionId };
6009
+ this.live = { componentName, value: copy, interactionId };
5832
6010
  this.deps.emit(new CustomEvent("checkpoint-saved", { detail: { interactionId } }));
5833
6011
  }
5834
6012
  /** See `ChatDriver.addInteractionInterceptor`. Returns a remover. */
@@ -5855,11 +6033,34 @@ var InteractionController = class {
5855
6033
  *
5856
6034
  * Runs before the interaction's message is appended, so the widget's first render already knows
5857
6035
  * its holder and nothing appears only to disappear.
6036
+ *
6037
+ * **A late claim.** An interceptor that declined may still claim it later, through its hold's
6038
+ * `claim`, once the asking is over and while nobody else has (see {@link InteractionHold.claim}).
5858
6039
  */
5859
6040
  offer(handle, opened) {
5860
6041
  let claimant;
6042
+ let asking = true;
6043
+ const claimLate = (interceptor) => {
6044
+ if (asking || claimant || opened.userOnly) return false;
6045
+ const pending = this.pending.get(opened.interactionId);
6046
+ if (!pending || !handle.setHeldBy(interceptor.id)) return false;
6047
+ claimant = interceptor;
6048
+ pending.claimant = interceptor;
6049
+ recordMetaEvent(this.deps.sessionKey, "interaction.held", {
6050
+ interactionId: opened.interactionId,
6051
+ heldBy: interceptor.id,
6052
+ claimant: interceptor.id,
6053
+ late: true
6054
+ });
6055
+ return true;
6056
+ };
5861
6057
  for (const interceptor of [...this.interceptors].reverse()) {
5862
- const hold = this.holdFor(handle, interceptor, () => claimant === interceptor);
6058
+ const hold = this.holdFor(
6059
+ handle,
6060
+ interceptor,
6061
+ () => claimant === interceptor,
6062
+ () => claimLate(interceptor)
6063
+ );
5863
6064
  let answer;
5864
6065
  try {
5865
6066
  answer = interceptor.onInteractionOpened({ ...opened, ctx: handle.context, hold });
@@ -5881,11 +6082,39 @@ var InteractionController = class {
5881
6082
  const pending = this.pending.get(opened.interactionId);
5882
6083
  if (pending) pending.claimant = interceptor;
5883
6084
  }
6085
+ asking = false;
5884
6086
  return handle.context.heldBy;
5885
6087
  }
5886
- /** The {@link InteractionHold} for one interceptor: it applies only while that one is the claimant. */
5887
- holdFor(handle, interceptor, isClaimant) {
6088
+ /**
6089
+ * Publish `agent-attention-ended` each time the interaction stops waiting on the person — a
6090
+ * claimant takes it, or it settles — so a notification still showing for it is closed
6091
+ * (GENC-1660). Wired once who holds it is decided, so a claim made as it opens is not one.
6092
+ */
6093
+ reportAttentionEnds(ctx) {
6094
+ const ended = () => this.deps.activityBus.publish("agent-attention-ended", {
6095
+ sessionKey: this.deps.sessionKey,
6096
+ interactionId: ctx.interactionId
6097
+ });
6098
+ let heldBy = ctx.heldBy;
6099
+ const unsubscribe = ctx.subscribe(() => {
6100
+ if (ctx.heldBy === heldBy) return;
6101
+ heldBy = ctx.heldBy;
6102
+ if (heldBy !== "user") ended();
6103
+ });
6104
+ ctx.onDispose(() => {
6105
+ unsubscribe();
6106
+ ended();
6107
+ });
6108
+ }
6109
+ /**
6110
+ * The {@link InteractionHold} for one interceptor: it applies only while that one is the claimant,
6111
+ * which `claim` can make it after the asking, if nobody else is.
6112
+ */
6113
+ holdFor(handle, interceptor, isClaimant, claim) {
5888
6114
  return {
6115
+ claim,
6116
+ attentionMessage: () => handle.attentionMessage,
6117
+ takeAttention: (onRequest) => handle.takeAttention(onRequest),
5889
6118
  set: (holder) => {
5890
6119
  if (!isClaimant()) return false;
5891
6120
  const next = holder === "user" ? "user" : interceptor.id;
@@ -6029,6 +6258,9 @@ var InteractionController = class {
6029
6258
  reject,
6030
6259
  onSettled,
6031
6260
  overrideId: chatInputDuringExecution ? interactionId : void 0,
6261
+ ...getInteractionRegistration(componentName)?.resolvesItself === true && {
6262
+ resolvesItself: true
6263
+ },
6032
6264
  // Resolve the interaction with a timeout result if the user does not
6033
6265
  // respond in time. `resolve` runs the same teardown a normal resolution
6034
6266
  // does (marks the widget read-only, reverts any input override) and
@@ -6042,7 +6274,7 @@ var InteractionController = class {
6042
6274
  });
6043
6275
  const handle = createInteractionContext(interactionId, {
6044
6276
  sessionKey: this.deps.sessionKey,
6045
- resolve: (result) => this.resolve(interactionId, result),
6277
+ resolve: (result, how) => this.resolve(interactionId, result, how),
6046
6278
  checkpoint: this.takeOverCheckpoint(componentName, data, interactionId),
6047
6279
  saveCheckpoint: (value) => this.saveCheckpoint(interactionId, componentName, value),
6048
6280
  requestAttention: (message) => this.deps.activityBus.publish("agent-attention", {
@@ -6053,6 +6285,7 @@ var InteractionController = class {
6053
6285
  })
6054
6286
  });
6055
6287
  this.contexts.set(interactionId, handle);
6288
+ handle.context.onDispose(trackInteractionContext(handle.context));
6056
6289
  bindStepRecorder(
6057
6290
  handle.context,
6058
6291
  (step, outcome) => recordMetaEvent(this.deps.sessionKey, "interaction.step", {
@@ -6085,6 +6318,7 @@ var InteractionController = class {
6085
6318
  userOnly: options?.userOnly === true
6086
6319
  });
6087
6320
  handle.markOffered();
6321
+ this.reportAttentionEnds(handle.context);
6088
6322
  recordMetaEvent(this.deps.sessionKey, "interaction.requested", {
6089
6323
  interactionId,
6090
6324
  component: componentName,
@@ -6114,7 +6348,7 @@ var InteractionController = class {
6114
6348
  const opened = this.pending.get(interactionId);
6115
6349
  if (opened) {
6116
6350
  opened.appended = true;
6117
- if (opened.early) this.resolve(interactionId, opened.early.result);
6351
+ if (opened.early) this.resolve(interactionId, opened.early.result, opened.early.options);
6118
6352
  }
6119
6353
  const signal = options?.signal;
6120
6354
  if (signal) {
@@ -6133,16 +6367,17 @@ var InteractionController = class {
6133
6367
  }
6134
6368
  /**
6135
6369
  * See `ChatDriver.resolveInteraction`. Warns and returns when the id is unknown — a widget can
6136
- * report twice, and the second report has nothing left to settle.
6370
+ * report twice, and the second report has nothing left to settle. `how` is what a controller's
6371
+ * `ctx.resolve` said about the settle (`resolvedItself`); every other route passes none.
6137
6372
  */
6138
- resolve(interactionId, result) {
6373
+ resolve(interactionId, result, how) {
6139
6374
  const interaction = this.pending.get(interactionId);
6140
6375
  if (!interaction) {
6141
6376
  logger2.warn(`Interaction with ID ${interactionId} not found.`);
6142
6377
  return;
6143
6378
  }
6144
6379
  if (!interaction.appended) {
6145
- interaction.early ??= { result };
6380
+ interaction.early ??= { result, options: how };
6146
6381
  return;
6147
6382
  }
6148
6383
  if (interaction.timeoutHandle) clearTimeout(interaction.timeoutHandle);
@@ -6152,8 +6387,7 @@ var InteractionController = class {
6152
6387
  result?.diagnostics
6153
6388
  );
6154
6389
  const costUsd = typeof result?.costUsd === "number" && Number.isFinite(result.costUsd) && result.costUsd > 0 ? result.costUsd : void 0;
6155
- const unanswered = result?.status === "timeout";
6156
- const answeredBy = unanswered ? void 0 : interaction.settledBy ?? "user";
6390
+ const { answeredBy, resolvedItself } = whoAnswered(interaction, result, how);
6157
6391
  const run = answeredBy && answeredBy !== "user" ? interaction.running : void 0;
6158
6392
  if (run) {
6159
6393
  interaction.settledInRun = run.token;
@@ -6167,20 +6401,23 @@ var InteractionController = class {
6167
6401
  ...message.interaction,
6168
6402
  resolved: result,
6169
6403
  ...answeredBy ? { answeredBy } : {},
6404
+ ...resolvedItself ? { resolvedItself } : {},
6170
6405
  ...edits ? { edits } : {}
6171
6406
  },
6172
6407
  ...costUsd != null ? { externalCostUsd: (message.externalCostUsd ?? 0) + costUsd } : {}
6173
6408
  }));
6174
6409
  if (patched) this.deps.notifyHistoryUpdated();
6175
6410
  if (typeof result?.userMessage === "string" && result.userMessage.trim()) {
6176
- this.deps.appendToHistory(
6177
- answeredBy && answeredBy !== "user" ? {
6411
+ if (!answeredBy || answeredBy === "user") {
6412
+ this.deps.appendToHistory({ role: "synthetic-user", content: result.userMessage });
6413
+ } else if (!run?.note) {
6414
+ this.deps.appendToHistory({
6178
6415
  role: "assistant",
6179
6416
  content: result.userMessage,
6180
6417
  delegate: answeredBy,
6181
6418
  excludeFromProvider: true
6182
- } : { role: "synthetic-user", content: result.userMessage }
6183
- );
6419
+ });
6420
+ }
6184
6421
  }
6185
6422
  if (interaction.overrideId) {
6186
6423
  this.deps.emit(new CustomEvent("interaction-stop", { detail: { interactionId } }));
@@ -6188,22 +6425,32 @@ var InteractionController = class {
6188
6425
  recordMetaEvent(this.deps.sessionKey, "interaction.resolved", {
6189
6426
  interactionId,
6190
6427
  ...answeredBy ? { answeredBy } : {},
6428
+ ...resolvedItself ? { resolvedItself } : {},
6191
6429
  ...edits ? { edits: edits.length } : {}
6192
6430
  });
6193
6431
  this.deps.activityBus.publish("interaction-resolved", void 0);
6194
6432
  try {
6195
- interaction.onSettled?.({ answeredBy, decision, ...edited ? { edited } : {} });
6433
+ interaction.onSettled?.({
6434
+ answeredBy,
6435
+ ...resolvedItself ? { resolvedItself } : {},
6436
+ decision,
6437
+ ...edited ? { edited } : {}
6438
+ });
6196
6439
  } catch (e) {
6197
6440
  logger2.warn("InteractionController: onSettled threw:", e);
6198
6441
  }
6199
6442
  this.deps.emit(
6200
6443
  new CustomEvent("interaction-settled", {
6201
- detail: { interactionId, ...answeredBy ? { answeredBy } : {} }
6444
+ detail: {
6445
+ interactionId,
6446
+ ...answeredBy ? { answeredBy } : {},
6447
+ ...resolvedItself ? { resolvedItself } : {}
6448
+ }
6202
6449
  })
6203
6450
  );
6204
- interaction.resolve(result);
6451
+ interaction.resolve(stampAnsweredBy(result, answeredBy));
6205
6452
  this.pending.delete(interactionId);
6206
- if (this.checkpoint?.interactionId === interactionId) this.checkpoint = void 0;
6453
+ if (this.live?.interactionId === interactionId) this.live = void 0;
6207
6454
  this.contexts.get(interactionId)?.dispose();
6208
6455
  this.contexts.delete(interactionId);
6209
6456
  }
@@ -8131,11 +8378,27 @@ var ChatDriver = class _ChatDriver extends EventTarget {
8131
8378
  getInteractionContext(interactionId) {
8132
8379
  return this.interactions.context(interactionId);
8133
8380
  }
8134
- /** {@inheritDoc AiDriver.getInteractionCheckpoint} */
8381
+ /** {@inheritDoc AiDriver.getInteractionCheckpoints} */
8382
+ getInteractionCheckpoints() {
8383
+ return this.interactions.getCheckpoints();
8384
+ }
8385
+ /** {@inheritDoc AiDriver.restoreInteractionCheckpoints} */
8386
+ restoreInteractionCheckpoints(saved) {
8387
+ this.interactions.restoreCheckpoints(saved);
8388
+ }
8389
+ /**
8390
+ * {@inheritDoc AiDriver.getInteractionCheckpoint}
8391
+ *
8392
+ * @deprecated Use `getInteractionCheckpoints`.
8393
+ */
8135
8394
  getInteractionCheckpoint() {
8136
8395
  return this.interactions.getCheckpoint();
8137
8396
  }
8138
- /** {@inheritDoc AiDriver.restoreInteractionCheckpoint} */
8397
+ /**
8398
+ * {@inheritDoc AiDriver.restoreInteractionCheckpoint}
8399
+ *
8400
+ * @deprecated Use `restoreInteractionCheckpoints`.
8401
+ */
8139
8402
  restoreInteractionCheckpoint(saved) {
8140
8403
  this.interactions.restoreCheckpoint(saved);
8141
8404
  }
@@ -9422,6 +9685,18 @@ var BaseDriverDecorator = class _BaseDriverDecorator extends EventTarget {
9422
9685
  getFlowOwner() {
9423
9686
  return this.inner.getFlowOwner?.() ?? null;
9424
9687
  }
9688
+ getInteractionCheckpoints() {
9689
+ if (this.inner.getInteractionCheckpoints) return this.inner.getInteractionCheckpoints();
9690
+ const one = this.inner.getInteractionCheckpoint?.();
9691
+ return one ? [one] : [];
9692
+ }
9693
+ restoreInteractionCheckpoints(saved) {
9694
+ if (this.inner.restoreInteractionCheckpoints) {
9695
+ this.inner.restoreInteractionCheckpoints(saved);
9696
+ } else {
9697
+ this.inner.restoreInteractionCheckpoint?.(saved[saved.length - 1]);
9698
+ }
9699
+ }
9425
9700
  getInteractionCheckpoint() {
9426
9701
  return this.inner.getInteractionCheckpoint?.();
9427
9702
  }
@@ -9698,7 +9973,8 @@ var OrchestratingDriver = class _OrchestratingDriver extends BaseDriverDecorator
9698
9973
  })
9699
9974
  );
9700
9975
  const routingInput = delegated.delegate ? frameDelegatedContent(input, delegated.delegateFraming) : input;
9701
- const pinned = this.resolvePinnedAgent();
9976
+ const pin = options?.agent;
9977
+ const pinned = this.resolvePinnedAgent(pin);
9702
9978
  if (!pinned) this.dispatchEvent(new CustomEvent("orchestrating-start"));
9703
9979
  let routed;
9704
9980
  if (pinned) {
@@ -9716,7 +9992,7 @@ var OrchestratingDriver = class _OrchestratingDriver extends BaseDriverDecorator
9716
9992
  let lastResult;
9717
9993
  while (true) {
9718
9994
  if (this.cancelled) break;
9719
- await this.installAgent(currentAgent);
9995
+ await this.installAgent(currentAgent, pin);
9720
9996
  if (this.cancelled) break;
9721
9997
  let result;
9722
9998
  if (isHandoff) {
@@ -9852,9 +10128,13 @@ var OrchestratingDriver = class _OrchestratingDriver extends BaseDriverDecorator
9852
10128
  }
9853
10129
  return winner ? { ...winner, agentName, basis: "reachable-max" } : void 0;
9854
10130
  }
9855
- async installAgent(agent) {
10131
+ /**
10132
+ * `pin` stands in for the user-pin when given, as for
10133
+ * {@link OrchestratingDriver.resolvePinnedAgent}.
10134
+ */
10135
+ async installAgent(agent, pin) {
9856
10136
  const { previousAgent } = await this.agentSession.activate(agent);
9857
- const isTerminal = isFallback(agent) || this.agentSession.getPinned() !== null || this.agentSession.getFlowOwner() !== null || !!(agent.onActivate || agent.onDeactivate);
10137
+ const isTerminal = isFallback(agent) || (pin === void 0 ? this.agentSession.getPinned() : pin) !== null || this.agentSession.getFlowOwner() !== null || !!(agent.onActivate || agent.onDeactivate);
9858
10138
  let agentToApply = agent;
9859
10139
  if (!isTerminal) {
9860
10140
  const declaredTools = agent.toolDefinitions;
@@ -10095,9 +10375,11 @@ ${recentMessages}` : ""}`;
10095
10375
  *
10096
10376
  * Logs and returns `undefined` if a name no longer matches the agents array
10097
10377
  * — protects against stale state after an agents-array reassignment.
10378
+ *
10379
+ * `pin` stands in for the user-pin when given — `SendMessageOptions.agent`.
10098
10380
  */
10099
- resolvePinnedAgent() {
10100
- const name = this.agentSession.getLockedName();
10381
+ resolvePinnedAgent(pin) {
10382
+ const name = this.agentSession.getLockedName(pin);
10101
10383
  if (name === null) return void 0;
10102
10384
  const match = this.agents.find((a) => a.name === name);
10103
10385
  if (!match) {
@@ -10136,6 +10418,68 @@ function defineAgent(config) {
10136
10418
  return config;
10137
10419
  }
10138
10420
 
10421
+ // src/autopilot/autopilot-tools.ts
10422
+ var WIDGET_READ = "widget_read";
10423
+ var WIDGET_ACT = "widget_act";
10424
+ var HAND_BACK = "hand_back";
10425
+ var AUTOPILOT_TOOL_DEFINITIONS = [
10426
+ {
10427
+ name: WIDGET_READ,
10428
+ description: "Read the current decision as it stands now: its state (one of its views, if it lists views; without one, its facts or the view shown as Now), and its actions with their numbers and whether each is available. Use it when the decision message may be out of date, or to read a view it lists.",
10429
+ parameters: {
10430
+ type: "object",
10431
+ properties: {
10432
+ view: { type: "string", description: "One of the views the decision lists, if any." }
10433
+ }
10434
+ }
10435
+ },
10436
+ {
10437
+ name: WIDGET_ACT,
10438
+ description: 'Act on the current decision, as the user would by clicking. `action` is the number of an option from the decision\'s list (e.g. "1"), or a named action from that list: "select" with `options` to choose several, "reply" with `text` to answer in your own words, any action listed as name({text}) with its argument in `text`, and any listed as name({fields}) with `fields`. If it settles the decision, your turn ends. `note` is shown to the user as the assistant speaking, before the outcome: one short sentence in the first person saying what you chose and why, e.g. "Your brief has no external feeds, so I\'ll skip Data Feeds." Never mention an autopilot or a second agent.',
10439
+ parameters: {
10440
+ type: "object",
10441
+ required: ["action"],
10442
+ properties: {
10443
+ action: {
10444
+ type: "string",
10445
+ description: 'An option number from the list, starting at 1, or a named action ("select", "reply").'
10446
+ },
10447
+ options: {
10448
+ type: "array",
10449
+ items: { type: "string" },
10450
+ description: 'For "select": the options to choose, by number or label.'
10451
+ },
10452
+ text: {
10453
+ type: "string",
10454
+ description: `For "reply" (your answer, in the user's place) and any action listed as name({text}): its one argument, as plain text.`
10455
+ },
10456
+ fields: {
10457
+ type: "string",
10458
+ description: "Only for an action listed as name({fields}): a JSON object, as text, matching the fields schema the decision lists for it."
10459
+ },
10460
+ note: {
10461
+ type: "string",
10462
+ description: "Shown to the user as the assistant speaking. One sentence, first person."
10463
+ }
10464
+ }
10465
+ }
10466
+ },
10467
+ {
10468
+ name: HAND_BACK,
10469
+ description: 'Give this one decision to the user, when it rests on something only they can know and neither their brief nor their own answers settle it, or when nothing on it can be done (every action unavailable). Your turn ends and the user sees the question. `reason` is shown to them as the assistant speaking, after "I need your input on this one:" \u2014 say plainly what you need from them. Never mention an autopilot or a second agent.',
10470
+ parameters: {
10471
+ type: "object",
10472
+ required: ["reason"],
10473
+ properties: {
10474
+ reason: {
10475
+ type: "string",
10476
+ description: "What the user needs to decide, and why it is theirs."
10477
+ }
10478
+ }
10479
+ }
10480
+ }
10481
+ ];
10482
+
10139
10483
  // src/config/define-stateful-agent.ts
10140
10484
  function defaultStatefulDebugSnapshot(state) {
10141
10485
  if (!state || typeof state !== "object") return state;
@@ -10335,6 +10679,23 @@ function defineStatefulAgent(opts) {
10335
10679
  };
10336
10680
  }
10337
10681
  function defineAutopilot(config) {
10682
+ const seen = new Set(AUTOPILOT_TOOL_DEFINITIONS.map((d) => d.name));
10683
+ for (const d of config.tools?.definitions ?? []) {
10684
+ if (seen.has(d.name)) {
10685
+ throw new Error(`defineAutopilot: the tool "${d.name}" is named twice.`);
10686
+ }
10687
+ seen.add(d.name);
10688
+ }
10689
+ for (const set of config.widgetTools ?? []) {
10690
+ for (const name of Object.keys(set.tools)) {
10691
+ if (seen.has(name)) {
10692
+ throw new Error(
10693
+ `defineAutopilot: the widget tool "${name}" (for "${set.componentName}") is named twice.`
10694
+ );
10695
+ }
10696
+ seen.add(name);
10697
+ }
10698
+ }
10338
10699
  return config;
10339
10700
  }
10340
10701