opencode chat backend

This commit is contained in:
2026-01-31 10:13:18 +00:00
parent b301822543
commit 925e8f2746
9 changed files with 777 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
export type AIResponse = {
text: string;
highlighted_issues: number[];
suggested_actions: string[] | null;
raw: string;
};
export const callAI = async (prompt: string): Promise<AIResponse> => {
const result = Bun.spawn(["opencode", "run", prompt, "--model", "opencode/kimi-k2.5-free"], {
stdout: "pipe",
stderr: "pipe",
});
// Collect all output
let rawOutput = "";
for await (const chunk of result.stdout) {
rawOutput += new TextDecoder().decode(chunk);
}
let stderrOutput = "";
for await (const chunk of result.stderr) {
stderrOutput += new TextDecoder().decode(chunk);
}
await result.exited;
try {
const jsonMatch = rawOutput.match(/\{[\s\S]*\}/);
const jsonStr = jsonMatch ? jsonMatch[0] : rawOutput;
const response = JSON.parse(jsonStr);
if (!response.text || !Array.isArray(response.highlighted_issues)) {
throw new Error("Invalid response structure");
}
const output = {
text: response.text,
highlighted_issues: response.highlighted_issues,
suggested_actions: response.suggested_actions || [],
raw: rawOutput,
};
return output;
} catch (e) {
console.log(
JSON.stringify(
{
error: "Failed to parse AI response as JSON",
parse_error: e instanceof Error ? e.message : String(e),
raw: rawOutput,
stderr: stderrOutput,
},
null,
2,
),
);
return {
text: "Sorry, an error occurred while processing the AI response.",
highlighted_issues: [],
suggested_actions: [],
raw: rawOutput,
};
}
};