@thegitai/cli 1.0.0-preview.9 → 1.0.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 (99) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +301 -15
  3. package/dist/bin/ai.js +139 -111
  4. package/dist/bin/browser-host.js +284 -0
  5. package/dist/src/agent-mode.js +33 -7
  6. package/dist/src/api/auth.js +8 -2
  7. package/dist/src/api/browser-login.js +77 -34
  8. package/dist/src/api/chat.js +700 -173
  9. package/dist/src/api/contracts.js +55 -1
  10. package/dist/src/api/default-host.js +1 -0
  11. package/dist/src/api/http.js +168 -8
  12. package/dist/src/api/models.js +47 -9
  13. package/dist/src/api/startup-handshake.js +36 -0
  14. package/dist/src/api/turn-recovery.js +189 -0
  15. package/dist/src/background-jobs.js +8 -2
  16. package/dist/src/browser/bridge.js +268 -0
  17. package/dist/src/browser/chrome-profiles.js +73 -0
  18. package/dist/src/browser/framing.js +42 -0
  19. package/dist/src/browser/native-host.js +219 -0
  20. package/dist/src/browser/protocol.js +46 -0
  21. package/dist/src/browser/session-bridge.js +130 -0
  22. package/dist/src/cli-args.js +19 -5
  23. package/dist/src/client-environment.js +2 -0
  24. package/dist/src/core/clipboard.js +8 -14
  25. package/dist/src/core/image-limits.js +56 -0
  26. package/dist/src/core/image-path-extractor.js +70 -3
  27. package/dist/src/core/session-image-store.js +246 -0
  28. package/dist/src/edit-journal.js +33 -2
  29. package/dist/src/executor.js +26 -4
  30. package/dist/src/help-text.js +152 -41
  31. package/dist/src/install-id.js +32 -0
  32. package/dist/src/job-follow-up.js +40 -0
  33. package/dist/src/mcp-client.js +335 -0
  34. package/dist/src/mcp-command.js +176 -0
  35. package/dist/src/mcp-config.js +123 -0
  36. package/dist/src/mcp-sdk.js +3 -0
  37. package/dist/src/mcp-types.js +26 -0
  38. package/dist/src/patcher.js +46 -4
  39. package/dist/src/permissions.js +301 -0
  40. package/dist/src/preferences.js +39 -0
  41. package/dist/src/project-key.js +11 -0
  42. package/dist/src/project-model-preferences.js +74 -0
  43. package/dist/src/quota-notice.js +62 -0
  44. package/dist/src/reasoning-effort.js +27 -0
  45. package/dist/src/session-safety.js +0 -12
  46. package/dist/src/session-store.js +71 -10
  47. package/dist/src/session.js +12 -4
  48. package/dist/src/signin.js +58 -0
  49. package/dist/src/skills/discovery.js +280 -0
  50. package/dist/src/skills/frontmatter.js +192 -0
  51. package/dist/src/skills/install.js +394 -0
  52. package/dist/src/skills/types.js +43 -0
  53. package/dist/src/skills-command.js +71 -0
  54. package/dist/src/tool-executor.js +41 -51
  55. package/dist/src/tools/browser.js +928 -0
  56. package/dist/src/tools/delete-file.js +21 -5
  57. package/dist/src/tools/index.js +34 -10
  58. package/dist/src/tools/patch-file.js +14 -29
  59. package/dist/src/tools/read-file.js +7 -1
  60. package/dist/src/tools/read-image-file.js +85 -0
  61. package/dist/src/tools/replace-document-text.js +28 -18
  62. package/dist/src/tools/restore-checkpoint.js +1 -2
  63. package/dist/src/tools/run-command.js +14 -71
  64. package/dist/src/tools/run-node-script.js +12 -81
  65. package/dist/src/tools/save-generated-image.js +120 -0
  66. package/dist/src/tools/shell-diagnostics.js +246 -72
  67. package/dist/src/tools/str-replace.js +14 -29
  68. package/dist/src/tools/undo-edit.js +2 -7
  69. package/dist/src/tools/use-skill.js +38 -0
  70. package/dist/src/tools/write-file.js +69 -14
  71. package/dist/src/ui/repl.js +1141 -299
  72. package/dist/src/ui/slash-commands.js +140 -0
  73. package/dist/src/ui/tui/attention.js +25 -0
  74. package/dist/src/ui/tui/bridge.js +38 -5
  75. package/dist/src/ui/tui/build-frame.js +858 -303
  76. package/dist/src/ui/tui/composer-layout.js +76 -0
  77. package/dist/src/ui/tui/desktop-notification.js +118 -0
  78. package/dist/src/ui/tui/markdown-render.js +21 -11
  79. package/dist/src/ui/tui/model-label.js +13 -0
  80. package/dist/src/ui/tui/shell-input.js +266 -61
  81. package/dist/src/ui/tui/shortcuts.js +61 -0
  82. package/dist/src/ui/tui/terminal-theme.js +28 -0
  83. package/dist/src/ui/tui/text.js +31 -6
  84. package/dist/src/ui/tui/user-input.js +593 -0
  85. package/dist/src/update-check.js +205 -0
  86. package/dist/src/usage-notice.js +52 -0
  87. package/dist/src/usage.js +25 -2
  88. package/dist/src/utils.js +4 -0
  89. package/dist/src/version.js +52 -2
  90. package/dist/vendor/mcp/LICENSES.txt +414 -0
  91. package/dist/vendor/mcp/client.cjs +82 -0
  92. package/package.json +22 -9
  93. package/dist/src/project-index.js +0 -233
  94. package/dist/src/tools/code-intel.js +0 -472
  95. package/dist/src/tools/find-symbol.js +0 -70
  96. package/dist/src/tools/hover-symbol.js +0 -95
  97. package/dist/src/tools/list-symbols.js +0 -55
  98. package/dist/src/tools/search-code.js +0 -37
  99. package/dist/src/tools/signature-help.js +0 -118
