@ampt/ai 2.1.0 → 2.2.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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## [2.2.1](https://github.com/getampt/cloud/compare/@ampt/ai-v2.2.0...@ampt/ai-v2.2.1) (2026-11-19)
4
+
5
+ ### Bug Fixes
6
+
7
+ - link the public changelog from every package ([#2947](https://github.com/getampt/cloud/issues/2947)) ([7a4a672](https://github.com/getampt/cloud/commit/7a4a672a4b4bfcb6d99b0eefd8045512044157b6))
8
+
9
+ ## [2.2.0](https://github.com/getampt/cloud/compare/@ampt/ai-v2.1.0...@ampt/ai-v2.2.0) (2026-11-08)
10
+
11
+ ### Features
12
+
13
+ - **packages:** ship security and reliability minors ([#2912](https://github.com/getampt/cloud/issues/2912)) ([7138f91](https://github.com/getampt/cloud/commit/7138f91af8faf8569b3568f757731e95ae21dc9b))
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @ampt/ai
2
+
3
+ AI helpers for Ampt apps: chat, embeddings, summarization, and translation.
4
+
5
+ - Docs: https://getampt.com/docs
6
+ - Changelog: https://ampt.dev/changelog?product=%40ampt%2Fai
7
+ - Questions: https://getampt.com/discord
package/dist/index.cjs CHANGED
@@ -20218,8 +20218,10 @@ var ALLOW = 0;
20218
20218
  var QuotaReader = class {
20219
20219
  url;
20220
20220
  baseKey;
20221
+ invokeQuotasEnabled;
20221
20222
  constructor(params) {
20222
20223
  if (params.url && params.token && params.cacheName) {
20224
+ this.invokeQuotasEnabled = params.invokeQuotasEnabled;
20223
20225
  this.url = `${params.url}/cache/${params.cacheName}?token=${params.token}&key={KEY}`;
20224
20226
  this.baseKey = params.key;
20225
20227
  }
@@ -20251,6 +20253,9 @@ var QuotaReader = class {
20251
20253
  if (!quotaReader) {
20252
20254
  return 0 /* ALLOW */;
20253
20255
  }
20256
+ if (!quotaReader.invokeQuotasEnabled && type === "invocations" /* INVOCATIONS */) {
20257
+ return 0 /* ALLOW */;
20258
+ }
20254
20259
  return await quotaReader.getThrottleState(type);
20255
20260
  }
20256
20261
  };
@@ -20308,7 +20313,7 @@ var AwsClient = class {
20308
20313
  }
20309
20314
  input = url;
20310
20315
  }
20311
- const signer = new AwsV4Signer(Object.assign({ url: input }, init, this, init && init.aws));
20316
+ const signer = new AwsV4Signer(Object.assign({ url: input.toString() }, init, this, init && init.aws));
20312
20317
  const signed = Object.assign({}, init, await signer.sign());
20313
20318
  delete signed.aws;
20314
20319
  try {
@@ -20484,23 +20489,36 @@ async function hmac(key, string) {
20484
20489
  async function hash(content) {
20485
20490
  return crypto.subtle.digest("SHA-256", typeof content === "string" ? encoder.encode(content) : content);
20486
20491
  }
20487
- function buf2hex(buffer) {
20488
- return Array.prototype.map.call(new Uint8Array(buffer), (x8) => ("0" + x8.toString(16)).slice(-2)).join("");
20492
+ var HEX_CHARS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
20493
+ function buf2hex(arrayBuffer) {
20494
+ const buffer = new Uint8Array(arrayBuffer);
20495
+ let out = "";
20496
+ for (let idx = 0; idx < buffer.length; idx++) {
20497
+ const n18 = buffer[idx];
20498
+ out += HEX_CHARS[n18 >>> 4 & 15];
20499
+ out += HEX_CHARS[n18 & 15];
20500
+ }
20501
+ return out;
20489
20502
  }
20490
20503
  function encodeRfc3986(urlEncodedStr) {
20491
20504
  return urlEncodedStr.replace(/[!'()*]/g, (c18) => "%" + c18.charCodeAt(0).toString(16).toUpperCase());
20492
20505
  }
20493
20506
  function guessServiceRegion(url, headers) {
20494
20507
  const { hostname, pathname } = url;
20508
+ if (hostname.endsWith(".on.aws")) {
20509
+ const match2 = hostname.match(/^[^.]{1,63}\.lambda-url\.([^.]{1,63})\.on\.aws$/);
20510
+ return match2 != null ? ["lambda", match2[1] || ""] : ["", ""];
20511
+ }
20495
20512
  if (hostname.endsWith(".r2.cloudflarestorage.com")) {
20496
20513
  return ["s3", "auto"];
20497
20514
  }
20498
20515
  if (hostname.endsWith(".backblazeb2.com")) {
20499
- const match2 = hostname.match(/^(?:[^.]+\.)?s3\.([^.]+)\.backblazeb2\.com$/);
20500
- return match2 != null ? ["s3", match2[1]] : ["", ""];
20516
+ const match2 = hostname.match(/^(?:[^.]{1,63}\.)?s3\.([^.]{1,63})\.backblazeb2\.com$/);
20517
+ return match2 != null ? ["s3", match2[1] || ""] : ["", ""];
20501
20518
  }
20502
- const match = hostname.replace("dualstack.", "").match(/([^.]+)\.(?:([^.]*)\.)?amazonaws\.com(?:\.cn)?$/);
20503
- let [service, region3] = (match || ["", ""]).slice(1, 3);
20519
+ const match = hostname.replace("dualstack.", "").match(/([^.]{1,63})\.(?:([^.]{0,63})\.)?amazonaws\.com(?:\.cn)?$/);
20520
+ let service = match && match[1] || "";
20521
+ let region3 = match && match[2];
20504
20522
  if (region3 === "us-gov") {
20505
20523
  region3 = "us-gov-west-1";
20506
20524
  } else if (region3 === "s3" || region3 === "s3-accelerate") {
@@ -20529,10 +20547,19 @@ function guessServiceRegion(url, headers) {
20529
20547
  } else if (region3 && /-\d$/.test(service) && !/-\d$/.test(region3)) {
20530
20548
  [service, region3] = [region3, service];
20531
20549
  }
20532
- return [HOST_SERVICES[service] || service, region3];
20550
+ return [HOST_SERVICES[service] || service, region3 || ""];
20533
20551
  }
20534
20552
 
20535
20553
  // ../../lib/runtime-logging/index.ts
20554
+ function serializeError(err) {
20555
+ return {
20556
+ message: err.message,
20557
+ stack: err.stack,
20558
+ code: err.code,
20559
+ name: err.name,
20560
+ ...err.cause && { cause: serializeError(err.cause) }
20561
+ };
20562
+ }
20536
20563
  function systemError(errOrMsg, _msg) {
20537
20564
  const err = typeof errOrMsg === "string" ? void 0 : errOrMsg;
20538
20565
  const msg = typeof errOrMsg === "string" ? errOrMsg : _msg;
@@ -20546,7 +20573,23 @@ function systemError(errOrMsg, _msg) {
20546
20573
  msg,
20547
20574
  reqId,
20548
20575
  type,
20549
- ...err && { err: { message: err.message, stack: err.stack, code: err.code, name: err.name } }
20576
+ ...err && { err: serializeError(err) }
20577
+ }) + "\n"
20578
+ );
20579
+ }
20580
+ function applicationWarning(errOrMsg, _msg) {
20581
+ const err = typeof errOrMsg === "string" ? void 0 : errOrMsg;
20582
+ const msg = typeof errOrMsg === "string" ? errOrMsg : _msg;
20583
+ const reqId = getInvocationId();
20584
+ const type = getInvocationType() || "system";
20585
+ process.stdout.write(
20586
+ JSON.stringify({
20587
+ time: Date.now(),
20588
+ level: 40,
20589
+ reqId,
20590
+ type,
20591
+ msg,
20592
+ ...err && { err: serializeError(err) }
20550
20593
  }) + "\n"
20551
20594
  );
20552
20595
  }
@@ -34869,6 +34912,9 @@ var SystemData = class _SystemData {
34869
34912
  };
34870
34913
  var systemData = SystemData.create();
34871
34914
 
34915
+ // ../../lib/ampt-utilities/src/compression.ts
34916
+ var import_node_zlib = require("zlib");
34917
+
34872
34918
  // ../../lib/events-core/parse-after.ts
34873
34919
  var import_dayjs = __toESM(require_dayjs_min(), 1);
34874
34920
  var import_utc = __toESM(require_utc(), 1);
@@ -34893,6 +34939,7 @@ function parseAfter(after, base) {
34893
34939
  try {
34894
34940
  const [num, unit] = after.toLowerCase().split(" ");
34895
34941
  if (!validUnit(unit, ["seconds", "minutes", "hours", "days", "weeks", "months", "years"])) {
34942
+ applicationWarning(`Warning: invalid unit "${unit}" in 'after' config parameter`);
34896
34943
  return NaN;
34897
34944
  }
34898
34945
  return import_dayjs.default.utc(base).add(Number.parseInt(num), unit).valueOf();
@@ -44829,7 +44876,7 @@ function trackImageInference(width, height, steps, modelInfo) {
44829
44876
  function trackEmbeddingsInference(result, modelInfo) {
44830
44877
  telemetry_default.count("inferenceUsage", modelInfo.inferenceUsage(result), "ai");
44831
44878
  }
44832
- async function _invoke(options, lock) {
44879
+ async function _invoke(options, release5) {
44833
44880
  const contentType = options.contentType ?? "application/json";
44834
44881
  const accept = options.accept ?? "application/json";
44835
44882
  const body = contentType === "application/json" ? JSON.stringify(options.body) : options.body;
@@ -44853,19 +44900,41 @@ async function _invoke(options, lock) {
44853
44900
  const iterator = result.body[Symbol.asyncIterator]();
44854
44901
  responseBody = new ReadableStream({
44855
44902
  async pull(controller) {
44856
- const { value, done } = await iterator.next();
44857
- if (done) {
44858
- controller.close();
44859
- await releaseLock(lock);
44860
- } else {
44861
- if (!value.chunk?.bytes) {
44862
- console.warn("Error in LLM response stream:", value);
44863
- return;
44864
- }
44865
- if (modelInfo.type === "language" /* Language */) {
44866
- trackLanguageInference(0, value.chunk.bytes.length, modelInfo);
44903
+ let completed = false;
44904
+ try {
44905
+ const { value, done } = await iterator.next();
44906
+ if (done) {
44907
+ controller.close();
44908
+ completed = true;
44909
+ } else {
44910
+ if (!value.chunk?.bytes) {
44911
+ console.warn("Error in LLM response stream:", value);
44912
+ return;
44913
+ }
44914
+ if (modelInfo.type === "language" /* Language */) {
44915
+ trackLanguageInference(0, value.chunk.bytes.length, modelInfo);
44916
+ }
44917
+ controller.enqueue(value.chunk.bytes);
44867
44918
  }
44868
- controller.enqueue(value.chunk.bytes);
44919
+ } catch (err) {
44920
+ controller.error(err);
44921
+ await release5();
44922
+ return;
44923
+ }
44924
+ if (completed) {
44925
+ await release5();
44926
+ }
44927
+ },
44928
+ async cancel() {
44929
+ const [releaseResult, cleanupResult] = await Promise.allSettled([
44930
+ release5(),
44931
+ Promise.resolve().then(() => iterator.return?.())
44932
+ ]);
44933
+ if (releaseResult.status === "rejected") {
44934
+ throw releaseResult.reason;
44935
+ }
44936
+ if (cleanupResult.status === "rejected") {
44937
+ throw cleanupResult.reason;
44869
44938
  }
44870
44939
  }
44871
44940
  });
@@ -44878,12 +44947,12 @@ async function _invoke(options, lock) {
44878
44947
  trackEmbeddingsInference(result.body, modelInfo);
44879
44948
  }
44880
44949
  responseBody = new ReadableStream({
44881
- async pull(controller) {
44950
+ pull(controller) {
44882
44951
  controller.enqueue(result.body);
44883
44952
  controller.close();
44884
- await releaseLock(lock);
44885
44953
  }
44886
44954
  });
44955
+ await release5();
44887
44956
  } else {
44888
44957
  throw new Error("Model returned an invalid response");
44889
44958
  }
@@ -44897,10 +44966,12 @@ async function invoke2(options) {
44897
44966
  if (!lock.acquired) {
44898
44967
  throw new Error("Max concurrency reached");
44899
44968
  }
44969
+ let releasePromise;
44970
+ const release5 = () => releasePromise ??= releaseLock(lock);
44900
44971
  try {
44901
- return await _invoke(options, lock);
44972
+ return await _invoke(options, release5);
44902
44973
  } catch (err) {
44903
- await releaseLock(lock);
44974
+ await release5();
44904
44975
  throw err;
44905
44976
  }
44906
44977
  }
@@ -55629,7 +55700,7 @@ createAggregatedClient2(commands4, Translate);
55629
55700
  // src/translate.ts
55630
55701
  var region2 = process.env.AWS_REGION ?? "us-east-1";
55631
55702
  var client = new Translate({ credentialDefaultProvider: credentialsProvider, region: region2 });
55632
- async function _translate(input, options, lock) {
55703
+ async function _translate(input, options, release5) {
55633
55704
  const auto = options.sourceLanguage === "auto";
55634
55705
  const result = await client.translateText({
55635
55706
  SourceLanguageCode: options.sourceLanguage,
@@ -55643,12 +55714,11 @@ async function _translate(input, options, lock) {
55643
55714
  const comprehendUsage = auto ? Math.max(3e4, input.length * 100) : 0;
55644
55715
  const translateUsage = input.length * 1500;
55645
55716
  telemetry_default.count("inferenceUsage", comprehendUsage + translateUsage, "ai");
55646
- let responseBody;
55647
- responseBody = new ReadableStream({
55648
- async pull(controller) {
55717
+ await release5();
55718
+ const responseBody = new ReadableStream({
55719
+ pull(controller) {
55649
55720
  controller.enqueue(Buffer.from(translatedText, "utf8"));
55650
55721
  controller.close();
55651
- await releaseLock(lock);
55652
55722
  }
55653
55723
  });
55654
55724
  return new Response(responseBody, {
@@ -55676,10 +55746,12 @@ async function translate(input, options) {
55676
55746
  if (!lock.acquired) {
55677
55747
  throw new Error("Max concurrency reached");
55678
55748
  }
55749
+ let releasePromise;
55750
+ const release5 = () => releasePromise ??= releaseLock(lock);
55679
55751
  try {
55680
- return await _translate(input, options, lock);
55752
+ return await _translate(input, options, release5);
55681
55753
  } catch (err) {
55682
- await releaseLock(lock);
55754
+ await release5();
55683
55755
  throw err;
55684
55756
  }
55685
55757
  }
@@ -55726,6 +55798,6 @@ tslib/tslib.es6.js:
55726
55798
  aws4fetch/dist/aws4fetch.esm.mjs:
55727
55799
  (**
55728
55800
  * @license MIT <https://opensource.org/licenses/MIT>
55729
- * @copyright Michael Hart 2022
55801
+ * @copyright Michael Hart 2024
55730
55802
  *)
55731
55803
  */
package/dist/index.js CHANGED
@@ -7147,8 +7147,10 @@ var ALLOW = 0;
7147
7147
  var QuotaReader = class {
7148
7148
  url;
7149
7149
  baseKey;
7150
+ invokeQuotasEnabled;
7150
7151
  constructor(params) {
7151
7152
  if (params.url && params.token && params.cacheName) {
7153
+ this.invokeQuotasEnabled = params.invokeQuotasEnabled;
7152
7154
  this.url = `${params.url}/cache/${params.cacheName}?token=${params.token}&key={KEY}`;
7153
7155
  this.baseKey = params.key;
7154
7156
  }
@@ -7180,6 +7182,9 @@ var QuotaReader = class {
7180
7182
  if (!quotaReader) {
7181
7183
  return 0 /* ALLOW */;
7182
7184
  }
7185
+ if (!quotaReader.invokeQuotasEnabled && type === "invocations" /* INVOCATIONS */) {
7186
+ return 0 /* ALLOW */;
7187
+ }
7183
7188
  return await quotaReader.getThrottleState(type);
7184
7189
  }
7185
7190
  };
@@ -7237,7 +7242,7 @@ var AwsClient = class {
7237
7242
  }
7238
7243
  input = url;
7239
7244
  }
7240
- const signer = new AwsV4Signer(Object.assign({ url: input }, init, this, init && init.aws));
7245
+ const signer = new AwsV4Signer(Object.assign({ url: input.toString() }, init, this, init && init.aws));
7241
7246
  const signed = Object.assign({}, init, await signer.sign());
7242
7247
  delete signed.aws;
7243
7248
  try {
@@ -7413,23 +7418,36 @@ async function hmac(key, string) {
7413
7418
  async function hash(content) {
7414
7419
  return crypto.subtle.digest("SHA-256", typeof content === "string" ? encoder.encode(content) : content);
7415
7420
  }
7416
- function buf2hex(buffer) {
7417
- return Array.prototype.map.call(new Uint8Array(buffer), (x7) => ("0" + x7.toString(16)).slice(-2)).join("");
7421
+ var HEX_CHARS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
7422
+ function buf2hex(arrayBuffer) {
7423
+ const buffer = new Uint8Array(arrayBuffer);
7424
+ let out = "";
7425
+ for (let idx = 0; idx < buffer.length; idx++) {
7426
+ const n14 = buffer[idx];
7427
+ out += HEX_CHARS[n14 >>> 4 & 15];
7428
+ out += HEX_CHARS[n14 & 15];
7429
+ }
7430
+ return out;
7418
7431
  }
7419
7432
  function encodeRfc3986(urlEncodedStr) {
7420
7433
  return urlEncodedStr.replace(/[!'()*]/g, (c14) => "%" + c14.charCodeAt(0).toString(16).toUpperCase());
7421
7434
  }
7422
7435
  function guessServiceRegion(url, headers) {
7423
7436
  const { hostname, pathname } = url;
7437
+ if (hostname.endsWith(".on.aws")) {
7438
+ const match2 = hostname.match(/^[^.]{1,63}\.lambda-url\.([^.]{1,63})\.on\.aws$/);
7439
+ return match2 != null ? ["lambda", match2[1] || ""] : ["", ""];
7440
+ }
7424
7441
  if (hostname.endsWith(".r2.cloudflarestorage.com")) {
7425
7442
  return ["s3", "auto"];
7426
7443
  }
7427
7444
  if (hostname.endsWith(".backblazeb2.com")) {
7428
- const match2 = hostname.match(/^(?:[^.]+\.)?s3\.([^.]+)\.backblazeb2\.com$/);
7429
- return match2 != null ? ["s3", match2[1]] : ["", ""];
7445
+ const match2 = hostname.match(/^(?:[^.]{1,63}\.)?s3\.([^.]{1,63})\.backblazeb2\.com$/);
7446
+ return match2 != null ? ["s3", match2[1] || ""] : ["", ""];
7430
7447
  }
7431
- const match = hostname.replace("dualstack.", "").match(/([^.]+)\.(?:([^.]*)\.)?amazonaws\.com(?:\.cn)?$/);
7432
- let [service, region3] = (match || ["", ""]).slice(1, 3);
7448
+ const match = hostname.replace("dualstack.", "").match(/([^.]{1,63})\.(?:([^.]{0,63})\.)?amazonaws\.com(?:\.cn)?$/);
7449
+ let service = match && match[1] || "";
7450
+ let region3 = match && match[2];
7433
7451
  if (region3 === "us-gov") {
7434
7452
  region3 = "us-gov-west-1";
7435
7453
  } else if (region3 === "s3" || region3 === "s3-accelerate") {
@@ -7458,10 +7476,19 @@ function guessServiceRegion(url, headers) {
7458
7476
  } else if (region3 && /-\d$/.test(service) && !/-\d$/.test(region3)) {
7459
7477
  [service, region3] = [region3, service];
7460
7478
  }
7461
- return [HOST_SERVICES[service] || service, region3];
7479
+ return [HOST_SERVICES[service] || service, region3 || ""];
7462
7480
  }
7463
7481
 
7464
7482
  // ../../lib/runtime-logging/index.ts
7483
+ function serializeError(err) {
7484
+ return {
7485
+ message: err.message,
7486
+ stack: err.stack,
7487
+ code: err.code,
7488
+ name: err.name,
7489
+ ...err.cause && { cause: serializeError(err.cause) }
7490
+ };
7491
+ }
7465
7492
  function systemError(errOrMsg, _msg) {
7466
7493
  const err = typeof errOrMsg === "string" ? void 0 : errOrMsg;
7467
7494
  const msg = typeof errOrMsg === "string" ? errOrMsg : _msg;
@@ -7475,7 +7502,23 @@ function systemError(errOrMsg, _msg) {
7475
7502
  msg,
7476
7503
  reqId,
7477
7504
  type,
7478
- ...err && { err: { message: err.message, stack: err.stack, code: err.code, name: err.name } }
7505
+ ...err && { err: serializeError(err) }
7506
+ }) + "\n"
7507
+ );
7508
+ }
7509
+ function applicationWarning(errOrMsg, _msg) {
7510
+ const err = typeof errOrMsg === "string" ? void 0 : errOrMsg;
7511
+ const msg = typeof errOrMsg === "string" ? errOrMsg : _msg;
7512
+ const reqId = getInvocationId();
7513
+ const type = getInvocationType() || "system";
7514
+ process.stdout.write(
7515
+ JSON.stringify({
7516
+ time: Date.now(),
7517
+ level: 40,
7518
+ reqId,
7519
+ type,
7520
+ msg,
7521
+ ...err && { err: serializeError(err) }
7479
7522
  }) + "\n"
7480
7523
  );
7481
7524
  }
@@ -21712,6 +21755,7 @@ function parseAfter(after, base) {
21712
21755
  try {
21713
21756
  const [num, unit] = after.toLowerCase().split(" ");
21714
21757
  if (!validUnit(unit, ["seconds", "minutes", "hours", "days", "weeks", "months", "years"])) {
21758
+ applicationWarning(`Warning: invalid unit "${unit}" in 'after' config parameter`);
21715
21759
  return NaN;
21716
21760
  }
21717
21761
  return import_dayjs.default.utc(base).add(Number.parseInt(num), unit).valueOf();
@@ -31647,7 +31691,7 @@ function trackImageInference(width, height, steps, modelInfo) {
31647
31691
  function trackEmbeddingsInference(result, modelInfo) {
31648
31692
  telemetry_default.count("inferenceUsage", modelInfo.inferenceUsage(result), "ai");
31649
31693
  }
31650
- async function _invoke(options, lock) {
31694
+ async function _invoke(options, release4) {
31651
31695
  const contentType = options.contentType ?? "application/json";
31652
31696
  const accept = options.accept ?? "application/json";
31653
31697
  const body = contentType === "application/json" ? JSON.stringify(options.body) : options.body;
@@ -31671,19 +31715,41 @@ async function _invoke(options, lock) {
31671
31715
  const iterator = result.body[Symbol.asyncIterator]();
31672
31716
  responseBody = new ReadableStream({
31673
31717
  async pull(controller) {
31674
- const { value, done } = await iterator.next();
31675
- if (done) {
31676
- controller.close();
31677
- await releaseLock(lock);
31678
- } else {
31679
- if (!value.chunk?.bytes) {
31680
- console.warn("Error in LLM response stream:", value);
31681
- return;
31682
- }
31683
- if (modelInfo.type === "language" /* Language */) {
31684
- trackLanguageInference(0, value.chunk.bytes.length, modelInfo);
31718
+ let completed = false;
31719
+ try {
31720
+ const { value, done } = await iterator.next();
31721
+ if (done) {
31722
+ controller.close();
31723
+ completed = true;
31724
+ } else {
31725
+ if (!value.chunk?.bytes) {
31726
+ console.warn("Error in LLM response stream:", value);
31727
+ return;
31728
+ }
31729
+ if (modelInfo.type === "language" /* Language */) {
31730
+ trackLanguageInference(0, value.chunk.bytes.length, modelInfo);
31731
+ }
31732
+ controller.enqueue(value.chunk.bytes);
31685
31733
  }
31686
- controller.enqueue(value.chunk.bytes);
31734
+ } catch (err) {
31735
+ controller.error(err);
31736
+ await release4();
31737
+ return;
31738
+ }
31739
+ if (completed) {
31740
+ await release4();
31741
+ }
31742
+ },
31743
+ async cancel() {
31744
+ const [releaseResult, cleanupResult] = await Promise.allSettled([
31745
+ release4(),
31746
+ Promise.resolve().then(() => iterator.return?.())
31747
+ ]);
31748
+ if (releaseResult.status === "rejected") {
31749
+ throw releaseResult.reason;
31750
+ }
31751
+ if (cleanupResult.status === "rejected") {
31752
+ throw cleanupResult.reason;
31687
31753
  }
31688
31754
  }
31689
31755
  });
@@ -31696,12 +31762,12 @@ async function _invoke(options, lock) {
31696
31762
  trackEmbeddingsInference(result.body, modelInfo);
31697
31763
  }
31698
31764
  responseBody = new ReadableStream({
31699
- async pull(controller) {
31765
+ pull(controller) {
31700
31766
  controller.enqueue(result.body);
31701
31767
  controller.close();
31702
- await releaseLock(lock);
31703
31768
  }
31704
31769
  });
31770
+ await release4();
31705
31771
  } else {
31706
31772
  throw new Error("Model returned an invalid response");
31707
31773
  }
@@ -31715,10 +31781,12 @@ async function invoke2(options) {
31715
31781
  if (!lock.acquired) {
31716
31782
  throw new Error("Max concurrency reached");
31717
31783
  }
31784
+ let releasePromise;
31785
+ const release4 = () => releasePromise ??= releaseLock(lock);
31718
31786
  try {
31719
- return await _invoke(options, lock);
31787
+ return await _invoke(options, release4);
31720
31788
  } catch (err) {
31721
- await releaseLock(lock);
31789
+ await release4();
31722
31790
  throw err;
31723
31791
  }
31724
31792
  }
@@ -42443,7 +42511,7 @@ createAggregatedClient2(commands4, Translate);
42443
42511
  // src/translate.ts
42444
42512
  var region2 = process.env.AWS_REGION ?? "us-east-1";
42445
42513
  var client = new Translate({ credentialDefaultProvider: credentialsProvider, region: region2 });
42446
- async function _translate(input, options, lock) {
42514
+ async function _translate(input, options, release4) {
42447
42515
  const auto = options.sourceLanguage === "auto";
42448
42516
  const result = await client.translateText({
42449
42517
  SourceLanguageCode: options.sourceLanguage,
@@ -42457,12 +42525,11 @@ async function _translate(input, options, lock) {
42457
42525
  const comprehendUsage = auto ? Math.max(3e4, input.length * 100) : 0;
42458
42526
  const translateUsage = input.length * 1500;
42459
42527
  telemetry_default.count("inferenceUsage", comprehendUsage + translateUsage, "ai");
42460
- let responseBody;
42461
- responseBody = new ReadableStream({
42462
- async pull(controller) {
42528
+ await release4();
42529
+ const responseBody = new ReadableStream({
42530
+ pull(controller) {
42463
42531
  controller.enqueue(Buffer.from(translatedText, "utf8"));
42464
42532
  controller.close();
42465
- await releaseLock(lock);
42466
42533
  }
42467
42534
  });
42468
42535
  return new Response(responseBody, {
@@ -42490,10 +42557,12 @@ async function translate(input, options) {
42490
42557
  if (!lock.acquired) {
42491
42558
  throw new Error("Max concurrency reached");
42492
42559
  }
42560
+ let releasePromise;
42561
+ const release4 = () => releasePromise ??= releaseLock(lock);
42493
42562
  try {
42494
- return await _translate(input, options, lock);
42563
+ return await _translate(input, options, release4);
42495
42564
  } catch (err) {
42496
- await releaseLock(lock);
42565
+ await release4();
42497
42566
  throw err;
42498
42567
  }
42499
42568
  }
@@ -42539,6 +42608,6 @@ tslib/tslib.es6.js:
42539
42608
  aws4fetch/dist/aws4fetch.esm.mjs:
42540
42609
  (**
42541
42610
  * @license MIT <https://opensource.org/licenses/MIT>
42542
- * @copyright Michael Hart 2022
42611
+ * @copyright Michael Hart 2024
42543
42612
  *)
42544
42613
  */
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@ampt/ai",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
+ "homepage": "https://ampt.dev/changelog?product=%40ampt%2Fai",
4
5
  "type": "module",
5
6
  "main": "./dist/index.cjs",
6
7
  "module": "./dist/index.js",
7
8
  "types": "./dist/index.d.ts",
8
9
  "scripts": {
9
10
  "prepublishOnly": "npm run build",
10
- "build": "tsup"
11
+ "build": "tsup",
12
+ "test": "vitest run",
13
+ "typecheck": "tsc -b ."
11
14
  },
12
15
  "exports": {
13
16
  ".": {