@ragable/sdk 0.7.6 → 0.7.7

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.mjs CHANGED
@@ -131,201 +131,6 @@ function extractErrorMessage(payload, fallback) {
131
131
  }
132
132
  return fallback || "Request failed";
133
133
  }
134
- var RagableRequestClient = class {
135
- constructor(options) {
136
- __publicField(this, "apiKey");
137
- __publicField(this, "baseUrl");
138
- __publicField(this, "fetchImpl");
139
- __publicField(this, "defaultHeaders");
140
- this.apiKey = options.apiKey;
141
- this.baseUrl = DEFAULT_RAGABLE_API_BASE.replace(/\/+$/, "");
142
- this.fetchImpl = bindFetch(options.fetch);
143
- this.defaultHeaders = options.headers;
144
- }
145
- toUrl(path) {
146
- const normalizedBase = this.baseUrl.replace(/\/+$/, "");
147
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
148
- return `${normalizedBase}${normalizedPath}`;
149
- }
150
- async request(path, options = {}) {
151
- const response = await this.rawFetch(path, options);
152
- const payload = await this.parseResponseBody(response);
153
- if (!response.ok) {
154
- const message = extractErrorMessage(payload, response.statusText);
155
- throw new RagableError(message, response.status, payload);
156
- }
157
- return payload;
158
- }
159
- /**
160
- * Low-level fetch with API key and JSON body encoding. Caller handles status and body.
161
- */
162
- async rawFetch(path, options = {}) {
163
- const headers = new Headers(this.defaultHeaders);
164
- headers.set("Authorization", `Bearer ${this.apiKey}`);
165
- let body = options.body;
166
- if (body !== void 0 && !isBodyInit(body)) {
167
- headers.set("Content-Type", "application/json");
168
- body = JSON.stringify(body);
169
- }
170
- return this.fetchImpl(this.toUrl(path), {
171
- ...options,
172
- headers,
173
- body
174
- });
175
- }
176
- async parseResponseBody(response) {
177
- if (response.status === 204) {
178
- return null;
179
- }
180
- const contentType = response.headers.get("content-type") ?? "";
181
- if (contentType.includes("application/json")) {
182
- return response.json();
183
- }
184
- return response.text();
185
- }
186
- };
187
- function isBodyInit(value) {
188
- return typeof value === "string" || value instanceof Blob || value instanceof FormData || value instanceof URLSearchParams || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
189
- }
190
-
191
- // src/shift.ts
192
- var ShiftClient = class {
193
- constructor(client) {
194
- this.client = client;
195
- __publicField(this, "indexes");
196
- __publicField(this, "documents");
197
- __publicField(this, "entries");
198
- this.indexes = {
199
- list: async () => {
200
- return this.client.request("/v1/shift/indexes");
201
- },
202
- create: async (params) => {
203
- return this.client.request("/v1/shift/indexes", {
204
- method: "POST",
205
- body: params
206
- });
207
- },
208
- get: async (indexId) => {
209
- return this.client.request(`/v1/shift/indexes/${indexId}`);
210
- },
211
- update: async (indexId, params) => {
212
- return this.client.request(`/v1/shift/indexes/${indexId}`, {
213
- method: "PUT",
214
- body: params
215
- });
216
- },
217
- delete: async (indexId) => {
218
- return this.client.request(
219
- `/v1/shift/indexes/${indexId}`,
220
- {
221
- method: "DELETE"
222
- }
223
- );
224
- }
225
- };
226
- this.documents = {
227
- create: async (indexId, params) => {
228
- return this.client.request(
229
- `/v1/shift/indexes/${indexId}/documents`,
230
- {
231
- method: "POST",
232
- body: params
233
- }
234
- );
235
- },
236
- upload: async (indexId, params) => {
237
- const formData = new FormData();
238
- const fileName = resolveUploadFileName(params);
239
- const contentType = resolveUploadContentType(params);
240
- const file = normalizeUploadFile(params.file, contentType);
241
- formData.set("file", file, fileName);
242
- if (params.metadata) {
243
- formData.set("metadata", JSON.stringify(params.metadata));
244
- }
245
- if (typeof params.chunkSize === "number") {
246
- formData.set("chunkSize", String(params.chunkSize));
247
- }
248
- if (typeof params.chunkOverlap === "number") {
249
- formData.set("chunkOverlap", String(params.chunkOverlap));
250
- }
251
- return this.client.request(
252
- `/v1/shift/indexes/${indexId}/files`,
253
- {
254
- method: "POST",
255
- body: formData
256
- }
257
- );
258
- }
259
- };
260
- this.entries = {
261
- list: async (indexId, params = {}) => {
262
- const search = new URLSearchParams();
263
- if (typeof params.take === "number") {
264
- search.set("take", String(params.take));
265
- }
266
- if (typeof params.skip === "number") {
267
- search.set("skip", String(params.skip));
268
- }
269
- const suffix = search.size > 0 ? `?${search.toString()}` : "";
270
- return this.client.request(
271
- `/v1/shift/indexes/${indexId}/entries${suffix}`
272
- );
273
- },
274
- delete: async (indexId, entryId) => {
275
- return this.client.request(
276
- `/v1/shift/indexes/${indexId}/entries/${entryId}`,
277
- {
278
- method: "DELETE"
279
- }
280
- );
281
- }
282
- };
283
- }
284
- async search(indexId, params) {
285
- return this.client.request(
286
- `/v1/shift/indexes/${indexId}/search`,
287
- {
288
- method: "POST",
289
- body: params
290
- }
291
- );
292
- }
293
- };
294
- function resolveUploadFileName(params) {
295
- if (params.fileName) {
296
- return params.fileName;
297
- }
298
- if (isNamedBlob(params.file)) {
299
- return params.file.name;
300
- }
301
- return "upload.bin";
302
- }
303
- function resolveUploadContentType(params) {
304
- if (params.contentType) {
305
- return params.contentType;
306
- }
307
- if (params.file instanceof Blob && params.file.type) {
308
- return params.file.type;
309
- }
310
- return "application/octet-stream";
311
- }
312
- function normalizeUploadFile(file, contentType) {
313
- if (file instanceof Blob) {
314
- return file;
315
- }
316
- if (file instanceof Uint8Array) {
317
- return new Blob([toArrayBuffer(file)], { type: contentType });
318
- }
319
- return new Blob([file], { type: contentType });
320
- }
321
- function isNamedBlob(value) {
322
- return value instanceof Blob && "name" in value && typeof value.name === "string";
323
- }
324
- function toArrayBuffer(value) {
325
- const copy = new Uint8Array(value.byteLength);
326
- copy.set(value);
327
- return copy.buffer;
328
- }
329
134
 