@@ -0,0 +1,205 @@
1
+ import { spawn } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import { stdin as input, stdout as output } from 'node:process';
4
+ import { emitKeypressEvents } from 'node:readline';
5
+ import readline from 'node:readline/promises';
6
+ import { fileURLToPath } from 'node:url';
7
+ import chalk from './colors.js';
8
+ import { isAbortError } from './utils.js';
9
+ import { compareCliVersions, UNKNOWN_CLI_VERSION } from './version.js';
10
+ const PACKAGE_NAME = '@thegitai/cli';
11
+ export function resolveInstallCommand(version, moduleDir = path.dirname(fileURLToPath(import.meta.url))) {
12
+ const spec = `${PACKAGE_NAME}@${version}`;
13
+ const location = moduleDir.replace(/\\/g, '/').toLowerCase();
14
+ if (location.includes('/pnpm/global/')) {
15
+ return { program: 'pnpm', args: ['add', '-g', spec] };
16
+ }
17
+ if (location.includes('/.bun/install/global/')) {
18
+ return { program: 'bun', args: ['add', '-g', spec] };
19
+ }
20
+ return { program: 'npm', args: ['install', '-g', spec] };
21
+ }
22
+ export function buildUpdateOffer({ currentVersion, latestVersion, moduleDir, }) {
23
+ if (!latestVersion)
24
+ return null;
25
+ if (currentVersion === UNKNOWN_CLI_VERSION)
26
+ return null;
27
+ const ordering = compareCliVersions(currentVersion, latestVersion);
28
+ if (ordering === null || ordering >= 0)
29
+ return null;
30
+ const { program, args } = resolveInstallCommand(latestVersion, moduleDir);
31
+ return {
32
+ currentVersion,
33
+ latestVersion,
34
+ program,
35
+ args,
36
+ display: [program, ...args].join(' '),
37
+ };
38
+ }
39
+ export const UPDATE_OPTIONS = ['update', 'skip'];
40
+ const UPDATE_KEY_HINT = '↑/↓ choose • Enter accepts • Ctrl+C quits';
41
+ function formatUpdateHeader(offer) {
42
+ return [
43
+ '',
44
+ ` ${chalk.yellow('✨ Update available')} - ${chalk.dim(offer.currentVersion)} → ${chalk.bold(offer.latestVersion)}`,
45
+ '',
46
+ ' TheGitAI works best with the latest version. TheGitAI may not work',
47
+ ' optimally if not updated.',
48
+ '',
49
+ ].join('\n');
50
+ }
51
+ export function formatUpdateOptions(offer, selectedIndex = 0) {
52
+ const rows = [
53
+ { note: `(runs \`${offer.display}\`)`, text: 'Update now' },
54
+ { note: '(Agent may not work optimally)', text: 'Skip' },
55
+ ];
56
+ return [
57
+ ...rows.map(({ note, text }, index) => index === selectedIndex
58
+ ? ` ${chalk.cyan(`› ${index + 1}. ${text}`)} ${chalk.dim(note)}`
59
+ : ` ${chalk.dim(`${index + 1}.`)} ${text} ${chalk.dim(note)}`),
60
+ '',
61
+ ` ${chalk.dim(UPDATE_KEY_HINT)}`,
62
+ '',
63
+ ].join('\n');
64
+ }
65
+ export function formatUpdateScreen(offer, selectedIndex = 0) {
66
+ return `${formatUpdateHeader(offer)}\n${formatUpdateOptions(offer, selectedIndex)}`;
67
+ }
68
+ export function readUpdateKey(key, selectedIndex) {
69
+ const name = key.name ?? '';
70
+ if (key.ctrl && (name === 'c' || name === 'd')) {
71
+ return { kind: 'answer', answer: 'cancel' };
72
+ }
73
+ if (name === 'return' || name === 'enter') {
74
+ return { kind: 'answer', answer: UPDATE_OPTIONS[selectedIndex] ?? 'update' };
75
+ }
76
+ if (name === 'up' || (name === 'tab' && key.sequence === '\u001b[Z')) {
77
+ return { kind: 'select', index: Math.max(selectedIndex - 1, 0) };
78
+ }
79
+ if (name === 'down' || name === 'tab') {
80
+ return { kind: 'select', index: Math.min(selectedIndex + 1, UPDATE_OPTIONS.length - 1) };
81
+ }
82
+ const digit = Number.parseInt(key.sequence ?? '', 10);
83
+ if (Number.isInteger(digit) && digit >= 1 && digit <= UPDATE_OPTIONS.length) {
84
+ return { kind: 'answer', answer: UPDATE_OPTIONS[digit - 1] };
85
+ }
86
+ return { kind: 'ignore' };
87
+ }
88
+ export function readUpdateChoice(answer) {
89
+ const value = answer.trim().toLowerCase();
90
+ if (value === '' || value === '1' || value === 'y' || value === 'yes') {
91
+ return 'update';
92
+ }
93
+ return 'skip';
94
+ }
95
+ export async function resolveUpdateAnswer(question) {
96
+ try {
97
+ return readUpdateChoice(await question());
98
+ }
99
+ catch (error) {
100
+ if (isAbortError(error))
101
+ return 'cancel';
102
+ throw error;
103
+ }
104
+ }
105
+ export function renderedRowCount(text, columns) {
106
+ const width = Math.max(1, columns);
107
+ const lines = text.split('\n');
108
+ if (lines[lines.length - 1] === '')
109
+ lines.pop();
110
+ return lines.reduce((rows, line) => {
111
+ const visible = [...line.replace(/\x1b\[[0-9;]*m/g, '')].length;
112
+ return rows + Math.max(1, Math.ceil(visible / width));
113
+ }, 0);
114
+ }
115
+ async function askUpdateChoiceWithKeys(offer) {
116
+ let selected = 0;
117
+ const show = (index) => {
118
+ const rows = renderedRowCount(`${formatUpdateOptions(offer, selected)}\n`, output.columns ?? 80);
119
+ selected = index;
120
+ output.write(`\u001b[${rows}A\u001b[0J${formatUpdateOptions(offer, selected)}\n`);
121
+ };
122
+ emitKeypressEvents(input);
123
+ const wasRaw = input.isRaw === true;
124
+ input.setRawMode(true);
125
+ input.resume();
126
+ try {
127
+ return await new Promise((resolve) => {
128
+ const onKeypress = (_text, key) => {
129
+ const action = readUpdateKey(key ?? {}, selected);
130
+ if (action.kind === 'ignore')
131
+ return;
132
+ if (action.kind === 'select') {
133
+ if (action.index !== selected)
134
+ show(action.index);
135
+ return;
136
+ }
137
+ const chosen = UPDATE_OPTIONS.indexOf(action.answer);
138
+ if (chosen >= 0 && chosen !== selected)
139
+ show(chosen);
140
+ input.off('keypress', onKeypress);
141
+ resolve(action.answer);
142
+ };
143
+ input.on('keypress', onKeypress);
144
+ });
145
+ }
146
+ finally {
147
+ if (!wasRaw)
148
+ input.setRawMode(false);
149
+ input.pause();
150
+ }
151
+ }
152
+ async function askUpdateChoice(offer) {
153
+ if (input.isTTY && output.isTTY)
154
+ return await askUpdateChoiceWithKeys(offer);
155
+ const rl = readline.createInterface({ input, output });
156
+ try {
157
+ return await resolveUpdateAnswer(() => rl.question(' ▸ '));
158
+ }
159
+ finally {
160
+ rl.close();
161
+ }
162
+ }
163
+ export async function runUpdate(offer) {
164
+ return await new Promise((resolve) => {
165
+ const child = spawn(offer.program, offer.args, {
166
+ stdio: 'inherit',
167
+ shell: process.platform === 'win32',
168
+ });
169
+ child.on('error', () => resolve(false));
170
+ child.on('close', (code) => resolve(code === 0));
171
+ });
172
+ }
173
+ export async function offerUpdateAtStartup({ currentVersion, latestVersion, ask = askUpdateChoice, install = runUpdate, write = (text) => {
174
+ process.stdout.write(text);
175
+ }, }) {
176
+ const offer = buildUpdateOffer({ currentVersion, latestVersion });
177
+ if (!offer)
178
+ return 'none';
179
+ write(`${formatUpdateScreen(offer)}\n`);
180
+ const answer = await ask(offer);
181
+ if (answer === 'cancel') {
182
+ write(`\n ${chalk.dim('Cancelled. Nothing was installed.')}\n\n`);
183
+ return 'cancelled';
184
+ }
185
+ if (answer === 'skip') {
186
+ write(`\n ${chalk.yellow(`Continuing on ${offer.currentVersion} - you'll be asked again next time.`)}\n\n`);
187
+ return 'skipped';
188
+ }
189
+ write(`\n ${chalk.dim(`$ ${offer.display}`)}\n\n`);
190
+ if (await install(offer)) {
191
+ write(`\n ${chalk.green(`✓ Updated to ${offer.latestVersion}.`)} Run ${chalk.bold('ai')} again to start.\n\n`);
192
+ return 'updated';
193
+ }
194
+ write([
195
+ '',
196
+ ` ${chalk.red('✖ The update did not complete.')} Run this yourself, then start ${chalk.bold('ai')} again:`,
197
+ '',
198
+ ` ${chalk.bold(offer.display)}`,
199
+ '',
200
+ ` ${chalk.dim(`Starting on ${offer.currentVersion} for now.`)}`,
201
+ '',
202
+ '',
203
+ ].join('\n'));
204
+ return 'failed';
205
+ }
@@ -0,0 +1,52 @@
1
+ import { USAGE_WINDOWS } from './usage.js';
2
+ function resetDescription(resetsAt, now) {
3
+ const minutes = Math.max(1, Math.ceil((resetsAt - now) / 60_000));
4
+ const hours = Math.floor(minutes / 60);
5
+ const days = Math.floor(hours / 24);
6
+ const remaining = days > 0
7
+ ? `${days}d ${hours % 24}h`
8
+ : hours > 0
9
+ ? `${hours}h ${minutes % 60}m`
10
+ : `${minutes}m`;
11
+ const time = new Intl.DateTimeFormat(undefined, {
12
+ month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', timeZoneName: 'short',
13
+ }).format(resetsAt);
14
+ return `Usage starts resetting in ${remaining}, at ${time}.`;
15
+ }
16
+ export function createUsageNoticeTracker() {
17
+ const shown = new Map();
18
+ return (usage, now = Date.now()) => {
19
+ let urgent = null;
20
+ for (const [label, window] of USAGE_WINDOWS) {
21
+ const left = usage?.percentLeft?.[window];
22
+ if (typeof left !== 'number' || !Number.isFinite(left) || left < 0 || left > 100)
23
+ continue;
24
+ const used = 100 - left;
25
+ if (used < 75)
26
+ shown.delete(window);
27
+ else if (used < 90 && shown.get(window) === 95)
28
+ shown.set(window, 80);
29
+ const threshold = used >= 95 ? 95 : used >= 80 ? 80 : 0;
30
+ if (!threshold)
31
+ continue;
32
+ const reset = usage?.resetsAt?.[window];
33
+ const resetsAt = typeof reset === 'string' ? Date.parse(reset) : NaN;
34
+ if (!Number.isFinite(resetsAt) || resetsAt <= now)
35
+ continue;
36
+ if (!urgent || used > urgent.used)
37
+ urgent = { label, window, used, threshold, resetsAt };
38
+ }
39
+ if (!urgent || urgent.threshold <= Math.max(0, ...shown.values()))
40
+ return null;
41
+ shown.set(urgent.window, urgent.threshold);
42
+ const percent = urgent.used === 100 ? 'over 99%' : `${Math.floor(urgent.used)}%`;
43
+ return {
44
+ title: 'Usage notice',
45
+ body: [
46
+ `You've used ${percent} of your ${urgent.label.toLowerCase()} allowance.`,
47
+ resetDescription(urgent.resetsAt, now),
48
+ 'Run /usage for details.',
49
+ ].join('\n'),
50
+ };
51
+ };
52
+ }
package/dist/src/usage.js CHANGED
@@ -1,9 +1,12 @@
1
- const USAGE_WINDOWS = [
1
+ export const USAGE_WINDOWS = [
2
2
  ['5-hour', '5-hourly'],
3
3
  ['Weekly', 'weekly'],
4
4
  ['Monthly', 'monthly'],
5
5
  ];
6
- function formatUtcMinute(value) {
6
+ export function usageWindowLabel(key) {
7
+ return USAGE_WINDOWS.find(([, id]) => id === key)?.[0] ?? '';
8
+ }
9
+ export function formatUtcMinute(value) {
7
10
  if (!value)
8
11
  return 'no active usage';
9
12
  const date = new Date(value);
@@ -31,6 +34,11 @@ function formatPercentLeft(value) {
31
34
  maximumFractionDigits: 1,
32
35
  }).format(percent)}% left`;
33
36
  }
37
+ const PLAN_NAMES = {
38
+ free: 'Free',
39
+ plus: 'Plus',
40
+ pro: 'Pro',
41
+ };
34
42
  export function formatUsageText(response) {
35
43
  const { customer, usage } = response;
36
44
  const lines = [`Usage for ${customer.email}`, ''];
@@ -38,10 +46,25 @@ export function formatUsageText(response) {
38
46
  lines.push('Usage is not available for this account yet.');
39
47
  return lines.join('\n');
40
48
  }
49
+ if (usage.plan) {
50
+ lines.push(`Plan: ${PLAN_NAMES[usage.plan] ?? usage.plan}`, '');
51
+ }
41
52
  lines.push('Window Left Resets');
42
53
  for (const [label, key] of USAGE_WINDOWS) {
43
54
  const value = formatPercentLeft(Number(usage.percentLeft[key] ?? 0));
44
55
  lines.push(`${label.padEnd(10)} ${value.padEnd(11)} ${formatUtcMinute(usage.resetsAt[key])}`);
45
56
  }
57
+ if (usage.autoOnly) {
58
+ const value = formatPercentLeft(Number(usage.autoPercentLeft ?? 0));
59
+ lines.push(`${'Auto extra'.padEnd(10)} ${value.padEnd(11)} ${formatUtcMinute(usage.resetsAt.monthly)}`, '', ...(usage.autoExtraSpent
60
+ ? [
61
+ 'Your monthly allowance and extra Auto usage are used up. Usage',
62
+ 'resumes when your plan renews.',
63
+ ]
64
+ : [
65
+ 'Your monthly allowance is used up. Auto options in /model still work',
66
+ 'on extra usage until it runs out or your plan renews.',
67
+ ]));
68
+ }
46
69
  return lines.join('\n');
47
70
  }
package/dist/src/utils.js CHANGED
@@ -1,3 +1,7 @@
1
+ export function isAbortError(error) {
2
+ const err = error;
3
+ return err?.name === 'AbortError' || err?.code === 'ABORT_ERR';
4
+ }
1
5
  export function cosineSimilarity(a, b) {
2
6
  let dot = 0;
3
7
  let magA = 0;
@@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  const PACKAGE_NAME = '@thegitai/cli';
5
- const UNKNOWN_VERSION = '0.0.0';
5
+ export const UNKNOWN_CLI_VERSION = '0.0.0';
6
6
  export function getCliVersion() {
7
7
  let dir = path.dirname(fileURLToPath(import.meta.url));
8
8
  for (let depth = 0; depth < 6; depth++) {
@@ -19,7 +19,7 @@ export function getCliVersion() {
19
19
  break;
20
20
  dir = parent;
21
21
  }
22
- return UNKNOWN_VERSION;
22
+ return UNKNOWN_CLI_VERSION;
23
23
  }
24
24
  export function getPlatformTag() {
25
25
  return `${process.platform}-${process.arch}`;
@@ -27,3 +27,53 @@ export function getPlatformTag() {
27
27
  export function formatVersionLine() {
28
28
  return `ai ${getCliVersion()} (${getPlatformTag()}, node ${process.version})`;
29
29
  }
30
+ const VERSION_PATTERN = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
31
+ function parseVersion(value) {
32
+ const match = VERSION_PATTERN.exec(String(value ?? '').trim());
33
+ if (!match)
34
+ return null;
35
+ return {
36
+ core: [Number(match[1]), Number(match[2]), Number(match[3])],
37
+ prerelease: match[4] ? match[4].split('.') : [],
38
+ };
39
+ }
40
+ function comparePrereleaseIdentifier(left, right) {
41
+ const leftNumeric = /^\d+$/.test(left);
42
+ const rightNumeric = /^\d+$/.test(right);
43
+ if (leftNumeric && rightNumeric) {
44
+ const a = Number(left);
45
+ const b = Number(right);
46
+ return a === b ? 0 : a < b ? -1 : 1;
47
+ }
48
+ if (leftNumeric !== rightNumeric)
49
+ return leftNumeric ? -1 : 1;
50
+ return left === right ? 0 : left < right ? -1 : 1;
51
+ }
52
+ export function isReadableCliVersion(value) {
53
+ return parseVersion(value) !== null;
54
+ }
55
+ export function compareCliVersions(left, right) {
56
+ const a = parseVersion(left);
57
+ const b = parseVersion(right);
58
+ if (!a || !b)
59
+ return null;
60
+ for (let index = 0; index < 3; index++) {
61
+ if (a.core[index] !== b.core[index]) {
62
+ return a.core[index] < b.core[index] ? -1 : 1;
63
+ }
64
+ }
65
+ if (!a.prerelease.length || !b.prerelease.length) {
66
+ if (a.prerelease.length === b.prerelease.length)
67
+ return 0;
68
+ return a.prerelease.length ? -1 : 1;
69
+ }
70
+ const shared = Math.min(a.prerelease.length, b.prerelease.length);
71
+ for (let index = 0; index < shared; index++) {
72
+ const ordering = comparePrereleaseIdentifier(a.prerelease[index], b.prerelease[index]);
73
+ if (ordering !== 0)
74
+ return ordering;
75
+ }
76
+ if (a.prerelease.length === b.prerelease.length)
77
+ return 0;
78
+ return a.prerelease.length < b.prerelease.length ? -1 : 1;
79
+ }