@forklaunch/core 3.0.1 → 3.1.0

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.
@@ -2627,6 +2627,92 @@ async function* sseData(stream) {
2627
2627
  }
2628
2628
  }
2629
2629
 
2630
+ // src/http/previewModelGatewayClient.ts
2631
+ function record(value) {
2632
+ return !!value && typeof value === "object" && !Array.isArray(value);
2633
+ }
2634
+ function invalidCatalog() {
2635
+ throw new ModelGatewayRequestError(
2636
+ 502,
2637
+ "The local preview model catalog is invalid or expired."
2638
+ );
2639
+ }
2640
+ function previewCatalog(raw) {
2641
+ if (!record(raw) || !record(raw.preview)) return invalidCatalog();
2642
+ const preview = raw.preview;
2643
+ if (Object.keys(raw).some((key) => !["models", "preview"].includes(key)) || Object.keys(preview).some(
2644
+ (key) => !["expiresAt", "maxCalls", "maxUnits"].includes(key)
2645
+ ) || !Array.isArray(raw.models) || raw.models.length !== 1 || !record(raw.models[0]) || Object.keys(raw.models[0]).some((key) => key !== "id") || typeof raw.models[0].id !== "string" || !/^[\w.:-]{1,160}$/.test(raw.models[0].id) || typeof preview.expiresAt !== "number" || !Number.isSafeInteger(preview.expiresAt) || preview.expiresAt <= Date.now() || preview.expiresAt > Date.now() + 15 * 6e4 || typeof preview.maxCalls !== "number" || !Number.isSafeInteger(preview.maxCalls) || preview.maxCalls < 1 || preview.maxCalls > 20 || typeof preview.maxUnits !== "number" || !Number.isSafeInteger(preview.maxUnits) || preview.maxUnits < 1 || preview.maxUnits > 1e5)
2646
+ return invalidCatalog();
2647
+ return {
2648
+ models: [{ id: raw.models[0].id }],
2649
+ preview: {
2650
+ expiresAt: preview.expiresAt,
2651
+ maxCalls: preview.maxCalls,
2652
+ maxUnits: preview.maxUnits
2653
+ }
2654
+ };
2655
+ }
2656
+ async function readPreviewCatalog(response) {
2657
+ if (!response.body) return invalidCatalog();
2658
+ const reader = response.body.getReader();
2659
+ const decoder = new TextDecoder();
2660
+ let text = "", bytes = 0;
2661
+ try {
2662
+ for (; ; ) {
2663
+ const { done, value } = await reader.read();
2664
+ if (done) break;
2665
+ bytes += value.byteLength;
2666
+ if (bytes > 8192) return invalidCatalog();
2667
+ text += decoder.decode(value, { stream: true });
2668
+ }
2669
+ text += decoder.decode();
2670
+ return previewCatalog(JSON.parse(text));
2671
+ } catch {
2672
+ return invalidCatalog();
2673
+ } finally {
2674
+ await reader.cancel().catch(() => {
2675
+ });
2676
+ reader.releaseLock();
2677
+ }
2678
+ }
2679
+ function createPreviewModelGatewayClient(options2 = {}) {
2680
+ const transport = createInstanceGatewayTransport(
2681
+ options2,
2682
+ "The local preview model gateway"
2683
+ );
2684
+ async function send(method, route, body) {
2685
+ try {
2686
+ return await transport.request(method, route, body);
2687
+ } catch (error) {
2688
+ if (error instanceof InstanceGatewayRequestError)
2689
+ throw new ModelGatewayRequestError(
2690
+ error.status,
2691
+ "The local preview model request was refused or is unavailable.",
2692
+ error.retryAfterSeconds
2693
+ );
2694
+ throw error;
2695
+ }
2696
+ }
2697
+ return {
2698
+ async models() {
2699
+ const response = await send("GET", "/models");
2700
+ return readPreviewCatalog(response);
2701
+ },
2702
+ chat: {
2703
+ completions: {
2704
+ async create(request) {
2705
+ return await (await send(
2706
+ "POST",
2707
+ "/models/chat/completions",
2708
+ request
2709
+ )).json();
2710
+ }
2711
+ }
2712
+ }
2713
+ };
2714
+ }
2715
+
2630
2716
  // src/http/emailClient.ts
2631
2717
  var EmailRequestError = class extends InstanceGatewayRequestError {
2632
2718
  name = "EmailRequestError";
@@ -2712,7 +2798,7 @@ function createEmailClient(options2 = {}) {
2712
2798
  INSTANCE_HMAC_KEY: options2.hmacKey ?? process.env.INSTANCE_HMAC_KEY
2713
2799
  })) {
2714
2800
  throw new Error(
2715
- "Email is sent by the ForkLaunch platform and is only available to managed instances: PLATFORM_GATEWAY_URL, INSTANCE_ID and INSTANCE_HMAC_KEY must be set. Locally, `forklaunch infra add <service> email` sets them to the gateway mock in docker-compose."
2801
+ "Email is sent by the ForkLaunch platform: PLATFORM_GATEWAY_URL, INSTANCE_ID and INSTANCE_HMAC_KEY must be set. A managed instance and an app deployed with `forklaunch deploy create` get them from the platform at deploy. Locally, `forklaunch infra add <service> email` sets them to the gateway mock in docker-compose."
2716
2802
  );
2717
2803
  }
2718
2804
  const transport = createInstanceGatewayTransport(options2, "Email");
@@ -3139,6 +3225,9 @@ function createStripeClient(options2 = {}) {
3139
3225
  const explicitGateway = Boolean(
3140
3226
  options2.gatewayUrl || options2.instanceId || options2.hmacKey
3141
3227
  );
3228
+ if (!explicitGateway && options2.apiKey) {
3229
+ return new StripeClass(options2.apiKey, options2.config);
3230
+ }
3142
3231
  if (!explicitGateway && !isManagedInstance()) {
3143
3232
  if (!options2.apiKey) {
3144
3233
  throw new Error(
@@ -5227,8 +5316,8 @@ var RateLimiter = class {
5227
5316
  const now = Date.now();
5228
5317
  let counter;
5229
5318
  try {
5230
- const record = await this.cache.readRecord(key);
5231
- counter = record.value;
5319
+ const record2 = await this.cache.readRecord(key);
5320
+ counter = record2.value;
5232
5321
  if (now >= counter.resetAt) {
5233
5322
  counter = { count: 0, resetAt: now + windowMs };
5234
5323
  }
@@ -5311,6 +5400,7 @@ export {
5311
5400
  createInstanceGatewayTransport,
5312
5401
  createModelGatewayClient,
5313
5402
  createPaymentsClient,
5403
+ createPreviewModelGatewayClient,
5314
5404
  createSmsClient,
5315
5405
  createStripeClient,
5316
5406
  createVoiceClient,