@caplets/pi 0.0.0 → 0.1.3

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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -1
  3. package/dist/index.js +54 -16
  4. package/package.json +23 -10
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Spirit-Led Software LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -26,4 +26,9 @@ pnpm add -D @caplets/pi
26
26
 
27
27
  The extension reads your existing Caplets configuration through `@caplets/core`; it does not create or mutate Pi config files.
28
28
 
29
- New or removed Caplets are snapshotted at extension load. Restart Pi or reload extensions to refresh the native tool list.
29
+ The extension hot reloads Caplets config and Caplet file edits. Existing tools execute against
30
+ the latest valid backend config. Newly added Caplets are registered in the current Pi session;
31
+ removed or disabled Caplets are deactivated with Pi's active-tool APIs when available. If Pi is
32
+ running without `getActiveTools()` / `setActiveTools()`, stale tools may remain registered until
33
+ Pi reloads extensions or restarts, but calls to removed Caplets return Caplets' normal structured
34
+ "server not found" error.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createNativeCapletsService } from "@caplets/core/native";
1
+ import { createNativeCapletsService, registerNativeCapletsProcessCleanup } from "@caplets/core/native";
2
2
  import { Type } from "@sinclair/typebox";
3
3
  import { operations } from "@caplets/core/generated-tool-input-schema";
4
4
  //#region src/schema.ts
@@ -15,9 +15,43 @@ function capletsPiParameters() {
15
15
  //#endregion
16
16
  //#region src/index.ts
17
17
  function capletsPiExtension(pi, options = {}) {
18
+ const ownsService = !options.service;
18
19
  const service = options.service ?? createNativeCapletsService();
19
- if (!options.service) registerProcessCleanup(service);
20
- for (const caplet of service.listTools()) pi.registerTool({
20
+ if (ownsService) registerNativeCapletsProcessCleanup(service);
21
+ const registeredCapletToolSignatures = /* @__PURE__ */ new Map();
22
+ let knownCapletTools = new Set(pi.getActiveTools?.().filter((name) => name.startsWith("caplets_")) ?? []);
23
+ const syncTools = (caplets = service.listTools()) => {
24
+ const nextCapletTools = new Set(caplets.map((caplet) => caplet.toolName));
25
+ for (const [toolName] of registeredCapletToolSignatures) if (!nextCapletTools.has(toolName)) registeredCapletToolSignatures.delete(toolName);
26
+ for (const caplet of caplets) {
27
+ const signature = piToolSignature(caplet);
28
+ if (registeredCapletToolSignatures.get(caplet.toolName) === signature) continue;
29
+ registeredCapletToolSignatures.set(caplet.toolName, signature);
30
+ pi.registerTool(createPiTool(service, caplet));
31
+ }
32
+ if (pi.getActiveTools && pi.setActiveTools) {
33
+ const activeNonCaplets = pi.getActiveTools().filter((name) => !knownCapletTools.has(name) && !nextCapletTools.has(name));
34
+ pi.setActiveTools([...activeNonCaplets, ...nextCapletTools]);
35
+ }
36
+ knownCapletTools = nextCapletTools;
37
+ };
38
+ syncTools();
39
+ const unsubscribe = service.onToolsChanged(syncTools);
40
+ pi.on?.("session_shutdown", () => {
41
+ unsubscribe();
42
+ if (ownsService) service.close();
43
+ });
44
+ }
45
+ function piToolSignature(caplet) {
46
+ return JSON.stringify({
47
+ caplet: caplet.caplet,
48
+ title: caplet.title,
49
+ description: caplet.description,
50
+ promptGuidance: caplet.promptGuidance
51
+ });
52
+ }
53
+ function createPiTool(service, caplet) {
54
+ return {
21
55
  name: caplet.toolName,
22
56
  label: caplet.title,
23
57
  description: caplet.description,
@@ -26,26 +60,30 @@ function capletsPiExtension(pi, options = {}) {
26
60
  parameters: capletsPiParameters(),
27
61
  async execute(_toolCallId, params) {
28
62
  const result = await service.execute(caplet.caplet, params);
63
+ const serialized = serializeResult(result);
29
64
  return {
30
65
  content: [{
31
66
  type: "text",
32
- text: JSON.stringify(result, null, 2)
67
+ text: serialized.text
33
68
  }],
34
- details: { result }
69
+ details: serialized.serializationError ? {
70
+ result,
71
+ serializationError: serialized.serializationError
72
+ } : { result }
35
73
  };
36
74
  }
37
- });
38
- }
39
- function registerProcessCleanup(service) {
40
- let closed = false;
41
- const close = () => {
42
- if (closed) return;
43
- closed = true;
44
- service.close();
45
75
  };
46
- process.once("beforeExit", close);
47
- process.once("SIGINT", close);
48
- process.once("SIGTERM", close);
76
+ }
77
+ function serializeResult(result) {
78
+ try {
79
+ return { text: JSON.stringify(result, null, 2) ?? "null" };
80
+ } catch (error) {
81
+ const serializationError = error instanceof Error ? error.message : String(error);
82
+ return {
83
+ text: `[Serialization error: ${serializationError}]`,
84
+ serializationError
85
+ };
86
+ }
49
87
  }
50
88
  //#endregion
51
89
  export { capletsPiExtension as default };
package/package.json CHANGED
@@ -1,7 +1,18 @@
1
1
  {
2
2
  "name": "@caplets/pi",
3
- "version": "0.0.0",
3
+ "version": "0.1.3",
4
4
  "description": "Native Pi extension for Caplets.",
5
+ "homepage": "https://github.com/spiritledsoftware/caplets#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/spiritledsoftware/caplets/issues"
8
+ },
9
+ "license": "MIT",
10
+ "author": "Spirit-Led Software LLC",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/spiritledsoftware/caplets.git",
14
+ "directory": "packages/pi"
15
+ },
5
16
  "files": [
6
17
  "dist",
7
18
  "README.md"
@@ -11,15 +22,12 @@
11
22
  "exports": {
12
23
  ".": "./dist/index.js"
13
24
  },
14
- "scripts": {
15
- "build": "rm -rf dist && rolldown -c",
16
- "prepack": "pnpm build",
17
- "typecheck": "tsc --noEmit",
18
- "test": "vitest run"
25
+ "publishConfig": {
26
+ "access": "public"
19
27
  },
20
28
  "dependencies": {
21
- "@caplets/core": "workspace:*",
22
- "@sinclair/typebox": "^0.34.49"
29
+ "@sinclair/typebox": "^0.34.49",
30
+ "@caplets/core": "0.12.1"
23
31
  },
24
32
  "devDependencies": {
25
33
  "@types/node": "^25.7.0",
@@ -28,9 +36,14 @@
28
36
  "vitest": "^4.1.6"
29
37
  },
30
38
  "peerDependencies": {
31
- "@earendil-works/pi-coding-agent": ">=0"
39
+ "@earendil-works/pi-coding-agent": "^0.74.0"
32
40
  },
33
41
  "engines": {
34
42
  "node": ">=22"
43
+ },
44
+ "scripts": {
45
+ "build": "rm -rf dist && rolldown -c",
46
+ "typecheck": "tsc --noEmit",
47
+ "test": "vitest run"
35
48
  }
36
- }
49
+ }