@cortexkit/pi-claustrum-auth 0.4.1 → 0.4.2

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.
Files changed (2) hide show
  1. package/dist/index.js +76 -32
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -622,7 +622,42 @@ function hasBillingAccessor() {
622
622
  }
623
623
  }
624
624
 
625
- class BillingAttempt {
625
+ class ResponseCompletion {
626
+ completed = false;
627
+ failed = false;
628
+ event(value) {
629
+ if (!object(value))
630
+ return;
631
+ if (value.type === "error" || value.type === "response.failed" || value.error)
632
+ this.failed = true;
633
+ if (value.type === "response.completed" || value.type === "response.incomplete")
634
+ this.completed = true;
635
+ if (Array.isArray(value.choices)) {
636
+ for (const choice of value.choices) {
637
+ if (!object(choice))
638
+ continue;
639
+ if (choice.finish_reason != null || object(choice.message))
640
+ this.completed = true;
641
+ }
642
+ }
643
+ }
644
+ bodyEnd(ended, status) {
645
+ return !this.failed && this.completed && ended && status >= 200 && status < 300;
646
+ }
647
+ cancel(status) {
648
+ return this.bodyEnd(this.completed, status);
649
+ }
650
+ json(value) {
651
+ this.event(value);
652
+ if (object(value) && !value.error)
653
+ this.completed = true;
654
+ }
655
+ done() {
656
+ this.completed = true;
657
+ }
658
+ }
659
+
660
+ class BillingAttempt extends ResponseCompletion {
626
661
  entry;
627
662
  provider;
628
663
  sessionId;
@@ -633,8 +668,6 @@ class BillingAttempt {
633
668
  pi;
634
669
  attemptId = randomUUID();
635
670
  reported = false;
636
- completed = false;
637
- failed = false;
638
671
  outputStarted = false;
639
672
  responseId;
640
673
  rawUsage;
@@ -642,6 +675,7 @@ class BillingAttempt {
642
675
  status;
643
676
  ended = false;
644
677
  constructor(entry, provider, sessionId, requestKind, model, receipt, attempt, pi = false) {
678
+ super();
645
679
  this.entry = entry;
646
680
  this.provider = provider;
647
681
  this.sessionId = sessionId;
@@ -652,24 +686,17 @@ class BillingAttempt {
652
686
  this.pi = pi;
653
687
  }
654
688
  event(value) {
689
+ super.event(value);
655
690
  if (!object(value))
656
691
  return;
657
- if (value.type === "error" || value.type === "response.failed" || value.error)
658
- this.failed = true;
659
- if (value.type === "response.completed" || value.type === "response.incomplete")
660
- this.completed = true;
661
692
  if (typeof value.type === "string" && value.type.endsWith(".delta"))
662
693
  this.outputStarted = true;
663
694
  if (Array.isArray(value.choices)) {
664
695
  for (const choice of value.choices) {
665
696
  if (!object(choice))
666
697
  continue;
667
- if (choice.finish_reason != null)
668
- this.completed = true;
669
698
  if (object(choice.delta) && Object.keys(choice.delta).some((key) => key !== "role"))
670
699
  this.outputStarted = true;
671
- if (object(choice.message))
672
- this.completed = true;
673
700
  }
674
701
  }
675
702
  const response = object(value.response) ? value.response : value;
@@ -687,17 +714,7 @@ class BillingAttempt {
687
714
  this.ended = ended;
688
715
  if (!this.pi || status < 200 || status >= 300)
689
716
  this.end();
690
- }
691
- cancel(status) {
692
- this.bodyEnd(this.completed, status);
693
- }
694
- json(value) {
695
- this.event(value);
696
- if (object(value) && !value.error)
697
- this.completed = true;
698
- }
699
- done() {
700
- this.completed = true;
717
+ return super.bodyEnd(ended, status);
701
718
  }
702
719
  piEnd(responseId, stopReason) {
703
720
  this.responseId = responseId;
@@ -742,11 +759,21 @@ class BillingAttempt {
742
759
  });
743
760
  }
744
761
  }
745
- function observeBillingResponse(record, response) {
746
- if (!record)
762
+ function observeBillingResponse(record, response, onCompleted) {
763
+ if (!record && !onCompleted)
747
764
  return response;
765
+ const observer = record ?? new ResponseCompletion;
766
+ let notified = false;
767
+ const finish = (completed) => {
768
+ if (!completed || notified || !onCompleted)
769
+ return;
770
+ notified = true;
771
+ try {
772
+ onCompleted();
773
+ } catch {}
774
+ };
748
775
  if (!response.body) {
749
- record.bodyEnd(true, response.status);
776
+ finish(observer.bodyEnd(true, response.status));
750
777
  return response;
751
778
  }
752
779
  const sse = /^text\/event-stream(?:\s*;|$)/i.test(response.headers.get("content-type") ?? "");
@@ -755,10 +782,10 @@ function observeBillingResponse(record, response) {
755
782
  let oversized = false;
756
783
  const body = sse ? response.body.pipeThrough(watchServerSentEvents((event) => {
757
784
  if (event.data === "[DONE]")
758
- record.done();
785
+ observer.done();
759
786
  else {
760
787
  try {
761
- record.event(JSON.parse(event.data));
788
+ observer.event(JSON.parse(event.data));
762
789
  } catch {}
763
790
  }
764
791
  }, () => {})) : response.body;
@@ -770,10 +797,10 @@ function observeBillingResponse(record, response) {
770
797
  if (next.done) {
771
798
  if (!sse && !oversized) {
772
799
  try {
773
- record.json(JSON.parse(json + decoder.decode()));
800
+ observer.json(JSON.parse(json + decoder.decode()));
774
801
  } catch {}
775
802
  }
776
- record.bodyEnd(true, response.status);
803
+ finish(observer.bodyEnd(true, response.status));
777
804
  controller.close();
778
805
  } else {
779
806
  if (!sse && !oversized) {
@@ -786,12 +813,12 @@ function observeBillingResponse(record, response) {
786
813
  controller.enqueue(next.value);
787
814
  }
788
815
  } catch (error) {
789
- record.bodyEnd(false, response.status);
816
+ observer.bodyEnd(false, response.status);
790
817
  controller.error(error);
791
818
  }
792
819
  },
793
820
  cancel(reason) {
794
- record.cancel(response.status);
821
+ finish(observer.cancel(response.status));
795
822
  return reader.cancel(reason);
796
823
  }
797
824
  }, { highWaterMark: 0 }), {
@@ -6760,6 +6787,23 @@ async function reportRoutedBalance(key, sessionId, service, status, body) {
6760
6787
  providerCode
6761
6788
  });
6762
6789
  }
6790
+ function routedCompletion(key, sessionId, response) {
6791
+ if (!key || !sessionId || !response.ok)
6792
+ return;
6793
+ const headers = {};
6794
+ response.headers.forEach((value, name) => {
6795
+ const lower = name.toLowerCase();
6796
+ if (lower.startsWith("x-ratelimit-") || lower.startsWith("ratelimit") || lower.startsWith("anthropic-ratelimit-") || lower === "retry-after")
6797
+ headers[lower] = value;
6798
+ });
6799
+ return () => reportRouting({
6800
+ sessionId,
6801
+ accountId: key.accountId,
6802
+ credentialId: key.credentialId,
6803
+ outcome: "served",
6804
+ headers
6805
+ });
6806
+ }
6763
6807
  // ../../node_modules/.bun/@cortexkit+common-auth@0.47.0+d48131f39628582b/node_modules/@cortexkit/common-auth/dist/routing/sticky.js
6764
6808
  var QUOTA_STALENESS_MS = 15 * 60000;
6765
6809
  var MIN_RESET_HOURS = 1 / 60;
@@ -6972,7 +7016,7 @@ function createFetch(options) {
6972
7016
  return reply;
6973
7017
  }
6974
7018
  billing = options.billing?.(receipt);
6975
- return observeBillingResponse(billing, reply);
7019
+ return observeBillingResponse(billing, reply, routedCompletion(routed, options.routing?.sessionId, reply));
6976
7020
  } catch (error) {
6977
7021
  const failedBilling = billing ?? options.billing?.(receipt);
6978
7022
  failedBilling?.end(false, false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-claustrum-auth",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Claustrum vault credentials for Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",