@tbox.cn/app-module-alipay-activity 0.2.3

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 (44) hide show
  1. package/AGENTS.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +48 -0
  4. package/dist/bumpin-VKHLYCKG.css +527 -0
  5. package/dist/chunk-RM7IUA3M.js +634 -0
  6. package/dist/client/index.d.ts +11 -0
  7. package/dist/client/index.js +235 -0
  8. package/dist/index.d.ts +7 -0
  9. package/dist/index.js +26 -0
  10. package/dist/linked-card-background-MVGUOJ7J.jpg +0 -0
  11. package/dist/server/index.d.ts +224 -0
  12. package/dist/server/index.js +28 -0
  13. package/package.json +69 -0
  14. package/schemas/credentials/alipay-open-v1.json +12 -0
  15. package/skills/alipay-activity/SKILL.md +59 -0
  16. package/src/client/assets/linked-card-background.jpg +0 -0
  17. package/src/client/assets.d.ts +10 -0
  18. package/src/client/cards/bumpin-activity/index.tsx +213 -0
  19. package/src/client/components/ImageWithFallback.tsx +37 -0
  20. package/src/client/index.ts +18 -0
  21. package/src/client/styles/bumpin.css +527 -0
  22. package/src/client/utils/sanitize-activity-html.ts +40 -0
  23. package/src/index.ts +1 -0
  24. package/src/server/cards/bumpin-activity/meta.ts +44 -0
  25. package/src/server/cards/bumpin-activity/samples.ts +164 -0
  26. package/src/server/handler.ts +87 -0
  27. package/src/server/index.ts +79 -0
  28. package/src/server/integrations/alipay/adapter.ts +210 -0
  29. package/src/server/integrations/alipay/client.ts +127 -0
  30. package/src/server/integrations/alipay/provider.ts +9 -0
  31. package/src/server/ports.ts +68 -0
  32. package/src/server/tools.ts +212 -0
  33. package/tbox.module.json +39 -0
  34. package/tests/adapter.test.ts +178 -0
  35. package/tests/bumpin-card-status.test.tsx +80 -0
  36. package/tests/client.test.ts +93 -0
  37. package/tests/intent-routing.test.ts +118 -0
  38. package/tests/ports.test.ts +160 -0
  39. package/tests/pretool-routing.test.ts +37 -0
  40. package/tests/samples.test.ts +29 -0
  41. package/tests/skill.test.ts +39 -0
  42. package/tests/tools.test.ts +193 -0
  43. package/tsconfig.json +8 -0
  44. package/tsup.config.ts +36 -0
