plugin-git-manager 1.3.3 → 1.4.1
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/dist/client/{156.c27c2392746e7866.js → 156.8e36af3e2cee173e.js} +1 -1
- package/dist/client/317.e7b1d59371812c30.js +10 -0
- package/dist/client/434.05d0ba2b821105dc.js +10 -0
- package/dist/client/453.cdcba6d2d0c9522b.js +10 -0
- package/dist/client/454.625da741a7e1161f.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/317.4e116f84ed9081b6.js +10 -0
- package/dist/client-v2/434.764113d20743a45b.js +10 -0
- package/dist/client-v2/453.5ab80c06d8e61a7e.js +10 -0
- package/dist/client-v2/454.b885b1d35ef7ff72.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +10 -9
- package/dist/locale/en-US.json +38 -1
- package/dist/locale/vi-VN.json +38 -1
- package/dist/locale/zh-CN.json +41 -1
- package/dist/node_modules/simple-git/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/actions/chat-context.js +282 -0
- package/dist/server/actions/git-actions.js +387 -80
- package/dist/server/actions/gitlab-api.js +101 -15
- package/dist/server/actions/subtree.js +20 -10
- package/dist/server/chat-context-admission.js +187 -0
- package/dist/server/plugin.js +142 -10
- package/dist/server/repository-access.js +2 -2
- package/dist/server/request-method-policy.js +4 -1
- package/dist/server/services/review-source.js +2 -1
- package/dist/server/utils/cluster-git.js +181 -0
- package/package.json +2 -2
- package/src/client/__tests__/FileExplorerAndManage.test.tsx +236 -0
- package/src/client/__tests__/GitContextPicker.test.tsx +120 -0
- package/src/client/__tests__/ai-context.test.tsx +119 -0
- package/src/client/__tests__/workflowRoutes.test.ts +3 -1
- package/src/client/ai-context.tsx +3 -113
- package/src/client/components/CommitHistory.tsx +19 -7
- package/src/client/components/FileExplorer.tsx +80 -34
- package/src/client/components/GitManagerSettings.tsx +21 -5
- package/src/client/components/GitOperations.tsx +25 -4
- package/src/client/components/GitRuntimeBanner.tsx +97 -0
- package/src/client/components/RepositoryConfig.tsx +28 -6
- package/src/client/context/GitManagerContext.tsx +174 -15
- package/src/client/index.tsx +3 -5
- package/src/client/workflowRoutes.ts +11 -0
- package/src/client-v2/ai-context.tsx +135 -72
- package/src/client-v2/components/GitContextPicker.tsx +418 -0
- package/src/client-v2/git-context-api.ts +59 -0
- package/src/client-v2/plugin.tsx +2 -4
- package/src/locale/en-US.json +38 -1
- package/src/locale/vi-VN.json +38 -1
- package/src/locale/zh-CN.json +41 -1
- package/src/server/__tests__/chat-context-admission.test.ts +276 -0
- package/src/server/__tests__/chat-context-options.test.ts +144 -0
- package/src/server/__tests__/git-actions.test.ts +329 -4
- package/src/server/__tests__/git-chat-context.test.ts +138 -0
- package/src/server/__tests__/git-security.test.ts +52 -0
- package/src/server/__tests__/smoke.test.ts +232 -1
- package/src/server/actions/chat-context.ts +302 -0
- package/src/server/actions/git-actions.ts +428 -89
- package/src/server/actions/gitlab-api.ts +113 -14
- package/src/server/actions/subtree.ts +23 -6
- package/src/server/chat-context-admission.ts +182 -0
- package/src/server/plugin.ts +163 -3
- package/src/server/repository-access.ts +2 -2
- package/src/server/request-method-policy.ts +3 -0
- package/src/server/services/review-source.ts +3 -2
- package/src/server/utils/cluster-git.ts +209 -0
- package/dist/client/317.17ed0018e3e7db00.js +0 -10
- package/dist/client/434.b2eeaaef33aae6e0.js +0 -10
- package/dist/client/453.fc3042c9740d8fab.js +0 -10
- package/dist/client-v2/317.d2466574d00f94d0.js +0 -10
- package/dist/client-v2/434.19c7579c06b9077c.js +0 -10
- package/dist/client-v2/453.5e507c94c59277b1.js +0 -10
|
@@ -28,7 +28,8 @@ var gitlab_api_exports = {};
|
|
|
28
28
|
__export(gitlab_api_exports, {
|
|
29
29
|
mergeRequestDetail: () => mergeRequestDetail,
|
|
30
30
|
mergeRequestNotes: () => mergeRequestNotes,
|
|
31
|
-
mergeRequests: () => mergeRequests
|
|
31
|
+
mergeRequests: () => mergeRequests,
|
|
32
|
+
readChatMergeRequest: () => readChatMergeRequest
|
|
32
33
|
});
|
|
33
34
|
module.exports = __toCommonJS(gitlab_api_exports);
|
|
34
35
|
var import_repository_access = require("../repository-access");
|
|
@@ -56,12 +57,18 @@ async function gitlabFetch(apiBase, endpoint, pat, params) {
|
|
|
56
57
|
}
|
|
57
58
|
const totalPages = response.headers.get("x-total-pages");
|
|
58
59
|
const total = response.headers.get("x-total");
|
|
60
|
+
const nextPage = response.headers.get("x-next-page");
|
|
59
61
|
const data = await response.json();
|
|
60
|
-
return {
|
|
62
|
+
return {
|
|
63
|
+
data,
|
|
64
|
+
totalPages: totalPages ? parseInt(totalPages, 10) : null,
|
|
65
|
+
total: total ? parseInt(total, 10) : null,
|
|
66
|
+
hasMore: nextPage !== null ? Boolean(nextPage) : null
|
|
67
|
+
};
|
|
61
68
|
}
|
|
62
|
-
async function getRepoApiContext(ctx) {
|
|
69
|
+
async function getRepoApiContext(ctx, requestedRepositoryId) {
|
|
63
70
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
64
|
-
const
|
|
71
|
+
const repositoryId = requestedRepositoryId ?? params.repositoryId;
|
|
65
72
|
if (typeof repositoryId !== "string" && typeof repositoryId !== "number") ctx.throw(400, "repositoryId is required");
|
|
66
73
|
const repo = await ctx.db.getRepository("gitRepositories").findOne({
|
|
67
74
|
filterByTk: repositoryId
|
|
@@ -106,7 +113,8 @@ async function githubFetch(endpoint, pat, params) {
|
|
|
106
113
|
}
|
|
107
114
|
}
|
|
108
115
|
const data = await response.json();
|
|
109
|
-
|
|
116
|
+
const hasMore = linkHeader ? linkHeader.split(",").some((link) => link.includes('rel="next"')) : false;
|
|
117
|
+
return { data, totalPages, total: null, hasMore };
|
|
110
118
|
}
|
|
111
119
|
async function mergeRequests(ctx, next) {
|
|
112
120
|
const { pat, apiBase, encodedProject, projectPath, isGitHub } = await getRepoApiContext(ctx);
|
|
@@ -217,19 +225,28 @@ async function mergeRequests(ctx, next) {
|
|
|
217
225
|
page: parseInt(String(page), 10),
|
|
218
226
|
perPage: parseInt(String(perPage), 10),
|
|
219
227
|
total: result.total,
|
|
220
|
-
totalPages: result.totalPages
|
|
228
|
+
totalPages: result.totalPages,
|
|
229
|
+
hasMore: result.hasMore ?? (result.totalPages !== null ? Number(page) < result.totalPages : result.data.length >= Math.min(parseInt(String(perPage), 10) || 20, 100))
|
|
221
230
|
}
|
|
222
231
|
};
|
|
223
232
|
await next();
|
|
224
233
|
}
|
|
234
|
+
function parseMrIid(value, ctx) {
|
|
235
|
+
if (value === void 0 || value === null || value === "") {
|
|
236
|
+
ctx.throw(400, "mrIid is required");
|
|
237
|
+
}
|
|
238
|
+
const text = String(value).trim();
|
|
239
|
+
const parsed = typeof value === "number" ? value : Number(text);
|
|
240
|
+
if (!/^\d+$/.test(text) || !Number.isSafeInteger(parsed) || parsed < 1) {
|
|
241
|
+
ctx.throw(400, "Invalid merge request ID");
|
|
242
|
+
}
|
|
243
|
+
return parsed;
|
|
244
|
+
}
|
|
225
245
|
async function mergeRequestDetail(ctx, next) {
|
|
226
246
|
var _a, _b, _c;
|
|
227
247
|
const { pat, apiBase, encodedProject, projectPath, isGitHub } = await getRepoApiContext(ctx);
|
|
228
248
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
229
|
-
const
|
|
230
|
-
if (!mrIid) {
|
|
231
|
-
ctx.throw(400, "mrIid is required");
|
|
232
|
-
}
|
|
249
|
+
const mrIid = parseMrIid(params.mrIid, ctx);
|
|
233
250
|
let data;
|
|
234
251
|
if (isGitHub) {
|
|
235
252
|
const [prResult, filesResult] = await Promise.all([
|
|
@@ -339,13 +356,81 @@ async function mergeRequestDetail(ctx, next) {
|
|
|
339
356
|
};
|
|
340
357
|
await next();
|
|
341
358
|
}
|
|
359
|
+
async function readChatMergeRequest(ctx, mrIid, repositoryId) {
|
|
360
|
+
var _a, _b, _c, _d, _e;
|
|
361
|
+
const validMrIid = parseMrIid(mrIid, ctx);
|
|
362
|
+
const { pat, apiBase, encodedProject, projectPath, isGitHub } = await getRepoApiContext(ctx, repositoryId);
|
|
363
|
+
if (isGitHub) {
|
|
364
|
+
const [request2, fileResult] = await Promise.all([
|
|
365
|
+
githubFetch(`/repos/${projectPath}/pulls/${validMrIid}`, pat),
|
|
366
|
+
githubFetch(`/repos/${projectPath}/pulls/${validMrIid}/files`, pat, { page: 1, per_page: 51 })
|
|
367
|
+
]);
|
|
368
|
+
const item2 = request2.data;
|
|
369
|
+
if (!Array.isArray(fileResult.data)) throw new Error("Unable to read merge request changes");
|
|
370
|
+
const files2 = fileResult.data.map(
|
|
371
|
+
(file) => ({
|
|
372
|
+
oldPath: file.previous_filename || file.filename,
|
|
373
|
+
newPath: file.filename,
|
|
374
|
+
status: file.status,
|
|
375
|
+
diff: file.patch
|
|
376
|
+
})
|
|
377
|
+
);
|
|
378
|
+
const limitations2 = [];
|
|
379
|
+
if (typeof item2.changed_files === "number" && item2.changed_files > files2.length) {
|
|
380
|
+
limitations2.push("Additional changed files were omitted by the provider.");
|
|
381
|
+
}
|
|
382
|
+
if (files2.some((file) => !file.diff)) {
|
|
383
|
+
limitations2.push("The provider omitted patches for one or more files, including binary or large files.");
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
title: item2.title,
|
|
387
|
+
description: item2.body,
|
|
388
|
+
state: item2.state === "open" ? "opened" : item2.merged_at ? "merged" : "closed",
|
|
389
|
+
sourceBranch: (_a = item2.head) == null ? void 0 : _a.ref,
|
|
390
|
+
targetBranch: (_b = item2.base) == null ? void 0 : _b.ref,
|
|
391
|
+
author: (_c = item2.user) == null ? void 0 : _c.login,
|
|
392
|
+
files: files2,
|
|
393
|
+
limitations: limitations2
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
if (!pat) ctx.throw(400, "Personal Access Token is required for GitLab API access");
|
|
397
|
+
const [request, changeResult] = await Promise.all([
|
|
398
|
+
gitlabFetch(apiBase, `/projects/${encodedProject}/merge_requests/${validMrIid}`, pat),
|
|
399
|
+
gitlabFetch(apiBase, `/projects/${encodedProject}/merge_requests/${validMrIid}/changes`, pat)
|
|
400
|
+
]);
|
|
401
|
+
const item = request.data;
|
|
402
|
+
const changeData = changeResult.data || {};
|
|
403
|
+
if (!Array.isArray(changeData.changes)) throw new Error("Unable to read merge request changes");
|
|
404
|
+
const files = changeData.changes.map(
|
|
405
|
+
(file) => ({
|
|
406
|
+
oldPath: file.old_path,
|
|
407
|
+
newPath: file.new_path,
|
|
408
|
+
status: file.new_file ? "added" : file.deleted_file ? "removed" : file.renamed_file ? "renamed" : "modified",
|
|
409
|
+
diff: file.diff,
|
|
410
|
+
unavailable: file.collapsed || file.too_large || file.binary || !file.diff
|
|
411
|
+
})
|
|
412
|
+
);
|
|
413
|
+
const limitations = [];
|
|
414
|
+
if (changeData.overflow) limitations.push("The provider omitted additional changed files or patches.");
|
|
415
|
+
if (files.some((file) => file.unavailable)) {
|
|
416
|
+
limitations.push("The provider omitted patches for one or more files, including binary or large files.");
|
|
417
|
+
}
|
|
418
|
+
return {
|
|
419
|
+
title: item.title,
|
|
420
|
+
description: item.description,
|
|
421
|
+
state: item.state,
|
|
422
|
+
sourceBranch: item.source_branch,
|
|
423
|
+
targetBranch: item.target_branch,
|
|
424
|
+
author: ((_d = item.author) == null ? void 0 : _d.username) || ((_e = item.author) == null ? void 0 : _e.name),
|
|
425
|
+
files,
|
|
426
|
+
limitations
|
|
427
|
+
};
|
|
428
|
+
}
|
|
342
429
|
async function mergeRequestNotes(ctx, next) {
|
|
343
430
|
const { pat, apiBase, encodedProject, projectPath, isGitHub } = await getRepoApiContext(ctx);
|
|
344
431
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
345
|
-
const {
|
|
346
|
-
|
|
347
|
-
ctx.throw(400, "mrIid is required");
|
|
348
|
-
}
|
|
432
|
+
const { page = 1, perPage = 50 } = params;
|
|
433
|
+
const mrIid = parseMrIid(params.mrIid, ctx);
|
|
349
434
|
let notes = [];
|
|
350
435
|
const pagination = {
|
|
351
436
|
page: parseInt(String(page), 10),
|
|
@@ -398,5 +483,6 @@ async function mergeRequestNotes(ctx, next) {
|
|
|
398
483
|
0 && (module.exports = {
|
|
399
484
|
mergeRequestDetail,
|
|
400
485
|
mergeRequestNotes,
|
|
401
|
-
mergeRequests
|
|
486
|
+
mergeRequests,
|
|
487
|
+
readChatMergeRequest
|
|
402
488
|
});
|
|
@@ -288,6 +288,7 @@ async function loadConfigAndRepository(ctx, configId) {
|
|
|
288
288
|
return { configRecord, config, repo, account };
|
|
289
289
|
}
|
|
290
290
|
async function subtreeOptions(ctx, next) {
|
|
291
|
+
var _a;
|
|
291
292
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
292
293
|
const repositoryId = params.repositoryId;
|
|
293
294
|
if (!repositoryId) ctx.throw(400, "repositoryId is required");
|
|
@@ -300,7 +301,8 @@ async function subtreeOptions(ctx, next) {
|
|
|
300
301
|
if (!account) ctx.throw(400, "Repository has no Git account configured");
|
|
301
302
|
const localPath = (0, import_git_actions.validateLocalPath)(stringValue(repo.get("localPath")));
|
|
302
303
|
const repoUrl = stringValue(repo.get("repoUrl"));
|
|
303
|
-
const git = (0, import_git_actions.
|
|
304
|
+
const git = await (0, import_git_actions.getGit)(ctx, localPath);
|
|
305
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
304
306
|
try {
|
|
305
307
|
const data = await (0, import_git_actions.withAuth)(
|
|
306
308
|
git,
|
|
@@ -328,23 +330,27 @@ async function subtreeOptions(ctx, next) {
|
|
|
328
330
|
return { branches: Array.from(new Set(branches)).sort(), folders };
|
|
329
331
|
},
|
|
330
332
|
account.username,
|
|
331
|
-
remoteName
|
|
333
|
+
remoteName,
|
|
334
|
+
lockManager
|
|
332
335
|
);
|
|
333
336
|
ctx.body = { success: true, data };
|
|
334
337
|
} catch (error) {
|
|
338
|
+
const status = (error == null ? void 0 : error.status) === 503 ? 503 : 400;
|
|
335
339
|
const safeError = (0, import_redact.redactError)(error);
|
|
336
|
-
ctx.throw(
|
|
340
|
+
ctx.throw(status, ctx.t(safeError.message, { ns: "plugin-git-manager" }));
|
|
337
341
|
}
|
|
338
342
|
await next();
|
|
339
343
|
}
|
|
340
344
|
async function subtreePreview(ctx, next) {
|
|
345
|
+
var _a;
|
|
341
346
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
342
347
|
const configId = params.configId;
|
|
343
348
|
if (!configId) ctx.throw(400, "configId is required");
|
|
344
349
|
const { config, repo, account } = await loadConfigAndRepository(ctx, configId);
|
|
345
350
|
const localPath = (0, import_git_actions.validateLocalPath)(stringValue(repo.get("localPath")));
|
|
346
351
|
const repoUrl = stringValue(repo.get("repoUrl"));
|
|
347
|
-
const git = (0, import_git_actions.
|
|
352
|
+
const git = await (0, import_git_actions.getGit)(ctx, localPath);
|
|
353
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
348
354
|
try {
|
|
349
355
|
const preview = await (0, import_git_actions.withAuth)(
|
|
350
356
|
git,
|
|
@@ -353,17 +359,19 @@ async function subtreePreview(ctx, next) {
|
|
|
353
359
|
account.pat,
|
|
354
360
|
() => createPreview(git, config),
|
|
355
361
|
account.username,
|
|
356
|
-
config.remoteName
|
|
362
|
+
config.remoteName,
|
|
363
|
+
lockManager
|
|
357
364
|
);
|
|
358
365
|
ctx.body = { success: true, data: preview };
|
|
359
366
|
} catch (error) {
|
|
367
|
+
const status = (error == null ? void 0 : error.status) === 503 ? 503 : 400;
|
|
360
368
|
const safeError = (0, import_redact.redactError)(error);
|
|
361
|
-
ctx.throw(
|
|
369
|
+
ctx.throw(status, ctx.t(safeError.message, { ns: "plugin-git-manager" }));
|
|
362
370
|
}
|
|
363
371
|
await next();
|
|
364
372
|
}
|
|
365
373
|
async function subtreeRunOnAppProcess(ctx, next) {
|
|
366
|
-
var _a, _b;
|
|
374
|
+
var _a, _b, _c;
|
|
367
375
|
const params = (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
368
376
|
const runParams = {
|
|
369
377
|
configId: params.configId,
|
|
@@ -378,7 +386,8 @@ async function subtreeRunOnAppProcess(ctx, next) {
|
|
|
378
386
|
const { config, repo, account } = await loadConfigAndRepository(ctx, runParams.configId);
|
|
379
387
|
const localPath = (0, import_git_actions.validateLocalPath)(stringValue(repo.get("localPath")));
|
|
380
388
|
const repoUrl = stringValue(repo.get("repoUrl"));
|
|
381
|
-
const git = (0, import_git_actions.
|
|
389
|
+
const git = await (0, import_git_actions.getGit)(ctx, localPath);
|
|
390
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
382
391
|
const runsRepository = ctx.db.getRepository("gitSubtreeRuns");
|
|
383
392
|
const startedAt = /* @__PURE__ */ new Date();
|
|
384
393
|
const run = await runsRepository.create({
|
|
@@ -388,7 +397,7 @@ async function subtreeRunOnAppProcess(ctx, next) {
|
|
|
388
397
|
executionMode: SUBTREE_EXECUTION_MODE,
|
|
389
398
|
status: "running",
|
|
390
399
|
startedAt,
|
|
391
|
-
triggeredById: (
|
|
400
|
+
triggeredById: (_c = (_b = ctx.state) == null ? void 0 : _b.currentUser) == null ? void 0 : _c.id
|
|
392
401
|
}
|
|
393
402
|
});
|
|
394
403
|
try {
|
|
@@ -415,7 +424,8 @@ async function subtreeRunOnAppProcess(ctx, next) {
|
|
|
415
424
|
return { preview, targetAfterSha };
|
|
416
425
|
},
|
|
417
426
|
account.username,
|
|
418
|
-
config.remoteName
|
|
427
|
+
config.remoteName,
|
|
428
|
+
lockManager
|
|
419
429
|
);
|
|
420
430
|
const finishedAt = /* @__PURE__ */ new Date();
|
|
421
431
|
await runsRepository.update({
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var chat_context_admission_exports = {};
|
|
28
|
+
__export(chat_context_admission_exports, {
|
|
29
|
+
enforceGitChatAdmission: () => enforceGitChatAdmission,
|
|
30
|
+
registerGitChatContextStrategies: () => registerGitChatContextStrategies
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(chat_context_admission_exports);
|
|
33
|
+
var import_chat_context = require("./actions/chat-context");
|
|
34
|
+
const TYPES = ["git-repository", "git-merge-request", "git-commit"];
|
|
35
|
+
const registered = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
const admitted = /* @__PURE__ */ new WeakMap();
|
|
37
|
+
function gitContext(value) {
|
|
38
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
39
|
+
const item = value;
|
|
40
|
+
if (typeof item.type !== "string" || !TYPES.includes(item.type)) return null;
|
|
41
|
+
return {
|
|
42
|
+
type: item.type,
|
|
43
|
+
uid: typeof item.uid === "string" ? item.uid : "",
|
|
44
|
+
...typeof item.title === "string" ? { title: item.title } : {}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function contextParams(ctx, item) {
|
|
48
|
+
const kind = item.type.slice(4);
|
|
49
|
+
if (!item.uid || !item.uid.trim()) return ctx.throw(400, "Invalid Git context");
|
|
50
|
+
if (kind === "repository") return { kind, repositoryId: item.uid };
|
|
51
|
+
const separator = item.uid.indexOf(":");
|
|
52
|
+
if (separator <= 0 || separator === item.uid.length - 1) return ctx.throw(400, "Invalid Git context");
|
|
53
|
+
const repositoryId = item.uid.slice(0, separator);
|
|
54
|
+
const target = item.uid.slice(separator + 1);
|
|
55
|
+
return kind === "merge-request" ? { kind, repositoryId, mrIid: target } : { kind, repositoryId, commitHash: target };
|
|
56
|
+
}
|
|
57
|
+
function contextKey(item) {
|
|
58
|
+
return JSON.stringify([item.type, item.uid]);
|
|
59
|
+
}
|
|
60
|
+
function resolvedItem(item, content) {
|
|
61
|
+
const name = typeof content.repositoryName === "string" ? content.repositoryName : String(content.repositoryId || "");
|
|
62
|
+
let title;
|
|
63
|
+
if (item.type === "git-repository") {
|
|
64
|
+
title = typeof content.name === "string" ? content.name : String(content.repositoryId || "");
|
|
65
|
+
} else if (item.type === "git-merge-request") {
|
|
66
|
+
title = [name, `!${content.mrIid}`, content.title].filter(Boolean).join(" \xB7 ");
|
|
67
|
+
} else {
|
|
68
|
+
title = [name, String(content.commitSha || "").slice(0, 7), content.subject].filter(Boolean).join(" \xB7 ");
|
|
69
|
+
}
|
|
70
|
+
return { type: item.type, uid: item.uid, title, content };
|
|
71
|
+
}
|
|
72
|
+
function aiPluginFor(app) {
|
|
73
|
+
try {
|
|
74
|
+
return app.pm.get("ai") || app.pm.get("@nocobase/plugin-ai");
|
|
75
|
+
} catch {
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function registerGitChatContextStrategies(app) {
|
|
80
|
+
var _a;
|
|
81
|
+
const handler = (_a = aiPluginFor(app)) == null ? void 0 : _a.workContextHandler;
|
|
82
|
+
if (!handler || typeof handler.registerStrategy !== "function") return false;
|
|
83
|
+
const names = registered.get(handler) || /* @__PURE__ */ new Set();
|
|
84
|
+
registered.set(handler, names);
|
|
85
|
+
for (const type of TYPES) {
|
|
86
|
+
if (names.has(type)) continue;
|
|
87
|
+
handler.registerStrategy(type, {
|
|
88
|
+
resolve: async (ctx, source) => {
|
|
89
|
+
var _a2;
|
|
90
|
+
const item = gitContext(source);
|
|
91
|
+
if (!item) return "";
|
|
92
|
+
try {
|
|
93
|
+
const content = ((_a2 = admitted.get(ctx)) == null ? void 0 : _a2.get(contextKey(item))) || await (0, import_chat_context.loadChatContext)(ctx, contextParams(ctx, item));
|
|
94
|
+
return JSON.stringify(resolvedItem(item, content));
|
|
95
|
+
} catch {
|
|
96
|
+
return JSON.stringify({ type, content: { unavailable: true } });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
names.add(type);
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
async function admitContextItems(ctx, values) {
|
|
105
|
+
if (!Array.isArray(values)) return;
|
|
106
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
107
|
+
const item = gitContext(values[index]);
|
|
108
|
+
if (!item) continue;
|
|
109
|
+
try {
|
|
110
|
+
if (!registerGitChatContextStrategies(ctx.app)) throw new Error("Git context strategy is unavailable");
|
|
111
|
+
const content = await (0, import_chat_context.loadChatContext)(ctx, contextParams(ctx, item));
|
|
112
|
+
let cache = admitted.get(ctx);
|
|
113
|
+
if (!cache) {
|
|
114
|
+
cache = /* @__PURE__ */ new Map();
|
|
115
|
+
admitted.set(ctx, cache);
|
|
116
|
+
}
|
|
117
|
+
cache.set(contextKey(item), content);
|
|
118
|
+
values[index] = resolvedItem(item, content);
|
|
119
|
+
} catch {
|
|
120
|
+
ctx.throw(
|
|
121
|
+
403,
|
|
122
|
+
ctx.t("Git context is unavailable. Check your access and select it again.", { ns: "plugin-git-manager" })
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function recordValue(record, key) {
|
|
128
|
+
if (!record || typeof record !== "object") return void 0;
|
|
129
|
+
const object = record;
|
|
130
|
+
return typeof object.get === "function" ? object.get(key) : object[key];
|
|
131
|
+
}
|
|
132
|
+
async function admitResentMessage(ctx, values) {
|
|
133
|
+
var _a, _b;
|
|
134
|
+
const sessionId = typeof values.sessionId === "string" ? values.sessionId : "";
|
|
135
|
+
const hasMessageId = Object.prototype.hasOwnProperty.call(values, "messageId");
|
|
136
|
+
let messageId;
|
|
137
|
+
if (hasMessageId) {
|
|
138
|
+
const supplied = values.messageId;
|
|
139
|
+
if (typeof supplied === "number" && Number.isSafeInteger(supplied) && supplied > 0) {
|
|
140
|
+
messageId = supplied;
|
|
141
|
+
} else if (typeof supplied === "string" && /^\d+$/.test(supplied) && /[1-9]/.test(supplied)) {
|
|
142
|
+
messageId = supplied;
|
|
143
|
+
} else {
|
|
144
|
+
return ctx.throw(400, "Invalid messageId");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const userId = (_b = (_a = ctx.auth) == null ? void 0 : _a.user) == null ? void 0 : _b.id;
|
|
148
|
+
const plugin = aiPluginFor(ctx.app);
|
|
149
|
+
if (!sessionId || !userId || !(plugin == null ? void 0 : plugin.aiConversationsManager)) return;
|
|
150
|
+
const conversation = await plugin.aiConversationsManager.getConversation({ sessionId, userId });
|
|
151
|
+
if (!conversation) return;
|
|
152
|
+
const repository = ctx.db.getRepository("aiConversations.messages", sessionId);
|
|
153
|
+
const target = hasMessageId ? await repository.findOne({ filter: { messageId } }) : await repository.findOne({ filter: { sessionId }, sort: ["-messageId"] });
|
|
154
|
+
if (!target) return;
|
|
155
|
+
const targetId = recordValue(target, "messageId");
|
|
156
|
+
if (typeof targetId !== "string" && typeof targetId !== "number" || !targetId) {
|
|
157
|
+
return ctx.throw(400, "Invalid messageId");
|
|
158
|
+
}
|
|
159
|
+
const message = recordValue(target, "role") === "user" ? target : await repository.findOne({
|
|
160
|
+
filter: { sessionId, role: "user", messageId: { $lt: targetId } },
|
|
161
|
+
sort: ["-messageId"]
|
|
162
|
+
});
|
|
163
|
+
await admitContextItems(ctx, recordValue(message, "workContext"));
|
|
164
|
+
}
|
|
165
|
+
async function enforceGitChatAdmission(ctx, next) {
|
|
166
|
+
var _a, _b;
|
|
167
|
+
if (((_a = ctx.action) == null ? void 0 : _a.resourceName) !== "aiConversations") return next();
|
|
168
|
+
const action = ctx.action.actionName;
|
|
169
|
+
if (action !== "sendMessages" && action !== "resendMessages") return next();
|
|
170
|
+
const values = (_b = ctx.action.params) == null ? void 0 : _b.values;
|
|
171
|
+
if (!values || typeof values !== "object" || Array.isArray(values)) return next();
|
|
172
|
+
if (action === "sendMessages" && Array.isArray(values.messages)) {
|
|
173
|
+
for (const message of values.messages) {
|
|
174
|
+
if (message && typeof message === "object" && message.role === "user") {
|
|
175
|
+
await admitContextItems(ctx, message.workContext);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} else if (action === "resendMessages") {
|
|
179
|
+
await admitResentMessage(ctx, values);
|
|
180
|
+
}
|
|
181
|
+
await next();
|
|
182
|
+
}
|
|
183
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
184
|
+
0 && (module.exports = {
|
|
185
|
+
enforceGitChatAdmission,
|
|
186
|
+
registerGitChatContextStrategies
|
|
187
|
+
});
|