@phantomcodes/cli 1.4.0 → 1.4.2
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 +13 -16
- package/dist/agents/command.d.mts +36 -0
- package/dist/agents/command.mjs +19 -7
- package/dist/agents/proxy.d.mts +44 -0
- package/dist/agents/proxy.mjs +4 -2
- package/dist/cli/app.d.mts +25 -0
- package/dist/cli/app.mjs +0 -2
- package/dist/cli/commands.d.mts +19 -0
- package/dist/cli/commands.mjs +1 -11
- package/dist/cli/help.d.mts +26 -0
- package/dist/cli/help.mjs +1 -1
- package/dist/cli/run.d.mts +2 -0
- package/dist/cli/screens.d.mts +171 -0
- package/dist/cli/screens.mjs +0 -31
- package/dist/cli/start.d.mts +34 -0
- package/dist/cli/start.mjs +0 -1
- package/dist/core/brand.d.mts +12 -0
- package/dist/core/errors.d.mts +27 -0
- package/dist/core/free.d.mts +13 -0
- package/dist/core/helpers.d.mts +75 -0
- package/dist/core/helpers.mjs +28 -3
- package/dist/core/motion.d.mts +47 -0
- package/dist/core/node-trust.d.mts +12 -0
- package/dist/core/pick.d.mts +86 -0
- package/dist/core/request.d.mts +64 -0
- package/dist/core/request.mjs +79 -30
- package/dist/core/secrets.d.mts +33 -0
- package/dist/core/shapes.d.mts +13 -0
- package/dist/core/state.d.mts +3 -0
- package/dist/core/style.d.mts +49 -0
- package/dist/core/version.d.mts +2 -0
- package/dist/core/version.mjs +1 -1
- package/dist/keys/command.d.mts +16 -0
- package/dist/keys/saved.d.mts +88 -0
- package/dist/models/choice.d.mts +7 -0
- package/dist/models/choice.mjs +1 -1
- package/dist/models/command.d.mts +8 -0
- package/dist/models/list.d.mts +39 -0
- package/dist/phantom.d.mts +45 -0
- package/dist/phantom.mjs +0 -1
- package/dist/receipts/command.d.mts +7 -0
- package/dist/receipts/receipts.d.mts +67 -0
- package/dist/receipts/receipts.mjs +15 -11
- package/dist/setup/agents.d.mts +149 -0
- package/dist/setup/agents.mjs +48 -35
- package/dist/setup/apply.d.mts +71 -0
- package/dist/setup/apply.mjs +7 -2
- package/dist/setup/command.d.mts +12 -0
- package/dist/setup/editors.d.mts +101 -0
- package/dist/setup/editors.mjs +4 -2
- package/dist/setup/legacy.d.mts +13 -0
- package/dist/setup/plan.d.mts +104 -0
- package/dist/setup/plan.mjs +5 -3
- package/dist/setup/screen.d.mts +30 -0
- package/dist/setup/shared.d.mts +141 -0
- package/dist/setup/shared.mjs +8 -2
- package/dist/setup/was.d.mts +42 -0
- package/dist/setup/was.mjs +91 -0
- package/dist/spend/command.d.mts +5 -0
- package/dist/spend/lineup.d.mts +54 -0
- package/dist/spend/lineup.mjs +3 -2
- package/dist/spend/report.d.mts +105 -0
- package/dist/spend/report.mjs +0 -2
- package/dist/spend/routing.d.mts +72 -0
- package/dist/workspace/command.d.mts +46 -0
- package/dist/workspace/command.mjs +83 -37
- package/package.json +2 -2
- package/skills/phantom-ai/SKILL.md +1 -15
- package/dist/plan/command.mjs +0 -91
|
@@ -2,12 +2,12 @@ import { createHash } from 'node:crypto';
|
|
|
2
2
|
import { hostname } from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
5
|
-
import { DEFAULT_BASE_URL,
|
|
5
|
+
import { DEFAULT_BASE_URL, REQUEST_TIMEOUT_MS, answerError, fetchAnswer, httpsOnly } from "../core/request.mjs";
|
|
6
6
|
import { stateDir } from "../core/state.mjs";
|
|
7
7
|
import { writeSecret, readSecret, removeSecret, keychainOn } from "../core/secrets.mjs";
|
|
8
8
|
import { keyCommand, loginCommand } from "../keys/command.mjs";
|
|
9
9
|
import { die } from "../core/errors.mjs";
|
|
10
|
-
import {
|
|
10
|
+
import { limitFlag, openUrl, pickIo, showsScreen } from "../core/helpers.mjs";
|
|
11
11
|
import { BackToMenu, Cancelled, askText, cancelKeys, choose, keyboard } from "../core/pick.mjs";
|
|
12
12
|
import { spawnSync } from 'node:child_process';
|
|
13
13
|
import { onPath } from "../setup/shared.mjs";
|
|
@@ -16,8 +16,13 @@ import { alreadySignedInScreen, fundScreen, keyChangeScreen, keysScreen, payment
|
|
|
16
16
|
import { keyId, keyPath, listNamedKeys, namedKeyPath, readNamedKey, resolveApiKey, saveApiKey, saveNamedKey } from "../keys/saved.mjs";
|
|
17
17
|
const credentialPath = (env) => path.join(stateDir(env), 'workspace');
|
|
18
18
|
const base = (input) => httpsOnly(input.baseUrl || input.env.PHANTOM_BASE_URL || DEFAULT_BASE_URL).replace(/\/$/, '');
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* One call to the workspace API, with the limits every call shares ({@link fetchAnswer}): 30 seconds
|
|
21
|
+
* unless `timeoutMs` says less, and a plain message for a web page, an empty answer or no connection.
|
|
22
|
+
* @throws {@link PhantomApiError} when the API answers with an error status.
|
|
23
|
+
*/
|
|
24
|
+
export async function wire(url, method, endpoint, body, token, timeoutMs) {
|
|
25
|
+
const { res, answer } = await fetchAnswer(url + endpoint, {
|
|
21
26
|
method,
|
|
22
27
|
redirect: 'error',
|
|
23
28
|
headers: {
|
|
@@ -25,11 +30,12 @@ async function wire(url, method, endpoint, body, token) {
|
|
|
25
30
|
...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
|
|
26
31
|
},
|
|
27
32
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
}, `${method} ${endpoint}`, timeoutMs);
|
|
34
|
+
if (!res.ok)
|
|
35
|
+
throw answerError(res.status, answer);
|
|
36
|
+
if (answer.kind === 'text')
|
|
37
|
+
die('Phantom AI sent a web page instead of data. Check PHANTOM_BASE_URL, and try again in a minute.', 'bad_answer');
|
|
38
|
+
return answer.json;
|
|
33
39
|
}
|
|
34
40
|
function credentials(input) {
|
|
35
41
|
const file = credentialPath(input.env);
|
|
@@ -194,15 +200,18 @@ async function pollForApproval(input, opts, ctx) {
|
|
|
194
200
|
io.stderr(' Sign-in cancelled. Nothing was saved.\n');
|
|
195
201
|
return 0;
|
|
196
202
|
}
|
|
203
|
+
// The sleep may have used up the time. One stuck request must not carry the wait past the deadline.
|
|
204
|
+
const left = deadline - Date.now();
|
|
205
|
+
if (left <= 0)
|
|
206
|
+
break;
|
|
197
207
|
let token;
|
|
198
208
|
try {
|
|
199
|
-
token = await wire(url, 'POST', '/auth/device/token', {
|
|
200
|
-
device_code: start.device_code,
|
|
201
|
-
});
|
|
209
|
+
token = await wire(url, 'POST', '/auth/device/token', { device_code: start.device_code }, undefined, Math.min(REQUEST_TIMEOUT_MS, left));
|
|
202
210
|
}
|
|
203
211
|
catch (error) {
|
|
204
212
|
const code = error.code;
|
|
205
|
-
|
|
213
|
+
// One poll that got no answer is not the end: ask again until the deadline.
|
|
214
|
+
if (code === 'authorization_pending' || code === 'request_timeout')
|
|
206
215
|
continue;
|
|
207
216
|
if (code === 'slow_down') {
|
|
208
217
|
interval += 5000;
|
|
@@ -298,6 +307,8 @@ export async function workspaceCommand(input) {
|
|
|
298
307
|
if (command === 'create' || command === 'rotate') {
|
|
299
308
|
if (flags.save === true)
|
|
300
309
|
die('--save requires a key name.');
|
|
310
|
+
// Checked before anything is sent: a mistyped limit must not turn into "no limit".
|
|
311
|
+
const typedLimit = limitFlag(flags, 'limit', true);
|
|
301
312
|
// keys create at a terminal with nothing given: a short form (name, then limit), and the key to hand over.
|
|
302
313
|
if (command === 'create' && flags.save === undefined && flags.limit === undefined) {
|
|
303
314
|
const pick = pickIo(input);
|
|
@@ -315,12 +326,9 @@ export async function workspaceCommand(input) {
|
|
|
315
326
|
const replacing = existsSync(target);
|
|
316
327
|
if (replacing && name !== mine)
|
|
317
328
|
die(`A key is already saved here as "${name}". Pick another name with --save <name>.`, 'key_exists');
|
|
318
|
-
const limit =
|
|
329
|
+
const limit = typedLimit ?? null;
|
|
319
330
|
const result = await wire(saved.base_url, 'POST', command === 'create' ? endpoint + '/keys' : endpoint + '/keys/' + String(id), command === 'create' ? { name, limit_usd: limit } : { action: 'rotate' }, saved.management_credential);
|
|
320
|
-
|
|
321
|
-
saveApiKey(env, result.api_key);
|
|
322
|
-
else
|
|
323
|
-
saveNamedKey(env, name, result.api_key, { replace: replacing });
|
|
331
|
+
saveIssuedKey(input, name, result.api_key, { replace: replacing, rotated: command === 'rotate' });
|
|
324
332
|
io.stdout(showsScreen(input)
|
|
325
333
|
? keyChangeScreen(command === 'create' ? 'created' : 'rotated', name, {
|
|
326
334
|
env,
|
|
@@ -361,6 +369,13 @@ export async function workspaceCommand(input) {
|
|
|
361
369
|
return 0;
|
|
362
370
|
}
|
|
363
371
|
if (command === 'set') {
|
|
372
|
+
// Every value is checked before the server is asked anything, so a typo changes nothing.
|
|
373
|
+
const typed = {
|
|
374
|
+
limit_usd: limitFlag(flags, 'limit', true),
|
|
375
|
+
budget_usd: limitFlag(flags, 'budget', true),
|
|
376
|
+
period_days: limitFlag(flags, 'period'),
|
|
377
|
+
rate_usd_per_min: limitFlag(flags, 'rate', true),
|
|
378
|
+
};
|
|
364
379
|
const which = await whichKey(input, saved, endpoint, 'change');
|
|
365
380
|
if (which === null)
|
|
366
381
|
return 0;
|
|
@@ -372,18 +387,12 @@ export async function workspaceCommand(input) {
|
|
|
372
387
|
const limit = await chooseLimit(pick);
|
|
373
388
|
if (limit === undefined)
|
|
374
389
|
return 0;
|
|
375
|
-
|
|
390
|
+
typed.limit_usd = limit;
|
|
376
391
|
}
|
|
377
392
|
const body = {};
|
|
378
|
-
const
|
|
379
|
-
limit_usd: flags.limit,
|
|
380
|
-
budget_usd: flags.budget,
|
|
381
|
-
period_days: flags.period,
|
|
382
|
-
rate_usd_per_min: flags.rate,
|
|
383
|
-
};
|
|
384
|
-
for (const [field, value] of Object.entries(controls))
|
|
393
|
+
for (const [field, value] of Object.entries(typed))
|
|
385
394
|
if (value !== undefined)
|
|
386
|
-
body[field] = value
|
|
395
|
+
body[field] = value;
|
|
387
396
|
if (typeof flags.name === 'string')
|
|
388
397
|
body.name = flags.name;
|
|
389
398
|
if (typeof flags['policy-file'] === 'string')
|
|
@@ -542,6 +551,8 @@ function leaveFund(input) {
|
|
|
542
551
|
input.io.stderr(' Nothing was charged.\n');
|
|
543
552
|
return 0;
|
|
544
553
|
}
|
|
554
|
+
/** Keys that still work. A rotate or revoke leaves the old key in the list, turned off. */
|
|
555
|
+
const stillOn = (keys) => keys.filter((k) => k.is_active !== false);
|
|
545
556
|
/**
|
|
546
557
|
* The key a key command acts on. --id may be the id, its first few
|
|
547
558
|
* characters, the key's name, or the start of the key, the way people
|
|
@@ -560,7 +571,7 @@ async function whichKey(input, saved, endpoint, verb) {
|
|
|
560
571
|
const pick = pickIo(input);
|
|
561
572
|
if (!pick)
|
|
562
573
|
die(`Say which key with --id: its id, name or start. For example: phantom keys ${verb === 'change' ? 'set' : verb} --id CI${verb === 'change' ? ' --limit 5' : ''}`, 'key_usage');
|
|
563
|
-
const chosen = await choose(pick, `Which key do you want to ${verb}?`, view.keys.map((k) => ({
|
|
574
|
+
const chosen = await choose(pick, `Which key do you want to ${verb}?`, stillOn(view.keys).map((k) => ({
|
|
564
575
|
label: k.name ?? 'Key',
|
|
565
576
|
hint: `${k.prefix ?? ''} · id ${k.id.slice(0, 8)} · spent $${(k.spent_usd ?? 0).toFixed(2)}`,
|
|
566
577
|
value: k.id,
|
|
@@ -571,17 +582,28 @@ async function whichKey(input, saved, endpoint, verb) {
|
|
|
571
582
|
input.io.stderr(' Nothing was changed.\n');
|
|
572
583
|
return chosen === null ? null : named(chosen);
|
|
573
584
|
}
|
|
574
|
-
/**
|
|
575
|
-
|
|
585
|
+
/**
|
|
586
|
+
* Finds one key by id, the start of its id, its name, or the start of the key itself. Refuses when none or more than one matches.
|
|
587
|
+
* Keys that are turned off never match: after a rotate, the old key and the new one share a name.
|
|
588
|
+
*/
|
|
589
|
+
export function findKey(allKeys, typed) {
|
|
590
|
+
const keys = stillOn(allKeys);
|
|
576
591
|
const t = typed.trim().toLowerCase();
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
592
|
+
const matching = (list) => {
|
|
593
|
+
const hits = list.filter((k) => k.id.toLowerCase() === t);
|
|
594
|
+
return hits.length ? hits : list.filter((k) => (t.length >= 4 && k.id.toLowerCase().startsWith(t)) || k.name?.toLowerCase() === t || (t.length >= 8 && k.prefix?.toLowerCase().startsWith(t)));
|
|
595
|
+
};
|
|
596
|
+
const loose = matching(keys);
|
|
597
|
+
const label = (k) => `${k.name ?? 'Key'} (${k.id.slice(0, 8)})`;
|
|
580
598
|
if (loose.length === 1)
|
|
581
599
|
return loose[0].id;
|
|
582
|
-
if (!loose.length)
|
|
583
|
-
|
|
584
|
-
|
|
600
|
+
if (!loose.length) {
|
|
601
|
+
const off = matching(allKeys.filter((k) => k.is_active === false));
|
|
602
|
+
if (off.length)
|
|
603
|
+
die(`"${typed}" is a key that is turned off (${off.map(label).join(', ')}). Your keys that work: ${keys.map(label).join(', ') || 'none'}.`, 'key_not_found');
|
|
604
|
+
die(`No key matches "${typed}". Your keys: ${keys.map(label).join(', ') || 'none yet'}.`, 'key_not_found');
|
|
605
|
+
}
|
|
606
|
+
die(`"${typed}" matches more than one key: ${loose.map(label).join(', ')}. Use more of the id.`, 'key_ambiguous');
|
|
585
607
|
}
|
|
586
608
|
/** The name a key with this id is saved under on this computer, if any: `default` for the login key. */
|
|
587
609
|
function savedNameFor(env, id) {
|
|
@@ -632,7 +654,7 @@ async function newKeyForm(input, pick, saved, endpoint) {
|
|
|
632
654
|
if (limit === undefined)
|
|
633
655
|
return leaveKeys(input);
|
|
634
656
|
const result = await wire(saved.base_url, 'POST', endpoint + '/keys', { name, limit_usd: limit }, saved.management_credential);
|
|
635
|
-
|
|
657
|
+
saveIssuedKey(input, name, result.api_key, { rotated: false });
|
|
636
658
|
const style = {
|
|
637
659
|
env,
|
|
638
660
|
color: colorOn(env, io.stdoutIsTTY),
|
|
@@ -656,6 +678,30 @@ async function newKeyForm(input, pick, saved, endpoint) {
|
|
|
656
678
|
}
|
|
657
679
|
return 0;
|
|
658
680
|
}
|
|
681
|
+
/**
|
|
682
|
+
* Saves a key the server has just made. When the save fails (a read-only file, a locked Keychain over
|
|
683
|
+
* SSH), this computer is the only place the key ever exists, so it goes to stderr once with how to
|
|
684
|
+
* save it, and the command exits 1. After a rotate the old key is already off, and the message says so.
|
|
685
|
+
*/
|
|
686
|
+
function saveIssuedKey(input, name, key, opts) {
|
|
687
|
+
const { env, io } = input;
|
|
688
|
+
try {
|
|
689
|
+
if (name === 'default')
|
|
690
|
+
saveApiKey(env, key);
|
|
691
|
+
else
|
|
692
|
+
saveNamedKey(env, name, key, { replace: opts.replace });
|
|
693
|
+
}
|
|
694
|
+
catch (error) {
|
|
695
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
696
|
+
const how = name === 'default' ? 'phantom login --paste (paste the key when it asks)' : `phantom keys save ${name} (paste the key when it asks)`;
|
|
697
|
+
io.stderr(`\nphantom made your new key but could not save it on this computer: ${why}\n` +
|
|
698
|
+
(opts.rotated ? 'Your old key is turned off, so this new key is the only one that works.\n' : '') +
|
|
699
|
+
'This is the only time it is shown. Copy it somewhere private now; it works like a password:\n\n' +
|
|
700
|
+
` ${key}\n\n` +
|
|
701
|
+
`Save it once the problem is fixed with: ${how}\n`);
|
|
702
|
+
die('The new key was not saved here.', 'key_not_saved');
|
|
703
|
+
}
|
|
704
|
+
}
|
|
659
705
|
/** Esc out of a key form: back to the menu if it started this, otherwise say nothing changed. */
|
|
660
706
|
function leaveKeys(input) {
|
|
661
707
|
if (input.io.fromMenu)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantomcodes/cli",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "AI inference for your coding agents. Route models, control spending, and fund your workspace.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"phantom",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
27
|
"url": "git+https://github.com/connortessaro/phantom.git",
|
|
28
|
-
"directory": "
|
|
28
|
+
"directory": "cli"
|
|
29
29
|
},
|
|
30
30
|
"type": "module",
|
|
31
31
|
"bin": {
|
|
@@ -138,20 +138,6 @@ phantom spend --workspace --from 2026-09-01 --to 2026-09-30
|
|
|
138
138
|
saved against its frontier model. `--workspace` shows every key in the workspace by
|
|
139
139
|
name. Dates are UTC days (`YYYY-MM-DD`); the default is this month.
|
|
140
140
|
|
|
141
|
-
## Plans
|
|
142
|
-
|
|
143
|
-
```bash
|
|
144
|
-
phantom plan --table
|
|
145
|
-
phantom plan pro
|
|
146
|
-
phantom plan manage
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
A Pro or Max plan pays for `phantom-1` calls up to an amount of model cost in any 7 days,
|
|
150
|
-
then the key's credit pays. `plan` shows the plan and the week's use. `plan pro` and
|
|
151
|
-
`plan max` print a Stripe Checkout link (and open it unless `--no-browser`); the user
|
|
152
|
-
pays there. `plan manage` opens Stripe's billing page. Naming a model, not `phantom-1`,
|
|
153
|
-
still spends credit.
|
|
154
|
-
|
|
155
141
|
## Advanced tasks
|
|
156
142
|
|
|
157
143
|
Use `phantom --help --all` for receipts, MCP and direct-key purchases.
|
|
@@ -159,7 +145,7 @@ Export guest recovery with `phantom workspace export --file
|
|
|
159
145
|
<path>` only to a private destination. Recovery grants workspace management access.
|
|
160
146
|
Delete an empty workspace with `phantom workspace delete`: it takes the workspace's keys
|
|
161
147
|
and saved sign-ins with it, asks first (`--yes` skips the question), and refuses while the
|
|
162
|
-
workspace holds credit, a running request
|
|
148
|
+
workspace holds credit, a running request or a payment in progress.
|
|
163
149
|
|
|
164
150
|
## Rules
|
|
165
151
|
|
package/dist/plan/command.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { unknownSub } from "../cli/start.mjs";
|
|
2
|
-
import { colorOn } from "../core/style.mjs";
|
|
3
|
-
import { linkScreen, planScreen } from "../cli/screens.mjs";
|
|
4
|
-
import { die } from "../core/errors.mjs";
|
|
5
|
-
import { pickIo, showsScreen, openUrl, out } from "../core/helpers.mjs";
|
|
6
|
-
import { choose } from "../core/pick.mjs";
|
|
7
|
-
import { request } from "../core/request.mjs";
|
|
8
|
-
import { savedManagement } from "../workspace/command.mjs";
|
|
9
|
-
/** The plan of the key's workspace and its week (GET /plan). */
|
|
10
|
-
export function getPlan(apiKey, baseUrl) {
|
|
11
|
-
return request('GET', '/plan', apiKey, undefined, baseUrl);
|
|
12
|
-
}
|
|
13
|
-
/** A Stripe Checkout link that puts `plan` on the key's workspace (POST /plan/checkout). */
|
|
14
|
-
export function planCheckout(apiKey, plan, baseUrl) {
|
|
15
|
-
return request('POST', '/plan/checkout', apiKey, { plan }, baseUrl);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* A link to Stripe's billing page for a workspace's plan (POST /plan/portal).
|
|
19
|
-
* It takes the workspace's management credential: an inference key gets 403.
|
|
20
|
-
*/
|
|
21
|
-
export function planPortal(credential, baseUrl) {
|
|
22
|
-
return request('POST', '/plan/portal', credential, undefined, baseUrl);
|
|
23
|
-
}
|
|
24
|
-
const dollars = (n) => `$${n.toFixed(2)}`;
|
|
25
|
-
/** `--table` output for `plan`. */
|
|
26
|
-
export function tablePlan(d) {
|
|
27
|
-
const p = d;
|
|
28
|
-
if (!p.plan) {
|
|
29
|
-
return p.available
|
|
30
|
-
? 'plan none\nsubscribe phantom plan pro or phantom plan max'
|
|
31
|
-
: 'plan none (plans are not available right now)';
|
|
32
|
-
}
|
|
33
|
-
const lines = [
|
|
34
|
-
`plan ${p.plan === 'max' ? 'Max' : 'Pro'}, on this ${p.scope}`,
|
|
35
|
-
`status ${p.status}`,
|
|
36
|
-
`used ${dollars(p.week_used_usd)} of ${dollars(p.week_limit_usd)} in the last 7 days (model cost, before markup)`,
|
|
37
|
-
];
|
|
38
|
-
if (p.resets_at)
|
|
39
|
-
lines.push(`room starts to free up ${p.resets_at}`);
|
|
40
|
-
if (p.renews_at)
|
|
41
|
-
lines.push(`period ends ${p.renews_at}`);
|
|
42
|
-
return lines.join('\n');
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* `phantom plan`: the plan of the key's workspace and how much of the week is
|
|
46
|
-
* used. `phantom plan pro|max` prints a Stripe Checkout link that puts the
|
|
47
|
-
* plan on that workspace, and opens it unless `--no-browser`. `phantom plan
|
|
48
|
-
* manage` does the same for Stripe's billing page, where the card changes, the
|
|
49
|
-
* plan switches and it cancels; it sends the workspace credential `phantom
|
|
50
|
-
* login` saved, since a key can't manage billing. Run by `run()`.
|
|
51
|
-
*/
|
|
52
|
-
export async function planCommand(input) {
|
|
53
|
-
const { io, apiKey, baseUrl, flags, subcommand, env } = input;
|
|
54
|
-
const table = showsScreen(input);
|
|
55
|
-
const style = { env, color: colorOn(env, io.stdoutIsTTY), opened: !flags['no-browser'] };
|
|
56
|
-
const open = (url) => {
|
|
57
|
-
if (!flags['no-browser'])
|
|
58
|
-
openUrl(url);
|
|
59
|
-
};
|
|
60
|
-
if (subcommand === undefined || subcommand === 'status') {
|
|
61
|
-
out(await getPlan(apiKey, baseUrl), table, (d) => planScreen(d, style), io.stdout);
|
|
62
|
-
}
|
|
63
|
-
else if (subcommand === 'pro' || subcommand === 'max') {
|
|
64
|
-
// At a terminal, say what the plan costs and covers, and ask, before sending anyone to pay.
|
|
65
|
-
const pick = pickIo(input);
|
|
66
|
-
if (pick) {
|
|
67
|
-
const offer = (await getPlan(apiKey, baseUrl)).prices?.[subcommand];
|
|
68
|
-
const what = offer ? `$${offer.usd_per_month} a month for $${offer.week_limit_usd} of model cost a week, every key` : 'for every key in this workspace';
|
|
69
|
-
const go = await choose(pick, `Start ${subcommand === 'max' ? 'Max' : 'Pro'}?`, [
|
|
70
|
-
{ label: 'Open Stripe to pay', hint: what, value: true },
|
|
71
|
-
{ label: 'Cancel', hint: 'nothing is charged', value: false },
|
|
72
|
-
]);
|
|
73
|
-
if (!go) {
|
|
74
|
-
io.stderr(' Nothing was charged.\n');
|
|
75
|
-
return 0;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
const result = await planCheckout(apiKey, subcommand, baseUrl);
|
|
79
|
-
out(result, table, (d) => linkScreen(`Start ${subcommand === 'max' ? 'Max' : 'Pro'} on Stripe`, d.checkout_url, ['Once it goes through:', 'phantom plan'], style), io.stdout);
|
|
80
|
-
open(result.checkout_url);
|
|
81
|
-
}
|
|
82
|
-
else if (subcommand === 'manage') {
|
|
83
|
-
const result = await planPortal(savedManagement(input) ?? apiKey, baseUrl);
|
|
84
|
-
out(result, table, (d) => linkScreen('Manage billing on Stripe: card, switch plan, cancel', d.portal_url, null, style), io.stdout);
|
|
85
|
-
open(result.portal_url);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
die(unknownSub('plan', String(subcommand), ['status', 'pro', 'max', 'manage']), 'plan_usage');
|
|
89
|
-
}
|
|
90
|
-
return 0;
|
|
91
|
-
}
|