@@ -0,0 +1,634 @@
1
+ // src/server/index.ts
2
+ import { definePreTool, loadModuleSkills } from "@tbox.cn/app-sdk/server";
3
+
4
+ // src/server/cards/bumpin-activity/meta.ts
5
+ import {
6
+ promotionBumpInActivityCardDataSchema
7
+ } from "@tbox.cn/app-contracts-mall";
8
+
9
+ // src/server/cards/bumpin-activity/samples.ts
10
+ var VOUCHER = {
11
+ prizeId: "PZ1741674669",
12
+ voucherName: "88\u5143\u60CA\u559C\u793C\u5305",
13
+ voucherType: "ITEM_REDUCTION_AMOUNT",
14
+ reductionAmount: "88",
15
+ thresholdAmountText: "\u6EE1100\u5143\u53EF\u7528",
16
+ unit: "\u5143",
17
+ merchantName: "\u793A\u4F8B\u5546\u6237"
18
+ };
19
+ var BASE = {
20
+ activityRef: "demo:bumpin:activity:1",
21
+ title: "\u78B0\u4E00\u4E0B | \u51FA\u60CA\u559C",
22
+ mainTitle: "\u78B0\u4E00\u4E0B\u652F\u4ED8 5 \u7B14\uFF0C\u5F97 88 \u5143\u60CA\u559C\u793C\u5305",
23
+ tagLabel: "\u78B0\u4E00\u4E0B\u8054\u5355",
24
+ actStatus: "PROCESSING",
25
+ totalProgress: "5",
26
+ currentProgress: "1",
27
+ progressUnit: "\u7B14",
28
+ startAtText: "2026-08-01",
29
+ endAtText: "2026-08-31",
30
+ vouchers: [VOUCHER],
31
+ mode: "detail",
32
+ progressStyle: "steps"
33
+ };
34
+ var samples = [
35
+ // ── detail 模式:对话流里的完整详情 ──
36
+ {
37
+ label: "\u8BE6\u60C5\xB7\u8054\u5355\u8FDB\u884C\u4E2D",
38
+ note: "\u57FA\u51C6\u5F62\u6001\uFF1Asteps \u5706\u5F62\u8282\u70B9 1/5\u3001\u5355\u5238\u5956\u52B1\u6C60\u3002\u672A\u4E0B\u53D1 backgroundUrl \u65F6\u5934\u90E8\u56DE\u843D\u5230\u6A21\u5757\u81EA\u5E26\u7684\u8054\u5355\u80CC\u666F\u56FE",
39
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
40
+ data: BASE
41
+ },
42
+ {
43
+ label: "\u8BE6\u60C5\xB7\u63A5\u8FD1\u8FBE\u6210",
44
+ note: "currentProgress=4/5\u2014\u2014\u8282\u70B9\u8F68\u9053\u70B9\u4EAE\u5230\u5012\u6570\u7B2C\u4E8C\u73AF\uFF0C\u68C0\u67E5\u672B\u73AF\u4E0E\u6570\u503C\u7684\u6536\u53E3",
45
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
46
+ data: { ...BASE, currentProgress: "4" }
47
+ },
48
+ {
49
+ label: "\u8BE6\u60C5\xB7\u5DF2\u8FBE\u6210",
50
+ note: "5/5\u2014\u2014\u5168\u90E8\u8282\u70B9\u70B9\u4EAE\uFF0C\u5956\u52B1\u6C60\u5E94\u662F\u53EF\u9886\u800C\u975E\u5F85\u89E3\u9501\u7684\u8BED\u6C14",
51
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
52
+ data: { ...BASE, currentProgress: "5" }
53
+ },
54
+ {
55
+ label: "\u8BE6\u60C5\xB7\u591A\u5238\u5956\u52B1\u6C60",
56
+ note: "\u4E09\u6863\u5238\u5E76\u5217\u2014\u2014\u68C0\u67E5\u5238\u884C\u7684\u622A\u65AD\u4E0E\u9762\u989D\u5BF9\u9F50\uFF0C\u4EE5\u53CA\u5361\u7247\u7EB5\u5411\u662F\u5426\u88AB\u6491\u5F97\u8FC7\u957F",
57
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
58
+ data: {
59
+ ...BASE,
60
+ currentProgress: "3",
61
+ vouchers: [
62
+ VOUCHER,
63
+ { ...VOUCHER, prizeId: "PZ2", voucherName: "30\u5143\u901A\u7528\u5238", reductionAmount: "30", thresholdAmountText: "\u6EE150\u5143\u53EF\u7528" },
64
+ { ...VOUCHER, prizeId: "PZ3", voucherName: "10\u5143\u5496\u5561\u5238", reductionAmount: "10", thresholdAmountText: "\u65E0\u95E8\u69DB", merchantName: "\u65E5\u5149\u5496\u5561" }
65
+ ]
66
+ }
67
+ },
68
+ {
69
+ label: "\u8BE6\u60C5\xB7\u65E0\u5956\u52B1\u6C60",
70
+ note: "vouchers \u4E3A\u7A7A\u2014\u2014\u5951\u7EA6\u5141\u8BB8\uFF0C\u5361\u7247\u5E94\u6536\u53E3\u800C\u4E0D\u662F\u7559\u4E00\u5757\u7A7A\u533A",
71
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
72
+ data: { ...BASE, vouchers: [] }
73
+ },
74
+ {
75
+ label: "\u8BE6\u60C5\xB7bar \u8FDB\u5EA6",
76
+ note: "progressStyle='bar'\u2014\u2014\u7EBF\u6027\u8FDB\u5EA6\u6761\u800C\u975E\u5706\u5F62\u8282\u70B9\uFF1B\u540C\u65F6\u4E0D\u518D\u56DE\u843D\u8054\u5355\u80CC\u666F\u56FE\uFF0C\u5934\u90E8\u5F62\u6001\u968F\u4E4B\u4E0D\u540C",
77
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
78
+ data: { ...BASE, progressStyle: "bar", currentProgress: "2" }
79
+ },
80
+ {
81
+ label: "\u8BE6\u60C5\xB7\u5E26\u53C2\u4E0E\u6B65\u9AA4",
82
+ note: "steps \u6570\u7EC4\u4E0B\u53D1\u65F6\u6E32\u67D3\u53C2\u4E0E\u8BF4\u660E\u2014\u2014\u4E0E\u8FDB\u5EA6\u8282\u70B9\u662F\u4E24\u56DE\u4E8B\uFF0C\u68C0\u67E5\u4E24\u8005\u4E0D\u6253\u67B6",
83
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
84
+ data: {
85
+ ...BASE,
86
+ steps: [
87
+ { title: "\u6253\u5F00\u652F\u4ED8\u5B9D\u78B0\u4E00\u4E0B", description: "\u5728\u5546\u573A\u4EFB\u610F\u6536\u94F6\u53F0\u5B8C\u6210\u652F\u4ED8" },
88
+ { title: "\u7D2F\u8BA1 5 \u7B14\u652F\u4ED8", description: "\u540C\u4E00\u6D3B\u52A8\u671F\u5185\u7D2F\u8BA1\uFF0C\u8DE8\u95E8\u5E97\u6709\u6548" },
89
+ { title: "\u9886\u53D6\u60CA\u559C\u793C\u5305", description: "\u8FBE\u6210\u540E\u5728\u672C\u5361\u7247\u9886\u53D6" }
90
+ ]
91
+ }
92
+ },
93
+ {
94
+ label: "\u8BE6\u60C5\xB7\u6D3B\u52A8\u672A\u5F00\u59CB",
95
+ note: "actStatus='NOT_START' + \u8D77\u6B62\u65F6\u95F4\u2014\u2014\u5934\u90E8\u300C\u672A\u5F00\u59CB\u300D\u89D2\u6807\u3001\u6709\u6548\u671F\u884C\u6539\u300C\u65E5\u5F00\u59CB\u300D\u3001\u8FDB\u5EA6\u4E0D\u8BF1\u5BFC\u53C2\u4E0E",
96
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
97
+ data: { ...BASE, actStatus: "NOT_START", currentProgress: "0" }
98
+ },
99
+ {
100
+ label: "\u8BE6\u60C5\xB7\u6D3B\u52A8\u5DF2\u7ED3\u675F",
101
+ note: "actStatus='FINISH'\u2014\u2014\u7EC8\u6001\u89D2\u6807\u300C\u5DF2\u7ED3\u675F\u300D\uFF0C\u68C0\u67E5\u662F\u5426\u4ECD\u5728\u9F13\u52B1\u7EE7\u7EED\u652F\u4ED8",
102
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
103
+ data: { ...BASE, actStatus: "FINISH", currentProgress: "3" }
104
+ },
105
+ {
106
+ label: "\u8BE6\u60C5\xB7\u5DF2\u9886\u5956",
107
+ note: "actStatus='RECEIVED'\u2014\u2014\u7528\u6237\u7EA7\u7EC8\u6001\uFF1A\u91D1\u8272\u300C\u5DF2\u9886\u5956\u300D\u89D2\u6807 + \u5956\u52B1\u6C60\u6807\u9898\u300C\u5DF2\u9886\u53D6\u300D\u524D\u7F00",
108
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
109
+ data: { ...BASE, actStatus: "RECEIVED", currentProgress: "5" }
110
+ },
111
+ {
112
+ label: "\u8BE6\u60C5\xB7\u957F\u6807\u9898\u4E0E\u957F\u63CF\u8FF0",
113
+ note: "\u4E3B\u6807\u9898\u4E0E\u63CF\u8FF0\u90FD\u8D85\u957F\u2014\u2014\u68C0\u67E5\u6362\u884C\u6491\u9AD8\u4E0E\u622A\u65AD\uFF0C\u522B\u628A\u5956\u52B1\u6C60\u6324\u51FA\u9996\u5C4F",
114
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
115
+ data: {
116
+ ...BASE,
117
+ mainTitle: "\u78B0\u4E00\u4E0B\u652F\u4ED8\u6EE1 5 \u7B14\u5373\u53EF\u89E3\u9501 88 \u5143\u60CA\u559C\u793C\u5305\uFF0C\u6D3B\u52A8\u671F\u5185\u8DE8\u95E8\u5E97\u7D2F\u8BA1\u6709\u6548\uFF0C\u5148\u5230\u5148\u5F97",
118
+ description: "\u6D3B\u52A8\u6700\u7EC8\u89E3\u91CA\u6743\u5F52\u4E3B\u529E\u65B9\u6240\u6709\uFF1B\u793C\u5305\u6570\u91CF\u6709\u9650\uFF0C\u8FBE\u6210\u540E\u8BF7\u5728\u6D3B\u52A8\u7ED3\u675F\u524D\u5B8C\u6210\u9886\u53D6\uFF0C\u903E\u671F\u89C6\u4E3A\u653E\u5F03\u3002"
119
+ }
120
+ },
121
+ {
122
+ label: "\u8BE6\u60C5\xB7\u5386\u53F2\u5361",
123
+ note: "isHistory\u2014\u2014\u5361\u5185\u52A8\u4F5C\u7981\u7528\uFF0C\u4FE1\u606F\u7167\u5E38\u5448\u73B0",
124
+ group: "\u8BE6\u60C5\u5F62\u6001\uFF08mode=detail\uFF09",
125
+ data: { ...BASE, currentProgress: "3" },
126
+ isHistory: true
127
+ },
128
+ // ── banner 模式:压缩形态 ──
129
+ {
130
+ label: "\u6A2A\u5E45\xB7\u8054\u5355\u8FDB\u884C\u4E2D",
131
+ note: "mode='banner'\u2014\u2014\u538B\u7F29\u6210\u4E00\u6761\uFF0C\u4E0E\u9996\u9875\u6D77\u62A5\u662F\u540C\u4E00\u6D3B\u52A8\u7684\u4E24\u79CD\u5448\u73B0",
132
+ group: "\u6A2A\u5E45\u5F62\u6001\uFF08mode=banner\uFF09",
133
+ data: { ...BASE, mode: "banner", currentProgress: "2" }
134
+ },
135
+ {
136
+ label: "\u6A2A\u5E45\xB7bar \u8FDB\u5EA6",
137
+ note: "banner + bar\u2014\u2014\u6700\u7D27\u51D1\u7684\u4E00\u6863\uFF0C\u68C0\u67E5\u8FDB\u5EA6\u6761\u4E0E\u6587\u6848\u5728\u4E00\u884C\u5185\u7684\u6324\u538B",
138
+ group: "\u6A2A\u5E45\u5F62\u6001\uFF08mode=banner\uFF09",
139
+ data: { ...BASE, mode: "banner", progressStyle: "bar", currentProgress: "2" }
140
+ },
141
+ {
142
+ label: "\u6A2A\u5E45\xB7\u5DF2\u8FBE\u6210",
143
+ note: "banner \u7EC8\u6001\u2014\u20145/5\uFF0C\u68C0\u67E5\u538B\u7F29\u5F62\u6001\u4E0B\u8FBE\u6210\u8BED\u6C14\u662F\u5426\u4ECD\u7136\u6E05\u695A",
144
+ group: "\u6A2A\u5E45\u5F62\u6001\uFF08mode=banner\uFF09",
145
+ data: { ...BASE, mode: "banner", currentProgress: "5" }
146
+ },
147
+ {
148
+ label: "\u6A2A\u5E45\xB7\u672A\u5F00\u59CB",
149
+ note: "banner + NOT_START\u2014\u2014\u538B\u7F29\u5F62\u6001\u4E0B\u300C\u672A\u5F00\u59CB\u300D\u89D2\u6807\u4E0E\u300C\u65E5\u5F00\u59CB\u300D\u884C\u4ECD\u9700\u53EF\u8FA8",
150
+ group: "\u6A2A\u5E45\u5F62\u6001\uFF08mode=banner\uFF09",
151
+ data: { ...BASE, mode: "banner", actStatus: "NOT_START", currentProgress: "0" }
152
+ }
153
+ ];
154
+
155
+ // src/server/cards/bumpin-activity/meta.ts
156
+ var meta = {
157
+ cardType: "promotion-bumpin-activity",
158
+ dataSchema: promotionBumpInActivityCardDataSchema,
159
+ displayName: "\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8",
160
+ description: "\u652F\u4ED8\u5B9D\u78B0\u51FA\u60CA\u559C\u4E92\u52A8\u8425\u9500\u6D3B\u52A8\uFF08\u8FDB\u5EA6 + \u5956\u52B1\u6C60\uFF09",
161
+ sampleData: {
162
+ activityRef: "demo:bumpin:activity:1",
163
+ title: "\u78B0\u4E00\u4E0B | \u51FA\u60CA\u559C",
164
+ mainTitle: "\u78B0\u4E00\u4E0B\u652F\u4ED8 5 \u7B14\uFF0C\u5F97 88 \u5143\u60CA\u559C\u793C\u5305",
165
+ tagLabel: "\u78B0\u4E00\u4E0B\u8054\u5355",
166
+ actStatus: "PROCESSING",
167
+ totalProgress: "5",
168
+ currentProgress: "1",
169
+ progressUnit: "\u7B14",
170
+ startAtText: "2026-08-01",
171
+ endAtText: "2026-08-31",
172
+ vouchers: [
173
+ {
174
+ prizeId: "PZ1741674669",
175
+ voucherName: "88\u5143\u60CA\u559C\u793C\u5305",
176
+ voucherType: "ITEM_REDUCTION_AMOUNT",
177
+ reductionAmount: "88",
178
+ thresholdAmountText: "\u6EE1100\u5143\u53EF\u7528",
179
+ unit: "\u5143",
180
+ merchantName: "\u793A\u4F8B\u5546\u6237"
181
+ }
182
+ ],
183
+ mode: "detail",
184
+ progressStyle: "steps"
185
+ },
186
+ samples,
187
+ schemaVersion: 1
188
+ };
189
+ var meta_default = meta;
190
+
191
+ // src/server/tools.ts
192
+ import { z } from "zod";
193
+ import { createTool, withCardEmissionHint, requireToolRequestContext } from "@tbox.cn/app-sdk/server";
194
+ import { memberGateCardOf, toMallQueryContext } from "@tbox.cn/app-contracts-mall";
195
+
196
+ // src/server/ports.ts
197
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
198
+ import { expandEnvVars } from "@tbox.cn/app-sdk/server";
199
+
200
+ // src/server/integrations/alipay/adapter.ts
201
+ var ACT_STATUS_VALUES = /* @__PURE__ */ new Set(["NOT_START", "INIT", "PROCESSING", "FINISH", "RECEIVED"]);
202
+ var ACTIVE_STATUSES = /* @__PURE__ */ new Set(["INIT", "PROCESSING"]);
203
+ function normalizeActStatus(raw) {
204
+ if (ACT_STATUS_VALUES.has(raw ?? "")) return raw;
205
+ return raw === void 0 ? "PROCESSING" : "FINISH";
206
+ }
207
+ function msToIso(ms) {
208
+ if (typeof ms !== "number" || ms <= 0) return void 0;
209
+ return new Date(ms).toISOString();
210
+ }
211
+ function mapBumpInActivity(actSchemeId, raw) {
212
+ const offerRefs = (raw.voucherList ?? []).map((v) => `alipay:bumpin:prize:${v.prizeId}`);
213
+ return {
214
+ activityRef: `alipay:bumpin:${actSchemeId}`,
215
+ title: raw.cardTitle ?? "\u78B0\u51FA\u60CA\u559C",
216
+ subtitle: raw.mainTitle,
217
+ coverUrl: raw.mainPrizeImage ?? raw.cardLogo,
218
+ categoryId: "bumpin",
219
+ registrationRequired: false,
220
+ startAt: msToIso(raw.gmtStart),
221
+ endAt: msToIso(raw.gmtExpired),
222
+ offerRefs,
223
+ // 联单判定唯一真源:actType=N_COUNT_TYPE(累计 N 笔解锁型);标题关键字判定已废弃
224
+ linkedOrder: raw.actType === "N_COUNT_TYPE",
225
+ // 活动生命周期状态(归一后透传;tools 层据此驱动卡片状态化展示)
226
+ activityStatus: normalizeActStatus(raw.actStatus)
227
+ };
228
+ }
229
+ function mapBumpInActivityDetail(actSchemeId, raw) {
230
+ const activity = mapBumpInActivity(actSchemeId, raw);
231
+ const activityStatus = normalizeActStatus(raw.actStatus);
232
+ const isActive = ACTIVE_STATUSES.has(activityStatus);
233
+ const progressText = raw.totalProgress !== void 0 ? `\u76EE\u6807\uFF1A${raw.totalProgress}${raw.progressUnit ?? ""}\uFF0C\u5DF2\u5B8C\u6210\uFF1A${raw.currentProgress ?? "0"}${raw.progressUnit ?? ""}` : void 0;
234
+ const voucherDescriptions = (raw.voucherList ?? []).map(
235
+ (v) => `${v.voucherName}\uFF08${v.merchantName}\uFF0C${v.thresholdAmountText}\uFF09`
236
+ );
237
+ const statusLine = isActive ? "\u6D3B\u52A8\u8FDB\u884C\u4E2D\uFF0C\u53C2\u4E0E\u8D44\u683C\u4EE5\u652F\u4ED8\u5B9D\u5B9E\u65F6\u6821\u9A8C\u4E3A\u51C6" : activityStatus === "NOT_START" ? "\u6D3B\u52A8\u5C1A\u672A\u5F00\u59CB" : activityStatus === "RECEIVED" ? "\u6D3B\u52A8\u5DF2\u7ED3\u675F\uFF0C\u5956\u52B1\u5DF2\u9886\u53D6" : "\u6D3B\u52A8\u5DF2\u7ED3\u675F";
238
+ const conditions = [
239
+ statusLine,
240
+ ...progressText ? [progressText] : []
241
+ ];
242
+ const steps = [
243
+ { title: "\u78B0\u4E00\u4E0B\u652F\u4ED8", description: "\u4F7F\u7528\u652F\u4ED8\u5B9D\u300C\u78B0\u4E00\u4E0B\u300D\u529F\u80FD\u5B8C\u6210\u652F\u4ED8\u5373\u53EF\u53C2\u4E0E\u6D3B\u52A8" },
244
+ ...voucherDescriptions.length > 0 ? [{ title: "\u5956\u52B1\u8BF4\u660E", description: `\u53EF\u5F97\u4F18\u60E0\u5238\uFF1A${voucherDescriptions.join("\uFF1B")}` }] : []
245
+ ];
246
+ return {
247
+ ...activity,
248
+ description: raw.activityRuleDetail,
249
+ sessions: [],
250
+ guide: {
251
+ conditions,
252
+ steps,
253
+ notice: raw.voucherList && raw.voucherList.length > 0 ? JSON.stringify(raw.voucherList) : void 0
254
+ }
255
+ };
256
+ }
257
+ var AlipayBumpInService = class {
258
+ constructor(sdk, actSchemeId) {
259
+ this.sdk = sdk;
260
+ this.actSchemeId = actSchemeId;
261
+ }
262
+ sdk;
263
+ actSchemeId;
264
+ async listActivities(q, _input) {
265
+ const detail = await this.getActivityDetail(q, `alipay:bumpin:${this.actSchemeId}`);
266
+ if (!detail) return { items: [], hasMore: false };
267
+ return { items: [detail], hasMore: false };
268
+ }
269
+ async getActivityDetail(q, activityRef) {
270
+ const actSchemeId = activityRef.replace("alipay:bumpin:", "");
271
+ if (!actSchemeId || actSchemeId === activityRef) return null;
272
+ if (actSchemeId !== this.actSchemeId) return null;
273
+ const credAttrs = q.requestContext.credentials?.attributes;
274
+ const alipayOpenId = typeof credAttrs?.["openid"] === "string" ? credAttrs["openid"] : "";
275
+ const isAlipayUserId = alipayOpenId.startsWith("2088");
276
+ const bizContent = {
277
+ ...isAlipayUserId ? { user_id: alipayOpenId } : { open_id: alipayOpenId },
278
+ act_scheme_id: actSchemeId
279
+ };
280
+ console.info("[AlipayBumpIn] exec request", {
281
+ appId: this.sdk.config.appId,
282
+ method: "alipay.commerce.iotnspplaycenter.activity.query",
283
+ bizContent
284
+ });
285
+ const result = await this.sdk.exec("alipay.commerce.iotnspplaycenter.activity.query", { bizContent });
286
+ const raw = result;
287
+ console.info("[AlipayBumpIn] exec response", { appId: this.sdk.config.appId, code: raw?.code, msg: raw?.msg, subCode: raw?.subCode, raw });
288
+ if (!raw || raw.code !== "10000") return null;
289
+ const detail = mapBumpInActivityDetail(actSchemeId, raw);
290
+ console.info("[AlipayBumpIn] mapped detail", detail);
291
+ return detail;
292
+ }
293
+ async listActivitySessions(_q, _activityRef) {
294
+ return [];
295
+ }
296
+ async listActivityCategories() {
297
+ return [{ categoryId: "bumpin", name: "\u78B0\u51FA\u60CA\u559C" }];
298
+ }
299
+ };
300
+
301
+ // src/server/integrations/alipay/client.ts
302
+ import { existsSync, readFileSync } from "fs";
303
+ import path from "path";
304
+ import { AlipaySdk } from "alipay-sdk";
305
+ function parseAlipayCredentialFromFile(configPath) {
306
+ if (!configPath) return null;
307
+ const dir = path.dirname(configPath);
308
+ const file = path.join(dir, "credentials", "alipay-bumpin.json");
309
+ if (!existsSync(file)) return null;
310
+ try {
311
+ const parsed = JSON.parse(readFileSync(file, "utf8"));
312
+ const value = parsed.value;
313
+ if (value && typeof value === "object" && typeof value.appId === "string" && typeof value.privateKey === "string" && typeof value.alipayPublicKey === "string") {
314
+ return value;
315
+ }
316
+ } catch {
317
+ }
318
+ return null;
319
+ }
320
+ function parseAlipayCredentialFromDiscreteEnv() {
321
+ const appId = process.env.MINI_APP_ID;
322
+ const privateKey = process.env.MINI_APP_PRIVATE_KEY;
323
+ const alipayPublicKey = process.env.MINI_APP_ALIPAY_PUB_KEY;
324
+ if (!appId || !privateKey || !alipayPublicKey) return null;
325
+ return { appId, privateKey, alipayPublicKey };
326
+ }
327
+ function parseAlipayCredentialFromEnv() {
328
+ const raw = process.env.ALIPAY_BUMPIN;
329
+ if (!raw) return null;
330
+ try {
331
+ const parsed = JSON.parse(raw);
332
+ const value = parsed.value;
333
+ if (value && typeof value === "object" && typeof value.appId === "string" && typeof value.privateKey === "string" && typeof value.alipayPublicKey === "string") {
334
+ return value;
335
+ }
336
+ } catch {
337
+ }
338
+ return null;
339
+ }
340
+ function createAlipaySdkClient(config, configPath) {
341
+ const credential = parseAlipayCredentialFromDiscreteEnv() ?? parseAlipayCredentialFromFile(configPath) ?? parseAlipayCredentialFromEnv();
342
+ if (!credential) return null;
343
+ const { appId, privateKey, alipayPublicKey } = credential;
344
+ if (appId.startsWith("PLACEHOLDER") || privateKey.startsWith("PLACEHOLDER") || alipayPublicKey.startsWith("PLACEHOLDER")) {
345
+ return null;
346
+ }
347
+ return new AlipaySdk({
348
+ appId,
349
+ privateKey,
350
+ alipayPublicKey,
351
+ keyType: "PKCS8",
352
+ signType: "RSA2",
353
+ camelcase: true,
354
+ gateway: config.gateway
355
+ });
356
+ }
357
+ var alipayBumpInAuthStrategy = {
358
+ credentialType: "alipay-open-v1",
359
+ apply: () => ({})
360
+ };
361
+
362
+ // src/server/ports.ts
363
+ var bumpinConfigPath;
364
+ function configureAlipayBumpIn(configPath) {
365
+ bumpinConfigPath = configPath;
366
+ }
367
+ function readAlipayBumpInConfig(ctx) {
368
+ if (!bumpinConfigPath || !existsSync2(bumpinConfigPath)) return null;
369
+ let raw;
370
+ try {
371
+ raw = JSON.parse(expandEnvVars(readFileSync2(bumpinConfigPath, "utf8"), process.env, bumpinConfigPath));
372
+ } catch (e) {
373
+ ctx?.logger?.warn(`bumpin.json \u89E3\u6790\u5931\u8D25\uFF08${e.message}\uFF09`);
374
+ return null;
375
+ }
376
+ const gateway = typeof raw.gateway === "string" && raw.gateway ? raw.gateway : "";
377
+ const actSchemeId = typeof raw.actSchemeId === "string" && raw.actSchemeId ? raw.actSchemeId : "";
378
+ if (!gateway || !actSchemeId) {
379
+ ctx?.logger?.warn("bumpin.json \u5DF2\u6CE8\u5165\u4F46 gateway / actSchemeId \u7F3A\u5931");
380
+ return null;
381
+ }
382
+ const linkedCardBackgroundUrl = typeof raw.linkedCardBackgroundUrl === "string" && raw.linkedCardBackgroundUrl ? raw.linkedCardBackgroundUrl : void 0;
383
+ const credentialRef = typeof raw.credentialRef === "string" && raw.credentialRef ? raw.credentialRef : void 0;
384
+ return {
385
+ gateway,
386
+ actSchemeId,
387
+ ...linkedCardBackgroundUrl ? { linkedCardBackgroundUrl } : {},
388
+ ...credentialRef ? { credentialRef } : {}
389
+ };
390
+ }
391
+ async function resolveAlipayBumpInService(ctx) {
392
+ const cfg = readAlipayBumpInConfig(ctx);
393
+ if (!cfg) return null;
394
+ const sdk = createAlipaySdkClient({ gateway: cfg.gateway, actSchemeId: cfg.actSchemeId }, bumpinConfigPath);
395
+ return sdk ? new AlipayBumpInService(sdk, cfg.actSchemeId) : null;
396
+ }
397
+
398
+ // src/server/tools.ts
399
+ var ACT_STATUS_VALUES2 = /* @__PURE__ */ new Set(["NOT_START", "INIT", "PROCESSING", "FINISH", "RECEIVED"]);
400
+ function toCardActStatus(raw) {
401
+ return ACT_STATUS_VALUES2.has(raw ?? "") ? raw : "PROCESSING";
402
+ }
403
+ function parseAlipayRichText(text) {
404
+ if (!text) return "";
405
+ return text.replace(/#\{\{[^}]*"text":"([^"]*)"[^}]*\}\}#/g, "$1");
406
+ }
407
+ function toBumpInCardData(detail, mode, backgroundUrl) {
408
+ const progressText = detail.guide?.conditions[1] ?? "";
409
+ const [, progressTotal = "", progressUnit = "", progressCurrent = ""] = progressText.match(/目标:(.+?)(.+?),已完成:(.+?)\2/) ?? progressText.match(/目标:(\S+?)(\S+),已完成:(\S+)/) ?? ["", "", "", ""];
410
+ let vouchers = [];
411
+ if (detail.guide?.notice) {
412
+ try {
413
+ const raw = JSON.parse(detail.guide.notice);
414
+ vouchers = raw.map((v) => ({
415
+ prizeId: String(v["prizeId"] ?? v["prize_id"] ?? ""),
416
+ voucherName: String(v["voucherName"] ?? v["voucher_name"] ?? ""),
417
+ voucherType: String(v["voucherType"] ?? v["voucher_type"] ?? ""),
418
+ reductionAmount: String(v["reductionAmount"] ?? v["reduction_amount"] ?? ""),
419
+ thresholdAmountText: String(v["thresholdAmountText"] ?? v["threshold_amount_text"] ?? ""),
420
+ unit: String(v["unit"] ?? ""),
421
+ merchantName: String(v["merchantName"] ?? v["merchant_name"] ?? ""),
422
+ merchantLogo: v["merchantLogo"] != null ? String(v["merchantLogo"]) : v["merchant_logo"] != null ? String(v["merchant_logo"]) : void 0,
423
+ itemLogo: v["itemLogo"] != null ? String(v["itemLogo"]) : v["item_logo"] != null ? String(v["item_logo"]) : void 0
424
+ }));
425
+ } catch {
426
+ }
427
+ }
428
+ const mainTitle = parseAlipayRichText(detail.subtitle);
429
+ const isLinkedActivity = detail.linkedOrder === true;
430
+ return {
431
+ activityRef: detail.activityRef,
432
+ title: detail.title,
433
+ mainTitle: mainTitle || detail.title,
434
+ coverUrl: isLinkedActivity ? void 0 : detail.coverUrl,
435
+ tagLabel: isLinkedActivity ? "\u78B0\u4E00\u4E0B\u8054\u5355" : void 0,
436
+ progressStyle: isLinkedActivity ? "steps" : "bar",
437
+ backgroundUrl: isLinkedActivity ? backgroundUrl || void 0 : void 0,
438
+ actStatus: toCardActStatus(detail.activityStatus),
439
+ totalProgress: progressTotal || void 0,
440
+ currentProgress: progressCurrent || void 0,
441
+ progressUnit: progressUnit || void 0,
442
+ startAtText: detail.startAt ? new Date(detail.startAt).toLocaleDateString("zh-CN") : void 0,
443
+ endAtText: detail.endAt ? new Date(detail.endAt).toLocaleDateString("zh-CN") : void 0,
444
+ description: detail.description,
445
+ vouchers,
446
+ steps: isLinkedActivity ? detail.guide?.steps : void 0,
447
+ mode
448
+ };
449
+ }
450
+ function createBumpInTools(getCtx2) {
451
+ const getBumpInActivity = createTool({
452
+ id: "getBumpInActivity",
453
+ description: "\u67E5\u8BE2\u6211\u7684\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\u8BE6\u60C5\uFF08\u78B0\u51FA\u60CA\u559C\u3001\u78B0\u4E00\u4E0B\u3001\u6211\u7684\u60CA\u559C\u6D3B\u52A8\u3001\u652F\u4ED8\u5B9D\u4E92\u52A8\u8425\u9500\u6D3B\u52A8\uFF09\u3002\u8FD4\u56DE\u6D3B\u52A8\u6807\u9898\u3001\u5956\u52B1\u6C60\u3001\u5B8C\u6210\u8FDB\u5EA6\u4E0E\u53C2\u4E0E\u6307\u5F15\u3002",
454
+ inputSchema: z.object({}),
455
+ execute: async (_args, execCtx) => {
456
+ const ctx = getCtx2();
457
+ const reqCtx = requireToolRequestContext(execCtx, "getBumpInActivity");
458
+ if (reqCtx.identity.source === "anonymous") {
459
+ const factory = ctx.services?.resolve?.("member.status");
460
+ const status = await factory?.(reqCtx).catch(() => void 0);
461
+ const card = status ? memberGateCardOf(reqCtx.identity.userId, status) : void 0;
462
+ return {
463
+ found: false,
464
+ noResultsReason: "\u8BF7\u5148\u767B\u5F55\u540E\u518D\u67E5\u770B\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8",
465
+ ...card ? { cards: [card] } : {}
466
+ };
467
+ }
468
+ const service = await resolveAlipayBumpInService(ctx);
469
+ if (!service) return { supported: false, message: "\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\u80FD\u529B\u672A\u88C5\u914D\uFF0C\u8BF7\u8054\u7CFB\u8FD0\u8425\u914D\u7F6E\u51ED\u8BC1" };
470
+ const q = toMallQueryContext(reqCtx);
471
+ const page = await service.listActivities(q, {});
472
+ const detail = page.items[0] ?? null;
473
+ if (!detail) return { supported: true, message: "\u672A\u627E\u5230\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\uFF0C\u8BF7\u786E\u8BA4\u6D3B\u52A8\u65B9\u6848 ID \u914D\u7F6E\u662F\u5426\u6B63\u786E" };
474
+ const cardData = toBumpInCardData(detail, "detail");
475
+ return {
476
+ ...cardData,
477
+ // note 契约(W7):标题 + 进度要点(cardData.totalProgress/currentProgress 由 guide 解析)
478
+ cards: [
479
+ {
480
+ cardType: "promotion-bumpin-activity",
481
+ data: cardData,
482
+ note: `${detail.title}${cardData.currentProgress ? ` \xB7 \u5DF2\u5B8C\u6210 ${cardData.currentProgress}/${cardData.totalProgress ?? "?"}` : ""}`
483
+ }
484
+ ]
485
+ };
486
+ }
487
+ });
488
+ const getBumpInActivityBanner = createTool({
489
+ id: "getBumpInActivityBanner",
490
+ description: "\u83B7\u53D6\u78B0\u51FA\u60CA\u559C\u9996\u9875 Banner \u5C55\u793A\u6570\u636E\uFF08\u4F9B\u9996\u9875\u76F4\u63A5\u62C9\u53D6\uFF0C\u65E0\u9700\u7528\u6237\u4E3B\u52A8\u8BE2\u95EE\uFF09\u3002\u8FD4\u56DE\u6D3B\u52A8\u4E3B\u56FE\u3001\u5956\u52B1\u6982\u89C8\uFF1Bmode=banner \u4EC5\u5C55\u793A\u9876\u90E8\u6982\u89C8\u4E0D\u5C55\u5F00\u5217\u8868\u3002",
491
+ inputSchema: z.object({}),
492
+ execute: async (_args, execCtx) => {
493
+ const ctx = getCtx2();
494
+ const reqCtx = requireToolRequestContext(execCtx, "getBumpInActivityBanner");
495
+ if (reqCtx.identity.source === "anonymous") {
496
+ const factory = ctx.services?.resolve?.("member.status");
497
+ const status = await factory?.(reqCtx).catch(() => void 0);
498
+ const card = status ? memberGateCardOf(reqCtx.identity.userId, status) : void 0;
499
+ return {
500
+ supported: false,
501
+ banner: null,
502
+ noResultsReason: "\u8BF7\u5148\u767B\u5F55\u540E\u518D\u67E5\u770B\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8",
503
+ ...card ? { cards: [card] } : {}
504
+ };
505
+ }
506
+ const service = await resolveAlipayBumpInService(ctx);
507
+ if (!service) return { supported: false, banner: null };
508
+ const q = toMallQueryContext(reqCtx);
509
+ const page = await service.listActivities(q, {});
510
+ const detail = page.items[0] ?? null;
511
+ if (!detail) return { supported: true, banner: null };
512
+ const cardData = toBumpInCardData(detail, "banner");
513
+ return {
514
+ supported: true,
515
+ banner: cardData,
516
+ cards: [{ cardType: "promotion-bumpin-activity", data: cardData, note: `${detail.title} Banner` }]
517
+ };
518
+ }
519
+ });
520
+ return {
521
+ getBumpInActivity: withCardEmissionHint(getBumpInActivity, { actions: true }),
522
+ getBumpInActivityBanner: withCardEmissionHint(getBumpInActivityBanner)
523
+ };
524
+ }
525
+
526
+ // src/server/handler.ts
527
+ import { memberGateCardOf as memberGateCardOf2, toMallQueryContext as toMallQueryContext2, PROMOTION_ACTIVITY_ROUTING_POLICY } from "@tbox.cn/app-contracts-mall";
528
+ var BUMPIN_DOMAIN_HINTS = ["\u78B0\u51FA\u60CA\u559C", "\u78B0\u4E00\u4E0B", "\u78B0\u4E00\u78B0", "\u60CA\u559C"];
529
+ function isLinkedActivityScope(ctx, reqCtx) {
530
+ const policy = ctx.services.resolve(PROMOTION_ACTIVITY_ROUTING_POLICY);
531
+ return policy?.(reqCtx) ?? false;
532
+ }
533
+ var BUMPIN_INTENTS = {
534
+ "bumpin-activity": {
535
+ patterns: ["\u78B0\u51FA\u60CA\u559C", "\u78B0\u4E00\u4E0B", "\u78B0\u4E00\u78B0"],
536
+ description: "\u67E5\u8BE2\u652F\u4ED8\u5B9D\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8"
537
+ }
538
+ };
539
+ function createAlipayActivityHandler(ctx) {
540
+ return {
541
+ id: "alipay-activity",
542
+ intents: Object.values(BUMPIN_INTENTS).map(({ patterns }) => ({ patterns: [...patterns], match: "exact" })),
543
+ handle: async (action, payload, _ctx, reqCtx) => {
544
+ const content = typeof payload?.content === "string" ? payload.content.trim() : "";
545
+ if (action === "activity-router") {
546
+ if (!reqCtx || !isLinkedActivityScope(ctx, reqCtx)) {
547
+ return { cards: [], continueWith: { content } };
548
+ }
549
+ }
550
+ if (!BUMPIN_DOMAIN_HINTS.some((hint) => content.includes(hint))) {
551
+ if (action === "activity-router") {
552
+ } else {
553
+ return { cards: [], continueWith: { content } };
554
+ }
555
+ }
556
+ if (!reqCtx) return { cards: [], text: "\u652F\u4ED8\u5B9D\u6D3B\u52A8\u670D\u52A1\u4E0D\u53EF\u7528\uFF08\u7F3A\u5C11\u8FD0\u884C\u4E0A\u4E0B\u6587\uFF09" };
557
+ if (reqCtx.identity.source === "anonymous") {
558
+ const factory = ctx.services?.resolve?.("member.status");
559
+ const status = await factory?.(reqCtx).catch(() => void 0);
560
+ const card = status ? memberGateCardOf2(reqCtx.identity.userId, status) : void 0;
561
+ return { cards: card ? [card] : [], text: "\u8BF7\u5148\u767B\u5F55\u540E\u518D\u67E5\u770B\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8" };
562
+ }
563
+ const q = toMallQueryContext2(reqCtx);
564
+ const bumpInService = await resolveAlipayBumpInService(ctx);
565
+ if (!bumpInService) return { cards: [], text: "\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\u80FD\u529B\u672A\u88C5\u914D\uFF0C\u8BF7\u8054\u7CFB\u8FD0\u8425\u914D\u7F6E\u51ED\u8BC1" };
566
+ const page = await bumpInService.listActivities(q, {});
567
+ const detail = page.items[0] ?? null;
568
+ if (!detail) return { cards: [], text: "\u672A\u627E\u5230\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\uFF0C\u8BF7\u786E\u8BA4\u6D3B\u52A8\u65B9\u6848 ID \u914D\u7F6E\u662F\u5426\u6B63\u786E" };
569
+ const cardData = toBumpInCardData(detail, "detail");
570
+ return {
571
+ cards: [{ cardType: "promotion-bumpin-activity", data: cardData }],
572
+ text: `\u5DF2\u627E\u5230\u78B0\u51FA\u60CA\u559C\u6D3B\u52A8\uFF1A${detail.title}`
573
+ };
574
+ }
575
+ };
576
+ }
577
+
578
+ // src/server/index.ts
579
+ var serverCtx;
580
+ var getCtx = () => {
581
+ if (!serverCtx) throw new Error("alipay-activity \u6A21\u5757\u672A\u6CE8\u518C\uFF08ServerContext \u672A\u7ED1\u5B9A\uFF09");
582
+ return serverCtx;
583
+ };
584
+ var tools = createBumpInTools(getCtx);
585
+ var cards = {
586
+ "promotion-bumpin-activity": meta_default
587
+ };
588
+ var skills = Object.fromEntries(
589
+ (await loadModuleSkills("@tbox.cn/app-module-alipay-activity", import.meta.url)).map((s) => [s.name, s])
590
+ );
591
+ var bumpInPreTool = definePreTool(tools.getBumpInActivity, {
592
+ routing: {
593
+ mode: "keywords",
594
+ terms: ["\u78B0\u4E00\u78B0", "\u78B0\u4E00\u4E0B", "\u78B0\u51FA\u60CA\u559C", "\u78B0\u60CA\u559C", "\u60CA\u559C\u6D3B\u52A8", "NFC"]
595
+ },
596
+ decide: ({ text }) => text && text.trim().length > 0 ? { decision: "run", args: {} } : { decision: "skip", reason: "\u7A7A\u6587\u672C" },
597
+ silent: true
598
+ });
599
+ var preTools = {
600
+ "getBumpInActivity": bumpInPreTool
601
+ };
602
+ var serverModule = {
603
+ declaration: {
604
+ moduleId: "alipay-activity"
605
+ },
606
+ cards,
607
+ skills,
608
+ preTools,
609
+ register(ctx) {
610
+ serverCtx = ctx;
611
+ ctx.authStrategies.register(alipayBumpInAuthStrategy);
612
+ for (const tool of Object.values(tools)) {
613
+ ctx.tools.register(tool);
614
+ }
615
+ ctx.cards.registerMap(cards);
616
+ ctx.handlers.register(createAlipayActivityHandler(ctx));
617
+ }
618
+ };
619
+ var server_default = serverModule;
620
+
621
+ export {
622
+ meta_default,
623
+ AlipayBumpInService,
624
+ alipayBumpInAuthStrategy,
625
+ configureAlipayBumpIn,
626
+ readAlipayBumpInConfig,
627
+ resolveAlipayBumpInService,
628
+ parseAlipayRichText,
629
+ toBumpInCardData,
630
+ createBumpInTools,
631
+ createAlipayActivityHandler,
632
+ serverModule,
633
+ server_default
634
+ };
@@ -0,0 +1,11 @@
1
+ import { CardComponent } from '@tbox.cn/app-contracts';
2
+ import { ClientContext } from '@tbox.cn/app-sdk/client';
3
+
4
+ declare const clientModule: {
5
+ cards: {
6
+ readonly 'promotion-bumpin-activity': CardComponent;
7
+ };
8
+ register(ctx: ClientContext): void;
9
+ };
10
+
11
+ export { clientModule, clientModule as default };