@ragable/sdk 0.7.5 → 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.d.mts +349 -529
- package/dist/index.d.ts +349 -529
- package/dist/index.js +663 -514
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +656 -506
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
2129
|
-
return Number.isFinite(
|
|
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(/\/+$/, "");
|
|
@@ -3202,6 +3415,113 @@ async function subscribeBrowserRealtime(options, ragableAuth, fetchImpl, params)
|
|
|
3202
3415
|
})();
|
|
3203
3416
|
return subscription;
|
|
3204
3417
|
}
|
|
3418
|
+
var BrowserStorageBucketClient = class {
|
|
3419
|
+
constructor(options, fetchImpl, bucketId) {
|
|
3420
|
+
this.options = options;
|
|
3421
|
+
this.fetchImpl = fetchImpl;
|
|
3422
|
+
this.bucketId = bucketId;
|
|
3423
|
+
}
|
|
3424
|
+
get authGroupId() {
|
|
3425
|
+
const id = this.options.authGroupId?.trim();
|
|
3426
|
+
if (!id) throw new RagableError("authGroupId is required for storage", 400, { code: "SDK_MISSING_AUTH_GROUP_ID" });
|
|
3427
|
+
return id;
|
|
3428
|
+
}
|
|
3429
|
+
base() {
|
|
3430
|
+
return `${normalizeBrowserApiBase()}/auth-groups/${this.authGroupId}/storage/buckets/${encodeURIComponent(this.bucketId)}`;
|
|
3431
|
+
}
|
|
3432
|
+
async bearerToken() {
|
|
3433
|
+
const staticKey = this.options.dataStaticKey?.trim() || (this.options.getDataStaticKey ? await this.options.getDataStaticKey() : null)?.trim() || null;
|
|
3434
|
+
if (staticKey) return staticKey;
|
|
3435
|
+
if (this.options.getAccessToken) {
|
|
3436
|
+
const tok = await this.options.getAccessToken();
|
|
3437
|
+
if (tok?.trim()) return tok.trim();
|
|
3438
|
+
}
|
|
3439
|
+
throw new RagableError("No auth token for storage. Provide dataStaticKey or getAccessToken.", 401, { code: "SDK_NO_ACCESS_TOKEN" });
|
|
3440
|
+
}
|
|
3441
|
+
async req(method, path, body) {
|
|
3442
|
+
const token = await this.bearerToken();
|
|
3443
|
+
const headers = new Headers(this.options.headers);
|
|
3444
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
3445
|
+
if (body !== void 0 && !(body instanceof FormData)) headers.set("Content-Type", "application/json");
|
|
3446
|
+
const res = await this.fetchImpl(`${this.base()}${path}`, {
|
|
3447
|
+
method,
|
|
3448
|
+
headers,
|
|
3449
|
+
body: body instanceof FormData ? body : body !== void 0 ? JSON.stringify(body) : void 0
|
|
3450
|
+
});
|
|
3451
|
+
const payload = await res.json().catch(() => ({}));
|
|
3452
|
+
if (!res.ok) throw new RagableError(payload?.error ?? res.statusText, res.status, payload);
|
|
3453
|
+
return payload;
|
|
3454
|
+
}
|
|
3455
|
+
list(params = {}) {
|
|
3456
|
+
const qs = new URLSearchParams();
|
|
3457
|
+
if (params.prefix) qs.set("prefix", params.prefix);
|
|
3458
|
+
if (params.delimiter) qs.set("delimiter", params.delimiter);
|
|
3459
|
+
if (params.maxResults != null) qs.set("maxResults", String(params.maxResults));
|
|
3460
|
+
if (params.pageToken) qs.set("pageToken", params.pageToken);
|
|
3461
|
+
const q = qs.toString();
|
|
3462
|
+
return this.req("GET", `/contents${q ? `?${q}` : ""}`);
|
|
3463
|
+
}
|
|
3464
|
+
async upload(params) {
|
|
3465
|
+
const token = await this.bearerToken();
|
|
3466
|
+
const headers = new Headers(this.options.headers);
|
|
3467
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
3468
|
+
const form = new FormData();
|
|
3469
|
+
const raw = params.file instanceof Blob ? params.file : new Blob([new Uint8Array(params.file instanceof ArrayBuffer ? params.file : params.file.buffer)], params.contentType ? { type: params.contentType } : {});
|
|
3470
|
+
const blob = raw;
|
|
3471
|
+
form.set("file", blob, params.fileName ?? "upload");
|
|
3472
|
+
form.set("objectPath", params.objectPath);
|
|
3473
|
+
if (params.cacheControl) form.set("cacheControl", params.cacheControl);
|
|
3474
|
+
const res = await this.fetchImpl(`${this.base()}/upload`, { method: "POST", headers, body: form });
|
|
3475
|
+
const payload = await res.json().catch(() => ({}));
|
|
3476
|
+
if (!res.ok) throw new RagableError(payload?.error ?? res.statusText, res.status, payload);
|
|
3477
|
+
return payload;
|
|
3478
|
+
}
|
|
3479
|
+
download(params) {
|
|
3480
|
+
const qs = new URLSearchParams({ objectPath: params.objectPath });
|
|
3481
|
+
if (params.asText != null) qs.set("asText", String(params.asText));
|
|
3482
|
+
if (params.maxTextBytes != null) qs.set("maxTextBytes", String(params.maxTextBytes));
|
|
3483
|
+
return this.req("GET", `/objects/download?${qs}`);
|
|
3484
|
+
}
|
|
3485
|
+
delete(objectPath) {
|
|
3486
|
+
return this.req("DELETE", "/objects", { objectPath });
|
|
3487
|
+
}
|
|
3488
|
+
bulkDelete(objectPaths) {
|
|
3489
|
+
return this.req("POST", "/objects/delete-bulk", { objectPaths });
|
|
3490
|
+
}
|
|
3491
|
+
getSignedUploadUrl(params) {
|
|
3492
|
+
return this.req("POST", "/signed-upload-url", params);
|
|
3493
|
+
}
|
|
3494
|
+
getSignedDownloadUrl(params) {
|
|
3495
|
+
const qs = new URLSearchParams({ objectPath: params.objectPath });
|
|
3496
|
+
if (params.expiresInSeconds != null) qs.set("expiresInSeconds", String(params.expiresInSeconds));
|
|
3497
|
+
return this.req("GET", `/signed-download-url?${qs}`);
|
|
3498
|
+
}
|
|
3499
|
+
copy(params) {
|
|
3500
|
+
return this.req("POST", "/objects/copy", params);
|
|
3501
|
+
}
|
|
3502
|
+
move(params) {
|
|
3503
|
+
return this.req("POST", "/objects/move", params);
|
|
3504
|
+
}
|
|
3505
|
+
createFolder(folderPath) {
|
|
3506
|
+
return this.req("POST", "/folders", { folderPath });
|
|
3507
|
+
}
|
|
3508
|
+
deleteFolder(folderPath) {
|
|
3509
|
+
return this.req("DELETE", "/folders", { folderPath });
|
|
3510
|
+
}
|
|
3511
|
+
getMetadata(objectPath) {
|
|
3512
|
+
const qs = new URLSearchParams({ objectPath });
|
|
3513
|
+
return this.req("GET", `/objects/metadata?${qs}`);
|
|
3514
|
+
}
|
|
3515
|
+
};
|
|
3516
|
+
var RagableBrowserStorageClient = class {
|
|
3517
|
+
constructor(options, fetchImpl) {
|
|
3518
|
+
this.options = options;
|
|
3519
|
+
this.fetchImpl = fetchImpl;
|
|
3520
|
+
}
|
|
3521
|
+
from(bucketId) {
|
|
3522
|
+
return new BrowserStorageBucketClient(this.options, this.fetchImpl, bucketId);
|
|
3523
|
+
}
|
|
3524
|
+
};
|
|
3205
3525
|
var RagableBrowserAgentsClient = class {
|
|
3206
3526
|
constructor(options) {
|
|
3207
3527
|
this.options = options;
|
|
@@ -3266,6 +3586,58 @@ var RagableBrowserAgentsClient = class {
|
|
|
3266
3586
|
}
|
|
3267
3587
|
yield* readSseStream(streamBody);
|
|
3268
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
|
+
*/
|
|
3269
3641
|
async *chatStreamByName(agentName, params) {
|
|
3270
3642
|
const headers = new Headers(this.options.headers);
|
|
3271
3643
|
headers.set("Content-Type", "application/json");
|
|
@@ -3294,8 +3666,9 @@ var RagableBrowserAgentsClient = class {
|
|
|
3294
3666
|
yield* readSseStream(response.body);
|
|
3295
3667
|
}
|
|
3296
3668
|
/**
|
|
3297
|
-
*
|
|
3298
|
-
*
|
|
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`.
|
|
3299
3672
|
*/
|
|
3300
3673
|
async runChatStreamByName(agentName, params, handlers = {}) {
|
|
3301
3674
|
return runAgentChatStream(this.chatStreamByName(agentName, params), handlers, {
|
|
@@ -3392,9 +3765,11 @@ var RagableBrowserAgentsClient = class {
|
|
|
3392
3765
|
var RagableBrowser = class {
|
|
3393
3766
|
constructor(options) {
|
|
3394
3767
|
__publicField(this, "agents");
|
|
3768
|
+
__publicField(this, "ai");
|
|
3395
3769
|
__publicField(this, "auth");
|
|
3396
3770
|
__publicField(this, "database");
|
|
3397
3771
|
__publicField(this, "db");
|
|
3772
|
+
__publicField(this, "storage");
|
|
3398
3773
|
__publicField(this, "transport");
|
|
3399
3774
|
__publicField(this, "_ragableAuth");
|
|
3400
3775
|
/** Delegates to `database.from()`. Kept for back-compat — prefer `database.from()`. */
|
|
@@ -3425,6 +3800,13 @@ var RagableBrowser = class {
|
|
|
3425
3800
|
this._ragableAuth = null;
|
|
3426
3801
|
}
|
|
3427
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
|
+
});
|
|
3428
3810
|
this.auth = new RagableBrowserAuthClient(options, this._ragableAuth);
|
|
3429
3811
|
this.database = new RagableBrowserDatabaseClient(
|
|
3430
3812
|
options,
|
|
@@ -3432,6 +3814,7 @@ var RagableBrowser = class {
|
|
|
3432
3814
|
);
|
|
3433
3815
|
this.database._setTransport(this.transport);
|
|
3434
3816
|
this.db = this.database;
|
|
3817
|
+
this.storage = new RagableBrowserStorageClient(options, bindFetch(options.fetch));
|
|
3435
3818
|
}
|
|
3436
3819
|
destroy() {
|
|
3437
3820
|
this._ragableAuth?.destroy();
|
|
@@ -3442,245 +3825,13 @@ function createBrowserClient(options) {
|
|
|
3442
3825
|
}
|
|
3443
3826
|
var createRagableBrowserClient = createBrowserClient;
|
|
3444
3827
|
|
|
3445
|
-
// src/rag.ts
|
|
3446
|
-
function formatRetrievalContext(results, options = {}) {
|
|
3447
|
-
const header = options.header ?? "Relevant passages:\n";
|
|
3448
|
-
const separator = options.separator ?? "\n\n";
|
|
3449
|
-
const minScore = options.minScore;
|
|
3450
|
-
let filtered = results;
|
|
3451
|
-
if (typeof minScore === "number") {
|
|
3452
|
-
filtered = results.filter((r) => r.score >= minScore);
|
|
3453
|
-
}
|
|
3454
|
-
const blocks = filtered.map((r, i) => {
|
|
3455
|
-
const scoreSuffix = options.includeScores === true ? ` (score: ${r.score.toFixed(4)})` : "";
|
|
3456
|
-
return `[${i + 1}]${scoreSuffix}
|
|
3457
|
-
${r.content.trim()}`;
|
|
3458
|
-
});
|
|
3459
|
-
let text = (blocks.length > 0 ? header : "") + blocks.join(separator);
|
|
3460
|
-
if (typeof options.maxChars === "number" && text.length > options.maxChars) {
|
|
3461
|
-
text = text.slice(0, options.maxChars).trimEnd() + "\n\u2026";
|
|
3462
|
-
}
|
|
3463
|
-
return text;
|
|
3464
|
-
}
|
|
3465
|
-
function createRagPipeline(client, options) {
|
|
3466
|
-
const { indexId } = options;
|
|
3467
|
-
return {
|
|
3468
|
-
indexId,
|
|
3469
|
-
ingestText(params) {
|
|
3470
|
-
return client.shift.documents.create(indexId, params);
|
|
3471
|
-
},
|
|
3472
|
-
ingestFile(params) {
|
|
3473
|
-
return client.shift.documents.upload(indexId, params);
|
|
3474
|
-
},
|
|
3475
|
-
search(params) {
|
|
3476
|
-
return client.shift.search(indexId, params);
|
|
3477
|
-
},
|
|
3478
|
-
async retrieve(params) {
|
|
3479
|
-
const { format: formatOpts, ...searchParams } = params;
|
|
3480
|
-
const { results } = await client.shift.search(indexId, searchParams);
|
|
3481
|
-
const context = formatRetrievalContext(results, formatOpts ?? {});
|
|
3482
|
-
return { results, context };
|
|
3483
|
-
}
|
|
3484
|
-
};
|
|
3485
|
-
}
|
|
3486
|
-
|
|
3487
|
-
// src/storage.ts
|
|
3488
|
-
function normalizeUploadFile2(file, contentType) {
|
|
3489
|
-
if (file instanceof Blob) return file;
|
|
3490
|
-
const u8 = file instanceof ArrayBuffer ? new Uint8Array(file) : new Uint8Array(file);
|
|
3491
|
-
return new Blob([u8.buffer], contentType ? { type: contentType } : {});
|
|
3492
|
-
}
|
|
3493
|
-
var StorageClient = class {
|
|
3494
|
-
constructor(client) {
|
|
3495
|
-
this.client = client;
|
|
3496
|
-
/**
|
|
3497
|
-
* Bucket-level CRUD — list, create and delete buckets for this organisation.
|
|
3498
|
-
*/
|
|
3499
|
-
__publicField(this, "buckets");
|
|
3500
|
-
this.buckets = {
|
|
3501
|
-
list: async (params) => {
|
|
3502
|
-
const qs = new URLSearchParams();
|
|
3503
|
-
if (params?.q) qs.set("q", params.q);
|
|
3504
|
-
if (params?.status) qs.set("status", params.status);
|
|
3505
|
-
if (params?.sort) qs.set("sort", params.sort);
|
|
3506
|
-
const query = qs.toString();
|
|
3507
|
-
return this.client.request(
|
|
3508
|
-
`/v1/storage/buckets${query ? `?${query}` : ""}`
|
|
3509
|
-
);
|
|
3510
|
-
},
|
|
3511
|
-
create: async (name) => {
|
|
3512
|
-
return this.client.request("/v1/storage/buckets", {
|
|
3513
|
-
method: "POST",
|
|
3514
|
-
body: { name }
|
|
3515
|
-
});
|
|
3516
|
-
},
|
|
3517
|
-
delete: async (bucketId) => {
|
|
3518
|
-
return this.client.request(
|
|
3519
|
-
`/v1/storage/buckets/${encodeURIComponent(bucketId)}`,
|
|
3520
|
-
{ method: "DELETE" }
|
|
3521
|
-
);
|
|
3522
|
-
}
|
|
3523
|
-
};
|
|
3524
|
-
}
|
|
3525
|
-
/**
|
|
3526
|
-
* Returns a {@link StorageBucketClient} scoped to the given bucket ID.
|
|
3527
|
-
*
|
|
3528
|
-
* All object operations (upload, download, list, copy, move, signed URLs, …)
|
|
3529
|
-
* are performed through the returned client.
|
|
3530
|
-
*
|
|
3531
|
-
* @param bucketId The Ragable bucket ID obtained from `buckets.list()` or `buckets.create()`.
|
|
3532
|
-
*/
|
|
3533
|
-
from(bucketId) {
|
|
3534
|
-
const { client } = this;
|
|
3535
|
-
const base = `/v1/storage/buckets/${encodeURIComponent(bucketId)}`;
|
|
3536
|
-
return {
|
|
3537
|
-
list: async (params) => {
|
|
3538
|
-
const qs = new URLSearchParams();
|
|
3539
|
-
if (params?.prefix) qs.set("prefix", params.prefix);
|
|
3540
|
-
if (params?.delimiter) qs.set("delimiter", params.delimiter);
|
|
3541
|
-
if (params?.maxResults != null)
|
|
3542
|
-
qs.set("maxResults", String(params.maxResults));
|
|
3543
|
-
if (params?.pageToken) qs.set("pageToken", params.pageToken);
|
|
3544
|
-
const query = qs.toString();
|
|
3545
|
-
return client.request(
|
|
3546
|
-
`${base}/contents${query ? `?${query}` : ""}`
|
|
3547
|
-
);
|
|
3548
|
-
},
|
|
3549
|
-
createFolder: async (folderPath) => {
|
|
3550
|
-
return client.request(
|
|
3551
|
-
`${base}/folders`,
|
|
3552
|
-
{ method: "POST", body: { folderPath } }
|
|
3553
|
-
);
|
|
3554
|
-
},
|
|
3555
|
-
deleteFolder: async (folderPath) => {
|
|
3556
|
-
return client.request(
|
|
3557
|
-
`${base}/folders`,
|
|
3558
|
-
{ method: "DELETE", body: { folderPath } }
|
|
3559
|
-
);
|
|
3560
|
-
},
|
|
3561
|
-
upload: async (params) => {
|
|
3562
|
-
const formData = new FormData();
|
|
3563
|
-
const blob = normalizeUploadFile2(params.file, params.contentType);
|
|
3564
|
-
const fileName = params.fileName ?? "upload";
|
|
3565
|
-
formData.set("file", blob, fileName);
|
|
3566
|
-
formData.set("objectPath", params.objectPath);
|
|
3567
|
-
if (params.cacheControl) {
|
|
3568
|
-
formData.set("cacheControl", params.cacheControl);
|
|
3569
|
-
}
|
|
3570
|
-
return client.request(`${base}/upload`, {
|
|
3571
|
-
method: "POST",
|
|
3572
|
-
body: formData
|
|
3573
|
-
});
|
|
3574
|
-
},
|
|
3575
|
-
delete: async (objectPath) => {
|
|
3576
|
-
return client.request(
|
|
3577
|
-
`${base}/objects`,
|
|
3578
|
-
{ method: "DELETE", body: { objectPath } }
|
|
3579
|
-
);
|
|
3580
|
-
},
|
|
3581
|
-
bulkDelete: async (objectPaths) => {
|
|
3582
|
-
return client.request(
|
|
3583
|
-
`${base}/objects/delete-bulk`,
|
|
3584
|
-
{ method: "POST", body: { objectPaths } }
|
|
3585
|
-
);
|
|
3586
|
-
},
|
|
3587
|
-
getMetadata: async (objectPath) => {
|
|
3588
|
-
const qs = new URLSearchParams({ objectPath });
|
|
3589
|
-
return client.request(
|
|
3590
|
-
`${base}/objects/metadata?${qs}`
|
|
3591
|
-
);
|
|
3592
|
-
},
|
|
3593
|
-
updateMetadata: async (params) => {
|
|
3594
|
-
return client.request(
|
|
3595
|
-
`${base}/objects/metadata`,
|
|
3596
|
-
{ method: "PATCH", body: params }
|
|
3597
|
-
);
|
|
3598
|
-
},
|
|
3599
|
-
download: async (params) => {
|
|
3600
|
-
const qs = new URLSearchParams({ objectPath: params.objectPath });
|
|
3601
|
-
if (params.asText != null) qs.set("asText", String(params.asText));
|
|
3602
|
-
if (params.maxTextBytes != null)
|
|
3603
|
-
qs.set("maxTextBytes", String(params.maxTextBytes));
|
|
3604
|
-
return client.request(
|
|
3605
|
-
`${base}/objects/download?${qs}`
|
|
3606
|
-
);
|
|
3607
|
-
},
|
|
3608
|
-
copy: async (params) => {
|
|
3609
|
-
return client.request(`${base}/objects/copy`, { method: "POST", body: params });
|
|
3610
|
-
},
|
|
3611
|
-
move: async (params) => {
|
|
3612
|
-
return client.request(`${base}/objects/move`, { method: "POST", body: params });
|
|
3613
|
-
},
|
|
3614
|
-
getSignedUploadUrl: async (params) => {
|
|
3615
|
-
return client.request(
|
|
3616
|
-
`${base}/signed-upload-url`,
|
|
3617
|
-
{ method: "POST", body: params }
|
|
3618
|
-
);
|
|
3619
|
-
},
|
|
3620
|
-
getSignedDownloadUrl: async (params) => {
|
|
3621
|
-
const qs = new URLSearchParams({ objectPath: params.objectPath });
|
|
3622
|
-
if (params.expiresInSeconds != null)
|
|
3623
|
-
qs.set("expiresInSeconds", String(params.expiresInSeconds));
|
|
3624
|
-
return client.request(
|
|
3625
|
-
`${base}/signed-download-url?${qs}`
|
|
3626
|
-
);
|
|
3627
|
-
},
|
|
3628
|
-
getSettings: async () => {
|
|
3629
|
-
return client.request(`${base}/settings`);
|
|
3630
|
-
},
|
|
3631
|
-
updateSettings: async (params) => {
|
|
3632
|
-
return client.request(`${base}/settings`, {
|
|
3633
|
-
method: "PATCH",
|
|
3634
|
-
body: params
|
|
3635
|
-
});
|
|
3636
|
-
}
|
|
3637
|
-
};
|
|
3638
|
-
}
|
|
3639
|
-
};
|
|
3640
|
-
|
|
3641
3828
|
// src/index.ts
|
|
3642
|
-
var Ragable = class {
|
|
3643
|
-
constructor(options) {
|
|
3644
|
-
__publicField(this, "shift");
|
|
3645
|
-
__publicField(this, "agents");
|
|
3646
|
-
__publicField(this, "storage");
|
|
3647
|
-
__publicField(this, "infrastructure");
|
|
3648
|
-
const client = new RagableRequestClient(options);
|
|
3649
|
-
this.shift = new ShiftClient(client);
|
|
3650
|
-
this.agents = new AgentsClient(client);
|
|
3651
|
-
this.storage = new StorageClient(client);
|
|
3652
|
-
this.infrastructure = {
|
|
3653
|
-
shift: this.shift
|
|
3654
|
-
};
|
|
3655
|
-
}
|
|
3656
|
-
};
|
|
3657
|
-
function isServerClientOptions(o) {
|
|
3658
|
-
return typeof o === "object" && o !== null && "apiKey" in o && typeof o.apiKey === "string" && o.apiKey.length > 0;
|
|
3659
|
-
}
|
|
3660
3829
|
function createClient(options) {
|
|
3661
|
-
|
|
3662
|
-
if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
|
|
3663
|
-
console.warn(
|
|
3664
|
-
"[@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, ... }))."
|
|
3665
|
-
);
|
|
3666
|
-
}
|
|
3667
|
-
return new Ragable(options);
|
|
3668
|
-
}
|
|
3669
|
-
if (typeof options === "object" && options !== null && "organizationId" in options && typeof options.organizationId === "string") {
|
|
3670
|
-
return createBrowserClient(
|
|
3671
|
-
options
|
|
3672
|
-
);
|
|
3673
|
-
}
|
|
3674
|
-
throw new Error(
|
|
3675
|
-
"createClient(options) requires apiKey (server) or organizationId (browser)"
|
|
3676
|
-
);
|
|
3677
|
-
}
|
|
3678
|
-
function createRagableServerClient(options) {
|
|
3679
|
-
return new Ragable(options);
|
|
3830
|
+
return createBrowserClient(options);
|
|
3680
3831
|
}
|
|
3681
3832
|
export {
|
|
3682
|
-
AgentsClient,
|
|
3683
3833
|
AuthBroadcastChannel,
|
|
3834
|
+
BrowserStorageBucketClient,
|
|
3684
3835
|
CookieStorageAdapter,
|
|
3685
3836
|
DEFAULT_RAGABLE_API_BASE,
|
|
3686
3837
|
LocalStorageAdapter,
|
|
@@ -3697,43 +3848,42 @@ export {
|
|
|
3697
3848
|
PostgrestUpdateRootBuilder,
|
|
3698
3849
|
PostgrestUpsertReturningBuilder,
|
|
3699
3850
|
PostgrestUpsertRootBuilder,
|
|
3700
|
-
Ragable,
|
|
3701
3851
|
RagableAbortError,
|
|
3702
3852
|
RagableAuth,
|
|
3703
3853
|
RagableBrowser,
|
|
3704
3854
|
RagableBrowserAgentsClient,
|
|
3855
|
+
RagableBrowserAiClient,
|
|
3705
3856
|
RagableBrowserAuthClient,
|
|
3706
3857
|
RagableBrowserDatabaseClient,
|
|
3858
|
+
RagableBrowserStorageClient,
|
|
3707
3859
|
RagableError,
|
|
3708
3860
|
RagableNetworkError,
|
|
3709
|
-
RagableRequestClient,
|
|
3710
3861
|
RagableSdkError,
|
|
3711
3862
|
RagableTimeoutError,
|
|
3712
3863
|
SessionStorageAdapter,
|
|
3713
|
-
ShiftClient,
|
|
3714
|
-
StorageClient,
|
|
3715
3864
|
Transport,
|
|
3716
3865
|
asPostgrestResponse,
|
|
3717
3866
|
assertPostgrestSuccess,
|
|
3718
3867
|
bindFetch,
|
|
3868
|
+
buildInferenceRequestBody,
|
|
3719
3869
|
collectAssistantTextFromUiSegments,
|
|
3720
3870
|
collectionRecordToRowWithMeta,
|
|
3721
3871
|
collectionRecordsToRowWithMeta,
|
|
3722
3872
|
createBrowserClient,
|
|
3723
3873
|
createClient,
|
|
3724
|
-
createRagPipeline,
|
|
3725
3874
|
createRagableBrowserClient,
|
|
3726
|
-
|
|
3875
|
+
createStreamResultFromParts,
|
|
3727
3876
|
detectStorage,
|
|
3728
3877
|
effectiveDataAuth,
|
|
3729
3878
|
extractErrorMessage,
|
|
3730
3879
|
finalizeAgentChatUiTurn,
|
|
3731
3880
|
foldAgentStreamIntoUiSegments,
|
|
3732
3881
|
formatPostgrestError,
|
|
3733
|
-
formatRetrievalContext,
|
|
3734
3882
|
formatSdkError,
|
|
3735
3883
|
generateIdempotencyKey,
|
|
3736
3884
|
isIncompleteAgentStreamError,
|
|
3885
|
+
mapAgentEvent,
|
|
3886
|
+
mapFireworksChunk,
|
|
3737
3887
|
normalizeBrowserApiBase,
|
|
3738
3888
|
parseAgentStreamAgentInfo,
|
|
3739
3889
|
parseAgentStreamDone,
|