@pixelbyte-software/pixcode 1.51.8 → 1.51.9

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 (307) hide show
  1. package/CODE_OF_CONDUCT.md +41 -41
  2. package/CONTRIBUTING.md +155 -155
  3. package/LICENSE +718 -718
  4. package/README.de.md +169 -169
  5. package/README.ja.md +167 -167
  6. package/README.ko.md +167 -167
  7. package/README.md +419 -419
  8. package/README.ru.md +169 -169
  9. package/README.tr.md +298 -298
  10. package/README.zh-CN.md +167 -167
  11. package/SECURITY.md +46 -46
  12. package/dist/api-automation.html +110 -110
  13. package/dist/api-docs.html +548 -548
  14. package/dist/assets/{index--kNEhygZ.js → index-Gs7C4hZT.js} +69 -69
  15. package/dist/clear-cache.html +85 -85
  16. package/dist/convert-icons.md +52 -52
  17. package/dist/docs.html +308 -308
  18. package/dist/favicon.svg +8 -8
  19. package/dist/features.html +133 -133
  20. package/dist/generate-icons.js +48 -48
  21. package/dist/humans.txt +15 -15
  22. package/dist/icons/codex-white.svg +3 -3
  23. package/dist/icons/codex.svg +3 -3
  24. package/dist/icons/cursor-white.svg +11 -11
  25. package/dist/icons/icon-128x128.svg +9 -9
  26. package/dist/icons/icon-144x144.svg +9 -9
  27. package/dist/icons/icon-152x152.svg +9 -9
  28. package/dist/icons/icon-192x192.svg +9 -9
  29. package/dist/icons/icon-384x384.svg +9 -9
  30. package/dist/icons/icon-512x512.svg +9 -9
  31. package/dist/icons/icon-72x72.svg +9 -9
  32. package/dist/icons/icon-96x96.svg +9 -9
  33. package/dist/icons/icon-template.svg +9 -9
  34. package/dist/icons/qwen-logo.svg +14 -14
  35. package/dist/index.html +58 -58
  36. package/dist/landing.html +268 -268
  37. package/dist/llms-full.txt +119 -119
  38. package/dist/llms.txt +53 -53
  39. package/dist/logo.svg +12 -12
  40. package/dist/manifest.json +60 -60
  41. package/dist/openapi.yaml +1696 -1696
  42. package/dist/orchestration.html +125 -125
  43. package/dist/robots.txt +4 -4
  44. package/dist/site.css +692 -692
  45. package/dist/sitemap.xml +51 -51
  46. package/dist/sw.js +132 -132
  47. package/dist-server/server/cli.js +96 -96
  48. package/dist-server/server/daemon/manager.js +33 -33
  49. package/dist-server/server/daemon-manager.js +64 -64
  50. package/dist-server/server/index.js +91 -65
  51. package/dist-server/server/index.js.map +1 -1
  52. package/dist-server/server/routes/commands.js +25 -25
  53. package/dist-server/server/routes/git.js +17 -17
  54. package/dist-server/server/routes/live-view.js +46 -46
  55. package/dist-server/server/services/hermes-gateway.js +5 -5
  56. package/dist-server/server/services/public-api-manifest.js +51 -51
  57. package/package.json +222 -222
  58. package/scripts/fix-node-pty.js +67 -67
  59. package/scripts/github/create-v1.38-issues.mjs +351 -351
  60. package/scripts/github/create-vscode-workbench-issues.mjs +121 -121
  61. package/scripts/hermes/configure-pixcode-mcp.mjs +165 -165
  62. package/scripts/hermes/pixcode-mcp-server.mjs +1009 -1009
  63. package/scripts/smoke/changes-panel-layout.mjs +48 -48
  64. package/scripts/smoke/chat-composer-fixed-layout.mjs +55 -55
  65. package/scripts/smoke/chat-message-timeline-order.mjs +41 -41
  66. package/scripts/smoke/chat-realtime-hydration.mjs +44 -44
  67. package/scripts/smoke/chat-session-provider-pools.mjs +35 -35
  68. package/scripts/smoke/chat-session-state.mjs +19 -19
  69. package/scripts/smoke/code-editor-theme.mjs +55 -55
  70. package/scripts/smoke/code-editor-vscode-engine.mjs +91 -91
  71. package/scripts/smoke/command-center-agent-writes.mjs +79 -79
  72. package/scripts/smoke/command-center-non-git.mjs +46 -46
  73. package/scripts/smoke/context-packet.mjs +43 -43
  74. package/scripts/smoke/control-room-ux-redesign.mjs +91 -91
  75. package/scripts/smoke/daemon-entrypoint.mjs +20 -20
  76. package/scripts/smoke/default-landing-routing.mjs +33 -33
  77. package/scripts/smoke/desktop-native-notifications.mjs +30 -30
  78. package/scripts/smoke/desktop-tray-icon.mjs +33 -33
  79. package/scripts/smoke/discord-release-workflow.mjs +24 -24
  80. package/scripts/smoke/git-install-update.mjs +255 -255
  81. package/scripts/smoke/handoff-artifact-protocol.mjs +50 -50
  82. package/scripts/smoke/hermes-api-install.mjs +56 -56
  83. package/scripts/smoke/hermes-gateway-persistence.mjs +104 -104
  84. package/scripts/smoke/hermes-mcp-pixcode-roundtrip.mjs +426 -426
  85. package/scripts/smoke/hermes-rest-chat-api.mjs +162 -162
  86. package/scripts/smoke/hermes-rest-chat-live.mjs +45 -45
  87. package/scripts/smoke/hermes-rest-codex-launch.mjs +209 -209
  88. package/scripts/smoke/hermes-rest-gateway.mjs +79 -79
  89. package/scripts/smoke/hermes-rest-live.mjs +42 -42
  90. package/scripts/smoke/hermes-roundtrip.mjs +167 -167
  91. package/scripts/smoke/hermes-settings-commands.mjs +349 -349
  92. package/scripts/smoke/hermes-smoke-launcher-guard.mjs +34 -34
  93. package/scripts/smoke/live-view-diagnostics.mjs +53 -53
  94. package/scripts/smoke/live-view-environment.mjs +92 -92
  95. package/scripts/smoke/live-view-integration.mjs +450 -450
  96. package/scripts/smoke/mac-desktop-runtime.mjs +37 -37
  97. package/scripts/smoke/mobile-tunnel-guidance.mjs +29 -29
  98. package/scripts/smoke/model-registry.mjs +36 -36
  99. package/scripts/smoke/multi-project-ui.mjs +45 -45
  100. package/scripts/smoke/multi-worker-slots.mjs +42 -42
  101. package/scripts/smoke/notification-center.mjs +87 -87
  102. package/scripts/smoke/notification-inapp-preference.mjs +23 -23
  103. package/scripts/smoke/notification-taxonomy.mjs +58 -58
  104. package/scripts/smoke/orchestration-api.mjs +172 -172
  105. package/scripts/smoke/orchestration-execution-dashboard.mjs +33 -33
  106. package/scripts/smoke/orchestration-live-run.mjs +176 -176
  107. package/scripts/smoke/orchestration-mobile-scroll.mjs +29 -29
  108. package/scripts/smoke/orchestration-model-sync.mjs +30 -30
  109. package/scripts/smoke/orchestration-permission-fallback.mjs +34 -34
  110. package/scripts/smoke/orchestration-runtime-guards.mjs +48 -48
  111. package/scripts/smoke/orchestration-user-facing-output.mjs +25 -25
  112. package/scripts/smoke/permission-policy.mjs +50 -50
  113. package/scripts/smoke/pixcode-workbench-1-48.mjs +167 -167
  114. package/scripts/smoke/provider-models-opencode-live.mjs +66 -66
  115. package/scripts/smoke/provider-rest-api.mjs +124 -124
  116. package/scripts/smoke/provider-selection-status.mjs +52 -52
  117. package/scripts/smoke/run-state-refresh.mjs +52 -52
  118. package/scripts/smoke/runtime-manager.mjs +99 -99
  119. package/scripts/smoke/shell-manual-disconnect.mjs +30 -30
  120. package/scripts/smoke/side-panel-editor-layout.mjs +34 -34
  121. package/scripts/smoke/static-root-routing.mjs +21 -21
  122. package/scripts/smoke/strict-handoff-compact.mjs +60 -60
  123. package/scripts/smoke/taskmaster-config.mjs +24 -24
  124. package/scripts/smoke/taskmaster-execution-telegram.mjs +3 -3
  125. package/scripts/smoke/taskmaster-onboarding.mjs +3 -3
  126. package/scripts/smoke/taskmaster-run-graph.mjs +3 -3
  127. package/scripts/smoke/telegram-control.mjs +242 -242
  128. package/scripts/smoke/tunnel-persistence.mjs +56 -56
  129. package/scripts/smoke/update-issue-progress.mjs +69 -69
  130. package/scripts/smoke/update-ux.mjs +55 -55
  131. package/scripts/smoke/v138-completion.mjs +132 -132
  132. package/scripts/smoke/v138-desktop-release-hardening.mjs +69 -69
  133. package/scripts/smoke/v138-diagnostics.mjs +63 -63
  134. package/scripts/smoke/v138-issue-planner.mjs +33 -33
  135. package/scripts/smoke/v143-remote-control.mjs +76 -76
  136. package/scripts/smoke/v144-production-loop.mjs +47 -47
  137. package/scripts/smoke/v145-platformization.mjs +46 -46
  138. package/scripts/smoke/v146-control-room-ui.mjs +150 -150
  139. package/scripts/smoke/version-modal-autoshow.mjs +29 -29
  140. package/scripts/smoke/vscode-workbench-layout.mjs +63 -63
  141. package/scripts/smoke/vscode-workbench-polish.mjs +461 -461
  142. package/scripts/smoke/workflow-fallback-replay.mjs +56 -56
  143. package/scripts/smoke/workflow-templates.mjs +43 -43
  144. package/scripts/smoke/workflow-trace-timeline.mjs +46 -46
  145. package/scripts/update-git-install.mjs +283 -283
  146. package/server/claude-sdk.js +920 -920
  147. package/server/cli.js +1035 -1035
  148. package/server/constants/config.js +4 -4
  149. package/server/cursor-cli.js +344 -344
  150. package/server/daemon/manager.js +563 -563
  151. package/server/daemon-manager.js +964 -964
  152. package/server/database/db.js +895 -895
  153. package/server/database/json-store.js +197 -197
  154. package/server/gemini-cli.js +550 -550
  155. package/server/gemini-response-handler.js +79 -79
  156. package/server/index.js +111 -81
  157. package/server/load-env.js +35 -35
  158. package/server/middleware/auth.js +155 -155
  159. package/server/modules/orchestration/a2a/adapter-registry.ts +108 -108
  160. package/server/modules/orchestration/a2a/adapters/abstract-a2a.adapter.ts +63 -63
  161. package/server/modules/orchestration/a2a/adapters/claude-code.adapter.ts +286 -286
  162. package/server/modules/orchestration/a2a/adapters/codex.adapter.ts +244 -244
  163. package/server/modules/orchestration/a2a/adapters/cursor.adapter.ts +249 -249
  164. package/server/modules/orchestration/a2a/adapters/gemini.adapter.ts +248 -248
  165. package/server/modules/orchestration/a2a/adapters/json-event.adapter.ts +100 -100
  166. package/server/modules/orchestration/a2a/adapters/opencode.adapter.ts +248 -248
  167. package/server/modules/orchestration/a2a/adapters/qwen.adapter.ts +248 -248
  168. package/server/modules/orchestration/a2a/agent-card.ts +55 -55
  169. package/server/modules/orchestration/a2a/routes.ts +590 -590
  170. package/server/modules/orchestration/a2a/task-store.ts +178 -178
  171. package/server/modules/orchestration/a2a/types.ts +126 -126
  172. package/server/modules/orchestration/a2a/validator.ts +113 -113
  173. package/server/modules/orchestration/hermes/hermes.routes.ts +642 -642
  174. package/server/modules/orchestration/index.ts +101 -101
  175. package/server/modules/orchestration/preview/port-watcher.ts +112 -112
  176. package/server/modules/orchestration/preview/preview-proxy.ts +60 -60
  177. package/server/modules/orchestration/preview/types.ts +19 -19
  178. package/server/modules/orchestration/security/permission-policy.ts +401 -401
  179. package/server/modules/orchestration/tasks/orchestration-task-store.ts +41 -41
  180. package/server/modules/orchestration/tasks/orchestration-task.routes.ts +64 -64
  181. package/server/modules/orchestration/tasks/orchestration-task.service.ts +209 -209
  182. package/server/modules/orchestration/tasks/orchestration-task.types.ts +40 -40
  183. package/server/modules/orchestration/tasks/task-run-graph.ts +155 -155
  184. package/server/modules/orchestration/workflows/approval-queue.ts +106 -106
  185. package/server/modules/orchestration/workflows/built-in-workflows.ts +127 -127
  186. package/server/modules/orchestration/workflows/context-packet.ts +186 -186
  187. package/server/modules/orchestration/workflows/handoff-artifact.ts +175 -175
  188. package/server/modules/orchestration/workflows/workflow-fallback-policy.ts +161 -161
  189. package/server/modules/orchestration/workflows/workflow-replay.ts +254 -254
  190. package/server/modules/orchestration/workflows/workflow-runner.ts +2070 -2070
  191. package/server/modules/orchestration/workflows/workflow-store.ts +97 -97
  192. package/server/modules/orchestration/workflows/workflow-templates.ts +272 -272
  193. package/server/modules/orchestration/workflows/workflow-trace.ts +424 -424
  194. package/server/modules/orchestration/workflows/workflow.routes.ts +586 -586
  195. package/server/modules/orchestration/workflows/workflow.types.ts +111 -111
  196. package/server/modules/orchestration/workflows/workspace-target.ts +122 -122
  197. package/server/modules/orchestration/workspace/docker-workspace.ts +136 -136
  198. package/server/modules/orchestration/workspace/path-safety.ts +55 -55
  199. package/server/modules/orchestration/workspace/types.ts +52 -52
  200. package/server/modules/orchestration/workspace/workspace-manager.ts +102 -102
  201. package/server/modules/orchestration/workspace/worktree-workspace.ts +126 -126
  202. package/server/modules/providers/index.ts +2 -2
  203. package/server/modules/providers/list/claude/claude-auth.provider.ts +146 -146
  204. package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
  205. package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
  206. package/server/modules/providers/list/claude/claude.provider.ts +15 -15
  207. package/server/modules/providers/list/codex/codex-auth.provider.ts +117 -117
  208. package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
  209. package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
  210. package/server/modules/providers/list/codex/codex.provider.ts +15 -15
  211. package/server/modules/providers/list/cursor/cursor-auth.provider.ts +147 -147
  212. package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
  213. package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
  214. package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
  215. package/server/modules/providers/list/gemini/gemini-auth.provider.ts +173 -173
  216. package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
  217. package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
  218. package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
  219. package/server/modules/providers/list/opencode/opencode-auth.provider.ts +131 -131
  220. package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
  221. package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +286 -286
  222. package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
  223. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +146 -146
  224. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
  225. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +265 -265
  226. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
  227. package/server/modules/providers/provider.registry.ts +40 -40
  228. package/server/modules/providers/provider.routes.ts +891 -891
  229. package/server/modules/providers/services/mcp.service.ts +86 -86
  230. package/server/modules/providers/services/provider-auth.service.ts +26 -26
  231. package/server/modules/providers/services/sessions.service.ts +45 -45
  232. package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
  233. package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
  234. package/server/modules/providers/shared/provider-configs.ts +142 -142
  235. package/server/modules/providers/tests/mcp.test.ts +293 -293
  236. package/server/openai-codex.js +462 -462
  237. package/server/opencode-cli.js +491 -491
  238. package/server/opencode-response-handler.js +111 -111
  239. package/server/projects.js +3008 -3008
  240. package/server/qwen-code-cli.js +410 -410
  241. package/server/qwen-response-handler.js +73 -73
  242. package/server/routes/agent.js +1435 -1435
  243. package/server/routes/auth.js +142 -142
  244. package/server/routes/codex.js +20 -20
  245. package/server/routes/commands.js +538 -538
  246. package/server/routes/cursor.js +61 -61
  247. package/server/routes/diagnostics.js +41 -41
  248. package/server/routes/gemini.js +25 -25
  249. package/server/routes/git.js +1641 -1641
  250. package/server/routes/live-view.js +387 -387
  251. package/server/routes/mcp-utils.js +13 -13
  252. package/server/routes/messages.js +50 -50
  253. package/server/routes/network.js +117 -117
  254. package/server/routes/platformization.js +182 -182
  255. package/server/routes/plugins.js +320 -320
  256. package/server/routes/production-agent-loop.js +90 -90
  257. package/server/routes/projects.js +911 -911
  258. package/server/routes/public-api.js +34 -34
  259. package/server/routes/qwen.js +27 -27
  260. package/server/routes/remote.js +50 -50
  261. package/server/routes/settings.js +321 -321
  262. package/server/routes/telegram.js +140 -140
  263. package/server/routes/user.js +125 -125
  264. package/server/routes/webhooks.js +63 -63
  265. package/server/services/control-room.js +102 -102
  266. package/server/services/diagnostics.js +165 -165
  267. package/server/services/external-access.js +375 -375
  268. package/server/services/hermes-gateway.js +1562 -1562
  269. package/server/services/hermes-install-jobs.js +729 -729
  270. package/server/services/install-jobs.js +715 -715
  271. package/server/services/live-view.js +956 -956
  272. package/server/services/managed-runtimes.js +493 -493
  273. package/server/services/model-registry.js +144 -144
  274. package/server/services/notification-orchestrator.js +365 -365
  275. package/server/services/notification-taxonomy.js +204 -204
  276. package/server/services/platformization.js +773 -773
  277. package/server/services/production-agent-loop.js +248 -248
  278. package/server/services/provider-cli-versions.js +149 -149
  279. package/server/services/provider-credentials.js +189 -189
  280. package/server/services/provider-models.js +396 -396
  281. package/server/services/public-api-manifest.js +190 -190
  282. package/server/services/remote-connection.js +127 -127
  283. package/server/services/runtime-manager.js +323 -323
  284. package/server/services/startup-update.js +234 -234
  285. package/server/services/telegram/bot.js +331 -331
  286. package/server/services/telegram/control-center.js +979 -979
  287. package/server/services/telegram/telegram-http-client.js +151 -151
  288. package/server/services/telegram/translations.js +340 -340
  289. package/server/services/vapid-keys.js +36 -36
  290. package/server/services/webhooks.js +216 -216
  291. package/server/sessionManager.js +225 -225
  292. package/server/shared/interfaces.ts +54 -54
  293. package/server/shared/types.ts +172 -172
  294. package/server/shared/utils.ts +193 -193
  295. package/server/tsconfig.json +36 -36
  296. package/server/utils/colors.js +21 -21
  297. package/server/utils/commandParser.js +305 -305
  298. package/server/utils/frontmatter.js +18 -18
  299. package/server/utils/gitConfig.js +34 -34
  300. package/server/utils/plugin-loader.js +457 -457
  301. package/server/utils/plugin-process-manager.js +185 -185
  302. package/server/utils/port-access.js +209 -209
  303. package/server/utils/runtime-paths.js +37 -37
  304. package/server/utils/url-detection.js +71 -71
  305. package/server/vite-daemon.js +79 -79
  306. package/shared/modelConstants.js +161 -161
  307. package/shared/networkHosts.js +22 -22
