@phantomcodes/cli 1.6.3 → 1.7.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.
Files changed (139) hide show
  1. package/README.md +138 -117
  2. package/bin/phantom.mjs +37 -2
  3. package/dist/agents/command.d.mts +8 -27
  4. package/dist/agents/command.mjs +323 -59
  5. package/dist/agents/login.d.mts +3 -0
  6. package/dist/agents/login.mjs +97 -0
  7. package/dist/agents/opencode-check.d.mts +15 -0
  8. package/dist/agents/opencode-check.mjs +193 -0
  9. package/dist/agents/phantom-key.d.mts +7 -0
  10. package/dist/agents/phantom-key.mjs +40 -0
  11. package/dist/agents/proxy.d.mts +26 -28
  12. package/dist/agents/proxy.mjs +225 -33
  13. package/dist/chatgpt/command.d.mts +2 -0
  14. package/dist/chatgpt/command.mjs +119 -0
  15. package/dist/chatgpt/session.d.mts +87 -0
  16. package/dist/chatgpt/session.mjs +253 -0
  17. package/dist/cli/accounts.d.mts +21 -0
  18. package/dist/cli/accounts.mjs +96 -0
  19. package/dist/cli/app.d.mts +4 -24
  20. package/dist/cli/app.mjs +224 -139
  21. package/dist/cli/commands.d.mts +1 -16
  22. package/dist/cli/commands.mjs +144 -239
  23. package/dist/cli/help.d.mts +0 -19
  24. package/dist/cli/help.mjs +30 -55
  25. package/dist/cli/run.mjs +35 -79
  26. package/dist/cli/start.d.mts +2 -31
  27. package/dist/cli/start.mjs +13 -116
  28. package/dist/core/brand.d.mts +0 -7
  29. package/dist/core/brand.mjs +0 -8
  30. package/dist/core/errors.d.mts +0 -16
  31. package/dist/core/errors.mjs +0 -16
  32. package/dist/core/helpers.d.mts +0 -46
  33. package/dist/core/helpers.mjs +1 -64
  34. package/dist/core/node-trust.d.mts +0 -5
  35. package/dist/core/node-trust.mjs +0 -6
  36. package/dist/core/pick.d.mts +7 -55
  37. package/dist/core/pick.mjs +49 -106
  38. package/dist/core/request.d.mts +0 -46
  39. package/dist/core/request.mjs +1 -60
  40. package/dist/core/secrets.d.mts +0 -4
  41. package/dist/core/secrets.mjs +0 -10
  42. package/dist/core/state.d.mts +3 -1
  43. package/dist/core/state.mjs +147 -4
  44. package/dist/core/style.d.mts +0 -27
  45. package/dist/core/style.mjs +0 -20
  46. package/dist/core/version.d.mts +1 -2
  47. package/dist/core/version.mjs +1 -2
  48. package/dist/local/accounts.d.mts +25 -0
  49. package/dist/local/accounts.mjs +40 -0
  50. package/dist/local/agent-headers.d.mts +7 -0
  51. package/dist/local/agent-headers.mjs +44 -0
  52. package/dist/local/chatgpt-plan.d.mts +147 -0
  53. package/dist/local/chatgpt-plan.mjs +384 -0
  54. package/dist/local/command.d.mts +24 -0
  55. package/dist/local/command.mjs +211 -0
  56. package/dist/local/config.d.mts +49 -0
  57. package/dist/local/config.mjs +168 -0
  58. package/dist/local/decide.d.mts +37 -0
  59. package/dist/local/decide.mjs +201 -0
  60. package/dist/local/failure-signs.d.mts +4 -0
  61. package/dist/local/failure-signs.mjs +99 -0
  62. package/dist/local/keys.d.mts +32 -0
  63. package/dist/local/keys.mjs +239 -0
  64. package/dist/local/models.d.mts +53 -0
  65. package/dist/local/models.mjs +117 -0
  66. package/dist/local/replay.d.mts +59 -0
  67. package/dist/local/replay.mjs +143 -0
  68. package/dist/local/report.d.mts +43 -0
  69. package/dist/local/report.mjs +153 -0
  70. package/dist/local/request-log.d.mts +53 -0
  71. package/dist/local/request-log.mjs +69 -0
  72. package/dist/local/retry-after.d.mts +3 -0
  73. package/dist/local/retry-after.mjs +28 -0
  74. package/dist/local/rust-router.d.mts +39 -0
  75. package/dist/local/rust-router.mjs +54 -0
  76. package/dist/local/server.d.mts +27 -0
  77. package/dist/local/server.mjs +449 -0
  78. package/dist/local/status-line.d.mts +17 -0
  79. package/dist/local/status-line.mjs +81 -0
  80. package/dist/local/tiers.d.mts +3 -0
  81. package/dist/local/tiers.mjs +137 -0
  82. package/dist/local/traces.d.mts +69 -0
  83. package/dist/local/traces.mjs +341 -0
  84. package/dist/phantom.d.mts +0 -37
  85. package/dist/phantom.mjs +0 -31
  86. package/package.json +15 -9
  87. package/skills/phantom-ai/SKILL.md +28 -97
  88. package/dist/cli/screens.d.mts +0 -177
  89. package/dist/cli/screens.mjs +0 -398
  90. package/dist/core/free.d.mts +0 -13
  91. package/dist/core/free.mjs +0 -14
  92. package/dist/core/motion.d.mts +0 -47
  93. package/dist/core/motion.mjs +0 -156
  94. package/dist/core/shapes.d.mts +0 -13
  95. package/dist/core/shapes.mjs +0 -1
  96. package/dist/keys/command.d.mts +0 -16
  97. package/dist/keys/command.mjs +0 -143
  98. package/dist/keys/saved.d.mts +0 -96
  99. package/dist/keys/saved.mjs +0 -169
  100. package/dist/models/choice.d.mts +0 -7
  101. package/dist/models/choice.mjs +0 -27
  102. package/dist/models/command.d.mts +0 -8
  103. package/dist/models/command.mjs +0 -66
  104. package/dist/models/list.d.mts +0 -39
  105. package/dist/models/list.mjs +0 -39
  106. package/dist/receipts/command.d.mts +0 -7
  107. package/dist/receipts/command.mjs +0 -41
  108. package/dist/receipts/receipts.d.mts +0 -76
  109. package/dist/receipts/receipts.mjs +0 -118
  110. package/dist/setup/agents.d.mts +0 -149
  111. package/dist/setup/agents.mjs +0 -886
  112. package/dist/setup/apply.d.mts +0 -85
  113. package/dist/setup/apply.mjs +0 -301
  114. package/dist/setup/command.d.mts +0 -12
  115. package/dist/setup/command.mjs +0 -307
  116. package/dist/setup/editors.d.mts +0 -108
  117. package/dist/setup/editors.mjs +0 -500
  118. package/dist/setup/legacy.d.mts +0 -13
  119. package/dist/setup/legacy.mjs +0 -36
  120. package/dist/setup/plan.d.mts +0 -104
  121. package/dist/setup/plan.mjs +0 -132
  122. package/dist/setup/screen.d.mts +0 -30
  123. package/dist/setup/screen.mjs +0 -260
  124. package/dist/setup/shared.d.mts +0 -141
  125. package/dist/setup/shared.mjs +0 -88
  126. package/dist/setup/was.d.mts +0 -42
  127. package/dist/setup/was.mjs +0 -91
  128. package/dist/spend/command.d.mts +0 -5
  129. package/dist/spend/command.mjs +0 -235
  130. package/dist/spend/lineup.d.mts +0 -61
  131. package/dist/spend/lineup.mjs +0 -39
  132. package/dist/spend/reason.d.mts +0 -5
  133. package/dist/spend/reason.mjs +0 -60
  134. package/dist/spend/report.d.mts +0 -105
  135. package/dist/spend/report.mjs +0 -68
  136. package/dist/spend/routing.d.mts +0 -74
  137. package/dist/spend/routing.mjs +0 -57
  138. package/dist/workspace/command.d.mts +0 -59
  139. package/dist/workspace/command.mjs +0 -871
