chore: baseline Agent Workbench 1.0.0
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import os from 'node:os';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
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 { 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);
|
||||
const provider = new Provider();
|
||||
const runtime = new Runtime(store, provider);
|
||||
const token = randomBytes(32).toString('hex');
|
||||
const clients = new Set();
|
||||
const started = Date.now();
|
||||
let actualPort = requestedPort;
|
||||
let closing = false;
|
||||
let exampleProject = null;
|
||||
try {
|
||||
const exampleRoot = path.join(appDirectory, 'examples', 'starter-project');
|
||||
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 }));
|
||||
|
||||
const server = http.createServer();
|
||||
server.on('request', createRequestHandler({ appDirectory, store, provider, runtime, token, clients, started,
|
||||
getPort: () => actualPort, exampleProject, shutdown }));
|
||||
|
||||
async function shutdown() {
|
||||
if (closing) return; closing = true;
|
||||
await runtime.stopAll();
|
||||
for (const response of clients) response.end();
|
||||
clients.clear();
|
||||
server.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.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