@coda-console/codex 1.13.1 → 1.14.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 +2 -0
- package/agent/compaction.mjs +39 -0
- package/agent/entry.mjs +16 -5
- package/agent/process-exit.mjs +26 -6
- package/agent/registry.json +2 -2
- package/agent/waits.mjs +15 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
# Codex
|
|
2
2
|
|
|
3
3
|
Codex Agent:Coda 自带的入口运行 codex-acp,报告子 Agent、后台命令与命令的自动审核。额度与登录由账号来源插件接管;未接入账号来源时,资源页的「Codex」显示 Codex 自己的登录与额度。
|
|
4
|
+
|
|
5
|
+
实时的上下文压缩(自动或手动)作为 Coda 的「整理上下文」记录显示:开始报 `compaction_update` in_progress、完成报 completed,Coda 据此知道会话压缩过;回合失败或中断、压缩未完成时补报 failed,不再出「Compact conversation」工具卡。恢复会话时,历史里的压缩仍显示为「Compact conversation」工具卡,不上报,免得 Coda 把它当成新的压缩。
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Codex's context compaction is told to Coda as ACP `compaction_update` (Coda keeps one 「整理上下文」 record per compactionId and
|
|
2
|
+
// notes a compaction when one completes), not as codex-acp's tool card or its "context compacted" text. The locked Codex says
|
|
3
|
+
// a compaction as an item (`contextCompaction`, started and completed, its id the compactionId); the deprecated
|
|
4
|
+
// `thread/compacted` carries no item and only its turn, so the one of the two a turn's compaction came as is the one told:
|
|
5
|
+
// the other, if it comes too, is not, and a compaction is never counted twice. One started whose turn ends failed or
|
|
6
|
+
// interrupted without completing it is told `failed` then, so Coda's record does not stay in progress.
|
|
7
|
+
const turns = new WeakMap();
|
|
8
|
+
|
|
9
|
+
// `event` is the item event or the legacy notification's params; `status` the item's (`in_progress`, `completed`), none for the notification.
|
|
10
|
+
export function codexCompactionUpdate(handler, event, status) {
|
|
11
|
+
const source = event.item ? 'item' : 'legacy', last = turns.get(handler);
|
|
12
|
+
if (last?.turnId === event.turnId && last.source !== source) return null;
|
|
13
|
+
turns.set(handler, { turnId: event.turnId, source, open: status === 'in_progress' ? event.item.id : null });
|
|
14
|
+
return { sessionUpdate: 'compaction_update', compactionId: event.item?.id ?? `${event.turnId}:compacted`, status: status ?? 'completed' };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The turn's end, as `turn/completed` says it.
|
|
18
|
+
export function codexCompactionEnded(handler, turn) {
|
|
19
|
+
const state = turns.get(handler), id = state?.open;
|
|
20
|
+
if (!id) return null;
|
|
21
|
+
state.open = null;
|
|
22
|
+
return turn.status === 'completed' ? null : { sessionUpdate: 'compaction_update', compactionId: id, status: 'failed', error: turn.error?.message ?? null };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function reportedCodexCompactions(source) {
|
|
26
|
+
const patches = [
|
|
27
|
+
[' return createContextCompactionStartUpdate(event.item);', 'codexCompactionUpdate(this, event, "in_progress")'],
|
|
28
|
+
[' return createContextCompactionCompleteUpdate(event.item);', 'codexCompactionUpdate(this, event, "completed")'],
|
|
29
|
+
[' return this.createContextCompactedEvent();', 'codexCompactionUpdate(this, notification.params)'],
|
|
30
|
+
];
|
|
31
|
+
for (const [before, call] of patches) {
|
|
32
|
+
if (source.split(before).length !== 2) throw new Error('codex-acp compaction patch no longer matches the locked adapter');
|
|
33
|
+
source = source.replace(before, ` return ${call};`);
|
|
34
|
+
}
|
|
35
|
+
const ended = ' this.sessionState.currentTurnId = null;\n return null;\n case "thread/tokenUsage/updated":';
|
|
36
|
+
if (source.split(ended).length !== 2) throw new Error('codex-acp compaction patch no longer matches the locked adapter');
|
|
37
|
+
source = source.replace(ended, ended.replace('return null;', 'return codexCompactionEnded(this, notification.params.turn);'));
|
|
38
|
+
return `import { codexCompactionUpdate, codexCompactionEnded } from ${JSON.stringify(import.meta.url)};\n${source.replace(/^#![^\n]*\n/, '')}`;
|
|
39
|
+
}
|
package/agent/entry.mjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// patches, which fail the start once the adapter changes. Between it and Coda the entry speaks Coda's protocol for it
|
|
3
3
|
// (waits.mjs, docs/agent-protocol.md), and of its own check of a call as that call's (reviews.mjs): its session instructions become the thread's developer instructions (instructions.mjs),
|
|
4
4
|
// and a steer that finds no turn going is answered `promptRequired` (steering.mjs); Codex's async messages are shown and its
|
|
5
|
-
// async questions asked as cards whose answers it is given within the same turn (questions.mjs);
|
|
5
|
+
// async questions asked as cards whose answers it is given within the same turn (questions.mjs); its context compactions told as
|
|
6
|
+
// `compaction_update` (compaction.mjs); what of Codex is not adapted
|
|
6
7
|
// is told to Coda (unrecognized.mjs); the models of the model service chosen in Coda join Codex's own (providers.mjs). What Coda wants of Codex beyond ACP is set here: the mode a session starts in, and
|
|
7
8
|
// which Codex program runs, through the account bridge when another plugin chooses the account (accounts.mjs).
|
|
8
9
|
import { createRequire, registerHooks } from 'node:module';
|
|
@@ -15,7 +16,7 @@ import { accountSource } from './account-source.mjs';
|
|
|
15
16
|
import { readLines } from './lines.mjs';
|
|
16
17
|
import { orderedCodexTitles } from './titles.mjs';
|
|
17
18
|
import { continuedCodexSubagents } from './subagents.mjs';
|
|
18
|
-
import { followCodexProcessExit } from './process-exit.mjs';
|
|
19
|
+
import { describeExit, followCodexProcessExit, listenExit } from './process-exit.mjs';
|
|
19
20
|
import { networkedCodexDevelopment } from './network.mjs';
|
|
20
21
|
import { codexOptionsAsShown } from './options.mjs';
|
|
21
22
|
import { instructedCodexThreads } from './instructions.mjs';
|
|
@@ -26,6 +27,7 @@ import { CodexWaits } from './waits.mjs';
|
|
|
26
27
|
import { CodexReviews } from './reviews.mjs';
|
|
27
28
|
import { explainCodexError, reportedCodexFailures } from './errors.mjs';
|
|
28
29
|
import { reportedCodexSleeps } from './sleep.mjs';
|
|
30
|
+
import { reportedCodexCompactions } from './compaction.mjs';
|
|
29
31
|
import { codexDisplayNames } from './display.mjs';
|
|
30
32
|
import { trustedCodaMcpRequests } from './mcp-policy.mjs';
|
|
31
33
|
import { CodexQuestions, shownCodexAsyncMessages } from './questions.mjs';
|
|
@@ -65,20 +67,29 @@ const pass = ({ agent, coda }) => { for (const each of agent) toCodex(each); for
|
|
|
65
67
|
listen(line => {
|
|
66
68
|
for (const message of waits.fromAgent(parse(line))) for (const told of unrecognized.fromAgent(message)) for (const each of reviews.fromAgent(told)) pass(questions.fromAgent(each));
|
|
67
69
|
});
|
|
70
|
+
// The Codex process ended on its own, not as Coda's input closing stops it: the log and the turns waiting on it say how
|
|
71
|
+
// (process-exit.mjs).
|
|
72
|
+
let stopping = false;
|
|
73
|
+
listenExit(exit => {
|
|
74
|
+
if (stopping) return;
|
|
75
|
+
const message = describeExit(exit);
|
|
76
|
+
process.stderr.write(`${message}\n`);
|
|
77
|
+
for (const each of waits.fail(message)) say(each);
|
|
78
|
+
});
|
|
68
79
|
listenUnrecognized(report => { for (const each of unrecognized.report({ ...report, sessionId: report.sessionId ?? waits.root })) say(each); });
|
|
69
80
|
readLines(process.stdin).on('line', line => {
|
|
70
81
|
const message = parse(line);
|
|
71
82
|
if (!message || typeof message !== 'object') return;
|
|
72
83
|
unrecognized.fromCoda(message);
|
|
73
84
|
pass(questions.fromCoda(message));
|
|
74
|
-
}).on('close', () => input.end());
|
|
85
|
+
}).on('close', () => { stopping = true; input.end(); });
|
|
75
86
|
|
|
76
87
|
// codex-acp as Node finds it from here (its package names dist/index.js as its main): the plugin's own dependency.
|
|
77
88
|
const adapter = new URL(import.meta.resolve('@agentclientprotocol/codex-acp'));
|
|
78
89
|
registerHooks({ load(url, context, nextLoad) {
|
|
79
90
|
const loaded = nextLoad(url, context);
|
|
80
91
|
if (url !== adapter.href) return loaded;
|
|
81
|
-
return { ...loaded, source: reportedCodexUnrecognized(shownCodexAsyncMessages(reportedCodexSleeps(reportedCodexFailures(codexOptionsAsShown(relayedCodexStreams(orderedCodexTitles(followCodexProcessExit(continuedCodexSubagents(
|
|
82
|
-
promptRequiredCodexSteering(instructedCodexThreads(servedCodexModels(networkedCodexDevelopment(trustedCodaMcpRequests(Buffer.from(loaded.source).toString('utf8'))))))))))))))) };
|
|
92
|
+
return { ...loaded, source: reportedCodexUnrecognized(shownCodexAsyncMessages(reportedCodexCompactions(reportedCodexSleeps(reportedCodexFailures(codexOptionsAsShown(relayedCodexStreams(orderedCodexTitles(followCodexProcessExit(continuedCodexSubagents(
|
|
93
|
+
promptRequiredCodexSteering(instructedCodexThreads(servedCodexModels(networkedCodexDevelopment(trustedCodaMcpRequests(Buffer.from(loaded.source).toString('utf8')))))))))))))))) };
|
|
83
94
|
} });
|
|
84
95
|
await import(adapter.href);
|
package/agent/process-exit.mjs
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
// codex-acp 1.12.0 outlives its Codex app-server. When that child exits on its own, the adapter only
|
|
2
2
|
// fails background tasks: a started turn keeps waiting for a completion that never comes, and every
|
|
3
3
|
// later request fails against the dead child. Follow the child out instead, so the session host reports
|
|
4
|
-
// an agent exit
|
|
5
|
-
//
|
|
6
|
-
|
|
4
|
+
// an agent exit. A deliberate provider restart moves the generation first, and stays unaffected. How the child exited (code or
|
|
5
|
+
// signal, and the end of its stderr) is told to the entry first, which logs it and answers the turns still waiting with it, so
|
|
6
|
+
// the backend settles them with that error. Fails loudly once the adapter changes, `getRecentStderr` included, which the patch calls
|
|
7
|
+
// from the exit callback.
|
|
8
|
+
const before = `.once("exit", () => {
|
|
9
|
+
if (generation !== this.codexProcessGeneration) return;
|
|
7
10
|
void this.finishAllAsyncTasks("failed", "after the Codex process exited");`;
|
|
8
11
|
// Exit only after stdout has flushed the failed background tasks.
|
|
9
|
-
const after =
|
|
12
|
+
const after = `.once("exit", (code, signal) => {
|
|
13
|
+
if (generation !== this.codexProcessGeneration) return;
|
|
14
|
+
codaProcessExit.exited({ code, signal, stderr: this.getRecentStderr() });
|
|
10
15
|
void this.finishAllAsyncTasks("failed", "after the Codex process exited").finally(() => {
|
|
11
16
|
logger.log("Codex process exited; ending the adapter with it");
|
|
12
17
|
process.stdout.write("", () => process.exit(1));
|
|
13
18
|
});`;
|
|
14
19
|
export function followCodexProcessExit(source) {
|
|
15
|
-
if (source.split(before).length !== 2) throw new Error('codex-acp process exit patch no longer matches the locked adapter');
|
|
16
|
-
return source.replace(before, after)
|
|
20
|
+
if (source.split(before).length !== 2 || !source.includes('this.getRecentStderr = ')) throw new Error('codex-acp process exit patch no longer matches the locked adapter');
|
|
21
|
+
return `import * as codaProcessExit from ${JSON.stringify(import.meta.url)};\n${source.replace(/^#![^\n]*\n/, '').replace(before, after)}`;
|
|
17
22
|
}
|
|
23
|
+
|
|
24
|
+
let heard = () => {};
|
|
25
|
+
export const listenExit = listener => { heard = listener; };
|
|
26
|
+
export const exited = exit => heard(exit);
|
|
27
|
+
// What Coda's turn error and the log say of it: how the Codex process ended, and the last lines it wrote to stderr.
|
|
28
|
+
export function describeExit({ code, signal, stderr }) {
|
|
29
|
+
const tail = stderr.trim().split('\n').slice(-5).join('\n').slice(-1000);
|
|
30
|
+
return `Codex 进程意外退出(${signal ? `被信号 ${signal} 终止` : `退出码 ${code}`})${tail ? `,stderr 末尾:\n${tail}` : ''}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The error that ends a prompt as the process ends (docs/agent-protocol.md「原生会话已结束」): the category tells Coda the native
|
|
34
|
+
// session is over, not that the service failed. `input` is not written: nothing the
|
|
35
|
+
// process said proves this prompt was read, and Coda then resumes by checking first, with the whole original input.
|
|
36
|
+
export const endedError = message => ({ code: -32603, message,
|
|
37
|
+
data: { _meta: { coda: { failure: { kind: 'session_ended' } } } } });
|
package/agent/registry.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"item:imageGeneration": { "shown": true, "why": "生成图片,作为工具调用" },
|
|
18
18
|
"item:enteredReviewMode": { "shown": false, "why": "/review 开始的标记,没有内容;结果在 exitedReviewMode" },
|
|
19
19
|
"item:exitedReviewMode": { "shown": true, "why": "/review 的结果" },
|
|
20
|
-
"item:contextCompaction": { "shown": true, "why": "
|
|
20
|
+
"item:contextCompaction": { "shown": true, "why": "压缩上下文,作为 compaction_update(开始 in_progress、完成 completed),不再出工具卡(compaction.mjs)" },
|
|
21
21
|
|
|
22
22
|
"item:agentMessage.id": { "shown": true, "why": "作为 messageId" },
|
|
23
23
|
"item:agentMessage.text": { "shown": true, "why": "正文" },
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"notification:item/reasoning/summaryTextDelta": { "shown": true, "why": "思考" },
|
|
89
89
|
"notification:item/reasoning/summaryPartAdded": { "shown": true, "why": "思考的分段" },
|
|
90
90
|
"notification:item/reasoning/textDelta": { "shown": true, "why": "思考" },
|
|
91
|
-
"notification:thread/compacted": { "shown": true, "why": "
|
|
91
|
+
"notification:thread/compacted": { "shown": true, "why": "压缩上下文(旧通知;与 item 同回合出现时只报其中先到的一个,compaction.mjs)" },
|
|
92
92
|
"notification:model/rerouted": { "shown": true, "why": "换了模型,作为思考" },
|
|
93
93
|
"notification:model/verification": { "shown": false, "why": "codex-acp 忽略" },
|
|
94
94
|
"notification:modelProvider/authRecoveryStarted": { "shown": false, "why": "模型服务的登录恢复;codex-acp 忽略" },
|
package/agent/waits.mjs
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// here and handed to the patched codex-acp in the request's `_meta` (instructions.mjs).
|
|
6
6
|
import { INSTRUCTIONS, sessionInstructions } from './instructions.mjs';
|
|
7
7
|
import { sleepTitle } from './sleep.mjs';
|
|
8
|
+
import { endedError } from './process-exit.mjs';
|
|
8
9
|
|
|
9
10
|
const record = value => value && typeof value === 'object' && !Array.isArray(value);
|
|
10
11
|
const string = value => typeof value === 'string' && value.length > 0;
|
|
@@ -51,16 +52,29 @@ export class CodexWaits {
|
|
|
51
52
|
if (wait?.stoppable) return { agent: [{ jsonrpc: '2.0', method: 'session/cancel', params: { sessionId: wait.subagentSessionId } }], coda: [{ jsonrpc: '2.0', id, result: {} }] };
|
|
52
53
|
return { agent: [], coda: [{ jsonrpc: '2.0', id, error: { code: -32602, message: '没有可以停止的等待项' } }] };
|
|
53
54
|
}
|
|
55
|
+
// The Codex process is gone: answer every prompt still waiting with the error `message`, as a session that ended. A later answer of codex-acp's own to one of them (its pending requests failing
|
|
56
|
+
// as the connection closes) is dropped.
|
|
57
|
+
fail(message) {
|
|
58
|
+
const answers = [];
|
|
59
|
+
for (const [id, asked] of this.asked) if (asked?.prompt) {
|
|
60
|
+
answers.push(...this.finishSleeps(base(asked.prompt), 'failed'), { jsonrpc: '2.0', id, error: endedError(message) });
|
|
61
|
+
this.asked.set(id, 'failed');
|
|
62
|
+
}
|
|
63
|
+
return answers;
|
|
64
|
+
}
|
|
54
65
|
// What codex-acp says, as said to Coda.
|
|
55
66
|
fromAgent(message) {
|
|
56
67
|
if (!record(message)) return [];
|
|
57
68
|
if ('id' in message && !('method' in message)) {
|
|
69
|
+
if (this.asked.get(message.id) === 'failed') { this.asked.delete(message.id); return []; }
|
|
58
70
|
const sessionId = this.asked.get(message.id)?.prompt;
|
|
59
71
|
// Cancelling or failing a turn can skip the sleep's completed item. End its waits before ending the prompt.
|
|
60
72
|
const finished = sessionId ? this.finishSleeps(base(sessionId), message.error ? 'failed' : message.result?.stopReason === 'cancelled' ? 'cancelled' : 'done') : [];
|
|
61
73
|
return [...finished, this.answer(message)];
|
|
62
74
|
}
|
|
63
|
-
if (record(message.params) && string(message.params.sessionId))
|
|
75
|
+
if (record(message.params) && string(message.params.sessionId)) {
|
|
76
|
+
message = { ...message, params: { ...message.params, sessionId: base(message.params.sessionId) } };
|
|
77
|
+
}
|
|
64
78
|
if (message.method !== 'session/update' || !record(message.params?.update)) return [message];
|
|
65
79
|
const { sessionId, update: u } = message.params;
|
|
66
80
|
const said = params => [{ jsonrpc: '2.0', method: '_coda/wait', params: { sessionId, ...params } }];
|