@superlinked/sie-sdk 0.6.22 → 0.6.23

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.cjs CHANGED
@@ -1191,7 +1191,7 @@ function extractDataPayload(block) {
1191
1191
  }
1192
1192
 
1193
1193
  // src/version.ts
1194
- var SDK_VERSION = "0.6.22";
1194
+ var SDK_VERSION = "0.6.23";
1195
1195
 
1196
1196
  // src/client.ts
1197
1197
  function sleep2(ms) {
@@ -1245,6 +1245,33 @@ function validateGenerationSeed(seed) {
1245
1245
  }
1246
1246
  return seed;
1247
1247
  }
1248
+ function validateGenerateGrammar(grammar) {
1249
+ if (typeof grammar !== "object" || grammar === null || Array.isArray(grammar)) {
1250
+ throw new TypeError("grammar must be an object");
1251
+ }
1252
+ const allowed = /* @__PURE__ */ new Set(["json_schema", "regex", "ebnf", "label", "strict"]);
1253
+ const unknown = Object.keys(grammar).filter((key) => !allowed.has(key));
1254
+ if (unknown.length > 0) {
1255
+ throw new TypeError(`grammar contains unsupported field(s): ${unknown.sort().join(", ")}`);
1256
+ }
1257
+ const variants = ["json_schema", "regex", "ebnf"].filter((key) => Object.hasOwn(grammar, key));
1258
+ if (variants.length !== 1) {
1259
+ throw new TypeError("grammar must contain exactly one of json_schema, regex, or ebnf");
1260
+ }
1261
+ const variant = variants[0];
1262
+ const value = grammar[variant];
1263
+ if (variant === "json_schema" ? typeof value !== "object" || value === null || Array.isArray(value) : typeof value !== "string") {
1264
+ throw new TypeError(
1265
+ variant === "json_schema" ? "grammar.json_schema must be an object" : `grammar.${variant} must be a string`
1266
+ );
1267
+ }
1268
+ if (grammar.label !== void 0 && grammar.label !== null && typeof grammar.label !== "string") {
1269
+ throw new TypeError("grammar.label must be a string");
1270
+ }
1271
+ if (grammar.strict !== void 0 && grammar.strict !== null && typeof grammar.strict !== "boolean") {
1272
+ throw new TypeError("grammar.strict must be a boolean");
1273
+ }
1274
+ }
1248
1275
  function applyGenerateOptions(body, options) {
1249
1276
  if (options.temperature !== void 0) body.temperature = options.temperature;
1250
1277
  if (options.topP !== void 0) body.top_p = options.topP;
@@ -1252,7 +1279,10 @@ function applyGenerateOptions(body, options) {
1252
1279
  if (options.stop !== void 0) body.stop = options.stop;
1253
1280
  if (options.frequencyPenalty !== void 0) body.frequency_penalty = options.frequencyPenalty;
1254
1281
  if (options.presencePenalty !== void 0) body.presence_penalty = options.presencePenalty;
1255
- if (options.grammar !== void 0) body.grammar = options.grammar;
1282
+ if (options.grammar !== void 0) {
1283
+ validateGenerateGrammar(options.grammar);
1284
+ body.grammar = options.grammar;
1285
+ }
1256
1286
  if (options.seed !== void 0) body.seed = validateGenerationSeed(options.seed);
1257
1287
  if (options.logitBias !== void 0) body.logit_bias = options.logitBias;
1258
1288
  if (options.routingKey !== void 0) body.routing_key = options.routingKey;
@@ -1292,6 +1322,25 @@ async function imageForWire(image) {
1292
1322
  }
1293
1323
  return toImageWireFormat(image);
1294
1324
  }
1325
+ function imageBytesToBase64(data) {
1326
+ if (typeof Buffer !== "undefined") {
1327
+ return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString("base64");
1328
+ }
1329
+ let binary = "";
1330
+ const chunkSize = 32768;
1331
+ for (let offset = 0; offset < data.length; offset += chunkSize) {
1332
+ binary += String.fromCharCode(...data.subarray(offset, offset + chunkSize));
1333
+ }
1334
+ return btoa(binary);
1335
+ }
1336
+ async function generationImagesForWire(images) {
1337
+ return Promise.all(
1338
+ images.map(async (image) => {
1339
+ const wire = await imageForWire(image);
1340
+ return { data: imageBytesToBase64(wire.data), format: wire.format };
1341
+ })
1342
+ );
1343
+ }
1295
1344
  async function itemImagesForWire(item) {
1296
1345
  if (!item.images || item.images.length === 0) {
1297
1346
  return item;
@@ -1623,6 +1672,9 @@ var SIEClient = class {
1623
1672
  prompt,
1624
1673
  max_new_tokens: options.maxNewTokens
1625
1674
  };
1675
+ if (options.images !== void 0) {
1676
+ body.images = await generationImagesForWire(options.images);
1677
+ }
1626
1678
  applyGenerateOptions(body, options);
1627
1679
  const { pool, gpu } = this.parseGpuParam(options.gpu);
1628
1680
  const headers = {
@@ -1843,6 +1895,9 @@ var SIEClient = class {
1843
1895
  max_new_tokens: options.maxNewTokens,
1844
1896
  stream: true
1845
1897
  };
1898
+ if (options.images !== void 0) {
1899
+ body.images = await generationImagesForWire(options.images);
1900
+ }
1846
1901
  applyGenerateOptions(body, options);
1847
1902
  if (options.logprobs !== void 0) body.logprobs = options.logprobs;
1848
1903
  if (options.topLogprobs !== void 0) {