@@ -1,85 +0,0 @@
1
- import { type Env } from '../core/state.mts';
2
- import type { ProviderResult } from './shared.mts';
3
- import { type SetupPlan, type SetupResult, type SetupOptions } from './plan.mts';
4
- /** Hides keys in text that is shown: `sk-phantom-****`, and an Anthropic key the user had in a file setup replaced: `sk-ant-****`. */
5
- export declare function maskKeys(text: string): string;
6
- /**
7
- * A line diff of two texts: unchanged lines start with two spaces, removed
8
- * lines with `- ` and added lines with `+ `. Unchanged lines more than two
9
- * away from a change are left out, and each gap shows as ` ...`.
10
- */
11
- export declare function lineDiff(before: string | null, after: string | null): string;
12
- /** Whether setup has written anything on this computer that undo could put back: it keeps a record of each file. */
13
- export declare function isSetUp(env: Env): boolean;
14
- /**
15
- * Writes a plan from {@link planSetup}. Before it edits a file for the first time it copies it
16
- * to `<file>.phantom.bak`. That copy stays as it is on every later run, even when the user
17
- * changed the file in between: a copy made then would already hold phantom's lines.
18
- * A file the user changed since phantom last wrote it is marked `edited`, so
19
- * {@link restoreSetup} takes out only phantom's lines. A file that changed after the
20
- * plan read it is left alone and listed in `skipped`.
21
- */
22
- export declare function applySetup(env: Env, plan: SetupPlan): SetupResult;
23
- /**
24
- * Points Claude Code at Phantom AI, or removes phantom's settings with `off`.
25
- * Uses the same backups and restore safeguards as {@link applySetup}.
26
- * The return value remains the provider result for existing callers.
27
- * @deprecated Use {@link setupAgents} or {@link planSetup} and {@link applySetup}. This helper remains for settings-only callers.
28
- */
29
- export declare function claudeProvider(env: Env, opts?: {
30
- off?: boolean;
31
- model?: string;
32
- onPath?: (bin: string) => boolean;
33
- }): ProviderResult;
34
- /** A plan as `--dry-run` reports it: each file that would change, with its diff, and nothing written. */
35
- export declare function previewSetup(plan: SetupPlan): SetupResult;
36
- /**
37
- * Installs the phantom-ai skill for one agent, or for every agent found
38
- * (see {@link SETUP_AGENTS}), and with `provider` sets that up
39
- * too, without asking: {@link planSetup}, then {@link applySetup}.
40
- * @throws {@link CliError} when no agent is found, the agent is unknown, or `provider` is set without an agent that supports it.
41
- */
42
- export declare function setupAgents(env: Env, opts?: SetupOptions): SetupResult;
43
- /** What {@link restoreSetup} did. */
44
- export type RestoreResult = {
45
- /** Files put back from their copy, removed because phantom created them, or cleaned of phantom's own lines. */
46
- restored: Array<{
47
- /** The file. */
48
- file: string;
49
- /** The copy it came from. null when phantom created the file, so it was removed, or when only phantom's lines were taken out. */
50
- from: string | null;
51
- /**
52
- * Present when the file had changed since setup, so restore took out only phantom's own lines and keys
53
- * and left the rest. false means none were left to take out.
54
- */
55
- lines_removed?: boolean;
56
- /** With `lines_removed`: the copy from before setup, which restore left in place in case the user wants it. */
57
- copy_kept?: string;
58
- }>;
59
- /** Files that could not be undone, and why. Each one makes the exit code 1. */
60
- skipped: Array<{
61
- /** The file. */
62
- file: string;
63
- /** Why. */
64
- reason: string;
65
- }>;
66
- /** True with `--dry-run`: nothing was written. */
67
- dry_run?: boolean;
68
- };
69
- /**
70
- * Undoes `phantom setup`: puts back the files it changed from the copies it kept
71
- * (`<file>.phantom.bak`), and removes the files it created.
72
- *
73
- * A file that changed since setup (the user edited it, or Codex added `[projects]` tables to its
74
- * config) would lose those changes if the copy went over it. For such a file restore takes out only
75
- * phantom's own lines and keys: JSON keys go back to the values setup recorded, TOML and YAML lines
76
- * marked `# phantom setup` go (a replaced line comes back), and the shell block goes. The copy stays
77
- * beside the file. A file left with nothing goes, or comes back from its copy.
78
- * A file whose lines can't be told apart (it no longer parses) is listed in `skipped`, and only
79
- * then does the exit code say something was left undone.
80
- * @param opts `agent` limits it to one agent's files (`shell` for the startup file).
81
- */
82
- export declare function restoreSetup(env: Env, opts?: {
83
- agent?: string;
84
- dryRun?: boolean;
85
- }): RestoreResult;
@@ -1,301 +0,0 @@
1
- import os from 'node:os';
2
- import path from 'node:path';
3
- import { createHash } from 'node:crypto';
4
- import { chmodSync, existsSync, mkdirSync, readFileSync, rmdirSync, rmSync, writeFileSync } from 'node:fs';
5
- import { stateDir } from "../core/state.mjs";
6
- import { readText } from "./shared.mjs";
7
- import { CliError } from "../core/errors.mjs";
8
- import { jsonText, shellRcFile, withoutShellBlock } from "./editors.mjs";
9
- import { claudePlan, SETUP_AGENTS } from "./agents.mjs";
10
- import { writeJsonWas } from "./was.mjs";
11
- import { planSetup } from "./plan.mjs";
12
- /** Hides keys in text that is shown: `sk-phantom-****`, and an Anthropic key the user had in a file setup replaced: `sk-ant-****`. */
13
- export function maskKeys(text) {
14
- return text.replace(/sk-phantom-[A-Za-z0-9_-]+/g, 'sk-phantom-****').replace(/sk-ant-[A-Za-z0-9_-]+/g, 'sk-ant-****');
15
- }
16
- /**
17
- * A line diff of two texts: unchanged lines start with two spaces, removed
18
- * lines with `- ` and added lines with `+ `. Unchanged lines more than two
19
- * away from a change are left out, and each gap shows as ` ...`.
20
- */
21
- export function lineDiff(before, after) {
22
- const split = (t) => (t === null || t === '' ? [] : t.replace(/\n$/, '').split('\n'));
23
- const a = split(before);
24
- const b = split(after);
25
- const rows = [];
26
- if (a.length * b.length > 4_000_000) {
27
- rows.push(...a.map((l) => '- ' + l), ...b.map((l) => '+ ' + l));
28
- }
29
- else {
30
- // Longest common subsequence, from the end, then walk it from the start.
31
- const dp = Array.from({ length: a.length + 1 }, () => new Uint32Array(b.length + 1));
32
- for (let i = a.length - 1; i >= 0; i--)
33
- for (let j = b.length - 1; j >= 0; j--)
34
- dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
35
- let i = 0;
36
- let j = 0;
37
- while (i < a.length || j < b.length) {
38
- if (i < a.length && j < b.length && a[i] === b[j]) {
39
- rows.push(' ' + a[i++]);
40
- j++;
41
- }
42
- else if (i < a.length && (j === b.length || dp[i + 1][j] >= dp[i][j + 1]))
43
- rows.push('- ' + a[i++]);
44
- else
45
- rows.push('+ ' + b[j++]);
46
- }
47
- }
48
- const changed = rows.map((r) => !r.startsWith(' '));
49
- const out = [];
50
- for (let k = 0; k < rows.length; k++) {
51
- if (changed.slice(Math.max(0, k - 2), k + 3).some(Boolean))
52
- out.push(rows[k]);
53
- else if (out[out.length - 1] !== ' ...')
54
- out.push(' ...');
55
- }
56
- return out.join('\n');
57
- }
58
- function backupsFile(env) {
59
- return path.join(stateDir(env), 'setup-backups.json');
60
- }
61
- function readBackups(env) {
62
- try {
63
- return JSON.parse(readFileSync(backupsFile(env), 'utf-8'));
64
- }
65
- catch {
66
- return {};
67
- }
68
- }
69
- function writeBackups(env, entries) {
70
- mkdirSync(stateDir(env), { recursive: true, mode: 0o700 });
71
- writeFileSync(backupsFile(env), jsonText(entries), { mode: 0o600 });
72
- }
73
- /** Whether setup has written anything on this computer that undo could put back: it keeps a record of each file. */
74
- export function isSetUp(env) {
75
- return Object.keys(readBackups(env)).length > 0;
76
- }
77
- const sha256 = (text) => createHash('sha256').update(text).digest('hex');
78
- /**
79
- * Writes a plan from {@link planSetup}. Before it edits a file for the first time it copies it
80
- * to `<file>.phantom.bak`. That copy stays as it is on every later run, even when the user
81
- * changed the file in between: a copy made then would already hold phantom's lines.
82
- * A file the user changed since phantom last wrote it is marked `edited`, so
83
- * {@link restoreSetup} takes out only phantom's lines. A file that changed after the
84
- * plan read it is left alone and listed in `skipped`.
85
- */
86
- export function applySetup(env, plan) {
87
- const backups = readBackups(env);
88
- const skipped = [...plan.skipped];
89
- const files = [];
90
- for (const f of plan.files) {
91
- if (readText(f.file) !== f.before) {
92
- skipped.push({ agent: f.agent, reason: `${f.file} changed after setup read it; run phantom setup again` });
93
- continue;
94
- }
95
- let backup;
96
- if (!f.skill) {
97
- const prior = backups[f.file];
98
- // The user changed the file since phantom last wrote it (or deleted it, or the copy is gone).
99
- const edited = Boolean(prior && (prior.edited || prior.sha !== sha256(f.before ?? '') || (prior.backup !== null && !existsSync(prior.backup))));
100
- let from;
101
- if (prior)
102
- from = prior.backup;
103
- else if (f.before !== null) {
104
- from = `${f.file}.phantom.bak`;
105
- writeFileSync(from, f.before, { mode: 0o600 });
106
- }
107
- else
108
- from = null;
109
- let standardCopy = Boolean(prior?.standard_copy);
110
- if (f.before !== null && (f.agent === 'claude' || f.file.endsWith('settings.json'))) {
111
- const standardBak = `${f.file}.bak`;
112
- if (!existsSync(standardBak)) {
113
- const content = from && existsSync(from) ? readFileSync(from, 'utf-8') : f.before;
114
- writeFileSync(standardBak, content, { mode: 0o600 });
115
- standardCopy = true;
116
- }
117
- }
118
- if (from && existsSync(from))
119
- backup = from;
120
- if (f.after === null && from === null)
121
- delete backups[f.file];
122
- else
123
- backups[f.file] = { agent: f.agent, backup: from, sha: sha256(f.after ?? ''), ...(edited ? { edited } : {}), ...(standardCopy ? { standard_copy: true } : {}) };
124
- }
125
- if (f.after === null)
126
- rmSync(f.file, { force: true });
127
- else {
128
- mkdirSync(path.dirname(f.file), { recursive: true });
129
- writeFileSync(f.file, f.after, f.mode ? { mode: f.mode } : {});
130
- if (f.mode)
131
- chmodSync(f.file, f.mode);
132
- }
133
- if (f.was !== undefined)
134
- writeJsonWas(env, f.file, f.after === null ? null : f.was);
135
- files.push({ agent: f.agent, file: f.file, action: f.action, ...(backup ? { backup } : {}), ...(f.skill ? {} : { diff: maskKeys(lineDiff(f.before, f.after)) }) });
136
- }
137
- if (plan.files.some((f) => !f.skill))
138
- writeBackups(env, backups);
139
- return { agents: plan.agents, files, ...(skipped.length ? { skipped } : {}), ...(plan.notes.length ? { notes: plan.notes } : {}) };
140
- }
141
- /**
142
- * Points Claude Code at Phantom AI, or removes phantom's settings with `off`.
143
- * Uses the same backups and restore safeguards as {@link applySetup}.
144
- * The return value remains the provider result for existing callers.
145
- * @deprecated Use {@link setupAgents} or {@link planSetup} and {@link applySetup}. This helper remains for settings-only callers.
146
- */
147
- export function claudeProvider(env, opts = {}) {
148
- const provider = claudePlan(env, opts);
149
- const files = provider.edits.map((edit) => {
150
- const before = readText(edit.file);
151
- return { ...edit, before, agent: 'claude', action: before === null ? 'create' : edit.after === null ? 'remove' : 'update' };
152
- });
153
- applySetup(env, { agents: [], files, skipped: [], notes: [] });
154
- return provider.result;
155
- }
156
- /** A plan as `--dry-run` reports it: each file that would change, with its diff, and nothing written. */
157
- export function previewSetup(plan) {
158
- return {
159
- agents: plan.agents,
160
- files: plan.files.map((f) => ({ agent: f.agent, file: f.file, action: f.action, ...(f.skill ? {} : { diff: maskKeys(lineDiff(f.before, f.after)) }) })),
161
- ...(plan.skipped.length ? { skipped: plan.skipped } : {}),
162
- ...(plan.notes.length ? { notes: plan.notes } : {}),
163
- dry_run: true,
164
- };
165
- }
166
- /**
167
- * Installs the phantom-ai skill for one agent, or for every agent found
168
- * (see {@link SETUP_AGENTS}), and with `provider` sets that up
169
- * too, without asking: {@link planSetup}, then {@link applySetup}.
170
- * @throws {@link CliError} when no agent is found, the agent is unknown, or `provider` is set without an agent that supports it.
171
- */
172
- export function setupAgents(env, opts = {}) {
173
- return applySetup(env, planSetup(env, opts));
174
- }
175
- /**
176
- * Removes the `<file>.bak` setup made, when it is still the copy setup wrote. One the user changed since, or one that
177
- * was there before setup, stays. `original` is the copy from before setup, when it is still there.
178
- */
179
- function dropStandardCopy(file, entry, original) {
180
- const copy = `${file}.bak`;
181
- if (!entry.standard_copy || !existsSync(copy))
182
- return;
183
- if (original !== null && readText(copy) !== original)
184
- return;
185
- rmSync(copy, { force: true });
186
- }
187
- /**
188
- * What a file holds once phantom's own lines and keys are taken out, for a file that changed since setup
189
- * (or whose copy is gone): the agent's own `--provider off` step, which uses the values setup recorded
190
- * (`setup-json-was.json`) and the `# phantom setup` marks, and for the shell file the phantom block.
191
- * `after` is null or empty when nothing is left. `skip` says why the lines can't be taken out.
192
- */
193
- function withoutPhantomLines(env, file, agent, now) {
194
- try {
195
- if (agent === 'shell') {
196
- if (now === null)
197
- return { after: null, changed: false };
198
- const after = withoutShellBlock(env, file, now);
199
- return { after, changed: after !== now };
200
- }
201
- const provider = SETUP_AGENTS[agent]?.provider;
202
- if (!provider)
203
- return { skip: `phantom can't tell which lines in it are its own. Remove the lines ending "# phantom setup" by hand` };
204
- const home = env.HOME || os.homedir();
205
- const plan = provider({ env, home, shellRc: shellRcFile(env, home) }, true);
206
- const edit = plan.edits.find((e) => e.file === file);
207
- if (edit)
208
- return { after: edit.after, changed: true };
209
- // The agent's off step found nothing of phantom's in the file it looks at, and that is this file.
210
- if (plan.result.settings === file)
211
- return { after: now, changed: false };
212
- return { skip: `phantom looks for this agent's settings in ${plan.result.settings} now. Remove the lines ending "# phantom setup" from it by hand` };
213
- }
214
- catch (err) {
215
- if (err instanceof CliError && err.code === 'setup_unreadable')
216
- return { skip: err.message };
217
- throw err;
218
- }
219
- }
220
- /**
221
- * Undoes `phantom setup`: puts back the files it changed from the copies it kept
222
- * (`<file>.phantom.bak`), and removes the files it created.
223
- *
224
- * A file that changed since setup (the user edited it, or Codex added `[projects]` tables to its
225
- * config) would lose those changes if the copy went over it. For such a file restore takes out only
226
- * phantom's own lines and keys: JSON keys go back to the values setup recorded, TOML and YAML lines
227
- * marked `# phantom setup` go (a replaced line comes back), and the shell block goes. The copy stays
228
- * beside the file. A file left with nothing goes, or comes back from its copy.
229
- * A file whose lines can't be told apart (it no longer parses) is listed in `skipped`, and only
230
- * then does the exit code say something was left undone.
231
- * @param opts `agent` limits it to one agent's files (`shell` for the startup file).
232
- */
233
- export function restoreSetup(env, opts = {}) {
234
- const backups = readBackups(env);
235
- const restored = [];
236
- const skipped = [];
237
- for (const [file, entry] of Object.entries(backups)) {
238
- if (opts.agent && opts.agent !== 'all' && entry.agent !== opts.agent)
239
- continue;
240
- const now = readText(file);
241
- const copyThere = entry.backup !== null && existsSync(entry.backup);
242
- const asPhantomWroteIt = !entry.edited && sha256(now ?? '') === entry.sha && (entry.backup === null || copyThere);
243
- if (asPhantomWroteIt) {
244
- if (!opts.dryRun) {
245
- if (entry.backup === null)
246
- rmSync(file, { force: true });
247
- else {
248
- dropStandardCopy(file, entry, readText(entry.backup));
249
- writeFileSync(file, readFileSync(entry.backup));
250
- rmSync(entry.backup);
251
- }
252
- delete backups[file];
253
- // The file is back as it was before setup, so what setup recorded about its keys no longer applies.
254
- writeJsonWas(env, file, null);
255
- }
256
- restored.push({ file, from: entry.backup });
257
- continue;
258
- }
259
- const cleaned = withoutPhantomLines(env, file, entry.agent, now);
260
- if ('skip' in cleaned) {
261
- skipped.push({ file, reason: cleaned.skip });
262
- continue;
263
- }
264
- if (!opts.dryRun) {
265
- dropStandardCopy(file, entry, copyThere ? readText(entry.backup) : null);
266
- const empty = cleaned.after === null || cleaned.after.trim() === '';
267
- if (empty && copyThere)
268
- writeFileSync(file, readFileSync(entry.backup));
269
- else if (empty)
270
- rmSync(file, { force: true });
271
- else if (cleaned.changed)
272
- writeFileSync(file, cleaned.after);
273
- delete backups[file];
274
- writeJsonWas(env, file, null);
275
- }
276
- restored.push({ file, from: null, lines_removed: cleaned.changed, ...(copyThere ? { copy_kept: entry.backup } : {}) });
277
- }
278
- if (!opts.dryRun && restored.length)
279
- writeBackups(env, backups);
280
- // The phantom-ai skill is phantom's own file, kept out of the backups, so undo removes it here.
281
- const home = env.HOME || os.homedir();
282
- for (const [agent, spec] of Object.entries(SETUP_AGENTS)) {
283
- if (!spec.skills || (opts.agent && opts.agent !== 'all' && agent !== opts.agent))
284
- continue;
285
- const skill = path.join(home, spec.skills, 'phantom-ai', 'SKILL.md');
286
- // Two agents can share a skills folder (pi and Codex use ~/.agents/skills), so each file is listed once.
287
- if (!existsSync(skill) || restored.some((r) => r.file === skill) || !/^name: phantom-ai$/m.test(readText(skill) ?? ''))
288
- continue;
289
- if (!opts.dryRun) {
290
- rmSync(skill, { force: true });
291
- try {
292
- rmdirSync(path.dirname(skill));
293
- }
294
- catch {
295
- // Not empty: something else lives there, so the folder stays.
296
- }
297
- }
298
- restored.push({ file: skill, from: null });
299
- }
300
- return { restored, skipped, ...(opts.dryRun ? { dry_run: true } : {}) };
301
- }
@@ -1,12 +0,0 @@
1
- import type { Env } from '../core/state.mts';
2
- import { type CommandInput } from '../core/helpers.mts';
3
- import { type ScreenOptions } from './screen.mts';
4
- /** `--table` output for `setup`, and what a terminal shows: see {@link setupScreen}. */
5
- export declare function tableSetup(d: unknown, opts?: ScreenOptions): string;
6
- /** `--table` output for `setup --restore`, and what a terminal shows: a ✓ per file put back, a ✗ per file left alone. */
7
- export declare function tableRestore(d: unknown, opts?: {
8
- env?: Env;
9
- color?: boolean;
10
- }): string;
11
- /** `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()`. */
12
- export declare function setupCommand(input: CommandInput): Promise<number>;