From 09f8d1d3bf1dc87e0fcacae839a81ad7435fa576 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 25 Sep 2025 00:23:50 +0800 Subject: [PATCH] feat(ai-chat): enhance date mention functionality with new relative date options - Added support for relative date mentions for the last 15 days and specific weekdays (Monday to Sunday) in the AI chat. - Updated localization files to include translations for the new date mention keys in English, Japanese, and Simplified Chinese. - Refactored the date mention configuration to improve clarity and maintainability. These changes aim to provide users with more flexible date mention options, enhancing the overall user experience in the AI chat interface. Signed-off-by: Innei --- .../components/message/AIMarkdownMessage.tsx | 2 +- .../mention/hooks/dateMentionConfig.ts | 99 +++++++++++++++++++ .../src/modules/ai-chat/services/index.ts | 66 ++++++++----- locales/ai/en.json | 2 + locales/ai/ja.json | 2 + locales/ai/zh-CN.json | 2 + 6 files changed, 148 insertions(+), 25 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMarkdownMessage.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMarkdownMessage.tsx index 2b2958eeb..d2a6c763f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMarkdownMessage.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/AIMarkdownMessage.tsx @@ -18,7 +18,7 @@ export const AIMarkdownStreamingMessage = memo( prose-h1:text-2xl prose-h2:text-xl prose-h2:mt-2 prose-h3:text-lg prose-h4:text-base prose-h5:text-base prose-h6:text-sm prose-li:list-disc prose-li:marker:text-accent prose-hr:border-border prose-hr:mx-8 w-[calc(var(--ai-chat-message-container-width,65ch))] - prose-pre:text-base + prose-pre:!text-base ` const stableStreamingState = useRef(isStreaming) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/dateMentionConfig.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/dateMentionConfig.ts index 453b19e4a..b4ada16ba 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/dateMentionConfig.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/dateMentionConfig.ts @@ -41,6 +41,12 @@ export const RELATIVE_DATE_DEFINITIONS: readonly RelativeDateDefinition[] = [ searchKeys: ["mentions.date.relative.last_7_days.search"], range: (today) => clampRangeToPastMonth({ start: today.subtract(6, "day"), end: today }), }, + { + id: "date:relative:last-15-days", + labelKey: "mentions.date.relative.last_15_days.label", + searchKeys: ["mentions.date.relative.last_15_days.search"], + range: (today) => clampRangeToPastMonth({ start: today.subtract(14, "day"), end: today }), + }, { id: "date:relative:last-30-days", labelKey: "mentions.date.relative.last_30_days.label", @@ -63,6 +69,99 @@ export const RELATIVE_DATE_DEFINITIONS: readonly RelativeDateDefinition[] = [ return clampRangeToPastMonth({ start, end }) }, }, + // Weekday in this week (future days are filtered by clampRangeToPastMonth) + { + id: "date:relative:this-week-monday", + labelKey: "mentions.date.weekday.day.monday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.monday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(1, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-tuesday", + labelKey: "mentions.date.weekday.day.tuesday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.tuesday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(2, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-wednesday", + labelKey: "mentions.date.weekday.day.wednesday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.wednesday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(3, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-thursday", + labelKey: "mentions.date.weekday.day.thursday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.thursday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(4, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-friday", + labelKey: "mentions.date.weekday.day.friday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.friday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(5, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-saturday", + labelKey: "mentions.date.weekday.day.saturday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.saturday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(6, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { + id: "date:relative:this-week-sunday", + labelKey: "mentions.date.weekday.day.sunday.label", + searchKeys: [ + "mentions.date.weekday.prefix.this.search", + "mentions.date.weekday.day.sunday.search", + ], + range: (today) => { + const startOfWeek = today.startOf("week") + const target = startOfWeek.add(0, "day") + return clampRangeToPastMonth({ start: target, end: target }) + }, + }, + { id: "date:relative:this-month", labelKey: "mentions.date.relative.this_month.label", diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts index 6a997f12d..cadb8eed8 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts @@ -4,6 +4,7 @@ import { aiChatMessagesTable, aiChatTable } from "@follow/database/schemas/index import { asc, count, eq, inArray, sql } from "drizzle-orm" import type { BizUIMessage } from "../store/types" +import { isDataBlockPart, isFileAttachmentBlock } from "../utils/extractor" class AIPersistServiceStatic { // Cache for session existence to avoid repeated queries @@ -154,33 +155,50 @@ class AIPersistServiceStatic { // Ensure the chat session exists first to avoid foreign key constraint failure await this.ensureSession(chatId) + const results = messages.reduce<(typeof aiChatMessagesTable.$inferInsert)[]>((acc, message) => { + if (message.parts.length === 0) return acc + const createdAt = message.metadata?.finishTime + ? new Date( + new Date(message.metadata.finishTime).getTime() - (message.metadata.duration ?? 0), + ) + : new Date() + + const cleanParts = [] as typeof message.parts + + for (const part of message.parts) { + if (isDataBlockPart(part)) { + const nextPart = structuredClone(part) + for (const block of nextPart.data) { + if (isFileAttachmentBlock(block)) { + Reflect.deleteProperty(block.attachment, "dataUrl") + } + } + + cleanParts.push(nextPart) + } else { + cleanParts.push(part) + } + } + + acc.push({ + id: message.id, + chatId, + role: message.role, + createdAt, + status: "completed" as const, + finishedAt: message.metadata?.finishTime + ? new Date(message.metadata.finishTime) + : undefined, + messageParts: cleanParts as (typeof aiChatMessagesTable.$inferInsert)["messageParts"], + metadata: message.metadata, + }) + + return acc + }, []) await db .insert(aiChatMessagesTable) - .values( - messages - .filter((message) => message.parts.length > 0) - .map((message) => { - const createdAt = message.metadata?.finishTime - ? new Date( - new Date(message.metadata.finishTime).getTime() - - (message.metadata.duration ?? 0), - ) - : new Date() - return { - id: message.id, - chatId, - role: message.role, - createdAt, - status: "completed" as const, - finishedAt: message.metadata?.finishTime - ? new Date(message.metadata.finishTime) - : undefined, - messageParts: message.parts, - metadata: message.metadata, - } as typeof aiChatMessagesTable.$inferInsert - }), - ) + .values(results) .onConflictDoUpdate({ target: [aiChatMessagesTable.id], set: { diff --git a/locales/ai/en.json b/locales/ai/en.json index 9d42d6b62..9a6080362 100644 --- a/locales/ai/en.json +++ b/locales/ai/en.json @@ -80,6 +80,8 @@ "integration.mcp.services.title": "MCP Services", "integration.title": "Integration", "mentions.date.display.with_range": "{{label}} ({{range}})", + "mentions.date.relative.last_15_days.label": "Last 15 days", + "mentions.date.relative.last_15_days.search": "last 15 days|past 15 days|last half month|past half month", "mentions.date.relative.last_30_days.label": "Last 30 days", "mentions.date.relative.last_30_days.search": "last 30 days|past 30 days", "mentions.date.relative.last_3_days.label": "Last 3 days", diff --git a/locales/ai/ja.json b/locales/ai/ja.json index 640a79bd5..90beb531e 100644 --- a/locales/ai/ja.json +++ b/locales/ai/ja.json @@ -74,6 +74,8 @@ "integration.mcp.services.title": "MCPサービス", "integration.title": "統合", "mentions.date.display.with_range": "{{label}}({{range}})", + "mentions.date.relative.last_15_days.label": "過去15日間", + "mentions.date.relative.last_15_days.search": "過去15日間|直近15日間|半月", "mentions.date.relative.last_30_days.label": "過去30日間", "mentions.date.relative.last_30_days.search": "過去30日間|直近30日間", "mentions.date.relative.last_3_days.label": "過去3日間", diff --git a/locales/ai/zh-CN.json b/locales/ai/zh-CN.json index dc8d93d42..1657eb431 100644 --- a/locales/ai/zh-CN.json +++ b/locales/ai/zh-CN.json @@ -76,6 +76,8 @@ "integration.mcp.services.title": "MCP服务", "integration.title": "集成", "mentions.date.display.with_range": "{{label}}({{range}})", + "mentions.date.relative.last_15_days.label": "最近15天", + "mentions.date.relative.last_15_days.search": "最近15天|近半个月", "mentions.date.relative.last_30_days.label": "最近30天", "mentions.date.relative.last_30_days.search": "最近30天|近一个月", "mentions.date.relative.last_3_days.label": "最近3天",