|
| 1 | +import { ApiResource } from '../apiResource'; |
| 2 | +import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; |
| 3 | +import { APIPromise, RequestOptions } from '../baseClient'; |
| 4 | +import { createHeaders } from './createHeaders'; |
| 5 | +import { toQueryParams } from '../utils'; |
| 6 | +import { GUARDRAILS_API } from '../constants'; |
| 7 | + |
| 8 | +// Check interface for guardrail checks |
| 9 | +export interface GuardrailCheck { |
| 10 | + id: string; |
| 11 | + parameters?: Record<string, any>; |
| 12 | + name?: string; |
| 13 | + is_enabled?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +// Feedback configuration interface |
| 17 | +export interface GuardrailFeedback { |
| 18 | + value: number; |
| 19 | + weight: number; |
| 20 | + metadata: string; |
| 21 | +} |
| 22 | + |
| 23 | +// Success actions interface |
| 24 | +export interface GuardrailSuccessActions { |
| 25 | + feedback: GuardrailFeedback; |
| 26 | +} |
| 27 | + |
| 28 | +// Fail actions interface |
| 29 | +export interface GuardrailFailActions { |
| 30 | + feedback: GuardrailFeedback; |
| 31 | +} |
| 32 | + |
| 33 | +// Actions interface for guardrail actions |
| 34 | +export interface GuardrailActions { |
| 35 | + deny: boolean; |
| 36 | + async: boolean; |
| 37 | + on_success: GuardrailSuccessActions; |
| 38 | + on_fail: GuardrailFailActions; |
| 39 | +} |
| 40 | + |
| 41 | +// Create Guardrail Request interface |
| 42 | +export interface CreateGuardrailRequest { |
| 43 | + name: string; |
| 44 | + workspace_id?: string; |
| 45 | + organisation_id?: string; |
| 46 | + checks: GuardrailCheck[]; |
| 47 | + actions: GuardrailActions; |
| 48 | +} |
| 49 | + |
| 50 | +// Create Guardrail Response interface |
| 51 | +export interface CreateGuardrailResponse extends APIResponseType { |
| 52 | + id: string; |
| 53 | + slug: string; |
| 54 | + version_id: string; |
| 55 | +} |
| 56 | + |
| 57 | +// List Guardrails Request parameters |
| 58 | +export interface ListGuardrailsParams { |
| 59 | + workspace_id?: string; |
| 60 | + organisation_id?: string; |
| 61 | + page_size?: number; |
| 62 | + current_page?: number; |
| 63 | +} |
| 64 | + |
| 65 | +// Guardrail Summary interface |
| 66 | +export interface GuardrailSummary extends APIResponseType { |
| 67 | + id: string; |
| 68 | + name: string; |
| 69 | + slug: string; |
| 70 | + organisation_id?: string; |
| 71 | + workspace_id?: string | null; |
| 72 | + status: 'active' | 'archived'; |
| 73 | + created_at: string; |
| 74 | + last_updated_at: string; |
| 75 | + owner_id: string; |
| 76 | + updated_by?: string | null; |
| 77 | +} |
| 78 | + |
| 79 | +// List Guardrails Response interface |
| 80 | +export interface ListGuardrailsResponse extends APIResponseType { |
| 81 | + data: GuardrailSummary[]; |
| 82 | + total: number; |
| 83 | +} |
| 84 | + |
| 85 | +// Get Guardrail parameters |
| 86 | +export interface GetGuardrailParams { |
| 87 | + guardrail_id: string; |
| 88 | +} |
| 89 | + |
| 90 | +// Guardrail Details interface |
| 91 | +export interface GuardrailDetails extends GuardrailSummary { |
| 92 | + checks: GuardrailCheck[]; |
| 93 | + actions: GuardrailActions; |
| 94 | +} |
| 95 | + |
| 96 | +// Update Guardrail Request interface |
| 97 | +export interface UpdateGuardrailRequest { |
| 98 | + name?: string; |
| 99 | + checks?: GuardrailCheck[]; |
| 100 | + actions?: GuardrailActions; |
| 101 | +} |
| 102 | + |
| 103 | +// Update Guardrail Response interface |
| 104 | +export interface UpdateGuardrailResponse extends APIResponseType { |
| 105 | + id: string; |
| 106 | + slug: string; |
| 107 | + version_id: string; |
| 108 | +} |
| 109 | + |
| 110 | +// Update Guardrail parameters |
| 111 | +export interface UpdateGuardrailParams { |
| 112 | + guardrail_id?: string; |
| 113 | + workspace_id?: string; |
| 114 | +} |
| 115 | + |
| 116 | +// Delete Guardrail parameters |
| 117 | +export interface DeleteGuardrailParams { |
| 118 | + guardrail_id: string; |
| 119 | +} |
| 120 | + |
| 121 | +export class Guardrails extends ApiResource { |
| 122 | + create( |
| 123 | + _body: CreateGuardrailRequest, |
| 124 | + params?: ApiClientInterface, |
| 125 | + opts?: RequestOptions |
| 126 | + ): APIPromise<CreateGuardrailResponse> { |
| 127 | + const body = _body; |
| 128 | + if (params) { |
| 129 | + this.client.customHeaders = { |
| 130 | + ...this.client.customHeaders, |
| 131 | + ...createHeaders({ ...params }), |
| 132 | + }; |
| 133 | + } |
| 134 | + const response = this.post<CreateGuardrailResponse>(GUARDRAILS_API, { |
| 135 | + body, |
| 136 | + ...opts, |
| 137 | + }); |
| 138 | + return response; |
| 139 | + } |
| 140 | + |
| 141 | + list( |
| 142 | + _body?: ListGuardrailsParams, |
| 143 | + params?: ApiClientInterface, |
| 144 | + opts?: RequestOptions |
| 145 | + ): APIPromise<ListGuardrailsResponse> { |
| 146 | + const body = _body; |
| 147 | + if (params) { |
| 148 | + this.client.customHeaders = { |
| 149 | + ...this.client.customHeaders, |
| 150 | + ...createHeaders({ ...params }), |
| 151 | + }; |
| 152 | + } |
| 153 | + const query = body ? toQueryParams(body) : ''; |
| 154 | + const response = this.getMethod<ListGuardrailsResponse>( |
| 155 | + `${GUARDRAILS_API}${query}`, |
| 156 | + { ...opts } |
| 157 | + ); |
| 158 | + return response; |
| 159 | + } |
| 160 | + |
| 161 | + retrieve( |
| 162 | + _body: GetGuardrailParams, |
| 163 | + params?: ApiClientInterface, |
| 164 | + opts?: RequestOptions |
| 165 | + ): APIPromise<GuardrailDetails> { |
| 166 | + const body = _body; |
| 167 | + const guardrailId = body.guardrail_id; |
| 168 | + if (params) { |
| 169 | + this.client.customHeaders = { |
| 170 | + ...this.client.customHeaders, |
| 171 | + ...createHeaders({ ...params }), |
| 172 | + }; |
| 173 | + } |
| 174 | + const response = this.getMethod<GuardrailDetails>( |
| 175 | + `${GUARDRAILS_API}/${guardrailId}`, |
| 176 | + { ...opts } |
| 177 | + ); |
| 178 | + return response; |
| 179 | + } |
| 180 | + |
| 181 | + update( |
| 182 | + _body: UpdateGuardrailRequest & UpdateGuardrailParams, |
| 183 | + params?: ApiClientInterface, |
| 184 | + opts?: RequestOptions |
| 185 | + ): APIPromise<UpdateGuardrailResponse> { |
| 186 | + const body = _body; |
| 187 | + const guardrailId = body.guardrail_id; |
| 188 | + delete body.guardrail_id; |
| 189 | + if (params) { |
| 190 | + this.client.customHeaders = { |
| 191 | + ...this.client.customHeaders, |
| 192 | + ...createHeaders({ ...params }), |
| 193 | + }; |
| 194 | + } |
| 195 | + const response = this.put<UpdateGuardrailResponse>( |
| 196 | + `${GUARDRAILS_API}/${guardrailId}`, |
| 197 | + { body, ...opts } |
| 198 | + ); |
| 199 | + return response; |
| 200 | + } |
| 201 | + |
| 202 | + delete( |
| 203 | + _body: DeleteGuardrailParams, |
| 204 | + params?: ApiClientInterface, |
| 205 | + opts?: RequestOptions |
| 206 | + ): APIPromise<any> { |
| 207 | + const body = _body; |
| 208 | + const guardrailId = body.guardrail_id; |
| 209 | + if (params) { |
| 210 | + this.client.customHeaders = { |
| 211 | + ...this.client.customHeaders, |
| 212 | + ...createHeaders({ ...params }), |
| 213 | + }; |
| 214 | + } |
| 215 | + const response = this.deleteMethod<any>( |
| 216 | + `${GUARDRAILS_API}/${guardrailId}`, |
| 217 | + { body, ...opts } |
| 218 | + ); |
| 219 | + return response; |
| 220 | + } |
| 221 | +} |
0 commit comments