forked from ob-labs/memory-powermem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
157 lines (142 loc) · 4.31 KB
/
Copy pathclient.ts
File metadata and controls
157 lines (142 loc) · 4.31 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* PowerMem HTTP API client.
* Calls POST /api/v1/memories, POST /api/v1/memories/search, DELETE /api/v1/memories/:id, GET /api/v1/system/health.
*/
import type { PowerMemConfig } from "./config.js";
export type PowerMemSearchResult = {
memory_id: number;
content: string;
score: number;
metadata?: Record<string, unknown>;
};
export type PowerMemAddResult = {
memory_id: number;
content: string;
user_id?: string;
agent_id?: string;
metadata?: Record<string, unknown>;
};
function buildUrl(baseUrl: string, path: string): string {
const base = baseUrl.replace(/\/+$/, "");
const p = path.startsWith("/") ? path : `/${path}`;
return `${base}${p}`;
}
function buildHeaders(apiKey?: string): Record<string, string> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (apiKey) {
headers["X-API-Key"] = apiKey;
}
return headers;
}
async function handleResponse<T>(res: Response, parseJson = true): Promise<T> {
const text = await res.text();
if (!res.ok) {
let message = `PowerMem API ${res.status}: ${res.statusText}`;
try {
const body = text ? JSON.parse(text) : null;
if (body?.message) message = body.message;
else if (body?.detail) message = Array.isArray(body.detail) ? body.detail.map((d: { msg?: string }) => d.msg ?? String(d)).join("; ") : String(body.detail);
} catch {
if (text) message = text.slice(0, 200);
}
throw new Error(message);
}
if (!parseJson) return undefined as T;
if (!text) return undefined as T;
return JSON.parse(text) as T;
}
export type PowerMemClientOptions = {
baseUrl: string;
apiKey?: string;
userId?: string;
agentId?: string;
};
export class PowerMemClient {
private readonly baseUrl: string;
private readonly apiKey?: string;
private readonly userId: string;
private readonly agentId: string;
constructor(options: PowerMemClientOptions) {
this.baseUrl = options.baseUrl.replace(/\/+$/, "");
this.apiKey = options.apiKey;
this.userId = options.userId ?? "openclaw-user";
this.agentId = options.agentId ?? "openclaw-agent";
}
static fromConfig(cfg: PowerMemConfig, userId: string, agentId: string): PowerMemClient {
return new PowerMemClient({
baseUrl: cfg.baseUrl,
apiKey: cfg.apiKey,
userId,
agentId,
});
}
private async request<T>(
method: string,
path: string,
body?: unknown,
parseJson = true,
): Promise<T> {
const url = buildUrl(this.baseUrl, path);
const res = await fetch(url, {
method,
headers: buildHeaders(this.apiKey),
body: body !== undefined ? JSON.stringify(body) : undefined,
});
return handleResponse<T>(res, parseJson);
}
/** GET /api/v1/system/health */
async health(): Promise<{ status: string }> {
const data = await this.request<{ data?: { status?: string } }>(
"GET",
"/api/v1/system/health",
undefined,
);
return { status: data?.data?.status ?? "unknown" };
}
/** POST /api/v1/memories */
async add(
content: string,
options: { infer?: boolean; metadata?: Record<string, unknown> } = {},
): Promise<PowerMemAddResult[]> {
const body = {
content,
user_id: this.userId,
agent_id: this.agentId,
infer: options.infer ?? true,
...(options.metadata && { metadata: options.metadata }),
};
const res = await this.request<{ success: boolean; data?: PowerMemAddResult[] }>(
"POST",
"/api/v1/memories",
body,
);
if (!res?.data) return [];
return res.data;
}
/** POST /api/v1/memories/search */
async search(query: string, limit = 5): Promise<PowerMemSearchResult[]> {
const body = {
query,
user_id: this.userId,
agent_id: this.agentId,
limit,
};
const res = await this.request<{
success: boolean;
data?: { results?: PowerMemSearchResult[] };
}>("POST", "/api/v1/memories/search", body);
return res?.data?.results ?? [];
}
/** DELETE /api/v1/memories/:memory_id */
async delete(memoryId: number | string): Promise<void> {
const id = typeof memoryId === "string" ? memoryId : String(memoryId);
await this.request(
"DELETE",
`/api/v1/memories/${id}?user_id=${encodeURIComponent(this.userId)}&agent_id=${encodeURIComponent(this.agentId)}`,
undefined,
false,
);
}
}