@tbox.cn/app-module-activity-play 0.0.0-stage → 0.3.1

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 (40) hide show
  1. package/INTEGRATION.md +82 -0
  2. package/LICENSE +21 -0
  3. package/README.md +30 -2
  4. package/dist/chunk-CM4OEPC3.js +689 -0
  5. package/dist/chunk-HUBEOW44.js +98 -0
  6. package/dist/client/index.css +107 -0
  7. package/dist/client/index.d.ts +11 -0
  8. package/dist/client/index.js +6 -0
  9. package/dist/index.css +107 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +46 -0
  12. package/dist/server/index.d.ts +309 -0
  13. package/dist/server/index.js +42 -0
  14. package/package.json +67 -3
  15. package/skills/activity-play/SKILL.md +55 -0
  16. package/src/client/cards/activity-play/ActivityPlayCard.tsx +77 -0
  17. package/src/client/cards/activity-play/index.ts +1 -0
  18. package/src/client/cards/view.ts +42 -0
  19. package/src/client/index.ts +22 -0
  20. package/src/client/styles/activity-play.css +136 -0
  21. package/src/index.ts +6 -0
  22. package/src/server/cards/activity-play/meta.ts +26 -0
  23. package/src/server/cards/activity-play/samples.ts +116 -0
  24. package/src/server/cards/index.ts +7 -0
  25. package/src/server/errors.ts +101 -0
  26. package/src/server/index.ts +72 -0
  27. package/src/server/ports.ts +50 -0
  28. package/src/server/query.ts +98 -0
  29. package/src/server/routes.ts +371 -0
  30. package/src/server/tools.ts +187 -0
  31. package/tbox.module.json +62 -0
  32. package/tests/cards-capture.test.ts +110 -0
  33. package/tests/entry.test.ts +458 -0
  34. package/tests/ports.test.ts +83 -0
  35. package/tests/routes.test.ts +271 -0
  36. package/tests/samples.test.ts +53 -0
  37. package/tests/smoke.test.ts +27 -0
  38. package/tests/tools.test.ts +251 -0
  39. package/tsconfig.json +8 -0
  40. package/tsup.config.ts +27 -0
