agents 0.32.0 → 0.34.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 (187) hide show
  1. package/README.md +1 -1
  2. package/dist/{agent-routing-vVMA7m8Y.d.ts → agent-routing-BSoZzNiP.d.ts} +537 -373
  3. package/dist/agent-routing.d.ts +1 -1
  4. package/dist/agent-tool-types.d.ts +1 -1
  5. package/dist/{agent-tools-gGXXkf1z.d.ts → agent-tools-Be-hpRpH.d.ts} +2 -2
  6. package/dist/agent-tools.d.ts +1 -1
  7. package/dist/{ai-sdk-Kon40i0q.js → ai-sdk-CRMlEBX7.js} +75 -44
  8. package/dist/ai-sdk-CRMlEBX7.js.map +1 -0
  9. package/dist/browser/ai.js +2 -3
  10. package/dist/browser/ai.js.map +1 -1
  11. package/dist/browser/index.d.ts +33 -21
  12. package/dist/browser/index.js +937 -4
  13. package/dist/browser/index.js.map +1 -0
  14. package/dist/browser/tools/ai-sdk.d.ts +4 -4
  15. package/dist/browser/tools/ai-sdk.js +9 -3
  16. package/dist/browser/tools/ai-sdk.js.map +1 -1
  17. package/dist/browser/tools/pi.d.ts +11 -8
  18. package/dist/browser/tools/pi.js +58 -11
  19. package/dist/browser/tools/pi.js.map +1 -1
  20. package/dist/browser/tools/tanstack-ai.d.ts +4 -4
  21. package/dist/browser/tools/tanstack-ai.js +3 -3
  22. package/dist/browser/tools/tanstack-ai.js.map +1 -1
  23. package/dist/{browser-D9RNpJuT.d.ts → browser-C9T74n4c.d.ts} +302 -95
  24. package/dist/browser-tool-DOqT1oSD.js +360 -0
  25. package/dist/browser-tool-DOqT1oSD.js.map +1 -0
  26. package/dist/{browser-tool-BhjCcNVE.d.ts → browser-tool-DxAYQY9T.d.ts} +2 -2
  27. package/dist/capability-DBAKc6UZ.js.map +1 -1
  28. package/dist/{capability-runner-BiVUtjqC.d.ts → capability-runner-uFOdSANC.d.ts} +3 -2
  29. package/dist/chat/index.d.ts +37 -6
  30. package/dist/chat/index.js +39 -3
  31. package/dist/chat/index.js.map +1 -1
  32. package/dist/chat/react.js +1 -0
  33. package/dist/chat/react.js.map +1 -1
  34. package/dist/chat-sdk/index.d.ts +3 -3
  35. package/dist/chat-sdk/index.js +1 -1
  36. package/dist/cli.js +62 -60
  37. package/dist/client-CkT8voRY.js +3740 -0
  38. package/dist/client-CkT8voRY.js.map +1 -0
  39. package/dist/{client-eSfgYvd3.d.ts → client-DqS6oequ.d.ts} +2 -2
  40. package/dist/client.d.ts +1 -1
  41. package/dist/{connector-CM-uuxKM.js → connector-Bphz6wIw.js} +414 -3
  42. package/dist/connector-Bphz6wIw.js.map +1 -0
  43. package/dist/{conversations-FjHDRZyD.d.ts → conversations-CoAYNW_q.d.ts} +4 -4
  44. package/dist/{current-agent-CKgT5d1O.d.ts → current-agent-DShbQHDO.d.ts} +24 -3
  45. package/dist/current-agent-DhoDkSnH.js.map +1 -1
  46. package/dist/{do-oauth-client-provider-DtNqv7oj.d.ts → do-oauth-client-provider-BsT8-vtd.d.ts} +22 -2
  47. package/dist/{durable-object-lifecycle-DOI_bzCH.d.ts → durable-object-lifecycle-CRCA6O0W.d.ts} +2 -2
  48. package/dist/experimental/channels/index.d.ts +2 -2
  49. package/dist/experimental/channels/web/index.d.ts +2 -2
  50. package/dist/experimental/channels/web/index.js +1 -1
  51. package/dist/{gateway-BFO6vRDn.d.ts → gateway-Dz8zyZBJ.d.ts} +2 -2
  52. package/dist/harness/ai-sdk/index.d.ts +2 -2
  53. package/dist/harness/container/index.d.ts +2 -2
  54. package/dist/harness/container/index.js +1 -1
  55. package/dist/harness/container/index.js.map +1 -1
  56. package/dist/harness/opencode/index.d.ts +1 -1
  57. package/dist/harness/pi/index.d.ts +200 -37
  58. package/dist/harness/pi/index.js +202 -100
  59. package/dist/harness/pi/index.js.map +1 -1
  60. package/dist/harness/think/index.d.ts +2 -2
  61. package/dist/{images-CfuRCjwq.js → images-D4gtE06D.js} +119 -27
  62. package/dist/images-D4gtE06D.js.map +1 -0
  63. package/dist/{index-8k4qsX1T.d.ts → index-BXVgv6tp.d.ts} +4 -4
  64. package/dist/{index-BEL3ZV-s.d.ts → index-Cs7Ed9la.d.ts} +2 -2
  65. package/dist/{index-CBt8oRJt.d.ts → index-XiZxauMU.d.ts} +3 -3
  66. package/dist/index.d.ts +21 -21
  67. package/dist/index.js +2 -2
  68. package/dist/{input-schema-BQykTEp_.js → input-schema-D6GilRXI.js} +3 -3
  69. package/dist/input-schema-D6GilRXI.js.map +1 -0
  70. package/dist/{input-schema-B0Etuo-o.js → input-schema-y8W5bOdM.js} +3 -3
  71. package/dist/input-schema-y8W5bOdM.js.map +1 -0
  72. package/dist/internal_context.d.ts +1 -1
  73. package/dist/lifecycle/index.d.ts +3 -3
  74. package/dist/lifecycle/index.js +1 -1
  75. package/dist/{lifecycle-v1rKHV2C.js → lifecycle-CB5Dmtrt.js} +126 -38
  76. package/dist/lifecycle-CB5Dmtrt.js.map +1 -0
  77. package/dist/mcp/client/do-oauth-client-provider.d.ts +1 -1
  78. package/dist/mcp/client/do-oauth-client-provider.js +45 -2
  79. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -1
  80. package/dist/mcp/client/index.d.ts +42 -36
  81. package/dist/mcp/client/index.js +2 -2
  82. package/dist/mcp/index.d.ts +16 -16
  83. package/dist/mcp/index.js +2 -2
  84. package/dist/models/ai-sdk/index.d.ts +9 -5
  85. package/dist/models/ai-sdk/index.js +1 -1
  86. package/dist/models/opencode/index.d.ts +3 -3
  87. package/dist/models/opencode/index.js +2 -2
  88. package/dist/models/opencode/index.js.map +1 -1
  89. package/dist/models/pi-ai/index.d.ts +4 -4
  90. package/dist/models/pi-ai/index.js +64 -170
  91. package/dist/models/pi-ai/index.js.map +1 -1
  92. package/dist/{options-CiIsJHXR.d.ts → options-CiXaymmZ.d.ts} +2 -2
  93. package/dist/queue/index.d.ts +1 -1
  94. package/dist/queue/index.js +1 -1
  95. package/dist/{queue-CcIjVYTE.js → queue-B_wl6K0Z.js} +24 -5
  96. package/dist/queue-B_wl6K0Z.js.map +1 -0
  97. package/dist/react.d.ts +2 -2
  98. package/dist/routing/index.d.ts +2 -2
  99. package/dist/{scheduler-BCLESHSC.d.ts → scheduler-CTpR0Ct0.d.ts} +2 -2
  100. package/dist/{scheduler-FIHv2-kJ.js → scheduler-DGNEmxQR.js} +2 -1
  101. package/dist/scheduler-DGNEmxQR.js.map +1 -0
  102. package/dist/schedules/index.d.ts +1 -1
  103. package/dist/schedules/index.js +1 -1
  104. package/dist/serializable.d.ts +1 -1
  105. package/dist/{session-7CDgVE0z.js → session-CRY0bGiI.js} +423 -411
  106. package/dist/session-CRY0bGiI.js.map +1 -0
  107. package/dist/sessions/index.d.ts +1 -1
  108. package/dist/{settings-Dk2il2Hf.js → settings-D2F_qONv.js} +11 -2
  109. package/dist/settings-D2F_qONv.js.map +1 -0
  110. package/dist/{src-DoGNJ4Ud.js → src-BO1FdLK1.js} +145 -86
  111. package/dist/src-BO1FdLK1.js.map +1 -0
  112. package/dist/state/index.d.ts +1 -1
  113. package/dist/streams/index.d.ts +1 -1
  114. package/dist/{streams-vrCtbUIn.d.ts → streams-CAZxZBDg.d.ts} +2 -2
  115. package/dist/sub-routing.d.ts +12 -12
  116. package/dist/tasks/index.d.ts +1 -1
  117. package/dist/tasks/index.js +1 -1
  118. package/dist/{tasks-BN_5AWqI.js → tasks-Cf_UfEO4.js} +2 -1
  119. package/dist/{tasks-BN_5AWqI.js.map → tasks-Cf_UfEO4.js.map} +1 -1
  120. package/dist/{tasks-E89KdwkS.d.ts → tasks-Cs8ilctE.d.ts} +2 -2
  121. package/dist/{tool-DkT_DJBm.js → tool-CmAGY6Fo.js} +24 -3
  122. package/dist/tool-CmAGY6Fo.js.map +1 -0
  123. package/dist/{tool-CxKsvxjm.js → tool-Kxcj8_22.js} +24 -4
  124. package/dist/tool-Kxcj8_22.js.map +1 -0
  125. package/dist/{tool-helpers-BI7ifO2S.js → tool-helpers-F3_Wy2YX.js} +2 -2
  126. package/dist/{tool-helpers-BI7ifO2S.js.map → tool-helpers-F3_Wy2YX.js.map} +1 -1
  127. package/dist/{transport-5moD2pW5.d.ts → transport-hV8AW4Bv.d.ts} +45 -12
  128. package/dist/voice/index.d.ts +2 -2
  129. package/dist/web/fetch/index.js +1 -1
  130. package/dist/web/fetch/tools/ai-sdk.js +2 -2
  131. package/dist/web/fetch/tools/pi.d.ts +9 -7
  132. package/dist/web/fetch/tools/pi.js +31 -7
  133. package/dist/web/fetch/tools/pi.js.map +1 -1
  134. package/dist/web/fetch/tools/tanstack-ai.js +2 -2
  135. package/dist/web/search/index.js +1 -1
  136. package/dist/web/search/tools/ai-sdk.js +2 -2
  137. package/dist/web/search/tools/pi.d.ts +9 -4
  138. package/dist/web/search/tools/pi.js +27 -11
  139. package/dist/web/search/tools/pi.js.map +1 -1
  140. package/dist/web/search/tools/tanstack-ai.js +2 -2
  141. package/dist/websockets/index.d.ts +2 -2
  142. package/dist/websockets/index.js +1 -1
  143. package/dist/{websockets-C6SJtWCJ.js → websockets-W4uhRg4-.js} +68 -5
  144. package/dist/websockets-W4uhRg4-.js.map +1 -0
  145. package/dist/workflow-types.d.ts +1 -1
  146. package/dist/workflows.d.ts +1 -1
  147. package/docs/agent-class.md +30 -4
  148. package/docs/browse-the-web.md +50 -3
  149. package/docs/chat-agents.md +13 -10
  150. package/docs/client-tools-continuation.md +1 -1
  151. package/docs/codemode.md +1 -1
  152. package/docs/configuration.md +6 -3
  153. package/docs/cross-domain-authentication.md +1 -1
  154. package/docs/fetch-the-web.md +20 -0
  155. package/docs/harnesses/pi.md +242 -33
  156. package/docs/harnesses/think.md +1 -1
  157. package/docs/http-websockets.md +40 -32
  158. package/docs/human-in-the-loop.md +2 -2
  159. package/docs/lifecycle.md +56 -0
  160. package/docs/mcp-client.md +104 -7
  161. package/docs/models-pi-ai.md +24 -10
  162. package/docs/models.md +42 -17
  163. package/docs/resumable-streaming.md +1 -1
  164. package/docs/retries.md +3 -6
  165. package/docs/search-the-web.md +10 -0
  166. package/docs/voice.md +1 -1
  167. package/package.json +16 -9
  168. package/dist/ai-sdk-Kon40i0q.js.map +0 -1
  169. package/dist/browser-1eUbPvC2.js +0 -931
  170. package/dist/browser-1eUbPvC2.js.map +0 -1
  171. package/dist/browser-tool-DRan7Lk6.js +0 -497
  172. package/dist/browser-tool-DRan7Lk6.js.map +0 -1
  173. package/dist/client-DMWujQJ1.js +0 -2325
  174. package/dist/client-DMWujQJ1.js.map +0 -1
  175. package/dist/connector-CM-uuxKM.js.map +0 -1
  176. package/dist/images-CfuRCjwq.js.map +0 -1
  177. package/dist/input-schema-B0Etuo-o.js.map +0 -1
  178. package/dist/input-schema-BQykTEp_.js.map +0 -1
  179. package/dist/lifecycle-v1rKHV2C.js.map +0 -1
  180. package/dist/queue-CcIjVYTE.js.map +0 -1
  181. package/dist/scheduler-FIHv2-kJ.js.map +0 -1
  182. package/dist/session-7CDgVE0z.js.map +0 -1
  183. package/dist/settings-Dk2il2Hf.js.map +0 -1
  184. package/dist/src-DoGNJ4Ud.js.map +0 -1
  185. package/dist/tool-CxKsvxjm.js.map +0 -1
  186. package/dist/tool-DkT_DJBm.js.map +0 -1
  187. package/dist/websockets-C6SJtWCJ.js.map +0 -1
