@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.
- package/dist/AgentChat.d.ts +7 -1
- package/dist/AgentChat.js +35 -5
- package/dist/AgentChatApp.d.ts +4 -1
- package/dist/AgentChatApp.js +2 -2
- package/dist/PersonasPage.js +8 -0
- package/dist/adapter.d.ts +9 -0
- package/dist/adapter.js +15 -0
- package/dist/mount.d.ts +7 -1
- package/dist/mount.js +2 -2
- package/dist/styles.css +1505 -1478
- package/dist/suggestions.d.ts +41 -0
- package/dist/suggestions.js +98 -0
- package/dist/ui/FollowUpSuggestions.d.ts +9 -0
- package/dist/ui/FollowUpSuggestions.js +13 -0
- package/dist/ui/Icon.d.ts +8 -0
- package/dist/ui/Icon.js +26 -0
- package/dist/ui/MemberCards.d.ts +15 -0
- package/dist/ui/MemberCards.js +13 -0
- package/dist/ui/icons.d.ts +19 -0
- package/dist/ui/icons.js +25 -0
- package/dist/ui/members.d.ts +42 -0
- package/dist/ui/members.js +53 -0
- package/dist/ui/slots.d.ts +29 -0
- package/dist/ui/slots.js +14 -0
- package/package.json +71 -72
- package/src/App.tsx +13 -13
- package/src/agent-chat/AgentChat.tsx +741 -692
- package/src/agent-chat/AgentChatApp.tsx +211 -206
- package/src/agent-chat/DockPanel.tsx +73 -73
- package/src/agent-chat/PersonasPage.tsx +705 -678
- package/src/agent-chat/adapter.ts +293 -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 +130 -123
- package/src/agent-chat/qual.ts +55 -55
- package/src/agent-chat/sse.ts +104 -104
- package/src/agent-chat/suggestions.ts +127 -0
- 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/FollowUpSuggestions.tsx +28 -0
- package/src/agent-chat/ui/Icon.tsx +53 -0
- package/src/agent-chat/ui/Lightbox.tsx +32 -32
- package/src/agent-chat/ui/MemberCards.tsx +46 -0
- 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/icons.ts +31 -0
- package/src/agent-chat/ui/members.ts +106 -0
- package/src/agent-chat/ui/slots.tsx +41 -0
- package/src/agent-chat/ui/timefmt.ts +110 -110
- package/src/agent-chat/ui/useNarrow.ts +20 -20
- package/src/fake-product/entry.tsx +63 -54
- package/src/main.tsx +8 -8
- package/src/styles.css +1505 -1478
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「下一步建议」的前端层:生效模式解析 + 自定义 `SuggestionAdapter`。
|
|
3
|
+
*
|
|
4
|
+
* 挂载走 `@assistant-ui` 原生 `adapters.suggestion`(run 结束自动生成、run 开始
|
|
5
|
+
* 自动清空),不自造状态机。数据来源由服务端决定:`llm` 调 `/api/suggestions`,
|
|
6
|
+
* `static` 直接用 `branding.ui.followUp.static`;任何失败静默返回空——建议区
|
|
7
|
+
* 消失而不是报错。
|
|
8
|
+
*/
|
|
9
|
+
import type { BrandingResponse } from "@njuptlzf/agent-chat-protocol";
|
|
10
|
+
import type { SuggestionAdapter } from "@assistant-ui/react";
|
|
11
|
+
export type FollowUpMode = "off" | "static" | "llm";
|
|
12
|
+
/**
|
|
13
|
+
* 与服务端 `SUGGESTIONS_DEFAULTS` 各持一份的缺省值(两个包不共享模块,
|
|
14
|
+
* 单测钉住两边一致)——改这里要同步那边。
|
|
15
|
+
*/
|
|
16
|
+
export declare const FOLLOW_UP_DEFAULTS: {
|
|
17
|
+
readonly count: 3;
|
|
18
|
+
readonly maxMessages: 10;
|
|
19
|
+
readonly messageChars: 500;
|
|
20
|
+
readonly totalChars: 4000;
|
|
21
|
+
};
|
|
22
|
+
/** 解析后的下一步建议配置(服务端已校验,这里再兜一道)。 */
|
|
23
|
+
export interface FollowUpConfig {
|
|
24
|
+
count: number;
|
|
25
|
+
maxMessages: number;
|
|
26
|
+
staticFallback: string[];
|
|
27
|
+
}
|
|
28
|
+
/** `branding.ui.followUp` 的声明形状(BrandingUi 未导出,用索引访问拿类型)。 */
|
|
29
|
+
type FollowUpDecl = NonNullable<BrandingResponse["ui"]>["followUp"];
|
|
30
|
+
/** `branding.ui.followUp` → 前端配置;缺字段走缺省,非法值回落。 */
|
|
31
|
+
export declare function followUpConfigOf(decl?: FollowUpDecl): FollowUpConfig;
|
|
32
|
+
/**
|
|
33
|
+
* 生效模式:**全局 `off` 是硬地板**——全局关掉后任何人格都不能重新打开;
|
|
34
|
+
* 否则人格 `followUpMode` > 全局 `mode` > 缺省 `llm`。默认助手(persona 为
|
|
35
|
+
* undefined)用 `branding.defaultPersona` 兜底;该人格不在 personas 表里时回落全局。
|
|
36
|
+
* 服务端 `/api/suggestions` 用同一套优先级复核,两边必须一致。
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveFollowUpMode(branding: BrandingResponse, persona?: string): FollowUpMode;
|
|
39
|
+
/** `off` 不产生 adapter(库不生成建议);`static` 本地返回、`llm` 请求服务端。 */
|
|
40
|
+
export declare function createFollowUpAdapter(cfg: FollowUpConfig, mode: Exclude<FollowUpMode, "off">, personaId?: string): SuggestionAdapter;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { apiHeaders, auth } from "./auth.js";
|
|
2
|
+
const MODES = ["off", "static", "llm"];
|
|
3
|
+
const isMode = (v) => typeof v === "string" && MODES.includes(v);
|
|
4
|
+
/**
|
|
5
|
+
* 与服务端 `SUGGESTIONS_DEFAULTS` 各持一份的缺省值(两个包不共享模块,
|
|
6
|
+
* 单测钉住两边一致)——改这里要同步那边。
|
|
7
|
+
*/
|
|
8
|
+
export const FOLLOW_UP_DEFAULTS = {
|
|
9
|
+
count: 3,
|
|
10
|
+
maxMessages: 10,
|
|
11
|
+
messageChars: 500,
|
|
12
|
+
totalChars: 4000,
|
|
13
|
+
};
|
|
14
|
+
/** `branding.ui.followUp` → 前端配置;缺字段走缺省,非法值回落。 */
|
|
15
|
+
export function followUpConfigOf(decl) {
|
|
16
|
+
const count = typeof decl?.count === "number" && Number.isInteger(decl.count) && decl.count >= 1 && decl.count <= 5
|
|
17
|
+
? decl.count
|
|
18
|
+
: FOLLOW_UP_DEFAULTS.count;
|
|
19
|
+
const maxMessages = typeof decl?.maxMessages === "number" && Number.isInteger(decl.maxMessages) && decl.maxMessages >= 1 && decl.maxMessages <= 20
|
|
20
|
+
? decl.maxMessages
|
|
21
|
+
: FOLLOW_UP_DEFAULTS.maxMessages;
|
|
22
|
+
const staticFallback = Array.isArray(decl?.static)
|
|
23
|
+
? decl.static.filter((s) => typeof s === "string" && s.trim() !== "").map((s) => s.trim())
|
|
24
|
+
: [];
|
|
25
|
+
return { count, maxMessages, staticFallback };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 生效模式:**全局 `off` 是硬地板**——全局关掉后任何人格都不能重新打开;
|
|
29
|
+
* 否则人格 `followUpMode` > 全局 `mode` > 缺省 `llm`。默认助手(persona 为
|
|
30
|
+
* undefined)用 `branding.defaultPersona` 兜底;该人格不在 personas 表里时回落全局。
|
|
31
|
+
* 服务端 `/api/suggestions` 用同一套优先级复核,两边必须一致。
|
|
32
|
+
*/
|
|
33
|
+
export function resolveFollowUpMode(branding, persona) {
|
|
34
|
+
const globalMode = isMode(branding.ui?.followUp?.mode) ? branding.ui.followUp.mode : "llm";
|
|
35
|
+
if (globalMode === "off")
|
|
36
|
+
return "off";
|
|
37
|
+
const id = persona ?? branding.defaultPersona;
|
|
38
|
+
const perPersona = id ? branding.personas?.[id]?.followUpMode : undefined;
|
|
39
|
+
return isMode(perPersona) ? perPersona : globalMode;
|
|
40
|
+
}
|
|
41
|
+
/** 只留文本 part(图片/附件/工具细节不出前端),再套单条与总量上限。 */
|
|
42
|
+
function clipForRequest(messages, maxMessages) {
|
|
43
|
+
const kept = [];
|
|
44
|
+
for (const message of messages) {
|
|
45
|
+
if (message.role !== "user" && message.role !== "assistant")
|
|
46
|
+
continue;
|
|
47
|
+
for (const part of message.content ?? []) {
|
|
48
|
+
if (part?.type !== "text" || typeof part.text !== "string")
|
|
49
|
+
continue;
|
|
50
|
+
const text = part.text.trim();
|
|
51
|
+
if (!text)
|
|
52
|
+
continue;
|
|
53
|
+
kept.push({ role: message.role, text: text.slice(0, FOLLOW_UP_DEFAULTS.messageChars) });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const window = kept.slice(-Math.max(1, maxMessages));
|
|
57
|
+
let total = 0;
|
|
58
|
+
const out = [];
|
|
59
|
+
for (let i = window.length - 1; i >= 0; i--) {
|
|
60
|
+
total += window[i].text.length;
|
|
61
|
+
if (total > FOLLOW_UP_DEFAULTS.totalChars)
|
|
62
|
+
break;
|
|
63
|
+
out.unshift(window[i]);
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
/** `off` 不产生 adapter(库不生成建议);`static` 本地返回、`llm` 请求服务端。 */
|
|
68
|
+
export function createFollowUpAdapter(cfg, mode, personaId) {
|
|
69
|
+
if (mode === "static") {
|
|
70
|
+
const suggestions = cfg.staticFallback.slice(0, cfg.count).map((prompt) => ({ prompt }));
|
|
71
|
+
return { generate: () => Promise.resolve(suggestions) };
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
async generate({ messages, signal }) {
|
|
75
|
+
try {
|
|
76
|
+
// 不用 postJson(不带 signal):这里的 abort 由库的 suggestion controller 驱动。
|
|
77
|
+
const res = await fetch("/api/suggestions", {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: { "content-type": "application/json", ...apiHeaders(auth.token) },
|
|
80
|
+
body: JSON.stringify({ messages: clipForRequest(messages, cfg.maxMessages), persona: personaId }),
|
|
81
|
+
signal,
|
|
82
|
+
});
|
|
83
|
+
if (!res.ok)
|
|
84
|
+
return [];
|
|
85
|
+
const data = (await res.json());
|
|
86
|
+
if (!Array.isArray(data.suggestions))
|
|
87
|
+
return [];
|
|
88
|
+
return data.suggestions
|
|
89
|
+
.filter((s) => typeof s === "string" && s.trim() !== "")
|
|
90
|
+
.slice(0, cfg.count)
|
|
91
|
+
.map((prompt) => ({ prompt }));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 轮次结束后的「下一步建议」条。数据由库的 suggestion adapter 生成(run 结束自动
|
|
3
|
+
* 触发、run 开始自动清空),这里只负责渲染。必须渲染在 `ThreadPrimitive.Root` 内。
|
|
4
|
+
*
|
|
5
|
+
* `ThreadPrimitive.Suggestions` 本身不渲染包裹元素(空建议时返回 null),所以这里
|
|
6
|
+
* 提供一层 `.ac-followup` 容器;容器空时由 CSS `:empty` 收起,不留空白条。
|
|
7
|
+
* 点击即发送(`send`),与空态快捷问法 chips 的行为一致。
|
|
8
|
+
*/
|
|
9
|
+
export declare function FollowUpSuggestions(): import("react").JSX.Element;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { ThreadPrimitive } from "@assistant-ui/react";
|
|
3
|
+
/**
|
|
4
|
+
* 轮次结束后的「下一步建议」条。数据由库的 suggestion adapter 生成(run 结束自动
|
|
5
|
+
* 触发、run 开始自动清空),这里只负责渲染。必须渲染在 `ThreadPrimitive.Root` 内。
|
|
6
|
+
*
|
|
7
|
+
* `ThreadPrimitive.Suggestions` 本身不渲染包裹元素(空建议时返回 null),所以这里
|
|
8
|
+
* 提供一层 `.ac-followup` 容器;容器空时由 CSS `:empty` 收起,不留空白条。
|
|
9
|
+
* 点击即发送(`send`),与空态快捷问法 chips 的行为一致。
|
|
10
|
+
*/
|
|
11
|
+
export function FollowUpSuggestions() {
|
|
12
|
+
return (_jsx("div", { className: "ac-followup", children: _jsx(ThreadPrimitive.Suggestions, { children: ({ suggestion }) => (_jsx(ThreadPrimitive.Suggestion, { className: "ac-followup-chip", prompt: suggestion.prompt, send: true, title: suggestion.prompt, children: suggestion.title })) }) }));
|
|
13
|
+
}
|
package/dist/ui/Icon.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { parseIconSpec } from "./icons.js";
|
|
3
|
+
/**
|
|
4
|
+
* 内置 SVG(24×24 线框,stroke 走 currentColor 跟随主题,不引任何图标库)。
|
|
5
|
+
* 用惰性函数而不是模块级 JSX 常量:node 侧测试经 tsx 加载本模块时用的是
|
|
6
|
+
* classic JSX 运行时,模块级 JSX 会在 import 期执行 `React.createElement`
|
|
7
|
+
* 直接炸掉;渲染期执行就没有这个问题(浏览器走 react-jsx 自动运行时)。
|
|
8
|
+
*/
|
|
9
|
+
const PATHS = {
|
|
10
|
+
paperclip: () => (_jsx("path", { d: "M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48" })),
|
|
11
|
+
plus: () => _jsx("path", { d: "M12 5v14M5 12h14" }),
|
|
12
|
+
image: () => (_jsxs(_Fragment, { children: [_jsx("rect", { x: "3", y: "3", width: "18", height: "18", rx: "2", ry: "2" }), _jsx("circle", { cx: "8.5", cy: "8.5", r: "1.5" }), _jsx("polyline", { points: "21 15 16 10 5 21" })] })),
|
|
13
|
+
upload: () => (_jsxs(_Fragment, { children: [_jsx("path", { d: "M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" }), _jsx("polyline", { points: "17 8 12 3 7 8" }), _jsx("line", { x1: "12", y1: "3", x2: "12", y2: "15" })] })),
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* 槽位用的图标渲染器。svg 走白名单 JSX(绝不用 dangerouslySetInnerHTML)、
|
|
17
|
+
* 文本走 React 文本节点、url 走 `<img>`(协议已在 parseIconSpec 校验)。
|
|
18
|
+
*/
|
|
19
|
+
export function Icon({ spec, className = "ac-icon" }) {
|
|
20
|
+
const icon = parseIconSpec(spec);
|
|
21
|
+
if (icon.kind === "url")
|
|
22
|
+
return _jsx("img", { className: className, src: icon.href, alt: "" });
|
|
23
|
+
if (icon.kind === "text")
|
|
24
|
+
return _jsx("span", { className: className, "aria-hidden": "true", children: icon.text });
|
|
25
|
+
return (_jsx("svg", { className: className, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: PATHS[icon.name]() }));
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多 phase 成员卡片的默认渲染(W4 / R-010,S6)。
|
|
3
|
+
*
|
|
4
|
+
* 纯展示组件:只吃 `reduceMembers` 的快照,不写状态、不碰协议细节。
|
|
5
|
+
* 通过 Slots 的 `MemberCards` 槽可整体换皮——产品换外观不重写归集逻辑。
|
|
6
|
+
* 档 1(单 phase)没有 member_* 事件,`members` 为空 ⇒ 整个组件返回 null,
|
|
7
|
+
* 用户看到的气泡与 W4 之前逐字相同(I8 的前端半边)。
|
|
8
|
+
*/
|
|
9
|
+
import type { MemberView } from "./members.ts";
|
|
10
|
+
export interface MemberCardsProps {
|
|
11
|
+
readonly members: readonly MemberView[];
|
|
12
|
+
/** 成员 id → 展示名(来自 branding.personas 的 label);缺省回落到 id。 */
|
|
13
|
+
readonly labels?: Readonly<Record<string, string>>;
|
|
14
|
+
}
|
|
15
|
+
export declare function MemberCards({ members, labels }: MemberCardsProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
const STATE_LABEL = {
|
|
3
|
+
running: "执行中…",
|
|
4
|
+
completed: "✓ 完成",
|
|
5
|
+
failed: "✗ 失败",
|
|
6
|
+
stopped: "已停止",
|
|
7
|
+
blocked: "⛔ 受限",
|
|
8
|
+
};
|
|
9
|
+
export function MemberCards({ members, labels }) {
|
|
10
|
+
if (members.length === 0)
|
|
11
|
+
return null;
|
|
12
|
+
return (_jsx("div", { className: "members", role: "group", "aria-label": "\u534F\u4F5C\u6210\u5458", children: members.map((m) => (_jsxs("div", { className: `member ${m.state}`, children: [_jsxs("div", { className: "member-head", children: [_jsx("span", { className: "member-name", children: labels?.[m.memberId] ?? m.memberId }), _jsx("span", { className: `member-state ${m.state}`, children: STATE_LABEL[m.state] })] }), m.task && _jsx("div", { className: "member-task", children: m.task }), m.text && _jsx("div", { className: "member-text", children: m.text }), m.usage && (_jsxs("div", { className: "member-usage", children: [m.usage.total, " tokens"] }))] }, `${m.phaseId}\u0000${m.memberId}`))) }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图标值解析:`branding.ui.icons.<槽名>` 的一个字符串怎么画。
|
|
3
|
+
* 三种形态:`svg:<内置名>` / `url:https?://…`(图片地址)/ 其它非空字符串(emoji 或短文本)。
|
|
4
|
+
* 任何非法值都回落默认矢量——图标坏了不该让界面报错,也不该引入任何 HTML 注入面。
|
|
5
|
+
*/
|
|
6
|
+
declare const BUILTIN_SVGS: readonly ["paperclip", "plus", "image", "upload"];
|
|
7
|
+
export type BuiltinSvgName = (typeof BUILTIN_SVGS)[number];
|
|
8
|
+
export type IconDescriptor = {
|
|
9
|
+
kind: "svg";
|
|
10
|
+
name: BuiltinSvgName;
|
|
11
|
+
} | {
|
|
12
|
+
kind: "url";
|
|
13
|
+
href: string;
|
|
14
|
+
} | {
|
|
15
|
+
kind: "text";
|
|
16
|
+
text: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function parseIconSpec(spec: string | undefined | null): IconDescriptor;
|
|
19
|
+
export {};
|
package/dist/ui/icons.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图标值解析:`branding.ui.icons.<槽名>` 的一个字符串怎么画。
|
|
3
|
+
* 三种形态:`svg:<内置名>` / `url:https?://…`(图片地址)/ 其它非空字符串(emoji 或短文本)。
|
|
4
|
+
* 任何非法值都回落默认矢量——图标坏了不该让界面报错,也不该引入任何 HTML 注入面。
|
|
5
|
+
*/
|
|
6
|
+
const BUILTIN_SVGS = ["paperclip", "plus", "image", "upload"];
|
|
7
|
+
const DEFAULT_ICON = { kind: "svg", name: "paperclip" };
|
|
8
|
+
const MAX_TEXT = 8;
|
|
9
|
+
export function parseIconSpec(spec) {
|
|
10
|
+
if (spec === undefined || spec === null)
|
|
11
|
+
return DEFAULT_ICON;
|
|
12
|
+
const value = spec.trim();
|
|
13
|
+
if (!value)
|
|
14
|
+
return DEFAULT_ICON;
|
|
15
|
+
if (value.startsWith("svg:")) {
|
|
16
|
+
const name = value.slice(4).trim();
|
|
17
|
+
return BUILTIN_SVGS.includes(name) ? { kind: "svg", name } : DEFAULT_ICON;
|
|
18
|
+
}
|
|
19
|
+
if (value.startsWith("url:")) {
|
|
20
|
+
const href = value.slice(4).trim();
|
|
21
|
+
// 只放行 http(s);相对路径与其它协议一律回落默认(纵深防御,前端兜一道)。
|
|
22
|
+
return /^https?:\/\/\S+$/i.test(href) ? { kind: "url", href } : DEFAULT_ICON;
|
|
23
|
+
}
|
|
24
|
+
return { kind: "text", text: value.length > MAX_TEXT ? value.slice(0, MAX_TEXT) : value };
|
|
25
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多 phase 的成员卡片归集(W4 / R-010,S6):把 SSE 的 `member_*` / `run_done`
|
|
3
|
+
* 事件流折成「谁在跑、各自说了什么、各自花了多少」的状态。
|
|
4
|
+
*
|
|
5
|
+
* 为什么是纯 reducer 而不是塞进 React state:
|
|
6
|
+
* ① 归集逻辑(乱序 done、delta 拼接、runId 切换时重置)是本次最该被测的东西,
|
|
7
|
+
* 纯函数能在**原生 loader**下测(web 测试默认不走 tsx,见 test-stable.mjs);
|
|
8
|
+
* ② adapter.ts 只负责把协议事件分发到 bridge,不认识 UI 形状;
|
|
9
|
+
* ③ UI 组件只渲染快照,不写状态机——与 ConfirmCard / QueueBar 同构。
|
|
10
|
+
*
|
|
11
|
+
* 降级契约:任何不认识的事件类型进 `reduceMembers` 都被安全忽略(返回原状态),
|
|
12
|
+
* 绝不 throw、绝不往成员卡片里塞垃圾。这样平台将来加事件,旧前端不会崩。
|
|
13
|
+
*/
|
|
14
|
+
import type { ChatEvent, TokenUsage } from "@njuptlzf/agent-chat-protocol";
|
|
15
|
+
/** 一个成员的视图状态。 */
|
|
16
|
+
export interface MemberView {
|
|
17
|
+
readonly memberId: string;
|
|
18
|
+
readonly phaseId: string;
|
|
19
|
+
/** running / completed / failed / stopped / blocked。 */
|
|
20
|
+
readonly state: "running" | "completed" | "failed" | "stopped" | "blocked";
|
|
21
|
+
/** 这个成员流式吐出的全部文本(delta 依次拼接)。 */
|
|
22
|
+
readonly text: string;
|
|
23
|
+
/** 交给它的任务文本(member_started 时带)。 */
|
|
24
|
+
readonly task: string;
|
|
25
|
+
/** 该成员自己的 usage;未收口时为空。 */
|
|
26
|
+
readonly usage: TokenUsage | undefined;
|
|
27
|
+
}
|
|
28
|
+
/** 一次 multi-phase Run 的视图快照。 */
|
|
29
|
+
export interface MembersState {
|
|
30
|
+
/** 当前 runId;切换 Run 时整组重置。 */
|
|
31
|
+
readonly runId: string | undefined;
|
|
32
|
+
readonly members: readonly MemberView[];
|
|
33
|
+
/** Run 收口(run_done)。 */
|
|
34
|
+
readonly done: boolean;
|
|
35
|
+
/** Run 级 usage(run_done 带)。 */
|
|
36
|
+
readonly usage: TokenUsage | undefined;
|
|
37
|
+
}
|
|
38
|
+
export declare const emptyMembersState: MembersState;
|
|
39
|
+
/**
|
|
40
|
+
* 折一个事件进状态。**纯函数**:不修改 prev,返回新对象(未命中时原样返回 prev)。
|
|
41
|
+
*/
|
|
42
|
+
export declare function reduceMembers(prev: MembersState, ev: ChatEvent): MembersState;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const emptyMembersState = { runId: undefined, members: [], done: false, usage: undefined };
|
|
2
|
+
const zeroUsage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 };
|
|
3
|
+
function isMemberState(s) {
|
|
4
|
+
return s === "running" || s === "completed" || s === "failed" || s === "stopped" || s === "blocked";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 折一个事件进状态。**纯函数**:不修改 prev,返回新对象(未命中时原样返回 prev)。
|
|
8
|
+
*/
|
|
9
|
+
export function reduceMembers(prev, ev) {
|
|
10
|
+
// 只处理编排事件;其余一律安全忽略(降级契约)。
|
|
11
|
+
if (ev.type !== "member_started" &&
|
|
12
|
+
ev.type !== "member_delta" &&
|
|
13
|
+
ev.type !== "member_done" &&
|
|
14
|
+
ev.type !== "run_done") {
|
|
15
|
+
return prev;
|
|
16
|
+
}
|
|
17
|
+
// 换 Run 重置:一个 session 里可以连续跑多个 flow,每组成员互不串台。
|
|
18
|
+
const base = prev.runId === undefined || prev.runId === ev.runId ? prev : emptyMembersState;
|
|
19
|
+
if (ev.type === "run_done") {
|
|
20
|
+
return { ...base, runId: ev.runId, done: true, usage: ev.usage };
|
|
21
|
+
}
|
|
22
|
+
const key = (m) => `${m.memberId}\u0000${m.phaseId}`;
|
|
23
|
+
const id = `${ev.memberId}\u0000${ev.phaseId}`;
|
|
24
|
+
const at = base.members.findIndex((m) => key(m) === id);
|
|
25
|
+
if (ev.type === "member_started") {
|
|
26
|
+
// started 可能晚于 delta(乱序)或重复到达:保留已有的 text/usage,只刷新
|
|
27
|
+
// task 与状态为 running。绝不用空串覆盖已经收到的字。
|
|
28
|
+
const next = {
|
|
29
|
+
memberId: ev.memberId,
|
|
30
|
+
phaseId: ev.phaseId,
|
|
31
|
+
state: "running",
|
|
32
|
+
text: at === -1 ? "" : base.members[at].text,
|
|
33
|
+
task: ev.task,
|
|
34
|
+
usage: at === -1 ? undefined : base.members[at].usage,
|
|
35
|
+
};
|
|
36
|
+
const members = at === -1 ? [...base.members, next] : base.members.map((m, i) => (i === at ? next : m));
|
|
37
|
+
return { ...base, runId: ev.runId, members };
|
|
38
|
+
}
|
|
39
|
+
// delta / done 若在 started 之前到达(乱序),建一个占位成员再更新——
|
|
40
|
+
// 前端不该因为服务端事件顺序抖动就丢字。
|
|
41
|
+
const cur = at === -1
|
|
42
|
+
? { memberId: ev.memberId, phaseId: ev.phaseId, state: "running", text: "", task: "", usage: undefined }
|
|
43
|
+
: base.members[at];
|
|
44
|
+
const updated = ev.type === "member_delta"
|
|
45
|
+
? { ...cur, text: cur.text + ev.delta }
|
|
46
|
+
: {
|
|
47
|
+
...cur,
|
|
48
|
+
state: isMemberState(ev.state) ? ev.state : "failed",
|
|
49
|
+
usage: ev.usage ?? zeroUsage,
|
|
50
|
+
};
|
|
51
|
+
const members = at === -1 ? [...base.members, updated] : base.members.map((m, i) => (i === at ? updated : m));
|
|
52
|
+
return { ...base, runId: ev.runId, members };
|
|
53
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ComponentType } from "react";
|
|
2
|
+
import { type MemberCardsProps } from "./MemberCards.tsx";
|
|
3
|
+
export interface IconProps {
|
|
4
|
+
/** 图标值(`branding.ui.icons` 里该槽的字符串);缺省回落内置矢量。 */
|
|
5
|
+
spec?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 聊天壳的视觉件槽位注册表:所有视觉件要么是 assistant-ui 原生原语,要么是
|
|
9
|
+
* 这里登记的可替换自定义组件——壳体 JSX 不写死字形、不自造建议状态机。
|
|
10
|
+
*
|
|
11
|
+
* `Slots` / `IconProps` **故意不进公共 barrel**(不新增公开导出符号):产品要
|
|
12
|
+
* 整体换皮时,在 `AgentApp` 的 `slots` 里传结构兼容的组件即可,TS 自动对上。
|
|
13
|
+
* 槽组件渲染在 `AssistantRuntimeProvider`(`FollowUpSuggestions` 还在
|
|
14
|
+
* `ThreadPrimitive.Root`)之内,可直接使用库原语;不得反向 import 平台内部路径。
|
|
15
|
+
*/
|
|
16
|
+
export interface Slots {
|
|
17
|
+
Icon: ComponentType<IconProps>;
|
|
18
|
+
FollowUpSuggestions: ComponentType;
|
|
19
|
+
/**
|
|
20
|
+
* 多 phase 成员卡片视图(W4)。产品可整体换皮;默认实现消费 `reduceMembers`
|
|
21
|
+
* 的快照。档 1 传空数组 ⇒ 默认实现返回 null,零 DOM、零视觉变化。
|
|
22
|
+
*/
|
|
23
|
+
MemberCards: ComponentType<MemberCardsProps>;
|
|
24
|
+
}
|
|
25
|
+
export declare const defaultSlots: Slots;
|
|
26
|
+
export declare const SlotsProvider: import("react").Provider<Slots>;
|
|
27
|
+
export declare function useSlots(): Slots;
|
|
28
|
+
/** 纯函数:默认槽位 + 局部覆盖(未覆盖的槽保持默认),不渲染 DOM 即可单测。 */
|
|
29
|
+
export declare function resolveSlots(overrides?: Partial<Slots>): Slots;
|
package/dist/ui/slots.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
import { Icon } from "./Icon.js";
|
|
3
|
+
import { FollowUpSuggestions } from "./FollowUpSuggestions.js";
|
|
4
|
+
import { MemberCards } from "./MemberCards.js";
|
|
5
|
+
export const defaultSlots = { Icon, FollowUpSuggestions, MemberCards };
|
|
6
|
+
const SlotsContext = createContext(defaultSlots);
|
|
7
|
+
export const SlotsProvider = SlotsContext.Provider;
|
|
8
|
+
export function useSlots() {
|
|
9
|
+
return useContext(SlotsContext);
|
|
10
|
+
}
|
|
11
|
+
/** 纯函数:默认槽位 + 局部覆盖(未覆盖的槽保持默认),不渲染 DOM 即可单测。 */
|
|
12
|
+
export function resolveSlots(overrides) {
|
|
13
|
+
return { ...defaultSlots, ...overrides };
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,72 +1,71 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@njuptlzf/agent-chat-web",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Business-agnostic web chat shell for agentChat products: AgentApp mount, AppDef contract, host panels and HTTP helpers.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "Apache-2.0",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": {
|
|
9
|
-
"agent-chat-source": "./src/agent-chat/index.ts",
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"default": "./dist/index.js"
|
|
12
|
-
},
|
|
13
|
-
"./styles.css": "./dist/styles.css",
|
|
14
|
-
"./app-def-check": {
|
|
15
|
-
"agent-chat-source": "./src/agent-chat/app-def-check.ts",
|
|
16
|
-
"types": "./dist/app-def-check.d.ts",
|
|
17
|
-
"default": "./dist/app-def-check.js"
|
|
18
|
-
},
|
|
19
|
-
"./package.json": "./package.json"
|
|
20
|
-
},
|
|
21
|
-
"files": [
|
|
22
|
-
"dist",
|
|
23
|
-
"src"
|
|
24
|
-
],
|
|
25
|
-
"repository": {
|
|
26
|
-
"type": "git",
|
|
27
|
-
"url": "git+https://github.com/njuptlzf/agent-chat.git",
|
|
28
|
-
"directory": "apps/web"
|
|
29
|
-
},
|
|
30
|
-
"bugs": {
|
|
31
|
-
"url": "https://github.com/njuptlzf/agent-chat/issues"
|
|
32
|
-
},
|
|
33
|
-
"homepage": "https://github.com/njuptlzf/agent-chat#readme",
|
|
34
|
-
"keywords": [
|
|
35
|
-
"agent-chat",
|
|
36
|
-
"chat-ui",
|
|
37
|
-
"react",
|
|
38
|
-
"vite"
|
|
39
|
-
],
|
|
40
|
-
"engines": {
|
|
41
|
-
"node": ">=22.
|
|
42
|
-
},
|
|
43
|
-
"publishConfig": {
|
|
44
|
-
"access": "public"
|
|
45
|
-
},
|
|
46
|
-
"scripts": {
|
|
47
|
-
"dev": "vite",
|
|
48
|
-
"build": "vite build",
|
|
49
|
-
"typecheck": "tsc --noEmit",
|
|
50
|
-
"preview": "vite preview"
|
|
51
|
-
},
|
|
52
|
-
"dependencies": {
|
|
53
|
-
"@njuptlzf/agent-chat-protocol": "^0.
|
|
54
|
-
"@assistant-ui/react": "^0.15.28",
|
|
55
|
-
"@assistant-ui/react-markdown": "^0.14.22",
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"react": "^19.0.0"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"@types/react": "^19.0.0",
|
|
65
|
-
"@
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@njuptlzf/agent-chat-web",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Business-agnostic web chat shell for agentChat products: AgentApp mount, AppDef contract, host panels and HTTP helpers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"agent-chat-source": "./src/agent-chat/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./styles.css": "./dist/styles.css",
|
|
14
|
+
"./app-def-check": {
|
|
15
|
+
"agent-chat-source": "./src/agent-chat/app-def-check.ts",
|
|
16
|
+
"types": "./dist/app-def-check.d.ts",
|
|
17
|
+
"default": "./dist/app-def-check.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/njuptlzf/agent-chat.git",
|
|
28
|
+
"directory": "apps/web"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/njuptlzf/agent-chat/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/njuptlzf/agent-chat#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"agent-chat",
|
|
36
|
+
"chat-ui",
|
|
37
|
+
"react",
|
|
38
|
+
"vite"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.15"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"dev": "vite",
|
|
48
|
+
"build": "vite build",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"preview": "vite preview"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@njuptlzf/agent-chat-protocol": "^0.4.0",
|
|
54
|
+
"@assistant-ui/react": "^0.15.28",
|
|
55
|
+
"@assistant-ui/react-markdown": "^0.14.22",
|
|
56
|
+
"exceljs": "^4.4.0",
|
|
57
|
+
"mammoth": "^1.13.0",
|
|
58
|
+
"pdfjs-dist": "^6.4.299",
|
|
59
|
+
"react": "^19.0.0",
|
|
60
|
+
"react-dom": "^19.0.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@types/react": "^19.0.0",
|
|
64
|
+
"@types/react-dom": "^19.0.0",
|
|
65
|
+
"@vitejs/plugin-react": "^5.0.0",
|
|
66
|
+
"typescript": "^5.9.0",
|
|
67
|
+
"vite": "^7.0.0"
|
|
68
|
+
},
|
|
69
|
+
"main": "./dist/index.js",
|
|
70
|
+
"types": "./dist/index.d.ts"
|
|
71
|
+
}
|
package/src/App.tsx
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 前端入口:**这里只剩选择产品**。
|
|
3
|
-
*
|
|
4
|
-
* 产品的事实(导航注册表、文案、侧栏入口、页面)住在 `apps/web/src/<产品>/entry.tsx`,
|
|
5
|
-
* 机械接线在 core 的 `agent-chat/mount.tsx`。换产品 = 设 `VITE_PRODUCT=<目录名>`
|
|
6
|
-
* 重新构建,**不用改这个文件**。
|
|
7
|
-
*
|
|
8
|
-
* `@product` 由 vite 的 alias 解析(默认产品见 apps/web/products.ts 的 DEFAULT_PRODUCT);
|
|
9
|
-
* tsc 侧由 tsconfig 的 paths 指向默认产品(所有 entry 都在 src 下,都会被类型检查)。
|
|
10
|
-
*/
|
|
11
|
-
import { App } from "@product";
|
|
12
|
-
|
|
13
|
-
export default App;
|
|
1
|
+
/**
|
|
2
|
+
* 前端入口:**这里只剩选择产品**。
|
|
3
|
+
*
|
|
4
|
+
* 产品的事实(导航注册表、文案、侧栏入口、页面)住在 `apps/web/src/<产品>/entry.tsx`,
|
|
5
|
+
* 机械接线在 core 的 `agent-chat/mount.tsx`。换产品 = 设 `VITE_PRODUCT=<目录名>`
|
|
6
|
+
* 重新构建,**不用改这个文件**。
|
|
7
|
+
*
|
|
8
|
+
* `@product` 由 vite 的 alias 解析(默认产品见 apps/web/products.ts 的 DEFAULT_PRODUCT);
|
|
9
|
+
* tsc 侧由 tsconfig 的 paths 指向默认产品(所有 entry 都在 src 下,都会被类型检查)。
|
|
10
|
+
*/
|
|
11
|
+
import { App } from "@product";
|
|
12
|
+
|
|
13
|
+
export default App;
|