aboutsummaryrefslogtreecommitdiffstats
path: root/tools/compare-models/src/bookmarkProcessor.ts
blob: 21280b974da8bffa7b9110a1a031ac5db79e4da7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { InferenceClient } from "@karakeep/shared/inference";
import { buildTextPrompt } from "@karakeep/shared/prompts";

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)}`,
    );
  }
}