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
修复 AI 标签解析并统一移动端入口
tianma-if742ccaa3c43b
diff --git a/apps/api/src/ai-runtime.test.mjs b/apps/api/src/ai-runtime.test.mjsindex f614226c..c8c64867 100644--- a/apps/api/src/ai-runtime.test.mjs+++ b/apps/api/src/ai-runtime.test.mjs@@ -25,8 +25,20 @@ describe("AI tag response parsing", () => {expect(parseAiTagSuggestionNames("日本轻小说")).toEqual(["日本轻小说"]);});+ test("accepts concise delimited tags from models that ignore the requested block", () => {+ expect(parseAiTagSuggestionNames("标签:军人婚姻、少数民族加分、政策公平"))+ .toEqual(["军人婚姻", "少数民族加分", "政策公平"]);+ expect(parseAiTagSuggestionNames("React, AI")).toEqual(["React", "AI"]);+ expect(parseAiTagSuggestionNames("没有合适的标签。 ")).toEqual([]);+ });++ test("scans past reasoning braces to find a later suggestions object", () => {+ expect(parseAiTagSuggestionNames('Reasoning {not json}. Result: {"tags":["React","AI"]}'))+ .toEqual(["React", "AI"]);+ });+test("rejects prose instead of treating it as tag data", () => {- expect(() => parseAiTagSuggestionNames("React, AI"))+ expect(() => parseAiTagSuggestionNames("This note discusses React, AI, and several implementation tradeoffs.")).toThrow("requested tag block");expect(() => parseAiTagSuggestionNames('{"answer":"React"}')).toThrow("requested tag block");diff --git a/apps/api/src/ai-runtime.ts b/apps/api/src/ai-runtime.tsindex b7cb6587..9b691e94 100644--- a/apps/api/src/ai-runtime.ts+++ b/apps/api/src/ai-runtime.ts@@ -29,7 +29,8 @@ export const generateAiText = (...args: Parameters<typeof generateText>) => geneexport const streamAiText = (...args: Parameters<typeof streamText>) => streamText(...args);-const findJsonValue = (text: string) => {+const findJsonValues = (text: string) => {+ const values: string[] = [];for (let start = 0; start < text.length; start += 1) {const opening = text[start];if (opening !== "{" && opening !== "[") continue;@@ -52,11 +53,15 @@ const findJsonValue = (text: string) => {else if (character === "}" || character === "]") {const expected = character === "}" ? "{" : "[";if (stack.pop() !== expected) break;- if (stack.length === 0) return text.slice(start, index + 1);+ if (stack.length === 0) {+ values.push(text.slice(start, index + 1));+ start = index;+ break;+ }}}}- return null;+ return values;};const readSuggestionArray = (value: unknown): unknown[] | null => {@@ -70,8 +75,8 @@ const readSuggestionArray = (value: unknown): unknown[] | null => {};export const parseAiTagSuggestionNames = (text: string) => {- const jsonText = findJsonValue(text.trim());- if (jsonText) {+ const trimmedText = text.trim();+ for (const jsonText of findJsonValues(trimmedText)) {try {const suggestions = readSuggestionArray(JSON.parse(jsonText));if (suggestions) {@@ -90,10 +95,10 @@ export const parseAiTagSuggestionNames = (text: string) => {.slice(0, MAX_AI_TAG_SUGGESTIONS);}} catch {- // Some reasoning models emit prose containing braces before the requested block.+ // Keep scanning because reasoning text can contain braces before the actual JSON.}}- const block = text.match(/<edgeever-tags>\s*([\s\S]*?)\s*<\/edgeever-tags>/i)?.[1];+ const block = trimmedText.match(/<edgeever-tags>\s*([\s\S]*?)\s*<\/edgeever-tags>/i)?.[1];if (block !== undefined) {return block.split(/\r?\n/)@@ -101,12 +106,30 @@ export const parseAiTagSuggestionNames = (text: string) => {.filter(Boolean).slice(0, MAX_AI_TAG_SUGGESTIONS);}- const plainTagLines = text+ if (/^(?:没有|无)(?:找到)?(?:合适|适合|可用|相关)?(?:的)?(?:新)?标签[。.!!]?$/.test(trimmedText)+ || /^(?:no|none|no suitable|no relevant) tags?[.!]?$/i.test(trimmedText)) {+ return [];+ }+ const plainTagLines = trimmedText.split(/\r?\n/).map((line) => line.trim().replace(/^(?:[-*]\s+|\d+[.)]\s+|#)/, "").trim()).filter((line) => line.length > 0 && line.length <= 80)- .filter((line) => !/[。.!!??::]$/.test(line) && !/[,,{}<>]/.test(line));+ .filter((line) => !/[。.!!??::]$/.test(line) && !/[,,、;;{}<>]/.test(line));if (plainTagLines.length > 0) return plainTagLines.slice(0, MAX_AI_TAG_SUGGESTIONS);+ const delimitedTags = trimmedText+ .replace(/^```(?:text|markdown)?\s*/i, "")+ .replace(/\s*```$/, "")+ .replace(/^(?:建议)?标签\s*[::]\s*/i, "")+ .split(/[\r\n,,、;;]+/)+ .map((tag) => tag.trim().replace(/^#/, "").trim())+ .filter(Boolean);+ if (+ delimitedTags.length > 0+ && delimitedTags.length <= MAX_AI_TAG_SUGGESTIONS+ && delimitedTags.every((tag) => tag.length <= 40 && !/[。.!!??:{}<>]/.test(tag))+ ) {+ return delimitedTags;+ }throw new Error("AI tag response did not contain the requested tag block.");};diff --git a/apps/mobile/src/components/icons.tsx b/apps/mobile/src/components/icons.tsxindex 4f7887f9..28a73934 100644--- a/apps/mobile/src/components/icons.tsx+++ b/apps/mobile/src/components/icons.tsx@@ -1,10 +1,12 @@import Feather from "@expo/vector-icons/Feather";+import MaterialCommunityIcons from "@expo/vector-icons/MaterialCommunityIcons";import type { ComponentProps } from "react";import { ActivityIndicator as NativeActivityIndicator, type ActivityIndicatorProps } from "react-native";import { resolveMobileThemeColor, useMobileTheme } from "../lib/mobile-theme";type FeatherName = ComponentProps<typeof Feather>["name"];type IconProps = Omit<ComponentProps<typeof Feather>, "name">;+type MaterialCommunityIconProps = Omit<ComponentProps<typeof MaterialCommunityIcons>, "name">;export const ActivityIndicator = ({ color, ...props }: ActivityIndicatorProps) => {const { resolvedTheme } = useMobileTheme();@@ -77,6 +79,10 @@ export const Sparkles = createIcon("star");export const Square = createIcon("square");export const Sun = createIcon("sun");export const Tag = createIcon("tag");+export const TagPlus = ({ color, ...props }: MaterialCommunityIconProps) => {+ const { resolvedTheme } = useMobileTheme();+ return <MaterialCommunityIcons name="tag-plus-outline" {...props} color={resolveMobileThemeColor(color as string | undefined, resolvedTheme)} />;+};export const Trash2 = createIcon("trash-2");export const Upload = createIcon("upload");export const UserRound = createIcon("user");diff --git a/apps/mobile/src/screens/WorkspaceScreen.tsx b/apps/mobile/src/screens/WorkspaceScreen.tsxindex 6df04845..91e8b806 100644--- a/apps/mobile/src/screens/WorkspaceScreen.tsx+++ b/apps/mobile/src/screens/WorkspaceScreen.tsx@@ -33,9 +33,9 @@ import {Search,ShieldCheck,SlidersHorizontal,- Sparkles,Sun,Tag,+ TagPlus,Trash2,UserRound,X,@@ -1880,7 +1880,7 @@ const SmartTagButton = ({? <ActivityIndicator color="#047857" size="small" />: status === "success"? <Check color="#047857" size={17} />- : <Sparkles color="#047857" size={17} />}+ : <TagPlus color="#047857" size={18} />}</Pressable>);};@@ -2572,7 +2572,6 @@ const CreateMemoModal = ({<ChevronDown color="#64748b" size={14} /></Pressable><Pressable accessibilityLabel="选择笔记标签" accessibilityRole="button" onPress={() => setTagPickerOpen(true)} style={styles.createMemoTagsButton}>- <Tag color="#64748b" size={15} /><Text numberOfLines={1} style={[styles.createMemoTagsInput, !tagsText && styles.createMemoTagsPlaceholder]}>{tagsText || "添加标签"}</Text>@@ -3155,7 +3154,6 @@ const RichEditorModal = ({<ChevronDown color="#64748b" size={14} /></Pressable><Pressable accessibilityLabel="选择笔记标签" accessibilityRole="button" onPress={() => setTagPickerOpen(true)} style={[styles.createMemoTagsButton, styles.richStandaloneTagsInput]}>- <Tag color="#64748b" size={15} /><Text numberOfLines={1} style={[styles.createMemoTagsInput, !tagsText && styles.createMemoTagsPlaceholder]}>{tagsText || "添加标签"}</Text>