Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kap-version-mismatch-desktop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kimi-code-app": patch
---

Show an update prompt on this computer when an SSH host runs a newer incompatible Kimi Code protocol version.
5 changes: 5 additions & 0 deletions .changeset/rc-user-requested-close-callback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/remote-control": patch
---

Invoke `onUserRequestedClose` on `RemoteControlManager` when the relay closes the tunnel after a user-requested disconnect.
5 changes: 5 additions & 0 deletions .changeset/shared-kap-daemon-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": minor
---

Run `kimi web`, `kimi rc`, and TUI `/web` and `/remote-control` against a shared background daemon on macOS and Linux instead of holding the terminal until `Ctrl+C`, and add `kimi daemon status|stop|restart` and `kimi rc stop` to manage it; pass `--no-daemon` to run an independent foreground server.
5 changes: 5 additions & 0 deletions .changeset/shared-kap-daemon-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kap-server": minor
---

Support listening on both loopback TCP and a Unix domain socket with JSONL/HTTP protocol sniffing, persist daemon Remote Control preferences across restarts, expose `kap_version` on `GET /api/v1/meta`, and classify newer incompatible remote hosts as `client_outdated`.
2 changes: 2 additions & 0 deletions apps/kimi-code/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { registerLoginCommand } from './sub/login';
import { registerProviderCommand } from './sub/provider';
import { registerSessionCommand } from './sub/session';
import { registerVisCommand } from './sub/vis';
import { registerDaemonCommand } from './sub/daemon';
import { registerWebCommand } from './sub/web';

