@coda-console/kimi 1.3.0 → 1.4.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
@@ -1,3 +1,3 @@
1
1
  # Kimi Code
2
2
 
3
- Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发,读 Kimi 的会话文件报出后台命令与子 Agent;资源页的「Kimi」面板显示账号与额度,可从 Coda 安装和登录。
3
+ Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发,读 Kimi 的会话文件报出后台命令与子 Agent;资源页的「Kimi」显示账号与额度,可从 Coda 安装和登录。
package/agent/binary.mjs CHANGED
@@ -1,5 +1,5 @@
1
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」panel signs in and reads the quota with (scripts/).
2
+ // 「Kimi」resource type signs in and reads the quota with (scripts/).
3
3
  import { existsSync } from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
5
  import { join } from 'node:path';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "kimi",
4
4
  "displayName": "Kimi Code",
5
5
  "description": "用 Kimi Code 跑会话",
6
- "version": "1.3.0",
6
+ "version": "1.4.0",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],
@@ -30,11 +30,11 @@
30
30
  "setupHint": "请在「资源」里为运行机添加 Kimi。"
31
31
  }
32
32
  ],
33
- "panels": [
33
+ "resources": [
34
34
  {
35
35
  "id": "account",
36
36
  "title": "Kimi",
37
- "command": "node \"$CODA_PLUGIN_PATH/scripts/panel.mjs\"",
37
+ "command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
38
38
  "maxAge": "PT5M",
39
39
  "signIn": {
40
40
  "command": "exec sh \"$CODA_PLUGIN_PATH/scripts/login.sh\"",
package/scripts/login.sh CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/bin/sh
2
- # The「Kimi」panel's login (`signIn`): `kimi login`, the device-code login of the CLI the agent runs. `kimi login` opens the
2
+ # The「Kimi」resource type's login (`signIn`): `kimi login`, the device-code login of the CLI the agent runs. `kimi login` opens the
3
3
  # link with `open` (`xdg-open` on Linux) whatever BROWSER says: on a machine the user is not at, that would be a page left
4
4
  # open for no one, so it finds do-nothing ones first on its PATH, in this plugin's own state.
5
5
  stubs="$CODA_PLUGIN_STATE/no-browser"
package/scripts/quota.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // What the「Kimi」panel shows (docs/plugins.md「内置插件:Kimi Code」): the signed-in account and its quota, from what the
1
+ // What the「Kimi」resource type shows (docs/plugins.md「内置插件:Kimi Code」): the signed-in account and its quota, from what the
2
2
  // Kimi CLI's local server says (`/oauth/userinfo`, `/oauth/usage`, the same getManagedUsage its /status uses).
3
3
  const line = (value, max = 80) => typeof value === 'string' ? [...value.replace(/\s+/g, ' ').trim()].slice(0, max).join('') : '';
4
4
  const instant = value => typeof value === 'string' && Number.isFinite(Date.parse(value)) ? new Date(value).toISOString() : null;
@@ -11,8 +11,8 @@ export function account(data) {
11
11
  const id = typeof data.userInfo.userId === 'string' || typeof data.userInfo.userId === 'number' ? line(String(data.userInfo.userId), 120) : '';
12
12
  return { name: line(data.userInfo.nickname), plan: line(data.userInfo.userLevelName), ...(id ? { id } : {}) };
13
13
  }
14
- // The windows the plan has (5 hours, 7 days, monthly; those it has not are skipped), then the booster balance. Bars of what
15
- // is left; a used ratio unknown draws none.
14
+ // The windows the plan has (5 hours, 7 days, monthly; those it has not are skipped), then the booster balance: values of
15
+ // what is left, with when it resets; a used ratio unknown leaves its window out.
16
16
  export function windows(data) {
17
17
  if (data?.kind !== 'ok' || !data.quota) return null;
18
18
  const usages = data.quota.usages && typeof data.quota.usages === 'object' ? data.quota.usages : {};
@@ -21,19 +21,20 @@ export function windows(data) {
21
21
  const used = ratio(usages[key]?.usedRatio);
22
22
  if (used === null) return [];
23
23
  const left = Math.round(100 - Math.min(100, used * 100)), resets = instant(usages[key].resetAt);
24
- return [{ type: 'meter', label, value: `剩余 ${left}%`, fraction: left / 100, ...(resets ? { time: resets, timeLabel: '重置' } : {}) }];
24
+ return [{ label, value: `剩余 ${left}%`, fraction: left / 100, ...(resets ? { time: resets } : {}) }];
25
25
  });
26
26
  const wallet = data.quota.extraUsage;
27
27
  const [total, left] = [wallet?.totalCents, wallet?.balanceCents].map(v => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : null);
28
28
  if (wallet && total !== null && left !== null) {
29
29
  const share = total > 0 ? Math.round(Math.min(1, left / total) * 100) : 0;
30
- meters.push({ type: 'meter', label: 'Extra Usage 余额', value: `剩余 ${share}%`, fraction: share / 100 });
30
+ meters.push({ label: 'Extra Usage 余额', value: `剩余 ${share}%`, fraction: share / 100 });
31
31
  }
32
32
  return meters;
33
33
  }
34
- export function card(who, usage) {
35
- const items = usage.error ? [{ type: 'error', text: `暂时无法获取额度:${usage.error}` }] : windows(usage.data) ?? [{ type: 'error', text: '暂时无法获取额度:Kimi 没有给出额度' }];
36
- // The account, compared across machines by Coda's group view: its user ID, which the card does not show.
37
- return { type: 'card', title: who.name || 'Kimi', ...(who.plan ? { subtitle: who.plan } : {}), ...(who.id ? { identity: `kimi:${who.id}` } : {}),
38
- items: items.length ? items : [{ type: 'error', text: '暂时无法获取额度:Kimi 没有给出额度' }] };
34
+ // The login as a resource: its nickname, its quota, or why it has none.
35
+ export function resource(who, usage) {
36
+ const values = usage.error ? [] : windows(usage.data) ?? [];
37
+ const error = usage.error ? `暂时无法获取额度:${usage.error}` : values.length ? null : '暂时无法获取额度:Kimi 没有给出额度';
38
+ // The account, compared across machines by Coda's group view: its user ID, which the row does not show.
39
+ return { name: who.name || 'Kimi', ...(who.id ? { identity: `kimi:${who.id}` } : {}), values, ...(error ? { error: line(error) } : {}) };
39
40
  }
@@ -1,4 +1,4 @@
1
- // The「Kimi」panel's reading, printed once as `{ items }`. No login file here shows nothing (the panel is then offered under
1
+ // The「Kimi」resources, printed once as `{ resources }`. No login file here shows nothing (the type is then offered under
2
2
  // 「添加资源」). Who is signed in is asked once for each state of the login file, which the CLI rewrites as it signs in and
3
3
  // renews, and kept in this plugin's state: Kimi's own upstream lookup takes seconds and gives up after 8. The quota is
4
4
  // asked each reading, of the same `kimi web`.
@@ -6,16 +6,16 @@ import { readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
6
6
  import { homedir } from 'node:os';
7
7
  import { join } from 'node:path';
8
8
  import { kimiBinary } from '../agent/binary.mjs';
9
- import { account, card } from './quota.mjs';
9
+ import { account, resource } from './quota.mjs';
10
10
  import { kimiWeb } from './web.mjs';
11
11
 
12
12
  const home = process.env.KIMI_CODE_HOME || join(homedir(), '.kimi-code');
13
13
  const print = value => console.log(JSON.stringify(value));
14
14
  let changed;
15
15
  try { changed = statSync(join(home, 'credentials/kimi-code.json')).mtimeMs; }
16
- catch (error) { if (error.code === 'ENOENT') { print({ items: [] }); process.exit(0); } console.error(`读不了 Kimi 的登录:${error.code}`); process.exit(1); }
16
+ catch (error) { if (error.code === 'ENOENT') { print({ resources: [] }); process.exit(0); } console.error(`读不了 Kimi 的登录:${error.code}`); process.exit(1); }
17
17
  const binary = kimiBinary();
18
- if (!binary) { print({ items: [{ type: 'card', title: 'Kimi', items: [{ type: 'error', text: '这台机器上找不到 Kimi CLI,用「···」里的「重新登录」安装' }] }] }); process.exit(0); }
18
+ if (!binary) { print({ resources: [{ name: 'Kimi', error: '这台机器上找不到 Kimi CLI,用「···」里的「重新登录」安装' }] }); process.exit(0); }
19
19
 
20
20
  const seenFile = process.env.CODA_PLUGIN_STATE && join(process.env.CODA_PLUGIN_STATE, 'account.json');
21
21
  let seen = null;
@@ -33,5 +33,5 @@ try {
33
33
  if (seenFile) { writeFileSync(`${seenFile}.tmp`, JSON.stringify({ changed, account: who })); renameSync(`${seenFile}.tmp`, seenFile); }
34
34
  } catch { /* Not told now: the account seen last, or the kind's own name, and asked again next time. */ }
35
35
  }
36
- print({ items: who ? [card(who, usage)] : [] });
36
+ print({ resources: who ? [resource(who, usage)] : [] });
37
37
  } catch (error) { console.error(error.message); process.exit(1); }