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,395 @@
|
|
|
1
|
+
// src/query/errors.ts
|
|
2
|
+
// DuckDB 原生错误 → 中文 QueryFailure(spec §十一 分派表)—— 查询层唯一的错误翻译出口。
|
|
3
|
+
//
|
|
4
|
+
// 分工(plan Task 3/5 裁决):
|
|
5
|
+
// · `query/sandbox.ts` 只抛**已分类**的错误(带 kind,部分带 code/hint/retryable);
|
|
6
|
+
// · 本文件负责把**执行期原生错误**(无 kind 的英文原文)细化成中文 + 表/列候选;
|
|
7
|
+
// · 命令层统一走 `thrownToQueryExit`,不自己拼 fail/usageFail。
|
|
8
|
+
//
|
|
9
|
+
// 三条硬规则:
|
|
10
|
+
// 1. **已带 kind 的错误原样透传** —— 不重新分类。sandbox 的 usage/sandbox_blocked/
|
|
11
|
+
// query_timeout/store_locked 都是有意分好的,这里再猜一次只会把语义改坏。
|
|
12
|
+
// 唯一例外(裁决 R9):sandbox 把原生错误挂在 `cause` 上时,kind/code 仍以 sandbox
|
|
13
|
+
// 为准,但「表/列不存在」的中文消息与候选从 cause 原文补算(见 classifyDuckDbError ①)。
|
|
14
|
+
// 2. **不裸透英文**:所有 message 以中文开头,原生原文放在括号/冒号后(spec §十一)。
|
|
15
|
+
// 3. **候选不硬凑**:DuckDB 自己的 `Did you mean "x"` 可能是系统表噪音(探针 15:
|
|
16
|
+
// 未知表时给出 `pg_constraint`);以 `TABLES` 计算出的候选为先,原生候选只补位。
|
|
17
|
+
//
|
|
18
|
+
// ⚠️ 分层约束(spec §5.1):不 import `sync/`、`commands/`。`output.ts` 是 CLI 输出层
|
|
19
|
+
// (信封与退出码),不是命令层,可以 import。
|
|
20
|
+
import { fail, sanitizeMessage, stripSqlEcho } from '../output.js';
|
|
21
|
+
/** 候选上限(spec §十一 / brief):超过即截断并加「等」。 */
|
|
22
|
+
const CANDIDATE_LIMIT = 5;
|
|
23
|
+
/** 编辑距离上限:只接受「打错一两个字符」的输入,再远就不是同一个名字了。 */
|
|
24
|
+
const MAX_DISTANCE = 2;
|
|
25
|
+
function asClassified(e) {
|
|
26
|
+
if (typeof e !== 'object' || e === null)
|
|
27
|
+
return null;
|
|
28
|
+
const o = e;
|
|
29
|
+
if (typeof o.kind !== 'string' || o.kind === '')
|
|
30
|
+
return null;
|
|
31
|
+
return {
|
|
32
|
+
message: typeof o.message === 'string' ? o.message : String(e),
|
|
33
|
+
kind: o.kind,
|
|
34
|
+
code: typeof o.code === 'string' ? o.code : undefined,
|
|
35
|
+
hint: typeof o.hint === 'string' ? o.hint : undefined,
|
|
36
|
+
retryable: typeof o.retryable === 'boolean' ? o.retryable : undefined,
|
|
37
|
+
cause: o.cause,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function errorText(e) {
|
|
41
|
+
return e instanceof Error ? e.message : String(e);
|
|
42
|
+
}
|
|
43
|
+
/** Levenshtein 编辑距离(两行滚动数组;名字都很短,无需优化)。 */
|
|
44
|
+
function levenshtein(a, b) {
|
|
45
|
+
if (a === b)
|
|
46
|
+
return 0;
|
|
47
|
+
if (a.length === 0)
|
|
48
|
+
return b.length;
|
|
49
|
+
if (b.length === 0)
|
|
50
|
+
return a.length;
|
|
51
|
+
let prev = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
52
|
+
for (let i = 1; i <= a.length; i++) {
|
|
53
|
+
const cur = [i];
|
|
54
|
+
for (let j = 1; j <= b.length; j++) {
|
|
55
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
56
|
+
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost);
|
|
57
|
+
}
|
|
58
|
+
prev = cur;
|
|
59
|
+
}
|
|
60
|
+
return prev[b.length];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 从候选名字里挑出与输入相关的(spec §十一「无候选时从 TABLES 计算」)。
|
|
64
|
+
*
|
|
65
|
+
* 排序规则:前缀匹配优先(`sess` → `sessions`),其次编辑距离 ≤ 2(`sessons` → `sessions`)。
|
|
66
|
+
* 大小写不敏感比较(DuckDB 标识符大小写不敏感,探针 15),返回**定义时的大小写**。
|
|
67
|
+
* 完全不相干的输入返回空数组 —— 不硬凑候选比给一堆噪音更有用。
|
|
68
|
+
*/
|
|
69
|
+
export function suggestNames(input, names, max = CANDIDATE_LIMIT) {
|
|
70
|
+
const needle = input.toLowerCase();
|
|
71
|
+
if (needle === '' || max <= 0)
|
|
72
|
+
return [];
|
|
73
|
+
const scored = [];
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
for (const name of names) {
|
|
76
|
+
const lower = name.toLowerCase();
|
|
77
|
+
if (seen.has(lower))
|
|
78
|
+
continue; // 名字池里重复(列名跨表重名)
|
|
79
|
+
seen.add(lower);
|
|
80
|
+
const distance = levenshtein(needle, lower);
|
|
81
|
+
// 仅大小写不同的精确同名(edge F6):`SESSIONS` → `sessions`。此前 `lower === needle`
|
|
82
|
+
// 直接 continue 把「只差大小写」的正确答案排除了,Agent 反而拿不到候选;而 DuckDB
|
|
83
|
+
// 标识符大小写不敏感(同一写法在 --sql 通道能跑通),必须把该名字本身作为候选。
|
|
84
|
+
if (lower === needle) {
|
|
85
|
+
if (name !== input)
|
|
86
|
+
scored.push({ name, rank: -1, distance: 0 });
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const isPrefix = lower.startsWith(needle) || needle.startsWith(lower);
|
|
90
|
+
if (isPrefix)
|
|
91
|
+
scored.push({ name, rank: 0, distance });
|
|
92
|
+
else if (distance <= MAX_DISTANCE)
|
|
93
|
+
scored.push({ name, rank: 1, distance });
|
|
94
|
+
}
|
|
95
|
+
// 同 rank 内距离近者优先;再按名字排序保证输出稳定(不依赖 TABLES 的定义顺序)。
|
|
96
|
+
scored.sort((x, y) => x.rank - y.rank || x.distance - y.distance || x.name.localeCompare(y.name));
|
|
97
|
+
return scored.slice(0, max).map((s) => s.name);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 原生候选:`Did you mean "x"?`、`Candidate bindings: "a", "b"` 或
|
|
101
|
+
* `Candidate tables: "t"`(探针 15 + 审查实测)。
|
|
102
|
+
* ⚠️ 容忍双冒号变体 `Candidate bindings: : "payload"`(DuckDB 1.5.6-r.1 实测)。
|
|
103
|
+
*/
|
|
104
|
+
function nativeCandidates(msg) {
|
|
105
|
+
const didYouMean = /Did you mean "([^"]+)"/.exec(msg);
|
|
106
|
+
if (didYouMean)
|
|
107
|
+
return [didYouMean[1]];
|
|
108
|
+
const bindings = /Candidate (?:bindings|tables):\s*:?\s*((?:"[^"]+"(?:,\s*)?)+)/.exec(msg);
|
|
109
|
+
if (bindings)
|
|
110
|
+
return [...bindings[1].matchAll(/"([^"]+)"/g)].map((m) => m[1]);
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 候选合并:`suggestNames` 的结果在前(来自 TABLES,是用户真正可能想要的),
|
|
115
|
+
* 原生候选只补位 —— 原生候选可能是系统表(`pg_constraint`),不能无条件排第一。
|
|
116
|
+
*
|
|
117
|
+
* ⚠️ 原生候选必须**再过一次相关性闸门**(正确性 P2):DuckDB 的 binder 会给出与自己
|
|
118
|
+
* 无关的候选(实测删掉 `robot_summary` 后 → `候选:staff_group_members`),照单全收会
|
|
119
|
+
* 把 Agent 推向另一张表、且与 `TABLES` 无关。`suggestNames` 用的同一阈值(前缀或距离 ≤2)
|
|
120
|
+
* 在这里同样适用 —— 不硬凑(spec §十一)。过滤后为空则退回「用 db tables 查看」。
|
|
121
|
+
*/
|
|
122
|
+
function mergeCandidates(primary, native) {
|
|
123
|
+
const out = [];
|
|
124
|
+
const seen = new Set();
|
|
125
|
+
for (const n of [...primary, ...native]) {
|
|
126
|
+
// 大小写不敏感去重:DuckDB 标识符大小写不敏感,`Sessions` 与 `sessions` 是同一个名字。
|
|
127
|
+
const key = n.toLowerCase();
|
|
128
|
+
if (seen.has(key))
|
|
129
|
+
continue;
|
|
130
|
+
seen.add(key);
|
|
131
|
+
out.push(n);
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/** 「候选:a、b、c」(超过上限截断并加「等」,spec §十一)。 */
|
|
136
|
+
function candidateHint(candidates) {
|
|
137
|
+
const shown = candidates.slice(0, CANDIDATE_LIMIT);
|
|
138
|
+
const suffix = candidates.length > CANDIDATE_LIMIT ? '等' : '';
|
|
139
|
+
return `候选:${shown.join('、')}${suffix}`;
|
|
140
|
+
}
|
|
141
|
+
// —— 表/列不存在的四种原生形态(探针 15 + 审查实测,1.5.6-r.1)——
|
|
142
|
+
// ① `Catalog Error: Table with name sessons does not exist!`
|
|
143
|
+
// ② `Binder Error: Referenced table "sessons" not found!`(限定名 `sessons.paylod`)
|
|
144
|
+
// ③ `Binder Error: Table "sessions" does not have a column named "paylod"`
|
|
145
|
+
// ④ `Binder Error: Referenced column "paylod" was not found because the FROM clause is missing`
|
|
146
|
+
// 四条都锚定错误类别前缀(`Binder Error:` / `Catalog Error:`)——不能省(审查 Minor 20):
|
|
147
|
+
// DuckDB 的错误消息会**回显 SQL 原文**(`LINE 1: …`),Parser Error 的报错里若恰好含
|
|
148
|
+
// `Referenced column "x" not found` 字样,无锚的正则会把「语法错」误判成「列不存在」。
|
|
149
|
+
// 实测:`SELECT 1 WHERE x = Referenced column "zzz" not found` → 原文是 Parser Error。
|
|
150
|
+
const TABLE_NAME_RE = /Catalog Error: Table with name ([^\s]+) does not exist/;
|
|
151
|
+
const TABLE_REF_RE = /Binder Error: Referenced table "([^"]+)" not found/;
|
|
152
|
+
const COLUMN_NAMED_RE = /Binder Error: Table "[^"]+" does not have a column named "([^"]+)"/;
|
|
153
|
+
const COLUMN_NAME_RE = /Binder Error: Referenced column "([^"]+)" (?:was )?not found/;
|
|
154
|
+
/** 表不存在的中文消息 + 候选 hint(原生分支与 cause 补算分支共用,裁决 R9)。 */
|
|
155
|
+
function tableNotFound(name, msg, tables) {
|
|
156
|
+
const candidates = mergeCandidates(
|
|
157
|
+
// 多要一个:只有知道「还有第 6 个」才能决定 hint 末尾加不加「等」。
|
|
158
|
+
suggestNames(name, tables.map((t) => t.name), CANDIDATE_LIMIT + 1),
|
|
159
|
+
// 原生候选(正确性 P2)同样过相关性闸门:DuckDB 的 `Candidate bindings/tables`
|
|
160
|
+
// 可能给出与本输入毫无关系的名字(实测 `robot_summary` → `staff_group_members`),
|
|
161
|
+
// 直接当「候选」展示会把 Agent 推向另一张表。只保留通过 `suggestNames` 阈值的那部分。
|
|
162
|
+
suggestNames(name, nativeCandidates(msg), CANDIDATE_LIMIT + 1));
|
|
163
|
+
return {
|
|
164
|
+
message: `表 ${name} 不存在`,
|
|
165
|
+
hint: candidates.length > 0 ? candidateHint(candidates) : '用 `qiyu db tables` 查看可用表',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** 列不存在的中文消息 + 候选 hint(候选池 = 全部表的全部列名:SQL 不告诉我们 FROM 哪张表)。 */
|
|
169
|
+
function columnNotFound(name, msg, tables) {
|
|
170
|
+
const allColumns = [...new Set(tables.flatMap((t) => t.columns.map((c) => c.name)))];
|
|
171
|
+
const candidates = mergeCandidates(suggestNames(name, allColumns, CANDIDATE_LIMIT + 1),
|
|
172
|
+
// 同 tableNotFound:原生候选须过相关性闸门(正确性 P2)。
|
|
173
|
+
suggestNames(name, nativeCandidates(msg), CANDIDATE_LIMIT + 1));
|
|
174
|
+
return {
|
|
175
|
+
message: `列 ${name} 不存在`,
|
|
176
|
+
hint: candidates.length > 0
|
|
177
|
+
? candidateHint(candidates)
|
|
178
|
+
: '用 `qiyu db describe <表>` 查看列清单',
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 原文 → 表/列不存在的 { message, hint }(四种形态统一入口)。
|
|
183
|
+
* 不是表/列不存在时返回 null —— 调用方自行决定后续分派。
|
|
184
|
+
*/
|
|
185
|
+
function missingNameFailure(msg, tables) {
|
|
186
|
+
const byTableName = TABLE_NAME_RE.exec(msg) ?? TABLE_REF_RE.exec(msg);
|
|
187
|
+
if (byTableName)
|
|
188
|
+
return tableNotFound(byTableName[1], msg, tables);
|
|
189
|
+
const byColumn = COLUMN_NAMED_RE.exec(msg) ?? COLUMN_NAME_RE.exec(msg);
|
|
190
|
+
if (byColumn)
|
|
191
|
+
return columnNotFound(byColumn[1], msg, tables);
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 原生错误 → QueryFailure(spec §十一 分派表)。
|
|
196
|
+
*
|
|
197
|
+
* 分派顺序有意义:先透传已分类错误,再按原文形态分流。
|
|
198
|
+
* ⚠️ layer2 的判定与 sandbox 对齐(`sandbox.ts` 的收窄裁决):只认「配置禁用了文件系统」
|
|
199
|
+
* 的措辞,**不认裸 `Permission Error`** —— 读库文件本身的 OS 权限错误也带这个前缀,
|
|
200
|
+
* 误报成「已被只读沙箱拦截」会把环境问题(退出码 1)说成用法问题(退出码 2)。
|
|
201
|
+
* `retryable` 一律 false:这些都是确定性错误,重试同一个查询只会得到同样的结果。
|
|
202
|
+
*/
|
|
203
|
+
export function classifyDuckDbError(e, tables) {
|
|
204
|
+
// ① 已分类错误原样透传(sandbox 的分工:它分类,这里不重新分类)。
|
|
205
|
+
// ⚠️ 唯一例外(裁决 R9):sandbox 在 prepare 期把原生 Catalog/Binder 错误包成
|
|
206
|
+
// `usage`/`query_error` 并把原文挂在 `cause` 上 —— kind/code 仍以 sandbox 为准,
|
|
207
|
+
// 但「表/列不存在」的中文消息与候选要从 cause 原文补算,否则候选永远算不出来
|
|
208
|
+
//(Task 8 验收「未知表 → 候选」)。cause 无关时一律不动,message 保持 sandbox 原文。
|
|
209
|
+
const classified = asClassified(e);
|
|
210
|
+
if (classified) {
|
|
211
|
+
const f = {
|
|
212
|
+
code: classified.code ?? classified.kind,
|
|
213
|
+
kind: classified.kind,
|
|
214
|
+
retryable: classified.retryable ?? false,
|
|
215
|
+
message: classified.message,
|
|
216
|
+
};
|
|
217
|
+
if (classified.hint !== undefined)
|
|
218
|
+
f.hint = classified.hint;
|
|
219
|
+
if (classified.cause !== undefined && classified.cause !== null) {
|
|
220
|
+
const causeMsg = errorText(classified.cause);
|
|
221
|
+
// 表/列不存在只对 usage 补算(sandbox 的 prepare 期包装;Minor #6 守卫)。
|
|
222
|
+
if (classified.kind === 'usage') {
|
|
223
|
+
const refined = missingNameFailure(causeMsg, tables);
|
|
224
|
+
if (refined) {
|
|
225
|
+
f.message = refined.message;
|
|
226
|
+
f.hint = refined.hint;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// R11:sandbox 的执行期 catch 把 OOM 包成 internal 且(修复后)挂 cause ——
|
|
230
|
+
// message 保持 sandbox 原文,只补可操作 hint。
|
|
231
|
+
if (classified.kind === 'internal' && /Out of Memory Error/i.test(causeMsg)) {
|
|
232
|
+
f.hint = '缩小时间范围或加过滤条件';
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return f;
|
|
236
|
+
}
|
|
237
|
+
const msg = errorText(e);
|
|
238
|
+
// ② layer2 拦截(文件/网络):spec §十一 行 4。
|
|
239
|
+
// ⚠️ 与 sandbox.ts 的收窄裁决对齐:裸 `Permission Error` 不算(可能是库文件的 OS
|
|
240
|
+
// 权限问题),只认「文件系统操作被配置禁用」的两种措辞。
|
|
241
|
+
if (/file system operations are disabled|Cannot access file/i.test(msg)) {
|
|
242
|
+
return {
|
|
243
|
+
kind: 'usage', code: 'sandbox_blocked', retryable: false,
|
|
244
|
+
message: '该语句尝试访问文件或网络,已被只读沙箱拦截',
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
// ③ 表/列不存在:spec §十一 行 5(四种原生形态统一走 missingNameFailure)。
|
|
248
|
+
const missing = missingNameFailure(msg, tables);
|
|
249
|
+
if (missing) {
|
|
250
|
+
return { kind: 'usage', code: 'query_error', retryable: false, ...missing };
|
|
251
|
+
}
|
|
252
|
+
// ④ SQL 语法错:spec §十一 行 6 —— 中文包装 + 底层原文(原文自带位置信息,保留全部行)。
|
|
253
|
+
if (/Parser Error/i.test(msg)) {
|
|
254
|
+
return {
|
|
255
|
+
kind: 'usage', code: 'query_error', retryable: false,
|
|
256
|
+
message: `SQL 语法错误:${msg}`,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
// ⑤ 未绑参:spec §十一 行 6(「SQL 语法错 / 未绑参」同归 usage/query_error)。
|
|
260
|
+
if (/Expected \d+ parameters/i.test(msg)) {
|
|
261
|
+
return {
|
|
262
|
+
kind: 'usage', code: 'query_error', retryable: false,
|
|
263
|
+
message: `SQL 参数个数不匹配:${msg}`,
|
|
264
|
+
hint: '请把过滤条件写成字面量;参数化查询仅供 CLI 内部代码使用',
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
// ⑥ 其余 Binder / Catalog 错误(函数不存在、类型不匹配等):同样是 SQL 写错了。
|
|
268
|
+
// ⚠️ 不锚定行首:sandbox 实测会包一层前缀(`Failed to extract statements: Binder Error: …`)。
|
|
269
|
+
if (/(Binder|Catalog) Error/i.test(msg)) {
|
|
270
|
+
return {
|
|
271
|
+
kind: 'usage', code: 'query_error', retryable: false,
|
|
272
|
+
message: `SQL 无法执行:${msg}`,
|
|
273
|
+
hint: '请检查表名、列名与 SQL 语法',
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
// ⑦ 内存不足:环境问题,不是 SQL 写错 —— internal + 可操作 hint。
|
|
277
|
+
if (/Out of Memory Error/i.test(msg)) {
|
|
278
|
+
return {
|
|
279
|
+
kind: 'internal', code: 'internal', retryable: false,
|
|
280
|
+
message: `查询内存不足(${msg})`,
|
|
281
|
+
hint: '缩小时间范围或加过滤条件',
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
// ⑧ 其余执行期错误 → internal,中文包装 + 底层原文(不裸透,spec §十一 末行)。
|
|
285
|
+
return {
|
|
286
|
+
kind: 'internal', code: 'internal', retryable: false,
|
|
287
|
+
message: `查询执行失败(底层:${msg})`,
|
|
288
|
+
hint: '请检查查询条件;若反复出现请报告此问题',
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* 命令层统一出口:分类后写信封并返回退出码。
|
|
293
|
+
*
|
|
294
|
+
* usage 错误必须**双通道**:stderr 给人读的纯文本 + stdout 的 `{ok:false,error}` JSON
|
|
295
|
+
* (与 `output.usageFail` 同一契约)。但 `usageFail` 把 code 固定成 `'usage'`,会丢掉
|
|
296
|
+
* spec §十一 分派表的 code 列(`query_error` / `sandbox_blocked`)——而命令层测试
|
|
297
|
+
* (plan Task 8「多语句 / COPY / ATTACH → sandbox_blocked」)正是按 code 断言的。
|
|
298
|
+
* ⚠️ **这是有意偏离 `usageFail`,不是遗漏**(controller 裁决 R9 concern #2 确认保留):
|
|
299
|
+
* 故这里复刻 usageFail 的 stderr 写法,stdout 交给 `fail()` 保留真实 code;`fail()`
|
|
300
|
+
* 对 kind='usage' 同样返回退出码 2。其余 kind 直接 `fail({...})`(退出码 1;
|
|
301
|
+
* `query_timeout` 不设特殊分支)。若将来 `output.usageFail` 支持自定义 code,可改回复用它。
|
|
302
|
+
*/
|
|
303
|
+
export function thrownToQueryExit(e, tables, noMask = false) {
|
|
304
|
+
return emitQueryFailure(classifyDuckDbError(e, tables), noMask);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* 已分类失败的唯一出口(`thrownToQueryExit` 与 `--table` 漂移识别共用)。
|
|
308
|
+
* usage 双通道:stderr 纯文本 + stdout JSON(见上一段的说明)。
|
|
309
|
+
*
|
|
310
|
+
* `noMask` 只影响**转换错误回显里的值**(见下);默认(false)脱敏。
|
|
311
|
+
*/
|
|
312
|
+
export function emitQueryFailure(f, noMask = false) {
|
|
313
|
+
// security S3/S4:DuckDB 原文与调用方输入都可能带终端控制字符,且 Parser/Binder 错误
|
|
314
|
+
// 会回显整行 SQL(其中可能含调用方写进 SQL 的 PII)。在**唯一出口**统一净化 + 裁掉
|
|
315
|
+
// SQL 回显 —— 逐调用点补会漏(security 实测同文件内两种行为并存)。
|
|
316
|
+
//
|
|
317
|
+
// ⚠️ 转换错误还会回显**表里的真实值**(CodeRabbit 终审 major):实测
|
|
318
|
+
// `SELECT CAST(userPhone AS INT) FROM sessions` →
|
|
319
|
+
// `Conversion Error: Could not convert string '13800138000' to INT32 …`
|
|
320
|
+
// —— 那是查询结果行之外的一条独立泄漏通道(脱敏只作用于 rows,错误消息不经过它)。
|
|
321
|
+
//
|
|
322
|
+
// ⚠️ **不止单引号一种句式**(安全审查 Major,2026-10-20 用随包 `libduckdb.dylib` 的
|
|
323
|
+
// 格式串 + 实跑核对):同一个值会以不同包装回显,只挡一种就漏其余——
|
|
324
|
+
// · `Could not convert string '…' to INT32` (单引号,INT/BIGINT)
|
|
325
|
+
// · `Could not convert string "…" to DECIMAL(5,2)` (双引号,实测)
|
|
326
|
+
// · `Could not parse string "…" according to format specifier "%Y"`(strptime,实测)
|
|
327
|
+
// · `Casting value "…" to type … failed: value is out of range!`
|
|
328
|
+
// · `Malformed JSON … Input: "<payload 原文片段>"`(payload 是 PII 完整副本,实测)
|
|
329
|
+
// · `Failed to cast value: …` / `Failed to cast value to decimal: …`
|
|
330
|
+
// 只替换**值**的位置,保留错误类别与列名等可读信息(usage 错误里的引号标识符不受影响)。
|
|
331
|
+
// ⚠️ **值内部可能含未转义的引号**(CodeRabbit 复审 Major,2026-10-20 实测):
|
|
332
|
+
// payload 是 JSON,DuckDB 回显时**不转义**内层引号——
|
|
333
|
+
// `Could not convert string "{"userName":"张三","x":1}" to DECIMAL(5,2)`
|
|
334
|
+
// 若按「引号到下一个引号」匹配,只会吃掉 `"{"`,剩余 `userName":"张三"...` 原样留在
|
|
335
|
+
// 消息里(strptime 更会在下一行再回显一遍原值)。故必须**锚定固定后缀**、贪婪匹配:
|
|
336
|
+
// · `… string <quote>…<quote> to …` / `… according to …`
|
|
337
|
+
// · `Casting value "…" to type …`
|
|
338
|
+
const redactValueEcho = (s) => s
|
|
339
|
+
.replace(/(Could not (?:convert|parse) string )(['"])[\s\S]*\2(?=\s+(?:to|according)\b)/gi, "$1'<已脱敏>'")
|
|
340
|
+
.replace(/(Casting value )"[\s\S]*"(?=\s+to type\b)/gi, "$1'<已脱敏>'")
|
|
341
|
+
// `Input: "…"` 之后整段都是原始输入(可能跨行)—— 到消息结尾全裁掉。
|
|
342
|
+
.replace(/(Malformed JSON[\s\S]*?\bInput: )"[\s\S]*$/i, "$1'<已脱敏>'")
|
|
343
|
+
.replace(/(Failed to cast value(?: to (?:decimal|numerical))?: )[\s\S]*$/i, "$1'<已脱敏>'")
|
|
344
|
+
// strptime 的第二处回显:`… according to format specifier "%Y"\n<原值>\n<缩进>^`
|
|
345
|
+
// —— 头部已脱敏,但下一行会把原值再打印一遍(实测,payload 场景同样中招)。
|
|
346
|
+
// ⚠️ 插入符可能带**前导空白**(DuckDB 按出错列位置缩进),lookahead 必须容忍。
|
|
347
|
+
.replace(/(according to format specifier "[^"]*"\n)[\s\S]*?\n(?=[ \t]*\^)/i, '$1<已脱敏>\n');
|
|
348
|
+
const clean = (s) => {
|
|
349
|
+
const withoutSql = stripSqlEcho(s);
|
|
350
|
+
return sanitizeMessage(noMask ? withoutSql : redactValueEcho(withoutSql));
|
|
351
|
+
};
|
|
352
|
+
const message = clean(f.message);
|
|
353
|
+
const hint = f.hint === undefined ? undefined : clean(f.hint);
|
|
354
|
+
if (f.kind === 'usage') {
|
|
355
|
+
process.stderr.write(message.endsWith('\n') ? message : message + '\n');
|
|
356
|
+
}
|
|
357
|
+
return fail({
|
|
358
|
+
code: f.code, kind: f.kind, retryable: f.retryable,
|
|
359
|
+
message: message.replace(/\n+$/, ''),
|
|
360
|
+
...(hint !== undefined ? { hint } : {}),
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* `--table` 通道的**库漂移**识别(PR 前契约审查 Minor)。
|
|
365
|
+
*
|
|
366
|
+
* 该通道的 SQL 由代码按 `TABLES` 生成,用户没写列名/表名 —— 撞上库漂移(列被删、表被删)
|
|
367
|
+
* 时,`classifyDuckDbError` 的「SQL 无法执行,请检查表名、列名与 SQL 语法」与候选清单
|
|
368
|
+
* 会把 Agent 指向**它无法修改的东西**(它没写 SQL),而唯一有效动作是核对漂移
|
|
369
|
+
*(`db describe` 对同一状态会给出「实际多列/少列」的警告)。
|
|
370
|
+
*
|
|
371
|
+
* 判据收紧到「缺失名确实属于该表的声明」才认作漂移 —— 否则可能把真实的代码 bug
|
|
372
|
+
*(生成的 SQL 里写错了列名)掩盖成环境问题。不属于时返回 null,交回原生分派。
|
|
373
|
+
*/
|
|
374
|
+
export function driftFailureFor(table, e) {
|
|
375
|
+
const msg = errorText(e);
|
|
376
|
+
const byTable = TABLE_NAME_RE.exec(msg) ?? TABLE_REF_RE.exec(msg);
|
|
377
|
+
if (byTable !== null && byTable[1] === table.name) {
|
|
378
|
+
return {
|
|
379
|
+
code: 'query_error', kind: 'usage', retryable: false,
|
|
380
|
+
message: `表 ${table.name} 在库中不存在——与 TABLES 声明漂移(表被删或建库不完整)`,
|
|
381
|
+
hint: `这不是你的查询写错了:本命令的 SQL 由 CLI 生成。用 \`qiyu db describe ${table.name}\` 核对,`
|
|
382
|
+
+ '并考虑重新同步或重建库(`qiyu store init` 只补未执行的迁移,不重建已删的表)',
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
const byColumn = COLUMN_NAMED_RE.exec(msg) ?? COLUMN_NAME_RE.exec(msg);
|
|
386
|
+
if (byColumn !== null && table.columns.some((c) => c.name === byColumn[1])) {
|
|
387
|
+
return {
|
|
388
|
+
code: 'query_error', kind: 'usage', retryable: false,
|
|
389
|
+
message: `列 ${byColumn[1]} 在库中不存在(表 ${table.name})——库与 TABLES 声明漂移`,
|
|
390
|
+
hint: `这不是你的查询写错了:本命令的 SQL 由 CLI 生成,列名来自 TABLES 声明。`
|
|
391
|
+
+ `用 \`qiyu db describe ${table.name}\` 查看实际列集与漂移警告`,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|