@njuptlzf/agent-chat-web 0.1.0 → 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 (81) hide show
  1. package/dist/AgentChat.d.ts +28 -0
  2. package/dist/AgentChat.js +355 -0
  3. package/dist/AgentChatApp.d.ts +31 -0
  4. package/dist/AgentChatApp.js +90 -0
  5. package/dist/DockPanel.d.ts +22 -0
  6. package/dist/DockPanel.js +22 -0
  7. package/dist/PersonasPage.d.ts +1 -0
  8. package/dist/PersonasPage.js +318 -0
  9. package/dist/adapter.d.ts +52 -0
  10. package/dist/adapter.js +242 -0
  11. package/dist/app-def-check.d.ts +50 -0
  12. package/dist/app-def-check.js +184 -0
  13. package/dist/app.d.ts +112 -0
  14. package/dist/app.js +174 -0
  15. package/dist/auth.d.ts +41 -0
  16. package/dist/auth.js +86 -0
  17. package/dist/dock.d.ts +52 -0
  18. package/dist/dock.js +132 -0
  19. package/dist/docs.d.ts +48 -0
  20. package/dist/docs.js +178 -0
  21. package/dist/index.d.ts +17 -0
  22. package/dist/index.js +8 -0
  23. package/dist/mount.d.ts +50 -0
  24. package/dist/mount.js +38 -0
  25. package/dist/qual.d.ts +21 -0
  26. package/dist/qual.js +53 -0
  27. package/dist/sse.d.ts +14 -0
  28. package/dist/sse.js +99 -0
  29. package/dist/styles.css +1478 -0
  30. package/dist/theme.d.ts +10 -0
  31. package/dist/theme.js +14 -0
  32. package/dist/ui/CodeCopyHeader.d.ts +7 -0
  33. package/dist/ui/CodeCopyHeader.js +22 -0
  34. package/dist/ui/ConfirmCard.d.ts +15 -0
  35. package/dist/ui/ConfirmCard.js +9 -0
  36. package/dist/ui/Lightbox.d.ts +6 -0
  37. package/dist/ui/Lightbox.js +31 -0
  38. package/dist/ui/QueueBar.d.ts +44 -0
  39. package/dist/ui/QueueBar.js +17 -0
  40. package/dist/ui/Splitter.d.ts +27 -0
  41. package/dist/ui/Splitter.js +61 -0
  42. package/dist/ui/ThemeToggle.d.ts +2 -0
  43. package/dist/ui/ThemeToggle.js +16 -0
  44. package/dist/ui/TierPicker.d.ts +14 -0
  45. package/dist/ui/TierPicker.js +54 -0
  46. package/dist/ui/UIPromptCard.d.ts +17 -0
  47. package/dist/ui/UIPromptCard.js +18 -0
  48. package/dist/ui/timefmt.d.ts +35 -0
  49. package/dist/ui/timefmt.js +110 -0
  50. package/dist/ui/useNarrow.d.ts +7 -0
  51. package/dist/ui/useNarrow.js +15 -0
  52. package/package.json +72 -68
  53. package/src/App.tsx +13 -0
  54. package/src/agent-chat/AgentChat.tsx +692 -692
  55. package/src/agent-chat/AgentChatApp.tsx +206 -206
  56. package/src/agent-chat/{Dock.tsx → DockPanel.tsx} +73 -73
  57. package/src/agent-chat/PersonasPage.tsx +678 -678
  58. package/src/agent-chat/adapter.ts +272 -272
  59. package/src/agent-chat/app-def-check.ts +215 -215
  60. package/src/agent-chat/app.tsx +327 -327
  61. package/src/agent-chat/auth.ts +83 -83
  62. package/src/agent-chat/dock.ts +161 -161
  63. package/src/agent-chat/docs.ts +179 -179
  64. package/src/agent-chat/index.ts +17 -17
  65. package/src/agent-chat/mount.tsx +123 -123
  66. package/src/agent-chat/qual.ts +55 -55
  67. package/src/agent-chat/sse.ts +104 -104
  68. package/src/agent-chat/theme.ts +23 -23
  69. package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -29
  70. package/src/agent-chat/ui/ConfirmCard.tsx +32 -32
  71. package/src/agent-chat/ui/Lightbox.tsx +32 -32
  72. package/src/agent-chat/ui/QueueBar.tsx +70 -70
  73. package/src/agent-chat/ui/Splitter.tsx +81 -81
  74. package/src/agent-chat/ui/ThemeToggle.tsx +25 -25
  75. package/src/agent-chat/ui/TierPicker.tsx +112 -112
  76. package/src/agent-chat/ui/UIPromptCard.tsx +68 -68
  77. package/src/agent-chat/ui/timefmt.ts +110 -110
  78. package/src/agent-chat/ui/useNarrow.ts +20 -20
  79. package/src/fake-product/entry.tsx +54 -0
  80. package/src/main.tsx +8 -0
  81. package/src/styles.css +1478 -1478
