@evolinkai/cli 0.14.0 → 0.14.1

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.
@@ -7,13 +7,15 @@ import { prepareSeedanceInput } from './seedance-input.js';
7
7
  import { searchUsage } from './search-usage.js';
8
8
  import { CONSERVATIVE_QUOTE_PARAMETERS } from './pricing-derived.js';
9
9
  import { INPUT_IMAGE_TOKENS, isTokenImageModel, tokenImageReservation } from './gpt-image-tokens.js';
10
- import { OMNI_IMAGE_TOKENS, OMNI_UNREAD_TIER, isOmniTokenModel, omniLowerInputTier, omniReservation } from './omni-tokens.js';
10
+ import { OMNI_IMAGE_TOKENS, OMNI_UNREAD_TIER, isOmniTokenModel, omniInputTiers, omniLowerInputTier, omniQuotedTier, omniReservation } from './omni-tokens.js';
11
+ import { isQwenTTSModel, qwenTTSInput, qwenTTSReservation } from './qwen-tts-tokens.js';
11
12
  /**
12
- * How long an approval from estimate_cost is shown as current. The rules' own `fresh_until`
13
+ * How long an approval from estimate_cost holds: 24 hours from the estimate. The rules' own `fresh_until`
13
14
  * is a short refresh window (minutes); people confirming a price in a chat may come back much
14
- * later. generate_* re-evaluates the current rules and refuses with `price_changed` when the
15
- * price moved, so an approval never submits at a stale price; one past this time still holds
16
- * for the same input and price (checkRulesApproval).
15
+ * later. generate_* re-evaluates the current rules every time: it refuses an approval past this
16
+ * time (`quote_expired`) or one whose price rose (`price_changed`), and submits at an unchanged or
17
+ * lower price (checkRulesApproval). The gateway keeps a request ID for 24 hours, so a retry under
18
+ * one approval is still within that window of the first submission.
17
19
  */
18
20
  export const APPROVAL_TTL_MS = 24 * 60 * 60_000;
19
21
  function fail(code, message) { throw new QuoteError(code, message); }
@@ -170,10 +172,12 @@ export function billingUsageHelp(rule, entry) {
170
172
  const declared = new Set((rule.parameters ?? []).map(p => p.name));
171
173
  return helpLines([...declared].filter(name => isUsageParameter(entry.id, name)).sort(), entry, rule, declared);
172
174
  }
173
- // GPT Image and Gemini Omni token models: the reference price counts the tokens the gateway reserves at submission for the input.
175
+ // GPT Image, Gemini Omni and Qwen TTS token models: the reference price counts the tokens the gateway reserves at
176
+ // submission for the input.
174
177
  const RESERVED_TOKENS = new Set(['image_input_tokens', 'image_cached_input_tokens', 'image_output_tokens',
175
178
  'text_input_tokens', 'text_cached_input_tokens', 'text_output_tokens']);
176
179
  const OMNI_TOKENS = new Set(['input_tokens', 'video_output_tokens', 'other_output_tokens']);
180
+ const QWEN_TOKENS = new Set(['input_tokens', 'output_tokens']);
177
181
  /** GPT Image 2.6 rules also bill an SVG's author model. SVG output is not quoted, so a reference price uses none. */
178
182
  const NO_SVG_AUTHOR = { svg_model: 'none', svg_input_tokens: 0, svg_output_tokens: 0 };
179
183
  const svgAuthor = (name) => Object.hasOwn(NO_SVG_AUTHOR, name);
@@ -186,6 +190,10 @@ function reservesTokens(rule, entry) {
186
190
  return (rule.parameters ?? []).every(p => OMNI_TOKENS.has(p.name))
187
191
  && rule.components.some(c => c.role === 'video_output' && c.pricing_status === 'available' && componentReads(c).has('video_output_tokens'));
188
192
  }
193
+ if (isQwenTTSModel(id)) {
194
+ return (rule.parameters ?? []).every(p => QWEN_TOKENS.has(p.name))
195
+ && rule.components.some(c => c.role === 'output' && c.pricing_status === 'available' && componentReads(c).has('output_tokens'));
196
+ }
189
197
  return isTokenImageModel(id)
190
198
  && (rule.parameters ?? []).every(p => RESERVED_TOKENS.has(p.name)
191
199
  || svgAuthor(p.name) && (p.name !== 'svg_model' || (p.values ?? p.enum ?? []).includes(NO_SVG_AUTHOR.svg_model)))
@@ -198,12 +206,16 @@ export function tokenReferenceHelp(rule, entry) {
198
206
  const rates = rule.components.filter(c => ![...componentReads(c)].some(svgAuthor))
199
207
  .map(c => `${c.id} $${c.rate.usd} per ${c.unit === '1k_tokens' ? '1K tokens' : c.unit.replace(/_/g, ' ')}`);
200
208
  if (isOmniTokenModel(entry.id.toLowerCase())) {
201
- return `Billed by actual token usage. estimate_cost gives a reference price for the exact input: the tokens EvoLink reserves at submission for its resolution, duration, prompt, images and input videos, at the published rates (public default rates: ${rates.join('; ')}). Give an input or reference video's length as media_seconds; its resolution is read at submission, so the reference counts the highest (${OMNI_UNREAD_TIER}). The task settles at the tokens it actually uses.`;
209
+ return `Billed by actual token usage. estimate_cost gives a reference price for the exact input: the tokens EvoLink reserves at submission for its resolution, duration, prompt, images and input videos, at the published rates (public default rates: ${rates.join('; ')}). Give an input or reference video's length as media_seconds; its resolution is read at submission, so the reference counts the highest (${OMNI_UNREAD_TIER}) and the estimate also lists the price at each input resolution. The task settles at the tokens it actually uses.`;
210
+ }
211
+ if (isQwenTTSModel(entry.id)) {
212
+ return `Billed by actual token usage. estimate_cost gives a reference price for the exact input: the tokens EvoLink reserves at submission for the text (after hot_fix.replace), an instruction or [tags] and hot_fix, at the published rates (public default rates: ${rates.join('; ')}). It counts each character's worst case, so the task usually settles below it, at the tokens it actually uses.`;
202
213
  }
203
214
  return `Billed by actual token usage. estimate_cost gives a reference price for the exact input: the tokens EvoLink reserves at submission for its size, quality, n, prompt and reference images, at the published rates (public default rates: ${rates.join('; ')}). The task settles at the tokens it actually uses.`;
204
215
  }
