@vibedev-si/dsh-vibedev 0.3.2 → 0.3.4
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/README.md +3 -2
- package/THIRD-PARTY-NOTICES.txt +1 -1
- package/lib/gateway/catalog.d.ts +48 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +685 -58
- package/lib/pricing.d.ts +99 -0
- package/lib/service.d.ts +18 -3
- package/lib/tasks/remote.d.ts +20 -2
- package/lib/tasks/store.d.ts +7 -1
- package/lib/tasks/tracker.d.ts +10 -1
- package/lib/tools/video.d.ts +3 -0
- package/lib/util/decimal.d.ts +62 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -26,8 +26,12 @@ const ACCOUNT_ROUTE_PREFIX = "/api/dsh-vibedev/account";
|
|
|
26
26
|
* when it already has a balance.
|
|
27
27
|
*/
|
|
28
28
|
const BALANCE_TTL_MS = 3e4;
|
|
29
|
-
/**
|
|
30
|
-
|
|
29
|
+
/**
|
|
30
|
+
* How long `?since=` holds a request open when nothing changes. The app installs an update only after open
|
|
31
|
+
* requests finish and waits at most 10 s for them, so this stays well below that (0.3.2 held 25 s, and an
|
|
32
|
+
* update failed with "task inspection timed out" whenever the poll had more than 10 s to go).
|
|
33
|
+
*/
|
|
34
|
+
const WAIT_MS = 8e3;
|
|
31
35
|
/**
|
|
32
36
|
* When the balance is read again after a charge: the gateway settles a charge
|
|
33
37
|
* shortly after the request ends, so it is read a moment later, and once more
|
|
@@ -1567,7 +1571,7 @@ const ENDPOINT = {
|
|
|
1567
1571
|
const record$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
1568
1572
|
const strings$1 = (value) => Array.isArray(value) && value.every((item) => typeof item === "string") ? value : void 0;
|
|
1569
1573
|
const positive$1 = (value) => typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : void 0;
|
|
1570
|
-
const price = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
1574
|
+
const price$1 = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
1571
1575
|
/**
|
|
1572
1576
|
* The protocol a model family is called over best, before the catalog's endpoint list has a say:
|
|
1573
1577
|
* Claude speaks Messages natively; GPT, the o-series, Codex, and Grok keep their reasoning state
|
|
@@ -1651,10 +1655,10 @@ function parseCatalog(body) {
|
|
|
1651
1655
|
...defaultReasoning === void 0 ? {} : { defaultReasoning },
|
|
1652
1656
|
adaptiveThinking: capabilities.adaptive_thinking === true,
|
|
1653
1657
|
cost: {
|
|
1654
|
-
input: price(entry.input_price_per_mtok),
|
|
1655
|
-
output: price(entry.output_price_per_mtok),
|
|
1656
|
-
cacheRead: price(entry.cache_read_price_per_mtok),
|
|
1657
|
-
cacheWrite: price(entry.cache_write_price_per_mtok)
|
|
1658
|
+
input: price$1(entry.input_price_per_mtok),
|
|
1659
|
+
output: price$1(entry.output_price_per_mtok),
|
|
1660
|
+
cacheRead: price$1(entry.cache_read_price_per_mtok),
|
|
1661
|
+
cacheWrite: price$1(entry.cache_write_price_per_mtok)
|
|
1658
1662
|
}
|
|
1659
1663
|
});
|
|
1660
1664
|
}
|
|
@@ -4070,6 +4074,59 @@ function parsePricing(value, audioUnit) {
|
|
|
4070
4074
|
} }
|
|
4071
4075
|
};
|
|
4072
4076
|
}
|
|
4077
|
+
const price = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
4078
|
+
/** A rule condition: `null` or absent is "any", a boolean is kept as it is (false included), anything else is unreadable. */
|
|
4079
|
+
function condition(raw, snake) {
|
|
4080
|
+
const value = field(raw, snake);
|
|
4081
|
+
if (value === void 0 || value === null) return {};
|
|
4082
|
+
return typeof value === "boolean" ? { value } : void 0;
|
|
4083
|
+
}
|
|
4084
|
+
/**
|
|
4085
|
+
* Parse `video_pricing`. Every rule is checked: one unreadable rule makes the
|
|
4086
|
+
* whole object unusable, since dropping it could make another rule look unique.
|
|
4087
|
+
* @param value - the entry's `video_pricing`.
|
|
4088
|
+
* @returns the pricing, the reason it cannot be used, or nothing when the entry has none.
|
|
4089
|
+
*/
|
|
4090
|
+
function parseVideoPricing(value) {
|
|
4091
|
+
if (value === void 0 || value === null) return {};
|
|
4092
|
+
const raw = record$3(value);
|
|
4093
|
+
if (raw === void 0) return { videoPricingIssue: "invalid" };
|
|
4094
|
+
if (field(raw, "schema_version") !== 1) return { videoPricingIssue: "unknown-schema" };
|
|
4095
|
+
const invalid = { videoPricingIssue: "invalid" };
|
|
4096
|
+
const estimateOnly = field(raw, "estimate_only");
|
|
4097
|
+
if (field(raw, "currency") !== "CNY" || field(raw, "final_basis") !== "provider_tokens" || field(raw, "estimate_basis") !== "output_plus_reference_video_seconds" || typeof estimateOnly !== "boolean") return invalid;
|
|
4098
|
+
const rulesRaw = field(raw, "rules");
|
|
4099
|
+
if (!Array.isArray(rulesRaw) || rulesRaw.length === 0) return invalid;
|
|
4100
|
+
const rules = [];
|
|
4101
|
+
for (const item of rulesRaw) {
|
|
4102
|
+
const entry = record$3(item);
|
|
4103
|
+
if (entry === void 0) return invalid;
|
|
4104
|
+
const resolution = text$4(field(entry, "resolution"));
|
|
4105
|
+
const perMillion = price(field(entry, "price_per_million_tokens"));
|
|
4106
|
+
const estimateRaw = field(entry, "estimated_price_per_second");
|
|
4107
|
+
const perSecond = estimateRaw === void 0 || estimateRaw === null ? void 0 : price(estimateRaw);
|
|
4108
|
+
const video = condition(entry, "reference_video");
|
|
4109
|
+
const audio = condition(entry, "reference_audio");
|
|
4110
|
+
const image = condition(entry, "reference_image");
|
|
4111
|
+
if (resolution === void 0 || perMillion === void 0 || estimateRaw !== void 0 && estimateRaw !== null && perSecond === void 0 || video === void 0 || audio === void 0 || image === void 0) return invalid;
|
|
4112
|
+
rules.push({
|
|
4113
|
+
resolution,
|
|
4114
|
+
...video.value === void 0 ? {} : { referenceVideo: video.value },
|
|
4115
|
+
...audio.value === void 0 ? {} : { referenceAudio: audio.value },
|
|
4116
|
+
...image.value === void 0 ? {} : { referenceImage: image.value },
|
|
4117
|
+
pricePerMillionTokensCny: perMillion,
|
|
4118
|
+
...perSecond === void 0 ? {} : { estimatedPricePerSecondCny: perSecond }
|
|
4119
|
+
});
|
|
4120
|
+
}
|
|
4121
|
+
return { videoPricing: {
|
|
4122
|
+
schemaVersion: 1,
|
|
4123
|
+
currency: "CNY",
|
|
4124
|
+
finalBasis: "provider_tokens",
|
|
4125
|
+
estimateBasis: "output_plus_reference_video_seconds",
|
|
4126
|
+
estimateOnly,
|
|
4127
|
+
rules
|
|
4128
|
+
} };
|
|
4129
|
+
}
|
|
4073
4130
|
/** Which kind of media model a catalog entry is, or undefined for a chat or internal model. */
|
|
4074
4131
|
function kindOf(entry, capabilities, audio) {
|
|
4075
4132
|
if (text$4(entry.internal_role) !== void 0) return void 0;
|
|
@@ -4111,6 +4168,7 @@ function parseMediaCatalog(body) {
|
|
|
4111
4168
|
const name = text$4(entry.display_name) ?? id;
|
|
4112
4169
|
const description = text$4(entry.description);
|
|
4113
4170
|
const pricing = parsePricing(entry.media_pricing, audio?.billingUnit);
|
|
4171
|
+
const { videoPricing, videoPricingIssue } = parseVideoPricing(field(entry, "video_pricing"));
|
|
4114
4172
|
const inputModalities = strings(capabilities.input_modalities) ?? strings(entry.input_modalities) ?? [];
|
|
4115
4173
|
const endpoints = strings(capabilities.allowed_endpoints) ?? [];
|
|
4116
4174
|
const aliases = (strings(entry.aliases) ?? []).map((alias) => alias.trim()).filter((alias) => alias !== "" && alias !== id);
|
|
@@ -4129,6 +4187,8 @@ function parseMediaCatalog(body) {
|
|
|
4129
4187
|
...aliases.length === 0 ? {} : { aliases },
|
|
4130
4188
|
...description === void 0 ? {} : { description },
|
|
4131
4189
|
...pricing === void 0 ? {} : { pricing },
|
|
4190
|
+
...videoPricing === void 0 ? {} : { videoPricing },
|
|
4191
|
+
...videoPricingIssue === void 0 ? {} : { videoPricingIssue },
|
|
4132
4192
|
...video === void 0 ? {} : { video },
|
|
4133
4193
|
...audio === void 0 ? {} : { audio },
|
|
4134
4194
|
...audioKind === void 0 ? {} : { audioKind }
|
|
@@ -4956,7 +5016,136 @@ var GatewaySearchProvider = class {
|
|
|
4956
5016
|
}
|
|
4957
5017
|
};
|
|
4958
5018
|
//#endregion
|
|
5019
|
+
//#region src/util/decimal.ts
|
|
5020
|
+
const PATTERN = /^(-?)(\d+)(?:\.(\d*))?(?:e([+-]?\d+))?$/i;
|
|
5021
|
+
/**
|
|
5022
|
+
* The exact decimal of a finite number (as written by `String`) or a decimal string.
|
|
5023
|
+
* @param value - the number or text, such as `0.5819`, `1e-7` or `"3.98066354"`.
|
|
5024
|
+
* @returns the decimal.
|
|
5025
|
+
* @throws RangeError for anything that is not a finite decimal.
|
|
5026
|
+
*/
|
|
5027
|
+
function decimal(value) {
|
|
5028
|
+
if (typeof value === "number" && !Number.isFinite(value)) throw new RangeError(`${value} is not a finite number`);
|
|
5029
|
+
const text = typeof value === "number" ? String(value) : value.trim();
|
|
5030
|
+
const match = PATTERN.exec(text);
|
|
5031
|
+
if (match === null) throw new RangeError(`"${text}" is not a decimal number`);
|
|
5032
|
+
const [, sign, whole = "0", fraction = "", exponent = "0"] = match;
|
|
5033
|
+
let digits = `${whole}${fraction}`;
|
|
5034
|
+
let scale = fraction.length - Number(exponent);
|
|
5035
|
+
if (scale < 0) {
|
|
5036
|
+
digits += "0".repeat(-scale);
|
|
5037
|
+
scale = 0;
|
|
5038
|
+
}
|
|
5039
|
+
const units = BigInt(digits);
|
|
5040
|
+
return {
|
|
5041
|
+
units: sign === "-" ? -units : units,
|
|
5042
|
+
scale
|
|
5043
|
+
};
|
|
5044
|
+
}
|
|
5045
|
+
/** Zero. */
|
|
5046
|
+
const ZERO = {
|
|
5047
|
+
units: 0n,
|
|
5048
|
+
scale: 0
|
|
5049
|
+
};
|
|
5050
|
+
function rescale(value, scale) {
|
|
5051
|
+
return value.units * 10n ** BigInt(scale - value.scale);
|
|
5052
|
+
}
|
|
5053
|
+
/**
|
|
5054
|
+
* The sum of two decimals.
|
|
5055
|
+
* @param a - the first.
|
|
5056
|
+
* @param b - the second.
|
|
5057
|
+
* @returns `a + b`, exactly.
|
|
5058
|
+
*/
|
|
5059
|
+
function add(a, b) {
|
|
5060
|
+
const scale = Math.max(a.scale, b.scale);
|
|
5061
|
+
return {
|
|
5062
|
+
units: rescale(a, scale) + rescale(b, scale),
|
|
5063
|
+
scale
|
|
5064
|
+
};
|
|
5065
|
+
}
|
|
5066
|
+
/**
|
|
5067
|
+
* The product of two decimals.
|
|
5068
|
+
* @param a - the first.
|
|
5069
|
+
* @param b - the second.
|
|
5070
|
+
* @returns `a × b`, exactly.
|
|
5071
|
+
*/
|
|
5072
|
+
function multiply(a, b) {
|
|
5073
|
+
return {
|
|
5074
|
+
units: a.units * b.units,
|
|
5075
|
+
scale: a.scale + b.scale
|
|
5076
|
+
};
|
|
5077
|
+
}
|
|
5078
|
+
/**
|
|
5079
|
+
* Round half up (away from zero) to a number of decimals.
|
|
5080
|
+
* @param value - the decimal.
|
|
5081
|
+
* @param places - decimals to keep.
|
|
5082
|
+
* @returns the rounded decimal.
|
|
5083
|
+
*/
|
|
5084
|
+
function roundHalfUp(value, places) {
|
|
5085
|
+
if (value.scale <= places) return value;
|
|
5086
|
+
const factor = 10n ** BigInt(value.scale - places);
|
|
5087
|
+
const negative = value.units < 0n;
|
|
5088
|
+
const rounded = ((negative ? -value.units : value.units) + factor / 2n) / factor;
|
|
5089
|
+
return {
|
|
5090
|
+
units: negative ? -rounded : rounded,
|
|
5091
|
+
scale: places
|
|
5092
|
+
};
|
|
5093
|
+
}
|
|
5094
|
+
/**
|
|
5095
|
+
* Round up to a whole number (towards positive infinity).
|
|
5096
|
+
* @param value - the decimal.
|
|
5097
|
+
* @returns the smallest whole number not below it.
|
|
5098
|
+
*/
|
|
5099
|
+
function ceil(value) {
|
|
5100
|
+
if (value.scale <= 0) return value;
|
|
5101
|
+
const factor = 10n ** BigInt(value.scale);
|
|
5102
|
+
return {
|
|
5103
|
+
units: value.units / factor + (value.units > 0n && value.units % factor !== 0n ? 1n : 0n),
|
|
5104
|
+
scale: 0
|
|
5105
|
+
};
|
|
5106
|
+
}
|
|
5107
|
+
/**
|
|
5108
|
+
* Exact text without trailing zeros (`2.9095`, `5`), padded to at least `minFraction` decimals.
|
|
5109
|
+
* @param value - the decimal.
|
|
5110
|
+
* @param minFraction - decimals always shown (`5.00` with 2).
|
|
5111
|
+
* @returns the text.
|
|
5112
|
+
*/
|
|
5113
|
+
function format(value, minFraction = 0) {
|
|
5114
|
+
const negative = value.units < 0n;
|
|
5115
|
+
const digits = (negative ? -value.units : value.units).toString().padStart(value.scale + 1, "0");
|
|
5116
|
+
const whole = digits.slice(0, digits.length - value.scale);
|
|
5117
|
+
const fraction = digits.slice(digits.length - value.scale).replace(/0+$/, "").padEnd(minFraction, "0");
|
|
5118
|
+
const text = fraction === "" ? whole : `${whole}.${fraction}`;
|
|
5119
|
+
return negative && /[1-9]/.test(text) ? `-${text}` : text;
|
|
5120
|
+
}
|
|
5121
|
+
/**
|
|
5122
|
+
* The nearest number, for fields typed as numbers (never for money amounts).
|
|
5123
|
+
* @param value - the decimal.
|
|
5124
|
+
* @returns the number.
|
|
5125
|
+
*/
|
|
5126
|
+
function toNumber(value) {
|
|
5127
|
+
return Number(format(value));
|
|
5128
|
+
}
|
|
5129
|
+
//#endregion
|
|
4959
5130
|
//#region src/pricing.ts
|
|
5131
|
+
/**
|
|
5132
|
+
* Price estimates from the catalog's `media_pricing` (selling prices with the
|
|
5133
|
+
* account multiplier applied). The gateway estimates the same way at admission:
|
|
5134
|
+
* per-second video prices times billed seconds, where billed seconds are the
|
|
5135
|
+
* output length plus the summed reference video length, rounded up once.
|
|
5136
|
+
* Estimates are for the user's information; the gateway's receipt is the charge.
|
|
5137
|
+
*
|
|
5138
|
+
* Video models priced per video Token (`video_pricing`) are quoted by
|
|
5139
|
+
* {@link quoteVideo}: the matching rule's per-second estimate times the output
|
|
5140
|
+
* length plus every reference video's length, in exact decimals. The final
|
|
5141
|
+
* charge is the provider's billable Tokens times the rule's Token price, so a
|
|
5142
|
+
* Token price is never shown as an unconditional per-second price, never as
|
|
5143
|
+
* 未定价 and never as free — and what cannot be estimated says so.
|
|
5144
|
+
*
|
|
5145
|
+
* A model without a price is "not priced" (未定价), never free: only a price the
|
|
5146
|
+
* catalog states as zero is shown as ¥0.
|
|
5147
|
+
* @module dsh-vibedev/pricing
|
|
5148
|
+
*/
|
|
4960
5149
|
/** Shown wherever the catalog gives no price. */
|
|
4961
5150
|
const UNPRICED = {
|
|
4962
5151
|
en: "not priced",
|
|
@@ -5013,13 +5202,52 @@ function unitSuffix(unit, kind) {
|
|
|
5013
5202
|
};
|
|
5014
5203
|
}
|
|
5015
5204
|
}
|
|
5205
|
+
/** Shown for a model whose `video_pricing` this plugin cannot use. */
|
|
5206
|
+
const VIDEO_PRICING_UNSUPPORTED = {
|
|
5207
|
+
en: "this plugin version does not support this video pricing mode: estimate unavailable; billed by actual video Token usage",
|
|
5208
|
+
zh: "当前插件版本暂不支持此视频计价方式的预估;按实际视频 Token 用量结算"
|
|
5209
|
+
};
|
|
5210
|
+
/** `480p 无参考视频、有参考音频` / `480p without reference video and with reference audio`. */
|
|
5211
|
+
function describeRuleConditions(rule, lang) {
|
|
5212
|
+
const parts = [
|
|
5213
|
+
[
|
|
5214
|
+
rule.referenceVideo,
|
|
5215
|
+
"reference video",
|
|
5216
|
+
"参考视频"
|
|
5217
|
+
],
|
|
5218
|
+
[
|
|
5219
|
+
rule.referenceAudio,
|
|
5220
|
+
"reference audio",
|
|
5221
|
+
"参考音频"
|
|
5222
|
+
],
|
|
5223
|
+
[
|
|
5224
|
+
rule.referenceImage,
|
|
5225
|
+
"reference image",
|
|
5226
|
+
"参考图"
|
|
5227
|
+
]
|
|
5228
|
+
].flatMap(([value, en, zh]) => value === void 0 ? [] : [lang === "zh" ? `${value ? "有" : "无"}${zh}` : `${value ? "with" : "without"} ${en}`]);
|
|
5229
|
+
return parts.length === 0 ? rule.resolution : `${rule.resolution} ${parts.join(lang === "zh" ? "、" : " and ")}`;
|
|
5230
|
+
}
|
|
5231
|
+
/** Every rule of a video Token pricing, each with its Token price and its per-second estimate (or that it has none). */
|
|
5232
|
+
function describeTokenPricing(pricing, lang) {
|
|
5233
|
+
const rules = pricing.rules.map((rule) => {
|
|
5234
|
+
const where = describeRuleConditions(rule, lang);
|
|
5235
|
+
const perMillion = exactYuan(rule.pricePerMillionTokensCny);
|
|
5236
|
+
const perSecond = rule.estimatedPricePerSecondCny === void 0 ? void 0 : exactYuan(rule.estimatedPricePerSecondCny);
|
|
5237
|
+
return lang === "zh" ? `${where} ${perMillion}/百万 Token(${perSecond === void 0 ? "暂无每秒预估" : `预计 ${perSecond}/秒`})` : `${where} ${perMillion} per million Tokens (${perSecond === void 0 ? "no per-second estimate" : `about ${perSecond}/s`})`;
|
|
5238
|
+
});
|
|
5239
|
+
return lang === "zh" ? `视频 Token 计价:${rules.join(";")}` : `video Token pricing (billed by actual video Tokens): ${rules.join(", ")}`;
|
|
5240
|
+
}
|
|
5016
5241
|
/**
|
|
5017
5242
|
* A one-line price for listings: `¥0.10 per image`, `¥0.99/s (720p)`; `not priced` without a price.
|
|
5243
|
+
* A video Token price lists its rules (`视频 Token 计价:480p 无参考视频 ¥60.06/百万 Token(预计 ¥0.5819/秒)…`).
|
|
5018
5244
|
* @param model - the model.
|
|
5019
5245
|
* @param lang - the language.
|
|
5020
5246
|
* @returns the price line.
|
|
5021
5247
|
*/
|
|
5022
5248
|
function describePrice(model, lang = "en") {
|
|
5249
|
+
if (model.videoPricingIssue !== void 0) return VIDEO_PRICING_UNSUPPORTED[lang];
|
|
5250
|
+
if (model.videoPricing !== void 0) return describeTokenPricing(model.videoPricing, lang);
|
|
5023
5251
|
const pricing = model.pricing;
|
|
5024
5252
|
if (pricing === void 0) return UNPRICED[lang];
|
|
5025
5253
|
const per = (unit) => unitSuffix(unit, model.kind)[lang];
|
|
@@ -5041,31 +5269,11 @@ function estimateImages(model, count) {
|
|
|
5041
5269
|
basis: count === 1 ? `${yuan(price.amount)} per image` : `${yuan(price.amount)} × ${count} images`
|
|
5042
5270
|
};
|
|
5043
5271
|
}
|
|
5044
|
-
/**
|
|
5045
|
-
|
|
5046
|
-
* @param model - the video model.
|
|
5047
|
-
* @param duration - the requested length in seconds, when given.
|
|
5048
|
-
* @param resolution - the requested resolution, when given.
|
|
5049
|
-
* @param referenceMs - the summed length of the reference videos.
|
|
5050
|
-
* @returns the estimate, or undefined without a price.
|
|
5051
|
-
*/
|
|
5052
|
-
function estimateVideo(model, duration, resolution, referenceMs = 0) {
|
|
5272
|
+
/** The legacy `media_pricing` price a video request pays: its resolution's tier, the only offered one, or the default. */
|
|
5273
|
+
function legacyVideoPrice(model, resolution) {
|
|
5053
5274
|
if (model.pricing === void 0) return void 0;
|
|
5054
5275
|
const only = model.video?.resolutions?.length === 1 ? model.video.resolutions[0] : void 0;
|
|
5055
|
-
|
|
5056
|
-
if (price === void 0) return void 0;
|
|
5057
|
-
if (price.unit === "generation") return {
|
|
5058
|
-
amountCny: price.amount,
|
|
5059
|
-
basis: `${yuan(price.amount)} per video`
|
|
5060
|
-
};
|
|
5061
|
-
if (price.unit !== "second") return void 0;
|
|
5062
|
-
const output = duration ?? DEFAULT_BILLED_SECONDS;
|
|
5063
|
-
const reference = Math.ceil(referenceMs / 1e3);
|
|
5064
|
-
const seconds = output + reference;
|
|
5065
|
-
return {
|
|
5066
|
-
amountCny: price.amount * seconds,
|
|
5067
|
-
basis: `${yuan(price.amount)}/s × ${reference === 0 ? `${output} s` : `(${output} s output + ${reference} s of reference video)`}`
|
|
5068
|
-
};
|
|
5276
|
+
return tierFor(model.pricing, resolution ?? only);
|
|
5069
5277
|
}
|
|
5070
5278
|
/**
|
|
5071
5279
|
* Quote an audio request: music, podcast and speech generation, or a transcription.
|
|
@@ -5130,6 +5338,316 @@ function quoteAudio(model, inputSeconds) {
|
|
|
5130
5338
|
}
|
|
5131
5339
|
};
|
|
5132
5340
|
}
|
|
5341
|
+
/** Shown wherever video Token pricing gives no estimate. */
|
|
5342
|
+
const TOKEN_UNAVAILABLE = {
|
|
5343
|
+
en: "estimate unavailable; billed by actual video Token usage",
|
|
5344
|
+
zh: "无法预估;按实际视频 Token 用量结算"
|
|
5345
|
+
};
|
|
5346
|
+
const BILLED_BY_TOKENS = {
|
|
5347
|
+
en: "billed by actual video Token usage",
|
|
5348
|
+
zh: "按实际视频 Token 用量结算"
|
|
5349
|
+
};
|
|
5350
|
+
/** An exact price: `¥0.6952`, `¥36.036`, `¥60.00` (at least two decimals, never rounded). */
|
|
5351
|
+
function exactYuan(amount) {
|
|
5352
|
+
return `¥${format(decimal(amount), 2)}`;
|
|
5353
|
+
}
|
|
5354
|
+
/** A display amount: half up to two decimals (`2.9095` → `¥2.91`). */
|
|
5355
|
+
function roundedYuan(amount) {
|
|
5356
|
+
return `¥${format(roundHalfUp(amount, 2), 2)}`;
|
|
5357
|
+
}
|
|
5358
|
+
const positiveSeconds = (value) => typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
5359
|
+
const wholeCount = (value) => value === void 0 || typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
5360
|
+
/** What is wrong with a request, in both languages, or undefined when it can be quoted. */
|
|
5361
|
+
function requestProblem(model, request) {
|
|
5362
|
+
if (model.kind !== "video") return {
|
|
5363
|
+
en: `${model.id} is not a video model.`,
|
|
5364
|
+
zh: `${model.id} 不是视频模型。`
|
|
5365
|
+
};
|
|
5366
|
+
if (request.durationSeconds !== void 0 && !positiveSeconds(request.durationSeconds)) return {
|
|
5367
|
+
en: "The output duration must be a positive number of seconds.",
|
|
5368
|
+
zh: "输出时长必须是正数秒。"
|
|
5369
|
+
};
|
|
5370
|
+
if (request.resolution !== void 0 && typeof request.resolution !== "string") return {
|
|
5371
|
+
en: "The resolution must be text.",
|
|
5372
|
+
zh: "分辨率必须是文本。"
|
|
5373
|
+
};
|
|
5374
|
+
const references = request.references;
|
|
5375
|
+
if (references === void 0) return void 0;
|
|
5376
|
+
if (typeof references !== "object" || references === null) return {
|
|
5377
|
+
en: "references must be an object.",
|
|
5378
|
+
zh: "references 必须是对象。"
|
|
5379
|
+
};
|
|
5380
|
+
if (!wholeCount(references.images) || !wholeCount(references.audios)) return {
|
|
5381
|
+
en: "Reference image and audio counts must be whole numbers of at least 0.",
|
|
5382
|
+
zh: "参考图和参考音频的数量必须是不小于 0 的整数。"
|
|
5383
|
+
};
|
|
5384
|
+
if (references.videos !== void 0) {
|
|
5385
|
+
if (!Array.isArray(references.videos)) return {
|
|
5386
|
+
en: "references.videos must be a list.",
|
|
5387
|
+
zh: "references.videos 必须是列表。"
|
|
5388
|
+
};
|
|
5389
|
+
for (const [index, video] of references.videos.entries()) if (typeof video !== "object" || video === null || video.durationSeconds !== void 0 && !positiveSeconds(video.durationSeconds)) return {
|
|
5390
|
+
en: `Reference video ${index + 1} must give its length as a positive number of seconds, or none when unknown.`,
|
|
5391
|
+
zh: `第 ${index + 1} 个参考视频的时长必须是正数秒(未知时不填)。`
|
|
5392
|
+
};
|
|
5393
|
+
}
|
|
5394
|
+
}
|
|
5395
|
+
/** The resolutions the catalog offers for a model, across its usable modes. */
|
|
5396
|
+
function offeredResolutions(model) {
|
|
5397
|
+
const video = model.video;
|
|
5398
|
+
if (video === void 0) return [];
|
|
5399
|
+
const modes = Object.values(effectiveVideoModes(video));
|
|
5400
|
+
const lists = modes.length === 0 ? [video.resolutions ?? []] : modes.map((mode) => mode?.resolutions ?? video.resolutions ?? []);
|
|
5401
|
+
const offered = /* @__PURE__ */ new Map();
|
|
5402
|
+
for (const list of lists) for (const item of list) if (!offered.has(item.toLowerCase())) offered.set(item.toLowerCase(), item);
|
|
5403
|
+
return [...offered.values()];
|
|
5404
|
+
}
|
|
5405
|
+
function describePresence(resolution, presence) {
|
|
5406
|
+
const has = (present, en, zh) => ({
|
|
5407
|
+
en: `${present ? "with" : "without"} ${en}`,
|
|
5408
|
+
zh: `${present ? "有" : "无"}${zh}`
|
|
5409
|
+
});
|
|
5410
|
+
const parts = [
|
|
5411
|
+
has(presence.video, "reference video", "参考视频"),
|
|
5412
|
+
has(presence.audio, "reference audio", "参考音频"),
|
|
5413
|
+
has(presence.image, "reference image", "参考图")
|
|
5414
|
+
];
|
|
5415
|
+
return {
|
|
5416
|
+
en: `${resolution} ${parts.map((part) => part.en).join(", ")}`,
|
|
5417
|
+
zh: `${resolution}、${parts.map((part) => part.zh).join("、")}`
|
|
5418
|
+
};
|
|
5419
|
+
}
|
|
5420
|
+
const specificity = (rule) => [
|
|
5421
|
+
rule.referenceVideo,
|
|
5422
|
+
rule.referenceAudio,
|
|
5423
|
+
rule.referenceImage
|
|
5424
|
+
].filter((item) => item !== void 0).length;
|
|
5425
|
+
const matches = (expected, present) => expected === void 0 || expected === present;
|
|
5426
|
+
function ruleOf(rule) {
|
|
5427
|
+
return {
|
|
5428
|
+
resolution: rule.resolution,
|
|
5429
|
+
...rule.referenceVideo === void 0 ? {} : { referenceVideo: rule.referenceVideo },
|
|
5430
|
+
...rule.referenceAudio === void 0 ? {} : { referenceAudio: rule.referenceAudio },
|
|
5431
|
+
...rule.referenceImage === void 0 ? {} : { referenceImage: rule.referenceImage }
|
|
5432
|
+
};
|
|
5433
|
+
}
|
|
5434
|
+
/** Seconds as written: `7`, `7.5`. */
|
|
5435
|
+
const secondsText = (value) => format(value);
|
|
5436
|
+
function quoteTokens(model, pricing, request) {
|
|
5437
|
+
const unavailable = (reason, basis, pricePerMillion) => ({
|
|
5438
|
+
status: "unavailable",
|
|
5439
|
+
mode: "video_token",
|
|
5440
|
+
model: model.id,
|
|
5441
|
+
reason,
|
|
5442
|
+
...pricePerMillion === void 0 ? {} : { pricePerMillionTokensCny: pricePerMillion },
|
|
5443
|
+
label: TOKEN_UNAVAILABLE,
|
|
5444
|
+
basis
|
|
5445
|
+
});
|
|
5446
|
+
let resolution = request.resolution?.trim();
|
|
5447
|
+
if (resolution === void 0 || resolution === "") {
|
|
5448
|
+
const offered = offeredResolutions(model);
|
|
5449
|
+
if (offered.length !== 1) return unavailable("missing-resolution", {
|
|
5450
|
+
en: `A resolution is needed to find the video Token price; ${BILLED_BY_TOKENS.en}.`,
|
|
5451
|
+
zh: `需要选择分辨率才能确定视频 Token 价格;${BILLED_BY_TOKENS.zh}。`
|
|
5452
|
+
});
|
|
5453
|
+
resolution = offered[0];
|
|
5454
|
+
}
|
|
5455
|
+
const videos = request.references?.videos ?? [];
|
|
5456
|
+
const presence = {
|
|
5457
|
+
video: videos.length > 0,
|
|
5458
|
+
audio: (request.references?.audios ?? 0) > 0,
|
|
5459
|
+
image: (request.references?.images ?? 0) > 0
|
|
5460
|
+
};
|
|
5461
|
+
const wanted = resolution.toLowerCase();
|
|
5462
|
+
const candidates = pricing.rules.filter((rule) => rule.resolution.trim().toLowerCase() === wanted && matches(rule.referenceVideo, presence.video) && matches(rule.referenceAudio, presence.audio) && matches(rule.referenceImage, presence.image));
|
|
5463
|
+
const asked = describePresence(resolution, presence);
|
|
5464
|
+
if (candidates.length === 0) return unavailable("no-matching-rule", {
|
|
5465
|
+
en: `The catalog has no video Token price for ${asked.en}; ${BILLED_BY_TOKENS.en}.`,
|
|
5466
|
+
zh: `目录中没有适用于 ${asked.zh} 的视频 Token 价格;${BILLED_BY_TOKENS.zh}。`
|
|
5467
|
+
});
|
|
5468
|
+
const top = Math.max(...candidates.map(specificity));
|
|
5469
|
+
const best = candidates.filter((rule) => specificity(rule) === top);
|
|
5470
|
+
if (best.length > 1) return unavailable("ambiguous-rules", {
|
|
5471
|
+
en: `Several video Token prices in the catalog apply equally to ${asked.en}, so the price cannot be determined; ${BILLED_BY_TOKENS.en}.`,
|
|
5472
|
+
zh: `目录中有多条视频 Token 价格同样适用于 ${asked.zh},无法确定价格;${BILLED_BY_TOKENS.zh}。`
|
|
5473
|
+
});
|
|
5474
|
+
const rule = best[0];
|
|
5475
|
+
const perMillion = rule.pricePerMillionTokensCny;
|
|
5476
|
+
const tokenPrice = {
|
|
5477
|
+
en: `${exactYuan(perMillion)} per million Tokens`,
|
|
5478
|
+
zh: `${exactYuan(perMillion)}/百万 Token`
|
|
5479
|
+
};
|
|
5480
|
+
const perSecond = rule.estimatedPricePerSecondCny;
|
|
5481
|
+
if (perSecond === void 0) return unavailable("no-estimated-price", {
|
|
5482
|
+
en: `The catalog gives no per-second estimate; ${tokenPrice.en}, ${BILLED_BY_TOKENS.en}.`,
|
|
5483
|
+
zh: `目录未给出每秒预估价;${tokenPrice.zh},${BILLED_BY_TOKENS.zh}。`
|
|
5484
|
+
}, perMillion);
|
|
5485
|
+
if (request.durationSeconds === void 0) return unavailable("missing-duration", {
|
|
5486
|
+
en: `An output duration is needed for an estimate; ${tokenPrice.en}, ${BILLED_BY_TOKENS.en}.`,
|
|
5487
|
+
zh: `需要输出时长才能预估;${tokenPrice.zh},${BILLED_BY_TOKENS.zh}。`
|
|
5488
|
+
}, perMillion);
|
|
5489
|
+
if (videos.some((video) => video.durationSeconds === void 0)) return unavailable("unknown-reference-duration", {
|
|
5490
|
+
en: `A reference video's length is unknown, so no estimate is possible; ${tokenPrice.en}, ${BILLED_BY_TOKENS.en}.`,
|
|
5491
|
+
zh: `参考视频时长未知,无法预估;${tokenPrice.zh},${BILLED_BY_TOKENS.zh}。`
|
|
5492
|
+
}, perMillion);
|
|
5493
|
+
const output = decimal(request.durationSeconds);
|
|
5494
|
+
const reference = videos.reduce((sum, video) => add(sum, decimal(video.durationSeconds)), ZERO);
|
|
5495
|
+
const billed = add(output, reference);
|
|
5496
|
+
const amount = multiply(decimal(perSecond), billed);
|
|
5497
|
+
const rate = exactYuan(perSecond);
|
|
5498
|
+
const seconds = presence.video ? {
|
|
5499
|
+
en: `(${secondsText(output)} s output + ${secondsText(reference)} s of reference video)`,
|
|
5500
|
+
zh: `(${secondsText(output)} 秒输出 + ${secondsText(reference)} 秒参考视频)`
|
|
5501
|
+
} : {
|
|
5502
|
+
en: `${secondsText(output)} s`,
|
|
5503
|
+
zh: `${secondsText(output)} 秒`
|
|
5504
|
+
};
|
|
5505
|
+
return {
|
|
5506
|
+
status: "estimate",
|
|
5507
|
+
mode: "video_token",
|
|
5508
|
+
model: model.id,
|
|
5509
|
+
currency: "CNY",
|
|
5510
|
+
amountCny: format(amount),
|
|
5511
|
+
estimatedPricePerSecondCny: perSecond,
|
|
5512
|
+
pricePerMillionTokensCny: perMillion,
|
|
5513
|
+
outputSeconds: request.durationSeconds,
|
|
5514
|
+
referenceVideoSeconds: toNumber(reference),
|
|
5515
|
+
billedSeconds: toNumber(billed),
|
|
5516
|
+
rule: ruleOf(rule),
|
|
5517
|
+
estimateOnly: true,
|
|
5518
|
+
finalBasis: "provider_tokens",
|
|
5519
|
+
label: {
|
|
5520
|
+
en: `about ${roundedYuan(amount)} · billed by actual video Tokens`,
|
|
5521
|
+
zh: `预计 ${roundedYuan(amount)} · 最终按实际视频 Token 结算`
|
|
5522
|
+
},
|
|
5523
|
+
basis: {
|
|
5524
|
+
en: `${rate}/s × ${seconds.en}, ${tokenPrice.en}`,
|
|
5525
|
+
zh: `${rate}/秒 × ${seconds.zh},${tokenPrice.zh}`
|
|
5526
|
+
}
|
|
5527
|
+
};
|
|
5528
|
+
}
|
|
5529
|
+
function quoteLegacy(model, request) {
|
|
5530
|
+
const resolution = request.resolution?.trim();
|
|
5531
|
+
const price = legacyVideoPrice(model, resolution === "" ? void 0 : resolution);
|
|
5532
|
+
if (price === void 0 || price.unit !== "generation" && price.unit !== "second") return {
|
|
5533
|
+
status: "unavailable",
|
|
5534
|
+
mode: "legacy",
|
|
5535
|
+
model: model.id,
|
|
5536
|
+
reason: "no-price",
|
|
5537
|
+
label: UNPRICED,
|
|
5538
|
+
basis: {
|
|
5539
|
+
en: "The catalog states no price for this request; the gateway receipt shows the charge.",
|
|
5540
|
+
zh: "目录未给出此请求的价格,以网关结算为准。"
|
|
5541
|
+
}
|
|
5542
|
+
};
|
|
5543
|
+
const estimate = (amount, basis) => {
|
|
5544
|
+
const shown = yuan(toNumber(amount));
|
|
5545
|
+
return {
|
|
5546
|
+
status: "estimate",
|
|
5547
|
+
mode: "legacy",
|
|
5548
|
+
model: model.id,
|
|
5549
|
+
currency: "CNY",
|
|
5550
|
+
amountCny: format(amount),
|
|
5551
|
+
label: {
|
|
5552
|
+
en: `about ${shown}`,
|
|
5553
|
+
zh: `约 ${shown}`
|
|
5554
|
+
},
|
|
5555
|
+
basis
|
|
5556
|
+
};
|
|
5557
|
+
};
|
|
5558
|
+
if (price.unit === "generation") return estimate(decimal(price.amount), {
|
|
5559
|
+
en: `${yuan(price.amount)} per video`,
|
|
5560
|
+
zh: `${yuan(price.amount)}/次`
|
|
5561
|
+
});
|
|
5562
|
+
const videos = request.references?.videos ?? [];
|
|
5563
|
+
if (videos.some((video) => video.durationSeconds === void 0)) return {
|
|
5564
|
+
status: "unavailable",
|
|
5565
|
+
mode: "legacy",
|
|
5566
|
+
model: model.id,
|
|
5567
|
+
reason: "unknown-reference-duration",
|
|
5568
|
+
label: {
|
|
5569
|
+
en: "estimate unavailable",
|
|
5570
|
+
zh: "无法预估"
|
|
5571
|
+
},
|
|
5572
|
+
basis: {
|
|
5573
|
+
en: `A reference video's length is unknown, so ${yuan(price.amount)}/s × (output + reference video seconds) cannot be estimated; the gateway receipt shows the charge.`,
|
|
5574
|
+
zh: `参考视频时长未知,无法按 ${yuan(price.amount)}/秒 × (输出 + 参考视频秒数) 预估,以网关结算为准。`
|
|
5575
|
+
}
|
|
5576
|
+
};
|
|
5577
|
+
const output = decimal(request.durationSeconds ?? DEFAULT_BILLED_SECONDS);
|
|
5578
|
+
const reference = ceil(videos.reduce((sum, video) => add(sum, decimal(video.durationSeconds)), ZERO));
|
|
5579
|
+
const amount = multiply(decimal(price.amount), add(output, reference));
|
|
5580
|
+
const plain = reference.units === 0n;
|
|
5581
|
+
return estimate(amount, {
|
|
5582
|
+
en: `${yuan(price.amount)}/s × ${plain ? `${format(output)} s` : `(${format(output)} s output + ${format(reference)} s of reference video)`}`,
|
|
5583
|
+
zh: `${yuan(price.amount)}/秒 × ${plain ? `${format(output)} 秒` : `(${format(output)} 秒输出 + ${format(reference)} 秒参考视频)`}`
|
|
5584
|
+
});
|
|
5585
|
+
}
|
|
5586
|
+
/**
|
|
5587
|
+
* Quote a video request from the catalog alone: no upload, no submission.
|
|
5588
|
+
*
|
|
5589
|
+
* Video Token pricing: the unique most specific rule for the resolution and
|
|
5590
|
+
* the presence of reference video, audio and images; its per-second estimate
|
|
5591
|
+
* times the output length plus every reference video's length. A model whose
|
|
5592
|
+
* `video_pricing` cannot be read gets no estimate and never falls back to its
|
|
5593
|
+
* legacy price. Legacy models are quoted as {@link estimateVideo} always did.
|
|
5594
|
+
* @param model - the resolved model.
|
|
5595
|
+
* @param request - the request.
|
|
5596
|
+
* @returns the quote.
|
|
5597
|
+
*/
|
|
5598
|
+
function quoteVideo(model, request) {
|
|
5599
|
+
const mode = model.videoPricing !== void 0 || model.videoPricingIssue !== void 0 ? "video_token" : model.pricing !== void 0 ? "legacy" : "none";
|
|
5600
|
+
const problem = requestProblem(model, request);
|
|
5601
|
+
if (problem !== void 0) return {
|
|
5602
|
+
status: "unavailable",
|
|
5603
|
+
mode,
|
|
5604
|
+
model: model.id,
|
|
5605
|
+
reason: "invalid-request",
|
|
5606
|
+
label: mode === "video_token" ? TOKEN_UNAVAILABLE : {
|
|
5607
|
+
en: "estimate unavailable",
|
|
5608
|
+
zh: "无法预估"
|
|
5609
|
+
},
|
|
5610
|
+
basis: {
|
|
5611
|
+
en: `The request cannot be quoted: ${problem.en}`,
|
|
5612
|
+
zh: `无法预估此请求:${problem.zh}`
|
|
5613
|
+
}
|
|
5614
|
+
};
|
|
5615
|
+
if (model.videoPricingIssue !== void 0) return {
|
|
5616
|
+
status: "unavailable",
|
|
5617
|
+
mode,
|
|
5618
|
+
model: model.id,
|
|
5619
|
+
reason: model.videoPricingIssue === "unknown-schema" ? "unknown-schema" : "invalid-pricing",
|
|
5620
|
+
label: TOKEN_UNAVAILABLE,
|
|
5621
|
+
basis: model.videoPricingIssue === "unknown-schema" ? {
|
|
5622
|
+
en: `This plugin version does not support this video pricing mode, so no estimate is possible; ${BILLED_BY_TOKENS.en}.`,
|
|
5623
|
+
zh: `当前插件版本暂不支持此视频计价方式的预估;${BILLED_BY_TOKENS.zh}。`
|
|
5624
|
+
} : {
|
|
5625
|
+
en: `The catalog's video Token pricing cannot be read, so no estimate is possible; ${BILLED_BY_TOKENS.en}.`,
|
|
5626
|
+
zh: `目录中的视频 Token 价格无法读取,暂无法预估;${BILLED_BY_TOKENS.zh}。`
|
|
5627
|
+
}
|
|
5628
|
+
};
|
|
5629
|
+
if (model.videoPricing !== void 0) return quoteTokens(model, model.videoPricing, request);
|
|
5630
|
+
if (model.pricing !== void 0) return quoteLegacy(model, request);
|
|
5631
|
+
return {
|
|
5632
|
+
status: "unavailable",
|
|
5633
|
+
mode: "none",
|
|
5634
|
+
model: model.id,
|
|
5635
|
+
reason: "no-price",
|
|
5636
|
+
label: UNPRICED,
|
|
5637
|
+
basis: {
|
|
5638
|
+
en: "The catalog states no price for this model; the gateway receipt shows the charge.",
|
|
5639
|
+
zh: "目录未给出价格,以网关结算为准。"
|
|
5640
|
+
}
|
|
5641
|
+
};
|
|
5642
|
+
}
|
|
5643
|
+
/**
|
|
5644
|
+
* The estimate a task records: the quote amount half up to two decimals.
|
|
5645
|
+
* @param quote - the quote.
|
|
5646
|
+
* @returns `8.34`, or undefined without an estimate.
|
|
5647
|
+
*/
|
|
5648
|
+
function quotedCny(quote) {
|
|
5649
|
+
return quote.status === "estimate" ? format(roundHalfUp(decimal(quote.amountCny), 2), 2) : void 0;
|
|
5650
|
+
}
|
|
5133
5651
|
//#endregion
|
|
5134
5652
|
//#region src/audio/request.ts
|
|
5135
5653
|
/**
|
|
@@ -6376,27 +6894,44 @@ async function prepareVideoTask(runtime, call, request, save) {
|
|
|
6376
6894
|
bytes: item.media.data.byteLength,
|
|
6377
6895
|
...assets[index]?.durationMs === void 0 ? {} : { durationMs: assets[index]?.durationMs }
|
|
6378
6896
|
}] : []);
|
|
6379
|
-
|
|
6897
|
+
const audios = loaded.flatMap((item, index) => item.input === "referenceAudios" ? [{
|
|
6380
6898
|
source: item.media.source,
|
|
6381
6899
|
mime: item.media.mime,
|
|
6382
6900
|
bytes: item.media.data.byteLength,
|
|
6383
6901
|
...assets[index]?.durationMs === void 0 ? {} : { durationMs: assets[index]?.durationMs }
|
|
6384
|
-
}] : [])
|
|
6902
|
+
}] : []);
|
|
6903
|
+
checkReferenceDurations(model, videos, audios);
|
|
6385
6904
|
const urls = {};
|
|
6386
6905
|
for (const [index, item] of loaded.entries()) {
|
|
6387
6906
|
const asset = assets[index];
|
|
6388
6907
|
(urls[item.input] ??= []).push(asset.referenceUrl);
|
|
6389
6908
|
}
|
|
6390
|
-
const
|
|
6391
|
-
|
|
6909
|
+
const quote = quoteVideo(model, {
|
|
6910
|
+
model: model.id,
|
|
6911
|
+
...plan.resolution === void 0 ? {} : { resolution: plan.resolution },
|
|
6912
|
+
...plan.duration === void 0 ? {} : { durationSeconds: plan.duration },
|
|
6913
|
+
references: {
|
|
6914
|
+
videos: videos.map((clip) => clip.durationMs === void 0 ? {} : { durationSeconds: clip.durationMs / 1e3 }),
|
|
6915
|
+
images: loaded.filter((item) => INPUT_KIND[item.input] === "image").length,
|
|
6916
|
+
audios: audios.length
|
|
6917
|
+
}
|
|
6918
|
+
});
|
|
6919
|
+
const estimatedCny = quotedCny(quote);
|
|
6392
6920
|
const shape = [
|
|
6393
6921
|
plan.duration === void 0 ? void 0 : `${plan.duration} s`,
|
|
6394
6922
|
plan.aspectRatio,
|
|
6395
6923
|
plan.resolution
|
|
6396
6924
|
].filter(Boolean).join(", ");
|
|
6925
|
+
const price = quote.status === "estimate" && quote.mode === "legacy" ? {
|
|
6926
|
+
en: `, ${quote.label.en} (${quote.basis.en})`,
|
|
6927
|
+
zh: `,${quote.label.zh}`
|
|
6928
|
+
} : {
|
|
6929
|
+
en: `, ${quote.label.en} (${quote.basis.en})`,
|
|
6930
|
+
zh: `,${quote.label.zh}(${quote.basis.zh})`
|
|
6931
|
+
};
|
|
6397
6932
|
await runtime.confirmSpending(call, "video_generate", {
|
|
6398
|
-
en: `Generate a video with ${model.id} (${plan.mode}${shape === "" ? "" : `, ${shape}`})${
|
|
6399
|
-
zh: `用 ${model.id} 生成视频(${plan.mode}${shape === "" ? "" : `,${shape}`})${
|
|
6933
|
+
en: `Generate a video with ${model.id} (${plan.mode}${shape === "" ? "" : `, ${shape}`})${price.en}. Charged only if it succeeds.`,
|
|
6934
|
+
zh: `用 ${model.id} 生成视频(${plan.mode}${shape === "" ? "" : `,${shape}`})${price.zh},成功才扣费。`
|
|
6400
6935
|
});
|
|
6401
6936
|
const now = runtime.now();
|
|
6402
6937
|
return {
|
|
@@ -6415,10 +6950,11 @@ async function prepareVideoTask(runtime, call, request, save) {
|
|
|
6415
6950
|
endpoint: "/v1/videos",
|
|
6416
6951
|
body: videoRequestBody(model.id, request.prompt, plan, urls),
|
|
6417
6952
|
status: "submitting",
|
|
6418
|
-
...
|
|
6953
|
+
...estimatedCny === void 0 ? {} : { estimatedCny }
|
|
6419
6954
|
},
|
|
6420
6955
|
inputs: loaded.map((item) => item.input),
|
|
6421
|
-
shape
|
|
6956
|
+
shape,
|
|
6957
|
+
quote
|
|
6422
6958
|
};
|
|
6423
6959
|
}
|
|
6424
6960
|
/**
|
|
@@ -6429,7 +6965,7 @@ async function prepareVideoTask(runtime, call, request, save) {
|
|
|
6429
6965
|
function videoGenerateTool(runtime) {
|
|
6430
6966
|
return defineTool({
|
|
6431
6967
|
name: "video_generate",
|
|
6432
|
-
description: "Generate a video with a VibeDev video model. It runs as a background job (usually 1-10 minutes): this call returns once the gateway accepts the task, you are notified when it finishes, and the file is saved in the workspace (media/videos/). Modes: text_to_video (prompt only), first_frame (animate an image), first_last_frame (animate from one image to another), omni_reference (reference images, videos and audio guide characters, motion, style and sound). Each model supports different modes, reference counts, durations, aspect ratios and resolutions; media_models lists them, and requests a model cannot serve are refused before anything is uploaded or charged. Charged per second of video
|
|
6968
|
+
description: "Generate a video with a VibeDev video model. It runs as a background job (usually 1-10 minutes): this call returns once the gateway accepts the task, you are notified when it finishes, and the file is saved in the workspace (media/videos/). Modes: text_to_video (prompt only), first_frame (animate an image), first_last_frame (animate from one image to another), omni_reference (reference images, videos and audio guide characters, motion, style and sound). Each model supports different modes, reference counts, durations, aspect ratios and resolutions; media_models lists them, and requests a model cannot serve are refused before anything is uploaded or charged. Charged at the model's catalog price (per second of video plus reference video length, per generation, or by actual video Tokens; media_models lists it) only when it succeeds; a running task cannot be cancelled.",
|
|
6433
6969
|
parameters: {
|
|
6434
6970
|
prompt: {
|
|
6435
6971
|
type: "string",
|
|
@@ -6617,8 +7153,9 @@ const view = (record) => ({
|
|
|
6617
7153
|
...record.outputs === void 0 ? {} : { outputs: record.outputs.map(({ index: _index, bytes: _bytes, ...output }) => ({ ...output })) },
|
|
6618
7154
|
...record.error === void 0 ? {} : { error: { ...record.error } },
|
|
6619
7155
|
...record.estimatedCny === void 0 ? {} : { estimatedCny: record.estimatedCny },
|
|
6620
|
-
...record.chargedCny === void 0 ? {} : { chargedCny: record.chargedCny },
|
|
7156
|
+
...record.chargedCny === void 0 || record.settlement === "pending" ? {} : { chargedCny: record.chargedCny },
|
|
6621
7157
|
...record.settlement === void 0 ? {} : { settlement: record.settlement },
|
|
7158
|
+
...record.settlement !== "pending" || record.settlementPendingReason === void 0 ? {} : { settlementPendingReason: record.settlementPendingReason },
|
|
6622
7159
|
...record.billing === void 0 ? {} : { billing: { ...record.billing } },
|
|
6623
7160
|
createdAt: record.createdAt,
|
|
6624
7161
|
updatedAt: record.updatedAt
|
|
@@ -6674,6 +7211,41 @@ var MediaHostService = class {
|
|
|
6674
7211
|
return quoteAudio(await this.runtime.findModel(model, signal));
|
|
6675
7212
|
}
|
|
6676
7213
|
/**
|
|
7214
|
+
* What a video request is expected to cost, from the (cached) catalog alone:
|
|
7215
|
+
* nothing is uploaded, submitted or recorded. Video Token pricing gives the
|
|
7216
|
+
* matching rule's estimate (`预计 ¥8.34 · 最终按实际视频 Token 结算`) or says why there
|
|
7217
|
+
* is none; legacy prices are quoted as before. A model the account does not
|
|
7218
|
+
* have is an `invalid-request`; a catalog failure (no sign-in, no network) is thrown.
|
|
7219
|
+
* @param request - the model (id or alias), resolution, duration and references.
|
|
7220
|
+
* @param signal - cancels the catalog request.
|
|
7221
|
+
* @returns the quote, under the model's canonical id.
|
|
7222
|
+
*/
|
|
7223
|
+
async videoQuote(request, signal) {
|
|
7224
|
+
const wanted = typeof request?.model === "string" ? request.model.trim() : "";
|
|
7225
|
+
let model;
|
|
7226
|
+
try {
|
|
7227
|
+
if (wanted === "") throw new MediaError("A video quote needs a model.", "MODEL_NOT_FOUND", { field: "model" });
|
|
7228
|
+
model = await this.runtime.findModel(wanted, signal);
|
|
7229
|
+
} catch (error) {
|
|
7230
|
+
if (!(error instanceof MediaError) || error.code !== "MODEL_NOT_FOUND") throw error;
|
|
7231
|
+
return {
|
|
7232
|
+
status: "unavailable",
|
|
7233
|
+
mode: "none",
|
|
7234
|
+
model: wanted,
|
|
7235
|
+
reason: "invalid-request",
|
|
7236
|
+
label: {
|
|
7237
|
+
en: "estimate unavailable",
|
|
7238
|
+
zh: "无法预估"
|
|
7239
|
+
},
|
|
7240
|
+
basis: {
|
|
7241
|
+
en: `There is no model "${wanted}" on this VibeDev account.`,
|
|
7242
|
+
zh: `当前 VibeDev 账号没有模型“${wanted}”。`
|
|
7243
|
+
}
|
|
7244
|
+
};
|
|
7245
|
+
}
|
|
7246
|
+
return quoteVideo(model, request);
|
|
7247
|
+
}
|
|
7248
|
+
/**
|
|
6677
7249
|
* Generate images now and save them.
|
|
6678
7250
|
* @param request - what to generate.
|
|
6679
7251
|
* @param target - the workspace and where the files go.
|
|
@@ -7022,23 +7594,59 @@ const seconds = (value) => {
|
|
|
7022
7594
|
const number = typeof value === "string" ? Number(value) : value;
|
|
7023
7595
|
return typeof number === "number" && Number.isFinite(number) && number > 0 ? number : void 0;
|
|
7024
7596
|
};
|
|
7025
|
-
/** The billing facts
|
|
7026
|
-
function
|
|
7027
|
-
|
|
7597
|
+
/** The blocks billing facts are read from, in order: `billing`, `usage`, `effective`, then the answer itself. */
|
|
7598
|
+
function billingBlocks(raw) {
|
|
7599
|
+
return [
|
|
7028
7600
|
record$1(raw.billing),
|
|
7029
7601
|
record$1(raw.usage),
|
|
7030
7602
|
record$1(raw.effective),
|
|
7031
7603
|
raw
|
|
7032
7604
|
].filter((block) => block !== void 0);
|
|
7033
|
-
|
|
7034
|
-
|
|
7035
|
-
|
|
7036
|
-
|
|
7037
|
-
|
|
7038
|
-
|
|
7039
|
-
|
|
7605
|
+
}
|
|
7606
|
+
function pickFrom(blocks, ...names) {
|
|
7607
|
+
for (const block of blocks) for (const name of names) if (block[name] !== void 0 && block[name] !== null) return block[name];
|
|
7608
|
+
}
|
|
7609
|
+
const SETTLEMENT_FIELDS = [
|
|
7610
|
+
"billing_status",
|
|
7611
|
+
"settlement_state",
|
|
7612
|
+
"settlement_status"
|
|
7613
|
+
];
|
|
7614
|
+
const tokens = (value) => {
|
|
7615
|
+
const number = typeof value === "string" && /^\d+$/.test(value.trim()) ? Number(value) : value;
|
|
7616
|
+
return typeof number === "number" && Number.isSafeInteger(number) && number >= 0 ? number : void 0;
|
|
7617
|
+
};
|
|
7618
|
+
/**
|
|
7619
|
+
* Whether a task's charge settled, for video and audio alike. An explicit
|
|
7620
|
+
* pending (anywhere in the answer) wins; settled needs an explicit word. Whether
|
|
7621
|
+
* any status was given at all is reported apart, so the tracker can tell an older
|
|
7622
|
+
* gateway (no status: a charge then implies settled) from a word it does not know.
|
|
7623
|
+
*/
|
|
7624
|
+
function settlementOf(raw, status) {
|
|
7625
|
+
const blocks = billingBlocks(raw);
|
|
7626
|
+
const words = [];
|
|
7627
|
+
let reported = status.toLowerCase() === SETTLEMENT_PENDING;
|
|
7628
|
+
for (const block of blocks) for (const name of SETTLEMENT_FIELDS) {
|
|
7629
|
+
if (block[name] === void 0 || block[name] === null) continue;
|
|
7630
|
+
reported = true;
|
|
7631
|
+
const word = text$1(block[name])?.toLowerCase();
|
|
7632
|
+
if (word !== void 0) words.push(word);
|
|
7633
|
+
}
|
|
7634
|
+
const settlement = status.toLowerCase() === SETTLEMENT_PENDING || words.includes("pending") ? "pending" : words.includes("settled") || words.includes("completed") ? "settled" : void 0;
|
|
7635
|
+
const reason = settlement === "pending" ? text$1(pickFrom(blocks, "billing_pending_reason", "settlement_pending_reason")) : void 0;
|
|
7636
|
+
const billedTokens = tokens(pickFrom(blocks, "billed_tokens"));
|
|
7040
7637
|
return {
|
|
7638
|
+
billingStatusReported: reported,
|
|
7041
7639
|
...settlement === void 0 ? {} : { settlement },
|
|
7640
|
+
...reason === void 0 ? {} : { settlementPendingReason: reason },
|
|
7641
|
+
...billedTokens === void 0 ? {} : { billedTokens }
|
|
7642
|
+
};
|
|
7643
|
+
}
|
|
7644
|
+
/** The billed lengths of an audio task: original seconds (speech) and minutes (transcription). */
|
|
7645
|
+
function billedLengths(raw) {
|
|
7646
|
+
const blocks = billingBlocks(raw);
|
|
7647
|
+
const originalSeconds = seconds(pickFrom(blocks, "original_seconds", "billed_original_seconds", "original_duration_seconds"));
|
|
7648
|
+
const minutes = seconds(pickFrom(blocks, "billed_minutes", "minutes"));
|
|
7649
|
+
return {
|
|
7042
7650
|
...originalSeconds === void 0 ? {} : { billedOriginalSeconds: originalSeconds },
|
|
7043
7651
|
...minutes === void 0 ? {} : { billedMinutes: minutes }
|
|
7044
7652
|
};
|
|
@@ -7096,6 +7704,7 @@ function parseVideoTask(body) {
|
|
|
7096
7704
|
...progress === void 0 ? {} : { progress },
|
|
7097
7705
|
...error === void 0 ? {} : { error },
|
|
7098
7706
|
...receipt(raw),
|
|
7707
|
+
...settlementOf(raw, status),
|
|
7099
7708
|
outputs: state === "completed" ? [{
|
|
7100
7709
|
index: 0,
|
|
7101
7710
|
...url === void 0 ? {} : { url },
|
|
@@ -7149,7 +7758,8 @@ function parseAudioTask(body) {
|
|
|
7149
7758
|
...progress === void 0 ? {} : { progress },
|
|
7150
7759
|
...error === void 0 ? {} : { error },
|
|
7151
7760
|
...receipt(raw),
|
|
7152
|
-
...
|
|
7761
|
+
...settlementOf(raw, status),
|
|
7762
|
+
...billedLengths(raw),
|
|
7153
7763
|
outputs: state === "completed" ? outputs : []
|
|
7154
7764
|
};
|
|
7155
7765
|
}
|
|
@@ -7659,19 +8269,36 @@ var TaskTracker = class {
|
|
|
7659
8269
|
}
|
|
7660
8270
|
return Math.min(6e4, 5e3 * 2 ** Math.min(failures - 1, 4));
|
|
7661
8271
|
}
|
|
7662
|
-
/**
|
|
8272
|
+
/**
|
|
8273
|
+
* The money and billing facts of a gateway answer, merged into what the record already knows.
|
|
8274
|
+
*
|
|
8275
|
+
* An explicit status decides: `pending` records no charge (a charge reported
|
|
8276
|
+
* alongside it is not final, and the task is not free either) and drops one
|
|
8277
|
+
* recorded earlier; `settled` records the charge exactly as given. Only an
|
|
8278
|
+
* answer that carries no billing or settlement status at all — older gateways —
|
|
8279
|
+
* lets a charge imply `settled`. An answer that says nothing leaves what the
|
|
8280
|
+
* record holds, so a settled task is never downgraded by it.
|
|
8281
|
+
*/
|
|
7663
8282
|
moneyOf(record, task) {
|
|
7664
|
-
const
|
|
7665
|
-
const
|
|
7666
|
-
const billing = task.billedOriginalSeconds !== void 0 || task.billedMinutes !== void 0 ? { billing: {
|
|
8283
|
+
const settlement = task.settlement ?? (!task.billingStatusReported && task.chargedCny !== void 0 ? "settled" : void 0);
|
|
8284
|
+
const billing = task.billedOriginalSeconds !== void 0 || task.billedMinutes !== void 0 || task.billedTokens !== void 0 ? { billing: {
|
|
7667
8285
|
...record.billing,
|
|
7668
8286
|
...task.billedOriginalSeconds === void 0 ? {} : { originalSeconds: task.billedOriginalSeconds },
|
|
7669
|
-
...task.billedMinutes === void 0 ? {} : { minutes: task.billedMinutes }
|
|
8287
|
+
...task.billedMinutes === void 0 ? {} : { minutes: task.billedMinutes },
|
|
8288
|
+
...task.billedTokens === void 0 ? {} : { tokens: task.billedTokens }
|
|
7670
8289
|
} } : {};
|
|
7671
8290
|
return {
|
|
7672
8291
|
...task.estimatedCny === void 0 ? {} : { estimatedCny: task.estimatedCny },
|
|
7673
|
-
...
|
|
7674
|
-
|
|
8292
|
+
...settlement === "pending" ? {
|
|
8293
|
+
settlement,
|
|
8294
|
+
chargedCny: void 0,
|
|
8295
|
+
settlementPendingReason: task.settlementPendingReason
|
|
8296
|
+
} : {},
|
|
8297
|
+
...settlement === "settled" ? {
|
|
8298
|
+
settlement,
|
|
8299
|
+
settlementPendingReason: void 0,
|
|
8300
|
+
...task.chargedCny === void 0 ? {} : { chargedCny: task.chargedCny }
|
|
8301
|
+
} : {},
|
|
7675
8302
|
...billing
|
|
7676
8303
|
};
|
|
7677
8304
|
}
|