All files / sdk actionRegistry.ts

80% Statements 36/45
26.31% Branches 10/38
83.33% Functions 5/6
81.81% Lines 36/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1276x 6x                                                 6x         12x 12x       5x 5x 1x   4x 1x   3x     3x 3x 3x           3x 3x                               3x       3x       5x 5x 5x 5x 1x 1x     5x       14x       2x 2x 1x     1x 1x       1x 1x 1x                       1x       1x      
import { z, ZodType, ZodObject, ZodString, AnyZodObject, ZodOptional } from "zod";
import { zodToJsonSchema, JsonSchema7Type } from "zod-to-json-schema";
import { Composio } from ".";
 
export interface CreateActionOptions {
    actionName?: string;
    toolName?: string;
    description?: string;
    inputParams: ZodObject<{ [key: string]: ZodString | ZodOptional<ZodString> }>;
    callback: (inputParams: Record<string, any>, authCredentials: Record<string, any> | undefined) => Promise<Record<string, any>>;
}
 
interface ParamsSchema {
    definitions: {
        input: {
            properties: Record<string, JsonSchema7Type>;
            required?: string[];
        };
    };
}
 
interface ExecuteMetadata {
    entityId?: string;
    connectionId?: string;
}
 
export class ActionRegistry {
    client: Composio;
    customActions: Map<string, { metadata: CreateActionOptions, schema: any }>;
 
    constructor(client: Composio) {
        this.client = client;
        this.customActions = new Map();
    }
 
    async createAction(options: CreateActionOptions): Promise<Record<string, any>> {
        const { callback } = options;
        if (typeof callback !== "function") {
            throw new Error("Callback must be a function");
        }
        if (!options.actionName) {
            throw new Error("You must provide actionName for this action");
        }
        Iif (!options.inputParams) {
            options.inputParams = z.object({});
        }
        const params = options.inputParams;
        const actionName = options.actionName  || callback.name || '';
        const paramsSchema: ParamsSchema = await zodToJsonSchema(
            params,
            {
                name: "input",
            }
        ) as ParamsSchema;
        const _params = paramsSchema.definitions.input.properties;
        const composioSchema = {
            name: actionName,
            description: options.description,
            parameters: {   
                title: actionName,
                type: "object",
                description: options.description,
                required: paramsSchema.definitions.input.required || [],
                properties: _params,
            },
            response: {
                type: "object",
                title: "Response for " + actionName,
                properties: [],
            }
        };
        this.customActions.set(options.actionName?.toLocaleLowerCase() || '', {
            metadata: options,
            schema: composioSchema
         });
        return composioSchema;
    }
 
    async getActions({actions}: {actions: Array<string>}): Promise<Array<any>> {
        const actionsArr: Array<any> = [];
        for (const name of actions) {
            const lowerCaseName = name.toLowerCase();
            if (this.customActions.has(lowerCaseName)) {
                const action = this.customActions.get(lowerCaseName);
                actionsArr.push(action!.schema);
            }
        }
        return actionsArr;
    }
 
    async getAllActions(): Promise<Array<any>> {
        return Array.from(this.customActions.values()).map((action: any) => action);
    }
 
    async executeAction(name: string, inputParams: Record<string, any>, metadata: ExecuteMetadata): Promise<any> {
        const lowerCaseName = name.toLocaleLowerCase();
        if (!this.customActions.has(lowerCaseName)) {
            throw new Error(`Action with name ${name} does not exist`);
        }
 
        const action = this.customActions.get(lowerCaseName);
        Iif (!action) {
            throw new Error(`Action with name ${name} could not be retrieved`);
        }
 
        const { callback, toolName } = action.metadata;
        let authCredentials = {};
        Iif (toolName) {
            const entity = await this.client.getEntity(metadata.entityId);
            const connection = await entity.getConnection(toolName, metadata.connectionId);
            Iif(!connection) {
                throw new Error(`Connection with app name ${toolName} and entityId ${metadata.entityId} not found`);
            }
            authCredentials = {
                headers: connection.connectionParams?.headers,
                queryParams: connection.connectionParams?.queryParams,
                baseUrl: connection.connectionParams?.baseUrl || connection.connectionParams?.base_url, 
            }
        }
        Iif (typeof callback !== "function") {
            throw new Error("Callback must be a function");
        }
 
        return await callback(inputParams, authCredentials);
    }
}