@taptap/cli 2026.11.6 → 2026.11.11
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 +52 -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 +1 -1
- package/skills/taptap-app-edit/references/app-edit-fields-and-packages.md +5 -3
- package/skills/taptap-asset-library/SKILL.md +2 -0
- package/skills/taptap-asset-library/references/asset-library-ingest.md +2 -2
- package/skills/taptap-asset-library/references/asset-library-search.md +23 -1
- package/skills/taptap-cli/SKILL.md +1 -0
- package/skills/taptap-cli/references/package-type-boundary.md +1 -0
- package/skills/taptap-cli/references/prerequisite-recovery.md +31 -0
- package/skills/taptap-identity/SKILL.md +3 -3
- package/skills/taptap-identity/references/identity-behavior-rules.md +1 -1
- package/skills/taptap-materials/SKILL.md +1 -1
- package/skills/taptap-package-management/SKILL.md +1 -1
- package/skills/taptap-package-management/references/package-management-diagnostics.md +3 -3
- package/skills/taptap-qualification/references/qualification-analysis.md +26 -0
- package/skills/taptap-qualification/references/qualification-materials.md +5 -1
- package/taptap-suite/SKILL.md +1 -0
- package/taptap-suite/references/taptap-app-edit/references/app-edit-field-map.md +1 -1
- package/taptap-suite/references/taptap-app-edit/references/app-edit-fields-and-packages.md +5 -3
- package/taptap-suite/references/taptap-asset-library/SKILL.md +2 -0
- 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 +23 -1
- package/taptap-suite/references/taptap-cli/references/package-type-boundary.md +1 -0
- package/taptap-suite/references/taptap-cli/references/prerequisite-recovery.md +31 -0
- package/taptap-suite/references/taptap-identity/SKILL.md +3 -3
- package/taptap-suite/references/taptap-identity/references/identity-behavior-rules.md +1 -1
- package/taptap-suite/references/taptap-materials/SKILL.md +1 -1
- package/taptap-suite/references/taptap-package-management/SKILL.md +1 -1
- package/taptap-suite/references/taptap-package-management/references/package-management-diagnostics.md +3 -3
- package/taptap-suite/references/taptap-qualification/references/qualification-analysis.md +26 -0
- package/taptap-suite/references/taptap-qualification/references/qualification-materials.md +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,58 @@ All notable TapTap CLI changes are documented here.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [v2026.11.11] - 2026-11-11
|
|
8
|
+
|
|
9
|
+
### Documentation
|
|
10
|
+
|
|
11
|
+
- Include the app scope in the package-type declaration recovery example.
|
|
12
|
+
|
|
13
|
+
### Tests
|
|
14
|
+
|
|
15
|
+
- Align the developer app listing fixture with the production response schema.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## [v2026.11.10] - 2026-11-10
|
|
19
|
+
|
|
20
|
+
### Improvements
|
|
21
|
+
|
|
22
|
+
- Explain recoverable prerequisites in one place (developer account, package-type
|
|
23
|
+
declaration, release quota, mini-game privacy) and route matching errors to the
|
|
24
|
+
command that fixes them.
|
|
25
|
+
- Retry rate-limited requests with Retry-After, pace overview metadata calls per
|
|
26
|
+
developer, and give foreground metadata refresh its own timeout budget.
|
|
27
|
+
- Match a numeric `app +list --kw` against app IDs (bounded paging) when the name
|
|
28
|
+
search is empty, and report the lookup in `id_lookup`.
|
|
29
|
+
- List composite `--data` inputs in `--help`, name the metadata-defined
|
|
30
|
+
operations in service help (they stay hidden from the command list), add
|
|
31
|
+
`schema <service> --names` for a light operation index, point at the embedded
|
|
32
|
+
skills (`skills list` / `skills read`), and validate closed catalog enums such
|
|
33
|
+
as `module_id` locally before sending.
|
|
34
|
+
|
|
35
|
+
### Fixes
|
|
36
|
+
|
|
37
|
+
- Give unknown commands and misused flags a usable next step instead of a bare
|
|
38
|
+
usage error.
|
|
39
|
+
- Preserve business error reasons in telemetry, skip `authorization_waiting` poll
|
|
40
|
+
noise, and keep the error payload when the gateway compacts it.
|
|
41
|
+
- Do not replay non-replayable write requests; keep selected-developer read
|
|
42
|
+
failures visible in `overview`; classify 429 as rate limiting with recovery
|
|
43
|
+
guidance.
|
|
44
|
+
- Refresh the capability catalog before resolving a shortcut operation on a fresh
|
|
45
|
+
install, so `app +list`/`overview` no longer fail until another command warms it.
|
|
46
|
+
- Correct `expected` semantics (null only for empty values), clarify conflict
|
|
47
|
+
hints, and keep internal error categories out of schema wording.
|
|
48
|
+
|
|
49
|
+
### Documentation
|
|
50
|
+
|
|
51
|
+
- Add a prerequisite recovery reference and align skill routing with it.
|
|
52
|
+
- Require a size check before recommending an asset-library icon, and state that
|
|
53
|
+
an AI declaration needs `package_types` plus a read-back confirmation.
|
|
54
|
+
- Document qualification subjects in Chinese and split saved-but-unapproved
|
|
55
|
+
materials from materials the user still has to prepare.
|
|
56
|
+
- Correct the generated schema text for qualification file fields and the AI
|
|
57
|
+
declaration package scope so descriptions match what the server accepts.
|
|
58
|
+
|
|
7
59
|
## [v2026.11.6] - 2026-11-06
|
|
8
60
|
|
|
9
61
|
### Improvements
|
|
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
|
+
6fcdfd9570a05d6a806041a767a22adbded27a9b6d642a3779f2a1f607ed760b bin/taptap-cli-darwin-amd64
|
|
2
|
+
c4f1139d81f7e7dd2c6fb7a8acb638d0287dce0bce3b8b7ae820ae9ced2c6d56 bin/taptap-cli-darwin-arm64
|
|
3
|
+
efb53c4345c32aab5506243203d69fbad8a1a7c049cc4c72a19cad3f47c36d8e bin/taptap-cli-linux-amd64
|
|
4
|
+
4b03c2a108650eedd80e049291cc9327227fa5024f9a10f5bfb3a527f1a11aa0 bin/taptap-cli-linux-arm64
|
|
5
|
+
21a72c368f2a06fdd12a962d2c01da68ec26ee1f6dc9d33b9b8c767fe22b05b8 bin/taptap-cli-linux-riscv64
|
|
6
|
+
3398016f48034d3de35b7b5cc8f53d05af2234bc4ab2d0d81cceac6109d7153c bin/taptap-cli-windows-amd64.exe
|
|
7
|
+
c66e58af8b0a023da090fa538c6df3d32b3ab8ac992c64e55ecbb6b1f95f59a5 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.11",
|
|
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
|
+
}
|