@njuptlzf/agent-chat-web 0.2.0 → 0.3.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 (61) hide show
  1. package/dist/AgentChat.d.ts +7 -1
  2. package/dist/AgentChat.js +23 -5
  3. package/dist/AgentChatApp.d.ts +4 -1
  4. package/dist/AgentChatApp.js +2 -2
  5. package/dist/PersonasPage.js +8 -0
  6. package/dist/assets/_commonjs-dynamic-modules-TDtrdbi3.js +1 -0
  7. package/dist/assets/exceljs.min-jlEt4kSc.js +29 -0
  8. package/dist/assets/index-B7uSE8SM.css +1 -0
  9. package/dist/assets/index-D57mU5H5.js +50 -0
  10. package/dist/assets/index-Dviy7gnB.js +44 -0
  11. package/dist/assets/pdf-CfhDiWmk.js +55 -0
  12. package/dist/assets/pdf.worker.min-CdXBBzB4.js +1 -0
  13. package/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
  14. package/dist/index.html +13 -0
  15. package/dist/mount.d.ts +7 -1
  16. package/dist/mount.js +2 -2
  17. package/dist/styles.css +1492 -1478
  18. package/dist/suggestions.d.ts +41 -0
  19. package/dist/suggestions.js +98 -0
  20. package/dist/ui/FollowUpSuggestions.d.ts +9 -0
  21. package/dist/ui/FollowUpSuggestions.js +13 -0
  22. package/dist/ui/Icon.d.ts +8 -0
  23. package/dist/ui/Icon.js +26 -0
  24. package/dist/ui/icons.d.ts +19 -0
  25. package/dist/ui/icons.js +25 -0
  26. package/dist/ui/slots.d.ts +23 -0
  27. package/dist/ui/slots.js +13 -0
  28. package/package.json +71 -72
  29. package/src/App.tsx +13 -13
  30. package/src/agent-chat/AgentChat.tsx +729 -692
  31. package/src/agent-chat/AgentChatApp.tsx +211 -206
  32. package/src/agent-chat/PersonasPage.tsx +705 -678
  33. package/src/agent-chat/adapter.ts +272 -272
  34. package/src/agent-chat/app-def-check.ts +215 -215
  35. package/src/agent-chat/app.tsx +327 -327
  36. package/src/agent-chat/auth.ts +83 -83
  37. package/src/agent-chat/dock.ts +161 -161
  38. package/src/agent-chat/docs.ts +179 -179
  39. package/src/agent-chat/index.ts +17 -17
  40. package/src/agent-chat/mount.tsx +8 -1
  41. package/src/agent-chat/qual.ts +55 -55
  42. package/src/agent-chat/sse.ts +104 -104
  43. package/src/agent-chat/suggestions.ts +127 -0
  44. package/src/agent-chat/theme.ts +23 -23
  45. package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -29
  46. package/src/agent-chat/ui/ConfirmCard.tsx +32 -32
  47. package/src/agent-chat/ui/FollowUpSuggestions.tsx +28 -0
  48. package/src/agent-chat/ui/Icon.tsx +53 -0
  49. package/src/agent-chat/ui/Lightbox.tsx +32 -32
  50. package/src/agent-chat/ui/QueueBar.tsx +70 -70
  51. package/src/agent-chat/ui/Splitter.tsx +81 -81
  52. package/src/agent-chat/ui/ThemeToggle.tsx +25 -25
  53. package/src/agent-chat/ui/TierPicker.tsx +112 -112
  54. package/src/agent-chat/ui/UIPromptCard.tsx +68 -68
  55. package/src/agent-chat/ui/icons.ts +31 -0
  56. package/src/agent-chat/ui/slots.tsx +35 -0
  57. package/src/agent-chat/ui/timefmt.ts +110 -110
  58. package/src/agent-chat/ui/useNarrow.ts +20 -20
  59. package/src/fake-product/entry.tsx +63 -54
  60. package/src/main.tsx +8 -8
  61. package/src/styles.css +1492 -1478
