Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
53 lines (44 loc) · 1.56 KB
/
Copy pathcli.ts
File metadata and controls
53 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Effect } from 'effect';
import { serve } from '@hono/node-server';
import { createServer } from './server/index.js';
import { createAppRuntime } from './layer.js';
import { loadConfig, ensureUserConfig } from '@codingcode/infra/config';
import { ensureTempCwd } from './core/path.js';
import { findAvailablePort } from './server/port-discovery.js';
import { AgentError } from './core/error.js';
import { SchedulerService } from './scheduler/port.js';
async function main() {
ensureUserConfig();
ensureTempCwd();
const config = loadConfig();
const basePort = config.server.port;
const rt = createAppRuntime();
const program = Effect.gen(function* () {
const port = yield* Effect.tryPromise(() => findAvailablePort(basePort));
// Initialize scheduler with the shared runtime
const scheduler = yield* SchedulerService;
scheduler.setRuntime(rt);
scheduler.initialize();
const app = yield* Effect.tryPromise(() => createServer(rt));
serve({ fetch: app.fetch, port });
console.log(`CODINGCODE_SERVER_READY:${port}`);
});
const result = await rt.runPromise(
program.pipe(
Effect.match({
onSuccess: () => ({ type: 'ok' as const }),
onFailure: (err: unknown) => ({ type: 'err' as const, err }),
})
)
);
if (result.type === 'err') {
const err = result.err;
if (err instanceof AgentError) {
console.error(`Error [${err.code}]: ${err.message}`);
process.exit(err.code === 'CONFIG_MISSING' ? 78 : 64);
}
console.error('Internal error:', err);
process.exit(1);
}
}
main();