Initial commit: Discord-Claude Gateway with event-driven agent runtime

This commit is contained in:
2026-02-22 00:56:18 -05:00
parent 5c3f313033
commit b69e669638

View File

@@ -171,6 +171,8 @@ export class AgentRuntime {
// Max turns to prevent runaway loops // Max turns to prevent runaway loops
args.push("--max-turns", "25"); 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( const child = execFile(
this.config.claudeCliPath, this.config.claudeCliPath,
args, args,
@@ -181,6 +183,7 @@ export class AgentRuntime {
}, },
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { 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") { if (error.killed || error.code === "ETIMEDOUT") {
reject(new Error("Query timed out")); reject(new Error("Query timed out"));
return; return;
@@ -189,9 +192,12 @@ export class AgentRuntime {
return; 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 { 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 lines = stdout.trim().split("\n");
const lastLine = lines[lines.length - 1]; const lastLine = lines[lines.length - 1];
const parsed = JSON.parse(lastLine) as ClaudeJsonResponse; const parsed = JSON.parse(lastLine) as ClaudeJsonResponse;
@@ -203,7 +209,6 @@ export class AgentRuntime {
resolve(parsed); resolve(parsed);
} catch (parseError) { } catch (parseError) {
// If JSON parsing fails, treat stdout as plain text result
resolve({ resolve({
type: "result", type: "result",
result: stdout.trim(), result: stdout.trim(),
@@ -212,8 +217,8 @@ export class AgentRuntime {
}, },
); );
// Handle child process errors
child.on("error", (err) => { child.on("error", (err) => {
console.error(`[DEBUG] Failed to spawn Claude CLI: ${err.message}`);
reject(new Error(`Failed to spawn Claude CLI: ${err.message}`)); reject(new Error(`Failed to spawn Claude CLI: ${err.message}`));
}); });
}); });