Skip to content

Commit 5d38440

Browse files
Update modelWhitelist.ts
1 parent 39e32b6 commit 5d38440

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

plugins/default/modelWhitelist.ts

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,55 @@
1-
import type {
1+
import {
22
HookEventType,
33
PluginContext,
44
PluginHandler,
55
PluginParameters,
66
} from '../types';
77

8-
interface WhitelistData {
9-
explanation: string;
10-
}
11-
128
export const handler: PluginHandler = async (
139
context: PluginContext,
1410
parameters: PluginParameters,
1511
eventType: HookEventType
1612
) => {
1713
let error = null;
1814
let verdict = false;
19-
let data: WhitelistData | null = null;
15+
let data: any = null;
2016

2117
try {
2218
const modelList = parameters.models;
2319
const not = parameters.not || false;
24-
const requestModel = context.request?.json.model as string | undefined;
25-
const requestMetadata: Record<string, unknown> = context?.metadata || {};
20+
let requestModel = context.request?.json.model;
2621

27-
if (!requestModel) {
28-
throw new Error('Missing model in request');
22+
if (!modelList || !Array.isArray(modelList)) {
23+
throw new Error('Missing or invalid model whitelist');
2924
}
3025

31-
// Use explicit models list only
32-
const allowedSet = Array.isArray(modelList)
33-
? modelList.map(String).filter(Boolean)
34-
: [];
35-
36-
if (!Array.isArray(allowedSet) || allowedSet.length === 0) {
37-
throw new Error('Missing allowed models configuration');
26+
if (!requestModel) {
27+
throw new Error('Missing model in request');
3828
}
3929

40-
const inList = allowedSet.includes(requestModel);
30+
const inList = modelList.includes(requestModel);
4131
verdict = not ? !inList : inList;
4232

43-
let explanation = '';
44-
if (verdict) {
45-
explanation = not
46-
? `Model "${requestModel}" is not in the blocked list.`
47-
: `Model "${requestModel}" is allowed.`;
48-
} else {
49-
explanation = not
50-
? `Model "${requestModel}" is in the blocked list.`
51-
: `Model "${requestModel}" is not in the allowed list.`;
52-
}
53-
54-
data = { explanation };
55-
} catch (e) {
56-
const err = e as Error;
57-
error = err;
5833
data = {
59-
explanation: `An error occurred while checking model whitelist: ${err.message}`,
34+
verdict,
35+
not,
36+
explanation: verdict
37+
? not
38+
? `Model "${requestModel}" is not in the allowed list as expected.`
39+
: `Model "${requestModel}" is allowed.`
40+
: not
41+
? `Model "${requestModel}" is in the allowed list when it should not be.`
42+
: `Model "${requestModel}" is not in the allowed list.`,
43+
requestedModel: requestModel,
44+
allowedModels: modelList,
45+
};
46+
} catch (e: any) {
47+
error = e;
48+
data = {
49+
explanation: `An error occurred while checking model whitelist: ${e.message}`,
50+
requestedModel: context.request?.json.model || 'No model specified',
51+
not: parameters.not || false,
52+
allowedModels: parameters.models || [],
6053
};
6154
}
6255

0 commit comments

Comments
 (0)