import type { IndemnClient } from './client.js'; import type { BotFunction, CreateFunctionInput, UpdateFunctionInput, FunctionParameter } from './types.js'; export class FunctionsSDK { constructor(private client: IndemnClient) {} async listFunctions(agentId: string): Promise { return this.client.copilotGet( `/organization/:org_id/ai-studio/bots/${agentId}/functions`, ); } async createFunction(agentId: string, input: CreateFunctionInput): Promise { return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions`, input, ); } async updateFunction(agentId: string, funcId: string, input: UpdateFunctionInput): Promise { return this.client.copilotPut( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}`, input, ); } async deleteFunction(agentId: string, funcId: string): Promise { await this.client.copilotDelete( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}`, ); } async testFunction(agentId: string, funcId: string, params?: Record): Promise { return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}/test-rest-api`, params, ); } async exportFunctions(agentId: string): Promise { return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions/export`, ); } async importFunctions(agentId: string, url: string): Promise { return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions/import`, { url }, ); } async listParameters(agentId: string, funcId: string): Promise { return this.client.copilotGet( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}/parameters`, ); } async addParameter(agentId: string, funcId: string, param: { name: string; type: string; description?: string; required?: boolean }): Promise { // Server expects an array of parameters const payload = [{ name: param.name, type: param.type, description: param.description, is_required: param.required ?? false, }]; return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}/parameters`, payload, ); } async updateParameter(agentId: string, funcId: string, paramId: string, param: { name?: string; type?: string; description?: string; required?: boolean }): Promise { // Server uses POST upsert (no PUT /:id endpoint exists). Include the param id for upsert matching. const payload = [{ id: paramId, ...param, is_required: param.required ?? false, }]; return this.client.copilotPost( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}/parameters`, payload, ); } async deleteParameter(agentId: string, funcId: string, paramId: string): Promise { await this.client.copilotDelete( `/organization/:org_id/ai-studio/bots/${agentId}/functions/${funcId}/parameters/${paramId}`, ); } async listMasterFunctions(): Promise { return this.client.copilotGet( '/organization/:org_id/ai-studio/master-functions', ); } async listLLMModels(): Promise { return this.client.copilotGet( '/organization/:org_id/ai-studio/llm-models', ); } }