88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import express from 'express'
|
|
import { spawn } from 'child_process'
|
|
import cors from 'cors'
|
|
|
|
const app = express()
|
|
app.use(cors())
|
|
app.use(express.json())
|
|
|
|
app.post('/api/execute', (req, res) => {
|
|
const { description, validation } = req.body
|
|
|
|
const prompt = `You are an AI coding assistant executing a work item.
|
|
|
|
WORK ITEM DESCRIPTION:
|
|
${description}
|
|
|
|
VALIDATION CONDITIONS:
|
|
${validation}
|
|
|
|
Please execute the work item by making the necessary code changes to implement the description. After execution, verify that the work meets the validation conditions.
|
|
|
|
Return your response in JSON format:
|
|
{
|
|
"executed": true,
|
|
"validated": true/false,
|
|
"output": "Brief summary of what was done",
|
|
"errorMessage": "Error message if validation failed or execution failed"
|
|
}
|
|
|
|
If execution or validation fails, provide a helpful error message. Respond with ONLY the JSON object.`
|
|
|
|
const child = spawn('opencode', ['run', prompt], {
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
shell: true,
|
|
})
|
|
|
|
let output = ''
|
|
let error = ''
|
|
|
|
child.stdout.on('data', (data: Buffer) => {
|
|
output += data.toString()
|
|
})
|
|
|
|
child.stderr.on('data', (data: Buffer) => {
|
|
error += data.toString()
|
|
})
|
|
|
|
child.on('close', (code: number) => {
|
|
if (code === 0) {
|
|
try {
|
|
const jsonMatch = output.match(/\{[\s\S]*\}/)
|
|
if (jsonMatch) {
|
|
const result = JSON.parse(jsonMatch[0])
|
|
res.json({
|
|
validated: result.validated ?? false,
|
|
errorMessage: result.errorMessage,
|
|
})
|
|
return
|
|
}
|
|
} catch (e) {
|
|
console.error('Failed to parse opencode output:', e)
|
|
}
|
|
res.status(500).json({
|
|
validated: false,
|
|
errorMessage: 'Failed to parse opencode response',
|
|
})
|
|
} else {
|
|
res.status(500).json({
|
|
validated: false,
|
|
errorMessage: error || `Opencode exited with code ${code}`,
|
|
})
|
|
}
|
|
})
|
|
|
|
child.on('error', (err: Error) => {
|
|
res.status(500).json({
|
|
validated: false,
|
|
errorMessage: `Failed to start opencode: ${err.message}`,
|
|
})
|
|
})
|
|
|
|
child.stdin.end()
|
|
})
|
|
|
|
const PORT = process.env.PORT || 3000
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`)
|
|
}) |