330
135
  // src/agent-stream.ts
331
136
  function assertAborted(signal) {
@@ -831,72 +636,6 @@ async function* readSseStream(body) {
831
636
  }
832
637
  }
833
638
 
834
- // src/agents.ts
835
- var AgentsClient = class {
836
- constructor(client) {
837
- this.client = client;
838
- }
839
- async list() {
840
- return this.client.request("/v1/agents");
841
- }
842
- async get(agentId) {
843
- return this.client.request(`/v1/agents/${agentId}`);
844
- }
845
- async chat(agentId, params) {
846
- return this.client.request(`/v1/agents/${agentId}/chat`, {
847
- method: "POST",
848
- body: {
849
- message: params.message,
850
- ...params.history !== void 0 ? { history: params.history } : {}
851
- }
852
- });
853
- }
854
- /**
855
- * Stream agent execution as SSE (`data: {json}` lines). Yields parsed JSON objects.
856
- */
857
- async *chatStream(agentId, params) {
858
- const response = await this.client.rawFetch(
859
- `/v1/agents/${agentId}/chat/stream`,
860
- {
861
- method: "POST",
862
- body: {
863
- message: params.message,
864
- ...params.history !== void 0 ? { history: params.history } : {}
865
- },
866
- ...params.signal !== void 0 ? { signal: params.signal } : {}
867
- }
868
- );
869
- if (!response.ok) {
870
- const payload = await parseMaybeJsonBody(response);
871
- const message = extractErrorMessage(payload, response.statusText);
872
- throw new RagableError(message, response.status, payload);
873
- }
874
- const body = response.body;
875
- if (!body) {
876
- return;
877
- }
878
- yield* readSseStream(body);
879
- }
880
- /**
881
- * Stream an agent turn with callbacks; returns the final `done` payload plus streamed text.
882
- * Prefer this over manual iteration when building chat UIs against the server API key client.
883
- */
884
- async runChatStream(agentId, params, handlers = {}) {
885
- return runAgentChatStream(this.chatStream(agentId, params), handlers, {
886
- signal: params.signal
887
- });
888
- }
889
- /**
890
- * Stream with dashboard-style `AgentChat` ergonomics: {@link AgentChatStreamUiHandlers.onSegments}
891
- * / `onStreamingText` for live UI; returns a persisted-shaped assistant message on `done`.
892
- */
893
- async runChatUi(agentId, params, handlers = {}) {
894
- return runAgentChatStreamForUi(this.chatStream(agentId, params), handlers, {
895
- signal: params.signal
896
- });
897
- }
898
- };
899
-
900
639
  // src/transport.ts
901
640
  var DEFAULT_RETRY = {
902
641
  maxRetries: 3,
@@ -2125,8 +1864,8 @@ function parseExpiresInSeconds(raw) {
2125
1864
  const mult = u === "s" ? 1 : u === "m" ? 60 : u === "h" ? 3600 : u === "d" ? 86400 : 1;
2126
1865
  return n * mult;
2127
1866
  }
2128
- const asNum = Number(s);
2129
- return Number.isFinite(asNum) ? asNum : 0;
1867
+ const asNum2 = Number(s);
1868
+ return Number.isFinite(asNum2) ? asNum2 : 0;
2130
1869
  }
2131
1870
  async function parseJsonOrThrow(response) {
2132
1871
  const text = await response.text();
@@ -2530,6 +2269,480 @@ function decodeJwtExpiry(jwt) {
2530
2269
  }
2531
2270
  }
2532
2271
 
