@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.js CHANGED
@@ -22,11 +22,27 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
22
22
  // src/index.ts
23
23
  var index_exports = {};
24
24
  __export(index_exports, {
25
+ AgentsClient: () => AgentsClient,
25
26
  Ragable: () => Ragable,
27
+ RagableBrowser: () => RagableBrowser,
28
+ RagableBrowserAgentsClient: () => RagableBrowserAgentsClient,
29
+ RagableBrowserAuthClient: () => RagableBrowserAuthClient,
30
+ RagableBrowserDatabaseClient: () => RagableBrowserDatabaseClient,
26
31
  RagableError: () => RagableError,
27
- createClient: () => createClient
32
+ RagableRequestClient: () => RagableRequestClient,
33
+ ShiftClient: () => ShiftClient,
34
+ createBrowserClient: () => createBrowserClient,
35
+ createClient: () => createClient,
36
+ createRagPipeline: () => createRagPipeline,
37
+ extractErrorMessage: () => extractErrorMessage,
38
+ formatRetrievalContext: () => formatRetrievalContext,
39
+ normalizeBrowserApiBase: () => normalizeBrowserApiBase,
40
+ parseSseDataLine: () => parseSseDataLine,
41
+ readSseStream: () => readSseStream
28
42
  });
29
43
  module.exports = __toCommonJS(index_exports);
