Skip to content

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

feat(ai): apply note content edits automatically

tianma-iffc4be760365d
diff --git a/apps/api/src/companion-agent-tools.ts b/apps/api/src/companion-agent-tools.ts
index 7e03a987..a6b8f078 100644
--- a/apps/api/src/companion-agent-tools.ts
+++ b/apps/api/src/companion-agent-tools.ts
@@ -188,7 +188,7 @@ const companionMcpDescription = (definition: (typeof COMPANION_MCP_TOOLS)[number
const readOnly = definition.annotations.readOnlyHint;
const autoApply = AUTO_APPLY_WRITES.has(definition.name);
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."
+ ? " This executes immediately, including contentMarkdown. Read the complete current note before replacing its body. Content edits keep revision history."
: readOnly || autoApply
? autoApply
? INBOX_DEFAULT_NOTEBOOK_TOOLS.has(definition.name)
@@ -349,11 +349,9 @@ export function createCompanionTools(args: { db: DatabaseAdapter; scope: Compani
throw new AppError("invalid_params", "This is an infographic. Do not replace it with 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);
+ throw new AppError("invalid_params", "This note edit is too large. 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 }));
+ parameters_.expectedRevision = memo.revision;
}
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));
@@ -433,6 +431,15 @@ export function createCompanionTools(args: { db: DatabaseAdapter; scope: Compani
changes: updated.changes,
});
}
+ if (definition.name === "update_memo") {
+ const updated = (result as { memo: MemoDetail }).memo;
+ const receipt = await done({ applied: true, id: updated.id, title: updated.title,
+ notebookId: updated.notebookId, revision: updated.revision });
+ inspected.set(updated.id, updated.revision);
+ remember(updated);
+ persistSession();
+ return receipt;
+ }
if ((definition.name === "create_memo" || definition.name === "use_note_template" || definition.name === "merge_memos")
&& result && typeof result === "object" && "memo" in result) {
remember((result as { memo: MemoDetail }).memo);
diff --git a/apps/api/src/companion-prepare.ts b/apps/api/src/companion-prepare.ts
index 9b0e6005..ee8c094b 100644
--- a/apps/api/src/companion-prepare.ts
+++ b/apps/api/src/companion-prepare.ts
@@ -23,13 +23,13 @@ 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 infographics, and manage note templates and AI instructions.
-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.
+Note tools, including update_memo with contentMarkdown, execute immediately when allowed. Read the complete current note before replacing its body. 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.
Never operate on hypothetical IDs: confirm the prerequisite first, then use its real result.
For multi-step organization (≥3 steps), call todo_write first and keep it current. If you cannot choose among existing notes or strategies, call ask_user_question once and stop. Never ask which notebook should receive a new note, diagram, infographic, or template. Omit notebookId and it is saved in the inbox notebook (等待分类).
-Resolve follow-up references such as "the translated content" from the most recent relevant exchange in this thread before asking a question. If you just translated the open note and the user asks to replace its original with the translation, use that exact translated text as the replacement; do not translate it again or ask for a target language. An older translation of different text does not override the latest relevant translation. Read the current note before proposing an update_memo that changes only contentMarkdown. Ask only if the referenced result is missing or genuinely ambiguous.
+Resolve follow-up references such as "the translated content" from the most recent relevant exchange in this thread before asking a question. If you just translated the open note and the user asks to replace its original with the translation, use that exact translated text as the replacement; do not translate it again or ask for a target language. An older translation of different text does not override the latest relevant translation. Read the current note before calling update_memo to change its contentMarkdown. Ask only if the referenced result is missing or genuinely ambiguous.
Do not repeat writes already listed as applied in Historical operation receipts.
The user already sees each tool in a timeline. Do not narrate that you will search, read, or create, and do not write status updates such as "I will now" or "I have obtained". After tools finish, write only the user-facing answer: what you did, with note links.
Emptying the trash, public sharing, binary uploads, and system administration are not exposed. Do not claim otherwise.
diff --git a/apps/api/src/companion-tool-actions.test.mjs b/apps/api/src/companion-tool-actions.test.mjs
index 9d1e822b..151e7aa0 100644
--- a/apps/api/src/companion-tool-actions.test.mjs
+++ b/apps/api/src/companion-tool-actions.test.mjs
@@ -158,21 +158,24 @@ describe("shared companion MCP adapter", () => {
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 });
- 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);
+ const updated = await tools.update_memo.execute({ memoId: f.notes[0].id, title: "Changed", contentMarkdown: "Exact replacement" });
+ expect(updated).toMatchObject({ applied: true, id: f.notes[0].id, title: "Changed" });
+ expect(updated).not.toHaveProperty("proposalId");
expect(await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id)).toMatchObject({ title: "Changed", contentMarkdown: "Exact replacement" });
- 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" });
+ expect(f.sqlite.query("SELECT COUNT(*) AS n FROM companion_actions").get().n).toBe(0);
+ expect(await tools.update_memo.execute({ memoId: f.notes[0].id, contentMarkdown: "Second replacement" }))
+ .toMatchObject({ applied: true, id: f.notes[0].id });
+ expect((await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id)).contentMarkdown).toBe("Second replacement");
+ });
+ test("an auto-applied body edit does not overwrite a note changed after its read", async () => {
+ const f = await setup();
+ const tools = createCompanionTools({ ...f, scope, signal: new AbortController().signal, assertActive: async () => {}, sources: [] });
+ await tools.get_memo.execute({ memoId: f.notes[0].id });
+ await updateMemoRecord(f.db, scope.workspaceId, f.notes[0].id, { contentMarkdown: "Newer edit" }, actor, "owner");
+ expect(await tools.update_memo.execute({ memoId: f.notes[0].id, contentMarkdown: "Stale replacement" }))
+ .toMatchObject({ error: expect.any(String) });
+ expect((await getMemoDetail(f.db, scope.workspaceId, f.notes[0].id)).contentMarkdown).toBe("Newer edit");
+ expect(f.sqlite.query("SELECT COUNT(*) AS n FROM companion_actions").get().n).toBe(0);
});
test("move, tag, and notebook writes execute immediately without a confirmation card", async () => {
const f = await setup();
diff --git a/apps/api/src/companion.test.mjs b/apps/api/src/companion.test.mjs
index 86d69d05..e236872c 100644
--- a/apps/api/src/companion.test.mjs
+++ b/apps/api/src/companion.test.mjs
@@ -565,11 +565,11 @@ 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("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");
+ test("assistant instructions describe direct note edits and trustworthy receipts", () => {
+ expect(COMPANION_INSTRUCTIONS).toContain("update_memo with contentMarkdown, execute immediately");
+ expect(COMPANION_INSTRUCTIONS).toContain("Read the complete current note before replacing its body");
+ expect(COMPANION_INSTRUCTIONS).toContain("Only report a note operation as completed when the tool result says applied");
+ expect(COMPANION_INSTRUCTIONS).not.toContain("awaiting_user_confirmation");
expect(COMPANION_INSTRUCTIONS).toContain("[Note title](#memo=memo_abc123)");
expect(COMPANION_INSTRUCTIONS).toContain("Do not drop memo_");
expect(COMPANION_INSTRUCTIONS).toContain("not as a heading");
@@ -623,7 +623,27 @@ describe("actual AI SDK companion runtime", () => {
expect(await listCompanionActions(db, scope)).toEqual([]);
});
- test("truncated notes cannot be used for a write proposal", async () => {
+ test("the real tool loop replaces note content without a confirmation card", async () => {
+ const { db, notes, row, complete, context } = await organizationFixture();
+ const calls = [
+ { toolName: "get_memo", input: JSON.stringify({ memoId: notes[0].id }) },
+ { toolName: "update_memo", input: JSON.stringify({ memoId: notes[0].id, contentMarkdown: "Translated content" }) },
+ ];
+ const model = new MockLanguageModelV4({ doStream: async () => {
+ const call = calls.shift();
+ return { stream: simulateReadableStream({ chunks: call ? [
+ { type: "tool-call", toolCallId: crypto.randomUUID(), ...call }, { ...finish, finishReason: { unified: "tool-calls" } },
+ ] : [{ type: "text-start", id: "1" }, { type: "text-delta", id: "1", delta: "Updated." }, { type: "text-end", id: "1" }, finish] }) };
+ } });
+ const result = await streamCompanion({ db, context, scope, input: input({ id: row.id, threadId: row.thread_id, allowNotes: true }), model,
+ memories: [], history: [], revision: 0, signal: new AbortController().signal, sources: [], assertActive: async () => {} });
+ expect(await result.text).toBe("Updated.");
+ expect((await getMemoDetail(db, scope.workspaceId, notes[0].id)).contentMarkdown).toBe("Translated content");
+ await complete();
+ expect(await listCompanionActions(db, scope)).toEqual([]);
+ });
+
+ test("truncated notes cannot be used for a body edit", async () => {
const { db, notes, row, context } = await organizationFixture();
await updateMemoRecord(db, scope.workspaceId, notes[0].id, { contentMarkdown: "x".repeat(9000) }, { actorType: "user", actorId: scope.ownerId }, scope.ownerId);
const calls = [