feat: persist execution state and tool results in SQLite
This commit is contained in:
+5
-11
@@ -7,13 +7,14 @@ import { fileURLToPath } from 'node:url';
|
||||
import { Store } from './store.mjs';
|
||||
import { Provider } from './provider.mjs';
|
||||
import { Runtime } from './runtime.mjs';
|
||||
import { createRequestHandler } from './app.mjs';
|
||||
import { createRequestHandler, createSessionPublisher } from './app.mjs';
|
||||
import { APP_VERSION } from './diagnostics.mjs';
|
||||
|
||||
const appDirectory = process.env.AGENT_WORKBENCH_APP_DIR || path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const dataDirectory = process.env.AGENT_WORKBENCH_DATA_DIR || path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), '.local', 'share'), 'AgentWorkbench');
|
||||
const requestedPort = Number(process.env.AGENT_WORKBENCH_PORT || 4318);
|
||||
const store = new Store(dataDirectory);
|
||||
store.claimRuntime();
|
||||
const provider = new Provider();
|
||||
const runtime = new Runtime(store, provider);
|
||||
const token = randomBytes(32).toString('hex');
|
||||
@@ -27,14 +28,7 @@ try {
|
||||
if (fs.existsSync(exampleRoot)) exampleProject = store.addProject(exampleRoot);
|
||||
} catch {}
|
||||
|
||||
function sendEvent(value) {
|
||||
const payload = 'data: ' + JSON.stringify(value) + '\n\n';
|
||||
for (const response of clients) {
|
||||
if (response.writableLength > 2 * 1024 * 1024) { response.destroy(); clients.delete(response); }
|
||||
else response.write(payload);
|
||||
}
|
||||
}
|
||||
store.on('update', session => sendEvent({ kind: 'session', session }));
|
||||
store.on('update', createSessionPublisher(clients));
|
||||
|
||||
const server = http.createServer();
|
||||
server.on('request', createRequestHandler({ appDirectory, store, provider, runtime, token, clients, started,
|
||||
@@ -45,11 +39,11 @@ async function shutdown() {
|
||||
await runtime.stopAll();
|
||||
for (const response of clients) response.end();
|
||||
clients.clear();
|
||||
server.close(() => process.exit(0));
|
||||
server.close(() => { store.close(); process.exit(0); });
|
||||
setTimeout(() => process.exit(0), 3000).unref();
|
||||
}
|
||||
process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown);
|
||||
server.on('error', error => { console.error('Workbench could not start: ' + error.message); process.exitCode = 1; });
|
||||
server.on('error', error => { store.close(); console.error('Workbench could not start: ' + error.message); process.exitCode = 1; });
|
||||
server.listen(requestedPort, '127.0.0.1', () => {
|
||||
actualPort = server.address().port;
|
||||
console.log(JSON.stringify({ app: 'agent-workbench', ready: true, url: 'http://127.0.0.1:' + actualPort, version: APP_VERSION }));
|
||||
|
||||
Reference in New Issue
Block a user