@evolinkai/cli 0.13.0 → 0.14.0
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 +20 -6
- package/package.json +1 -1
- package/skills/evolink-cli/SKILL.md +21 -7
- package/skills/evolink-cli/references/capabilities.md +1 -1
- package/skills/evolink-cli/references/media-workflows.md +1 -1
- package/skills/evolink-cli/references/usage-and-budget.md +5 -3
- package/src/cli.mjs +76 -21
- package/src/help.mjs +12 -5
- package/src/media.mjs +142 -37
- package/src/platform/core/src/data/model-constraints.js +8 -5
- package/src/platform/core/src/data/model-params.overrides.generated.js +1 -1
- package/src/platform/core/src/services/api-client.js +1 -1
- package/src/platform/core/src/services/batch-generation.js +117 -0
- package/src/platform/core/src/services/gpt-image-tokens.js +264 -0
- package/src/platform/core/src/services/omni-tokens.js +245 -0
- package/src/platform/core/src/services/param-validator.js +48 -1
- package/src/platform/core/src/services/pricing-call-limit.js +4 -4
- package/src/platform/core/src/services/pricing-derived.js +3 -3
- package/src/platform/core/src/services/pricing-rules-client.js +23 -5
- package/src/platform/core/src/services/pricing-rules-estimate.js +266 -67
- package/src/platform/core/src/tool-guidance.js +25 -2
- package/src/platform/core/src/tools/estimate-cost.js +201 -60
- package/src/platform/core/src/tools/generate.js +497 -75
- package/src/platform/core/src/tools/get-model.js +15 -3
- package/src/platform/core/src/tools/get-pricing-rules.js +4 -3
- package/src/platform/core/src/tools/shared.js +1 -1
- package/src/platform/core/src/version.js +1 -1
- package/src/platform/source.json +41 -35
|
@@ -4,13 +4,35 @@
|
|
|
4
4
|
export const PAID_GENERATION_GUIDANCE = [
|
|
5
5
|
'Before each new paid generation, call estimate_cost with the exact model and input, then use the generation confirmation template in the server instructions.',
|
|
6
6
|
'If the estimate is partial or unavailable, explain what is not covered or that the final cost is unknown.',
|
|
7
|
-
'End the reply and wait for the user to explicitly approve that quoted task before calling a generate tool.',
|
|
7
|
+
'End the reply and wait for the user to explicitly approve that quoted task before calling a generate tool, unless the user opted out (skip_estimate).',
|
|
8
8
|
'You may proceed under a prior explicit approval only when the user specified the task or batch scope and a spending budget that still covers this request; track the remaining budget.',
|
|
9
9
|
'Your own suggested budget, a model recommendation, sufficient balance or a general request to generate is not spending approval.',
|
|
10
10
|
'Show estimated cost and ask for approval. Do not derive max_cost_usd from a quote; compare only an explicit user budget, without promising a final charge.',
|
|
11
11
|
'A new variation, regeneration or retry after a failed task is a new paid generation and needs approval unless that explicit batch approval covers it.',
|
|
12
12
|
'Never remove or raise a user spending cap without their explicit approval.',
|
|
13
13
|
].join(' ');
|
|
14
|
+
/**
|
|
15
|
+
* When the user opts out of estimates. The server instructions carry it in full; a generate tool's description only
|
|
16
|
+
* points to skip_estimate, since each description must stay short.
|
|
17
|
+
*/
|
|
18
|
+
export const ESTIMATE_OPT_OUT_GUIDANCE = [
|
|
19
|
+
'Exception: when the user explicitly says not to estimate or not to confirm (for example "generate directly", "no need to estimate", "stop asking", "from now on just generate"), skip estimate_cost and the confirmation and pass skip_estimate: true, within the scope the user gave: this task, this batch, or every later generation in this conversation; if the scope is unclear, only this task. Never infer it from silence, the balance or your own suggestion.',
|
|
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
|
+
'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
|
+
'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.',
|
|
24
|
+
].join(' ');
|
|
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
|
+
export const BATCH_GENERATION_GUIDANCE = [
|
|
27
|
+
'For several images or tasks of one model, pass inputs (up to 50 items, each like input) to estimate_cost and then, with its pricing_quote, to the generate tool: one estimate, one approval for the batch total, and one call that submits every item at once and returns each task_id right away.',
|
|
28
|
+
'For tasks of different models, submit one task per item with wait_seconds: 0.',
|
|
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
|
+
'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.',
|
|
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.',
|
|
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
|
+
].join(' ');
|
|
14
36
|
/** Shared confirmation wording; approval and spending rules above still apply. */
|
|
15
37
|
export const GENERATION_CONFIRMATION_GUIDANCE = [
|
|
16
38
|
'When spending approval is needed, use the following layout in English by default unless the user explicitly requests another language. Fill it from the exact model and input checked by `estimate_cost`; summarize the content in one sentence. Render normal text and bullets; the code blocks below only illustrate the layout.',
|
|
@@ -43,9 +65,10 @@ export const GENERATION_CONFIRMATION_GUIDANCE = [
|
|
|
43
65
|
'- For a complete estimate, show the returned amount or range in credits and approximate USD. Label it as an estimate, not a guaranteed final charge.',
|
|
44
66
|
'- Do not add a separate estimated cap or max_cost_usd to the default confirmation. If the user specified a budget, state whether the estimate fits it; pause if it exceeds the budget. A budget comparison does not limit the final settlement.',
|
|
45
67
|
'- For a partial estimate, label it "部分估价" / "Partial estimate", state what is excluded, and say it is neither the total nor an upper bound.',
|
|
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.',
|
|
46
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.',
|
|
47
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.',
|
|
48
|
-
'- For token
|
|
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.',
|
|
49
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.',
|
|
50
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.',
|
|
51
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.',
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
// Generated from Evolink-AI/evolink-mcp; adapted for direct REST operations. See platform/LICENSE and platform/NOTICE.
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { getCredits } from '../services/api-client.js';
|
|
5
|
+
import { MAX_BATCH_ITEMS, batchTotal } from '../services/batch-generation.js';
|
|
5
6
|
import { resolveModel, suggestModels } from '../services/model-catalog.js';
|
|
6
7
|
import { formatIssues, validateInput } from '../services/param-validator.js';
|
|
7
8
|
import { formatEstimateRange } from '../services/pricing-client.js';
|
|
8
|
-
import { PricingRulesError } from '../services/pricing-rules-client.js';
|
|
9
|
+
import { PricingRulesError, reusablePricingRules } from '../services/pricing-rules-client.js';
|
|
9
10
|
import { QuoteError, QuoteParameters } from '../services/pricing-quote-client.js';
|
|
10
|
-
import { createRulesEstimate } from '../services/pricing-rules-estimate.js';
|
|
11
|
+
import { calculateRulesEstimate, createRulesEstimate, readPricingRulesFor, recentRulesNote } from '../services/pricing-rules-estimate.js';
|
|
11
12
|
import { prepareSeedanceInput } from '../services/seedance-input.js';
|
|
12
13
|
import { API_KEYS_URL, MCP_CONSOLE_URL, MCP_KEY_NAME, TOP_UP_URL } from '../services/http-policy.js';
|
|
13
14
|
import { trackedLink } from '../services/utm.js';
|
|
@@ -21,29 +22,33 @@ export function registerEstimateCost(server, config) {
|
|
|
21
22
|
'Check a generation input and estimate what it will cost, without submitting anything. Free.',
|
|
22
23
|
'Returns whether the input is valid, the expected price range in USD and credits, what it is based on, and whether the account balance and any spending limit (the EvoLink MCP limit, or this API key\'s) cover it.',
|
|
23
24
|
'Call it before a paid generate_* call, then use the generation confirmation template in the server instructions.',
|
|
25
|
+
`For several tasks of one model, pass inputs (up to ${MAX_BATCH_ITEMS}): one total and one pricing_quote approve the whole batch.`,
|
|
24
26
|
].join(' '),
|
|
25
27
|
inputSchema: {
|
|
26
28
|
model: z.string().min(1).max(128).describe('Model ID, e.g. "seedance-2.0-text-to-video".'),
|
|
27
29
|
input: z.record(z.string(), z.unknown()).optional()
|
|
28
30
|
.describe('The input you plan to pass to the generate tool, e.g. {"prompt":"…","duration":5,"quality":"1080p"}.'),
|
|
31
|
+
// The leading max() lets zod stop at an oversized array instead of checking every element.
|
|
32
|
+
inputs: z.array(z.record(z.string(), z.unknown())).max(MAX_BATCH_ITEMS).min(1).optional()
|
|
33
|
+
.describe(`Several inputs for this model (1–${MAX_BATCH_ITEMS}), each as you will pass it in the generate tool's inputs, in the same order. Returns each input's estimate, the total and one pricing_quote that approves the whole batch. media_seconds and pricing_parameters apply to every input. Pass input or inputs, not both.`),
|
|
29
34
|
pricing_source: z.enum(['pricing_rules', 'account', 'public_reference']).default('pricing_rules').describe('Use published full pricing rules for public default estimates. The old account/public_reference values are compatibility aliases; neither requests a backend account quote.'),
|
|
30
|
-
pricing_parameters: QuoteParameters.optional().describe('Measured or explicitly expected usage declared by the pricing policy, such as input_seconds or search_count. Missing enabled-search counts produce a zero-search partial subtotal and per-search rates. Do not invent counts, include user_group/prices/prompts, or treat expected counts as execution limits.'),
|
|
35
|
+
pricing_parameters: QuoteParameters.optional().describe('Measured or explicitly expected usage declared by the pricing policy, such as input_seconds or search_count. Missing enabled-search counts produce a zero-search partial subtotal and per-search rates. GPT Image token models do not use token counts here: their reference price comes from the input. Do not invent counts, include user_group/prices/prompts, or treat expected counts as execution limits.'),
|
|
31
36
|
media_seconds: z.number().positive().max(MEDIA_SECONDS_MAX).optional().describe(MEDIA_SECONDS_DESCRIPTION),
|
|
32
37
|
},
|
|
33
38
|
annotations: { title: 'Estimate cost', ...READ_ONLY },
|
|
34
|
-
}, async ({ model, input, media_seconds: mediaSeconds, pricing_source: pricingSource, pricing_parameters: pricingParameters }) => {
|
|
35
|
-
const over = pricingCallLimitReached('estimate_cost');
|
|
36
|
-
if (over)
|
|
37
|
-
return failure(`This account already made ${over.limit} estimate_cost calls in the last minute, the most allowed. Wait ${retryText(over.retryAfterSeconds)}, then estimate again. Nothing was submitted or charged.`, {
|
|
38
|
-
error: { category: 'rate_limited', code: 'pricing_calls_per_minute', retry_after_seconds: over.retryAfterSeconds },
|
|
39
|
-
pricing_source: pricingSource, submission_allowed: false, final_budget_enforced: false, charged: 'no'
|
|
40
|
-
});
|
|
39
|
+
}, async ({ model, input, inputs, media_seconds: mediaSeconds, pricing_source: pricingSource, pricing_parameters: pricingParameters }) => {
|
|
41
40
|
try {
|
|
41
|
+
if (inputs !== undefined && input !== undefined)
|
|
42
|
+
return failure('Pass input for one generation or inputs for a batch, not both. No estimate was created.', {
|
|
43
|
+
error: { category: 'invalid_request', param: 'inputs' }, charged: 'no', batch: { items: inputs.length }
|
|
44
|
+
});
|
|
42
45
|
const { catalog, entry } = await resolveModel(model, config, { fresh: true, allowStale: false, skipPricing: true });
|
|
43
46
|
if (!entry) {
|
|
44
47
|
const suggestions = suggestModels(catalog, model);
|
|
45
48
|
return failure(`Unknown model "${model}".${suggestions.length ? ` Did you mean: ${suggestions.join(', ')}?` : ''} Use search_models to find model IDs.`, { error: { category: 'not_found', param: 'model' }, suggestions });
|
|
46
49
|
}
|
|
50
|
+
if (inputs !== undefined)
|
|
51
|
+
return await estimateBatch(config, entry, inputs, { mediaSeconds, pricingParameters, pricingSource });
|
|
47
52
|
const prepared = prepareSeedanceInput(entry, input ?? {});
|
|
48
53
|
const values = prepared.input;
|
|
49
54
|
const validation = entry.spec ? validateInput(entry.spec, values) : undefined;
|
|
@@ -51,6 +56,9 @@ export function registerEstimateCost(server, config) {
|
|
|
51
56
|
validation?.warnings.push(...prepared.warnings);
|
|
52
57
|
if (validation?.errors.length)
|
|
53
58
|
return failure('The generation input is invalid; no estimate was created.', { input_valid: false, problems: validation.errors, error: { category: 'invalid_request' }, charged: 'no' });
|
|
59
|
+
const over = estimateLimitReached(config, entry);
|
|
60
|
+
if (over)
|
|
61
|
+
return limitFailure(over, pricingSource);
|
|
54
62
|
const rulesEstimate = await createRulesEstimate(entry, values, { mediaSeconds, pricingParameters, reuseRules: config.reusePricingRules === true });
|
|
55
63
|
const estimate = rulesEstimate.estimate;
|
|
56
64
|
const lines = [`Estimate for ${entry.id} (${entry.kind}); nothing was submitted or charged.`];
|
|
@@ -72,6 +80,8 @@ export function registerEstimateCost(server, config) {
|
|
|
72
80
|
lines.push(`Estimated cost: ${range}`);
|
|
73
81
|
else if (estimate.status === 'partial' && range)
|
|
74
82
|
lines.push(`Partial estimate at published rates: ${range} (not a total or an upper bound).`);
|
|
83
|
+
else if (estimate.status === 'token_billed' && range)
|
|
84
|
+
lines.push(`Reference price: ${range}. Billed by actual token usage and settled when the task finishes, so the final charge may be lower or slightly higher.`);
|
|
75
85
|
else if (estimate.status === 'token_billed')
|
|
76
86
|
lines.push('Cost: billed by tokens used; it is only known after the task runs.');
|
|
77
87
|
else if (estimate.status === 'needs_input')
|
|
@@ -113,55 +123,11 @@ export function registerEstimateCost(server, config) {
|
|
|
113
123
|
...(prepared.applies ? { effective_input: values, defaults_applied: prepared.defaults,
|
|
114
124
|
amount_scope: estimate.status === 'partial' ? 'known_components_subtotal' : estimate.status === 'estimated' ? 'expected_usage_estimate' : 'not_established' } : {}),
|
|
115
125
|
};
|
|
116
|
-
|
|
117
|
-
const credits = await getCredits(config, 'estimate_cost');
|
|
118
|
-
const balance = Math.max(0, credits.user.remaining_credits);
|
|
119
|
-
lines.push(`Account balance: ${money(balance)}`);
|
|
120
|
-
structured.balance_credits = balance;
|
|
121
|
-
if (estimate.status === 'estimated' && estimate.max_credits !== undefined) {
|
|
122
|
-
const enough = balance >= estimate.max_credits;
|
|
123
|
-
structured.enough_balance = enough;
|
|
124
|
-
if (!enough)
|
|
125
|
-
lines.push(`The account balance may not cover this; top up at ${trackedLink(TOP_UP_URL, 'top_up')}.`);
|
|
126
|
-
}
|
|
127
|
-
// The key's own limit is checked before the balance: warn now instead of failing on submit.
|
|
128
|
-
if (!credits.token.unlimited_credits) {
|
|
129
|
-
const signedIn = currentCredentialMode() === 'signed_in';
|
|
130
|
-
const left = Math.max(0, credits.token.remaining_credits);
|
|
131
|
-
lines.push(signedIn
|
|
132
|
-
? `EvoLink MCP limit left: ${money(left)} (shared by all connected assistants).`
|
|
133
|
-
: `This API key's limit left: ${money(left)}.`);
|
|
134
|
-
structured.limit_scope = signedIn ? 'mcp' : 'api_key';
|
|
135
|
-
structured.limit_remaining_credits = left;
|
|
136
|
-
if (estimate.status === 'estimated' && estimate.max_credits !== undefined) {
|
|
137
|
-
const enoughLimit = left >= estimate.max_credits;
|
|
138
|
-
structured.enough_limit = enoughLimit;
|
|
139
|
-
if (!enoughLimit) {
|
|
140
|
-
lines.push(signedIn
|
|
141
|
-
? `The EvoLink MCP limit may not cover this. It is a limit the user set, not the account balance; they can raise it at ${trackedLink(MCP_CONSOLE_URL, 'api_keys')} (the key named "${MCP_KEY_NAME}").`
|
|
142
|
-
: `This API key's limit may not cover this; raise it at ${trackedLink(API_KEYS_URL, 'api_keys')} or use another key.`);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const daily = dailyLimitOf(credits.token);
|
|
147
|
-
if (daily && estimate.status === 'estimated' && estimate.max_credits !== undefined) {
|
|
148
|
-
const signedIn = currentCredentialMode() === 'signed_in';
|
|
149
|
-
const enoughToday = daily.left >= estimate.max_credits;
|
|
150
|
-
structured.daily_left_credits = daily.left;
|
|
151
|
-
structured.enough_daily_limit = enoughToday;
|
|
152
|
-
if (!enoughToday) {
|
|
153
|
-
const resets = `it resets at midnight${daily.zone ? ` (${daily.zone})` : ''}`;
|
|
154
|
-
lines.push(signedIn
|
|
155
|
-
? `Today's EvoLink MCP limit may not cover this (${money(daily.left)} left today; ${resets}). It is a limit the user set, not the account balance.`
|
|
156
|
-
: `Today's limit of this API key may not cover this (${money(daily.left)} left today; ${resets}).`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
catch {
|
|
161
|
-
lines.push('Balance: could not be read right now.');
|
|
162
|
-
}
|
|
126
|
+
await addBalance(config, estimate, lines, structured);
|
|
163
127
|
if (priced)
|
|
164
|
-
lines.push(`Estimate valid until ${rulesEstimate.approval.expires_at}.`,
|
|
128
|
+
lines.push(`Estimate valid until ${rulesEstimate.approval.expires_at}.`, estimate.status === 'token_billed'
|
|
129
|
+
? 'Public default reference price; account discounts are not included. Confirm it before generation. The final charge follows actual token usage.'
|
|
130
|
+
: 'Public default estimate; account discounts are not included. Confirm this estimated cost before generation. Final charges depend on actual usage.');
|
|
165
131
|
else
|
|
166
132
|
lines.push('No total or approval record was issued. Estimate again with the missing usage before asking the user to approve a generation.');
|
|
167
133
|
return ok(lines.join('\n'), structured);
|
|
@@ -172,12 +138,12 @@ export function registerEstimateCost(server, config) {
|
|
|
172
138
|
? `The platform is limiting pricing requests right now, so no estimate was created. Try again in ${retryText(error.retryAfter)}. Nothing was submitted or charged.`
|
|
173
139
|
: 'The model pricing rules could not be verified. No estimate or generation was created.', {
|
|
174
140
|
error: { category: 'estimate_unavailable', code: error.code, status: error.status, ...(error.retryAfter !== undefined ? { retry_after_seconds: error.retryAfter } : {}) }, pricing_source: pricingSource,
|
|
175
|
-
submission_allowed: false, final_budget_enforced: false, charged: 'no'
|
|
141
|
+
submission_allowed: false, final_budget_enforced: false, charged: 'no', ...(inputs ? { batch: { items: inputs.length } } : {})
|
|
176
142
|
});
|
|
177
143
|
if (error instanceof QuoteError)
|
|
178
144
|
return failure(`${error.message} Nothing was submitted or charged.`, {
|
|
179
145
|
error: { category: 'estimate_unavailable', code: error.code, ...(error.param ? { param: error.param } : {}) },
|
|
180
|
-
pricing_source: pricingSource, submission_allowed: false, final_budget_enforced: false, charged: 'no'
|
|
146
|
+
pricing_source: pricingSource, submission_allowed: false, final_budget_enforced: false, charged: 'no', ...(inputs ? { batch: { items: inputs.length } } : {})
|
|
181
147
|
});
|
|
182
148
|
const failed = errorResult(error);
|
|
183
149
|
return { ...failed, structuredContent: { ...failed.structuredContent, pricing_source: pricingSource,
|
|
@@ -185,3 +151,178 @@ export function registerEstimateCost(server, config) {
|
|
|
185
151
|
}
|
|
186
152
|
});
|
|
187
153
|
}
|
|
154
|
+
/** Only an estimate that reads the platform's rules counts; one answered from rules read moments ago does not. */
|
|
155
|
+
function estimateLimitReached(config, entry) {
|
|
156
|
+
const reused = config.reusePricingRules === true && reusablePricingRules({ model: entry.id.toLowerCase(), view: 'full' });
|
|
157
|
+
const over = reused ? undefined : pricingCallLimitReached('estimate_cost');
|
|
158
|
+
return over;
|
|
159
|
+
}
|
|
160
|
+
function limitFailure(over, pricingSource) {
|
|
161
|
+
return failure(`This account already made ${over.limit} estimate_cost calls that read the pricing rules in the last minute, the most allowed. Wait ${retryText(over.retryAfterSeconds)}, then estimate again. Nothing was submitted or charged.`, {
|
|
162
|
+
error: { category: 'rate_limited', code: 'pricing_calls_per_minute', retry_after_seconds: over.retryAfterSeconds },
|
|
163
|
+
pricing_source: pricingSource, submission_allowed: false, final_budget_enforced: false, charged: 'no'
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/** Whether the balance and the spending limits cover the estimate; a reference price is what the gateway reserves at submission. */
|
|
167
|
+
async function addBalance(config, estimate, lines, structured) {
|
|
168
|
+
const checkable = (estimate.status === 'estimated' || estimate.status === 'token_billed') && estimate.max_credits !== undefined;
|
|
169
|
+
try {
|
|
170
|
+
const credits = await getCredits(config, 'estimate_cost');
|
|
171
|
+
const balance = Math.max(0, credits.user.remaining_credits);
|
|
172
|
+
lines.push(`Account balance: ${money(balance)}`);
|
|
173
|
+
structured.balance_credits = balance;
|
|
174
|
+
if (checkable && estimate.max_credits !== undefined) {
|
|
175
|
+
const enough = balance >= estimate.max_credits;
|
|
176
|
+
structured.enough_balance = enough;
|
|
177
|
+
if (!enough)
|
|
178
|
+
lines.push(`The account balance may not cover this; top up at ${trackedLink(TOP_UP_URL, 'top_up')}.`);
|
|
179
|
+
}
|
|
180
|
+
// The key's own limit is checked before the balance: warn now instead of failing on submit.
|
|
181
|
+
if (!credits.token.unlimited_credits) {
|
|
182
|
+
const signedIn = currentCredentialMode() === 'signed_in';
|
|
183
|
+
const left = Math.max(0, credits.token.remaining_credits);
|
|
184
|
+
lines.push(signedIn
|
|
185
|
+
? `EvoLink MCP limit left: ${money(left)} (shared by all connected assistants).`
|
|
186
|
+
: `This API key's limit left: ${money(left)}.`);
|
|
187
|
+
structured.limit_scope = signedIn ? 'mcp' : 'api_key';
|
|
188
|
+
structured.limit_remaining_credits = left;
|
|
189
|
+
if (checkable && estimate.max_credits !== undefined) {
|
|
190
|
+
const enoughLimit = left >= estimate.max_credits;
|
|
191
|
+
structured.enough_limit = enoughLimit;
|
|
192
|
+
if (!enoughLimit) {
|
|
193
|
+
lines.push(signedIn
|
|
194
|
+
? `The EvoLink MCP limit may not cover this. It is a limit the user set, not the account balance; they can raise it at ${trackedLink(MCP_CONSOLE_URL, 'api_keys')} (the key named "${MCP_KEY_NAME}").`
|
|
195
|
+
: `This API key's limit may not cover this; raise it at ${trackedLink(API_KEYS_URL, 'api_keys')} or use another key.`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const daily = dailyLimitOf(credits.token);
|
|
200
|
+
if (daily && checkable && estimate.max_credits !== undefined) {
|
|
201
|
+
const signedIn = currentCredentialMode() === 'signed_in';
|
|
202
|
+
const enoughToday = daily.left >= estimate.max_credits;
|
|
203
|
+
structured.daily_left_credits = daily.left;
|
|
204
|
+
structured.enough_daily_limit = enoughToday;
|
|
205
|
+
if (!enoughToday) {
|
|
206
|
+
const resets = `it resets at midnight${daily.zone ? ` (${daily.zone})` : ''}`;
|
|
207
|
+
lines.push(signedIn
|
|
208
|
+
? `Today's EvoLink MCP limit may not cover this (${money(daily.left)} left today; ${resets}). It is a limit the user set, not the account balance.`
|
|
209
|
+
: `Today's limit of this API key may not cover this (${money(daily.left)} left today; ${resets}).`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
lines.push('Balance: could not be read right now.');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const itemsText = (indexes) => indexes.map(index => `inputs[${index}]`).join(', ');
|
|
218
|
+
/** One input's price, for the per-input list of a batch. */
|
|
219
|
+
function itemPrice(estimate) {
|
|
220
|
+
const range = formatEstimateRange(estimate);
|
|
221
|
+
if (estimate.status === 'needs_input' || !range)
|
|
222
|
+
return `no total yet (depends on ${(estimate.missing_usage ?? []).join(', ') || 'usage measured when it runs'})`;
|
|
223
|
+
return estimate.status === 'partial' ? `${range} partial` : estimate.status === 'token_billed' ? `${range} reference price` : range;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Several inputs for one model, estimated from one read of its rules and counted once against the account: each
|
|
227
|
+
* input's price, the total, and one approval record for the whole batch, in input order.
|
|
228
|
+
*/
|
|
229
|
+
async function estimateBatch(config, entry, inputs, options) {
|
|
230
|
+
const batch = { items: inputs.length };
|
|
231
|
+
const prepared = inputs.map(item => prepareSeedanceInput(entry, item));
|
|
232
|
+
const checks = prepared.map(item => {
|
|
233
|
+
if (!entry.spec)
|
|
234
|
+
return undefined;
|
|
235
|
+
const check = validateInput(entry.spec, item.input);
|
|
236
|
+
check.errors.push(...item.errors);
|
|
237
|
+
check.warnings.push(...item.warnings);
|
|
238
|
+
return check;
|
|
239
|
+
});
|
|
240
|
+
const located = (index, issues) => issues.map(issue => ({ ...issue, param: `inputs[${index}].${issue.param}` }));
|
|
241
|
+
const problems = checks.flatMap((check, index) => located(index, check?.errors ?? []));
|
|
242
|
+
if (problems.length)
|
|
243
|
+
return failure(['Some inputs are invalid; no estimate was created:', ...formatIssues(problems)].join('\n'), {
|
|
244
|
+
input_valid: false, problems, error: { category: 'invalid_request' }, charged: 'no', batch
|
|
245
|
+
});
|
|
246
|
+
const over = estimateLimitReached(config, entry);
|
|
247
|
+
if (over)
|
|
248
|
+
return limitFailure(over, options.pricingSource);
|
|
249
|
+
const reuse = config.reusePricingRules === true;
|
|
250
|
+
const { body, readAt } = await readPricingRulesFor(entry, { reuseRules: reuse });
|
|
251
|
+
const results = prepared.map((item, index) => {
|
|
252
|
+
try {
|
|
253
|
+
return calculateRulesEstimate(body, entry, item.input, { mediaSeconds: options.mediaSeconds, pricingParameters: options.pricingParameters });
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
throw error instanceof QuoteError ? new QuoteError(error.code, `inputs[${index}]: ${error.message}`, error.param) : error;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
const total = batchTotal(entry.id, results), combined = total.estimate, status = total.status;
|
|
260
|
+
if (readAt)
|
|
261
|
+
combined.notes.push(recentRulesNote(readAt));
|
|
262
|
+
const count = inputs.length;
|
|
263
|
+
const lines = [`Estimate for ${count} input${count === 1 ? '' : 's'} of ${entry.id} (${entry.kind}); nothing was submitted or charged.`];
|
|
264
|
+
lines.push(entry.spec ? 'Inputs look valid.' : 'Inputs not checked: this model\'s parameters are not documented here.');
|
|
265
|
+
const warnings = checks.flatMap((check, index) => located(index, check?.warnings ?? []));
|
|
266
|
+
if (warnings.length)
|
|
267
|
+
lines.push('Warnings:', ...formatIssues(warnings));
|
|
268
|
+
prepared.forEach((item, index) => {
|
|
269
|
+
for (const applied of item.defaults)
|
|
270
|
+
lines.push(`Applied default: inputs[${index}].${applied.param}=${applied.value} (${applied.reason}); this effective value is used for pricing and submission.`);
|
|
271
|
+
});
|
|
272
|
+
const prices = results.map(result => itemPrice(result.estimate));
|
|
273
|
+
const same = prices.every(price => price === prices[0]);
|
|
274
|
+
const each = same && count > 1 ? `; ${prices[0]} each` : '';
|
|
275
|
+
const range = formatEstimateRange(combined);
|
|
276
|
+
if (status === 'estimated' && range)
|
|
277
|
+
lines.push(`Estimated cost: ${range} in total for ${count} input${count === 1 ? '' : 's'}${each}.`);
|
|
278
|
+
else if (status === 'partial' && range)
|
|
279
|
+
lines.push(`Partial estimate at published rates: ${range} in total (not a total or an upper bound; partial for ${itemsText(total.partial)}).`);
|
|
280
|
+
else if (status === 'token_billed' && range)
|
|
281
|
+
lines.push(`Reference price: ${range} in total for ${count} input${count === 1 ? '' : 's'}${each}. Billed by actual token usage and settled when each task finishes, so the final charge may be lower or slightly higher.`);
|
|
282
|
+
else if (status === 'needs_input')
|
|
283
|
+
lines.push(`Cost: cannot estimate yet. ${itemsText(total.needsInput)} depend${total.needsInput.length === 1 ? 's' : ''} on usage this request does not give:`, ...(combined.usage_help ?? []).map(line => `- ${line}`));
|
|
284
|
+
if (!same)
|
|
285
|
+
lines.push('Per input:', ...prices.map((price, index) => `- inputs[${index}]: ${price}`));
|
|
286
|
+
if (combined.basis.length)
|
|
287
|
+
lines.push('Based on:', ...combined.basis.map(line => `- ${line}`));
|
|
288
|
+
if (combined.possible_extras.length)
|
|
289
|
+
lines.push('May also charge:', ...combined.possible_extras.map(line => `- ${line}`));
|
|
290
|
+
for (const note of combined.notes)
|
|
291
|
+
lines.push(`Note: ${note}`);
|
|
292
|
+
// Without a total for every input there is nothing to approve: no amounts and no approval record.
|
|
293
|
+
const quoted = status !== 'needs_input';
|
|
294
|
+
const rule = results[0].rule;
|
|
295
|
+
const structured = {
|
|
296
|
+
model: entry.id,
|
|
297
|
+
type: entry.kind,
|
|
298
|
+
batch,
|
|
299
|
+
pricing_scope: 'public_default',
|
|
300
|
+
pricing_source: 'pricing_rules',
|
|
301
|
+
...(quoted ? { pricing_quote: total.approval, amounts: total.amounts } : { submission_allowed: false }),
|
|
302
|
+
total_known: status === 'estimated',
|
|
303
|
+
...(combined.missing_usage ? { missing_usage: combined.missing_usage } : {}),
|
|
304
|
+
items: results.map((result, index) => ({ index, status: result.estimate.status, total_known: result.estimate.total_known === true,
|
|
305
|
+
...(result.estimate.status === 'needs_input' ? {} : { amounts: result.amounts }),
|
|
306
|
+
...(result.estimate.missing_usage ? { missing_usage: result.estimate.missing_usage } : {}),
|
|
307
|
+
...(result.estimate.search_charges?.length ? { search_charges: result.estimate.search_charges } : {}),
|
|
308
|
+
...(prepared[index].applies ? { effective_input: prepared[index].input, defaults_applied: prepared[index].defaults } : {}) })),
|
|
309
|
+
policy_version: rule.policy_version,
|
|
310
|
+
policy_checksum: rule.policy_checksum,
|
|
311
|
+
settlement_basis: rule.settlement_basis,
|
|
312
|
+
...(quoted ? { expires_at: total.approval.expires_at } : {}),
|
|
313
|
+
...(readAt ? { rules_read_at: readAt } : {}),
|
|
314
|
+
final_budget_enforced: false,
|
|
315
|
+
input_valid: entry.spec ? true : null,
|
|
316
|
+
problems: [],
|
|
317
|
+
warnings,
|
|
318
|
+
estimate: combined,
|
|
319
|
+
};
|
|
320
|
+
await addBalance(config, combined, lines, structured);
|
|
321
|
+
if (quoted)
|
|
322
|
+
lines.push(`Estimate valid until ${total.approval.expires_at}.`, status === 'token_billed'
|
|
323
|
+
? `Public default reference price; account discounts are not included. Confirm it for the whole batch before generation. The final charge follows actual token usage.`
|
|
324
|
+
: `Public default estimate; account discounts are not included. Confirm this estimated cost for the whole batch before generation. Final charges depend on actual usage.`, `One approval covers all ${count} input${count === 1 ? '' : 's'}: pass this pricing_quote with the same inputs, in the same order, to generate_${entry.kind}.`);
|
|
325
|
+
else
|
|
326
|
+
lines.push('No total or approval record was issued. Estimate again with the missing usage before asking the user to approve a generation.');
|
|
327
|
+
return ok(lines.join('\n'), structured);
|
|
328
|
+
}
|