From ff3078e93411c3467d797258744a7f17a7dbdf0a Mon Sep 17 00:00:00 2001 From: polwex Date: Wed, 16 Jul 2025 10:07:06 +0700 Subject: m --- app/src/pages/categorize.tsx | 171 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 app/src/pages/categorize.tsx (limited to 'app/src/pages/categorize.tsx') diff --git a/app/src/pages/categorize.tsx b/app/src/pages/categorize.tsx new file mode 100644 index 0000000..d91d8c1 --- /dev/null +++ b/app/src/pages/categorize.tsx @@ -0,0 +1,171 @@ +import * as Bun from "bun"; +import { Suspense } from "react"; +import { TwitterApiService, TwitterBookmark } from "../lib/twitter-api"; +import { + userCategories, + type CategorizationResponse, +} from "../lib/categorization"; +import { LLMService } from "../lib/llm-service"; +import ProcessedBookmark from "../components/cat/Entry"; + +interface CategorizePageProps { + bookmarks: Awaited>; + currentBookmarkIndex: number; + categorization?: CategorizationResponse; + error?: string; +} + +async function CategorizationFetcher({ + bookmarks, + currentIndex, +}: { + bookmarks: TwitterBookmark[]; + currentIndex: number; +}) { + if (currentIndex >= bookmarks.length) { + return ( +
+

+ All Bookmarks Categorized! +

+

+ You've successfully categorized all your bookmarks. +

+ + Back to Bookmarks + +
+ ); + } + + const bookmark = bookmarks[currentIndex]; + const llmRes = await callLLM(bookmark!); + if ("error" in llmRes) + return ( +
+ Error categorizing bookmark: {llmRes.error} +
+ ); + return ( +
+ +
+ ); +} + +function LoadingSpinner() { + return ( +
+
+ + Loading your Twitter bookmarks... + +
+ ); +} + +export default async function CategorizePage(props: any) { + // console.log({ props }); + const params = new URLSearchParams(props.query); + const currentIndex = Number(params.get("idx") || "0"); + const cookie = Bun.env.TWATTER_COKI; + + if (!cookie) { + return ( +
+ Missing Twitter cookie configuration +
+ ); + } + const twbookmarks = await getTwData(cookie); + if ("error" in twbookmarks) { + return ( +
+ Error fetching Twatter bookmarks +
+ ); + } + // const currentIndex = parseInt(searchParams.index || '0', 10); + const totalCount = twbookmarks.ok.length; + + return ( +
+
+ Categorize Bookmarks - SORMARK + +
+

+ Categorize Bookmarks +

+

+ Review and categorize your Twitter bookmarks one by one +

+
+ + }> + <> +
+

+ Bookmark {currentIndex + 1} of {totalCount} +

+
+
+
+
+ + +
+
+
+ ); +} + +import bmarks from "../lib/testData.json"; +async function getTwData(cookie: string) { + try { + // const twitterService = new TwitterApiService(cookie); + // const bookmarks = await twitterService.fetchAllBookmarks(); + // return { ok: bookmarks }; + return { ok: bmarks } as any; + } catch (error) { + return { error: `${error}` }; + } +} + +async function callLLM(bookmark: TwitterBookmark) { + const apiKey = Bun.env.GEMINI_API_KEY!; + try { + const llmService = new LLMService(apiKey); + + const categorization = await llmService.categorizeBookmark({ + bookmark, + userCategories, + }); + + return { ok: categorization }; + } catch (error) { + return { error: `${error}` }; + } +} + +export const getConfig = async () => { + return { + render: "dynamic", + } as const; +}; -- cgit v1.2.3