-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupermemory-client.ts
More file actions
127 lines (111 loc) · 3.26 KB
/
supermemory-client.ts
File metadata and controls
127 lines (111 loc) · 3.26 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Supermemory API Client (v3)
*
* Wraps the Supermemory REST API for memory storage and search.
* API docs: https://supermemory.ai/docs/api-reference
*/
import type {
SupermemoryAddResponse,
SupermemorySearchResponse,
SupermemorySearchResult,
} from "./types.js";
export type SupermemoryClientOptions = {
apiKey: string;
baseUrl?: string;
};
export class SupermemoryClient {
private readonly apiKey: string;
private readonly baseUrl: string;
constructor(options: SupermemoryClientOptions) {
this.apiKey = options.apiKey;
this.baseUrl = options.baseUrl ?? "https://api.supermemory.ai";
}
/**
* Add content to long-term memory.
* POST /v3/documents
*/
async add(params: {
content: string;
containerTag?: string;
metadata?: Record<string, unknown>;
customId?: string;
}): Promise<SupermemoryAddResponse> {
const url = new URL("/v3/documents", this.baseUrl);
const body: Record<string, unknown> = { content: params.content };
if (params.containerTag) {
// containerTags is an array in v3 API
body.containerTags = [params.containerTag];
}
if (params.metadata) {
body.metadata = params.metadata;
}
if (params.customId) {
body.customId = params.customId;
}
const response = await fetch(url.toString(), {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Supermemory add failed: ${response.status} ${text}`);
}
return response.json() as Promise<SupermemoryAddResponse>;
}
/**
* Search memories for a given query.
* POST /v3/search
*/
async search(params: {
q: string;
containerTag?: string;
limit?: number;
chunkThreshold?: number;
rewriteQuery?: boolean;
}): Promise<SupermemorySearchResult[]> {
const url = new URL("/v3/search", this.baseUrl);
const body: Record<string, unknown> = { q: params.q };
if (params.containerTag) {
body.containerTags = [params.containerTag];
}
if (params.limit !== undefined) {
body.limit = params.limit;
}
if (params.chunkThreshold !== undefined) {
body.chunkThreshold = params.chunkThreshold;
}
if (params.rewriteQuery !== undefined) {
body.rewriteQuery = params.rewriteQuery;
}
const response = await fetch(url.toString(), {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Supermemory search failed: ${response.status} ${text}`);
}
const data = (await response.json()) as SupermemorySearchResponse;
// Flatten chunks from all documents into search results
const results: SupermemorySearchResult[] = [];
for (const doc of data.results ?? []) {
for (const chunk of doc.chunks ?? []) {
results.push({
id: doc.documentId,
content: chunk.content,
score: chunk.score,
metadata: doc.metadata,
});
}
}
return results;
}
}