notioncode 0.1.0 → 0.1.2

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 (78) hide show
  1. package/README.md +22 -9
  2. package/agent-runtime-server/package-lock.json +4377 -0
  3. package/agent-runtime-server/package.json +36 -0
  4. package/agent-runtime-server/scripts/fix-node-pty.js +67 -0
  5. package/agent-runtime-server/server/agent-session-service.js +816 -0
  6. package/agent-runtime-server/server/claude-sdk.js +836 -0
  7. package/agent-runtime-server/server/cli.js +330 -0
  8. package/agent-runtime-server/server/constants/config.js +5 -0
  9. package/agent-runtime-server/server/cursor-cli.js +335 -0
  10. package/agent-runtime-server/server/database/db.js +653 -0
  11. package/agent-runtime-server/server/database/init.sql +99 -0
  12. package/agent-runtime-server/server/gemini-cli.js +460 -0
  13. package/agent-runtime-server/server/gemini-response-handler.js +79 -0
  14. package/agent-runtime-server/server/index.js +2569 -0
  15. package/agent-runtime-server/server/load-env.js +32 -0
  16. package/agent-runtime-server/server/middleware/auth.js +132 -0
  17. package/agent-runtime-server/server/openai-codex.js +512 -0
  18. package/agent-runtime-server/server/projects.js +2594 -0
  19. package/agent-runtime-server/server/providers/claude/adapter.js +278 -0
  20. package/agent-runtime-server/server/providers/codex/adapter.js +248 -0
  21. package/agent-runtime-server/server/providers/cursor/adapter.js +353 -0
  22. package/agent-runtime-server/server/providers/gemini/adapter.js +186 -0
  23. package/agent-runtime-server/server/providers/registry.js +44 -0
  24. package/agent-runtime-server/server/providers/types.js +119 -0
  25. package/agent-runtime-server/server/providers/utils.js +29 -0
  26. package/agent-runtime-server/server/routes/agent-sessions.js +238 -0
  27. package/agent-runtime-server/server/routes/agent.js +1244 -0
  28. package/agent-runtime-server/server/routes/auth.js +144 -0
  29. package/agent-runtime-server/server/routes/cli-auth.js +478 -0
  30. package/agent-runtime-server/server/routes/codex.js +329 -0
  31. package/agent-runtime-server/server/routes/commands.js +596 -0
  32. package/agent-runtime-server/server/routes/cursor.js +798 -0
  33. package/agent-runtime-server/server/routes/gemini.js +24 -0
  34. package/agent-runtime-server/server/routes/git.js +1508 -0
  35. package/agent-runtime-server/server/routes/mcp-utils.js +48 -0
  36. package/agent-runtime-server/server/routes/mcp.js +552 -0
  37. package/agent-runtime-server/server/routes/messages.js +61 -0
  38. package/agent-runtime-server/server/routes/plugins.js +307 -0
  39. package/agent-runtime-server/server/routes/projects.js +548 -0
  40. package/agent-runtime-server/server/routes/settings.js +276 -0
  41. package/agent-runtime-server/server/routes/taskmaster.js +1963 -0
  42. package/agent-runtime-server/server/routes/user.js +123 -0
  43. package/agent-runtime-server/server/services/notification-orchestrator.js +227 -0
  44. package/agent-runtime-server/server/services/vapid-keys.js +35 -0
  45. package/agent-runtime-server/server/sessionManager.js +226 -0
  46. package/agent-runtime-server/server/utils/commandParser.js +303 -0
  47. package/agent-runtime-server/server/utils/frontmatter.js +18 -0
  48. package/agent-runtime-server/server/utils/gitConfig.js +34 -0
  49. package/agent-runtime-server/server/utils/mcp-detector.js +198 -0
  50. package/agent-runtime-server/server/utils/plugin-loader.js +457 -0
  51. package/agent-runtime-server/server/utils/plugin-process-manager.js +184 -0
  52. package/agent-runtime-server/server/utils/taskmaster-websocket.js +129 -0
  53. package/agent-runtime-server/shared/modelConstants.js +12 -0
  54. package/agent-runtime-server/shared/modelConstants.test.js +34 -0
  55. package/agent-runtime-server/shared/networkHosts.js +22 -0
  56. package/agent-runtime-server/test_sdk.mjs +16 -0
  57. package/bin/bridges/darwin-x64/nocode-bridge +0 -0
  58. package/bin/{nocode-local.js → notioncode.js} +2 -8
  59. package/dist/assets/icon-CQtd7WEB.png +0 -0
  60. package/dist/assets/index-D_1ZrHDe.js +1 -0
  61. package/dist/assets/index-DhCWie1Z.css +1 -0
  62. package/dist/assets/index-DkGqIiwF.js +689 -0
  63. package/dist/index.html +46 -0
  64. package/dist/onboarding/step1_create.png +0 -0
  65. package/dist/onboarding/step2_capabilities.png +0 -0
  66. package/dist/onboarding/step2b_content_access.png +0 -0
  67. package/dist/onboarding/step2c_page_access.png +0 -0
  68. package/dist/onboarding/step3_token.png +0 -0
  69. package/dist/onboarding/step4_webhook.png +0 -0
  70. package/dist/onboarding/step6a_verify.png +0 -0
  71. package/dist/onboarding/step6b_copy_verify_token.png +0 -0
  72. package/dist/tinyfish-fish-only.png +0 -0
  73. package/lib/certs.js +332 -0
  74. package/lib/install.js +48 -4
  75. package/lib/start.js +346 -29
  76. package/package.json +10 -4
  77. package/src/shared/modelRegistry.d.ts +24 -0
  78. package/src/shared/modelRegistry.js +163 -0
