mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 18:33:01 +00:00
opencode chat backend
This commit is contained in:
65
packages/backend/src/routes/ai/opencode.ts
Normal file
65
packages/backend/src/routes/ai/opencode.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user