chatccc 0.2.18 → 0.2.20
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 +21 -17
- package/bin/chatccc.mjs +23 -23
- package/config.sample.json +29 -26
- package/images/avatars/badges/badge_claude.png +0 -0
- package/images/avatars/badges/badge_codex.png +0 -0
- package/images/avatars/badges/badge_cursor.png +0 -0
- package/images/avatars/brand-sources/claude_code_app_icon.png +0 -0
- package/images/avatars/brand-sources/codex_app_icon.png +0 -0
- package/images/avatars/brand-sources/cursor_icon_512.png +0 -0
- package/package.json +1 -1
- package/src/__tests__/config-reload.test.ts +41 -19
- package/src/adapters/codex-adapter.ts +294 -294
- package/src/cards.ts +23 -18
- package/src/config.ts +651 -616
- package/src/feishu-api.ts +115 -16
- package/src/index.ts +18 -11
- package/src/session.ts +3 -3
- package/src/web-ui.ts +1708 -1591
|
@@ -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
|
}
|
package/src/cards.ts
CHANGED
|
@@ -108,13 +108,17 @@ export function buildProgressCard(
|
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
export function buildHelpCard(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
111
|
+
export function buildHelpCard(
|
|
112
|
+
userText: string,
|
|
113
|
+
opts: { greeting?: string; defaultToolLabel?: string } = {},
|
|
114
|
+
): string {
|
|
115
|
+
const greeting = opts.greeting ?? `你发送了: ${userText}`;
|
|
116
|
+
const defaultToolLabel = opts.defaultToolLabel ?? "Claude Code";
|
|
117
|
+
const lines = [
|
|
118
|
+
`发送 **/new** 创建新会话(默认 ${defaultToolLabel})`,
|
|
119
|
+
"发送 **/new claude** 创建新 Claude 对话",
|
|
120
|
+
"发送 **/new cursor** 创建新 Cursor 会话",
|
|
121
|
+
"发送 **/new codex** 创建新 Codex 会话",
|
|
118
122
|
"发送 **/forget** 重置当前会话(保留工作目录,同一群内继续)",
|
|
119
123
|
].join("\n");
|
|
120
124
|
return JSON.stringify({
|
|
@@ -124,7 +128,7 @@ export function buildHelpCard(userText: string, opts: { greeting?: string } = {}
|
|
|
124
128
|
{ tag: "div", text: { tag: "lark_md", content: greeting } },
|
|
125
129
|
{ tag: "div", text: { tag: "lark_md", content: lines } },
|
|
126
130
|
buildButtons([
|
|
127
|
-
{ text:
|
|
131
|
+
{ text: `新建默认会话(/new,${defaultToolLabel})`, value: JSON.stringify({ cmd: "new" }), type: "primary" },
|
|
128
132
|
{ text: "新建 Cursor 会话(/new cursor)", value: JSON.stringify({ cmd: "new cursor" }), type: "primary" },
|
|
129
133
|
{ text: "新建 Codex 会话(/new codex)", value: JSON.stringify({ cmd: "new codex" }), type: "primary" },
|
|
130
134
|
{ text: "重启 ChatCCC(/restart)", value: JSON.stringify({ cmd: "restart" }), type: "danger" },
|
|
@@ -248,14 +252,15 @@ export function buildCdCard(
|
|
|
248
252
|
}
|
|
249
253
|
|
|
250
254
|
// 所有会话列表卡片(Claude Code 优先,然后 Cursor)
|
|
251
|
-
export function buildSessionsCard(sessions: Array<{
|
|
252
|
-
sessionId: string;
|
|
253
|
-
active: boolean;
|
|
254
|
-
turnCount: number;
|
|
255
|
-
elapsedSeconds: number | null;
|
|
256
|
-
model: string;
|
|
257
|
-
tool: string;
|
|
258
|
-
}
|
|
255
|
+
export function buildSessionsCard(sessions: Array<{
|
|
256
|
+
sessionId: string;
|
|
257
|
+
active: boolean;
|
|
258
|
+
turnCount: number;
|
|
259
|
+
elapsedSeconds: number | null;
|
|
260
|
+
model: string;
|
|
261
|
+
tool: string;
|
|
262
|
+
}>, opts: { defaultToolLabel?: string } = {}): string {
|
|
263
|
+
const defaultToolLabel = opts.defaultToolLabel ?? "Claude Code";
|
|
259
264
|
// 按 tool 分组排序:Claude Code 在前,Cursor 其次,Codex 最后
|
|
260
265
|
const claudeCodeSessions = sessions.filter(s => s.tool !== "cursor" && s.tool !== "codex");
|
|
261
266
|
const cursorSessions = sessions.filter(s => s.tool === "cursor");
|
|
@@ -269,7 +274,7 @@ export function buildSessionsCard(sessions: Array<{
|
|
|
269
274
|
config: { wide_screen_mode: true },
|
|
270
275
|
header: { template: "blue", title: { content: "所有会话", tag: "plain_text" } },
|
|
271
276
|
elements: [
|
|
272
|
-
{ tag: "div", text: { tag: "lark_md", content:
|
|
277
|
+
{ tag: "div", text: { tag: "lark_md", content: `当前没有会话记录。\n\n使用 **/new**(默认 ${defaultToolLabel})、**/new claude**、**/new cursor** 或 **/new codex** 创建新会话。` } },
|
|
273
278
|
{ tag: "hr" },
|
|
274
279
|
{ tag: "action", actions: [{ tag: "button", text: { tag: "plain_text", content: "收起" }, type: "default", value: { action: "close" } }] },
|
|
275
280
|
],
|
|
@@ -355,4 +360,4 @@ export function buildStatusCard(statusText: string, template = "blue"): string {
|
|
|
355
360
|
},
|
|
356
361
|
],
|
|
357
362
|
});
|
|
358
|
-
}
|
|
363
|
+
}
|