@@ -1,920 +1,920 @@
1
- /**
2
- * Claude SDK Integration
3
- *
4
- * This module provides SDK-based integration with Claude using the @anthropic-ai/claude-agent-sdk.
5
- * It mirrors the interface of claude-cli.js but uses the SDK internally for better performance
6
- * and maintainability.
7
- *
8
- * Key features:
9
- * - Direct SDK integration without child processes
10
- * - Session management with abort capability
11
- * - Options mapping between CLI and SDK formats
12
- * - WebSocket message streaming
13
- */
14
-
15
- import crypto from 'crypto';
16
- import { existsSync, readFileSync, promises as fs } from 'fs';
17
- import path from 'path';
18
- import os from 'os';
19
-
20
- import { query } from '@anthropic-ai/claude-agent-sdk';
21
-
22
- import {
23
- createNotificationEvent,
24
- notifyRunFailed,
25
- notifyRunStopped,
26
- notifyUserIfEnabled
27
- } from './services/notification-orchestrator.js';
28
- import { sessionsService } from './modules/providers/services/sessions.service.js';
29
- import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
30
- import { resolveClaudeExecutable, resolveGitBashPath } from './services/install-jobs.js';
31
- import { createNormalizedMessage } from './shared/utils.js';
32
- import { evaluatePermissionRequest } from './modules/orchestration/security/permission-policy.js';
33
-
34
- const activeSessions = new Map();
35
- const pendingToolApprovals = new Map();
36
-
37
- const TOOL_APPROVAL_TIMEOUT_MS = parseInt(process.env.CLAUDE_TOOL_APPROVAL_TIMEOUT_MS, 10) || 55000;
38
-
39
- const TOOLS_REQUIRING_INTERACTION = new Set(['AskUserQuestion', 'ExitPlanMode']);
40
-
41
- function createRequestId() {
42
- if (typeof crypto.randomUUID === 'function') {
43
- return crypto.randomUUID();
44
- }
45
- return crypto.randomBytes(16).toString('hex');
46
- }
47
-
48
- function waitForToolApproval(requestId, options = {}) {
49
- const { timeoutMs = TOOL_APPROVAL_TIMEOUT_MS, signal, onCancel, metadata } = options;
50
-
51
- return new Promise(resolve => {
52
- let settled = false;
53
-
54
- const finalize = (decision) => {
55
- if (settled) return;
56
- settled = true;
57
- cleanup();
58
- resolve(decision);
59
- };
60
-
61
- let timeout;
62
-
63
- const cleanup = () => {
64
- pendingToolApprovals.delete(requestId);
65
- if (timeout) clearTimeout(timeout);
66
- if (signal && abortHandler) {
67
- signal.removeEventListener('abort', abortHandler);
68
- }
69
- };
70
-
71
- // timeoutMs 0 = wait indefinitely (interactive tools)
72
- if (timeoutMs > 0) {
73
- timeout = setTimeout(() => {
74
- onCancel?.('timeout');
75
- finalize(null);
76
- }, timeoutMs);
77
- }
78
-
79
- const abortHandler = () => {
80
- onCancel?.('cancelled');
81
- finalize({ cancelled: true });
82
- };
83
-
84
- if (signal) {
85
- if (signal.aborted) {
86
- onCancel?.('cancelled');
87
- finalize({ cancelled: true });
88
- return;
89
- }
90
- signal.addEventListener('abort', abortHandler, { once: true });
91
- }
92
-
93
- const resolver = (decision) => {
94
- finalize(decision);
95
- };
96
- // Attach metadata for getPendingApprovalsForSession lookup
97
- if (metadata) {
98
- Object.assign(resolver, metadata);
99
- }
100
- pendingToolApprovals.set(requestId, resolver);
101
- });
102
- }
103
-
104
- function resolveToolApproval(requestId, decision) {
105
- const resolver = pendingToolApprovals.get(requestId);
106
- if (resolver) {
107
- resolver(decision);
108
- }
109
- }
110
-
111
- // Match stored permission entries against a tool + input combo.
112
- // This only supports exact tool names and the Bash(command:*) shorthand
113
- // used by the UI; it intentionally does not implement full glob semantics,
114
- // introduced to stay consistent with the UI's "Allow rule" format.
115
- function matchesToolPermission(entry, toolName, input) {
116
- if (!entry || !toolName) {
117
- return false;
118
- }
119
-
120
- if (entry === toolName) {
121
- return true;
122
- }
123
-
124
- const bashMatch = entry.match(/^Bash\((.+):\*\)$/);
125
- if (toolName === 'Bash' && bashMatch) {
126
- const allowedPrefix = bashMatch[1];
127
- let command = '';
128
-
129
- if (typeof input === 'string') {
130
- command = input.trim();
131
- } else if (input && typeof input === 'object' && typeof input.command === 'string') {
132
- command = input.command.trim();
133
- }
134
-
135
- if (!command) {
136
- return false;
137
- }
138
-
139
- return command.startsWith(allowedPrefix);
140
- }
141
-
142
- return false;
143
- }
144
-
145
- function readClaudeSettingsEnv(filePath) {
146
- try {
147
- if (!existsSync(filePath)) return {};
148
- const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
149
- const env = parsed?.env;
150
- if (!env || typeof env !== 'object') return {};
151
-
152
- return Object.fromEntries(
153
- Object.entries(env)
154
- .filter(([key, value]) =>
155
- typeof value === 'string' &&
156
- value.trim() &&
157
- !key.startsWith('//'),
158
- ),
159
- );
160
- } catch {
161
- return {};
162
- }
163
- }
164
-
165
- function loadClaudeSettingsEnv(cwd) {
166
- const files = [
167
- path.join(os.homedir(), '.claude', 'settings.json'),
168
- ];
169
- if (cwd) {
170
- files.push(
171
- path.join(cwd, '.claude', 'settings.json'),
172
- path.join(cwd, '.claude', 'settings.local.json'),
173
- );
174
- }
175
-
176
- return files.reduce((env, filePath) => ({
177
- ...env,
178
- ...readClaudeSettingsEnv(filePath),
179
- }), {});
180
- }
181
-
182
- /**
183
- * Maps CLI options to SDK-compatible options format
184
- * @param {Object} options - CLI options
185
- * @returns {Object} SDK-compatible options
186
- */
187
- function mapCliOptionsToSDK(options = {}) {
188
- const { sessionId, cwd, toolsSettings, permissionMode, permissionPolicy, permissionPolicyContext } = options;
189
-
190
- const sdkOptions = {};
191
-
192
- // Forward all host env vars (e.g. ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN) to the subprocess.
193
- // Since claude-agent-sdk 0.2.113+, options.env REPLACES process.env in the subprocess
194
- // instead of overlaying it. Without spreading process.env here, users who rely on
195
- // ANTHROPIC_BASE_URL, HTTP(S)_PROXY, etc. would silently lose those settings.
196
- sdkOptions.env = { ...process.env, ...loadClaudeSettingsEnv(cwd) };
197
-
198
- // Claude Code on Windows hard-requires a POSIX bash (typically from Git
199
- // for Windows) and reads its path from CLAUDE_CODE_GIT_BASH_PATH. If the
200
- // user has git-bash installed but hasn't exported that var, the CLI
201
- // exits with code 1 + a guidance message and the pixcode UI just sees
202
- // an opaque "Claude Code process exited with code 1" error. Auto-probing
203
- // a handful of known install locations lets the CLI boot transparently.
204
- if (!sdkOptions.env.CLAUDE_CODE_GIT_BASH_PATH) {
205
- const bashPath = resolveGitBashPath();
206
- if (bashPath) sdkOptions.env.CLAUDE_CODE_GIT_BASH_PATH = bashPath;
207
- }
208
-
209
- // Resolve the Claude Code CLI path cross-platform. The SDK uses plain
210
- // `child_process.spawn(command, args)` with no shell — and its own
211
- // `nb()` helper treats anything not ending in .js/.mjs/.ts as a native
212
- // executable. That means:
213
- // - Unix: bare `"claude"` works (kernel PATH + shebang handle it).
214
- // - Windows: bare `"claude"` fails ("native binary not found"). A
215
- // `.cmd` shim fails with EINVAL (post-CVE-2024 Node refuses .cmd
216
- // without shell:true). We need the underlying `.exe` instead.
217
- // `resolveClaudeExecutable()` does a `where`/`which` lookup and, on
218
- // Windows, peeks inside npm .cmd shims to recover the real .exe target.
219
- // If nothing is found we leave the option unset so the SDK falls through
220
- // to its own bundled-native-binary resolver.
221
- const resolvedClaudePath = process.env.CLAUDE_CLI_PATH || resolveClaudeExecutable();
222
- if (resolvedClaudePath) {
223
- sdkOptions.pathToClaudeCodeExecutable = resolvedClaudePath;
224
- }
225
-
226
- // Map working directory
227
- if (cwd) {
228
- sdkOptions.cwd = cwd;
229
- }
230
-
231
- // Map permission mode
232
- if (permissionMode && permissionMode !== 'default') {
233
- sdkOptions.permissionMode = permissionMode;
234
- }
235
- sdkOptions.permissionPolicy = permissionPolicy;
236
- sdkOptions.permissionPolicyContext = permissionPolicyContext;
237
-
238
- // Map tool settings
239
- const settings = toolsSettings || {
240
- allowedTools: [],
241
- disallowedTools: [],
242
- skipPermissions: false
243
- };
244
-
245
- // Handle tool permissions
246
- if (settings.skipPermissions && permissionMode !== 'plan') {
247
- // When skipping permissions, use bypassPermissions mode
248
- sdkOptions.permissionMode = 'bypassPermissions';
249
- }
250
-
251
- let allowedTools = [...(settings.allowedTools || [])];
252
-
253
- // Add plan mode default tools
254
- if (permissionMode === 'plan') {
255
- const planModeTools = ['Read', 'Task', 'exit_plan_mode', 'TodoRead', 'TodoWrite', 'WebFetch', 'WebSearch'];
256
- for (const tool of planModeTools) {
257
- if (!allowedTools.includes(tool)) {
258
- allowedTools.push(tool);
259
- }
260
- }
261
- }
262
-
263
- sdkOptions.allowedTools = allowedTools;
264
-
265
- // Use the tools preset to make all default built-in tools available (including AskUserQuestion).
266
- // This was introduced in SDK 0.1.57. Omitting this preserves existing behavior (all tools available),
267
- // but being explicit ensures forward compatibility and clarity.
268
- sdkOptions.tools = { type: 'preset', preset: 'claude_code' };
269
-
270
- sdkOptions.disallowedTools = settings.disallowedTools || [];
271
-
272
- // Map model only when Pixcode explicitly passes one. If omitted, let
273
- // Claude Code load the user's own project/user/local settings, including
274
- // ~/.claude/settings.json "model".
275
- if (typeof options.model === 'string' && options.model.trim()) {
276
- sdkOptions.model = options.model.trim();
277
- }
278
- // Model logged at query start below
279
-
280
- // Map system prompt configuration
281
- sdkOptions.systemPrompt = {
282
- type: 'preset',
283
- preset: 'claude_code' // Required to use CLAUDE.md
284
- };
285
-
286
- // Map setting sources for CLAUDE.md loading
287
- // This loads CLAUDE.md from project, user (~/.config/claude/CLAUDE.md), and local directories
288
- sdkOptions.settingSources = ['project', 'user', 'local'];
289
-
290
- // Map resume session
291
- if (sessionId) {
292
- sdkOptions.resume = sessionId;
293
- }
294
-
295
- return sdkOptions;
296
- }
297
-
298
- /**
299
- * Adds a session to the active sessions map
300
- * @param {string} sessionId - Session identifier
301
- * @param {Object} queryInstance - SDK query instance
302
- * @param {Array<string>} tempImagePaths - Temp image file paths for cleanup
303
- * @param {string} tempDir - Temp directory for cleanup
304
- */
305
- function addSession(sessionId, queryInstance, tempImagePaths = [], tempDir = null, writer = null) {
306
- activeSessions.set(sessionId, {
307
- instance: queryInstance,
308
- startTime: Date.now(),
309
- status: 'active',
310
- tempImagePaths,
311
- tempDir,
312
- writer
313
- });
314
- }
315
-
316
- /**
317
- * Removes a session from the active sessions map
318
- * @param {string} sessionId - Session identifier
319
- */
320
- function removeSession(sessionId) {
321
- activeSessions.delete(sessionId);
322
- }
323
-
324
- /**
325
- * Gets a session from the active sessions map
326
- * @param {string} sessionId - Session identifier
327
- * @returns {Object|undefined} Session data or undefined
328
- */
329
- function getSession(sessionId) {
330
- return activeSessions.get(sessionId);
331
- }
332
-
333
- /**
334
- * Gets all active session IDs
335
- * @returns {Array<string>} Array of active session IDs
336
- */
337
- function getAllSessions() {
338
- return Array.from(activeSessions.keys());
339
- }
340
-
341
- /**
342
- * Transforms SDK messages to WebSocket format expected by frontend
343
- * @param {Object} sdkMessage - SDK message object
344
- * @returns {Object} Transformed message ready for WebSocket
345
- */
346
- function transformMessage(sdkMessage) {
347
- // Extract parent_tool_use_id for subagent tool grouping
348
- if (sdkMessage.parent_tool_use_id) {
349
- return {
350
- ...sdkMessage,
351
- parentToolUseId: sdkMessage.parent_tool_use_id
352
- };
353
- }
354
- return sdkMessage;
355
- }
356
-
357
- /**
358
- * Extracts token usage from SDK result messages
359
- * @param {Object} resultMessage - SDK result message
360
- * @returns {Object|null} Token budget object or null
361
- */
362
- function extractTokenBudget(resultMessage) {
363
- if (resultMessage.type !== 'result' || !resultMessage.modelUsage) {
364
- return null;
365
- }
366
-
367
- // Get the first model's usage data
368
- const modelKey = Object.keys(resultMessage.modelUsage)[0];
369
- const modelData = resultMessage.modelUsage[modelKey];
370
-
371
- if (!modelData) {
372
- return null;
373
- }
374
-
375
- // Use cumulative tokens if available (tracks total for the session)
376
- // Otherwise fall back to per-request tokens
377
- const inputTokens = modelData.cumulativeInputTokens || modelData.inputTokens || 0;
378
- const outputTokens = modelData.cumulativeOutputTokens || modelData.outputTokens || 0;
379
- const cacheReadTokens = modelData.cumulativeCacheReadInputTokens || modelData.cacheReadInputTokens || 0;
380
- const cacheCreationTokens = modelData.cumulativeCacheCreationInputTokens || modelData.cacheCreationInputTokens || 0;
381
-
382
- // Total used = input + output + cache tokens
383
- const totalUsed = inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens;
384
-
385
- // Use configured context window budget from environment (default 160000)
386
- // This is the user's budget limit, not the model's context window
387
- const contextWindow = parseInt(process.env.CONTEXT_WINDOW) || 160000;
388
-
389
- // Token calc logged via token-budget WS event
390
-
391
- return {
392
- used: totalUsed,
393
- total: contextWindow
394
- };
395
- }
396
-
397
- /**
398
- * Handles image processing for SDK queries
399
- * Saves base64 images to temporary files and returns modified prompt with file paths
400
- * @param {string} command - Original user prompt
401
- * @param {Array} images - Array of image objects with base64 data
402
- * @param {string} cwd - Working directory for temp file creation
403
- * @returns {Promise<Object>} {modifiedCommand, tempImagePaths, tempDir}
404
- */
405
- async function handleImages(command, images, cwd) {
406
- const tempImagePaths = [];
407
- let tempDir = null;
408
-
409
- if (!images || images.length === 0) {
410
- return { modifiedCommand: command, tempImagePaths, tempDir };
411
- }
412
-
413
- try {
414
- // Create temp directory in the project directory
415
- const workingDir = cwd || process.cwd();
416
- tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
417
- await fs.mkdir(tempDir, { recursive: true });
418
-
419
- // Save each image to a temp file
420
- for (const [index, image] of images.entries()) {
421
- // Extract base64 data and mime type
422
- const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
423
- if (!matches) {
424
- console.error('Invalid image data format');
425
- continue;
426
- }
427
-
428
- const [, mimeType, base64Data] = matches;
429
- const extension = mimeType.split('/')[1] || 'png';
430
- const filename = `image_${index}.${extension}`;
431
- const filepath = path.join(tempDir, filename);
432
-
433
- // Write base64 data to file
434
- await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
435
- tempImagePaths.push(filepath);
436
- }
437
-
438
- // Include the full image paths in the prompt
439
- let modifiedCommand = command;
440
- if (tempImagePaths.length > 0 && command && command.trim()) {
441
- const imageNote = `\n\n[Images provided at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
442
- modifiedCommand = command + imageNote;
443
- }
444
-
445
- // Images processed
446
- return { modifiedCommand, tempImagePaths, tempDir };
447
- } catch (error) {
448
- console.error('Error processing images for SDK:', error);
449
- return { modifiedCommand: command, tempImagePaths, tempDir };
450
- }
451
- }
452
-
453
- /**
454
- * Cleans up temporary image files
455
- * @param {Array<string>} tempImagePaths - Array of temp file paths to delete
456
- * @param {string} tempDir - Temp directory to remove
457
- */
458
- async function cleanupTempFiles(tempImagePaths, tempDir) {
459
- if (!tempImagePaths || tempImagePaths.length === 0) {
460
- return;
461
- }
462
-
463
- try {
464
- // Delete individual temp files
465
- for (const imagePath of tempImagePaths) {
466
- await fs.unlink(imagePath).catch(err =>
467
- console.error(`Failed to delete temp image ${imagePath}:`, err)
468
- );
469
- }
470
-
471
- // Delete temp directory
472
- if (tempDir) {
473
- await fs.rm(tempDir, { recursive: true, force: true }).catch(err =>
474
- console.error(`Failed to delete temp directory ${tempDir}:`, err)
475
- );
476
- }
477
-
478
- // Temp files cleaned
479
- } catch (error) {
480
- console.error('Error during temp file cleanup:', error);
481
- }
482
- }
483
-
484
- /**
485
- * Loads MCP server configurations from ~/.claude.json
486
- * @param {string} cwd - Current working directory for project-specific configs
487
- * @returns {Object|null} MCP servers object or null if none found
488
- */
489
- async function loadMcpConfig(cwd) {
490
- try {
491
- const claudeConfigPath = path.join(os.homedir(), '.claude.json');
492
-
493
- // Check if config file exists
494
- try {
495
- await fs.access(claudeConfigPath);
496
- } catch (error) {
497
- // File doesn't exist, return null
498
- // No config file
499
- return null;
500
- }
501
-
502
- // Read and parse config file
503
- let claudeConfig;
504
- try {
505
- const configContent = await fs.readFile(claudeConfigPath, 'utf8');
506
- claudeConfig = JSON.parse(configContent);
507
- } catch (error) {
508
- console.error('Failed to parse ~/.claude.json:', error.message);
509
- return null;
510
- }
511
-
512
- // Extract MCP servers (merge global and project-specific)
513
- let mcpServers = {};
514
-
515
- // Add global MCP servers
516
- if (claudeConfig.mcpServers && typeof claudeConfig.mcpServers === 'object') {
517
- mcpServers = { ...claudeConfig.mcpServers };
518
- // Global MCP servers loaded
519
- }
520
-
521
- // Add/override with project-specific MCP servers
522
- if (claudeConfig.claudeProjects && cwd) {
523
- const projectConfig = claudeConfig.claudeProjects[cwd];
524
- if (projectConfig && projectConfig.mcpServers && typeof projectConfig.mcpServers === 'object') {
525
- mcpServers = { ...mcpServers, ...projectConfig.mcpServers };
526
- // Project MCP servers merged
527
- }
528
- }
529
-
530
- // Return null if no servers found
531
- if (Object.keys(mcpServers).length === 0) {
532
- return null;
533
- }
534
- return mcpServers;
535
- } catch (error) {
536
- console.error('Error loading MCP config:', error.message);
537
- return null;
538
- }
539
- }
540
-
541
- /**
542
- * Executes a Claude query using the SDK
543
- * @param {string} command - User prompt/command
544
- * @param {Object} options - Query options
545
- * @param {Object} ws - WebSocket connection
546
- * @returns {Promise<void>}
547
- */
548
- async function queryClaudeSDK(command, options = {}, ws) {
549
- const { sessionId, sessionSummary } = options;
550
- let capturedSessionId = sessionId;
551
- let sessionCreatedSent = false;
552
- let tempImagePaths = [];
553
- let tempDir = null;
554
-
555
- const emitNotification = (event) => {
556
- notifyUserIfEnabled({
557
- userId: ws?.userId || null,
558
- writer: ws,
559
- event
560
- });
561
- };
562
-
563
- try {
564
- // Map CLI options to SDK format
565
- const sdkOptions = mapCliOptionsToSDK(options);
566
-
567
- // Load MCP configuration
568
- const mcpServers = await loadMcpConfig(options.cwd);
569
- if (mcpServers) {
570
- sdkOptions.mcpServers = mcpServers;
571
- }
572
-
573
- // Handle images - save to temp files and modify prompt
574
- const imageResult = await handleImages(command, options.images, options.cwd);
575
- const finalCommand = imageResult.modifiedCommand;
576
- tempImagePaths = imageResult.tempImagePaths;
577
- tempDir = imageResult.tempDir;
578
-
579
- sdkOptions.hooks = {
580
- Notification: [{
581
- matcher: '',
582
- hooks: [async (input) => {
583
- const message = typeof input?.message === 'string' ? input.message : 'Claude requires your attention.';
584
- emitNotification(createNotificationEvent({
585
- provider: 'claude',
586
- sessionId: capturedSessionId || sessionId || null,
587
- kind: 'action_required',
588
- code: 'agent.notification',
589
- meta: { message, sessionName: sessionSummary },
590
- severity: 'warning',
591
- requiresUserAction: true,
592
- dedupeKey: `claude:hook:notification:${capturedSessionId || sessionId || 'none'}:${message}`
593
- }));
594
- return {};
595
- }]
596
- }]
597
- };
598
-
599
- sdkOptions.canUseTool = async (toolName, input, context) => {
600
- const requiresInteraction = TOOLS_REQUIRING_INTERACTION.has(toolName);
601
- const policyDecision = evaluatePermissionRequest({
602
- policy: sdkOptions.permissionPolicy,
603
- request: {
604
- source: 'provider_tool',
605
- toolName,
606
- input,
607
- command: input && typeof input === 'object' && typeof input.command === 'string'
608
- ? input.command
609
- : undefined,
610
- cwd: options.cwd,
611
- workspacePath: options.cwd,
612
- summary: `${toolName} tool request`,
613
- },
614
- context: sdkOptions.permissionPolicyContext,
615
- });
616
-
617
- if (policyDecision.behavior === 'deny') {
618
- return { behavior: 'deny', message: policyDecision.message };
619
- }
620
-
621
- if (!requiresInteraction && policyDecision.behavior !== 'prompt') {
622
- if (sdkOptions.permissionMode === 'bypassPermissions') {
623
- return { behavior: 'allow', updatedInput: input };
624
- }
625
-
626
- const isDisallowed = (sdkOptions.disallowedTools || []).some(entry =>
627
- matchesToolPermission(entry, toolName, input)
628
- );
629
- if (isDisallowed) {
630
- return { behavior: 'deny', message: 'Tool disallowed by settings' };
631
- }
632
-
633
- const isAllowed = (sdkOptions.allowedTools || []).some(entry =>
634
- matchesToolPermission(entry, toolName, input)
635
- );
636
- if (isAllowed) {
637
- return { behavior: 'allow', updatedInput: input };
638
- }
639
- }
640
-
641
- const requestId = createRequestId();
642
- ws.send(createNormalizedMessage({ kind: 'permission_request', requestId, toolName, input, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
643
- emitNotification(createNotificationEvent({
644
- provider: 'claude',
645
- sessionId: capturedSessionId || sessionId || null,
646
- kind: 'action_required',
647
- code: 'permission.required',
648
- meta: { toolName, sessionName: sessionSummary },
649
- severity: 'warning',
650
- requiresUserAction: true,
651
- dedupeKey: `claude:permission:${capturedSessionId || sessionId || 'none'}:${requestId}`
652
- }));
653
-
654
- const decision = await waitForToolApproval(requestId, {
655
- timeoutMs: requiresInteraction ? 0 : undefined,
656
- signal: context?.signal,
657
- metadata: {
658
- _sessionId: capturedSessionId || sessionId || null,
659
- _toolName: toolName,
660
- _input: input,
661
- _receivedAt: new Date(),
662
- },
663
- onCancel: (reason) => {
664
- ws.send(createNormalizedMessage({ kind: 'permission_cancelled', requestId, reason, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
665
- }
666
- });
667
- if (!decision) {
668
- return { behavior: 'deny', message: 'Permission request timed out' };
669
- }
670
-
671
- if (decision.cancelled) {
672
- return { behavior: 'deny', message: 'Permission request cancelled' };
673
- }
674
-
675
- if (decision.allow) {
676
- if (decision.rememberEntry && typeof decision.rememberEntry === 'string') {
677
- if (!sdkOptions.allowedTools.includes(decision.rememberEntry)) {
678
- sdkOptions.allowedTools.push(decision.rememberEntry);
679
- }
680
- if (Array.isArray(sdkOptions.disallowedTools)) {
681
- sdkOptions.disallowedTools = sdkOptions.disallowedTools.filter(entry => entry !== decision.rememberEntry);
682
- }
683
- }
684
- return { behavior: 'allow', updatedInput: decision.updatedInput ?? input };
685
- }
686
-
687
- return { behavior: 'deny', message: decision.message ?? 'User denied tool use' };
688
- };
689
-
690
- // Set stream-close timeout for interactive tools (Query constructor reads it synchronously). Claude Agent SDK has a default of 5s and this overrides it
691
- const prevStreamTimeout = process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
692
- process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = '300000';
693
-
694
- let queryInstance;
695
- try {
696
- queryInstance = query({
697
- prompt: finalCommand,
698
- options: sdkOptions
699
- });
700
- } catch (hookError) {
701
- // Older/newer SDK versions may not accept hook shapes yet.
702
- // Keep notification behavior operational via runtime events even if hook registration fails.
703
- console.warn('Failed to initialize Claude query with hooks, retrying without hooks:', hookError?.message || hookError);
704
- delete sdkOptions.hooks;
705
- queryInstance = query({
706
- prompt: finalCommand,
707
- options: sdkOptions
708
- });
709
- }
710
-
711
- // Restore immediately — Query constructor already captured the value
712
- if (prevStreamTimeout !== undefined) {
713
- process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = prevStreamTimeout;
714
- } else {
715
- delete process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
716
- }
717
-
718
- // Track the query instance for abort capability
719
- if (capturedSessionId) {
720
- addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
721
- }
722
-
723
- // Process streaming messages
724
- console.log('Starting async generator loop for session:', capturedSessionId || 'NEW');
725
- for await (const message of queryInstance) {
726
- // Capture session ID from first message
727
- if (message.session_id && !capturedSessionId) {
728
-
729
- capturedSessionId = message.session_id;
730
- addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
731
-
732
- // Set session ID on writer
733
- if (ws.setSessionId && typeof ws.setSessionId === 'function') {
734
- ws.setSessionId(capturedSessionId);
735
- }
736
-
737
- // Send session-created event only once for new sessions
738
- if (!sessionId && !sessionCreatedSent) {
739
- sessionCreatedSent = true;
740
- ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'claude' }));
741
- }
742
- } else {
743
- // session_id already captured
744
- }
745
-
746
- // Transform and normalize message via adapter
747
- const transformedMessage = transformMessage(message);
748
- const sid = capturedSessionId || sessionId || null;
749
-
750
- // Use adapter to normalize SDK events into NormalizedMessage[]
751
- const normalized = sessionsService.normalizeMessage('claude', transformedMessage, sid);
752
- for (const msg of normalized) {
753
- // Preserve parentToolUseId from SDK wrapper for subagent tool grouping
754
- if (transformedMessage.parentToolUseId && !msg.parentToolUseId) {
755
- msg.parentToolUseId = transformedMessage.parentToolUseId;
756
- }
757
- ws.send(msg);
758
- }
759
-
760
- // Extract and send token budget updates from result messages
761
- if (message.type === 'result') {
762
- const models = Object.keys(message.modelUsage || {});
763
- if (models.length > 0) {
764
- // Model info available in result message
765
- }
766
- const tokenBudgetData = extractTokenBudget(message);
767
- if (tokenBudgetData) {
768
- ws.send(createNormalizedMessage({ kind: 'status', text: 'token_budget', tokenBudget: tokenBudgetData, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
769
- }
770
- }
771
- }
772
-
773
- // Clean up session on completion
774
- if (capturedSessionId) {
775
- removeSession(capturedSessionId);
776
- }
777
-
778
- // Clean up temporary image files
779
- await cleanupTempFiles(tempImagePaths, tempDir);
780
-
781
- // Send completion event
782
- ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 0, isNewSession: !sessionId && !!command, sessionId: capturedSessionId, provider: 'claude' }));
783
- notifyRunStopped({
784
- userId: ws?.userId || null,
785
- provider: 'claude',
786
- sessionId: capturedSessionId || sessionId || null,
787
- sessionName: sessionSummary,
788
- stopReason: 'completed'
789
- });
790
- // Complete
791
-
792
- } catch (error) {
793
- console.error('SDK query error:', error);
794
-
795
- // Clean up session on error
796
- if (capturedSessionId) {
797
- removeSession(capturedSessionId);
798
- }
799
-
800
- // Clean up temporary image files on error
801
- await cleanupTempFiles(tempImagePaths, tempDir);
802
-
803
- // Check if Claude CLI is installed for a clearer error message
804
- const installed = await providerAuthService.isProviderInstalled('claude');
805
- const errorContent = !installed
806
- ? 'Claude Code is not installed. Please install it first: https://docs.anthropic.com/en/docs/claude-code'
807
- : error.message;
808
-
809
- // Send error to WebSocket
810
- ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
811
- notifyRunFailed({
812
- userId: ws?.userId || null,
813
- provider: 'claude',
814
- sessionId: capturedSessionId || sessionId || null,
815
- sessionName: sessionSummary,
816
- error
817
- });
818
- }
819
- }
820
-
821
- /**
822
- * Aborts an active SDK session
823
- * @param {string} sessionId - Session identifier
824
- * @returns {boolean} True if session was aborted, false if not found
825
- */
826
- async function abortClaudeSDKSession(sessionId) {
827
- const session = getSession(sessionId);
828
-
829
- if (!session) {
830
- console.log(`Session ${sessionId} not found`);
831
- return false;
832
- }
833
-
834
- try {
835
- console.log(`Aborting SDK session: ${sessionId}`);
836
-
837
- // Call interrupt() on the query instance
838
- await session.instance.interrupt();
839
-
840
- // Update session status
841
- session.status = 'aborted';
842
-
843
- // Clean up temporary image files
844
- await cleanupTempFiles(session.tempImagePaths, session.tempDir);
845
-
846
- // Clean up session
847
- removeSession(sessionId);
848
-
849
- return true;
850
- } catch (error) {
851
- console.error(`Error aborting session ${sessionId}:`, error);
852
- return false;
853
- }
854
- }
855
-
856
- /**
857
- * Checks if an SDK session is currently active
858
- * @param {string} sessionId - Session identifier
859
- * @returns {boolean} True if session is active
860
- */
861
- function isClaudeSDKSessionActive(sessionId) {
862
- const session = getSession(sessionId);
863
- return session && session.status === 'active';
864
- }
865
-
866
- /**
867
- * Gets all active SDK session IDs
868
- * @returns {Array<string>} Array of active session IDs
869
- */
870
- function getActiveClaudeSDKSessions() {
871
- return getAllSessions();
872
- }
873
-
874
- /**
875
- * Get pending tool approvals for a specific session.
876
- * @param {string} sessionId - The session ID
877
- * @returns {Array} Array of pending permission request objects
878
- */
879
- function getPendingApprovalsForSession(sessionId) {
880
- const pending = [];
881
- for (const [requestId, resolver] of pendingToolApprovals.entries()) {
882
- if (resolver._sessionId === sessionId) {
883
- pending.push({
884
- requestId,
885
- toolName: resolver._toolName || 'UnknownTool',
886
- input: resolver._input,
887
- context: resolver._context,
888
- sessionId,
889
- receivedAt: resolver._receivedAt || new Date(),
890
- });
891
- }
892
- }
893
- return pending;
894
- }
895
-
896
- /**
897
- * Reconnect a session's WebSocketWriter to a new raw WebSocket.
898
- * Called when client reconnects (e.g. page refresh) while SDK is still running.
899
- * @param {string} sessionId - The session ID
900
- * @param {Object} newRawWs - The new raw WebSocket connection
901
- * @returns {boolean} True if writer was successfully reconnected
902
- */
903
- function reconnectSessionWriter(sessionId, newRawWs) {
904
- const session = getSession(sessionId);
905
- if (!session?.writer?.updateWebSocket) return false;
906
- session.writer.updateWebSocket(newRawWs);
907
- console.log(`[RECONNECT] Writer swapped for session ${sessionId}`);
908
- return true;
909
- }
910
-
911
- // Export public API
912
- export {
913
- queryClaudeSDK,
914
- abortClaudeSDKSession,
915
- isClaudeSDKSessionActive,
916
- getActiveClaudeSDKSessions,
917
- resolveToolApproval,
918
- getPendingApprovalsForSession,
919
- reconnectSessionWriter
920
- };
1
+ /**
2
+ * Claude SDK Integration
3
+ *
4
+ * This module provides SDK-based integration with Claude using the @anthropic-ai/claude-agent-sdk.
5
+ * It mirrors the interface of claude-cli.js but uses the SDK internally for better performance
6
+ * and maintainability.
7
+ *
8
+ * Key features:
9
+ * - Direct SDK integration without child processes
10
+ * - Session management with abort capability
11
+ * - Options mapping between CLI and SDK formats
12
+ * - WebSocket message streaming
13
+ */
14
+
15
+ import crypto from 'crypto';
16
+ import { existsSync, readFileSync, promises as fs } from 'fs';
17
+ import path from 'path';
18
+ import os from 'os';
19
+
20
+ import { query } from '@anthropic-ai/claude-agent-sdk';
21
+
22
+ import {
23
+ createNotificationEvent,
24
+ notifyRunFailed,
25
+ notifyRunStopped,
26
+ notifyUserIfEnabled
27
+ } from './services/notification-orchestrator.js';
28
+ import { sessionsService } from './modules/providers/services/sessions.service.js';
29
+ import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
30
+ import { resolveClaudeExecutable, resolveGitBashPath } from './services/install-jobs.js';
31
+ import { createNormalizedMessage } from './shared/utils.js';
32
+ import { evaluatePermissionRequest } from './modules/orchestration/security/permission-policy.js';
33
+
34
+ const activeSessions = new Map();
35
+ const pendingToolApprovals = new Map();
36
+
37
+ const TOOL_APPROVAL_TIMEOUT_MS = parseInt(process.env.CLAUDE_TOOL_APPROVAL_TIMEOUT_MS, 10) || 55000;
38
+
39
+ const TOOLS_REQUIRING_INTERACTION = new Set(['AskUserQuestion', 'ExitPlanMode']);
40
+
41
+ function createRequestId() {
42
+ if (typeof crypto.randomUUID === 'function') {
43
+ return crypto.randomUUID();
44
+ }
45
+ return crypto.randomBytes(16).toString('hex');
46
+ }
47
+
48
+ function waitForToolApproval(requestId, options = {}) {
49
+ const { timeoutMs = TOOL_APPROVAL_TIMEOUT_MS, signal, onCancel, metadata } = options;
50
+
51
+ return new Promise(resolve => {
52
+ let settled = false;
53
+
54
+ const finalize = (decision) => {
55
+ if (settled) return;
56
+ settled = true;
57
+ cleanup();
58
+ resolve(decision);
59
+ };
60
+
61
+ let timeout;
62
+
63
+ const cleanup = () => {
64
+ pendingToolApprovals.delete(requestId);
65
+ if (timeout) clearTimeout(timeout);
66
+ if (signal && abortHandler) {
67
+ signal.removeEventListener('abort', abortHandler);
68
+ }
69
+ };
70
+
71
+ // timeoutMs 0 = wait indefinitely (interactive tools)
72
+ if (timeoutMs > 0) {
73
+ timeout = setTimeout(() => {
74
+ onCancel?.('timeout');
75
+ finalize(null);
76
+ }, timeoutMs);
77
+ }
78
+
79
+ const abortHandler = () => {
80
+ onCancel?.('cancelled');
81
+ finalize({ cancelled: true });
82
+ };
83
+
84
+ if (signal) {
85
+ if (signal.aborted) {
86
+ onCancel?.('cancelled');
87
+ finalize({ cancelled: true });
88
+ return;
89
+ }
90
+ signal.addEventListener('abort', abortHandler, { once: true });
91
+ }
92
+
93
+ const resolver = (decision) => {
94
+ finalize(decision);
95
+ };
96
+ // Attach metadata for getPendingApprovalsForSession lookup
97
+ if (metadata) {
98
+ Object.assign(resolver, metadata);
99
+ }
100
+ pendingToolApprovals.set(requestId, resolver);
101
+ });
102
+ }
103
+
104
+ function resolveToolApproval(requestId, decision) {
105
+ const resolver = pendingToolApprovals.get(requestId);
106
+ if (resolver) {
107
+ resolver(decision);
108
+ }
109
+ }
110
+
111
+ // Match stored permission entries against a tool + input combo.
112
+ // This only supports exact tool names and the Bash(command:*) shorthand
113
+ // used by the UI; it intentionally does not implement full glob semantics,
114
+ // introduced to stay consistent with the UI's "Allow rule" format.
115
+ function matchesToolPermission(entry, toolName, input) {
116
+ if (!entry || !toolName) {
117
+ return false;
118
+ }
119
+
120
+ if (entry === toolName) {
121
+ return true;
122
+ }
123
+
124
+ const bashMatch = entry.match(/^Bash\((.+):\*\)$/);
125
+ if (toolName === 'Bash' && bashMatch) {
126
+ const allowedPrefix = bashMatch[1];
127
+ let command = '';
128
+
129
+ if (typeof input === 'string') {
130
+ command = input.trim();
131
+ } else if (input && typeof input === 'object' && typeof input.command === 'string') {
132
+ command = input.command.trim();
133
+ }
134
+
135
+ if (!command) {
136
+ return false;
137
+ }
138
+
139
+ return command.startsWith(allowedPrefix);
140
+ }
141
+
142
+ return false;
143
+ }
144
+
145
+ function readClaudeSettingsEnv(filePath) {
146
+ try {
147
+ if (!existsSync(filePath)) return {};
148
+ const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
149
+ const env = parsed?.env;
150
+ if (!env || typeof env !== 'object') return {};
151
+
152
+ return Object.fromEntries(
153
+ Object.entries(env)
154
+ .filter(([key, value]) =>
155
+ typeof value === 'string' &&
156
+ value.trim() &&
157
+ !key.startsWith('//'),
158
+ ),
159
+ );
160
+ } catch {
161
+ return {};
162
+ }
163
+ }
164
+
165
+ function loadClaudeSettingsEnv(cwd) {
166
+ const files = [
167
+ path.join(os.homedir(), '.claude', 'settings.json'),
168
+ ];
169
+ if (cwd) {
170
+ files.push(
171
+ path.join(cwd, '.claude', 'settings.json'),
172
+ path.join(cwd, '.claude', 'settings.local.json'),
173
+ );
174
+ }
175
+
176
+ return files.reduce((env, filePath) => ({
177
+ ...env,
178
+ ...readClaudeSettingsEnv(filePath),
179
+ }), {});
180
+ }
181
+
182
+ /**
183
+ * Maps CLI options to SDK-compatible options format
184
+ * @param {Object} options - CLI options
185
+ * @returns {Object} SDK-compatible options
186
+ */
187
+ function mapCliOptionsToSDK(options = {}) {
188
+ const { sessionId, cwd, toolsSettings, permissionMode, permissionPolicy, permissionPolicyContext } = options;
189
+
190
+ const sdkOptions = {};
191
+
192
+ // Forward all host env vars (e.g. ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN) to the subprocess.
193
+ // Since claude-agent-sdk 0.2.113+, options.env REPLACES process.env in the subprocess
194
+ // instead of overlaying it. Without spreading process.env here, users who rely on
195
+ // ANTHROPIC_BASE_URL, HTTP(S)_PROXY, etc. would silently lose those settings.
196
+ sdkOptions.env = { ...process.env, ...loadClaudeSettingsEnv(cwd) };
197
+
198
+ // Claude Code on Windows hard-requires a POSIX bash (typically from Git
199
+ // for Windows) and reads its path from CLAUDE_CODE_GIT_BASH_PATH. If the
200
+ // user has git-bash installed but hasn't exported that var, the CLI
201
+ // exits with code 1 + a guidance message and the pixcode UI just sees
202
+ // an opaque "Claude Code process exited with code 1" error. Auto-probing
203
+ // a handful of known install locations lets the CLI boot transparently.
204
+ if (!sdkOptions.env.CLAUDE_CODE_GIT_BASH_PATH) {
205
+ const bashPath = resolveGitBashPath();
206
+ if (bashPath) sdkOptions.env.CLAUDE_CODE_GIT_BASH_PATH = bashPath;
207
+ }
208
+
209
+ // Resolve the Claude Code CLI path cross-platform. The SDK uses plain
210
+ // `child_process.spawn(command, args)` with no shell — and its own
211
+ // `nb()` helper treats anything not ending in .js/.mjs/.ts as a native
212
+ // executable. That means:
213
+ // - Unix: bare `"claude"` works (kernel PATH + shebang handle it).
214
+ // - Windows: bare `"claude"` fails ("native binary not found"). A
215
+ // `.cmd` shim fails with EINVAL (post-CVE-2024 Node refuses .cmd
216
+ // without shell:true). We need the underlying `.exe` instead.
217
+ // `resolveClaudeExecutable()` does a `where`/`which` lookup and, on
218
+ // Windows, peeks inside npm .cmd shims to recover the real .exe target.
219
+ // If nothing is found we leave the option unset so the SDK falls through
220
+ // to its own bundled-native-binary resolver.
221
+ const resolvedClaudePath = process.env.CLAUDE_CLI_PATH || resolveClaudeExecutable();
222
+ if (resolvedClaudePath) {
223
+ sdkOptions.pathToClaudeCodeExecutable = resolvedClaudePath;
224
+ }
225
+
226
+ // Map working directory
227
+ if (cwd) {
228
+ sdkOptions.cwd = cwd;
229
+ }
230
+
231
+ // Map permission mode
232
+ if (permissionMode && permissionMode !== 'default') {
233
+ sdkOptions.permissionMode = permissionMode;
234
+ }
235
+ sdkOptions.permissionPolicy = permissionPolicy;
236
+ sdkOptions.permissionPolicyContext = permissionPolicyContext;
237
+
238
+ // Map tool settings
239
+ const settings = toolsSettings || {
240
+ allowedTools: [],
241
+ disallowedTools: [],
242
+ skipPermissions: false
243
+ };
244
+
245
+ // Handle tool permissions
246
+ if (settings.skipPermissions && permissionMode !== 'plan') {
247
+ // When skipping permissions, use bypassPermissions mode
248
+ sdkOptions.permissionMode = 'bypassPermissions';
249
+ }
250
+
251
+ let allowedTools = [...(settings.allowedTools || [])];
252
+
253
+ // Add plan mode default tools
254
+ if (permissionMode === 'plan') {
255
+ const planModeTools = ['Read', 'Task', 'exit_plan_mode', 'TodoRead', 'TodoWrite', 'WebFetch', 'WebSearch'];
256
+ for (const tool of planModeTools) {
257
+ if (!allowedTools.includes(tool)) {
258
+ allowedTools.push(tool);
259
+ }
260
+ }
261
+ }
262
+
263
+ sdkOptions.allowedTools = allowedTools;
264
+
265
+ // Use the tools preset to make all default built-in tools available (including AskUserQuestion).
266
+ // This was introduced in SDK 0.1.57. Omitting this preserves existing behavior (all tools available),
267
+ // but being explicit ensures forward compatibility and clarity.
268
+ sdkOptions.tools = { type: 'preset', preset: 'claude_code' };
269
+
270
+ sdkOptions.disallowedTools = settings.disallowedTools || [];
271
+
272
+ // Map model only when Pixcode explicitly passes one. If omitted, let
273
+ // Claude Code load the user's own project/user/local settings, including
274
+ // ~/.claude/settings.json "model".
275
+ if (typeof options.model === 'string' && options.model.trim()) {
276
+ sdkOptions.model = options.model.trim();
277
+ }
278
+ // Model logged at query start below
279
+
280
+ // Map system prompt configuration
281
+ sdkOptions.systemPrompt = {
282
+ type: 'preset',
283
+ preset: 'claude_code' // Required to use CLAUDE.md
284
+ };
285
+
286
+ // Map setting sources for CLAUDE.md loading
287
+ // This loads CLAUDE.md from project, user (~/.config/claude/CLAUDE.md), and local directories
288
+ sdkOptions.settingSources = ['project', 'user', 'local'];
289
+
290
+ // Map resume session
291
+ if (sessionId) {
292
+ sdkOptions.resume = sessionId;
293
+ }
294
+
295
+ return sdkOptions;
296
+ }
297
+
298
+ /**
299
+ * Adds a session to the active sessions map
300
+ * @param {string} sessionId - Session identifier
301
+ * @param {Object} queryInstance - SDK query instance
302
+ * @param {Array<string>} tempImagePaths - Temp image file paths for cleanup
303
+ * @param {string} tempDir - Temp directory for cleanup
304
+ */
305
+ function addSession(sessionId, queryInstance, tempImagePaths = [], tempDir = null, writer = null) {
306
+ activeSessions.set(sessionId, {
307
+ instance: queryInstance,
308
+ startTime: Date.now(),
309
+ status: 'active',
310
+ tempImagePaths,
311
+ tempDir,
312
+ writer
313
+ });
314
+ }
315
+
316
+ /**
317
+ * Removes a session from the active sessions map
318
+ * @param {string} sessionId - Session identifier
319
+ */
320
+ function removeSession(sessionId) {
321
+ activeSessions.delete(sessionId);
322
+ }
323
+
324
+ /**
325
+ * Gets a session from the active sessions map
326
+ * @param {string} sessionId - Session identifier
327
+ * @returns {Object|undefined} Session data or undefined
328
+ */
329
+ function getSession(sessionId) {
330
+ return activeSessions.get(sessionId);
331
+ }
332
+
333
+ /**
334
+ * Gets all active session IDs
335
+ * @returns {Array<string>} Array of active session IDs
336
+ */
337
+ function getAllSessions() {
338
+ return Array.from(activeSessions.keys());
339
+ }
340
+
341
+ /**
342
+ * Transforms SDK messages to WebSocket format expected by frontend
343
+ * @param {Object} sdkMessage - SDK message object
344
+ * @returns {Object} Transformed message ready for WebSocket
345
+ */
346
+ function transformMessage(sdkMessage) {
347
+ // Extract parent_tool_use_id for subagent tool grouping
348
+ if (sdkMessage.parent_tool_use_id) {
349
+ return {
350
+ ...sdkMessage,
351
+ parentToolUseId: sdkMessage.parent_tool_use_id
352
+ };
353
+ }
354
+ return sdkMessage;
355
+ }
356
+
357
+ /**
358
+ * Extracts token usage from SDK result messages
359
+ * @param {Object} resultMessage - SDK result message
360
+ * @returns {Object|null} Token budget object or null
361
+ */
362
+ function extractTokenBudget(resultMessage) {
363
+ if (resultMessage.type !== 'result' || !resultMessage.modelUsage) {
364
+ return null;
365
+ }
366
+
367
+ // Get the first model's usage data
368
+ const modelKey = Object.keys(resultMessage.modelUsage)[0];
369
+ const modelData = resultMessage.modelUsage[modelKey];
370
+
371
+ if (!modelData) {
372
+ return null;
373
+ }
374
+
375
+ // Use cumulative tokens if available (tracks total for the session)
376
+ // Otherwise fall back to per-request tokens
377
+ const inputTokens = modelData.cumulativeInputTokens || modelData.inputTokens || 0;
378
+ const outputTokens = modelData.cumulativeOutputTokens || modelData.outputTokens || 0;
379
+ const cacheReadTokens = modelData.cumulativeCacheReadInputTokens || modelData.cacheReadInputTokens || 0;
380
+ const cacheCreationTokens = modelData.cumulativeCacheCreationInputTokens || modelData.cacheCreationInputTokens || 0;
381
+
382
+ // Total used = input + output + cache tokens
383
+ const totalUsed = inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens;
384
+
385
+ // Use configured context window budget from environment (default 160000)
386
+ // This is the user's budget limit, not the model's context window
387
+ const contextWindow = parseInt(process.env.CONTEXT_WINDOW) || 160000;
388
+
389
+ // Token calc logged via token-budget WS event
390
+
391
+ return {
392
+ used: totalUsed,
393
+ total: contextWindow
394
+ };
395
+ }
396
+
397
+ /**
398
+ * Handles image processing for SDK queries
399
+ * Saves base64 images to temporary files and returns modified prompt with file paths
400
+ * @param {string} command - Original user prompt
401
+ * @param {Array} images - Array of image objects with base64 data
402
+ * @param {string} cwd - Working directory for temp file creation
403
+ * @returns {Promise<Object>} {modifiedCommand, tempImagePaths, tempDir}
404
+ */
405
+ async function handleImages(command, images, cwd) {
406
+ const tempImagePaths = [];
407
+ let tempDir = null;
408
+
409
+ if (!images || images.length === 0) {
410
+ return { modifiedCommand: command, tempImagePaths, tempDir };
411
+ }
412
+
413
+ try {
414
+ // Create temp directory in the project directory
415
+ const workingDir = cwd || process.cwd();
416
+ tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
417
+ await fs.mkdir(tempDir, { recursive: true });
418
+
419
+ // Save each image to a temp file
420
+ for (const [index, image] of images.entries()) {
421
+ // Extract base64 data and mime type
422
+ const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
423
+ if (!matches) {
424
+ console.error('Invalid image data format');
425
+ continue;
426
+ }
427
+
428
+ const [, mimeType, base64Data] = matches;
429
+ const extension = mimeType.split('/')[1] || 'png';
430
+ const filename = `image_${index}.${extension}`;
431
+ const filepath = path.join(tempDir, filename);
432
+
433
+ // Write base64 data to file
434
+ await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
435
+ tempImagePaths.push(filepath);
436
+ }
437
+
438
+ // Include the full image paths in the prompt
439
+ let modifiedCommand = command;
440
+ if (tempImagePaths.length > 0 && command && command.trim()) {
441
+ const imageNote = `\n\n[Images provided at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
442
+ modifiedCommand = command + imageNote;
443
+ }
444
+
445
+ // Images processed
446
+ return { modifiedCommand, tempImagePaths, tempDir };
447
+ } catch (error) {
448
+ console.error('Error processing images for SDK:', error);
449
+ return { modifiedCommand: command, tempImagePaths, tempDir };
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Cleans up temporary image files
455
+ * @param {Array<string>} tempImagePaths - Array of temp file paths to delete
456
+ * @param {string} tempDir - Temp directory to remove
457
+ */
458
+ async function cleanupTempFiles(tempImagePaths, tempDir) {
459
+ if (!tempImagePaths || tempImagePaths.length === 0) {
460
+ return;
461
+ }
462
+
463
+ try {
464
+ // Delete individual temp files
465
+ for (const imagePath of tempImagePaths) {
466
+ await fs.unlink(imagePath).catch(err =>
467
+ console.error(`Failed to delete temp image ${imagePath}:`, err)
468
+ );
469
+ }
470
+
471
+ // Delete temp directory
472
+ if (tempDir) {
473
+ await fs.rm(tempDir, { recursive: true, force: true }).catch(err =>
474
+ console.error(`Failed to delete temp directory ${tempDir}:`, err)
475
+ );
476
+ }
477
+
478
+ // Temp files cleaned
479
+ } catch (error) {
480
+ console.error('Error during temp file cleanup:', error);
481
+ }
482
+ }
483
+
484
+ /**
485
+ * Loads MCP server configurations from ~/.claude.json
486
+ * @param {string} cwd - Current working directory for project-specific configs
487
+ * @returns {Object|null} MCP servers object or null if none found
488
+ */
489
+ async function loadMcpConfig(cwd) {
490
+ try {
491
+ const claudeConfigPath = path.join(os.homedir(), '.claude.json');
492
+
493
+ // Check if config file exists
494
+ try {
495
+ await fs.access(claudeConfigPath);
496
+ } catch (error) {
497
+ // File doesn't exist, return null
498
+ // No config file
499
+ return null;
500
+ }
501
+
502
+ // Read and parse config file
503
+ let claudeConfig;
504
+ try {
505
+ const configContent = await fs.readFile(claudeConfigPath, 'utf8');
506
+ claudeConfig = JSON.parse(configContent);
507
+ } catch (error) {
508
+ console.error('Failed to parse ~/.claude.json:', error.message);
509
+ return null;
510
+ }
511
+
512
+ // Extract MCP servers (merge global and project-specific)
513
+ let mcpServers = {};
514
+
515
+ // Add global MCP servers
516
+ if (claudeConfig.mcpServers && typeof claudeConfig.mcpServers === 'object') {
517
+ mcpServers = { ...claudeConfig.mcpServers };
518
+ // Global MCP servers loaded
519
+ }
520
+
521
+ // Add/override with project-specific MCP servers
522
+ if (claudeConfig.claudeProjects && cwd) {
523
+ const projectConfig = claudeConfig.claudeProjects[cwd];
524
+ if (projectConfig && projectConfig.mcpServers && typeof projectConfig.mcpServers === 'object') {
525
+ mcpServers = { ...mcpServers, ...projectConfig.mcpServers };
526
+ // Project MCP servers merged
527
+ }
528
+ }
529
+
530
+ // Return null if no servers found
531
+ if (Object.keys(mcpServers).length === 0) {
532
+ return null;
533
+ }
534
+ return mcpServers;
535
+ } catch (error) {
536
+ console.error('Error loading MCP config:', error.message);
537
+ return null;
538
+ }
539
+ }
540
+
541
+ /**
542
+ * Executes a Claude query using the SDK
543
+ * @param {string} command - User prompt/command
544
+ * @param {Object} options - Query options
545
+ * @param {Object} ws - WebSocket connection
546
+ * @returns {Promise<void>}
547
+ */
548
+ async function queryClaudeSDK(command, options = {}, ws) {
549
+ const { sessionId, sessionSummary } = options;
550
+ let capturedSessionId = sessionId;
551
+ let sessionCreatedSent = false;
552
+ let tempImagePaths = [];
553
+ let tempDir = null;
554
+
555
+ const emitNotification = (event) => {
556
+ notifyUserIfEnabled({
557
+ userId: ws?.userId || null,
558
+ writer: ws,
559
+ event
560
+ });
561
+ };
562
+
563
+ try {
564
+ // Map CLI options to SDK format
565
+ const sdkOptions = mapCliOptionsToSDK(options);
566
+
567
+ // Load MCP configuration
568
+ const mcpServers = await loadMcpConfig(options.cwd);
569
+ if (mcpServers) {
570
+ sdkOptions.mcpServers = mcpServers;
571
+ }
572
+
573
+ // Handle images - save to temp files and modify prompt
574
+ const imageResult = await handleImages(command, options.images, options.cwd);
575
+ const finalCommand = imageResult.modifiedCommand;
576
+ tempImagePaths = imageResult.tempImagePaths;
577
+ tempDir = imageResult.tempDir;
578
+
579
+ sdkOptions.hooks = {
580
+ Notification: [{
581
+ matcher: '',
582
+ hooks: [async (input) => {
583
+ const message = typeof input?.message === 'string' ? input.message : 'Claude requires your attention.';
584
+ emitNotification(createNotificationEvent({
585
+ provider: 'claude',
586
+ sessionId: capturedSessionId || sessionId || null,
587
+ kind: 'action_required',
588
+ code: 'agent.notification',
589
+ meta: { message, sessionName: sessionSummary },
590
+ severity: 'warning',
591
+ requiresUserAction: true,
592
+ dedupeKey: `claude:hook:notification:${capturedSessionId || sessionId || 'none'}:${message}`
593
+ }));
594
+ return {};
595
+ }]
596
+ }]
597
+ };
598
+
599
+ sdkOptions.canUseTool = async (toolName, input, context) => {
600
+ const requiresInteraction = TOOLS_REQUIRING_INTERACTION.has(toolName);
601
+ const policyDecision = evaluatePermissionRequest({
602
+ policy: sdkOptions.permissionPolicy,
603
+ request: {
604
+ source: 'provider_tool',
605
+ toolName,
606
+ input,
607
+ command: input && typeof input === 'object' && typeof input.command === 'string'
608
+ ? input.command
609
+ : undefined,
610
+ cwd: options.cwd,
611
+ workspacePath: options.cwd,
612
+ summary: `${toolName} tool request`,
613
+ },
614
+ context: sdkOptions.permissionPolicyContext,
615
+ });
616
+
617
+ if (policyDecision.behavior === 'deny') {
618
+ return { behavior: 'deny', message: policyDecision.message };
619
+ }
620
+
621
+ if (!requiresInteraction && policyDecision.behavior !== 'prompt') {
622
+ if (sdkOptions.permissionMode === 'bypassPermissions') {
623
+ return { behavior: 'allow', updatedInput: input };
624
+ }
625
+
626
+ const isDisallowed = (sdkOptions.disallowedTools || []).some(entry =>
627
+ matchesToolPermission(entry, toolName, input)
628
+ );
629
+ if (isDisallowed) {
630
+ return { behavior: 'deny', message: 'Tool disallowed by settings' };
631
+ }
632
+
633
+ const isAllowed = (sdkOptions.allowedTools || []).some(entry =>
634
+ matchesToolPermission(entry, toolName, input)
635
+ );
636
+ if (isAllowed) {
637
+ return { behavior: 'allow', updatedInput: input };
638
+ }
639
+ }
640
+
641
+ const requestId = createRequestId();
642
+ ws.send(createNormalizedMessage({ kind: 'permission_request', requestId, toolName, input, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
643
+ emitNotification(createNotificationEvent({
644
+ provider: 'claude',
645
+ sessionId: capturedSessionId || sessionId || null,
646
+ kind: 'action_required',
647
+ code: 'permission.required',
648
+ meta: { toolName, sessionName: sessionSummary },
649
+ severity: 'warning',
650
+ requiresUserAction: true,
651
+ dedupeKey: `claude:permission:${capturedSessionId || sessionId || 'none'}:${requestId}`
652
+ }));
653
+
654
+ const decision = await waitForToolApproval(requestId, {
655
+ timeoutMs: requiresInteraction ? 0 : undefined,
656
+ signal: context?.signal,
657
+ metadata: {
658
+ _sessionId: capturedSessionId || sessionId || null,
659
+ _toolName: toolName,
660
+ _input: input,
661
+ _receivedAt: new Date(),
662
+ },
663
+ onCancel: (reason) => {
664
+ ws.send(createNormalizedMessage({ kind: 'permission_cancelled', requestId, reason, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
665
+ }
666
+ });
667
+ if (!decision) {
668
+ return { behavior: 'deny', message: 'Permission request timed out' };
669
+ }
670
+
671
+ if (decision.cancelled) {
672
+ return { behavior: 'deny', message: 'Permission request cancelled' };
673
+ }
674
+
675
+ if (decision.allow) {
676
+ if (decision.rememberEntry && typeof decision.rememberEntry === 'string') {
677
+ if (!sdkOptions.allowedTools.includes(decision.rememberEntry)) {
678
+ sdkOptions.allowedTools.push(decision.rememberEntry);
679
+ }
680
+ if (Array.isArray(sdkOptions.disallowedTools)) {
681
+ sdkOptions.disallowedTools = sdkOptions.disallowedTools.filter(entry => entry !== decision.rememberEntry);
682
+ }
683
+ }
684
+ return { behavior: 'allow', updatedInput: decision.updatedInput ?? input };
685
+ }
686
+
687
+ return { behavior: 'deny', message: decision.message ?? 'User denied tool use' };
688
+ };
689
+
690
+ // Set stream-close timeout for interactive tools (Query constructor reads it synchronously). Claude Agent SDK has a default of 5s and this overrides it
691
+ const prevStreamTimeout = process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
692
+ process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = '300000';
693
+
694
+ let queryInstance;
695
+ try {
696
+ queryInstance = query({
697
+ prompt: finalCommand,
698
+ options: sdkOptions
699
+ });
700
+ } catch (hookError) {
701
+ // Older/newer SDK versions may not accept hook shapes yet.
702
+ // Keep notification behavior operational via runtime events even if hook registration fails.
703
+ console.warn('Failed to initialize Claude query with hooks, retrying without hooks:', hookError?.message || hookError);
704
+ delete sdkOptions.hooks;
705
+ queryInstance = query({
706
+ prompt: finalCommand,
707
+ options: sdkOptions
708
+ });
709
+ }
710
+
711
+ // Restore immediately — Query constructor already captured the value
712
+ if (prevStreamTimeout !== undefined) {
713
+ process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = prevStreamTimeout;
714
+ } else {
715
+ delete process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
716
+ }
717
+
718
+ // Track the query instance for abort capability
719
+ if (capturedSessionId) {
720
+ addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
721
+ }
722
+
723
+ // Process streaming messages
724
+ console.log('Starting async generator loop for session:', capturedSessionId || 'NEW');
725
+ for await (const message of queryInstance) {
726
+ // Capture session ID from first message
727
+ if (message.session_id && !capturedSessionId) {
728
+
729
+ capturedSessionId = message.session_id;
730
+ addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
731
+
732
+ // Set session ID on writer
733
+ if (ws.setSessionId && typeof ws.setSessionId === 'function') {
734
+ ws.setSessionId(capturedSessionId);
735
+ }
736
+
737
+ // Send session-created event only once for new sessions
738
+ if (!sessionId && !sessionCreatedSent) {
739
+ sessionCreatedSent = true;
740
+ ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'claude' }));
741
+ }
742
+ } else {
743
+ // session_id already captured
744
+ }
745
+
746
+ // Transform and normalize message via adapter
747
+ const transformedMessage = transformMessage(message);
748
+ const sid = capturedSessionId || sessionId || null;
749
+
750
+ // Use adapter to normalize SDK events into NormalizedMessage[]
751
+ const normalized = sessionsService.normalizeMessage('claude', transformedMessage, sid);
752
+ for (const msg of normalized) {
753
+ // Preserve parentToolUseId from SDK wrapper for subagent tool grouping
754
+ if (transformedMessage.parentToolUseId && !msg.parentToolUseId) {
755
+ msg.parentToolUseId = transformedMessage.parentToolUseId;
756
+ }
757
+ ws.send(msg);
758
+ }
759
+
760
+ // Extract and send token budget updates from result messages
761
+ if (message.type === 'result') {
762
+ const models = Object.keys(message.modelUsage || {});
763
+ if (models.length > 0) {
764
+ // Model info available in result message
765
+ }
766
+ const tokenBudgetData = extractTokenBudget(message);
767
+ if (tokenBudgetData) {
768
+ ws.send(createNormalizedMessage({ kind: 'status', text: 'token_budget', tokenBudget: tokenBudgetData, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
769
+ }
770
+ }
771
+ }
772
+
773
+ // Clean up session on completion
774
+ if (capturedSessionId) {
775
+ removeSession(capturedSessionId);
776
+ }
777
+
778
+ // Clean up temporary image files
779
+ await cleanupTempFiles(tempImagePaths, tempDir);
780
+
781
+ // Send completion event
782
+ ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 0, isNewSession: !sessionId && !!command, sessionId: capturedSessionId, provider: 'claude' }));
783
+ notifyRunStopped({
784
+ userId: ws?.userId || null,
785
+ provider: 'claude',
786
+ sessionId: capturedSessionId || sessionId || null,
787
+ sessionName: sessionSummary,
788
+ stopReason: 'completed'
789
+ });
790
+ // Complete
791
+
792
+ } catch (error) {
793
+ console.error('SDK query error:', error);
794
+
795
+ // Clean up session on error
796
+ if (capturedSessionId) {
797
+ removeSession(capturedSessionId);
798
+ }
799
+
800
+ // Clean up temporary image files on error
801
+ await cleanupTempFiles(tempImagePaths, tempDir);
802
+
803
+ // Check if Claude CLI is installed for a clearer error message
804
+ const installed = await providerAuthService.isProviderInstalled('claude');
805
+ const errorContent = !installed
806
+ ? 'Claude Code is not installed. Please install it first: https://docs.anthropic.com/en/docs/claude-code'
807
+ : error.message;
808
+
809
+ // Send error to WebSocket
810
+ ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: capturedSessionId || sessionId || null, provider: 'claude' }));
811
+ notifyRunFailed({
812
+ userId: ws?.userId || null,
813
+ provider: 'claude',
814
+ sessionId: capturedSessionId || sessionId || null,
815
+ sessionName: sessionSummary,
816
+ error
817
+ });
818
+ }
819
+ }
820
+
821
+ /**
822
+ * Aborts an active SDK session
823
+ * @param {string} sessionId - Session identifier
824
+ * @returns {boolean} True if session was aborted, false if not found
825
+ */
826
+ async function abortClaudeSDKSession(sessionId) {
827
+ const session = getSession(sessionId);
828
+
829
+ if (!session) {
830
+ console.log(`Session ${sessionId} not found`);
831
+ return false;
832
+ }
833
+
834
+ try {
835
+ console.log(`Aborting SDK session: ${sessionId}`);
836
+
837
+ // Call interrupt() on the query instance
838
+ await session.instance.interrupt();
839
+
840
+ // Update session status
841
+ session.status = 'aborted';
842
+
843
+ // Clean up temporary image files
844
+ await cleanupTempFiles(session.tempImagePaths, session.tempDir);
845
+
846
+ // Clean up session
847
+ removeSession(sessionId);
848
+
849
+ return true;
850
+ } catch (error) {
851
+ console.error(`Error aborting session ${sessionId}:`, error);
852
+ return false;
853
+ }
854
+ }
855
+
856
+ /**
857
+ * Checks if an SDK session is currently active
858
+ * @param {string} sessionId - Session identifier
859
+ * @returns {boolean} True if session is active
860
+ */
861
+ function isClaudeSDKSessionActive(sessionId) {
862
+ const session = getSession(sessionId);
863
+ return session && session.status === 'active';
864
+ }
865
+
866
+ /**
867
+ * Gets all active SDK session IDs
868
+ * @returns {Array<string>} Array of active session IDs
869
+ */
870
+ function getActiveClaudeSDKSessions() {
871
+ return getAllSessions();
872
+ }
873
+
874
+ /**
875
+ * Get pending tool approvals for a specific session.
876
+ * @param {string} sessionId - The session ID
877
+ * @returns {Array} Array of pending permission request objects
878
+ */
879
+ function getPendingApprovalsForSession(sessionId) {
880
+ const pending = [];
881
+ for (const [requestId, resolver] of pendingToolApprovals.entries()) {
882
+ if (resolver._sessionId === sessionId) {
883
+ pending.push({
884
+ requestId,
885
+ toolName: resolver._toolName || 'UnknownTool',
886
+ input: resolver._input,
887
+ context: resolver._context,
888
+ sessionId,
889
+ receivedAt: resolver._receivedAt || new Date(),
890
+ });
891
+ }
892
+ }
893
+ return pending;
894
+ }
895
+
896
+ /**
897
+ * Reconnect a session's WebSocketWriter to a new raw WebSocket.
898
+ * Called when client reconnects (e.g. page refresh) while SDK is still running.
899
+ * @param {string} sessionId - The session ID
900
+ * @param {Object} newRawWs - The new raw WebSocket connection
901
+ * @returns {boolean} True if writer was successfully reconnected
902
+ */
903
+ function reconnectSessionWriter(sessionId, newRawWs) {
904
+ const session = getSession(sessionId);
905
+ if (!session?.writer?.updateWebSocket) return false;
906
+ session.writer.updateWebSocket(newRawWs);
907
+ console.log(`[RECONNECT] Writer swapped for session ${sessionId}`);
908
+ return true;
909
+ }
910
+
911
+ // Export public API
912
+ export {
913
+ queryClaudeSDK,
914
+ abortClaudeSDKSession,
915
+ isClaudeSDKSessionActive,
916
+ getActiveClaudeSDKSessions,
917
+ resolveToolApproval,
918
+ getPendingApprovalsForSession,
919
+ reconnectSessionWriter
920
+ };