@ipab/cli 0.1.0-rc.11 → 0.1.0-rc.13

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 (151) hide show
  1. package/README.md +15 -10
  2. package/dist/authoring.d.ts +6 -2
  3. package/dist/authoring.d.ts.map +1 -1
  4. package/dist/authoring.js +65 -18
  5. package/dist/authoring.js.map +1 -1
  6. package/dist/builtins/skill.ipab.car +0 -0
  7. package/dist/cli-daemon.d.ts +14 -0
  8. package/dist/cli-daemon.d.ts.map +1 -0
  9. package/dist/cli-daemon.js +400 -0
  10. package/dist/cli-daemon.js.map +1 -0
  11. package/dist/cli.d.ts +2 -1
  12. package/dist/cli.d.ts.map +1 -1
  13. package/dist/cli.js +1631 -1100
  14. package/dist/cli.js.map +1 -1
  15. package/dist/config-secret.js +1 -1
  16. package/dist/config-secret.js.map +1 -1
  17. package/dist/daemon-api.d.ts +80 -0
  18. package/dist/daemon-api.d.ts.map +1 -0
  19. package/dist/daemon-api.js +2411 -0
  20. package/dist/daemon-api.js.map +1 -0
  21. package/dist/daemon-client.d.ts +322 -0
  22. package/dist/daemon-client.d.ts.map +1 -0
  23. package/dist/daemon-client.js +400 -0
  24. package/dist/daemon-client.js.map +1 -0
  25. package/dist/daemon-protocol.d.ts +22 -0
  26. package/dist/daemon-protocol.d.ts.map +1 -0
  27. package/dist/daemon-protocol.js +39 -0
  28. package/dist/daemon-protocol.js.map +1 -0
  29. package/dist/daemon-telemetry.d.ts +86 -0
  30. package/dist/daemon-telemetry.d.ts.map +1 -0
  31. package/dist/daemon-telemetry.js +502 -0
  32. package/dist/daemon-telemetry.js.map +1 -0
  33. package/dist/daemon-ui.d.ts +69 -0
  34. package/dist/daemon-ui.d.ts.map +1 -1
  35. package/dist/daemon-ui.js +285 -4
  36. package/dist/daemon-ui.js.map +1 -1
  37. package/dist/daemon.d.ts +10 -0
  38. package/dist/daemon.d.ts.map +1 -1
  39. package/dist/daemon.js +289 -38
  40. package/dist/daemon.js.map +1 -1
  41. package/dist/history.d.ts +3 -1
  42. package/dist/history.d.ts.map +1 -1
  43. package/dist/history.js +16 -1
  44. package/dist/history.js.map +1 -1
  45. package/dist/identity.d.ts.map +1 -1
  46. package/dist/identity.js +7 -2
  47. package/dist/identity.js.map +1 -1
  48. package/dist/ipc.d.ts +19 -7
  49. package/dist/ipc.d.ts.map +1 -1
  50. package/dist/ipc.js +96 -70
  51. package/dist/ipc.js.map +1 -1
  52. package/dist/kubo-topology.d.ts +2 -0
  53. package/dist/kubo-topology.d.ts.map +1 -1
  54. package/dist/kubo-topology.js +17 -20
  55. package/dist/kubo-topology.js.map +1 -1
  56. package/dist/kubo.d.ts +5 -3
  57. package/dist/kubo.d.ts.map +1 -1
  58. package/dist/kubo.js +83 -13
  59. package/dist/kubo.js.map +1 -1
  60. package/dist/name-reference.d.ts +8 -0
  61. package/dist/name-reference.d.ts.map +1 -0
  62. package/dist/name-reference.js +63 -0
  63. package/dist/name-reference.js.map +1 -0
  64. package/dist/pins.d.ts.map +1 -1
  65. package/dist/pins.js +18 -3
  66. package/dist/pins.js.map +1 -1
  67. package/dist/pivot-rpc.d.ts +37 -2
  68. package/dist/pivot-rpc.d.ts.map +1 -1
  69. package/dist/pivot-rpc.js +188 -8
  70. package/dist/pivot-rpc.js.map +1 -1
  71. package/dist/search-index.d.ts.map +1 -1
  72. package/dist/search-index.js +1 -0
  73. package/dist/search-index.js.map +1 -1
  74. package/dist/skill-definition.d.ts +9 -0
  75. package/dist/skill-definition.d.ts.map +1 -1
  76. package/dist/skill-definition.js +26 -2
  77. package/dist/skill-definition.js.map +1 -1
  78. package/dist/skill-source.d.ts +1 -0
  79. package/dist/skill-source.d.ts.map +1 -1
  80. package/dist/skill-source.js +10 -4
  81. package/dist/skill-source.js.map +1 -1
  82. package/dist/skills.d.ts +8 -2
  83. package/dist/skills.d.ts.map +1 -1
  84. package/dist/skills.js +31 -2
  85. package/dist/skills.js.map +1 -1
  86. package/dist/state-storage.d.ts.map +1 -1
  87. package/dist/state-storage.js +2 -1
  88. package/dist/state-storage.js.map +1 -1
  89. package/dist/state.d.ts +1 -1
  90. package/dist/state.d.ts.map +1 -1
  91. package/dist/state.js +3 -2
  92. package/dist/state.js.map +1 -1
  93. package/dist/swarm-files.d.ts.map +1 -1
  94. package/dist/swarm-files.js +7 -2
  95. package/dist/swarm-files.js.map +1 -1
  96. package/dist/swarm-fs-core.d.ts +217 -0
  97. package/dist/swarm-fs-core.d.ts.map +1 -0
  98. package/dist/swarm-fs-core.js +885 -0
  99. package/dist/swarm-fs-core.js.map +1 -0
  100. package/dist/swarm-fs-path.d.ts +17 -0
  101. package/dist/swarm-fs-path.d.ts.map +1 -0
  102. package/dist/swarm-fs-path.js +46 -0
  103. package/dist/swarm-fs-path.js.map +1 -0
  104. package/dist/swarm-fs.d.ts +9 -0
  105. package/dist/swarm-fs.d.ts.map +1 -0
  106. package/dist/swarm-fs.js +21 -0
  107. package/dist/swarm-fs.js.map +1 -0
  108. package/dist/swarm-observations.d.ts +6 -0
  109. package/dist/swarm-observations.d.ts.map +1 -1
  110. package/dist/swarm-observations.js +28 -8
  111. package/dist/swarm-observations.js.map +1 -1
  112. package/dist/swarm.d.ts +134 -18
  113. package/dist/swarm.d.ts.map +1 -1
  114. package/dist/swarm.js +1293 -484
  115. package/dist/swarm.js.map +1 -1
  116. package/dist/unixfs-tree.d.ts +55 -0
  117. package/dist/unixfs-tree.d.ts.map +1 -1
  118. package/dist/unixfs-tree.js +361 -2
  119. package/dist/unixfs-tree.js.map +1 -1
  120. package/package.json +5 -5
  121. package/src/authoring.ts +88 -28
  122. package/src/cli-daemon.ts +482 -0
  123. package/src/cli.ts +2080 -1464
  124. package/src/config-secret.ts +1 -1
  125. package/src/daemon-api.ts +3148 -0
  126. package/src/daemon-client.ts +975 -0
  127. package/src/daemon-protocol.ts +61 -0
  128. package/src/daemon-telemetry.ts +656 -0
  129. package/src/daemon-ui.ts +364 -4
  130. package/src/daemon.ts +337 -43
  131. package/src/history.ts +26 -1
  132. package/src/identity.ts +10 -2
  133. package/src/ipc.ts +113 -79
  134. package/src/kubo-topology.ts +25 -22
  135. package/src/kubo.ts +127 -9
  136. package/src/name-reference.ts +91 -0
  137. package/src/pins.ts +23 -3
  138. package/src/pivot-rpc.ts +272 -14
  139. package/src/search-index.ts +1 -0
  140. package/src/skill-definition.ts +45 -3
  141. package/src/skill-source.ts +10 -4
  142. package/src/skills.ts +43 -2
  143. package/src/state-storage.ts +5 -1
  144. package/src/state.ts +3 -1
  145. package/src/swarm-files.ts +10 -2
  146. package/src/swarm-fs-core.ts +1461 -0
  147. package/src/swarm-fs-path.ts +68 -0
  148. package/src/swarm-fs.ts +104 -0
  149. package/src/swarm-observations.ts +40 -14
  150. package/src/swarm.ts +2149 -1033
  151. package/src/unixfs-tree.ts +447 -2