@@ -0,0 +1,689 @@
1
+ // src/server/index.ts
2
+ import { loadModuleSkills } from "@tbox.cn/app-agent-sdk-server";
3
+
4
+ // src/server/routes.ts
5
+ import { Router } from "express";
6
+ import { getRequestContext } from "@tbox.cn/app-agent-sdk-server";
7
+ import {
8
+ parseMallScope,
9
+ toMallQueryContext as toMallQueryContext2,
10
+ withMallScopeFromQuery,
11
+ mallScopeOf as mallScopeOf3,
12
+ ACTIVITY_PLAY_QUERY_SERVICE as ACTIVITY_PLAY_QUERY_SERVICE2,
13
+ ACTIVITY_PLAY_ENTRY_SERVICE as ACTIVITY_PLAY_ENTRY_SERVICE2,
14
+ activityPlayActivitySchema,
15
+ activityPlayEntrySchema,
16
+ ACTIVITY_PLAY_ENTRY_PROTOCOL_VERSION
17
+ } from "@tbox.cn/app-contracts-mall";
18
+
19
+ // src/server/query.ts
20
+ import {
21
+ activityPlayRefSchema,
22
+ mallScopeOf as mallScopeOf2,
23
+ toMallQueryContext
24
+ } from "@tbox.cn/app-contracts-mall";
25
+
26
+ // src/server/ports.ts
27
+ import { ACTIVITY_PLAY_ENTRY_SERVICE, ACTIVITY_PLAY_QUERY_SERVICE, mallScopeOf } from "@tbox.cn/app-contracts-mall";
28
+
29
+ // src/server/errors.ts
30
+ import { PROVIDER_ERROR_CODES } from "@tbox.cn/app-contracts";
31
+ var ActivityPlayHttpError = class _ActivityPlayHttpError extends Error {
32
+ httpStatus;
33
+ code;
34
+ constructor(code, httpStatus, message) {
35
+ super(message);
36
+ this.name = "ActivityPlayHttpError";
37
+ this.code = code;
38
+ this.httpStatus = httpStatus;
39
+ }
40
+ static invalidRequest(message) {
41
+ return new _ActivityPlayHttpError("INVALID_REQUEST", 400, message);
42
+ }
43
+ static scopeRequired(message) {
44
+ return new _ActivityPlayHttpError("SCOPE_REQUIRED", 400, message);
45
+ }
46
+ static scopeMismatch(message) {
47
+ return new _ActivityPlayHttpError("SCOPE_MISMATCH", 403, message);
48
+ }
49
+ static notFound(message) {
50
+ return new _ActivityPlayHttpError("ACTIVITY_NOT_FOUND", 404, message);
51
+ }
52
+ // ===== 入口准备业务错误(task-03:状态门与入口缺席——409 族,前端停止导航) =====
53
+ static activityDisabled(message) {
54
+ return new _ActivityPlayHttpError("ACTIVITY_DISABLED", 409, message);
55
+ }
56
+ static activityNotStarted(message) {
57
+ return new _ActivityPlayHttpError("ACTIVITY_NOT_STARTED", 409, message);
58
+ }
59
+ static activityEnded(message) {
60
+ return new _ActivityPlayHttpError("ACTIVITY_ENDED", 409, message);
61
+ }
62
+ static entryUnavailable(message) {
63
+ return new _ActivityPlayHttpError("ENTRY_UNAVAILABLE", 409, message);
64
+ }
65
+ static notConfigured(message) {
66
+ return new _ActivityPlayHttpError("NOT_CONFIGURED", 503, message);
67
+ }
68
+ static providerError(message) {
69
+ return new _ActivityPlayHttpError("PROVIDER_ERROR", 502, message);
70
+ }
71
+ };
72
+ var CONFIGURATION_ERROR_CODES = /* @__PURE__ */ new Set([
73
+ PROVIDER_ERROR_CODES.SERVICE_NOT_CONFIGURED,
74
+ PROVIDER_ERROR_CODES.INSTANCE_NOT_FOUND,
75
+ PROVIDER_ERROR_CODES.INSTANCE_DISABLED,
76
+ PROVIDER_ERROR_CODES.PROVIDER_NOT_FOUND,
77
+ PROVIDER_ERROR_CODES.PROVIDER_NOT_IMPLEMENTED,
78
+ PROVIDER_ERROR_CODES.BINDING_UNRESOLVED
79
+ ]);
80
+ function toActivityPlayHttpError(e) {
81
+ if (e instanceof ActivityPlayHttpError) return e;
82
+ const code = e?.code;
83
+ if (typeof code === "string" && CONFIGURATION_ERROR_CODES.has(code)) {
84
+ return ActivityPlayHttpError.notConfigured("\u6D3B\u52A8\u76EE\u5F55\u4F9B\u7ED9\u672A\u914D\u7F6E\u6216\u5B9E\u4F8B\u4E0D\u53EF\u7528");
85
+ }
86
+ return ActivityPlayHttpError.providerError("\u6D3B\u52A8\u76EE\u5F55\u4F9B\u7ED9\u6267\u884C\u5F02\u5E38");
87
+ }
88
+
89
+ // src/server/ports.ts
90
+ async function resolveActivityPlayQueryPort(reqCtx, ctx) {
91
+ const scope = mallScopeOf(reqCtx);
92
+ if (!scope.mallId) {
93
+ throw ActivityPlayHttpError.scopeRequired("\u7F3A\u5C11\u5546\u5708\uFF08mallId\uFF09");
94
+ }
95
+ if (!ctx.integrations) {
96
+ throw ActivityPlayHttpError.notConfigured("\u672A\u88C5\u914D\u96C6\u6210\u914D\u7F6E\uFF08integrations registry \u7F3A\u5E2D\uFF09");
97
+ }
98
+ return await ctx.integrations.resolveService({
99
+ service: ACTIVITY_PLAY_QUERY_SERVICE,
100
+ scope,
101
+ request: reqCtx
102
+ });
103
+ }
104
+ async function resolveActivityPlayEntryPort(reqCtx, ctx) {
105
+ const scope = mallScopeOf(reqCtx);
106
+ if (!scope.mallId) {
107
+ throw ActivityPlayHttpError.scopeRequired("\u7F3A\u5C11\u5546\u5708\uFF08mallId\uFF09");
108
+ }
109
+ if (!ctx.integrations) {
110
+ throw ActivityPlayHttpError.notConfigured("\u672A\u88C5\u914D\u96C6\u6210\u914D\u7F6E\uFF08integrations registry \u7F3A\u5E2D\uFF09");
111
+ }
112
+ return await ctx.integrations.resolveService({
113
+ service: ACTIVITY_PLAY_ENTRY_SERVICE,
114
+ scope,
115
+ request: reqCtx
116
+ });
117
+ }
118
+
119
+ // src/server/query.ts
120
+ var ACTIVITY_PLAY_LIMIT_DEFAULT = 20;
121
+ var ACTIVITY_PLAY_LIMIT_MIN = 1;
122
+ var ACTIVITY_PLAY_LIMIT_MAX = 50;
123
+ function normalizeListLimit(raw) {
124
+ if (raw === void 0) return ACTIVITY_PLAY_LIMIT_DEFAULT;
125
+ if (!/^[0-9]+$/.test(raw)) {
126
+ throw ActivityPlayHttpError.invalidRequest("limit \u53C2\u6570\u975E\u6CD5");
127
+ }
128
+ const value = Number(raw);
129
+ if (Number.isInteger(value) && value >= ACTIVITY_PLAY_LIMIT_MIN && value <= ACTIVITY_PLAY_LIMIT_MAX) {
130
+ return value;
131
+ }
132
+ throw ActivityPlayHttpError.invalidRequest(`limit \u8D85\u51FA\u8303\u56F4\uFF08${ACTIVITY_PLAY_LIMIT_MIN}\u2014${ACTIVITY_PLAY_LIMIT_MAX}\uFF09`);
133
+ }
134
+ function normalizeKeyword(raw) {
135
+ const trimmed = typeof raw === "string" ? raw.trim() : "";
136
+ return trimmed.length > 0 ? trimmed : void 0;
137
+ }
138
+ function normalizeActivityRef(raw) {
139
+ const parsed = activityPlayRefSchema.safeParse(raw.trim());
140
+ if (!parsed.success) {
141
+ throw ActivityPlayHttpError.invalidRequest("activityRef \u53C2\u6570\u975E\u6CD5");
142
+ }
143
+ return parsed.data;
144
+ }
145
+ function requireQueryContext(reqCtx) {
146
+ const scope = mallScopeOf2(reqCtx);
147
+ if (!scope.mallId) {
148
+ throw ActivityPlayHttpError.scopeRequired("\u7F3A\u5C11\u5546\u5708\uFF08\u67E5\u8BE2\u4E0A\u4E0B\u6587\u672A\u7ED1\u5B9A\u5546\u5708\uFF09");
149
+ }
150
+ return { q: toMallQueryContext(reqCtx), mallId: scope.mallId };
151
+ }
152
+ async function queryActivityPlayList(reqCtx, ctx, input) {
153
+ const { q } = requireQueryContext(reqCtx);
154
+ const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
155
+ return port.listActivities(q, input);
156
+ }
157
+ async function queryActivityPlayDetail(reqCtx, ctx, activityRef) {
158
+ const { q } = requireQueryContext(reqCtx);
159
+ const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
160
+ return port.getActivity(q, activityRef);
161
+ }
162
+ function requireMallIdOf(reqCtx) {
163
+ const mallId = mallScopeOf2(reqCtx).mallId;
164
+ if (mallId) return mallId;
165
+ throw ActivityPlayHttpError.scopeRequired("\u7F3A\u5C11\u5546\u5708\uFF08\u67E5\u8BE2\u4E0A\u4E0B\u6587\u672A\u7ED1\u5B9A\u5546\u5708\uFF09");
166
+ }
167
+
168
+ // src/server/routes.ts
169
+ var MALL_ID_RE = /^[\w-]{1,32}$/;
170
+ function queryText(req, key, label) {
171
+ const raw = req.query[key];
172
+ if (raw === void 0) return void 0;
173
+ if (typeof raw !== "string") {
174
+ throw ActivityPlayHttpError.invalidRequest(`${label} \u53C2\u6570\u975E\u6CD5\uFF08\u987B\u4E3A\u5355\u503C\u5B57\u7B26\u4E32\uFF09`);
175
+ }
176
+ const trimmed = raw.trim();
177
+ return trimmed.length > 0 ? trimmed : void 0;
178
+ }
179
+ function queryValue(req, key, label) {
180
+ const raw = req.query[key];
181
+ if (raw === void 0) return void 0;
182
+ if (typeof raw !== "string") {
183
+ throw ActivityPlayHttpError.invalidRequest(`${label} \u53C2\u6570\u975E\u6CD5\uFF08\u987B\u4E3A\u5355\u503C\u5B57\u7B26\u4E32\uFF09`);
184
+ }
185
+ return raw;
186
+ }
187
+ function resolveScopedRequestContext(req) {
188
+ const reqCtx = getRequestContext(req);
189
+ if (!reqCtx) {
190
+ throw new ActivityPlayHttpError("LOGIN_REQUIRED", 401, "\u7F3A\u5C11\u53EF\u4FE1\u8BF7\u6C42\u4E0A\u4E0B\u6587");
191
+ }
192
+ const boundMallId = parseMallScope(reqCtx.attributes)?.mallId;
193
+ const queryMallId = queryText(req, "mallId", "mallId");
194
+ if (queryMallId && boundMallId && queryMallId !== boundMallId) {
195
+ throw ActivityPlayHttpError.scopeMismatch(
196
+ `\u8BF7\u6C42\u5546\u5708\u4E0E\u5DF2\u7ED1\u5B9A\u4E0A\u4E0B\u6587\u51B2\u7A81\uFF08query=${queryMallId}, bound=${boundMallId}\uFF09`
197
+ );
198
+ }
199
+ if (queryMallId) {
200
+ if (!MALL_ID_RE.test(queryMallId)) {
201
+ throw ActivityPlayHttpError.invalidRequest("mallId \u5F62\u72B6\u975E\u6CD5");
202
+ }
203
+ return withMallScopeFromQuery(reqCtx, req.query);
204
+ }
205
+ if (boundMallId) {
206
+ if (!MALL_ID_RE.test(boundMallId)) {
207
+ throw ActivityPlayHttpError.invalidRequest("\u5DF2\u7ED1\u5B9A\u5546\u5708\u6807\u8BC6\u975E\u6CD5");
208
+ }
209
+ return reqCtx;
210
+ }
211
+ throw ActivityPlayHttpError.scopeRequired("\u7F3A\u5C11\u5546\u5708\uFF08mallId \u5FC5\u586B\u6216\u4E0A\u4E0B\u6587\u672A\u7ED1\u5B9A\u5546\u5708\uFF09");
212
+ }
213
+ function sendError(res, err, opts) {
214
+ const httpError = toActivityPlayHttpError(err);
215
+ if (httpError.httpStatus >= 500) {
216
+ const where = opts?.fields;
217
+ opts?.logger?.warn?.(
218
+ `[activity-play] \u4F9B\u7ED9\u5F02\u5E38 code=${httpError.code}` + (where?.activityRef ? ` ref=${where.activityRef}` : "") + (where?.mallId ? ` mallId=${where.mallId}` : "")
219
+ );
220
+ }
221
+ res.status(httpError.httpStatus).json({ error: { code: httpError.code, message: httpError.message } });
222
+ }
223
+ function rawActivityRefOf(req) {
224
+ const raw = typeof req.params["activityRef"] === "string" ? req.params["activityRef"] : "";
225
+ return /^[\w:.-]{1,128}$/.test(raw) ? raw : void 0;
226
+ }
227
+ function safeMallIdOf(req) {
228
+ const raw = typeof req.query["mallId"] === "string" ? req.query["mallId"] : "";
229
+ return /^[\w-]{1,32}$/.test(raw) ? raw : void 0;
230
+ }
231
+ function activityMatchesForEntry(a, b) {
232
+ return a.activityRef === b.activityRef && a.title === b.title && a.status === b.status && a.loginRequired === b.loginRequired && (a.description ?? void 0) === (b.description ?? void 0) && (a.imageUrl ?? void 0) === (b.imageUrl ?? void 0) && a.startsAt === b.startsAt && a.endsAt === b.endsAt;
233
+ }
234
+ function assertSupplyActivity(activity, expectedRef) {
235
+ const parsed = activityPlayActivitySchema.safeParse(activity);
236
+ if (!parsed.success || activity.activityRef !== expectedRef) {
237
+ throw ActivityPlayHttpError.providerError("\u6D3B\u52A8\u4F9B\u7ED9\u6570\u636E\u975E\u6CD5");
238
+ }
239
+ }
240
+ async function assertSupplyConsistency(ctx, queryPort, entryPort, scope) {
241
+ const qb = queryPort.supplyBinding;
242
+ const eb = entryPort.supplyBinding;
243
+ if (!qb || !eb || qb.provider !== eb.provider || qb.implementation !== eb.implementation || qb.directoryHash !== eb.directoryHash) {
244
+ throw ActivityPlayHttpError.entryUnavailable("\u5165\u53E3\u4F9B\u7ED9\u4E0E\u6D3B\u52A8\u76EE\u5F55\u4F9B\u7ED9\u4E0D\u4E00\u81F4");
245
+ }
246
+ const integrations = ctx.integrations;
247
+ const check = integrations?.hasProviderBinding?.bind(integrations);
248
+ const queryBound = check?.(ACTIVITY_PLAY_QUERY_SERVICE2, scope, qb.provider, qb.implementation);
249
+ const entryBound = check?.(ACTIVITY_PLAY_ENTRY_SERVICE2, scope, eb.provider, eb.implementation);
250
+ if (queryBound !== true || entryBound !== true) {
251
+ throw ActivityPlayHttpError.entryUnavailable("\u5165\u53E3\u4F9B\u7ED9\u4E0E\u6D3B\u52A8\u76EE\u5F55\u4F9B\u7ED9\u4E0D\u4E00\u81F4");
252
+ }
253
+ }
254
+ function logEntryIssue(logger, fields) {
255
+ logger?.warn?.(
256
+ `[activity-play] entry \u5F02\u5E38 code=${fields.code}` + (fields.activityRef ? ` ref=${fields.activityRef}` : "") + (fields.mallId ? ` mallId=${fields.mallId}` : "")
257
+ );
258
+ }
259
+ function validateEntryBody(req) {
260
+ const body = req.body;
261
+ if (body === void 0) return;
262
+ if (body === null || typeof body !== "object" || Array.isArray(body)) {
263
+ throw ActivityPlayHttpError.invalidRequest("\u5165\u53E3\u8BF7\u6C42\u4E1A\u52A1\u4F53\u987B\u4E3A\u7A7A\u5BF9\u8C61\u6216\u7701\u7565");
264
+ }
265
+ for (const [key, value] of Object.entries(body)) {
266
+ if (key === "context") continue;
267
+ if (value !== void 0) {
268
+ throw ActivityPlayHttpError.invalidRequest("\u5165\u53E3\u8BF7\u6C42\u4E1A\u52A1\u4F53\u987B\u4E3A\u7A7A\u5BF9\u8C61\u6216\u7701\u7565\uFF08\u4E0D\u63A5\u53D7\u76EE\u6807/\u8EAB\u4EFD/\u53C2\u6570\u8986\u76D6\uFF09");
269
+ }
270
+ }
271
+ }
272
+ function requireEntryLogin(reqCtx, activity) {
273
+ if (!activity.loginRequired) return;
274
+ const identity = reqCtx.identity;
275
+ if (!identity || identity.source === "anonymous") {
276
+ throw new ActivityPlayHttpError("LOGIN_REQUIRED", 401, "\u8BE5\u6D3B\u52A8\u8981\u6C42\u5148\u767B\u5F55");
277
+ }
278
+ }
279
+ function createActivityPlayRouter(ctx) {
280
+ const router = Router();
281
+ router.get("/activities", async (req, res) => {
282
+ try {
283
+ const scopedReqCtx = resolveScopedRequestContext(req);
284
+ const limit = normalizeListLimit(queryValue(req, "limit", "limit"));
285
+ const keyword = normalizeKeyword(queryText(req, "keyword", "keyword"));
286
+ const page = await queryActivityPlayList(scopedReqCtx, ctx, {
287
+ ...keyword ? { keyword } : {},
288
+ limit
289
+ });
290
+ res.json({ data: { items: page.items } });
291
+ } catch (err) {
292
+ sendError(res, err, {
293
+ logger: ctx.logger,
294
+ fields: { activityRef: rawActivityRefOf(req), mallId: safeMallIdOf(req) }
295
+ });
296
+ }
297
+ });
298
+ router.post("/activities/:activityRef/entry", async (req, res) => {
299
+ try {
300
+ const activityRefRaw = typeof req.params["activityRef"] === "string" ? req.params["activityRef"] : "";
301
+ const activityRef = normalizeActivityRef(activityRefRaw);
302
+ validateEntryBody(req);
303
+ const scopedReqCtx = resolveScopedRequestContext(req);
304
+ const q = toMallQueryContext2(scopedReqCtx);
305
+ const queryPort = await resolveActivityPlayQueryPort(scopedReqCtx, ctx);
306
+ const activity = await queryPort.getActivity(q, activityRef);
307
+ if (!activity) {
308
+ throw ActivityPlayHttpError.notFound(`\u6D3B\u52A8\u4E0D\u5B58\u5728: ${activityRef}`);
309
+ }
310
+ assertSupplyActivity(activity, activityRef);
311
+ if (activity.status === "disabled") {
312
+ throw ActivityPlayHttpError.activityDisabled("\u6D3B\u52A8\u5DF2\u505C\u7528");
313
+ }
314
+ if (activity.status === "not_started") {
315
+ throw ActivityPlayHttpError.activityNotStarted("\u6D3B\u52A8\u5C1A\u672A\u5F00\u59CB");
316
+ }
317
+ if (activity.status === "ended") {
318
+ throw ActivityPlayHttpError.activityEnded("\u6D3B\u52A8\u5DF2\u7ED3\u675F");
319
+ }
320
+ requireEntryLogin(scopedReqCtx, activity);
321
+ const entryPort = await resolveActivityPlayEntryPort(scopedReqCtx, ctx);
322
+ await assertSupplyConsistency(ctx, queryPort, entryPort, mallScopeOf3(scopedReqCtx));
323
+ const entryRecord = await entryPort.getEntry(q, activityRef);
324
+ if (!entryRecord) {
325
+ throw ActivityPlayHttpError.entryUnavailable("\u8BE5\u6D3B\u52A8\u6682\u672A\u767B\u8BB0\u53C2\u4E0E\u5165\u53E3");
326
+ }
327
+ assertSupplyActivity(entryRecord.activity, activityRef);
328
+ if (!activityMatchesForEntry(activity, entryRecord.activity)) {
329
+ throw ActivityPlayHttpError.entryUnavailable("\u5165\u53E3\u4F9B\u7ED9\u4E0E\u6D3B\u52A8\u76EE\u5F55\u4E0D\u4E00\u81F4");
330
+ }
331
+ const payload = {
332
+ protocolVersion: ACTIVITY_PLAY_ENTRY_PROTOCOL_VERSION,
333
+ activityRef,
334
+ target: entryRecord.target
335
+ };
336
+ const parsed = activityPlayEntrySchema.safeParse(payload);
337
+ if (!parsed.success) {
338
+ throw ActivityPlayHttpError.providerError("\u5165\u53E3\u4F9B\u7ED9\u6570\u636E\u975E\u6CD5");
339
+ }
340
+ res.setHeader("Cache-Control", "no-store");
341
+ res.json({ data: parsed.data });
342
+ } catch (err) {
343
+ const httpError = toActivityPlayHttpError(err);
344
+ if (httpError.httpStatus >= 500) {
345
+ logEntryIssue(ctx.logger, {
346
+ code: httpError.code,
347
+ activityRef: rawActivityRefOf(req),
348
+ mallId: safeMallIdOf(req)
349
+ });
350
+ }
351
+ res.setHeader("Cache-Control", "no-store");
352
+ res.status(httpError.httpStatus).json({ error: { code: httpError.code, message: httpError.message } });
353
+ }
354
+ });
355
+ router.get("/activities/:activityRef", async (req, res) => {
356
+ try {
357
+ const activityRefRaw = typeof req.params["activityRef"] === "string" ? req.params["activityRef"] : "";
358
+ const activityRef = normalizeActivityRef(activityRefRaw);
359
+ const scopedReqCtx = resolveScopedRequestContext(req);
360
+ const queryPort = await resolveActivityPlayQueryPort(scopedReqCtx, ctx);
361
+ const activity = await queryPort.getActivity(toMallQueryContext2(scopedReqCtx), activityRef);
362
+ if (!activity) {
363
+ throw ActivityPlayHttpError.notFound(`\u6D3B\u52A8\u4E0D\u5B58\u5728: ${activityRef}`);
364
+ }
365
+ res.json({ data: activity });
366
+ } catch (err) {
367
+ sendError(res, err, {
368
+ logger: ctx.logger,
369
+ fields: { activityRef: rawActivityRefOf(req), mallId: safeMallIdOf(req) }
370
+ });
371
+ }
372
+ });
373
+ return router;
374
+ }
375
+
376
+ // src/server/tools.ts
377
+ import { z } from "zod";
378
+ import {
379
+ createTool,
380
+ requireToolRequestContext,
381
+ withCardEmissionHint
382
+ } from "@tbox.cn/app-agent-sdk-server";
383
+ import { activityPlayCardDataSchema } from "@tbox.cn/app-contracts-mall";
384
+ var ACTIVITY_PLAY_CARD_TYPE = "activity-play";
385
+ function toFailureFact(err) {
386
+ const httpError = toActivityPlayHttpError(err);
387
+ return { found: false, errorCode: httpError.code, message: httpError.message };
388
+ }
389
+ function statusTextOf(status) {
390
+ switch (status) {
391
+ case "not_started":
392
+ return "\u672A\u5F00\u59CB";
393
+ case "active":
394
+ return "\u8FDB\u884C\u4E2D";
395
+ case "ended":
396
+ return "\u5DF2\u7ED3\u675F";
397
+ case "disabled":
398
+ return "\u5DF2\u505C\u7528";
399
+ }
400
+ }
401
+ function activityFactOf(activity) {
402
+ return {
403
+ activityRef: activity.activityRef,
404
+ title: activity.title,
405
+ status: activity.status,
406
+ statusText: statusTextOf(activity.status),
407
+ loginRequired: activity.loginRequired,
408
+ ...activity.description ? { description: activity.description } : {},
409
+ ...activity.startsAt ? { startsAt: activity.startsAt } : {},
410
+ ...activity.endsAt ? { endsAt: activity.endsAt } : {}
411
+ };
412
+ }
413
+ function cardNoteOf(items) {
414
+ const titles = items.slice(0, 3).map((item) => item.title).join("\u3001");
415
+ const tail = items.length > 3 ? ` \u7B49 ${items.length} \u9879` : "";
416
+ return `\u6D3B\u52A8\u73A9\u6CD5 ${items.length} \u9879 \xB7 ${titles}${tail}`.slice(0, 120);
417
+ }
418
+ function createActivityPlayTools(opts) {
419
+ const listActivityPlays = createTool({
420
+ id: "listActivityPlays",
421
+ description: "\u67E5\u8BE2\u5F53\u524D\u5546\u573A\u767B\u8BB0\u7684\u6D3B\u52A8\u73A9\u6CD5\u76EE\u5F55\uFF08\u5927\u8F6C\u76D8\u3001\u62BD\u5956\u3001\u7B7E\u5230\u3001\u4E92\u52A8\u6E38\u620F\u7B49\uFF09\uFF0C\u53EF\u7528 keyword \u6309\u540D\u79F0\u6216\u8BF4\u660E\u8FC7\u6EE4\u3001limit \u63A7\u5236\u8FD4\u56DE\u6570\u91CF\uFF08\u7F3A\u7701 20\uFF09\uFF1B\u53EA\u8BFB\u67E5\u8BE2\uFF0C\u4E0D\u6267\u884C\u53C2\u4E0E\u6216\u62BD\u5956\u3002",
422
+ inputSchema: z.object({
423
+ keyword: z.string().optional(),
424
+ limit: z.string().optional()
425
+ }),
426
+ execute: async (args, execCtx) => {
427
+ const reqCtx = requireToolRequestContext(execCtx, "listActivityPlays");
428
+ const ctx = opts.getCtx();
429
+ try {
430
+ const limit = normalizeListLimit(typeof args.limit === "string" ? args.limit : void 0);
431
+ const keyword = normalizeKeyword(typeof args.keyword === "string" ? args.keyword : void 0);
432
+ const page = await queryActivityPlayList(reqCtx, ctx, {
433
+ ...keyword ? { keyword } : {},
434
+ limit
435
+ });
436
+ if (page.items.length === 0) {
437
+ return {
438
+ found: false,
439
+ count: 0,
440
+ ...keyword ? { keyword } : {},
441
+ noResultsReason: keyword ? `\u5F53\u524D\u5546\u573A\u6CA1\u6709\u5339\u914D\u300C${keyword}\u300D\u7684\u6D3B\u52A8\u73A9\u6CD5` : "\u5F53\u524D\u5546\u573A\u6682\u672A\u767B\u8BB0\u6D3B\u52A8\u73A9\u6CD5"
442
+ };
443
+ }
444
+ const mallId = requireMallIdOf(reqCtx);
445
+ const data = { mallId, mode: "list", items: page.items };
446
+ activityPlayCardDataSchema.parse(data);
447
+ const facts = {
448
+ found: true,
449
+ mallId,
450
+ mode: "list",
451
+ count: page.items.length,
452
+ truncated: page.items.length === limit,
453
+ // 列表被 limit 截断不等同没有更多活动——提示模型勿冒称总数
454
+ items: page.items.map(activityFactOf),
455
+ // loginRequired false 只说明本系统目录不要求先登录——不得引申“参与无需登录/已具备资格”;
456
+ // 活动方/插件侧的实际要求以活动页面为准(评审返修:收紧过度承诺)
457
+ notice: "\u6D3B\u52A8\u73A9\u6CD5\u4E3A\u672C\u5730\u767B\u8BB0\u76EE\u5F55\uFF1B\u53C2\u4E0E\u987B\u5728\u6D3B\u52A8\u9875\u9762\u5B8C\u6210\u3002loginRequired \u4EC5\u8BF4\u660E\u672C\u7CFB\u7EDF\u662F\u5426\u8981\u6C42\u5148\u767B\u5F55\uFF0C\u6D3B\u52A8\u65B9\u5B9E\u9645\u8981\u6C42\uFF08\u767B\u5F55/\u8D44\u683C\uFF09\u4EE5\u6D3B\u52A8\u9875\u9762\u4E3A\u51C6\uFF1B\u67E5\u8BE2\u7ED3\u679C\u4E0D\u4EE3\u8868\u53C2\u4E0E\u8D44\u683C\u3002"
458
+ };
459
+ return { ...facts, cards: [{ cardType: ACTIVITY_PLAY_CARD_TYPE, data, note: cardNoteOf(page.items) }] };
460
+ } catch (err) {
461
+ return toFailureFact(err);
462
+ }
463
+ }
464
+ });
465
+ const getActivityPlayDetail = createTool({
466
+ id: "getActivityPlayDetail",
467
+ description: "\u6309 activityRef \u67E5\u8BE2\u5F53\u524D\u5546\u573A\u6307\u5B9A\u6D3B\u52A8\u73A9\u6CD5\u7684\u8BE6\u60C5\uFF08\u540D\u79F0\u3001\u8BF4\u660E\u3001\u5F00\u59CB/\u7ED3\u675F\u65F6\u95F4\u4E0E\u5F53\u524D\u72B6\u6001\uFF09\uFF1B\u7528\u6237\u53EA\u8BF4\u540D\u79F0\u65F6\u5148\u7528 listActivityPlays \u62FF\u5230 activityRef \u518D\u67E5\u8BE6\u60C5\u3002\u53EA\u8BFB\u67E5\u8BE2\uFF0C\u4E0D\u542B\u53C2\u4E0E\u5165\u53E3\uFF0C\u4E0D\u4EE3\u8868\u767B\u5F55\u6216\u53C2\u4E0E\u8D44\u683C\u3002",
468
+ inputSchema: z.object({
469
+ activityRef: z.string()
470
+ }),
471
+ execute: async (args, execCtx) => {
472
+ const reqCtx = requireToolRequestContext(execCtx, "getActivityPlayDetail");
473
+ const ctx = opts.getCtx();
474
+ try {
475
+ const activityRef = normalizeActivityRef(typeof args.activityRef === "string" ? args.activityRef : "");
476
+ const activity = await queryActivityPlayDetail(reqCtx, ctx, activityRef);
477
+ if (!activity) {
478
+ return {
479
+ found: false,
480
+ errorCode: "ACTIVITY_NOT_FOUND",
481
+ message: `\u672A\u627E\u5230\u6D3B\u52A8\u73A9\u6CD5: ${activityRef}`
482
+ };
483
+ }
484
+ const mallId = requireMallIdOf(reqCtx);
485
+ const data = { mallId, mode: "detail", items: [activity] };
486
+ activityPlayCardDataSchema.parse(data);
487
+ const facts = {
488
+ found: true,
489
+ mallId,
490
+ mode: "detail",
491
+ ...activityFactOf(activity),
492
+ notice: activity.loginRequired ? "\u672C\u7CFB\u7EDF\u76EE\u5F55\u8981\u6C42\u5148\u767B\u5F55\uFF1B\u53C2\u4E0E\u987B\u5728\u6D3B\u52A8\u9875\u9762\u5B8C\u6210\u3002\u6D3B\u52A8\u65B9\u5B9E\u9645\u8981\u6C42\u4E0E\u53C2\u4E0E\u8D44\u683C\u4EE5\u6D3B\u52A8\u9875\u9762\u4E3A\u51C6\u3002" : "\u672C\u7CFB\u7EDF\u76EE\u5F55\u4E0D\u8981\u6C42\u767B\u5F55\uFF1B\u53C2\u4E0E\u987B\u5728\u6D3B\u52A8\u9875\u9762\u5B8C\u6210\u3002\u6D3B\u52A8\u65B9\u662F\u5426\u8981\u6C42\u767B\u5F55\u6216\u8D44\u683C\u4EE5\u6D3B\u52A8\u9875\u9762\u4E3A\u51C6\uFF0C\u672C\u7ED3\u679C\u4E0D\u4EE3\u8868\u53C2\u4E0E\u8D44\u683C\u3002"
493
+ };
494
+ return {
495
+ ...facts,
496
+ cards: [{ cardType: ACTIVITY_PLAY_CARD_TYPE, data, note: `\u6D3B\u52A8\u8BE6\u60C5 \xB7 ${activity.title} \xB7 ${statusTextOf(activity.status)}` }]
497
+ };
498
+ } catch (err) {
499
+ return toFailureFact(err);
500
+ }
501
+ }
502
+ });
503
+ return {
504
+ listActivityPlays: withCardEmissionHint(listActivityPlays),
505
+ getActivityPlayDetail: withCardEmissionHint(getActivityPlayDetail)
506
+ };
507
+ }
508
+
509
+ // src/server/cards/activity-play/meta.ts
510
+ import { activityPlayCardDataSchema as activityPlayCardDataSchema2 } from "@tbox.cn/app-contracts-mall";
511
+
512
+ // src/server/cards/activity-play/samples.ts
513
+ var activityPlayCardSamples = [
514
+ {
515
+ label: "\u5217\u8868\xB7\u591A\u72B6\u6001",
516
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u5217\u8868",
517
+ note: "\u540C\u4E00\u76EE\u5F55\u5185 not_started/active/ended \u5E76\u5217\uFF08\u72B6\u6001\u7531\u670D\u52A1\u7AEF\u65F6\u95F4\u8BA1\u7B97\uFF09",
518
+ data: {
519
+ mallId: "mall-demo",
520
+ mode: "list",
521
+ items: [
522
+ { activityRef: "demo:wheel", title: "\u5E78\u8FD0\u5927\u8F6C\u76D8", description: "\u8FDB\u5165\u6D3B\u52A8\u9875\u9762\u67E5\u770B\u53C2\u4E0E\u89C4\u5219", status: "active", loginRequired: false },
523
+ { activityRef: "demo:points", title: "\u79EF\u5206\u7FFB\u500D", startsAt: "2026-12-01T10:00:00+08:00", endsAt: "2026-12-31T22:00:00+08:00", status: "not_started", loginRequired: true },
524
+ { activityRef: "demo:ended", title: "\u5468\u5E74\u5E86\u62BD\u5956", description: "\u6D3B\u52A8\u5DF2\u7ED3\u675F", endsAt: "2026-10-31T22:00:00+08:00", status: "ended", loginRequired: false }
525
+ ]
526
+ }
527
+ },
528
+ {
529
+ label: "\u5217\u8868\xB7\u5E26\u5C01\u9762",
530
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u5217\u8868",
531
+ note: "\u53EF\u9009 imageUrl \u5B57\u6BB5\u5728\u573A\uFF08HTTPS \u5360\u4F4D\u2014\u2014\u7F3A\u7701\u56FE\u515C\u5E95\u5206\u652F\u7684\u7B49\u4EF7\u793A\u8303\uFF09",
532
+ data: {
533
+ mallId: "mall-demo",
534
+ mode: "list",
535
+ items: [
536
+ { activityRef: "demo:wheel", title: "\u5E78\u8FD0\u5927\u8F6C\u76D8", imageUrl: "https://example.com/wheel.png", status: "active", loginRequired: false }
537
+ ]
538
+ }
539
+ },
540
+ {
541
+ label: "\u5217\u8868\xB7\u7A7A\u6001",
542
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u5217\u8868",
543
+ note: "mode=list \u5141\u8BB8\u7A7A items\uFF08\u524D\u7AEF\u7A7A\u6001\u6E32\u67D3\uFF1B\u5DE5\u5177\u5B9E\u9645\u7A7A\u7ED3\u679C\u4E0D\u4EA7\u751F\u5361\u7247\u5019\u9009\uFF0C\u6837\u672C\u4EC5\u4F9B\u6E32\u67D3\u5668\u9A8C\u8BC1\uFF09",
544
+ data: {
545
+ mallId: "mall-demo",
546
+ mode: "list",
547
+ items: []
548
+ }
549
+ },
550
+ {
551
+ label: "\u8BE6\u60C5\xB7\u8FDB\u884C\u4E2D",
552
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u8BE6\u60C5",
553
+ note: "detail \u6070\u4E00\u9879\u2014\u2014\u57FA\u51C6\u8BE6\u60C5\u6001",
554
+ data: {
555
+ mallId: "mall-demo",
556
+ mode: "detail",
557
+ items: [
558
+ {
559
+ activityRef: "demo:wheel",
560
+ title: "\u5E78\u8FD0\u5927\u8F6C\u76D8",
561
+ description: "\u8FDB\u5165\u6D3B\u52A8\u9875\u9762\u67E5\u770B\u53C2\u4E0E\u89C4\u5219\uFF0C\u4EE5\u5B9E\u9645\u6D3B\u52A8\u9875\u9762\u4E3A\u51C6",
562
+ imageUrl: "https://example.com/wheel.png",
563
+ startsAt: "2026-11-01T10:00:00+08:00",
564
+ endsAt: "2026-11-30T22:00:00+08:00",
565
+ status: "active",
566
+ loginRequired: false
567
+ }
568
+ ]
569
+ }
570
+ },
571
+ {
572
+ label: "\u8BE6\u60C5\xB7\u672A\u5F00\u59CB",
573
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u8BE6\u60C5",
574
+ note: "\u65F6\u95F4\u4E0E\u72B6\u6001\u5E76\u5B58\uFF08\u56DE\u590D\u300C\u4EC0\u4E48\u65F6\u5019\u5F00\u59CB\u300D\u7684\u5C55\u793A\u5206\u652F\uFF09",
575
+ data: {
576
+ mallId: "mall-demo",
577
+ mode: "detail",
578
+ items: [
579
+ {
580
+ activityRef: "demo:points",
581
+ title: "\u79EF\u5206\u7FFB\u500D",
582
+ description: "\u6D3B\u52A8\u671F\u95F4\u6D88\u8D39\u79EF\u5206\u53CC\u500D",
583
+ startsAt: "2026-12-01T10:00:00+08:00",
584
+ endsAt: "2026-12-31T22:00:00+08:00",
585
+ status: "not_started",
586
+ loginRequired: true
587
+ }
588
+ ]
589
+ }
590
+ },
591
+ {
592
+ label: "\u8BE6\u60C5\xB7\u5DF2\u505C\u7528",
593
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u8BE6\u60C5",
594
+ note: "\u8BE6\u60C5\u5141\u8BB8\u8FD4\u56DE disabled\uFF08\u8BF4\u660E\u505C\u7528\u72B6\u6001\uFF0C\u4E0D\u7B49\u4E8E\u4E0D\u5B58\u5728\uFF09\uFF1B\u5217\u8868\u6A21\u5F0F\u4E0D\u51FA\u73B0\u505C\u7528\u6761\u76EE",
595
+ data: {
596
+ mallId: "mall-demo",
597
+ mode: "detail",
598
+ items: [
599
+ { activityRef: "demo:hidden", title: "\u505C\u7528\u4E2D\u7684\u6D3B\u52A8", status: "disabled", loginRequired: false }
600
+ ]
601
+ }
602
+ },
603
+ {
604
+ label: "\u8BE6\u60C5\xB7\u5DF2\u7ED3\u675F",
605
+ group: "\u6D3B\u52A8\u73A9\u6CD5\xB7\u8BE6\u60C5",
606
+ note: " ended \u6001\uFF08\u5386\u53F2\u8BF4\u660E\u7528\uFF09",
607
+ data: {
608
+ mallId: "mall-demo",
609
+ mode: "detail",
610
+ items: [
611
+ { activityRef: "demo:ended", title: "\u5468\u5E74\u5E86\u62BD\u5956", description: "\u6D3B\u52A8\u5DF2\u7ED3\u675F", endsAt: "2026-10-31T22:00:00+08:00", status: "ended", loginRequired: false }
612
+ ]
613
+ }
614
+ }
615
+ ];
616
+
617
+ // src/server/cards/activity-play/meta.ts
618
+ var meta = {
619
+ cardType: "activity-play",
620
+ dataSchema: activityPlayCardDataSchema2,
621
+ displayName: "\u6D3B\u52A8\u73A9\u6CD5",
622
+ description: "\u6D3B\u52A8\u73A9\u6CD5\u76EE\u5F55\u5361\u7247\uFF08\u5217\u8868/\u8BE6\u60C5\u53CC\u6A21\u5F0F\uFF1B\u672C\u5730\u767B\u8BB0\u7684\u8F6C\u76D8\u3001\u62BD\u5956\u3001\u4E92\u52A8\u6E38\u620F\u7B49\u73A9\u6CD5\uFF09",
623
+ schemaVersion: 1,
624
+ sampleData: {
625
+ mallId: "mall-demo",
626
+ mode: "list",
627
+ items: [
628
+ { activityRef: "demo:wheel", title: "\u5E78\u8FD0\u5927\u8F6C\u76D8", description: "\u8FDB\u5165\u6D3B\u52A8\u9875\u9762\u67E5\u770B\u53C2\u4E0E\u89C4\u5219", status: "active", loginRequired: false }
629
+ ]
630
+ },
631
+ samples: activityPlayCardSamples
632
+ };
633
+ var meta_default = meta;
634
+
635
+ // src/server/cards/index.ts
636
+ var activityPlayCards = {
637
+ "activity-play": meta_default
638
+ };
639
+
640
+ // src/server/index.ts
641
+ var moduleCtx;
642
+ var getCtx = () => {
643
+ if (!moduleCtx) throw new Error("activity-play \u6A21\u5757\u672A\u6CE8\u518C\uFF08ServerContext \u672A\u7ED1\u5B9A\uFF09");
644
+ return moduleCtx;
645
+ };
646
+ var tools = createActivityPlayTools({ getCtx });
647
+ var skills = Object.fromEntries(
648
+ (await loadModuleSkills("@tbox.cn/app-module-activity-play", import.meta.url)).map((s) => [s.name, s])
649
+ );
650
+ var cards = {
651
+ "activity-play": meta_default
652
+ };
653
+ var serverModule = {
654
+ declaration: {
655
+ moduleId: "activity-play"
656
+ },
657
+ cards,
658
+ skills,
659
+ register(ctx) {
660
+ moduleCtx = ctx;
661
+ for (const tool of Object.values(tools)) {
662
+ ctx.tools.register(tool);
663
+ }
664
+ ctx.cards.registerMap(cards);
665
+ ctx.routes.register("activity-play", createActivityPlayRouter(ctx));
666
+ }
667
+ };
668
+
669
+ export {
670
+ ActivityPlayHttpError,
671
+ toActivityPlayHttpError,
672
+ resolveActivityPlayQueryPort,
673
+ resolveActivityPlayEntryPort,
674
+ ACTIVITY_PLAY_LIMIT_DEFAULT,
675
+ ACTIVITY_PLAY_LIMIT_MIN,
676
+ ACTIVITY_PLAY_LIMIT_MAX,
677
+ normalizeListLimit,
678
+ normalizeKeyword,
679
+ normalizeActivityRef,
680
+ queryActivityPlayList,
681
+ queryActivityPlayDetail,
682
+ requireMallIdOf,
683
+ activityMatchesForEntry,
684
+ createActivityPlayRouter,
685
+ ACTIVITY_PLAY_CARD_TYPE,
686
+ createActivityPlayTools,
687
+ activityPlayCards,
688
+ serverModule
689
+ };