|
| 1 | +import type { |
| 2 | + HookEventType, |
| 3 | + PluginContext, |
| 4 | + PluginHandler, |
| 5 | + PluginParameters, |
| 6 | +} from '../types'; |
| 7 | + |
| 8 | +interface RulesData { |
| 9 | + explanation: string; |
| 10 | +} |
| 11 | + |
| 12 | +export const handler: PluginHandler = async ( |
| 13 | + context: PluginContext, |
| 14 | + parameters: PluginParameters, |
| 15 | + eventType: HookEventType |
| 16 | +) => { |
| 17 | + let error = null; |
| 18 | + let verdict = false; |
| 19 | + let data: RulesData | null = null; |
| 20 | + |
| 21 | + try { |
| 22 | + const rulesConfig = parameters.rules as Record<string, unknown> | undefined; |
| 23 | + const not = parameters.not || false; |
| 24 | + const requestModel = context.request?.json.model as string | undefined; |
| 25 | + const requestMetadata: Record<string, unknown> = context?.metadata || {}; |
| 26 | + |
| 27 | + if (!requestModel) { |
| 28 | + throw new Error('Missing model in request'); |
| 29 | + } |
| 30 | + |
| 31 | + if (!rulesConfig || typeof rulesConfig !== 'object') { |
| 32 | + throw new Error('Missing rules configuration'); |
| 33 | + } |
| 34 | + |
| 35 | + type RulesShape = { |
| 36 | + defaults?: unknown; |
| 37 | + metadata?: unknown; |
| 38 | + }; |
| 39 | + const cfg = rulesConfig as RulesShape; |
| 40 | + |
| 41 | + const defaultsArray = Array.isArray(cfg.defaults) |
| 42 | + ? (cfg.defaults as unknown[]) |
| 43 | + : []; |
| 44 | + const defaults = defaultsArray.map((m) => String(m)); |
| 45 | + |
| 46 | + const metadata = |
| 47 | + cfg.metadata && typeof cfg.metadata === 'object' |
| 48 | + ? (cfg.metadata as Record<string, Record<string, unknown>>) |
| 49 | + : {}; |
| 50 | + |
| 51 | + const matched = new Set<string>(); |
| 52 | + const matchedRules: string[] = []; |
| 53 | + |
| 54 | + for (const [key, mapping] of Object.entries(metadata)) { |
| 55 | + const reqVal = requestMetadata[key]; |
| 56 | + if (reqVal === undefined || reqVal === null) continue; |
| 57 | + |
| 58 | + const reqVals = Array.isArray(reqVal) |
| 59 | + ? reqVal.map((v) => String(v)) |
| 60 | + : [String(reqVal)]; |
| 61 | + |
| 62 | + for (const val of reqVals) { |
| 63 | + const modelsUnknown = (mapping as Record<string, unknown>)[val]; |
| 64 | + if (Array.isArray(modelsUnknown)) { |
| 65 | + const models = (modelsUnknown as unknown[]).filter( |
| 66 | + (m) => typeof m === 'string' |
| 67 | + ) as string[]; |
| 68 | + matchedRules.push(`${key}:${val}`); |
| 69 | + for (const m of models) { |
| 70 | + if (m && typeof m === 'string') { |
| 71 | + matched.add(String(m)); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + let allowedSet = Array.from(matched); |
| 79 | + let usingDefaults = false; |
| 80 | + if (allowedSet.length === 0) { |
| 81 | + allowedSet = defaults; |
| 82 | + usingDefaults = true; |
| 83 | + } |
| 84 | + |
| 85 | + if (!Array.isArray(allowedSet) || allowedSet.length === 0) { |
| 86 | + throw new Error('No allowed models resolved from rules'); |
| 87 | + } |
| 88 | + |
| 89 | + const inList = allowedSet.includes(requestModel); |
| 90 | + verdict = not ? !inList : inList; |
| 91 | + |
| 92 | + let explanation = ''; |
| 93 | + if (verdict) { |
| 94 | + explanation = not |
| 95 | + ? `Model "${requestModel}" is not permitted by rules (blocked list).` |
| 96 | + : `Model "${requestModel}" is allowed by rules.`; |
| 97 | + if (matchedRules.length) { |
| 98 | + explanation += ` (matched rules: ${matchedRules.join(', ')})`; |
| 99 | + } else if (usingDefaults) { |
| 100 | + explanation += ' (using default models)'; |
| 101 | + } |
| 102 | + } else { |
| 103 | + explanation = not |
| 104 | + ? `Model "${requestModel}" is permitted by rules (in blocked list).` |
| 105 | + : `Model "${requestModel}" is not allowed by rules.`; |
| 106 | + } |
| 107 | + |
| 108 | + data = { explanation }; |
| 109 | + } catch (e) { |
| 110 | + const err = e as Error; |
| 111 | + error = err; |
| 112 | + data = { |
| 113 | + explanation: `An error occurred while checking model rules: ${err.message}`, |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + return { error, verdict, data }; |
| 118 | +}; |
0 commit comments