plugin-git-manager 1.3.3 → 1.4.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/dist/client/{156.c27c2392746e7866.js → 156.8e36af3e2cee173e.js} +1 -1
- package/dist/client/{317.17ed0018e3e7db00.js → 317.2d0d33871e24ee24.js} +1 -1
- package/dist/client/{434.b2eeaaef33aae6e0.js → 434.2401f7257495b66e.js} +1 -1
- package/dist/client/{453.fc3042c9740d8fab.js → 453.58657b9ba0904477.js} +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client-v2/{434.19c7579c06b9077c.js → 434.32b0a07b821b9329.js} +1 -1
- package/dist/client-v2/{453.5e507c94c59277b1.js → 453.f9a0ec6d8e1bd1cc.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +10 -9
- package/dist/locale/en-US.json +27 -1
- package/dist/locale/vi-VN.json +27 -1
- package/dist/locale/zh-CN.json +30 -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 +99 -4
- package/dist/server/actions/gitlab-api.js +86 -7
- package/dist/server/chat-context-admission.js +187 -0
- package/dist/server/plugin.js +8 -0
- package/dist/server/request-method-policy.js +2 -0
- package/package.json +2 -2
- package/src/client/__tests__/GitContextPicker.test.tsx +120 -0
- package/src/client/__tests__/ai-context.test.tsx +119 -0
- package/src/client/ai-context.tsx +3 -113
- package/src/client/context/GitManagerContext.tsx +18 -2
- package/src/client/index.tsx +3 -5
- 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 +27 -1
- package/src/locale/vi-VN.json +27 -1
- package/src/locale/zh-CN.json +30 -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-chat-context.test.ts +138 -0
- package/src/server/__tests__/git-security.test.ts +52 -0
- package/src/server/__tests__/smoke.test.ts +200 -0
- package/src/server/actions/chat-context.ts +302 -0
- package/src/server/actions/git-actions.ts +119 -6
- package/src/server/actions/gitlab-api.ts +97 -4
- package/src/server/chat-context-admission.ts +182 -0
- package/src/server/plugin.ts +11 -0
- package/src/server/request-method-policy.ts +2 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import type { Context } from '@nocobase/actions';
|
|
4
|
+
import { loadChatContext } from './actions/chat-context';
|
|
5
|
+
|
|
6
|
+
type GitContextType = 'git-repository' | 'git-merge-request' | 'git-commit';
|
|
7
|
+
type GitContextItem = { type: GitContextType; uid: string; title?: string };
|
|
8
|
+
type WorkContextHandler = {
|
|
9
|
+
registerStrategy: (
|
|
10
|
+
type: string,
|
|
11
|
+
strategy: { resolve: (ctx: Context, item: GitContextItem) => Promise<string> },
|
|
12
|
+
) => void;
|
|
13
|
+
};
|
|
14
|
+
type AIPlugin = {
|
|
15
|
+
workContextHandler?: WorkContextHandler;
|
|
16
|
+
aiConversationsManager?: {
|
|
17
|
+
getConversation: (options: { sessionId: string; userId: number | string }) => Promise<unknown>;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type App = { pm: { get: (name: string) => unknown } };
|
|
21
|
+
|
|
22
|
+
const TYPES: GitContextType[] = ['git-repository', 'git-merge-request', 'git-commit'];
|
|
23
|
+
const registered = new WeakMap<WorkContextHandler, Set<GitContextType>>();
|
|
24
|
+
const admitted = new WeakMap<object, Map<string, Record<string, unknown>>>();
|
|
25
|
+
|
|
26
|
+
function gitContext(value: unknown): GitContextItem | null {
|
|
27
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
28
|
+
const item = value as Record<string, unknown>;
|
|
29
|
+
if (typeof item.type !== 'string' || !TYPES.includes(item.type as GitContextType)) return null;
|
|
30
|
+
return {
|
|
31
|
+
type: item.type as GitContextType,
|
|
32
|
+
uid: typeof item.uid === 'string' ? item.uid : '',
|
|
33
|
+
...(typeof item.title === 'string' ? { title: item.title } : {}),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function contextParams(ctx: Context, item: GitContextItem): Record<string, unknown> {
|
|
38
|
+
const kind = item.type.slice(4);
|
|
39
|
+
if (!item.uid || !item.uid.trim()) return ctx.throw(400, 'Invalid Git context');
|
|
40
|
+
if (kind === 'repository') return { kind, repositoryId: item.uid };
|
|
41
|
+
const separator = item.uid.indexOf(':');
|
|
42
|
+
if (separator <= 0 || separator === item.uid.length - 1) return ctx.throw(400, 'Invalid Git context');
|
|
43
|
+
const repositoryId = item.uid.slice(0, separator);
|
|
44
|
+
const target = item.uid.slice(separator + 1);
|
|
45
|
+
return kind === 'merge-request' ? { kind, repositoryId, mrIid: target } : { kind, repositoryId, commitHash: target };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function contextKey(item: GitContextItem): string {
|
|
49
|
+
return JSON.stringify([item.type, item.uid]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function resolvedItem(item: GitContextItem, content: Record<string, unknown>) {
|
|
53
|
+
const name = typeof content.repositoryName === 'string' ? content.repositoryName : String(content.repositoryId || '');
|
|
54
|
+
let title: string;
|
|
55
|
+
if (item.type === 'git-repository') {
|
|
56
|
+
title = typeof content.name === 'string' ? content.name : String(content.repositoryId || '');
|
|
57
|
+
} else if (item.type === 'git-merge-request') {
|
|
58
|
+
title = [name, `!${content.mrIid}`, content.title].filter(Boolean).join(' · ');
|
|
59
|
+
} else {
|
|
60
|
+
title = [name, String(content.commitSha || '').slice(0, 7), content.subject].filter(Boolean).join(' · ');
|
|
61
|
+
}
|
|
62
|
+
return { type: item.type, uid: item.uid, title, content };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function aiPluginFor(app: App): AIPlugin | undefined {
|
|
66
|
+
try {
|
|
67
|
+
return (app.pm.get('ai') || app.pm.get('@nocobase/plugin-ai')) as AIPlugin | undefined;
|
|
68
|
+
} catch {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function registerGitChatContextStrategies(app: App): boolean {
|
|
74
|
+
const handler = aiPluginFor(app)?.workContextHandler;
|
|
75
|
+
if (!handler || typeof handler.registerStrategy !== 'function') return false;
|
|
76
|
+
const names = registered.get(handler) || new Set<GitContextType>();
|
|
77
|
+
registered.set(handler, names);
|
|
78
|
+
for (const type of TYPES) {
|
|
79
|
+
if (names.has(type)) continue;
|
|
80
|
+
handler.registerStrategy(type, {
|
|
81
|
+
resolve: async (ctx, source) => {
|
|
82
|
+
const item = gitContext(source);
|
|
83
|
+
if (!item) return '';
|
|
84
|
+
try {
|
|
85
|
+
const content =
|
|
86
|
+
admitted.get(ctx)?.get(contextKey(item)) || (await loadChatContext(ctx, contextParams(ctx, item)));
|
|
87
|
+
return JSON.stringify(resolvedItem(item, content));
|
|
88
|
+
} catch {
|
|
89
|
+
return JSON.stringify({ type, content: { unavailable: true } });
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
names.add(type);
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function admitContextItems(ctx: Context, values: unknown): Promise<void> {
|
|
99
|
+
if (!Array.isArray(values)) return;
|
|
100
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
101
|
+
const item = gitContext(values[index]);
|
|
102
|
+
if (!item) continue;
|
|
103
|
+
try {
|
|
104
|
+
if (!registerGitChatContextStrategies(ctx.app)) throw new Error('Git context strategy is unavailable');
|
|
105
|
+
const content = await loadChatContext(ctx, contextParams(ctx, item));
|
|
106
|
+
let cache = admitted.get(ctx);
|
|
107
|
+
if (!cache) {
|
|
108
|
+
cache = new Map();
|
|
109
|
+
admitted.set(ctx, cache);
|
|
110
|
+
}
|
|
111
|
+
cache.set(contextKey(item), content);
|
|
112
|
+
values[index] = resolvedItem(item, content);
|
|
113
|
+
} catch {
|
|
114
|
+
ctx.throw(
|
|
115
|
+
403,
|
|
116
|
+
ctx.t('Git context is unavailable. Check your access and select it again.', { ns: 'plugin-git-manager' }),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function recordValue(record: unknown, key: string): unknown {
|
|
123
|
+
if (!record || typeof record !== 'object') return undefined;
|
|
124
|
+
const object = record as { get?: (field: string) => unknown } & Record<string, unknown>;
|
|
125
|
+
return typeof object.get === 'function' ? object.get(key) : object[key];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function admitResentMessage(ctx: Context, values: Record<string, unknown>) {
|
|
129
|
+
const sessionId = typeof values.sessionId === 'string' ? values.sessionId : '';
|
|
130
|
+
const hasMessageId = Object.prototype.hasOwnProperty.call(values, 'messageId');
|
|
131
|
+
let messageId: string | number | undefined;
|
|
132
|
+
if (hasMessageId) {
|
|
133
|
+
const supplied = values.messageId;
|
|
134
|
+
if (typeof supplied === 'number' && Number.isSafeInteger(supplied) && supplied > 0) {
|
|
135
|
+
messageId = supplied;
|
|
136
|
+
} else if (typeof supplied === 'string' && /^\d+$/.test(supplied) && /[1-9]/.test(supplied)) {
|
|
137
|
+
messageId = supplied;
|
|
138
|
+
} else {
|
|
139
|
+
return ctx.throw(400, 'Invalid messageId');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const userId = ctx.auth?.user?.id;
|
|
143
|
+
const plugin = aiPluginFor(ctx.app);
|
|
144
|
+
if (!sessionId || !userId || !plugin?.aiConversationsManager) return;
|
|
145
|
+
const conversation = await plugin.aiConversationsManager.getConversation({ sessionId, userId });
|
|
146
|
+
if (!conversation) return;
|
|
147
|
+
const repository = ctx.db.getRepository('aiConversations.messages', sessionId);
|
|
148
|
+
const target = hasMessageId
|
|
149
|
+
? await repository.findOne({ filter: { messageId } })
|
|
150
|
+
: await repository.findOne({ filter: { sessionId }, sort: ['-messageId'] });
|
|
151
|
+
if (!target) return;
|
|
152
|
+
const targetId = recordValue(target, 'messageId');
|
|
153
|
+
if ((typeof targetId !== 'string' && typeof targetId !== 'number') || !targetId) {
|
|
154
|
+
return ctx.throw(400, 'Invalid messageId');
|
|
155
|
+
}
|
|
156
|
+
const message =
|
|
157
|
+
recordValue(target, 'role') === 'user'
|
|
158
|
+
? target
|
|
159
|
+
: await repository.findOne({
|
|
160
|
+
filter: { sessionId, role: 'user', messageId: { $lt: targetId } },
|
|
161
|
+
sort: ['-messageId'],
|
|
162
|
+
});
|
|
163
|
+
await admitContextItems(ctx, recordValue(message, 'workContext'));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function enforceGitChatAdmission(ctx: Context, next: () => Promise<void>) {
|
|
167
|
+
if (ctx.action?.resourceName !== 'aiConversations') return next();
|
|
168
|
+
const action = ctx.action.actionName;
|
|
169
|
+
if (action !== 'sendMessages' && action !== 'resendMessages') return next();
|
|
170
|
+
const values = ctx.action.params?.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
|
+
}
|
package/src/server/plugin.ts
CHANGED
|
@@ -12,6 +12,7 @@ import * as reviewCutoverActions from './actions/review-cutover';
|
|
|
12
12
|
import * as pollerActions from './actions/poller';
|
|
13
13
|
import * as rolePermissionsActions from './actions/role-permissions';
|
|
14
14
|
import * as subtreeActions from './actions/subtree';
|
|
15
|
+
import * as chatContextActions from './actions/chat-context';
|
|
15
16
|
import { unregisterReviewQueue } from './actions/review';
|
|
16
17
|
import { registerGitReviewAiTools } from './ai-tools';
|
|
17
18
|
import { enforceGitRequestMethod } from './request-method-policy';
|
|
@@ -21,6 +22,7 @@ import { GitReviewService } from './services/review-service';
|
|
|
21
22
|
import { LegacyPostService } from './services/legacy-post-service';
|
|
22
23
|
import { enforceReviewControlAccess } from './review-control-access';
|
|
23
24
|
import { enforceRepositoryAccess, enforceRepositoryCollectionAccess } from './repository-access';
|
|
25
|
+
import { enforceGitChatAdmission, registerGitChatContextStrategies } from './chat-context-admission';
|
|
24
26
|
import { isGitManagerResourceResponse, redactCredentialFields } from './utils/redact';
|
|
25
27
|
import { containsCredentialBearingUrlField, URL_USERINFO_NOT_ALLOWED } from './utils/url-security';
|
|
26
28
|
|
|
@@ -111,6 +113,8 @@ export class PluginGitManagerServer extends Plugin {
|
|
|
111
113
|
mergeRequests: gitlabApi.mergeRequests,
|
|
112
114
|
mergeRequestDetail: gitlabApi.mergeRequestDetail,
|
|
113
115
|
mergeRequestNotes: gitlabApi.mergeRequestNotes,
|
|
116
|
+
chatContextOptions: chatContextActions.chatContextOptions,
|
|
117
|
+
chatContextContent: chatContextActions.chatContextContent,
|
|
114
118
|
triggerReview: reviewActions.triggerReview,
|
|
115
119
|
reviewApprovePost: reviewActions.reviewApprovePost,
|
|
116
120
|
reviewLegacyPostStatus: reviewActions.reviewLegacyPostStatus,
|
|
@@ -135,6 +139,10 @@ export class PluginGitManagerServer extends Plugin {
|
|
|
135
139
|
},
|
|
136
140
|
});
|
|
137
141
|
|
|
142
|
+
// These actions perform their own checks against the caller's existing Git and collection permissions.
|
|
143
|
+
this.app.acl.allow('gitManager', 'chatContextOptions', 'loggedIn');
|
|
144
|
+
this.app.acl.allow('gitManager', 'chatContextContent', 'loggedIn');
|
|
145
|
+
|
|
138
146
|
const legacyApprovalAction = this.app.acl.getAvailableAction('reviewApprovePost');
|
|
139
147
|
const priorAliases = legacyApprovalAction?.options?.aliases;
|
|
140
148
|
this.app.acl.setAvailableAction('reviewApprovePost', {
|
|
@@ -186,6 +194,7 @@ export class PluginGitManagerServer extends Plugin {
|
|
|
186
194
|
}
|
|
187
195
|
|
|
188
196
|
this.app.resourceManager.use(enforceGitRequestMethod);
|
|
197
|
+
this.app.resourceManager.use(enforceGitChatAdmission);
|
|
189
198
|
this.app.resourceManager.use(enforceReviewControlAccess);
|
|
190
199
|
(this as any).app.resourceManager.use(enforceRepositoryAccess);
|
|
191
200
|
(this as any).app.resourceManager.use(enforceRepositoryCollectionAccess);
|
|
@@ -211,6 +220,8 @@ export class PluginGitManagerServer extends Plugin {
|
|
|
211
220
|
|
|
212
221
|
registerGitReviewAiTools((this as any).app);
|
|
213
222
|
|
|
223
|
+
this.app.on('afterLoad', () => registerGitChatContextStrategies(this.app));
|
|
224
|
+
|
|
214
225
|
(this as any).app.on('afterStart', async () => {
|
|
215
226
|
await ensureAutoReviewFlowSchema((this as any).app).catch(
|
|
216
227
|
(err) => (this as any).app.log?.error?.('plugin-git-manager: ensure schema error', err),
|