qiyu-cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +467 -2
- package/dist/cli.js +248 -0
- package/dist/commands/api.js +770 -0
- package/dist/commands/auth.js +349 -0
- package/dist/commands/credentials-source.js +70 -0
- package/dist/commands/db.js +503 -0
- package/dist/commands/local-fallback-wiring.js +67 -0
- package/dist/commands/local-fallback.js +34 -0
- package/dist/commands/query.js +564 -0
- package/dist/commands/skill-specs.js +64 -0
- package/dist/commands/skill.js +908 -0
- package/dist/commands/spec-types.js +145 -0
- package/dist/commands/stats.js +299 -0
- package/dist/commands/store-flags.js +56 -0
- package/dist/commands/store.js +258 -0
- package/dist/commands/sync-run.js +479 -0
- package/dist/commands/sync.js +757 -0
- package/dist/commands/tree.js +27 -0
- package/dist/core/config.js +259 -0
- package/dist/core/credentials.js +71 -0
- package/dist/core/edition.js +28 -0
- package/dist/core/envelope.js +201 -0
- package/dist/core/errors.js +356 -0
- package/dist/core/http.js +348 -0
- package/dist/core/ratelimit.js +126 -0
- package/dist/core/registry/index.js +212 -0
- package/dist/core/registry/params/ai.knowledge.addKnowledge.json +88 -0
- package/dist/core/registry/params/ai.knowledge.deleteKnowledges.json +46 -0
- package/dist/core/registry/params/ai.knowledge.editChannelAnswer.json +60 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledge.json +95 -0
- package/dist/core/registry/params/ai.knowledge.editKnowledgeNoSimilars.json +102 -0
- package/dist/core/registry/params/ai.knowledge.getCategoryTree.json +39 -0
- package/dist/core/registry/params/ai.knowledge.getDetail.json +46 -0
- package/dist/core/registry/params/ai.knowledge.search.json +67 -0
- package/dist/core/registry/params/ai.statistic.transferHuman.json +60 -0
- package/dist/core/registry/params/category.list.json +39 -0
- package/dist/core/registry/params/chat.queue.group.query.json +39 -0
- package/dist/core/registry/params/crm.modifyOrder.json +48 -0
- package/dist/core/registry/params/crm.modifyUser.json +48 -0
- package/dist/core/registry/params/crm.syncCrmInfo.json +46 -0
- package/dist/core/registry/params/crm.tagChange.json +74 -0
- package/dist/core/registry/params/data.overview.ipcc.json +32 -0
- package/dist/core/registry/params/data.overview.session.json +53 -0
- package/dist/core/registry/params/evaluation.setting.get.json +39 -0
- package/dist/core/registry/params/event.applyStaff.json +116 -0
- package/dist/core/registry/params/event.evaluate.json +74 -0
- package/dist/core/registry/params/event.inviteEvaluate.json +81 -0
- package/dist/core/registry/params/event.qcResult.json +166 -0
- package/dist/core/registry/params/event.queryQueueStatus.json +39 -0
- package/dist/core/registry/params/event.queueEnter.json +40 -0
- package/dist/core/registry/params/event.queueLeave.json +46 -0
- package/dist/core/registry/params/event.quitQueue.json +39 -0
- package/dist/core/registry/params/event.sessionChange.json +95 -0
- package/dist/core/registry/params/event.sessionData.json +452 -0
- package/dist/core/registry/params/event.sessionEnd.json +117 -0
- package/dist/core/registry/params/event.sessionStart.json +109 -0
- package/dist/core/registry/params/event.updateUInfo.json +46 -0
- package/dist/core/registry/params/export.session.check.json +7 -0
- package/dist/core/registry/params/export.session.json +7 -0
- package/dist/core/registry/params/export.session.one.json +7 -0
- package/dist/core/registry/params/export.session.one.message.json +7 -0
- package/dist/core/registry/params/get_call_order.json +50 -0
- package/dist/core/registry/params/get_call_user_info.json +36 -0
- package/dist/core/registry/params/get_order.json +50 -0
- package/dist/core/registry/params/get_token.json +22 -0
- package/dist/core/registry/params/get_user_info.json +36 -0
- package/dist/core/registry/params/icoutcall.callIntercept.list.json +32 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.fuzzySearch.json +53 -0
- package/dist/core/registry/params/icoutcall.dialogFlow.getDialogFlowList.json +32 -0
- package/dist/core/registry/params/icoutcall.get.customer.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobdetail.json +39 -0
- package/dist/core/registry/params/icoutcall.get.jobproperties.json +39 -0
- package/dist/core/registry/params/icoutcall.history.seat.json +67 -0
- package/dist/core/registry/params/icoutcall.history.team.json +67 -0
- package/dist/core/registry/params/icoutcall.job.create.json +53 -0
- package/dist/core/registry/params/icoutcall.job.importcustomer.json +46 -0
- package/dist/core/registry/params/icoutcall.job.pause.json +39 -0
- package/dist/core/registry/params/icoutcall.job.readdcustomer.json +109 -0
- package/dist/core/registry/params/icoutcall.job.start.json +39 -0
- package/dist/core/registry/params/icoutcall.job.terminate.json +39 -0
- package/dist/core/registry/params/icoutcall.phone.getLineList.json +32 -0
- package/dist/core/registry/params/ipcc.calltask.add.auto.json +117 -0
- package/dist/core/registry/params/ipcc.calltask.add.byCustom.json +96 -0
- package/dist/core/registry/params/ipcc.calltask.add.byRelation.json +82 -0
- package/dist/core/registry/params/ipcc.calltask.add.toGroup.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.add.toStaff.json +89 -0
- package/dist/core/registry/params/ipcc.calltask.get.json +47 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.add.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.delete.json +40 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.get.json +54 -0
- package/dist/core/registry/params/ipcc.calltaskdetail.update.json +40 -0
- package/dist/core/registry/params/ipcc.customfield.template.get.json +47 -0
- package/dist/core/registry/params/ipcc.login.json +47 -0
- package/dist/core/registry/params/kefu.add.json +130 -0
- package/dist/core/registry/params/kefu.delete.json +39 -0
- package/dist/core/registry/params/kefu.update.json +137 -0
- package/dist/core/registry/params/kefu.v2.query.json +39 -0
- package/dist/core/registry/params/kefuGroup.add.json +39 -0
- package/dist/core/registry/params/kefuGroup.delete.json +39 -0
- package/dist/core/registry/params/kefuGroup.update.json +46 -0
- package/dist/core/registry/params/kefuGroup.updateKefus.json +46 -0
- package/dist/core/registry/params/merchant.account.login.json +53 -0
- package/dist/core/registry/params/merchant.changePassword.json +46 -0
- package/dist/core/registry/params/merchant.create.json +81 -0
- package/dist/core/registry/params/merchant.disable.json +39 -0
- package/dist/core/registry/params/merchant.enable.json +39 -0
- package/dist/core/registry/params/merchant.list.json +46 -0
- package/dist/core/registry/params/merchant.query.json +46 -0
- package/dist/core/registry/params/merchant.update.json +74 -0
- package/dist/core/registry/params/message.send.image.json +53 -0
- package/dist/core/registry/params/message.send.json +53 -0
- package/dist/core/registry/params/message.send.voice.json +53 -0
- package/dist/core/registry/params/message.sendFile.json +39 -0
- package/dist/core/registry/params/message.uploadFile.json +39 -0
- package/dist/core/registry/params/online.session.category.list.json +39 -0
- package/dist/core/registry/params/online.session.customfield.template.json +39 -0
- package/dist/core/registry/params/online.session.customfield.update.json +74 -0
- package/dist/core/registry/params/order.upload.json +40 -0
- package/dist/core/registry/params/push.msg.image.json +74 -0
- package/dist/core/registry/params/push.msg.richText.json +74 -0
- package/dist/core/registry/params/push.msg.text.json +74 -0
- package/dist/core/registry/params/receive.message.json +89 -0
- package/dist/core/registry/params/robot.data.getQuestionsByCategoryName.json +67 -0
- package/dist/core/registry/params/robot.data.knowledge.json +46 -0
- package/dist/core/registry/params/robot.data.kpoint.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardByEffectiveQuestion.json +46 -0
- package/dist/core/registry/params/robot.data.queryStandardQuestionList.json +46 -0
- package/dist/core/registry/params/robot.evaluation.comment.json +67 -0
- package/dist/core/registry/params/robot.evaluation.setting.json +46 -0
- package/dist/core/registry/params/robot.session.assist.json +74 -0
- package/dist/core/registry/params/robot.session.chat.json +74 -0
- package/dist/core/registry/params/robot.session.close.json +53 -0
- package/dist/core/registry/params/robot.session.commonQuestions.json +74 -0
- package/dist/core/registry/params/robot.session.create.json +81 -0
- package/dist/core/registry/params/robot.session.evaluation.json +67 -0
- package/dist/core/registry/params/robot.session.msgAssociate.json +46 -0
- package/dist/core/registry/params/robot.session.msgEvaluation.json +53 -0
- package/dist/core/registry/params/robot.session.msgPredict.json +74 -0
- package/dist/core/registry/params/robot.session.transfer.json +36 -0
- package/dist/core/registry/params/robot.session.uploadFile.json +32 -0
- package/dist/core/registry/params/robot.statistic.summary.json +53 -0
- package/dist/core/registry/params/robot.transfer.config.json +15 -0
- package/dist/core/registry/params/session.user.profile.json +39 -0
- package/dist/core/registry/params/settings.wxTemplates.json +46 -0
- package/dist/core/registry/params/smstask.create.json +53 -0
- package/dist/core/registry/params/smstask.status.json +39 -0
- package/dist/core/registry/params/staff.group.list.new.json +39 -0
- package/dist/core/registry/params/staff.group.members.new.json +46 -0
- package/dist/core/registry/params/staff.list.new.json +46 -0
- package/dist/core/registry/params/staff.login.json +47 -0
- package/dist/core/registry/params/statistic.overview.json +61 -0
- package/dist/core/registry/params/statistic.performance.json +82 -0
- package/dist/core/registry/params/statistic.salaryDetail.json +54 -0
- package/dist/core/registry/params/statistic.satisfaction.report.json +68 -0
- package/dist/core/registry/params/statistic.staffAttendance.json +75 -0
- package/dist/core/registry/params/statistic.staffquality.json +75 -0
- package/dist/core/registry/params/statistic.staffworklod.json +68 -0
- package/dist/core/registry/params/tag.all.json +32 -0
- package/dist/core/registry/params/tag.batchUpdate.json +32 -0
- package/dist/core/registry/params/tag.update.json +60 -0
- package/dist/core/registry/params/ticket.apply.json +46 -0
- package/dist/core/registry/params/ticket.batch.modify.json +53 -0
- package/dist/core/registry/params/ticket.create.json +151 -0
- package/dist/core/registry/params/ticket.filter.count.json +46 -0
- package/dist/core/registry/params/ticket.filter.list.json +46 -0
- package/dist/core/registry/params/ticket.finish.json +60 -0
- package/dist/core/registry/params/ticket.followers.update.json +60 -0
- package/dist/core/registry/params/ticket.list.json +74 -0
- package/dist/core/registry/params/ticket.log.json +39 -0
- package/dist/core/registry/params/ticket.modify.json +74 -0
- package/dist/core/registry/params/ticket.new.detail.json +39 -0
- package/dist/core/registry/params/ticket.reminder.json +60 -0
- package/dist/core/registry/params/ticket.reopen.json +67 -0
- package/dist/core/registry/params/ticket.reply.json +60 -0
- package/dist/core/registry/params/ticket.search.json +116 -0
- package/dist/core/registry/params/ticket.template.fields.json +39 -0
- package/dist/core/registry/params/ticket.template.list.json +39 -0
- package/dist/core/registry/params/ticket.transfer.json +74 -0
- package/dist/core/registry/params/track.event.set.json +32 -0
- package/dist/core/registry/params/wecom.robot.switch.json +43 -0
- package/dist/core/registry/params.js +84 -0
- package/dist/core/registry/specs.json +2172 -0
- package/dist/core/registry/validate.json +11 -0
- package/dist/core/registry/validation-overrides.js +98 -0
- package/dist/core/sanitize.js +24 -0
- package/dist/core/sign.js +21 -0
- package/dist/core/time.js +194 -0
- package/dist/core/validate.js +140 -0
- package/dist/output.js +263 -0
- package/dist/postinstall.js +178 -0
- package/dist/query/errors.js +395 -0
- package/dist/query/mask.js +368 -0
- package/dist/query/metric-catalog.js +85 -0
- package/dist/query/sandbox.js +544 -0
- package/dist/query/table-query.js +297 -0
- package/dist/query/time-shape.js +44 -0
- package/dist/schedule/install.js +377 -0
- package/dist/skill/check.js +429 -0
- package/dist/skill/detect.js +371 -0
- package/dist/skill/install.js +1202 -0
- package/dist/skill/paths.js +343 -0
- package/dist/skill/qiyu/SKILL.md +92 -0
- package/dist/skill/qiyu/errors.md +55 -0
- package/dist/skill/qiyu/forbidden.md +52 -0
- package/dist/skill/qiyu/metrics.md +65 -0
- package/dist/skill/qiyu/protocols.md +55 -0
- package/dist/skill/qiyu/setup.md +50 -0
- package/dist/skill/refs.js +379 -0
- package/dist/store/config.js +137 -0
- package/dist/store/duckdb.js +321 -0
- package/dist/store/index.js +18 -0
- package/dist/store/retry.js +36 -0
- package/dist/store/schema.js +419 -0
- package/dist/store/types.js +4 -0
- package/dist/sync/day.js +8 -0
- package/dist/sync/engine.js +818 -0
- package/dist/sync/rate-gate.js +95 -0
- package/dist/sync/registry.js +22 -0
- package/dist/sync/state.js +114 -0
- package/dist/sync/tasks/dim.js +186 -0
- package/dist/sync/tasks/knowledge.js +252 -0
- package/dist/sync/tasks/pace.js +66 -0
- package/dist/sync/tasks/pipeline.js +321 -0
- package/dist/sync/tasks/reports.js +448 -0
- package/dist/sync/tasks/sessions.js +188 -0
- package/dist/sync/tasks/tickets.js +219 -0
- package/dist/sync/types.js +1 -0
- package/dist/sync/windowing.js +56 -0
- package/package.json +52 -4
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// src/commands/store.ts
|
|
2
|
+
import { statSync, lstatSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import { resolveStoreConfig, currentStoreEnv } from '../store/config.js';
|
|
5
|
+
import { openStore } from '../store/index.js';
|
|
6
|
+
import { ok, fail, usageFail, sanitizeControlChars, setPretty } from '../output.js';
|
|
7
|
+
import { parseStoreFlags } from './store-flags.js';
|
|
8
|
+
import { GLOBAL_FLAGS, GLOBAL_FLAG_NAMES, prettyAfterSubcommand, REJECTED_FLAGS, STORE_FLAGS } from './spec-types.js';
|
|
9
|
+
/**
|
|
10
|
+
* 命令契约(F 组 D4):`skill check` 据此判断 skill 正文里的 `qiyu store …` 是否真的存在。
|
|
11
|
+
* `flags` 是**完整接受集**(含全局 flag),`--pretty` 同时在 `rejected` 里。
|
|
12
|
+
*/
|
|
13
|
+
export const STORE_SPECS = [
|
|
14
|
+
{
|
|
15
|
+
path: ['store', 'init'],
|
|
16
|
+
flags: [...GLOBAL_FLAGS, ...STORE_FLAGS],
|
|
17
|
+
positionals: [],
|
|
18
|
+
rejected: REJECTED_FLAGS,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
path: ['store', 'status'],
|
|
22
|
+
flags: [...GLOBAL_FLAGS, ...STORE_FLAGS],
|
|
23
|
+
positionals: [],
|
|
24
|
+
rejected: REJECTED_FLAGS,
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
const USAGE = `用法:qiyu store <子命令> [参数]
|
|
28
|
+
|
|
29
|
+
子命令:
|
|
30
|
+
store init [--db <path>] [--profile <name>] [--backend <name>]
|
|
31
|
+
初始化本地数据库(DuckDB)
|
|
32
|
+
store status [--db <path>] [--profile <name>] [--backend <name>]
|
|
33
|
+
查看数据库状态与各表行数
|
|
34
|
+
|
|
35
|
+
⚠️ 存储配置需手工编辑 ~/.config/qiyu/config.json(CLI 不会自动创建)。
|
|
36
|
+
也可用 --db / QIYU_DB_PATH 直接指定库路径。
|
|
37
|
+
`;
|
|
38
|
+
const KNOWN = new Set(GLOBAL_FLAG_NAMES);
|
|
39
|
+
export function thrownToExit(e) {
|
|
40
|
+
const err = e;
|
|
41
|
+
// usage 类错误(store 层预检抛出,如未知列/缺列/连接已关闭):归 usageFail 双通道、
|
|
42
|
+
// 退出码 2 —— 这是用法错误不是环境故障(M1 修:此前被下方排除条件挡住后落 internal)。
|
|
43
|
+
if (err.kind === 'usage') {
|
|
44
|
+
return usageFail(err.message ?? String(e), err.hint);
|
|
45
|
+
}
|
|
46
|
+
if (err.kind) {
|
|
47
|
+
return fail({ code: err.kind, kind: err.kind, retryable: err.retryable ?? false,
|
|
48
|
+
message: err.message ?? String(e), hint: err.hint });
|
|
49
|
+
}
|
|
50
|
+
// ⚠️ 无 kind 的错误分两类(spec §8.2 验收 41/54):
|
|
51
|
+
// ① DuckDB 拒绝打开非 DuckDB 文件 / 执行期错误 → 环境/数据问题,退出码 1、internal;
|
|
52
|
+
// ② 其余 → 用法错误,退出码 2(usageFail 双通道)。
|
|
53
|
+
const msg = err.message ?? String(e);
|
|
54
|
+
if (/not a valid DuckDB database/i.test(msg)) {
|
|
55
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
56
|
+
message: `无法打开 ${msg}`, hint: '该路径已存在且不是 DuckDB 数据库,请换 --db 路径' });
|
|
57
|
+
}
|
|
58
|
+
// ⚠️ open/执行期 errno 分流(R 组 R6):EACCES/EPERM/EROFS 是环境问题不是程序错误,
|
|
59
|
+
// 归因必须准(K 组 M3 只做了 lstat/statSync 层,这里补 open 期)。
|
|
60
|
+
if (/^(EACCES|EPERM|EROFS)/.test(msg)) {
|
|
61
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
62
|
+
message: `无法打开 ${msg}`, hint: '请检查路径写权限(磁盘只读或目录无写权限都会导致无法打开)' });
|
|
63
|
+
}
|
|
64
|
+
// ⚠️ internal 兜底(第四轮 I 组 H1/M8 修):spec §8.2 禁止把 DuckDB 英文原文裸透
|
|
65
|
+
// 给 Agent —— 必须中文包装 + 底层原文;hint 中性化(status 只读路径也会走到这里,
|
|
66
|
+
// 不能说「写入」)。
|
|
67
|
+
return fail({ code: 'internal', kind: 'internal', retryable: false,
|
|
68
|
+
message: `存储操作失败(底层:${msg})`, hint: '数据库执行期错误。若持续出现,请用 qiyu store status 检查数据库状态,或用 --db 指向其他数据库文件' });
|
|
69
|
+
}
|
|
70
|
+
export async function storeCommand(argv) {
|
|
71
|
+
setPretty(false); // G1:每条命令入口重置输出格式 —— 同一进程内多次 run(测试)不残留上一条命令的开关。
|
|
72
|
+
const [sub, ...rest] = argv;
|
|
73
|
+
if (sub === undefined || sub === '--help' || sub === '-h') {
|
|
74
|
+
// --help 不吞额外参数;--pretty 豁免(B 惯例)
|
|
75
|
+
const extra = rest.filter((a) => !KNOWN.has(a));
|
|
76
|
+
if (extra.length > 0)
|
|
77
|
+
return usageFail(`未知参数:${extra.join(' ')}。--help 不接受额外参数`);
|
|
78
|
+
process.stdout.write(USAGE);
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
if (sub !== 'init' && sub !== 'status') {
|
|
82
|
+
// store 级:--pretty 是合法**输出开关**,但它必须写在具体子命令之后(`store init --pretty`)。
|
|
83
|
+
// 写在这里时没有任何子命令可修饰 —— 给出正确写法,而不是说成"未知子命令"(B 惯例)。
|
|
84
|
+
if (sub === '--pretty') {
|
|
85
|
+
const unknown = rest.filter((a) => a.startsWith('-') && !KNOWN.has(a));
|
|
86
|
+
const positional = rest.filter((a) => !a.startsWith('-'));
|
|
87
|
+
if (unknown.length + positional.length > 0) {
|
|
88
|
+
return usageFail(`未知参数:${[...unknown, ...positional].join(' ')}。--pretty 不接受额外参数`);
|
|
89
|
+
}
|
|
90
|
+
if (rest.includes('--help') || rest.includes('-h')) {
|
|
91
|
+
process.stdout.write(USAGE);
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
return usageFail(prettyAfterSubcommand('store'));
|
|
95
|
+
}
|
|
96
|
+
return usageFail(`未知子命令:${sub}\n可用子命令:init、status`);
|
|
97
|
+
}
|
|
98
|
+
// 子命令级:**先 parseStoreFlags**(消费带值 flag 的值位置),再判未知参数(spec §7.3/验收 46)。
|
|
99
|
+
// ⚠️ 次序不能反:若先过滤「未知 token」再 parseStoreFlags,`store init --db <path>` 的
|
|
100
|
+
// --db 及其值会被当未知参数误拒(v1 计划的实测缺陷,D/F 组独立发现)。
|
|
101
|
+
if (rest.includes('--help') || rest.includes('-h')) {
|
|
102
|
+
const rawRest = rest.filter((a) => !KNOWN.has(a));
|
|
103
|
+
if (rawRest.length > 0)
|
|
104
|
+
return usageFail(`未知参数:${rawRest.join(' ')}`);
|
|
105
|
+
process.stdout.write(USAGE);
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
// G1:--pretty 已实现 —— 命令行合法即开启缩进输出(原拒绝点)。
|
|
109
|
+
if (rest.includes('--pretty'))
|
|
110
|
+
setPretty(true);
|
|
111
|
+
let flags;
|
|
112
|
+
try {
|
|
113
|
+
flags = parseStoreFlags(rest);
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
return usageFail(sanitizeControlChars(e.message));
|
|
117
|
+
}
|
|
118
|
+
// parseStoreFlags 之后剩下的 rest 才是真正的未知参数(带值 flag 的值位已被消费)
|
|
119
|
+
if (flags.rest.length > 0)
|
|
120
|
+
return usageFail(`未知参数:${flags.rest.join(' ')}`);
|
|
121
|
+
let resolved;
|
|
122
|
+
try {
|
|
123
|
+
resolved = resolveStoreConfig({ flags: { db: flags.db, profile: flags.profile, backend: flags.backend },
|
|
124
|
+
env: currentStoreEnv() });
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
return usageFail(sanitizeControlChars(e.message));
|
|
128
|
+
}
|
|
129
|
+
const { config, configPath, configExists } = resolved;
|
|
130
|
+
if (sub === 'init') {
|
|
131
|
+
// 文件前置检查(spec §7.1 步骤 2;验收 41)
|
|
132
|
+
// ⚠️ lstat 异常按 errno 分流(K 组 M3):只有 ENOENT 才是「不存在 → DuckDB 会创建」;
|
|
133
|
+
// EACCES/ELOOP/ENAMETOOLONG 等是**可访问性问题**,直接归因给 Agent——否则 DuckDB
|
|
134
|
+
// 的英文底层错误经 internal 兜底透出,Agent 拿不到「检查权限」这个可操作归因。
|
|
135
|
+
let st;
|
|
136
|
+
try {
|
|
137
|
+
st = lstatSync(config.path);
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
const code = e.code;
|
|
141
|
+
// ⚠️ ENOTDIR 与 ENOENT 同样放行(测试为权威的偏离声明:brief 原文只放行 ENOENT,
|
|
142
|
+
// 但 `--db blocker/sub/q.db`(blocker 是普通文件)lstat 抛 ENOTDIR,会被这里的
|
|
143
|
+
// 「无法访问」拦截,永远到不了「无法创建父目录」分支 —— 父路径是普通文件正是
|
|
144
|
+
// 验收 41 的注入场景)。两者都交给下方 mkdirSync 真正失败时归因。
|
|
145
|
+
if (code !== 'ENOENT' && code !== 'ENOTDIR') {
|
|
146
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
147
|
+
message: `无法访问 ${config.path}:${e instanceof Error ? e.message : String(e)}`,
|
|
148
|
+
hint: '请检查路径权限(父目录无执行权限/链接循环/路径过长都会导致无法访问)' });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (st !== undefined && st.isFile() === false && st.isDirectory() === true) {
|
|
152
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
153
|
+
message: `不是普通文件:${config.path}`, hint: '--db 指向的不是普通文件,请改成一个数据库文件路径' });
|
|
154
|
+
}
|
|
155
|
+
if (st !== undefined && st.isFile() === false && st.isSymbolicLink() === false && st.isDirectory() === false) {
|
|
156
|
+
// 其它特殊文件(设备/套接字等)同样拒绝
|
|
157
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
158
|
+
message: `不是普通文件:${config.path}`, hint: '--db 指向的不是普通文件,请改成一个数据库文件路径' });
|
|
159
|
+
}
|
|
160
|
+
if (st !== undefined && st.isSymbolicLink() === true) {
|
|
161
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
162
|
+
message: `目标是符号链接:${config.path}`, hint: '--db 目标是符号链接,请给出真实路径' });
|
|
163
|
+
}
|
|
164
|
+
// 父目录不存在 → mkdirSync(recursive);失败 → 退出码 1、message 含路径与底层原因
|
|
165
|
+
const parent = dirname(config.path);
|
|
166
|
+
try {
|
|
167
|
+
mkdirSync(parent, { recursive: true });
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
// ⚠️ message 同时含父目录与目标库完整路径(测试为权威的偏离声明:brief 原文
|
|
171
|
+
// 只含 parent,但验收 41 断言 message 含完整 --db 路径 —— Agent 拿到错误要能
|
|
172
|
+
// 对回自己传的参数)。
|
|
173
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
174
|
+
message: `无法创建父目录 ${parent}(目标文件 ${config.path}):${e instanceof Error ? e.message : String(e)}`,
|
|
175
|
+
hint: '请检查父目录权限,或用 --db 换一个可写的路径' });
|
|
176
|
+
}
|
|
177
|
+
// ⚠️ 魔数在偏移 8(前 8 字节是块校验和)——不自写魔数检查,交给 DuckDB 打开时校验:
|
|
178
|
+
// 非 DuckDB 文件打开即抛 "not a valid DuckDB database",由 thrownToExit 映射。
|
|
179
|
+
let store;
|
|
180
|
+
try {
|
|
181
|
+
store = await openStore(config, 'rw');
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
return thrownToExit(e);
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await store.init();
|
|
188
|
+
const tables = await store.tables();
|
|
189
|
+
const sv = await store.query('SELECT COALESCE(MAX(version),0) AS v FROM schema_version');
|
|
190
|
+
return ok({ path: config.path, backend: config.backend, config_path: configPath,
|
|
191
|
+
config_exists: configExists,
|
|
192
|
+
schema_version: Number(sv.rows[0]?.v ?? 0),
|
|
193
|
+
tables });
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
return thrownToExit(e);
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
await store.close();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// status
|
|
203
|
+
// ⚠️ statSync 异常按 errno 分流(K 组 M6):EACCES 误报「请先运行 init」会把
|
|
204
|
+
// 只读路径的权限错误引导向写操作(init),Agent 必然再失败一次 —— 归因必须准。
|
|
205
|
+
let st;
|
|
206
|
+
try {
|
|
207
|
+
st = statSync(config.path);
|
|
208
|
+
}
|
|
209
|
+
catch (e) {
|
|
210
|
+
const code = e.code;
|
|
211
|
+
if (code === 'ENOENT') {
|
|
212
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
213
|
+
message: `数据库文件不存在:${config.path}`, hint: '数据库尚未初始化。请先运行:qiyu store init' });
|
|
214
|
+
// ⚠️ 修复批 2 裁定:D 交付后本 hint **不改指** `qiyu sync run`(设计稿 §9.5 的
|
|
215
|
+
// 「D 交付后改指 sync run」不再采纳)——「初始化存储」的最小命令是 store init,
|
|
216
|
+
// 且 sync status/verify 是只读命令,指它们跑一次写操作并非必要。
|
|
217
|
+
// 同步侧(sync status/verify/install、sync run 的 storeInitFail)与 C1 同口径。
|
|
218
|
+
}
|
|
219
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
220
|
+
message: `无法访问 ${config.path}:${e instanceof Error ? e.message : String(e)}`,
|
|
221
|
+
hint: '请检查路径权限(status 是只读命令,不需要写权限)' });
|
|
222
|
+
}
|
|
223
|
+
if (!st.isFile()) {
|
|
224
|
+
// ⚠️ P 组 P-H2:目录成因**不能**引导向 store init(init 也拒绝目录 → 死循环)——
|
|
225
|
+
// 独立 hint,与「不存在」成因分形。
|
|
226
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
227
|
+
message: `不是普通文件:${config.path}`,
|
|
228
|
+
hint: '--db 指向的是目录而非数据库文件,请给出 .duckdb 数据库文件路径' });
|
|
229
|
+
}
|
|
230
|
+
let store;
|
|
231
|
+
try {
|
|
232
|
+
store = await openStore(config, 'ro');
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
return thrownToExit(e);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
const names = await store.tables();
|
|
239
|
+
if (!names.includes('schema_version')) {
|
|
240
|
+
return fail({ code: 'store_not_initialized', kind: 'store_not_initialized', retryable: false,
|
|
241
|
+
message: `数据库结构不完整(缺表):${config.path}`,
|
|
242
|
+
hint: names.length > 0 ? '目标数据库不是本 CLI 创建的,请换 --db 路径' : '请运行:qiyu store init' });
|
|
243
|
+
}
|
|
244
|
+
const tables = [];
|
|
245
|
+
for (const n of names)
|
|
246
|
+
tables.push({ name: n, rows: await store.count(n) });
|
|
247
|
+
const sv = await store.query('SELECT COALESCE(MAX(version),0) AS v FROM schema_version');
|
|
248
|
+
return ok({ path: config.path, backend: config.backend, config_path: configPath,
|
|
249
|
+
config_exists: configExists,
|
|
250
|
+
schema_version: Number(sv.rows[0]?.v ?? 0), tables });
|
|
251
|
+
}
|
|
252
|
+
catch (e) {
|
|
253
|
+
return thrownToExit(e);
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
await store.close();
|
|
257
|
+
}
|
|
258
|
+
}
|