@vibelet/cli 0.1.38 → 1.0.1
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.
- package/README.md +80 -0
- package/bin/cloudflared-quick-tunnel.mjs +11 -0
- package/bin/cloudflared-resolver.mjs +171 -0
- package/bin/vibelet-runtime-policy.mjs +36 -0
- package/bin/vibelet.cjs +12 -0
- package/bin/vibelet.mjs +1235 -0
- package/dist/index.cjs +126 -0
- package/package.json +24 -22
- package/app.json +0 -5
- package/dist/advertised-hosts.d.ts +0 -34
- package/dist/advertised-hosts.d.ts.map +0 -1
- package/dist/advertised-hosts.js +0 -176
- package/dist/advertised-hosts.js.map +0 -1
- package/dist/advertised-hosts.test.d.ts +0 -2
- package/dist/advertised-hosts.test.d.ts.map +0 -1
- package/dist/advertised-hosts.test.js +0 -96
- package/dist/advertised-hosts.test.js.map +0 -1
- package/dist/audit.d.ts +0 -30
- package/dist/audit.d.ts.map +0 -1
- package/dist/audit.js +0 -73
- package/dist/audit.js.map +0 -1
- package/dist/audit.test.d.ts +0 -2
- package/dist/audit.test.d.ts.map +0 -1
- package/dist/audit.test.js +0 -33
- package/dist/audit.test.js.map +0 -1
- package/dist/auth.d.ts +0 -6
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js +0 -27
- package/dist/auth.js.map +0 -1
- package/dist/claude-hooks.d.ts +0 -58
- package/dist/claude-hooks.d.ts.map +0 -1
- package/dist/claude-hooks.js +0 -129
- package/dist/claude-hooks.js.map +0 -1
- package/dist/cli-version.d.ts +0 -3
- package/dist/cli-version.d.ts.map +0 -1
- package/dist/cli-version.js +0 -35
- package/dist/cli-version.js.map +0 -1
- package/dist/cli-version.test.d.ts +0 -2
- package/dist/cli-version.test.d.ts.map +0 -1
- package/dist/cli-version.test.js +0 -38
- package/dist/cli-version.test.js.map +0 -1
- package/dist/config.d.ts +0 -30
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -327
- package/dist/config.js.map +0 -1
- package/dist/config.test.d.ts +0 -2
- package/dist/config.test.d.ts.map +0 -1
- package/dist/config.test.js +0 -184
- package/dist/config.test.js.map +0 -1
- package/dist/dev-auth.test.d.ts +0 -2
- package/dist/dev-auth.test.d.ts.map +0 -1
- package/dist/dev-auth.test.js +0 -154
- package/dist/dev-auth.test.js.map +0 -1
- package/dist/dev-script.test.d.ts +0 -2
- package/dist/dev-script.test.d.ts.map +0 -1
- package/dist/dev-script.test.js +0 -412
- package/dist/dev-script.test.js.map +0 -1
- package/dist/drivers/claude.d.ts +0 -34
- package/dist/drivers/claude.d.ts.map +0 -1
- package/dist/drivers/claude.js +0 -413
- package/dist/drivers/claude.js.map +0 -1
- package/dist/drivers/claude.test.d.ts +0 -2
- package/dist/drivers/claude.test.d.ts.map +0 -1
- package/dist/drivers/claude.test.js +0 -951
- package/dist/drivers/claude.test.js.map +0 -1
- package/dist/drivers/codex.d.ts +0 -38
- package/dist/drivers/codex.d.ts.map +0 -1
- package/dist/drivers/codex.js +0 -771
- package/dist/drivers/codex.js.map +0 -1
- package/dist/drivers/codex.test.d.ts +0 -2
- package/dist/drivers/codex.test.d.ts.map +0 -1
- package/dist/drivers/codex.test.js +0 -939
- package/dist/drivers/codex.test.js.map +0 -1
- package/dist/drivers/types.d.ts +0 -14
- package/dist/drivers/types.d.ts.map +0 -1
- package/dist/drivers/types.js +0 -2
- package/dist/drivers/types.js.map +0 -1
- package/dist/e2e.test.d.ts +0 -2
- package/dist/e2e.test.d.ts.map +0 -1
- package/dist/e2e.test.js +0 -111
- package/dist/e2e.test.js.map +0 -1
- package/dist/identity.d.ts +0 -10
- package/dist/identity.d.ts.map +0 -1
- package/dist/identity.js +0 -66
- package/dist/identity.js.map +0 -1
- package/dist/identity.test.d.ts +0 -2
- package/dist/identity.test.d.ts.map +0 -1
- package/dist/identity.test.js +0 -25
- package/dist/identity.test.js.map +0 -1
- package/dist/index-entry.test.d.ts +0 -2
- package/dist/index-entry.test.d.ts.map +0 -1
- package/dist/index-entry.test.js +0 -272
- package/dist/index-entry.test.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -707
- package/dist/index.js.map +0 -1
- package/dist/logger.d.ts +0 -31
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -75
- package/dist/logger.js.map +0 -1
- package/dist/metrics.d.ts +0 -52
- package/dist/metrics.d.ts.map +0 -1
- package/dist/metrics.js +0 -89
- package/dist/metrics.js.map +0 -1
- package/dist/pairing-store.d.ts +0 -29
- package/dist/pairing-store.d.ts.map +0 -1
- package/dist/pairing-store.js +0 -131
- package/dist/pairing-store.js.map +0 -1
- package/dist/pairing-store.test.d.ts +0 -2
- package/dist/pairing-store.test.d.ts.map +0 -1
- package/dist/pairing-store.test.js +0 -47
- package/dist/pairing-store.test.js.map +0 -1
- package/dist/paths.d.ts +0 -16
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js +0 -18
- package/dist/paths.js.map +0 -1
- package/dist/perf-compare.d.ts +0 -13
- package/dist/perf-compare.d.ts.map +0 -1
- package/dist/perf-compare.js +0 -125
- package/dist/perf-compare.js.map +0 -1
- package/dist/port-conflict.d.ts +0 -9
- package/dist/port-conflict.d.ts.map +0 -1
- package/dist/port-conflict.js +0 -33
- package/dist/port-conflict.js.map +0 -1
- package/dist/port-conflict.test.d.ts +0 -2
- package/dist/port-conflict.test.d.ts.map +0 -1
- package/dist/port-conflict.test.js +0 -38
- package/dist/port-conflict.test.js.map +0 -1
- package/dist/process-scanner.d.ts +0 -43
- package/dist/process-scanner.d.ts.map +0 -1
- package/dist/process-scanner.js +0 -453
- package/dist/process-scanner.js.map +0 -1
- package/dist/process-scanner.perf.test.d.ts +0 -2
- package/dist/process-scanner.perf.test.d.ts.map +0 -1
- package/dist/process-scanner.perf.test.js +0 -186
- package/dist/process-scanner.perf.test.js.map +0 -1
- package/dist/process-scanner.test.d.ts +0 -2
- package/dist/process-scanner.test.d.ts.map +0 -1
- package/dist/process-scanner.test.js +0 -399
- package/dist/process-scanner.test.js.map +0 -1
- package/dist/push-protocol.d.ts +0 -15
- package/dist/push-protocol.d.ts.map +0 -1
- package/dist/push-protocol.js +0 -23
- package/dist/push-protocol.js.map +0 -1
- package/dist/push-protocol.test.d.ts +0 -2
- package/dist/push-protocol.test.d.ts.map +0 -1
- package/dist/push-protocol.test.js +0 -57
- package/dist/push-protocol.test.js.map +0 -1
- package/dist/push-store.d.ts +0 -22
- package/dist/push-store.d.ts.map +0 -1
- package/dist/push-store.js +0 -103
- package/dist/push-store.js.map +0 -1
- package/dist/push-store.test.d.ts +0 -2
- package/dist/push-store.test.d.ts.map +0 -1
- package/dist/push-store.test.js +0 -79
- package/dist/push-store.test.js.map +0 -1
- package/dist/push.d.ts +0 -65
- package/dist/push.d.ts.map +0 -1
- package/dist/push.js +0 -202
- package/dist/push.js.map +0 -1
- package/dist/push.test.d.ts +0 -2
- package/dist/push.test.d.ts.map +0 -1
- package/dist/push.test.js +0 -199
- package/dist/push.test.js.map +0 -1
- package/dist/safe-stdio.d.ts +0 -3
- package/dist/safe-stdio.d.ts.map +0 -1
- package/dist/safe-stdio.js +0 -46
- package/dist/safe-stdio.js.map +0 -1
- package/dist/scanner.d.ts +0 -30
- package/dist/scanner.d.ts.map +0 -1
- package/dist/scanner.js +0 -859
- package/dist/scanner.js.map +0 -1
- package/dist/scanner.perf.test.d.ts +0 -2
- package/dist/scanner.perf.test.d.ts.map +0 -1
- package/dist/scanner.perf.test.js +0 -320
- package/dist/scanner.perf.test.js.map +0 -1
- package/dist/scanner.test.d.ts +0 -2
- package/dist/scanner.test.d.ts.map +0 -1
- package/dist/scanner.test.js +0 -948
- package/dist/scanner.test.js.map +0 -1
- package/dist/session-inventory.d.ts +0 -63
- package/dist/session-inventory.d.ts.map +0 -1
- package/dist/session-inventory.js +0 -525
- package/dist/session-inventory.js.map +0 -1
- package/dist/session-inventory.perf.test.d.ts +0 -2
- package/dist/session-inventory.perf.test.d.ts.map +0 -1
- package/dist/session-inventory.perf.test.js +0 -220
- package/dist/session-inventory.perf.test.js.map +0 -1
- package/dist/session-inventory.test.d.ts +0 -2
- package/dist/session-inventory.test.d.ts.map +0 -1
- package/dist/session-inventory.test.js +0 -712
- package/dist/session-inventory.test.js.map +0 -1
- package/dist/session-manager.d.ts +0 -75
- package/dist/session-manager.d.ts.map +0 -1
- package/dist/session-manager.js +0 -1515
- package/dist/session-manager.js.map +0 -1
- package/dist/session-manager.test.d.ts +0 -2
- package/dist/session-manager.test.d.ts.map +0 -1
- package/dist/session-manager.test.js +0 -2861
- package/dist/session-manager.test.js.map +0 -1
- package/dist/session-store.d.ts +0 -42
- package/dist/session-store.d.ts.map +0 -1
- package/dist/session-store.js +0 -163
- package/dist/session-store.js.map +0 -1
- package/dist/session-store.test.d.ts +0 -2
- package/dist/session-store.test.d.ts.map +0 -1
- package/dist/session-store.test.js +0 -236
- package/dist/session-store.test.js.map +0 -1
- package/dist/session-title.d.ts +0 -6
- package/dist/session-title.d.ts.map +0 -1
- package/dist/session-title.js +0 -105
- package/dist/session-title.js.map +0 -1
- package/dist/session-title.perf.test.d.ts +0 -2
- package/dist/session-title.perf.test.d.ts.map +0 -1
- package/dist/session-title.perf.test.js +0 -99
- package/dist/session-title.perf.test.js.map +0 -1
- package/dist/session-title.test.d.ts +0 -2
- package/dist/session-title.test.d.ts.map +0 -1
- package/dist/session-title.test.js +0 -199
- package/dist/session-title.test.js.map +0 -1
- package/dist/shutdown-endpoint.test.d.ts +0 -2
- package/dist/shutdown-endpoint.test.d.ts.map +0 -1
- package/dist/shutdown-endpoint.test.js +0 -93
- package/dist/shutdown-endpoint.test.js.map +0 -1
- package/dist/storage-housekeeping.d.ts +0 -28
- package/dist/storage-housekeeping.d.ts.map +0 -1
- package/dist/storage-housekeeping.js +0 -76
- package/dist/storage-housekeeping.js.map +0 -1
- package/dist/storage-housekeeping.test.d.ts +0 -2
- package/dist/storage-housekeeping.test.d.ts.map +0 -1
- package/dist/storage-housekeeping.test.js +0 -65
- package/dist/storage-housekeeping.test.js.map +0 -1
- package/dist/test-daemon-harness.d.ts +0 -31
- package/dist/test-daemon-harness.d.ts.map +0 -1
- package/dist/test-daemon-harness.js +0 -337
- package/dist/test-daemon-harness.js.map +0 -1
- package/dist/token-auth.test.d.ts +0 -2
- package/dist/token-auth.test.d.ts.map +0 -1
- package/dist/token-auth.test.js +0 -52
- package/dist/token-auth.test.js.map +0 -1
- package/dist/utils.d.ts +0 -4
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -40
- package/dist/utils.js.map +0 -1
- package/dist/utils.test.d.ts +0 -2
- package/dist/utils.test.d.ts.map +0 -1
- package/dist/utils.test.js +0 -54
- package/dist/utils.test.js.map +0 -1
- package/dist/ws-data.d.ts +0 -4
- package/dist/ws-data.d.ts.map +0 -1
- package/dist/ws-data.js +0 -20
- package/dist/ws-data.js.map +0 -1
- package/dist/ws-data.test.d.ts +0 -2
- package/dist/ws-data.test.d.ts.map +0 -1
- package/dist/ws-data.test.js +0 -17
- package/dist/ws-data.test.js.map +0 -1
- package/perf-reporter.mjs +0 -138
- package/scripts/build-release.mjs +0 -41
- package/scripts/dev.mjs +0 -537
- package/src/advertised-hosts.test.ts +0 -125
- package/src/advertised-hosts.ts +0 -225
- package/src/audit.test.ts +0 -38
- package/src/audit.ts +0 -117
- package/src/auth.ts +0 -31
- package/src/claude-hooks.ts +0 -195
- package/src/cli-version.test.ts +0 -36
- package/src/cli-version.ts +0 -46
- package/src/config.test.ts +0 -254
- package/src/config.ts +0 -324
- package/src/dev-auth.test.ts +0 -183
- package/src/dev-script.test.ts +0 -511
- package/src/drivers/claude.test.ts +0 -1186
- package/src/drivers/claude.ts +0 -443
- package/src/drivers/codex.test.ts +0 -1096
- package/src/drivers/codex.ts +0 -879
- package/src/drivers/types.ts +0 -15
- package/src/e2e.test.ts +0 -139
- package/src/identity.test.ts +0 -26
- package/src/identity.ts +0 -82
- package/src/index-entry.test.ts +0 -336
- package/src/index.ts +0 -781
- package/src/logger.ts +0 -112
- package/src/metrics.ts +0 -117
- package/src/pairing-store.test.ts +0 -53
- package/src/pairing-store.ts +0 -154
- package/src/paths.ts +0 -19
- package/src/perf-compare.ts +0 -164
- package/src/port-conflict.test.ts +0 -45
- package/src/port-conflict.ts +0 -44
- package/src/process-scanner.perf.test.ts +0 -222
- package/src/process-scanner.test.ts +0 -575
- package/src/process-scanner.ts +0 -514
- package/src/push-protocol.test.ts +0 -74
- package/src/push-protocol.ts +0 -36
- package/src/push-store.test.ts +0 -89
- package/src/push-store.ts +0 -126
- package/src/push.test.ts +0 -234
- package/src/push.ts +0 -318
- package/src/safe-stdio.ts +0 -51
- package/src/scanner.perf.test.ts +0 -359
- package/src/scanner.test.ts +0 -1045
- package/src/scanner.ts +0 -924
- package/src/session-inventory.perf.test.ts +0 -250
- package/src/session-inventory.test.ts +0 -1002
- package/src/session-inventory.ts +0 -721
- package/src/session-manager.test.ts +0 -3430
- package/src/session-manager.ts +0 -1775
- package/src/session-store.test.ts +0 -276
- package/src/session-store.ts +0 -202
- package/src/session-title.perf.test.ts +0 -118
- package/src/session-title.test.ts +0 -286
- package/src/session-title.ts +0 -108
- package/src/shutdown-endpoint.test.ts +0 -95
- package/src/storage-housekeeping.test.ts +0 -78
- package/src/storage-housekeeping.ts +0 -111
- package/src/test-daemon-harness.ts +0 -410
- package/src/token-auth.test.ts +0 -67
- package/src/utils.test.ts +0 -65
- package/src/utils.ts +0 -47
- package/src/ws-data.test.ts +0 -20
- package/src/ws-data.ts +0 -26
- package/tsconfig.json +0 -12
package/src/drivers/claude.ts
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
import { spawn, type ChildProcess } from 'child_process';
|
|
2
|
-
import { existsSync } from 'fs';
|
|
3
|
-
import type { Driver, MessageHandler } from './types.js';
|
|
4
|
-
import { config } from '../config.js';
|
|
5
|
-
import { logger as rootLogger } from '../logger.js';
|
|
6
|
-
import { audit } from '../audit.js';
|
|
7
|
-
import { cleanupClaudeHookFiles, createClaudeHookFiles, type ClaudeHookFiles } from '../claude-hooks.js';
|
|
8
|
-
import { writeStderrSafe } from '../safe-stdio.js';
|
|
9
|
-
|
|
10
|
-
const log = rootLogger.child({ module: 'claude' });
|
|
11
|
-
export const CLAUDE_SYNTHETIC_APPROVAL_PREFIX = 'claude-permission-denial:';
|
|
12
|
-
const ANSI_ESCAPE_PATTERN = /\u001B\[[0-?]*[ -/]*[@-~]/g;
|
|
13
|
-
|
|
14
|
-
function shouldLogRemote(): boolean {
|
|
15
|
-
return process.env.VIBE_TEST !== '1' && !process.argv.includes('--test');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function logRemote(message: string): void {
|
|
19
|
-
if (!shouldLogRemote()) return;
|
|
20
|
-
writeStderrSafe(`\x1b[33m⚡ [Vibelet] ${message}\x1b[0m\n`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function extractStructuredToolResultText(content: unknown): string {
|
|
24
|
-
if (!Array.isArray(content)) return '';
|
|
25
|
-
return content
|
|
26
|
-
.map((item) => {
|
|
27
|
-
if (!item || typeof item !== 'object') return '';
|
|
28
|
-
if (typeof (item as any).text === 'string') return (item as any).text;
|
|
29
|
-
if (typeof (item as any).content === 'string') return (item as any).content;
|
|
30
|
-
return '';
|
|
31
|
-
})
|
|
32
|
-
.filter(Boolean)
|
|
33
|
-
.join('\n');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function isToolReferenceOnlyContent(content: unknown): boolean {
|
|
37
|
-
return Array.isArray(content)
|
|
38
|
-
&& content.length > 0
|
|
39
|
-
&& content.every((item) => item && typeof item === 'object' && (item as any).type === 'tool_reference');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function stringifyToolResult(content: unknown): string {
|
|
43
|
-
if (typeof content === 'string') return content;
|
|
44
|
-
const extracted = extractStructuredToolResultText(content);
|
|
45
|
-
if (extracted) return extracted;
|
|
46
|
-
if (isToolReferenceOnlyContent(content)) return '';
|
|
47
|
-
return JSON.stringify(content);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function looksLikePermissionDenial(message: string): boolean {
|
|
51
|
-
return /requested permissions|haven't granted/i.test(message);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function sanitizeClaudeErrorText(text: string): string {
|
|
55
|
-
return text
|
|
56
|
-
.replace(ANSI_ESCAPE_PATTERN, '')
|
|
57
|
-
.replace(/\s+/g, ' ')
|
|
58
|
-
.trim();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function isGenericClaudeErrorText(text: string): boolean {
|
|
62
|
-
const normalized = text.toLowerCase();
|
|
63
|
-
return normalized === 'error' || normalized === 'failed' || normalized === 'unknown error';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function looksLikeClaudeUsageLimit(text: string): boolean {
|
|
67
|
-
return (
|
|
68
|
-
/\brate limit\b/i.test(text) ||
|
|
69
|
-
/\busage limit\b/i.test(text) ||
|
|
70
|
-
/\bquota\b/i.test(text) ||
|
|
71
|
-
/\btoo many requests\b/i.test(text) ||
|
|
72
|
-
/\bcredit balance\b/i.test(text) ||
|
|
73
|
-
/\bcredits? remaining\b/i.test(text) ||
|
|
74
|
-
/\bmax(?:imum)? usage\b/i.test(text)
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function formatClaudeUsageLimitMessage(detail: string): string {
|
|
79
|
-
return detail.toLowerCase().startsWith('claude ')
|
|
80
|
-
? detail
|
|
81
|
-
: `Claude usage limit reached. ${detail}`;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function formatClaudeFailureMessage(options: {
|
|
85
|
-
resultText?: string;
|
|
86
|
-
stderrLines?: string[];
|
|
87
|
-
exitCode?: number | null;
|
|
88
|
-
}): string {
|
|
89
|
-
const normalizedResult = sanitizeClaudeErrorText(options.resultText ?? '');
|
|
90
|
-
const stderrDetails = (options.stderrLines ?? [])
|
|
91
|
-
.map(sanitizeClaudeErrorText)
|
|
92
|
-
.filter(Boolean)
|
|
93
|
-
.slice(-3);
|
|
94
|
-
const limitDetail = [normalizedResult, ...stderrDetails].find(
|
|
95
|
-
(text) => Boolean(text) && looksLikeClaudeUsageLimit(text),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
if (limitDetail) {
|
|
99
|
-
return formatClaudeUsageLimitMessage(limitDetail);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const detail = [normalizedResult, ...stderrDetails].find(
|
|
103
|
-
(text) => Boolean(text) && !isGenericClaudeErrorText(text),
|
|
104
|
-
);
|
|
105
|
-
if (detail) {
|
|
106
|
-
if (options.exitCode != null && detail !== normalizedResult) {
|
|
107
|
-
return `Claude exited with code ${options.exitCode}: ${detail}`;
|
|
108
|
-
}
|
|
109
|
-
return detail;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (options.exitCode != null) {
|
|
113
|
-
return `Claude exited with code ${options.exitCode}`;
|
|
114
|
-
}
|
|
115
|
-
return 'Claude returned an error result.';
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function isClaudeSyntheticApprovalRequestId(requestId: string): boolean {
|
|
119
|
-
return requestId.startsWith(CLAUDE_SYNTHETIC_APPROVAL_PREFIX);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export class ClaudeDriver implements Driver {
|
|
123
|
-
private proc: ChildProcess | null = null;
|
|
124
|
-
private handler: MessageHandler | null = null;
|
|
125
|
-
private sessionId = '';
|
|
126
|
-
private buffer = '';
|
|
127
|
-
private cwd = '';
|
|
128
|
-
private approvalMode?: string;
|
|
129
|
-
private sawFinalResult = false;
|
|
130
|
-
private interrupted = false;
|
|
131
|
-
private exitHandler: ((code: number | null) => void) | null = null;
|
|
132
|
-
private lastStderr: string[] = [];
|
|
133
|
-
private pendingPermissionDescriptions = new Map<string, string>();
|
|
134
|
-
private emittedToolCallIds = new Set<string>();
|
|
135
|
-
/** True while the CLI is replaying old messages before the new response starts. */
|
|
136
|
-
private replayPhase = false;
|
|
137
|
-
private hookPort: number | null = null;
|
|
138
|
-
private hookSecret: string | null = null;
|
|
139
|
-
private hookFiles: ClaudeHookFiles | null = null;
|
|
140
|
-
|
|
141
|
-
private buildClaudeEnv(): Record<string, string> {
|
|
142
|
-
const env = config.buildSanitizedEnv();
|
|
143
|
-
for (const key of Object.keys(env)) {
|
|
144
|
-
if (key.startsWith('CMUX_')) {
|
|
145
|
-
delete env[key];
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return env;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async start(cwd: string, resumeSessionId?: string, approvalMode?: string): Promise<string> {
|
|
152
|
-
this.cwd = cwd;
|
|
153
|
-
this.approvalMode = approvalMode;
|
|
154
|
-
if (resumeSessionId) this.sessionId = resumeSessionId;
|
|
155
|
-
if (!this.sessionId) this.sessionId = `pending_${Date.now()}`;
|
|
156
|
-
log.info({ sessionId: this.sessionId, cwd }, 'session initialized');
|
|
157
|
-
audit.emit('driver.spawn', { agent: 'claude', sessionId: this.sessionId, cwd });
|
|
158
|
-
return this.sessionId;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
configureHookBridge(port: number, secret: string): void {
|
|
162
|
-
this.hookPort = port;
|
|
163
|
-
this.hookSecret = secret;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
sendPrompt(text: string): void {
|
|
167
|
-
const args = [
|
|
168
|
-
'-p', text,
|
|
169
|
-
'--output-format', 'stream-json',
|
|
170
|
-
'--verbose',
|
|
171
|
-
'--include-partial-messages',
|
|
172
|
-
];
|
|
173
|
-
|
|
174
|
-
if (this.sessionId && !this.sessionId.startsWith('pending_')) {
|
|
175
|
-
args.push('--resume', this.sessionId);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
cleanupClaudeHookFiles(this.hookFiles);
|
|
179
|
-
this.hookFiles = null;
|
|
180
|
-
|
|
181
|
-
if (this.approvalMode !== 'plan' && this.approvalMode !== 'acceptEdits' && this.approvalMode !== 'autoApprove' && this.hookPort && this.hookSecret) {
|
|
182
|
-
this.hookFiles = createClaudeHookFiles(this.hookPort, this.hookSecret);
|
|
183
|
-
args.push('--settings', this.hookFiles.settingsPath);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (this.approvalMode === 'plan') args.push('--permission-mode', 'plan');
|
|
187
|
-
if (this.approvalMode === 'acceptEdits') args.push('--permission-mode', 'acceptEdits');
|
|
188
|
-
if (this.approvalMode === 'autoApprove') args.push('--dangerously-skip-permissions');
|
|
189
|
-
|
|
190
|
-
const label = this.sessionId.startsWith('pending_') ? '(new)' : `(resume ${this.sessionId.slice(0, 8)})`;
|
|
191
|
-
log.info({ sessionId: this.sessionId, label, promptPreview: text.slice(0, 50) }, 'running claude');
|
|
192
|
-
logRemote( `New message: ${text.slice(0, 60)}`);
|
|
193
|
-
this.sawFinalResult = false;
|
|
194
|
-
this.interrupted = false;
|
|
195
|
-
this.lastStderr = [];
|
|
196
|
-
this.pendingPermissionDescriptions.clear();
|
|
197
|
-
this.emittedToolCallIds.clear();
|
|
198
|
-
this.replayPhase = true;
|
|
199
|
-
|
|
200
|
-
this.proc = spawn(config.claudePath, args, {
|
|
201
|
-
cwd: this.cwd || undefined,
|
|
202
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
203
|
-
env: this.buildClaudeEnv(),
|
|
204
|
-
...(process.platform === 'win32' ? { shell: true } : {}),
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
this.proc.on('error', (err) => {
|
|
208
|
-
let message = err.message;
|
|
209
|
-
if ((err as NodeJS.ErrnoException).code === 'ENOENT' && this.cwd && !existsSync(this.cwd)) {
|
|
210
|
-
message = `Working directory does not exist: ${this.cwd}`;
|
|
211
|
-
}
|
|
212
|
-
log.error({ sessionId: this.sessionId, error: message, cwd: this.cwd }, 'spawn error');
|
|
213
|
-
audit.emit('driver.error', { agent: 'claude', sessionId: this.sessionId, error: message });
|
|
214
|
-
this.handler?.({ type: 'error', sessionId: this.sessionId, message });
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
this.buffer = '';
|
|
218
|
-
this.proc.stdout!.on('data', (chunk: Buffer) => {
|
|
219
|
-
this.buffer += chunk.toString();
|
|
220
|
-
const lines = this.buffer.split('\n');
|
|
221
|
-
this.buffer = lines.pop()!;
|
|
222
|
-
for (const line of lines) {
|
|
223
|
-
if (!line.trim()) continue;
|
|
224
|
-
try {
|
|
225
|
-
this.handleRaw(JSON.parse(line));
|
|
226
|
-
} catch {
|
|
227
|
-
log.warn({ sessionId: this.sessionId, linePreview: line.slice(0, 100) }, 'failed to parse stdout line');
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
this.proc.stderr!.on('data', (chunk: Buffer) => {
|
|
233
|
-
const t = chunk.toString().trim();
|
|
234
|
-
if (t) {
|
|
235
|
-
log.debug({ sessionId: this.sessionId, stderr: t }, 'stderr');
|
|
236
|
-
this.lastStderr.push(t);
|
|
237
|
-
if (this.lastStderr.length > 10) this.lastStderr.shift();
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
this.proc.on('exit', (code, signal) => {
|
|
242
|
-
log.info({ sessionId: this.sessionId, exitCode: code, signal }, 'process exited');
|
|
243
|
-
const wasInterrupted = this.interrupted;
|
|
244
|
-
this.proc = null;
|
|
245
|
-
cleanupClaudeHookFiles(this.hookFiles);
|
|
246
|
-
this.hookFiles = null;
|
|
247
|
-
this.interrupted = false;
|
|
248
|
-
if (wasInterrupted && !this.sawFinalResult) {
|
|
249
|
-
this.handler?.({ type: 'session.interrupted', sessionId: this.sessionId });
|
|
250
|
-
} else if (code && code !== 0 && !this.sawFinalResult) {
|
|
251
|
-
log.error({ sessionId: this.sessionId, exitCode: code, lastStderr: this.lastStderr.slice(-3) }, 'abnormal exit');
|
|
252
|
-
this.handler?.({
|
|
253
|
-
type: 'error',
|
|
254
|
-
sessionId: this.sessionId,
|
|
255
|
-
message: formatClaudeFailureMessage({ stderrLines: this.lastStderr, exitCode: code }),
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
this.exitHandler?.(code);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
respondApproval(requestId: string, approved: boolean): boolean {
|
|
263
|
-
log.info({ sessionId: this.sessionId, requestId, approved }, 'approval response');
|
|
264
|
-
if (!this.proc?.stdin?.writable) {
|
|
265
|
-
log.error({ sessionId: this.sessionId }, 'cannot send approval: stdin not writable');
|
|
266
|
-
return false;
|
|
267
|
-
}
|
|
268
|
-
const response = JSON.stringify({
|
|
269
|
-
type: 'control_response',
|
|
270
|
-
request_id: requestId,
|
|
271
|
-
permission_granted: approved,
|
|
272
|
-
});
|
|
273
|
-
this.proc.stdin.write(response + '\n');
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
setApprovalMode(mode: string): void {
|
|
278
|
-
this.approvalMode = mode;
|
|
279
|
-
log.info({ sessionId: this.sessionId, approvalMode: mode }, 'approval mode updated');
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
interrupt(): void {
|
|
283
|
-
if (this.proc && !this.proc.killed) {
|
|
284
|
-
this.interrupted = true;
|
|
285
|
-
this.proc.kill('SIGTERM');
|
|
286
|
-
log.info({ sessionId: this.sessionId }, 'interrupted');
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
stop(): void {
|
|
291
|
-
if (!this.proc) return;
|
|
292
|
-
this.proc.kill('SIGTERM');
|
|
293
|
-
const p = this.proc;
|
|
294
|
-
setTimeout(() => { if (!p.killed) p.kill('SIGKILL'); }, 5000).unref();
|
|
295
|
-
this.proc = null;
|
|
296
|
-
cleanupClaudeHookFiles(this.hookFiles);
|
|
297
|
-
this.hookFiles = null;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
onMessage(handler: MessageHandler): void {
|
|
301
|
-
this.handler = handler;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
onExit(handler: (code: number | null) => void): void {
|
|
305
|
-
this.exitHandler = handler;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private handleRaw(raw: Record<string, unknown>): void {
|
|
309
|
-
const type = raw.type as string;
|
|
310
|
-
|
|
311
|
-
if (type === 'system' && (raw as any).subtype === 'init') {
|
|
312
|
-
const newId = (raw as any).session_id ?? '';
|
|
313
|
-
if (newId && newId !== this.sessionId) {
|
|
314
|
-
log.info({ oldSessionId: this.sessionId, newSessionId: newId }, 'session ID resolved');
|
|
315
|
-
audit.emit('driver.init', { agent: 'claude', sessionId: newId });
|
|
316
|
-
this.sessionId = newId;
|
|
317
|
-
}
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (!this.handler) return;
|
|
322
|
-
|
|
323
|
-
switch (type) {
|
|
324
|
-
case 'assistant': {
|
|
325
|
-
if (this.replayPhase) break; // skip replayed history from --include-partial-messages
|
|
326
|
-
const content = (raw as any).message?.content;
|
|
327
|
-
if (!Array.isArray(content)) break;
|
|
328
|
-
for (const block of content) {
|
|
329
|
-
if (block.type === 'tool_use' && !this.emittedToolCallIds.has(block.id)) {
|
|
330
|
-
this.emittedToolCallIds.add(block.id);
|
|
331
|
-
this.handler({
|
|
332
|
-
type: 'tool.call', sessionId: this.sessionId,
|
|
333
|
-
toolName: block.name, input: block.input ?? {}, toolCallId: block.id,
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
case 'user': {
|
|
341
|
-
if (this.replayPhase) break; // skip replayed history from --include-partial-messages
|
|
342
|
-
const content = (raw as any).message?.content;
|
|
343
|
-
if (!Array.isArray(content)) break;
|
|
344
|
-
for (const block of content) {
|
|
345
|
-
if (block.type === 'tool_result') {
|
|
346
|
-
const output = stringifyToolResult(block.content);
|
|
347
|
-
if (!output) continue;
|
|
348
|
-
if (block.is_error === true && typeof block.tool_use_id === 'string' && looksLikePermissionDenial(output)) {
|
|
349
|
-
this.pendingPermissionDescriptions.set(block.tool_use_id, output);
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
352
|
-
this.handler({
|
|
353
|
-
type: 'tool.result', sessionId: this.sessionId,
|
|
354
|
-
toolCallId: block.tool_use_id, output,
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
case 'control_request': {
|
|
362
|
-
this.replayPhase = false;
|
|
363
|
-
const req = (raw as any).request;
|
|
364
|
-
if (req?.subtype === 'can_use_tool') {
|
|
365
|
-
audit.emit('approval.request', { agent: 'claude', sessionId: this.sessionId, toolName: req.tool_name });
|
|
366
|
-
this.handler({
|
|
367
|
-
type: 'approval.request', sessionId: this.sessionId,
|
|
368
|
-
requestId: (raw as any).request_id,
|
|
369
|
-
toolName: req.tool_name ?? 'unknown',
|
|
370
|
-
input: req.input ?? {},
|
|
371
|
-
description: req.description ?? req.title ?? '',
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
case 'stream_event': {
|
|
378
|
-
this.replayPhase = false; // stream events only come from the new response
|
|
379
|
-
const event = (raw as any).event;
|
|
380
|
-
if (event?.type === 'content_block_delta' && event?.delta?.type === 'text_delta' && event?.delta?.text) {
|
|
381
|
-
this.handler({ type: 'text.delta', sessionId: this.sessionId, content: event.delta.text });
|
|
382
|
-
}
|
|
383
|
-
break;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
case 'result': {
|
|
387
|
-
this.replayPhase = false;
|
|
388
|
-
this.sawFinalResult = true;
|
|
389
|
-
const resultText = typeof (raw as any).result === 'string'
|
|
390
|
-
? (raw as any).result
|
|
391
|
-
: '';
|
|
392
|
-
if ((raw as any).is_error) {
|
|
393
|
-
logRemote( 'Claude failed.');
|
|
394
|
-
this.handler({
|
|
395
|
-
type: 'error',
|
|
396
|
-
sessionId: this.sessionId,
|
|
397
|
-
message: formatClaudeFailureMessage({ resultText, stderrLines: this.lastStderr }),
|
|
398
|
-
});
|
|
399
|
-
break;
|
|
400
|
-
}
|
|
401
|
-
const permissionDenials = Array.isArray((raw as any).permission_denials)
|
|
402
|
-
? (raw as any).permission_denials as Array<Record<string, unknown>>
|
|
403
|
-
: [];
|
|
404
|
-
if (permissionDenials.length > 0) {
|
|
405
|
-
const firstDenial = permissionDenials[0] ?? {};
|
|
406
|
-
const toolUseId = typeof firstDenial.tool_use_id === 'string'
|
|
407
|
-
? firstDenial.tool_use_id
|
|
408
|
-
: `missing_${Date.now()}`;
|
|
409
|
-
const toolName = typeof firstDenial.tool_name === 'string'
|
|
410
|
-
? firstDenial.tool_name
|
|
411
|
-
: 'unknown';
|
|
412
|
-
const input = firstDenial.tool_input && typeof firstDenial.tool_input === 'object'
|
|
413
|
-
? firstDenial.tool_input as Record<string, unknown>
|
|
414
|
-
: {};
|
|
415
|
-
const description = this.pendingPermissionDescriptions.get(toolUseId)
|
|
416
|
-
?? `Claude requested permissions to use ${toolName}.`;
|
|
417
|
-
audit.emit('approval.request', { agent: 'claude', sessionId: this.sessionId, toolName });
|
|
418
|
-
this.handler({
|
|
419
|
-
type: 'approval.request',
|
|
420
|
-
sessionId: this.sessionId,
|
|
421
|
-
requestId: `${CLAUDE_SYNTHETIC_APPROVAL_PREFIX}${toolUseId}`,
|
|
422
|
-
toolName,
|
|
423
|
-
input,
|
|
424
|
-
description,
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
this.pendingPermissionDescriptions.clear();
|
|
428
|
-
const cost = (raw as any).total_cost_usd;
|
|
429
|
-
const usage = (raw as any).usage
|
|
430
|
-
? { inputTokens: (raw as any).usage.input_tokens, outputTokens: (raw as any).usage.output_tokens }
|
|
431
|
-
: undefined;
|
|
432
|
-
logRemote( 'Claude finished. Run `claude --continue` to continue on desktop.');
|
|
433
|
-
audit.emit('session.done', { agent: 'claude', sessionId: this.sessionId, cost, usage });
|
|
434
|
-
this.handler({
|
|
435
|
-
type: 'session.done', sessionId: this.sessionId,
|
|
436
|
-
cost,
|
|
437
|
-
usage,
|
|
438
|
-
});
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|