@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,136 @@
1
+ /* ============================================================
2
+ * module-activity-play 客户端域样式(占位渲染器 2026-11-16)。
3
+ * 中立形态:卡框纪律沿平台 .tbox-card(描边/圆角/裁切平台统一),本文件只铺域 surface
4
+ * 与占位排版;厂商/品牌视觉归正式渲染器与主题包(模块兜底 = 中立,禁 --tbox-asset-* 声明)。
5
+ * 机械规则:直引 --tbox-* 恒带 fallback;rgba() 传统语法;禁 color-mix / !important。
6
+ * ============================================================ */
7
+
8
+ /* ===== 域变量 ===== */
9
+ .ap-card {
10
+ --ap-muted: var(--tbox-text-secondary, #8a8a8a);
11
+ --ap-faint: var(--tbox-text-muted, #b0b0b0);
12
+ --ap-border: var(--tbox-border-color, #ebebeb);
13
+ --ap-surface: var(--tbox-surface-subtle, #fafafa);
14
+ --ap-success: var(--tbox-success, #0f8a5f);
15
+ --ap-success-light: var(--tbox-success-light, #e6f7f0);
16
+ --ap-warning: var(--tbox-warning, #b26a00);
17
+ --ap-warning-light: var(--tbox-warning-light, #fff4e0);
18
+ }
19
+
20
+ /* ===== 卡根 ===== */
21
+ .ap-card {
22
+ padding: 12px;
23
+ background: var(--ap-surface);
24
+ display: flex;
25
+ flex-direction: column;
26
+ gap: 10px;
27
+ }
28
+
29
+ .ap-card__eyebrow {
30
+ font-size: 12px;
31
+ line-height: 1;
32
+ color: var(--ap-faint);
33
+ letter-spacing: 1px;
34
+ }
35
+
36
+ /* ===== 状态徽标 ===== */
37
+ .ap-badge {
38
+ flex: none;
39
+ font-size: 12px;
40
+ line-height: 18px;
41
+ padding: 0 6px;
42
+ border-radius: 9px;
43
+ }
44
+
45
+ .ap-badge--success {
46
+ color: var(--ap-success);
47
+ background: var(--ap-success-light);
48
+ }
49
+
50
+ .ap-badge--warning {
51
+ color: var(--ap-warning);
52
+ background: var(--ap-warning-light);
53
+ }
54
+
55
+ .ap-badge--muted {
56
+ color: var(--ap-muted);
57
+ background: rgba(0, 0, 0, 0.06);
58
+ }
59
+
60
+ /* ===== 列表形态 ===== */
61
+ .ap-card__list {
62
+ display: flex;
63
+ flex-direction: column;
64
+ }
65
+
66
+ .ap-card__row {
67
+ padding: 10px 0;
68
+ border-top: 1px solid var(--ap-border);
69
+ }
70
+
71
+ .ap-card__row:first-child {
72
+ border-top: none;
73
+ }
74
+
75
+ .ap-card__row-title {
76
+ display: flex;
77
+ align-items: center;
78
+ gap: 6px;
79
+ }
80
+
81
+ .ap-card__row-title strong {
82
+ font-size: 14px;
83
+ color: var(--tbox-text-primary, #303030);
84
+ overflow: hidden;
85
+ text-overflow: ellipsis;
86
+ white-space: nowrap;
87
+ }
88
+
89
+ .ap-card__row-desc {
90
+ margin-top: 4px;
91
+ font-size: 12px;
92
+ line-height: 18px;
93
+ color: var(--ap-muted);
94
+ display: -webkit-box;
95
+ -webkit-box-orient: vertical;
96
+ -webkit-line-clamp: 2;
97
+ overflow: hidden;
98
+ }
99
+
100
+ .ap-card__empty {
101
+ font-size: 13px;
102
+ line-height: 20px;
103
+ color: var(--ap-muted);
104
+ }
105
+
106
+ /* ===== 详情形态 ===== */
107
+ .ap-card--detail .ap-card__detail-head {
108
+ display: flex;
109
+ align-items: center;
110
+ gap: 8px;
111
+ flex-wrap: wrap;
112
+ }
113
+
114
+ .ap-card--detail .ap-card__detail-head strong {
115
+ font-size: 16px;
116
+ color: var(--tbox-text-primary, #303030);
117
+ }
118
+
119
+ .ap-card--detail .ap-card__detail-desc {
120
+ font-size: 13px;
121
+ line-height: 20px;
122
+ color: var(--ap-muted);
123
+ }
124
+
125
+ .ap-card__cover {
126
+ width: 100%;
127
+ max-height: 180px;
128
+ object-fit: cover;
129
+ border-radius: 6px;
130
+ }
131
+
132
+ .ap-card__meta {
133
+ font-size: 12px;
134
+ line-height: 18px;
135
+ color: var(--ap-faint);
136
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * module-activity-play 主入口(package.json exports "." 指向)。
3
+ * 消费方通常走 ./server / ./client 子路径;主入口聚合导出便于直接引用。
4
+ */
5
+ export * from './server';
6
+ export * from './client';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * activity-play 卡元数据(单一稳定传输卡型;mode 区分列表/详情——视觉由前端渲染器组织)。
3
+ * 数据契约单源 = contracts-mall activityPlayCardDataSchema(条目复用 activityPlayActivitySchema,
4
+ * 卡 data 不携带 B 端 enabled 配置字段)。信封字段(ID/时间戳/历史标记)由 SDK 生成。
5
+ */
6
+ import type { CardMeta } from '@tbox.cn/app-contracts';
7
+ import { activityPlayCardDataSchema } from '@tbox.cn/app-contracts-mall';
8
+ import { activityPlayCardSamples } from './samples';
9
+
10
+ const meta: CardMeta<typeof activityPlayCardDataSchema> = {
11
+ cardType: 'activity-play',
12
+ dataSchema: activityPlayCardDataSchema,
13
+ displayName: '活动玩法',
14
+ description: '活动玩法目录卡片(列表/详情双模式;本地登记的转盘、抽奖、互动游戏等玩法)',
15
+ schemaVersion: 1,
16
+ sampleData: {
17
+ mallId: 'mall-demo',
18
+ mode: 'list',
19
+ items: [
20
+ { activityRef: 'demo:wheel', title: '幸运大转盘', description: '进入活动页面查看参与规则', status: 'active', loginRequired: false },
21
+ ],
22
+ },
23
+ samples: activityPlayCardSamples,
24
+ };
25
+
26
+ export default meta;
@@ -0,0 +1,116 @@
1
+ /**
2
+ * activity-play 卡附加预览样本(task-02——覆盖列表/详情/空态与四种活动状态)。
3
+ *
4
+ * 方法论(对齐 member/parking 画廊沉淀约定):
5
+ * - 样本即契约:全样本 data 必须 activityPlayCardDataSchema 校验通过且无 shape 外键
6
+ * (module tests/samples.test.ts 双断言);
7
+ * - mallId 恒用 demo 商圈标识(卡片 data 的 mallId 来自执行上下文——样本仅预览展示位);
8
+ * - imageUrl 只用 example.com 占位(缺省图兜底分支的等价示范);
9
+ * - 时间形态为含时区 ISO(前端展示格式不在后端样本职责内)。
10
+ */
11
+ import type { CardSample } from '@tbox.cn/app-contracts';
12
+ import type { ActivityPlayCardData } from '@tbox.cn/app-contracts-mall';
13
+
14
+ export const activityPlayCardSamples: CardSample<ActivityPlayCardData>[] = [
15
+ {
16
+ label: '列表·多状态',
17
+ group: '活动玩法·列表',
18
+ note: '同一目录内 not_started/active/ended 并列(状态由服务端时间计算)',
19
+ data: {
20
+ mallId: 'mall-demo',
21
+ mode: 'list',
22
+ items: [
23
+ { activityRef: 'demo:wheel', title: '幸运大转盘', description: '进入活动页面查看参与规则', status: 'active', loginRequired: false },
24
+ { activityRef: 'demo:points', title: '积分翻倍', startsAt: '2026-12-01T10:00:00+08:00', endsAt: '2026-12-31T22:00:00+08:00', status: 'not_started', loginRequired: true },
25
+ { activityRef: 'demo:ended', title: '周年庆抽奖', description: '活动已结束', endsAt: '2026-10-31T22:00:00+08:00', status: 'ended', loginRequired: false },
26
+ ],
27
+ },
28
+ },
29
+ {
30
+ label: '列表·带封面',
31
+ group: '活动玩法·列表',
32
+ note: '可选 imageUrl 字段在场(HTTPS 占位——缺省图兜底分支的等价示范)',
33
+ data: {
34
+ mallId: 'mall-demo',
35
+ mode: 'list',
36
+ items: [
37
+ { activityRef: 'demo:wheel', title: '幸运大转盘', imageUrl: 'https://example.com/wheel.png', status: 'active', loginRequired: false },
38
+ ],
39
+ },
40
+ },
41
+ {
42
+ label: '列表·空态',
43
+ group: '活动玩法·列表',
44
+ note: 'mode=list 允许空 items(前端空态渲染;工具实际空结果不产生卡片候选,样本仅供渲染器验证)',
45
+ data: {
46
+ mallId: 'mall-demo',
47
+ mode: 'list',
48
+ items: [],
49
+ },
50
+ },
51
+ {
52
+ label: '详情·进行中',
53
+ group: '活动玩法·详情',
54
+ note: 'detail 恰一项——基准详情态',
55
+ data: {
56
+ mallId: 'mall-demo',
57
+ mode: 'detail',
58
+ items: [
59
+ {
60
+ activityRef: 'demo:wheel',
61
+ title: '幸运大转盘',
62
+ description: '进入活动页面查看参与规则,以实际活动页面为准',
63
+ imageUrl: 'https://example.com/wheel.png',
64
+ startsAt: '2026-11-01T10:00:00+08:00',
65
+ endsAt: '2026-11-30T22:00:00+08:00',
66
+ status: 'active',
67
+ loginRequired: false,
68
+ },
69
+ ],
70
+ },
71
+ },
72
+ {
73
+ label: '详情·未开始',
74
+ group: '活动玩法·详情',
75
+ note: '时间与状态并存(回复「什么时候开始」的展示分支)',
76
+ data: {
77
+ mallId: 'mall-demo',
78
+ mode: 'detail',
79
+ items: [
80
+ {
81
+ activityRef: 'demo:points',
82
+ title: '积分翻倍',
83
+ description: '活动期间消费积分双倍',
84
+ startsAt: '2026-12-01T10:00:00+08:00',
85
+ endsAt: '2026-12-31T22:00:00+08:00',
86
+ status: 'not_started',
87
+ loginRequired: true,
88
+ },
89
+ ],
90
+ },
91
+ },
92
+ {
93
+ label: '详情·已停用',
94
+ group: '活动玩法·详情',
95
+ note: '详情允许返回 disabled(说明停用状态,不等于不存在);列表模式不出现停用条目',
96
+ data: {
97
+ mallId: 'mall-demo',
98
+ mode: 'detail',
99
+ items: [
100
+ { activityRef: 'demo:hidden', title: '停用中的活动', status: 'disabled', loginRequired: false },
101
+ ],
102
+ },
103
+ },
104
+ {
105
+ label: '详情·已结束',
106
+ group: '活动玩法·详情',
107
+ note: ' ended 态(历史说明用)',
108
+ data: {
109
+ mallId: 'mall-demo',
110
+ mode: 'detail',
111
+ items: [
112
+ { activityRef: 'demo:ended', title: '周年庆抽奖', description: '活动已结束', endsAt: '2026-10-31T22:00:00+08:00', status: 'ended', loginRequired: false },
113
+ ],
114
+ },
115
+ },
116
+ ];
@@ -0,0 +1,7 @@
1
+ /** activity-play 卡 meta 聚合导出面(注册真源 = server/index.ts 内联 cards 字面量——
2
+ * promotion 先例同构双面)。 */
3
+ import activityPlayMeta from './activity-play/meta';
4
+
5
+ export const activityPlayCards = {
6
+ 'activity-play': activityPlayMeta,
7
+ } as const;
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 活动玩法 HTTP 错误模型(模块自建统一错误面)。
3
+ * 响应形状 = {"error":{"code","message"}}(嵌套信封——与平台 auth 面扁平方差并存,
4
+ * 是本域接口基线的约定形态,见 docs/plans/activity-play-plugin/api.md)。
5
+ * 纪律:message 面向调用方可读;供给/凭证原始错误细节不透传(日志侧留痕)。
6
+ */
7
+ import { PROVIDER_ERROR_CODES } from '@tbox.cn/app-contracts';
8
+
9
+ export type ActivityPlayErrorCode =
10
+ | 'INVALID_REQUEST'
11
+ | 'SCOPE_REQUIRED'
12
+ | 'SCOPE_MISMATCH'
13
+ | 'LOGIN_REQUIRED'
14
+ | 'ACTIVITY_NOT_FOUND'
15
+ | 'ACTIVITY_DISABLED'
16
+ | 'ACTIVITY_NOT_STARTED'
17
+ | 'ACTIVITY_ENDED'
18
+ | 'ENTRY_UNAVAILABLE'
19
+ | 'NOT_CONFIGURED'
20
+ | 'PROVIDER_ERROR';
21
+
22
+ /** 可直接渲染的 HTTP 错误(service/route 层统一抛出/转换) */
23
+ export class ActivityPlayHttpError extends Error {
24
+ readonly httpStatus: number;
25
+ readonly code: ActivityPlayErrorCode;
26
+
27
+ constructor(code: ActivityPlayErrorCode, httpStatus: number, message: string) {
28
+ super(message);
29
+ this.name = 'ActivityPlayHttpError';
30
+ this.code = code;
31
+ this.httpStatus = httpStatus;
32
+ }
33
+
34
+ static invalidRequest(message: string): ActivityPlayHttpError {
35
+ return new ActivityPlayHttpError('INVALID_REQUEST', 400, message);
36
+ }
37
+
38
+ static scopeRequired(message: string): ActivityPlayHttpError {
39
+ return new ActivityPlayHttpError('SCOPE_REQUIRED', 400, message);
40
+ }
41
+
42
+ static scopeMismatch(message: string): ActivityPlayHttpError {
43
+ return new ActivityPlayHttpError('SCOPE_MISMATCH', 403, message);
44
+ }
45
+
46
+ static notFound(message: string): ActivityPlayHttpError {
47
+ return new ActivityPlayHttpError('ACTIVITY_NOT_FOUND', 404, message);
48
+ }
49
+
50
+ // ===== 入口准备业务错误(task-03:状态门与入口缺席——409 族,前端停止导航) =====
51
+
52
+ static activityDisabled(message: string): ActivityPlayHttpError {
53
+ return new ActivityPlayHttpError('ACTIVITY_DISABLED', 409, message);
54
+ }
55
+
56
+ static activityNotStarted(message: string): ActivityPlayHttpError {
57
+ return new ActivityPlayHttpError('ACTIVITY_NOT_STARTED', 409, message);
58
+ }
59
+
60
+ static activityEnded(message: string): ActivityPlayHttpError {
61
+ return new ActivityPlayHttpError('ACTIVITY_ENDED', 409, message);
62
+ }
63
+
64
+ static entryUnavailable(message: string): ActivityPlayHttpError {
65
+ return new ActivityPlayHttpError('ENTRY_UNAVAILABLE', 409, message);
66
+ }
67
+
68
+ static notConfigured(message: string): ActivityPlayHttpError {
69
+ return new ActivityPlayHttpError('NOT_CONFIGURED', 503, message);
70
+ }
71
+
72
+ static providerError(message: string): ActivityPlayHttpError {
73
+ return new ActivityPlayHttpError('PROVIDER_ERROR', 502, message);
74
+ }
75
+ }
76
+
77
+ /** 供给侧错误 → NOT_CONFIGURED(配置/装配缺口,非供给执行异常)的 error code 集 */
78
+ const CONFIGURATION_ERROR_CODES = new Set<string>([
79
+ PROVIDER_ERROR_CODES.SERVICE_NOT_CONFIGURED,
80
+ PROVIDER_ERROR_CODES.INSTANCE_NOT_FOUND,
81
+ PROVIDER_ERROR_CODES.INSTANCE_DISABLED,
82
+ PROVIDER_ERROR_CODES.PROVIDER_NOT_FOUND,
83
+ PROVIDER_ERROR_CODES.PROVIDER_NOT_IMPLEMENTED,
84
+ PROVIDER_ERROR_CODES.BINDING_UNRESOLVED,
85
+ ]);
86
+
87
+ /**
88
+ * 任意异常 → ActivityPlayHttpError 归一(映射守门):
89
+ * - 已是本域 HTTP 错误 → 原样;
90
+ * - 供给错误码(未配置/实例缺席或禁用/绑定未解析/未实施)→ 503 NOT_CONFIGURED;
91
+ * - 其余(供给配置非法、执行异常、未知)→ 502 PROVIDER_ERROR——不吐原始 message
92
+ * (防配置/凭证内容泄漏),细节由调用方 logger 留痕。
93
+ */
94
+ export function toActivityPlayHttpError(e: unknown): ActivityPlayHttpError {
95
+ if (e instanceof ActivityPlayHttpError) return e;
96
+ const code = (e as { code?: string })?.code;
97
+ if (typeof code === 'string' && CONFIGURATION_ERROR_CODES.has(code)) {
98
+ return ActivityPlayHttpError.notConfigured('活动目录供给未配置或实例不可用');
99
+ }
100
+ return ActivityPlayHttpError.providerError('活动目录供给执行异常');
101
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * module-activity-play 服务端注册入口(task-02 起含工具/卡片/技能)。
3
+ * - 声明 activity-play.query 槽 own/consume(供给归 provider 包,本模块零 provider import);
4
+ * - register 只注册不解析(resolveService 仅路由/工具 execute 内);工具实例模块顶层
5
+ * 创建一次(getCtx 惰性取注册期 ctx;请求级供给在每次 execute 按各自上下文解析,
6
+ * 无请求态缓存——连续/并发跨用户跨商圈不复用解析结果);
7
+ * - 两只只读工具经 withCardEmissionHint 包装后注册(声明 = 注册同一实例);
8
+ * - 卡片单一稳定卡型 activity-play(mode 承载列表/详情);client 渲染器由前端负责人
9
+ * ——2026-11-16 用户授权先行占位 renderer(中立保形,装配门禁解锁;正式渲染器落地后整体替换 client 卡目录);
10
+ * - 路由自注册闭环(ctx.routes.mount 先挂、registerAll 后注册即时生效)。
11
+ */
12
+ import type { ServerContext, ServerModule } from '@tbox.cn/app-agent-sdk-server';
13
+ import { loadModuleSkills } from '@tbox.cn/app-agent-sdk-server';
14
+ import { createActivityPlayRouter } from './routes';
15
+ import { createActivityPlayTools } from './tools';
16
+ import activityPlayMeta from './cards/activity-play/meta';
17
+
18
+ export { createActivityPlayRouter, activityMatchesForEntry } from './routes';
19
+ export { resolveActivityPlayQueryPort, resolveActivityPlayEntryPort } from './ports';
20
+ export { createActivityPlayTools, ACTIVITY_PLAY_CARD_TYPE } from './tools';
21
+ export { activityPlayCards } from './cards';
22
+ export {
23
+ ACTIVITY_PLAY_LIMIT_DEFAULT,
24
+ ACTIVITY_PLAY_LIMIT_MIN,
25
+ ACTIVITY_PLAY_LIMIT_MAX,
26
+ normalizeListLimit,
27
+ normalizeKeyword,
28
+ normalizeActivityRef,
29
+ queryActivityPlayList,
30
+ queryActivityPlayDetail,
31
+ requireMallIdOf,
32
+ } from './query';
33
+ export { ActivityPlayHttpError, toActivityPlayHttpError } from './errors';
34
+ export type { ActivityPlayErrorCode } from './errors';
35
+
36
+ /** 模块级 ServerContext 引用(register 期绑定;工具 execute 内读取——实例分裂免疫) */
37
+ let moduleCtx: ServerContext | undefined;
38
+ const getCtx = (): ServerContext => {
39
+ if (!moduleCtx) throw new Error('activity-play 模块未注册(ServerContext 未绑定)');
40
+ return moduleCtx;
41
+ };
42
+
43
+ /** 工具实例顶层创建(register 幂等;wrapped 实例 = 声明/注册同一份) */
44
+ const tools = createActivityPlayTools({ getCtx });
45
+
46
+ /** 本地 skill(文件化:包内 skills/activity-play/;声明式通道装配) */
47
+ const skills = Object.fromEntries(
48
+ (await loadModuleSkills('@tbox.cn/app-module-activity-play', import.meta.url)).map((s) => [s.name, s]),
49
+ );
50
+
51
+ /** cards map 内联字面量(值 → meta 单文件——doctor card-type-consistent 声明位解析形态,
52
+ * 与 promotion 先例同构;聚合导出面见 ./cards) */
53
+ const cards = {
54
+ 'activity-play': activityPlayMeta,
55
+ } as const;
56
+
57
+ export const serverModule = {
58
+ declaration: {
59
+ moduleId: 'activity-play',
60
+ },
61
+ cards,
62
+ skills,
63
+ register(ctx: ServerContext): void {
64
+ moduleCtx = ctx;
65
+ for (const tool of Object.values(tools)) {
66
+ ctx.tools.register(tool);
67
+ }
68
+ ctx.cards.registerMap(cards);
69
+ // GET /api/m/activity-play/activities + /api/m/activity-play/activities/:activityRef
70
+ ctx.routes.register('activity-play', createActivityPlayRouter(ctx));
71
+ },
72
+ } satisfies ServerModule;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * activity-play 槽供给解析(query + entry 双槽——唯一消费通道,模块零 provider import,
3
+ * 经 ctx.integrations registry 解析;实例绑定决定供给范围)。
4
+ * 本域无 InMemory 兜底:预期缺席/装配缺口显性 503(NOT_CONFIGURED),供给执行
5
+ * 异常上抛 502(PROVIDER_ERROR)——不得当空结果、不回退演示数据。
6
+ * entry 槽未配置(仅查询形态)时,列表/详情/工具不受影响(consumes optional),
7
+ * 只有入口准备路由在该解析处显性失败 503。
8
+ */
9
+ import type { ActivityPlayEntryPort, ActivityPlayQueryPort } from '@tbox.cn/app-contracts-mall';
10
+ import { ACTIVITY_PLAY_ENTRY_SERVICE, ACTIVITY_PLAY_QUERY_SERVICE, mallScopeOf } from '@tbox.cn/app-contracts-mall';
11
+ import type { RequestContext } from '@tbox.cn/app-contracts';
12
+ import type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
13
+ import { ActivityPlayHttpError } from './errors';
14
+
15
+ export async function resolveActivityPlayQueryPort(
16
+ reqCtx: RequestContext,
17
+ ctx: ServerContext,
18
+ ): Promise<ActivityPlayQueryPort> {
19
+ const scope = mallScopeOf(reqCtx);
20
+ if (!scope.mallId) {
21
+ throw ActivityPlayHttpError.scopeRequired('缺少商圈(mallId)');
22
+ }
23
+ if (!ctx.integrations) {
24
+ throw ActivityPlayHttpError.notConfigured('未装配集成配置(integrations registry 缺席)');
25
+ }
26
+ return (await ctx.integrations.resolveService({
27
+ service: ACTIVITY_PLAY_QUERY_SERVICE,
28
+ scope,
29
+ request: reqCtx,
30
+ })) as ActivityPlayQueryPort;
31
+ }
32
+
33
+ /** activity-play.entry 槽供给解析(口径与 query 槽同——错误分类走同一 errors 单源) */
34
+ export async function resolveActivityPlayEntryPort(
35
+ reqCtx: RequestContext,
36
+ ctx: ServerContext,
37
+ ): Promise<ActivityPlayEntryPort> {
38
+ const scope = mallScopeOf(reqCtx);
39
+ if (!scope.mallId) {
40
+ throw ActivityPlayHttpError.scopeRequired('缺少商圈(mallId)');
41
+ }
42
+ if (!ctx.integrations) {
43
+ throw ActivityPlayHttpError.notConfigured('未装配集成配置(integrations registry 缺席)');
44
+ }
45
+ return (await ctx.integrations.resolveService({
46
+ service: ACTIVITY_PLAY_ENTRY_SERVICE,
47
+ scope,
48
+ request: reqCtx,
49
+ })) as ActivityPlayEntryPort;
50
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * 活动玩法查询核心(HTTP 与工具两只消费方的共源层——task-02 抽取)。
3
+ * 职责:可信上下文 → 共用查询(scope 校验/供给解析/错误分类单源),两侧只做各自的
4
+ * 入参形态解析(HTTP query 形态 / 工具字符串化 args 形态)与出口
5
+ * (HTTP 错误信封 / 工具失败事实)。任何一侧不得直接读 provider 配置。
6
+ *
7
+ * 语义(任务 01/02 钉死):无商圈或缺席 → SCOPE_REQUIRED;供给配置缺口/实例缺席/禁用
8
+ * → NOT_CONFIGURED;供给异常(含非法配置)→ PROVIDER_ERROR;未知引用 → null(调用方
9
+ * 分别映射 404 / ACTIVITY_NOT_FOUND 事实)。错误全走 ActivityPlayHttpError(errors.ts
10
+ * 单源分类,HTTP 信封与工具失败事实共用 code/message 面)。
11
+ */
12
+ import type { ActivityPlayActivity, ActivityPlayPage } from '@tbox.cn/app-contracts-mall';
13
+ import {
14
+ activityPlayRefSchema,
15
+ mallScopeOf,
16
+ toMallQueryContext,
17
+ } from '@tbox.cn/app-contracts-mall';
18
+ import type { ActivityPlayQueryContext } from '@tbox.cn/app-contracts-mall';
19
+ import type { RequestContext } from '@tbox.cn/app-contracts';
20
+ import type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
21
+ import { resolveActivityPlayQueryPort } from './ports';
22
+ import { ActivityPlayHttpError } from './errors';
23
+
24
+ /** 列表数量契约(缺省 20;范围 1—50——HTTP 与工具同源非法拒绝,不静默钳制) */
25
+ export const ACTIVITY_PLAY_LIMIT_DEFAULT = 20;
26
+ export const ACTIVITY_PLAY_LIMIT_MIN = 1;
27
+ export const ACTIVITY_PLAY_LIMIT_MAX = 50;
28
+
29
+ /**
30
+ * limit 归一(字符串化入参与 HTTP query 同规则):
31
+ * **缺席**(undefined——字段没传)→ 缺省 20;传了但为空串/纯空白/非数字/越界 →
32
+ * INVALID_REQUEST(明确拒绝——“未传”与“传了非法值”二分,空值不静默变成缺省数量;
33
+ * 评审返修:此前空白串被归为缺省,违背任务书空值拒绝语义)。
34
+ */
35
+ export function normalizeListLimit(raw: string | undefined): number {
36
+ if (raw === undefined) return ACTIVITY_PLAY_LIMIT_DEFAULT;
37
+ if (!/^[0-9]+$/.test(raw)) {
38
+ throw ActivityPlayHttpError.invalidRequest('limit 参数非法');
39
+ }
40
+ const value = Number(raw);
41
+ if (Number.isInteger(value) && value >= ACTIVITY_PLAY_LIMIT_MIN && value <= ACTIVITY_PLAY_LIMIT_MAX) {
42
+ return value;
43
+ }
44
+ throw ActivityPlayHttpError.invalidRequest(`limit 超出范围(${ACTIVITY_PLAY_LIMIT_MIN}—${ACTIVITY_PLAY_LIMIT_MAX})`);
45
+ }
46
+
47
+ /** keyword 归一:trim 后空串按缺省;'invalid' 等普通字符串是合法关键词(不哨兵化) */
48
+ export function normalizeKeyword(raw: string | undefined): string | undefined {
49
+ const trimmed = typeof raw === 'string' ? raw.trim() : '';
50
+ return trimmed.length > 0 ? trimmed : undefined;
51
+ }
52
+
53
+ /** activityRef 校验(复用契约单源;附件给出的引用非法即 INVALID_REQUEST) */
54
+ export function normalizeActivityRef(raw: string): string {
55
+ const parsed = activityPlayRefSchema.safeParse(raw.trim());
56
+ if (!parsed.success) {
57
+ throw ActivityPlayHttpError.invalidRequest('activityRef 参数非法');
58
+ }
59
+ return parsed.data;
60
+ }
61
+
62
+ /** 可信查询上下文组装(缺商圈显性失败——不默认其他商圈) */
63
+ function requireQueryContext(reqCtx: RequestContext): { q: ActivityPlayQueryContext; mallId: string } {
64
+ const scope = mallScopeOf(reqCtx);
65
+ if (!scope.mallId) {
66
+ throw ActivityPlayHttpError.scopeRequired('缺少商圈(查询上下文未绑定商圈)');
67
+ }
68
+ return { q: toMallQueryContext(reqCtx), mallId: scope.mallId };
69
+ }
70
+
71
+ /** 列表查询(供给侧承载 keyword 匹配/停用隐藏/limit 截断——本层零重复实现) */
72
+ export async function queryActivityPlayList(
73
+ reqCtx: RequestContext,
74
+ ctx: ServerContext,
75
+ input: { keyword?: string; limit: number },
76
+ ): Promise<ActivityPlayPage> {
77
+ const { q } = requireQueryContext(reqCtx);
78
+ const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
79
+ return port.listActivities(q, input);
80
+ }
81
+
82
+ /** 详情查询(存在 → 活动对象[可携 disabled];不存在 → null[两调用方各自映射 404/失败事实]) */
83
+ export async function queryActivityPlayDetail(
84
+ reqCtx: RequestContext,
85
+ ctx: ServerContext,
86
+ activityRef: string,
87
+ ): Promise<ActivityPlayActivity | null> {
88
+ const { q } = requireQueryContext(reqCtx);
89
+ const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
90
+ return port.getActivity(q, activityRef);
91
+ }
92
+
93
+ /** 当前商圈标识(join key:出卡 mallId 恒取执行上下文 scope,非模型输入/硬编码) */
94
+ export function requireMallIdOf(reqCtx: RequestContext): string {
95
+ const mallId = mallScopeOf(reqCtx).mallId;
96
+ if (mallId) return mallId;
97
+ throw ActivityPlayHttpError.scopeRequired('缺少商圈(查询上下文未绑定商圈)');
98
+ }