Skip to content

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

feat(web): replace the AI dialog with a companion sidebar

tianma-if871a9325d5a5
diff --git a/apps/api/src/companion-agent-tools.ts b/apps/api/src/companion-agent-tools.ts
index 962ec7d5..55be779a 100644
--- a/apps/api/src/companion-agent-tools.ts
+++ b/apps/api/src/companion-agent-tools.ts
@@ -1,4 +1,4 @@
-import type { CompanionSource, CompanionTodo, CompanionToolCall, CompanionToolDefinition, CompanionTurnInput, MemoDetail, MemoSummary } from "@edgeever/shared";
+import { COMPANION_NOTE_EDIT_MAX_CHARS, parseDiagramDocument, type CompanionSource, type CompanionTodo, type CompanionToolCall, type CompanionToolDefinition, type CompanionTurnInput, type MemoDetail, type MemoSummary } from "@edgeever/shared";
import type { DatabaseAdapter } from "./storage-contract";
import type { AppContext } from "./api-context";
import type { CompanionScope } from "./companion-service";
@@ -134,7 +134,9 @@ const companionMcpCatalog = (input: CompanionTurnInput) => COMPANION_MCP_TOOLS.f
const companionMcpDescription = (definition: (typeof COMPANION_MCP_TOOLS)[number]) => {
const readOnly = definition.annotations.readOnlyHint;
const autoApply = AUTO_APPLY_WRITES.has(definition.name);
- const execution = readOnly || autoApply
+ const execution = definition.name === "update_memo"
+ ? " Title, tags, and notebook changes execute immediately. Including contentMarkdown returns awaiting_user_confirmation and does not change the note until the user confirms. Never describe that proposal as applied."
+ : readOnly || autoApply
? autoApply
? " This executes immediately. New notes are created in the named notebook. Trashed notes go to the recycle bin; edits keep revision history."
: ""
@@ -252,6 +254,21 @@ export function createCompanionTools(args: { db: DatabaseAdapter; scope: Compani
return done(await proposeCompanionToolAction(args.db, args.scope, args.input.id,
definition.name, parameters_, typeof _reason === "string" ? _reason : definition.title, session.cursor ?? current, inspected));
}
+ if (definition.name === "update_memo" && parameters_.dryRun !== true && typeof parameters_.contentMarkdown === "string") {
+ const memo = await getMemoDetail(args.db, args.scope.workspaceId, String(parameters_.memoId));
+ if (!memo || inspected.get(memo.id) !== memo.revision) {
+ throw new AppError("companion_action_unread", "Read the complete source notes before changing their content.", 400);
+ }
+ if (parseDiagramDocument(memo.contentMarkdown)) {
+ throw new AppError("invalid_params", "This is an editable diagram. Change it with update_diagram, not update_memo.", 400);
+ }
+ if (memo.contentMarkdown.length > COMPANION_NOTE_EDIT_MAX_CHARS || parameters_.contentMarkdown.length > COMPANION_NOTE_EDIT_MAX_CHARS) {
+ throw new AppError("invalid_params", "This note edit is too large to review. Split it into a smaller change.", 400);
+ }
+ return done(await proposeCompanionToolAction(args.db, args.scope, args.input.id,
+ definition.name, parameters_, typeof _reason === "string" ? _reason : definition.title, session.cursor ?? current, inspected, [],
+ { baseContentMarkdown: memo.contentMarkdown }));
+ }
if (autoApply && parameters_.dryRun !== true && (definition.name === "update_memo" || definition.name === "update_diagram" || definition.name === "restore_memo_revision")) {
const memo = await getMemoDetail(args.db, args.scope.workspaceId, String(parameters_.memoId));
if (!memo || inspected.get(memo.id) !== memo.revision) {
diff --git a/apps/api/src/companion-attachments.test.mjs b/apps/api/src/companion-attachments.test.mjs
new file mode 100644
index 00000000..bbfc4ad4
--- /dev/null
+++ b/apps/api/src/companion-attachments.test.mjs
@@ -0,0 +1,197 @@
+import { afterEach, describe, expect, test } from "bun:test";
+import { Database } from "bun:sqlite";
+import { globSync, readFileSync } from "node:fs";
+import { Hono } from "hono";
+import { MockLanguageModelV4 } from "ai/test";
+import { simulateReadableStream } from "ai";
+import { createSelfHostedStorageAdapter } from "./self-hosted-storage-adapter.ts";
+import { registerCompanionRoutes } from "./companion-routes.ts";
+import { beginCompanionTurn, checkpointCompanionTurn, getCompanionTurn, listCompanionTurns, mapCompanionTurn } from "./companion-service.ts";
+import { prepareCompanionTurn } from "./companion-prepare.ts";
+import { streamCompanion } from "./companion-runtime.ts";
+import { bindCompanionTurnAttachments, loadCompanionAttachmentParts, storeCompanionAttachment } from "./companion-attachments.ts";
+import { parsePreparedCompanion } from "../../../packages/client/src/companion-direct-stream.ts";
+
+const databases = [];
+afterEach(() => databases.splice(0).forEach((db) => db.close()));
+const scope = { workspaceId: "ws_test", ownerId: "owner" };
+const credentials = { provider: "anthropic", baseUrl: "https://api.anthropic.com", apiKey: "test", modelId: "claude" };
+const finish = { type: "finish", finishReason: { unified: "stop" }, usage: { inputTokens: { total: 1 }, outputTokens: { total: 1 } } };
+
+function setup(options = {}) {
+ const sqlite = new Database(":memory:");
+ databases.push(sqlite);
+ for (const file of globSync("migrations/*.sql").sort()) sqlite.exec(readFileSync(file, "utf8"));
+ sqlite.query("INSERT INTO workspaces(id, name) VALUES (?, 'Test')").run(scope.workspaceId);
+ const storage = createSelfHostedStorageAdapter(sqlite, "/tmp/edgeever-companion-attachment-test-unused");
+ const db = storage.db;
+ const auth = { kind: "user", actorType: "user", actorId: scope.ownerId, workspaceId: scope.workspaceId, scopes: [], role: "member" };
+ let modelLoads = 0;
+ const app = new Hono();
+ app.use("*", async (c, next) => { c.set("auth", auth); await next(); });
+ registerCompanionRoutes(app, {
+ isDemoMode: () => false,
+ loadCredentials: async () => options.credentials ?? credentials,
+ loadModel: async () => { modelLoads += 1; return { modelId: "mock-model" }; },
+ stream: async () => { throw new Error("proxy stream should not run"); },
+ });
+ const request = (path, body) => app.request(`/api/v1/companion/${path}`, {
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body),
+ }, { storage });
+ const turnInput = (message, extra = {}) => ({
+ id: crypto.randomUUID(), threadId: crypto.randomUUID(), message, allowNotes: false, useMemory: false, locale: "en-US", ...extra,
+ });
+ return { sqlite, db, request, modelLoads: () => modelLoads, turnInput };
+}
+
+describe("companion attachments", () => {
+ test("inlines a text upload into the current user message without storing bytes on the turn", async () => {
+ const f = setup();
+ const text = "hello notes";
+ const base64Data = Buffer.from(text).toString("base64");
+ const threadId = crypto.randomUUID();
+ const prior = f.turnInput("earlier", { threadId });
+ const priorRow = await beginCompanionTurn(f.db, scope, prior, "mock");
+ await checkpointCompanionTurn(f.db, scope, priorRow, "done", [], "completed");
+ const uploaded = await (await f.request("attachments", { filename: "notes.txt", mediaType: "text/plain", base64Data })).json();
+ const payload = f.turnInput("Read the file", { threadId, attachmentIds: [uploaded.attachment.id] });
+ const response = await f.request("turns/prepare", payload);
+ expect(response.status).toBe(200);
+ const prepared = await response.json();
+ expect(prepared.messages[0]).toEqual({ role: "user", content: "earlier" });
+ expect(prepared.messages[1]).toEqual({ role: "assistant", content: "done" });
+ expect(prepared.messages.at(-1)).toEqual({
+ role: "user",
+ content: [
+ { type: "text", text: "Read the file" },
+ { type: "text", text: "Attached file notes.txt (text/plain):\nhello notes" },
+ ],
+ });
+ const row = await getCompanionTurn(f.db, scope, payload.id);
+ expect(row.attachment_meta_json).not.toContain(base64Data);
+ expect(row.attachment_meta_json).not.toContain(text);
+ expect(mapCompanionTurn(row).attachments).toEqual([{
+ id: uploaded.attachment.id, filename: "notes.txt", mediaType: "text/plain", byteLength: Buffer.byteLength(text),
+ }]);
+ expect(JSON.stringify(mapCompanionTurn(row))).not.toContain(base64Data);
+ const resumed = await prepareCompanionTurn({
+ db: f.db, scope, input: payload, row, credentials, resume: { response: "Working" },
+ });
+ expect(typeof resumed.messages.at(-1).content).toBe("string");
+ expect(resumed.messages.at(-3).content).toEqual(prepared.messages.at(-1).content);
+ const model = new MockLanguageModelV4({ doStream: async () => ({ stream: simulateReadableStream({ chunks: [
+ { type: "text-start", id: "1" }, { type: "text-delta", id: "1", delta: "ok" }, { type: "text-end", id: "1" }, finish,
+ ] }) }) });
+ const result = await streamCompanion({
+ db: f.db, scope, input: payload, model, memories: [], history: await listCompanionTurns(f.db, scope, threadId),
+ revision: row.memory_revision, signal: new AbortController().signal, sources: [], assertActive: async () => {},
+ provider: "anthropic",
+ });
+ expect(await result.text).toBe("ok");
+ const prompt = model.doStreamCalls[0].prompt;
+ expect(prompt.at(-1)).toMatchObject({
+ role: "user",
+ content: [
+ { type: "text", text: "Read the file" },
+ { type: "text", text: "Attached file notes.txt (text/plain):\nhello notes" },
+ ],
+ });
+ expect(JSON.stringify(prompt)).not.toContain(base64Data);
+ });
+
+ test("round-trips image bytes through base64 chunks", async () => {
+ const f = setup();
+ const base64Data = "A".repeat(700_004);
+ const stored = await storeCompanionAttachment(f.db, scope, { filename: "pic.png", mediaType: "image/png", base64Data });
+ expect(f.sqlite.query("SELECT length(data) AS n FROM companion_turn_attachment_parts WHERE attachment_id = ? ORDER BY part_index")
+ .all(stored.attachment.id).map((row) => row.n)).toEqual([700_000, 4]);
+ const input = f.turnInput("See this");
+ const row = await beginCompanionTurn(f.db, scope, input, "mock");
+ await bindCompanionTurnAttachments(f.db, scope, row.id, [stored.attachment.id]);
+ expect(await loadCompanionAttachmentParts(f.db, scope, row.id, "openai-compatible")).toEqual([
+ { type: "image", image: base64Data, mediaType: "image/png" },
+ ]);
+ });
+
+ test("rejects a PDF for an openai-compatible model before a turn starts", async () => {
+ const f = setup({ credentials: { provider: "openai-compatible", baseUrl: "https://example.invalid/v1", apiKey: "k", modelId: "m" } });
+ const uploaded = await (await f.request("attachments", {
+ filename: "brief.pdf", mediaType: "application/pdf", base64Data: Buffer.from("%PDF-1.4").toString("base64"),
+ })).json();
+ const response = await f.request("turns", f.turnInput("Read the pdf", { attachmentIds: [uploaded.attachment.id] }));
+ expect(response.status).toBe(400);
+ expect(await response.json()).toMatchObject({ error: { code: "companion_attachment_unsupported" } });
+ expect(f.modelLoads()).toBe(0);
+ expect(f.sqlite.query("SELECT COUNT(*) AS n FROM companion_turns WHERE status = 'running'").get().n).toBe(0);
+ });
+
+ test("places a PDF on the current user message for anthropic", async () => {
+ const f = setup();
+ const base64Data = Buffer.from("%PDF-1.4").toString("base64");
+ const uploaded = await (await f.request("attachments", { filename: "brief.pdf", mediaType: "application/pdf", base64Data })).json();
+ const payload = f.turnInput("Read the pdf", { attachmentIds: [uploaded.attachment.id] });
+ const prepared = await (await f.request("turns/prepare", payload)).json();
+ expect(prepared.messages.at(-1)).toEqual({
+ role: "user",
+ content: [
+ { type: "text", text: "Read the pdf" },
+ { type: "file", data: base64Data, mediaType: "application/pdf", filename: "brief.pdf" },
+ ],
+ });
+ });
+
+ test("rejects an expired or foreign-turn attachment", async () => {
+ const f = setup();
+ const text = await storeCompanionAttachment(f.db, scope, {
+ filename: "notes.txt", mediaType: "text/plain", base64Data: Buffer.from("stale").toString("base64"),
+ });
+ f.sqlite.query("UPDATE companion_turn_attachments SET expires_at = '2000-01-01T00:00:00.000Z' WHERE id = ?").run(text.attachment.id);
+ const expiredTurn = await beginCompanionTurn(f.db, scope, f.turnInput("expired"), "mock");
+ await expect(bindCompanionTurnAttachments(f.db, scope, expiredTurn.id, [text.attachment.id]))
+ .rejects.toMatchObject({ code: "companion_attachment_expired" });
+ await checkpointCompanionTurn(f.db, scope, expiredTurn, "", [], "cancelled");
+
+ const image = await storeCompanionAttachment(f.db, scope, { filename: "pic.png", mediaType: "image/png", base64Data: "AAAA" });
+ const first = await beginCompanionTurn(f.db, scope, f.turnInput("first"), "mock");
+ await bindCompanionTurnAttachments(f.db, scope, first.id, [image.attachment.id]);
+ await checkpointCompanionTurn(f.db, scope, first, "done", [], "completed");
+ const second = await beginCompanionTurn(f.db, scope, f.turnInput("second"), "mock");
+ await expect(bindCompanionTurnAttachments(f.db, scope, second.id, [image.attachment.id]))
+ .rejects.toMatchObject({ code: "companion_attachment_unavailable" });
+ await expect(bindCompanionTurnAttachments(f.db, scope, second.id, [image.attachment.id, image.attachment.id]))
+ .rejects.toMatchObject({ code: "companion_attachment_unavailable" });
+ await checkpointCompanionTurn(f.db, scope, second, "", [], "cancelled");
+
+ const stale = await storeCompanionAttachment(f.db, scope, {
+ filename: "again.txt", mediaType: "text/plain", base64Data: Buffer.from("later").toString("base64"),
+ });
+ f.sqlite.query("UPDATE companion_turn_attachments SET expires_at = '2000-01-01T00:00:00.000Z' WHERE id = ?").run(stale.attachment.id);
+ const routed = await f.request("turns", f.turnInput("again", { attachmentIds: [stale.attachment.id] }));
+ expect(routed.status).toBe(409);
+ expect(await routed.json()).toMatchObject({ error: { code: "companion_attachment_expired" } });
+ expect(f.sqlite.query("SELECT status FROM companion_turns WHERE message = 'again'").get().status).toBe("cancelled");
+ });
+
+ test("direct parser keeps a user content array and still accepts string messages", () => {
+ const base = {
+ turn: { id: "11111111-1111-4111-8111-111111111111" },
+ provider: "anthropic", baseUrl: "https://api.anthropic.com", apiKey: "k", modelId: "claude",
+ instructions: "Be brief.", maxSteps: 8, maxOutputTokens: 2048, tools: [],
+ };
+ expect(parsePreparedCompanion({ ...base, messages: [{ role: "user", content: "hi" }] })?.messages)
+ .toEqual([{ role: "user", content: "hi" }]);
+ const parts = [
+ { type: "text", text: "hi" },
+ { type: "image", image: "aaaa", mediaType: "image/png" },
+ { type: "file", data: "bbbb", mediaType: "application/pdf", filename: "a.pdf" },
+ ];
+ expect(parsePreparedCompanion({
+ ...base,
+ messages: [{ role: "user", content: parts }, { role: "assistant", content: "ok" }],
+ })?.messages).toEqual([{ role: "user", content: parts }, { role: "assistant", content: "ok" }]);
+ expect(parsePreparedCompanion({
+ ...base,
+ messages: [{ role: "user", content: "hi" }, { role: "assistant", content: [{ type: "text", text: "no" }] }],
+ })).toBeNull();
+ });
+});
diff --git a/apps/api/src/companion-attachments.ts b/apps/api/src/companion-attachments.ts
new file mode 100644
index 00000000..15388c4e
--- /dev/null
+++ b/apps/api/src/companion-attachments.ts
@@ -0,0 +1,170 @@
+import {
+ AI_ATTACHMENT_MEDIA_TYPES,
+ companionSupportsAttachment,
+ getBase64DecodedByteLength,
+ isAiTextAttachment,
+ type AiAttachmentInput,
+ type AiAttachmentMediaType,
+ type AiProvider,
+ type CompanionModelContentPart,
+ type CompanionTurnAttachmentMeta,
+} from "@edgeever/shared";
+import { AppError } from "./app-error";
+import type { CompanionScope } from "./companion-service";
+import type { DatabaseAdapter } from "./storage-contract";
+
+// D1 binds large strings poorly; keep each stored chunk under this size.
+const PART_CHARS = 700_000;
+const ATTACHMENT_TTL_MS = 24 * 60 * 60 * 1000;
+
+type AttachmentRow = {
+ id: string;
+ turn_id: string | null;
+ filename: string;
+ media_type: string;
+ byte_length: number;
+ expires_at: string;
+};
+
+const scopeBindings = (scope: CompanionScope) => [scope.workspaceId, scope.ownerId];
+
+const chunkBase64 = (data: string) => {
+ const parts: string[] = [];
+ for (let index = 0; index < data.length; index += PART_CHARS) parts.push(data.slice(index, index + PART_CHARS));
+ return parts;
+};
+
+const decodeBase64Text = (data: string) => {
+ try {
+ const binary = atob(data);
+ const bytes = Uint8Array.from(binary, (character) => character.charCodeAt(0));
+ return new TextDecoder().decode(bytes);
+ } catch {
+ throw new AppError("companion_attachment_expired", "An attachment expired. Upload it again.", 409);
+ }
+};
+
+const isKnownMediaType = (mediaType: string): mediaType is AiAttachmentMediaType =>
+ (AI_ATTACHMENT_MEDIA_TYPES as readonly string[]).includes(mediaType);
+
+// Delete parts explicitly. Do not rely on the foreign-key cascade.
+const purgeExpiredCompanionAttachments = async (db: DatabaseAdapter, scope: CompanionScope, now: string) => {
+ await db.batch([
+ db.prepare(`DELETE FROM companion_turn_attachment_parts WHERE attachment_id IN (
+ SELECT id FROM companion_turn_attachments WHERE workspace_id = ? AND owner_id = ? AND expires_at <= ?
+ )`).bind(...scopeBindings(scope), now),
+ db.prepare("DELETE FROM companion_turn_attachments WHERE workspace_id = ? AND owner_id = ? AND expires_at <= ?")
+ .bind(...scopeBindings(scope), now),
+ ]);
+};
+
+export const storeCompanionAttachment = async (db: DatabaseAdapter, scope: CompanionScope, input: AiAttachmentInput) => {
+ const now = new Date();
+ await purgeExpiredCompanionAttachments(db, scope, now.toISOString());
+ const byteLength = getBase64DecodedByteLength(input.base64Data);
+ if (byteLength === null) throw new AppError("invalid_params", "Attachment data must be valid base64.", 400);
+ const id = crypto.randomUUID();
+ const createdAt = now.toISOString();
+ const expiresAt = new Date(now.getTime() + ATTACHMENT_TTL_MS).toISOString();
+ const parts = chunkBase64(input.base64Data);
+ await db.batch([
+ db.prepare(`INSERT INTO companion_turn_attachments(
+ id, workspace_id, owner_id, turn_id, filename, media_type, byte_length, created_at, expires_at
+ ) VALUES (?, ?, ?, NULL, ?, ?, ?, ?, ?)`).bind(id, ...scopeBindings(scope), input.filename, input.mediaType, byteLength, createdAt, expiresAt),
+ ...parts.map((data, partIndex) => db.prepare(
+ "INSERT INTO companion_turn_attachment_parts(attachment_id, part_index, data) VALUES (?, ?, ?)",
+ ).bind(id, partIndex, data)),
+ ]);
+ return { attachment: { id, filename: input.filename, mediaType: input.mediaType, byteLength, expiresAt } };
+};
+
+const loadOwnedAttachments = async (db: DatabaseAdapter, scope: CompanionScope, ids: readonly string[]) => {
+ const rows = await db.prepare(`SELECT id, turn_id, filename, media_type, byte_length, expires_at
+ FROM companion_turn_attachments WHERE workspace_id = ? AND owner_id = ? AND id IN (${ids.map(() => "?").join(", ")})`)
+ .bind(...scopeBindings(scope), ...ids).all<AttachmentRow>();
+ return new Map(rows.results.map((row) => [row.id, row]));
+};
+
+export const assertCompanionAttachmentsSupported = async (
+ db: DatabaseAdapter, scope: CompanionScope, provider: AiProvider, ids: readonly string[] | undefined,
+) => {
+ if (!ids?.length) return;
+ const owned = await loadOwnedAttachments(db, scope, ids);
+ for (const id of ids) {
+ const mediaType = owned.get(id)?.media_type;
+ if (!mediaType || !companionSupportsAttachment(provider, mediaType)) {
+ throw new AppError("companion_attachment_unsupported", "This model cannot read that attachment type.", 400);
+ }
+ }
+};
+
+export const bindCompanionTurnAttachments = async (
+ db: DatabaseAdapter, scope: CompanionScope, turnId: string, ids: readonly string[] | undefined,
+) => {
+ if (!ids?.length) return;
+ if (new Set(ids).size !== ids.length) {
+ throw new AppError("companion_attachment_unavailable", "Attachment ids must be unique.", 400);
+ }
+ const now = new Date().toISOString();
+ const owned = await loadOwnedAttachments(db, scope, ids);
+ if (ids.some((id) => !owned.has(id))) {
+ throw new AppError("companion_attachment_unavailable", "An attachment is missing or belongs to another account.", 404);
+ }
+ for (const id of ids) {
+ const row = owned.get(id)!;
+ if (row.expires_at <= now) throw new AppError("companion_attachment_expired", "An attachment expired. Upload it again.", 409);
+ if (row.turn_id && row.turn_id !== turnId) {
+ throw new AppError("companion_attachment_unavailable", "An attachment is already bound to another turn.", 409);
+ }
+ }
+ const meta: CompanionTurnAttachmentMeta[] = ids.map((id) => {
+ const row = owned.get(id)!;
+ return { id: row.id, filename: row.filename, mediaType: row.media_type, byteLength: row.byte_length };
+ });
+ const result = await db.batch([
+ db.prepare(`UPDATE companion_turn_attachments SET turn_id = ?
+ WHERE workspace_id = ? AND owner_id = ? AND id IN (${ids.map(() => "?").join(", ")})
+ AND (turn_id IS NULL OR turn_id = ?) AND expires_at > ?`)
+ .bind(turnId, ...scopeBindings(scope), ...ids, turnId, now),
+ db.prepare("UPDATE companion_turns SET attachment_meta_json = ? WHERE workspace_id = ? AND owner_id = ? AND id = ?")
+ .bind(JSON.stringify(meta), ...scopeBindings(scope), turnId),
+ ]);
+ if (Number(result[0]?.meta.changes) !== ids.length || Number(result[1]?.meta.changes) !== 1) {
+ throw new AppError("companion_attachment_unavailable", "Attachments could not be bound to this turn.", 409);
+ }
+};
+
+const attachmentParts = (row: AttachmentRow, data: string): CompanionModelContentPart => {
+ if (isKnownMediaType(row.media_type) && isAiTextAttachment(row.media_type)) {
+ return { type: "text", text: `Attached file ${row.filename} (${row.media_type}):\n${decodeBase64Text(data)}` };
+ }
+ if (row.media_type.startsWith("image/")) return { type: "image", image: data, mediaType: row.media_type };
+ if (row.media_type === "application/pdf") return { type: "file", data, mediaType: row.media_type, filename: row.filename };
+ throw new AppError("companion_attachment_unsupported", "This model cannot read that attachment type.", 400);
+};
+
+export const loadCompanionAttachmentParts = async (
+ db: DatabaseAdapter, scope: CompanionScope, turnId: string, provider: AiProvider,
+): Promise<CompanionModelContentPart[]> => {
+ const now = new Date().toISOString();
+ const rows = await db.prepare(`SELECT id, turn_id, filename, media_type, byte_length, expires_at
+ FROM companion_turn_attachments WHERE workspace_id = ? AND owner_id = ? AND turn_id = ? ORDER BY created_at, id`)
+ .bind(...scopeBindings(scope), turnId).all<AttachmentRow>();
+ if (!rows.results.length) return [];
+ const parts: CompanionModelContentPart[] = [];
+ for (const row of rows.results) {
+ if (row.expires_at <= now) throw new AppError("companion_attachment_expired", "An attachment expired. Upload it again.", 409);
+ if (!companionSupportsAttachment(provider, row.media_type)) {
+ throw new AppError("companion_attachment_unsupported", "This model cannot read that attachment type.", 400);
+ }
+ const stored = await db.prepare(
+ "SELECT part_index, data FROM companion_turn_attachment_parts WHERE attachment_id = ? ORDER BY part_index",
+ ).bind(row.id).all<{ part_index: number; data: string }>();
+ const chunks = stored.results;
+ if (!chunks.length || chunks.some((chunk, index) => chunk.part_index !== index || !chunk.data)) {
+ throw new AppError("companion_attachment_expired", "An attachment expired. Upload it again.", 409);
+ }
+ parts.push(attachmentParts(row, chunks.map((chunk) => chunk.data).join("")));
+ }
+ return parts;
+};
diff --git a/apps/api/src/companion-prepare.ts b/apps/api/src/companion-prepare.ts
index cc02f5d0..c2310c60 100644
--- a/apps/api/src/companion-prepare.ts
+++ b/apps/api/src/companion-prepare.ts
@@ -1,14 +1,17 @@
import { selectCompanionMemories } from "./companion-memory-context";
export { selectCompanionMemories } from "./companion-memory-context";
import type {
- CompanionAnswer, CompanionMemory, CompanionPreparedTurn, CompanionQuestion, CompanionTodo, CompanionToolCall, CompanionTurnInput,
+ CompanionAnswer, CompanionMemory, CompanionModelContentPart, CompanionPreparedMessage, CompanionPreparedTurn,
+ CompanionQuestion, CompanionTodo, CompanionToolCall, CompanionTurnInput,
} from "@edgeever/shared";
+import { AppError } from "./app-error";
import type { DatabaseAdapter } from "./storage-contract";
+import { loadCompanionAttachmentParts } from "./companion-attachments";
import { listCompanionMemories, listCompanionTurns, mapCompanionTurn, type CompanionScope, type TurnRow } from "./companion-service";
import { companionToolDefinitions } from "./companion-agent-tools";
import { parseJsonArray } from "./companion-tool-receipts";
-export const COMPANION_IDENTITY_VERSION = 12;
+export const COMPANION_IDENTITY_VERSION = 13;
export const COMPANION_MAX_STEPS = 8;
export const COMPANION_MAX_OUTPUT_TOKENS = 2048;
export const COMPANION_INSTRUCTIONS = `You are EdgeEver, a thoughtful personal knowledge companion.
@@ -19,7 +22,7 @@ The user controls long-term memory through the UI. You cannot save, edit, or for
Only report a note operation as completed when the tool result says applied, or a persisted receipt says applied. A proposal is not completion.
Never claim a reminder was scheduled or an external action completed.
You can use EdgeEver's shared tools to read, create, update, import, merge, move, tag, trash and restore notes, restore revisions, organize notebooks, create editable diagrams, and manage note templates and AI instructions.
-All available write tools execute immediately. Trashed notes go to the recycle bin; content edits keep revision history. Read tools and explicit dry runs execute immediately.
+Title, tag, move, trash, restore, create, merge, and diagram tools execute immediately. An update_memo that includes contentMarkdown returns awaiting_user_confirmation and does not change the note until the user confirms. Never describe that proposal as applied. Trashed notes go to the recycle bin; content edits keep revision history. Read tools and explicit dry runs execute immediately.
Read every source note completely before merging or replacing its body. Do not merge merely because notes share a broad topic: look for one coherent idea or the user's explicit selection.
Merging preserves source bodies/attachments and existing tags, moves sources to trash and revokes their public shares. A destination notebook may be specified.
Content changes use update_memo with the exact replacement Markdown. Prefer existing tags; remove tags only when requested.
@@ -134,6 +137,24 @@ export const companionResumeMessages = (
return messages;
};
+export async function companionModelMessages(args: {
+ db: DatabaseAdapter; scope: CompanionScope; input: CompanionTurnInput; history: TurnRow[]; revision: number;
+ provider: CompanionPreparedTurn["provider"]; turnId: string;
+ resume?: { response?: string; answers?: CompanionAnswer[] };
+}): Promise<CompanionPreparedMessage[]> {
+ const messages: CompanionPreparedMessage[] = companionResumeMessages(args.input, args.history, args.revision, args.resume);
+ const parts = await loadCompanionAttachmentParts(args.db, args.scope, args.turnId, args.provider);
+ if (!parts.length) return messages;
+ const extra = !args.resume ? 0 : args.resume.response?.trim() ? 2 : 1;
+ const index = messages.length - 1 - extra;
+ const current = messages[index];
+ if (!current || current.role !== "user" || typeof current.content !== "string") {
+ throw new AppError("companion_attachment_expired", "Attachment context is unavailable.", 409);
+ }
+ const content: CompanionModelContentPart[] = [{ type: "text", text: current.content }, ...parts];
+ return messages.map((message, messageIndex) => messageIndex === index ? { role: "user", content } : message);
+}
+
export function companionAgentInstructions(
input: CompanionTurnInput, memories: CompanionMemory[], receipts: unknown[],
) {
@@ -190,7 +211,10 @@ export async function prepareCompanionTurn(args: {
const receipts = args.input.allowNotes
? await companionExecutionReceipts(args.db, args.scope, args.input, args.row.memory_revision, runTools)
: [];
- const messages = companionResumeMessages(args.input, history, args.row.memory_revision, args.resume);
+ const messages = await companionModelMessages({
+ db: args.db, scope: args.scope, input: args.input, history, revision: args.row.memory_revision,
+ resume: args.resume, provider: args.credentials.provider, turnId: args.row.id,
+ });
return {
turn: mapCompanionTurn(args.row),
provider: args.credentials.provider,
diff --git a/apps/api/src/companion-routes.ts b/apps/api/src/companion-routes.ts
index 1c30a74a..596a9459 100644
--- a/apps/api/src/companion-routes.ts
+++ b/apps/api/src/companion-routes.ts
@@ -1,6 +1,6 @@
-import { CompanionDiscoveryCompleteSchema, CompanionDiscoverySettingsInputSchema, CompanionIdSchema, CompanionMemoryImportSchema, CompanionMemoryInputSchema, CompanionMemoryUpdateSchema,
+import { AiAttachmentSchema, CompanionDiscoveryCompleteSchema, CompanionDiscoverySettingsInputSchema, CompanionIdSchema, CompanionMemoryImportSchema, CompanionMemoryInputSchema, CompanionMemoryUpdateSchema,
CompanionToolExecuteSchema, CompanionTurnCheckpointSchema, CompanionTurnCompleteSchema,
- CompanionTurnInputSchema, CompanionTurnResumeSchema, sealCompanionProcess, type CompanionEvent, type CompanionSource, type CompanionToolCall, type CompanionTurnInput } from "@edgeever/shared";
+ CompanionTurnInputSchema, CompanionTurnResumeSchema, sealCompanionProcess, type AiProvider, type CompanionEvent, type CompanionSource, type CompanionToolCall, type CompanionTurnInput } from "@edgeever/shared";
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { bodyLimit } from "hono/body-limit";
@@ -15,6 +15,7 @@ import { beginCompanionTurn, checkpointCompanionTurn, clearCompanionHistory, com
import { parseJsonArray, parseJsonObject } from "./companion-tool-receipts";
import type { streamCompanion } from "./companion-runtime";
import { prepareCompanionTurn, type CompanionRunState } from "./companion-prepare";
+import { assertCompanionAttachmentsSupported, bindCompanionTurnAttachments, storeCompanionAttachment } from "./companion-attachments";
import { executeCompanionTurnTool, parseCompanionAgentSession } from "./companion-agent-tools";
import { applyCompanionAction, dismissCompanionAction, listCompanionActions } from "./companion-actions";
import { acknowledgeDiscovery, rememberDiscoveryFeedback, checkDiscoveries, completeDiscoveryGeneration, discoveryPreparePayload, getDiscoverySettings, listDiscoveries, saveDiscoverySettings, startDiscoveryCheck } from "./companion-discovery";
@@ -43,10 +44,11 @@ export const companionGenerationFailure = (error: unknown): { code: string } =>
const streamCompanionTurn = (
c: AppContext,
- dependencies: { stream?: typeof streamCompanion },
+ dependencies: { stream?: typeof streamCompanion; loadCredentials?: typeof loadDefaultAiModelCredentials },
args: {
db: AppContext["env"]["storage"]["db"]; scope: CompanionScope; row: TurnRow; input: CompanionTurnInput;
- model: Awaited<ReturnType<typeof loadDefaultAiModel>>; resume?: { response?: string; answers?: import("@edgeever/shared").CompanionAnswer[] };
+ model: Awaited<ReturnType<typeof loadDefaultAiModel>>; provider?: AiProvider;
+ resume?: { response?: string; answers?: import("@edgeever/shared").CompanionAnswer[] };
},
) => {
const { db, scope, row, input, model } = args;
@@ -98,9 +100,13 @@ const streamCompanionTurn = (
const [memories, history] = await Promise.all([listCompanionMemories(db, scope), listCompanionTurns(db, scope, input.threadId)]);
const stream = dependencies.stream ?? (await import("./companion-runtime")).streamCompanion;
await assertActive();
+ let provider = args.provider;
+ if (!provider && parseJsonArray(row.attachment_meta_json).length) {
+ provider = (await (dependencies.loadCredentials ?? loadDefaultAiModelCredentials)(db, scope.workspaceId, c.env)).provider;
+ }
const result = await stream({
db, scope, input, model, memories, history, revision: row.memory_revision, signal, sources, assertActive, context: c, run,
- resume: args.resume,
+ resume: args.resume, provider,
});
for await (const part of result.fullStream) {
signal.throwIfAborted();
@@ -155,7 +161,12 @@ export const registerCompanionRoutes = (parent: Hono<AppEnv>, dependencies: {
}) => {
const app = new Hono<AppEnv>();
app.onError((error, c) => fail(c, error));
- app.use("/api/v1/companion/*", bodyLimit({ maxSize: 128 * 1024 }));
+ // Upload bodies exceed the JSON limit used by the rest of the companion API.
+ const limitCompanionBody = bodyLimit({ maxSize: 128 * 1024 });
+ app.use("/api/v1/companion/*", (c, next) => {
+ if (c.req.path.endsWith("/companion/attachments") && c.req.method === "POST") return next();
+ return limitCompanionBody(c, next);
+ });
app.use("/api/v1/companion/*", async (c, next) => {
const denied = requireUser(c);
if (denied) return denied;
@@ -430,12 +441,33 @@ export const registerCompanionRoutes = (parent: Hono<AppEnv>, dependencies: {
return c.json({ memories: await importCompanionMemories(c.env.storage.db, scope, c.req.valid("json").memories, c.req.valid("json").controls) });
});
+ const cancelTurn = async (db: AppContext["env"]["storage"]["db"], scope: CompanionScope, id: string) => {
+ await db.prepare("UPDATE companion_turns SET status = 'cancelled' WHERE id = ? AND workspace_id = ? AND owner_id = ?")
+ .bind(id, scope.workspaceId, scope.ownerId).run();
+ };
+ const bindTurnAttachments = async (db: AppContext["env"]["storage"]["db"], scope: CompanionScope, turnId: string, ids?: string[]) => {
+ try {
+ await bindCompanionTurnAttachments(db, scope, turnId, ids);
+ } catch (error) {
+ await cancelTurn(db, scope, turnId);
+ throw error;
+ }
+ };
+ app.post("/api/v1/companion/attachments", bodyLimit({ maxSize: 7 * 1024 * 1024 }), zValidator("json", AiAttachmentSchema), async c => {
+ return c.json(await storeCompanionAttachment(c.env.storage.db, scopeFor(c), c.req.valid("json")));
+ });
app.post("/api/v1/companion/turns", zValidator("json", CompanionTurnInputSchema), async c => {
const input = c.req.valid("json");
const db = c.env.storage.db;
const scope = scopeFor(c);
const duplicate = await getCompanionTurn(db, scope, input.id);
if (duplicate) return apiError(c, "companion_request_exists", "This request already exists. Recover it by its ID; it will not be billed again.", 409);
+ let provider: AiProvider | undefined;
+ if (input.attachmentIds?.length) {
+ const credentials = await (dependencies.loadCredentials ?? loadDefaultAiModelCredentials)(db, scope.workspaceId, c.env);
+ provider = credentials.provider;
+ await assertCompanionAttachmentsSupported(db, scope, credentials.provider, input.attachmentIds);
+ }
const model = await (dependencies.loadModel ?? loadDefaultAiModel)(db, scope.workspaceId, c.env);
await listCompanionMemories(db, scope);
const expectedRevision = await companionRevision(db, scope);
@@ -443,10 +475,11 @@ export const registerCompanionRoutes = (parent: Hono<AppEnv>, dependencies: {
input.useMemory = input.useMemory && settings.useMemory === true;
const row = await beginCompanionTurn(db, scope, input, model.modelId);
if (row.memory_revision !== expectedRevision) {
- await db.prepare("UPDATE companion_turns SET status = 'cancelled' WHERE id = ? AND workspace_id = ? AND owner_id = ?").bind(row.id, scope.workspaceId, scope.ownerId).run();
+ await cancelTurn(db, scope, row.id);
throw new AppError("companion_context_changed", "Memory settings changed. Retry with current settings.", 409);
}
- return streamCompanionTurn(c, dependencies, { db, scope, row, input, model });
+ await bindTurnAttachments(db, scope, row.id, input.attachmentIds);
+ return streamCompanionTurn(c, dependencies, { db, scope, row, input, model, provider });
});
app.post("/api/v1/companion/turns/prepare", zValidator("json", CompanionTurnInputSchema), async c => {
const input = c.req.valid("json");
@@ -455,15 +488,19 @@ export const registerCompanionRoutes = (parent: Hono<AppEnv>, dependencies: {
const duplicate = await getCompanionTurn(db, scope, input.id);
if (duplicate) return apiError(c, "companion_request_exists", "This request already exists. Recover it by its ID; it will not be billed again.", 409);
const credentials = await (dependencies.loadCredentials ?? loadDefaultAiModelCredentials)(db, scope.workspaceId, c.env);
+ if (input.attachmentIds?.length) {
+ await assertCompanionAttachmentsSupported(db, scope, credentials.provider, input.attachmentIds);
+ }
await listCompanionMemories(db, scope);
const expectedRevision = await companionRevision(db, scope);
const settings = await getDiscoverySettings(db, scope);
input.useMemory = input.useMemory && settings.useMemory === true;
const row = await beginCompanionTurn(db, scope, input, credentials.modelId);
if (row.memory_revision !== expectedRevision) {
- await db.prepare("UPDATE companion_turns SET status = 'cancelled' WHERE id = ? AND workspace_id = ? AND owner_id = ?").bind(row.id, scope.workspaceId, scope.ownerId).run();
+ await cancelTurn(db, scope, row.id);
throw new AppError("companion_context_changed", "Memory settings changed. Retry with current settings.", 409);
}
+ await bindTurnAttachments(db, scope, row.id, input.attachmentIds);
return c.json(await prepareCompanionTurn({ db, scope, input, row, credentials }));
});
parent.route("/", app);
diff --git a/apps/api/src/companion-runtime.ts b/apps/api/src/companion-runtime.ts
index ed5e1a77..400cb1cb 100644
--- a/apps/api/src/companion-runtime.ts
+++ b/apps/api/src/companion-runtime.ts
@@ -1,6 +1,7 @@
-import { isStepCount, jsonSchema, tool, ToolLoopAgent, type LanguageModel } from "ai";
+import { isStepCount, jsonSchema, tool, ToolLoopAgent, type LanguageModel, type ModelMessage } from "ai";
import type {
- CompanionAnswer, CompanionMemory, CompanionSource, CompanionTurnInput,
+ AiProvider, CompanionAnswer, CompanionMemory, CompanionModelContentPart, CompanionPreparedMessage,
+ CompanionSource, CompanionTurnInput,
} from "@edgeever/shared";
import type { DatabaseAdapter } from "./storage-contract";
import type { CompanionScope, TurnRow } from "./companion-service";
@@ -9,7 +10,7 @@ import { companionToolDefinitions, createCompanionTools } from "./companion-agen
import {
companionAgentInstructions,
companionExecutionReceipts,
- companionResumeMessages,
+ companionModelMessages,
COMPANION_MAX_OUTPUT_TOKENS,
COMPANION_MAX_STEPS,
type CompanionRunState,
@@ -31,10 +32,26 @@ export {
type CompanionRunState,
} from "./companion-prepare";
+const toModelMessages = (messages: CompanionPreparedMessage[]): ModelMessage[] => messages.map((message): ModelMessage => {
+ if (message.role === "assistant") {
+ if (typeof message.content !== "string") throw new Error("Assistant content must be a string.");
+ return { role: "assistant", content: message.content };
+ }
+ if (typeof message.content === "string") return { role: "user", content: message.content };
+ return {
+ role: "user",
+ content: message.content.map((part: CompanionModelContentPart) => {
+ if (part.type === "text") return { type: "text" as const, text: part.text };
+ if (part.type === "image") return { type: "image" as const, image: part.image, mediaType: part.mediaType };
+ return { type: "file" as const, data: part.data, mediaType: part.mediaType, ...(part.filename ? { filename: part.filename } : {}) };
+ }),
+ };
+});
+
export const streamCompanion = async (args: {
db: DatabaseAdapter; scope: CompanionScope; input: CompanionTurnInput; model: LanguageModel;
memories: CompanionMemory[]; history: TurnRow[]; revision: number; signal: AbortSignal;
- sources: CompanionSource[]; assertActive: () => Promise<void>;
+ sources: CompanionSource[]; assertActive: () => Promise<void>; provider?: AiProvider;
context?: AppContext; run?: CompanionRunState; resume?: { response?: string; answers?: CompanionAnswer[] };
}) => {
const run = args.run ?? { tools: [], todos: [], questions: [], pause: { ask: false } };
@@ -64,5 +81,9 @@ export const streamCompanion = async (args: {
maxOutputTokens: COMPANION_MAX_OUTPUT_TOKENS,
maxRetries: 0,
});
- return agent.stream({ messages: companionResumeMessages(args.input, args.history, args.revision, args.resume), abortSignal: args.signal });
+ const messages = await companionModelMessages({
+ db: args.db, scope: args.scope, input: args.input, history: args.history, revision: args.revision,
+ resume: args.resume, provider: args.provider ?? "openai-compatible", turnId: args.input.id,
+ });
+ return agent.stream({ messages: toModelMessages(messages), abortSignal: args.signal });
};
diff --git a/apps/api/src/companion-service.ts b/apps/api/src/companion-service.ts
index a0e8be18..f33b7181 100644
--- a/apps/api/src/companion-service.ts
+++ b/apps/api/src/companion-service.ts
@@ -1,6 +1,6 @@
import type {
CompanionAnswer, CompanionMention, CompanionMemory, CompanionQuestion, CompanionSource, CompanionTodo,
- CompanionToolCall, CompanionTurn, CompanionTurnInput,
+ CompanionToolCall, CompanionTurn, CompanionTurnAttachmentMeta, CompanionTurnInput,
} from "@edgeever/shared";
import { refreshCompanionPreferences } from "./companion-learning";
import { AppError } from "./app-error";
@@ -15,6 +15,7 @@ export type TurnRow = {
created_at: string; memory_revision: number; use_memory: number; allow_notes: number; locale: string;
tools_json?: string; todos_json?: string; questions_json?: string; mentions_json?: string;
focus_json?: string; answers_json?: string; process_text?: string; agent_session_json?: string;
+ attachment_meta_json?: string;
};
export const COMPANION_TURN_LEASE_MS = 180_000;
export const companionTurnLease = () => new Date(Date.now() + COMPANION_TURN_LEASE_MS).toISOString();
@@ -25,13 +26,17 @@ export const turnInputFromRow = (row: TurnRow): CompanionTurnInput => ({
focus: Object.keys(turnFocus(row)).length ? turnFocus(row) : undefined,
});
const bindScope = (scope: CompanionScope) => [scope.workspaceId, scope.ownerId];
-export const mapCompanionTurn = (row: TurnRow): CompanionTurn => ({
- id: row.id, threadId: row.thread_id, message: row.message, response: row.response, process: row.process_text ?? "",
- status: row.status, sources: parseJsonArray<CompanionSource>(row.sources_json),
- tools: parseJsonArray<CompanionToolCall>(row.tools_json), todos: parseJsonArray<CompanionTodo>(row.todos_json),
- questions: parseJsonArray<CompanionQuestion>(row.questions_json), mentions: parseJsonArray<CompanionMention>(row.mentions_json),
- model: row.model, inputTokens: row.input_tokens, outputTokens: row.output_tokens, createdAt: row.created_at,
-});
+export const mapCompanionTurn = (row: TurnRow): CompanionTurn => {
+ const attachments = parseJsonArray<CompanionTurnAttachmentMeta>(row.attachment_meta_json);
+ return {
+ id: row.id, threadId: row.thread_id, message: row.message, response: row.response, process: row.process_text ?? "",
+ status: row.status, sources: parseJsonArray<CompanionSource>(row.sources_json),
+ tools: parseJsonArray<CompanionToolCall>(row.tools_json), todos: parseJsonArray<CompanionTodo>(row.todos_json),
+ questions: parseJsonArray<CompanionQuestion>(row.questions_json), mentions: parseJsonArray<CompanionMention>(row.mentions_json),
+ ...(attachments.length ? { attachments } : {}),
+ model: row.model, inputTokens: row.input_tokens, outputTokens: row.output_tokens, createdAt: row.created_at,
+ };
+};
export const turnFocus = (row: TurnRow) => parseJsonObject<NonNullable<CompanionTurnInput["focus"]>>(row.focus_json, {});
export const turnAnswers = (row: TurnRow) => parseJsonArray<CompanionAnswer>(row.answers_json);
export const ensureCompanionState = async (db: DatabaseAdapter, scope: CompanionScope) => {
diff --git a/apps/api/src/companion-tool-actions.test.mjs b/apps/api/src/companion-tool-actions.test.mjs
index 599dc36d..f41026b8 100644
--- a/apps/api/src/companion-tool-actions.test.mjs
+++ b/apps/api/src/companion-tool-actions.test.mjs
@@ -146,17 +146,33 @@ describe("shared companion MCP adapter", () => {
test("create_memo, update_memo and trash_memos execute immediately", async () => {
const f = await setup();
const tools = createCompanionTools({ ...f, scope, signal: new AbortController().signal, assertActive: async () => {}, sources: [] });
+ await expect(tools.update_memo.execute({ memoId: f.notes[0].id, contentMarkdown: "unread" })).rejects.toMatchObject({ code: "companion_action_unread" });
const created = await tools.create_memo.execute({ notebookId: "ideas", title: "New", contentMarkdown: "Exact new body" });
expect(created).toMatchObject({ applied: true });
const createdId = created.memo?.id ?? created.id;
expect(createdId).toBeTruthy();
expect(await getMemoDetail(f.db, scope.workspaceId, createdId)).toMatchObject({ title: "New", contentMarkdown: "Exact new body" });
+ expect(await tools.trash_memos.execute({ memoIds: [createdId] })).toMatchObject({ applied: true });
+ expect((await getMemoDetail(f.db, scope.workspaceId, createdId, true)).isDeleted).toBe(true);
+ await tools.get_memo.execute({ memoId: f.notes[1].id });
+ expect(await tools.update_memo.execute({ memoId: f.notes[1].id, title: "Title only" })).toMatchObject({ applied: true });
+ expect(await getMemoDetail(f.db, scope.workspaceId, f.notes[1].id)).toMatchObject({ title: "Title only", contentMarkdown: "Two original content" });
await tools.get_memo.execute({ memoId: f.notes[0].id });
- expect(await tools.update_memo.execute({ memoId: f.notes[0].id, title: "Changed", contentMarkdown: "Exact replacement" })).toMatchObject({ applied: true });
+ const proposed = await tools.update_memo.execute({ memoId: f.notes[0].id, title: "Changed", contentMarkdown: "Exact replacement" });
+ const proposalId = proposed.proposalId;
+ expect(proposed).toMatchObject({ status: "awaiting_user_confirmation", proposalId: expect.any(String) });
+ expect(await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id)).toMatchObject({ title: "One", contentMarkdown: "One original content" });
+ expect((await getCompanionAction(f.db, scope, proposalId))?.preview?.baseContentMarkdown).toBe("One original content");
+ const fresh = await getMemoDetail(f.db, scope.workspaceId, f.notes[1].id);
+ const conflictId = await f.propose("update_memo", { memoId: f.notes[1].id, contentMarkdown: "Should not land" }, new Map([[f.notes[1].id, fresh.revision]]));
+ f.sqlite.query("UPDATE memo_contents SET revision = revision + 1 WHERE memo_id = ?").run(f.notes[1].id);
+ await f.complete();
+ expect((await f.request(`actions/${proposalId}/apply`)).status).toBe(200);
expect(await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id)).toMatchObject({ title: "Changed", contentMarkdown: "Exact replacement" });
- expect(await tools.trash_memos.execute({ memoIds: [f.notes[0].id] })).toMatchObject({ applied: true });
- expect((await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id, true)).isDeleted).toBe(true);
- expect(f.sqlite.query("SELECT COUNT(*) AS n FROM companion_actions").get().n).toBe(0);
+ const conflict = await f.request(`actions/${conflictId}/apply`);
+ expect(conflict.status).toBe(409);
+ expect(await conflict.json()).toMatchObject({ error: { code: "companion_action_conflict" } });
+ expect(await getMemoDetail(f.db, scope.workspaceId, f.notes[1].id)).toMatchObject({ title: "Title only", contentMarkdown: "Two original content" });
});
test("move, tag, and notebook writes execute immediately without a confirmation card", async () => {
const f = await setup();
@@ -199,6 +215,10 @@ describe("shared companion MCP adapter", () => {
});
expect(JSON.stringify(diagramMemo)).not.toContain("edgeever-diagram-v1");
expect(diagramMemo.content).toBeUndefined();
+ await expect(tools.update_memo.execute({ memoId: created.id, contentMarkdown: "# no" })).rejects.toThrow(
+ "This is an editable diagram. Change it with update_diagram, not update_memo.",
+ );
+ expect(f.sqlite.query("SELECT COUNT(*) AS n FROM companion_actions").get().n).toBe(0);
expect(() => validateCompanionTool("update_diagram", {
memoId: created.id, expectedRevision: 0,
operations: [{ op: "add_node", node: { id: "eval", label: "评估", parentId: "root" } }],
diff --git a/apps/api/src/companion-tool-actions.ts b/apps/api/src/companion-tool-actions.ts
index 46a01fb9..862223a5 100644
--- a/apps/api/src/companion-tool-actions.ts
+++ b/apps/api/src/companion-tool-actions.ts
@@ -16,7 +16,8 @@ export const companionWorkspaceCursor = async (db: DatabaseAdapter, workspaceId:
const stale = () => new AppError("companion_action_conflict", "Notes changed or the suggestion is no longer eligible. Request a fresh suggestion.", 409);
export async function proposeCompanionToolAction(db: DatabaseAdapter, scope: CompanionScope, turnId: string,
- toolName: string, input: Record<string, unknown>, reason: string, cursor: number, inspected: ReadonlyMap<string, number>, evidenceIds: string[] = []) {
+ toolName: string, input: Record<string, unknown>, reason: string, cursor: number, inspected: ReadonlyMap<string, number>, evidenceIds: string[] = [],
+ previewExtras?: { baseContentMarkdown?: string }) {
const { definition, args } = validateCompanionTool(toolName, input);
if (definition.annotations.readOnlyHint || args.dryRun === true) throw new AppError("invalid_params", "This call does not need approval.", 400);
const plan = CompanionToolPlanSchema.parse({ kind: "tool", toolName, arguments: args, reason });
@@ -46,13 +47,18 @@ export async function proposeCompanionToolAction(db: DatabaseAdapter, scope: Com
if (toolName === "update_memo") plan.arguments.expectedRevision = notes[0].revision;
const id = crypto.randomUUID();
const now = new Date().toISOString();
+ const preview = {
+ notebooks,
+ affectedCount: tagPreview?.updated ?? notes.length,
+ ...(previewExtras?.baseContentMarkdown !== undefined ? { baseContentMarkdown: previewExtras.baseContentMarkdown } : {}),
+ };
const result = await db.prepare(`INSERT INTO companion_actions(id, turn_id, workspace_id, owner_id, payload_json, created_at, expires_at, workspace_cursor)
SELECT ?, t.id, t.workspace_id, t.owner_id, ?, ?, ?, ? FROM companion_turns t
JOIN companion_state s ON s.workspace_id = t.workspace_id AND s.owner_id = t.owner_id
WHERE t.id = ? AND t.workspace_id = ? AND t.owner_id = ? AND t.status = 'running' AND t.allow_notes = 1
AND t.memory_revision = s.memory_revision AND t.expires_at > ?
AND (${workspaceCursorSql}) = ? AND (SELECT COUNT(*) FROM companion_actions WHERE turn_id = t.id) < 3`)
- .bind(id, JSON.stringify({ plan, notes, preview: { notebooks, affectedCount: tagPreview?.updated ?? notes.length } }), now, new Date(Date.now() + 86400000).toISOString(), cursor,
+ .bind(id, JSON.stringify({ plan, notes, preview }), now, new Date(Date.now() + 86400000).toISOString(), cursor,
turnId, scope.workspaceId, scope.ownerId, now, scope.workspaceId, cursor).run();
if (Number(result.meta.changes) !== 1) throw stale();
return { proposalId: id, status: "awaiting_user_confirmation", message: "Nothing was changed. Review and confirm the exact operation in its card." };
diff --git a/apps/api/src/companion-tool-receipts.ts b/apps/api/src/companion-tool-receipts.ts
index f30b8625..165fda32 100644
--- a/apps/api/src/companion-tool-receipts.ts
+++ b/apps/api/src/companion-tool-receipts.ts
@@ -26,6 +26,7 @@ export function describeCompanionTool(
previous?: Map<string, { revision: number; title?: string }>,
): CompanionToolEffect[] {
const record = asRecord(result);
+ if (record?.status === "awaiting_user_confirmation") return [];
const memo = asMemo(record?.memo) ?? asMemo(result);
const prior = (id: string) => previous?.get(id);
if (name === "search_memos" || name === "list_memos") {
diff --git a/apps/api/src/companion.test.mjs b/apps/api/src/companion.test.mjs
index 20202837..84a72b4c 100644
--- a/apps/api/src/companion.test.mjs
+++ b/apps/api/src/companion.test.mjs
@@ -527,7 +527,9 @@ describe("companion client-direct HTTP contracts", () => {
describe("actual AI SDK companion runtime", () => {
test("proposal reasons contain evidence instead of card boilerplate", () => {
- expect(COMPANION_INSTRUCTIONS).toContain("All available write tools execute immediately");
+ expect(COMPANION_INSTRUCTIONS).toContain("awaiting_user_confirmation");
+ expect(COMPANION_INSTRUCTIONS).toContain("Never describe that proposal as applied");
+ expect(COMPANION_INSTRUCTIONS).not.toContain("All available write tools execute immediately");
expect(COMPANION_INSTRUCTIONS).not.toContain("user must confirm the suggestion card");
expect(COMPANION_INSTRUCTIONS).toContain("[Note title](#memo=memo_abc123)");
expect(COMPANION_INSTRUCTIONS).toContain("Do not drop memo_");
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index e30ee1a4..1aebd900 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -18,8 +18,10 @@
"electron-builder": "^26.0.12"
},
"dependencies": {
+ "@agentclientprotocol/sdk": "^1.5.1",
"@edgeever/public-network": "workspace:*",
"croner": "^10.0.1",
- "electron-updater": "^6.6.2"
+ "electron-updater": "^6.6.2",
+ "zod": "^4.6.5"
}
}
diff --git a/apps/desktop/src/main/acp-host.mjs b/apps/desktop/src/main/acp-host.mjs
new file mode 100644
index 00000000..1238ff84
--- /dev/null
+++ b/apps/desktop/src/main/acp-host.mjs
@@ -0,0 +1,629 @@
+import { spawn as nodeSpawn } from "node:child_process";
+import { randomUUID } from "node:crypto";
+import { accessSync, constants as fsConstants, readFileSync, realpathSync, statSync } from "node:fs";
+import { mkdtemp, rm } from "node:fs/promises";
+import { homedir, tmpdir } from "node:os";
+import path from "node:path";
+import { Readable, Writable } from "node:stream";
+import { ClientSideConnection, ndJsonStream, PROTOCOL_VERSION, RequestError } from "@agentclientprotocol/sdk";
+import { waitForChildProcessSpawn } from "./child-process-start.mjs";
+
+const ADAPTERS = {
+ codex: { id: "codex", label: "Codex" },
+ antigravity: { id: "antigravity", label: "Antigravity" },
+};
+const AUTH_REQUIRED_CODE = -32000;
+const MAX_ATTACHMENTS = 8;
+const MAX_ATTACHMENT_CHARS = 8_000_000;
+const HANDSHAKE_TIMEOUT_MS = 20_000;
+const WORKSPACE_MARKER = `${path.sep}edgeever-acp-`;
+const IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/webp", "image/gif"]);
+
+const liveChildren = new Set();
+let exitHooked = false;
+
+const clientVersion = () => {
+ try {
+ const version = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
+ return typeof version === "string" && version.trim() ? version.trim() : "0.0.0";
+ } catch {
+ return "0.0.0";
+ }
+};
+
+const trackChild = (child) => {
+ if (!child) return;
+ liveChildren.add(child);
+ child.once?.("exit", () => liveChildren.delete(child));
+ if (!exitHooked) {
+ exitHooked = true;
+ process.once("exit", () => {
+ for (const live of liveChildren) {
+ try { live.kill("SIGKILL"); } catch { /* already gone */ }
+ }
+ });
+ }
+};
+
+export const codexBasenames = (platform = process.platform) => (
+ platform === "win32" ? ["codex-acp", "codex-acp.exe"] : ["codex-acp"]
+);
+
+const slashRejected = (value) => value.includes("/") || value.includes("\\") || value.includes("\0");
+
+const findOnPath = (names, { access, pathEnv, delimiter, sep }) => {
+ for (const dir of String(pathEnv ?? "").split(delimiter)) {
+ if (!dir) continue;
+ for (const name of names) {
+ if (slashRejected(name)) continue;
+ const candidate = dir.endsWith("/") || dir.endsWith("\\") ? `${dir}${name}` : `${dir}${sep}${name}`;
+ try {
+ access(candidate, fsConstants.X_OK);
+ return candidate;
+ } catch {
+ // Try the next PATH entry.
+ }
+ }
+ }
+ return null;
+};
+
+const resolutionDeps = (deps = {}) => ({
+ platform: deps.platform ?? process.platform,
+ access: deps.accessSync ?? accessSync,
+ realpath: deps.realpathSync ?? realpathSync,
+ stat: deps.statSync ?? statSync,
+ pathEnv: deps.pathEnv ?? process.env.PATH ?? "",
+ delimiter: deps.delimiter ?? path.delimiter,
+ sep: deps.sep ?? path.sep,
+});
+
+export function resolveAcpCommand(input, deps = {}) {
+ const resolved = resolutionDeps(deps);
+ const id = input?.id;
+ if (id !== "codex" && id !== "antigravity") return { ok: false, state: "failed", detail: "unknown_adapter" };
+ if (id === "codex") return resolveCodex(input?.path, resolved);
+ return resolveAntigravity(input?.path, resolved);
+}
+
+function resolveCodex(configured, deps) {
+ const allowed = codexBasenames(deps.platform);
+ let names = allowed;
+ if (typeof configured === "string" && configured.trim()) {
+ const name = configured.trim();
+ if (slashRejected(name) || !allowed.includes(name)) return { ok: false, state: "failed", detail: "invalid_path" };
+ names = [name];
+ }
+ const command = findOnPath(names, deps);
+ return command ? { ok: true, command } : { ok: false, state: "not_installed" };
+}
+
+function resolveAntigravity(configured, { realpath, stat }) {
+ if (typeof configured !== "string" || !configured.trim()) return { ok: false, state: "not_installed" };
+ const raw = configured.trim();
+ if (raw.includes("\0") || !path.isAbsolute(raw) || raw.split(/[/\\]/).includes("..")) {
+ return { ok: false, state: "failed", detail: "invalid_path" };
+ }
+ let resolvedPath;
+ try {
+ resolvedPath = realpath(raw);
+ } catch (error) {
+ return error?.code === "ENOENT"
+ ? { ok: false, state: "not_installed" }
+ : { ok: false, state: "failed", detail: "invalid_path" };
+ }
+ let info;
+ try {
+ info = stat(resolvedPath);
+ } catch (error) {
+ return error?.code === "ENOENT"
+ ? { ok: false, state: "not_installed" }
+ : { ok: false, state: "failed", detail: "invalid_path" };
+ }
+ if (typeof info?.isFile !== "function" || !info.isFile()) return { ok: false, state: "failed", detail: "invalid_path" };
+ return { ok: true, command: resolvedPath };
+}
+
+export function isAuthRequiredError(error) {
+ if (!error) return false;
+ const message = error instanceof Error ? error.message : String(error?.message ?? error);
+ if (/auth/i.test(message)) return true;
+ return error instanceof RequestError && error.code === AUTH_REQUIRED_CODE;
+}
+
+export function sanitizeFailureDetail(error) {
+ const message = error instanceof Error ? error.message : String(error?.message ?? error ?? "");
+ let text = message.replace(/\s+/g, " ").trim();
+ const home = homedir();
+ if (home && home !== "/" && home !== "\\") text = text.split(home).join("[path]");
+ text = text.replace(/~\/[^\s]+/g, "[path]");
+ text = text.replace(/(?:\/[\w.+@-]+){2,}/g, "[path]");
+ text = text.replace(/[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*/g, "[path]");
+ text = text.replace(/\b(?:[A-Za-z0-9_]*(?:key|token|secret|password|credential)[A-Za-z0-9_]*)=[^\s]+/gi, "[redacted]");
+ text = text.replace(/\b(?:bearer|authorization)\s+[^\s]+/gi, "[redacted]");
+ text = text.replace(/\s+/g, " ").trim();
+ if (!text || text === "[path]" || text === "[redacted]") return "connection_failed";
+ return text.slice(0, 160);
+}
+
+export function classifyAcpFailure(error) {
+ if (error?.code === "ENOENT" || error?.code === "ENOTDIR") return { state: "not_installed" };
+ if (error?.code === "TIMEOUT" || error?.message === "connection_timeout") {
+ return { state: "failed", detail: "connection_timeout" };
+ }
+ if (isAuthRequiredError(error)) return { state: "needs_login" };
+ return { state: "failed", detail: sanitizeFailureDetail(error) };
+}
+
+const adapterShell = (id) => ({ ...ADAPTERS[id] });
+
+const adapterFromResolution = (id, resolution) => {
+ const adapter = adapterShell(id);
+ if (!resolution.ok && resolution.state === "failed") {
+ return { ...adapter, state: "failed", detail: resolution.detail || "connection_failed" };
+ }
+ if (!resolution.ok) return { ...adapter, state: "not_installed" };
+ return adapter;
+};
+
+const mediaTypeBase = (value) => (
+ typeof value === "string" ? value.split(";")[0].trim().toLowerCase() : ""
+);
+
+const attachmentToken = (filename) => {
+ const base = String(filename ?? "").split(/[/\\]/).filter(Boolean).pop() ?? "";
+ const cleaned = base.replace(/[^A-Za-z0-9._-]+/g, "_").replace(/^\.+/, "").slice(0, 80);
+ return cleaned || "attachment";
+};
+
+const decodeText = (dataBase64) => Buffer.from(dataBase64, "base64").toString("utf8");
+
+const rejectAttachment = (attachment, reason) => ({
+ filename: typeof attachment?.filename === "string" && attachment.filename ? attachment.filename : "attachment",
+ reason,
+});
+
+export function buildPromptContent({ prompt, contextText, attachments, promptCapabilities = {} } = {}) {
+ const blocks = [];
+ const rejectedAttachments = [];
+ if (typeof contextText === "string" && contextText.length > 0) blocks.push({ type: "text", text: contextText });
+ blocks.push({ type: "text", text: typeof prompt === "string" ? prompt : "" });
+ const list = Array.isArray(attachments) ? attachments : [];
+ list.forEach((attachment, index) => {
+ if (index >= MAX_ATTACHMENTS) {
+ rejectedAttachments.push(rejectAttachment(attachment, "Too many attachments."));
+ return;
+ }
+ const mapped = mapAttachment(attachment, promptCapabilities);
+ if (mapped.block) blocks.push(mapped.block);
+ if (mapped.rejected) rejectedAttachments.push(mapped.rejected);
+ });
+ assertPromptBlocksSafe(blocks);
+ return { blocks, rejectedAttachments };
+}
+
+function mapAttachment(attachment, capabilities) {
+ if (!attachment || typeof attachment !== "object") {
+ return { rejected: rejectAttachment(attachment, "This attachment could not be read.") };
+ }
+ const dataBase64 = attachment.dataBase64;
+ if (typeof dataBase64 !== "string" || dataBase64.length > MAX_ATTACHMENT_CHARS) {
+ return { rejected: rejectAttachment(attachment, "This attachment could not be read.") };
+ }
+ const mediaType = mediaTypeBase(attachment.mediaType);
+ if (mediaType.startsWith("text/") || mediaType === "application/json") {
+ return { block: { type: "text", text: decodeText(dataBase64) } };
+ }
+ if (IMAGE_TYPES.has(mediaType)) {
+ if (capabilities.image !== true) {
+ return { rejected: rejectAttachment(attachment, "This agent does not accept images.") };
+ }
+ return { block: { type: "image", data: dataBase64, mimeType: mediaType } };
+ }
+ if (mediaType === "application/pdf") {
+ if (capabilities.embeddedContext !== true) {
+ return { rejected: rejectAttachment(attachment, "This agent does not accept embedded files.") };
+ }
+ return {
+ block: {
+ type: "resource",
+ resource: {
+ uri: `edgeever-attachment:${attachmentToken(attachment.filename)}`,
+ blob: dataBase64,
+ mimeType: "application/pdf",
+ },
+ },
+ };
+ }
+ return { rejected: rejectAttachment(attachment, "This file type is not supported.") };
+}
+
+function assertPromptBlocksSafe(blocks) {
+ for (const block of blocks) {
+ if (!block || block.type === "resource_link") throw new Error("invalid_prompt");
+ if (block.type === "resource") {
+ const uri = block.resource?.uri ?? "";
+ if (!uri.startsWith("edgeever-attachment:") || /[/\\]/.test(uri) || uri.includes("..")) {
+ throw new Error("invalid_prompt");
+ }
+ }
+ if (block.type === "image" && typeof block.uri === "string") throw new Error("invalid_prompt");
+ }
+}
+
+const safeToolToken = (value) => {
+ if (typeof value !== "string") return "";
+ const trimmed = value.trim();
+ if (!trimmed || trimmed.length > 120 || /[/\\]/.test(trimmed) || trimmed.includes("://")) return "";
+ return trimmed;
+};
+
+export function eventsFromSessionUpdate(requestId, params) {
+ const update = params?.update;
+ if (!update || typeof update !== "object") return [];
+ if (update.sessionUpdate === "agent_message_chunk" || update.sessionUpdate === "agent_thought_chunk") {
+ if (update.content?.type !== "text" || typeof update.content.text !== "string" || update.content.text.length === 0) {
+ return [];
+ }
+ return [{
+ requestId,
+ type: update.sessionUpdate === "agent_message_chunk" ? "text-delta" : "reasoning",
+ text: update.content.text,
+ }];
+ }
+ if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") return [];
+ const name = safeToolToken(update.name) || safeToolToken(update.toolCallId) || "tool";
+ const status = safeToolToken(update.status) || "pending";
+ const event = { requestId, type: "tool", name, status };
+ const title = safeToolToken(update.title);
+ if (title) event.title = title;
+ return [event];
+}
+
+export function acpInitializeParams(version = clientVersion()) {
+ return {
+ protocolVersion: PROTOCOL_VERSION,
+ clientCapabilities: {
+ fs: { readTextFile: false, writeTextFile: false },
+ terminal: false,
+ },
+ clientInfo: { name: "edgeever", version },
+ };
+}
+
+export function createAcpSpawnPlan(command, cwd) {
+ if (typeof command !== "string" || !command.trim() || command.includes("\0")) throw new Error("invalid_command");
+ if (typeof cwd !== "string" || !cwd.trim()) throw new Error("invalid_workspace");
+ const workspace = path.resolve(cwd);
+ if (workspace === path.resolve(homedir())) throw new Error("invalid_workspace");
+ return {
+ command,
+ args: [],
+ options: {
+ cwd: workspace,
+ stdio: ["pipe", "pipe", "ignore"],
+ windowsHide: true,
+ shell: false,
+ },
+ };
+}
+
+export function spawnAcpChild(spawnImpl, command, cwd) {
+ const plan = createAcpSpawnPlan(command, cwd);
+ const options = { ...plan.options, shell: false, stdio: ["pipe", "pipe", "ignore"] };
+ if (options.shell !== false) throw new Error("ACP processes cannot use a shell");
+ const child = spawnImpl(plan.command, [], options);
+ trackChild(child);
+ return waitForChildProcessSpawn(child);
+}
+
+// Bun's test runner makes Readable.toWeb end a child pipe before any bytes arrive.
+// Electron (Node) keeps the official Writable.toWeb / Readable.toWeb conversion.
+const useBunStdioBridge = typeof process.versions?.bun === "string";
+
+export function nodeReadableToWeb(nodeStream) {
+ let settled = false;
+ return new ReadableStream({
+ start(controller) {
+ const close = () => {
+ if (settled) return;
+ settled = true;
+ try { controller.close(); } catch { /* already closed */ }
+ };
+ const fail = (error) => {
+ if (settled) return;
+ settled = true;
+ try { controller.error(error); } catch { /* already closed */ }
+ };
+ nodeStream.on("data", (chunk) => {
+ if (settled) return;
+ const bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(String(chunk));
+ try { controller.enqueue(bytes); } catch { /* closed */ }
+ });
+ nodeStream.on("end", close);
+ nodeStream.on("error", fail);
+ },
+ cancel() {
+ settled = true;
+ },
+ });
+}
+
+export function nodeWritableToWeb(nodeStream) {
+ return new WritableStream({
+ write(chunk) {
+ return new Promise((resolve, reject) => {
+ try {
+ nodeStream.write(Buffer.from(chunk), (error) => (error ? reject(error) : resolve()));
+ } catch (error) {
+ reject(error);
+ }
+ });
+ },
+ close() {
+ return new Promise((resolve, reject) => {
+ try {
+ nodeStream.end((error) => (error ? reject(error) : resolve()));
+ } catch (error) {
+ reject(error);
+ }
+ });
+ },
+ });
+}
+
+const acpNdJsonStream = (child) => {
+ if (!child?.stdin || !child?.stdout) throw new Error("stdio_bridge_unavailable");
+ if (!useBunStdioBridge && typeof Writable.toWeb === "function" && typeof Readable.toWeb === "function") {
+ return ndJsonStream(Writable.toWeb(child.stdin), Readable.toWeb(child.stdout));
+ }
+ return ndJsonStream(nodeWritableToWeb(child.stdin), nodeReadableToWeb(child.stdout));
+};
+
+export async function createAcpWorkspace(mkdtempImpl = mkdtemp, root = tmpdir()) {
+ const resolvedRoot = path.resolve(root);
+ if (resolvedRoot === path.resolve(homedir())) throw new Error("invalid_workspace");
+ const directory = await mkdtempImpl(path.join(resolvedRoot, "edgeever-acp-"));
+ if (path.resolve(directory) === path.resolve(homedir())) throw new Error("invalid_workspace");
+ return directory;
+}
+
+export async function removeAcpWorkspace(directory, rmImpl = rm) {
+ if (typeof directory !== "string" || !directory) return;
+ const resolved = path.resolve(directory);
+ if (resolved === path.resolve(homedir()) || !resolved.includes(WORKSPACE_MARKER)) return;
+ await rmImpl(resolved, { recursive: true, force: true });
+}
+
+const normalizePromptCapabilities = (initialized) => {
+ const caps = initialized?.agentCapabilities?.promptCapabilities ?? {};
+ return {
+ image: caps.image === true,
+ embeddedContext: caps.embeddedContext === true,
+ };
+};
+
+const createEdgeEverAcpClient = (requestId, emit) => ({
+ requestPermission() {
+ return { outcome: { outcome: "cancelled" } };
+ },
+ sessionUpdate(params) {
+ for (const event of eventsFromSessionUpdate(requestId, params)) emit(event);
+ },
+ readTextFile() {
+ throw new Error("filesystem access is not available");
+ },
+ writeTextFile() {
+ throw new Error("filesystem access is not available");
+ },
+ createTerminal() {
+ throw new Error("terminal access is not available");
+ },
+ terminalOutput() {
+ throw new Error("terminal access is not available");
+ },
+ releaseTerminal() {
+ throw new Error("terminal access is not available");
+ },
+ waitForTerminalExit() {
+ throw new Error("terminal access is not available");
+ },
+ killTerminal() {
+ throw new Error("terminal access is not available");
+ },
+});
+
+function stopChild(child) {
+ if (!child) return;
+ const killTimer = setTimeout(() => {
+ if (child.exitCode == null) {
+ try { child.kill("SIGKILL"); } catch { /* already gone */ }
+ }
+ }, 500);
+ killTimer.unref?.();
+ child.once?.("exit", () => clearTimeout(killTimer));
+ try { child.stdin?.destroy(); } catch { /* closed */ }
+ try { child.kill("SIGTERM"); } catch { /* already gone */ }
+}
+
+const promptFailureMessage = (failure) => {
+ if (failure.state === "not_installed") return "not_installed";
+ if (failure.state === "needs_login") return "needs_login";
+ return failure.detail || "connection_failed";
+};
+
+export function createAcpHostRuntime(options = {}) {
+ const spawnImpl = options.spawnImpl ?? nodeSpawn;
+ const mkdtempImpl = options.mkdtemp ?? mkdtemp;
+ const rmImpl = options.rm ?? rm;
+ const version = options.clientVersion ?? clientVersion();
+ const handshakeTimeoutMs = options.handshakeTimeoutMs ?? HANDSHAKE_TIMEOUT_MS;
+ const commandDeps = options;
+ const active = new Map();
+
+ const connect = async (command, requestId, emit, signal) => {
+ const cwd = await createAcpWorkspace(mkdtempImpl, options.tmpRoot);
+ let child = null;
+ const abort = () => stopChild(child);
+ signal?.addEventListener("abort", abort, { once: true });
+ try {
+ if (signal?.aborted) throw Object.assign(new Error("connection_timeout"), { code: "TIMEOUT" });
+ child = await spawnAcpChild(spawnImpl, command, cwd);
+ if (signal?.aborted) throw Object.assign(new Error("connection_timeout"), { code: "TIMEOUT" });
+ const stream = acpNdJsonStream(child);
+ const connection = new ClientSideConnection(() => createEdgeEverAcpClient(requestId, emit), stream);
+ void connection.closed?.catch(() => {});
+ const initialized = await connection.initialize(acpInitializeParams(version));
+ const session = await connection.newSession({ cwd, mcpServers: [] });
+ if (signal?.aborted) throw Object.assign(new Error("connection_timeout"), { code: "TIMEOUT" });
+ return {
+ cwd,
+ child,
+ connection,
+ sessionId: session.sessionId,
+ promptCapabilities: normalizePromptCapabilities(initialized),
+ stop: abort,
+ };
+ } catch (error) {
+ abort();
+ await removeAcpWorkspace(cwd, rmImpl);
+ throw error;
+ } finally {
+ signal?.removeEventListener("abort", abort);
+ }
+ };
+
+ const withHandshakeTimeout = async (operation) => {
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), handshakeTimeoutMs);
+ timer.unref?.();
+ try {
+ return await operation(controller.signal);
+ } catch (error) {
+ if (controller.signal.aborted && !isAuthRequiredError(error) && error?.code !== "ENOENT") {
+ throw Object.assign(new Error("connection_timeout"), { code: "TIMEOUT" });
+ }
+ throw error;
+ } finally {
+ clearTimeout(timer);
+ }
+ };
+
+ return {
+ listAdapters() {
+ const codex = resolveAcpCommand({ id: "codex" }, commandDeps);
+ const codexAdapter = codex.ok
+ ? { ...adapterShell("codex"), state: "failed", detail: "not_probed" }
+ : adapterFromResolution("codex", codex);
+ return [codexAdapter, { ...adapterShell("antigravity"), state: "not_installed" }];
+ },
+
+ async probeAdapter(input) {
+ const id = input?.id;
+ if (id !== "codex" && id !== "antigravity") throw new Error("unknown_adapter");
+ const resolved = resolveAcpCommand(input, commandDeps);
+ if (!resolved.ok) return adapterFromResolution(id, resolved);
+ let connected;
+ try {
+ connected = await withHandshakeTimeout((signal) => connect(resolved.command, `probe-${id}`, () => {}, signal));
+ } catch (error) {
+ return { ...adapterShell(id), ...failureFields(classifyAcpFailure(error)) };
+ }
+ const adapter = {
+ ...adapterShell(id),
+ state: "available",
+ promptCapabilities: connected.promptCapabilities,
+ };
+ connected.stop();
+ await removeAcpWorkspace(connected.cwd, rmImpl);
+ return adapter;
+ },
+
+ async prompt(input, emit = () => {}) {
+ if (!input || typeof input !== "object" || typeof input.prompt !== "string") throw new Error("invalid_prompt");
+ if (input.adapterId !== "codex" && input.adapterId !== "antigravity") throw new Error("unknown_adapter");
+ const requestId = randomUUID();
+ const notify = (event) => emit(event);
+ const fail = (message) => {
+ notify({ requestId, type: "error", message });
+ return { requestId };
+ };
+ const resolved = resolveAcpCommand({ id: input.adapterId, path: input.path }, commandDeps);
+ if (!resolved.ok) return fail(promptFailureMessage(resolved));
+
+ let connected;
+ try {
+ connected = await withHandshakeTimeout((signal) => connect(resolved.command, requestId, notify, signal));
+ } catch (error) {
+ return fail(promptFailureMessage(classifyAcpFailure(error)));
+ }
+
+ let content;
+ try {
+ content = buildPromptContent({
+ prompt: input.prompt,
+ contextText: input.contextText,
+ attachments: input.attachments,
+ promptCapabilities: connected.promptCapabilities,
+ });
+ } catch (error) {
+ connected.stop();
+ await removeAcpWorkspace(connected.cwd, rmImpl);
+ return fail(promptFailureMessage(classifyAcpFailure(error)));
+ }
+
+ const session = {
+ cancelled: false,
+ settled: false,
+ ...connected,
+ };
+ active.set(requestId, session);
+ const finish = (event) => {
+ if (session.settled) return;
+ session.settled = true;
+ notify(event);
+ session.stop();
+ active.delete(requestId);
+ void removeAcpWorkspace(session.cwd, rmImpl);
+ };
+ void connected.connection.prompt({ sessionId: connected.sessionId, prompt: content.blocks }).then(() => {
+ finish({ requestId, type: "done" });
+ }).catch((error) => {
+ if (session.cancelled) finish({ requestId, type: "done" });
+ else finish({ requestId, type: "error", message: promptFailureMessage(classifyAcpFailure(error)) });
+ });
+ return { requestId, rejectedAttachments: content.rejectedAttachments };
+ },
+
+ async cancel(requestId) {
+ const session = active.get(requestId);
+ if (!session) return { ok: true };
+ session.cancelled = true;
+ try {
+ if (session.connection && session.sessionId) await session.connection.cancel({ sessionId: session.sessionId });
+ } catch {
+ // The process may already be gone. Killing it is enough.
+ }
+ session.stop();
+ return { ok: true };
+ },
+ };
+}
+
+const failureFields = (failure) => (
+ failure.state === "failed"
+ ? { state: "failed", detail: failure.detail || "connection_failed" }
+ : { state: failure.state }
+);
+
+export function registerAcpIpc(ipcMain, runtime = createAcpHostRuntime()) {
+ const send = (sender, event) => {
+ if (!sender || sender.isDestroyed?.()) return;
+ sender.send("desktop:acp-event", event);
+ };
+ ipcMain.handle("desktop:acp-list", () => runtime.listAdapters());
+ ipcMain.handle("desktop:acp-probe", (_event, input) => runtime.probeAdapter(input));
+ ipcMain.handle("desktop:acp-prompt", (event, input) => runtime.prompt(input, (acpEvent) => send(event.sender, acpEvent)));
+ ipcMain.handle("desktop:acp-cancel", (_event, requestId) => runtime.cancel(requestId));
+ return runtime;
+}
diff --git a/apps/desktop/src/main/acp-host.test.mjs b/apps/desktop/src/main/acp-host.test.mjs
new file mode 100644
index 00000000..52edac87
--- /dev/null
+++ b/apps/desktop/src/main/acp-host.test.mjs
@@ -0,0 +1,554 @@
+import { describe, expect, test } from "bun:test";
+import { EventEmitter } from "node:events";
+import { realpathSync } from "node:fs";
+import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
+import { createRequire } from "node:module";
+import { homedir, tmpdir } from "node:os";
+import path from "node:path";
+import { pathToFileURL } from "node:url";
+import { RequestError } from "@agentclientprotocol/sdk";
+import {
+ acpInitializeParams,
+ buildPromptContent,
+ classifyAcpFailure,
+ createAcpHostRuntime,
+ createAcpSpawnPlan,
+ eventsFromSessionUpdate,
+ isAuthRequiredError,
+ registerAcpIpc,
+ resolveAcpCommand,
+ sanitizeFailureDetail,
+ spawnAcpChild,
+} from "./acp-host.mjs";
+
+const require = createRequire(import.meta.url);
+const sdkHref = pathToFileURL(require.resolve("@agentclientprotocol/sdk")).href;
+const home = path.resolve(homedir());
+
+const encode = (value) => Buffer.from(value).toString("base64");
+
+const collector = () => {
+ const events = [];
+ let waiters = [];
+ return {
+ events,
+ emit(event) {
+ events.push(event);
+ waiters = waiters.filter((waiter) => {
+ if (!waiter.predicate(event)) return true;
+ waiter.resolve(event);
+ return false;
+ });
+ },
+ waitFor(predicate, timeout = 5000) {
+ const found = events.find(predicate);
+ if (found) return Promise.resolve(found);
+ return new Promise((resolve, reject) => {
+ const timer = setTimeout(() => {
+ reject(new Error(`timed out; saw ${events.map((event) => event.type).join(",") || "nothing"}`));
+ }, timeout);
+ waiters.push({
+ predicate,
+ resolve: (event) => {
+ clearTimeout(timer);
+ resolve(event);
+ },
+ });
+ });
+ },
+ };
+};
+
+const fakeAgentSource = ({ reportPath, secretPath, allowImage, allowEmbedded, hold }) => `#!/usr/bin/env bun
+import * as acp from ${JSON.stringify(sdkHref)};
+import { writeFileSync } from "node:fs";
+
+const reportPath = ${JSON.stringify(reportPath)};
+const secretPath = ${JSON.stringify(secretPath)};
+const allowImage = ${allowImage ? "true" : "false"};
+const allowEmbedded = ${allowEmbedded ? "true" : "false"};
+const hold = ${hold ? "true" : "false"};
+const report = { initialize: null, newSession: null, permission: null, readError: null, readResult: null, prompt: null };
+const save = () => writeFileSync(reportPath, JSON.stringify(report));
+writeFileSync(reportPath + ".pid", String(process.pid));
+let releaseHold = () => {};
+const released = new Promise((resolve) => {
+ releaseHold = resolve;
+});
+
+const stream = acp.ndJsonStream(
+ new WritableStream({
+ write(chunk) {
+ return new Promise((resolve, reject) => {
+ process.stdout.write(Buffer.from(chunk), (error) => (error ? reject(error) : resolve()));
+ });
+ },
+ }),
+ new ReadableStream({
+ start(controller) {
+ process.stdin.on("data", (chunk) => {
+ const bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(String(chunk));
+ try { controller.enqueue(bytes); } catch { /* closed */ }
+ });
+ process.stdin.on("end", () => {
+ try { controller.close(); } catch { /* already closed */ }
+ });
+ process.stdin.on("error", (error) => {
+ try { controller.error(error); } catch { /* already closed */ }
+ });
+ },
+ }),
+);
+
+acp.agent({ name: "edgeever-fake-agent" })
+ .onRequest("initialize", (ctx) => {
+ report.initialize = ctx.params;
+ save();
+ return {
+ protocolVersion: ctx.params.protocolVersion,
+ agentCapabilities: { promptCapabilities: { image: allowImage, embeddedContext: allowEmbedded } },
+ };
+ })
+ .onRequest("session/new", (ctx) => {
+ report.newSession = { cwd: ctx.params.cwd, mcpServers: ctx.params.mcpServers };
+ save();
+ return { sessionId: "sess-1" };
+ })
+ .onRequest("session/prompt", async (ctx) => {
+ report.prompt = ctx.params.prompt;
+ save();
+ await ctx.client.notify("session/update", {
+ sessionId: ctx.params.sessionId,
+ update: { sessionUpdate: "agent_thought_chunk", content: { type: "text", text: "thinking" } },
+ });
+ await ctx.client.notify("session/update", {
+ sessionId: ctx.params.sessionId,
+ update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "hello from agent" } },
+ });
+ await ctx.client.notify("session/update", {
+ sessionId: ctx.params.sessionId,
+ update: { sessionUpdate: "tool_call", toolCallId: "call_1", name: "search", title: "Search notes", status: "pending" },
+ });
+ if (!hold) {
+ try {
+ const result = await ctx.client.request("fs/read_text_file", { sessionId: ctx.params.sessionId, path: secretPath });
+ report.readResult = result?.content ?? result ?? null;
+ } catch (error) {
+ report.readError = error instanceof Error ? error.message : String(error);
+ report.readResult = null;
+ }
+ report.permission = await ctx.client.request("session/request_permission", {
+ sessionId: ctx.params.sessionId,
+ toolCall: { toolCallId: "call_1", title: "Search notes", status: "pending" },
+ options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
+ });
+ save();
+ return { stopReason: "end_turn" };
+ }
+ await released;
+ return { stopReason: "cancelled" };
+ })
+ .onNotification("session/cancel", () => {
+ releaseHold();
+ })
+ .connect(stream);
+`;
+
+const writeFakeAgent = async (directory, options) => {
+ const scriptPath = path.join(directory, "fake-agent.mjs");
+ await writeFile(scriptPath, fakeAgentSource(options), "utf8");
+ await chmod(scriptPath, 0o755);
+ return scriptPath;
+};
+
+const waitUntilExited = async (pid) => {
+ const started = Date.now();
+ while (Date.now() - started < 3000) {
+ try {
+ process.kill(pid, 0);
+ } catch (error) {
+ if (error?.code === "ESRCH") return;
+ throw error;
+ }
+ await new Promise((resolve) => setTimeout(resolve, 40));
+ }
+ throw new Error(`process ${pid} is still running`);
+};
+
+describe("ACP command allow-list", () => {
+ test("rejects relative paths and basenames that contain a slash", () => {
+ for (const configured of ["codex-acp/evil", "bin\\codex-acp", "../codex-acp", "./codex-acp"]) {
+ const result = resolveAcpCommand({ id: "codex", path: configured }, { pathEnv: "", platform: "darwin" });
+ expect(result.ok).toBe(false);
+ expect(result.detail).toBe("invalid_path");
+ expect(JSON.stringify(result)).not.toContain(configured);
+ }
+ expect(resolveAcpCommand({ id: "antigravity", path: "agy" }).ok).toBe(false);
+ expect(resolveAcpCommand({ id: "antigravity", path: "relative/agy" }).detail).toBe("invalid_path");
+ expect(resolveAcpCommand({ id: "antigravity", path: "../agy" }).detail).toBe("invalid_path");
+ expect(JSON.stringify(resolveAcpCommand({ id: "antigravity", path: "/tmp/../agy" }))).not.toContain("/tmp/../agy");
+ });
+
+ test("lists Codex from PATH presence without marking it available", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-path-"));
+ try {
+ const binary = path.join(directory, "codex-acp");
+ await writeFile(binary, "#!/bin/sh\nexit 0\n", { mode: 0o755 });
+ const present = createAcpHostRuntime({ pathEnv: directory, platform: "darwin", sep: path.sep, delimiter: path.delimiter });
+ const listed = present.listAdapters();
+ expect(listed.find((adapter) => adapter.id === "codex")).toEqual({
+ id: "codex",
+ label: "Codex",
+ state: "failed",
+ detail: "not_probed",
+ });
+ expect(listed.find((adapter) => adapter.id === "antigravity")?.state).toBe("not_installed");
+ expect(JSON.stringify(listed)).not.toContain(directory);
+
+ const missing = createAcpHostRuntime({ pathEnv: "", platform: "darwin" });
+ expect(missing.listAdapters().find((adapter) => adapter.id === "codex")?.state).toBe("not_installed");
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+
+ test("looks up codex-acp.exe only as a win32 basename", () => {
+ const seen = [];
+ resolveAcpCommand({ id: "codex" }, {
+ platform: "win32",
+ pathEnv: "C:\\Tools",
+ delimiter: ";",
+ sep: "\\",
+ accessSync(candidate) {
+ seen.push(candidate);
+ const error = new Error("missing");
+ error.code = "ENOENT";
+ throw error;
+ },
+ });
+ expect(seen).toEqual(["C:\\Tools\\codex-acp", "C:\\Tools\\codex-acp.exe"]);
+ });
+
+ test("requires an Antigravity absolute file and does not accept a directory", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-agy-"));
+ try {
+ const file = path.join(directory, "antigravity-acp");
+ await writeFile(file, "", { mode: 0o755 });
+ const fileResult = resolveAcpCommand({ id: "antigravity", path: file });
+ expect(fileResult).toEqual({ ok: true, command: realpathSync(file) });
+ expect(resolveAcpCommand({ id: "antigravity", path: directory })).toMatchObject({ ok: false, detail: "invalid_path" });
+ const missing = resolveAcpCommand({ id: "antigravity", path: path.join(directory, "missing-binary") });
+ expect(missing).toEqual({ ok: false, state: "not_installed" });
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+});
+
+describe("ACP prompt blocks", () => {
+ const image = { filename: "pic.png", mediaType: "image/png", dataBase64: encode("png-bytes") };
+ const pdf = { filename: "/Users/me/secret.pdf", mediaType: "application/pdf", dataBase64: encode("%PDF-1.7") };
+ const note = { filename: "/tmp/notes/body.txt", mediaType: "text/plain", dataBase64: encode("hello notes") };
+
+ test("omits images and PDFs until the agent advertises those capabilities", () => {
+ const blocked = buildPromptContent({
+ prompt: "go",
+ contextText: "ctx",
+ attachments: [image, pdf, note, { filename: "song.mp3", mediaType: "audio/mpeg", dataBase64: encode("nope") }],
+ promptCapabilities: {},
+ });
+ expect(blocked.blocks.map((block) => block.type)).toEqual(["text", "text", "text"]);
+ expect(blocked.blocks.map((block) => block.text)).toEqual(["ctx", "go", "hello notes"]);
+ expect(blocked.rejectedAttachments.map((item) => item.reason)).toEqual([
+ "This agent does not accept images.",
+ "This agent does not accept embedded files.",
+ "This file type is not supported.",
+ ]);
+ expect(JSON.stringify(blocked.blocks)).not.toContain("/Users/me/secret.pdf");
+ expect(JSON.stringify(blocked.blocks)).not.toContain("/tmp/notes/body.txt");
+ expect(JSON.stringify(blocked.blocks)).not.toContain("resource_link");
+ });
+
+ test("includes images and embedded PDFs without a filesystem path", () => {
+ const allowed = buildPromptContent({
+ prompt: "go",
+ attachments: [image, pdf],
+ promptCapabilities: { image: true, embeddedContext: true },
+ });
+ const picture = allowed.blocks.find((block) => block.type === "image");
+ const resource = allowed.blocks.find((block) => block.type === "resource");
+ expect(picture).toEqual({ type: "image", data: image.dataBase64, mimeType: "image/png" });
+ expect(resource.resource.uri).toBe("edgeever-attachment:secret.pdf");
+ expect(resource.resource.mimeType).toBe("application/pdf");
+ expect(resource.resource.blob).toBe(pdf.dataBase64);
+ const serialized = JSON.stringify(allowed.blocks);
+ expect(serialized).not.toContain("/Users/me");
+ expect(serialized).not.toContain("file:");
+ expect(serialized).not.toContain("resource_link");
+ expect(allowed.rejectedAttachments).toEqual([]);
+ });
+});
+
+describe("ACP spawn and failure mapping", () => {
+ test("forces shell false and refuses the home directory as cwd", async () => {
+ const source = await Bun.file(new URL("./acp-host.mjs", import.meta.url)).text();
+ expect(source).toContain("shell: false");
+ expect(source).not.toContain("shell: true");
+ expect(() => createAcpSpawnPlan("/bin/echo", home)).toThrow(/invalid_workspace/);
+ const workspace = await mkdtemp(path.join(tmpdir(), "edgeever-acp-spawn-"));
+ const child = new EventEmitter();
+ let captured;
+ const spawned = spawnAcpChild((command, args, options) => {
+ captured = { command, args, options };
+ queueMicrotask(() => child.emit("spawn"));
+ return child;
+ }, "/opt/codex-acp", workspace);
+ await spawned;
+ expect(captured).toEqual({
+ command: "/opt/codex-acp",
+ args: [],
+ options: expect.objectContaining({ shell: false, cwd: path.resolve(workspace) }),
+ });
+ expect(captured.options.shell).toBe(false);
+ expect(captured.options.stdio).toEqual(["pipe", "pipe", "ignore"]);
+ await rm(workspace, { recursive: true, force: true });
+ });
+
+ test("maps auth, missing binaries, and strips home paths from details", () => {
+ expect(isAuthRequiredError(RequestError.authRequired())).toBe(true);
+ expect(isAuthRequiredError(new Error("session failed: auth_required"))).toBe(true);
+ expect(isAuthRequiredError(new Error("connection reset"))).toBe(false);
+ expect(classifyAcpFailure(Object.assign(new Error(`spawn ${home}/bin ENOENT`), { code: "ENOENT" })).state).toBe("not_installed");
+ const detail = sanitizeFailureDetail(new Error(`failed ${home}/.gemini/oauth TOKEN=abc Bearer secret-value`));
+ expect(detail).not.toContain(home);
+ expect(detail).not.toContain("TOKEN=abc");
+ expect(detail).not.toContain("secret-value");
+ expect(detail).not.toContain(".gemini");
+ });
+
+ test("client capabilities do not enable filesystem or terminal access", () => {
+ expect(acpInitializeParams("1.2.3")).toEqual({
+ protocolVersion: 1,
+ clientCapabilities: {
+ fs: { readTextFile: false, writeTextFile: false },
+ terminal: false,
+ },
+ clientInfo: { name: "edgeever", version: "1.2.3" },
+ });
+ });
+
+ test("maps text, reasoning, and tool progress without diff payloads", () => {
+ expect(eventsFromSessionUpdate("r1", {
+ update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "hello" } },
+ })).toEqual([{ requestId: "r1", type: "text-delta", text: "hello" }]);
+ expect(eventsFromSessionUpdate("r1", {
+ update: { sessionUpdate: "agent_thought_chunk", content: { type: "text", text: "hmm" } },
+ })).toEqual([{ requestId: "r1", type: "reasoning", text: "hmm" }]);
+ const tool = eventsFromSessionUpdate("r1", {
+ update: {
+ sessionUpdate: "tool_call",
+ toolCallId: "call_1",
+ name: "edit",
+ title: "Edit note",
+ status: "pending",
+ content: [{ type: "diff", path: `${home}/note.md`, oldText: "a", newText: "b" }],
+ },
+ });
+ expect(tool).toEqual([{ requestId: "r1", type: "tool", name: "edit", status: "pending", title: "Edit note" }]);
+ expect(JSON.stringify(tool)).not.toContain(home);
+ expect(eventsFromSessionUpdate("r1", {
+ update: { sessionUpdate: "tool_call", toolCallId: "call_2", title: `${home}/secret`, status: "completed" },
+ })).toEqual([{ requestId: "r1", type: "tool", name: "call_2", status: "completed" }]);
+ });
+});
+
+// bun test started at the workspace root drops child stdin and stdout pipes
+// (the bytes never arrive). The same processes work when cwd is apps/desktop,
+// which is how `bun run test:desktop` runs this file.
+const stdioPipesWork = await (async () => {
+ const proc = Bun.spawn(["/bin/echo", "ok"], { stdout: "pipe", stderr: "ignore" });
+ const text = await new Response(proc.stdout).text();
+ await proc.exited;
+ return text.includes("ok");
+})();
+const sessionTest = stdioPipesWork ? test : test.skip;
+
+describe("ACP stdio session", () => {
+ const attachments = () => ([
+ { filename: "/Users/me/secret.pdf", mediaType: "application/pdf", dataBase64: encode("%PDF-1.7") },
+ { filename: "pic.png", mediaType: "image/png", dataBase64: encode("png-bytes") },
+ ]);
+
+ const readReport = async (reportPath) => JSON.parse(await readFile(reportPath, "utf8"));
+
+ sessionTest("probes a session, then closes the process", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-probe-"));
+ const reportPath = path.join(directory, "report.json");
+ try {
+ const scriptPath = await writeFakeAgent(directory, {
+ reportPath,
+ secretPath: path.join(directory, "secret.txt"),
+ allowImage: true,
+ allowEmbedded: true,
+ hold: false,
+ });
+ const wrapper = path.join(directory, "wrap.sh");
+ await writeFile(wrapper, `#!/bin/sh\necho RAN > ${JSON.stringify(path.join(directory, "ran.txt"))}\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(scriptPath)}\n`);
+ await chmod(wrapper, 0o755);
+ const { spawn } = await import("node:child_process");
+ const spawned = [];
+ const runtime = createAcpHostRuntime({
+ spawnImpl(command, args, options) {
+ spawned.push({ command, args, cwd: options.cwd, shell: options.shell });
+ return spawn(command, args, options);
+ },
+ });
+ const probed = await runtime.probeAdapter({ id: "antigravity", path: wrapper });
+ expect(spawned[0]?.shell).toBe(false);
+ expect(realpathSync(spawned[0]?.command)).toBe(realpathSync(wrapper));
+ expect(probed.state).toBe("available");
+ expect(probed.promptCapabilities).toEqual({ image: true, embeddedContext: true });
+ const report = await readReport(reportPath);
+ expect(report.initialize.clientCapabilities).toEqual({
+ auth: { terminal: false },
+ fs: { readTextFile: false, writeTextFile: false },
+ terminal: false,
+ });
+ expect(path.resolve(report.newSession.cwd)).not.toBe(home);
+ expect(report.newSession.cwd).toContain(`${path.sep}edgeever-acp-`);
+ expect(report.newSession.mcpServers).toEqual([]);
+ const pid = Number(await readFile(`${reportPath}.pid`, "utf8"));
+ await waitUntilExited(pid);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ }, 15_000);
+
+ sessionTest("streams updates, cancels permission, and never reads a local file", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-prompt-"));
+ const reportPath = path.join(directory, "report.json");
+ const secretPath = path.join(directory, "secret.txt");
+ await writeFile(secretPath, "SENTINEL_SECRET_CONTENT");
+ try {
+ const scriptPath = await writeFakeAgent(directory, {
+ reportPath,
+ secretPath,
+ allowImage: true,
+ allowEmbedded: true,
+ hold: false,
+ });
+ const runtime = createAcpHostRuntime();
+ const events = collector();
+ const result = await runtime.prompt({
+ adapterId: "antigravity",
+ path: scriptPath,
+ prompt: "hello",
+ contextText: "context",
+ attachments: attachments(),
+ }, events.emit);
+ await events.waitFor((event) => event.type === "done");
+ const report = await readReport(reportPath);
+ expect(report.permission?.outcome?.outcome).toBe("cancelled");
+ expect(report.readResult).toBeNull();
+ expect(JSON.stringify(report)).not.toContain("SENTINEL_SECRET_CONTENT");
+ expect(path.resolve(report.newSession.cwd)).not.toBe(home);
+ const serialized = JSON.stringify(report.prompt);
+ expect(serialized).not.toContain("/Users/me/secret.pdf");
+ expect(serialized).not.toContain(secretPath);
+ expect(serialized).not.toContain("resource_link");
+ expect(report.prompt.some((block) => block.type === "image" && block.mimeType === "image/png")).toBe(true);
+ expect(report.prompt.some((block) => block.type === "resource" && String(block.resource?.uri).startsWith("edgeever-attachment:"))).toBe(true);
+ expect(events.events).toContainEqual({ requestId: result.requestId, type: "reasoning", text: "thinking" });
+ expect(events.events).toContainEqual({ requestId: result.requestId, type: "text-delta", text: "hello from agent" });
+ expect(events.events).toContainEqual({
+ requestId: result.requestId,
+ type: "tool",
+ name: "search",
+ status: "pending",
+ title: "Search notes",
+ });
+ expect(events.events.at(-1)).toEqual({ requestId: result.requestId, type: "done" });
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ }, 15_000);
+
+ sessionTest("drops image and PDF blocks when the agent does not advertise them", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-omit-"));
+ 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();
+ const events = collector();
+ const result = await runtime.prompt({
+ adapterId: "antigravity",
+ path: scriptPath,
+ prompt: "hello",
+ attachments: attachments(),
+ }, events.emit);
+ expect(result.rejectedAttachments?.map((item) => item.reason)).toEqual([
+ "This agent does not accept embedded files.",
+ "This agent does not accept images.",
+ ]);
+ await events.waitFor((event) => event.type === "done");
+ const report = await readReport(reportPath);
+ expect(report.prompt.every((block) => block.type === "text")).toBe(true);
+ expect(JSON.stringify(report.prompt)).not.toContain("/Users/me");
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ }, 15_000);
+
+ sessionTest("cancels an in-flight prompt and ignores unknown ids", async () => {
+ const directory = await mkdtemp(path.join(tmpdir(), "edgeever-acp-cancel-"));
+ 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: true,
+ });
+ const runtime = createAcpHostRuntime();
+ const events = collector();
+ const result = await runtime.prompt({
+ adapterId: "antigravity",
+ path: scriptPath,
+ prompt: "wait",
+ }, events.emit);
+ await events.waitFor((event) => event.type === "text-delta");
+ expect(await runtime.cancel(result.requestId)).toEqual({ ok: true });
+ expect(await runtime.cancel("missing-request")).toEqual({ ok: true });
+ await events.waitFor((event) => event.type === "done" || event.type === "error");
+ const pid = Number(await readFile(`${reportPath}.pid`, "utf8"));
+ await waitUntilExited(pid);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ }, 15_000);
+});
+
+test("desktop preload and main process expose the ACP host", async () => {
+ const [main, preload, host] = await Promise.all([
+ Bun.file(new URL("./index.mjs", import.meta.url)).text(),
+ Bun.file(new URL("../preload/index.cjs", import.meta.url)).text(),
+ Bun.file(new URL("./acp-host.mjs", import.meta.url)).text(),
+ ]);
+ expect(main).toContain("registerAcpIpc(ipcMain)");
+ expect(host).toContain('ipcMain.handle("desktop:acp-prompt"');
+ expect(preload).toContain('listAcpAdapters: () => ipcRenderer.invoke("desktop:acp-list")');
+ expect(preload).toContain('probeAcpAdapter: (input) => ipcRenderer.invoke("desktop:acp-probe", input)');
+ expect(preload).toContain('promptAcp: (input) => ipcRenderer.invoke("desktop:acp-prompt", input)');
+ expect(preload).toContain('cancelAcp: (requestId) => ipcRenderer.invoke("desktop:acp-cancel", requestId)');
+ expect(preload).toContain('ipcRenderer.on("desktop:acp-event"');
+ const handlers = new Map();
+ registerAcpIpc({ handle: (channel, handler) => handlers.set(channel, handler) });
+ expect([...handlers.keys()]).toEqual(["desktop:acp-list", "desktop:acp-probe", "desktop:acp-prompt", "desktop:acp-cancel"]);
+});
diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjs
index d2e26832..d2b7007e 100644
--- a/apps/desktop/src/main/index.mjs
+++ b/apps/desktop/src/main/index.mjs
@@ -48,6 +48,7 @@ import {
import electronUpdater from "electron-updater";
import { createPluginPublicNetworkRuntime } from "./plugin-public-network.mjs";
import { createAiDirectRuntime } from "./ai-direct.mjs";
+import { registerAcpIpc } from "./acp-host.mjs";
import { shouldQuitAfterAllWindowsClosed } from "./window-lifecycle.mjs";
import {
RENDERER_HIBERNATE_PREPARE_TIMEOUT_MS,
@@ -1593,6 +1594,7 @@ const startApplication = async () => {
ipcMain.on("desktop:ai-direct-cancel", (event, requestId) => {
if (event.sender === mainWindow?.webContents && typeof requestId === "string") aiDirect.cancel(requestId);
});
+ registerAcpIpc(ipcMain);
ipcMain.handle("desktop:sync-scheduled-tasks", async (event, tasks) => {
if (event.sender !== mainWindow?.webContents) throw new Error("Scheduled tasks must come from the main window");
if (!Array.isArray(tasks) || tasks.length > 1_000) throw new Error("Invalid scheduled task list");
diff --git a/apps/desktop/src/preload/index.cjs b/apps/desktop/src/preload/index.cjs
index e8901d38..d7fb2579 100644
--- a/apps/desktop/src/preload/index.cjs
+++ b/apps/desktop/src/preload/index.cjs
@@ -48,6 +48,15 @@ contextBridge.exposeInMainWorld("edgeeverDesktop", Object.freeze({
ipcRenderer.on("desktop:ai-direct-chunk", listener);
return () => ipcRenderer.removeListener("desktop:ai-direct-chunk", listener);
},
+ listAcpAdapters: () => ipcRenderer.invoke("desktop:acp-list"),
+ probeAcpAdapter: (input) => ipcRenderer.invoke("desktop:acp-probe", input),
+ promptAcp: (input) => ipcRenderer.invoke("desktop:acp-prompt", input),
+ cancelAcp: (requestId) => ipcRenderer.invoke("desktop:acp-cancel", requestId),
+ onAcpEvent: (callback) => {
+ const listener = (_event, payload) => callback(payload);
+ ipcRenderer.on("desktop:acp-event", listener);
+ return () => ipcRenderer.removeListener("desktop:acp-event", listener);
+ },
clearLocalData: () => ipcRenderer.invoke("desktop:clear-local-data"),
recordRendererError: (details) => ipcRenderer.invoke("desktop:record-renderer-error", details),
openRendererIssue: (details) => ipcRenderer.invoke("desktop:open-renderer-issue", details),
diff --git a/apps/web/package.json b/apps/web/package.json
index 051096aa..d007b26f 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -12,9 +12,11 @@
"@floating-ui/react-dom": "^2.1.8",
"@fontsource-variable/noto-sans-sc": "^5.2.8",
"@lezer/highlight": "^1.2.3",
+ "@radix-ui/react-hover-card": "^1.1.23",
"@radix-ui/react-separator": "^1.1.15",
"@radix-ui/react-slot": "^1.3.3",
"@radix-ui/react-tooltip": "^1.2.16",
+ "@radix-ui/react-use-controllable-state": "^1.2.6",
"@streamdown/cjk": "^1.0.3",
"@streamdown/code": "^1.1.1",
"@streamdown/math": "^1.0.2",
@@ -31,6 +33,7 @@
"lucide-react": "1.21.0",
"mermaid": "^11.16.0",
"motion": "^12.43.0",
+ "nanoid": "^6.0.1",
"pagedjs": "^0.4.3",
"streamdown": "^2.6.0",
"use-stick-to-bottom": "^1.1.6"
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 0167e325..106289f2 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -109,7 +109,7 @@ import {
} from "./editor/math-formula";
import { memoShareQueryKey, ShareMemoDialog } from "./dialogs/ShareMemoDialog";
import { ShareNoteImageDialog } from "./dialogs/ShareNoteImageDialog";
-import { AiAssistantDialog, type AiAssistantAnchor } from "./dialogs/AiAssistantDialog";
+import { AiSidebar, readAiSidebarOpen, readAiSidebarWidth, writeAiSidebarOpen } from "./ai-sidebar/AiSidebar";
import { api } from "@/lib/api";
import { isDesktopResourceRuntime, stageDesktopResource, toDesktopResourceDownloadUrl, toDesktopResourceUrl } from "@/lib/desktop-resources";
import { contentReferencesStagedResourceUrl, findMatchingMemoResource, repairMemoStagedResourceUrls, repairTiptapStagedResourceUrls } from "@/lib/staged-resource-repair";
@@ -193,7 +193,6 @@ import {
insertMarkdownSnippet,
isAttachmentLinkHref,
} from "@/lib/editor-external-link";
-import { insertAiDraftAtTextCursor } from "@/lib/ai-draft-insertion";
import { createFileBatchQueue, processFileUploadBatch } from "@/lib/file-batch";
import { resourceToMarkdown } from "@/lib/markdown-resource-paste";
import { MEMO_ID_REMAPPED_EVENT, MEMO_SYNC_ACKNOWLEDGED_EVENT } from "@/lib/sync-events";
@@ -201,9 +200,7 @@ import { useStandaloneMobileEditor } from "@/hooks/useStandaloneMobileEditor";
import { statusSettleMotion } from "@/lib/motion";
import {
getRichTextAiSelectionContext,
- getRichTextAiReplacementRange,
getRichTextAiSelectionReplacement,
- normalizeAiSelectionReplacement,
} from "@/lib/ai-selection-replacement";
import { getAttachmentResourceId } from "@/lib/attachment-links";
import {
@@ -281,8 +278,6 @@ import {
setMobilePlainTextElementValue,
SUPPORTED_PASTE_IMAGE_TYPES,
syncStatusToSaveState,
- type AiInsertionTarget,
- type AiSelectionContext,
type MobilePlainTextElement,
} from "./editor/editor-pane-helpers";
@@ -402,7 +397,6 @@ const RichEditorPane = ({
onDocumentActionConsumed,
selectionActionBar,
onOpenMemo,
- onOpenAiPrompts,
companionAvailable = false,
beforeCompanionApply,
onCompanionNotesChanged,
@@ -442,11 +436,13 @@ const RichEditorPane = ({
const [imageUploadState, setImageUploadState] = useState<"idle" | "compressing" | "uploading" | "error">("idle");
const [imagePreview, setImagePreview] = useState<ImagePreviewRequestDetail | null>(null);
const [historyOpen, setHistoryOpen] = useState(false);
- const [aiAssistantOpen, setAiAssistantOpen] = useState(false);
- const [aiAssistantAnchor, setAiAssistantAnchor] = useState<AiAssistantAnchor>({ left: 24, placement: "below", top: 96 });
+ const [aiAssistantOpen, setAiAssistantOpenState] = useState(readAiSidebarOpen);
+ const setAiSidebarOpen = useCallback((open: boolean) => {
+ setAiAssistantOpenState(open);
+ writeAiSidebarOpen(open);
+ }, []);
const aiBubbleMenu = useAiBubbleMenu(aiAssistantOpen);
- const [aiSelection, setAiSelection] = useState<AiSelectionContext | null>(null);
- const [aiInsertionTarget, setAiInsertionTarget] = useState<AiInsertionTarget | null>(null);
+ const [aiSelectionText, setAiSelectionText] = useState("");
const [mobileNotebookSheetOpen, setMobileNotebookSheetOpen] = useState(false);
const [notebookUpdatePending, setNotebookUpdatePending] = useState(false);
const handledPluginNavigationRequestRef = useRef(0);
@@ -494,6 +490,28 @@ const RichEditorPane = ({
const [editorOutlineCollapsed, setEditorOutlineCollapsed] = useState(() =>
readEditorOutlineCollapsedPreference({ defaultCollapsed: !demoMode })
);
+ const editorColumnRef = useRef<HTMLDivElement>(null);
+ // The outline is 300px and the desktop gutter is 6rem per side. Below this
+ // pane width those two leave the article at 0, which happens once the AI
+ // sidebar is docked. Hide the outline until the pane is wide enough again.
+ const [editorPaneTight, setEditorPaneTight] = useState(false);
+ useLayoutEffect(() => {
+ const node = editorColumnRef.current;
+ if (!node) return;
+ const update = () => {
+ const column = node.getBoundingClientRect().width;
+ const parent = node.parentElement?.getBoundingClientRect().width ?? column;
+ // While the sidebar is opening, its width is still animating, so the column
+ // has not given up that space yet. Reserve it immediately or the outline
+ // crushes the article for the whole slide.
+ const projected = aiAssistantOpen ? parent - readAiSidebarWidth() : column;
+ setEditorPaneTight(projected < 720);
+ };
+ update();
+ const observer = new ResizeObserver(update);
+ observer.observe(node);
+ return () => observer.disconnect();
+ }, [aiAssistantOpen, isLoading, memo?.id]);
const [phonePreviewOpen, setPhonePreviewOpen] = useState(readEditorPhonePreviewPreference);
const [memoIdCopyNotice, setMemoIdCopyNotice] = useState<{ status: "copied" | "error"; id: string } | null>(null);
const handledSaveAndSyncTokenRef = useRef(saveAndSyncToken);
@@ -609,14 +627,12 @@ const RichEditorPane = ({
useEffect(() => {
if (!desktopReadingProtection) return;
- setAiAssistantOpen(false);
- setAiSelection(null);
- setAiInsertionTarget(null);
+ setAiSidebarOpen(false);
closeNoteReplaceRef.current();
setExternalLinkDialogOpen(false);
setMathFormulaOpen(false);
setNoteLinkPickerOpen(false);
- }, [desktopReadingProtection]);
+ }, [desktopReadingProtection, setAiSidebarOpen]);
const memoRef = useRef<MemoDetail | null>(memo);
const editSessionRef = useRef<MemoEditSession | null>(null);
@@ -1950,194 +1966,49 @@ const RichEditorPane = ({
: memoRef.current?.contentMarkdown ?? "";
}, [editor, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);
- const openAiAssistant = useCallback(() => {
- if (effectiveReadOnly) return;
- let selection: AiSelectionContext | null = null;
- let insertionTarget: AiInsertionTarget | null = null;
-
+ const readLiveAiSelection = useCallback(() => {
if (useMobilePlainTextEditor) {
const source = getMobilePlainTextValue();
const plainTextElement = mobileTextAreaRef.current;
const from = plainTextElement instanceof HTMLTextAreaElement ? plainTextElement.selectionStart : 0;
const to = plainTextElement instanceof HTMLTextAreaElement ? plainTextElement.selectionEnd : from;
- insertionTarget = { kind: "plain", position: to };
- const contentMarkdown = source.slice(from, to).trim();
- if (to > from && contentMarkdown) selection = { kind: "plain", from, to, contentMarkdown };
- } else if (useMarkdownSourceEditor) {
- const selectionPos = markdownSourceEditorRef.current?.getSelection() ?? { from: 0, to: 0 };
- const from = selectionPos.from;
- const to = selectionPos.to;
- insertionTarget = { kind: "markdown", position: to };
- const contentMarkdown = markdownSource.slice(from, to).trim();
- if (to > from && contentMarkdown) selection = { kind: "markdown", from, to, contentMarkdown };
- } else if (isEditorReady(editor)) {
- insertionTarget = { kind: "rich", position: editor.state.selection.head };
- const richSelection = getRichTextAiSelectionContext(editor.state.doc, editor.state.selection);
- if (richSelection) selection = { kind: "rich", ...richSelection };
+ return source.slice(from, to).trim();
}
-
- let anchor: AiAssistantAnchor | null = null;
- if (!useMobilePlainTextEditor && !useMarkdownSourceEditor && isEditorReady(editor)) {
- try {
- const coords = editor.view.coordsAtPos(editor.state.selection.head);
- const placeAbove = coords.bottom > window.innerHeight * 0.58;
- anchor = {
- left: coords.left,
- placement: placeAbove ? "above" : "below",
- top: placeAbove ? coords.top - 8 : coords.bottom + 8,
- };
- } catch {
- anchor = null;
- }
+ if (useMarkdownSourceEditor) {
+ const selectionPos = markdownSourceEditorRef.current?.getSelection() ?? { from: 0, to: 0 };
+ return markdownSource.slice(selectionPos.from, selectionPos.to).trim();
}
- if (!anchor) {
- const fallback = useMarkdownSourceEditor
- ? markdownSourceEditorRef.current?.getSelectionCoordinates() ?? markdownSourceEditorRef.current?.getScrollContainer()?.getBoundingClientRect()
- : useMobilePlainTextEditor
- ? mobileTextAreaRef.current?.getBoundingClientRect()
- : editorScrollContainerRef.current?.getBoundingClientRect();
- anchor = {
- left: fallback?.left ?? 24,
- placement: "below",
- top: Math.min((fallback?.top ?? 72) + 48, window.innerHeight - 120),
- };
+ if (isEditorReady(editor)) {
+ return getRichTextAiSelectionContext(editor.state.doc, editor.state.selection)?.contentMarkdown.trim() ?? "";
}
+ return "";
+ }, [editor, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);
- setAiAssistantAnchor(anchor);
- setAiSelection(selection);
- setAiInsertionTarget(insertionTarget);
- setAiAssistantOpen(true);
- }, [editor, effectiveReadOnly, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);
+ const openAiAssistant = useCallback(() => {
+ if (effectiveReadOnly) return;
+ setAiSelectionText(readLiveAiSelection());
+ setAiSidebarOpen(true);
+ }, [effectiveReadOnly, readLiveAiSelection, setAiSidebarOpen]);
useEffect(() => {
openAiAssistantRef.current = openAiAssistant;
}, [openAiAssistant]);
- const handleAiAssistantOpenChange = useCallback((nextOpen: boolean) => {
- setAiAssistantOpen(nextOpen);
- if (!nextOpen) {
- setAiSelection(null);
- setAiInsertionTarget(null);
- }
- }, []);
-
- const applyAiDraft = useCallback((draft: string, mode: "append" | "replace") => {
- if (effectiveReadOnly) return false;
- if (mode === "replace" && aiSelection) {
- const replacementDraft = normalizeAiSelectionReplacement(draft);
- if (!replacementDraft) return false;
-
- if (aiSelection.kind === "plain") {
- const source = getMobilePlainTextValue();
- const { next, caret } = insertMarkdownSnippet(source, replacementDraft, aiSelection.from, aiSelection.to);
- setMobilePlainText(next);
- setMobilePlainTextElementValue(mobileTextAreaRef.current, next);
- persistCurrentDraft(title, tagsText, next);
- window.requestAnimationFrame(() => {
- const plainTextElement = mobileTextAreaRef.current;
- plainTextElement?.focus();
- if (plainTextElement instanceof HTMLTextAreaElement) plainTextElement.setSelectionRange(caret, caret);
- });
- } else if (aiSelection.kind === "markdown") {
- const { next, caret } = insertMarkdownSnippet(markdownSource, replacementDraft, aiSelection.from, aiSelection.to);
- setMarkdownSource(next);
- window.requestAnimationFrame(() => {
- markdownSourceEditorRef.current?.focus();
- markdownSourceEditorRef.current?.setSelection(caret, caret);
- });
- } else if (aiSelection.kind === "rich" && isEditorReady(editor)) {
- const maxPos = editor.state.doc.content.size;
- const { from, to } = getRichTextAiReplacementRange(aiSelection.from, aiSelection.to, maxPos);
- try {
- const applied = editor.commands.insertContentAt(
- { from, to },
- getRichTextAiSelectionReplacement(replacementDraft, aiSelection.isInline),
- );
- if (!applied) return false;
- editor.commands.focus();
- } catch {
- return false;
- }
- } else {
- return false;
- }
- markDirty();
- setAiSelection(null);
- setAiInsertionTarget(null);
- setAiAssistantOpen(false);
- return true;
- }
-
- if (mode === "append" && aiInsertionTarget) {
- const insertionDraft = draft.trim();
- if (!insertionDraft) return false;
-
- if (aiInsertionTarget.kind === "plain") {
- const source = getMobilePlainTextValue();
- const { next, caret } = insertAiDraftAtTextCursor(source, insertionDraft, aiInsertionTarget.position);
- setMobilePlainText(next);
- setMobilePlainTextElementValue(mobileTextAreaRef.current, next);
- persistCurrentDraft(title, tagsText, next);
- window.requestAnimationFrame(() => {
- const plainTextElement = mobileTextAreaRef.current;
- plainTextElement?.focus();
- if (plainTextElement instanceof HTMLTextAreaElement) plainTextElement.setSelectionRange(caret, caret);
- });
- } else if (aiInsertionTarget.kind === "markdown") {
- const { next, caret } = insertAiDraftAtTextCursor(markdownSource, insertionDraft, aiInsertionTarget.position);
- setMarkdownSource(next);
- window.requestAnimationFrame(() => {
- markdownSourceEditorRef.current?.focus();
- markdownSourceEditorRef.current?.setSelection(caret, caret);
- });
- } else if (isEditorReady(editor)) {
- const position = Math.max(0, Math.min(aiInsertionTarget.position, editor.state.doc.content.size));
- try {
- const applied = editor.commands.insertContentAt(
- position,
- getRichTextAiSelectionReplacement(insertionDraft, false),
- );
- if (!applied) return false;
- editor.commands.focus();
- } catch {
- return false;
- }
- } else {
- return false;
- }
-
- markDirty();
- setAiSelection(null);
- setAiInsertionTarget(null);
- setAiAssistantOpen(false);
- return true;
- }
-
- const current = getCurrentMarkdownForAi();
- const next = mode === "append" && current.trim()
- ? `${current.replace(/\s+$/, "")}\n\n${draft}`
- : draft;
- if (useMobilePlainTextEditor) {
- setMobilePlainText(next);
- setMobilePlainTextElementValue(mobileTextAreaRef.current, next);
- persistCurrentDraft(title, tagsText, next);
- } else if (useMarkdownSourceEditor) {
- setMarkdownSource(next);
- } else if (isEditorReady(editor)) {
- try {
- if (!editor.commands.setContent(markdownToDoc(next))) return false;
- } catch {
- return false;
- }
- } else {
- return false;
- }
- markDirty();
- setAiSelection(null);
- setAiInsertionTarget(null);
- setAiAssistantOpen(false);
- return true;
- }, [aiInsertionTarget, aiSelection, editor, effectiveReadOnly, getCurrentMarkdownForAi, getMobilePlainTextValue, markDirty, markdownSource, persistCurrentDraft, tagsText, title, useMarkdownSourceEditor, useMobilePlainTextEditor]);
+ useEffect(() => {
+ if (!aiAssistantOpen) return;
+ const refresh = () => {
+ const next = readLiveAiSelection();
+ setAiSelectionText((current) => current === next ? current : next);
+ };
+ refresh();
+ const richEditor = !useMobilePlainTextEditor && !useMarkdownSourceEditor && isEditorReady(editor) ? editor : null;
+ richEditor?.on("selectionUpdate", refresh);
+ document.addEventListener("selectionchange", refresh);
+ return () => {
+ richEditor?.off("selectionUpdate", refresh);
+ document.removeEventListener("selectionchange", refresh);
+ };
+ }, [aiAssistantOpen, editor, readLiveAiSelection, useMarkdownSourceEditor, useMobilePlainTextEditor]);
const getCurrentContentJson = useCallback((): TiptapDoc | null => {
if (useMobilePlainTextEditor) {
@@ -2916,7 +2787,6 @@ const RichEditorPane = ({
const editorShortcutBlocked = Boolean(
historyOpen ||
shareOpen ||
- aiAssistantOpen ||
mobileNotebookSheetOpen ||
noteLinkPickerOpen ||
externalLinkDialogOpen ||
@@ -3566,7 +3436,8 @@ const RichEditorPane = ({
const saveStatusLabel = `${saveLabel} · ${t("editor.characterCount", { count: characterCount })}`;
return (
- <div className="relative flex h-full min-w-0 flex-col bg-transparent">
+ <div className="relative flex h-full min-w-0 bg-transparent">
+ <div ref={editorColumnRef} className="relative flex h-full min-w-0 min-h-0 flex-1 flex-col">
{selectionActionBar}
<ExternalLinkDialog
open={externalLinkDialogOpen}
@@ -3851,17 +3722,6 @@ const RichEditorPane = ({
moreMenuClassName="w-56 rounded-md"
onOpenExecutionCenter={onOpenExecutionCenter}
onSearch={() => openNoteSearch()}
- textNoteActions={(
- <>
- {!effectiveReadOnly && (
- <IconTooltip label={`${t("aiAssistant.open")} (${formatShortcutBinding(shortcutSettings.openAiAssistant)})`}>
- <Button className="hidden h-8 w-8 text-slate-500 transition-colors hover:bg-slate-100 hover:text-slate-950 focus-visible:ring-2 focus-visible:ring-slate-300 sm:inline-flex" size="icon" variant="ghost" aria-label={t("aiAssistant.open")} onClick={openAiAssistant}>
- <Sparkles className="h-4 w-4" strokeWidth={1.75} />
- </Button>
- </IconTooltip>
- )}
- </>
- )}
textNoteMenuItems={(
<>
{!isMobileViewport && !useMobilePlainTextEditor && !useMarkdownSourceEditor && (
@@ -3889,15 +3749,6 @@ const RichEditorPane = ({
)}
{t(wechatCopyState === "copying" ? "editor.copyingToWeChat" : wechatCopyState === "copied" ? "editor.copiedToWeChat" : wechatCopyState === "error" ? "editor.copyToWeChatFailed" : "editor.copyToWeChat")}
</DropdownMenuItem>
- {!effectiveReadOnly ? (
- <DropdownMenuItem
- className="flex h-9 w-full items-center gap-2 px-3 text-left text-xs text-slate-700 hover:bg-slate-50 cursor-pointer outline-none sm:hidden"
- onClick={openAiAssistant}
- >
- <Sparkles className="h-4 w-4 text-slate-500" />
- {t("aiAssistant.title")}
- </DropdownMenuItem>
- ) : null}
</>
)}
moreMenuItems={(
@@ -4103,6 +3954,9 @@ const RichEditorPane = ({
? "w-full justify-center"
: "w-full"
)}
+ style={editorPaneTight && !useMarkdownSourceEditor
+ ? { "--editor-reading-gutter": "1.75rem" } as CSSProperties
+ : undefined}
>
<div
className={cn(
@@ -4216,7 +4070,7 @@ const RichEditorPane = ({
noteProse={noteProse}
/>
)}
- {!isMobileViewport && !useMobilePlainTextEditor && !useMarkdownSourceEditor && !phonePreviewOpen && (
+ {!editorPaneTight && !isMobileViewport && !useMobilePlainTextEditor && !useMarkdownSourceEditor && !phonePreviewOpen && (
<EditorOutline
editor={editor}
scrollContainer={editorScrollContainer}
@@ -4296,6 +4150,31 @@ const RichEditorPane = ({
</ClipboardCopyNotice>
)}
+ {!effectiveReadOnly && !aiAssistantOpen ? (
+ <IconTooltip
+ side="left"
+ label={`${t("aiAssistant.open")} (${formatShortcutBinding(shortcutSettings.openAiAssistant)})`}
+ >
+ <Button
+ type="button"
+ variant="outline"
+ size="icon"
+ data-ai-assistant-launcher=""
+ className={cn(
+ "absolute z-30 size-11 rounded-full border-slate-200 bg-card text-slate-950 shadow-[0_8px_24px_rgba(15,23,42,0.14)] hover:bg-card hover:text-slate-950",
+ // The phone note view already has an edit button in this corner.
+ isMobileViewport && !mobileEditingActive
+ ? "bottom-[calc(4.75rem+env(safe-area-inset-bottom))] right-4"
+ : "bottom-[calc(1.25rem+env(safe-area-inset-bottom))] right-5",
+ )}
+ aria-label={t("aiAssistant.open")}
+ onClick={openAiAssistant}
+ >
+ <Sparkles className="size-5" strokeWidth={1.75} />
+ </Button>
+ </IconTooltip>
+ ) : null}
+
{isMobileViewport && !mobileEditingActive && !readOnly && (
<Button
className="fixed bottom-[calc(1rem+env(safe-area-inset-bottom))] right-4 z-30 h-12 w-12 rounded-full shadow-lg sm:hidden"
@@ -4337,24 +4216,6 @@ const RichEditorPane = ({
/>
)}
- <AiAssistantDialog
- open={aiAssistantOpen}
- anchor={aiAssistantAnchor}
- title={title}
- contentMarkdown={currentMarkdownForAi}
- selectionMarkdown={aiSelection?.contentMarkdown}
- memoId={memo?.id}
- notebookId={memo?.notebookId}
- notebookTitle={notebookOptions.find((notebook) => notebook.id === memo?.notebookId)?.name}
- companionAvailable={companionAvailable}
- onOpenChange={handleAiAssistantOpenChange}
- onApply={applyAiDraft}
- onOpenPromptLibrary={onOpenAiPrompts}
- beforeCompanionApply={beforeCompanionApply}
- onCompanionNotesChanged={onCompanionNotesChanged}
- onOpenCompanionNote={onOpenCompanionNote}
- />
-
<ShareMemoDialog memoId={memo.id} open={shareOpen} onOpenChange={setShareOpen} />
{imageShareSource && (
@@ -4364,7 +4225,21 @@ const RichEditorPane = ({
onOpenChange={setImageShareOpen}
/>
)}
-
+ </div>
+ <AiSidebar
+ open={aiAssistantOpen}
+ onOpenChange={setAiSidebarOpen}
+ companionAvailable={companionAvailable}
+ selectionMarkdown={aiSelectionText}
+ contentMarkdown={currentMarkdownForAi}
+ memoId={memo.id}
+ notebookId={memo.notebookId}
+ notebookTitle={notebookOptions.find((notebook) => notebook.id === memo.notebookId)?.name}
+ noteTitle={title}
+ beforeCompanionApply={beforeCompanionApply}
+ onCompanionNotesChanged={onCompanionNotesChanged}
+ onOpenCompanionNote={onOpenCompanionNote}
+ />
</div>
);
};
diff --git a/apps/web/src/components/MemoEditorHeaderActions.test.mjs b/apps/web/src/components/MemoEditorHeaderActions.test.mjs
index 81a1bfe6..b4f0a261 100644
--- a/apps/web/src/components/MemoEditorHeaderActions.test.mjs
+++ b/apps/web/src/components/MemoEditorHeaderActions.test.mjs
@@ -25,9 +25,11 @@ describe("shared memo editor header actions", () => {
test("is reused by both editors while text-only actions remain explicit slots", () => {
expect(editorSource).toContain("<MemoEditorHeaderActions");
expect(diagramSource).toContain("<MemoEditorHeaderActions");
- expect(editorSource).toContain("textNoteActions={(\n");
expect(editorSource).toContain("textNoteMenuItems=");
+ expect(editorSource).toContain("data-ai-assistant-launcher");
+ expect(editorSource).not.toContain("textNoteActions=");
expect(diagramSource).not.toContain("textNoteActions=");
+ expect(diagramSource).not.toContain("data-ai-assistant-launcher");
expect(diagramSource).not.toContain("<WeChatIcon");
});
diff --git a/apps/web/src/components/SettingsPane.tsx b/apps/web/src/components/SettingsPane.tsx
index 8785f93d..76c6a414 100644
--- a/apps/web/src/components/SettingsPane.tsx
+++ b/apps/web/src/components/SettingsPane.tsx
@@ -37,6 +37,7 @@ import { PasswordCard } from "./settings/PasswordCard";
import { UserManagementCard } from "./settings/UserManagementCard";
import { ObjectStorageCard } from "./settings/ObjectStorageCard";
import { AiModelCard } from "./settings/AiModelCard";
+import { DesktopAcpAgentCard } from "./settings/DesktopAcpAgentCard";
import { ThemeToggle } from "./ThemeToggle";
import type { AuthUser } from "@edgeever/shared";
import { contentEnterMotion } from "@/lib/motion";
@@ -232,6 +233,7 @@ export const SettingsPane = ({
return (
<SettingsGroup>
<AiModelCard />
+ <DesktopAcpAgentCard />
</SettingsGroup>
);
case "mcp":
diff --git a/apps/web/src/components/ai-elements/attachments.tsx b/apps/web/src/components/ai-elements/attachments.tsx
new file mode 100644
index 00000000..d4e4e342
--- /dev/null
+++ b/apps/web/src/components/ai-elements/attachments.tsx
@@ -0,0 +1,426 @@
+"use client";
+
+import { Button } from "@/components/ui/button";
+import {
+ HoverCard,
+ HoverCardContent,
+ HoverCardTrigger,
+} from "@/components/ui/hover-card";
+import { cn } from "@/lib/utils";
+import type { FileUIPart, SourceDocumentUIPart } from "ai";
+import {
+ FileTextIcon,
+ GlobeIcon,
+ ImageIcon,
+ Music2Icon,
+ PaperclipIcon,
+ VideoIcon,
+ XIcon,
+} from "lucide-react";
+import type { ComponentProps, HTMLAttributes, ReactNode } from "react";
+import { createContext, useCallback, useContext, useMemo } from "react";
+
+// ============================================================================
+// Types
+// ============================================================================
+
+export type AttachmentData =
+ | (FileUIPart & { id: string })
+ | (SourceDocumentUIPart & { id: string });
+
+export type AttachmentMediaCategory =
+ | "image"
+ | "video"
+ | "audio"
+ | "document"
+ | "source"
+ | "unknown";
+
+export type AttachmentVariant = "grid" | "inline" | "list";
+
+const mediaCategoryIcons: Record<AttachmentMediaCategory, typeof ImageIcon> = {
+ audio: Music2Icon,
+ document: FileTextIcon,
+ image: ImageIcon,
+ source: GlobeIcon,
+ unknown: PaperclipIcon,
+ video: VideoIcon,
+};
+
+// ============================================================================
+// Utility Functions
+// ============================================================================
+
+export const getMediaCategory = (
+ data: AttachmentData
+): AttachmentMediaCategory => {
+ if (data.type === "source-document") {
+ return "source";
+ }
+
+ const mediaType = data.mediaType ?? "";
+
+ if (mediaType.startsWith("image/")) {
+ return "image";
+ }
+ if (mediaType.startsWith("video/")) {
+ return "video";
+ }
+ if (mediaType.startsWith("audio/")) {
+ return "audio";
+ }
+ if (mediaType.startsWith("application/") || mediaType.startsWith("text/")) {
+ return "document";
+ }
+
+ return "unknown";
+};
+
+export const getAttachmentLabel = (data: AttachmentData): string => {
+ if (data.type === "source-document") {
+ return data.title || data.filename || "Source";
+ }
+
+ const category = getMediaCategory(data);
+ return data.filename || (category === "image" ? "Image" : "Attachment");
+};
+
+const renderAttachmentImage = (
+ url: string,
+ filename: string | undefined,
+ isGrid: boolean
+) =>
+ isGrid ? (
+ <img
+ alt={filename || "Image"}
+ className="size-full object-cover"
+ height={96}
+ src={url}
+ width={96}
+ />
+ ) : (
+ <img
+ alt={filename || "Image"}
+ className="size-full rounded object-cover"
+ height={20}
+ src={url}
+ width={20}
+ />
+ );
+
+// ============================================================================
+// Contexts
+// ============================================================================
+
+interface AttachmentsContextValue {
+ variant: AttachmentVariant;
+}
+
+const AttachmentsContext = createContext<AttachmentsContextValue | null>(null);
+
+interface AttachmentContextValue {
+ data: AttachmentData;
+ mediaCategory: AttachmentMediaCategory;
+ onRemove?: () => void;
+ variant: AttachmentVariant;
+}
+
+const AttachmentContext = createContext<AttachmentContextValue | null>(null);
+
+// ============================================================================
+// Hooks
+// ============================================================================
+
+export const useAttachmentsContext = () =>
+ useContext(AttachmentsContext) ?? { variant: "grid" as const };
+
+export const useAttachmentContext = () => {
+ const ctx = useContext(AttachmentContext);
+ if (!ctx) {
+ throw new Error("Attachment components must be used within <Attachment>");
+ }
+ return ctx;
+};
+
+// ============================================================================
+// Attachments - Container
+// ============================================================================
+
+export type AttachmentsProps = HTMLAttributes<HTMLDivElement> & {
+ variant?: AttachmentVariant;
+};
+
+export const Attachments = ({
+ variant = "grid",
+ className,
+ children,
+ ...props
+}: AttachmentsProps) => {
+ const contextValue = useMemo(() => ({ variant }), [variant]);
+
+ return (
+ <AttachmentsContext.Provider value={contextValue}>
+ <div
+ className={cn(
+ "flex items-start",
+ variant === "list" ? "flex-col gap-2" : "flex-wrap gap-2",
+ variant === "grid" && "ml-auto w-fit",
+ className
+ )}
+ {...props}
+ >
+ {children}
+ </div>
+ </AttachmentsContext.Provider>
+ );
+};
+
+// ============================================================================
+// Attachment - Item
+// ============================================================================
+
+export type AttachmentProps = HTMLAttributes<HTMLDivElement> & {
+ data: AttachmentData;
+ onRemove?: () => void;
+};
+
+export const Attachment = ({
+ data,
+ onRemove,
+ className,
+ children,
+ ...props
+}: AttachmentProps) => {
+ const { variant } = useAttachmentsContext();
+ const mediaCategory = getMediaCategory(data);
+
+ const contextValue = useMemo<AttachmentContextValue>(
+ () => ({ data, mediaCategory, onRemove, variant }),
+ [data, mediaCategory, onRemove, variant]
+ );
+
+ return (
+ <AttachmentContext.Provider value={contextValue}>
+ <div
+ className={cn(
+ "group relative",
+ variant === "grid" && "size-24 overflow-hidden rounded-lg",
+ variant === "inline" && [
+ "flex h-8 cursor-pointer select-none items-center gap-1.5",
+ "rounded-md border border-border px-1.5",
+ "font-medium text-sm transition-all",
+ "hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
+ ],
+ variant === "list" && [
+ "flex w-full items-center gap-3 rounded-lg border p-3",
+ "hover:bg-accent/50",
+ ],
+ className
+ )}
+ {...props}
+ >
+ {children}
+ </div>
+ </AttachmentContext.Provider>
+ );
+};
+
+// ============================================================================
+// AttachmentPreview - Media preview
+// ============================================================================
+
+export type AttachmentPreviewProps = HTMLAttributes<HTMLDivElement> & {
+ fallbackIcon?: ReactNode;
+};
+
+export const AttachmentPreview = ({
+ fallbackIcon,
+ className,
+ ...props
+}: AttachmentPreviewProps) => {
+ const { data, mediaCategory, variant } = useAttachmentContext();
+
+ const iconSize = variant === "inline" ? "size-3" : "size-4";
+
+ const renderIcon = (Icon: typeof ImageIcon) => (
+ <Icon className={cn(iconSize, "text-muted-foreground")} />
+ );
+
+ const renderContent = () => {
+ if (mediaCategory === "image" && data.type === "file" && data.url) {
+ return renderAttachmentImage(data.url, data.filename, variant === "grid");
+ }
+
+ if (mediaCategory === "video" && data.type === "file" && data.url) {
+ return <video className="size-full object-cover" muted src={data.url} />;
+ }
+
+ const Icon = mediaCategoryIcons[mediaCategory];
+ return fallbackIcon ?? renderIcon(Icon);
+ };
+
+ return (
+ <div
+ className={cn(
+ "flex shrink-0 items-center justify-center overflow-hidden",
+ variant === "grid" && "size-full bg-muted",
+ variant === "inline" && "size-5 rounded bg-background",
+ variant === "list" && "size-12 rounded bg-muted",
+ className
+ )}
+ {...props}
+ >
+ {renderContent()}
+ </div>
+ );
+};
+
+// ============================================================================
+// AttachmentInfo - Name and type display
+// ============================================================================
+
+export type AttachmentInfoProps = HTMLAttributes<HTMLDivElement> & {
+ showMediaType?: boolean;
+};
+
+export const AttachmentInfo = ({
+ showMediaType = false,
+ className,
+ ...props
+}: AttachmentInfoProps) => {
+ const { data, variant } = useAttachmentContext();
+ const label = getAttachmentLabel(data);
+
+ if (variant === "grid") {
+ return null;
+ }
+
+ return (
+ <div className={cn("min-w-0 flex-1", className)} {...props}>
+ <span className="block truncate">{label}</span>
+ {showMediaType && data.mediaType && (
+ <span className="block truncate text-muted-foreground text-xs">
+ {data.mediaType}
+ </span>
+ )}
+ </div>
+ );
+};
+
+// ============================================================================
+// AttachmentRemove - Remove button
+// ============================================================================
+
+export type AttachmentRemoveProps = ComponentProps<typeof Button> & {
+ label?: string;
+};
+
+export const AttachmentRemove = ({
+ label = "Remove",
+ className,
+ children,
+ ...props
+}: AttachmentRemoveProps) => {
+ const { onRemove, variant } = useAttachmentContext();
+
+ const handleClick = useCallback(
+ (e: React.MouseEvent) => {
+ e.stopPropagation();
+ onRemove?.();
+ },
+ [onRemove]
+ );
+
+ if (!onRemove) {
+ return null;
+ }
+
+ return (
+ <Button
+ aria-label={label}
+ className={cn(
+ variant === "grid" && [
+ "absolute top-2 right-2 size-6 rounded-full p-0",
+ "bg-background/80 backdrop-blur-sm",
+ "opacity-0 transition-opacity group-hover:opacity-100",
+ "hover:bg-background",
+ "[&>svg]:size-3",
+ ],
+ variant === "inline" && [
+ "size-5 rounded p-0",
+ "opacity-0 transition-opacity group-hover:opacity-100",
+ "[&>svg]:size-2.5",
+ ],
+ variant === "list" && ["size-8 shrink-0 rounded p-0", "[&>svg]:size-4"],
+ className
+ )}
+ onClick={handleClick}
+ type="button"
+ variant="ghost"
+ {...props}
+ >
+ {children ?? <XIcon />}
+ <span className="sr-only">{label}</span>
+ </Button>
+ );
+};
+
+// ============================================================================
+// AttachmentHoverCard - Hover preview
+// ============================================================================
+
+export type AttachmentHoverCardProps = ComponentProps<typeof HoverCard>;
+
+export const AttachmentHoverCard = ({
+ openDelay = 0,
+ closeDelay = 0,
+ ...props
+}: AttachmentHoverCardProps) => (
+ <HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
+);
+
+export type AttachmentHoverCardTriggerProps = ComponentProps<
+ typeof HoverCardTrigger
+>;
+
+export const AttachmentHoverCardTrigger = (
+ props: AttachmentHoverCardTriggerProps
+) => <HoverCardTrigger {...props} />;
+
+export type AttachmentHoverCardContentProps = ComponentProps<
+ typeof HoverCardContent
+>;
+
+export const AttachmentHoverCardContent = ({
+ align = "start",
+ className,
+ ...props
+}: AttachmentHoverCardContentProps) => (
+ <HoverCardContent
+ align={align}
+ className={cn("w-auto p-2", className)}
+ {...props}
+ />
+);
+
+// ============================================================================
+// AttachmentEmpty - Empty state
+// ============================================================================
+
+export type AttachmentEmptyProps = HTMLAttributes<HTMLDivElement>;
+
+export const AttachmentEmpty = ({
+ className,
+ children,
+ ...props
+}: AttachmentEmptyProps) => (
+ <div
+ className={cn(
+ "flex items-center justify-center p-4 text-muted-foreground text-sm",
+ className
+ )}
+ {...props}
+ >
+ {children ?? "No attachments"}
+ </div>
+);
diff --git a/apps/web/src/components/ai-elements/prompt-input.tsx b/apps/web/src/components/ai-elements/prompt-input.tsx
new file mode 100644
index 00000000..181f8cc9
--- /dev/null
+++ b/apps/web/src/components/ai-elements/prompt-input.tsx
@@ -0,0 +1,1413 @@
+"use client";
+
+import { Button } from "@/components/ui/button";
+import {
+ Command,
+ CommandEmpty,
+ CommandGroup,
+ CommandInput,
+ CommandItem,
+ CommandList,
+ CommandSeparator,
+} from "@/components/ui/command";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu";
+import {
+ HoverCard,
+ HoverCardContent,
+ HoverCardTrigger,
+} from "@/components/ui/hover-card";
+import {
+ InputGroup,
+ InputGroupAddon,
+ InputGroupButton,
+ InputGroupTextarea,
+} from "@/components/ui/input-group";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { cn } from "@/lib/utils";
+import type { ChatStatus, FileUIPart } from "ai";
+import {
+ CornerDownLeftIcon,
+ ImageIcon,
+ Loader2Icon,
+ MicIcon,
+ PaperclipIcon,
+ PlusIcon,
+ SquareIcon,
+ XIcon,
+} from "lucide-react";
+import { nanoid } from "nanoid";
+import {
+ type ChangeEvent,
+ type ChangeEventHandler,
+ Children,
+ type ClipboardEventHandler,
+ type ComponentProps,
+ createContext,
+ type FormEvent,
+ type FormEventHandler,
+ Fragment,
+ type HTMLAttributes,
+ type KeyboardEventHandler,
+ type PropsWithChildren,
+ type ReactNode,
+ type RefObject,
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
+
+// ============================================================================
+// Provider Context & Types
+// ============================================================================
+
+export type AttachmentsContext = {
+ files: (FileUIPart & { id: string })[];
+ add: (files: File[] | FileList) => void;
+ remove: (id: string) => void;
+ clear: () => void;
+ openFileDialog: () => void;
+ fileInputRef: RefObject<HTMLInputElement | null>;
+};
+
+export type TextInputContext = {
+ value: string;
+ setInput: (v: string) => void;
+ clear: () => void;
+};
+
+export type PromptInputControllerProps = {
+ textInput: TextInputContext;
+ attachments: AttachmentsContext;
+ /** INTERNAL: Allows PromptInput to register its file textInput + "open" callback */
+ __registerFileInput: (
+ ref: RefObject<HTMLInputElement | null>,
+ open: () => void
+ ) => void;
+};
+
+const PromptInputController = createContext<PromptInputControllerProps | null>(
+ null
+);
+const ProviderAttachmentsContext = createContext<AttachmentsContext | null>(
+ null
+);
+
+export const usePromptInputController = () => {
+ const ctx = useContext(PromptInputController);
+ if (!ctx) {
+ throw new Error(
+ "Wrap your component inside <PromptInputProvider> to use usePromptInputController()."
+ );
+ }
+ return ctx;
+};
+
+// Optional variants (do NOT throw). Useful for dual-mode components.
+const useOptionalPromptInputController = () =>
+ useContext(PromptInputController);
+
+export const useProviderAttachments = () => {
+ const ctx = useContext(ProviderAttachmentsContext);
+ if (!ctx) {
+ throw new Error(
+ "Wrap your component inside <PromptInputProvider> to use useProviderAttachments()."
+ );
+ }
+ return ctx;
+};
+
+const useOptionalProviderAttachments = () =>
+ useContext(ProviderAttachmentsContext);
+
+export type PromptInputProviderProps = PropsWithChildren<{
+ initialInput?: string;
+}>;
+
+/**
+ * Optional global provider that lifts PromptInput state outside of PromptInput.
+ * If you don't use it, PromptInput stays fully self-managed.
+ */
+export function PromptInputProvider({
+ initialInput: initialTextInput = "",
+ children,
+}: PromptInputProviderProps) {
+ // ----- textInput state
+ const [textInput, setTextInput] = useState(initialTextInput);
+ const clearInput = useCallback(() => setTextInput(""), []);
+
+ // ----- attachments state (global when wrapped)
+ const [attachmentFiles, setAttachmentFiles] = useState<
+ (FileUIPart & { id: string })[]
+ >([]);
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
+ const openRef = useRef<() => void>(() => {});
+
+ const add = useCallback((files: File[] | FileList) => {
+ const incoming = Array.from(files);
+ if (incoming.length === 0) {
+ return;
+ }
+
+ setAttachmentFiles((prev) =>
+ prev.concat(
+ incoming.map((file) => ({
+ id: nanoid(),
+ type: "file" as const,
+ url: URL.createObjectURL(file),
+ mediaType: file.type,
+ filename: file.name,
+ }))
+ )
+ );
+ }, []);
+
+ const remove = useCallback((id: string) => {
+ setAttachmentFiles((prev) => {
+ const found = prev.find((f) => f.id === id);
+ if (found?.url) {
+ URL.revokeObjectURL(found.url);
+ }
+ return prev.filter((f) => f.id !== id);
+ });
+ }, []);
+
+ const clear = useCallback(() => {
+ setAttachmentFiles((prev) => {
+ for (const f of prev) {
+ if (f.url) {
+ URL.revokeObjectURL(f.url);
+ }
+ }
+ return [];
+ });
+ }, []);
+
+ // Keep a ref to attachments for cleanup on unmount (avoids stale closure)
+ const attachmentsRef = useRef(attachmentFiles);
+ attachmentsRef.current = attachmentFiles;
+
+ // Cleanup blob URLs on unmount to prevent memory leaks
+ useEffect(() => {
+ return () => {
+ for (const f of attachmentsRef.current) {
+ if (f.url) {
+ URL.revokeObjectURL(f.url);
+ }
+ }
+ };
+ }, []);
+
+ const openFileDialog = useCallback(() => {
+ openRef.current?.();
+ }, []);
+
+ const attachments = useMemo<AttachmentsContext>(
+ () => ({
+ files: attachmentFiles,
+ add,
+ remove,
+ clear,
+ openFileDialog,
+ fileInputRef,
+ }),
+ [attachmentFiles, add, remove, clear, openFileDialog]
+ );
+
+ const __registerFileInput = useCallback(
+ (ref: RefObject<HTMLInputElement | null>, open: () => void) => {
+ fileInputRef.current = ref.current;
+ openRef.current = open;
+ },
+ []
+ );
+
+ const controller = useMemo<PromptInputControllerProps>(
+ () => ({
+ textInput: {
+ value: textInput,
+ setInput: setTextInput,
+ clear: clearInput,
+ },
+ attachments,
+ __registerFileInput,
+ }),
+ [textInput, clearInput, attachments, __registerFileInput]
+ );
+
+ return (
+ <PromptInputController.Provider value={controller}>
+ <ProviderAttachmentsContext.Provider value={attachments}>
+ {children}
+ </ProviderAttachmentsContext.Provider>
+ </PromptInputController.Provider>
+ );
+}
+
+// ============================================================================
+// Component Context & Hooks
+// ============================================================================
+
+const LocalAttachmentsContext = createContext<AttachmentsContext | null>(null);
+
+export const usePromptInputAttachments = () => {
+ // Dual-mode: prefer provider if present, otherwise use local
+ const provider = useOptionalProviderAttachments();
+ const local = useContext(LocalAttachmentsContext);
+ const context = provider ?? local;
+ if (!context) {
+ throw new Error(
+ "usePromptInputAttachments must be used within a PromptInput or PromptInputProvider"
+ );
+ }
+ return context;
+};
+
+export type PromptInputAttachmentProps = HTMLAttributes<HTMLDivElement> & {
+ data: FileUIPart & { id: string };
+ className?: string;
+};
+
+export function PromptInputAttachment({
+ data,
+ className,
+ ...props
+}: PromptInputAttachmentProps) {
+ const attachments = usePromptInputAttachments();
+
+ const filename = data.filename || "";
+
+ const mediaType =
+ data.mediaType?.startsWith("image/") && data.url ? "image" : "file";
+ const isImage = mediaType === "image";
+
+ const attachmentLabel = filename || (isImage ? "Image" : "Attachment");
+
+ return (
+ <PromptInputHoverCard>
+ <HoverCardTrigger asChild>
+ <div
+ className={cn(
+ "group relative flex h-8 cursor-pointer select-none items-center gap-1.5 rounded-md border border-border px-1.5 font-medium text-sm transition-all hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
+ className
+ )}
+ key={data.id}
+ {...props}
+ >
+ <div className="relative size-5 shrink-0">
+ <div className="absolute inset-0 flex size-5 items-center justify-center overflow-hidden rounded bg-background transition-opacity group-hover:opacity-0">
+ {isImage ? (
+ <img
+ alt={filename || "attachment"}
+ className="size-5 object-cover"
+ height={20}
+ src={data.url}
+ width={20}
+ />
+ ) : (
+ <div className="flex size-5 items-center justify-center text-muted-foreground">
+ <PaperclipIcon className="size-3" />
+ </div>
+ )}
+ </div>
+ <Button
+ aria-label="Remove attachment"
+ className="absolute inset-0 size-5 cursor-pointer rounded p-0 opacity-0 transition-opacity group-hover:pointer-events-auto group-hover:opacity-100 [&>svg]:size-2.5"
+ onClick={(e) => {
+ e.stopPropagation();
+ attachments.remove(data.id);
+ }}
+ type="button"
+ variant="ghost"
+ >
+ <XIcon />
+ <span className="sr-only">Remove</span>
+ </Button>
+ </div>
+
+ <span className="flex-1 truncate">{attachmentLabel}</span>
+ </div>
+ </HoverCardTrigger>
+ <PromptInputHoverCardContent className="w-auto p-2">
+ <div className="w-auto space-y-3">
+ {isImage && (
+ <div className="flex max-h-96 w-96 items-center justify-center overflow-hidden rounded-md border">
+ <img
+ alt={filename || "attachment preview"}
+ className="max-h-full max-w-full object-contain"
+ height={384}
+ src={data.url}
+ width={448}
+ />
+ </div>
+ )}
+ <div className="flex items-center gap-2.5">
+ <div className="min-w-0 flex-1 space-y-1 px-0.5">
+ <h4 className="truncate font-semibold text-sm leading-none">
+ {filename || (isImage ? "Image" : "Attachment")}
+ </h4>
+ {data.mediaType && (
+ <p className="truncate font-mono text-muted-foreground text-xs">
+ {data.mediaType}
+ </p>
+ )}
+ </div>
+ </div>
+ </div>
+ </PromptInputHoverCardContent>
+ </PromptInputHoverCard>
+ );
+}
+
+export type PromptInputAttachmentsProps = Omit<
+ HTMLAttributes<HTMLDivElement>,
+ "children"
+> & {
+ children: (attachment: FileUIPart & { id: string }) => ReactNode;
+};
+
+export function PromptInputAttachments({
+ children,
+ className,
+ ...props
+}: PromptInputAttachmentsProps) {
+ const attachments = usePromptInputAttachments();
+
+ if (!attachments.files.length) {
+ return null;
+ }
+
+ return (
+ <div
+ className={cn("flex flex-wrap items-center gap-2 p-3 w-full", className)}
+ {...props}
+ >
+ {attachments.files.map((file) => (
+ <Fragment key={file.id}>{children(file)}</Fragment>
+ ))}
+ </div>
+ );
+}
+
+export type PromptInputActionAddAttachmentsProps = ComponentProps<
+ typeof DropdownMenuItem
+> & {
+ label?: string;
+};
+
+export const PromptInputActionAddAttachments = ({
+ label = "Add photos or files",
+ ...props
+}: PromptInputActionAddAttachmentsProps) => {
+ const attachments = usePromptInputAttachments();
+
+ return (
+ <DropdownMenuItem
+ {...props}
+ onSelect={(e) => {
+ e.preventDefault();
+ attachments.openFileDialog();
+ }}
+ >
+ <ImageIcon className="mr-2 size-4" /> {label}
+ </DropdownMenuItem>
+ );
+};
+
+export type PromptInputMessage = {
+ text: string;
+ files: FileUIPart[];
+};
+
+export type PromptInputProps = Omit<
+ HTMLAttributes<HTMLFormElement>,
+ "onSubmit" | "onError"
+> & {
+ accept?: string; // e.g., "image/*" or leave undefined for any
+ multiple?: boolean;
+ // When true, accepts drops anywhere on document. Default false (opt-in).
+ globalDrop?: boolean;
+ // Render a hidden input with given name and keep it in sync for native form posts. Default false.
+ syncHiddenInput?: boolean;
+ // Minimal constraints
+ maxFiles?: number;
+ maxFileSize?: number; // bytes
+ onError?: (err: {
+ code: "max_files" | "max_file_size" | "accept";
+ message: string;
+ }) => void;
+ onSubmit: (
+ message: PromptInputMessage,
+ event: FormEvent<HTMLFormElement>
+ ) => void | Promise<void>;
+};
+
+export const PromptInput = ({
+ className,
+ accept,
+ multiple,
+ globalDrop,
+ syncHiddenInput,
+ maxFiles,
+ maxFileSize,
+ onError,
+ onSubmit,
+ children,
+ ...props
+}: PromptInputProps) => {
+ // Try to use a provider controller if present
+ const controller = useOptionalPromptInputController();
+ const usingProvider = !!controller;
+
+ // Refs
+ const inputRef = useRef<HTMLInputElement | null>(null);
+ const formRef = useRef<HTMLFormElement | null>(null);
+
+ // ----- Local attachments (only used when no provider)
+ const [items, setItems] = useState<(FileUIPart & { id: string })[]>([]);
+ const files = usingProvider ? controller.attachments.files : items;
+
+ // Keep a ref to files for cleanup on unmount (avoids stale closure)
+ const filesRef = useRef(files);
+ filesRef.current = files;
+
+ const openFileDialogLocal = useCallback(() => {
+ inputRef.current?.click();
+ }, []);
+
+ const matchesAccept = useCallback(
+ (f: File) => {
+ if (!accept || accept.trim() === "") {
+ return true;
+ }
+
+ const patterns = accept
+ .split(",")
+ .map((s) => s.trim())
+ .filter(Boolean);
+
+ return patterns.some((pattern) => {
+ if (pattern.endsWith("/*")) {
+ const prefix = pattern.slice(0, -1); // e.g: image/* -> image/
+ return f.type.startsWith(prefix);
+ }
+ return f.type === pattern;
+ });
+ },
+ [accept]
+ );
+
+ const addLocal = useCallback(
+ (fileList: File[] | FileList) => {
+ const incoming = Array.from(fileList);
+ const accepted = incoming.filter((f) => matchesAccept(f));
+ if (incoming.length && accepted.length === 0) {
+ onError?.({
+ code: "accept",
+ message: "No files match the accepted types.",
+ });
+ return;
+ }
+ const withinSize = (f: File) =>
+ maxFileSize ? f.size <= maxFileSize : true;
+ const sized = accepted.filter(withinSize);
+ if (accepted.length > 0 && sized.length === 0) {
+ onError?.({
+ code: "max_file_size",
+ message: "All files exceed the maximum size.",
+ });
+ return;
+ }
+
+ setItems((prev) => {
+ const capacity =
+ typeof maxFiles === "number"
+ ? Math.max(0, maxFiles - prev.length)
+ : undefined;
+ const capped =
+ typeof capacity === "number" ? sized.slice(0, capacity) : sized;
+ if (typeof capacity === "number" && sized.length > capacity) {
+ onError?.({
+ code: "max_files",
+ message: "Too many files. Some were not added.",
+ });
+ }
+ const next: (FileUIPart & { id: string })[] = [];
+ for (const file of capped) {
+ next.push({
+ id: nanoid(),
+ type: "file",
+ url: URL.createObjectURL(file),
+ mediaType: file.type,
+ filename: file.name,
+ });
+ }
+ return prev.concat(next);
+ });
+ },
+ [matchesAccept, maxFiles, maxFileSize, onError]
+ );
+
+ const removeLocal = useCallback(
+ (id: string) =>
+ setItems((prev) => {
+ const found = prev.find((file) => file.id === id);
+ if (found?.url) {
+ URL.revokeObjectURL(found.url);
+ }
+ return prev.filter((file) => file.id !== id);
+ }),
+ []
+ );
+
+ const clearLocal = useCallback(
+ () =>
+ setItems((prev) => {
+ for (const file of prev) {
+ if (file.url) {
+ URL.revokeObjectURL(file.url);
+ }
+ }
+ return [];
+ }),
+ []
+ );
+
+ const add = usingProvider ? controller.attachments.add : addLocal;
+ const remove = usingProvider ? controller.attachments.remove : removeLocal;
+ const clear = usingProvider ? controller.attachments.clear : clearLocal;
+ const openFileDialog = usingProvider
+ ? controller.attachments.openFileDialog
+ : openFileDialogLocal;
+
+ // Let provider know about our hidden file input so external menus can call openFileDialog()
+ useEffect(() => {
+ if (!usingProvider) return;
+ controller.__registerFileInput(inputRef, () => inputRef.current?.click());
+ }, [usingProvider, controller]);
+
+ // Note: File input cannot be programmatically set for security reasons
+ // The syncHiddenInput prop is no longer functional
+ useEffect(() => {
+ if (syncHiddenInput && inputRef.current && files.length === 0) {
+ inputRef.current.value = "";
+ }
+ }, [files, syncHiddenInput]);
+
+ // Attach drop handlers on nearest form and document (opt-in)
+ useEffect(() => {
+ const form = formRef.current;
+ if (!form) return;
+ if (globalDrop) return // when global drop is on, let the document-level handler own drops
+
+ const onDragOver = (e: DragEvent) => {
+ if (e.dataTransfer?.types?.includes("Files")) {
+ e.preventDefault();
+ }
+ };
+ const onDrop = (e: DragEvent) => {
+ if (e.dataTransfer?.types?.includes("Files")) {
+ e.preventDefault();
+ }
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
+ add(e.dataTransfer.files);
+ }
+ };
+ form.addEventListener("dragover", onDragOver);
+ form.addEventListener("drop", onDrop);
+ return () => {
+ form.removeEventListener("dragover", onDragOver);
+ form.removeEventListener("drop", onDrop);
+ };
+ }, [add, globalDrop]);
+
+ useEffect(() => {
+ if (!globalDrop) return;
+
+ const onDragOver = (e: DragEvent) => {
+ if (e.dataTransfer?.types?.includes("Files")) {
+ e.preventDefault();
+ }
+ };
+ const onDrop = (e: DragEvent) => {
+ if (e.dataTransfer?.types?.includes("Files")) {
+ e.preventDefault();
+ }
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
+ add(e.dataTransfer.files);
+ }
+ };
+ document.addEventListener("dragover", onDragOver);
+ document.addEventListener("drop", onDrop);
+ return () => {
+ document.removeEventListener("dragover", onDragOver);
+ document.removeEventListener("drop", onDrop);
+ };
+ }, [add, globalDrop]);
+
+ useEffect(
+ () => () => {
+ if (!usingProvider) {
+ for (const f of filesRef.current) {
+ if (f.url) URL.revokeObjectURL(f.url);
+ }
+ }
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- cleanup only on unmount; filesRef always current
+ [usingProvider]
+ );
+
+ const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
+ if (event.currentTarget.files) {
+ add(event.currentTarget.files);
+ }
+ // Reset input value to allow selecting files that were previously removed
+ event.currentTarget.value = "";
+ };
+
+ const convertBlobUrlToDataUrl = async (
+ url: string
+ ): Promise<string | null> => {
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ return new Promise((resolve) => {
+ const reader = new FileReader();
+ reader.onloadend = () => resolve(reader.result as string);
+ reader.onerror = () => resolve(null);
+ reader.readAsDataURL(blob);
+ });
+ } catch {
+ return null;
+ }
+ };
+
+ const ctx = useMemo<AttachmentsContext>(
+ () => ({
+ files: files.map((item) => ({ ...item, id: item.id })),
+ add,
+ remove,
+ clear,
+ openFileDialog,
+ fileInputRef: inputRef,
+ }),
+ [files, add, remove, clear, openFileDialog]
+ );
+
+ const handleSubmit: FormEventHandler<HTMLFormElement> = (event) => {
+ event.preventDefault();
+
+ const form = event.currentTarget;
+ const text = usingProvider
+ ? controller.textInput.value
+ : (() => {
+ const formData = new FormData(form);
+ return (formData.get("message") as string) || "";
+ })();
+
+ // Reset form immediately after capturing text to avoid race condition
+ // where user input during async blob conversion would be lost
+ if (!usingProvider) {
+ form.reset();
+ }
+
+ // Convert blob URLs to data URLs asynchronously
+ Promise.all(
+ files.map(async ({ id, ...item }) => {
+ if (item.url && item.url.startsWith("blob:")) {
+ const dataUrl = await convertBlobUrlToDataUrl(item.url);
+ // If conversion failed, keep the original blob URL
+ return {
+ ...item,
+ url: dataUrl ?? item.url,
+ };
+ }
+ return item;
+ })
+ )
+ .then((convertedFiles: FileUIPart[]) => {
+ try {
+ const result = onSubmit({ text, files: convertedFiles }, event);
+
+ // Handle both sync and async onSubmit
+ if (result instanceof Promise) {
+ result
+ .then(() => {
+ clear();
+ if (usingProvider) {
+ controller.textInput.clear();
+ }
+ })
+ .catch(() => {
+ // Don't clear on error - user may want to retry
+ });
+ } else {
+ // Sync function completed without throwing, clear attachments
+ clear();
+ if (usingProvider) {
+ controller.textInput.clear();
+ }
+ }
+ } catch {
+ // Don't clear on error - user may want to retry
+ }
+ })
+ .catch(() => {
+ // Don't clear on error - user may want to retry
+ });
+ };
+
+ // Render with or without local provider
+ const inner = (
+ <>
+ <input
+ accept={accept}
+ aria-label="Upload files"
+ className="hidden"
+ multiple={multiple}
+ onChange={handleChange}
+ ref={inputRef}
+ title="Upload files"
+ type="file"
+ />
+ <form
+ className={cn("w-full", className)}
+ onSubmit={handleSubmit}
+ ref={formRef}
+ {...props}
+ >
+ <InputGroup className="overflow-hidden">{children}</InputGroup>
+ </form>
+ </>
+ );
+
+ return usingProvider ? (
+ inner
+ ) : (
+ <LocalAttachmentsContext.Provider value={ctx}>
+ {inner}
+ </LocalAttachmentsContext.Provider>
+ );
+};
+
+export type PromptInputBodyProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputBody = ({
+ className,
+ ...props
+}: PromptInputBodyProps) => (
+ <div className={cn("contents", className)} {...props} />
+);
+
+export type PromptInputTextareaProps = ComponentProps<
+ typeof InputGroupTextarea
+>;
+
+export const PromptInputTextarea = ({
+ onChange,
+ className,
+ placeholder = "What would you like to know?",
+ ...props
+}: PromptInputTextareaProps) => {
+ const controller = useOptionalPromptInputController();
+ const attachments = usePromptInputAttachments();
+ const [isComposing, setIsComposing] = useState(false);
+
+ const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
+ if (e.key === "Enter") {
+ if (isComposing || e.nativeEvent.isComposing) {
+ return;
+ }
+ if (e.shiftKey) {
+ return;
+ }
+ e.preventDefault();
+
+ // Check if the submit button is disabled before submitting
+ const form = e.currentTarget.form;
+ const submitButton = form?.querySelector(
+ 'button[type="submit"]'
+ ) as HTMLButtonElement | null;
+ if (submitButton?.disabled) {
+ return;
+ }
+
+ form?.requestSubmit();
+ }
+
+ // Remove last attachment when Backspace is pressed and textarea is empty
+ if (
+ e.key === "Backspace" &&
+ e.currentTarget.value === "" &&
+ attachments.files.length > 0
+ ) {
+ e.preventDefault();
+ const lastAttachment = attachments.files.at(-1);
+ if (lastAttachment) {
+ attachments.remove(lastAttachment.id);
+ }
+ }
+ };
+
+ const handlePaste: ClipboardEventHandler<HTMLTextAreaElement> = (event) => {
+ const items = event.clipboardData?.items;
+
+ if (!items) {
+ return;
+ }
+
+ const files: File[] = [];
+
+ for (const item of items) {
+ if (item.kind === "file") {
+ const file = item.getAsFile();
+ if (file) {
+ files.push(file);
+ }
+ }
+ }
+
+ if (files.length > 0) {
+ event.preventDefault();
+ attachments.add(files);
+ }
+ };
+
+ const controlledProps = controller
+ ? {
+ value: controller.textInput.value,
+ onChange: (e: ChangeEvent<HTMLTextAreaElement>) => {
+ controller.textInput.setInput(e.currentTarget.value);
+ onChange?.(e);
+ },
+ }
+ : {
+ onChange,
+ };
+
+ return (
+ <InputGroupTextarea
+ className={cn("field-sizing-content max-h-48 min-h-16", className)}
+ name="message"
+ onCompositionEnd={() => setIsComposing(false)}
+ onCompositionStart={() => setIsComposing(true)}
+ onKeyDown={handleKeyDown}
+ onPaste={handlePaste}
+ placeholder={placeholder}
+ {...props}
+ {...controlledProps}
+ />
+ );
+};
+
+export type PromptInputHeaderProps = Omit<
+ ComponentProps<typeof InputGroupAddon>,
+ "align"
+>;
+
+export const PromptInputHeader = ({
+ className,
+ ...props
+}: PromptInputHeaderProps) => (
+ <InputGroupAddon
+ align="block-end"
+ className={cn("order-first flex-wrap gap-1", className)}
+ {...props}
+ />
+);
+
+export type PromptInputFooterProps = Omit<
+ ComponentProps<typeof InputGroupAddon>,
+ "align"
+>;
+
+export const PromptInputFooter = ({
+ className,
+ ...props
+}: PromptInputFooterProps) => (
+ <InputGroupAddon
+ align="block-end"
+ className={cn("justify-between gap-1", className)}
+ {...props}
+ />
+);
+
+export type PromptInputToolsProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputTools = ({
+ className,
+ ...props
+}: PromptInputToolsProps) => (
+ <div className={cn("flex items-center gap-1", className)} {...props} />
+);
+
+export type PromptInputButtonProps = ComponentProps<typeof InputGroupButton>;
+
+export const PromptInputButton = ({
+ variant = "ghost",
+ className,
+ size,
+ ...props
+}: PromptInputButtonProps) => {
+ const newSize =
+ size ?? (Children.count(props.children) > 1 ? "sm" : "icon-sm");
+
+ return (
+ <InputGroupButton
+ className={cn(className)}
+ size={newSize}
+ type="button"
+ variant={variant}
+ {...props}
+ />
+ );
+};
+
+export type PromptInputActionMenuProps = ComponentProps<typeof DropdownMenu>;
+export const PromptInputActionMenu = (props: PromptInputActionMenuProps) => (
+ <DropdownMenu {...props} />
+);
+
+export type PromptInputActionMenuTriggerProps = PromptInputButtonProps;
+
+export const PromptInputActionMenuTrigger = ({
+ className,
+ children,
+ ...props
+}: PromptInputActionMenuTriggerProps) => (
+ <DropdownMenuTrigger asChild>
+ <PromptInputButton className={className} {...props}>
+ {children ?? <PlusIcon className="size-4" />}
+ </PromptInputButton>
+ </DropdownMenuTrigger>
+);
+
+export type PromptInputActionMenuContentProps = ComponentProps<
+ typeof DropdownMenuContent
+>;
+export const PromptInputActionMenuContent = ({
+ className,
+ ...props
+}: PromptInputActionMenuContentProps) => (
+ <DropdownMenuContent align="start" className={cn(className)} {...props} />
+);
+
+export type PromptInputActionMenuItemProps = ComponentProps<
+ typeof DropdownMenuItem
+>;
+export const PromptInputActionMenuItem = ({
+ className,
+ ...props
+}: PromptInputActionMenuItemProps) => (
+ <DropdownMenuItem className={cn(className)} {...props} />
+);
+
+// Note: Actions that perform side-effects (like opening a file dialog)
+// are provided in opt-in modules (e.g., prompt-input-attachments).
+
+export type PromptInputSubmitProps = ComponentProps<typeof InputGroupButton> & {
+ status?: ChatStatus;
+};
+
+export const PromptInputSubmit = ({
+ className,
+ variant = "ghost",
+ size = "icon-sm",
+ status,
+ children,
+ ...props
+}: PromptInputSubmitProps) => {
+ let Icon = <CornerDownLeftIcon className="size-4" />;
+
+ if (status === "submitted") {
+ Icon = <Loader2Icon className="size-4 animate-spin" />;
+ } else if (status === "streaming") {
+ Icon = <SquareIcon className="size-4" />;
+ } else if (status === "error") {
+ Icon = <XIcon className="size-4" />;
+ }
+
+ return (
+ <InputGroupButton
+ aria-label="Submit"
+ className={cn(className)}
+ size={size}
+ type="submit"
+ variant={variant}
+ {...props}
+ >
+ {children ?? Icon}
+ </InputGroupButton>
+ );
+};
+
+interface SpeechRecognition extends EventTarget {
+ continuous: boolean;
+ interimResults: boolean;
+ lang: string;
+ start(): void;
+ stop(): void;
+ onstart: ((this: SpeechRecognition, ev: Event) => any) | null;
+ onend: ((this: SpeechRecognition, ev: Event) => any) | null;
+ onresult:
+ | ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => any)
+ | null;
+ onerror:
+ | ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => any)
+ | null;
+}
+
+interface SpeechRecognitionEvent extends Event {
+ results: SpeechRecognitionResultList;
+ resultIndex: number;
+}
+
+type SpeechRecognitionResultList = {
+ readonly length: number;
+ item(index: number): SpeechRecognitionResult;
+ [index: number]: SpeechRecognitionResult;
+};
+
+type SpeechRecognitionResult = {
+ readonly length: number;
+ item(index: number): SpeechRecognitionAlternative;
+ [index: number]: SpeechRecognitionAlternative;
+ isFinal: boolean;
+};
+
+type SpeechRecognitionAlternative = {
+ transcript: string;
+ confidence: number;
+};
+
+interface SpeechRecognitionErrorEvent extends Event {
+ error: string;
+}
+
+declare global {
+ interface Window {
+ SpeechRecognition: {
+ new (): SpeechRecognition;
+ };
+ webkitSpeechRecognition: {
+ new (): SpeechRecognition;
+ };
+ }
+}
+
+export type PromptInputSpeechButtonProps = ComponentProps<
+ typeof PromptInputButton
+> & {
+ textareaRef?: RefObject<HTMLTextAreaElement | null>;
+ onTranscriptionChange?: (text: string) => void;
+};
+
+export const PromptInputSpeechButton = ({
+ className,
+ textareaRef,
+ onTranscriptionChange,
+ ...props
+}: PromptInputSpeechButtonProps) => {
+ const [isListening, setIsListening] = useState(false);
+ const [recognition, setRecognition] = useState<SpeechRecognition | null>(
+ null
+ );
+ const recognitionRef = useRef<SpeechRecognition | null>(null);
+
+ useEffect(() => {
+ if (
+ typeof window !== "undefined" &&
+ ("SpeechRecognition" in window || "webkitSpeechRecognition" in window)
+ ) {
+ const SpeechRecognition =
+ window.SpeechRecognition || window.webkitSpeechRecognition;
+ const speechRecognition = new SpeechRecognition();
+
+ speechRecognition.continuous = true;
+ speechRecognition.interimResults = true;
+ speechRecognition.lang = "en-US";
+
+ speechRecognition.onstart = () => {
+ setIsListening(true);
+ };
+
+ speechRecognition.onend = () => {
+ setIsListening(false);
+ };
+
+ speechRecognition.onresult = (event) => {
+ let finalTranscript = "";
+
+ for (let i = event.resultIndex; i < event.results.length; i++) {
+ const result = event.results[i];
+ if (result.isFinal) {
+ finalTranscript += result[0]?.transcript ?? "";
+ }
+ }
+
+ if (finalTranscript && textareaRef?.current) {
+ const textarea = textareaRef.current;
+ const currentValue = textarea.value;
+ const newValue =
+ currentValue + (currentValue ? " " : "") + finalTranscript;
+
+ textarea.value = newValue;
+ textarea.dispatchEvent(new Event("input", { bubbles: true }));
+ onTranscriptionChange?.(newValue);
+ }
+ };
+
+ speechRecognition.onerror = (event) => {
+ console.error("Speech recognition error:", event.error);
+ setIsListening(false);
+ };
+
+ recognitionRef.current = speechRecognition;
+ setRecognition(speechRecognition);
+ }
+
+ return () => {
+ if (recognitionRef.current) {
+ recognitionRef.current.stop();
+ }
+ };
+ }, [textareaRef, onTranscriptionChange]);
+
+ const toggleListening = useCallback(() => {
+ if (!recognition) {
+ return;
+ }
+
+ if (isListening) {
+ recognition.stop();
+ } else {
+ recognition.start();
+ }
+ }, [recognition, isListening]);
+
+ return (
+ <PromptInputButton
+ className={cn(
+ "relative transition-all duration-200",
+ isListening && "animate-pulse bg-accent text-accent-foreground",
+ className
+ )}
+ disabled={!recognition}
+ onClick={toggleListening}
+ {...props}
+ >
+ <MicIcon className="size-4" />
+ </PromptInputButton>
+ );
+};
+
+export type PromptInputSelectProps = ComponentProps<typeof Select>;
+
+export const PromptInputSelect = (props: PromptInputSelectProps) => (
+ <Select {...props} />
+);
+
+export type PromptInputSelectTriggerProps = ComponentProps<
+ typeof SelectTrigger
+>;
+
+export const PromptInputSelectTrigger = ({
+ className,
+ ...props
+}: PromptInputSelectTriggerProps) => (
+ <SelectTrigger
+ className={cn(
+ "border-none bg-transparent font-medium text-muted-foreground shadow-none transition-colors",
+ "hover:bg-accent hover:text-foreground aria-expanded:bg-accent aria-expanded:text-foreground",
+ className
+ )}
+ {...props}
+ />
+);
+
+export type PromptInputSelectContentProps = ComponentProps<
+ typeof SelectContent
+>;
+
+export const PromptInputSelectContent = ({
+ className,
+ ...props
+}: PromptInputSelectContentProps) => (
+ <SelectContent className={cn(className)} {...props} />
+);
+
+export type PromptInputSelectItemProps = ComponentProps<typeof SelectItem>;
+
+export const PromptInputSelectItem = ({
+ className,
+ ...props
+}: PromptInputSelectItemProps) => (
+ <SelectItem className={cn(className)} {...props} />
+);
+
+export type PromptInputSelectValueProps = ComponentProps<typeof SelectValue>;
+
+export const PromptInputSelectValue = ({
+ className,
+ ...props
+}: PromptInputSelectValueProps) => (
+ <SelectValue className={cn(className)} {...props} />
+);
+
+export type PromptInputHoverCardProps = ComponentProps<typeof HoverCard>;
+
+export const PromptInputHoverCard = ({
+ openDelay = 0,
+ closeDelay = 0,
+ ...props
+}: PromptInputHoverCardProps) => (
+ <HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
+);
+
+export type PromptInputHoverCardTriggerProps = ComponentProps<
+ typeof HoverCardTrigger
+>;
+
+export const PromptInputHoverCardTrigger = (
+ props: PromptInputHoverCardTriggerProps
+) => <HoverCardTrigger {...props} />;
+
+export type PromptInputHoverCardContentProps = ComponentProps<
+ typeof HoverCardContent
+>;
+
+export const PromptInputHoverCardContent = ({
+ align = "start",
+ ...props
+}: PromptInputHoverCardContentProps) => (
+ <HoverCardContent align={align} {...props} />
+);
+
+export type PromptInputTabsListProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputTabsList = ({
+ className,
+ ...props
+}: PromptInputTabsListProps) => <div className={cn(className)} {...props} />;
+
+export type PromptInputTabProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputTab = ({
+ className,
+ ...props
+}: PromptInputTabProps) => <div className={cn(className)} {...props} />;
+
+export type PromptInputTabLabelProps = HTMLAttributes<HTMLHeadingElement>;
+
+export const PromptInputTabLabel = ({
+ className,
+ ...props
+}: PromptInputTabLabelProps) => (
+ <h3
+ className={cn(
+ "mb-2 px-3 font-medium text-muted-foreground text-xs",
+ className
+ )}
+ {...props}
+ />
+);
+
+export type PromptInputTabBodyProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputTabBody = ({
+ className,
+ ...props
+}: PromptInputTabBodyProps) => (
+ <div className={cn("space-y-1", className)} {...props} />
+);
+
+export type PromptInputTabItemProps = HTMLAttributes<HTMLDivElement>;
+
+export const PromptInputTabItem = ({
+ className,
+ ...props
+}: PromptInputTabItemProps) => (
+ <div
+ className={cn(
+ "flex items-center gap-2 px-3 py-2 text-xs hover:bg-accent",
+ className
+ )}
+ {...props}
+ />
+);
+
+export type PromptInputCommandProps = ComponentProps<typeof Command>;
+
+export const PromptInputCommand = ({
+ className,
+ ...props
+}: PromptInputCommandProps) => <Command className={cn(className)} {...props} />;
+
+export type PromptInputCommandInputProps = ComponentProps<typeof CommandInput>;
+
+export const PromptInputCommandInput = ({
+ className,
+ ...props
+}: PromptInputCommandInputProps) => (
+ <CommandInput className={cn(className)} {...props} />
+);
+
+export type PromptInputCommandListProps = ComponentProps<typeof CommandList>;
+
+export const PromptInputCommandList = ({
+ className,
+ ...props
+}: PromptInputCommandListProps) => (
+ <CommandList className={cn(className)} {...props} />
+);
+
+export type PromptInputCommandEmptyProps = ComponentProps<typeof CommandEmpty>;
+
+export const PromptInputCommandEmpty = ({
+ className,
+ ...props
+}: PromptInputCommandEmptyProps) => (
+ <CommandEmpty className={cn(className)} {...props} />
+);
+
+export type PromptInputCommandGroupProps = ComponentProps<typeof CommandGroup>;
+
+export const PromptInputCommandGroup = ({
+ className,
+ ...props
+}: PromptInputCommandGroupProps) => (
+ <CommandGroup className={cn(className)} {...props} />
+);
+
+export type PromptInputCommandItemProps = ComponentProps<typeof CommandItem>;
+
+export const PromptInputCommandItem = ({
+ className,
+ ...props
+}: PromptInputCommandItemProps) => (
+ <CommandItem className={cn(className)} {...props} />
+);
+
+export type PromptInputCommandSeparatorProps = ComponentProps<
+ typeof CommandSeparator
+>;
+
+export const PromptInputCommandSeparator = ({
+ className,
+ ...props
+}: PromptInputCommandSeparatorProps) => (
+ <CommandSeparator className={cn(className)} {...props} />
+);
diff --git a/apps/web/src/components/ai-elements/reasoning.tsx b/apps/web/src/components/ai-elements/reasoning.tsx
new file mode 100644
index 00000000..5ff34bfe
--- /dev/null
+++ b/apps/web/src/components/ai-elements/reasoning.tsx
@@ -0,0 +1,180 @@
+"use client";
+
+import { useControllableState } from "@radix-ui/react-use-controllable-state";
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/components/ui/collapsible";
+import { cn } from "@/lib/utils";
+import { BrainIcon, ChevronDownIcon } from "lucide-react";
+import type { ComponentProps, ReactNode } from "react";
+import { createContext, memo, useContext, useEffect, useState } from "react";
+import { Streamdown } from "streamdown";
+import { Shimmer } from "./shimmer";
+
+type ReasoningContextValue = {
+ isStreaming: boolean;
+ isOpen: boolean;
+ setIsOpen: (open: boolean) => void;
+ duration: number | undefined;
+};
+
+const ReasoningContext = createContext<ReasoningContextValue | null>(null);
+
+export const useReasoning = () => {
+ const context = useContext(ReasoningContext);
+ if (!context) {
+ throw new Error("Reasoning components must be used within Reasoning");
+ }
+ return context;
+};
+
+export type ReasoningProps = ComponentProps<typeof Collapsible> & {
+ isStreaming?: boolean;
+ open?: boolean;
+ defaultOpen?: boolean;
+ onOpenChange?: (open: boolean) => void;
+ duration?: number;
+};
+
+const AUTO_CLOSE_DELAY = 1000;
+const MS_IN_S = 1000;
+
+export const Reasoning = memo(
+ ({
+ className,
+ isStreaming = false,
+ open,
+ defaultOpen = true,
+ onOpenChange,
+ duration: durationProp,
+ children,
+ ...props
+ }: ReasoningProps) => {
+ const [isOpen, setIsOpen] = useControllableState({
+ prop: open,
+ defaultProp: defaultOpen,
+ onChange: onOpenChange,
+ });
+ const [duration, setDuration] = useControllableState({
+ prop: durationProp,
+ defaultProp: undefined,
+ });
+
+ const [hasAutoClosed, setHasAutoClosed] = useState(false);
+ const [startTime, setStartTime] = useState<number | null>(null);
+
+ // Track duration when streaming starts and ends
+ useEffect(() => {
+ if (isStreaming) {
+ if (startTime === null) {
+ setStartTime(Date.now());
+ }
+ } else if (startTime !== null) {
+ setDuration(Math.ceil((Date.now() - startTime) / MS_IN_S));
+ setStartTime(null);
+ }
+ }, [isStreaming, startTime, setDuration]);
+
+ // Auto-open when streaming starts, auto-close when streaming ends (once only)
+ useEffect(() => {
+ if (defaultOpen && !isStreaming && isOpen && !hasAutoClosed) {
+ // Add a small delay before closing to allow user to see the content
+ const timer = setTimeout(() => {
+ setIsOpen(false);
+ setHasAutoClosed(true);
+ }, AUTO_CLOSE_DELAY);
+
+ return () => clearTimeout(timer);
+ }
+ }, [isStreaming, isOpen, defaultOpen, setIsOpen, hasAutoClosed]);
+
+ const handleOpenChange = (newOpen: boolean) => {
+ setIsOpen(newOpen);
+ };
+
+ return (
+ <ReasoningContext.Provider
+ value={{ isStreaming, isOpen, setIsOpen, duration }}
+ >
+ <Collapsible
+ className={cn("not-prose mb-4", className)}
+ onOpenChange={handleOpenChange}
+ open={isOpen}
+ {...props}
+ >
+ {children}
+ </Collapsible>
+ </ReasoningContext.Provider>
+ );
+ }
+);
+
+export type ReasoningTriggerProps = ComponentProps<typeof CollapsibleTrigger> & {
+ getThinkingMessage?: (isStreaming: boolean, duration?: number) => ReactNode;
+};
+
+const defaultGetThinkingMessage = (isStreaming: boolean, duration?: number) => {
+ if (isStreaming || duration === 0) {
+ return <Shimmer duration={1}>Thinking...</Shimmer>;
+ }
+ if (duration === undefined) {
+ return <p>Thought for a few seconds</p>;
+ }
+ return <p>Thought for {duration} seconds</p>;
+};
+
+export const ReasoningTrigger = memo(
+ ({ className, children, getThinkingMessage = defaultGetThinkingMessage, ...props }: ReasoningTriggerProps) => {
+ const { isStreaming, isOpen, duration } = useReasoning();
+
+ return (
+ <CollapsibleTrigger
+ className={cn(
+ "flex w-full items-center gap-2 text-muted-foreground text-sm transition-colors hover:text-foreground",
+ className
+ )}
+ {...props}
+ >
+ {children ?? (
+ <>
+ <BrainIcon className="size-4" />
+ {getThinkingMessage(isStreaming, duration)}
+ <ChevronDownIcon
+ className={cn(
+ "size-4 transition-transform",
+ isOpen ? "rotate-180" : "rotate-0"
+ )}
+ />
+ </>
+ )}
+ </CollapsibleTrigger>
+ );
+ }
+);
+
+export type ReasoningContentProps = ComponentProps<
+ typeof CollapsibleContent
+> & {
+ children: string;
+};
+
+export const ReasoningContent = memo(
+ ({ className, children, ...props }: ReasoningContentProps) => (
+ <CollapsibleContent
+ className={cn(
+ "mt-4 text-sm",
+ "data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 text-muted-foreground outline-none data-[state=closed]:animate-out data-[state=open]:animate-in",
+ className
+ )}
+ {...props}
+ >
+ <Streamdown>{children}</Streamdown>
+ </CollapsibleContent>
+ )
+);
+
+Reasoning.displayName = "Reasoning";
+ReasoningTrigger.displayName = "ReasoningTrigger";
+ReasoningContent.displayName = "ReasoningContent";
diff --git a/apps/web/src/components/ai-elements/shimmer.tsx b/apps/web/src/components/ai-elements/shimmer.tsx
new file mode 100644
index 00000000..9163aac4
--- /dev/null
+++ b/apps/web/src/components/ai-elements/shimmer.tsx
@@ -0,0 +1,64 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { motion } from "motion/react";
+import {
+ type CSSProperties,
+ type ElementType,
+ type JSX,
+ memo,
+ useMemo,
+} from "react";
+
+export type TextShimmerProps = {
+ children: string;
+ as?: ElementType;
+ className?: string;
+ duration?: number;
+ spread?: number;
+};
+
+const ShimmerComponent = ({
+ children,
+ as: Component = "p",
+ className,
+ duration = 2,
+ spread = 2,
+}: TextShimmerProps) => {
+ const MotionComponent = motion.create(
+ Component as keyof JSX.IntrinsicElements
+ );
+
+ const dynamicSpread = useMemo(
+ () => (children?.length ?? 0) * spread,
+ [children, spread]
+ );
+
+ return (
+ <MotionComponent
+ animate={{ backgroundPosition: "0% center" }}
+ className={cn(
+ "relative inline-block bg-[length:250%_100%,auto] bg-clip-text text-transparent",
+ "[--bg:linear-gradient(90deg,#0000_calc(50%-var(--spread)),var(--color-background),#0000_calc(50%+var(--spread)))] [background-repeat:no-repeat,padding-box]",
+ className
+ )}
+ initial={{ backgroundPosition: "100% center" }}
+ style={
+ {
+ "--spread": `${dynamicSpread}px`,
+ backgroundImage:
+ "var(--bg), linear-gradient(var(--color-muted-foreground), var(--color-muted-foreground))",
+ } as CSSProperties
+ }
+ transition={{
+ repeat: Number.POSITIVE_INFINITY,
+ duration,
+ ease: "linear",
+ }}
+ >
+ {children}
+ </MotionComponent>
+ );
+};
+
+export const Shimmer = memo(ShimmerComponent);
diff --git a/apps/web/src/components/ai-sidebar/AiSidebar.tsx b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
new file mode 100644
index 00000000..8596b6eb
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
@@ -0,0 +1,1291 @@
+import { useCallback, useEffect, useMemo, useRef, useState, type ClipboardEvent, type MouseEvent, type PointerEvent as ReactPointerEvent, type RefObject } from "react";
+import { AnimatePresence } from "motion/react";
+import * as m from "motion/react-m";
+import { useTranslation } from "react-i18next";
+import { Loader2, PanelRightClose, Paperclip, Sparkles } from "lucide-react";
+import type { CompanionAction, CompanionAnswer, CompanionEvent, CompanionTurn, CompanionTurnInput } from "@edgeever/shared";
+import { buildRevisionDiffRows, createMemoLinkHref, parseMemoLinkHref } from "@edgeever/shared";
+import { Button } from "@/components/ui/button";
+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
+import {
+ Attachment,
+ AttachmentInfo,
+ AttachmentPreview,
+ AttachmentRemove,
+ Attachments,
+} from "@/components/ai-elements/attachments";
+import { Conversation, ConversationContent, ConversationEmptyState, ConversationScrollButton } from "@/components/ai-elements/conversation";
+import { Message, MessageContent } from "@/components/ai-elements/message";
+import {
+ PromptInput,
+ PromptInputFooter,
+ PromptInputHeader,
+ PromptInputProvider,
+ PromptInputSubmit,
+ PromptInputTextarea,
+ PromptInputTools,
+ usePromptInputController,
+} from "@/components/ai-elements/prompt-input";
+import { Reasoning, ReasoningContent, ReasoningTrigger } from "@/components/ai-elements/reasoning";
+import { api, ApiRequestError } from "@/lib/api";
+import {
+ AI_ATTACHMENT_ACCEPT,
+ AiAttachmentError,
+ formatAiAttachmentSize,
+ prepareAiAttachments,
+ type PreparedAiAttachment,
+} from "@/lib/ai-attachments";
+import { companionLocale } from "@/lib/companion-locale";
+import {
+ AI_SIDEBAR_ADAPTER_KEY,
+ AI_SIDEBAR_ADAPTER_PATH_KEY,
+ AI_SIDEBAR_OPEN_KEY,
+ AI_SIDEBAR_SOURCE_KEY,
+ AI_SIDEBAR_WIDTH_KEY,
+ cancelDesktopAcp,
+ desktopAcpAvailable,
+ promptDesktopAcp,
+ subscribeDesktopAcp,
+ type DesktopAcpAdapterId,
+ type DesktopAcpEvent,
+} from "@/lib/desktop-acp";
+import { sidebarRevealTransition } from "@/lib/motion";
+import { cn } from "@/lib/utils";
+import { CompanionQuestionForm } from "../CompanionQuestionForm";
+import { AiSidebarMessage } from "./AiSidebarMessage";
+
+const SIDEBAR_DEFAULT_WIDTH = 380;
+const sidebarThreadClassName = cn(
+ "gap-3 p-3 text-[13px] leading-[1.6]",
+ "[&_[data-streamdown=heading-1]]:mt-3 [&_[data-streamdown=heading-1]]:mb-1 [&_[data-streamdown=heading-1]]:text-[15px] [&_[data-streamdown=heading-1]]:leading-snug",
+ "[&_[data-streamdown=heading-2]]:mt-3 [&_[data-streamdown=heading-2]]:mb-1 [&_[data-streamdown=heading-2]]:text-[15px] [&_[data-streamdown=heading-2]]:leading-snug",
+ "[&_[data-streamdown=heading-3]]:mt-2.5 [&_[data-streamdown=heading-3]]:mb-1 [&_[data-streamdown=heading-3]]:text-sm [&_[data-streamdown=heading-3]]:leading-snug",
+ "[&_[data-streamdown=heading-4]]:mt-2 [&_[data-streamdown=heading-4]]:mb-1 [&_[data-streamdown=heading-4]]:text-[13px]",
+ "[&_[data-streamdown=heading-5]]:mt-2 [&_[data-streamdown=heading-5]]:mb-1 [&_[data-streamdown=heading-5]]:text-[13px]",
+ "[&_[data-streamdown=heading-6]]:mt-2 [&_[data-streamdown=heading-6]]:mb-1 [&_[data-streamdown=heading-6]]:text-xs",
+ "[&_[data-streamdown=list-item]]:py-0.5",
+ "[&_[data-streamdown=blockquote]]:my-2",
+ "[&_[data-streamdown=inline-code]]:text-[12px]",
+ "[&_[data-streamdown=code-block-body]]:text-[12px] [&_[data-streamdown=code-block-body]]:leading-[1.55]",
+ "[&_[data-streamdown=code-block-body]_span]:before:text-[12px]",
+);
+const sidebarUserMessageClassName = "whitespace-pre-wrap break-words text-[13px] leading-5 group-[.is-user]:px-3 group-[.is-user]:py-2";
+const SIDEBAR_MIN_WIDTH = 320;
+const SIDEBAR_MAX_WIDTH = 560;
+const NARROW_QUERY = "(max-width: 767px)";
+
+const SKILLS = [
+ { id: "summarize", command: "/summarize" },
+ { id: "improve", command: "/improve" },
+ { id: "translate", command: "/translate" },
+] as const;
+
+type SkillId = (typeof SKILLS)[number]["id"];
+
+type PendingAttachment = PreparedAiAttachment & {
+ localId: string;
+ previewUrl: string | null;
+};
+
+type LocalToolRow = { id: string; name: string; status: string; title?: string };
+
+type LocalTurn = {
+ id: string;
+ message: string;
+ response: string;
+ reasoning: string;
+ tools: LocalToolRow[];
+ attachments: Array<{ id: string; filename: string; mediaType: string; byteLength: number }>;
+ status: "running" | "completed" | "failed" | "cancelled";
+};
+
+type ActiveTurn = {
+ id: string;
+ kind: "companion" | "acp";
+ controller: AbortController;
+ requestId?: string;
+ started: boolean;
+};
+
+type UploadedCompanionAttachment = {
+ id: string;
+ filename: string;
+ mediaType: string;
+ byteLength: number;
+ expiresAt: string;
+};
+
+export type AiSidebarFocus = {
+ memoId?: string;
+ notebookId?: string;
+ notebookTitle?: string;
+ noteTitle?: string;
+ selectionMarkdown?: string;
+ contentMarkdown?: string;
+};
+
+type AiSidebarProps = AiSidebarFocus & {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ companionAvailable: boolean;
+ beforeCompanionApply?: () => Promise<void>;
+ onCompanionNotesChanged?: () => Promise<void>;
+ onOpenCompanionNote?: (id: string, notebookId: string) => void;
+};
+
+const clampSidebarWidth = (value: number) => Math.min(SIDEBAR_MAX_WIDTH, Math.max(SIDEBAR_MIN_WIDTH, Math.round(value)));
+
+const readStorage = (key: string) => {
+ if (typeof window === "undefined") return null;
+ try {
+ return window.localStorage.getItem(key);
+ } catch {
+ return null;
+ }
+};
+
+const writeStorage = (key: string, value: string) => {
+ try {
+ window.localStorage.setItem(key, value);
+ } catch {
+ // Preference storage can be unavailable in private mode.
+ }
+};
+
+export const readAiSidebarOpen = () => readStorage(AI_SIDEBAR_OPEN_KEY) === "true";
+
+export const writeAiSidebarOpen = (open: boolean) => {
+ writeStorage(AI_SIDEBAR_OPEN_KEY, open ? "true" : "false");
+};
+
+export const readAiSidebarWidth = () => {
+ const raw = readStorage(AI_SIDEBAR_WIDTH_KEY);
+ if (!raw?.trim()) return SIDEBAR_DEFAULT_WIDTH;
+ const parsed = Number(raw);
+ return Number.isFinite(parsed) ? clampSidebarWidth(parsed) : SIDEBAR_DEFAULT_WIDTH;
+};
+
+const readAiSidebarSource = (): "builtin" | "local" => (
+ readStorage(AI_SIDEBAR_SOURCE_KEY) === "local" && desktopAcpAvailable() ? "local" : "builtin"
+);
+
+const readLocalAdapter = (): { id: DesktopAcpAdapterId; path?: string } | null => {
+ const id = readStorage(AI_SIDEBAR_ADAPTER_KEY);
+ if (id !== "codex" && id !== "antigravity") return null;
+ const path = readStorage(AI_SIDEBAR_ADAPTER_PATH_KEY)?.trim();
+ return path ? { id, path } : { id };
+};
+
+const attachmentServerCode = (code: string | undefined) => (
+ code === "companion_attachment_unsupported" || code === "companion_attachment_expired" ? code : null
+);
+
+const isAbortError = (cause: unknown) => cause instanceof DOMException
+ ? cause.name === "AbortError"
+ : cause instanceof Error && cause.name === "AbortError";
+
+const noteEditDraft = (action: CompanionAction) => {
+ if (action.plan.kind !== "tool" || action.plan.toolName !== "update_memo") return null;
+ const after = action.plan.arguments.contentMarkdown;
+ const before = action.preview?.baseContentMarkdown;
+ if (typeof after !== "string" || typeof before !== "string") return null;
+ return { before, after };
+};
+
+const emptyCompanionTurn = (
+ partial: Pick<CompanionTurn, "id" | "threadId" | "message"> & { attachments?: CompanionTurn["attachments"] },
+): CompanionTurn => ({
+ ...partial,
+ response: "",
+ process: "",
+ status: "running",
+ sources: [],
+ tools: [],
+ todos: [],
+ questions: [],
+ mentions: [],
+ model: "",
+ inputTokens: null,
+ outputTokens: null,
+ createdAt: new Date().toISOString(),
+});
+
+const uploadCompanionAttachment = (attachment: { filename: string; mediaType: string; base64Data: string }) => {
+ const client = api as typeof api & {
+ uploadCompanionAttachment: (input: { filename: string; mediaType: string; base64Data: string }) => Promise<{ attachment: UploadedCompanionAttachment }>;
+ };
+ return client.uploadCompanionAttachment(attachment);
+};
+
+const matchSkill = (text: string) => {
+ const trimmed = text.trim();
+ const lower = trimmed.toLowerCase();
+ const skill = SKILLS.find((item) => lower === item.command || lower.startsWith(`${item.command} `));
+ if (!skill) return null;
+ return { id: skill.id, rest: trimmed.slice(skill.command.length).trim() };
+};
+
+const mergeReasoning = (previous: string, next: string) => {
+ if (!next) return previous;
+ if (!previous || next.startsWith(previous)) return next;
+ if (previous.endsWith(next)) return previous;
+ return previous + next;
+};
+
+const linkedCompanionText = (text: string, turn: CompanionTurn) => text.replace(/\[note:([^\]]+)\]/g, (_match, id: string) => {
+ const source = turn.sources.find((item) => item.id === id);
+ const label = (source?.title || id).replace(/[[\]]/g, "");
+ return `[${label}](${createMemoLinkHref(id)})`;
+});
+
+const attachmentFileData = (attachment: { id: string; filename: string; mediaType: string; url?: string }) => ({
+ id: attachment.id,
+ type: "file" as const,
+ url: attachment.url ?? "",
+ filename: attachment.filename,
+ mediaType: attachment.mediaType,
+});
+
+function AttachmentChips({
+ items,
+ onRemove,
+}: {
+ items: Array<{ id: string; filename: string; mediaType: string; byteLength?: number; url?: string }>;
+ onRemove?: (id: string) => void;
+}) {
+ const { t } = useTranslation();
+ if (!items.length) return null;
+ return (
+ <Attachments variant="inline" className="ml-0 w-full">
+ {items.map((item) => (
+ <Attachment
+ key={item.id}
+ data={attachmentFileData(item)}
+ onRemove={onRemove ? () => onRemove(item.id) : undefined}
+ >
+ <AttachmentPreview />
+ <AttachmentInfo />
+ {typeof item.byteLength === "number" ? (
+ <span className="shrink-0 text-xs text-slate-400">{formatAiAttachmentSize(item.byteLength)}</span>
+ ) : null}
+ <AttachmentRemove label={t("aiAssistant.removeAttachment", { name: item.filename })} />
+ </Attachment>
+ ))}
+ </Attachments>
+ );
+}
+
+function NoteEditDiff({ before, after }: { before: string; after: string }) {
+ const rows = useMemo(() => buildRevisionDiffRows(before, after), [before, after]);
+ return (
+ <div className="max-h-52 overflow-auto rounded-md border border-slate-200 bg-card">
+ {rows.leftRows.map((left, index) => {
+ const right = rows.rightRows[index];
+ return (
+ <div key={`${left.lineNumber ?? "l"}-${index}`} className="grid grid-cols-2 divide-x divide-slate-200 font-mono text-[11px] leading-4">
+ <div className={cn(
+ "whitespace-pre-wrap break-words px-1.5 py-0.5",
+ left.state === "changed" && "bg-rose-50 text-rose-950",
+ left.state === "empty" && "bg-slate-50 text-transparent",
+ left.state === "same" && "text-slate-600",
+ )}>{left.text || " "}</div>
+ <div className={cn(
+ "whitespace-pre-wrap break-words px-1.5 py-0.5",
+ right?.state === "changed" && "bg-slate-100 text-slate-950",
+ right?.state === "empty" && "bg-slate-50 text-transparent",
+ right?.state === "same" && "text-slate-600",
+ )}>{right?.text || " "}</div>
+ </div>
+ );
+ })}
+ </div>
+ );
+}
+
+function SidebarComposer({
+ attachments,
+ attachmentError,
+ busy,
+ locked,
+ placeholder,
+ onAddFiles,
+ onRemoveAttachment,
+ onLaunch,
+ onStop,
+ skillPrompt,
+}: {
+ attachments: PendingAttachment[];
+ attachmentError: string | null;
+ busy: boolean;
+ locked: boolean;
+ placeholder: string;
+ onAddFiles: (files: File[]) => void;
+ onRemoveAttachment: (id: string) => void;
+ onLaunch: (message: string) => Promise<void>;
+ onStop: () => void;
+ skillPrompt: (id: SkillId, rest?: string) => string;
+}) {
+ const { t } = useTranslation();
+ const { textInput } = usePromptInputController();
+ const fileInputRef = useRef<HTMLInputElement>(null);
+ const draft = textInput.value;
+ const token = draft.trimStart().split(/\s/, 1)[0]?.toLowerCase() ?? "";
+ const slashOpen = token.startsWith("/") && !draft.trimStart().includes("\n");
+ const slashMatches = slashOpen ? SKILLS.filter((skill) => skill.command.startsWith(token)) : [];
+
+ const launch = async (message: string, restoreDraft?: string) => {
+ try {
+ await onLaunch(message);
+ if (restoreDraft !== undefined) textInput.clear();
+ } catch (cause) {
+ if (restoreDraft !== undefined && !isAbortError(cause)) textInput.setInput(restoreDraft);
+ throw cause;
+ }
+ };
+
+ const onPaste = (event: ClipboardEvent<HTMLTextAreaElement>) => {
+ const files = Array.from(event.clipboardData?.files ?? []);
+ if (!files.length) return;
+ event.preventDefault();
+ onAddFiles(files);
+ };
+
+ return (
+ <div className="space-y-2">
+ {slashMatches.length ? (
+ <ul className="overflow-hidden rounded-md border border-slate-200 bg-card text-sm">
+ {slashMatches.map((skill) => (
+ <li key={skill.id}>
+ <button
+ type="button"
+ className="flex w-full items-center justify-between gap-3 px-3 py-2 text-left hover:bg-slate-50"
+ onClick={() => void launch(skillPrompt(skill.id), draft).catch(() => undefined)}
+ >
+ <span className="font-medium text-slate-800">{t(`aiAssistant.sidebar.skills.${skill.id}`)}</span>
+ <span className="truncate text-xs text-slate-400">{skill.command}</span>
+ </button>
+ </li>
+ ))}
+ </ul>
+ ) : null}
+ <PromptInput
+ accept={AI_ATTACHMENT_ACCEPT}
+ multiple
+ onSubmit={async ({ text }) => {
+ const raw = text.trim();
+ if (!raw) throw new Error("empty");
+ const skill = matchSkill(raw);
+ await launch(skill ? skillPrompt(skill.id, skill.rest) : raw);
+ }}
+ >
+ {attachments.length ? (
+ <PromptInputHeader>
+ <AttachmentChips
+ items={attachments.map((item) => ({
+ id: item.localId,
+ filename: item.filename,
+ mediaType: item.mediaType,
+ byteLength: item.byteLength,
+ url: item.previewUrl ?? undefined,
+ }))}
+ onRemove={onRemoveAttachment}
+ />
+ </PromptInputHeader>
+ ) : null}
+ <PromptInputTextarea
+ className="text-[13px] leading-5 md:text-[13px]"
+ disabled={busy || locked}
+ placeholder={placeholder}
+ onPaste={onPaste}
+ />
+ <PromptInputFooter>
+ <PromptInputTools>
+ <Button
+ type="button"
+ size="icon-sm"
+ variant="ghost"
+ title={t("aiAssistant.addAttachment")}
+ aria-label={t("aiAssistant.addAttachment")}
+ disabled={busy || locked}
+ onClick={() => fileInputRef.current?.click()}
+ >
+ <Paperclip className="h-4 w-4" />
+ </Button>
+ </PromptInputTools>
+ {busy ? (
+ <Button type="button" size="sm" variant="outline" onClick={onStop}>
+ {t("aiAssistant.sidebar.stop")}
+ </Button>
+ ) : (
+ <PromptInputSubmit aria-label={t("companion.send")} disabled={locked || !draft.trim()} variant="solid" />
+ )}
+ </PromptInputFooter>
+ </PromptInput>
+ <input
+ ref={fileInputRef}
+ className="hidden"
+ type="file"
+ multiple
+ accept={AI_ATTACHMENT_ACCEPT}
+ aria-label={t("aiAssistant.addAttachment")}
+ onChange={(event) => {
+ const files = Array.from(event.target.files ?? []);
+ event.target.value = "";
+ if (files.length) onAddFiles(files);
+ }}
+ />
+ {attachmentError ? <p role="alert" className="text-xs font-medium text-rose-700">{attachmentError}</p> : null}
+ </div>
+ );
+}
+
+// The editor hides this session with CSS. Closing must not unmount it or abort a turn.
+function AiSidebarSession({
+ open,
+ onOpenChange,
+ companionAvailable,
+ selectionMarkdown,
+ contentMarkdown,
+ memoId,
+ notebookId,
+ notebookTitle,
+ noteTitle,
+ beforeCompanionApply,
+ onCompanionNotesChanged,
+ onOpenCompanionNote,
+ addFilesRef,
+ onStopReady,
+}: AiSidebarProps & {
+ addFilesRef: RefObject<(files: File[]) => void>;
+ onStopReady: RefObject<() => void>;
+}) {
+ const { t, i18n } = useTranslation();
+ const [source, setSource] = useState<"builtin" | "local">(readAiSidebarSource);
+ const [turns, setTurns] = useState<CompanionTurn[]>([]);
+ const [actions, setActions] = useState<CompanionAction[]>([]);
+ const [localTurns, setLocalTurns] = useState<LocalTurn[]>([]);
+ const [threadId, setThreadId] = useState<string>(() => crypto.randomUUID());
+ const [attachments, setAttachments] = useState<PendingAttachment[]>([]);
+ const [attachmentError, setAttachmentError] = useState<string | null>(null);
+ const [conflicts, setConflicts] = useState<Record<string, string>>({});
+ const [busy, setBusy] = useState(false);
+ const [acting, setActing] = useState(false);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState<string | null>(null);
+ const [useMemory, setUseMemory] = useState(false);
+ const alive = useRef(true);
+ const locked = useRef(false);
+ const threadPinned = useRef(false);
+ const active = useRef<ActiveTurn | null>(null);
+ const attachmentSnapshot = useRef<PendingAttachment[]>([]);
+ const acpTurnIds = useRef(new Map<string, string>());
+ const acpBuffer = useRef<DesktopAcpEvent[]>([]);
+ const focusRef = useRef({ selectionMarkdown, contentMarkdown, memoId, notebookId, notebookTitle, noteTitle, companionAvailable });
+ focusRef.current = { selectionMarkdown, contentMarkdown, memoId, notebookId, notebookTitle, noteTitle, companionAvailable };
+
+ const explainError = useCallback((cause: unknown) => {
+ const code = cause instanceof ApiRequestError ? cause.code : "";
+ if (code === "companion_attachment_unsupported") return t("aiAssistant.sidebar.attachmentUnsupported");
+ if (code === "companion_attachment_expired") return t("aiAssistant.sidebar.attachmentExpired");
+ if (code === "ai_not_configured") return t("companion.configureModel");
+ if (code === "ai_credentials_rejected") return t("companion.credentialsRejected");
+ if (code === "ai_provider_payment_required") return t("companion.providerPaymentRequired");
+ if (code === "ai_provider_rate_limited") return t("companion.providerRateLimited");
+ if (code === "ai_provider_request_rejected") return t("companion.providerRequestRejected");
+ if (code === "companion_memory_conflict") return t("companion.conflict");
+ if (code === "companion_history_full") return t("companion.historyFull");
+ if (code === "companion_action_conflict") return t("aiAssistant.sidebar.proposalConflict");
+ if (code === "companion_action_unsynced") return t("companion.actions.unsynced");
+ if (code === "companion_busy") return t("companion.recovered");
+ if (cause instanceof ApiRequestError && cause.status === 403) return t("aiAssistant.sidebar.unavailable");
+ return t("companion.failed");
+ }, [t]);
+
+ const restoreAttachments = useCallback((snapshot: PendingAttachment[]) => {
+ setAttachments((current) => current.length > 0 ? current : snapshot);
+ }, []);
+
+ const reloadCompanion = useCallback(async () => {
+ const [turnResult, actionResult] = await Promise.all([
+ api.listCompanionTurns(),
+ api.listCompanionActions(),
+ ]);
+ if (!alive.current) return;
+ setTurns(turnResult.turns);
+ setActions(actionResult.actions);
+ setThreadId((current) => threadPinned.current ? current : (turnResult.turns[0]?.threadId ?? current));
+ }, []);
+
+ useEffect(() => {
+ alive.current = true;
+ return () => {
+ alive.current = false;
+ const current = active.current;
+ current?.controller.abort();
+ if (current?.kind === "acp" && current.requestId) void cancelDesktopAcp(current.requestId).catch(() => undefined);
+ };
+ }, []);
+
+ const applyAcpEventRef = useRef<(event: DesktopAcpEvent) => void>(() => undefined);
+ useEffect(() => subscribeDesktopAcp((event) => {
+ if (!acpTurnIds.current.has(event.requestId)) {
+ acpBuffer.current.push(event);
+ return;
+ }
+ applyAcpEventRef.current(event);
+ }), []);
+
+ applyAcpEventRef.current = (event) => {
+ const turnId = acpTurnIds.current.get(event.requestId);
+ if (!turnId || !alive.current) return;
+ if (event.type === "text-delta") {
+ setLocalTurns((previous) => previous.map((turn) => turn.id === turnId && turn.status !== "cancelled"
+ ? { ...turn, response: turn.response + event.text }
+ : turn));
+ return;
+ }
+ if (event.type === "reasoning") {
+ setLocalTurns((previous) => previous.map((turn) => turn.id === turnId && turn.status !== "cancelled"
+ ? { ...turn, reasoning: mergeReasoning(turn.reasoning, event.text) }
+ : turn));
+ return;
+ }
+ if (event.type === "tool") {
+ setLocalTurns((previous) => previous.map((turn) => {
+ if (turn.id !== turnId || turn.status === "cancelled") return turn;
+ const row = { id: crypto.randomUUID(), name: event.name, status: event.status, title: event.title };
+ let index = -1;
+ for (let toolIndex = turn.tools.length - 1; toolIndex >= 0; toolIndex -= 1) {
+ const item = turn.tools[toolIndex];
+ if (item.name === event.name && item.title === event.title && item.status !== "done") {
+ index = toolIndex;
+ break;
+ }
+ }
+ if (index < 0) return { ...turn, tools: [...turn.tools, row] };
+ const tools = turn.tools.slice();
+ tools[index] = { ...tools[index], status: event.status, title: event.title };
+ return { ...turn, tools };
+ }));
+ return;
+ }
+ if (event.type === "error" || event.type === "done") {
+ setLocalTurns((previous) => previous.map((turn) => turn.id === turnId && turn.status === "running"
+ ? { ...turn, status: event.type === "error" ? "failed" : "completed" }
+ : turn));
+ if (event.type === "error" && event.message) setError(event.message);
+ if (active.current?.requestId === event.requestId) {
+ active.current = null;
+ locked.current = false;
+ setBusy(false);
+ }
+ }
+ };
+
+ useEffect(() => {
+ const syncSource = () => setSource(readAiSidebarSource());
+ syncSource();
+ if (!open) return;
+ window.addEventListener("focus", syncSource);
+ window.addEventListener("storage", syncSource);
+ return () => {
+ window.removeEventListener("focus", syncSource);
+ window.removeEventListener("storage", syncSource);
+ };
+ }, [open]);
+
+ useEffect(() => {
+ if (!companionAvailable || source === "local") {
+ setLoading(false);
+ return;
+ }
+ let cancelled = false;
+ setLoading(true);
+ void Promise.all([
+ reloadCompanion(),
+ api.getCompanionDiscoverySettings().then((result) => {
+ if (!cancelled && alive.current) setUseMemory(result.settings.useMemory === true);
+ }),
+ ]).catch((cause) => {
+ if (!cancelled && alive.current) setError(explainError(cause));
+ }).finally(() => {
+ if (!cancelled && alive.current) setLoading(false);
+ });
+ return () => { cancelled = true; };
+ }, [companionAvailable, explainError, reloadCompanion, source]);
+
+ const applyStreamEvent = useCallback((id: string, event: CompanionEvent) => {
+ if (!alive.current) return;
+ if (event.type === "text-delta") {
+ setTurns((previous) => previous.map((turn) => turn.id === id ? { ...turn, response: turn.response + event.text } : turn));
+ }
+ if (event.type === "process") {
+ setTurns((previous) => previous.map((turn) => turn.id === id ? { ...turn, process: event.text, response: "" } : turn));
+ }
+ if (event.type === "tools") {
+ setTurns((previous) => previous.map((turn) => turn.id === id ? { ...turn, tools: event.tools, todos: event.todos, questions: event.questions } : turn));
+ }
+ if (event.type === "done") setTurns((previous) => previous.map((turn) => turn.id === id ? event.turn : turn));
+ if (event.type === "error") {
+ setError(explainError(new ApiRequestError(event.code, 500, event.code)));
+ if (attachmentServerCode(event.code)) restoreAttachments(attachmentSnapshot.current);
+ }
+ }, [explainError, restoreAttachments]);
+
+ const recoverTurn = useCallback(async (id: string) => {
+ try {
+ const { turn } = await api.getCompanionTurn(id);
+ if (alive.current) setTurns((previous) => previous.map((item) => item.id === id ? turn : item));
+ } catch {
+ if (alive.current) {
+ setTurns((previous) => previous.map((item) => item.id === id && item.status === "running" ? { ...item, status: "failed" } : item));
+ }
+ }
+ if (alive.current) await reloadCompanion().catch(() => undefined);
+ await onCompanionNotesChanged?.()?.catch(() => undefined);
+ }, [onCompanionNotesChanged, reloadCompanion]);
+
+ const buildFocus = useCallback((): CompanionTurnInput["focus"] => {
+ const focus = focusRef.current;
+ const selection = focus.selectionMarkdown?.trim().slice(0, 2000) ?? "";
+ const content = focus.contentMarkdown?.trim() ?? "";
+ const excerpt = content.slice(0, 4000);
+ const next: NonNullable<CompanionTurnInput["focus"]> = {};
+ if (focus.memoId) next.memoId = focus.memoId;
+ if (focus.notebookId) next.notebookId = focus.notebookId;
+ if (focus.notebookTitle) next.notebookTitle = focus.notebookTitle;
+ if (focus.noteTitle?.trim()) next.title = focus.noteTitle.trim();
+ if (selection) next.selectionMarkdown = selection;
+ if (excerpt) {
+ next.contentMarkdown = excerpt;
+ if (content.length > 4000) next.contentTruncated = true;
+ }
+ return Object.keys(next).length ? next : undefined;
+ }, []);
+
+ const contextText = useCallback(() => {
+ const focus = focusRef.current;
+ const selection = focus.selectionMarkdown?.trim().slice(0, 2000) ?? "";
+ const excerpt = focus.contentMarkdown?.trim().slice(0, 2000) ?? "";
+ return [selection, excerpt].filter(Boolean).join("\n\n");
+ }, []);
+
+ const skillPrompt = useCallback((id: SkillId, rest = "") => {
+ const prompt = t(`aiAssistant.sidebar.skillPrompts.${id}`);
+ return rest ? `${prompt}\n\n${rest}` : prompt;
+ }, [t]);
+
+ const stop = useCallback(async () => {
+ const current = active.current;
+ if (!current) {
+ const runningTurn = turns.find((turn) => turn.threadId === threadId && turn.status === "running");
+ if (!runningTurn || readAiSidebarSource() === "local") return;
+ try { await api.cancelCompanionTurn(runningTurn.id); }
+ catch (cause) { if (alive.current) setError(explainError(cause)); }
+ await reloadCompanion().catch(() => undefined);
+ return;
+ }
+ current.controller.abort();
+ if (current.kind === "acp") {
+ setLocalTurns((previous) => previous.map((turn) => turn.id === current.id && turn.status === "running"
+ ? { ...turn, status: "cancelled" }
+ : turn));
+ if (!current.requestId) return;
+ await cancelDesktopAcp(current.requestId).catch(() => undefined);
+ active.current = null;
+ locked.current = false;
+ setBusy(false);
+ return;
+ }
+ if (current.started) {
+ try { await api.cancelCompanionTurn(current.id); }
+ catch (cause) { if (alive.current) setError(explainError(cause)); }
+ }
+ }, [explainError, reloadCompanion, threadId, turns]);
+
+ useEffect(() => {
+ onStopReady.current = () => { void stop(); };
+ }, [onStopReady, stop]);
+
+ const launch = useCallback(async (message: string) => {
+ const text = message.trim();
+ if (!text || locked.current) throw new Error("busy");
+ const mode = readAiSidebarSource();
+ setSource(mode);
+ if (mode === "builtin" && !focusRef.current.companionAvailable) {
+ setError(t("aiAssistant.sidebar.unavailable"));
+ throw new Error("unavailable");
+ }
+ const controller = new AbortController();
+ const id = crypto.randomUUID();
+ const snapshot = attachments;
+ attachmentSnapshot.current = snapshot;
+ locked.current = true;
+ setBusy(true);
+ setError(null);
+ setAttachmentError(null);
+ active.current = { id, kind: mode === "local" ? "acp" : "companion", controller, started: false };
+ try {
+ if (mode === "local") {
+ const adapter = readLocalAdapter();
+ if (!adapter) {
+ setError(t("aiAssistant.sidebar.localMissing"));
+ throw new Error("local-missing");
+ }
+ if (controller.signal.aborted) throw new DOMException("aborted", "AbortError");
+ const localAttachments = snapshot.map((item) => ({
+ id: item.localId,
+ filename: item.filename,
+ mediaType: item.mediaType,
+ byteLength: item.byteLength,
+ }));
+ setLocalTurns((previous) => [...previous, {
+ id,
+ message: text,
+ response: "",
+ reasoning: "",
+ tools: [],
+ attachments: localAttachments,
+ status: "running",
+ }]);
+ const result = await promptDesktopAcp({
+ adapterId: adapter.id,
+ ...(adapter.path ? { path: adapter.path } : {}),
+ prompt: text,
+ ...(contextText() ? { contextText: contextText() } : {}),
+ ...(snapshot.length ? {
+ attachments: snapshot.map((item) => ({
+ filename: item.filename,
+ mediaType: item.mediaType,
+ dataBase64: item.base64Data,
+ })),
+ } : {}),
+ });
+ if (controller.signal.aborted) {
+ await cancelDesktopAcp(result.requestId).catch(() => undefined);
+ setLocalTurns((previous) => previous.map((turn) => turn.id === id ? { ...turn, status: "cancelled" } : turn));
+ throw new DOMException("aborted", "AbortError");
+ }
+ const rejected = new Set((result.rejectedAttachments ?? []).map((item) => item.filename));
+ if (rejected.size) {
+ setAttachments(snapshot.filter((item) => rejected.has(item.filename)));
+ setError(t("aiAssistant.sidebar.localRejected", { names: [...rejected].join(", ") }));
+ } else {
+ setAttachments([]);
+ }
+ acpTurnIds.current.set(result.requestId, id);
+ if (active.current?.id === id) active.current.requestId = result.requestId;
+ active.current = active.current?.id === id ? { ...active.current, requestId: result.requestId, started: true } : active.current;
+ const queued = acpBuffer.current.filter((event) => event.requestId === result.requestId);
+ acpBuffer.current = acpBuffer.current.filter((event) => event.requestId !== result.requestId);
+ for (const event of queued) applyAcpEventRef.current(event);
+ return;
+ }
+
+ const uploaded: UploadedCompanionAttachment[] = [];
+ threadPinned.current = true;
+ const activeThreadId = threadId;
+ for (const item of snapshot) {
+ if (controller.signal.aborted) throw new DOMException("aborted", "AbortError");
+ const result = await uploadCompanionAttachment({
+ filename: item.filename,
+ mediaType: item.mediaType,
+ base64Data: item.base64Data,
+ });
+ if (controller.signal.aborted) throw new DOMException("aborted", "AbortError");
+ uploaded.push(result.attachment);
+ }
+ if (controller.signal.aborted) throw new DOMException("aborted", "AbortError");
+ setAttachments([]);
+ setTurns((previous) => [{
+ ...emptyCompanionTurn({ id, threadId: activeThreadId, message: text }),
+ attachments: uploaded.map((item) => ({
+ id: item.id,
+ filename: item.filename,
+ mediaType: item.mediaType,
+ byteLength: item.byteLength,
+ })),
+ }, ...previous]);
+ if (active.current?.id === id) active.current.started = true;
+ const payload: CompanionTurnInput = {
+ id,
+ threadId: activeThreadId,
+ message: text,
+ useMemory,
+ allowNotes: true,
+ allowWrites: true,
+ locale: companionLocale(i18n.resolvedLanguage),
+ ...(uploaded.length ? { attachmentIds: uploaded.map((item) => item.id) } : {}),
+ ...(buildFocus() ? { focus: buildFocus() } : {}),
+ };
+ void (async () => {
+ let completed = false;
+ try {
+ await api.streamCompanion(payload, {
+ signal: controller.signal,
+ onEvent: (event) => {
+ if (event.type === "done") completed = true;
+ applyStreamEvent(id, event);
+ },
+ });
+ } catch (cause) {
+ if (attachmentServerCode(cause instanceof ApiRequestError ? cause.code : undefined)) {
+ restoreAttachments(snapshot);
+ }
+ if (alive.current && !controller.signal.aborted && !completed) setError(explainError(cause));
+ } finally {
+ if (active.current?.id === id) active.current = null;
+ locked.current = false;
+ if (alive.current) {
+ setBusy(false);
+ await recoverTurn(id);
+ }
+ }
+ })();
+ } catch (cause) {
+ if (active.current?.id === id) active.current = null;
+ locked.current = false;
+ setBusy(false);
+ if (!isAbortError(cause)) {
+ setLocalTurns((previous) => previous.map((turn) => turn.id === id && turn.status === "running" ? { ...turn, status: "failed" } : turn));
+ }
+ if (isAbortError(cause)) throw cause;
+ const code = cause instanceof ApiRequestError ? attachmentServerCode(cause.code) : null;
+ if (code) {
+ setError(explainError(cause));
+ throw cause;
+ }
+ if (cause instanceof Error && cause.message === "desktop_acp_unavailable") {
+ setError(t("aiAssistant.sidebar.localMissing"));
+ throw cause;
+ }
+ if (cause instanceof Error && (cause.message === "unavailable" || cause.message === "local-missing")) throw cause;
+ setError(explainError(cause));
+ throw cause;
+ }
+ }, [applyStreamEvent, attachments, buildFocus, contextText, explainError, i18n.resolvedLanguage, recoverTurn, restoreAttachments, t, threadId, useMemory]);
+
+ const addFiles = useCallback(async (files: File[]) => {
+ if (!files.length) return;
+ let existing: PreparedAiAttachment[] = attachments;
+ const added: PendingAttachment[] = [];
+ let nextError: string | null = null;
+ for (const file of files) {
+ try {
+ const [prepared] = await prepareAiAttachments([file], existing);
+ if (!prepared) continue;
+ existing = [...existing, prepared];
+ added.push({
+ ...prepared,
+ localId: crypto.randomUUID(),
+ previewUrl: prepared.mediaType.startsWith("image/") ? `data:${prepared.mediaType};base64,${prepared.base64Data}` : null,
+ });
+ } catch (cause) {
+ const code = cause instanceof AiAttachmentError ? cause.code : "readFailed";
+ nextError = t(`aiAssistant.attachmentErrors.${code}`);
+ }
+ }
+ if (added.length) setAttachments((current) => [...current, ...added]);
+ setAttachmentError(nextError);
+ }, [attachments, t]);
+
+ useEffect(() => {
+ addFilesRef.current = (files) => { void addFiles(files); };
+ }, [addFiles, addFilesRef]);
+
+ const resume = (turn: CompanionTurn, answers?: CompanionAnswer[]) => {
+ if (locked.current) return;
+ const controller = new AbortController();
+ locked.current = true;
+ setBusy(true);
+ setError(null);
+ active.current = { id: turn.id, kind: "companion", controller, started: true };
+ setTurns((previous) => previous.map((item) => item.id === turn.id ? { ...item, status: "running" } : item));
+ void (async () => {
+ let completed = false;
+ try {
+ await api.resumeCompanionTurn(turn.id, answers ? { answers } : {}, {
+ signal: controller.signal,
+ onEvent: (event) => {
+ if (event.type === "done") completed = true;
+ applyStreamEvent(turn.id, event);
+ },
+ });
+ } catch (cause) {
+ if (alive.current && !controller.signal.aborted && !completed) setError(explainError(cause));
+ } finally {
+ if (active.current?.id === turn.id) active.current = null;
+ locked.current = false;
+ if (alive.current) setBusy(false);
+ if (alive.current) await recoverTurn(turn.id);
+ }
+ })();
+ };
+
+ const applyAction = async (action: CompanionAction) => {
+ if (locked.current) return;
+ locked.current = true;
+ setActing(true);
+ setError(null);
+ try {
+ await beforeCompanionApply?.();
+ const result = await api.applyCompanionAction(action.id);
+ if (alive.current) {
+ setConflicts((current) => {
+ const next = { ...current };
+ delete next[action.id];
+ return next;
+ });
+ setActions((previous) => previous.map((item) => item.id === action.id ? result.action : item));
+ }
+ try {
+ await onCompanionNotesChanged?.();
+ } catch {
+ if (alive.current) setError(t("companion.actions.syncFailed"));
+ }
+ await reloadCompanion().catch(() => undefined);
+ } catch (cause) {
+ if (cause instanceof ApiRequestError && (cause.status === 409 || cause.code === "companion_action_conflict")) {
+ if (alive.current) setConflicts((current) => ({ ...current, [action.id]: t("aiAssistant.sidebar.proposalConflict") }));
+ return;
+ }
+ if (alive.current) setError(explainError(cause));
+ } finally {
+ locked.current = false;
+ if (alive.current) setActing(false);
+ }
+ };
+
+ const dismissAction = async (action: CompanionAction) => {
+ if (locked.current) return;
+ locked.current = true;
+ setActing(true);
+ try {
+ await api.dismissCompanionAction(action.id);
+ await reloadCompanion();
+ } catch (cause) {
+ if (alive.current) setError(explainError(cause));
+ } finally {
+ locked.current = false;
+ if (alive.current) setActing(false);
+ }
+ };
+
+ const openLinkedNote = (event: MouseEvent<HTMLDivElement>, turn: CompanionTurn) => {
+ const target = event.target;
+ if (!(target instanceof Element) || !onOpenCompanionNote) return;
+ const link = target.closest("a");
+ if (!(link instanceof HTMLAnchorElement)) return;
+ const linkedId = parseMemoLinkHref(link.getAttribute("href"))
+ ?? parseMemoLinkHref(link.hash)
+ ?? parseMemoLinkHref(link.href);
+ if (!linkedId) return;
+ const source = turn.sources.find((item) => item.id === linkedId);
+ if (!source?.notebookId) return;
+ event.preventDefault();
+ onOpenCompanionNote(linkedId, source.notebookId);
+ };
+
+ const selectionCount = Array.from((selectionMarkdown ?? "").trim()).length;
+ const threadTurns = turns.filter((turn) => turn.threadId === threadId).slice().reverse();
+ const visibleCompanion = source === "builtin";
+ const visibleTurns = visibleCompanion ? threadTurns : localTurns;
+ const running = busy || visibleTurns.some((turn) => turn.status === "running");
+
+ const renderActions = (turnId: string) => actions
+ .filter((action) => action.turnId === turnId && action.status === "pending")
+ .map((action) => {
+ const draft = noteEditDraft(action);
+ return (
+ <div key={action.id} className="space-y-2 rounded-md border border-slate-200 p-2">
+ <p className="text-xs leading-5 text-slate-600">{action.plan.reason}</p>
+ {draft ? <NoteEditDiff before={draft.before} after={draft.after} /> : null}
+ {conflicts[action.id] ? <p role="alert" className="text-xs text-rose-700">{conflicts[action.id]}</p> : null}
+ <div className="flex gap-2">
+ <Button type="button" size="sm" variant="solid" disabled={running || acting} onClick={() => void applyAction(action)}>
+ {t(draft ? "aiAssistant.sidebar.proposalConfirm" : "aiAssistant.sidebar.proposalConfirmOther")}
+ </Button>
+ <Button type="button" size="sm" variant="outline" disabled={running || acting} onClick={() => void dismissAction(action)}>
+ {t(draft ? "aiAssistant.sidebar.proposalReject" : "aiAssistant.sidebar.proposalDismiss")}
+ </Button>
+ </div>
+ </div>
+ );
+ });
+
+ return (
+ <div className="flex h-full min-h-0 min-w-0 flex-col bg-card">
+ <div className="flex h-12 shrink-0 items-center gap-2 border-b border-slate-200 px-2">
+ <Sparkles className="ml-1 h-4 w-4 shrink-0 text-slate-500" aria-hidden="true" />
+ <h2 className="min-w-0 flex-1 truncate text-sm font-semibold text-slate-950">{t("aiAssistant.sidebar.title")}</h2>
+ {source === "local" ? (
+ <span className="shrink-0 text-xs text-slate-500">{t("aiAssistant.sidebar.localStatus")}</span>
+ ) : null}
+ <Button
+ type="button"
+ size="icon-sm"
+ variant="ghost"
+ title={t("aiAssistant.sidebar.close")}
+ aria-label={t("aiAssistant.sidebar.close")}
+ onClick={() => onOpenChange(false)}
+ >
+ <PanelRightClose className="h-4 w-4" />
+ </Button>
+ </div>
+ {selectionCount > 0 ? (
+ <p className="shrink-0 border-b border-slate-200 px-3 py-1.5 text-xs text-slate-500">
+ {t("aiAssistant.sidebar.selectionBadge", { count: selectionCount })}
+ </p>
+ ) : null}
+ {error ? <p role="alert" className="shrink-0 px-3 pt-2 text-sm text-rose-700">{error}</p> : null}
+ <Conversation className="min-h-0 flex-1">
+ <ConversationContent className={sidebarThreadClassName}>
+ {loading && visibleCompanion ? <p role="status" className="text-sm text-slate-500">{t("common.loading")}</p> : null}
+ {!loading && !visibleTurns.length ? (
+ <ConversationEmptyState
+ icon={<Sparkles className="h-6 w-6" />}
+ title={t("aiAssistant.sidebar.emptyTitle")}
+ description={t("aiAssistant.sidebar.emptyDescription")}
+ />
+ ) : null}
+ {visibleCompanion ? threadTurns.map((turn) => (
+ <div key={turn.id} className="space-y-2">
+ <Message from="user">
+ <MessageContent className={sidebarUserMessageClassName}>{turn.message}</MessageContent>
+ <AttachmentChips items={turn.attachments ?? []} />
+ </Message>
+ <Message from="assistant">
+ {turn.process?.trim() ? (
+ <Reasoning isStreaming={turn.status === "running"}>
+ <ReasoningTrigger getThinkingMessage={() => t("companion.process")} />
+ <ReasoningContent>{turn.process}</ReasoningContent>
+ </Reasoning>
+ ) : turn.status === "running" ? (
+ <p role="status" className="flex items-center gap-2 text-xs text-slate-500">
+ <Loader2 className="h-3.5 w-3.5 animate-spin" aria-hidden="true" />
+ {t("aiAssistant.sidebar.working")}
+ </p>
+ ) : null}
+ {turn.tools?.length ? (
+ <ul className="space-y-1 text-xs text-slate-500">
+ {turn.tools.map((tool) => (
+ <li key={tool.id}>{t("aiAssistant.sidebar.toolProgress", { name: tool.name, status: tool.status })}</li>
+ ))}
+ </ul>
+ ) : null}
+ {turn.response ? (
+ <div onClick={(event) => openLinkedNote(event, turn)}>
+ <AiSidebarMessage isAnimating={turn.status === "running"}>
+ {linkedCompanionText(turn.response, turn)}
+ </AiSidebarMessage>
+ </div>
+ ) : null}
+ {turn.status === "interrupted" && turn.questions?.length && !running ? (
+ <CompanionQuestionForm questions={turn.questions} busy={busy || acting} onSubmit={(answers) => resume(turn, answers)} />
+ ) : null}
+ {turn.status === "interrupted" && !turn.questions?.length && !running ? (
+ <Button type="button" size="sm" variant="outline" disabled={busy || acting} onClick={() => resume(turn)}>
+ {t("companion.continue")}
+ </Button>
+ ) : null}
+ {turn.status === "failed" || turn.status === "cancelled" ? (
+ <p className="text-xs text-slate-500">{t(`companion.status.${turn.status}`)}</p>
+ ) : null}
+ {renderActions(turn.id)}
+ </Message>
+ </div>
+ )) : localTurns.map((turn) => (
+ <div key={turn.id} className="space-y-2">
+ <Message from="user">
+ <MessageContent className={sidebarUserMessageClassName}>{turn.message}</MessageContent>
+ <AttachmentChips items={turn.attachments} />
+ </Message>
+ <Message from="assistant">
+ {turn.reasoning.trim() ? (
+ <Reasoning isStreaming={turn.status === "running"}>
+ <ReasoningTrigger getThinkingMessage={() => t("companion.process")} />
+ <ReasoningContent>{turn.reasoning}</ReasoningContent>
+ </Reasoning>
+ ) : turn.status === "running" ? (
+ <p role="status" className="flex items-center gap-2 text-xs text-slate-500">
+ <Loader2 className="h-3.5 w-3.5 animate-spin" aria-hidden="true" />
+ {t("aiAssistant.sidebar.working")}
+ </p>
+ ) : null}
+ {turn.tools.length ? (
+ <ul className="space-y-1 text-xs text-slate-500">
+ {turn.tools.map((tool) => (
+ <li key={tool.id}>{t("aiAssistant.sidebar.toolProgress", { name: tool.title || tool.name, status: tool.status })}</li>
+ ))}
+ </ul>
+ ) : null}
+ {turn.response ? <AiSidebarMessage isAnimating={turn.status === "running"}>{turn.response}</AiSidebarMessage> : null}
+ {turn.status === "failed" || turn.status === "cancelled" ? (
+ <p className="text-xs text-slate-500">{t(`companion.status.${turn.status}`)}</p>
+ ) : null}
+ </Message>
+ </div>
+ ))}
+ </ConversationContent>
+ <ConversationScrollButton />
+ </Conversation>
+ <div className="shrink-0 space-y-2 border-t border-slate-200 p-3">
+ {!companionAvailable && source !== "local" ? (
+ <p className="text-xs leading-5 text-slate-500">{t("aiAssistant.sidebar.unavailable")}</p>
+ ) : null}
+ <div className="flex flex-wrap gap-1.5">
+ {SKILLS.map((skill) => (
+ <Button
+ key={skill.id}
+ type="button"
+ size="sm"
+ variant="outline"
+ disabled={running || acting}
+ onClick={() => void launch(skillPrompt(skill.id)).catch(() => undefined)}
+ >
+ {t(`aiAssistant.sidebar.skills.${skill.id}`)}
+ </Button>
+ ))}
+ </div>
+ <PromptInputProvider>
+ <SidebarComposer
+ attachments={attachments}
+ attachmentError={attachmentError}
+ busy={running}
+ locked={acting}
+ placeholder={t("aiAssistant.sidebar.placeholder")}
+ onAddFiles={(files) => { void addFiles(files); }}
+ onRemoveAttachment={(id) => setAttachments((current) => current.filter((item) => item.localId !== id))}
+ onLaunch={launch}
+ onStop={() => { void stop(); }}
+ skillPrompt={skillPrompt}
+ />
+ </PromptInputProvider>
+ </div>
+ </div>
+ );
+}
+
+export function AiSidebar(props: AiSidebarProps) {
+ const { t } = useTranslation();
+ const { open, onOpenChange } = props;
+ const [width, setWidth] = useState(readAiSidebarWidth);
+ const [resizing, setResizing] = useState(false);
+ const [narrow, setNarrow] = useState(() => typeof window !== "undefined" && window.matchMedia(NARROW_QUERY).matches);
+ const [viewportWidth, setViewportWidth] = useState(() => (typeof window === "undefined" ? 1024 : window.innerWidth));
+ const addFilesRef = useRef<(files: File[]) => void>(() => undefined);
+ const onStopReady = useRef<() => void>(() => undefined);
+
+ useEffect(() => {
+ const media = window.matchMedia(NARROW_QUERY);
+ const sync = () => {
+ setNarrow(media.matches);
+ setViewportWidth(window.innerWidth);
+ };
+ sync();
+ media.addEventListener("change", sync);
+ window.addEventListener("resize", sync);
+ return () => {
+ media.removeEventListener("change", sync);
+ window.removeEventListener("resize", sync);
+ };
+ }, []);
+
+ const resize = (event: ReactPointerEvent<HTMLDivElement>) => {
+ if (narrow) return;
+ event.preventDefault();
+ setResizing(true);
+ const handle = event.currentTarget;
+ handle.setPointerCapture(event.pointerId);
+ const startX = event.clientX;
+ const startWidth = width;
+ const move = (pointer: PointerEvent) => {
+ setWidth(clampSidebarWidth(startWidth + startX - pointer.clientX));
+ };
+ const up = (pointer: PointerEvent) => {
+ const next = clampSidebarWidth(startWidth + startX - pointer.clientX);
+ setWidth(next);
+ writeStorage(AI_SIDEBAR_WIDTH_KEY, String(next));
+ setResizing(false);
+ handle.removeEventListener("pointermove", move);
+ handle.removeEventListener("pointerup", up);
+ };
+ handle.addEventListener("pointermove", move);
+ handle.addEventListener("pointerup", up);
+ };
+
+ const panelWidth = narrow ? Math.min(viewportWidth, 384) : width;
+
+ return (
+ <>
+ <AnimatePresence>
+ {open && narrow ? (
+ <m.button
+ key="ai-sidebar-scrim"
+ type="button"
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={sidebarRevealTransition}
+ className="fixed inset-0 z-40 bg-slate-950/25"
+ aria-label={t("aiAssistant.sidebar.close")}
+ onClick={() => onOpenChange(false)}
+ />
+ ) : null}
+ </AnimatePresence>
+ <m.aside
+ initial={false}
+ animate={narrow ? { x: open ? 0 : "100%", width: panelWidth } : { x: 0, width: open ? width : 0 }}
+ transition={resizing ? { duration: 0 } : sidebarRevealTransition}
+ aria-hidden={!open}
+ aria-label={t("aiAssistant.sidebar.title")}
+ inert={open ? undefined : true}
+ className={cn(
+ "h-full min-h-0 shrink-0 overflow-hidden bg-card",
+ narrow ? "fixed inset-y-0 right-0 z-50 shadow-xl" : "relative",
+ )}
+ onDragOverCapture={(event) => {
+ if (event.dataTransfer?.types?.includes("Files")) event.preventDefault();
+ }}
+ onDropCapture={(event) => {
+ const files = Array.from(event.dataTransfer?.files ?? []);
+ if (!files.length) return;
+ event.preventDefault();
+ event.stopPropagation();
+ addFilesRef.current(files);
+ }}
+ >
+ <div className="absolute inset-y-0 right-0 flex min-h-0 flex-col border-l border-slate-200 bg-card" style={{ width: panelWidth }}>
+ {open && !narrow ? (
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <div
+ role="separator"
+ aria-orientation="vertical"
+ aria-valuemin={SIDEBAR_MIN_WIDTH}
+ aria-valuemax={SIDEBAR_MAX_WIDTH}
+ aria-valuenow={width}
+ aria-label={t("aiAssistant.sidebar.resize")}
+ tabIndex={0}
+ className="absolute left-0 top-0 z-10 h-full w-1.5 -translate-x-1/2 cursor-col-resize"
+ onPointerDown={resize}
+ onKeyDown={(event) => {
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
+ event.preventDefault();
+ const next = clampSidebarWidth(width + (event.key === "ArrowLeft" ? 16 : -16));
+ setWidth(next);
+ writeStorage(AI_SIDEBAR_WIDTH_KEY, String(next));
+ }}
+ />
+ </TooltipTrigger>
+ <TooltipContent side="left">{t("aiAssistant.sidebar.resize")}</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ ) : null}
+ <AiSidebarSession {...props} addFilesRef={addFilesRef} onStopReady={onStopReady} />
+ </div>
+ </m.aside>
+ </>
+ );
+}
diff --git a/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx b/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx
new file mode 100644
index 00000000..4c514558
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/AiSidebarMessage.tsx
@@ -0,0 +1,23 @@
+import { cjk } from "@streamdown/cjk";
+import { code } from "@streamdown/code";
+import { math } from "@streamdown/math";
+import { mermaid } from "@streamdown/mermaid";
+import { memo } from "react";
+import { Streamdown } from "streamdown";
+import "katex/dist/katex.min.css";
+import { cn } from "@/lib/utils";
+
+const sidebarStreamdownPlugins = { cjk, code, mermaid, math };
+
+export const AiSidebarMessage = memo(
+ ({ className, ...props }: { children: string; className?: string; isAnimating?: boolean }) => (
+ <Streamdown
+ className={cn("size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0", className)}
+ plugins={sidebarStreamdownPlugins}
+ {...props}
+ />
+ ),
+ (prev, next) => prev.children === next.children && prev.isAnimating === next.isAnimating,
+);
+
+AiSidebarMessage.displayName = "AiSidebarMessage";
diff --git a/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs b/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
index 5f195472..20981d64 100644
--- a/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
+++ b/apps/web/src/components/dialogs/AiAssistantDialog.test.mjs
@@ -2,16 +2,18 @@ import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
describe("AI assistant modes", () => {
- test("keeps a floating panel with instruction and a single ask chat", () => {
- const source = readFileSync(new URL("./AiAssistantDialog.tsx", import.meta.url), "utf8");
+ test("keeps the companion sidebar wired through the editor", () => {
const editor = readFileSync(new URL("../EditorPane.tsx", import.meta.url), "utf8");
- expect(source).toContain('useState<AiAssistantMode>("instruction")');
- expect(source).toContain('(["instruction", "ask"] as const)');
- expect(source).not.toContain('"organize"');
- expect(source).not.toContain("inset-y-3 right-3");
- expect(source).toContain("<CompanionChat");
+ const sidebar = readFileSync(new URL("../ai-sidebar/AiSidebar.tsx", import.meta.url), "utf8");
+ expect(editor).toContain("<AiSidebar");
expect(editor).toContain("companionAvailable={companionAvailable}");
expect(editor).toContain("beforeCompanionApply={beforeCompanionApply}");
+ expect(editor).not.toContain("<AiAssistantDialog");
+ expect(editor).not.toContain("onApply={applyAiDraft}");
+ expect(sidebar).toContain("companionAvailable");
+ expect(sidebar).toContain("sidebarRevealTransition");
+ expect(sidebar).not.toContain("{open && <AiSidebarSession");
+ expect(sidebar).not.toContain("hidden w-0");
});
test("agent mode opens a fresh thread and only resumes last chat on request", () => {
diff --git a/apps/web/src/components/dialogs/AiAssistantDialog.tsx b/apps/web/src/components/dialogs/AiAssistantDialog.tsx
deleted file mode 100644
index 0fcf1522..00000000
--- a/apps/web/src/components/dialogs/AiAssistantDialog.tsx
+++ /dev/null
@@ -1,1118 +0,0 @@
-import {
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
- type CSSProperties,
- type FormEvent,
- type PointerEvent as ReactPointerEvent,
-} from "react";
-import { createPortal } from "react-dom";
-import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
-import { AlertCircle, BookmarkPlus, Check, Copy, FileText, GripHorizontal, Library, Loader2, Paperclip, PenLine, RefreshCw, Sparkles, Square, Trash2, X } from "lucide-react";
-import { useTranslation } from "react-i18next";
-import { Button } from "@/components/ui/button";
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog";
-import { Input } from "@/components/ui/input";
-import {
- Select,
- SelectContent,
- SelectGroup,
- SelectItem,
- SelectLabel,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-import { api, ApiRequestError } from "@/lib/api";
-import {
- AI_ATTACHMENT_ACCEPT,
- AiAttachmentError,
- formatAiAttachmentSize,
- prepareAiAttachments,
- type PreparedAiAttachment,
-} from "@/lib/ai-attachments";
-import {
- aiTones,
- buildAiAssistantLastActionPreference,
- buildAiAssistantRequest,
- buildAiRefinementInstruction,
- getAiAssistantLastActionScope,
- getDefaultAiAction,
- getDefaultTargetLanguage,
- promptAllowsAppend,
- promptAllowsReplace,
- promptNeedsTargetLanguage,
- promptNeedsTone,
- readStoredAiAssistantLastActionPreference,
- readStoredAiAssistantMode,
- resolveAiAssistantComposerInput,
- resolveAiAssistantOpenAction,
- targetLanguages,
- writeStoredAiAssistantLastActionPreference,
- writeStoredAiAssistantMode,
- type AiAssistantAction,
- type AiAssistantMode,
- type AiTone,
- type TargetLanguage,
-} from "@/lib/ai-assistant";
-import { CompanionChat } from "@/components/CompanionChat";
-import { parseDiagramDocument } from "@edgeever/shared";
-import { copyTextToClipboard } from "@/lib/clipboard";
-import {
- clampFloatingPanelPosition,
- resolveAnchoredFloatingPanelLayout,
- type FloatingPanelPosition,
-} from "@/lib/floating-panel";
-import { cn } from "@/lib/utils";
-
-const FREEFORM_VALUE = "custom";
-const PROMPT_VALUE_PREFIX = "prompt:";
-const AI_ASSISTANT_LAYER_SELECTOR = '[data-edgeever-ai-assistant-layer="true"]';
-const AI_ASSISTANT_VIEWPORT_GAP = 12;
-const AI_ASSISTANT_SELECT_TRIGGER_CLASSNAME = "h-10 w-full min-w-0 shadow-none focus:border-slate-900 focus:ring-2 focus:ring-slate-900/10 focus:ring-offset-0 data-[state=open]:border-slate-900 data-[state=open]:ring-2 data-[state=open]:ring-slate-900/10";
-
-const getViewportSize = () => ({
- height: typeof window === "undefined" ? 768 : window.innerHeight,
- width: typeof window === "undefined" ? 1024 : window.innerWidth,
-});
-
-export const isAiAssistantPointerTarget = (target: EventTarget | null, panel: HTMLElement | null) => {
- if (!(target instanceof Node)) return false;
- if (panel?.contains(target)) return true;
- return target instanceof Element && Boolean(target.closest(AI_ASSISTANT_LAYER_SELECTOR));
-};
-
-const promptSelectValue = (id: string) => `${PROMPT_VALUE_PREFIX}${id}`;
-
-const parsePromptSelectValue = (value: string) =>
- value.startsWith(PROMPT_VALUE_PREFIX) ? value.slice(PROMPT_VALUE_PREFIX.length) : null;
-
-export type AiAssistantAnchor = {
- left: number;
- placement: "above" | "below";
- top: number;
-};
-
-export const AiAssistantDialog = ({
- open,
- anchor,
- title,
- contentMarkdown,
- selectionMarkdown,
- memoId,
- notebookId,
- notebookTitle,
- companionAvailable = false,
- onOpenChange,
- onApply,
- onOpenPromptLibrary,
- beforeCompanionApply,
- onCompanionNotesChanged,
- onOpenCompanionNote,
-}: {
- open: boolean;
- anchor: AiAssistantAnchor;
- title: string;
- contentMarkdown: string;
- selectionMarkdown?: string | null;
- memoId?: string;
- notebookId?: string;
- notebookTitle?: string;
- companionAvailable?: boolean;
- onOpenChange: (open: boolean) => void;
- onApply: (text: string, mode: "append" | "replace") => boolean;
- onOpenPromptLibrary?: () => void;
- beforeCompanionApply?: () => Promise<void>;
- onCompanionNotesChanged?: () => Promise<void>;
- onOpenCompanionNote?: (id: string, notebookId: string) => void;
-}) => {
- const { t, i18n } = useTranslation();
- const queryClient = useQueryClient();
- const hasSelection = Boolean(selectionMarkdown?.trim());
- const lastActionScope = getAiAssistantLastActionScope(hasSelection);
- const sourceMarkdown = hasSelection ? selectionMarkdown!.trim() : contentMarkdown;
- const noteHasContent = Boolean(contentMarkdown.trim());
- const defaultTargetLanguage = getDefaultTargetLanguage(i18n.resolvedLanguage);
- const defaultAction = getDefaultAiAction(hasSelection);
- const [action, setAction] = useState<AiAssistantAction>(defaultAction);
- const [selectedPromptId, setSelectedPromptId] = useState<string | null>(null);
- const [targetLanguage, setTargetLanguage] = useState<TargetLanguage>(() => defaultTargetLanguage);
- const [tone, setTone] = useState<AiTone>("professional");
- const [customInstruction, setCustomInstruction] = useState("");
- const [refinement, setRefinement] = useState("");
- const [output, setOutput] = useState("");
- const [error, setError] = useState<string | null>(null);
- const [isGenerating, setIsGenerating] = useState(false);
- const [copied, setCopied] = useState(false);
- const [saveDialogOpen, setSaveDialogOpen] = useState(false);
- const [saveName, setSaveName] = useState("");
- const [saveDescription, setSaveDescription] = useState("");
- const [promptFeedback, setPromptFeedback] = useState<string | null>(null);
- const [attachments, setAttachments] = useState<PreparedAiAttachment[]>([]);
- const [attachmentError, setAttachmentError] = useState<string | null>(null);
- const [isReadingAttachments, setIsReadingAttachments] = useState(false);
- const [mode, setMode] = useState<AiAssistantMode>("instruction");
- const [initializedForOpen, setInitializedForOpen] = useState(false);
- const [panelElement, setPanelElement] = useState<HTMLElement | null>(null);
- const [draggedPosition, setDraggedPosition] = useState<FloatingPanelPosition | null>(null);
- const [isDragging, setIsDragging] = useState(false);
- const [viewportSize, setViewportSize] = useState(getViewportSize);
- const controllerRef = useRef<AbortController | null>(null);
- const panelRef = useRef<HTMLElement | null>(null);
- const dragStateRef = useRef<{
- offsetX: number;
- offsetY: number;
- pointerId: number;
- } | null>(null);
- const instructionRef = useRef<HTMLTextAreaElement | null>(null);
- const attachmentInputRef = useRef<HTMLInputElement | null>(null);
- const attachmentReadIdRef = useRef(0);
- const customInstructionEditedRef = useRef(false);
- const lastRequestRef = useRef<{
- preserveOutput: boolean;
- request: Parameters<typeof api.streamAiGeneration>[0];
- } | null>(null);
- const assignPanelRef = useCallback((node: HTMLElement | null) => {
- panelRef.current = node;
- setPanelElement(node);
- }, []);
-
- const promptsQuery = useQuery({
- queryKey: ["ai-prompts", i18n.resolvedLanguage],
- queryFn: async () => (await api.listAiPrompts(i18n.resolvedLanguage)).prompts,
- enabled: open,
- retry: false,
- });
- const prompts = promptsQuery.data ?? [];
- const selectedPrompt = useMemo(
- () => prompts.find((prompt) => prompt.id === selectedPromptId) ?? null,
- [prompts, selectedPromptId],
- );
- const effectiveActionKey = selectedPrompt?.action ?? action;
- const effectiveParameterKind = selectedPrompt?.parameterKind ?? "none";
- const effectiveResultMode = selectedPrompt?.resultMode ?? "both";
-
- const selectValue = selectedPromptId
- ? promptSelectValue(selectedPromptId)
- : FREEFORM_VALUE;
-
- useEffect(() => () => controllerRef.current?.abort(), []);
- useEffect(() => {
- controllerRef.current?.abort();
- attachmentReadIdRef.current += 1;
- if (!open) {
- setInitializedForOpen(false);
- setDraggedPosition(null);
- setIsDragging(false);
- dragStateRef.current = null;
- return;
- }
- setCustomInstruction("");
- setRefinement("");
- setOutput("");
- setError(null);
- setCopied(false);
- setIsGenerating(false);
- setSaveDialogOpen(false);
- setSaveName("");
- setSaveDescription("");
- setPromptFeedback(null);
- setAttachments([]);
- setAttachmentError(null);
- setIsReadingAttachments(false);
- setInitializedForOpen(false);
- setDraggedPosition(null);
- setIsDragging(false);
- dragStateRef.current = null;
- customInstructionEditedRef.current = false;
- lastRequestRef.current = null;
- setMode(hasSelection ? "instruction" : readStoredAiAssistantMode());
- }, [defaultAction, defaultTargetLanguage, hasSelection, open]);
-
- useEffect(() => {
- if (!open) return;
- const handleResize = () => {
- const nextViewportSize = getViewportSize();
- setViewportSize(nextViewportSize);
- setDraggedPosition((current) => {
- const panel = panelRef.current;
- if (!current || !panel) return current;
- const panelRect = panel.getBoundingClientRect();
- return clampFloatingPanelPosition(
- current,
- { height: panelRect.height, width: panelRect.width },
- nextViewportSize,
- AI_ASSISTANT_VIEWPORT_GAP,
- );
- });
- };
- handleResize();
- window.addEventListener("resize", handleResize);
- return () => window.removeEventListener("resize", handleResize);
- }, [open]);
-
- useEffect(() => {
- if (!open || initializedForOpen || promptsQuery.isLoading) return;
- if (customInstructionEditedRef.current || customInstruction.trim()) {
- setSelectedPromptId(null);
- setAction("custom");
- setInitializedForOpen(true);
- return;
- }
- const resolved = resolveAiAssistantOpenAction({
- hasSelection,
- preference: readStoredAiAssistantLastActionPreference(lastActionScope),
- prompts,
- });
- setSelectedPromptId(resolved.selectedPromptId);
- setAction(resolved.action);
- setTargetLanguage(resolved.targetLanguage ?? defaultTargetLanguage);
- setTone(resolved.tone ?? "professional");
- setInitializedForOpen(true);
- }, [customInstruction, defaultAction, defaultTargetLanguage, initializedForOpen, lastActionScope, open, prompts, promptsQuery.isLoading]);
-
- useEffect(() => {
- if (!open) return;
- const frame = window.requestAnimationFrame(() => instructionRef.current?.focus());
- const handlePointerDown = (event: PointerEvent) => {
- if (isAiAssistantPointerTarget(event.target, panelRef.current)) return;
- onOpenChange(false);
- };
- window.addEventListener("pointerdown", handlePointerDown);
- return () => {
- window.cancelAnimationFrame(frame);
- window.removeEventListener("pointerdown", handlePointerDown);
- };
- }, [onOpenChange, open]);
-
- useEffect(() => {
- if (!open || !initializedForOpen || promptsQuery.isLoading || !selectedPromptId || selectedPrompt) return;
- setSelectedPromptId(null);
- setAction("custom");
- setOutput("");
- setError(t("aiAssistant.promptMissing"));
- }, [initializedForOpen, open, promptsQuery.isLoading, selectedPrompt, selectedPromptId, t]);
-
- const clearResult = () => {
- setOutput("");
- setError(null);
- setPromptFeedback(null);
- };
-
- const persistLastAction = ({
- nextAction,
- nextPrompt,
- nextPromptId,
- nextTargetLanguage,
- nextTone,
- }: {
- nextAction: AiAssistantAction;
- nextPrompt?: { seedKey: typeof prompts[number]["seedKey"] } | null;
- nextPromptId: string | null;
- nextTargetLanguage?: TargetLanguage;
- nextTone?: AiTone;
- }) => {
- writeStoredAiAssistantLastActionPreference(lastActionScope, buildAiAssistantLastActionPreference({
- action: nextAction,
- promptId: nextPromptId,
- seedKey: nextPrompt?.seedKey ?? null,
- targetLanguage: nextTargetLanguage ?? targetLanguage,
- tone: nextTone ?? tone,
- }));
- };
-
- const handleActionChange = (value: string) => {
- customInstructionEditedRef.current = false;
- if (value === FREEFORM_VALUE) {
- setAction("custom");
- setSelectedPromptId(null);
- persistLastAction({ nextAction: "custom", nextPrompt: null, nextPromptId: null });
- clearResult();
- return;
- }
-
- const promptId = parsePromptSelectValue(value);
- if (!promptId) return;
- const prompt = prompts.find((item) => item.id === promptId);
- setSelectedPromptId(promptId);
- setAction(prompt?.action ?? "custom");
- persistLastAction({
- nextAction: prompt?.action ?? "custom",
- nextPrompt: prompt ?? null,
- nextPromptId: promptId,
- });
- clearResult();
- };
-
- const handleComposerChange = (value: string) => {
- customInstructionEditedRef.current = true;
- if (!initializedForOpen) {
- setSelectedPromptId(null);
- setAction("custom");
- }
- setCustomInstruction(value);
- clearResult();
- };
-
- const runGeneration = async (
- request: Parameters<typeof api.streamAiGeneration>[0],
- { preserveOutput = false }: { preserveOutput?: boolean } = {},
- ) => {
- controllerRef.current?.abort();
- const controller = new AbortController();
- controllerRef.current = controller;
- lastRequestRef.current = { preserveOutput, request };
- if (!preserveOutput) setOutput("");
- setError(null);
- setCopied(false);
- setIsGenerating(true);
- let receivedOutput = false;
- try {
- await api.streamAiGeneration(
- request,
- {
- signal: controller.signal,
- onEvent: (event) => {
- if (event.type === "text-delta") {
- if (preserveOutput && !receivedOutput) {
- receivedOutput = true;
- setOutput(event.text);
- } else {
- setOutput((current) => current + event.text);
- }
- }
- if (event.type === "error") setError(event.message);
- },
- },
- );
- } catch (caught) {
- if (controller.signal.aborted || (caught instanceof DOMException && caught.name === "AbortError")) return;
- setError(caught instanceof ApiRequestError
- ? caught.code === "ai_not_configured"
- ? t("aiAssistant.configure")
- : caught.code === "ai_source_required"
- ? t("aiAssistant.sourceRequired")
- : caught.code === "ai_request_invalid"
- ? t("aiAssistant.requestInvalid")
- : caught.message
- : caught instanceof TypeError && caught.message === "Failed to fetch"
- ? t("aiAssistant.networkError")
- : caught instanceof Error ? caught.message : t("aiModel.failed"));
- } finally {
- if (controllerRef.current === controller) {
- controllerRef.current = null;
- setIsGenerating(false);
- }
- }
- };
-
- const generate = () => {
- const currentInstruction = instructionRef.current?.value ?? customInstruction;
- if (!selectedPrompt && effectiveActionKey === "custom" && !currentInstruction.trim()) {
- setError(t("aiAssistant.customInstructionRequired"));
- instructionRef.current?.focus();
- return;
- }
-
- persistLastAction({
- nextAction: effectiveActionKey,
- nextPrompt: selectedPrompt,
- nextPromptId: selectedPromptId,
- });
- const composerInput = resolveAiAssistantComposerInput({
- composerText: currentInstruction,
- hasSelection,
- isFreeformCustom,
- noteContentMarkdown: sourceMarkdown,
- noteTitle: title,
- });
-
- return runGeneration(buildAiAssistantRequest({
- action: effectiveActionKey,
- contentMarkdown: composerInput.contentMarkdown,
- customInstruction: composerInput.customInstruction,
- locale: i18n.resolvedLanguage,
- parameterKind: selectedPrompt ? effectiveParameterKind : undefined,
- promptId: selectedPrompt?.id,
- targetLanguage,
- title: composerInput.title,
- tone,
- attachments: attachments.map(({ byteLength: _byteLength, ...attachment }) => attachment),
- }));
- };
-
- const addAttachments = async (files: File[]) => {
- if (!files.length) return;
- const readId = attachmentReadIdRef.current + 1;
- attachmentReadIdRef.current = readId;
- setAttachmentError(null);
- setIsReadingAttachments(true);
- try {
- const prepared = await prepareAiAttachments(files, attachments);
- if (attachmentReadIdRef.current !== readId) return;
- setAttachments((current) => [...current, ...prepared]);
- clearResult();
- } catch (caught) {
- if (attachmentReadIdRef.current !== readId) return;
- const code = caught instanceof AiAttachmentError ? caught.code : "readFailed";
- setAttachmentError(t(`aiAssistant.attachmentErrors.${code}`));
- } finally {
- if (attachmentReadIdRef.current === readId) {
- setIsReadingAttachments(false);
- if (attachmentInputRef.current) attachmentInputRef.current.value = "";
- }
- }
- };
-
- const refine = () => {
- const instruction = refinement.trim();
- if (!output || !instruction) return;
- const refinementTargetLanguage = promptNeedsTargetLanguage(effectiveParameterKind)
- ? targetLanguage
- : undefined;
- const refinementTone = promptNeedsTone(effectiveParameterKind) ? tone : undefined;
- return runGeneration({
- action: "custom",
- title: "",
- contentMarkdown: output,
- instruction: buildAiRefinementInstruction({
- originalAction: effectiveActionKey,
- originalInstruction: selectedPrompt?.instruction
- ?? (isFreeformCustom ? customInstruction : undefined),
- refinement: instruction,
- targetLanguage: refinementTargetLanguage,
- tone: refinementTone,
- }),
- targetLanguage: refinementTargetLanguage,
- tone: refinementTone,
- }, { preserveOutput: true });
- };
-
- const retry = () => {
- if (lastRequestRef.current) {
- return runGeneration(lastRequestRef.current.request, {
- preserveOutput: lastRequestRef.current.preserveOutput,
- });
- }
- return generate();
- };
-
- const applyOutput = (mode: "append" | "replace") => {
- setError(null);
- if (!onApply(output, mode)) {
- setError(t("aiAssistant.applyFailed"));
- }
- };
-
- const copy = async () => {
- if (await copyTextToClipboard(output)) {
- setCopied(true);
- window.setTimeout(() => setCopied(false), 1600);
- }
- };
-
- const createPromptMutation = useMutation({
- mutationFn: () => api.createAiPrompt({
- name: saveName.trim(),
- description: saveDescription.trim() || undefined,
- instruction: customInstruction.trim(),
- parameterKind: "none",
- resultMode: "both",
- }),
- onSuccess: ({ prompt }) => {
- queryClient.setQueryData(
- ["ai-prompts", i18n.resolvedLanguage],
- (current: typeof prompts | undefined) => [
- ...(current ?? []).filter((item) => item.id !== prompt.id),
- prompt,
- ],
- );
- setSelectedPromptId(prompt.id);
- setAction(prompt.action);
- persistLastAction({
- nextAction: prompt.action,
- nextPrompt: prompt,
- nextPromptId: prompt.id,
- });
- customInstructionEditedRef.current = false;
- setSaveDialogOpen(false);
- setSaveName("");
- setSaveDescription("");
- setPromptFeedback(t("aiAssistant.promptSaved"));
- void queryClient.invalidateQueries({ queryKey: ["ai-prompts"] });
- },
- });
-
- const promptErrorMessage = createPromptMutation.error
- ? createPromptMutation.error instanceof Error
- ? createPromptMutation.error.message
- : t("aiAssistant.promptSaveFailed")
- : null;
-
- const isFreeformCustom = !selectedPromptId && action === "custom";
- const showInstructionComposer = isFreeformCustom;
- const showSourceComposer = !isFreeformCustom && !hasSelection && !noteHasContent;
- const usesComposerAsSource = showSourceComposer && Boolean(customInstruction.trim());
- const canSaveAsPrompt = isFreeformCustom && customInstruction.trim().length > 0;
- const generateDisabled = isGenerating
- || isReadingAttachments;
-
- const handleDragStart = useCallback((event: ReactPointerEvent<HTMLDivElement>) => {
- if (event.button !== 0 || event.pointerType === "touch") return;
- if (event.target instanceof Element && event.target.closest("button")) return;
- const panel = panelRef.current;
- if (!panel) return;
- const panelRect = panel.getBoundingClientRect();
- dragStateRef.current = {
- offsetX: event.clientX - panelRect.left,
- offsetY: event.clientY - panelRect.top,
- pointerId: event.pointerId,
- };
- setDraggedPosition({ left: panelRect.left, top: panelRect.top });
- setIsDragging(true);
- event.preventDefault();
- }, []);
-
- const handleDragMove = useCallback((event: PointerEvent) => {
- const dragState = dragStateRef.current;
- const panel = panelRef.current;
- if (!dragState || dragState.pointerId !== event.pointerId || !panel) return;
- const panelRect = panel.getBoundingClientRect();
- setDraggedPosition(clampFloatingPanelPosition(
- {
- left: event.clientX - dragState.offsetX,
- top: event.clientY - dragState.offsetY,
- },
- { height: panelRect.height, width: panelRect.width },
- getViewportSize(),
- AI_ASSISTANT_VIEWPORT_GAP,
- ));
- }, []);
-
- const handleDragEnd = useCallback((event: PointerEvent) => {
- if (dragStateRef.current?.pointerId !== event.pointerId) return;
- dragStateRef.current = null;
- setIsDragging(false);
- }, []);
-
- useEffect(() => {
- if (!open) return;
- const handleWindowBlur = () => {
- dragStateRef.current = null;
- setIsDragging(false);
- };
- window.addEventListener("pointermove", handleDragMove);
- window.addEventListener("pointerup", handleDragEnd);
- window.addEventListener("pointercancel", handleDragEnd);
- window.addEventListener("blur", handleWindowBlur);
- return () => {
- window.removeEventListener("pointermove", handleDragMove);
- window.removeEventListener("pointerup", handleDragEnd);
- window.removeEventListener("pointercancel", handleDragEnd);
- window.removeEventListener("blur", handleWindowBlur);
- };
- }, [handleDragEnd, handleDragMove, open]);
-
- const chatting = mode === "ask";
- const openDiagram = useMemo(() => parseDiagramDocument(contentMarkdown), [contentMarkdown]);
- const selectMode = (next: AiAssistantMode) => {
- setMode(next);
- writeStoredAiAssistantMode(next);
- };
- const panelStyle = useMemo<CSSProperties>(() => {
- const { height: viewportHeight, width: viewportWidth } = viewportSize;
- if (draggedPosition) {
- return {
- left: draggedPosition.left,
- maxHeight: Math.max(0, Math.min(viewportHeight * 0.7, viewportHeight - draggedPosition.top - AI_ASSISTANT_VIEWPORT_GAP)),
- top: draggedPosition.top,
- };
- }
- const panelWidth = Math.min(576, Math.max(0, viewportWidth - AI_ASSISTANT_VIEWPORT_GAP * 2));
- return resolveAnchoredFloatingPanelLayout(
- anchor,
- panelWidth,
- { height: viewportHeight, width: viewportWidth },
- AI_ASSISTANT_VIEWPORT_GAP,
- );
- }, [anchor, draggedPosition, viewportSize]);
-
- return (
- <>
- {open && typeof document !== "undefined" ? createPortal(
- <section
- ref={assignPanelRef}
- aria-label={t("aiAssistant.title")}
- className="fixed z-[70] flex max-h-[70dvh] w-[min(36rem,calc(100vw-1.5rem))] flex-col overflow-hidden rounded-xl border border-slate-200 bg-card p-4 shadow-2xl ring-1 ring-slate-950/5"
- role="dialog"
- style={panelStyle}
- onKeyDown={(event) => {
- if (event.key === "Escape") {
- event.preventDefault();
- event.stopPropagation();
- onOpenChange(false);
- }
- }}
- >
- <div
- className={cn(
- "-mx-4 -mt-4 mb-3 flex h-16 shrink-0 touch-none select-none items-center justify-between gap-3 px-4 cursor-grab",
- isDragging && "cursor-grabbing",
- )}
- data-ai-assistant-drag-handle="true"
- onPointerDown={handleDragStart}
- >
- <div className="flex min-w-0 flex-1 items-center gap-2">
- <Sparkles className="h-5 w-5 shrink-0 text-slate-700" />
- <span className="truncate text-sm font-semibold text-slate-950">{t("aiAssistant.title")}</span>
- <span className="shrink-0 rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-700">
- {t(chatting
- ? "aiAssistant.workspaceScope"
- : usesComposerAsSource
- ? "aiAssistant.inputScope"
- : hasSelection ? "aiAssistant.selectedScope" : "aiAssistant.noteScope")}
- </span>
- <GripHorizontal aria-hidden="true" className="ml-auto h-4 w-4 shrink-0 text-slate-300" />
- </div>
- <Button type="button" size="icon" variant="ghost" className="h-8 w-8 shrink-0 cursor-pointer" aria-label={t("common.close")} onClick={() => onOpenChange(false)}>
- <X className="h-4 w-4" />
- </Button>
- </div>
- <div className="mb-3 flex shrink-0 gap-1 rounded-lg bg-slate-100 p-1" role="tablist" aria-label={t("aiAssistant.title")}>
- {(["instruction", "ask"] as const).map((item) => (
- <button
- key={item}
- type="button"
- role="tab"
- aria-selected={mode === item}
- className={cn(
- "h-8 flex-1 rounded-md px-2 text-xs font-medium",
- mode === item ? "bg-card text-slate-900 shadow-sm" : "text-slate-500 hover:text-slate-800",
- )}
- onClick={() => selectMode(item)}
- >
- {t(`aiAssistant.modes.${item}`)}
- </button>
- ))}
- </div>
- {chatting ? (
- <CompanionChat
- available={companionAvailable}
- focus={{
- memoId,
- notebookId,
- notebookTitle,
- title,
- selectionMarkdown,
- ...(openDiagram
- ? { diagramKind: openDiagram.kind }
- : contentMarkdown.trim()
- ? {
- contentMarkdown: contentMarkdown.trim().slice(0, 4000),
- ...(contentMarkdown.trim().length > 4000 ? { contentTruncated: true } : {}),
- }
- : {}),
- }}
- placeholder={t("aiAssistant.modes.askPlaceholder")}
- beforeApply={beforeCompanionApply ?? (async () => undefined)}
- onNotesChanged={onCompanionNotesChanged ?? (async () => undefined)}
- onOpenNote={onOpenCompanionNote ?? (() => undefined)}
- />
- ) : (
- <>
- <div className="min-h-0 flex-1 overflow-y-auto pr-1">
- <div className="grid gap-4">
- {hasSelection ? (
- <p className="max-h-12 overflow-hidden whitespace-pre-wrap border-l-2 border-slate-300 pl-3 text-xs leading-5 text-slate-500">
- {selectionMarkdown}
- </p>
- ) : null}
- {showInstructionComposer || showSourceComposer ? (
- <div className="grid gap-2">
- <label className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
- {t(showInstructionComposer
- ? (hasSelection ? "aiAssistant.customInstructionSelected" : "aiAssistant.customInstruction")
- : "aiAssistant.inputContent")}
- <textarea
- ref={instructionRef}
- className="min-h-24 resize-y rounded-md border border-slate-200 bg-card px-3 py-2 text-sm font-normal text-slate-900 outline-none focus:border-slate-900 focus:ring-2 focus:ring-slate-900/10"
- value={customInstruction}
- onChange={(event) => {
- handleComposerChange(event.target.value);
- }}
- onCompositionEnd={(event) => {
- handleComposerChange(event.currentTarget.value);
- }}
- onKeyDown={(event) => {
- if (
- event.key !== "Enter"
- || event.shiftKey
- || event.nativeEvent.isComposing
- || isGenerating
- || isReadingAttachments
- ) {
- return;
- }
- event.preventDefault();
- void generate();
- }}
- placeholder={t(showInstructionComposer
- ? (hasSelection
- ? "aiAssistant.customInstructionSelectedPlaceholder"
- : "aiAssistant.customInstructionPlaceholder")
- : "aiAssistant.inputContentEmptyPlaceholder")}
- maxLength={2_000}
- />
- </label>
- </div>
- ) : null}
- <div className="grid gap-2">
- <div className="flex items-center justify-between gap-3">
- <span className="text-xs font-normal leading-5 text-slate-700">{t("aiAssistant.actionLabel")}</span>
- {onOpenPromptLibrary ? (
- <button
- type="button"
- className="inline-flex items-center gap-1 text-xs font-medium text-slate-800 hover:text-slate-950"
- onClick={() => {
- onOpenChange(false);
- onOpenPromptLibrary();
- }}
- >
- <Library className="h-3.5 w-3.5" />
- {t("aiAssistant.managePrompts")}
- </button>
- ) : null}
- </div>
- <div className="grid min-w-0 grid-cols-1 gap-2 sm:grid-cols-[minmax(0,1fr)_auto]">
- <Select value={selectValue} onValueChange={handleActionChange}>
- <SelectTrigger aria-label={t("aiAssistant.actionLabel")} className={AI_ASSISTANT_SELECT_TRIGGER_CLASSNAME}>
- <SelectValue placeholder={t("aiAssistant.actionLabel")} />
- </SelectTrigger>
- <SelectContent
- className="z-[80] max-h-[min(20rem,var(--radix-select-content-available-height))]"
- collisionBoundary={panelElement}
- collisionPadding={8}
- sideOffset={6}
- data-edgeever-ai-assistant-layer="true"
- >
- {prompts.length ? (
- <SelectGroup>
- <SelectLabel>{t("aiAssistant.myPrompts")}</SelectLabel>
- {prompts.map((prompt) => (
- <SelectItem key={prompt.id} value={promptSelectValue(prompt.id)}>
- {prompt.name}
- </SelectItem>
- ))}
- </SelectGroup>
- ) : null}
- <SelectGroup>
- <SelectItem value={FREEFORM_VALUE}>{t("aiAssistant.actions.custom")}</SelectItem>
- </SelectGroup>
- </SelectContent>
- </Select>
- <div className="grid min-w-0 grid-cols-2 gap-2 sm:grid-cols-[8rem_7rem]">
- <Button
- type="button"
- variant="outline"
- className={cn(
- "h-10 min-w-0 w-full gap-1.5 whitespace-nowrap px-3 text-sm font-medium",
- isFreeformCustom && "border-slate-900 bg-slate-100 text-slate-950 hover:bg-slate-200 hover:text-slate-950",
- )}
- onClick={() => handleActionChange(FREEFORM_VALUE)}
- >
- <PenLine className="h-3.5 w-3.5 shrink-0" />
- {t("aiAssistant.useCustom")}
- </Button>
- {isGenerating ? (
- <Button type="button" variant="solid" className="h-10 min-w-0 w-full gap-1.5 whitespace-nowrap px-3 text-sm font-semibold" onClick={() => controllerRef.current?.abort()}>
- <Square className="h-3.5 w-3.5 shrink-0" />{t("aiAssistant.stop")}
- </Button>
- ) : (
- <Button type="button" variant="solid" className="h-10 min-w-0 w-full gap-1.5 whitespace-nowrap px-3 text-sm font-semibold" disabled={generateDisabled} onClick={() => void generate()}>
- <Sparkles className="h-4 w-4 shrink-0" />
- {t("aiAssistant.generate")}
- <kbd aria-hidden="true" className="ml-0.5 rounded bg-card/10 px-1 py-0.5 text-xs font-medium leading-none text-white/65">
- ↵
- </kbd>
- </Button>
- )}
- </div>
- </div>
- </div>
- {promptNeedsTargetLanguage(effectiveParameterKind) ? (
- <div className="grid gap-1.5">
- <span className="text-xs font-normal leading-5 text-slate-700">{t("aiAssistant.targetLanguage")}</span>
- <Select value={targetLanguage} onValueChange={(value) => {
- const nextTargetLanguage = value as TargetLanguage;
- setTargetLanguage(nextTargetLanguage);
- persistLastAction({
- nextAction: action,
- nextPrompt: selectedPrompt,
- nextPromptId: selectedPromptId,
- nextTargetLanguage,
- });
- clearResult();
- }}>
- <SelectTrigger aria-label={t("aiAssistant.targetLanguage")} className={AI_ASSISTANT_SELECT_TRIGGER_CLASSNAME}>
- <SelectValue />
- </SelectTrigger>
- <SelectContent
- className="z-[80] max-h-[min(20rem,var(--radix-select-content-available-height))]"
- collisionBoundary={panelElement}
- collisionPadding={8}
- sideOffset={6}
- data-edgeever-ai-assistant-layer="true"
- >
- {targetLanguages.map((language) => (
- <SelectItem key={language} value={language}>{t(`aiAssistant.targetLanguages.${language}`)}</SelectItem>
- ))}
- </SelectContent>
- </Select>
- </div>
- ) : null}
- {promptNeedsTone(effectiveParameterKind) ? (
- <div className="grid gap-1.5">
- <span className="text-xs font-normal leading-5 text-slate-700">{t("aiAssistant.tone")}</span>
- <Select value={tone} onValueChange={(value) => {
- const nextTone = value as AiTone;
- setTone(nextTone);
- persistLastAction({
- nextAction: action,
- nextPrompt: selectedPrompt,
- nextPromptId: selectedPromptId,
- nextTone,
- });
- clearResult();
- }}>
- <SelectTrigger aria-label={t("aiAssistant.tone")} className={AI_ASSISTANT_SELECT_TRIGGER_CLASSNAME}><SelectValue /></SelectTrigger>
- <SelectContent
- className="z-[80] max-h-[min(20rem,var(--radix-select-content-available-height))]"
- collisionBoundary={panelElement}
- collisionPadding={8}
- sideOffset={6}
- data-edgeever-ai-assistant-layer="true"
- >
- {aiTones.map((item) => <SelectItem key={item} value={item}>{t(`aiAssistant.tones.${item}`)}</SelectItem>)}
- </SelectContent>
- </Select>
- </div>
- ) : null}
- <div className="grid gap-2">
- <div className="flex flex-wrap gap-2">
- <input
- ref={attachmentInputRef}
- className="sr-only"
- type="file"
- multiple
- accept={AI_ATTACHMENT_ACCEPT}
- onChange={(event) => void addAttachments(Array.from(event.target.files ?? []))}
- />
- <Button
- type="button"
- variant="outline"
- size="sm"
- disabled={isGenerating || isReadingAttachments}
- onClick={() => attachmentInputRef.current?.click()}
- >
- {isReadingAttachments
- ? <Loader2 className="h-3.5 w-3.5 animate-spin" />
- : <Paperclip className="h-3.5 w-3.5" />}
- {t("aiAssistant.addAttachment")}
- </Button>
- {canSaveAsPrompt ? (
- <Button
- type="button"
- variant="outline"
- size="sm"
- onClick={() => {
- setSaveName("");
- setSaveDescription("");
- setSaveDialogOpen(true);
- createPromptMutation.reset();
- }}
- >
- <BookmarkPlus className="h-3.5 w-3.5" />
- {t("aiAssistant.saveAsPrompt")}
- </Button>
- ) : null}
- </div>
- {attachments.length ? (
- <ul className="flex flex-wrap gap-2" aria-label={t("aiAssistant.attachments")}>
- {attachments.map((attachment, index) => (
- <li key={`${attachment.filename}-${index}`} className="flex min-w-0 max-w-full items-center gap-1.5 rounded-md bg-slate-100 px-2 py-1 text-xs text-slate-700">
- <FileText className="h-3.5 w-3.5 shrink-0 text-slate-500" />
- <span className="max-w-48 truncate">{attachment.filename}</span>
- <span className="shrink-0 text-slate-400">{formatAiAttachmentSize(attachment.byteLength)}</span>
- <button
- type="button"
- className="ml-0.5 rounded p-0.5 text-slate-400 hover:bg-slate-200 hover:text-slate-700 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-900/20"
- aria-label={t("aiAssistant.removeAttachment", { name: attachment.filename })}
- onClick={() => {
- setAttachments((current) => current.filter((_, itemIndex) => itemIndex !== index));
- setAttachmentError(null);
- clearResult();
- }}
- >
- <X className="h-3.5 w-3.5" />
- </button>
- </li>
- ))}
- </ul>
- ) : null}
- <p className="text-xs text-slate-400">{t("aiAssistant.attachmentHint")}</p>
- {attachmentError ? <p className="text-xs font-medium text-rose-600" role="alert">{attachmentError}</p> : null}
- {promptFeedback ? <p className="text-xs font-medium text-emerald-700">{promptFeedback}</p> : null}
- {promptErrorMessage ? <p className="text-xs font-medium text-rose-600" role="alert">{promptErrorMessage}</p> : null}
- </div>
- <div className="grid gap-1.5">
- <div className="flex items-center justify-between gap-3">
- <span className="text-xs font-normal leading-5 text-slate-700">{t("aiAssistant.result")}</span>
- {isGenerating ? (
- <span className="flex items-center gap-1.5 text-xs font-medium text-slate-600">
- <Loader2 className="h-3.5 w-3.5 animate-spin" />{t("aiAssistant.generating")}
- </span>
- ) : null}
- </div>
- <div
- className={cn(
- "max-h-56 overflow-y-auto whitespace-pre-wrap rounded-lg border p-4 text-sm leading-6",
- error
- ? "border-rose-200 bg-rose-50 text-rose-700"
- : "min-h-28 border-slate-200 bg-slate-50 text-slate-800",
- )}
- aria-busy={isGenerating}
- aria-live="polite"
- data-testid="ai-assistant-result"
- >
- {error ? (
- <div className="flex items-start gap-2 font-medium" role="alert">
- <AlertCircle className="mt-0.5 h-4 w-4 shrink-0" />
- <span>{error}</span>
- </div>
- ) : output || <span className="text-slate-400">{t("aiAssistant.resultPlaceholder")}</span>}
- </div>
- </div>
- {output && !isGenerating ? (
- <div className="grid gap-1.5 rounded-lg border border-slate-200 bg-card p-3">
- <span className="text-xs font-normal leading-5 text-slate-700">{t("aiAssistant.refine")}</span>
- <div className="flex flex-col gap-2 sm:flex-row">
- <input
- className="h-10 min-w-0 flex-1 rounded-md border border-slate-200 bg-card px-3 text-sm outline-none focus:border-slate-900 focus:ring-2 focus:ring-slate-900/10"
- value={refinement}
- onChange={(event) => setRefinement(event.target.value)}
- aria-label={t("aiAssistant.refine")}
- onKeyDown={(event) => {
- if (event.key === "Enter" && !event.nativeEvent.isComposing && !isGenerating && refinement.trim()) {
- event.preventDefault();
- void refine();
- }
- }}
- placeholder={t("aiAssistant.refinePlaceholder")}
- maxLength={2_000}
- />
- <Button type="button" variant="outline" disabled={isGenerating || !refinement.trim()} onClick={() => void refine()}>{t("aiAssistant.refineAction")}</Button>
- </div>
- </div>
- ) : null}
- </div>
- </div>
- {output ? (
- <div className="mt-3 flex shrink-0 flex-wrap justify-between gap-2 border-t border-slate-200 pt-3" data-ai-assistant-actions>
- <div className="flex flex-wrap gap-2">
- <Button type="button" variant="outline" disabled={isGenerating} onClick={() => void copy()}>{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}{t(copied ? "aiAssistant.copied" : "aiAssistant.copy")}</Button>
- <Button type="button" variant="outline" disabled={isGenerating} onClick={() => { setOutput(""); setError(null); }}><Trash2 className="h-4 w-4" />{t("aiAssistant.discard")}</Button>
- <Button type="button" variant="outline" disabled={isGenerating} onClick={() => void retry()}><RefreshCw className="h-4 w-4" />{t("aiAssistant.retry")}</Button>
- </div>
- <div className="flex flex-wrap gap-2">
- {!usesComposerAsSource && promptAllowsReplace(effectiveResultMode) ? (
- <Button type="button" variant={hasSelection ? "solid" : "outline"} disabled={isGenerating} onClick={() => applyOutput("replace")}>
- {t(hasSelection ? "aiAssistant.replaceSelection" : "aiAssistant.replace")}
- </Button>
- ) : null}
- {promptAllowsAppend(effectiveResultMode) ? (
- <Button type="button" variant={hasSelection && !usesComposerAsSource && promptAllowsReplace(effectiveResultMode) ? "outline" : "solid"} disabled={isGenerating} onClick={() => applyOutput("append")}>{t("aiAssistant.append")}</Button>
- ) : null}
- </div>
- </div>
- ) : null}
- </>
- )}
- </section>,
- document.body,
- ) : null}
-
- <Dialog open={saveDialogOpen} onOpenChange={(nextOpen) => {
- setSaveDialogOpen(nextOpen);
- if (!nextOpen) {
- setSaveName("");
- setSaveDescription("");
- createPromptMutation.reset();
- }
- }}>
- <DialogContent className="sm:max-w-md" data-edgeever-ai-assistant-layer="true">
- <form
- className="grid gap-4"
- onSubmit={(event: FormEvent) => {
- event.preventDefault();
- if (!saveName.trim() || !customInstruction.trim()) return;
- createPromptMutation.mutate();
- }}
- >
- <DialogHeader>
- <DialogTitle>{t("aiAssistant.saveAsPromptTitle")}</DialogTitle>
- <DialogDescription>{t("aiPrompts.description")}</DialogDescription>
- </DialogHeader>
- <label className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
- {t("aiAssistant.promptName")}
- <Input
- value={saveName}
- onChange={(event) => setSaveName(event.target.value)}
- placeholder={t("aiAssistant.promptNamePlaceholder")}
- maxLength={80}
- required
- autoFocus
- />
- </label>
- <label className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
- {t("aiAssistant.promptDescription")}
- <Input
- value={saveDescription}
- onChange={(event) => setSaveDescription(event.target.value)}
- placeholder={t("aiPrompts.descriptionPlaceholder")}
- maxLength={200}
- />
- </label>
- {createPromptMutation.error ? (
- <p className="text-xs font-medium text-rose-600" role="alert">
- {createPromptMutation.error instanceof Error
- ? createPromptMutation.error.message
- : t("aiAssistant.promptSaveFailed")}
- </p>
- ) : null}
- <DialogFooter>
- <Button type="button" variant="outline" onClick={() => setSaveDialogOpen(false)}>
- {t("common.cancel")}
- </Button>
- <Button
- type="submit"
- variant="solid"
- disabled={!saveName.trim() || !customInstruction.trim() || createPromptMutation.isPending}
- >
- {createPromptMutation.isPending ? <Loader2 className="h-4 w-4 animate-spin" /> : <BookmarkPlus className="h-4 w-4" />}
- {t("aiPrompts.create")}
- </Button>
- </DialogFooter>
- </form>
- </DialogContent>
- </Dialog>
- </>
- );
-};
diff --git a/apps/web/src/components/editor/EditorPaneChrome.tsx b/apps/web/src/components/editor/EditorPaneChrome.tsx
index ac135439..c6bc8df2 100644
--- a/apps/web/src/components/editor/EditorPaneChrome.tsx
+++ b/apps/web/src/components/editor/EditorPaneChrome.tsx
@@ -21,11 +21,19 @@ import type { ResourceMenuTarget } from "./useEditorResourceActions";
import { attachmentResourceMenuPosition } from "./attachment-resource-menu";
import { imageResourceMenuPosition } from "./image-resource-menu-position";
-export const IconTooltip = ({ label, children }: { label: string; children: ReactNode }) => (
+export const IconTooltip = ({
+ label,
+ side = "bottom",
+ children,
+}: {
+ label: string;
+ side?: "top" | "right" | "bottom" | "left";
+ children: ReactNode;
+}) => (
<TooltipProvider delayDuration={0} skipDelayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>{children}</TooltipTrigger>
- <TooltipContent side="bottom">{label}</TooltipContent>
+ <TooltipContent side={side}>{label}</TooltipContent>
</Tooltip>
</TooltipProvider>
);
diff --git a/apps/web/src/components/settings/DesktopAcpAgentCard.tsx b/apps/web/src/components/settings/DesktopAcpAgentCard.tsx
new file mode 100644
index 00000000..f343fa26
--- /dev/null
+++ b/apps/web/src/components/settings/DesktopAcpAgentCard.tsx
@@ -0,0 +1,210 @@
+import { useEffect, useState } from "react";
+import { useTranslation } from "react-i18next";
+import { Bot, Info } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
+import { Input } from "@/components/ui/input";
+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
+import {
+ AI_SIDEBAR_ADAPTER_KEY,
+ AI_SIDEBAR_ADAPTER_PATH_KEY,
+ AI_SIDEBAR_SOURCE_KEY,
+ listDesktopAcpAdapters,
+ probeDesktopAcpAdapter,
+ type AiSidebarSource,
+ type DesktopAcpAdapter,
+ type DesktopAcpAdapterId,
+} from "@/lib/desktop-acp";
+import { cn } from "@/lib/utils";
+import {
+ SETTINGS_CARD_DESCRIPTION_CLASSNAME,
+ SETTINGS_CARD_HEADER_CLASSNAME,
+ SETTINGS_CARD_ICON_CLASSNAME,
+ SETTINGS_CARD_TITLE_CLASSNAME,
+ SETTINGS_ITEM_DESCRIPTION_CLASSNAME,
+ SETTINGS_ITEM_TITLE_CLASSNAME,
+} from "./settings-ui";
+
+const adapterIds: DesktopAcpAdapterId[] = ["codex", "antigravity"];
+
+const readSource = (): AiSidebarSource => (
+ localStorage.getItem(AI_SIDEBAR_SOURCE_KEY) === "local" ? "local" : "builtin"
+);
+
+const readAdapterId = (): DesktopAcpAdapterId => (
+ localStorage.getItem(AI_SIDEBAR_ADAPTER_KEY) === "antigravity" ? "antigravity" : "codex"
+);
+
+const statusKey = (adapter: DesktopAcpAdapter | undefined, probing: boolean) => {
+ if (probing) return "aiAssistant.agentSource.probing";
+ if (!adapter || adapter.detail === "not_probed") return "aiAssistant.agentSource.notProbed";
+ if (adapter.detail === "invalid_path") return "aiAssistant.agentSource.invalidPath";
+ return `aiAssistant.agentSource.states.${adapter.state}`;
+};
+
+const DesktopAcpAgentCardBody = () => {
+ const { t } = useTranslation();
+ const [source, setSource] = useState<AiSidebarSource>("builtin");
+ const [adapterId, setAdapterId] = useState<DesktopAcpAdapterId>("codex");
+ const [adapterPath, setAdapterPath] = useState("");
+ const [ready, setReady] = useState(false);
+ const [listed, setListed] = useState<DesktopAcpAdapter[]>([]);
+ const [probed, setProbed] = useState<DesktopAcpAdapter | null>(null);
+ const [probing, setProbing] = useState(false);
+
+ useEffect(() => {
+ setSource(readSource());
+ setAdapterId(readAdapterId());
+ setAdapterPath(localStorage.getItem(AI_SIDEBAR_ADAPTER_PATH_KEY) ?? "");
+ setReady(true);
+ }, []);
+
+ useEffect(() => {
+ if (!ready) return;
+ localStorage.setItem(AI_SIDEBAR_SOURCE_KEY, source);
+ localStorage.setItem(AI_SIDEBAR_ADAPTER_KEY, adapterId);
+ localStorage.setItem(AI_SIDEBAR_ADAPTER_PATH_KEY, adapterPath);
+ }, [adapterId, adapterPath, ready, source]);
+
+ useEffect(() => {
+ let cancelled = false;
+ void listDesktopAcpAdapters().then((adapters) => {
+ if (!cancelled) setListed(adapters);
+ }).catch(() => {});
+ return () => {
+ cancelled = true;
+ };
+ }, []);
+
+ const shown = probed?.id === adapterId ? probed : listed.find((adapter) => adapter.id === adapterId);
+ const status = t(statusKey(shown, probing));
+
+ const selectAdapter = (id: DesktopAcpAdapterId) => {
+ setAdapterId(id);
+ setProbed(null);
+ };
+
+ const probe = async () => {
+ setProbing(true);
+ try {
+ const result = await probeDesktopAcpAdapter({
+ id: adapterId,
+ ...(adapterId === "antigravity" ? { path: adapterPath.trim() } : {}),
+ });
+ setProbed(result);
+ } catch {
+ setProbed({ id: adapterId, label: adapterId === "codex" ? "Codex" : "Antigravity", state: "failed" });
+ } finally {
+ setProbing(false);
+ }
+ };
+
+ return (
+ <Card className="w-full min-w-0 overflow-hidden shadow-none">
+ <CardHeader className={SETTINGS_CARD_HEADER_CLASSNAME}>
+ <CardTitle className={SETTINGS_CARD_TITLE_CLASSNAME}>
+ <Bot className={SETTINGS_CARD_ICON_CLASSNAME} />
+ {t("aiAssistant.agentSource.title")}
+ </CardTitle>
+ <CardDescription className={SETTINGS_CARD_DESCRIPTION_CLASSNAME}>
+ {t("aiAssistant.agentSource.description")}
+ </CardDescription>
+ </CardHeader>
+ <CardContent className="grid gap-4 p-4 pt-0">
+ <div role="radiogroup" aria-label={t("aiAssistant.agentSource.title")} className="overflow-hidden rounded-lg border border-slate-200/70 divide-y divide-slate-200/70">
+ {(["builtin", "local"] as const).map((option) => {
+ const checked = source === option;
+ return (
+ <label key={option} className={cn("flex cursor-pointer items-start gap-3 px-3.5 py-2.5", checked && "bg-slate-50/80")}>
+ <input
+ className="mt-0.5"
+ type="radio"
+ name="edgeever-acp-source"
+ value={option}
+ checked={checked}
+ onChange={() => setSource(option)}
+ />
+ <span className="min-w-0">
+ <span className={SETTINGS_ITEM_TITLE_CLASSNAME}>{t(`aiAssistant.agentSource.${option}`)}</span>
+ <span className={cn(SETTINGS_ITEM_DESCRIPTION_CLASSNAME, "block")}>{t(`aiAssistant.agentSource.${option}Hint`)}</span>
+ </span>
+ </label>
+ );
+ })}
+ </div>
+
+ {source === "local" ? (
+ <div className="grid gap-3">
+ <div role="radiogroup" aria-label={t("aiAssistant.agentSource.adapter")} className="flex flex-wrap gap-2">
+ {adapterIds.map((id) => {
+ const checked = adapterId === id;
+ return (
+ <label
+ key={id}
+ className={cn(
+ "flex h-8 cursor-pointer items-center gap-2 rounded-md border px-2.5 text-xs",
+ checked ? "border-slate-900 bg-card text-slate-950" : "border-slate-200 text-slate-600"
+ )}
+ >
+ <input
+ className="sr-only"
+ type="radio"
+ name="edgeever-acp-adapter"
+ value={id}
+ checked={checked}
+ onChange={() => selectAdapter(id)}
+ />
+ <span aria-hidden="true" className={cn("h-1.5 w-1.5 rounded-full", checked ? "bg-slate-950" : "bg-slate-400")} />
+ {t(`aiAssistant.agentSource.${id}`)}
+ </label>
+ );
+ })}
+ </div>
+
+ {adapterId === "antigravity" ? (
+ <label className="grid gap-1.5 text-xs font-normal leading-5 text-slate-700">
+ <span className="flex items-center gap-1.5">
+ {t("aiAssistant.agentSource.pathLabel")}
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <button type="button" className="text-slate-400" aria-label={t("aiAssistant.agentSource.pathHint")}>
+ <Info className="h-3.5 w-3.5" />
+ </button>
+ </TooltipTrigger>
+ <TooltipContent className="max-w-72">{t("aiAssistant.agentSource.pathHint")}</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ </span>
+ <Input
+ className="h-8 text-xs"
+ value={adapterPath}
+ spellCheck={false}
+ autoCapitalize="off"
+ autoCorrect="off"
+ placeholder={t("aiAssistant.agentSource.pathPlaceholder")}
+ onChange={(event) => {
+ setAdapterPath(event.target.value);
+ setProbed(null);
+ }}
+ />
+ </label>
+ ) : null}
+
+ <div className="flex items-center justify-between gap-3">
+ <p className="text-xs leading-5 text-slate-600" role="status">{status}</p>
+ <Button type="button" variant="outline" size="sm" className="h-8 bg-card text-xs font-normal" disabled={probing} title={t("aiAssistant.agentSource.probeHint")} onClick={() => void probe()}>
+ {probing ? t("aiAssistant.agentSource.probing") : t("aiAssistant.agentSource.probe")}
+ </Button>
+ </div>
+ </div>
+ ) : null}
+ </CardContent>
+ </Card>
+ );
+};
+
+export const DesktopAcpAgentCard = () => {
+ if (typeof window === "undefined" || typeof window.edgeeverDesktop?.listAcpAdapters !== "function") return null;
+ return <DesktopAcpAgentCardBody />;
+};
diff --git a/apps/web/src/components/ui/button.tsx b/apps/web/src/components/ui/button.tsx
index f3acdaff..07219d68 100644
--- a/apps/web/src/components/ui/button.tsx
+++ b/apps/web/src/components/ui/button.tsx
@@ -23,6 +23,8 @@ const buttonVariants = cva(
sm: "h-8 gap-1.5 px-2.5 text-xs",
md: "h-9 gap-2 px-3 text-sm",
icon: "h-8 w-8 p-0",
+ "icon-sm": "size-7 p-0",
+ "icon-xs": "size-6 p-0",
},
},
defaultVariants: {
diff --git a/apps/web/src/components/ui/hover-card.tsx b/apps/web/src/components/ui/hover-card.tsx
new file mode 100644
index 00000000..74efa6be
--- /dev/null
+++ b/apps/web/src/components/ui/hover-card.tsx
@@ -0,0 +1,29 @@
+"use client"
+
+import * as React from "react"
+import * as HoverCardPrimitive from "@radix-ui/react-hover-card"
+
+import { cn } from "@/lib/utils"
+
+const HoverCard = HoverCardPrimitive.Root
+
+const HoverCardTrigger = HoverCardPrimitive.Trigger
+
+const HoverCardContent = React.forwardRef<
+ React.ElementRef<typeof HoverCardPrimitive.Content>,
+ React.ComponentPropsWithoutRef<typeof HoverCardPrimitive.Content>
+>(({ className, align = "center", sideOffset = 4, ...props }, ref) => (
+ <HoverCardPrimitive.Content
+ ref={ref}
+ align={align}
+ sideOffset={sideOffset}
+ className={cn(
+ "z-50 w-64 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 origin-[--radix-hover-card-content-transform-origin]",
+ className
+ )}
+ {...props}
+ />
+))
+HoverCardContent.displayName = HoverCardPrimitive.Content.displayName
+
+export { HoverCard, HoverCardTrigger, HoverCardContent }
diff --git a/apps/web/src/components/ui/input-group.tsx b/apps/web/src/components/ui/input-group.tsx
new file mode 100644
index 00000000..07773992
--- /dev/null
+++ b/apps/web/src/components/ui/input-group.tsx
@@ -0,0 +1,170 @@
+"use client"
+
+import * as React from "react"
+import { cva, type VariantProps } from "class-variance-authority"
+
+import { cn } from "@/lib/utils"
+import { Button } from "@/components/ui/button"
+import { Input } from "@/components/ui/input"
+import { Textarea } from "@/components/ui/textarea"
+
+function InputGroup({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="input-group"
+ role="group"
+ className={cn(
+ "group/input-group border-input dark:bg-input/30 shadow-xs relative flex w-full items-center rounded-md border outline-none transition-[color,box-shadow]",
+ "h-9 has-[>textarea]:h-auto",
+
+ // Variants based on alignment.
+ "has-[>[data-align=inline-start]]:[&>input]:pl-2",
+ "has-[>[data-align=inline-end]]:[&>input]:pr-2",
+ "has-[>[data-align=block-start]]:h-auto has-[>[data-align=block-start]]:flex-col has-[>[data-align=block-start]]:[&>input]:pb-3",
+ "has-[>[data-align=block-end]]:h-auto has-[>[data-align=block-end]]:flex-col has-[>[data-align=block-end]]:[&>input]:pt-3",
+
+ // Focus state.
+ "has-[[data-slot=input-group-control]:focus-visible]:ring-ring has-[[data-slot=input-group-control]:focus-visible]:ring-1",
+
+ // Error state.
+ "has-[[data-slot][aria-invalid=true]]:ring-destructive/20 has-[[data-slot][aria-invalid=true]]:border-destructive dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40",
+
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+const inputGroupAddonVariants = cva(
+ "text-muted-foreground flex h-auto cursor-text select-none items-center justify-center gap-2 py-1.5 text-sm font-medium group-data-[disabled=true]/input-group:opacity-50 [&>kbd]:rounded-[calc(var(--radius)-5px)] [&>svg:not([class*='size-'])]:size-4",
+ {
+ variants: {
+ align: {
+ "inline-start":
+ "order-first pl-3 has-[>button]:ml-[-0.45rem] has-[>kbd]:ml-[-0.35rem]",
+ "inline-end":
+ "order-last pr-3 has-[>button]:mr-[-0.4rem] has-[>kbd]:mr-[-0.35rem]",
+ "block-start":
+ "[.border-b]:pb-3 order-first w-full justify-start px-3 pt-3 group-has-[>input]/input-group:pt-2.5",
+ "block-end":
+ "[.border-t]:pt-3 order-last w-full justify-start px-3 pb-3 group-has-[>input]/input-group:pb-2.5",
+ },
+ },
+ defaultVariants: {
+ align: "inline-start",
+ },
+ }
+)
+
+function InputGroupAddon({
+ className,
+ align = "inline-start",
+ ...props
+}: React.ComponentProps<"div"> & VariantProps<typeof inputGroupAddonVariants>) {
+ return (
+ <div
+ role="group"
+ data-slot="input-group-addon"
+ data-align={align}
+ className={cn(inputGroupAddonVariants({ align }), className)}
+ onClick={(e) => {
+ if ((e.target as HTMLElement).closest("button")) {
+ return
+ }
+ e.currentTarget.parentElement?.querySelector("input")?.focus()
+ }}
+ {...props}
+ />
+ )
+}
+
+const inputGroupButtonVariants = cva(
+ "flex items-center gap-2 text-sm shadow-none",
+ {
+ variants: {
+ size: {
+ xs: "h-6 gap-1 rounded-[calc(var(--radius)-5px)] px-2 has-[>svg]:px-2 [&>svg:not([class*='size-'])]:size-3.5",
+ sm: "h-8 gap-1.5 rounded-md px-2.5 has-[>svg]:px-2.5",
+ "icon-xs":
+ "size-6 rounded-[calc(var(--radius)-5px)] p-0 has-[>svg]:p-0",
+ "icon-sm": "size-8 p-0 has-[>svg]:p-0",
+ },
+ },
+ defaultVariants: {
+ size: "xs",
+ },
+ }
+)
+
+function InputGroupButton({
+ className,
+ type = "button",
+ variant = "ghost",
+ size = "xs",
+ ...props
+}: Omit<React.ComponentProps<typeof Button>, "size"> &
+ VariantProps<typeof inputGroupButtonVariants>) {
+ return (
+ <Button
+ type={type}
+ data-size={size}
+ variant={variant}
+ className={cn(inputGroupButtonVariants({ size }), className)}
+ {...props}
+ />
+ )
+}
+
+function InputGroupText({ className, ...props }: React.ComponentProps<"span">) {
+ return (
+ <span
+ className={cn(
+ "text-muted-foreground flex items-center gap-2 text-sm [&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function InputGroupInput({
+ className,
+ ...props
+}: React.ComponentProps<"input">) {
+ return (
+ <Input
+ data-slot="input-group-control"
+ className={cn(
+ "flex-1 rounded-none border-0 bg-transparent shadow-none focus-visible:ring-0 dark:bg-transparent",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function InputGroupTextarea({
+ className,
+ ...props
+}: React.ComponentProps<"textarea">) {
+ return (
+ <Textarea
+ data-slot="input-group-control"
+ className={cn(
+ "flex-1 resize-none rounded-none border-0 bg-transparent py-3 shadow-none focus-visible:ring-0 dark:bg-transparent",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+export {
+ InputGroup,
+ InputGroupAddon,
+ InputGroupButton,
+ InputGroupText,
+ InputGroupInput,
+ InputGroupTextarea,
+}
diff --git a/apps/web/src/components/ui/textarea.tsx b/apps/web/src/components/ui/textarea.tsx
new file mode 100644
index 00000000..e56b0aff
--- /dev/null
+++ b/apps/web/src/components/ui/textarea.tsx
@@ -0,0 +1,22 @@
+import * as React from "react"
+
+import { cn } from "@/lib/utils"
+
+const Textarea = React.forwardRef<
+ HTMLTextAreaElement,
+ React.ComponentProps<"textarea">
+>(({ className, ...props }, ref) => {
+ return (
+ <textarea
+ className={cn(
+ "flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
+ className
+ )}
+ ref={ref}
+ {...props}
+ />
+ )
+})
+Textarea.displayName = "Textarea"
+
+export { Textarea }
diff --git a/apps/web/src/lib/dark-theme.test.mjs b/apps/web/src/lib/dark-theme.test.mjs
index 5ab980a7..f97c879d 100644
--- a/apps/web/src/lib/dark-theme.test.mjs
+++ b/apps/web/src/lib/dark-theme.test.mjs
@@ -80,7 +80,6 @@ describe("dark theme contracts", () => {
test("dark chrome uses workspace tokens instead of leftover blue-slate", () => {
const css = readFileSync(new URL("../styles/globals.css", import.meta.url), "utf8");
- expect(css).toContain("html.dark[data-edgeever-environment=\"local\"] body::after");
expect(css).toContain("--tooltip-bg: #2c3330;");
expect(css).toContain("--scrollbar-thumb: rgb(137 150 142 / 0.38);");
expect(css).toContain("--search-match: rgb(22 160 110 / 0.32);");
diff --git a/apps/web/src/lib/desktop-acp.ts b/apps/web/src/lib/desktop-acp.ts
new file mode 100644
index 00000000..b5774101
--- /dev/null
+++ b/apps/web/src/lib/desktop-acp.ts
@@ -0,0 +1,85 @@
+export type DesktopAcpAdapterId = "codex" | "antigravity";
+export type DesktopAcpAdapterState = "not_installed" | "needs_login" | "available" | "failed";
+
+export type DesktopAcpPromptCapabilities = {
+ image?: boolean;
+ embeddedContext?: boolean;
+};
+
+export type DesktopAcpAdapter = {
+ id: DesktopAcpAdapterId;
+ label: string;
+ state: DesktopAcpAdapterState;
+ detail?: string;
+ promptCapabilities?: DesktopAcpPromptCapabilities;
+};
+
+export type DesktopAcpAttachment = {
+ filename: string;
+ mediaType: string;
+ dataBase64: string;
+};
+
+export type DesktopAcpPromptInput = {
+ adapterId: DesktopAcpAdapterId;
+ path?: string;
+ prompt: string;
+ contextText?: string;
+ attachments?: DesktopAcpAttachment[];
+};
+
+export type DesktopAcpPromptResult = {
+ requestId: string;
+ rejectedAttachments?: Array<{ filename: string; reason: string }>;
+};
+
+export type DesktopAcpEvent =
+ | { requestId: string; type: "text-delta"; text: string }
+ | { requestId: string; type: "reasoning"; text: string }
+ | { requestId: string; type: "tool"; name: string; status: string; title?: string }
+ | { requestId: string; type: "done" }
+ | { requestId: string; type: "error"; message: string };
+
+export const AI_SIDEBAR_WIDTH_KEY = "edgeever.aiSidebar.width";
+export const AI_SIDEBAR_OPEN_KEY = "edgeever.aiSidebar.open";
+export const AI_SIDEBAR_SOURCE_KEY = "edgeever.aiSidebar.source";
+export const AI_SIDEBAR_ADAPTER_KEY = "edgeever.aiSidebar.adapterId";
+export const AI_SIDEBAR_ADAPTER_PATH_KEY = "edgeever.aiSidebar.adapterPath";
+
+export type AiSidebarSource = "builtin" | "local";
+
+const bridge = () => (typeof window === "undefined" ? undefined : window.edgeeverDesktop);
+
+export const desktopAcpAvailable = () => Boolean(bridge()?.listAcpAdapters);
+
+export const listDesktopAcpAdapters = async (): Promise<DesktopAcpAdapter[]> => {
+ const desktop = bridge();
+ if (!desktop?.listAcpAdapters) return [];
+ return desktop.listAcpAdapters();
+};
+
+export const probeDesktopAcpAdapter = async (input: { id: DesktopAcpAdapterId; path?: string }): Promise<DesktopAcpAdapter> => {
+ const desktop = bridge();
+ if (!desktop?.probeAcpAdapter) {
+ return { id: input.id, label: input.id, state: "failed", detail: "desktop_unavailable" };
+ }
+ return desktop.probeAcpAdapter(input);
+};
+
+export const promptDesktopAcp = async (input: DesktopAcpPromptInput): Promise<DesktopAcpPromptResult> => {
+ const desktop = bridge();
+ if (!desktop?.promptAcp) throw new Error("desktop_acp_unavailable");
+ return desktop.promptAcp(input);
+};
+
+export const cancelDesktopAcp = async (requestId: string) => {
+ const desktop = bridge();
+ if (!desktop?.cancelAcp) return { ok: true as const };
+ return desktop.cancelAcp(requestId);
+};
+
+export const subscribeDesktopAcp = (callback: (event: DesktopAcpEvent) => void) => {
+ const desktop = bridge();
+ if (!desktop?.onAcpEvent) return () => undefined;
+ return desktop.onAcpEvent(callback);
+};
diff --git a/apps/web/src/lib/motion.ts b/apps/web/src/lib/motion.ts
index 060b61f2..f494323f 100644
--- a/apps/web/src/lib/motion.ts
+++ b/apps/web/src/lib/motion.ts
@@ -14,6 +14,8 @@ export const paneEnterMotion: MotionProps = {
transition: transition(0.18),
};
+export const sidebarRevealTransition: Transition = transition(0.32);
+
export const contentEnterMotion: MotionProps = {
initial: { opacity: 0, y: 4 },
animate: { opacity: 1, y: 0 },
diff --git a/apps/web/src/styles/globals.css b/apps/web/src/styles/globals.css
index 2224f879..ddd06b8b 100644
--- a/apps/web/src/styles/globals.css
+++ b/apps/web/src/styles/globals.css
@@ -198,48 +198,6 @@
animation: edgeever-token-created 2.2s ease-out;
}
-html[data-edgeever-environment="local"] body::after,
-html[data-edgeever-environment="demo"] body::after {
- position: fixed;
- right: 12px;
- bottom: 12px;
- z-index: 2147483647;
- border-radius: 9999px;
- padding: 4px 9px;
- font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
- letter-spacing: 0.08em;
- pointer-events: none;
- box-shadow: 0 2px 8px rgb(15 23 42 / 10%);
-}
-
-html[data-edgeever-environment="local"] body::after {
- content: "LOCAL DEV";
- border: 1px solid rgb(16 185 129 / 45%);
- background: rgb(236 253 245 / 94%);
- color: rgb(4 120 87);
-}
-
-html[data-edgeever-environment="demo"] body::after {
- content: "LOCAL DEMO";
- border: 1px solid rgb(245 158 11 / 50%);
- background: rgb(255 251 235 / 96%);
- color: rgb(180 83 9);
-}
-
-html.dark[data-edgeever-environment="local"] body::after {
- border-color: rgb(22 160 110 / 42%);
- background: rgb(18 42 36 / 92%);
- color: #9ee0c8;
- box-shadow: 0 2px 8px rgb(0 0 0 / 28%);
-}
-
-html.dark[data-edgeever-environment="demo"] body::after {
- border-color: rgb(217 119 6 / 48%);
- background: rgb(69 26 3 / 88%);
- color: #fde68a;
- box-shadow: 0 2px 8px rgb(0 0 0 / 28%);
-}
-
.ProseMirror .tiptap-mathematics-render {
font-size: var(--editor-code-font-size, 0.875em);
}
diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.ts
index 9a26ae58..314b4298 100644
--- a/apps/web/src/vite-env.d.ts
+++ b/apps/web/src/vite-env.d.ts
@@ -91,6 +91,35 @@ interface EdgeEverDesktopBridge {
readWeChatImportMedia?(importId: string, mediaId: string): Promise<{ filename: string; mimeType: string; bytes: Uint8Array }>;
finishWeChatImport?(importId: string, success: boolean): Promise<void>;
retryWeChatImport?(importId: string): Promise<boolean>;
+ listAcpAdapters?(): Promise<Array<{
+ id: "codex" | "antigravity";
+ label: string;
+ state: "not_installed" | "needs_login" | "available" | "failed";
+ detail?: string;
+ promptCapabilities?: { image?: boolean; embeddedContext?: boolean };
+ }>>;
+ probeAcpAdapter?(input: { id: "codex" | "antigravity"; path?: string }): Promise<{
+ id: "codex" | "antigravity";
+ label: string;
+ state: "not_installed" | "needs_login" | "available" | "failed";
+ detail?: string;
+ promptCapabilities?: { image?: boolean; embeddedContext?: boolean };
+ }>;
+ promptAcp?(input: {
+ adapterId: "codex" | "antigravity";
+ path?: string;
+ prompt: string;
+ contextText?: string;
+ attachments?: Array<{ filename: string; mediaType: string; dataBase64: string }>;
+ }): Promise<{ requestId: string; rejectedAttachments?: Array<{ filename: string; reason: string }> }>;
+ cancelAcp?(requestId: string): Promise<{ ok: true }>;
+ onAcpEvent?(callback: (event:
+ | { requestId: string; type: "text-delta"; text: string }
+ | { requestId: string; type: "reasoning"; text: string }
+ | { requestId: string; type: "tool"; name: string; status: string; title?: string }
+ | { requestId: string; type: "done" }
+ | { requestId: string; type: "error"; message: string }
+ ) => void): () => void;
onImportWeChatChat?(callback: (payload: {
ok: boolean;
kind?: "file";
diff --git a/bun.lock b/bun.lock
index ffb44e2a..a5a199ba 100644
--- a/bun.lock
+++ b/bun.lock
@@ -102,11 +102,13 @@
},
"apps/desktop": {
"name": "@edgeever/desktop",
- "version": "1.86.0",
+ "version": "1.90.0",
"dependencies": {
+ "@agentclientprotocol/sdk": "^1.5.1",
"@edgeever/public-network": "workspace:*",
"croner": "^10.0.1",
"electron-updater": "^6.6.2",
+ "zod": "^4.6.5",
},
"devDependencies": {
"electron": "^44.0.0",
@@ -115,7 +117,7 @@
},
"apps/extension": {
"name": "@edgeever/extension",
- "version": "0.1.5",
+ "version": "0.1.8",
"dependencies": {
"@mozilla/readability": "^0.6.0",
"turndown": "^7.2.4",
@@ -227,9 +229,11 @@
"@floating-ui/react-dom": "^2.1.8",
"@fontsource-variable/noto-sans-sc": "^5.2.8",
"@lezer/highlight": "^1.2.3",
+ "@radix-ui/react-hover-card": "^1.1.23",
"@radix-ui/react-separator": "^1.1.15",
"@radix-ui/react-slot": "^1.3.3",
"@radix-ui/react-tooltip": "^1.2.16",
+ "@radix-ui/react-use-controllable-state": "^1.2.6",
"@streamdown/cjk": "^1.0.3",
"@streamdown/code": "^1.1.1",
"@streamdown/math": "^1.0.2",
@@ -246,6 +250,7 @@
"lucide-react": "1.21.0",
"mermaid": "^11.16.0",
"motion": "^12.43.0",
+ "nanoid": "^6.0.1",
"pagedjs": "^0.4.3",
"streamdown": "^2.6.0",
"use-stick-to-bottom": "^1.1.6",
@@ -315,6 +320,8 @@
"react-native": "0.86.3",
},
"packages": {
+ "@agentclientprotocol/sdk": ["@agentclientprotocol/[email protected]", "", { "peerDependencies": { "zod": "^3.25.0 || ^4.0.0" } }, "sha512-nSUIrC1fOR9+ppKfx9KdwjBBND2sAPzFHo4yVQrGbXZHnayUYpwL8CDVR5LSxbapY/oKMOzNP0wvwfyx3PS2KA=="],
+
"@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "4.0.7", "@ai-sdk/provider-utils": "5.0.25" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-Wg5jfray0X4+qkrr73GZ7U7K2JNGx+S+rNY9LorVlXhkhTqnvtNLYWRA1WxSxlCDCw3yjRCJdL9kyc+b7naAog=="],
"@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "4.0.7", "@ai-sdk/provider-utils": "5.0.25", "@vercel/oidc": "3.2.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-LIAO6kAG8fpXQb9L0iwPk1FIbXftvqnyC56v5NEAzeWTeL8fUsy/Hx86VPBTWEDFdwbVprjWifJOAqS6AOj3mA=="],
@@ -1133,6 +1140,8 @@
"@radix-ui/react-focus-scope": ["@radix-ui/[email protected]", "", { "dependencies": { "@radix-ui/react-compose-refs": "1.1.5", "@radix-ui/react-primitive": "2.1.10", "@radix-ui/react-use-callback-ref": "1.1.4" }, "peerDependencies": { "@types/react": "*", "@types/react-dom": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-wmRZ2WWLvmt6KHy2rNPOdPUjwq5xOHY02+m+udwJTn0aNIox/rkskAvJTyTLGhPK6KgrUjlJUJpgmx/+wFiFIQ=="],
+ "@radix-ui/react-hover-card": ["@radix-ui/[email protected]", "", { "dependencies": { "@radix-ui/primitive": "1.1.7", "@radix-ui/react-compose-refs": "1.1.5", "@radix-ui/react-context": "1.2.2", "@radix-ui/react-dismissable-layer": "1.1.19", "@radix-ui/react-popper": "1.3.7", "@radix-ui/react-portal": "1.1.17", "@radix-ui/react-presence": "1.1.10", "@radix-ui/react-primitive": "2.1.10", "@radix-ui/react-use-controllable-state": "1.2.6" }, "peerDependencies": { "@types/react": "*", "@types/react-dom": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-H8qONfZd3ltrU3+jHCIgITbWo6e1iTKvP9DHdrvYbX48ooRM5FjEDTn16AMwdfuOGkWdZEhpl3PLL/Wk/AnHDQ=="],
+
"@radix-ui/react-id": ["@radix-ui/[email protected]", "", { "dependencies": { "@radix-ui/react-use-layout-effect": "1.1.4" }, "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-TMQp2llA+RYn7JcjnrMnz7wN4pcVttPZnRZo52PLQsoLVKzNlVwUeHmfePgTgRluXFvlD3GD5g5MOVVTJCO0qA=="],
"@radix-ui/react-menu": ["@radix-ui/[email protected]", "", { "dependencies": { "@radix-ui/primitive": "1.1.7", "@radix-ui/react-collection": "1.1.15", "@radix-ui/react-compose-refs": "1.1.5", "@radix-ui/react-context": "1.2.2", "@radix-ui/react-direction": "1.1.4", "@radix-ui/react-dismissable-layer": "1.1.19", "@radix-ui/react-focus-guards": "1.1.6", "@radix-ui/react-focus-scope": "1.1.16", "@radix-ui/react-id": "1.1.4", "@radix-ui/react-popper": "1.3.7", "@radix-ui/react-portal": "1.1.17", "@radix-ui/react-presence": "1.1.10", "@radix-ui/react-primitive": "2.1.10", "@radix-ui/react-roving-focus": "1.1.19", "@radix-ui/react-slot": "1.3.3", "@radix-ui/react-use-callback-ref": "1.1.4", "aria-hidden": "^1.2.4", "react-remove-scroll": "^2.7.2" }, "peerDependencies": { "@types/react": "*", "@types/react-dom": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-uW7RVuU6Lp/ZtfeY4b3kL32zccgEWvPv1+cf17ubYzHa9cL8AHokmk36cG/XEiH/smbQvumnieXX9j/e9RqJWA=="],
@@ -3275,7 +3284,7 @@
"multitars": ["[email protected]", "", {}, "sha512-6GwVw5eLi9sThdtlS4PKwC7yRLaf45pYhIEzKBHdKxi+YOXGKFX8acIniH+Uh/+k9mS2lQOupTccjoe5r0/1IQ=="],
- "nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA=="],
+ "nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.js" } }, "sha512-3wVS3i51pE2pi1k5FFL/95BGfVS0kSsvDVuGXHOtxox/TywUmtgq+3qiTOTbs9J7KfHaXPiN171k/A6dBnaXFw=="],
"natural-compare": ["[email protected]", "", {}, "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw=="],
@@ -4347,6 +4356,8 @@
"@edgeever/client/ai": ["[email protected]", "", { "dependencies": { "@ai-sdk/gateway": "4.0.91", "@ai-sdk/provider": "4.0.18", "@ai-sdk/provider-utils": "5.0.47" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-hOowAtiIUXwRqcdVRXll6ToNHYDdfXg+sFF1kYPlNDlgZ63rjR4mp1O29n5lSWZUNcHM1TlSzs30+6/XDUVcAA=="],
+ "@edgeever/desktop/zod": ["[email protected]", "", {}, "sha512-v5l/aFXZQeai4awLbOpSoHecE9UiMrnfx75tEXLjNonXVARxQ5mOeipTjROUchszUNCqnE+hqAMujRsRHsut2Q=="],
+
"@edgeever/mobile/@tanstack/react-query": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-core": "5.102.8" }, "peerDependencies": { "react": "^18 || ^19" } }, "sha512-TYBea4OuXWD7MhaSHq069TWbFe7rcwWN6kzT7JF0OKi1K6c1gTv2IzD6A6ExJsCMozdkqBWeuIUZmu4KQg0O5A=="],
"@edgeever/mobile/react": ["[email protected]", "", {}, "sha512-Ku/hhYbVjOQnXDZFv2+RibmLFGwFdeeKHFcOTlrt7xplBnya5OGn/hIRDsqDiSUcfORsDC7MPxwork8jBwsIWA=="],
@@ -4637,6 +4648,8 @@
"expo-router/@expo/metro-runtime": ["@expo/[email protected]", "", { "dependencies": { "@expo/log-box": "^57.0.4", "anser": "^1.4.9", "pretty-format": "^29.7.0", "stacktrace-parser": "^0.1.10", "whatwg-fetch": "^3.0.0" }, "peerDependencies": { "expo": "*", "react": "*", "react-dom": "*", "react-native": "*" }, "optionalPeers": ["react-dom"] }, "sha512-9RaFkqIzRud6ol9fc5ZKHgNDYeDjQVBdAQC+rjSQ6RiKV0pY+SjDqGmKY5a4M9TaV0qLMCNNrp5R88BqC2xbuQ=="],
+ "expo-router/nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA=="],
+
"expo-updates/chalk": ["[email protected]", "", { "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" } }, "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA=="],
"expo-updates/glob": ["[email protected]", "", { "dependencies": { "minimatch": "^10.2.2", "minipass": "^7.1.3", "path-scurry": "^2.0.2" } }, "sha512-Wjlyrolmm8uDpm/ogGyXZXb1Z+Ca2B8NbJwqBVg0axK9GbBeoS7yGV6vjXnYdGm6X53iehEuxxbyiKp8QmN4Vw=="],
@@ -4773,6 +4786,8 @@
"playwright/fsevents": ["[email protected]", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="],
+ "postcss/nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA=="],
+
"postject/commander": ["[email protected]", "", {}, "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ=="],
"pretty-format/ansi-styles": ["[email protected]", "", {}, "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA=="],
diff --git a/migrations/0057_companion_attachments.sql b/migrations/0057_companion_attachments.sql
new file mode 100644
index 00000000..3bd2e421
--- /dev/null
+++ b/migrations/0057_companion_attachments.sql
@@ -0,0 +1,28 @@
+-- Chat attachments are turn-scoped bytes, not note resources.
+-- Upload happens before a turn exists, so turn_id stays nullable and has no
+-- foreign key. Historical turns keep filenames only, in attachment_meta_json.
+ALTER TABLE companion_turns ADD COLUMN attachment_meta_json TEXT NOT NULL DEFAULT '[]';
+
+CREATE TABLE companion_turn_attachments (
+ id TEXT PRIMARY KEY,
+ workspace_id TEXT NOT NULL,
+ owner_id TEXT NOT NULL,
+ turn_id TEXT,
+ filename TEXT NOT NULL,
+ media_type TEXT NOT NULL,
+ byte_length INTEGER NOT NULL,
+ created_at TEXT NOT NULL,
+ expires_at TEXT NOT NULL
+);
+CREATE INDEX idx_companion_turn_attachments_owner
+ ON companion_turn_attachments(workspace_id, owner_id, expires_at);
+CREATE INDEX idx_companion_turn_attachments_turn
+ ON companion_turn_attachments(turn_id);
+
+CREATE TABLE companion_turn_attachment_parts (
+ attachment_id TEXT NOT NULL,
+ part_index INTEGER NOT NULL,
+ data TEXT NOT NULL,
+ PRIMARY KEY (attachment_id, part_index),
+ FOREIGN KEY (attachment_id) REFERENCES companion_turn_attachments(id) ON DELETE CASCADE
+);
diff --git a/package.json b/package.json
index 5f79fde5..aee83409 100644
--- a/package.json
+++ b/package.json
@@ -63,7 +63,7 @@
"release": "bun scripts/release.mjs",
"publish:stores": "bun scripts/store-delivery.mjs",
"test:desktop:sidecar": "bun scripts/test-desktop-sidecar.mjs",
- "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/app-protocol.test.mjs apps/desktop/src/main/child-process-start.test.mjs apps/desktop/src/main/clipboard-write.test.mjs apps/desktop/src/main/desktop-diagnostics.test.mjs apps/desktop/src/main/desktop-menu.test.mjs apps/desktop/src/main/desktop-update-flow.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/wechat-chat-archive.test.mjs apps/desktop/src/main/wechat-share-import.test.mjs apps/desktop/src/main/share-extension-registration.test.mjs apps/desktop/src/main/windows-share-menu.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/local-data-reset.test.mjs apps/desktop/src/main/ai-direct.test.mjs apps/desktop/src/main/plugin-public-network.test.mjs apps/desktop/src/main/renderer-recovery.test.mjs apps/desktop/src/main/renderer-hibernate.test.mjs apps/desktop/src/main/renderer-startup-guard.test.mjs apps/desktop/src/main/renderer-storage-migration.test.mjs apps/desktop/src/main/resource-request.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/scheduled-task-scheduler.test.mjs apps/desktop/src/main/screenshot-capture.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/staged-resource-alias.test.mjs apps/desktop/src/main/tray-icon.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/desktop/src/main/window-visibility.test.mjs apps/desktop/src/main/windows-update-trust.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/clipboard.test.mjs apps/web/src/lib/desktop-renderer-ready.test.mjs apps/web/src/lib/desktop-workspace-restore.test.mjs apps/web/src/lib/desktop-repository.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/runtime.test.mjs apps/web/src/lib/screenshot-import.test.mjs apps/web/src/lib/wechat-chat-import.test.mjs apps/web/src/lib/shared-file-import.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
+ "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/app-protocol.test.mjs apps/desktop/src/main/child-process-start.test.mjs apps/desktop/src/main/clipboard-write.test.mjs apps/desktop/src/main/desktop-diagnostics.test.mjs apps/desktop/src/main/desktop-menu.test.mjs apps/desktop/src/main/desktop-update-flow.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/wechat-chat-archive.test.mjs apps/desktop/src/main/wechat-share-import.test.mjs apps/desktop/src/main/share-extension-registration.test.mjs apps/desktop/src/main/windows-share-menu.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/local-data-reset.test.mjs apps/desktop/src/main/ai-direct.test.mjs apps/desktop/src/main/plugin-public-network.test.mjs apps/desktop/src/main/renderer-recovery.test.mjs apps/desktop/src/main/renderer-hibernate.test.mjs apps/desktop/src/main/renderer-startup-guard.test.mjs apps/desktop/src/main/renderer-storage-migration.test.mjs apps/desktop/src/main/resource-request.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/scheduled-task-scheduler.test.mjs apps/desktop/src/main/screenshot-capture.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/staged-resource-alias.test.mjs apps/desktop/src/main/tray-icon.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/desktop/src/main/window-visibility.test.mjs apps/desktop/src/main/windows-update-trust.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/clipboard.test.mjs apps/web/src/lib/desktop-renderer-ready.test.mjs apps/web/src/lib/desktop-workspace-restore.test.mjs apps/web/src/lib/desktop-repository.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/runtime.test.mjs apps/web/src/lib/screenshot-import.test.mjs apps/web/src/lib/wechat-chat-import.test.mjs apps/web/src/lib/shared-file-import.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun test --cwd apps/desktop src/main/acp-host.test.mjs && bun run test:desktop:sidecar",
"verify:desktop-renderer": "bun scripts/verify-desktop-renderer.mjs",
"verify:renderer-origin-migration": "bun run --cwd apps/desktop verify-origin-migration",
"verify:desktop-cross-version-startup": "bun scripts/verify-desktop-cross-version-startup.mjs",
diff --git a/packages/client/src/companion-direct-stream.test.mjs b/packages/client/src/companion-direct-stream.test.mjs
index b8d0b985..6fe65518 100644
--- a/packages/client/src/companion-direct-stream.test.mjs
+++ b/packages/client/src/companion-direct-stream.test.mjs
@@ -35,6 +35,30 @@ describe("companion prepare payload", () => {
test("rejects a payload that would send the API key as a target probe", () => {
expect(parsePreparedCompanion({ ...preparedTurn, apiKey: undefined })).toBeNull();
expect(parsePreparedCompanion(preparedTurn)?.modelId).toBe("openai/gpt-4o-mini");
+ expect(parsePreparedCompanion(preparedTurn)?.messages).toEqual([{ role: "user", content: "hi" }]);
+ });
+
+ test("keeps user attachment parts and rejects a malformed message", () => {
+ const parts = [
+ { type: "text", text: "hi" },
+ { type: "image", image: "aaaa", mediaType: "image/png" },
+ { type: "file", data: "bbbb", mediaType: "application/pdf", filename: "a.pdf" },
+ ];
+ expect(parsePreparedCompanion({
+ ...preparedTurn,
+ messages: [{ role: "user", content: parts }, { role: "assistant", content: "ok" }],
+ })?.messages).toEqual([
+ { role: "user", content: parts },
+ { role: "assistant", content: "ok" },
+ ]);
+ expect(parsePreparedCompanion({
+ ...preparedTurn,
+ messages: [{ role: "assistant", content: [{ type: "text", text: "no" }] }],
+ })).toBeNull();
+ expect(parsePreparedCompanion({
+ ...preparedTurn,
+ messages: [{ role: "user", content: "hi" }, { role: "user", content: [{ type: "image" }] }],
+ })).toBeNull();
});
});
diff --git a/packages/client/src/companion-direct-stream.ts b/packages/client/src/companion-direct-stream.ts
index f9786fa3..61b17276 100644
--- a/packages/client/src/companion-direct-stream.ts
+++ b/packages/client/src/companion-direct-stream.ts
@@ -1,5 +1,7 @@
import type {
CompanionEvent,
+ CompanionModelContentPart,
+ CompanionPreparedMessage,
CompanionPreparedTurn,
CompanionQuestion,
CompanionTodo,
@@ -8,6 +10,7 @@ import type {
CompanionTurn,
CompanionTurnCompleteInput,
} from "@edgeever/shared";
+import type { ModelMessage } from "ai";
import { sealCompanionProcess } from "@edgeever/shared";
import { isAiCorsFailure } from "./ai-direct-stream";
import { createClientAiModel } from "./ai-model";
@@ -17,6 +20,35 @@ const asRecord = (value: unknown): Record<string, unknown> | null =>
const asString = (value: unknown) => typeof value === "string" ? value : null;
+const contentPart = (value: unknown): CompanionModelContentPart | null => {
+ const part = asRecord(value);
+ if (!part || Array.isArray(value)) return null;
+ if (part.type === "text" && typeof part.text === "string") return { type: "text", text: part.text };
+ if (part.type === "image" && typeof part.image === "string" && typeof part.mediaType === "string") {
+ return { type: "image", image: part.image, mediaType: part.mediaType };
+ }
+ if (part.type === "file" && typeof part.data === "string" && typeof part.mediaType === "string"
+ && (part.filename === undefined || typeof part.filename === "string")) {
+ return {
+ type: "file", data: part.data, mediaType: part.mediaType,
+ ...(typeof part.filename === "string" ? { filename: part.filename } : {}),
+ };
+ }
+ return null;
+};
+
+const messageContent = (role: string, content: unknown): CompanionPreparedMessage["content"] | null => {
+ if (typeof content === "string") return content;
+ if (role !== "user" || !Array.isArray(content) || content.length === 0) return null;
+ const parts: CompanionModelContentPart[] = [];
+ for (const item of content) {
+ const part = contentPart(item);
+ if (!part) return null;
+ parts.push(part);
+ }
+ return parts;
+};
+
export const parsePreparedCompanion = (value: unknown): CompanionPreparedTurn | null => {
const record = asRecord(value);
const turn = asRecord(record?.turn);
@@ -36,13 +68,15 @@ export const parsePreparedCompanion = (value: unknown): CompanionPreparedTurn |
) {
return null;
}
- const messages = record.messages.flatMap((item) => {
+ const messages: CompanionPreparedMessage[] = [];
+ for (const item of record.messages) {
const message = asRecord(item);
- if (!message || (message.role !== "user" && message.role !== "assistant") || typeof message.content !== "string") {
- return [];
- }
- return [{ role: message.role as "user" | "assistant", content: message.content }];
- });
+ const content = message && (message.role === "user" || message.role === "assistant")
+ ? messageContent(message.role, message.content)
+ : null;
+ if (!message || (message.role !== "user" && message.role !== "assistant") || content === null) return null;
+ messages.push({ role: message.role, content });
+ }
const tools = record.tools.flatMap((item) => {
const tool = asRecord(item);
const inputSchema = asRecord(tool?.inputSchema);
@@ -176,8 +210,23 @@ export const streamDirectCompanion = async (
maxOutputTokens: prepared.maxOutputTokens,
maxRetries: 0,
});
+ const messages: ModelMessage[] = prepared.messages.map((message) => {
+ if (message.role === "assistant") {
+ if (typeof message.content !== "string") throw new Error("Assistant content must be a string.");
+ return { role: "assistant", content: message.content };
+ }
+ if (typeof message.content === "string") return { role: "user", content: message.content };
+ return {
+ role: "user",
+ content: message.content.map((part) => {
+ if (part.type === "text") return { type: "text" as const, text: part.text };
+ if (part.type === "image") return { type: "image" as const, image: part.image, mediaType: part.mediaType };
+ return { type: "file" as const, data: part.data, mediaType: part.mediaType, ...(part.filename ? { filename: part.filename } : {}) };
+ }),
+ };
+ });
const result = await agent.stream({
- messages: prepared.messages,
+ messages,
abortSignal: options.signal,
});
for await (const part of result.fullStream) {
diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts
index 90390950..44711744 100644
--- a/packages/client/src/index.ts
+++ b/packages/client/src/index.ts
@@ -1078,6 +1078,11 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {
);
},
clearCompanionHistory: () => request<{ ok: true }>("/api/v1/companion/history", { method: "DELETE" }),
+ uploadCompanionAttachment: (attachment: { filename: string; mediaType: string; base64Data: string }) =>
+ request<{ attachment: { id: string; filename: string; mediaType: string; byteLength: number; expiresAt: string } }>(
+ "/api/v1/companion/attachments",
+ { method: "POST", body: JSON.stringify(attachment) },
+ ),
exportCompanion: () => request<{ version: 2; controls: { useMemory: boolean; learningEnabled: boolean }; exportedAt: string; memories: CompanionMemory[]; turns: CompanionTurn[]; actions: CompanionAction[] }>("/api/v1/companion/export"),
importCompanionMemories: (memories: { content: string; kind?: "explicit" | "inferred" }[], controls?: { useMemory: boolean; learningEnabled: boolean }) => request<{ memories: CompanionMemory[] }>("/api/v1/companion/import-memories", {
method: "POST", body: JSON.stringify({ version: 2, memories, controls }),
diff --git a/packages/shared/src/companion.ts b/packages/shared/src/companion.ts
index 36511bba..d75bc8cc 100644
--- a/packages/shared/src/companion.ts
+++ b/packages/shared/src/companion.ts
@@ -1,4 +1,5 @@
import { z } from "zod";
+import { isAiTextAttachment, type AiAttachmentMediaType } from "./ai-assistant";
export const CompanionIdSchema = z.string().uuid();
export const CompanionMemoryInputSchema = z.object({
@@ -36,9 +37,27 @@ export const CompanionTurnInputSchema = z.object({
locale: z.enum(["zh-CN", "en-US", "ja"]).default("en-US"),
focus: CompanionTurnFocusSchema.optional(),
mentions: z.array(CompanionMentionSchema).max(8).optional(),
+ attachmentIds: z.array(z.string().uuid()).max(4).optional(),
}).strict();
export type CompanionTurnInput = z.infer<typeof CompanionTurnInputSchema>;
+export type CompanionModelContentPart =
+ | { type: "text"; text: string }
+ | { type: "image"; image: string; mediaType: string }
+ | { type: "file"; data: string; mediaType: string; filename?: string };
+
+export const COMPANION_NOTE_EDIT_MAX_CHARS = 200_000;
+
+export const companionSupportsAttachment = (
+ provider: "openai-compatible" | "anthropic" | "google",
+ mediaType: string,
+) => {
+ if (isAiTextAttachment(mediaType as AiAttachmentMediaType)) return true;
+ if (mediaType === "image/jpeg" || mediaType === "image/png" || mediaType === "image/webp" || mediaType === "image/gif") return true;
+ if (mediaType === "application/pdf") return provider === "anthropic" || provider === "google";
+ return false;
+};
+
export const CompanionQuestionSchema = z.object({
id: z.string().trim().min(1).max(80),
prompt: z.string().trim().min(1).max(200),
@@ -87,7 +106,7 @@ export type CompanionToolDefinition = {
};
export type CompanionPreparedMessage = {
role: "user" | "assistant";
- content: string;
+ content: string | CompanionModelContentPart[];
};
export type CompanionPreparedTurn = {
turn: CompanionTurn;
@@ -142,6 +161,12 @@ export type CompanionMemory = {
updatedAt: string;
};
export type CompanionSource = { id: string; title: string; revision: number; notebookId?: string };
+export type CompanionTurnAttachmentMeta = {
+ id: string;
+ filename: string;
+ mediaType: string;
+ byteLength: number;
+};
export const CompanionDiscoverySettingsInputSchema = z.object({
enabled: z.boolean(),
@@ -229,7 +254,7 @@ export type CompanionAction = {
resultMemoId: string | null;
resultNotebookId?: string | null;
result?: unknown;
- preview?: { notebooks: Array<{ id: string; name: string }>; affectedCount?: number };
+ preview?: { notebooks: Array<{ id: string; name: string }>; affectedCount?: number; baseContentMarkdown?: string };
createdAt: string;
};
export type CompanionTurn = {
@@ -244,6 +269,7 @@ export type CompanionTurn = {
todos: CompanionTodo[];
questions: CompanionQuestion[];
mentions: CompanionMention[];
+ attachments?: CompanionTurnAttachmentMeta[];
model: string;
inputTokens: number | null;
outputTokens: number | null;
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 5b40c71c..b88ab851 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -1289,6 +1289,69 @@ export const enUS = {
requestInvalid: "The AI request is invalid. Check the selected action and input, then try again.",
networkError: "Could not connect to the service. Make sure it is running, then try again.",
configure: "Configure an AI model in Me → AI Integrations first.",
+ sidebar: {
+ title: "AI assistant",
+ close: "Collapse assistant",
+ resize: "Resize assistant",
+ stop: "Stop",
+ placeholder: "Ask about this note, or type / for a skill",
+ emptyTitle: "Start from this note",
+ emptyDescription: "Summaries, rewrites, and translations come back as replies. They do not change the note on their own.",
+ working: "Replying…",
+ selectionBadge: "{{count}} characters selected · Current note",
+ skills: {
+ summarize: "Summarize",
+ improve: "Improve writing",
+ translate: "Translate",
+ },
+ skillPrompts: {
+ summarize: "Summarize what I'm looking at and reply with the summary. Do not claim the note was edited. Propose a note edit only if I want the note changed.",
+ improve: "Improve the writing of what I'm looking at and reply with the revised text. Do not claim the note was edited. Propose a note edit only if I want the note changed.",
+ translate: "Translate what I'm looking at and reply with the translation. Do not claim the note was edited. Propose a note edit only if I want the note changed.",
+ },
+ slash: {
+ summarize: "Summarize the current text",
+ improve: "Improve the current text",
+ translate: "Translate the current text",
+ },
+ attachmentUnsupported: "This model does not support these attachments. They are still in the composer.",
+ attachmentExpired: "These attachments expired. They are still in the composer — send them again.",
+ localRejected: "This local agent does not support these files. They stay in the composer: {{names}}",
+ localStatus: "Local agent",
+ localMissing: "No local agent is available. Choose an adapter, or use the built-in assistant.",
+ unavailable: "Sign in to a personal account to send. Demo mode and signed-out sessions cannot use the built-in assistant.",
+ proposalConfirm: "Apply to note",
+ proposalReject: "Don't apply",
+ proposalConflict: "The note changed, so nothing was applied.",
+ proposalConfirmOther: "Confirm",
+ proposalDismiss: "Dismiss",
+ toolProgress: "{{name}} · {{status}}",
+ },
+ agentSource: {
+ title: "Agent source",
+ description: "Use the built-in companion, or a local ACP agent on this computer.",
+ builtin: "Built-in companion",
+ builtinHint: "Uses the AI model configured above.",
+ local: "Local ACP agent",
+ localHint: "Codex or Antigravity on this computer. EdgeEver does not grant the agent your notes folder or a terminal.",
+ adapter: "Agent",
+ codex: "Codex",
+ antigravity: "Antigravity",
+ pathLabel: "Antigravity executable",
+ pathPlaceholder: "/absolute/path/to/antigravity-acp",
+ pathHint: "Absolute path to an executable file. EdgeEver does not download agents or search application folders.",
+ probe: "Check connection",
+ probing: "Checking…",
+ probeHint: "Opens a temporary session and closes it. Your notes are not sent.",
+ notProbed: "Not checked yet",
+ invalidPath: "Enter an absolute path to an executable file.",
+ states: {
+ not_installed: "Not installed",
+ needs_login: "Needs login",
+ available: "Session available",
+ failed: "Connection failed",
+ },
+ },
},
accountInfo: {
title: "Current account",
diff --git a/packages/shared/src/i18n/ja.ts b/packages/shared/src/i18n/ja.ts
index 0f087fe4..af49b688 100644
--- a/packages/shared/src/i18n/ja.ts
+++ b/packages/shared/src/i18n/ja.ts
@@ -1289,6 +1289,69 @@ export const ja = {
requestInvalid: "AI リクエストが無効です。選んだ操作と入力を確認して再試行してください。",
networkError: "サービスに接続できませんでした。稼働を確認してから再試行してください。",
configure: "先に「マイ → AI 連携」で AI モデルを設定してください。",
+ sidebar: {
+ title: "AI アシスタント",
+ close: "アシスタントをしまう",
+ resize: "幅を調整",
+ stop: "停止",
+ placeholder: "このノートについて聞くか、/ でスキルを入力",
+ emptyTitle: "このノートから始める",
+ emptyDescription: "要約、推敲、翻訳は返信として返ります。ノートは自動では変わりません。",
+ working: "返信しています…",
+ selectionBadge: "{{count}} 文字を選択 · 現在のノート",
+ skills: {
+ summarize: "要約",
+ improve: "推敲",
+ translate: "翻訳",
+ },
+ skillPrompts: {
+ summarize: "今見ている内容を要約し、要約を返信してください。ノートを変更したとは言わないでください。ノートの変更は、私が希望した場合だけ提案してください。",
+ improve: "今見ている内容を推敲し、推敲後の文章を返信してください。ノートを変更したとは言わないでください。ノートの変更は、私が希望した場合だけ提案してください。",
+ translate: "今見ている内容を翻訳し、訳文を返信してください。ノートを変更したとは言わないでください。ノートの変更は、私が希望した場合だけ提案してください。",
+ },
+ slash: {
+ summarize: "今の内容を要約",
+ improve: "今の内容を推敲",
+ translate: "今の内容を翻訳",
+ },
+ attachmentUnsupported: "このモデルは添付に対応していません。ファイルは入力欄に残しています。",
+ attachmentExpired: "添付の有効期限が切れました。ファイルは入力欄に残しています。",
+ localRejected: "このローカル Agent は次のファイルに対応していません。入力欄に残しています:{{names}}",
+ localStatus: "ローカル Agent",
+ localMissing: "使えるローカル Agent がありません。アダプターを選ぶか、内蔵アシスタントを使ってください。",
+ unavailable: "送信するには個人アカウントでサインインしてください。デモモードや未ログインでは内蔵アシスタントを使えません。",
+ proposalConfirm: "ノートに適用",
+ proposalReject: "適用しない",
+ proposalConflict: "ノートが変わったため、今回は変更していません。",
+ proposalConfirmOther: "確認",
+ proposalDismiss: "無視",
+ toolProgress: "{{name}} · {{status}}",
+ },
+ agentSource: {
+ title: "エージェントの接続先",
+ description: "内蔵コンパニオンか、このコンピュータ上のローカル ACP エージェントを使います。",
+ builtin: "内蔵コンパニオン",
+ builtinHint: "上で設定した AI モデルを使います。",
+ local: "ローカル ACP エージェント",
+ localHint: "このコンピュータ上の Codex または Antigravity です。EdgeEver はノートのフォルダもターミナルも渡しません。",
+ adapter: "エージェント",
+ codex: "Codex",
+ antigravity: "Antigravity",
+ pathLabel: "Antigravity の実行ファイル",
+ pathPlaceholder: "/absolute/path/to/antigravity-acp",
+ pathHint: "実行ファイルの絶対パスを入力します。EdgeEver はエージェントをダウンロードせず、アプリケーションフォルダも検索しません。",
+ probe: "接続を確認",
+ probing: "確認中…",
+ probeHint: "一時的なセッションを開いてすぐ閉じます。ノートは送信しません。",
+ notProbed: "未確認",
+ invalidPath: "実行ファイルの絶対パスを入力してください。",
+ states: {
+ not_installed: "未インストール",
+ needs_login: "ログインが必要",
+ available: "セッションを利用できます",
+ failed: "接続に失敗しました",
+ },
+ },
},
accountInfo: {
title: "現在のアカウント",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 7d520f32..d5fbff34 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -1289,6 +1289,69 @@ export const zhCN = {
requestInvalid: "AI 请求参数无效,请检查处理方式和输入内容后重试。",
networkError: "无法连接服务。请确认服务正在运行,然后重试。",
configure: "请先在“我的 → AI 集成”中配置 AI 模型。",
+ sidebar: {
+ title: "AI 助手",
+ close: "收起助手",
+ resize: "调整助手宽度",
+ stop: "停止",
+ placeholder: "问问这篇笔记,或输入 / 使用技能",
+ emptyTitle: "从当前笔记开始",
+ emptyDescription: "总结、润色和翻译会作为回复出现,不会直接改掉笔记。",
+ working: "正在回复…",
+ selectionBadge: "已选 {{count}} 字 · 当前笔记",
+ skills: {
+ summarize: "总结",
+ improve: "润色",
+ translate: "翻译",
+ },
+ skillPrompts: {
+ summarize: "请总结我正在看的内容,并把总结作为回复写给我。不要声称已经修改了笔记。只有在我希望改笔记时,才提出修改建议。",
+ improve: "请润色我正在看的内容,并把润色后的文字作为回复写给我。不要声称已经修改了笔记。只有在我希望改笔记时,才提出修改建议。",
+ translate: "请翻译我正在看的内容,并把译文作为回复写给我。不要声称已经修改了笔记。只有在我希望改笔记时,才提出修改建议。",
+ },
+ slash: {
+ summarize: "总结当前内容",
+ improve: "润色当前内容",
+ translate: "翻译当前内容",
+ },
+ attachmentUnsupported: "当前模型不支持这些附件,文件仍留在输入框。",
+ attachmentExpired: "附件已过期。文件仍留在输入框,请重新发送。",
+ localRejected: "本地 Agent 不支持这些文件,已留在输入框:{{names}}",
+ localStatus: "本地 Agent",
+ localMissing: "还没有可用的本地 Agent。请先选择适配器,或改用内置助手。",
+ unavailable: "需要登录个人账户后才能发送。演示模式或未登录时不能使用内置助手。",
+ proposalConfirm: "应用到笔记",
+ proposalReject: "不采用",
+ proposalConflict: "笔记已经变化,这次没有改动笔记。",
+ proposalConfirmOther: "确认",
+ proposalDismiss: "忽略",
+ toolProgress: "{{name}} · {{status}}",
+ },
+ agentSource: {
+ title: "代理来源",
+ description: "使用内置助手,或使用这台电脑上的本地 ACP 代理。",
+ builtin: "内置助手",
+ builtinHint: "使用上方配置的 AI 模型。",
+ local: "本地 ACP 代理",
+ localHint: "使用这台电脑上的 Codex 或 Antigravity。EdgeEver 不会把笔记目录或终端交给代理。",
+ adapter: "代理",
+ codex: "Codex",
+ antigravity: "Antigravity",
+ pathLabel: "Antigravity 可执行文件",
+ pathPlaceholder: "/absolute/path/to/antigravity-acp",
+ pathHint: "填写可执行文件的绝对路径。EdgeEver 不会下载代理,也不会搜索应用程序目录。",
+ probe: "检查连接",
+ probing: "正在检查…",
+ probeHint: "会临时建立会话并立即关闭,不会发送你的笔记。",
+ notProbed: "尚未检查",
+ invalidPath: "请填写可执行文件的绝对路径。",
+ states: {
+ not_installed: "未安装",
+ needs_login: "需要登录",
+ available: "会话可用",
+ failed: "连接失败",
+ },
+ },
},
accountInfo: {
title: "当前账户",