export type MainCommandHandler = (opts: CLIOptions) => void;
Expand Down Expand Up @@ -154,6 +155,7 @@ export function createProgram(
registerSessionCommand(program);
registerAcpCommand(program);
registerWebCommand(program);
registerDaemonCommand(program);
registerLoginCommand(program);
registerDoctorCommand(program);
registerVisCommand(program);
Expand Down
181 changes: 181 additions & 0 deletions apps/kimi-code/src/cli/sub/daemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import chalk from 'chalk';
import type { Command } from 'commander';

import { darkColors } from '#/tui/theme/colors';
import { getDataDir } from '#/utils/paths';

import {
getDaemonStatusSnapshot,
isSharedDaemonSupported,
restartSharedDaemon,
stopSharedDaemon,
type DaemonStatusSnapshot,
type RunningDaemonInfo,
} from './web/daemon-client';

export interface DaemonCommandDeps {
readonly homeDir?: () => string;
readonly platform?: NodeJS.Platform;
readonly getStatus?: (homeDir: string) => Promise<DaemonStatusSnapshot>;
readonly stopDaemon?: typeof stopSharedDaemon;
readonly restartDaemon?: typeof restartSharedDaemon;
readonly stdout: Pick<NodeJS.WriteStream, 'write'>;
readonly stderr: Pick<NodeJS.WriteStream, 'write'>;
}

const DEFAULT_DAEMON_COMMAND_DEPS: DaemonCommandDeps = {
homeDir: getDataDir,
stdout: process.stdout,
stderr: process.stderr,
};

function assertSupportedPlatform(deps: DaemonCommandDeps): void {
if (!isSharedDaemonSupported(deps.platform ?? process.platform)) {
throw new Error(
"Kimi background daemon is not yet supported on Windows; run 'kimi web' or 'kimi rc' in the foreground.",
);
}
}

export function formatDaemonStatus(snapshot: DaemonStatusSnapshot): string {
const title = (text: string): string => chalk.bold.hex(darkColors.primary)(text);
const label = (text: string): string => chalk.bold.hex(darkColors.textDim)(text);
const ok = (text: string): string => chalk.hex(darkColors.success)(text);
const muted = (text: string): string => chalk.hex(darkColors.textMuted)(text);
const accent = (text: string): string => chalk.hex(darkColors.accent)(text);

if (!snapshot.running || snapshot.daemon === undefined) {
const lines = [
`${title('Kimi daemon')} ${muted('stopped')}`,
` ${label('Remote Control: ')}${muted(snapshot.persistedRemoteControl ? 'off (saved: enabled on next start)' : 'off')}`,
` ${label('Logs: ')}${snapshot.logPath}`,
];
return `${lines.join('\n')}\n`;
}

const d = snapshot.daemon;
const rc = snapshot.remoteControl;
const rcSummary =
rc === undefined
? muted('unknown')
: rc.enabled && rc.url !== undefined
? `${ok('on')} ${accent(rc.url)}${rc.deviceName ? ` ${muted(`(${rc.deviceName})`)}` : ''}`
: rc.state === 'starting'
? muted('starting…')
: rc.error !== undefined
? `${muted('off')} (${rc.error})`
: muted('off');

const lines = [
`${title('Kimi daemon')} ${ok('running')}`,
` ${label('Version: ')}${d.version} ${muted(`(KAP v${String(d.kapVersion)})`)}`,
` ${label('PID: ')}${d.pid !== undefined ? String(d.pid) : muted('unknown')}`,
` ${label('Started: ')}${d.startedAt || muted('unknown')}`,
` ${label('Listeners: ')}${accent(d.origin)} ${muted('·')} unix:${d.socketPath}`,
` ${label('Remote Control: ')}${rcSummary}`,
` ${label('Active sessions: ')}${String(snapshot.activeSessions.length)}`,
];
for (const session of snapshot.activeSessions) {
const summaryTitle = session.title.trim() || '(untitled)';
lines.push(` - ${session.id} [${session.activity}] ${summaryTitle}`);
}
lines.push(` ${label('Logs: ')}${snapshot.logPath}`);
return `${lines.join('\n')}\n`;
}

function formatRestartSummary(daemon: RunningDaemonInfo): string {
const pidPart = daemon.pid !== undefined ? ` (pid ${String(daemon.pid)})` : '';
return `Kimi daemon ${daemon.version}${pidPart} listening on ${daemon.origin}.\n`;
}

export async function handleDaemonStatus(
deps: DaemonCommandDeps = DEFAULT_DAEMON_COMMAND_DEPS,
): Promise<void> {
assertSupportedPlatform(deps);
const homeDir = (deps.homeDir ?? getDataDir)();
const snapshot = await (deps.getStatus ?? getDaemonStatusSnapshot)(homeDir);
deps.stdout.write(formatDaemonStatus(snapshot));
}

export async function handleDaemonStop(
opts: { force?: boolean },
deps: DaemonCommandDeps = DEFAULT_DAEMON_COMMAND_DEPS,
): Promise<void> {
assertSupportedPlatform(deps);
const homeDir = (deps.homeDir ?? getDataDir)();
const result = await (deps.stopDaemon ?? stopSharedDaemon)({
homeDir,
force: opts.force === true,
});
if (result.outcome === 'not_running') {
deps.stdout.write('Kimi daemon is not running.\n');
return;
}
if (result.outcome === 'cancelled') {
deps.stdout.write('Aborted; Kimi daemon is still running.\n');
return;
}
const pidLabel = result.pid !== undefined ? ` (pid ${String(result.pid)})` : '';
deps.stdout.write(`Kimi daemon${pidLabel} ${result.outcome}.\n`);
}

export async function handleDaemonRestart(
opts: { force?: boolean },
deps: DaemonCommandDeps = DEFAULT_DAEMON_COMMAND_DEPS,
): Promise<void> {
assertSupportedPlatform(deps);
const homeDir = (deps.homeDir ?? getDataDir)();
const result = await (deps.restartDaemon ?? restartSharedDaemon)({
homeDir,
force: opts.force === true,
});
if (result.cancelled) {
deps.stdout.write('Aborted; Kimi daemon was not restarted.\n');
return;
}
deps.stdout.write(formatRestartSummary(result.daemon));
}

export function registerDaemonCommand(program: Command): void {
const daemon = program
.command('daemon')
.description('Inspect or manage the shared background Kimi daemon.');

daemon
.command('status')
.description('Show version, PID, listeners, Remote Control state, active sessions, and log path.')
.action(async () => {
try {
await handleDaemonStatus();
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
});

daemon
.command('stop')
.description('Stop the background Kimi daemon.')
.option('-f, --force', 'Stop without confirmation even if sessions are actively running.', false)
.action(async (opts: { force?: boolean }) => {
try {
await handleDaemonStop(opts);
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
});

daemon
.command('restart')
.description('Restart the background Kimi daemon and restore persisted settings.')
.option('-f, --force', 'Restart without confirmation even if sessions are actively running.', false)
.action(async (opts: { force?: boolean }) => {
try {
await handleDaemonRestart(opts);
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
});
}
Loading
Loading