@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
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,29 @@ All notable TapTap CLI changes are documented here.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [v2026.11.12] - 2026-11-12
|
|
8
|
+
|
|
9
|
+
### Improvements
|
|
10
|
+
|
|
11
|
+
- Diagnose conflicting package-type declarations with `package-management diagnose-package-type`
|
|
12
|
+
and fail H5 uploads before any write, pointing at the controlled
|
|
13
|
+
`recover-package-type-declaration` recovery command; native `apk`/`windows` additive saves
|
|
14
|
+
are reported as normal saves, not as a type switch.
|
|
15
|
+
- Shorten metadata-defined command names that repeated their service tag
|
|
16
|
+
(`official-message-push get-official-message-push-info` becomes
|
|
17
|
+
`official-message-push get-info`); the historical names remain accepted as
|
|
18
|
+
aliases, and `schema` resolves both spellings.
|
|
19
|
+
|
|
20
|
+
## [v2026.11.11] - 2026-11-11
|
|
21
|
+
|
|
22
|
+
### Documentation
|
|
23
|
+
|
|
24
|
+
- Include the app scope in the package-type declaration recovery example.
|
|
25
|
+
|
|
26
|
+
### Tests
|
|
27
|
+
|
|
28
|
+
- Align the developer app listing fixture with the production response schema.
|
|
29
|
+
|
|
7
30
|
|
|
8
31
|
## [v2026.11.10] - 2026-11-10
|
|
9
32
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/checksums.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
ad2eadbb48ed0c437e9b8e7285b69f4284327350735fc88cc63bda6d6801560b bin/taptap-cli-darwin-amd64
|
|
2
|
+
a46f1f88d1887ddb224cfc5296b767f2616108c9275f770fe3102e4a0d72f46d bin/taptap-cli-darwin-arm64
|
|
3
|
+
b318be9bc8f6533ed8ac4716b66ef2e4018ad57740420be9b2de5b55f4d5e487 bin/taptap-cli-linux-amd64
|
|
4
|
+
3ec55f057217ca1d6fe5e73f222898ae6ed2362534eee833710b9fdbdc5cdad6 bin/taptap-cli-linux-arm64
|
|
5
|
+
fb9f991d688bf3ee387056fa4533c2d40f047349e53ea8263f716794022ba91b bin/taptap-cli-linux-riscv64
|
|
6
|
+
f25f8935c84a7970cd45a1e49e4fb28e7821c75e8dac4545952412fdcb829fd6 bin/taptap-cli-windows-amd64.exe
|
|
7
|
+
8e21be47f6583cc5f855b7a39f4a9e1b58cd3dac34c121a8485dbb9bd5bad0b2 bin/taptap-cli-windows-arm64.exe
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taptap/cli",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.12",
|
|
4
4
|
"description": "TapTap AI Agent CLI",
|
|
5
5
|
"bin": {
|
|
6
6
|
"taptap-cli": "scripts/run.js"
|
|
@@ -46,6 +46,10 @@
|
|
|
46
46
|
"affordance",
|
|
47
47
|
"scripts/claude-hooks",
|
|
48
48
|
"skills",
|
|
49
|
-
"taptap-suite"
|
|
50
|
-
|
|
49
|
+
"taptap-suite",
|
|
50
|
+
"scripts/bot/{*.mjs,entry.cjs,agents/*.mjs}"
|
|
51
|
+
],
|
|
52
|
+
"optionalDependencies": {
|
|
53
|
+
"ws": "^8.18.3"
|
|
54
|
+
}
|
|
51
55
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { loadReplyChain } from "./forum-context.mjs";
|
|
5
|
+
import { tap, botArgs, assertLoginBinding } from "./tap.mjs";
|
|
6
|
+
import { positive, hash } from "./store.mjs";
|
|
7
|
+
import { getAgent } from "./agents/index.mjs";
|
|
8
|
+
|
|
9
|
+
function safeFrame(data) {
|
|
10
|
+
const { queryGrantRef, ...event } = data.event;
|
|
11
|
+
return {
|
|
12
|
+
event,
|
|
13
|
+
content: data.content,
|
|
14
|
+
conversationKey: data.conversationKey,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
// The host owns event grants and delivery state. The adapter only sees this event's tools.
|
|
18
|
+
export async function handleTurn(db, id, config, row, phase, signal) {
|
|
19
|
+
const data = JSON.parse(row.frame).data;
|
|
20
|
+
const event = data.event;
|
|
21
|
+
let queryStarted = false;
|
|
22
|
+
let sendStarted = false;
|
|
23
|
+
let content, grant, chainPromise;
|
|
24
|
+
const query = (params) =>
|
|
25
|
+
tap(config, botArgs(config, "get-content"), {
|
|
26
|
+
query_grant_ref: event.queryGrantRef,
|
|
27
|
+
...params,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
async function readContent() {
|
|
31
|
+
signal.throwIfAborted();
|
|
32
|
+
if (content) return { message_body: content.content };
|
|
33
|
+
if (queryStarted || Number(event.queryExpiresAt) <= Date.now())
|
|
34
|
+
throw Error("查询已使用或已过期");
|
|
35
|
+
queryStarted = true;
|
|
36
|
+
const value = await query({
|
|
37
|
+
kind: "ITEM",
|
|
38
|
+
target: {
|
|
39
|
+
type: event.sourceType.slice("BOT_PLATFORM_SOURCE_".length),
|
|
40
|
+
id: event.sourceId,
|
|
41
|
+
},
|
|
42
|
+
content_only: true,
|
|
43
|
+
});
|
|
44
|
+
if (value.content_only !== true || typeof value.item?.content !== "string")
|
|
45
|
+
throw Error("正文响应不匹配");
|
|
46
|
+
content = value.item;
|
|
47
|
+
grant = value.reply_authorization;
|
|
48
|
+
return { message_body: content.content };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function reply(text) {
|
|
52
|
+
if (
|
|
53
|
+
signal.aborted ||
|
|
54
|
+
sendStarted ||
|
|
55
|
+
!content ||
|
|
56
|
+
!grant ||
|
|
57
|
+
grant.event_id !== event.eventId ||
|
|
58
|
+
!/^[A-Za-z0-9_-]{43}$/.test(grant.reply_grant) ||
|
|
59
|
+
!positive(grant.expires_at) ||
|
|
60
|
+
Number(grant.expires_at) <= Date.now() ||
|
|
61
|
+
typeof text !== "string" ||
|
|
62
|
+
!text.trim() ||
|
|
63
|
+
[...text].length > 1000 ||
|
|
64
|
+
text.includes(grant.reply_grant) ||
|
|
65
|
+
text.includes(event.queryGrantRef)
|
|
66
|
+
)
|
|
67
|
+
throw Error("回复授权或参数无效");
|
|
68
|
+
await assertLoginBinding(config);
|
|
69
|
+
signal.throwIfAborted();
|
|
70
|
+
if (sendStarted) throw Error("该事件正在提交回复");
|
|
71
|
+
sendStarted = true;
|
|
72
|
+
phase("submitting");
|
|
73
|
+
try {
|
|
74
|
+
const r = await tap(config, botArgs(config, "reply"), {
|
|
75
|
+
event_id: event.eventId,
|
|
76
|
+
reply_grant: grant.reply_grant,
|
|
77
|
+
text,
|
|
78
|
+
});
|
|
79
|
+
if (
|
|
80
|
+
!positive(r.reply_id) ||
|
|
81
|
+
!["published", "pending_review", "rejected", "deleted"].includes(
|
|
82
|
+
r.status,
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
throw Error("回复回执无效");
|
|
86
|
+
const result = { reply_id: r.reply_id, status: r.status };
|
|
87
|
+
phase("submitted", JSON.stringify(result));
|
|
88
|
+
return result;
|
|
89
|
+
} catch (e) {
|
|
90
|
+
phase("uncertain");
|
|
91
|
+
throw e;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const agent = getAgent(config.agent);
|
|
96
|
+
// Keep existing Codex conversation keys readable after upgrading.
|
|
97
|
+
const scope = [
|
|
98
|
+
id,
|
|
99
|
+
event.bindingId,
|
|
100
|
+
config.threadWorkspace ?? config.workspace,
|
|
101
|
+
!!config.write,
|
|
102
|
+
data.conversationKey || event.eventId,
|
|
103
|
+
];
|
|
104
|
+
if (agent.name !== "codex") scope.unshift(agent.name);
|
|
105
|
+
const key = hash(JSON.stringify(scope));
|
|
106
|
+
db.exec(
|
|
107
|
+
"CREATE TABLE IF NOT EXISTS threads(k TEXT PRIMARY KEY, thread TEXT NOT NULL)",
|
|
108
|
+
);
|
|
109
|
+
const previousThread = db
|
|
110
|
+
.prepare("SELECT thread FROM threads WHERE k=?")
|
|
111
|
+
.get(key)?.thread;
|
|
112
|
+
await agent.runTurn({
|
|
113
|
+
frame: safeFrame(data),
|
|
114
|
+
workspace: config.workspace,
|
|
115
|
+
write: config.write,
|
|
116
|
+
model: config.model,
|
|
117
|
+
options: config.agentOptions ?? { executable: config.codex },
|
|
118
|
+
signal,
|
|
119
|
+
previousThread,
|
|
120
|
+
threadKey: key,
|
|
121
|
+
saveVisibleSession: (session) =>
|
|
122
|
+
db
|
|
123
|
+
.prepare("UPDATE events SET visibleSession=? WHERE id=? AND key=?")
|
|
124
|
+
.run(session, id, row.key),
|
|
125
|
+
saveThread: (thread) =>
|
|
126
|
+
db.prepare("INSERT OR REPLACE INTO threads VALUES(?,?)").run(key, thread),
|
|
127
|
+
readContent,
|
|
128
|
+
async readChain() {
|
|
129
|
+
const current = await readContent();
|
|
130
|
+
chainPromise ??= loadReplyChain(data, content, query, signal);
|
|
131
|
+
return { ...current, ...(await chainPromise) };
|
|
132
|
+
},
|
|
133
|
+
reply,
|
|
134
|
+
});
|
|
135
|
+
if (!sendStarted) phase("handled_without_reply");
|
|
136
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { nativeCli } from "../tap.mjs";
|
|
5
|
+
import { run } from "../process.mjs";
|
|
6
|
+
import { version } from "../version.mjs";
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { randomUUID, createHash } from "node:crypto";
|
|
11
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
12
|
+
|
|
13
|
+
function profileRoot() {
|
|
14
|
+
if (process.platform === "darwin")
|
|
15
|
+
return path.join(homedir(), "Library/Application Support");
|
|
16
|
+
if (process.platform === "win32")
|
|
17
|
+
return process.env.APPDATA || path.join(homedir(), "AppData/Roaming");
|
|
18
|
+
return process.env.XDG_CONFIG_HOME || path.join(homedir(), ".config");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function connectClient(options = {}, pair = false) {
|
|
22
|
+
const file =
|
|
23
|
+
options.discovery ||
|
|
24
|
+
path.join(profileRoot(), "Cindy/local-agent/discovery.json");
|
|
25
|
+
const d = JSON.parse(
|
|
26
|
+
await readFile(file, "utf8").catch(() => {
|
|
27
|
+
throw Error("请打开 Cindy,或用 --cindy-discovery 指定其发现文件");
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
if (
|
|
31
|
+
d.protocol !== 1 ||
|
|
32
|
+
!Number.isInteger(d.port) ||
|
|
33
|
+
d.port < 1 ||
|
|
34
|
+
d.port > 65535 ||
|
|
35
|
+
!/^[\w-]{43}$/.test(d.instance)
|
|
36
|
+
)
|
|
37
|
+
throw Error("Cindy 发现文件无效");
|
|
38
|
+
const prefix = randomUUID();
|
|
39
|
+
let sequence = 0,
|
|
40
|
+
token;
|
|
41
|
+
async function rpc(method, params = {}, signal) {
|
|
42
|
+
const timeout = AbortSignal.timeout(
|
|
43
|
+
method === "capabilities" ? 3000 : 30000,
|
|
44
|
+
);
|
|
45
|
+
const response = await fetch("http://127.0.0.1:" + d.port + "/rpc", {
|
|
46
|
+
method: "POST",
|
|
47
|
+
redirect: "error",
|
|
48
|
+
signal: signal ? AbortSignal.any([timeout, signal]) : timeout,
|
|
49
|
+
headers: {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
...(token && method !== "capabilities"
|
|
52
|
+
? { Authorization: "Bearer " + token }
|
|
53
|
+
: {}),
|
|
54
|
+
},
|
|
55
|
+
body: JSON.stringify({
|
|
56
|
+
version: 1,
|
|
57
|
+
instance: d.instance,
|
|
58
|
+
id: prefix + ":" + ++sequence,
|
|
59
|
+
method,
|
|
60
|
+
params,
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
const msg = await response.json();
|
|
64
|
+
if (!response.ok || msg.error || !Object.hasOwn(msg, "result"))
|
|
65
|
+
throw Object.assign(
|
|
66
|
+
Error(
|
|
67
|
+
"Cindy " + method + " (" + (msg.error?.code || response.status) + ")",
|
|
68
|
+
),
|
|
69
|
+
{ code: msg.error?.code },
|
|
70
|
+
);
|
|
71
|
+
return msg.result;
|
|
72
|
+
}
|
|
73
|
+
const caps = await rpc("capabilities");
|
|
74
|
+
if (
|
|
75
|
+
caps.protocol !== 1 ||
|
|
76
|
+
caps.instance !== d.instance ||
|
|
77
|
+
caps.instanceInfo?.kind !== "installed" ||
|
|
78
|
+
!caps.features?.isolatedSessions ||
|
|
79
|
+
!caps.features?.toolPolicy
|
|
80
|
+
)
|
|
81
|
+
throw Error("目标不是支持本机应用连接的 Cindy 正式版");
|
|
82
|
+
// Use the binary bundled with this adapter, even when --cli points to an RND build.
|
|
83
|
+
const account = createHash("sha256").update(path.resolve(file)).digest("hex");
|
|
84
|
+
async function credential(value) {
|
|
85
|
+
const result = await run(nativeCli(), ["community-bot", "cindy-credential"], {
|
|
86
|
+
input: JSON.stringify({ account, token: value }),
|
|
87
|
+
env: { ...process.env, TAPTAP_CLI_NO_TELEMETRY: "1", TAPTAP_CLI_NO_UPDATE_NOTIFIER: "1", TAPTAP_CLI_NO_SKILLS_NOTIFIER: "1" },
|
|
88
|
+
});
|
|
89
|
+
if (result.code !== 0) throw Error("无法访问 TapTap Bot 的 Cindy 凭证,请检查本机凭证存储权限");
|
|
90
|
+
return JSON.parse(result.stdout).token;
|
|
91
|
+
}
|
|
92
|
+
token = await credential();
|
|
93
|
+
if (token && pair) {
|
|
94
|
+
try {
|
|
95
|
+
await rpc("models");
|
|
96
|
+
} catch (err) {
|
|
97
|
+
if (!err.message.includes("(UNAUTHORIZED)")) throw err;
|
|
98
|
+
token = null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!token) {
|
|
102
|
+
if (!pair) throw Error("请先运行 taptap-cli bot cindy-login");
|
|
103
|
+
const { id, claim } = await rpc("connect/start", {
|
|
104
|
+
client: { id: "cn.taptap.bot", name: "TapTap Bot", version },
|
|
105
|
+
});
|
|
106
|
+
console.error(
|
|
107
|
+
"请在 Cindy 中允许 TapTap Bot,然后在本机应用连接中开启「在 Cindy 中创建可见任务」",
|
|
108
|
+
);
|
|
109
|
+
try {
|
|
110
|
+
const until = Date.now() + 118000;
|
|
111
|
+
while (Date.now() < until) {
|
|
112
|
+
const result = await rpc("connect/poll", { id, claim });
|
|
113
|
+
if (result.state === "approved" && result.token) {
|
|
114
|
+
await credential(result.token);
|
|
115
|
+
token = result.token;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
if (result.state !== "waiting") throw Error("Cindy 授权未通过");
|
|
119
|
+
await delay(500);
|
|
120
|
+
}
|
|
121
|
+
if (!token) throw Error("Cindy 授权超时");
|
|
122
|
+
} finally {
|
|
123
|
+
if (!token) await rpc("connect/cancel", { id, claim }).catch(() => {});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return { rpc, instance: d.instance };
|
|
127
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { contextTool, forumMessage } from "../forum-context.mjs";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
import { connectClient } from "./cindy-client.mjs";
|
|
7
|
+
|
|
8
|
+
const sessions = new Map();
|
|
9
|
+
export const defaultKeepAliveMinutes = 30;
|
|
10
|
+
const pollIntervalMs = 60000;
|
|
11
|
+
|
|
12
|
+
async function stopKeepAlive(session) {
|
|
13
|
+
clearInterval(session.pollTimer);
|
|
14
|
+
clearTimeout(session.expiryTimer);
|
|
15
|
+
session.pollController?.abort();
|
|
16
|
+
await session.poll;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function keepAlive(session, minutes) {
|
|
20
|
+
// Idle polls and active turns share one cursor. Stop and join the idle poll before sending.
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
session.pollController = controller;
|
|
23
|
+
session.pollTimer = setInterval(() => {
|
|
24
|
+
if (session.poll) return;
|
|
25
|
+
session.poll = session.client
|
|
26
|
+
.rpc(
|
|
27
|
+
"events",
|
|
28
|
+
{ sessionId: session.id, cursor: session.cursor },
|
|
29
|
+
controller.signal,
|
|
30
|
+
)
|
|
31
|
+
.then((batch) => {
|
|
32
|
+
session.cursor = batch.cursor;
|
|
33
|
+
})
|
|
34
|
+
.catch((err) => {
|
|
35
|
+
if (err.code === "SESSION_UNAVAILABLE" && !controller.signal.aborted) {
|
|
36
|
+
forget(session);
|
|
37
|
+
clearInterval(session.pollTimer);
|
|
38
|
+
clearTimeout(session.expiryTimer);
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.finally(() => {
|
|
42
|
+
session.poll = undefined;
|
|
43
|
+
});
|
|
44
|
+
}, pollIntervalMs);
|
|
45
|
+
session.pollTimer.unref?.();
|
|
46
|
+
session.expiryTimer = setTimeout(() => {
|
|
47
|
+
void close(session).catch(() => {});
|
|
48
|
+
}, minutes * 60000);
|
|
49
|
+
session.expiryTimer.unref?.();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function forget(session) {
|
|
53
|
+
if (sessions.get(session.key) === session) sessions.delete(session.key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const instructions =
|
|
57
|
+
"你在回复 TapTap 论坛里 @ 机器人的人。用对方的语言直接给出可发布的回复,最多 1000 字符。原帖和上级楼层是上下文,仅回答当前留言。";
|
|
58
|
+
async function chooseModel(client, requested) {
|
|
59
|
+
const { models } = await client.rpc("models");
|
|
60
|
+
const model = models.find((m) => m.id === requested);
|
|
61
|
+
if (!model) throw Error("Cindy 没有可用的指定模型,请在 Cindy 检查模型设置");
|
|
62
|
+
return model.id;
|
|
63
|
+
}
|
|
64
|
+
async function close(session) {
|
|
65
|
+
forget(session);
|
|
66
|
+
await stopKeepAlive(session);
|
|
67
|
+
try {
|
|
68
|
+
await session.client.rpc("session/close", { sessionId: session.id });
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (err.code !== "SESSION_UNAVAILABLE") throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function open(ctx, client, signal) {
|
|
74
|
+
// Bound the number of active local API runs. Saved handles permit later continuation.
|
|
75
|
+
if (sessions.size >= 4) await close(sessions.values().next().value);
|
|
76
|
+
const id = randomUUID();
|
|
77
|
+
const model = await chooseModel(client, ctx.model);
|
|
78
|
+
const result = await client.rpc(
|
|
79
|
+
"session/open",
|
|
80
|
+
{
|
|
81
|
+
sessionId: id,
|
|
82
|
+
cwd: ctx.workspace,
|
|
83
|
+
model,
|
|
84
|
+
toolPolicy: "client-only",
|
|
85
|
+
tools: [
|
|
86
|
+
{
|
|
87
|
+
name: contextTool.name,
|
|
88
|
+
description: contextTool.description,
|
|
89
|
+
parameters: contextTool.inputSchema,
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
instructions,
|
|
93
|
+
...(ctx.previousThread ? { resume: ctx.previousThread } : {}),
|
|
94
|
+
},
|
|
95
|
+
signal,
|
|
96
|
+
);
|
|
97
|
+
const session = {
|
|
98
|
+
key: ctx.threadKey,
|
|
99
|
+
id,
|
|
100
|
+
client,
|
|
101
|
+
visible: result.cindySessionId,
|
|
102
|
+
cursor: 0,
|
|
103
|
+
resumed: result.resumed === true,
|
|
104
|
+
};
|
|
105
|
+
if (!session.visible) {
|
|
106
|
+
await close(session);
|
|
107
|
+
throw Error(
|
|
108
|
+
"请在 Cindy 的本机应用连接中为 TapTap Bot 打开「在 Cindy 中创建可见任务」",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
ctx.saveThread(result.id);
|
|
112
|
+
sessions.set(session.key, session);
|
|
113
|
+
return session;
|
|
114
|
+
}
|
|
115
|
+
export async function runCindyTurn(ctx, client = undefined) {
|
|
116
|
+
const signal = AbortSignal.any([ctx.signal, AbortSignal.timeout(15 * 60000)]);
|
|
117
|
+
client ??= await connectClient(ctx.options);
|
|
118
|
+
let session = sessions.get(ctx.threadKey);
|
|
119
|
+
if (session && session.client.instance !== client.instance) {
|
|
120
|
+
forget(session);
|
|
121
|
+
await stopKeepAlive(session);
|
|
122
|
+
session = undefined;
|
|
123
|
+
}
|
|
124
|
+
if (session) {
|
|
125
|
+
await stopKeepAlive(session);
|
|
126
|
+
// Refresh eviction order when this forum chain becomes active again.
|
|
127
|
+
sessions.delete(session.key);
|
|
128
|
+
sessions.set(session.key, session);
|
|
129
|
+
}
|
|
130
|
+
const reused = Boolean(session);
|
|
131
|
+
session ??= await open(ctx, client, signal);
|
|
132
|
+
try {
|
|
133
|
+
const send = async (target, continuing) => {
|
|
134
|
+
const text = await forumMessage(ctx, continuing);
|
|
135
|
+
return target.client.rpc(
|
|
136
|
+
"send",
|
|
137
|
+
{ sessionId: target.id, parts: [{ type: "text", text }] },
|
|
138
|
+
signal,
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
try {
|
|
142
|
+
await send(session, reused || session.resumed);
|
|
143
|
+
} catch (err) {
|
|
144
|
+
// Cindy rejected this cached run before accepting the message.
|
|
145
|
+
if (!reused || err.code !== "SESSION_UNAVAILABLE") throw err;
|
|
146
|
+
sessions.delete(session.key);
|
|
147
|
+
session = await open(ctx, client, signal);
|
|
148
|
+
await send(session, session.resumed);
|
|
149
|
+
}
|
|
150
|
+
ctx.saveVisibleSession(session.visible);
|
|
151
|
+
let answer = "";
|
|
152
|
+
for (;;) {
|
|
153
|
+
const batch = await session.client.rpc(
|
|
154
|
+
"events",
|
|
155
|
+
{ sessionId: session.id, cursor: session.cursor },
|
|
156
|
+
signal,
|
|
157
|
+
);
|
|
158
|
+
session.cursor = batch.cursor;
|
|
159
|
+
for (const { event } of batch.events) {
|
|
160
|
+
if (event.type === "host_tool") {
|
|
161
|
+
const call = event.data;
|
|
162
|
+
const valid =
|
|
163
|
+
call.name === contextTool.name &&
|
|
164
|
+
Object.keys(call.arguments ?? {}).length === 0;
|
|
165
|
+
await session.client.rpc(
|
|
166
|
+
"tool/reply",
|
|
167
|
+
{
|
|
168
|
+
sessionId: session.id,
|
|
169
|
+
callId: call.id,
|
|
170
|
+
isError: !valid,
|
|
171
|
+
text: valid
|
|
172
|
+
? JSON.stringify(await ctx.readChain())
|
|
173
|
+
: "不支持的工具或参数",
|
|
174
|
+
},
|
|
175
|
+
signal,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (event.type === "text") answer += event.data;
|
|
179
|
+
if (event.type === "turn_end") {
|
|
180
|
+
if (event.data.status !== "completed")
|
|
181
|
+
throw Error("Cindy 轮次未完成:" + event.data.status);
|
|
182
|
+
await ctx.reply(answer.trim());
|
|
183
|
+
if (!ctx.signal.aborted && ctx.frame.conversationKey)
|
|
184
|
+
keepAlive(
|
|
185
|
+
session,
|
|
186
|
+
ctx.options?.keepAliveMinutes ?? defaultKeepAliveMinutes,
|
|
187
|
+
);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} catch (err) {
|
|
193
|
+
await close(session).catch(() => {});
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
export const cindy = {
|
|
198
|
+
name: "cindy",
|
|
199
|
+
runTurn: runCindyTurn,
|
|
200
|
+
async login(options) {
|
|
201
|
+
await connectClient(options, true);
|
|
202
|
+
},
|
|
203
|
+
async listModels(options) {
|
|
204
|
+
const { models } = await (await connectClient(options, true)).rpc("models");
|
|
205
|
+
return models.map(model => ({
|
|
206
|
+
id: model.id,
|
|
207
|
+
name: model.label || model.id,
|
|
208
|
+
isDefault: model.isDefault === true,
|
|
209
|
+
}));
|
|
210
|
+
},
|
|
211
|
+
async checkReady(options, model) {
|
|
212
|
+
await chooseModel(await connectClient(options, true), model);
|
|
213
|
+
},
|
|
214
|
+
async shutdown() {
|
|
215
|
+
await Promise.allSettled([...sessions.values()].map(close));
|
|
216
|
+
},
|
|
217
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright (c) 2026 TapTap
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
7
|
+
import { run } from "../process.mjs";
|
|
8
|
+
|
|
9
|
+
// Use the same workspace link as the Codex desktop launcher. The desktop app
|
|
10
|
+
// registers the folder in its sidebar and owns the corresponding project.
|
|
11
|
+
async function openWorkspace(workspace) {
|
|
12
|
+
const url = "codex://threads/new?path=" + encodeURIComponent(workspace);
|
|
13
|
+
let result;
|
|
14
|
+
if (process.platform === "darwin") {
|
|
15
|
+
result = await run("/usr/bin/open", ["-g", url], { timeout: 5000 });
|
|
16
|
+
} else if (process.platform === "win32") {
|
|
17
|
+
// A constant PowerShell command keeps paths and URI characters out of shell code.
|
|
18
|
+
result = await run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command",
|
|
19
|
+
"Start-Process -ErrorAction Stop -FilePath $env:TAP_BOT_WORKSPACE_URI"], {
|
|
20
|
+
env: { ...process.env, TAP_BOT_WORKSPACE_URI: url }, timeout: 5000,
|
|
21
|
+
});
|
|
22
|
+
} else return false;
|
|
23
|
+
return result.code === 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function findWorkspace(rpc, workspace) {
|
|
27
|
+
let cursor;
|
|
28
|
+
do {
|
|
29
|
+
const page = await rpc("project/list", { cursor, limit: 100 });
|
|
30
|
+
const project = page.data.find(({ roots }) =>
|
|
31
|
+
roots.some(({ path: root }) => path.relative(workspace, root) === ""));
|
|
32
|
+
if (project) return project.id;
|
|
33
|
+
cursor = page.nextCursor;
|
|
34
|
+
} while (cursor);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function ensureProject(workspace, withServer, open = openWorkspace) {
|
|
39
|
+
const registered = await withServer(async rpc => {
|
|
40
|
+
const existing = await findWorkspace(rpc, workspace);
|
|
41
|
+
if (existing) return existing;
|
|
42
|
+
// The desktop launcher is optional (including a missing command or a timeout).
|
|
43
|
+
if (!(await open(workspace).catch(() => false))) return null;
|
|
44
|
+
for (let attempt = 0; attempt < 40; attempt++) {
|
|
45
|
+
await delay(250);
|
|
46
|
+
const id = await findWorkspace(rpc, workspace);
|
|
47
|
+
if (id) return id;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
});
|
|
51
|
+
if (registered) return registered;
|
|
52
|
+
// A fresh app-server can see a late desktop registration before creating a project.
|
|
53
|
+
return withServer(async rpc => {
|
|
54
|
+
const existing = await findWorkspace(rpc, workspace);
|
|
55
|
+
if (existing) return existing;
|
|
56
|
+
const { project } = await rpc("project/create", {
|
|
57
|
+
name: path.basename(workspace), roots: [{ path: workspace }],
|
|
58
|
+
idempotencyKey: createHash("sha256").update(workspace).digest("hex"),
|
|
59
|
+
});
|
|
60
|
+
if (!project?.id) throw Error("Codex 未返回项目 ID");
|
|
61
|
+
return project.id;
|
|
62
|
+
});
|
|
63
|
+
}
|