agents 0.26.0 → 0.27.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 (233) hide show
  1. package/README.md +8 -9
  2. package/dist/{agent-routing-0DR0Tk2v.d.ts → agent-routing-Bhdbvf7s.d.ts} +13 -8
  3. package/dist/agent-routing.d.ts +1 -1
  4. package/dist/agent-tool-types.d.ts +1 -1
  5. package/dist/{agent-tools-YrATZJu2.d.ts → agent-tools-bJZwBMfq.d.ts} +2 -2
  6. package/dist/agent-tools.d.ts +1 -1
  7. package/dist/ai-sdk-OoGkFy3s.js +2444 -0
  8. package/dist/ai-sdk-OoGkFy3s.js.map +1 -0
  9. package/dist/apply-chunks-1FheRb9W.js +99 -0
  10. package/dist/apply-chunks-1FheRb9W.js.map +1 -0
  11. package/dist/broadcast-state-CEyI4uAK.js +69 -0
  12. package/dist/broadcast-state-CEyI4uAK.js.map +1 -0
  13. package/dist/browser/ai-sdk.d.ts +1 -1
  14. package/dist/browser/ai-sdk.js +2 -2
  15. package/dist/browser/ai-sdk.js.map +1 -1
  16. package/dist/browser/index.d.ts +1 -1
  17. package/dist/browser/tanstack-ai.d.ts +1 -1
  18. package/dist/browser/tanstack-ai.js +1 -1
  19. package/dist/{browser-DE3d-RLk.d.ts → browser-tmkcAAI3.d.ts} +2 -2
  20. package/dist/{browser-tool-DuNdlwZ9.d.ts → browser-tool-Bn-nzJQI.d.ts} +3 -3
  21. package/dist/{browser-tool-CQ10e6Kj.js → browser-tool-CBqs6hqa.js} +58 -18
  22. package/dist/browser-tool-CBqs6hqa.js.map +1 -0
  23. package/dist/capability-B4WbF81e.js.map +1 -1
  24. package/dist/{capability-runner-DBAnGr6L.d.ts → capability-runner-O5VbQzqg.d.ts} +9 -1
  25. package/dist/channel-Bak_6zDP.d.ts +333 -0
  26. package/dist/chat/index.d.ts +11 -9
  27. package/dist/chat/index.js +5 -194
  28. package/dist/chat/index.js.map +1 -1
  29. package/dist/chat/react.d.ts +2 -4
  30. package/dist/chat/react.js +3 -1
  31. package/dist/chat/react.js.map +1 -1
  32. package/dist/chat-sdk/index.d.ts +1 -1
  33. package/dist/chat-sdk/index.js +1 -1
  34. package/dist/cli.js +144 -0
  35. package/dist/{client-fyQrLGkD.js → client-1tEQ1Oqz.js} +27 -2
  36. package/dist/{client-fyQrLGkD.js.map → client-1tEQ1Oqz.js.map} +1 -1
  37. package/dist/{client-je3koD9Z.d.ts → client-DS3Dz9l0.d.ts} +2 -2
  38. package/dist/client-tools-aIBO0Fk7.d.ts +53 -0
  39. package/dist/client.d.ts +1 -1
  40. package/dist/conversations-DH0ut_8n.d.ts +192 -0
  41. package/dist/{current-agent-vAk_5JMt.d.ts → current-agent-C07C5o5Y.d.ts} +3 -3
  42. package/dist/daemon-core-Buubj-y1.js +481 -0
  43. package/dist/daemon-core-Buubj-y1.js.map +1 -0
  44. package/dist/daemon-core-CbohVPAu.d.ts +132 -0
  45. package/dist/{do-oauth-client-provider-Tmf1vgKz.d.ts → do-oauth-client-provider-DfsUcpc3.d.ts} +7 -1
  46. package/dist/{durable-object-lifecycle-HhgOMWq9.d.ts → durable-object-lifecycle-DRqp475_.d.ts} +2 -2
  47. package/dist/{channels/email.d.ts → experimental/channels/email/index.d.ts} +25 -12
  48. package/dist/{channels/email.js → experimental/channels/email/index.js} +13 -35
  49. package/dist/experimental/channels/email/index.js.map +1 -0
  50. package/dist/experimental/channels/index.d.ts +270 -0
  51. package/dist/experimental/channels/index.js +1186 -0
  52. package/dist/experimental/channels/index.js.map +1 -0
  53. package/dist/{channels/slack.d.ts → experimental/channels/slack/index.d.ts} +19 -9
  54. package/dist/experimental/channels/slack/index.js +267 -0
  55. package/dist/experimental/channels/slack/index.js.map +1 -0
  56. package/dist/{channels/telegram.d.ts → experimental/channels/telegram/index.d.ts} +19 -9
  57. package/dist/experimental/channels/telegram/index.js +198 -0
  58. package/dist/experimental/channels/telegram/index.js.map +1 -0
  59. package/dist/experimental/channels/web/ai-sdk.d.ts +55 -0
  60. package/dist/experimental/channels/web/ai-sdk.js +266 -0
  61. package/dist/experimental/channels/web/ai-sdk.js.map +1 -0
  62. package/dist/experimental/channels/web/client.d.ts +97 -0
  63. package/dist/experimental/channels/web/client.js +349 -0
  64. package/dist/experimental/channels/web/client.js.map +1 -0
  65. package/dist/experimental/channels/web/index.d.ts +189 -0
  66. package/dist/experimental/channels/web/index.js +332 -0
  67. package/dist/experimental/channels/web/index.js.map +1 -0
  68. package/dist/gateway-BDC07gYO.d.ts +100 -0
  69. package/dist/handle-zAzh0UW2.d.ts +460 -0
  70. package/dist/harness/ai-sdk/index.d.ts +175 -0
  71. package/dist/harness/ai-sdk/index.js +605 -0
  72. package/dist/harness/ai-sdk/index.js.map +1 -0
  73. package/dist/harness/container/daemon/index.d.ts +76 -0
  74. package/dist/harness/container/daemon/index.js +3 -0
  75. package/dist/harness/container/index.d.ts +740 -0
  76. package/dist/harness/container/index.js +2152 -0
  77. package/dist/harness/container/index.js.map +1 -0
  78. package/dist/harness/container/runtime/index.d.ts +144 -0
  79. package/dist/harness/container/runtime/index.js +1082 -0
  80. package/dist/harness/container/runtime/index.js.map +1 -0
  81. package/dist/harness/opencode/index.d.ts +369 -0
  82. package/dist/harness/opencode/index.js +1072 -0
  83. package/dist/harness/opencode/index.js.map +1 -0
  84. package/dist/harness/pi/index.d.ts +2 -1
  85. package/dist/harness/store/index.d.ts +24 -0
  86. package/dist/harness/store/index.js +2 -0
  87. package/dist/harness/think/index.d.ts +821 -0
  88. package/dist/harness/think/index.js +2529 -0
  89. package/dist/harness/think/index.js.map +1 -0
  90. package/dist/harness-B6B9S3rb.d.ts +203 -0
  91. package/dist/{images-iiTDSyRu.js → images-Ce9SCu3X.js} +2 -248
  92. package/dist/images-Ce9SCu3X.js.map +1 -0
  93. package/dist/{index-BreL0YrQ.d.ts → index-D0A5tbWu.d.ts} +2 -2
  94. package/dist/index-Ib_4dwOo.d.ts +200 -0
  95. package/dist/index.d.ts +5 -5
  96. package/dist/index.js +2 -2
  97. package/dist/ingress-EFbrS547.js +9 -0
  98. package/dist/ingress-EFbrS547.js.map +1 -0
  99. package/dist/input-schema-DNrpTu49.js +23 -0
  100. package/dist/input-schema-DNrpTu49.js.map +1 -0
  101. package/dist/internal-kmw70xwR.js +46 -0
  102. package/dist/internal-kmw70xwR.js.map +1 -0
  103. package/dist/internal_context.d.ts +1 -1
  104. package/dist/lifecycle/index.d.ts +3 -3
  105. package/dist/mcp/client/do-oauth-client-provider.d.ts +1 -1
  106. package/dist/mcp/client/do-oauth-client-provider.js +9 -0
  107. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -1
  108. package/dist/mcp/client/index.d.ts +1 -1
  109. package/dist/mcp/client/index.js +1 -1
  110. package/dist/mcp/client/x402.d.ts +35 -4
  111. package/dist/mcp/client/x402.js +24 -3
  112. package/dist/mcp/client/x402.js.map +1 -1
  113. package/dist/mcp/index.d.ts +1 -1
  114. package/dist/mcp/index.js +2 -2
  115. package/dist/models/ai-sdk/index.d.ts +16 -14
  116. package/dist/models/ai-sdk/index.js +1 -2442
  117. package/dist/models/opencode/index.d.ts +65 -0
  118. package/dist/models/opencode/index.js +285 -0
  119. package/dist/models/opencode/index.js.map +1 -0
  120. package/dist/models/pi-ai/index.d.ts +9 -7
  121. package/dist/models/pi-ai/index.js +2 -1
  122. package/dist/models/pi-ai/index.js.map +1 -1
  123. package/dist/options-pwTNq9z6.d.ts +134 -0
  124. package/dist/protocol-BEgrJYC5.d.ts +331 -0
  125. package/dist/protocol-CukuI73M.d.ts +386 -0
  126. package/dist/protocol-cOklVBBD.js +227 -0
  127. package/dist/protocol-cOklVBBD.js.map +1 -0
  128. package/dist/{protocol-kHsr-f0X.js → protocol-eXeJxhgu.js} +2 -67
  129. package/dist/protocol-eXeJxhgu.js.map +1 -0
  130. package/dist/protocol-y2sSY584.js +148 -0
  131. package/dist/protocol-y2sSY584.js.map +1 -0
  132. package/dist/queue/index.d.ts +1 -1
  133. package/dist/react.d.ts +2 -2
  134. package/dist/routing/index.d.ts +2 -2
  135. package/dist/{scheduler-RdACNXDP.d.ts → scheduler-CrFCnv_J.d.ts} +2 -2
  136. package/dist/schedules/index.d.ts +1 -1
  137. package/dist/serializable.d.ts +1 -1
  138. package/dist/sessions/index.d.ts +11 -434
  139. package/dist/sessions/index.js +1 -2271
  140. package/dist/sessions-B9pNuxdT.js +2272 -0
  141. package/dist/sessions-B9pNuxdT.js.map +1 -0
  142. package/dist/settings-C45hDliX.js +253 -0
  143. package/dist/settings-C45hDliX.js.map +1 -0
  144. package/dist/{transport-PfFnt-Ld.d.ts → settings-NgGwF7Qt.d.ts} +14 -208
  145. package/dist/{src-BZeu-SCD.js → src-DgCbZtIR.js} +4 -4
  146. package/dist/{src-BZeu-SCD.js.map → src-DgCbZtIR.js.map} +1 -1
  147. package/dist/stall-watchdog-BpRF2uwG.js +195 -0
  148. package/dist/stall-watchdog-BpRF2uwG.js.map +1 -0
  149. package/dist/state/index.d.ts +1 -1
  150. package/dist/store-DLeqKuj3.d.ts +310 -0
  151. package/dist/store-vgcPYo8K.js +432 -0
  152. package/dist/store-vgcPYo8K.js.map +1 -0
  153. package/dist/streams/index.d.ts +1 -1
  154. package/dist/streams/index.js +1 -1
  155. package/dist/{streams-DW-UCOUc.js → streams-CITClVer.js} +16 -3
  156. package/dist/streams-CITClVer.js.map +1 -0
  157. package/dist/{streams-Cmkj9QfW.d.ts → streams-fbG61qM8.d.ts} +2 -2
  158. package/dist/sub-routing.d.ts +1 -1
  159. package/dist/tasks/index.d.ts +1 -1
  160. package/dist/tasks/index.js +1 -1
  161. package/dist/{tasks-68DESxf0.js → tasks-CkTZNaV1.js} +2 -1
  162. package/dist/tasks-CkTZNaV1.js.map +1 -0
  163. package/dist/{tasks-9YxYtByh.d.ts → tasks-PtlB4Uyn.d.ts} +2 -2
  164. package/dist/tool-3PA8xjB_.js +421 -0
  165. package/dist/tool-3PA8xjB_.js.map +1 -0
  166. package/dist/tool-BPJEbwaX.d.ts +278 -0
  167. package/dist/transport-k2mFrtL8.d.ts +200 -0
  168. package/dist/turns-DCV3ycDl.js +317 -0
  169. package/dist/turns-DCV3ycDl.js.map +1 -0
  170. package/dist/voice/client.js +1 -1
  171. package/dist/voice/index.d.ts +2 -2
  172. package/dist/voice/index.js +1 -1
  173. package/dist/websearch/index.d.ts +58 -0
  174. package/dist/websearch/index.js +2 -0
  175. package/dist/websearch/tools/ai-sdk.d.ts +77 -0
  176. package/dist/websearch/tools/ai-sdk.js +46 -0
  177. package/dist/websearch/tools/ai-sdk.js.map +1 -0
  178. package/dist/websearch/tools/pi.d.ts +82 -0
  179. package/dist/websearch/tools/pi.js +89 -0
  180. package/dist/websearch/tools/pi.js.map +1 -0
  181. package/dist/websearch/tools/tanstack-ai.d.ts +71 -0
  182. package/dist/websearch/tools/tanstack-ai.js +46 -0
  183. package/dist/websearch/tools/tanstack-ai.js.map +1 -0
  184. package/dist/websockets/index.d.ts +12 -296
  185. package/dist/websockets/index.js +1 -1
  186. package/dist/{websockets-Ci-LZFHH.js → websockets-CmENGb-Z.js} +18 -4
  187. package/dist/websockets-CmENGb-Z.js.map +1 -0
  188. package/dist/{wire-types-CrjKvDyi.d.ts → wire-types-Cv0iwjk5.d.ts} +3 -51
  189. package/dist/workflow-types.d.ts +1 -1
  190. package/dist/workflows.d.ts +1 -1
  191. package/dist/workflows.js +1 -1
  192. package/docs/browse-the-web.md +3 -0
  193. package/docs/harnesses/think.md +215 -0
  194. package/docs/index.md +2 -1
  195. package/docs/lifecycle.md +40 -0
  196. package/docs/search-the-web.md +133 -0
  197. package/package.json +117 -38
  198. package/dist/browser-tool-CQ10e6Kj.js.map +0 -1
  199. package/dist/channel-Bnm4S7T2.d.ts +0 -491
  200. package/dist/channels/ai-sdk.d.ts +0 -46
  201. package/dist/channels/ai-sdk.js +0 -120
  202. package/dist/channels/ai-sdk.js.map +0 -1
  203. package/dist/channels/email.js.map +0 -1
  204. package/dist/channels/index.d.ts +0 -233
  205. package/dist/channels/index.js +0 -608
  206. package/dist/channels/index.js.map +0 -1
  207. package/dist/channels/slack.js +0 -614
  208. package/dist/channels/slack.js.map +0 -1
  209. package/dist/channels/tanstack-ai.d.ts +0 -39
  210. package/dist/channels/tanstack-ai.js +0 -17
  211. package/dist/channels/tanstack-ai.js.map +0 -1
  212. package/dist/channels/telegram.js +0 -427
  213. package/dist/channels/telegram.js.map +0 -1
  214. package/dist/channels/voice.d.ts +0 -45
  215. package/dist/channels/voice.js +0 -122
  216. package/dist/channels/voice.js.map +0 -1
  217. package/dist/images-iiTDSyRu.js.map +0 -1
  218. package/dist/index-BB0kqhIz.d.ts +0 -101
  219. package/dist/ingress-BfetZbMO.js +0 -83
  220. package/dist/ingress-BfetZbMO.js.map +0 -1
  221. package/dist/internal-CYlgHl1l.js +0 -59
  222. package/dist/internal-CYlgHl1l.js.map +0 -1
  223. package/dist/models/ai-sdk/index.js.map +0 -1
  224. package/dist/protocol-kHsr-f0X.js.map +0 -1
  225. package/dist/sessions/index.js.map +0 -1
  226. package/dist/streams-DW-UCOUc.js.map +0 -1
  227. package/dist/surface-bZZJqBka.js +0 -17
  228. package/dist/surface-bZZJqBka.js.map +0 -1
  229. package/dist/tasks-68DESxf0.js.map +0 -1
  230. package/dist/tool-schema-CBjGPrsQ.js +0 -31
  231. package/dist/tool-schema-CBjGPrsQ.js.map +0 -1
  232. package/dist/websockets-Ci-LZFHH.js.map +0 -1
  233. package/docs/channels.md +0 -323