205
- /** An Omni reservation's tokens as the rule's parameters. */
206
- const omniParameters = (rule, usage) => Object.fromEntries((rule.parameters ?? []).map(p => [p.name, usage.tokens[p.name] ?? 0]));
216
+ /** An Omni or Qwen TTS reservation's tokens as the rule's parameters. */
217
+ const tokenParameters = (rule, tokens) => Object.fromEntries((rule.parameters ?? []).map(p => [p.name, tokens[p.name] ?? 0]));
218
+ const omniParameters = (rule, usage) => tokenParameters(rule, usage.tokens);
207
219
  function reservation(rule, entry, input, countPromptTokens, mediaSeconds) {
208
220
  if (!reservesTokens(rule, entry))
209
221
  return undefined;
@@ -212,6 +224,10 @@ function reservation(rule, entry, input, countPromptTokens, mediaSeconds) {
212
224
  const usage = omniReservation(id, input, mediaSeconds, { countPromptTokens });
213
225
  return 'needs' in usage ? usage : { omni: usage, parameters: omniParameters(rule, usage) };
214
226
  }
227
+ if (isQwenTTSModel(id)) {
228
+ const usage = qwenTTSReservation(input);
229
+ return { qwen: usage, parameters: tokenParameters(rule, usage.tokens) };
230
+ }
215
231
  const usage = tokenImageReservation(id, input, countPromptTokens);
216
232
  const parameters = {};
217
233
  for (const p of rule.parameters ?? []) {
@@ -250,14 +266,18 @@ function omniBasis(usage) {
250
266
  parts.push(`${tokenText(t.other_output_tokens)} other output tokens`);
251
267
  return `Reference price: published token rates applied to what EvoLink reserves at submission for this input: ${parts.join(', ')}, each part rounded up.`;
252
268
  }
253
- function omniNotes(usage, ignored, lower) {
269
+ const tierList = (prices) => prices.map(price => `${price.resolution}${price.quoted ? ' (quoted)' : ''}: ${price.amounts.credits} credits (about $${price.amounts.usd})`).join('; ');
270
+ function omniNotes(usage, ignored, prices) {
254
271
  const notes = [CONFIRM_REFERENCE];
255
272
  const videos = usage.input_videos;
256
273
  if (videos?.resolution_assumed) {
257
- notes.push(`EvoLink reads the input video's resolution at submission; the request does not give it, so the reference counts the highest, ${videos.resolution}, as EvoLink reserves when it cannot read it.${lower ? ` A ${lower.tier} input video reserves ${lower.amounts.credits} credits (about $${lower.amounts.usd}) instead.` : ''}`);
274
+ notes.push(`EvoLink reads the input video's resolution at submission; the request does not give it, so the reference counts the highest, ${videos.resolution}, as EvoLink reserves when it cannot read it.${prices.length ? ` If the ${videos.count > 1 ? 'reference videos are' : 'input video is'} ${tierList(prices)}.` : ''}`);
275
+ const output = usage.output.resolution;
276
+ if (!usage.output.follows_input && output !== '360p')
277
+ notes.push(`An input video at or below the output resolution, ${output}, reserves the same as one at ${output}.`);
258
278
  }
259
279
  if (usage.upscale?.source_assumed) {
260
- notes.push(`EvoLink reads the source video's resolution at submission; the reference counts the source that costs most for ${usage.upscale.target}, ${usage.upscale.source}.${lower ? ` A ${lower.tier} source reserves ${lower.amounts.credits} credits (about $${lower.amounts.usd}) instead.` : ''}`);
280
+ notes.push(`EvoLink reads the source video's resolution at submission; the reference counts the source that costs most for ${usage.upscale.target}, ${usage.upscale.source}.${prices.length ? ` If the source video is ${tierList(prices)}.` : ''}`);
261
281
  }
262
282
  if (videos && usage.route !== 'upscale')
263
283
  notes.push(`media_seconds counts ${videos.count > 1 ? 'each reference video' : 'the input video'} at ${videos.seconds} s (rounded up); EvoLink measures ${videos.count > 1 ? 'each' : 'it'} at submission.`);
@@ -271,6 +291,19 @@ function omniNotes(usage, ignored, lower) {
271
291
  notes.push(ignoredNote(ignored));
272
292
  return notes;
273
293
  }
294
+ function qwenBasis(usage) {
295
+ return `Reference price: published token rates applied to what EvoLink reserves at submission for this input: ${tokenText(usage.tokens.input_tokens)} input tokens and ${tokenText(usage.tokens.output_tokens)} output tokens for ${tokenText(usage.characters)} characters of text${usage.hot_fix_characters ? ' and hot_fix' : ''}, each part rounded up.`;
296
+ }
297
+ function qwenNotes(usage, ignored) {
298
+ const notes = [CONFIRM_REFERENCE, 'EvoLink reserves the most each kind of character uses (Han characters, letters without case, lowercase letters, spaces and punctuation, other characters), so the task usually settles below the reference price.'];
299
+ if (usage.expressive)
300
+ notes.push('An instruction or a [tag] in the text raises the reserved output tokens.');
301
+ if (usage.hot_fix_characters)
302
+ notes.push('The text is counted after hot_fix.replace, as it is synthesized; hot_fix itself also counts toward the input tokens.');
303
+ if (ignored.length > 0)
304
+ notes.push(ignoredNote(ignored));
305
+ return notes;
306
+ }
274
307
  function evaluate(node, params) {
275
308
  const nodes = node.args ?? [], at = (i) => evaluate(nodes[i], params), number = (i) => numeric(at(i));
276
309
  switch (node.op) {
@@ -394,15 +427,15 @@ export function calculateRulesEstimate(body, entry, input, options = {}) {
394
427
  if (!rule || rule.model_id !== entry.id.toLowerCase() || rule.modality !== entry.kind || rule.policy_source !== 'published' || rule.pricing_status !== 'available') {
395
428
  fail('pricing_rules_unavailable', 'This model has no available published media pricing policy.');
396
429
  }
397
- const prepared = prepareSeedanceInput(entry, input);
430
+ const prepared = prepareSeedanceInput(entry, qwenTTSInput(entry.id, input));
398
431
  if (prepared.errors.length)
399
432
  throw new QuoteError('invalid_quote_parameter', `${prepared.errors[0].param} ${prepared.errors[0].problem}`, prepared.errors[0].param);
400
433
  const effectiveInput = prepared.input;
401
434
  const declared = new Set((rule.parameters ?? []).map(p => p.name));
402
435
  const id = entry.id.toLowerCase();
403
- // GPT Image and Gemini Omni token models never use token counts from pricing_parameters: the reference counts what
404
- // the gateway reserves for this input. Leaving them out of the request hash lets a generation pass them or not.
405
- const ignored = isTokenImageModel(id) || isOmniTokenModel(id)
436
+ // GPT Image, Gemini Omni and Qwen TTS token models never use token counts from pricing_parameters: the reference counts
437
+ // what the gateway reserves for this input. Leaving them out of the request hash lets a generation pass them or not.
438
+ const ignored = isTokenImageModel(id) || isOmniTokenModel(id) || isQwenTTSModel(id)
406
439
  ? Object.keys(options.pricingParameters ?? {}).filter(name => declared.has(name) && (/tokens$/.test(name) || svgAuthor(name))).sort() : [];
407
440
  const pricingParameters = ignored.length
408
441
  ? Object.fromEntries(Object.entries(options.pricingParameters).filter(([name]) => !ignored.includes(name))) : options.pricingParameters;
@@ -447,20 +480,27 @@ export function calculateRulesEstimate(body, entry, input, options = {}) {
447
480
  request_hash: requestHash, total_uc: amounts.uc, expires_at: new Date(Date.now() + APPROVAL_TTL_MS).toISOString(),
448
481
  fingerprint: quoteRequestHash(entry.id, { rules: economicRules(rule), parameters: params,
449
482
  exchange_rate_version: body.meta.exchange_rate_version, components, missing: [...missing].sort(), total_uc: amounts.uc,
450
- ...(reserved?.gpt || reserved?.omni ? { reservation: reserved.gpt ?? reserved.omni } : {}) }) };
483
+ ...(reserved?.gpt || reserved?.omni || reserved?.qwen ? { reservation: reserved.gpt ?? reserved.omni ?? reserved.qwen } : {}) }) };
451
484
  const status = missing.size ? components.length ? 'partial' : 'needs_input'
452
- : reserved?.gpt || reserved?.omni ? 'token_billed' : 'estimated';
485
+ : reserved?.gpt || reserved?.omni || reserved?.qwen ? 'token_billed' : 'estimated';
453
486
  const missingUsage = [...missing].sort(), usageHelp = helpLines(missingUsage, entry, rule, declared);
454
- const usage = reserved?.gpt, omni = reserved?.omni;
455
- // An Omni input video of unknown resolution counts the highest; the same input at a lower one, for the notes.
456
- const lowerTier = omni && status === 'token_billed' ? omniLowerInputTier(omni) : undefined;
457
- let lower;
458
- if (omni && lowerTier) {
459
- const alternative = omniReservation(id, effectiveInput, options.mediaSeconds, { countPromptTokens: options.countPromptTokens, inputTier: lowerTier });
460
- const cost = 'needs' in alternative ? undefined : chargeComponents(rule, declared, { ...params, ...omniParameters(rule, alternative) }, 1n, new Set()).total;
461
- if (cost !== undefined && cost < total)
462
- lower = { tier: lowerTier, amounts: amountsForUC(String(cost)) };
487
+ const usage = reserved?.gpt, omni = reserved?.omni, qwen = reserved?.qwen;
488
+ // An Omni input video of unknown resolution counts the highest; the same input at each resolution EvoLink can read
489
+ // it at, for the notes. lower_input keeps the one lower price 1.12.0 gave (720p; an upscale: the cheaper source).
490
+ const tierPrices = [];
491
+ if (omni && status === 'token_billed') {
492
+ const quoted = omniQuotedTier(omni);
493
+ for (const tier of omniInputTiers(omni)) {
494
+ const alternative = omniReservation(id, effectiveInput, options.mediaSeconds, { countPromptTokens: options.countPromptTokens, inputTier: tier });
495
+ if ('needs' in alternative)
496
+ continue;
497
+ const cost = chargeComponents(rule, declared, { ...params, ...omniParameters(rule, alternative) }, 1n, new Set()).total;
498
+ tierPrices.push({ resolution: tier, amounts: amountsForUC(String(cost)), ...(tier === quoted ? { quoted: true } : {}) });
499
+ }
463
500
  }
501
+ const lowerTier = omni && status === 'token_billed' ? omniLowerInputTier(omni) : undefined;
502
+ const lowerPrice = tierPrices.find(price => price.resolution === lowerTier);
503
+ const lower = lowerPrice && BigInt(lowerPrice.amounts.uc) < total ? { tier: lowerPrice.resolution, amounts: lowerPrice.amounts } : undefined;
464
504
  const estimate = { status,
465
505
  // Nothing could be priced: a zero amount would read as free.
466
506
  ...(status === 'needs_input' ? {} : { min_usd: Number(amounts.usd), max_usd: Number(amounts.usd),
@@ -469,16 +509,20 @@ export function calculateRulesEstimate(body, entry, input, options = {}) {
469
509
  basis: [status === 'token_billed' && usage
470
510
  ? `Reference price: published token rates applied to what EvoLink reserves at submission for this input. Per image: ${usage.per_image.image_output_tokens.toLocaleString('en-US')} output tokens (${usage.output.width}×${usage.output.height}${usage.output.gif ? `, ${usage.output.gif.frames} GIF frames` : ''}, quality ${usage.output.quality}), about ${usage.per_image.text_input_tokens.toLocaleString('en-US')} prompt tokens and ${usage.per_image.image_input_tokens.toLocaleString('en-US')} reference-image tokens, each part rounded up; ${usage.images} image${usage.images > 1 ? 's' : ''}.`
471
511
  : status === 'token_billed' && omni ? omniBasis(omni)
472
- : 'Published full pricing rules evaluated for the supplied generation parameters; exact UC rounding and component minimums.'],
512
+ : status === 'token_billed' && qwen ? qwenBasis(qwen)
513
+ : 'Published full pricing rules evaluated for the supplied generation parameters; exact UC rounding and component minimums.'],
473
514
  ...(missing.size ? { uncheckable_reason: 'usage', missing_usage: missingUsage, usage_help: usageHelp } : {}),
474
515
  ...(status === 'token_billed' && usage ? { reference_usage: usage } : {}),
475
- ...(status === 'token_billed' && omni ? { reference_usage: { ...omni, ...(lower ? { lower_input: { resolution: lower.tier, amounts: lower.amounts } } : {}) } } : {}),
516
+ ...(status === 'token_billed' && omni ? { reference_usage: { ...omni, ...(lower ? { lower_input: { resolution: lower.tier, amounts: lower.amounts } } : {}),
517
+ ...(tierPrices.length ? { input_resolution_prices: tierPrices } : {}) } } : {}),
518
+ ...(status === 'token_billed' && qwen ? { reference_usage: qwen } : {}),
476
519
  possible_extras: status === 'partial' ? usageHelp.map(line => `Additional charge depends on ${line}`) : [],
477
520
  notes: ['Public default prices; account discounts are not included.', ...status === 'needs_input'
478
521
  ? ['No total is quoted until the missing usage is known, and generate_* will not submit before then.']
479
522
  : status === 'token_billed' && usage ? reservationNotes(usage, ignored)
480
- : status === 'token_billed' && omni ? omniNotes(omni, ignored, lower)
481
- : ['Confirm the estimated cost before generation. Actual billed usage and final charges may differ.']] };
523
+ : status === 'token_billed' && omni ? omniNotes(omni, ignored, tierPrices)
524
+ : status === 'token_billed' && qwen ? qwenNotes(qwen, ignored)
525
+ : ['Confirm the estimated cost before generation. Actual billed usage and final charges may differ.']] };
482
526
  if (zeroUsage.length)
483
527
  estimate.notes.push(`pricing_parameters ${zeroUsage.join(', ')} given as 0 would price this generation at 0. No media generation is free, so ${zeroUsage.length > 1 ? 'they count' : 'it counts'} as missing.`);
484
528
  if (ignored.length && status !== 'token_billed')
@@ -554,9 +598,9 @@ export async function readPricingRulesFor(entry, options = {}) {
554
598
  }
555
599
  /** Said with an estimate made from rules read earlier, while the platform limits pricing requests. */
556
600
  export function recentRulesNote(readAt) {
557
- return `The platform is limiting pricing requests right now, so this estimate uses the published rules read at ${readAt}. generate_* reads the current rules again before submitting and asks for a new approval if the price changed.`;
601
+ return `The platform is limiting pricing requests right now, so this estimate uses the published rules read at ${readAt}. generate_* reads the current rules again before submitting and asks for a new approval if the price rose.`;
558
602
  }
559
- /** An approval from cached or recent rules can never submit at a changed price: checkRulesApproval refuses it. */
603
+ /** An approval from cached or recent rules can never submit at a higher price: checkRulesApproval refuses it. */
560
604
  export async function createRulesEstimate(entry, input, options = {}) {
561
605
  const { body, readAt } = await readPricingRulesFor(entry, options);
562
606
  const result = calculateRulesEstimate(body, entry, input, options);
@@ -565,20 +609,27 @@ export async function createRulesEstimate(entry, input, options = {}) {
565
609
  result.estimate.notes.push(recentRulesNote(readAt));
566
610
  return { ...result, rules_read_at: readAt };
567
611
  }
612
+ /** An approval holds until its expires_at, not at it. */
613
+ export const approvalExpired = (approval, now = Date.now()) => Date.parse(approval.expires_at) <= now;
568
614
  /**
569
- * An approval holds for the same input at the same price. Past its expires_at it still holds when neither changed
570
- * (`expired: true`, for the result to say so); a changed input or price needs a new estimate and approval.
615
+ * Only a higher total needs a new approval: an unchanged or lower one submits, whatever else changed in the rules. A
616
+ * partial estimate's total is the part it prices.
617
+ */
618
+ export const priceRose = (approval, fresh) => BigInt(fresh.total_uc) > BigInt(approval.total_uc);
619
+ /**
620
+ * An approval holds for the same input until its expires_at, at the approved price or lower. A changed input
621
+ * (quote_input_changed), an expired approval (quote_expired) or a higher price (price_changed) needs a new estimate and
622
+ * approval.
571
623
  */
572
624
  export function checkRulesApproval(approval, fresh) {
573
- const expired = Date.parse(approval.expires_at) <= Date.now();
574
625
  if (approval.batch_items !== undefined) {
575
626
  fail('quote_input_changed', 'This pricing_quote approves a batch (inputs); pass the same inputs, or estimate this input and obtain approval.');
576
627
  }
577
628
  if (approval.request_hash !== fresh.approval.request_hash || approval.model_id !== fresh.approval.model_id) {
578
629
  fail('quote_input_changed', 'The generation input differs from the approved estimate.');
579
630
  }
580
- if (approval.fingerprint !== fresh.approval.fingerprint || approval.total_uc !== fresh.approval.total_uc) {
581
- fail('price_changed', 'The pricing rules or estimated price changed. Estimate again and obtain approval.');
582
- }
583
- return { expired };
631
+ if (approvalExpired(approval))
632
+ fail('quote_expired', `The approved estimate expired at ${approval.expires_at}. Estimate again and obtain approval.`);
633
+ if (priceRose(approval, fresh.approval))
634
+ fail('price_changed', 'The estimated price rose after it was approved. Estimate again and obtain approval.');
584
635
  }
@@ -0,0 +1,187 @@
1
+ // Copyright 2024 EvoLink AI. SPDX-License-Identifier: Apache-2.0
2
+ // Generated from Evolink-AI/evolink-mcp; adapted for direct REST operations. See platform/LICENSE and platform/NOTICE.
3
+ import { QuoteError } from './pricing-quote-client.js';
4
+ // The tokens the gateway reserves for a Qwen Audio TTS speech task at submission. Ported from GroAPI
5
+ // (feat/task-microservice-split) service/qwen_audio_tts.go (QwenAudioTTSEffectiveText and
6
+ // EstimateQwenAudioTTSPreChargeUsage) and the speech checks in controller/unified_audio_qwen_tts.go that run before it.
7
+ // The gateway counts each character's worst case by its class, so a task usually settles below the reservation, at the
8
+ // tokens the upstream reports.
9
+ const MODELS = new Set(['qwen-audio-3.1-tts-flash']);
10
+ /** Qwen Audio TTS models, which the gateway reserves and bills by input and output token counts. */
11
+ export const isQwenTTSModel = (model) => MODELS.has(model.trim().toLowerCase());
12
+ /**
13
+ * The input as the gateway reads it: hot_fix null is no hot_fix there (it neither checks it nor sends it on), so it is
14
+ * left out before the documented parameters are checked, the input is priced and it is submitted.
15
+ */
16
+ export function qwenTTSInput(model, input) {
17
+ if (input.hot_fix !== null || !isQwenTTSModel(model))
18
+ return input;
19
+ const rest = { ...input };
20
+ delete rest.hot_fix;
21
+ return rest;
22
+ }
23
+ /** The longest text one task synthesizes, in characters, also after hot_fix.replace. */
24
+ export const QWEN_TTS_MAX_TEXT = 5000;
25
+ /** instruction holds at most this many billing characters, a Han character counting two. */
26
+ const MAX_INSTRUCTION = 100;
27
+ /** hot_fix holds at most this many word entries across its lists. */
28
+ const MAX_HOT_FIX_ENTRIES = 200;
29
+ const HOT_FIX_LISTS = new Set(['pronunciation', 'replace']);
30
+ /** Reserved for every task: what a one-character request uses at most. */
31
+ const BASE_INPUT_TOKENS = 2, BASE_OUTPUT_TOKENS = 10;
32
+ /** Spaces and common punctuation, read like the words around them. */
33
+ const PUNCTUATION = new Set(' .,!?;:\'"-()\n,。!?;:、“”‘’()…—');
34
+ /** Input tokens per character, by class. */
35
+ const INPUT_TOKENS = { han: 1, caseless: 1, lower: 1, punctuation: 1, other: 3 };
36
+ /** Output tokens per character in halves, as read and with an instruction or a [tag] in the text. */
37
+ const OUTPUT_HALVES = {
38
+ han: [16, 20], caseless: [8, 16], lower: [9, 18], punctuation: [6, 12], other: [30, 30]
39
+ };
40
+ const HAN = /^\p{Script=Han}$/u, LETTER = /^\p{L}$/u, UPPER = /^\p{Lu}$/u, LOWER = /^\p{Ll}$/u;
41
+ /** The class the gateway counts a character in, checked in its order: Han, a letter without case, lowercase, punctuation. */
42
+ export function qwenCharacterClass(character) {
43
+ if (HAN.test(character))
44
+ return 'han';
45
+ if (LETTER.test(character) && !UPPER.test(character) && !LOWER.test(character))
46
+ return 'caseless';
47
+ if (LOWER.test(character))
48
+ return 'lower';
49
+ if (PUNCTUATION.has(character))
50
+ return 'punctuation';
51
+ return 'other';
52
+ }
53
+ function invalid(param, message) { throw new QuoteError('invalid_quote_parameter', message, param); }
54
+ /** A string as the gateway decodes it from the request JSON: an unpaired surrogate becomes U+FFFD. */
55
+ const decoded = (text) => text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, '\uFFFD');
56
+ function textParam(input, param) {
57
+ const value = input[param];
58
+ if (value === undefined || value === null)
59
+ return '';
60
+ return typeof value === 'string' ? decoded(value) : invalid(param, `${param} must be a string.`);
61
+ }
62
+ const characters = (text) => [...text].length;
63
+ /** strings.TrimSpace(text) == "": only the characters unicode.IsSpace accepts. */
64
+ const blank = (text) => /^[\t\n\v\f\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]*$/u.test(text);
65
+ /** CountBillingChars: a Han character counts two, any other one. */
66
+ const billingCharacters = (text) => { let count = 0; for (const character of text)
67
+ count += HAN.test(character) ? 2 : 1; return count; };
68
+ const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
69
+ /** sort.Strings: by UTF-8 bytes, which is code point order. */
70
+ function byCodePoint(a, b) {
71
+ const x = [...a], y = [...b];
72
+ for (let i = 0; i < Math.min(x.length, y.length); i++) {
73
+ const difference = x[i].codePointAt(0) - y[i].codePointAt(0);
74
+ if (difference)
75
+ return difference;
76
+ }
77
+ return x.length - y.length;
78
+ }
79
+ /** validateQwenAudioTTSHotFix: {pronunciation?, replace?}, each a non-empty list of {"word": "value"} objects. */
80
+ function checkHotFix(hotFix) {
81
+ const shape = () => invalid('hot_fix', 'hot_fix must be an object with pronunciation and/or replace arrays of {"word": "value"} objects.');
82
+ if (!isObject(hotFix) || !Object.keys(hotFix).length)
83
+ shape();
84
+ let entries = 0;
85
+ for (const [list, items] of Object.entries(hotFix)) {
86
+ if (!HOT_FIX_LISTS.has(list) || !Array.isArray(items) || !items.length)
87
+ shape();
88
+ for (const item of items) {
89
+ if (!isObject(item) || !Object.keys(item).length)
90
+ shape();
91
+ for (const [word, value] of Object.entries(item)) {
92
+ if (typeof value !== 'string' || word === '' || value === '')
93
+ shape();
94
+ entries++;
95
+ }
96
+ }
97
+ }
98
+ if (entries > MAX_HOT_FIX_ENTRIES)
99
+ invalid('hot_fix', `hot_fix may contain at most ${MAX_HOT_FIX_ENTRIES} entries in total, got ${entries}.`);
100
+ }
101
+ /**
102
+ * QwenAudioTTSEffectiveText: the text after hot_fix.replace, item by item and each item's words in code point order,
103
+ * and its length. A replacement that would make the text longer than max is not made (ok false, with that length).
104
+ */
105
+ export function qwenEffectiveText(text, hotFix, max = QWEN_TTS_MAX_TEXT) {
106
+ let length = characters(text);
107
+ if (!isObject(hotFix))
108
+ return { text, characters: length, ok: length <= max };
109
+ if (length > max)
110
+ return { text, characters: length, ok: false };
111
+ for (const item of Array.isArray(hotFix.replace) ? hotFix.replace : []) {
112
+ const pair = isObject(item) ? item : {};
113
+ for (const word of Object.keys(pair).sort((a, b) => byCodePoint(decoded(a), decoded(b)))) {
114
+ const target = pair[word];
115
+ if (typeof target !== 'string' || word === '')
116
+ continue;
117
+ const from = decoded(word), to = decoded(target);
118
+ const hits = text.split(from).length - 1;
119
+ if (!hits)
120
+ continue;
121
+ const projected = length + hits * (characters(to) - characters(from));
122
+ if (projected > max)
123
+ return { text, characters: projected, ok: false };
124
+ text = text.split(from).join(to);
125
+ length = projected;
126
+ }
127
+ }
128
+ return { text, characters: length, ok: true };
129
+ }
130
+ /** Characters of json.Marshal(value): Go escapes <, > and & (HTML), U+2028 and U+2029, and control characters. */
131
+ export function goJSONCharacters(value) {
132
+ if (typeof value === 'string') {
133
+ let count = 2;
134
+ for (const character of decoded(value)) {
135
+ const code = character.codePointAt(0);
136
+ count += character === '"' || character === '\\' || '\b\f\n\r\t'.includes(character) ? 2
137
+ : code < 0x20 || character === '<' || character === '>' || character === '&' || code === 0x2028 || code === 0x2029 ? 6 : 1;
138
+ }
139
+ return count;
140
+ }
141
+ if (Array.isArray(value))
142
+ return 2 + value.reduce((sum, item, i) => sum + (i ? 1 : 0) + goJSONCharacters(item), 0);
143
+ if (isObject(value))
144
+ return 2 + Object.entries(value).reduce((sum, [key, item], i) => sum + (i ? 1 : 0) + goJSONCharacters(key) + 1 + goJSONCharacters(item), 0);
145
+ if (value === null)
146
+ return 4;
147
+ if (typeof value === 'boolean')
148
+ return value ? 4 : 5;
149
+ // checkHotFix leaves only objects, lists and strings; Go writes numbers differently from JavaScript.
150
+ return invalid('hot_fix', 'hot_fix holds only objects, lists and strings.');
151
+ }
152
+ /**
153
+ * The tokens the gateway reserves at submission for this input. The text is prompt, or input when prompt is empty;
154
+ * the checks are the gateway's own before it reserves, which the documented parameters do not all cover.
155
+ */
156
+ export function qwenTTSReservation(input) {
157
+ const prompt = textParam(input, 'prompt'), alias = textParam(input, 'input');
158
+ if (prompt !== '' && alias !== '' && prompt !== alias)
159
+ invalid('input', 'prompt and input differ; send only one.');
160
+ const text = prompt !== '' ? prompt : alias;
161
+ if (blank(text))
162
+ invalid('prompt', 'prompt is required.');
163
+ if (characters(text) > QWEN_TTS_MAX_TEXT)
164
+ invalid('prompt', `prompt must be at most ${QWEN_TTS_MAX_TEXT} characters, got ${characters(text)}.`);
165
+ const instruction = textParam(input, 'instruction');
166
+ const billed = billingCharacters(instruction);
167
+ if (billed > MAX_INSTRUCTION)
168
+ invalid('instruction', `instruction must be at most ${MAX_INSTRUCTION} billing characters (Han characters count as 2), got ${billed}.`);
169
+ const hotFix = input.hot_fix === null ? undefined : input.hot_fix;
170
+ if (hotFix !== undefined)
171
+ checkHotFix(hotFix);
172
+ const effective = qwenEffectiveText(text, hotFix);
173
+ if (!effective.ok)
174
+ invalid('hot_fix', `prompt must be at most ${QWEN_TTS_MAX_TEXT} characters after hot_fix.replace is applied, got ${effective.characters}.`);
175
+ const hotFixCharacters = hotFix === undefined ? 0 : goJSONCharacters(hotFix);
176
+ const expressive = instruction !== '' || effective.text.includes('[');
177
+ const classes = { han: 0, caseless: 0, lower: 0, punctuation: 0, other: 0 };
178
+ let inputTokens = BASE_INPUT_TOKENS + hotFixCharacters * 3, halves = BASE_OUTPUT_TOKENS * 2;
179
+ for (const character of effective.text) {
180
+ const kind = qwenCharacterClass(character);
181
+ classes[kind]++;
182
+ inputTokens += INPUT_TOKENS[kind];
183
+ halves += OUTPUT_HALVES[kind][expressive ? 1 : 0];
184
+ }
185
+ return { tokens: { input_tokens: inputTokens, output_tokens: Math.ceil(halves / 2) }, characters: effective.characters, classes,
186
+ hot_fix_characters: hotFixCharacters, expressive };
187
+ }
@@ -20,7 +20,7 @@ export const ESTIMATE_OPT_OUT_GUIDANCE = [
20
20
  'When the user opts out for the rest of the conversation, tell them once, clearly, that later generations in this conversation will be submitted without asking, and that they can start a new conversation or ask you to restore estimates to bring them back.',
21
21
  'If estimate_cost fails for a reason other than invalid input, generate with skip_estimate: true when the user has opted out; otherwise ask once, in one sentence, whether to generate anyway with the cost settled by actual usage, and on yes use skip_estimate: true.',
22
22
  'skip_estimate does not set aside an explicit user budget, does not approve enabling model web or image search, and does not change what is charged; after submitting, tell the user what was reserved or charged.',
23
- 'A pricing_quote that expired keeps its approval while the input and price are unchanged; a changed price needs a new estimate and approval.',
23
+ 'A pricing_quote holds for 24 hours from the estimate, at the approved price or lower: an expired one, a higher price or a changed input needs a new estimate (free) and approval.',
24
24
  ].join(' ');
25
25
  /** How to submit several tasks: clients run paid tool calls one at a time, so one call per task and the wait for each result are what to avoid. */
26
26
  export const BATCH_GENERATION_GUIDANCE = [
@@ -28,9 +28,9 @@ export const BATCH_GENERATION_GUIDANCE = [
28
28
  'For tasks of different models, submit one task per item with wait_seconds: 0.',
29
29
  'Either way, collect the results with list_tasks (task_ids, up to 50 per call; free and read-only, so these calls can run in parallel), and tell the user the tasks generate at the same time.',
30
30
  'Do not rely on n for the count: some models, such as GPT Image, often return fewer images than n.',
31
- 'To finish a batch, call the generate tool again with the same client_request_id and exactly the same inputs within 24 hours: the items its result lists to finish (not submitted, or refused and since fixed) are then submitted, and accepted items return their original tasks without a new charge.',
31
+ 'To finish a batch, call the generate tool again with the same client_request_id and exactly the same inputs within 24 hours of its first submission: the items its result lists to finish (not submitted, or refused and since fixed) are then submitted, and accepted items return their original tasks without a new charge.',
32
32
  'For an item with an unknown outcome, retry later the same way or check list_tasks.',
33
- 'Never resubmit accepted, not-submitted or unknown-outcome items under a new client_request_id: that creates and charges new tasks.',
33
+ 'Within those 24 hours, never resubmit accepted, not-submitted or unknown-outcome items under a new client_request_id: that creates and charges new tasks. Past them the same client_request_id no longer prevents a second charge: check list_tasks first, then submit only the items that did not go through, as a new batch with a new client_request_id.',
34
34
  'An item refused because it differs from what that client_request_id submitted earlier is a new task: it needs a new estimate, approval and client_request_id.',
35
35
  ].join(' ');
36
36
  /** Shared confirmation wording; approval and spending rules above still apply. */
@@ -68,7 +68,7 @@ export const GENERATION_CONFIRMATION_GUIDANCE = [
68
68
  '- For a batch (inputs), use one confirmation for the whole batch: in Output give the number of items and what differs between them, and in Cost the total plus the price per item, or the range when items differ. One approval covers exactly that batch.',
69
69
  '- Keep model web and image search off unless the user explicitly requests or approves enabling it. Never enable search on your own or just because a copied example enables it. If you think search is needed, first explain its extra per-search rates and unknown total, end the reply, and wait for explicit user consent before enabling it. Approval of a task without search does not authorize adding search; changed search settings require a fresh estimate and approval.',
70
70
  '- When search_charges is returned, disclose each enabled search type and its per-search rate before generation approval, even when the user requested search. A zero-search subtotal excludes unknown search fees; one task may search multiple times or not at all. Expected counts are assumptions, not execution limits. Actual counts depend on the task and model strategy. Do not add a search-fee warning when search is off.',
71
- '- For a token_billed estimate, show the reference price in credits and approximate USD and say "参考价 {积分}(约 {美元}),按实际 token 用量结算,完成后多退少补,实际可能更低或略高" / "Reference price {credits} (about {USD}); billed by actual token usage and settled when the task finishes, so the final charge may be lower or slightly higher." When size is auto (except a GIF, whose frames have a fixed size), add that setting a size gives a closer reference price. For a Gemini Omni video with an input or reference video, pass its length as media_seconds; the reference counts that video at the highest resolution (4k), so also show the lower-resolution price the estimate gives. If an estimate still needs token usage, say "按实际 token 用量计费,生成前无法确定总价" / "Billed by actual token usage; the total is unknown before generation." and include the applicable published unit rates when returned. If no price is available, say so rather than assuming token billing.',
71
+ '- For a token_billed estimate, show the reference price in credits and approximate USD and say "参考价 {积分}(约 {美元}),按实际 token 用量结算,完成后多退少补,实际可能更低或略高" / "Reference price {credits} (about {USD}); billed by actual token usage and settled when the task finishes, so the final charge may be lower or slightly higher." When size is auto (except a GIF, whose frames have a fixed size), add that setting a size gives a closer reference price. For a Gemini Omni video with an input or reference video, pass its length as media_seconds; the reference counts that video at the highest resolution (4k), so also show the price the estimate lists for each input resolution. If an estimate still needs token usage, say "按实际 token 用量计费,生成前无法确定总价" / "Billed by actual token usage; the total is unknown before generation." and include the applicable published unit rates when returned. If no price is available, say so rather than assuming token billing.',
72
72
  '- If the user set a fixed cap that cannot be checked, explain that gap and replace the usual closing with an explicit question about proceeding without that cap. Do not treat a generic "Confirm generation" as consent to remove or raise a cap.',
73
73
  '- The suggested confirmation phrase is not a required command; any clear approval of the quoted task is valid. Existing task or batch approval still follows the spending approval rules; do not ask again when it already covers this request.',
74
74
  '- Keep routine explanations about the task and cost, for example "这次生成需要确认费用" / "Please confirm the cost for this generation." Do not add skill paths, filenames or quoted internal rules to routine confirmations unless the user asks or higher-priority host instructions require them.',
@@ -9,6 +9,7 @@ import { formatEstimateRange } from '../services/pricing-client.js';
9
9
  import { PricingRulesError, reusablePricingRules } from '../services/pricing-rules-client.js';
10
10
  import { QuoteError, QuoteParameters } from '../services/pricing-quote-client.js';
11
11
  import { calculateRulesEstimate, createRulesEstimate, readPricingRulesFor, recentRulesNote } from '../services/pricing-rules-estimate.js';
12
+ import { qwenTTSInput } from '../services/qwen-tts-tokens.js';
12
13
  import { prepareSeedanceInput } from '../services/seedance-input.js';
13
14
  import { API_KEYS_URL, MCP_CONSOLE_URL, MCP_KEY_NAME, TOP_UP_URL } from '../services/http-policy.js';
14
15
  import { trackedLink } from '../services/utm.js';
@@ -49,7 +50,7 @@ export function registerEstimateCost(server, config) {
49
50
  }
50
51
  if (inputs !== undefined)
51
52
  return await estimateBatch(config, entry, inputs, { mediaSeconds, pricingParameters, pricingSource });
52
- const prepared = prepareSeedanceInput(entry, input ?? {});
53
+ const prepared = prepareSeedanceInput(entry, qwenTTSInput(entry.id, input ?? {}));
53
54
  const values = prepared.input;
54
55
  const validation = entry.spec ? validateInput(entry.spec, values) : undefined;
55
56
  validation?.errors.push(...prepared.errors);
@@ -228,7 +229,7 @@ function itemPrice(estimate) {
228
229
  */
229
230
  async function estimateBatch(config, entry, inputs, options) {
230
231
  const batch = { items: inputs.length };
231
- const prepared = inputs.map(item => prepareSeedanceInput(entry, item));
232
+ const prepared = inputs.map(item => prepareSeedanceInput(entry, qwenTTSInput(entry.id, item)));
232
233
  const checks = prepared.map(item => {
233
234
  if (!entry.spec)
234
235
  return undefined;