plugin-ai-api 1.0.21 → 1.0.24
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/123.e6fe04c856ce6417.js +10 -0
- package/dist/client/902.e74518750f1e4201.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/123.05f1f649923f93eb.js +10 -0
- package/dist/client-v2/902.c7c00a565085438a.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/constants.js +5 -2
- package/dist/locale/en-US.json +15 -1
- package/dist/locale/vi-VN.json +15 -1
- package/dist/locale/zh-CN.json +15 -1
- package/dist/server/collections/ai-api-user-permissions.js +67 -0
- package/dist/server/collections/ai-api-user-quota-policies.js +2 -1
- package/dist/server/plugin.js +32 -0
- package/dist/server/resource/ai-api-user-permissions.js +75 -0
- package/dist/server/routes/agent-completions.js +5 -0
- package/dist/server/routes/chat-completions.js +52 -27
- package/dist/server/routes/completions.js +59 -33
- package/dist/server/routes/embeddings.js +6 -14
- package/dist/server/routes/models.js +24 -0
- package/dist/server/utils/direct-llm-context.js +184 -0
- package/dist/server/utils/openai-format.js +17 -3
- package/dist/server/utils/user-permissions.js +160 -0
- package/dist/server/validation.js +3 -0
- package/dist/swagger.js +4 -3
- package/package.json +2 -2
- package/src/client/__tests__/settings-registration.test.tsx +69 -0
- package/src/client/plugin.tsx +14 -3
- package/src/client-v2/__tests__/settings-registration.test.tsx +33 -4
- package/src/client-v2/pages/UserPermissionsPage.tsx +322 -0
- package/src/client-v2/pages/UserQuotasPage.tsx +18 -0
- package/src/client-v2/plugin.tsx +12 -3
- package/src/constants.ts +7 -0
- package/src/locale/en-US.json +15 -1
- package/src/locale/vi-VN.json +15 -1
- package/src/locale/zh-CN.json +15 -1
- package/src/server/__tests__/direct-llm-context.test.ts +125 -0
- package/src/server/__tests__/models.test.ts +44 -2
- package/src/server/__tests__/openai-format.test.ts +52 -1
- package/src/server/__tests__/permission-sync.test.ts +109 -0
- package/src/server/__tests__/usage-route.test.ts +265 -5
- package/src/server/__tests__/user-permissions-resource.test.ts +66 -0
- package/src/server/__tests__/user-permissions.test.ts +284 -0
- package/src/server/__tests__/validation.test.ts +36 -0
- package/src/server/collections/ai-api-user-permissions.ts +46 -0
- package/src/server/collections/ai-api-user-quota-policies.ts +1 -0
- package/src/server/plugin.ts +42 -1
- package/src/server/resource/ai-api-user-permissions.ts +76 -0
- package/src/server/routes/agent-completions.ts +7 -0
- package/src/server/routes/chat-completions.ts +58 -30
- package/src/server/routes/completions.ts +68 -34
- package/src/server/routes/embeddings.ts +10 -15
- package/src/server/routes/models.ts +28 -0
- package/src/server/utils/direct-llm-context.ts +216 -0
- package/src/server/utils/openai-format.ts +26 -0
- package/src/server/utils/user-permissions.ts +218 -0
- package/src/server/validation.ts +3 -0
- package/src/swagger.ts +9 -3
- package/dist/client/902.92e1daaf1ab16ebf.js +0 -10
- package/dist/client-v2/902.9054d990ddc223ac.js +0 -10
|
@@ -0,0 +1,160 @@
|
|
|
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 user_permissions_exports = {};
|
|
28
|
+
__export(user_permissions_exports, {
|
|
29
|
+
buildAccessScope: () => buildAccessScope,
|
|
30
|
+
enforceModelAccess: () => enforceModelAccess,
|
|
31
|
+
invalidateUserPermissionCache: () => invalidateUserPermissionCache,
|
|
32
|
+
isModelAllowed: () => isModelAllowed,
|
|
33
|
+
isServiceAllowed: () => isServiceAllowed,
|
|
34
|
+
resolveUserAccessScope: () => resolveUserAccessScope
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(user_permissions_exports);
|
|
37
|
+
var import_openai_format = require("./openai-format");
|
|
38
|
+
const SCOPE_TTL_MS = 15e3;
|
|
39
|
+
const scopeCache = /* @__PURE__ */ new Map();
|
|
40
|
+
const NO_RECORD_SCOPE = {
|
|
41
|
+
hasUserRecord: false,
|
|
42
|
+
denyAll: false,
|
|
43
|
+
allowedServices: null,
|
|
44
|
+
allowAllModels: true,
|
|
45
|
+
allowedModels: /* @__PURE__ */ new Set(),
|
|
46
|
+
lookupFailed: false
|
|
47
|
+
};
|
|
48
|
+
const LOOKUP_FAILED_SCOPE = { ...NO_RECORD_SCOPE, denyAll: true, lookupFailed: true };
|
|
49
|
+
function invalidateUserPermissionCache(userId) {
|
|
50
|
+
if (userId === void 0 || userId === null) {
|
|
51
|
+
scopeCache.clear();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const suffix = `:${userId}`;
|
|
55
|
+
for (const key of scopeCache.keys()) {
|
|
56
|
+
if (key.endsWith(suffix)) scopeCache.delete(key);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function valueOf(row, name) {
|
|
60
|
+
if (!row) return void 0;
|
|
61
|
+
const candidate = row;
|
|
62
|
+
if (typeof candidate.get === "function") return candidate.get(name);
|
|
63
|
+
return row[name];
|
|
64
|
+
}
|
|
65
|
+
function toStringArray(value) {
|
|
66
|
+
if (!Array.isArray(value)) return [];
|
|
67
|
+
return value.filter((item) => typeof item === "string" && item.length > 0);
|
|
68
|
+
}
|
|
69
|
+
function buildAccessScope(row) {
|
|
70
|
+
if (!row) return NO_RECORD_SCOPE;
|
|
71
|
+
if (valueOf(row, "enabled") === false) {
|
|
72
|
+
return { ...NO_RECORD_SCOPE, hasUserRecord: true, denyAll: true, allowedServices: [] };
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
hasUserRecord: true,
|
|
76
|
+
denyAll: false,
|
|
77
|
+
allowedServices: toStringArray(valueOf(row, "allowedLlmServices")),
|
|
78
|
+
allowAllModels: valueOf(row, "allowAllModels") !== false,
|
|
79
|
+
allowedModels: new Set(toStringArray(valueOf(row, "allowedModels"))),
|
|
80
|
+
lookupFailed: false
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async function resolveUserAccessScope(ctx) {
|
|
84
|
+
var _a, _b, _c, _d;
|
|
85
|
+
const userId = (_a = ctx.state.currentUser) == null ? void 0 : _a.id;
|
|
86
|
+
if (userId === void 0 || userId === null) return NO_RECORD_SCOPE;
|
|
87
|
+
const key = `${((_b = ctx.app) == null ? void 0 : _b.name) ?? "main"}:${userId}`;
|
|
88
|
+
const cached = scopeCache.get(key);
|
|
89
|
+
if (cached && cached.expiresAt > Date.now()) return cached.scope;
|
|
90
|
+
let scope;
|
|
91
|
+
try {
|
|
92
|
+
const row = await ctx.db.getRepository("aiApiUserPermissions").findOne({ filter: { userId } });
|
|
93
|
+
scope = buildAccessScope(row);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
(_d = (_c = ctx.log) == null ? void 0 : _c.error) == null ? void 0 : _d.call(_c, "AI API user permissions lookup failed, denying access:", err);
|
|
96
|
+
return LOOKUP_FAILED_SCOPE;
|
|
97
|
+
}
|
|
98
|
+
scopeCache.set(key, { scope, expiresAt: Date.now() + SCOPE_TTL_MS });
|
|
99
|
+
return scope;
|
|
100
|
+
}
|
|
101
|
+
function matchesService(list, serviceName, serviceTitle) {
|
|
102
|
+
return list.some((entry) => entry === serviceName || entry === serviceTitle);
|
|
103
|
+
}
|
|
104
|
+
function isServiceAllowed(scope, globalEnabledServices, service) {
|
|
105
|
+
if (scope.denyAll) return false;
|
|
106
|
+
const globalList = toStringArray(globalEnabledServices);
|
|
107
|
+
if (globalList.length && !matchesService(globalList, service.name, service.title)) return false;
|
|
108
|
+
if (!scope.hasUserRecord) return true;
|
|
109
|
+
return matchesService(scope.allowedServices ?? [], service.name, service.title);
|
|
110
|
+
}
|
|
111
|
+
function isModelAllowed(scope, fullModelId) {
|
|
112
|
+
if (scope.denyAll) return false;
|
|
113
|
+
if (!scope.hasUserRecord) return true;
|
|
114
|
+
if (scope.allowAllModels) return true;
|
|
115
|
+
return scope.allowedModels.has(fullModelId);
|
|
116
|
+
}
|
|
117
|
+
async function enforceModelAccess(ctx, globalEnabledServices, service, modelId) {
|
|
118
|
+
const scope = await resolveUserAccessScope(ctx);
|
|
119
|
+
const serviceLabel = service.title || service.name;
|
|
120
|
+
if (scope.lookupFailed) {
|
|
121
|
+
ctx.status = 503;
|
|
122
|
+
ctx.body = (0, import_openai_format.toOpenAIError)(
|
|
123
|
+
503,
|
|
124
|
+
"Unable to verify LLM permissions for this user. Please retry shortly.",
|
|
125
|
+
"service_unavailable",
|
|
126
|
+
"permission_check_failed"
|
|
127
|
+
);
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (!isServiceAllowed(scope, globalEnabledServices, service)) {
|
|
131
|
+
ctx.status = 403;
|
|
132
|
+
ctx.body = (0, import_openai_format.toOpenAIError)(
|
|
133
|
+
403,
|
|
134
|
+
`LLM service '${serviceLabel}' is not enabled for API access`,
|
|
135
|
+
"permission_denied",
|
|
136
|
+
"model_not_available"
|
|
137
|
+
);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
if (!isModelAllowed(scope, `${service.name}/${modelId}`)) {
|
|
141
|
+
ctx.status = 403;
|
|
142
|
+
ctx.body = (0, import_openai_format.toOpenAIError)(
|
|
143
|
+
403,
|
|
144
|
+
`Model '${service.name}/${modelId}' is not permitted for this user. Use GET /v1/models to see available models.`,
|
|
145
|
+
"permission_denied",
|
|
146
|
+
"model_not_available"
|
|
147
|
+
);
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
153
|
+
0 && (module.exports = {
|
|
154
|
+
buildAccessScope,
|
|
155
|
+
enforceModelAccess,
|
|
156
|
+
invalidateUserPermissionCache,
|
|
157
|
+
isModelAllowed,
|
|
158
|
+
isServiceAllowed,
|
|
159
|
+
resolveUserAccessScope
|
|
160
|
+
});
|
|
@@ -101,6 +101,9 @@ function validateQuotaPolicy(model) {
|
|
|
101
101
|
if (!["allow", "use_reserved"].includes(String(model.get("missingUsageBehavior")))) {
|
|
102
102
|
throw new Error("missingUsageBehavior must be allow or use_reserved.");
|
|
103
103
|
}
|
|
104
|
+
if (!["reject", "truncate"].includes(String(model.get("contextOverflowBehavior") ?? "reject"))) {
|
|
105
|
+
throw new Error("contextOverflowBehavior must be reject or truncate.");
|
|
106
|
+
}
|
|
104
107
|
try {
|
|
105
108
|
(0, import_dayjs.default)().tz(String(model.get("timezone") || "UTC"));
|
|
106
109
|
} catch {
|
package/dist/swagger.js
CHANGED
|
@@ -78,7 +78,7 @@ var swagger_default = {
|
|
|
78
78
|
get: {
|
|
79
79
|
tags: ["ai-llm"],
|
|
80
80
|
summary: "List available models",
|
|
81
|
-
description: "Returns
|
|
81
|
+
description: "Returns the LLM models available to the authenticated caller across registered services. Model IDs are formatted as `serviceName/modelId`.\n\nThe catalog is user-scoped: it starts from `enabledLlmServices` in the AI API configuration, then narrows to the caller's `aiApiUserPermissions` record when one exists. A user grant can only narrow the global whitelist, never widen it, so two users may receive different lists from the same request.",
|
|
82
82
|
security: [{ BearerAuth: [] }],
|
|
83
83
|
responses: {
|
|
84
84
|
200: {
|
|
@@ -105,6 +105,7 @@ var swagger_default = {
|
|
|
105
105
|
get: {
|
|
106
106
|
tags: ["ai-llm"],
|
|
107
107
|
summary: "Get model details",
|
|
108
|
+
description: "User-scoped in the same way as `GET /v1/models`: a model the caller is not granted is reported as not found rather than disclosed.",
|
|
108
109
|
security: [{ BearerAuth: [] }],
|
|
109
110
|
parameters: [
|
|
110
111
|
{
|
|
@@ -124,7 +125,7 @@ var swagger_default = {
|
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
},
|
|
127
|
-
404: { description: "Model not found" }
|
|
128
|
+
404: { description: "Model not found, or not available to this user" }
|
|
128
129
|
}
|
|
129
130
|
}
|
|
130
131
|
},
|
|
@@ -272,7 +273,7 @@ var swagger_default = {
|
|
|
272
273
|
enabledLlmServices: {
|
|
273
274
|
type: "array",
|
|
274
275
|
items: { type: "string" },
|
|
275
|
-
description: "List of enabled LLM service names"
|
|
276
|
+
description: "List of enabled LLM service names. This is the outer bound for every caller; per-user `aiApiUserPermissions` records can only narrow it further."
|
|
276
277
|
},
|
|
277
278
|
rateLimitPerMinute: {
|
|
278
279
|
type: "integer",
|
package/package.json
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
import { Application } from '@nocobase/client';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
import { AI_API_ACL_SNIPPET, AI_API_USER_PERMISSIONS_SNIPPET } from '../../constants';
|
|
13
|
+
import { PluginAiApiClient } from '../plugin';
|
|
14
|
+
|
|
15
|
+
describe('AI API v1 settings registration', () => {
|
|
16
|
+
async function loadPlugin() {
|
|
17
|
+
const app = new Application({});
|
|
18
|
+
await new PluginAiApiClient({}, app).load();
|
|
19
|
+
return app;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it('registers the same ordered settings tabs and ACL boundaries as v2', async () => {
|
|
23
|
+
const app = await loadPlugin();
|
|
24
|
+
app.pluginSettingsManager.setAclSnippets([]);
|
|
25
|
+
|
|
26
|
+
const menu = app.pluginSettingsManager.get('ai-api');
|
|
27
|
+
|
|
28
|
+
expect(menu?.children?.map((item) => item.name)).toEqual([
|
|
29
|
+
'ai-api.config',
|
|
30
|
+
'ai-api.model-pricing',
|
|
31
|
+
'ai-api.model-metadata',
|
|
32
|
+
'ai-api.user-permissions',
|
|
33
|
+
'ai-api.user-quotas',
|
|
34
|
+
'ai-api.usage',
|
|
35
|
+
]);
|
|
36
|
+
expect(app.pluginSettingsManager.getAclSnippet('ai-api.user-permissions')).toBe(AI_API_USER_PERMISSIONS_SNIPPET);
|
|
37
|
+
expect(app.pluginSettingsManager.getRoutePath('ai-api.user-permissions')).toBe(
|
|
38
|
+
'/admin/settings/ai-api/user-permissions',
|
|
39
|
+
);
|
|
40
|
+
expect(app.pluginSettingsManager.getList().map((item) => item.name)).not.toContain('ai-api-user-permissions');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('requires access to the legacy parent before exposing the user permissions tab', async () => {
|
|
44
|
+
const app = await loadPlugin();
|
|
45
|
+
app.pluginSettingsManager.setAclSnippets([`!${AI_API_ACL_SNIPPET}`]);
|
|
46
|
+
|
|
47
|
+
expect(app.pluginSettingsManager.getList()).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('hides only the user permissions tab when its own snippet is denied', async () => {
|
|
51
|
+
const app = await loadPlugin();
|
|
52
|
+
app.pluginSettingsManager.setAclSnippets([`!${AI_API_USER_PERMISSIONS_SNIPPET}`]);
|
|
53
|
+
|
|
54
|
+
expect(app.pluginSettingsManager.get('ai-api')?.children?.map((item) => item.name)).toEqual([
|
|
55
|
+
'ai-api.config',
|
|
56
|
+
'ai-api.model-pricing',
|
|
57
|
+
'ai-api.model-metadata',
|
|
58
|
+
'ai-api.user-quotas',
|
|
59
|
+
'ai-api.usage',
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('hides both settings surfaces when both snippets are denied', async () => {
|
|
64
|
+
const app = await loadPlugin();
|
|
65
|
+
app.pluginSettingsManager.setAclSnippets([`!${AI_API_ACL_SNIPPET}`, `!${AI_API_USER_PERMISSIONS_SNIPPET}`]);
|
|
66
|
+
|
|
67
|
+
expect(app.pluginSettingsManager.getList()).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
});
|
package/src/client/plugin.tsx
CHANGED
|
@@ -9,12 +9,13 @@
|
|
|
9
9
|
|
|
10
10
|
import { Plugin, lazy } from '@nocobase/client';
|
|
11
11
|
import PluginACLClient from '@nocobase/plugin-acl/client';
|
|
12
|
-
import { AI_API_ACL_SNIPPET } from '../constants';
|
|
12
|
+
import { AI_API_ACL_SNIPPET, AI_API_USER_PERMISSIONS_SNIPPET } from '../constants';
|
|
13
13
|
import React from 'react';
|
|
14
14
|
|
|
15
15
|
const AiApiConfigPage = React.lazy(() => import('../client-v2/pages/GeneralPage'));
|
|
16
16
|
const AiApiModelPricingPage = React.lazy(() => import('../client-v2/pages/ModelPricingPage'));
|
|
17
17
|
const AiApiModelMetadataPage = React.lazy(() => import('../client-v2/pages/ModelMetadataPage'));
|
|
18
|
+
const AiApiUserPermissionsPage = React.lazy(() => import('../client-v2/pages/UserPermissionsPage'));
|
|
18
19
|
const AiApiUserQuotasPage = React.lazy(() => import('../client-v2/pages/UserQuotasPage'));
|
|
19
20
|
const AiApiUsagePage = React.lazy(() => import('../client-v2/pages/UsagePage'));
|
|
20
21
|
const { AiApiRolePermissions } = lazy(() => import('./components/AiApiRolePermissions'), 'AiApiRolePermissions');
|
|
@@ -48,18 +49,28 @@ export class PluginAiApiClient extends Plugin {
|
|
|
48
49
|
sort: 3,
|
|
49
50
|
});
|
|
50
51
|
|
|
52
|
+
// Keep the legacy settings layout aligned with client-v2. The v1 settings
|
|
53
|
+
// manager evaluates the parent ACL first, so this tab additionally requires
|
|
54
|
+
// access to the AI API settings parent even though it keeps its own snippet.
|
|
55
|
+
this.app.pluginSettingsManager.add('ai-api.user-permissions', {
|
|
56
|
+
title: this.t('User LLM permissions'),
|
|
57
|
+
Component: AiApiUserPermissionsPage,
|
|
58
|
+
aclSnippet: AI_API_USER_PERMISSIONS_SNIPPET,
|
|
59
|
+
sort: 4,
|
|
60
|
+
});
|
|
61
|
+
|
|
51
62
|
this.app.pluginSettingsManager.add('ai-api.user-quotas', {
|
|
52
63
|
title: this.t('User quotas'),
|
|
53
64
|
Component: AiApiUserQuotasPage,
|
|
54
65
|
aclSnippet: AI_API_ACL_SNIPPET,
|
|
55
|
-
sort:
|
|
66
|
+
sort: 5,
|
|
56
67
|
});
|
|
57
68
|
|
|
58
69
|
this.app.pluginSettingsManager.add('ai-api.usage', {
|
|
59
70
|
title: this.t('Usage'),
|
|
60
71
|
Component: AiApiUsagePage,
|
|
61
72
|
aclSnippet: AI_API_ACL_SNIPPET,
|
|
62
|
-
sort:
|
|
73
|
+
sort: 6,
|
|
63
74
|
});
|
|
64
75
|
|
|
65
76
|
// Add "AI API" tab in Settings → Users & Permissions → [Role]
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { createMockClient } from '@nocobase/client-v2';
|
|
11
|
-
import { AI_API_ACL_SNIPPET } from '../../constants';
|
|
11
|
+
import { AI_API_ACL_SNIPPET, AI_API_USER_PERMISSIONS_SNIPPET } from '../../constants';
|
|
12
12
|
import { PluginAiApiClient } from '../plugin';
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -17,6 +17,11 @@ import { PluginAiApiClient } from '../plugin';
|
|
|
17
17
|
* Two regressions are cheap to reintroduce and invisible until a non-admin logs in:
|
|
18
18
|
* omitting `aclSnippet` makes addPageTabItem default to `pm.ai-api.<key>`, a snippet the
|
|
19
19
|
* server never registers, and omitting `sort` silently reorders the tabs alphabetically.
|
|
20
|
+
*
|
|
21
|
+
* The tabs deliberately span two snippets: everything sits under AI_API_ACL_SNIPPET except
|
|
22
|
+
* user permissions, which is gated separately so granting it does not also grant gateway
|
|
23
|
+
* configuration. Each snippet is evaluated independently, so denying one leaves the other's
|
|
24
|
+
* tabs — and therefore the menu — reachable.
|
|
20
25
|
*/
|
|
21
26
|
describe('AI API v2 settings registration', () => {
|
|
22
27
|
async function loadPlugin() {
|
|
@@ -25,24 +30,47 @@ describe('AI API v2 settings registration', () => {
|
|
|
25
30
|
return app;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
|
-
it('registers every page under
|
|
33
|
+
it('registers every page under a snippet the server exposes', async () => {
|
|
29
34
|
const app = await loadPlugin();
|
|
30
35
|
const menu = app.pluginSettingsManager.get('ai-api', false);
|
|
31
36
|
|
|
32
37
|
expect(menu?.aclSnippet).toBe(AI_API_ACL_SNIPPET);
|
|
33
38
|
for (const child of menu?.children ?? []) {
|
|
34
|
-
expect(app.pluginSettingsManager.getAclSnippet(child.name), child.name).toBe(
|
|
39
|
+
expect(app.pluginSettingsManager.getAclSnippet(child.name), child.name).toBe(
|
|
40
|
+
child.name === 'ai-api.user-permissions' ? AI_API_USER_PERMISSIONS_SNIPPET : AI_API_ACL_SNIPPET,
|
|
41
|
+
);
|
|
35
42
|
}
|
|
36
43
|
});
|
|
37
44
|
|
|
38
|
-
it('
|
|
45
|
+
it('leaves only the separately-gated tab when the main snippet is denied', async () => {
|
|
39
46
|
const app = await loadPlugin();
|
|
40
47
|
app.pluginSettingsManager.setAclSnippets([`!${AI_API_ACL_SNIPPET}`]);
|
|
41
48
|
|
|
49
|
+
// User permissions carries its own snippet, so it survives on its own merit and keeps the
|
|
50
|
+
// menu reachable. That separation is the point: a role can be allowed to hand out model
|
|
51
|
+
// access without also being allowed to reconfigure the gateway.
|
|
52
|
+
expect(app.pluginSettingsManager.get('ai-api')?.children?.map((item) => item.name)).toEqual([
|
|
53
|
+
'ai-api.user-permissions',
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('hides the menu and every tab when both snippets are denied', async () => {
|
|
58
|
+
const app = await loadPlugin();
|
|
59
|
+
app.pluginSettingsManager.setAclSnippets([`!${AI_API_ACL_SNIPPET}`, `!${AI_API_USER_PERMISSIONS_SNIPPET}`]);
|
|
60
|
+
|
|
42
61
|
expect(app.pluginSettingsManager.get('ai-api')).toBeNull();
|
|
43
62
|
expect(app.pluginSettingsManager.getList().map((item) => item.name)).not.toContain('ai-api');
|
|
44
63
|
});
|
|
45
64
|
|
|
65
|
+
it('hides only the user permissions tab when its own snippet is denied', async () => {
|
|
66
|
+
const app = await loadPlugin();
|
|
67
|
+
app.pluginSettingsManager.setAclSnippets([`!${AI_API_USER_PERMISSIONS_SNIPPET}`]);
|
|
68
|
+
|
|
69
|
+
const children = app.pluginSettingsManager.get('ai-api')?.children?.map((item) => item.name) ?? [];
|
|
70
|
+
expect(children).not.toContain('ai-api.user-permissions');
|
|
71
|
+
expect(children).toContain('ai-api.user-quotas');
|
|
72
|
+
});
|
|
73
|
+
|
|
46
74
|
it('keeps registration order instead of sorting tabs by name', async () => {
|
|
47
75
|
const app = await loadPlugin();
|
|
48
76
|
app.pluginSettingsManager.setAclSnippets([]);
|
|
@@ -51,6 +79,7 @@ describe('AI API v2 settings registration', () => {
|
|
|
51
79
|
'ai-api.index',
|
|
52
80
|
'ai-api.model-pricing',
|
|
53
81
|
'ai-api.model-metadata',
|
|
82
|
+
'ai-api.user-permissions',
|
|
54
83
|
'ai-api.user-quotas',
|
|
55
84
|
'ai-api.usage',
|
|
56
85
|
]);
|