@phantomcodes/cli 1.6.3 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/README.md +139 -118
- package/bin/phantom.mjs +37 -2
- package/dist/agents/command.d.mts +8 -27
- package/dist/agents/command.mjs +323 -59
- package/dist/agents/login.d.mts +3 -0
- package/dist/agents/login.mjs +97 -0
- package/dist/agents/opencode-check.d.mts +15 -0
- package/dist/agents/opencode-check.mjs +193 -0
- package/dist/agents/phantom-key.d.mts +7 -0
- package/dist/agents/phantom-key.mjs +40 -0
- package/dist/agents/proxy.d.mts +26 -28
- package/dist/agents/proxy.mjs +225 -33
- package/dist/chatgpt/command.d.mts +2 -0
- package/dist/chatgpt/command.mjs +119 -0
- package/dist/chatgpt/session.d.mts +87 -0
- package/dist/chatgpt/session.mjs +253 -0
- package/dist/cli/accounts.d.mts +21 -0
- package/dist/cli/accounts.mjs +96 -0
- package/dist/cli/app.d.mts +4 -24
- package/dist/cli/app.mjs +224 -139
- package/dist/cli/commands.d.mts +1 -16
- package/dist/cli/commands.mjs +144 -239
- package/dist/cli/help.d.mts +0 -19
- package/dist/cli/help.mjs +30 -55
- package/dist/cli/run.mjs +35 -79
- package/dist/cli/start.d.mts +2 -31
- package/dist/cli/start.mjs +13 -116
- package/dist/core/brand.d.mts +0 -7
- package/dist/core/brand.mjs +0 -8
- package/dist/core/errors.d.mts +0 -16
- package/dist/core/errors.mjs +0 -16
- package/dist/core/helpers.d.mts +0 -46
- package/dist/core/helpers.mjs +1 -64
- package/dist/core/node-trust.d.mts +0 -5
- package/dist/core/node-trust.mjs +0 -6
- package/dist/core/pick.d.mts +7 -55
- package/dist/core/pick.mjs +49 -106
- package/dist/core/request.d.mts +0 -46
- package/dist/core/request.mjs +1 -60
- package/dist/core/secrets.d.mts +0 -4
- package/dist/core/secrets.mjs +0 -10
- package/dist/core/state.d.mts +3 -1
- package/dist/core/state.mjs +147 -4
- package/dist/core/style.d.mts +0 -27
- package/dist/core/style.mjs +0 -20
- package/dist/core/version.d.mts +1 -2
- package/dist/core/version.mjs +1 -2
- package/dist/local/accounts.d.mts +25 -0
- package/dist/local/accounts.mjs +40 -0
- package/dist/local/agent-headers.d.mts +7 -0
- package/dist/local/agent-headers.mjs +44 -0
- package/dist/local/chatgpt-plan.d.mts +147 -0
- package/dist/local/chatgpt-plan.mjs +384 -0
- package/dist/local/command.d.mts +24 -0
- package/dist/local/command.mjs +211 -0
- package/dist/local/config.d.mts +49 -0
- package/dist/local/config.mjs +168 -0
- package/dist/local/decide.d.mts +37 -0
- package/dist/local/decide.mjs +201 -0
- package/dist/local/failure-signs.d.mts +4 -0
- package/dist/local/failure-signs.mjs +99 -0
- package/dist/local/keys.d.mts +32 -0
- package/dist/local/keys.mjs +239 -0
- package/dist/local/models.d.mts +53 -0
- package/dist/local/models.mjs +117 -0
- package/dist/local/replay.d.mts +59 -0
- package/dist/local/replay.mjs +143 -0
- package/dist/local/report.d.mts +43 -0
- package/dist/local/report.mjs +153 -0
- package/dist/local/request-log.d.mts +53 -0
- package/dist/local/request-log.mjs +69 -0
- package/dist/local/retry-after.d.mts +3 -0
- package/dist/local/retry-after.mjs +28 -0
- package/dist/local/rust-router.d.mts +39 -0
- package/dist/local/rust-router.mjs +54 -0
- package/dist/local/server.d.mts +27 -0
- package/dist/local/server.mjs +449 -0
- package/dist/local/status-line.d.mts +17 -0
- package/dist/local/status-line.mjs +81 -0
- package/dist/local/tiers.d.mts +3 -0
- package/dist/local/tiers.mjs +137 -0
- package/dist/local/traces.d.mts +69 -0
- package/dist/local/traces.mjs +341 -0
- package/dist/phantom.d.mts +0 -37
- package/dist/phantom.mjs +0 -31
- package/package.json +15 -9
- package/skills/phantom-ai/SKILL.md +28 -97
- package/dist/cli/screens.d.mts +0 -177
- package/dist/cli/screens.mjs +0 -398
- package/dist/core/free.d.mts +0 -13
- package/dist/core/free.mjs +0 -14
- package/dist/core/motion.d.mts +0 -47
- package/dist/core/motion.mjs +0 -156
- package/dist/core/shapes.d.mts +0 -13
- package/dist/core/shapes.mjs +0 -1
- package/dist/keys/command.d.mts +0 -16
- package/dist/keys/command.mjs +0 -143
- package/dist/keys/saved.d.mts +0 -96
- package/dist/keys/saved.mjs +0 -169
- package/dist/models/choice.d.mts +0 -7
- package/dist/models/choice.mjs +0 -27
- package/dist/models/command.d.mts +0 -8
- package/dist/models/command.mjs +0 -66
- package/dist/models/list.d.mts +0 -39
- package/dist/models/list.mjs +0 -39
- package/dist/receipts/command.d.mts +0 -7
- package/dist/receipts/command.mjs +0 -41
- package/dist/receipts/receipts.d.mts +0 -76
- package/dist/receipts/receipts.mjs +0 -118
- package/dist/setup/agents.d.mts +0 -149
- package/dist/setup/agents.mjs +0 -886
- package/dist/setup/apply.d.mts +0 -85
- package/dist/setup/apply.mjs +0 -301
- package/dist/setup/command.d.mts +0 -12
- package/dist/setup/command.mjs +0 -307
- package/dist/setup/editors.d.mts +0 -108
- package/dist/setup/editors.mjs +0 -500
- package/dist/setup/legacy.d.mts +0 -13
- package/dist/setup/legacy.mjs +0 -36
- package/dist/setup/plan.d.mts +0 -104
- package/dist/setup/plan.mjs +0 -132
- package/dist/setup/screen.d.mts +0 -30
- package/dist/setup/screen.mjs +0 -260
- package/dist/setup/shared.d.mts +0 -141
- package/dist/setup/shared.mjs +0 -88
- package/dist/setup/was.d.mts +0 -42
- package/dist/setup/was.mjs +0 -91
- package/dist/spend/command.d.mts +0 -5
- package/dist/spend/command.mjs +0 -235
- package/dist/spend/lineup.d.mts +0 -61
- package/dist/spend/lineup.mjs +0 -39
- package/dist/spend/reason.d.mts +0 -5
- package/dist/spend/reason.mjs +0 -60
- package/dist/spend/report.d.mts +0 -105
- package/dist/spend/report.mjs +0 -68
- package/dist/spend/routing.d.mts +0 -74
- package/dist/spend/routing.mjs +0 -57
- package/dist/workspace/command.d.mts +0 -59
- package/dist/workspace/command.mjs +0 -871
package/dist/setup/command.mjs
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
3
|
-
import { createInterface } from 'node:readline/promises';
|
|
4
|
-
import { die } from "../core/errors.mjs";
|
|
5
|
-
import { out, pickIo } from "../core/helpers.mjs";
|
|
6
|
-
import { BackToMenu, checklist, choose } from "../core/pick.mjs";
|
|
7
|
-
import { request } from "../core/request.mjs";
|
|
8
|
-
import { PhantomApiError } from "../core/errors.mjs";
|
|
9
|
-
import { keyHash, resolveApiKey } from "../keys/saved.mjs";
|
|
10
|
-
import { savedWorkspace, wire, workspaceLogin } from "../workspace/command.mjs";
|
|
11
|
-
import { SETUP_AGENTS, SETUP_NAMES } from "./agents.mjs";
|
|
12
|
-
import { planSetup } from "./plan.mjs";
|
|
13
|
-
import { applySetup, previewSetup, restoreSetup } from "./apply.mjs";
|
|
14
|
-
import { setupScreen } from "./screen.mjs";
|
|
15
|
-
import { colorOn, paint, unstyled } from "../core/style.mjs";
|
|
16
|
-
import { inApp } from "../cli/screens.mjs";
|
|
17
|
-
import { brandIntro, motionOn, play, reveal } from "../core/motion.mjs";
|
|
18
|
-
import { savedModel } from "../models/choice.mjs";
|
|
19
|
-
/** `--table` output for `setup`, and what a terminal shows: see {@link setupScreen}. */
|
|
20
|
-
export function tableSetup(d, opts = {}) {
|
|
21
|
-
return setupScreen(d, opts);
|
|
22
|
-
}
|
|
23
|
-
/** `--table` output for `setup --restore`, and what a terminal shows: a ✓ per file put back, a ✗ per file left alone. */
|
|
24
|
-
export function tableRestore(d, opts = {}) {
|
|
25
|
-
const r = d;
|
|
26
|
-
const p = paint(opts.env ?? {}, Boolean(opts.color));
|
|
27
|
-
const home = opts.env?.HOME;
|
|
28
|
-
const short = (file) => (home && file.startsWith(home + '/') ? '~' + file.slice(home.length) : file);
|
|
29
|
-
if (!r.restored.length && !r.skipped.length)
|
|
30
|
-
return 'Nothing to restore.';
|
|
31
|
-
const lines = [
|
|
32
|
-
...r.restored.map((x) => {
|
|
33
|
-
// A file the user changed since setup: only phantom's own lines came out.
|
|
34
|
-
if (x.lines_removed !== undefined) {
|
|
35
|
-
const mark = r.dry_run ? p.dim('·') : p.lime('✓');
|
|
36
|
-
if (!x.lines_removed)
|
|
37
|
-
return ` ${mark} ${short(x.file)} had no Phantom lines left`;
|
|
38
|
-
const kept = x.copy_kept ? ` The copy from before setup is still at ${short(x.copy_kept)}.` : '';
|
|
39
|
-
return ` ${mark} ${r.dry_run ? 'takes' : 'took'} Phantom's lines out of ${short(x.file)}; your own changes stay.${kept}`;
|
|
40
|
-
}
|
|
41
|
-
const verb = x.from ? (r.dry_run ? 'puts back' : 'put back') : r.dry_run ? 'deletes' : 'removed';
|
|
42
|
-
const what = x.from ? `${verb} ${short(x.file)} as it was before setup` : `${verb} ${short(x.file)} (phantom made this file)`;
|
|
43
|
-
return ` ${r.dry_run ? p.dim('·') : p.lime('✓')} ${what}`;
|
|
44
|
-
}),
|
|
45
|
-
...r.skipped.map((x) => ` ${p.red('✗')} left ${short(x.file)}: ${x.reason}`),
|
|
46
|
-
];
|
|
47
|
-
if (!r.dry_run && r.restored.length) {
|
|
48
|
-
// What undo leaves alone, so nobody thinks they're back to nothing when they're still signed in.
|
|
49
|
-
lines.push('', inApp() ? ` ${p.dim('Still here: your sign-in and saved keys. To remove those too, pick Sign out below.')}` : ` ${p.dim('Still here: your sign-in and saved keys. To remove those too:')} ${p.cyan('phantom logout --all')}`);
|
|
50
|
-
// Cursor was set up by hand, so undo can't reach it; say what to do there.
|
|
51
|
-
if (opts.env?.HOME && existsSync(path.join(opts.env.HOME, '.cursor')))
|
|
52
|
-
lines.push(` ${p.dim("Cursor: if you set it up by hand, turn off Override OpenAI Base URL in Cursor Settings, Models.")}`);
|
|
53
|
-
lines.push(` ${p.lime('→')} Restart your agents (and open a new terminal) so they go back to how they were.`);
|
|
54
|
-
}
|
|
55
|
-
return lines.join('\n');
|
|
56
|
-
}
|
|
57
|
-
/** `phantom setup`: plans, previews, asks, applies or restores agent setup. Plain setup with no key yet signs in first when it runs in a terminal. Run by `run()`. */
|
|
58
|
-
export async function setupCommand(input) {
|
|
59
|
-
const { env, io, flags } = input;
|
|
60
|
-
// A person at a terminal gets the short summary; a pipe, or --json, gets JSON.
|
|
61
|
-
const table = input.table || (Boolean(io.stdoutIsTTY) && !flags['json']);
|
|
62
|
-
const diff = Boolean(flags['diff']);
|
|
63
|
-
// The summary goes to stdout and the question to stderr, so each gets color only when it is a terminal.
|
|
64
|
-
const shown = (opts = {}) => (d) => tableSetup(d, { env, diff, color: colorOn(env, io.stdoutIsTTY), columns: process.stdout.columns, ...opts });
|
|
65
|
-
/**
|
|
66
|
-
* Prints a result to stdout: JSON for a pipe, the screen otherwise, played
|
|
67
|
-
* as a flipbook when motion is on. The header draws in on a preview; after
|
|
68
|
-
* the changes, each ✓ ticks in after the one before.
|
|
69
|
-
*/
|
|
70
|
-
const emit = async (result) => {
|
|
71
|
-
if (!table || !motionOn(env, io.stdoutIsTTY))
|
|
72
|
-
return out(result, table, shown(), io.stdout);
|
|
73
|
-
const lines = shown()(result).split('\n');
|
|
74
|
-
const book = result.dry_run ? brandIntro(lines) : reveal(lines, (line) => (/^\s+\S*[✓!✗]/.test(unstyled(line)) ? 60 : 0));
|
|
75
|
-
await play(io.stdout, book, { rows: process.stdout.rows, skipOnKey: true });
|
|
76
|
-
};
|
|
77
|
-
const agent = flags['agent'];
|
|
78
|
-
if (agent === true)
|
|
79
|
-
die(`--agent requires ${SETUP_NAMES.join(', ')} or all`);
|
|
80
|
-
const provider = flags['provider'];
|
|
81
|
-
if (provider !== undefined && provider !== true && provider !== 'off')
|
|
82
|
-
die('--provider takes no value, or off');
|
|
83
|
-
const model = flags['model'];
|
|
84
|
-
if (model === true)
|
|
85
|
-
die('--model requires a model id, such as auto or claude-sonnet-5');
|
|
86
|
-
if (flags['mcp'])
|
|
87
|
-
die('--mcp has been removed. Start your agent on Phantom AI with phantom claude, phantom codex or phantom pi.');
|
|
88
|
-
const dryRun = Boolean(flags['dry-run']);
|
|
89
|
-
if (flags['restore']) {
|
|
90
|
-
// At a terminal, undo shows what it will put back, and asks, the way setup asks before it changes anything.
|
|
91
|
-
const pick = !dryRun && !flags['yes'] ? pickIo(input) : null;
|
|
92
|
-
if (pick) {
|
|
93
|
-
const preview = restoreSetup(env, { agent, dryRun: true });
|
|
94
|
-
if (!preview.restored.length && !preview.skipped.length) {
|
|
95
|
-
io.stdout(' Nothing to undo: setup has no changes on this computer.\n');
|
|
96
|
-
return 0;
|
|
97
|
-
}
|
|
98
|
-
io.stderr(`\n${tableRestore(preview, { env, color: colorOn(env, io.isTTY) })}\n\n`);
|
|
99
|
-
// The marker starts on Cancel: undo can't itself be undone, so it takes a deliberate move.
|
|
100
|
-
const go = await choose(pick, 'Undo these changes?', [
|
|
101
|
-
{ label: 'Undo them', hint: 'your agents go back to how they were', value: true },
|
|
102
|
-
{ label: 'Cancel', hint: 'change nothing', value: false },
|
|
103
|
-
], 1);
|
|
104
|
-
if (!go) {
|
|
105
|
-
if (go === null && io.fromMenu)
|
|
106
|
-
throw new BackToMenu();
|
|
107
|
-
io.stderr(' Nothing was changed.\n');
|
|
108
|
-
return 0;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const result = restoreSetup(env, { agent, dryRun });
|
|
112
|
-
out(result, table, (d) => tableRestore(d, { env, color: colorOn(env, io.stdoutIsTTY) }), io.stdout);
|
|
113
|
-
return result.skipped.length ? 1 : 0;
|
|
114
|
-
}
|
|
115
|
-
// Plain `phantom setup` sets up everything it finds and asks first. Any of
|
|
116
|
-
// the older flags runs just what they name, without asking.
|
|
117
|
-
const guided = agent === undefined && provider === undefined;
|
|
118
|
-
// Checked before anything is written, so a typo in --limit changes nothing.
|
|
119
|
-
const limit = capFlag(flags['limit']);
|
|
120
|
-
// With no key yet, sign in first, as phantom claude does, so the agents
|
|
121
|
-
// get one in this run. Its words go to stderr; stdout stays setup's result.
|
|
122
|
-
if (guided && !dryRun && io.isTTY && !resolveApiKey(env)) {
|
|
123
|
-
await workspaceLogin({ ...input, io: { ...io, stdout: io.stderr }, flags: flags['no-browser'] ? { 'no-browser': true } : {} }, { inSetup: true });
|
|
124
|
-
// Sign-in was cancelled: stop here rather than set up the agents without a key.
|
|
125
|
-
if (!resolveApiKey(env)) {
|
|
126
|
-
io.stderr(' Nothing was changed.\n');
|
|
127
|
-
return 0;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const options = {
|
|
131
|
-
agent,
|
|
132
|
-
provider: guided || provider === true ? true : provider === 'off' ? 'off' : false,
|
|
133
|
-
// No --model: the one picked with `phantom models`, or phantom-1.
|
|
134
|
-
model: typeof model === 'string' ? model : savedModel(env),
|
|
135
|
-
guided,
|
|
136
|
-
shell: !flags['no-shell'],
|
|
137
|
-
};
|
|
138
|
-
const found = planSetup(env, options);
|
|
139
|
-
let plan = found;
|
|
140
|
-
// At a terminal, plain setup asks which of the agents it found to connect (all ticked, Space unticks),
|
|
141
|
-
// then whether to make the changes. Esc on the question goes back to the checklist.
|
|
142
|
-
const pick = guided && !flags['yes'] && !dryRun ? pickIo(input) : null;
|
|
143
|
-
const ticks = Boolean(pick) && (found.agents.length > 1 || found.files.some((f) => f.agent === 'shell'));
|
|
144
|
-
/** Shows the checklist, or returns false when Esc there means leave. */
|
|
145
|
-
// The shell file is a row of its own, so it can be left out like an agent.
|
|
146
|
-
const shellRow = found.files.some((f) => f.agent === 'shell');
|
|
147
|
-
const tick = async () => {
|
|
148
|
-
const only = await checklist(pick, 'Which agents should use Phantom AI?', [
|
|
149
|
-
...found.agents.map((a) => ({ label: SETUP_AGENTS[a.agent].label, hint: a.provider ? 'skill and model settings' : 'skill', value: a.agent, checked: plan.agents.some((p) => p.agent === a.agent) })),
|
|
150
|
-
...(shellRow ? [{ label: 'Your shell', hint: 'new terminals get your key; untick to add it yourself', value: 'shell', checked: plan.files.some((f) => f.agent === 'shell') }] : []),
|
|
151
|
-
]);
|
|
152
|
-
if (!only)
|
|
153
|
-
return false;
|
|
154
|
-
plan = planSetup(env, { ...options, only: only.filter((a) => a !== 'shell'), shell: shellRow ? only.includes('shell') : options.shell });
|
|
155
|
-
return true;
|
|
156
|
-
};
|
|
157
|
-
/** Esc on setup's first step: back to the menu when it started setup, otherwise stop with nothing changed. */
|
|
158
|
-
const leave = () => {
|
|
159
|
-
if (io.fromMenu)
|
|
160
|
-
throw new BackToMenu();
|
|
161
|
-
io.stderr(' Nothing was changed.\n');
|
|
162
|
-
return 0;
|
|
163
|
-
};
|
|
164
|
-
if (ticks && !(await tick()))
|
|
165
|
-
return leave();
|
|
166
|
-
/**
|
|
167
|
-
* Prints the result and gives the exit code: 1 when a file was skipped, or
|
|
168
|
-
* when there was no key so the agents weren't pointed at Phantom AI. A script
|
|
169
|
-
* checking only the exit code then can't mistake half a setup for all of it.
|
|
170
|
-
*/
|
|
171
|
-
const finish = async (result) => {
|
|
172
|
-
const partial = guided && provider !== 'off' && !resolveApiKey(env);
|
|
173
|
-
await emit(partial ? { ...result, complete: false } : result);
|
|
174
|
-
return result.skipped || partial ? 1 : 0;
|
|
175
|
-
};
|
|
176
|
-
if (dryRun) {
|
|
177
|
-
await emit(previewSetup(plan));
|
|
178
|
-
return 0;
|
|
179
|
-
}
|
|
180
|
-
if (guided && !flags['yes'] && plan.files.length) {
|
|
181
|
-
const preview = setupScreen(previewSetup(plan), { env, diff, color: colorOn(env, io.isTTY), columns: process.stderr.columns, mode: 'ask', hint: !(pick && !io.ask) });
|
|
182
|
-
// The question waits for the header and rows to finish drawing in.
|
|
183
|
-
if (motionOn(env, io.isTTY))
|
|
184
|
-
await play(io.stderr, brandIntro(preview.split('\n')), { rows: process.stderr.rows, skipOnKey: true });
|
|
185
|
-
else
|
|
186
|
-
io.stderr(preview + '\n');
|
|
187
|
-
io.stderr('\n');
|
|
188
|
-
// At a terminal: a choice instead of typing y or n, with a look at every line first if wanted.
|
|
189
|
-
if (pick && !io.ask) {
|
|
190
|
-
for (;;) {
|
|
191
|
-
const answer = await choose({ ...pick, esc: ticks || io.fromMenu ? 'back' : 'cancel' }, 'Make these changes?', [
|
|
192
|
-
{ label: 'Make these changes', value: 'yes' },
|
|
193
|
-
{ label: 'Show every line first', value: 'diff' },
|
|
194
|
-
{ label: 'Cancel', hint: 'change nothing', value: 'no' },
|
|
195
|
-
]);
|
|
196
|
-
if (answer === 'diff') {
|
|
197
|
-
io.stderr(setupScreen(previewSetup(plan), { env, diff: true, color: colorOn(env, io.isTTY), columns: process.stderr.columns, mode: 'ask' }) + '\n\n');
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
// Esc: back to the checklist, then the preview again; or out, when there was no checklist.
|
|
201
|
-
if (answer === null) {
|
|
202
|
-
if (!ticks || !(await tick()))
|
|
203
|
-
return leave();
|
|
204
|
-
io.stderr('\n' + setupScreen(previewSetup(plan), { env, diff, color: colorOn(env, io.isTTY), columns: process.stderr.columns, mode: 'ask', hint: false }) + '\n\n');
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
if (answer !== 'yes') {
|
|
208
|
-
await emit({ ...previewSetup(plan), dry_run: undefined, applied: false });
|
|
209
|
-
return 0;
|
|
210
|
-
}
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
return capAfter(input, await finish(applySetup(env, plan)), limit, guided ? pick : null);
|
|
214
|
-
}
|
|
215
|
-
const ask = io.ask ??
|
|
216
|
-
(process.stdin.isTTY
|
|
217
|
-
? async (q) => {
|
|
218
|
-
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
219
|
-
try {
|
|
220
|
-
return await rl.question(q);
|
|
221
|
-
}
|
|
222
|
-
finally {
|
|
223
|
-
rl.close();
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
: null);
|
|
227
|
-
if (!ask)
|
|
228
|
-
die('phantom setup changes the files above. Run it in a terminal to confirm, or add --yes (or --dry-run to only look)', 'confirm_needed');
|
|
229
|
-
const q = paint(env, colorOn(env, io.isTTY));
|
|
230
|
-
const answer = await ask(` ${q.lime('?')} ${q.bold('Make these changes?')} ${q.dim('[y/N]')} `);
|
|
231
|
-
if (!/^y(es)?$/i.test(answer.trim())) {
|
|
232
|
-
await emit({ ...previewSetup(plan), dry_run: undefined, applied: false });
|
|
233
|
-
return 0;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return capAfter(input, await finish(applySetup(env, plan)), limit, null);
|
|
237
|
-
}
|
|
238
|
-
/** `--limit`: dollars a month above zero, `none` to remove the cap, or undefined when it isn't given. */
|
|
239
|
-
function capFlag(value) {
|
|
240
|
-
if (value === undefined)
|
|
241
|
-
return undefined;
|
|
242
|
-
if (value === 'none')
|
|
243
|
-
return null;
|
|
244
|
-
const usd = Number(String(value).replace(/^\$/, ''));
|
|
245
|
-
if (typeof value !== 'string' || !Number.isFinite(usd) || usd <= 0)
|
|
246
|
-
die('--limit takes dollars a month above zero, such as 20, or none', 'setup_limit');
|
|
247
|
-
return usd;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Sets the key's monthly cap. The key alone may only tighten its caps (the
|
|
251
|
-
* server answers 403 to a raise or a removal), so that change goes through the
|
|
252
|
-
* workspace this computer is signed in to, whose credential may set any cap.
|
|
253
|
-
* False when the key alone may not make the change and no workspace is saved.
|
|
254
|
-
*/
|
|
255
|
-
async function setMonthlyCap(input, apiKey, cap) {
|
|
256
|
-
const body = cap === null ? { budget_usd: null } : { budget_usd: cap, period_days: null };
|
|
257
|
-
try {
|
|
258
|
-
await request('PATCH', '/key/budget', apiKey, body, input.baseUrl);
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
catch (err) {
|
|
262
|
-
if (!(err instanceof PhantomApiError) || err.status !== 403)
|
|
263
|
-
throw err;
|
|
264
|
-
}
|
|
265
|
-
const workspace = savedWorkspace(input);
|
|
266
|
-
if (!workspace)
|
|
267
|
-
return false;
|
|
268
|
-
await wire(workspace.base_url, 'PATCH', `/workspaces/${encodeURIComponent(workspace.workspace_id)}/keys/${keyHash(apiKey)}`, body, workspace.management_credential);
|
|
269
|
-
return true;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* After setup has written its files: the key's monthly spend cap (PATCH
|
|
273
|
-
* /key/budget, a calendar month). `--limit` sets it; without one, setup at a
|
|
274
|
-
* terminal asks once, only when the key has no cap yet. Returns setup's exit code.
|
|
275
|
-
*/
|
|
276
|
-
async function capAfter(input, code, limit, pick) {
|
|
277
|
-
const { env, io, baseUrl } = input;
|
|
278
|
-
const apiKey = resolveApiKey(env);
|
|
279
|
-
if (!apiKey || (limit === undefined && !pick))
|
|
280
|
-
return code;
|
|
281
|
-
let cap = limit;
|
|
282
|
-
if (cap === undefined) {
|
|
283
|
-
const now = await request('GET', '/key/budget', apiKey, undefined, baseUrl).catch(() => null);
|
|
284
|
-
if (!now || now.budget_usd !== null)
|
|
285
|
-
return code;
|
|
286
|
-
io.stderr('\n');
|
|
287
|
-
const answer = await choose(pick, 'Cap what this key can spend each month?', [
|
|
288
|
-
{ label: 'No cap', hint: 'it can spend the whole workspace balance; free models cost nothing', value: 0 },
|
|
289
|
-
{ label: '$10 a month', value: 10 },
|
|
290
|
-
{ label: '$25 a month', value: 25 },
|
|
291
|
-
{ label: '$50 a month', value: 50 },
|
|
292
|
-
]);
|
|
293
|
-
if (!answer)
|
|
294
|
-
return code;
|
|
295
|
-
cap = answer;
|
|
296
|
-
}
|
|
297
|
-
const p = paint(env, colorOn(env, io.isTTY));
|
|
298
|
-
if (!(await setMonthlyCap(input, apiKey, cap))) {
|
|
299
|
-
io.stderr(` ${p.yellow('!')} The key's cap is unchanged. The key alone can only lower its cap; raising or removing it needs its workspace.\n` +
|
|
300
|
-
` Run ${p.cyan('phantom login')}, then ${p.cyan(`phantom setup --limit ${cap === null ? 'none' : cap}`)} again, or change it in Edit controls on phantom.codes/keys.\n`);
|
|
301
|
-
return 1;
|
|
302
|
-
}
|
|
303
|
-
io.stderr(cap === null
|
|
304
|
-
? ` ${p.lime('✓')} This key has no monthly cap now.\n`
|
|
305
|
-
: ` ${p.lime('✓')} This key can spend at most $${cap} a month. Change it any time: ${p.cyan('phantom setup --limit <USD|none>')}\n`);
|
|
306
|
-
return code;
|
|
307
|
-
}
|
package/dist/setup/editors.d.mts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { Env } from '../core/state.mts';
|
|
2
|
-
/**
|
|
3
|
-
* A JSON or JSONC file's object, `{}` when it does not exist. Comments and
|
|
4
|
-
* trailing commas are fine: jsonc-parser reads them, as in Vercel's setup.
|
|
5
|
-
*/
|
|
6
|
-
export declare function jsonObject(file: string, text: string | null): Record<string, unknown>;
|
|
7
|
-
export declare function jsonText(v: unknown): string;
|
|
8
|
-
/**
|
|
9
|
-
* One change to a JSON file: the path of keys to a value, and the value. undefined removes it.
|
|
10
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
11
|
-
*/
|
|
12
|
-
export type JsonChange = [path: string[], value: unknown];
|
|
13
|
-
/** The value at a path of keys, or undefined. */
|
|
14
|
-
export declare function jsonGet(obj: unknown, p: string[]): unknown;
|
|
15
|
-
/** Sets the value at a path of keys, making the objects on the way. undefined removes it. */
|
|
16
|
-
export declare function jsonSet(obj: Record<string, unknown>, p: string[], value: unknown): void;
|
|
17
|
-
/**
|
|
18
|
-
* Applies `changes` to a JSON or JSONC file's text with jsonc-parser's
|
|
19
|
-
* `modify` and `applyEdits`, as Vercel's setup does, so comments, key order
|
|
20
|
-
* and formatting stay as they were. Only the text an edit inserts is
|
|
21
|
-
* formatted, in the file's own indent (none in a one-line file), so removing
|
|
22
|
-
* what was added gives back the same bytes. A new file is written in full. If
|
|
23
|
-
* the edited text doesn't read back as expected, the file is written in full
|
|
24
|
-
* instead, as Vercel's does.
|
|
25
|
-
* @throws {@link CliError} `setup_unreadable` when the file is not valid JSON or JSONC.
|
|
26
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
27
|
-
*/
|
|
28
|
-
export declare function jsonEdit(file: string, text: string | null, changes: JsonChange[]): string;
|
|
29
|
-
/**
|
|
30
|
-
* Sets a top-level `key: value` in a YAML file, marked `# phantom setup`. A line
|
|
31
|
-
* it replaces stays as a comment, so `--provider off` can put it back.
|
|
32
|
-
* @throws {@link CliError} `setup_unreadable` when the file indents with tabs, or the key holds more than one line.
|
|
33
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
34
|
-
*/
|
|
35
|
-
export declare function yamlSetTop(file: string, text: string | null, key: string, value: string): string;
|
|
36
|
-
/**
|
|
37
|
-
* Adds `item` (lines, unindented, the first starting with `- `) first in the
|
|
38
|
-
* top-level `models:` list of a YAML file, each line marked `# phantom setup`.
|
|
39
|
-
* Starts the list when there is none, and the file with `head` when it is
|
|
40
|
-
* empty. An entry phantom added before is replaced.
|
|
41
|
-
* @throws {@link CliError} `setup_unreadable` when the file indents with tabs or writes the list on one line.
|
|
42
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
43
|
-
*/
|
|
44
|
-
export declare function yamlAddModel(file: string, text: string | null, item: string[], head?: string[]): string;
|
|
45
|
-
/**
|
|
46
|
-
* Removes each YAML line marked `# phantom setup`, and puts back the lines they replaced. Returns '' when that leaves nothing but an empty `models:`, as in a file phantom started.
|
|
47
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
48
|
-
*/
|
|
49
|
-
export declare function yamlRemoveOurs(file: string, text: string): string;
|
|
50
|
-
/**
|
|
51
|
-
* Sets `key` in `table` ([] is the top of the file) to `value`, written as
|
|
52
|
-
* TOML, and marks the line with `# phantom setup`. A line it replaces stays as a
|
|
53
|
-
* comment, so `--provider off` can put it back. A missing table is added at
|
|
54
|
-
* the end of the file. The result must parse to the old file with this one
|
|
55
|
-
* key changed.
|
|
56
|
-
* @throws {@link CliError} `setup_unreadable` when the file is not valid TOML, or sets the key in a way a line edit can't change, such as an inline table or a dotted key.
|
|
57
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
58
|
-
*/
|
|
59
|
-
export declare function tomlSet(file: string, text: string, table: string[], key: string, value: string): string;
|
|
60
|
-
/**
|
|
61
|
-
* Removes what {@link tomlSet} wrote: each line marked `# phantom setup`, and a
|
|
62
|
-
* table phantom added once nothing else is left in it. Lines it replaced come back.
|
|
63
|
-
* @throws {@link CliError} `setup_unreadable` when the file is not valid TOML, before or after.
|
|
64
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
65
|
-
*/
|
|
66
|
-
export declare function tomlRemoveOurs(file: string, text: string): string;
|
|
67
|
-
/**
|
|
68
|
-
* The shell startup file the key block goes in, by $SHELL, as Vercel's
|
|
69
|
-
* `ai-gateway setup` picks it: `~/.zshrc` (or `$ZDOTDIR/.zshrc`),
|
|
70
|
-
* `~/.bash_profile` on macOS and `~/.bashrc` elsewhere, fish's `config.fish`
|
|
71
|
-
* (under `$XDG_CONFIG_HOME`), or `~/.profile` for any other shell. With no
|
|
72
|
-
* $SHELL, zsh on macOS and bash elsewhere.
|
|
73
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
74
|
-
*/
|
|
75
|
-
export declare function shellRcFile(env: Env, home?: string): string;
|
|
76
|
-
/**
|
|
77
|
-
* The parts of a phantom block that change: which agents use it, and each agent's own lines.
|
|
78
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
79
|
-
*/
|
|
80
|
-
export type ShellBlockState = {
|
|
81
|
-
/** The agents that read the key from the environment, in the order they were set up. */
|
|
82
|
-
agents: string[];
|
|
83
|
-
/** Each agent's own lines, from {@link SetupAgentSpec.shellEnv}. */
|
|
84
|
-
lines: Map<string, string[]>;
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* The block phantom adds to a shell startup file. When the shell starts it sets
|
|
88
|
-
* PHANTOM_API_KEY, unless it is set already: on macOS straight from the
|
|
89
|
-
* Keychain item the login key points to, and otherwise (or with
|
|
90
|
-
* PHANTOM_KEY_NAME set) by running `phantom key show`. The key itself is never
|
|
91
|
-
* written here. Each agent's own variables follow, set only when there is a key.
|
|
92
|
-
* @param fish Fish syntax instead of sh, bash and zsh.
|
|
93
|
-
* @deprecated Compatibility export. Use {@link planSetup}, {@link applySetup}, and {@link restoreSetup} for agent setup.
|
|
94
|
-
*/
|
|
95
|
-
export declare function shellBlock(env: Env, fish: boolean, state: ShellBlockState, onPathFn?: (bin: string) => boolean): string;
|
|
96
|
-
/**
|
|
97
|
-
* The startup file's text with the whole phantom block taken out, whichever
|
|
98
|
-
* agents it lists. Text outside the block stays as it was. Unchanged when
|
|
99
|
-
* there is no block.
|
|
100
|
-
* @throws {@link CliError} `setup_unreadable` when the block has no matching end line.
|
|
101
|
-
*/
|
|
102
|
-
export declare function withoutShellBlock(env: Env, file: string, text: string): string;
|
|
103
|
-
/**
|
|
104
|
-
* The startup file's text with the phantom block updated: agents in `on` added
|
|
105
|
-
* with fresh lines, agents in `off` dropped. The block goes away when no agent
|
|
106
|
-
* is left. Text outside the block stays as it was.
|
|
107
|
-
*/
|
|
108
|
-
export declare function withShellBlock(env: Env, file: string, text: string | null, on: Map<string, Record<string, string>>, off: string[], onPathFn?: (bin: string) => boolean): string | null;
|