File size: 1,796 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export interface GuardrailLog {
  debug?: (tag: string, message: string, meta?: Record<string, unknown>) => void;
  info?: (tag: string, message: string, meta?: Record<string, unknown>) => void;
  warn?: (tag: string, message: string, meta?: Record<string, unknown>) => void;
  error?: (tag: string, message: string, meta?: Record<string, unknown>) => void;
}

export interface GuardrailContext {
  apiKeyInfo?: Record<string, unknown> | null;
  disabledGuardrails?: string[] | null;
  endpoint?: string | null;
  headers?: Headers | Record<string, unknown> | null;
  log?: GuardrailLog | Console | null;
  method?: string | null;
  model?: string | null;
  provider?: string | null;
  sourceFormat?: string | null;
  stream?: boolean;
  targetFormat?: string | null;
}

export interface GuardrailResult<TValue = unknown> {
  block?: boolean;
  message?: string;
  meta?: Record<string, unknown> | null;
  modifiedPayload?: TValue;
  modifiedResponse?: TValue;
}

export interface GuardrailExecutionResult {
  blocked: boolean;
  error?: string;
  guardrail: string;
  message?: string;
  meta?: Record<string, unknown> | null;
  modified: boolean;
  skipped: boolean;
  stage: "pre" | "post";
}

export class BaseGuardrail {
  enabled: boolean;
  name: string;
  priority: number;

  constructor(name: string, options: { enabled?: boolean; priority?: number } = {}) {
    this.name = name;
    this.enabled = options.enabled !== false;
    this.priority = options.priority ?? 100;
  }

  async preCall(
    _payload: unknown,
    _context: GuardrailContext
  ): Promise<GuardrailResult<unknown> | void> {
    return { block: false };
  }

  async postCall(
    _response: unknown,
    _context: GuardrailContext
  ): Promise<GuardrailResult<unknown> | void> {
    return { block: false };
  }
}