Initial commit: Discord-Claude Gateway with event-driven agent runtime
This commit is contained in:
@@ -171,6 +171,8 @@ export class AgentRuntime {
|
||||
// Max turns to prevent runaway loops
|
||||
args.push("--max-turns", "25");
|
||||
|
||||
console.log(`[DEBUG] Spawning Claude CLI: ${this.config.claudeCliPath} ${args.slice(0, 4).join(" ")} ... (${args.length} args total)`);
|
||||
|
||||
const child = execFile(
|
||||
this.config.claudeCliPath,
|
||||
args,
|
||||
@@ -181,6 +183,7 @@ export class AgentRuntime {
|
||||
},
|
||||
(error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`[DEBUG] Claude CLI error: code=${error.code}, killed=${error.killed}, stderr=${stderr?.slice(0, 500)}`);
|
||||
if (error.killed || error.code === "ETIMEDOUT") {
|
||||
reject(new Error("Query timed out"));
|
||||
return;
|
||||
@@ -189,9 +192,12 @@ export class AgentRuntime {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[DEBUG] Claude CLI stdout (${stdout.length} chars): ${stdout.slice(0, 300)}...`);
|
||||
if (stderr) {
|
||||
console.log(`[DEBUG] Claude CLI stderr: ${stderr.slice(0, 300)}`);
|
||||
}
|
||||
|
||||
try {
|
||||
// The JSON output may contain multiple JSON objects (one per line for stream-json)
|
||||
// With --output-format json, the last line is the final result
|
||||
const lines = stdout.trim().split("\n");
|
||||
const lastLine = lines[lines.length - 1];
|
||||
const parsed = JSON.parse(lastLine) as ClaudeJsonResponse;
|
||||
@@ -203,7 +209,6 @@ export class AgentRuntime {
|
||||
|
||||
resolve(parsed);
|
||||
} catch (parseError) {
|
||||
// If JSON parsing fails, treat stdout as plain text result
|
||||
resolve({
|
||||
type: "result",
|
||||
result: stdout.trim(),
|
||||
@@ -212,8 +217,8 @@ export class AgentRuntime {
|
||||
},
|
||||
);
|
||||
|
||||
// Handle child process errors
|
||||
child.on("error", (err) => {
|
||||
console.error(`[DEBUG] Failed to spawn Claude CLI: ${err.message}`);
|
||||
reject(new Error(`Failed to spawn Claude CLI: ${err.message}`));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user