copilot-operator 0.1.48 → 0.1.51
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 +582 -574
- package/dist/src/api/operator.controller.js +112 -926
- package/dist/src/api/operator.service.js +217 -2461
- package/dist/src/orchestrator/hold.js +49 -0
- package/dist/src/orchestrator/taskRunner.js +157 -23
- package/dist/src/plan/kerrigan.js +107 -0
- package/dist/src/plan/systemGuide.js +419 -3283
- package/dist/src/session/chatNotice.js +76 -0
- package/dist/src/session/store.js +2 -57
- package/dist/src/session/workspace.js +52 -5
- package/dist/src/session/workspaceGit.js +212 -0
- package/dist/src/transport/chatSession.js +28 -4
- package/dist/src/transport/chatTransport.js +1 -1
- package/dist/src/transport/copilotTransport.js +133 -1
- package/dist/web/404.html +5 -3
- package/dist/web/_next/static/chunks/134-ddd50a7fbb536ec8.js +1 -0
- package/dist/web/_next/static/chunks/190-70555f20dd0da19b.js +1 -0
- package/dist/web/_next/static/chunks/643-e85d221d57900ed7.js +1 -0
- package/dist/web/_next/static/chunks/867-869900ec999e6345.js +1 -0
- package/dist/web/_next/static/chunks/app/appearance/page-cdef33c421397cbe.js +1 -0
- package/dist/web/_next/static/chunks/app/defaults/page-e0a2b8f9a41a94c4.js +1 -0
- package/dist/web/_next/static/chunks/app/layout-b4fc21e26e1bad95.js +1 -0
- package/dist/web/_next/static/chunks/app/level1/page-31b74cae86d960d8.js +1 -0
- package/dist/web/_next/static/chunks/app/page-1075921d44db3e4d.js +1 -0
- package/dist/web/_next/static/chunks/app/presets/page-f8abdd83cdf468f7.js +1 -0
- package/dist/web/_next/static/chunks/app/system/page-2a270bb885885821.js +1 -0
- package/dist/web/_next/static/css/8b14cb3cf0051138.css +1 -0
- package/dist/web/_next/static/eqTUTH_SJtBgNgq8Zzy3n/_buildManifest.js +1 -0
- package/dist/web/appearance.html +5 -3
- package/dist/web/appearance.txt +22 -23
- package/dist/web/defaults.html +5 -3
- package/dist/web/defaults.txt +22 -23
- package/dist/web/index.html +5 -3
- package/dist/web/index.txt +22 -23
- package/dist/web/level1.html +5 -3
- package/dist/web/level1.txt +22 -23
- package/dist/web/presets.html +6 -4
- package/dist/web/presets.txt +22 -23
- package/dist/web/system.html +5 -3
- package/dist/web/system.txt +22 -23
- package/package.json +4 -20
- package/dist/src/api/saveToDesktop.js +0 -157
- package/dist/src/plan/brief.js +0 -2003
- package/dist/src/plan/changes.js +0 -217
- package/dist/src/plan/script.js +0 -1014
- package/dist/src/session/debugExport.js +0 -165
- package/dist/src/session/exportRecord.js +0 -277
- package/dist/src/session/metrics.js +0 -95
- package/dist/src/vcs/startChoice.js +0 -367
- package/dist/web/_next/static/chunks/197-f7c33a0114fc3db3.js +0 -1
- package/dist/web/_next/static/chunks/499-751e0be9d4f86e2d.js +0 -1
- package/dist/web/_next/static/chunks/549-a4473e9519a97026.js +0 -1
- package/dist/web/_next/static/chunks/699-9d031f8c145a0465.js +0 -1
- package/dist/web/_next/static/chunks/867-b159f25612da5cbc.js +0 -1
- package/dist/web/_next/static/chunks/897-5a34109533c3da50.js +0 -1
- package/dist/web/_next/static/chunks/app/appearance/page-a18019cdc395de3d.js +0 -1
- package/dist/web/_next/static/chunks/app/defaults/page-614be29d185a2947.js +0 -1
- package/dist/web/_next/static/chunks/app/history/page-0bb537407fed5944.js +0 -1
- package/dist/web/_next/static/chunks/app/import/page-8ef75fc08638e75f.js +0 -1
- package/dist/web/_next/static/chunks/app/layout-485ce9395cb17998.js +0 -1
- package/dist/web/_next/static/chunks/app/level1/page-dd3ebf37d29dce4f.js +0 -1
- package/dist/web/_next/static/chunks/app/page-3caf0b02046754a1.js +0 -1
- package/dist/web/_next/static/chunks/app/presets/page-93ab518db67e52a2.js +0 -1
- package/dist/web/_next/static/chunks/app/sessions/page-28376d2e4a9b4738.js +0 -1
- package/dist/web/_next/static/chunks/app/sessions/view/page-835d3533fdc63082.js +0 -1
- package/dist/web/_next/static/chunks/app/system/page-227a259e383ca2a0.js +0 -1
- package/dist/web/_next/static/css/ddb518572ea43e30.css +0 -1
- package/dist/web/_next/static/nLLy7-v8M5YRGIMtuTvMJ/_buildManifest.js +0 -1
- package/dist/web/history.html +0 -16
- package/dist/web/history.txt +0 -26
- package/dist/web/import.html +0 -16
- package/dist/web/import.txt +0 -26
- package/dist/web/sessions/view.html +0 -16
- package/dist/web/sessions/view.txt +0 -26
- package/dist/web/sessions.html +0 -16
- package/dist/web/sessions.txt +0 -26
- package/prompts/organisation.bg.md +0 -43
- package/prompts/organisation.en.md +0 -43
- package/prompts/persona.bg.md +0 -17
- package/prompts/persona.en.md +0 -17
- package/prompts/work.bg.md +0 -19
- package/prompts/work.en.md +0 -19
- /package/dist/web/_next/static/{nLLy7-v8M5YRGIMtuTvMJ → eqTUTH_SJtBgNgq8Zzy3n}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const WORDS = {
|
|
2
|
+
en: {
|
|
3
|
+
done: 'The task is done',
|
|
4
|
+
stopped: 'The task was stopped',
|
|
5
|
+
failed: 'The task ended unfinished',
|
|
6
|
+
notStarted: 'The task could not start',
|
|
7
|
+
doneLine: 'Copilot finished it. What was done is in the copilot-operator chat.',
|
|
8
|
+
stoppedLine: 'It was stopped; nothing more is sent.',
|
|
9
|
+
blockedLine: 'Copilot could not finish it',
|
|
10
|
+
failedLine: 'It ended with an error',
|
|
11
|
+
limitLine: 'It stopped at a limit from the settings',
|
|
12
|
+
notStartedLine: 'Nothing was sent',
|
|
13
|
+
statusOf: { done: 'done', aborted: 'stopped', blocked: 'blocked', failed: 'failed', 'limit-reached': 'at a limit', queued: 'not started' },
|
|
14
|
+
keep: 'This window stays open for your next message. You can close it at any time.',
|
|
15
|
+
ok: 'OK',
|
|
16
|
+
quote: (s) => `“${s}”`,
|
|
17
|
+
},
|
|
18
|
+
bg: {
|
|
19
|
+
done: 'Заданието е изпълнено',
|
|
20
|
+
stopped: 'Заданието е спряно',
|
|
21
|
+
failed: 'Заданието приключи незавършено',
|
|
22
|
+
notStarted: 'Заданието не можа да започне',
|
|
23
|
+
doneLine: 'Copilot го завърши. Какво е направено, виждате в чата на copilot-operator.',
|
|
24
|
+
stoppedLine: 'Спряно е; нищо повече не се изпраща.',
|
|
25
|
+
blockedLine: 'Copilot не успя да го завърши',
|
|
26
|
+
failedLine: 'Завърши с грешка',
|
|
27
|
+
limitLine: 'Спря при ограничение от настройките',
|
|
28
|
+
notStartedLine: 'Нищо не беше изпратено',
|
|
29
|
+
statusOf: { done: 'изпълнено', aborted: 'спряно', blocked: 'незавършено', failed: 'с грешка', 'limit-reached': 'при ограничение', queued: 'не започна' },
|
|
30
|
+
keep: 'Прозорецът остава отворен за следващото ви съобщение. Можете да го затворите по всяко време.',
|
|
31
|
+
ok: 'ОК',
|
|
32
|
+
quote: (s) => `„${s}“`,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
/** A reason as a line: its first sentence or so, since the whole of it is in the chat. */
|
|
36
|
+
function brief(text, max = 220) {
|
|
37
|
+
const one = (text ?? '').replace(/\s+/g, ' ').trim();
|
|
38
|
+
return one.length > max ? `${one.slice(0, max - 1).trimEnd()}…` : one;
|
|
39
|
+
}
|
|
40
|
+
/** The card for a run of the chat that has ended: how its message(s) ended, and that the window stays open. */
|
|
41
|
+
export function chatNotice(lang, tasks, error) {
|
|
42
|
+
const w = WORDS[lang] ?? WORDS.en;
|
|
43
|
+
const ran = tasks.filter((t) => t.status !== 'queued');
|
|
44
|
+
const unfinished = ran.filter((t) => t.status !== 'done' && t.status !== 'aborted');
|
|
45
|
+
const stopped = ran.some((t) => t.status === 'aborted');
|
|
46
|
+
const kind = unfinished.length > 0 ? 'failed' : stopped ? 'stopped' : ran.length > 0 ? 'done' : error ? 'notStarted' : 'stopped';
|
|
47
|
+
const subject = tasks.length === 1 ? w.quote(tasks[0].title) : undefined;
|
|
48
|
+
const lines = [];
|
|
49
|
+
if (tasks.length > 1) {
|
|
50
|
+
for (const t of tasks)
|
|
51
|
+
lines.push(`${w.quote(t.title)} — ${w.statusOf[t.status] ?? t.status}`);
|
|
52
|
+
}
|
|
53
|
+
else if (kind === 'done') {
|
|
54
|
+
lines.push(w.doneLine);
|
|
55
|
+
}
|
|
56
|
+
else if (kind === 'stopped') {
|
|
57
|
+
lines.push(w.stoppedLine);
|
|
58
|
+
}
|
|
59
|
+
else if (kind === 'notStarted') {
|
|
60
|
+
lines.push(`${w.notStartedLine}: ${brief(error)}`);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const t = unfinished[0];
|
|
64
|
+
const said = t.status === 'blocked' ? w.blockedLine : t.status === 'limit-reached' ? w.limitLine : w.failedLine;
|
|
65
|
+
const why = brief(t.status === 'blocked' ? t.summary || t.reason : t.reason || t.summary);
|
|
66
|
+
lines.push(why ? `${said}: ${why}` : `${said}.`);
|
|
67
|
+
}
|
|
68
|
+
lines.push(w.keep);
|
|
69
|
+
return {
|
|
70
|
+
tone: kind === 'done' ? 'done' : kind === 'stopped' ? 'stopped' : 'failed',
|
|
71
|
+
title: w[kind],
|
|
72
|
+
...(subject ? { subject } : {}),
|
|
73
|
+
lines,
|
|
74
|
+
button: w.ok,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -192,8 +192,6 @@ export class SessionStore {
|
|
|
192
192
|
dir;
|
|
193
193
|
sessionsDir;
|
|
194
194
|
presetsDir;
|
|
195
|
-
/** Named personas the operator keeps for the import page, one file each. */
|
|
196
|
-
personasDir;
|
|
197
195
|
level1Path;
|
|
198
196
|
modelsPath;
|
|
199
197
|
constructor(dataDir, defaultLevel1Path) {
|
|
@@ -201,7 +199,6 @@ export class SessionStore {
|
|
|
201
199
|
this.dir = resolve(dataDir);
|
|
202
200
|
this.sessionsDir = join(this.dir, 'sessions');
|
|
203
201
|
this.presetsDir = join(this.dir, 'level2');
|
|
204
|
-
this.personasDir = join(this.dir, 'personas');
|
|
205
202
|
this.level1Path = join(this.dir, 'level1.md');
|
|
206
203
|
this.modelsPath = join(this.dir, 'models.json');
|
|
207
204
|
}
|
|
@@ -223,28 +220,6 @@ export class SessionStore {
|
|
|
223
220
|
async resetLevel1() {
|
|
224
221
|
await removeFile(this.level1Path);
|
|
225
222
|
}
|
|
226
|
-
// --- the organisation's part of the plan persona -------------------------------------
|
|
227
|
-
/**
|
|
228
|
-
* The operator's own part of the plan brief: where tickets come from, what to search, how
|
|
229
|
-
* the machine is laid out. One customised copy in `data/organisation.md`, whatever language
|
|
230
|
-
* it is written in; the shipped default comes per language from the prompts folder.
|
|
231
|
-
*/
|
|
232
|
-
async getContext(kind, lang) {
|
|
233
|
-
const custom = join(this.dir, `context-${kind}.md`);
|
|
234
|
-
const shipped = join(this.defaultLevel1Path, '..', `${kind}.${lang}.md`);
|
|
235
|
-
const example = await readFile(shipped, 'utf8').catch(() => '');
|
|
236
|
-
if (existsSync(custom))
|
|
237
|
-
return { content: await readFile(custom, 'utf8'), customised: true, example };
|
|
238
|
-
// Nothing until the operator writes it: an empty field is what makes the persona ask.
|
|
239
|
-
// The shipped text is the example it is shown, not the answer.
|
|
240
|
-
return { content: '', customised: false, example };
|
|
241
|
-
}
|
|
242
|
-
async setContext(kind, content) {
|
|
243
|
-
await writeFileAtomically(join(this.dir, `context-${kind}.md`), content);
|
|
244
|
-
}
|
|
245
|
-
async resetContext(kind) {
|
|
246
|
-
await removeFile(join(this.dir, `context-${kind}.md`));
|
|
247
|
-
}
|
|
248
223
|
// --- the model catalogue ----------------------------------------------------------------
|
|
249
224
|
/** What the chat's picker offered when it was last read, or null if it never was. */
|
|
250
225
|
async getModels() {
|
|
@@ -277,8 +252,8 @@ export class SessionStore {
|
|
|
277
252
|
*
|
|
278
253
|
* Stripping the characters a file name cannot hold is not enough on its own: a leading dot and
|
|
279
254
|
* ".." survive it, and `safePresetName` refuses both, so ".env" and "a..b" were saved and then
|
|
280
|
-
* could never be deleted. The name goes through the same rule as the delete,
|
|
281
|
-
*
|
|
255
|
+
* could never be deleted. The name goes through the same rule as the delete, so a name that could
|
|
256
|
+
* not be removed is refused here instead.
|
|
282
257
|
*/
|
|
283
258
|
async savePreset(name, content) {
|
|
284
259
|
const safe = name.replace(/[^\p{L}\p{N}._ -]/gu, '').trim();
|
|
@@ -290,36 +265,6 @@ export class SessionStore {
|
|
|
290
265
|
async deletePreset(name) {
|
|
291
266
|
await removeFile(await namedFile(this.presetsDir, name));
|
|
292
267
|
}
|
|
293
|
-
// --- named personas -------------------------------------------------------------------
|
|
294
|
-
//
|
|
295
|
-
// "How the tasks are carried out" is one box on the import page, and the persona in it depends
|
|
296
|
-
// on the kind of work: a Playwright suite wants a different agent from an API service. These
|
|
297
|
-
// are saved copies of that box under a name, so switching is one choice instead of pasting a
|
|
298
|
-
// JSON back in. The box itself (`context-persona.md`) stays the one that is in force.
|
|
299
|
-
async listPersonas() {
|
|
300
|
-
const names = (await readdir(this.personasDir).catch(() => []))
|
|
301
|
-
.filter((n) => n.endsWith('.md'))
|
|
302
|
-
.sort((a, b) => a.localeCompare(b));
|
|
303
|
-
const out = [];
|
|
304
|
-
for (const n of names) {
|
|
305
|
-
const path = join(this.personasDir, n);
|
|
306
|
-
const content = await readFile(path, 'utf8');
|
|
307
|
-
const { mtime } = await import('node:fs').then((fs) => fs.statSync(path));
|
|
308
|
-
out.push({ name: n.slice(0, -3), content, updatedAt: mtime.toISOString() });
|
|
309
|
-
}
|
|
310
|
-
return out;
|
|
311
|
-
}
|
|
312
|
-
async savePersona(name, content) {
|
|
313
|
-
const safe = name.replace(/[^\p{L}\p{N}._ -]/gu, '').trim();
|
|
314
|
-
if (!safe)
|
|
315
|
-
throw new Error('The persona name is empty after removing unsafe characters.');
|
|
316
|
-
await mkdir(this.personasDir, { recursive: true });
|
|
317
|
-
await writeFileAtomically(join(this.personasDir, `${safePresetName(safe)}.md`), content);
|
|
318
|
-
return { name: safe, content, updatedAt: new Date().toISOString() };
|
|
319
|
-
}
|
|
320
|
-
async deletePersona(name) {
|
|
321
|
-
await removeFile(await namedFile(this.personasDir, name));
|
|
322
|
-
}
|
|
323
268
|
// --- sessions -------------------------------------------------------------------------
|
|
324
269
|
/**
|
|
325
270
|
* Every session, newest first — by when it was made, not by what it is called.
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The workspace: the project folder the workbench has open (Settings → project folder), read as it is on disk —
|
|
3
3
|
* the working tree, not a commit — for the explorer and the editor, the way an IDE shows the folder it opened
|
|
4
|
-
* (operator's request, 2026-10-17: "like Windsurf").
|
|
4
|
+
* (operator's request, 2026-10-17: "like Windsurf"). One thing writes to it: the editor saving a file the operator
|
|
5
|
+
* changed (2026-10-20, `saveWorkspaceFile`), and only a file that is already there, as it was when it was opened.
|
|
5
6
|
*/
|
|
7
|
+
import { createHash } from 'node:crypto';
|
|
6
8
|
import { existsSync, statSync } from 'node:fs';
|
|
7
|
-
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
9
|
+
import { readdir, readFile, rename, stat, writeFile } from 'node:fs/promises';
|
|
8
10
|
import { basename, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
9
11
|
import { git, looksLikeRepo, repoState } from '../vcs/git.js';
|
|
10
12
|
export async function workspaceInfo(dir, name = '') {
|
|
@@ -53,13 +55,24 @@ export async function workspaceFiles(dir) {
|
|
|
53
55
|
await walk('');
|
|
54
56
|
return { files: files.sort(), truncated: files.length >= MAX_FILES };
|
|
55
57
|
}
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
+
/** The largest file the editor reads and writes. */
|
|
59
|
+
const MAX_EDIT_BYTES = 2 * 1024 * 1024;
|
|
60
|
+
/** A path of the folder, cleaned, and where it is on disk; anything outside it, or in git's own folder, is refused. */
|
|
61
|
+
function insideFolder(dir, path) {
|
|
58
62
|
const clean = path.replace(/\\/g, '/').replace(/^\/+/, '');
|
|
59
63
|
const full = resolve(dir, clean);
|
|
60
64
|
const rel = relative(resolve(dir), full);
|
|
61
65
|
if (!clean || isAbsolute(clean) || rel.startsWith('..') || rel.split(sep).includes('.git'))
|
|
62
66
|
throw new Error('That path is not inside the project folder.');
|
|
67
|
+
return { clean, full };
|
|
68
|
+
}
|
|
69
|
+
/** A file's bytes as a short fingerprint: which version of it an edit was made on (see `saveWorkspaceFile`). */
|
|
70
|
+
function versionOf(bytes) {
|
|
71
|
+
return createHash('sha256').update(bytes).digest('hex').slice(0, 20);
|
|
72
|
+
}
|
|
73
|
+
/** One file of the folder as it is on disk, as text; a binary or very large one is said to be one. */
|
|
74
|
+
export async function workspaceFile(dir, path, maxBytes = MAX_EDIT_BYTES) {
|
|
75
|
+
const { clean, full } = insideFolder(dir, path);
|
|
63
76
|
const st = await stat(full).catch(() => null);
|
|
64
77
|
if (!st?.isFile())
|
|
65
78
|
throw new Error(`There is no file ${clean} in the project folder.`);
|
|
@@ -67,5 +80,39 @@ export async function workspaceFile(dir, path, maxBytes = 2 * 1024 * 1024) {
|
|
|
67
80
|
return { path: clean, text: null, binary: false, tooLarge: true, size: st.size };
|
|
68
81
|
const bytes = await readFile(full);
|
|
69
82
|
const binary = bytes.subarray(0, 8000).includes(0);
|
|
70
|
-
return { path: clean, text: binary ? null : bytes.toString('utf8').replace(
|
|
83
|
+
return { path: clean, text: binary ? null : bytes.toString('utf8').replace(/^\ufeff/, ''), binary, tooLarge: false, size: st.size, version: versionOf(bytes) };
|
|
84
|
+
}
|
|
85
|
+
/** Said when the file changed on disk after the editor read it; the page offers to read it again or to save anyway. */
|
|
86
|
+
export const CHANGED_ON_DISK = 'changed-on-disk';
|
|
87
|
+
/**
|
|
88
|
+
* Saves the editor's text over a file of the folder (2026-10-20: "clicking a file, it can be edited"). Only a file that
|
|
89
|
+
* is there already and is text; and only over the version the edit was made on (`base`), so a change the chat made to
|
|
90
|
+
* it meanwhile is not overwritten without the operator knowing — without `base` it is saved whatever is there. The
|
|
91
|
+
* file keeps its own line endings and its byte-order mark: the editor works in LF, and a CRLF file stays CRLF.
|
|
92
|
+
*/
|
|
93
|
+
export async function saveWorkspaceFile(dir, path, text, base) {
|
|
94
|
+
const { clean, full } = insideFolder(dir, path);
|
|
95
|
+
const st = await stat(full).catch(() => null);
|
|
96
|
+
if (!st?.isFile())
|
|
97
|
+
throw new Error(`There is no file ${clean} in the project folder.`);
|
|
98
|
+
if (st.size > MAX_EDIT_BYTES)
|
|
99
|
+
throw new Error(`${clean} is too large to edit here.`);
|
|
100
|
+
const before = await readFile(full);
|
|
101
|
+
if (before.subarray(0, 8000).includes(0))
|
|
102
|
+
throw new Error(`${clean} is not a text file.`);
|
|
103
|
+
if (base && versionOf(before) !== base) {
|
|
104
|
+
throw Object.assign(new Error(`${clean} changed on disk after it was opened. Read it again to see the change, or save over it.`), { code: CHANGED_ON_DISK });
|
|
105
|
+
}
|
|
106
|
+
const old = before.toString('utf8');
|
|
107
|
+
const bom = old.startsWith('\ufeff') ? '\ufeff' : '';
|
|
108
|
+
const crlf = (old.match(/\r\n/g)?.length ?? 0) > (old.match(/(?<!\r)\n/g)?.length ?? 0);
|
|
109
|
+
const lf = text.replace(/\r\n/g, '\n');
|
|
110
|
+
const bytes = Buffer.from(bom + (crlf ? lf.replace(/\n/g, '\r\n') : lf), 'utf8');
|
|
111
|
+
if (bytes.length > MAX_EDIT_BYTES)
|
|
112
|
+
throw new Error(`The text is too large to save (${bytes.length} bytes).`);
|
|
113
|
+
// Written beside it and moved over it, so a file is never left half written.
|
|
114
|
+
const temp = `${full}.cop-save-${process.pid}`;
|
|
115
|
+
await writeFile(temp, bytes);
|
|
116
|
+
await rename(temp, full);
|
|
117
|
+
return { path: clean, version: versionOf(bytes), size: bytes.length };
|
|
71
118
|
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The open project folder's git, as an IDE's Source Control view shows it (operator's request, 2026-10-19: "see
|
|
3
|
+
* which files changed and how — the diff; let the Commits button show Git as VS Code does"):
|
|
4
|
+
*
|
|
5
|
+
* - the changes: what is staged (the index against HEAD) and what is not (the working tree against the index), the
|
|
6
|
+
* files git does not track yet among them, each with its letter and its lines added and removed;
|
|
7
|
+
* - the history of the branch, newest first, and for a commit the files it changed;
|
|
8
|
+
* - for any of those files, its two versions, for the side-by-side view.
|
|
9
|
+
*
|
|
10
|
+
* Read-only, every command of it: nothing here stages, commits, checks out or writes a file. A file's two versions
|
|
11
|
+
* are given only for a file the list itself names, so the view cannot be pointed at anything else in the repository
|
|
12
|
+
* or outside it.
|
|
13
|
+
*/
|
|
14
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
15
|
+
import { isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
16
|
+
import { fileAt, git, gitBytes, looksLikeRepo } from '../vcs/git.js';
|
|
17
|
+
/** git's empty tree: what a repository with no commit yet is compared with. */
|
|
18
|
+
const EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
|
|
19
|
+
const MAX_FILE = 2 * 1024 * 1024;
|
|
20
|
+
const MAX_COMMITS = 200;
|
|
21
|
+
const SHA = /^[0-9a-f]{7,40}$/i;
|
|
22
|
+
async function hasHead(dir) {
|
|
23
|
+
return (await git(dir, ['rev-parse', '--verify', '--quiet', 'HEAD'])).ok;
|
|
24
|
+
}
|
|
25
|
+
/** `--name-status -z` read into files, renames and copies with both their paths. */
|
|
26
|
+
function nameStatus(out) {
|
|
27
|
+
const tokens = out.split('\0');
|
|
28
|
+
const files = [];
|
|
29
|
+
for (let i = 0; i < tokens.length;) {
|
|
30
|
+
const status = tokens[i] ?? '';
|
|
31
|
+
if (!status)
|
|
32
|
+
break;
|
|
33
|
+
const letter = status[0];
|
|
34
|
+
if (letter === 'R' || letter === 'C') {
|
|
35
|
+
files.push({ status: letter, oldPath: tokens[i + 1] ?? '', path: tokens[i + 2] ?? '', added: 0, removed: 0 });
|
|
36
|
+
i += 3;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
files.push({ status: letter, path: tokens[i + 1] ?? '', added: 0, removed: 0 });
|
|
40
|
+
i += 2;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return files;
|
|
44
|
+
}
|
|
45
|
+
/** `--numstat -z` counted into the files it names. */
|
|
46
|
+
function addCounts(files, out) {
|
|
47
|
+
const parts = out.split('\0');
|
|
48
|
+
for (let i = 0; i < parts.length;) {
|
|
49
|
+
const head = parts[i] ?? '';
|
|
50
|
+
if (!head)
|
|
51
|
+
break;
|
|
52
|
+
const [added = '0', removed = '0', path = ''] = head.split('\t');
|
|
53
|
+
// A rename is "added<TAB>removed<TAB>" followed by the old path and the new one.
|
|
54
|
+
const newPath = path === '' ? (parts[i + 2] ?? '') : path;
|
|
55
|
+
i += path === '' ? 3 : 1;
|
|
56
|
+
const file = files.find((f) => f.path === newPath);
|
|
57
|
+
if (file) {
|
|
58
|
+
file.added = added === '-' ? -1 : Number(added) || 0;
|
|
59
|
+
file.removed = removed === '-' ? -1 : Number(removed) || 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function diffFiles(dir, args) {
|
|
64
|
+
const names = await gitBytes(dir, ['diff', '--name-status', '-M', '-z', ...args]);
|
|
65
|
+
if (!names.ok)
|
|
66
|
+
return [];
|
|
67
|
+
const files = nameStatus(names.stdout.toString('utf8'));
|
|
68
|
+
const counts = await gitBytes(dir, ['diff', '--numstat', '-M', '-z', ...args]);
|
|
69
|
+
if (counts.ok)
|
|
70
|
+
addCounts(files, counts.stdout.toString('utf8'));
|
|
71
|
+
return files;
|
|
72
|
+
}
|
|
73
|
+
/** A file git does not track yet: all of it added, counted in lines when it is text. */
|
|
74
|
+
async function untracked(dir, path) {
|
|
75
|
+
const full = join(dir, path);
|
|
76
|
+
const info = await stat(full).catch(() => null);
|
|
77
|
+
let added = 0;
|
|
78
|
+
if (info?.isFile() && info.size <= MAX_FILE) {
|
|
79
|
+
const bytes = await readFile(full).catch(() => Buffer.alloc(0));
|
|
80
|
+
if (bytes.subarray(0, 8000).includes(0))
|
|
81
|
+
added = -1;
|
|
82
|
+
else {
|
|
83
|
+
const text = bytes.toString('utf8');
|
|
84
|
+
added = text === '' ? 0 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { status: 'U', path, added, removed: added < 0 ? -1 : 0 };
|
|
88
|
+
}
|
|
89
|
+
/** The folder's git as the Source Control view shows it. */
|
|
90
|
+
export async function workspaceGit(dir) {
|
|
91
|
+
const none = { dir, isRepo: false, staged: [], changes: [], commits: [], moreCommits: false };
|
|
92
|
+
if (!dir || !looksLikeRepo(dir))
|
|
93
|
+
return none;
|
|
94
|
+
const head = await hasHead(dir);
|
|
95
|
+
const branch = (await git(dir, ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout || undefined;
|
|
96
|
+
const sha = head ? (await git(dir, ['rev-parse', 'HEAD'])).stdout : undefined;
|
|
97
|
+
const staged = await diffFiles(dir, ['--cached', head ? 'HEAD' : EMPTY_TREE]);
|
|
98
|
+
const unstaged = await diffFiles(dir, []);
|
|
99
|
+
// In conflict: git's `U` in `--name-status`, said as the IDE says it.
|
|
100
|
+
for (const f of [...staged, ...unstaged])
|
|
101
|
+
if (f.status === 'U')
|
|
102
|
+
f.status = '!';
|
|
103
|
+
const others = await gitBytes(dir, ['ls-files', '--others', '--exclude-standard', '-z']);
|
|
104
|
+
const fresh = others.ok ? others.stdout.toString('utf8').split('\0').filter(Boolean).sort().slice(0, 2000) : [];
|
|
105
|
+
const changes = [...unstaged, ...(await Promise.all(fresh.map((p) => untracked(dir, p))))];
|
|
106
|
+
let upstream;
|
|
107
|
+
const up = await git(dir, ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}']);
|
|
108
|
+
if (up.ok && up.stdout) {
|
|
109
|
+
const counts = await git(dir, ['rev-list', '--left-right', '--count', `${up.stdout}...HEAD`]);
|
|
110
|
+
const [behind = '0', ahead = '0'] = counts.stdout.split(/\s+/);
|
|
111
|
+
upstream = { name: up.stdout, ahead: Number(ahead) || 0, behind: Number(behind) || 0 };
|
|
112
|
+
}
|
|
113
|
+
const commits = [];
|
|
114
|
+
if (head) {
|
|
115
|
+
const log = await gitBytes(dir, ['log', `-n${MAX_COMMITS + 1}`, '--format=%H%x1f%h%x1f%an%x1f%aI%x1f%s%x1e']);
|
|
116
|
+
if (log.ok) {
|
|
117
|
+
for (const record of log.stdout.toString('utf8').split('\x1e')) {
|
|
118
|
+
const [full = '', short = '', author = '', at = '', subject = ''] = record.trim().split('\x1f');
|
|
119
|
+
if (full)
|
|
120
|
+
commits.push({ sha: full, short, author, at, subject });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
dir,
|
|
126
|
+
isRepo: true,
|
|
127
|
+
...(branch ? { branch } : {}),
|
|
128
|
+
...(sha ? { head: sha } : {}),
|
|
129
|
+
...(upstream ? { upstream } : {}),
|
|
130
|
+
staged,
|
|
131
|
+
changes,
|
|
132
|
+
commits: commits.slice(0, MAX_COMMITS),
|
|
133
|
+
moreCommits: commits.length > MAX_COMMITS,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** One commit: who, when, what it says, and the files it changed (a first commit against nothing). */
|
|
137
|
+
export async function workspaceCommit(dir, sha) {
|
|
138
|
+
if (!SHA.test(sha))
|
|
139
|
+
throw new Error('That is not a commit.');
|
|
140
|
+
if (!looksLikeRepo(dir))
|
|
141
|
+
throw new Error('The project folder is not a git repository.');
|
|
142
|
+
const info = await gitBytes(dir, ['show', '-s', '--format=%H%x1f%h%x1f%an%x1f%aI%x1f%s%x1f%b', sha]);
|
|
143
|
+
if (!info.ok)
|
|
144
|
+
throw new Error(`There is no commit ${sha} in the project folder.`);
|
|
145
|
+
const [full = '', short = '', author = '', at = '', subject = '', body = ''] = info.stdout.toString('utf8').split('\x1f');
|
|
146
|
+
const parent = await git(dir, ['rev-parse', '--verify', '--quiet', `${full}^`]);
|
|
147
|
+
const files = await diffFiles(dir, [parent.ok ? `${full}^` : EMPTY_TREE, full]);
|
|
148
|
+
return { sha: full, short, author, at, subject, body: body.trim(), files };
|
|
149
|
+
}
|
|
150
|
+
/** A path the view was given, as git writes it, or why it is not one. */
|
|
151
|
+
function cleanPath(dir, path) {
|
|
152
|
+
const clean = path.replace(/\\/g, '/').replace(/^\/+/, '');
|
|
153
|
+
const rel = relative(resolve(dir), resolve(dir, clean));
|
|
154
|
+
if (!clean || isAbsolute(clean) || rel.startsWith('..') || rel.split(sep).includes('.git'))
|
|
155
|
+
throw new Error('That path is not inside the project folder.');
|
|
156
|
+
return clean;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The two versions of one file the view lists: for a staged change, HEAD and the index; for a change not staged, the
|
|
160
|
+
* index and the working tree (a file git does not track: nothing, and the file on disk); for a commit's file, its
|
|
161
|
+
* parent's version and the commit's.
|
|
162
|
+
*/
|
|
163
|
+
export async function workspaceGitVersions(dir, area, path, commit) {
|
|
164
|
+
const want = cleanPath(dir, path);
|
|
165
|
+
let file;
|
|
166
|
+
let before = { bytes: null, tooLarge: false };
|
|
167
|
+
let after = { bytes: null, tooLarge: false };
|
|
168
|
+
if (area === 'commit') {
|
|
169
|
+
const c = await workspaceCommit(dir, commit ?? '');
|
|
170
|
+
file = c.files.find((f) => f.path === want);
|
|
171
|
+
if (!file)
|
|
172
|
+
throw new Error('That file is not one this commit changed.');
|
|
173
|
+
const parent = await git(dir, ['rev-parse', '--verify', '--quiet', `${c.sha}^`]);
|
|
174
|
+
if (file.status !== 'A' && parent.ok)
|
|
175
|
+
before = await fileAt(dir, `${c.sha}^`, file.oldPath ?? file.path, MAX_FILE);
|
|
176
|
+
if (file.status !== 'D')
|
|
177
|
+
after = await fileAt(dir, c.sha, file.path, MAX_FILE);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
const state = await workspaceGit(dir);
|
|
181
|
+
file = (area === 'staged' ? state.staged : state.changes).find((f) => f.path === want);
|
|
182
|
+
if (!file)
|
|
183
|
+
throw new Error(area === 'staged' ? 'That file has no staged change.' : 'That file has no change in the working tree.');
|
|
184
|
+
if (area === 'staged') {
|
|
185
|
+
if (file.status !== 'A' && state.head)
|
|
186
|
+
before = await fileAt(dir, 'HEAD', file.oldPath ?? file.path, MAX_FILE);
|
|
187
|
+
if (file.status !== 'D')
|
|
188
|
+
after = await fileAt(dir, '', file.path, MAX_FILE);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
// Against the index, which is HEAD's version where nothing is staged; a file git does not track has neither.
|
|
192
|
+
if (file.status !== 'U')
|
|
193
|
+
before = await fileAt(dir, '', file.oldPath ?? file.path, MAX_FILE);
|
|
194
|
+
if (file.status !== 'D') {
|
|
195
|
+
const info = await stat(join(dir, file.path)).catch(() => null);
|
|
196
|
+
if (info?.isFile())
|
|
197
|
+
after = info.size > MAX_FILE ? { bytes: null, tooLarge: true } : { bytes: await readFile(join(dir, file.path)), tooLarge: false };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const binary = [before.bytes, after.bytes].some((b) => !!b && b.subarray(0, 8000).includes(0));
|
|
202
|
+
const text = (b) => (b === null || binary ? null : b.toString('utf8').replace(/^/, ''));
|
|
203
|
+
return {
|
|
204
|
+
path: file.path,
|
|
205
|
+
...(file.oldPath ? { oldPath: file.oldPath } : {}),
|
|
206
|
+
status: file.status,
|
|
207
|
+
before: text(before.bytes),
|
|
208
|
+
after: text(after.bytes),
|
|
209
|
+
binary,
|
|
210
|
+
tooLarge: before.tooLarge || after.tooLarge,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
@@ -31,6 +31,25 @@ export function chatCode(sessionId) {
|
|
|
31
31
|
const tail = /-([a-z0-9]{3,8})$/i.exec(sessionId)?.[1];
|
|
32
32
|
return (tail ?? sessionId.replace(/[^a-z0-9]/gi, '').slice(-6)).toLowerCase();
|
|
33
33
|
}
|
|
34
|
+
/** Cyrillic letters in Latin ones: the Bulgarian streamlined system, with the Russian and Ukrainian letters it lacks. */
|
|
35
|
+
const CYRILLIC = {
|
|
36
|
+
а: 'a', б: 'b', в: 'v', г: 'g', д: 'd', е: 'e', ж: 'zh', з: 'z', и: 'i', й: 'y', к: 'k', л: 'l', м: 'm', н: 'n',
|
|
37
|
+
о: 'o', п: 'p', р: 'r', с: 's', т: 't', у: 'u', ф: 'f', х: 'h', ц: 'ts', ч: 'ch', ш: 'sh', щ: 'sht', ъ: 'a', ь: 'y',
|
|
38
|
+
ю: 'yu', я: 'ya', ё: 'yo', ы: 'y', э: 'e', є: 'ye', і: 'i', ї: 'yi', ґ: 'g',
|
|
39
|
+
};
|
|
40
|
+
/** A text in Latin letters: Cyrillic transliterated, accents taken off; what is left of other scripts is kept. */
|
|
41
|
+
export function latin(text) {
|
|
42
|
+
let out = '';
|
|
43
|
+
for (const ch of text) {
|
|
44
|
+
const lower = ch.toLowerCase();
|
|
45
|
+
const mapped = CYRILLIC[lower];
|
|
46
|
+
if (mapped === undefined)
|
|
47
|
+
out += ch;
|
|
48
|
+
else
|
|
49
|
+
out += ch === lower ? mapped : mapped.charAt(0).toUpperCase() + mapped.slice(1);
|
|
50
|
+
}
|
|
51
|
+
return out.normalize('NFKD').replace(/[\u0300-\u036f]/g, '');
|
|
52
|
+
}
|
|
34
53
|
/**
|
|
35
54
|
* Builds the chat name. Shape:
|
|
36
55
|
*
|
|
@@ -50,11 +69,16 @@ export function buildChatName(runId, label) {
|
|
|
50
69
|
return `op/${runId}`.slice(0, MAX_CHAT_NAME);
|
|
51
70
|
}
|
|
52
71
|
const room = MAX_CHAT_NAME - prefix.length;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
72
|
+
/*
|
|
73
|
+
* In Latin letters, Cyrillic transliterated ("Създай калкулатор" gives "Sazday-kalkulator"). Copilot's rename dialog
|
|
74
|
+
* refused a Bulgarian name of 50 letters, and then the same cut to 50 bytes, and took only a short Latin one: two
|
|
75
|
+
* refusals before every first message of a new chat (operator, 2026-10-19: "why two attempts — should it not be
|
|
76
|
+
* one?"). An ASCII name is the same length by any count, so the first name offered is the one it takes; the shorter
|
|
77
|
+
* forms of `chatNameForms` stay for a refusal nobody expects.
|
|
78
|
+
*/
|
|
79
|
+
const clean = latin(label)
|
|
56
80
|
.replace(/\s+/g, '-')
|
|
57
|
-
.replace(/[
|
|
81
|
+
.replace(/[^A-Za-z0-9._-]/g, '')
|
|
58
82
|
.replace(/-{2,}/g, '-')
|
|
59
83
|
.replace(/^-|-$/g, '')
|
|
60
84
|
.slice(0, room);
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* running in this process — a check script — can call `setTransportFactory`.
|
|
21
21
|
*/
|
|
22
22
|
import { CopilotTransport } from './copilotTransport.js';
|
|
23
|
-
export { ReplyTimeoutError, isReplyTimeout } from './copilotTransport.js';
|
|
23
|
+
export { ReplyTimeoutError, isReplyTimeout, WindowClosed } from './copilotTransport.js';
|
|
24
24
|
const realChat = (opts) => new CopilotTransport(opts);
|
|
25
25
|
let factory = realChat;
|
|
26
26
|
/** A chat for these options: the real Copilot window, unless a check has put a scripted one in. */
|