From b472db3b8e923400a3ecf04b16a0c3e94a10eea4 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:51:57 +0800 Subject: [PATCH] feat: save ai summary as description, close #3478 --- .../renderer/src/atoms/settings/general.ts | 7 ++ .../modules/command/commands/integration.tsx | 67 ++++++++++++------- .../command/hooks/use-register-command.ts | 3 +- .../src/modules/settings/tabs/integration.tsx | 3 + locales/settings/en.json | 1 + packages/shared/src/settings/defaults.ts | 4 +- packages/shared/src/settings/interface.ts | 2 + 7 files changed, 61 insertions(+), 26 deletions(-) diff --git a/apps/desktop/src/renderer/src/atoms/settings/general.ts b/apps/desktop/src/renderer/src/atoms/settings/general.ts index 895679afd..1be78c825 100644 --- a/apps/desktop/src/renderer/src/atoms/settings/general.ts +++ b/apps/desktop/src/renderer/src/atoms/settings/general.ts @@ -178,6 +178,13 @@ export function useActionLanguage() { ) as SupportedLanguages } +export function getActionLanguage() { + const { actionLanguage, language } = getGeneralSettingsInternal() + return ( + actionLanguage === DEFAULT_ACTION_LANGUAGE ? language : actionLanguage + ) as SupportedLanguages +} + export const subscribeShouldUseIndexedDB = (callback: (value: boolean) => void) => jotaiStore.sub(__generalSettingAtom, () => callback(getGeneralSettingsInternal().dataPersist)) diff --git a/apps/desktop/src/renderer/src/modules/command/commands/integration.tsx b/apps/desktop/src/renderer/src/modules/command/commands/integration.tsx index 52055fc20..508b97e4b 100644 --- a/apps/desktop/src/renderer/src/modules/command/commands/integration.tsx +++ b/apps/desktop/src/renderer/src/modules/command/commands/integration.tsx @@ -16,10 +16,13 @@ import { useTranslation } from "react-i18next" import { toast } from "sonner" import { getReadabilityContent, getReadabilityStatus, ReadabilityStatus } from "~/atoms/readability" -import { useIntegrationSettingKey } from "~/atoms/settings/integration" +import { getActionLanguage } from "~/atoms/settings/general" +import { getIntegrationSettings, useIntegrationSettingKey } from "~/atoms/settings/integration" import { useRouteParams } from "~/hooks/biz/useRouteParams" import { tipcClient } from "~/lib/client" import { parseHtml } from "~/lib/parse-html" +import { queryClient } from "~/lib/query-client" +import { Queries } from "~/queries" import type { FlatEntryModel } from "~/store/entry" import { useEntryStore } from "~/store/entry" @@ -464,29 +467,6 @@ const useRegisterCuboxCommands = () => { const enableCuboxAutoMemo = useIntegrationSettingKey("enableCuboxAutoMemo") const cuboxAvailable = enableCubox && !!cuboxToken - const buildUrlRequestBody = (entry: FlatEntryModel) => { - return { - type: "url", - content: entry.entries.url || "", - title: entry.entries.title || "", - description: entry.entries.description || "", - tags: [], - folder: "", - } - } - - const buildMemoRequestBody = (entry: FlatEntryModel, selectedText: string) => { - return { - type: "memo", - content: selectedText, - title: entry.entries.title || "", - description: entry.entries.description || "", - tags: [], - folder: "", - source_url: entry.entries.url, - } - } - useRegisterCommandEffect( !cuboxAvailable ? [] @@ -539,3 +519,42 @@ const useRegisterCuboxCommands = () => { }, ) } + +const getDescription = (entry: FlatEntryModel) => { + const actionLanguage = getActionLanguage() + const { saveSummaryAsDescription } = getIntegrationSettings() + + if (!saveSummaryAsDescription) { + return entry.entries.description || "" + } + const summary = queryClient + .getQueriesData({ + queryKey: Queries.ai.summary({ entryId: entry.entries.id, language: actionLanguage }).key, + }) + .at(0) + ?.at(1) as string | undefined + return summary || entry.entries.description || "" +} + +const buildUrlRequestBody = (entry: FlatEntryModel) => { + return { + type: "url", + content: entry.entries.url || "", + title: entry.entries.title || "", + description: getDescription(entry), + tags: [], + folder: "", + } +} + +const buildMemoRequestBody = (entry: FlatEntryModel, selectedText: string) => { + return { + type: "memo", + content: selectedText, + title: entry.entries.title || "", + description: getDescription(entry), + tags: [], + folder: "", + source_url: entry.entries.url, + } +} diff --git a/apps/desktop/src/renderer/src/modules/command/hooks/use-register-command.ts b/apps/desktop/src/renderer/src/modules/command/hooks/use-register-command.ts index 6eeb9f6ec..adf98ed0d 100644 --- a/apps/desktop/src/renderer/src/modules/command/hooks/use-register-command.ts +++ b/apps/desktop/src/renderer/src/modules/command/hooks/use-register-command.ts @@ -20,7 +20,8 @@ export const useRegisterCommandEffect = ( const { t } = useTranslation() useEffect(() => { if (!Array.isArray(options)) { - return registerCommand(options) + const unsubscribe = registerCommand(options) + return () => unsubscribe() } const unsubscribes = options.map((option) => registerCommand(option)) diff --git a/apps/desktop/src/renderer/src/modules/settings/tabs/integration.tsx b/apps/desktop/src/renderer/src/modules/settings/tabs/integration.tsx index a12d31158..b32b5e220 100644 --- a/apps/desktop/src/renderer/src/modules/settings/tabs/integration.tsx +++ b/apps/desktop/src/renderer/src/modules/settings/tabs/integration.tsx @@ -210,6 +210,9 @@ export const SettingIntegration = () => { label: t("integration.cubox.autoMemo.label"), description: t("integration.cubox.autoMemo.description"), }), + defineSettingItem("saveSummaryAsDescription", { + label: t("integration.save_ai_summary_as_description.label"), + }), BottomTip, ]} /> diff --git a/locales/settings/en.json b/locales/settings/en.json index 14cc4abe2..c79e6d20f 100644 --- a/locales/settings/en.json +++ b/locales/settings/en.json @@ -259,6 +259,7 @@ "integration.readwise.title": "Readwise", "integration.readwise.token.description": "You can get it here: ", "integration.readwise.token.label": "Readwise Access Token", + "integration.save_ai_summary_as_description.label": "Save AI Summary as description", "integration.sidebar_title": "Integration", "integration.tip": "Tip: Your sensitive data is stored locally and is not uploaded to the server.", "integration.title": "Integration", diff --git a/packages/shared/src/settings/defaults.ts b/packages/shared/src/settings/defaults.ts index adee6d4a3..2321d9602 100644 --- a/packages/shared/src/settings/defaults.ts +++ b/packages/shared/src/settings/defaults.ts @@ -90,7 +90,7 @@ export const defaultUISettings: UISettings = { export const defaultIntegrationSettings: IntegrationSettings = { // eagle - enableEagle: true, + enableEagle: false, // readwise enableReadwise: false, @@ -120,6 +120,8 @@ export const defaultIntegrationSettings: IntegrationSettings = { enableCubox: false, cuboxToken: "", enableCuboxAutoMemo: false, + + saveSummaryAsDescription: false, } export const defaultSettings = { diff --git a/packages/shared/src/settings/interface.ts b/packages/shared/src/settings/interface.ts index c481ba823..ae61b7332 100644 --- a/packages/shared/src/settings/interface.ts +++ b/packages/shared/src/settings/interface.ts @@ -106,4 +106,6 @@ export interface IntegrationSettings { enableCubox: boolean cuboxToken: string enableCuboxAutoMemo: boolean + + saveSummaryAsDescription: boolean }