Skip to content
Merged
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
13 changes: 11 additions & 2 deletions apps/kimi-code/src/cli/sub/web/remote-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { acquireRemoteControlLock } from './remote-control-lock';

export const REMOTE_CONTROL_RELAY_ORIGIN = 'https://code-rc.kimi.com';

export const REMOTE_CONTROL_RELAY_URL_ENV = 'KIMI_CODE_REMOTE_CONTROL_RELAY_URL';

export const REMOTE_CONTROL_FLAG_ENV = 'KIMI_CODE_EXPERIMENTAL_REMOTE_CONTROL';

const TRUTHY_ENV_VALUES = new Set(['1', 'true', 'yes', 'on']);
Expand All @@ -31,6 +33,13 @@ export function isRemoteControlEnabled(
return truthy('KIMI_CODE_EXPERIMENTAL_FLAG') || truthy(REMOTE_CONTROL_FLAG_ENV);
}

export function resolveRemoteControlRelayOrigin(
env: Readonly<Record<string, string | undefined>> = process.env,
): string {
const value = env[REMOTE_CONTROL_RELAY_URL_ENV]?.trim();
return value === undefined || value.length === 0 ? REMOTE_CONTROL_RELAY_ORIGIN : value;
Comment on lines +36 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add a changeset for the relay override

This adds a user-perceivable configuration option to the published CLI, but the commit contains no .changeset entry, so the release changelog and package bump will omit the new environment variable. Add a patch changeset for @moonshot-ai/kimi-code that states how to use the override.

AGENTS.md reference: AGENTS.md:L85-L87

Useful? React with 👍 / 👎.

}

const MAX_HTTP_HEADER_BYTES = 64 * 1024;
const MAX_HTTP_REQUEST_BYTES = 10 * 1024 * 1024;
const HTTP_REQUEST_TIMEOUT_MS = 30_000;
Expand Down Expand Up @@ -174,7 +183,7 @@ export function formatRemoteControlStatus(status: RemoteControlStatus): string {
export function buildRemoteControlUrl(
deviceId: string,
sessionId?: string,
relayOrigin = REMOTE_CONTROL_RELAY_ORIGIN,
relayOrigin = resolveRemoteControlRelayOrigin(),
): string {
const url = new URL(relayOrigin);
const relayPath = url.pathname.replace(/\/+$/, '');
Expand Down Expand Up @@ -291,7 +300,7 @@ export async function startRemoteControl(
if (token?.refreshToken === undefined || token.refreshToken.length === 0) {
throw new Error('Remote Control requires a Kimi login. Run `kimi login` first.');
}
const relayOrigin = options.relayOrigin ?? REMOTE_CONTROL_RELAY_ORIGIN;
const relayOrigin = options.relayOrigin ?? resolveRemoteControlRelayOrigin();
const deviceId = createKimiDeviceId(options.homeDir);
const deviceName = hostname();
const url = buildRemoteControlUrl(deviceId, undefined, relayOrigin);
Expand Down
16 changes: 16 additions & 0 deletions apps/kimi-code/test/cli/web/remote-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
formatRemoteControlStatus,
isRemoteControlEnabled,
parseRawHttpRequest,
resolveRemoteControlRelayOrigin,
rewriteRemoteControlResponse,
startRemoteControl,
type RemoteControlHandle,
Expand Down Expand Up @@ -72,6 +73,21 @@ describe('Remote Control URLs', () => {
'https://code-rc.kimi.com/devices/device-1/sessions/session%2Fa%20b?rc=1&from=kimi_code_cli',
);
});

it('falls back to the default relay origin when the env is unset or blank', () => {
expect(resolveRemoteControlRelayOrigin({})).toBe('https://code-rc.kimi.com');
expect(
resolveRemoteControlRelayOrigin({ KIMI_CODE_REMOTE_CONTROL_RELAY_URL: ' ' }),
).toBe('https://code-rc.kimi.com');
});

it('builds device URLs from the relay origin env override', () => {
vi.stubEnv('KIMI_CODE_REMOTE_CONTROL_RELAY_URL', 'https://rc.example.test/coding-relay/');
expect(resolveRemoteControlRelayOrigin()).toBe('https://rc.example.test/coding-relay/');
expect(buildRemoteControlUrl('device-1')).toBe(
'https://rc.example.test/coding-relay/devices/device-1/?rc=1&from=kimi_code_cli',
);
});
});

describe('Remote Control output', () => {
Expand Down
Loading