@thegitai/cli 1.0.0-preview.6 → 1.0.0-preview.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -7
- package/dist/bin/ai.js +140 -111
- package/dist/bin/browser-host.js +284 -0
- package/dist/src/agent-mode.js +32 -7
- package/dist/src/api/auth.js +8 -2
- package/dist/src/api/browser-login.js +147 -35
- package/dist/src/api/chat.js +691 -166
- package/dist/src/api/contracts.js +55 -1
- package/dist/src/api/default-host.js +1 -0
- package/dist/src/api/http.js +139 -8
- package/dist/src/api/models.js +34 -9
- package/dist/src/api/startup-handshake.js +36 -0
- package/dist/src/api/turn-recovery.js +189 -0
- package/dist/src/background-jobs.js +8 -2
- package/dist/src/browser/bridge.js +268 -0
- package/dist/src/browser/chrome-profiles.js +73 -0
- package/dist/src/browser/framing.js +42 -0
- package/dist/src/browser/native-host.js +219 -0
- package/dist/src/browser/protocol.js +46 -0
- package/dist/src/browser/session-bridge.js +130 -0
- package/dist/src/cli-args.js +19 -5
- package/dist/src/client-environment.js +2 -0
- package/dist/src/core/clipboard.js +7 -13
- package/dist/src/core/image-limits.js +56 -0
- package/dist/src/core/image-path-extractor.js +70 -3
- package/dist/src/core/session-image-store.js +246 -0
- package/dist/src/edit-journal.js +33 -2
- package/dist/src/executor.js +25 -3
- package/dist/src/help-text.js +128 -22
- package/dist/src/job-follow-up.js +40 -0
- package/dist/src/mcp-client.js +335 -0
- package/dist/src/mcp-command.js +176 -0
- package/dist/src/mcp-config.js +123 -0
- package/dist/src/mcp-sdk.js +3 -0
- package/dist/src/mcp-types.js +26 -0
- package/dist/src/patcher.js +43 -4
- package/dist/src/permissions.js +301 -0
- package/dist/src/preferences.js +39 -0
- package/dist/src/project-key.js +11 -0
- package/dist/src/project-model-preferences.js +74 -0
- package/dist/src/quota-notice.js +35 -0
- package/dist/src/reasoning-effort.js +27 -0
- package/dist/src/session-safety.js +0 -12
- package/dist/src/session-store.js +123 -30
- package/dist/src/session.js +20 -4
- package/dist/src/signin.js +58 -0
- package/dist/src/tool-executor.js +26 -49
- package/dist/src/tools/browser.js +872 -0
- package/dist/src/tools/delete-file.js +20 -4
- package/dist/src/tools/index.js +32 -10
- package/dist/src/tools/patch-file.js +12 -26
- package/dist/src/tools/read-file.js +5 -0
- package/dist/src/tools/read-image-file.js +85 -0
- package/dist/src/tools/replace-document-text.js +28 -18
- package/dist/src/tools/restore-checkpoint.js +0 -1
- package/dist/src/tools/run-command.js +14 -71
- package/dist/src/tools/run-node-script.js +12 -81
- package/dist/src/tools/save-generated-image.js +120 -0
- package/dist/src/tools/str-replace.js +12 -26
- package/dist/src/tools/undo-edit.js +1 -6
- package/dist/src/tools/write-file.js +67 -11
- package/dist/src/ui/prompt-history-store.js +1 -1
- package/dist/src/ui/repl.js +1017 -299
- package/dist/src/ui/slash-commands.js +122 -0
- package/dist/src/ui/tui/attention.js +25 -0
- package/dist/src/ui/tui/bridge.js +38 -5
- package/dist/src/ui/tui/build-frame.js +807 -270
- package/dist/src/ui/tui/composer-layout.js +76 -0
- package/dist/src/ui/tui/desktop-notification.js +118 -0
- package/dist/src/ui/tui/markdown-render.js +21 -11
- package/dist/src/ui/tui/model-label.js +13 -0
- package/dist/src/ui/tui/shell-input.js +275 -73
- package/dist/src/ui/tui/shortcuts.js +61 -0
- package/dist/src/ui/tui/terminal-theme.js +28 -0
- package/dist/src/ui/tui/text.js +31 -6
- package/dist/src/ui/tui/user-input.js +593 -0
- package/dist/src/update-check.js +205 -0
- package/dist/src/usage-notice.js +52 -0
- package/dist/src/usage.js +5 -2
- package/dist/src/utils.js +13 -0
- package/dist/src/version.js +52 -2
- package/dist/vendor/mcp/LICENSES.txt +414 -0
- package/dist/vendor/mcp/client.cjs +82 -0
- package/package.json +22 -9
- package/dist/src/project-index.js +0 -233
- package/dist/src/tools/code-intel.js +0 -472
- package/dist/src/tools/find-symbol.js +0 -70
- package/dist/src/tools/hover-symbol.js +0 -95
- package/dist/src/tools/list-symbols.js +0 -55
- package/dist/src/tools/search-code.js +0 -37
- package/dist/src/tools/signature-help.js +0 -118
package/dist/src/patcher.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import chalk from './colors.js';
|
|
2
|
-
import { chmodSync, closeSync, constants, existsSync, fchmodSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, unlinkSync, writeFileSync, } from 'fs';
|
|
2
|
+
import { chmodSync, closeSync, constants, existsSync, fchmodSync, fstatSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, unlinkSync, writeFileSync, } from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { createInterface } from 'readline';
|
|
5
5
|
import { runCommand } from './executor.js';
|
|
@@ -271,22 +271,61 @@ export function writeProjectFileBuffer(rootDir, filePath, content) {
|
|
|
271
271
|
}
|
|
272
272
|
return { absPath, changed: true };
|
|
273
273
|
}
|
|
274
|
+
const MAX_DELETED_CONTENT_BYTES = 64 * 1024;
|
|
275
|
+
const DELETED_BINARY_SNIFF_BYTES = 4096;
|
|
276
|
+
function headHasNulByte(absPath) {
|
|
277
|
+
const chunk = Buffer.allocUnsafe(DELETED_BINARY_SNIFF_BYTES);
|
|
278
|
+
let fd = null;
|
|
279
|
+
try {
|
|
280
|
+
fd = openSync(absPath, 'r');
|
|
281
|
+
const read = readSync(fd, chunk, 0, chunk.length, 0);
|
|
282
|
+
return read > 0 && chunk.subarray(0, read).includes(0);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
if (fd !== null)
|
|
289
|
+
closeSync(fd);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
274
292
|
export function deleteProjectFile(rootDir, filePath) {
|
|
275
293
|
const absPath = resolveProjectPath(rootDir, filePath);
|
|
276
294
|
if (!existsSync(absPath)) {
|
|
277
295
|
return { deleted: false, absPath };
|
|
278
296
|
}
|
|
297
|
+
let bytes;
|
|
279
298
|
let content;
|
|
299
|
+
let contentOmitted;
|
|
280
300
|
try {
|
|
281
|
-
|
|
282
|
-
|
|
301
|
+
const stat = lstatSync(absPath);
|
|
302
|
+
if (!stat.isSymbolicLink()) {
|
|
303
|
+
bytes = stat.size;
|
|
304
|
+
if (stat.size > MAX_DELETED_CONTENT_BYTES) {
|
|
305
|
+
contentOmitted = headHasNulByte(absPath) ? 'binary' : 'too_large';
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const buffer = readFileSync(absPath);
|
|
309
|
+
if (buffer.includes(0))
|
|
310
|
+
contentOmitted = 'binary';
|
|
311
|
+
else
|
|
312
|
+
content = buffer.toString('utf-8');
|
|
313
|
+
}
|
|
283
314
|
}
|
|
284
315
|
}
|
|
285
316
|
catch {
|
|
317
|
+
bytes = undefined;
|
|
286
318
|
content = undefined;
|
|
319
|
+
contentOmitted = undefined;
|
|
287
320
|
}
|
|
288
321
|
unlinkSync(absPath);
|
|
289
|
-
return {
|
|
322
|
+
return {
|
|
323
|
+
deleted: true,
|
|
324
|
+
absPath,
|
|
325
|
+
...(bytes === undefined ? {} : { bytes }),
|
|
326
|
+
...(content === undefined ? {} : { content }),
|
|
327
|
+
...(contentOmitted === undefined ? {} : { contentOmitted }),
|
|
328
|
+
};
|
|
290
329
|
}
|
|
291
330
|
export function readProjectFile(rootDir, filePath) {
|
|
292
331
|
const absPath = resolveProjectPath(rootDir, filePath);
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { getUnquotedShellText } from './agent-mode.js';
|
|
2
|
+
export const PERMISSION_BUCKETS = ['create', 'edit', 'delete', 'run', 'browse', 'mcp'];
|
|
3
|
+
export function createSessionGrants() {
|
|
4
|
+
return { buckets: [], commandPrefixes: [], origins: [] };
|
|
5
|
+
}
|
|
6
|
+
export function bucketActionLabel(bucket) {
|
|
7
|
+
if (bucket === 'create')
|
|
8
|
+
return 'create files';
|
|
9
|
+
if (bucket === 'edit')
|
|
10
|
+
return 'edit files';
|
|
11
|
+
if (bucket === 'delete')
|
|
12
|
+
return 'delete files';
|
|
13
|
+
if (bucket === 'browse')
|
|
14
|
+
return 'act on any website';
|
|
15
|
+
if (bucket === 'mcp')
|
|
16
|
+
return 'use MCP tools';
|
|
17
|
+
return 'run commands';
|
|
18
|
+
}
|
|
19
|
+
export function originForUrl(url) {
|
|
20
|
+
const raw = String(url ?? '').trim();
|
|
21
|
+
if (!raw)
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
const parsed = new URL(/^[a-z][a-z0-9+.-]*:/i.test(raw) ? raw : `https://${raw}`);
|
|
25
|
+
if (parsed.protocol === 'about:' || parsed.protocol === 'chrome:')
|
|
26
|
+
return null;
|
|
27
|
+
return parsed.origin;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function isOriginGranted(grants, origin) {
|
|
34
|
+
if (!grants || !origin)
|
|
35
|
+
return false;
|
|
36
|
+
return grants.origins?.includes(origin) === true;
|
|
37
|
+
}
|
|
38
|
+
export function grantOrigin(grants, origin) {
|
|
39
|
+
const normalized = String(origin ?? '').trim();
|
|
40
|
+
if (!normalized)
|
|
41
|
+
return;
|
|
42
|
+
const origins = grants.origins ??= [];
|
|
43
|
+
if (!origins.includes(normalized))
|
|
44
|
+
origins.push(normalized);
|
|
45
|
+
}
|
|
46
|
+
const NO_PREFIX_GRANT_BINARIES = new Set([
|
|
47
|
+
'rm',
|
|
48
|
+
'rmdir',
|
|
49
|
+
'mv',
|
|
50
|
+
'dd',
|
|
51
|
+
'mkfs',
|
|
52
|
+
'shred',
|
|
53
|
+
'sudo',
|
|
54
|
+
'doas',
|
|
55
|
+
'su',
|
|
56
|
+
'chmod',
|
|
57
|
+
'chown',
|
|
58
|
+
'chgrp',
|
|
59
|
+
'kill',
|
|
60
|
+
'pkill',
|
|
61
|
+
'killall',
|
|
62
|
+
'shutdown',
|
|
63
|
+
'reboot',
|
|
64
|
+
'curl',
|
|
65
|
+
'wget',
|
|
66
|
+
'ssh',
|
|
67
|
+
'scp',
|
|
68
|
+
'nc',
|
|
69
|
+
'eval',
|
|
70
|
+
'exec',
|
|
71
|
+
'source',
|
|
72
|
+
]);
|
|
73
|
+
const INTERPRETER_OR_WRAPPER_BINARIES = new Set([
|
|
74
|
+
'bash',
|
|
75
|
+
'sh',
|
|
76
|
+
'zsh',
|
|
77
|
+
'fish',
|
|
78
|
+
'dash',
|
|
79
|
+
'ksh',
|
|
80
|
+
'csh',
|
|
81
|
+
'tcsh',
|
|
82
|
+
'python',
|
|
83
|
+
'python2',
|
|
84
|
+
'python3',
|
|
85
|
+
'node',
|
|
86
|
+
'deno',
|
|
87
|
+
'bun',
|
|
88
|
+
'ruby',
|
|
89
|
+
'perl',
|
|
90
|
+
'php',
|
|
91
|
+
'lua',
|
|
92
|
+
'osascript',
|
|
93
|
+
'awk',
|
|
94
|
+
'gawk',
|
|
95
|
+
'sed',
|
|
96
|
+
'env',
|
|
97
|
+
'timeout',
|
|
98
|
+
'xargs',
|
|
99
|
+
'nohup',
|
|
100
|
+
'setsid',
|
|
101
|
+
'watch',
|
|
102
|
+
'script',
|
|
103
|
+
'nice',
|
|
104
|
+
'ionice',
|
|
105
|
+
'stdbuf',
|
|
106
|
+
'time',
|
|
107
|
+
'sudo',
|
|
108
|
+
'doas',
|
|
109
|
+
'su',
|
|
110
|
+
'find',
|
|
111
|
+
'make',
|
|
112
|
+
]);
|
|
113
|
+
const SCRIPT_RUNNER_VERBS = new Set(['run', 'exec', 'run-script', 'x', 'dlx']);
|
|
114
|
+
const VERB_PATTERN = /^[A-Za-z][A-Za-z0-9_:-]*$/;
|
|
115
|
+
const BINARY_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._+-]*$/;
|
|
116
|
+
const SHELL_CONTROL_OPERATOR_PATTERN = /[&|;<>\n]/;
|
|
117
|
+
const COMMAND_SUBSTITUTION_PATTERN = /`|\$\(/;
|
|
118
|
+
function normalizeCommand(command) {
|
|
119
|
+
return String(command ?? '').trim().replace(/\s+/g, ' ');
|
|
120
|
+
}
|
|
121
|
+
function parseCommand(command) {
|
|
122
|
+
const raw = String(command ?? '');
|
|
123
|
+
if (!raw.trim())
|
|
124
|
+
return null;
|
|
125
|
+
if (COMMAND_SUBSTITUTION_PATTERN.test(raw))
|
|
126
|
+
return null;
|
|
127
|
+
if (SHELL_CONTROL_OPERATOR_PATTERN.test(getUnquotedShellText(raw)))
|
|
128
|
+
return null;
|
|
129
|
+
const all = normalizeCommand(raw).match(/\S+/g) ?? [];
|
|
130
|
+
const tokens = [];
|
|
131
|
+
for (const token of all) {
|
|
132
|
+
if (!tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(token))
|
|
133
|
+
continue;
|
|
134
|
+
if (!tokens.length && token === 'command')
|
|
135
|
+
continue;
|
|
136
|
+
tokens.push(token);
|
|
137
|
+
}
|
|
138
|
+
if (!tokens.length)
|
|
139
|
+
return null;
|
|
140
|
+
const binary = (tokens[0].split('/').pop() ?? '').trim();
|
|
141
|
+
if (!binary)
|
|
142
|
+
return null;
|
|
143
|
+
tokens[0] = binary;
|
|
144
|
+
return { canonical: tokens.join(' '), tokens };
|
|
145
|
+
}
|
|
146
|
+
function grantPrefixFromTokens(tokens) {
|
|
147
|
+
const binary = tokens[0];
|
|
148
|
+
if (!BINARY_PATTERN.test(binary))
|
|
149
|
+
return null;
|
|
150
|
+
if (NO_PREFIX_GRANT_BINARIES.has(binary))
|
|
151
|
+
return null;
|
|
152
|
+
if (INTERPRETER_OR_WRAPPER_BINARIES.has(binary))
|
|
153
|
+
return null;
|
|
154
|
+
const verb = tokens[1];
|
|
155
|
+
if (!verb || !VERB_PATTERN.test(verb))
|
|
156
|
+
return null;
|
|
157
|
+
if (SCRIPT_RUNNER_VERBS.has(verb)) {
|
|
158
|
+
const script = tokens[2];
|
|
159
|
+
if (!script || !VERB_PATTERN.test(script))
|
|
160
|
+
return null;
|
|
161
|
+
return `${binary} ${verb} ${script}`;
|
|
162
|
+
}
|
|
163
|
+
return `${binary} ${verb}`;
|
|
164
|
+
}
|
|
165
|
+
export function commandGrantPrefix(command) {
|
|
166
|
+
const parsed = parseCommand(command);
|
|
167
|
+
return parsed ? grantPrefixFromTokens(parsed.tokens) : null;
|
|
168
|
+
}
|
|
169
|
+
export function isBucketGranted(grants, bucket) {
|
|
170
|
+
return Boolean(grants?.buckets.includes(bucket));
|
|
171
|
+
}
|
|
172
|
+
export function isCommandGranted(grants, command) {
|
|
173
|
+
if (!grants)
|
|
174
|
+
return false;
|
|
175
|
+
if (grants.buckets.includes('run'))
|
|
176
|
+
return true;
|
|
177
|
+
if (!grants.commandPrefixes.length)
|
|
178
|
+
return false;
|
|
179
|
+
const parsed = parseCommand(command);
|
|
180
|
+
if (!parsed)
|
|
181
|
+
return false;
|
|
182
|
+
if (grantPrefixFromTokens(parsed.tokens) === null)
|
|
183
|
+
return false;
|
|
184
|
+
return grants.commandPrefixes.some((prefix) => parsed.canonical === prefix || parsed.canonical.startsWith(`${prefix} `));
|
|
185
|
+
}
|
|
186
|
+
export function grantBucket(grants, bucket) {
|
|
187
|
+
if (!grants.buckets.includes(bucket))
|
|
188
|
+
grants.buckets.push(bucket);
|
|
189
|
+
}
|
|
190
|
+
export function grantCommandPrefix(grants, prefix) {
|
|
191
|
+
const normalized = normalizeCommand(prefix);
|
|
192
|
+
if (!normalized)
|
|
193
|
+
return;
|
|
194
|
+
if (!grants.commandPrefixes.includes(normalized)) {
|
|
195
|
+
grants.commandPrefixes.push(normalized);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
export function buildPermissionOptions(bucket, command, origin) {
|
|
199
|
+
const options = [
|
|
200
|
+
{ label: 'Approve once', decision: { kind: 'once' } },
|
|
201
|
+
];
|
|
202
|
+
const prefix = bucket === 'run' && command ? commandGrantPrefix(command) : null;
|
|
203
|
+
if (prefix) {
|
|
204
|
+
options.push({
|
|
205
|
+
label: `Always allow: ${prefix}`,
|
|
206
|
+
decision: { kind: 'always-prefix', prefix },
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (bucket === 'browse' && origin) {
|
|
210
|
+
options.push({
|
|
211
|
+
label: `Allow this site for the session: ${origin}`,
|
|
212
|
+
decision: { kind: 'always-origin', origin },
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
options.push({
|
|
216
|
+
label: `Always allow: ${bucketActionLabel(bucket)}`,
|
|
217
|
+
decision: { kind: 'always-bucket' },
|
|
218
|
+
});
|
|
219
|
+
options.push({ label: 'Deny', decision: { kind: 'deny' } });
|
|
220
|
+
return options;
|
|
221
|
+
}
|
|
222
|
+
function declinedSubject(bucket) {
|
|
223
|
+
if (bucket === 'mcp')
|
|
224
|
+
return { noun: 'MCP tool call', effect: 'The MCP tool was not called' };
|
|
225
|
+
if (bucket === 'run') {
|
|
226
|
+
return { noun: 'command', effect: 'Nothing was executed' };
|
|
227
|
+
}
|
|
228
|
+
if (bucket === 'create') {
|
|
229
|
+
return { noun: 'file creation', effect: 'Nothing was created' };
|
|
230
|
+
}
|
|
231
|
+
if (bucket === 'delete') {
|
|
232
|
+
return { noun: 'file deletion', effect: 'Nothing was deleted' };
|
|
233
|
+
}
|
|
234
|
+
if (bucket === 'browse') {
|
|
235
|
+
return { noun: 'browser action', effect: 'The page was not touched' };
|
|
236
|
+
}
|
|
237
|
+
return { noun: 'edit', effect: 'Nothing was changed' };
|
|
238
|
+
}
|
|
239
|
+
function declinedResponse(bucket, toolName, extra) {
|
|
240
|
+
const { noun, effect } = declinedSubject(bucket);
|
|
241
|
+
return {
|
|
242
|
+
ok: false,
|
|
243
|
+
skipped: true,
|
|
244
|
+
...extra,
|
|
245
|
+
failureCategory: 'user_declined',
|
|
246
|
+
failureDetails: {
|
|
247
|
+
category: 'user_declined',
|
|
248
|
+
tool: toolName,
|
|
249
|
+
action: 'Respect the real user’s decision. Do not retry the same or an equivalent action; reconsider the approach or ask one specific question if needed.',
|
|
250
|
+
},
|
|
251
|
+
error: `The real user rejected this proposed ${noun}. ${effect}; this was not a tool failure or an automated system skip.`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
export async function ensurePermission(context, request, toolName, extra = {}) {
|
|
255
|
+
if (context.autoYes)
|
|
256
|
+
return null;
|
|
257
|
+
if (request.mcpTool && context.grants?.mcpTools?.includes(request.mcpTool))
|
|
258
|
+
return null;
|
|
259
|
+
if (isBucketGranted(context.grants, request.bucket))
|
|
260
|
+
return null;
|
|
261
|
+
if (request.bucket === 'run' &&
|
|
262
|
+
request.command &&
|
|
263
|
+
isCommandGranted(context.grants, request.command)) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (request.bucket === 'browse' &&
|
|
267
|
+
isOriginGranted(context.grants, request.origin ?? null)) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
if (!context.requestPermission) {
|
|
271
|
+
return {
|
|
272
|
+
ok: false,
|
|
273
|
+
...extra,
|
|
274
|
+
error: `requestPermission is required when autoYes is false (${toolName})`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
const options = request.mcpTool ? [
|
|
278
|
+
{ label: 'Approve once', decision: { kind: 'once' } },
|
|
279
|
+
{ label: 'Allow this MCP tool for the session', decision: { kind: 'always-mcp-tool', tool: request.mcpTool } },
|
|
280
|
+
{ label: 'Deny', decision: { kind: 'deny' } },
|
|
281
|
+
] : buildPermissionOptions(request.bucket, request.command, request.origin);
|
|
282
|
+
const decision = await context.requestPermission({ ...request, options });
|
|
283
|
+
if (decision.kind === 'deny') {
|
|
284
|
+
return declinedResponse(request.bucket, toolName, extra);
|
|
285
|
+
}
|
|
286
|
+
if (context.grants) {
|
|
287
|
+
if (decision.kind === 'always-mcp-tool' && decision.tool === request.mcpTool) {
|
|
288
|
+
(context.grants.mcpTools ??= []).push(decision.tool);
|
|
289
|
+
}
|
|
290
|
+
else if (decision.kind === 'always-bucket') {
|
|
291
|
+
grantBucket(context.grants, request.bucket);
|
|
292
|
+
}
|
|
293
|
+
else if (decision.kind === 'always-prefix') {
|
|
294
|
+
grantCommandPrefix(context.grants, decision.prefix);
|
|
295
|
+
}
|
|
296
|
+
else if (decision.kind === 'always-origin') {
|
|
297
|
+
grantOrigin(context.grants, decision.origin);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { getClientStateDir } from './client-state.js';
|
|
4
|
+
export const DEFAULT_PREFERENCES = {
|
|
5
|
+
notifications: true,
|
|
6
|
+
};
|
|
7
|
+
export function getPreferencesPath(env = process.env) {
|
|
8
|
+
return path.join(getClientStateDir(env), 'prefs.json');
|
|
9
|
+
}
|
|
10
|
+
export function readPreferences(env = process.env) {
|
|
11
|
+
const filePath = getPreferencesPath(env);
|
|
12
|
+
if (!existsSync(filePath))
|
|
13
|
+
return { ...DEFAULT_PREFERENCES };
|
|
14
|
+
try {
|
|
15
|
+
const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
|
|
16
|
+
return {
|
|
17
|
+
notifications: parsed.notifications === false ? false : DEFAULT_PREFERENCES.notifications,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return { ...DEFAULT_PREFERENCES };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function writePreferences(preferences, env = process.env) {
|
|
25
|
+
const filePath = getPreferencesPath(env);
|
|
26
|
+
mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
27
|
+
const tempPath = `${filePath}.${process.pid}.tmp`;
|
|
28
|
+
try {
|
|
29
|
+
writeFileSync(tempPath, `${JSON.stringify(preferences, null, 2)}\n`, {
|
|
30
|
+
encoding: 'utf8',
|
|
31
|
+
mode: 0o600,
|
|
32
|
+
});
|
|
33
|
+
renameSync(tempPath, filePath);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
rmSync(tempPath, { force: true });
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export function getSessionProjectKey(rootDir) {
|
|
4
|
+
const resolvedRoot = path.resolve(rootDir);
|
|
5
|
+
const projectName = path.basename(resolvedRoot).replace(/[^a-zA-Z0-9._-]/g, '_') || 'project';
|
|
6
|
+
const hash = createHash('sha256')
|
|
7
|
+
.update(resolvedRoot)
|
|
8
|
+
.digest('hex')
|
|
9
|
+
.slice(0, 16);
|
|
10
|
+
return `${projectName}-${hash}`;
|
|
11
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { getClientStateDir } from './client-state.js';
|
|
5
|
+
import { getSessionProjectKey } from './project-key.js';
|
|
6
|
+
import { isReasoningEffort } from './reasoning-effort.js';
|
|
7
|
+
function preferencesPath(rootDir, env) {
|
|
8
|
+
return path.join(getClientStateDir(env), 'projects', getSessionProjectKey(rootDir), 'model-efforts.json');
|
|
9
|
+
}
|
|
10
|
+
function parseScopes(contents) {
|
|
11
|
+
const parsed = JSON.parse(contents);
|
|
12
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
13
|
+
throw new Error('Project model preferences must be an object.');
|
|
14
|
+
}
|
|
15
|
+
return Object.fromEntries(Object.entries(parsed).map(([scope, models]) => {
|
|
16
|
+
if (!models || typeof models !== 'object' || Array.isArray(models)) {
|
|
17
|
+
throw new Error('Project model preferences contain an invalid model selection.');
|
|
18
|
+
}
|
|
19
|
+
const entries = Object.entries(models).map(([id, effort]) => {
|
|
20
|
+
if (!/^[1-9]\d*$/.test(id) || !isReasoningEffort(effort)) {
|
|
21
|
+
throw new Error('Project model preferences contain an invalid effort selection.');
|
|
22
|
+
}
|
|
23
|
+
return [id, effort];
|
|
24
|
+
});
|
|
25
|
+
return [scope, Object.fromEntries(entries)];
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
function readScopes(rootDir, env, onInvalid) {
|
|
29
|
+
const file = preferencesPath(rootDir, env);
|
|
30
|
+
if (!existsSync(file))
|
|
31
|
+
return {};
|
|
32
|
+
const contents = readFileSync(file, 'utf8');
|
|
33
|
+
try {
|
|
34
|
+
return parseScopes(contents);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (onInvalid === 'throw')
|
|
38
|
+
throw error;
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function scopeKey(scope) {
|
|
43
|
+
return scope.trim().replace(/\/+$/, '') || 'local';
|
|
44
|
+
}
|
|
45
|
+
export function readProjectModelEfforts(rootDir, scope = '', env = process.env) {
|
|
46
|
+
return readScopes(rootDir, env, 'default')[scopeKey(scope)] ?? {};
|
|
47
|
+
}
|
|
48
|
+
export function projectModelEffort(efforts, modelId) {
|
|
49
|
+
return efforts[String(modelId)] ?? 'default';
|
|
50
|
+
}
|
|
51
|
+
export function saveProjectModelEffort(rootDir, modelId, effort, scope = '', env = process.env) {
|
|
52
|
+
if (!Number.isInteger(modelId) || modelId <= 0 || !isReasoningEffort(effort)) {
|
|
53
|
+
throw new Error('Invalid project model preference.');
|
|
54
|
+
}
|
|
55
|
+
const scopes = readScopes(rootDir, env, 'throw');
|
|
56
|
+
const key = scopeKey(scope);
|
|
57
|
+
const models = { ...scopes[key] };
|
|
58
|
+
if (effort === 'default')
|
|
59
|
+
delete models[String(modelId)];
|
|
60
|
+
else
|
|
61
|
+
models[String(modelId)] = effort;
|
|
62
|
+
const file = preferencesPath(rootDir, env);
|
|
63
|
+
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
64
|
+
const temp = `${file}.${randomUUID()}.tmp`;
|
|
65
|
+
try {
|
|
66
|
+
writeFileSync(temp, `${JSON.stringify({ ...scopes, [key]: models }, null, 2)}\n`, { mode: 0o600 });
|
|
67
|
+
renameSync(temp, file);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
rmSync(temp, { force: true });
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
return models;
|
|
74
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isQuotaExceededError, ServerApiError } from './api/http.js';
|
|
2
|
+
import { USAGE_WINDOWS, formatUtcMinute, usageWindowLabel } from './usage.js';
|
|
3
|
+
export function buildQuotaNotice(error) {
|
|
4
|
+
if (!(error instanceof ServerApiError) || !isQuotaExceededError(error)) {
|
|
5
|
+
return null;
|
|
6
|
+
}
|
|
7
|
+
const label = usageWindowLabel(String(error.details.window ?? ''));
|
|
8
|
+
const inSentence = label.toLowerCase();
|
|
9
|
+
const resetsAt = error.details.resetsAt ?? null;
|
|
10
|
+
const lines = [];
|
|
11
|
+
if (label) {
|
|
12
|
+
const reset = resetsAt ? formatUtcMinute(resetsAt) : '';
|
|
13
|
+
lines.push(reset && reset !== 'unknown'
|
|
14
|
+
? `You've used your ${inSentence} limit. It resets at ${reset}.`
|
|
15
|
+
: `You've used your ${inSentence} limit.`);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
lines.push(firstMessageLine(error));
|
|
19
|
+
}
|
|
20
|
+
lines.push(label
|
|
21
|
+
? `Run /usage to see your ${otherWindowNames(label)} windows.`
|
|
22
|
+
: 'Run /usage to see your remaining usage.');
|
|
23
|
+
return { title: 'Usage limit reached', body: lines.join('\n') };
|
|
24
|
+
}
|
|
25
|
+
function otherWindowNames(usedLabel) {
|
|
26
|
+
const names = USAGE_WINDOWS.map(([name]) => name)
|
|
27
|
+
.filter((name) => name !== usedLabel)
|
|
28
|
+
.map((name) => name.toLowerCase());
|
|
29
|
+
return names.length > 1
|
|
30
|
+
? `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
|
|
31
|
+
: (names[0] ?? '');
|
|
32
|
+
}
|
|
33
|
+
function firstMessageLine(error) {
|
|
34
|
+
return String(error.message ?? '').split('\n')[0]?.trim() || 'Usage limit reached.';
|
|
35
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const REASONING_EFFORTS = ['default', 'low', 'balanced', 'deep'];
|
|
2
|
+
export const REASONING_EFFORT_LABELS = {
|
|
3
|
+
default: 'Default',
|
|
4
|
+
low: 'Low',
|
|
5
|
+
balanced: 'Balanced',
|
|
6
|
+
deep: 'Deep',
|
|
7
|
+
};
|
|
8
|
+
export const REASONING_EFFORT_DESCRIPTIONS = {
|
|
9
|
+
default: 'Uses our recommended setting for this model.',
|
|
10
|
+
low: 'Favors quicker responses and lower reasoning cost.',
|
|
11
|
+
balanced: 'Balances reasoning depth, speed, and cost.',
|
|
12
|
+
deep: 'Favors thorough reasoning; may take longer and cost more.',
|
|
13
|
+
};
|
|
14
|
+
export function isReasoningEffort(value) {
|
|
15
|
+
return REASONING_EFFORTS.some((effort) => effort === value);
|
|
16
|
+
}
|
|
17
|
+
export function normalizeReasoningEffort(value) {
|
|
18
|
+
if (value === undefined)
|
|
19
|
+
return 'default';
|
|
20
|
+
if (isReasoningEffort(value))
|
|
21
|
+
return value;
|
|
22
|
+
throw new Error('Invalid reasoning effort. Choose Default, Low, Balanced, or Deep.');
|
|
23
|
+
}
|
|
24
|
+
export function cycleReasoningEffort(effort, direction) {
|
|
25
|
+
const index = REASONING_EFFORTS.indexOf(effort);
|
|
26
|
+
return REASONING_EFFORTS[(index + direction + REASONING_EFFORTS.length) % REASONING_EFFORTS.length];
|
|
27
|
+
}
|
|
@@ -4,7 +4,6 @@ import path from 'node:path';
|
|
|
4
4
|
import { ARTIFACT_IGNORE_DIRS, normalizeProjectRelativePath, shouldIgnoreArtifactPath, } from './artifact-policy.js';
|
|
5
5
|
import { hashBytes, readFileEditSnapshot, storedContentBuffer, } from './edit-journal.js';
|
|
6
6
|
import { deleteProjectFile, resolveProjectPath, writeProjectFile, writeProjectFileBuffer, } from './patcher.js';
|
|
7
|
-
import { removeIndexFile, upsertIndexFile } from './project-index.js';
|
|
8
7
|
const MAX_CHECKPOINTS = 20;
|
|
9
8
|
const MAX_SESSION_EDITS = 500;
|
|
10
9
|
const MAX_READ_RECORDS = 200;
|
|
@@ -672,7 +671,6 @@ export async function restoreCheckpointFiles(args) {
|
|
|
672
671
|
const restored = [];
|
|
673
672
|
const applied = [];
|
|
674
673
|
let changed = false;
|
|
675
|
-
const syncPolicy = args.projectIndex.initialized;
|
|
676
674
|
for (const snapshot of targets) {
|
|
677
675
|
const before = readFileEditSnapshot(args.rootDir, snapshot.filePath);
|
|
678
676
|
try {
|
|
@@ -680,15 +678,11 @@ export async function restoreCheckpointFiles(args) {
|
|
|
680
678
|
const result = writeStoredProjectFile(args.rootDir, snapshot.filePath, snapshot.content ?? '', snapshot.contentEncoding);
|
|
681
679
|
if (result.changed)
|
|
682
680
|
changed = true;
|
|
683
|
-
if (syncPolicy)
|
|
684
|
-
await upsertIndexFile(args.projectIndex, snapshot.filePath);
|
|
685
681
|
}
|
|
686
682
|
else {
|
|
687
683
|
const result = deleteProjectFile(args.rootDir, snapshot.filePath);
|
|
688
684
|
if (result.deleted)
|
|
689
685
|
changed = true;
|
|
690
|
-
if (syncPolicy)
|
|
691
|
-
await removeIndexFile(args.projectIndex, snapshot.filePath);
|
|
692
686
|
}
|
|
693
687
|
const after = readFileEditSnapshot(args.rootDir, snapshot.filePath);
|
|
694
688
|
applied.push({ snapshot, before, after });
|
|
@@ -710,15 +704,9 @@ export async function restoreCheckpointFiles(args) {
|
|
|
710
704
|
throw new Error('previous file content is unavailable');
|
|
711
705
|
}
|
|
712
706
|
writeStoredProjectFile(args.rootDir, item.snapshot.filePath, item.before.content, item.before.contentEncoding);
|
|
713
|
-
if (syncPolicy) {
|
|
714
|
-
await upsertIndexFile(args.projectIndex, item.snapshot.filePath);
|
|
715
|
-
}
|
|
716
707
|
}
|
|
717
708
|
else {
|
|
718
709
|
deleteProjectFile(args.rootDir, item.snapshot.filePath);
|
|
719
|
-
if (syncPolicy) {
|
|
720
|
-
await removeIndexFile(args.projectIndex, item.snapshot.filePath);
|
|
721
|
-
}
|
|
722
710
|
}
|
|
723
711
|
rolledBack.push({ filePath: item.snapshot.filePath });
|
|
724
712
|
}
|