dsh-git-desk 0.18.1 → 0.20.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.
- package/README.md +11 -0
- package/client.js +5 -4847
- package/dist/contract.js +5 -0
- package/{index.js → dist/host/index.js} +4 -5
- package/dist/host/lib/ai.js +266 -0
- package/dist/host/lib/exec.js +193 -0
- package/dist/host/lib/git.js +1317 -0
- package/dist/host/lib/http.js +168 -0
- package/dist/host/lib/parse.js +381 -0
- package/dist/host/lib/rpc.js +316 -0
- package/dist/host/lib/types.js +1 -0
- package/package.json +18 -4
- package/lib/ai.js +0 -187
- package/lib/exec.js +0 -168
- package/lib/git.js +0 -1037
- package/lib/http.js +0 -139
- package/lib/parse.js +0 -319
- package/lib/rpc.js +0 -266
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { preferences, writeCommitMessage } from './ai.js';
|
|
2
|
+
import { GitError } from './exec.js';
|
|
3
|
+
import { applyBlocks, branches, checkout, cherryPickCommit, commit, commitDetail, createBranch, createBranchAndCommit, discoverRepositories, diff, fetch, findRepository, initRepository, log, pull, push, recentSubjects, resetToCommit, resolveConflict, resolveRepository, revertCommit, stagePaths, stagedPatch, stashApply, stashDrop, stashList, stashPush, status, undoCommit, unstagePaths, } from './git.js';
|
|
4
|
+
/**
|
|
5
|
+
* Read one field from a client payload.
|
|
6
|
+
* @param payload the payload.
|
|
7
|
+
* @param key the field name.
|
|
8
|
+
* @returns the field value.
|
|
9
|
+
*/
|
|
10
|
+
function valueOf(payload, key) {
|
|
11
|
+
return payload !== null && typeof payload === 'object' ? payload[key] : undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Read one required string field.
|
|
15
|
+
* @param payload the payload.
|
|
16
|
+
* @param key the field name.
|
|
17
|
+
* @returns the field.
|
|
18
|
+
*/
|
|
19
|
+
function stringField(payload, key) {
|
|
20
|
+
const value = valueOf(payload, key);
|
|
21
|
+
if (typeof value !== 'string' || value === '') {
|
|
22
|
+
throw new GitError('git/bad-request', `The request is missing "${key}".`);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Read one optional string field.
|
|
28
|
+
* @param payload the payload.
|
|
29
|
+
* @param key the field name.
|
|
30
|
+
* @returns the field, or undefined.
|
|
31
|
+
*/
|
|
32
|
+
function optionalString(payload, key) {
|
|
33
|
+
const value = valueOf(payload, key);
|
|
34
|
+
return typeof value === 'string' && value !== '' ? value : undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Read one list of path strings.
|
|
38
|
+
* @param payload the payload.
|
|
39
|
+
* @param key the field name.
|
|
40
|
+
* @returns the paths, empty when the field is absent.
|
|
41
|
+
*/
|
|
42
|
+
function pathsField(payload, key) {
|
|
43
|
+
const value = valueOf(payload, key);
|
|
44
|
+
if (!Array.isArray(value))
|
|
45
|
+
return [];
|
|
46
|
+
return value.filter((item) => typeof item === 'string' && item !== '');
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Read the diff side a request names.
|
|
50
|
+
* @param payload the payload.
|
|
51
|
+
* @returns the side.
|
|
52
|
+
*/
|
|
53
|
+
function sideField(payload) {
|
|
54
|
+
const value = valueOf(payload, 'side');
|
|
55
|
+
if (value !== 'unstaged' && value !== 'staged' && value !== 'conflict' && value !== 'log') {
|
|
56
|
+
throw new GitError('git/invalid-side', 'Unknown diff side.');
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolve the workspace a request targets.
|
|
62
|
+
* @param ctx the host context.
|
|
63
|
+
* @param payload the client payload.
|
|
64
|
+
* @returns the workspace.
|
|
65
|
+
*/
|
|
66
|
+
function workspaceOf(ctx, payload) {
|
|
67
|
+
const id = valueOf(payload, 'workspaceId');
|
|
68
|
+
if (typeof id !== 'string' || id === '') {
|
|
69
|
+
throw new GitError('git/no-workspace', 'Choose a workspace first.');
|
|
70
|
+
}
|
|
71
|
+
const workspace = ctx.workspaceRegistry.get(id);
|
|
72
|
+
if (workspace === undefined) {
|
|
73
|
+
throw new GitError('git/no-workspace', 'That workspace is no longer registered.');
|
|
74
|
+
}
|
|
75
|
+
return workspace;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Resolve the repository a request targets.
|
|
79
|
+
* @param runtime the runtime helpers.
|
|
80
|
+
* @param workspace the workspace.
|
|
81
|
+
* @param payload the client payload.
|
|
82
|
+
* @returns the repository root.
|
|
83
|
+
*/
|
|
84
|
+
async function repoOf(runtime, workspace, payload) {
|
|
85
|
+
const requested = valueOf(payload, 'repo');
|
|
86
|
+
if (typeof requested === 'string' && requested !== '') {
|
|
87
|
+
return resolveRepository(runtime, workspace.path, requested);
|
|
88
|
+
}
|
|
89
|
+
const root = await findRepository(runtime, workspace.path);
|
|
90
|
+
if (root === null) {
|
|
91
|
+
throw new GitError('git/not-repo', 'This workspace is not a Git repository.', {
|
|
92
|
+
path: workspace.path,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return root;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Name one workspace for the panel.
|
|
99
|
+
* @param workspace the workspace.
|
|
100
|
+
* @returns the panel shape.
|
|
101
|
+
*/
|
|
102
|
+
function workspaceInfo(workspace) {
|
|
103
|
+
return { id: workspace.id, title: workspace.title, path: workspace.path };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Read the status for one workspace, including the repository list.
|
|
107
|
+
* @param runtime the runtime helpers.
|
|
108
|
+
* @param workspace the workspace.
|
|
109
|
+
* @param payload the client payload.
|
|
110
|
+
* @returns the snapshot.
|
|
111
|
+
*/
|
|
112
|
+
async function statusFor(runtime, workspace, payload) {
|
|
113
|
+
const requested = valueOf(payload, 'repo');
|
|
114
|
+
if (typeof requested === 'string' && requested !== '') {
|
|
115
|
+
const root = await resolveRepository(runtime, workspace.path, requested);
|
|
116
|
+
const snapshot = await status(runtime, root);
|
|
117
|
+
return { ...snapshot, selected: root, workspace: workspaceInfo(workspace) };
|
|
118
|
+
}
|
|
119
|
+
const repos = await discoverRepositories(runtime, workspace.path);
|
|
120
|
+
const workspaceRepo = await findRepository(runtime, workspace.path);
|
|
121
|
+
if (workspaceRepo !== null) {
|
|
122
|
+
const snapshot = await status(runtime, workspaceRepo);
|
|
123
|
+
return { ...snapshot, repos, selected: workspaceRepo, workspace: workspaceInfo(workspace) };
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
repo: false,
|
|
127
|
+
root: null,
|
|
128
|
+
branch: {
|
|
129
|
+
oid: null,
|
|
130
|
+
name: null,
|
|
131
|
+
upstream: null,
|
|
132
|
+
ahead: 0,
|
|
133
|
+
behind: 0,
|
|
134
|
+
detached: false,
|
|
135
|
+
initial: true,
|
|
136
|
+
},
|
|
137
|
+
entries: [],
|
|
138
|
+
merging: false,
|
|
139
|
+
rebasing: false,
|
|
140
|
+
reverting: false,
|
|
141
|
+
cherryPicking: false,
|
|
142
|
+
conflicts: [],
|
|
143
|
+
repos,
|
|
144
|
+
selected: null,
|
|
145
|
+
workspace: workspaceInfo(workspace),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Run one Git endpoint.
|
|
150
|
+
* @param ctx the host context.
|
|
151
|
+
* @param runtime the runtime helpers.
|
|
152
|
+
* @param endpoint the endpoint name.
|
|
153
|
+
* @param payload the client payload.
|
|
154
|
+
* @returns the endpoint value.
|
|
155
|
+
*/
|
|
156
|
+
async function dispatch(ctx, runtime, endpoint, payload) {
|
|
157
|
+
if (endpoint === 'preferences')
|
|
158
|
+
return preferences(ctx);
|
|
159
|
+
const workspace = workspaceOf(ctx, payload);
|
|
160
|
+
if (endpoint === 'init') {
|
|
161
|
+
const snapshot = await initRepository(runtime, workspace.path);
|
|
162
|
+
return {
|
|
163
|
+
...snapshot,
|
|
164
|
+
repos: await discoverRepositories(runtime, workspace.path),
|
|
165
|
+
selected: snapshot.root,
|
|
166
|
+
workspace: workspaceInfo(workspace),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (endpoint === 'status')
|
|
170
|
+
return statusFor(runtime, workspace, payload);
|
|
171
|
+
const root = await repoOf(runtime, workspace, payload);
|
|
172
|
+
switch (endpoint) {
|
|
173
|
+
case 'diff':
|
|
174
|
+
return diff(runtime, root, {
|
|
175
|
+
path: stringField(payload, 'path'),
|
|
176
|
+
side: sideField(payload),
|
|
177
|
+
commit: optionalString(payload, 'commit'),
|
|
178
|
+
parent: optionalString(payload, 'parent'),
|
|
179
|
+
});
|
|
180
|
+
case 'stage':
|
|
181
|
+
return stagePaths(runtime, root, pathsField(payload, 'paths'));
|
|
182
|
+
case 'unstage':
|
|
183
|
+
return unstagePaths(runtime, root, pathsField(payload, 'paths'));
|
|
184
|
+
case 'applyBlocks':
|
|
185
|
+
return applyBlocks(runtime, root, {
|
|
186
|
+
path: stringField(payload, 'path'),
|
|
187
|
+
side: stringField(payload, 'side'),
|
|
188
|
+
blocks: valueOf(payload, 'blocks'),
|
|
189
|
+
baseHash: valueOf(payload, 'baseHash'),
|
|
190
|
+
targetHash: valueOf(payload, 'targetHash'),
|
|
191
|
+
confirm: valueOf(payload, 'confirm'),
|
|
192
|
+
});
|
|
193
|
+
case 'commit':
|
|
194
|
+
return commit(runtime, root, {
|
|
195
|
+
message: optionalString(payload, 'message') ?? '',
|
|
196
|
+
amend: valueOf(payload, 'amend') === true,
|
|
197
|
+
});
|
|
198
|
+
case 'createBranchAndCommit':
|
|
199
|
+
return createBranchAndCommit(runtime, root, {
|
|
200
|
+
name: stringField(payload, 'name'),
|
|
201
|
+
message: stringField(payload, 'message'),
|
|
202
|
+
});
|
|
203
|
+
case 'push':
|
|
204
|
+
return push(runtime, root);
|
|
205
|
+
case 'pull':
|
|
206
|
+
return pull(runtime, root);
|
|
207
|
+
case 'fetch':
|
|
208
|
+
return fetch(runtime, root);
|
|
209
|
+
case 'log':
|
|
210
|
+
return log(runtime, root);
|
|
211
|
+
case 'commitDetail':
|
|
212
|
+
return commitDetail(runtime, root, { hash: stringField(payload, 'hash') });
|
|
213
|
+
case 'undoCommit':
|
|
214
|
+
return undoCommit(runtime, root, { hash: stringField(payload, 'hash') });
|
|
215
|
+
case 'revertCommit':
|
|
216
|
+
return revertCommit(runtime, root, { hash: stringField(payload, 'hash') });
|
|
217
|
+
case 'cherryPickCommit':
|
|
218
|
+
return cherryPickCommit(runtime, root, { hash: stringField(payload, 'hash') });
|
|
219
|
+
case 'resetToCommit':
|
|
220
|
+
return resetToCommit(runtime, root, {
|
|
221
|
+
hash: stringField(payload, 'hash'),
|
|
222
|
+
mode: stringField(payload, 'mode'),
|
|
223
|
+
confirm: valueOf(payload, 'confirm') === true,
|
|
224
|
+
});
|
|
225
|
+
case 'branches':
|
|
226
|
+
return branches(runtime, root);
|
|
227
|
+
case 'createBranch':
|
|
228
|
+
return createBranch(runtime, root, { name: stringField(payload, 'name') });
|
|
229
|
+
case 'checkout':
|
|
230
|
+
return checkout(runtime, root, {
|
|
231
|
+
branch: stringField(payload, 'branch'),
|
|
232
|
+
remote: valueOf(payload, 'remote') === true,
|
|
233
|
+
});
|
|
234
|
+
case 'stashList':
|
|
235
|
+
return stashList(runtime, root);
|
|
236
|
+
case 'stashPush':
|
|
237
|
+
return stashPush(runtime, root, {
|
|
238
|
+
message: optionalString(payload, 'message'),
|
|
239
|
+
paths: valueOf(payload, 'paths'),
|
|
240
|
+
includeUntracked: valueOf(payload, 'includeUntracked') === true,
|
|
241
|
+
});
|
|
242
|
+
case 'stashApply':
|
|
243
|
+
return stashApply(runtime, root, {
|
|
244
|
+
ref: stringField(payload, 'ref'),
|
|
245
|
+
pop: valueOf(payload, 'pop') === true,
|
|
246
|
+
});
|
|
247
|
+
case 'stashDrop':
|
|
248
|
+
return stashDrop(runtime, root, { ref: stringField(payload, 'ref') });
|
|
249
|
+
case 'resolve':
|
|
250
|
+
return resolveConflict(runtime, root, {
|
|
251
|
+
path: stringField(payload, 'path'),
|
|
252
|
+
side: stringField(payload, 'side'),
|
|
253
|
+
});
|
|
254
|
+
case 'aiMessage': {
|
|
255
|
+
const patch = await stagedPatch(runtime, root);
|
|
256
|
+
if (patch.trim() === '') {
|
|
257
|
+
throw new GitError('git/nothing-staged', 'Stage at least one file before asking AI for a message.');
|
|
258
|
+
}
|
|
259
|
+
const recent = await recentSubjects(runtime, root, 5);
|
|
260
|
+
return writeCommitMessage(ctx, {
|
|
261
|
+
patch,
|
|
262
|
+
recent,
|
|
263
|
+
signal: AbortSignal.timeout(60_000),
|
|
264
|
+
model: valueOf(payload, 'model'),
|
|
265
|
+
instruction: valueOf(payload, 'instruction'),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
default:
|
|
269
|
+
throw new GitError('git/unknown-method', `Unknown Git action: ${endpoint}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Build the endpoint handler that the HTTP route calls.
|
|
274
|
+
* @param ctx the host context with `workspaceRegistry`.
|
|
275
|
+
* @param runtime the runtime helpers.
|
|
276
|
+
* @returns the handler.
|
|
277
|
+
*/
|
|
278
|
+
export function createHandler(ctx, runtime) {
|
|
279
|
+
return async function handler(endpoint, payload, signal, peer) {
|
|
280
|
+
if (peer === undefined || peer.principal?.kind !== 'owner') {
|
|
281
|
+
return {
|
|
282
|
+
ok: false,
|
|
283
|
+
error: {
|
|
284
|
+
code: 'git/forbidden',
|
|
285
|
+
message: 'Git actions are available only to the local user.',
|
|
286
|
+
details: {},
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
const value = await dispatch(ctx, runtime, endpoint, payload);
|
|
292
|
+
return { ok: true, value };
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
if (error instanceof GitError) {
|
|
296
|
+
return {
|
|
297
|
+
ok: false,
|
|
298
|
+
error: {
|
|
299
|
+
code: error.code,
|
|
300
|
+
message: error.message,
|
|
301
|
+
details: error.details,
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
306
|
+
return {
|
|
307
|
+
ok: false,
|
|
308
|
+
error: {
|
|
309
|
+
code: signal?.aborted === true ? 'git/cancelled' : 'git/internal',
|
|
310
|
+
message: message === '' ? 'The Git action failed.' : message,
|
|
311
|
+
details: {},
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-git-desk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Git desk for DeepSeek Harness: a right-sidebar Git workbench with full-file side-by-side diffs, block staging, commits, history, branches, stash, and sync.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,16 +26,15 @@
|
|
|
26
26
|
"url": "https://github.com/P4rthPat3l/dsh-git-desk/issues"
|
|
27
27
|
},
|
|
28
28
|
"exports": {
|
|
29
|
-
".": "./index.js",
|
|
29
|
+
".": "./dist/host/index.js",
|
|
30
30
|
"./client": "./client.js",
|
|
31
31
|
"./locale/*.json": "./locale/*.json",
|
|
32
32
|
"./icon": "./icon.svg"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
|
-
"
|
|
35
|
+
"dist",
|
|
36
36
|
"client.js",
|
|
37
37
|
"cordis.patch.yml",
|
|
38
|
-
"lib",
|
|
39
38
|
"locale/*.json",
|
|
40
39
|
"icon.svg",
|
|
41
40
|
"LICENSE",
|
|
@@ -59,5 +58,20 @@
|
|
|
59
58
|
"@deepseek-ai/dsh-client-ui-workspace"
|
|
60
59
|
]
|
|
61
60
|
}
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "rm -rf dist && tsc -p src/host/tsconfig.json && node scripts/build.mjs",
|
|
64
|
+
"typecheck": "tsc -p src/host/tsconfig.json --noEmit && tsc -p src/client/tsconfig.json",
|
|
65
|
+
"test": "node --test tests/*.test.mjs"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@types/node": "^22.20.5",
|
|
69
|
+
"@types/react": "^18.3.31",
|
|
70
|
+
"@types/react-dom": "^18.3.7",
|
|
71
|
+
"esbuild": "^0.25.0",
|
|
72
|
+
"jsdom": "^25.0.1",
|
|
73
|
+
"react": "^18.3.1",
|
|
74
|
+
"react-dom": "^18.3.1",
|
|
75
|
+
"typescript": "^5.9.3"
|
|
62
76
|
}
|
|
63
77
|
}
|
package/lib/ai.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { GitError } from './exec.js';
|
|
2
|
-
|
|
3
|
-
const MAX_PATCH_CHARS = 60_000;
|
|
4
|
-
const MAX_TITLE_CHARS = 120;
|
|
5
|
-
const MAX_MESSAGE_CHARS = 2_000;
|
|
6
|
-
const MAX_INSTRUCTION_CHARS = 4_000;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The default wording, also shown in Settings so the field starts populated.
|
|
10
|
-
* Reply format stays separate: a user instruction may not break the title
|
|
11
|
-
* line the log and the parser rely on.
|
|
12
|
-
*/
|
|
13
|
-
const DEFAULT_INSTRUCTION = [
|
|
14
|
-
'You write Git commit messages for a developer.',
|
|
15
|
-
'The type is one of feat, fix, refactor, chore, docs, test, perf, style, ci, build.',
|
|
16
|
-
'The scope is one lowercase noun, even when recent commits omit it.',
|
|
17
|
-
'Write the summary in the imperative, lowercase, with no trailing period.',
|
|
18
|
-
'Add a short body that explains why the change was made and which behavior it affects.',
|
|
19
|
-
'Describe the change as a whole; never list files one by one.',
|
|
20
|
-
'Match the style of the recent commit subjects.',
|
|
21
|
-
].join(' ');
|
|
22
|
-
|
|
23
|
-
const FORMAT = [
|
|
24
|
-
'Reply with a title line, then one blank line, then a short body.',
|
|
25
|
-
'Title format: type(scope): summary.',
|
|
26
|
-
'Keep the title on one line and under 100 characters.',
|
|
27
|
-
'Never use a code fence, quotes, or a label such as "Commit message:".',
|
|
28
|
-
'Add no trailers, such as Co-Authored-By.',
|
|
29
|
-
].join(' ');
|
|
30
|
-
|
|
31
|
-
/** Read a stored instruction, falling back to the shipped wording. */
|
|
32
|
-
export function instructionOf(value) {
|
|
33
|
-
if (typeof value !== 'string') return DEFAULT_INSTRUCTION;
|
|
34
|
-
const text = value.trim();
|
|
35
|
-
return text === '' ? DEFAULT_INSTRUCTION : text.slice(0, MAX_INSTRUCTION_CHARS);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Read the model the user picked in Settings. The plugin never stores a
|
|
40
|
-
* provider or a key of its own, so AI actions follow the harness selection
|
|
41
|
-
* unless the panel sends an explicit override.
|
|
42
|
-
*/
|
|
43
|
-
export function selectedModel(ctx) {
|
|
44
|
-
const service = typeof ctx.get === 'function' ? ctx.get('agentDefaultModel') : undefined;
|
|
45
|
-
if (service === undefined || typeof service.currentSelection !== 'function') return null;
|
|
46
|
-
const selection = service.currentSelection();
|
|
47
|
-
if (selection === null || typeof selection !== 'object') return null;
|
|
48
|
-
if (typeof selection.provider !== 'string' || typeof selection.model !== 'string') return null;
|
|
49
|
-
if (selection.provider === '' || selection.model === '') return null;
|
|
50
|
-
return { provider: selection.provider, model: selection.model };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Accept only a complete provider/model pair from the client. */
|
|
54
|
-
function overrideModel(value) {
|
|
55
|
-
if (value === null || typeof value !== 'object') return null;
|
|
56
|
-
const provider = value.provider;
|
|
57
|
-
const model = value.model;
|
|
58
|
-
if (typeof provider !== 'string' || typeof model !== 'string') return null;
|
|
59
|
-
if (provider === '' || model === '') return null;
|
|
60
|
-
return { provider, model };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Models the settings page may offer, grouped by provider. A provider that
|
|
65
|
-
* cannot answer contributes no group rather than failing the whole read.
|
|
66
|
-
*/
|
|
67
|
-
export async function preferences(ctx) {
|
|
68
|
-
const llm = typeof ctx.get === 'function' ? ctx.get('llm') : undefined;
|
|
69
|
-
const groups = [];
|
|
70
|
-
if (llm !== undefined && typeof llm.listProviders === 'function' && typeof llm.listModels === 'function') {
|
|
71
|
-
for (const provider of llm.listProviders()) {
|
|
72
|
-
const id = typeof provider?.id === 'string' ? provider.id : '';
|
|
73
|
-
if (id === '') continue;
|
|
74
|
-
let listed;
|
|
75
|
-
try {
|
|
76
|
-
listed = await llm.listModels(id);
|
|
77
|
-
} catch {
|
|
78
|
-
// An unconfigured provider refuses its catalog; it offers no choice.
|
|
79
|
-
listed = null;
|
|
80
|
-
}
|
|
81
|
-
if (!Array.isArray(listed)) continue;
|
|
82
|
-
const models = listed.flatMap((model) =>
|
|
83
|
-
typeof model?.id === 'string' && model.id !== ''
|
|
84
|
-
? [{ id: model.id, name: typeof model.name === 'string' && model.name !== '' ? model.name : model.id }]
|
|
85
|
-
: [],
|
|
86
|
-
);
|
|
87
|
-
if (models.length === 0) continue;
|
|
88
|
-
const label = typeof provider.name === 'string' && provider.name !== '' ? provider.name : id;
|
|
89
|
-
groups.push({ id, label, models });
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return { instruction: DEFAULT_INSTRUCTION, groups };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Reduce a model reply to a title line and an optional body. The title is
|
|
97
|
-
* normalized the way a Git log reads it; the body keeps its paragraph breaks.
|
|
98
|
-
*/
|
|
99
|
-
export function cleanMessage(text) {
|
|
100
|
-
const lines = text
|
|
101
|
-
.replace(/```[a-z]*\r?\n?/gi, '')
|
|
102
|
-
.replace(/```/g, '')
|
|
103
|
-
.replace(/^\s*commit message\s*:\s*/i, '')
|
|
104
|
-
.split(/\r?\n/)
|
|
105
|
-
.map((line) => line.replace(/[ \t]+$/, ''));
|
|
106
|
-
while (lines.length > 0 && lines[0].trim() === '') lines.shift();
|
|
107
|
-
while (lines.length > 0 && lines[lines.length - 1].trim() === '') lines.pop();
|
|
108
|
-
if (lines.length === 0) return '';
|
|
109
|
-
const title = lines[0]
|
|
110
|
-
.replace(/^["'`]+|["'`]+$/g, '')
|
|
111
|
-
.replace(/\s+/g, ' ')
|
|
112
|
-
.replace(/[.。]+$/, '')
|
|
113
|
-
.trim()
|
|
114
|
-
.slice(0, MAX_TITLE_CHARS);
|
|
115
|
-
if (title === '') return '';
|
|
116
|
-
const body = [];
|
|
117
|
-
for (const line of lines.slice(1)) {
|
|
118
|
-
if (line.trim() === '') {
|
|
119
|
-
if (body.length > 0 && body[body.length - 1] !== '') body.push('');
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
body.push(line.trim());
|
|
123
|
-
}
|
|
124
|
-
while (body.length > 0 && body[body.length - 1] === '') body.pop();
|
|
125
|
-
if (body.length === 0) return title;
|
|
126
|
-
const message = `${title}\n\n${body.join('\n').trim()}`;
|
|
127
|
-
return message.slice(0, MAX_MESSAGE_CHARS).trim();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function promptFor(recent, patch) {
|
|
131
|
-
const body =
|
|
132
|
-
patch.length > MAX_PATCH_CHARS ? `${patch.slice(0, MAX_PATCH_CHARS)}\n[diff truncated]` : patch;
|
|
133
|
-
const style =
|
|
134
|
-
recent.length === 0 ? '(none)' : recent.map((subject) => `- ${subject}`).join('\n');
|
|
135
|
-
return `Recent commit subjects:\n${style}\n\nStaged changes:\n${body}`;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* One model call over the staged patch. The caller owns cancellation and
|
|
140
|
-
* passes a signal that also covers the request timeout. `model` overrides the
|
|
141
|
-
* Composer selection; `instruction` replaces the default wording.
|
|
142
|
-
*/
|
|
143
|
-
export async function writeCommitMessage(ctx, { patch, recent, signal, model: requested, instruction }) {
|
|
144
|
-
const llm = typeof ctx.get === 'function' ? ctx.get('llm') : undefined;
|
|
145
|
-
if (llm === undefined || typeof llm.stream !== 'function') {
|
|
146
|
-
throw new GitError('git/ai-unavailable', 'AI is not available in this build.');
|
|
147
|
-
}
|
|
148
|
-
const model = overrideModel(requested) ?? selectedModel(ctx);
|
|
149
|
-
if (model === null) {
|
|
150
|
-
throw new GitError('git/ai-unavailable', 'Select a model in Settings before using AI actions.');
|
|
151
|
-
}
|
|
152
|
-
const system = `${instructionOf(instruction)} ${FORMAT}`;
|
|
153
|
-
let text = '';
|
|
154
|
-
let failure = null;
|
|
155
|
-
try {
|
|
156
|
-
const chunks = llm.stream({
|
|
157
|
-
provider: model.provider,
|
|
158
|
-
model: model.model,
|
|
159
|
-
system,
|
|
160
|
-
messages: [{ role: 'user', content: [{ type: 'text', text: promptFor(recent, patch) }] }],
|
|
161
|
-
temperature: 0.2,
|
|
162
|
-
maxTokens: 200,
|
|
163
|
-
signal,
|
|
164
|
-
});
|
|
165
|
-
for await (const chunk of chunks) {
|
|
166
|
-
if (chunk.type === 'text-delta') text += chunk.text;
|
|
167
|
-
else if (chunk.type === 'finish' && chunk.reason.kind !== 'stop') failure = chunk.reason;
|
|
168
|
-
}
|
|
169
|
-
} catch (error) {
|
|
170
|
-
if (signal !== undefined && signal.aborted) {
|
|
171
|
-
throw new GitError('git/ai-timeout', 'The model request timed out. Try again.');
|
|
172
|
-
}
|
|
173
|
-
throw new GitError('git/ai-failed', 'The model request failed.', {
|
|
174
|
-
cause: error instanceof Error ? error.message : String(error),
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
if (failure !== null) {
|
|
178
|
-
if (failure.kind === 'aborted') {
|
|
179
|
-
throw new GitError('git/ai-timeout', 'The model request timed out. Try again.');
|
|
180
|
-
}
|
|
181
|
-
const message = failure.failure?.message;
|
|
182
|
-
throw new GitError('git/ai-failed', typeof message === 'string' && message !== '' ? message : 'The model request failed.');
|
|
183
|
-
}
|
|
184
|
-
const message = cleanMessage(text);
|
|
185
|
-
if (message === '') throw new GitError('git/ai-empty', 'The model returned an empty message. Try again.');
|
|
186
|
-
return { message, provider: model.provider, model: model.model };
|
|
187
|
-
}
|