@truefoundry/ai-gateway-sdk 0.1.1 → 0.1.3
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/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic/api.d.ts +1 -0
- package/dist/providers/anthropic/api.js +4 -1
- package/dist/providers/anthropic/api.js.map +1 -1
- package/dist/providers/anthropic/chatComplete.d.ts +5 -2
- package/dist/providers/anthropic/chatComplete.js +16 -2
- package/dist/providers/anthropic/chatComplete.js.map +1 -1
- package/dist/providers/anthropic/types.d.ts +2 -0
- package/dist/providers/anthropic/types.js.map +1 -1
- package/dist/providers/google-vertex-ai/api.d.ts +1 -0
- package/dist/providers/google-vertex-ai/api.js +1 -2
- package/dist/providers/google-vertex-ai/api.js.map +1 -1
- package/dist/providers/utils/serviceTierUtils.d.ts +4 -0
- package/dist/providers/utils/serviceTierUtils.js +13 -0
- package/dist/providers/utils/serviceTierUtils.js.map +1 -1
- package/dist/services/buildExecutionReport.d.ts +2 -1
- package/dist/services/buildExecutionReport.js +24 -9
- package/dist/services/buildExecutionReport.js.map +1 -1
- package/dist/services/execute.js +2 -0
- package/dist/services/execute.js.map +1 -1
- package/dist/services/handleProviderResponse.d.ts +2 -0
- package/dist/services/handleProviderResponse.js +5 -2
- package/dist/services/handleProviderResponse.js.map +1 -1
- package/dist/services/transformToProviderRequest.js +3 -0
- package/dist/services/transformToProviderRequest.js.map +1 -1
- package/dist/types/MessagesRequest.d.ts +4 -0
- package/dist/types/contracts.d.ts +1 -1
- package/dist/types/messagesResponse.d.ts +4 -0
- package/dist/utils/billedUsage/audio.d.ts +13 -0
- package/dist/utils/billedUsage/audio.js +91 -0
- package/dist/utils/billedUsage/audio.js.map +1 -0
- package/dist/utils/billedUsage/index.d.ts +5 -0
- package/dist/utils/billedUsage/index.js +9 -0
- package/dist/utils/billedUsage/index.js.map +1 -0
- package/dist/utils/billedUsage/types.d.ts +13 -0
- package/dist/utils/billedUsage/types.js +2 -0
- package/dist/utils/billedUsage/types.js.map +1 -0
- package/dist/utils/billedUsage/video.d.ts +25 -0
- package/dist/utils/billedUsage/video.js +122 -0
- package/dist/utils/billedUsage/video.js.map +1 -0
- package/package.json +1 -1
- package/dist/utils/billedUsage.d.ts +0 -28
- package/dist/utils/billedUsage.js +0 -124
- package/dist/utils/billedUsage.js.map +0 -1
|
@@ -418,6 +418,10 @@ export interface MessageCreateParamsBase {
|
|
|
418
418
|
* Determines whether to use priority capacity (if available) or standard capacity
|
|
419
419
|
*/
|
|
420
420
|
service_tier?: 'auto' | 'standard_only';
|
|
421
|
+
/**
|
|
422
|
+
* Fast mode: `fast` serves output faster at premium pricing on supported models.
|
|
423
|
+
*/
|
|
424
|
+
speed?: 'fast' | 'standard';
|
|
421
425
|
/**
|
|
422
426
|
* Custom text sequences that will cause the model to stop generating.
|
|
423
427
|
*/
|
|
@@ -13,7 +13,7 @@ import type { HeadersOverride, RetryConfig } from '../autogen/models.js';
|
|
|
13
13
|
import type { Model } from '../entities/Model.js';
|
|
14
14
|
import type { ProviderAccountEntity } from '../entities/ProviderAccount.js';
|
|
15
15
|
import type { Endpoint } from '../providers/types.js';
|
|
16
|
-
import type { BilledUsageOverlay } from '../utils/billedUsage.js';
|
|
16
|
+
import type { BilledUsageOverlay } from '../utils/billedUsage/index.js';
|
|
17
17
|
import type { CachePort } from './cache.js';
|
|
18
18
|
import type { ExecutionReport } from './executionReport.js';
|
|
19
19
|
import type { CatalogProviderConfigs, ProviderModelConfigs } from './modelConfig.js';
|
|
@@ -134,6 +134,10 @@ export interface Usage {
|
|
|
134
134
|
* If the request used the priority, standard, or batch tier.
|
|
135
135
|
*/
|
|
136
136
|
service_tier?: 'standard' | 'priority' | 'batch' | null;
|
|
137
|
+
/**
|
|
138
|
+
* The speed the request was served at (fast mode).
|
|
139
|
+
*/
|
|
140
|
+
speed?: 'fast' | 'standard' | null;
|
|
137
141
|
/** Per-attempt usage (server-side fallbacks / compaction). */
|
|
138
142
|
iterations?: AnthropicUsageIteration[];
|
|
139
143
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BilledUsageInput, BilledUsageOverlay } from './types.js';
|
|
2
|
+
/** ElevenLabs bills the characters sent: `{ text: "café 🎵" }` is 6. */
|
|
3
|
+
export declare function countElevenLabsTtsBillableCharacters(body: Record<string, unknown>): number;
|
|
4
|
+
/** Vertex bills `input.text`, `input.ssml` (marks stripped) or the serialized `input.multi_speaker_markup`. */
|
|
5
|
+
export declare function countVertexTtsBillableCharactersFromBody(body: Record<string, unknown>): number;
|
|
6
|
+
/**
|
|
7
|
+
* Billed seconds from a speech-to-text response:
|
|
8
|
+
* - Google Cloud STT v1: `{ totalBilledTime: "90s" }`
|
|
9
|
+
* - Google Cloud STT v2: `{ metadata: { totalBilledDuration: "2.5s" } }`
|
|
10
|
+
* - ElevenLabs Scribe: `{ audio_duration_secs: 12.4 }`
|
|
11
|
+
*/
|
|
12
|
+
export declare function extractSpeechToTextBilledSeconds(responseJson: Record<string, unknown>): number | undefined;
|
|
13
|
+
export declare function billedAudioUsage({ endpoint, provider, requestBody, responseJson, }: BilledUsageInput): BilledUsageOverlay | undefined;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Provider } from '../../entities/Provider.js';
|
|
2
|
+
import { GOOGLE_VERTEX_AI } from '../../globals.js';
|
|
3
|
+
import { asRecord } from '../jsonValue.js';
|
|
4
|
+
function countCodePoints(s) {
|
|
5
|
+
return [...s].length;
|
|
6
|
+
}
|
|
7
|
+
/** `<mark>` tags are excluded from Google Cloud TTS character billing: `Hi <mark name="a"/>there` bills `Hi there`. */
|
|
8
|
+
function stripSsmlMarkTags(ssml) {
|
|
9
|
+
return ssml.replace(/<mark\b[^>]*>[\s\S]*?<\/mark>/gi, '').replace(/<mark\b[^>]*\/\s*>/gi, '');
|
|
10
|
+
}
|
|
11
|
+
/** ElevenLabs bills the characters sent: `{ text: "café 🎵" }` is 6. */
|
|
12
|
+
export function countElevenLabsTtsBillableCharacters(body) {
|
|
13
|
+
return typeof body.text === 'string' ? countCodePoints(body.text) : 0;
|
|
14
|
+
}
|
|
15
|
+
/** Vertex bills `input.text`, `input.ssml` (marks stripped) or the serialized `input.multi_speaker_markup`. */
|
|
16
|
+
export function countVertexTtsBillableCharactersFromBody(body) {
|
|
17
|
+
const input = asRecord(body.input);
|
|
18
|
+
if (!input)
|
|
19
|
+
return 0;
|
|
20
|
+
if (typeof input.text === 'string')
|
|
21
|
+
return countCodePoints(input.text);
|
|
22
|
+
if (typeof input.ssml === 'string')
|
|
23
|
+
return countCodePoints(stripSsmlMarkTags(input.ssml));
|
|
24
|
+
if (input.multi_speaker_markup != null)
|
|
25
|
+
return countCodePoints(JSON.stringify(input.multi_speaker_markup));
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
/** Parses a protobuf Duration string: `"3.5s"` is 3.5. */
|
|
29
|
+
function parseGoogleDurationSeconds(duration) {
|
|
30
|
+
if (typeof duration !== 'string')
|
|
31
|
+
return undefined;
|
|
32
|
+
const match = duration.trim().match(/^([\d.]+)s$/i);
|
|
33
|
+
if (!match)
|
|
34
|
+
return undefined;
|
|
35
|
+
const seconds = Number.parseFloat(match[1]);
|
|
36
|
+
return Number.isFinite(seconds) ? seconds : undefined;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Billed seconds from a speech-to-text response:
|
|
40
|
+
* - Google Cloud STT v1: `{ totalBilledTime: "90s" }`
|
|
41
|
+
* - Google Cloud STT v2: `{ metadata: { totalBilledDuration: "2.5s" } }`
|
|
42
|
+
* - ElevenLabs Scribe: `{ audio_duration_secs: 12.4 }`
|
|
43
|
+
*/
|
|
44
|
+
export function extractSpeechToTextBilledSeconds(responseJson) {
|
|
45
|
+
const { audio_duration_secs: scribeSeconds } = responseJson;
|
|
46
|
+
return (parseGoogleDurationSeconds(responseJson.totalBilledTime) ??
|
|
47
|
+
parseGoogleDurationSeconds(asRecord(responseJson.metadata)?.totalBilledDuration) ??
|
|
48
|
+
(typeof scribeSeconds === 'number' ? scribeSeconds : undefined));
|
|
49
|
+
}
|
|
50
|
+
function countSpeechCharacters(provider, body) {
|
|
51
|
+
if (provider === Provider.elevenlabs)
|
|
52
|
+
return countElevenLabsTtsBillableCharacters(body);
|
|
53
|
+
// The gateway's OpenAI-shaped speech request carries the text as a plain string: `{ input: "hello" }`.
|
|
54
|
+
const openAiInputLength = typeof body.input === 'string' ? countCodePoints(body.input) : 0;
|
|
55
|
+
return countVertexTtsBillableCharactersFromBody(body) || openAiInputLength;
|
|
56
|
+
}
|
|
57
|
+
/** `tts-proxy` forwards the provider-native body; only these providers bill by characters. */
|
|
58
|
+
function countProxySpeechCharacters(provider, body) {
|
|
59
|
+
switch (provider) {
|
|
60
|
+
case Provider.elevenlabs:
|
|
61
|
+
return countElevenLabsTtsBillableCharacters(body);
|
|
62
|
+
case Provider.vertex:
|
|
63
|
+
case Provider.google:
|
|
64
|
+
case GOOGLE_VERTEX_AI:
|
|
65
|
+
// `google` (`gcp`) and `vertex` are legacy aliases that resolve to the Vertex config.
|
|
66
|
+
return countVertexTtsBillableCharactersFromBody(body);
|
|
67
|
+
default:
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export function billedAudioUsage({ endpoint, provider, requestBody, responseJson, }) {
|
|
72
|
+
switch (endpoint) {
|
|
73
|
+
case 'createSpeech':
|
|
74
|
+
case 'tts-proxy': {
|
|
75
|
+
const body = asRecord(requestBody);
|
|
76
|
+
const count = endpoint === 'createSpeech' ? countSpeechCharacters : countProxySpeechCharacters;
|
|
77
|
+
const characters = body ? count(provider, body) : 0;
|
|
78
|
+
return characters > 0 ? { input_characters: characters } : undefined;
|
|
79
|
+
}
|
|
80
|
+
case 'createTranscription':
|
|
81
|
+
case 'createTranslation':
|
|
82
|
+
case 'stt-proxy': {
|
|
83
|
+
const json = asRecord(responseJson);
|
|
84
|
+
const seconds = json && extractSpeechToTextBilledSeconds(json);
|
|
85
|
+
return seconds === undefined ? undefined : { audio_seconds: seconds };
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=audio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio.js","sourceRoot":"","sources":["../../../src/utils/billedUsage/audio.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxC,SAAS,eAAe,CAAC,CAAS;IAChC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACvB,CAAC;AAED,uHAAuH;AACvH,SAAS,iBAAiB,CAAC,IAAY;IACrC,OAAO,IAAI,CAAC,OAAO,CAAC,iCAAiC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,EAAE,CAAC,CAAC;AACjG,CAAC;AAED,wEAAwE;AACxE,MAAM,UAAU,oCAAoC,CAAC,IAA6B;IAChF,OAAO,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,wCAAwC,CAAC,IAA6B;IACpF,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,CAAC,KAAK;QAAE,OAAO,CAAC,CAAC;IACrB,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,eAAe,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACvE,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QA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,CAAC;YACjB,MAAM,IAAI,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;YACpC,MAAM,OAAO,GAAG,IAAI,IAAI,gCAAgC,CAAC,IAAI,CAAC,CAAC;YAC/D,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,EAAE,CAAC;QACxE,CAAC;QACD;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BilledUsageInput, BilledUsageOverlay } from './types.js';
|
|
2
|
+
export { extractSpeechToTextBilledSeconds } from './audio.js';
|
|
3
|
+
export type { BilledUsageOverlay } from './types.js';
|
|
4
|
+
export { billedUsageFromOpenRouterBody, DEEPINFRA_INFERENCE_PATH } from './video.js';
|
|
5
|
+
export declare function billedUsageFromRequestResponse(input: BilledUsageInput): BilledUsageOverlay | undefined;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Billed characters, seconds and video, peeked off request and response bodies without Hono Context. */
|
|
2
|
+
import { billedAudioUsage } from './audio.js';
|
|
3
|
+
import { billedVideoUsage } from './video.js';
|
|
4
|
+
export { extractSpeechToTextBilledSeconds } from './audio.js';
|
|
5
|
+
export { billedUsageFromOpenRouterBody, DEEPINFRA_INFERENCE_PATH } from './video.js';
|
|
6
|
+
export function billedUsageFromRequestResponse(input) {
|
|
7
|
+
return billedAudioUsage(input) ?? billedVideoUsage(input);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/billedUsage/index.ts"],"names":[],"mappings":"AAAA,yGAAyG;AACzG,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,gCAAgC,EAAE,MAAM,SAAS,CAAC;AAE3D,OAAO,EAAE,6BAA6B,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAElF,MAAM,UAAU,8BAA8B,CAAC,KAAuB;IACpE,OAAO,gBAAgB,CAAC,KAAK,CAAC,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Endpoint } from '../../providers/types.js';
|
|
2
|
+
import type { TokenUsage } from '../../types/executionReport.js';
|
|
3
|
+
/** Usage the request or response bodies imply but the provider does not report itself. */
|
|
4
|
+
export type BilledUsageOverlay = Pick<TokenUsage, 'input_characters' | 'audio_seconds' | 'video_seconds' | 'video_resolution' | 'video_audio'>;
|
|
5
|
+
export interface BilledUsageInput {
|
|
6
|
+
endpoint: Endpoint;
|
|
7
|
+
provider: string;
|
|
8
|
+
requestBody: unknown;
|
|
9
|
+
requestUrl?: string;
|
|
10
|
+
/** Needed by `video-proxy`, where only the submit `POST` bills. */
|
|
11
|
+
requestMethod?: string;
|
|
12
|
+
responseJson?: unknown;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/billedUsage/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { BilledUsageInput, BilledUsageOverlay } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Veo bills `durationSeconds * sampleCount` at `resolution`; omitted fields bill Veo's own defaults (8s, 720p, audio on).
|
|
4
|
+
* `{ parameters: { durationSeconds: 6, sampleCount: 2 } }` is 12 seconds at 720p with audio.
|
|
5
|
+
*/
|
|
6
|
+
export declare function billedUsageFromVeoBody(body: Record<string, unknown>): BilledUsageOverlay;
|
|
7
|
+
/**
|
|
8
|
+
* `{ duration: 8, resolution: "720p", generate_audio: false }` is 8 seconds at 720p without audio.
|
|
9
|
+
* OpenRouter's default duration differs per model and is undocumented, so `duration` is required; a
|
|
10
|
+
* body that cannot be billed throws a `ValidationError` (400) before dispatch.
|
|
11
|
+
*/
|
|
12
|
+
export declare function billedUsageFromOpenRouterBody(body: Record<string, unknown>): BilledUsageOverlay;
|
|
13
|
+
/** DeepInfra native path `/v1/inference/{org}/{model}`. */
|
|
14
|
+
export declare const DEEPINFRA_INFERENCE_PATH: RegExp;
|
|
15
|
+
/**
|
|
16
|
+
* Billed from the request body because the response is never read:
|
|
17
|
+
* - seconds: `duration` for Pixverse, p-video, Seedance and Wan2.6; every other model bills 5s,
|
|
18
|
+
* times `sample_count` (1-4) for Veo
|
|
19
|
+
* - resolution: see `deepInfraResolution`; the live API accepts `480P`, catalogue rate keys are lowercase
|
|
20
|
+
*
|
|
21
|
+
* `google/veo-3` with `{ sample_count: 2 }` is 10 seconds (5s default x 2) at 720p.
|
|
22
|
+
*/
|
|
23
|
+
export declare function billedUsageFromDeepInfraBody(body: Record<string, unknown>, modelId: string): BilledUsageOverlay;
|
|
24
|
+
/** Billed video usage of a `video-proxy` submit; the URL tells OpenRouter and DeepInfra submits from their polls. */
|
|
25
|
+
export declare function billedVideoUsage({ endpoint, provider, requestBody, requestMethod, requestUrl, }: BilledUsageInput): BilledUsageOverlay | undefined;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Provider } from '../../entities/Provider.js';
|
|
2
|
+
import { ValidationError } from '../../errors.js';
|
|
3
|
+
import { GOOGLE_VERTEX_AI } from '../../globals.js';
|
|
4
|
+
import { asRecord } from '../jsonValue.js';
|
|
5
|
+
const DEFAULT_RESOLUTION = '720p';
|
|
6
|
+
const DEFAULT_GENERATE_AUDIO = true;
|
|
7
|
+
/** Client JSON can hold objects whose coercion throws (`{ toString: 0 }`); only numbers and numeric strings count. */
|
|
8
|
+
const toNumber = (value) => typeof value === 'number' || typeof value === 'string' ? Number(value) : NaN;
|
|
9
|
+
const nonEmptyString = (value) => (typeof value === 'string' && value ? value : undefined);
|
|
10
|
+
/** `positiveOr(NaN, 8)` and `positiveOr(0, 8)` are 8; an absent field arrives here as NaN. */
|
|
11
|
+
const positiveOr = (value, fallback) => Number.isFinite(value) && value > 0 ? value : fallback;
|
|
12
|
+
/**
|
|
13
|
+
* Veo bills `durationSeconds * sampleCount` at `resolution`; omitted fields bill Veo's own defaults (8s, 720p, audio on).
|
|
14
|
+
* `{ parameters: { durationSeconds: 6, sampleCount: 2 } }` is 12 seconds at 720p with audio.
|
|
15
|
+
*/
|
|
16
|
+
export function billedUsageFromVeoBody(body) {
|
|
17
|
+
const parameters = asRecord(body.parameters) ?? {};
|
|
18
|
+
const { generateAudio } = parameters;
|
|
19
|
+
return {
|
|
20
|
+
video_seconds: positiveOr(toNumber(parameters.durationSeconds), 8) * positiveOr(toNumber(parameters.sampleCount), 1),
|
|
21
|
+
video_resolution: nonEmptyString(parameters.resolution) ?? DEFAULT_RESOLUTION,
|
|
22
|
+
video_audio: typeof generateAudio === 'boolean' ? generateAudio : DEFAULT_GENERATE_AUDIO,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** `{ resolution: "1080p" }` or `{ size: "1920x1080" }` (billed by the shorter side) is `1080p`; neither is `720p`. */
|
|
26
|
+
function openRouterResolution({ resolution, size }) {
|
|
27
|
+
for (const [field, value] of [
|
|
28
|
+
['resolution', resolution],
|
|
29
|
+
['size', size],
|
|
30
|
+
]) {
|
|
31
|
+
if (value != null && typeof value !== 'string') {
|
|
32
|
+
throw new ValidationError(`Video proxy requests for OpenRouter require "${field}" to be a string.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// OpenRouter does not document which field wins when both are sent, so billing could not match the output.
|
|
36
|
+
if (resolution && size) {
|
|
37
|
+
throw new ValidationError('Video proxy requests for OpenRouter accept either "resolution" or "size", not both.');
|
|
38
|
+
}
|
|
39
|
+
if (resolution)
|
|
40
|
+
return resolution;
|
|
41
|
+
const dimensions = size?.match(/^(\d+)x(\d+)$/);
|
|
42
|
+
return dimensions ? `${Math.min(Number(dimensions[1]), Number(dimensions[2]))}p` : DEFAULT_RESOLUTION;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* `{ duration: 8, resolution: "720p", generate_audio: false }` is 8 seconds at 720p without audio.
|
|
46
|
+
* OpenRouter's default duration differs per model and is undocumented, so `duration` is required; a
|
|
47
|
+
* body that cannot be billed throws a `ValidationError` (400) before dispatch.
|
|
48
|
+
*/
|
|
49
|
+
export function billedUsageFromOpenRouterBody(body) {
|
|
50
|
+
const duration = toNumber(body.duration);
|
|
51
|
+
if (!Number.isFinite(duration) || duration <= 0) {
|
|
52
|
+
throw new ValidationError('Video proxy requests for OpenRouter require a positive numeric "duration" in the request body.');
|
|
53
|
+
}
|
|
54
|
+
const { generate_audio: generateAudio } = body;
|
|
55
|
+
return {
|
|
56
|
+
video_seconds: duration,
|
|
57
|
+
video_resolution: openRouterResolution(body),
|
|
58
|
+
video_audio: typeof generateAudio === 'boolean' ? generateAudio : DEFAULT_GENERATE_AUDIO,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** DeepInfra native path `/v1/inference/{org}/{model}`. */
|
|
62
|
+
export const DEEPINFRA_INFERENCE_PATH = /^\/v1\/inference\/([A-Za-z0-9][\w.-]*\/[A-Za-z0-9][\w.-]*)$/;
|
|
63
|
+
// Only these models read a request `duration`; the rest bill the 5s default.
|
|
64
|
+
const DEEPINFRA_DURATION_FIELD_MODEL = /^(?:Pixverse|PrunaAI|ByteDance)\/|^Wan-AI\/Wan2\.6/;
|
|
65
|
+
// Veo renders `sample_count` (1-4) videos per request.
|
|
66
|
+
const DEEPINFRA_SAMPLE_COUNT_MODEL = /^google\/veo/;
|
|
67
|
+
// Wan2.6 reads resolution only from `size` and renders 1920*1080 when it is omitted.
|
|
68
|
+
const DEEPINFRA_SIZE_FIELD_MODEL = /^Wan-AI\/Wan2\.6/;
|
|
69
|
+
function deepInfraResolution(body, modelId) {
|
|
70
|
+
if (DEEPINFRA_SIZE_FIELD_MODEL.test(modelId)) {
|
|
71
|
+
// `size: "1280*720"` is billed by the shorter side, `720p`; a missing or malformed `size` bills the default.
|
|
72
|
+
const [width, height] = (nonEmptyString(body.size) ?? '').split('*').map(Number);
|
|
73
|
+
return width > 0 && height > 0 ? `${Math.min(width, height)}p` : '1080p';
|
|
74
|
+
}
|
|
75
|
+
return nonEmptyString(body.resolution) ?? nonEmptyString(body.quality) ?? DEFAULT_RESOLUTION;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Billed from the request body because the response is never read:
|
|
79
|
+
* - seconds: `duration` for Pixverse, p-video, Seedance and Wan2.6; every other model bills 5s,
|
|
80
|
+
* times `sample_count` (1-4) for Veo
|
|
81
|
+
* - resolution: see `deepInfraResolution`; the live API accepts `480P`, catalogue rate keys are lowercase
|
|
82
|
+
*
|
|
83
|
+
* `google/veo-3` with `{ sample_count: 2 }` is 10 seconds (5s default x 2) at 720p.
|
|
84
|
+
*/
|
|
85
|
+
export function billedUsageFromDeepInfraBody(body, modelId) {
|
|
86
|
+
const duration = DEEPINFRA_DURATION_FIELD_MODEL.test(modelId) ? toNumber(body.duration) : NaN;
|
|
87
|
+
const sampleCount = DEEPINFRA_SAMPLE_COUNT_MODEL.test(modelId) ? toNumber(body.sample_count) : NaN;
|
|
88
|
+
return {
|
|
89
|
+
video_seconds: positiveOr(duration, 5) *
|
|
90
|
+
(Number.isInteger(sampleCount) && sampleCount >= 1 && sampleCount <= 4 ? sampleCount : 1),
|
|
91
|
+
video_resolution: deepInfraResolution(body, modelId).toLowerCase(),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Billed video usage of a `video-proxy` submit; the URL tells OpenRouter and DeepInfra submits from their polls. */
|
|
95
|
+
export function billedVideoUsage({ endpoint, provider, requestBody, requestMethod, requestUrl, }) {
|
|
96
|
+
const body = asRecord(requestBody);
|
|
97
|
+
// Polls and downloads are `GET`s (the gateway hands them an empty `{}` body); only a `POST` submits a paid job.
|
|
98
|
+
if (endpoint !== 'video-proxy' || requestMethod !== 'POST' || !body)
|
|
99
|
+
return undefined;
|
|
100
|
+
const pathname = requestUrl ? new URL(requestUrl).pathname : undefined;
|
|
101
|
+
switch (provider) {
|
|
102
|
+
case Provider.openrouter:
|
|
103
|
+
// Submit is `POST /api/v1/videos`; polls are `GET /api/v1/videos/{id}` and carry no billable body.
|
|
104
|
+
return pathname?.endsWith('/videos') ? billedUsageFromOpenRouterBody(body) : undefined;
|
|
105
|
+
case Provider.deepinfra: {
|
|
106
|
+
const modelId = pathname?.match(DEEPINFRA_INFERENCE_PATH)?.[1];
|
|
107
|
+
return modelId ? billedUsageFromDeepInfraBody(body, modelId) : undefined;
|
|
108
|
+
}
|
|
109
|
+
case Provider.vertex:
|
|
110
|
+
case Provider.google:
|
|
111
|
+
case GOOGLE_VERTEX_AI:
|
|
112
|
+
case Provider.gemini:
|
|
113
|
+
// `google` (`gcp`) and `vertex` are legacy aliases that resolve to the Vertex config.
|
|
114
|
+
// Only the submit bills: Vertex polls with `POST ...:fetchPredictOperation` (body `{ operationName }`),
|
|
115
|
+
// which would otherwise bill a second generation at the Veo defaults.
|
|
116
|
+
return pathname?.endsWith(':predictLongRunning') ? billedUsageFromVeoBody(body) : undefined;
|
|
117
|
+
default:
|
|
118
|
+
// Includes self-hosted ComfyUI (`custom`), which has no cost tracking.
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=video.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"video.js","sourceRoot":"","sources":["../../../src/utils/billedUsage/video.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxC,MAAM,kBAAkB,GA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M,CAAC;QACrB,KAAK,QAAQ,CAAC,MAAM,CAAC;QACrB,KAAK,gBAAgB,CAAC;QACtB,KAAK,QAAQ,CAAC,MAAM;YAClB,sFAAsF;YACtF,wGAAwG;YACxG,sEAAsE;YACtE,OAAO,QAAQ,EAAE,QAAQ,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,sBAAsB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC9F;YACE,uEAAuE;YACvE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request/response billed chars, seconds, and video — same rules as live
|
|
3
|
-
* audioBillingUtils / videoRequestUtils, without Hono Context.
|
|
4
|
-
*/
|
|
5
|
-
import type { Endpoint } from '../providers/types.js';
|
|
6
|
-
import type { TokenUsage } from '../types/executionReport.js';
|
|
7
|
-
export type BilledUsageOverlay = Pick<TokenUsage, 'input_characters' | 'audio_seconds' | 'video_seconds' | 'video_resolution' | 'video_audio'>;
|
|
8
|
-
export declare function countElevenLabsTtsBillableCharacters(body: Record<string, unknown>): number;
|
|
9
|
-
export declare function countVertexTtsBillableCharactersFromBody(body: Record<string, unknown>): number;
|
|
10
|
-
export declare function parseGoogleDurationSeconds(durationStr: unknown): number | undefined;
|
|
11
|
-
export declare function extractSpeechToTextBilledSeconds(responseJson: Record<string, unknown>): number | undefined;
|
|
12
|
-
export declare function billedUsageFromVeoBody(body: Record<string, unknown>): BilledUsageOverlay;
|
|
13
|
-
/**
|
|
14
|
-
* Billed video usage for a DeepInfra video-proxy submit, peeked off the request body because the
|
|
15
|
-
* response is never read. The gateway bills the same way (`resolveDeepInfraBillingParams` in
|
|
16
|
-
* `tfy-llm-gateway`); keep the two in step.
|
|
17
|
-
* - duration: `duration` for Pixverse, p-video, Seedance and Wan2.6; every other model bills the 5s default,
|
|
18
|
-
* times `sample_count` (1-4) for Veo
|
|
19
|
-
* - resolution: shorter side of `size` (`width*height`) for Wan2.6, default 1080p; `resolution` | `quality` for the rest, default 720p; lowercased
|
|
20
|
-
*/
|
|
21
|
-
export declare function billedUsageFromDeepInfraBody(body: Record<string, unknown>, modelId: string): BilledUsageOverlay;
|
|
22
|
-
export declare function billedUsageFromRequestResponse(args: {
|
|
23
|
-
endpoint: Endpoint;
|
|
24
|
-
provider: string;
|
|
25
|
-
requestBody: unknown;
|
|
26
|
-
requestUrl?: string;
|
|
27
|
-
responseJson?: unknown;
|
|
28
|
-
}): BilledUsageOverlay | undefined;
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { asRecord } from './jsonValue.js';
|
|
2
|
-
function countCodePoints(s) {
|
|
3
|
-
return [...s].length;
|
|
4
|
-
}
|
|
5
|
-
function stripSsmlMarkTags(ssml) {
|
|
6
|
-
return ssml.replace(/<mark\b[^>]*>[\s\S]*?<\/mark>/gi, '').replace(/<mark\b[^>]*\/\s*>/gi, '');
|
|
7
|
-
}
|
|
8
|
-
export function countElevenLabsTtsBillableCharacters(body) {
|
|
9
|
-
return typeof body.text === 'string' ? countCodePoints(body.text) : 0;
|
|
10
|
-
}
|
|
11
|
-
export function countVertexTtsBillableCharactersFromBody(body) {
|
|
12
|
-
const inp = asRecord(body.input);
|
|
13
|
-
if (!inp)
|
|
14
|
-
return 0;
|
|
15
|
-
if (typeof inp.text === 'string')
|
|
16
|
-
return countCodePoints(inp.text);
|
|
17
|
-
if (typeof inp.ssml === 'string')
|
|
18
|
-
return countCodePoints(stripSsmlMarkTags(inp.ssml));
|
|
19
|
-
if (inp.multi_speaker_markup != null)
|
|
20
|
-
return countCodePoints(JSON.stringify(inp.multi_speaker_markup));
|
|
21
|
-
return 0;
|
|
22
|
-
}
|
|
23
|
-
export function parseGoogleDurationSeconds(durationStr) {
|
|
24
|
-
if (typeof durationStr !== 'string')
|
|
25
|
-
return undefined;
|
|
26
|
-
const m = durationStr.trim().match(/^([\d.]+)s$/i);
|
|
27
|
-
if (!m)
|
|
28
|
-
return undefined;
|
|
29
|
-
const n = Number.parseFloat(m[1]);
|
|
30
|
-
return Number.isFinite(n) ? n : undefined;
|
|
31
|
-
}
|
|
32
|
-
export function extractSpeechToTextBilledSeconds(responseJson) {
|
|
33
|
-
const v1 = parseGoogleDurationSeconds(responseJson.totalBilledTime);
|
|
34
|
-
if (v1 !== undefined)
|
|
35
|
-
return v1;
|
|
36
|
-
const meta = asRecord(responseJson.metadata);
|
|
37
|
-
if (meta) {
|
|
38
|
-
const v2 = parseGoogleDurationSeconds(meta.totalBilledDuration);
|
|
39
|
-
if (v2 !== undefined)
|
|
40
|
-
return v2;
|
|
41
|
-
}
|
|
42
|
-
if (typeof responseJson.audio_duration_secs === 'number' && Number.isFinite(responseJson.audio_duration_secs)) {
|
|
43
|
-
return responseJson.audio_duration_secs;
|
|
44
|
-
}
|
|
45
|
-
return undefined;
|
|
46
|
-
}
|
|
47
|
-
const DEFAULT_VIDEO_DURATION_SECONDS = 8;
|
|
48
|
-
const DEFAULT_VIDEO_RESOLUTION = '720p';
|
|
49
|
-
const DEFAULT_VIDEO_SAMPLE_COUNT = 1;
|
|
50
|
-
const DEFAULT_VIDEO_GENERATE_AUDIO = true;
|
|
51
|
-
// Omitted fields bill Veo's own defaults (matches the gateway's `resolveNativeVeoBillingParams`).
|
|
52
|
-
export function billedUsageFromVeoBody(body) {
|
|
53
|
-
const p = asRecord(body.parameters) ?? {};
|
|
54
|
-
const durationRaw = p.durationSeconds != null ? Number(p.durationSeconds) : DEFAULT_VIDEO_DURATION_SECONDS;
|
|
55
|
-
const sampleRaw = p.sampleCount != null ? Number(p.sampleCount) : DEFAULT_VIDEO_SAMPLE_COUNT;
|
|
56
|
-
const durationSeconds = Number.isFinite(durationRaw) && durationRaw > 0 ? durationRaw : DEFAULT_VIDEO_DURATION_SECONDS;
|
|
57
|
-
const sampleCount = Number.isFinite(sampleRaw) && sampleRaw > 0 ? sampleRaw : DEFAULT_VIDEO_SAMPLE_COUNT;
|
|
58
|
-
return {
|
|
59
|
-
video_seconds: durationSeconds * sampleCount,
|
|
60
|
-
video_resolution: typeof p.resolution === 'string' && p.resolution ? p.resolution : DEFAULT_VIDEO_RESOLUTION,
|
|
61
|
-
video_audio: typeof p.generateAudio === 'boolean' ? p.generateAudio : DEFAULT_VIDEO_GENERATE_AUDIO,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
const DEEPINFRA_DEFAULT_VIDEO_DURATION_SECONDS = 5;
|
|
65
|
-
// Only these models read a request `duration`; the rest bill the 5s default.
|
|
66
|
-
const DEEPINFRA_DURATION_FIELD_MODEL = /^(?:Pixverse|PrunaAI|ByteDance)\/|^Wan-AI\/Wan2\.6/;
|
|
67
|
-
// Veo renders `sample_count` (1-4) videos per request.
|
|
68
|
-
const DEEPINFRA_SAMPLE_COUNT_MODEL = /^google\/veo/;
|
|
69
|
-
// Wan2.6 reads resolution only from `size` and renders 1920*1080 when it is omitted.
|
|
70
|
-
const DEEPINFRA_SIZE_FIELD_MODEL = /^Wan-AI\/Wan2\.6/;
|
|
71
|
-
// DeepInfra native path `/v1/inference/{org}/{model}`.
|
|
72
|
-
const DEEPINFRA_INFERENCE_PATH = /^\/v1\/inference\/([A-Za-z0-9][\w.-]*\/[A-Za-z0-9][\w.-]*)$/;
|
|
73
|
-
/**
|
|
74
|
-
* Billed video usage for a DeepInfra video-proxy submit, peeked off the request body because the
|
|
75
|
-
* response is never read. The gateway bills the same way (`resolveDeepInfraBillingParams` in
|
|
76
|
-
* `tfy-llm-gateway`); keep the two in step.
|
|
77
|
-
* - duration: `duration` for Pixverse, p-video, Seedance and Wan2.6; every other model bills the 5s default,
|
|
78
|
-
* times `sample_count` (1-4) for Veo
|
|
79
|
-
* - resolution: shorter side of `size` (`width*height`) for Wan2.6, default 1080p; `resolution` | `quality` for the rest, default 720p; lowercased
|
|
80
|
-
*/
|
|
81
|
-
export function billedUsageFromDeepInfraBody(body, modelId) {
|
|
82
|
-
const duration = DEEPINFRA_DURATION_FIELD_MODEL.test(modelId) ? Number(body.duration) : NaN;
|
|
83
|
-
const [width, height] = String(body.size).split('*').map(Number);
|
|
84
|
-
const resolution = DEEPINFRA_SIZE_FIELD_MODEL.test(modelId)
|
|
85
|
-
? width > 0 && height > 0
|
|
86
|
-
? `${Math.min(width, height)}p`
|
|
87
|
-
: '1080p'
|
|
88
|
-
: body.resolution || body.quality || DEFAULT_VIDEO_RESOLUTION;
|
|
89
|
-
const sampleCount = DEEPINFRA_SAMPLE_COUNT_MODEL.test(modelId) ? Number(body.sample_count) : NaN;
|
|
90
|
-
return {
|
|
91
|
-
video_seconds: (Number.isFinite(duration) && duration > 0 ? duration : DEEPINFRA_DEFAULT_VIDEO_DURATION_SECONDS) *
|
|
92
|
-
(Number.isInteger(sampleCount) && sampleCount >= 1 && sampleCount <= 4 ? sampleCount : 1),
|
|
93
|
-
video_resolution: String(resolution).toLowerCase(),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
export function billedUsageFromRequestResponse(args) {
|
|
97
|
-
const out = {};
|
|
98
|
-
const body = asRecord(args.requestBody);
|
|
99
|
-
const json = asRecord(args.responseJson);
|
|
100
|
-
if (args.endpoint === 'createSpeech' && body) {
|
|
101
|
-
const chars = args.provider === 'elevenlabs'
|
|
102
|
-
? countElevenLabsTtsBillableCharacters(body)
|
|
103
|
-
: countVertexTtsBillableCharactersFromBody(body) ||
|
|
104
|
-
(typeof body.input === 'string' ? countCodePoints(body.input) : 0);
|
|
105
|
-
if (chars > 0)
|
|
106
|
-
out.input_characters = chars;
|
|
107
|
-
}
|
|
108
|
-
if ((args.endpoint === 'createTranscription' || args.endpoint === 'createTranslation') && json) {
|
|
109
|
-
const sec = extractSpeechToTextBilledSeconds(json);
|
|
110
|
-
if (sec !== undefined)
|
|
111
|
-
out.audio_seconds = sec;
|
|
112
|
-
}
|
|
113
|
-
if (args.provider === 'deepinfra' && args.endpoint === 'video-proxy') {
|
|
114
|
-
// DeepInfra is never billed from the Veo `parameters` shape.
|
|
115
|
-
const modelId = args.requestUrl && new URL(args.requestUrl).pathname.match(DEEPINFRA_INFERENCE_PATH)?.[1];
|
|
116
|
-
if (body && modelId)
|
|
117
|
-
Object.assign(out, billedUsageFromDeepInfraBody(body, modelId));
|
|
118
|
-
}
|
|
119
|
-
else if (args.endpoint === 'video-proxy' && body) {
|
|
120
|
-
Object.assign(out, billedUsageFromVeoBody(body));
|
|
121
|
-
}
|
|
122
|
-
return Object.keys(out).length > 0 ? out : undefined;
|
|
123
|
-
}
|
|
124
|
-
//# sourceMappingURL=billedUsage.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"billedUsage.js","sourceRoot":"","sources":["../../src/utils/billedUsage.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAOvC,SAAS,eAAe,CAAC,CAAS;IAChC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACvB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY;IACrC,OAAO,IAAI,CAAC,OAAO,CAAC,iCAAiC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,EAAE,CAAC,CAAC;AACjG,CAAC;AAED,MAAM,UAAU,oCAAoC,CAAC,IAA6B;IAChF,OAAO,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAED,MAAM,UAAU,wCAAwC,CAAC,IAA6B;IACpF,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjC,IAAI,CAAC,GAAG;QAAE,OAAO,CAAC,CAAC;IACnB,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACnE,IAAI,OAAO,GAAG,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