@ai-matrx/media 0.15.48 → 0.15.49

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/speech.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { ProviderSessionFailure, ProviderSessionFailureVerdict } from '@ai-matrx/agents/matrx';
1
+ import { ProviderSessionFailure, ProviderSessionFailureVerdict, ProviderSessionUsage } from '@ai-matrx/agents/matrx';
2
2
  import { b as SpeechPronunciation, V as VoicePurpose } from './tts-config-JbF86KIt.cjs';
3
3
  import { GenerationConfig } from '@cartesia/cartesia-js/resources/tts';
4
4
  import { SupportedLanguage } from '@cartesia/cartesia-js/resources/voices';
@@ -124,6 +124,11 @@ interface SpeechPorts {
124
124
  * person's sentence and whether retrying can help.
125
125
  */
126
126
  reportProviderFailure?: (failure: ProviderSessionFailure) => Promise<ProviderSessionFailureVerdict | null>;
127
+ /**
128
+ * Report what a browser-held provider session spent (`POST /broker/usage`, once per `usage_id`).
129
+ * The host owns retry and delivery; it must never throw.
130
+ */
131
+ reportProviderUsage?: (usage: ProviderSessionUsage) => void;
127
132
  /** Tell the person speech stopped (a toast on the web). Without it the error is only on the item. */
128
133
  notifyError?: (title: string, description: string) => void;
129
134
  /**
package/dist/speech.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ProviderSessionFailure, ProviderSessionFailureVerdict } from '@ai-matrx/agents/matrx';
1
+ import { ProviderSessionFailure, ProviderSessionFailureVerdict, ProviderSessionUsage } from '@ai-matrx/agents/matrx';
2
2
  import { b as SpeechPronunciation, V as VoicePurpose } from './tts-config-JbF86KIt.js';
3
3
  import { GenerationConfig } from '@cartesia/cartesia-js/resources/tts';
4
4
  import { SupportedLanguage } from '@cartesia/cartesia-js/resources/voices';
@@ -124,6 +124,11 @@ interface SpeechPorts {
124
124
  * person's sentence and whether retrying can help.
125
125
  */
126
126
  reportProviderFailure?: (failure: ProviderSessionFailure) => Promise<ProviderSessionFailureVerdict | null>;
127
+ /**
128
+ * Report what a browser-held provider session spent (`POST /broker/usage`, once per `usage_id`).
129
+ * The host owns retry and delivery; it must never throw.
130
+ */
131
+ reportProviderUsage?: (usage: ProviderSessionUsage) => void;
127
132
  /** Tell the person speech stopped (a toast on the web). Without it the error is only on the item. */
128
133
  notifyError?: (title: string, description: string) => void;
129
134
  /**
package/dist/speech.js CHANGED
@@ -795,6 +795,8 @@ var init_cartesia_socket = __esm({
795
795
  #listeners = /* @__PURE__ */ new Map();
796
796
  /** Set when the provider refused this account and reconnecting cannot help. */
797
797
  #refused = null;
798
+ /** Sends accepted per context, for the usage idempotency key. */
799
+ #sendCounts = /* @__PURE__ */ new Map();
798
800
  constructor(native, options) {
799
801
  this.#native = native;
800
802
  this.#options = options;
@@ -864,7 +866,7 @@ var init_cartesia_socket = __esm({
864
866
  source = new CartesiaAudioSource(this.#options.sampleRate);
865
867
  this.#sources.set(contextId, source);
866
868
  }
867
- await this.#native.send({
869
+ const sent = this.#native.send({
868
870
  context_id: contextId,
869
871
  model_id: request.modelId,
870
872
  transcript: request.transcript,
@@ -881,6 +883,8 @@ var init_cartesia_socket = __esm({
881
883
  max_buffer_delay_ms: request.maxBufferDelayMs,
882
884
  generation_config: request.generationConfig
883
885
  });
886
+ await sent;
887
+ this.#reportUsage(contextId, request);
884
888
  return {
885
889
  source,
886
890
  on: (_event, listener) => {
@@ -890,6 +894,25 @@ var init_cartesia_socket = __esm({
890
894
  }
891
895
  };
892
896
  }
897
+ /**
898
+ * Cartesia bills the characters it is sent. Report each accepted send once: the context id plus a
899
+ * per-context sequence is the idempotency key, so the host's retries can never double-bill.
900
+ */
901
+ #reportUsage(contextId, request) {
902
+ const characters = request.transcript?.length ?? 0;
903
+ if (characters <= 0) return;
904
+ const sequence = (this.#sendCounts.get(contextId) ?? 0) + 1;
905
+ this.#sendCounts.set(contextId, sequence);
906
+ try {
907
+ speechPorts().reportProviderUsage?.({
908
+ provider: "cartesia",
909
+ model: request.modelId,
910
+ usage_id: `${contextId}:${sequence}`,
911
+ characters
912
+ });
913
+ } catch {
914
+ }
915
+ }
893
916
  continue(request) {
894
917
  return this.send(request);
895
918
  }