@harrylabsj/kiwi 0.12.14 → 0.12.16

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 (169) 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 +114 -0
  11. package/dist/a2a/server/negotiation-approvals.js +240 -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 +247 -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/mcp/merchant-ops-server.d.ts +38 -0
  34. package/dist/mcp/merchant-ops-server.js +283 -0
  35. package/dist/mcp/merchant-ops-server.js.map +1 -0
  36. package/dist/mcp/merchant-runtime-assembly.js +28 -0
  37. package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
  38. package/dist/mcp/merchant-server.d.ts +16 -0
  39. package/dist/mcp/merchant-server.js +24 -0
  40. package/dist/mcp/merchant-server.js.map +1 -1
  41. package/dist/merchant/application/actor.d.ts +1 -1
  42. package/dist/merchant/application/actor.js +43 -1
  43. package/dist/merchant/application/actor.js.map +1 -1
  44. package/dist/merchant/negotiation-observer.d.ts +35 -0
  45. package/dist/merchant/negotiation-observer.js +159 -51
  46. package/dist/merchant/negotiation-observer.js.map +1 -1
  47. package/dist/merchant/negotiation-ops.d.ts +77 -0
  48. package/dist/merchant/negotiation-ops.js +203 -0
  49. package/dist/merchant/negotiation-ops.js.map +1 -0
  50. package/dist/merchant/quote-calculator.d.ts +7 -0
  51. package/dist/merchant/quote-calculator.js +35 -24
  52. package/dist/merchant/quote-calculator.js.map +1 -1
  53. package/dist/merchant/watch/detect.d.ts +37 -0
  54. package/dist/merchant/watch/detect.js +147 -0
  55. package/dist/merchant/watch/detect.js.map +1 -0
  56. package/dist/merchant/watch/notify.d.ts +48 -0
  57. package/dist/merchant/watch/notify.js +108 -0
  58. package/dist/merchant/watch/notify.js.map +1 -0
  59. package/dist/merchant/watch/service.d.ts +43 -0
  60. package/dist/merchant/watch/service.js +167 -0
  61. package/dist/merchant/watch/service.js.map +1 -0
  62. package/dist/merchant/watch/state.d.ts +49 -0
  63. package/dist/merchant/watch/state.js +68 -0
  64. package/dist/merchant/watch/state.js.map +1 -0
  65. package/dist/merchant/watch/weixin.d.ts +54 -0
  66. package/dist/merchant/watch/weixin.js +271 -0
  67. package/dist/merchant/watch/weixin.js.map +1 -0
  68. package/dist/merchant-core/commercial/calculate.d.ts +18 -0
  69. package/dist/merchant-core/commercial/calculate.js +196 -0
  70. package/dist/merchant-core/commercial/calculate.js.map +1 -0
  71. package/dist/merchant-core/commercial/types.d.ts +91 -0
  72. package/dist/merchant-core/commercial/types.js +17 -0
  73. package/dist/merchant-core/commercial/types.js.map +1 -0
  74. package/dist/merchant-core/contribution/calculate.d.ts +24 -0
  75. package/dist/merchant-core/contribution/calculate.js +172 -0
  76. package/dist/merchant-core/contribution/calculate.js.map +1 -0
  77. package/dist/merchant-core/contribution/repository.d.ts +60 -0
  78. package/dist/merchant-core/contribution/repository.js +121 -0
  79. package/dist/merchant-core/contribution/repository.js.map +1 -0
  80. package/dist/merchant-core/contribution/types.d.ts +97 -0
  81. package/dist/merchant-core/contribution/types.js +24 -0
  82. package/dist/merchant-core/contribution/types.js.map +1 -0
  83. package/dist/merchant-core/followup/lease.d.ts +27 -0
  84. package/dist/merchant-core/followup/lease.js +45 -0
  85. package/dist/merchant-core/followup/lease.js.map +1 -0
  86. package/dist/merchant-core/followup/repository.d.ts +141 -0
  87. package/dist/merchant-core/followup/repository.js +280 -0
  88. package/dist/merchant-core/followup/repository.js.map +1 -0
  89. package/dist/merchant-core/followup/retry.d.ts +27 -0
  90. package/dist/merchant-core/followup/retry.js +61 -0
  91. package/dist/merchant-core/followup/retry.js.map +1 -0
  92. package/dist/merchant-core/followup/service.d.ts +114 -0
  93. package/dist/merchant-core/followup/service.js +191 -0
  94. package/dist/merchant-core/followup/service.js.map +1 -0
  95. package/dist/merchant-core/followup/state.d.ts +25 -0
  96. package/dist/merchant-core/followup/state.js +126 -0
  97. package/dist/merchant-core/followup/state.js.map +1 -0
  98. package/dist/merchant-core/followup/types.d.ts +75 -0
  99. package/dist/merchant-core/followup/types.js +41 -0
  100. package/dist/merchant-core/followup/types.js.map +1 -0
  101. package/dist/merchant-core/metrics/timing.d.ts +63 -0
  102. package/dist/merchant-core/metrics/timing.js +125 -0
  103. package/dist/merchant-core/metrics/timing.js.map +1 -0
  104. package/dist/merchant-core/operating/repository.d.ts +97 -0
  105. package/dist/merchant-core/operating/repository.js +266 -0
  106. package/dist/merchant-core/operating/repository.js.map +1 -0
  107. package/dist/merchant-core/operating/service.d.ts +63 -0
  108. package/dist/merchant-core/operating/service.js +336 -0
  109. package/dist/merchant-core/operating/service.js.map +1 -0
  110. package/dist/merchant-core/operating/types.d.ts +102 -0
  111. package/dist/merchant-core/operating/types.js +24 -0
  112. package/dist/merchant-core/operating/types.js.map +1 -0
  113. package/dist/merchant-core/opportunity/repository.d.ts +128 -0
  114. package/dist/merchant-core/opportunity/repository.js +251 -0
  115. package/dist/merchant-core/opportunity/repository.js.map +1 -0
  116. package/dist/merchant-core/opportunity/service.d.ts +117 -0
  117. package/dist/merchant-core/opportunity/service.js +388 -0
  118. package/dist/merchant-core/opportunity/service.js.map +1 -0
  119. package/dist/merchant-core/opportunity/state.d.ts +33 -0
  120. package/dist/merchant-core/opportunity/state.js +166 -0
  121. package/dist/merchant-core/opportunity/state.js.map +1 -0
  122. package/dist/merchant-core/opportunity/types.d.ts +141 -0
  123. package/dist/merchant-core/opportunity/types.js +42 -0
  124. package/dist/merchant-core/opportunity/types.js.map +1 -0
  125. package/dist/merchant-core/ops/facade.d.ts +159 -0
  126. package/dist/merchant-core/ops/facade.js +213 -0
  127. package/dist/merchant-core/ops/facade.js.map +1 -0
  128. package/dist/merchant-core/ops/schema.d.ts +25 -0
  129. package/dist/merchant-core/ops/schema.js +448 -0
  130. package/dist/merchant-core/ops/schema.js.map +1 -0
  131. package/dist/merchant-core/ops/surface.d.ts +49 -0
  132. package/dist/merchant-core/ops/surface.js +304 -0
  133. package/dist/merchant-core/ops/surface.js.map +1 -0
  134. package/dist/merchant-core/orders/csv.d.ts +36 -0
  135. package/dist/merchant-core/orders/csv.js +173 -0
  136. package/dist/merchant-core/orders/csv.js.map +1 -0
  137. package/dist/merchant-core/orders/model.d.ts +155 -0
  138. package/dist/merchant-core/orders/model.js +237 -0
  139. package/dist/merchant-core/orders/model.js.map +1 -0
  140. package/dist/merchant-core/orders/repository.d.ts +99 -0
  141. package/dist/merchant-core/orders/repository.js +221 -0
  142. package/dist/merchant-core/orders/repository.js.map +1 -0
  143. package/dist/merchant-core/orders/service.d.ts +84 -0
  144. package/dist/merchant-core/orders/service.js +121 -0
  145. package/dist/merchant-core/orders/service.js.map +1 -0
  146. package/dist/merchant-core/orders/shopping-cli-source.d.ts +58 -0
  147. package/dist/merchant-core/orders/shopping-cli-source.js +164 -0
  148. package/dist/merchant-core/orders/shopping-cli-source.js.map +1 -0
  149. package/dist/merchant-core/proposal/engine.d.ts +31 -0
  150. package/dist/merchant-core/proposal/engine.js +348 -0
  151. package/dist/merchant-core/proposal/engine.js.map +1 -0
  152. package/dist/merchant-core/proposal/publish.d.ts +46 -0
  153. package/dist/merchant-core/proposal/publish.js +79 -0
  154. package/dist/merchant-core/proposal/publish.js.map +1 -0
  155. package/dist/merchant-core/proposal/repository.d.ts +60 -0
  156. package/dist/merchant-core/proposal/repository.js +186 -0
  157. package/dist/merchant-core/proposal/repository.js.map +1 -0
  158. package/dist/merchant-core/proposal/service.d.ts +73 -0
  159. package/dist/merchant-core/proposal/service.js +149 -0
  160. package/dist/merchant-core/proposal/service.js.map +1 -0
  161. package/dist/merchant-core/proposal/types.d.ts +163 -0
  162. package/dist/merchant-core/proposal/types.js +25 -0
  163. package/dist/merchant-core/proposal/types.js.map +1 -0
  164. package/dist/merchant-runtime/services.d.ts +0 -8
  165. package/dist/merchant-runtime/services.js +38 -0
  166. package/dist/merchant-runtime/services.js.map +1 -1
  167. package/dist/product-cli.js +5 -0
  168. package/dist/product-cli.js.map +1 -1
  169. package/package.json +1 -1
