@ragable/sdk 0.7.9 → 0.7.10

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
@@ -2335,6 +2335,95 @@ function stripTrailingCommas(text) {
2335
2335
  return text.replace(/,(\s*[}\]])/g, "$1").replace(/,\s*$/, "");
2336
2336
  }
2337
2337
 
2338
+ // src/content.ts
2339
+ var MAX_IMAGE_BYTES = 5 * 1024 * 1024;
2340
+ function toWireUserContent(content) {
2341
+ if (typeof content === "string") return content;
2342
+ const out = [];
2343
+ for (const part of content) {
2344
+ if (!part) continue;
2345
+ if (part.type === "text") {
2346
+ out.push(toWireTextPart(part));
2347
+ } else if (part.type === "image") {
2348
+ out.push(toWireImagePart(part));
2349
+ }
2350
+ }
2351
+ return out;
2352
+ }
2353
+ function toWireTextPart(part) {
2354
+ return { type: "text", text: part.text };
2355
+ }
2356
+ function toWireImagePart(part) {
2357
+ const url = imagePartToUrl(part);
2358
+ const detail = part.detail;
2359
+ return {
2360
+ type: "image_url",
2361
+ image_url: detail ? { url, detail } : { url }
2362
+ };
2363
+ }
2364
+ function imagePartToUrl(part) {
2365
+ const img = part.image;
2366
+ if (img instanceof URL) return img.href;
2367
+ if (typeof img === "string") {
2368
+ if (img.startsWith("http://") || img.startsWith("https://")) return img;
2369
+ if (img.startsWith("data:")) return img;
2370
+ const mediaType = requireMediaType(part, "raw base64 string");
2371
+ assertBase64SizeOk(img);
2372
+ return `data:${mediaType};base64,${img}`;
2373
+ }
2374
+ if (img instanceof Uint8Array || img instanceof ArrayBuffer) {
2375
+ const bytes = img instanceof Uint8Array ? img : new Uint8Array(img);
2376
+ assertBinarySizeOk(bytes);
2377
+ const mediaType = requireMediaType(part, "binary image data");
2378
+ const b64 = bytesToBase64(bytes);
2379
+ return `data:${mediaType};base64,${b64}`;
2380
+ }
2381
+ throw new RagableError(
2382
+ "ImagePart.image must be a string, URL, Uint8Array, or ArrayBuffer",
2383
+ 400,
2384
+ { code: "SDK_INVALID_IMAGE_PART" }
2385
+ );
2386
+ }
2387
+ function requireMediaType(part, what) {
2388
+ const m = part.mediaType?.trim();
2389
+ if (!m) {
2390
+ throw new RagableError(
2391
+ `ImagePart.mediaType is required for ${what} (e.g. "image/png")`,
2392
+ 400,
2393
+ { code: "SDK_IMAGE_MEDIA_TYPE_REQUIRED" }
2394
+ );
2395
+ }
2396
+ return m;
2397
+ }
2398
+ function assertBinarySizeOk(bytes) {
2399
+ if (bytes.byteLength > MAX_IMAGE_BYTES) {
2400
+ throw new RagableError(
2401
+ `Image exceeds 5MB limit (${bytes.byteLength} bytes)`,
2402
+ 400,
2403
+ { code: "SDK_IMAGE_TOO_LARGE" }
2404
+ );
2405
+ }
2406
+ }
2407
+ function assertBase64SizeOk(b64) {
2408
+ const approxBytes = Math.floor(b64.length * 3 / 4);
2409
+ if (approxBytes > MAX_IMAGE_BYTES) {
2410
+ throw new RagableError(
2411
+ `Image exceeds 5MB limit (~${approxBytes} bytes decoded)`,
2412
+ 400,
2413
+ { code: "SDK_IMAGE_TOO_LARGE" }
2414
+ );
2415
+ }
2416
+ }
2417
+ function bytesToBase64(bytes) {
2418
+ const CHUNK = 32768;
2419
+ let binary = "";
2420
+ for (let i = 0; i < bytes.length; i += CHUNK) {
2421
+ const slice = bytes.subarray(i, i + CHUNK);
2422
+ binary += String.fromCharCode(...slice);
2423
+ }
2424
+ return btoa(binary);
2425
+ }
2426
+
2338
2427
  // src/stream-parts.ts
2339
2428
  function normalizeFinishReason(raw) {
2340
2429
  switch (raw) {
@@ -2547,7 +2636,9 @@ var ZERO_USAGE = {
2547
2636
  function buildInferenceRequestBody(params, responseFormat) {
2548
2637
  const body = {
2549
2638
  model: params.model,
2550
- messages: params.messages
2639
+ messages: params.messages.map(
2640
+ (m) => m.role === "user" ? { role: "user", content: toWireUserContent(m.content) } : m
2641
+ )
2551
2642
  };
2552
2643
  if (params.system !== void 0) body.system = params.system;
2553
2644
  if (typeof params.temperature === "number")
@@ -4322,6 +4413,7 @@ export {
4322
4413
  bindFetch,
4323
4414
  buildInferenceRequestBody,
4324
4415
  buildResponseFormat,
4416
+ bytesToBase64,
4325
4417
  collectAssistantTextFromUiSegments,
4326
4418
  collectionRecordToRowWithMeta,
4327
4419
  collectionRecordsToRowWithMeta,
@@ -4337,6 +4429,7 @@ export {
4337
4429
  formatPostgrestError,
4338
4430
  formatSdkError,
4339
4431
  generateIdempotencyKey,
4432
+ imagePartToUrl,
4340
4433
  isIncompleteAgentStreamError,
4341
4434
  mapAgentEvent,
4342
4435
  mapFireworksChunk,
@@ -4351,6 +4444,7 @@ export {
4351
4444
  runAgentChatStreamLenient,
4352
4445
  streamObjectFromContext,
4353
4446
  toRagableResult,
4447
+ toWireUserContent,
4354
4448
  tryParsePartialJson,
4355
4449
  unwrapPostgrest,
4356
4450
  wrapStreamTextAsObject