@coda-console/codex 2.3.1 → 2.5.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/package.json CHANGED
@@ -3,9 +3,14 @@
3
3
  "id": "codex",
4
4
  "displayName": "Codex",
5
5
  "description": "用 OpenAI Codex 跑会话",
6
- "version": "2.3.1",
6
+ "version": "2.5.0",
7
+ "type": "module",
8
+ "main": "main.mjs",
9
+ "activationEvents": [
10
+ "onStartupFinished"
11
+ ],
7
12
  "engines": {
8
- "coda": 20
13
+ "coda": 28
9
14
  },
10
15
  "categories": [
11
16
  "Agent"
@@ -32,10 +37,13 @@
32
37
  "command": "exec \"$CODA_NODE\" \"$CODA_PLUGIN_PATH/agent/entry.mjs\"",
33
38
  "modes": {
34
39
  "title": "审批模式",
35
- "unset": "Agent 默认",
40
+ "unset": "默认(完全放开)",
36
41
  "note": "未选择时为完全访问。",
42
+ "manual": "manual",
37
43
  "auto": "agent",
38
- "full": "agent-full-access"
44
+ "full": "agent-full-access",
45
+ "safe": [],
46
+ "default": "full"
39
47
  },
40
48
  "skillsDir": ".agents/skills",
41
49
  "configFiles": [
@@ -67,10 +75,310 @@
67
75
  "order": 2,
68
76
  "default": false,
69
77
  "description": "在 Coda 里管理多个 Codex 账号:资源页的「Codex」改为每个账号一行(首次打开时导入这台机器已有的 Codex 登录),可添加、切换、移除账号和使用完全重置;会话在开始和两轮之间换到所选的账号,令牌由插件续期。关着时用这台机器上 Codex 自己的登录。"
78
+ },
79
+ "codex.accountHosting": {
80
+ "type": "string",
81
+ "enum": [
82
+ "local",
83
+ "host",
84
+ "remote"
85
+ ],
86
+ "default": "local",
87
+ "title": "账号托管",
88
+ "order": 3,
89
+ "description": "Codex 账号放在哪里:local(本机,默认):各机器用自己的 Codex 登录,开了「多账号」时用本机管理的账号;host(空间托管中心):这台机器集中保管账号并给空间里别的机器发短期令牌,别的机器设成 remote 就能用这里的账号;remote(使用空间托管):会话的令牌都向空间里的托管机领取,托管机离线时直接报错,不会退回本机登录。"
90
+ },
91
+ "codex.accountHost": {
92
+ "type": "string",
93
+ "default": "",
94
+ "title": "指定托管机",
95
+ "order": 4,
96
+ "description": "只在「账号托管」为 remote 时生效:留空时自动使用空间里唯一的托管机;空间里有多台托管机时,填要用的那台的机器 ID 或机器名。"
70
97
  }
71
98
  }
72
99
  },
