import type { InferenceClient } from "@karakeep/shared/inference";
import { buildTextPrompt } from "@karakeep/shared/prompts.server";
import { inferTags } from "./inferenceClient";
import type { Bookmark } from "./types";
export async function extractBookmarkContent(
bookmark: Bookmark,
): Promise<string> {
if (bookmark.content.type === "link") {
const parts = [];
if (bookmark.content.url) {
parts.push(`URL: ${bookmark.content.url}`);
}
if (bookmark.title) {
parts.push(`Title: ${bookmark.title}`);
}
if (bookmark.content.description) {
parts.push(`Description: ${bookmark.content.description}`);
}
if (bookmark.content.htmlContent) {
parts.push(`Content: ${bookmark.content.htmlContent}`);
}
return parts.join("\n");
}
if (bookmark.content.type === "text" && bookmark.content.text) {
return bookmark.content.text;
}
return "";
}
export async function runTaggingForModel(
bookmark: Bookmark,
inferenceClient: InferenceClient,
lang: string = "english",
contextLength: number = 8000,
): Promise<string[]> {
const content = await extractBookmarkContent(bookmark);
if (!content) {
return [];
}
try {
// Use the shared prompt builder with empty custom prompts and default tag style
const prompt = await buildTextPrompt(
lang,
[], // No custom prompts for comparison tool
content,
contextLength,
"as-generated", // Use tags as generated by the model
);
const tags = await inferTags(inferenceClient, prompt);
return tags;
} catch (error) {
throw new Error(
`Failed to generate tags: ${error instanceof Error ? error.message : String(error)}`,
);
}
}