44
+
45
+ // src/request-client.ts
30
46
  var RagableError = class extends Error {
31
47
  constructor(message, status, body) {
32
48
  super(message);
@@ -37,6 +53,20 @@ var RagableError = class extends Error {
37
53
  this.body = body;
38
54
  }
39
55
  };
56
+ function extractErrorMessage(payload, fallback) {
57
+ if (payload && typeof payload === "object") {
58
+ if ("error" in payload && typeof payload.error === "string") {
59
+ return payload.error;
60
+ }
61
+ if ("message" in payload && typeof payload.message === "string") {
62
+ return payload.message;
63
+ }
64
+ }
65
+ if (typeof payload === "string" && payload.length > 0) {
66
+ return payload;
67
+ }
68
+ return fallback || "Request failed";
69
+ }
40
70
  var RagableRequestClient = class {
41
71
  constructor(options) {
42
72
  __publicField(this, "apiKey");
@@ -48,7 +78,24 @@ var RagableRequestClient = class {
48
78
  this.fetchImpl = options.fetch ?? fetch;
49
79
  this.defaultHeaders = options.headers;
50
80
  }
81
+ toUrl(path) {
82
+ const normalizedBase = this.baseUrl.replace(/\/+$/, "");
83
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
84
+ return `${normalizedBase}${normalizedPath}`;
85
+ }
51
86
  async request(path, options = {}) {
87
+ const response = await this.rawFetch(path, options);
88
+ const payload = await this.parseResponseBody(response);
89
+ if (!response.ok) {
90
+ const message = extractErrorMessage(payload, response.statusText);
91
+ throw new RagableError(message, response.status, payload);
92
+ }
93
+ return payload;
94
+ }
95
+ /**
96
+ * Low-level fetch with API key and JSON body encoding. Caller handles status and body.
97
+ */
98
+ async rawFetch(path, options = {}) {
52
99
  const headers = new Headers(this.defaultHeaders);
53
100
  headers.set("Authorization", `Bearer ${this.apiKey}`);
54
101
  let body = options.body;
@@ -56,22 +103,11 @@ var RagableRequestClient = class {
56
103
  headers.set("Content-Type", "application/json");
57
104
  body = JSON.stringify(body);
58
105
  }
59
- const response = await this.fetchImpl(this.toUrl(path), {
106
+ return this.fetchImpl(this.toUrl(path), {
60
107
  ...options,
61
108
  headers,
62
109
  body
63
110
  });
64
- const payload = await this.parseResponseBody(response);
65
- if (!response.ok) {
66
- const message = extractErrorMessage(payload, response.statusText);
67
- throw new RagableError(message, response.status, payload);
68
- }
69
- return payload;
70
- }
71
- toUrl(path) {
72
- const normalizedBase = this.baseUrl.replace(/\/+$/, "");
73
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
74
- return `${normalizedBase}${normalizedPath}`;
75
111
  }
76
112
  async parseResponseBody(response) {
77
113
  if (response.status === 204) {
@@ -84,6 +120,11 @@ var RagableRequestClient = class {
84
120
  return response.text();
85
121
  }
86
122
  };
123
+ function isBodyInit(value) {
124
+ return typeof value === "string" || value instanceof Blob || value instanceof FormData || value instanceof URLSearchParams || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
125
+ }
126
+
127
+ // src/shift.ts
87
128
  var ShiftClient = class {
88
129
  constructor(client) {
89
130
  this.client = client;
@@ -186,34 +227,6 @@ var ShiftClient = class {
186
227
  );
187
228
  }
188
229
  };
189
- var Ragable = class {
190
- constructor(options) {
191
- __publicField(this, "shift");
192
- __publicField(this, "infrastructure");
193
- const client = new RagableRequestClient(options);
194
- this.shift = new ShiftClient(client);
195
- this.infrastructure = {
196
- shift: this.shift
197
- };
198
- }
199
- };
200
- function createClient(options) {
201
- return new Ragable(options);
202
- }
203
- function extractErrorMessage(payload, fallback) {
204
- if (payload && typeof payload === "object") {
205
- if ("error" in payload && typeof payload.error === "string") {
206
- return payload.error;
207
- }
208
- if ("message" in payload && typeof payload.message === "string") {
209
- return payload.message;
210
- }
211
- }
212
- if (typeof payload === "string" && payload.length > 0) {
213
- return payload;
214
- }
215
- return fallback || "Request failed";
216
- }
217
230
  function resolveUploadFileName(params) {
218
231
  if (params.fileName) {
219
232
  return params.fileName;
@@ -244,18 +257,398 @@ function normalizeUploadFile(file, contentType) {
244
257
  function isNamedBlob(value) {
245
258
  return value instanceof Blob && "name" in value && typeof value.name === "string";
246
259
  }
247
- function isBodyInit(value) {
248
- return typeof value === "string" || value instanceof Blob || value instanceof FormData || value instanceof URLSearchParams || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
249
- }
250
260
  function toArrayBuffer(value) {
251
261
  const copy = new Uint8Array(value.byteLength);
252
262
  copy.set(value);
253
263
  return copy.buffer;
254
264
  }
265
+
266
+ // src/sse.ts
267
+ async function parseMaybeJsonBody(response) {
268
+ const contentType = response.headers.get("content-type") ?? "";
269
+ if (contentType.includes("application/json")) {
270
+ try {
271
+ return await response.json();
272
+ } catch {
273
+ return null;
274
+ }
275
+ }
276
+ try {
277
+ return await response.text();
278
+ } catch {
279
+ return null;
280
+ }
281
+ }
282
+ function parseSseDataLine(line) {
283
+ const dataPrefix = "data: ";
284
+ if (!line.startsWith(dataPrefix)) {
285
+ return null;
286
+ }
287
+ const json = line.slice(dataPrefix.length).trim();
288
+ if (json.length === 0 || json === "[DONE]") {
289
+ return null;
290
+ }
291
+ try {
292
+ return JSON.parse(json);
293
+ } catch {
294
+ return null;
295
+ }
296
+ }
297
+ async function* readSseStream(body) {
298
+ const reader = body.getReader();
299
+ const decoder = new TextDecoder();
300
+ let buffer = "";
301
+ try {
302
+ while (true) {
303
+ const { done, value } = await reader.read();
304
+ if (done) {
305
+ break;
306
+ }
307
+ buffer += decoder.decode(value, { stream: true });
308
+ let boundary = buffer.indexOf("\n\n");
309
+ while (boundary !== -1) {
310
+ const block = buffer.slice(0, boundary);
311
+ buffer = buffer.slice(boundary + 2);
312
+ for (const line of block.split("\n")) {
313
+ const evt = parseSseDataLine(line);
314
+ if (evt) {
315
+ yield evt;
316
+ }
317
+ }
318
+ boundary = buffer.indexOf("\n\n");
319
+ }
320
+ }
321
+ if (buffer.trim().length > 0) {
322
+ for (const line of buffer.split("\n")) {
323
+ const evt = parseSseDataLine(line);
324
+ if (evt) {
325
+ yield evt;
326
+ }
327
+ }
328
+ }
329
+ } finally {
330
+ reader.releaseLock();
331
+ }
332
+ }
333
+
334
+ // src/agents.ts
335
+ var AgentsClient = class {
336
+ constructor(client) {
337
+ this.client = client;
338
+ }
339
+ async list() {
340
+ return this.client.request("/v1/agents");
341
+ }
342
+ async get(agentId) {
343
+ return this.client.request(`/v1/agents/${agentId}`);
344
+ }
345
+ async chat(agentId, params) {
346
+ return this.client.request(`/v1/agents/${agentId}/chat`, {
347
+ method: "POST",
348
+ body: {
349
+ message: params.message,
350
+ ...params.history !== void 0 ? { history: params.history } : {}
351
+ }
352
+ });
353
+ }
354
+ /**
355
+ * Stream agent execution as SSE (`data: {json}` lines). Yields parsed JSON objects.
356
+ */
357
+ async *chatStream(agentId, params) {
358
+ const response = await this.client.rawFetch(
359
+ `/v1/agents/${agentId}/chat/stream`,
360
+ {
361
+ method: "POST",
362
+ body: {
363
+ message: params.message,
364
+ ...params.history !== void 0 ? { history: params.history } : {}
365
+ }
366
+ }
367
+ );
368
+ if (!response.ok) {
369
+ const payload = await parseMaybeJsonBody(response);
370
+ const message = extractErrorMessage(payload, response.statusText);
371
+ throw new RagableError(message, response.status, payload);
372
+ }
373
+ const body = response.body;
374
+ if (!body) {
375
+ return;
376
+ }
377
+ yield* readSseStream(body);
378
+ }
379
+ };
380
+
381
+ // src/browser.ts
382
+ function normalizeBrowserApiBase(baseUrl) {
383
+ return (baseUrl ?? "http://localhost:8080/api").replace(/\/+$/, "");
384
+ }
385
+ function requireAuthGroupId(options) {
386
+ const id = options.authGroupId?.trim();
387
+ if (!id) {
388
+ throw new Error(
389
+ "authGroupId is required for auth and database methods on the browser client"
390
+ );
391
+ }
392
+ return id;
393
+ }
394
+ async function requireAccessToken(options) {
395
+ const getter = options.getAccessToken;
396
+ if (!getter) {
397
+ throw new Error(
398
+ "getAccessToken is required for this call (return the end-user access JWT)"
399
+ );
400
+ }
401
+ const token = await getter();
402
+ if (!token?.trim()) {
403
+ throw new Error("getAccessToken returned no token");
404
+ }
405
+ return token.trim();
406
+ }
407
+ async function parseJsonOrThrow(response) {
408
+ const payload = await parseMaybeJsonBody(response);
409
+ if (!response.ok) {
410
+ const message = extractErrorMessage(payload, response.statusText);
411
+ throw new RagableError(message, response.status, payload);
412
+ }
413
+ return payload;
414
+ }
415
+ var RagableBrowserAuthClient = class {
416
+ constructor(options) {
417
+ this.options = options;
418
+ }
419
+ get fetchImpl() {
420
+ return this.options.fetch ?? fetch;
421
+ }
422
+ toUrl(path) {
423
+ return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
424
+ }
425
+ baseHeaders(json) {
426
+ const h = new Headers(this.options.headers);
427
+ if (json) {
428
+ h.set("Content-Type", "application/json");
429
+ }
430
+ return h;
431
+ }
432
+ authPrefix() {
433
+ const gid = requireAuthGroupId(this.options);
434
+ return `/auth-groups/${gid}/auth`;
435
+ }
436
+ async register(body) {
437
+ const response = await this.fetchImpl(`${this.toUrl(this.authPrefix())}/register`, {
438
+ method: "POST",
439
+ headers: this.baseHeaders(true),
440
+ body: JSON.stringify(body)
441
+ });
442
+ return parseJsonOrThrow(response);
443
+ }
444
+ async login(body) {
445
+ const response = await this.fetchImpl(`${this.toUrl(this.authPrefix())}/login`, {
446
+ method: "POST",
447
+ headers: this.baseHeaders(true),
448
+ body: JSON.stringify(body)
449
+ });
450
+ return parseJsonOrThrow(response);
451
+ }
452
+ async refresh(body) {
453
+ const response = await this.fetchImpl(`${this.toUrl(this.authPrefix())}/refresh`, {
454
+ method: "POST",
455
+ headers: this.baseHeaders(true),
456
+ body: JSON.stringify(body)
457
+ });
458
+ return parseJsonOrThrow(response);
459
+ }
460
+ async getMe() {
461
+ const token = await requireAccessToken(this.options);
462
+ const headers = this.baseHeaders(false);
463
+ headers.set("Authorization", `Bearer ${token}`);
464
+ const response = await this.fetchImpl(`${this.toUrl(this.authPrefix())}/me`, {
465
+ method: "GET",
466
+ headers
467
+ });
468
+ return parseJsonOrThrow(response);
469
+ }
470
+ async updateMe(body) {
471
+ const token = await requireAccessToken(this.options);
472
+ const headers = this.baseHeaders(true);
473
+ headers.set("Authorization", `Bearer ${token}`);
474
+ const response = await this.fetchImpl(`${this.toUrl(this.authPrefix())}/me`, {
475
+ method: "PATCH",
476
+ headers,
477
+ body: JSON.stringify(body)
478
+ });
479
+ return parseJsonOrThrow(response);
480
+ }
481
+ };
482
+ var RagableBrowserDatabaseClient = class {
483
+ constructor(options) {
484
+ this.options = options;
485
+ }
486
+ get fetchImpl() {
487
+ return this.options.fetch ?? fetch;
488
+ }
489
+ toUrl(path) {
490
+ return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
491
+ }
492
+ async query(params) {
493
+ const gid = requireAuthGroupId(this.options);
494
+ const token = await requireAccessToken(this.options);
495
+ const headers = this.baseHeaders();
496
+ headers.set("Authorization", `Bearer ${token}`);
497
+ headers.set("Content-Type", "application/json");
498
+ const response = await this.fetchImpl(
499
+ this.toUrl(`/auth-groups/${gid}/data/query`),
500
+ {
501
+ method: "POST",
502
+ headers,
503
+ body: JSON.stringify({
504
+ databaseInstanceId: params.databaseInstanceId,
505
+ sql: params.sql,
506
+ ...params.params !== void 0 ? { params: params.params } : {},
507
+ ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
508
+ ...params.rowLimit !== void 0 ? { rowLimit: params.rowLimit } : {}
509
+ })
510
+ }
511
+ );
512
+ return parseJsonOrThrow(response);
513
+ }
514
+ baseHeaders() {
515
+ return new Headers(this.options.headers);
516
+ }
517
+ };
518
+ var RagableBrowserAgentsClient = class {
519
+ constructor(options) {
520
+ this.options = options;
521
+ }
522
+ get fetchImpl() {
523
+ return this.options.fetch ?? fetch;
524
+ }
525
+ toUrl(path) {
526
+ return `${normalizeBrowserApiBase(this.options.baseUrl)}${path.startsWith("/") ? path : `/${path}`}`;
527
+ }
528
+ /**
529
+ * Stream agent execution as SSE (`POST /public/organizations/:orgId/agents/:agentId/chat/stream`).
530
+ */
531
+ async *chatStream(agentId, params) {
532
+ const orgId = this.options.organizationId;
533
+ const body = {
534
+ message: params.message,
535
+ ...params.history !== void 0 ? { history: params.history } : {},
536
+ ...params.triggerSubtype !== void 0 ? { triggerSubtype: params.triggerSubtype } : {},
537
+ ...params.triggerNodeId !== void 0 ? { triggerNodeId: params.triggerNodeId } : {}
538
+ };
539
+ const headers = new Headers(this.options.headers);
540
+ headers.set("Content-Type", "application/json");
541
+ const response = await this.fetchImpl(
542
+ this.toUrl(`/public/organizations/${orgId}/agents/${agentId}/chat/stream`),
543
+ {
544
+ method: "POST",
545
+ headers,
546
+ body: JSON.stringify(body)
547
+ }
548
+ );
549
+ if (!response.ok) {
550
+ const payload = await parseMaybeJsonBody(response);
551
+ const message = extractErrorMessage(payload, response.statusText);
552
+ throw new RagableError(message, response.status, payload);
553
+ }
554
+ const streamBody = response.body;
555
+ if (!streamBody) {
556
+ return;
557
+ }
558
+ yield* readSseStream(streamBody);
559
+ }
560
+ };
561
+ var RagableBrowser = class {
562
+ constructor(options) {
563
+ __publicField(this, "agents");
564
+ __publicField(this, "auth");
565
+ __publicField(this, "database");
566
+ this.agents = new RagableBrowserAgentsClient(options);
567
+ this.auth = new RagableBrowserAuthClient(options);
568
+ this.database = new RagableBrowserDatabaseClient(options);
569
+ }
570
+ };
571
+ function createBrowserClient(options) {
572
+ return new RagableBrowser(options);
573
+ }
574
+
575
+ // src/rag.ts
576
+ function formatRetrievalContext(results, options = {}) {
577
+ const header = options.header ?? "Relevant passages:\n";
578
+ const separator = options.separator ?? "\n\n";
579
+ const minScore = options.minScore;
580
+ let filtered = results;
581
+ if (typeof minScore === "number") {
582
+ filtered = results.filter((r) => r.score >= minScore);
583
+ }
584
+ const blocks = filtered.map((r, i) => {
585
+ const scoreSuffix = options.includeScores === true ? ` (score: ${r.score.toFixed(4)})` : "";
586
+ return `[${i + 1}]${scoreSuffix}
587
+ ${r.content.trim()}`;
588
+ });
589
+ let text = (blocks.length > 0 ? header : "") + blocks.join(separator);
590
+ if (typeof options.maxChars === "number" && text.length > options.maxChars) {
591
+ text = text.slice(0, options.maxChars).trimEnd() + "\n\u2026";
592
+ }
593
+ return text;
594
+ }
595
+ function createRagPipeline(client, options) {
596
+ const { indexId } = options;
597
+ return {
598
+ indexId,
599
+ ingestText(params) {
600
+ return client.shift.documents.create(indexId, params);
601
+ },
602
+ ingestFile(params) {
603
+ return client.shift.documents.upload(indexId, params);
604
+ },
605
+ search(params) {
606
+ return client.shift.search(indexId, params);
607
+ },
608
+ async retrieve(params) {
609
+ const { format: formatOpts, ...searchParams } = params;
610
+ const { results } = await client.shift.search(indexId, searchParams);
611
+ const context = formatRetrievalContext(results, formatOpts ?? {});
612
+ return { results, context };
613
+ }
614
+ };
615
+ }
616
+
617
+ // src/index.ts
618
+ var Ragable = class {
619
+ constructor(options) {
620
+ __publicField(this, "shift");
621
+ __publicField(this, "agents");
622
+ __publicField(this, "infrastructure");
623
+ const client = new RagableRequestClient(options);
624
+ this.shift = new ShiftClient(client);
625
+ this.agents = new AgentsClient(client);
626
+ this.infrastructure = {
627
+ shift: this.shift
628
+ };
629
+ }
630
+ };
631
+ function createClient(options) {
632
+ return new Ragable(options);
633
+ }
255
634
  // Annotate the CommonJS export names for ESM import in node:
256
635
  0 && (module.exports = {
636
+ AgentsClient,
257
637
  Ragable,
638
+ RagableBrowser,
639
+ RagableBrowserAgentsClient,
640
+ RagableBrowserAuthClient,
641
+ RagableBrowserDatabaseClient,
258
642
  RagableError,
259
- createClient
643
+ RagableRequestClient,
644
+ ShiftClient,
645
+ createBrowserClient,
646
+ createClient,
647
+ createRagPipeline,
648
+ extractErrorMessage,
649
+ formatRetrievalContext,
650
+ normalizeBrowserApiBase,
651
+ parseSseDataLine,
652
+ readSseStream
260
653
  });
261
654
  //# sourceMappingURL=index.js.map