2272
+ // src/stream-parts.ts
2273
+ function normalizeFinishReason(raw) {
2274
+ switch (raw) {
2275
+ case "stop":
2276
+ case "length":
2277
+ case "content-filter":
2278
+ case "error":
2279
+ return raw;
2280
+ case "tool_calls":
2281
+ return "tool-calls";
2282
+ case null:
2283
+ case void 0:
2284
+ case "":
2285
+ return "unknown";
2286
+ default:
2287
+ return "unknown";
2288
+ }
2289
+ }
2290
+ function chunkUsage(chunk) {
2291
+ const u = chunk.usage ?? {};
2292
+ const cached = u.prompt_tokens_details?.cached_tokens ?? u.cache_read_input_tokens;
2293
+ return {
2294
+ promptTokens: typeof u.prompt_tokens === "number" ? u.prompt_tokens : 0,
2295
+ completionTokens: typeof u.completion_tokens === "number" ? u.completion_tokens : 0,
2296
+ totalTokens: typeof u.total_tokens === "number" ? u.total_tokens : 0,
2297
+ ...typeof cached === "number" ? { cachedPromptTokens: cached } : {}
2298
+ };
2299
+ }
2300
+ function createToolCallAccumulator() {
2301
+ return { byIndex: /* @__PURE__ */ new Map() };
2302
+ }
2303
+ function parseJsonOrRaw(raw) {
2304
+ try {
2305
+ return JSON.parse(raw);
2306
+ } catch {
2307
+ return raw;
2308
+ }
2309
+ }
2310
+ function mapFireworksChunk(chunk, acc) {
2311
+ const out = [];
2312
+ if (chunk.error?.message) {
2313
+ out.push({ type: "error", error: chunk.error.message });
2314
+ return out;
2315
+ }
2316
+ const choice = chunk.choices?.[0];
2317
+ const delta = choice?.delta;
2318
+ if (delta?.content) {
2319
+ out.push({ type: "text-delta", textDelta: delta.content });
2320
+ }
2321
+ const reasoning = delta?.reasoning_content ?? delta?.reasoning;
2322
+ if (typeof reasoning === "string" && reasoning.length > 0) {
2323
+ out.push({ type: "reasoning", textDelta: reasoning });
2324
+ }
2325
+ if (Array.isArray(delta?.tool_calls)) {
2326
+ for (const tc of delta.tool_calls) {
2327
+ const idx = typeof tc.index === "number" ? tc.index : 0;
2328
+ let entry = acc.byIndex.get(idx);
2329
+ if (!entry) {
2330
+ entry = { id: "", name: "", argsBuffer: "", emitted: false };
2331
+ acc.byIndex.set(idx, entry);
2332
+ }
2333
+ if (tc.id) entry.id = tc.id;
2334
+ if (tc.function?.name) entry.name = tc.function.name;
2335
+ if (typeof tc.function?.arguments === "string") {
2336
+ entry.argsBuffer += tc.function.arguments;
2337
+ }
2338
+ }
2339
+ }
2340
+ if (choice?.finish_reason) {
2341
+ for (const entry of acc.byIndex.values()) {
2342
+ if (entry.emitted || !entry.name) continue;
2343
+ entry.emitted = true;
2344
+ out.push({
2345
+ type: "tool-call",
2346
+ toolCallId: entry.id || `call_${entry.name}_${Date.now()}`,
2347
+ toolName: entry.name,
2348
+ args: entry.argsBuffer ? parseJsonOrRaw(entry.argsBuffer) : {}
2349
+ });
2350
+ }
2351
+ out.push({
2352
+ type: "finish",
2353
+ finishReason: normalizeFinishReason(choice.finish_reason),
2354
+ usage: chunkUsage(chunk)
2355
+ });
2356
+ } else if (chunk.usage) {
2357
+ out.push({
2358
+ type: "finish",
2359
+ finishReason: "unknown",
2360
+ usage: chunkUsage(chunk)
2361
+ });
2362
+ }
2363
+ return out;
2364
+ }
2365
+ function asStr(v, fallback = "") {
2366
+ return typeof v === "string" ? v : fallback;
2367
+ }
2368
+ function asNum(v, fallback = 0) {
2369
+ return typeof v === "number" && Number.isFinite(v) ? v : fallback;
2370
+ }
2371
+ function mapAgentEvent(event) {
2372
+ switch (event.type) {
2373
+ case "token":
2374
+ return { type: "text-delta", textDelta: asStr(event.token) };
2375
+ case "reasoning_token":
2376
+ return { type: "reasoning", textDelta: asStr(event.token) };
2377
+ case "tool:call":
2378
+ return {
2379
+ type: "tool-call",
2380
+ toolCallId: asStr(event.nodeId),
2381
+ toolName: asStr(event.toolName),
2382
+ args: event.args
2383
+ };
2384
+ case "tool:result":
2385
+ return {
2386
+ type: "tool-result",
2387
+ toolCallId: asStr(event.nodeId),
2388
+ toolName: asStr(event.toolName),
2389
+ result: event.result,
2390
+ ...typeof event.durationMs === "number" ? { durationMs: event.durationMs } : {}
2391
+ };
2392
+ case "done": {
2393
+ const usage = {
2394
+ promptTokens: asNum(event.inputTokens),
2395
+ completionTokens: asNum(event.outputTokens),
2396
+ totalTokens: asNum(event.inputTokens) + asNum(event.outputTokens),
2397
+ ...typeof event.cachedPromptTokens === "number" ? { cachedPromptTokens: event.cachedPromptTokens } : {}
2398
+ };
2399
+ return {
2400
+ type: "finish",
2401
+ finishReason: normalizeFinishReason(
2402
+ asStr(event.finishReason) || asStr(event.stopReason) || null
2403
+ ),
2404
+ usage
2405
+ };
2406
+ }
2407
+ default:
2408
+ return null;
2409
+ }
2410
+ }
2411
+
2412
+ // src/ai.ts
2413
+ var PartBroadcast = class {
2414
+ constructor() {
2415
+ __publicField(this, "state", {
2416
+ parts: [],
2417
+ resolved: false,
2418
+ error: null,
2419
+ waiters: []
2420
+ });
2421
+ }
2422
+ push(part) {
2423
+ this.state.parts.push(part);
2424
+ this.notify();
2425
+ }
2426
+ end() {
2427
+ if (this.state.resolved) return;
2428
+ this.state.resolved = true;
2429
+ this.notify();
2430
+ }
2431
+ fail(error) {
2432
+ if (this.state.resolved) return;
2433
+ this.state.error = error;
2434
+ this.state.resolved = true;
2435
+ this.notify();
2436
+ }
2437
+ notify() {
2438
+ const waiters = this.state.waiters;
2439
+ this.state.waiters = [];
2440
+ for (const w of waiters) w();
2441
+ }
2442
+ consume() {
2443
+ const state = this.state;
2444
+ return {
2445
+ [Symbol.asyncIterator]: () => {
2446
+ let idx = 0;
2447
+ return {
2448
+ next: async () => {
2449
+ while (true) {
2450
+ if (idx < state.parts.length) {
2451
+ return { value: state.parts[idx++], done: false };
2452
+ }
2453
+ if (state.resolved) {
2454
+ if (state.error) throw state.error;
2455
+ return { value: void 0, done: true };
2456
+ }
2457
+ await new Promise((resolve) => {
2458
+ state.waiters.push(resolve);
2459
+ });
2460
+ }
2461
+ }
2462
+ };
2463
+ }
2464
+ };
2465
+ }
2466
+ };
2467
+ function defer() {
2468
+ let resolve;
2469
+ let reject;
2470
+ const promise = new Promise((res, rej) => {
2471
+ resolve = res;
2472
+ reject = rej;
2473
+ });
2474
+ return { promise, resolve, reject };
2475
+ }
2476
+ var ZERO_USAGE = {
2477
+ promptTokens: 0,
2478
+ completionTokens: 0,
2479
+ totalTokens: 0
2480
+ };
2481
+ function buildInferenceRequestBody(params) {
2482
+ const body = {
2483
+ model: params.model,
2484
+ messages: params.messages
2485
+ };
2486
+ if (params.system !== void 0) body.system = params.system;
2487
+ if (typeof params.temperature === "number")
2488
+ body.temperature = params.temperature;
2489
+ if (typeof params.maxTokens === "number") body.max_tokens = params.maxTokens;
2490
+ if (typeof params.topP === "number") body.top_p = params.topP;
2491
+ if (params.reasoningEffort) body.reasoning_effort = params.reasoningEffort;
2492
+ return body;
2493
+ }
2494
+ async function consumeInferenceStream(body, broadcast, deferreds) {
2495
+ const reader = body.getReader();
2496
+ const decoder = new TextDecoder();
2497
+ const acc = createToolCallAccumulator();
2498
+ let textBuffer = "";
2499
+ let reasoningBuffer = "";
2500
+ let lastUsage = ZERO_USAGE;
2501
+ let lastFinish = "unknown";
2502
+ const toolCallsArr = [];
2503
+ let buffer = "";
2504
+ const dispatch = (rawLine) => {
2505
+ const parsed = parseSseDataLine(rawLine);
2506
+ if (!parsed) return;
2507
+ const chunk = parsed;
2508
+ const parts = mapFireworksChunk(chunk, acc);
2509
+ for (const part of parts) {
2510
+ if (part.type === "text-delta") textBuffer += part.textDelta;
2511
+ else if (part.type === "reasoning") reasoningBuffer += part.textDelta;
2512
+ else if (part.type === "tool-call") {
2513
+ toolCallsArr.push({
2514
+ toolCallId: part.toolCallId,
2515
+ toolName: part.toolName,
2516
+ args: part.args
2517
+ });
2518
+ } else if (part.type === "finish") {
2519
+ lastFinish = part.finishReason;
2520
+ lastUsage = part.usage;
2521
+ }
2522
+ broadcast.push(part);
2523
+ }
2524
+ };
2525
+ try {
2526
+ while (true) {
2527
+ const { done, value } = await reader.read();
2528
+ if (done) break;
2529
+ buffer += decoder.decode(value, { stream: true });
2530
+ let boundary = buffer.indexOf("\n\n");
2531
+ while (boundary !== -1) {
2532
+ const block = buffer.slice(0, boundary);
2533
+ buffer = buffer.slice(boundary + 2);
2534
+ for (const line of block.split("\n")) dispatch(line);
2535
+ boundary = buffer.indexOf("\n\n");
2536
+ }
2537
+ }
2538
+ if (buffer.trim().length > 0) {
2539
+ for (const line of buffer.split("\n")) dispatch(line);
2540
+ }
2541
+ broadcast.end();
2542
+ deferreds.text.resolve(textBuffer);
2543
+ deferreds.reasoning.resolve(reasoningBuffer);
2544
+ deferreds.usage.resolve(lastUsage);
2545
+ deferreds.finishReason.resolve(lastFinish);
2546
+ deferreds.toolCalls.resolve(toolCallsArr);
2547
+ } catch (error) {
2548
+ broadcast.fail(error);
2549
+ deferreds.text.reject(error);
2550
+ deferreds.reasoning.reject(error);
2551
+ deferreds.usage.reject(error);
2552
+ deferreds.finishReason.reject(error);
2553
+ deferreds.toolCalls.reject(error);
2554
+ } finally {
2555
+ try {
2556
+ reader.releaseLock();
2557
+ } catch {
2558
+ }
2559
+ }
2560
+ }
2561
+ function createStreamResultFromParts(source) {
2562
+ const broadcast = new PartBroadcast();
2563
+ const text = defer();
2564
+ const reasoning = defer();
2565
+ const usage = defer();
2566
+ const finishReason = defer();
2567
+ const toolCalls = defer();
2568
+ void (async () => {
2569
+ let textBuffer = "";
2570
+ let reasoningBuffer = "";
2571
+ let lastUsage = ZERO_USAGE;
2572
+ let lastFinish = "unknown";
2573
+ const toolCallsArr = [];
2574
+ try {
2575
+ for await (const part of source) {
2576
+ if (part.type === "text-delta") textBuffer += part.textDelta;
2577
+ else if (part.type === "reasoning")
2578
+ reasoningBuffer += part.textDelta;
2579
+ else if (part.type === "tool-call") {
2580
+ toolCallsArr.push({
2581
+ toolCallId: part.toolCallId,
2582
+ toolName: part.toolName,
2583
+ args: part.args
2584
+ });
2585
+ } else if (part.type === "finish") {
2586
+ lastFinish = part.finishReason;
2587
+ lastUsage = part.usage;
2588
+ }
2589
+ broadcast.push(part);
2590
+ }
2591
+ broadcast.end();
2592
+ text.resolve(textBuffer);
2593
+ reasoning.resolve(reasoningBuffer);
2594
+ usage.resolve(lastUsage);
2595
+ finishReason.resolve(lastFinish);
2596
+ toolCalls.resolve(toolCallsArr);
2597
+ } catch (error) {
2598
+ broadcast.fail(error);
2599
+ text.reject(error);
2600
+ reasoning.reject(error);
2601
+ usage.reject(error);
2602
+ finishReason.reject(error);
2603
+ toolCalls.reject(error);
2604
+ }
2605
+ })();
2606
+ return {
2607
+ fullStream: broadcast.consume(),
2608
+ textStream: {
2609
+ [Symbol.asyncIterator]: () => {
2610
+ const inner = broadcast.consume()[Symbol.asyncIterator]();
2611
+ return {
2612
+ next: async () => {
2613
+ while (true) {
2614
+ const r = await inner.next();
2615
+ if (r.done)
2616
+ return { value: void 0, done: true };
2617
+ if (r.value.type === "text-delta") {
2618
+ return { value: r.value.textDelta, done: false };
2619
+ }
2620
+ }
2621
+ }
2622
+ };
2623
+ }
2624
+ },
2625
+ text: text.promise,
2626
+ reasoning: reasoning.promise,
2627
+ usage: usage.promise,
2628
+ finishReason: finishReason.promise,
2629
+ toolCalls: toolCalls.promise
2630
+ };
2631
+ }
2632
+ function streamInferenceFromContext(ctx, params) {
2633
+ const broadcast = new PartBroadcast();
2634
+ const text = defer();
2635
+ const reasoning = defer();
2636
+ const usage = defer();
2637
+ const finishReason = defer();
2638
+ const toolCalls = defer();
2639
+ const start = async () => {
2640
+ const response = await ctx.fetch(ctx.url, {
2641
+ method: "POST",
2642
+ headers: ctx.headers,
2643
+ body: JSON.stringify(buildInferenceRequestBody(params)),
2644
+ ...params.signal !== void 0 ? { signal: params.signal } : {}
2645
+ });
2646
+ if (!response.ok) {
2647
+ const payload = await parseMaybeJsonBody(response);
2648
+ const message = extractErrorMessage(payload, response.statusText);
2649
+ throw new RagableError(message, response.status, payload);
2650
+ }
2651
+ if (!response.body) {
2652
+ throw new RagableError("Inference stream has no body", 502, {
2653
+ code: "SDK_INFERENCE_STREAM_NO_BODY"
2654
+ });
2655
+ }
2656
+ await consumeInferenceStream(response.body, broadcast, {
2657
+ text,
2658
+ reasoning,
2659
+ usage,
2660
+ finishReason,
2661
+ toolCalls
2662
+ });
2663
+ };
2664
+ start().catch((err) => {
2665
+ broadcast.fail(err);
2666
+ text.reject(err);
2667
+ reasoning.reject(err);
2668
+ usage.reject(err);
2669
+ finishReason.reject(err);
2670
+ toolCalls.reject(err);
2671
+ });
2672
+ return {
2673
+ fullStream: broadcast.consume(),
2674
+ textStream: {
2675
+ [Symbol.asyncIterator]: () => {
2676
+ const inner = broadcast.consume()[Symbol.asyncIterator]();
2677
+ return {
2678
+ next: async () => {
2679
+ while (true) {
2680
+ const r = await inner.next();
2681
+ if (r.done) return { value: void 0, done: true };
2682
+ if (r.value.type === "text-delta") {
2683
+ return { value: r.value.textDelta, done: false };
2684
+ }
2685
+ }
2686
+ }
2687
+ };
2688
+ }
2689
+ },
2690
+ text: text.promise,
2691
+ reasoning: reasoning.promise,
2692
+ usage: usage.promise,
2693
+ finishReason: finishReason.promise,
2694
+ toolCalls: toolCalls.promise
2695
+ };
2696
+ }
2697
+ var RagableBrowserAiClient = class {
2698
+ constructor(options) {
2699
+ this.options = options;
2700
+ __publicField(this, "fetchImpl");
2701
+ this.fetchImpl = bindFetch(options.fetch);
2702
+ }
2703
+ requireWebsiteId() {
2704
+ const id = this.options.websiteId?.trim();
2705
+ if (!id) {
2706
+ throw new RagableError(
2707
+ "client.ai.streamText requires websiteId on the client. Pass createBrowserClient({ websiteId, organizationId, ... }).",
2708
+ 400,
2709
+ { code: "SDK_MISSING_WEBSITE_ID" }
2710
+ );
2711
+ }
2712
+ return id;
2713
+ }
2714
+ buildContext() {
2715
+ const url = `${this.options.apiBase}/public/organizations/${encodeURIComponent(
2716
+ this.options.organizationId
2717
+ )}/websites/${encodeURIComponent(
2718
+ this.requireWebsiteId()
2719
+ )}/inference/stream`;
2720
+ const headers = new Headers(this.options.headers);
2721
+ headers.set("Content-Type", "application/json");
2722
+ headers.set("Accept", "text/event-stream");
2723
+ return { url, headers, fetch: this.fetchImpl };
2724
+ }
2725
+ streamText(params) {
2726
+ return streamInferenceFromContext(this.buildContext(), params);
2727
+ }
2728
+ async generateText(params) {
2729
+ const result = this.streamText(params);
2730
+ for await (const _ of result.fullStream) {
2731
+ void _;
2732
+ }
2733
+ const [text, reasoning, usage, finishReason, toolCalls] = await Promise.all(
2734
+ [
2735
+ result.text,
2736
+ result.reasoning,
2737
+ result.usage,
2738
+ result.finishReason,
2739
+ result.toolCalls
2740
+ ]
2741
+ );
2742
+ return { text, reasoning, usage, finishReason, toolCalls };
2743
+ }
2744
+ };
2745
+
2533
2746
  // src/browser.ts