@@ -1,55 +1,55 @@
1
-
2
- /**
3
- * 当前选项的本地偏好(从 <product>/qual.ts 拆入 core):
4
- * 产品可选项目录是通用协议形状,"记住上次选了哪一项"是壳机制不是产品事实。
5
- * 存储键保持 `ac.qual` 不变——老用户的偏好直接沿用。
6
- *
7
- * 产品侧只留具体选项的语义与路由映射。
8
- */
9
- const KEY = "ac.qual";
10
-
11
- function loadQual(): string | null {
12
- try {
13
- return localStorage.getItem(KEY);
14
- } catch {
15
- return null;
16
- }
17
- }
18
-
19
- export function saveQual(id: string): void {
20
- try {
21
- localStorage.setItem(KEY, id);
22
- } catch {
23
- // 隐私模式下 localStorage 可能抛错;选项只是偏好,丢了回选择页即可。
24
- }
25
- }
26
-
27
- /** 可用 = 内容已就绪(`enabled` 不为 false)。 */
28
- export function isEnabled(q: { enabled?: boolean }): boolean {
29
- return q.enabled !== false;
30
- }
31
-
32
- /** 当前选项:本地记忆优先,否则回落第一个可用的选项。 */
33
- export function currentQual<T extends { id: string; enabled?: boolean }>(quals: T[] | undefined): T | null {
34
- if (!quals || quals.length === 0) return null;
35
- const saved = loadQual();
36
- const hit = saved ? quals.find((q) => q.id === saved && isEnabled(q)) : undefined;
37
- return hit ?? quals.find(isEnabled) ?? null;
38
- }
39
-
40
- /**
41
- * 深链里的选项(`#/<业务页>?qual=<轨道>&id=…`)。
42
- *
43
- * 为什么需要:页面记住的选项(ac.qual)与会话的 `AC_SCOPE` 是两件事——助手在
44
- * system-integration 的会话里给了一张题卡,点进去时页面可能还停在 architect,
45
- * 结果 404。深链把选项带上,壳静默切过去。
46
- *
47
- * 只认 `?` 之后的 query 段:路径里也可能出现 qual 字样(`#/quals`),用 includes
48
- * 判断会误伤。空值当没写。
49
- */
50
- export function qualFromHash(hash: string): string | null {
51
- const q = hash.indexOf("?");
52
- if (q < 0) return null;
53
- const id = new URLSearchParams(hash.slice(q + 1)).get("qual");
54
- return id ? id : null;
55
- }
1
+
2
+ /**
3
+ * 当前选项的本地偏好(从 <product>/qual.ts 拆入 core):
4
+ * 产品可选项目录是通用协议形状,"记住上次选了哪一项"是壳机制不是产品事实。
5
+ * 存储键保持 `ac.qual` 不变——老用户的偏好直接沿用。
6
+ *
7
+ * 产品侧只留具体选项的语义与路由映射。
8
+ */
9
+ const KEY = "ac.qual";
10
+
11
+ function loadQual(): string | null {
12
+ try {
13
+ return localStorage.getItem(KEY);
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+
19
+ export function saveQual(id: string): void {
20
+ try {
21
+ localStorage.setItem(KEY, id);
22
+ } catch {
23
+ // 隐私模式下 localStorage 可能抛错;选项只是偏好,丢了回选择页即可。
24
+ }
25
+ }
26
+
27
+ /** 可用 = 内容已就绪(`enabled` 不为 false)。 */
28
+ export function isEnabled(q: { enabled?: boolean }): boolean {
29
+ return q.enabled !== false;
30
+ }
31
+
32
+ /** 当前选项:本地记忆优先,否则回落第一个可用的选项。 */
33
+ export function currentQual<T extends { id: string; enabled?: boolean }>(quals: T[] | undefined): T | null {
34
+ if (!quals || quals.length === 0) return null;
35
+ const saved = loadQual();
36
+ const hit = saved ? quals.find((q) => q.id === saved && isEnabled(q)) : undefined;
37
+ return hit ?? quals.find(isEnabled) ?? null;
38
+ }
39
+
40
+ /**
41
+ * 深链里的选项(`#/<业务页>?qual=<轨道>&id=…`)。
42
+ *
43
+ * 为什么需要:页面记住的选项(ac.qual)与会话的 `AC_SCOPE` 是两件事——助手在
44
+ * system-integration 的会话里给了一张题卡,点进去时页面可能还停在 architect,
45
+ * 结果 404。深链把选项带上,壳静默切过去。
46
+ *
47
+ * 只认 `?` 之后的 query 段:路径里也可能出现 qual 字样(`#/quals`),用 includes
48
+ * 判断会误伤。空值当没写。
49
+ */
50
+ export function qualFromHash(hash: string): string | null {
51
+ const q = hash.indexOf("?");
52
+ if (q < 0) return null;
53
+ const id = new URLSearchParams(hash.slice(q + 1)).get("qual");
54
+ return id ? id : null;
55
+ }
@@ -1,104 +1,104 @@
1
- import type { ChatEvent, ChatRequest, ConfirmDecision } from "@njuptlzf/agent-chat-protocol";
2
- import { apiHeaders } from "./auth.ts";
3
-
4
- /**
5
- * Turn a non-2xx response into an Error carrying the server's own message.
6
- *
7
- * 服务端的 400 是给人看的(「标签最多 20 个字」「unknown tag "x"」),只抛
8
- * `HTTP 400` 会把这些话全丢掉,用户只能看到一个状态码。读不出 JSON 就退回状态码。
9
- */
10
- async function fail(res: Response): Promise<Error> {
11
- let detail = "";
12
- try {
13
- const body = (await res.json()) as { error?: { message?: string } };
14
- detail = body?.error?.message ?? "";
15
- } catch {
16
- // 不是 JSON(网关错误页之类)——状态码已经够用。
17
- }
18
- return new Error(detail ? `${detail}(HTTP ${res.status})` : `HTTP ${res.status}`);
19
- }
20
-
21
- /** GET a JSON API with the optional host auth token; throws on non-2xx. */
22
- export async function fetchJson<T>(path: string, authHeader?: string): Promise<T> {
23
- const res = await fetch(path, { headers: apiHeaders(authHeader) });
24
- if (!res.ok) throw await fail(res);
25
- return res.json() as Promise<T>;
26
- }
27
-
28
- /** POST a JSON body with the optional host auth token; throws on non-2xx. */
29
- export async function postJson<T>(path: string, body: unknown, authHeader?: string): Promise<T> {
30
- const res = await fetch(path, {
31
- method: "POST",
32
- headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
33
- body: JSON.stringify(body),
34
- });
35
- if (!res.ok) throw await fail(res);
36
- return res.json() as Promise<T>;
37
- }
38
-
39
- /** DELETE with the optional host auth token; throws on non-2xx. */
40
- export async function deleteJson<T>(path: string, authHeader?: string): Promise<T> {
41
- const res = await fetch(path, {
42
- method: "DELETE",
43
- headers: apiHeaders(authHeader),
44
- });
45
- if (!res.ok) throw await fail(res);
46
- return res.json() as Promise<T>;
47
- }
48
-
49
- /** Answer a `tool_confirm` event; must be called while its run's stream is open. */
50
- export async function postDecision(dec: ConfirmDecision, authHeader?: string): Promise<boolean> {
51
- const res = await fetch("/api/decision", {
52
- method: "POST",
53
- headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
54
- body: JSON.stringify(dec),
55
- });
56
- return res.ok;
57
- }
58
-
59
- /**
60
- * POST /api/chat and stream back protocol events. SSE frames are
61
- * `data: <json>\n\n`; we parse them by hand because EventSource cannot POST.
62
- */
63
- export async function* streamChat(
64
- req: ChatRequest,
65
- signal: AbortSignal,
66
- authHeader?: string,
67
- ): AsyncGenerator<ChatEvent> {
68
- const res = await fetch("/api/chat", {
69
- method: "POST",
70
- headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
71
- body: JSON.stringify(req),
72
- signal,
73
- });
74
-
75
- if (!res.ok || !res.body) {
76
- // Non-SSE error responses (401/404/429/…) carry { error: { code, message } }.
77
- let message = `HTTP ${res.status}`;
78
- try {
79
- const body = (await res.json()) as { error?: { message?: string } };
80
- if (body.error?.message) message = body.error.message;
81
- } catch {
82
- /* keep the status line */
83
- }
84
- yield { type: "error", code: res.status === 401 ? "unauthorized" : res.status === 429 ? "quota_exceeded" : "internal", message, retryable: false };
85
- yield { type: "done", aborted: false, usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 } };
86
- return;
87
- }
88
-
89
- const reader = res.body.getReader();
90
- const decoder = new TextDecoder();
91
- let buf = "";
92
- while (true) {
93
- const { value, done } = await reader.read();
94
- if (done) break;
95
- buf += decoder.decode(value, { stream: true });
96
- let idx;
97
- while ((idx = buf.indexOf("\n\n")) >= 0) {
98
- const frame = buf.slice(0, idx);
99
- buf = buf.slice(idx + 2);
100
- const line = frame.split("\n").find((l) => l.startsWith("data: "));
101
- if (line) yield JSON.parse(line.slice(6)) as ChatEvent;
102
- }
103
- }
104
- }
1
+ import type { ChatEvent, ChatRequest, ConfirmDecision } from "@njuptlzf/agent-chat-protocol";
2
+ import { apiHeaders } from "./auth.ts";
3
+
4
+ /**
5
+ * Turn a non-2xx response into an Error carrying the server's own message.
6
+ *
7
+ * 服务端的 400 是给人看的(「标签最多 20 个字」「unknown tag "x"」),只抛
8
+ * `HTTP 400` 会把这些话全丢掉,用户只能看到一个状态码。读不出 JSON 就退回状态码。
9
+ */
10
+ async function fail(res: Response): Promise<Error> {
11
+ let detail = "";
12
+ try {
13
+ const body = (await res.json()) as { error?: { message?: string } };
14
+ detail = body?.error?.message ?? "";
15
+ } catch {
16
+ // 不是 JSON(网关错误页之类)——状态码已经够用。
17
+ }
18
+ return new Error(detail ? `${detail}(HTTP ${res.status})` : `HTTP ${res.status}`);
19
+ }
20
+
21
+ /** GET a JSON API with the optional host auth token; throws on non-2xx. */
22
+ export async function fetchJson<T>(path: string, authHeader?: string): Promise<T> {
23
+ const res = await fetch(path, { headers: apiHeaders(authHeader) });
24
+ if (!res.ok) throw await fail(res);
25
+ return res.json() as Promise<T>;
26
+ }
27
+
28
+ /** POST a JSON body with the optional host auth token; throws on non-2xx. */
29
+ export async function postJson<T>(path: string, body: unknown, authHeader?: string): Promise<T> {
30
+ const res = await fetch(path, {
31
+ method: "POST",
32
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
33
+ body: JSON.stringify(body),
34
+ });
35
+ if (!res.ok) throw await fail(res);
36
+ return res.json() as Promise<T>;
37
+ }
38
+
39
+ /** DELETE with the optional host auth token; throws on non-2xx. */
40
+ export async function deleteJson<T>(path: string, authHeader?: string): Promise<T> {
41
+ const res = await fetch(path, {
42
+ method: "DELETE",
43
+ headers: apiHeaders(authHeader),
44
+ });
45
+ if (!res.ok) throw await fail(res);
46
+ return res.json() as Promise<T>;
47
+ }
48
+
49
+ /** Answer a `tool_confirm` event; must be called while its run's stream is open. */
50
+ export async function postDecision(dec: ConfirmDecision, authHeader?: string): Promise<boolean> {
51
+ const res = await fetch("/api/decision", {
52
+ method: "POST",
53
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
54
+ body: JSON.stringify(dec),
55
+ });
56
+ return res.ok;
57
+ }
58
+
59
+ /**
60
+ * POST /api/chat and stream back protocol events. SSE frames are
61
+ * `data: <json>\n\n`; we parse them by hand because EventSource cannot POST.
62
+ */
63
+ export async function* streamChat(
64
+ req: ChatRequest,
65
+ signal: AbortSignal,
66
+ authHeader?: string,
67
+ ): AsyncGenerator<ChatEvent> {
68
+ const res = await fetch("/api/chat", {
69
+ method: "POST",
70
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
71
+ body: JSON.stringify(req),
72
+ signal,
73
+ });
74
+
75
+ if (!res.ok || !res.body) {
76
+ // Non-SSE error responses (401/404/429/…) carry { error: { code, message } }.
77
+ let message = `HTTP ${res.status}`;
78
+ try {
79
+ const body = (await res.json()) as { error?: { message?: string } };
80
+ if (body.error?.message) message = body.error.message;
81
+ } catch {
82
+ /* keep the status line */
83
+ }
84
+ yield { type: "error", code: res.status === 401 ? "unauthorized" : res.status === 429 ? "quota_exceeded" : "internal", message, retryable: false };
85
+ yield { type: "done", aborted: false, usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 } };
86
+ return;
87
+ }
88
+
89
+ const reader = res.body.getReader();
90
+ const decoder = new TextDecoder();
91
+ let buf = "";
92
+ while (true) {
93
+ const { value, done } = await reader.read();
94
+ if (done) break;
95
+ buf += decoder.decode(value, { stream: true });
96
+ let idx;
97
+ while ((idx = buf.indexOf("\n\n")) >= 0) {
98
+ const frame = buf.slice(0, idx);
99
+ buf = buf.slice(idx + 2);
100
+ const line = frame.split("\n").find((l) => l.startsWith("data: "));
101
+ if (line) yield JSON.parse(line.slice(6)) as ChatEvent;
102
+ }
103
+ }
104
+ }
@@ -1,23 +1,23 @@
1
- /**
2
- * Theme mode (L2): "auto" follows the OS via `color-scheme: light dark` +
3
- * light-dark() tokens; "light"/dark" pin it with a data-theme attribute on
4
- * <html>. The choice persists per browser (localStorage) — it is a device
5
- * preference, not product state, so it never travels through the protocol.
6
- */
7
- export type ThemeMode = "auto" | "light" | "dark";
8
-
9
- const KEY = "ac.theme";
10
-
11
- export function loadThemeMode(): ThemeMode {
12
- const v = globalThis.localStorage?.getItem(KEY);
13
- return v === "light" || v === "dark" ? v : "auto";
14
- }
15
-
16
- export function applyThemeMode(mode: ThemeMode): void {
17
- if (mode === "auto") document.documentElement.removeAttribute("data-theme");
18
- else document.documentElement.setAttribute("data-theme", mode);
19
- }
20
-
21
- export function saveThemeMode(mode: ThemeMode): void {
22
- globalThis.localStorage?.setItem(KEY, mode);
23
- }
1
+ /**
2
+ * Theme mode (L2): "auto" follows the OS via `color-scheme: light dark` +
3
+ * light-dark() tokens; "light"/dark" pin it with a data-theme attribute on
4
+ * <html>. The choice persists per browser (localStorage) — it is a device
5
+ * preference, not product state, so it never travels through the protocol.
6
+ */
7
+ export type ThemeMode = "auto" | "light" | "dark";
8
+
9
+ const KEY = "ac.theme";
10
+
11
+ export function loadThemeMode(): ThemeMode {
12
+ const v = globalThis.localStorage?.getItem(KEY);
13
+ return v === "light" || v === "dark" ? v : "auto";
14
+ }
15
+
16
+ export function applyThemeMode(mode: ThemeMode): void {
17
+ if (mode === "auto") document.documentElement.removeAttribute("data-theme");
18
+ else document.documentElement.setAttribute("data-theme", mode);
19
+ }
20
+
21
+ export function saveThemeMode(mode: ThemeMode): void {
22
+ globalThis.localStorage?.setItem(KEY, mode);
23
+ }
@@ -1,29 +1,29 @@
1
- import { useState } from "react";
2
- import type { CodeHeaderProps } from "@assistant-ui/react-markdown";
3
-
4
- /**
5
- * Code-fence header with a copy button (assistant-ui's default CodeHeader
6
- * renders nothing). It receives the raw `code` from the primitive, so there is
7
- * no DOM scraping here — the string the model emitted is the string copied.
8
- */
9
- export default function CodeCopyHeader({ language, code }: CodeHeaderProps) {
10
- const [copied, setCopied] = useState(false);
11
- const copy = async () => {
12
- try {
13
- await navigator.clipboard.writeText(code);
14
- setCopied(true);
15
- setTimeout(() => setCopied(false), 1500);
16
- } catch {
17
- // Clipboard can be refused (permissions/insecure context): stay silent,
18
- // the code is still selectable by hand.
19
- }
20
- };
21
- return (
22
- <div className="code-head">
23
- <span className="code-lang">{language || "text"}</span>
24
- <button type="button" className="copy-code" onClick={() => void copy()}>
25
- {copied ? "已复制" : "复制"}
26
- </button>
27
- </div>
28
- );
29
- }
1
+ import { useState } from "react";
2
+ import type { CodeHeaderProps } from "@assistant-ui/react-markdown";
3
+
4
+ /**
5
+ * Code-fence header with a copy button (assistant-ui's default CodeHeader
6
+ * renders nothing). It receives the raw `code` from the primitive, so there is
7
+ * no DOM scraping here — the string the model emitted is the string copied.
8
+ */
9
+ export default function CodeCopyHeader({ language, code }: CodeHeaderProps) {
10
+ const [copied, setCopied] = useState(false);
11
+ const copy = async () => {
12
+ try {
13
+ await navigator.clipboard.writeText(code);
14
+ setCopied(true);
15
+ setTimeout(() => setCopied(false), 1500);
16
+ } catch {
17
+ // Clipboard can be refused (permissions/insecure context): stay silent,
18
+ // the code is still selectable by hand.
19
+ }
20
+ };
21
+ return (
22
+ <div className="code-head">
23
+ <span className="code-lang">{language || "text"}</span>
24
+ <button type="button" className="copy-code" onClick={() => void copy()}>
25
+ {copied ? "已复制" : "复制"}
26
+ </button>
27
+ </div>
28
+ );
29
+ }
@@ -1,32 +1,32 @@
1
- export interface ConfirmRequest {
2
- sessionId: string;
3
- toolCallId: string;
4
- name: string;
5
- args: Record<string, unknown>;
6
- }
7
-
8
- /**
9
- * Tool-execution permission card (`tool_confirm` event, custom component UI-R3).
10
- * assistant-ui has no server-parked approval primitive; answering POSTs
11
- * /api/decision — the caller (ChatApp) keeps that plumbing, this is pure view.
12
- */
13
- export function ConfirmCard({
14
- request,
15
- onAnswer,
16
- }: {
17
- request: ConfirmRequest;
18
- onAnswer: (allow: boolean) => void;
19
- }) {
20
- return (
21
- <div className="confirm">
22
- <div className="confirm-title">
23
- ⚠️ 助手请求执行 <b>{request.name}</b>,是否允许?
24
- </div>
25
- <pre className="confirm-args">{JSON.stringify(request.args, null, 2)}</pre>
26
- <div className="confirm-actions">
27
- <button className="allow" onClick={() => onAnswer(true)}>允许</button>
28
- <button className="deny" onClick={() => onAnswer(false)}>拒绝</button>
29
- </div>
30
- </div>
31
- );
32
- }
1
+ export interface ConfirmRequest {
2
+ sessionId: string;
3
+ toolCallId: string;
4
+ name: string;
5
+ args: Record<string, unknown>;
6
+ }
7
+
8
+ /**
9
+ * Tool-execution permission card (`tool_confirm` event, custom component UI-R3).
10
+ * assistant-ui has no server-parked approval primitive; answering POSTs
11
+ * /api/decision — the caller (ChatApp) keeps that plumbing, this is pure view.
12
+ */
13
+ export function ConfirmCard({
14
+ request,
15
+ onAnswer,
16
+ }: {
17
+ request: ConfirmRequest;
18
+ onAnswer: (allow: boolean) => void;
19
+ }) {
20
+ return (
21
+ <div className="confirm">
22
+ <div className="confirm-title">
23
+ ⚠️ 助手请求执行 <b>{request.name}</b>,是否允许?
24
+ </div>
25
+ <pre className="confirm-args">{JSON.stringify(request.args, null, 2)}</pre>
26
+ <div className="confirm-actions">
27
+ <button className="allow" onClick={() => onAnswer(true)}>允许</button>
28
+ <button className="deny" onClick={() => onAnswer(false)}>拒绝</button>
29
+ </div>
30
+ </div>
31
+ );
32
+ }
@@ -1,32 +1,32 @@
1
- import { useEffect, useState } from "react";
2
-
3
- /**
4
- * ⑥ 图片点击放大(lightbox)。全局委托监听,只认 `.thread` 内的 `<img>`
5
- * (品牌 logo、头像等不在对话区,不会触发)。点击遮罩或放大图本身关闭,
6
- * Esc 也关闭。纯前端,零协议改动。
7
- */
8
- export function Lightbox() {
9
- const [src, setSrc] = useState<string | null>(null);
10
- useEffect(() => {
11
- const onClick = (e: MouseEvent) => {
12
- const t = e.target as HTMLElement | null;
13
- if (!(t instanceof HTMLImageElement) || !t.closest(".thread")) return;
14
- setSrc(t.currentSrc || t.src);
15
- };
16
- const onKey = (e: KeyboardEvent) => {
17
- if (e.key === "Escape") setSrc(null);
18
- };
19
- document.addEventListener("click", onClick, true);
20
- document.addEventListener("keydown", onKey);
21
- return () => {
22
- document.removeEventListener("click", onClick, true);
23
- document.removeEventListener("keydown", onKey);
24
- };
25
- }, []);
26
- if (!src) return null;
27
- return (
28
- <div className="lightbox" role="dialog" aria-label="图片预览" onClick={() => setSrc(null)}>
29
- <img src={src} alt="" />
30
- </div>
31
- );
32
- }
1
+ import { useEffect, useState } from "react";
2
+
3
+ /**
4
+ * ⑥ 图片点击放大(lightbox)。全局委托监听,只认 `.thread` 内的 `<img>`
5
+ * (品牌 logo、头像等不在对话区,不会触发)。点击遮罩或放大图本身关闭,
6
+ * Esc 也关闭。纯前端,零协议改动。
7
+ */
8
+ export function Lightbox() {
9
+ const [src, setSrc] = useState<string | null>(null);
10
+ useEffect(() => {
11
+ const onClick = (e: MouseEvent) => {
12
+ const t = e.target as HTMLElement | null;
13
+ if (!(t instanceof HTMLImageElement) || !t.closest(".thread")) return;
14
+ setSrc(t.currentSrc || t.src);
15
+ };
16
+ const onKey = (e: KeyboardEvent) => {
17
+ if (e.key === "Escape") setSrc(null);
18
+ };
19
+ document.addEventListener("click", onClick, true);
20
+ document.addEventListener("keydown", onKey);
21
+ return () => {
22
+ document.removeEventListener("click", onClick, true);
23
+ document.removeEventListener("keydown", onKey);
24
+ };
25
+ }, []);
26
+ if (!src) return null;
27
+ return (
28
+ <div className="lightbox" role="dialog" aria-label="图片预览" onClick={() => setSrc(null)}>
29
+ <img src={src} alt="" />
30
+ </div>
31
+ );
32
+ }