chatccc 0.2.52 → 0.2.53
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 +95 -256
- package/bin/chatccc.mjs +23 -23
- package/demo/ilink_echo_probe.ts +222 -222
- package/im-skills/feishu-skill/download-video.mjs +162 -162
- package/im-skills/feishu-skill/receive-send-file.md +66 -66
- package/im-skills/feishu-skill/receive-send-image.md +27 -27
- package/im-skills/feishu-skill/send-file.mjs +50 -50
- package/im-skills/feishu-skill/send-image.mjs +50 -50
- package/im-skills/feishu-skill/skill.md +10 -10
- package/package.json +59 -59
- package/src/__tests__/agent-image-rpc.test.ts +33 -33
- package/src/__tests__/agent-rpc-body.test.ts +41 -41
- package/src/__tests__/card-plain-text.test.ts +42 -42
- package/src/__tests__/codex-adapter.test.ts +304 -304
- package/src/__tests__/config-reload.test.ts +26 -26
- package/src/__tests__/config-sample.test.ts +19 -19
- package/src/__tests__/crash-logging.test.ts +62 -62
- package/src/__tests__/fixtures/codex_simple_text.jsonl +4 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +6 -6
- package/src/__tests__/im-skills.test.ts +46 -46
- package/src/__tests__/session.test.ts +57 -2
- package/src/__tests__/sim-agent.test.ts +173 -173
- package/src/__tests__/sim-platform.test.ts +76 -76
- package/src/__tests__/sim-store.test.ts +213 -213
- package/src/__tests__/stream-state.test.ts +134 -134
- package/src/__tests__/wechat-platform.test.ts +57 -32
- package/src/adapters/codex-adapter.ts +294 -294
- package/src/adapters/codex-session-meta-store.ts +130 -130
- package/src/agent-file-rpc.ts +156 -156
- package/src/agent-image-rpc.ts +152 -152
- package/src/agent-rpc-body.ts +48 -48
- package/src/card-plain-text.ts +108 -108
- package/src/im-skills.ts +109 -109
- package/src/platform-adapter.ts +60 -60
- package/src/session-chat-binding.ts +6 -1
- package/src/session.ts +40 -35
- package/src/sim-agent.ts +167 -167
- package/src/trace.ts +50 -50
- package/src/wechat-platform.ts +1 -1
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
// =============================================================================
|
|
2
|
-
// codex-adapter.ts — OpenAI Codex CLI 适配器
|
|
3
|
-
// =============================================================================
|
|
4
|
-
// 通过 codex exec --json 与 Codex CLI 交互。
|
|
5
|
-
// - createSession: 生成 UUID sessionId,记录 cwd,不创建 Codex 线程(延迟到首次 prompt)
|
|
6
|
-
// - prompt: 首次调用用 codex exec 创建线程,后续用 codex exec resume 恢复
|
|
7
|
-
// - getSessionInfo: 从持久化映射读取 cwd / threadId
|
|
8
|
-
// =============================================================================
|
|
9
|
-
|
|
10
|
-
import { spawn, type ChildProcess } from "node:child_process";
|
|
11
|
-
import { createInterface } from "node:readline";
|
|
12
|
-
import { randomUUID } from "node:crypto";
|
|
13
|
-
|
|
14
|
-
import type {
|
|
15
|
-
ToolAdapter,
|
|
16
|
-
UnifiedBlock,
|
|
17
|
-
UnifiedStreamMessage,
|
|
18
|
-
CreateSessionResult,
|
|
19
|
-
SessionInfo,
|
|
20
|
-
} from "./adapter-interface.ts";
|
|
21
|
-
import {
|
|
22
|
-
defaultCodexSessionMetaStore,
|
|
23
|
-
type CodexSessionMetaStore,
|
|
24
|
-
} from "./codex-session-meta-store.ts";
|
|
25
|
-
import { config } from "../config.ts";
|
|
26
|
-
|
|
27
|
-
// ---------------------------------------------------------------------------
|
|
28
|
-
// 命令与参数
|
|
29
|
-
// ---------------------------------------------------------------------------
|
|
30
|
-
|
|
31
|
-
/** 可通过 config.json codex.path 自定义 Codex 可执行文件路径 */
|
|
32
|
-
function detectCodexCommand(): string {
|
|
33
|
-
return config.codex.path || "codex";
|
|
34
|
-
}
|
|
35
|
-
const CODEX_COMMAND = detectCodexCommand();
|
|
36
|
-
|
|
37
|
-
/** exec 模式共用参数:JSONL 输出、绕过沙盒和确认、跳过 git 仓库检查 */
|
|
38
|
-
const CODEX_BASE_ARGS = [
|
|
39
|
-
"exec",
|
|
40
|
-
"--json",
|
|
41
|
-
"--dangerously-bypass-approvals-and-sandbox",
|
|
42
|
-
"--skip-git-repo-check",
|
|
43
|
-
];
|
|
44
|
-
|
|
45
|
-
/** codex 模型;留空("")表示不传 --model,由 codex config.toml 决定 */
|
|
46
|
-
function resolveCodexModel(): string | null {
|
|
47
|
-
const m = config.codex.model;
|
|
48
|
-
return m.trim() !== "" ? m : null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** codex 努力程度(映射为 -c model_reasoning_effort=<value>);留空表示不传 */
|
|
52
|
-
function resolveCodexEffort(): string | null {
|
|
53
|
-
const e = config.codex.effort;
|
|
54
|
-
return e.trim() !== "" ? e : null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// ---------------------------------------------------------------------------
|
|
58
|
-
// 类型:Codex JSONL 消息行
|
|
59
|
-
// ---------------------------------------------------------------------------
|
|
60
|
-
|
|
61
|
-
interface CodexItem {
|
|
62
|
-
id?: string;
|
|
63
|
-
type?: string;
|
|
64
|
-
text?: string;
|
|
65
|
-
command?: string;
|
|
66
|
-
aggregated_output?: string;
|
|
67
|
-
exit_code?: number | null;
|
|
68
|
-
status?: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
interface CodexEvent {
|
|
72
|
-
type: string;
|
|
73
|
-
thread_id?: string;
|
|
74
|
-
item?: CodexItem;
|
|
75
|
-
usage?: {
|
|
76
|
-
input_tokens?: number;
|
|
77
|
-
cached_input_tokens?: number;
|
|
78
|
-
output_tokens?: number;
|
|
79
|
-
reasoning_output_tokens?: number;
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// ---------------------------------------------------------------------------
|
|
84
|
-
// normalizeCodexMessage — Codex 事件 → UnifiedStreamMessage | null
|
|
85
|
-
// ---------------------------------------------------------------------------
|
|
86
|
-
|
|
87
|
-
export function normalizeCodexMessage(
|
|
88
|
-
msg: CodexEvent,
|
|
89
|
-
): UnifiedStreamMessage | null {
|
|
90
|
-
// agent_message 文本回复
|
|
91
|
-
if (
|
|
92
|
-
msg.type === "item.completed" &&
|
|
93
|
-
msg.item?.type === "agent_message" &&
|
|
94
|
-
msg.item.text
|
|
95
|
-
) {
|
|
96
|
-
return {
|
|
97
|
-
type: "assistant",
|
|
98
|
-
blocks: [{ type: "text", text: msg.item.text }],
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// command_execution 工具调用开始
|
|
103
|
-
if (
|
|
104
|
-
msg.type === "item.started" &&
|
|
105
|
-
msg.item?.type === "command_execution" &&
|
|
106
|
-
msg.item.command
|
|
107
|
-
) {
|
|
108
|
-
return {
|
|
109
|
-
type: "assistant",
|
|
110
|
-
blocks: [
|
|
111
|
-
{
|
|
112
|
-
type: "tool_use",
|
|
113
|
-
name: "Bash",
|
|
114
|
-
input: { command: msg.item.command },
|
|
115
|
-
},
|
|
116
|
-
],
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// command_execution 工具调用完成
|
|
121
|
-
if (
|
|
122
|
-
msg.type === "item.completed" &&
|
|
123
|
-
msg.item?.type === "command_execution"
|
|
124
|
-
) {
|
|
125
|
-
const exitCode = msg.item.exit_code;
|
|
126
|
-
return {
|
|
127
|
-
type: "assistant",
|
|
128
|
-
blocks: [
|
|
129
|
-
{
|
|
130
|
-
type: "tool_result",
|
|
131
|
-
tool_use_id: msg.item.id ?? "",
|
|
132
|
-
content: msg.item.aggregated_output ?? "",
|
|
133
|
-
is_error: exitCode != null && exitCode !== 0 ? true : undefined,
|
|
134
|
-
},
|
|
135
|
-
],
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// thread.started / turn.started / turn.completed → 不映射为用户可见消息
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ---------------------------------------------------------------------------
|
|
144
|
-
// 子进程辅助函数
|
|
145
|
-
// ---------------------------------------------------------------------------
|
|
146
|
-
|
|
147
|
-
function spawnCodex(
|
|
148
|
-
args: string[],
|
|
149
|
-
cwd?: string,
|
|
150
|
-
stdinText?: string,
|
|
151
|
-
): ChildProcess {
|
|
152
|
-
const allArgs = [...args];
|
|
153
|
-
const model = resolveCodexModel();
|
|
154
|
-
if (model) {
|
|
155
|
-
// 把 -m 插在 exec 后面、其他参数前面
|
|
156
|
-
const execIdx = allArgs.indexOf("exec");
|
|
157
|
-
allArgs.splice(execIdx + 1, 0, "-m", model);
|
|
158
|
-
}
|
|
159
|
-
const effort = resolveCodexEffort();
|
|
160
|
-
if (effort) {
|
|
161
|
-
allArgs.push("-c", `model_reasoning_effort="${effort}"`);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const proc = spawn(CODEX_COMMAND, allArgs, {
|
|
165
|
-
cwd,
|
|
166
|
-
stdio: [stdinText !== undefined ? "pipe" : "ignore", "pipe", "pipe"],
|
|
167
|
-
windowsHide: true,
|
|
168
|
-
shell: true,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
let stderr = "";
|
|
172
|
-
proc.stderr!.on("data", (chunk: Buffer) => {
|
|
173
|
-
stderr += chunk.toString();
|
|
174
|
-
});
|
|
175
|
-
proc.on("close", (code) => {
|
|
176
|
-
if (code !== 0 && stderr.trim()) {
|
|
177
|
-
console.error(
|
|
178
|
-
`[Codex stderr] exit=${code}: ${stderr.trim().slice(0, 2000)}`,
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
if (stdinText !== undefined) {
|
|
184
|
-
proc.stdin!.write(stdinText);
|
|
185
|
-
proc.stdin!.end();
|
|
186
|
-
}
|
|
187
|
-
return proc;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async function* readJsonLines(
|
|
191
|
-
proc: ChildProcess,
|
|
192
|
-
signal?: AbortSignal,
|
|
193
|
-
): AsyncGenerator<CodexEvent> {
|
|
194
|
-
const rl = createInterface({ input: proc.stdout!, crlfDelay: Infinity });
|
|
195
|
-
for await (const line of rl) {
|
|
196
|
-
if (signal?.aborted) break;
|
|
197
|
-
const trimmed = line.trim();
|
|
198
|
-
if (!trimmed) continue;
|
|
199
|
-
try {
|
|
200
|
-
yield JSON.parse(trimmed) as CodexEvent;
|
|
201
|
-
} catch {
|
|
202
|
-
// 非 JSON 行静默跳过(如 "Reading prompt from stdin...")
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// ---------------------------------------------------------------------------
|
|
208
|
-
// 适配器实现
|
|
209
|
-
// ---------------------------------------------------------------------------
|
|
210
|
-
|
|
211
|
-
class CodexAdapter implements ToolAdapter {
|
|
212
|
-
readonly displayName = "Codex";
|
|
213
|
-
readonly sessionDescPrefix = "Codex Session:";
|
|
214
|
-
private metaStore: CodexSessionMetaStore;
|
|
215
|
-
|
|
216
|
-
constructor(metaStore: CodexSessionMetaStore) {
|
|
217
|
-
this.metaStore = metaStore;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// createSession: 生成 sessionId,记录 cwd,不创建 Codex 线程(延迟到首次 prompt)
|
|
221
|
-
async createSession(cwd: string): Promise<CreateSessionResult> {
|
|
222
|
-
const sessionId = randomUUID();
|
|
223
|
-
await this.metaStore.set(sessionId, { cwd });
|
|
224
|
-
return { sessionId };
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
async *prompt(
|
|
228
|
-
sessionId: string,
|
|
229
|
-
userText: string,
|
|
230
|
-
cwd: string,
|
|
231
|
-
signal?: AbortSignal,
|
|
232
|
-
): AsyncIterable<UnifiedStreamMessage> {
|
|
233
|
-
let meta = await this.metaStore.get(sessionId);
|
|
234
|
-
const threadId = meta?.threadId;
|
|
235
|
-
const isFirstPrompt = !threadId;
|
|
236
|
-
|
|
237
|
-
// 首次 prompt: codex exec 创建新线程
|
|
238
|
-
// 后续 prompt: codex exec resume 恢复已有线程(resume 不接受 -C,cwd 继承自原线程)
|
|
239
|
-
const args = isFirstPrompt
|
|
240
|
-
? [...CODEX_BASE_ARGS, "-C", cwd, "-"]
|
|
241
|
-
: [...CODEX_BASE_ARGS, "resume", threadId, "-"];
|
|
242
|
-
|
|
243
|
-
const proc = spawnCodex(args, cwd, userText);
|
|
244
|
-
|
|
245
|
-
try {
|
|
246
|
-
for await (const raw of readJsonLines(proc, signal)) {
|
|
247
|
-
if (signal?.aborted) break;
|
|
248
|
-
|
|
249
|
-
// 首次 prompt 时从 thread.started 事件学习 threadId
|
|
250
|
-
if (
|
|
251
|
-
isFirstPrompt &&
|
|
252
|
-
raw.type === "thread.started" &&
|
|
253
|
-
raw.thread_id
|
|
254
|
-
) {
|
|
255
|
-
void this.metaStore
|
|
256
|
-
.setThreadId(sessionId, raw.thread_id)
|
|
257
|
-
.catch(() => {});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const normalized = normalizeCodexMessage(raw);
|
|
261
|
-
if (normalized) yield normalized;
|
|
262
|
-
}
|
|
263
|
-
} finally {
|
|
264
|
-
proc.kill();
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async getSessionInfo(
|
|
269
|
-
sessionId: string,
|
|
270
|
-
): Promise<SessionInfo | undefined> {
|
|
271
|
-
const meta = await this.metaStore.get(sessionId);
|
|
272
|
-
if (!meta) return undefined;
|
|
273
|
-
return { sessionId, cwd: meta.cwd };
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async closeSession(_sessionId: string): Promise<void> {
|
|
277
|
-
// no-op:子进程由 prompt 的 finally 自动 kill
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// ---------------------------------------------------------------------------
|
|
282
|
-
// 工厂函数
|
|
283
|
-
// ---------------------------------------------------------------------------
|
|
284
|
-
|
|
285
|
-
export interface CreateCodexAdapterOptions {
|
|
286
|
-
metaStore?: CodexSessionMetaStore;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export function createCodexAdapter(
|
|
290
|
-
options: CreateCodexAdapterOptions = {},
|
|
291
|
-
): ToolAdapter {
|
|
292
|
-
return new CodexAdapter(
|
|
293
|
-
options.metaStore ?? defaultCodexSessionMetaStore,
|
|
294
|
-
);
|
|
1
|
+
// =============================================================================
|
|
2
|
+
// codex-adapter.ts — OpenAI Codex CLI 适配器
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// 通过 codex exec --json 与 Codex CLI 交互。
|
|
5
|
+
// - createSession: 生成 UUID sessionId,记录 cwd,不创建 Codex 线程(延迟到首次 prompt)
|
|
6
|
+
// - prompt: 首次调用用 codex exec 创建线程,后续用 codex exec resume 恢复
|
|
7
|
+
// - getSessionInfo: 从持久化映射读取 cwd / threadId
|
|
8
|
+
// =============================================================================
|
|
9
|
+
|
|
10
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
11
|
+
import { createInterface } from "node:readline";
|
|
12
|
+
import { randomUUID } from "node:crypto";
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
ToolAdapter,
|
|
16
|
+
UnifiedBlock,
|
|
17
|
+
UnifiedStreamMessage,
|
|
18
|
+
CreateSessionResult,
|
|
19
|
+
SessionInfo,
|
|
20
|
+
} from "./adapter-interface.ts";
|
|
21
|
+
import {
|
|
22
|
+
defaultCodexSessionMetaStore,
|
|
23
|
+
type CodexSessionMetaStore,
|
|
24
|
+
} from "./codex-session-meta-store.ts";
|
|
25
|
+
import { config } from "../config.ts";
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// 命令与参数
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
/** 可通过 config.json codex.path 自定义 Codex 可执行文件路径 */
|
|
32
|
+
function detectCodexCommand(): string {
|
|
33
|
+
return config.codex.path || "codex";
|
|
34
|
+
}
|
|
35
|
+
const CODEX_COMMAND = detectCodexCommand();
|
|
36
|
+
|
|
37
|
+
/** exec 模式共用参数:JSONL 输出、绕过沙盒和确认、跳过 git 仓库检查 */
|
|
38
|
+
const CODEX_BASE_ARGS = [
|
|
39
|
+
"exec",
|
|
40
|
+
"--json",
|
|
41
|
+
"--dangerously-bypass-approvals-and-sandbox",
|
|
42
|
+
"--skip-git-repo-check",
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
/** codex 模型;留空("")表示不传 --model,由 codex config.toml 决定 */
|
|
46
|
+
function resolveCodexModel(): string | null {
|
|
47
|
+
const m = config.codex.model;
|
|
48
|
+
return m.trim() !== "" ? m : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** codex 努力程度(映射为 -c model_reasoning_effort=<value>);留空表示不传 */
|
|
52
|
+
function resolveCodexEffort(): string | null {
|
|
53
|
+
const e = config.codex.effort;
|
|
54
|
+
return e.trim() !== "" ? e : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// 类型:Codex JSONL 消息行
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
interface CodexItem {
|
|
62
|
+
id?: string;
|
|
63
|
+
type?: string;
|
|
64
|
+
text?: string;
|
|
65
|
+
command?: string;
|
|
66
|
+
aggregated_output?: string;
|
|
67
|
+
exit_code?: number | null;
|
|
68
|
+
status?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface CodexEvent {
|
|
72
|
+
type: string;
|
|
73
|
+
thread_id?: string;
|
|
74
|
+
item?: CodexItem;
|
|
75
|
+
usage?: {
|
|
76
|
+
input_tokens?: number;
|
|
77
|
+
cached_input_tokens?: number;
|
|
78
|
+
output_tokens?: number;
|
|
79
|
+
reasoning_output_tokens?: number;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// normalizeCodexMessage — Codex 事件 → UnifiedStreamMessage | null
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
export function normalizeCodexMessage(
|
|
88
|
+
msg: CodexEvent,
|
|
89
|
+
): UnifiedStreamMessage | null {
|
|
90
|
+
// agent_message 文本回复
|
|
91
|
+
if (
|
|
92
|
+
msg.type === "item.completed" &&
|
|
93
|
+
msg.item?.type === "agent_message" &&
|
|
94
|
+
msg.item.text
|
|
95
|
+
) {
|
|
96
|
+
return {
|
|
97
|
+
type: "assistant",
|
|
98
|
+
blocks: [{ type: "text", text: msg.item.text }],
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// command_execution 工具调用开始
|
|
103
|
+
if (
|
|
104
|
+
msg.type === "item.started" &&
|
|
105
|
+
msg.item?.type === "command_execution" &&
|
|
106
|
+
msg.item.command
|
|
107
|
+
) {
|
|
108
|
+
return {
|
|
109
|
+
type: "assistant",
|
|
110
|
+
blocks: [
|
|
111
|
+
{
|
|
112
|
+
type: "tool_use",
|
|
113
|
+
name: "Bash",
|
|
114
|
+
input: { command: msg.item.command },
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// command_execution 工具调用完成
|
|
121
|
+
if (
|
|
122
|
+
msg.type === "item.completed" &&
|
|
123
|
+
msg.item?.type === "command_execution"
|
|
124
|
+
) {
|
|
125
|
+
const exitCode = msg.item.exit_code;
|
|
126
|
+
return {
|
|
127
|
+
type: "assistant",
|
|
128
|
+
blocks: [
|
|
129
|
+
{
|
|
130
|
+
type: "tool_result",
|
|
131
|
+
tool_use_id: msg.item.id ?? "",
|
|
132
|
+
content: msg.item.aggregated_output ?? "",
|
|
133
|
+
is_error: exitCode != null && exitCode !== 0 ? true : undefined,
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// thread.started / turn.started / turn.completed → 不映射为用户可见消息
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// 子进程辅助函数
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
|
|
147
|
+
function spawnCodex(
|
|
148
|
+
args: string[],
|
|
149
|
+
cwd?: string,
|
|
150
|
+
stdinText?: string,
|
|
151
|
+
): ChildProcess {
|
|
152
|
+
const allArgs = [...args];
|
|
153
|
+
const model = resolveCodexModel();
|
|
154
|
+
if (model) {
|
|
155
|
+
// 把 -m 插在 exec 后面、其他参数前面
|
|
156
|
+
const execIdx = allArgs.indexOf("exec");
|
|
157
|
+
allArgs.splice(execIdx + 1, 0, "-m", model);
|
|
158
|
+
}
|
|
159
|
+
const effort = resolveCodexEffort();
|
|
160
|
+
if (effort) {
|
|
161
|
+
allArgs.push("-c", `model_reasoning_effort="${effort}"`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const proc = spawn(CODEX_COMMAND, allArgs, {
|
|
165
|
+
cwd,
|
|
166
|
+
stdio: [stdinText !== undefined ? "pipe" : "ignore", "pipe", "pipe"],
|
|
167
|
+
windowsHide: true,
|
|
168
|
+
shell: true,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
let stderr = "";
|
|
172
|
+
proc.stderr!.on("data", (chunk: Buffer) => {
|
|
173
|
+
stderr += chunk.toString();
|
|
174
|
+
});
|
|
175
|
+
proc.on("close", (code) => {
|
|
176
|
+
if (code !== 0 && stderr.trim()) {
|
|
177
|
+
console.error(
|
|
178
|
+
`[Codex stderr] exit=${code}: ${stderr.trim().slice(0, 2000)}`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (stdinText !== undefined) {
|
|
184
|
+
proc.stdin!.write(stdinText);
|
|
185
|
+
proc.stdin!.end();
|
|
186
|
+
}
|
|
187
|
+
return proc;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function* readJsonLines(
|
|
191
|
+
proc: ChildProcess,
|
|
192
|
+
signal?: AbortSignal,
|
|
193
|
+
): AsyncGenerator<CodexEvent> {
|
|
194
|
+
const rl = createInterface({ input: proc.stdout!, crlfDelay: Infinity });
|
|
195
|
+
for await (const line of rl) {
|
|
196
|
+
if (signal?.aborted) break;
|
|
197
|
+
const trimmed = line.trim();
|
|
198
|
+
if (!trimmed) continue;
|
|
199
|
+
try {
|
|
200
|
+
yield JSON.parse(trimmed) as CodexEvent;
|
|
201
|
+
} catch {
|
|
202
|
+
// 非 JSON 行静默跳过(如 "Reading prompt from stdin...")
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
// 适配器实现
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
class CodexAdapter implements ToolAdapter {
|
|
212
|
+
readonly displayName = "Codex";
|
|
213
|
+
readonly sessionDescPrefix = "Codex Session:";
|
|
214
|
+
private metaStore: CodexSessionMetaStore;
|
|
215
|
+
|
|
216
|
+
constructor(metaStore: CodexSessionMetaStore) {
|
|
217
|
+
this.metaStore = metaStore;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// createSession: 生成 sessionId,记录 cwd,不创建 Codex 线程(延迟到首次 prompt)
|
|
221
|
+
async createSession(cwd: string): Promise<CreateSessionResult> {
|
|
222
|
+
const sessionId = randomUUID();
|
|
223
|
+
await this.metaStore.set(sessionId, { cwd });
|
|
224
|
+
return { sessionId };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async *prompt(
|
|
228
|
+
sessionId: string,
|
|
229
|
+
userText: string,
|
|
230
|
+
cwd: string,
|
|
231
|
+
signal?: AbortSignal,
|
|
232
|
+
): AsyncIterable<UnifiedStreamMessage> {
|
|
233
|
+
let meta = await this.metaStore.get(sessionId);
|
|
234
|
+
const threadId = meta?.threadId;
|
|
235
|
+
const isFirstPrompt = !threadId;
|
|
236
|
+
|
|
237
|
+
// 首次 prompt: codex exec 创建新线程
|
|
238
|
+
// 后续 prompt: codex exec resume 恢复已有线程(resume 不接受 -C,cwd 继承自原线程)
|
|
239
|
+
const args = isFirstPrompt
|
|
240
|
+
? [...CODEX_BASE_ARGS, "-C", cwd, "-"]
|
|
241
|
+
: [...CODEX_BASE_ARGS, "resume", threadId, "-"];
|
|
242
|
+
|
|
243
|
+
const proc = spawnCodex(args, cwd, userText);
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
for await (const raw of readJsonLines(proc, signal)) {
|
|
247
|
+
if (signal?.aborted) break;
|
|
248
|
+
|
|
249
|
+
// 首次 prompt 时从 thread.started 事件学习 threadId
|
|
250
|
+
if (
|
|
251
|
+
isFirstPrompt &&
|
|
252
|
+
raw.type === "thread.started" &&
|
|
253
|
+
raw.thread_id
|
|
254
|
+
) {
|
|
255
|
+
void this.metaStore
|
|
256
|
+
.setThreadId(sessionId, raw.thread_id)
|
|
257
|
+
.catch(() => {});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const normalized = normalizeCodexMessage(raw);
|
|
261
|
+
if (normalized) yield normalized;
|
|
262
|
+
}
|
|
263
|
+
} finally {
|
|
264
|
+
proc.kill();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async getSessionInfo(
|
|
269
|
+
sessionId: string,
|
|
270
|
+
): Promise<SessionInfo | undefined> {
|
|
271
|
+
const meta = await this.metaStore.get(sessionId);
|
|
272
|
+
if (!meta) return undefined;
|
|
273
|
+
return { sessionId, cwd: meta.cwd };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async closeSession(_sessionId: string): Promise<void> {
|
|
277
|
+
// no-op:子进程由 prompt 的 finally 自动 kill
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---------------------------------------------------------------------------
|
|
282
|
+
// 工厂函数
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
|
|
285
|
+
export interface CreateCodexAdapterOptions {
|
|
286
|
+
metaStore?: CodexSessionMetaStore;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function createCodexAdapter(
|
|
290
|
+
options: CreateCodexAdapterOptions = {},
|
|
291
|
+
): ToolAdapter {
|
|
292
|
+
return new CodexAdapter(
|
|
293
|
+
options.metaStore ?? defaultCodexSessionMetaStore,
|
|
294
|
+
);
|
|
295
295
|
}
|