@coda-console/kimi 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发,读 Kimi 的会话文件报出后台命令与子 Agent;资源页的「Kimi」显示账号与额度,可从 Coda 安装和登录。
4
4
 
5
+ **审批模式**:ACP 的模式 ID 不变(`default`、`plan`、`auto`、`yolo`),清单 `modes` 是 `manual: "default"`、`auto: "yolo"`、`full: "auto"`、`safe: ["plan"]`、`default: "manual"`。Kimi 2.1.1 里 `auto` 的放行判定早于敏感文件与 `.git` 检查,`yolo` 在其之后、仍可能询问,所以实际 `auto` 比 `yolo` 更宽松:「自动审批」对应 `yolo`、「全部放行」对应 `auto`,短名同样对调(`yolo` →「自动审批」,`auto` →「全部放行」)。
6
+
5
7
  不声明 `systemPrompt`;会话指令由 Coda 后端在新会话第一次非斜杠命令的用户输入前送入单独的文本块,恢复会话不再送,插件不用处理。
6
8
 
7
9
  压缩上下文(自动,或用户的 `/compact`):Kimi 的 ACP 只用一段文字说,所以读主 Agent 的 wire(`agents/main/wire.jsonl`)里的 `full_compaction.begin`、`full_compaction.complete`、`full_compaction.cancel`,按 ACP `compaction_update` 报给 Coda:开始 `in_progress`,完成 `completed`,取消 `failed`,同一次压缩用同一个 `compactionId`。和其余会话文件观察一样,只认核对过的 Kimi 版本;本进程启动前就开始的压缩只报结束。子 Agent 的压缩不报。
@@ -10,7 +12,7 @@ Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发
10
12
 
11
13
  资源页的「Kimi」显示这台机器上 Kimi CLI 的登录与额度,可从 Coda 安装和登录。
12
14
 
13
- - **CLI**:先找安装器放的 `~/.kimi-code/bin/kimi`,再找 `PATH` 上的 `kimi`(入口跑会话、记录、登录、安装用的是同一个查找);不能执行的文件(残留的、权限不对的)不算,不会挡住 `PATH` 上能用的。
15
+ - **CLI**:用 Coda 经 `CODA_TOOLS` 交来的 `kimi` 路径(见下面「Kimi CLI」),入口跑会话、记录、登录、安装用的是同一份;记录和登录不自己找,没有交来时记录写「这台机器上找不到 Kimi CLI」,登录报「找不到 Kimi CLI」。
14
16
  - **来源**:有 `$KIMI_CODE_HOME/credentials/kimi-code.json`(默认 `~/.kimi-code`)才算登录了;没有就输出为空,「Kimi」出现在「添加资源」里。
15
17
  账号与额度都问 CLI 自己的本地服务:临时启动 `kimi web --no-open --host 127.0.0.1 --port 0`(临时工作目录,只听回环地址,不开浏览器),
16
18
  从启动输出取本次的地址和令牌,账号问 `GET /api/v1/oauth/userinfo`(只取昵称 `nickname` 与会员等级 `userLevelName`,不取手机号、邮箱),
@@ -23,5 +25,14 @@ Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发
23
25
  - **身份**:`kimi:<用户 ID>`。登录文件变了而这次没问到是谁时,沿用上次的昵称与等级,但不带身份。
24
26
  - **登录**:操作 `sign-in`(`scripts/sign-in.mjs`)运行 `kimi login`(设备码),把它打印的链接与设备码用 `coda progress` 报给 Coda(只认完整的行,两路输出各自成行;报完才算登录结束,报失败登录就失败),20 秒内没有链接就失败,原因是那时 CLI 说的最后一行;这套流程在 `scripts/device-login.mjs`。它不看 `BROWSER`,在 macOS 上直接调 `open`、Linux 上调 `xdg-open`:
25
27
  命令把只含空操作 `open`、`xdg-open` 的目录(插件状态目录里的 `no-browser/`)放在 `PATH` 最前,运行机上不弹出没人看的浏览器页面。
