@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,70 +1,70 @@
1
- /**
2
- * Waiting-queue bar (custom component, UI-R3).
3
- *
4
- * Why not the official queue: `ComposerPrimitive.Queue` + `unstable_enableMessageQueue`
5
- * cover enqueue/remove, but the local runtime wires the queue driver WITHOUT a
6
- * `cancel` callback, so a steer-lane move degrades to "process next" instead of
7
- * interrupting the running answer; and `notifyCancelled` pauses queue advance
8
- * after a user-initiated stop, which kills our "stop then flush next" behavior.
9
- * Forensics: @assistant-ui/core local-thread-runtime-core.js:356-366,
10
- * message-queue.d.ts:13-30. Kept as a reusable custom component per the
11
- * official-first rule (it does not match).
12
- */
13
-
14
- /** A content part a queued send can carry: the composer only produces these three. */
15
- export type QueuedPart =
16
- | { type: "text"; text: string }
17
- | { type: "image"; image: string }
18
- | { type: "file"; filename?: string; mimeType: string; data: string };
19
-
20
- /**
21
- * One queued send (E2, documents since B4-A). It holds content parts rather
22
- * than a string because a message waiting behind the current answer may carry
23
- * images and converted documents too — the queue is only a local staging
24
- * area, and whatever sits here is appended to the thread verbatim once the run
25
- * ends.
26
- */
27
- export type QueuedMessage = readonly QueuedPart[];
28
-
29
- /** What the bar shows for one queued message (the payload itself is a parts array). */
30
- export function queuedPreview(message: QueuedMessage): { text: string; images: number; docs: number } {
31
- const text = message
32
- .filter((p): p is Extract<QueuedPart, { type: "text" }> => p.type === "text")
33
- .map((p) => p.text)
34
- .join("");
35
- const images = message.filter((p) => p.type === "image").length;
36
- const docs = message.filter((p) => p.type === "file").length;
37
- return { text: text.trim(), images, docs };
38
- }
39
-
40
- export function QueueBar({
41
- queue,
42
- onSteer,
43
- onDrop,
44
- }: {
45
- queue: readonly QueuedMessage[];
46
- onSteer: (i: number) => void;
47
- onDrop: (i: number) => void;
48
- }) {
49
- return (
50
- <div className="queuebar">
51
- <span className="q-label">等待 {queue.length} 条</span>
52
- {queue.map((q, i) => {
53
- const { text, images, docs } = queuedPreview(q);
54
- return (
55
- <span className="q-item" key={i} title={text || `${images} 图片 / ${docs} 文档`}>
56
- {images > 0 && <span className="q-img" aria-label={`${images} 张图片`}>🖼{images > 1 ? `×${images}` : ""}</span>}
57
- {docs > 0 && <span className="q-img" aria-label={`${docs} 个文档`}>📄{docs > 1 ? `×${docs}` : ""}</span>}
58
- <span className="q-text">{text}</span>
59
- <button onClick={() => onSteer(i)} title="中止当前回答,优先发送这条">
60
- 插队
61
- </button>
62
- <button onClick={() => onDrop(i)} title="从队列移除">
63
- 移除
64
- </button>
65
- </span>
66
- );
67
- })}
68
- </div>
69
- );
70
- }
1
+ /**
2
+ * Waiting-queue bar (custom component, UI-R3).
3
+ *
4
+ * Why not the official queue: `ComposerPrimitive.Queue` + `unstable_enableMessageQueue`
5
+ * cover enqueue/remove, but the local runtime wires the queue driver WITHOUT a
6
+ * `cancel` callback, so a steer-lane move degrades to "process next" instead of
7
+ * interrupting the running answer; and `notifyCancelled` pauses queue advance
8
+ * after a user-initiated stop, which kills our "stop then flush next" behavior.
9
+ * Forensics: @assistant-ui/core local-thread-runtime-core.js:356-366,
10
+ * message-queue.d.ts:13-30. Kept as a reusable custom component per the
11
+ * official-first rule (it does not match).
12
+ */
13
+
14
+ /** A content part a queued send can carry: the composer only produces these three. */
15
+ export type QueuedPart =
16
+ | { type: "text"; text: string }
17
+ | { type: "image"; image: string }
18
+ | { type: "file"; filename?: string; mimeType: string; data: string };
19
+
20
+ /**
21
+ * One queued send (E2, documents since B4-A). It holds content parts rather
22
+ * than a string because a message waiting behind the current answer may carry
23
+ * images and converted documents too — the queue is only a local staging
24
+ * area, and whatever sits here is appended to the thread verbatim once the run
25
+ * ends.
26
+ */
27
+ export type QueuedMessage = readonly QueuedPart[];
28
+
29
+ /** What the bar shows for one queued message (the payload itself is a parts array). */
30
+ export function queuedPreview(message: QueuedMessage): { text: string; images: number; docs: number } {
31
+ const text = message
32
+ .filter((p): p is Extract<QueuedPart, { type: "text" }> => p.type === "text")
33
+ .map((p) => p.text)
34
+ .join("");
35
+ const images = message.filter((p) => p.type === "image").length;
36
+ const docs = message.filter((p) => p.type === "file").length;
37
+ return { text: text.trim(), images, docs };
38
+ }
39
+
40
+ export function QueueBar({
41
+ queue,
42
+ onSteer,
43
+ onDrop,
44
+ }: {
45
+ queue: readonly QueuedMessage[];
46
+ onSteer: (i: number) => void;
47
+ onDrop: (i: number) => void;
48
+ }) {
49
+ return (
50
+ <div className="queuebar">
51
+ <span className="q-label">等待 {queue.length} 条</span>
52
+ {queue.map((q, i) => {
53
+ const { text, images, docs } = queuedPreview(q);
54
+ return (
55
+ <span className="q-item" key={i} title={text || `${images} 图片 / ${docs} 文档`}>
56
+ {images > 0 && <span className="q-img" aria-label={`${images} 张图片`}>🖼{images > 1 ? `×${images}` : ""}</span>}
57
+ {docs > 0 && <span className="q-img" aria-label={`${docs} 个文档`}>📄{docs > 1 ? `×${docs}` : ""}</span>}
58
+ <span className="q-text">{text}</span>
59
+ <button onClick={() => onSteer(i)} title="中止当前回答,优先发送这条">
60
+ 插队
61
+ </button>
62
+ <button onClick={() => onDrop(i)} title="从队列移除">
63
+ 移除
64
+ </button>
65
+ </span>
66
+ );
67
+ })}
68
+ </div>
69
+ );
70
+ }
@@ -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
+ }