@herbertgao/pi-extensions 2026.9.11 → 2026.9.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 (192) hide show
  1. package/README.md +4 -4
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
  4. package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +8 -0
  5. package/node_modules/@herbertgao/pi-subagents/package.json +1 -1
  6. package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +5 -4
  7. package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +39 -16
  8. package/node_modules/@juicesharp/rpiv-ask-user-question/package.json +2 -2
  9. package/node_modules/@narumitw/pi-btw/README.md +21 -4
  10. package/node_modules/@narumitw/pi-btw/dist/index.ts +1668 -958
  11. package/node_modules/@narumitw/pi-btw/dist/index.ts.map +4 -4
  12. package/node_modules/@narumitw/pi-btw/docs/workflows.md +10 -2
  13. package/node_modules/@narumitw/pi-btw/package.json +1 -1
  14. package/node_modules/@narumitw/pi-btw/src/btw.ts +17 -79
  15. package/node_modules/@narumitw/pi-btw/src/conversation-context.ts +74 -0
  16. package/node_modules/@narumitw/pi-btw/src/fullscreen-ui.ts +196 -7
  17. package/node_modules/@narumitw/pi-btw/src/main-thread-updates.ts +40 -0
  18. package/node_modules/@narumitw/pi-btw/src/menu.ts +34 -2
  19. package/node_modules/@narumitw/pi-btw/src/settings.ts +49 -0
  20. package/node_modules/@narumitw/pi-btw/src/transcript-pager.ts +9 -1
  21. package/node_modules/@narumitw/pi-btw/src/workspace-layout.ts +559 -0
  22. package/node_modules/pi-mcp-adapter/CHANGELOG.md +62 -0
  23. package/node_modules/pi-mcp-adapter/README.md +75 -7
  24. package/node_modules/pi-mcp-adapter/bearer-command-resolver.ts +193 -0
  25. package/node_modules/pi-mcp-adapter/cli.js +56 -5
  26. package/node_modules/pi-mcp-adapter/commands.ts +96 -5
  27. package/node_modules/pi-mcp-adapter/config.ts +73 -6
  28. package/node_modules/pi-mcp-adapter/direct-tool-surface.ts +22 -28
  29. package/node_modules/pi-mcp-adapter/direct-tools.ts +13 -5
  30. package/node_modules/pi-mcp-adapter/dist/abort.d.ts +2 -0
  31. package/node_modules/pi-mcp-adapter/dist/abort.js +36 -0
  32. package/node_modules/pi-mcp-adapter/dist/abort.js.map +1 -0
  33. package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.d.ts +6 -0
  34. package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js +194 -0
  35. package/node_modules/pi-mcp-adapter/dist/bearer-command-resolver.js.map +1 -0
  36. package/node_modules/pi-mcp-adapter/dist/config.d.ts +5 -0
  37. package/node_modules/pi-mcp-adapter/dist/config.js +75 -6
  38. package/node_modules/pi-mcp-adapter/dist/config.js.map +1 -1
  39. package/node_modules/pi-mcp-adapter/dist/consent-manager.d.ts +18 -0
  40. package/node_modules/pi-mcp-adapter/dist/consent-manager.js +88 -0
  41. package/node_modules/pi-mcp-adapter/dist/consent-manager.js.map +1 -0
  42. package/node_modules/pi-mcp-adapter/dist/elicitation-handler.d.ts +17 -0
  43. package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js +316 -0
  44. package/node_modules/pi-mcp-adapter/dist/elicitation-handler.js.map +1 -0
  45. package/node_modules/pi-mcp-adapter/dist/errors.d.ts +131 -0
  46. package/node_modules/pi-mcp-adapter/dist/errors.js +278 -0
  47. package/node_modules/pi-mcp-adapter/dist/errors.js.map +1 -0
  48. package/node_modules/pi-mcp-adapter/dist/http-ca.d.ts +8 -0
  49. package/node_modules/pi-mcp-adapter/dist/http-ca.js +88 -0
  50. package/node_modules/pi-mcp-adapter/dist/http-ca.js.map +1 -0
  51. package/node_modules/pi-mcp-adapter/dist/jev-client.d.ts +15 -0
  52. package/node_modules/pi-mcp-adapter/dist/jev-client.js +340 -0
  53. package/node_modules/pi-mcp-adapter/dist/jev-client.js.map +1 -0
  54. package/node_modules/pi-mcp-adapter/dist/jev-contracts.d.ts +77 -0
  55. package/node_modules/pi-mcp-adapter/dist/jev-contracts.js +2 -0
  56. package/node_modules/pi-mcp-adapter/dist/jev-contracts.js.map +1 -0
  57. package/node_modules/pi-mcp-adapter/dist/jev-key-store.d.ts +22 -0
  58. package/node_modules/pi-mcp-adapter/dist/jev-key-store.js +85 -0
  59. package/node_modules/pi-mcp-adapter/dist/jev-key-store.js.map +1 -0
  60. package/node_modules/pi-mcp-adapter/dist/json-schema-validator.d.ts +2 -0
  61. package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js +55 -0
  62. package/node_modules/pi-mcp-adapter/dist/json-schema-validator.js.map +1 -0
  63. package/node_modules/pi-mcp-adapter/dist/lifecycle.d.ts +56 -0
  64. package/node_modules/pi-mcp-adapter/dist/lifecycle.js +410 -0
  65. package/node_modules/pi-mcp-adapter/dist/lifecycle.js.map +1 -0
  66. package/node_modules/pi-mcp-adapter/dist/logger.d.ts +51 -0
  67. package/node_modules/pi-mcp-adapter/dist/logger.js +131 -0
  68. package/node_modules/pi-mcp-adapter/dist/logger.js.map +1 -0
  69. package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.d.ts +26 -0
  70. package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js +103 -0
  71. package/node_modules/pi-mcp-adapter/dist/mcp-auth-fetch.js.map +1 -0
  72. package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.d.ts +118 -0
  73. package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js +1098 -0
  74. package/node_modules/pi-mcp-adapter/dist/mcp-auth-flow.js.map +1 -0
  75. package/node_modules/pi-mcp-adapter/dist/mcp-auth.d.ts +168 -0
  76. package/node_modules/pi-mcp-adapter/dist/mcp-auth.js +1117 -0
  77. package/node_modules/pi-mcp-adapter/dist/mcp-auth.js.map +1 -0
  78. package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js +7 -140
  79. package/node_modules/pi-mcp-adapter/dist/mcp-bearer-store.js.map +1 -1
  80. package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.d.ts +53 -0
  81. package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js +441 -0
  82. package/node_modules/pi-mcp-adapter/dist/mcp-callback-server.js.map +1 -0
  83. package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.d.ts +148 -0
  84. package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js +689 -0
  85. package/node_modules/pi-mcp-adapter/dist/mcp-oauth-provider.js.map +1 -0
  86. package/node_modules/pi-mcp-adapter/dist/mcp-probe.d.ts +6 -0
  87. package/node_modules/pi-mcp-adapter/dist/mcp-probe.js +166 -0
  88. package/node_modules/pi-mcp-adapter/dist/mcp-probe.js.map +1 -0
  89. package/node_modules/pi-mcp-adapter/dist/mcp-tasks.d.ts +102 -0
  90. package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js +369 -0
  91. package/node_modules/pi-mcp-adapter/dist/mcp-tasks.js.map +1 -0
  92. package/node_modules/pi-mcp-adapter/dist/mcp-trace.d.ts +95 -0
  93. package/node_modules/pi-mcp-adapter/dist/mcp-trace.js +242 -0
  94. package/node_modules/pi-mcp-adapter/dist/mcp-trace.js.map +1 -0
  95. package/node_modules/pi-mcp-adapter/dist/metadata-cache.js +2 -11
  96. package/node_modules/pi-mcp-adapter/dist/metadata-cache.js.map +1 -1
  97. package/node_modules/pi-mcp-adapter/dist/npx-resolver.d.ts +6 -0
  98. package/node_modules/pi-mcp-adapter/dist/npx-resolver.js +505 -0
  99. package/node_modules/pi-mcp-adapter/dist/npx-resolver.js.map +1 -0
  100. package/node_modules/pi-mcp-adapter/dist/request-headers-command.d.ts +10 -0
  101. package/node_modules/pi-mcp-adapter/dist/request-headers-command.js +312 -0
  102. package/node_modules/pi-mcp-adapter/dist/request-headers-command.js.map +1 -0
  103. package/node_modules/pi-mcp-adapter/dist/runtime-owner.d.ts +13 -0
  104. package/node_modules/pi-mcp-adapter/dist/runtime-owner.js +97 -0
  105. package/node_modules/pi-mcp-adapter/dist/runtime-owner.js.map +1 -0
  106. package/node_modules/pi-mcp-adapter/dist/sampling-handler.d.ts +17 -0
  107. package/node_modules/pi-mcp-adapter/dist/sampling-handler.js +203 -0
  108. package/node_modules/pi-mcp-adapter/dist/sampling-handler.js.map +1 -0
  109. package/node_modules/pi-mcp-adapter/dist/secure-keyring.d.ts +11 -0
  110. package/node_modules/pi-mcp-adapter/dist/secure-keyring.js +172 -0
  111. package/node_modules/pi-mcp-adapter/dist/secure-keyring.js.map +1 -0
  112. package/node_modules/pi-mcp-adapter/dist/server-manager.d.ts +157 -0
  113. package/node_modules/pi-mcp-adapter/dist/server-manager.js +1743 -0
  114. package/node_modules/pi-mcp-adapter/dist/server-manager.js.map +1 -0
  115. package/node_modules/pi-mcp-adapter/dist/session-approvals.d.ts +34 -0
  116. package/node_modules/pi-mcp-adapter/dist/session-approvals.js +140 -0
  117. package/node_modules/pi-mcp-adapter/dist/session-approvals.js.map +1 -0
  118. package/node_modules/pi-mcp-adapter/dist/session-recovery.d.ts +26 -0
  119. package/node_modules/pi-mcp-adapter/dist/session-recovery.js +142 -0
  120. package/node_modules/pi-mcp-adapter/dist/session-recovery.js.map +1 -0
  121. package/node_modules/pi-mcp-adapter/dist/state.d.ts +73 -0
  122. package/node_modules/pi-mcp-adapter/dist/state.js +2 -0
  123. package/node_modules/pi-mcp-adapter/dist/state.js.map +1 -0
  124. package/node_modules/pi-mcp-adapter/dist/types.d.ts +36 -0
  125. package/node_modules/pi-mcp-adapter/dist/types.js +18 -0
  126. package/node_modules/pi-mcp-adapter/dist/types.js.map +1 -1
  127. package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.d.ts +17 -0
  128. package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js +219 -0
  129. package/node_modules/pi-mcp-adapter/dist/ui-resource-handler.js.map +1 -0
  130. package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.d.ts +15 -0
  131. package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js +85 -0
  132. package/node_modules/pi-mcp-adapter/dist/unix-socket-transport.js.map +1 -0
  133. package/node_modules/pi-mcp-adapter/dist/utils.d.ts +1 -0
  134. package/node_modules/pi-mcp-adapter/dist/utils.js +13 -0
  135. package/node_modules/pi-mcp-adapter/dist/utils.js.map +1 -1
  136. package/node_modules/pi-mcp-adapter/elicitation-handler.ts +29 -19
  137. package/node_modules/pi-mcp-adapter/examples/jev-accessibility-loop.mjs +85 -0
  138. package/node_modules/pi-mcp-adapter/examples/jev-semantic-filter.mjs +34 -0
  139. package/node_modules/pi-mcp-adapter/index.ts +129 -6
  140. package/node_modules/pi-mcp-adapter/init.ts +6 -19
  141. package/node_modules/pi-mcp-adapter/jev-client.ts +273 -0
  142. package/node_modules/pi-mcp-adapter/jev-contracts.ts +53 -0
  143. package/node_modules/pi-mcp-adapter/jev-key-store.ts +79 -0
  144. package/node_modules/pi-mcp-adapter/lifecycle.ts +14 -3
  145. package/node_modules/pi-mcp-adapter/mcp-bearer-store.ts +7 -142
  146. package/node_modules/pi-mcp-adapter/mcp-code.ts +119 -11
  147. package/node_modules/pi-mcp-adapter/mcp-output-guard.ts +4 -0
  148. package/node_modules/pi-mcp-adapter/mcp-references.ts +5 -3
  149. package/node_modules/pi-mcp-adapter/mcp-script-worker.mjs +5 -0
  150. package/node_modules/pi-mcp-adapter/mcp-tasks.ts +467 -0
  151. package/node_modules/pi-mcp-adapter/metadata-cache.ts +2 -13
  152. package/node_modules/pi-mcp-adapter/namespace-tools.ts +1 -1
  153. package/node_modules/pi-mcp-adapter/package.json +17 -9
  154. package/node_modules/pi-mcp-adapter/proxy-modes.ts +319 -166
  155. package/node_modules/pi-mcp-adapter/request-headers-command.ts +6 -3
  156. package/node_modules/pi-mcp-adapter/search-ranking.ts +38 -7
  157. package/node_modules/pi-mcp-adapter/secure-keyring.ts +170 -0
  158. package/node_modules/pi-mcp-adapter/semantic-search.ts +186 -0
  159. package/node_modules/pi-mcp-adapter/server-manager.ts +293 -50
  160. package/node_modules/pi-mcp-adapter/session-approvals.ts +14 -0
  161. package/node_modules/pi-mcp-adapter/skills/mcp-scripting/SKILL.md +2 -0
  162. package/node_modules/pi-mcp-adapter/state.ts +3 -1
  163. package/node_modules/pi-mcp-adapter/tool-approval.ts +26 -4
  164. package/node_modules/pi-mcp-adapter/tool-metadata.ts +6 -14
  165. package/node_modules/pi-mcp-adapter/tool-registrar.ts +8 -6
  166. package/node_modules/pi-mcp-adapter/tool-result-renderer.ts +4 -1
  167. package/node_modules/pi-mcp-adapter/types.ts +53 -0
  168. package/node_modules/pi-mcp-adapter/ui-server.ts +17 -9
  169. package/node_modules/pi-mcp-adapter/utils.ts +16 -0
  170. package/node_modules/pi-multi-account/CHANGELOG.md +23 -0
  171. package/node_modules/pi-multi-account/README.md +38 -11
  172. package/node_modules/pi-multi-account/index.ts +371 -96
  173. package/node_modules/pi-multi-account/package.json +5 -4
  174. package/node_modules/pi-multi-account/provider-payload-stream.ts +36 -28
  175. package/node_modules/pi-multi-account/usage.ts +31 -2
  176. package/node_modules/pi-typesafe/README.md +3 -1
  177. package/node_modules/pi-typesafe/dist/auth.d.ts +10 -3
  178. package/node_modules/pi-typesafe/dist/auth.js +13 -7
  179. package/node_modules/pi-typesafe/dist/backends.d.ts +31 -0
  180. package/node_modules/pi-typesafe/dist/backends.js +33 -0
  181. package/node_modules/pi-typesafe/dist/client.d.ts +3 -9
  182. package/node_modules/pi-typesafe/dist/client.js +63 -39
  183. package/node_modules/pi-typesafe/dist/credentials.d.ts +11 -5
  184. package/node_modules/pi-typesafe/dist/credentials.js +15 -8
  185. package/node_modules/pi-typesafe/dist/extension.js +4 -1
  186. package/node_modules/pi-typesafe/dist/index.d.ts +1 -1
  187. package/node_modules/pi-typesafe/dist/index.js +1 -1
  188. package/node_modules/pi-typesafe/dist/login.d.ts +13 -7
  189. package/node_modules/pi-typesafe/dist/login.js +17 -8
  190. package/node_modules/pi-typesafe/dist/schema.js +19 -13
  191. package/node_modules/pi-typesafe/package.json +1 -1
  192. package/package.json +11 -10
