@tbox.cn/app-module-activity-play 0.3.2 → 0.4.0
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/INTEGRATION.md +59 -80
- package/README.md +10 -2
- package/dist/chunk-GDHTMID6.js +185 -0
- package/dist/chunk-XIDGKP5D.js +1154 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +38 -10
- package/dist/server/index.d.ts +593 -85
- package/dist/server/index.js +37 -9
- package/package.json +5 -5
- package/skills/activity-play/SKILL.md +41 -15
- package/src/client/cards/activity-play/ActivityPlayCard.tsx +3 -3
- package/src/client/cards/activity-play-prize/ActivityPlayPrizeDetailCard.tsx +44 -0
- package/src/client/cards/activity-play-prize/ActivityPlayPrizeListCard.tsx +62 -0
- package/src/client/cards/activity-play-prize/index.ts +8 -0
- package/src/client/cards/view.ts +6 -3
- package/src/client/index.ts +6 -0
- package/src/server/cards/activity-play/meta.ts +5 -4
- package/src/server/cards/activity-play/samples.ts +43 -18
- package/src/server/cards/index.ts +5 -1
- package/src/server/errors.ts +9 -28
- package/src/server/index.ts +48 -11
- package/src/server/ports.ts +4 -25
- package/src/server/prizes/ports.ts +35 -0
- package/src/server/prizes/query.ts +11 -8
- package/src/server/prizes/routes.ts +139 -0
- package/src/server/prizes/tools.ts +180 -0
- package/src/server/query.ts +47 -28
- package/src/server/routes.ts +44 -230
- package/src/server/tools.ts +36 -31
- package/tbox.module.json +15 -8
- package/tests/cards-capture.test.ts +3 -4
- package/tests/ports.test.ts +57 -0
- package/tests/prizes-routes.test.ts +230 -0
- package/tests/prizes-tools.test.ts +216 -0
- package/tests/prizes.test.ts +33 -15
- package/tests/routes.test.ts +89 -16
- package/tests/samples.test.ts +2 -2
- package/tests/smoke.test.ts +8 -4
- package/tests/tools.test.ts +50 -31
- package/dist/chunk-CM4OEPC3.js +0 -689
- package/dist/chunk-HUBEOW44.js +0 -98
- package/tests/entry.test.ts +0 -458
package/src/server/query.ts
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 活动玩法查询核心(HTTP 与工具两只消费方的共源层——task-02
|
|
3
|
-
* 职责:可信上下文 → 共用查询(scope
|
|
4
|
-
*
|
|
5
|
-
* (HTTP 错误信封 / 工具失败事实)。任何一侧不得直接读 provider
|
|
2
|
+
* 活动玩法查询核心(HTTP 与工具两只消费方的共源层——task-02 抽取,远端活动改造 2026-11-17)。
|
|
3
|
+
* 职责:可信上下文 → 共用查询(scope 校验/供给解析/错误分类/出站契约校验单源),两侧只做
|
|
4
|
+
* 各自的入参形态解析(HTTP query 形态 / 工具字符串化 args 形态)与出口
|
|
5
|
+
* (HTTP 错误信封 / 工具失败事实)。任何一侧不得直接读 provider 连接配置。
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
* → NOT_CONFIGURED
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* 语义(远端活动改造钉死):无商圈或缺席 → SCOPE_REQUIRED;供给连接缺口/实例缺席/禁用
|
|
8
|
+
* → NOT_CONFIGURED;供给异常(含连接配置非法、上游失败)→ PROVIDER_ERROR;上游不存在
|
|
9
|
+
* → null(调用方分别映射 404 / ACTIVITY_NOT_FOUND 事实)。activityId 按原字符串收发
|
|
10
|
+
* ('001'/'1.20'/'1e3' 不改写、不预解析——数字与字符串形态互不折叠)。HTTP 与工具
|
|
11
|
+
* 走同一查询及出站契约校验(供给劣质对象在此统一拦截——不外泄、不静默丢行)。
|
|
11
12
|
*/
|
|
12
|
-
import
|
|
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';
|
|
13
|
+
import { activityPlayActivitySchema, activityPlayIdSchema, mallScopeOf, toMallQueryContext } from '@tbox.cn/app-contracts-mall';
|
|
14
|
+
import type { ActivityPlayActivity, ActivityPlayPage, ActivityPlayQueryContext } from '@tbox.cn/app-contracts-mall';
|
|
19
15
|
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
20
16
|
import type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
21
17
|
import { resolveActivityPlayQueryPort } from './ports';
|
|
@@ -29,8 +25,7 @@ export const ACTIVITY_PLAY_LIMIT_MAX = 50;
|
|
|
29
25
|
/**
|
|
30
26
|
* limit 归一(字符串化入参与 HTTP query 同规则):
|
|
31
27
|
* **缺席**(undefined——字段没传)→ 缺省 20;传了但为空串/纯空白/非数字/越界 →
|
|
32
|
-
* INVALID_REQUEST
|
|
33
|
-
* 评审返修:此前空白串被归为缺省,违背任务书空值拒绝语义)。
|
|
28
|
+
* INVALID_REQUEST(明确拒绝——“未传”与“传了非法值”二分,空值不静默变成缺省数量)。
|
|
34
29
|
*/
|
|
35
30
|
export function normalizeListLimit(raw: string | undefined): number {
|
|
36
31
|
if (raw === undefined) return ACTIVITY_PLAY_LIMIT_DEFAULT;
|
|
@@ -50,13 +45,22 @@ export function normalizeKeyword(raw: string | undefined): string | undefined {
|
|
|
50
45
|
return trimmed.length > 0 ? trimmed : undefined;
|
|
51
46
|
}
|
|
52
47
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
48
|
+
/**
|
|
49
|
+
* activityId 归一(原字符串语义):契约 schema 校验(非空/≤128/拒控制字符与路径段破坏符);
|
|
50
|
+
* 不做数字转换/字符集白名单/**不做 trim**——供应商定义标识空间('001' 与 '1' ' record-001 '
|
|
51
|
+
* 均按原值收发;列表返回 ' record-001 ' 时详情必须原样命中,改写即 404 串答)。
|
|
52
|
+
*/
|
|
53
|
+
export function normalizeActivityId(raw: string): string {
|
|
54
|
+
const parsed = activityPlayIdParser(raw);
|
|
55
|
+
if (parsed === undefined) {
|
|
56
|
+
throw ActivityPlayHttpError.invalidRequest('activityId 参数非法(空/超长/控制字符/路径段破坏符)');
|
|
58
57
|
}
|
|
59
|
-
return parsed
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function activityPlayIdParser(raw: string): string | undefined {
|
|
62
|
+
const parsed = activityPlayIdSchema.safeParse(raw);
|
|
63
|
+
return parsed.success ? parsed.data : undefined;
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
/** 可信查询上下文组装(缺商圈显性失败——不默认其他商圈) */
|
|
@@ -68,7 +72,15 @@ function requireQueryContext(reqCtx: RequestContext): { q: ActivityPlayQueryCont
|
|
|
68
72
|
return { q: toMallQueryContext(reqCtx), mallId: scope.mallId };
|
|
69
73
|
}
|
|
70
74
|
|
|
71
|
-
/**
|
|
75
|
+
/** 供给活动对象出站契约校验(HTTP 与工具同门——劣质对象显性 502,不外泄) */
|
|
76
|
+
function assertSupplyActivity(activity: ActivityPlayActivity, expectedId: string): void {
|
|
77
|
+
const parsed = activityPlayActivitySchema.safeParse(activity);
|
|
78
|
+
if (!parsed.success || activity.activityId !== expectedId) {
|
|
79
|
+
throw ActivityPlayHttpError.providerError('活动供给数据非法');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 列表查询(供给侧承载上游查询/适配层 keyword 过滤与 limit 截断——本层零重复实现) */
|
|
72
84
|
export async function queryActivityPlayList(
|
|
73
85
|
reqCtx: RequestContext,
|
|
74
86
|
ctx: ServerContext,
|
|
@@ -76,18 +88,25 @@ export async function queryActivityPlayList(
|
|
|
76
88
|
): Promise<ActivityPlayPage> {
|
|
77
89
|
const { q } = requireQueryContext(reqCtx);
|
|
78
90
|
const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
|
|
79
|
-
|
|
91
|
+
const page = await port.listActivities(q, input);
|
|
92
|
+
for (const item of page.items) {
|
|
93
|
+
assertSupplyActivity(item, item.activityId);
|
|
94
|
+
}
|
|
95
|
+
return page;
|
|
80
96
|
}
|
|
81
97
|
|
|
82
|
-
/**
|
|
98
|
+
/** 详情查询(上游命中 → 活动对象(可携 unknown/disabled);不存在/不可见 → null) */
|
|
83
99
|
export async function queryActivityPlayDetail(
|
|
84
100
|
reqCtx: RequestContext,
|
|
85
101
|
ctx: ServerContext,
|
|
86
|
-
|
|
102
|
+
activityId: string,
|
|
87
103
|
): Promise<ActivityPlayActivity | null> {
|
|
88
104
|
const { q } = requireQueryContext(reqCtx);
|
|
89
105
|
const port = await resolveActivityPlayQueryPort(reqCtx, ctx);
|
|
90
|
-
|
|
106
|
+
const activity = await port.getActivity(q, activityId);
|
|
107
|
+
if (!activity) return null;
|
|
108
|
+
assertSupplyActivity(activity, activityId);
|
|
109
|
+
return activity;
|
|
91
110
|
}
|
|
92
111
|
|
|
93
112
|
/** 当前商圈标识(join key:出卡 mallId 恒取执行上下文 scope,非模型输入/硬编码) */
|
package/src/server/routes.ts
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 活动玩法 HTTP 接口(挂载 /api/m/activity-play,鉴权归平台 /api 中间件)。
|
|
3
3
|
* 仅承载 HTTP 出入口形态:query 单值提取 / 错误信封 / 404 映射;查询语义与校验
|
|
4
|
-
* 单源在 ./query.ts
|
|
4
|
+
* 单源在 ./query.ts(与工具共用——工具/HTTP 对同参数同错误分类)。
|
|
5
5
|
*
|
|
6
|
-
* GET /activities?mallId&keyword&limit
|
|
7
|
-
* GET /activities/:
|
|
6
|
+
* GET /activities?mallId&keyword&limit → {"data":{"items":[活动对象]}}
|
|
7
|
+
* GET /activities/:activityId?mallId → {"data":活动对象}
|
|
8
|
+
* GET /prizes?mallId → {"data":{"total":…,"items":[…]}}(本人奖品——任务07)
|
|
9
|
+
* GET /prizes/:prizeRecordRef?mallId → {"data":PrizeDetail}
|
|
8
10
|
*
|
|
9
|
-
*
|
|
11
|
+
* 语义钉死(远端活动改造 2026-11-17;奖品面 2026-11-18 任务07):
|
|
10
12
|
* - mallId 必填;请求商圈与已绑定上下文冲突 → 403 SCOPE_MISMATCH(可信上下文不可被
|
|
11
13
|
* 客户端静默覆盖);无任何商圈(上下文缺商圈且 query 无 mallId)→ 400 SCOPE_REQUIRED;
|
|
12
14
|
* - 用户身份恒取可信请求上下文(getRequestContext),不解析/不信任任何客户端
|
|
13
|
-
* userId/openid
|
|
15
|
+
* userId/openid/会员号类参数;活动接口不涉及玩家身份(本人奖品经 prizes/ 路由的
|
|
16
|
+
* 主体检查——缺用户 LOGIN_REQUIRED 401 且零奖品 API 调用);
|
|
14
17
|
* - limit 缺省 20、合法域 1—50(normalizeListLimit 单源——非法/越界 400,不静默钳制);
|
|
15
|
-
* -
|
|
16
|
-
*
|
|
18
|
+
* - activityId/prizeRecordRef 路径段按原字符串定位('001'/'1.20' 不改写;不从标题猜测);
|
|
19
|
+
* - 活动不存在/不可见 → 404 ACTIVITY_NOT_FOUND;奖品不存在/不可见 → 404 PRIZE_NOT_FOUND
|
|
20
|
+
* (同文案不区分,不泄露他人记录);供给未配置/实例缺席/禁用 → 503
|
|
21
|
+
* NOT_CONFIGURED;供给连接非法或上游失败 → 502 PROVIDER_ERROR(不回退演示数据、
|
|
22
|
+
* 不折损为空列表/空奖品结果);
|
|
23
|
+
* - 成功与业务错误响应均 Cache-Control: no-store(远端事实/个人数据不落缓存);
|
|
24
|
+
* - 入口准备(POST entry)已随远端活动改造退场——详情只服务展示,不承担
|
|
25
|
+
* 点击前检查;参与跳转由前端直接携带 mallId/activityId 经宿主桥处理。
|
|
26
|
+
* - 奖品两 GET 的实现与错误收口归 prizes/routes.ts(本域独立文件面——认证/_scope
|
|
27
|
+
* 错误不经活动面转换)。
|
|
17
28
|
*/
|
|
18
29
|
import { Router } from 'express';
|
|
19
30
|
import type { Request, Response } from 'express';
|
|
20
31
|
import { getRequestContext } from '@tbox.cn/app-agent-sdk-server';
|
|
21
32
|
import {
|
|
22
33
|
parseMallScope,
|
|
23
|
-
toMallQueryContext,
|
|
24
34
|
withMallScopeFromQuery,
|
|
25
|
-
mallScopeOf,
|
|
26
|
-
ACTIVITY_PLAY_QUERY_SERVICE,
|
|
27
|
-
ACTIVITY_PLAY_ENTRY_SERVICE,
|
|
28
|
-
activityPlayActivitySchema,
|
|
29
|
-
activityPlayEntrySchema,
|
|
30
|
-
ACTIVITY_PLAY_ENTRY_PROTOCOL_VERSION,
|
|
31
|
-
} from '@tbox.cn/app-contracts-mall';
|
|
32
|
-
import type {
|
|
33
|
-
ActivityPlayActivity,
|
|
34
|
-
ActivityPlayEntryRecord,
|
|
35
|
-
ActivityPlaySupplyBinding,
|
|
36
35
|
} from '@tbox.cn/app-contracts-mall';
|
|
37
36
|
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
38
37
|
import type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
39
38
|
import {
|
|
40
|
-
|
|
39
|
+
normalizeActivityId,
|
|
41
40
|
normalizeKeyword,
|
|
42
41
|
normalizeListLimit,
|
|
43
42
|
queryActivityPlayList,
|
|
43
|
+
queryActivityPlayDetail,
|
|
44
44
|
} from './query';
|
|
45
|
-
import { resolveActivityPlayEntryPort, resolveActivityPlayQueryPort } from './ports';
|
|
46
45
|
import { ActivityPlayHttpError, toActivityPlayHttpError } from './errors';
|
|
46
|
+
import { createActivityPlayPrizeRouter } from './prizes/routes';
|
|
47
47
|
|
|
48
48
|
/** mallId 形状守卫(与 provider adapter 同则,防注入类字符) */
|
|
49
49
|
const MALL_ID_RE = /^[\w-]{1,32}$/;
|
|
@@ -102,30 +102,31 @@ function resolveScopedRequestContext(req: Request): RequestContext {
|
|
|
102
102
|
throw ActivityPlayHttpError.scopeRequired('缺少商圈(mallId 必填或上下文未绑定商圈)');
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
/** 统一错误收口:映射守门 + logger 留痕(原始供给细节不透传响应) */
|
|
105
|
+
/** 统一错误收口:映射守门 + no-store + logger 留痕(原始供给细节不透传响应) */
|
|
106
106
|
function sendError(res: Response, err: unknown, opts?: {
|
|
107
107
|
logger?: { warn?: (msg: string, ...meta: unknown[]) => void };
|
|
108
|
-
fields?: {
|
|
108
|
+
fields?: { activityId?: string; mallId?: string };
|
|
109
109
|
}): void {
|
|
110
110
|
const httpError = toActivityPlayHttpError(err);
|
|
111
111
|
if (httpError.httpStatus >= 500) {
|
|
112
112
|
// 日志安全纪律(评审返修):只记安全 code 与定位字段——不拼供给 err.message
|
|
113
|
-
|
|
113
|
+
//(实测其可能携带连接/参数细节;HTTP 响应零细节早有断言)
|
|
114
114
|
const where = opts?.fields;
|
|
115
115
|
opts?.logger?.warn?.(
|
|
116
116
|
`[activity-play] 供给异常 code=${httpError.code}`
|
|
117
|
-
+ (where?.
|
|
117
|
+
+ (where?.activityId ? ` id=${where.activityId}` : '')
|
|
118
118
|
+ (where?.mallId ? ` mallId=${where.mallId}` : ''),
|
|
119
119
|
);
|
|
120
120
|
}
|
|
121
|
+
// 业务错误响应恒 no-store(远端活动改造——错误体不含可缓存事实)
|
|
122
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
121
123
|
res.status(httpError.httpStatus).json({ error: { code: httpError.code, message: httpError.message } });
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return /^[\w:.-]{1,128}$/.test(raw) ? raw : undefined;
|
|
126
|
+
/** 日志取值护甲:activityId/mallId 仅在过形状守卫后进日志(拒绝注入面写日志) */
|
|
127
|
+
function rawActivityIdOf(req: Request): string | undefined {
|
|
128
|
+
const raw = typeof req.params['activityId'] === 'string' ? req.params['activityId'] : '';
|
|
129
|
+
return raw.length <= 128 && !/[/?#%]/.test(raw) ? raw : undefined;
|
|
129
130
|
}
|
|
130
131
|
|
|
131
132
|
function safeMallIdOf(req: Request): string | undefined {
|
|
@@ -133,115 +134,6 @@ function safeMallIdOf(req: Request): string | undefined {
|
|
|
133
134
|
return /^[\w-]{1,32}$/.test(raw) ? raw : undefined;
|
|
134
135
|
}
|
|
135
136
|
|
|
136
|
-
/**
|
|
137
|
-
* 两槽一致性内容级比对(task-03):query 活动对象 vs entry 供给侧活动快照——全字段
|
|
138
|
-
*(含 status/loginRequired:不能用 A 目录的状态/登录要求放行 B 目录的入口)。
|
|
139
|
-
* 状态在两次读取的毫秒临界差会表现为 ENTRY_UNAVAILABLE(fail-closed——重试即恢复)。
|
|
140
|
-
*/
|
|
141
|
-
export function activityMatchesForEntry(a: ActivityPlayActivity, b: ActivityPlayActivity): boolean {
|
|
142
|
-
return a.activityRef === b.activityRef
|
|
143
|
-
&& a.title === b.title
|
|
144
|
-
&& a.status === b.status
|
|
145
|
-
&& a.loginRequired === b.loginRequired
|
|
146
|
-
&& (a.description ?? undefined) === (b.description ?? undefined)
|
|
147
|
-
&& (a.imageUrl ?? undefined) === (b.imageUrl ?? undefined)
|
|
148
|
-
&& a.startsAt === b.startsAt
|
|
149
|
-
&& a.endsAt === b.endsAt;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 供给侧活动对象契约校验(评审返修:非法活动数据不得通过状态/登录检查):
|
|
154
|
-
* 过 activityPlayActivitySchema(status/loginRequired 形状——'paused'/缺 loginRequired
|
|
155
|
-
* 直接 502)+ 引用必须与请求一致(供给返回另一 activityRef 时不得以请求引用包装输出)。
|
|
156
|
-
* query 侧活动与 entry 快照两侧同规则。
|
|
157
|
-
*/
|
|
158
|
-
function assertSupplyActivity(activity: ActivityPlayActivity, expectedRef: string): void {
|
|
159
|
-
const parsed = activityPlayActivitySchema.safeParse(activity);
|
|
160
|
-
if (!parsed.success || activity.activityRef !== expectedRef) {
|
|
161
|
-
throw ActivityPlayHttpError.providerError('活动供给数据非法');
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 两槽供给一致性联检(评审返修两轮):
|
|
167
|
-
* 1) 自报签名比对:两侧 Port 的 supplyBinding(绑定双键 + 归一目录指纹)完全相等——
|
|
168
|
-
* 目录任何差异(含 entry 目标)即不等;签名缺席(供给未实现联检口)→ fail-closed。
|
|
169
|
-
* **仅自报不足**:同一工厂被复用绑到别槽时,实现按自身常量生成签名(自报口径可谎报
|
|
170
|
-
* 一致)——因此:
|
|
171
|
-
* 2) registry 实绑点查:用 SDK 既有 `hasProviderBinding` 在当前 scope 对两槽分别核对
|
|
172
|
-
* 「实际有效绑定 === 该侧自报签名」;任一不符或点查能力缺席(旧 SDK)→ 不可证一致
|
|
173
|
-
* → fail-closed ENTRY_UNAVAILABLE。
|
|
174
|
-
*/
|
|
175
|
-
async function assertSupplyConsistency(
|
|
176
|
-
ctx: ServerContext,
|
|
177
|
-
queryPort: { supplyBinding?: ActivityPlaySupplyBinding },
|
|
178
|
-
entryPort: { supplyBinding?: ActivityPlaySupplyBinding },
|
|
179
|
-
scope: Record<string, string>,
|
|
180
|
-
): Promise<void> {
|
|
181
|
-
const qb = queryPort.supplyBinding;
|
|
182
|
-
const eb = entryPort.supplyBinding;
|
|
183
|
-
if (!qb || !eb
|
|
184
|
-
|| qb.provider !== eb.provider
|
|
185
|
-
|| qb.implementation !== eb.implementation
|
|
186
|
-
|| qb.directoryHash !== eb.directoryHash) {
|
|
187
|
-
throw ActivityPlayHttpError.entryUnavailable('入口供给与活动目录供给不一致');
|
|
188
|
-
}
|
|
189
|
-
const integrations = ctx.integrations as {
|
|
190
|
-
hasProviderBinding?: (
|
|
191
|
-
service: string,
|
|
192
|
-
scope: Record<string, string>,
|
|
193
|
-
provider: string,
|
|
194
|
-
implementation: string,
|
|
195
|
-
) => boolean;
|
|
196
|
-
} | undefined;
|
|
197
|
-
const check = integrations?.hasProviderBinding?.bind(integrations);
|
|
198
|
-
const queryBound = check?.(ACTIVITY_PLAY_QUERY_SERVICE, scope, qb.provider, qb.implementation);
|
|
199
|
-
const entryBound = check?.(ACTIVITY_PLAY_ENTRY_SERVICE, scope, eb.provider, eb.implementation);
|
|
200
|
-
if (queryBound !== true || entryBound !== true) {
|
|
201
|
-
throw ActivityPlayHttpError.entryUnavailable('入口供给与活动目录供给不一致');
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/** 入口异常安全日志(评审返修:不拼供给 err.message——只记安全 code 与定位字段) */
|
|
206
|
-
function logEntryIssue(
|
|
207
|
-
logger: { warn?: (msg: string, ...meta: unknown[]) => void } | undefined,
|
|
208
|
-
fields: { code: string; activityRef?: string; mallId?: string },
|
|
209
|
-
): void {
|
|
210
|
-
logger?.warn?.(
|
|
211
|
-
`[activity-play] entry 异常 code=${fields.code}`
|
|
212
|
-
+ (fields.activityRef ? ` ref=${fields.activityRef}` : '')
|
|
213
|
-
+ (fields.mallId ? ` mallId=${fields.mallId}` : ''),
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* 入口准备业务体校验:空对象或省略合法;平台保留 body.context(上层中间件消费转
|
|
219
|
-
* 请求上下文——本模块忽略该键,不重复实现解析)之外的任何非空字段 INVALID_REQUEST
|
|
220
|
-
*(不容目标/身份/插件参数覆盖入口)。
|
|
221
|
-
*/
|
|
222
|
-
function validateEntryBody(req: Request): void {
|
|
223
|
-
const body: unknown = req.body;
|
|
224
|
-
if (body === undefined) return;
|
|
225
|
-
if (body === null || typeof body !== 'object' || Array.isArray(body)) {
|
|
226
|
-
throw ActivityPlayHttpError.invalidRequest('入口请求业务体须为空对象或省略');
|
|
227
|
-
}
|
|
228
|
-
for (const [key, value] of Object.entries(body as Record<string, unknown>)) {
|
|
229
|
-
if (key === 'context') continue; // 平台保留键(上层鉴权中间件摄入——接口文档说明)
|
|
230
|
-
if (value !== undefined) {
|
|
231
|
-
throw ActivityPlayHttpError.invalidRequest('入口请求业务体须为空对象或省略(不接受目标/身份/参数覆盖)');
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** 步骤 4:本系统登录要求检查(身份唯一直读可信 RequestContext——guest/local/external 均有效) */
|
|
237
|
-
function requireEntryLogin(reqCtx: RequestContext, activity: ActivityPlayActivity): void {
|
|
238
|
-
if (!activity.loginRequired) return;
|
|
239
|
-
const identity = reqCtx.identity;
|
|
240
|
-
if (!identity || identity.source === 'anonymous') {
|
|
241
|
-
throw new ActivityPlayHttpError('LOGIN_REQUIRED', 401, '该活动要求先登录');
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
137
|
export function createActivityPlayRouter(ctx: ServerContext): Router {
|
|
246
138
|
const router = Router();
|
|
247
139
|
|
|
@@ -255,117 +147,39 @@ export function createActivityPlayRouter(ctx: ServerContext): Router {
|
|
|
255
147
|
...(keyword ? { keyword } : {}),
|
|
256
148
|
limit,
|
|
257
149
|
});
|
|
150
|
+
// 远端活动事实不落缓存(成功响应恒 no-store)
|
|
151
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
258
152
|
res.json({ data: { items: page.items } });
|
|
259
153
|
} catch (err) {
|
|
260
154
|
sendError(res, err, {
|
|
261
155
|
logger: ctx.logger,
|
|
262
|
-
fields: {
|
|
156
|
+
fields: { activityId: rawActivityIdOf(req), mallId: safeMallIdOf(req) },
|
|
263
157
|
});
|
|
264
158
|
}
|
|
265
159
|
});
|
|
266
160
|
|
|
267
|
-
//
|
|
268
|
-
|
|
269
|
-
// → 本系统登录要求(401)→ entry 供给与两槽一致性(绑定签名/供给对象校验/内容比对
|
|
270
|
-
// 三道联检)→ 出站 schema 自证。成功与业务错误均 no-store;异常日志只记安全 code 与
|
|
271
|
-
// 定位字段。
|
|
272
|
-
router.post('/activities/:activityRef/entry', async (req: Request, res: Response) => {
|
|
161
|
+
// GET /api/m/activity-play/activities/:activityId?mallId=…(详情只服务展示——不承担参与前检查)
|
|
162
|
+
router.get('/activities/:activityId', async (req: Request, res: Response) => {
|
|
273
163
|
try {
|
|
274
|
-
const
|
|
275
|
-
const
|
|
276
|
-
validateEntryBody(req);
|
|
164
|
+
const activityIdRaw = typeof req.params['activityId'] === 'string' ? req.params['activityId'] : '';
|
|
165
|
+
const activityId = normalizeActivityId(activityIdRaw);
|
|
277
166
|
const scopedReqCtx = resolveScopedRequestContext(req);
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
// 2. 重新读当前活动(不读卡片旧状态/客户端身份)——供给对象先过公开 schema 且
|
|
281
|
-
// 引用一致(评审返修:'paused'/缺 loginRequired 等非法供给数据不得通过状态与
|
|
282
|
-
// 登录检查——直接 502;供给返回另一 activityRef 也不得以请求引用包装输出)
|
|
283
|
-
const queryPort = await resolveActivityPlayQueryPort(scopedReqCtx, ctx);
|
|
284
|
-
const activity = await queryPort.getActivity(q, activityRef);
|
|
167
|
+
const activity = await queryActivityPlayDetail(scopedReqCtx, ctx, activityId);
|
|
285
168
|
if (!activity) {
|
|
286
|
-
throw ActivityPlayHttpError.notFound(`活动不存在: ${
|
|
287
|
-
}
|
|
288
|
-
assertSupplyActivity(activity, activityRef);
|
|
289
|
-
|
|
290
|
-
// 3. 服务端时间状态门(disabled 优先——statusOf 单源结果位;'paused' 等非法枚举
|
|
291
|
-
// 已在步骤 2 拒 502——只有 active 前进)
|
|
292
|
-
if (activity.status === 'disabled') {
|
|
293
|
-
throw ActivityPlayHttpError.activityDisabled('活动已停用');
|
|
294
|
-
}
|
|
295
|
-
if (activity.status === 'not_started') {
|
|
296
|
-
throw ActivityPlayHttpError.activityNotStarted('活动尚未开始');
|
|
297
|
-
}
|
|
298
|
-
if (activity.status === 'ended') {
|
|
299
|
-
throw ActivityPlayHttpError.activityEnded('活动已结束');
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// 4. 本系统登录要求(loginRequired 只表示本系统登录——不判会员资格/插件登录)
|
|
303
|
-
requireEntryLogin(scopedReqCtx, activity);
|
|
304
|
-
|
|
305
|
-
// 5. entry 供给(槽未配置 503 / 异常 502 归 errors 单源)与两槽一致性三道联检:
|
|
306
|
-
// a) supplyBinding(绑定键 provider/implementation + 归一目录指纹)——评审批据
|
|
307
|
-
// 「活动 DTO 相同不足以证明供给一致」:不同目录 entry 目标(query 目录 aPlay/
|
|
308
|
-
// entry 目录 otherPlay)、不同 provider/implementation、签名缺席,均不可证
|
|
309
|
-
// 同源 → fail-closed 409 ENTRY_UNAVAILABLE;
|
|
310
|
-
// b) entry 快照活动对象过公开 schema 且引用一致(同 query 侧——502);
|
|
311
|
-
// c) 内容级全字段比对 activityMatchesForEntry(fail-closed 409)
|
|
312
|
-
const entryPort = await resolveActivityPlayEntryPort(scopedReqCtx, ctx);
|
|
313
|
-
await assertSupplyConsistency(ctx, queryPort, entryPort, mallScopeOf(scopedReqCtx));
|
|
314
|
-
const entryRecord: ActivityPlayEntryRecord | null = await entryPort.getEntry(q, activityRef);
|
|
315
|
-
if (!entryRecord) {
|
|
316
|
-
throw ActivityPlayHttpError.entryUnavailable('该活动暂未登记参与入口');
|
|
317
|
-
}
|
|
318
|
-
assertSupplyActivity(entryRecord.activity, activityRef);
|
|
319
|
-
if (!activityMatchesForEntry(activity, entryRecord.activity)) {
|
|
320
|
-
throw ActivityPlayHttpError.entryUnavailable('入口供给与活动目录不一致');
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// 6. 出站自证(供给侧非法结构不得未经校验输出 → PROVIDER_ERROR)
|
|
324
|
-
const payload = {
|
|
325
|
-
protocolVersion: ACTIVITY_PLAY_ENTRY_PROTOCOL_VERSION,
|
|
326
|
-
activityRef,
|
|
327
|
-
target: entryRecord.target,
|
|
328
|
-
};
|
|
329
|
-
const parsed = activityPlayEntrySchema.safeParse(payload);
|
|
330
|
-
if (!parsed.success) {
|
|
331
|
-
throw ActivityPlayHttpError.providerError('入口供给数据非法');
|
|
169
|
+
throw ActivityPlayHttpError.notFound(`活动不存在: ${activityId}`);
|
|
332
170
|
}
|
|
333
171
|
res.setHeader('Cache-Control', 'no-store');
|
|
334
|
-
res.json({ data: parsed.data });
|
|
335
|
-
} catch (err) {
|
|
336
|
-
// 入口业务错误同样 no-store(上层鉴权错误保持平台行为)
|
|
337
|
-
const httpError = toActivityPlayHttpError(err);
|
|
338
|
-
if (httpError.httpStatus >= 500) {
|
|
339
|
-
logEntryIssue(ctx.logger, {
|
|
340
|
-
code: httpError.code,
|
|
341
|
-
activityRef: rawActivityRefOf(req),
|
|
342
|
-
mallId: safeMallIdOf(req),
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
res.setHeader('Cache-Control', 'no-store');
|
|
346
|
-
res.status(httpError.httpStatus).json({ error: { code: httpError.code, message: httpError.message } });
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
// GET /api/m/activity-play/activities/:activityRef?mallId=…
|
|
351
|
-
router.get('/activities/:activityRef', async (req: Request, res: Response) => {
|
|
352
|
-
try {
|
|
353
|
-
const activityRefRaw = typeof req.params['activityRef'] === 'string' ? req.params['activityRef'] : '';
|
|
354
|
-
const activityRef = normalizeActivityRef(activityRefRaw);
|
|
355
|
-
const scopedReqCtx = resolveScopedRequestContext(req);
|
|
356
|
-
const queryPort = await resolveActivityPlayQueryPort(scopedReqCtx, ctx);
|
|
357
|
-
const activity = await queryPort.getActivity(toMallQueryContext(scopedReqCtx), activityRef);
|
|
358
|
-
if (!activity) {
|
|
359
|
-
throw ActivityPlayHttpError.notFound(`活动不存在: ${activityRef}`);
|
|
360
|
-
}
|
|
361
172
|
res.json({ data: activity });
|
|
362
173
|
} catch (err) {
|
|
363
174
|
sendError(res, err, {
|
|
364
175
|
logger: ctx.logger,
|
|
365
|
-
fields: {
|
|
176
|
+
fields: { activityId: rawActivityIdOf(req), mallId: safeMallIdOf(req) },
|
|
366
177
|
});
|
|
367
178
|
}
|
|
368
179
|
});
|
|
369
180
|
|
|
181
|
+
// 本人奖品两 GET(任务07——prizes 子路由:主体检查先于供给调用,错误收口独立)
|
|
182
|
+
router.use(createActivityPlayPrizeRouter(ctx));
|
|
183
|
+
|
|
370
184
|
return router;
|
|
371
185
|
}
|
package/src/server/tools.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 活动玩法只读工具集(task-02
|
|
2
|
+
* 活动玩法只读工具集(task-02 服务端出卡;远端活动改造 2026-11-17)。
|
|
3
3
|
*
|
|
4
4
|
* 上下文统一化:运行上下文 = 工具 execute 第二参(requireToolRequestContext 显式
|
|
5
|
-
* 读取——认证错误沿平台 fail-visible
|
|
5
|
+
* 读取——认证错误沿平台 fail-visible 处理,不吞);商圈恒取该可信上下文
|
|
6
6
|
* (mallId 不在 inputSchema——不声明任何身份/scope/入口覆盖字段)。
|
|
7
7
|
* 查询语义单源 = ./query.ts(与 HTTP 同参同错误分类);本层只做字符串化入参归一、
|
|
8
|
-
* 出卡编排与失败事实组装。无模块级请求态缓存(每次 execute
|
|
8
|
+
* 出卡编排与失败事实组装。无模块级请求态缓存(每次 execute 用各自上下文解析供给——
|
|
9
|
+
* 每次查询取得外部事实)。
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
+
* 出卡语义(对齐 cards.md 出卡管线):
|
|
11
12
|
* - 非空结果 → 顶层答话事实 + cards 候选(capture 校验/入池/剥离归平台单点);
|
|
12
|
-
* -
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* - 上游合法空列表/关键词无匹配 → 明确空结果事实,不产 cards 键(不出卡语义,非错误);
|
|
14
|
+
* 供应商失败(NOT_CONFIGURED/PROVIDER_ERROR)→ 失败事实——不得被模型表述为「没有活动」;
|
|
15
|
+
* - 标识不存在 → ACTIVITY_NOT_FOUND 失败事实;
|
|
15
16
|
* - 工具省略 outputSchema(capture 改写出参——协议约定);经 withCardEmissionHint
|
|
16
17
|
* 预告知(注册与 preTool 声明共用 wrapped 实例);失败 message 面向调用方安全文案,
|
|
17
|
-
*
|
|
18
|
+
* 原始异常与连接内容不透出。
|
|
19
|
+
*
|
|
20
|
+
* 表述纪律(远端活动改造):活动事实是查询时状态,最终参与由插件判断;unknown 状态保守展示
|
|
21
|
+
* (不推断「进行中」)——statusText/statusTone 与占位渲染器同口径。
|
|
18
22
|
*/
|
|
19
23
|
import { z } from 'zod';
|
|
20
24
|
import {
|
|
@@ -27,7 +31,7 @@ import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
|
27
31
|
import type { ActivityPlayActivity } from '@tbox.cn/app-contracts-mall';
|
|
28
32
|
import { activityPlayCardDataSchema } from '@tbox.cn/app-contracts-mall';
|
|
29
33
|
import {
|
|
30
|
-
|
|
34
|
+
normalizeActivityId,
|
|
31
35
|
normalizeKeyword,
|
|
32
36
|
normalizeListLimit,
|
|
33
37
|
queryActivityPlayDetail,
|
|
@@ -39,11 +43,10 @@ import { toActivityPlayHttpError } from './errors';
|
|
|
39
43
|
export const ACTIVITY_PLAY_CARD_TYPE = 'activity-play';
|
|
40
44
|
|
|
41
45
|
/**
|
|
42
|
-
* 入参不做 JSON
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* (与 HTTP 同参同果)。
|
|
46
|
+
* 入参不做 JSON 预解析(沿旧例):本域纯字符串入参契约(activityId/keyword/limit);
|
|
47
|
+
* 通用 JSON 转换会把 '1.20'→1.2、'1e3'→1000 折叠成 number——活动标识恰好撞上另一
|
|
48
|
+
* 供给记录造成串答。引擎级 harden(sanitizeLlmToolInput:null 系键清洗)在装配期
|
|
49
|
+
* 统一承担,模块侧**保留原始字符串**直交归一层单源校验(与 HTTP 同参同果)。
|
|
47
50
|
*/
|
|
48
51
|
|
|
49
52
|
/** 工具失败事实(可辨识错误码 + 安全文案;与 HTTP 信封同 code 面——errors.ts 单源分类) */
|
|
@@ -56,25 +59,26 @@ function toFailureFact(err: unknown): {
|
|
|
56
59
|
return { found: false, errorCode: httpError.code, message: httpError.message };
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
/** 状态中文口径(答话事实与卡片 import
|
|
62
|
+
/** 状态中文口径(答话事实与卡片 import 一致——同一活动对象派生;unknown 保守) */
|
|
60
63
|
function statusTextOf(status: ActivityPlayActivity['status']): string {
|
|
61
64
|
switch (status) {
|
|
62
65
|
case 'not_started': return '未开始';
|
|
63
66
|
case 'active': return '进行中';
|
|
64
67
|
case 'ended': return '已结束';
|
|
65
68
|
case 'disabled': return '已停用';
|
|
69
|
+
case 'unknown': return '状态未知';
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
/** 答话事实条目(capture 剥离 data 后模型答话的完整依据——不及顶层即不完整) */
|
|
70
74
|
function activityFactOf(activity: ActivityPlayActivity) {
|
|
71
75
|
return {
|
|
72
|
-
|
|
76
|
+
activityId: activity.activityId,
|
|
73
77
|
title: activity.title,
|
|
74
78
|
status: activity.status,
|
|
75
79
|
statusText: statusTextOf(activity.status),
|
|
76
|
-
loginRequired: activity.loginRequired,
|
|
77
80
|
...(activity.description ? { description: activity.description } : {}),
|
|
81
|
+
...(activity.imageUrl ? { imageUrl: activity.imageUrl } : {}),
|
|
78
82
|
...(activity.startsAt ? { startsAt: activity.startsAt } : {}),
|
|
79
83
|
...(activity.endsAt ? { endsAt: activity.endsAt } : {}),
|
|
80
84
|
};
|
|
@@ -91,7 +95,7 @@ export function createActivityPlayTools(opts: { getCtx: () => ServerContext }) {
|
|
|
91
95
|
const listActivityPlays = createTool({
|
|
92
96
|
id: 'listActivityPlays',
|
|
93
97
|
description:
|
|
94
|
-
'
|
|
98
|
+
'查询当前商场在售的活动玩法列表(大转盘、抽奖、签到、互动游戏等——来自外部供应商实时数据),可用 keyword 按名称或说明过滤、limit 控制返回数量(缺省 20);只读查询,不执行参与或抽奖;查询结果为当下的活动事实与状态,不代表参与资格。',
|
|
95
99
|
inputSchema: z.object({
|
|
96
100
|
keyword: z.string().optional(),
|
|
97
101
|
limit: z.string().optional(),
|
|
@@ -107,14 +111,14 @@ export function createActivityPlayTools(opts: { getCtx: () => ServerContext }) {
|
|
|
107
111
|
limit,
|
|
108
112
|
});
|
|
109
113
|
if (page.items.length === 0) {
|
|
110
|
-
//
|
|
114
|
+
// 空结果事实(当前商家无活动或关键词无匹配):不是错误,不产卡片候选
|
|
111
115
|
return {
|
|
112
116
|
found: false as const,
|
|
113
117
|
count: 0,
|
|
114
118
|
...(keyword ? { keyword } : {}),
|
|
115
119
|
noResultsReason: keyword
|
|
116
120
|
? `当前商场没有匹配「${keyword}」的活动玩法`
|
|
117
|
-
: '
|
|
121
|
+
: '当前商场暂无活动玩法',
|
|
118
122
|
};
|
|
119
123
|
}
|
|
120
124
|
const mallId = requireMallIdOf(reqCtx);
|
|
@@ -127,9 +131,8 @@ export function createActivityPlayTools(opts: { getCtx: () => ServerContext }) {
|
|
|
127
131
|
count: page.items.length,
|
|
128
132
|
truncated: page.items.length === limit, // 列表被 limit 截断不等同没有更多活动——提示模型勿冒称总数
|
|
129
133
|
items: page.items.map(activityFactOf),
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
notice: '活动玩法为本地登记目录;参与须在活动页面完成。loginRequired 仅说明本系统是否要求先登录,活动方实际要求(登录/资格)以活动页面为准;查询结果不代表参与资格。',
|
|
134
|
+
// 状态是查询时事实(供应商侧可能变化),最终能否参与由活动插件判断
|
|
135
|
+
notice: '以上为当前查询到的外部活动事实;活动状态可能随时变化,最终参与(开放时间/次数/资格)以活动页面为准。查看某一活动的更多信息可用 getActivityPlayDetail。',
|
|
133
136
|
};
|
|
134
137
|
return { ...facts, cards: [{ cardType: ACTIVITY_PLAY_CARD_TYPE, data, note: cardNoteOf(page.items) }] };
|
|
135
138
|
} catch (err) {
|
|
@@ -141,21 +144,23 @@ export function createActivityPlayTools(opts: { getCtx: () => ServerContext }) {
|
|
|
141
144
|
const getActivityPlayDetail = createTool({
|
|
142
145
|
id: 'getActivityPlayDetail',
|
|
143
146
|
description:
|
|
144
|
-
'按
|
|
147
|
+
'按 activityId 查询当前商场指定活动玩法的详情(名称、说明、图片、起止时间与当前状态——来自外部供应商实时数据);用户只说名称时先用 listActivityPlays 拿到 activityId 再查详情。只读查询,不含参与入口,参与跳转由前端卡片处理;查询结果不代表参与资格。',
|
|
145
148
|
inputSchema: z.object({
|
|
146
|
-
|
|
149
|
+
activityId: z.string(),
|
|
147
150
|
}),
|
|
148
151
|
execute: async (args: Record<string, unknown>, execCtx: ToolExecContextLike) => {
|
|
149
152
|
const reqCtx: RequestContext = requireToolRequestContext(execCtx, 'getActivityPlayDetail');
|
|
150
153
|
const ctx = opts.getCtx();
|
|
151
154
|
try {
|
|
152
|
-
|
|
153
|
-
const
|
|
155
|
+
// 原字符串收参('001'/'1.20'/'1e3' 不折叠)——与 HTTP 详情路由同参同果
|
|
156
|
+
const activityIdRaw = typeof args.activityId === 'string' ? args.activityId : '';
|
|
157
|
+
const activityId = normalizeActivityId(activityIdRaw);
|
|
158
|
+
const activity = await queryActivityPlayDetail(reqCtx, ctx, activityId);
|
|
154
159
|
if (!activity) {
|
|
155
160
|
return {
|
|
156
161
|
found: false as const,
|
|
157
162
|
errorCode: 'ACTIVITY_NOT_FOUND',
|
|
158
|
-
message: `未找到活动玩法: ${
|
|
163
|
+
message: `未找到活动玩法: ${activityId}(当前商家范围内不存在或已不可见)`,
|
|
159
164
|
};
|
|
160
165
|
}
|
|
161
166
|
const mallId = requireMallIdOf(reqCtx);
|
|
@@ -166,9 +171,9 @@ export function createActivityPlayTools(opts: { getCtx: () => ServerContext }) {
|
|
|
166
171
|
mallId,
|
|
167
172
|
mode: 'detail' as const,
|
|
168
173
|
...activityFactOf(activity),
|
|
169
|
-
notice: activity.
|
|
170
|
-
? '
|
|
171
|
-
: '
|
|
174
|
+
notice: activity.status === 'unknown'
|
|
175
|
+
? '当前活动状态未知——请勿按进行中表述或引导参与。活动事实为查询时快照,最终参与以活动页面为准。'
|
|
176
|
+
: '活动事实为查询时快照,状态可能随时变化;最终参与(开放时间/次数/资格)以活动页面为准。',
|
|
172
177
|
};
|
|
173
178
|
return {
|
|
174
179
|
...facts,
|