agents 0.31.0 → 0.33.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 (148) hide show
  1. package/dist/{agent-routing-wS1TimZ_.d.ts → agent-routing-vVMA7m8Y.d.ts} +6 -6
  2. package/dist/agent-routing.d.ts +1 -1
  3. package/dist/agent-tool-types.d.ts +1 -1
  4. package/dist/{agent-tools-Bamxxb6Y.d.ts → agent-tools-gGXXkf1z.d.ts} +2 -2
  5. package/dist/agent-tools.d.ts +1 -1
  6. package/dist/agent-tools.js +0 -1
  7. package/dist/agent-tools.js.map +1 -1
  8. package/dist/{ai-sdk-hW3vntnk.d.ts → ai-sdk-CpuRsjP5.d.ts} +2 -2
  9. package/dist/browser/ai.js +2 -3
  10. package/dist/browser/ai.js.map +1 -1
  11. package/dist/browser/index.d.ts +29 -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 +5 -5
  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 +6 -6
  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-CI97khiJ.d.ts} +222 -94
  24. package/dist/{browser-tool-BhjCcNVE.d.ts → browser-tool-Cxtiwfua.d.ts} +2 -2
  25. package/dist/browser-tool-DOqT1oSD.js +360 -0
  26. package/dist/browser-tool-DOqT1oSD.js.map +1 -0
  27. package/dist/catalog-BpOejOsE.js +239 -0
  28. package/dist/catalog-BpOejOsE.js.map +1 -0
  29. package/dist/catalog-BpiVV1OF.d.ts +173 -0
  30. package/dist/chat/index.d.ts +2 -2
  31. package/dist/chat/react.js +1 -0
  32. package/dist/chat/react.js.map +1 -1
  33. package/dist/chat-sdk/index.d.ts +1 -1
  34. package/dist/chat-sdk/index.js +1 -1
  35. package/dist/cli.js +63 -62
  36. package/dist/{client-wn4-yqGz.d.ts → client-eSfgYvd3.d.ts} +2 -2
  37. package/dist/client.d.ts +1 -1
  38. package/dist/{cloudflare-Dzvc7V2N.js → cloudflare-CUUm_9q9.js} +26 -4
  39. package/dist/cloudflare-CUUm_9q9.js.map +1 -0
  40. package/dist/{connector-CM-uuxKM.js → connector-Dl098To-.js} +402 -3
  41. package/dist/connector-Dl098To-.js.map +1 -0
  42. package/dist/{do-oauth-client-provider-DfsUcpc3.d.ts → do-oauth-client-provider-DtNqv7oj.d.ts} +26 -2
  43. package/dist/{index-YSKgfgg9.d.ts → emitter-BJZtCVjQ.d.ts} +6 -140
  44. package/dist/emitter-DPOVcCln.js +15 -0
  45. package/dist/emitter-DPOVcCln.js.map +1 -0
  46. package/dist/experimental/channels/web/index.js +1 -1
  47. package/dist/experimental/webmcp.js +1 -1
  48. package/dist/harness/container/index.js +0 -1
  49. package/dist/harness/container/index.js.map +1 -1
  50. package/dist/harness/container/runtime/index.js +2 -2
  51. package/dist/harness/opencode/index.js +0 -1
  52. package/dist/harness/opencode/index.js.map +1 -1
  53. package/dist/harness/pi/index.d.ts +84 -30
  54. package/dist/harness/pi/index.js +120 -224
  55. package/dist/harness/pi/index.js.map +1 -1
  56. package/dist/index.d.ts +2 -3
  57. package/dist/index.js +2 -2
  58. package/dist/{input-schema-BQykTEp_.js → input-schema-0uUu392-.js} +2 -2
  59. package/dist/input-schema-0uUu392-.js.map +1 -0
  60. package/dist/{input-schema-B0Etuo-o.js → input-schema-waggtYrH.js} +2 -2
  61. package/dist/input-schema-waggtYrH.js.map +1 -0
  62. package/dist/markdown-BZ-VLtA1.js +86 -0
  63. package/dist/markdown-BZ-VLtA1.js.map +1 -0
  64. package/dist/mcp/client/do-oauth-client-provider.d.ts +4 -2
  65. package/dist/mcp/client/do-oauth-client-provider.js +15 -1
  66. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -1
  67. package/dist/mcp/client/index.d.ts +1 -1
  68. package/dist/mcp/client/x402.d.ts +8 -8
  69. package/dist/mcp/index.d.ts +2 -2
  70. package/dist/mcp/index.js +4 -4
  71. package/dist/mcp/index.js.map +1 -1
  72. package/dist/models/pi-ai/index.js +12 -15
  73. package/dist/models/pi-ai/index.js.map +1 -1
  74. package/dist/observability/ai/index.js +1 -2
  75. package/dist/observability/ai/index.js.map +1 -1
  76. package/dist/observability/index.d.ts +170 -9
  77. package/dist/observability/index.js +386 -11
  78. package/dist/observability/index.js.map +1 -1
  79. package/dist/react.d.ts +2 -2
  80. package/dist/resource-B59OV2a2.js +325 -0
  81. package/dist/resource-B59OV2a2.js.map +1 -0
  82. package/dist/routing/index.d.ts +1 -1
  83. package/dist/script-CFDLTk5b.d.ts +91 -0
  84. package/dist/script-workspace-BpdAYq1R.d.ts +41 -0
  85. package/dist/serializable.d.ts +1 -1
  86. package/dist/{session-7CDgVE0z.js → session-CRY0bGiI.js} +423 -411
  87. package/dist/session-CRY0bGiI.js.map +1 -0
  88. package/dist/skill-DRBcIBjC.d.ts +221 -0
  89. package/dist/skills/index.d.ts +275 -36
  90. package/dist/skills/index.js +265 -1534
  91. package/dist/skills/index.js.map +1 -1
  92. package/dist/skills/runner.d.ts +37 -0
  93. package/dist/skills/runner.js +912 -0
  94. package/dist/skills/runner.js.map +1 -0
  95. package/dist/skills/tools/ai-sdk.d.ts +36 -0
  96. package/dist/skills/tools/ai-sdk.js +94 -0
  97. package/dist/skills/tools/ai-sdk.js.map +1 -0
  98. package/dist/source-9RQzlEhM.d.ts +112 -0
  99. package/dist/{src-BGolXG_H.js → src-DoGNJ4Ud.js} +4 -5
  100. package/dist/src-DoGNJ4Ud.js.map +1 -0
  101. package/dist/sub-routing.d.ts +1 -1
  102. package/dist/tool-CxKsvxjm.js.map +1 -1
  103. package/dist/tool-DkT_DJBm.js.map +1 -1
  104. package/dist/{tool-helpers-BodWi4gd.js → tool-helpers-F3_Wy2YX.js} +2 -3
  105. package/dist/{tool-helpers-BodWi4gd.js.map → tool-helpers-F3_Wy2YX.js.map} +1 -1
  106. package/dist/vite.d.ts +2 -2
  107. package/dist/vite.js +10 -258
  108. package/dist/vite.js.map +1 -1
  109. package/dist/voice/index.d.ts +1 -1
  110. package/dist/web/fetch/tools/ai-sdk.js +1 -1
  111. package/dist/web/fetch/tools/pi.d.ts +4 -7
  112. package/dist/web/fetch/tools/pi.js +7 -6
  113. package/dist/web/fetch/tools/pi.js.map +1 -1
  114. package/dist/web/fetch/tools/tanstack-ai.js +1 -1
  115. package/dist/web/search/tools/ai-sdk.js +1 -1
  116. package/dist/web/search/tools/pi.d.ts +5 -4
  117. package/dist/web/search/tools/pi.js +5 -10
  118. package/dist/web/search/tools/pi.js.map +1 -1
  119. package/dist/web/search/tools/tanstack-ai.js +1 -1
  120. package/dist/websockets/index.js +1 -1
  121. package/dist/{websockets-CyoeosdO.js → websockets-C6SJtWCJ.js} +1 -2
  122. package/dist/{websockets-CyoeosdO.js.map → websockets-C6SJtWCJ.js.map} +1 -1
  123. package/dist/workflow-types.d.ts +1 -1
  124. package/dist/workflows.d.ts +1 -1
  125. package/dist/workflows.js +0 -1
  126. package/dist/workflows.js.map +1 -1
  127. package/docs/browse-the-web.md +36 -3
  128. package/docs/harnesses/pi.md +59 -33
  129. package/docs/mcp-client.md +23 -1
  130. package/docs/mcp-servers.md +2 -2
  131. package/docs/models-pi-ai.md +1 -1
  132. package/docs/observability.md +5 -3
  133. package/package.json +37 -22
  134. package/dist/browser-1eUbPvC2.js +0 -931
  135. package/dist/browser-1eUbPvC2.js.map +0 -1
  136. package/dist/browser-tool-lGnvEBLJ.js +0 -497
  137. package/dist/browser-tool-lGnvEBLJ.js.map +0 -1
  138. package/dist/cloudflare-Dzvc7V2N.js.map +0 -1
  139. package/dist/connector-CM-uuxKM.js.map +0 -1
  140. package/dist/index-iv9FTXX9.d.ts +0 -311
  141. package/dist/input-schema-B0Etuo-o.js.map +0 -1
  142. package/dist/input-schema-BQykTEp_.js.map +0 -1
  143. package/dist/session-7CDgVE0z.js.map +0 -1
  144. package/dist/skills/compile.d.ts +0 -40
  145. package/dist/skills/compile.js +0 -65
  146. package/dist/skills/compile.js.map +0 -1
  147. package/dist/src-BGolXG_H.js.map +0 -1
  148. package/skills-module.d.ts +0 -22
