@thegitai/cli 1.0.0-preview.8 → 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 (100) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +301 -15
  3. package/dist/bin/ai.js +142 -112
  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 +147 -35
  8. package/dist/src/api/chat.js +703 -166
  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 +123 -30
  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/prompt-history-store.js +1 -1
  72. package/dist/src/ui/repl.js +1155 -301
  73. package/dist/src/ui/slash-commands.js +140 -0
  74. package/dist/src/ui/tui/attention.js +25 -0
  75. package/dist/src/ui/tui/bridge.js +38 -5
  76. package/dist/src/ui/tui/build-frame.js +903 -304
  77. package/dist/src/ui/tui/composer-layout.js +76 -0
  78. package/dist/src/ui/tui/desktop-notification.js +118 -0
  79. package/dist/src/ui/tui/markdown-render.js +21 -11
  80. package/dist/src/ui/tui/model-label.js +13 -0
  81. package/dist/src/ui/tui/shell-input.js +266 -61
  82. package/dist/src/ui/tui/shortcuts.js +61 -0
  83. package/dist/src/ui/tui/terminal-theme.js +28 -0
  84. package/dist/src/ui/tui/text.js +31 -6
  85. package/dist/src/ui/tui/user-input.js +593 -0
  86. package/dist/src/update-check.js +205 -0
  87. package/dist/src/usage-notice.js +52 -0
  88. package/dist/src/usage.js +25 -2
  89. package/dist/src/utils.js +13 -0
  90. package/dist/src/version.js +52 -2
  91. package/dist/vendor/mcp/LICENSES.txt +414 -0
  92. package/dist/vendor/mcp/client.cjs +82 -0
  93. package/package.json +22 -9
  94. package/dist/src/project-index.js +0 -233
  95. package/dist/src/tools/code-intel.js +0 -472
  96. package/dist/src/tools/find-symbol.js +0 -70
  97. package/dist/src/tools/hover-symbol.js +0 -95
  98. package/dist/src/tools/list-symbols.js +0 -55
  99. package/dist/src/tools/search-code.js +0 -37
  100. package/dist/src/tools/signature-help.js +0 -118
@@ -1,19 +1,25 @@
1
1
  export const AGENT_MODES = ['default', 'auto-accept', 'plan'];
2
2
  const PLAN_MODE_TOOL_NAMES = new Set([
3
- 'search_code',
4
3
  'list_files',
5
4
  'list_directories',
6
5
  'read_file',
7
6
  'grep_code',
8
- 'find_symbol',
9
- 'list_symbols',
10
- 'hover_symbol',
11
- 'signature_help',
12
7
  'read_document',
13
8
  'analyze_image',
14
9
  'run_command',
15
10
  'shell_job_output',
16
11
  'update_todos',
12
+ 'use_skill',
13
+ 'browser',
14
+ 'browser_tabs',
15
+ 'browser_read_page',
16
+ 'browser_get_page_text',
17
+ 'browser_find',
18
+ 'browser_console',
19
+ 'browser_downloads',
20
+ 'browser_network',
21
+ 'browser_screenshot',
22
+ 'browser_batch',
17
23
  ]);
