@njuptlzf/agent-chat-server 0.1.1 → 0.2.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.
Files changed (65) hide show
  1. package/dist/audit.d.ts +54 -0
  2. package/dist/audit.js +133 -0
  3. package/dist/authz.d.ts +134 -0
  4. package/dist/authz.js +238 -0
  5. package/dist/events.d.ts +33 -0
  6. package/dist/events.js +169 -0
  7. package/dist/index.d.ts +16 -0
  8. package/dist/index.js +4 -0
  9. package/dist/main.d.ts +1 -0
  10. package/dist/main.js +126 -0
  11. package/dist/manifest.d.ts +147 -0
  12. package/dist/manifest.js +491 -0
  13. package/dist/mcp-config.d.ts +87 -0
  14. package/dist/mcp-config.js +353 -0
  15. package/dist/model-config.d.ts +123 -0
  16. package/dist/model-config.js +371 -0
  17. package/dist/model-failover.d.ts +42 -0
  18. package/dist/model-failover.js +82 -0
  19. package/dist/models-api.d.ts +98 -0
  20. package/dist/models-api.js +326 -0
  21. package/dist/persona-config.d.ts +112 -0
  22. package/dist/persona-config.js +368 -0
  23. package/dist/persona-trial.d.ts +50 -0
  24. package/dist/persona-trial.js +77 -0
  25. package/dist/product-routes.d.ts +35 -0
  26. package/dist/product-routes.js +92 -0
  27. package/dist/server.d.ts +162 -0
  28. package/dist/server.js +2572 -0
  29. package/dist/session.d.ts +218 -0
  30. package/dist/session.js +545 -0
  31. package/dist/sqlite-stores.d.ts +173 -0
  32. package/dist/sqlite-stores.js +921 -0
  33. package/dist/stores.d.ts +506 -0
  34. package/dist/stores.js +614 -0
  35. package/dist/testing.d.ts +42 -0
  36. package/dist/testing.js +47 -0
  37. package/dist/trial-grant.d.ts +40 -0
  38. package/dist/trial-grant.js +24 -0
  39. package/dist/usage-report.d.ts +23 -0
  40. package/dist/usage-report.js +68 -0
  41. package/dist/util.d.ts +3 -0
  42. package/dist/util.js +5 -0
  43. package/package.json +64 -58
  44. package/scripts/agentchat-integrate.mjs +607 -601
  45. package/src/audit.ts +169 -169
  46. package/src/authz.ts +307 -307
  47. package/src/events.ts +206 -206
  48. package/src/index.ts +26 -26
  49. package/src/main.ts +142 -142
  50. package/src/manifest.ts +562 -562
  51. package/src/mcp-config.ts +385 -385
  52. package/src/model-config.ts +424 -424
  53. package/src/model-failover.ts +95 -95
  54. package/src/models-api.ts +360 -360
  55. package/src/persona-config.ts +436 -436
  56. package/src/persona-trial.ts +121 -121
  57. package/src/product-routes.ts +102 -102
  58. package/src/server.ts +2647 -2647
  59. package/src/session.ts +688 -688
  60. package/src/sqlite-stores.ts +1040 -1040
  61. package/src/stores.ts +899 -899
  62. package/src/testing.ts +80 -80
  63. package/src/trial-grant.ts +62 -62
  64. package/src/usage-report.ts +86 -86
  65. package/src/util.ts +6 -6
