@harrylabsj/kiwi 0.12.14 → 0.12.15

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.
Files changed (137) hide show
  1. package/dist/a2a/node.d.ts +5 -0
  2. package/dist/a2a/node.js +69 -2
  3. package/dist/a2a/node.js.map +1 -1
  4. package/dist/a2a/server/merchant-handler.d.ts +8 -0
  5. package/dist/a2a/server/merchant-handler.js +105 -14
  6. package/dist/a2a/server/merchant-handler.js.map +1 -1
  7. package/dist/a2a/server/negotiation-advisor.d.ts +54 -0
  8. package/dist/a2a/server/negotiation-advisor.js +107 -0
  9. package/dist/a2a/server/negotiation-advisor.js.map +1 -0
  10. package/dist/a2a/server/negotiation-approvals.d.ts +101 -0
  11. package/dist/a2a/server/negotiation-approvals.js +208 -0
  12. package/dist/a2a/server/negotiation-approvals.js.map +1 -0
  13. package/dist/buyer-core/a2a-knp.d.ts +14 -0
  14. package/dist/buyer-core/a2a-knp.js +24 -0
  15. package/dist/buyer-core/a2a-knp.js.map +1 -1
  16. package/dist/buyer-core/a2a-negotiator.js +66 -11
  17. package/dist/buyer-core/a2a-negotiator.js.map +1 -1
  18. package/dist/buyer-core/protocol-recovery.d.ts +1 -1
  19. package/dist/buyer-core/receive-gate.js +7 -0
  20. package/dist/buyer-core/receive-gate.js.map +1 -1
  21. package/dist/buyer-core/service.d.ts +13 -0
  22. package/dist/buyer-core/service.js +73 -10
  23. package/dist/buyer-core/service.js.map +1 -1
  24. package/dist/cli.js +54 -0
  25. package/dist/cli.js.map +1 -1
  26. package/dist/cloud/bootstrap.js +12 -0
  27. package/dist/cloud/bootstrap.js.map +1 -1
  28. package/dist/http/merchant-management/api.d.ts +3 -0
  29. package/dist/http/merchant-management/api.js +419 -0
  30. package/dist/http/merchant-management/api.js.map +1 -1
  31. package/dist/http/merchant-management/page.js +95 -1
  32. package/dist/http/merchant-management/page.js.map +1 -1
  33. package/dist/merchant/application/actor.d.ts +1 -1
  34. package/dist/merchant/application/actor.js +43 -1
  35. package/dist/merchant/application/actor.js.map +1 -1
  36. package/dist/merchant/quote-calculator.d.ts +7 -0
  37. package/dist/merchant/quote-calculator.js +35 -24
  38. package/dist/merchant/quote-calculator.js.map +1 -1
  39. package/dist/merchant-core/commercial/calculate.d.ts +18 -0
  40. package/dist/merchant-core/commercial/calculate.js +196 -0
  41. package/dist/merchant-core/commercial/calculate.js.map +1 -0
  42. package/dist/merchant-core/commercial/types.d.ts +91 -0
  43. package/dist/merchant-core/commercial/types.js +17 -0
  44. package/dist/merchant-core/commercial/types.js.map +1 -0
  45. package/dist/merchant-core/contribution/calculate.d.ts +24 -0
  46. package/dist/merchant-core/contribution/calculate.js +172 -0
  47. package/dist/merchant-core/contribution/calculate.js.map +1 -0
  48. package/dist/merchant-core/contribution/repository.d.ts +60 -0
  49. package/dist/merchant-core/contribution/repository.js +121 -0
  50. package/dist/merchant-core/contribution/repository.js.map +1 -0
  51. package/dist/merchant-core/contribution/types.d.ts +97 -0
  52. package/dist/merchant-core/contribution/types.js +24 -0
  53. package/dist/merchant-core/contribution/types.js.map +1 -0
  54. package/dist/merchant-core/followup/lease.d.ts +27 -0
  55. package/dist/merchant-core/followup/lease.js +45 -0
  56. package/dist/merchant-core/followup/lease.js.map +1 -0
  57. package/dist/merchant-core/followup/repository.d.ts +141 -0
  58. package/dist/merchant-core/followup/repository.js +280 -0
  59. package/dist/merchant-core/followup/repository.js.map +1 -0
  60. package/dist/merchant-core/followup/retry.d.ts +27 -0
  61. package/dist/merchant-core/followup/retry.js +61 -0
  62. package/dist/merchant-core/followup/retry.js.map +1 -0
  63. package/dist/merchant-core/followup/service.d.ts +114 -0
  64. package/dist/merchant-core/followup/service.js +191 -0
  65. package/dist/merchant-core/followup/service.js.map +1 -0
  66. package/dist/merchant-core/followup/state.d.ts +25 -0
  67. package/dist/merchant-core/followup/state.js +126 -0
  68. package/dist/merchant-core/followup/state.js.map +1 -0
  69. package/dist/merchant-core/followup/types.d.ts +75 -0
  70. package/dist/merchant-core/followup/types.js +41 -0
  71. package/dist/merchant-core/followup/types.js.map +1 -0
  72. package/dist/merchant-core/metrics/timing.d.ts +63 -0
  73. package/dist/merchant-core/metrics/timing.js +125 -0
  74. package/dist/merchant-core/metrics/timing.js.map +1 -0
  75. package/dist/merchant-core/operating/repository.d.ts +97 -0
  76. package/dist/merchant-core/operating/repository.js +266 -0
  77. package/dist/merchant-core/operating/repository.js.map +1 -0
  78. package/dist/merchant-core/operating/service.d.ts +63 -0
  79. package/dist/merchant-core/operating/service.js +336 -0
  80. package/dist/merchant-core/operating/service.js.map +1 -0
  81. package/dist/merchant-core/operating/types.d.ts +102 -0
  82. package/dist/merchant-core/operating/types.js +24 -0
  83. package/dist/merchant-core/operating/types.js.map +1 -0
  84. package/dist/merchant-core/opportunity/repository.d.ts +128 -0
  85. package/dist/merchant-core/opportunity/repository.js +251 -0
  86. package/dist/merchant-core/opportunity/repository.js.map +1 -0
  87. package/dist/merchant-core/opportunity/service.d.ts +117 -0
  88. package/dist/merchant-core/opportunity/service.js +388 -0
  89. package/dist/merchant-core/opportunity/service.js.map +1 -0
  90. package/dist/merchant-core/opportunity/state.d.ts +33 -0
  91. package/dist/merchant-core/opportunity/state.js +166 -0
  92. package/dist/merchant-core/opportunity/state.js.map +1 -0
  93. package/dist/merchant-core/opportunity/types.d.ts +141 -0
  94. package/dist/merchant-core/opportunity/types.js +42 -0
  95. package/dist/merchant-core/opportunity/types.js.map +1 -0
  96. package/dist/merchant-core/ops/facade.d.ts +159 -0
  97. package/dist/merchant-core/ops/facade.js +213 -0
  98. package/dist/merchant-core/ops/facade.js.map +1 -0
  99. package/dist/merchant-core/ops/schema.d.ts +25 -0
  100. package/dist/merchant-core/ops/schema.js +448 -0
  101. package/dist/merchant-core/ops/schema.js.map +1 -0
  102. package/dist/merchant-core/ops/surface.d.ts +49 -0
  103. package/dist/merchant-core/ops/surface.js +304 -0
  104. package/dist/merchant-core/ops/surface.js.map +1 -0
  105. package/dist/merchant-core/orders/csv.d.ts +36 -0
  106. package/dist/merchant-core/orders/csv.js +173 -0
  107. package/dist/merchant-core/orders/csv.js.map +1 -0
  108. package/dist/merchant-core/orders/model.d.ts +155 -0
  109. package/dist/merchant-core/orders/model.js +237 -0
  110. package/dist/merchant-core/orders/model.js.map +1 -0
  111. package/dist/merchant-core/orders/repository.d.ts +99 -0
  112. package/dist/merchant-core/orders/repository.js +221 -0
  113. package/dist/merchant-core/orders/repository.js.map +1 -0
  114. package/dist/merchant-core/orders/service.d.ts +84 -0
  115. package/dist/merchant-core/orders/service.js +121 -0
  116. package/dist/merchant-core/orders/service.js.map +1 -0
  117. package/dist/merchant-core/orders/shopping-cli-source.d.ts +58 -0
  118. package/dist/merchant-core/orders/shopping-cli-source.js +164 -0
  119. package/dist/merchant-core/orders/shopping-cli-source.js.map +1 -0
  120. package/dist/merchant-core/proposal/engine.d.ts +31 -0
  121. package/dist/merchant-core/proposal/engine.js +348 -0
  122. package/dist/merchant-core/proposal/engine.js.map +1 -0
  123. package/dist/merchant-core/proposal/publish.d.ts +46 -0
  124. package/dist/merchant-core/proposal/publish.js +79 -0
  125. package/dist/merchant-core/proposal/publish.js.map +1 -0
  126. package/dist/merchant-core/proposal/repository.d.ts +60 -0
  127. package/dist/merchant-core/proposal/repository.js +186 -0
  128. package/dist/merchant-core/proposal/repository.js.map +1 -0
  129. package/dist/merchant-core/proposal/service.d.ts +73 -0
  130. package/dist/merchant-core/proposal/service.js +149 -0
  131. package/dist/merchant-core/proposal/service.js.map +1 -0
  132. package/dist/merchant-core/proposal/types.d.ts +163 -0
  133. package/dist/merchant-core/proposal/types.js +25 -0
  134. package/dist/merchant-core/proposal/types.js.map +1 -0
  135. package/dist/product-cli.js +5 -0
  136. package/dist/product-cli.js.map +1 -1
  137. package/package.json +1 -1
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * 经营计划服务(v0.2 §5.1 / FR01–FR04、FR15、FR17)。
18
+ *
19
+ * 不强制的部分(conditions/actions 的具体枚举)留给 M0 冻结;这里先把
20
+ * 不能含糊的约束做死:
21
+ * - 同商家/来源/账户/SKU 的生效区间不得重叠;
22
+ * - 已生效计划不能原地改条款,只能原子切版本;
23
+ * - 暂停不释放区间,恢复必须仍在有效期内且未被撤销;
24
+ * - 所有写命令支持 idempotency_key(同键同内容重放,异内容冲突)。
25
+ */
26
+ import { contentDigest } from "../../negotiation/jcs.js";
27
+ import { MAX_JSON_BYTES, MerchantOperatingPlanRepository, newPlanId, } from "./repository.js";
28
+ import { OperatingPlanError, } from "./types.js";
29
+ function requireText(value, name, max = 256) {
30
+ if (typeof value !== "string" || value.trim() === "" || value.length > max) {
31
+ throw new OperatingPlanError("validation", `${name} 必须是非空字符串(≤${max})`);
32
+ }
33
+ return value;
34
+ }
35
+ function requireTimestamp(value, name) {
36
+ if (typeof value !== "string" || !Number.isFinite(Date.parse(value))) {
37
+ throw new OperatingPlanError("validation", `${name} 必须是可解析的 RFC 3339 时间`);
38
+ }
39
+ return value;
40
+ }
41
+ function requireJsonObject(value, name) {
42
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
43
+ throw new OperatingPlanError("validation", `${name} 必须是对象`);
44
+ }
45
+ const encoded = JSON.stringify(value);
46
+ if (encoded === undefined || Buffer.byteLength(encoded, "utf8") > MAX_JSON_BYTES) {
47
+ throw new OperatingPlanError("validation", `${name} 超过 ${MAX_JSON_BYTES} 字节`);
48
+ }
49
+ return value;
50
+ }
51
+ function validateAuthorization(value) {
52
+ requireText(value?.owner, "authorization.owner", 128);
53
+ if (!Array.isArray(value.scopes) || value.scopes.some((scope) => typeof scope !== "string" || scope.trim() === "")) {
54
+ throw new OperatingPlanError("validation", "authorization.scopes 必须是非空字符串数组");
55
+ }
56
+ if (value.expires_at !== undefined)
57
+ requireTimestamp(value.expires_at, "authorization.expires_at");
58
+ if (value.revoked_at !== undefined)
59
+ requireTimestamp(value.revoked_at, "authorization.revoked_at");
60
+ return {
61
+ owner: value.owner,
62
+ scopes: [...new Set(value.scopes)],
63
+ ...(value.expires_at !== undefined ? { expires_at: value.expires_at } : {}),
64
+ ...(value.revoked_at !== undefined ? { revoked_at: value.revoked_at } : {}),
65
+ };
66
+ }
67
+ function validateInput(input) {
68
+ requireText(input.merchant_id, "merchant_id", 128);
69
+ requireText(input.source, "source", 64);
70
+ requireText(input.source_account_id, "source_account_id", 128);
71
+ requireText(input.sku, "sku", 128);
72
+ if (input.billing_ref !== undefined)
73
+ requireText(input.billing_ref, "billing_ref", 128);
74
+ if (input.template_id !== undefined)
75
+ requireText(input.template_id, "template_id", 128);
76
+ if (input.template_revision !== undefined) {
77
+ if (!Number.isSafeInteger(input.template_revision) || input.template_revision < 1) {
78
+ throw new OperatingPlanError("validation", "template_revision 必须是正整数");
79
+ }
80
+ }
81
+ requireTimestamp(input.effective_from, "effective_from");
82
+ if (input.effective_to !== undefined) {
83
+ requireTimestamp(input.effective_to, "effective_to");
84
+ if (Date.parse(input.effective_to) <= Date.parse(input.effective_from)) {
85
+ throw new OperatingPlanError("invalid_interval", "effective_to 必须晚于 effective_from");
86
+ }
87
+ }
88
+ requireJsonObject(input.conditions, "conditions");
89
+ requireJsonObject(input.actions, "actions");
90
+ if (input.private_rules_ref !== undefined)
91
+ requireText(input.private_rules_ref, "private_rules_ref", 256);
92
+ return {
93
+ ...input,
94
+ authorization: validateAuthorization(input.authorization),
95
+ };
96
+ }
97
+ function authorizationActive(authorization, at) {
98
+ if (authorization.revoked_at !== undefined && Date.parse(authorization.revoked_at) <= Date.parse(at)) {
99
+ return { ok: false, reason: "authorization_revoked" };
100
+ }
101
+ if (authorization.expires_at !== undefined && Date.parse(authorization.expires_at) <= Date.parse(at)) {
102
+ return { ok: false, reason: "plan_expired" };
103
+ }
104
+ return { ok: true };
105
+ }
106
+ function inWindow(plan, at) {
107
+ return (Date.parse(plan.effective_from) <= Date.parse(at) &&
108
+ (plan.effective_to === undefined || Date.parse(at) < Date.parse(plan.effective_to)));
109
+ }
110
+ export class MerchantOperatingPlanService {
111
+ repository;
112
+ now;
113
+ constructor(options) {
114
+ this.repository = options.repository;
115
+ this.now = options.now;
116
+ }
117
+ create(input, command = {}) {
118
+ const normalized = validateInput(input);
119
+ return this.withIdempotency(normalized.merchant_id, "operating_plans.create", input, command, () => this.repository.transaction(() => {
120
+ const overlap = this.repository.findOverlaps({
121
+ merchantId: normalized.merchant_id,
122
+ source: normalized.source,
123
+ sourceAccountId: normalized.source_account_id,
124
+ sku: normalized.sku,
125
+ effectiveFrom: normalized.effective_from,
126
+ ...(normalized.effective_to !== undefined ? { effectiveTo: normalized.effective_to } : {}),
127
+ });
128
+ if (overlap.length > 0) {
129
+ throw new OperatingPlanError("interval_overlap", "同一商品在相同生效区间内已存在经营计划");
130
+ }
131
+ return this.repository.insert({
132
+ ...normalized,
133
+ plan_id: newPlanId(),
134
+ status: "ACTIVE",
135
+ revision: 1,
136
+ }, command.actor ?? normalized.authorization.owner);
137
+ }));
138
+ }
139
+ update(input, command = {}) {
140
+ const current = this.repository.getById(input.merchant_id, input.plan_id);
141
+ if (current === undefined) {
142
+ throw new OperatingPlanError("not_found", `plan ${input.plan_id} not found`);
143
+ }
144
+ if (Date.parse(this.now()) >= Date.parse(current.effective_from)) {
145
+ throw new OperatingPlanError("invalid_interval", "计划已生效,不能原地修改条款;请用版本切换(switchVersion)");
146
+ }
147
+ return this.withIdempotency(current.merchant_id, "operating_plans.update", input, command, () => this.repository.transaction(() => {
148
+ const patch = input.patch;
149
+ if (patch.effective_from !== undefined) {
150
+ throw new OperatingPlanError("validation", "effective_from 只能通过版本切换修改");
151
+ }
152
+ if (patch.effective_to !== undefined) {
153
+ requireTimestamp(patch.effective_to, "effective_to");
154
+ }
155
+ const merged = validateInput({ ...current, ...patch, merchant_id: current.merchant_id });
156
+ const overlap = this.repository.findOverlaps({
157
+ merchantId: merged.merchant_id,
158
+ source: merged.source,
159
+ sourceAccountId: merged.source_account_id,
160
+ sku: merged.sku,
161
+ effectiveFrom: merged.effective_from,
162
+ ...(merged.effective_to !== undefined ? { effectiveTo: merged.effective_to } : {}),
163
+ excludePlanId: current.plan_id,
164
+ });
165
+ if (overlap.length > 0) {
166
+ throw new OperatingPlanError("interval_overlap", "修改后的生效区间与其他计划重叠");
167
+ }
168
+ return this.repository.update({
169
+ planId: current.plan_id,
170
+ merchantId: current.merchant_id,
171
+ expectedRevision: input.expected_revision,
172
+ patch: merged,
173
+ }, command.actor ?? merged.authorization.owner);
174
+ }));
175
+ }
176
+ switchVersion(input, command = {}) {
177
+ const current = this.repository.getById(input.merchant_id, input.plan_id);
178
+ if (current === undefined) {
179
+ throw new OperatingPlanError("not_found", `plan ${input.plan_id} not found`);
180
+ }
181
+ requireTimestamp(input.effective_from, "effective_from");
182
+ if (Date.parse(input.effective_from) <= Date.parse(this.now())) {
183
+ throw new OperatingPlanError("invalid_interval", "新版本必须从现在之后生效,不能回写历史");
184
+ }
185
+ if (Date.parse(input.effective_from) <= Date.parse(current.effective_from)) {
186
+ throw new OperatingPlanError("invalid_interval", "新版本生效时间必须晚于当前版本");
187
+ }
188
+ if (current.effective_to !== undefined && Date.parse(input.effective_from) >= Date.parse(current.effective_to)) {
189
+ throw new OperatingPlanError("invalid_interval", "新版本生效时间必须落在当前计划区间内");
190
+ }
191
+ return this.withIdempotency(current.merchant_id, "operating_plans.switch", input, command, () => this.repository.transaction(() => {
192
+ const merged = validateInput({
193
+ ...current,
194
+ ...input.patch,
195
+ merchant_id: current.merchant_id,
196
+ source: current.source,
197
+ source_account_id: current.source_account_id,
198
+ sku: current.sku,
199
+ effective_from: input.effective_from,
200
+ ...(input.patch.effective_to !== undefined
201
+ ? { effective_to: input.patch.effective_to }
202
+ : current.effective_to !== undefined
203
+ ? { effective_to: current.effective_to }
204
+ : {}),
205
+ });
206
+ const overlap = this.repository.findOverlaps({
207
+ merchantId: merged.merchant_id,
208
+ source: merged.source,
209
+ sourceAccountId: merged.source_account_id,
210
+ sku: merged.sku,
211
+ effectiveFrom: merged.effective_from,
212
+ ...(merged.effective_to !== undefined ? { effectiveTo: merged.effective_to } : {}),
213
+ excludePlanId: current.plan_id,
214
+ });
215
+ if (overlap.length > 0) {
216
+ throw new OperatingPlanError("interval_overlap", "新版本生效区间与其他计划重叠");
217
+ }
218
+ const previous = this.repository.closeInterval({
219
+ planId: current.plan_id,
220
+ merchantId: current.merchant_id,
221
+ expectedRevision: input.expected_revision,
222
+ effectiveTo: input.effective_from,
223
+ }, command.actor ?? merged.authorization.owner);
224
+ const next = this.repository.insert({ ...merged, plan_id: newPlanId(), status: "ACTIVE", revision: 1 }, command.actor ?? merged.authorization.owner);
225
+ return { previous, next };
226
+ }));
227
+ }
228
+ pause(input, command = {}) {
229
+ const current = this.repository.getById(input.merchant_id, input.plan_id);
230
+ if (current === undefined)
231
+ throw new OperatingPlanError("not_found", `plan ${input.plan_id} not found`);
232
+ if (current.status === "PAUSED")
233
+ return current;
234
+ return this.withIdempotency(current.merchant_id, "operating_plans.pause", input, command, () => this.repository.transaction(() => this.repository.update({
235
+ planId: current.plan_id,
236
+ merchantId: current.merchant_id,
237
+ expectedRevision: input.expected_revision,
238
+ status: "PAUSED",
239
+ }, input.actor ?? command.actor ?? current.authorization.owner)));
240
+ }
241
+ resume(input, command = {}) {
242
+ const current = this.repository.getById(input.merchant_id, input.plan_id);
243
+ if (current === undefined)
244
+ throw new OperatingPlanError("not_found", `plan ${input.plan_id} not found`);
245
+ if (current.status === "ACTIVE")
246
+ return current;
247
+ const at = this.now();
248
+ if (!inWindow(current, at)) {
249
+ throw new OperatingPlanError("plan_expired", "计划已过有效期,不能恢复");
250
+ }
251
+ const authorization = authorizationActive(current.authorization, at);
252
+ if (!authorization.ok) {
253
+ throw new OperatingPlanError(authorization.reason, "授权已撤销或到期,不能恢复计划");
254
+ }
255
+ return this.withIdempotency(current.merchant_id, "operating_plans.resume", input, command, () => this.repository.transaction(() => this.repository.update({
256
+ planId: current.plan_id,
257
+ merchantId: current.merchant_id,
258
+ expectedRevision: input.expected_revision,
259
+ status: "ACTIVE",
260
+ }, input.actor ?? command.actor ?? current.authorization.owner)));
261
+ }
262
+ getEffective(input) {
263
+ return this.repository.getEffective({
264
+ merchantId: input.merchantId,
265
+ source: input.source,
266
+ sourceAccountId: input.sourceAccountId,
267
+ sku: input.sku,
268
+ at: input.at ?? this.now(),
269
+ });
270
+ }
271
+ list(merchantId, filter = {}) {
272
+ return this.repository.list(merchantId, filter);
273
+ }
274
+ /**
275
+ * 三级就绪检查(FR03)。缺订单源/成本时如实阻断对应级别,不用默认值
276
+ * 伪装成“已具备”。
277
+ */
278
+ readiness(plan, input) {
279
+ const at = input.at;
280
+ if (!Number.isFinite(Date.parse(at))) {
281
+ throw new OperatingPlanError("validation", "at 必须是可解析的 RFC 3339 时间");
282
+ }
283
+ const blocked = (blocker) => ({ ok: false, blockers: [blocker] });
284
+ const receive = [];
285
+ if (!inWindow(plan, at))
286
+ receive.push("outside_effective_window");
287
+ const authorization = authorizationActive(plan.authorization, at);
288
+ if (!authorization.ok) {
289
+ receive.push(authorization.reason === "authorization_revoked"
290
+ ? "authorization_revoked"
291
+ : "authorization_expired");
292
+ }
293
+ // 缺数量范围/单位不阻断接收(FR03:仍可接收并补问),只阻断正式报价。
294
+ const quote = [];
295
+ if (plan.status !== "ACTIVE")
296
+ quote.push("plan_paused");
297
+ if (receive.length > 0)
298
+ quote.push(...receive);
299
+ if (plan.conditions.quantity_range === undefined && plan.conditions.quantity_min === undefined) {
300
+ quote.push("missing_quantity_range");
301
+ }
302
+ if (plan.conditions.allowed_units === undefined)
303
+ quote.push("missing_allowed_units");
304
+ if (input.contribution_gate_enabled === true && input.cost_complete !== true) {
305
+ quote.push(input.cost_complete === false ? "missing_cost" : "contribution_unknown");
306
+ }
307
+ const orders = [];
308
+ if (input.order_source_ready !== true)
309
+ orders.push("order_source_unavailable");
310
+ return {
311
+ can_receive_demand: receive.length === 0 ? { ok: true, blockers: [] } : { ok: false, blockers: receive },
312
+ can_publish_quote: quote.length === 0 ? { ok: true, blockers: [] } : { ok: false, blockers: quote },
313
+ can_verify_orders: orders.length === 0 ? { ok: true, blockers: [] } : blocked("order_source_unavailable"),
314
+ };
315
+ }
316
+ withIdempotency(merchantId, operation, request, command, work) {
317
+ const key = command.idempotency_key;
318
+ if (key === undefined)
319
+ return work();
320
+ requireText(key, "idempotency_key", 128);
321
+ const digest = contentDigest({ operation, request });
322
+ const existing = this.repository.readIdempotent(merchantId, key);
323
+ if (existing !== undefined) {
324
+ if (existing.operation !== operation || existing.requestDigest !== digest) {
325
+ throw new OperatingPlanError("idempotency_conflict", "同一幂等键已用于不同内容");
326
+ }
327
+ return JSON.parse(existing.result);
328
+ }
329
+ return this.repository.transaction(() => {
330
+ const result = work();
331
+ this.repository.saveIdempotent({ merchantId, key, operation, request, result });
332
+ return result;
333
+ });
334
+ }
335
+ }
336
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/merchant-core/operating/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;GASG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EACL,cAAc,EACd,+BAA+B,EAC/B,SAAS,GACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,kBAAkB,GAQnB,MAAM,YAAY,CAAC;AAapB,SAAS,WAAW,CAAC,KAAc,EAAE,IAAY,EAAE,GAAG,GAAG,GAAG;IAC1D,IA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QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;QACjE,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,IAAI,QAAQ,CAAC,SAAS,KAAK,SAAS,IAAI,QAAQ,CAAC,aAAa,KAAK,MAAM,EAAE,CAAC;gBAC1E,MAAM,IAAI,kBAAkB,CAAC,sBAAsB,EAAE,cAAc,CAAC,CAAC;YACvE,CAAC;YACD,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAM,CAAC;QAC1C,CAAC;QACD,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,EAAE;YACtC,MAAM,MAAM,GAAG,IAAI,EAAE,CAAC;YACtB,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;YAChF,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * 商品经营计划(v0.2 §5.1 / FR01–FR04)。
18
+ *
19
+ * 计划只引用外部商品身份与事实,不复制库存/价格主数据。conditions 与
20
+ * actions 是商家经营条件的版本化 JSON(M0 冻结字段前只做结构边界校验,
21
+ * 不提前发明最终枚举);生效区间、暂停/恢复、版本切换是强约束。
22
+ */
23
+ export type OperatingPlanStatus = "ACTIVE" | "PAUSED";
24
+ export type OperatingPlanErrorCode = "validation" | "not_found" | "version_conflict" | "interval_overlap" | "invalid_interval" | "plan_paused" | "plan_expired" | "authorization_revoked" | "idempotency_conflict" | "corrupt_plan_intervals";
25
+ export declare class OperatingPlanError extends Error {
26
+ readonly code: OperatingPlanErrorCode;
27
+ constructor(code: OperatingPlanErrorCode, message: string);
28
+ }
29
+ export interface OperatingPlanAuthorization {
30
+ /** 计划负责人(商家主体账号)。 */
31
+ owner: string;
32
+ /** 授权范围(例如 publish_quote / follow_up / approve_exception)。 */
33
+ scopes: string[];
34
+ /** 授权到期时间;缺省表示随计划区间。 */
35
+ expires_at?: string;
36
+ /** 显式撤销时间;撤销后不得恢复为新动作的执行依据。 */
37
+ revoked_at?: string;
38
+ }
39
+ export interface OperatingPlanInput {
40
+ merchant_id: string;
41
+ /** 商品源命名空间(例如 shopping-cli)。 */
42
+ source: string;
43
+ /** 来源账户;外部连接器/凭据确定,不接受请求正文自报。 */
44
+ source_account_id: string;
45
+ sku: string;
46
+ /** 计费商品引用(可选;D02 签认前不启用计费)。 */
47
+ billing_ref?: string;
48
+ template_id?: string;
49
+ template_revision?: number;
50
+ /** 生效区间,UTC,半开 [effective_from, effective_to)。 */
51
+ effective_from: string;
52
+ effective_to?: string;
53
+ /** 适用需求/规格/地区/交付条件;字段枚举由 M0 冻结。 */
54
+ conditions: Record<string, unknown>;
55
+ /** 允许动作:促销、阶梯、替代、交付方式、人工例外入口。 */
56
+ actions: Record<string, unknown>;
57
+ authorization: OperatingPlanAuthorization;
58
+ /** 私有成本/底价规则引用;只存引用,不存成本本身。 */
59
+ private_rules_ref?: string;
60
+ }
61
+ export interface OperatingPlan extends OperatingPlanInput {
62
+ plan_id: string;
63
+ status: OperatingPlanStatus;
64
+ revision: number;
65
+ created_at: string;
66
+ updated_at: string;
67
+ }
68
+ export interface UpdateOperatingPlanInput {
69
+ merchant_id: string;
70
+ plan_id: string;
71
+ expected_revision: number;
72
+ /** 只允许修改未开始生效的计划;已生效计划用 switchVersion。 */
73
+ patch: Partial<Omit<OperatingPlanInput, "merchant_id" | "source" | "source_account_id" | "sku">>;
74
+ }
75
+ export interface SwitchPlanVersionInput {
76
+ merchant_id: string;
77
+ plan_id: string;
78
+ expected_revision: number;
79
+ /** 新版本生效时间;必须晚于当前版本生效时间且落在当前区间内。 */
80
+ effective_from: string;
81
+ /** 新版本覆盖项;未提供的字段沿用旧版本。 */
82
+ patch: Partial<Omit<OperatingPlanInput, "merchant_id" | "source" | "source_account_id" | "sku">>;
83
+ }
84
+ export interface PlanReadinessInput {
85
+ /** 当前时间(RFC 3339)。 */
86
+ at: string;
87
+ /** 成本快照是否完整;缺省未知。 */
88
+ cost_complete?: boolean;
89
+ /** 是否启用最低贡献硬门。 */
90
+ contribution_gate_enabled?: boolean;
91
+ /** 外部订单源是否已接通且可用。 */
92
+ order_source_ready?: boolean;
93
+ }
94
+ export interface ReadinessCheck {
95
+ ok: boolean;
96
+ blockers: string[];
97
+ }
98
+ export interface OperatingPlanReadiness {
99
+ can_receive_demand: ReadinessCheck;
100
+ can_publish_quote: ReadinessCheck;
101
+ can_verify_orders: ReadinessCheck;
102
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export class OperatingPlanError extends Error {
17
+ code;
18
+ constructor(code, message) {
19
+ super(message);
20
+ this.name = "OperatingPlanError";
21
+ this.code = code;
22
+ }
23
+ }
24
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/operating/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAwBH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,IAAI,CAAyB;IACtC,YAAY,IAA4B,EAAE,OAAe;QACvD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { DatabaseSync } from "node:sqlite";
17
+ import { type OpportunityLink, type OpportunitySource, type OpportunitySourceEvent, type OpportunityStatus, type RequirementField, type RequirementSnapshot, type SalesOpportunity } from "./types.js";
18
+ export interface OpportunityRepositoryOptions {
19
+ db: DatabaseSync;
20
+ now: () => string;
21
+ }
22
+ export declare class OpportunityRepository {
23
+ private readonly db;
24
+ private readonly now;
25
+ private inTx;
26
+ constructor(options: OpportunityRepositoryOptions);
27
+ transaction<T>(work: () => T): T;
28
+ getSourceByExternalId(input: {
29
+ merchantId: string;
30
+ sourceNamespace: string;
31
+ sourceAccountId: string;
32
+ externalInquiryId: string;
33
+ }): OpportunitySource | undefined;
34
+ getSource(merchantId: string, sourceId: string): OpportunitySource | undefined;
35
+ insertSource(input: Omit<OpportunitySource, "source_id" | "revision" | "created_at" | "updated_at">): OpportunitySource;
36
+ touchSource(merchantId: string, sourceId: string): OpportunitySource;
37
+ getSourceEvent(sourceId: string, externalEventId: string): OpportunitySourceEvent | undefined;
38
+ insertSourceEvent(input: {
39
+ sourceId: string;
40
+ externalEventId: string;
41
+ eventKind: string;
42
+ payload: unknown;
43
+ receivedAt: string;
44
+ }): OpportunitySourceEvent;
45
+ getOpportunity(merchantId: string, opportunityId: string): SalesOpportunity | undefined;
46
+ listOpportunities(merchantId: string, filter?: {
47
+ status?: OpportunityStatus;
48
+ owner?: string;
49
+ }): SalesOpportunity[];
50
+ insertOpportunity(input: {
51
+ merchantId: string;
52
+ status: OpportunityStatus;
53
+ owner: string;
54
+ nextAction?: string;
55
+ nextActionDueAt?: string;
56
+ waitingReason?: string;
57
+ rfqCaseId?: string;
58
+ }): SalesOpportunity;
59
+ updateOpportunity(input: {
60
+ merchantId: string;
61
+ opportunityId: string;
62
+ expectedRevision: number;
63
+ status: OpportunityStatus;
64
+ owner?: string;
65
+ nextAction?: string | null;
66
+ nextActionDueAt?: string | null;
67
+ waitingReason?: string | null;
68
+ }): SalesOpportunity;
69
+ getLinkBySource(sourceId: string): OpportunityLink | undefined;
70
+ insertLink(input: {
71
+ merchantId: string;
72
+ opportunityId: string;
73
+ sourceId: string;
74
+ basis: string;
75
+ confirmedBy: string;
76
+ confirmedAt: string;
77
+ }): OpportunityLink;
78
+ getLink(linkId: string): OpportunityLink | undefined;
79
+ updateLink(input: {
80
+ linkId: string;
81
+ merchantId: string;
82
+ expectedRevision: number;
83
+ opportunityId: string;
84
+ status: "LINKED" | "UNLINKED";
85
+ basis: string;
86
+ confirmedBy: string;
87
+ confirmedAt: string;
88
+ }): OpportunityLink;
89
+ appendLinkEvent(input: {
90
+ merchantId: string;
91
+ linkId: string;
92
+ opportunityId: string;
93
+ sourceId: string;
94
+ event: string;
95
+ actor: string;
96
+ basis: string;
97
+ }): void;
98
+ getLatestRequirement(merchantId: string, opportunityId: string): RequirementSnapshot | undefined;
99
+ insertRequirement(input: {
100
+ merchantId: string;
101
+ opportunityId: string;
102
+ revision: number;
103
+ fields: RequirementField[];
104
+ createdBy: string;
105
+ }): RequirementSnapshot;
106
+ appendOpportunityEvent(input: {
107
+ merchantId: string;
108
+ opportunityId: string;
109
+ event: string;
110
+ fromStatus: OpportunityStatus;
111
+ toStatus: OpportunityStatus;
112
+ actor: string;
113
+ sideEffects: string[];
114
+ detail: Record<string, unknown>;
115
+ }): void;
116
+ readIdempotent(merchantId: string, key: string): {
117
+ operation: string;
118
+ requestDigest: string;
119
+ result: string;
120
+ } | undefined;
121
+ saveIdempotent(input: {
122
+ merchantId: string;
123
+ key: string;
124
+ operation: string;
125
+ request: unknown;
126
+ result: unknown;
127
+ }): void;
128
+ }