Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): follow the desktop agent mode in infographic notes

tianma-if36d24de667c0
diff --git a/apps/desktop/src/main/acp-host.mjs b/apps/desktop/src/main/acp-host.mjs
index 1f8b27dc..083696af 100644
--- a/apps/desktop/src/main/acp-host.mjs
+++ b/apps/desktop/src/main/acp-host.mjs
@@ -915,7 +915,7 @@ export function createAcpHostRuntime(options = {}) {
let mcpBridge;
try {
// OpenClaw's ACP Gateway bridge rejects session-scoped MCP servers.
- if (options.mcpAccess && input.adapterId !== "openClaw") {
+ if (options.mcpAccess && input.noteAccess !== false && input.adapterId !== "openClaw") {
const access = await options.mcpAccess();
mcpBridge = await (options.startMcpBridge ?? startAcpMcpBridge)(access);
}
diff --git a/apps/desktop/src/main/acp-host.test.mjs b/apps/desktop/src/main/acp-host.test.mjs
index 4f891668..7a86b020 100644
--- a/apps/desktop/src/main/acp-host.test.mjs
+++ b/apps/desktop/src/main/acp-host.test.mjs
@@ -672,6 +672,42 @@ describe("ACP stdio session", () => {
}
}, 15_000);
+ sessionTest("keeps infographic prompts from granting note write access", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-infographic-"));
+ const reportPath = path.join(directory, "report.json");
+ try {
+ const scriptPath = await writeFakeAgent(directory, {
+ reportPath,
+ secretPath: path.join(directory, "secret.txt"),
+ allowImage: false,
+ allowEmbedded: false,
+ hold: false,
+ });
+ const runtime = createAcpHostRuntime({
+ mcpAccess: () => { throw new Error("infographic prompts must not open note access"); },
+ startMcpBridge: () => { throw new Error("infographic prompts must not open note access"); },
+ });
+ const events = collector();
+ await runtime.prompt({
+ adapterId: "antigravity",
+ path: scriptPath,
+ prompt: "换成小米",
+ contextText: "Reply with one json proposal.",
+ noteAccess: false,
+ }, events.emit);
+ await events.waitFor((event) => event.type === "done");
+ const report = await readReport(reportPath);
+ expect(report.newSession.mcpServers).toEqual([]);
+ expect(JSON.stringify(report.prompt)).not.toContain("edgeever-current-workspace");
+ expect(report.prompt).toEqual([
+ { type: "text", text: "Reply with one json proposal." },
+ { type: "text", text: "换成小米" },
+ ]);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ }, 15_000);
+
sessionTest("keeps OpenClaw ACP sessions free of unsupported session MCP servers", async () => {
const directory = await mkdtemp(path.join(tmpdir(), "edgeever-openclaw-acp-"));
const reportPath = path.join(directory, "report.json");
diff --git a/apps/web/src/components/InfographicEditorPane.tsx b/apps/web/src/components/InfographicEditorPane.tsx
index cfa1d73e..a399a9a3 100644
--- a/apps/web/src/components/InfographicEditorPane.tsx
+++ b/apps/web/src/components/InfographicEditorPane.tsx
@@ -20,6 +20,8 @@ import { MEMO_EDITOR_METADATA_ROW_CLASS_NAME, MEMO_EDITOR_TOP_ROW_CLASS_NAME, ne
import { MemoEditorToolbarDivider } from "@/components/MemoEditorToolbarChrome";
import { MemoTitleInput } from "@/components/MemoTitleInput";
import { api } from "@/lib/api";
+import { readAiSidebarAdapter, readAiSidebarSource } from "@/lib/desktop-acp";
+import { buildInfographicLocalAgentContext, collectInfographicLocalAgentText, infographicLocalAgentErrorKey, parseInfographicLocalReply } from "@/lib/infographic-local-agent";
import { formatShortcutBinding, getNotebookMoveOptions, type ShortcutSettings } from "@/lib/app-helpers";
import { createLocalEditSession, requiresLocalEditSession } from "@/components/editor/editor-pane-helpers";
import { canvasToPngBlob, downloadBlob, frameInfographicExportSvg, infographicExportBasename, rasterizeInfographicSvg, readInfographicSheetColor } from "@/lib/infographic-image-export";
@@ -481,14 +483,55 @@ export default function InfographicEditorPane({
if (currentContent.length > INFOGRAPHIC_AGENT_SOURCE_MAX_LENGTH) throw new Error(t("infographic.contentTooLarge"));
const candidates = infographicAgentCandidates(promptText, getTemplates(), existingTemplate);
if (!candidates.length) throw new Error(t("infographic.aiInvalidResponse"));
- await api.streamInfographicAgent({
+ const agentHistory = history.filter((turn) => !turn.undoneAt && turn.kind !== "failed")
+ .slice(-12).map((turn) => ({ prompt: turn.prompt, response: `${turn.kind === "clarified" ? "No infographic change was applied. Clarification: " : ""}${turn.response || turn.resultTitle}${turn.decision ? `\nDecision: ${turn.decision}` : ""}`.slice(0, 2000) }));
+ if (readAiSidebarSource() === "local") {
+ const adapter = readAiSidebarAdapter();
+ if (!adapter) throw new Error(t("aiAssistant.sidebar.localMissing"));
+ let raw = "";
+ try {
+ raw = await collectInfographicLocalAgentText({
+ request: {
+ adapterId: adapter.id,
+ ...(adapter.path ? { path: adapter.path } : {}),
+ prompt: promptText,
+ contextText: buildInfographicLocalAgentContext({
+ prompt: promptText,
+ ...(existingTemplate ? { currentTemplate: existingTemplate } : {}),
+ currentContent,
+ candidates,
+ history: agentHistory,
+ }),
+ noteAccess: false,
+ },
+ signal: controller.signal,
+ onText: (text) => {
+ response = text;
+ setActiveTurn((current) => current ? { ...current, response: text } : current);
+ },
+ });
+ } catch (caught) {
+ if (controller.signal.aborted) throw caught;
+ const message = caught instanceof Error ? caught.message : "";
+ const key = infographicLocalAgentErrorKey(message, adapter.id);
+ throw new Error(key ? t(key) : message || t("infographic.aiError"));
+ }
+ const reply = parseInfographicLocalReply(raw, candidates, promptText);
+ response = reply.visibleText;
+ if (reply.proposal) {
+ proposal = reply.proposal;
+ setActiveTurn((current) => current ? { ...current, response, template: reply.proposal?.template, decision: reply.proposal?.explanation } : current);
+ } else if (reply.question) {
+ question = reply.question;
+ setActiveTurn((current) => current ? { ...current, response, question } : current);
+ } else throw new Error(t("infographic.aiInvalidResponse"));
+ } else await api.streamInfographicAgent({
prompt: promptText,
locale: i18n.resolvedLanguage,
...(existingTemplate ? { currentTemplate: existingTemplate } : {}),
currentContent,
candidates,
- history: history.filter((turn) => !turn.undoneAt && turn.kind !== "failed")
- .slice(-12).map((turn) => ({ prompt: turn.prompt, response: `${turn.kind === "clarified" ? "No infographic change was applied. Clarification: " : ""}${turn.response || turn.resultTitle}${turn.decision ? `\nDecision: ${turn.decision}` : ""}`.slice(0, 2000) })),
+ history: agentHistory,
}, { signal: controller.signal, onEvent: (event) => {
if (event.type === "text-delta") {
response += event.text;
diff --git a/apps/web/src/components/ai-sidebar/AiSidebar.tsx b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
index c7b53a57..1ae7f6f9 100644
--- a/apps/web/src/components/ai-sidebar/AiSidebar.tsx
+++ b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
@@ -48,19 +48,16 @@ import {
} from "@/lib/ai-attachments";
import { companionLocale } from "@/lib/companion-locale";
import {
- AI_SIDEBAR_ADAPTER_KEY,
- AI_SIDEBAR_ADAPTER_PATH_KEY,
AI_SIDEBAR_LOCAL_THREAD_KEY,
AI_SIDEBAR_LOCAL_THREADS_KEY,
AI_SIDEBAR_OPEN_KEY,
- AI_SIDEBAR_SOURCE_KEY,
AI_SIDEBAR_THREAD_KEY,
AI_SIDEBAR_WIDTH_KEY,
cancelDesktopAcp,
- desktopAcpAvailable,
promptDesktopAcp,
+ readAiSidebarAdapter,
+ readAiSidebarSource,
subscribeDesktopAcp,
- type DesktopAcpAdapterId,
type DesktopAcpEvent,
} from "@/lib/desktop-acp";
import {
@@ -232,10 +229,6 @@ export const readAiSidebarWidth = () => {
return Number.isFinite(parsed) ? clampSidebarWidth(parsed) : SIDEBAR_DEFAULT_WIDTH;
};
-const readAiSidebarSource = (): "builtin" | "local" => (
- readStorage(AI_SIDEBAR_SOURCE_KEY) === "local" && desktopAcpAvailable() ? "local" : "builtin"
-);
-
const THREAD_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const THREAD_RECENCY_MS = 30 * 24 * 60 * 60 * 1000;
@@ -350,12 +343,7 @@ function AiSidebarThreadMenu({
);
}
-const readLocalAdapter = (): { id: DesktopAcpAdapterId; path?: string } | null => {
- const id = readStorage(AI_SIDEBAR_ADAPTER_KEY);
- if (id !== "codex" && id !== "claudeCode" && id !== "antigravity" && id !== "openClaw" && id !== "hermesAgent" && id !== "grokBuild" && id !== "deepseekHarness" && id !== "piAgent" && id !== "workbuddyCn" && id !== "workbuddyIntl") return null;
- const path = readStorage(AI_SIDEBAR_ADAPTER_PATH_KEY)?.trim();
- return id === "antigravity" && path ? { id, path } : { id };
-};
+const readLocalAdapter = readAiSidebarAdapter;
const attachmentServerCode = (code: string | undefined) => (
code === "companion_attachment_unsupported" || code === "companion_attachment_expired" ? code : null
diff --git a/apps/web/src/components/ai-sidebar/InfographicSidebarSession.tsx b/apps/web/src/components/ai-sidebar/InfographicSidebarSession.tsx
index cd65316a..0c15bb94 100644
--- a/apps/web/src/components/ai-sidebar/InfographicSidebarSession.tsx
+++ b/apps/web/src/components/ai-sidebar/InfographicSidebarSession.tsx
@@ -14,7 +14,7 @@ import {
usePromptInputController,
} from "@/components/ai-elements/prompt-input";
import { cn } from "@/lib/utils";
-import { BuiltinAgentStatus } from "./BuiltinAgentStatus";
+import { SidebarAgentModeStatus } from "./SidebarAgentModeStatus";
const threadClassName = cn(
"gap-3 p-3 text-[13px] leading-[1.6]",
@@ -114,7 +114,7 @@ export function InfographicSidebarSession({
<Sparkles className="size-3.5 shrink-0" aria-hidden="true" />
<span className="truncate">{title}</span>
</span>
- <BuiltinAgentStatus />
+ <SidebarAgentModeStatus />
<Button
type="button"
size="icon-sm"
diff --git a/apps/web/src/components/ai-sidebar/SidebarAgentModeStatus.tsx b/apps/web/src/components/ai-sidebar/SidebarAgentModeStatus.tsx
new file mode 100644
index 00000000..8e6f98e8
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/SidebarAgentModeStatus.tsx
@@ -0,0 +1,48 @@
+import { useEffect, useState } from "react";
+import { useTranslation } from "react-i18next";
+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
+import { readAiSidebarAdapter, readAiSidebarSource, type AiSidebarSource } from "@/lib/desktop-acp";
+import { BuiltinAgentStatus } from "./BuiltinAgentStatus";
+
+const chipClassName = "ml-auto min-w-0 max-w-[46%] shrink-0 truncate rounded-sm text-right text-[11px] leading-4 text-slate-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-400";
+
+export function SidebarAgentModeStatus() {
+ const { t } = useTranslation();
+ const [source, setSource] = useState<AiSidebarSource>(readAiSidebarSource);
+ const [adapterId, setAdapterId] = useState(() => readAiSidebarAdapter()?.id ?? null);
+
+ useEffect(() => {
+ const sync = () => {
+ setSource(readAiSidebarSource());
+ setAdapterId(readAiSidebarAdapter()?.id ?? null);
+ };
+ sync();
+ window.addEventListener("focus", sync);
+ window.addEventListener("storage", sync);
+ document.addEventListener("visibilitychange", sync);
+ return () => {
+ window.removeEventListener("focus", sync);
+ window.removeEventListener("storage", sync);
+ document.removeEventListener("visibilitychange", sync);
+ };
+ }, []);
+
+ if (source !== "local") return <BuiltinAgentStatus />;
+
+ const label = adapterId ? t(`aiAssistant.agentSource.${adapterId}`) : t("aiAssistant.sidebar.localStatus");
+ return (
+ <TooltipProvider delayDuration={300}>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span className={chipClassName} data-ai-local-agent="" tabIndex={0} aria-label={label}>
+ {label}
+ </span>
+ </TooltipTrigger>
+ <TooltipContent className="max-w-xs" side="bottom">
+ <p>{label}</p>
+ <p className="mt-1">{t("aiAssistant.agentSource.localHint")}</p>
+ </TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ );
+}
diff --git a/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs b/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
index 4adb80ce..1ff73a50 100644
--- a/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
+++ b/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
@@ -18,8 +18,15 @@ describe("AI assistant modes", () => {
expect(sidebar).toContain("<InfographicSidebarSession");
expect(sidebar).toContain("<BuiltinAgentStatus");
const infographic = readFileSync(new URL("../ai-sidebar/InfographicSidebarSession.tsx", import.meta.url), "utf8");
+ const mode = readFileSync(new URL("../ai-sidebar/SidebarAgentModeStatus.tsx", import.meta.url), "utf8");
const status = readFileSync(new URL("../ai-sidebar/BuiltinAgentStatus.tsx", import.meta.url), "utf8");
- expect(infographic).toContain("<BuiltinAgentStatus");
+ const pane = readFileSync(new URL("../InfographicEditorPane.tsx", import.meta.url), "utf8");
+ expect(infographic).toContain("<SidebarAgentModeStatus");
+ expect(mode).toContain("readAiSidebarSource");
+ expect(mode).toContain("<BuiltinAgentStatus");
+ expect(mode).toContain('data-ai-local-agent=""');
+ expect(pane).toContain('readAiSidebarSource() === "local"');
+ expect(pane).toContain("noteAccess: false");
expect(status).toContain('data-ai-builtin-agent=""');
expect(status).toContain('t("aiAssistant.agentSource.builtin")');
expect(status).toContain('t("aiModel.noDefaultModel")');
diff --git a/apps/web/src/lib/desktop-acp.test.mjs b/apps/web/src/lib/desktop-acp.test.mjs
index b2aa9ed4..8bd52807 100644
--- a/apps/web/src/lib/desktop-acp.test.mjs
+++ b/apps/web/src/lib/desktop-acp.test.mjs
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
-import { displayedDesktopAcpAdapter } from "./desktop-acp.ts";
+import { aiSidebarAdapterFromStorage, aiSidebarSourceFromStorage, displayedDesktopAcpAdapter } from "./desktop-acp.ts";
test("a checked custom Antigravity executable takes priority over the managed connector status", () => {
const managed = { id: "antigravity", label: "Antigravity", state: "failed", version: "1.2.1", managed: true };
@@ -16,6 +16,15 @@ test("a newly installed managed connector takes priority over an older probe", (
expect(displayedDesktopAcpAdapter({ id: "antigravity", path: "", listed: [current], probed: previous })).toBe(current);
});
+test("the sidebar follows a stored local agent only when the desktop bridge exists", () => {
+ expect(aiSidebarSourceFromStorage("local", true)).toBe("local");
+ expect(aiSidebarSourceFromStorage("local", false)).toBe("builtin");
+ expect(aiSidebarSourceFromStorage("builtin", true)).toBe("builtin");
+ expect(aiSidebarAdapterFromStorage("codex", "/tmp/ignored")).toEqual({ id: "codex" });
+ expect(aiSidebarAdapterFromStorage("antigravity", " /tmp/agy ")).toEqual({ id: "antigravity", path: "/tmp/agy" });
+ expect(aiSidebarAdapterFromStorage("missing", "")).toBeNull();
+});
+
test("a prompt-time login failure takes priority over an earlier successful session probe", () => {
const current = { id: "grokBuild", label: "Grok Build", state: "needs_login", authMethods: [{ id: "browser", name: "Browser" }] };
const earlierProbe = { id: "grokBuild", label: "Grok Build", state: "available" };
diff --git a/apps/web/src/lib/desktop-acp.ts b/apps/web/src/lib/desktop-acp.ts
index e99aea7b..0f4ba31f 100644
--- a/apps/web/src/lib/desktop-acp.ts
+++ b/apps/web/src/lib/desktop-acp.ts
@@ -49,6 +49,9 @@ export type DesktopAcpPromptInput = {
prompt: string;
contextText?: string;
attachments?: DesktopAcpAttachment[];
+ // Infographic edits apply a validated proposal in the editor. They must not
+ // also grant the agent write access to the note document.
+ noteAccess?: boolean;
};
export type DesktopAcpPromptResult = {
@@ -75,6 +78,39 @@ export const AI_SIDEBAR_ADAPTER_PATH_KEY = "edgeever.aiSidebar.adapterPath";
export type AiSidebarSource = "builtin" | "local";
+const AI_SIDEBAR_ADAPTER_IDS = new Set<string>([
+ "codex", "claudeCode", "antigravity", "openClaw", "hermesAgent", "grokBuild", "deepseekHarness", "piAgent", "workbuddyCn", "workbuddyIntl",
+]);
+
+export const aiSidebarSourceFromStorage = (value: string | null, desktopAvailable: boolean): AiSidebarSource => (
+ value === "local" && desktopAvailable ? "local" : "builtin"
+);
+
+export const aiSidebarAdapterFromStorage = (id: string | null, path: string | null): { id: DesktopAcpAdapterId; path?: string } | null => {
+ if (!id || !AI_SIDEBAR_ADAPTER_IDS.has(id)) return null;
+ const adapterId = id as DesktopAcpAdapterId;
+ const trimmed = path?.trim() ?? "";
+ return adapterId === "antigravity" && trimmed ? { id: adapterId, path: trimmed } : { id: adapterId };
+};
+
+const readStorageItem = (key: string) => {
+ if (typeof window === "undefined") return null;
+ try {
+ return window.localStorage.getItem(key);
+ } catch {
+ return null;
+ }
+};
+
+export const readAiSidebarSource = (): AiSidebarSource => (
+ aiSidebarSourceFromStorage(readStorageItem(AI_SIDEBAR_SOURCE_KEY), desktopAcpAvailable())
+);
+
+export const readAiSidebarAdapter = () => aiSidebarAdapterFromStorage(
+ readStorageItem(AI_SIDEBAR_ADAPTER_KEY),
+ readStorageItem(AI_SIDEBAR_ADAPTER_PATH_KEY),
+);
+
const bridge = () => (typeof window === "undefined" ? undefined : window.edgeeverDesktop);
export const desktopAcpAvailable = () => Boolean(bridge()?.listAcpAdapters);
diff --git a/apps/web/src/lib/infographic-local-agent.test.mjs b/apps/web/src/lib/infographic-local-agent.test.mjs
new file mode 100644
index 00000000..b59b5ea0
--- /dev/null
+++ b/apps/web/src/lib/infographic-local-agent.test.mjs
@@ -0,0 +1,91 @@
+import { expect, test } from "bun:test";
+import {
+ buildInfographicLocalAgentContext,
+ collectInfographicLocalAgentText,
+ parseInfographicLocalReply,
+ visibleInfographicAgentReply,
+} from "./infographic-local-agent.ts";
+
+const chartReply = `已换成小米集团的季度营收。
+\`\`\`json
+{"type":"proposal","template":"chart-column-simple","explanation":"换成小米公开营收。","data":{"title":"小米集团2024年季度营收","values":[{"label":"Q1 2024","value":755},{"label":"Q2 2024","value":889}]}}
+\`\`\``;
+
+test("a local infographic reply keeps the sentence and one validated proposal", () => {
+ expect(visibleInfographicAgentReply(chartReply)).toBe("已换成小米集团的季度营收。");
+ const reply = parseInfographicLocalReply(chartReply, ["chart-column-simple"], "给我换成小米的。");
+ expect(reply.rejected).toBe(false);
+ expect(reply.proposal?.template).toBe("chart-column-simple");
+ expect(reply.proposal?.data.values).toEqual([
+ { label: "Q1 2024", value: 755 },
+ { label: "Q2 2024", value: 889 },
+ ]);
+ expect(reply.visibleText).toBe("已换成小米集团的季度营收。");
+});
+
+test("a clarification stays text and does not change the infographic", () => {
+ const raw = `需要先确认年份。\n\`\`\`json\n{"type":"question","question":"去年是指2024年吗?"}\n\`\`\``;
+ const reply = parseInfographicLocalReply(raw, ["chart-column-simple"], "去年四个季度的营收");
+ expect(reply.proposal).toBeNull();
+ expect(reply.rejected).toBe(false);
+ expect(reply.question).toBe("去年是指2024年吗?");
+ expect(parseInfographicLocalReply("我先说明口径,暂不改图。", ["chart-column-simple"], "换个数").question).toBe("我先说明口径,暂不改图。");
+});
+
+test("a history request rejects a chart proposal", () => {
+ const reply = parseInfographicLocalReply(chartReply, ["chart-column-simple", "sequence-timeline-simple"], "展示字节跳动发展历程");
+ expect(reply.proposal).toBeNull();
+ expect(reply.rejected).toBe(true);
+});
+
+test("the local prompt forbids note writes and lists only allowed templates", () => {
+ const context = buildInfographicLocalAgentContext({
+ prompt: "换成小米",
+ currentTemplate: "chart-column-simple",
+ currentContent: "{\"template\":\"chart-column-simple\"}",
+ candidates: ["chart-column-simple"],
+ history: [{ prompt: "做营收图", response: "已生成" }],
+ });
+ expect(context).toContain("Do not edit files");
+ expect(context).toContain("Allowed templates: chart-column-simple");
+ expect(context).toContain("User: 做营收图");
+});
+
+test("local infographic text follows one ACP request and can be cancelled", async () => {
+ let emit = () => undefined;
+ let cancelId = "";
+ const controller = new AbortController();
+ const text = await collectInfographicLocalAgentText({
+ request: { adapterId: "codex", prompt: "换成小米", noteAccess: false },
+ signal: controller.signal,
+ deps: {
+ prompt: async () => {
+ emit({ requestId: "other", type: "text-delta", text: "ignore" });
+ emit({ requestId: "req-1", type: "text-delta", text: "已更新" });
+ emit({ requestId: "req-1", type: "done" });
+ return { requestId: "req-1" };
+ },
+ subscribe: (callback) => {
+ emit = callback;
+ return () => undefined;
+ },
+ cancel: async (requestId) => { cancelId = requestId; },
+ },
+ });
+ expect(text).toBe("已更新");
+
+ const aborted = new AbortController();
+ await expect(collectInfographicLocalAgentText({
+ request: { adapterId: "codex", prompt: "停止", noteAccess: false },
+ signal: aborted.signal,
+ deps: {
+ prompt: async () => {
+ aborted.abort();
+ return { requestId: "req-2" };
+ },
+ subscribe: () => () => undefined,
+ cancel: async (requestId) => { cancelId = requestId; },
+ },
+ })).rejects.toThrow("aborted");
+ expect(cancelId).toBe("req-2");
+});
diff --git a/apps/web/src/lib/infographic-local-agent.ts b/apps/web/src/lib/infographic-local-agent.ts
new file mode 100644
index 00000000..3b2f160b
--- /dev/null
+++ b/apps/web/src/lib/infographic-local-agent.ts
@@ -0,0 +1,191 @@
+import { infographicRequestsTimeline } from "@edgeever/shared";
+import {
+ cancelDesktopAcp,
+ promptDesktopAcp,
+ subscribeDesktopAcp,
+ type DesktopAcpEvent,
+ type DesktopAcpPromptInput,
+} from "./desktop-acp";
+import { parseGeneratedOfficialData } from "./infographic-generation";
+
+export type InfographicLocalProposal = {
+ template: string;
+ data: Record<string, unknown>;
+ explanation: string;
+};
+
+export type InfographicLocalReply = {
+ visibleText: string;
+ proposal: InfographicLocalProposal | null;
+ question: string;
+ rejected: boolean;
+};
+
+const DATA_SHAPES = "chart={title,values:[{label,value:number}]}; comparison={title,compares:[{label,children:[{label,desc}]}]}; quadrant={title,compares:[four items]}; hierarchy={title,root:{label,children:[...]}}; relation={title,nodes:[{id,label}],relations:[{from,to}]}; sequence={title,sequences:[{label,desc}]}; list={title,lists:[{label,desc}]}.";
+
+export const buildInfographicLocalAgentContext = (input: {
+ prompt: string;
+ currentTemplate?: string;
+ currentContent: string;
+ candidates: string[];
+ history: Array<{ prompt: string; response: string }>;
+}) => {
+ const history = input.history.slice(-12).map((turn) => `User: ${turn.prompt}\nAssistant: ${turn.response}`).join("\n\n");
+ return [
+ "You edit one AntV infographic in EdgeEver. The current infographic content is the source of truth. Keep the current template for content-only changes, including replacing one comparison subject. Change the template when the data relationship changes. Choose only from the allowed template IDs. Preserve information the user did not ask to change.",
+ infographicRequestsTimeline(input.prompt)
+ ? "The user requested one subject's development history. Choose an allowed sequence-timeline template and chronological events. Do not ask for clarification."
+ : "Ask a short clarification only when you cannot make a useful edit.",
+ "Do not edit files, run commands, or change the note yourself. EdgeEver applies one validated change.",
+ "Reply in the user's language. First write one short sentence. Then add one fenced json block and nothing after it.",
+ 'The json block is either {"type":"proposal","template":"...","data":{...},"explanation":"..."} or {"type":"question","question":"..."}.',
+ "Binary comparisons need exactly two compares with matching children. Use concise labels and complete content.",
+ `Current template: ${input.currentTemplate ?? "none"}.`,
+ `Current infographic content: ${input.currentContent.slice(0, 12_000) || "none"}.`,
+ `Allowed templates: ${input.candidates.join(", ")}.`,
+ `Data shapes: ${DATA_SHAPES}`,
+ history ? `Previous turns:\n${history}` : "",
+ ].filter(Boolean).join("\n");
+};
+
+export const visibleInfographicAgentReply = (raw: string) => {
+ const withoutFences = raw.replace(/```(?:json)?\s*[\s\S]*?```/gi, "");
+ const openFence = withoutFences.indexOf("```");
+ const clipped = (openFence >= 0 ? withoutFences.slice(0, openFence) : withoutFences).trim();
+ const payload = clipped.search(/\{\s*"(?:type|template|question)"/);
+ if (payload < 0) return clipped;
+ return clipped.slice(0, payload).trim();
+};
+
+const jsonRecord = (raw: string): Record<string, unknown> | null => {
+ const fenced = raw.match(/```(?:json)?\s*([\s\S]*?)```/i)?.[1] ?? raw;
+ const first = fenced.indexOf("{");
+ const last = fenced.lastIndexOf("}");
+ if (first < 0 || last <= first) return null;
+ try {
+ const value: unknown = JSON.parse(fenced.slice(first, last + 1));
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : null;
+ } catch {
+ return null;
+ }
+};
+
+const explanationOf = (record: Record<string, unknown>) => (
+ typeof record.explanation === "string" ? record.explanation.trim().slice(0, 500) : ""
+);
+
+export const parseInfographicLocalReply = (raw: string, candidates: string[], prompt: string): InfographicLocalReply => {
+ const visibleText = visibleInfographicAgentReply(raw);
+ const record = jsonRecord(raw);
+ const questionText = record && typeof record.question === "string" ? record.question.trim().slice(0, 300) : "";
+ if (record?.type === "question" && questionText) {
+ return { visibleText: visibleText || questionText, proposal: null, question: questionText, rejected: false };
+ }
+ if (record && (record.type === "proposal" || typeof record.template === "string") && record.type !== "question") {
+ const template = typeof record.template === "string" ? record.template : "";
+ const timelineMismatch = infographicRequestsTimeline(prompt) && !template.startsWith("sequence-timeline-");
+ const data = !timelineMismatch && candidates.includes(template) ? parseGeneratedOfficialData(JSON.stringify(record), template) : null;
+ if (template && data) {
+ const explanation = explanationOf(record);
+ return {
+ visibleText: visibleText || explanation,
+ proposal: { template, data, explanation },
+ question: "",
+ rejected: false,
+ };
+ }
+ if (record.type === "proposal" || timelineMismatch) {
+ return { visibleText: visibleText || explanationOf(record), proposal: null, question: "", rejected: true };
+ }
+ }
+ if (visibleText) return { visibleText, proposal: null, question: visibleText.slice(0, 300), rejected: false };
+ return { visibleText: "", proposal: null, question: "", rejected: true };
+};
+
+export const infographicLocalAgentErrorKey = (message: string, adapterId: string | null) => {
+ if (message === "needs_login") {
+ return adapterId === "workbuddyCn" || adapterId === "workbuddyIntl"
+ ? "aiAssistant.sidebar.workbuddyLoginRequired"
+ : "aiAssistant.sidebar.localLoginRequired";
+ }
+ if (message === "agent_refused") return "aiAssistant.sidebar.agentRefused";
+ if (message === "note_access_unavailable") return "aiAssistant.sidebar.noteAccessUnavailable";
+ if (message === "not_installed" || message === "desktop_acp_unavailable") return "aiAssistant.sidebar.localMissing";
+ return null;
+};
+
+type AcpTextDeps = {
+ prompt: (input: DesktopAcpPromptInput) => Promise<{ requestId: string }>;
+ subscribe: (callback: (event: DesktopAcpEvent) => void) => () => void;
+ cancel: (requestId: string) => Promise<unknown>;
+};
+
+export const collectInfographicLocalAgentText = async ({
+ request,
+ signal,
+ onText,
+ deps,
+}: {
+ request: DesktopAcpPromptInput;
+ signal: AbortSignal;
+ onText?: (visible: string) => void;
+ deps?: AcpTextDeps;
+}) => {
+ const prompt = deps?.prompt ?? promptDesktopAcp;
+ const subscribe = deps?.subscribe ?? subscribeDesktopAcp;
+ const cancel = deps?.cancel ?? cancelDesktopAcp;
+ if (signal.aborted) throw new DOMException("aborted", "AbortError");
+ let requestId = "";
+ let text = "";
+ let settled = false;
+ let failure: Error | null = null;
+ const pending: DesktopAcpEvent[] = [];
+ let resolveDone: (() => void) | null = null;
+ let rejectDone: ((error: Error) => void) | null = null;
+ const done = new Promise<void>((resolve, reject) => {
+ resolveDone = resolve;
+ rejectDone = reject;
+ });
+ void done.catch(() => undefined);
+ const finish = (error?: Error) => {
+ if (settled) return;
+ settled = true;
+ failure = error ?? null;
+ if (error) rejectDone?.(error);
+ else resolveDone?.();
+ };
+ const apply = (event: DesktopAcpEvent) => {
+ if (!requestId || event.requestId !== requestId || settled) return;
+ if (event.type === "text-delta") {
+ text += event.text;
+ onText?.(visibleInfographicAgentReply(text));
+ return;
+ }
+ if (event.type === "error") finish(new Error(event.message || "connection_failed"));
+ if (event.type === "done") finish();
+ };
+ const unsubscribe = subscribe((event) => {
+ if (!requestId) pending.push(event);
+ else apply(event);
+ });
+ const onAbort = () => {
+ if (requestId) void cancel(requestId);
+ finish(new DOMException("aborted", "AbortError"));
+ };
+ signal.addEventListener("abort", onAbort, { once: true });
+ try {
+ const started = await prompt(request);
+ requestId = started.requestId;
+ if (signal.aborted) {
+ void cancel(requestId);
+ throw new DOMException("aborted", "AbortError");
+ }
+ for (const event of pending.splice(0)) apply(event);
+ if (!settled) await done;
+ if (failure) throw failure;
+ return text;
+ } finally {
+ signal.removeEventListener("abort", onAbort);
+ unsubscribe();
+ }
+};
diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.ts
index b4e1048d..798e50e0 100644
--- a/apps/web/src/vite-env.d.ts
+++ b/apps/web/src/vite-env.d.ts
@@ -133,6 +133,7 @@ interface EdgeEverDesktopBridge {
prompt: string;
contextText?: string;
attachments?: Array<{ filename: string; mediaType: string; dataBase64: string }>;
+ noteAccess?: boolean;
}): Promise<{ requestId: string; rejectedAttachments?: Array<{ filename: string; reason: string }> }>;
cancelAcp?(requestId: string): Promise<{ ok: true }>;
onAcpEvent?(callback: (event: