|
| 1 | +/** |
| 2 | + * `kimi vis` sub-command. |
| 3 | + * |
| 4 | + * CLI glue only: resolves the kimi home, starts the in-process session |
| 5 | + * visualizer server (auto-picking a free port by default), prints the URL, |
| 6 | + * optionally opens the browser (with an optional session deep-link), then |
| 7 | + * waits for Ctrl-C and shuts the server down. The visualizer server itself |
| 8 | + * lives in `@moonshot-ai/vis-server`. |
| 9 | + */ |
| 10 | + |
| 11 | +import type { Command } from 'commander'; |
| 12 | + |
| 13 | +import { createCliTelemetryBootstrap } from '#/cli/telemetry'; |
| 14 | +import { openUrl } from '#/utils/open-url'; |
| 15 | + |
| 16 | +interface WritableLike { |
| 17 | + write(chunk: string): boolean; |
| 18 | +} |
| 19 | + |
| 20 | +export interface StartedVisServer { |
| 21 | + readonly port: number; |
| 22 | + readonly host: string; |
| 23 | + readonly url: string; |
| 24 | + readonly close: () => Promise<void>; |
| 25 | +} |
| 26 | + |
| 27 | +export interface StartVisServerArgs { |
| 28 | + readonly homeDir: string; |
| 29 | + readonly port: number; |
| 30 | + readonly host?: string; |
| 31 | + readonly webAsset?: { gzipped: Uint8Array }; |
| 32 | +} |
| 33 | + |
| 34 | +export interface VisDeps { |
| 35 | + readonly getHomeDir: () => string; |
| 36 | + readonly startVisServer: (opts: StartVisServerArgs) => Promise<StartedVisServer>; |
| 37 | + readonly openUrl: (url: string) => Promise<void>; |
| 38 | + readonly waitForShutdown: () => Promise<void>; |
| 39 | + readonly stdout: WritableLike; |
| 40 | + readonly stderr: WritableLike; |
| 41 | + readonly exit: (code: number) => never; |
| 42 | +} |
| 43 | + |
| 44 | +export interface VisOptions { |
| 45 | + readonly open: boolean; |
| 46 | + readonly port?: number; |
| 47 | + readonly host?: string; |
| 48 | + readonly sessionId?: string; |
| 49 | +} |
| 50 | + |
| 51 | +export async function handleVis(deps: VisDeps, opts: VisOptions): Promise<void> { |
| 52 | + const homeDir = deps.getHomeDir(); |
| 53 | + |
| 54 | + // Lazily load the embedded single-file SPA so normal `kimi` startup never |
| 55 | + // pays for it. The module is generated at build time (prebuild). When running |
| 56 | + // from source without a build — e.g. tests — the generated value module is |
| 57 | + // absent and the dynamic import throws; in that case the server falls back to |
| 58 | + // its own static `public/` directory. |
| 59 | + let webAsset: { gzipped: Uint8Array } | undefined; |
| 60 | + try { |
| 61 | + const { VIS_WEB_GZIP_B64 } = await import('#/generated/vis-web-asset'); |
| 62 | + if (VIS_WEB_GZIP_B64.length > 0) { |
| 63 | + webAsset = { gzipped: new Uint8Array(Buffer.from(VIS_WEB_GZIP_B64, 'base64')) }; |
| 64 | + } |
| 65 | + } catch { |
| 66 | + // Embedded asset not generated in this context — fall back to filesystem. |
| 67 | + } |
| 68 | + |
| 69 | + let server: StartedVisServer; |
| 70 | + try { |
| 71 | + server = await deps.startVisServer({ |
| 72 | + homeDir, |
| 73 | + port: opts.port ?? 0, |
| 74 | + ...(opts.host === undefined ? {} : { host: opts.host }), |
| 75 | + ...(webAsset === undefined ? {} : { webAsset }), |
| 76 | + }); |
| 77 | + } catch (error) { |
| 78 | + const msg = error instanceof Error ? error.message : String(error); |
| 79 | + deps.stderr.write(`Failed to start kimi vis: ${msg}\n`); |
| 80 | + return deps.exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + const target = |
| 84 | + opts.sessionId === undefined |
| 85 | + ? server.url |
| 86 | + : `${server.url}sessions/${encodeURIComponent(opts.sessionId)}`; |
| 87 | + |
| 88 | + deps.stdout.write(`kimi vis is running at ${server.url}\n`); |
| 89 | + deps.stdout.write('Press Ctrl-C to stop.\n'); |
| 90 | + |
| 91 | + if (opts.open) { |
| 92 | + try { |
| 93 | + await deps.openUrl(target); |
| 94 | + } catch { |
| 95 | + deps.stderr.write(`Could not open a browser; visit ${target} manually.\n`); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + await deps.waitForShutdown(); |
| 100 | + await server.close(); |
| 101 | +} |
| 102 | + |
| 103 | +export function registerVisCommand(parent: Command, overrides?: Partial<VisDeps>): void { |
| 104 | + parent |
| 105 | + .command('vis') |
| 106 | + .description('Launch the session visualizer in your browser.') |
| 107 | + .option('--port <number>', 'Port to bind. Default: auto-pick a free port.') |
| 108 | + .option('--host <host>', 'Host to bind. Default: 127.0.0.1.') |
| 109 | + .option('--no-open', 'Do not open the browser automatically.') |
| 110 | + .argument('[sessionId]', 'Open directly to this session.') |
| 111 | + .action( |
| 112 | + async ( |
| 113 | + sessionId: string | undefined, |
| 114 | + options: { port?: string; host?: string; open?: boolean }, |
| 115 | + ) => { |
| 116 | + const port = options.port === undefined ? undefined : Number.parseInt(options.port, 10); |
| 117 | + await handleVis(createDefaultVisDeps(overrides), { |
| 118 | + open: options.open !== false, |
| 119 | + ...(port === undefined || Number.isNaN(port) ? {} : { port }), |
| 120 | + ...(options.host === undefined ? {} : { host: options.host }), |
| 121 | + ...(sessionId === undefined ? {} : { sessionId }), |
| 122 | + }); |
| 123 | + }, |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +function createDefaultVisDeps(overrides: Partial<VisDeps> = {}): VisDeps { |
| 128 | + return { |
| 129 | + getHomeDir: overrides.getHomeDir ?? (() => createCliTelemetryBootstrap().homeDir), |
| 130 | + startVisServer: |
| 131 | + overrides.startVisServer ?? |
| 132 | + (async (opts) => { |
| 133 | + // Dynamic import keeps the vis server (and Hono) out of the hot path. |
| 134 | + const { startVisServer } = await import('@moonshot-ai/vis-server/start'); |
| 135 | + return startVisServer(opts); |
| 136 | + }), |
| 137 | + // `openUrl` is a synchronous fire-and-forget; adapt it to the async dep. |
| 138 | + openUrl: |
| 139 | + overrides.openUrl ?? |
| 140 | + (async (url: string) => { |
| 141 | + openUrl(url); |
| 142 | + }), |
| 143 | + waitForShutdown: overrides.waitForShutdown ?? waitForSigint, |
| 144 | + stdout: overrides.stdout ?? process.stdout, |
| 145 | + stderr: overrides.stderr ?? process.stderr, |
| 146 | + exit: overrides.exit ?? ((code: number) => process.exit(code)), |
| 147 | + }; |
| 148 | +} |
| 149 | + |
| 150 | +function waitForSigint(): Promise<void> { |
| 151 | + return new Promise<void>((resolve) => { |
| 152 | + const onSig = (): void => { |
| 153 | + process.off('SIGINT', onSig); |
| 154 | + resolve(); |
| 155 | + }; |
| 156 | + process.on('SIGINT', onSig); |
| 157 | + }); |
| 158 | +} |
0 commit comments