|
1 | | -import type { |
| 1 | +import { |
2 | 2 | HookEventType, |
3 | 3 | PluginContext, |
4 | 4 | PluginHandler, |
5 | 5 | PluginParameters, |
6 | 6 | } from '../types'; |
7 | 7 |
|
8 | | -interface WhitelistData { |
9 | | - explanation: string; |
10 | | -} |
11 | | - |
12 | 8 | export const handler: PluginHandler = async ( |
13 | 9 | context: PluginContext, |
14 | 10 | parameters: PluginParameters, |
15 | 11 | eventType: HookEventType |
16 | 12 | ) => { |
17 | 13 | let error = null; |
18 | 14 | let verdict = false; |
19 | | - let data: WhitelistData | null = null; |
| 15 | + let data: any = null; |
20 | 16 |
|
21 | 17 | try { |
22 | 18 | const modelList = parameters.models; |
23 | 19 | 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; |
26 | 21 |
|
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'); |
29 | 24 | } |
30 | 25 |
|
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'); |
38 | 28 | } |
39 | 29 |
|
40 | | - const inList = allowedSet.includes(requestModel); |
| 30 | + const inList = modelList.includes(requestModel); |
41 | 31 | verdict = not ? !inList : inList; |
42 | 32 |
|
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; |
58 | 33 | 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 || [], |
60 | 53 | }; |
61 | 54 | } |
62 | 55 |
|
|
0 commit comments