locadex 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/fixErrors.d.ts +4 -0
- package/dist/commands/fixErrors.d.ts.map +1 -0
- package/dist/commands/fixErrors.js +41 -0
- package/dist/commands/fixErrors.js.map +1 -0
- package/dist/logging/logger.d.ts.map +1 -1
- package/dist/logging/logger.js.map +1 -1
- package/dist/mcp/getGuide.d.ts.map +1 -1
- package/dist/mcp/getGuide.js +2 -1
- package/dist/mcp/getGuide.js.map +1 -1
- package/dist/mcp/tools/guides.d.ts.map +1 -1
- package/dist/mcp/tools/guides.js +25 -55
- package/dist/mcp/tools/guides.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +4 -2
- package/dist/mcp.js.map +1 -1
- package/dist/tasks/concurrency.d.ts.map +1 -1
- package/dist/tasks/concurrency.js +15 -23
- package/dist/tasks/concurrency.js.map +1 -1
- package/dist/tasks/fixErrors.d.ts +2 -0
- package/dist/tasks/fixErrors.d.ts.map +1 -0
- package/dist/tasks/fixErrors.js +82 -0
- package/dist/tasks/fixErrors.js.map +1 -0
- package/dist/tasks/i18n.d.ts.map +1 -1
- package/dist/tasks/i18n.js +25 -82
- package/dist/tasks/i18n.js.map +1 -1
- package/dist/tasks/setup.d.ts.map +1 -1
- package/dist/tasks/setup.js +21 -9
- package/dist/tasks/setup.js.map +1 -1
- package/dist/types/claude-sdk.d.ts +13 -9
- package/dist/types/claude-sdk.d.ts.map +1 -1
- package/dist/types/claude-sdk.js.map +1 -1
- package/dist/utils/claudeCode.d.ts +13 -1
- package/dist/utils/claudeCode.d.ts.map +1 -1
- package/dist/utils/claudeCode.js +173 -66
- package/dist/utils/claudeCode.js.map +1 -1
- package/dist/utils/errors.d.ts +20 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +39 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/packages/installPackage.d.ts +1 -2
- package/dist/utils/packages/installPackage.d.ts.map +1 -1
- package/dist/utils/packages/installPackage.js +19 -28
- package/dist/utils/packages/installPackage.js.map +1 -1
- package/dist/utils/shared.d.ts +1 -1
- package/dist/utils/shared.js +1 -1
- package/dist/utils/shared.js.map +1 -1
- package/dist/utils/shutdown.d.ts +1 -2
- package/dist/utils/shutdown.d.ts.map +1 -1
- package/dist/utils/shutdown.js +1 -5
- package/dist/utils/shutdown.js.map +1 -1
- package/guides/next/advanced/{ternary-operators.md → conditional-rendering.md} +97 -39
- package/guides/next/advanced/external-strings.md +346 -0
- package/guides/next/advanced/interpolated-strings.md +35 -115
- package/guides/next/advanced/{complicated-mapping-expressions.md → mapping-expressions.md} +58 -51
- package/guides/next/basic/branches.md +62 -45
- package/guides/next/basic/jsx.md +35 -33
- package/guides/next/basic/strings.md +43 -25
- package/guides/next/basic/variables.md +12 -12
- package/guides/next/important/functions.md +13 -11
- package/guides/next/{advanced → migration}/migrating.md +2 -3
- package/package.json +1 -1
- package/guides/next/advanced/var-outside-client-component.md +0 -446
- package/guides/next/advanced/var-outside-client-server-component.md +0 -550
- package/guides/next/advanced/var-outside-server-component.md +0 -545
- package/guides/next/basic/client-side-components.md +0 -221
- package/guides/next/basic/server-side-components.md +0 -165
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-sdk.js","sourceRoot":"/","sources":["types/claude-sdk.ts"],"names":[],"mappings":"","sourcesContent":["import { type Message, type MessageParam } from '@anthropic-ai/sdk/resources';\
|
|
1
|
+
{"version":3,"file":"claude-sdk.js","sourceRoot":"/","sources":["types/claude-sdk.ts"],"names":[],"mappings":"","sourcesContent":["import { type Message, type MessageParam } from '@anthropic-ai/sdk/resources';\nexport type ClaudeSDKMessage =\n // An assistant message\n | {\n type: 'assistant';\n message: Message; // from Anthropic SDK\n session_id: string;\n }\n\n // A user message\n | {\n type: 'user';\n message: MessageParam; // from Anthropic SDK\n session_id: string;\n }\n\n // Emitted as the last message\n | {\n type: 'result';\n subtype: 'success';\n duration_ms: number;\n duration_api_ms: number;\n is_error: boolean;\n num_turns: number;\n result: string;\n session_id: string;\n total_cost_usd: number;\n }\n\n // Emitted as the last message, when we've reached the maximum number of turns\n | {\n type: 'result';\n subtype: 'error_max_turns' | 'error_during_execution';\n duration_ms: number;\n duration_api_ms: number;\n is_error: boolean;\n num_turns: number;\n session_id: string;\n total_cost_usd: number;\n }\n\n // Emitted as the first message at the start of a conversation\n | {\n type: 'system';\n subtype: 'init';\n apiKeySource: string;\n cwd: string;\n session_id: string;\n tools: string[];\n mcp_servers: {\n name: string;\n status: string;\n }[];\n model: string;\n permissionMode: 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan';\n };\n"]}
|
|
@@ -21,6 +21,7 @@ export declare class ClaudeCodeRunner {
|
|
|
21
21
|
private controller;
|
|
22
22
|
private softTurnLimit;
|
|
23
23
|
private turns;
|
|
24
|
+
private stats;
|
|
24
25
|
constructor(manager: LocadexManager, controller: AbortController, options: {
|
|
25
26
|
id: string;
|
|
26
27
|
apiKey: string;
|
|
@@ -29,7 +30,18 @@ export declare class ClaudeCodeRunner {
|
|
|
29
30
|
});
|
|
30
31
|
getSessionId(): string | undefined;
|
|
31
32
|
reset(): void;
|
|
32
|
-
|
|
33
|
+
resetStats(): void;
|
|
34
|
+
aggregateStats(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Wraps a promise with a timeout mechanism that can abort the underlying operation
|
|
37
|
+
*/
|
|
38
|
+
private withTimeout;
|
|
39
|
+
/**
|
|
40
|
+
* Retries an async operation with exponential backoff
|
|
41
|
+
* Retries on TimeoutError but not on UserAbortError
|
|
42
|
+
*/
|
|
43
|
+
private withRetry;
|
|
44
|
+
run(prompt: string, options: ClaudeRunOptions, _obs: ClaudeCodeObservation): Promise<string>;
|
|
33
45
|
private handleSDKOutput;
|
|
34
46
|
generateReport(): string;
|
|
35
47
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claudeCode.d.ts","sourceRoot":"/","sources":["utils/claudeCode.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"claudeCode.d.ts","sourceRoot":"/","sources":["utils/claudeCode.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAUrD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAGlB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,WAAW,qBAAqB;CAAG;AAazC,qBAAa,gBAAgB;IAwBzB,OAAO,CAAC,OAAO;IAvBjB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,KAAK,CAAa;IAE1B,OAAO,CAAC,KAAK,CASX;gBAGA,OAAO,EAAE,cAAc,EACvB,UAAU,EAAE,eAAe,EACnB,OAAO,EAAE;QACf,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;QAClB,aAAa,EAAE,MAAM,CAAC;KACvB;IA2BH,YAAY,IAAI,MAAM,GAAG,SAAS;IAGlC,KAAK;IAKL,UAAU;IAYV,cAAc;IAad;;OAEG;YACW,WAAW;IA2BzB;;;OAGG;YACW,SAAS;IAiDjB,GAAG,CACP,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,qBAAqB,GAC1B,OAAO,CAAC,MAAM,CAAC;IAiPlB,OAAO,CAAC,eAAe;IAuEvB,cAAc,IAAI,MAAM;CAGzB"}
|
package/dist/utils/claudeCode.js
CHANGED
|
@@ -4,6 +4,7 @@ import { logger } from '../logging/logger.js';
|
|
|
4
4
|
import { posthog } from '../telemetry.js';
|
|
5
5
|
import { getSessionId } from './session.js';
|
|
6
6
|
import * as Sentry from '@sentry/node';
|
|
7
|
+
import { TimeoutError, UserAbortError, AgentProcessError, AgentSpawnError, } from './errors.js';
|
|
7
8
|
const DEFAULT_ALLOWED_TOOLS = [
|
|
8
9
|
'mcp__locadex__fetch-docs',
|
|
9
10
|
'mcp__locadex__list-docs',
|
|
@@ -13,38 +14,6 @@ const DEFAULT_ALLOWED_TOOLS = [
|
|
|
13
14
|
'Write',
|
|
14
15
|
].concat(guides.map((guide) => `mcp__locadex__${guide.id}`));
|
|
15
16
|
const DISALLOWED_TOOLS = ['NotebookEdit', 'WebFetch', 'WebSearch'];
|
|
16
|
-
/**
|
|
17
|
-
* Wraps a promise with a timeout mechanism
|
|
18
|
-
*/
|
|
19
|
-
async function withTimeout(promise, timeoutSec, timeoutMessage) {
|
|
20
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
21
|
-
global.setTimeout(() => {
|
|
22
|
-
reject(new Error(timeoutMessage || `Operation timed out after ${timeoutSec}s`));
|
|
23
|
-
}, timeoutSec * 1000);
|
|
24
|
-
});
|
|
25
|
-
return Promise.race([promise, timeoutPromise]);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Retries an async operation with exponential backoff
|
|
29
|
-
*/
|
|
30
|
-
async function withRetry(operation, maxRetries = 1, baseDelayMs = 1000) {
|
|
31
|
-
let lastError;
|
|
32
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
33
|
-
try {
|
|
34
|
-
return await operation();
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
lastError = error;
|
|
38
|
-
if (attempt === maxRetries) {
|
|
39
|
-
throw lastError;
|
|
40
|
-
}
|
|
41
|
-
const delay = baseDelayMs * Math.pow(2, attempt);
|
|
42
|
-
logger.debugMessage(`Agent operation failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${error}`);
|
|
43
|
-
await new Promise((resolve) => global.setTimeout(resolve, delay));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
throw lastError;
|
|
47
|
-
}
|
|
48
17
|
export class ClaudeCodeRunner {
|
|
49
18
|
options;
|
|
50
19
|
id;
|
|
@@ -55,6 +24,7 @@ export class ClaudeCodeRunner {
|
|
|
55
24
|
controller;
|
|
56
25
|
softTurnLimit;
|
|
57
26
|
turns = 0;
|
|
27
|
+
stats;
|
|
58
28
|
constructor(manager, controller, options) {
|
|
59
29
|
this.options = options;
|
|
60
30
|
this.manager = manager;
|
|
@@ -62,6 +32,16 @@ export class ClaudeCodeRunner {
|
|
|
62
32
|
this.mcpConfig = options.mcpConfig;
|
|
63
33
|
this.controller = controller;
|
|
64
34
|
this.softTurnLimit = options.softTurnLimit;
|
|
35
|
+
this.stats = {
|
|
36
|
+
cost: 0,
|
|
37
|
+
wallDuration: 0,
|
|
38
|
+
apiDuration: 0,
|
|
39
|
+
turns: 0,
|
|
40
|
+
mcpToolCalls: 0,
|
|
41
|
+
inputTokens: 0,
|
|
42
|
+
outputTokens: 0,
|
|
43
|
+
cachedInputTokens: 0,
|
|
44
|
+
};
|
|
65
45
|
// Ensure API key is set
|
|
66
46
|
if (!process.env.ANTHROPIC_API_KEY && !this.options.apiKey) {
|
|
67
47
|
throw new Error('ANTHROPIC_API_KEY environment variable or apiKey option is required');
|
|
@@ -73,24 +53,110 @@ export class ClaudeCodeRunner {
|
|
|
73
53
|
reset() {
|
|
74
54
|
this.sessionId = undefined;
|
|
75
55
|
this.turns = 0;
|
|
56
|
+
this.resetStats();
|
|
57
|
+
}
|
|
58
|
+
resetStats() {
|
|
59
|
+
this.stats = {
|
|
60
|
+
cost: 0,
|
|
61
|
+
wallDuration: 0,
|
|
62
|
+
apiDuration: 0,
|
|
63
|
+
turns: 0,
|
|
64
|
+
mcpToolCalls: 0,
|
|
65
|
+
inputTokens: 0,
|
|
66
|
+
outputTokens: 0,
|
|
67
|
+
cachedInputTokens: 0,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
aggregateStats() {
|
|
71
|
+
this.manager.stats.updateStats({
|
|
72
|
+
newCost: this.stats.cost,
|
|
73
|
+
newWallDuration: this.stats.wallDuration,
|
|
74
|
+
newApiDuration: this.stats.apiDuration,
|
|
75
|
+
newTurns: this.stats.turns,
|
|
76
|
+
newToolCalls: this.stats.mcpToolCalls,
|
|
77
|
+
newInputTokens: this.stats.inputTokens,
|
|
78
|
+
newOutputTokens: this.stats.outputTokens,
|
|
79
|
+
newCachedInputTokens: this.stats.cachedInputTokens,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Wraps a promise with a timeout mechanism that can abort the underlying operation
|
|
84
|
+
*/
|
|
85
|
+
async withTimeout(promiseFactory, timeoutSec, timeoutMessage) {
|
|
86
|
+
const timeoutController = new AbortController();
|
|
87
|
+
let timeoutId;
|
|
88
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
89
|
+
timeoutId = global.setTimeout(() => {
|
|
90
|
+
timeoutController.abort();
|
|
91
|
+
reject(new TimeoutError(timeoutMessage || `Operation timed out after ${timeoutSec}s`, timeoutSec));
|
|
92
|
+
}, timeoutSec * 1000);
|
|
93
|
+
});
|
|
94
|
+
const promise = promiseFactory(timeoutController);
|
|
95
|
+
return Promise.race([promise, timeoutPromise]).finally(() => {
|
|
96
|
+
// Clear the timeout regardless of how the promise resolves
|
|
97
|
+
global.clearTimeout(timeoutId);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Retries an async operation with exponential backoff
|
|
102
|
+
* Retries on TimeoutError but not on UserAbortError
|
|
103
|
+
*/
|
|
104
|
+
async withRetry(operation, maxRetries = 1, baseDelayMs = 1000) {
|
|
105
|
+
let lastError;
|
|
106
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
107
|
+
try {
|
|
108
|
+
return await operation();
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
lastError = error;
|
|
112
|
+
// Don't retry on user aborts - these are intentional
|
|
113
|
+
if (lastError instanceof UserAbortError ||
|
|
114
|
+
lastError.name === 'AbortError') {
|
|
115
|
+
logger.debugMessage(`Claude Code operation aborted by user`);
|
|
116
|
+
throw lastError;
|
|
117
|
+
}
|
|
118
|
+
// Don't retry on the last attempt
|
|
119
|
+
if (attempt === maxRetries) {
|
|
120
|
+
throw lastError;
|
|
121
|
+
}
|
|
122
|
+
const delay = baseDelayMs * Math.pow(2, attempt);
|
|
123
|
+
// Log different messages for different error types
|
|
124
|
+
if (lastError instanceof TimeoutError) {
|
|
125
|
+
logger.debugMessage(`Claude Code operation timed out (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${lastError.message}`);
|
|
126
|
+
// reset the session id
|
|
127
|
+
this.reset();
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
logger.debugMessage(`Claude Code operation failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${lastError.message}`);
|
|
131
|
+
}
|
|
132
|
+
await new Promise((resolve) => global.setTimeout(resolve, delay));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw lastError;
|
|
76
136
|
}
|
|
77
|
-
async run(prompt, options,
|
|
137
|
+
async run(prompt, options, _obs) {
|
|
78
138
|
this.changes = [];
|
|
79
|
-
return withRetry(() => Sentry.startSpan({
|
|
139
|
+
return this.withRetry(() => Sentry.startSpan({
|
|
80
140
|
name: 'claude-code-exec',
|
|
81
141
|
op: 'claude-code.exec',
|
|
82
142
|
attributes: {
|
|
83
143
|
'process.command': 'claude',
|
|
84
144
|
},
|
|
85
|
-
}, () => withTimeout(new Promise((resolve, reject) => {
|
|
145
|
+
}, () => this.withTimeout((timeoutController) => new Promise((resolve, reject) => {
|
|
86
146
|
const args = ['-p', prompt];
|
|
87
147
|
if (options.additionalSystemPrompt) {
|
|
88
148
|
args.push('--append-system-prompt', options.additionalSystemPrompt);
|
|
89
149
|
}
|
|
90
150
|
args.push('--output-format', 'stream-json');
|
|
91
151
|
args.push('--verbose');
|
|
92
|
-
if (this.sessionId
|
|
93
|
-
|
|
152
|
+
if (this.sessionId) {
|
|
153
|
+
if (this.turns < this.softTurnLimit) {
|
|
154
|
+
args.push('--resume', this.sessionId);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
logger.debugMessage(`[${this.id}] Resetting session id because of soft turn limit reached: ${this.turns} >= ${this.softTurnLimit}`);
|
|
158
|
+
this.reset();
|
|
159
|
+
}
|
|
94
160
|
}
|
|
95
161
|
if (this.mcpConfig) {
|
|
96
162
|
args.push('--mcp-config', this.mcpConfig);
|
|
@@ -114,14 +180,22 @@ export class ClaudeCodeRunner {
|
|
|
114
180
|
mcpConfig: this.mcpConfig,
|
|
115
181
|
additionalAllowedTools: options.additionalAllowedTools,
|
|
116
182
|
}, null, 2)}. API key is ${this.options.apiKey ? 'set' : 'not set'}`);
|
|
183
|
+
// Create a combined abort controller that triggers on either user abort or timeout
|
|
184
|
+
const combinedController = new AbortController();
|
|
185
|
+
const abortHandler = () => {
|
|
186
|
+
combinedController.abort();
|
|
187
|
+
};
|
|
188
|
+
this.controller.signal.addEventListener('abort', abortHandler);
|
|
189
|
+
timeoutController.signal.addEventListener('abort', abortHandler);
|
|
117
190
|
const claude = spawn('claude', args, {
|
|
118
191
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
119
192
|
env,
|
|
120
|
-
signal:
|
|
193
|
+
signal: combinedController.signal,
|
|
121
194
|
});
|
|
122
195
|
logger.debugMessage(`[${this.id}] Spawned claude code process`);
|
|
123
|
-
const output =
|
|
124
|
-
|
|
196
|
+
const output = {
|
|
197
|
+
error: '',
|
|
198
|
+
};
|
|
125
199
|
let buffer = '';
|
|
126
200
|
claude.stdout?.on('data', (data) => {
|
|
127
201
|
buffer += data.toString();
|
|
@@ -133,7 +207,10 @@ export class ClaudeCodeRunner {
|
|
|
133
207
|
try {
|
|
134
208
|
logger.debugMessage(`[${this.id}] ${line}`);
|
|
135
209
|
const outputData = JSON.parse(line);
|
|
136
|
-
this.handleSDKOutput(outputData,
|
|
210
|
+
const result = this.handleSDKOutput(outputData, _obs);
|
|
211
|
+
if (!result.success) {
|
|
212
|
+
output.error = result.error ?? '';
|
|
213
|
+
}
|
|
137
214
|
}
|
|
138
215
|
catch (error) {
|
|
139
216
|
logger.debugMessage(`[${this.id}] Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -144,29 +221,55 @@ export class ClaudeCodeRunner {
|
|
|
144
221
|
claude.stderr?.on('data', (data) => {
|
|
145
222
|
logger.debugMessage(`[${this.id}] ${data.toString().trim()}`);
|
|
146
223
|
});
|
|
147
|
-
claude.on('close', (code) => {
|
|
148
|
-
|
|
149
|
-
|
|
224
|
+
claude.on('close', (code, signal) => {
|
|
225
|
+
// Clean up event listeners
|
|
226
|
+
this.controller.signal.removeEventListener('abort', abortHandler);
|
|
227
|
+
timeoutController.signal.removeEventListener('abort', abortHandler);
|
|
228
|
+
if (signal === 'SIGTERM' || signal === 'SIGKILL') {
|
|
229
|
+
// Process was terminated due to abort
|
|
230
|
+
if (this.controller.signal.aborted) {
|
|
231
|
+
reject(new UserAbortError('Claude Code process was aborted by user'));
|
|
232
|
+
}
|
|
233
|
+
else if (timeoutController.signal.aborted) {
|
|
234
|
+
reject(new TimeoutError(`Claude Code process timed out after ${options.timeoutSec}s`, options.timeoutSec));
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
reject(new AgentProcessError(`[${this.id}] Claude Code process was terminated with signal ${signal}`, code ?? undefined));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else if (code === 0) {
|
|
241
|
+
logger.debugMessage(`[${this.id}] Claude Code exited with code ${code}`);
|
|
242
|
+
resolve('');
|
|
150
243
|
}
|
|
151
244
|
else {
|
|
152
|
-
logger.debugMessage(`[${this.id}] Claude Code exited with code ${code}: ${
|
|
153
|
-
reject(new
|
|
245
|
+
logger.debugMessage(`[${this.id}] Claude Code exited with code ${code}: ${output.error}`);
|
|
246
|
+
reject(new AgentProcessError(`[${this.id}] Claude Code exited with code ${code}: ${output.error}`, code ?? undefined));
|
|
154
247
|
}
|
|
155
248
|
});
|
|
156
249
|
claude.on('error', (error) => {
|
|
157
|
-
//
|
|
250
|
+
// Clean up event listeners
|
|
251
|
+
this.controller.signal.removeEventListener('abort', abortHandler);
|
|
252
|
+
timeoutController.signal.removeEventListener('abort', abortHandler);
|
|
158
253
|
if (error.name === 'AbortError') {
|
|
159
|
-
|
|
160
|
-
|
|
254
|
+
// Determine if this was a user abort or timeout abort
|
|
255
|
+
if (this.controller.signal.aborted) {
|
|
256
|
+
reject(new UserAbortError('Claude Code process was aborted by user'));
|
|
257
|
+
}
|
|
258
|
+
else if (timeoutController.signal.aborted) {
|
|
259
|
+
reject(new TimeoutError(`Claude Code process timed out after ${options.timeoutSec}s`, options.timeoutSec));
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
reject(new UserAbortError('Claude Code process was aborted'));
|
|
263
|
+
}
|
|
161
264
|
}
|
|
162
265
|
else {
|
|
163
266
|
logger.debugMessage(`[${this.id}] failed to run Claude Code: ${error.message}`);
|
|
164
|
-
reject(new
|
|
267
|
+
reject(new AgentSpawnError(`[${this.id}] failed to run Claude Code: ${error.message}`, error));
|
|
165
268
|
}
|
|
166
269
|
});
|
|
167
270
|
}), options.timeoutSec, `Claude Code operation timed out after ${options.timeoutSec}s`)), options.maxRetries);
|
|
168
271
|
}
|
|
169
|
-
handleSDKOutput(outputData,
|
|
272
|
+
handleSDKOutput(outputData, _obs) {
|
|
170
273
|
if (outputData.type === 'assistant') {
|
|
171
274
|
const text = [];
|
|
172
275
|
const toolUses = [];
|
|
@@ -193,36 +296,40 @@ export class ClaudeCodeRunner {
|
|
|
193
296
|
if (toolUses.length > 0) {
|
|
194
297
|
logger.debugMessage(`[${this.id}] used tools: ${toolUses.join(', ')}`);
|
|
195
298
|
}
|
|
196
|
-
this.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
});
|
|
299
|
+
this.stats.mcpToolCalls += toolUses.length;
|
|
300
|
+
this.stats.inputTokens += outputData.message.usage.input_tokens;
|
|
301
|
+
this.stats.outputTokens += outputData.message.usage.output_tokens;
|
|
302
|
+
this.stats.cachedInputTokens +=
|
|
303
|
+
outputData.message.usage.cache_read_input_tokens ?? 0;
|
|
202
304
|
}
|
|
203
305
|
else if (outputData.type === 'result') {
|
|
306
|
+
this.stats.cost = outputData.total_cost_usd;
|
|
307
|
+
this.stats.wallDuration = outputData.duration_ms;
|
|
308
|
+
this.stats.apiDuration = outputData.duration_api_ms;
|
|
309
|
+
this.stats.turns = outputData.num_turns;
|
|
310
|
+
this.turns = outputData.num_turns;
|
|
204
311
|
if (!outputData.is_error) {
|
|
205
|
-
logger.verboseMessage(`[${this.id}] finished task.\nCost: $${
|
|
312
|
+
logger.verboseMessage(`[${this.id}] finished task.\nCost: $${outputData.total_cost_usd.toFixed(2)}\nDuration: ${outputData.duration_ms / 1000}s`);
|
|
206
313
|
}
|
|
207
314
|
else {
|
|
208
|
-
logger.verboseMessage(`[${this.id}] finished task with error: ${outputData.subtype}\nCost: $${outputData.
|
|
315
|
+
logger.verboseMessage(`[${this.id}] finished task with error: ${outputData.subtype}\nCost: $${outputData.total_cost_usd}\nDuration: ${outputData.duration_ms / 1000}s`);
|
|
316
|
+
return {
|
|
317
|
+
success: false,
|
|
318
|
+
error: outputData.subtype,
|
|
319
|
+
};
|
|
209
320
|
}
|
|
210
321
|
if (outputData.subtype === 'success') {
|
|
211
322
|
this.changes.push(outputData.result);
|
|
212
323
|
}
|
|
213
|
-
this.manager.stats.updateStats({
|
|
214
|
-
newCost: Number(outputData.cost_usd),
|
|
215
|
-
newWallDuration: Number(outputData.duration_ms),
|
|
216
|
-
newApiDuration: Number(outputData.duration_api_ms),
|
|
217
|
-
newTurns: Number(outputData.num_turns),
|
|
218
|
-
});
|
|
219
|
-
this.turns = Number(outputData.num_turns);
|
|
220
324
|
}
|
|
221
325
|
else if (outputData.type === 'system') {
|
|
222
326
|
if (outputData.subtype === 'init') {
|
|
223
327
|
this.sessionId = outputData.session_id;
|
|
224
328
|
}
|
|
225
329
|
}
|
|
330
|
+
return {
|
|
331
|
+
success: true,
|
|
332
|
+
};
|
|
226
333
|
}
|
|
227
334
|
generateReport() {
|
|
228
335
|
return this.changes.join('\n');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claudeCode.js","sourceRoot":"/","sources":["utils/claudeCode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AAkBvC,MAAM,qBAAqB,GAAG;IAC5B,0BAA0B;IAC1B,yBAAyB;IACzB,MAAM;IACN,MAAM;IACN,WAAW;IACX,OAAO;CACR,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iBAAi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AQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,eAC3E,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,IACnC,GAAG,CACJ,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,cAAc,CACnB,IAAI,IAAI,CAAC,EAAE,+BAA+B,UAAU,CAAC,OAAO,YAAY,UAAU,CAAC,QAAQ,eACzF,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,IACnC,GAAG,CACJ,CAAC;YACJ,CAAC;YACD,IAAI,UAAU,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;gBACrC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YACvC,CAAC;YACD,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC;gBAC7B,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC;gBACpC,eAAe,EAAE,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC;gBAC/C,cAAc,EAAE,MAAM,CAAC,UAAU,CAAC,eAAe,CAAC;gBAClD,QAAQ,EAAE,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC;aACvC,CAAC,CAAC;YACH,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;QAC5C,CAAC;aAAM,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACxC,IAAI,UAAU,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;gBAClC,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,UAAU,CAAC;YACzC,CAAC;QACH,CAAC;IACH,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;CACF","sourcesContent":["import { spawn } from 'node:child_process';\nimport { ClaudeSDKMessage } from '../types/claude-sdk.js';\nimport { guides } from '../mcp/tools/guides.js';\nimport { logger } from '../logging/logger.js';\nimport { posthog } from '../telemetry.js';\nimport { LocadexManager } from './locadexManager.js';\nimport { getSessionId } from './session.js';\nimport * as Sentry from '@sentry/node';\n\nexport type ClaudeRunOptions = {\n additionalSystemPrompt?: string;\n additionalAllowedTools?: string[];\n maxTurns?: number; // Hard limit on the number of turns per Claude Code call\n\n // required\n timeoutSec: number; // Timeout per .run() call\n maxRetries: number; // Max number of retries per .run() call\n};\n\nexport type ClaudeRunnerOptions = {\n softTurnLimit?: number; // Soft limit on the number of turns per Claude runner\n};\n\nexport interface ClaudeCodeObservation {}\n\nconst DEFAULT_ALLOWED_TOOLS = [\n 'mcp__locadex__fetch-docs',\n 'mcp__locadex__list-docs',\n 'Bash',\n 'Edit',\n 'MultiEdit',\n 'Write',\n].concat(guides.map((guide) => `mcp__locadex__${guide.id}`));\n\nconst DISALLOWED_TOOLS = ['NotebookEdit', 'WebFetch', 'WebSearch'];\n\n/**\n * Wraps a promise with a timeout mechanism\n */\nasync function withTimeout<T>(\n promise: Promise<T>,\n timeoutSec: number,\n timeoutMessage?: string\n): Promise<T> {\n const timeoutPromise = new Promise<never>((_, reject) => {\n global.setTimeout(() => {\n reject(\n new Error(timeoutMessage || `Operation timed out after ${timeoutSec}s`)\n );\n }, timeoutSec * 1000);\n });\n\n return Promise.race([promise, timeoutPromise]);\n}\n\n/**\n * Retries an async operation with exponential backoff\n */\nasync function withRetry<T>(\n operation: () => Promise<T>,\n maxRetries: number = 1,\n baseDelayMs: number = 1000\n): Promise<T> {\n let lastError: Error;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n return await operation();\n } catch (error) {\n lastError = error as Error;\n\n if (attempt === maxRetries) {\n throw lastError;\n }\n\n const delay = baseDelayMs * Math.pow(2, attempt);\n logger.debugMessage(\n `Agent operation failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${error}`\n );\n await new Promise((resolve) => global.setTimeout(resolve, delay));\n }\n }\n\n throw lastError!;\n}\n\nexport class ClaudeCodeRunner {\n private id: string;\n private sessionId: string | undefined;\n private mcpConfig: string | undefined;\n private manager: LocadexManager;\n private changes: string[] = [];\n private controller: AbortController;\n private softTurnLimit: number;\n private turns: number = 0;\n\n constructor(\n manager: LocadexManager,\n controller: AbortController,\n private options: {\n id: string;\n apiKey: string;\n mcpConfig: string;\n softTurnLimit: number;\n }\n ) {\n this.manager = manager;\n this.id = options.id;\n this.mcpConfig = options.mcpConfig;\n this.controller = controller;\n this.softTurnLimit = options.softTurnLimit;\n\n // Ensure API key is set\n if (!process.env.ANTHROPIC_API_KEY && !this.options.apiKey) {\n throw new Error(\n 'ANTHROPIC_API_KEY environment variable or apiKey option is required'\n );\n }\n }\n\n getSessionId(): string | undefined {\n return this.sessionId;\n }\n reset() {\n this.sessionId = undefined;\n this.turns = 0;\n }\n\n async run(\n prompt: string,\n options: ClaudeRunOptions,\n obs: ClaudeCodeObservation\n ): Promise<string> {\n this.changes = [];\n return withRetry(\n () =>\n Sentry.startSpan(\n {\n name: 'claude-code-exec',\n op: 'claude-code.exec',\n attributes: {\n 'process.command': 'claude',\n },\n },\n () =>\n withTimeout(\n new Promise((resolve, reject) => {\n const args = ['-p', prompt];\n\n if (options.additionalSystemPrompt) {\n args.push(\n '--append-system-prompt',\n options.additionalSystemPrompt\n );\n }\n\n args.push('--output-format', 'stream-json');\n args.push('--verbose');\n\n if (this.sessionId && this.turns < this.softTurnLimit) {\n args.push('--resume', this.sessionId);\n }\n\n if (this.mcpConfig) {\n args.push('--mcp-config', this.mcpConfig);\n }\n\n args.push(\n '--allowedTools',\n [\n ...DEFAULT_ALLOWED_TOOLS,\n ...(options?.additionalAllowedTools || []),\n ].join(',')\n );\n\n args.push('--disallowedTools', DISALLOWED_TOOLS.join(','));\n\n if (options.maxTurns) {\n args.push('--max-turns', options.maxTurns.toString());\n }\n\n const env = { ...process.env };\n env.ANTHROPIC_API_KEY = this.options.apiKey;\n logger.debugMessage(\n `[${this.id}] Spawning Claude Code with additional args: ${JSON.stringify(\n {\n maxTurns: options.maxTurns,\n softTurnLimit: this.softTurnLimit,\n timeoutSec: options.timeoutSec,\n maxRetries: options.maxRetries,\n sessionId: this.sessionId,\n mcpConfig: this.mcpConfig,\n additionalAllowedTools: options.additionalAllowedTools,\n },\n null,\n 2\n )}. API key is ${this.options.apiKey ? 'set' : 'not set'}`\n );\n\n const claude = spawn('claude', args, {\n stdio: ['ignore', 'pipe', 'pipe'],\n env,\n signal: this.controller.signal,\n });\n\n logger.debugMessage(`[${this.id}] Spawned claude code process`);\n\n const output = '';\n const errorOutput = '';\n\n let buffer = '';\n claude.stdout?.on('data', (data) => {\n buffer += data.toString();\n const lines = buffer.split('\\n');\n\n // Keep the last incomplete line in buffer\n buffer = lines.pop() || '';\n\n for (const line of lines) {\n if (line.trim()) {\n try {\n logger.debugMessage(`[${this.id}] ${line}`);\n const outputData: ClaudeSDKMessage = JSON.parse(line);\n this.handleSDKOutput(outputData, obs);\n } catch (error) {\n logger.debugMessage(\n `[${this.id}] Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n }\n }\n });\n\n claude.stderr?.on('data', (data) => {\n logger.debugMessage(`[${this.id}] ${data.toString().trim()}`);\n });\n\n claude.on('close', (code) => {\n if (code === 0) {\n resolve(output.trim());\n } else {\n logger.debugMessage(\n `[${this.id}] Claude Code exited with code ${code}: ${errorOutput}`\n );\n reject(\n new Error(\n `[${this.id}] Claude Code exited with code ${code}: ${errorOutput}`\n )\n );\n }\n });\n\n claude.on('error', (error) => {\n // Check if this is an AbortError\n if (error.name === 'AbortError') {\n logger.debugMessage(\n `[${this.id}] Claude Code process was aborted`\n );\n reject(\n new Error(`[${this.id}] Claude Code process was aborted`)\n );\n } else {\n logger.debugMessage(\n `[${this.id}] failed to run Claude Code: ${error.message}`\n );\n reject(\n new Error(\n `[${this.id}] failed to run Claude Code: ${error.message}`\n )\n );\n }\n });\n }),\n options.timeoutSec,\n `Claude Code operation timed out after ${options.timeoutSec}s`\n )\n ),\n options.maxRetries\n );\n }\n\n private handleSDKOutput(\n outputData: ClaudeSDKMessage,\n obs: ClaudeCodeObservation\n ) {\n if (outputData.type === 'assistant') {\n const text: string[] = [];\n const toolUses: string[] = [];\n outputData.message.content.forEach((c) => {\n if (c.type === 'text') {\n text.push(c.text);\n }\n if (c.type === 'tool_use') {\n toolUses.push(c.name);\n if (c.name.startsWith('mcp__locadex__')) {\n posthog.capture({\n distinctId: getSessionId(),\n event: 'tool_used',\n properties: {\n tool: c.name,\n },\n });\n }\n }\n });\n if (text.length > 0) {\n logger.verboseMessage(`[${this.id}] ${text.join('').trim()}`);\n }\n if (toolUses.length > 0) {\n logger.debugMessage(`[${this.id}] used tools: ${toolUses.join(', ')}`);\n }\n this.manager.stats.updateStats({\n newToolCalls: toolUses.length,\n newInputTokens: outputData.message.usage.input_tokens,\n newOutputTokens: outputData.message.usage.output_tokens,\n newCachedInputTokens:\n outputData.message.usage.cache_read_input_tokens ?? 0,\n });\n } else if (outputData.type === 'result') {\n if (!outputData.is_error) {\n logger.verboseMessage(\n `[${this.id}] finished task.\\nCost: $${Number(outputData.cost_usd).toFixed(2)}\\nDuration: ${\n Number(outputData.duration_ms) / 1000\n }s`\n );\n } else {\n logger.verboseMessage(\n `[${this.id}] finished task with error: ${outputData.subtype}\\nCost: $${outputData.cost_usd}\\nDuration: ${\n Number(outputData.duration_ms) / 1000\n }s`\n );\n }\n if (outputData.subtype === 'success') {\n this.changes.push(outputData.result);\n }\n this.manager.stats.updateStats({\n newCost: Number(outputData.cost_usd),\n newWallDuration: Number(outputData.duration_ms),\n newApiDuration: Number(outputData.duration_api_ms),\n newTurns: Number(outputData.num_turns),\n });\n this.turns = Number(outputData.num_turns);\n } else if (outputData.type === 'system') {\n if (outputData.subtype === 'init') {\n this.sessionId = outputData.session_id;\n }\n }\n }\n\n generateReport(): string {\n return this.changes.join('\\n');\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"claudeCode.js","sourceRoot":"/","sources":["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;iBAAM,CAAC;gBACN,MAAM,CAAC,cAAc,CACnB,IAAI,IAAI,CAAC,EAAE,+BAA+B,UAAU,CAAC,OAAO,YAAY,UAAU,CAAC,cAAc,eAC/F,UAAU,CAAC,WAAW,GAAG,IAC3B,GAAG,CACJ,CAAC;gBACF,OAAO;oBACL,OAAO,EAAE,KAAK;oBACd,KAAK,EAAE,UAAU,CAAC,OAAO;iBAC1B,CAAC;YACJ,CAAC;YACD,IAAI,UAAU,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;gBACrC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;aAAM,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACxC,IAAI,UAAU,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;gBAClC,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,UAAU,CAAC;YACzC,CAAC;QACH,CAAC;QACD,OAAO;YACL,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;CACF","sourcesContent":["import { spawn } from 'node:child_process';\nimport { ClaudeSDKMessage } from '../types/claude-sdk.js';\nimport { guides } from '../mcp/tools/guides.js';\nimport { logger } from '../logging/logger.js';\nimport { posthog } from '../telemetry.js';\nimport { LocadexManager } from './locadexManager.js';\nimport { getSessionId } from './session.js';\nimport * as Sentry from '@sentry/node';\nimport {\n TimeoutError,\n UserAbortError,\n AgentProcessError,\n AgentSpawnError,\n} from './errors.js';\n\nexport type ClaudeRunOptions = {\n additionalSystemPrompt?: string;\n additionalAllowedTools?: string[];\n maxTurns?: number; // Hard limit on the number of turns per Claude Code call\n\n // required\n timeoutSec: number; // Timeout per .run() call\n maxRetries: number; // Max number of retries per .run() call\n};\n\nexport type ClaudeRunnerOptions = {\n softTurnLimit?: number; // Soft limit on the number of turns per Claude runner\n};\n\nexport interface ClaudeCodeObservation {}\n\nconst DEFAULT_ALLOWED_TOOLS = [\n 'mcp__locadex__fetch-docs',\n 'mcp__locadex__list-docs',\n 'Bash',\n 'Edit',\n 'MultiEdit',\n 'Write',\n].concat(guides.map((guide) => `mcp__locadex__${guide.id}`));\n\nconst DISALLOWED_TOOLS = ['NotebookEdit', 'WebFetch', 'WebSearch'];\n\nexport class ClaudeCodeRunner {\n private id: string;\n private sessionId: string | undefined;\n private mcpConfig: string | undefined;\n private manager: LocadexManager;\n private changes: string[] = [];\n private controller: AbortController;\n private softTurnLimit: number;\n private turns: number = 0;\n\n private stats: {\n cost: number;\n wallDuration: number;\n apiDuration: number;\n turns: number;\n mcpToolCalls: number;\n inputTokens: number;\n outputTokens: number;\n cachedInputTokens: number;\n };\n\n constructor(\n manager: LocadexManager,\n controller: AbortController,\n private options: {\n id: string;\n apiKey: string;\n mcpConfig: string;\n softTurnLimit: number;\n }\n ) {\n this.manager = manager;\n this.id = options.id;\n this.mcpConfig = options.mcpConfig;\n this.controller = controller;\n this.softTurnLimit = options.softTurnLimit;\n\n this.stats = {\n cost: 0,\n wallDuration: 0,\n apiDuration: 0,\n turns: 0,\n mcpToolCalls: 0,\n inputTokens: 0,\n outputTokens: 0,\n cachedInputTokens: 0,\n };\n\n // Ensure API key is set\n if (!process.env.ANTHROPIC_API_KEY && !this.options.apiKey) {\n throw new Error(\n 'ANTHROPIC_API_KEY environment variable or apiKey option is required'\n );\n }\n }\n\n getSessionId(): string | undefined {\n return this.sessionId;\n }\n reset() {\n this.sessionId = undefined;\n this.turns = 0;\n this.resetStats();\n }\n resetStats() {\n this.stats = {\n cost: 0,\n wallDuration: 0,\n apiDuration: 0,\n turns: 0,\n mcpToolCalls: 0,\n inputTokens: 0,\n outputTokens: 0,\n cachedInputTokens: 0,\n };\n }\n aggregateStats() {\n this.manager.stats.updateStats({\n newCost: this.stats.cost,\n newWallDuration: this.stats.wallDuration,\n newApiDuration: this.stats.apiDuration,\n newTurns: this.stats.turns,\n newToolCalls: this.stats.mcpToolCalls,\n newInputTokens: this.stats.inputTokens,\n newOutputTokens: this.stats.outputTokens,\n newCachedInputTokens: this.stats.cachedInputTokens,\n });\n }\n\n /**\n * Wraps a promise with a timeout mechanism that can abort the underlying operation\n */\n private async withTimeout<T>(\n promiseFactory: (abortController: AbortController) => Promise<T>,\n timeoutSec: number,\n timeoutMessage?: string\n ): Promise<T> {\n const timeoutController = new AbortController();\n let timeoutId: ReturnType<typeof global.setTimeout>;\n\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = global.setTimeout(() => {\n timeoutController.abort();\n reject(\n new TimeoutError(\n timeoutMessage || `Operation timed out after ${timeoutSec}s`,\n timeoutSec\n )\n );\n }, timeoutSec * 1000);\n });\n\n const promise = promiseFactory(timeoutController);\n return Promise.race([promise, timeoutPromise]).finally(() => {\n // Clear the timeout regardless of how the promise resolves\n global.clearTimeout(timeoutId);\n });\n }\n\n /**\n * Retries an async operation with exponential backoff\n * Retries on TimeoutError but not on UserAbortError\n */\n private async withRetry<T>(\n operation: () => Promise<T>,\n maxRetries: number = 1,\n baseDelayMs: number = 1000\n ): Promise<T> {\n let lastError: Error;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n return await operation();\n } catch (error) {\n lastError = error as Error;\n\n // Don't retry on user aborts - these are intentional\n if (\n lastError instanceof UserAbortError ||\n lastError.name === 'AbortError'\n ) {\n logger.debugMessage(`Claude Code operation aborted by user`);\n throw lastError;\n }\n\n // Don't retry on the last attempt\n if (attempt === maxRetries) {\n throw lastError;\n }\n\n const delay = baseDelayMs * Math.pow(2, attempt);\n\n // Log different messages for different error types\n if (lastError instanceof TimeoutError) {\n logger.debugMessage(\n `Claude Code operation timed out (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${lastError.message}`\n );\n // reset the session id\n this.reset();\n } else {\n logger.debugMessage(\n `Claude Code operation failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms: ${lastError.message}`\n );\n }\n\n await new Promise((resolve) => global.setTimeout(resolve, delay));\n }\n }\n\n throw lastError!;\n }\n\n async run(\n prompt: string,\n options: ClaudeRunOptions,\n _obs: ClaudeCodeObservation\n ): Promise<string> {\n this.changes = [];\n return this.withRetry(\n () =>\n Sentry.startSpan(\n {\n name: 'claude-code-exec',\n op: 'claude-code.exec',\n attributes: {\n 'process.command': 'claude',\n },\n },\n () =>\n this.withTimeout(\n (timeoutController: AbortController) =>\n new Promise<string>((resolve, reject) => {\n const args = ['-p', prompt];\n\n if (options.additionalSystemPrompt) {\n args.push(\n '--append-system-prompt',\n options.additionalSystemPrompt\n );\n }\n\n args.push('--output-format', 'stream-json');\n args.push('--verbose');\n\n if (this.sessionId) {\n if (this.turns < this.softTurnLimit) {\n args.push('--resume', this.sessionId);\n } else {\n logger.debugMessage(\n `[${this.id}] Resetting session id because of soft turn limit reached: ${this.turns} >= ${this.softTurnLimit}`\n );\n this.reset();\n }\n }\n\n if (this.mcpConfig) {\n args.push('--mcp-config', this.mcpConfig);\n }\n\n args.push(\n '--allowedTools',\n [\n ...DEFAULT_ALLOWED_TOOLS,\n ...(options?.additionalAllowedTools || []),\n ].join(',')\n );\n\n args.push('--disallowedTools', DISALLOWED_TOOLS.join(','));\n\n if (options.maxTurns) {\n args.push('--max-turns', options.maxTurns.toString());\n }\n\n const env = { ...process.env };\n env.ANTHROPIC_API_KEY = this.options.apiKey;\n logger.debugMessage(\n `[${this.id}] Spawning Claude Code with additional args: ${JSON.stringify(\n {\n maxTurns: options.maxTurns,\n softTurnLimit: this.softTurnLimit,\n timeoutSec: options.timeoutSec,\n maxRetries: options.maxRetries,\n sessionId: this.sessionId,\n mcpConfig: this.mcpConfig,\n additionalAllowedTools: options.additionalAllowedTools,\n },\n null,\n 2\n )}. API key is ${this.options.apiKey ? 'set' : 'not set'}`\n );\n\n // Create a combined abort controller that triggers on either user abort or timeout\n const combinedController = new AbortController();\n\n const abortHandler = () => {\n combinedController.abort();\n };\n\n this.controller.signal.addEventListener(\n 'abort',\n abortHandler\n );\n timeoutController.signal.addEventListener(\n 'abort',\n abortHandler\n );\n\n const claude = spawn('claude', args, {\n stdio: ['ignore', 'pipe', 'pipe'],\n env,\n signal: combinedController.signal,\n });\n\n logger.debugMessage(\n `[${this.id}] Spawned claude code process`\n );\n\n const output = {\n error: '',\n };\n\n let buffer = '';\n claude.stdout?.on('data', (data) => {\n buffer += data.toString();\n const lines = buffer.split('\\n');\n\n // Keep the last incomplete line in buffer\n buffer = lines.pop() || '';\n\n for (const line of lines) {\n if (line.trim()) {\n try {\n logger.debugMessage(`[${this.id}] ${line}`);\n const outputData: ClaudeSDKMessage = JSON.parse(line);\n const result = this.handleSDKOutput(outputData, _obs);\n if (!result.success) {\n output.error = result.error ?? '';\n }\n } catch (error) {\n logger.debugMessage(\n `[${this.id}] Failed to parse JSON: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n }\n }\n });\n\n claude.stderr?.on('data', (data) => {\n logger.debugMessage(\n `[${this.id}] ${data.toString().trim()}`\n );\n });\n\n claude.on('close', (code, signal) => {\n // Clean up event listeners\n this.controller.signal.removeEventListener(\n 'abort',\n abortHandler\n );\n timeoutController.signal.removeEventListener(\n 'abort',\n abortHandler\n );\n\n if (signal === 'SIGTERM' || signal === 'SIGKILL') {\n // Process was terminated due to abort\n if (this.controller.signal.aborted) {\n reject(\n new UserAbortError(\n 'Claude Code process was aborted by user'\n )\n );\n } else if (timeoutController.signal.aborted) {\n reject(\n new TimeoutError(\n `Claude Code process timed out after ${options.timeoutSec}s`,\n options.timeoutSec\n )\n );\n } else {\n reject(\n new AgentProcessError(\n `[${this.id}] Claude Code process was terminated with signal ${signal}`,\n code ?? undefined\n )\n );\n }\n } else if (code === 0) {\n logger.debugMessage(\n `[${this.id}] Claude Code exited with code ${code}`\n );\n resolve('');\n } else {\n logger.debugMessage(\n `[${this.id}] Claude Code exited with code ${code}: ${output.error}`\n );\n reject(\n new AgentProcessError(\n `[${this.id}] Claude Code exited with code ${code}: ${output.error}`,\n code ?? undefined\n )\n );\n }\n });\n\n claude.on('error', (error) => {\n // Clean up event listeners\n this.controller.signal.removeEventListener(\n 'abort',\n abortHandler\n );\n timeoutController.signal.removeEventListener(\n 'abort',\n abortHandler\n );\n\n if (error.name === 'AbortError') {\n // Determine if this was a user abort or timeout abort\n if (this.controller.signal.aborted) {\n reject(\n new UserAbortError(\n 'Claude Code process was aborted by user'\n )\n );\n } else if (timeoutController.signal.aborted) {\n reject(\n new TimeoutError(\n `Claude Code process timed out after ${options.timeoutSec}s`,\n options.timeoutSec\n )\n );\n } else {\n reject(\n new UserAbortError('Claude Code process was aborted')\n );\n }\n } else {\n logger.debugMessage(\n `[${this.id}] failed to run Claude Code: ${error.message}`\n );\n reject(\n new AgentSpawnError(\n `[${this.id}] failed to run Claude Code: ${error.message}`,\n error\n )\n );\n }\n });\n }),\n options.timeoutSec,\n `Claude Code operation timed out after ${options.timeoutSec}s`\n )\n ),\n options.maxRetries\n );\n }\n\n private handleSDKOutput(\n outputData: ClaudeSDKMessage,\n _obs: ClaudeCodeObservation\n ): { success: boolean; error?: string } {\n if (outputData.type === 'assistant') {\n const text: string[] = [];\n const toolUses: string[] = [];\n outputData.message.content.forEach((c) => {\n if (c.type === 'text') {\n text.push(c.text);\n }\n if (c.type === 'tool_use') {\n toolUses.push(c.name);\n if (c.name.startsWith('mcp__locadex__')) {\n posthog.capture({\n distinctId: getSessionId(),\n event: 'tool_used',\n properties: {\n tool: c.name,\n },\n });\n }\n }\n });\n if (text.length > 0) {\n logger.verboseMessage(`[${this.id}] ${text.join('').trim()}`);\n }\n if (toolUses.length > 0) {\n logger.debugMessage(`[${this.id}] used tools: ${toolUses.join(', ')}`);\n }\n this.stats.mcpToolCalls += toolUses.length;\n this.stats.inputTokens += outputData.message.usage.input_tokens;\n this.stats.outputTokens += outputData.message.usage.output_tokens;\n this.stats.cachedInputTokens +=\n outputData.message.usage.cache_read_input_tokens ?? 0;\n } else if (outputData.type === 'result') {\n this.stats.cost = outputData.total_cost_usd;\n this.stats.wallDuration = outputData.duration_ms;\n this.stats.apiDuration = outputData.duration_api_ms;\n this.stats.turns = outputData.num_turns;\n this.turns = outputData.num_turns;\n if (!outputData.is_error) {\n logger.verboseMessage(\n `[${this.id}] finished task.\\nCost: $${outputData.total_cost_usd.toFixed(2)}\\nDuration: ${\n outputData.duration_ms / 1000\n }s`\n );\n } else {\n logger.verboseMessage(\n `[${this.id}] finished task with error: ${outputData.subtype}\\nCost: $${outputData.total_cost_usd}\\nDuration: ${\n outputData.duration_ms / 1000\n }s`\n );\n return {\n success: false,\n error: outputData.subtype,\n };\n }\n if (outputData.subtype === 'success') {\n this.changes.push(outputData.result);\n }\n } else if (outputData.type === 'system') {\n if (outputData.subtype === 'init') {\n this.sessionId = outputData.session_id;\n }\n }\n return {\n success: true,\n };\n }\n\n generateReport(): string {\n return this.changes.join('\\n');\n }\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare class AgentError extends Error {
|
|
2
|
+
readonly code?: string | undefined;
|
|
3
|
+
constructor(message: string, code?: string | undefined);
|
|
4
|
+
}
|
|
5
|
+
export declare class TimeoutError extends AgentError {
|
|
6
|
+
readonly timeoutSec: number;
|
|
7
|
+
constructor(message: string, timeoutSec: number);
|
|
8
|
+
}
|
|
9
|
+
export declare class UserAbortError extends AgentError {
|
|
10
|
+
constructor(message?: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class AgentProcessError extends AgentError {
|
|
13
|
+
readonly exitCode?: number | undefined;
|
|
14
|
+
constructor(message: string, exitCode?: number | undefined);
|
|
15
|
+
}
|
|
16
|
+
export declare class AgentSpawnError extends AgentError {
|
|
17
|
+
readonly originalError: Error;
|
|
18
|
+
constructor(message: string, originalError: Error);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"/","sources":["utils/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,UAAW,SAAQ,KAAK;aAGjB,IAAI,CAAC,EAAE,MAAM;gBAD7B,OAAO,EAAE,MAAM,EACC,IAAI,CAAC,EAAE,MAAM,YAAA;CAKhC;AAED,qBAAa,YAAa,SAAQ,UAAU;aAGxB,UAAU,EAAE,MAAM;gBADlC,OAAO,EAAE,MAAM,EACC,UAAU,EAAE,MAAM;CAKrC;AAED,qBAAa,cAAe,SAAQ,UAAU;gBAChC,OAAO,GAAE,MAAwC;CAI9D;AAED,qBAAa,iBAAkB,SAAQ,UAAU;aAG7B,QAAQ,CAAC,EAAE,MAAM;gBADjC,OAAO,EAAE,MAAM,EACC,QAAQ,CAAC,EAAE,MAAM,YAAA;CAKpC;AAED,qBAAa,eAAgB,SAAQ,UAAU;aAG3B,aAAa,EAAE,KAAK;gBADpC,OAAO,EAAE,MAAM,EACC,aAAa,EAAE,KAAK;CAKvC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export class AgentError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(message, code) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = 'AgentError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class TimeoutError extends AgentError {
|
|
10
|
+
timeoutSec;
|
|
11
|
+
constructor(message, timeoutSec) {
|
|
12
|
+
super(message, 'TIMEOUT');
|
|
13
|
+
this.timeoutSec = timeoutSec;
|
|
14
|
+
this.name = 'TimeoutError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class UserAbortError extends AgentError {
|
|
18
|
+
constructor(message = 'Operation was aborted by user') {
|
|
19
|
+
super(message, 'USER_ABORT');
|
|
20
|
+
this.name = 'UserAbortError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class AgentProcessError extends AgentError {
|
|
24
|
+
exitCode;
|
|
25
|
+
constructor(message, exitCode) {
|
|
26
|
+
super(message, 'PROCESS_ERROR');
|
|
27
|
+
this.exitCode = exitCode;
|
|
28
|
+
this.name = 'AgentProcessError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class AgentSpawnError extends AgentError {
|
|
32
|
+
originalError;
|
|
33
|
+
constructor(message, originalError) {
|
|
34
|
+
super(message, 'SPAWN_ERROR');
|
|
35
|
+
this.originalError = originalError;
|
|
36
|
+
this.name = 'AgentSpawnError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"/","sources":["utils/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAW,SAAQ,KAAK;IAGjB;IAFlB,YACE,OAAe,EACC,IAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,SAAI,GAAJ,IAAI,CAAS;QAG7B,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF;AAED,MAAM,OAAO,YAAa,SAAQ,UAAU;IAGxB;IAFlB,YACE,OAAe,EACC,UAAkB;QAElC,KAAK,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAFV,eAAU,GAAV,UAAU,CAAQ;QAGlC,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,OAAO,cAAe,SAAQ,UAAU;IAC5C,YAAY,UAAkB,+BAA+B;QAC3D,KAAK,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF;AAED,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAG7B;IAFlB,YACE,OAAe,EACC,QAAiB;QAEjC,KAAK,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QAFhB,aAAQ,GAAR,QAAQ,CAAS;QAGjC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED,MAAM,OAAO,eAAgB,SAAQ,UAAU;IAG3B;IAFlB,YACE,OAAe,EACC,aAAoB;QAEpC,KAAK,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;QAFd,kBAAa,GAAb,aAAa,CAAO;QAGpC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF","sourcesContent":["export class AgentError extends Error {\n constructor(\n message: string,\n public readonly code?: string\n ) {\n super(message);\n this.name = 'AgentError';\n }\n}\n\nexport class TimeoutError extends AgentError {\n constructor(\n message: string,\n public readonly timeoutSec: number\n ) {\n super(message, 'TIMEOUT');\n this.name = 'TimeoutError';\n }\n}\n\nexport class UserAbortError extends AgentError {\n constructor(message: string = 'Operation was aborted by user') {\n super(message, 'USER_ABORT');\n this.name = 'UserAbortError';\n }\n}\n\nexport class AgentProcessError extends AgentError {\n constructor(\n message: string,\n public readonly exitCode?: number\n ) {\n super(message, 'PROCESS_ERROR');\n this.name = 'AgentProcessError';\n }\n}\n\nexport class AgentSpawnError extends AgentError {\n constructor(\n message: string,\n public readonly originalError: Error\n ) {\n super(message, 'SPAWN_ERROR');\n this.name = 'AgentSpawnError';\n }\n}\n"]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { PackageManager } from 'gtx-cli/utils/packageManager';
|
|
2
2
|
import { LocadexManager } from '../locadexManager.js';
|
|
3
|
-
export declare function installClaudeCode(): Promise<void>;
|
|
4
3
|
export declare function addTranslateScript(manager: LocadexManager, packageJson: Record<string, any>, packageManager: PackageManager): Promise<void>;
|
|
5
|
-
export declare function
|
|
4
|
+
export declare function installGlobalPackage(packageName: string, version: string): Promise<void>;
|
|
6
5
|
//# sourceMappingURL=installPackage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"installPackage.d.ts","sourceRoot":"/","sources":["utils/packages/installPackage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"installPackage.d.ts","sourceRoot":"/","sources":["utils/packages/installPackage.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAG9D,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,cAAc,EACvB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,cAAc,EAAE,cAAc,iBAsC/B;AAED,wBAAsB,oBAAoB,CACxC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,iBAqBhB"}
|