@@ -1,215 +1,215 @@
1
- /**
2
- * 接缝:core 对 `AppDef` 的**结构契约**校验器。
3
- *
4
- * 为什么要有这个:core 的 `useAppHost` 从 def 里读七八个字段,每个字段都带着
5
- * 一条**没说出口**的要求(写错了不报错,只是某个路由失灵)。第二接入方照抄
6
- * `App.tsx` 时无从知道这些——只能靠「跑起来看侧栏少了一项」。
7
- *
8
- * 这里把隐含要求写成显式的校验器:产品写完 def 跑一遍,报错就是问题。
9
- * core 不 import 任何产品(边界守卫守着),校验器只认结构。
10
- */
11
- import type { AppDef, HostCtx } from "./app.tsx";
12
-
13
- export interface DefProblem {
14
- /** 出问题的位置(字段名或路由键)。 */
15
- where: string;
16
- /** 人话说明 + 怎么修。 */
17
- message: string;
18
- }
19
-
20
- /**
21
- * 路由键的合法形状。
22
- *
23
- * core 的 `routeFromHash` 用 `/^#\/([a-z]+)/` 从 hash 里取路由键——**只认小写
24
- * 字母**。所以 "my-page"、"q1"、"记录" 这类键在 hash 里取不出来,会被静默
25
- * 打回 chatRoute(页面看着像「点不动」)。这条以前只有 core 源码里那个正则
26
- * 说过,现在写进契约里。
27
- */
28
- export const ROUTE_KEY_RE = /^[a-z]+$/;
29
-
30
- /** rewrites 的值可以带 query(`record?tab=wrong`),取路径部分来判合法性。 */
31
- function routePathOf(target: string): string {
32
- return target.split("?")[0]!;
33
- }
34
-
35
- /**
36
- * 校验用的**标准上下文**(构建期与测试共用,避免两处各造一份漂移)。
37
- *
38
- * 为什么要好几个:`navItems` 与 `guard` 是**动态**的——侧栏条目随「有没有选
39
- * 选项、这个选项有哪些科目、是不是 admin、审计有没有被取消」而变。只用一个
40
- * 上下文校验,等于只测了其中一条分支;其余分支写错了照样发版。
41
- *
42
- * 四份覆盖了所有会改变导航的条件组合:未登录 / 普通用户单科 / 普通用户三科 /
43
- * 管理员三科。第二接入方如果有更多维度(比如按会员等级显示入口),自己再补。
44
- */
45
- export function sampleHostContexts(): Array<{ name: string; ctx: HostCtx }> {
46
- const branding = {
47
- title: "示例产品",
48
- defaultPersona: "default",
49
- personas: { default: { label: "助手" } },
50
- } as unknown as HostCtx["branding"];
51
- const qual = (subjects: string[]) =>
52
- ({ id: "demo", level: "senior", label: "示例选项", subjects }) as unknown as HostCtx["qual"];
53
- const who = (roles: string[], extra: Record<string, unknown> = {}) =>
54
- ({
55
- userId: "u1",
56
- roles,
57
- authzEnabled: true,
58
- memberUntil: null,
59
- bannedUntil: null,
60
- auditRevoked: false,
61
- quota: { runsToday: 0, runCap: null, tokensToday: 0, tokenCap: null },
62
- ...extra,
63
- }) as unknown as HostCtx["who"];
64
- const base = {
65
- branding,
66
- route: "",
67
- persona: "",
68
- onQuals: false,
69
- go: () => {},
70
- pickQual: () => {},
71
- };
72
- return [
73
- { name: "未登录", ctx: { ...base, who: null, qual: null } as HostCtx },
74
- { name: "普通用户·单科", ctx: { ...base, who: who(["user"]), qual: qual(["mcq"]) } as HostCtx },
75
- { name: "普通用户·三科", ctx: { ...base, who: who(["user"]), qual: qual(["mcq", "case", "essay"]) } as HostCtx },
76
- { name: "管理员·三科", ctx: { ...base, who: who(["admin"]), qual: qual(["mcq", "case", "essay"]) } as HostCtx },
77
- // 审计被取消是一条独立分支:navItems 会隐去审计入口,guard 会把它弹回。
78
- { name: "普通用户·审计被取消", ctx: { ...base, who: who(["user"], { auditRevoked: true }), qual: qual(["mcq"]) } as HostCtx },
79
- ];
80
- }
81
-
82
- /**
83
- * 校验一份 AppDef 是否满足 core 的要求。
84
- *
85
- * 返回**空数组** = 没问题。每条问题都指明位置与怎么修。
86
- *
87
- * 注意 `ctx` 是必需的:`navItems` 与 `guard` 都要 ctx 才能算出结果(它们依赖
88
- * 当前选项与登录身份),而这两个字段的错误恰好是最难自查的(侧栏少一项 /
89
- * 守卫把人弹到不存在的页)。
90
- */
91
- export function validateAppDef(def: AppDef, ctx: HostCtx): DefProblem[] {
92
- const problems: DefProblem[] = [];
93
- const add = (where: string, message: string) => problems.push({ where, message });
94
-
95
- // ① routes:合法 hash 路由的键集。core 认不出的键 = 一个进不去的页面。
96
- const routes = def.routes;
97
- if (!routes || typeof routes !== "object" || Array.isArray(routes)) {
98
- add("routes", "必须是对象(路由键 → 导航文案)");
99
- return problems; // 后面几项都依赖它,缺了就别报噪音了
100
- }
101
- const keys = Object.keys(routes);
102
- if (keys.length === 0) {
103
- add("routes", "不能为空——core 用它认合法 hash 路由,空表等于所有链接都打回 chatRoute");
104
- }
105
- for (const k of keys) {
106
- if (!ROUTE_KEY_RE.test(k)) {
107
- add(`routes.${k}`, "路由键只能是小写字母(core 用 /^#\\/([a-z]+)/ 取键,含数字/连字符/中文会取不出来)");
108
- }
109
- if (typeof routes[k] !== "string" || !routes[k]!.trim()) {
110
- add(`routes.${k}`, "导航文案必须是非空字符串(侧栏会显示它)");
111
- }
112
- }
113
-
114
- // ② chatRoute:聊天坞的路由键。它在 routes 里才谈得上「有文案、有高亮」。
115
- // 两条判据各管一种情况:空值(必填没填)vs 非空但不在路由表里。
116
- if (!def.chatRoute) {
117
- add("chatRoute", "必填(core 用它兜底:认不出的 hash 路由都回落到它)");
118
- } else if (!(def.chatRoute in routes)) {
119
- add("chatRoute", `"${def.chatRoute}" 不在 routes 里(core 认不出这个路由键,且侧栏没有它的文案)`);
120
- }
121
-
122
- // ③ home:收起聊天坞时退回的业务页。必须是合法路由,否则退回后又被打回 chatRoute。
123
- if (!def.home) {
124
- add("home", "必填(core 用它作为「最后一个业务页」的初值)");
125
- } else if (!(def.home in routes)) {
126
- add("home", `"${def.home}" 不在 routes 里(core 退回这个页时会被打回 chatRoute)`);
127
- }
128
-
129
- // ④ rewrites:老链接就地改写。**键是老链接**(可能早就不在 routes 里了——
130
- // 那正是要改写的原因),只有目标必须是合法路由。
131
- for (const [from, to] of Object.entries(def.rewrites ?? {})) {
132
- if (typeof to !== "string" || !to.trim()) {
133
- add(`rewrites.${from}`, "改写目标必须是非空字符串");
134
- continue;
135
- }
136
- const p = routePathOf(to);
137
- if (!(p in routes)) {
138
- add(`rewrites.${from}`, `改写目标 "${to}" 的路由 "${p}" 不在 routes 里(改写过去就是个进不去的页)`);
139
- }
140
- }
141
-
142
- // ⑤ navAlias:高亮归属。值必须是合法路由,否则高亮无处可落(那一项永远不亮)。
143
- for (const [from, to] of Object.entries(def.navAlias ?? {})) {
144
- if (!(from in routes)) {
145
- add(`navAlias.${from}`, "高亮归属的键不在 routes 里");
146
- }
147
- if (typeof to !== "string" || !(to in routes)) {
148
- add(`navAlias.${from}`, `高亮归属目标 "${to}" 不在 routes 里(侧栏那一项永远不高亮)`);
149
- }
150
- }
151
-
152
- // ⑥ navItems:侧栏条目。每项的 route 必须有文案(core 直接渲染 label)。
153
- try {
154
- const items = def.navItems(ctx);
155
- const navRoutes = items.map((i) => i.route);
156
- for (const [i, item] of items.entries()) {
157
- if (!item || typeof item.route !== "string" || !item.route.trim()) {
158
- add(`navItems[${i}].route`, "必须是非空字符串");
159
- continue;
160
- }
161
- if (!(item.route in routes)) {
162
- add(`navItems[${i}].route`, `"${item.route}" 不在 routes 里(侧栏点了会跳到进不去的页)`);
163
- }
164
- if (typeof item.label !== "string" || !item.label.trim()) {
165
- add(`navItems[${i}].label`, "必须是非空字符串(侧栏显示它)");
166
- }
167
- }
168
- const dup = navRoutes.filter((r, i) => navRoutes.indexOf(r) !== i);
169
- if (dup.length) {
170
- add("navItems", `有重复路由:${[...new Set(dup)].join("、")}(同一项出现两次,侧栏会重复)`);
171
- }
172
- } catch (err) {
173
- add("navItems", `调用抛错:${String((err as Error)?.message ?? err)}`);
174
- }
175
-
176
- // ⑦ guard:受限路由弹回的目标。返回值必须是合法路由,否则人被弹到一个进不去的页。
177
- // core 会对**每个路由**都问一次 guard,所以同一个坏目标会重复出现——按
178
- // 「目标」归并成一条,把触发它的路由列出来(否则一个 guard 写错能刷十几行,
179
- // 真正要看的信息被淹掉)。
180
- if (def.guard) {
181
- const badTargets = new Map<string, string[]>();
182
- for (const r of keys) {
183
- try {
184
- const target = def.guard(r, ctx);
185
- if (target !== null && target !== undefined && (typeof target !== "string" || !(target in routes))) {
186
- const key = String(target);
187
- const from = badTargets.get(key);
188
- if (from) from.push(r);
189
- else badTargets.set(key, [r]);
190
- }
191
- } catch (err) {
192
- add(`guard("${r}")`, `调用抛错:${String((err as Error)?.message ?? err)}`);
193
- }
194
- }
195
- for (const [target, from] of badTargets) {
196
- add(
197
- "guard",
198
- `弹回目标 "${target}" 不在 routes 里(人会停在一个进不去的 hash;触发它的路由:${from.join("、")})`,
199
- );
200
- }
201
- }
202
-
203
- // ⑧ page:页面渲染。缺了就没有任何页面能渲染出来。
204
- if (typeof def.page !== "function") {
205
- add("page", "必填(core 用它渲染页面内容)");
206
- }
207
-
208
- // ⑨ 侧栏可达性**不在这里查**:`navItems(ctx)` 是**动态**的(依赖当前选项的
209
- // 科目、是否 admin、授权闸是否启用),同一个路由在某一刻不在侧栏是完全
210
- // 正常的(<product> 的 case/essay 由科目驱动、admin/personas 只给 admin)。
211
- // 静态查「routes 里的路由是否都在侧栏」必然误判。
212
- // 反方向(侧栏项必须在 routes 里)才是硬要求,已在 ⑥ 查。
213
-
214
- return problems;
215
- }
1
+ /**
2
+ * 接缝:core 对 `AppDef` 的**结构契约**校验器。
3
+ *
4
+ * 为什么要有这个:core 的 `useAppHost` 从 def 里读七八个字段,每个字段都带着
5
+ * 一条**没说出口**的要求(写错了不报错,只是某个路由失灵)。第二接入方照抄
6
+ * `App.tsx` 时无从知道这些——只能靠「跑起来看侧栏少了一项」。
7
+ *
8
+ * 这里把隐含要求写成显式的校验器:产品写完 def 跑一遍,报错就是问题。
9
+ * core 不 import 任何产品(边界守卫守着),校验器只认结构。
10
+ */
11
+ import type { AppDef, HostCtx } from "./app.tsx";
12
+
13
+ export interface DefProblem {
14
+ /** 出问题的位置(字段名或路由键)。 */
15
+ where: string;
16
+ /** 人话说明 + 怎么修。 */
17
+ message: string;
18
+ }
19
+
20
+ /**
21
+ * 路由键的合法形状。
22
+ *
23
+ * core 的 `routeFromHash` 用 `/^#\/([a-z]+)/` 从 hash 里取路由键——**只认小写
24
+ * 字母**。所以 "my-page"、"q1"、"记录" 这类键在 hash 里取不出来,会被静默
25
+ * 打回 chatRoute(页面看着像「点不动」)。这条以前只有 core 源码里那个正则
26
+ * 说过,现在写进契约里。
27
+ */
28
+ export const ROUTE_KEY_RE = /^[a-z]+$/;
29
+
30
+ /** rewrites 的值可以带 query(`record?tab=wrong`),取路径部分来判合法性。 */
31
+ function routePathOf(target: string): string {
32
+ return target.split("?")[0]!;
33
+ }
34
+
35
+ /**
36
+ * 校验用的**标准上下文**(构建期与测试共用,避免两处各造一份漂移)。
37
+ *
38
+ * 为什么要好几个:`navItems` 与 `guard` 是**动态**的——侧栏条目随「有没有选
39
+ * 选项、这个选项有哪些科目、是不是 admin、审计有没有被取消」而变。只用一个
40
+ * 上下文校验,等于只测了其中一条分支;其余分支写错了照样发版。
41
+ *
42
+ * 四份覆盖了所有会改变导航的条件组合:未登录 / 普通用户单科 / 普通用户三科 /
43
+ * 管理员三科。第二接入方如果有更多维度(比如按会员等级显示入口),自己再补。
44
+ */
45
+ export function sampleHostContexts(): Array<{ name: string; ctx: HostCtx }> {
46
+ const branding = {
47
+ title: "示例产品",
48
+ defaultPersona: "default",
49
+ personas: { default: { label: "助手" } },
50
+ } as unknown as HostCtx["branding"];
51
+ const qual = (subjects: string[]) =>
52
+ ({ id: "demo", level: "senior", label: "示例选项", subjects }) as unknown as HostCtx["qual"];
53
+ const who = (roles: string[], extra: Record<string, unknown> = {}) =>
54
+ ({
55
+ userId: "u1",
56
+ roles,
57
+ authzEnabled: true,
58
+ memberUntil: null,
59
+ bannedUntil: null,
60
+ auditRevoked: false,
61
+ quota: { runsToday: 0, runCap: null, tokensToday: 0, tokenCap: null },
62
+ ...extra,
63
+ }) as unknown as HostCtx["who"];
64
+ const base = {
65
+ branding,
66
+ route: "",
67
+ persona: "",
68
+ onQuals: false,
69
+ go: () => {},
70
+ pickQual: () => {},
71
+ };
72
+ return [
73
+ { name: "未登录", ctx: { ...base, who: null, qual: null } as HostCtx },
74
+ { name: "普通用户·单科", ctx: { ...base, who: who(["user"]), qual: qual(["mcq"]) } as HostCtx },
75
+ { name: "普通用户·三科", ctx: { ...base, who: who(["user"]), qual: qual(["mcq", "case", "essay"]) } as HostCtx },
76
+ { name: "管理员·三科", ctx: { ...base, who: who(["admin"]), qual: qual(["mcq", "case", "essay"]) } as HostCtx },
77
+ // 审计被取消是一条独立分支:navItems 会隐去审计入口,guard 会把它弹回。
78
+ { name: "普通用户·审计被取消", ctx: { ...base, who: who(["user"], { auditRevoked: true }), qual: qual(["mcq"]) } as HostCtx },
79
+ ];
80
+ }
81
+
82
+ /**
83
+ * 校验一份 AppDef 是否满足 core 的要求。
84
+ *
85
+ * 返回**空数组** = 没问题。每条问题都指明位置与怎么修。
86
+ *
87
+ * 注意 `ctx` 是必需的:`navItems` 与 `guard` 都要 ctx 才能算出结果(它们依赖
88
+ * 当前选项与登录身份),而这两个字段的错误恰好是最难自查的(侧栏少一项 /
89
+ * 守卫把人弹到不存在的页)。
90
+ */
91
+ export function validateAppDef(def: AppDef, ctx: HostCtx): DefProblem[] {
92
+ const problems: DefProblem[] = [];
93
+ const add = (where: string, message: string) => problems.push({ where, message });
94
+
95
+ // ① routes:合法 hash 路由的键集。core 认不出的键 = 一个进不去的页面。
96
+ const routes = def.routes;
97
+ if (!routes || typeof routes !== "object" || Array.isArray(routes)) {
98
+ add("routes", "必须是对象(路由键 → 导航文案)");
99
+ return problems; // 后面几项都依赖它,缺了就别报噪音了
100
+ }
101
+ const keys = Object.keys(routes);
102
+ if (keys.length === 0) {
103
+ add("routes", "不能为空——core 用它认合法 hash 路由,空表等于所有链接都打回 chatRoute");
104
+ }
105
+ for (const k of keys) {
106
+ if (!ROUTE_KEY_RE.test(k)) {
107
+ add(`routes.${k}`, "路由键只能是小写字母(core 用 /^#\\/([a-z]+)/ 取键,含数字/连字符/中文会取不出来)");
108
+ }
109
+ if (typeof routes[k] !== "string" || !routes[k]!.trim()) {
110
+ add(`routes.${k}`, "导航文案必须是非空字符串(侧栏会显示它)");
111
+ }
112
+ }
113
+
114
+ // ② chatRoute:聊天坞的路由键。它在 routes 里才谈得上「有文案、有高亮」。
115
+ // 两条判据各管一种情况:空值(必填没填)vs 非空但不在路由表里。
116
+ if (!def.chatRoute) {
117
+ add("chatRoute", "必填(core 用它兜底:认不出的 hash 路由都回落到它)");
118
+ } else if (!(def.chatRoute in routes)) {
119
+ add("chatRoute", `"${def.chatRoute}" 不在 routes 里(core 认不出这个路由键,且侧栏没有它的文案)`);
120
+ }
121
+
122
+ // ③ home:收起聊天坞时退回的业务页。必须是合法路由,否则退回后又被打回 chatRoute。
123
+ if (!def.home) {
124
+ add("home", "必填(core 用它作为「最后一个业务页」的初值)");
125
+ } else if (!(def.home in routes)) {
126
+ add("home", `"${def.home}" 不在 routes 里(core 退回这个页时会被打回 chatRoute)`);
127
+ }
128
+
129
+ // ④ rewrites:老链接就地改写。**键是老链接**(可能早就不在 routes 里了——
130
+ // 那正是要改写的原因),只有目标必须是合法路由。
131
+ for (const [from, to] of Object.entries(def.rewrites ?? {})) {
132
+ if (typeof to !== "string" || !to.trim()) {
133
+ add(`rewrites.${from}`, "改写目标必须是非空字符串");
134
+ continue;
135
+ }
136
+ const p = routePathOf(to);
137
+ if (!(p in routes)) {
138
+ add(`rewrites.${from}`, `改写目标 "${to}" 的路由 "${p}" 不在 routes 里(改写过去就是个进不去的页)`);
139
+ }
140
+ }
141
+
142
+ // ⑤ navAlias:高亮归属。值必须是合法路由,否则高亮无处可落(那一项永远不亮)。
143
+ for (const [from, to] of Object.entries(def.navAlias ?? {})) {
144
+ if (!(from in routes)) {
145
+ add(`navAlias.${from}`, "高亮归属的键不在 routes 里");
146
+ }
147
+ if (typeof to !== "string" || !(to in routes)) {
148
+ add(`navAlias.${from}`, `高亮归属目标 "${to}" 不在 routes 里(侧栏那一项永远不高亮)`);
149
+ }
150
+ }
151
+
152
+ // ⑥ navItems:侧栏条目。每项的 route 必须有文案(core 直接渲染 label)。
153
+ try {
154
+ const items = def.navItems(ctx);
155
+ const navRoutes = items.map((i) => i.route);
156
+ for (const [i, item] of items.entries()) {
157
+ if (!item || typeof item.route !== "string" || !item.route.trim()) {
158
+ add(`navItems[${i}].route`, "必须是非空字符串");
159
+ continue;
160
+ }
161
+ if (!(item.route in routes)) {
162
+ add(`navItems[${i}].route`, `"${item.route}" 不在 routes 里(侧栏点了会跳到进不去的页)`);
163
+ }
164
+ if (typeof item.label !== "string" || !item.label.trim()) {
165
+ add(`navItems[${i}].label`, "必须是非空字符串(侧栏显示它)");
166
+ }
167
+ }
168
+ const dup = navRoutes.filter((r, i) => navRoutes.indexOf(r) !== i);
169
+ if (dup.length) {
170
+ add("navItems", `有重复路由:${[...new Set(dup)].join("、")}(同一项出现两次,侧栏会重复)`);
171
+ }
172
+ } catch (err) {
173
+ add("navItems", `调用抛错:${String((err as Error)?.message ?? err)}`);
174
+ }
175
+
176
+ // ⑦ guard:受限路由弹回的目标。返回值必须是合法路由,否则人被弹到一个进不去的页。
177
+ // core 会对**每个路由**都问一次 guard,所以同一个坏目标会重复出现——按
178
+ // 「目标」归并成一条,把触发它的路由列出来(否则一个 guard 写错能刷十几行,
179
+ // 真正要看的信息被淹掉)。
180
+ if (def.guard) {
181
+ const badTargets = new Map<string, string[]>();
182
+ for (const r of keys) {
183
+ try {
184
+ const target = def.guard(r, ctx);
185
+ if (target !== null && target !== undefined && (typeof target !== "string" || !(target in routes))) {
186
+ const key = String(target);
187
+ const from = badTargets.get(key);
188
+ if (from) from.push(r);
189
+ else badTargets.set(key, [r]);
190
+ }
191
+ } catch (err) {
192
+ add(`guard("${r}")`, `调用抛错:${String((err as Error)?.message ?? err)}`);
193
+ }
194
+ }
195
+ for (const [target, from] of badTargets) {
196
+ add(
197
+ "guard",
198
+ `弹回目标 "${target}" 不在 routes 里(人会停在一个进不去的 hash;触发它的路由:${from.join("、")})`,
199
+ );
200
+ }
201
+ }
202
+
203
+ // ⑧ page:页面渲染。缺了就没有任何页面能渲染出来。
204
+ if (typeof def.page !== "function") {
205
+ add("page", "必填(core 用它渲染页面内容)");
206
+ }
207
+
208
+ // ⑨ 侧栏可达性**不在这里查**:`navItems(ctx)` 是**动态**的(依赖当前选项的
209
+ // 科目、是否 admin、授权闸是否启用),同一个路由在某一刻不在侧栏是完全
210
+ // 正常的(<product> 的 case/essay 由科目驱动、admin/personas 只给 admin)。
211
+ // 静态查「routes 里的路由是否都在侧栏」必然误判。
212
+ // 反方向(侧栏项必须在 routes 里)才是硬要求,已在 ⑥ 查。
213
+
214
+ return problems;
215
+ }