From 5e0338879013a67d2bf37c86e4b50d9cc0857749 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Fri, 11 Jul 2025 16:51:11 +0530 Subject: [PATCH 1/2] feat: guardrails API Support --- src/apis/guardrails.ts | 221 +++++++++++++++++++++++++++++++++++++++++ src/apis/index.ts | 1 + src/client.ts | 1 + src/constants.ts | 1 + 4 files changed, 224 insertions(+) create mode 100644 src/apis/guardrails.ts diff --git a/src/apis/guardrails.ts b/src/apis/guardrails.ts new file mode 100644 index 0000000..d8a2682 --- /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 { + guardrailId: 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 { + guardrailId?: string; + workspace_id?: string; +} + +// Delete Guardrail parameters +export interface DeleteGuardrailParams { + guardrailId: 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.guardrailId; + 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.guardrailId; + delete body.guardrailId; + 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.guardrailId; + 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 9daf9c1..f0f889c 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -31,3 +31,4 @@ export { Labels } from './labels'; export { Collections } from './collections'; export { Evals } from './evals'; export { Containers } from './containers'; +export { Guardrails } from './guardrails'; diff --git a/src/client.ts b/src/client.ts index 6f6e463..0486793 100644 --- a/src/client.ts +++ b/src/client.ts @@ -210,6 +210,7 @@ export class Portkey extends ApiClient { apiKeys = new API.ApiKeys(this); configs = new API.Configs(this); logs = new API.Logs(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 85505e1..b85071b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -50,4 +50,5 @@ export const PROMPTS_API = '/prompts'; export const PROMPT_PARTIALS_API = '/prompts/partials'; export const LABELS_API = '/labels'; export const COLLECTIONS_API = '/collections'; +export const GUARDRAILS_API = '/guardrails'; export const AUDIO_FILE_DURATION_HEADER = 'audio-file-duration'; From 9ff9fad69999fe2c4072ec7410f8345a5d5d3072 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Thu, 16 Oct 2025 18:53:51 +0530 Subject: [PATCH 2/2] feat: snake case the id --- src/apis/guardrails.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/apis/guardrails.ts b/src/apis/guardrails.ts index d8a2682..d520e77 100644 --- a/src/apis/guardrails.ts +++ b/src/apis/guardrails.ts @@ -84,7 +84,7 @@ export interface ListGuardrailsResponse extends APIResponseType { // Get Guardrail parameters export interface GetGuardrailParams { - guardrailId: string; + guardrail_id: string; } // Guardrail Details interface @@ -109,13 +109,13 @@ export interface UpdateGuardrailResponse extends APIResponseType { // Update Guardrail parameters export interface UpdateGuardrailParams { - guardrailId?: string; + guardrail_id?: string; workspace_id?: string; } // Delete Guardrail parameters export interface DeleteGuardrailParams { - guardrailId: string; + guardrail_id: string; } export class Guardrails extends ApiResource { @@ -164,7 +164,7 @@ export class Guardrails extends ApiResource { opts?: RequestOptions ): APIPromise { const body = _body; - const guardrailId = body.guardrailId; + const guardrailId = body.guardrail_id; if (params) { this.client.customHeaders = { ...this.client.customHeaders, @@ -184,8 +184,8 @@ export class Guardrails extends ApiResource { opts?: RequestOptions ): APIPromise { const body = _body; - const guardrailId = body.guardrailId; - delete body.guardrailId; + const guardrailId = body.guardrail_id; + delete body.guardrail_id; if (params) { this.client.customHeaders = { ...this.client.customHeaders, @@ -205,7 +205,7 @@ export class Guardrails extends ApiResource { opts?: RequestOptions ): APIPromise { const body = _body; - const guardrailId = body.guardrailId; + const guardrailId = body.guardrail_id; if (params) { this.client.customHeaders = { ...this.client.customHeaders,