@tbox.cn/app-module-mall-core 0.1.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/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/chunk-FBL6MHK5.js +267 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +22 -0
- package/dist/server/index.d.ts +103 -0
- package/dist/server/index.js +24 -0
- package/package.json +54 -0
- package/src/index.ts +6 -0
- package/src/server/assembly.ts +159 -0
- package/src/server/index.ts +69 -0
- package/src/server/mall-info-cache.ts +129 -0
- package/src/server/mall-info-tool.ts +54 -0
- package/src/server/pre-tools.ts +28 -0
- package/tbox.module.json +34 -0
- package/tests/assembly.test.ts +242 -0
- package/tests/cache-service.test.ts +20 -0
- package/tests/mall-info-cache.test.ts +99 -0
- package/tests/mall-info-tool.test.ts +157 -0
- package/tests/mall-name.test.ts +71 -0
- package/tsconfig.json +7 -0
- package/tsup.config.ts +18 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mall 域装配(单一模板统一化 W3-1):register 期注册于 `ctx.domainAssemblies`
|
|
3
|
+
* (SCOPE_ASSEMBLY_KIND),全部消费点运行期惰性查表——模板 app.ts 域语义清零。
|
|
4
|
+
*
|
|
5
|
+
* 自 mall 模板 app.ts 装配段 + config/deployment.ts 迁入(语义逐字保留):
|
|
6
|
+
* - 输入 = `ctx.integrations.raw`(原始装载产物;投影计算全走 contracts-mall 纯函数);
|
|
7
|
+
* - 成员 = 四消费点(validateContext / scopeSessions / scopeDefaults / instanceIdOf)
|
|
8
|
+
* + WS 双钩(defaultMallContext / enhanceHelloContext)+ mall-directory 服务数据源。
|
|
9
|
+
*
|
|
10
|
+
* 隐式默认单实例(provider 声明融合):provider 包 register 期经
|
|
11
|
+
* ctx.integrations.declareProviderInstances 声明部署实例;本装配 project() 访问期惰性融合
|
|
12
|
+
* (register 顺序无关——mall-core 先注册仍生效;显式拓扑恒赢,融合规则见 contracts-mall)。
|
|
13
|
+
*
|
|
14
|
+
* instanceIdOf 注入 mall 语义(SDK 零领域铁律——mallId 派生归本模块)。
|
|
15
|
+
*/
|
|
16
|
+
import { createLogger, type ServerContext } from '@tbox.cn/app-sdk/server';
|
|
17
|
+
import type { IntegrationsRaw } from '@tbox.cn/app-sdk/server';
|
|
18
|
+
import { SCOPE_ASSEMBLY_KIND } from '@tbox.cn/app-contracts';
|
|
19
|
+
import type { ProviderInstanceDeclarations } from '@tbox.cn/app-contracts';
|
|
20
|
+
import {
|
|
21
|
+
createMallRuntimeAssembly,
|
|
22
|
+
withMallName,
|
|
23
|
+
type MallDeploymentControlPlane,
|
|
24
|
+
type MallRuntimeAssembly,
|
|
25
|
+
type MallScopeAssembly,
|
|
26
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
27
|
+
import { mallControlPlaneOf } from '@tbox.cn/app-contracts-mall/server';
|
|
28
|
+
import { cachedMallName, resolveMallInfo } from './mall-info-cache.js';
|
|
29
|
+
|
|
30
|
+
const deploymentLogger = createLogger('DEPLOYMENT');
|
|
31
|
+
|
|
32
|
+
/** D1 enhanceContext 预热上界(2s 有界;后台继续补缓存) */
|
|
33
|
+
const WARM_TIMEOUT_MS = 2000;
|
|
34
|
+
|
|
35
|
+
/** 无声明兜底常量(冻结共享——stub registry 路径 ref 稳定,投影缓存同样命中) */
|
|
36
|
+
const NO_DECLARATIONS: ProviderInstanceDeclarations = Object.freeze({});
|
|
37
|
+
|
|
38
|
+
/** project() 投影 bundle(一次成束——withMallName 包装随束缓存) */
|
|
39
|
+
interface ProjectedAssembly {
|
|
40
|
+
readonly controlPlane: MallDeploymentControlPlane;
|
|
41
|
+
readonly runtime: MallRuntimeAssembly;
|
|
42
|
+
readonly buildScopeWithMallName: (attributes: Record<string, unknown>) => Record<string, string>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* mall 域装配形状 = TIER1 契约 MallScopeAssembly(W3-3 下沉 contracts-mall:
|
|
47
|
+
* module-mall-home 等消费方按契约类型查表——域模块间零包依赖)。
|
|
48
|
+
*/
|
|
49
|
+
export type { MallScopeAssembly } from '@tbox.cn/app-contracts-mall';
|
|
50
|
+
|
|
51
|
+
/** 域装配 kind 便捷解析(消费点统一入口——类型锚 = MallScopeAssembly)。 */
|
|
52
|
+
export function resolveMallScopeAssembly(ctx: ServerContext): MallScopeAssembly | undefined {
|
|
53
|
+
return ctx.domainAssemblies.resolve<MallScopeAssembly>(SCOPE_ASSEMBLY_KIND);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 从 integrations 原始装载产物构造域装配(register 期调用;零 IO——投影为纯函数计算)。
|
|
58
|
+
* raw 缺席(无 config/integrations.json)→ undefined(装配点按"无控制面"降级,语义对齐旧模板)。
|
|
59
|
+
*
|
|
60
|
+
* 惰性投影契约:controlPlane/scopeDefaults/defaultMallContext/buildScopeWithMallName
|
|
61
|
+
* 访问期经 project() 求值(声明 ref 缓存——registry COW 快照引用变化才重投影;
|
|
62
|
+
* register 顺序无关——provider 声明晚于本装配注册仍生效)。
|
|
63
|
+
* 函数成员 register 期定型(稳定标识——消费方捕获引用恒安全,调用时才读投影)。
|
|
64
|
+
*/
|
|
65
|
+
export function createScopeAssembly(
|
|
66
|
+
raw: IntegrationsRaw | undefined,
|
|
67
|
+
ctx: ServerContext,
|
|
68
|
+
): MallScopeAssembly | undefined {
|
|
69
|
+
if (!raw) return undefined;
|
|
70
|
+
|
|
71
|
+
const rootMiniAppId =
|
|
72
|
+
typeof raw.config.config?.miniAppId === 'string' && raw.config.config.miniAppId
|
|
73
|
+
? raw.config.config.miniAppId
|
|
74
|
+
: undefined;
|
|
75
|
+
|
|
76
|
+
let projectionRef: ProviderInstanceDeclarations | undefined;
|
|
77
|
+
let projection: ProjectedAssembly | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* 惰性投影:读声明快照(方法级 ?.——存量测试 stub registry 无此方法,对象级链会
|
|
80
|
+
* TypeError 全数崩栈)→ 声明 ref 未变复用缓存(声明只发生在 register 期——实际恒一次
|
|
81
|
+
* 投影计算;「晚声明」经 ref 变化天然正确失效)。
|
|
82
|
+
*/
|
|
83
|
+
const project = (): ProjectedAssembly => {
|
|
84
|
+
const declared = ctx.integrations?.providerInstanceDeclarations?.() ?? NO_DECLARATIONS;
|
|
85
|
+
if (!projection || projectionRef !== declared) {
|
|
86
|
+
const controlPlane = mallControlPlaneOf(raw.config, declared);
|
|
87
|
+
const runtime = createMallRuntimeAssembly(controlPlane);
|
|
88
|
+
projection = {
|
|
89
|
+
controlPlane,
|
|
90
|
+
runtime,
|
|
91
|
+
buildScopeWithMallName: withMallName(runtime, controlPlane, cachedMallName),
|
|
92
|
+
};
|
|
93
|
+
projectionRef = declared;
|
|
94
|
+
}
|
|
95
|
+
return projection;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
// 数据成员 getter:访问期读投影(mall-home routes / mall-directory 闭包自动生效)
|
|
100
|
+
get controlPlane() {
|
|
101
|
+
return project().controlPlane;
|
|
102
|
+
},
|
|
103
|
+
rootMiniAppId,
|
|
104
|
+
// 函数成员稳定包装(register 期定型——消费方捕获引用恒安全;调用时才读投影)
|
|
105
|
+
extractScopeKey: (c) => project().runtime.extractScopeKey(c),
|
|
106
|
+
validateScope: (a) => project().runtime.validateScope(a),
|
|
107
|
+
extractMallScope: (c) => project().runtime.extractMallScope(c),
|
|
108
|
+
buildScope: (a) => project().runtime.buildScope(a),
|
|
109
|
+
buildScopeWithMallName: (a) => project().buildScopeWithMallName(a),
|
|
110
|
+
instanceIdOf: (scope) => scope['mallId'] ?? '',
|
|
111
|
+
get scopeDefaults() {
|
|
112
|
+
const defaultMallId = project().controlPlane.defaultMallId;
|
|
113
|
+
return defaultMallId ? { defaultMallId } : undefined;
|
|
114
|
+
},
|
|
115
|
+
defaultMallContext() {
|
|
116
|
+
return buildDefaultMallContext(project().controlPlane, rootMiniAppId);
|
|
117
|
+
},
|
|
118
|
+
async enhanceHelloContext(context) {
|
|
119
|
+
const mall = context['mall'] as { mallId?: unknown; miniAppId?: unknown } | undefined;
|
|
120
|
+
const mallId = typeof mall?.mallId === 'string' && mall.mallId ? mall.mallId : undefined;
|
|
121
|
+
if (!mallId) return context;
|
|
122
|
+
const nextMall = { ...mall };
|
|
123
|
+
if (!nextMall.miniAppId && rootMiniAppId) {
|
|
124
|
+
nextMall.miniAppId = rootMiniAppId;
|
|
125
|
+
deploymentLogger.warn(`[MALL] HELLO context 缺 miniAppId,按 root config 回填(mallId=${mallId})`);
|
|
126
|
+
}
|
|
127
|
+
// mallId 未命中 malls 参考表 → mall.info 预热(2s 有界;后台继续补缓存)
|
|
128
|
+
if (!project().controlPlane.malls.some((m) => m.mallId === mallId)) {
|
|
129
|
+
const warm = resolveMallInfo(mallId, ctx.integrations);
|
|
130
|
+
await Promise.race([warm, new Promise<void>((r) => setTimeout(r, WARM_TIMEOUT_MS))]).catch(
|
|
131
|
+
() => undefined,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return { ...context, mall: nextMall };
|
|
135
|
+
},
|
|
136
|
+
} satisfies MallScopeAssembly;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 016:默认商圈 HELLO context(无 URL 参数访问时的服务端兜底)。
|
|
141
|
+
* v4 mallId-only 注入(D8/D9):miniAppId 是尽力回填的展示值,非会话边界——
|
|
142
|
+
* 读源统一 root config.miniAppId 优先、回落参考表 attributes.miniAppId;双缺不再
|
|
143
|
+
* 阻断注入(此前 warn-refuse 会造成默认商圈静默失效),按 mallId-only 注入。
|
|
144
|
+
* 缺省 defaultMallId → undefined(不兜底)。
|
|
145
|
+
*/
|
|
146
|
+
export function buildDefaultMallContext(
|
|
147
|
+
controlPlane: MallDeploymentControlPlane,
|
|
148
|
+
rootMiniAppId?: string,
|
|
149
|
+
): Record<string, unknown> | undefined {
|
|
150
|
+
if (!controlPlane.defaultMallId) return undefined;
|
|
151
|
+
const mall = controlPlane.malls.find((m) => m.mallId === controlPlane.defaultMallId);
|
|
152
|
+
const miniAppId = rootMiniAppId ?? mall?.miniAppId;
|
|
153
|
+
if (!miniAppId) {
|
|
154
|
+
deploymentLogger.warn(
|
|
155
|
+
`defaultMallId=${controlPlane.defaultMallId} 无 miniAppId 真源(root config 与参考表双缺)——按 mallId-only 注入默认商圈`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return { mall: { mallId: controlPlane.defaultMallId, ...(miniAppId ? { miniAppId } : {}) } };
|
|
159
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* module-mall-core 服务端入口(单一模板统一化 W3-1):
|
|
3
|
+
* 域装配注册(ctx.domainAssemblies → SCOPE_ASSEMBLY_KIND)+ mall-info 工具/缓存/preTool +
|
|
4
|
+
* mall-directory 服务(mall 域能力无 UI——UI 归 module-mall-home)。
|
|
5
|
+
*
|
|
6
|
+
* register 只注册不解析(注册期纯净铁律):域装配投影为纯函数计算(ctx.integrations.raw 输入),
|
|
7
|
+
* 消费点(模板装配/WS/HTTP)运行期惰性查表。
|
|
8
|
+
*/
|
|
9
|
+
import type { ServerContext, ServerModule } from '@tbox.cn/app-sdk/server';
|
|
10
|
+
import { SCOPE_ASSEMBLY_KIND } from '@tbox.cn/app-contracts';
|
|
11
|
+
import { MALL_INFO_CACHE_SERVICE, type MallInfoCachePort } from '@tbox.cn/app-contracts-mall';
|
|
12
|
+
import { createScopeAssembly } from './assembly.js';
|
|
13
|
+
import { bindMallInfoContext, mallInfoTool } from './mall-info-tool.js';
|
|
14
|
+
import { cachedMallName, resolveMallInfo } from './mall-info-cache.js';
|
|
15
|
+
import { preTools } from './pre-tools.js';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
createScopeAssembly,
|
|
19
|
+
resolveMallScopeAssembly,
|
|
20
|
+
buildDefaultMallContext,
|
|
21
|
+
type MallScopeAssembly,
|
|
22
|
+
} from './assembly.js';
|
|
23
|
+
export { cachedMallName, resolveMallInfo } from './mall-info-cache.js';
|
|
24
|
+
export { bindMallInfoContext, mallInfoTool } from './mall-info-tool.js';
|
|
25
|
+
export { preTools } from './pre-tools.js';
|
|
26
|
+
|
|
27
|
+
export const serverModule = {
|
|
28
|
+
declaration: {
|
|
29
|
+
moduleId: 'mall-core',
|
|
30
|
+
},
|
|
31
|
+
cards: {},
|
|
32
|
+
preTools,
|
|
33
|
+
register(ctx: ServerContext): void {
|
|
34
|
+
// mall-info 工具上下文绑定(execute 惰性读取 ctx.integrations——register 期赋值)
|
|
35
|
+
bindMallInfoContext(ctx);
|
|
36
|
+
|
|
37
|
+
// 域装配注册(W1-③ 注册表消费首例):raw 缺席(无 integrations.json)→ 不注册,
|
|
38
|
+
// 装配点按"无控制面"降级(语义对齐迁移前模板 if (deployment) 分支)
|
|
39
|
+
const assembly = createScopeAssembly(ctx.integrations?.raw, ctx);
|
|
40
|
+
if (assembly) {
|
|
41
|
+
ctx.domainAssemblies.register(SCOPE_ASSEMBLY_KIND, assembly);
|
|
42
|
+
|
|
43
|
+
// 015 S7:mall-directory 服务注入(deployment.malls 单一来源;shopping-guide
|
|
44
|
+
// 「附近商场」数据源 + customer-service-faq 客服电话;模块只 resolveService 消费)
|
|
45
|
+
ctx.services.register('mall-directory', {
|
|
46
|
+
nearbyMalls: () =>
|
|
47
|
+
assembly.controlPlane.malls.map((m) => ({
|
|
48
|
+
mallId: m.mallId,
|
|
49
|
+
name: m.name,
|
|
50
|
+
address: `${m.name}(deployment 配置)`,
|
|
51
|
+
status: { code: 'open' as const, label: '营业中' },
|
|
52
|
+
})),
|
|
53
|
+
contactPhone: (mallId: string) =>
|
|
54
|
+
assembly.controlPlane.malls.find((m) => m.mallId === mallId)?.contactPhone,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// mall.info 缓存面(mall.info-cache,W3-3):缓存装饰跨模块暴露——缓存纪律
|
|
59
|
+
// (TTL/负缓存/LRU/single-flight/形状守卫)见 ./mall-info-cache;消费方 resolveService 直读
|
|
60
|
+
ctx.services.register(MALL_INFO_CACHE_SERVICE, {
|
|
61
|
+
cachedMallName: (mallId: string) => cachedMallName(mallId),
|
|
62
|
+
resolveMallInfo: (mallId: string) => resolveMallInfo(mallId, ctx.integrations),
|
|
63
|
+
} satisfies MallInfoCachePort);
|
|
64
|
+
|
|
65
|
+
ctx.tools.register(mallInfoTool);
|
|
66
|
+
},
|
|
67
|
+
} satisfies ServerModule;
|
|
68
|
+
|
|
69
|
+
export default serverModule;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 商场基础信息缓存(mall.info 槽消费方;动态商场信息源)。
|
|
3
|
+
*
|
|
4
|
+
* 自 mall 模板 config/mall-info.ts 迁入(单一模板统一化 W3-1;语义逐字保留)。
|
|
5
|
+
*
|
|
6
|
+
* 职责:
|
|
7
|
+
* - cachedMallName:withMallName 同步回退通道(会话建立后 mallName 直接回显)
|
|
8
|
+
* - resolveMallInfo:缓存 → single-flight → mall.info 槽拉取;失败/未知 → undefined(fail-soft)
|
|
9
|
+
*
|
|
10
|
+
* 缓存纪律(S1 DoS 加固):
|
|
11
|
+
* - 正缓存 TTL 30min(plaza 信息低频变更)
|
|
12
|
+
* - 负缓存 TTL 5min(垃圾/拉取失败 ID 防重复打上游)
|
|
13
|
+
* - LRU 封顶 1024(Map 插入序近似,可接受)
|
|
14
|
+
* - in-flight 去重(single-flight:并发同 ID 只打一次上游)
|
|
15
|
+
*
|
|
16
|
+
* mallId 形状守卫与 provider 侧同则(双保险,防 path 注入类字符进上游 URL)。
|
|
17
|
+
*/
|
|
18
|
+
import type { MallInfo, MallInfoPort } from '@tbox.cn/app-contracts-mall';
|
|
19
|
+
import type { IntegrationsRegistry } from '@tbox.cn/app-sdk/server';
|
|
20
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
21
|
+
|
|
22
|
+
const CACHE_TTL_MS = 30 * 60 * 1000; // plaza 信息低频变更(实测 updateTime 佐证)
|
|
23
|
+
const NEGATIVE_TTL_MS = 5 * 60 * 1000; // 负缓存:垃圾/拉取失败 ID 防重复打上游
|
|
24
|
+
const CACHE_MAX_ENTRIES = 1024; // LRU 封顶
|
|
25
|
+
const MALL_ID_RE = /^[\w-]{1,32}$/; // 形状守卫(与 provider 侧同则,双保险)
|
|
26
|
+
|
|
27
|
+
interface CacheEntry {
|
|
28
|
+
mallInfo?: MallInfo;
|
|
29
|
+
negative?: boolean;
|
|
30
|
+
expiresAt: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const cache = new Map<string, CacheEntry>();
|
|
34
|
+
const inFlight = new Map<string, Promise<MallInfo | undefined>>(); // single-flight
|
|
35
|
+
|
|
36
|
+
/** 匿名请求形态(mall.info 无用户态) */
|
|
37
|
+
const ANON_REQUEST: RequestContext = {
|
|
38
|
+
identity: { userId: '', source: 'anonymous' },
|
|
39
|
+
credentials: { provider: 'anonymous', token: '' },
|
|
40
|
+
attributes: {},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** 缓存名读取(withMallName 同步回退通道;过期/负缓存 → undefined) */
|
|
44
|
+
export function cachedMallName(mallId: string): string | undefined {
|
|
45
|
+
const entry = cache.get(mallId);
|
|
46
|
+
if (!entry || entry.expiresAt <= Date.now()) {
|
|
47
|
+
cache.delete(mallId);
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return entry.negative ? undefined : entry.mallInfo?.name;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 槽解析(未装配/未配置 → null;失败 → null,fail-soft) */
|
|
54
|
+
async function resolveMallInfoService(
|
|
55
|
+
mallId: string,
|
|
56
|
+
integrations: IntegrationsRegistry | undefined,
|
|
57
|
+
): Promise<MallInfoPort | null> {
|
|
58
|
+
if (!integrations) return null;
|
|
59
|
+
try {
|
|
60
|
+
const svc = (await integrations.resolveService({
|
|
61
|
+
service: 'mall.info',
|
|
62
|
+
scope: { mallId },
|
|
63
|
+
request: ANON_REQUEST,
|
|
64
|
+
})) as MallInfoPort | null | undefined;
|
|
65
|
+
return svc ?? null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null; // 槽未装配/未配置/通配缺席——fail-soft 返回 null
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 解析商场信息(缓存 → single-flight → mall.info 槽拉取)。
|
|
73
|
+
* 失败/未知/形状非法 → undefined(调用方负责降级)。
|
|
74
|
+
*/
|
|
75
|
+
export async function resolveMallInfo(
|
|
76
|
+
mallId: string,
|
|
77
|
+
integrations: IntegrationsRegistry | undefined,
|
|
78
|
+
): Promise<MallInfo | undefined> {
|
|
79
|
+
if (!MALL_ID_RE.test(mallId)) return undefined;
|
|
80
|
+
|
|
81
|
+
// 1. 缓存命中
|
|
82
|
+
const entry = cache.get(mallId);
|
|
83
|
+
if (entry && entry.expiresAt > Date.now()) {
|
|
84
|
+
return entry.negative ? undefined : entry.mallInfo;
|
|
85
|
+
}
|
|
86
|
+
if (entry) cache.delete(mallId); // 过期即删
|
|
87
|
+
|
|
88
|
+
// 2. single-flight:并发同 ID 共享一次上游拉取
|
|
89
|
+
const existing = inFlight.get(mallId);
|
|
90
|
+
if (existing) return existing;
|
|
91
|
+
|
|
92
|
+
const promise = (async (): Promise<MallInfo | undefined> => {
|
|
93
|
+
const service = await resolveMallInfoService(mallId, integrations);
|
|
94
|
+
if (!service) {
|
|
95
|
+
cache.set(mallId, { negative: true, expiresAt: Date.now() + NEGATIVE_TTL_MS });
|
|
96
|
+
trimCache();
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const info = await service.getMallInfo(mallId);
|
|
101
|
+
if (!info) {
|
|
102
|
+
cache.set(mallId, { negative: true, expiresAt: Date.now() + NEGATIVE_TTL_MS });
|
|
103
|
+
} else {
|
|
104
|
+
cache.set(mallId, { mallInfo: info, expiresAt: Date.now() + CACHE_TTL_MS });
|
|
105
|
+
}
|
|
106
|
+
trimCache();
|
|
107
|
+
return info;
|
|
108
|
+
} catch {
|
|
109
|
+
cache.set(mallId, { negative: true, expiresAt: Date.now() + NEGATIVE_TTL_MS });
|
|
110
|
+
trimCache();
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
})();
|
|
114
|
+
inFlight.set(mallId, promise);
|
|
115
|
+
try {
|
|
116
|
+
return await promise;
|
|
117
|
+
} finally {
|
|
118
|
+
inFlight.delete(mallId);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** LRU 封顶:Map 插入序近似 LRU(先插先出;缓存键量级远小于上限时无开销) */
|
|
123
|
+
function trimCache(): void {
|
|
124
|
+
while (cache.size > CACHE_MAX_ENTRIES) {
|
|
125
|
+
const firstKey = cache.keys().next().value;
|
|
126
|
+
if (firstKey === undefined) break;
|
|
127
|
+
cache.delete(firstKey as string);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// 铁律:不直接 import @mastra/core,经 @tbox.cn/app-sdk/server 的 createTool 入口(单例铁律)
|
|
2
|
+
import { createTool, type ServerContext } from '@tbox.cn/app-sdk/server';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { resolveMallInfo } from './mall-info-cache.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 商场基本信息工具(mall.info 槽消费方;动态商场信息源)。
|
|
8
|
+
* 自 mall 模板 tools/mall-info.ts 迁入(单一模板统一化 W3-1;语义逐字保留)。
|
|
9
|
+
*
|
|
10
|
+
* - 数据面 = 本模块缓存服务 resolveMallInfo(缓存 → single-flight → mall.info 槽拉取,fail-soft);
|
|
11
|
+
* - register 期经 bindMallInfoContext 绑定 ctx(execute 惰性读取 ctx.integrations——
|
|
12
|
+
* 对齐模块 getCtx 先例,实例分裂免疫;未绑定(裸上下文收集面)→ fail-soft found:false);
|
|
13
|
+
* - 不出卡:纯事实注入型工具(对齐 mall-context 结构型定位)——found:false 分支
|
|
14
|
+
* 天然无 cards 语义,无 cardType、不经 withCardEmissionHint。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** 模块级 ctx 引用(register 期绑定;execute 内读取——不 import SDK 全局单例,实例分裂免疫) */
|
|
18
|
+
let ctxRef: ServerContext | undefined;
|
|
19
|
+
|
|
20
|
+
/** register 期绑定(模块注册面;测试可显式绑定裸上下文) */
|
|
21
|
+
export function bindMallInfoContext(ctx: ServerContext | undefined): void {
|
|
22
|
+
ctxRef = ctx;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const mallInfoTool = createTool({
|
|
26
|
+
id: 'mall-info',
|
|
27
|
+
description:
|
|
28
|
+
'查询商场基本信息(名称/地址/经纬度)。用户问某商场叫什么、在哪儿、地址时调用;当前会话商场 ID 可从 mall-info 注入块取。',
|
|
29
|
+
// D11 参数规范:纯 string(preTool decide 注入 / 模型直传入参)
|
|
30
|
+
inputSchema: z.object({ mallId: z.string().describe('商场 ID') }),
|
|
31
|
+
// 无 outputSchema:可选字段动态形状(对齐 mall-context;出卡工具建议省略的约定此处同理由)
|
|
32
|
+
execute: async (input: { mallId?: string }) => {
|
|
33
|
+
const mallId = typeof input.mallId === 'string' ? input.mallId.trim() : '';
|
|
34
|
+
if (!mallId) return { found: false as const, reason: 'mallId 缺失' };
|
|
35
|
+
// fail-soft:槽未装配/未配置/未知 mallId/拉取失败 → undefined(缓存服务内部已收口)
|
|
36
|
+
const info = await resolveMallInfo(mallId, ctxRef?.integrations);
|
|
37
|
+
if (!info) {
|
|
38
|
+
return {
|
|
39
|
+
found: false as const,
|
|
40
|
+
mallId,
|
|
41
|
+
reason: '商场信息不可用(mall.info 槽未装配/未配置/未知 mallId)',
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
found: true as const,
|
|
46
|
+
mallId: info.mallId,
|
|
47
|
+
name: info.name,
|
|
48
|
+
...(info.address ? { address: info.address } : {}),
|
|
49
|
+
...(info.latitude != null ? { latitude: info.latitude } : {}),
|
|
50
|
+
...(info.longitude != null ? { longitude: info.longitude } : {}),
|
|
51
|
+
...(info.cityId ? { cityId: info.cityId } : {}),
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// 铁律:不直接 import @mastra/core,经 @tbox.cn/app-sdk/server 的 definePreTool 入口(单例铁律)
|
|
2
|
+
import { definePreTool, type PreToolDef } from '@tbox.cn/app-sdk/server';
|
|
3
|
+
import { mallSlot } from '@tbox.cn/app-contracts-mall';
|
|
4
|
+
import { mallInfoTool } from './mall-info-tool.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 模块前置工具声明(010 声明式通道;自 mall 模板 pre-tools.ts 迁入——单一模板统一化 W3-1)。
|
|
8
|
+
*
|
|
9
|
+
* mall-info —— 商场基本信息每轮背景注入(mall-context 同款三件套,2026-09-14):
|
|
10
|
+
* always + decide 自 mallSlot.read(attributes) 注入 mallId(B3 入参化——dedup 键随
|
|
11
|
+
* mallId 变更自动换键)+ silent + 会话内 dedup(accepts: found===true——
|
|
12
|
+
* not-found 不落键,下轮重试防钉死;缓存服务负缓存 5min 限流上游)。
|
|
13
|
+
*/
|
|
14
|
+
export const preTools = {
|
|
15
|
+
'mall-info': definePreTool(mallInfoTool, {
|
|
16
|
+
routing: { mode: 'always' },
|
|
17
|
+
// 上下文统一化:连接级领域属性经 decide 入参 attributes 判定(与 mall-context 同通道)
|
|
18
|
+
decide: ({ attributes }) => {
|
|
19
|
+
const scope = mallSlot.read(attributes);
|
|
20
|
+
if (!scope) return { decision: 'skip', reason: '无 mall scope(attributes 缺席/解析失败)' };
|
|
21
|
+
return { decision: 'run', args: { mallId: scope.mallId } };
|
|
22
|
+
},
|
|
23
|
+
silent: true,
|
|
24
|
+
dedup: {
|
|
25
|
+
accepts: (result) => (result as { found?: boolean } | null)?.found === true,
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
} satisfies Record<string, PreToolDef>;
|
package/tbox.module.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"name": "module-mall-core",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"kind": "business",
|
|
6
|
+
"risk": {
|
|
7
|
+
"level": "medium",
|
|
8
|
+
"writeBoundary": "source"
|
|
9
|
+
},
|
|
10
|
+
"distribution": {
|
|
11
|
+
"defaultMode": "codegen"
|
|
12
|
+
},
|
|
13
|
+
"contributes": {
|
|
14
|
+
"handlers": [],
|
|
15
|
+
"tools": [
|
|
16
|
+
"mall-info"
|
|
17
|
+
],
|
|
18
|
+
"cards": [],
|
|
19
|
+
"routes": [],
|
|
20
|
+
"pages": [],
|
|
21
|
+
"tabs": [],
|
|
22
|
+
"actions": [],
|
|
23
|
+
"services": []
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"modules": [
|
|
27
|
+
{
|
|
28
|
+
"id": "contracts-mall",
|
|
29
|
+
"required": true
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"env": []
|
|
34
|
+
}
|