aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mcp/src/utils.ts
blob: 26a86436f09bdb0e11056d0bb7426dd506ad28a2 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { CallToolResult } from "@modelcontextprotocol/sdk/types";

import { KarakeepAPISchemas } from "@karakeep/sdk";

export function toMcpToolError(
  error: { code: string; message: string } | undefined,
): CallToolResult {
  return {
    isError: true,
    content: [
      {
        type: "text",
        text: error ? JSON.stringify(error) : `Something went wrong`,
      },
    ],
  };
}

interface CompactBookmark {
  id: string;
  createdAt: string;
  title: string;
  summary: string;
  note: string;
  content:
    | {
        type: "link";
        url: string;
        description: string;
        author: string;
        publisher: string;
      }
    | {
        type: "text";
        sourceUrl: string;
      }
    | {
        type: "media";
        assetId: string;
        assetType: string;
        sourceUrl: string;
      }
    | {
        type: "unknown";
      };
  tags: string[];
}

export function compactBookmark(
  bookmark: KarakeepAPISchemas["Bookmark"],
): CompactBookmark {
  let content: CompactBookmark["content"];
  if (bookmark.content.type === "link") {
    content = {
      type: "link",
      url: bookmark.content.url,
      description: bookmark.content.description ?? "",
      author: bookmark.content.author ?? "",
      publisher: bookmark.content.publisher ?? "",
    };
  } else if (bookmark.content.type === "text") {
    content = {
      type: "text",
      sourceUrl: bookmark.content.sourceUrl ?? "",
    };
  } else if (bookmark.content.type === "asset") {
    content = {
      type: "media",
      assetId: bookmark.content.assetId,
      assetType: bookmark.content.assetType,
      sourceUrl: bookmark.content.sourceUrl ?? "",
    };
  } else {
    content = {
      type: "unknown",
    };
  }

  return {
    id: bookmark.id,
    createdAt: bookmark.createdAt,
    title: bookmark.title
      ? bookmark.title
      : ((bookmark.content.type === "link"
          ? bookmark.content.title
          : undefined) ?? ""),
    summary: bookmark.summary ?? "",
    note: bookmark.note ?? "",
    content,
    tags: bookmark.tags.map((t) => t.name),
  };
}