package/src/pivot-rpc.ts CHANGED
@@ -26,6 +26,8 @@ import {
26
26
  } from "@ipab/bundle/pointer/scope";
27
27
  import { HostError, message, object } from "./errors.js";
28
28
  import {
29
+ p2pClose,
30
+ p2pListen,
29
31
  probePeer,
30
32
  validateAddress,
31
33
  type KuboNode,
@@ -42,6 +44,159 @@ export const PIVOT_RECEIPT_PROTOCOL = PIVOT_PROTOCOL;
42
44
  export const MAX_PIVOT_RPC_BYTES = 64 * 1024;
43
45
  const MAX_REQUEST_URL_BYTES = 4096;
44
46
 
47
+ export interface PivotStatus {
48
+ v: 1;
49
+ ready: boolean;
50
+ headCid: string | null;
51
+ indexedHeadCid: string | null;
52
+ syncError: string | null;
53
+ }
54
+
55
+ export interface PivotReplicateRequest {
56
+ v: 1;
57
+ service: string;
58
+ scopeId: string;
59
+ headId: string;
60
+ epoch: number;
61
+ headCid: string;
62
+ member: string;
63
+ timestamp: number;
64
+ nonce: string;
65
+ signature: string;
66
+ }
67
+
68
+ export function pivotReplicateSigningMessage(
69
+ request: Omit<PivotReplicateRequest, "signature">,
70
+ ): string {
71
+ return JSON.stringify([
72
+ "IPAB-PIVOT-REPLICATE/1",
73
+ request.service,
74
+ request.scopeId,
75
+ request.headId,
76
+ request.epoch,
77
+ request.headCid,
78
+ request.member,
79
+ request.timestamp,
80
+ request.nonce,
81
+ ]);
82
+ }
83
+
84
+ export function parsePivotReplicateRequest(
85
+ input: unknown,
86
+ ): PivotReplicateRequest {
87
+ const data = object(input, "Pivot replicate request");
88
+ let validCid: boolean;
89
+ try {
90
+ validCid =
91
+ typeof data.headCid === "string" &&
92
+ CID.parse(data.headCid).toV1().toString() === data.headCid;
93
+ } catch {
94
+ throw new HostError("Invalid replicate headCid.");
95
+ }
96
+ if (
97
+ Object.keys(data).sort().join() !==
98
+ "epoch,headCid,headId,member,nonce,scopeId,service,signature,timestamp,v" ||
99
+ data.v !== 1 ||
100
+ typeof data.service !== "string" ||
101
+ canonicalService(data.service) !== data.service ||
102
+ typeof data.scopeId !== "string" ||
103
+ !RESOURCE_ID_PATTERN.test(data.scopeId) ||
104
+ typeof data.headId !== "string" ||
105
+ !RESOURCE_ID_PATTERN.test(data.headId) ||
106
+ typeof data.epoch !== "number" ||
107
+ !Number.isSafeInteger(data.epoch) ||
108
+ data.epoch < 1 ||
109
+ typeof data.headCid !== "string" ||
110
+ !validCid ||
111
+ !isCanonicalPublicKey(data.member) ||
112
+ typeof data.timestamp !== "number" ||
113
+ !Number.isSafeInteger(data.timestamp) ||
114
+ data.timestamp < 0 ||
115
+ typeof data.nonce !== "string" ||
116
+ !/^[A-Za-z0-9_-]{22,128}$/.test(data.nonce) ||
117
+ typeof data.signature !== "string" ||
118
+ !/^[A-Za-z0-9_-]{86}$/.test(data.signature)
119
+ )
120
+ throw new HostError("Invalid Pivot replicate request.");
121
+ return {
122
+ v: 1,
123
+ service: data.service,
124
+ scopeId: data.scopeId,
125
+ headId: data.headId,
126
+ epoch: data.epoch,
127
+ headCid: data.headCid,
128
+ member: data.member,
129
+ timestamp: data.timestamp,
130
+ nonce: data.nonce,
131
+ signature: data.signature,
132
+ };
133
+ }
134
+
135
+ export async function signPivotReplicateRequest(
136
+ input: Omit<PivotReplicateRequest, "v" | "timestamp" | "nonce" | "signature">,
137
+ privateKey: string,
138
+ ): Promise<PivotReplicateRequest> {
139
+ const body: Omit<PivotReplicateRequest, "signature"> = {
140
+ ...input,
141
+ v: 1,
142
+ timestamp: Math.floor(Date.now() / 1000),
143
+ nonce: toBase64Url(randomBytes(24)),
144
+ };
145
+ return parsePivotReplicateRequest({
146
+ ...body,
147
+ signature: await signEd25519(
148
+ await importPrivateKeyPkcs8(privateKey),
149
+ pivotReplicateSigningMessage(body),
150
+ ),
151
+ });
152
+ }
153
+
154
+ export async function verifyPivotReplicateRequest(
155
+ request: PivotReplicateRequest,
156
+ nowSec = Math.floor(Date.now() / 1000),
157
+ ): Promise<void> {
158
+ if (Math.abs(request.timestamp - nowSec) > 60)
159
+ throw new HostError(
160
+ "Replicate request timestamp must be within 60 seconds.",
161
+ );
162
+ const { signature, ...body } = request;
163
+ if (
164
+ !(await verifyEd25519(
165
+ await importPublicKeyRaw(request.member),
166
+ signature,
167
+ pivotReplicateSigningMessage(body),
168
+ ))
169
+ )
170
+ throw new HostError(
171
+ "Invalid member signature on Pivot replicate request.",
172
+ 1,
173
+ "PIVOT_UNAUTHORIZED",
174
+ );
175
+ }
176
+
177
+ export function parsePivotStatus(input: unknown): PivotStatus {
178
+ const data = object(input, "Pivot status");
179
+ const cid = (value: unknown): value is string | null =>
180
+ value === null ||
181
+ (typeof value === "string" && CID.parse(value).toV1().toString() === value);
182
+ if (
183
+ data.v !== 1 ||
184
+ typeof data.ready !== "boolean" ||
185
+ !cid(data.headCid) ||
186
+ !cid(data.indexedHeadCid) ||
187
+ !(data.syncError === null || typeof data.syncError === "string") ||
188
+ (data.ready && data.headCid === null)
189
+ )
190
+ throw new HostError("Invalid Pivot status.");
191
+ return {
192
+ v: 1,
193
+ ready: data.ready,
194
+ headCid: data.headCid,
195
+ indexedHeadCid: data.indexedHeadCid,
196
+ syncError: data.syncError,
197
+ };
198
+ }
199
+
45
200
  export interface PivotReceiptRequest {
46
201
  v: 1;
47
202
  service: string;
@@ -225,6 +380,7 @@ async function readBoundedBody(request: IncomingMessage): Promise<string> {
225
380
  export interface PivotServer {
226
381
  port: number;
227
382
  address: string;
383
+ readonly listening: boolean;
228
384
  close: () => Promise<void>;
229
385
  }
230
386
 
@@ -237,6 +393,11 @@ export type PivotReceiptHandler = (
237
393
 
238
394
  export interface PivotServiceHandlers {
239
395
  receipt?: PivotReceiptHandler;
396
+ status?: (signal: AbortSignal) => Promise<PivotStatus>;
397
+ replicate?: (
398
+ request: PivotReplicateRequest,
399
+ signal: AbortSignal,
400
+ ) => Promise<{ ready: true; headCid: string }>;
240
401
  search?: (params: ValidatedSearchParams) => SearchApiResponse | null;
241
402
  diagnostic?: (text: string) => void;
242
403
  }
@@ -271,6 +432,53 @@ export async function startPivotHttpServer(
271
432
  }
272
433
  const parsedUrl = new URL(rawUrl, "http://127.0.0.1");
273
434
 
435
+ if (
436
+ (parsedUrl.pathname === "/v1/status" && handlers.status) ||
437
+ (parsedUrl.pathname === "/v1/replicate" && handlers.replicate)
438
+ ) {
439
+ const statusRequest = parsedUrl.pathname === "/v1/status";
440
+ if (request.method !== (statusRequest ? "GET" : "POST")) {
441
+ sendJson(response, 405, { error: "Method not allowed." });
442
+ return;
443
+ }
444
+ try {
445
+ if (statusRequest) {
446
+ sendJson(response, 200, await handlers.status!(controller.signal));
447
+ } else {
448
+ let input: unknown;
449
+ try {
450
+ input = JSON.parse(await readBoundedBody(request)) as unknown;
451
+ } catch (cause) {
452
+ if (cause instanceof ServiceError) throw cause;
453
+ throw new HostError("Invalid replicate JSON.");
454
+ }
455
+ const result = await handlers.replicate!(
456
+ parsePivotReplicateRequest(input),
457
+ controller.signal,
458
+ );
459
+ sendJson(response, 200, result);
460
+ }
461
+ } catch (cause) {
462
+ const status =
463
+ cause instanceof ServiceError
464
+ ? cause.status
465
+ : cause instanceof HostError &&
466
+ cause.code === "PIVOT_UNAUTHORIZED"
467
+ ? 403
468
+ : cause instanceof HostError
469
+ ? 400
470
+ : 500;
471
+ handlers.diagnostic?.(
472
+ `Pivot ${statusRequest ? "status" : "replicate"} failed: ${message(cause)}`,
473
+ );
474
+ if (!response.headersSent)
475
+ sendJson(response, status, {
476
+ error: { message: message(cause).slice(0, 512) },
477
+ });
478
+ }
479
+ return;
480
+ }
481
+
274
482
  if (parsedUrl.pathname === "/v1/search") {
275
483
  try {
276
484
  if (request.method !== "GET") {
@@ -398,6 +606,9 @@ export async function startPivotHttpServer(
398
606
  return {
399
607
  port: address.port,
400
608
  address: `/ip4/127.0.0.1/tcp/${address.port}`,
609
+ get listening() {
610
+ return server.listening;
611
+ },
401
612
  close,
402
613
  };
403
614
  }
@@ -446,25 +657,26 @@ async function kuboP2pRpc(
446
657
  );
447
658
  }
448
659
 
660
+ export async function ensurePivotListener(
661
+ node: KuboNode,
662
+ targetAddress: string,
663
+ signal?: AbortSignal,
664
+ fetcher: typeof fetch = fetch,
665
+ ): Promise<boolean> {
666
+ return p2pListen(node, PIVOT_PROTOCOL, targetAddress, signal, fetcher);
667
+ }
668
+
449
669
  export async function mountPivotServer(
450
670
  node: KuboNode,
451
671
  handlers: PivotServiceHandlers,
452
672
  fetcher: typeof fetch = fetch,
673
+ signal?: AbortSignal,
453
674
  ): Promise<PivotServer> {
454
675
  const httpServer = await startPivotHttpServer(handlers);
455
676
  const target = httpServer.address;
677
+ const selfListen = `/p2p/${node.peerId}`;
456
678
  try {
457
- // Clear any stale listener for this protocol on the same Swarm node first.
458
- await kuboP2pRpc(
459
- node,
460
- `p2p/close?protocol=${encodeURIComponent(PIVOT_PROTOCOL)}`,
461
- fetcher,
462
- ).catch(() => {});
463
- await kuboP2pRpc(
464
- node,
465
- `p2p/listen?arg=${encodeURIComponent(PIVOT_PROTOCOL)}&arg=${encodeURIComponent(target)}`,
466
- fetcher,
467
- );
679
+ await p2pListen(node, PIVOT_PROTOCOL, target, signal, fetcher);
468
680
  } catch (cause) {
469
681
  await httpServer.close();
470
682
  throw cause;
@@ -472,11 +684,19 @@ export async function mountPivotServer(
472
684
  return {
473
685
  port: httpServer.port,
474
686
  address: target,
687
+ get listening() {
688
+ return httpServer.listening;
689
+ },
475
690
  close: async () => {
476
691
  try {
477
- await kuboP2pRpc(
692
+ await p2pClose(
478
693
  node,
479
- `p2p/close?protocol=${encodeURIComponent(PIVOT_PROTOCOL)}&target-address=${encodeURIComponent(target)}`,
694
+ {
695
+ protocol: PIVOT_PROTOCOL,
696
+ listenAddress: selfListen,
697
+ targetAddress: target,
698
+ },
699
+ undefined,
480
700
  fetcher,
481
701
  ).catch(() => {});
482
702
  } finally {
@@ -490,8 +710,9 @@ export async function mountPivotReceiptServer(
490
710
  node: KuboNode,
491
711
  handler: PivotReceiptHandler,
492
712
  fetcher: typeof fetch = fetch,
713
+ signal?: AbortSignal,
493
714
  ): Promise<PivotReceiptServer> {
494
- return mountPivotServer(node, { receipt: handler }, fetcher);
715
+ return mountPivotServer(node, { receipt: handler }, fetcher, signal);
495
716
  }
496
717
 
497
718
  export async function withPivotP2pForward<T>(
@@ -578,6 +799,7 @@ export async function requestPivotReceiptOverP2p(
578
799
  `Pivot receipt RPC returned non-JSON (HTTP ${response.status}).`,
579
800
  );
580
801
  }
802
+
581
803
  if (!response.ok) {
582
804
  const detail =
583
805
  parsed &&
@@ -599,3 +821,39 @@ export async function requestPivotReceiptOverP2p(
599
821
  },
600
822
  );
601
823
  }
824
+
825
+ export async function pivotReadinessOverP2p(
826
+ options: Parameters<typeof withPivotP2pForward>[0],
827
+ request?: PivotReplicateRequest,
828
+ ): Promise<PivotStatus | { ready: true; headCid: string }> {
829
+ return withPivotP2pForward(options, async ({ host, port }) => {
830
+ const response = await (options.fetcher ?? fetch)(
831
+ `http://${host}:${port}/v1/${request ? "replicate" : "status"}`,
832
+ {
833
+ method: request ? "POST" : "GET",
834
+ headers: {
835
+ accept: "application/json",
836
+ "content-type": "application/json",
837
+ },
838
+ ...(request ? { body: JSON.stringify(request) } : {}),
839
+ redirect: "error",
840
+ cache: "no-store",
841
+ signal: AbortSignal.any([
842
+ ...(options.signal ? [options.signal] : []),
843
+ AbortSignal.timeout(request ? 150_000 : 15_000),
844
+ ]),
845
+ },
846
+ );
847
+ const text = await boundedText(response);
848
+ if (!response.ok)
849
+ throw new HostError(
850
+ `Pivot ${request ? "replicate" : "status"} failed (HTTP ${response.status}): ${text.slice(0, 512)}. Ensure the Pivot runs a version supporting /v1/status and /v1/replicate.`,
851
+ );
852
+ const input: unknown = JSON.parse(text);
853
+ if (!request) return parsePivotStatus(input);
854
+ const result = object(input, "Pivot replicate response");
855
+ if (result.ready !== true || result.headCid !== request.headCid)
856
+ throw new HostError("Pivot did not confirm the requested HEAD.");
857
+ return { ready: true, headCid: request.headCid };
858
+ });
859
+ }
@@ -538,6 +538,7 @@ export async function indexV7UnixfsSwarm(
538
538
  };
539
539
 
540
540
  await diffDirectory(oldRoot, newRoot, "", 0);
541
+ options.signal?.throwIfAborted();
541
542
  const indexedAt = (options.now ?? (() => new Date().toISOString()))();
542
543
  state.applySearchIndexDelta(configUrl, {
543
544
  headCid: canonicalHead,
@@ -25,9 +25,19 @@ export const skillType: BundleType = {
25
25
  },
26
26
  };
27
27
  export const skillSchema = await encodeSchema(skillType);
28
- export async function skillIdentity(
28
+
29
+ export interface SkillMetadata {
30
+ name: string;
31
+ description: string;
32
+ license?: string;
33
+ compatibility?: string;
34
+ metadata?: Record<string, string>;
35
+ allowedTools?: string;
36
+ }
37
+
38
+ export async function inspectSkillBundle(
29
39
  bundle: Awaited<ReturnType<typeof openBundle>>,
30
- ): Promise<string> {
40
+ ): Promise<SkillMetadata> {
31
41
  if (
32
42
  bundle.schemaKey !== skillSchema.cid.toString() ||
33
43
  bundle.manifest.entry !== "SKILL.md" ||
@@ -49,5 +59,37 @@ export async function skillIdentity(
49
59
  metadata.name.length > 64
50
60
  )
51
61
  throw new HostError("Invalid Skill frontmatter name.");
52
- return metadata.name;
62
+ if (
63
+ typeof metadata.description !== "string" ||
64
+ metadata.description.length === 0 ||
65
+ metadata.description.length > 1024
66
+ )
67
+ throw new HostError("Invalid Skill frontmatter description.");
68
+ const result: SkillMetadata = {
69
+ name: metadata.name,
70
+ description: metadata.description,
71
+ };
72
+ if (typeof metadata.license === "string") result.license = metadata.license;
73
+ if (typeof metadata.compatibility === "string")
74
+ result.compatibility = metadata.compatibility;
75
+ if (
76
+ metadata.metadata &&
77
+ typeof metadata.metadata === "object" &&
78
+ !Array.isArray(metadata.metadata)
79
+ ) {
80
+ const entries = Object.entries(
81
+ metadata.metadata as Record<string, unknown>,
82
+ );
83
+ if (entries.every(([, value]) => typeof value === "string"))
84
+ result.metadata = Object.fromEntries(entries as [string, string][]);
85
+ }
86
+ if (typeof metadata["allowed-tools"] === "string")
87
+ result.allowedTools = metadata["allowed-tools"];
88
+ return result;
89
+ }
90
+
91
+ export async function skillIdentity(
92
+ bundle: Awaited<ReturnType<typeof openBundle>>,
93
+ ): Promise<string> {
94
+ return (await inspectSkillBundle(bundle)).name;
53
95
  }
@@ -2,7 +2,7 @@ import { fileURLToPath } from "node:url";
2
2
  import { buffer } from "node:stream/consumers";
3
3
  import { openBundle } from "@ipab/bundle";
4
4
  import { readCar } from "./authoring.js";
5
- import { skillIdentity } from "./skill-definition.js";
5
+ import { inspectSkillBundle, skillIdentity } from "./skill-definition.js";
6
6
 
7
7
  async function loadSkill() {
8
8
  const car = await readCar(
@@ -12,7 +12,13 @@ async function loadSkill() {
12
12
  const bundle = await openBundle(car.root, { resolveCid: car.resolveCid });
13
13
  if ((await skillIdentity(bundle)) !== "ipab")
14
14
  throw new Error("IPAB Skill seed identity must be ipab.");
15
- return { cid: car.root.toString(), blocks: car.blocks, bundle };
15
+ const metadata = await inspectSkillBundle(bundle);
16
+ return {
17
+ cid: car.root.toString(),
18
+ blocks: car.blocks,
19
+ bundle,
20
+ description: metadata.description,
21
+ };
16
22
  }
17
23
 
18
24
  export async function seedSkillSource() {
@@ -21,9 +27,9 @@ export async function seedSkillSource() {
21
27
  }
22
28
 
23
29
  export async function bundledSkillSource() {
24
- const { cid, bundle } = await loadSkill();
30
+ const { cid, bundle, description } = await loadSkill();
25
31
  const files = new Map<string, Uint8Array>();
26
32
  for (const path of bundle.files)
27
33
  files.set(path, await buffer(await bundle.readFile(path)));
28
- return { cid, files };
34
+ return { cid, files, description };
29
35
  }
package/src/skills.ts CHANGED
@@ -30,17 +30,20 @@ import {
30
30
  import {
31
31
  swarmHeader,
32
32
  swarmSkills,
33
+ swarmTypeSkills,
33
34
  syncPivotReplicationAndIndex,
34
35
  syncSwarmRegistry,
35
36
  syncSwarmTemplates,
37
+ syncSwarmTypeSkills,
36
38
  type SharedSkill,
37
39
  type SwarmContext,
40
+ type TypeSkillAssociation,
38
41
  } from "./swarm.js";
39
42
 
40
43
  export const skillsInstaller = "skills@1.9.1";
41
44
  export const bundledSkillOrigin = "bundled:ipab";
42
45
  type Fingerprint = Record<string, string>;
43
- interface Installation extends SharedSkill {
46
+ interface Installation extends Omit<SharedSkill, "description"> {
44
47
  swarm: string;
45
48
  path: string;
46
49
  agents: string[];
@@ -585,6 +588,7 @@ export async function syncDaemonSwarm(
585
588
  headCid: headerInfo.head?.root ?? null,
586
589
  stateCid: headerInfo.stateCid ?? null,
587
590
  skills: { status: "skipped", count: 0, error: null },
591
+ typeSkills: { status: "skipped", count: 0, error: null },
588
592
  templates: { status: "skipped", count: 0, error: null },
589
593
  registry: { status: "skipped", count: 0, error: null },
590
594
  };
@@ -592,6 +596,7 @@ export async function syncDaemonSwarm(
592
596
  for (const [label, task] of [
593
597
  ["pivot-index", () => syncPivotReplicationAndIndex(context, swarm, header)],
594
598
  ["skills", () => syncDaemonSkills(context, swarm, port, false)],
599
+ ["typeSkills", () => syncSwarmTypeSkills(context, swarm, header)],
595
600
  ["templates", () => syncSwarmTemplates(context, swarm, header)],
596
601
  ["registry", () => syncSwarmRegistry(context, swarm, header)],
597
602
  ] as const) {
@@ -672,6 +677,35 @@ export interface SkillsResult {
672
677
  }[];
673
678
  ok: boolean;
674
679
  }
680
+ export interface SkillsListResult extends SkillsResult {
681
+ typeSkills: TypeSkillAssociation[];
682
+ }
683
+ export async function listSwarmSkills(
684
+ context: SwarmContext,
685
+ swarm: string,
686
+ options: SkillsOptions,
687
+ port: SkillsPort = officialSkillsPort,
688
+ refresh?: { forceRefresh?: boolean },
689
+ ): Promise<SkillsListResult> {
690
+ const result = await manageSkills(context, swarm, options, port);
691
+ const typeSkills = await swarmTypeSkills(context, swarm, {
692
+ mode: "local-first",
693
+ ...(refresh?.forceRefresh !== undefined
694
+ ? { forceRefresh: refresh.forceRefresh }
695
+ : {}),
696
+ });
697
+ return {
698
+ swarm: result.swarm,
699
+ installer: result.installer,
700
+ agents: result.agents,
701
+ dryRun: result.dryRun,
702
+ items: result.items,
703
+ typeSkills,
704
+ orphaned: result.orphaned,
705
+ results: result.results,
706
+ ok: result.ok,
707
+ };
708
+ }
675
709
  export async function inspectBundledSkill(
676
710
  source: Awaited<ReturnType<typeof bundledSkillSource>>,
677
711
  port: SkillsPort,
@@ -818,7 +852,14 @@ export async function manageSkills(
818
852
  await context.store.recoverOwner(owner, []);
819
853
  const discovered =
820
854
  "bundledSkill" in context
821
- ? [{ name: "ipab", identity: "ipab", cid: context.bundledSkill.cid }]
855
+ ? [
856
+ {
857
+ name: "ipab",
858
+ identity: "ipab",
859
+ cid: context.bundledSkill.cid,
860
+ description: context.bundledSkill.description,
861
+ },
862
+ ]
822
863
  : await swarmSkills(context, swarm, options.names, owner);
823
864
  const items: SkillStatus[] = [];
824
865
  const snapshots = new Map<string, Fingerprint | null>();
@@ -266,7 +266,11 @@ function access(directory: string, initialize: boolean, sealed = false): void {
266
266
  join(directory, database),
267
267
  names.includes(`${database}-wal`),
268
268
  ));
269
- if (!managed && entries.some((e) => e.name !== "kubo-bin")) unsafe();
269
+ if (
270
+ !managed &&
271
+ entries.some((e) => e.name !== "kubo-bin" && e.name !== "swarm-daemon")
272
+ )
273
+ unsafe();
270
274
 
271
275
  if (windows()) {
272
276
  if (
package/src/state.ts CHANGED
@@ -262,9 +262,11 @@ export class State {
262
262
  directory: string,
263
263
  private readonly builtins: readonly NamedPin[] = [],
264
264
  env: NodeJS.ProcessEnv = process.env,
265
+ wrapDb?: (db: DatabaseSync) => DatabaseSync,
265
266
  ) {
266
267
  this.directory = resolve(directory);
267
- this.db = new DatabaseSync(openStateStorage(this.directory, env));
268
+ const rawDb = new DatabaseSync(openStateStorage(this.directory, env));
269
+ this.db = wrapDb ? wrapDb(rawDb) : rawDb;
268
270
  try {
269
271
  enableWal(this.db);
270
272
  this.db.exec(`PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;
@@ -8,8 +8,16 @@ import {
8
8
  secureNew,
9
9
  type ProtectedPath,
10
10
  } from "./identity.js";
11
- import { nodeError } from "./authoring.js";
12
- import { HostError } from "./errors.js";
11
+ import { HostError } from "./host-error.js";
12
+
13
+ function nodeError(cause: unknown, code: string): boolean {
14
+ return (
15
+ typeof cause === "object" &&
16
+ cause !== null &&
17
+ "code" in cause &&
18
+ (cause as { code?: unknown }).code === code
19
+ );
20
+ }
13
21
 
14
22
  export async function privateDirectory(
15
23
  parent: string,