@coda-console/claude 1.14.3 → 1.15.1

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
@@ -113,13 +113,13 @@ ACP 2.6.0 只使用默认 stable v1。逐项经过真实连接的解析测试:
113
113
  两处都有时取前者。已用比例是 0 到 1 的小数,超过 1 按剩余 0;重置时间是 Unix 秒。每个窗口只留最近一次,多个会话报告同一窗口时以后到的为准。
114
114
  - **显示**:没有会话报告过(没跑过 Claude 会话,或是 API key、Bedrock、Vertex 的会话,没有这些窗口)时输出为空,「Claude」出现在「添加资源」里。
115
115
  报告过就一个资源(不写名字:文件里只有账号的哈希),每个窗口一个读数(5 小时、7 天、Opus · 7 天、Sonnet · 7 天、Fable · 7 天,按这个顺序),条画剩余,时刻是重置时间。
116
- 过了重置时间的窗口不再显示(重置后用了多少这里看不到,既不当作用满也不当作清空);全部过期时那一行写「额度记录已过期,会话再运行后更新」。每次打开都读文件。
116
+ 过了重置时间的窗口不再显示(重置后用了多少这里看不到,既不当作用满也不当作清空);全部过期时那一行写「额度记录已过期,会话再运行后更新」。记录命令 `scripts/resource.mjs` 每次运行都读文件。
117
117
  - **身份**:会话启动时 SDK 报出账号邮箱的,资源带身份 `claude:<邮箱的哈希>`(Coda 的资源页据此合并同一账号;文件里只存哈希)。
118
118
  它跟着最近启动的会话换账号:换了账号旧窗口作废,别的账号的旧会话再报来的额度不写入;拿不到邮箱的会话(如 setup token、API key)不带身份。
119
- - **登录**:`signIn` 运行 `scripts/login.mjs`,它启动插件依赖的 Claude SDK 自带的 `claude auth login --claudeai`(与会话用的是同一个,不用 `PATH` 上另一份 Claude),
119
+ - **登录**:记录里的 `signIn` 对应操作 `sign-in`,由 `scripts/sign-in.mjs` 实现,它启动插件依赖的 Claude SDK 自带的 `claude auth login --claudeai`(与会话用的是同一个,不用 `PATH` 上另一份 Claude),
120
120
  运行机不打开浏览器(`BROWSER=/usr/bin/true`),也不导入环境里的 refresh token,而是发起新的网页登录。
121
- 只把 CLI 输出里官方的授权链接(`claude.ai/oauth/authorize` 或 `claude.com/cai/oauth/authorize`,带 `state` 与 `code_challenge`)交给 Coda,其余输出不传出(可能带着授权码)。
122
- 用户贴回的授权码须是 `<码>#<state>` 且 `state` 与这次的链接相符,才交给 CLI;不符就结束登录,写「授权码不对」。CLI 没登成时写固定的「Claude 登录没有完成,请重新登录。」,不回显它的输出。
121
+ 只把 CLI 输出里官方的授权链接(`claude.ai/oauth/authorize` 或 `claude.com/cai/oauth/authorize`,带 `state` 与 `code_challenge`)用 `coda progress` 报给 Coda,其余输出不传出(可能带着授权码);报完才算登录结束,报失败登录就失败;20 秒内没有链接、或 CLI 在拿到授权码之前就退出(也不再去问授权码),原因取 CLI 说的最后一行(授权码交给 CLI 之后它的输出不传出)。
122
+ 随后 `coda ask` 要授权码(`{ input: authorizationCode }`),用户贴回的授权码须是 `<码>#<state>` 且 `state` 与这次的链接相符,才交给 CLI;不符就结束登录,写「授权码不对」。CLI 没登成时写固定的「Claude 登录没有完成,请重新登录。」,不回显它的输出。
123
123
  PKCE、令牌交换、凭据保存和续期都由 Claude CLI 负责,沿用服务账号的 `CLAUDE_CONFIG_DIR`。配置了 token 文件、OAuth token、API key 或云服务商时它们仍然优先,添加窗口里写明。
