@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/src/agent-chat/docs.ts
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser-side document -> Markdown conversion (批 B4-A).
|
|
3
|
-
*
|
|
4
|
-
* Everything we send the model must be text, so the browser converts the
|
|
5
|
-
* user's file and the result travels as `ChatRequest.attachments`. The server
|
|
6
|
-
* parses nothing — that is the design decision this module exists for
|
|
7
|
-
* (the B5 design doc in docs/ records the server-side alternative).
|
|
8
|
-
*
|
|
9
|
-
* Accepted fidelity loss (user's call): text layers of pdf/docx/xlsx are
|
|
10
|
-
* extracted roughly linearly; layout, complex tables and scanned pages come
|
|
11
|
-
* out degraded, and an empty extraction is surfaced as an error instead of
|
|
12
|
-
* being sent. The heavy parsers are dynamically imported so the base bundle
|
|
13
|
-
* stays lean.
|
|
14
|
-
*/
|
|
15
|
-
import type { ChatAttachment } from "@njuptlzf/agent-chat-protocol";
|
|
16
|
-
|
|
17
|
-
export const MAX_DOCS = 4;
|
|
18
|
-
/** Keep in sync with the server's MAX_ATTACHMENT_CHARS; the server is the gate. */
|
|
19
|
-
export const MAX_DOC_CHARS = 24_000;
|
|
20
|
-
|
|
21
|
-
export type DocKind = "text" | "csv" | "pdf" | "docx" | "xlsx";
|
|
22
|
-
|
|
23
|
-
const EXT_KIND: Record<string, DocKind> = {
|
|
24
|
-
md: "text", markdown: "text", txt: "text", log: "text", json: "text",
|
|
25
|
-
yaml: "text", yml: "text", toml: "text", ini: "text",
|
|
26
|
-
csv: "csv", tsv: "csv",
|
|
27
|
-
pdf: "pdf",
|
|
28
|
-
docx: "docx",
|
|
29
|
-
xlsx: "xlsx", xlsm: "xlsx",
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export function docKind(fileName: string): DocKind | undefined {
|
|
33
|
-
const dot = fileName.lastIndexOf(".");
|
|
34
|
-
if (dot < 0) return undefined;
|
|
35
|
-
return EXT_KIND[fileName.slice(dot + 1).toLowerCase()];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** An attachment plus what the user needs to know about its conversion. */
|
|
39
|
-
export interface DocEntry extends ChatAttachment {
|
|
40
|
-
truncated: boolean;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Hard cap with a human-readable marker. The result stays under `cap` chars
|
|
45
|
-
* (marker included) so a client-capped document never trips the server check.
|
|
46
|
-
*/
|
|
47
|
-
export function truncateDoc(text: string, cap = MAX_DOC_CHARS): { text: string; truncated: boolean } {
|
|
48
|
-
const clean = text.trim();
|
|
49
|
-
if (clean.length <= cap) return { text: clean, truncated: false };
|
|
50
|
-
const keep = cap - 100;
|
|
51
|
-
return {
|
|
52
|
-
text: `${clean.slice(0, keep)}\n\n…[已截断:原文 ${clean.length} 字符,只上传了前 ${keep} 字符]`,
|
|
53
|
-
truncated: true,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The file-part carrier for the local runtime. The adapter re-reads these
|
|
59
|
-
* back into ChatRequest.attachments by mimeType, so "text/markdown" here
|
|
60
|
-
* means "converted document text" (data is the Markdown itself, not base64).
|
|
61
|
-
*/
|
|
62
|
-
export function docPart(doc: ChatAttachment): { type: "file"; filename: string; mimeType: string; data: string } {
|
|
63
|
-
return { type: "file", filename: doc.name, mimeType: "text/markdown", data: doc.text };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Rows -> Markdown table; every cell escaped so no pipe can forge a column. */
|
|
67
|
-
export function tableFromRows(rows: string[][]): string {
|
|
68
|
-
if (rows.length === 0) return "";
|
|
69
|
-
const esc = (v: string) => v.trim().replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
70
|
-
const width = rows.reduce((m, r) => Math.max(m, r.length), 0);
|
|
71
|
-
const grid = rows.map((r) => Array.from({ length: width }, (_, i) => esc(r[i] ?? "")));
|
|
72
|
-
const line = (cells: string[]) => `| ${cells.join(" | ")} |`;
|
|
73
|
-
return [line(grid[0]!), line(grid[0]!.map(() => "---")), ...grid.slice(1).map(line)].join("\n");
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Minimal quote-aware delimited-text parser with first-line delimiter sniffing. */
|
|
77
|
-
function parseDelimited(text: string): string[][] {
|
|
78
|
-
const src = text.replace(/\r\n?/g, "\n").replace(/\n$/, "");
|
|
79
|
-
const nl = src.indexOf("\n");
|
|
80
|
-
const firstLine = nl < 0 ? src : src.slice(0, nl);
|
|
81
|
-
const count = (d: string) => firstLine.split(d).length;
|
|
82
|
-
const delim = [",", "\t", ";"].reduce((a, b) => (count(b) > count(a) ? b : a), ",");
|
|
83
|
-
const rows: string[][] = [];
|
|
84
|
-
let row: string[] = [];
|
|
85
|
-
let field = "";
|
|
86
|
-
let quoted = false;
|
|
87
|
-
for (let i = 0; i < src.length; i++) {
|
|
88
|
-
const c = src[i]!;
|
|
89
|
-
if (quoted) {
|
|
90
|
-
if (c === '"') {
|
|
91
|
-
if (src[i + 1] === '"') { field += '"'; i++; } else quoted = false;
|
|
92
|
-
} else field += c;
|
|
93
|
-
} else if (c === '"') quoted = true;
|
|
94
|
-
else if (c === delim) { row.push(field); field = ""; }
|
|
95
|
-
else if (c === "\n") { row.push(field); rows.push(row); row = []; field = ""; }
|
|
96
|
-
else field += c;
|
|
97
|
-
}
|
|
98
|
-
row.push(field);
|
|
99
|
-
rows.push(row);
|
|
100
|
-
return rows.filter((r) => r.some((f) => f.trim() !== ""));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function csvToMarkdown(text: string): string {
|
|
104
|
-
return tableFromRows(parseDelimited(text));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
let pdfWorkerConfigured = false;
|
|
108
|
-
|
|
109
|
-
async function pdfToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
110
|
-
const pdfjs = await import("pdfjs-dist");
|
|
111
|
-
if (!pdfWorkerConfigured) {
|
|
112
|
-
pdfWorkerConfigured = true;
|
|
113
|
-
const { default: workerUrl } = await import("pdfjs-dist/build/pdf.worker.min.mjs?url");
|
|
114
|
-
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
115
|
-
}
|
|
116
|
-
const doc = await pdfjs.getDocument({ data: new Uint8Array(data) }).promise;
|
|
117
|
-
const pages: string[] = [];
|
|
118
|
-
for (let i = 1; i <= doc.numPages; i++) {
|
|
119
|
-
const content = await (await doc.getPage(i)).getTextContent();
|
|
120
|
-
let text = "";
|
|
121
|
-
let lastY: number | undefined;
|
|
122
|
-
for (const item of content.items) {
|
|
123
|
-
if (!("str" in item)) continue;
|
|
124
|
-
const y = item.transform[5];
|
|
125
|
-
if (lastY !== undefined && Math.abs(y - lastY) > 4) text += "\n";
|
|
126
|
-
text += item.str;
|
|
127
|
-
lastY = y;
|
|
128
|
-
}
|
|
129
|
-
pages.push(doc.numPages > 1 ? `【第 ${i} 页】\n${text.trim()}` : text.trim());
|
|
130
|
-
}
|
|
131
|
-
return pages.join("\n\n");
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function docxToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
135
|
-
const mammoth = (await import("mammoth")).default;
|
|
136
|
-
// The shipped lib/index.d.ts has not caught up with the runtime:
|
|
137
|
-
// convertToMarkdown exists (lib/index.js:14) but is not declared on the
|
|
138
|
-
// interface. Local cast instead of patching global types.
|
|
139
|
-
const convert = (mammoth as unknown as {
|
|
140
|
-
convertToMarkdown: (input: { arrayBuffer: ArrayBuffer }) => Promise<{ value: string }>;
|
|
141
|
-
}).convertToMarkdown;
|
|
142
|
-
const result = await convert({ arrayBuffer: data });
|
|
143
|
-
return result.value;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async function xlsxToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
147
|
-
const { Workbook } = await import("exceljs");
|
|
148
|
-
const wb = new Workbook();
|
|
149
|
-
await wb.xlsx.load(data);
|
|
150
|
-
const parts: string[] = [];
|
|
151
|
-
for (const ws of wb.worksheets) {
|
|
152
|
-
const rows: string[][] = [];
|
|
153
|
-
ws.eachRow({ includeEmpty: false }, (row) => {
|
|
154
|
-
const cells: string[] = [];
|
|
155
|
-
row.eachCell({ includeEmpty: true }, (cell) => cells.push(cell.text));
|
|
156
|
-
rows.push(cells);
|
|
157
|
-
});
|
|
158
|
-
if (rows.length > 0) parts.push(`【工作表 ${ws.name}】\n${tableFromRows(rows)}`);
|
|
159
|
-
}
|
|
160
|
-
return parts.join("\n\n");
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Convert one browser File to a sendable attachment. Throws with a user-readable message. */
|
|
164
|
-
export async function fileToDoc(file: File): Promise<DocEntry> {
|
|
165
|
-
const kind = docKind(file.name);
|
|
166
|
-
if (!kind) throw new Error(`暂不支持的文件类型:${file.name}`);
|
|
167
|
-
let raw: string;
|
|
168
|
-
switch (kind) {
|
|
169
|
-
case "text": raw = await file.text(); break;
|
|
170
|
-
case "csv": raw = csvToMarkdown(await file.text()); break;
|
|
171
|
-
case "pdf": raw = await pdfToMarkdown(await file.arrayBuffer()); break;
|
|
172
|
-
case "docx": raw = await docxToMarkdown(await file.arrayBuffer()); break;
|
|
173
|
-
case "xlsx": raw = await xlsxToMarkdown(await file.arrayBuffer()); break;
|
|
174
|
-
}
|
|
175
|
-
// Empty extraction (scanned pdf, empty file) must not silently reach the model.
|
|
176
|
-
if (raw.trim() === "") throw new Error(`未检出可提取文本(扫描件或空文件?):${file.name}`);
|
|
177
|
-
const t = truncateDoc(raw);
|
|
178
|
-
return { name: file.name, text: t.text, truncated: t.truncated };
|
|
179
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Browser-side document -> Markdown conversion (批 B4-A).
|
|
3
|
+
*
|
|
4
|
+
* Everything we send the model must be text, so the browser converts the
|
|
5
|
+
* user's file and the result travels as `ChatRequest.attachments`. The server
|
|
6
|
+
* parses nothing — that is the design decision this module exists for
|
|
7
|
+
* (the B5 design doc in docs/ records the server-side alternative).
|
|
8
|
+
*
|
|
9
|
+
* Accepted fidelity loss (user's call): text layers of pdf/docx/xlsx are
|
|
10
|
+
* extracted roughly linearly; layout, complex tables and scanned pages come
|
|
11
|
+
* out degraded, and an empty extraction is surfaced as an error instead of
|
|
12
|
+
* being sent. The heavy parsers are dynamically imported so the base bundle
|
|
13
|
+
* stays lean.
|
|
14
|
+
*/
|
|
15
|
+
import type { ChatAttachment } from "@njuptlzf/agent-chat-protocol";
|
|
16
|
+
|
|
17
|
+
export const MAX_DOCS = 4;
|
|
18
|
+
/** Keep in sync with the server's MAX_ATTACHMENT_CHARS; the server is the gate. */
|
|
19
|
+
export const MAX_DOC_CHARS = 24_000;
|
|
20
|
+
|
|
21
|
+
export type DocKind = "text" | "csv" | "pdf" | "docx" | "xlsx";
|
|
22
|
+
|
|
23
|
+
const EXT_KIND: Record<string, DocKind> = {
|
|
24
|
+
md: "text", markdown: "text", txt: "text", log: "text", json: "text",
|
|
25
|
+
yaml: "text", yml: "text", toml: "text", ini: "text",
|
|
26
|
+
csv: "csv", tsv: "csv",
|
|
27
|
+
pdf: "pdf",
|
|
28
|
+
docx: "docx",
|
|
29
|
+
xlsx: "xlsx", xlsm: "xlsx",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function docKind(fileName: string): DocKind | undefined {
|
|
33
|
+
const dot = fileName.lastIndexOf(".");
|
|
34
|
+
if (dot < 0) return undefined;
|
|
35
|
+
return EXT_KIND[fileName.slice(dot + 1).toLowerCase()];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** An attachment plus what the user needs to know about its conversion. */
|
|
39
|
+
export interface DocEntry extends ChatAttachment {
|
|
40
|
+
truncated: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hard cap with a human-readable marker. The result stays under `cap` chars
|
|
45
|
+
* (marker included) so a client-capped document never trips the server check.
|
|
46
|
+
*/
|
|
47
|
+
export function truncateDoc(text: string, cap = MAX_DOC_CHARS): { text: string; truncated: boolean } {
|
|
48
|
+
const clean = text.trim();
|
|
49
|
+
if (clean.length <= cap) return { text: clean, truncated: false };
|
|
50
|
+
const keep = cap - 100;
|
|
51
|
+
return {
|
|
52
|
+
text: `${clean.slice(0, keep)}\n\n…[已截断:原文 ${clean.length} 字符,只上传了前 ${keep} 字符]`,
|
|
53
|
+
truncated: true,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The file-part carrier for the local runtime. The adapter re-reads these
|
|
59
|
+
* back into ChatRequest.attachments by mimeType, so "text/markdown" here
|
|
60
|
+
* means "converted document text" (data is the Markdown itself, not base64).
|
|
61
|
+
*/
|
|
62
|
+
export function docPart(doc: ChatAttachment): { type: "file"; filename: string; mimeType: string; data: string } {
|
|
63
|
+
return { type: "file", filename: doc.name, mimeType: "text/markdown", data: doc.text };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Rows -> Markdown table; every cell escaped so no pipe can forge a column. */
|
|
67
|
+
export function tableFromRows(rows: string[][]): string {
|
|
68
|
+
if (rows.length === 0) return "";
|
|
69
|
+
const esc = (v: string) => v.trim().replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
70
|
+
const width = rows.reduce((m, r) => Math.max(m, r.length), 0);
|
|
71
|
+
const grid = rows.map((r) => Array.from({ length: width }, (_, i) => esc(r[i] ?? "")));
|
|
72
|
+
const line = (cells: string[]) => `| ${cells.join(" | ")} |`;
|
|
73
|
+
return [line(grid[0]!), line(grid[0]!.map(() => "---")), ...grid.slice(1).map(line)].join("\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Minimal quote-aware delimited-text parser with first-line delimiter sniffing. */
|
|
77
|
+
function parseDelimited(text: string): string[][] {
|
|
78
|
+
const src = text.replace(/\r\n?/g, "\n").replace(/\n$/, "");
|
|
79
|
+
const nl = src.indexOf("\n");
|
|
80
|
+
const firstLine = nl < 0 ? src : src.slice(0, nl);
|
|
81
|
+
const count = (d: string) => firstLine.split(d).length;
|
|
82
|
+
const delim = [",", "\t", ";"].reduce((a, b) => (count(b) > count(a) ? b : a), ",");
|
|
83
|
+
const rows: string[][] = [];
|
|
84
|
+
let row: string[] = [];
|
|
85
|
+
let field = "";
|
|
86
|
+
let quoted = false;
|
|
87
|
+
for (let i = 0; i < src.length; i++) {
|
|
88
|
+
const c = src[i]!;
|
|
89
|
+
if (quoted) {
|
|
90
|
+
if (c === '"') {
|
|
91
|
+
if (src[i + 1] === '"') { field += '"'; i++; } else quoted = false;
|
|
92
|
+
} else field += c;
|
|
93
|
+
} else if (c === '"') quoted = true;
|
|
94
|
+
else if (c === delim) { row.push(field); field = ""; }
|
|
95
|
+
else if (c === "\n") { row.push(field); rows.push(row); row = []; field = ""; }
|
|
96
|
+
else field += c;
|
|
97
|
+
}
|
|
98
|
+
row.push(field);
|
|
99
|
+
rows.push(row);
|
|
100
|
+
return rows.filter((r) => r.some((f) => f.trim() !== ""));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function csvToMarkdown(text: string): string {
|
|
104
|
+
return tableFromRows(parseDelimited(text));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let pdfWorkerConfigured = false;
|
|
108
|
+
|
|
109
|
+
async function pdfToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
110
|
+
const pdfjs = await import("pdfjs-dist");
|
|
111
|
+
if (!pdfWorkerConfigured) {
|
|
112
|
+
pdfWorkerConfigured = true;
|
|
113
|
+
const { default: workerUrl } = await import("pdfjs-dist/build/pdf.worker.min.mjs?url");
|
|
114
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
115
|
+
}
|
|
116
|
+
const doc = await pdfjs.getDocument({ data: new Uint8Array(data) }).promise;
|
|
117
|
+
const pages: string[] = [];
|
|
118
|
+
for (let i = 1; i <= doc.numPages; i++) {
|
|
119
|
+
const content = await (await doc.getPage(i)).getTextContent();
|
|
120
|
+
let text = "";
|
|
121
|
+
let lastY: number | undefined;
|
|
122
|
+
for (const item of content.items) {
|
|
123
|
+
if (!("str" in item)) continue;
|
|
124
|
+
const y = item.transform[5];
|
|
125
|
+
if (lastY !== undefined && Math.abs(y - lastY) > 4) text += "\n";
|
|
126
|
+
text += item.str;
|
|
127
|
+
lastY = y;
|
|
128
|
+
}
|
|
129
|
+
pages.push(doc.numPages > 1 ? `【第 ${i} 页】\n${text.trim()}` : text.trim());
|
|
130
|
+
}
|
|
131
|
+
return pages.join("\n\n");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function docxToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
135
|
+
const mammoth = (await import("mammoth")).default;
|
|
136
|
+
// The shipped lib/index.d.ts has not caught up with the runtime:
|
|
137
|
+
// convertToMarkdown exists (lib/index.js:14) but is not declared on the
|
|
138
|
+
// interface. Local cast instead of patching global types.
|
|
139
|
+
const convert = (mammoth as unknown as {
|
|
140
|
+
convertToMarkdown: (input: { arrayBuffer: ArrayBuffer }) => Promise<{ value: string }>;
|
|
141
|
+
}).convertToMarkdown;
|
|
142
|
+
const result = await convert({ arrayBuffer: data });
|
|
143
|
+
return result.value;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function xlsxToMarkdown(data: ArrayBuffer): Promise<string> {
|
|
147
|
+
const { Workbook } = await import("exceljs");
|
|
148
|
+
const wb = new Workbook();
|
|
149
|
+
await wb.xlsx.load(data);
|
|
150
|
+
const parts: string[] = [];
|
|
151
|
+
for (const ws of wb.worksheets) {
|
|
152
|
+
const rows: string[][] = [];
|
|
153
|
+
ws.eachRow({ includeEmpty: false }, (row) => {
|
|
154
|
+
const cells: string[] = [];
|
|
155
|
+
row.eachCell({ includeEmpty: true }, (cell) => cells.push(cell.text));
|
|
156
|
+
rows.push(cells);
|
|
157
|
+
});
|
|
158
|
+
if (rows.length > 0) parts.push(`【工作表 ${ws.name}】\n${tableFromRows(rows)}`);
|
|
159
|
+
}
|
|
160
|
+
return parts.join("\n\n");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Convert one browser File to a sendable attachment. Throws with a user-readable message. */
|
|
164
|
+
export async function fileToDoc(file: File): Promise<DocEntry> {
|
|
165
|
+
const kind = docKind(file.name);
|
|
166
|
+
if (!kind) throw new Error(`暂不支持的文件类型:${file.name}`);
|
|
167
|
+
let raw: string;
|
|
168
|
+
switch (kind) {
|
|
169
|
+
case "text": raw = await file.text(); break;
|
|
170
|
+
case "csv": raw = csvToMarkdown(await file.text()); break;
|
|
171
|
+
case "pdf": raw = await pdfToMarkdown(await file.arrayBuffer()); break;
|
|
172
|
+
case "docx": raw = await docxToMarkdown(await file.arrayBuffer()); break;
|
|
173
|
+
case "xlsx": raw = await xlsxToMarkdown(await file.arrayBuffer()); break;
|
|
174
|
+
}
|
|
175
|
+
// Empty extraction (scanned pdf, empty file) must not silently reach the model.
|
|
176
|
+
if (raw.trim() === "") throw new Error(`未检出可提取文本(扫描件或空文件?):${file.name}`);
|
|
177
|
+
const t = truncateDoc(raw);
|
|
178
|
+
return { name: file.name, text: t.text, truncated: t.truncated };
|
|
179
|
+
}
|
package/src/agent-chat/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@njuptlzf/agent-chat-web` 的公共入口。
|
|
3
|
-
*
|
|
4
|
-
* 平台前端壳对外只有《[PUBLIC_API](../../../../docs/design/PUBLIC_API.md)》
|
|
5
|
-
* 登记的 13 项:产品 web 通过 `@njuptlzf/agent-chat-web` 取 `AppDef` / `HostCtx` /
|
|
6
|
-
* `AgentApp` 与几个 HTTP 小工具,不再走 `apps/web/src/agent-chat/*` 深路径。
|
|
7
|
-
* 壳内部(卡片槽、AppShell、面板)都是实现,随时可改。
|
|
8
|
-
*/
|
|
9
|
-
export type { AppDef, HostCtx } from "./app.tsx";
|
|
10
|
-
export { apiHeaders, auth } from "./auth.ts";
|
|
11
|
-
export { sendToChat } from "./dock.ts";
|
|
12
|
-
export { AgentApp } from "./mount.tsx";
|
|
13
|
-
export { default as PersonasPage } from "./PersonasPage.tsx";
|
|
14
|
-
export { isEnabled } from "./qual.ts";
|
|
15
|
-
export { deleteJson, fetchJson, postJson } from "./sse.ts";
|
|
16
|
-
export { fmtDate, fmtTime, relativeTime } from "./ui/timefmt.ts";
|
|
17
|
-
export { applyThemeMode, loadThemeMode, saveThemeMode, type ThemeMode } from "./theme.ts";
|
|
1
|
+
/**
|
|
2
|
+
* `@njuptlzf/agent-chat-web` 的公共入口。
|
|
3
|
+
*
|
|
4
|
+
* 平台前端壳对外只有《[PUBLIC_API](../../../../docs/design/PUBLIC_API.md)》
|
|
5
|
+
* 登记的 13 项:产品 web 通过 `@njuptlzf/agent-chat-web` 取 `AppDef` / `HostCtx` /
|
|
6
|
+
* `AgentApp` 与几个 HTTP 小工具,不再走 `apps/web/src/agent-chat/*` 深路径。
|
|
7
|
+
* 壳内部(卡片槽、AppShell、面板)都是实现,随时可改。
|
|
8
|
+
*/
|
|
9
|
+
export type { AppDef, HostCtx } from "./app.tsx";
|
|
10
|
+
export { apiHeaders, auth } from "./auth.ts";
|
|
11
|
+
export { sendToChat } from "./dock.ts";
|
|
12
|
+
export { AgentApp } from "./mount.tsx";
|
|
13
|
+
export { default as PersonasPage } from "./PersonasPage.tsx";
|
|
14
|
+
export { isEnabled } from "./qual.ts";
|
|
15
|
+
export { deleteJson, fetchJson, postJson } from "./sse.ts";
|
|
16
|
+
export { fmtDate, fmtTime, relativeTime } from "./ui/timefmt.ts";
|
|
17
|
+
export { applyThemeMode, loadThemeMode, saveThemeMode, type ThemeMode } from "./theme.ts";
|
package/src/agent-chat/mount.tsx
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 组合根的通用实现(接缝的最后一层,2026-11-03)。
|
|
3
|
-
*
|
|
4
|
-
* 以前这份接线长在 `apps/web/src/App.tsx` 里(113 行),产品事实与机械传参
|
|
5
|
-
* 混在一处——第二个产品只能**把整份文件复制走再改 import**。这里把机械部分
|
|
6
|
-
* 收进 core,产品只保留自己的事实(导航注册表、文案兜底、侧栏入口、宿主级组件)。
|
|
7
|
-
*
|
|
8
|
-
* 插槽契约(照抄时最容易踩的一条):
|
|
9
|
-
* `footer` / `extras` 是**普通闭包**,不是组件——它们在 AgentApp 的 render
|
|
10
|
-
* 期间被调用,所以里面**不要调用 hook**(要 state 就在产品自己的组件里
|
|
11
|
-
* 用 useState,闭包里只捕获 setter)。
|
|
12
|
-
*
|
|
13
|
-
* 依赖方向恒为 产品 → agent-chat:本文件不 import 任何产品代码。
|
|
14
|
-
*/
|
|
15
|
-
import type { ReactNode } from "react";
|
|
16
|
-
import type { BrandingResponse } from "@njuptlzf/agent-chat-protocol";
|
|
17
|
-
import AgentChatApp from "./AgentChatApp.tsx";
|
|
18
|
-
import
|
|
19
|
-
import { AppShell, themeVarsOf, useAppHost, type AppDef } from "./app.tsx";
|
|
20
|
-
|
|
21
|
-
/** 宿主状态(`useAppHost` 的返回值):插槽从这里取品牌、身份、路由、人格。 */
|
|
22
|
-
export type AppHost = ReturnType<typeof useAppHost>;
|
|
23
|
-
|
|
24
|
-
/** 产品给的界面文案:全由 branding(manifest 声明)与当前人格决定。 */
|
|
25
|
-
export interface AppTexts {
|
|
26
|
-
/** 坞标题(换人格时要换,否则点了「声明人格」还写着「默认助手」)。 */
|
|
27
|
-
title: string;
|
|
28
|
-
/** 助手在界面上的叫法(「正在回答」这类提示语用)。 */
|
|
29
|
-
assistantLabel: string;
|
|
30
|
-
/** 提示条:这个助手是谁、不做什么。 */
|
|
31
|
-
policyNote?: string;
|
|
32
|
-
welcome?: string;
|
|
33
|
-
suggestions?: string[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface AgentAppProps {
|
|
37
|
-
/** 导航注册表(产品事实)。 */
|
|
38
|
-
def: AppDef;
|
|
39
|
-
/** 文案兜底:core 不认得任何人格名,也不写死任何一句话。 */
|
|
40
|
-
texts(branding: BrandingResponse, persona: string): AppTexts;
|
|
41
|
-
/** 侧栏底部的产品入口(声明人格、反馈问题…)。闭包,不能调 hook。 */
|
|
42
|
-
footer?(host: AppHost): ReactNode;
|
|
43
|
-
/** 宿主级组件(每日总结、反馈抽屉…)。闭包,不能调 hook。 */
|
|
44
|
-
extras?(host: AppHost): ReactNode;
|
|
45
|
-
/** 加载失败时给产品的启动提示(core 不知道启动命令长什么样)。 */
|
|
46
|
-
loadErrorHint?: ReactNode;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 产品组合根:branding/whoami → 宿主状态 → 侧栏 + 坞 + 页面。
|
|
51
|
-
*
|
|
52
|
-
* 与改造前 `App.tsx` 的行为逐项对齐(full/compact 的判据、key 重挂、插槽位置),
|
|
53
|
-
* 差别只有一处:人格不再由 core 判断"谁是声明人格"——`host.persona` 原样传给
|
|
54
|
-
* `AgentChatApp`("" 收敛成 undefined = 产品默认助手),文案由 `texts` 决定。
|
|
55
|
-
*/
|
|
56
|
-
export function AgentApp({ def, texts, footer, extras, loadErrorHint }: AgentAppProps) {
|
|
57
|
-
const host = useAppHost(def);
|
|
58
|
-
if (host.loadError) {
|
|
59
|
-
return (
|
|
60
|
-
<div className="center">
|
|
61
|
-
<p>无法连接服务:{host.loadError}</p>
|
|
62
|
-
{loadErrorHint}
|
|
63
|
-
</div>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
if (!host.ready) return <div className="center">加载中…</div>;
|
|
67
|
-
|
|
68
|
-
const { branding, who, qual, route, shown, onQuals, persona, go, ctx } = host;
|
|
69
|
-
// 坞是否撑满内容区。同一个判据喂给 Dock.full 与 AgentChatApp.compact
|
|
70
|
-
// (2026-11-03:以前 compact 单独判「路由不是聊天页」,漏了选项页这种
|
|
71
|
-
// 「路由还是 chatRoute 但坞不是全屏」的状态,会话侧栏把输入框挤成 56px)。
|
|
72
|
-
const dockFull = !onQuals && route === def.chatRoute;
|
|
73
|
-
const t = texts(branding, persona);
|
|
74
|
-
|
|
75
|
-
return (
|
|
76
|
-
<div className="shell" style={themeVarsOf(branding)}>
|
|
77
|
-
<AppShell
|
|
78
|
-
branding={branding}
|
|
79
|
-
qual={qual}
|
|
80
|
-
route={shown}
|
|
81
|
-
nav={def.navItems(ctx!)}
|
|
82
|
-
who={who}
|
|
83
|
-
// 侧栏导航回聊天路由 = 回**默认人格**(2026-11-04):从声明人格切「默认助手」时只改
|
|
84
|
-
// 路由的话,人格还挂在声明人格上,标题/预备问题都不换。人格化入口(人格化按钮)
|
|
85
|
-
// 走 host.go 自己先 setPersona,不经过这里,所以不会被这条冲掉。
|
|
86
|
-
onRoute={(r) => {
|
|
87
|
-
if (r === def.chatRoute && host.persona) host.setPersona("");
|
|
88
|
-
go(r);
|
|
89
|
-
}}
|
|
90
|
-
onQuals={() => go("quals")}
|
|
91
|
-
switchTitle={def.labels?.switchTitle}
|
|
92
|
-
footer={footer?.(host)}
|
|
93
|
-
>
|
|
94
|
-
<
|
|
95
|
-
title={t.title}
|
|
96
|
-
full={dockFull}
|
|
97
|
-
onClose={host.closeChat}
|
|
98
|
-
chat={
|
|
99
|
-
<AgentChatApp
|
|
100
|
-
// key 让切人格时重挂:会话绑死人格,同一个实例换 persona 只会撞 persona_mismatch。
|
|
101
|
-
key={persona}
|
|
102
|
-
branding={branding}
|
|
103
|
-
scope={qual?.id}
|
|
104
|
-
renderCard={def.cardRenderer}
|
|
105
|
-
// core 只做形状收敛("" → undefined = 产品默认助手),不认识具体名字。
|
|
106
|
-
persona={persona || undefined}
|
|
107
|
-
title={t.title}
|
|
108
|
-
assistantLabel={t.assistantLabel}
|
|
109
|
-
policyNote={t.policyNote}
|
|
110
|
-
welcome={t.welcome}
|
|
111
|
-
suggestions={t.suggestions}
|
|
112
|
-
compact={!dockFull}
|
|
113
|
-
/>
|
|
114
|
-
}
|
|
115
|
-
>
|
|
116
|
-
{def.page(ctx!)}
|
|
117
|
-
</
|
|
118
|
-
{/* 宿主级、只在 boot 拉一次的东西(每日总结弹窗等):切路由不该让它重挂。 */}
|
|
119
|
-
{extras?.(host)}
|
|
120
|
-
</AppShell>
|
|
121
|
-
</div>
|
|
122
|
-
);
|
|
123
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 组合根的通用实现(接缝的最后一层,2026-11-03)。
|
|
3
|
+
*
|
|
4
|
+
* 以前这份接线长在 `apps/web/src/App.tsx` 里(113 行),产品事实与机械传参
|
|
5
|
+
* 混在一处——第二个产品只能**把整份文件复制走再改 import**。这里把机械部分
|
|
6
|
+
* 收进 core,产品只保留自己的事实(导航注册表、文案兜底、侧栏入口、宿主级组件)。
|
|
7
|
+
*
|
|
8
|
+
* 插槽契约(照抄时最容易踩的一条):
|
|
9
|
+
* `footer` / `extras` 是**普通闭包**,不是组件——它们在 AgentApp 的 render
|
|
10
|
+
* 期间被调用,所以里面**不要调用 hook**(要 state 就在产品自己的组件里
|
|
11
|
+
* 用 useState,闭包里只捕获 setter)。
|
|
12
|
+
*
|
|
13
|
+
* 依赖方向恒为 产品 → agent-chat:本文件不 import 任何产品代码。
|
|
14
|
+
*/
|
|
15
|
+
import type { ReactNode } from "react";
|
|
16
|
+
import type { BrandingResponse } from "@njuptlzf/agent-chat-protocol";
|
|
17
|
+
import AgentChatApp from "./AgentChatApp.tsx";
|
|
18
|
+
import DockPanel from "./DockPanel.tsx";
|
|
19
|
+
import { AppShell, themeVarsOf, useAppHost, type AppDef } from "./app.tsx";
|
|
20
|
+
|
|
21
|
+
/** 宿主状态(`useAppHost` 的返回值):插槽从这里取品牌、身份、路由、人格。 */
|
|
22
|
+
export type AppHost = ReturnType<typeof useAppHost>;
|
|
23
|
+
|
|
24
|
+
/** 产品给的界面文案:全由 branding(manifest 声明)与当前人格决定。 */
|
|
25
|
+
export interface AppTexts {
|
|
26
|
+
/** 坞标题(换人格时要换,否则点了「声明人格」还写着「默认助手」)。 */
|
|
27
|
+
title: string;
|
|
28
|
+
/** 助手在界面上的叫法(「正在回答」这类提示语用)。 */
|
|
29
|
+
assistantLabel: string;
|
|
30
|
+
/** 提示条:这个助手是谁、不做什么。 */
|
|
31
|
+
policyNote?: string;
|
|
32
|
+
welcome?: string;
|
|
33
|
+
suggestions?: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AgentAppProps {
|
|
37
|
+
/** 导航注册表(产品事实)。 */
|
|
38
|
+
def: AppDef;
|
|
39
|
+
/** 文案兜底:core 不认得任何人格名,也不写死任何一句话。 */
|
|
40
|
+
texts(branding: BrandingResponse, persona: string): AppTexts;
|
|
41
|
+
/** 侧栏底部的产品入口(声明人格、反馈问题…)。闭包,不能调 hook。 */
|
|
42
|
+
footer?(host: AppHost): ReactNode;
|
|
43
|
+
/** 宿主级组件(每日总结、反馈抽屉…)。闭包,不能调 hook。 */
|
|
44
|
+
extras?(host: AppHost): ReactNode;
|
|
45
|
+
/** 加载失败时给产品的启动提示(core 不知道启动命令长什么样)。 */
|
|
46
|
+
loadErrorHint?: ReactNode;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 产品组合根:branding/whoami → 宿主状态 → 侧栏 + 坞 + 页面。
|
|
51
|
+
*
|
|
52
|
+
* 与改造前 `App.tsx` 的行为逐项对齐(full/compact 的判据、key 重挂、插槽位置),
|
|
53
|
+
* 差别只有一处:人格不再由 core 判断"谁是声明人格"——`host.persona` 原样传给
|
|
54
|
+
* `AgentChatApp`("" 收敛成 undefined = 产品默认助手),文案由 `texts` 决定。
|
|
55
|
+
*/
|
|
56
|
+
export function AgentApp({ def, texts, footer, extras, loadErrorHint }: AgentAppProps) {
|
|
57
|
+
const host = useAppHost(def);
|
|
58
|
+
if (host.loadError) {
|
|
59
|
+
return (
|
|
60
|
+
<div className="center">
|
|
61
|
+
<p>无法连接服务:{host.loadError}</p>
|
|
62
|
+
{loadErrorHint}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (!host.ready) return <div className="center">加载中…</div>;
|
|
67
|
+
|
|
68
|
+
const { branding, who, qual, route, shown, onQuals, persona, go, ctx } = host;
|
|
69
|
+
// 坞是否撑满内容区。同一个判据喂给 Dock.full 与 AgentChatApp.compact
|
|
70
|
+
// (2026-11-03:以前 compact 单独判「路由不是聊天页」,漏了选项页这种
|
|
71
|
+
// 「路由还是 chatRoute 但坞不是全屏」的状态,会话侧栏把输入框挤成 56px)。
|
|
72
|
+
const dockFull = !onQuals && route === def.chatRoute;
|
|
73
|
+
const t = texts(branding, persona);
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<div className="shell" style={themeVarsOf(branding)}>
|
|
77
|
+
<AppShell
|
|
78
|
+
branding={branding}
|
|
79
|
+
qual={qual}
|
|
80
|
+
route={shown}
|
|
81
|
+
nav={def.navItems(ctx!)}
|
|
82
|
+
who={who}
|
|
83
|
+
// 侧栏导航回聊天路由 = 回**默认人格**(2026-11-04):从声明人格切「默认助手」时只改
|
|
84
|
+
// 路由的话,人格还挂在声明人格上,标题/预备问题都不换。人格化入口(人格化按钮)
|
|
85
|
+
// 走 host.go 自己先 setPersona,不经过这里,所以不会被这条冲掉。
|
|
86
|
+
onRoute={(r) => {
|
|
87
|
+
if (r === def.chatRoute && host.persona) host.setPersona("");
|
|
88
|
+
go(r);
|
|
89
|
+
}}
|
|
90
|
+
onQuals={() => go("quals")}
|
|
91
|
+
switchTitle={def.labels?.switchTitle}
|
|
92
|
+
footer={footer?.(host)}
|
|
93
|
+
>
|
|
94
|
+
<DockPanel
|
|
95
|
+
title={t.title}
|
|
96
|
+
full={dockFull}
|
|
97
|
+
onClose={host.closeChat}
|
|
98
|
+
chat={
|
|
99
|
+
<AgentChatApp
|
|
100
|
+
// key 让切人格时重挂:会话绑死人格,同一个实例换 persona 只会撞 persona_mismatch。
|
|
101
|
+
key={persona}
|
|
102
|
+
branding={branding}
|
|
103
|
+
scope={qual?.id}
|
|
104
|
+
renderCard={def.cardRenderer}
|
|
105
|
+
// core 只做形状收敛("" → undefined = 产品默认助手),不认识具体名字。
|
|
106
|
+
persona={persona || undefined}
|
|
107
|
+
title={t.title}
|
|
108
|
+
assistantLabel={t.assistantLabel}
|
|
109
|
+
policyNote={t.policyNote}
|
|
110
|
+
welcome={t.welcome}
|
|
111
|
+
suggestions={t.suggestions}
|
|
112
|
+
compact={!dockFull}
|
|
113
|
+
/>
|
|
114
|
+
}
|
|
115
|
+
>
|
|
116
|
+
{def.page(ctx!)}
|
|
117
|
+
</DockPanel>
|
|
118
|
+
{/* 宿主级、只在 boot 拉一次的东西(每日总结弹窗等):切路由不该让它重挂。 */}
|
|
119
|
+
{extras?.(host)}
|
|
120
|
+
</AppShell>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|