@coda-console/codex 1.9.1 → 1.11.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 ADDED
@@ -0,0 +1,3 @@
1
+ # Codex
2
+
3
+ Codex Agent:Coda 自带的入口运行 codex-acp,报告子 Agent、后台命令与命令的自动审核。额度与登录由账号来源插件接管;未接入账号来源时,资源页的「Codex」显示 Codex 自己的登录与额度。
@@ -5,7 +5,7 @@
5
5
  // `refresh: true` asks the owner to renew a rejected access token, even before its expiry.
6
6
  // Either may answer `{ "id", "error": { "message" } }`. A source taking full ownership reports an error until an account
7
7
  // is chosen; a legacy source's `null` leaves the underlying login as it is. A source that ends is started again when asked.
8
- // An installed source owns the account panel and handles `--login` for Codex's existing login entry.
8
+ // An installed source owns the account's resources and handles `--login` for Codex's existing login entry.
9
9
  import { spawn } from 'node:child_process';
10
10
  import { dirname } from 'node:path';
11
11
  import { readLines } from './lines.mjs';
@@ -28,7 +28,7 @@ export class AccountSource {
28
28
  }
29
29
  start() {
30
30
  const { command } = this.source;
31
- // As a panel's command is run (docs/plugins.md「面板」): its plugin's directory and state, and this Node first on the PATH.
31
+ // As a resource type's command is run (docs/plugins.md「资源」): its plugin's directory and state, and this Node first on the PATH.
32
32
  const child = spawn('/bin/sh', ['-c', command], { env: sourceEnv(this.source, this.env),
33
33
  stdio: ['pipe', 'pipe', 'ignore'] });
34
34
  child.stdin.on('error', () => {});
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "name": "@coda-console/codex",
3
3
  "id": "codex",
4
4
  "displayName": "Codex",
5
- "description": "Codex Agent:Coda 自带的入口运行 codex-acp,报告子 Agent、后台命令与命令的自动审核。额度与登录由账号来源插件接管;未接入账号来源时使用 Codex 自己的登录面板。",
6
- "version": "1.9.1",
5
+ "description": "用 OpenAI Codex 跑会话",
6
+ "version": "1.11.0",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],
@@ -59,11 +59,11 @@
59
59
  }
60
60
  }
61
61
  },