124
124
  SDK 0.3.296 的登录输出与原生二进制包布局是这里的版本边界,升级时复核。
125
125
 
package/agent/limits.mjs CHANGED
@@ -5,7 +5,7 @@ import { readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
5
5
  // profile scope, so /usage cannot be asked with it; but the CLI sends a `rate_limit_event` whenever a window's rounded
6
6
  // percentage or reset time moves, with the fraction of the window used and when it resets (SDKRateLimitInfo). Each
7
7
  // session's entry (session.mjs) writes it to `limits.json` in this plugin's state; the「Claude」resource reads it
8
- // (scripts/resources.mjs, docs/plugins.md「内置插件:Claude」).
8
+ // (scripts/resource.mjs, docs/plugins.md「内置插件:Claude」).
9
9
  // `unifiedWindows`, an internal field of newer CLIs, carries every window each time; the top-level fields, which every
10
10
  // CLI sends, only the one that limits now.
11
11
  const labels = { five_hour: '5 小时', seven_day: '7 天', seven_day_opus: 'Opus · 7 天', seven_day_sonnet: 'Sonnet · 7 天', seven_day_overage_included: 'Fable · 7 天' };
@@ -61,6 +61,7 @@ export const MESSAGES = {
61
61
  'system/task_summary': internal,
62
62
  'system/session_metadata': internal,
63
63
  'system/dev_intent': internal,
64
+ 'system/permission_check_status': internal,
64
65
  active_goal: internal,
65
66
  autocompact_state: internal,
66
67
  command_lifecycle: internal,
package/package.json CHANGED
@@ -3,7 +3,10 @@
3
3
  "id": "claude",
4
4
  "displayName": "Claude",
5
5
  "description": "用 Claude Agent SDK 跑会话",
6
- "version": "1.14.3",
6
+ "version": "1.15.1",
7
+ "engines": {
8
+ "coda": 6
9
+ },
7
10
  "categories": [
8
11
  "Agent"
9
12
  ],
@@ -61,18 +64,19 @@
61
64
  }
62
65
  }
63
66
  },