@@ -0,0 +1,312 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { randomUUID } from "node:crypto";
3
+ import { interpolateEnvVars } from "./utils.js";
4
+ const DEFAULT_TIMEOUT_MS = 10_000;
5
+ const MAX_OUTPUT_BYTES = 64 * 1024;
6
+ const USE_PROCESS_GROUP = process.platform !== "win32";
7
+ const CLEANUP_TOKEN_ENV = "PI_MCP_REQUEST_HEADERS_CLEANUP_TOKEN";
8
+ // Busy hosts can exceed spawnSync's 1 MiB default when `ps axeww` dumps each
9
+ // process environment. Keep discovery below a bounded cap instead of letting
10
+ // `ps` get SIGTERM'd and reporting a false cleanup failure.
11
+ const PS_MAX_BUFFER_BYTES = 8 * 1024 * 1024;
12
+ function isNoSuchProcessError(error) {
13
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ESRCH";
14
+ }
15
+ function runPosixPs(args) {
16
+ if (process.env.PI_MCP_ADAPTER_TEST_FAIL_PS === "1")
17
+ return { status: 1, signal: null, stdout: "" };
18
+ const result = spawnSync("ps", args, { encoding: "utf8", maxBuffer: PS_MAX_BUFFER_BYTES });
19
+ return { status: result.status, signal: result.signal, stdout: result.stdout };
20
+ }
21
+ function psFailureReason(result) {
22
+ return result.status === null
23
+ ? `ps was killed by signal ${result.signal ?? "unknown"}`
24
+ : `ps exited with code ${result.status}`;
25
+ }
26
+ function collectPosixProcessPids(rootPid, cleanupToken) {
27
+ const result = runPosixPs(["axeww", "-o", "pid=,ppid=,command="]);
28
+ if (result.status !== 0) {
29
+ throw new Error(`HTTP request headers command cleanup failed: ${psFailureReason(result)}`);
30
+ }
31
+ const childrenByParent = new Map();
32
+ const processPids = new Set();
33
+ const needle = cleanupToken ? `${CLEANUP_TOKEN_ENV}=${cleanupToken}` : undefined;
34
+ for (const line of result.stdout.split("\n")) {
35
+ const match = /^\s*(\d+)\s+(\d+)(?:\s+(.*))?$/.exec(line);
36
+ if (!match)
37
+ continue;
38
+ const pid = Number(match[1]);
39
+ const ppid = Number(match[2]);
40
+ if (!Number.isInteger(pid) || !Number.isInteger(ppid))
41
+ continue;
42
+ const children = childrenByParent.get(ppid);
43
+ if (children)
44
+ children.push(pid);
45
+ else
46
+ childrenByParent.set(ppid, [pid]);
47
+ if (needle && pid !== process.pid && match[3]?.includes(needle))
48
+ processPids.add(pid);
49
+ }
50
+ const stack = [...(childrenByParent.get(rootPid) ?? [])];
51
+ while (stack.length > 0) {
52
+ const pid = stack.pop();
53
+ processPids.add(pid);
54
+ stack.push(...(childrenByParent.get(pid) ?? []));
55
+ }
56
+ return [...processPids];
57
+ }
58
+ function assertPosixProcessDiscoveryAvailable() {
59
+ const result = runPosixPs(["axeww", "-o", "pid=,ppid=,command="]);
60
+ if (result.status !== 0) {
61
+ throw new Error(`HTTP request headers command cleanup failed: ${psFailureReason(result)}`);
62
+ }
63
+ }
64
+ function isTaskkillNoSuchProcess(result) {
65
+ return `${result.stdout ?? ""}\n${result.stderr ?? ""}`.toLowerCase().includes("not found");
66
+ }
67
+ function signalPid(pid, signal) {
68
+ try {
69
+ process.kill(pid, signal);
70
+ }
71
+ catch (error) {
72
+ if (!isNoSuchProcessError(error))
73
+ throw error;
74
+ }
75
+ }
76
+ function signalProcessGroup(pid, signal) {
77
+ try {
78
+ process.kill(-pid, signal);
79
+ }
80
+ catch (error) {
81
+ if (!isNoSuchProcessError(error))
82
+ throw error;
83
+ }
84
+ }
85
+ function killRequestHeadersCommand(child, trackedPosixDescendantPids = new Set(), cleanupToken) {
86
+ if (process.platform === "win32" && child.pid !== undefined) {
87
+ const result = spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], {
88
+ encoding: "utf8",
89
+ windowsHide: true,
90
+ });
91
+ if (result.status === 0 || result.status === 128 || isTaskkillNoSuchProcess(result))
92
+ return;
93
+ throw new Error(`HTTP request headers command cleanup failed: taskkill exited with code ${result.status ?? "unknown"}`);
94
+ }
95
+ if (USE_PROCESS_GROUP && child.pid !== undefined) {
96
+ const frozenPids = new Set();
97
+ let cleanupError;
98
+ try {
99
+ signalProcessGroup(child.pid, "SIGSTOP");
100
+ for (const pid of trackedPosixDescendantPids) {
101
+ signalPid(pid, "SIGSTOP");
102
+ frozenPids.add(pid);
103
+ }
104
+ let stablePasses = 0;
105
+ for (let pass = 0; pass < 16; pass++) {
106
+ const candidates = collectPosixProcessPids(child.pid, cleanupToken);
107
+ const newPids = candidates.filter(pid => !frozenPids.has(pid));
108
+ if (newPids.length === 0) {
109
+ stablePasses++;
110
+ if (stablePasses >= 2)
111
+ return;
112
+ continue;
113
+ }
114
+ stablePasses = 0;
115
+ for (const pid of newPids) {
116
+ signalPid(pid, "SIGSTOP");
117
+ frozenPids.add(pid);
118
+ }
119
+ }
120
+ cleanupError = new Error("HTTP request headers command cleanup failed: descendant process tree did not stabilize");
121
+ }
122
+ catch (error) {
123
+ cleanupError = error instanceof Error ? error : new Error(String(error));
124
+ }
125
+ finally {
126
+ signalProcessGroup(child.pid, "SIGKILL");
127
+ for (const pid of frozenPids)
128
+ signalPid(pid, "SIGKILL");
129
+ }
130
+ if (cleanupError)
131
+ throw cleanupError;
132
+ return;
133
+ }
134
+ child.kill("SIGKILL");
135
+ }
136
+ function resolvedCommand(config) {
137
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
138
+ throw new Error("HTTP request headers command must be an object");
139
+ }
140
+ if (typeof config.command !== "string" || config.command.trim() === "") {
141
+ throw new Error("HTTP request headers command requires a non-empty command");
142
+ }
143
+ if (config.args !== undefined && (!Array.isArray(config.args) || config.args.some(arg => typeof arg !== "string"))) {
144
+ throw new Error("HTTP request headers command args must be strings");
145
+ }
146
+ if (config.env !== undefined && (typeof config.env !== "object"
147
+ || Array.isArray(config.env)
148
+ || Object.values(config.env).some(value => typeof value !== "string"))) {
149
+ throw new Error("HTTP request headers command env values must be strings");
150
+ }
151
+ const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
152
+ if (!Number.isInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 60_000) {
153
+ throw new Error("HTTP request headers command timeoutMs must be an integer between 1 and 60000");
154
+ }
155
+ return {
156
+ command: interpolateEnvVars(config.command),
157
+ args: (config.args ?? []).map(interpolateEnvVars),
158
+ env: {
159
+ ...process.env,
160
+ ...Object.fromEntries(Object.entries(config.env ?? {}).map(([key, value]) => [key, interpolateEnvVars(value)])),
161
+ },
162
+ timeoutMs,
163
+ };
164
+ }
165
+ async function invokeRequestHeadersCommand(config, envelope, signal) {
166
+ const resolved = resolvedCommand(config);
167
+ if (USE_PROCESS_GROUP)
168
+ assertPosixProcessDiscoveryAvailable();
169
+ return new Promise((resolve, reject) => {
170
+ let stdout = Buffer.alloc(0);
171
+ let settled = false;
172
+ const cleanupToken = randomUUID();
173
+ const child = spawn(resolved.command, resolved.args, {
174
+ env: { ...resolved.env, [CLEANUP_TOKEN_ENV]: cleanupToken },
175
+ stdio: ["pipe", "pipe", "ignore"],
176
+ windowsHide: true,
177
+ detached: USE_PROCESS_GROUP,
178
+ });
179
+ const trackedPosixDescendantPids = new Set();
180
+ let trackingError;
181
+ const trackPosixDescendants = () => {
182
+ if (!USE_PROCESS_GROUP || child.pid === undefined || settled || trackingError)
183
+ return;
184
+ try {
185
+ for (const pid of collectPosixProcessPids(child.pid, cleanupToken))
186
+ trackedPosixDescendantPids.add(pid);
187
+ }
188
+ catch (error) {
189
+ trackingError = error instanceof Error ? error : new Error(String(error));
190
+ }
191
+ };
192
+ const descendantTracker = USE_PROCESS_GROUP ? setInterval(trackPosixDescendants, 50) : undefined;
193
+ descendantTracker?.unref();
194
+ const finish = (result) => {
195
+ if (settled)
196
+ return;
197
+ settled = true;
198
+ clearTimeout(timer);
199
+ if (descendantTracker)
200
+ clearInterval(descendantTracker);
201
+ signal.removeEventListener("abort", abort);
202
+ if (result.status === "error")
203
+ reject(result.error);
204
+ else
205
+ resolve(result.headers);
206
+ };
207
+ const finishAfterKill = (result) => {
208
+ try {
209
+ killRequestHeadersCommand(child, trackedPosixDescendantPids, cleanupToken);
210
+ finish(result);
211
+ }
212
+ catch (cleanupError) {
213
+ finish({ status: "error", error: cleanupError instanceof Error ? cleanupError : new Error(String(cleanupError)) });
214
+ }
215
+ };
216
+ const failAfterKill = (message) => {
217
+ finishAfterKill({ status: "error", error: trackingError ?? new Error(message) });
218
+ };
219
+ const abort = () => {
220
+ failAfterKill("HTTP request headers command aborted");
221
+ };
222
+ const timer = setTimeout(() => {
223
+ failAfterKill(`HTTP request headers command timed out after ${resolved.timeoutMs}ms`);
224
+ }, resolved.timeoutMs);
225
+ signal.addEventListener("abort", abort, { once: true });
226
+ if (signal.aborted) {
227
+ abort();
228
+ return;
229
+ }
230
+ child.on("error", () => finish({ status: "error", error: new Error("HTTP request headers command failed to start") }));
231
+ child.stdout.on("data", (chunk) => {
232
+ if (settled)
233
+ return;
234
+ stdout = Buffer.concat([stdout, Buffer.from(chunk)]);
235
+ if (stdout.byteLength > MAX_OUTPUT_BYTES) {
236
+ failAfterKill("HTTP request headers command output exceeded 64 KiB");
237
+ }
238
+ });
239
+ child.on("close", code => {
240
+ if (settled)
241
+ return;
242
+ if (trackingError) {
243
+ failAfterKill(trackingError.message);
244
+ return;
245
+ }
246
+ if (code !== 0) {
247
+ failAfterKill(`HTTP request headers command exited with code ${code ?? "unknown"}`);
248
+ return;
249
+ }
250
+ let parsed;
251
+ try {
252
+ parsed = JSON.parse(stdout.toString("utf8"));
253
+ }
254
+ catch {
255
+ finishAfterKill({ status: "error", error: new Error("HTTP request headers command returned invalid JSON") });
256
+ return;
257
+ }
258
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
259
+ finishAfterKill({ status: "error", error: new Error("HTTP request headers command must return a JSON object") });
260
+ return;
261
+ }
262
+ const entries = Object.entries(parsed);
263
+ if (entries.some(([, value]) => typeof value !== "string")) {
264
+ finishAfterKill({ status: "error", error: new Error("HTTP request headers command values must be strings") });
265
+ return;
266
+ }
267
+ try {
268
+ finishAfterKill({ status: "success", headers: new Headers(entries) });
269
+ }
270
+ catch {
271
+ finishAfterKill({ status: "error", error: new Error("HTTP request headers command returned an invalid header") });
272
+ }
273
+ });
274
+ child.stdin.on("error", () => { });
275
+ child.stdin.end(JSON.stringify(envelope));
276
+ });
277
+ }
278
+ /** Wrap fetch so a trusted command can derive headers from the exact request. */
279
+ export function createRequestHeadersCommandFetch(config, delegate = globalThis.fetch) {
280
+ // Validate static configuration before the first request.
281
+ resolvedCommand(config);
282
+ return async (input, init) => {
283
+ const request = new Request(input, init);
284
+ const signal = init?.signal === null
285
+ ? request.signal
286
+ : init?.signal ?? (input instanceof Request ? input.signal : request.signal);
287
+ const body = Buffer.from(await request.clone().arrayBuffer());
288
+ const derived = await invokeRequestHeadersCommand(config, {
289
+ version: 1,
290
+ method: request.method.toUpperCase(),
291
+ url: request.url,
292
+ bodyBase64: body.toString("base64"),
293
+ }, signal);
294
+ const headers = new Headers(request.headers);
295
+ derived.forEach((value, name) => headers.set(name, value));
296
+ return delegate(new URL(request.url), {
297
+ method: request.method,
298
+ headers,
299
+ ...(request.method === "GET" || request.method === "HEAD" ? {} : { body }),
300
+ signal,
301
+ cache: request.cache,
302
+ credentials: request.credentials,
303
+ integrity: request.integrity,
304
+ keepalive: request.keepalive,
305
+ mode: request.mode,
306
+ redirect: request.redirect,
307
+ referrer: request.referrer,
308
+ referrerPolicy: request.referrerPolicy,
309
+ });
310
+ };
311
+ }
312
+ //# sourceMappingURL=request-headers-command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-headers-command.js","sourceRoot":"","sources":["../request-headers-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAqB,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CA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D,MAAM,OAAO,GAAG,MAAM,2BAA2B,CAAC,MAAM,EAAE;YACxD,OAAO,EAAE,CAAC;YACV,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE;YACpC,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;SACpC,EAAE,MAAM,CAAC,CAAC;QACX,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAC7C,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QAC3D,OAAO,QAAQ,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACpC,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,OAAO;YACP,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;YAC1E,MAAM;YACN,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,cAAc,EAAE,OAAO,CAAC,cAAc;SACvC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent";
2
+ export interface McpRuntimeOwner {
3
+ readonly signal: AbortSignal;
4
+ isActive(): boolean;
5
+ addCleanup(cleanup: () => void | Promise<void>): void;
6
+ stop(reason?: string): Promise<void>;
7
+ throwIfInactive(): void;
8
+ }
9
+ export declare function createMcpRuntimeOwner(): McpRuntimeOwner;
10
+ export declare function combineAbortSignals(...signals: Array<AbortSignal | undefined>): AbortSignal | undefined;
11
+ /** Fence session-bound UI calls after the owning extension runtime stops. */
12
+ export declare function createOwnedUi(ui: ExtensionUIContext, owner: McpRuntimeOwner): ExtensionUIContext;
13
+ export declare function isAbortError(error: unknown, signal?: AbortSignal): boolean;
@@ -0,0 +1,97 @@
1
+ import { formatTerminalError } from "./utils.js";
2
+ export function createMcpRuntimeOwner() {
3
+ const controller = new AbortController();
4
+ const cleanups = [];
5
+ let stopPromise;
6
+ const reportCleanupFailure = (error, late) => {
7
+ console.error(`MCP: ${late ? "late " : ""}runtime cleanup failed: ${formatTerminalError(error)}`);
8
+ };
9
+ return {
10
+ signal: controller.signal,
11
+ isActive: () => !controller.signal.aborted,
12
+ addCleanup: cleanup => {
13
+ if (controller.signal.aborted) {
14
+ void Promise.resolve().then(cleanup).catch(error => reportCleanupFailure(error, true));
15
+ return;
16
+ }
17
+ cleanups.push(cleanup);
18
+ },
19
+ stop: (reason = "MCP extension runtime stopped") => {
20
+ if (stopPromise)
21
+ return stopPromise;
22
+ controller.abort(new Error(reason));
23
+ const pendingCleanups = cleanups.splice(0).reverse().map(cleanup => Promise.resolve().then(cleanup));
24
+ stopPromise = Promise.allSettled(pendingCleanups).then(results => {
25
+ const failures = results.flatMap(result => result.status === "rejected" ? [result.reason] : []);
26
+ if (failures.length > 0) {
27
+ const aggregate = new AggregateError(failures, "MCP runtime cleanup failed");
28
+ console.error(`MCP: runtime cleanup failed: ${formatTerminalError(aggregate)}`);
29
+ throw aggregate;
30
+ }
31
+ });
32
+ return stopPromise;
33
+ },
34
+ throwIfInactive: () => controller.signal.throwIfAborted(),
35
+ };
36
+ }
37
+ export function combineAbortSignals(...signals) {
38
+ const active = signals.filter((signal) => signal !== undefined);
39
+ if (active.length === 0)
40
+ return undefined;
41
+ if (active.length === 1)
42
+ return active[0];
43
+ if (typeof AbortSignal.any === "function")
44
+ return AbortSignal.any(active);
45
+ // AbortSignal.any was added after Node 20.0.0, our minimum supported version.
46
+ const controller = new AbortController();
47
+ const alreadyAborted = active.find(signal => signal.aborted);
48
+ if (alreadyAborted) {
49
+ controller.abort(alreadyAborted.reason);
50
+ return controller.signal;
51
+ }
52
+ const onAbort = (event) => {
53
+ for (const signal of active)
54
+ signal.removeEventListener("abort", onAbort);
55
+ controller.abort(event.target.reason);
56
+ };
57
+ for (const signal of active)
58
+ signal.addEventListener("abort", onAbort, { once: true });
59
+ return controller.signal;
60
+ }
61
+ /** Fence session-bound UI calls after the owning extension runtime stops. */
62
+ export function createOwnedUi(ui, owner) {
63
+ const proxies = new WeakMap();
64
+ const wrap = (value) => {
65
+ if ((typeof value !== "object" || value === null) && typeof value !== "function") {
66
+ return value;
67
+ }
68
+ const object = value;
69
+ const existing = proxies.get(object);
70
+ if (existing)
71
+ return existing;
72
+ const proxy = new Proxy(object, {
73
+ get(target, property, receiver) {
74
+ if (!owner.isActive())
75
+ return undefined;
76
+ const member = Reflect.get(target, property, receiver);
77
+ if (typeof member === "function") {
78
+ return (...args) => {
79
+ if (!owner.isActive())
80
+ return undefined;
81
+ return Reflect.apply(member, target, args);
82
+ };
83
+ }
84
+ return owner.isActive() ? wrap(member) : undefined;
85
+ },
86
+ });
87
+ proxies.set(object, proxy);
88
+ return proxy;
89
+ };
90
+ return wrap(ui);
91
+ }
92
+ export function isAbortError(error, signal) {
93
+ if (signal?.aborted)
94
+ return true;
95
+ return error instanceof Error && (error.name === "AbortError" || error.message === "MCP extension runtime stopped");
96
+ }
97
+ //# sourceMappingURL=runtime-owner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime-owner.js","sourceRoot":"","sources":["../runtime-owner.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAUjD,MAAM,UAAU,qBA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AQ,EAAE,QAAQ,CAAC,CAAC;gBACvD,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;oBACjC,OAAO,CAAC,GAAG,IAAe,EAAE,EAAE;wBAC5B,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;4BAAE,OAAO,SAAS,CAAC;wBACxC,OAAO,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;oBAC7C,CAAC,CAAC;gBACJ,CAAC;gBACD,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YACrD,CAAC;SACF,CAAC,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IACF,OAAO,IAAI,CAAC,EAAE,CAAuB,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,MAAoB;IAC/D,IAAI,MAAM,EAAE,OAAO;QAAE,OAAO,IAAI,CAAC;IACjC,OAAO,KAAK,YAAY,KAAK,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,IAAI,KAAK,CAAC,OAAO,KAAK,+BAA+B,CAAC,CAAC;AACtH,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Api, Model } from "@earendil-works/pi-ai";
2
+ import type { ExtensionUIContext, ModelRegistry } from "@earendil-works/pi-coding-agent";
3
+ import type { Client } from "@modelcontextprotocol/client";
4
+ import { type CreateMessageRequest, type CreateMessageResult } from "@modelcontextprotocol/client";
5
+ export type SamplingUIContext = Pick<ExtensionUIContext, "confirm">;
6
+ export type SamplingModelRegistry = Pick<ModelRegistry, "getAvailable" | "complete">;
7
+ export interface SamplingHandlerOptions {
8
+ serverName: string;
9
+ autoApprove: boolean;
10
+ ui?: SamplingUIContext;
11
+ modelRegistry: SamplingModelRegistry;
12
+ getCurrentModel: () => Model<Api> | undefined;
13
+ getSignal: () => AbortSignal | undefined;
14
+ }
15
+ export type ServerSamplingConfig = Omit<SamplingHandlerOptions, "serverName">;
16
+ export declare function registerSamplingHandler(client: Client, options: SamplingHandlerOptions): void;
17
+ export declare function handleSamplingRequest(options: SamplingHandlerOptions, request: CreateMessageRequest): Promise<CreateMessageResult>;