2534
2747
  function normalizeBrowserApiBase() {
2535
2748
  return DEFAULT_RAGABLE_API_BASE.replace(/\/+$/, "");
@@ -3373,6 +3586,58 @@ var RagableBrowserAgentsClient = class {
3373
3586
  }
3374
3587
  yield* readSseStream(streamBody);
3375
3588
  }
3589
+ /**
3590
+ * Stream a project agent defined in `agents/*.json` using the same result
3591
+ * shape as `client.ai.streamText`. Preferred over {@link chatStreamByName}
3592
+ * and {@link runChatStreamByName} — those remain for back-compat only.
3593
+ *
3594
+ * ```ts
3595
+ * const { textStream, text } = client.agents.run("support", {
3596
+ * messages: [{ role: "user", content: "I can't log in" }],
3597
+ * });
3598
+ * for await (const delta of textStream) process.stdout.write(delta);
3599
+ * console.log(await text);
3600
+ * ```
3601
+ */
3602
+ run(agentName, params) {
3603
+ const source = this.runStreamParts(agentName, params);
3604
+ return createStreamResultFromParts(source);
3605
+ }
3606
+ async *runStreamParts(agentName, params) {
3607
+ const { messages } = params;
3608
+ if (!Array.isArray(messages) || messages.length === 0) {
3609
+ throw new RagableError(
3610
+ "agents.run requires at least one message",
3611
+ 400,
3612
+ { code: "SDK_AGENTS_RUN_EMPTY_MESSAGES" }
3613
+ );
3614
+ }
3615
+ const last = messages[messages.length - 1];
3616
+ if (!last || last.role !== "user") {
3617
+ throw new RagableError(
3618
+ 'agents.run: the final message must have role "user"',
3619
+ 400,
3620
+ { code: "SDK_AGENTS_RUN_INVALID_LAST_MESSAGE" }
3621
+ );
3622
+ }
3623
+ const history = messages.slice(0, -1).filter((m) => m.role === "user" || m.role === "assistant").map((m) => ({
3624
+ role: m.role,
3625
+ content: m.content
3626
+ }));
3627
+ const events = this.chatStreamByName(agentName, {
3628
+ message: last.content,
3629
+ ...history.length > 0 ? { history } : {},
3630
+ ...params.signal !== void 0 ? { signal: params.signal } : {}
3631
+ });
3632
+ for await (const event of events) {
3633
+ const mapped = mapAgentEvent(event);
3634
+ if (mapped) yield mapped;
3635
+ }
3636
+ }
3637
+ /**
3638
+ * @deprecated Use {@link run} for new code. This method is kept for
3639
+ * back-compat with the original chat-stream DX.
3640
+ */
3376
3641
  async *chatStreamByName(agentName, params) {
3377
3642
  const headers = new Headers(this.options.headers);
3378
3643
  headers.set("Content-Type", "application/json");
@@ -3401,8 +3666,9 @@ var RagableBrowserAgentsClient = class {
3401
3666
  yield* readSseStream(response.body);
3402
3667
  }
3403
3668
  /**
3404
- * Stream a project agent (`/agents/*.json`) with callbacks; returns the final `done` payload
3405
- * plus streamed assistant text. Prefer this over manual `for await` when building chat UIs.
3669
+ * @deprecated Use {@link run} for new code. Returns the legacy callback-based
3670
+ * result type leaking server internals; the new Vercel-style API exposes
3671
+ * `textStream` / `fullStream` / promises for `text`, `usage`, `finishReason`.
3406
3672
  */
3407
3673
  async runChatStreamByName(agentName, params, handlers = {}) {
3408
3674
  return runAgentChatStream(this.chatStreamByName(agentName, params), handlers, {
@@ -3499,6 +3765,7 @@ var RagableBrowserAgentsClient = class {
3499
3765
  var RagableBrowser = class {
3500
3766
  constructor(options) {
3501
3767
  __publicField(this, "agents");
3768
+ __publicField(this, "ai");
3502
3769
  __publicField(this, "auth");
3503
3770
  __publicField(this, "database");
3504
3771
  __publicField(this, "db");
@@ -3533,6 +3800,13 @@ var RagableBrowser = class {
3533
3800
  this._ragableAuth = null;
3534
3801
  }
3535
3802
  this.agents = new RagableBrowserAgentsClient(options);
3803
+ this.ai = new RagableBrowserAiClient({
3804
+ organizationId: options.organizationId,
3805
+ ...options.websiteId !== void 0 ? { websiteId: options.websiteId } : {},
3806
+ ...options.fetch !== void 0 ? { fetch: options.fetch } : {},
3807
+ ...options.headers !== void 0 ? { headers: options.headers } : {},
3808
+ apiBase: normalizeBrowserApiBase()
3809
+ });
3536
3810
  this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
3537
3811
  this.database = new RagableBrowserDatabaseClient(
3538
3812
  options,
@@ -3551,245 +3825,13 @@ function createBrowserClient(options) {
3551
3825
  }
3552
3826
  var createRagableBrowserClient = createBrowserClient;
3553
3827
 
3554
- // src/rag.ts
3555
- function formatRetrievalContext(results, options = {}) {
3556
- const header = options.header ?? "Relevant passages:\n";
3557
- const separator = options.separator ?? "\n\n";
3558
- const minScore = options.minScore;
3559
- let filtered = results;
3560
- if (typeof minScore === "number") {
3561
- filtered = results.filter((r) => r.score >= minScore);
3562
- }
3563
- const blocks = filtered.map((r, i) => {
3564
- const scoreSuffix = options.includeScores === true ? ` (score: ${r.score.toFixed(4)})` : "";
3565
- return `[${i + 1}]${scoreSuffix}
3566
- ${r.content.trim()}`;
3567
- });
3568
- let text = (blocks.length > 0 ? header : "") + blocks.join(separator);
3569
- if (typeof options.maxChars === "number" && text.length > options.maxChars) {
3570
- text = text.slice(0, options.maxChars).trimEnd() + "\n\u2026";
3571
- }
3572
- return text;
3573
- }
3574
- function createRagPipeline(client, options) {
3575
- const { indexId } = options;
3576
- return {
3577
- indexId,
3578
- ingestText(params) {
3579
- return client.shift.documents.create(indexId, params);
3580
- },
3581
- ingestFile(params) {
3582
- return client.shift.documents.upload(indexId, params);
3583
- },
3584
- search(params) {
3585
- return client.shift.search(indexId, params);
3586
- },
3587
- async retrieve(params) {
3588
- const { format: formatOpts, ...searchParams } = params;
3589
- const { results } = await client.shift.search(indexId, searchParams);
3590
- const context = formatRetrievalContext(results, formatOpts ?? {});
3591
- return { results, context };
3592
- }
3593
- };
3594
- }
3595
-
3596
- // src/storage.ts
3597
- function normalizeUploadFile2(file, contentType) {
3598
- if (file instanceof Blob) return file;
3599
- const u8 = file instanceof ArrayBuffer ? new Uint8Array(file) : new Uint8Array(file);
3600
- return new Blob([u8.buffer], contentType ? { type: contentType } : {});
3601
- }
3602
- var StorageClient = class {
3603
- constructor(client) {
3604
- this.client = client;
3605
- /**
3606
- * Bucket-level CRUD — list, create and delete buckets for this organisation.
3607
- */
3608
- __publicField(this, "buckets");
3609
- this.buckets = {
3610
- list: async (params) => {
3611
- const qs = new URLSearchParams();
3612
- if (params?.q) qs.set("q", params.q);
3613
- if (params?.status) qs.set("status", params.status);
3614
- if (params?.sort) qs.set("sort", params.sort);
3615
- const query = qs.toString();
3616
- return this.client.request(
3617
- `/v1/storage/buckets${query ? `?${query}` : ""}`
3618
- );
3619
- },
3620
- create: async (name) => {
3621
- return this.client.request("/v1/storage/buckets", {
3622
- method: "POST",
3623
- body: { name }
3624
- });
3625
- },
3626
- delete: async (bucketId) => {
3627
- return this.client.request(
3628
- `/v1/storage/buckets/${encodeURIComponent(bucketId)}`,
3629
- { method: "DELETE" }
3630
- );
3631
- }
3632
- };
3633
- }
3634
- /**
3635
- * Returns a {@link StorageBucketClient} scoped to the given bucket ID.
3636
- *
3637
- * All object operations (upload, download, list, copy, move, signed URLs, …)
3638
- * are performed through the returned client.
3639
- *
3640
- * @param bucketId The Ragable bucket ID obtained from `buckets.list()` or `buckets.create()`.
3641
- */
3642
- from(bucketId) {
3643
- const { client } = this;
3644
- const base = `/v1/storage/buckets/${encodeURIComponent(bucketId)}`;
3645
- return {
3646
- list: async (params) => {
3647
- const qs = new URLSearchParams();
3648
- if (params?.prefix) qs.set("prefix", params.prefix);
3649
- if (params?.delimiter) qs.set("delimiter", params.delimiter);
3650
- if (params?.maxResults != null)
3651
- qs.set("maxResults", String(params.maxResults));
3652
- if (params?.pageToken) qs.set("pageToken", params.pageToken);
3653
- const query = qs.toString();
3654
- return client.request(
3655
- `${base}/contents${query ? `?${query}` : ""}`
3656
- );
3657
- },
3658
- createFolder: async (folderPath) => {
3659
- return client.request(
3660
- `${base}/folders`,
3661
- { method: "POST", body: { folderPath } }
3662
- );
3663
- },
3664
- deleteFolder: async (folderPath) => {
3665
- return client.request(
3666
- `${base}/folders`,
3667
- { method: "DELETE", body: { folderPath } }
3668
- );
3669
- },
3670
- upload: async (params) => {
3671
- const formData = new FormData();
3672
- const blob = normalizeUploadFile2(params.file, params.contentType);
3673
- const fileName = params.fileName ?? "upload";
3674
- formData.set("file", blob, fileName);
3675
- formData.set("objectPath", params.objectPath);
3676
- if (params.cacheControl) {
3677
- formData.set("cacheControl", params.cacheControl);
3678
- }
3679
- return client.request(`${base}/upload`, {
3680
- method: "POST",
3681
- body: formData
3682
- });
3683
- },
3684
- delete: async (objectPath) => {
3685
- return client.request(
3686
- `${base}/objects`,
3687
- { method: "DELETE", body: { objectPath } }
3688
- );
3689
- },
3690
- bulkDelete: async (objectPaths) => {
3691
- return client.request(
3692
- `${base}/objects/delete-bulk`,
3693
- { method: "POST", body: { objectPaths } }
3694
- );
3695
- },
3696
- getMetadata: async (objectPath) => {
3697
- const qs = new URLSearchParams({ objectPath });
3698
- return client.request(
3699
- `${base}/objects/metadata?${qs}`
3700
- );
3701
- },
3702
- updateMetadata: async (params) => {
3703
- return client.request(
3704
- `${base}/objects/metadata`,
3705
- { method: "PATCH", body: params }
3706
- );
3707
- },
3708
- download: async (params) => {
3709
- const qs = new URLSearchParams({ objectPath: params.objectPath });
3710
- if (params.asText != null) qs.set("asText", String(params.asText));
3711
- if (params.maxTextBytes != null)
3712
- qs.set("maxTextBytes", String(params.maxTextBytes));
3713
- return client.request(
3714
- `${base}/objects/download?${qs}`
3715
- );
3716
- },
3717
- copy: async (params) => {
3718
- return client.request(`${base}/objects/copy`, { method: "POST", body: params });
3719
- },
3720
- move: async (params) => {
3721
- return client.request(`${base}/objects/move`, { method: "POST", body: params });
3722
- },
3723
- getSignedUploadUrl: async (params) => {
3724
- return client.request(
3725
- `${base}/signed-upload-url`,
3726
- { method: "POST", body: params }
3727
- );
3728
- },
3729
- getSignedDownloadUrl: async (params) => {
3730
- const qs = new URLSearchParams({ objectPath: params.objectPath });
3731
- if (params.expiresInSeconds != null)
3732
- qs.set("expiresInSeconds", String(params.expiresInSeconds));
3733
- return client.request(
3734
- `${base}/signed-download-url?${qs}`
3735
- );
3736
- },
3737
- getSettings: async () => {
3738
- return client.request(`${base}/settings`);
3739
- },
3740
- updateSettings: async (params) => {
3741
- return client.request(`${base}/settings`, {
3742
- method: "PATCH",
3743
- body: params
3744
- });
3745
- }
3746
- };
3747
- }
3748
- };
3749
-
3750
3828
  // src/index.ts
3751
- var Ragable = class {
3752
- constructor(options) {
3753
- __publicField(this, "shift");
3754
- __publicField(this, "agents");
3755
- __publicField(this, "storage");
3756
- __publicField(this, "infrastructure");
3757
- const client = new RagableRequestClient(options);
3758
- this.shift = new ShiftClient(client);
3759
- this.agents = new AgentsClient(client);
3760
- this.storage = new StorageClient(client);
3761
- this.infrastructure = {
3762
- shift: this.shift
3763
- };
3764
- }
3765
- };
3766
- function isServerClientOptions(o) {
3767
- return typeof o === "object" && o !== null && "apiKey" in o && typeof o.apiKey === "string" && o.apiKey.length > 0;
3768
- }
3769
3829
  function createClient(options) {
3770
- if (isServerClientOptions(options)) {
3771
- if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
3772
- console.warn(
3773
- "[@ragable/sdk] createClient: `apiKey` is set, so the server client is returned. It has no `database` or `auth` \u2014 only `agents` and `shift`. For `db.collections.<name>` / `database.from()` / `auth.*`, use the browser client without `apiKey` (e.g. createClient({ organizationId, authGroupId, databaseInstanceId, ... }))."
3774
- );
3775
- }
3776
- return new Ragable(options);
3777
- }
3778
- if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
3779
- return createBrowserClient(
3780
- options
3781
- );
3782
- }
3783
- throw new Error(
3784
- "createClient(options) requires apiKey (server) or organizationId (browser)"
3785
- );
3786
- }
3787
- function createRagableServerClient(options) {
3788
- return new Ragable(options);
3830
+ return createBrowserClient(options);
3789
3831
  }
3790
3832
  export {
3791
- AgentsClient,
3792
3833
  AuthBroadcastChannel,
3834
+ BrowserStorageBucketClient,
3793
3835
  CookieStorageAdapter,
3794
3836
  DEFAULT_RAGABLE_API_BASE,
3795
3837
  LocalStorageAdapter,
@@ -3806,43 +3848,42 @@ export {
3806
3848
  PostgrestUpdateRootBuilder,
3807
3849
  PostgrestUpsertReturningBuilder,
3808
3850
  PostgrestUpsertRootBuilder,
3809
- Ragable,
3810
3851
  RagableAbortError,
3811
3852
  RagableAuth,
3812
3853
  RagableBrowser,
3813
3854
  RagableBrowserAgentsClient,
3855
+ RagableBrowserAiClient,
3814
3856
  RagableBrowserAuthClient,
3815
3857
  RagableBrowserDatabaseClient,
3858
+ RagableBrowserStorageClient,
3816
3859
  RagableError,
3817
3860
  RagableNetworkError,
3818
- RagableRequestClient,
3819
3861
  RagableSdkError,
3820
3862
  RagableTimeoutError,
3821
3863
  SessionStorageAdapter,
3822
- ShiftClient,
3823
- StorageClient,
3824
3864
  Transport,
3825
3865
  asPostgrestResponse,
3826
3866
  assertPostgrestSuccess,
3827
3867
  bindFetch,
3868
+ buildInferenceRequestBody,
3828
3869
  collectAssistantTextFromUiSegments,
3829
3870
  collectionRecordToRowWithMeta,
3830
3871
  collectionRecordsToRowWithMeta,
3831
3872
  createBrowserClient,
3832
3873
  createClient,
3833
- createRagPipeline,
3834
3874
  createRagableBrowserClient,
3835
- createRagableServerClient,
3875
+ createStreamResultFromParts,
3836
3876
  detectStorage,
3837
3877
  effectiveDataAuth,
3838
3878
  extractErrorMessage,
3839
3879
  finalizeAgentChatUiTurn,
3840
3880
  foldAgentStreamIntoUiSegments,
3841
3881
  formatPostgrestError,
3842
- formatRetrievalContext,
3843
3882
  formatSdkError,
3844
3883
  generateIdempotencyKey,
3845
3884
  isIncompleteAgentStreamError,
3885
+ mapAgentEvent,
3886
+ mapFireworksChunk,
3846
3887
  normalizeBrowserApiBase,
3847
3888
  parseAgentStreamAgentInfo,
3848
3889
  parseAgentStreamDone,