@njuptlzf/agent-chat-web 0.1.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 (85) hide show
  1. package/dist/AgentChat.d.ts +34 -0
  2. package/dist/AgentChat.js +373 -0
  3. package/dist/AgentChatApp.d.ts +34 -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 +326 -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/assets/_commonjs-dynamic-modules-TDtrdbi3.js +1 -0
  16. package/dist/assets/exceljs.min-jlEt4kSc.js +29 -0
  17. package/dist/assets/index-B7uSE8SM.css +1 -0
  18. package/dist/assets/index-D57mU5H5.js +50 -0
  19. package/dist/assets/index-Dviy7gnB.js +44 -0
  20. package/dist/assets/pdf-CfhDiWmk.js +55 -0
  21. package/dist/assets/pdf.worker.min-CdXBBzB4.js +1 -0
  22. package/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
  23. package/dist/auth.d.ts +41 -0
  24. package/dist/auth.js +86 -0
  25. package/dist/dock.d.ts +52 -0
  26. package/dist/dock.js +132 -0
  27. package/dist/docs.d.ts +48 -0
  28. package/dist/docs.js +178 -0
  29. package/dist/index.d.ts +17 -0
  30. package/dist/index.html +13 -0
  31. package/dist/index.js +8 -0
  32. package/dist/mount.d.ts +56 -0
  33. package/dist/mount.js +38 -0
  34. package/dist/qual.d.ts +21 -0
  35. package/dist/qual.js +53 -0
  36. package/dist/sse.d.ts +14 -0
  37. package/dist/sse.js +99 -0
  38. package/dist/styles.css +1492 -0
  39. package/dist/suggestions.d.ts +41 -0
  40. package/dist/suggestions.js +98 -0
  41. package/dist/theme.d.ts +10 -0
  42. package/dist/theme.js +14 -0
  43. package/dist/ui/CodeCopyHeader.d.ts +7 -0
  44. package/dist/ui/CodeCopyHeader.js +22 -0
  45. package/dist/ui/ConfirmCard.d.ts +15 -0
  46. package/dist/ui/ConfirmCard.js +9 -0
  47. package/dist/ui/FollowUpSuggestions.d.ts +9 -0
  48. package/dist/ui/FollowUpSuggestions.js +13 -0
  49. package/dist/ui/Icon.d.ts +8 -0
  50. package/dist/ui/Icon.js +26 -0
  51. package/dist/ui/Lightbox.d.ts +6 -0
  52. package/dist/ui/Lightbox.js +31 -0
  53. package/dist/ui/QueueBar.d.ts +44 -0
  54. package/dist/ui/QueueBar.js +17 -0
  55. package/dist/ui/Splitter.d.ts +27 -0
  56. package/dist/ui/Splitter.js +61 -0
  57. package/dist/ui/ThemeToggle.d.ts +2 -0
  58. package/dist/ui/ThemeToggle.js +16 -0
  59. package/dist/ui/TierPicker.d.ts +14 -0
  60. package/dist/ui/TierPicker.js +54 -0
  61. package/dist/ui/UIPromptCard.d.ts +17 -0
  62. package/dist/ui/UIPromptCard.js +18 -0
  63. package/dist/ui/icons.d.ts +19 -0
  64. package/dist/ui/icons.js +25 -0
  65. package/dist/ui/slots.d.ts +23 -0
  66. package/dist/ui/slots.js +13 -0
  67. package/dist/ui/timefmt.d.ts +35 -0
  68. package/dist/ui/timefmt.js +110 -0
  69. package/dist/ui/useNarrow.d.ts +7 -0
  70. package/dist/ui/useNarrow.js +15 -0
  71. package/package.json +14 -11
  72. package/src/App.tsx +13 -0
  73. package/src/agent-chat/AgentChat.tsx +39 -2
  74. package/src/agent-chat/AgentChatApp.tsx +5 -0
  75. package/src/agent-chat/{Dock.tsx → DockPanel.tsx} +73 -73
  76. package/src/agent-chat/PersonasPage.tsx +28 -1
  77. package/src/agent-chat/mount.tsx +130 -123
  78. package/src/agent-chat/suggestions.ts +127 -0
  79. package/src/agent-chat/ui/FollowUpSuggestions.tsx +28 -0
  80. package/src/agent-chat/ui/Icon.tsx +53 -0
  81. package/src/agent-chat/ui/icons.ts +31 -0
  82. package/src/agent-chat/ui/slots.tsx +35 -0
  83. package/src/fake-product/entry.tsx +63 -0
  84. package/src/main.tsx +8 -0
  85. package/src/styles.css +15 -1
