Initial commit: Discord-Claude Gateway with event-driven agent runtime
This commit is contained in:
@@ -106,7 +106,7 @@ export class AgentRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
responseText: response.result,
|
responseText: response.result || undefined,
|
||||||
targetChannelId: channelId,
|
targetChannelId: channelId,
|
||||||
sessionId: response.session_id,
|
sessionId: response.session_id,
|
||||||
};
|
};
|
||||||
@@ -233,16 +233,58 @@ export class AgentRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// The CLI with --output-format json returns newline-delimited JSON objects.
|
||||||
|
// We need to find the "result" type message which contains the actual response.
|
||||||
const lines = stdout.trim().split("\n");
|
const lines = stdout.trim().split("\n");
|
||||||
const lastLine = lines[lines.length - 1];
|
let resultText = "";
|
||||||
const parsed = JSON.parse(lastLine) as ClaudeJsonResponse;
|
let sessionId: string | undefined;
|
||||||
|
|
||||||
if (parsed.is_error) {
|
for (const line of lines) {
|
||||||
reject(new Error(`Claude error: ${parsed.result ?? "Unknown error"}`));
|
try {
|
||||||
return;
|
// Each line might be a JSON object or part of a JSON array
|
||||||
|
const cleaned = line.replace(/^\[/, "").replace(/,?\]$/, "").replace(/^,/, "").trim();
|
||||||
|
if (!cleaned) continue;
|
||||||
|
const obj = JSON.parse(cleaned);
|
||||||
|
|
||||||
|
if (obj.type === "system" && obj.subtype === "init" && obj.session_id) {
|
||||||
|
sessionId = obj.session_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(parsed);
|
if (obj.type === "result" && obj.result) {
|
||||||
|
resultText = obj.result;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Skip unparseable lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we couldn't parse individual lines, try parsing the whole thing as a JSON array
|
||||||
|
if (!resultText) {
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(stdout.trim());
|
||||||
|
if (Array.isArray(arr)) {
|
||||||
|
for (const obj of arr) {
|
||||||
|
if (obj.type === "system" && obj.subtype === "init" && obj.session_id) {
|
||||||
|
sessionId = obj.session_id;
|
||||||
|
}
|
||||||
|
if (obj.type === "result" && obj.result) {
|
||||||
|
resultText = obj.result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Not a JSON array either
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[DEBUG] Parsed result: ${resultText.length} chars, session=${sessionId ?? "none"}`);
|
||||||
|
|
||||||
|
resolve({
|
||||||
|
type: "result",
|
||||||
|
result: resultText || undefined,
|
||||||
|
session_id: sessionId,
|
||||||
|
is_error: false,
|
||||||
|
});
|
||||||
} catch {
|
} catch {
|
||||||
resolve({
|
resolve({
|
||||||
type: "result",
|
type: "result",
|
||||||
|
|||||||
Reference in New Issue
Block a user