dknowc-dsh 1.4.0 → 2.0.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 +18 -16
- package/skills/dknowc-official-doc-writer/SKILL.md +112 -143
- 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 +18 -8
- 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 +60 -0
- package/skills/dknowc-official-doc-writer/scripts/build_trace_json.py +288 -0
- package/skills/dknowc-official-doc-writer/scripts/check_materials.py +265 -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 +13 -4
- 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 +122 -50
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +238 -94
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +11 -2
- 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 +33 -15
- package/skills/dknowc-ppt-assistant/attribution.json +5 -0
- package/skills/dknowc-ppt-assistant/references/material_usage.md +9 -5
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +14 -2
- 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 +60 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +5 -3
- package/skills/dknowc-ppt-assistant/scripts/mcp_convert.py +188 -0
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +184 -94
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +141 -121
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +33 -3
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +15 -17
- package/skills/dknowc-trusted-search/README.md +27 -23
- package/skills/dknowc-trusted-search/SKILL.md +213 -141
- package/skills/dknowc-trusted-search/attribution.json +5 -0
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +17 -7
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +129 -521
- 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 +26 -5
- package/skills/dknowc-trusted-search/scripts/initialize.py +19 -27
- package/skills/dknowc-trusted-search/scripts/mcp_convert.py +189 -0
- package/skills/dknowc-trusted-search/scripts/merge_search_results.py +272 -0
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +151 -45
- package/skills/dknowc-trusted-search/scripts/render_trace_html.py +340 -129
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +28 -28
- 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,24 +1,45 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// 国内 MaaS headless 注册助手(供 skillhub skill 调用;纯 node,内置 fetch,无三方依赖)。
|
|
3
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-
|
|
5
|
-
//
|
|
6
|
-
//
|
|
4
|
+
// node register_key.mjs [--base URL] register --phone <p> --vcode <c> [--type 11] [--organ ..] [--name ..] [--channel ..] [--source ..] [--password ..] [--new-key] [--no-persist]
|
|
5
|
+
// node register_key.mjs save-key (MCP 取 Key 路径落盘:密钥经 stdin 或 --api-key 传入,随公文写作 3.7.7 移植)
|
|
6
|
+
// 无需 cookie / 加密 / 登录态。注册成功自动把 API Key 写入本机专用配置文件
|
|
7
|
+
// ~/.config/dknowc/api_key(XDG,600 权限;Windows %APPDATA%\dknowc\api_key),
|
|
8
|
+
// 并清理历史 ~/.zshrc Key 标记块;业务脚本从配置文件直读,无需重启宿主;
|
|
9
|
+
// persist 命令可事后手动补持久化。默认打生产,--base 可切测试环境。
|
|
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";
|
|
16
|
-
|
|
23
|
+
// 3.7.7 对齐(2026-09-29 用户决策):source 由 "agent" 改为 skill 名,与 X-Dknowc-Attribution
|
|
24
|
+
// 声明的 source 字段同名同义;body 里的 channel 渠道码(UUID 埋点)保持不变,同名不同义、互不影响。
|
|
25
|
+
const DEFAULT_SOURCE = "dknowc-trusted-search";
|
|
17
26
|
const API_KEY_ENV = "DKNOWC_API_KEY";
|
|
18
27
|
const MAAS_PLATFORM_URL = "https://platform.dknowc.cn/auth/#/login";
|
|
19
28
|
const FALLBACK_REGISTER_URL = MAAS_PLATFORM_URL;
|
|
20
|
-
const
|
|
21
|
-
|
|
29
|
+
const KEY_FILE_NAME = "api_key";
|
|
30
|
+
// 历史 ~/.zshrc 标记块(1.3.5 迁移:写入配置文件后清理,避免污染 shell 配置)
|
|
31
|
+
const ZSHRC_START = "# >>> dknowc trusted search api key >>>";
|
|
32
|
+
const ZSHRC_END = "# <<< dknowc trusted search api key <<<";
|
|
33
|
+
|
|
34
|
+
function apiKeyFilePath() {
|
|
35
|
+
const home = os.homedir();
|
|
36
|
+
if (process.platform === "win32") {
|
|
37
|
+
const appdata = process.env.APPDATA || home;
|
|
38
|
+
return path.join(appdata, "dknowc", KEY_FILE_NAME);
|
|
39
|
+
}
|
|
40
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
41
|
+
return path.join(xdg, "dknowc", KEY_FILE_NAME);
|
|
42
|
+
}
|
|
22
43
|
|
|
23
44
|
function maskPhone(phone) {
|
|
24
45
|
const p = String(phone || "");
|
|
@@ -51,13 +72,34 @@ function parseArgs(argv) {
|
|
|
51
72
|
return out;
|
|
52
73
|
}
|
|
53
74
|
|
|
75
|
+
// 来源声明(X-Dknowc-Attribution,仅统计用、不参与鉴权;随公文写作 3.7.7 移植):
|
|
76
|
+
// 读包根 attribution.json + SKILL.md 的 version;读取失败不加头、不阻断请求。
|
|
77
|
+
function buildAttributionHeader() {
|
|
78
|
+
try {
|
|
79
|
+
const root = path.resolve(__dirname, "..");
|
|
80
|
+
const meta = JSON.parse(fs.readFileSync(path.join(root, "attribution.json"), "utf-8"));
|
|
81
|
+
if (!meta.source) return null;
|
|
82
|
+
const parts = [`kind=${meta.kind || "skill"}`, `source=${meta.source}`];
|
|
83
|
+
try {
|
|
84
|
+
const m = fs.readFileSync(path.join(root, "SKILL.md"), "utf-8").match(/^version:\s*"?([^"\n]+)"?/m);
|
|
85
|
+
if (m) parts.push(`version=${m[1].trim()}`);
|
|
86
|
+
} catch {}
|
|
87
|
+
if (meta.channel) parts.push(`channel=${meta.channel}`);
|
|
88
|
+
return parts.join(";");
|
|
89
|
+
} catch { return null; }
|
|
90
|
+
}
|
|
91
|
+
function withAttribution(headers) {
|
|
92
|
+
const attr = buildAttributionHeader();
|
|
93
|
+
return attr ? { ...headers, "X-Dknowc-Attribution": attr } : headers;
|
|
94
|
+
}
|
|
95
|
+
|
|
54
96
|
async function postJson(url, payload, headers = {}) {
|
|
55
97
|
const controller = new AbortController();
|
|
56
98
|
const timer = setTimeout(() => controller.abort(), 30000);
|
|
57
99
|
try {
|
|
58
100
|
const response = await fetch(url, {
|
|
59
101
|
method: "POST",
|
|
60
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
102
|
+
headers: withAttribution({ "Content-Type": "application/json", ...headers }),
|
|
61
103
|
body: JSON.stringify(payload),
|
|
62
104
|
signal: controller.signal,
|
|
63
105
|
});
|
|
@@ -104,41 +146,40 @@ async function createNewApiKey(openBase, existingApiKey, name, remark) {
|
|
|
104
146
|
return { result, apiKey };
|
|
105
147
|
}
|
|
106
148
|
|
|
107
|
-
function shellSingleQuote(value) {
|
|
108
|
-
return `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
149
|
function escapeRegExp(value) {
|
|
112
150
|
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
113
151
|
}
|
|
114
152
|
|
|
115
|
-
function
|
|
153
|
+
function removeLegacyZshrcBlocks() {
|
|
154
|
+
// 迁移:写入配置文件后从 ~/.zshrc 移除历史 Key 标记块(避免污染 shell 配置)
|
|
116
155
|
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
156
|
try {
|
|
125
|
-
existing = fs.existsSync(zshrcPath) ? fs.readFileSync(zshrcPath, "utf8") : "";
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
157
|
+
let existing = fs.existsSync(zshrcPath) ? fs.readFileSync(zshrcPath, "utf8") : "";
|
|
158
|
+
if (!existing) return;
|
|
159
|
+
const re = new RegExp(`${escapeRegExp(ZSHRC_START)}[\\s\\S]*?${escapeRegExp(ZSHRC_END)}\\n?`, "m");
|
|
160
|
+
if (re.test(existing)) {
|
|
161
|
+
existing = existing.replace(re, "");
|
|
162
|
+
// 保留 zshrc 原有权限,不强制改 600(2026-09-28 审查修复)
|
|
163
|
+
let mode;
|
|
164
|
+
try { mode = fs.statSync(zshrcPath).mode & 0o777; } catch { mode = 0o600; }
|
|
165
|
+
fs.writeFileSync(zshrcPath, existing, { encoding: "utf8", mode });
|
|
166
|
+
}
|
|
167
|
+
} catch {
|
|
168
|
+
/* 清理失败不阻断:遗留块仍由 api_key.py 的 zshrc 兜底读到 */
|
|
129
169
|
}
|
|
170
|
+
}
|
|
130
171
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
: `${existing}${existing && !existing.endsWith("\n") ? "\n" : ""}${block}`;
|
|
135
|
-
|
|
172
|
+
function writeApiKeyToConfigFile(apiKey) {
|
|
173
|
+
// 写本机专用配置文件(纯文本一行 Key,600 权限);成功后清理历史 ~/.zshrc 块
|
|
174
|
+
const filePath = apiKeyFilePath();
|
|
136
175
|
try {
|
|
137
|
-
fs.
|
|
138
|
-
|
|
176
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
177
|
+
fs.writeFileSync(filePath, `${String(apiKey).trim()}\n`, { encoding: "utf8", mode: 0o600 });
|
|
178
|
+
removeLegacyZshrcBlocks();
|
|
179
|
+
return { written: true, path: filePath, error: null };
|
|
139
180
|
} catch (error) {
|
|
140
181
|
const msg = error && error.message ? error.message : String(error);
|
|
141
|
-
return { written: false, path:
|
|
182
|
+
return { written: false, path: filePath, error: msg };
|
|
142
183
|
}
|
|
143
184
|
}
|
|
144
185
|
|
|
@@ -147,6 +188,43 @@ async function main() {
|
|
|
147
188
|
const cmd = args._[0];
|
|
148
189
|
const base = args.base || DEFAULT_BASE;
|
|
149
190
|
|
|
191
|
+
if (cmd === "save-key") {
|
|
192
|
+
// 3.7.7 移植:MCP 取 Key 路径的落盘子命令。宿主提供 dknowc-mcp 的 create_api_key 且调用
|
|
193
|
+
// 成功(返回顶层 JSON {"apiKey": "..."})后,用本命令落盘,用户无需手机号验证码。
|
|
194
|
+
// 密钥来源优先 stdin(printf '%s' "<密钥>" | node register_key.mjs save-key,避免出现在
|
|
195
|
+
// 命令行参数里),其次 --api-key;仅接受 sk- 开头。落盘复用 writeApiKeyToConfigFile
|
|
196
|
+
// (600 权限 + 清理历史 ~/.zshrc 块),与手机号注册路径同一份持久化逻辑。
|
|
197
|
+
let apiKey = "";
|
|
198
|
+
try {
|
|
199
|
+
if (!process.stdin.isTTY) {
|
|
200
|
+
const chunks = [];
|
|
201
|
+
for await (const c of process.stdin) chunks.push(c);
|
|
202
|
+
apiKey = Buffer.concat(chunks).toString("utf-8");
|
|
203
|
+
}
|
|
204
|
+
} catch { /* stdin 读取失败时回退 --api-key */ }
|
|
205
|
+
if (!apiKey.trim() && args["api-key"] && args["api-key"] !== true) apiKey = String(args["api-key"]);
|
|
206
|
+
apiKey = apiKey.trim();
|
|
207
|
+
if (!apiKey.startsWith("sk-") || apiKey.length < 20) {
|
|
208
|
+
console.log(JSON.stringify({
|
|
209
|
+
status: false,
|
|
210
|
+
msg: "密钥格式不符(应为 sk- 开头的 MaaS API Key),请确认调用的是 create_api_key 的返回值",
|
|
211
|
+
}));
|
|
212
|
+
process.exit(1);
|
|
213
|
+
}
|
|
214
|
+
const w = writeApiKeyToConfigFile(apiKey);
|
|
215
|
+
console.log(JSON.stringify({
|
|
216
|
+
status: w.written,
|
|
217
|
+
envWriteSucceeded: w.written,
|
|
218
|
+
envWriteTarget: w.path,
|
|
219
|
+
error: w.error || null,
|
|
220
|
+
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥
|
|
221
|
+
user_message: w.written
|
|
222
|
+
? "已通过你的深知可信工作台授权直接开通搜索功能,无需手机号验证,马上开始检索。"
|
|
223
|
+
: "密钥保存到本机时出现问题,需要改用手机号验证码方式开通。",
|
|
224
|
+
}));
|
|
225
|
+
process.exit(w.written ? 0 : 1);
|
|
226
|
+
}
|
|
227
|
+
|
|
150
228
|
if (cmd === "send") {
|
|
151
229
|
if (!args.phone) {
|
|
152
230
|
console.error("缺少 --phone");
|
|
@@ -217,17 +295,20 @@ async function main() {
|
|
|
217
295
|
newKeyError = created.result.errmsg || created.result.msg || "新 API Key 创建失败";
|
|
218
296
|
}
|
|
219
297
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
298
|
+
// 注册成功自动持久化到本机专用配置文件 ~/.config/dknowc/api_key(1.3.5 起,对齐公文写作
|
|
299
|
+
// 3.7.5:不再写 ~/.zshrc,避免污染 shell 配置;读取顺序 env→配置文件→历史 zshrc 兜底)。
|
|
300
|
+
// --no-persist 可跳过;persist 命令保留用于事后手动补持久化。
|
|
223
301
|
// user_message:给用户的固定话术,Agent 必须原样转述,不得改写后发挥。
|
|
224
|
-
//
|
|
302
|
+
// 权益告知(注册赠送 10 万积分 + 实名认证再送 10 万积分)已前移到开通引导(initialize.guide_message / onboarding_scripts S1),
|
|
225
303
|
// 此处只做轻确认,避免成功节点信息过重导致转述截断。
|
|
304
|
+
const cfgWrite = ok && apiKeyToSave && !args["no-persist"]
|
|
305
|
+
? writeApiKeyToConfigFile(apiKeyToSave)
|
|
306
|
+
: { written: false, path: apiKeyFilePath(), error: null };
|
|
226
307
|
let userMessage;
|
|
227
308
|
if (ok && apiKeyToSave) {
|
|
228
309
|
userMessage = Boolean(data.existed)
|
|
229
310
|
? `这个手机号之前开通过,已直接找回原来的密钥和额度,不用重新注册。我马上开始检索。`
|
|
230
|
-
:
|
|
311
|
+
: `开通成功,访问密钥已写入本机,注册赠送的 10 万积分已到账。我马上开始检索。`;
|
|
231
312
|
if (newKeyError) {
|
|
232
313
|
userMessage += ` 另外你要求的新密钥生成失败(${newKeyError}),已先沿用现有密钥继续,不影响使用;需要的话稍后再重新生成。`;
|
|
233
314
|
}
|
|
@@ -246,23 +327,48 @@ async function main() {
|
|
|
246
327
|
newKeyCreated,
|
|
247
328
|
user_message: userMessage,
|
|
248
329
|
envName: API_KEY_ENV,
|
|
249
|
-
apiKey
|
|
330
|
+
// 防明文泄漏(对齐公文写作 3.7.4):注册成功且已持久化时不输出明文 apiKey,
|
|
331
|
+
// 只输出掩码与写入路径——业务脚本从配置文件直读,无需明文;仅写入失败时回退明文供临时注入。
|
|
332
|
+
apiKey: cfgWrite.written ? null : apiKeyToSave,
|
|
250
333
|
apiKeyMasked: maskKey(apiKeyToSave),
|
|
251
334
|
envWriteRequired: false,
|
|
252
|
-
envWriteTarget:
|
|
253
|
-
envWriteSucceeded: Boolean(
|
|
254
|
-
envWriteError:
|
|
255
|
-
envWriteInstruction:
|
|
256
|
-
? `已写入 ${
|
|
257
|
-
: `未能写入 ${
|
|
258
|
-
currentSessionInstruction:
|
|
335
|
+
envWriteTarget: cfgWrite.path,
|
|
336
|
+
envWriteSucceeded: Boolean(cfgWrite.written),
|
|
337
|
+
envWriteError: cfgWrite.error,
|
|
338
|
+
envWriteInstruction: cfgWrite.written
|
|
339
|
+
? `已写入 ${cfgWrite.path};业务脚本直读该文件,无需重启宿主。`
|
|
340
|
+
: `未能写入 ${cfgWrite.path},可运行 persist 命令补写,或由 Agent / 平台密钥配置将返回的 apiKey 写入环境变量 ${API_KEY_ENV}。`,
|
|
341
|
+
currentSessionInstruction: `已持久化时初始化会从配置文件直读 Key,无需手动注入;未持久化时用本次返回的 apiKey 临时注入环境变量 ${API_KEY_ENV},不得向用户展示完整 Key。`,
|
|
259
342
|
newKeyError,
|
|
260
343
|
fallbackRegisterUrl: FALLBACK_REGISTER_URL,
|
|
261
344
|
}));
|
|
262
345
|
process.exit(ok && Boolean(apiKeyToSave) ? 0 : 1);
|
|
263
346
|
}
|
|
264
347
|
|
|
265
|
-
|
|
348
|
+
if (cmd === "persist") {
|
|
349
|
+
// 手动补持久化:从当前任务临时注入的环境变量(或 --api-key)读取 Key 写入专用配置文件。
|
|
350
|
+
const apiKey = (args["api-key"] && args["api-key"] !== true)
|
|
351
|
+
? args["api-key"]
|
|
352
|
+
: process.env[API_KEY_ENV] || "";
|
|
353
|
+
if (!apiKey) {
|
|
354
|
+
console.error(`缺少 ${API_KEY_ENV} 环境变量或 --api-key`);
|
|
355
|
+
process.exit(2);
|
|
356
|
+
}
|
|
357
|
+
const cfgWrite = writeApiKeyToConfigFile(apiKey);
|
|
358
|
+
console.log(JSON.stringify({
|
|
359
|
+
status: Boolean(cfgWrite.written),
|
|
360
|
+
envName: API_KEY_ENV,
|
|
361
|
+
envWriteTarget: cfgWrite.path,
|
|
362
|
+
envWriteSucceeded: Boolean(cfgWrite.written),
|
|
363
|
+
envWriteError: cfgWrite.error,
|
|
364
|
+
envWriteInstruction: cfgWrite.written
|
|
365
|
+
? `已写入 ${cfgWrite.path};脚本直读该文件,无需重启宿主。`
|
|
366
|
+
: `未能写入 ${cfgWrite.path},请由 Agent 或平台密钥配置将返回的 apiKey 写入环境变量 ${API_KEY_ENV}。`,
|
|
367
|
+
}));
|
|
368
|
+
process.exit(cfgWrite.written ? 0 : 1);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
console.error("用法: node register_key.mjs <send|register|persist> ...");
|
|
266
372
|
process.exit(2);
|
|
267
373
|
}
|
|
268
374
|
|