segmind 0.4.0 → 0.5.1

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/README.md CHANGED
@@ -135,7 +135,8 @@ const videoModels = await segmind.models.listAll({ output: 'video', sort: 'cheap
135
135
  ## Generations
136
136
 
137
137
  ```ts
138
- const page = await segmind.generations.list({ modelName: 'flux-schnell' });
138
+ const page = await segmind.generations.list({ modelName: 'flux-schnell', perPage: 50 });
139
+ const theirs = await segmind.generations.list({ userEmail: 'teammate@example.com' }); // inside a team
139
140
  const history = await segmind.generations.history({ status: 'FAILED', perPage: 50 }); // failures included
140
141
  const fromKey = await segmind.generations.history({ keySuffix: 'a1b2c3', initiator: ['API', 'API_V2'] });
141
142
  const one = await segmind.generations.get(requestId);
@@ -144,6 +145,8 @@ const latest = await segmind.generations.recent('flux-schnell');
144
145
 
145
146
  `history` lists one row per output, each with the request's full cost: de-duplicate on `request_id` before summing `credits_deduction`. Each row carries its `initiator` and the `key_mask` of the key that made it. Filter by key with `keySuffix`, the last 6 characters of the key: the full key is never sent.
146
147
 
148
+ Inside a team, `list` and `history` narrow to one member with `userId` or `userEmail`, one or the other; outside a team only your own id or email is accepted.
149
+
147
150
  ## Webhooks
148
151
 
149
152
  ```ts
package/dist/index.cjs CHANGED
@@ -512,22 +512,22 @@ function mimeFromName(name) {
512
512
  const extension = name.includes(".") ? name.split(".").pop().toLowerCase() : "";
513
513
  return MIME_BY_EXTENSION[extension];
514
514
  }
515
- async function readFileInput(input, options) {
516
- let bytes;
515
+ async function openFileInput(input, options) {
516
+ let body;
517
517
  let name = options.filename;
518
518
  let mimeType = options.mimeType;
519
519
  if (typeof input === "string") {
520
520
  if (detectRuntime() !== "node") {
521
521
  throw new SegmindError(`${options.caller}: file paths work in Node only; pass a Blob or bytes`);
522
522
  }
523
- const { readFile } = await import(
523
+ const { openAsBlob } = await import(
524
524
  /* webpackIgnore: true */
525
525
  /* turbopackIgnore: true */
526
526
  /* @vite-ignore */
527
- "node:fs/promises"
527
+ "node:fs"
528
528
  );
529
529
  try {
530
- bytes = new Uint8Array(await readFile(input));
530
+ body = await openAsBlob(input);
531
531
  } catch (err) {
532
532
  throw new SegmindError(`${options.caller}: cannot read ${JSON.stringify(input)}: ${err.message}`, {
533
533
  cause: err
@@ -535,13 +535,13 @@ async function readFileInput(input, options) {
535
535
  }
536
536
  name ??= input.split(/[\\/]/).pop();
537
537
  } else if (typeof Blob !== "undefined" && input instanceof Blob) {
538
- bytes = new Uint8Array(await input.arrayBuffer());
538
+ body = input;
539
539
  name ??= input.name || void 0;
540
540
  mimeType ??= input.type || void 0;
541
541
  } else {
542
- bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
542
+ body = input instanceof Uint8Array ? input : new Uint8Array(input);
543
543
  }
544
- mimeType ??= name && mimeFromName(name) || sniff(bytes);
544
+ mimeType ??= name && mimeFromName(name) || sniff(await head(body));
545
545
  if (!name || !name.includes(".")) {
546
546
  const extension = mimeType ? EXTENSION_BY_MIME[mimeType] : void 0;
547
547
  if (!extension) {
@@ -549,7 +549,17 @@ async function readFileInput(input, options) {
549
549
  }
550
550
  name = `${name || "upload"}.${extension}`;
551
551
  }
552
- return { bytes, filename: name, mimeType };
552
+ const size = body instanceof Uint8Array ? body.byteLength : body.size;
553
+ return { body, size, filename: name, mimeType };
554
+ }
555
+ async function readFileInput(input, options) {
556
+ const { body, filename, mimeType } = await openFileInput(input, options);
557
+ const bytes = body instanceof Uint8Array ? body : new Uint8Array(await body.arrayBuffer());
558
+ return { bytes, filename, mimeType };
559
+ }
560
+ async function head(body) {
561
+ if (body instanceof Uint8Array) return body;
562
+ return new Uint8Array(await body.slice(0, 16).arrayBuffer());
553
563
  }
554
564
  function sniff(bytes) {
555
565
  const at = (offset, signature) => signature.every((b, i) => bytes[offset + i] === b);
@@ -579,16 +589,16 @@ var Files = class {
579
589
  return this.uploadOne(input, options);
580
590
  }
581
591
  async uploadOne(input, options) {
582
- const file = await readFileInput(input, { ...options, caller: "files.upload" });
592
+ const file = await openFileInput(input, { ...options, caller: "files.upload" });
583
593
  const url = options.persist ? await this.persist(file, options.signal) : await this.stage(file, options.signal);
584
- return { url, filename: file.filename, size: file.bytes.byteLength, persistent: options.persist === true };
594
+ return { url, filename: file.filename, size: file.size, persistent: options.persist === true };
585
595
  }
586
596
  /** A temporary model input on the inference API's input bucket. */
587
597
  async stage(file, signal) {
588
598
  const ticket = await this.client.http.json({
589
599
  method: "POST",
590
600
  url: `${this.client.spotURL}/inference-request/input-presign`,
591
- body: { filename: file.filename, size: file.bytes.byteLength },
601
+ body: { filename: file.filename, size: file.size },
592
602
  signal,
593
603
  retry: true
594
604
  });
@@ -611,7 +621,7 @@ var Files = class {
611
621
  const ticket = await http.json({
612
622
  method: "POST",
613
623
  url: `${assetsURL}/upload-asset/presign`,
614
- body: { filename: file.filename, contentType, size: file.bytes.byteLength },
624
+ body: { filename: file.filename, contentType, size: file.size },
615
625
  signal,
616
626
  retry: true
617
627
  });
@@ -630,9 +640,12 @@ var Files = class {
630
640
  });
631
641
  return confirmed.asset?.url ?? ticket.publicUrl;
632
642
  }
633
- /** A PUT to the same key is idempotent, so it retries. `bare` keeps the API key off the storage host. */
643
+ /**
644
+ * A PUT to the same key is idempotent, so it retries; a `Blob` body can be
645
+ * sent again, unlike a stream. `bare` keeps the API key off the storage host.
646
+ */
634
647
  async put(url, file, headers, signal) {
635
- await this.client.http.request({ method: "PUT", url, rawBody: file.bytes, headers, bare: true, signal, retry: true });
648
+ await this.client.http.request({ method: "PUT", url, rawBody: file.body, headers, bare: true, signal, retry: true });
636
649
  }
637
650
  };
638
651
 
@@ -646,10 +659,12 @@ var Generations = class {
646
659
  async list(options = {}) {
647
660
  return this.read("/inference-request/generations", options.signal, {
648
661
  page: options.page ?? 1,
662
+ per_page: options.perPage,
649
663
  model_name: options.modelName,
650
664
  start_date: options.startDate,
651
665
  end_date: options.endDate,
652
- user_id: options.userId
666
+ user_id: options.userId,
667
+ user_email: options.userEmail
653
668
  });
654
669
  }
655
670
  /** The newest generations for one model. */
@@ -875,7 +890,7 @@ function failureDetail(body) {
875
890
  }
876
891
 
877
892
  // src/version.ts
878
- var VERSION = "0.4.0";
893
+ var VERSION = "0.5.1";
879
894
 
880
895
  // src/webhooks.ts
881
896
  var Webhooks = class {