26
- - **安装**:操作 `install` 就是 `scripts/setup.mjs`(结果 `{}`,说明写在标准错误):`scripts/install.sh` 运行官方安装脚本 `curl -fsSL https://code.kimi.com/kimi-code/install.sh | bash`,装进 `~/.kimi-code/bin`(CLI 约 180 MB)。
27
- 运行时设 `KIMI_NO_MODIFY_PATH=1`、`PATH` 里去掉 `~/.kimi-code/bin`,脚本因此不改 shell 配置,也不改名 `PATH` 上旧版 Python 的 `kimi`。
28
+ - **安装**:操作 `install` 与清单里工具的安装命令是同一个脚本 `scripts/install.mjs`(结果 `{}`,说明写在标准错误):`scripts/install.sh` 运行官方安装脚本 `curl -fsSL https://code.kimi.com/kimi-code/install.sh | bash`,固定装 `KIMI_VERSION=2.1.1`(会话文件按它的布局读),装进 `~/.kimi-code/bin`(CLI 约 180 MB)。
29
+ 运行时设 `KIMI_NO_MODIFY_PATH=1`、`PATH` 里去掉 `~/.kimi-code/bin`,脚本因此不改 shell 配置,也不改名 `PATH` 上旧版 Python 的 `kimi`。安装器失败时退出码非零,标准错误一行说明(退出码、结束它的信号,或安装器起不来的原因)。
30
+ 脚本不查装没装好、版本够不够,那是 Coda 的事(见下面「Kimi CLI」)。
31
+
32
+ ## Kimi CLI
33
+
34
+ 插件在清单里声明依赖的外部程序 `kimi`(`contributes.tools`,需要 Coda API Level 38):Coda 在每台机器上按 `~/.kimi-code/bin/kimi`、再按 PATH 上的 `kimi` 的顺序找一次,用 `--version` 查版本,低于 `minimum`(2.1.1)算太旧。找到且版本够才启动 Agent(`agentTools`),缺了、太旧或跑不起来时拦住 Agent 并说明原因。入口、记录、登录都不自己找 CLI,只用 Coda 经 `CODA_TOOLS` 交来的那一份路径;入口没有收到就报错退出,不启动任何 CLI。
35
+
36
+ 装没装好、版本够不够,都由 Coda 判断。安装、更新插件和在插件页点「修复」时,Coda 在工具不是可用状态时运行 `scripts/install.mjs`,跑完重新找、重新查版本,仍不可用就让这次准备失败。
37
+
38
+ 最低版本只写在清单的 `minimum`。插件里的 `SUPPORTED`(`agent/observe.mjs`,2.1.1)是另一件事:会话文件的布局只核对过这个版本,所以只有握手报的版本恰是它时才读文件、声明 `waits`;别的版本(新的也一样)照常转发,但不读文件、不声明,后台任务、子 Agent、定时看不见,失败的一轮可能显示成成功;这件事每个会话对接受它的 Coda 说一次(`_coda/unrecognized`,名字 `files:version`,与另外三种对不上同一句,只有原因不同:Kimi x:版本不是核对过的,不读它的会话文件(核对过的是 2.1.1),后台任务、子 Agent、定时看不见,失败的一轮可能显示成成功)。版本对而文件布局对不上时也说一次,名字按哪里对不上分。安装脚本固定装它,测试要求清单的 `minimum` 与它相同。
package/agent/binary.mjs CHANGED
@@ -1,13 +1,8 @@
1
- // Which Kimi CLI runs: the one its installer puts in ~/.kimi-code/bin, else the first `kimi` on the PATH; the same the
2
- // 「Kimi」resource type signs in and reads the quota with (scripts/).
3
- import { accessSync, constants, statSync } from 'node:fs';
4
- import { homedir } from 'node:os';
5
- import { join } from 'node:path';
6
-
7
- // A file that can be run: a leftover that cannot is not the CLI, and does not hide the one further on the PATH.
8
- const runnable = path => { try { accessSync(path, constants.X_OK); return statSync(path).isFile(); } catch { return false; } };
9
- export function kimiBinary(env = process.env) {
10
- const installed = join(homedir(), '.kimi-code/bin/kimi');
11
- if (runnable(installed)) return installed;
12
- return (env.PATH ?? '').split(':').filter(Boolean).map(dir => join(dir, 'kimi')).find(runnable) ?? null;
1
+ // The Kimi CLI Coda found on this machine (the `kimi` tool of `contributes.tools`) and hands over as CODA_TOOLS: what the entry runs
2
+ // and what the「Kimi」resource type signs in and reads the quota with (scripts/). Null when it did not (no variable, not JSON, no such
3
+ // entry), which a record says as "not installed". Nothing here looks for one.
4
+ export function kimiTool(env = process.env) {
5
+ let path;
6
+ try { path = JSON.parse(env.CODA_TOOLS).kimi; } catch {}
7
+ return typeof path === 'string' && path ? path : null;
13
8
  }
package/agent/display.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // Compact labels belong to the adapter. Keep native IDs, full names, groups and all unrelated metadata intact.
2
- const modes = { default: '审批', plan: '规划', auto: '自动审批', yolo: '全部放行' };
2
+ const modes = { default: '审批', plan: '规划', yolo: '自动审批', auto: '全部放行' };
3
3
  const label = (value, category, id = value.value ?? value.id) => {
4
4
  const shortName = category === 'mode' ? (Object.hasOwn(modes, id) ? modes[id] : undefined)
5
5
  : category === 'thought_level' ? value.name?.replace(/^Thinking /, '')
package/agent/entry.mjs CHANGED
@@ -3,13 +3,21 @@
3
3
  // its own (not as Coda's input closing or a signal to this process stops it) with a non-zero exit code, a signal or a prompt
4
4
  // unanswered, the log and the prompts still waiting say how (exit.mjs).
5
5
  import { spawn } from 'node:child_process';
6
+ import { writeSync } from 'node:fs';
6
7
  import { readLines } from './lines.mjs';
7
- import { kimiBinary } from './binary.mjs';
8
+ import { kimiTool } from './binary.mjs';
8
9
  import { KimiRelay } from './relay.mjs';
9
10
  import { describeExit, endedError } from './exit.mjs';
10
11
 
12
+ // The Kimi CLI is the one Coda found on this machine (the `kimi` tool of `contributes.tools`) and passes as CODA_TOOLS; without it the
13
+ // entry does not look for one of its own.
14
+ const binary = kimiTool();
15
+ if (!binary) {
16
+ writeSync(2, 'Coda 没有交来 Kimi CLI 的路径:环境变量 CODA_TOOLS 缺失、不是 JSON,或没有 kimi 这一项\n');
17
+ process.exit(1);
18
+ }
11
19
  const startedAt = Date.now();
12
- const kimi = spawn(kimiBinary() ?? 'kimi', ['acp'], { stdio: ['pipe', 'pipe', 'pipe'] });
20
+ const kimi = spawn(binary, ['acp'], { stdio: ['pipe', 'pipe', 'pipe'] });
13
21
  let stderr = '';
14
22
  kimi.stderr.on('data', chunk => { process.stderr.write(chunk); stderr = (stderr + chunk).slice(-2048); });
15
23
  kimi.stdin.on('error', () => {});
package/agent/observe.mjs CHANGED
@@ -9,9 +9,10 @@ const identifier = value => typeof value === 'string' && /^[\w-]+$/.test(value);
9
9
 
10
10
  // What Kimi keeps of a session in its own files (KIMI_CODE_HOME): its tasks, its main agent's turns, the crons it scheduled
11
11
  // (`cron.add` / `cron.delete` / `cron.cursor` on the main wire) and its subagents' own conversations, which its ACP does
12
- // not tell. These files are an implementation detail, not an ACP contract. Accept only the layout inspected in 2.1.1; a
13
- // new version simply loses this observation, and says which way its files are not as understood (`unread`, relay.mjs).
14
- // The one place the version checked is written: setup installs it (scripts/setup.mjs).
12
+ // not tell. These files are an implementation detail, not an ACP contract. Accept only the layout inspected in 2.1.1; under
13
+ // another version the observation is not made, and both say so (`unread`, relay.mjs), the layout of 2.1.1 that is not as
14
+ // understood by which way it is not. SUPPORTED is also the version the install pins (scripts/install.mjs), and the manifest's
15
+ // `minimum` for the `kimi` tool, which a test holds equal to it: Coda stops the agent under anything older.
15
16
  export const SUPPORTED = '2.1.1';
16
17
  // Bounded ordinary-file reads also prevent a broken index/FIFO from holding up
17
18
  // the entry. No paths, prompts, task output or credentials leave this module.
package/agent/relay.mjs CHANGED
@@ -10,9 +10,10 @@ import { kimiDisplayNames } from './display.mjs';
10
10
  // Nothing waits on a look longer than `hold`. CODA_DISABLE_RUNTIME_PATCHES=kimi leaves a plain relay. A known quota error in
11
11
  // a prompt's answer is classified at this boundary (errors.mjs), without retrying or changing other errors; an `end_turn`
12
12
  // Kimi gave to a prompt whose turn its files say failed is passed on as that failure, and so is a -32000 to a prompt whose turn its files
13
- // say ended on a lost login (`auth.login_required`). A Kimi whose files are not as understood
14
- // (another version, another layout) is relayed as it would be anyway, but said so once a session and way, to a Coda that takes
15
- // it (`_coda/unrecognized`, docs/agent-protocol.md「没适配的东西」): the user is told what goes unseen.
13
+ // say ended on a lost login (`auth.login_required`). A Kimi of the version checked whose files are not laid out as understood
14
+ // is relayed as it would be anyway, but said so once a session and way, to a Coda that takes it (`_coda/unrecognized`,
15
+ // docs/agent-protocol.md「没适配的东西」): the user is told what goes unseen. So is another version of Kimi, whose files are not
16
+ // read at all: it is told what that costs, as the others are: the tasks, subagents and crons are not shown, and a turn that failed may show as one that went well.
16
17
  import { classifyKimiError, failedAnswer } from './errors.mjs';
17
18
  import { SUPPORTED, kimiSession } from './observe.mjs';
18
19
  import { KimiWaits } from './waits.mjs';
@@ -21,9 +22,14 @@ import { KimiTurns } from './turns.mjs';
21
22
  const record = value => value && typeof value === 'object' && !Array.isArray(value);
22
23
  // What Kimi says in a turn, as opposed to what it says of the session.
23
24
  const SAID = new Set(['agent_message_chunk', 'agent_thought_chunk', 'tool_call', 'tool_call_update', 'plan']);
24
- // Each way Kimi's files are not as understood (observe.mjs), by the name it is told by.
25
- const UNREAD = { 'files:version': '这个版本的 Kimi 没有核对过', 'files:state.version': '会话的 state.json 版本对不上',
26
- 'files:wire.protocol_version': '主 Agent 的 wire 协议版本对不上', 'files:layout': '会话文件的布局对不上' };
25
+ // Each way Kimi's files are not read as understood (observe.mjs), by the name it is told by: the sentence said of it, given Kimi's version.
26
+ const unseen = (version, why) => `Kimi ${version ?? '(版本未知)'}:${why}(核对过的是 ${SUPPORTED}),后台任务、子 Agent、定时看不见,失败的一轮可能显示成成功`;
27
+ const UNREAD = {
28
+ 'files:version': version => unseen(version, '版本不是核对过的,不读它的会话文件'),
29
+ 'files:state.version': version => unseen(version, '会话的 state.json 版本对不上'),
30
+ 'files:wire.protocol_version': version => unseen(version, '主 Agent 的 wire 协议版本对不上'),
31
+ 'files:layout': version => unseen(version, '会话文件的布局对不上'),
32
+ };
27
33
 
28
34
  export class KimiRelay {
29
35
  // `say`: a message to Coda; `tell`: one to Kimi.
@@ -101,7 +107,7 @@ export class KimiRelay {
101
107
  if (!Object.hasOwn(UNREAD, name) || !this.taken || this.told.has(key)) return;
102
108
  this.told.add(key);
103
109
  this.say({ jsonrpc: '2.0', method: '_coda/unrecognized', params: { sessionId, kind: 'output', name, lost: true,
104
- detail: `Kimi ${this.version ?? '(版本未知)'}:${UNREAD[name]}(核对过的是 ${SUPPORTED}),后台任务、子 Agent、定时看不见,失败的一轮可能显示成成功` } });
110
+ detail: UNREAD[name](this.version) } });
105
111
  }