@@ -0,0 +1,836 @@
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 { query } from '@anthropic-ai/claude-agent-sdk';
16
+ import crypto from 'crypto';
17
+ import { promises as fs } from 'fs';
18
+ import path from 'path';
19
+ import os from 'os';
20
+ import { CLAUDE_MODELS } from '../shared/modelConstants.js';
21
+ import {
22
+ createNotificationEvent,
23
+ notifyRunFailed,
24
+ notifyRunStopped,
25
+ notifyUserIfEnabled
26
+ } from './services/notification-orchestrator.js';
27
+ import { claudeAdapter } from './providers/claude/adapter.js';
28
+ import { createNormalizedMessage } from './providers/types.js';
29
+
30
+ const activeSessions = new Map();
31
+ const pendingToolApprovals = new Map();
32
+
33
+ const TOOL_APPROVAL_TIMEOUT_MS = parseInt(process.env.CLAUDE_TOOL_APPROVAL_TIMEOUT_MS, 10) || 55000;
34
+
35
+ const TOOLS_REQUIRING_INTERACTION = new Set(['AskUserQuestion']);
36
+
37
+ function createRequestId() {
38
+ if (typeof crypto.randomUUID === 'function') {
39
+ return crypto.randomUUID();
40
+ }
41
+ return crypto.randomBytes(16).toString('hex');
42
+ }
43
+
44
+ function waitForToolApproval(requestId, options = {}) {
45
+ const { timeoutMs = TOOL_APPROVAL_TIMEOUT_MS, signal, onCancel, metadata } = options;
46
+
47
+ return new Promise(resolve => {
48
+ let settled = false;
49
+
50
+ const finalize = (decision) => {
51
+ if (settled) return;
52
+ settled = true;
53
+ cleanup();
54
+ resolve(decision);
55
+ };
56
+
57
+ let timeout;
58
+
59
+ const cleanup = () => {
60
+ pendingToolApprovals.delete(requestId);
61
+ if (timeout) clearTimeout(timeout);
62
+ if (signal && abortHandler) {
63
+ signal.removeEventListener('abort', abortHandler);
64
+ }
65
+ };
66
+
67
+ // timeoutMs 0 = wait indefinitely (interactive tools)
68
+ if (timeoutMs > 0) {
69
+ timeout = setTimeout(() => {
70
+ onCancel?.('timeout');
71
+ finalize(null);
72
+ }, timeoutMs);
73
+ }
74
+
75
+ const abortHandler = () => {
76
+ onCancel?.('cancelled');
77
+ finalize({ cancelled: true });
78
+ };
79
+
80
+ if (signal) {
81
+ if (signal.aborted) {
82
+ onCancel?.('cancelled');
83
+ finalize({ cancelled: true });
84
+ return;
85
+ }
86
+ signal.addEventListener('abort', abortHandler, { once: true });
87
+ }
88
+
89
+ const resolver = (decision) => {
90
+ finalize(decision);
91
+ };
92
+ // Attach metadata for getPendingApprovalsForSession lookup
93
+ if (metadata) {
94
+ Object.assign(resolver, metadata);
95
+ }
96
+ pendingToolApprovals.set(requestId, resolver);
97
+ });
98
+ }
99
+
100
+ function resolveToolApproval(requestId, decision) {
101
+ const resolver = pendingToolApprovals.get(requestId);
102
+ if (resolver) {
103
+ resolver(decision);
104
+ }
105
+ }
106
+
107
+ // Match stored permission entries against a tool + input combo.
108
+ // This only supports exact tool names and the Bash(command:*) shorthand
109
+ // used by the UI; it intentionally does not implement full glob semantics,
110
+ // introduced to stay consistent with the UI's "Allow rule" format.
111
+ function matchesToolPermission(entry, toolName, input) {
112
+ if (!entry || !toolName) {
113
+ return false;
114
+ }
115
+
116
+ if (entry === toolName) {
117
+ return true;
118
+ }
119
+
120
+ const bashMatch = entry.match(/^Bash\((.+):\*\)$/);
121
+ if (toolName === 'Bash' && bashMatch) {
122
+ const allowedPrefix = bashMatch[1];
123
+ let command = '';
124
+
125
+ if (typeof input === 'string') {
126
+ command = input.trim();
127
+ } else if (input && typeof input === 'object' && typeof input.command === 'string') {
128
+ command = input.command.trim();
129
+ }
130
+
131
+ if (!command) {
132
+ return false;
133
+ }
134
+
135
+ return command.startsWith(allowedPrefix);
136
+ }
137
+
138
+ return false;
139
+ }
140
+
141
+ /**
142
+ * Maps CLI options to SDK-compatible options format
143
+ * @param {Object} options - CLI options
144
+ * @returns {Object} SDK-compatible options
145
+ */
146
+ function mapCliOptionsToSDK(options = {}) {
147
+ const { sessionId, cwd, toolsSettings, permissionMode, apiKey, baseUrl } = options;
148
+
149
+ const sdkOptions = {};
150
+
151
+ // Map working directory
152
+ if (cwd) {
153
+ sdkOptions.cwd = cwd;
154
+ }
155
+
156
+ if (apiKey || baseUrl || options.env) {
157
+ sdkOptions.env = {
158
+ ...process.env,
159
+ ...(options.env || {}),
160
+ ...(apiKey ? { ANTHROPIC_API_KEY: apiKey } : {}),
161
+ ...(baseUrl ? { ANTHROPIC_BASE_URL: baseUrl } : {}),
162
+ };
163
+ }
164
+
165
+ // Map permission mode
166
+ if (permissionMode && permissionMode !== 'default') {
167
+ sdkOptions.permissionMode = permissionMode;
168
+ }
169
+
170
+ if (sdkOptions.permissionMode === 'bypassPermissions') {
171
+ sdkOptions.allowDangerouslySkipPermissions = true;
172
+ }
173
+
174
+ // Map tool settings
175
+ const settings = toolsSettings || {
176
+ allowedTools: [],
177
+ disallowedTools: [],
178
+ skipPermissions: false
179
+ };
180
+
181
+ // Handle tool permissions
182
+ if (settings.skipPermissions && permissionMode !== 'plan') {
183
+ // When skipping permissions, use bypassPermissions mode
184
+ sdkOptions.permissionMode = 'bypassPermissions';
185
+ sdkOptions.allowDangerouslySkipPermissions = true;
186
+ }
187
+
188
+ let allowedTools = [...(settings.allowedTools || [])];
189
+
190
+ // Add plan mode default tools
191
+ if (permissionMode === 'plan') {
192
+ const planModeTools = ['Read', 'Task', 'exit_plan_mode', 'TodoRead', 'TodoWrite', 'WebFetch', 'WebSearch'];
193
+ for (const tool of planModeTools) {
194
+ if (!allowedTools.includes(tool)) {
195
+ allowedTools.push(tool);
196
+ }
197
+ }
198
+ }
199
+
200
+ sdkOptions.allowedTools = allowedTools;
201
+
202
+ // Use the tools preset to make all default built-in tools available (including AskUserQuestion).
203
+ // This was introduced in SDK 0.1.57. Omitting this preserves existing behavior (all tools available),
204
+ // but being explicit ensures forward compatibility and clarity.
205
+ sdkOptions.tools = { type: 'preset', preset: 'claude_code' };
206
+
207
+ sdkOptions.disallowedTools = settings.disallowedTools || [];
208
+
209
+ // Map model (default to sonnet)
210
+ // Valid models: sonnet, opus, haiku, opusplan, sonnet[1m]
211
+ sdkOptions.model = options.model || CLAUDE_MODELS.DEFAULT;
212
+ // Model logged at query start below
213
+
214
+ // Map system prompt configuration
215
+ sdkOptions.systemPrompt = {
216
+ type: 'preset',
217
+ preset: 'claude_code' // Required to use CLAUDE.md
218
+ };
219
+
220
+ // Map setting sources for CLAUDE.md loading
221
+ // This loads CLAUDE.md from project, user (~/.config/claude/CLAUDE.md), and local directories
222
+ sdkOptions.settingSources = ['project', 'user', 'local'];
223
+
224
+ // Map resume session
225
+ if (sessionId && !apiKey) {
226
+ sdkOptions.resume = sessionId;
227
+ }
228
+
229
+ return sdkOptions;
230
+ }
231
+
232
+ /**
233
+ * Adds a session to the active sessions map
234
+ * @param {string} sessionId - Session identifier
235
+ * @param {Object} queryInstance - SDK query instance
236
+ * @param {Array<string>} tempImagePaths - Temp image file paths for cleanup
237
+ * @param {string} tempDir - Temp directory for cleanup
238
+ */
239
+ function addSession(sessionId, queryInstance, tempImagePaths = [], tempDir = null, writer = null) {
240
+ activeSessions.set(sessionId, {
241
+ instance: queryInstance,
242
+ startTime: Date.now(),
243
+ status: 'active',
244
+ tempImagePaths,
245
+ tempDir,
246
+ writer
247
+ });
248
+ }
249
+
250
+ /**
251
+ * Removes a session from the active sessions map
252
+ * @param {string} sessionId - Session identifier
253
+ */
254
+ function removeSession(sessionId) {
255
+ activeSessions.delete(sessionId);
256
+ }
257
+
258
+ /**
259
+ * Gets a session from the active sessions map
260
+ * @param {string} sessionId - Session identifier
261
+ * @returns {Object|undefined} Session data or undefined
262
+ */
263
+ function getSession(sessionId) {
264
+ return activeSessions.get(sessionId);
265
+ }
266
+
267
+ /**
268
+ * Gets all active session IDs
269
+ * @returns {Array<string>} Array of active session IDs
270
+ */
271
+ function getAllSessions() {
272
+ return Array.from(activeSessions.keys());
273
+ }
274
+
275
+ /**
276
+ * Transforms SDK messages to WebSocket format expected by frontend
277
+ * @param {Object} sdkMessage - SDK message object
278
+ * @returns {Object} Transformed message ready for WebSocket
279
+ */
280
+ function transformMessage(sdkMessage) {
281
+ // Extract parent_tool_use_id for subagent tool grouping
282
+ if (sdkMessage.parent_tool_use_id) {
283
+ return {
284
+ ...sdkMessage,
285
+ parentToolUseId: sdkMessage.parent_tool_use_id
286
+ };
287
+ }
288
+ return sdkMessage;
289
+ }
290
+
291
+ /**
292
+ * Extracts token usage from SDK result messages
293
+ * @param {Object} resultMessage - SDK result message
294
+ * @returns {Object|null} Token budget object or null
295
+ */
296
+ function extractTokenBudget(resultMessage) {
297
+ if (resultMessage.type !== 'result' || !resultMessage.modelUsage) {
298
+ return null;
299
+ }
300
+
301
+ // Get the first model's usage data
302
+ const modelKey = Object.keys(resultMessage.modelUsage)[0];
303
+ const modelData = resultMessage.modelUsage[modelKey];
304
+
305
+ if (!modelData) {
306
+ return null;
307
+ }
308
+
309
+ // Use cumulative tokens if available (tracks total for the session)
310
+ // Otherwise fall back to per-request tokens
311
+ const inputTokens = modelData.cumulativeInputTokens || modelData.inputTokens || 0;
312
+ const outputTokens = modelData.cumulativeOutputTokens || modelData.outputTokens || 0;
313
+ const cacheReadTokens = modelData.cumulativeCacheReadInputTokens || modelData.cacheReadInputTokens || 0;
314
+ const cacheCreationTokens = modelData.cumulativeCacheCreationInputTokens || modelData.cacheCreationInputTokens || 0;
315
+
316
+ // Total used = input + output + cache tokens
317
+ const totalUsed = inputTokens + outputTokens + cacheReadTokens + cacheCreationTokens;
318
+
319
+ // Use configured context window budget from environment (default 160000)
320
+ // This is the user's budget limit, not the model's context window
321
+ const contextWindow = parseInt(process.env.CONTEXT_WINDOW) || 160000;
322
+
323
+ // Token calc logged via token-budget WS event
324
+
325
+ return {
326
+ used: totalUsed,
327
+ total: contextWindow,
328
+ inputTokens,
329
+ outputTokens,
330
+ cacheReadTokens,
331
+ cacheCreationTokens
332
+ };
333
+ }
334
+
335
+ /**
336
+ * Handles image processing for SDK queries
337
+ * Saves base64 images to temporary files and returns modified prompt with file paths
338
+ * @param {string} command - Original user prompt
339
+ * @param {Array} images - Array of image objects with base64 data
340
+ * @param {string} cwd - Working directory for temp file creation
341
+ * @returns {Promise<Object>} {modifiedCommand, tempImagePaths, tempDir}
342
+ */
343
+ async function handleImages(command, images, cwd) {
344
+ const tempImagePaths = [];
345
+ let tempDir = null;
346
+
347
+ if (!images || images.length === 0) {
348
+ return { modifiedCommand: command, tempImagePaths, tempDir };
349
+ }
350
+
351
+ try {
352
+ // Create temp directory in the project directory
353
+ const workingDir = cwd || process.cwd();
354
+ tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
355
+ await fs.mkdir(tempDir, { recursive: true });
356
+
357
+ // Save each image to a temp file
358
+ for (const [index, image] of images.entries()) {
359
+ // Extract base64 data and mime type
360
+ const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
361
+ if (!matches) {
362
+ console.error('Invalid image data format');
363
+ continue;
364
+ }
365
+
366
+ const [, mimeType, base64Data] = matches;
367
+ const extension = mimeType.split('/')[1] || 'png';
368
+ const filename = `image_${index}.${extension}`;
369
+ const filepath = path.join(tempDir, filename);
370
+
371
+ // Write base64 data to file
372
+ await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
373
+ tempImagePaths.push(filepath);
374
+ }
375
+
376
+ // Include the full image paths in the prompt
377
+ let modifiedCommand = command;
378
+ if (tempImagePaths.length > 0 && command && command.trim()) {
379
+ const imageNote = `\n\n[Images provided at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
380
+ modifiedCommand = command + imageNote;
381
+ }
382
+
383
+ // Images processed
384
+ return { modifiedCommand, tempImagePaths, tempDir };
385
+ } catch (error) {
386
+ console.error('Error processing images for SDK:', error);
387
+ return { modifiedCommand: command, tempImagePaths, tempDir };
388
+ }
389
+ }
390
+
391
+ /**
392
+ * Cleans up temporary image files
393
+ * @param {Array<string>} tempImagePaths - Array of temp file paths to delete
394
+ * @param {string} tempDir - Temp directory to remove
395
+ */
396
+ async function cleanupTempFiles(tempImagePaths, tempDir) {
397
+ if (!tempImagePaths || tempImagePaths.length === 0) {
398
+ return;
399
+ }
400
+
401
+ try {
402
+ // Delete individual temp files
403
+ for (const imagePath of tempImagePaths) {
404
+ await fs.unlink(imagePath).catch(err =>
405
+ console.error(`Failed to delete temp image ${imagePath}:`, err)
406
+ );
407
+ }
408
+
409
+ // Delete temp directory
410
+ if (tempDir) {
411
+ await fs.rm(tempDir, { recursive: true, force: true }).catch(err =>
412
+ console.error(`Failed to delete temp directory ${tempDir}:`, err)
413
+ );
414
+ }
415
+
416
+ // Temp files cleaned
417
+ } catch (error) {
418
+ console.error('Error during temp file cleanup:', error);
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Loads MCP server configurations from ~/.claude.json
424
+ * @param {string} cwd - Current working directory for project-specific configs
425
+ * @returns {Object|null} MCP servers object or null if none found
426
+ */
427
+ async function loadMcpConfig(cwd) {
428
+ try {
429
+ const claudeConfigPath = path.join(os.homedir(), '.claude.json');
430
+
431
+ // Check if config file exists
432
+ try {
433
+ await fs.access(claudeConfigPath);
434
+ } catch (error) {
435
+ // File doesn't exist, return null
436
+ // No config file
437
+ return null;
438
+ }
439
+
440
+ // Read and parse config file
441
+ let claudeConfig;
442
+ try {
443
+ const configContent = await fs.readFile(claudeConfigPath, 'utf8');
444
+ claudeConfig = JSON.parse(configContent);
445
+ } catch (error) {
446
+ console.error('Failed to parse ~/.claude.json:', error.message);
447
+ return null;
448
+ }
449
+
450
+ // Extract MCP servers (merge global and project-specific)
451
+ let mcpServers = {};
452
+
453
+ // Add global MCP servers
454
+ if (claudeConfig.mcpServers && typeof claudeConfig.mcpServers === 'object') {
455
+ mcpServers = { ...claudeConfig.mcpServers };
456
+ // Global MCP servers loaded
457
+ }
458
+
459
+ // Add/override with project-specific MCP servers
460
+ if (claudeConfig.claudeProjects && cwd) {
461
+ const projectConfig = claudeConfig.claudeProjects[cwd];
462
+ if (projectConfig && projectConfig.mcpServers && typeof projectConfig.mcpServers === 'object') {
463
+ mcpServers = { ...mcpServers, ...projectConfig.mcpServers };
464
+ // Project MCP servers merged
465
+ }
466
+ }
467
+
468
+ // Return null if no servers found
469
+ if (Object.keys(mcpServers).length === 0) {
470
+ return null;
471
+ }
472
+ return mcpServers;
473
+ } catch (error) {
474
+ console.error('Error loading MCP config:', error.message);
475
+ return null;
476
+ }
477
+ }
478
+
479
+ /**
480
+ * Executes a Claude query using the SDK
481
+ * @param {string} command - User prompt/command
482
+ * @param {Object} options - Query options
483
+ * @param {Object} ws - WebSocket connection
484
+ * @returns {Promise<void>}
485
+ */
486
+ async function queryClaudeSDK(command, options = {}, ws) {
487
+ const effectiveSessionId = options.apiKey ? null : (options.sessionId ?? null);
488
+ const { sessionSummary } = options;
489
+ let capturedSessionId = effectiveSessionId;
490
+ let sessionCreatedSent = false;
491
+ let tempImagePaths = [];
492
+ let tempDir = null;
493
+
494
+ const emitNotification = (event) => {
495
+ notifyUserIfEnabled({
496
+ userId: ws?.userId || null,
497
+ writer: ws,
498
+ event
499
+ });
500
+ };
501
+
502
+ try {
503
+ // Map CLI options to SDK format
504
+ const sdkOptions = mapCliOptionsToSDK(options);
505
+
506
+ // Load MCP configuration
507
+ const mcpServers = await loadMcpConfig(options.cwd);
508
+ if (mcpServers) {
509
+ sdkOptions.mcpServers = mcpServers;
510
+ }
511
+
512
+ // Handle images - save to temp files and modify prompt
513
+ const imageResult = await handleImages(command, options.images, options.cwd);
514
+ const finalCommand = imageResult.modifiedCommand;
515
+ tempImagePaths = imageResult.tempImagePaths;
516
+ tempDir = imageResult.tempDir;
517
+
518
+ sdkOptions.hooks = {
519
+ Notification: [{
520
+ matcher: '',
521
+ hooks: [async (input) => {
522
+ const message = typeof input?.message === 'string' ? input.message : 'Claude requires your attention.';
523
+ emitNotification(createNotificationEvent({
524
+ provider: 'claude',
525
+ sessionId: capturedSessionId || effectiveSessionId || null,
526
+ kind: 'action_required',
527
+ code: 'agent.notification',
528
+ meta: { message, sessionName: sessionSummary },
529
+ severity: 'warning',
530
+ requiresUserAction: true,
531
+ dedupeKey: `claude:hook:notification:${capturedSessionId || effectiveSessionId || 'none'}:${message}`
532
+ }));
533
+ return {};
534
+ }]
535
+ }]
536
+ };
537
+
538
+ sdkOptions.canUseTool = async (toolName, input, context) => {
539
+ const requiresInteraction = TOOLS_REQUIRING_INTERACTION.has(toolName);
540
+
541
+ if (!requiresInteraction) {
542
+ if (sdkOptions.permissionMode === 'bypassPermissions') {
543
+ return { behavior: 'allow', updatedInput: input };
544
+ }
545
+
546
+ const isDisallowed = (sdkOptions.disallowedTools || []).some(entry =>
547
+ matchesToolPermission(entry, toolName, input)
548
+ );
549
+ if (isDisallowed) {
550
+ return { behavior: 'deny', message: 'Tool disallowed by settings' };
551
+ }
552
+
553
+ const isAllowed = (sdkOptions.allowedTools || []).some(entry =>
554
+ matchesToolPermission(entry, toolName, input)
555
+ );
556
+ if (isAllowed) {
557
+ return { behavior: 'allow', updatedInput: input };
558
+ }
559
+ }
560
+
561
+ const requestId = createRequestId();
562
+ ws.send(createNormalizedMessage({ kind: 'permission_request', requestId, toolName, input, sessionId: capturedSessionId || effectiveSessionId || null, provider: 'claude' }));
563
+ emitNotification(createNotificationEvent({
564
+ provider: 'claude',
565
+ sessionId: capturedSessionId || effectiveSessionId || null,
566
+ kind: 'action_required',
567
+ code: 'permission.required',
568
+ meta: { toolName, sessionName: sessionSummary },
569
+ severity: 'warning',
570
+ requiresUserAction: true,
571
+ dedupeKey: `claude:permission:${capturedSessionId || effectiveSessionId || 'none'}:${requestId}`
572
+ }));
573
+
574
+ const decision = await waitForToolApproval(requestId, {
575
+ timeoutMs: requiresInteraction ? 0 : undefined,
576
+ signal: context?.signal,
577
+ metadata: {
578
+ _sessionId: capturedSessionId || sessionId || null,
579
+ _toolName: toolName,
580
+ _input: input,
581
+ _receivedAt: new Date(),
582
+ },
583
+ onCancel: (reason) => {
584
+ ws.send(createNormalizedMessage({ kind: 'permission_cancelled', requestId, reason, sessionId: capturedSessionId || effectiveSessionId || null, provider: 'claude' }));
585
+ }
586
+ });
587
+ if (!decision) {
588
+ return { behavior: 'deny', message: 'Permission request timed out' };
589
+ }
590
+
591
+ if (decision.cancelled) {
592
+ return { behavior: 'deny', message: 'Permission request cancelled' };
593
+ }
594
+
595
+ if (decision.allow) {
596
+ if (decision.rememberEntry && typeof decision.rememberEntry === 'string') {
597
+ if (!sdkOptions.allowedTools.includes(decision.rememberEntry)) {
598
+ sdkOptions.allowedTools.push(decision.rememberEntry);
599
+ }
600
+ if (Array.isArray(sdkOptions.disallowedTools)) {
601
+ sdkOptions.disallowedTools = sdkOptions.disallowedTools.filter(entry => entry !== decision.rememberEntry);
602
+ }
603
+ }
604
+ return { behavior: 'allow', updatedInput: decision.updatedInput ?? input };
605
+ }
606
+
607
+ return { behavior: 'deny', message: decision.message ?? 'User denied tool use' };
608
+ };
609
+
610
+ // Set stream-close timeout for interactive tools (Query constructor reads it synchronously). Claude Agent SDK has a default of 5s and this overrides it
611
+ const prevStreamTimeout = process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
612
+ process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = '300000';
613
+
614
+ let queryInstance;
615
+ try {
616
+ queryInstance = query({
617
+ prompt: finalCommand,
618
+ options: sdkOptions
619
+ });
620
+ } catch (hookError) {
621
+ // Older/newer SDK versions may not accept hook shapes yet.
622
+ // Keep notification behavior operational via runtime events even if hook registration fails.
623
+ console.warn('Failed to initialize Claude query with hooks, retrying without hooks:', hookError?.message || hookError);
624
+ delete sdkOptions.hooks;
625
+ queryInstance = query({
626
+ prompt: finalCommand,
627
+ options: sdkOptions
628
+ });
629
+ }
630
+
631
+ // Restore immediately — Query constructor already captured the value
632
+ if (prevStreamTimeout !== undefined) {
633
+ process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT = prevStreamTimeout;
634
+ } else {
635
+ delete process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT;
636
+ }
637
+
638
+ // Track the query instance for abort capability
639
+ if (capturedSessionId) {
640
+ addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
641
+ }
642
+
643
+ // Process streaming messages
644
+ console.log('Starting async generator loop for session:', capturedSessionId || 'NEW');
645
+ for await (const message of queryInstance) {
646
+ // Capture session ID from first message
647
+ if (message.session_id && !capturedSessionId) {
648
+
649
+ capturedSessionId = message.session_id;
650
+ addSession(capturedSessionId, queryInstance, tempImagePaths, tempDir, ws);
651
+
652
+ // Set session ID on writer
653
+ if (ws.setSessionId && typeof ws.setSessionId === 'function') {
654
+ ws.setSessionId(capturedSessionId);
655
+ }
656
+
657
+ // Send session-created event only once for new sessions
658
+ if (!effectiveSessionId && !sessionCreatedSent) {
659
+ sessionCreatedSent = true;
660
+ ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'claude' }));
661
+ }
662
+ } else {
663
+ // session_id already captured
664
+ }
665
+
666
+ // Transform and normalize message via adapter
667
+ const transformedMessage = transformMessage(message);
668
+ const sid = capturedSessionId || effectiveSessionId || null;
669
+
670
+ // Use adapter to normalize SDK events into NormalizedMessage[]
671
+ const normalized = claudeAdapter.normalizeMessage(transformedMessage, sid);
672
+ for (const msg of normalized) {
673
+ // Preserve parentToolUseId from SDK wrapper for subagent tool grouping
674
+ if (transformedMessage.parentToolUseId && !msg.parentToolUseId) {
675
+ msg.parentToolUseId = transformedMessage.parentToolUseId;
676
+ }
677
+ ws.send(msg);
678
+ }
679
+
680
+ // Extract and send token budget updates from result messages
681
+ if (message.type === 'result') {
682
+ const models = Object.keys(message.modelUsage || {});
683
+ if (models.length > 0) {
684
+ // Model info available in result message
685
+ }
686
+ const tokenBudgetData = extractTokenBudget(message);
687
+ if (tokenBudgetData) {
688
+ ws.send(createNormalizedMessage({ kind: 'status', text: 'token_budget', tokenBudget: tokenBudgetData, sessionId: capturedSessionId || effectiveSessionId || null, provider: 'claude' }));
689
+ }
690
+ }
691
+ }
692
+
693
+ // Clean up session on completion
694
+ if (capturedSessionId) {
695
+ removeSession(capturedSessionId);
696
+ }
697
+
698
+ // Clean up temporary image files
699
+ await cleanupTempFiles(tempImagePaths, tempDir);
700
+
701
+ // Send completion event
702
+ ws.send(createNormalizedMessage({ kind: 'complete', exitCode: 0, isNewSession: !effectiveSessionId && !!command, sessionId: capturedSessionId, provider: 'claude' }));
703
+ notifyRunStopped({
704
+ userId: ws?.userId || null,
705
+ provider: 'claude',
706
+ sessionId: capturedSessionId || sessionId || null,
707
+ sessionName: sessionSummary,
708
+ stopReason: 'completed'
709
+ });
710
+ // Complete
711
+
712
+ } catch (error) {
713
+ console.error('SDK query error:', error);
714
+
715
+ // Clean up session on error
716
+ if (capturedSessionId) {
717
+ removeSession(capturedSessionId);
718
+ }
719
+
720
+ // Clean up temporary image files on error
721
+ await cleanupTempFiles(tempImagePaths, tempDir);
722
+
723
+ // Send error to WebSocket
724
+ ws.send(createNormalizedMessage({ kind: 'error', content: error.message, sessionId: capturedSessionId || effectiveSessionId || null, provider: 'claude' }));
725
+ notifyRunFailed({
726
+ userId: ws?.userId || null,
727
+ provider: 'claude',
728
+ sessionId: capturedSessionId || sessionId || null,
729
+ sessionName: sessionSummary,
730
+ error
731
+ });
732
+
733
+ throw error;
734
+ }
735
+ }
736
+
737
+ /**
738
+ * Aborts an active SDK session
739
+ * @param {string} sessionId - Session identifier
740
+ * @returns {boolean} True if session was aborted, false if not found
741
+ */
742
+ async function abortClaudeSDKSession(sessionId) {
743
+ const session = getSession(sessionId);
744
+
745
+ if (!session) {
746
+ console.log(`Session ${sessionId} not found`);
747
+ return false;
748
+ }
749
+
750
+ try {
751
+ console.log(`Aborting SDK session: ${sessionId}`);
752
+
753
+ // Call interrupt() on the query instance
754
+ await session.instance.interrupt();
755
+
756
+ // Update session status
757
+ session.status = 'aborted';
758
+
759
+ // Clean up temporary image files
760
+ await cleanupTempFiles(session.tempImagePaths, session.tempDir);
761
+
762
+ // Clean up session
763
+ removeSession(sessionId);
764
+
765
+ return true;
766
+ } catch (error) {
767
+ console.error(`Error aborting session ${sessionId}:`, error);
768
+ return false;
769
+ }
770
+ }
771
+
772
+ /**
773
+ * Checks if an SDK session is currently active
774
+ * @param {string} sessionId - Session identifier
775
+ * @returns {boolean} True if session is active
776
+ */
777
+ function isClaudeSDKSessionActive(sessionId) {
778
+ const session = getSession(sessionId);
779
+ return session && session.status === 'active';
780
+ }
781
+
782
+ /**
783
+ * Gets all active SDK session IDs
784
+ * @returns {Array<string>} Array of active session IDs
785
+ */
786
+ function getActiveClaudeSDKSessions() {
787
+ return getAllSessions();
788
+ }
789
+
790
+ /**
791
+ * Get pending tool approvals for a specific session.
792
+ * @param {string} sessionId - The session ID
793
+ * @returns {Array} Array of pending permission request objects
794
+ */
795
+ function getPendingApprovalsForSession(sessionId) {
796
+ const pending = [];
797
+ for (const [requestId, resolver] of pendingToolApprovals.entries()) {
798
+ if (resolver._sessionId === sessionId) {
799
+ pending.push({
800
+ requestId,
801
+ toolName: resolver._toolName || 'UnknownTool',
802
+ input: resolver._input,
803
+ context: resolver._context,
804
+ sessionId,
805
+ receivedAt: resolver._receivedAt || new Date(),
806
+ });
807
+ }
808
+ }
809
+ return pending;
810
+ }
811
+
812
+ /**
813
+ * Reconnect a session's WebSocketWriter to a new raw WebSocket.
814
+ * Called when client reconnects (e.g. page refresh) while SDK is still running.
815
+ * @param {string} sessionId - The session ID
816
+ * @param {Object} newRawWs - The new raw WebSocket connection
817
+ * @returns {boolean} True if writer was successfully reconnected
818
+ */
819
+ function reconnectSessionWriter(sessionId, newRawWs) {
820
+ const session = getSession(sessionId);
821
+ if (!session?.writer?.updateWebSocket) return false;
822
+ session.writer.updateWebSocket(newRawWs);
823
+ console.log(`[RECONNECT] Writer swapped for session ${sessionId}`);
824
+ return true;
825
+ }
826
+
827
+ // Export public API
828
+ export {
829
+ queryClaudeSDK,
830
+ abortClaudeSDKSession,
831
+ isClaudeSDKSessionActive,
832
+ getActiveClaudeSDKSessions,
833
+ resolveToolApproval,
834
+ getPendingApprovalsForSession,
835
+ reconnectSessionWriter
836
+ };