62
- "panels": [
62
+ "resources": [
63
63
  {
64
64
  "id": "login",
65
65
  "title": "Codex",
66
- "command": "node \"$CODA_PLUGIN_PATH/scripts/panel.mjs\"",
66
+ "command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
67
67
  "maxAge": "PT0S",
68
68
  "actions": {
69
69
  "reset": {
@@ -79,7 +79,7 @@
79
79
  ],
80
80
  "extensionPoints": {
81
81
  "account-source": {
82
- "description": "Codex 的账号与登录入口:来源给出当前账号和令牌,响应 --login,并提供账号与额度面板;接入后 Codex 自带的额度面板隐藏。"
82
+ "description": "Codex 的账号与登录入口:来源给出当前账号和令牌,响应 --login,并提供账号与额度资源;接入后 Codex 自带的那一行不再显示。"
83
83
  }
84
84
  }
85
85
  },
package/scripts/quota.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import { createHash } from 'node:crypto';
2
2
 
3
- // What the Codex panel shows of a login (docs/plugins.md「内置插件:Codex」), from what Codex's app-server says of it:
4
- // `account/read` and `account/rateLimits/read`. Kept apart from the commands so the tests can hand it answers.
3
+ // What the「Codex」resource type shows of a login (docs/plugins.md「内置插件:Codex」), from what Codex's app-server says of
4
+ // it: `account/read` and `account/rateLimits/read`. Kept apart from the commands so the tests can hand it answers.
5
5
 
6
- const CARD_ITEMS = 20;
6
+ const VALUES = 8;
7
7
  const line = (value, max = 80) => typeof value === 'string' ? [...value.replace(/\s+/g, ' ').trim()].slice(0, max).join('') : '';
8
- // The account a card shows, compared across machines by Coda's group view (its panel cards' `identity`): who signed in
9
- // and in which ChatGPT workspace, as the quota is one person's in one workspace (one email may sign in to several, and a
10
- // workspace has many members). A hash of the email and of the workspace's account ID hashed as codex-account keeps it;
11
- // none where Codex does not say both.
8
+ // The account a resource is, compared across machines by Coda's group view (its `identity`): who signed in and in which
9
+ // ChatGPT workspace, as the quota is one person's in one workspace (one email may sign in to several, and a workspace has
10
+ // many members). A hash of the email and of the workspace's account ID hashed as codex-account keeps it; none where Codex
11
+ // does not say both.
12
12
  const hash = text => createHash('sha256').update(text).digest('hex');
13
13
  const identity = (account, workspace) => {
14
14
  const email = line(account.email, 320).toLowerCase();
@@ -19,7 +19,8 @@ const unixTime = value => typeof value === 'number' && Number.isFinite(new Date(
19
19
  // docs/agent-protocol.md「文字里的时刻」).
20
20
  const mark = seconds => `<t:${unixTime(seconds)}>`;
21
21
 
22
- // A bar per window of each limit, of what is left. A limit's name is said only when there are several.
22
+ // A value per window of each limit, of what is left and when it resets; unknown is said as unknown, never a zero. A limit's
23
+ // name is said only when there are several.
23
24
  export function windows(result) {
24
25
  const byID = result?.rateLimitsByLimitId ? Object.values(result.rateLimitsByLimitId) : [];
25
26
  const limits = (byID.length ? byID : [result?.rateLimits]).filter(Boolean);
@@ -29,9 +30,9 @@ export function windows(result) {
29
30
  const duration = minutes === 300 ? '5 小时' : minutes === 10080 ? '7 天' : Number.isSafeInteger(minutes) && minutes > 0 ? `${minutes} 分钟` : '额度';
30
31
  const used = typeof w.usedPercent === 'number' && w.usedPercent >= 0 && w.usedPercent <= 100 ? w.usedPercent : null;
31
32
  const left = used === null ? null : Math.round(100 - used), resets = unixTime(w.resetsAt);
32
- return [{ type: 'meter', label: `${limits.length > 1 ? `${line(limit.limitName ?? limit.limitId, 40) || '额度'} · ` : ''}${duration}`,
33
- ...(left === null ? {} : { value: `剩余 ${left}%`, fraction: left / 100 }),
34
- ...(resets ? { time: resets, timeLabel: '重置' } : {}) }];
33
+ return [{ label: `${limits.length > 1 ? `${line(limit.limitName ?? limit.limitId, 40) || '额度'} · ` : ''}${duration}`,
34
+ ...(left === null ? { value: '—' } : { value: `剩余 ${left}%`, fraction: left / 100 }),
35
+ ...(resets ? { time: resets } : {}) }];
35
36
  }));
36
37
  }
37
38
 
@@ -45,39 +46,33 @@ export function credits(result) {
45
46
  return { count: summary.availableCount, listed };
46
47
  }
47
48
 
48
- // The login's card: its button for a full reset (the one to expire first, named in its question), its windows, then its
49
- // resets one line each.
50
- export function card(account, result, workspace) {
49
+ // The login as a resource: its windows, then its full resets left, timed by the one to expire first; its button for a full
50
+ // reset (that one, named in its question).
51
+ export function resource(account, result, workspace) {
51
52
  const name = line(account.email) || '本机登录';
52
- const items = windows(result), resets = credits(result);
53
+ const values = windows(result), resets = credits(result), actions = [];
53
54
  if (resets) {
54
- const first = resets.listed[0];
55
- const lines = resets.listed.map((c, i) => ({ type: 'stat', label: `第 ${i + 1} 次`, value: c.expiresAt == null ? '不会过期' : `${mark(c.expiresAt)} 到期` }));
56
- const room = CARD_ITEMS - items.length - (first ? 2 : 1);
57
- items.push({ type: 'stat', label: '完全重置', value: `剩余 ${resets.count} 次` }, ...lines.slice(0, Math.max(0, room)));
58
- if (first && resets.count > 0) items.unshift({ type: 'button', label: '使用完全重置', action: 'reset', args: { creditId: first.id },
55
+ const first = resets.listed[0], expires = first?.expiresAt == null ? null : unixTime(first.expiresAt);
56
+ values.splice(VALUES - 1, Infinity, { label: '完全重置', value: `剩余 ${resets.count} 次`, ...(expires ? { time: expires } : {}) });
57
+ if (first && resets.count > 0) actions.push({ label: '使用完全重置', action: 'reset', args: { creditId: first.id },
59
58
  confirm: `使用 ${line(name, 60)} 的完全重置?\n用最早到期的那一次(${first.expiresAt == null ? '不会过期' : `${mark(first.expiresAt)} 到期`})。成功后消耗 1 次,无法撤销。` });
60
59
  }
61
- if (!items.length) items.push({ type: 'error', text: '暂时无法获取额度:Codex 没有给出额度窗口' });
62
- return { type: 'card', title: name, ...(line(account.planType) ? { subtitle: line(account.planType) } : {}), ...identity(account, workspace), items: items.slice(0, CARD_ITEMS) };
60
+ return { name, ...identity(account, workspace), values: values.slice(0, VALUES),
61
+ ...(values.length ? {} : { error: '暂时无法获取额度:Codex 没有给出额度窗口' }), ...(actions.length ? { actions } : {}) };
63
62
  }
64
63
 
65
- // The panel: nothing without a login (it is then offered under「添加资源」), a card for one. Quota Codex cannot read stays
66
- // on the card, which keeps「重新登录」 at hand; one not signed in with ChatGPT has no windows to read.
67
- export async function panel(server) {
64
+ // The resources: none without a login (the type is then offered under「添加资源」), the login for one. Quota Codex cannot
65
+ // read is said on its row, which keeps「重新登录」 at hand; one not signed in with ChatGPT has no windows to read.
66
+ export async function resources(server) {
68
67
  const { account, workspaceRouting } = await server.request('account/read', { refreshToken: false });
69
68
  const workspace = workspaceRouting?.chatgptAccountId;
70
- if (!account) return { items: [] };
71
- if (account.type !== 'chatgpt') return { items: [{ type: 'card', title: '本机登录', subtitle: line(account.type),
72
- items: [{ type: 'markdown', text: '不是用 ChatGPT 账号登录的,没有额度可显示' }] }] };
69
+ if (!account) return { resources: [] };
70
+ if (account.type !== 'chatgpt') return { resources: [{ name: '本机登录', values: [{ value: '不是 ChatGPT 登录,没有额度' }] }] };
73
71
  let result;
74
72
  try { result = await server.request('account/rateLimits/read'); }
75
- catch (error) {
76
- return { items: [{ type: 'card', title: line(account.email) || '本机登录', ...(line(account.planType) ? { subtitle: line(account.planType) } : {}), ...identity(account, workspace),
77
- items: [{ type: 'error', text: `暂时无法获取额度:${error.message}` }] }] };
78
- }
79
- return { items: [card(account, result, workspace)] };
73
+ catch (error) { return { resources: [{ name: line(account.email) || '本机登录', ...identity(account, workspace), error: line(`暂时无法获取额度:${error.message}`) }] }; }
74
+ return { resources: [resource(account, result, workspace)] };
80
75
  }
81
76
 
82
- // What a full reset came to, as one line for the panel when it did not reset anything.
77
+ // What a full reset came to, as one line on the row when it did not reset anything.
83
78
  export const outcomes = { nothingToReset: '当前额度无需重置', alreadyRedeemed: '这次重置已被使用', noCredit: '这次重置已不可用' };
package/scripts/reset.mjs CHANGED
@@ -1,11 +1,11 @@
1
- // The panel's「使用完全重置」: redeems exactly the credit its button named (without one Codex would pick for itself), once.
2
- // Anything but a reset is said in one line and fails the action, so the panel shows it.
1
+ // The「使用完全重置」button: redeems exactly the credit its button named (without one Codex would pick for itself), once.
2
+ // Anything but a reset is said in one line and fails the action, so its row shows it.
3
3
  import { randomUUID } from 'node:crypto';
4
4
  import { withAppServer } from './app-server.mjs';
5
5
  import { outcomes } from './quota.mjs';
6
6
  import { accountSource } from '../agent/account-source.mjs';
7
7
 
8
- if (accountSource()) { console.error('额度已由账号插件接管,请在账号面板使用完全重置'); process.exit(1); }
8
+ if (accountSource()) { console.error('额度已由账号插件接管,请在账号插件的那一行使用完全重置'); process.exit(1); }
9
9
 
10
10
  const { creditId } = JSON.parse(process.env.CODA_PANEL_ARGS || '{}');
11
11
  if (typeof creditId !== 'string' || !creditId) { console.error('没有指明用哪一次完全重置'); process.exit(1); }
@@ -0,0 +1,7 @@
1
+ // An account source owns the「Codex」resources. Without one, show Codex's own login as before.
2
+ import { withAppServer } from './app-server.mjs';
3
+ import { resources } from './quota.mjs';
4
+ import { accountSource } from '../agent/account-source.mjs';
5
+
6
+ try { console.log(JSON.stringify(accountSource() ? { resources: [] } : await withAppServer(resources))); }
7
+ catch (error) { console.error(error.message); process.exit(1); }
package/scripts/panel.mjs DELETED
@@ -1,7 +0,0 @@
1
- // An account source owns its quota panel. Without one, show Codex's own login as before.
2
- import { withAppServer } from './app-server.mjs';
3
- import { panel } from './quota.mjs';
4
- import { accountSource } from '../agent/account-source.mjs';
5
-
6
- try { console.log(JSON.stringify(accountSource() ? { items: [] } : await withAppServer(panel))); }
7
- catch (error) { console.error(error.message); process.exit(1); }