18
24
  const PLAN_MODE_RUN_COMMAND_NAMES = new Set([
19
25
  'pwd',
@@ -46,7 +52,7 @@ export function nextAgentMode(mode) {
46
52
  export function agentModeAllowsTool(mode, toolName) {
47
53
  return mode !== 'plan' || PLAN_MODE_TOOL_NAMES.has(toolName);
48
54
  }
49
- function getUnquotedShellText(command) {
55
+ export function getUnquotedShellText(command) {
50
56
  let quote = null;
51
57
  let escaped = false;
52
58
  let text = '';
@@ -140,8 +146,28 @@ export function buildPlanModeToolBlockedResult(toolName, action = 'Plan mode is
140
146
  }
141
147
  export function agentModeLabel(mode) {
142
148
  if (mode === 'auto-accept')
143
- return 'Auto-Accept';
149
+ return 'Bypass all permissions';
144
150
  if (mode === 'plan')
145
151
  return 'Plan · read-only';
152
+ return 'Default · Ask for Approval';
153
+ }
154
+ export function agentModeLabelTiers(mode) {
155
+ return [agentModeLabel(mode)];
156
+ }
157
+ export function agentModeShortLabel(mode) {
158
+ if (mode === 'auto-accept')
159
+ return 'Bypass';
160
+ if (mode === 'plan')
161
+ return 'Plan';
146
162
  return 'Default';
147
163
  }
164
+ export function agentModeColor(mode) {
165
+ if (mode === 'auto-accept')
166
+ return 'red';
167
+ if (mode === 'plan')
168
+ return 'yellow';
169
+ return 'cyan';
170
+ }
171
+ export function agentModeCycleSequence() {
172
+ return AGENT_MODES.map(agentModeShortLabel).join(' → ');
173
+ }
@@ -50,20 +50,26 @@ export async function fetchWhoami({ config, fetchImpl = globalThis.fetch, }) {
50
50
  const data = await fetchWhoamiResponse({ config, fetchImpl });
51
51
  return data.customer;
52
52
  }
53
- export async function fetchWhoamiResponse({ config, fetchImpl = globalThis.fetch, }) {
53
+ export async function fetchWhoamiResponse({ config, fetchImpl = globalThis.fetch, budget = {}, }) {
54
+ const { timeoutMs, ...ladder } = budget;
54
55
  const data = (await retryTransient(() => authorizedJson({
55
56
  config,
56
57
  path: '/v1/auth/whoami',
57
58
  fetchImpl,
58
- })));
59
+ ...(timeoutMs == null ? {} : { timeoutMs }),
60
+ }), ladder));
59
61
  if (!data?.customer?.email) {
60
62
  throw new Error('Server returned an invalid whoami response.');
61
63
  }
64
+ const latestVersion = data.release?.latestVersion;
62
65
  return {
63
66
  customer: data.customer,
64
67
  debugUi: {
65
68
  showSessionId: data.debugUi?.showSessionId === true,
66
69
  },
70
+ ...(typeof latestVersion === 'string' && latestVersion
71
+ ? { release: { latestVersion } }
72
+ : {}),
67
73
  ...(data.usage ? { usage: data.usage } : {}),
68
74
  };
69
75
  }
@@ -1,17 +1,28 @@
1
+ import { execSync } from 'node:child_process';
1
2
  import crypto from 'node:crypto';
3
+ import { readFileSync } from 'node:fs';
2
4
  import http from 'node:http';
3
5
  import os from 'node:os';
4
6
  import { openUrl } from '../core/open-url.js';
5
7
  import { ServerApiError, createTraceContext, failureCode, failureMessage, normalizeServerUrl, readJsonResponse, } from './http.js';
6
- const DEFAULT_WEBSITE_URL = 'https://thegit.ai';
7
- const DEFAULT_SERVER_URL = 'https://thegit.ai';
8
- const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
8
+ import { DEFAULT_THEGITAI_HOST } from './default-host.js';
9
+ import { isAbortError } from '../utils.js';
10
+ const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
9
11
  function shutDownServer(server) {
10
12
  server.closeAllConnections?.();
11
13
  server.close();
12
14
  }
15
+ export class SignInCancelledError extends Error {
16
+ constructor() {
17
+ super('Sign-in cancelled.');
18
+ this.name = 'SignInCancelledError';
19
+ }
20
+ }
21
+ export function isSignInCancelled(error) {
22
+ return error instanceof SignInCancelledError;
23
+ }
13
24
  export function resolveWebsiteUrl() {
14
- return DEFAULT_WEBSITE_URL.replace(/\/+$/, '');
25
+ return DEFAULT_THEGITAI_HOST.replace(/\/+$/, '');
15
26
  }
16
27
  function defaultDeviceName() {
17
28
  try {
@@ -21,6 +32,73 @@ function defaultDeviceName() {
21
32
  return os.hostname();
22
33
  }
23
34
  }
35
+ function readLinuxOsPrettyName() {
36
+ try {
37
+ const content = readFileSync('/etc/os-release', 'utf8');
38
+ return content.match(/^PRETTY_NAME="?([^"\n]*)"?$/m)?.[1]?.trim() ?? '';
39
+ }
40
+ catch {
41
+ return '';
42
+ }
43
+ }
44
+ function macArchLabel() {
45
+ try {
46
+ if (process.arch === 'arm64')
47
+ return 'Apple Silicon';
48
+ if (os.cpus().some((cpu) => cpu.model.includes('Apple'))) {
49
+ return 'Apple Silicon';
50
+ }
51
+ }
52
+ catch {
53
+ }
54
+ return 'Intel';
55
+ }
56
+ export function describeOperatingSystem() {
57
+ try {
58
+ if (process.platform === 'linux') {
59
+ const base = readLinuxOsPrettyName() || `Linux ${os.release()}`;
60
+ return process.arch === 'arm64' ? `${base}, ARM64` : base;
61
+ }
62
+ if (process.platform === 'darwin') {
63
+ let version = '';
64
+ try {
65
+ version = execSync('sw_vers -productVersion', {
66
+ stdio: ['ignore', 'pipe', 'ignore'],
67
+ })
68
+ .toString()
69
+ .trim();
70
+ }
71
+ catch {
72
+ }
73
+ return `${version ? `macOS ${version}` : 'macOS'}, ${macArchLabel()}`;
74
+ }
75
+ if (process.platform === 'win32') {
76
+ let label = '';
77
+ try {
78
+ label = os.version();
79
+ }
80
+ catch {
81
+ }
82
+ const build = Number(os.release().split('.')[2] ?? '0');
83
+ if (build >= 22000)
84
+ label = label.replace(/Windows 10/i, 'Windows 11');
85
+ const base = label || `Windows ${os.release()}`;
86
+ return process.arch === 'arm64' ? `${base}, ARM64` : base;
87
+ }
88
+ return `${process.platform} ${os.release()}`;
89
+ }
90
+ catch {
91
+ return process.platform;
92
+ }
93
+ }
94
+ export function withOperatingSystemInfo(name) {
95
+ const trimmed = name.trim();
96
+ const osLabel = describeOperatingSystem();
97
+ const combined = osLabel && !trimmed.includes(osLabel)
98
+ ? `${trimmed} (${osLabel})`
99
+ : trimmed;
100
+ return combined.slice(0, 180);
101
+ }
24
102
  export function generatePkce() {
25
103
  const verifier = crypto.randomBytes(32).toString('base64url');
26
104
  const challenge = crypto
@@ -33,12 +111,8 @@ function buildAuthUrl(websiteUrl, params) {
33
111
  const url = new URL(`${websiteUrl}/cli-auth`);
34
112
  url.searchParams.set('code_challenge', params.codeChallenge);
35
113
  url.searchParams.set('device_name', params.deviceName);
36
- if (params.redirectUri)
37
- url.searchParams.set('redirect_uri', params.redirectUri);
38
- if (params.state)
39
- url.searchParams.set('state', params.state);
40
- if (params.paste)
41
- url.searchParams.set('mode', 'paste');
114
+ url.searchParams.set('redirect_uri', params.redirectUri);
115
+ url.searchParams.set('state', params.state);
42
116
  return url.toString();
43
117
  }
44
118
  const RESULT_PAGE = (heading, detail) => `<!doctype html><html><head><meta charset="utf-8"><title>TheGitAI CLI</title>` +
@@ -71,36 +145,38 @@ async function exchangeCodeForToken({ serverUrl, code, codeVerifier, fetchImpl,
71
145
  customer,
72
146
  };
73
147
  }
148
+ async function readPastedResult({ promptCode, signal, serverUrl, codeVerifier, fetchImpl, onPasteRejected, }) {
149
+ while (!signal.aborted) {
150
+ const code = (await promptCode(signal)).trim();
151
+ if (code) {
152
+ try {
153
+ return await exchangeCodeForToken({ serverUrl, code, codeVerifier, fetchImpl });
154
+ }
155
+ catch (error) {
156
+ if (signal.aborted)
157
+ break;
158
+ onPasteRejected?.(error.message);
159
+ }
160
+ }
161
+ await new Promise((resolve) => setImmediate(resolve));
162
+ }
163
+ return await new Promise(() => { });
164
+ }
74
165
  export async function loginViaBrowser(options) {
75
- const serverUrl = normalizeServerUrl(options.serverUrl ?? DEFAULT_SERVER_URL);
166
+ const serverUrl = normalizeServerUrl(options.serverUrl ?? DEFAULT_THEGITAI_HOST);
76
167
  const websiteUrl = resolveWebsiteUrl();
77
168
  const fetchImpl = options.fetchImpl ?? globalThis.fetch;
78
169
  const openBrowser = options.openBrowser ?? openUrl;
79
170
  const onUrl = options.onUrl ?? (() => { });
80
- const deviceName = options.deviceName ?? defaultDeviceName();
171
+ const deviceName = withOperatingSystemInfo(options.deviceName ?? defaultDeviceName());
81
172
  const { verifier, challenge } = generatePkce();
82
- if (options.noBrowser) {
83
- const authUrl = buildAuthUrl(websiteUrl, {
84
- codeChallenge: challenge,
85
- deviceName,
86
- paste: true,
87
- });
88
- onUrl(authUrl);
89
- if (!options.promptCode) {
90
- throw new Error('No way to read the authorization code in this context.');
91
- }
92
- const code = (await options.promptCode()).trim();
93
- if (!code) {
94
- throw new Error('No authorization code was entered.');
95
- }
96
- return exchangeCodeForToken({ serverUrl, code, codeVerifier: verifier, fetchImpl });
97
- }
98
173
  const state = crypto.randomBytes(16).toString('base64url');
99
174
  const server = http.createServer();
175
+ let timer;
100
176
  const codePromise = new Promise((resolve, reject) => {
101
- const timer = setTimeout(() => {
177
+ timer = setTimeout(() => {
102
178
  shutDownServer(server);
103
- reject(new Error('Timed out waiting for the browser login to complete.'));
179
+ reject(new Error('Sign-in timed out. Authorization codes last 10 minutes - run `ai` again to start over.'));
104
180
  }, options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
105
181
  server.on('request', (req, res) => {
106
182
  const requestUrl = new URL(req.url ?? '/', 'http://127.0.0.1');
@@ -113,7 +189,7 @@ export async function loginViaBrowser(options) {
113
189
  const returnedState = requestUrl.searchParams.get('state') ?? '';
114
190
  if (!code || returnedState !== state) {
115
191
  res.writeHead(400, { 'content-type': 'text/html', connection: 'close' });
116
- res.end(RESULT_PAGE('Login failed', 'The request could not be verified. Please run ai login again.'));
192
+ res.end(RESULT_PAGE('Login failed', 'The request could not be verified. Please run `ai` again.'));
117
193
  clearTimeout(timer);
118
194
  shutDownServer(server);
119
195
  reject(new Error('The login callback could not be verified.'));
@@ -143,8 +219,44 @@ export async function loginViaBrowser(options) {
143
219
  state,
144
220
  });
145
221
  onUrl(authUrl);
146
- await openBrowser(authUrl).catch(() => false);
147
- options.onWaiting?.();
148
- const code = await codePromise;
149
- return exchangeCodeForToken({ serverUrl, code, codeVerifier: verifier, fetchImpl });
222
+ const opened = await openBrowser(authUrl).catch(() => false);
223
+ options.onBrowserOpen?.(opened);
224
+ let raceSettled = false;
225
+ const untilSettled = (promise) => promise.catch((error) => {
226
+ if (raceSettled)
227
+ return new Promise(() => { });
228
+ throw error;
229
+ });
230
+ const pasteAbort = new AbortController();
231
+ const routes = [
232
+ untilSettled(codePromise.then((code) => exchangeCodeForToken({ serverUrl, code, codeVerifier: verifier, fetchImpl }))),
233
+ ];
234
+ if (options.promptCode) {
235
+ const pasted = readPastedResult({
236
+ promptCode: options.promptCode,
237
+ signal: pasteAbort.signal,
238
+ serverUrl,
239
+ codeVerifier: verifier,
240
+ fetchImpl,
241
+ onPasteRejected: options.onPasteRejected,
242
+ }).catch((error) => {
243
+ if (!raceSettled && isAbortError(error))
244
+ throw new SignInCancelledError();
245
+ if (!raceSettled) {
246
+ return new Promise(() => { });
247
+ }
248
+ throw error;
249
+ });
250
+ routes.push(untilSettled(pasted));
251
+ }
252
+ try {
253
+ return await Promise.race(routes);
254
+ }
255
+ finally {
256
+ raceSettled = true;
257
+ pasteAbort.abort();
258
+ if (timer)
259
+ clearTimeout(timer);
260
+ shutDownServer(server);
261
+ }
150
262
  }