AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(web): respect AI assistant input
tianma-ifdcbb0d338321
diff --git a/apps/api/src/ai-routes.test.mjs b/apps/api/src/ai-routes.test.mjsindex 370a62a6..e1efad78 100644--- a/apps/api/src/ai-routes.test.mjs+++ b/apps/api/src/ai-routes.test.mjs@@ -129,6 +129,44 @@ describe("AI route contracts", () => {expect(AiGenerateSchema.parse({ ...input, stream: true }).stream).toBe(true);});+ test("allows custom generation from an empty note while source-based actions still require content", async () => {+ expect(AiGenerateSchema.safeParse({+ action: "custom",+ title: "",+ contentMarkdown: "",+ instruction: "Write a friendly greeting email.",+ }).success).toBe(true);+ expect(AiGenerateSchema.safeParse({+ action: "custom",+ promptId: "aiprompt_blank_note_generator",+ title: "",+ contentMarkdown: "",+ }).success).toBe(true);+ expect(AiGenerateSchema.safeParse({+ action: "summarize",+ title: "",+ contentMarkdown: "",+ }).success).toBe(false);++ const app = createApp();+ const response = await app.request(+ "/api/v1/ai/generate",+ {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ action: "summarize", title: "", contentMarkdown: "" }),+ },+ environment,+ );+ expect(response.status).toBe(400);+ expect(await response.json()).toEqual({+ error: {+ code: "ai_source_required",+ message: "Note content is required for this AI action.",+ },+ });+ });+test("validates AI tag suggestion inputs without requiring saved note content", () => {expect(AiTagSuggestionsRequestSchema.safeParse({title: "Unsaved title",diff --git a/apps/api/src/ai-routes.ts b/apps/api/src/ai-routes.tsindex 1ec8e244..564ae65e 100644--- a/apps/api/src/ai-routes.ts+++ b/apps/api/src/ai-routes.ts@@ -61,6 +61,21 @@ const providerErrorMessage = (error: unknown) => {return error.message.replace(/Bearer\s+\S+/gi, "Bearer [redacted]").slice(0, 1000);};+const validateAiGeneration = zValidator("json", AiGenerateSchema, (result, context) => {+ if (result.success) return;+ const sourceRequired = result.error.issues.some(+ (issue) => issue.path[0] === "contentMarkdown" && issue.message === "Note content is required.",+ );+ return apiError(+ context,+ sourceRequired ? "ai_source_required" : "ai_request_invalid",+ sourceRequired+ ? "Note content is required for this AI action."+ : "The AI request is invalid.",+ 400,+ );+});+const encryptionConfigured = (context: AppContext) =>Boolean(resolvePrimaryAiCredentialEncryptionKey(context.env));@@ -540,7 +555,7 @@ export const registerAiRoutes = (app: Hono<AppEnv>, dependencies: AiRouteDependeapp.post("/api/v1/ai/generate",- zValidator("json", AiGenerateSchema),+ validateAiGeneration,async (context) => {const denied = requireUser(context);if (denied) return denied;diff --git a/apps/web/src/components/dialogs/AiAssistantDialog.tsx b/apps/web/src/components/dialogs/AiAssistantDialog.tsxindex dc8b329c..50209378 100644--- a/apps/web/src/components/dialogs/AiAssistantDialog.tsx+++ b/apps/web/src/components/dialogs/AiAssistantDialog.tsx@@ -10,7 +10,7 @@ import {} from "react";import { createPortal } from "react-dom";import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";-import { BookmarkPlus, Check, Copy, FileText, GripHorizontal, Library, Loader2, Paperclip, PenLine, RefreshCw, Sparkles, Square, Trash2, X } from "lucide-react";+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 {@@ -48,6 +48,7 @@ import {promptAllowsReplace,promptNeedsTargetLanguage,promptNeedsTone,+ resolveAiAssistantComposerInput,targetLanguages,type AiAssistantAction,type AiTone,@@ -264,7 +265,6 @@ export const AiAssistantDialog = ({if (!open || !initializedForOpen || promptsQuery.isLoading || !selectedPromptId || selectedPrompt) return;setSelectedPromptId(null);setAction("custom");- setCustomInstruction("");setOutput("");setError(t("aiAssistant.promptMissing"));}, [initializedForOpen, open, promptsQuery.isLoading, selectedPrompt, selectedPromptId, t]);@@ -280,7 +280,6 @@ export const AiAssistantDialog = ({if (value === FREEFORM_VALUE) {setAction("custom");setSelectedPromptId(null);- setCustomInstruction("");clearResult();return;}@@ -290,7 +289,6 @@ export const AiAssistantDialog = ({const prompt = prompts.find((item) => item.id === promptId);setSelectedPromptId(promptId);setAction(prompt?.action ?? "custom");- setCustomInstruction("");clearResult();};@@ -316,8 +314,16 @@ export const AiAssistantDialog = ({);} 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")+ 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) {@@ -335,15 +341,22 @@ export const AiAssistantDialog = ({return;}+ const composerInput = resolveAiAssistantComposerInput({+ composerText: currentInstruction,+ isFreeformCustom,+ noteContentMarkdown: sourceMarkdown,+ noteTitle: title,+ });+return runGeneration(buildAiAssistantRequest({action: effectiveActionKey,- contentMarkdown: sourceMarkdown,- customInstruction: currentInstruction,+ contentMarkdown: composerInput.contentMarkdown,+ customInstruction: composerInput.customInstruction,locale: i18n.resolvedLanguage,parameterKind: selectedPrompt ? effectiveParameterKind : undefined,promptId: selectedPrompt?.id,targetLanguage,- title,+ title: composerInput.title,tone,attachments: attachments.map(({ byteLength: _byteLength, ...attachment }) => attachment),}));@@ -411,11 +424,8 @@ export const AiAssistantDialog = ({parameterKind: "none",resultMode: "both",}),- onSuccess: async (response) => {+ onSuccess: async () => {await queryClient.invalidateQueries({ queryKey: ["ai-prompts"] });- setSelectedPromptId(response.prompt.id);- setAction(response.prompt.action);- setCustomInstruction(response.prompt.instruction);setSaveDialogOpen(false);setSaveName("");setSaveDescription("");@@ -430,6 +440,7 @@ export const AiAssistantDialog = ({: null;const isFreeformCustom = !selectedPromptId && action === "custom";+ const usesComposerAsSource = !isFreeformCustom && Boolean(customInstruction.trim());const canSaveAsPrompt = isFreeformCustom && customInstruction.trim().length > 0;const generateDisabled = isGenerating|| isReadingAttachments;@@ -542,7 +553,9 @@ export const AiAssistantDialog = ({<Sparkles className="h-5 w-5 shrink-0 text-emerald-600" /><span className="truncate text-sm font-semibold text-slate-950">{t("aiAssistant.title")}</span><span className="shrink-0 rounded-full bg-emerald-100 px-2 py-0.5 text-xs font-medium text-emerald-800">- {t(hasSelection ? "aiAssistant.selectedScope" : "aiAssistant.noteScope")}+ {t(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>@@ -673,22 +686,18 @@ export const AiAssistantDialog = ({) : null}<div className="order-1 grid gap-2"><label className="grid gap-1.5 text-sm font-medium text-slate-700">- {t("aiAssistant.customInstruction")}+ {t(isFreeformCustom ? "aiAssistant.customInstruction" : "aiAssistant.inputContent")}<textarearef={instructionRef}className="min-h-24 resize-y rounded-md border border-slate-200 bg-white px-3 py-2 text-sm font-normal text-slate-900 outline-none focus:border-emerald-400 focus:ring-2 focus:ring-emerald-500/15"value={customInstruction}onChange={(event) => {customInstructionEditedRef.current = true;- setSelectedPromptId(null);- setAction("custom");setCustomInstruction(event.target.value);clearResult();}}onCompositionEnd={(event) => {customInstructionEditedRef.current = true;- setSelectedPromptId(null);- setAction("custom");setCustomInstruction(event.currentTarget.value);clearResult();}}@@ -705,7 +714,9 @@ export const AiAssistantDialog = ({event.preventDefault();void generate();}}- placeholder={t("aiAssistant.customInstructionPlaceholder")}+ placeholder={t(isFreeformCustom+ ? "aiAssistant.customInstructionPlaceholder"+ : "aiAssistant.inputContentPlaceholder")}maxLength={2_000}/></label>@@ -785,13 +796,23 @@ export const AiAssistantDialog = ({) : null}</div><div- className={cn("max-h-56 min-h-28 overflow-y-auto whitespace-pre-wrap rounded-lg border bg-slate-50 p-4 text-sm leading-6 text-slate-800", error ? "border-rose-200" : "border-slate-200")}+ 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">- {output || <span className="text-slate-400">{t("aiAssistant.resultPlaceholder")}</span>}+ {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>- {error ? <p className="text-xs font-medium text-rose-600" role="alert">{error}</p> : null}</div>{output && !isGenerating ? (<div className="order-4 grid gap-1.5 rounded-lg border border-slate-200 bg-white p-3">@@ -825,13 +846,13 @@ export const AiAssistantDialog = ({<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">- {promptAllowsReplace(effectiveResultMode) ? (+ {!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 && promptAllowsReplace(effectiveResultMode) ? "outline" : "solid"} disabled={isGenerating} onClick={() => applyOutput("append")}>{t("aiAssistant.append")}</Button>+ <Button type="button" variant={hasSelection && !usesComposerAsSource && promptAllowsReplace(effectiveResultMode) ? "outline" : "solid"} disabled={isGenerating} onClick={() => applyOutput("append")}>{t("aiAssistant.append")}</Button>) : null}</div></div>diff --git a/apps/web/src/lib/ai-assistant.test.mjs b/apps/web/src/lib/ai-assistant.test.mjsindex 678b17f7..a7d66768 100644--- a/apps/web/src/lib/ai-assistant.test.mjs+++ b/apps/web/src/lib/ai-assistant.test.mjs@@ -5,6 +5,7 @@ import {getDefaultAiAction,getDefaultTargetLanguage,parseDefaultAiPromptKey,+ resolveAiAssistantComposerInput,} from "./ai-assistant.ts";describe("AI assistant interaction model", () => {@@ -81,6 +82,47 @@ describe("AI assistant interaction model", () => {})).toMatchObject({ attachments: [attachment] });});+ test("sends a custom instruction when generating from a blank note", () => {+ expect(buildAiAssistantRequest({+ action: "custom",+ contentMarkdown: "",+ customInstruction: "Write a friendly greeting email.",+ targetLanguage: "en",+ title: "",+ tone: "professional",+ })).toEqual({+ action: "custom",+ title: "",+ contentMarkdown: "",+ instruction: "Write a friendly greeting email.",+ });+ });++ test("uses retained composer text as the source for a selected processing action", () => {+ expect(resolveAiAssistantComposerInput({+ composerText: "写一首诗",+ isFreeformCustom: false,+ noteContentMarkdown: "整篇笔记不应被翻译",+ noteTitle: "现有笔记",+ })).toEqual({+ contentMarkdown: "写一首诗",+ customInstruction: "",+ title: "",+ usesComposerAsSource: true,+ });+ expect(resolveAiAssistantComposerInput({+ composerText: "",+ isFreeformCustom: false,+ noteContentMarkdown: "留空时处理整篇笔记",+ noteTitle: "现有笔记",+ })).toEqual({+ contentMarkdown: "留空时处理整篇笔记",+ customInstruction: "",+ title: "现有笔记",+ usesComposerAsSource: false,+ });+ });+test("keeps extractive output additive while allowing rewritten content to replace its source", () => {expect(canReplaceAiSource("summarize")).toBe(false);expect(canReplaceAiSource("continue-writing")).toBe(false);diff --git a/apps/web/src/lib/ai-assistant.ts b/apps/web/src/lib/ai-assistant.tsindex add1b53e..7260bb79 100644--- a/apps/web/src/lib/ai-assistant.ts+++ b/apps/web/src/lib/ai-assistant.ts@@ -43,6 +43,26 @@ export {promptNeedsTone,};+export const resolveAiAssistantComposerInput = ({+ composerText,+ isFreeformCustom,+ noteContentMarkdown,+ noteTitle,+}: {+ composerText: string;+ isFreeformCustom: boolean;+ noteContentMarkdown: string;+ noteTitle: string;+}) => {+ const usesComposerAsSource = !isFreeformCustom && Boolean(composerText.trim());+ return {+ contentMarkdown: usesComposerAsSource ? composerText : noteContentMarkdown,+ customInstruction: isFreeformCustom ? composerText : "",+ title: usesComposerAsSource ? "" : noteTitle,+ usesComposerAsSource,+ };+};+export const buildAiAssistantRequest = ({action,contentMarkdown,diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.tsindex 7076f6a0..55ff945d 100644--- a/packages/shared/src/i18n/en-US.ts+++ b/packages/shared/src/i18n/en-US.ts@@ -589,6 +589,9 @@ export const enUS = {customInstruction: "Tell AI what to do",customInstructionPlaceholder: "For example: Rewrite this as a concise, friendly email and preserve every date.",customInstructionRequired: "Enter an instruction for AI.",+ inputContent: "Content to process",+ inputContentPlaceholder: "Enter content, or leave blank to process the whole note.",+ inputScope: "Input content",addAttachment: "Add files",attachments: "Attached files",removeAttachment: "Remove {{name}}",@@ -626,6 +629,9 @@ export const enUS = {replace: "Replace note",replaceSelection: "Accept and replace selection",applyFailed: "Could not apply the generated result. Select the content again and retry.",+ sourceRequired: "This action needs note content. Add some content first, or use a custom prompt to generate from a blank note.",+ 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.",},accountInfo: {diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.tsindex e4db1b34..529ede04 100644--- a/packages/shared/src/i18n/zh-CN.ts+++ b/packages/shared/src/i18n/zh-CN.ts@@ -589,6 +589,9 @@ export const zhCN = {customInstruction: "告诉 AI 你想怎么处理",customInstructionPlaceholder: "例如:改写成一封简洁、友好的邮件,并保留所有日期。",customInstructionRequired: "请输入你希望 AI 执行的指令。",+ inputContent: "输入要处理的内容",+ inputContentPlaceholder: "输入内容;留空则处理当前整篇笔记。",+ inputScope: "输入内容",addAttachment: "添加文件",attachments: "已添加文件",removeAttachment: "移除 {{name}}",@@ -626,6 +629,9 @@ export const zhCN = {replace: "替换笔记",replaceSelection: "接受并替换选中内容",applyFailed: "无法应用生成结果。请重新选择内容后再试。",+ sourceRequired: "当前处理方式需要笔记内容。请先输入内容,或改用自定义指令从空白开始生成。",+ requestInvalid: "AI 请求参数无效,请检查处理方式和输入内容后重试。",+ networkError: "无法连接服务。请确认服务正在运行,然后重试。",configure: "请先在“我的 → AI 集成”中配置 AI 模型。",},accountInfo: {diff --git a/packages/shared/src/schemas.ts b/packages/shared/src/schemas.tsindex 1d5dfed7..bb45dce0 100644--- a/packages/shared/src/schemas.ts+++ b/packages/shared/src/schemas.ts@@ -255,7 +255,8 @@ export const AiGenerateSchema = z.object({if (!input.promptId && input.action === "custom" && !input.instruction) {context.addIssue({ code: "custom", path: ["instruction"], message: "An instruction is required for a custom action." });}- if (!input.title && !input.contentMarkdown.trim() && input.attachments.length === 0) {+ const canGenerateWithoutSource = input.action === "custom" && Boolean(input.instruction || input.promptId);+ if (!input.title && !input.contentMarkdown.trim() && input.attachments.length === 0 && !canGenerateWithoutSource) {context.addIssue({ code: "custom", path: ["contentMarkdown"], message: "Note content is required." });}const totalAttachmentBytes = input.attachments.reduce(diff --git a/tests/e2e/ai-custom-prompts.spec.ts b/tests/e2e/ai-custom-prompts.spec.tsindex 38b0a934..9e4c9a05 100644--- a/tests/e2e/ai-custom-prompts.spec.ts+++ b/tests/e2e/ai-custom-prompts.spec.ts@@ -205,6 +205,95 @@ test.describe("AI custom prompts", () => {expect(clampedBox!.y + clampedBox!.height).toBeLessThanOrEqual(viewport!.height - 11);});+ test("generates from a blank note with a custom instruction and explains source requirements", async ({ page }) => {+ const memo = await createMemo(page, `e2e-ai-blank-${Date.now()}`, "");+ await ensureAuthenticatedPage(page);+ await page.getByRole("button", { name: new RegExp(notebookName) }).click();+ await page.locator(`[data-memo-id="${memo.id}"]`).locator("button").first().click();+ await expect(page.locator(".ProseMirror[contenteditable='true']")).toBeVisible();+ await page.getByPlaceholder("无标题笔记", { exact: true }).fill("");++ let customRequest: Record<string, unknown> | null = null;+ let translatedRequest: Record<string, unknown> | null = null;+ await page.route("**/api/v1/ai/generate", async (route) => {+ const body = route.request().postDataJSON() as Record<string, unknown>;+ if (body.action === "translate") {+ translatedRequest = body;+ await route.fulfill({+ status: 200,+ contentType: "text/event-stream; charset=utf-8",+ body: [+ `data: ${JSON.stringify({ type: "start" })}`,+ `data: ${JSON.stringify({ type: "text-delta", text: "Write a poem." })}`,+ `data: ${JSON.stringify({ type: "finish", finishReason: "stop" })}`,+ "",+ ].join("\n\n"),+ });+ return;+ }+ if (body.action !== "custom" || body.promptId) {+ await route.fulfill({+ status: 400,+ contentType: "application/json",+ body: JSON.stringify({+ error: {+ code: "ai_source_required",+ message: "Note content is required for this AI action.",+ },+ }),+ });+ return;+ }+ customRequest = body;+ await route.fulfill({+ status: 200,+ contentType: "text/event-stream; charset=utf-8",+ body: [+ `data: ${JSON.stringify({ type: "start" })}`,+ `data: ${JSON.stringify({ type: "text-delta", text: "你好!祝你今天一切顺利。" })}`,+ `data: ${JSON.stringify({ type: "finish", finishReason: "stop" })}`,+ "",+ ].join("\n\n"),+ });+ });++ await page.getByRole("button", { name: "打开 AI 写作助手", exact: true }).click();+ const assistant = page.getByRole("dialog", { name: "AI 笔记助手" });+ await expect(assistant).toBeVisible();+ await assistant.getByRole("button", { name: "生成", exact: true }).click();+ const result = assistant.getByTestId("ai-assistant-result");+ await expect(result.getByRole("alert")).toHaveText("当前处理方式需要笔记内容。请先输入内容,或改用自定义指令从空白开始生成。");+ await expect(result).not.toContainText("生成结果将显示在这里。");+ await expect(assistant).not.toContainText("contentMarkdown");++ await assistant.getByRole("button", { name: "自定义指令", exact: true }).click();+ const instruction = assistant.getByRole("textbox", { name: "告诉 AI 你想怎么处理" });+ await instruction.fill("写一首诗");+ await selectAction(assistant, "翻译");+ await expect(assistant.getByRole("textbox", { name: "输入要处理的内容" })).toHaveValue("写一首诗");+ await assistant.getByRole("button", { name: "生成", exact: true }).click();+ await expect(result).toHaveText("Write a poem.");+ expect(translatedRequest).toMatchObject({+ action: "translate",+ title: "",+ contentMarkdown: "写一首诗",+ targetLanguage: "en",+ });+ expect(translatedRequest).not.toHaveProperty("instruction");++ await assistant.getByRole("button", { name: "清除结果", exact: true }).click();+ await assistant.getByRole("button", { name: "自定义指令", exact: true }).click();+ await expect(instruction).toHaveValue("写一首诗");+ await assistant.getByRole("button", { name: "生成", exact: true }).click();+ await expect(assistant.getByText("你好!祝你今天一切顺利。", { exact: true })).toBeVisible();+ expect(customRequest).toMatchObject({+ action: "custom",+ title: "",+ contentMarkdown: "",+ instruction: "写一首诗",+ });+ });+test("opens the function menu from a bare slash and runs its AI command", async ({ page }) => {const memo = await createMemo(page, `e2e-slash-menu-${Date.now()}`, "斜杠菜单测试");await ensureAuthenticatedPage(page);