@taptap/cli 2026.11.10 → 2026.11.12
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/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +23 -0
- package/bin/taptap-cli-darwin-amd64 +0 -0
- package/bin/taptap-cli-darwin-arm64 +0 -0
- package/bin/taptap-cli-linux-amd64 +0 -0
- package/bin/taptap-cli-linux-arm64 +0 -0
- package/bin/taptap-cli-linux-riscv64 +0 -0
- package/bin/taptap-cli-windows-amd64.exe +0 -0
- package/bin/taptap-cli-windows-arm64.exe +0 -0
- package/checksums.txt +7 -7
- package/package.json +7 -3
- package/scripts/bot/agent-turn.mjs +136 -0
- package/scripts/bot/agents/cindy-client.mjs +127 -0
- package/scripts/bot/agents/cindy.mjs +217 -0
- package/scripts/bot/agents/codex-project.mjs +63 -0
- package/scripts/bot/agents/codex-server.mjs +140 -0
- package/scripts/bot/agents/codex-sidebar.mjs +79 -0
- package/scripts/bot/agents/codex.mjs +240 -0
- package/scripts/bot/agents/index.mjs +20 -0
- package/scripts/bot/cli.mjs +392 -0
- package/scripts/bot/entry.cjs +10 -0
- package/scripts/bot/forum-context.mjs +97 -0
- package/scripts/bot/listener.mjs +328 -0
- package/scripts/bot/models.mjs +22 -0
- package/scripts/bot/process.mjs +47 -0
- package/scripts/bot/store.mjs +216 -0
- package/scripts/bot/tap.mjs +125 -0
- package/scripts/bot/version.mjs +6 -0
- package/scripts/run.js +2 -0
- package/skills/taptap-app-edit/references/app-edit-field-map.md +2 -0
- package/skills/taptap-app-edit/references/app-edit-fields-and-packages.md +9 -3
- package/skills/taptap-asset-library/references/asset-library-ingest.md +2 -2
- package/skills/taptap-asset-library/references/asset-library-search.md +15 -0
- package/skills/taptap-cli/references/package-type-boundary.md +1 -0
- package/skills/taptap-materials/SKILL.md +2 -2
- package/skills/taptap-official-message-push/SKILL.md +1 -1
- package/skills/taptap-official-message-push/references/push-runbook.md +11 -8
- package/skills/taptap-package-management/SKILL.md +1 -1
- package/skills/taptap-package-management/references/package-management-diagnostics.md +8 -3
- package/taptap-suite/references/taptap-app-edit/references/app-edit-field-map.md +2 -0
- package/taptap-suite/references/taptap-app-edit/references/app-edit-fields-and-packages.md +9 -3
- package/taptap-suite/references/taptap-asset-library/references/asset-library-ingest.md +2 -2
- package/taptap-suite/references/taptap-asset-library/references/asset-library-search.md +15 -0
- package/taptap-suite/references/taptap-cli/references/package-type-boundary.md +1 -0
- package/taptap-suite/references/taptap-materials/SKILL.md +2 -2
- package/taptap-suite/references/taptap-official-message-push/SKILL.md +1 -1
- package/taptap-suite/references/taptap-official-message-push/references/push-runbook.md +11 -8
- package/taptap-suite/references/taptap-package-management/SKILL.md +1 -1
- package/taptap-suite/references/taptap-package-management/references/package-management-diagnostics.md +8 -3
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { createInterface } from "node:readline";
|
|
8
|
+
import { version } from "../version.mjs";
|
|
9
|
+
|
|
10
|
+
export function agentEnvironment() {
|
|
11
|
+
return Object.fromEntries(Object.entries(process.env).filter(
|
|
12
|
+
([key]) => !key.toUpperCase().startsWith("TAPTAP_CLI_"),
|
|
13
|
+
));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function codexCommand(options = {}) {
|
|
17
|
+
if (options.executable) return [options.executable, []];
|
|
18
|
+
if (process.platform === "win32") {
|
|
19
|
+
// npm's Windows launcher is a .cmd file; start its JS with Node, without a shell.
|
|
20
|
+
for (const dir of (process.env.PATH || "").split(path.delimiter)) {
|
|
21
|
+
if (!path.isAbsolute(dir)) continue;
|
|
22
|
+
const exe = path.join(dir, "codex.exe");
|
|
23
|
+
if (existsSync(exe)) return [exe, []];
|
|
24
|
+
const entry = path.join(dir, "node_modules", "@openai", "codex", "bin", "codex.js");
|
|
25
|
+
if (existsSync(entry)) return [process.execPath, [entry]];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return ["codex", []];
|
|
29
|
+
}
|
|
30
|
+
export function codexOverrides(write = false) {
|
|
31
|
+
const disabled = [
|
|
32
|
+
"apps",
|
|
33
|
+
"browser_use",
|
|
34
|
+
"browser_use_external",
|
|
35
|
+
"in_app_browser",
|
|
36
|
+
"computer_use",
|
|
37
|
+
"image_generation",
|
|
38
|
+
"view_image",
|
|
39
|
+
"multi_agent",
|
|
40
|
+
"multi_agent_v2",
|
|
41
|
+
"plugins",
|
|
42
|
+
"remote_plugin",
|
|
43
|
+
"hooks",
|
|
44
|
+
"memories",
|
|
45
|
+
"skill_search",
|
|
46
|
+
"skill_mcp_dependency_install",
|
|
47
|
+
"tool_suggest",
|
|
48
|
+
"shell_snapshot",
|
|
49
|
+
];
|
|
50
|
+
const options = [
|
|
51
|
+
...disabled.map((name) => "features." + name + "=false"),
|
|
52
|
+
"features.skip_host_skill_discovery=true",
|
|
53
|
+
"apps._default.enabled=false",
|
|
54
|
+
'web_search="disabled"',
|
|
55
|
+
"features.shell_tool=" + write,
|
|
56
|
+
"features.unified_exec=" + write,
|
|
57
|
+
];
|
|
58
|
+
return options.flatMap((option) => ["-c", option]);
|
|
59
|
+
}
|
|
60
|
+
export async function withCodexServer({ workspace, options, allowWrite, signal, onStop, onMessage }, work) {
|
|
61
|
+
const [file, args] = codexCommand(options);
|
|
62
|
+
const overrides = codexOverrides(allowWrite);
|
|
63
|
+
const child = spawn(file, [...args, "app-server", ...overrides], {
|
|
64
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
65
|
+
cwd: workspace,
|
|
66
|
+
env: agentEnvironment(),
|
|
67
|
+
windowsHide: true,
|
|
68
|
+
});
|
|
69
|
+
const lines = createInterface({ input: child.stdout });
|
|
70
|
+
const pending = new Map();
|
|
71
|
+
let seq = 0, stopped;
|
|
72
|
+
function stop(e) {
|
|
73
|
+
stopped ??= e;
|
|
74
|
+
for (const p of pending.values()) p.reject(e);
|
|
75
|
+
pending.clear();
|
|
76
|
+
onStop?.(e);
|
|
77
|
+
}
|
|
78
|
+
const abort = () => stop(Error("Codex 会话已停止"));
|
|
79
|
+
child.on("error", () => stop(Error("无法启动 Codex")));
|
|
80
|
+
child.on("exit", () => stop(Error("Codex app-server 已退出")));
|
|
81
|
+
child.stdin.on("error", abort);
|
|
82
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
83
|
+
const write = (value) => !child.stdin.destroyed && child.stdin.write(JSON.stringify(value) + "\n");
|
|
84
|
+
function rpc(method, params) {
|
|
85
|
+
if (stopped) return Promise.reject(stopped);
|
|
86
|
+
const id = ++seq;
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
pending.delete(id);
|
|
90
|
+
reject(Error("Codex RPC 超时 " + method));
|
|
91
|
+
}, 45000);
|
|
92
|
+
pending.set(id, {
|
|
93
|
+
method,
|
|
94
|
+
resolve: (x) => {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
resolve(x);
|
|
97
|
+
},
|
|
98
|
+
reject: (e) => {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
reject(e);
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
write({ id, method, params });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
lines.on("line", (line) => {
|
|
107
|
+
let m;
|
|
108
|
+
try { m = JSON.parse(line); } catch { return; }
|
|
109
|
+
if (!m.method && pending.has(m.id)) {
|
|
110
|
+
const p = pending.get(m.id);
|
|
111
|
+
pending.delete(m.id);
|
|
112
|
+
if (m.error) {
|
|
113
|
+
const error = new Error(`Codex RPC 失败 ${p.method} (${m.error.code})`);
|
|
114
|
+
if (p.method === "thread/resume" && m.error.code === -32600 &&
|
|
115
|
+
/\balready has an active writer\b/i.test(m.error.message)) {
|
|
116
|
+
error.eventErrorCode = "codex_thread_writer_busy";
|
|
117
|
+
error.message += ":该会话已有其他客户端持有写入权(already has an active writer)";
|
|
118
|
+
}
|
|
119
|
+
p.reject(error);
|
|
120
|
+
} else p.resolve(m.result);
|
|
121
|
+
} else if (onMessage) onMessage(m, write);
|
|
122
|
+
else if (m.method && m.id != null)
|
|
123
|
+
write({ id: m.id, error: { code: -32601, message: "Unsupported request" } });
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
signal?.throwIfAborted();
|
|
127
|
+
await rpc("initialize", {
|
|
128
|
+
clientInfo: { name: "taptap_cli_bot", version },
|
|
129
|
+
capabilities: { experimentalApi: true },
|
|
130
|
+
});
|
|
131
|
+
write({ method: "initialized", params: {} });
|
|
132
|
+
return await work(rpc);
|
|
133
|
+
} finally {
|
|
134
|
+
signal?.removeEventListener("abort", abort);
|
|
135
|
+
lines.close();
|
|
136
|
+
child.stdin.end();
|
|
137
|
+
child.kill();
|
|
138
|
+
stop(Error("会话结束"));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { lstat } from "node:fs/promises";
|
|
6
|
+
import net from "node:net";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
|
|
10
|
+
function encode(value) {
|
|
11
|
+
const body = Buffer.from(JSON.stringify(value));
|
|
12
|
+
const header = Buffer.alloc(4);
|
|
13
|
+
header.writeUInt32LE(body.length);
|
|
14
|
+
return Buffer.concat([header, body]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function desktopEndpoint() {
|
|
18
|
+
if (process.platform === "win32") return String.raw`\\.\pipe\codex-ipc`;
|
|
19
|
+
if (process.platform === "darwin") {
|
|
20
|
+
return path.join(process.env.CODEX_HOME || path.join(homedir(), ".codex"), "ipc", "ipc.sock");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// A separate app-server saves the thread, but cannot refresh the desktop's
|
|
25
|
+
// in-memory project list. The local desktop IPC currently reloads one active
|
|
26
|
+
// thread on the v1 thread-unarchived notification. Send the notification only;
|
|
27
|
+
// never archive, restore, or navigate to a thread.
|
|
28
|
+
export async function syncNewThread(rpc, threadId, projectId, endpoint = desktopEndpoint()) {
|
|
29
|
+
if (!endpoint || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(threadId)) return;
|
|
30
|
+
if (process.platform !== "win32") {
|
|
31
|
+
const socket = await lstat(endpoint);
|
|
32
|
+
if (!socket.isSocket() || socket.uid !== process.getuid()) throw Error("Codex IPC 路径或所有者无效");
|
|
33
|
+
}
|
|
34
|
+
const { data } = await rpc("thread/list", {
|
|
35
|
+
projectId, archived: false, limit: 100, sortKey: "updated_at", useStateDbOnly: true,
|
|
36
|
+
});
|
|
37
|
+
if (!data.some(thread => thread.id === threadId && thread.projectId === projectId && !thread.ephemeral)) return;
|
|
38
|
+
const requestId = randomUUID();
|
|
39
|
+
await new Promise((resolve, reject) => {
|
|
40
|
+
const socket = net.createConnection(endpoint);
|
|
41
|
+
const timer = setTimeout(() => finish(Error("Codex 桌面同步超时")), 3000);
|
|
42
|
+
let pending = Buffer.alloc(0), received = false;
|
|
43
|
+
function finish(error) {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
socket.destroy();
|
|
46
|
+
if (error) reject(error);
|
|
47
|
+
else resolve();
|
|
48
|
+
}
|
|
49
|
+
socket.once("error", finish);
|
|
50
|
+
socket.once("close", () => finish(Error("Codex 桌面连接已关闭")));
|
|
51
|
+
socket.once("connect", () => socket.write(encode({
|
|
52
|
+
type: "request", requestId, method: "initialize", version: 0,
|
|
53
|
+
sourceClientId: "uninitialized", params: { clientType: "taptap-cli" },
|
|
54
|
+
})));
|
|
55
|
+
socket.on("data", chunk => {
|
|
56
|
+
if (received) return;
|
|
57
|
+
pending = Buffer.concat([pending, chunk]);
|
|
58
|
+
while (pending.length >= 4) {
|
|
59
|
+
const length = pending.readUInt32LE();
|
|
60
|
+
if (!length || length > 65536) return finish(Error("Codex IPC 响应长度无效"));
|
|
61
|
+
if (pending.length < length + 4) return;
|
|
62
|
+
let message;
|
|
63
|
+
try { message = JSON.parse(pending.toString("utf8", 4, 4 + length)); }
|
|
64
|
+
catch { return finish(Error("Codex IPC 响应格式无效")); }
|
|
65
|
+
pending = pending.subarray(length + 4);
|
|
66
|
+
if (message?.type !== "response" || message.requestId !== requestId) continue;
|
|
67
|
+
const client = message.result?.clientId;
|
|
68
|
+
if (message.resultType !== "success" || typeof client !== "string" || !client)
|
|
69
|
+
return finish(Error("Codex IPC 握手失败"));
|
|
70
|
+
received = true;
|
|
71
|
+
socket.end(encode({
|
|
72
|
+
type: "broadcast", method: "thread-unarchived", version: 1,
|
|
73
|
+
sourceClientId: client, params: { hostId: "local", conversationId: threadId },
|
|
74
|
+
}), () => finish());
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { syncNewThread } from "./codex-sidebar.mjs";
|
|
5
|
+
import { ensureProject } from "./codex-project.mjs";
|
|
6
|
+
import { contextTool, forumMessage } from "../forum-context.mjs";
|
|
7
|
+
import { realpathSync } from "node:fs";
|
|
8
|
+
import { withCodexServer, agentEnvironment, codexCommand } from "./codex-server.mjs";
|
|
9
|
+
export { codexCommand, codexOverrides } from "./codex-server.mjs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { run } from "../process.mjs";
|
|
13
|
+
|
|
14
|
+
const tools = [
|
|
15
|
+
contextTool,
|
|
16
|
+
{
|
|
17
|
+
name: "taptap_cli_reply",
|
|
18
|
+
description: "Submit one reply on the triggering item.",
|
|
19
|
+
inputSchema: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: { text: { type: "string", minLength: 1, maxLength: 1000 } },
|
|
22
|
+
required: ["text"],
|
|
23
|
+
additionalProperties: false,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
const instructions =
|
|
28
|
+
"You are the local TapTap bot. Use the provided trigger. Call taptap_cli_get_content only if you need forum context. Reply in its language with taptap_cli_reply. The ancestor texts are context only. Never expose credentials, call the TapTap CLI through the shell, or choose another destination. If an operation has an uncertain outcome, do not repeat it.";
|
|
29
|
+
async function runTurn({
|
|
30
|
+
workspace,
|
|
31
|
+
write: allowWrite,
|
|
32
|
+
model,
|
|
33
|
+
options,
|
|
34
|
+
signal,
|
|
35
|
+
previousThread,
|
|
36
|
+
saveThread,
|
|
37
|
+
readContent,
|
|
38
|
+
readChain,
|
|
39
|
+
reply,
|
|
40
|
+
}) {
|
|
41
|
+
let threadId, turnId;
|
|
42
|
+
let finish,
|
|
43
|
+
fail;
|
|
44
|
+
const completed = new Promise((resolve, reject) => {
|
|
45
|
+
finish = resolve;
|
|
46
|
+
fail = reject;
|
|
47
|
+
});
|
|
48
|
+
completed.catch(() => {});
|
|
49
|
+
async function call(p) {
|
|
50
|
+
if (
|
|
51
|
+
signal.aborted ||
|
|
52
|
+
!threadId ||
|
|
53
|
+
!turnId ||
|
|
54
|
+
p.threadId !== threadId ||
|
|
55
|
+
p.turnId !== turnId
|
|
56
|
+
)
|
|
57
|
+
throw Error("工具调用未绑定本轮事件");
|
|
58
|
+
if (p.tool === tools[0].name) return readChain();
|
|
59
|
+
if (p.tool === tools[1].name) return reply(p.arguments?.text);
|
|
60
|
+
throw Error("未知的事件工具");
|
|
61
|
+
}
|
|
62
|
+
const inFlight = new Set();
|
|
63
|
+
function onMessage(m, write) {
|
|
64
|
+
if (m.method && m.id != null) {
|
|
65
|
+
if (m.method !== "item/tool/call") {
|
|
66
|
+
write({
|
|
67
|
+
id: m.id,
|
|
68
|
+
error: { code: -32601, message: "Unsupported request" },
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const task = (async () => {
|
|
73
|
+
let result,
|
|
74
|
+
success = true;
|
|
75
|
+
try {
|
|
76
|
+
result = await call(m.params);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
success = false;
|
|
79
|
+
result = { error: e.message };
|
|
80
|
+
}
|
|
81
|
+
write({
|
|
82
|
+
id: m.id,
|
|
83
|
+
result: {
|
|
84
|
+
success,
|
|
85
|
+
contentItems: [{ type: "inputText", text: JSON.stringify(result) }],
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
})();
|
|
89
|
+
inFlight.add(task);
|
|
90
|
+
task.finally(() => inFlight.delete(task)).catch(fail);
|
|
91
|
+
} else if (m.params?.threadId === threadId && m.method === "turn/started")
|
|
92
|
+
turnId = m.params.turn.id;
|
|
93
|
+
else if (
|
|
94
|
+
m.params?.threadId === threadId &&
|
|
95
|
+
m.method === "turn/completed" &&
|
|
96
|
+
m.params.turn.id === turnId
|
|
97
|
+
) {
|
|
98
|
+
if (m.params.turn.status === "completed") finish();
|
|
99
|
+
else fail(Error("Codex 轮次未完成"));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const timer = setTimeout(() => fail(Error("Codex 轮次超时")), 15 * 60 * 1000);
|
|
103
|
+
try {
|
|
104
|
+
await withCodexServer({ workspace, options, allowWrite, signal, onMessage, onStop: fail }, async (rpc) => {
|
|
105
|
+
const { config } = await rpc("config/read", { cwd: workspace });
|
|
106
|
+
const params = {
|
|
107
|
+
cwd: workspace,
|
|
108
|
+
approvalPolicy: "never",
|
|
109
|
+
sandbox: allowWrite ? "workspace-write" : "read-only",
|
|
110
|
+
developerInstructions: instructions,
|
|
111
|
+
// Overriding a missing MCP entry would create an invalid transport.
|
|
112
|
+
...(config.mcp_servers?.node_repl && { config: { "mcp_servers.node_repl.enabled": false } }),
|
|
113
|
+
...(model ? { model: model } : {}),
|
|
114
|
+
};
|
|
115
|
+
const projectId = options?.projectId;
|
|
116
|
+
if (!projectId) throw Error("请重新连接机器人以注册 Codex 项目");
|
|
117
|
+
const t = previousThread
|
|
118
|
+
? await rpc("thread/resume", { ...params, threadId: previousThread })
|
|
119
|
+
: await rpc("thread/start", {
|
|
120
|
+
...params,
|
|
121
|
+
projectId,
|
|
122
|
+
dynamicTools: tools.map((x) => ({ ...x, type: "function" })),
|
|
123
|
+
});
|
|
124
|
+
threadId = t.thread.id;
|
|
125
|
+
if (
|
|
126
|
+
t.approvalPolicy !== "never" ||
|
|
127
|
+
path.resolve(t.cwd ?? t.thread.cwd ?? workspace) !== workspace ||
|
|
128
|
+
(t.sandbox?.writableRoots || []).some(
|
|
129
|
+
(root) => path.resolve(root) !== workspace,
|
|
130
|
+
) ||
|
|
131
|
+
t.sandbox?.type !== (allowWrite ? "workspaceWrite" : "readOnly") ||
|
|
132
|
+
(previousThread && threadId !== previousThread)
|
|
133
|
+
)
|
|
134
|
+
throw Error("Codex 工作区权限未确认");
|
|
135
|
+
const inventory = await rpc("mcpServerStatus/list", {
|
|
136
|
+
threadId,
|
|
137
|
+
limit: 100,
|
|
138
|
+
});
|
|
139
|
+
if (
|
|
140
|
+
inventory.nextCursor ||
|
|
141
|
+
inventory.data?.some(
|
|
142
|
+
(s) =>
|
|
143
|
+
s.runtimeStatus !== "disabled" &&
|
|
144
|
+
(s.tools?.length || Object.keys(s.tools || {}).length),
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
throw Error("请禁用此 Codex 配置的外部 MCP 工具");
|
|
148
|
+
saveThread(threadId);
|
|
149
|
+
if (t.thread.projectId !== projectId) {
|
|
150
|
+
const updated = await rpc("thread/metadata/update", { threadId, projectId });
|
|
151
|
+
if (updated.thread?.projectId !== projectId) throw Error("Codex 会话未归入机器人项目");
|
|
152
|
+
}
|
|
153
|
+
const started = await rpc("turn/start", {
|
|
154
|
+
threadId,
|
|
155
|
+
...(model ? { model } : {}),
|
|
156
|
+
input: [
|
|
157
|
+
{
|
|
158
|
+
type: "text",
|
|
159
|
+
text: await forumMessage(
|
|
160
|
+
{ readContent, readChain },
|
|
161
|
+
Boolean(previousThread),
|
|
162
|
+
),
|
|
163
|
+
text_elements: [],
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
turnId = started.turn.id;
|
|
168
|
+
await completed;
|
|
169
|
+
await Promise.allSettled([...inFlight]);
|
|
170
|
+
if (!previousThread && !signal.aborted) {
|
|
171
|
+
// readContent reuses the body already fetched for this event's prompt.
|
|
172
|
+
const { message_body } = await readContent();
|
|
173
|
+
const line = message_body.replace(/[\s\u0000-\u001f\u007f]+/gu, " ").trim();
|
|
174
|
+
const chars = Array.from(line);
|
|
175
|
+
const name = chars.length > 80 ? chars.slice(0, 79).join("") + "…" : line;
|
|
176
|
+
if (name) await rpc("thread/name/set", { threadId, name }).catch(error => {
|
|
177
|
+
console.error("Codex 会话命名失败:" + error.message);
|
|
178
|
+
});
|
|
179
|
+
await syncNewThread(rpc, threadId, projectId).catch(error => {
|
|
180
|
+
console.error("Codex 侧边栏同步失败:" + error.message);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
} finally {
|
|
185
|
+
clearTimeout(timer);
|
|
186
|
+
await Promise.allSettled([...inFlight]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function checkReady(options) {
|
|
191
|
+
const [file, args] = codexCommand(options);
|
|
192
|
+
const result = await run(file, [...args, "login", "status"], { env: agentEnvironment() });
|
|
193
|
+
if (result.code !== 0) throw Error("请先运行 taptap-cli bot codex-login");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function listModels(options, workspace) {
|
|
197
|
+
await checkReady(options);
|
|
198
|
+
return withCodexServer({ options, workspace }, async (rpc) => {
|
|
199
|
+
const models = [];
|
|
200
|
+
const seen = new Set();
|
|
201
|
+
let cursor;
|
|
202
|
+
do {
|
|
203
|
+
const page = await rpc("model/list", { limit: 100, ...(cursor ? { cursor } : {}) });
|
|
204
|
+
for (const model of page.data) {
|
|
205
|
+
models.push({
|
|
206
|
+
id: model.model,
|
|
207
|
+
name: model.displayName || model.model,
|
|
208
|
+
isDefault: model.isDefault === true,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
cursor = page.nextCursor;
|
|
212
|
+
if (cursor && seen.has(cursor)) throw Error("Codex 模型列表游标重复");
|
|
213
|
+
seen.add(cursor);
|
|
214
|
+
} while (cursor);
|
|
215
|
+
return models;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function login(options) {
|
|
220
|
+
const [file, args] = codexCommand(options);
|
|
221
|
+
const child = spawn(file, [...args, "login"], {
|
|
222
|
+
stdio: "inherit",
|
|
223
|
+
env: agentEnvironment(),
|
|
224
|
+
windowsHide: false,
|
|
225
|
+
});
|
|
226
|
+
await new Promise((resolve, reject) => {
|
|
227
|
+
child.once("error", reject);
|
|
228
|
+
child.once("close", (code) =>
|
|
229
|
+
code === 0 ? resolve() : reject(Error("Codex 授权未完成")),
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function prepareProject(workspace, options) {
|
|
235
|
+
const root = realpathSync(workspace);
|
|
236
|
+
const server = { workspace, options };
|
|
237
|
+
return ensureProject(root, work => withCodexServer(server, work));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export const codex = { name: "codex", runTurn, checkReady, listModels, login, prepareProject };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { codex } from "./codex.mjs";
|
|
5
|
+
|
|
6
|
+
import { cindy } from "./cindy.mjs";
|
|
7
|
+
|
|
8
|
+
const agents = new Map([
|
|
9
|
+
[codex.name, codex],
|
|
10
|
+
[cindy.name, cindy],
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export function getAgent(name = codex.name) {
|
|
14
|
+
const agent = agents.get(name);
|
|
15
|
+
if (!agent)
|
|
16
|
+
throw Error(
|
|
17
|
+
"未知 agent " + name + "。可用 agent:" + [...agents.keys()].join(", "),
|
|
18
|
+
);
|
|
19
|
+
return agent;
|
|
20
|
+
}
|