@wiro-ai/wiro-mcp 1.5.8 → 1.6.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 +14 -12
- package/dist/client.d.ts +4 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +6 -1
- package/dist/client.js.map +1 -1
- package/dist/instructions.js +2 -2
- package/dist/instructions.js.map +1 -1
- package/dist/playbooks.d.ts.map +1 -1
- package/dist/playbooks.js +44 -40
- package/dist/playbooks.js.map +1 -1
- package/dist/prompts.js +3 -3
- package/dist/prompts.js.map +1 -1
- package/dist/tools/apply-agent-skills.d.ts.map +1 -1
- package/dist/tools/apply-agent-skills.js +102 -639
- package/dist/tools/apply-agent-skills.js.map +1 -1
- package/dist/tools/connect-agent-oauth.d.ts.map +1 -1
- package/dist/tools/connect-agent-oauth.js +260 -111
- package/dist/tools/connect-agent-oauth.js.map +1 -1
- package/dist/tools/control-agent.d.ts.map +1 -1
- package/dist/tools/control-agent.js +20 -9
- package/dist/tools/control-agent.js.map +1 -1
- package/dist/tools/deploy-agent.d.ts.map +1 -1
- package/dist/tools/deploy-agent.js +42 -28
- package/dist/tools/deploy-agent.js.map +1 -1
- package/dist/tools/get-agent-activity.d.ts.map +1 -1
- package/dist/tools/get-agent-activity.js +3 -1
- package/dist/tools/get-agent-activity.js.map +1 -1
- package/dist/tools/get-agent-conversation.d.ts.map +1 -1
- package/dist/tools/get-agent-conversation.js +7 -4
- package/dist/tools/get-agent-conversation.js.map +1 -1
- package/dist/tools/get-agent-message.d.ts.map +1 -1
- package/dist/tools/get-agent-message.js +5 -1
- package/dist/tools/get-agent-message.js.map +1 -1
- package/dist/tools/get-agent-template.d.ts.map +1 -1
- package/dist/tools/get-agent-template.js +11 -9
- package/dist/tools/get-agent-template.js.map +1 -1
- package/dist/tools/get-my-agent.js +4 -4
- package/dist/tools/get-my-agent.js.map +1 -1
- package/dist/tools/list-agent-skills.d.ts.map +1 -1
- package/dist/tools/list-agent-skills.js +22 -17
- package/dist/tools/list-agent-skills.js.map +1 -1
- package/dist/tools/list-agent-templates.d.ts.map +1 -1
- package/dist/tools/list-agent-templates.js +16 -10
- package/dist/tools/list-agent-templates.js.map +1 -1
- package/dist/tools/list-my-agents.d.ts.map +1 -1
- package/dist/tools/list-my-agents.js +2 -2
- package/dist/tools/list-my-agents.js.map +1 -1
- package/dist/tools/manage-agent-billing.d.ts.map +1 -1
- package/dist/tools/manage-agent-billing.js +476 -187
- package/dist/tools/manage-agent-billing.js.map +1 -1
- package/dist/tools/send-agent-message.d.ts.map +1 -1
- package/dist/tools/send-agent-message.js +10 -5
- package/dist/tools/send-agent-message.js.map +1 -1
- package/dist/tools/set-agent-credentials.d.ts.map +1 -1
- package/dist/tools/set-agent-credentials.js +24 -6
- package/dist/tools/set-agent-credentials.js.map +1 -1
- package/dist/tools/update-agent.d.ts.map +1 -1
- package/dist/tools/update-agent.js +2 -1
- package/dist/tools/update-agent.js.map +1 -1
- package/dist/utils/agent-catalog.d.ts +16 -12
- package/dist/utils/agent-catalog.d.ts.map +1 -1
- package/dist/utils/agent-catalog.js +4 -11
- package/dist/utils/agent-catalog.js.map +1 -1
- package/dist/utils/agent-config.d.ts +98 -45
- package/dist/utils/agent-config.d.ts.map +1 -1
- package/dist/utils/agent-config.js +246 -147
- package/dist/utils/agent-config.js.map +1 -1
- package/dist/utils/agent-message.d.ts +18 -0
- package/dist/utils/agent-message.d.ts.map +1 -1
- package/dist/utils/agent-setup.d.ts +53 -2
- package/dist/utils/agent-setup.d.ts.map +1 -1
- package/dist/utils/agent-setup.js +371 -22
- package/dist/utils/agent-setup.js.map +1 -1
- package/dist/utils/agent-targets.d.ts.map +1 -1
- package/dist/utils/agent-targets.js +9 -2
- package/dist/utils/agent-targets.js.map +1 -1
- package/dist/utils/agent-turns.d.ts +18 -0
- package/dist/utils/agent-turns.d.ts.map +1 -1
- package/dist/utils/agent-turns.js +73 -14
- package/dist/utils/agent-turns.js.map +1 -1
- package/dist/utils/api.d.ts +1 -1
- package/dist/utils/api.d.ts.map +1 -1
- package/dist/utils/api.js +3 -3
- package/dist/utils/api.js.map +1 -1
- package/dist/utils/quoted-text.d.ts +7 -0
- package/dist/utils/quoted-text.d.ts.map +1 -1
- package/dist/utils/quoted-text.js +14 -0
- package/dist/utils/quoted-text.js.map +1 -1
- package/dist/utils/user-agent.d.ts +76 -18
- package/dist/utils/user-agent.d.ts.map +1 -1
- package/dist/utils/user-agent.js +202 -49
- package/dist/utils/user-agent.js.map +1 -1
- package/package.json +1 -1
|
@@ -4,9 +4,9 @@ import { ApiResultError, asString, errorResult, errorText, isPlainObject, requir
|
|
|
4
4
|
import { daysLeftText, readPlanChange, underOneDayLeft, workspaceHint } from '../utils/agent-config.js';
|
|
5
5
|
import { DEFAULT_WAIT_SECONDS } from '../utils/poll.js';
|
|
6
6
|
import { isPlainTeamMember, teamAgentBillingRefused } from '../utils/workspace.js';
|
|
7
|
-
import { AMOUNT_TOLERANCE_USD, WALLET_NOT_READ_NOTE, agentDashboardUrl, agentSummarySchema,
|
|
7
|
+
import { AGENT_PLANS, AMOUNT_TOLERANCE_USD, WALLET_NOT_READ_NOTE, agentDashboardUrl, agentSummarySchema, asPlan, chargeRoute, comparePlans, formatAgentSummaryLines, formatCredits, formatUsd, isTransitionalStatus, isUnsettledPlan, isWalletNotRead, outcomeUnknown, planLineText, planName, readAgentDetail, roundUsd, sameAmount, toAgentDetail, toAgentSummary, unsettledPlanText, } from '../utils/user-agent.js';
|
|
8
8
|
import { nextActionSchema, strictInput } from './schemas.js';
|
|
9
|
-
const ACTIONS = ['subscribe', '
|
|
9
|
+
const ACTIONS = ['subscribe', 'change_plan', 'cancel', 'renew', 'buy_credits'];
|
|
10
10
|
const quoteSchema = z.object({
|
|
11
11
|
chargeNowUsd: z.number().nullable(),
|
|
12
12
|
estimated: z.boolean(),
|
|
@@ -14,10 +14,15 @@ const quoteSchema = z.object({
|
|
|
14
14
|
recurringUsd: z.number().nullable().optional(),
|
|
15
15
|
monthlyCredits: z.number().nullable().optional(),
|
|
16
16
|
creditsNow: z.number().optional()
|
|
17
|
-
.describe('
|
|
17
|
+
.describe('change_plan to a larger plan: the credits this period gains now. monthlyCredits is what every renewal gives.'),
|
|
18
18
|
creditsAdded: z.number().optional(),
|
|
19
19
|
paidFrom: z.enum(['personal_wallet', 'team_wallet', 'card']),
|
|
20
|
-
|
|
20
|
+
plan: z.enum(AGENT_PLANS).optional()
|
|
21
|
+
.describe('subscribe and change_plan: the plan this quote is for. renew: the plan it renews on.'),
|
|
22
|
+
change: z.enum(['upgrade', 'downgrade', 'undo_downgrade']).optional()
|
|
23
|
+
.describe('change_plan: upgrade (a larger plan, now), downgrade (a smaller plan, from the next renewal) or undo_downgrade (keep the plan, and undo the move set for the next renewal).'),
|
|
24
|
+
startsAt: z.string().optional()
|
|
25
|
+
.describe('change_plan to a smaller plan: when the period ends; the new plan starts with the renewal after it.'),
|
|
21
26
|
pack: z.string().optional(),
|
|
22
27
|
});
|
|
23
28
|
const resultSchema = z.object({
|
|
@@ -25,10 +30,14 @@ const resultSchema = z.object({
|
|
|
25
30
|
monthlyPriceUsd: z.number().optional(),
|
|
26
31
|
monthlyCredits: z.number().optional(),
|
|
27
32
|
creditsNow: z.number().optional()
|
|
28
|
-
.describe('
|
|
33
|
+
.describe('change_plan to a larger plan: the credits this period gained with it. monthlyCredits is what every renewal gives.'),
|
|
29
34
|
creditsAdded: z.number().optional(),
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
plan: z.string().optional(),
|
|
36
|
+
previousPlan: z.string().optional(),
|
|
37
|
+
nextPlan: z.string().nullable().optional()
|
|
38
|
+
.describe('change_plan: the plan set for the next renewal, or null when none is.'),
|
|
39
|
+
change: z.enum(['upgrade', 'downgrade', 'undo_downgrade']).optional(),
|
|
40
|
+
startsAt: z.string().optional(),
|
|
32
41
|
cancelsAt: z.string().optional(),
|
|
33
42
|
renewal: z.enum(['undo_cancel', 'renewed']).optional(),
|
|
34
43
|
});
|
|
@@ -51,7 +60,7 @@ function waitAction(guid, reason) {
|
|
|
51
60
|
};
|
|
52
61
|
}
|
|
53
62
|
/**
|
|
54
|
-
* The wait after
|
|
63
|
+
* The wait after a plan change, a pack or a renewal, which keep a running agent up:
|
|
55
64
|
* only an agent on its way, one that was stopping (it starts again) or one queued
|
|
56
65
|
* or starting, is worth waiting for.
|
|
57
66
|
*/
|
|
@@ -91,7 +100,7 @@ function subscribeQuoteNote(status) {
|
|
|
91
100
|
function subscribeWait(agent) {
|
|
92
101
|
return subscribeStarts(agent.status) ? waitAction(agent.guid, 'Wait until the agent is running.') : undefined;
|
|
93
102
|
}
|
|
94
|
-
/** What
|
|
103
|
+
/** What a move to a larger plan does to this period's credits. An allowance above the new plan's is cut to it for the time left. */
|
|
95
104
|
function upgradeCreditsText(creditsNow, renewal, done) {
|
|
96
105
|
if (creditsNow > 0) {
|
|
97
106
|
return `${formatCredits(creditsNow)} credits ${done ? 'came with it' : 'come now'} for the rest of this period${renewal}. `;
|
|
@@ -124,13 +133,16 @@ function planChangeAgentNote(status, what, quote = false) {
|
|
|
124
133
|
}
|
|
125
134
|
}
|
|
126
135
|
/**
|
|
127
|
-
* The
|
|
128
|
-
*
|
|
129
|
-
*
|
|
136
|
+
* The price of the plan asked for and what moving to it does today, as Wiro works it out on the plan the
|
|
137
|
+
* agent has now. null when it cannot be read, or when the answer is for another plan: the quote then falls
|
|
138
|
+
* back to the plan's price the agent shows and to the most the move can charge.
|
|
130
139
|
*/
|
|
131
|
-
async function
|
|
140
|
+
async function readPlanPreview(client, guid, plan, signal) {
|
|
132
141
|
try {
|
|
133
|
-
const body = requireApiSuccess(await client.post('/UserAgent/PricingPreview', { useragentguid: guid, tier:
|
|
142
|
+
const body = requireApiSuccess(await client.post('/UserAgent/PricingPreview', { useragentguid: guid, tier: plan }, signal), `The ${planName(plan)} price could not be read.`);
|
|
143
|
+
const priced = asString(body.tier);
|
|
144
|
+
if (priced !== undefined && priced !== plan)
|
|
145
|
+
return null;
|
|
134
146
|
const priceUsd = toNumber(body.totalPriceUsd);
|
|
135
147
|
const monthlyCredits = toNumber(body.totalMonthlyCredits);
|
|
136
148
|
return {
|
|
@@ -144,31 +156,53 @@ async function readProPreview(client, guid, signal) {
|
|
|
144
156
|
return null;
|
|
145
157
|
}
|
|
146
158
|
}
|
|
147
|
-
|
|
148
|
-
|
|
159
|
+
/** A plan's price as said in a sentence: "$30.00 every 30 days with 3,000 monthly credits". */
|
|
160
|
+
function planTerms(price) {
|
|
161
|
+
return `${formatUsd(price.priceUsd)} every 30 days with ${formatCredits(price.monthlyCredits)} monthly credits`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* What the next renewal charges and grants: the plan set for it, or the agent's own plan, at today's price.
|
|
165
|
+
* null when the plans' prices cannot be read.
|
|
166
|
+
*/
|
|
167
|
+
function renewalPlanPrice(agent) {
|
|
168
|
+
const key = asPlan(agent.nextPlan) ?? asPlan(agent.tier);
|
|
169
|
+
return key ? agent.pricing?.[key] ?? null : null;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* What the wallet paid for a plan, as Wiro answers it (chargedUsd), with the plan's price when a discount on the
|
|
173
|
+
* account took the charge below it. Without that answer, the plan's price.
|
|
174
|
+
*/
|
|
175
|
+
function walletPaid(body, priceUsd) {
|
|
176
|
+
const chargedUsd = toNumber(body.chargedUsd) ?? priceUsd;
|
|
177
|
+
const discounted = chargedUsd < priceUsd && !sameAmount(chargedUsd, priceUsd);
|
|
178
|
+
return { chargedUsd, text: `${formatUsd(chargedUsd)}${discounted ? ` (the ${formatUsd(priceUsd)} price after a discount)` : ''}` };
|
|
149
179
|
}
|
|
150
180
|
/** The amount a confirmed call charges now, or at most when that is worked out only then; null when it charges nothing. */
|
|
151
181
|
function chargeOf(quote) {
|
|
152
182
|
const amount = quote?.chargeNowUsd ?? quote?.maxChargeUsd ?? null;
|
|
153
183
|
return amount !== null && amount > 0 ? amount : null;
|
|
154
184
|
}
|
|
185
|
+
/** A move to a larger plan is approved at the most it can charge, a ceiling; every other charge at its exact amount. */
|
|
186
|
+
function approvedAsCeiling(action, quote) {
|
|
187
|
+
return action === 'change_plan' && quote?.change === 'upgrade';
|
|
188
|
+
}
|
|
155
189
|
/**
|
|
156
190
|
* The amount the user approves with confirm_charge_usd, or null when the
|
|
157
|
-
* call charges nothing.
|
|
158
|
-
* amount Wiro works out for a wallet plan, which only shrinks as the
|
|
159
|
-
* runs on, or the full price difference when Wiro works the amount out
|
|
160
|
-
* at the
|
|
191
|
+
* call charges nothing. A move to a larger plan is approved at the most it can
|
|
192
|
+
* charge: the amount Wiro works out for a wallet plan, which only shrinks as the
|
|
193
|
+
* period runs on, or the full price difference when Wiro works the amount out
|
|
194
|
+
* only at the change.
|
|
161
195
|
*/
|
|
162
196
|
function approvalAmountOf(action, quote) {
|
|
163
|
-
if (action
|
|
197
|
+
if (!approvedAsCeiling(action, quote))
|
|
164
198
|
return chargeOf(quote);
|
|
165
199
|
const max = quote?.maxChargeUsd ?? null;
|
|
166
200
|
return max !== null && max > 0 ? max : null;
|
|
167
201
|
}
|
|
168
202
|
/**
|
|
169
203
|
* Why a confirmed call may not charge yet: the approved amount is missing,
|
|
170
|
-
* or the fresh quote no longer matches it. For
|
|
171
|
-
* amount is a ceiling.
|
|
204
|
+
* or the fresh quote no longer matches it. For a move to a larger plan the
|
|
205
|
+
* approved amount is a ceiling.
|
|
172
206
|
*/
|
|
173
207
|
function approvalProblem(action, quote, approved) {
|
|
174
208
|
const amount = approvalAmountOf(action, quote);
|
|
@@ -176,15 +210,15 @@ function approvalProblem(action, quote, approved) {
|
|
|
176
210
|
return null;
|
|
177
211
|
if (approved === undefined)
|
|
178
212
|
return 'missing';
|
|
179
|
-
const fits = action
|
|
213
|
+
const fits = approvedAsCeiling(action, quote)
|
|
180
214
|
? amount <= approved + AMOUNT_TOLERANCE_USD
|
|
181
215
|
: sameAmount(amount, approved);
|
|
182
216
|
return fits ? null : 'changed';
|
|
183
217
|
}
|
|
184
218
|
/** The first lines of a new quote when the approved amount no longer fits. */
|
|
185
|
-
function changedLead(action, amount, approved, callerRequestId) {
|
|
186
|
-
if (action
|
|
187
|
-
return `The most this
|
|
219
|
+
function changedLead(action, quote, amount, approved, callerRequestId) {
|
|
220
|
+
if (approvedAsCeiling(action, quote)) {
|
|
221
|
+
return `The most this plan change can charge changed since the quote: it is now ${formatUsd(amount)}, and the user approved at most ${formatUsd(approved)}. Nothing was charged. Show the new amount to the user and ask again.`;
|
|
188
222
|
}
|
|
189
223
|
const changed = `The amount changed since the quote: it is now ${formatUsd(amount)}, and the user approved ${formatUsd(approved)}.`;
|
|
190
224
|
if (action === 'buy_credits' && callerRequestId) {
|
|
@@ -194,6 +228,14 @@ function changedLead(action, amount, approved, callerRequestId) {
|
|
|
194
228
|
}
|
|
195
229
|
return `${changed} Nothing was charged. Show the new amount to the user and ask again.`;
|
|
196
230
|
}
|
|
231
|
+
/** The plans with their prices, for a call that did not name one, and the plan the agent is on. */
|
|
232
|
+
function plansListing(agent) {
|
|
233
|
+
const listed = agent.pricing ? planLineText(agent.pricing) : '';
|
|
234
|
+
const on = agent.tier ? ` The agent is on ${planName(agent.tier)}.` : '';
|
|
235
|
+
return listed
|
|
236
|
+
? `Choose a plan: ${listed}.${on} A move to a larger plan starts now; a move to a smaller one starts at the next renewal.`
|
|
237
|
+
: `Choose a plan: starter, pro or max.${on} The plans' prices could not be read just now.`;
|
|
238
|
+
}
|
|
197
239
|
async function planFor(client, action, agent, raw, input, signal) {
|
|
198
240
|
const guid = agent.guid;
|
|
199
241
|
const active = raw?.status === 'active';
|
|
@@ -204,18 +246,29 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
204
246
|
const wallet = agent.workspace === 'team' ? 'the team wallet' : 'the personal wallet';
|
|
205
247
|
const periodEnd = agent.subscription?.periodEndsAt ?? 'the end of the current period';
|
|
206
248
|
const cardPlan = (what) => `This plan is billed by card, and card payments are made in the Wiro dashboard. Nothing was charged here. To ${what}, open ${agent.dashboardUrl}`;
|
|
249
|
+
// A plan that has not ended and is not active, such as a card plan whose last payment did not go through
|
|
250
|
+
// (past_due, then unpaid). Wiro counts every status but expired, cancelled and refunded as a plan that has
|
|
251
|
+
// not ended: it starts no new plan beside one, and renews, changes, cancels and tops up only an active plan.
|
|
252
|
+
// lead says what this action cannot do; null for any other plan.
|
|
253
|
+
const unsettled = (lead) => (agent.subscription && isUnsettledPlan(agent.subscription)
|
|
254
|
+
? unsettledPlanText(agent.subscription, agent, lead)
|
|
255
|
+
: null);
|
|
207
256
|
switch (action) {
|
|
208
257
|
case 'subscribe': {
|
|
209
258
|
if (active) {
|
|
210
259
|
return {
|
|
211
260
|
outcome: 'refused',
|
|
212
|
-
message: 'This agent already has an active plan. Use renew to undo a cancellation,
|
|
261
|
+
message: 'This agent already has an active plan. Use renew to undo a cancellation, change_plan to move to another plan, or buy_credits for more credits.',
|
|
213
262
|
};
|
|
214
263
|
}
|
|
215
|
-
|
|
216
|
-
const
|
|
264
|
+
// Its payment is fixed instead: a second plan would bill beside it.
|
|
265
|
+
const open = unsettled('A new plan is not started beside it, since both would bill. Nothing was charged.');
|
|
266
|
+
if (open)
|
|
267
|
+
return { outcome: 'refused', message: open };
|
|
268
|
+
const plan = input.plan ?? asPlan(agent.tier) ?? 'starter';
|
|
269
|
+
const price = agent.pricing?.[plan];
|
|
217
270
|
if (!price)
|
|
218
|
-
throw new Error(
|
|
271
|
+
throw new Error(`The ${planName(plan)} plan cannot be priced right now, so the agent cannot be subscribed. Try again later.`);
|
|
219
272
|
const missing = agent.missingCredentials;
|
|
220
273
|
return {
|
|
221
274
|
outcome: 'quote',
|
|
@@ -225,9 +278,9 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
225
278
|
recurringUsd: price.priceUsd,
|
|
226
279
|
monthlyCredits: price.monthlyCredits,
|
|
227
280
|
paidFrom,
|
|
228
|
-
|
|
281
|
+
plan,
|
|
229
282
|
},
|
|
230
|
-
message: `Subscribe on ${
|
|
283
|
+
message: `Subscribe on ${planName(plan)}: ${formatUsd(price.priceUsd)} now from ${wallet}, then every 30 days until the plan is cancelled, with ${formatCredits(price.monthlyCredits)} credits per month. `
|
|
231
284
|
+ subscribeQuoteNote(agent.status)
|
|
232
285
|
+ (missing.length > 0
|
|
233
286
|
? ` Missing credentials (${missing.map(item => item.title).join(', ')}) can be added afterwards; until then the skills that need them fail.`
|
|
@@ -236,15 +289,16 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
236
289
|
const body = requireApiSuccess(await client.post('/UserAgent/CreateSubscriptionCheckout', {
|
|
237
290
|
useragentguid: guid,
|
|
238
291
|
useprepaid: true,
|
|
239
|
-
tier,
|
|
292
|
+
tier: plan,
|
|
240
293
|
}, signal), 'The plan could not be started.');
|
|
241
|
-
const
|
|
294
|
+
const monthly = toNumber(body.monthlypriceusd) ?? price.priceUsd;
|
|
295
|
+
const paid = walletPaid(body, monthly);
|
|
242
296
|
const credits = toNumber(body.monthlycredits) ?? price.monthlyCredits;
|
|
243
297
|
return {
|
|
244
298
|
outcome: 'done',
|
|
245
|
-
message: `Subscribed on ${
|
|
299
|
+
message: `Subscribed on ${planName(plan)} and charged ${paid.text} from ${wallet}. The plan renews every 30 days until it is cancelled. `
|
|
246
300
|
+ (subscribeStarts(agent.status) ? 'The agent is starting.' : 'The agent keeps running: a new plan does not restart it.'),
|
|
247
|
-
result: { chargedUsd:
|
|
301
|
+
result: { chargedUsd: paid.chargedUsd, monthlyPriceUsd: monthly, monthlyCredits: credits, plan },
|
|
248
302
|
nextAction: subscribeWait(agent),
|
|
249
303
|
};
|
|
250
304
|
},
|
|
@@ -276,11 +330,22 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
276
330
|
if (!cancelled) {
|
|
277
331
|
return { outcome: 'not_needed', message: 'The plan is active and renews by itself. Nothing to do.' };
|
|
278
332
|
}
|
|
279
|
-
|
|
333
|
+
// A renewal charges the plan set for it, or the agent's own, at today's price. A wallet cancel keeps a
|
|
334
|
+
// move set for the renewal, so the plan is named: it can be another plan than the agent's.
|
|
335
|
+
const next = renewalPlanPrice(agent);
|
|
336
|
+
const renewsOn = asPlan(agent.nextPlan) ?? asPlan(agent.tier);
|
|
337
|
+
const amount = next?.priceUsd ?? (agent.monthlyPriceUsd || agent.subscription?.amountUsd);
|
|
280
338
|
return {
|
|
281
339
|
outcome: 'quote',
|
|
282
|
-
quote: {
|
|
283
|
-
|
|
340
|
+
quote: {
|
|
341
|
+
chargeNowUsd: 0,
|
|
342
|
+
estimated: false,
|
|
343
|
+
recurringUsd: amount,
|
|
344
|
+
monthlyCredits: next?.monthlyCredits ?? agent.credits.renewal ?? agent.credits.monthly,
|
|
345
|
+
paidFrom,
|
|
346
|
+
...(renewsOn ? { plan: renewsOn } : {}),
|
|
347
|
+
},
|
|
348
|
+
message: `Undo the cancellation: nothing is charged now. The plan then renews${renewsOn ? ` on ${planName(renewsOn)}` : ''} from ${wallet} for ${formatUsd(amount)} after ${periodEnd} and every 30 days after that until it is cancelled.`,
|
|
284
349
|
confirmReason: 'Ask the user to confirm first: the plan will charge the wallet again at each renewal.',
|
|
285
350
|
perform: async (signal) => {
|
|
286
351
|
requireApiSuccess(await client.post('/UserAgent/RenewSubscription', { guid }, signal), 'The cancellation could not be undone.');
|
|
@@ -292,6 +357,9 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
292
357
|
},
|
|
293
358
|
};
|
|
294
359
|
}
|
|
360
|
+
const open = unsettled('Action renew brings back only a plan that has ended.');
|
|
361
|
+
if (open)
|
|
362
|
+
return { outcome: 'refused', message: open };
|
|
295
363
|
if (raw.status !== 'expired') {
|
|
296
364
|
return {
|
|
297
365
|
outcome: 'refused',
|
|
@@ -306,7 +374,15 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
306
374
|
nextAction: billingCall(guid, 'subscribe'),
|
|
307
375
|
};
|
|
308
376
|
}
|
|
309
|
-
const
|
|
377
|
+
const own = agent.monthlyPriceUsd ?? toNumber(raw.amount);
|
|
378
|
+
const planKey = asPlan(agent.nextPlan) ?? asPlan(agent.tier);
|
|
379
|
+
// A renewal charges the plan set for it, or the agent's own, at its price today, which can differ
|
|
380
|
+
// from the price the plan ended at.
|
|
381
|
+
const today = renewalPlanPrice(agent);
|
|
382
|
+
const earlier = today && own && own > 0 && !sameAmount(today.priceUsd, own)
|
|
383
|
+
? ` The plan ended at ${formatUsd(own)} a month; it renews at the ${planName(planKey)} price today, with ${formatCredits(today.monthlyCredits)} credits per month.`
|
|
384
|
+
: '';
|
|
385
|
+
const price = today?.priceUsd ?? own;
|
|
310
386
|
if (!price || price <= 0) {
|
|
311
387
|
return {
|
|
312
388
|
outcome: 'refused',
|
|
@@ -314,29 +390,32 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
314
390
|
nextAction: billingCall(guid, 'subscribe'),
|
|
315
391
|
};
|
|
316
392
|
}
|
|
317
|
-
const tier = asTier(agent.tier);
|
|
318
393
|
return {
|
|
319
394
|
outcome: 'quote',
|
|
320
395
|
quote: {
|
|
321
396
|
chargeNowUsd: price,
|
|
322
397
|
estimated: false,
|
|
323
398
|
recurringUsd: price,
|
|
324
|
-
monthlyCredits:
|
|
399
|
+
monthlyCredits: today?.monthlyCredits ?? null,
|
|
325
400
|
paidFrom,
|
|
326
|
-
...(
|
|
401
|
+
...(planKey ? { plan: planKey } : {}),
|
|
327
402
|
},
|
|
328
|
-
message: `Renew the expired plan: ${formatUsd(price)} now from ${wallet}, then every 30 days until it is cancelled
|
|
403
|
+
message: `Renew the expired plan${planKey ? ` on ${planName(planKey)}` : ''}: ${formatUsd(price)} now from ${wallet}, then every 30 days until it is cancelled.${earlier} A stopped or failed agent is queued to start again.`,
|
|
329
404
|
perform: async (signal) => {
|
|
330
405
|
const body = requireApiSuccess(await client.post('/UserAgent/RenewSubscription', { guid }, signal), 'The plan could not be renewed.');
|
|
331
|
-
const
|
|
406
|
+
const monthly = toNumber(body.amount) ?? price;
|
|
407
|
+
const paid = walletPaid(body, monthly);
|
|
332
408
|
const credits = toNumber(body.monthlycredits);
|
|
409
|
+
// The plan it renewed on, which the wallet line names too.
|
|
410
|
+
const renewedOn = asPlan(body.tier) ?? planKey;
|
|
333
411
|
return {
|
|
334
412
|
outcome: 'done',
|
|
335
|
-
message: `Renewed
|
|
413
|
+
message: `Renewed${renewedOn ? ` on ${planName(renewedOn)}` : ''} and charged ${paid.text} from ${wallet}. The plan renews every 30 days until it is cancelled.`,
|
|
336
414
|
result: {
|
|
337
|
-
chargedUsd:
|
|
338
|
-
monthlyPriceUsd:
|
|
415
|
+
chargedUsd: paid.chargedUsd,
|
|
416
|
+
monthlyPriceUsd: monthly,
|
|
339
417
|
...(credits !== null ? { monthlyCredits: credits } : {}),
|
|
418
|
+
...(renewedOn ? { plan: renewedOn } : {}),
|
|
340
419
|
renewal: 'renewed',
|
|
341
420
|
},
|
|
342
421
|
nextAction: renewWait(agent),
|
|
@@ -345,8 +424,17 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
345
424
|
};
|
|
346
425
|
}
|
|
347
426
|
case 'cancel': {
|
|
348
|
-
if (!active)
|
|
349
|
-
|
|
427
|
+
if (!active) {
|
|
428
|
+
const open = unsettled('Only an active plan is cancelled here.');
|
|
429
|
+
return open ? { outcome: 'refused', message: open } : { outcome: 'not_needed', message: 'There is no active plan to cancel.' };
|
|
430
|
+
}
|
|
431
|
+
// A move set for the next renewal. A card plan's cancel drops it. A wallet cancel keeps it: an undo
|
|
432
|
+
// renews on it, and the plan's end shows the plan on it, so renew brings it back there.
|
|
433
|
+
const moveSet = asPlan(agent.nextPlan) && agent.nextPlan !== agent.tier ? agent.nextPlan : null;
|
|
434
|
+
const dropsMove = moveSet ? ` The move to ${planName(moveSet)} set for the next renewal is dropped with it.` : '';
|
|
435
|
+
const keepsMove = moveSet
|
|
436
|
+
? ` The move to ${planName(moveSet)} set for the next renewal stays: if the cancellation is undone, the plan renews on ${planName(moveSet)}, and once it has ended, action renew brings it back on ${planName(moveSet)}.`
|
|
437
|
+
: '';
|
|
350
438
|
const performCancel = async (signal) => {
|
|
351
439
|
const body = requireApiSuccess(await client.post('/UserAgent/CancelSubscription', { guid }, signal), 'The plan could not be cancelled.');
|
|
352
440
|
const cancelsAt = unixToIso(body.cancelsAt);
|
|
@@ -376,7 +464,7 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
376
464
|
return {
|
|
377
465
|
outcome: 'quote',
|
|
378
466
|
quote: { chargeNowUsd: 0, estimated: false, paidFrom: 'card' },
|
|
379
|
-
message: `Cancel the team's card plan at the end of the current period (${periodEnd}). Nothing is refunded
|
|
467
|
+
message: `Cancel the team's card plan at the end of the current period (${periodEnd}). Nothing is refunded.${dropsMove}`,
|
|
380
468
|
confirmReason: 'Ask the user to confirm the cancellation first.',
|
|
381
469
|
perform: performCancel,
|
|
382
470
|
};
|
|
@@ -387,112 +475,250 @@ async function planFor(client, action, agent, raw, input, signal) {
|
|
|
387
475
|
return {
|
|
388
476
|
outcome: 'quote',
|
|
389
477
|
quote: { chargeNowUsd: 0, estimated: false, paidFrom },
|
|
390
|
-
message: `Cancel the plan: nothing is refunded and it stops renewing. The agent keeps its monthly credits until about a day after ${periodEnd}; then the monthly credits end and the agent stops unless it has extra credits left. Action renew undoes the cancellation before then
|
|
478
|
+
message: `Cancel the plan: nothing is refunded and it stops renewing. The agent keeps its monthly credits until about a day after ${periodEnd}; then the monthly credits end and the agent stops unless it has extra credits left. Action renew undoes the cancellation before then.${keepsMove}`,
|
|
391
479
|
confirmReason: 'Ask the user to confirm the cancellation first.',
|
|
392
480
|
perform: performCancel,
|
|
393
481
|
};
|
|
394
482
|
}
|
|
395
|
-
case '
|
|
483
|
+
case 'change_plan': {
|
|
484
|
+
if (!input.plan) {
|
|
485
|
+
const larger = AGENT_PLANS.find(plan => {
|
|
486
|
+
const current = asPlan(agent.tier);
|
|
487
|
+
return current ? comparePlans(plan, current) > 0 : false;
|
|
488
|
+
});
|
|
489
|
+
return {
|
|
490
|
+
outcome: 'refused',
|
|
491
|
+
...(agent.pricing ? { heading: 'Plans' } : {}),
|
|
492
|
+
message: plansListing(agent),
|
|
493
|
+
...(active && larger ? { nextAction: billingCall(guid, 'change_plan', { plan: larger }) } : {}),
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
const target = input.plan;
|
|
497
|
+
const current = asPlan(agent.tier);
|
|
396
498
|
if (!active) {
|
|
397
|
-
|
|
398
|
-
if (
|
|
499
|
+
const open = unsettled('A plan change needs an active plan.');
|
|
500
|
+
if (open)
|
|
501
|
+
return { outcome: 'refused', message: open };
|
|
502
|
+
// A wallet plan that ended comes back with renew on the plan set for its renewal, else on its own
|
|
503
|
+
// plan, so renew answers a target that is that plan; any other target takes a new plan. When the plan
|
|
504
|
+
// ends, Wiro writes that plan on the agent: the one set for its renewal, else the one nearest its last price.
|
|
505
|
+
const renewsOn = asPlan(agent.nextPlan) ?? current;
|
|
506
|
+
if (renewsOn === target && raw?.status === 'expired' && prepaid) {
|
|
399
507
|
return {
|
|
400
508
|
outcome: 'refused',
|
|
401
|
-
message:
|
|
509
|
+
message: renewsOn === current
|
|
510
|
+
? `This agent is already on ${planName(target)}, and its plan has ended. Action renew brings the plan back; subscribe starts a new plan.`
|
|
511
|
+
: `This agent's plan has ended, and action renew brings it back on ${planName(target)}, the plan set for its renewal; subscribe starts a new plan.`,
|
|
402
512
|
nextAction: billingCall(guid, 'renew'),
|
|
403
513
|
};
|
|
404
514
|
}
|
|
405
515
|
return {
|
|
406
516
|
outcome: 'refused',
|
|
407
|
-
message:
|
|
408
|
-
nextAction: billingCall(guid, 'subscribe', {
|
|
517
|
+
message: `Changing the plan needs an active plan. Use action subscribe with plan ${target} instead.`,
|
|
518
|
+
nextAction: billingCall(guid, 'subscribe', { plan: target }),
|
|
409
519
|
};
|
|
410
520
|
}
|
|
411
|
-
if (
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
if (
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
521
|
+
if (!current)
|
|
522
|
+
throw new Error('The agent\'s plan could not be read, so it cannot be changed right now. Try again later.');
|
|
523
|
+
const next = asPlan(agent.nextPlan) ?? null;
|
|
524
|
+
const when = agent.subscription?.periodEndsAt ? `the renewal after ${agent.subscription.periodEndsAt}` : 'the next renewal';
|
|
525
|
+
const move = comparePlans(target, current);
|
|
526
|
+
// Wiro changes a team's card plan only in the dashboard, not with a project's key.
|
|
527
|
+
const teamCard = route === 'card' && agent.workspace === 'team';
|
|
528
|
+
if (move === 0) {
|
|
529
|
+
if (!next || next === current) {
|
|
530
|
+
return {
|
|
531
|
+
outcome: 'not_needed',
|
|
532
|
+
message: next === current
|
|
533
|
+
? `The agent is already on ${planName(current)}. From ${when} it is billed at today's ${planName(current)} price${agent.pricing?.[current] ? `, ${planTerms(agent.pricing[current])}` : ''}. Nothing to do.`
|
|
534
|
+
: `The agent is already on ${planName(current)}. Nothing to do.`,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
// A move to another plan is set for the next renewal: keeping this one undoes it. A team's card
|
|
538
|
+
// plan changes only in the dashboard.
|
|
539
|
+
if (teamCard)
|
|
540
|
+
return { outcome: 'refused', message: cardPlan(`keep it on ${planName(current)}`) };
|
|
541
|
+
const keep = agent.pricing?.[current];
|
|
542
|
+
return {
|
|
543
|
+
outcome: 'quote',
|
|
544
|
+
quote: {
|
|
545
|
+
chargeNowUsd: 0,
|
|
546
|
+
estimated: false,
|
|
547
|
+
recurringUsd: keep?.priceUsd ?? null,
|
|
548
|
+
monthlyCredits: keep?.monthlyCredits ?? null,
|
|
549
|
+
paidFrom: route === 'card' ? 'card' : paidFrom,
|
|
550
|
+
plan: current,
|
|
551
|
+
change: 'undo_downgrade',
|
|
552
|
+
},
|
|
553
|
+
message: `Keep ${planName(current)}: the move to ${planName(next)} set for ${when} is undone, and nothing is charged now. `
|
|
554
|
+
+ `The plan then renews on ${planName(current)}${keep ? ` at ${planTerms(keep)}` : ''}.`,
|
|
555
|
+
confirmReason: 'Ask the user to confirm first: the plan then keeps renewing on its current plan.',
|
|
556
|
+
perform: async (signal) => {
|
|
557
|
+
const body = requireApiSuccess(await client.post('/UserAgent/UpgradeTier', { guid, targetTier: current }, signal), 'The move set for the next renewal could not be undone.');
|
|
558
|
+
if (asString(body.url))
|
|
559
|
+
return { outcome: 'refused', message: cardPlan(`keep it on ${planName(current)}`) };
|
|
560
|
+
// An agent on a price from before the plans keeps its plan with that plan set for its renewal, so
|
|
561
|
+
// the renewal bills the plan's own price.
|
|
562
|
+
if (asPlan(body.nextplan) === current) {
|
|
563
|
+
const at = unixToIso(body.startsAt ?? body.periodEnd) ?? agent.subscription?.periodEndsAt;
|
|
564
|
+
const priceUsd = toNumber(body.newPriceUsd) ?? keep?.priceUsd;
|
|
565
|
+
const monthlyCredits = toNumber(body.newMonthlyCredits) ?? keep?.monthlyCredits;
|
|
566
|
+
const terms = priceUsd !== undefined && monthlyCredits !== undefined ? `, ${planTerms({ priceUsd, monthlyCredits })}` : '';
|
|
567
|
+
return {
|
|
568
|
+
outcome: 'done',
|
|
569
|
+
message: `The move to ${planName(next)} is undone. The agent stays on ${planName(current)}, and nothing was charged. `
|
|
570
|
+
+ `It renews at the ${planName(current)} price ${at ? `from ${at}` : 'from the next renewal'}${terms}.`,
|
|
571
|
+
result: { chargedUsd: 0, plan: current, nextPlan: current, change: 'undo_downgrade', ...(at ? { startsAt: at } : {}) },
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
outcome: 'done',
|
|
576
|
+
message: `The move to ${planName(next)} is undone. The agent stays on ${planName(current)}, and nothing was charged.`,
|
|
577
|
+
result: { chargedUsd: 0, plan: current, nextPlan: null, change: 'undo_downgrade' },
|
|
578
|
+
};
|
|
579
|
+
},
|
|
580
|
+
// Nothing set, or the agent's own plan at its price today, is the move undone.
|
|
581
|
+
verify: after => (after.nextPlan && after.nextPlan !== current
|
|
582
|
+
? 'get_my_agent still shows the move set for the next renewal; check it again in a minute.'
|
|
583
|
+
: null),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
if (move > 0) {
|
|
587
|
+
// No checkout pages are opened for card plans: the user pays them in the dashboard.
|
|
588
|
+
if (route === 'card')
|
|
589
|
+
return { outcome: 'refused', message: cardPlan(`move it to ${planName(target)}`) };
|
|
590
|
+
const preview = await readPlanPreview(client, guid, target, signal);
|
|
591
|
+
const targetPrice = preview?.price ?? agent.pricing?.[target];
|
|
592
|
+
if (!targetPrice)
|
|
593
|
+
throw new Error(`The ${planName(target)} price of this agent is not available right now. Try again later.`);
|
|
594
|
+
const ending = `The plan is cancelled, so it ends about a day after ${periodEnd} and does not renew on ${planName(target)}; this charge covers only the time left. To keep the plan, undo the cancellation first with action renew.`;
|
|
595
|
+
const currentPrice = agent.monthlyPriceUsd || agent.subscription?.amountUsd || 0;
|
|
596
|
+
const fullDifference = roundUsd(Math.max(0, targetPrice.priceUsd - currentPrice));
|
|
597
|
+
// A wallet plan pays the amount Wiro works out on it now. A card plan moving to wallet billing is
|
|
598
|
+
// charged from the wallet for the time left of its card period, which Wiro works out at the change.
|
|
599
|
+
const change = preview?.change?.wallet ? preview.change : null;
|
|
600
|
+
const exact = change?.chargeNowUsd ?? null;
|
|
601
|
+
const renewalCredits = change?.creditsFromRenewal ?? targetPrice.monthlyCredits;
|
|
602
|
+
const creditsNow = change?.creditsNow ?? null;
|
|
603
|
+
const days = daysLeftText(change?.secondsLeft ?? null);
|
|
604
|
+
const undoes = next && next !== current ? ` This also undoes the move to ${planName(next)} set for the next renewal.` : '';
|
|
605
|
+
const money = exact !== null
|
|
606
|
+
? `Move to ${planName(target)}: ${formatUsd(exact)} now from ${wallet} for the rest of this period`
|
|
607
|
+
+ `${exact > 0 && underOneDayLeft(change?.secondsLeft ?? null) ? ', at the one-day minimum' : days ? ` (${days})` : ''}. `
|
|
608
|
+
: `Move to ${planName(target)}: the difference for the rest of this period is taken from ${wallet} now. Wiro works it out from the time left when the plan changes, and it is never more than ${formatUsd(fullDifference)}, so that is the amount to approve. `;
|
|
609
|
+
const credits = creditsNow !== null
|
|
610
|
+
? upgradeCreditsText(creditsNow, cancelled ? '' : `, and every renewal gives ${formatCredits(renewalCredits)}`, false)
|
|
611
|
+
: `The added credits come for the time left in this period${cancelled ? '' : `, and every renewal gives ${formatCredits(renewalCredits)}`}. `;
|
|
612
|
+
return {
|
|
613
|
+
outcome: 'quote',
|
|
614
|
+
quote: {
|
|
615
|
+
chargeNowUsd: exact,
|
|
616
|
+
estimated: exact === null,
|
|
617
|
+
maxChargeUsd: exact ?? fullDifference,
|
|
618
|
+
recurringUsd: cancelled ? null : targetPrice.priceUsd,
|
|
619
|
+
monthlyCredits: renewalCredits,
|
|
620
|
+
...(creditsNow !== null ? { creditsNow } : {}),
|
|
621
|
+
paidFrom,
|
|
622
|
+
plan: target,
|
|
623
|
+
change: 'upgrade',
|
|
624
|
+
},
|
|
625
|
+
message: money + credits
|
|
626
|
+
+ (cancelled ? `${ending} ` : `From the next renewal the plan costs ${formatUsd(targetPrice.priceUsd)} every 30 days. `)
|
|
627
|
+
+ `${planChangeAgentNote(agent.status, 'the plan change', true)}${undoes} A move to a smaller plan later starts at the renewal after it is asked for, with nothing refunded.`,
|
|
628
|
+
perform: async (signal) => {
|
|
629
|
+
const body = requireApiSuccess(await client.post('/UserAgent/UpgradeTier', { guid, targetTier: target }, signal), 'The plan could not be changed.');
|
|
630
|
+
if (asString(body.url)) {
|
|
631
|
+
// A checkout link means the plan is paid by card, and card payments are made in the dashboard.
|
|
632
|
+
return { outcome: 'refused', message: cardPlan(`move it to ${planName(target)}`) };
|
|
633
|
+
}
|
|
634
|
+
const charged = toNumber(body.proratedCharge) ?? toNumber(body.chargedUsd);
|
|
635
|
+
const newPrice = toNumber(body.newPriceUsd) ?? targetPrice.priceUsd;
|
|
636
|
+
const fromRenewal = toNumber(body.creditsFromRenewal) ?? toNumber(body.newMonthlyCredits) ?? renewalCredits;
|
|
637
|
+
const came = toNumber(body.creditsNow);
|
|
638
|
+
const previous = asString(body.previousTier);
|
|
639
|
+
return {
|
|
640
|
+
outcome: 'done',
|
|
641
|
+
message: `Moved to ${planName(target)} and charged ${formatUsd(charged ?? 0)} from ${wallet}. `
|
|
642
|
+
+ (came !== null
|
|
643
|
+
? upgradeCreditsText(came, cancelled ? '' : `, and every renewal gives ${formatCredits(fromRenewal)}`, true)
|
|
644
|
+
: '')
|
|
645
|
+
+ (cancelled
|
|
646
|
+
? `The plan is still cancelled and ends about a day after ${periodEnd}; action renew undoes the cancellation. `
|
|
647
|
+
: `The plan now costs ${formatUsd(newPrice)} every 30 days. `)
|
|
648
|
+
+ planChangeAgentNote(agent.status, 'the plan change'),
|
|
649
|
+
result: {
|
|
650
|
+
chargedUsd: charged,
|
|
651
|
+
monthlyPriceUsd: newPrice,
|
|
652
|
+
monthlyCredits: fromRenewal,
|
|
653
|
+
...(came !== null ? { creditsNow: came } : {}),
|
|
654
|
+
plan: target,
|
|
655
|
+
...(previous ? { previousPlan: previous } : {}),
|
|
656
|
+
change: 'upgrade',
|
|
657
|
+
},
|
|
658
|
+
nextAction: waitIfStarting(agent),
|
|
659
|
+
};
|
|
660
|
+
},
|
|
661
|
+
verify: after => (after.tier !== target ? `get_my_agent does not show ${planName(target)} yet; check it again in a minute before doing anything else.` : null),
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
// A smaller plan starts with the next renewal: nothing moves now, and nothing is refunded.
|
|
665
|
+
if (cancelled || agent.subscription?.atPeriodEnd === 'ends') {
|
|
666
|
+
return {
|
|
667
|
+
outcome: 'refused',
|
|
668
|
+
message: `The plan is set to end after ${periodEnd}, so it does not renew on another plan. To keep the agent on ${planName(target)}, `
|
|
669
|
+
+ (route === 'card' && !cancelled
|
|
670
|
+
? `keep the plan in the dashboard first (${agent.dashboardUrl}), then change the plan.`
|
|
671
|
+
: 'undo the cancellation first with action renew, then change the plan.'),
|
|
672
|
+
...(cancelled ? { nextAction: billingCall(guid, 'renew') } : {}),
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
if (next === target) {
|
|
676
|
+
return {
|
|
677
|
+
outcome: 'not_needed',
|
|
678
|
+
message: `The move to ${planName(target)} is already set: it starts with ${when}. Until then the agent keeps ${planName(current)} and its credits.`,
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
if (teamCard)
|
|
682
|
+
return { outcome: 'refused', message: cardPlan(`move it to ${planName(target)}`) };
|
|
683
|
+
const smaller = agent.pricing?.[target];
|
|
684
|
+
const card = route === 'card';
|
|
685
|
+
const startsAt = agent.subscription?.periodEndsAt;
|
|
437
686
|
return {
|
|
438
687
|
outcome: 'quote',
|
|
439
688
|
quote: {
|
|
440
|
-
chargeNowUsd:
|
|
441
|
-
estimated:
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
689
|
+
chargeNowUsd: 0,
|
|
690
|
+
estimated: false,
|
|
691
|
+
recurringUsd: smaller?.priceUsd ?? null,
|
|
692
|
+
monthlyCredits: smaller?.monthlyCredits ?? null,
|
|
693
|
+
paidFrom: card ? 'card' : paidFrom,
|
|
694
|
+
plan: target,
|
|
695
|
+
change: 'downgrade',
|
|
696
|
+
...(startsAt ? { startsAt } : {}),
|
|
448
697
|
},
|
|
449
|
-
message:
|
|
450
|
-
+ (
|
|
451
|
-
+
|
|
698
|
+
message: `Move to ${planName(target)} at the next renewal: nothing is charged or refunded now. The agent keeps ${planName(current)} and its credits until ${periodEnd}. `
|
|
699
|
+
+ `From ${when} the plan is ${planName(target)}${smaller ? `, ${planTerms(smaller)}` : ''}, ${card ? 'paid by card' : `paid from ${wallet}`}. `
|
|
700
|
+
+ `Asking for ${planName(current)} again before then undoes the move.`,
|
|
701
|
+
confirmReason: 'Ask the user to confirm first: from the next renewal the agent has the smaller plan\'s credits.',
|
|
452
702
|
perform: async (signal) => {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
const
|
|
459
|
-
const newPrice = toNumber(body.newPriceUsd) ?? pro.priceUsd;
|
|
460
|
-
const fromRenewal = toNumber(body.creditsFromRenewal) ?? toNumber(body.newMonthlyCredits) ?? renewalCredits;
|
|
461
|
-
const came = toNumber(body.creditsNow);
|
|
462
|
-
const previousTier = asString(body.previousTier);
|
|
703
|
+
// Wiro moves an agent to a smaller plan only when the request says so: a bare smaller plan is
|
|
704
|
+
// refused, so an older client that asks for one as an upgrade changes nothing.
|
|
705
|
+
const body = requireApiSuccess(await client.post('/UserAgent/UpgradeTier', { guid, targetTier: target, allowDowngrade: true }, signal), 'The move to the smaller plan could not be set.');
|
|
706
|
+
if (asString(body.url))
|
|
707
|
+
return { outcome: 'refused', message: cardPlan(`move it to ${planName(target)}`) };
|
|
708
|
+
const at = unixToIso(body.startsAt ?? body.effectiveAt ?? body.periodEnd) ?? startsAt;
|
|
463
709
|
return {
|
|
464
710
|
outcome: 'done',
|
|
465
|
-
message: `
|
|
466
|
-
|
|
467
|
-
? upgradeCreditsText(came, cancelled ? '' : `, and every renewal gives ${formatCredits(fromRenewal)}`, true)
|
|
468
|
-
: '')
|
|
469
|
-
+ (cancelled
|
|
470
|
-
? `The plan is still cancelled and ends about a day after ${periodEnd}; action renew undoes the cancellation. `
|
|
471
|
-
: `The plan now costs ${formatUsd(newPrice)} every 30 days. `)
|
|
472
|
-
+ planChangeAgentNote(agent.status, 'the upgrade'),
|
|
473
|
-
result: {
|
|
474
|
-
chargedUsd: charged,
|
|
475
|
-
monthlyPriceUsd: newPrice,
|
|
476
|
-
monthlyCredits: fromRenewal,
|
|
477
|
-
...(came !== null ? { creditsNow: came } : {}),
|
|
478
|
-
tier: 'pro',
|
|
479
|
-
...(previousTier ? { previousTier } : {}),
|
|
480
|
-
},
|
|
481
|
-
nextAction: waitIfStarting(agent),
|
|
711
|
+
message: `The move to ${planName(target)} is set: it starts with the renewal after ${at ?? 'this period'}. Until then the agent keeps ${planName(current)} and its credits. Nothing was charged or refunded.`,
|
|
712
|
+
result: { chargedUsd: 0, plan: current, nextPlan: target, change: 'downgrade', ...(at ? { startsAt: at } : {}) },
|
|
482
713
|
};
|
|
483
714
|
},
|
|
715
|
+
verify: after => (after.nextPlan !== target ? `get_my_agent does not show the move to ${planName(target)} yet; check it again in a minute.` : null),
|
|
484
716
|
};
|
|
485
717
|
}
|
|
486
718
|
case 'buy_credits': {
|
|
487
719
|
if (!active) {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
if (agent.tier !== 'pro') {
|
|
491
|
-
return {
|
|
492
|
-
outcome: 'refused',
|
|
493
|
-
message: 'Extra credit packs are only available on Pro. Upgrade first with action upgrade_to_pro.',
|
|
494
|
-
nextAction: billingCall(guid, 'upgrade_to_pro'),
|
|
495
|
-
};
|
|
720
|
+
const open = unsettled('Extra credits need an active plan.');
|
|
721
|
+
return { outcome: 'refused', message: open ?? 'Extra credits need an active plan. Use action subscribe or renew first.' };
|
|
496
722
|
}
|
|
497
723
|
if (route === 'card')
|
|
498
724
|
return { outcome: 'refused', message: cardPlan('buy the pack') };
|
|
@@ -542,7 +768,7 @@ function confirmArguments(guid, action, quote, requestId) {
|
|
|
542
768
|
return {
|
|
543
769
|
agent_guid: guid,
|
|
544
770
|
action,
|
|
545
|
-
...(action === 'subscribe' && quote?.
|
|
771
|
+
...((action === 'subscribe' || action === 'change_plan') && quote?.plan ? { plan: quote.plan } : {}),
|
|
546
772
|
...(quote?.pack ? { pack: quote.pack } : {}),
|
|
547
773
|
...(requestId ? { request_id: requestId } : {}),
|
|
548
774
|
...(amount !== null ? { confirm_charge_usd: amount } : {}),
|
|
@@ -595,7 +821,7 @@ async function observeOpenOutcome(client, action, before, quote, requestId, sent
|
|
|
595
821
|
happened: true,
|
|
596
822
|
row,
|
|
597
823
|
text: 'The agent now has an active plan, so the payment went through. Do not subscribe again.',
|
|
598
|
-
result: { chargedUsd: charged, ...(quote?.
|
|
824
|
+
result: { chargedUsd: charged, ...(quote?.plan ? { plan: quote.plan } : {}) },
|
|
599
825
|
nextAction: subscribeWait(before),
|
|
600
826
|
}
|
|
601
827
|
: { happened: false, text: 'The agent shows no active plan yet. Check get_my_agent again in a minute and the wallet balance with get_account before trying again; if the plan is still not active but the balance changed, contact Wiro support.' };
|
|
@@ -609,24 +835,28 @@ async function observeOpenOutcome(client, action, before, quote, requestId, sent
|
|
|
609
835
|
nextAction: renewWait(before),
|
|
610
836
|
}
|
|
611
837
|
: { happened: false, text: 'The plan still shows as expired. Check get_my_agent again in a minute and the wallet balance with get_account before renewing again; if the plan is still expired but the balance changed, contact Wiro support.' };
|
|
612
|
-
case '
|
|
613
|
-
|
|
838
|
+
case 'change_plan': {
|
|
839
|
+
// Only a move to a larger plan charges, so it is the only one checked here.
|
|
840
|
+
const target = quote?.plan;
|
|
841
|
+
if (!target || quote?.change !== 'upgrade')
|
|
842
|
+
return { happened: false, text: '' };
|
|
843
|
+
if (after.tier === target) {
|
|
614
844
|
return {
|
|
615
845
|
happened: true,
|
|
616
846
|
row,
|
|
617
|
-
text: `The agent is on
|
|
618
|
-
result: { chargedUsd: null,
|
|
847
|
+
text: `The agent is on ${planName(target)} now, so the plan change went through. Do not change it again. The prorated charge is not shown, and it is at most ${formatUsd(quote.maxChargeUsd)}.`,
|
|
848
|
+
result: { chargedUsd: null, plan: target, change: 'upgrade' },
|
|
619
849
|
nextAction: waitIfStarting(before),
|
|
620
850
|
};
|
|
621
851
|
}
|
|
622
|
-
const
|
|
623
|
-
const priced =
|
|
624
|
-
.some(value => value !== null && sameAmount(value,
|
|
852
|
+
const targetPrice = before.pricing?.[target]?.priceUsd;
|
|
853
|
+
const priced = targetPrice !== undefined && [after.monthlyPriceUsd, after.subscription?.amountUsd ?? null]
|
|
854
|
+
.some(value => value !== null && sameAmount(value, targetPrice));
|
|
625
855
|
return {
|
|
626
856
|
happened: false,
|
|
627
857
|
text: priced
|
|
628
|
-
?
|
|
629
|
-
:
|
|
858
|
+
? `The agent does not show ${planName(target)} yet. Do not change the plan again: check get_my_agent and the wallet balance with get_account, and if the balance changed, contact Wiro support.`
|
|
859
|
+
: `The agent still shows ${planName(after.tier) || 'its earlier plan'}. Check get_my_agent and the wallet balance with get_account before trying again; if the balance changed, contact Wiro support.`,
|
|
630
860
|
};
|
|
631
861
|
}
|
|
632
862
|
case 'buy_credits': {
|
|
@@ -664,6 +894,12 @@ async function observeOpenOutcome(client, action, before, quote, requestId, sent
|
|
|
664
894
|
return { happened: false, text: '' };
|
|
665
895
|
}
|
|
666
896
|
}
|
|
897
|
+
// Wiro renews an ended plan at no more than the price the agent shows. When the renewal takes more (its plan
|
|
898
|
+
// moved since), Wiro charges nothing, shows the renewal's plan and price on the agent and asks for the renewal
|
|
899
|
+
// again. The first group is Wiro's account of it, without its closing request to renew again.
|
|
900
|
+
const RENEWAL_REPRICED = /^(This plan now renews on the .+? plan at \$[\d.,]+ a month, not the \$[\d.,]+ it showed\. Nothing was charged\.)/i;
|
|
901
|
+
// Wiro's answer to a smaller plan sent as a move to a larger one, which changes nothing.
|
|
902
|
+
const SMALLER_PLAN_REFUSAL = /^The \w+ plan is smaller than this agent's \w+ plan, so this is not an upgrade\. Nothing was changed\./i;
|
|
667
903
|
// Code 104: another payment from the same wallet, or another change to the agent's plan, was going through.
|
|
668
904
|
const BUSY_NOTE = 'Nothing was charged or changed, and it is not a low balance. Offer to try again in a minute.';
|
|
669
905
|
function annotateKnownRefusal(error, requestId) {
|
|
@@ -673,6 +909,10 @@ function annotateKnownRefusal(error, requestId) {
|
|
|
673
909
|
}
|
|
674
910
|
if (isWalletNotRead(error))
|
|
675
911
|
return new Error(`${text}\n\n${WALLET_NOT_READ_NOTE}`);
|
|
912
|
+
if (SMALLER_PLAN_REFUSAL.test(text)) {
|
|
913
|
+
// A move this server sent as one to a larger plan: the agent's plan moved up after it was read.
|
|
914
|
+
return new Error(`${text}\n\nNothing was charged: the agent's plan changed after it was read. get_my_agent shows the plan it is on now, and change_plan quotes a move from it.`);
|
|
915
|
+
}
|
|
676
916
|
if (/insufficient wallet balance/i.test(text)) {
|
|
677
917
|
return new Error(`${text}\n\nNothing was charged. Funds are added at https://wiro.ai/panel/wallet/deposit/credit-card (for a team project, select that team in the dashboard first); once the user has added them and says yes, try again. get_account shows the balance.`);
|
|
678
918
|
}
|
|
@@ -687,41 +927,48 @@ function annotateKnownRefusal(error, requestId) {
|
|
|
687
927
|
export function registerManageAgentBilling(server, client) {
|
|
688
928
|
server.registerTool('manage_agent_billing', {
|
|
689
929
|
title: 'Manage a Wiro agent\'s plan and credits',
|
|
690
|
-
description: 'Pay for, renew,
|
|
691
|
-
+ '
|
|
692
|
-
+ '
|
|
693
|
-
+ '
|
|
694
|
-
+ '
|
|
695
|
-
+ '
|
|
696
|
-
+ '
|
|
697
|
-
+ '
|
|
698
|
-
+ '
|
|
930
|
+
description: 'Pay for, renew, change or cancel a deployed agent\'s plan, or buy extra credits. An agent has '
|
|
931
|
+
+ 'one of three plans, smallest first: Starter, Pro or Max. Each has a fixed monthly price and monthly credits, '
|
|
932
|
+
+ 'and every plan includes every skill. Charges come from the Wiro wallet of the agent\'s workspace (the team '
|
|
933
|
+
+ 'wallet for a team agent); the connected project must belong to that same workspace. Every call without '
|
|
934
|
+
+ 'confirm: true only returns what will happen and the amount (for a move to a larger plan, the prorated '
|
|
935
|
+
+ 'amount Wiro works out, or the most it can charge when that is worked out only at the change). Show it to '
|
|
936
|
+
+ 'the user, get their approval, then repeat the call with confirm: true and, for a charge, confirm_charge_usd '
|
|
937
|
+
+ 'set to the quoted amount (for a move to a larger plan, quote.maxChargeUsd); if the price changed in '
|
|
938
|
+
+ 'between, nothing is charged and a new quote comes back. Never repeat a confirmed charge that failed or '
|
|
939
|
+
+ 'timed out without checking get_my_agent first. '
|
|
699
940
|
+ 'subscribe starts a plan for an agent without one; renew undoes a cancellation or renews an expired '
|
|
700
|
-
+ 'wallet plan;
|
|
701
|
-
+ '
|
|
702
|
-
+ '
|
|
703
|
-
+ '
|
|
704
|
-
+ '
|
|
705
|
-
+ '
|
|
706
|
-
+ '
|
|
707
|
-
+ '
|
|
708
|
-
+ '
|
|
941
|
+
+ 'wallet plan; change_plan moves to another plan: a larger plan starts now and charges the prorated '
|
|
942
|
+
+ 'difference for the rest of the period, a smaller plan starts with the next renewal and charges or refunds '
|
|
943
|
+
+ 'nothing, and asking for the plan the agent is on undoes a move set for the next renewal; buy_credits adds '
|
|
944
|
+
+ 'a credit pack, on any plan; cancel stops renewals with no refund. Plans renew every 30 days until '
|
|
945
|
+
+ 'cancelled. Renewals, plan changes and packs leave a running agent running. An agent whose plan is billed '
|
|
946
|
+
+ 'by card moves to a larger plan and is topped up in the Wiro dashboard, and a personal card plan is '
|
|
947
|
+
+ 'cancelled there; the tool returns the link and charges nothing. A personal card plan can move to a smaller '
|
|
948
|
+
+ 'plan here: the card pays the smaller plan from the next renewal; a team\'s card plan changes plans in the '
|
|
949
|
+
+ 'dashboard. A card plan that is moving to wallet billing '
|
|
950
|
+
+ 'pays change_plan and buy_credits from the wallet, and cancel works once the wallet bills it. A card plan '
|
|
951
|
+
+ 'whose last payment did not go through has not ended and takes no new plan: the payment is fixed from '
|
|
952
|
+
+ 'Billing in the dashboard, in the personal workspace of the person whose card pays for the plan, and the '
|
|
953
|
+
+ 'plan then renews by itself. On a team '
|
|
954
|
+
+ 'agent only a team admin, the organization\'s owner or the person who deployed the agent can pay for it or '
|
|
955
|
+
+ 'change its plan; other members are refused and nothing is charged.',
|
|
709
956
|
inputSchema: strictInput({
|
|
710
957
|
agent_guid: z.string().min(1).max(100)
|
|
711
958
|
.describe('The deployed agent\'s guid, from list_my_agents or deploy_agent.'),
|
|
712
959
|
action: z.enum(ACTIONS)
|
|
713
|
-
.describe('subscribe,
|
|
714
|
-
|
|
715
|
-
.describe('subscribe
|
|
960
|
+
.describe('subscribe, change_plan, cancel, renew or buy_credits.'),
|
|
961
|
+
plan: z.enum(AGENT_PLANS).optional()
|
|
962
|
+
.describe('subscribe: the plan to pay for (default: the agent\'s current plan, else starter). change_plan: the plan to move to; leave it out to get the plans with their prices.'),
|
|
716
963
|
pack: z.enum(['small', 'medium', 'large']).optional()
|
|
717
964
|
.describe('buy_credits only: the credit pack to buy. Leave it out to get the packs with their credits and prices.'),
|
|
718
965
|
request_id: z.string().regex(/^[A-Za-z0-9_-]{8,128}$/).optional()
|
|
719
966
|
.describe('buy_credits only: the id returned by the quote. Repeating a purchase with the same id never charges twice. Every new purchase needs a new quote; an id whose purchase already completed buys nothing.'),
|
|
720
967
|
confirm_charge_usd: z.number().min(0).max(100_000).optional()
|
|
721
|
-
.describe('With confirm: true, the amount the user approved, from the quote\'s nextAction (quote.chargeNowUsd, or quote.maxChargeUsd for
|
|
968
|
+
.describe('With confirm: true, the amount the user approved, from the quote\'s nextAction (quote.chargeNowUsd, or quote.maxChargeUsd for a move to a larger plan). Required for every action that charges: subscribe, change_plan to a larger plan, buy_credits and renewing an expired plan. If a fresh quote no longer matches it, nothing is charged and the new quote is returned. For a move to a larger plan it is the most the user agreed to pay now.'),
|
|
722
969
|
confirm: z.boolean().default(false)
|
|
723
970
|
.describe('false returns only the quote. true performs the action. Set it only after the user approved it.'),
|
|
724
|
-
}),
|
|
971
|
+
}, 'The plan to pay for or move to is the argument plan: starter, pro or max.'),
|
|
725
972
|
outputSchema: {
|
|
726
973
|
action: z.enum(ACTIONS),
|
|
727
974
|
agentGuid: z.string(),
|
|
@@ -741,7 +988,7 @@ export function registerManageAgentBilling(server, client) {
|
|
|
741
988
|
idempotentHint: false,
|
|
742
989
|
openWorldHint: true,
|
|
743
990
|
},
|
|
744
|
-
}, async ({ agent_guid, action,
|
|
991
|
+
}, async ({ agent_guid, action, plan: planInput, pack, request_id, confirm_charge_usd, confirm }, extra) => {
|
|
745
992
|
const guid = agent_guid.trim();
|
|
746
993
|
const requestId = action === 'buy_credits' ? request_id ?? newRequestId() : undefined;
|
|
747
994
|
let sentAtMs = 0;
|
|
@@ -785,13 +1032,65 @@ export function registerManageAgentBilling(server, client) {
|
|
|
785
1032
|
},
|
|
786
1033
|
};
|
|
787
1034
|
};
|
|
1035
|
+
/** A quote with the call that confirms it. lead goes before the quote's own text. */
|
|
1036
|
+
const quoteResponse = (quoted, lead, row, priceChanged) => {
|
|
1037
|
+
const amount = approvalAmountOf(action, quoted.quote);
|
|
1038
|
+
const additions = [
|
|
1039
|
+
...(requestId ? [`request_id "${requestId}"`] : []),
|
|
1040
|
+
...(amount !== null ? [`confirm_charge_usd ${amount}`] : []),
|
|
1041
|
+
'confirm: true',
|
|
1042
|
+
];
|
|
1043
|
+
return respond({
|
|
1044
|
+
outcome: 'quote',
|
|
1045
|
+
message: `${lead}${quoted.message}\n\nNothing has been done yet. To go ahead, call manage_agent_billing again with the same arguments plus ${additions.join(', ')}.`,
|
|
1046
|
+
quote: quoted.quote,
|
|
1047
|
+
...(priceChanged ? { priceChanged: true } : {}),
|
|
1048
|
+
nextAction: {
|
|
1049
|
+
tool: 'manage_agent_billing',
|
|
1050
|
+
arguments: confirmArguments(guid, action, quoted.quote, requestId),
|
|
1051
|
+
reason: quoted.confirmReason ?? CHARGE_REASON,
|
|
1052
|
+
},
|
|
1053
|
+
agentRow: row,
|
|
1054
|
+
withRequestId: true,
|
|
1055
|
+
});
|
|
1056
|
+
};
|
|
1057
|
+
/**
|
|
1058
|
+
* After Wiro refused a renewal that would charge more than the agent showed: it charged nothing and
|
|
1059
|
+
* wrote the plan and the price the renewal takes on the agent. The agent is read again and quoted
|
|
1060
|
+
* again, and the user approves the new quote; nothing is renewed on their behalf.
|
|
1061
|
+
*/
|
|
1062
|
+
const quoteRenewalAgain = async (refusal) => {
|
|
1063
|
+
try {
|
|
1064
|
+
const row = await readAgentDetail(client, guid, extra.signal);
|
|
1065
|
+
const agent = toAgentDetail(row);
|
|
1066
|
+
const raw = isPlainObject(row.subscription) ? row.subscription : null;
|
|
1067
|
+
const again = await planFor(client, action, agent, raw, { plan: planInput, pack, requestId: requestId ?? '' }, extra.signal);
|
|
1068
|
+
if (again.outcome !== 'quote' || !again.perform) {
|
|
1069
|
+
return respond({
|
|
1070
|
+
outcome: again.outcome,
|
|
1071
|
+
heading: again.heading,
|
|
1072
|
+
message: `${refusal} The agent was read again.\n\n${again.message}`,
|
|
1073
|
+
nextAction: again.nextAction,
|
|
1074
|
+
agentRow: row,
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
const amount = approvalAmountOf(action, again.quote);
|
|
1078
|
+
const changed = amount !== null && (confirm_charge_usd === undefined || !sameAmount(amount, confirm_charge_usd));
|
|
1079
|
+
return quoteResponse(again, `${refusal} The agent was read again for this new quote. Show it to the user and ask again; renew only after they agree to it.\n\n`, row, changed);
|
|
1080
|
+
}
|
|
1081
|
+
catch (readError) {
|
|
1082
|
+
if (extra.signal.aborted)
|
|
1083
|
+
return errorResult(readError);
|
|
1084
|
+
return errorResult(new Error(`${refusal}\n\nThe agent could not be read again just now. Call manage_agent_billing with action renew and without confirm for the new quote, and show it to the user before renewing.`));
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
788
1087
|
try {
|
|
789
1088
|
const row = await readAgentDetail(client, guid, extra.signal);
|
|
790
1089
|
const agent = toAgentDetail(row);
|
|
791
1090
|
before = agent;
|
|
792
1091
|
plainMember = await isPlainTeamMember(client, row, extra.signal);
|
|
793
1092
|
const raw = isPlainObject(row.subscription) ? row.subscription : null;
|
|
794
|
-
plan = await planFor(client, action, agent, raw, {
|
|
1093
|
+
plan = await planFor(client, action, agent, raw, { plan: planInput, pack, requestId: requestId ?? '' }, extra.signal);
|
|
795
1094
|
if (plan.outcome !== 'quote' || !plan.perform) {
|
|
796
1095
|
return respond({ outcome: plan.outcome, heading: plan.heading, message: plan.message, nextAction: plan.nextAction, agentRow: row });
|
|
797
1096
|
}
|
|
@@ -810,28 +1109,11 @@ export function registerManageAgentBilling(server, client) {
|
|
|
810
1109
|
if (!confirm || problem) {
|
|
811
1110
|
const amount = approvalAmountOf(action, plan.quote);
|
|
812
1111
|
const lead = problem === 'changed'
|
|
813
|
-
? `${changedLead(action, amount, confirm_charge_usd, request_id)}\n\n`
|
|
1112
|
+
? `${changedLead(action, plan.quote, amount, confirm_charge_usd, request_id)}\n\n`
|
|
814
1113
|
: problem === 'missing'
|
|
815
1114
|
? 'Nothing was charged: confirm_charge_usd is missing. It is the amount the user approved, and it must be sent with confirm: true.\n\n'
|
|
816
1115
|
: '';
|
|
817
|
-
|
|
818
|
-
...(requestId ? [`request_id "${requestId}"`] : []),
|
|
819
|
-
...(amount !== null ? [`confirm_charge_usd ${amount}`] : []),
|
|
820
|
-
'confirm: true',
|
|
821
|
-
];
|
|
822
|
-
return respond({
|
|
823
|
-
outcome: 'quote',
|
|
824
|
-
message: `${lead}${plan.message}\n\nNothing has been done yet. To go ahead, call manage_agent_billing again with the same arguments plus ${additions.join(', ')}.`,
|
|
825
|
-
quote: plan.quote,
|
|
826
|
-
...(problem === 'changed' ? { priceChanged: true } : {}),
|
|
827
|
-
nextAction: {
|
|
828
|
-
tool: 'manage_agent_billing',
|
|
829
|
-
arguments: confirmArguments(guid, action, plan.quote, requestId),
|
|
830
|
-
reason: plan.confirmReason ?? CHARGE_REASON,
|
|
831
|
-
},
|
|
832
|
-
agentRow: row,
|
|
833
|
-
withRequestId: true,
|
|
834
|
-
});
|
|
1116
|
+
return quoteResponse(plan, lead, row, problem === 'changed');
|
|
835
1117
|
}
|
|
836
1118
|
sentAtMs = Date.now();
|
|
837
1119
|
const performed = await plan.perform(extra.signal);
|
|
@@ -889,11 +1171,13 @@ export function registerManageAgentBilling(server, client) {
|
|
|
889
1171
|
}
|
|
890
1172
|
// Without the read-back, the row shows the agent as it was before the change.
|
|
891
1173
|
const stale = !afterRead && performed.outcome === 'done';
|
|
1174
|
+
// A change the agent read again does not show yet is said, so the result never claims more than it shows.
|
|
1175
|
+
const notShown = afterRead && performed.outcome === 'done' && plan.verify ? plan.verify(toAgentSummary(after)) : null;
|
|
892
1176
|
return respond({
|
|
893
1177
|
outcome: performed.outcome,
|
|
894
1178
|
message: stale
|
|
895
1179
|
? `${performed.message} The agent could not be read afterwards; get_my_agent shows its new status, plan and credits.`
|
|
896
|
-
: performed.message,
|
|
1180
|
+
: notShown ? `${performed.message} ${notShown}` : performed.message,
|
|
897
1181
|
quote: plan.quote,
|
|
898
1182
|
result: performed.result,
|
|
899
1183
|
nextAction: performed.nextAction,
|
|
@@ -905,6 +1189,11 @@ export function registerManageAgentBilling(server, client) {
|
|
|
905
1189
|
if (error instanceof ApiResultError && requestId && /still pending\. Retry with the same request id/i.test(error.message)) {
|
|
906
1190
|
return errorResult(new Error(`${error.message}\n\n${retryPurchaseText(guid, plan?.quote, requestId)}`));
|
|
907
1191
|
}
|
|
1192
|
+
const repriced = action === 'renew' && sentAtMs > 0 && error instanceof ApiResultError
|
|
1193
|
+
? RENEWAL_REPRICED.exec(error.message.trim())
|
|
1194
|
+
: null;
|
|
1195
|
+
if (repriced)
|
|
1196
|
+
return quoteRenewalAgain(repriced[1]);
|
|
908
1197
|
if (sentAtMs > 0 && !extra.signal.aborted && outcomeUnknown(error)) {
|
|
909
1198
|
const text = errorText(error);
|
|
910
1199
|
const quote = plan?.quote;
|