48 lines
1.0 KiB
TypeScript
48 lines
1.0 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 child = spawn('opencode', ['run', description], {
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
})
|
|
|
|
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) => {
|
|
res.json({
|
|
validated: code === 0,
|
|
errorMessage: error || (code !== 0 ? `Opencode exited with code ${code}` : undefined),
|
|
output,
|
|
})
|
|
})
|
|
|
|
child.on('error', (err: Error) => {
|
|
res.status(500).json({
|
|
validated: false,
|
|
errorMessage: `Failed to start opencode: ${err.message}`,
|
|
})
|
|
})
|
|
|
|
child.stdin.end()
|
|
})
|
|
|
|
const PORT = 3001
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`)
|
|
}) |