|
| 1 | +export type MemoryItem = { |
| 2 | + id: string; |
| 3 | + namespace: string; |
| 4 | + project_id: string; |
| 5 | + kind: string; |
| 6 | + text: string; |
| 7 | + metadata?: Record<string, unknown>; |
| 8 | +}; |
| 9 | + |
| 10 | +export class ContextForgeClient { |
| 11 | + private baseUrl: string; |
| 12 | + |
| 13 | + constructor(baseUrl: string) { |
| 14 | + this.baseUrl = baseUrl.replace(/\/$/, ""); |
| 15 | + } |
| 16 | + |
| 17 | + async health(): Promise<{ status: string }> { |
| 18 | + const r = await fetch(`${this.baseUrl}/v0/health`); |
| 19 | + if (!r.ok) throw new Error(`health failed: ${r.status}`); |
| 20 | + return r.json(); |
| 21 | + } |
| 22 | + |
| 23 | + async store(items: MemoryItem[]): Promise<{ stored: number }> { |
| 24 | + const r = await fetch(`${this.baseUrl}/v0/store`, { |
| 25 | + method: "POST", |
| 26 | + headers: { "Content-Type": "application/json" }, |
| 27 | + body: JSON.stringify({ items }), |
| 28 | + }); |
| 29 | + if (!r.ok) throw new Error(`store failed: ${r.status}`); |
| 30 | + return r.json(); |
| 31 | + } |
| 32 | + |
| 33 | + async search(namespace: string, project_id: string, query: string, top_k = 5): Promise<{ results: MemoryItem[] }> { |
| 34 | + const r = await fetch(`${this.baseUrl}/v0/search`, { |
| 35 | + method: "POST", |
| 36 | + headers: { "Content-Type": "application/json" }, |
| 37 | + body: JSON.stringify({ namespace, project_id, query, top_k }), |
| 38 | + }); |
| 39 | + if (!r.ok) throw new Error(`search failed: ${r.status}`); |
| 40 | + return r.json(); |
| 41 | + } |
| 42 | + |
| 43 | + async embed(texts: string[]): Promise<{ vectors: number[][] }> { |
| 44 | + const r = await fetch(`${this.baseUrl}/v0/embed`, { |
| 45 | + method: "POST", |
| 46 | + headers: { "Content-Type": "application/json" }, |
| 47 | + body: JSON.stringify({ texts }), |
| 48 | + }); |
| 49 | + if (!r.ok) throw new Error(`embed failed: ${r.status}`); |
| 50 | + return r.json(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
0 commit comments