106
112
  // Waits, at most `hold`, for a look at the files: one begun after this, when `fresh`.
107
113
  look({ fresh = false } = {}) {
package/package.json CHANGED
@@ -3,9 +3,9 @@
3
3
  "id": "kimi",
4
4
  "displayName": "Kimi Code",
5
5
  "description": "用 Kimi Code 跑会话",
6
- "version": "1.6.0",
6
+ "version": "1.8.0",
7
7
  "engines": {
8
- "coda": 6
8
+ "coda": 38
9
9
  },
10
10
  "categories": [
11
11
  "Agent"
@@ -23,8 +23,11 @@
23
23
  "modes": {
24
24
  "title": "审批模式",
25
25
  "unset": "人工审批",
26
- "auto": "auto",
27
- "full": "yolo"
26
+ "manual": "default",
27
+ "auto": "yolo",
28
+ "full": "auto",
29
+ "safe": ["plan"],
30
+ "default": "manual"
28
31
  },
29
32
  "skillsDir": ".agents/skills",
30
33
  "configFiles": [
@@ -38,7 +41,7 @@
38
41
  "kind": "coda/resource@1",
39
42
  "commands": {
40
43
  "sign-in": "scripts/sign-in.mjs",
41
- "install": "scripts/setup.mjs"
44
+ "install": "scripts/install.mjs"
42
45
  }
43
46
  }
44
47
  ],
@@ -48,9 +51,15 @@
48
51
  "command": "scripts/resource.mjs"
49
52
  }
