@harrylabsj/kiwi 0.12.13 → 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 (140) 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 +59 -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/cloud/merchant-connect.d.ts +8 -0
  29. package/dist/cloud/merchant-connect.js +3 -0
  30. package/dist/cloud/merchant-connect.js.map +1 -1
  31. package/dist/http/merchant-management/api.d.ts +3 -0
  32. package/dist/http/merchant-management/api.js +419 -0
  33. package/dist/http/merchant-management/api.js.map +1 -1
  34. package/dist/http/merchant-management/page.js +95 -1
  35. package/dist/http/merchant-management/page.js.map +1 -1
  36. package/dist/merchant/application/actor.d.ts +1 -1
  37. package/dist/merchant/application/actor.js +43 -1
  38. package/dist/merchant/application/actor.js.map +1 -1
  39. package/dist/merchant/quote-calculator.d.ts +7 -0
  40. package/dist/merchant/quote-calculator.js +35 -24
  41. package/dist/merchant/quote-calculator.js.map +1 -1
  42. package/dist/merchant-core/commercial/calculate.d.ts +18 -0
  43. package/dist/merchant-core/commercial/calculate.js +196 -0
  44. package/dist/merchant-core/commercial/calculate.js.map +1 -0
  45. package/dist/merchant-core/commercial/types.d.ts +91 -0
  46. package/dist/merchant-core/commercial/types.js +17 -0
  47. package/dist/merchant-core/commercial/types.js.map +1 -0
  48. package/dist/merchant-core/contribution/calculate.d.ts +24 -0
  49. package/dist/merchant-core/contribution/calculate.js +172 -0
  50. package/dist/merchant-core/contribution/calculate.js.map +1 -0
  51. package/dist/merchant-core/contribution/repository.d.ts +60 -0
  52. package/dist/merchant-core/contribution/repository.js +121 -0
  53. package/dist/merchant-core/contribution/repository.js.map +1 -0
  54. package/dist/merchant-core/contribution/types.d.ts +97 -0
  55. package/dist/merchant-core/contribution/types.js +24 -0
  56. package/dist/merchant-core/contribution/types.js.map +1 -0
  57. package/dist/merchant-core/followup/lease.d.ts +27 -0
  58. package/dist/merchant-core/followup/lease.js +45 -0
  59. package/dist/merchant-core/followup/lease.js.map +1 -0
  60. package/dist/merchant-core/followup/repository.d.ts +141 -0
  61. package/dist/merchant-core/followup/repository.js +280 -0
  62. package/dist/merchant-core/followup/repository.js.map +1 -0
  63. package/dist/merchant-core/followup/retry.d.ts +27 -0
  64. package/dist/merchant-core/followup/retry.js +61 -0
  65. package/dist/merchant-core/followup/retry.js.map +1 -0
  66. package/dist/merchant-core/followup/service.d.ts +114 -0
  67. package/dist/merchant-core/followup/service.js +191 -0
  68. package/dist/merchant-core/followup/service.js.map +1 -0
  69. package/dist/merchant-core/followup/state.d.ts +25 -0
  70. package/dist/merchant-core/followup/state.js +126 -0
  71. package/dist/merchant-core/followup/state.js.map +1 -0
  72. package/dist/merchant-core/followup/types.d.ts +75 -0
  73. package/dist/merchant-core/followup/types.js +41 -0
  74. package/dist/merchant-core/followup/types.js.map +1 -0
  75. package/dist/merchant-core/metrics/timing.d.ts +63 -0
  76. package/dist/merchant-core/metrics/timing.js +125 -0
  77. package/dist/merchant-core/metrics/timing.js.map +1 -0
  78. package/dist/merchant-core/operating/repository.d.ts +97 -0
  79. package/dist/merchant-core/operating/repository.js +266 -0
  80. package/dist/merchant-core/operating/repository.js.map +1 -0
  81. package/dist/merchant-core/operating/service.d.ts +63 -0
  82. package/dist/merchant-core/operating/service.js +336 -0
  83. package/dist/merchant-core/operating/service.js.map +1 -0
  84. package/dist/merchant-core/operating/types.d.ts +102 -0
  85. package/dist/merchant-core/operating/types.js +24 -0
  86. package/dist/merchant-core/operating/types.js.map +1 -0
  87. package/dist/merchant-core/opportunity/repository.d.ts +128 -0
  88. package/dist/merchant-core/opportunity/repository.js +251 -0
  89. package/dist/merchant-core/opportunity/repository.js.map +1 -0
  90. package/dist/merchant-core/opportunity/service.d.ts +117 -0
  91. package/dist/merchant-core/opportunity/service.js +388 -0
  92. package/dist/merchant-core/opportunity/service.js.map +1 -0
  93. package/dist/merchant-core/opportunity/state.d.ts +33 -0
  94. package/dist/merchant-core/opportunity/state.js +166 -0
  95. package/dist/merchant-core/opportunity/state.js.map +1 -0
  96. package/dist/merchant-core/opportunity/types.d.ts +141 -0
  97. package/dist/merchant-core/opportunity/types.js +42 -0
  98. package/dist/merchant-core/opportunity/types.js.map +1 -0
  99. package/dist/merchant-core/ops/facade.d.ts +159 -0
  100. package/dist/merchant-core/ops/facade.js +213 -0
  101. package/dist/merchant-core/ops/facade.js.map +1 -0
  102. package/dist/merchant-core/ops/schema.d.ts +25 -0
  103. package/dist/merchant-core/ops/schema.js +448 -0
  104. package/dist/merchant-core/ops/schema.js.map +1 -0
  105. package/dist/merchant-core/ops/surface.d.ts +49 -0
  106. package/dist/merchant-core/ops/surface.js +304 -0
  107. package/dist/merchant-core/ops/surface.js.map +1 -0
  108. package/dist/merchant-core/orders/csv.d.ts +36 -0
  109. package/dist/merchant-core/orders/csv.js +173 -0
  110. package/dist/merchant-core/orders/csv.js.map +1 -0
  111. package/dist/merchant-core/orders/model.d.ts +155 -0
  112. package/dist/merchant-core/orders/model.js +237 -0
  113. package/dist/merchant-core/orders/model.js.map +1 -0
  114. package/dist/merchant-core/orders/repository.d.ts +99 -0
  115. package/dist/merchant-core/orders/repository.js +221 -0
  116. package/dist/merchant-core/orders/repository.js.map +1 -0
  117. package/dist/merchant-core/orders/service.d.ts +84 -0
  118. package/dist/merchant-core/orders/service.js +121 -0
  119. package/dist/merchant-core/orders/service.js.map +1 -0
  120. package/dist/merchant-core/orders/shopping-cli-source.d.ts +58 -0
  121. package/dist/merchant-core/orders/shopping-cli-source.js +164 -0
  122. package/dist/merchant-core/orders/shopping-cli-source.js.map +1 -0
  123. package/dist/merchant-core/proposal/engine.d.ts +31 -0
  124. package/dist/merchant-core/proposal/engine.js +348 -0
  125. package/dist/merchant-core/proposal/engine.js.map +1 -0
  126. package/dist/merchant-core/proposal/publish.d.ts +46 -0
  127. package/dist/merchant-core/proposal/publish.js +79 -0
  128. package/dist/merchant-core/proposal/publish.js.map +1 -0
  129. package/dist/merchant-core/proposal/repository.d.ts +60 -0
  130. package/dist/merchant-core/proposal/repository.js +186 -0
  131. package/dist/merchant-core/proposal/repository.js.map +1 -0
  132. package/dist/merchant-core/proposal/service.d.ts +73 -0
  133. package/dist/merchant-core/proposal/service.js +149 -0
  134. package/dist/merchant-core/proposal/service.js.map +1 -0
  135. package/dist/merchant-core/proposal/types.d.ts +163 -0
  136. package/dist/merchant-core/proposal/types.js +25 -0
  137. package/dist/merchant-core/proposal/types.js.map +1 -0
  138. package/dist/product-cli.js +5 -0
  139. package/dist/product-cli.js.map +1 -1
  140. package/package.json +1 -1
