@njuptlzf/agent-chat-web 0.2.0 → 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.
Files changed (61) hide show
  1. package/dist/AgentChat.d.ts +7 -1
  2. package/dist/AgentChat.js +35 -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/adapter.d.ts +9 -0
  7. package/dist/adapter.js +15 -0
  8. package/dist/mount.d.ts +7 -1
  9. package/dist/mount.js +2 -2
  10. package/dist/styles.css +1505 -1478
  11. package/dist/suggestions.d.ts +41 -0
  12. package/dist/suggestions.js +98 -0
  13. package/dist/ui/FollowUpSuggestions.d.ts +9 -0
  14. package/dist/ui/FollowUpSuggestions.js +13 -0
  15. package/dist/ui/Icon.d.ts +8 -0
  16. package/dist/ui/Icon.js +26 -0
  17. package/dist/ui/MemberCards.d.ts +15 -0
  18. package/dist/ui/MemberCards.js +13 -0
  19. package/dist/ui/icons.d.ts +19 -0
  20. package/dist/ui/icons.js +25 -0
  21. package/dist/ui/members.d.ts +42 -0
  22. package/dist/ui/members.js +53 -0
  23. package/dist/ui/slots.d.ts +29 -0
  24. package/dist/ui/slots.js +14 -0
  25. package/package.json +71 -72
  26. package/src/App.tsx +13 -13
  27. package/src/agent-chat/AgentChat.tsx +741 -692
  28. package/src/agent-chat/AgentChatApp.tsx +211 -206
  29. package/src/agent-chat/DockPanel.tsx +73 -73
  30. package/src/agent-chat/PersonasPage.tsx +705 -678
  31. package/src/agent-chat/adapter.ts +293 -272
  32. package/src/agent-chat/app-def-check.ts +215 -215
  33. package/src/agent-chat/app.tsx +327 -327
  34. package/src/agent-chat/auth.ts +83 -83
  35. package/src/agent-chat/dock.ts +161 -161
  36. package/src/agent-chat/docs.ts +179 -179
  37. package/src/agent-chat/index.ts +17 -17
  38. package/src/agent-chat/mount.tsx +130 -123
  39. package/src/agent-chat/qual.ts +55 -55
  40. package/src/agent-chat/sse.ts +104 -104
  41. package/src/agent-chat/suggestions.ts +127 -0
  42. package/src/agent-chat/theme.ts +23 -23
  43. package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -29
  44. package/src/agent-chat/ui/ConfirmCard.tsx +32 -32
  45. package/src/agent-chat/ui/FollowUpSuggestions.tsx +28 -0
  46. package/src/agent-chat/ui/Icon.tsx +53 -0
  47. package/src/agent-chat/ui/Lightbox.tsx +32 -32
  48. package/src/agent-chat/ui/MemberCards.tsx +46 -0
  49. package/src/agent-chat/ui/QueueBar.tsx +70 -70
  50. package/src/agent-chat/ui/Splitter.tsx +81 -81
  51. package/src/agent-chat/ui/ThemeToggle.tsx +25 -25
  52. package/src/agent-chat/ui/TierPicker.tsx +112 -112
  53. package/src/agent-chat/ui/UIPromptCard.tsx +68 -68
  54. package/src/agent-chat/ui/icons.ts +31 -0
  55. package/src/agent-chat/ui/members.ts +106 -0
  56. package/src/agent-chat/ui/slots.tsx +41 -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 +1505 -1478