50
53
  ],
51
- "setup": {
52
- "command": "exec \"$CODA_NODE\" \"$CODA_PLUGIN_PATH/scripts/setup.mjs\""
53
- }
54
+ "tools": {
55
+ "kimi": {
56
+ "find": ["~/.kimi-code/bin/kimi", "kimi"],
57
+ "minimum": "2.1.1",
58
+ "version": ["--version"],
59
+ "install": "exec \"$CODA_NODE\" \"$CODA_PLUGIN_PATH/scripts/install.mjs\""
60
+ }
61
+ },
62
+ "agentTools": { "kimi": ["kimi"] }
54
63
  },
55
64
  "license": "MIT",
56
65
  "repository": {
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ // The install command of the plugin's `kimi` tool (`contributes.tools`), which is also the `install` operation of `coda/resource@1`.
3
+ // Coda runs it on a machine where the tool is missing, too old or broken (when the plugin is installed or updated, and when the
4
+ // user asks for the repair), and finds the tool and asks its version again once it ends; whether it is there, and new enough, is
5
+ // Coda's to judge, not this script's. It runs Kimi's own installer (install.sh), which puts SUPPORTED, the version the relay was
6
+ // checked against, in ~/.kimi-code/bin. What it prints goes to standard error with the rest of its story; ended well, this prints
7
+ // `{}` (the operation's result). If the installer fails, one line says how.
8
+ import { spawnSync } from 'node:child_process';
9
+ import { realpathSync } from 'node:fs';
10
+ import { fileURLToPath } from 'node:url';
11
+ import { SUPPORTED } from '../agent/observe.mjs';
12
+
13
+ export const runInstaller = () => spawnSync('sh', [fileURLToPath(new URL('./install.sh', import.meta.url))], { stdio: ['ignore', 2, 2],
14
+ env: { ...process.env, KIMI_VERSION: SUPPORTED } });
15
+
16
+ // What to say on standard error when the installer fails, else null.
17
+ export function install(run = runInstaller) {
18
+ const ran = run();
19
+ if (ran.error) return `Kimi 安装器没能启动:${ran.error.code ?? ran.error.message}`;
20
+ if (ran.status === 0) return null;
21
+ return `Kimi 安装器失败(${ran.status === null ? `被信号 ${ran.signal} 结束` : `退出码 ${ran.status}`})`;
22
+ }
23
+
24
+ if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
25
+ const failed = install();
26
+ if (failed) { console.error(failed); process.exit(1); }
27
+ console.log('{}');
28
+ }
@@ -1,7 +1,7 @@
1
1
  #!/bin/sh
