diff --git a/src/apis/guardrails.ts b/src/apis/guardrails.ts new file mode 100644 index 0000000..d520e77 --- /dev/null +++ b/src/apis/guardrails.ts @@ -0,0 +1,221 @@ +import { ApiResource } from '../apiResource'; +import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; +import { APIPromise, RequestOptions } from '../baseClient'; +import { createHeaders } from './createHeaders'; +import { toQueryParams } from '../utils'; +import { GUARDRAILS_API } from '../constants'; + +// Check interface for guardrail checks +export interface GuardrailCheck { + id: string; + parameters?: Record; + name?: string; + is_enabled?: boolean; +} + +// Feedback configuration interface +export interface GuardrailFeedback { + value: number; + weight: number; + metadata: string; +} + +// Success actions interface +export interface GuardrailSuccessActions { + feedback: GuardrailFeedback; +} + +// Fail actions interface +export interface GuardrailFailActions { + feedback: GuardrailFeedback; +} + +// Actions interface for guardrail actions +export interface GuardrailActions { + deny: boolean; + async: boolean; + on_success: GuardrailSuccessActions; + on_fail: GuardrailFailActions; +} + +// Create Guardrail Request interface +export interface CreateGuardrailRequest { + name: string; + workspace_id?: string; + organisation_id?: string; + checks: GuardrailCheck[]; + actions: GuardrailActions; +} + +// Create Guardrail Response interface +export interface CreateGuardrailResponse extends APIResponseType { + id: string; + slug: string; + version_id: string; +} + +// List Guardrails Request parameters +export interface ListGuardrailsParams { + workspace_id?: string; + organisation_id?: string; + page_size?: number; + current_page?: number; +} + +// Guardrail Summary interface +export interface GuardrailSummary extends APIResponseType { + id: string; + name: string; + slug: string; + organisation_id?: string; + workspace_id?: string | null; + status: 'active' | 'archived'; + created_at: string; + last_updated_at: string; + owner_id: string; + updated_by?: string | null; +} + +// List Guardrails Response interface +export interface ListGuardrailsResponse extends APIResponseType { + data: GuardrailSummary[]; + total: number; +} + +// Get Guardrail parameters +export interface GetGuardrailParams { + guardrail_id: string; +} + +// Guardrail Details interface +export interface GuardrailDetails extends GuardrailSummary { + checks: GuardrailCheck[]; + actions: GuardrailActions; +} + +// Update Guardrail Request interface +export interface UpdateGuardrailRequest { + name?: string; + checks?: GuardrailCheck[]; + actions?: GuardrailActions; +} + +// Update Guardrail Response interface +export interface UpdateGuardrailResponse extends APIResponseType { + id: string; + slug: string; + version_id: string; +} + +// Update Guardrail parameters +export interface UpdateGuardrailParams { + guardrail_id?: string; + workspace_id?: string; +} + +// Delete Guardrail parameters +export interface DeleteGuardrailParams { + guardrail_id: string; +} + +export class Guardrails extends ApiResource { + create( + _body: CreateGuardrailRequest, + params?: ApiClientInterface, + opts?: RequestOptions + ): APIPromise { + const body = _body; + if (params) { + this.client.customHeaders = { + ...this.client.customHeaders, + ...createHeaders({ ...params }), + }; + } + const response = this.post(GUARDRAILS_API, { + body, + ...opts, + }); + return response; + } + + list( + _body?: ListGuardrailsParams, + params?: ApiClientInterface, + opts?: RequestOptions + ): APIPromise { + const body = _body; + if (params) { + this.client.customHeaders = { + ...this.client.customHeaders, + ...createHeaders({ ...params }), + }; + } + const query = body ? toQueryParams(body) : ''; + const response = this.getMethod( + `${GUARDRAILS_API}${query}`, + { ...opts } + ); + return response; + } + + retrieve( + _body: GetGuardrailParams, + params?: ApiClientInterface, + opts?: RequestOptions + ): APIPromise { + const body = _body; + const guardrailId = body.guardrail_id; + if (params) { + this.client.customHeaders = { + ...this.client.customHeaders, + ...createHeaders({ ...params }), + }; + } + const response = this.getMethod( + `${GUARDRAILS_API}/${guardrailId}`, + { ...opts } + ); + return response; + } + + update( + _body: UpdateGuardrailRequest & UpdateGuardrailParams, + params?: ApiClientInterface, + opts?: RequestOptions + ): APIPromise { + const body = _body; + const guardrailId = body.guardrail_id; + delete body.guardrail_id; + if (params) { + this.client.customHeaders = { + ...this.client.customHeaders, + ...createHeaders({ ...params }), + }; + } + const response = this.put( + `${GUARDRAILS_API}/${guardrailId}`, + { body, ...opts } + ); + return response; + } + + delete( + _body: DeleteGuardrailParams, + params?: ApiClientInterface, + opts?: RequestOptions + ): APIPromise { + const body = _body; + const guardrailId = body.guardrail_id; + if (params) { + this.client.customHeaders = { + ...this.client.customHeaders, + ...createHeaders({ ...params }), + }; + } + const response = this.deleteMethod( + `${GUARDRAILS_API}/${guardrailId}`, + { body, ...opts } + ); + return response; + } +} diff --git a/src/apis/index.ts b/src/apis/index.ts index 9cf88e7..3bbb992 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -37,3 +37,4 @@ export { MainRealtime } from './mainRealtime'; export { Conversations } from './conversations'; export { Videos } from './videos'; export { ChatKit } from './chatkit'; +export { Guardrails } from './guardrails'; diff --git a/src/client.ts b/src/client.ts index 6665e46..1566c3e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -216,7 +216,7 @@ export class Portkey extends ApiClient { realtime = new API.MainRealtime(this); conversations = new API.Conversations(this); videos = new API.Videos(this); - + guardrails = new API.Guardrails(this); beta = { assistants: new API.Assistants(this), threads: new API.Threads(this), diff --git a/src/constants.ts b/src/constants.ts index 12cc9fa..14b01d0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -52,4 +52,5 @@ export const LABELS_API = '/labels'; export const COLLECTIONS_API = '/collections'; export const INTEGRATIONS_API = '/integrations'; export const PROVIDERS_API = '/providers'; +export const GUARDRAILS_API = '/guardrails'; export const AUDIO_FILE_DURATION_HEADER = 'audio-file-duration';