forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-web-ui.ts
More file actions
61 lines (52 loc) · 2.01 KB
/
Copy pathrun-web-ui.ts
File metadata and controls
61 lines (52 loc) · 2.01 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
54
55
56
57
58
59
60
61
import { ExecLogger } from '../../../logging';
import { findAvailablePort } from '../server';
import { WEB_UI_DEFAULT_PORT } from './constants';
import { type WebUIOptions, WebUIServer } from './web-server';
import { spawn } from 'child_process';
export interface RunWebUIOptions {
/** Options to pass to WebUIServer (minus uiPort, which is resolved automatically) */
serverOptions: Omit<WebUIOptions, 'uiPort' | 'onReady' | 'onLog'>;
/** Logger command label (e.g. 'dev') */
logLabel: string;
/** Optional log handler override. Defaults to console logging errors. */
onLog?: (level: 'info' | 'warn' | 'error', message: string) => void;
}
/**
* Shared entry point for launching the web UI.
* Handles port discovery, logger setup, browser launch, SIGINT, and keep-alive.
*/
export async function runWebUI(opts: RunWebUIOptions): Promise<void> {
const { serverOptions, logLabel } = opts;
const logger = new ExecLogger({ command: logLabel });
const uiPort = await findAvailablePort(WEB_UI_DEFAULT_PORT);
if (uiPort !== WEB_UI_DEFAULT_PORT) {
console.log(`Port ${WEB_UI_DEFAULT_PORT} in use, using ${uiPort}`);
}
console.log(`Starting web UI...`);
console.log(`Log: ${logger.getRelativeLogPath()}`);
const onLog =
opts.onLog ??
((level: 'info' | 'warn' | 'error', msg: string) => {
if (level === 'error') console.error(`Web UI: ${msg}`);
});
const webUI = new WebUIServer({
...serverOptions,
uiPort,
onReady: url => {
const chatUrl = url;
console.log(`\nChat UI: ${chatUrl}`);
console.log(`Press Ctrl+C to stop\n`);
const openCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
spawn(openCmd, [chatUrl], { stdio: 'ignore', detached: true }).unref();
},
onLog,
});
webUI.start();
process.on('SIGINT', () => {
console.log('\nStopping servers...');
webUI.stop();
});
// Keep process alive
// eslint-disable-next-line @typescript-eslint/no-empty-function
await new Promise(() => {});
}