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,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"merchant.account.login": {
|
|
3
|
+
"note": "上游自相矛盾:接口描述要求 code/shopCode 必填其一,schema 却把三者全标必填。按接口描述建模为 one-of。",
|
|
4
|
+
"relaxRequired": ["code", "shopCode"],
|
|
5
|
+
"oneOf": [{ "anyOf": ["code", "shopCode"], "message": "code 与 shopCode 必填其一(都存在时以 code 为准)" }]
|
|
6
|
+
},
|
|
7
|
+
"ai.knowledge.editKnowledgeNoSimilars": {
|
|
8
|
+
"note": "设计稿 §5.4.1:该接口取 question,复用 editKnowledge body 形态的调用方会传 name——改名必须在请求构建器处理,不能留给调用方。",
|
|
9
|
+
"aliases": { "name": "question" }
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import raw from './validate.json' with { type: 'json' };
|
|
2
|
+
import { allEndpoints } from './index.js';
|
|
3
|
+
import { loadParams } from './params.js';
|
|
4
|
+
/** 纯函数:fail-closed 校验(§五 加载规则),坏数据抛普通 Error(模块初始化期由入口兜底为裸崩)。 */
|
|
5
|
+
export function validateOverrides(rawIn, specs, paramsFor) {
|
|
6
|
+
if (rawIn === null || typeof rawIn !== 'object' || Array.isArray(rawIn)) {
|
|
7
|
+
throw new Error('validate.json 顶层必须是对象');
|
|
8
|
+
}
|
|
9
|
+
const ids = new Set(specs.map((s) => s.id));
|
|
10
|
+
const out = {};
|
|
11
|
+
for (const [id, v] of Object.entries(rawIn)) {
|
|
12
|
+
if (!ids.has(id))
|
|
13
|
+
throw new Error(`validate.json 的键 ${id} 不是注册表里的接口 id`);
|
|
14
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v)) {
|
|
15
|
+
throw new Error(`validate.json[${id}] 必须是对象`);
|
|
16
|
+
}
|
|
17
|
+
const o = v;
|
|
18
|
+
const file = paramsFor(id);
|
|
19
|
+
const bodyNames = new Set(file.params.filter((p) => p.in === 'body').map((p) => p.name));
|
|
20
|
+
const requiredBody = new Set(file.params.filter((p) => p.in === 'body' && p.required).map((p) => p.name));
|
|
21
|
+
const ov = {};
|
|
22
|
+
if (o.note !== undefined) {
|
|
23
|
+
if (typeof o.note !== 'string')
|
|
24
|
+
throw new Error(`validate.json[${id}].note 必须是字符串`);
|
|
25
|
+
ov.note = o.note;
|
|
26
|
+
}
|
|
27
|
+
if (o.relaxRequired !== undefined) {
|
|
28
|
+
if (!Array.isArray(o.relaxRequired) || !o.relaxRequired.every((x) => typeof x === 'string')) {
|
|
29
|
+
throw new Error(`validate.json[${id}].relaxRequired 必须是字符串数组`);
|
|
30
|
+
}
|
|
31
|
+
for (const f of o.relaxRequired) {
|
|
32
|
+
if (!bodyNames.has(f))
|
|
33
|
+
throw new Error(`validate.json[${id}].relaxRequired 引用了非 body 字段 ${f}`);
|
|
34
|
+
if (!requiredBody.has(f))
|
|
35
|
+
throw new Error(`validate.json[${id}].relaxRequired 引用了非必填字段 ${f}(无效果死配置)`);
|
|
36
|
+
}
|
|
37
|
+
ov.relaxRequired = o.relaxRequired;
|
|
38
|
+
}
|
|
39
|
+
if (o.oneOf !== undefined) {
|
|
40
|
+
if (!Array.isArray(o.oneOf))
|
|
41
|
+
throw new Error(`validate.json[${id}].oneOf 必须是数组`);
|
|
42
|
+
ov.oneOf = o.oneOf.map((g, gi) => {
|
|
43
|
+
if (g === null || typeof g !== 'object' || Array.isArray(g)) {
|
|
44
|
+
throw new Error(`validate.json[${id}].oneOf[${gi}] 必须是对象`);
|
|
45
|
+
}
|
|
46
|
+
const gg = g;
|
|
47
|
+
if (!Array.isArray(gg.anyOf) || gg.anyOf.length < 2 || !gg.anyOf.every((x) => typeof x === 'string')) {
|
|
48
|
+
throw new Error(`validate.json[${id}].oneOf[${gi}].anyOf 必须是 ≥2 的字符串数组`);
|
|
49
|
+
}
|
|
50
|
+
for (const f of gg.anyOf) {
|
|
51
|
+
if (!bodyNames.has(f))
|
|
52
|
+
throw new Error(`validate.json[${id}].oneOf[${gi}].anyOf 引用了非 body 字段 ${f}`);
|
|
53
|
+
}
|
|
54
|
+
if (typeof gg.message !== 'string')
|
|
55
|
+
throw new Error(`validate.json[${id}].oneOf[${gi}].message 必须是字符串`);
|
|
56
|
+
return { anyOf: gg.anyOf, message: gg.message };
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (o.aliases !== undefined) {
|
|
60
|
+
if (o.aliases === null || typeof o.aliases !== 'object' || Array.isArray(o.aliases)) {
|
|
61
|
+
throw new Error(`validate.json[${id}].aliases 必须是对象`);
|
|
62
|
+
}
|
|
63
|
+
const targets = new Set();
|
|
64
|
+
for (const [src, dst] of Object.entries(o.aliases)) {
|
|
65
|
+
if (typeof dst !== 'string')
|
|
66
|
+
throw new Error(`validate.json[${id}].aliases 的目标键必须是字符串`);
|
|
67
|
+
if (!bodyNames.has(dst))
|
|
68
|
+
throw new Error(`validate.json[${id}].aliases 的目标键 ${dst} 不是 body 字段`);
|
|
69
|
+
if (bodyNames.has(src))
|
|
70
|
+
throw new Error(`validate.json[${id}].aliases 的源键 ${src} 本身就是 body 字段(不该被别名覆盖)`);
|
|
71
|
+
if (targets.has(dst))
|
|
72
|
+
throw new Error(`validate.json[${id}] 的别名目标 ${dst} 被多个源键指向`);
|
|
73
|
+
targets.add(dst);
|
|
74
|
+
}
|
|
75
|
+
ov.aliases = o.aliases;
|
|
76
|
+
}
|
|
77
|
+
const known = new Set(['note', 'relaxRequired', 'oneOf', 'aliases']);
|
|
78
|
+
for (const k of Object.keys(o)) {
|
|
79
|
+
if (!known.has(k))
|
|
80
|
+
throw new Error(`validate.json[${id}] 含未知键 ${k}`);
|
|
81
|
+
}
|
|
82
|
+
out[id] = ov;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 生产入口(与 `specs.json` 的 `const SPECS = loadSpecs(raw)` 同款两段式):
|
|
88
|
+
* **模块级常量**,在模块初始化期求值一次并缓存,**不是每次调用重读**(spec §十.0 第 40 条)。
|
|
89
|
+
* 后果(spec §十.0 明文,均为有意):validate.json 畸形 → 模块**求值期**抛错 →
|
|
90
|
+
* ESM 静态 import 链下入口模块体未执行、`cli.ts` 的 `.catch()` 未注册 → **未捕获异常**:
|
|
91
|
+
* stdout 0 字节、stderr 原生堆栈、退出码 1;且失败范围是**整个 CLI**(`auth whoami`/`api list` 一起挂)。
|
|
92
|
+
* 这与 `specs.json` 的既有行为逐字一致,B 不引入第二种失败语义。
|
|
93
|
+
* `loadValidationOverrides()` 仅作只读访问器,供 `callOne`/`schema` 取常量值。
|
|
94
|
+
*/
|
|
95
|
+
const OVERRIDES = validateOverrides(raw, allEndpoints(), loadParams);
|
|
96
|
+
export function loadValidationOverrides() {
|
|
97
|
+
return OVERRIDES;
|
|
98
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** 把不受信文本(调用方 body 的字段名等)中的终端控制字符抹成占位,防止 stderr 注入 ANSI/OSC 序列。 */
|
|
2
|
+
export function sanitizeControlChars(s) {
|
|
3
|
+
// eslint-disable-next-line no-control-regex
|
|
4
|
+
return s.replace(/[\u0000-\u0008\u000A-\u001F\u007F-\u009F]/g, '·');
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 逐行净化:保留换行结构(多行消息的可读性),剥掉每一行里的终端控制字符。
|
|
8
|
+
*
|
|
9
|
+
* `sanitizeControlChars` 会把 `\n` 一并抹掉(它落在 `\u000A-\u001F` 区间),故直接对整个
|
|
10
|
+
* 多行消息调用它会把换行也毁掉。错误出口(`usageFail` / `emitQueryFailure`)的 message
|
|
11
|
+
* 是多行的(用法说明、DuckDB 原文),走这个既防 stderr 注入又保住行结构。
|
|
12
|
+
*/
|
|
13
|
+
export function sanitizeMessage(s) {
|
|
14
|
+
return s.split('\n').map(sanitizeControlChars).join('\n');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 裁掉 DuckDB 错误原文里的 SQL 回显。DuckDB 的 Parser/Binder 错误会把出错行的 SQL 全文
|
|
18
|
+
* 附在 `LINE n: …` 之后(实测 `Parser Error … LINE 1: SELECT 1 FROM (SELECT userPhone …`)
|
|
19
|
+
* ——其中可能含调用方写进 SQL 的 PII(security S3:这是把原始值写进 stderr 的主要载体)。
|
|
20
|
+
* 保留错误类别与行号标记(`LINE n:` 之前的全部内容),只把回显的 SQL 本体换成占位。
|
|
21
|
+
*/
|
|
22
|
+
export function stripSqlEcho(s) {
|
|
23
|
+
return s.replace(/(\n\s*LINE \d+:)[\s\S]*$/, '$1 (SQL 原文已省略)');
|
|
24
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/** 计算字符串的 md5,小写十六进制。按 UTF-8 字节计算。 */
|
|
3
|
+
export function md5Hex(body) {
|
|
4
|
+
return createHash('md5').update(body, 'utf8').digest('hex');
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 七鱼鉴权签名。
|
|
8
|
+
* checksum = SHA1(appSecret + md5(body原文) + time),小写十六进制。
|
|
9
|
+
* time 为秒级时间戳,有效期 5 分钟。
|
|
10
|
+
*
|
|
11
|
+
* ⚠️ body 必须是**即将原样发送**的那个字符串。任何序列化差异都会导致 14002。
|
|
12
|
+
*/
|
|
13
|
+
export function checksum(appSecret, body, time) {
|
|
14
|
+
const raw = `${appSecret}${md5Hex(body)}${time}`;
|
|
15
|
+
return createHash('sha1').update(raw, 'utf8').digest('hex');
|
|
16
|
+
}
|
|
17
|
+
/** 拼出完整的鉴权查询串。顺序固定为 appKey、time、checksum。 */
|
|
18
|
+
export function signedQuery(appKey, appSecret, body, time) {
|
|
19
|
+
const cs = checksum(appSecret, body, time);
|
|
20
|
+
return `appKey=${encodeURIComponent(appKey)}&time=${time}&checksum=${cs}`;
|
|
21
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// src/core/time.ts
|
|
2
|
+
// Asia/Shanghai 自然日对齐与时间参数解析(**唯一定义点**)。
|
|
3
|
+
// G2/§6.2 联动:`--since/--until` 的日期按上海零点解析、报表日期列按上海天对齐、
|
|
4
|
+
// attendance/performance 的 endTime 上限是「昨天 00:00」——三处必须同源,否则
|
|
5
|
+
// 「同一业务日」在不同层得到不同的毫秒值(A 中-2 的键分裂根因)。
|
|
6
|
+
// E 起 `query/` 与 `sync/` 共用本模块(query 不得 import sync,故下沉 core)。
|
|
7
|
+
// Asia/Shanghai 无夏令时(恒 +08:00):自然日对齐用固定偏移、不依赖 Intl;
|
|
8
|
+
// `shanghaiToday` 例外用 Intl 取上海日期键。
|
|
9
|
+
/** 上海固定偏移(毫秒)。 */
|
|
10
|
+
export const SHANGHAI_OFFSET_MS = 8 * 3_600_000;
|
|
11
|
+
/** 一天的毫秒数。 */
|
|
12
|
+
export const DAY_MS = 86_400_000;
|
|
13
|
+
/**
|
|
14
|
+
* ECMAScript Date 可表示的最大绝对毫秒(±8.64e15,约 ±27.5 万年)。
|
|
15
|
+
* 超出后 `toISOString()` 抛 `RangeError: Invalid time value` —— 一条微秒级脏值
|
|
16
|
+
* (`1792467000000000000`)就能让 `db tables` 整条命令 internal 失败(正确性审查 F4)。
|
|
17
|
+
*/
|
|
18
|
+
export const MAX_DATE_MS = 8.64e15;
|
|
19
|
+
/**
|
|
20
|
+
* 能安全渲染成 `+08:00` ISO 的最大绝对毫秒(= `MAX_DATE_MS − 8h`,F 维度审查 gap 2)。
|
|
21
|
+
*
|
|
22
|
+
* ⚠️ 为什么不是 `MAX_DATE_MS`:`shanghaiIso` 先把 `ms` 加上上海偏移再 `toISOString()`,
|
|
23
|
+
* 于是 `(MAX_DATE_MS − 8h, MAX_DATE_MS]` 这段**守卫放行、实际抛** `RangeError`——
|
|
24
|
+
* 一条脏值就把整条命令打成 internal(与 F4 同一失败模式,只是精确落在边界上)。
|
|
25
|
+
* 判据必须落在**偏移之后**的量上:`|ms| <= MAX_SHANGHAI_SAFE_MS` 才是「能回显」。
|
|
26
|
+
*
|
|
27
|
+
* 三处共用同一常量(不各写一个字面量):
|
|
28
|
+
* · `shanghaiIso` / `msToShanghaiIso` 的越界守卫;
|
|
29
|
+
* · `commands/db.ts` 的 `shapeSqlOf` 与 `query/table-query.ts` 的 `shapeClause`
|
|
30
|
+
* 生成的 epoch-ms 形状谓词上界 —— 「形状合格」必须与「能回显」是同一集合,
|
|
31
|
+
* 否则 SQL 会把恰好会抛的值放进候选集(gap 2 的放大器)。
|
|
32
|
+
*
|
|
33
|
+
* 负侧按同一阈值对称收紧(`Math.abs`):`(-MAX_DATE_MS, -(MAX_DATE_MS−8h)]` 这段
|
|
34
|
+
* 偏移后其实仍可表示,但那是公元 −271821 年附近、且 epoch-ms 形状谓词的下界
|
|
35
|
+
* (`> 1e11`)已排除全部负值 —— 回显原值不丢任何真实信息,换单一阈值更好核。
|
|
36
|
+
*/
|
|
37
|
+
export const MAX_SHANGHAI_SAFE_MS = MAX_DATE_MS - SHANGHAI_OFFSET_MS;
|
|
38
|
+
/**
|
|
39
|
+
* 毫秒 → 所属上海自然日 00:00 的 epoch 毫秒。
|
|
40
|
+
* ⚠️ 纯算术、不抛;但 `ms` 落在负侧极端带
|
|
41
|
+
* (`[-MAX_DATE_MS, -MAX_DATE_MS + (DAY_MS − 8h))`,约 16 小时宽)时,向下取整会把
|
|
42
|
+
* 结果推到 `Date` 可表示范围之外(实测 `shanghaiDayStart(-8.64e15) === -8640000028800000`,
|
|
43
|
+
* 而 `new Date(-8640000028800000).toISOString()` 抛 `RangeError`)。
|
|
44
|
+
* 需要 ISO 形态的调用方必须走 `shanghaiDayIso`(它在造型前挡住越界)。
|
|
45
|
+
*/
|
|
46
|
+
export function shanghaiDayStart(ms) {
|
|
47
|
+
return Math.floor((ms + SHANGHAI_OFFSET_MS) / DAY_MS) * DAY_MS - SHANGHAI_OFFSET_MS;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 毫秒 → 上海「昨天 00:00」的 epoch 毫秒。
|
|
51
|
+
* attendance/performance 的 `endTime` 不得晚于今天 00:00(`14501 endTime is invalid`),
|
|
52
|
+
* 且实测「今天 0:00」这条边界不稳定(同一取值时而 200 时而 14501,设计稿 §6.2)——
|
|
53
|
+
* 统一退到昨天 0:00 留整日余量:当天数据不做(次日自动补上),绝不贴着边界。
|
|
54
|
+
*/
|
|
55
|
+
export function shanghaiYesterdayStart(nowMs) {
|
|
56
|
+
return shanghaiDayStart(nowMs) - DAY_MS;
|
|
57
|
+
}
|
|
58
|
+
/** 天对齐值 → ISO 字符串(与 deleteRangeInsert 的范围值同形——报表日期列的唯一形态)。 */
|
|
59
|
+
export function shanghaiDayIso(ms) {
|
|
60
|
+
const dayStart = shanghaiDayStart(ms);
|
|
61
|
+
// ⚠️ gap 2 的负侧:`shanghaiDayStart` 的向下取整会把极端负值推到 `Date` 范围外
|
|
62
|
+
// (见该函数的注释),`toISOString()` 随即抛 `RangeError` —— 一条脏值炸整条命令。
|
|
63
|
+
// 越界按本模块既有约定**原样回显**(保住可解释),绝不抛。
|
|
64
|
+
if (!Number.isFinite(dayStart) || Math.abs(dayStart) > MAX_DATE_MS)
|
|
65
|
+
return String(ms);
|
|
66
|
+
return new Date(dayStart).toISOString();
|
|
67
|
+
}
|
|
68
|
+
const SHANGHAI_DAY = new Intl.DateTimeFormat('en-CA', {
|
|
69
|
+
timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit',
|
|
70
|
+
});
|
|
71
|
+
/** 毫秒 → 上海时区的 ISO 回显(resolved_range/watermark 的输出形态,spec §九.3)。 */
|
|
72
|
+
export function shanghaiIso(ms) {
|
|
73
|
+
// ⚠️ 越界值原样回显(正确性审查 F4):`toISOString()` 对 |ms| > 8.64e15 抛 RangeError,
|
|
74
|
+
// 而调用点可能拿到库里的脏值(微秒误写)或用户给的超大 `--since 999999999d`。
|
|
75
|
+
// 抛异常会让整条命令 internal 失败;回显原始数字至少保住「可解释」。
|
|
76
|
+
//
|
|
77
|
+
// ⚠️ gap 2:判据必须落在**偏移之后**的量上。本函数先把 `ms` 加上上海偏移(+8h)
|
|
78
|
+
// 再 `toISOString()`,所以 `|ms| <= MAX_DATE_MS` 仍不够 —— `(MAX_DATE_MS−8h, MAX_DATE_MS]`
|
|
79
|
+
// 这段守卫放行、实际抛 RangeError。故用 `MAX_SHANGHAI_SAFE_MS`(= MAX_DATE_MS − 8h)。
|
|
80
|
+
if (!Number.isFinite(ms) || Math.abs(ms) > MAX_SHANGHAI_SAFE_MS)
|
|
81
|
+
return String(ms);
|
|
82
|
+
const shifted = new Date(ms + SHANGHAI_OFFSET_MS);
|
|
83
|
+
// 整秒时省略毫秒段(spec 示例形态 "2026-09-14T00:00:00+08:00");带毫秒保留
|
|
84
|
+
const iso = shifted.toISOString();
|
|
85
|
+
const msPart = shifted.getUTCMilliseconds();
|
|
86
|
+
return (msPart === 0 ? iso.replace('.000Z', 'Z') : iso).replace('Z', '+08:00');
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 时间值 → 上海 ISO(**全命令层统一的时间回显口径**,E 中-2/C-11)。
|
|
90
|
+
* 入参可能是引擎产出的 epoch 毫秒(number 或数字字符串),也可能是 ISO 8601
|
|
91
|
+
* 字符串(`iso-day`/`iso-instant` 列的 MIN/MAX 形态)——两者都统一为 +08:00 ISO;
|
|
92
|
+
* 无法解析为时间的字符串原样回显(非时间值不吞掉)。
|
|
93
|
+
* ⚠️ ISO 分支只接受带时区的 ISO 8601 字符串;日期-only(`YYYY-MM-DD`)会被
|
|
94
|
+
* `Date.parse` 按 UTC 解析、再偏 8 小时——调用方不得喂日期-only 值。
|
|
95
|
+
* gap 的 range 此前直接输出毫秒字符串,与 resolved_range 的 +08:00 ISO 形态不一致,
|
|
96
|
+
* Agent 得按字段各写一套解析——本函数是两侧的同一落点。
|
|
97
|
+
*/
|
|
98
|
+
export function msToShanghaiIso(v) {
|
|
99
|
+
const numeric = typeof v === 'number' ? v : Number(v);
|
|
100
|
+
const n = Number.isFinite(numeric) ? numeric : Date.parse(String(v));
|
|
101
|
+
// 越界值不抛 RangeError(正确性审查 F4):库里一条微秒级脏值不应炸掉整条 db tables。
|
|
102
|
+
// ⚠️ gap 2:上界与 `shanghaiIso` 同源(`MAX_SHANGHAI_SAFE_MS`)——两者都要保证
|
|
103
|
+
// 「偏移之后仍可表示」,差一个 8h 就会让脏值在下游抛 RangeError。
|
|
104
|
+
if (!Number.isFinite(n) || Math.abs(n) > MAX_SHANGHAI_SAFE_MS)
|
|
105
|
+
return String(v);
|
|
106
|
+
return String(v).trim() !== '' ? shanghaiIso(n) : String(v);
|
|
107
|
+
}
|
|
108
|
+
/** 上海自然日 00:00 → epoch 毫秒(YYYY-MM-DD)。 */
|
|
109
|
+
function shanghaiMidnightUtc(y, m, d) {
|
|
110
|
+
return Date.UTC(y, m - 1, d) - SHANGHAI_OFFSET_MS;
|
|
111
|
+
}
|
|
112
|
+
/** 上海时区的「今天」日期键(YYYY-MM-DD)。 */
|
|
113
|
+
function shanghaiToday(clock) {
|
|
114
|
+
return SHANGHAI_DAY.format(new Date(clock()));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 时间参数解析(spec §九.3:相对时间与日期按 Asia/Shanghai 解析,回显 resolved_range):
|
|
118
|
+
* · Nd(如 30d)→ 相对时长:end=clock 当前、start=end−N 天(纯时长与时区无关,
|
|
119
|
+
* 按上海自然日切分由引擎窗口逻辑吸收);
|
|
120
|
+
* · yesterday → 上海昨天的 00:00(单日窗);
|
|
121
|
+
* · YYYY-MM-DD → 上海当天 00:00;
|
|
122
|
+
* · 其余(完整 ISO 8601)→ Date.parse,NaN 视为用法错误。
|
|
123
|
+
*/
|
|
124
|
+
export function parseTimeArg(arg, clock, label) {
|
|
125
|
+
void label;
|
|
126
|
+
const dur = /^(\d+)d$/.exec(arg);
|
|
127
|
+
if (dur) {
|
|
128
|
+
const end = clock();
|
|
129
|
+
// ⚠️ 带相对时长的 `--since` 也要挡住溢出(正确性审查 F4):
|
|
130
|
+
// `999999999d` 的 `end − N×DAY_MS` 远出 Date 范围,后续 `shanghaiIso` 会抛 RangeError。
|
|
131
|
+
// 与 ISO 分支同一上界,回到「未知 ≠ 猜」(usage)而不是崩到 internal。
|
|
132
|
+
// ⚠️ gap 2:上界是 `MAX_SHANGHAI_SAFE_MS`(偏移后的量),与 `shanghaiIso` 同源——
|
|
133
|
+
// 本函数接受的范围必须恰好是「能回显成 +08:00 ISO」的范围,否则 resolved_range
|
|
134
|
+
// 会退化成裸数字。
|
|
135
|
+
const start = end - Number(dur[1]) * DAY_MS;
|
|
136
|
+
if (!Number.isFinite(start) || Math.abs(start) > MAX_SHANGHAI_SAFE_MS)
|
|
137
|
+
return null;
|
|
138
|
+
return { start, end };
|
|
139
|
+
}
|
|
140
|
+
if (arg === 'yesterday') {
|
|
141
|
+
// 上海昨天 00:00 ~ 今天 00:00 的单日窗
|
|
142
|
+
const today = shanghaiToday(clock);
|
|
143
|
+
const [ty, tm, td] = today.split('-').map(Number);
|
|
144
|
+
const todayStart = shanghaiMidnightUtc(ty, tm, td);
|
|
145
|
+
return { start: todayStart - DAY_MS, end: todayStart };
|
|
146
|
+
}
|
|
147
|
+
const date = /^(\d{4})-(\d{2})-(\d{2})$/.exec(arg);
|
|
148
|
+
if (date) {
|
|
149
|
+
const y = Number(date[1]);
|
|
150
|
+
const m = Number(date[2]);
|
|
151
|
+
const d = Number(date[3]);
|
|
152
|
+
// ⚠️ 回验(正确性 P1 / edge F4):`Date.UTC` 会把越界的月/日**静默进位**
|
|
153
|
+
// (2026-02-30 → 2026-03-02、2026-13-45 → 2027-02-14),也会把 0–99 年映射到
|
|
154
|
+
// 1900+(0000-01-01 → 1900-01-01)。旧实现直接返回进位结果、退出码 0 且无警告:
|
|
155
|
+
// Agent 拿到一条与输入对不上的区间,甚至把「输入了不存在的日期」误读成「同步缺口」。
|
|
156
|
+
// 回验不一致即视为无法解析(红线 3「未知 ≠ 猜」),由调用方走 usage。
|
|
157
|
+
const ms = shanghaiMidnightUtc(y, m, d);
|
|
158
|
+
const back = new Date(ms + SHANGHAI_OFFSET_MS);
|
|
159
|
+
if (back.getUTCFullYear() !== y || back.getUTCMonth() + 1 !== m || back.getUTCDate() !== d) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
return ms;
|
|
163
|
+
}
|
|
164
|
+
// ⚠️ 无时区后缀的 ISO 必须显式按 Asia/Shanghai 解释(edge F5):`Date.parse` 对
|
|
165
|
+
// 无偏移量的 ISO 使用**进程时区**,在 CI(常为 UTC)上同一输入会偏 8 小时,
|
|
166
|
+
// 而本 CLI 的全部时间语义是 Asia/Shanghai(spec §三),且偏了没有任何警告。
|
|
167
|
+
//
|
|
168
|
+
// ⚠️ 且只接受**两种 ISO 形态**(CodeRabbit 终审 minor):`Date.parse` 的遗留解析器
|
|
169
|
+
// 还接受 `2026/09/15`、`Sep 15 2026` 这类非 ISO 写法,并按进程时区解释 ——
|
|
170
|
+
// 同一个 F5 缺陷的另一扇门。契约(README / --help)只承诺 ISO 8601,故其余一律拒绝。
|
|
171
|
+
const tzless = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?$/.test(arg);
|
|
172
|
+
const zoned = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})$/i.test(arg);
|
|
173
|
+
if (!tzless && !zoned)
|
|
174
|
+
return null;
|
|
175
|
+
// ⚠️ 带时刻的 ISO 同样要回验日历日(正确性审查 F7):`Date.parse('2026-02-30T00:00:00')`
|
|
176
|
+
// 静默进位成 3/2,退出码 0 —— 与纯日期分支已修的那类缺陷是同一扇门的另一半。
|
|
177
|
+
// 回验的是**日期字段**(年/月/日);时刻字段(24:00、60 秒)由下面的 Number 范围校验兜。
|
|
178
|
+
const ymd = /^(\d{4})-(\d{2})-(\d{2})/.exec(arg);
|
|
179
|
+
if (ymd) {
|
|
180
|
+
const y = Number(ymd[1]);
|
|
181
|
+
const m = Number(ymd[2]);
|
|
182
|
+
const d = Number(ymd[3]);
|
|
183
|
+
const probe = new Date(Date.UTC(y, m - 1, d));
|
|
184
|
+
if (probe.getUTCFullYear() !== y || probe.getUTCMonth() + 1 !== m || probe.getUTCDate() !== d) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const full = Date.parse(tzless ? `${arg.replace(' ', 'T')}+08:00` : arg.replace(' ', 'T'));
|
|
189
|
+
// ⚠️ gap 2:上界与 `shanghaiIso` 同源(偏移后的量)—— 本函数接受的值必须
|
|
190
|
+
// 恰好是「能回显成 +08:00 ISO」的值,否则 resolved_range 会退化成裸数字。
|
|
191
|
+
if (Number.isNaN(full) || Math.abs(full) > MAX_SHANGHAI_SAFE_MS)
|
|
192
|
+
return null;
|
|
193
|
+
return full; // 解析失败 → 调用方 usage
|
|
194
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { sanitizeControlChars } from './sanitize.js';
|
|
2
|
+
/** 应用 override 后的**有效 body 参数表**(只含 `in === 'body'` 的参数):把 `ov.relaxRequired` 列出的
|
|
3
|
+
* body 字段 required 降为 false;其余原样。
|
|
4
|
+
* ⚠️ **必须只返回 body 参数**(不是 `file.params` 全量):`validateBody` 拿它做
|
|
5
|
+
* `hasBodySchema = bodyKind==='json' && bodyParams.length > 0` 与缺必填判定——若把 query 参数
|
|
6
|
+
* 也算进来,像 `tag.all`(json、0 body、3 个 required 的鉴权 query)会被误判「有表」且硬拒
|
|
7
|
+
* 「缺 appKey/time/checksum」(那是 CLI 自填的 query,从不经 --body)。见验收 8/27。
|
|
8
|
+
* ⚠️ `api schema` 的 params 输出(Task 7)与 `validateBody` 的缺必填判定(Task 5)**共用本函数**
|
|
9
|
+
* (spec §十.0 :999-1004);schema 用 `file.params` 取全量、用本函数的返回值只查 body 字段的 override。
|
|
10
|
+
* 纯函数、无 I/O。 */
|
|
11
|
+
export function effectiveBodyParams(file, ov) {
|
|
12
|
+
const bodyOnly = file.params.filter((p) => p.in === 'body');
|
|
13
|
+
if (!ov?.relaxRequired || ov.relaxRequired.length === 0)
|
|
14
|
+
return bodyOnly;
|
|
15
|
+
const relax = new Set(ov.relaxRequired);
|
|
16
|
+
return bodyOnly.map((p) => (relax.has(p.name) ? { ...p, required: false } : p));
|
|
17
|
+
}
|
|
18
|
+
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
19
|
+
/** 归一化别名(D8):返回改写后的 body 与 alias_normalized 警告;源/目标键冲突 → 抛(硬)。 */
|
|
20
|
+
export function normalizeAliases(file, body, ov) {
|
|
21
|
+
// ⚠️ 非普通对象原样返回:别名扫描只对普通对象做,绝不在 undefined 上调 Object.keys/展开。
|
|
22
|
+
if (!ov?.aliases || !isPlainObject(body))
|
|
23
|
+
return { body, warnings: [] };
|
|
24
|
+
const aliases = ov.aliases;
|
|
25
|
+
const srcPresent = Object.keys(aliases).filter((s) => Object.hasOwn(body, s));
|
|
26
|
+
if (srcPresent.length === 0)
|
|
27
|
+
return { body, warnings: [] };
|
|
28
|
+
for (const s of srcPresent) {
|
|
29
|
+
if (Object.hasOwn(body, aliases[s])) {
|
|
30
|
+
throw new Error(`body 同时含别名源键 ${sanitizeControlChars(s)} 与目标键 ${sanitizeControlChars(aliases[s])} —— 只传其一(不预设谁优先)`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// 按 body 键出现顺序输出 warning
|
|
34
|
+
const warnings = [];
|
|
35
|
+
// 必须用 Object.fromEntries 而非 `rewritten[k] = v` 逐键赋值:body 里可能有
|
|
36
|
+
// `__proto__` 这个 own key(`JSON.parse('{"__proto__":…}')` 会产出),而对普通对象
|
|
37
|
+
// 做 `obj["__proto__"] = v` 会被 setPrototypeOf 语义吞掉,导致字段静默消失。
|
|
38
|
+
// 原型不会被污染,但字段会丢;fromEntries 会保留它为普通 own key。
|
|
39
|
+
const rewritten = Object.fromEntries(Object.entries(body).map(([k, v]) => {
|
|
40
|
+
if (!Object.hasOwn(aliases, k))
|
|
41
|
+
return [k, v];
|
|
42
|
+
const dst = aliases[k];
|
|
43
|
+
warnings.push({ kind: 'alias_normalized', field: k,
|
|
44
|
+
message: `${sanitizeControlChars(k)} 已按本接口约定改写为 ${sanitizeControlChars(dst)} 发送;请求正常,无需重试` });
|
|
45
|
+
return [dst, v];
|
|
46
|
+
}));
|
|
47
|
+
return { body: rewritten, warnings };
|
|
48
|
+
}
|
|
49
|
+
const BODY_TYPE_MAP = {
|
|
50
|
+
string: (v) => typeof v === 'string',
|
|
51
|
+
integer: (v) => typeof v === 'number',
|
|
52
|
+
number: (v) => typeof v === 'number',
|
|
53
|
+
boolean: (v) => typeof v === 'boolean',
|
|
54
|
+
array: (v) => Array.isArray(v),
|
|
55
|
+
object: (v) => v !== null && typeof v === 'object' && !Array.isArray(v),
|
|
56
|
+
};
|
|
57
|
+
/** 渲染实际类型(固定词汇,不用 typeof —— typeof null/[] 都是 object)。 */
|
|
58
|
+
function renderActual(v) {
|
|
59
|
+
if (v === null)
|
|
60
|
+
return 'null';
|
|
61
|
+
if (Array.isArray(v))
|
|
62
|
+
return '数组';
|
|
63
|
+
return typeof v;
|
|
64
|
+
}
|
|
65
|
+
/** 纯函数:顶层形状契约 + 硬/软规则;不碰 I/O。 */
|
|
66
|
+
export function validateBody(spec, file, body, ov, mode) {
|
|
67
|
+
// hasBodySchema 判据 = 合取式(§三/§八.6):bodyKind==='json' 且 body 参数数 > 0。
|
|
68
|
+
const bodyParams = effectiveBodyParams(file, ov);
|
|
69
|
+
const hasBodySchema = file.bodyKind === 'json' && bodyParams.length > 0;
|
|
70
|
+
// bodyKind !== 'json' 的接口根本不进形状表:只可能出 non_json_body(软)。
|
|
71
|
+
// ⚠️ message 不得含「未验证」(spec §四 第 68 条③ + 验收 9)——那是「值得再试」的措辞;只陈述「已发出 + 不要重发」。
|
|
72
|
+
if (file.bodyKind !== 'json') {
|
|
73
|
+
if (body === undefined)
|
|
74
|
+
return { hard: [], soft: [] };
|
|
75
|
+
return { hard: [], soft: [{ kind: 'non_json_body',
|
|
76
|
+
message: `本接口 body_kind 是 ${sanitizeControlChars(file.bodyKind)},你的 --body 已按原样字面量发送、未经字段校验;本 CLI 不构造非 JSON 的 body 编码。该字面量已随请求发出,不要重发(重发 = 再执行一次接口,写接口会重复写入)` }] };
|
|
77
|
+
}
|
|
78
|
+
if (!hasBodySchema)
|
|
79
|
+
return { hard: [], soft: [] };
|
|
80
|
+
const hard = [];
|
|
81
|
+
// 非对象 body + 有表 → 硬拒
|
|
82
|
+
if (body === undefined) {
|
|
83
|
+
const missing = bodyParams.filter((p) => p.required);
|
|
84
|
+
if (missing.length > 0) {
|
|
85
|
+
return { hard: [{ kind: 'missing_required', field: missing[0].name,
|
|
86
|
+
message: `未传 --body,但该接口需要 JSON 对象 body(缺必填字段:${missing.map((p) => sanitizeControlChars(p.name)).join('、')})。用 qiyu api schema ${spec.id} 查看完整参数表` }], soft: [] };
|
|
87
|
+
}
|
|
88
|
+
return { hard: [], soft: [] };
|
|
89
|
+
}
|
|
90
|
+
if (!isPlainObject(body)) {
|
|
91
|
+
return { hard: [{ kind: 'non_object_body',
|
|
92
|
+
message: `该接口需要 JSON 对象 body,实际是 ${sanitizeControlChars(body === null ? 'null' : Array.isArray(body) ? '数组' : '标量')}。用 qiyu api schema ${spec.id} 查看参数表` }], soft: [] };
|
|
93
|
+
}
|
|
94
|
+
const bodyNames = new Set(bodyParams.map((p) => p.name));
|
|
95
|
+
const aliasSrc = new Set(Object.keys(ov?.aliases ?? {}));
|
|
96
|
+
const soft = [];
|
|
97
|
+
// 硬:逐字段缺必填(params 表顺序)
|
|
98
|
+
for (const p of bodyParams) {
|
|
99
|
+
if (p.required && (!Object.hasOwn(body, p.name) || body[p.name] === undefined)) {
|
|
100
|
+
hard.push({ kind: 'missing_required', field: p.name,
|
|
101
|
+
message: `缺少必填字段 ${sanitizeControlChars(p.name)}(body 里没有该键)。用 qiyu api schema ${spec.id} 查看完整参数表` });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// 硬:one-of 组(组内任一存在即满足;全缺 → 用 override message),排在 per-field 之后
|
|
105
|
+
for (const g of ov?.oneOf ?? []) {
|
|
106
|
+
if (!g.anyOf.some((f) => Object.hasOwn(body, f) && body[f] !== undefined)) {
|
|
107
|
+
hard.push({ kind: 'one_of_missing', message: g.message });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (hard.length > 0)
|
|
111
|
+
return { hard, soft: [] };
|
|
112
|
+
// 软:unknown_field(body 键序)、type_mismatch(params 表序)
|
|
113
|
+
for (const k of Object.keys(body)) {
|
|
114
|
+
if (!bodyNames.has(k) && !aliasSrc.has(k)) {
|
|
115
|
+
soft.push({ kind: 'unknown_field', field: k,
|
|
116
|
+
message: `字段 ${sanitizeControlChars(k)} 不在本接口的文档参数表中,服务端可能忽略它;但本次请求已经发出(退出码 0)——不要重发(重发 = 再执行一次接口,写接口会重复写入)` });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const p of bodyParams) {
|
|
120
|
+
if (p.type && Object.hasOwn(body, p.name) && body[p.name] !== undefined) {
|
|
121
|
+
const check = BODY_TYPE_MAP[p.type];
|
|
122
|
+
if (check && !check(body[p.name])) {
|
|
123
|
+
soft.push({ kind: 'type_mismatch', field: p.name,
|
|
124
|
+
message: `字段 ${sanitizeControlChars(p.name)} 声明类型 ${sanitizeControlChars(p.type)},实际是 ${sanitizeControlChars(renderActual(body[p.name]))}。本值已随请求发出(退出码 0),不要重发` });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// strict 升级:unknown_field / type_mismatch 从软转硬(硬形态 message 不得含「已发出」)
|
|
129
|
+
if (mode.strict) {
|
|
130
|
+
const upgraded = soft.filter((w) => w.kind === 'unknown_field' || w.kind === 'type_mismatch');
|
|
131
|
+
if (upgraded.length > 0) {
|
|
132
|
+
const hardMsgs = upgraded.map((w) => ({ kind: w.kind, field: w.field,
|
|
133
|
+
message: w.kind === 'unknown_field'
|
|
134
|
+
? `本地参数表未收录字段 ${sanitizeControlChars(w.field ?? '')},未发出请求,请修正后重试(这是本地参数表闭集一致性检查,不是对服务端行为的保证 —— 若确认该字段合法,请改用默认模式或报告缺失的注册表项,不要删字段重试)`
|
|
135
|
+
: `字段 ${sanitizeControlChars(w.field ?? '')} 类型不符(声明类型与传入值不符),未发出请求,请修正后重试 —— 不要删字段重试` }));
|
|
136
|
+
return { hard: hardMsgs, soft: [] };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { hard: [], soft };
|
|
140
|
+
}
|