dknowc-dsh 2.3.0 → 2.3.1
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 +3 -47
- package/package.json +2 -2
- package/skills/dknowc-official-doc-writer/README.md +23 -14
- package/skills/dknowc-official-doc-writer/SKILL.md +4 -3
- package/skills/dknowc-official-doc-writer/reference/anti_ai_patterns.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/initialization.md +19 -16
- package/skills/dknowc-official-doc-writer/reference/local_memory_guide.md +7 -7
- package/skills/dknowc-official-doc-writer/reference/materials_pipeline.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/onboarding_scripts.md +42 -31
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +10 -11
- package/skills/dknowc-official-doc-writer/reference/research_notes.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/revision_workflow.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +7 -7
- package/skills/dknowc-official-doc-writer/reference/search_intro.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/standards/00_index.md +63 -77
- package/skills/dknowc-official-doc-writer/reference/standards/99_expressions.md +0 -1
- package/skills/dknowc-official-doc-writer/reference/task_router.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/working_principles.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/writing_rules.md +1 -1
- package/skills/dknowc-official-doc-writer/scripts/api_key.py +9 -2
- package/skills/dknowc-official-doc-writer/scripts/attribution.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/build_trace_json.py +11 -9
- package/skills/dknowc-official-doc-writer/scripts/check_materials.py +8 -5
- package/skills/dknowc-official-doc-writer/scripts/deep_convert.py +4 -4
- package/skills/dknowc-official-doc-writer/scripts/deep_query.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +3 -2
- package/skills/dknowc-official-doc-writer/scripts/extract_reference.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +30 -13
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +15 -7
- package/skills/dknowc-official-doc-writer/scripts/mcp_direct.py +2 -3
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +13 -6
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/prose_lint.py +7 -2
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +538 -205
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +10 -10
- package/skills/dknowc-official-doc-writer/scripts/review_document.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +3 -3
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +1 -1
- package/skills/dknowc-ppt-assistant/README.md +21 -7
- package/skills/dknowc-ppt-assistant/SKILL.md +5 -5
- package/skills/dknowc-ppt-assistant/THIRD_PARTY_NOTICES.md +3 -2
- package/skills/dknowc-ppt-assistant/references/initialization.md +45 -6
- package/skills/dknowc-ppt-assistant/references/material_usage.md +7 -7
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +48 -17
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +1 -1
- package/skills/dknowc-ppt-assistant/references/upstream-example/README.md +2 -0
- package/skills/dknowc-ppt-assistant/scripts/api_key.py +7 -0
- package/skills/dknowc-ppt-assistant/scripts/attribution.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/compact_svg_coordinates.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/config.py +9 -9
- package/skills/dknowc-ppt-assistant/scripts/console_encoding.py +1 -2
- package/skills/dknowc-ppt-assistant/scripts/error_helper.py +3 -3
- package/skills/dknowc-ppt-assistant/scripts/hyperlink_contract.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +11 -13
- package/skills/dknowc-ppt-assistant/scripts/language_tags.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/mcp_convert.py +17 -15
- package/skills/dknowc-ppt-assistant/scripts/mcp_direct.py +3 -2
- package/skills/dknowc-ppt-assistant/scripts/native_payloads.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_animations.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/__init__.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/errors.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/formula.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/loader.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/models.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/registry.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantic_hash.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_shapes/semantics.py +3 -3
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/animation_import.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/formula_import.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/hyperlinks.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_authoring.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_registry_to_svg.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/preset_svg_markup.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/slide_to_svg.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_to_svg/transition_import.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/pptx_transitions.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/preset_shape_svg.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/project_utils.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +710 -322
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +9 -9
- package/skills/dknowc-ppt-assistant/scripts/resource_paths.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/shape_boolean_svg.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/slide_roster.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/__init__.py +2 -9
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/align_embed_images.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/crop_images.py +3 -3
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/__init__.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/checker.py +9 -10
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/cli.py +4 -4
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/svg_contracts.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_quality/xml_support.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_quality_checker.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/hyperlinks.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_colors.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/drawingml/theme_fonts.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/geometry_properties.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_ast.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_compiler.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_omml.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_parser.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_profile.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/formula_run_properties.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/native_objects/inline_formula.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/builder.py +9 -11
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/cli.py +7 -5
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_structure.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/pptx_package/template_validation.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/semantic_markers.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/shape_boolean.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx/text_outline.py +2 -2
- package/skills/dknowc-ppt-assistant/scripts/svg_to_pptx.py +1 -1
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +22 -5
- package/skills/dknowc-ppt-assistant/scripts/update_spec.py +25 -1
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +11 -9
- package/skills/dknowc-ppt-assistant/workflows/routing.md +1 -1
- package/skills/dknowc-trusted-search/README.md +8 -8
- package/skills/dknowc-trusted-search/SKILL.md +74 -56
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +40 -15
- package/skills/dknowc-trusted-search/scripts/api_key.py +11 -26
- package/skills/dknowc-trusted-search/scripts/attribution.py +1 -1
- package/skills/dknowc-trusted-search/scripts/deep_query.py +1 -1
- package/skills/dknowc-trusted-search/scripts/initialize.py +10 -10
- package/skills/dknowc-trusted-search/scripts/mcp_direct.py +3 -2
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +617 -275
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +4 -4
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +2 -2
- package/skills/dknowc-official-doc-writer/scripts/mcp_convert.py +0 -287
- package/skills/dknowc-ppt-assistant/references/upstream-docs/svg-pipeline.md +0 -842
- package/skills/dknowc-ppt-assistant/scripts/finalize_svg.py +0 -507
- package/skills/dknowc-ppt-assistant/scripts/svg_finalize/svg_rect_to_path.py +0 -346
- package/skills/dknowc-trusted-search/scripts/mcp_convert.py +0 -198
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// MaaS
|
|
3
|
-
// node register_key.mjs
|
|
4
|
-
//
|
|
5
|
-
//
|
|
2
|
+
// 国内 MaaS 注册助手(供 skill 调用;纯 node,内置 fetch,无三方依赖)。
|
|
3
|
+
// node register_key.mjs [--guide URL] challenge --phone <p> [--channel <c>]
|
|
4
|
+
// 可选 --clean-zshrc:显式清理 ~/.zshrc 中的历史 Key 标记块(默认不修改该文件)
|
|
5
|
+
// 读官方 Agent 注册指引 → 申请一道文字题 → 本机保存会话与题目;退出 0 返回题目,
|
|
6
|
+
// 退出 2 表示平台要求人工图片验证(返回 verificationUrl),退出 1 为失败。
|
|
7
|
+
// node register_key.mjs answer --phone <p> --answer <非负整数>
|
|
8
|
+
// 提交答案;答对即申请发码。退出 0=已发短信;3=答错换题(JSON 带新 question,需再答);
|
|
9
|
+
// 2=转人工接手;4=发送结果未确认(不重发);1=失败。
|
|
10
|
+
// node register_key.mjs register --phone <p> --vcode <c> [--type 6] [--organ ..] [--name ..] [--channel ..] [--source ..] [--password ..] [--new-key]
|
|
6
11
|
// 可能返回 pending:true(账号已创建、密钥资源准备中)——非错误,稍后同参数重试一次。
|
|
7
|
-
// node register_key.mjs save-key
|
|
8
|
-
//
|
|
9
|
-
//
|
|
12
|
+
// node register_key.mjs save-key # 把 MCP create_api_key 拿到的密钥落盘(stdin 优先,或 --api-key)
|
|
13
|
+
// 说明:文字题由调用方(Agent)直接计算,本脚本只负责会话、题目状态与提交,不求解。
|
|
14
|
+
// 2026-10-29 注册链路改造:平台新增 Agent 注册文字题(先解题再发码,图片验证只作为人工兜底)。
|
|
15
|
+
// 旧 /sendMessage 发码入口对 Agent 一律要求图片验证,已由 challenge + answer 取代。
|
|
16
|
+
// 2026-10-09 改造仍保留于 register 一步:X-MaaS 客户端观测请求头、错误码族映射、验证链接安全校验。
|
|
10
17
|
|
|
11
18
|
import fs from "fs";
|
|
12
19
|
import os from "os";
|
|
@@ -21,7 +28,7 @@ const DEFAULT_OPEN_BASE = "https://open.dknowc.cn";
|
|
|
21
28
|
const DEFAULT_CHANNEL = "46A3BA1D-3E1A-4E8C-BD50-A6DCBEE1DB05";
|
|
22
29
|
const DEFAULT_TYPE = "11";
|
|
23
30
|
// body 的 source 是注册 API 的合同标记:服务端按 source="agent" 放行"老用户直接返 Key"
|
|
24
|
-
//
|
|
31
|
+
// 路径(生产环境可能出现:source=dknowc-ppt-assistant 时返回"用户已存在!"错误,
|
|
25
32
|
// source=agent 时正常返回 Key)。它与 X-Dknowc-Attribution 统计声明里的 skill 标识是两个
|
|
26
33
|
// 字段:统计声明头的键名为 agentSource(2026-10-09 平台方确认改名,避免与 body 的
|
|
27
34
|
// source 混淆),值仍读 attribution.json 的 dknowc-ppt-assistant,不受本值影响。
|
|
@@ -33,29 +40,62 @@ const MAAS_CLIENT_NAME = "dknowc-ppt-assistant";
|
|
|
33
40
|
const INSTANCE_FILE_NAME = "maas_client_instance";
|
|
34
41
|
const API_KEY_ENV = "DKNOWC_API_KEY";
|
|
35
42
|
const MAAS_PLATFORM_URL = "https://platform.dknowc.cn/auth/#/login";
|
|
36
|
-
|
|
37
|
-
//
|
|
43
|
+
const FALLBACK_REGISTER_URL = MAAS_PLATFORM_URL;
|
|
44
|
+
// 2026-10-29 Agent 注册:官方机器可读指引与注册页。指引里 challenge/submit 是相对地址,
|
|
45
|
+
// 必须相对「指引的最终响应 URL」解析,保留认证代理前缀(不得相对 SPA 的 #/agent-register 解析)。
|
|
46
|
+
const DEFAULT_GUIDE = "https://platform.dknowc.cn/maas/identity-center/maas/login/open-api/maas/identity/v1/agent-registration-guide";
|
|
47
|
+
|
|
48
|
+
// 端点域名白名单:注册/发码/指引三类地址默认只允许官方域名且必须 HTTPS,
|
|
49
|
+
// 防止 --base / --guide / --open-base 把手机号或密钥发往任意主机。
|
|
50
|
+
// 本地测试(mock 服务)可用 DKNOWC_ALLOW_ANY_ENDPOINT=1 显式放开。
|
|
51
|
+
const ALLOWED_ENDPOINT_HOSTS = new Set(["platform.dknowc.cn", "open.dknowc.cn"]);
|
|
52
|
+
function assertEndpointAllowed(rawUrl, flagName) {
|
|
53
|
+
const raw = String(rawUrl || "");
|
|
54
|
+
if (process.env.DKNOWC_ALLOW_ANY_ENDPOINT === "1") return raw;
|
|
55
|
+
let u;
|
|
56
|
+
const fail = (reason) => {
|
|
57
|
+
emit(errorPayload(
|
|
58
|
+
"ENDPOINT_REJECTED",
|
|
59
|
+
null,
|
|
60
|
+
`${flagName} ${reason}。仅允许 HTTPS 的 platform.dknowc.cn / open.dknowc.cn;如确需自定义端点,请设置 DKNOWC_ALLOW_ANY_ENDPOINT=1。`,
|
|
61
|
+
), 1);
|
|
62
|
+
};
|
|
63
|
+
try { u = new URL(raw); } catch { fail(`不是合法 URL(${raw})`); }
|
|
64
|
+
if (u.protocol !== "https:" || !ALLOWED_ENDPOINT_HOSTS.has(u.hostname)) {
|
|
65
|
+
fail(`指向非官方主机(${u.protocol}//${u.hostname})`);
|
|
66
|
+
}
|
|
67
|
+
return raw;
|
|
68
|
+
}
|
|
69
|
+
// 挑战状态(Cookie / token / attempt / 题目)落盘,供 challenge 与 answer 两次调用之间传递。
|
|
70
|
+
// attempt 必须始终使用服务端返回值,刷新题目不增加机会,故本机只做保存、不改写。
|
|
71
|
+
const CHALLENGE_STATE_FILE = "maas_agent_challenge.json";
|
|
72
|
+
// Key 持久化目标:本机专用配置文件(XDG 规范路径),不再写 ~/.zshrc。
|
|
73
|
+
// 3.7.5 迁移:写入新文件成功后,清理 ~/.zshrc 中的历史 Key 块。
|
|
38
74
|
const KEY_FILE_NAME = "api_key";
|
|
39
75
|
const ZSHRC_START = "# >>> dknowc ppt assistant api key >>>";
|
|
40
76
|
const ZSHRC_END = "# <<< dknowc ppt assistant api key <<<";
|
|
41
|
-
// 历史 ~/.zshrc
|
|
77
|
+
// 历史 ~/.zshrc 标记块(历史版本与历史产品名)。注意:脚本默认**不修改** ~/.zshrc;
|
|
78
|
+
// 仅当用户显式传 --clean-zshrc 时才清理这些块(历史块只作读取兜底,见 api_key.py)。
|
|
42
79
|
const LEGACY_BLOCKS = [
|
|
43
80
|
[ZSHRC_START, ZSHRC_END],
|
|
81
|
+
["# >>> dknowc official doc writer api key >>>", "# <<< dknowc official doc writer api key <<<"],
|
|
44
82
|
["# >>> dknowc api key >>>", "# <<< dknowc api key <<<"],
|
|
45
83
|
];
|
|
46
84
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function apiKeyFilePath() {
|
|
85
|
+
// 配置目录:默认 ~/.config/dknowc(Windows 为 %APPDATA%\dknowc),可用 DKNOWC_CONFIG_DIR 覆盖。
|
|
86
|
+
function configDir() {
|
|
87
|
+
const override = process.env.DKNOWC_CONFIG_DIR;
|
|
88
|
+
if (override && override.trim()) return override.trim();
|
|
52
89
|
const home = os.homedir();
|
|
53
90
|
if (process.platform === "win32") {
|
|
54
|
-
|
|
55
|
-
return path.join(appdata, "dknowc", KEY_FILE_NAME);
|
|
91
|
+
return path.join(process.env.APPDATA || home, "dknowc");
|
|
56
92
|
}
|
|
57
93
|
const xdg = process.env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
58
|
-
return path.join(xdg, "dknowc"
|
|
94
|
+
return path.join(xdg, "dknowc");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function apiKeyFilePath() {
|
|
98
|
+
return path.join(configDir(), KEY_FILE_NAME);
|
|
59
99
|
}
|
|
60
100
|
|
|
61
101
|
function removeLegacyZshrcBlocks() {
|
|
@@ -73,17 +113,9 @@ function removeLegacyZshrcBlocks() {
|
|
|
73
113
|
} catch (e) { /* 清理失败不阻断写入流程 */ }
|
|
74
114
|
}
|
|
75
115
|
|
|
76
|
-
function
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
}
|
|
116
|
+
function maskPhone(phone) {
|
|
117
|
+
const p = String(phone || "");
|
|
118
|
+
return p.length >= 7 ? `${p.slice(0, 3)}****${p.slice(-4)}` : p;
|
|
87
119
|
}
|
|
88
120
|
|
|
89
121
|
// —— 2026-10-09 注册链路改造新增(user-auth maas-1.1.6 契约)——
|
|
@@ -91,13 +123,7 @@ function writeApiKeyToConfigFile(apiKey) {
|
|
|
91
123
|
// Client-Instance-Id 持久化:与 api_key 同目录(~/.config/dknowc/maas_client_instance)。
|
|
92
124
|
// 值来自服务端响应头签发,仅做回传;读不到/落盘失败都不阻断请求。
|
|
93
125
|
function instanceIdFilePath() {
|
|
94
|
-
|
|
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);
|
|
126
|
+
return path.join(configDir(), INSTANCE_FILE_NAME);
|
|
101
127
|
}
|
|
102
128
|
function readInstanceId() {
|
|
103
129
|
try { return fs.readFileSync(instanceIdFilePath(), "utf-8").trim() || null; } catch { return null; }
|
|
@@ -105,50 +131,68 @@ function readInstanceId() {
|
|
|
105
131
|
function writeInstanceId(id) {
|
|
106
132
|
try {
|
|
107
133
|
fs.mkdirSync(path.dirname(instanceIdFilePath()), { recursive: true, mode: 0o700 });
|
|
108
|
-
fs.writeFileSync(instanceIdFilePath(), `${String(id).trim()}\n`, { encoding: "
|
|
134
|
+
fs.writeFileSync(instanceIdFilePath(), `${String(id).trim()}\n`, { encoding: "utf-8", mode: 0o600 });
|
|
109
135
|
} catch { /* 落盘失败不阻断请求 */ }
|
|
110
136
|
}
|
|
111
137
|
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
function
|
|
118
|
-
|
|
119
|
-
const m = String(base || "").match(/^(https?:\/\/[^\/]+)\/auth\/home\/userAuto\/?$/);
|
|
120
|
-
return m ? m[1] : null;
|
|
138
|
+
// —— 2026-10-29 Agent 注册链路:文字题 + 人工兜底 ——
|
|
139
|
+
|
|
140
|
+
// 验证页链接安全校验:必须与指引同源(生产 https://platform.dknowc.cn;测试环境随 --guide),
|
|
141
|
+
// 路径以 sms-verification-page 结尾(兼容认证代理前缀),无用户名/密码/query,fragment 仅为
|
|
142
|
+
// #token= 后跟 43 位 Base64URL 字符;拒绝编码/路径遍历/其他版本与非规范 URL。
|
|
143
|
+
function originOf(url) {
|
|
144
|
+
try { return new URL(String(url)).origin; } catch { return null; }
|
|
121
145
|
}
|
|
122
|
-
function validateVerificationUrl(rawUrl,
|
|
123
|
-
const origin =
|
|
146
|
+
function validateVerificationUrl(rawUrl, guideUrl) {
|
|
147
|
+
const origin = originOf(guideUrl);
|
|
124
148
|
if (!origin) return null;
|
|
125
149
|
try {
|
|
126
150
|
const u = new URL(String(rawUrl || ""));
|
|
127
151
|
if (u.username || u.password || u.search) return null;
|
|
128
152
|
if (u.origin !== origin) return null;
|
|
129
|
-
if (u.pathname
|
|
153
|
+
if (!u.pathname.endsWith("/sms-verification-page")) return null;
|
|
130
154
|
if (!/^#token=[A-Za-z0-9_-]{43}$/.test(u.hash)) return null;
|
|
131
155
|
if (String(rawUrl) !== u.toString()) return null; // 拒绝编码/非规范写法
|
|
132
156
|
return u.toString();
|
|
133
157
|
} catch { return null; }
|
|
134
158
|
}
|
|
135
159
|
|
|
160
|
+
function stateFilePath() {
|
|
161
|
+
return path.join(configDir(), CHALLENGE_STATE_FILE);
|
|
162
|
+
}
|
|
163
|
+
function readChallengeState() {
|
|
164
|
+
try {
|
|
165
|
+
const raw = fs.readFileSync(stateFilePath(), "utf-8");
|
|
166
|
+
const st = JSON.parse(raw);
|
|
167
|
+
return st && typeof st === "object" ? st : null;
|
|
168
|
+
} catch { return null; }
|
|
169
|
+
}
|
|
170
|
+
function writeChallengeState(st) {
|
|
171
|
+
try {
|
|
172
|
+
fs.mkdirSync(configDir(), { recursive: true, mode: 0o700 });
|
|
173
|
+
fs.writeFileSync(stateFilePath(), JSON.stringify(st, null, 2) + "\n", { encoding: "utf-8", mode: 0o600 });
|
|
174
|
+
return true;
|
|
175
|
+
} catch { return false; }
|
|
176
|
+
}
|
|
177
|
+
function clearChallengeState() {
|
|
178
|
+
try { fs.unlinkSync(stateFilePath()); } catch { /* 不存在时忽略 */ }
|
|
179
|
+
}
|
|
180
|
+
function challengeExpired(st) {
|
|
181
|
+
const exp = Number(st && st.expiresAt);
|
|
182
|
+
if (!Number.isFinite(exp) || exp <= 0) return false;
|
|
183
|
+
return Date.now() >= exp - 5000; // 预留 5 秒余量,避免临界提交
|
|
184
|
+
}
|
|
185
|
+
|
|
136
186
|
function parseArgs(argv) {
|
|
137
187
|
const out = { _: [] };
|
|
138
188
|
for (let i = 0; i < argv.length; i++) {
|
|
139
|
-
const
|
|
140
|
-
if (
|
|
141
|
-
const key =
|
|
189
|
+
const a = argv[i];
|
|
190
|
+
if (a.startsWith("--")) {
|
|
191
|
+
const key = a.slice(2);
|
|
142
192
|
const next = argv[i + 1];
|
|
143
|
-
if (next === undefined || next.startsWith("--")) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
out[key] = next;
|
|
147
|
-
i++;
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
out._.push(arg);
|
|
151
|
-
}
|
|
193
|
+
if (next === undefined || next.startsWith("--")) { out[key] = true; }
|
|
194
|
+
else { out[key] = next; i++; }
|
|
195
|
+
} else { out._.push(a); }
|
|
152
196
|
}
|
|
153
197
|
return out;
|
|
154
198
|
}
|
|
@@ -182,349 +226,693 @@ function withAttribution(headers) {
|
|
|
182
226
|
return attr ? { ...headers, "X-Dknowc-Attribution": attr } : headers;
|
|
183
227
|
}
|
|
184
228
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
229
|
+
// —— 极简 Cookie 会话 ——
|
|
230
|
+
// Agent 注册要求「同一会话」完成领题与作答:指引响应下发 sl-session,后续请求必须带回。
|
|
231
|
+
// Node fetch 无 Cookie jar,这里只保留 name=value,不处理 Domain/Path 等属性(全部请求同源同路径段)。
|
|
232
|
+
function readSetCookies(res) {
|
|
233
|
+
let list = [];
|
|
188
234
|
try {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
});
|
|
195
|
-
const text = await response.text();
|
|
196
|
-
try {
|
|
197
|
-
return JSON.parse(text);
|
|
198
|
-
} catch {
|
|
199
|
-
return { status: false, msg: `非 JSON 响应:${text.slice(0, 200)}` };
|
|
235
|
+
if (typeof res.headers.getSetCookie === "function") {
|
|
236
|
+
list = res.headers.getSetCookie();
|
|
237
|
+
} else {
|
|
238
|
+
const raw = res.headers.get("set-cookie");
|
|
239
|
+
if (raw) list = raw.split(/,(?=[^;,]+=)/);
|
|
200
240
|
}
|
|
201
|
-
} catch
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
241
|
+
} catch { list = []; }
|
|
242
|
+
const out = [];
|
|
243
|
+
for (const line of list) {
|
|
244
|
+
const head = String(line).split(";")[0];
|
|
245
|
+
const idx = head.indexOf("=");
|
|
246
|
+
if (idx <= 0) continue;
|
|
247
|
+
const name = head.slice(0, idx).trim();
|
|
248
|
+
const value = head.slice(idx + 1).trim();
|
|
249
|
+
if (!name) continue;
|
|
250
|
+
out.push([name, value]);
|
|
206
251
|
}
|
|
252
|
+
return out;
|
|
207
253
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
"
|
|
212
|
-
|
|
213
|
-
"23456789",
|
|
214
|
-
"!@#$%^&*",
|
|
215
|
-
];
|
|
216
|
-
const pick = (value) => value[Math.floor(Math.random() * value.length)];
|
|
217
|
-
const chars = pools.map(pick);
|
|
218
|
-
const all = pools.join("");
|
|
219
|
-
for (let i = 0; i < 8; i++) chars.push(pick(all));
|
|
220
|
-
for (let i = chars.length - 1; i > 0; i--) {
|
|
221
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
222
|
-
[chars[i], chars[j]] = [chars[j], chars[i]];
|
|
254
|
+
function mergeCookies(jar, pairs) {
|
|
255
|
+
const next = { ...(jar || {}) };
|
|
256
|
+
for (const [k, v] of pairs || []) {
|
|
257
|
+
if (v === "" || v === "deleted") delete next[k];
|
|
258
|
+
else next[k] = v;
|
|
223
259
|
}
|
|
224
|
-
return
|
|
260
|
+
return next;
|
|
261
|
+
}
|
|
262
|
+
function cookieHeader(jar) {
|
|
263
|
+
const entries = Object.entries(jar || {});
|
|
264
|
+
return entries.length ? entries.map(([k, v]) => `${k}=${v}`).join("; ") : null;
|
|
225
265
|
}
|
|
226
266
|
|
|
227
|
-
//
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
// (发送结果未知时不得立即补发,由调用方按专用话术处理)。
|
|
233
|
-
async function postRegister(url, payload) {
|
|
234
|
-
const controller = new AbortController();
|
|
235
|
-
const timer = setTimeout(() => controller.abort(), 30000);
|
|
267
|
+
// 通用 JSON 请求:返回 { json, finalUrl, setCookies, requestUnknown, httpStatus }。
|
|
268
|
+
// requestUnknown=true 表示超时/断网等"结果未确认"情形 —— 发码请求结果未知时不得立即重发。
|
|
269
|
+
async function httpJson(method, url, { body, cookies, headers, timeoutMs = 30000 } = {}) {
|
|
270
|
+
const ctl = new AbortController();
|
|
271
|
+
const timer = setTimeout(() => ctl.abort(), timeoutMs);
|
|
236
272
|
try {
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
signal: controller.signal,
|
|
273
|
+
const h = { ...(headers || {}) };
|
|
274
|
+
if (body !== undefined) h["Content-Type"] = "application/json";
|
|
275
|
+
const ck = cookieHeader(cookies);
|
|
276
|
+
if (ck) h["Cookie"] = ck;
|
|
277
|
+
const r = await fetch(url, {
|
|
278
|
+
method,
|
|
279
|
+
headers: h,
|
|
280
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
281
|
+
signal: ctl.signal,
|
|
282
|
+
redirect: "follow",
|
|
248
283
|
});
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
} catch (error) {
|
|
257
|
-
const msg = error && error.message ? error.message : String(error);
|
|
284
|
+
const setCookies = readSetCookies(r);
|
|
285
|
+
const text = await r.text();
|
|
286
|
+
let json;
|
|
287
|
+
try { json = JSON.parse(text); }
|
|
288
|
+
catch { json = { status: false, msg: "非JSON响应(前200字符): " + text.slice(0, 200) }; }
|
|
289
|
+
return { json, finalUrl: r.url || url, setCookies, requestUnknown: false, httpStatus: r.status };
|
|
290
|
+
} catch (e) {
|
|
258
291
|
return {
|
|
259
|
-
|
|
292
|
+
json: {
|
|
293
|
+
status: false,
|
|
294
|
+
// 失败原因写进 msg 便于定位(DNS/代理/证书/TLS),不包含任何凭据
|
|
295
|
+
msg: "请求异常: " + (e && e.message ? e.message : String(e))
|
|
296
|
+
+ (e && e.cause && (e.cause.code || e.cause.message) ? " (" + (e.cause.code || e.cause.message) + ")" : ""),
|
|
297
|
+
},
|
|
298
|
+
setCookies: [],
|
|
260
299
|
requestUnknown: true,
|
|
300
|
+
finalUrl: url,
|
|
301
|
+
httpStatus: 0,
|
|
261
302
|
};
|
|
262
|
-
} finally {
|
|
263
|
-
|
|
303
|
+
} finally { clearTimeout(timer); }
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 注册链路(register)专用 POST:
|
|
307
|
+
// - 携带 X-MaaS 客户端观测请求头(user-auth maas-1.1.6):Entry-Surface 固定 DIRECT_API、
|
|
308
|
+
// Client-Name 为本 Skill 真实包名、Client-Version 运行时读 SKILL.md、Client-Instance-Id
|
|
309
|
+
// 首次不带(服务端响应头签发后落盘,后续请求回传,不自行生成);
|
|
310
|
+
// - 返回 { body, requestUnknown }:requestUnknown=true 表示超时/断网等"结果未确认"情形
|
|
311
|
+
// (发送结果未知时不得立即补发,由调用方按专用话术处理)。
|
|
312
|
+
async function post(url, payload) {
|
|
313
|
+
const { json, setCookies, requestUnknown } = await httpJson("POST", url, {
|
|
314
|
+
body: payload,
|
|
315
|
+
headers: withAttribution({
|
|
316
|
+
"X-MaaS-Entry-Surface": "DIRECT_API",
|
|
317
|
+
"X-MaaS-Client-Name": MAAS_CLIENT_NAME,
|
|
318
|
+
"X-MaaS-Client-Version": readSkillVersion(),
|
|
319
|
+
...(readInstanceId() ? { "X-MaaS-Client-Instance-Id": readInstanceId() } : {}),
|
|
320
|
+
}),
|
|
321
|
+
});
|
|
322
|
+
const issued = setCookies.find(([k]) => k.toLowerCase() === "x-maas-client-instance-id");
|
|
323
|
+
if (issued && issued[1]) writeInstanceId(issued[1]);
|
|
324
|
+
return { body: json, requestUnknown };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async function postWithBearer(url, apiKey, payload) {
|
|
328
|
+
const { json } = await httpJson("POST", url, {
|
|
329
|
+
body: payload,
|
|
330
|
+
headers: withAttribution({
|
|
331
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
332
|
+
}),
|
|
333
|
+
});
|
|
334
|
+
return json;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function genPassword() {
|
|
338
|
+
// 8-32 位,含大写/小写/数字/特殊至少 3 类
|
|
339
|
+
const pools = ["ABCDEFGHJKLMNPQRSTUVWXYZ", "abcdefghijkmnpqrstuvwxyz", "23456789", "!@#$%^&*"];
|
|
340
|
+
const pick = (s) => s[Math.floor(Math.random() * s.length)];
|
|
341
|
+
let chars = pools.map(pick); // 每类至少一个
|
|
342
|
+
const all = pools.join("");
|
|
343
|
+
for (let i = 0; i < 8; i++) chars.push(pick(all)); // 补到 12 位
|
|
344
|
+
for (let i = chars.length - 1; i > 0; i--) { // 洗牌
|
|
345
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
346
|
+
[chars[i], chars[j]] = [chars[j], chars[i]];
|
|
264
347
|
}
|
|
348
|
+
return chars.join("");
|
|
265
349
|
}
|
|
266
350
|
|
|
267
351
|
async function createNewApiKey(openBase, existingApiKey, name, remark) {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
352
|
+
const result = await postWithBearer(
|
|
353
|
+
openBase.replace(/\/$/, "") + "/open-api/maas/api-key/create",
|
|
354
|
+
existingApiKey,
|
|
271
355
|
{ name, remark },
|
|
272
|
-
{ Authorization: `Bearer ${existingApiKey}` },
|
|
273
356
|
);
|
|
274
357
|
const apiKey = result && result.data ? result.data.appKey : "";
|
|
275
358
|
return { result, apiKey };
|
|
276
359
|
}
|
|
277
360
|
|
|
278
|
-
function
|
|
279
|
-
|
|
280
|
-
|
|
361
|
+
function writeApiKeyToConfigFile(apiKey) {
|
|
362
|
+
// 写本机专用配置文件(纯文本一行 Key,600 权限);成功后清理历史 ~/.zshrc 块
|
|
363
|
+
const filePath = apiKeyFilePath();
|
|
364
|
+
try {
|
|
365
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
366
|
+
fs.writeFileSync(filePath, `${String(apiKey).trim()}\n`, { encoding: "utf8", mode: 0o600 });
|
|
367
|
+
// 不自动修改 ~/.zshrc:历史块只作读取兜底;清理需用户显式 --clean-zshrc
|
|
368
|
+
return { written: true, path: filePath, error: null };
|
|
369
|
+
} catch (e) {
|
|
370
|
+
return { written: false, path: filePath, error: e && e.message ? e.message : String(e) };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function escapeRegExp(value) {
|
|
375
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
281
376
|
}
|
|
282
377
|
|
|
283
|
-
|
|
284
|
-
|
|
378
|
+
// —— Agent 注册:响应归类 ——
|
|
379
|
+
// 平台返回 status:true 只代表"本步成功":领题成功≠已发码,发码成功≠注册完成。
|
|
380
|
+
function emit(obj, code) {
|
|
381
|
+
console.log(JSON.stringify(obj));
|
|
382
|
+
process.exit(code);
|
|
285
383
|
}
|
|
286
384
|
|
|
287
|
-
function
|
|
288
|
-
|
|
289
|
-
if (
|
|
290
|
-
|
|
385
|
+
function handoffPayload(rawUrl, guideUrl, phone) {
|
|
386
|
+
const vUrl = validateVerificationUrl(rawUrl, guideUrl);
|
|
387
|
+
if (!vUrl) {
|
|
388
|
+
return {
|
|
389
|
+
payload: {
|
|
390
|
+
status: false,
|
|
391
|
+
code: "CAPTCHA_LINK_UNAVAILABLE",
|
|
392
|
+
user_message: "这次开通需要先完成一次页面验证,但验证链接校验没有通过(可能已过期或不可用)。跟我说一声,我重新给你申请一个新的验证链接。",
|
|
393
|
+
instruction: "不得展示未通过校验的链接;用户同意后重新调用 challenge 申请。",
|
|
394
|
+
},
|
|
395
|
+
exitCode: 1,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
payload: {
|
|
400
|
+
status: false,
|
|
401
|
+
code: "HUMAN_REQUIRED",
|
|
402
|
+
fallbackRequired: true,
|
|
403
|
+
verificationUrl: vUrl,
|
|
404
|
+
verificationUrlValidated: true,
|
|
405
|
+
user_message: `短信还没发送。请打开这个验证链接,核对手机号(${maskPhone(phone)})后点击“验证并发送短信”,按提示完成验证:\n${vUrl}\n收到短信后把 6 位验证码回复给我,我继续开通,不需要重新填写注册表。`,
|
|
406
|
+
instruction: "转人工接手:把 user_message(含链接)原样转述给手机号持有人后暂停;链接一次性、约 10 分钟有效,仅私下展示给本人,不贴群、不写日志。持有人完成验证后不得再次调用 challenge 或 answer 发码。",
|
|
407
|
+
},
|
|
408
|
+
exitCode: 2,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function questionPayload(st, extra = {}) {
|
|
413
|
+
const q = st.question || {};
|
|
414
|
+
return {
|
|
415
|
+
status: true,
|
|
416
|
+
flow: "agent-text",
|
|
417
|
+
question: { id: q.id || null, prompt: q.prompt || "", answerFormat: q.answerFormat || "非负整数,不带单位" },
|
|
418
|
+
questionCount: st.questionCount || 1,
|
|
419
|
+
requiredCorrect: st.requiredCorrect || 1,
|
|
420
|
+
attempt: Number(st.attempt) || 1,
|
|
421
|
+
maxAttempts: Number(st.maxAttempts) || 3,
|
|
422
|
+
expiresAt: st.expiresAt || null,
|
|
423
|
+
...extra,
|
|
424
|
+
instruction: "直接按 question.prompt 计算答案,然后调用 answer --phone <手机号> --answer <非负整数>;不要把题目、token 或答题过程发给用户。",
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function errorPayload(code, userMessage, instruction) {
|
|
429
|
+
return { status: false, code, user_message: userMessage, instruction };
|
|
291
430
|
}
|
|
292
431
|
|
|
293
432
|
async function main() {
|
|
294
|
-
const
|
|
295
|
-
const
|
|
296
|
-
const
|
|
433
|
+
const a = parseArgs(process.argv.slice(2));
|
|
434
|
+
const base = assertEndpointAllowed(a.base || DEFAULT_BASE, "--base");
|
|
435
|
+
const cmd = a._[0];
|
|
297
436
|
|
|
298
|
-
if (cmd === "
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
437
|
+
if (cmd === "save-key") {
|
|
438
|
+
// 3.7.7:MCP 取 Key 路径的落盘子命令。宿主提供 dknowc-mcp 的 create_api_key 且调用
|
|
439
|
+
// 成功(返回顶层 JSON {"apiKey": "..."})后,用本命令落盘,用户无需手机号验证码。
|
|
440
|
+
// 密钥来源优先 stdin(printf '%s' "<密钥>" | node register_key.mjs save-key,避免出现在
|
|
441
|
+
// 命令行参数里),其次 --api-key;仅接受 sk- 开头。落盘复用 writeApiKeyToConfigFile
|
|
442
|
+
// (600 权限 + 清理历史 ~/.zshrc 块),与手机号注册路径同一份持久化逻辑。
|
|
443
|
+
let apiKey = "";
|
|
444
|
+
try {
|
|
445
|
+
if (!process.stdin.isTTY) {
|
|
446
|
+
const chunks = [];
|
|
447
|
+
for await (const c of process.stdin) chunks.push(c);
|
|
448
|
+
apiKey = Buffer.concat(chunks).toString("utf-8");
|
|
449
|
+
}
|
|
450
|
+
} catch { /* stdin 读取失败时回退 --api-key */ }
|
|
451
|
+
if (!apiKey.trim() && a["api-key"] && a["api-key"] !== true) apiKey = String(a["api-key"]);
|
|
452
|
+
apiKey = apiKey.trim();
|
|
453
|
+
if (!apiKey.startsWith("sk-") || apiKey.length < 20) {
|
|
305
454
|
console.log(JSON.stringify({
|
|
306
455
|
status: false,
|
|
307
|
-
msg: "
|
|
308
|
-
user_message: "这个手机号格式好像不对,麻烦核对一下再发我。",
|
|
456
|
+
msg: "密钥格式不符(应为 sk- 开头的 MaaS API Key),请确认调用的是 create_api_key 的返回值",
|
|
309
457
|
}));
|
|
310
458
|
process.exit(1);
|
|
311
459
|
}
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
460
|
+
const w = writeApiKeyToConfigFile(apiKey);
|
|
461
|
+
const zshrcCleaned = (w.written && a["clean-zshrc"]) ? (removeLegacyZshrcBlocks(), true) : false;
|
|
462
|
+
console.log(JSON.stringify({
|
|
463
|
+
zshrcCleaned,
|
|
464
|
+
status: w.written,
|
|
465
|
+
envWriteSucceeded: w.written,
|
|
466
|
+
envWriteTarget: w.path,
|
|
467
|
+
error: w.error || null,
|
|
468
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥
|
|
469
|
+
user_message: w.written
|
|
470
|
+
? "已通过你的深知可信工作台授权直接开通搜索功能,无需手机号验证,马上开始检索。"
|
|
471
|
+
: "密钥保存到本机时出现问题,需要改用手机号验证码方式开通。",
|
|
472
|
+
}));
|
|
473
|
+
process.exit(w.written ? 0 : 1);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// —— 第一步:领题 ——
|
|
477
|
+
if (cmd === "challenge") {
|
|
478
|
+
if (!a.phone || a.phone === true) {
|
|
479
|
+
console.error("缺少 --phone");
|
|
480
|
+
emit(errorPayload("ARGUMENT_MISSING", null, "缺少 --phone,请传入用户手机号后重试。"), 2);
|
|
481
|
+
}
|
|
482
|
+
const phone = String(a.phone).trim();
|
|
483
|
+
if (!/^1[3-9][0-9]{9}$/.test(phone)) {
|
|
484
|
+
emit(errorPayload("PHONE_INVALID", "这个手机号格式好像不对,麻烦核对一下再发我。", "不重发、不换号,等用户提供正确手机号。"), 1);
|
|
485
|
+
}
|
|
486
|
+
const guideUrl = assertEndpointAllowed(a.guide && a.guide !== true ? String(a.guide) : DEFAULT_GUIDE, "--guide");
|
|
487
|
+
const channel = a.channel && a.channel !== true ? String(a.channel) : DEFAULT_CHANNEL;
|
|
488
|
+
|
|
489
|
+
// 同号未过期的题直接复用:刷新题目不增加机会、不延长到期时间,也不该重复请求服务端
|
|
490
|
+
const cached = readChallengeState();
|
|
491
|
+
if (cached && cached.phone === phone && cached.question && !challengeExpired(cached)) {
|
|
492
|
+
emit(questionPayload(cached, { reused: true }), 0);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const g = await httpJson("GET", guideUrl, { headers: withAttribution({}) });
|
|
496
|
+
const guideOk = !g.requestUnknown && g.json && g.json.challenge && g.json.challenge.url;
|
|
497
|
+
if (!guideOk) {
|
|
498
|
+
// 文字题接口未上线或平台临时回退时,回落原有短信直发分支(3.9.3 之前的行为)。
|
|
499
|
+
// 两条路径对用户等价:退出 0=已发码、退出 2=人工验证链接、退出 1=停点;输出带 flow 标明走了哪条。
|
|
500
|
+
const { body: r, requestUnknown } = await post(base + "/sendMessage", { phone, type: "register", channel });
|
|
501
|
+
const lc = String(r.code || (r.data && r.data.code) || "");
|
|
502
|
+
if (lc === "CAPTCHA_REQUIRED") {
|
|
503
|
+
const h = handoffPayload(r.verificationUrl, base, phone);
|
|
504
|
+
emit({ ...h.payload, flow: "legacy" }, h.exitCode);
|
|
505
|
+
}
|
|
506
|
+
if (lc === "SMS_RATE_LIMIT_PHONE" || lc === "SMS_RATE_LIMIT") {
|
|
507
|
+
const secs = Number(r.retryAfterSeconds || (r.data && r.data.retryAfterSeconds) || 0);
|
|
508
|
+
const mins = secs > 0 ? Math.max(1, Math.floor(secs / 60)) : 0;
|
|
509
|
+
emit({
|
|
510
|
+
...errorPayload(
|
|
511
|
+
"SMS_RATE_LIMIT",
|
|
512
|
+
mins > 0
|
|
513
|
+
? `验证码发送太频繁了,短信通道要求约 ${mins} 分钟后再试。你先忙别的,想重发时跟我说一声。`
|
|
514
|
+
: "验证码发送太频繁了,稍等几分钟再试;想重发时跟我说一声。",
|
|
515
|
+
"限频停点:等用户明确要求且冷却过后再发起,不自动重试。",
|
|
516
|
+
),
|
|
517
|
+
flow: "legacy",
|
|
518
|
+
}, 1);
|
|
519
|
+
}
|
|
520
|
+
if (lc === "CAPTCHA_LINK_UNAVAILABLE" || lc === "CAPTCHA_REPLAY") {
|
|
521
|
+
emit({
|
|
522
|
+
...errorPayload(
|
|
523
|
+
"CAPTCHA_LINK_UNAVAILABLE",
|
|
524
|
+
"刚才的验证链接已过期或已使用。跟我说一声,我重新给你申请一个新的验证链接。",
|
|
525
|
+
"不重复提交旧链接;用户确认后重新发起。",
|
|
526
|
+
),
|
|
527
|
+
flow: "legacy",
|
|
528
|
+
}, 1);
|
|
529
|
+
}
|
|
530
|
+
if (lc === "CAPTCHA_REJECTED" || lc === "CAPTCHA_BUSY" || lc === "CAPTCHA_STATE_UNAVAILABLE") {
|
|
531
|
+
emit({
|
|
532
|
+
...errorPayload(
|
|
533
|
+
"CAPTCHA_REJECTED",
|
|
534
|
+
"这次验证没有通过(或验证服务暂时不可用)。可以稍后重新打开链接再试;还是不行跟我说,我重新申请一个。",
|
|
535
|
+
"据实说明,不伪造验证结果。",
|
|
536
|
+
),
|
|
537
|
+
flow: "legacy",
|
|
538
|
+
}, 1);
|
|
539
|
+
}
|
|
540
|
+
if (lc.startsWith("SMS_PROVIDER_")) {
|
|
541
|
+
emit({
|
|
542
|
+
...errorPayload(
|
|
543
|
+
"SMS_PROVIDER_FAILED",
|
|
544
|
+
`短信服务暂时出了点问题,发不出验证码。可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
545
|
+
"供应商失败停点:不自动重试。",
|
|
546
|
+
),
|
|
547
|
+
flow: "legacy",
|
|
548
|
+
}, 1);
|
|
344
549
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
550
|
+
if (requestUnknown || String(r.msg || "").startsWith("非JSON响应")) {
|
|
551
|
+
emit({
|
|
552
|
+
status: false,
|
|
553
|
+
code: "RESULT_UNKNOWN",
|
|
554
|
+
resultUnknown: true,
|
|
555
|
+
flow: "legacy",
|
|
556
|
+
user_message: "刚才发送请求的结果没有确认(网络波动)。先看一下手机有没有收到验证码短信:收到了直接把 6 位验证码发我;没收到再跟我说,我重新发。",
|
|
557
|
+
instruction: "结果未确认停点:先等用户确认是否收到短信,未确认前不重新发送。",
|
|
558
|
+
detail: r.msg || null,
|
|
559
|
+
}, 1);
|
|
560
|
+
}
|
|
561
|
+
if (r.status) {
|
|
562
|
+
emit({
|
|
563
|
+
status: true,
|
|
564
|
+
code: "SMS_SENT",
|
|
565
|
+
flow: "legacy",
|
|
566
|
+
user_message: `验证码已发送到 ${maskPhone(phone)},请把最新一条短信里的 6 位验证码发我。`,
|
|
567
|
+
instruction: "已发码:转述 user_message 后暂停,等用户回复 6 位验证码,再调用 register。不得重复发码,也不需要调用 answer。",
|
|
568
|
+
}, 0);
|
|
569
|
+
}
|
|
570
|
+
if (String(r.msg || "").includes("手机号")) {
|
|
571
|
+
emit({ ...errorPayload("PHONE_INVALID", "这个手机号格式好像不对,麻烦核对一下再发我。", "不重复提交同一号码,等用户核对。"), flow: "legacy" }, 1);
|
|
572
|
+
}
|
|
573
|
+
emit({
|
|
574
|
+
...errorPayload(
|
|
575
|
+
"SEND_FAILED",
|
|
576
|
+
`验证码发送没成功(可能是网络或短信通道问题),可以再试一次;如果连续失败,也可以用网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
577
|
+
"失败停点:最多重试一次,超限引导网页开通。",
|
|
578
|
+
),
|
|
579
|
+
flow: "legacy",
|
|
580
|
+
detail: r.msg || (g.json && g.json.msg) || null,
|
|
581
|
+
}, 1);
|
|
582
|
+
}
|
|
583
|
+
const guideFinal = g.finalUrl || guideUrl;
|
|
584
|
+
let challengeUrl;
|
|
585
|
+
let submitUrl;
|
|
586
|
+
try {
|
|
587
|
+
challengeUrl = new URL(String(g.json.challenge.url), guideFinal).toString();
|
|
588
|
+
submitUrl = new URL(String(g.json.submit && g.json.submit.url ? g.json.submit.url : "./agent-registration-send"), guideFinal).toString();
|
|
589
|
+
} catch {
|
|
590
|
+
emit(errorPayload(
|
|
591
|
+
"GUIDE_INVALID",
|
|
592
|
+
`开通服务暂时不可用,可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
593
|
+
"指引地址解析失败,不要自行拼接其他地址。",
|
|
594
|
+
), 1);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
let jar = mergeCookies({}, g.setCookies);
|
|
598
|
+
const headers = withAttribution({
|
|
599
|
+
"X-MaaS-Entry-Surface": "DIRECT_API",
|
|
600
|
+
"X-MaaS-Client-Name": MAAS_CLIENT_NAME,
|
|
601
|
+
"X-MaaS-Client-Version": readSkillVersion(),
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
let r = await httpJson("POST", challengeUrl, { body: { phone, channel }, cookies: jar, headers });
|
|
605
|
+
jar = mergeCookies(jar, r.setCookies);
|
|
606
|
+
// 个别环境要求显式用途;服务端未接受时补一次 purpose,仍失败按错误码处理(不循环重试)
|
|
607
|
+
if (r.json && r.json.status === false && /用途/.test(String(r.json.msg || ""))) {
|
|
608
|
+
r = await httpJson("POST", challengeUrl, { body: { phone, channel, purpose: "register" }, cookies: jar, headers });
|
|
609
|
+
jar = mergeCookies(jar, r.setCookies);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const j = r.json || {};
|
|
613
|
+
if (j.status === true && j.question) {
|
|
614
|
+
const st = {
|
|
615
|
+
phone,
|
|
616
|
+
guideUrl: guideFinal,
|
|
617
|
+
challengeUrl,
|
|
618
|
+
submitUrl,
|
|
619
|
+
cookies: jar,
|
|
620
|
+
challengeToken: j.challengeToken || null,
|
|
621
|
+
attempt: Number(j.attempt) || 1,
|
|
622
|
+
maxAttempts: Number(j.maxAttempts) || 3,
|
|
623
|
+
expiresAt: Number(j.expiresAt) || null,
|
|
624
|
+
questionCount: Number(j.questionCount) || 1,
|
|
625
|
+
requiredCorrect: Number(j.requiredCorrect) || 1,
|
|
626
|
+
question: j.question,
|
|
627
|
+
createdAt: new Date().toISOString(),
|
|
628
|
+
};
|
|
629
|
+
writeChallengeState(st);
|
|
630
|
+
emit(questionPayload(st), 0);
|
|
631
|
+
}
|
|
632
|
+
if (j.fallbackRequired === true || j.code === "AGENT_TEXT_FAILED") {
|
|
633
|
+
const h = handoffPayload(j.verificationUrl, guideFinal, phone);
|
|
634
|
+
emit(h.payload, h.exitCode);
|
|
635
|
+
}
|
|
636
|
+
if (j.code === "SMS_RATE_LIMIT") {
|
|
637
|
+
const secs = Number(j.retryAfterSeconds || (j.data && j.data.retryAfterSeconds) || 0);
|
|
348
638
|
const mins = secs > 0 ? Math.max(1, Math.floor(secs / 60)) : 0;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
639
|
+
emit(errorPayload(
|
|
640
|
+
"SMS_RATE_LIMIT",
|
|
641
|
+
mins > 0
|
|
642
|
+
? `验证码发送太频繁了,短信通道要求约 ${mins} 分钟后再试。你先忙别的,想重发时跟我说一声。`
|
|
643
|
+
: "验证码发送太频繁了,稍等几分钟再试;想重发时跟我说一声。",
|
|
644
|
+
"限频停点:等用户明确要求且冷却过后再领题,不自动重试。",
|
|
645
|
+
), 1);
|
|
646
|
+
}
|
|
647
|
+
if (j.code === "AGENT_TEXT_TARGET_LOCKED") {
|
|
648
|
+
emit(errorPayload(
|
|
649
|
+
"AGENT_TEXT_TARGET_LOCKED",
|
|
650
|
+
`这个手机号的开通请求有点频繁,需要稍后再试;想马上用的话可以走网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
651
|
+
"目标锁定:本轮停止,不换手机号、不换会话、不换接口绕过。",
|
|
652
|
+
), 1);
|
|
653
|
+
}
|
|
654
|
+
if (r.requestUnknown) {
|
|
655
|
+
emit({
|
|
656
|
+
...errorPayload(
|
|
657
|
+
"RESULT_UNKNOWN",
|
|
658
|
+
"刚才的请求结果没有确认(网络波动)。先等一下,我稍后再试;如果手机收到了验证码短信,直接发我。",
|
|
659
|
+
"结果未确认:不并发、不立即重试;等用户确认或稍后单次重试。",
|
|
660
|
+
),
|
|
661
|
+
requestUnknown: true,
|
|
662
|
+
detail: (r && r.json && r.json.msg) || null,
|
|
663
|
+
}, 1);
|
|
664
|
+
}
|
|
665
|
+
if (j.code === "SMS_REQUEST_REJECTED") {
|
|
666
|
+
emit(errorPayload(
|
|
667
|
+
"SMS_REQUEST_REJECTED",
|
|
668
|
+
"这个手机号这次没能通过开通校验,麻烦核对一下号码再发我。",
|
|
669
|
+
"不重复提交同一号码,等用户核对后再领题。",
|
|
670
|
+
), 1);
|
|
373
671
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
672
|
+
emit(errorPayload(
|
|
673
|
+
"AGENT_TEXT_UNAVAILABLE",
|
|
674
|
+
`开通服务暂时不可用,可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
675
|
+
"服务不可用:本轮停止,不绕过、不切换其他接口。",
|
|
676
|
+
), 1);
|
|
378
677
|
}
|
|
379
678
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
679
|
+
// —— 第二步:作答(答案由 Agent 计算后传入)——
|
|
680
|
+
if (cmd === "answer") {
|
|
681
|
+
const st = readChallengeState();
|
|
682
|
+
if (!st) {
|
|
683
|
+
emit(errorPayload(
|
|
684
|
+
"CHALLENGE_MISSING",
|
|
685
|
+
null,
|
|
686
|
+
"本机没有进行中的题目:先调用 challenge 领取题目。",
|
|
687
|
+
), 1);
|
|
688
|
+
}
|
|
689
|
+
if (a.phone && a.phone !== true && String(a.phone).trim() !== st.phone) {
|
|
690
|
+
emit(errorPayload(
|
|
691
|
+
"PHONE_MISMATCH",
|
|
692
|
+
null,
|
|
693
|
+
"传入手机号与当前题目不一致:请用同一手机号,或重新调用 challenge。",
|
|
694
|
+
), 1);
|
|
695
|
+
}
|
|
696
|
+
if (challengeExpired(st)) {
|
|
697
|
+
clearChallengeState();
|
|
698
|
+
emit(errorPayload(
|
|
699
|
+
"CHALLENGE_EXPIRED",
|
|
700
|
+
"刚才那道题已经过期了,我重新给你申请一次。",
|
|
701
|
+
"题目过期:重新调用 challenge(新题、新 attempt),不要沿用旧 token。",
|
|
702
|
+
), 1);
|
|
703
|
+
}
|
|
704
|
+
const rawAnswer = a.answer;
|
|
705
|
+
const answer = rawAnswer === undefined || rawAnswer === true ? "" : String(rawAnswer).trim();
|
|
706
|
+
if (!/^(0|[1-9][0-9]*)$/.test(answer)) {
|
|
707
|
+
emit(errorPayload(
|
|
708
|
+
"ANSWER_INVALID",
|
|
709
|
+
null,
|
|
710
|
+
"答案必须是非负整数字符串(不带单位、空格、小数、负号或前导零;零写 \"0\")。重新计算后再次调用 answer。",
|
|
711
|
+
), 1);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const headers = withAttribution({
|
|
715
|
+
"X-MaaS-Entry-Surface": "DIRECT_API",
|
|
716
|
+
"X-MaaS-Client-Name": MAAS_CLIENT_NAME,
|
|
717
|
+
"X-MaaS-Client-Version": readSkillVersion(),
|
|
718
|
+
});
|
|
719
|
+
const r = await httpJson("POST", st.submitUrl, {
|
|
720
|
+
body: { challengeToken: st.challengeToken, attempt: Number(st.attempt), answer },
|
|
721
|
+
cookies: st.cookies,
|
|
722
|
+
headers,
|
|
723
|
+
});
|
|
724
|
+
const jar = mergeCookies(st.cookies, r.setCookies);
|
|
725
|
+
const j = r.json || {};
|
|
726
|
+
|
|
727
|
+
if (j.status === true) {
|
|
728
|
+
clearChallengeState();
|
|
729
|
+
emit({
|
|
730
|
+
status: true,
|
|
731
|
+
code: "SMS_SENT",
|
|
732
|
+
flow: "agent-text",
|
|
733
|
+
user_message: `验证码已发送到 ${maskPhone(st.phone)},请把最新一条短信里的 6 位验证码发我。`,
|
|
734
|
+
instruction: "已发码:转述 user_message 后暂停,等用户回复 6 位验证码,再调用 register。不得重复发码。",
|
|
735
|
+
}, 0);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
if (j.code === "AGENT_TEXT_RETRY") {
|
|
739
|
+
const nc = j.nextChallenge || j.challenge || null;
|
|
740
|
+
const nq = nc && nc.question;
|
|
741
|
+
if (nc && nq) {
|
|
742
|
+
const next = {
|
|
743
|
+
...st,
|
|
744
|
+
cookies: jar,
|
|
745
|
+
challengeToken: nc.challengeToken || nc.token || st.challengeToken,
|
|
746
|
+
attempt: Number(nc.attempt) || Number(st.attempt) + 1,
|
|
747
|
+
maxAttempts: Number(nc.maxAttempts) || Number(st.maxAttempts) || 3,
|
|
748
|
+
expiresAt: Number(nc.expiresAt) || st.expiresAt,
|
|
749
|
+
question: nq,
|
|
750
|
+
lastRetryAt: new Date().toISOString(),
|
|
751
|
+
};
|
|
752
|
+
writeChallengeState(next);
|
|
753
|
+
emit(questionPayload(next, { retry: true }), 3);
|
|
754
|
+
}
|
|
755
|
+
emit(errorPayload(
|
|
756
|
+
"AGENT_TEXT_RETRY",
|
|
757
|
+
null,
|
|
758
|
+
"服务端要求换题但未返回可用新题:本轮停止,重新调用 challenge;不要重置 attempt。",
|
|
759
|
+
), 1);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
if (j.fallbackRequired === true || j.code === "AGENT_TEXT_FAILED") {
|
|
763
|
+
const h = handoffPayload(j.verificationUrl, st.guideUrl || DEFAULT_GUIDE, st.phone);
|
|
764
|
+
clearChallengeState();
|
|
765
|
+
emit(h.payload, h.exitCode);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (j.code === "SMS_RATE_LIMIT" || j.code === "AGENT_TEXT_TARGET_LOCKED" || j.code === "AGENT_TEXT_UNAVAILABLE") {
|
|
769
|
+
clearChallengeState();
|
|
770
|
+
const msg = j.code === "SMS_RATE_LIMIT"
|
|
771
|
+
? "验证码发送太频繁了,稍等几分钟再试;想重发时跟我说一声。"
|
|
772
|
+
: j.code === "AGENT_TEXT_TARGET_LOCKED"
|
|
773
|
+
? `这个手机号的开通请求有点频繁,需要稍后再试;想马上用的话可以走网页方式开通:${FALLBACK_REGISTER_URL}`
|
|
774
|
+
: `开通服务暂时不可用,可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`;
|
|
775
|
+
emit(errorPayload(j.code, msg, "本轮终止:不换会话、不换手机号、不换接口绕过。"), 1);
|
|
384
776
|
}
|
|
385
777
|
|
|
778
|
+
if (r.requestUnknown) {
|
|
779
|
+
// 保留状态:结果未知时由用户决定是否重试,脚本不自动重发
|
|
780
|
+
emit({
|
|
781
|
+
status: false,
|
|
782
|
+
code: "RESULT_UNKNOWN",
|
|
783
|
+
resultUnknown: true,
|
|
784
|
+
user_message: "刚才发送请求的结果没有确认(网络波动)。先看一下手机有没有收到验证码短信:收到了直接把 6 位验证码发我;没收到再跟我说,我重新发。",
|
|
785
|
+
instruction: "结果未确认停点:先等用户确认是否收到短信;未确认前不得再次调用 answer,也不得重新领题发码。",
|
|
786
|
+
}, 4);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
emit(errorPayload(
|
|
790
|
+
j.code || "SEND_FAILED",
|
|
791
|
+
`验证码发送没成功(${j.msg || "网络或短信通道问题"}),可以再试一次;如果连续失败,也可以用网页方式开通:${FALLBACK_REGISTER_URL}`,
|
|
792
|
+
"失败停点:最多重试一次,超限引导网页开通。",
|
|
793
|
+
), 1);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
if (cmd === "register") {
|
|
797
|
+
if (!a.phone || !a.vcode) { console.error("缺少 --phone 或 --vcode"); process.exit(2); }
|
|
386
798
|
const payload = {
|
|
387
|
-
phone:
|
|
388
|
-
vcode:
|
|
389
|
-
password:
|
|
390
|
-
type:
|
|
391
|
-
organ:
|
|
392
|
-
name:
|
|
393
|
-
apiKeyName:
|
|
394
|
-
channel: args.channel && args.channel !== true ? args.channel : DEFAULT_CHANNEL,
|
|
395
|
-
source: args.source && args.source !== true ? args.source : DEFAULT_SOURCE,
|
|
799
|
+
phone: a.phone,
|
|
800
|
+
vcode: a.vcode,
|
|
801
|
+
password: a.password && a.password !== true ? a.password : genPassword(),
|
|
802
|
+
type: a.type && a.type !== true ? a.type : DEFAULT_TYPE,
|
|
803
|
+
organ: a.organ && a.organ !== true ? a.organ : "个人",
|
|
804
|
+
name: a.name && a.name !== true ? a.name : "用户",
|
|
805
|
+
apiKeyName: a["apikey-name"] && a["apikey-name"] !== true ? a["apikey-name"] : "agent-key",
|
|
396
806
|
};
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const
|
|
807
|
+
payload.channel = a.channel && a.channel !== true ? a.channel : DEFAULT_CHANNEL;
|
|
808
|
+
payload.source = a.source && a.source !== true ? a.source : DEFAULT_SOURCE;
|
|
809
|
+
const { body: r } = await post(base + "/register", payload);
|
|
810
|
+
const data = r.data || {};
|
|
400
811
|
// 2026-10-09(user-auth maas-1.1.6):registered:true + deliveryStatus:"PENDING"——
|
|
401
812
|
// 账号已创建、Key 资源尚在准备。非错误:不盲目重注册,等用户示意后在短信码有效期内
|
|
402
813
|
// (5 分钟)用同参数重试一次;过期再走重新发码流程。
|
|
403
|
-
if (!data.apiKey && (data.deliveryStatus === "PENDING" ||
|
|
814
|
+
if (!data.apiKey && (data.deliveryStatus === "PENDING" || r.registered === true)) {
|
|
404
815
|
console.log(JSON.stringify({
|
|
405
816
|
status: false,
|
|
406
817
|
pending: true,
|
|
407
|
-
msg:
|
|
818
|
+
msg: r.msg || null,
|
|
408
819
|
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥
|
|
409
820
|
user_message: "账号已经创建成功,系统正在准备你的访问密钥(一般一两分钟内就好)。稍等后跟我说一声“再试”,我再确认一次;不用重新注册,也不用重新发验证码。",
|
|
410
821
|
instruction: "非错误:不要重新注册、不要重新发码。等用户示意后用同一手机号与同一验证码重试一次 register(短信码 5 分钟有效);若提示验证码过期,再走重新发码流程。",
|
|
411
822
|
}));
|
|
412
823
|
process.exit(0);
|
|
413
824
|
}
|
|
414
|
-
|
|
825
|
+
const ok = Boolean(r.status) && Boolean(data.apiKey);
|
|
826
|
+
let apiKeyToSave = ok ? data.apiKey : "";
|
|
415
827
|
let newKeyCreated = false;
|
|
416
828
|
let newKeyError = null;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
const
|
|
420
|
-
?
|
|
421
|
-
:
|
|
422
|
-
const
|
|
423
|
-
?
|
|
424
|
-
|
|
425
|
-
const created = await createNewApiKey(
|
|
426
|
-
args["open-base"] && args["open-base"] !== true ? args["open-base"] : DEFAULT_OPEN_BASE,
|
|
427
|
-
apiKey,
|
|
829
|
+
if (ok && a["new-key"]) {
|
|
830
|
+
const keyName = a["new-key-name"] && a["new-key-name"] !== true ? a["new-key-name"] : payload.apiKeyName;
|
|
831
|
+
const keyRemark = a["new-key-remark"] && a["new-key-remark"] !== true
|
|
832
|
+
? a["new-key-remark"]
|
|
833
|
+
: "由深知可信PPT按用户要求重新生成";
|
|
834
|
+
const { result, apiKey } = await createNewApiKey(
|
|
835
|
+
assertEndpointAllowed(a["open-base"] && a["open-base"] !== true ? a["open-base"] : DEFAULT_OPEN_BASE, "--open-base"),
|
|
836
|
+
data.apiKey,
|
|
428
837
|
keyName,
|
|
429
838
|
keyRemark,
|
|
430
839
|
);
|
|
431
|
-
if (
|
|
432
|
-
|
|
840
|
+
if (apiKey) {
|
|
841
|
+
apiKeyToSave = apiKey;
|
|
433
842
|
newKeyCreated = true;
|
|
434
843
|
} else {
|
|
435
|
-
//
|
|
436
|
-
newKeyError =
|
|
844
|
+
// 新建失败:沿用原密钥继续(不中断用户任务),newKeyCreated=false 明确区分新旧
|
|
845
|
+
newKeyError = result.errmsg || result.msg || "新 API Key 创建失败";
|
|
437
846
|
}
|
|
438
847
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
// 避免宿主进程读不到环境变量造成"每任务重新注册"的误判。
|
|
442
|
-
const registered = Boolean(apiKey);
|
|
443
|
-
const persistResult = registered && !args["no-persist"]
|
|
444
|
-
? writeApiKeyToConfigFile(apiKey)
|
|
848
|
+
const zshrcWrite = ok && apiKeyToSave && !a["no-zshrc"]
|
|
849
|
+
? writeApiKeyToConfigFile(apiKeyToSave)
|
|
445
850
|
: { written: false, path: apiKeyFilePath(), error: null };
|
|
446
|
-
|
|
851
|
+
// 落盘失败时:把明文 Key 写入仅当前用户可读的待用文件(600),stdout 只给路径与掩码,
|
|
852
|
+
// 避免完整密钥出现在对话框/宿主 UI 回显里。Agent 从该文件读取注入环境变量后即可删除。
|
|
853
|
+
const zshrcCleaned = (zshrcWrite.written && a["clean-zshrc"]) ? (removeLegacyZshrcBlocks(), true) : false;
|
|
854
|
+
let pendingKeyFile = null;
|
|
855
|
+
if (ok && apiKeyToSave && !zshrcWrite.written && !a["no-zshrc"]) {
|
|
856
|
+
try {
|
|
857
|
+
const pendingPath = path.join(configDir(), "api_key.pending");
|
|
858
|
+
fs.mkdirSync(configDir(), { recursive: true, mode: 0o700 });
|
|
859
|
+
fs.writeFileSync(pendingPath, `${String(apiKeyToSave).trim()}\n`, { encoding: "utf8", mode: 0o600 });
|
|
860
|
+
pendingKeyFile = pendingPath;
|
|
861
|
+
} catch { /* 临时文件也写不了时,才退回明文输出 */ }
|
|
862
|
+
}
|
|
447
863
|
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥。
|
|
864
|
+
// 权益告知(注册赠送 10 万积分 + 实名认证再送 10 万积分)已前移到开通引导
|
|
865
|
+
// (initialize.guide_message / onboarding_scripts S1),此处只做轻确认,避免成功节点信息过重导致转述截断。
|
|
448
866
|
let userMessage;
|
|
449
|
-
if (
|
|
450
|
-
userMessage =
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
867
|
+
if (ok && apiKeyToSave) {
|
|
868
|
+
userMessage = Boolean(data.existed)
|
|
869
|
+
? `这个手机号之前开通过,已直接找回原来的密钥和积分,不用重新注册。我马上开始检索。`
|
|
870
|
+
: `开通成功,访问密钥已写入本机,注册赠送的 10 万积分已到账。我马上开始检索。`;
|
|
871
|
+
if (newKeyError) {
|
|
872
|
+
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
873
|
+
}
|
|
874
|
+
} else if (String(r.msg || "").includes("验证码")) {
|
|
875
|
+
userMessage = `验证码校验没通过(可能是输入有误或已过期)。请核对 ${maskPhone(a.phone)} 最新一条短信的 6 位验证码重新发我;需要我重新发送一条,直接说一声。`;
|
|
876
|
+
} else if (String(r.msg || "").includes("已存在")) {
|
|
877
|
+
// 老用户但服务端未放行返 Key(生产环境出现过此形态):据实说明并引导网页
|
|
457
878
|
// 登录,不冒充找回成功、不诱导重复注册。
|
|
458
|
-
userMessage = `这个手机号之前注册过深知账号,这次没能自动取回密钥。可以稍后再试一次,或到平台用手机号验证码登录查看:${
|
|
879
|
+
userMessage = `这个手机号之前注册过深知账号,这次没能自动取回密钥。可以稍后再试一次,或到平台用手机号验证码登录查看:${FALLBACK_REGISTER_URL}`;
|
|
459
880
|
} else {
|
|
460
|
-
userMessage = `开通服务暂时没连上(${
|
|
881
|
+
userMessage = `开通服务暂时没连上(${r.msg || "网络波动"}),可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`;
|
|
461
882
|
}
|
|
462
|
-
if (registered && newKeyError) {
|
|
463
|
-
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
883
|
console.log(JSON.stringify({
|
|
467
|
-
status:
|
|
468
|
-
msg:
|
|
884
|
+
status: ok && Boolean(apiKeyToSave),
|
|
885
|
+
msg: r.msg,
|
|
469
886
|
url: data.url || null,
|
|
470
887
|
existed: Boolean(data.existed),
|
|
471
888
|
keyCreatedByRegister: Boolean(data.keyCreated),
|
|
472
|
-
newKeyRequested: Boolean(
|
|
889
|
+
newKeyRequested: Boolean(a["new-key"]),
|
|
473
890
|
newKeyCreated,
|
|
891
|
+
user_message: userMessage,
|
|
474
892
|
envName: API_KEY_ENV,
|
|
475
893
|
// 明文 Key 只在"未写入本机配置文件、必须靠它临时注入环境变量"时才输出,其余情况
|
|
476
894
|
// 只给掩码——脚本输出可能被宿主 UI 回显或模型贴进对话
|
|
477
|
-
apiKey: (
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
895
|
+
apiKey: (apiKeyToSave && !zshrcWrite.written && !a["no-zshrc"] && !pendingKeyFile) ? apiKeyToSave : null,
|
|
896
|
+
pendingKeyFile,
|
|
897
|
+
apiKeyMasked: apiKeyToSave ? `${apiKeyToSave.slice(0, 7)}...${apiKeyToSave.slice(-4)}` : null,
|
|
898
|
+
zshrcCleaned,
|
|
899
|
+
envWriteRequired: Boolean(apiKeyToSave) && !zshrcWrite.written,
|
|
900
|
+
envWriteTarget: zshrcWrite.path,
|
|
901
|
+
envWriteSucceeded: Boolean(zshrcWrite.written),
|
|
902
|
+
envWriteError: zshrcWrite.error,
|
|
903
|
+
envWriteInstruction: zshrcWrite.written
|
|
904
|
+
? `已写入 ${zshrcWrite.path};脚本直读该文件,无需重启宿主,本次不再返回明文密钥。`
|
|
905
|
+
: (pendingKeyFile
|
|
906
|
+
? `未能写入 ${zshrcWrite.path};明文 Key 已写入仅本用户可读的 ${pendingKeyFile}(600 权限),请由 Agent 读取该文件临时注入环境变量 ${API_KEY_ENV},用完删除该文件;不得展示给用户、不得写入公开文件。`
|
|
907
|
+
: `未能写入 ${zshrcWrite.path},本次已在 apiKey 字段返回明文,请由 Agent 临时注入环境变量 ${API_KEY_ENV}(不得展示给用户、不得写入公开文件)。`),
|
|
908
|
+
currentSessionInstruction: `当前任务继续执行初始化时,请用本次返回的 apiKey 临时注入环境变量 ${API_KEY_ENV},不得向用户展示完整 Key。`,
|
|
486
909
|
newKeyError,
|
|
487
|
-
|
|
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 || "未知原因"}),本次可先用临时密钥继续,稍后再试。`,
|
|
910
|
+
fallbackRegisterUrl: FALLBACK_REGISTER_URL,
|
|
523
911
|
}));
|
|
524
|
-
process.exit(
|
|
912
|
+
process.exit(ok && Boolean(apiKeyToSave) ? 0 : 1);
|
|
525
913
|
}
|
|
526
914
|
|
|
527
|
-
console.error("用法: node register_key.mjs <
|
|
915
|
+
console.error("用法: node register_key.mjs <challenge|answer|register|save-key> ...");
|
|
528
916
|
process.exit(2);
|
|
529
917
|
}
|
|
530
918
|
|