@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.
- package/dist/AgentChat.d.ts +28 -0
- package/dist/AgentChat.js +355 -0
- package/dist/AgentChatApp.d.ts +31 -0
- package/dist/AgentChatApp.js +90 -0
- package/dist/DockPanel.d.ts +22 -0
- package/dist/DockPanel.js +22 -0
- package/dist/PersonasPage.d.ts +1 -0
- package/dist/PersonasPage.js +318 -0
- package/dist/adapter.d.ts +52 -0
- package/dist/adapter.js +242 -0
- package/dist/app-def-check.d.ts +50 -0
- package/dist/app-def-check.js +184 -0
- package/dist/app.d.ts +112 -0
- package/dist/app.js +174 -0
- package/dist/auth.d.ts +41 -0
- package/dist/auth.js +86 -0
- package/dist/dock.d.ts +52 -0
- package/dist/dock.js +132 -0
- package/dist/docs.d.ts +48 -0
- package/dist/docs.js +178 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +8 -0
- package/dist/mount.d.ts +50 -0
- package/dist/mount.js +38 -0
- package/dist/qual.d.ts +21 -0
- package/dist/qual.js +53 -0
- package/dist/sse.d.ts +14 -0
- package/dist/sse.js +99 -0
- package/dist/styles.css +1478 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +14 -0
- package/dist/ui/CodeCopyHeader.d.ts +7 -0
- package/dist/ui/CodeCopyHeader.js +22 -0
- package/dist/ui/ConfirmCard.d.ts +15 -0
- package/dist/ui/ConfirmCard.js +9 -0
- package/dist/ui/Lightbox.d.ts +6 -0
- package/dist/ui/Lightbox.js +31 -0
- package/dist/ui/QueueBar.d.ts +44 -0
- package/dist/ui/QueueBar.js +17 -0
- package/dist/ui/Splitter.d.ts +27 -0
- package/dist/ui/Splitter.js +61 -0
- package/dist/ui/ThemeToggle.d.ts +2 -0
- package/dist/ui/ThemeToggle.js +16 -0
- package/dist/ui/TierPicker.d.ts +14 -0
- package/dist/ui/TierPicker.js +54 -0
- package/dist/ui/UIPromptCard.d.ts +17 -0
- package/dist/ui/UIPromptCard.js +18 -0
- package/dist/ui/timefmt.d.ts +35 -0
- package/dist/ui/timefmt.js +110 -0
- package/dist/ui/useNarrow.d.ts +7 -0
- package/dist/ui/useNarrow.js +15 -0
- package/package.json +72 -68
- package/src/App.tsx +13 -0
- package/src/agent-chat/AgentChat.tsx +692 -692
- package/src/agent-chat/AgentChatApp.tsx +206 -206
- package/src/agent-chat/{Dock.tsx → DockPanel.tsx} +73 -73
- package/src/agent-chat/PersonasPage.tsx +678 -678
- package/src/agent-chat/adapter.ts +272 -272
- package/src/agent-chat/app-def-check.ts +215 -215
- package/src/agent-chat/app.tsx +327 -327
- package/src/agent-chat/auth.ts +83 -83
- package/src/agent-chat/dock.ts +161 -161
- package/src/agent-chat/docs.ts +179 -179
- package/src/agent-chat/index.ts +17 -17
- package/src/agent-chat/mount.tsx +123 -123
- package/src/agent-chat/qual.ts +55 -55
- package/src/agent-chat/sse.ts +104 -104
- package/src/agent-chat/theme.ts +23 -23
- package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -29
- package/src/agent-chat/ui/ConfirmCard.tsx +32 -32
- package/src/agent-chat/ui/Lightbox.tsx +32 -32
- package/src/agent-chat/ui/QueueBar.tsx +70 -70
- package/src/agent-chat/ui/Splitter.tsx +81 -81
- package/src/agent-chat/ui/ThemeToggle.tsx +25 -25
- package/src/agent-chat/ui/TierPicker.tsx +112 -112
- package/src/agent-chat/ui/UIPromptCard.tsx +68 -68
- package/src/agent-chat/ui/timefmt.ts +110 -110
- package/src/agent-chat/ui/useNarrow.ts +20 -20
- package/src/fake-product/entry.tsx +54 -0
- package/src/main.tsx +8 -0
- package/src/styles.css +1478 -1478
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
|
+
}
|