|
| 1 | +/** |
| 2 | + * Graphiti API Client (stub) |
| 3 | + * Wraps Graphiti REST endpoints or MCP tools with simple methods |
| 4 | + */ |
| 5 | + |
| 6 | +import type { |
| 7 | + Episode, |
| 8 | + EntityNode, |
| 9 | + RelationEdge, |
| 10 | + TemporalQuery, |
| 11 | + GraphContext, |
| 12 | + GraphitiStatus, |
| 13 | +} from './types.js'; |
| 14 | +import type { GraphitiIntegrationConfig } from './config.js'; |
| 15 | +import { DEFAULT_GRAPHITI_CONFIG } from './config.js'; |
| 16 | + |
| 17 | +export class GraphitiClientError extends Error { |
| 18 | + constructor( |
| 19 | + message: string, |
| 20 | + public readonly code?: string, |
| 21 | + public readonly statusCode?: number |
| 22 | + ) { |
| 23 | + super(message); |
| 24 | + this.name = 'GraphitiClientError'; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export class GraphitiClient { |
| 29 | + private readonly endpoint: string; |
| 30 | + private readonly timeout: number; |
| 31 | + private readonly maxRetries: number; |
| 32 | + private readonly namespace: string; |
| 33 | + |
| 34 | + constructor(config: Partial<GraphitiIntegrationConfig> = {}) { |
| 35 | + const merged = { ...DEFAULT_GRAPHITI_CONFIG, ...config }; |
| 36 | + this.endpoint = merged.endpoint.replace(/\/$/, ''); |
| 37 | + this.timeout = merged.timeoutMs; |
| 38 | + this.maxRetries = merged.maxRetries; |
| 39 | + this.namespace = merged.projectNamespace || 'default'; |
| 40 | + } |
| 41 | + |
| 42 | + private async request<T>( |
| 43 | + path: string, |
| 44 | + options: RequestInit = {} |
| 45 | + ): Promise<T> { |
| 46 | + const controller = new AbortController(); |
| 47 | + const timeoutId = setTimeout(() => controller.abort(), this.timeout); |
| 48 | + |
| 49 | + let lastError: Error | undefined; |
| 50 | + for (let attempt = 0; attempt <= this.maxRetries; attempt++) { |
| 51 | + try { |
| 52 | + const res = await fetch(`${this.endpoint}${path}`, { |
| 53 | + ...options, |
| 54 | + signal: controller.signal, |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + ...(options.headers || {}), |
| 58 | + }, |
| 59 | + }); |
| 60 | + clearTimeout(timeoutId); |
| 61 | + if (!res.ok) { |
| 62 | + const msg = await res.text().catch(() => res.statusText); |
| 63 | + throw new GraphitiClientError( |
| 64 | + `Request failed: ${msg}`, |
| 65 | + 'HTTP_ERROR', |
| 66 | + res.status |
| 67 | + ); |
| 68 | + } |
| 69 | + return (await res.json()) as T; |
| 70 | + } catch (err) { |
| 71 | + lastError = err as Error; |
| 72 | + if (err instanceof GraphitiClientError) throw err; |
| 73 | + if ((err as Error).name === 'AbortError') { |
| 74 | + throw new GraphitiClientError('Request timeout', 'TIMEOUT'); |
| 75 | + } |
| 76 | + if (attempt < this.maxRetries) { |
| 77 | + await new Promise((r) => setTimeout(r, Math.pow(2, attempt) * 100)); |
| 78 | + continue; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + clearTimeout(timeoutId); |
| 83 | + throw new GraphitiClientError( |
| 84 | + lastError?.message || 'Network error', |
| 85 | + 'NETWORK_ERROR' |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + // Episodes |
| 90 | + async upsertEpisode(episode: Episode): Promise<{ id: string }> { |
| 91 | + const payload = { ...episode, namespace: this.namespace }; |
| 92 | + const res = await this.request<{ id: string }>(`/episodes`, { |
| 93 | + method: 'POST', |
| 94 | + body: JSON.stringify(payload), |
| 95 | + }); |
| 96 | + return res; |
| 97 | + } |
| 98 | + |
| 99 | + // Entities |
| 100 | + async upsertEntities(entities: EntityNode[]): Promise<{ ids: string[] }> { |
| 101 | + const payload = { entities, namespace: this.namespace }; |
| 102 | + return this.request<{ ids: string[] }>(`/entities:batchUpsert`, { |
| 103 | + method: 'POST', |
| 104 | + body: JSON.stringify(payload), |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + // Relations |
| 109 | + async upsertRelations(edges: RelationEdge[]): Promise<{ ids: string[] }> { |
| 110 | + const payload = { edges, namespace: this.namespace }; |
| 111 | + return this.request<{ ids: string[] }>(`/relations:batchUpsert`, { |
| 112 | + method: 'POST', |
| 113 | + body: JSON.stringify(payload), |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + // Temporal query + hybrid retrieval |
| 118 | + async queryTemporal(query: TemporalQuery): Promise<GraphContext> { |
| 119 | + const payload = { ...query, namespace: this.namespace }; |
| 120 | + return this.request<GraphContext>(`/query/temporal`, { |
| 121 | + method: 'POST', |
| 122 | + body: JSON.stringify(payload), |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + // Health/status |
| 127 | + async getStatus(): Promise<GraphitiStatus> { |
| 128 | + try { |
| 129 | + return await this.request<GraphitiStatus>( |
| 130 | + `/status?namespace=${encodeURIComponent(this.namespace)}` |
| 131 | + ); |
| 132 | + } catch { |
| 133 | + return { connected: false }; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments