@ragable/sdk 0.0.2 → 0.3.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.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,89 @@
1
+ /**
2
+ * Supabase-style schema types for `createBrowserClient<Database>()`.
3
+ * Define `Row` (and optionally `Insert` / `Update`) per table, then use
4
+ * `Tables<Database, 'my_table'>` with `database.query<...>()`.
5
+ *
6
+ * @see https://supabase.com/docs/reference/javascript/typescript-support
7
+ */
8
+ /** JSON-serializable values (matches common Supabase `Json` definition). */
9
+ type Json = string | number | boolean | null | {
10
+ [key: string]: Json | undefined;
11
+ } | Json[];
12
+ /** One table’s type bundle — at minimum provide `Row`. */
13
+ type RagableTableDefinition<Row extends Record<string, unknown> = Record<string, unknown>, Insert extends Record<string, unknown> = Row, Update extends Record<string, unknown> = Partial<Row>> = {
14
+ Row: Row;
15
+ Insert?: Insert;
16
+ Update?: Update;
17
+ Relationships?: unknown;
18
+ };
19
+ /**
20
+ * Top-level database shape (Supabase `Database` pattern).
21
+ * Consumers extend `public.Tables` with table names → {@link RagableTableDefinition}.
22
+ */
23
+ interface RagableDatabase {
24
+ public: {
25
+ Tables: Record<string, RagableTableDefinition>;
26
+ Views?: Record<string, {
27
+ Row: Record<string, unknown>;
28
+ }>;
29
+ Enums?: Record<string, string>;
30
+ };
31
+ }
32
+ /**
33
+ * Default schema when no generic is passed to `createBrowserClient`.
34
+ * `Record<string, never>` for `Tables` is assignable to `Record<string, RagableTableDefinition>`;
35
+ * use `database.query<YourRow>()` or pass a concrete `Database` generic.
36
+ */
37
+ interface DefaultRagableDatabase extends RagableDatabase {
38
+ public: {
39
+ Tables: Record<string, never>;
40
+ Views: Record<string, never>;
41
+ Enums: Record<string, never>;
42
+ };
43
+ }
44
+ /** Row type for a table name (Supabase `Tables<'x'>` pattern). */
45
+ type Tables<D extends RagableDatabase, TableName extends keyof D["public"]["Tables"] & string> = [D["public"]["Tables"][TableName]] extends [never] ? Record<string, unknown> : D["public"]["Tables"][TableName] extends {
46
+ Row: infer R;
47
+ } ? R extends Record<string, unknown> ? R : Record<string, unknown> : Record<string, unknown>;
48
+ /** Insert payload type when declared; otherwise a partial of `Row`. */
49
+ type TablesInsert<D extends RagableDatabase, TableName extends keyof D["public"]["Tables"] & string> = [D["public"]["Tables"][TableName]] extends [never] ? Record<string, unknown> : D["public"]["Tables"][TableName] extends {
50
+ Insert: infer I;
51
+ } ? I extends Record<string, unknown> ? I : Record<string, unknown> : Partial<Tables<D, TableName>>;
52
+ /** Update payload type when declared; otherwise partial `Row`. */
53
+ type TablesUpdate<D extends RagableDatabase, TableName extends keyof D["public"]["Tables"] & string> = [D["public"]["Tables"][TableName]] extends [never] ? Record<string, unknown> : D["public"]["Tables"][TableName] extends {
54
+ Update: infer U;
55
+ } ? U extends Record<string, unknown> ? U : Record<string, unknown> : Partial<Tables<D, TableName>>;
56
+
1
57
  interface RagableClientOptions {
2
58
  apiKey: string;
3
59
  baseUrl?: string;
4
60
  fetch?: typeof fetch;
5
61
  headers?: HeadersInit;
6
62
  }
63
+ type RequestOptions = Omit<RequestInit, "body"> & {
64
+ body?: unknown;
65
+ };
66
+ declare class RagableError extends Error {
67
+ readonly status: number;
68
+ readonly body: unknown;
69
+ constructor(message: string, status: number, body: unknown);
70
+ }
71
+ declare function extractErrorMessage(payload: unknown, fallback: string): string;
72
+ declare class RagableRequestClient {
73
+ private readonly apiKey;
74
+ private readonly baseUrl;
75
+ private readonly fetchImpl;
76
+ private readonly defaultHeaders;
77
+ constructor(options: RagableClientOptions);
78
+ toUrl(path: string): string;
79
+ request<T>(path: string, options?: RequestOptions): Promise<T>;
80
+ /**
81
+ * Low-level fetch with API key and JSON body encoding. Caller handles status and body.
82
+ */
83
+ rawFetch(path: string, options?: RequestOptions): Promise<Response>;
84
+ private parseResponseBody;
85
+ }
86
+
7
87
  interface ShiftIndex {
8
88
  id: string;
9
89
  name: string;
@@ -70,24 +150,6 @@ interface ShiftListEntriesResponse {
70
150
  entries: ShiftEntry[];
71
151
  total: number;
72
152
  }
73
- declare class RagableError extends Error {
74
- readonly status: number;
75
- readonly body: unknown;
76
- constructor(message: string, status: number, body: unknown);
77
- }
78
- type RequestOptions = Omit<RequestInit, "body"> & {
79
- body?: unknown;
80
- };
81
- declare class RagableRequestClient {
82
- private readonly apiKey;
83
- private readonly baseUrl;
84
- private readonly fetchImpl;
85
- private readonly defaultHeaders;
86
- constructor(options: RagableClientOptions);
87
- request<T>(path: string, options?: RequestOptions): Promise<T>;
88
- private toUrl;
89
- private parseResponseBody;
90
- }
91
153
  declare class ShiftClient {
92
154
  private readonly client;
93
155
  readonly indexes: {
@@ -116,13 +178,259 @@ declare class ShiftClient {
116
178
  results: ShiftSearchResult[];
117
179
  }>;
118
180
  }
181
+
182
+ interface AgentSummary {
183
+ id: string;
184
+ name: string;
185
+ description: string | null;
186
+ active: boolean;
187
+ createdAt: string;
188
+ updatedAt: string;
189
+ }
190
+ interface AgentChatMessage {
191
+ role: "user" | "assistant";
192
+ content: string;
193
+ }
194
+ interface AgentChatParams {
195
+ message: string;
196
+ history?: AgentChatMessage[];
197
+ }
198
+ /** Mirrors backend ExecutionResult JSON shape (traces are opaque in the SDK). */
199
+ interface AgentChatResult {
200
+ response: string;
201
+ traces: unknown[];
202
+ totalDurationMs: number;
203
+ httpResponse?: unknown;
204
+ }
205
+ /**
206
+ * Loose SSE event from POST /v1/agents/:id/chat/stream — narrows with `type` at runtime.
207
+ */
208
+ type AgentStreamEvent = Record<string, unknown> & {
209
+ type: string;
210
+ };
211
+ declare class AgentsClient {
212
+ private readonly client;
213
+ constructor(client: RagableRequestClient);
214
+ list(): Promise<{
215
+ agents: AgentSummary[];
216
+ }>;
217
+ get(agentId: string): Promise<AgentSummary>;
218
+ chat(agentId: string, params: AgentChatParams): Promise<AgentChatResult>;
219
+ /**
220
+ * Stream agent execution as SSE (`data: {json}` lines). Yields parsed JSON objects.
221
+ */
222
+ chatStream(agentId: string, params: AgentChatParams): AsyncGenerator<AgentStreamEvent, void, undefined>;
223
+ }
224
+
225
+ declare function normalizeBrowserApiBase(baseUrl?: string): string;
226
+ interface RagableBrowserClientOptions {
227
+ /** Organization id (UUID) — public agent chat URLs. */
228
+ organizationId: string;
229
+ /**
230
+ * Authentication group id when using {@link RagableBrowserAuthClient} or
231
+ * {@link RagableBrowserDatabaseClient}.
232
+ */
233
+ authGroupId?: string;
234
+ /**
235
+ * Returns the end-user access JWT (from login/register/refresh).
236
+ * Required for `auth.getMe`, `auth.updateMe`, and `database.query`.
237
+ */
238
+ getAccessToken?: () => string | null | Promise<string | null>;
239
+ /**
240
+ * API base URL including `/api`, e.g. `https://api.example.com/api`.
241
+ * @default "http://localhost:8080/api"
242
+ */
243
+ baseUrl?: string;
244
+ fetch?: typeof fetch;
245
+ headers?: HeadersInit;
246
+ }
247
+ /** Successful register/login response (typed `user` via `AuthUser` generic). */
248
+ interface BrowserAuthSession<AuthUser extends Record<string, unknown> = Record<string, unknown>> {
249
+ user: AuthUser;
250
+ accessToken: string;
251
+ refreshToken: string;
252
+ expiresIn: string;
253
+ }
254
+ interface BrowserAuthTokens {
255
+ accessToken: string;
256
+ refreshToken: string;
257
+ expiresIn: string;
258
+ }
259
+ /** End-user auth (email/password) for a linked authentication group — no org API key. */
260
+ declare class RagableBrowserAuthClient<AuthUser extends Record<string, unknown> = Record<string, unknown>> {
261
+ private readonly options;
262
+ constructor(options: RagableBrowserClientOptions);
263
+ private get fetchImpl();
264
+ private toUrl;
265
+ private baseHeaders;
266
+ private authPrefix;
267
+ register(body: {
268
+ email: string;
269
+ password: string;
270
+ name?: string;
271
+ }): Promise<BrowserAuthSession<AuthUser>>;
272
+ login(body: {
273
+ email: string;
274
+ password: string;
275
+ }): Promise<BrowserAuthSession<AuthUser>>;
276
+ refresh(body: {
277
+ refreshToken: string;
278
+ }): Promise<BrowserAuthTokens>;
279
+ getMe(): Promise<{
280
+ user: AuthUser;
281
+ }>;
282
+ updateMe(body: {
283
+ name?: string | null;
284
+ password?: string;
285
+ }): Promise<{
286
+ user: AuthUser;
287
+ }>;
288
+ }
289
+ interface BrowserSqlQueryParams {
290
+ databaseInstanceId: string;
291
+ sql: string;
292
+ params?: unknown[];
293
+ /** Ignored — the API only allows safe read-only SQL for end users. */
294
+ readOnly?: true;
295
+ timeoutMs?: number;
296
+ rowLimit?: number;
297
+ }
298
+ interface BrowserSqlQueryResult<Row extends Record<string, unknown> = Record<string, unknown>> {
299
+ command: string;
300
+ rowCount: number;
301
+ truncated: boolean;
302
+ rows: Row[];
303
+ }
304
+ /**
305
+ * Run SQL against a Postgres instance linked to a website that uses the same `authGroupId`,
306
+ * authenticated as the signed-in end-user (access token).
307
+ *
308
+ * Pass `createBrowserClient<YourDatabase>()` and use `query<Tables<YourDatabase, 't'>>()`
309
+ * for Supabase-style row typing.
310
+ */
311
+ declare class RagableBrowserDatabaseClient<_Schema extends RagableDatabase = DefaultRagableDatabase> {
312
+ private readonly options;
313
+ constructor(options: RagableBrowserClientOptions);
314
+ private get fetchImpl();
315
+ private toUrl;
316
+ query<Row extends Record<string, unknown> = Record<string, unknown>>(params: BrowserSqlQueryParams): Promise<BrowserSqlQueryResult<Row>>;
317
+ private baseHeaders;
318
+ }
319
+ /**
320
+ * Browser-safe client: **no org API key**. Uses public routes and end-user JWTs
321
+ * from {@link RagableBrowserAuthClient}.
322
+ */
323
+ declare class RagableBrowserAgentsClient {
324
+ private readonly options;
325
+ constructor(options: RagableBrowserClientOptions);
326
+ private get fetchImpl();
327
+ private toUrl;
328
+ /**
329
+ * Stream agent execution as SSE (`POST /public/organizations/:orgId/agents/:agentId/chat/stream`).
330
+ */
331
+ chatStream(agentId: string, params: AgentPublicChatParams): AsyncGenerator<AgentStreamEvent, void, undefined>;
332
+ }
333
+ /** Public chat params — same shape as {@link AgentChatParams} plus optional trigger fields. */
334
+ interface AgentPublicChatParams extends AgentChatParams {
335
+ triggerSubtype?: string;
336
+ triggerNodeId?: string;
337
+ }
338
+ /**
339
+ * Browser client root. Use `createBrowserClient<Database, AuthUser>()` for Supabase-style
340
+ * typing on {@link RagableBrowserDatabaseClient.query} and auth `user` payloads.
341
+ */
342
+ declare class RagableBrowser<Database extends RagableDatabase = DefaultRagableDatabase, AuthUser extends Record<string, unknown> = Record<string, unknown>> {
343
+ readonly agents: RagableBrowserAgentsClient;
344
+ readonly auth: RagableBrowserAuthClient<AuthUser>;
345
+ readonly database: RagableBrowserDatabaseClient<Database>;
346
+ constructor(options: RagableBrowserClientOptions);
347
+ }
348
+ /**
349
+ * Browser client: **no org `ragable_*` API key**.
350
+ * - {@link RagableBrowser.agents} — public agent SSE.
351
+ * - {@link RagableBrowser.auth} / {@link RagableBrowser.database} — need `authGroupId` (+ `getAccessToken` for protected calls).
352
+ *
353
+ * Pass schema and user types like Supabase: `createBrowserClient<Database, AuthUser>({ ... })`.
354
+ * For Shift and `/v1/*`, use {@link createClient} on a server.
355
+ */
356
+ declare function createBrowserClient<Database extends RagableDatabase = DefaultRagableDatabase, AuthUser extends Record<string, unknown> = Record<string, unknown>>(options: RagableBrowserClientOptions): RagableBrowser<Database, AuthUser>;
357
+
358
+ /**
359
+ * Shared SSE parsing for `data: {json}` lines (Ragable agent streams).
360
+ */
361
+ type SseJsonEvent = Record<string, unknown> & {
362
+ type: string;
363
+ };
364
+ declare function parseSseDataLine(line: string): SseJsonEvent | null;
365
+ /**
366
+ * Read an SSE body and yield parsed `data:` JSON objects (double-newline framed).
367
+ */
368
+ declare function readSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseJsonEvent, void, undefined>;
369
+
370
+ /** Minimal client surface for {@link createRagPipeline} (implemented by {@link Ragable}). */
371
+ interface RagClientForPipeline {
372
+ shift: {
373
+ documents: {
374
+ create: (indexId: string, params: ShiftAddDocumentParams) => Promise<ShiftIngestResponse>;
375
+ upload: (indexId: string, params: ShiftUploadFileParams) => Promise<ShiftIngestResponse>;
376
+ };
377
+ search: (indexId: string, params: ShiftSearchParams) => Promise<{
378
+ results: ShiftSearchResult[];
379
+ }>;
380
+ };
381
+ }
382
+ interface FormatContextOptions {
383
+ /** Prepended before numbered passages (default: "Relevant passages:\\n") */
384
+ header?: string;
385
+ /** Joiner between passages (default: "\\n\\n") */
386
+ separator?: string;
387
+ /** Omit results with similarity score below this (default: no filter) */
388
+ minScore?: number;
389
+ /** Truncate total context string to this UTF-16 length (default: no limit) */
390
+ maxChars?: number;
391
+ /** Include similarity score in each block (default: false) */
392
+ includeScores?: boolean;
393
+ }
394
+ interface RagPipelineOptions {
395
+ indexId: string;
396
+ }
397
+ type RetrieveParams = ShiftSearchParams & {
398
+ format?: FormatContextOptions;
399
+ };
400
+ interface RagPipeline {
401
+ readonly indexId: string;
402
+ ingestText(params: ShiftAddDocumentParams): Promise<ShiftIngestResponse>;
403
+ ingestFile(params: ShiftUploadFileParams): Promise<ShiftIngestResponse>;
404
+ search(params: ShiftSearchParams): Promise<{
405
+ results: ShiftSearchResult[];
406
+ }>;
407
+ retrieve(params: RetrieveParams): Promise<{
408
+ results: ShiftSearchResult[];
409
+ context: string;
410
+ }>;
411
+ }
412
+ /**
413
+ * Turn vector search hits into a single block of text for system prompts or RAG user messages.
414
+ */
415
+ declare function formatRetrievalContext(results: ShiftSearchResult[], options?: FormatContextOptions): string;
416
+ /**
417
+ * High-level RAG helpers bound to one Shift index: ingest, search, retrieve with prompt-ready context.
418
+ */
419
+ declare function createRagPipeline(client: RagClientForPipeline, options: RagPipelineOptions): RagPipeline;
420
+
119
421
  declare class Ragable {
120
422
  readonly shift: ShiftClient;
423
+ readonly agents: AgentsClient;
121
424
  readonly infrastructure: {
122
425
  shift: ShiftClient;
123
426
  };
124
427
  constructor(options: RagableClientOptions);
125
428
  }
429
+ /**
430
+ * Server-side client with your **secret** `ragable_*` API key.
431
+ * Use from Node, Edge, or your Engine — never in browser bundles.
432
+ * For public agent streaming from the browser without a key, use {@link createBrowserClient}.
433
+ */
126
434
  declare function createClient(options: RagableClientOptions): Ragable;
127
435
 
128
- export { Ragable, type RagableClientOptions, RagableError, type ShiftAddDocumentParams, type ShiftCreateIndexParams, type ShiftEntry, type ShiftIndex, type ShiftIngestResponse, type ShiftListEntriesParams, type ShiftListEntriesResponse, type ShiftSearchParams, type ShiftSearchResult, type ShiftUpdateIndexParams, type ShiftUploadFileParams, type ShiftUploadableFile, createClient };
436
+ export { type AgentChatMessage, type AgentChatParams, type AgentChatResult, type AgentPublicChatParams, type AgentStreamEvent, type AgentSummary, AgentsClient, type BrowserAuthSession, type BrowserAuthTokens, type BrowserSqlQueryParams, type BrowserSqlQueryResult, type DefaultRagableDatabase, type FormatContextOptions, type Json, type RagClientForPipeline, type RagPipeline, type RagPipelineOptions, Ragable, RagableBrowser, RagableBrowserAgentsClient, RagableBrowserAuthClient, type RagableBrowserClientOptions, RagableBrowserDatabaseClient, type RagableClientOptions, type RagableDatabase, RagableError, RagableRequestClient, type RagableTableDefinition, type RequestOptions, type RetrieveParams, type ShiftAddDocumentParams, ShiftClient, type ShiftCreateIndexParams, type ShiftEntry, type ShiftIndex, type ShiftIngestResponse, type ShiftListEntriesParams, type ShiftListEntriesResponse, type ShiftSearchParams, type ShiftSearchResult, type ShiftUpdateIndexParams, type ShiftUploadFileParams, type ShiftUploadableFile, type SseJsonEvent, type Tables, type TablesInsert, type TablesUpdate, createBrowserClient, createClient, createRagPipeline, extractErrorMessage, formatRetrievalContext, normalizeBrowserApiBase, parseSseDataLine, readSseStream };