2
2
  # The Kimi CLI's own installer, as https://www.kimi.com/code gives it. It puts the CLI in ~/.kimi-code/bin, where Coda looks
3
- # first, and would add that to the shell's configuration and rename an older Python `kimi` it finds on the PATH: told not to
4
- # touch the configuration, with that directory not on the PATH, it leaves both alone. It installs KIMI_VERSION, which setup.mjs
3
+ # for it first, and would add that to the shell's configuration and rename an older Python `kimi` it finds on the PATH: told not to
4
+ # touch the configuration, with that directory not on the PATH, it leaves both alone. It installs KIMI_VERSION, which install.mjs
5
5
  # sets to the version the relay was checked against; the latest without it.
6
6
  PATH=$(printf '%s' "$PATH" | tr ':' '\n' | grep -vx "$HOME/.kimi-code/bin" | paste -sd: -)
7
7
  KIMI_NO_MODIFY_PATH=1 PATH="$PATH" exec bash -c 'set -o pipefail; curl -fsSL https://code.kimi.com/kimi-code/install.sh | bash'
@@ -9,12 +9,12 @@
9
9
  import { readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
10
10
  import { homedir } from 'node:os';
11
11
  import { join } from 'node:path';
12
- import { kimiBinary } from '../agent/binary.mjs';
12
+ import { kimiTool } from '../agent/binary.mjs';
13
13
  import { account, line, refused, resource } from './quota.mjs';
14
14
  import { kimiWeb } from './web.mjs';
15
15
 
16
16
  const home = process.env.KIMI_CODE_HOME || join(homedir(), '.kimi-code');
17
- const binary = kimiBinary();
17
+ const binary = kimiTool();
18
18
  const signedOut = '登录已失效,请重新登录';
19
19
  const problem = reason => ({ resources: [{ name: 'Kimi', values: [], buttons: [], error: line(reason) }] });
20
20
 
@@ -5,10 +5,10 @@
5
5
  import { spawn } from 'node:child_process';
6
6
  import { chmodSync, mkdirSync, writeFileSync } from 'node:fs';
7
7
  import { join } from 'node:path';
8
- import { kimiBinary } from '../agent/binary.mjs';
8
+ import { kimiTool } from '../agent/binary.mjs';
9
9
  import { deviceLogin } from './device-login.mjs';
10
10
 
11
- const binary = kimiBinary();
11
+ const binary = kimiTool();
12
12
  if (!binary) { console.error('这台机器上找不到 Kimi CLI'); process.exit(1); }
13
13
  const stubs = join(process.env.CODA_PLUGIN_STATE, 'no-browser');
14
14
  mkdirSync(stubs, { recursive: true });
package/scripts/setup.mjs DELETED
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env node
2
- // The plugin's setup (`contributes.setup`), which Coda runs on each machine when the user installs or updates the plugin: it
3
- // ends well once the machine can run the agent; the `install` operation of `coda/resource@1` (the「Kimi」type's) runs it too. Without a Kimi CLI that runs, or
4
- // with one older than MINIMUM (the file layout the relay reads, agent/observe.mjs), Kimi's own installer (install.sh) puts that
5
- // version (KIMI_VERSION) in ~/.kimi-code/bin, where the agent looks first; a newer one is kept. Why the machine is not ready
6
- // is said in one line on standard error; the result, when it is, is `{}`.
7
- import { spawnSync } from 'node:child_process';
8
- import { realpathSync } from 'node:fs';
9
- import { fileURLToPath } from 'node:url';
10
- import { kimiBinary } from '../agent/binary.mjs';
11
- import { SUPPORTED } from '../agent/observe.mjs';
12
-
13
- export const MINIMUM = SUPPORTED;
14
- // The first x.y.z a version command prints; whether one is older than another.
15
- export const versionIn = text => /(\d+)\.(\d+)\.(\d+)/.exec(text ?? '')?.slice(1).join('.') ?? null;
16
- export function older(version, than) {
17
- const a = version.split('.').map(Number), b = than.split('.').map(Number), i = a.findIndex((n, k) => n !== b[k]);
18
- return i >= 0 && a[i] < b[i];
19
- }
20
- const asked = binary => spawnSync(binary, ['--version'], { encoding: 'utf8', timeout: 30_000, stdio: ['ignore', 'pipe', 'pipe'] });
21
-
22
- // What the machine needs, given the CLI found (`binary`, null for none) and how its `--version` runs (`run`):
23
- // `install`, `upgrade` or `none`, and why.
24
- export function needed(binary, run = asked) {
25
- if (!binary) return { action: 'install', reason: '这台机器上没有 Kimi CLI' };
26
- const ran = run(binary);
27
- if (ran.status !== 0) return { action: 'install', reason: `Kimi CLI 无法运行(${binary})` };
28
- const version = versionIn(`${ran.stdout ?? ''}\n${ran.stderr ?? ''}`);
29
- if (!version) return { action: 'upgrade', reason: `读不出 Kimi CLI 的版本(${binary})` };
30
- if (older(version, MINIMUM)) return { action: 'upgrade', reason: `Kimi CLI ${version} 低于所需的 ${MINIMUM}` };
31
- return { action: 'none', version };
32
- }
33
-
34
- if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
35
- const before = needed(kimiBinary());
36
- if (before.action !== 'none') {
37
- // What the installer prints goes to standard error with the rest of its story.
38
- const installed = spawnSync('sh', [fileURLToPath(new URL('./install.sh', import.meta.url))], { stdio: ['ignore', 2, 2],
39
- env: { ...process.env, KIMI_VERSION: MINIMUM } });
40
- if (installed.status !== 0) { console.error(`${before.reason},安装失败(退出码 ${installed.status ?? installed.signal})`); process.exit(1); }
41
- }
42
- const after = needed(kimiBinary());
43
- if (after.action !== 'none') { console.error(after.reason); process.exit(1); }
44
- console.error(`Kimi CLI ${after.version}`);
45
- console.log('{}');
46
- }