@tbox.cn/app-module-parking 0.2.0 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +16 -0
- package/README.md +58 -11
- package/dist/chunk-O4W4A6TQ.js +2696 -0
- package/dist/chunk-QSHINO4B.js +1073 -0
- package/dist/client/index.d.ts +17 -3
- package/dist/client/index.js +4 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.js +14 -4
- package/dist/parking-BC2TJ5GU.css +1306 -0
- package/dist/server/index.d.ts +1216 -72
- package/dist/server/index.js +11 -3
- package/package.json +24 -16
- package/skills/parking/SKILL.md +165 -0
- package/skills/parking/references/payment-safety.md +25 -0
- package/src/client/cards/index.ts +8 -0
- package/src/client/cards/parking-info/index.tsx +41 -0
- package/src/client/cards/parking-payment/benefit-selection.ts +79 -0
- package/src/client/cards/parking-payment/index.tsx +435 -0
- package/src/client/cards/parking-payment-records/index.tsx +259 -0
- package/src/client/cards/parking-payment-result/index.tsx +83 -0
- package/src/client/cards/parking-vehicle-management/PlateKeyboard.tsx +106 -0
- package/src/client/cards/parking-vehicle-management/index.tsx +344 -0
- package/src/client/cards/parking-vehicle-management/plate-keyboard.ts +113 -0
- package/src/client/cards/view.ts +83 -0
- package/src/client/index.ts +19 -4
- package/src/client/styles/parking.css +1306 -0
- package/src/client/types/css.d.ts +2 -0
- package/src/server/actions.ts +975 -0
- package/src/server/cards/index.ts +14 -0
- package/src/server/cards/parking-info/meta.ts +19 -0
- package/src/server/cards/parking-info/samples.ts +50 -0
- package/src/server/cards/parking-payment/meta.ts +29 -0
- package/src/server/cards/parking-payment/samples.ts +311 -0
- package/src/server/cards/parking-payment-records/meta.ts +32 -0
- package/src/server/cards/parking-payment-records/samples.ts +86 -0
- package/src/server/cards/parking-payment-result/meta.ts +21 -0
- package/src/server/cards/parking-payment-result/samples.ts +84 -0
- package/src/server/cards/parking-vehicle-management/meta.ts +20 -0
- package/src/server/cards/parking-vehicle-management/samples.ts +73 -0
- package/src/server/domain.ts +279 -0
- package/src/server/handler.ts +65 -16
- package/src/server/index.ts +120 -18
- package/src/server/intent-context.ts +14 -0
- package/src/server/ports.ts +59 -0
- package/src/server/service.ts +474 -45
- package/src/server/tools.ts +846 -0
- package/tbox.module.json +107 -0
- package/tests/benefit-selection.test.ts +70 -0
- package/tests/cards-render.test.tsx +645 -0
- package/tests/dispatcher-flow.test.ts +256 -0
- package/tests/payment-flow.test.ts +420 -0
- package/tests/plate-keyboard.test.ts +158 -0
- package/tests/ports-resolve.test.ts +113 -0
- package/tests/pretool-parking-fee.test.ts +104 -0
- package/tests/samples.test.ts +44 -0
- package/tests/skills.test.ts +37 -0
- package/tests/snapshot-service.test.ts +87 -0
- package/tests/tools-gate.test.ts +237 -0
- package/tests/tools-membership.test.ts +91 -0
- package/tests/view.test.ts +61 -0
- package/tsup.config.ts +9 -1
- package/dist/chunk-BYD3C6NE.js +0 -140
- package/dist/chunk-QQLBZY6D.js +0 -55
- package/src/client/cards/parking-fee/index.tsx +0 -59
- package/src/server/cards/parking-fee/meta.ts +0 -26
- package/src/server/tool.ts +0 -36
- package/tbox.component.json +0 -20
- package/tests/service.test.ts +0 -49
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parking-vehicle-management 附加预览样本(沉淀自 mall dev-gallery 停车场景画廊)。
|
|
3
|
+
* 方法论:见同目录 ../parking-payment/samples.ts 头注释。
|
|
4
|
+
*/
|
|
5
|
+
import type { CardSample } from '@tbox.cn/app-contracts';
|
|
6
|
+
import type { ParkingVehicleManagementCardData } from '@tbox.cn/app-contracts-mall';
|
|
7
|
+
|
|
8
|
+
const VEHICLE = '车辆管理';
|
|
9
|
+
|
|
10
|
+
export const samples: CardSample<ParkingVehicleManagementCardData>[] = [
|
|
11
|
+
{
|
|
12
|
+
label: '空态录入',
|
|
13
|
+
note: '无绑定车辆 → 强制录入态(无法取消,不出「取消」按钮)',
|
|
14
|
+
group: VEHICLE,
|
|
15
|
+
data: { bound: [] },
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
label: '多车与在场',
|
|
19
|
+
note: '首辆在场标记——真机默认形态',
|
|
20
|
+
group: VEHICLE,
|
|
21
|
+
data: {
|
|
22
|
+
bound: [
|
|
23
|
+
{ vehicleRef: 'v_1', plateNo: '浙A12345', parked: true },
|
|
24
|
+
{ vehicleRef: 'v_2', plateNo: '浙AD98765' },
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
label: '入会引导',
|
|
30
|
+
note: '仅 not_enrolled 才出引导;已入会/未知/历史卡一律不显示',
|
|
31
|
+
group: VEHICLE,
|
|
32
|
+
data: {
|
|
33
|
+
bound: [{ vehicleRef: 'v_1', plateNo: '浙A12345', parked: true }],
|
|
34
|
+
membership: { state: 'not_enrolled' },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
label: '车辆较多',
|
|
39
|
+
note: '5 辆混合普通/新能源车牌——检查列表高度与车牌等宽对齐',
|
|
40
|
+
group: VEHICLE,
|
|
41
|
+
data: {
|
|
42
|
+
bound: [
|
|
43
|
+
{ vehicleRef: 'v_1', plateNo: '浙A12345', parked: true },
|
|
44
|
+
{ vehicleRef: 'v_2', plateNo: '浙AD98765' },
|
|
45
|
+
{ vehicleRef: 'v_3', plateNo: '沪B66888' },
|
|
46
|
+
{ vehicleRef: 'v_4', plateNo: '苏EF12345' },
|
|
47
|
+
{ vehicleRef: 'v_5', plateNo: '京N7Q3M5' },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
label: '历史卡',
|
|
53
|
+
note: 'isHistory——不出「添加车辆」入口、不出入会引导,寻车与解绑全禁用',
|
|
54
|
+
group: VEHICLE,
|
|
55
|
+
data: {
|
|
56
|
+
bound: [
|
|
57
|
+
{ vehicleRef: 'v_1', plateNo: '浙A12345', parked: true },
|
|
58
|
+
{ vehicleRef: 'v_2', plateNo: '浙AD98765' },
|
|
59
|
+
],
|
|
60
|
+
membership: { state: 'not_enrolled' },
|
|
61
|
+
},
|
|
62
|
+
isHistory: true,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
label: '授权已过期',
|
|
66
|
+
note: '无 token:「添加车辆」与解绑置灰,寻车按钮仍在(它不需要令牌门禁)',
|
|
67
|
+
group: VEHICLE,
|
|
68
|
+
data: {
|
|
69
|
+
bound: [{ vehicleRef: 'v_1', plateNo: '浙A12345', parked: true }],
|
|
70
|
+
},
|
|
71
|
+
noTokens: true,
|
|
72
|
+
},
|
|
73
|
+
];
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 停车域状态机与收口纯函数(015 S5)。
|
|
3
|
+
* - 报价缓存:仅未支付态、TTL 5min(W4 前提一)
|
|
4
|
+
* - 支付操作:PAYING →(reconcile 权威)→ PAID/CANCELLED;UNKNOWN 悬挂(V2:下次交互收口)
|
|
5
|
+
* - 收口规则(W4 前提三):查询命中已支付态 → 自动收口 PaymentOperation
|
|
6
|
+
*/
|
|
7
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
8
|
+
import type {
|
|
9
|
+
QuoteSnapshot,
|
|
10
|
+
PaymentOperation,
|
|
11
|
+
ParkingPaymentRecord,
|
|
12
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
13
|
+
|
|
14
|
+
/** StateStore 命名空间(ctx.state;模块动作/工具统一使用) */
|
|
15
|
+
export const PARKING_QUOTE_NAMESPACE = 'parking.quotes';
|
|
16
|
+
export const PARKING_PAYMENT_NAMESPACE = 'parking.payment-operations';
|
|
17
|
+
export const PARKING_LATEST_PAYMENT_NAMESPACE = 'parking.latest-payment';
|
|
18
|
+
/** quoteRef → paymentRef 映射(W4 收口接线:queryFee 命中 PAID 时反查悬挂操作) */
|
|
19
|
+
export const PARKING_QUOTE_PAYMENT_NAMESPACE = 'parking.quote-payment';
|
|
20
|
+
export const QUOTE_TTL_MS = 5 * 60 * 1000;
|
|
21
|
+
export const PAYMENT_OPERATION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
22
|
+
|
|
23
|
+
/** 支付渠道常量(POC channel 50 = 支付宝) */
|
|
24
|
+
export const PAYMENT_CHANNEL_ALIPAY = '50';
|
|
25
|
+
|
|
26
|
+
export interface StoreLike {
|
|
27
|
+
get<T>(namespace: string, ref: string): Promise<Readonly<T> | null>;
|
|
28
|
+
put<T>(
|
|
29
|
+
namespace: string,
|
|
30
|
+
ref: string,
|
|
31
|
+
value: T,
|
|
32
|
+
ttlMs: number,
|
|
33
|
+
): Promise<boolean>;
|
|
34
|
+
getEntry<T>(
|
|
35
|
+
namespace: string,
|
|
36
|
+
ref: string,
|
|
37
|
+
): Promise<Readonly<{ value: Readonly<T>; revision: number }> | null>;
|
|
38
|
+
compareAndSet<T>(
|
|
39
|
+
namespace: string,
|
|
40
|
+
ref: string,
|
|
41
|
+
expectedRevision: number,
|
|
42
|
+
value: T,
|
|
43
|
+
ttlMs: number,
|
|
44
|
+
): Promise<boolean>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface QuoteOwner {
|
|
48
|
+
readonly subjectId: string;
|
|
49
|
+
readonly mallId: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type OwnedQuoteSnapshot = QuoteSnapshot & {
|
|
53
|
+
readonly ownerSubjectId: string;
|
|
54
|
+
readonly ownerMallId: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** 报价缓存(W4:仅未支付态缓存;PAYING/PAID 直接穿透过厂商) */
|
|
58
|
+
export async function cacheQuote(
|
|
59
|
+
state: StoreLike,
|
|
60
|
+
quote: QuoteSnapshot,
|
|
61
|
+
owner: QuoteOwner,
|
|
62
|
+
): Promise<void> {
|
|
63
|
+
if (quote.paymentState !== 'UNPAID') return;
|
|
64
|
+
await state.put<OwnedQuoteSnapshot>(
|
|
65
|
+
PARKING_QUOTE_NAMESPACE,
|
|
66
|
+
quoteOwnerRef(quote.quoteRef, owner),
|
|
67
|
+
{ ...quote, ownerSubjectId: owner.subjectId, ownerMallId: owner.mallId },
|
|
68
|
+
QUOTE_TTL_MS,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function loadCachedQuote(
|
|
73
|
+
state: StoreLike,
|
|
74
|
+
quoteRef: string,
|
|
75
|
+
owner: QuoteOwner,
|
|
76
|
+
): Promise<QuoteSnapshot | null> {
|
|
77
|
+
const cached = await state.get<OwnedQuoteSnapshot>(
|
|
78
|
+
PARKING_QUOTE_NAMESPACE,
|
|
79
|
+
quoteOwnerRef(quoteRef, owner),
|
|
80
|
+
);
|
|
81
|
+
return cached &&
|
|
82
|
+
cached.paymentState === 'UNPAID' &&
|
|
83
|
+
cached.ownerSubjectId === owner.subjectId &&
|
|
84
|
+
cached.ownerMallId === owner.mallId
|
|
85
|
+
? cached
|
|
86
|
+
: null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 报价 revision CAS 校验(reprice/confirm 提交防旧卡重放) */
|
|
90
|
+
export async function assertQuoteRevision(
|
|
91
|
+
state: StoreLike,
|
|
92
|
+
quoteRef: string,
|
|
93
|
+
expectedRevision: number,
|
|
94
|
+
owner: QuoteOwner,
|
|
95
|
+
): Promise<QuoteSnapshot | null> {
|
|
96
|
+
const cached = await loadCachedQuote(state, quoteRef, owner);
|
|
97
|
+
if (!cached) return null;
|
|
98
|
+
return cached.revision === expectedRevision ? cached : null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 支付操作持久化(建单成功 → PAYING;F5:收口判定恒 reconcile) */
|
|
102
|
+
export async function persistPaymentOperation(
|
|
103
|
+
state: StoreLike,
|
|
104
|
+
op: PaymentOperation,
|
|
105
|
+
): Promise<void> {
|
|
106
|
+
await state.put(
|
|
107
|
+
PARKING_PAYMENT_NAMESPACE,
|
|
108
|
+
op.paymentRef,
|
|
109
|
+
op,
|
|
110
|
+
PAYMENT_OPERATION_TTL_MS,
|
|
111
|
+
);
|
|
112
|
+
await state.put(
|
|
113
|
+
PARKING_LATEST_PAYMENT_NAMESPACE,
|
|
114
|
+
paymentOwnerRef(op.subjectId, op.mallId),
|
|
115
|
+
{ paymentRef: op.paymentRef },
|
|
116
|
+
PAYMENT_OPERATION_TTL_MS,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function loadPaymentOperation(
|
|
121
|
+
state: StoreLike,
|
|
122
|
+
paymentRef: string,
|
|
123
|
+
): Promise<PaymentOperation | null> {
|
|
124
|
+
return state.get<PaymentOperation>(PARKING_PAYMENT_NAMESPACE, paymentRef);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 客户端引用二次授权:ActionGrant 不绑定 formData,必须核对本地操作归属。 */
|
|
128
|
+
export async function loadOwnedPaymentOperation(
|
|
129
|
+
state: StoreLike,
|
|
130
|
+
paymentRef: string,
|
|
131
|
+
owner: { subjectId: string; mallId: string },
|
|
132
|
+
): Promise<PaymentOperation | null> {
|
|
133
|
+
const op = await loadPaymentOperation(state, paymentRef);
|
|
134
|
+
return op?.subjectId === owner.subjectId && op.mallId === owner.mallId
|
|
135
|
+
? op
|
|
136
|
+
: null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 零入参支付状态 Tool:只取当前 subject/mall 最新支付操作。 */
|
|
140
|
+
export async function loadLatestOwnedPaymentOperation(
|
|
141
|
+
state: StoreLike,
|
|
142
|
+
owner: { subjectId: string; mallId: string },
|
|
143
|
+
): Promise<PaymentOperation | null> {
|
|
144
|
+
const latest = await state.get<{ paymentRef: string }>(
|
|
145
|
+
PARKING_LATEST_PAYMENT_NAMESPACE,
|
|
146
|
+
paymentOwnerRef(owner.subjectId, owner.mallId),
|
|
147
|
+
);
|
|
148
|
+
return latest
|
|
149
|
+
? loadOwnedPaymentOperation(state, latest.paymentRef, owner)
|
|
150
|
+
: null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 对外支付引用不承载厂商单号。 */
|
|
154
|
+
export function createPaymentRef(): string {
|
|
155
|
+
return `payment-${randomUUID()}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function paymentOwnerRef(subjectId: string, mallId: string): string {
|
|
159
|
+
return createHash('sha256')
|
|
160
|
+
.update(`${subjectId}\u0000${mallId}`)
|
|
161
|
+
.digest('hex');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** quoteRef → paymentRef 映射(confirm 建单时写入;W4 收口反查锚点) */
|
|
165
|
+
export async function recordQuotePayment(
|
|
166
|
+
state: StoreLike,
|
|
167
|
+
quoteRef: string,
|
|
168
|
+
paymentRef: string,
|
|
169
|
+
owner: QuoteOwner,
|
|
170
|
+
): Promise<void> {
|
|
171
|
+
await state.put(
|
|
172
|
+
PARKING_QUOTE_PAYMENT_NAMESPACE,
|
|
173
|
+
quoteOwnerRef(quoteRef, owner),
|
|
174
|
+
{ paymentRef },
|
|
175
|
+
PAYMENT_OPERATION_TTL_MS,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function loadQuotePayment(
|
|
180
|
+
state: StoreLike,
|
|
181
|
+
quoteRef: string,
|
|
182
|
+
owner: QuoteOwner,
|
|
183
|
+
): Promise<{ paymentRef: string } | null> {
|
|
184
|
+
return state.get<{ paymentRef: string }>(
|
|
185
|
+
PARKING_QUOTE_PAYMENT_NAMESPACE,
|
|
186
|
+
quoteOwnerRef(quoteRef, owner),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function quoteOwnerRef(quoteRef: string, owner: QuoteOwner): string {
|
|
191
|
+
return createHash('sha256')
|
|
192
|
+
.update(`${quoteRef}\u0000${owner.subjectId}\u0000${owner.mallId}`)
|
|
193
|
+
.digest('hex');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** reconcile 收口(F5 唯一写 PAID 的路径) */
|
|
197
|
+
export async function settlePaymentOperation(
|
|
198
|
+
state: StoreLike,
|
|
199
|
+
paymentRef: string,
|
|
200
|
+
settled: 'PAID' | 'CANCELLED' | 'UNKNOWN',
|
|
201
|
+
): Promise<PaymentOperation | null> {
|
|
202
|
+
const entry = await state.getEntry<PaymentOperation>(
|
|
203
|
+
PARKING_PAYMENT_NAMESPACE,
|
|
204
|
+
paymentRef,
|
|
205
|
+
);
|
|
206
|
+
if (!entry) return null;
|
|
207
|
+
if (entry.value.status === 'PAID' || entry.value.status === 'CANCELLED')
|
|
208
|
+
return entry.value; // 幂等
|
|
209
|
+
const next: PaymentOperation =
|
|
210
|
+
settled === 'UNKNOWN'
|
|
211
|
+
? entry.value // UNKNOWN 不改态(悬挂语义,V2)
|
|
212
|
+
: {
|
|
213
|
+
...entry.value,
|
|
214
|
+
status: settled,
|
|
215
|
+
...(settled === 'PAID' ? { paidAt: new Date().toISOString() } : {}),
|
|
216
|
+
};
|
|
217
|
+
const updated = await state.compareAndSet(
|
|
218
|
+
PARKING_PAYMENT_NAMESPACE,
|
|
219
|
+
paymentRef,
|
|
220
|
+
entry.revision,
|
|
221
|
+
next,
|
|
222
|
+
PAYMENT_OPERATION_TTL_MS,
|
|
223
|
+
);
|
|
224
|
+
// Code security: CAS 竞争失败时返回实际持久化终态,禁止向客户端暴露未落库的支付状态。
|
|
225
|
+
return updated ? next : loadPaymentOperation(state, paymentRef);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* W4 前提三:已支付态查询命中 → 自动收口悬挂 PAYING 操作。
|
|
230
|
+
* 工具每次查询(queryFee/records)后调用;paidSignal = 厂商侧已支付证据。
|
|
231
|
+
*/
|
|
232
|
+
export async function autoSettleOnPaidEvidence(
|
|
233
|
+
state: StoreLike,
|
|
234
|
+
quote: QuoteSnapshot,
|
|
235
|
+
paidEvidence: { paymentRef: string } | undefined,
|
|
236
|
+
): Promise<void> {
|
|
237
|
+
if (!paidEvidence || quote.paymentState !== 'PAID') return;
|
|
238
|
+
const op = await loadPaymentOperation(state, paidEvidence.paymentRef);
|
|
239
|
+
if (op && op.status === 'PAYING') {
|
|
240
|
+
await settlePaymentOperation(state, paidEvidence.paymentRef, 'PAID');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** 悬挂 PAYING 操作发现(查询入口统一走;返回需 reconcile 的 paymentRef 列表) */
|
|
245
|
+
export async function findHangingPayments(
|
|
246
|
+
state: StoreLike,
|
|
247
|
+
paymentRefs: readonly string[],
|
|
248
|
+
): Promise<string[]> {
|
|
249
|
+
const hanging: string[] = [];
|
|
250
|
+
for (const ref of paymentRefs) {
|
|
251
|
+
const op = await loadPaymentOperation(state, ref);
|
|
252
|
+
if (op?.status === 'PAYING') hanging.push(ref);
|
|
253
|
+
}
|
|
254
|
+
return hanging;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** 缴费记录脱敏常量(POC 厂商口径 caveats,厂商事实全迁) */
|
|
258
|
+
export const PARKING_RECORDS_CAVEATS = [
|
|
259
|
+
'仅展示近 6 个月缴费记录',
|
|
260
|
+
'记录金额为实付金额(权益抵扣后)',
|
|
261
|
+
'退款记录以厂商侧为准',
|
|
262
|
+
'部分场外缴费可能延迟入账',
|
|
263
|
+
'开票请以可开票订单列表为准',
|
|
264
|
+
'同一订单多次支付以最终成功一笔为准',
|
|
265
|
+
'如对记录有疑问请联系商场服务台',
|
|
266
|
+
] as const;
|
|
267
|
+
|
|
268
|
+
/** 金额展示(cents → 元字符串) */
|
|
269
|
+
export function formatCents(cents: number): string {
|
|
270
|
+
return `${(cents / 100).toFixed(2)} 元`;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 记录脱敏(recordRef 哈希 + 单号 mask;POC 口径) */
|
|
274
|
+
export function maskOrderNo(orderNo: string): string {
|
|
275
|
+
if (orderNo.length <= 8) return orderNo;
|
|
276
|
+
return `${orderNo.slice(0, 4)}****${orderNo.slice(-4)}`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export type { QuoteSnapshot, PaymentOperation, ParkingPaymentRecord };
|
package/src/server/handler.ts
CHANGED
|
@@ -1,22 +1,71 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* parking 意图 handler(015:意图路由出卡实时;写操作全走 moduleAction 动作)。
|
|
3
|
+
* 上下文统一化:运行上下文 = handle 第四参 reqCtx(引擎意图分发注入)。
|
|
4
|
+
*/
|
|
5
|
+
import type { HandlerDefinition, ServerContext } from '@tbox.cn/app-sdk/server';
|
|
6
|
+
import { resolveParkingQueryPort } from './ports';
|
|
7
|
+
import { toIntentQueryContext } from './intent-context';
|
|
8
|
+
import { buildParkingPaymentFacts, parkingMembershipGate } from './tools';
|
|
9
|
+
import type { StoreLike } from './domain';
|
|
4
10
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
11
|
+
/** 停车缴费硬路由关键词(W3-3 自模板 intent-routes.ts 迁入——意图与处理器同处定义,011 D7):
|
|
12
|
+
* 整句全等命中(match:'exact',不入 BM25 词袋)绕过 LLM 工具选择;intent action 与
|
|
13
|
+
* 显式路由 query-fee 分支同实现出报价卡。queryFee 无车牌时走扫描绑定车队逻辑(不追问车牌)。 */
|
|
14
|
+
const PARKING_PAY_PHRASES = [
|
|
15
|
+
'停车缴费',
|
|
16
|
+
'缴停车费',
|
|
17
|
+
'交停车费',
|
|
18
|
+
'帮我交停车费',
|
|
19
|
+
'帮我缴停车费',
|
|
20
|
+
'帮我查一下停车缴费',
|
|
21
|
+
'查停车缴费',
|
|
22
|
+
'查询停车缴费',
|
|
23
|
+
'停车缴费多少',
|
|
24
|
+
'停车费多少钱',
|
|
25
|
+
] as const;
|
|
26
|
+
|
|
27
|
+
export function createParkingIntentHandler(ctx: ServerContext): HandlerDefinition {
|
|
7
28
|
return {
|
|
8
29
|
id: 'parking',
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
intents: [{ patterns: [...PARKING_PAY_PHRASES], match: 'exact' }],
|
|
31
|
+
handle: async (action, payload, _ctx, reqCtx) => {
|
|
32
|
+
const c = toIntentQueryContext(reqCtx);
|
|
33
|
+
if (!c) return { cards: [], text: '停车服务不可用(缺少运行上下文)' };
|
|
34
|
+
const service = await resolveParkingQueryPort(c.reqCtx, ctx);
|
|
35
|
+
|
|
36
|
+
switch (action) {
|
|
37
|
+
// intent(collectIntentRoutes 恒置 action:'intent')与 query-fee(显式路由兼容态)
|
|
38
|
+
// 同实现:防御性门禁保住「模块内属人路径全拦」不变式,出 parking-payment 报价卡。
|
|
39
|
+
case 'query-fee':
|
|
40
|
+
case 'intent': {
|
|
41
|
+
const gate = await parkingMembershipGate(ctx, c.reqCtx as never, c.q, service);
|
|
42
|
+
if (gate) return { cards: gate.card ? [gate.card] : [], text: gate.notice };
|
|
43
|
+
const quotes = await service.queryFee(c.q, { plateNo: (payload as { plateNo?: string })?.plateNo });
|
|
44
|
+
if (quotes.length === 0) return { cards: [], text: '当前没有待缴停车费' };
|
|
45
|
+
const cards: { cardType: string; data: Record<string, unknown> }[] = [];
|
|
46
|
+
const textParts: string[] = [];
|
|
47
|
+
for (const quote of quotes) {
|
|
48
|
+
const facts = await buildParkingPaymentFacts({
|
|
49
|
+
ctx,
|
|
50
|
+
reqCtx: c.reqCtx as unknown as import('@tbox.cn/app-contracts').RequestContext,
|
|
51
|
+
q: c.q,
|
|
52
|
+
state: ctx.state as unknown as StoreLike,
|
|
53
|
+
quote,
|
|
54
|
+
});
|
|
55
|
+
cards.push({ cardType: 'parking-payment', data: facts });
|
|
56
|
+
textParts.push(`${quote.plateNo} 待缴 ${(quote.payableCents / 100).toFixed(2)} 元`);
|
|
57
|
+
}
|
|
58
|
+
return { cards, text: textParts.join(';') };
|
|
59
|
+
}
|
|
60
|
+
default: {
|
|
61
|
+
// 概览已退场;兜底落收费规则——用户问不清时「多少钱」是最可能的诉求
|
|
62
|
+
const info = await service.queryParkingInfo(c.q, 'chargeRules');
|
|
63
|
+
return {
|
|
64
|
+
cards: [{ cardType: 'parking-info', data: { kind: info.kind, title: info.title, entries: info.entries.map((e) => ({ label: e.label, value: e.value })) } }],
|
|
65
|
+
text: `${info.title}:${info.entries.map((e) => `${e.label} ${e.value}`).join(';')}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
20
69
|
},
|
|
21
70
|
};
|
|
22
71
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -1,34 +1,136 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type {
|
|
2
|
+
ServerContext,
|
|
3
|
+
ServerModule,
|
|
4
|
+
TypedBus,
|
|
5
|
+
PreToolDef,
|
|
6
|
+
} from '@tbox.cn/app-sdk/server';
|
|
7
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
8
|
+
import { definePreTool, loadModuleSkills } from '@tbox.cn/app-sdk/server';
|
|
9
|
+
import type { MallEventMap, ParkingQueryContext, ParkingSnapshotPort } from '@tbox.cn/app-contracts-mall';
|
|
10
|
+
import { PARKING_SNAPSHOT_SERVICE } from '@tbox.cn/app-contracts-mall';
|
|
11
|
+
import { inMemoryParking, resolveParkingQueryPort } from './ports';
|
|
12
|
+
import parkingPaymentMeta from './cards/parking-payment/meta';
|
|
13
|
+
import parkingPaymentResultMeta from './cards/parking-payment-result/meta';
|
|
14
|
+
import parkingPaymentRecordsMeta from './cards/parking-payment-records/meta';
|
|
15
|
+
import parkingVehicleManagementMeta from './cards/parking-vehicle-management/meta';
|
|
16
|
+
import parkingInfoMeta from './cards/parking-info/meta';
|
|
17
|
+
import { createParkingTools, decideParkingFeePreRun, decideListVehiclesPreRun } from './tools';
|
|
18
|
+
import { createParkingActions } from './actions';
|
|
19
|
+
import { createParkingIntentHandler } from './handler';
|
|
7
20
|
|
|
8
21
|
export { InMemoryParkingService } from './service';
|
|
9
|
-
export {
|
|
22
|
+
export {
|
|
23
|
+
resolveParkingQueryPort,
|
|
24
|
+
resolveParkingPaymentPort,
|
|
25
|
+
resolveParkingVehiclePort,
|
|
26
|
+
inMemoryParking,
|
|
27
|
+
} from './ports';
|
|
28
|
+
export { parkingCards } from './cards';
|
|
10
29
|
|
|
11
|
-
/**
|
|
12
|
-
|
|
30
|
+
/** 模块级 ServerContext 引用(shopping-guide getCtx 先例;register 期赋值,工具 execute 内读取) */
|
|
31
|
+
let serverCtx: ServerContext | undefined;
|
|
32
|
+
const getCtx = (): ServerContext => {
|
|
33
|
+
if (!serverCtx) throw new Error('parking 模块未注册(ServerContext 未绑定)');
|
|
34
|
+
return serverCtx;
|
|
35
|
+
};
|
|
13
36
|
|
|
14
|
-
/**
|
|
37
|
+
/** 工具实例顶层创建(register 前亦可安全引用——首 execute 时 ctx 必已绑定) */
|
|
38
|
+
const tools = createParkingTools({ getCtx });
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 停车费问句确定性预执行(W5 P0,2026-09-01):费用意图轮零 LLM 预执行出报价卡。
|
|
42
|
+
* - 零参 run:queryParkingFee 服务端自动扫描在场车(与「我的车」问词语义恰好匹配);
|
|
43
|
+
* - 四门 decide 见 tools.ts decideParkingFeePreRun(纯函数单源,测试直测);
|
|
44
|
+
* - silent:不发前端事件;结果 reminder 注入 + cards 经 capture 入本轮候选池;
|
|
45
|
+
* - 超时/失败 = skip 语义(引擎统一 withDeadline),模型路径照常兜底。
|
|
46
|
+
*/
|
|
47
|
+
const parkingFeePreTool = definePreTool(tools.queryParkingFee, {
|
|
48
|
+
routing: { mode: 'keywords', terms: ['停车费', '停车缴费', '缴费', '出场费', '待支付', '停车单'] },
|
|
49
|
+
decide: ({ text }) => decideParkingFeePreRun(text),
|
|
50
|
+
silent: true,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 车辆/车牌问句预执行(2026-09-02):车辆意图轮零 LLM 预执行出车辆管理卡。
|
|
55
|
+
* - 零参 run:listVehicles 无入参,直查绑定车辆列表;
|
|
56
|
+
* - decide 见 tools.ts decideListVehiclesPreRun(纯函数单源);
|
|
57
|
+
* - silent:不发前端事件;结果 reminder 注入 + cards 经 capture 入本轮候选池;
|
|
58
|
+
* - 兜底场景:模型凭历史轮/上下文直答不调工具 → 候选池空 → 无卡。预执行保证池非空。
|
|
59
|
+
*/
|
|
60
|
+
const parkingVehiclesPreTool = definePreTool(tools.listVehicles, {
|
|
61
|
+
routing: { mode: 'keywords', terms: ['车牌', '绑定车辆', '我的车', '车辆', '解绑'] },
|
|
62
|
+
decide: ({ text }) => decideListVehiclesPreRun(text),
|
|
63
|
+
silent: true,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const preTools = {
|
|
67
|
+
queryParkingFee: parkingFeePreTool,
|
|
68
|
+
listVehicles: parkingVehiclesPreTool,
|
|
69
|
+
} satisfies Record<string, PreToolDef>;
|
|
70
|
+
|
|
71
|
+
/** 卡片 meta 声明 map(key 约定 = meta.cardType;字面量形态——doctor AST 解析依赖) */
|
|
15
72
|
const cards = {
|
|
16
|
-
'parking-
|
|
73
|
+
'parking-payment': parkingPaymentMeta,
|
|
74
|
+
'parking-payment-result': parkingPaymentResultMeta,
|
|
75
|
+
'parking-payment-records': parkingPaymentRecordsMeta,
|
|
76
|
+
'parking-vehicle-management': parkingVehicleManagementMeta,
|
|
77
|
+
'parking-info': parkingInfoMeta,
|
|
17
78
|
} as const;
|
|
18
79
|
|
|
80
|
+
/** 文件加载式 skill(009 两形态之二;promotion 先例):顶层 await → 填回声明式通道(装配端 registerAll 自动注册)。
|
|
81
|
+
* codegen 展开改写约定:第一参随包名改写(@tbox.cn/app-module-parking → @app/module-parking)。 */
|
|
82
|
+
const skills = Object.fromEntries(
|
|
83
|
+
(await loadModuleSkills('@tbox.cn/app-module-parking', import.meta.url)).map(
|
|
84
|
+
(s) => [s.name, s],
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
|
|
19
88
|
/** 模块注册入口:自注册闭环(与客户端 clientModule 对称) */
|
|
20
89
|
export const serverModule = {
|
|
90
|
+
declaration: {
|
|
91
|
+
moduleId: 'parking',
|
|
92
|
+
},
|
|
21
93
|
cards,
|
|
94
|
+
skills,
|
|
95
|
+
preTools,
|
|
22
96
|
register(ctx: ServerContext): void {
|
|
23
|
-
ctx
|
|
24
|
-
|
|
97
|
+
serverCtx = ctx;
|
|
98
|
+
// InMemory 单例恒注册(跨模块消费方 resolveService 兼容;真实供给经三槽 resolver(query/payment/vehicle)槽位轴)
|
|
99
|
+
ctx.services.register('parking', inMemoryParking);
|
|
100
|
+
|
|
101
|
+
// 停车快览工厂(parking.snapshot,W3-3):首页联单面板跨模块消费面
|
|
102
|
+
// (绑定车辆列表 + 在场探测)。query 槽 Pick 子集——可选能力口(probeParkedVehicles)
|
|
103
|
+
// 缺席性透传;工厂运行期执行(注册期纯净铁律)。
|
|
104
|
+
ctx.services.register(
|
|
105
|
+
PARKING_SNAPSHOT_SERVICE,
|
|
106
|
+
async (reqCtx: RequestContext): Promise<ParkingSnapshotPort> => {
|
|
107
|
+
const service = await resolveParkingQueryPort(reqCtx, ctx);
|
|
108
|
+
return {
|
|
109
|
+
listVehicles: (q: ParkingQueryContext) => service.listVehicles(q),
|
|
110
|
+
...(service.probeParkedVehicles
|
|
111
|
+
? {
|
|
112
|
+
probeParkedVehicles: (q: ParkingQueryContext, plateNos: readonly string[]) =>
|
|
113
|
+
service.probeParkedVehicles!(q, plateNos),
|
|
114
|
+
}
|
|
115
|
+
: {}),
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
for (const tool of Object.values(tools)) {
|
|
121
|
+
ctx.tools.register(tool);
|
|
122
|
+
}
|
|
25
123
|
ctx.cards.registerMap(cards);
|
|
26
|
-
ctx.handlers.register(
|
|
124
|
+
ctx.handlers.register(createParkingIntentHandler(ctx));
|
|
125
|
+
|
|
126
|
+
// 支付链、车辆管理、记录详情/开票与刷新动作;register 闭包 ctx.state。
|
|
127
|
+
const actions = createParkingActions(ctx);
|
|
128
|
+
for (const action of Object.values(actions)) {
|
|
129
|
+
ctx.cardActions.register(action);
|
|
130
|
+
}
|
|
27
131
|
|
|
132
|
+
// 类型化事件总线(保留:积分抵扣停车费时跨模块通知 member)
|
|
28
133
|
const mallBus = ctx.bus as unknown as TypedBus<MallEventMap>;
|
|
29
|
-
|
|
30
|
-
parking.onCouponUsed = (e) => {
|
|
31
|
-
void mallBus.publish('parking.coupon.used', e);
|
|
32
|
-
};
|
|
134
|
+
void mallBus;
|
|
33
135
|
},
|
|
34
136
|
} satisfies ServerModule;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parking 意图上下文提取(handler 专用;与 tools.ts 的 toParkingQueryContext 同语义收敛于此)。
|
|
3
|
+
* 上下文统一化:显式入参 reqCtx(handler 第四参),不再读 ctx.auth 全局单例。
|
|
4
|
+
*/
|
|
5
|
+
import { toMallQueryContext } from '@tbox.cn/app-contracts-mall';
|
|
6
|
+
import type { ParkingQueryContext } from '@tbox.cn/app-contracts-mall';
|
|
7
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
8
|
+
|
|
9
|
+
export function toIntentQueryContext(
|
|
10
|
+
reqCtx: RequestContext | undefined,
|
|
11
|
+
): { q: ParkingQueryContext; reqCtx: RequestContext } | null {
|
|
12
|
+
if (!reqCtx) return null;
|
|
13
|
+
return { q: toMallQueryContext(reqCtx), reqCtx };
|
|
14
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parking Port 分槽解析(017 槽位原子化):三槽独立解析(query/payment/vehicle),
|
|
3
|
+
* 唯一供给轴 = integrations registry 槽位。adapter 工厂由各 provider 包 register 期注册
|
|
4
|
+
* (同域三槽共享 implementation id,catalog 聚合后同 impl 供给);transport 缓存与失效
|
|
5
|
+
* 统一由平台 registry 负责(键含 service——槽级隔离)。
|
|
6
|
+
*
|
|
7
|
+
* 预期缺席(未配置/实例缺键/禁用)→ InMemoryParkingService 单例兜底(演示数据 + mock
|
|
8
|
+
* 支付闭环,保开箱体验);其余错误原样上抛(配置即声明)。
|
|
9
|
+
* 部分绑定形态(query 厂商 + payment InMemory)为显式行为:InMemory createOrder 对外来
|
|
10
|
+
* quoteRef 直接 throw(V2),跨槽引用失败可见。
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
mallScopeOf,
|
|
14
|
+
PARKING_PAYMENT_SERVICE,
|
|
15
|
+
PARKING_QUERY_SERVICE,
|
|
16
|
+
PARKING_VEHICLE_SERVICE,
|
|
17
|
+
type ParkingPaymentPort,
|
|
18
|
+
type ParkingQueryPort,
|
|
19
|
+
type ParkingVehiclePort,
|
|
20
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
21
|
+
import { isExpectedProviderAbsence, type RequestContext } from '@tbox.cn/app-contracts';
|
|
22
|
+
import type { ServerContext } from '@tbox.cn/app-sdk/server';
|
|
23
|
+
import { InMemoryParkingService } from './service';
|
|
24
|
+
|
|
25
|
+
/** 缺省 InMemory 单例(模块包顶层;跨模块消费方链路兼容;实现三 Port) */
|
|
26
|
+
export const inMemoryParking = new InMemoryParkingService();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 槽位解析公共体(请求级;register 只注册不解析——铁律)。
|
|
30
|
+
* mallId 提取自 reqCtx.attributes(HTTP 场景 auth context 无 mall scope);缺失落 {} scope
|
|
31
|
+
* (registry 按 instances 键匹配,demo 单实例场景命中)。
|
|
32
|
+
*/
|
|
33
|
+
async function resolveParkingSlot<T>(
|
|
34
|
+
service: string,
|
|
35
|
+
reqCtx: RequestContext,
|
|
36
|
+
ctx: ServerContext | undefined,
|
|
37
|
+
fallback: T,
|
|
38
|
+
): Promise<T> {
|
|
39
|
+
if (!ctx?.integrations) return fallback;
|
|
40
|
+
try {
|
|
41
|
+
return (await ctx.integrations.resolveService({
|
|
42
|
+
service,
|
|
43
|
+
scope: mallScopeOf(reqCtx),
|
|
44
|
+
request: reqCtx,
|
|
45
|
+
})) as T;
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if (isExpectedProviderAbsence(e)) return fallback; // 预期缺席 → InMemory 兜底
|
|
48
|
+
throw e; // PROVIDER_NOT_IMPLEMENTED 等非预期缺席 → 上抛(配置即声明,F5 起无回退)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const resolveParkingQueryPort = (reqCtx: RequestContext, ctx?: ServerContext) =>
|
|
53
|
+
resolveParkingSlot<ParkingQueryPort>(PARKING_QUERY_SERVICE, reqCtx, ctx, inMemoryParking);
|
|
54
|
+
export const resolveParkingPaymentPort = (reqCtx: RequestContext, ctx?: ServerContext) =>
|
|
55
|
+
resolveParkingSlot<ParkingPaymentPort>(PARKING_PAYMENT_SERVICE, reqCtx, ctx, inMemoryParking);
|
|
56
|
+
export const resolveParkingVehiclePort = (reqCtx: RequestContext, ctx?: ServerContext) =>
|
|
57
|
+
resolveParkingSlot<ParkingVehiclePort>(PARKING_VEHICLE_SERVICE, reqCtx, ctx, inMemoryParking);
|
|
58
|
+
|
|
59
|
+
export { InMemoryParkingService };
|