@@ -0,0 +1,114 @@
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 §8)。
18
+ *
19
+ * P0 默认只产出商家待办/草稿;没有可靠客户发送通道。执行外部动作前必须
20
+ * `markExternalStarted`,崩溃/租约过期后只有该标记为 0 才可回到 READY。
21
+ */
22
+ import { FollowUpRepository, type FollowUpTask } from "./repository.js";
23
+ import { type FollowUpStatus, type FollowUpTransition } from "./types.js";
24
+ export interface FollowUpServiceOptions {
25
+ repository: FollowUpRepository;
26
+ now: () => string;
27
+ }
28
+ export interface ScheduleFollowUpInput {
29
+ merchant_id: string;
30
+ opportunity_id?: string;
31
+ trigger_event: string;
32
+ action_type: string;
33
+ target_version?: string;
34
+ due_at?: string;
35
+ max_attempts: number;
36
+ idempotency_key?: string;
37
+ payload?: Record<string, unknown>;
38
+ }
39
+ export declare class FollowUpService {
40
+ private readonly repository;
41
+ private readonly now;
42
+ constructor(options: FollowUpServiceOptions);
43
+ schedule(input: ScheduleFollowUpInput): {
44
+ task: FollowUpTask;
45
+ transition: FollowUpTransition;
46
+ };
47
+ get(merchantId: string, taskId: string): FollowUpTask | undefined;
48
+ list(merchantId: string, filter?: {
49
+ status?: FollowUpStatus;
50
+ opportunityId?: string;
51
+ }): FollowUpTask[];
52
+ listDue(merchantId: string, limit?: number): FollowUpTask[];
53
+ markDue(input: {
54
+ merchant_id: string;
55
+ task_id: string;
56
+ requires_approval?: boolean;
57
+ }): FollowUpTask;
58
+ claim(input: {
59
+ merchant_id: string;
60
+ task_id: string;
61
+ worker_id: string;
62
+ lease_ms: number;
63
+ }): {
64
+ task: FollowUpTask;
65
+ transition: FollowUpTransition;
66
+ };
67
+ markExternalStarted(input: {
68
+ merchant_id: string;
69
+ task_id: string;
70
+ fencing_token: number;
71
+ }): void;
72
+ needApproval(input: {
73
+ merchant_id: string;
74
+ task_id: string;
75
+ fencing_token: number;
76
+ }): FollowUpTask;
77
+ approve(input: {
78
+ merchant_id: string;
79
+ task_id: string;
80
+ still_due: boolean;
81
+ }): FollowUpTask;
82
+ reject(input: {
83
+ merchant_id: string;
84
+ task_id: string;
85
+ }): FollowUpTask;
86
+ finish(input: {
87
+ merchant_id: string;
88
+ task_id: string;
89
+ fencing_token: number;
90
+ outcome: "succeeded" | "failed" | "unknown";
91
+ receipt?: Record<string, unknown>;
92
+ error_code?: string;
93
+ }): FollowUpTask;
94
+ retry(input: {
95
+ merchant_id: string;
96
+ task_id: string;
97
+ retryable: boolean;
98
+ still_authorized: boolean;
99
+ }): FollowUpTask;
100
+ resolve(input: {
101
+ merchant_id: string;
102
+ task_id: string;
103
+ outcome: "completed" | "not_executed" | "failed";
104
+ still_authorized: boolean;
105
+ }): FollowUpTask;
106
+ stop(input: {
107
+ merchant_id: string;
108
+ task_id: string;
109
+ in_flight: boolean;
110
+ }): FollowUpTask;
111
+ recoverExpiredLeases(merchantId: string): FollowUpTask[];
112
+ private requireTask;
113
+ private apply;
114
+ }
@@ -0,0 +1,191 @@
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 §8)。
18
+ *
19
+ * P0 默认只产出商家待办/草稿;没有可靠客户发送通道。执行外部动作前必须
20
+ * `markExternalStarted`,崩溃/租约过期后只有该标记为 0 才可回到 READY。
21
+ */
22
+ import { FollowUpRepository } from "./repository.js";
23
+ import { transitionFollowUp } from "./state.js";
24
+ import { FollowUpError, } from "./types.js";
25
+ export class FollowUpService {
26
+ repository;
27
+ now;
28
+ constructor(options) {
29
+ this.repository = options.repository;
30
+ this.now = options.now;
31
+ }
32
+ schedule(input) {
33
+ const task = this.repository.transaction(() => {
34
+ const created = this.repository.createTask({
35
+ merchantId: input.merchant_id,
36
+ ...(input.opportunity_id !== undefined ? { opportunityId: input.opportunity_id } : {}),
37
+ triggerEvent: input.trigger_event,
38
+ actionType: input.action_type,
39
+ ...(input.target_version !== undefined ? { targetVersion: input.target_version } : {}),
40
+ ...(input.due_at !== undefined ? { dueAt: input.due_at } : {}),
41
+ maxAttempts: input.max_attempts,
42
+ ...(input.idempotency_key !== undefined ? { idempotencyKey: input.idempotency_key } : {}),
43
+ payload: input.payload ?? {},
44
+ });
45
+ return created.task;
46
+ });
47
+ // 新建任务以 PENDING 落库;状态机的 NEW→PENDING 在这里等价记录。
48
+ return {
49
+ task,
50
+ transition: {
51
+ from: "NEW",
52
+ event: "SCHEDULE",
53
+ to: "PENDING",
54
+ side_effects: ["validate_target_permission_and_unique_key"],
55
+ },
56
+ };
57
+ }
58
+ get(merchantId, taskId) {
59
+ return this.repository.getTask(merchantId, taskId);
60
+ }
61
+ list(merchantId, filter = {}) {
62
+ return this.repository.listTasks(merchantId, filter);
63
+ }
64
+ listDue(merchantId, limit) {
65
+ return this.repository.listDue(merchantId, this.now(), limit);
66
+ }
67
+ markDue(input) {
68
+ const task = this.requireTask(input.merchant_id, input.task_id);
69
+ return this.apply(input.merchant_id, task, {
70
+ type: "DUE",
71
+ due: true,
72
+ ...(input.requires_approval === true ? { requires_approval: true } : {}),
73
+ }).task;
74
+ }
75
+ claim(input) {
76
+ return this.repository.transaction(() => {
77
+ const task = this.requireTask(input.merchant_id, input.task_id);
78
+ if (task.status !== "READY") {
79
+ throw new FollowUpError("invalid_transition", `任务状态 ${task.status} 不能领取`);
80
+ }
81
+ if (task.attempts >= task.max_attempts) {
82
+ throw new FollowUpError("invalid_transition", "重试预算已用尽");
83
+ }
84
+ const transition = transitionFollowUp(task.status, { type: "CLAIM" });
85
+ const claimed = this.repository.claim({
86
+ merchantId: input.merchant_id,
87
+ taskId: input.task_id,
88
+ workerId: input.worker_id,
89
+ leaseMs: input.lease_ms,
90
+ now: this.now(),
91
+ });
92
+ if (claimed === undefined) {
93
+ throw new FollowUpError("invalid_transition", "任务已被其他执行者领取");
94
+ }
95
+ this.repository.recordAttempt({
96
+ merchantId: input.merchant_id,
97
+ taskId: input.task_id,
98
+ attemptNo: claimed.attempts,
99
+ status: "RUNNING",
100
+ startedAt: this.now(),
101
+ });
102
+ return { task: claimed, transition };
103
+ });
104
+ }
105
+ markExternalStarted(input) {
106
+ this.repository.markExternalStarted(input.merchant_id, input.task_id, input.fencing_token);
107
+ }
108
+ needApproval(input) {
109
+ const task = this.requireTask(input.merchant_id, input.task_id);
110
+ return this.apply(input.merchant_id, task, { type: "NEED_APPROVAL" }, input.fencing_token).task;
111
+ }
112
+ approve(input) {
113
+ const task = this.requireTask(input.merchant_id, input.task_id);
114
+ return this.apply(input.merchant_id, task, { type: "APPROVE", still_due: input.still_due }).task;
115
+ }
116
+ reject(input) {
117
+ const task = this.requireTask(input.merchant_id, input.task_id);
118
+ return this.apply(input.merchant_id, task, { type: "REJECT" }).task;
119
+ }
120
+ finish(input) {
121
+ const task = this.requireTask(input.merchant_id, input.task_id);
122
+ const event = input.outcome === "succeeded"
123
+ ? { type: "SUCCEED" }
124
+ : input.outcome === "failed"
125
+ ? { type: "FAIL", reason_code: input.error_code ?? "unknown" }
126
+ : { type: "UNKNOWN" };
127
+ const result = this.apply(input.merchant_id, task, event, input.fencing_token);
128
+ this.repository.finishAttempt({
129
+ merchantId: input.merchant_id,
130
+ taskId: input.task_id,
131
+ attemptNo: task.attempts,
132
+ status: input.outcome,
133
+ endedAt: this.now(),
134
+ ...(input.receipt !== undefined ? { receipt: input.receipt } : {}),
135
+ ...(input.error_code !== undefined ? { errorCode: input.error_code } : {}),
136
+ });
137
+ return result.task;
138
+ }
139
+ retry(input) {
140
+ const task = this.requireTask(input.merchant_id, input.task_id);
141
+ return this.apply(input.merchant_id, task, {
142
+ type: "RETRY",
143
+ retryable: input.retryable,
144
+ attempts_so_far: task.attempts,
145
+ max_attempts: task.max_attempts,
146
+ still_authorized: input.still_authorized,
147
+ }).task;
148
+ }
149
+ resolve(input) {
150
+ const task = this.requireTask(input.merchant_id, input.task_id);
151
+ return this.apply(input.merchant_id, task, {
152
+ type: "RESOLVE",
153
+ outcome: input.outcome,
154
+ still_authorized: input.still_authorized,
155
+ }).task;
156
+ }
157
+ stop(input) {
158
+ const task = this.requireTask(input.merchant_id, input.task_id);
159
+ const result = this.apply(input.merchant_id, task, {
160
+ type: "STOP",
161
+ in_flight: input.in_flight,
162
+ });
163
+ return result.task;
164
+ }
165
+ recoverExpiredLeases(merchantId) {
166
+ return this.repository.transaction(() => this.repository.recoverExpiredLeases(merchantId, this.now()));
167
+ }
168
+ requireTask(merchantId, taskId) {
169
+ const task = this.repository.getTask(merchantId, taskId);
170
+ if (task === undefined)
171
+ throw new FollowUpError("validation", `task ${taskId} not found`);
172
+ return task;
173
+ }
174
+ apply(merchantId, task, event, fencingToken) {
175
+ const transition = transitionFollowUp(task.status, event);
176
+ const token = task.status === "RUNNING"
177
+ ? fencingToken ?? task.fencing_token
178
+ : undefined;
179
+ const updated = this.repository.applyTransition({
180
+ merchantId,
181
+ taskId: task.task_id,
182
+ from: task.status,
183
+ to: transition.to,
184
+ ...(token !== undefined ? { fencingToken: token } : {}),
185
+ ...(transition.to === "UNKNOWN" ? { stopRequested: true } : {}),
186
+ ...(event.type === "FAIL" ? { lastError: event.reason_code } : {}),
187
+ });
188
+ return { task: updated, transition };
189
+ }
190
+ }
191
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/merchant-core/followup/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,kB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AAK,CACX,UAAkB,EAClB,IAAkB,EAClB,KAAoB,EACpB,YAAqB;QAErB,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC1D,MAAM,KAAK,GACT,IAAI,CAAC,MAAM,KAAK,SAAS;YACvB,CAAC,CAAC,YAAY,IAAI,IAAI,CAAC,aAAa;YACpC,CAAC,CAAC,SAAS,CAAC;QAChB,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC;YAC9C,UAAU;YACV,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,IAAI,EAAE,IAAI,CAAC,MAAM;YACjB,EAAE,EAAE,UAAU,CAAC,EAAE;YACjB,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACvD,GAAG,CAAC,UAAU,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC/D,GAAG,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACnE,CAAC,CAAC;QACH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IACvC,CAAC;CACF"}
@@ -0,0 +1,25 @@
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 §8.1 / §8.5 迁移表)。
18
+ *
19
+ * 纯函数:返回目标状态与副作用;不读写数据库、不调用外部通道。终态
20
+ * SUCCEEDED/CANCELLED 拒绝任何恢复请求;外部结果不明只能进入 UNKNOWN,
21
+ * 由回执/权威查询/有证据的人工核对解决。
22
+ */
23
+ import { type FollowUpEvent, type FollowUpStatus, type FollowUpTransition } from "./types.js";
24
+ export declare function isTerminalFollowUp(status: FollowUpStatus): boolean;
25
+ export declare function transitionFollowUp(from: FollowUpStatus, event: FollowUpEvent): FollowUpTransition;
@@ -0,0 +1,126 @@
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 §8.1 / §8.5 迁移表)。
18
+ *
19
+ * 纯函数:返回目标状态与副作用;不读写数据库、不调用外部通道。终态
20
+ * SUCCEEDED/CANCELLED 拒绝任何恢复请求;外部结果不明只能进入 UNKNOWN,
21
+ * 由回执/权威查询/有证据的人工核对解决。
22
+ */
23
+ import { FollowUpError, } from "./types.js";
24
+ const TERMINAL = new Set(["SUCCEEDED", "CANCELLED"]);
25
+ function invalid(from, event) {
26
+ throw new FollowUpError("invalid_transition", `跟进任务状态 ${from} 不接受事件 ${event}`);
27
+ }
28
+ export function isTerminalFollowUp(status) {
29
+ return TERMINAL.has(status);
30
+ }
31
+ export function transitionFollowUp(from, event) {
32
+ if (isTerminalFollowUp(from)) {
33
+ throw new FollowUpError("terminal_state", `终态 ${from} 不可恢复或重试`);
34
+ }
35
+ const result = (to, side_effects) => ({
36
+ from,
37
+ event: event.type,
38
+ to,
39
+ side_effects,
40
+ });
41
+ switch (event.type) {
42
+ case "SCHEDULE":
43
+ if (from !== "NEW")
44
+ return invalid(from, event.type);
45
+ return result("PENDING", ["validate_target_permission_and_unique_key"]);
46
+ case "DUE":
47
+ if (from !== "PENDING")
48
+ return invalid(from, event.type);
49
+ if (!event.due)
50
+ return result("PENDING", []);
51
+ return result(event.requires_approval === true ? "WAITING_APPROVAL" : "READY", [
52
+ ...(event.requires_approval === true ? ["bind_approval_summary"] : []),
53
+ ]);
54
+ case "CLAIM":
55
+ if (from !== "READY")
56
+ return invalid(from, event.type);
57
+ return result(event.requires_approval === true ? "WAITING_APPROVAL" : "RUNNING", [
58
+ ...(event.requires_approval === true ? ["release_lease", "bind_approval_summary"] : ["acquire_lease_and_fencing_token"]),
59
+ ]);
60
+ case "NEED_APPROVAL":
61
+ if (from !== "RUNNING")
62
+ return invalid(from, event.type);
63
+ return result("WAITING_APPROVAL", ["release_lease", "bind_approval_summary"]);
64
+ case "APPROVE":
65
+ if (from !== "WAITING_APPROVAL")
66
+ return invalid(from, event.type);
67
+ return result(event.still_due ? "READY" : "PENDING", ["record_approval"]);
68
+ case "REJECT":
69
+ if (from !== "WAITING_APPROVAL")
70
+ return invalid(from, event.type);
71
+ return result("CANCELLED", ["record_rejection", "release_lease"]);
72
+ case "SUCCEED":
73
+ if (from !== "RUNNING")
74
+ return invalid(from, event.type);
75
+ return result("SUCCEEDED", ["record_external_receipt", "release_lease"]);
76
+ case "FAIL":
77
+ if (from !== "RUNNING")
78
+ return invalid(from, event.type);
79
+ return result("FAILED", ["record_failure_reason", "release_lease"]);
80
+ case "UNKNOWN":
81
+ if (from !== "RUNNING")
82
+ return invalid(from, event.type);
83
+ return result("UNKNOWN", ["suspend_retries", "require_receipt_or_manual_check"]);
84
+ case "LEASE_EXPIRED":
85
+ if (from !== "RUNNING")
86
+ return invalid(from, event.type);
87
+ return event.proven_not_sent
88
+ ? result("READY", ["release_lease", "allow_new_lease"])
89
+ : result("UNKNOWN", ["release_lease", "suspend_retries", "require_receipt_or_manual_check"]);
90
+ case "RETRY":
91
+ if (from !== "FAILED")
92
+ return invalid(from, event.type);
93
+ if (!event.retryable ||
94
+ !event.still_authorized ||
95
+ !Number.isSafeInteger(event.attempts_so_far) ||
96
+ !Number.isSafeInteger(event.max_attempts) ||
97
+ event.attempts_so_far < 0 ||
98
+ event.max_attempts < 1 ||
99
+ event.attempts_so_far >= event.max_attempts) {
100
+ return result("FAILED", ["keep_failed_for_manual_handling"]);
101
+ }
102
+ return result("PENDING", ["enqueue_retry_with_backoff"]);
103
+ case "RESOLVE":
104
+ if (from !== "UNKNOWN")
105
+ return invalid(from, event.type);
106
+ if (event.outcome === "completed") {
107
+ return result("SUCCEEDED", ["record_resolution_evidence"]);
108
+ }
109
+ if (event.outcome === "failed") {
110
+ return result("FAILED", ["record_resolution_evidence"]);
111
+ }
112
+ return event.still_authorized
113
+ ? result("READY", ["record_resolution_evidence", "allow_new_lease"])
114
+ : result("CANCELLED", ["record_resolution_evidence", "authorization_revoked"]);
115
+ case "STOP":
116
+ // 结果不明时不能取消:停止只记 stop_requested,仍要继续只读核实到
117
+ // 真实结果,避免把“可能已发生”伪装成“已撤回”。
118
+ if (from === "UNKNOWN" || (from === "RUNNING" && event.in_flight)) {
119
+ return result("UNKNOWN", ["record_stop_requested", "continue_readonly_verification"]);
120
+ }
121
+ return result("CANCELLED", ["invalidate_unexecuted_tasks", "stop_followups"]);
122
+ default:
123
+ return invalid(from, event.type);
124
+ }
125
+ }
126
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/merchant-core/followup/state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;GAMG;AAEH,OAAO,EACL,aAAa,G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,EAAG,KAAuB,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;AACH,CAAC"}
@@ -0,0 +1,75 @@
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 §8 / FR08–FR11、FR31–FR35)。
18
+ *
19
+ * 任务状态只由服务端迁移;模型不能批准状态,外部动作结果不明时必须
20
+ * 进入 UNKNOWN,不能靠等待超时假定失败。
21
+ */
22
+ export declare const FOLLOWUP_STATUSES: readonly ["NEW", "PENDING", "READY", "RUNNING", "WAITING_APPROVAL", "SUCCEEDED", "CANCELLED", "FAILED", "UNKNOWN"];
23
+ export type FollowUpStatus = (typeof FOLLOWUP_STATUSES)[number];
24
+ export type FollowUpEvent = {
25
+ type: "SCHEDULE";
26
+ } | {
27
+ type: "DUE";
28
+ due: boolean;
29
+ requires_approval?: boolean;
30
+ } | {
31
+ type: "CLAIM";
32
+ requires_approval?: boolean;
33
+ } | {
34
+ type: "NEED_APPROVAL";
35
+ } | {
36
+ type: "APPROVE";
37
+ still_due: boolean;
38
+ } | {
39
+ type: "REJECT";
40
+ } | {
41
+ type: "SUCCEED";
42
+ } | {
43
+ type: "FAIL";
44
+ reason_code: string;
45
+ } | {
46
+ type: "UNKNOWN";
47
+ } | {
48
+ type: "LEASE_EXPIRED";
49
+ proven_not_sent: boolean;
50
+ } | {
51
+ type: "RETRY";
52
+ retryable: boolean;
53
+ attempts_so_far: number;
54
+ max_attempts: number;
55
+ still_authorized: boolean;
56
+ } | {
57
+ type: "RESOLVE";
58
+ outcome: "completed" | "not_executed" | "failed";
59
+ still_authorized: boolean;
60
+ } | {
61
+ type: "STOP";
62
+ in_flight: boolean;
63
+ };
64
+ export interface FollowUpTransition {
65
+ from: FollowUpStatus;
66
+ event: FollowUpEvent["type"];
67
+ to: FollowUpStatus;
68
+ /** 执行器/服务需要执行的副作用。 */
69
+ side_effects: string[];
70
+ }
71
+ export type FollowUpErrorCode = "invalid_transition" | "terminal_state" | "validation";
72
+ export declare class FollowUpError extends Error {
73
+ readonly code: FollowUpErrorCode;
74
+ constructor(code: FollowUpErrorCode, message: string);
75
+ }
@@ -0,0 +1,41 @@
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 §8 / FR08–FR11、FR31–FR35)。
18
+ *
19
+ * 任务状态只由服务端迁移;模型不能批准状态,外部动作结果不明时必须
20
+ * 进入 UNKNOWN,不能靠等待超时假定失败。
21
+ */
22
+ export const FOLLOWUP_STATUSES = [
23
+ "NEW",
24
+ "PENDING",
25
+ "READY",
26
+ "RUNNING",
27
+ "WAITING_APPROVAL",
28
+ "SUCCEEDED",
29
+ "CANCELLED",
30
+ "FAILED",
31
+ "UNKNOWN",
32
+ ];
33
+ export class FollowUpError extends Error {
34
+ code;
35
+ constructor(code, message) {
36
+ super(message);
37
+ this.name = "FollowUpError";
38
+ this.code = code;
39
+ }
40
+ }
41
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/followup/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,KAAK;IACL,SAAS;IACT,OAAO;IACP,SAAS;IACT,kBAAkB;IAClB,WAAW;IACX,WAAW;IACX,QAAQ;IACR,SAAS;CACD,CAAC;AAyCX,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,IAAI,CAAoB;IACjC,YAAY,IAAuB,EAAE,OAAe;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,63 @@
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 §21.2 / AC20)。
18
+ *
19
+ * 纯函数:主动人工人时只累加 active_start→pause/active_end 区间;等待客户/
20
+ * 审批不算主动人时。改善率中位数、P90 与错误率均按冻结口径计算,不以平均
21
+ * 值替代。缺失计时标缺失,不填 0。
22
+ */
23
+ export type TimingEventKind = "task_start" | "active_start" | "pause" | "resume" | "active_end" | "task_complete";
24
+ export interface TimingEvent {
25
+ kind: TimingEventKind;
26
+ at: string;
27
+ actor?: string;
28
+ task_id?: string;
29
+ reason?: string;
30
+ }
31
+ export interface TimingSummary {
32
+ active_ms: number;
33
+ duration_ms: number | null;
34
+ missing: string[];
35
+ }
36
+ /**
37
+ * 计算主动人时与端到端历时。事件必须按时间排序;乱序/缺开始时间时
38
+ * 返回 missing 原因,不猜测。
39
+ */
40
+ export declare function summarizeTiming(events: readonly TimingEvent[]): TimingSummary;
41
+ /** 升序第 ceil(p/100 × n) 项;空数组返回 null。p 在 (0,100]。 */
42
+ export declare function percentile(values: readonly number[], p: number): number | null;
43
+ export declare function median(values: readonly number[]): number | null;
44
+ export interface MatchedTaskPair {
45
+ pair_id: string;
46
+ original_active_ms: number;
47
+ kiwi_active_ms: number;
48
+ /** 未完成样本单列,不伪作完成。 */
49
+ completed?: boolean;
50
+ }
51
+ export interface ImprovementSummary {
52
+ sample_count: number;
53
+ completed_count: number;
54
+ incomplete_count: number;
55
+ median_improvement_rate: number | null;
56
+ original_median_ms: number | null;
57
+ kiwi_median_ms: number | null;
58
+ original_p90_ms: number | null;
59
+ kiwi_p90_ms: number | null;
60
+ missing_pairs: number;
61
+ }
62
+ /** 改善率 r = (T原 − T新) / T原;T原必须 > 0;中位数按 §21.2 口径。 */
63
+ export declare function summarizeMatchedPairs(pairs: readonly MatchedTaskPair[]): ImprovementSummary;