@ekodb/ekodb-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,404 @@
1
+ /**
2
+ * ekoDB TypeScript Client
3
+ */
4
+ import { QueryBuilder } from './query-builder';
5
+ import { SearchQuery, SearchQueryBuilder, SearchResponse } from './search';
6
+ import { Schema, SchemaBuilder, CollectionMetadata } from './schema';
7
+ export interface Record {
8
+ [key: string]: any;
9
+ }
10
+ /**
11
+ * Rate limit information from the server
12
+ */
13
+ export interface RateLimitInfo {
14
+ /** Maximum requests allowed per window */
15
+ limit: number;
16
+ /** Requests remaining in current window */
17
+ remaining: number;
18
+ /** Unix timestamp when the rate limit resets */
19
+ reset: number;
20
+ }
21
+ /**
22
+ * Client configuration options
23
+ */
24
+ export interface ClientConfig {
25
+ /** Base URL of the ekoDB server */
26
+ baseURL: string;
27
+ /** API key for authentication */
28
+ apiKey: string;
29
+ /** Enable automatic retries for rate limiting and transient errors (default: true) */
30
+ shouldRetry?: boolean;
31
+ /** Maximum number of retry attempts (default: 3) */
32
+ maxRetries?: number;
33
+ /** Request timeout in milliseconds (default: 30000) */
34
+ timeout?: number;
35
+ }
36
+ /**
37
+ * Rate limit error
38
+ */
39
+ export declare class RateLimitError extends Error {
40
+ retryAfterSecs: number;
41
+ constructor(retryAfterSecs: number, message?: string);
42
+ }
43
+ export interface Query {
44
+ limit?: number;
45
+ offset?: number;
46
+ filter?: Record;
47
+ }
48
+ export interface BatchOperationResult {
49
+ successful: string[];
50
+ failed: Array<{
51
+ id?: string;
52
+ error: string;
53
+ }>;
54
+ }
55
+ export interface CollectionConfig {
56
+ collection_name: string;
57
+ fields?: string[];
58
+ search_options?: any;
59
+ }
60
+ export interface ChatRequest {
61
+ collections: CollectionConfig[];
62
+ llm_provider: string;
63
+ llm_model?: string;
64
+ message: string;
65
+ system_prompt?: string;
66
+ bypass_ripple?: boolean;
67
+ }
68
+ export interface CreateChatSessionRequest {
69
+ collections: CollectionConfig[];
70
+ llm_provider: string;
71
+ llm_model?: string;
72
+ system_prompt?: string;
73
+ bypass_ripple?: boolean;
74
+ parent_id?: string;
75
+ branch_point_idx?: number;
76
+ max_context_messages?: number;
77
+ }
78
+ export interface ChatMessageRequest {
79
+ message: string;
80
+ bypass_ripple?: boolean;
81
+ force_summarize?: boolean;
82
+ }
83
+ export interface TokenUsage {
84
+ prompt_tokens: number;
85
+ completion_tokens: number;
86
+ total_tokens: number;
87
+ }
88
+ export interface ChatResponse {
89
+ chat_id: string;
90
+ message_id: string;
91
+ responses: string[];
92
+ context_snippets: any[];
93
+ execution_time_ms: number;
94
+ token_usage?: TokenUsage;
95
+ }
96
+ export interface ChatSession {
97
+ chat_id: string;
98
+ created_at: string;
99
+ updated_at: string;
100
+ llm_provider: string;
101
+ llm_model: string;
102
+ collections: CollectionConfig[];
103
+ system_prompt?: string;
104
+ title?: string;
105
+ message_count: number;
106
+ }
107
+ export interface ChatSessionResponse {
108
+ session: Record;
109
+ message_count: number;
110
+ }
111
+ export interface ListSessionsQuery {
112
+ limit?: number;
113
+ skip?: number;
114
+ sort?: string;
115
+ }
116
+ export interface ListSessionsResponse {
117
+ sessions: ChatSession[];
118
+ total: number;
119
+ returned: number;
120
+ skip: number;
121
+ limit?: number;
122
+ }
123
+ export interface GetMessagesQuery {
124
+ limit?: number;
125
+ skip?: number;
126
+ sort?: string;
127
+ }
128
+ export interface GetMessagesResponse {
129
+ messages: Record[];
130
+ total: number;
131
+ skip: number;
132
+ limit?: number;
133
+ returned: number;
134
+ }
135
+ export interface UpdateSessionRequest {
136
+ system_prompt?: string;
137
+ llm_model?: string;
138
+ collections?: CollectionConfig[];
139
+ max_context_messages?: number;
140
+ }
141
+ export declare enum MergeStrategy {
142
+ Chronological = "Chronological",
143
+ Summarized = "Summarized",
144
+ LatestOnly = "LatestOnly"
145
+ }
146
+ export interface MergeSessionsRequest {
147
+ source_chat_ids: string[];
148
+ target_chat_id: string;
149
+ merge_strategy: MergeStrategy;
150
+ }
151
+ export declare class EkoDBClient {
152
+ private baseURL;
153
+ private apiKey;
154
+ private token;
155
+ private shouldRetry;
156
+ private maxRetries;
157
+ private timeout;
158
+ private rateLimitInfo;
159
+ constructor(config: string | ClientConfig, apiKey?: string);
160
+ /**
161
+ * Initialize the client by getting an auth token
162
+ */
163
+ init(): Promise<void>;
164
+ /**
165
+ * Get the current rate limit information
166
+ */
167
+ getRateLimitInfo(): RateLimitInfo | null;
168
+ /**
169
+ * Check if approaching rate limit (less than 10% remaining)
170
+ */
171
+ isNearRateLimit(): boolean;
172
+ /**
173
+ * Refresh the authentication token
174
+ */
175
+ private refreshToken;
176
+ /**
177
+ * Extract rate limit information from response headers
178
+ */
179
+ private extractRateLimitInfo;
180
+ /**
181
+ * Sleep for a specified number of seconds
182
+ */
183
+ private sleep;
184
+ /**
185
+ * Make an HTTP request to the ekoDB API with retry logic
186
+ */
187
+ private makeRequest;
188
+ /**
189
+ * Insert a document into a collection
190
+ */
191
+ insert(collection: string, record: Record, ttl?: string): Promise<Record>;
192
+ /**
193
+ * Find documents in a collection
194
+ *
195
+ * @param collection - Collection name
196
+ * @param query - Query object or QueryBuilder instance
197
+ * @returns Array of matching records
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * // Using QueryBuilder
202
+ * const results = await client.find("users",
203
+ * new QueryBuilder()
204
+ * .eq("status", "active")
205
+ * .gt("age", 18)
206
+ * .sortDesc("created_at")
207
+ * .limit(10)
208
+ * );
209
+ *
210
+ * // Using plain Query object
211
+ * const results = await client.find("users", { limit: 10 });
212
+ * ```
213
+ */
214
+ find(collection: string, query?: Query | QueryBuilder): Promise<Record[]>;
215
+ /**
216
+ * Find a document by ID
217
+ */
218
+ findByID(collection: string, id: string): Promise<Record>;
219
+ /**
220
+ * Update a document
221
+ */
222
+ update(collection: string, id: string, record: Record): Promise<Record>;
223
+ /**
224
+ * Delete a document
225
+ */
226
+ delete(collection: string, id: string): Promise<void>;
227
+ /**
228
+ * Batch insert multiple documents
229
+ */
230
+ batchInsert(collection: string, records: Record[]): Promise<Record[]>;
231
+ /**
232
+ * Batch update multiple documents
233
+ */
234
+ batchUpdate(collection: string, updates: Array<{
235
+ id: string;
236
+ data: Record;
237
+ }>): Promise<Record[]>;
238
+ /**
239
+ * Batch delete multiple documents
240
+ */
241
+ batchDelete(collection: string, ids: string[]): Promise<number>;
242
+ /**
243
+ * Set a key-value pair
244
+ */
245
+ kvSet(key: string, value: any): Promise<void>;
246
+ /**
247
+ * Get a value by key
248
+ */
249
+ kvGet(key: string): Promise<any>;
250
+ /**
251
+ * Delete a key
252
+ */
253
+ kvDelete(key: string): Promise<void>;
254
+ /**
255
+ * List all collections
256
+ */
257
+ listCollections(): Promise<string[]>;
258
+ /**
259
+ * Delete a collection
260
+ */
261
+ deleteCollection(collection: string): Promise<void>;
262
+ /**
263
+ * Create a collection with schema
264
+ *
265
+ * @param collection - Collection name
266
+ * @param schema - Schema definition or SchemaBuilder instance
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * const schema = new SchemaBuilder()
271
+ * .addField("name", new FieldTypeSchemaBuilder("string").required())
272
+ * .addField("email", new FieldTypeSchemaBuilder("string").unique())
273
+ * .addField("age", new FieldTypeSchemaBuilder("number").range(0, 150));
274
+ *
275
+ * await client.createCollection("users", schema);
276
+ * ```
277
+ */
278
+ createCollection(collection: string, schema: Schema | SchemaBuilder): Promise<void>;
279
+ /**
280
+ * Get collection metadata and schema
281
+ *
282
+ * @param collection - Collection name
283
+ * @returns Collection metadata including schema and analytics
284
+ */
285
+ getCollection(collection: string): Promise<CollectionMetadata>;
286
+ /**
287
+ * Get collection schema
288
+ *
289
+ * @param collection - Collection name
290
+ * @returns The collection schema
291
+ */
292
+ getSchema(collection: string): Promise<Schema>;
293
+ /**
294
+ * Search documents in a collection using full-text, vector, or hybrid search
295
+ *
296
+ * @param collection - Collection name
297
+ * @param searchQuery - Search query object or SearchQueryBuilder instance
298
+ * @returns Search response with results and metadata
299
+ *
300
+ * @example
301
+ * ```typescript
302
+ * // Full-text search
303
+ * const results = await client.search("users",
304
+ * new SearchQueryBuilder("john")
305
+ * .fields(["name", "email"])
306
+ * .fuzzy(true)
307
+ * .limit(10)
308
+ * );
309
+ *
310
+ * // Vector search
311
+ * const results = await client.search("documents",
312
+ * new SearchQueryBuilder("")
313
+ * .vector([0.1, 0.2, 0.3, ...])
314
+ * .vectorK(5)
315
+ * );
316
+ *
317
+ * // Hybrid search
318
+ * const results = await client.search("products",
319
+ * new SearchQueryBuilder("laptop")
320
+ * .vector([0.1, 0.2, ...])
321
+ * .textWeight(0.7)
322
+ * .vectorWeight(0.3)
323
+ * );
324
+ * ```
325
+ */
326
+ search(collection: string, searchQuery: SearchQuery | SearchQueryBuilder): Promise<SearchResponse>;
327
+ /**
328
+ * Create a new chat session
329
+ */
330
+ createChatSession(request: CreateChatSessionRequest): Promise<ChatResponse>;
331
+ /**
332
+ * Send a message in an existing chat session
333
+ */
334
+ chatMessage(sessionId: string, request: ChatMessageRequest): Promise<ChatResponse>;
335
+ /**
336
+ * Get a chat session by ID
337
+ */
338
+ getChatSession(sessionId: string): Promise<ChatSessionResponse>;
339
+ /**
340
+ * List all chat sessions
341
+ */
342
+ listChatSessions(query?: ListSessionsQuery): Promise<ListSessionsResponse>;
343
+ /**
344
+ * Get messages from a chat session
345
+ */
346
+ getChatSessionMessages(sessionId: string, query?: GetMessagesQuery): Promise<GetMessagesResponse>;
347
+ /**
348
+ * Update a chat session
349
+ */
350
+ updateChatSession(sessionId: string, request: UpdateSessionRequest): Promise<ChatSessionResponse>;
351
+ /**
352
+ * Branch a chat session
353
+ */
354
+ branchChatSession(request: CreateChatSessionRequest): Promise<ChatResponse>;
355
+ /**
356
+ * Delete a chat session
357
+ */
358
+ deleteChatSession(sessionId: string): Promise<void>;
359
+ /**
360
+ * Regenerate an AI response message
361
+ */
362
+ regenerateMessage(sessionId: string, messageId: string): Promise<ChatResponse>;
363
+ /**
364
+ * Update a specific message
365
+ */
366
+ updateChatMessage(sessionId: string, messageId: string, content: string): Promise<void>;
367
+ /**
368
+ * Delete a specific message
369
+ */
370
+ deleteChatMessage(sessionId: string, messageId: string): Promise<void>;
371
+ /**
372
+ * Toggle the "forgotten" status of a message
373
+ */
374
+ toggleForgottenMessage(sessionId: string, messageId: string, forgotten: boolean): Promise<void>;
375
+ /**
376
+ * Merge multiple chat sessions into one
377
+ */
378
+ mergeChatSessions(request: MergeSessionsRequest): Promise<ChatSessionResponse>;
379
+ /**
380
+ * Create a WebSocket client
381
+ */
382
+ websocket(wsURL: string): WebSocketClient;
383
+ }
384
+ /**
385
+ * WebSocket client for real-time queries
386
+ */
387
+ export declare class WebSocketClient {
388
+ private wsURL;
389
+ private token;
390
+ private ws;
391
+ constructor(wsURL: string, token: string);
392
+ /**
393
+ * Connect to WebSocket
394
+ */
395
+ private connect;
396
+ /**
397
+ * Find all records in a collection via WebSocket
398
+ */
399
+ findAll(collection: string): Promise<Record[]>;
400
+ /**
401
+ * Close the WebSocket connection
402
+ */
403
+ close(): void;
404
+ }