CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.
TypeScript ai-agentsai-diagramai-presentationbomcadcanvasdiagramdiagram-generatordrawioflowchartinfographicspptppt-generatorpresentation-generatorreactsvgedittext-to-diagramtypescriptworkflow
refactor(chat): give the three transcripts one set of message readers
codingFeng1014434062764e7
diff --git a/client/shared/chat/message-content.ts b/client/shared/chat/message-content.tsnew file mode 100644index 0000000..34d2b9d--- /dev/null+++ b/client/shared/chat/message-content.ts@@ -0,0 +1,102 @@+/**+ * Reading the text back out of a chat message.+ *+ * Every workspace's transcript renders the same underlying shape, and every+ * one of them appends uploaded file contents to the user's own words before+ * sending. These are the readers that undo that for display.+ */++export interface MessagePart {+ type: string;+ text?: string;+ toolCallId?: string;+ state?: string;+ input?: unknown;+ output?: unknown;+ errorText?: string;+ [key: string]: unknown;+}++export interface UIMessage {+ id: string;+ role: "user" | "assistant" | "system";+ content?: string | unknown[];+ parts?: MessagePart[];+ [key: string]: unknown;+}++export interface TextSection {+ type: "text" | "file";+ content: string;+ filename?: string;+ fileType?: "pdf" | "text";+ /** Size of the extracted text, shown on the collapsed attachment. */+ charCount?: number;+}++/** Matches the `[PDF: name]` / `[File: name]` blocks the chat panels append. */+const FILE_SECTION = /\[(PDF|File):\s*([^\]]+)\]\n([\s\S]*?)(?=\n\n\[(PDF|File):|$)/g;++/**+ * Splits a message into what the user typed and the file contents that were+ * appended to it, so the transcript can show the files as collapsed+ * attachments instead of walls of extracted text.+ */+export function splitTextIntoFileSections(text: string): TextSection[] {+ const sections: TextSection[] = [];+ const pattern = new RegExp(FILE_SECTION.source, "g");+ let lastIndex = 0;+ let match: RegExpExecArray | null;++ while ((match = pattern.exec(text)) !== null) {+ const beforeText = text.slice(lastIndex, match.index).trim();+ if (beforeText) sections.push({ type: "text", content: beforeText });++ const fileContent = match[3].trim();+ sections.push({+ type: "file",+ content: fileContent,+ filename: match[2].trim(),+ fileType: match[1].toLowerCase() === "pdf" ? "pdf" : "text",+ charCount: fileContent.length,+ });+ lastIndex = match.index + match[0].length;+ }++ const remainingText = text.slice(lastIndex).trim();+ if (remainingText) sections.push({ type: "text", content: remainingText });++ // No file blocks at all: hand back the message unchanged rather than empty.+ if (sections.length === 0) sections.push({ type: "text", content: text });++ return sections;+}++/**+ * The message's text, from whichever of the three shapes it arrived in:+ * streaming `parts`, an array `content`, or a plain string.+ */+export function getMessageTextContent(message: UIMessage): string {+ if (message.parts && Array.isArray(message.parts)) {+ return message.parts+ .filter((part) => part.type === "text")+ .map((part) => String(part.text ?? ""))+ .join("\n");+ }++ if (Array.isArray(message.content)) {+ return message.content+ .filter((part): part is MessagePart => (part as MessagePart)?.type === "text")+ .map((part) => String(part.text || ""))+ .join("\n");+ }++ return message.content || "";+}++/** What the user actually typed, with the appended file contents stripped. */+export function getUserOriginalText(message: UIMessage): string {+ return getMessageTextContent(message)+ .replace(/\n\n\[(PDF|File):\s*[^\]]+\]\n[\s\S]*$/, "")+ .trim();+}diff --git a/client/shared/chat/ppt-message-tags.ts b/client/shared/chat/ppt-message-tags.tsnew file mode 100644index 0000000..ddc85c0--- /dev/null+++ b/client/shared/chat/ppt-message-tags.ts@@ -0,0 +1,215 @@+/**+ * The markers a PPT conversation carries inside otherwise plain message text.+ *+ * The chat input writes `[[PPT_SLIDE|n|title|kind]]` when the user attaches a+ * slide, and the assistant answers slide edits as a `ppt_edit` JSON payload.+ * The transcript has to pull both back out to render them as chips and diffs+ * rather than as raw text.+ */++export interface PptEditSlidePatch {+ id?: string;+ title?: string;+ content?: string[];+ description?: string;+ note?: string;+ layout?: string;+ [key: string]: unknown;+}++export interface PptToolPayload {+ type: "ppt_edit";+ slides: PptEditSlidePatch[];+}++export type PptSlideTag = { n: number; title?: string; kind?: "outline" | "slide_image" };+export type ImageTag = { name?: string; url: string };++export type InlinePptPart =+ | { type: "text"; text: string }+ | { type: "ppt"; n: number; title?: string; kind?: "outline" | "slide_image" };++/** Pulls out slide markers that sit alone on their own line. */+export function extractPptSlideTags(text: string): { tags: PptSlideTag[]; rest: string } {+ const tags: PptSlideTag[] = [];+ const restLines: string[] = [];++ for (const line of String(text || "").split(/\r?\n/)) {+ const tagged = line.match(/^\[\[PPT_SLIDE\|(\d+)\|([^|]*)\|(outline|slide_image)\]\]$/);+ if (tagged) {+ const n = Number(tagged[1]);+ const title = String(tagged[2] || "").trim();+ if (!Number.isNaN(n)) {+ tags.push({ n, title: title || undefined, kind: tagged[3] as "outline" | "slide_image" });+ }+ continue;+ }++ // Markers written before `kind` existed default to a slide image.+ const legacy = line.match(/^\[\[PPT_SLIDE\|(\d+)\|(.*)\]\]$/);+ if (legacy) {+ const n = Number(legacy[1]);+ const title = String(legacy[2] || "").trim();+ if (!Number.isNaN(n)) tags.push({ n, title: title || undefined, kind: "slide_image" });+ continue;+ }++ restLines.push(line);+ }++ return { tags, rest: restLines.join("\n").replace(/^\s+|\s+$/g, "") };+}++const SAFE_IMAGE_PREFIXES = ["data:image", "blob:", "http://", "https://"];++/** Pulls out image markers, ignoring any whose URL is not one we will render. */+export function extractImageTags(text: string): { images: ImageTag[]; rest: string } {+ const images: ImageTag[] = [];+ const restLines: string[] = [];++ for (const line of String(text || "").split(/\r?\n/)) {+ const match = line.match(/^\[\[IMAGE\|([^|]*)\|([\s\S]+)\]\]$/);+ if (match) {+ const name = String(match[1] || "").trim();+ const url = String(match[2] || "").trim();+ if (SAFE_IMAGE_PREFIXES.some((prefix) => url.startsWith(prefix))) {+ images.push({ name: name || undefined, url });+ continue;+ }+ }+ restLines.push(line);+ }++ return { images, rest: restLines.join("\n").replace(/^\s+|\s+$/g, "") };+}++/**+ * Splits a line into text and the slide markers embedded in it, so a slide+ * referenced mid-sentence renders as a chip in place.+ */+export function splitInlinePptTags(text: string): InlinePptPart[] {+ const raw = String(text || "");+ const out: InlinePptPart[] = [];+ const pattern = /\[\[PPT_SLIDE\|(\d+)\|([\s\S]*?)\]\]/g;+ let lastIndex = 0;+ let match: RegExpExecArray | null;++ while ((match = pattern.exec(raw))) {+ const before = raw.slice(lastIndex, match.index);+ if (before) out.push({ type: "text", text: before });++ const n = Number(match[1]);+ const title = String(match[2] || "").trim();+ if (Number.isNaN(n)) {+ // Not a real marker after all; keep it as the text it looked like.+ out.push({ type: "text", text: match[0] });+ } else {+ let kind: "outline" | "slide_image" = "slide_image";+ let normalizedTitle = title;+ // A title may itself contain "|", so only a trailing known kind counts.+ const parts = title.split("|");+ if (parts.length >= 2) {+ const maybeKind = parts[parts.length - 1].trim();+ if (maybeKind === "outline" || maybeKind === "slide_image") {+ kind = maybeKind;+ normalizedTitle = parts.slice(0, -1).join("|").trim();+ }+ }+ out.push({ type: "ppt", n, title: normalizedTitle || undefined, kind });+ }+ lastIndex = match.index + match[0].length;+ }++ const rest = raw.slice(lastIndex);+ if (rest) out.push({ type: "text", text: rest });+ if (out.length === 0) out.push({ type: "text", text: raw });+ return out;+}++/**+ * Reads a slide-edit payload out of an assistant reply, whether it arrived+ * fenced, bare, or as a naked array of slides.+ */+export function extractPptToolPayload(text: string): PptToolPayload | null {+ const raw = String(text || "").trim();+ if (!raw) return null;++ const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)\s*```/i);+ let parsed: unknown;+ try {+ parsed = JSON.parse((fenced?.[1] ?? raw).trim());+ } catch {+ return null;+ }++ if (Array.isArray(parsed)) {+ return {+ type: "ppt_edit",+ slides: parsed.filter((s): s is PptEditSlidePatch => !!s && typeof s === "object"),+ };+ }+ if (!parsed || typeof parsed !== "object") return null;++ const record = parsed as Record<string, unknown>;+ const declaredType = String(record.type || "").trim().toLowerCase();+ if (declaredType && declaredType !== "ppt_edit") return null;++ const slides = Array.isArray(record.slides)+ ? record.slides.filter((s): s is PptEditSlidePatch => !!s && typeof s === "object")+ : [];+ return slides.length > 0 ? { type: "ppt_edit", slides } : null;+}++/** The slide's 1-based number, from its id where it has one. */+export function getSlideNumber(slide: PptEditSlidePatch, index: number): number {+ const match = String(slide?.id || "").match(/slide-(\d+)/i);+ if (match) {+ const n = Number(match[1]);+ if (!Number.isNaN(n)) return n;+ }+ return index + 1;+}++/** The fields shown first, in the order a reader expects them. */+const KNOWN_FIELDS = ["title", "description", "layout", "note", "content"];+const EMPTY_VALUE = "(空)";++/** Flattens a patch into displayable rows, keeping unknown fields rather than hiding them. */+export function slidePatchEntries(slide: PptEditSlidePatch): Array<{ key: string; value: string }> {+ const rows: Array<{ key: string; value: string }> = [];++ for (const key of KNOWN_FIELDS) {+ if (!(key in slide)) continue;+ const raw = slide[key];+ if (Array.isArray(raw)) {+ const text = raw.map((x) => String(x || "").trim()).filter(Boolean).join(";");+ rows.push({ key, value: text || EMPTY_VALUE });+ } else if (raw && typeof raw === "object") {+ rows.push({ key, value: JSON.stringify(raw) });+ } else {+ rows.push({ key, value: String(raw ?? "").trim() || EMPTY_VALUE });+ }+ }++ for (const [key, value] of Object.entries(slide)) {+ if (key === "id" || KNOWN_FIELDS.includes(key)) continue;+ if (value === undefined) continue;+ rows.push({ key, value: typeof value === "string" ? value : JSON.stringify(value) });+ }++ return rows;+}++const FIELD_LABELS: Record<string, { zh: string; en: string }> = {+ title: { zh: "标题", en: "Title" },+ description: { zh: "画面描述", en: "Description" },+ layout: { zh: "布局", en: "Layout" },+ note: { zh: "备注", en: "Note" },+ content: { zh: "内容", en: "Content" },+};++/** An unknown field falls back to its own key rather than disappearing. */+export function slideFieldLabel(key: string, uiLang: "zh" | "en"): string {+ const hit = FIELD_LABELS[key];+ return hit ? hit[uiLang] : key;+}diff --git a/client/workspaces/cad/chat/ChatMessageDisplay.tsx b/client/workspaces/cad/chat/ChatMessageDisplay.tsxindex 32f5238..a41989b 100644--- a/client/workspaces/cad/chat/ChatMessageDisplay.tsx+++ b/client/workspaces/cad/chat/ChatMessageDisplay.tsx@@ -7,26 +7,26 @@ import { ScrollArea } from "@/shared/ui/scroll-area";import { CodeBlock } from "@/workspaces/cad/chat/code-block";import { useUiLanguage } from "@/shared/i18n";import { cn } from "@/shared/lib/utils";+import {+ getMessageTextContent,+ getUserOriginalText,+ splitTextIntoFileSections,+ type MessagePart,+ type UIMessage,+} from "@/shared/chat/message-content";+import {+ extractImageTags,+ extractPptSlideTags,+ extractPptToolPayload,+ getSlideNumber,+ slideFieldLabel,+ slidePatchEntries,+ splitInlinePptTags,+} from "@/shared/chat/ppt-message-tags";++export type { MessagePart, UIMessage };// Types-export interface MessagePart {- type: 'text' | 'reasoning' | 'tool-call' | 'tool-result' | 'file';- text?: string;- toolName?: string;- toolCallId?: string;- state?: string;- input?: any;- output?: string;- url?: string; // for images-}--export interface UIMessage {- id: string;- role: 'user' | 'assistant' | 'system';- content: string; // Fallback- parts?: MessagePart[];-}-interface ChatMessageDisplayProps {messages: UIMessage[];setInput: (input: string) => void;@@ -38,24 +38,6 @@ interface ChatMessageDisplayProps {}// Helper to split text content into regular text and file sections-interface TextSection {- type: "text" | "file";- content: string;- filename?: string;- charCount?: number;- fileType?: "pdf" | "text";-}--interface PptEditSlidePatch {- id?: string;- [key: string]: any;-}--interface PptToolPayload {- type: "ppt_edit";- slides: PptEditSlidePatch[];-}-interface CadPatchEdit {search: string;replace: string;@@ -104,131 +86,6 @@ function AutoApplyCodeOnce({return null;}-function splitTextIntoFileSections(text: string): TextSection[] {- const sections: TextSection[] = [];- const filePattern = /\[(PDF|File):\s*([^\]]+)\]\n([\s\S]*?)(?=\n\n\[(PDF|File):|$)/g;- let lastIndex = 0;- let match;-- while ((match = filePattern.exec(text)) !== null) {- const beforeText = text.slice(lastIndex, match.index).trim();- if (beforeText) {- sections.push({ type: "text", content: beforeText });- }-- const fileType = match[1].toLowerCase() === "pdf" ? "pdf" : "text";- const filename = match[2].trim();- const fileContent = match[3].trim();- sections.push({- type: "file",- content: fileContent,- filename,- charCount: fileContent.length,- fileType,- });-- lastIndex = match.index + match[0].length;- }-- const remainingText = text.slice(lastIndex).trim();- if (remainingText) {- sections.push({ type: "text", content: remainingText });- }-- if (sections.length === 0) {- sections.push({ type: "text", content: text });- }-- return sections;-}--type PptSlideTag = { n: number; title?: string; kind?: "outline" | "slide_image" };--function extractPptSlideTags(text: string): { tags: PptSlideTag[]; rest: string } {- const lines = String(text || "").split(/\r?\n/);- const tags: PptSlideTag[] = [];- const restLines: string[] = [];- for (const line of lines) {- const m = line.match(/^\[\[PPT_SLIDE\|(\d+)\|([^|]*)\|(outline|slide_image)\]\]$/);- if (m) {- const n = Number(m[1]);- const title = String(m[2] || "").trim();- const kind = m[3] as "outline" | "slide_image";- if (!Number.isNaN(n)) tags.push({ n, title: title || undefined, kind });- continue;- }- const legacy = line.match(/^\[\[PPT_SLIDE\|(\d+)\|(.*)\]\]$/);- if (legacy) {- const n = Number(legacy[1]);- const title = String(legacy[2] || "").trim();- if (!Number.isNaN(n)) tags.push({ n, title: title || undefined, kind: "slide_image" });- continue;- }- restLines.push(line);- }- const rest = restLines.join("\n").replace(/^\s+|\s+$/g, "");- return { tags, rest };-}--type ImageTag = { name?: string; url: string };--function extractImageTags(text: string): { images: ImageTag[]; rest: string } {- const lines = String(text || "").split(/\r?\n/);- const images: ImageTag[] = [];- const restLines: string[] = [];- for (const line of lines) {- const m = line.match(/^\[\[IMAGE\|([^|]*)\|([\s\S]+)\]\]$/);- if (m) {- const name = String(m[1] || "").trim();- const url = String(m[2] || "").trim();- if (url.startsWith("data:image") || url.startsWith("blob:") || url.startsWith("http://") || url.startsWith("https://")) {- images.push({ name: name || undefined, url });- continue;- }- }- restLines.push(line);- }- const rest = restLines.join("\n").replace(/^\s+|\s+$/g, "");- return { images, rest };-}--type InlinePptPart =- | { type: "text"; text: string }- | { type: "ppt"; n: number; title?: string; kind?: "outline" | "slide_image" };--function splitInlinePptTags(text: string): InlinePptPart[] {- const raw = String(text || "");- const out: InlinePptPart[] = [];- const re = /\[\[PPT_SLIDE\|(\d+)\|([\s\S]*?)\]\]/g;- let lastIndex = 0;- let m: RegExpExecArray | null;- while ((m = re.exec(raw))) {- const before = raw.slice(lastIndex, m.index);- if (before) out.push({ type: "text", text: before });- const n = Number(m[1]);- const title = String(m[2] || "").trim();- if (!Number.isNaN(n)) {- let kind: "outline" | "slide_image" = "slide_image";- let normalizedTitle = title;- const parts = title.split("|");- if (parts.length >= 2) {- const maybeKind = parts[parts.length - 1].trim();- if (maybeKind === "outline" || maybeKind === "slide_image") {- kind = maybeKind;- normalizedTitle = parts.slice(0, -1).join("|").trim();- }- }- out.push({ type: "ppt", n, title: normalizedTitle || undefined, kind });- }- else out.push({ type: "text", text: m[0] });- lastIndex = m.index + m[0].length;- }- const rest = raw.slice(lastIndex);- if (rest) out.push({ type: "text", text: rest });- if (out.length === 0) out.push({ type: "text", text: raw });- return out;-}-type MarkdownSegment =| { type: "markdown"; content: string }| { type: "code"; code: string; language: string };@@ -289,46 +146,6 @@ function splitMarkdownAndCodeBlocks(text: string): MarkdownSegment[] {return segments;}-const getMessageTextContent = (message: UIMessage): string => {- if (message.parts && message.parts.length > 0) {- return message.parts- .filter((part) => part.type === "text")- .map((part) => part.text || "")- .join("\n");- }- return message.content || "";-};--const getUserOriginalText = (message: UIMessage): string => {- const fullText = getMessageTextContent(message);- const filePattern = /\n\n\[(PDF|File):\s*[^\]]+\]\n[\s\S]*$/;- return fullText.replace(filePattern, "").trim();-};--function extractPptToolPayload(text: string): PptToolPayload | null {- const raw = String(text || "").trim();- if (!raw) return null;- const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)\s*```/i);- const candidate = (fenced?.[1] ?? raw).trim();- let parsed: any;- try {- parsed = JSON.parse(candidate);- } catch {- return null;- }- if (Array.isArray(parsed)) {- return { type: "ppt_edit", slides: parsed.filter((s: any) => s && typeof s === "object") };- }- if (!parsed || typeof parsed !== "object") return null;- const typeRaw = String((parsed as any).type || "").trim().toLowerCase();- if (typeRaw && typeRaw !== "ppt_edit") return null;- const type = "ppt_edit" as const;- const slides = Array.isArray((parsed as any).slides)- ? (parsed as any).slides.filter((s: any) => s && typeof s === "object")- : [];- return slides.length > 0 ? { type, slides } : null;-}-function extractCadPatchToolPayload(text: string): CadPatchToolPayload | null {const raw = String(text || "").trim();if (!raw) return null;@@ -359,54 +176,6 @@ function extractCadPatchToolPayload(text: string): CadPatchToolPayload | null {return { type: "cad_patch", target: "2d_svg", mode: "patch", edits };}-function getSlideNumber(slide: PptEditSlidePatch, index: number): number {- const id = String(slide?.id || "");- const m = id.match(/slide-(\d+)/i);- if (m) {- const n = Number(m[1]);- if (!Number.isNaN(n)) return n;- }- return index + 1;-}--function slidePatchEntries(slide: PptEditSlidePatch): Array<{ key: string; value: string }> {- const keys = ["title", "description", "layout", "note", "content"];- const rows: Array<{ key: string; value: string }> = [];- for (const key of keys) {- if (!(key in slide)) continue;- const raw = (slide as any)[key];- if (Array.isArray(raw)) {- const text = raw.map((x) => String(x || "").trim()).filter(Boolean).join(";");- rows.push({ key, value: text || "(空)" });- continue;- }- if (raw && typeof raw === "object") {- rows.push({ key, value: JSON.stringify(raw) });- continue;- }- rows.push({ key, value: String(raw ?? "").trim() || "(空)" });- }- for (const [k, v] of Object.entries(slide)) {- if (k === "id" || keys.includes(k)) continue;- if (v === undefined) continue;- rows.push({ key: k, value: typeof v === "string" ? v : JSON.stringify(v) });- }- return rows;-}--function slideFieldLabel(key: string, uiLang: "zh" | "en"): string {- const dict: Record<string, { zh: string; en: string }> = {- title: { zh: "标题", en: "Title" },- description: { zh: "画面描述", en: "Description" },- layout: { zh: "布局", en: "Layout" },- note: { zh: "备注", en: "Note" },- content: { zh: "内容", en: "Content" },- };- const hit = dict[key];- if (hit) return uiLang === "zh" ? hit.zh : hit.en;- return key;-}-function CadPatchEditsDisplay({ edits, uiLang }: { edits: CadPatchEdit[]; uiLang: "zh" | "en" }) {const tr = (zh: string, en: string) => (uiLang === "zh" ? zh : en);return (diff --git a/client/workspaces/flow/chat/ChatMessageDisplay.tsx b/client/workspaces/flow/chat/ChatMessageDisplay.tsxindex f109e37..7f553ee 100644--- a/client/workspaces/flow/chat/ChatMessageDisplay.tsx+++ b/client/workspaces/flow/chat/ChatMessageDisplay.tsx@@ -1,5 +1,12 @@"use client"+import {+ getMessageTextContent,+ getUserOriginalText,+ splitTextIntoFileSections,+ type TextSection,+} from "@/shared/chat/message-content"+/*** A message as the chat hook hands it over. `parts` is deliberately loose:* the AI SDK streams tool calls, files and reasoning blocks whose shapes vary@@ -92,74 +99,6 @@ function OperationsDisplay({ operations }: { operations: DiagramOperation[] }) {import { useDiagram } from "@/workspaces/flow/state/diagram-context"// Helper to split text content into regular text and file sections (PDF or text files)-interface TextSection {- type: "text" | "file"- content: string- filename?: string- charCount?: number- fileType?: "pdf" | "text"-}--function splitTextIntoFileSections(text: string): TextSection[] {- const sections: TextSection[] = []- // Match [PDF: filename] or [File: filename] patterns- const filePattern =- /\[(PDF|File):\s*([^\]]+)\]\n([\s\S]*?)(?=\n\n\[(PDF|File):|$)/g- let lastIndex = 0- let match-- while ((match = filePattern.exec(text)) !== null) {- // Add text before this file section- const beforeText = text.slice(lastIndex, match.index).trim()- if (beforeText) {- sections.push({ type: "text", content: beforeText })- }-- // Add file section- const fileType = match[1].toLowerCase() === "pdf" ? "pdf" : "text"- const filename = match[2].trim()- const fileContent = match[3].trim()- sections.push({- type: "file",- content: fileContent,- filename,- charCount: fileContent.length,- fileType,- })-- lastIndex = match.index + match[0].length- }-- // Add remaining text after last file section- const remainingText = text.slice(lastIndex).trim()- if (remainingText) {- sections.push({ type: "text", content: remainingText })- }-- // If no file sections found, return original text- if (sections.length === 0) {- sections.push({ type: "text", content: text })- }-- return sections-}--const getMessageTextContent = (message: UIMessage): string => {- if (message.parts && message.parts.length > 0) {- return message.parts- .filter((part) => part.type === "text")- .map((part) => String(part.text ?? ""))- .join("\n")- }- if (Array.isArray(message.content)) {- return message.content- .filter((part: any) => part?.type === "text")- .map((part: any) => String(part?.text || ""))- .join("\n")- }- return message.content || ""-}-function extractFileSectionsFromText(text: string): TextSection[] {return splitTextIntoFileSections(text).filter((s) => s.type === "file",@@ -174,13 +113,6 @@ function stripFileSectionsFromText(text: string): string {}// Get only the user's original text, excluding appended file content-const getUserOriginalText = (message: UIMessage): string => {- const fullText = getMessageTextContent(message)- // Strip out [PDF: ...] and [File: ...] sections that were appended- const filePattern = /\n\n\[(PDF|File):\s*[^\]]+\]\n[\s\S]*$/- return fullText.replace(filePattern, "").trim()-}-interface ChatMessageDisplayProps {messages: UIMessage[]setInput: (input: string) => voiddiff --git a/client/workspaces/ppt/chat/ChatMessageDisplay.tsx b/client/workspaces/ppt/chat/ChatMessageDisplay.tsxindex 981d890..901d371 100644--- a/client/workspaces/ppt/chat/ChatMessageDisplay.tsx+++ b/client/workspaces/ppt/chat/ChatMessageDisplay.tsx@@ -7,26 +7,26 @@ import { ScrollArea } from "@/shared/ui/scroll-area";import { CodeBlock } from "@/workspaces/ppt/chat/code-block";import { useUiLanguage } from "@/shared/i18n";import { cn } from "@/shared/lib/utils";+import {+ getMessageTextContent,+ getUserOriginalText,+ splitTextIntoFileSections,+ type MessagePart,+ type UIMessage,+} from "@/shared/chat/message-content";+import {+ extractImageTags,+ extractPptSlideTags,+ extractPptToolPayload,+ getSlideNumber,+ slideFieldLabel,+ slidePatchEntries,+ splitInlinePptTags,+} from "@/shared/chat/ppt-message-tags";++export type { MessagePart, UIMessage };// Types-export interface MessagePart {- type: 'text' | 'reasoning' | 'tool-call' | 'tool-result' | 'file';- text?: string;- toolName?: string;- toolCallId?: string;- state?: string;- input?: any;- output?: string;- url?: string; // for images-}--export interface UIMessage {- id: string;- role: 'user' | 'assistant' | 'system';- content: string; // Fallback- parts?: MessagePart[];-}-interface ChatMessageDisplayProps {messages: UIMessage[];setInput: (input: string) => void;@@ -37,237 +37,6 @@ interface ChatMessageDisplayProps {}// Helper to split text content into regular text and file sections-interface TextSection {- type: "text" | "file";- content: string;- filename?: string;- charCount?: number;- fileType?: "pdf" | "text";-}--interface PptEditSlidePatch {- id?: string;- [key: string]: any;-}--interface PptToolPayload {- type: "ppt_edit";- slides: PptEditSlidePatch[];-}--function splitTextIntoFileSections(text: string): TextSection[] {- const sections: TextSection[] = [];- const filePattern = /\[(PDF|File):\s*([^\]]+)\]\n([\s\S]*?)(?=\n\n\[(PDF|File):|$)/g;- let lastIndex = 0;- let match;-- while ((match = filePattern.exec(text)) !== null) {- const beforeText = text.slice(lastIndex, match.index).trim();- if (beforeText) {- sections.push({ type: "text", content: beforeText });- }-- const fileType = match[1].toLowerCase() === "pdf" ? "pdf" : "text";- const filename = match[2].trim();- const fileContent = match[3].trim();- sections.push({- type: "file",- content: fileContent,- filename,- charCount: fileContent.length,- fileType,- });-- lastIndex = match.index + match[0].length;- }-- const remainingText = text.slice(lastIndex).trim();- if (remainingText) {- sections.push({ type: "text", content: remainingText });- }-- if (sections.length === 0) {- sections.push({ type: "text", content: text });- }-- return sections;-}--type PptSlideTag = { n: number; title?: string; kind?: "outline" | "slide_image" };--function extractPptSlideTags(text: string): { tags: PptSlideTag[]; rest: string } {- const lines = String(text || "").split(/\r?\n/);- const tags: PptSlideTag[] = [];- const restLines: string[] = [];- for (const line of lines) {- const m = line.match(/^\[\[PPT_SLIDE\|(\d+)\|([^|]*)\|(outline|slide_image)\]\]$/);- if (m) {- const n = Number(m[1]);- const title = String(m[2] || "").trim();- const kind = m[3] as "outline" | "slide_image";- if (!Number.isNaN(n)) tags.push({ n, title: title || undefined, kind });- continue;- }- const legacy = line.match(/^\[\[PPT_SLIDE\|(\d+)\|(.*)\]\]$/);- if (legacy) {- const n = Number(legacy[1]);- const title = String(legacy[2] || "").trim();- if (!Number.isNaN(n)) tags.push({ n, title: title || undefined, kind: "slide_image" });- continue;- }- restLines.push(line);- }- const rest = restLines.join("\n").replace(/^\s+|\s+$/g, "");- return { tags, rest };-}--type ImageTag = { name?: string; url: string };--function extractImageTags(text: string): { images: ImageTag[]; rest: string } {- const lines = String(text || "").split(/\r?\n/);- const images: ImageTag[] = [];- const restLines: string[] = [];- for (const line of lines) {- const m = line.match(/^\[\[IMAGE\|([^|]*)\|([\s\S]+)\]\]$/);- if (m) {- const name = String(m[1] || "").trim();- const url = String(m[2] || "").trim();- if (url.startsWith("data:image") || url.startsWith("blob:") || url.startsWith("http://") || url.startsWith("https://")) {- images.push({ name: name || undefined, url });- continue;- }- }- restLines.push(line);- }- const rest = restLines.join("\n").replace(/^\s+|\s+$/g, "");- return { images, rest };-}--type InlinePptPart =- | { type: "text"; text: string }- | { type: "ppt"; n: number; title?: string; kind?: "outline" | "slide_image" };--function splitInlinePptTags(text: string): InlinePptPart[] {- const raw = String(text || "");- const out: InlinePptPart[] = [];- const re = /\[\[PPT_SLIDE\|(\d+)\|([\s\S]*?)\]\]/g;- let lastIndex = 0;- let m: RegExpExecArray | null;- while ((m = re.exec(raw))) {- const before = raw.slice(lastIndex, m.index);- if (before) out.push({ type: "text", text: before });- const n = Number(m[1]);- const title = String(m[2] || "").trim();- if (!Number.isNaN(n)) {- let kind: "outline" | "slide_image" = "slide_image";- let normalizedTitle = title;- const parts = title.split("|");- if (parts.length >= 2) {- const maybeKind = parts[parts.length - 1].trim();- if (maybeKind === "outline" || maybeKind === "slide_image") {- kind = maybeKind;- normalizedTitle = parts.slice(0, -1).join("|").trim();- }- }- out.push({ type: "ppt", n, title: normalizedTitle || undefined, kind });- }- else out.push({ type: "text", text: m[0] });- lastIndex = m.index + m[0].length;- }- const rest = raw.slice(lastIndex);- if (rest) out.push({ type: "text", text: rest });- if (out.length === 0) out.push({ type: "text", text: raw });- return out;-}--const getMessageTextContent = (message: UIMessage): string => {- if (message.parts && message.parts.length > 0) {- return message.parts- .filter((part) => part.type === "text")- .map((part) => part.text || "")- .join("\n");- }- return message.content || "";-};--const getUserOriginalText = (message: UIMessage): string => {- const fullText = getMessageTextContent(message);- const filePattern = /\n\n\[(PDF|File):\s*[^\]]+\]\n[\s\S]*$/;- return fullText.replace(filePattern, "").trim();-};--function extractPptToolPayload(text: string): PptToolPayload | null {- const raw = String(text || "").trim();- if (!raw) return null;- const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)\s*```/i);- const candidate = (fenced?.[1] ?? raw).trim();- let parsed: any;- try {- parsed = JSON.parse(candidate);- } catch {- return null;- }- if (Array.isArray(parsed)) {- return { type: "ppt_edit", slides: parsed.filter((s: any) => s && typeof s === "object") };- }- if (!parsed || typeof parsed !== "object") return null;- const typeRaw = String((parsed as any).type || "").trim().toLowerCase();- if (typeRaw && typeRaw !== "ppt_edit") return null;- const type = "ppt_edit" as const;- const slides = Array.isArray((parsed as any).slides)- ? (parsed as any).slides.filter((s: any) => s && typeof s === "object")- : [];- return slides.length > 0 ? { type, slides } : null;-}--function getSlideNumber(slide: PptEditSlidePatch, index: number): number {- const id = String(slide?.id || "");- const m = id.match(/slide-(\d+)/i);- if (m) {- const n = Number(m[1]);- if (!Number.isNaN(n)) return n;- }- return index + 1;-}--function slidePatchEntries(slide: PptEditSlidePatch): Array<{ key: string; value: string }> {- const keys = ["title", "description", "layout", "note", "content"];- const rows: Array<{ key: string; value: string }> = [];- for (const key of keys) {- if (!(key in slide)) continue;- const raw = (slide as any)[key];- if (Array.isArray(raw)) {- const text = raw.map((x) => String(x || "").trim()).filter(Boolean).join(";");- rows.push({ key, value: text || "(空)" });- continue;- }- if (raw && typeof raw === "object") {- rows.push({ key, value: JSON.stringify(raw) });- continue;- }- rows.push({ key, value: String(raw ?? "").trim() || "(空)" });- }- for (const [k, v] of Object.entries(slide)) {- if (k === "id" || keys.includes(k)) continue;- if (v === undefined) continue;- rows.push({ key: k, value: typeof v === "string" ? v : JSON.stringify(v) });- }- return rows;-}--function slideFieldLabel(key: string, uiLang: "zh" | "en"): string {- const dict: Record<string, { zh: string; en: string }> = {- title: { zh: "标题", en: "Title" },- description: { zh: "画面描述", en: "Description" },- layout: { zh: "布局", en: "Layout" },- note: { zh: "备注", en: "Note" },- content: { zh: "内容", en: "Content" },- };- const hit = dict[key];- if (hit) return uiLang === "zh" ? hit.zh : hit.en;- return key;-}-export function ChatMessageDisplay({messages,setInput,diff --git a/tests/chat-message-content.test.ts b/tests/chat-message-content.test.tsnew file mode 100644index 0000000..4fed057--- /dev/null+++ b/tests/chat-message-content.test.ts@@ -0,0 +1,205 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import {+ getMessageTextContent,+ getUserOriginalText,+ splitTextIntoFileSections,+ type UIMessage,+} from "../client/shared/chat/message-content";+import {+ extractImageTags,+ extractPptSlideTags,+ extractPptToolPayload,+ getSlideNumber,+ slideFieldLabel,+ slidePatchEntries,+ splitInlinePptTags,+} from "../client/shared/chat/ppt-message-tags";++/**+ * The readers every workspace transcript runs over a message before showing+ * it. All three chat panels append uploaded file contents to what the user+ * typed, and the PPT one embeds slide markers in the text; getting either+ * wrong shows the user raw machinery instead of their own words.+ */++test("a message with no attachments comes back as one text section", () => {+ const sections = splitTextIntoFileSections("just a question");+ assert.deepEqual(sections, [{ type: "text", content: "just a question" }]);+});++test("an appended file becomes its own section with a character count", () => {+ const sections = splitTextIntoFileSections("summarise this\n\n[PDF: report.pdf]\nthe extracted text");+ assert.equal(sections.length, 2);+ assert.deepEqual(sections[0], { type: "text", content: "summarise this" });+ assert.equal(sections[1].type, "file");+ assert.equal(sections[1].filename, "report.pdf");+ assert.equal(sections[1].fileType, "pdf");+ assert.equal(sections[1].content, "the extracted text");+ assert.equal(sections[1].charCount, "the extracted text".length, "the count the header shows");+});++test("several attachments each get their own section", () => {+ const sections = splitTextIntoFileSections(+ "look\n\n[File: a.txt]\nAAA\n\n[File: b.txt]\nBBB",+ );+ assert.deepEqual(+ sections.map((s) => s.filename ?? s.type),+ ["text", "a.txt", "b.txt"],+ );+});++test("a non-pdf attachment is typed as text", () => {+ const [section] = splitTextIntoFileSections("[File: notes.md]\nhello");+ assert.equal(section.fileType, "text");+});++test("an empty message is not turned into nothing", () => {+ assert.deepEqual(splitTextIntoFileSections(""), [{ type: "text", content: "" }]);+});++test("text is read from streaming parts", () => {+ const message: UIMessage = {+ id: "1",+ role: "assistant",+ parts: [+ { type: "text", text: "hello" },+ { type: "tool-call", toolCallId: "x" },+ { type: "text", text: "world" },+ ],+ };+ assert.equal(getMessageTextContent(message), "hello\nworld", "non-text parts are skipped");+});++test("text is read from a multimodal content array", () => {+ // The shape a message takes once it has been sent with an image attached.+ const message: UIMessage = {+ id: "1",+ role: "user",+ content: [+ { type: "text", text: "what is this" },+ { type: "image", image: "data:image/png;base64,x" },+ ] as unknown[],+ };+ assert.equal(getMessageTextContent(message), "what is this");+});++test("text is read from a plain string content", () => {+ assert.equal(getMessageTextContent({ id: "1", role: "user", content: "plain" }), "plain");+});++test("a message with nothing in it reads as empty", () => {+ assert.equal(getMessageTextContent({ id: "1", role: "user" }), "");+ assert.equal(getMessageTextContent({ id: "1", role: "assistant", parts: [] }), "");+});++test("the user's own words are recovered from an attachment-laden message", () => {+ const message: UIMessage = {+ id: "1",+ role: "user",+ content: "summarise this\n\n[PDF: report.pdf]\npages and pages of text",+ };+ assert.equal(getUserOriginalText(message), "summarise this", "editing shows what was typed");+});++test("a message without attachments is returned whole", () => {+ assert.equal(getUserOriginalText({ id: "1", role: "user", content: " hi " }), "hi");+});++test("slide markers on their own line are lifted out of the text", () => {+ const { tags, rest } = extractPptSlideTags("[[PPT_SLIDE|2|Revenue|outline]]\nplease redo this");+ assert.deepEqual(tags, [{ n: 2, title: "Revenue", kind: "outline" }]);+ assert.equal(rest, "please redo this");+});++test("a marker from before kind existed still reads as a slide image", () => {+ const { tags } = extractPptSlideTags("[[PPT_SLIDE|3|Old Title]]");+ assert.deepEqual(tags, [{ n: 3, title: "Old Title", kind: "slide_image" }]);+});++test("a marker with no title keeps the slide number", () => {+ const { tags } = extractPptSlideTags("[[PPT_SLIDE|4||outline]]");+ assert.deepEqual(tags, [{ n: 4, title: undefined, kind: "outline" }]);+});++test("image markers are lifted out, and only for renderable urls", () => {+ const { images, rest } = extractImageTags(+ ["[[IMAGE|shot|data:image/png;base64,AAA]]", "[[IMAGE|bad|javascript:alert(1)]]", "text"].join("\n"),+ );+ assert.deepEqual(images, [{ name: "shot", url: "data:image/png;base64,AAA" }]);+ assert.ok(rest.includes("javascript:alert(1)"), "a rejected marker stays as text rather than rendering");+ assert.ok(rest.includes("text"));+});++test("a marker mid-sentence splits into text and a chip", () => {+ const parts = splitInlinePptTags("please fix [[PPT_SLIDE|2|Revenue|outline]] today");+ assert.deepEqual(parts, [+ { type: "text", text: "please fix " },+ { type: "ppt", n: 2, title: "Revenue", kind: "outline" },+ { type: "text", text: " today" },+ ]);+});++test("a title containing a pipe survives the split", () => {+ // Only a trailing known kind is treated as the kind, so titles keep theirs.+ const parts = splitInlinePptTags("[[PPT_SLIDE|1|A|B|slide_image]]");+ assert.deepEqual(parts, [{ type: "ppt", n: 1, title: "A|B", kind: "slide_image" }]);+});++test("text with no markers comes back as a single run", () => {+ assert.deepEqual(splitInlinePptTags("nothing here"), [{ type: "text", text: "nothing here" }]);+});++test("a slide-edit payload is read whether fenced or bare", () => {+ const slides = '{"type":"ppt_edit","slides":[{"id":"slide-2","title":"New"}]}';+ for (const raw of [slides, "Sure!\n```json\n" + slides + "\n```"]) {+ const payload = extractPptToolPayload(raw);+ assert.equal(payload?.slides[0].title, "New", raw);+ }+});++test("a naked array of slides counts as an edit payload", () => {+ const payload = extractPptToolPayload('[{"id":"slide-1","title":"A"}]');+ assert.equal(payload?.type, "ppt_edit");+ assert.equal(payload?.slides.length, 1);+});++test("anything that is not a slide edit is refused", () => {+ assert.equal(extractPptToolPayload(""), null);+ assert.equal(extractPptToolPayload("just prose"), null);+ assert.equal(extractPptToolPayload('{"type":"cad_patch","edits":[]}'), null, "another tool's payload");+ assert.equal(extractPptToolPayload('{"type":"ppt_edit","slides":[]}'), null, "nothing to apply");+});++test("the slide number comes from the id, and falls back to position", () => {+ assert.equal(getSlideNumber({ id: "slide-7" }, 0), 7);+ assert.equal(getSlideNumber({ id: "cover" }, 3), 4, "1-based position");+ assert.equal(getSlideNumber({}, 0), 1);+});++test("a patch is flattened into rows in a readable order", () => {+ const rows = slidePatchEntries({ id: "slide-1", note: "n", title: "T", content: ["a", "b"] });+ assert.deepEqual(+ rows.map((r) => r.key),+ ["title", "note", "content"],+ "known fields come first, in their own order",+ );+ assert.equal(rows[2].value, "a;b");+});++test("an empty field is shown as empty rather than dropped", () => {+ const rows = slidePatchEntries({ title: "", content: [] });+ assert.equal(rows.find((r) => r.key === "title")?.value, "(空)");+ assert.equal(rows.find((r) => r.key === "content")?.value, "(空)");+});++test("a field the model invented is still shown", () => {+ const rows = slidePatchEntries({ id: "slide-1", surprise: "value" });+ assert.deepEqual(rows, [{ key: "surprise", value: "value" }]);+});++test("field labels follow the language and fall back to the key", () => {+ assert.equal(slideFieldLabel("title", "zh"), "标题");+ assert.equal(slideFieldLabel("title", "en"), "Title");+ assert.equal(slideFieldLabel("surprise", "en"), "surprise");+});