dknowc-dsh 1.4.0 → 2.1.0
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 +18 -19
- package/package.json +3 -3
- package/skills/dknowc-official-doc-writer/README.md +24 -18
- package/skills/dknowc-official-doc-writer/SKILL.md +118 -144
- package/skills/dknowc-official-doc-writer/attribution.json +5 -0
- package/skills/dknowc-official-doc-writer/reference/material_usage_guidance.md +5 -0
- package/skills/dknowc-official-doc-writer/reference/onboarding_scripts.md +60 -13
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/research_notes.md +34 -0
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +2 -9
- package/skills/dknowc-official-doc-writer/reference/sample_trace_report.html +38 -18
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +100 -33
- package/skills/dknowc-official-doc-writer/reference/search_intro.md +6 -6
- package/skills/dknowc-official-doc-writer/reference/search_policy.md +77 -55
- package/skills/dknowc-official-doc-writer/reference/task_router.md +6 -6
- package/skills/dknowc-official-doc-writer/scripts/api_key.py +41 -14
- package/skills/dknowc-official-doc-writer/scripts/attribution.py +61 -0
- package/skills/dknowc-official-doc-writer/scripts/build_trace_json.py +293 -0
- package/skills/dknowc-official-doc-writer/scripts/check_materials.py +263 -0
- package/skills/dknowc-official-doc-writer/scripts/deep_convert.py +235 -0
- package/skills/dknowc-official-doc-writer/scripts/deep_query.py +333 -0
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +14 -5
- package/skills/dknowc-official-doc-writer/scripts/extract_reference.py +245 -0
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +106 -18
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +13 -7
- package/skills/dknowc-official-doc-writer/scripts/mcp_convert.py +272 -0
- package/skills/dknowc-official-doc-writer/scripts/mcp_direct.py +257 -143
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +72 -8
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +14 -4
- package/skills/dknowc-official-doc-writer/scripts/prose_lint.py +6 -1
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +283 -59
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +264 -97
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +26 -57
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +13 -0
- package/skills/dknowc-ppt-assistant/README.md +14 -10
- package/skills/dknowc-ppt-assistant/SKILL.md +34 -16
- package/skills/dknowc-ppt-assistant/attribution.json +5 -0
- package/skills/dknowc-ppt-assistant/references/material_usage.md +10 -6
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +56 -15
- package/skills/dknowc-ppt-assistant/references/sample_trace_report.html +123 -115
- package/skills/dknowc-ppt-assistant/references/search_intro.md +1 -1
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +1 -1
- package/skills/dknowc-ppt-assistant/scripts/api_key.py +41 -14
- package/skills/dknowc-ppt-assistant/scripts/attribution.py +61 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +6 -4
- package/skills/dknowc-ppt-assistant/scripts/mcp_convert.py +188 -0
- package/skills/dknowc-ppt-assistant/scripts/preview_slide_html.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +356 -94
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +260 -233
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +35 -5
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +21 -16
- package/skills/dknowc-trusted-search/README.md +28 -24
- package/skills/dknowc-trusted-search/SKILL.md +217 -141
- package/skills/dknowc-trusted-search/attribution.json +5 -0
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +51 -12
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +129 -521
- package/skills/dknowc-trusted-search/resources/echarts.custom.min.js +48 -0
- package/skills/dknowc-trusted-search/scripts/api_key.py +41 -14
- package/skills/dknowc-trusted-search/scripts/attribution.py +61 -0
- package/skills/dknowc-trusted-search/scripts/chart_embed.py +350 -0
- package/skills/dknowc-trusted-search/scripts/deep_query.py +27 -6
- package/skills/dknowc-trusted-search/scripts/initialize.py +20 -29
- package/skills/dknowc-trusted-search/scripts/mcp_convert.py +198 -0
- package/skills/dknowc-trusted-search/scripts/merge_search_results.py +335 -0
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +339 -65
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +386 -210
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +30 -30
- package/src/index.js +2 -7
- package/skills/dknowc-ppt-assistant/scripts/adapt_mcp_result.py +0 -279
- package/skills/dknowc-trusted-consulting/README.md +0 -63
- package/skills/dknowc-trusted-consulting/SKILL.md +0 -241
- package/skills/dknowc-trusted-consulting/reference/consult_intro.md +0 -47
- package/skills/dknowc-trusted-consulting/reference/onboarding_scripts.md +0 -132
- package/skills/dknowc-trusted-consulting/reference/sample_consult_answer.md +0 -31
- package/skills/dknowc-trusted-consulting/reference/sample_trace_report.html +0 -867
- package/skills/dknowc-trusted-consulting/scripts/adapt_mcp_result.py +0 -283
- package/skills/dknowc-trusted-consulting/scripts/api_key.py +0 -41
- package/skills/dknowc-trusted-consulting/scripts/gov_chat.py +0 -508
- package/skills/dknowc-trusted-consulting/scripts/initialize.py +0 -123
- package/skills/dknowc-trusted-consulting/scripts/mcp_direct.py +0 -166
- package/skills/dknowc-trusted-consulting/scripts/register_key.mjs +0 -281
- package/skills/dknowc-trusted-consulting/scripts/render_trace_html.py +0 -2534
- package/skills/dknowc-trusted-search/scripts/adapt_mcp_result.py +0 -261
- package/skills/dknowc-trusted-search/scripts/render_policy_visualization.py +0 -894
|
@@ -1,34 +1,136 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
// node register_key.mjs
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
2
|
+
// MaaS key bootstrap helper for SkillHub Public.
|
|
3
|
+
// node register_key.mjs send --phone <phone>
|
|
4
|
+
// 退出码三态:0=短信已发送;2=需用户在验证页完成行为验证(CAPTCHA_REQUIRED,短信尚未发送);1=错误。
|
|
5
|
+
// node register_key.mjs register --phone <phone> --vcode <code> [--new-key]
|
|
6
|
+
// 可能返回 pending:true(账号已创建、密钥资源准备中)——非错误,稍后同参数重试一次。
|
|
7
|
+
// node register_key.mjs save-key # MCP create_api_key 拿到的密钥落盘(stdin 优先,或 --api-key)
|
|
8
|
+
// 2026-10-09 注册链路改造(user-auth maas-1.1.6):sendMessage 两分支(直发/验证链接)、
|
|
9
|
+
// X-MaaS 客户端观测请求头、错误码族映射、验证链接安全校验,详见各分支内注释。
|
|
7
10
|
|
|
8
11
|
import fs from "fs";
|
|
9
12
|
import os from "os";
|
|
10
13
|
import path from "path";
|
|
14
|
+
import { fileURLToPath } from "url";
|
|
15
|
+
|
|
16
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
17
|
+
const __dirname = path.dirname(__filename);
|
|
11
18
|
|
|
12
19
|
const DEFAULT_BASE = "https://platform.dknowc.cn/auth/home/userAuto";
|
|
13
20
|
const DEFAULT_OPEN_BASE = "https://open.dknowc.cn";
|
|
14
21
|
const DEFAULT_CHANNEL = "46A3BA1D-3E1A-4E8C-BD50-A6DCBEE1DB05";
|
|
15
22
|
const DEFAULT_TYPE = "11";
|
|
23
|
+
// body 的 source 是注册 API 的合同标记:服务端按 source="agent" 放行"老用户直接返 Key"
|
|
24
|
+
// 路径(2026-10-09 生产实测:source=dknowc-ppt-assistant 时返回"用户已存在!"错误,
|
|
25
|
+
// source=agent 时正常返回 Key)。它与 X-Dknowc-Attribution 统计声明里的 skill 标识是两个
|
|
26
|
+
// 字段:统计声明头的键名为 agentSource(2026-10-09 平台方确认改名,避免与 body 的
|
|
27
|
+
// source 混淆),值仍读 attribution.json 的 dknowc-ppt-assistant,不受本值影响。
|
|
16
28
|
const DEFAULT_SOURCE = "agent";
|
|
29
|
+
// 2026-10-09 注册链路改造:MaaS 客户端观测请求头(X-MaaS-*)用本 Skill 真实包名,不冒充
|
|
30
|
+
// 其他客户端;Client-Version 运行时读包根 SKILL.md;Instance-Id 由服务端响应头签发后落盘
|
|
31
|
+
// 回传,不自行生成(user-auth maas-1.1.6 契约)。
|
|
32
|
+
const MAAS_CLIENT_NAME = "dknowc-ppt-assistant";
|
|
33
|
+
const INSTANCE_FILE_NAME = "maas_client_instance";
|
|
17
34
|
const API_KEY_ENV = "DKNOWC_API_KEY";
|
|
18
35
|
const MAAS_PLATFORM_URL = "https://platform.dknowc.cn/auth/#/login";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
36
|
+
// Key 持久化目标:本机专用配置文件(XDG 规范路径),不再依赖 ~/.zshrc。
|
|
37
|
+
// 迁移:写入新文件成功后,清理 ~/.zshrc 中的历史 Key 块。
|
|
38
|
+
const KEY_FILE_NAME = "api_key";
|
|
39
|
+
const ZSHRC_START = "# >>> dknowc ppt assistant api key >>>";
|
|
40
|
+
const ZSHRC_END = "# <<< dknowc ppt assistant api key <<<";
|
|
41
|
+
// 历史 ~/.zshrc 标记块(含旧名),迁移期统一清理
|
|
42
|
+
const LEGACY_BLOCKS = [
|
|
43
|
+
[ZSHRC_START, ZSHRC_END],
|
|
44
|
+
["# >>> dknowc api key >>>", "# <<< dknowc api key <<<"],
|
|
45
|
+
];
|
|
22
46
|
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
return p.length >= 7 ? `${p.slice(0, 3)}****${p.slice(-4)}` : p;
|
|
47
|
+
function escapeRegExp(value) {
|
|
48
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
26
49
|
}
|
|
27
50
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
51
|
+
function apiKeyFilePath() {
|
|
52
|
+
const home = os.homedir();
|
|
53
|
+
if (process.platform === "win32") {
|
|
54
|
+
const appdata = process.env.APPDATA || home;
|
|
55
|
+
return path.join(appdata, "dknowc", KEY_FILE_NAME);
|
|
56
|
+
}
|
|
57
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
58
|
+
return path.join(xdg, "dknowc", KEY_FILE_NAME);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function removeLegacyZshrcBlocks() {
|
|
62
|
+
// 迁移:从 ~/.zshrc 移除历史 Key 标记块(避免污染 shell 配置)
|
|
63
|
+
const zshrcPath = path.join(os.homedir(), ".zshrc");
|
|
64
|
+
try {
|
|
65
|
+
let existing = fs.existsSync(zshrcPath) ? fs.readFileSync(zshrcPath, "utf8") : "";
|
|
66
|
+
if (!existing) return;
|
|
67
|
+
let changed = false;
|
|
68
|
+
for (const [start, end] of LEGACY_BLOCKS) {
|
|
69
|
+
const re = new RegExp(`${escapeRegExp(start)}[\\s\\S]*?${escapeRegExp(end)}\\n?`, "m");
|
|
70
|
+
if (re.test(existing)) { existing = existing.replace(re, ""); changed = true; }
|
|
71
|
+
}
|
|
72
|
+
if (changed) fs.writeFileSync(zshrcPath, existing, { encoding: "utf8", mode: 0o600 });
|
|
73
|
+
} catch (e) { /* 清理失败不阻断写入流程 */ }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function writeApiKeyToConfigFile(apiKey) {
|
|
77
|
+
// 写本机专用配置文件(纯文本一行 Key,600 权限);成功后清理历史 ~/.zshrc 块
|
|
78
|
+
const filePath = apiKeyFilePath();
|
|
79
|
+
try {
|
|
80
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
81
|
+
fs.writeFileSync(filePath, `${String(apiKey).trim()}\n`, { encoding: "utf8", mode: 0o600 });
|
|
82
|
+
removeLegacyZshrcBlocks();
|
|
83
|
+
return { written: true, path: filePath, error: null };
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return { written: false, path: filePath, error: e && e.message ? e.message : String(e) };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// —— 2026-10-09 注册链路改造新增(user-auth maas-1.1.6 契约)——
|
|
90
|
+
|
|
91
|
+
// Client-Instance-Id 持久化:与 api_key 同目录(~/.config/dknowc/maas_client_instance)。
|
|
92
|
+
// 值来自服务端响应头签发,仅做回传;读不到/落盘失败都不阻断请求。
|
|
93
|
+
function instanceIdFilePath() {
|
|
94
|
+
const home = os.homedir();
|
|
95
|
+
if (process.platform === "win32") {
|
|
96
|
+
const appdata = process.env.APPDATA || home;
|
|
97
|
+
return path.join(appdata, "dknowc", INSTANCE_FILE_NAME);
|
|
98
|
+
}
|
|
99
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
100
|
+
return path.join(xdg, "dknowc", INSTANCE_FILE_NAME);
|
|
101
|
+
}
|
|
102
|
+
function readInstanceId() {
|
|
103
|
+
try { return fs.readFileSync(instanceIdFilePath(), "utf-8").trim() || null; } catch { return null; }
|
|
104
|
+
}
|
|
105
|
+
function writeInstanceId(id) {
|
|
106
|
+
try {
|
|
107
|
+
fs.mkdirSync(path.dirname(instanceIdFilePath()), { recursive: true, mode: 0o700 });
|
|
108
|
+
fs.writeFileSync(instanceIdFilePath(), `${String(id).trim()}\n`, { encoding: "utf8", mode: 0o600 });
|
|
109
|
+
} catch { /* 落盘失败不阻断请求 */ }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 验证页链接安全校验:展示给用户前必须通过。规则(开发文档 2026-10-09):
|
|
113
|
+
// 与调用方预配置的 API base 同源(生产 https://platform.dknowc.cn;测试环境随 --base,
|
|
114
|
+
// 不硬编码生产 host)、精确页面路径、无用户名/密码、无 query、fragment 仅为 #token=
|
|
115
|
+
// 后跟 43 位 Base64URL 字符(A-Z a-z 0-9 _ -);拒绝编码/路径遍历/其他版本与非规范 URL。
|
|
116
|
+
const VERIFICATION_PAGE_PATH = "/auth/open-api/maas/identity/v1/sms-verification-page";
|
|
117
|
+
function verificationPageOrigin(base) {
|
|
118
|
+
// 从 base 裁掉 /auth/home/userAuto 得到环境前缀(生产即 https://platform.dknowc.cn)
|
|
119
|
+
const m = String(base || "").match(/^(https?:\/\/[^\/]+)\/auth\/home\/userAuto\/?$/);
|
|
120
|
+
return m ? m[1] : null;
|
|
121
|
+
}
|
|
122
|
+
function validateVerificationUrl(rawUrl, base) {
|
|
123
|
+
const origin = verificationPageOrigin(base);
|
|
124
|
+
if (!origin) return null;
|
|
125
|
+
try {
|
|
126
|
+
const u = new URL(String(rawUrl || ""));
|
|
127
|
+
if (u.username || u.password || u.search) return null;
|
|
128
|
+
if (u.origin !== origin) return null;
|
|
129
|
+
if (u.pathname !== VERIFICATION_PAGE_PATH) return null;
|
|
130
|
+
if (!/^#token=[A-Za-z0-9_-]{43}$/.test(u.hash)) return null;
|
|
131
|
+
if (String(rawUrl) !== u.toString()) return null; // 拒绝编码/非规范写法
|
|
132
|
+
return u.toString();
|
|
133
|
+
} catch { return null; }
|
|
32
134
|
}
|
|
33
135
|
|
|
34
136
|
function parseArgs(argv) {
|
|
@@ -51,13 +153,42 @@ function parseArgs(argv) {
|
|
|
51
153
|
return out;
|
|
52
154
|
}
|
|
53
155
|
|
|
156
|
+
// —— 统一来源声明(X-Dknowc-Attribution,按 MaaS 平台方约定的调用来源统计方案)——
|
|
157
|
+
// 读包根 attribution.json(kind/source/channel)+ SKILL.md 的 version;仅统计用、
|
|
158
|
+
// 不参与鉴权;读取失败返回 null(不加头、不阻断请求)。
|
|
159
|
+
// 头内键名用 agentSource(2026-10-09 平台方改名,区别于注册 body 的 source 合同标记)。
|
|
160
|
+
function readSkillVersion() {
|
|
161
|
+
// 运行时从包根 SKILL.md 解析版本号(供 X-Dknowc-Attribution 与 X-MaaS-Client-Version 共用)
|
|
162
|
+
try {
|
|
163
|
+
const root = path.resolve(__dirname, "..");
|
|
164
|
+
const m = fs.readFileSync(path.join(root, "SKILL.md"), "utf-8").match(/^version:\s*"?([^"\n]+)"?/m);
|
|
165
|
+
return m ? m[1].trim() : "unknown";
|
|
166
|
+
} catch { return "unknown"; }
|
|
167
|
+
}
|
|
168
|
+
function buildAttributionHeader() {
|
|
169
|
+
try {
|
|
170
|
+
const root = path.resolve(__dirname, "..");
|
|
171
|
+
const meta = JSON.parse(fs.readFileSync(path.join(root, "attribution.json"), "utf-8"));
|
|
172
|
+
if (!meta.source) return null;
|
|
173
|
+
const parts = [`kind=${meta.kind || "skill"}`, `agentSource=${meta.source}`];
|
|
174
|
+
const v = readSkillVersion();
|
|
175
|
+
if (v !== "unknown") parts.push(`version=${v}`);
|
|
176
|
+
if (meta.channel) parts.push(`channel=${meta.channel}`);
|
|
177
|
+
return parts.join(";");
|
|
178
|
+
} catch { return null; }
|
|
179
|
+
}
|
|
180
|
+
function withAttribution(headers) {
|
|
181
|
+
const attr = buildAttributionHeader();
|
|
182
|
+
return attr ? { ...headers, "X-Dknowc-Attribution": attr } : headers;
|
|
183
|
+
}
|
|
184
|
+
|
|
54
185
|
async function postJson(url, payload, headers = {}) {
|
|
55
186
|
const controller = new AbortController();
|
|
56
187
|
const timer = setTimeout(() => controller.abort(), 30000);
|
|
57
188
|
try {
|
|
58
189
|
const response = await fetch(url, {
|
|
59
190
|
method: "POST",
|
|
60
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
191
|
+
headers: withAttribution({ "Content-Type": "application/json", ...headers }),
|
|
61
192
|
body: JSON.stringify(payload),
|
|
62
193
|
signal: controller.signal,
|
|
63
194
|
});
|
|
@@ -93,6 +224,46 @@ function genPassword() {
|
|
|
93
224
|
return chars.join("");
|
|
94
225
|
}
|
|
95
226
|
|
|
227
|
+
// 注册链路(sendMessage / register)专用 POST:
|
|
228
|
+
// - 携带 X-MaaS 客户端观测请求头(user-auth maas-1.1.6):Entry-Surface 固定 DIRECT_API、
|
|
229
|
+
// Client-Name 为本 Skill 真实包名、Client-Version 运行时读 SKILL.md、Client-Instance-Id
|
|
230
|
+
// 首次不带(服务端响应头签发后落盘,后续请求回传,不自行生成);
|
|
231
|
+
// - 返回 { body, requestUnknown }:requestUnknown=true 表示超时/断网等"结果未确认"情形
|
|
232
|
+
// (发送结果未知时不得立即补发,由调用方按专用话术处理)。
|
|
233
|
+
async function postRegister(url, payload) {
|
|
234
|
+
const controller = new AbortController();
|
|
235
|
+
const timer = setTimeout(() => controller.abort(), 30000);
|
|
236
|
+
try {
|
|
237
|
+
const headers = withAttribution({ "Content-Type": "application/json" });
|
|
238
|
+
headers["X-MaaS-Entry-Surface"] = "DIRECT_API";
|
|
239
|
+
headers["X-MaaS-Client-Name"] = MAAS_CLIENT_NAME;
|
|
240
|
+
headers["X-MaaS-Client-Version"] = readSkillVersion();
|
|
241
|
+
const iid = readInstanceId();
|
|
242
|
+
if (iid) headers["X-MaaS-Client-Instance-Id"] = iid;
|
|
243
|
+
const response = await fetch(url, {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers,
|
|
246
|
+
body: JSON.stringify(payload),
|
|
247
|
+
signal: controller.signal,
|
|
248
|
+
});
|
|
249
|
+
const issued = response.headers.get("X-MaaS-Client-Instance-Id");
|
|
250
|
+
if (issued && issued.trim()) writeInstanceId(issued.trim());
|
|
251
|
+
const text = await response.text();
|
|
252
|
+
let body;
|
|
253
|
+
try { body = JSON.parse(text); }
|
|
254
|
+
catch { body = { status: false, msg: `非 JSON 响应:${text.slice(0, 200)}` }; }
|
|
255
|
+
return { body, requestUnknown: false };
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const msg = error && error.message ? error.message : String(error);
|
|
258
|
+
return {
|
|
259
|
+
body: { status: false, msg: `请求异常:${msg}` },
|
|
260
|
+
requestUnknown: true,
|
|
261
|
+
};
|
|
262
|
+
} finally {
|
|
263
|
+
clearTimeout(timer);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
96
267
|
async function createNewApiKey(openBase, existingApiKey, name, remark) {
|
|
97
268
|
const url = `${openBase.replace(/\/$/, "")}/open-api/maas/api-key/create`;
|
|
98
269
|
const result = await postJson(
|
|
@@ -104,42 +275,19 @@ async function createNewApiKey(openBase, existingApiKey, name, remark) {
|
|
|
104
275
|
return { result, apiKey };
|
|
105
276
|
}
|
|
106
277
|
|
|
107
|
-
function
|
|
108
|
-
|
|
278
|
+
function maskPhone(phone) {
|
|
279
|
+
const p = String(phone || "");
|
|
280
|
+
return p.length === 11 ? `${p.slice(0, 3)}****${p.slice(-4)}` : p;
|
|
109
281
|
}
|
|
110
282
|
|
|
111
|
-
function
|
|
112
|
-
return
|
|
283
|
+
function isValidCnPhone(phone) {
|
|
284
|
+
return /^1[3-9]\d{9}$/.test(String(phone || ""));
|
|
113
285
|
}
|
|
114
286
|
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
`export ${API_KEY_ENV}=${shellSingleQuote(apiKey)}`,
|
|
120
|
-
ZSHRC_END,
|
|
121
|
-
"",
|
|
122
|
-
].join("\n");
|
|
123
|
-
let existing = "";
|
|
124
|
-
try {
|
|
125
|
-
existing = fs.existsSync(zshrcPath) ? fs.readFileSync(zshrcPath, "utf8") : "";
|
|
126
|
-
} catch (error) {
|
|
127
|
-
const msg = error && error.message ? error.message : String(error);
|
|
128
|
-
return { written: false, path: zshrcPath, error: msg };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const pattern = new RegExp(`${escapeRegExp(ZSHRC_START)}[\\s\\S]*?${escapeRegExp(ZSHRC_END)}\\n?`, "m");
|
|
132
|
-
const next = pattern.test(existing)
|
|
133
|
-
? existing.replace(pattern, block)
|
|
134
|
-
: `${existing}${existing && !existing.endsWith("\n") ? "\n" : ""}${block}`;
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
fs.writeFileSync(zshrcPath, next, { encoding: "utf8", mode: 0o600 });
|
|
138
|
-
return { written: true, path: zshrcPath, error: null };
|
|
139
|
-
} catch (error) {
|
|
140
|
-
const msg = error && error.message ? error.message : String(error);
|
|
141
|
-
return { written: false, path: zshrcPath, error: msg };
|
|
142
|
-
}
|
|
287
|
+
function maskKey(apiKey) {
|
|
288
|
+
if (!apiKey) return null;
|
|
289
|
+
if (apiKey.length <= 12) return "***";
|
|
290
|
+
return `${apiKey.slice(0, 7)}...${apiKey.slice(-4)}`;
|
|
143
291
|
}
|
|
144
292
|
|
|
145
293
|
async function main() {
|
|
@@ -152,24 +300,81 @@ async function main() {
|
|
|
152
300
|
console.error("缺少 --phone");
|
|
153
301
|
process.exit(2);
|
|
154
302
|
}
|
|
155
|
-
|
|
156
|
-
|
|
303
|
+
const channel = args.channel && args.channel !== true ? args.channel : DEFAULT_CHANNEL;
|
|
304
|
+
if (!isValidCnPhone(args.phone)) {
|
|
305
|
+
console.log(JSON.stringify({
|
|
306
|
+
status: false,
|
|
307
|
+
msg: "手机号格式不正确",
|
|
308
|
+
user_message: "这个手机号格式好像不对,麻烦核对一下再发我。",
|
|
309
|
+
}));
|
|
310
|
+
process.exit(1);
|
|
311
|
+
}
|
|
312
|
+
const { body: result, requestUnknown } = await postRegister(`${base}/sendMessage`, {
|
|
157
313
|
phone: args.phone,
|
|
158
314
|
type: "register",
|
|
159
|
-
channel
|
|
315
|
+
channel,
|
|
160
316
|
});
|
|
161
|
-
|
|
317
|
+
const code = String(result.code || (result.data && result.data.code) || "");
|
|
318
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥。
|
|
319
|
+
// 2026-10-09 两分支(user-auth maas-1.1.6):status:true 短信已直发;
|
|
320
|
+
// CAPTCHA_REQUIRED 短信尚未发送——需用户在验证页完成验证后由页面发码,
|
|
321
|
+
// 用户完成页面验证后 Agent 不得再调 sendMessage(验证页不是可无限发码的通行证)。
|
|
162
322
|
let userMessage;
|
|
163
|
-
|
|
323
|
+
let exitCode = 1;
|
|
324
|
+
let extra = {};
|
|
325
|
+
if (code === "CAPTCHA_REQUIRED") {
|
|
326
|
+
const vUrl = validateVerificationUrl(result.verificationUrl, base);
|
|
327
|
+
if (vUrl) {
|
|
328
|
+
userMessage = `短信还没发送。请打开这个验证链接,核对手机号(${maskPhone(args.phone)})后点击“验证并发送短信”,按提示完成验证:\n${vUrl}\n收到短信后把 6 位验证码回复给我,我继续开通,不需要重新填写注册表。`;
|
|
329
|
+
extra = {
|
|
330
|
+
verificationUrl: vUrl,
|
|
331
|
+
verificationUrlValidated: true,
|
|
332
|
+
instruction: "等待用户验证:将 user_message(含链接)转述给用户后暂停,等用户回复 6 位短信验证码。用户在页面完成验证后不得再次调用 send;链接一次性、约 10 分钟有效,过期或已用需用户同意后重新 send 申请新链接。",
|
|
333
|
+
};
|
|
334
|
+
exitCode = 2;
|
|
335
|
+
} else {
|
|
336
|
+
// 链接校验不通过:不向用户展示原始链接(防钓鱼/防篡改),按"链接不可用"处理
|
|
337
|
+
userMessage = "这次开通需要先完成一次页面验证,但验证链接校验没有通过(可能已过期或不可用)。跟我说一声,我重新给你申请一个新的验证链接。";
|
|
338
|
+
extra = {
|
|
339
|
+
verificationUrl: undefined, // 确保 JSON 输出不携带未通过校验的链接
|
|
340
|
+
code: "CAPTCHA_LINK_UNAVAILABLE",
|
|
341
|
+
msg: result.msg || "验证链接校验未通过",
|
|
342
|
+
instruction: "不得展示未通过校验的链接;用户同意后重新调用 send 申请新链接。",
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
} else if (code === "SMS_RATE_LIMIT_PHONE") {
|
|
346
|
+
// 展示服务端间隔(若有);未给时只说稍后再试,不伪造精确倒计时
|
|
347
|
+
const secs = Number(result.retryAfterSeconds || (result.data && result.data.retryAfterSeconds) || 0);
|
|
348
|
+
const mins = secs > 0 ? Math.max(1, Math.floor(secs / 60)) : 0;
|
|
349
|
+
userMessage = mins > 0
|
|
350
|
+
? `验证码发送太频繁了,短信通道要求约 ${mins} 分钟后再试。你先忙别的,想重发时跟我说一声。`
|
|
351
|
+
: "验证码发送太频繁了,稍等几分钟再试;想重发时跟我说一声。";
|
|
352
|
+
extra = { instruction: "限频停点:等待用户明确要求且冷却时间过后才重试,不自动重发。" };
|
|
353
|
+
} else if (code === "CAPTCHA_LINK_UNAVAILABLE" || code === "CAPTCHA_REPLAY") {
|
|
354
|
+
userMessage = "刚才的验证链接已过期或已使用。跟我说一声,我重新给你申请一个新的验证链接。";
|
|
355
|
+
extra = { instruction: "不重复提交旧链接 token;用户确认后重新调用 send 申请新链接。" };
|
|
356
|
+
} else if (code === "CAPTCHA_REJECTED" || code === "CAPTCHA_BUSY" || code === "CAPTCHA_STATE_UNAVAILABLE") {
|
|
357
|
+
userMessage = "这次验证没有通过(或验证服务暂时不可用)。可以稍后重新打开链接再试;还是不行跟我说,我重新申请一个。";
|
|
358
|
+
extra = { instruction: "据实提示验证未通过或暂不可用,不伪造验证凭据、不把失败当成功。" };
|
|
359
|
+
} else if (code.startsWith("SMS_PROVIDER_")) {
|
|
360
|
+
userMessage = `短信服务暂时出了点问题,发不出验证码。可以稍后再试,或用网页方式开通:${MAAS_PLATFORM_URL}`;
|
|
361
|
+
extra = { instruction: "短信供应商失败停点:不自动重试。" };
|
|
362
|
+
} else if (requestUnknown || String(result.msg || "").startsWith("非 JSON 响应")) {
|
|
363
|
+
// 发送结果未确认(超时/断网/响应异常):先让用户检查是否已收到短信,不马上补发
|
|
364
|
+
userMessage = "刚才发送请求的结果没有确认(网络波动)。先看一下手机有没有收到验证码短信:收到了直接把 6 位验证码发我;没收到再跟我说,我重新发。";
|
|
365
|
+
extra = { resultUnknown: true, instruction: "结果未确认停点:先等用户确认是否收到短信,未确认前不重新发送。" };
|
|
366
|
+
} else if (result.status) {
|
|
164
367
|
userMessage = `验证码已发送到 ${maskPhone(args.phone)},请把最新一条短信里的 6 位验证码发我。`;
|
|
368
|
+
exitCode = 0;
|
|
165
369
|
} else if (String(result.msg || "").includes("手机号")) {
|
|
166
370
|
userMessage = "这个手机号格式好像不对,麻烦核对一下再发我。";
|
|
167
371
|
} else {
|
|
168
|
-
userMessage = `验证码发送没成功(可能是网络或短信通道问题),可以再试一次;如果连续失败,也可以用网页方式开通:${
|
|
372
|
+
userMessage = `验证码发送没成功(可能是网络或短信通道问题),可以再试一次;如果连续失败,也可以用网页方式开通:${MAAS_PLATFORM_URL}`;
|
|
169
373
|
}
|
|
170
|
-
console.log(JSON.stringify({ ...result, user_message: userMessage }));
|
|
171
|
-
if (
|
|
172
|
-
|
|
374
|
+
console.log(JSON.stringify({ ...result, ...extra, user_message: userMessage }));
|
|
375
|
+
if (exitCode === 0) console.error("验证码已发送(话术见 user_message,请向用户转述后索取 6 位验证码)。");
|
|
376
|
+
if (exitCode === 2) console.error("需要用户在验证页完成验证(话术见 user_message):暂停等待短信验证码;用户完成后不得再调 send。");
|
|
377
|
+
process.exit(exitCode);
|
|
173
378
|
}
|
|
174
379
|
|
|
175
380
|
if (cmd === "register") {
|
|
@@ -190,79 +395,136 @@ async function main() {
|
|
|
190
395
|
source: args.source && args.source !== true ? args.source : DEFAULT_SOURCE,
|
|
191
396
|
};
|
|
192
397
|
|
|
193
|
-
const result = await
|
|
398
|
+
const { body: result } = await postRegister(`${base}/register`, payload);
|
|
194
399
|
const data = result.data || {};
|
|
195
|
-
|
|
196
|
-
|
|
400
|
+
// 2026-10-09(user-auth maas-1.1.6):registered:true + deliveryStatus:"PENDING"——
|
|
401
|
+
// 账号已创建、Key 资源尚在准备。非错误:不盲目重注册,等用户示意后在短信码有效期内
|
|
402
|
+
// (5 分钟)用同参数重试一次;过期再走重新发码流程。
|
|
403
|
+
if (!data.apiKey && (data.deliveryStatus === "PENDING" || result.registered === true)) {
|
|
404
|
+
console.log(JSON.stringify({
|
|
405
|
+
status: false,
|
|
406
|
+
pending: true,
|
|
407
|
+
msg: result.msg || null,
|
|
408
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥
|
|
409
|
+
user_message: "账号已经创建成功,系统正在准备你的访问密钥(一般一两分钟内就好)。稍等后跟我说一声“再试”,我再确认一次;不用重新注册,也不用重新发验证码。",
|
|
410
|
+
instruction: "非错误:不要重新注册、不要重新发码。等用户示意后用同一手机号与同一验证码重试一次 register(短信码 5 分钟有效);若提示验证码过期,再走重新发码流程。",
|
|
411
|
+
}));
|
|
412
|
+
process.exit(0);
|
|
413
|
+
}
|
|
414
|
+
let apiKey = result.status && data.apiKey ? data.apiKey : "";
|
|
197
415
|
let newKeyCreated = false;
|
|
198
416
|
let newKeyError = null;
|
|
199
|
-
|
|
417
|
+
|
|
418
|
+
if (apiKey && args["new-key"]) {
|
|
200
419
|
const keyName = args["new-key-name"] && args["new-key-name"] !== true
|
|
201
420
|
? args["new-key-name"]
|
|
202
421
|
: payload.apiKeyName;
|
|
203
422
|
const keyRemark = args["new-key-remark"] && args["new-key-remark"] !== true
|
|
204
423
|
? args["new-key-remark"]
|
|
205
|
-
: "由 SkillHub
|
|
424
|
+
: "由 SkillHub 深知可信PPT按用户要求重新生成";
|
|
206
425
|
const created = await createNewApiKey(
|
|
207
426
|
args["open-base"] && args["open-base"] !== true ? args["open-base"] : DEFAULT_OPEN_BASE,
|
|
208
|
-
|
|
427
|
+
apiKey,
|
|
209
428
|
keyName,
|
|
210
429
|
keyRemark,
|
|
211
430
|
);
|
|
212
431
|
if (created.apiKey) {
|
|
213
|
-
|
|
432
|
+
apiKey = created.apiKey;
|
|
214
433
|
newKeyCreated = true;
|
|
215
434
|
} else {
|
|
216
|
-
//
|
|
435
|
+
// 新建失败:沿用原密钥继续并如实告知,不弃 key、不冒充新 Key
|
|
217
436
|
newKeyError = created.result.errmsg || created.result.msg || "新 API Key 创建失败";
|
|
218
437
|
}
|
|
219
438
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
439
|
+
|
|
440
|
+
// 注册成功即写入本机专用配置文件(--no-persist 可关闭):脚本直读文件,
|
|
441
|
+
// 避免宿主进程读不到环境变量造成"每任务重新注册"的误判。
|
|
442
|
+
const registered = Boolean(apiKey);
|
|
443
|
+
const persistResult = registered && !args["no-persist"]
|
|
444
|
+
? writeApiKeyToConfigFile(apiKey)
|
|
445
|
+
: { written: false, path: apiKeyFilePath(), error: null };
|
|
446
|
+
|
|
223
447
|
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥。
|
|
224
|
-
// 权益告知(300 次额度 + 实名认证赠金)已前移到开通引导(initialize.guide_message / onboarding_scripts S1),
|
|
225
|
-
// 此处只做轻确认,避免成功节点信息过重导致转述截断。
|
|
226
448
|
let userMessage;
|
|
227
|
-
if (
|
|
228
|
-
userMessage =
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
233
|
-
}
|
|
234
|
-
} else if (String(result.msg || "").includes("验证码")) {
|
|
449
|
+
if (registered && data.existed) {
|
|
450
|
+
userMessage = "这个手机号之前开通过,已直接找回原来的密钥和积分,不用重新注册。我马上开始检索。";
|
|
451
|
+
} else if (registered) {
|
|
452
|
+
userMessage = "开通成功,访问密钥已写入本机,注册赠送的 10 万积分已到账。我马上开始检索。";
|
|
453
|
+
} else if (/vcode|验证码/i.test(String(result.msg || ""))) {
|
|
235
454
|
userMessage = `验证码校验没通过(可能是输入有误或已过期)。请核对 ${maskPhone(args.phone)} 最新一条短信的 6 位验证码重新发我;需要我重新发送一条,直接说一声。`;
|
|
455
|
+
} else if (String(result.msg || "").includes("已存在")) {
|
|
456
|
+
// 老用户但服务端未放行返 Key(2026-10-09 生产实测过此形态):据实说明并引导网页
|
|
457
|
+
// 登录,不冒充找回成功、不诱导重复注册。
|
|
458
|
+
userMessage = `这个手机号之前注册过深知账号,这次没能自动取回密钥。可以稍后再试一次,或到平台用手机号验证码登录查看:${MAAS_PLATFORM_URL}`;
|
|
236
459
|
} else {
|
|
237
|
-
userMessage = `开通服务暂时没连上(${result.msg || "网络波动"}),可以稍后再试,或用网页方式开通:${
|
|
460
|
+
userMessage = `开通服务暂时没连上(${result.msg || "网络波动"}),可以稍后再试,或用网页方式开通:${MAAS_PLATFORM_URL}`;
|
|
461
|
+
}
|
|
462
|
+
if (registered && newKeyError) {
|
|
463
|
+
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
238
464
|
}
|
|
465
|
+
|
|
239
466
|
console.log(JSON.stringify({
|
|
240
|
-
status:
|
|
467
|
+
status: registered,
|
|
241
468
|
msg: result.msg,
|
|
242
469
|
url: data.url || null,
|
|
243
470
|
existed: Boolean(data.existed),
|
|
244
471
|
keyCreatedByRegister: Boolean(data.keyCreated),
|
|
245
472
|
newKeyRequested: Boolean(args["new-key"]),
|
|
246
473
|
newKeyCreated,
|
|
247
|
-
user_message: userMessage,
|
|
248
474
|
envName: API_KEY_ENV,
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
475
|
+
// 明文 Key 只在"未写入本机配置文件、必须靠它临时注入环境变量"时才输出,其余情况
|
|
476
|
+
// 只给掩码——脚本输出可能被宿主 UI 回显或模型贴进对话
|
|
477
|
+
apiKey: (apiKey && !persistResult.written && !args["no-persist"]) ? apiKey : null,
|
|
478
|
+
apiKeyMasked: maskKey(apiKey),
|
|
479
|
+
keyFilePersisted: Boolean(persistResult.written),
|
|
480
|
+
keyFilePath: persistResult.path || null,
|
|
481
|
+
keyFileError: persistResult.error || null,
|
|
482
|
+
persistInstruction: persistResult.written
|
|
483
|
+
? `访问密钥已写入本机专用配置文件(${persistResult.path},仅本机、600 权限),后续任务直接读取、无需重复注册。`
|
|
484
|
+
: `本次返回的 apiKey 仅供当前任务临时注入 ${API_KEY_ENV}(不得展示给用户、不得写入公开文件)。`,
|
|
485
|
+
fallbackRegisterUrl: MAAS_PLATFORM_URL,
|
|
259
486
|
newKeyError,
|
|
260
|
-
|
|
487
|
+
user_message: userMessage,
|
|
488
|
+
}));
|
|
489
|
+
process.exit(apiKey && !newKeyError ? 0 : 1);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (cmd === "save-key") {
|
|
493
|
+
// MCP create_api_key 拿到的密钥经 stdin 传入(避免出现在命令行参数与日志),--api-key 兜底
|
|
494
|
+
let key = "";
|
|
495
|
+
if (args["api-key"] && args["api-key"] !== true) {
|
|
496
|
+
key = String(args["api-key"]).trim();
|
|
497
|
+
} else {
|
|
498
|
+
key = await new Promise((resolve) => {
|
|
499
|
+
let buf = "";
|
|
500
|
+
process.stdin.setEncoding("utf8");
|
|
501
|
+
process.stdin.on("data", (chunk) => { buf += chunk; });
|
|
502
|
+
process.stdin.on("end", () => resolve(buf.trim()));
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
const looksValid = /^sk-/.test(key) && key.length >= 20;
|
|
506
|
+
if (!looksValid) {
|
|
507
|
+
console.log(JSON.stringify({
|
|
508
|
+
status: false,
|
|
509
|
+
user_message: "密钥格式不对(应以 sk- 开头),没有写入本机。请重新获取后再试。",
|
|
510
|
+
}));
|
|
511
|
+
process.exit(1);
|
|
512
|
+
}
|
|
513
|
+
const saved = writeApiKeyToConfigFile(key);
|
|
514
|
+
console.log(JSON.stringify({
|
|
515
|
+
status: Boolean(saved.written),
|
|
516
|
+
keyFilePath: saved.path || null,
|
|
517
|
+
keyFileError: saved.error || null,
|
|
518
|
+
envName: API_KEY_ENV,
|
|
519
|
+
apiKeyMasked: maskKey(key),
|
|
520
|
+
user_message: saved.written
|
|
521
|
+
? "已通过你的深知可信工作台授权直接开通搜索功能,无需手机号验证,马上开始检索。"
|
|
522
|
+
: `密钥写入本机配置文件失败(${saved.error || "未知原因"}),本次可先用临时密钥继续,稍后再试。`,
|
|
261
523
|
}));
|
|
262
|
-
process.exit(
|
|
524
|
+
process.exit(saved.written ? 0 : 1);
|
|
263
525
|
}
|
|
264
526
|
|
|
265
|
-
console.error("用法: node register_key.mjs <send|register> ...");
|
|
527
|
+
console.error("用法: node register_key.mjs <send|register|save-key> ...");
|
|
266
528
|
process.exit(2);
|
|
267
529
|
}
|
|
268
530
|
|