aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2024-10-27 00:12:11 +0000
committerMohamed Bassem <me@mbassem.com>2024-10-27 00:12:11 +0000
commit731d2dfbea39aa140ccb6d2d2cabd49186320299 (patch)
tree2311d04b5dc61102c63d4e4ec9c7c97b359faad6 /packages/trpc
parent3e727f7ba3ad157ca1ccc6100711266cae1bde23 (diff)
downloadkarakeep-731d2dfbea39aa140ccb6d2d2cabd49186320299.tar.zst
feature: Add a summarize with AI button for links
Diffstat (limited to 'packages/trpc')
-rw-r--r--packages/trpc/routers/bookmarks.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index 6439111a..c5147c70 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -21,6 +21,9 @@ import {
tagsOnBookmarks,
} from "@hoarder/db/schema";
import { deleteAsset } from "@hoarder/shared/assetdb";
+import serverConfig from "@hoarder/shared/config";
+import { InferenceClientFactory } from "@hoarder/shared/inference";
+import { buildSummaryPrompt } from "@hoarder/shared/prompts";
import {
LinkCrawlerQueue,
OpenAIQueue,
@@ -393,6 +396,7 @@ export const bookmarksAppRouter = router({
archived: input.archived,
favourited: input.favourited,
note: input.note,
+ summary: input.summary,
createdAt: input.createdAt,
})
.where(
@@ -969,4 +973,69 @@ export const bookmarksAppRouter = router({
() => ({}),
);
}),
+ summarizeBookmark: authedProcedure
+ .input(
+ z.object({
+ bookmarkId: z.string(),
+ }),
+ )
+ .output(
+ z.object({
+ summary: z.string(),
+ }),
+ )
+ .use(ensureBookmarkOwnership)
+ .mutation(async ({ input, ctx }) => {
+ const inferenceClient = InferenceClientFactory.build();
+ if (!inferenceClient) {
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message: "No inference client configured",
+ });
+ }
+ const bookmark = await ctx.db.query.bookmarkLinks.findFirst({
+ where: and(eq(bookmarks.id, input.bookmarkId)),
+ });
+
+ if (!bookmark) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "Bookmark not found or not a link",
+ });
+ }
+
+ const bookmarkDetails = `
+Title: ${bookmark.title ?? ""}
+Description: ${bookmark.description ?? ""}
+Content: ${bookmark.content ?? ""}
+`;
+
+ const summaryPrompt = buildSummaryPrompt(
+ serverConfig.inference.inferredTagLang,
+ bookmarkDetails,
+ serverConfig.inference.contextLength,
+ );
+
+ const summary = await inferenceClient.inferFromText(summaryPrompt, {
+ json: false,
+ });
+
+ if (!summary.response) {
+ throw new TRPCError({
+ code: "INTERNAL_SERVER_ERROR",
+ message: "Failed to summarize bookmark",
+ });
+ }
+ await ctx.db
+ .update(bookmarks)
+ .set({
+ summary: summary.response,
+ })
+ .where(eq(bookmarks.id, input.bookmarkId));
+
+ return {
+ bookmarkId: input.bookmarkId,
+ summary: summary.response,
+ };
+ }),
});