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,219 @@
|
|
|
1
|
+
// src/sync/tasks/tickets.ts
|
|
2
|
+
// D-T9:工单任务(spec §5.2 末行/§5.5)—— tickets.search(工单明细)+ ticket.logs(逐单日志)。
|
|
3
|
+
// 两任务共用 op 窗口语义(Review Focus 1 的实测事实,设计稿 §10.1):
|
|
4
|
+
// ① **op 窗口必须按 ≤31 天切片**:opStart 被服务端静默钳到「当前时刻前约 31 天」,
|
|
5
|
+
// 而 90 天硬上限在诱导切 89 天窗 —— 每片会静默少取约 58 天且永不报错(maxWindowDays=31);
|
|
6
|
+
// ② **op 参数必须成对且 opEnd 为有效时间戳**:opEnd 缺省/为 0/只传一个/顺序颠倒时
|
|
7
|
+
// 服务端不报错而返回全量(信封层判不出异常)—— fetch 在构造 body 前守卫,客户端抛
|
|
8
|
+
// usage(绝不发请求);
|
|
9
|
+
// ③ **水位必须用操作时间**(opStart/opEnd=「窗口内是否有日志」),按创建时间(start/end)
|
|
10
|
+
// 增量永远拿不到状态变更;水位续跑带 **3 天回看重叠**(overlapDays=3,覆盖乱序到达的
|
|
11
|
+
// 日志/状态变更);
|
|
12
|
+
// ④ search 只识别 8 个参数(start/end/opStart/opEnd/status/templateId/limit/offset)——
|
|
13
|
+
// 其余参数(staffId/groupId/typeId/filterId/ids/keyword/orderBy/sort)被静默忽略,
|
|
14
|
+
// 透传只会误导调用方(契约诚实性),body 只含 op 对 + limit=500 + offset;
|
|
15
|
+
// ⑤ limit=500 精确兑现、offset 可一直翻到 total(无 9245 之墙,实测推翻)。
|
|
16
|
+
// 工单行线上 27 键且漂移频繁 → 业务字段收敛进 payload JSON 字符串列(schema.ts 工单批次注释)。
|
|
17
|
+
// ticket.log 无分页(limit/pageNo/pageSize 全被忽略),按窗口内 touched 工单逐张全量拉。
|
|
18
|
+
import { callPaced, nowIsoOf } from './pace.js';
|
|
19
|
+
/** 窗口起止的 ISO(与引擎水位/删除范围的值同形——T5 验收项)。 */
|
|
20
|
+
const isoOf = (ms) => new Date(ms).toISOString();
|
|
21
|
+
/** 限频节流与测试 sleep 注入收敛至共享 pace.ts(中-1)。 */
|
|
22
|
+
export { __setSleep } from './pace.js';
|
|
23
|
+
/**
|
|
24
|
+
* op 参数守卫(Review Focus 1 钉死):构造请求体之前校验窗口两端齐全且有序。
|
|
25
|
+
* opEnd 缺省/为 0 → 服务端把它当「没传」处理、静默返回全量(code:200,
|
|
26
|
+
* 实测 {opStart: now-1d, opEnd: now} → total 100 vs {opStart: now-1d, opEnd: 0} → total 2885);
|
|
27
|
+
* opStart > opEnd(顺序颠倒)同样静默失效。三者都必须在**客户端**拦下 —— 请求发出去
|
|
28
|
+
* 信封层判不出异常, Agent 会拿着全量数据误以为窗口过滤生效了。
|
|
29
|
+
* ⚠️ C 中-2:`opStart <= 0` 也必须拒绝 —— 0/负数不是真实时间戳(1970 是哨兵不是日期),
|
|
30
|
+
* 且 opStart 早于「now − 31 天」会被服务端**静默钳制**(窗口解释权交给服务端)。
|
|
31
|
+
* 首跑/--full 的起点由引擎的 `windowStartFloorDays` 抬到 now − 31 天(见任务声明),
|
|
32
|
+
* 走到这里时必然是正数;走到这里仍是 0 的,只可能是显式给了 0 起点 —— 显式失败。
|
|
33
|
+
*/
|
|
34
|
+
function assertOpWindow(w) {
|
|
35
|
+
// opEnd 取 0 或缺省的形态:窗口起点非负恒成立,故 opEnd<=0 只可能是「未有效赋值」
|
|
36
|
+
if (!Number.isFinite(w.end) || w.end <= 0 || !Number.isFinite(w.start) || w.start <= 0) {
|
|
37
|
+
throw usageError(`工单 op 窗口无效:opEnd 缺省/为 0 时服务端会静默返回全量,` +
|
|
38
|
+
`opStart<=0(1970 哨兵)会被静默钳制(窗口 start=${w.start}, end=${w.end})`);
|
|
39
|
+
}
|
|
40
|
+
if (w.start > w.end) {
|
|
41
|
+
throw usageError(`工单 op 窗口顺序颠倒(opStart > opEnd 会静默失效):start=${w.start} > end=${w.end}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** usage 错误(退出码 2):本地参数问题,重试无用。 */
|
|
45
|
+
function usageError(msg) {
|
|
46
|
+
const e = new Error(msg);
|
|
47
|
+
e.kind = 'usage';
|
|
48
|
+
e.hint = '这是同步任务的本地窗口参数问题(op 参数对必须齐全有序),修正后重跑';
|
|
49
|
+
return e;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* tickets.search 的请求体构造(导出供测试对拍——http mock 只见 URL,body 事实在签名里)。
|
|
53
|
+
* search 只识别 8 个参数:body 恰为 opStart/opEnd(毫秒时间戳,守卫已保证有效)
|
|
54
|
+
* + limit=500(实测精确兑现)+ offset(翻页位)。
|
|
55
|
+
*/
|
|
56
|
+
export function ticketBody(taskId, w, offset) {
|
|
57
|
+
switch (taskId) {
|
|
58
|
+
case 'tickets.search':
|
|
59
|
+
return { opStart: w.start, opEnd: w.end, limit: 500, offset };
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`未知工单任务:${taskId}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** 形状 C 载荷 → {total, tickets};载荷缺 tickets 数组视为响应形态漂移,显式失败。 */
|
|
65
|
+
function searchPayload(payload, endpointId) {
|
|
66
|
+
const o = (payload ?? {});
|
|
67
|
+
if (!Array.isArray(o.tickets)) {
|
|
68
|
+
throw new Error(`接口 ${endpointId} 返回的载荷没有 tickets 数组(响应形态漂移?),拒绝落库`);
|
|
69
|
+
}
|
|
70
|
+
// total 缺省视为单页(与 reports.pagedRaw 同口径)
|
|
71
|
+
return { total: typeof o.total === 'number' ? o.total : o.tickets.length,
|
|
72
|
+
tickets: o.tickets };
|
|
73
|
+
}
|
|
74
|
+
/** 每页上限(实测 limit=500 精确兑现)。 */
|
|
75
|
+
const PAGE_SIZE = 500;
|
|
76
|
+
/** 页数上限兜底(C 中-3/中-13,与 reports/knowledge 同口径)。 */
|
|
77
|
+
const MAX_PAGES_FALLBACK = 1000;
|
|
78
|
+
/** 空串主键拒绝(C 低-7):`''` 会写进 VARCHAR 主键列,且空串行还能反复覆盖同键。 */
|
|
79
|
+
const isEmptyKey = (v) => v === null || v === undefined || v === '';
|
|
80
|
+
/**
|
|
81
|
+
* 窗口内工单原始行的分页拉取(tickets.search / ticket.logs 的 touched 集合同源):
|
|
82
|
+
* limit=500 精确兑现、offset 按**返回行数**推进(不按投影后行数 —— 丢脏行不会让游标原地打转)、
|
|
83
|
+
* total 收敛即停。
|
|
84
|
+
* 终检(C 中-3):循环退出必须满足「已取原始行数 >= 声明 total」——上游声明 total=100 却
|
|
85
|
+
* 提前返回 0 行的短读是最危险的静默失败方向(会被当合法空窗),此处显式失败。
|
|
86
|
+
* 页数上限(C 中-13):`total 恒大于已取行数`的响应形态漂移在有限次调用后终止,绝不无限翻页。
|
|
87
|
+
*/
|
|
88
|
+
async function searchAllPages(w, ctx) {
|
|
89
|
+
const raw = [];
|
|
90
|
+
let declared = null;
|
|
91
|
+
let offset = 0;
|
|
92
|
+
for (let page = 1;; page++) {
|
|
93
|
+
const maxPages = declared !== null ? Math.ceil(declared / PAGE_SIZE) + 5 : MAX_PAGES_FALLBACK;
|
|
94
|
+
if (page > maxPages) {
|
|
95
|
+
throw new Error(`接口 ticket.search 翻页超限(${page - 1} 页 > 上限 ${maxPages},声明 ` +
|
|
96
|
+
`total=${String(declared)},已取 ${raw.length} 行):total 与已取行数不收敛,拒绝落库`);
|
|
97
|
+
}
|
|
98
|
+
const res = await callPaced('ticket.search', ticketBody('tickets.search', w, offset), ctx);
|
|
99
|
+
const { total, tickets } = searchPayload(res.payload, 'ticket.search');
|
|
100
|
+
if (declared === null)
|
|
101
|
+
declared = total;
|
|
102
|
+
else if (total !== declared) {
|
|
103
|
+
throw new Error(`接口 ticket.search 各页声明 total 漂移(${declared} → ${total}):` +
|
|
104
|
+
'遍历期间上游数据变更,拒绝落库(重跑即可)');
|
|
105
|
+
}
|
|
106
|
+
raw.push(...tickets);
|
|
107
|
+
if (raw.length >= declared || tickets.length === 0)
|
|
108
|
+
break;
|
|
109
|
+
offset += tickets.length;
|
|
110
|
+
}
|
|
111
|
+
if (raw.length < (declared ?? 0)) {
|
|
112
|
+
throw new Error(`接口 ticket.search 分页短读:声明 total=${String(declared)},实取 ${raw.length} 行` +
|
|
113
|
+
'(响应形态漂移/遍历期间上游变更),拒绝落库——重跑即可');
|
|
114
|
+
}
|
|
115
|
+
return raw;
|
|
116
|
+
}
|
|
117
|
+
/** 行投影:ticket_id 主键列(响应键 id);payload 收敛 + PII 敏感列提升。 */
|
|
118
|
+
function ticketRow(src, nowIso) {
|
|
119
|
+
const id = src.id;
|
|
120
|
+
// 键缺失/空串行:丢弃且不计入 fetched(与 apiCount 口径一致;C 低-7 空串会被当成同键反复覆盖)
|
|
121
|
+
if (isEmptyKey(id))
|
|
122
|
+
return null;
|
|
123
|
+
return {
|
|
124
|
+
ticket_id: String(id),
|
|
125
|
+
payload: JSON.stringify(src),
|
|
126
|
+
crmUserName: strOrNull(src.crmUserName),
|
|
127
|
+
crmUserPhone: strOrNull(src.crmUserPhone),
|
|
128
|
+
crmUserEmail: strOrNull(src.crmUserEmail),
|
|
129
|
+
userMobile: strOrNull(src.userMobile),
|
|
130
|
+
synchronized_at: nowIso,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const strOrNull = (v) => v === null || v === undefined ? null : typeof v === 'string' ? v : JSON.stringify(v);
|
|
134
|
+
/** tickets.search 任务 fetch:op 守卫 → limit=500 分页(total 收敛 + 短读终检)→ 投影落库。 */
|
|
135
|
+
async function fetchTickets(w, ctx) {
|
|
136
|
+
assertOpWindow(w);
|
|
137
|
+
const nowIso = nowIsoOf(ctx);
|
|
138
|
+
const out = [];
|
|
139
|
+
for (const r of await searchAllPages(w, ctx)) {
|
|
140
|
+
const row = ticketRow(r, nowIso);
|
|
141
|
+
if (row)
|
|
142
|
+
out.push(row);
|
|
143
|
+
}
|
|
144
|
+
return { rows: out, apiCount: out.length };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* ticket.logs 任务 fetch:先用同窗口的 search 找出 touched 工单(窗口内有操作 = op 语义),
|
|
148
|
+
* 再逐张 log(ticketId) 全量拉日志(无分页,limit/pageNo/pageSize 全被忽略)。
|
|
149
|
+
* 响应实测时间降序、id 非升序 —— 行落库经 (ticket_id, log_id) upsert 幂等吸收,顺序无关。
|
|
150
|
+
*/
|
|
151
|
+
async function fetchTicketLogs(w, ctx) {
|
|
152
|
+
assertOpWindow(w);
|
|
153
|
+
const nowIso = nowIsoOf(ctx);
|
|
154
|
+
// —— touched 工单:与 tickets.search 同窗同分页(同一份实现,短读/翻页上限语义一致)——
|
|
155
|
+
const touched = [];
|
|
156
|
+
for (const r of await searchAllPages(w, ctx)) {
|
|
157
|
+
if (!isEmptyKey(r.id))
|
|
158
|
+
touched.push(String(r.id));
|
|
159
|
+
}
|
|
160
|
+
// —— 逐单拉日志 ——
|
|
161
|
+
const out = [];
|
|
162
|
+
for (const tid of touched) {
|
|
163
|
+
const res = await callPaced('ticket.log', { ticketId: Number(tid) }, ctx);
|
|
164
|
+
// ticket.log 信封未实测(registry envelope=null)→ 宽松模式载荷;data 数组形态按实测 §10.3。
|
|
165
|
+
// 其余形态视为响应形态漂移,显式失败(与 reports.ts 的 searchPayload 同口径)——
|
|
166
|
+
// 静默按空处理会把整单日志吞成 complete。
|
|
167
|
+
let rows;
|
|
168
|
+
if (Array.isArray(res.payload)) {
|
|
169
|
+
rows = res.payload;
|
|
170
|
+
}
|
|
171
|
+
else if (Array.isArray(res.payload?.data)) {
|
|
172
|
+
rows = res.payload.data;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
const shape = res.payload === null || res.payload === undefined
|
|
176
|
+
? 'null'
|
|
177
|
+
: Array.isArray(res.payload) ? 'array' : typeof res.payload;
|
|
178
|
+
throw new Error(`接口 ticket.log 返回的载荷既不是数组也没有 data 数组(实测形态为顶层 data,得到 ${shape}),拒绝落库`);
|
|
179
|
+
}
|
|
180
|
+
for (const r of rows) {
|
|
181
|
+
const logId = r.id;
|
|
182
|
+
if (isEmptyKey(logId))
|
|
183
|
+
continue; // 键缺失/空串行:丢弃且不计入 fetched(C 低-7)
|
|
184
|
+
out.push({ ticket_id: tid, log_id: String(logId), payload: JSON.stringify(r), synchronized_at: nowIso });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return { rows: out, apiCount: out.length };
|
|
188
|
+
}
|
|
189
|
+
export const TICKET_TASKS = [
|
|
190
|
+
{
|
|
191
|
+
id: 'tickets.search', table: 'tickets', kind: 'windowed',
|
|
192
|
+
maxWindowDays: 31, overlapDays: 3,
|
|
193
|
+
watermark: { field: 'op_end', initial: null },
|
|
194
|
+
primaryKey: ['ticket_id'],
|
|
195
|
+
endpoint: 'ticket.search',
|
|
196
|
+
// C 中-2:首跑/--full 的起点是 0(哨兵)→ 引擎抬到 now − 31 天
|
|
197
|
+
//(服务端对更早的 opStart 静默钳制,发 1970 只是把窗口解释权交给服务端)。
|
|
198
|
+
windowStartFloorDays: 31,
|
|
199
|
+
write: { kind: 'upsertRows' },
|
|
200
|
+
fetch: fetchTickets,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
id: 'ticket.logs', table: 'ticket_logs', kind: 'windowed',
|
|
204
|
+
maxWindowDays: 31, overlapDays: 3,
|
|
205
|
+
// 水位沿用 tickets 的 op 窗口推进(spec §5.5:没有 logs 表就回答不了处理量/时长)
|
|
206
|
+
watermark: { field: 'op_end', initial: null },
|
|
207
|
+
primaryKey: ['ticket_id', 'log_id'],
|
|
208
|
+
// 同路径任务共享冷却(B 中-6):touched 集合与逐单日志都经 ticket.search / ticket.log,
|
|
209
|
+
// 闸门键取主要驱动路径 ticket.search(与 tickets.search 任务同键,交替调用照样被拦)。
|
|
210
|
+
endpoint: 'ticket.search',
|
|
211
|
+
windowStartFloorDays: 31,
|
|
212
|
+
write: { kind: 'upsertRows' },
|
|
213
|
+
fetch: fetchTicketLogs,
|
|
214
|
+
},
|
|
215
|
+
];
|
|
216
|
+
/** 按 id 取工单任务(T12 汇总与测试的取用形态)。 */
|
|
217
|
+
export function taskById(id) {
|
|
218
|
+
return TICKET_TASKS.find((t) => t.id === id);
|
|
219
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步引擎窗口切分纯函数(spec §6.1)。
|
|
3
|
+
* Range 为半开区间 [start, end),毫秒时间戳。
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 把范围按 maxWindowDays 切成若干窗。
|
|
7
|
+
* - 步长 = maxWindowDays − overlapDays,窗宽 = maxWindowDays;
|
|
8
|
+
* - 末窗收边:end 不超过 range.end;
|
|
9
|
+
* - 终止条件:当前窗的重叠端(end − overlap)已覆盖到 range.end,即相邻窗口
|
|
10
|
+
* 在整个 range 上都确立了重叠保证;若末窗被截断导致重叠端差一截,则从
|
|
11
|
+
* end − overlap 再补一窗,直至无前进空间(防死循环)。
|
|
12
|
+
*/
|
|
13
|
+
export function sliceByMaxWindow(range, maxWindowDays, overlapDays) {
|
|
14
|
+
const windowMs = maxWindowDays * 86_400_000;
|
|
15
|
+
const overlapMs = overlapDays * 86_400_000;
|
|
16
|
+
if (overlapMs >= windowMs) {
|
|
17
|
+
throw new RangeError(`overlapDays 必须小于 maxWindowDays:${overlapDays} >= ${maxWindowDays}`);
|
|
18
|
+
}
|
|
19
|
+
const windows = [];
|
|
20
|
+
let start = range.start;
|
|
21
|
+
while (start < range.end) {
|
|
22
|
+
const end = Math.min(start + windowMs, range.end);
|
|
23
|
+
windows.push({ start, end });
|
|
24
|
+
// 重叠端已覆盖到 range.end → 收尾;否则从重叠端起下一窗
|
|
25
|
+
if (end - overlapMs >= range.end)
|
|
26
|
+
break;
|
|
27
|
+
const nextStart = end - overlapMs;
|
|
28
|
+
// 无前进空间(末窗截断后反复补窗会原地打转)→ 终止
|
|
29
|
+
if (nextStart <= start)
|
|
30
|
+
break;
|
|
31
|
+
start = nextStart;
|
|
32
|
+
}
|
|
33
|
+
return windows;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 合并相邻(start == 前者 end)与重叠的区间,保留缝隙。
|
|
37
|
+
* 输入不要求有序,结果按 start 升序。
|
|
38
|
+
*/
|
|
39
|
+
export function mergeRanges(rs) {
|
|
40
|
+
if (rs.length === 0)
|
|
41
|
+
return [];
|
|
42
|
+
const sorted = [...rs].sort((a, b) => a.start - b.start);
|
|
43
|
+
const merged = [{ ...sorted[0] }];
|
|
44
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
45
|
+
const last = merged[merged.length - 1];
|
|
46
|
+
const cur = sorted[i];
|
|
47
|
+
if (cur.start <= last.end) {
|
|
48
|
+
// 相邻或重叠:延伸右端(含完全包含的情形)
|
|
49
|
+
last.end = Math.max(last.end, cur.end);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
merged.push({ ...cur });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return merged;
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qiyu-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "网易七鱼开放 API 的命令行封装,主要用户是 AI Agent:默认输出机器可读 JSON,用退出码表达意图,危险操作有硬门禁",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "MisonL",
|
|
8
|
+
"homepage": "https://github.com/MisonL/qiyu-cli#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/MisonL/qiyu-cli.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/MisonL/qiyu-cli/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"qiyu",
|
|
18
|
+
"七鱼",
|
|
19
|
+
"网易七鱼",
|
|
20
|
+
"cli",
|
|
21
|
+
"api",
|
|
22
|
+
"agent",
|
|
23
|
+
"ai-agent",
|
|
24
|
+
"customer-service"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.12"
|
|
28
|
+
},
|
|
29
|
+
"bin": {
|
|
30
|
+
"qiyu": "dist/cli.js"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc && node scripts/copy-assets.mjs",
|
|
37
|
+
"check:pack": "node scripts/check-pack.mjs",
|
|
38
|
+
"prepare": "npm run build",
|
|
39
|
+
"postinstall": "node dist/postinstall.js || exit 0",
|
|
40
|
+
"test": "node scripts/test-shards.mjs",
|
|
41
|
+
"test:single": "vitest run --maxWorkers=1 --no-file-parallelism",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.test.json"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.0.0",
|
|
46
|
+
"fflate": "^0.8.3",
|
|
47
|
+
"typescript": "^5.6.0",
|
|
48
|
+
"vitest": "^2.1.0",
|
|
49
|
+
"yaml": "2.9.1"
|
|
50
|
+
},
|
|
51
|
+
"optionalDependencies": {
|
|
52
|
+
"@duckdb/node-api": "1.5.6-r.1"
|
|
53
|
+
}
|
|
54
|
+
}
|