dknowc-dsh 1.2.1 → 1.4.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 +4 -2
- package/package.json +3 -3
- package/skills/dknowc-official-doc-writer/README.md +15 -38
- package/skills/dknowc-official-doc-writer/SKILL.md +134 -221
- package/skills/dknowc-official-doc-writer/reference/local_memory_guide.md +53 -0
- package/skills/dknowc-official-doc-writer/reference/material_usage_guidance.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/onboarding_scripts.md +132 -0
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +16 -16
- package/skills/dknowc-official-doc-writer/reference/revision_workflow.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/sample_search_result.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/sample_trace_report.html +1006 -224
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +64 -33
- package/skills/dknowc-official-doc-writer/reference/search_intro.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/search_policy.md +26 -13
- package/skills/dknowc-official-doc-writer/reference/standards/01_report.md +3 -2
- package/skills/dknowc-official-doc-writer/reference/standards/02_qingshi.md +3 -2
- package/skills/dknowc-official-doc-writer/reference/standards/03_pifu.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/04_tongzhi.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/05_yijian.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/06_han.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/08_tongbao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/09_tonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/10_gonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/11_fuhan_approve.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/12_fuhan_objection.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/13_reminder.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/14_generic.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/16_decision.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/18_order.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/19_gazette.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/20_motion.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/22_application.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/23_publicity.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/task_router.md +10 -10
- package/skills/dknowc-official-doc-writer/scripts/adapt_mcp_result.py +52 -2
- package/skills/dknowc-official-doc-writer/scripts/api_key.py +41 -0
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +40 -7
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +30 -26
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +45 -23
- package/skills/dknowc-official-doc-writer/scripts/local_memory.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/mcp_direct.py +9 -1
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +7 -11
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +51 -10
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +1400 -595
- package/skills/dknowc-official-doc-writer/scripts/review_document.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +174 -48
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +3 -3
- package/skills/dknowc-ppt-assistant/README.md +4 -4
- package/skills/dknowc-ppt-assistant/SKILL.md +16 -20
- package/skills/dknowc-ppt-assistant/references/content-pack.md +3 -3
- package/skills/dknowc-ppt-assistant/references/material_usage.md +9 -6
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +120 -0
- package/skills/dknowc-ppt-assistant/references/sample_search_result.md +1 -1
- package/skills/dknowc-ppt-assistant/references/sample_trace_report.html +1543 -0
- 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 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +54 -23
- package/skills/dknowc-ppt-assistant/scripts/mcp_direct.py +8 -0
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +112 -26
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +1556 -609
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +65 -15
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +10 -10
- package/skills/dknowc-ppt-assistant/workflows/routing.md +1 -1
- package/skills/dknowc-trusted-consulting/README.md +8 -9
- package/skills/dknowc-trusted-consulting/SKILL.md +54 -34
- package/skills/dknowc-trusted-consulting/reference/consult_intro.md +3 -35
- package/skills/dknowc-trusted-consulting/reference/onboarding_scripts.md +132 -0
- package/skills/dknowc-trusted-consulting/reference/sample_trace_report.html +682 -272
- package/skills/dknowc-trusted-consulting/scripts/api_key.py +41 -0
- package/skills/dknowc-trusted-consulting/scripts/gov_chat.py +39 -19
- package/skills/dknowc-trusted-consulting/scripts/initialize.py +52 -18
- package/skills/dknowc-trusted-consulting/scripts/mcp_direct.py +8 -0
- package/skills/dknowc-trusted-consulting/scripts/register_key.mjs +122 -21
- package/skills/dknowc-trusted-consulting/scripts/render_trace_html.py +1665 -743
- package/skills/dknowc-trusted-search/README.md +2 -1
- package/skills/dknowc-trusted-search/SKILL.md +33 -33
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +132 -0
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +1148 -233
- package/skills/dknowc-trusted-search/scripts/api_key.py +41 -0
- package/skills/dknowc-trusted-search/scripts/deep_query.py +81 -9
- package/skills/dknowc-trusted-search/scripts/initialize.py +64 -17
- package/skills/dknowc-trusted-search/scripts/mcp_direct.py +8 -0
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +111 -24
- package/skills/dknowc-trusted-search/scripts/render_policy_visualization.py +2 -3
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +1688 -718
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +86 -14
- package/src/index.js +3 -3
- package/skills/dknowc-official-doc-writer/official-docs/input/mcp_intro_content.md +0 -61
- package/skills/dknowc-official-doc-writer/scripts/check_release.py +0 -93
- package/skills/dknowc-ppt-assistant/scripts/check_release.py +0 -106
- package/skills/dknowc-trusted-consulting/scripts/check_release.py +0 -66
- package/skills/dknowc-trusted-search/reference/search_intro.md +0 -79
- package/skills/dknowc-trusted-search/scripts/check_release.py +0 -79
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
- 知识库覆盖 600 万篇公开规范性文件,接入 7000 万篇可溯源、可核验的权威公开资料,每日更新。
|
|
9
9
|
- 覆盖范围包括 54 个行业、300 多个地市、2800 多个县,国家和地方政策均可在对应层级检索。
|
|
10
10
|
- 检索结果按演示需要分为政策依据、数据支撑、典型案例三类素材,每条素材带原文出处(发文机关、文号、发布日期、原文链接),可点开核验。
|
|
11
|
-
- 对演示文稿的直接价值:PPT
|
|
11
|
+
- 对演示文稿的直接价值:PPT 里的政策名、数字、案例不再凭印象写,汇报场合经得起追问;权威数据可直接编译为原生可编辑图表;执行过检索的任务另附可点击的溯源核验报告,页面上每条引用都可回溯到原文。
|
|
12
12
|
|
|
13
13
|
向用户介绍能力时,从上述素材中挑与当前任务最相关的两三点说,不逐条罗列;数据表述保持本文件口径,不夸大、不使用"百分百准确""绝对权威"等表述。
|
|
14
14
|
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
python3 {skillDir}/scripts/svg_quality_checker.py dknowc-projects/<项目名> --quick-generate --stage final --json
|
|
76
76
|
|
|
77
77
|
# 编译导出(quick 模式)
|
|
78
|
-
uv run --with python-pptx --with XlsxWriter python3 {skillDir}/scripts/svg_to_pptx.py dknowc-projects/<项目名> --quick-generate
|
|
78
|
+
uv run --with python-pptx --with XlsxWriter uv run --with python-pptx --with XlsxWriter python3 {skillDir}/scripts/svg_to_pptx.py dknowc-projects/<项目名> --quick-generate
|
|
79
79
|
```
|
|
80
80
|
|
|
81
81
|
质检 errors(exit 1)必须修复后重新导出;warnings 可评估后放行。导出成功后 `.pptx` 写入项目 `exports/`。
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""统一解析 DKNOWC_API_KEY:优先进程环境变量,缺失时从 ~/.zshrc 兜底解析。
|
|
3
|
+
|
|
4
|
+
宿主应用(WorkBuddy 等)的会话进程可能读不到用户 shell 环境变量——启动早于
|
|
5
|
+
key 写入,或宿主安全更新后不再加载 ~/.zshrc 的导出变量(WorkBuddy 5.5.3 实测
|
|
6
|
+
持续读不到)。进程环境变量缺失但盘上已有 key 时直接从 zshrc 解析,避免误报
|
|
7
|
+
缺失、触发不必要的手机号验证码引导,也免去"写完 key 必须重启宿主"的限制。key 仅在本机解析并用于调用深知
|
|
8
|
+
接口,不写入任何其他位置,不随公开包分发。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
API_KEY_ENV = "DKNOWC_API_KEY"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def resolve_api_key() -> tuple[str, str]:
|
|
21
|
+
"""返回 (key, source)。source ∈ {'environment', 'zshrc', ''}(空串表示未找到)。"""
|
|
22
|
+
value = os.environ.get(API_KEY_ENV, "").strip()
|
|
23
|
+
if value:
|
|
24
|
+
return value, "environment"
|
|
25
|
+
|
|
26
|
+
zshrc = Path.home() / ".zshrc"
|
|
27
|
+
try:
|
|
28
|
+
text = zshrc.read_text(encoding="utf-8", errors="ignore")
|
|
29
|
+
except OSError:
|
|
30
|
+
return "", ""
|
|
31
|
+
|
|
32
|
+
# 匹配 export DKNOWC_API_KEY='xxx' / "xxx" / xxx;多条时取最后一条(最新写入)
|
|
33
|
+
pattern = re.compile(
|
|
34
|
+
r'^\s*export\s+' + re.escape(API_KEY_ENV) + r'\s*=\s*[\'"]?([^\'"\r\n#]+?)[\'"]?\s*$',
|
|
35
|
+
re.M,
|
|
36
|
+
)
|
|
37
|
+
for value in reversed(pattern.findall(text)):
|
|
38
|
+
value = value.strip()
|
|
39
|
+
if value:
|
|
40
|
+
return value, "zshrc"
|
|
41
|
+
return "", ""
|
|
@@ -15,6 +15,7 @@ Public 版统一只从环境变量 DKNOWC_API_KEY 读取 API Key。
|
|
|
15
15
|
import argparse
|
|
16
16
|
import json
|
|
17
17
|
import os
|
|
18
|
+
import sys
|
|
18
19
|
import platform
|
|
19
20
|
import shutil
|
|
20
21
|
|
|
@@ -47,46 +48,49 @@ def _looks_like_key(value: str) -> bool:
|
|
|
47
48
|
|
|
48
49
|
|
|
49
50
|
def _in_dsh() -> bool:
|
|
50
|
-
"""dsh 环境标志:dsh 的 bash 工具会注入 DSH_SHELL=1。
|
|
51
|
-
|
|
52
|
-
dsh 场景下,脚本子进程无法隐式继承 DKNOWC_API_KEY(dsh 安全机制会清理
|
|
53
|
-
名字含 KEY 的环境变量),但 dsh 插件会通过 shell-env 显式通道把该 Key
|
|
54
|
-
注入为 DSH_DKNOWC_API_KEY。检索子能力走 MCP 转接,无 Key 时仅暂停
|
|
55
|
-
检索,不阻断纯生成任务。
|
|
56
|
-
"""
|
|
51
|
+
"""dsh 环境标志:dsh 的 bash 工具会注入 DSH_SHELL=1。"""
|
|
57
52
|
return os.environ.get("DSH_SHELL") == "1"
|
|
58
53
|
|
|
59
54
|
|
|
60
55
|
def check_api_key_config():
|
|
61
56
|
if _in_dsh():
|
|
57
|
+
# dsh 场景:优先插件经 shell-env 注入的 DSH_DKNOWC_API_KEY(与 MCP Bearer 同源);
|
|
58
|
+
# 缺失时 ~/.zshrc 兜底。无 Key 仅暂停检索,不阻断纯生成(材料/免检索模式)。
|
|
62
59
|
api_key = os.environ.get("DSH_DKNOWC_API_KEY", "").strip()
|
|
60
|
+
source = "environment"
|
|
61
|
+
if not _looks_like_key(api_key):
|
|
62
|
+
try:
|
|
63
|
+
from api_key import resolve_api_key as _rk
|
|
64
|
+
api_key2, source2 = _rk()
|
|
65
|
+
if _looks_like_key(api_key2):
|
|
66
|
+
api_key, source = api_key2, source2
|
|
67
|
+
except ImportError:
|
|
68
|
+
pass
|
|
63
69
|
if _looks_like_key(api_key):
|
|
64
|
-
return {
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"api_key_source": "environment",
|
|
68
|
-
"api_key_hint": None,
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
"api_key_configured": False,
|
|
72
|
-
"api_key_env": API_KEY_ENV,
|
|
73
|
-
"api_key_source": None,
|
|
74
|
-
"api_key_hint": f"未检测到可用的 {API_KEY_ENV}(dsh 主进程环境变量未配置或为空)。仅检索任务受影响,不涉及检索的生成任务可继续。",
|
|
75
|
-
}
|
|
70
|
+
return {"api_key_configured": True, "api_key_env": API_KEY_ENV, "api_key_source": source, "api_key_hint": None}
|
|
71
|
+
return {"api_key_configured": False, "api_key_env": API_KEY_ENV, "api_key_source": None,
|
|
72
|
+
"api_key_hint": f"未检测到可用的 {API_KEY_ENV}(dsh 主进程环境变量与 ~/.zshrc 中均未找到)。需要先将有效的 API Key 配置到启动 dsh 的环境变量 {API_KEY_ENV}(如 ~/.zshrc),再重启 dsh 或新建会话。仅检索任务受影响,不涉及检索的生成任务可继续。"}
|
|
76
73
|
|
|
77
|
-
|
|
74
|
+
# 环境变量优先,缺失时从 ~/.zshrc 兜底解析(宿主进程早于 key 写入启动、
|
|
75
|
+
# 或宿主不再加载 ~/.zshrc 导出变量时不误报缺失,与公文写作同源方案)
|
|
76
|
+
try:
|
|
77
|
+
from api_key import resolve_api_key
|
|
78
|
+
api_key, source = resolve_api_key()
|
|
79
|
+
except ImportError:
|
|
80
|
+
api_key = os.environ.get(API_KEY_ENV, "").strip()
|
|
81
|
+
source = "environment" if api_key else ""
|
|
78
82
|
if _looks_like_key(api_key):
|
|
79
83
|
return {
|
|
80
84
|
"api_key_configured": True,
|
|
81
85
|
"api_key_env": API_KEY_ENV,
|
|
82
|
-
"api_key_source":
|
|
86
|
+
"api_key_source": source,
|
|
83
87
|
"api_key_hint": None,
|
|
84
88
|
}
|
|
85
89
|
return {
|
|
86
90
|
"api_key_configured": False,
|
|
87
91
|
"api_key_env": API_KEY_ENV,
|
|
88
92
|
"api_key_source": None,
|
|
89
|
-
"api_key_hint": f"本 Skill 的素材检索需要通过环境变量 {API_KEY_ENV}
|
|
93
|
+
"api_key_hint": f"本 Skill 的素材检索需要通过环境变量 {API_KEY_ENV}(或 ~/.zshrc 持久化)连接深知可信智能服务。当前两处均未检测到可用 Key,请先注册或登录深知可信智能 MaaS 账号获取 API Key。",
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
|
|
@@ -131,8 +135,33 @@ def check_environment():
|
|
|
131
135
|
if not config_status["api_key_configured"]:
|
|
132
136
|
search_blocking_issues.append("api_key_missing")
|
|
133
137
|
|
|
138
|
+
# guide_message:需要检索但 Key 未配置时给用户的引导话术(S1 三段式:价值 / 权益+开通方式 / 退路+样例钩子)。
|
|
139
|
+
# Agent 结合当前任务语境转述(完整口径见 references/onboarding_scripts.md)。
|
|
140
|
+
guide_message = None if config_status["api_key_configured"] else (
|
|
141
|
+
"这份汇报 PPT 需要引用政策原文和权威数据,凭印象写政策名和数字,汇报场合被当场指出来最影响效果。"
|
|
142
|
+
"开通权威检索后,每条政策、数据都带原文出处、可点开核验,权威数据还能直接做成可编辑的原生图表。\n"
|
|
143
|
+
"开通是免费的:自带 300 次权威检索额度,完成实名认证还能再领 100 元体验金。"
|
|
144
|
+
"只需手机号收一次验证码——两步、约 10 秒,不用去网站,剩下的我来办;手机号仅用于本次验证,不会有营销骚扰。\n"
|
|
145
|
+
"也可以先不开通:我基于你手头的材料先把 PPT 做出来,政策和数据的位置先标注「数据待核验」。"
|
|
146
|
+
"想先看看开通后自动生成的核验报告长什么样,我可以发你一份示例看看。"
|
|
147
|
+
)
|
|
148
|
+
# env_message:依赖缺失时给用户的统一话术——用「小组件」等通俗说法(requests/python-pptx 等组件名
|
|
149
|
+
# 对用户无理解价值);就绪时不输出环境话题。多 Python 环境下检测口径以 python_executable 为准。
|
|
150
|
+
missing_basic = [k for k, ok in (("小组件", requests_available),) if not ok]
|
|
151
|
+
missing_compile = 2 - int(python_pptx_available) - int(xlsxwriter_available)
|
|
152
|
+
if blocking_issues or pptx_blocking_issues:
|
|
153
|
+
total = len(missing_basic) + missing_compile
|
|
154
|
+
env_message = (
|
|
155
|
+
f"检测到本机制作环境需要补装 {'一个' if total == 1 else str(total)} 个小组件"
|
|
156
|
+
"(约 10 秒,只装一次),我现在装好可以吗?"
|
|
157
|
+
"(基础小组件影响检索,编译小组件影响最后的 PPT 导出,缺失时我会自动用隔离环境补齐)"
|
|
158
|
+
)
|
|
159
|
+
else:
|
|
160
|
+
env_message = None
|
|
161
|
+
|
|
134
162
|
return {
|
|
135
163
|
"python": platform.python_version(),
|
|
164
|
+
"python_executable": sys.executable,
|
|
136
165
|
"python3_available": python3_available,
|
|
137
166
|
"requests": requests_available,
|
|
138
167
|
"python_pptx": python_pptx_available,
|
|
@@ -152,7 +181,9 @@ def check_environment():
|
|
|
152
181
|
),
|
|
153
182
|
"blocking_issues": blocking_issues,
|
|
154
183
|
"ready": not blocking_issues,
|
|
155
|
-
"
|
|
184
|
+
"guide_message": guide_message,
|
|
185
|
+
"env_message": env_message,
|
|
186
|
+
"maas_platform_url": "https://platform.dknowc.cn/auth/#/login",
|
|
156
187
|
"environment_state": {
|
|
157
188
|
"dependency_install_declined": bool(state.get("dependency_install_declined")),
|
|
158
189
|
},
|
|
@@ -119,6 +119,14 @@ def main() -> int:
|
|
|
119
119
|
args = parser.parse_args()
|
|
120
120
|
|
|
121
121
|
api_key = os.environ.get(API_KEY_ENV, "").strip()
|
|
122
|
+
if not api_key:
|
|
123
|
+
# dsh 兜底:注册成功后 Key 已自动写入 ~/.zshrc(register_key.mjs 持久化),
|
|
124
|
+
# 本进程环境变量可能未注入——从 ~/.zshrc 解析(api_key.py 同源逻辑)。
|
|
125
|
+
try:
|
|
126
|
+
from api_key import resolve_api_key
|
|
127
|
+
api_key, _src = resolve_api_key()
|
|
128
|
+
except ImportError:
|
|
129
|
+
pass
|
|
122
130
|
if not api_key:
|
|
123
131
|
print(f"错误:缺少环境变量 {API_KEY_ENV}(请用 DKNOWC_API_KEY=<key> 前缀赋值方式传入)。", file=sys.stderr)
|
|
124
132
|
return 2
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// MaaS
|
|
3
|
-
//
|
|
4
|
-
// node register_key.mjs
|
|
5
|
-
//
|
|
2
|
+
// 国内 MaaS headless 注册助手(供 skillhub skill 调用;纯 node,内置 fetch,无三方依赖)。
|
|
3
|
+
// node register_key.mjs [--base URL] send --phone <p>
|
|
4
|
+
// node register_key.mjs [--base URL] register --phone <p> --vcode <c> [--type 11] [--organ ..] [--name ..] [--channel ..] [--source ..] [--password ..] [--new-key] [--no-zshrc]
|
|
5
|
+
// 无需 cookie / 加密 / 登录态。注册成功后自动把 API Key 写入 ~/.zshrc 标记块(--no-zshrc 跳过);
|
|
6
|
+
// 业务脚本从 ~/.zshrc 直读,无需重启宿主。默认打生产,--base 可切测试环境。
|
|
7
|
+
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
import os from "os";
|
|
10
|
+
import path from "path";
|
|
6
11
|
|
|
7
12
|
const DEFAULT_BASE = "https://platform.dknowc.cn/auth/home/userAuto";
|
|
8
13
|
const DEFAULT_OPEN_BASE = "https://open.dknowc.cn";
|
|
@@ -10,7 +15,21 @@ const DEFAULT_CHANNEL = "46A3BA1D-3E1A-4E8C-BD50-A6DCBEE1DB05";
|
|
|
10
15
|
const DEFAULT_TYPE = "11";
|
|
11
16
|
const DEFAULT_SOURCE = "agent";
|
|
12
17
|
const API_KEY_ENV = "DKNOWC_API_KEY";
|
|
13
|
-
const MAAS_PLATFORM_URL = "https://platform.dknowc.cn/";
|
|
18
|
+
const MAAS_PLATFORM_URL = "https://platform.dknowc.cn/auth/#/login";
|
|
19
|
+
const FALLBACK_REGISTER_URL = MAAS_PLATFORM_URL;
|
|
20
|
+
const ZSHRC_START = "# >>> dknowc api key >>>";
|
|
21
|
+
const ZSHRC_END = "# <<< dknowc api key <<<";
|
|
22
|
+
|
|
23
|
+
function maskPhone(phone) {
|
|
24
|
+
const p = String(phone || "");
|
|
25
|
+
return p.length >= 7 ? `${p.slice(0, 3)}****${p.slice(-4)}` : p;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function maskKey(apiKey) {
|
|
29
|
+
if (!apiKey) return null;
|
|
30
|
+
if (apiKey.length <= 12) return "***";
|
|
31
|
+
return `${apiKey.slice(0, 7)}...${apiKey.slice(-4)}`;
|
|
32
|
+
}
|
|
14
33
|
|
|
15
34
|
function parseArgs(argv) {
|
|
16
35
|
const out = { _: [] };
|
|
@@ -85,10 +104,42 @@ async function createNewApiKey(openBase, existingApiKey, name, remark) {
|
|
|
85
104
|
return { result, apiKey };
|
|
86
105
|
}
|
|
87
106
|
|
|
88
|
-
function
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
107
|
+
function shellSingleQuote(value) {
|
|
108
|
+
return `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function escapeRegExp(value) {
|
|
112
|
+
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function writeApiKeyToZshrc(apiKey) {
|
|
116
|
+
const zshrcPath = path.join(os.homedir(), ".zshrc");
|
|
117
|
+
const block = [
|
|
118
|
+
ZSHRC_START,
|
|
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
|
+
}
|
|
92
143
|
}
|
|
93
144
|
|
|
94
145
|
async function main() {
|
|
@@ -101,13 +152,23 @@ async function main() {
|
|
|
101
152
|
console.error("缺少 --phone");
|
|
102
153
|
process.exit(2);
|
|
103
154
|
}
|
|
104
|
-
|
|
155
|
+
// 渠道细分:sendMessage 与 register 请求体统一携带 SkillHub 渠道码。
|
|
105
156
|
const result = await postJson(`${base}/sendMessage`, {
|
|
106
157
|
phone: args.phone,
|
|
107
158
|
type: "register",
|
|
108
|
-
channel,
|
|
159
|
+
channel: args.channel && args.channel !== true ? args.channel : DEFAULT_CHANNEL,
|
|
109
160
|
});
|
|
110
|
-
|
|
161
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥
|
|
162
|
+
let userMessage;
|
|
163
|
+
if (result.status) {
|
|
164
|
+
userMessage = `验证码已发送到 ${maskPhone(args.phone)},请把最新一条短信里的 6 位验证码发我。`;
|
|
165
|
+
} else if (String(result.msg || "").includes("手机号")) {
|
|
166
|
+
userMessage = "这个手机号格式好像不对,麻烦核对一下再发我。";
|
|
167
|
+
} else {
|
|
168
|
+
userMessage = `验证码发送没成功(可能是网络或短信通道问题),可以再试一次;如果连续失败,也可以用网页方式开通:${FALLBACK_REGISTER_URL}`;
|
|
169
|
+
}
|
|
170
|
+
console.log(JSON.stringify({ ...result, user_message: userMessage }));
|
|
171
|
+
if (result.status) console.error("验证码已发送(话术见 user_message,请向用户转述后索取 6 位验证码)。");
|
|
111
172
|
process.exit(result.status ? 0 : 1);
|
|
112
173
|
}
|
|
113
174
|
|
|
@@ -131,11 +192,11 @@ async function main() {
|
|
|
131
192
|
|
|
132
193
|
const result = await postJson(`${base}/register`, payload);
|
|
133
194
|
const data = result.data || {};
|
|
134
|
-
|
|
195
|
+
const ok = Boolean(result.status) && Boolean(data.apiKey);
|
|
196
|
+
let apiKeyToSave = ok ? data.apiKey : "";
|
|
135
197
|
let newKeyCreated = false;
|
|
136
198
|
let newKeyError = null;
|
|
137
|
-
|
|
138
|
-
if (apiKey && args["new-key"]) {
|
|
199
|
+
if (ok && args["new-key"]) {
|
|
139
200
|
const keyName = args["new-key-name"] && args["new-key-name"] !== true
|
|
140
201
|
? args["new-key-name"]
|
|
141
202
|
: payload.apiKeyName;
|
|
@@ -144,36 +205,61 @@ async function main() {
|
|
|
144
205
|
: "由 SkillHub 深知可信搜索按用户要求重新生成";
|
|
145
206
|
const created = await createNewApiKey(
|
|
146
207
|
args["open-base"] && args["open-base"] !== true ? args["open-base"] : DEFAULT_OPEN_BASE,
|
|
147
|
-
apiKey,
|
|
208
|
+
data.apiKey,
|
|
148
209
|
keyName,
|
|
149
210
|
keyRemark,
|
|
150
211
|
);
|
|
151
212
|
if (created.apiKey) {
|
|
152
|
-
|
|
213
|
+
apiKeyToSave = created.apiKey;
|
|
153
214
|
newKeyCreated = true;
|
|
154
215
|
} else {
|
|
155
|
-
|
|
216
|
+
// 新建失败:沿用原密钥继续(不中断用户任务),newKeyCreated=false 明确区分新旧
|
|
156
217
|
newKeyError = created.result.errmsg || created.result.msg || "新 API Key 创建失败";
|
|
157
218
|
}
|
|
158
219
|
}
|
|
159
|
-
|
|
220
|
+
const zshrcWrite = ok && apiKeyToSave && !args["no-zshrc"]
|
|
221
|
+
? writeApiKeyToZshrc(apiKeyToSave)
|
|
222
|
+
: { written: false, path: path.join(os.homedir(), ".zshrc"), error: null };
|
|
223
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥。
|
|
224
|
+
// 权益告知(300 次额度 + 实名认证赠金)已前移到开通引导(initialize.guide_message / onboarding_scripts S1),
|
|
225
|
+
// 此处只做轻确认,避免成功节点信息过重导致转述截断。
|
|
226
|
+
let userMessage;
|
|
227
|
+
if (ok && apiKeyToSave) {
|
|
228
|
+
userMessage = Boolean(data.existed)
|
|
229
|
+
? `这个手机号之前开通过,已直接找回原来的密钥和额度,不用重新注册。我马上开始检索。`
|
|
230
|
+
: `开通成功,访问密钥已写入本机,300 次免费检索额度已生效。我马上开始检索。`;
|
|
231
|
+
if (newKeyError) {
|
|
232
|
+
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
233
|
+
}
|
|
234
|
+
} else if (String(result.msg || "").includes("验证码")) {
|
|
235
|
+
userMessage = `验证码校验没通过(可能是输入有误或已过期)。请核对 ${maskPhone(args.phone)} 最新一条短信的 6 位验证码重新发我;需要我重新发送一条,直接说一声。`;
|
|
236
|
+
} else {
|
|
237
|
+
userMessage = `开通服务暂时没连上(${result.msg || "网络波动"}),可以稍后再试,或用网页方式开通:${FALLBACK_REGISTER_URL}`;
|
|
238
|
+
}
|
|
160
239
|
console.log(JSON.stringify({
|
|
161
|
-
status: Boolean(
|
|
240
|
+
status: ok && Boolean(apiKeyToSave),
|
|
162
241
|
msg: result.msg,
|
|
163
242
|
url: data.url || null,
|
|
164
243
|
existed: Boolean(data.existed),
|
|
165
244
|
keyCreatedByRegister: Boolean(data.keyCreated),
|
|
166
245
|
newKeyRequested: Boolean(args["new-key"]),
|
|
167
246
|
newKeyCreated,
|
|
247
|
+
user_message: userMessage,
|
|
168
248
|
envName: API_KEY_ENV,
|
|
169
|
-
apiKey,
|
|
170
|
-
apiKeyMasked: maskKey(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
249
|
+
apiKey: apiKeyToSave,
|
|
250
|
+
apiKeyMasked: maskKey(apiKeyToSave),
|
|
251
|
+
envWriteRequired: false,
|
|
252
|
+
envWriteTarget: zshrcWrite.path,
|
|
253
|
+
envWriteSucceeded: Boolean(zshrcWrite.written),
|
|
254
|
+
envWriteError: zshrcWrite.error,
|
|
255
|
+
envWriteInstruction: zshrcWrite.written
|
|
256
|
+
? `已写入 ${zshrcWrite.path};脚本直读该文件,无需重启宿主。当前任务可继续使用返回的 apiKey。`
|
|
257
|
+
: `未能写入 ${zshrcWrite.path},请由 Agent 或平台密钥配置将返回的 apiKey 写入环境变量 ${API_KEY_ENV}。`,
|
|
258
|
+
currentSessionInstruction: `当前任务继续执行初始化时,请用本次返回的 apiKey 临时注入环境变量 ${API_KEY_ENV},不得向用户展示完整 Key。`,
|
|
174
259
|
newKeyError,
|
|
260
|
+
fallbackRegisterUrl: FALLBACK_REGISTER_URL,
|
|
175
261
|
}));
|
|
176
|
-
process.exit(
|
|
262
|
+
process.exit(ok && Boolean(apiKeyToSave) ? 0 : 1);
|
|
177
263
|
}
|
|
178
264
|
|
|
179
265
|
console.error("用法: node register_key.mjs <send|register> ...");
|