64
- "resources": [
67
+ "protocolImplementations": [
65
68
  {
66
- "id": "limits",
67
- "title": "Claude",
68
- "command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
69
- "signIn": {
70
- "command": "exec node \"$CODA_PLUGIN_PATH/scripts/login.mjs\"",
71
- "installed": "node \"$CODA_PLUGIN_PATH/scripts/login.mjs\" --installed",
72
- "input": "authorizationCode",
73
- "note": "登录保存在这台机器上,供 Claude SDK 使用;额度在会话运行后出现。这台机器若已配置 OAuth token、API key 或云服务商,会话仍优先用那些配置。"
69
+ "kind": "coda/resource@1",
70
+ "commands": {
71
+ "sign-in": "scripts/sign-in.mjs"
74
72
  }
75
73
  }
74
+ ],
75
+ "records": [
76
+ {
77
+ "kind": "coda/resource@1",
78
+ "command": "scripts/resource.mjs"
79
+ }
76
80
  ]
77
81
  },
78
82
  "license": "MIT",
package/scripts/quota.mjs CHANGED
@@ -6,7 +6,7 @@ export function resources(limits) {
6
6
  const windows = limits.quota();
7
7
  // The account the windows are of, compared across machines by Coda's group view; its email is not kept, so it has no name.
8
8
  const identity = limits.account ? { identity: `claude:${limits.account}` } : {};
9
- if (!windows.length) return { resources: [{ name: 'Claude', ...identity, error: '额度记录已过期,会话再运行后更新' }] };
9
+ if (!windows.length) return { resources: [{ name: 'Claude', ...identity, values: [], buttons: [], error: '额度记录已过期,会话再运行后更新' }] };
10
10
  // Past its reset a window is not known, neither full nor empty: what was used since is not seen from here.
11
- return { resources: [{ ...identity, values: windows.map(w => ({ label: w.label, value: `剩余 ${w.remainingPercent}%`, fraction: w.remainingPercent / 100, time: w.resetsAt })) }] };
11
+ return { resources: [{ ...identity, values: windows.map(w => ({ label: w.label, value: `剩余 ${w.remainingPercent}%`, fraction: w.remainingPercent / 100, time: w.resetsAt })), buttons: [] }] };
12
12
  }
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ // The `coda/resource@1` record of the「Claude」type, printed once: the resources from what the sessions wrote into this
3
+ // plugin's state, and whether the CLI to sign in with is there.
4
+ import { join } from 'node:path';
5
+ import { ClaudeSessionLimits } from '../agent/limits.mjs';
6
+ import { resources } from './quota.mjs';
7
+ import { claudeBinary } from './sign-in.mjs';
8
+
9
+ const limits = new ClaudeSessionLimits({ path: process.env.CODA_PLUGIN_STATE ? join(process.env.CODA_PLUGIN_STATE, 'limits.json') : null });
10
+ console.log(JSON.stringify([{ key: 'limits', spec: { type: 'limits', title: 'Claude', ...resources(limits), signIn: { installed: claudeBinary() !== null, installable: false, input: 'authorizationCode',
11
+ note: '登录保存在这台机器上,供 Claude SDK 使用;额度在会话运行后出现。这台机器若已配置 OAuth token、API key 或云服务商,会话仍优先用那些配置。' } } }]));
@@ -1,9 +1,10 @@
1
- // The「Claude」resource type's login (`signIn`): `claude auth login --claudeai` of the CLI that comes with the plugin's own Claude
2
- // SDK (the one its sessions run), never another on the PATH. Coda shows the authorization link this prints
3
- // and writes the code the page gave back to its input, once (docs/plugins.md「资源的登录」). Only the official link is
4
- // passed on, and only a code bound to it is handed to the CLI, which does the exchange, keeps and renews the credentials.
5
- // What the CLI says otherwise is not passed on: it may carry the code. `--installed` says whether that CLI is there.
6
- import { spawn } from 'node:child_process';
1
+ #!/usr/bin/env node
2
+ // The「Claude」resource type's sign-in (the `sign-in` operation of `coda/resource@1`): `claude auth login --claudeai` of the CLI
3
+ // that comes with the plugin's own Claude SDK (the one its sessions run), never another on the PATH. It reports the
4
+ // authorization link this prints (`coda progress`) and asks for the code the page gave (`coda ask`), once. Only the official
5
+ // link is passed on, and only a code bound to it is handed to the CLI, which does the exchange, keeps and renews the
6
+ // credentials. What the CLI says otherwise is not passed on: it may carry the code.
7
+ import { execFile, spawn } from 'node:child_process';
7
8
  import { createRequire } from 'node:module';
8
9
  import { resolve } from 'node:path';
9
10
  import { fileURLToPath } from 'node:url';
@@ -36,41 +37,52 @@ export function authorization(output) {
36
37
  // The code the page gives is `<code>#<state>`: one for another login is not handed over.
37
38
  export const bound = (code, url) => /^[A-Za-z0-9_-]+#[A-Za-z0-9_-]+$/.test(code) && code.split('#')[1] === url.searchParams.get('state');
38
39
 
40
+ // Coda's own command: what it prints, or why it failed (its last line).
41
+ const coda = (...args) => new Promise((done, fail) => execFile('coda', args, (error, stdout, stderr) =>
42
+ error ? fail(new Error(stderr.trim().split('\n').at(-1) || error.message)) : done(stdout)));
43
+ const lastLine = text => stripVTControlCharacters(text).split('\n').map(line => line.trim()).filter(Boolean).at(-1);
44
+
39
45
  if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
40
46
  const binary = claudeBinary();
41
- if (process.argv[2] === '--installed') process.exit(binary ? 0 : 1);
42
47
  if (!binary) { console.error('找不到 Claude SDK 自带的 CLI'); process.exit(1); }
43
48
  const env = { ...process.env, BROWSER: '/usr/bin/true', NO_COLOR: '1' };
44
49
  // auth login otherwise silently imports this token instead of opening a fresh browser login.
45
50
  delete env.CLAUDE_CODE_OAUTH_REFRESH_TOKEN; delete env.CLAUDE_CODE_OAUTH_SCOPES;
46
51
  const child = spawn(binary, ['auth', 'login', '--claudeai'], { env, stdio: ['pipe', 'pipe', 'pipe'] });
47
52
  child.stdin.on('error', () => {});
48
- let output = '', url = null, failed = null;
53
+ let output = '', url = null, reporting = Promise.resolve(), exited = false, submitted = false, failed = null;
54
+ // The first reason stays: what the CLI says when it is ended for it is not the reason.
55
+ const fail = message => { failed ??= message; child.kill(); };
56
+ // Without a link in 20 seconds, the login is over with what the CLI said last.
57
+ const silent = setTimeout(() => fail(lastLine(output) || 'Claude 登录没有完成,请重新登录。'), 20_000);
58
+ // What the CLI says is kept until the code is given to it: after that it may carry the code.
49
59
  const heard = chunk => {
50
- if (url) return;
60
+ if (submitted) return;
51
61
  output += chunk;
62
+ if (url) return;
52
63
  url = authorization(output);
53
- if (url) console.log(url.href);
64
+ if (!url) return;
65
+ clearTimeout(silent);
66
+ reporting = coda('progress', JSON.stringify({ url: url.href })).catch(error => fail(error.message));
67
+ reporting.then(async () => {
68
+ // A CLI that has ended is not asked for a code: its last line is the reason.
69
+ if (failed || exited) return;
70
+ const code = JSON.parse(await coda('ask', JSON.stringify({ input: 'authorizationCode' }))).code.trim();
71
+ if (!bound(code, url)) return fail('授权码不对:要贴这次登录的网页给出的完整授权码,请重新登录。');
72
+ submitted = true;
73
+ child.stdin.write(`${code}\n`);
74
+ }).catch(error => fail(error.message));
54
75
  };
55
76
  child.stdout.on('data', heard); child.stderr.on('data', heard);
56
- // Coda writes the code once, ending it with a newline.
57
- let typed = '';
58
- process.stdin.setEncoding('utf8').on('data', chunk => {
59
- typed += chunk;
60
- const end = typed.indexOf('\n');
61
- if (end < 0) return;
62
- const code = typed.slice(0, end).trim();
63
- process.stdin.pause();
64
- if (url && bound(code, url)) return child.stdin.write(`${code}\n`);
65
- failed = '授权码不对:要贴这次登录的网页给出的完整授权码,请重新登录。';
66
- child.kill();
67
- });
68
77
  child.on('error', () => { failed ??= 'Claude CLI 没能启动'; });
78
+ child.on('exit', () => { exited = true; });
69
79
  // Coda ends the whole group; one ended alone takes its CLI along.
70
80
  for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, () => { child.kill(); process.exit(1); });
71
- child.on('close', code => {
72
- if (code === 0 && !failed) process.exit(0);
73
- console.error(failed ?? 'Claude 登录没有完成,请重新登录。');
81
+ child.on('close', async code => {
82
+ clearTimeout(silent);
83
+ await reporting;
84
+ if (code === 0 && !failed) { console.log('{}'); process.exit(0); }
85
+ console.error(failed ?? (submitted ? null : lastLine(output)) ?? 'Claude 登录没有完成,请重新登录。');
74
86
  process.exit(1);
75
87
  });
76
88
  }
@@ -1,6 +0,0 @@
1
- // The「Claude」resources, printed once as `{ resources }`, from what the sessions wrote into this plugin's state.
2
- import { join } from 'node:path';
3
- import { ClaudeSessionLimits } from '../agent/limits.mjs';
4
- import { resources } from './quota.mjs';
5
-
6
- console.log(JSON.stringify(resources(new ClaudeSessionLimits({ path: process.env.CODA_PLUGIN_STATE ? join(process.env.CODA_PLUGIN_STATE, 'limits.json') : null }))));