@@ -1,81 +1,81 @@
1
- import { useCallback, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
2
-
3
- function clamp(v: number, min: number, max: number): number {
4
- return Math.min(max, Math.max(min, v));
5
- }
6
-
7
- /**
8
- * 解析左栏宽度:记忆值合法才用,否则回落默认。
9
- * 损坏的 localStorage(NaN/越界/改参后旧值)一律回默认,不会把布局带坏。
10
- */
11
- export function resolveWidth(stored: string | null, fallback: number, min: number, max: number): number {
12
- const v = Number(stored);
13
- return stored !== null && stored !== "" && Number.isFinite(v) && v >= min && v <= max ? v : fallback;
14
- }
15
-
16
- /**
17
- * 可拖拽竖向分隔条(agentChat 通用能力,零业务知识):拖动调整左栏宽度,
18
- * 宽度记忆在 localStorage(每浏览器),双击恢复默认。参考 DSH 侧栏拖拽。
19
- */
20
- export function usePaneWidth(storageKey: string, fallback: number, min: number, max: number) {
21
- const [width, setWidth] = useState(() => {
22
- let stored: string | null = null;
23
- try {
24
- stored = localStorage.getItem(storageKey);
25
- } catch {
26
- /* 隐私模式读不到就算了 */
27
- }
28
- return resolveWidth(stored, fallback, min, max);
29
- });
30
- const drag = useRef<{ startX: number; startW: number } | null>(null);
31
-
32
- const commit = useCallback(
33
- (v: number) => {
34
- const w = clamp(v, min, max);
35
- setWidth(w);
36
- try {
37
- localStorage.setItem(storageKey, String(w));
38
- } catch {
39
- /* 写不进去只影响"记忆",不影响本次拖拽 */
40
- }
41
- },
42
- [storageKey, min, max],
43
- );
44
-
45
- return {
46
- width,
47
- handlers: {
48
- onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
49
- e.currentTarget.setPointerCapture(e.pointerId);
50
- drag.current = { startX: e.clientX, startW: width };
51
- },
52
- onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
53
- const d = drag.current;
54
- if (d) commit(d.startW + e.clientX - d.startX);
55
- },
56
- onPointerUp: () => {
57
- drag.current = null;
58
- },
59
- onDoubleClick: () => commit(fallback),
60
- },
61
- };
62
- }
63
-
64
- interface SplitterProps {
65
- onPointerDown?: (e: ReactPointerEvent<HTMLDivElement>) => void;
66
- onPointerMove?: (e: ReactPointerEvent<HTMLDivElement>) => void;
67
- onPointerUp?: (e: ReactPointerEvent<HTMLDivElement>) => void;
68
- onDoubleClick?: () => void;
69
- }
70
-
71
- export function Splitter(props: SplitterProps) {
72
- return (
73
- <div
74
- className="splitter"
75
- role="separator"
76
- aria-orientation="vertical"
77
- title="拖动调整宽度,双击恢复默认"
78
- {...props}
79
- />
80
- );
81
- }
1
+ import { useCallback, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
2
+
3
+ function clamp(v: number, min: number, max: number): number {
4
+ return Math.min(max, Math.max(min, v));
5
+ }
6
+
7
+ /**
8
+ * 解析左栏宽度:记忆值合法才用,否则回落默认。
9
+ * 损坏的 localStorage(NaN/越界/改参后旧值)一律回默认,不会把布局带坏。
10
+ */
11
+ export function resolveWidth(stored: string | null, fallback: number, min: number, max: number): number {
12
+ const v = Number(stored);
13
+ return stored !== null && stored !== "" && Number.isFinite(v) && v >= min && v <= max ? v : fallback;
14
+ }
15
+
16
+ /**
17
+ * 可拖拽竖向分隔条(agentChat 通用能力,零业务知识):拖动调整左栏宽度,
18
+ * 宽度记忆在 localStorage(每浏览器),双击恢复默认。参考 DSH 侧栏拖拽。
19
+ */
20
+ export function usePaneWidth(storageKey: string, fallback: number, min: number, max: number) {
21
+ const [width, setWidth] = useState(() => {
22
+ let stored: string | null = null;
23
+ try {
24
+ stored = localStorage.getItem(storageKey);
25
+ } catch {
26
+ /* 隐私模式读不到就算了 */
27
+ }
28
+ return resolveWidth(stored, fallback, min, max);
29
+ });
30
+ const drag = useRef<{ startX: number; startW: number } | null>(null);
31
+
32
+ const commit = useCallback(
33
+ (v: number) => {
34
+ const w = clamp(v, min, max);
35
+ setWidth(w);
36
+ try {
37
+ localStorage.setItem(storageKey, String(w));
38
+ } catch {
39
+ /* 写不进去只影响"记忆",不影响本次拖拽 */
40
+ }
41
+ },
42
+ [storageKey, min, max],
43
+ );
44
+
45
+ return {
46
+ width,
47
+ handlers: {
48
+ onPointerDown: (e: ReactPointerEvent<HTMLDivElement>) => {
49
+ e.currentTarget.setPointerCapture(e.pointerId);
50
+ drag.current = { startX: e.clientX, startW: width };
51
+ },
52
+ onPointerMove: (e: ReactPointerEvent<HTMLDivElement>) => {
53
+ const d = drag.current;
54
+ if (d) commit(d.startW + e.clientX - d.startX);
55
+ },
56
+ onPointerUp: () => {
57
+ drag.current = null;
58
+ },
59
+ onDoubleClick: () => commit(fallback),
60
+ },
61
+ };
62
+ }
63
+
64
+ interface SplitterProps {
65
+ onPointerDown?: (e: ReactPointerEvent<HTMLDivElement>) => void;
66
+ onPointerMove?: (e: ReactPointerEvent<HTMLDivElement>) => void;
67
+ onPointerUp?: (e: ReactPointerEvent<HTMLDivElement>) => void;
68
+ onDoubleClick?: () => void;
69
+ }
70
+
71
+ export function Splitter(props: SplitterProps) {
72
+ return (
73
+ <div
74
+ className="splitter"
75
+ role="separator"
76
+ aria-orientation="vertical"
77
+ title="拖动调整宽度,双击恢复默认"
78
+ {...props}
79
+ />
80
+ );
81
+ }
@@ -1,25 +1,25 @@
1
- import { useState } from "react";
2
- import { applyThemeMode, loadThemeMode, saveThemeMode, type ThemeMode } from "../theme.ts";
3
-
4
- const ICON: Record<ThemeMode, string> = { auto: "🌗", light: "☀️", dark: "🌙" };
5
- const LABEL: Record<ThemeMode, string> = { auto: "跟随系统", light: "浅色", dark: "深色" };
6
- const CYCLE: Record<ThemeMode, ThemeMode> = { auto: "light", light: "dark", dark: "auto" };
7
-
8
- /** 三态循环:跟随系统 → 浅色 → 深色。自包含:状态、持久化、应用都在这里。 */
9
- export default function ThemeToggle() {
10
- const [mode, setMode] = useState<ThemeMode>(loadThemeMode);
11
- return (
12
- <button
13
- className="theme-toggle"
14
- title={`主题:${LABEL[mode]}(点击切换)`}
15
- onClick={() => {
16
- const next = CYCLE[mode];
17
- setMode(next);
18
- applyThemeMode(next);
19
- saveThemeMode(next);
20
- }}
21
- >
22
- {ICON[mode]}
23
- </button>
24
- );
25
- }
1
+ import { useState } from "react";
2
+ import { applyThemeMode, loadThemeMode, saveThemeMode, type ThemeMode } from "../theme.ts";
3
+
4
+ const ICON: Record<ThemeMode, string> = { auto: "🌗", light: "☀️", dark: "🌙" };
5
+ const LABEL: Record<ThemeMode, string> = { auto: "跟随系统", light: "浅色", dark: "深色" };
6
+ const CYCLE: Record<ThemeMode, ThemeMode> = { auto: "light", light: "dark", dark: "auto" };
7
+
8
+ /** 三态循环:跟随系统 → 浅色 → 深色。自包含:状态、持久化、应用都在这里。 */
9
+ export default function ThemeToggle() {
10
+ const [mode, setMode] = useState<ThemeMode>(loadThemeMode);
11
+ return (
12
+ <button
13
+ className="theme-toggle"
14
+ title={`主题:${LABEL[mode]}(点击切换)`}
15
+ onClick={() => {
16
+ const next = CYCLE[mode];
17
+ setMode(next);
18
+ applyThemeMode(next);
19
+ saveThemeMode(next);
20
+ }}
21
+ >
22
+ {ICON[mode]}
23
+ </button>
24
+ );
25
+ }
@@ -1,112 +1,112 @@
1
- import { useEffect, useRef, useState } from "react";
2
- import type { ModelTier } from "@njuptlzf/agent-chat-protocol";
3
-
4
- /**
5
- * 档位下拉选择器(U1b)。assistant-ui 0.15.28 没有通用下拉/菜单原语
6
- * (index.d.ts 只有 thread/composer 原语与 unstable trigger-popover 钩子),
7
- * 按规范自定义。样式对齐宿主截图:触发钮只显示当前档位,弹出列表每行
8
- * label + 右对齐消耗倍率,顶部结构化描述卡(名称/说明/倍率/单价)。
9
- * ponytail: 只做点击+Esc+方向键,无焦点陷阱/滚动锁定;升级路径 = 换无头
10
- * 菜单库(如 radix popover)。
11
- */
12
- export function TierPicker({
13
- tiers,
14
- value,
15
- onChange,
16
- }: {
17
- tiers: ModelTier[];
18
- value?: string;
19
- onChange: (id: string) => void;
20
- }) {
21
- const [open, setOpen] = useState(false);
22
- const [hover, setHover] = useState<number | null>(null);
23
- const rootRef = useRef<HTMLDivElement | null>(null);
24
-
25
- const current = tiers.find((t) => t.id === value) ?? tiers[0];
26
- const shown = tiers[hover ?? tiers.indexOf(current)] ?? current;
27
-
28
- useEffect(() => {
29
- if (!open) return;
30
- const onDoc = (e: MouseEvent) => {
31
- if (!rootRef.current?.contains(e.target as Node)) setOpen(false);
32
- };
33
- document.addEventListener("mousedown", onDoc);
34
- return () => document.removeEventListener("mousedown", onDoc);
35
- }, [open]);
36
-
37
- const pick = (t: ModelTier) => {
38
- onChange(t.id);
39
- setOpen(false);
40
- setHover(null);
41
- };
42
-
43
- return (
44
- <div
45
- className="tierpick"
46
- ref={rootRef}
47
- onKeyDown={(e) => {
48
- if (e.key === "Escape") {
49
- setOpen(false);
50
- setHover(null);
51
- } else if (open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
52
- e.preventDefault();
53
- const base = hover ?? tiers.indexOf(current);
54
- setHover(e.key === "ArrowDown" ? Math.min(tiers.length - 1, base + 1) : Math.max(0, base - 1));
55
- } else if (open && e.key === "Enter" && hover !== null) {
56
- e.preventDefault();
57
- const t = tiers[hover];
58
- if (t) pick(t);
59
- }
60
- }}
61
- >
62
- {open && (
63
- <div className="tier-pop" role="listbox" aria-label="回答档位">
64
- <div className="tier-desc">
65
- <div className="tier-desc-name">{shown.label}</div>
66
- {shown.description && <div className="tier-desc-text">{shown.description}</div>}
67
- <div className="tier-desc-meta">
68
- <span>消耗倍率</span>
69
- <span>×{shown.multiplier}</span>
70
- </div>
71
- {shown.pricePerThousandTokensCents !== undefined && (
72
- <div className="tier-desc-meta">
73
- <span>单价</span>
74
- <span>{shown.pricePerThousandTokensCents} 分/千字</span>
75
- </div>
76
- )}
77
- </div>
78
- {tiers.map((t, i) => (
79
- <button
80
- type="button"
81
- key={t.id}
82
- role="option"
83
- aria-selected={t.id === current.id}
84
- className={t.id === current.id ? "tier-row on" : "tier-row"}
85
- onMouseEnter={() => setHover(i)}
86
- onClick={() => pick(t)}
87
- >
88
- <span>{t.label}</span>
89
- <span className="tier-mult">×{t.multiplier}</span>
90
- </button>
91
- ))}
92
- </div>
93
- )}
94
- <button
95
- type="button"
96
- className="tier-btn"
97
- aria-haspopup="listbox"
98
- aria-expanded={open}
99
- onKeyDown={(e) => {
100
- if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
101
- e.preventDefault();
102
- setOpen(true);
103
- }
104
- }}
105
- onClick={() => setOpen((o) => !o)}
106
- >
107
- {current.label}
108
- <span className="tier-caret">▾</span>
109
- </button>
110
- </div>
111
- );
112
- }
1
+ import { useEffect, useRef, useState } from "react";
2
+ import type { ModelTier } from "@njuptlzf/agent-chat-protocol";
3
+
4
+ /**
5
+ * 档位下拉选择器(U1b)。assistant-ui 0.15.28 没有通用下拉/菜单原语
6
+ * (index.d.ts 只有 thread/composer 原语与 unstable trigger-popover 钩子),
7
+ * 按规范自定义。样式对齐宿主截图:触发钮只显示当前档位,弹出列表每行
8
+ * label + 右对齐消耗倍率,顶部结构化描述卡(名称/说明/倍率/单价)。
9
+ * ponytail: 只做点击+Esc+方向键,无焦点陷阱/滚动锁定;升级路径 = 换无头
10
+ * 菜单库(如 radix popover)。
11
+ */
12
+ export function TierPicker({
13
+ tiers,
14
+ value,
15
+ onChange,
16
+ }: {
17
+ tiers: ModelTier[];
18
+ value?: string;
19
+ onChange: (id: string) => void;
20
+ }) {
21
+ const [open, setOpen] = useState(false);
22
+ const [hover, setHover] = useState<number | null>(null);
23
+ const rootRef = useRef<HTMLDivElement | null>(null);
24
+
25
+ const current = tiers.find((t) => t.id === value) ?? tiers[0];
26
+ const shown = tiers[hover ?? tiers.indexOf(current)] ?? current;
27
+
28
+ useEffect(() => {
29
+ if (!open) return;
30
+ const onDoc = (e: MouseEvent) => {
31
+ if (!rootRef.current?.contains(e.target as Node)) setOpen(false);
32
+ };
33
+ document.addEventListener("mousedown", onDoc);
34
+ return () => document.removeEventListener("mousedown", onDoc);
35
+ }, [open]);
36
+
37
+ const pick = (t: ModelTier) => {
38
+ onChange(t.id);
39
+ setOpen(false);
40
+ setHover(null);
41
+ };
42
+
43
+ return (
44
+ <div
45
+ className="tierpick"
46
+ ref={rootRef}
47
+ onKeyDown={(e) => {
48
+ if (e.key === "Escape") {
49
+ setOpen(false);
50
+ setHover(null);
51
+ } else if (open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
52
+ e.preventDefault();
53
+ const base = hover ?? tiers.indexOf(current);
54
+ setHover(e.key === "ArrowDown" ? Math.min(tiers.length - 1, base + 1) : Math.max(0, base - 1));
55
+ } else if (open && e.key === "Enter" && hover !== null) {
56
+ e.preventDefault();
57
+ const t = tiers[hover];
58
+ if (t) pick(t);
59
+ }
60
+ }}
61
+ >
62
+ {open && (
63
+ <div className="tier-pop" role="listbox" aria-label="回答档位">
64
+ <div className="tier-desc">
65
+ <div className="tier-desc-name">{shown.label}</div>
66
+ {shown.description && <div className="tier-desc-text">{shown.description}</div>}
67
+ <div className="tier-desc-meta">
68
+ <span>消耗倍率</span>
69
+ <span>×{shown.multiplier}</span>
70
+ </div>
71
+ {shown.pricePerThousandTokensCents !== undefined && (
72
+ <div className="tier-desc-meta">
73
+ <span>单价</span>
74
+ <span>{shown.pricePerThousandTokensCents} 分/千字</span>
75
+ </div>
76
+ )}
77
+ </div>
78
+ {tiers.map((t, i) => (
79
+ <button
80
+ type="button"
81
+ key={t.id}
82
+ role="option"
83
+ aria-selected={t.id === current.id}
84
+ className={t.id === current.id ? "tier-row on" : "tier-row"}
85
+ onMouseEnter={() => setHover(i)}
86
+ onClick={() => pick(t)}
87
+ >
88
+ <span>{t.label}</span>
89
+ <span className="tier-mult">×{t.multiplier}</span>
90
+ </button>
91
+ ))}
92
+ </div>
93
+ )}
94
+ <button
95
+ type="button"
96
+ className="tier-btn"
97
+ aria-haspopup="listbox"
98
+ aria-expanded={open}
99
+ onKeyDown={(e) => {
100
+ if (!open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
101
+ e.preventDefault();
102
+ setOpen(true);
103
+ }
104
+ }}
105
+ onClick={() => setOpen((o) => !o)}
106
+ >
107
+ {current.label}
108
+ <span className="tier-caret">▾</span>
109
+ </button>
110
+ </div>
111
+ );
112
+ }
@@ -1,68 +1,68 @@
1
- import { useState } from "react";
2
- import type { UiPromptEvent } from "../adapter.ts";
3
-
4
- /**
5
- * Card for an extension dialog (`ctx.ui.select/confirm/input`, custom component
6
- * UI-R3 — assistant-ui has no equivalent primitive for extension UI prompts).
7
- * The server parks the run inside the extension's await until this answers
8
- * POST /api/ui-response, so every kind offers an explicit way out: `cancelled`
9
- * maps to the fail-closed default (false / undefined) if the user dismisses
10
- * instead of choosing.
11
- */
12
- export function UIPromptCard({
13
- prompt,
14
- onAnswer,
15
- }: {
16
- prompt: UiPromptEvent;
17
- onAnswer: (a: { value?: string; confirmed?: boolean; cancelled?: true }) => void;
18
- }) {
19
- const [text, setText] = useState("");
20
- const submitInput = () => {
21
- if (text.trim()) onAnswer({ value: text });
22
- };
23
- return (
24
- <div className="uidialog" role={prompt.kind === "confirm" ? "alertdialog" : "dialog"} aria-label={prompt.title}>
25
- <div className="uidialog-title">{prompt.title}</div>
26
- {prompt.message && <div className="uidialog-msg">{prompt.message}</div>}
27
- {prompt.kind === "select" && (
28
- <div className="uidialog-options">
29
- {(prompt.options ?? []).map((o) => (
30
- <button key={o} onClick={() => onAnswer({ value: o })}>
31
- {o}
32
- </button>
33
- ))}
34
- <button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
35
- 取消
36
- </button>
37
- </div>
38
- )}
39
- {prompt.kind === "confirm" && (
40
- <div className="uidialog-actions">
41
- <button className="allow" onClick={() => onAnswer({ confirmed: true })}>
42
- 确定
43
- </button>
44
- <button className="deny" onClick={() => onAnswer({ cancelled: true })}>
45
- 取消
46
- </button>
47
- </div>
48
- )}
49
- {prompt.kind === "input" && (
50
- <div className="uidialog-input">
51
- <input
52
- value={text}
53
- placeholder={prompt.placeholder ?? ""}
54
- autoFocus
55
- onChange={(e) => setText(e.target.value)}
56
- onKeyDown={(e) => e.key === "Enter" && submitInput()}
57
- />
58
- <button onClick={submitInput} disabled={!text.trim()}>
59
- 提交
60
- </button>
61
- <button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
62
- 取消
63
- </button>
64
- </div>
65
- )}
66
- </div>
67
- );
68
- }
1
+ import { useState } from "react";
2
+ import type { UiPromptEvent } from "../adapter.ts";
3
+
4
+ /**
5
+ * Card for an extension dialog (`ctx.ui.select/confirm/input`, custom component
6
+ * UI-R3 — assistant-ui has no equivalent primitive for extension UI prompts).
7
+ * The server parks the run inside the extension's await until this answers
8
+ * POST /api/ui-response, so every kind offers an explicit way out: `cancelled`
9
+ * maps to the fail-closed default (false / undefined) if the user dismisses
10
+ * instead of choosing.
11
+ */
12
+ export function UIPromptCard({
13
+ prompt,
14
+ onAnswer,
15
+ }: {
16
+ prompt: UiPromptEvent;
17
+ onAnswer: (a: { value?: string; confirmed?: boolean; cancelled?: true }) => void;
18
+ }) {
19
+ const [text, setText] = useState("");
20
+ const submitInput = () => {
21
+ if (text.trim()) onAnswer({ value: text });
22
+ };
23
+ return (
24
+ <div className="uidialog" role={prompt.kind === "confirm" ? "alertdialog" : "dialog"} aria-label={prompt.title}>
25
+ <div className="uidialog-title">{prompt.title}</div>
26
+ {prompt.message && <div className="uidialog-msg">{prompt.message}</div>}
27
+ {prompt.kind === "select" && (
28
+ <div className="uidialog-options">
29
+ {(prompt.options ?? []).map((o) => (
30
+ <button key={o} onClick={() => onAnswer({ value: o })}>
31
+ {o}
32
+ </button>
33
+ ))}
34
+ <button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
35
+ 取消
36
+ </button>
37
+ </div>
38
+ )}
39
+ {prompt.kind === "confirm" && (
40
+ <div className="uidialog-actions">
41
+ <button className="allow" onClick={() => onAnswer({ confirmed: true })}>
42
+ 确定
43
+ </button>
44
+ <button className="deny" onClick={() => onAnswer({ cancelled: true })}>
45
+ 取消
46
+ </button>
47
+ </div>
48
+ )}
49
+ {prompt.kind === "input" && (
50
+ <div className="uidialog-input">
51
+ <input
52
+ value={text}
53
+ placeholder={prompt.placeholder ?? ""}
54
+ autoFocus
55
+ onChange={(e) => setText(e.target.value)}
56
+ onKeyDown={(e) => e.key === "Enter" && submitInput()}
57
+ />
58
+ <button onClick={submitInput} disabled={!text.trim()}>
59
+ 提交
60
+ </button>
61
+ <button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
62
+ 取消
63
+ </button>
64
+ </div>
65
+ )}
66
+ </div>
67
+ );
68
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * 图标值解析:`branding.ui.icons.<槽名>` 的一个字符串怎么画。
3
+ * 三种形态:`svg:<内置名>` / `url:https?://…`(图片地址)/ 其它非空字符串(emoji 或短文本)。
4
+ * 任何非法值都回落默认矢量——图标坏了不该让界面报错,也不该引入任何 HTML 注入面。
5
+ */
6
+ const BUILTIN_SVGS = ["paperclip", "plus", "image", "upload"] as const;
7
+ export type BuiltinSvgName = (typeof BUILTIN_SVGS)[number];
8
+
9
+ export type IconDescriptor =
10
+ | { kind: "svg"; name: BuiltinSvgName }
11
+ | { kind: "url"; href: string }
12
+ | { kind: "text"; text: string };
13
+
14
+ const DEFAULT_ICON: IconDescriptor = { kind: "svg", name: "paperclip" };
15
+ const MAX_TEXT = 8;
16
+
17
+ export function parseIconSpec(spec: string | undefined | null): IconDescriptor {
18
+ if (spec === undefined || spec === null) return DEFAULT_ICON;
19
+ const value = spec.trim();
20
+ if (!value) return DEFAULT_ICON;
21
+ if (value.startsWith("svg:")) {
22
+ const name = value.slice(4).trim() as BuiltinSvgName;
23
+ return (BUILTIN_SVGS as readonly string[]).includes(name) ? { kind: "svg", name } : DEFAULT_ICON;
24
+ }
25
+ if (value.startsWith("url:")) {
26
+ const href = value.slice(4).trim();
27
+ // 只放行 http(s);相对路径与其它协议一律回落默认(纵深防御,前端兜一道)。
28
+ return /^https?:\/\/\S+$/i.test(href) ? { kind: "url", href } : DEFAULT_ICON;
29
+ }
30
+ return { kind: "text", text: value.length > MAX_TEXT ? value.slice(0, MAX_TEXT) : value };
31
+ }