@@ -1,121 +1,121 @@
1
- /**
2
- * 人格「试跑」:用**还没保存的草稿**真调一次模型。
3
- *
4
- * 与「编译预览」的分界:预览回答"拼出来是什么"(免费、不建会话),试跑回答
5
- * "它真的会怎么答"(花钱、真模型)。两者都要有——预览能看 prompt,看不出语气。
6
- *
7
- * 旁路(设计稿):**不写历史、不扣钱、不记 runLog、不占配额**。
8
- * 只留一条 `admin.persona.trial` 审计(在 server.ts 的路由里写)。
9
- * 理由是试跑是管理动作,不是用户消费:让它进用户账本会污染统计与限额。
10
- */
11
- import type { TokenUsage } from "@njuptlzf/agent-chat-protocol";
12
- import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
13
- import { createEventMapper, emptyUsage } from "./events.ts";
14
- import type { LoadedProduct, ToolContext } from "./manifest.ts";
15
- import type { McpConfigStore } from "./mcp-config.ts";
16
- import type { PersonaOverride } from "./persona-config.ts";
17
- import { createProductSession, disposeProductSession } from "./session.ts";
18
-
19
- /** 试跑最多等这么久:管理页不能因为模型卡住就永远转圈。 */
20
- export const TRIAL_TIMEOUT_MS = 90_000;
21
- /** 试跑输入上限:这是"试一句话",不是聊天窗口。 */
22
- export const TRIAL_MAX_INPUT_CHARS = 2_000;
23
-
24
- export interface TrialInput {
25
- userId: string;
26
- persona: string;
27
- message: string;
28
- /** 编辑器里的草稿;不传 = 用已保存的配置(与真实会话一致)。 */
29
- override?: PersonaOverride;
30
- qual?: string;
31
- timeoutMs?: number;
32
- }
33
-
34
- export interface TrialResult {
35
- reply: string;
36
- /** 声明给模型的工具名(与真实会话同一道白名单/停用过滤)。 */
37
- tools: string[];
38
- /** 这次运行里真的被调用过的工具。 */
39
- usedTools: string[];
40
- usage: TokenUsage;
41
- ms: number;
42
- }
43
-
44
- /** 试跑需要的装配件(与 createChatServer 的 deps 同源,单独列出来便于测试)。 */
45
- export interface TrialEnv {
46
- product: LoadedProduct;
47
- modelRuntime: ModelRuntime;
48
- toolFactory: (ctx: ToolContext) => unknown[];
49
- mcpConfig?: McpConfigStore;
50
- dataDir: string;
51
- }
52
-
53
- /**
54
- * 真跑一次。**复用 `createProductSession`**,所以 prompt、技能、工具白名单、
55
- * MCP 可见性都与真实会话走同一条编译链——试跑看到的不是"另一套实现"。
56
- */
57
- export async function runPersonaTrial(env: TrialEnv, input: TrialInput): Promise<TrialResult> {
58
- const started = Date.now();
59
- const tools = env.toolFactory({
60
- productId: env.product.manifest.id,
61
- productDir: env.product.dir,
62
- userId: input.userId,
63
- dataDir: env.dataDir,
64
- qual: input.qual ?? "",
65
- persona: input.persona,
66
- });
67
-
68
- const session = await createProductSession({
69
- product: env.product,
70
- modelRuntime: env.modelRuntime,
71
- tools,
72
- userId: input.userId,
73
- dataDir: env.dataDir,
74
- qual: input.qual,
75
- persona: input.persona,
76
- personaOverride: input.override,
77
- mcpConfig: env.mcpConfig,
78
- });
79
-
80
- const mapper = createEventMapper();
81
- const texts: string[] = [];
82
- const usedTools: string[] = [];
83
- let modelError: string | undefined;
84
- const unsubscribe = session.subscribe((event) => {
85
- for (const out of mapper.map(event)) {
86
- if (out.type === "message_end" && out.text) texts.push(out.text);
87
- if (out.type === "tool_start") usedTools.push(out.name);
88
- if (out.type === "error" && out.code === "model_error") modelError = out.message;
89
- }
90
- });
91
-
92
- const timeoutMs = input.timeoutMs ?? TRIAL_TIMEOUT_MS;
93
- let timedOut = false;
94
- const timer = setTimeout(() => {
95
- timedOut = true;
96
- void session.abort();
97
- }, timeoutMs);
98
- let usage = emptyUsage();
99
- let declared: string[] = [];
100
- try {
101
- await session.prompt(input.message);
102
- await session.waitForIdle();
103
- usage = mapper.totalUsage();
104
- // 必须在 dispose 之前读:声明给模型的工具以会话实际装配为准。
105
- declared = session.getActiveToolNames();
106
- } finally {
107
- clearTimeout(timer);
108
- unsubscribe();
109
- await disposeProductSession(session);
110
- }
111
-
112
- if (timedOut) throw new Error(`试跑超时(${Math.round(timeoutMs / 1000)} 秒),已中止`);
113
- if (modelError) throw new Error(modelError);
114
- return {
115
- reply: texts.join("\n\n").trim(),
116
- tools: declared,
117
- usedTools,
118
- usage,
119
- ms: Date.now() - started,
120
- };
121
- }
1
+ /**
2
+ * 人格「试跑」:用**还没保存的草稿**真调一次模型。
3
+ *
4
+ * 与「编译预览」的分界:预览回答"拼出来是什么"(免费、不建会话),试跑回答
5
+ * "它真的会怎么答"(花钱、真模型)。两者都要有——预览能看 prompt,看不出语气。
6
+ *
7
+ * 旁路(设计稿):**不写历史、不扣钱、不记 runLog、不占配额**。
8
+ * 只留一条 `admin.persona.trial` 审计(在 server.ts 的路由里写)。
9
+ * 理由是试跑是管理动作,不是用户消费:让它进用户账本会污染统计与限额。
10
+ */
11
+ import type { TokenUsage } from "@njuptlzf/agent-chat-protocol";
12
+ import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
13
+ import { createEventMapper, emptyUsage } from "./events.ts";
14
+ import type { LoadedProduct, ToolContext } from "./manifest.ts";
15
+ import type { McpConfigStore } from "./mcp-config.ts";
16
+ import type { PersonaOverride } from "./persona-config.ts";
17
+ import { createProductSession, disposeProductSession } from "./session.ts";
18
+
19
+ /** 试跑最多等这么久:管理页不能因为模型卡住就永远转圈。 */
20
+ export const TRIAL_TIMEOUT_MS = 90_000;
21
+ /** 试跑输入上限:这是"试一句话",不是聊天窗口。 */
22
+ export const TRIAL_MAX_INPUT_CHARS = 2_000;
23
+
24
+ export interface TrialInput {
25
+ userId: string;
26
+ persona: string;
27
+ message: string;
28
+ /** 编辑器里的草稿;不传 = 用已保存的配置(与真实会话一致)。 */
29
+ override?: PersonaOverride;
30
+ qual?: string;
31
+ timeoutMs?: number;
32
+ }
33
+
34
+ export interface TrialResult {
35
+ reply: string;
36
+ /** 声明给模型的工具名(与真实会话同一道白名单/停用过滤)。 */
37
+ tools: string[];
38
+ /** 这次运行里真的被调用过的工具。 */
39
+ usedTools: string[];
40
+ usage: TokenUsage;
41
+ ms: number;
42
+ }
43
+
44
+ /** 试跑需要的装配件(与 createChatServer 的 deps 同源,单独列出来便于测试)。 */
45
+ export interface TrialEnv {
46
+ product: LoadedProduct;
47
+ modelRuntime: ModelRuntime;
48
+ toolFactory: (ctx: ToolContext) => unknown[];
49
+ mcpConfig?: McpConfigStore;
50
+ dataDir: string;
51
+ }
52
+
53
+ /**
54
+ * 真跑一次。**复用 `createProductSession`**,所以 prompt、技能、工具白名单、
55
+ * MCP 可见性都与真实会话走同一条编译链——试跑看到的不是"另一套实现"。
56
+ */
57
+ export async function runPersonaTrial(env: TrialEnv, input: TrialInput): Promise<TrialResult> {
58
+ const started = Date.now();
59
+ const tools = env.toolFactory({
60
+ productId: env.product.manifest.id,
61
+ productDir: env.product.dir,
62
+ userId: input.userId,
63
+ dataDir: env.dataDir,
64
+ qual: input.qual ?? "",
65
+ persona: input.persona,
66
+ });
67
+
68
+ const session = await createProductSession({
69
+ product: env.product,
70
+ modelRuntime: env.modelRuntime,
71
+ tools,
72
+ userId: input.userId,
73
+ dataDir: env.dataDir,
74
+ qual: input.qual,
75
+ persona: input.persona,
76
+ personaOverride: input.override,
77
+ mcpConfig: env.mcpConfig,
78
+ });
79
+
80
+ const mapper = createEventMapper();
81
+ const texts: string[] = [];
82
+ const usedTools: string[] = [];
83
+ let modelError: string | undefined;
84
+ const unsubscribe = session.subscribe((event) => {
85
+ for (const out of mapper.map(event)) {
86
+ if (out.type === "message_end" && out.text) texts.push(out.text);
87
+ if (out.type === "tool_start") usedTools.push(out.name);
88
+ if (out.type === "error" && out.code === "model_error") modelError = out.message;
89
+ }
90
+ });
91
+
92
+ const timeoutMs = input.timeoutMs ?? TRIAL_TIMEOUT_MS;
93
+ let timedOut = false;
94
+ const timer = setTimeout(() => {
95
+ timedOut = true;
96
+ void session.abort();
97
+ }, timeoutMs);
98
+ let usage = emptyUsage();
99
+ let declared: string[] = [];
100
+ try {
101
+ await session.prompt(input.message);
102
+ await session.waitForIdle();
103
+ usage = mapper.totalUsage();
104
+ // 必须在 dispose 之前读:声明给模型的工具以会话实际装配为准。
105
+ declared = session.getActiveToolNames();
106
+ } finally {
107
+ clearTimeout(timer);
108
+ unsubscribe();
109
+ await disposeProductSession(session);
110
+ }
111
+
112
+ if (timedOut) throw new Error(`试跑超时(${Math.round(timeoutMs / 1000)} 秒),已中止`);
113
+ if (modelError) throw new Error(modelError);
114
+ return {
115
+ reply: texts.join("\n\n").trim(),
116
+ tools: declared,
117
+ usedTools,
118
+ usage,
119
+ ms: Date.now() - started,
120
+ };
121
+ }
@@ -1,102 +1,102 @@
1
- /**
2
- * 按 manifest 的 `server.routes` 声明**装配**产品业务路由。
3
- *
4
- * 装配逻辑放在 core 而非入口脚本中,是为了让形状校验可测:
5
- * `createRoutes?.()` 在形状不对时若返回 undefined,会静默不挂任何产品路由、
6
- * 服务照常起来而产品页面全线 404(fail-open)。
7
- *
8
- * 抽成 core 的两个函数后,三件事变得可测(进程内,不用 spawn 真进程):
9
- * ① 形状校验:声明了 routes 就必须导出合法的 createRoutes(),
10
- * 否则**抛错**(fail-closed:启动失败,不是静默空路由);
11
- * ② init() 的错误必须往外传(不吞、不降级);
12
- * ③ core 不 import 任何产品——路径是运行时从 manifest 读的,
13
- * 动态 import 的 URL 是算出来的(静态 import 才会被边界守卫抓到)。
14
- */
15
- import path from "node:path";
16
- import { pathToFileURL } from "node:url";
17
- import type { LoadedProduct } from "./manifest.ts";
18
- import type { ProductAdminRoute, ProductRoute, ServerDeps } from "./server.ts";
19
-
20
- /** 产品路由模块必须导出的形状(createRoutes() 的返回值)。 */
21
- export interface ProductRouteModule {
22
- /** 用户面路由。 */
23
- routes: ProductRoute[];
24
- /** 管理面路由(闸名与 core 内建组同一命名空间)。 */
25
- admin: ProductAdminRoute[];
26
- /** 启动期装配钩子(业务数据/索引之类)。可省略。 */
27
- init?(deps: ServerDeps): void | Promise<void>;
28
- }
29
-
30
- /**
31
- * 加载产品路由模块并校验形状。
32
- *
33
- * - manifest 没声明 `server.routes` ⇒ 返回 undefined(纯 core 部署,没有产品路由);
34
- * - 声明了但文件不在/形状不对 ⇒ **抛错**(调用方据此启动失败)。
35
- *
36
- * 校验是逐项的,因为每个字段错了都会静默丢功能:
37
- * routes 缺 createRoutes / 不是数组 ⇒ 产品业务路由全丢(页面 404);
38
- * admin 写成别的键名 ⇒ 管理页 404;
39
- * admin 条目少 match/group/handle ⇒ 闸判定错(可能放行或全拒)。
40
- */
41
- export async function loadProductRoutes(product: LoadedProduct): Promise<ProductRouteModule | undefined> {
42
- const rel = product.manifest.server?.routes;
43
- if (!rel) return undefined;
44
- const mod = (await import(pathToFileURL(path.resolve(product.dir, rel)).href)) as {
45
- createRoutes?: unknown;
46
- };
47
- const create = mod?.createRoutes;
48
- if (typeof create !== "function") {
49
- throw new Error(`产品路由模块 ${rel} 必须导出 createRoutes()`);
50
- }
51
- const made = (create as () => unknown)();
52
- if (!made || typeof made !== "object") {
53
- throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回对象`);
54
- }
55
- const m = made as Partial<ProductRouteModule>;
56
- if (!Array.isArray(m.routes)) {
57
- throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回 { routes: [...] }`);
58
- }
59
- if (!Array.isArray(m.admin)) {
60
- throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回 { admin: [...] }`);
61
- }
62
- for (const [i, r] of m.routes.entries()) {
63
- if (typeof r !== "function") {
64
- throw new Error(`产品路由模块 ${rel} 的 routes[${i}] 必须是函数`);
65
- }
66
- }
67
- for (const [i, a] of m.admin.entries()) {
68
- const bad = !a || typeof a.match !== "function" || typeof a.group !== "string" || typeof a.handle !== "function";
69
- if (bad) {
70
- throw new Error(`产品路由模块 ${rel} 的 admin[${i}] 必须是 { match(), group, handle() }`);
71
- }
72
- }
73
- if (m.init !== undefined && typeof m.init !== "function") {
74
- throw new Error(`产品路由模块 ${rel} 的 init 必须是函数(可省略)`);
75
- }
76
- return { routes: m.routes, admin: m.admin, init: m.init };
77
- }
78
-
79
- /**
80
- * 调产品的 init() 钩子。**错误必须往外传**——产品装配失败就是启动失败,
81
- * 不能吞掉降级成「起来了但业务数据是空的」(业务数据坏了该启动红,不是首请求 500)。
82
- */
83
- export async function initProductRoutes(mod: ProductRouteModule | undefined, deps: ServerDeps): Promise<void> {
84
- await mod?.init?.(deps);
85
- }
86
-
87
- /** 产品装配模块必须导出的形状:createProductStores(db) 返回要并入 ServerDeps 的产品 Store。 */
88
- export type ProductAssembly = (db: unknown) => Record<string, unknown>;
89
-
90
- /**
91
- * 按 manifest 加载产品装配根(S4)。
92
- * 未声明 ⇒ undefined(纯 core 部署);声明了但形状不对 ⇒ 抛错,启动失败而不是静默缺 Store。
93
- */
94
- export async function loadProductAssembly(product: LoadedProduct): Promise<ProductAssembly | undefined> {
95
- const rel = product.manifest.server?.assembly;
96
- if (!rel) return undefined;
97
- const mod = (await import(pathToFileURL(path.resolve(product.dir, rel)).href)) as { createProductStores?: unknown };
98
- if (typeof mod.createProductStores !== "function") {
99
- throw new Error(`产品装配模块 ${rel} 必须导出 createProductStores()`);
100
- }
101
- return mod.createProductStores as ProductAssembly;
102
- }
1
+ /**
2
+ * 按 manifest 的 `server.routes` 声明**装配**产品业务路由。
3
+ *
4
+ * 装配逻辑放在 core 而非入口脚本中,是为了让形状校验可测:
5
+ * `createRoutes?.()` 在形状不对时若返回 undefined,会静默不挂任何产品路由、
6
+ * 服务照常起来而产品页面全线 404(fail-open)。
7
+ *
8
+ * 抽成 core 的两个函数后,三件事变得可测(进程内,不用 spawn 真进程):
9
+ * ① 形状校验:声明了 routes 就必须导出合法的 createRoutes(),
10
+ * 否则**抛错**(fail-closed:启动失败,不是静默空路由);
11
+ * ② init() 的错误必须往外传(不吞、不降级);
12
+ * ③ core 不 import 任何产品——路径是运行时从 manifest 读的,
13
+ * 动态 import 的 URL 是算出来的(静态 import 才会被边界守卫抓到)。
14
+ */
15
+ import path from "node:path";
16
+ import { pathToFileURL } from "node:url";
17
+ import type { LoadedProduct } from "./manifest.ts";
18
+ import type { ProductAdminRoute, ProductRoute, ServerDeps } from "./server.ts";
19
+
20
+ /** 产品路由模块必须导出的形状(createRoutes() 的返回值)。 */
21
+ export interface ProductRouteModule {
22
+ /** 用户面路由。 */
23
+ routes: ProductRoute[];
24
+ /** 管理面路由(闸名与 core 内建组同一命名空间)。 */
25
+ admin: ProductAdminRoute[];
26
+ /** 启动期装配钩子(业务数据/索引之类)。可省略。 */
27
+ init?(deps: ServerDeps): void | Promise<void>;
28
+ }
29
+
30
+ /**
31
+ * 加载产品路由模块并校验形状。
32
+ *
33
+ * - manifest 没声明 `server.routes` ⇒ 返回 undefined(纯 core 部署,没有产品路由);
34
+ * - 声明了但文件不在/形状不对 ⇒ **抛错**(调用方据此启动失败)。
35
+ *
36
+ * 校验是逐项的,因为每个字段错了都会静默丢功能:
37
+ * routes 缺 createRoutes / 不是数组 ⇒ 产品业务路由全丢(页面 404);
38
+ * admin 写成别的键名 ⇒ 管理页 404;
39
+ * admin 条目少 match/group/handle ⇒ 闸判定错(可能放行或全拒)。
40
+ */
41
+ export async function loadProductRoutes(product: LoadedProduct): Promise<ProductRouteModule | undefined> {
42
+ const rel = product.manifest.server?.routes;
43
+ if (!rel) return undefined;
44
+ const mod = (await import(pathToFileURL(path.resolve(product.dir, rel)).href)) as {
45
+ createRoutes?: unknown;
46
+ };
47
+ const create = mod?.createRoutes;
48
+ if (typeof create !== "function") {
49
+ throw new Error(`产品路由模块 ${rel} 必须导出 createRoutes()`);
50
+ }
51
+ const made = (create as () => unknown)();
52
+ if (!made || typeof made !== "object") {
53
+ throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回对象`);
54
+ }
55
+ const m = made as Partial<ProductRouteModule>;
56
+ if (!Array.isArray(m.routes)) {
57
+ throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回 { routes: [...] }`);
58
+ }
59
+ if (!Array.isArray(m.admin)) {
60
+ throw new Error(`产品路由模块 ${rel} 的 createRoutes() 必须返回 { admin: [...] }`);
61
+ }
62
+ for (const [i, r] of m.routes.entries()) {
63
+ if (typeof r !== "function") {
64
+ throw new Error(`产品路由模块 ${rel} 的 routes[${i}] 必须是函数`);
65
+ }
66
+ }
67
+ for (const [i, a] of m.admin.entries()) {
68
+ const bad = !a || typeof a.match !== "function" || typeof a.group !== "string" || typeof a.handle !== "function";
69
+ if (bad) {
70
+ throw new Error(`产品路由模块 ${rel} 的 admin[${i}] 必须是 { match(), group, handle() }`);
71
+ }
72
+ }
73
+ if (m.init !== undefined && typeof m.init !== "function") {
74
+ throw new Error(`产品路由模块 ${rel} 的 init 必须是函数(可省略)`);
75
+ }
76
+ return { routes: m.routes, admin: m.admin, init: m.init };
77
+ }
78
+
79
+ /**
80
+ * 调产品的 init() 钩子。**错误必须往外传**——产品装配失败就是启动失败,
81
+ * 不能吞掉降级成「起来了但业务数据是空的」(业务数据坏了该启动红,不是首请求 500)。
82
+ */
83
+ export async function initProductRoutes(mod: ProductRouteModule | undefined, deps: ServerDeps): Promise<void> {
84
+ await mod?.init?.(deps);
85
+ }
86
+
87
+ /** 产品装配模块必须导出的形状:createProductStores(db) 返回要并入 ServerDeps 的产品 Store。 */
88
+ export type ProductAssembly = (db: unknown) => Record<string, unknown>;
89
+
90
+ /**
91
+ * 按 manifest 加载产品装配根(S4)。
92
+ * 未声明 ⇒ undefined(纯 core 部署);声明了但形状不对 ⇒ 抛错,启动失败而不是静默缺 Store。
93
+ */
94
+ export async function loadProductAssembly(product: LoadedProduct): Promise<ProductAssembly | undefined> {
95
+ const rel = product.manifest.server?.assembly;
96
+ if (!rel) return undefined;
97
+ const mod = (await import(pathToFileURL(path.resolve(product.dir, rel)).href)) as { createProductStores?: unknown };
98
+ if (typeof mod.createProductStores !== "function") {
99
+ throw new Error(`产品装配模块 ${rel} 必须导出 createProductStores()`);
100
+ }
101
+ return mod.createProductStores as ProductAssembly;
102
+ }