100
+ "protocols": [
101
+ {
102
+ "name": "codex/account-pool@1",
103
+ "title": "Codex 账号托管中心",
104
+ "description": "空间里集中保管 Codex 账号的一台机器,每台一条:别的机器上的 Codex 插件经它领取所选账号的短期访问令牌、上报限流与额度故障。记录与操作都不含令牌、邮箱或登录凭据;令牌只经一次性请求的流 tokens 送出。",
105
+ "spec": {
106
+ "type": "object",
107
+ "properties": {},
108
+ "additionalProperties": false
109
+ },
110
+ "status": {
111
+ "type": "object",
112
+ "properties": {
113
+ "ready": {
114
+ "type": "boolean"
115
+ },
116
+ "accountCount": {
117
+ "type": "integer",
118
+ "minimum": 0
119
+ },
120
+ "hasSelection": {
121
+ "type": "boolean"
122
+ }
123
+ },
124
+ "additionalProperties": false
125
+ },
126
+ "operations": {
127
+ "status": {
128
+ "safe": true,
129
+ "description": "托管中心的池状态:账号数、所选账号的 ID(不含名字与邮箱)、各账号的登录、冷却与最近一次采集的额度摘要(各窗口剩余与重置时间、完全重置次数)。只读:不导入本机的 Codex 登录,不写记录,不含令牌;账号库读不了时 ready 为 false。",
130
+ "input": {
131
+ "type": "object",
132
+ "additionalProperties": false
133
+ },
134
+ "result": {
135
+ "type": "object",
136
+ "properties": {
137
+ "ready": {
138
+ "type": "boolean"
139
+ },
140
+ "accountCount": {
141
+ "type": "integer",
142
+ "minimum": 0
143
+ },
144
+ "hasSelection": {
145
+ "type": "boolean"
146
+ },
147
+ "selected": {
148
+ "type": [
149
+ "object",
150
+ "null"
151
+ ],
152
+ "properties": {
153
+ "id": {
154
+ "type": "string",
155
+ "minLength": 1,
156
+ "maxLength": 256
157
+ }
158
+ },
159
+ "required": [
160
+ "id"
161
+ ],
162
+ "additionalProperties": false
163
+ },
164
+ "pool": {
165
+ "type": "object",
166
+ "properties": {
167
+ "accounts": {
168
+ "type": "array",
169
+ "items": {
170
+ "type": "object",
171
+ "properties": {
172
+ "accountId": {
173
+ "type": "string"
174
+ },
175
+ "signedIn": {
176
+ "type": [
177
+ "boolean",
178
+ "null"
179
+ ]
180
+ },
181
+ "fault": {
182
+ "type": [
183
+ "string",
184
+ "null"
185
+ ]
186
+ },
187
+ "quotaAt": {
188
+ "type": [
189
+ "string",
190
+ "null"
191
+ ]
192
+ },
193
+ "quota": {
194
+ "type": [
195
+ "object",
196
+ "null"
197
+ ],
198
+ "properties": {
199
+ "windows": {
200
+ "type": "array",
201
+ "items": {
202
+ "type": "object",
203
+ "properties": {
204
+ "label": {
205
+ "type": "string"
206
+ },
207
+ "value": {
208
+ "type": "string"
209
+ },
210
+ "fraction": {
211
+ "type": "number",
212
+ "minimum": 0,
213
+ "maximum": 1
214
+ },
215
+ "time": {
216
+ "type": "string"
217
+ }
218
+ },
219
+ "required": [
220
+ "label",
221
+ "value"
222
+ ],
223
+ "additionalProperties": false
224
+ }
225
+ },
226
+ "resets": {
227
+ "type": [
228
+ "object",
229
+ "null"
230
+ ],
231
+ "properties": {
232
+ "count": {
233
+ "type": "integer"
234
+ },
235
+ "nextExpiresAt": {
236
+ "type": "string"
237
+ }
238
+ },
239
+ "required": [
240
+ "count"
241
+ ],
242
+ "additionalProperties": false
243
+ }
244
+ },
245
+ "required": [
246
+ "windows",
247
+ "resets"
248
+ ],
249
+ "additionalProperties": false
250
+ },
251
+ "cooldown": {
252
+ "type": [
253
+ "object",
254
+ "null"
255
+ ]
256
+ }
257
+ },
258
+ "required": [
259
+ "accountId",
260
+ "signedIn",
261
+ "fault",
262
+ "quotaAt",
263
+ "quota",
264
+ "cooldown"
265
+ ],
266
+ "additionalProperties": false
267
+ }
268
+ }
269
+ },
270
+ "required": [
271
+ "accounts"
272
+ ],
273
+ "additionalProperties": false
274
+ }
275
+ },
276
+ "required": [
277
+ "ready",
278
+ "accountCount",
279
+ "hasSelection",
280
+ "selected",
281
+ "pool"
282
+ ],
283
+ "additionalProperties": false
284
+ }
285
+ },
286
+ "report-fault": {
287
+ "description": "上报某个账号被限流或额度用完,托管中心据此让它冷却一段时间;只收故障的种类与时间,不收自由文本。结果是生效的冷却,没有(已过期)为 null。",
288
+ "input": {
289
+ "type": "object",
290
+ "properties": {
291
+ "accountId": {
292
+ "type": "string",
293
+ "minLength": 1,
294
+ "maxLength": 256
295
+ },
296
+ "kind": {
297
+ "enum": [
298
+ "rate_limited",
299
+ "quota_exhausted"
300
+ ]
301
+ },
302
+ "retryAfterMs": {
303
+ "type": "integer",
304
+ "minimum": 1,
305
+ "maximum": 2592000000
306
+ },
307
+ "resetAt": {
308
+ "type": "string",
309
+ "minLength": 1,
310
+ "maxLength": 64
311
+ },
312
+ "model": {
313
+ "type": "string",
314
+ "maxLength": 64
315
+ }
316
+ },
317
+ "required": [
318
+ "accountId",
319
+ "kind"
320
+ ],
321
+ "additionalProperties": false
322
+ },
323
+ "result": {
324
+ "type": [
325
+ "object",
326
+ "null"
327
+ ],
328
+ "properties": {
329
+ "kind": {
330
+ "enum": [
331
+ "rate_limited",
332
+ "quota_exhausted"
333
+ ]
334
+ },
335
+ "until": {
336
+ "type": "string"
337
+ },
338
+ "remainingMs": {
339
+ "type": "integer",
340
+ "minimum": 0
341
+ },
342
+ "model": {
343
+ "type": "string"
344
+ }
345
+ },
346
+ "required": [
347
+ "kind",
348
+ "until",
349
+ "remainingMs"
350
+ ],
351
+ "additionalProperties": false
352
+ }
353
+ }
354
+ },
355
+ "streams": {
356
+ "tokens": {
357
+ "description": "一次性请求:请求体是至多 16 KiB 的 JSON,{ op: \"selected\" } 回答 { selected: { id, name, instanceId? } }(instanceId 区分移除后同一 id 重新登录的账号);{ op: \"tokens\", account, refresh?, rejectedGeneration? } 回答 { tokens: { accessToken, chatgptAccountId, chatgptPlanType? }, generation, expiresAt }。回答带 Cache-Control: no-store;失败是 { error: { code, message, retryAfterMs?, resetAt? } },文案固定,不转述上游的话。",
358
+ "shape": "write",
359
+ "input": {
360
+ "type": "object",
361
+ "properties": {},
362
+ "additionalProperties": false
363
+ }
364
+ }
365
+ },
366
+ "permissions": {
367
+ "pool": {
368
+ "title": "向空间里的 Codex 托管机领取账号的短期令牌,上报账号故障",
369
+ "operations": [
370
+ "status",
371
+ "report-fault"
372
+ ],
373
+ "streams": [
374
+ "tokens"
375
+ ]
376
+ }
377
+ }
378
+ }
379
+ ],
73
380
  "protocolImplementations": [
381
+ "codex/account-pool@1",
74
382
  {
75
383
  "kind": "coda/resource@1",
76
384
  "commands": {
@@ -96,6 +404,8 @@
96
404
  "access": "public"
97
405
  },
98
406
  "files": [
407
+ "main.mjs",
408
+ "lib",
99
409
  "agent",
100
410
  "scripts",
101
411
  "icons"
package/scripts/act.mjs CHANGED
@@ -1,15 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
  // The actions of the buttons on the「Codex」resources (the `act` operation of `coda/resource@1`), `{ action, args }` on standard
3
- // input: a full reset of this machine's own login, or with「多账号」on (the setting `codex.multipleAccounts`), switching to, a full
4
- // reset of and removing a managed account. Anything but what is asked for is said in one line and fails the action, so its row
3
+ // input: a full reset of this machine's own login, or with「多账号」on (the setting `codex.multipleAccounts`) or as the space's hosting
4
+ // centre (`codex.accountHosting` = host), switching to, a full reset of and removing a managed account; the centre's account service
5
+ // is told of each, so that what it kept of the account (its quota, the record) is not out of date. A consumer of the centre (remote)
6
+ // has no account here to act on. Anything but what is asked for is said in one line and fails the action, so its row
5
7
  // shows it.
6
8
  import { randomUUID } from 'node:crypto';
7
9
  import { readFileSync } from 'node:fs';
8
10
  import { withAppServer } from './app-server.mjs';
9
11
  import { withAccount } from './codex.mjs';
10
- import { choose, multipleAccounts, remove, tokens, withManagedAccount } from './manager.mjs';
12
+ import { isHostMode, isRemoteMode, remoteRefusal, usesManagedStore } from '../lib/config.mjs';
13
+ import { tell } from '../lib/ipc.mjs';
14
+ import { choose, remove, tokens, withManagedAccount } from './manager.mjs';
11
15
  import { outcomes } from './quota.mjs';
12
16
 
17
+ if (isRemoteMode()) { console.error(remoteRefusal); process.exit(1); }
18
+ // What this machine's account service is to know of a change, when it is the centre.
19
+ const told = payload => isHostMode() ? tell(process.env.CODA_PLUGIN_STATE, payload) : undefined;
20
+
13
21
  // Redeems exactly the credit its button named (without one Codex would pick for itself), once, with the app-server `withServer`
14
22
  // runs it against.
15
23
  async function redeem(withServer, creditId) {
@@ -34,15 +42,16 @@ async function select({ account }) {
34
42
  await withAccount(await tokens(account), async () => {});
35
43
  await choose(account);
36
44
  } catch (error) { throw new Error(`切换失败:${error.message}`); }
45
+ await told({ op: 'invalidate', account });
37
46
  }
38
47
 
39
48
  // The「使用完全重置」button of a managed account.
40
49
  async function resetManaged({ account, creditId }) {
41
50
  if (typeof account !== 'string' || !account || typeof creditId !== 'string' || !creditId) throw new Error('没有指明用哪个账号的哪一次完全重置');
42
- await redeem(work => withManagedAccount(account, work), creditId);
51
+ try { await redeem(work => withManagedAccount(account, work), creditId); } finally { await told({ op: 'invalidate', account }); }
43
52
  }
44
53
 
45
- const actions = multipleAccounts() ? { select, reset: resetManaged, remove: ({ account }) => remove(account) } : { reset };
54
+ const actions = usesManagedStore() ? { select, reset: resetManaged, remove: async ({ account }) => { await remove(account); await told({ op: 'forget', account }); } } : { reset };
46
55
  const { action, args } = JSON.parse(readFileSync(0, 'utf8'));
47
56
  try {
48
57
  if (!Object.hasOwn(actions, action)) throw new Error(`不认识的动作:${action}`);
@@ -26,7 +26,11 @@ export class AppServer {
26
26
  return;
27
27
  }
28
28
  this.pending.delete(message.id); clearTimeout(waiter.timer);
29
- if (message.error) waiter.reject(new Error(oneLine(message.error.message) || 'Codex 拒绝了请求'));
29
+ if (message.error) {
30
+ // The line shown is cut short; what tells the kind of failure (the code, the data, the whole message) stays on the error.
31
+ const error = new Error(oneLine(message.error.message) || 'Codex 拒绝了请求');
32
+ waiter.reject(Object.assign(error, { rpcCode: message.error.code, data: message.error.data, raw: typeof message.error.message === 'string' ? message.error.message.slice(0, 4000) : undefined }));
33
+ }
30
34
  else waiter.resolve(message.result);
31
35
  });
32
36
  // Resolves how it ended: `{ code, signal }` as the process exited, or, if it could not be started at all (no such
package/scripts/cli.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  // The plugin's own Codex CLI, as the sign-in runs it. With「多账号」on (the setting `codex.multipleAccounts`), its login adds to
2
- // or updates the managed accounts instead (login.mjs).
3
- import { multipleAccounts } from './manager.mjs';
2
+ // or updates the managed accounts instead (login.mjs), as does a hosting centre's (`codex.accountHosting` = host); a consumer of the
3
+ // centre (remote) has no login of its own to make.
4
+ import { isRemoteMode, remoteRefusal, usesManagedStore } from '../lib/config.mjs';
4
5
 
5
- if (multipleAccounts() && process.argv[2] === 'login') await import('./login.mjs');
6
+ if (isRemoteMode() && process.argv[2] === 'login') { console.error(remoteRefusal); process.exit(1); }
7
+ else if (usesManagedStore() && process.argv[2] === 'login') await import('./login.mjs');
6
8
  else await import('@openai/codex/bin/codex.js');
package/scripts/login.mjs CHANGED
@@ -3,6 +3,8 @@
3
3
  import { spawn } from 'node:child_process';
4
4
  import { mkdtemp, rm } from 'node:fs/promises';
5
5
  import { join } from 'node:path';
6
+ import { isHostMode } from '../lib/config.mjs';
7
+ import { tell } from '../lib/ipc.mjs';
6
8
  import { codexCli } from './app-server.mjs';
7
9
  import { accountEnv, fileCredentials } from './codex.mjs';
8
10
  import { paths, prepare, list, saveLogin } from './manager.mjs';
@@ -23,7 +25,9 @@ try {
23
25
  });
24
26
  child = null;
25
27
  if (cancelled || code !== 0) throw new Error(cancelled ? '登录已取消' : '登录未完成,请重试');
26
- await saveLogin(home, at, { isCancelled: () => cancelled });
28
+ const record = await saveLogin(home, at, { isCancelled: () => cancelled });
29
+ // The centre's account service takes the account anew (its quota, the record of how many there are).
30
+ if (isHostMode()) await tell(process.env.CODA_PLUGIN_STATE, { op: 'invalidate', account: record.identity });
27
31
  } catch (error) { console.error(error.message); process.exitCode = 1; }
28
32
  finally {
29
33
  if (home) await rm(home, { recursive: true, force: true });