package/dist/mount.js ADDED
@@ -0,0 +1,38 @@
1
+ import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
2
+ import AgentChatApp from "./AgentChatApp.js";
3
+ import DockPanel from "./DockPanel.js";
4
+ import { AppShell, themeVarsOf, useAppHost } from "./app.js";
5
+ /**
6
+ * 产品组合根:branding/whoami → 宿主状态 → 侧栏 + 坞 + 页面。
7
+ *
8
+ * 与改造前 `App.tsx` 的行为逐项对齐(full/compact 的判据、key 重挂、插槽位置),
9
+ * 差别只有一处:人格不再由 core 判断"谁是声明人格"——`host.persona` 原样传给
10
+ * `AgentChatApp`("" 收敛成 undefined = 产品默认助手),文案由 `texts` 决定。
11
+ */
12
+ export function AgentApp({ def, texts, slots, footer, extras, loadErrorHint }) {
13
+ const host = useAppHost(def);
14
+ if (host.loadError) {
15
+ return (_jsxs("div", { className: "center", children: [_jsxs("p", { children: ["\u65E0\u6CD5\u8FDE\u63A5\u670D\u52A1\uFF1A", host.loadError] }), loadErrorHint] }));
16
+ }
17
+ if (!host.ready)
18
+ return _jsx("div", { className: "center", children: "\u52A0\u8F7D\u4E2D\u2026" });
19
+ const { branding, who, qual, route, shown, onQuals, persona, go, ctx } = host;
20
+ // 坞是否撑满内容区。同一个判据喂给 Dock.full 与 AgentChatApp.compact
21
+ // (2026-11-03:以前 compact 单独判「路由不是聊天页」,漏了选项页这种
22
+ // 「路由还是 chatRoute 但坞不是全屏」的状态,会话侧栏把输入框挤成 56px)。
23
+ const dockFull = !onQuals && route === def.chatRoute;
24
+ const t = texts(branding, persona);
25
+ return (_jsx("div", { className: "shell", style: themeVarsOf(branding), children: _jsxs(AppShell, { branding: branding, qual: qual, route: shown, nav: def.navItems(ctx), who: who,
26
+ // 侧栏导航回聊天路由 = 回**默认人格**(2026-11-04):从声明人格切「默认助手」时只改
27
+ // 路由的话,人格还挂在声明人格上,标题/预备问题都不换。人格化入口(人格化按钮)
28
+ // 走 host.go 自己先 setPersona,不经过这里,所以不会被这条冲掉。
29
+ onRoute: (r) => {
30
+ if (r === def.chatRoute && host.persona)
31
+ host.setPersona("");
32
+ go(r);
33
+ }, onQuals: () => go("quals"), switchTitle: def.labels?.switchTitle, footer: footer?.(host), children: [_jsx(DockPanel, { title: t.title, full: dockFull, onClose: host.closeChat, chat: _jsx(AgentChatApp
34
+ // key 让切人格时重挂:会话绑死人格,同一个实例换 persona 只会撞 persona_mismatch。
35
+ , { branding: branding, scope: qual?.id, renderCard: def.cardRenderer, slots: slots,
36
+ // core 只做形状收敛("" → undefined = 产品默认助手),不认识具体名字。
37
+ persona: persona || undefined, title: t.title, assistantLabel: t.assistantLabel, policyNote: t.policyNote, welcome: t.welcome, suggestions: t.suggestions, compact: !dockFull }, persona), children: def.page(ctx) }), extras?.(host)] }) }));
38
+ }
package/dist/qual.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ export declare function saveQual(id: string): void;
2
+ /** 可用 = 内容已就绪(`enabled` 不为 false)。 */
3
+ export declare function isEnabled(q: {
4
+ enabled?: boolean;
5
+ }): boolean;
6
+ /** 当前选项:本地记忆优先,否则回落第一个可用的选项。 */
7
+ export declare function currentQual<T extends {
8
+ id: string;
9
+ enabled?: boolean;
10
+ }>(quals: T[] | undefined): T | null;
11
+ /**
12
+ * 深链里的选项(`#/<业务页>?qual=<轨道>&id=…`)。
13
+ *
14
+ * 为什么需要:页面记住的选项(ac.qual)与会话的 `AC_SCOPE` 是两件事——助手在
15
+ * system-integration 的会话里给了一张题卡,点进去时页面可能还停在 architect,
16
+ * 结果 404。深链把选项带上,壳静默切过去。
17
+ *
18
+ * 只认 `?` 之后的 query 段:路径里也可能出现 qual 字样(`#/quals`),用 includes
19
+ * 判断会误伤。空值当没写。
20
+ */
21
+ export declare function qualFromHash(hash: string): string | null;
package/dist/qual.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 当前选项的本地偏好(从 <product>/qual.ts 拆入 core):
3
+ * 产品可选项目录是通用协议形状,"记住上次选了哪一项"是壳机制不是产品事实。
4
+ * 存储键保持 `ac.qual` 不变——老用户的偏好直接沿用。
5
+ *
6
+ * 产品侧只留具体选项的语义与路由映射。
7
+ */
8
+ const KEY = "ac.qual";
9
+ function loadQual() {
10
+ try {
11
+ return localStorage.getItem(KEY);
12
+ }
13
+ catch {
14
+ return null;
15
+ }
16
+ }
17
+ export function saveQual(id) {
18
+ try {
19
+ localStorage.setItem(KEY, id);
20
+ }
21
+ catch {
22
+ // 隐私模式下 localStorage 可能抛错;选项只是偏好,丢了回选择页即可。
23
+ }
24
+ }
25
+ /** 可用 = 内容已就绪(`enabled` 不为 false)。 */
26
+ export function isEnabled(q) {
27
+ return q.enabled !== false;
28
+ }
29
+ /** 当前选项:本地记忆优先,否则回落第一个可用的选项。 */
30
+ export function currentQual(quals) {
31
+ if (!quals || quals.length === 0)
32
+ return null;
33
+ const saved = loadQual();
34
+ const hit = saved ? quals.find((q) => q.id === saved && isEnabled(q)) : undefined;
35
+ return hit ?? quals.find(isEnabled) ?? null;
36
+ }
37
+ /**
38
+ * 深链里的选项(`#/<业务页>?qual=<轨道>&id=…`)。
39
+ *
40
+ * 为什么需要:页面记住的选项(ac.qual)与会话的 `AC_SCOPE` 是两件事——助手在
41
+ * system-integration 的会话里给了一张题卡,点进去时页面可能还停在 architect,
42
+ * 结果 404。深链把选项带上,壳静默切过去。
43
+ *
44
+ * 只认 `?` 之后的 query 段:路径里也可能出现 qual 字样(`#/quals`),用 includes
45
+ * 判断会误伤。空值当没写。
46
+ */
47
+ export function qualFromHash(hash) {
48
+ const q = hash.indexOf("?");
49
+ if (q < 0)
50
+ return null;
51
+ const id = new URLSearchParams(hash.slice(q + 1)).get("qual");
52
+ return id ? id : null;
53
+ }
package/dist/sse.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { ChatEvent, ChatRequest, ConfirmDecision } from "@njuptlzf/agent-chat-protocol";
2
+ /** GET a JSON API with the optional host auth token; throws on non-2xx. */
3
+ export declare function fetchJson<T>(path: string, authHeader?: string): Promise<T>;
4
+ /** POST a JSON body with the optional host auth token; throws on non-2xx. */
5
+ export declare function postJson<T>(path: string, body: unknown, authHeader?: string): Promise<T>;
6
+ /** DELETE with the optional host auth token; throws on non-2xx. */
7
+ export declare function deleteJson<T>(path: string, authHeader?: string): Promise<T>;
8
+ /** Answer a `tool_confirm` event; must be called while its run's stream is open. */
9
+ export declare function postDecision(dec: ConfirmDecision, authHeader?: string): Promise<boolean>;
10
+ /**
11
+ * POST /api/chat and stream back protocol events. SSE frames are
12
+ * `data: <json>\n\n`; we parse them by hand because EventSource cannot POST.
13
+ */
14
+ export declare function streamChat(req: ChatRequest, signal: AbortSignal, authHeader?: string): AsyncGenerator<ChatEvent>;
package/dist/sse.js ADDED
@@ -0,0 +1,99 @@
1
+ import { apiHeaders } from "./auth.js";
2
+ /**
3
+ * Turn a non-2xx response into an Error carrying the server's own message.
4
+ *
5
+ * 服务端的 400 是给人看的(「标签最多 20 个字」「unknown tag "x"」),只抛
6
+ * `HTTP 400` 会把这些话全丢掉,用户只能看到一个状态码。读不出 JSON 就退回状态码。
7
+ */
8
+ async function fail(res) {
9
+ let detail = "";
10
+ try {
11
+ const body = (await res.json());
12
+ detail = body?.error?.message ?? "";
13
+ }
14
+ catch {
15
+ // 不是 JSON(网关错误页之类)——状态码已经够用。
16
+ }
17
+ return new Error(detail ? `${detail}(HTTP ${res.status})` : `HTTP ${res.status}`);
18
+ }
19
+ /** GET a JSON API with the optional host auth token; throws on non-2xx. */
20
+ export async function fetchJson(path, authHeader) {
21
+ const res = await fetch(path, { headers: apiHeaders(authHeader) });
22
+ if (!res.ok)
23
+ throw await fail(res);
24
+ return res.json();
25
+ }
26
+ /** POST a JSON body with the optional host auth token; throws on non-2xx. */
27
+ export async function postJson(path, body, authHeader) {
28
+ const res = await fetch(path, {
29
+ method: "POST",
30
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
31
+ body: JSON.stringify(body),
32
+ });
33
+ if (!res.ok)
34
+ throw await fail(res);
35
+ return res.json();
36
+ }
37
+ /** DELETE with the optional host auth token; throws on non-2xx. */
38
+ export async function deleteJson(path, authHeader) {
39
+ const res = await fetch(path, {
40
+ method: "DELETE",
41
+ headers: apiHeaders(authHeader),
42
+ });
43
+ if (!res.ok)
44
+ throw await fail(res);
45
+ return res.json();
46
+ }
47
+ /** Answer a `tool_confirm` event; must be called while its run's stream is open. */
48
+ export async function postDecision(dec, authHeader) {
49
+ const res = await fetch("/api/decision", {
50
+ method: "POST",
51
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
52
+ body: JSON.stringify(dec),
53
+ });
54
+ return res.ok;
55
+ }
56
+ /**
57
+ * POST /api/chat and stream back protocol events. SSE frames are
58
+ * `data: <json>\n\n`; we parse them by hand because EventSource cannot POST.
59
+ */
60
+ export async function* streamChat(req, signal, authHeader) {
61
+ const res = await fetch("/api/chat", {
62
+ method: "POST",
63
+ headers: { "content-type": "application/json", ...apiHeaders(authHeader) },
64
+ body: JSON.stringify(req),
65
+ signal,
66
+ });
67
+ if (!res.ok || !res.body) {
68
+ // Non-SSE error responses (401/404/429/…) carry { error: { code, message } }.
69
+ let message = `HTTP ${res.status}`;
70
+ try {
71
+ const body = (await res.json());
72
+ if (body.error?.message)
73
+ message = body.error.message;
74
+ }
75
+ catch {
76
+ /* keep the status line */
77
+ }
78
+ yield { type: "error", code: res.status === 401 ? "unauthorized" : res.status === 429 ? "quota_exceeded" : "internal", message, retryable: false };
79
+ yield { type: "done", aborted: false, usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 } };
80
+ return;
81
+ }
82
+ const reader = res.body.getReader();
83
+ const decoder = new TextDecoder();
84
+ let buf = "";
85
+ while (true) {
86
+ const { value, done } = await reader.read();
87
+ if (done)
88
+ break;
89
+ buf += decoder.decode(value, { stream: true });
90
+ let idx;
91
+ while ((idx = buf.indexOf("\n\n")) >= 0) {
92
+ const frame = buf.slice(0, idx);
93
+ buf = buf.slice(idx + 2);
94
+ const line = frame.split("\n").find((l) => l.startsWith("data: "));
95
+ if (line)
96
+ yield JSON.parse(line.slice(6));
97
+ }
98
+ }
99
+ }