Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions src/apis/guardrails.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;
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<CreateGuardrailResponse> {
const body = _body;
if (params) {
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params }),
};
}
const response = this.post<CreateGuardrailResponse>(GUARDRAILS_API, {
body,
...opts,
});
return response;
}

list(
_body?: ListGuardrailsParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<ListGuardrailsResponse> {
const body = _body;
if (params) {
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params }),
};
}
const query = body ? toQueryParams(body) : '';
const response = this.getMethod<ListGuardrailsResponse>(
`${GUARDRAILS_API}${query}`,
{ ...opts }
);
return response;
}

retrieve(
_body: GetGuardrailParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<GuardrailDetails> {
const body = _body;
const guardrailId = body.guardrail_id;
if (params) {
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params }),
};
}
const response = this.getMethod<GuardrailDetails>(
`${GUARDRAILS_API}/${guardrailId}`,
{ ...opts }
);
return response;
}

update(
_body: UpdateGuardrailRequest & UpdateGuardrailParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<UpdateGuardrailResponse> {
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<UpdateGuardrailResponse>(
`${GUARDRAILS_API}/${guardrailId}`,
{ body, ...opts }
);
return response;
}

delete(
_body: DeleteGuardrailParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<any> {
const body = _body;
const guardrailId = body.guardrail_id;
if (params) {
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params }),
};
}
const response = this.deleteMethod<any>(
`${GUARDRAILS_API}/${guardrailId}`,
{ body, ...opts }
);
return response;
}
}
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export { MainRealtime } from './mainRealtime';
export { Conversations } from './conversations';
export { Videos } from './videos';
export { ChatKit } from './chatkit';
export { Guardrails } from './guardrails';
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';