@@ -3,10 +3,11 @@ import { t as _classPrivateMethodInitSpec } from "../../classPrivateMethodInitSp
3
3
  import { t as LifecycleCapability } from "../../capability-DBAKc6UZ.js";
4
4
  import { isDurableObjectMemoryLimitReset } from "../../retries.js";
5
5
  import { i as renderSkillResource, n as formatSkillDiagnostic, r as loadSkillCatalog } from "../../catalog-BpOejOsE.js";
6
- import { Type } from "@earendil-works/pi-ai";
7
- import { LiveDoc, ROOT_CONVERSATION_ID, watchEvents } from "@earendil-works/pi-durable";
6
+ import { obj, opt, str } from "@earendil-works/pi-schema";
7
+ import { LiveDoc, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
8
8
  import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
9
9
  import { openDurableObjectSqliteStorage } from "@earendil-works/pi-durable/storage/sqlite/cloudflare";
10
+ import { StringEnum } from "@earendil-works/pi-ai";
10
11
  //#region src/harness/pi/session-store.ts
11
12
  const DEFAULT_PREFIX = "pi_";
12
13
  /** Open pi-durable's storage over this object's SQLite database. */
@@ -58,6 +59,19 @@ const WAKE_FN = "wake";
58
59
  * `pi-wake:<session>`; a left-over one hands over to this one.
59
60
  */
60
61
  const WAKE_JOB_ID = "pi-wake";
62
+ /**
63
+ * `change` applied over `base` as pi applies an `AgentChange`: a field that is
64
+ * `undefined` in `change` keeps `base`'s, any other value (`null` included)
65
+ * replaces it. A plain spread would copy the `undefined` over `base`'s value.
66
+ */
67
+ function applyAgentChange(base, change) {
68
+ const applied = {
69
+ ...base,
70
+ ...change
71
+ };
72
+ for (const key in base) if (change[key] === void 0) applied[key] = base[key];
73
+ return applied;
74
+ }
61
75
  /** pi-durable's stored reference to a pi-ai model. */
62
76
  function modelRef(model) {
63
77
  return {
@@ -104,8 +118,9 @@ var _onWake = /* @__PURE__ */ new WeakMap();
104
118
  *
105
119
  * What pi cannot do on a Durable Object is wake itself: its scheduler runs
106
120
  * in memory, and an evicted object has no memory. The harness is that wake,
107
- * with one Lifecycle job driven by `pi.wake()`. Input goes to pi once, in
108
- * `submit()`, after the job is scheduled. While pi has work running, the
121
+ * with one Lifecycle job driven by `pi.wake()`. Anything that can start
122
+ * work, such as `submit()`, `signal()`, `abort()` or `reset()`, goes to pi
123
+ * once, after the job is scheduled. While pi has work running, the
109
124
  * job waits on it, rescheduling itself as a heartbeat. While pi's tasks
110
125
  * sleep, the job is due when the first one wakes; pi tells the harness
111
126
  * through the `onWake` the factory passes to `Harness.open`. When pi has
@@ -177,6 +192,31 @@ var PiHarness = class extends LifecycleCapability {
177
192
  session(id = ROOT_SESSION) {
178
193
  return new PiSession(this, id);
179
194
  }
195
+ /**
196
+ * The root session, creating it with `agent` and `init` in one commit
197
+ * when it does not exist yet, as pi's `Harness.root()` does. `agent` is
198
+ * applied over `defaults`. When the root already exists, nothing is
199
+ * written and `init` does not run; `created` says which happened.
200
+ *
201
+ * Without this call, the first use of the root creates it with
202
+ * `defaults`. So call it before anything else uses the root, for example
203
+ * to seed the root's transcript with an existing history.
204
+ */
205
+ async root(options = {}) {
206
+ const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
207
+ let created = false;
208
+ await _assertClassBrand(_PiHarness_brand, this, _root).call(this, pi, {
209
+ ...options.agent ? { agent: options.agent } : {},
210
+ init: async (tx, id) => {
211
+ created = true;
212
+ await options.init?.(tx, id);
213
+ }
214
+ });
215
+ return {
216
+ session: this.session(ROOT_SESSION),
217
+ created
218
+ };
219
+ }
180
220
  /** Submit a prompt and wait for its answer. */
181
221
  prompt(input, options = {}) {
182
222
  return this.session(options.session).prompt(input, options);
@@ -185,17 +225,17 @@ var PiHarness = class extends LifecycleCapability {
185
225
  submit(input, options = {}) {
186
226
  return this.session(options.session).submit(input, options);
187
227
  }
228
+ /**
229
+ * Run an idle session again on its transcript as it is, without new input.
230
+ * See `PiSession.continue`.
231
+ */
232
+ continue(options = {}) {
233
+ return this.session(options.session).continue(options);
234
+ }
188
235
  /** Stop one operation, or everything running in a session. */
189
236
  abort(options = {}) {
190
237
  return this.session(options.session).abort(options.operationId);
191
238
  }
192
- /**
193
- * Run again the tasks pi's crash-loop guard parked in a session's work.
194
- * False when nothing was parked.
195
- */
196
- retry(options = {}) {
197
- return this.session(options.session).retry();
198
- }
199
239
  wait(operationId, options = {}) {
200
240
  return this.session(options.session).wait(operationId, options.signal);
201
241
  }
@@ -207,9 +247,7 @@ var PiHarness = class extends LifecycleCapability {
207
247
  async pending(options = {}) {
208
248
  const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
209
249
  const id = options.session === void 0 ? void 0 : conversationId(options.session);
210
- const inspection = await pi.inspect(BG);
211
- const parked = await _assertClassBrand(_PiHarness_brand, this, _parked).call(this, inspection);
212
- return inspection.submissions.flatMap((record) => {
250
+ return (await pi.inspect(BG)).submissions.flatMap((record) => {
213
251
  if (record.requestId === void 0) return [];
214
252
  if (id !== void 0 && record.conversationId !== id) return [];
215
253
  let status;
@@ -218,7 +256,7 @@ var PiHarness = class extends LifecycleCapability {
218
256
  status = "queued";
219
257
  break;
220
258
  case "placed":
221
- status = parked.some((task) => task.sessions.has(record.conversationId)) ? "parked" : "running";
259
+ status = "running";
222
260
  break;
223
261
  case "done":
224
262
  case "unanswered": return [];
@@ -231,14 +269,30 @@ var PiHarness = class extends LifecycleCapability {
231
269
  }];
232
270
  });
233
271
  }
272
+ /**
273
+ * Deliver `value` to pi task `taskId` under `key`, as pi's
274
+ * `Harness.signal()` does: for a tool parked on that signal, such as one
275
+ * waiting for a person's answer or a webhook. The wake job is scheduled
276
+ * first, so if the object dies while the woken call runs, the job's alarm
277
+ * restarts it. Call this rather than `pi().signal()`, which does not.
278
+ */
279
+ async signal(taskId, signal, key, value) {
280
+ const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
281
+ return _assertClassBrand(_PiHarness_brand, this, _admit).call(this, () => pi.signal(taskId, signal, key, value, BG));
282
+ }
234
283
  /** The opened pi Harness, for anything the interface does not cover. */
235
284
  async pi() {
236
285
  return (await _assertClassBrand(_PiHarness_brand, this, _open).call(this)).pi;
237
286
  }
238
- /** @internal */
287
+ /**
288
+ * @internal Every use of a session goes through here. The first use of
289
+ * the root creates it, with `defaults`.
290
+ */
239
291
  async conversation(session) {
240
292
  const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
241
- const conversation = await pi.conversation(conversationId(session), BG);
293
+ const id = conversationId(session);
294
+ if (id === ROOT_CONVERSATION_ID) return _assertClassBrand(_PiHarness_brand, this, _root).call(this, pi, {});
295
+ const conversation = await pi.conversation(id, BG);
242
296
  if (!conversation) throw new Error(`Unknown pi session ${session}`);
243
297
  return conversation;
244
298
  }
@@ -248,35 +302,22 @@ var PiHarness = class extends LifecycleCapability {
248
302
  }
249
303
  /** @internal */
250
304
  async enqueue(session, input, options) {
251
- const operationId = options.operationId ?? crypto.randomUUID();
252
- const { storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
253
- const conversation = await this.conversation(session);
254
- return {
255
- operationId,
256
- session,
257
- accepted: await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, async () => {
258
- const accepted = await storage.submissionByRequest(conversation.id, operationId, BG) === void 0;
259
- await conversation.submit({
260
- type: "input",
261
- content: input,
262
- whenBusy: options.whenBusy ?? "followUp",
263
- requestId: operationId,
264
- ...options.entry ? { entry: options.entry } : {}
265
- }, BG);
266
- return accepted;
267
- })
268
- };
305
+ return _assertClassBrand(_PiHarness_brand, this, _admitDraft).call(this, session, options.operationId, {
306
+ type: "input",
307
+ content: input,
308
+ whenBusy: options.whenBusy ?? "followUp",
309
+ ...options.whenIdle ? { whenIdle: options.whenIdle } : {},
310
+ ...options.onAbort ? { onAbort: options.onAbort } : {},
311
+ ...options.entry ? { entry: options.entry } : {},
312
+ ...options.notify ? { notify: options.notify } : {}
313
+ });
269
314
  }
270
- /** @internal Run again the tasks pi's restart limit parked in a session. */
271
- async retryParked(session) {
272
- const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
273
- const id = conversationId(session);
274
- const parked = (await _assertClassBrand(_PiHarness_brand, this, _parked).call(this, await pi.inspect(BG))).filter((task) => task.sessions.has(id));
275
- if (parked.length === 0) return false;
276
- await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, async () => {
277
- for (const task of parked) await pi.retryTask(task.id, BG);
315
+ /** @internal Start a run on the context as it is, without new input. */
316
+ enqueueContinue(session, options) {
317
+ return _assertClassBrand(_PiHarness_brand, this, _admitDraft).call(this, session, options.operationId, {
318
+ type: "continue",
319
+ ...options.notify ? { notify: options.notify } : {}
278
320
  });
279
- return true;
280
321
  }
281
322
  /** @internal Withdraw a queued input, or abort the run it joined. */
282
323
  async withdraw(session, operationId) {
@@ -284,17 +325,34 @@ var PiHarness = class extends LifecycleCapability {
284
325
  const id = conversationId(session);
285
326
  const record = await storage.submissionByRequest(id, operationId, BG);
286
327
  if (!record || record.status === "done" || record.status === "unanswered") return false;
287
- const outcome = await pi.abortSubmission(record.id, BG, id);
328
+ const outcome = await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, () => pi.abortSubmission(record.id, BG, id));
288
329
  switch (outcome) {
289
330
  case "aborted": return true;
290
331
  case "already_placed":
291
- await (await pi.conversation(id, BG))?.abort(BG);
332
+ await this.abortConversation(session);
292
333
  return true;
293
334
  case "settled":
294
335
  case "not_found": return false;
295
336
  default: return outcome;
296
337
  }
297
338
  }
339
+ /**
340
+ * @internal Abort a session's running work and withdraw its queued
341
+ * inputs. Stopping a parked call runs its `cancel()`, which is work, so
342
+ * the wake is scheduled first.
343
+ */
344
+ async abortConversation(session) {
345
+ const conversation = await this.conversation(session);
346
+ await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, () => conversation.abort(BG));
347
+ }
348
+ /**
349
+ * @internal Start a new context. Inputs kept behind a failed run or an
350
+ * abort run in it, so the wake is scheduled first.
351
+ */
352
+ async resetConversation(session, handoff) {
353
+ const conversation = await this.conversation(session);
354
+ await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, () => conversation.reset(handoff, BG));
355
+ }
298
356
  /** @internal Wait for pi to settle an operation, by its request id. */
299
357
  async settled(session, operationId, signal) {
300
358
  const conversation = await this.conversation(session);
@@ -308,16 +366,53 @@ var PiHarness = class extends LifecycleCapability {
308
366
  };
309
367
  return _assertClassBrand(_PiHarness_brand, this, _result).call(this, session, operationId, await submission.wait(context), context);
310
368
  }
369
+ /**
370
+ * @internal `agent` applied over the defaults, by pi's rule for an
371
+ * `AgentChange`: a field set to a value replaces the default, `null`
372
+ * clears it, and `undefined` leaves it in place.
373
+ */
374
+ agentOverDefaults(agent = {}) {
375
+ return applyAgentChange(this.agentDefaults(), agent);
376
+ }
311
377
  /** @internal The per-conversation defaults supported by pi-durable. */
312
378
  agentDefaults() {
313
379
  const defaults = _classPrivateFieldGet2(_options, this).defaults;
314
380
  return {
315
381
  ...defaults?.model === void 0 ? {} : { model: modelRef(defaults.model) },
316
- ...defaults?.thinkingLevel === void 0 ? {} : { thinkingLevel: defaults.thinkingLevel }
382
+ ...defaults?.thinkingLevel === void 0 ? {} : { thinkingLevel: defaults.thinkingLevel },
383
+ ...defaults?.maxTurns === void 0 ? {} : { maxTurns: defaults.maxTurns }
317
384
  };
318
385
  }
319
386
  };
320
387
  /**
388
+ * pi's root conversation, created with `options` over `defaults` when
389
+ * absent. pi checks for the root and creates it in one commit, so two
390
+ * calls never both create it.
391
+ */
392
+ function _root(pi, options) {
393
+ return pi.root(BG, {
394
+ agent: this.agentOverDefaults(options.agent),
395
+ ...options.init ? { init: options.init } : {}
396
+ });
397
+ }
398
+ async function _admitDraft(session, requested, draft) {
399
+ const operationId = requested ?? crypto.randomUUID();
400
+ const { storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
401
+ const conversation = await this.conversation(session);
402
+ return {
403
+ operationId,
404
+ session,
405
+ accepted: await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, async () => {
406
+ const accepted = await storage.submissionByRequest(conversation.id, operationId, BG) === void 0;
407
+ await conversation.submit({
408
+ ...draft,
409
+ requestId: operationId
410
+ }, BG);
411
+ return accepted;
412
+ })
413
+ };
414
+ }
415
+ /**
321
416
  * Hand pi work inside the wake. The wake is scheduled first, so if the
322
417
  * object dies after pi has the work, that job restarts it; while `hand`
323
418
  * is in flight the step will not complete. The wake steps again after, so
@@ -456,7 +551,6 @@ async function _doOpen(onClose) {
456
551
  try {
457
552
  const closed = pi.closed;
458
553
  pi.subscribeClose(() => onClose(pi, closed));
459
- await pi.root(BG, { agent: this.agentDefaults() });
460
554
  pi.resume();
461
555
  await new Promise((resolve) => setTimeout(resolve, 0));
462
556
  if (!_classPrivateFieldGet2(_onWakeWired, this)) console.warn("PiHarness: the harness factory did not pass onWake to Harness.open, so pi's timers keep this Durable Object in memory while a task sleeps. Pass it: harness: ({ storage, context, onWake }) => Harness.open(storage, { ...options, onWake }, context)");
@@ -481,29 +575,6 @@ function _piClosing(opening, pi, closed) {
481
575
  _classPrivateFieldSet2(_opening, this, void 0);
482
576
  _classPrivateFieldSet2(_lastClose, this, closed);
483
577
  }
484
- /**
485
- * Tasks pi's restart limit parked, each with the sessions whose work it
486
- * is: its own, and every session that owns that one, such as the session
487
- * of a tool that started a subagent.
488
- */
489
- async function _parked(inspection) {
490
- const { storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
491
- const parked = [];
492
- for (const { record, state } of inspection.tasks) {
493
- if (state.kind !== "blocked" || state.reason !== "restart_limit") continue;
494
- const sessions = /* @__PURE__ */ new Set();
495
- let id = record.conversationId;
496
- while (id !== void 0 && !sessions.has(id)) {
497
- sessions.add(id);
498
- id = (await storage.conversation(id, BG))?.owner?.conversationId;
499
- }
500
- parked.push({
501
- id: record.id,
502
- sessions
503
- });
504
- }
505
- return parked;
506
- }
507
578
  async function _findSubmission(id, operationId, context) {
508
579
  const { pi, storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
509
580
  const record = await storage.submissionByRequest(id, operationId, context);
@@ -516,7 +587,7 @@ async function _result(session, operationId, settled, context) {
516
587
  status: "unanswered",
517
588
  reason: settled.reason
518
589
  };
519
- if (settled.type !== "input") return {
590
+ if (settled.type === "write") return {
520
591
  operationId,
521
592
  session,
522
593
  status: "done"
@@ -551,6 +622,16 @@ var PiSession = class {
551
622
  messages: await this.messages()
552
623
  };
553
624
  }
625
+ /**
626
+ * Start a run on the transcript as it is, without a new message: for
627
+ * example, after a run ended unanswered because the model failed or the
628
+ * object kept crashing. Only an idle session takes one; a busy one rejects
629
+ * with pi-durable's `ConversationBusy`. A transcript that ends in an answer
630
+ * settles `unanswered` with reason `nothing_to_answer`.
631
+ */
632
+ continue(options = {}) {
633
+ return _classPrivateFieldGet2(_harness, this).enqueueContinue(this.id, options);
634
+ }
554
635
  /** Join the running work after its current tool round. */
555
636
  steer(input, options = {}) {
556
637
  return this.submit(input, {
@@ -564,25 +645,17 @@ var PiSession = class {
564
645
  }
565
646
  /**
566
647
  * Withdraw one queued operation (or abort the run it joined), or, with no
567
- * id, abort the session: pi withdraws queued inputs and aborts the
568
- * running work.
648
+ * id, abort the session: pi withdraws queued inputs, apart from those
649
+ * submitted with `onAbort: "keep"`, and aborts the running work.
569
650
  */
570
651
  async abort(operationId) {
571
652
  if (operationId !== void 0) return _classPrivateFieldGet2(_harness, this).withdraw(this.id, operationId);
572
- await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).abort(BG);
653
+ await _classPrivateFieldGet2(_harness, this).abortConversation(this.id);
573
654
  return true;
574
655
  }
575
- /**
576
- * Run again the tasks pi's crash-loop guard (`settings.restartLimit`)
577
- * parked in this session's work, including subagent sessions it owns.
578
- * False when nothing was parked.
579
- */
580
- retry() {
581
- return _classPrivateFieldGet2(_harness, this).retryParked(this.id);
582
- }
583
656
  /** Start a new context, optionally from a handoff note. */
584
657
  async reset(handoff) {
585
- await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).reset(handoff, BG);
658
+ await _classPrivateFieldGet2(_harness, this).resetConversation(this.id, handoff);
586
659
  }
587
660
  /**
588
661
  * Change this session's model, by its `provider` and `id`; it must be on
@@ -595,9 +668,25 @@ var PiSession = class {
595
668
  async messages() {
596
669
  return [...(await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).context(BG)).entries];
597
670
  }
598
- /** pi's agent events for this session: a snapshot, then one batch per commit. */
599
- async events(context = BG) {
600
- return watchEvents(await _classPrivateFieldGet2(_harness, this).pi(), conversationId(this.id), context);
671
+ /**
672
+ * Watch this session: pi's conversation view, a window over the transcript
673
+ * with the session's documents, such as `pi.live` (the answer being
674
+ * streamed, running tools, and why the last run ended without an answer)
675
+ * and `pi.inbox`. The first value holds the newest entries; new entries
676
+ * are appended. Each change carries pi's operations, for a client that
677
+ * forwards them. To render more than the window, merge each value into a
678
+ * `ConversationTranscript` that pages with `entries()`.
679
+ */
680
+ async watch(window, context = BG) {
681
+ return (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).watch(context, window);
682
+ }
683
+ /**
684
+ * A page of this session's transcript, newest first unless `query.order`
685
+ * is `ascending`, including entries before its resets. The loader a
686
+ * `ConversationTranscript` takes.
687
+ */
688
+ async entries(query, limit, context = BG) {
689
+ return (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).entries(query, limit, void 0, context);
601
690
  }
602
691
  async busy() {
603
692
  return (await (await _classPrivateFieldGet2(_harness, this).pi()).snapshot(LiveDoc, conversationId(this.id), BG))?.run !== void 0;
@@ -613,25 +702,38 @@ var PiSessions = class {
613
702
  get(id) {
614
703
  return _classPrivateFieldGet2(_harness2, this).session(id);
615
704
  }
616
- /** A new top-level session, configured like the root. */
617
- async create() {
705
+ /**
706
+ * A new top-level session, configured with `defaults`, then `agent`; `init`
707
+ * runs in the commit that creates it.
708
+ */
709
+ async create(options = {}) {
618
710
  const conversation = await (await _classPrivateFieldGet2(_harness2, this).pi()).createConversation({
619
711
  ownership: { kind: "ownerless" },
620
- agent: _classPrivateFieldGet2(_harness2, this).agentDefaults()
712
+ agent: _classPrivateFieldGet2(_harness2, this).agentOverDefaults(options.agent),
713
+ ...options.init ? { init: options.init } : {}
621
714
  }, BG);
622
715
  return _classPrivateFieldGet2(_harness2, this).session(String(conversation.id));
623
716
  }
624
- /** A new session that sees `from`'s history up to its newest entry. */
625
- async fork(from) {
717
+ /**
718
+ * A new session that sees `from`'s history up to its newest entry, with
719
+ * `from`'s configuration, then `agent`; `init` runs in the commit that
720
+ * creates it.
721
+ */
722
+ async fork(from, options = {}) {
626
723
  const conversation = await _classPrivateFieldGet2(_harness2, this).conversation(from);
627
724
  const at = (await conversation.entries({}, 1, void 0, BG)).items[0];
628
725
  if (!at) throw new Error(`Session ${from} has no entries to fork from`);
629
- const fork = await conversation.fork(at.id, { ownership: { kind: "ownerless" } }, BG);
726
+ const fork = await conversation.fork(at.id, {
727
+ ownership: { kind: "ownerless" },
728
+ ...options.agent ? { agent: options.agent } : {},
729
+ ...options.init ? { init: options.init } : {}
730
+ }, BG);
630
731
  return _classPrivateFieldGet2(_harness2, this).session(String(fork.id));
631
732
  }
632
733
  /**
633
734
  * Every conversation, including ones subagent tools own. Read from pi's
634
- * storage directly: the Harness has no conversation listing.
735
+ * storage directly: the Harness has no conversation listing. The root is
736
+ * listed once it exists: its first use, or `harness.root()`, creates it.
635
737
  */
636
738
  async list() {
637
739
  const storage = await _classPrivateFieldGet2(_harness2, this).storage();
@@ -679,11 +781,11 @@ function errorResult(error) {
679
781
  function skillTools(catalog) {
680
782
  const names = catalog.skills.map((entry) => entry.skill.name);
681
783
  if (names.length === 0) return [];
682
- const nameSchema = Type.Union(names.map((name) => Type.Literal(name)));
784
+ const nameSchema = StringEnum(names);
683
785
  return [{
684
786
  name: "activate_skill",
685
787
  description: "Activate a skill by name. Use this when the user's task matches one of the available skills; the response contains the skill's full instructions.",
686
- parameters: Type.Object({ name: nameSchema }),
788
+ parameters: obj({ name: nameSchema }, { open: true }),
687
789
  replay: "safe",
688
790
  async execute(input) {
689
791
  const activated = catalog.activate(input.name);
@@ -692,10 +794,10 @@ function skillTools(catalog) {
692
794
  }, {
693
795
  name: "read_skill_resource",
694
796
  description: "Read a file bundled with a skill, such as a reference document or template. Provide the skill name and the file's path from the skill's resource list, or a qualified path like skill-name/references/file.md.",
695
- parameters: Type.Object({
696
- name: Type.Optional(nameSchema),
697
- path: Type.String({ minLength: 1 })
698
- }),
797
+ parameters: obj({
798
+ name: opt(nameSchema),
799
+ path: str({ minLength: 1 })
800
+ }, { open: true }),
699
801
  replay: "safe",
700
802
  async execute(input) {
701
803
  const read = await catalog.readResource(input.name === void 0 ? { path: input.path } : {