File size: 2,263 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
// Advertise the `terminal-auth` method to ACP clients. Two paths coexist:
//
//   1. First-class `type:'terminal'` per ACP 0.23 — clients re-invoke the
//      configured agent binary appending `args` (we use `['--login']` so the
//      combined command is `<binary> <agent-args> --login`, handled by the
//      subcommand's `--login` flag).
//   2. Legacy `_meta['terminal-auth']` shape — clients that don't yet honor
//      the first-class field (Zed without `AcpBetaFeatureFlag`, current
//      JetBrains plugin, etc.) read `{command,args,env,label}` from `_meta`
//      and spawn `<command> <args>` directly.
//
// Most clients hit path 1; path 2 is required for Zed today because the
// first-class handler is beta-gated.

import type { AuthMethod } from '@agentclientprotocol/sdk';

/**
 * Build the `terminal-auth` method advertised to ACP clients.
 *
 * Optional inputs:
 *  - `env`: extra env vars forwarded to the spawned login subprocess (e.g.
 *    `{ KIMI_CODE_HOME: '/tmp/sandbox' }` so the token lands under the same
 *    data root the server reads from).
 *  - `legacyCommand`: absolute path of the agent binary, used to populate
 *    `_meta['terminal-auth'].command` so legacy clients can spawn it directly.
 *    When omitted, the `_meta` fallback is left off entirely.
 */
export function buildTerminalAuthMethod(
  opts: {
    env?: Readonly<Record<string, string>>;
    legacyCommand?: string;
  } = {},
): AuthMethod {
  const env = opts.env ?? {};
  const method: AuthMethod = {
    id: 'login',
    type: 'terminal',
    name: 'Login with Kimi account',
    description: 'Open the device-code login flow in a terminal.',
    args: ['--login'],
    env: { ...env },
  };
  if (opts.legacyCommand !== undefined && opts.legacyCommand.length > 0) {
    (method as AuthMethod & { _meta: { 'terminal-auth': unknown } })._meta = {
      'terminal-auth': {
        type: 'terminal',
        label: 'Login with Kimi account',
        command: opts.legacyCommand,
        args: ['login'],
        env: { ...env },
      },
    };
  }
  return method;
}

/**
 * Default `terminal-auth` advertisement with no env propagation and no legacy
 * `_meta` fallback.
 */
export const TERMINAL_AUTH_METHOD: AuthMethod = buildTerminalAuthMethod();