This repository was archived by the owner on Jun 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathclient.ts
More file actions
71 lines (61 loc) · 1.72 KB
/
Copy pathclient.ts
File metadata and controls
71 lines (61 loc) · 1.72 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
import type { XMemOpenClawConfig } from "./config.ts"
import { redactSecrets, truncate } from "./memory.ts"
export type XMemSearchResult = {
domain?: string
content?: string
score?: number
metadata?: Record<string, unknown>
}
export class XMemClient {
constructor(private cfg: XMemOpenClawConfig) {}
status() {
return {
apiKeyConfigured: Boolean(this.cfg.apiKey),
apiUrl: this.cfg.apiUrl,
userId: this.cfg.userId,
}
}
private async request(pathname: string, payload: Record<string, unknown>) {
if (!this.cfg.apiKey) {
throw new Error("XMEM_API_KEY is not configured.")
}
const response = await fetch(`${this.cfg.apiUrl}${pathname}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.cfg.apiKey}`,
},
body: JSON.stringify(payload),
})
const text = await response.text()
let body: any
try {
body = JSON.parse(text)
} catch {
body = { error: text }
}
if (!response.ok || body?.status === "error") {
throw new Error(body?.error || body?.detail || `XMem request failed with HTTP ${response.status}`)
}
return body?.data ?? body
}
async search(query: string, limit = 8): Promise<XMemSearchResult[]> {
const data = await this.request("/v1/memory/search", {
query: redactSecrets(query),
user_id: this.cfg.userId,
top_k: limit,
domains: ["profile", "temporal", "summary"],
})
return data?.results || []
}
async addMemory(text: string, metadata: Record<string, unknown> = {}) {
return this.request("/v1/memory/ingest", {
user_query: truncate(redactSecrets(text)),
agent_response: "",
user_id: this.cfg.userId,
session_datetime: new Date().toISOString(),
effort_level: "low",
metadata,
})
}
}