@@ -0,0 +1,605 @@
1
+ import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, r as _assertClassBrand, t as _classPrivateFieldGet2 } from "../../classPrivateFieldGet2-DZBYAB34.js";
2
+ import { t as _classPrivateMethodInitSpec } from "../../classPrivateMethodInitSpec-qMjJ6sHQ.js";
3
+ import { t as LifecycleCapability } from "../../capability-B4WbF81e.js";
4
+ import { a as toTranscriptMessage, i as toResponseChunks, n as awaitsInput, o as toUIMessage, r as toResponseChunk, s as toUIMessageChunk, t as answerToolCall } from "../../turns-DCV3ycDl.js";
5
+ import { convertToModelMessages, jsonSchema, stepCountIs, streamText, toUIMessageStream, tool } from "ai";
6
+ //#region src/harness/ai-sdk/harness.ts
7
+ const PREFIX = "ai-sdk-harness:";
8
+ const WAKE_FN = "wake";
9
+ const DEFAULT_SESSION = "main";
10
+ const HEARTBEAT_MS = 3e4;
11
+ var _options = /* @__PURE__ */ new WeakMap();
12
+ var _heartbeatMs = /* @__PURE__ */ new WeakMap();
13
+ var _runs = /* @__PURE__ */ new WeakMap();
14
+ var _driving = /* @__PURE__ */ new WeakMap();
15
+ var _watchers = /* @__PURE__ */ new WeakMap();
16
+ var _waiters = /* @__PURE__ */ new WeakMap();
17
+ var _AiSdkHarness_brand = /* @__PURE__ */ new WeakSet();
18
+ /**
19
+ * An agent harness around the AI SDK's `streamText`, hosted in a Durable
20
+ * Object: sessions, their transcripts, and a queue of operations, all in the
21
+ * object's storage. One operation runs at a time per session; a run is one
22
+ * `streamText` call over the session's transcript.
23
+ *
24
+ * The AI SDK keeps no state of its own, so a run that an eviction cuts short
25
+ * starts again from the saved transcript, and its unsaved output is lost.
26
+ * Steering is not supported: `whenBusy: "steer"` queues like a follow-up.
27
+ *
28
+ * @experimental The API may change between releases.
29
+ */
30
+ var AiSdkHarness = class extends LifecycleCapability {
31
+ constructor(options) {
32
+ super("ai-sdk-harness");
33
+ _classPrivateMethodInitSpec(this, _AiSdkHarness_brand);
34
+ _classPrivateFieldInitSpec(this, _options, void 0);
35
+ _classPrivateFieldInitSpec(this, _heartbeatMs, void 0);
36
+ _classPrivateFieldInitSpec(this, _runs, /* @__PURE__ */ new Map());
37
+ _classPrivateFieldInitSpec(this, _driving, /* @__PURE__ */ new Map());
38
+ _classPrivateFieldInitSpec(this, _watchers, /* @__PURE__ */ new Map());
39
+ _classPrivateFieldInitSpec(this, _waiters, /* @__PURE__ */ new Map());
40
+ _classPrivateFieldSet2(_options, this, options);
41
+ _classPrivateFieldSet2(_heartbeatMs, this, options.heartbeatMs ?? HEARTBEAT_MS);
42
+ this.sessions = {
43
+ create: async () => _assertClassBrand(_AiSdkHarness_brand, this, _create).call(this, { id: crypto.randomUUID() }),
44
+ fork: async (from) => {
45
+ _assertClassBrand(_AiSdkHarness_brand, this, _ensure).call(this, from);
46
+ const session = _assertClassBrand(_AiSdkHarness_brand, this, _create).call(this, {
47
+ id: crypto.randomUUID(),
48
+ parent: from
49
+ });
50
+ for (const message of _assertClassBrand(_AiSdkHarness_brand, this, _messages).call(this, from)) _assertClassBrand(_AiSdkHarness_brand, this, _putMessage).call(this, session.id, message);
51
+ return session;
52
+ },
53
+ list: async () => {
54
+ _assertClassBrand(_AiSdkHarness_brand, this, _ensure).call(this, DEFAULT_SESSION);
55
+ return [..._get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).list({ prefix: `${PREFIX}s:` })].map(([, { id, parent }]) => ({
56
+ id,
57
+ ...parent !== void 0 && { parent },
58
+ busy: _assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, id).length > 0
59
+ }));
60
+ }
61
+ };
62
+ }
63
+ async onStart() {
64
+ const sessions = [..._get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).list({ prefix: `${PREFIX}s:` })];
65
+ for (const [, session] of sessions) if (_assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, session.id).length > 0) await _assertClassBrand(_AiSdkHarness_brand, this, _wake).call(this, session.id);
66
+ }
67
+ async onJob(context) {
68
+ if (context.job.fn !== WAKE_FN) return;
69
+ const session = context.job.payload?.session;
70
+ if (typeof session !== "string") return;
71
+ if (!_classPrivateFieldGet2(_driving, this).has(session) && _assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, session).length === 0) return;
72
+ this.lifecycle.trackAlarmWork(_assertClassBrand(_AiSdkHarness_brand, this, _drive).call(this, session));
73
+ return { rescheduleAt: Date.now() + _classPrivateFieldGet2(_heartbeatMs, this) };
74
+ }
75
+ session(id = DEFAULT_SESSION) {
76
+ return {
77
+ id,
78
+ submit: (input, options) => _assertClassBrand(_AiSdkHarness_brand, this, _submit).call(this, id, input, options),
79
+ abort: (operationId) => _assertClassBrand(_AiSdkHarness_brand, this, _abort).call(this, id, operationId),
80
+ wait: (operationId, signal) => _assertClassBrand(_AiSdkHarness_brand, this, _wait).call(this, id, operationId, signal),
81
+ reset: (handoff) => _assertClassBrand(_AiSdkHarness_brand, this, _reset).call(this, id, handoff),
82
+ watch: async () => _assertClassBrand(_AiSdkHarness_brand, this, _watch).call(this, id)
83
+ };
84
+ }
85
+ };
86
+ async function _submit(session, input, options = {}) {
87
+ _assertClassBrand(_AiSdkHarness_brand, this, _ensure).call(this, session);
88
+ const operationId = options.operationId ?? crypto.randomUUID();
89
+ if (_assertClassBrand(_AiSdkHarness_brand, this, _operation).call(this, session, operationId)) return {
90
+ operationId,
91
+ session,
92
+ accepted: false
93
+ };
94
+ const op = {
95
+ operationId,
96
+ seq: _assertClassBrand(_AiSdkHarness_brand, this, _nextSeq).call(this, session),
97
+ input,
98
+ status: "queued"
99
+ };
100
+ _assertClassBrand(_AiSdkHarness_brand, this, _saveOperation).call(this, session, op);
101
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
102
+ type: "operation",
103
+ status: {
104
+ operationId,
105
+ status: "queued"
106
+ }
107
+ }]);
108
+ await _assertClassBrand(_AiSdkHarness_brand, this, _wake).call(this, session);
109
+ _assertClassBrand(_AiSdkHarness_brand, this, _drive).call(this, session);
110
+ return {
111
+ operationId,
112
+ session,
113
+ accepted: true
114
+ };
115
+ }
116
+ async function _abort(session, operationId) {
117
+ const run = _classPrivateFieldGet2(_runs, this).get(session);
118
+ const targets = operationId ? [_assertClassBrand(_AiSdkHarness_brand, this, _operation).call(this, session, operationId)].filter((op) => op !== void 0 && isPending(op)) : _assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, session);
119
+ for (const op of targets) if (run?.operations.includes(op.operationId)) run.abort.abort();
120
+ else _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, {
121
+ status: "unanswered",
122
+ reason: "withdrawn"
123
+ });
124
+ return targets.length > 0;
125
+ }
126
+ async function _wait(session, operationId, signal) {
127
+ const op = _assertClassBrand(_AiSdkHarness_brand, this, _operation).call(this, session, operationId);
128
+ if (!op) return {
129
+ operationId,
130
+ session,
131
+ status: "unanswered",
132
+ reason: "not_found"
133
+ };
134
+ const settled = isPending(op) ? await new Promise((resolve, reject) => {
135
+ if (signal?.aborted) {
136
+ reject(signal.reason);
137
+ return;
138
+ }
139
+ const key = waiterKey(session, operationId);
140
+ const waiters = _classPrivateFieldGet2(_waiters, this).get(key) ?? /* @__PURE__ */ new Set();
141
+ _classPrivateFieldGet2(_waiters, this).set(key, waiters);
142
+ const done = (result) => {
143
+ signal?.removeEventListener("abort", aborted);
144
+ resolve(result);
145
+ };
146
+ const aborted = () => {
147
+ waiters.delete(done);
148
+ reject(signal?.reason);
149
+ };
150
+ waiters.add(done);
151
+ signal?.addEventListener("abort", aborted, { once: true });
152
+ }) : op;
153
+ return settled.status === "done" ? {
154
+ operationId,
155
+ session,
156
+ status: "done",
157
+ ...settled.text !== void 0 && { text: settled.text }
158
+ } : {
159
+ operationId,
160
+ session,
161
+ status: "unanswered",
162
+ ...settled.reason !== void 0 && { reason: settled.reason }
163
+ };
164
+ }
165
+ async function _reset(session, handoff) {
166
+ _assertClassBrand(_AiSdkHarness_brand, this, _ensure).call(this, session);
167
+ await _assertClassBrand(_AiSdkHarness_brand, this, _abort).call(this, session);
168
+ await _classPrivateFieldGet2(_driving, this).get(session);
169
+ for (const [key] of _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).list({ prefix: messagePrefix(session) })) _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).delete(key);
170
+ const events = [{ type: "reset" }];
171
+ if (handoff) {
172
+ const note = {
173
+ id: crypto.randomUUID(),
174
+ role: "system",
175
+ parts: [{
176
+ type: "text",
177
+ text: handoff
178
+ }]
179
+ };
180
+ _assertClassBrand(_AiSdkHarness_brand, this, _putMessage).call(this, session, note);
181
+ events.push({
182
+ type: "message",
183
+ message: _assertClassBrand(_AiSdkHarness_brand, this, _transcript).call(this, note)
184
+ });
185
+ }
186
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, events);
187
+ }
188
+ function _watch(session) {
189
+ _assertClassBrand(_AiSdkHarness_brand, this, _ensure).call(this, session);
190
+ const run = _classPrivateFieldGet2(_runs, this).get(session);
191
+ const state = {
192
+ messages: _assertClassBrand(_AiSdkHarness_brand, this, _messages).call(this, session).map((m) => _assertClassBrand(_AiSdkHarness_brand, this, _transcript).call(this, m)),
193
+ pending: _assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, session).map((op) => ({
194
+ operationId: op.operationId,
195
+ status: op.status === "placed" ? "placed" : "queued"
196
+ })),
197
+ ...run && { run: {
198
+ operations: [...run.operations],
199
+ ...run.partial.length > 0 && { partial: [...run.partial] }
200
+ } }
201
+ };
202
+ const watchers = _classPrivateFieldGet2(_watchers, this).get(session) ?? /* @__PURE__ */ new Set();
203
+ _classPrivateFieldGet2(_watchers, this).set(session, watchers);
204
+ const watcher = new Watcher(state, () => watchers.delete(watcher));
205
+ watchers.add(watcher);
206
+ return watcher;
207
+ }
208
+ /** Run the session's operations in order until none are left. */
209
+ function _drive(session) {
210
+ let driving = _classPrivateFieldGet2(_driving, this).get(session);
211
+ if (!driving) {
212
+ driving = (async () => {
213
+ try {
214
+ for (;;) {
215
+ const next = _assertClassBrand(_AiSdkHarness_brand, this, _pending).call(this, session)[0];
216
+ if (!next) return;
217
+ await _assertClassBrand(_AiSdkHarness_brand, this, _run).call(this, session, next);
218
+ }
219
+ } finally {
220
+ _classPrivateFieldGet2(_driving, this).delete(session);
221
+ }
222
+ })();
223
+ _classPrivateFieldGet2(_driving, this).set(session, driving);
224
+ }
225
+ return driving;
226
+ }
227
+ async function _run(session, op) {
228
+ const { operationId } = op;
229
+ let owner = "parts" in op.input ? op.input.from?.participantId : void 0;
230
+ if (op.status === "queued") {
231
+ let placed;
232
+ if ("parts" in op.input) placed = {
233
+ id: op.input.messageId ?? operationId,
234
+ role: "user",
235
+ parts: op.input.parts.map(toUIPart)
236
+ };
237
+ else {
238
+ const answered = _assertClassBrand(_AiSdkHarness_brand, this, _answer).call(this, session, op.input);
239
+ if (typeof answered === "string") {
240
+ _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, {
241
+ status: "unanswered",
242
+ reason: answered
243
+ });
244
+ return;
245
+ }
246
+ placed = answered;
247
+ }
248
+ _assertClassBrand(_AiSdkHarness_brand, this, _putMessage).call(this, session, placed);
249
+ op = {
250
+ ...op,
251
+ status: "placed"
252
+ };
253
+ _assertClassBrand(_AiSdkHarness_brand, this, _saveOperation).call(this, session, op);
254
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
255
+ type: "message",
256
+ message: _assertClassBrand(_AiSdkHarness_brand, this, _transcript).call(this, placed)
257
+ }, {
258
+ type: "operation",
259
+ status: {
260
+ operationId,
261
+ status: "placed"
262
+ }
263
+ }]);
264
+ }
265
+ const messages = _assertClassBrand(_AiSdkHarness_brand, this, _messages).call(this, session);
266
+ const last = messages.at(-1);
267
+ if (last?.role === "assistant") {
268
+ owner = last.metadata?.owner;
269
+ if (awaitsInput$1(last)) {
270
+ _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, { status: "done" });
271
+ return;
272
+ }
273
+ }
274
+ const run = {
275
+ operations: [operationId],
276
+ partial: [],
277
+ abort: new AbortController()
278
+ };
279
+ _classPrivateFieldGet2(_runs, this).set(session, run);
280
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
281
+ type: "run-start",
282
+ operations: [operationId]
283
+ }]);
284
+ const { model, tools, system } = _classPrivateFieldGet2(_options, this);
285
+ let message;
286
+ let error;
287
+ try {
288
+ const stream = toUIMessageStream({
289
+ stream: streamText({
290
+ model,
291
+ ...system !== void 0 && { system },
292
+ ...tools && { tools },
293
+ stopWhen: _classPrivateFieldGet2(_options, this).stopWhen ?? stepCountIs(5),
294
+ messages: await convertToModelMessages(messages, tools && { tools }),
295
+ abortSignal: run.abort.signal
296
+ }).stream,
297
+ ...tools && { tools },
298
+ originalMessages: messages,
299
+ generateMessageId: () => crypto.randomUUID(),
300
+ onEnd: ({ responseMessage }) => {
301
+ message = {
302
+ ...responseMessage,
303
+ metadata: {
304
+ ...responseMessage.metadata,
305
+ ...owner !== void 0 && { owner }
306
+ }
307
+ };
308
+ }
309
+ });
310
+ for await (const chunk of toResponseChunks(stream, {
311
+ ...tools && { tools },
312
+ ...owner !== void 0 && { owner }
313
+ })) {
314
+ run.partial.push(chunk);
315
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
316
+ type: "chunk",
317
+ chunk
318
+ }]);
319
+ }
320
+ } catch (caught) {
321
+ error = caught;
322
+ }
323
+ if (message) {
324
+ _assertClassBrand(_AiSdkHarness_brand, this, _putMessage).call(this, session, message);
325
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
326
+ type: "message",
327
+ message: _assertClassBrand(_AiSdkHarness_brand, this, _transcript).call(this, message)
328
+ }]);
329
+ }
330
+ _classPrivateFieldGet2(_runs, this).delete(session);
331
+ if (run.abort.signal.aborted) _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, {
332
+ status: "unanswered",
333
+ reason: "aborted"
334
+ });
335
+ else if (error !== void 0) {
336
+ console.error(`AI SDK run for "${operationId}" failed`, error);
337
+ _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, {
338
+ status: "unanswered",
339
+ reason: error instanceof Error ? error.message : "failed"
340
+ });
341
+ } else _assertClassBrand(_AiSdkHarness_brand, this, _settle).call(this, session, op, {
342
+ status: "done",
343
+ text: message ? textOf(message) : ""
344
+ });
345
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
346
+ type: "run-end",
347
+ operations: [operationId]
348
+ }]);
349
+ }
350
+ /** The message an answer updates, or why it is not wanted. */
351
+ function _answer(session, answer) {
352
+ const messages = _assertClassBrand(_AiSdkHarness_brand, this, _messages).call(this, session);
353
+ const event = answer.type === "approval" ? {
354
+ type: "approval-response",
355
+ approvalId: answer.approvalId,
356
+ approved: answer.approved,
357
+ ...answer.reason !== void 0 && { reason: answer.reason }
358
+ } : {
359
+ type: "tool-result",
360
+ toolCallId: answer.toolCallId,
361
+ result: answer.result
362
+ };
363
+ if (answer.type === "tool-result") {
364
+ const owner = messages.find((m) => m.parts.some((p) => "toolCallId" in p && p.toolCallId === answer.toolCallId))?.metadata?.owner;
365
+ const from = answer.from?.participantId;
366
+ if (owner !== void 0 && from !== void 0 && owner !== from) return "not_owner";
367
+ }
368
+ return answerToolCall(messages, event) ?? "not_waiting";
369
+ }
370
+ function _settle(session, op, result) {
371
+ const settled = {
372
+ ...op,
373
+ ...result
374
+ };
375
+ _assertClassBrand(_AiSdkHarness_brand, this, _saveOperation).call(this, session, settled);
376
+ const { operationId } = op;
377
+ _assertClassBrand(_AiSdkHarness_brand, this, _emit).call(this, session, [{
378
+ type: "operation",
379
+ status: result.status === "done" ? {
380
+ operationId,
381
+ status: "done",
382
+ ...result.text !== void 0 && { text: result.text }
383
+ } : {
384
+ operationId,
385
+ status: "unanswered",
386
+ reason: result.reason
387
+ }
388
+ }]);
389
+ const key = waiterKey(session, operationId);
390
+ for (const waiter of _classPrivateFieldGet2(_waiters, this).get(key) ?? []) waiter(settled);
391
+ _classPrivateFieldGet2(_waiters, this).delete(key);
392
+ }
393
+ function _wake(session) {
394
+ return this.lifecycle.jobs.push({
395
+ id: `${PREFIX}wake:${session}`,
396
+ fn: WAKE_FN,
397
+ time: Date.now(),
398
+ payload: { session },
399
+ singleflight: true
400
+ });
401
+ }
402
+ function _emit(session, events) {
403
+ for (const watcher of _classPrivateFieldGet2(_watchers, this).get(session) ?? []) watcher.push(events);
404
+ }
405
+ function _get_kv() {
406
+ return this.lifecycle.storage.kv;
407
+ }
408
+ function _create(session) {
409
+ _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(`${PREFIX}s:${session.id}`, session);
410
+ return this.session(session.id);
411
+ }
412
+ /**
413
+ * Sessions are created on first use, so any id names one. That is not an
414
+ * authorization gap: whoever reaches the agent may create sessions anyway.
415
+ */
416
+ function _ensure(session) {
417
+ if (_get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).get(`${PREFIX}s:${session}`) === void 0) _assertClassBrand(_AiSdkHarness_brand, this, _create).call(this, { id: session });
418
+ }
419
+ function _nextSeq(session) {
420
+ const key = `${PREFIX}seq:${session}`;
421
+ const seq = (_get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).get(key) ?? 0) + 1;
422
+ _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(key, seq);
423
+ return seq;
424
+ }
425
+ function _messages(session) {
426
+ return [..._get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).list({ prefix: messagePrefix(session) })].map(([, message]) => message);
427
+ }
428
+ /** Save a message, in place of any with its id. */
429
+ function _putMessage(session, message) {
430
+ const indexKey = `${PREFIX}mi:${JSON.stringify(session)}:${message.id}`;
431
+ let key = _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).get(indexKey);
432
+ if (key === void 0 || _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).get(key) === void 0) {
433
+ key = messagePrefix(session) + pad(_assertClassBrand(_AiSdkHarness_brand, this, _nextSeq).call(this, session));
434
+ _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(indexKey, key);
435
+ }
436
+ _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(key, message);
437
+ }
438
+ function _operation(session, operationId) {
439
+ return _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).get(operationKey(session, operationId));
440
+ }
441
+ function _saveOperation(session, op) {
442
+ _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(operationKey(session, op.operationId), op);
443
+ const pendingKey = `${pendingPrefix(session)}${pad(op.seq)}`;
444
+ if (isPending(op)) _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).put(pendingKey, op.operationId);
445
+ else _get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).delete(pendingKey);
446
+ }
447
+ /** Unsettled operations, oldest first. */
448
+ function _pending(session) {
449
+ return [..._get_kv.call(_assertClassBrand(_AiSdkHarness_brand, this)).list({ prefix: pendingPrefix(session) })].map(([, id]) => _assertClassBrand(_AiSdkHarness_brand, this, _operation).call(this, session, id)).filter((op) => op !== void 0);
450
+ }
451
+ function _transcript(message) {
452
+ const owner = message.metadata?.owner;
453
+ return toTranscriptMessage(message, {
454
+ ..._classPrivateFieldGet2(_options, this).tools && { tools: _classPrivateFieldGet2(_options, this).tools },
455
+ ...owner !== void 0 && { owner }
456
+ });
457
+ }
458
+ var _close = /* @__PURE__ */ new WeakMap();
459
+ var _listener = /* @__PURE__ */ new WeakMap();
460
+ var _queue = /* @__PURE__ */ new WeakMap();
461
+ var _draining = /* @__PURE__ */ new WeakMap();
462
+ var _stopped = /* @__PURE__ */ new WeakMap();
463
+ var _Watcher_brand = /* @__PURE__ */ new WeakSet();
464
+ /** One watch: events queue until `start`, then reach the listener in order. */
465
+ var Watcher = class {
466
+ constructor(state, onStop) {
467
+ this.state = state;
468
+ this.onStop = onStop;
469
+ _classPrivateMethodInitSpec(this, _Watcher_brand);
470
+ _classPrivateFieldInitSpec(this, _close, void 0);
471
+ _classPrivateFieldInitSpec(this, _listener, void 0);
472
+ _classPrivateFieldInitSpec(this, _queue, []);
473
+ _classPrivateFieldInitSpec(this, _draining, false);
474
+ _classPrivateFieldInitSpec(this, _stopped, false);
475
+ this.closed = new Promise((resolve) => {
476
+ _classPrivateFieldSet2(_close, this, resolve);
477
+ });
478
+ }
479
+ start(listener) {
480
+ _classPrivateFieldSet2(_listener, this, listener);
481
+ _assertClassBrand(_Watcher_brand, this, _drain).call(this);
482
+ }
483
+ async stop() {
484
+ _classPrivateFieldSet2(_stopped, this, true);
485
+ this.onStop();
486
+ _classPrivateFieldGet2(_close, this).call(this);
487
+ }
488
+ push(events) {
489
+ if (_classPrivateFieldGet2(_stopped, this)) return;
490
+ _classPrivateFieldGet2(_queue, this).push(events);
491
+ _assertClassBrand(_Watcher_brand, this, _drain).call(this);
492
+ }
493
+ };
494
+ async function _drain() {
495
+ if (!_classPrivateFieldGet2(_listener, this) || _classPrivateFieldGet2(_draining, this)) return;
496
+ _classPrivateFieldSet2(_draining, this, true);
497
+ try {
498
+ while (_classPrivateFieldGet2(_queue, this).length > 0 && !_classPrivateFieldGet2(_stopped, this)) {
499
+ const events = _classPrivateFieldGet2(_queue, this).shift();
500
+ try {
501
+ await _classPrivateFieldGet2(_listener, this).call(this, events);
502
+ } catch (error) {
503
+ console.error("A session watcher failed", error);
504
+ }
505
+ }
506
+ } finally {
507
+ _classPrivateFieldSet2(_draining, this, false);
508
+ }
509
+ }
510
+ function isPending(op) {
511
+ return op.status === "queued" || op.status === "placed";
512
+ }
513
+ /** Whether the message has a tool call waiting for a result or approval. */
514
+ function awaitsInput$1(message) {
515
+ return message.parts.some((part) => "toolCallId" in part && (part.state === "input-available" || part.state === "approval-requested"));
516
+ }
517
+ function textOf(message) {
518
+ return message.parts.map((part) => part.type === "text" ? part.text : "").join("");
519
+ }
520
+ function toUIPart(part) {
521
+ return part.type === "text" ? {
522
+ type: "text",
523
+ text: part.text
524
+ } : {
525
+ type: "file",
526
+ mediaType: part.mediaType,
527
+ url: part.url,
528
+ ...part.filename !== void 0 && { filename: part.filename }
529
+ };
530
+ }
531
+ function pad(seq) {
532
+ return String(seq).padStart(12, "0");
533
+ }
534
+ function messagePrefix(session) {
535
+ return `${PREFIX}m:${JSON.stringify(session)}:`;
536
+ }
537
+ function pendingPrefix(session) {
538
+ return `${PREFIX}p:${JSON.stringify(session)}:`;
539
+ }
540
+ function operationKey(session, operationId) {
541
+ return `${PREFIX}o:${JSON.stringify(session)}:${operationId}`;
542
+ }
543
+ function waiterKey(session, operationId) {
544
+ return JSON.stringify([session, operationId]);
545
+ }
546
+ //#endregion
547
+ //#region src/experimental/channels/tool-schema.ts
548
+ const channelMessageJsonSchema = {
549
+ type: "object",
550
+ properties: {
551
+ title: {
552
+ type: "string",
553
+ description: "Optional title or topic for the message"
554
+ },
555
+ markdown: {
556
+ type: "string",
557
+ minLength: 1,
558
+ description: "Message content formatted as Markdown"
559
+ }
560
+ },
561
+ required: ["markdown"],
562
+ additionalProperties: false
563
+ };
564
+ function parseChannelMessage(value) {
565
+ if (typeof value === "object" && value !== null) {
566
+ const candidate = value;
567
+ if (typeof candidate.markdown === "string" && candidate.markdown.length > 0 && (candidate.title === void 0 || typeof candidate.title === "string")) return {
568
+ ...typeof candidate.title === "string" ? { title: candidate.title } : {},
569
+ markdown: candidate.markdown
570
+ };
571
+ }
572
+ throw new Error("Expected an optional string title and a non-empty Markdown string");
573
+ }
574
+ //#endregion
575
+ //#region src/harness/ai-sdk/send-message-tool.ts
576
+ const channelMessageSchema = jsonSchema(channelMessageJsonSchema, { validate(value) {
577
+ try {
578
+ return {
579
+ success: true,
580
+ value: parseChannelMessage(value)
581
+ };
582
+ } catch (error) {
583
+ return {
584
+ success: false,
585
+ error: error instanceof Error ? error : new Error(String(error))
586
+ };
587
+ }
588
+ } });
589
+ /**
590
+ * Adapt one Host-resolved surface to an AI SDK tool.
591
+ *
592
+ * The caller chooses the key used in its ToolSet and owns model-facing policy
593
+ * such as the description, examples, metadata, and approval requirement.
594
+ */
595
+ function createSendMessageTool(gateway, surface, options = {}) {
596
+ return tool({
597
+ ...options,
598
+ inputSchema: channelMessageSchema,
599
+ execute: (message) => gateway.deliver(surface, message)
600
+ });
601
+ }
602
+ //#endregion
603
+ export { AiSdkHarness, answerToolCall, awaitsInput, createSendMessageTool, toResponseChunk, toResponseChunks, toTranscriptMessage, toUIMessage, toUIMessageChunk };
604
+
605
+ //# sourceMappingURL=index.js.map