@@ -1,12 +1,13 @@
1
1
  import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, r as _assertClassBrand, t as _classPrivateFieldGet2 } from "../../classPrivateFieldGet2-DZBYAB34.js";
2
2
  import { t as _classPrivateMethodInitSpec } from "../../classPrivateMethodInitSpec-qMjJ6sHQ.js";
3
3
  import { t as LifecycleCapability } from "../../capability-DBAKc6UZ.js";
4
- import "../../lifecycle-v1rKHV2C.js";
5
4
  import { isDurableObjectMemoryLimitReset } from "../../retries.js";
6
- import { Type } from "@earendil-works/pi-ai";
7
- import { LiveDoc, ROOT_CONVERSATION_ID, watchEvents } from "@earendil-works/pi-durable";
5
+ import { i as renderSkillResource, n as formatSkillDiagnostic, r as loadSkillCatalog } from "../../catalog-BpOejOsE.js";
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. */
@@ -185,17 +186,17 @@ var PiHarness = class extends LifecycleCapability {
185
186
  submit(input, options = {}) {
186
187
  return this.session(options.session).submit(input, options);
187
188
  }
189
+ /**
190
+ * Run an idle session again on its transcript as it is, without new input.
191
+ * See `PiSession.continue`.
192
+ */
193
+ continue(options = {}) {
194
+ return this.session(options.session).continue(options);
195
+ }
188
196
  /** Stop one operation, or everything running in a session. */
189
197
  abort(options = {}) {
190
198
  return this.session(options.session).abort(options.operationId);
191
199
  }
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
200
  wait(operationId, options = {}) {
200
201
  return this.session(options.session).wait(operationId, options.signal);
201
202
  }
@@ -207,9 +208,7 @@ var PiHarness = class extends LifecycleCapability {
207
208
  async pending(options = {}) {
208
209
  const { pi } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
209
210
  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) => {
211
+ return (await pi.inspect(BG)).submissions.flatMap((record) => {
213
212
  if (record.requestId === void 0) return [];
214
213
  if (id !== void 0 && record.conversationId !== id) return [];
215
214
  let status;
@@ -218,7 +217,7 @@ var PiHarness = class extends LifecycleCapability {
218
217
  status = "queued";
219
218
  break;
220
219
  case "placed":
221
- status = parked.some((task) => task.sessions.has(record.conversationId)) ? "parked" : "running";
220
+ status = "running";
222
221
  break;
223
222
  case "done":
224
223
  case "unanswered": return [];
@@ -248,35 +247,17 @@ var PiHarness = class extends LifecycleCapability {
248
247
  }
249
248
  /** @internal */
250
249
  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
- };
269
- }
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);
250
+ return _assertClassBrand(_PiHarness_brand, this, _admitDraft).call(this, session, options.operationId, {
251
+ type: "input",
252
+ content: input,
253
+ whenBusy: options.whenBusy ?? "followUp",
254
+ ...options.whenIdle ? { whenIdle: options.whenIdle } : {},
255
+ ...options.entry ? { entry: options.entry } : {}
278
256
  });
279
- return true;
257
+ }
258
+ /** @internal Start a run on the context as it is, without new input. */
259
+ enqueueContinue(session, options) {
260
+ return _assertClassBrand(_PiHarness_brand, this, _admitDraft).call(this, session, options.operationId, { type: "continue" });
280
261
  }
281
262
  /** @internal Withdraw a queued input, or abort the run it joined. */
282
263
  async withdraw(session, operationId) {
@@ -313,10 +294,28 @@ var PiHarness = class extends LifecycleCapability {
313
294
  const defaults = _classPrivateFieldGet2(_options, this).defaults;
314
295
  return {
315
296
  ...defaults?.model === void 0 ? {} : { model: modelRef(defaults.model) },
316
- ...defaults?.thinkingLevel === void 0 ? {} : { thinkingLevel: defaults.thinkingLevel }
297
+ ...defaults?.thinkingLevel === void 0 ? {} : { thinkingLevel: defaults.thinkingLevel },
298
+ ...defaults?.maxTurns === void 0 ? {} : { maxTurns: defaults.maxTurns }
317
299
  };
318
300
  }
319
301
  };
302
+ async function _admitDraft(session, requested, draft) {
303
+ const operationId = requested ?? crypto.randomUUID();
304
+ const { storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
305
+ const conversation = await this.conversation(session);
306
+ return {
307
+ operationId,
308
+ session,
309
+ accepted: await _assertClassBrand(_PiHarness_brand, this, _admit).call(this, async () => {
310
+ const accepted = await storage.submissionByRequest(conversation.id, operationId, BG) === void 0;
311
+ await conversation.submit({
312
+ ...draft,
313
+ requestId: operationId
314
+ }, BG);
315
+ return accepted;
316
+ })
317
+ };
318
+ }
320
319
  /**
321
320
  * Hand pi work inside the wake. The wake is scheduled first, so if the
322
321
  * object dies after pi has the work, that job restarts it; while `hand`
@@ -481,29 +480,6 @@ function _piClosing(opening, pi, closed) {
481
480
  _classPrivateFieldSet2(_opening, this, void 0);
482
481
  _classPrivateFieldSet2(_lastClose, this, closed);
483
482
  }
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
483
  async function _findSubmission(id, operationId, context) {
508
484
  const { pi, storage } = await _assertClassBrand(_PiHarness_brand, this, _open).call(this);
509
485
  const record = await storage.submissionByRequest(id, operationId, context);
@@ -516,7 +492,7 @@ async function _result(session, operationId, settled, context) {
516
492
  status: "unanswered",
517
493
  reason: settled.reason
518
494
  };
519
- if (settled.type !== "input") return {
495
+ if (settled.type === "write") return {
520
496
  operationId,
521
497
  session,
522
498
  status: "done"
@@ -551,6 +527,16 @@ var PiSession = class {
551
527
  messages: await this.messages()
552
528
  };
553
529
  }
530
+ /**
531
+ * Start a run on the transcript as it is, without a new message: for
532
+ * example, after a run ended unanswered because the model failed or the
533
+ * object kept crashing. Only an idle session takes one; a busy one rejects
534
+ * with pi-durable's `ConversationBusy`. A transcript that ends in an answer
535
+ * settles `unanswered` with reason `nothing_to_answer`.
536
+ */
537
+ continue(options = {}) {
538
+ return _classPrivateFieldGet2(_harness, this).enqueueContinue(this.id, options);
539
+ }
554
540
  /** Join the running work after its current tool round. */
555
541
  steer(input, options = {}) {
556
542
  return this.submit(input, {
@@ -572,14 +558,6 @@ var PiSession = class {
572
558
  await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).abort(BG);
573
559
  return true;
574
560
  }
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
561
  /** Start a new context, optionally from a handoff note. */
584
562
  async reset(handoff) {
585
563
  await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).reset(handoff, BG);
@@ -595,9 +573,25 @@ var PiSession = class {
595
573
  async messages() {
596
574
  return [...(await (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).context(BG)).entries];
597
575
  }
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);
576
+ /**
577
+ * Watch this session: pi's conversation view, a window over the transcript
578
+ * with the session's documents, such as `pi.live` (the answer being
579
+ * streamed, running tools, and why the last run ended without an answer)
580
+ * and `pi.inbox`. The first value holds the newest entries; new entries
581
+ * are appended. Each change carries pi's operations, for a client that
582
+ * forwards them. To render more than the window, merge each value into a
583
+ * `ConversationTranscript` that pages with `entries()`.
584
+ */
585
+ async watch(window, context = BG) {
586
+ return (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).watch(context, window);
587
+ }
588
+ /**
589
+ * A page of this session's transcript, newest first unless `query.order`
590
+ * is `ascending`, including entries before its resets. The loader a
591
+ * `ConversationTranscript` takes.
592
+ */
593
+ async entries(query, limit, context = BG) {
594
+ return (await _classPrivateFieldGet2(_harness, this).conversation(this.id)).entries(query, limit, void 0, context);
601
595
  }
602
596
  async busy() {
603
597
  return (await (await _classPrivateFieldGet2(_harness, this).pi()).snapshot(LiveDoc, conversationId(this.id), BG))?.run !== void 0;
@@ -655,182 +649,84 @@ var PiSessions = class {
655
649
  };
656
650
  //#endregion
657
651
  //#region src/harness/pi/skills.ts
658
- function skillsFingerprint(sources) {
659
- return sources.map((source) => `${source.id}:${source.fingerprint}`).join("|");
660
- }
661
652
  function textResult(text) {
662
653
  return { output: [{
663
654
  type: "text",
664
655
  text
665
656
  }] };
666
657
  }
667
- function attributes(pairs) {
668
- return pairs.filter((pair) => pair[1] !== void 0).map(([key, value]) => ` ${key}="${value.replace(/"/g, """)}"`).join("");
669
- }
670
- function describeResource(resource) {
671
- const details = [
672
- resource.kind,
673
- resource.encoding ?? "text",
674
- resource.mimeType,
675
- resource.size === void 0 ? void 0 : `${resource.size} bytes`
676
- ].filter((value) => value !== void 0);
677
- return `- ${resource.path} (${details.join(", ")})`;
678
- }
679
- function renderSkillContent(skill) {
680
- const resources = skill.content.resources ?? [];
681
- const lines = [`<skill_content${attributes([["name", skill.descriptor.name], ["version", skill.descriptor.version]])}>`, skill.content.body.trim()];
682
- if (resources.length > 0) {
683
- lines.push("<skill_resources>");
684
- for (const resource of resources) lines.push(` <file${attributes([
685
- ["kind", resource.kind],
686
- ["encoding", resource.encoding ?? "text"],
687
- ["size", resource.size === void 0 ? void 0 : String(resource.size)]
688
- ])}>${resource.path}</file>`);
689
- lines.push("</skill_resources>");
690
- }
691
- lines.push("</skill_content>", "");
692
- lines.push(resources.length === 0 ? "No bundled resources." : ["Bundled resources:", ...resources.map(describeResource)].join("\n"));
693
- return lines.join("\n");
658
+ /** A failed tool call: pi sends it to the model as an error result. */
659
+ function errorResult(error) {
660
+ return {
661
+ output: [{
662
+ type: "text",
663
+ text: error.message
664
+ }],
665
+ isError: true
666
+ };
694
667
  }
695
668
  /**
696
- * Resolve `agents/skills` sources into a system-prompt catalog and two
697
- * tools.
698
- *
699
- * The first source to list a name wins, as in `SkillRegistry`. pi-durable
700
- * has no skill concept of its own, so the model activates skills through
701
- * `activate_skill` and reads bundled files through `read_skill_resource` —
702
- * the same tools `@cloudflare/think` offers, so skills authored for one
703
- * framework work in the other.
669
+ * Model-facing tools for a catalog: `activate_skill` and
670
+ * `read_skill_resource`. These match the tools `@cloudflare/think` offers, so
671
+ * a skill behaves the same in both frameworks.
704
672
  */
705
- async function resolveSkillSources(sources) {
706
- const fingerprint = skillsFingerprint(sources);
707
- const warnings = [];
708
- const resolved = /* @__PURE__ */ new Map();
709
- for (const source of sources) {
710
- let descriptors;
711
- try {
712
- descriptors = await source.list();
713
- } catch (error) {
714
- warnings.push(`Skill source "${source.id}" failed to list skills: ${error instanceof Error ? error.message : String(error)}`);
715
- continue;
716
- }
717
- for (const descriptor of descriptors) {
718
- if (resolved.has(descriptor.name)) {
719
- warnings.push(`Skill "${descriptor.name}" from source "${source.id}" is shadowed by an earlier source`);
720
- continue;
721
- }
722
- let content;
723
- try {
724
- content = await source.load(descriptor.name);
725
- } catch (error) {
726
- warnings.push(`Skill "${descriptor.name}" from source "${source.id}" failed to load: ${error instanceof Error ? error.message : String(error)}`);
727
- continue;
728
- }
729
- if (!content) continue;
730
- resolved.set(descriptor.name, {
731
- descriptor,
732
- content,
733
- source
734
- });
735
- }
736
- }
737
- const visible = [...resolved.values()].filter((skill) => skill.descriptor.metadata?.["disable-model-invocation"] !== true);
738
- if (visible.length === 0) return {
739
- fingerprint,
740
- tools: [],
741
- catalog: null,
742
- warnings
743
- };
744
- const names = visible.map((skill) => skill.descriptor.name);
745
- const nameSchema = Type.Union(names.map((name) => Type.Literal(name)));
746
- const byName = new Map(visible.map((skill) => [skill.descriptor.name, skill]));
747
- const activateSkill = {
673
+ function skillTools(catalog) {
674
+ const names = catalog.skills.map((entry) => entry.skill.name);
675
+ if (names.length === 0) return [];
676
+ const nameSchema = StringEnum(names);
677
+ return [{
748
678
  name: "activate_skill",
749
679
  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.",
750
- parameters: Type.Object({ name: nameSchema }),
680
+ parameters: obj({ name: nameSchema }, { open: true }),
751
681
  replay: "safe",
752
682
  async execute(input) {
753
- const skill = byName.get(input.name);
754
- return textResult(skill ? renderSkillContent(skill) : `Skill not found: ${input.name}`);
683
+ const activated = catalog.activate(input.name);
684
+ return activated.ok ? textResult(activated.value) : errorResult(activated.error);
755
685
  }
756
- };
757
- const readResource = {
686
+ }, {
758
687
  name: "read_skill_resource",
759
- 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.",
760
- parameters: Type.Object({
761
- name: Type.Optional(nameSchema),
762
- path: Type.String({ minLength: 1 })
763
- }),
688
+ 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.",
689
+ parameters: obj({
690
+ name: opt(nameSchema),
691
+ path: str({ minLength: 1 })
692
+ }, { open: true }),
764
693
  replay: "safe",
765
694
  async execute(input) {
766
- const target = resolveResourceTarget(byName, input.name, input.path);
767
- if (!target) return textResult(`Skill resource not found: ${input.name ? `${input.name}/` : ""}${input.path}`);
768
- const resource = await target.skill.source.readResource?.(target.skill.descriptor.name, target.path).catch(() => null);
769
- if (!resource) return textResult(`Skill resource not found: ${target.skill.descriptor.name}/${target.path}`);
770
- return textResult([
771
- `<skill_resource${attributes([
772
- ["name", target.skill.descriptor.name],
773
- ["path", resource.path],
774
- ["kind", resource.kind],
775
- ["encoding", resource.encoding ?? "text"],
776
- ["mimeType", resource.mimeType]
777
- ])}>`,
778
- resource.content,
779
- "</skill_resource>"
780
- ].join("\n"));
695
+ const read = await catalog.readResource(input.name === void 0 ? { path: input.path } : {
696
+ name: input.name,
697
+ path: input.path
698
+ });
699
+ return read.ok ? textResult(renderSkillResource(read.value)) : errorResult(read.error);
781
700
  }
782
- };
783
- const catalog = [
784
- "Available skills. When a task matches a skill, use activate_skill with its name before proceeding.",
785
- "",
786
- ...visible.map((skill) => `- ${skill.descriptor.name}: ${skill.descriptor.description}`)
787
- ].join("\n");
788
- return {
789
- fingerprint,
790
- tools: [activateSkill, readResource],
791
- catalog,
792
- warnings
793
- };
794
- }
795
- function resolveResourceTarget(byName, name, path) {
796
- if (path.split("/").some((segment) => segment === "..")) return void 0;
797
- if (name !== void 0) {
798
- const skill = byName.get(name);
799
- return skill && hasResource(skill, path) ? {
800
- skill,
801
- path
802
- } : void 0;
803
- }
804
- const [head, ...rest] = path.split("/");
805
- const skill = head === void 0 ? void 0 : byName.get(head);
806
- const relative = rest.join("/");
807
- return skill && relative && hasResource(skill, relative) ? {
808
- skill,
809
- path: relative
810
- } : void 0;
811
- }
812
- function hasResource(skill, path) {
813
- return (skill.content.resources ?? []).some((resource) => resource.path === path);
701
+ }];
814
702
  }
815
703
  /**
816
704
  * `agents/skills` sources as a pi extension, named `agents.skills`: the
817
705
  * activation tools and a `skills` system prompt section. Install it on
818
- * pi's registry in the harness factory.
706
+ * pi's registry in the harness factory. Sources are read once, when the
707
+ * extension is built.
819
708
  *
820
709
  * ```ts
821
- * registry.install(await skills(sources));
710
+ * import * as agentSkills from "agents/skills";
711
+ *
712
+ * registry.install(
713
+ * await skills([agentSkills.workspace(workspace), agentSkills.r2(env.SKILLS)])
714
+ * );
822
715
  * ```
716
+ *
717
+ * @param sources - Skill sources in priority order.
718
+ * @returns The pi extension.
823
719
  */
824
720
  async function skills(sources) {
825
- const resolved = await resolveSkillSources(sources);
826
- for (const warning of resolved.warnings) console.warn(`pi skills: ${warning}`);
827
- const catalog = resolved.catalog;
721
+ const catalog = await loadSkillCatalog(sources);
722
+ for (const diagnostic of catalog.diagnostics) console.warn(`pi skills: ${formatSkillDiagnostic(diagnostic)}`);
723
+ const prompt = catalog.prompt();
828
724
  return {
829
725
  name: "agents.skills",
830
- tools: resolved.tools,
831
- sections: catalog === null ? [] : [{
726
+ tools: skillTools(catalog),
727
+ sections: prompt === null ? [] : [{
832
728
  key: "skills",
833
- render: () => catalog
729
+ render: () => prompt
834
730
  }]
835
731
  };
836
732
  }