@@ -0,0 +1,91 @@
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 §9.2、FR14)。
18
+ *
19
+ * 这一层只做确定性算术:选择公开促销价、应用行优惠、按税基拆分、汇总
20
+ * 总额并给出有效期。它不读取私有底价以外的商家策略,也不接受模型提交
21
+ * 的最终金额。RFQ 与 KNP 适配层共享同一输入,保证同事实/同规则得到
22
+ * 同结果;wire 形状与发布对象由各自适配层负责。
23
+ */
24
+ import type { PricingResult, TaxBasis } from "../rfq/types.js";
25
+ export declare const COMMERCIAL_CALCULATION_VERSION: "kiwi-commercial/1";
26
+ export type CommercialCalculationVersion = typeof COMMERCIAL_CALCULATION_VERSION;
27
+ /** 公开促销候选(金额为最小货币单位整数,币种固定 CNY)。 */
28
+ export interface CommercialPromotion {
29
+ promotion_id: string;
30
+ revision: number;
31
+ unit_price_minor: number;
32
+ /** 促销结束时间(RFC 3339);到期后不得再被选中。 */
33
+ ends_at: string;
34
+ /** 数值越大优先级越高;同优先级按价格低→高、promotion_id 字典序。 */
35
+ priority: number;
36
+ }
37
+ /** 税口径:basis 与 bps 必须显式给出,未知不得进入计价。 */
38
+ export interface CommercialTax {
39
+ basis: TaxBasis;
40
+ rate_bps: number;
41
+ }
42
+ export interface CommercialQuoteInput {
43
+ /** P0 仅 CNY(与 PricingEngine / KNP Money 一致)。 */
44
+ currency: "CNY";
45
+ quantity: number;
46
+ /** 公开 list 单价(minor)。 */
47
+ base_unit_price_minor: number;
48
+ /** 公开促销候选;空数组等价没有促销。 */
49
+ promotions?: readonly CommercialPromotion[];
50
+ /**
51
+ * 私有 floor(minor)。只用于判断“能否自动报价”,绝不进入返回的
52
+ * 价格、投影或日志。低于 floor 时返回 approval_required。
53
+ */
54
+ private_floor_minor?: number;
55
+ /** 行级优惠(minor);不得大于行基数。 */
56
+ line_discount_minor?: number;
57
+ /** 商家承担的配送金额(minor);0/缺省表示包邮。 */
58
+ shipping_amount_minor?: number;
59
+ line_tax: CommercialTax;
60
+ shipping_tax: CommercialTax;
61
+ /** 报价有效期上限(秒);从 now 起算。 */
62
+ quote_ttl_seconds?: number;
63
+ /** 商品事实或商家策略给出的额外有效期上限(RFC 3339)。 */
64
+ valid_until_cap?: string;
65
+ /** 计算基准时间(RFC 3339)。 */
66
+ now: string;
67
+ }
68
+ export interface CommercialPriceSourceBase {
69
+ kind: "base";
70
+ }
71
+ export interface CommercialPriceSourcePromotion {
72
+ kind: "promotion";
73
+ promotion_id: string;
74
+ revision: number;
75
+ }
76
+ export type CommercialPriceSource = CommercialPriceSourceBase | CommercialPriceSourcePromotion;
77
+ export type CommercialQuoteResult = {
78
+ status: "quoted";
79
+ calculation_version: CommercialCalculationVersion;
80
+ currency: "CNY";
81
+ quantity: number;
82
+ unit_price_minor: number;
83
+ source: CommercialPriceSource;
84
+ pricing: PricingResult;
85
+ /** 实际生效的最早截止时间;没有约束时为 null。 */
86
+ valid_until: string | null;
87
+ } | {
88
+ status: "approval_required";
89
+ calculation_version: CommercialCalculationVersion;
90
+ reason: "below_private_floor" | "invalid_promotion";
91
+ };
@@ -0,0 +1,17 @@
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 const COMMERCIAL_CALCULATION_VERSION = "kiwi-commercial/1";
17
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/commercial/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAaH,MAAM,CAAC,MAAM,8BAA8B,GAAG,mBAA4B,CAAC"}
@@ -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
+ /**
17
+ * 贡献核算(v0.2 §13.1–§13.4 / FR26–FR28)。
18
+ *
19
+ * 纯整数运算;未知成本不填 0;贡献率用万分比交叉相乘判断阈值,展示舍入
20
+ * 不改变发布结论。现金退款与成本恢复分开:退款只减收入,只有显式成本
21
+ * 恢复才减成本。
22
+ */
23
+ import { type ContributionAssessment, type ContributionInput } from "./types.js";
24
+ export declare function calculateContribution(input: ContributionInput): ContributionAssessment;
@@ -0,0 +1,172 @@
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 §13.1–§13.4 / FR26–FR28)。
18
+ *
19
+ * 纯整数运算;未知成本不填 0;贡献率用万分比交叉相乘判断阈值,展示舍入
20
+ * 不改变发布结论。现金退款与成本恢复分开:退款只减收入,只有显式成本
21
+ * 恢复才减成本。
22
+ */
23
+ import { ContributionError, } from "./types.js";
24
+ const MAX_MINOR = BigInt(1_000_000_000_000);
25
+ function requireMinor(value, name, allowNegative = false) {
26
+ if (typeof value !== "number" ||
27
+ !Number.isSafeInteger(value) ||
28
+ value > Number(MAX_MINOR) ||
29
+ (!allowNegative && value < 0)) {
30
+ throw new ContributionError(`${name} 必须是 [${allowNegative ? -Number(MAX_MINOR) : 0}, ${Number(MAX_MINOR)}] 内的安全整数`);
31
+ }
32
+ return value;
33
+ }
34
+ function optionalMinor(value, name) {
35
+ if (value === undefined)
36
+ return 0;
37
+ return requireMinor(value, name);
38
+ }
39
+ /** 非负净收入的四舍五入整数除法;负分子按远离 0 取整,仅用于展示。 */
40
+ function roundDiv(numerator, denominator) {
41
+ if (denominator <= 0n)
42
+ throw new ContributionError("rate denominator must be positive");
43
+ const negative = numerator < 0n;
44
+ const abs = negative ? -numerator : numerator;
45
+ const rounded = (2n * abs + denominator) / (2n * denominator);
46
+ const value = negative ? -rounded : rounded;
47
+ if (value > BigInt(Number.MAX_SAFE_INTEGER) || value < BigInt(Number.MIN_SAFE_INTEGER)) {
48
+ throw new ContributionError("贡献率超出安全整数范围");
49
+ }
50
+ return Number(value);
51
+ }
52
+ function evaluateGate(input, contributionMinor, netRevenueMinor) {
53
+ const gate = input.gate;
54
+ if (gate === undefined || !gate.enabled) {
55
+ return { status: "NOT_EVALUATED", reasons: ["gate_disabled"] };
56
+ }
57
+ const reasons = [];
58
+ if (input.completeness === "INCOMPLETE") {
59
+ // 缺项没有可证明上限:独立例外也不能当成硬门满足。
60
+ return { status: "BLOCKED", reasons: ["missing_cost"] };
61
+ }
62
+ if (input.completeness === "ESTIMATED") {
63
+ if (gate.exception_authorized !== true) {
64
+ return { status: "BLOCKED", reasons: ["estimated_requires_exception"] };
65
+ }
66
+ reasons.push("estimated_exception");
67
+ }
68
+ if (contributionMinor === null) {
69
+ return { status: "BLOCKED", reasons: [...reasons, "contribution_unknown"] };
70
+ }
71
+ if (gate.min_contribution_minor !== undefined) {
72
+ const minimum = requireMinor(gate.min_contribution_minor, "gate.min_contribution_minor");
73
+ if (contributionMinor < minimum)
74
+ reasons.push("below_min_contribution");
75
+ }
76
+ if (gate.min_rate_bps !== undefined) {
77
+ const minRate = requireMinor(gate.min_rate_bps, "gate.min_rate_bps");
78
+ if (minRate > 10_000)
79
+ throw new ContributionError("gate.min_rate_bps 不得超过 10000");
80
+ if (netRevenueMinor <= 0) {
81
+ reasons.push("rate_not_applicable");
82
+ }
83
+ else if (BigInt(contributionMinor) * 10000n <
84
+ BigInt(minRate) * BigInt(netRevenueMinor)) {
85
+ reasons.push("below_min_rate");
86
+ }
87
+ }
88
+ const blocked = reasons.some((reason) => ["below_min_contribution", "below_min_rate", "rate_not_applicable"].includes(reason));
89
+ if (blocked)
90
+ return { status: "BLOCKED", reasons };
91
+ if (reasons.includes("estimated_exception")) {
92
+ return { status: "EXCEPTION_ALLOWED", reasons };
93
+ }
94
+ return { status: "PASSED", reasons };
95
+ }
96
+ export function calculateContribution(input) {
97
+ if (input.kind !== "ESTIMATED" && input.kind !== "ACTUAL") {
98
+ throw new ContributionError("kind 必须是 ESTIMATED 或 ACTUAL");
99
+ }
100
+ if (input.completeness !== "COMPLETE" &&
101
+ input.completeness !== "ESTIMATED" &&
102
+ input.completeness !== "INCOMPLETE") {
103
+ throw new ContributionError("completeness 非法");
104
+ }
105
+ const hasNet = input.net_revenue_minor !== undefined;
106
+ const hasGross = input.gross_revenue_minor !== undefined || input.refund_minor !== undefined;
107
+ if (hasNet && hasGross) {
108
+ throw new ContributionError("net_revenue_minor 与 gross_revenue_minor/refund_minor 只能二选一");
109
+ }
110
+ let netRevenue;
111
+ if (hasNet) {
112
+ netRevenue = requireMinor(input.net_revenue_minor, "net_revenue_minor", true);
113
+ }
114
+ else if (input.gross_revenue_minor !== undefined) {
115
+ const gross = requireMinor(input.gross_revenue_minor, "gross_revenue_minor");
116
+ const refund = optionalMinor(input.refund_minor, "refund_minor");
117
+ netRevenue = requireMinor(gross - refund, "net_revenue_minor", true);
118
+ if (refund > gross)
119
+ throw new ContributionError("refund_minor 不得超过 gross_revenue_minor");
120
+ }
121
+ else {
122
+ throw new ContributionError("必须提供 net_revenue_minor 或 gross_revenue_minor");
123
+ }
124
+ const goods = optionalMinor(input.costs.goods_cost_minor, "costs.goods_cost_minor");
125
+ const recovery = optionalMinor(input.cost_recovery_minor, "cost_recovery_minor");
126
+ if (recovery > goods)
127
+ throw new ContributionError("cost_recovery_minor 不得超过商品成本");
128
+ const shipping = optionalMinor(input.costs.shipping_cost_minor, "costs.shipping_cost_minor");
129
+ const platform = optionalMinor(input.costs.platform_fee_minor, "costs.platform_fee_minor");
130
+ const other = optionalMinor(input.costs.other_variable_minor, "costs.other_variable_minor");
131
+ const missing = [...(input.missing_cost_items ?? [])].map((item) => {
132
+ if (typeof item !== "string" || item.trim() === "") {
133
+ throw new ContributionError("missing_cost_items 必须是非空字符串");
134
+ }
135
+ return item;
136
+ });
137
+ if (input.completeness === "INCOMPLETE" && missing.length === 0) {
138
+ throw new ContributionError("未算全时必须列出 missing_cost_items");
139
+ }
140
+ if (input.completeness !== "INCOMPLETE" && missing.length > 0) {
141
+ throw new ContributionError("完整/暂估口径不应带有缺失成本项");
142
+ }
143
+ const goodsNet = goods - recovery;
144
+ const totalCost = requireMinor(goodsNet + shipping + platform + other, "total_cost");
145
+ const knownContribution = requireMinor(netRevenue - totalCost, "known_contribution", true);
146
+ const contribution = input.completeness === "INCOMPLETE" ? null : knownContribution;
147
+ let rateBps = null;
148
+ if (contribution !== null && netRevenue > 0) {
149
+ rateBps = roundDiv(BigInt(contribution) * 10000n, BigInt(netRevenue));
150
+ }
151
+ const ratePercent = rateBps === null ? null : Math.round(rateBps) / 100;
152
+ return {
153
+ method_version: "kiwi-contribution/1",
154
+ kind: input.kind,
155
+ completeness: input.completeness,
156
+ net_revenue_minor: netRevenue,
157
+ costs: {
158
+ goods_net_minor: goodsNet,
159
+ shipping_minor: shipping,
160
+ platform_fee_minor: platform,
161
+ other_variable_minor: other,
162
+ total_minor: totalCost,
163
+ },
164
+ contribution_minor: contribution,
165
+ known_contribution_minor: knownContribution,
166
+ missing_cost_items: missing,
167
+ rate_bps: rateBps,
168
+ rate_percent: ratePercent,
169
+ gate: evaluateGate(input, contribution, netRevenue),
170
+ };
171
+ }
172
+ //# sourceMappingURL=calculate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calculate.js","sourceRoot":"","sources":["../../../src/merchant-core/contribution/calculate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;GAMG;AAEH,OAAO,EACL,iBAAiB,GAKlB,MAAM,YAAY,CAAC;AAEpB,MAAM,SAAS,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAE5C,SAAS,YAAY,CAAC,KAAc,EAAE,IAAY,EAAE,aAAa,GAAG,KAAK;IACvE,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAC5B,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC;QACzB,CAAC,CAAC,aAAa,IAAI,KAAK,GAAG,CAAC,CAAC,EAC7B,CAAC;QACD,MAAM,IAAI,iBAAiB,CACzB,GAAG,IAAI,SAAS,aAAa,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,UAAU,CACvF,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AAC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,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,GAAG,CAAC;IAExE,OAAO;QACL,cAAc,EAAE,qBAAqB;QACrC,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,iBAAiB,EAAE,UAAU;QAC7B,KAAK,EAAE;YACL,eAAe,EAAE,QAAQ;YACzB,cAAc,EAAE,QAAQ;YACxB,kBAAkB,EAAE,QAAQ;YAC5B,oBAAoB,EAAE,KAAK;YAC3B,WAAW,EAAE,SAAS;SACvB;QACD,kBAAkB,EAAE,YAAY;QAChC,wBAAwB,EAAE,iBAAiB;QAC3C,kBAAkB,EAAE,OAAO;QAC3B,QAAQ,EAAE,OAAO;QACjB,YAAY,EAAE,WAAW;QACzB,IAAI,EAAE,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,UAAU,CAAC;KACpD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,60 @@
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 { ContributionAssessment, CostCompleteness } from "./types.js";
18
+ export type CostRefKind = "quote" | "order";
19
+ export interface PrivateCostSnapshotRecord {
20
+ snapshot_id: string;
21
+ merchant_id: string;
22
+ ref_kind: CostRefKind;
23
+ ref_id: string;
24
+ currency: string;
25
+ completeness: CostCompleteness;
26
+ costs: Record<string, number>;
27
+ missing_items: string[];
28
+ version: number;
29
+ created_at: string;
30
+ }
31
+ export interface ContributionAssessmentRecord {
32
+ assessment_id: string;
33
+ merchant_id: string;
34
+ ref_kind: CostRefKind;
35
+ ref_id: string;
36
+ assessment: ContributionAssessment;
37
+ cost_snapshot_id: string | null;
38
+ created_at: string;
39
+ }
40
+ export interface ContributionRepositoryOptions {
41
+ db: DatabaseSync;
42
+ now: () => string;
43
+ }
44
+ export declare class ContributionRepository {
45
+ private readonly db;
46
+ private readonly now;
47
+ constructor(options: ContributionRepositoryOptions);
48
+ saveCostSnapshot(input: Omit<PrivateCostSnapshotRecord, "snapshot_id" | "created_at">): PrivateCostSnapshotRecord;
49
+ getCostSnapshot(merchantId: string, snapshotId: string): PrivateCostSnapshotRecord | undefined;
50
+ listCostSnapshots(merchantId: string, refKind: CostRefKind, refId: string): PrivateCostSnapshotRecord[];
51
+ saveAssessment(input: {
52
+ merchantId: string;
53
+ refKind: CostRefKind;
54
+ refId: string;
55
+ assessment: ContributionAssessment;
56
+ costSnapshotId?: string;
57
+ }): ContributionAssessmentRecord;
58
+ getAssessment(merchantId: string, assessmentId: string): ContributionAssessmentRecord | undefined;
59
+ listAssessments(merchantId: string, refKind: CostRefKind, refId: string): ContributionAssessmentRecord[];
60
+ }
@@ -0,0 +1,121 @@
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 §13 / §16)。
18
+ *
19
+ * 成本快照与贡献评估都是版本化历史:新成本不改写旧报价/订单的评估,
20
+ * 事后核算创建新记录。查询按商家隔离。
21
+ */
22
+ import { randomUUID } from "node:crypto";
23
+ function snapshotFromRow(row) {
24
+ return {
25
+ snapshot_id: row.snapshot_id,
26
+ merchant_id: row.merchant_id,
27
+ ref_kind: row.ref_kind,
28
+ ref_id: row.ref_id,
29
+ currency: row.currency,
30
+ completeness: row.completeness,
31
+ costs: JSON.parse(row.costs_json),
32
+ missing_items: JSON.parse(row.missing_json),
33
+ version: row.version,
34
+ created_at: row.created_at,
35
+ };
36
+ }
37
+ function assessmentFromRow(row) {
38
+ const costs = JSON.parse(row.costs_json);
39
+ return {
40
+ assessment_id: row.assessment_id,
41
+ merchant_id: row.merchant_id,
42
+ ref_kind: row.ref_kind,
43
+ ref_id: row.ref_id,
44
+ assessment: {
45
+ method_version: "kiwi-contribution/1",
46
+ kind: row.kind,
47
+ completeness: row.completeness,
48
+ net_revenue_minor: row.net_revenue_minor,
49
+ costs,
50
+ contribution_minor: row.contribution_minor,
51
+ known_contribution_minor: row.known_contribution_minor,
52
+ missing_cost_items: JSON.parse(row.missing_json),
53
+ rate_bps: row.rate_bps,
54
+ rate_percent: row.rate_bps === null ? null : Math.round(row.rate_bps) / 100,
55
+ gate: {
56
+ status: row.gate_status,
57
+ reasons: JSON.parse(row.gate_reasons_json),
58
+ },
59
+ },
60
+ cost_snapshot_id: row.cost_snapshot_id,
61
+ created_at: row.created_at,
62
+ };
63
+ }
64
+ export class ContributionRepository {
65
+ db;
66
+ now;
67
+ constructor(options) {
68
+ this.db = options.db;
69
+ this.now = options.now;
70
+ }
71
+ saveCostSnapshot(input) {
72
+ const snapshotId = `cost_${randomUUID()}`;
73
+ this.db
74
+ .prepare(`INSERT INTO private_cost_snapshots
75
+ (snapshot_id, merchant_id, ref_kind, ref_id, currency, completeness,
76
+ costs_json, missing_json, version, created_at)
77
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
78
+ .run(snapshotId, input.merchant_id, input.ref_kind, input.ref_id, input.currency, input.completeness, JSON.stringify(input.costs), JSON.stringify(input.missing_items), input.version, this.now());
79
+ return this.getCostSnapshot(input.merchant_id, snapshotId);
80
+ }
81
+ getCostSnapshot(merchantId, snapshotId) {
82
+ const row = this.db
83
+ .prepare("SELECT * FROM private_cost_snapshots WHERE merchant_id = ? AND snapshot_id = ?")
84
+ .get(merchantId, snapshotId);
85
+ return row === undefined ? undefined : snapshotFromRow(row);
86
+ }
87
+ listCostSnapshots(merchantId, refKind, refId) {
88
+ const rows = this.db
89
+ .prepare(`SELECT * FROM private_cost_snapshots
90
+ WHERE merchant_id = ? AND ref_kind = ? AND ref_id = ?
91
+ ORDER BY version DESC, created_at DESC`)
92
+ .all(merchantId, refKind, refId);
93
+ return rows.map(snapshotFromRow);
94
+ }
95
+ saveAssessment(input) {
96
+ const assessmentId = `contrib_${randomUUID()}`;
97
+ this.db
98
+ .prepare(`INSERT INTO contribution_assessments
99
+ (assessment_id, merchant_id, ref_kind, ref_id, method_version, kind, completeness,
100
+ net_revenue_minor, costs_json, contribution_minor, known_contribution_minor, rate_bps,
101
+ missing_json, gate_status, gate_reasons_json, cost_snapshot_id, created_at)
102
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
103
+ .run(assessmentId, input.merchantId, input.refKind, input.refId, input.assessment.method_version, input.assessment.kind, input.assessment.completeness, input.assessment.net_revenue_minor, JSON.stringify(input.assessment.costs), input.assessment.contribution_minor, input.assessment.known_contribution_minor, input.assessment.rate_bps, JSON.stringify(input.assessment.missing_cost_items), input.assessment.gate.status, JSON.stringify(input.assessment.gate.reasons), input.costSnapshotId ?? null, this.now());
104
+ return this.getAssessment(input.merchantId, assessmentId);
105
+ }
106
+ getAssessment(merchantId, assessmentId) {
107
+ const row = this.db
108
+ .prepare("SELECT * FROM contribution_assessments WHERE merchant_id = ? AND assessment_id = ?")
109
+ .get(merchantId, assessmentId);
110
+ return row === undefined ? undefined : assessmentFromRow(row);
111
+ }
112
+ listAssessments(merchantId, refKind, refId) {
113
+ const rows = this.db
114
+ .prepare(`SELECT * FROM contribution_assessments
115
+ WHERE merchant_id = ? AND ref_kind = ? AND ref_id = ?
116
+ ORDER BY created_at DESC`)
117
+ .all(merchantId, refKind, refId);
118
+ return rows.map(assessmentFromRow);
119
+ }
120
+ }
121
+ //# sourceMappingURL=repository.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository.js","sourceRoot":"","sources":["../../../src/merchant-core/contribution/repository.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAoEzC,SAAS,eAAe,CAAC,GAAgB;IACvC,OAAO;QACL,WAAW,EAAE,GAAG,CAAC,WAAW;QAC5B,WAAW,EAAE,GAAG,CAAC,WAAW;QAC5B,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,YAAY,EAAE,GAAG,CAAC,YAAY;QAC9B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAA2B;QAC3D,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,CAAa;QACvD,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,UAAU,EAAE,GAAG,CAAC,UAAU;KAC3B,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAC,GAAkB;IAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAoC,CAAC;IAC5E,OAAO;QACL,aAAa,EAAE,GAAG,CAAC,aAAa;QAChC,WAAW,EAAE,GAAG,CAAC,WAAW;QAC5B,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,UAAU,EAAE;YACV,cAAc,EAAE,qBAAqB;YACrC,IAAI,EAAE,GAA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C,UAAU,CAAC,wBAAwB,EACzC,KAAK,CAAC,UAAU,CAAC,QAAQ,EACzB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,kBAAkB,CAAC,EACnD,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAC5B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAC7C,KAAK,CAAC,cAAc,IAAI,IAAI,EAC5B,IAAI,CAAC,GAAG,EAAE,CACX,CAAC;QACJ,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,UAAU,EAAE,YAAY,CAAE,CAAC;IAC7D,CAAC;IAED,aAAa,CAAC,UAAkB,EAAE,YAAoB;QACpD,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE;aAChB,OAAO,CAAC,oFAAoF,CAAC;aAC7F,GAAG,CAAC,UAAU,EAAE,YAAY,CAAyC,CAAC;QACzE,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;IAChE,CAAC;IAED,eAAe,CAAC,UAAkB,EAAE,OAAoB,EAAE,KAAa;QACrE,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE;aACjB,OAAO,CACN;;mCAE2B,CAC5B;aACA,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,KAAK,CAA+B,CAAC;QACjE,OAAO,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;CACF"}
@@ -0,0 +1,97 @@
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 §13 / FR16、FR26–FR28)。
18
+ *
19
+ * 贡献 = 净收入 − 净商品成本 − 商家配送总成本 − 平台及支付费用 − 其他
20
+ * 可归属变动履约成本。未知成本不按 0 填充;暂估/未算全默认不能通过最低
21
+ * 贡献硬门。贡献不是企业净利润,也不包含“节省人工工资”。
22
+ */
23
+ export type CostCompleteness = "COMPLETE" | "ESTIMATED" | "INCOMPLETE";
24
+ export type ContributionKind = "ESTIMATED" | "ACTUAL";
25
+ export interface ContributionCosts {
26
+ /** 净商品成本(已扣除有证据的成本恢复)。 */
27
+ goods_cost_minor?: number;
28
+ /** 商家承担的配送总成本。 */
29
+ shipping_cost_minor?: number;
30
+ /** 平台及支付费用。 */
31
+ platform_fee_minor?: number;
32
+ /** 其他可归属变动履约成本。 */
33
+ other_variable_minor?: number;
34
+ }
35
+ export interface ContributionGate {
36
+ enabled: boolean;
37
+ min_contribution_minor?: number;
38
+ /** 贡献率下限,万分比(10% = 1000 bps),用交叉相乘避免浮点。 */
39
+ min_rate_bps?: number;
40
+ /** 暂估/缺项的独立例外授权(不是硬门满足)。 */
41
+ exception_authorized?: boolean;
42
+ }
43
+ export interface ContributionInput {
44
+ /** 订单有效交易金额(商品/服务/向客户收取的运费),已按实际取消/退款调整。 */
45
+ net_revenue_minor?: number;
46
+ /** 可选:原始收入与退款;提供时与 net_revenue 二选一(不得同时给)。 */
47
+ gross_revenue_minor?: number;
48
+ refund_minor?: number;
49
+ /** 有证据的成本恢复(退货入库/供应商退费),用于减少商品成本。 */
50
+ cost_recovery_minor?: number;
51
+ costs: ContributionCosts;
52
+ completeness: CostCompleteness;
53
+ missing_cost_items?: string[];
54
+ kind: ContributionKind;
55
+ gate?: ContributionGate;
56
+ }
57
+ export type GateDecision = {
58
+ status: "PASSED";
59
+ reasons: string[];
60
+ } | {
61
+ status: "BLOCKED";
62
+ reasons: string[];
63
+ } | {
64
+ status: "NOT_EVALUATED";
65
+ reasons: string[];
66
+ } | {
67
+ status: "EXCEPTION_ALLOWED";
68
+ reasons: string[];
69
+ };
70
+ export interface ContributionAssessment {
71
+ method_version: "kiwi-contribution/1";
72
+ kind: ContributionKind;
73
+ completeness: CostCompleteness;
74
+ net_revenue_minor: number;
75
+ costs: {
76
+ goods_net_minor: number;
77
+ shipping_minor: number;
78
+ platform_fee_minor: number;
79
+ other_variable_minor: number;
80
+ total_minor: number;
81
+ };
82
+ /** 口径完整/暂估时可展示的贡献;未算全时为 null(不得填 0)。 */
83
+ contribution_minor: number | null;
84
+ /** 已知成本的贡献(未算全时用于展示已知部分,不作为发布依据)。 */
85
+ known_contribution_minor: number;
86
+ missing_cost_items: string[];
87
+ /** 贡献率(bps,整数);净收入 ≤ 0 时为 null。 */
88
+ rate_bps: number | null;
89
+ /** 贡献率百分比,展示用两位小数;净收入 ≤ 0 时为 null。 */
90
+ rate_percent: number | null;
91
+ gate: GateDecision;
92
+ }
93
+ export type ContributionErrorCode = "validation";
94
+ export declare class ContributionError extends Error {
95
+ readonly code: ContributionErrorCode;
96
+ constructor(message: string);
97
+ }
@@ -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 ContributionError extends Error {
17
+ code;
18
+ constructor(message) {
19
+ super(message);
20
+ this.name = "ContributionError";
21
+ this.code = "validation";
22
+ }
23
+ }
24
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/contribution/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAgFH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAwB;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF"}