All files / sdk/models actions.ts

59.37% Statements 19/32
67.74% Branches 21/31
62.5% Functions 5/8
60% Lines 18/30

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281  11x 11x                                                                                                                                                                                                                                                                                             11x       25x                         4x 4x           4x                               20x   20x     20x       20x   1x 10x     20x                       20x                               5x 5x           5x                                                                                          
import { ActionExecutionReqDTO, ActionProxyRequestConfigDTO, ActionsListResponseDTO, AdvancedUseCaseSearchData } from "../client";
import apiClient from "../client/client";
import { CEG } from "../utils/error";
import { BackendClient } from "./backendClient";
 
/**
 * The `Actions` class provides methods to interact with the Composio platform's actions.
 * It allows fetching details of specific actions, listing all actions, and executing actions.
 *
 * - `get` method retrieves details of a specific action.
 * - `list` method retrieves a list of all actions.
 * - `execute` method executes a specific action.
 *
 * Each method returns a `CancelablePromise` which can be canceled. If canceled, the promise
 * will reject with a `Cancellation` object.
 *
 * @typeParam Composio The client configuration object type.
 * @groupDescription Methods
 * The methods in this class are grouped under 'Actions Methods' and provide functionalities
 * to interact with actions in the Composio platform. This includes fetching, listing, and
 * executing actions.
 */
 
export type GetListActionsData = {
    /**
     * Name of the apps like "github", "linear" separated by a comma
     */
    apps?: string;
    /**
     * Filter by Action names
     */
    actions?: string;
    /**
     * Filter by Action tags
     */
    tags?: string;
    /**
     * Filter by use case
     */
    useCase?: string | undefined;
    /**
     * Limit of use-cases based search
     */
    usecaseLimit?: number;
    /**
     * Show all actions - i.e disable pagination
     */
    showAll?: boolean;
    /**
     * Show actions enabled for the API Key
     */
    showEnabledOnly?: boolean;
    /**
     * Use smart tag filtering
     */
    filterImportantActions?: boolean;
    /**
     * Should search in available apps only
     */
    filterByAvailableApps?: boolean;
}
 
export type Parameter = {
    /**
     * The name of the parameter.
     */
    name: string;
 
    /**
     * The location of the parameter (e.g., query, header).
     */
    in: string;
 
    /**
     * The value of the parameter.
     */
    value: string | number;
};
 
export type CustomAuthData = {
    /**
     * The base URL for the custom authentication.
     */
    base_url?: string;
 
    /**
     * An array of parameters for the custom authentication.
     */
    parameters: Parameter[];
 
    /**
     * An optional object containing the body for the custom authentication.
     */
    body?: Record<string, unknown>;
}
 
export type ExecuteActionData = {
    /**
     * The name of the action to execute.
     */
    actionName: string;
    requestBody?: {
        /**
         * The unique identifier of the connection to use for executing the action.
         */
        connectedAccountId?: string;
        /**
         * An object containing the input parameters for the action. If you want to execute 
         * NLP based action (i.e text), you can use text parameter instead of input.
         */
        input?: {
            [key: string]: unknown;
        };
        appName?: string;
        /**
         * The text to supply to the action which will be automatically converted to 
         * appropriate input parameters.
         */
        text?: string;
 
        /**
         * The custom authentication configuration for executing the action.
         */
        authConfig?: CustomAuthData;
    };
};
 
export type ExecuteActionResponse = {
    /**
     * An object containing the details of the action execution.
     */
    execution_details?: {
        /**
         * A boolean indicating whether the action was executed successfully.
         *
         */
        executed?: boolean;
    };
    /**
     * An object containing the response data from the action execution.
     */
    response_data?: {
        [key: string]: unknown;
    };
};
export class Actions {
    backendClient: BackendClient;
 
    constructor(backendClient: BackendClient) {
        this.backendClient = backendClient;
    }
 
    /**
     * Retrieves details of a specific action in the Composio platform by providing its action name.
     * 
     * The response includes the action's name, display name, description, input parameters, expected response, associated app information, and enabled status.
     * 
     * @param {GetActionData} data The data for the request.
     * @returns {CancelablePromise<GetActionResponse[0]>} A promise that resolves to the details of the action.
     * @throws {ApiError} If the request fails.
     */
    async get(data: { actionName: string; }) {
        try{
        const actions = await apiClient.actionsV2.getActionV2({
            path: {
                actionId: data.actionName
            }
        });
 
            return (actions.data!);
        } catch(e){
            throw CEG.handleAllError(e)
        }
    }
 
    /**
     * Retrieves a list of all actions in the Composio platform.
     * 
     * This method allows you to fetch a list of all the available actions. It supports pagination to handle large numbers of actions. The response includes an array of action objects, each containing information such as the action's name, display name, description, input parameters, expected response, associated app information, and enabled status.
     * 
     * @param {GetListActionsData} data The data for the request.
     * @returns {Promise<ActionsListResponseDTO>} A promise that resolves to the list of all actions.
     * @throws {ApiError} If the request fails.
     */
    async list(data: GetListActionsData = {}): Promise<ActionsListResponseDTO> {
        try {
 
            let apps = data.apps;
            
            // Throw error if user has provided both filterByAvailableApps and apps
            Iif(data?.filterByAvailableApps && data?.apps){
                throw new Error("Both filterByAvailableApps and apps cannot be provided together");
            }
 
            if(data?.filterByAvailableApps){
                // Todo: To create a new API to get all integrated apps for a user instead of fetching all apps
                const integratedApps = await apiClient.appConnector.listAllConnectors();
                apps = integratedApps.data?.items.map((app)=> app?.appName).join(",");
            }
            
            const response = await apiClient.actionsV2.listActionsV2({
                query: {
                    actions: data.actions,
                    apps: apps,
                    showAll: data.showAll,
                    tags: data.tags,
                    useCase: data.useCase as string,
                    filterImportantActions: data.filterImportantActions,
                    showEnabledOnly: data.showEnabledOnly,
                    usecaseLimit: data.usecaseLimit || undefined
                }
            });
            return response.data!;
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
 
    /**
     * Executes a specific action in the Composio platform.
     * 
     * This method allows you to trigger the execution of an action by providing its name and the necessary input parameters. The request includes the connected account ID to identify the app connection to use for the action, and the input parameters required by the action. The response provides details about the execution status and the response data returned by the action.
     * 
     * @param {ExecuteActionData} data The data for the request.
     * @returns {Promise<ActionExecutionResDto>} A promise that resolves to the execution status and response data.
     * @throws {ApiError} If the request fails.
     */
    async execute(data: ExecuteActionData){
        try {
            const { data: res } = await apiClient.actionsV2.executeActionV2({
                body: data.requestBody as unknown as ActionExecutionReqDTO,
                path: {
                    actionId: data.actionName
                }
            });
            return res!;
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
 
    async findActionEnumsByUseCase(data: {
        apps: Array<string>,
        useCase: string,
        limit?: number,
    }): Promise<Array<string>> {
        try {
            const { data: res } = await apiClient.actionsV2.advancedUseCaseSearch({
                query: {
                    apps: data.apps.join(","),
                    useCase: data.useCase,
                    limit: data.limit || undefined
                }
            });
            return res!.items.map((item) => item.actions).flat() || [];
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
 
    /**
     * Executes a action using Composio Proxy
     * 
     * This method allows you to trigger the execution of an action by providing its name and the necessary input parameters. The request includes the connected account ID to identify the app connection to use for the action, and the input parameters required by the action. The response provides details about the execution status and the response data returned by the action.
     * 
     * @param {ExecuteActionData} data The data for the request.
     * @returns {Promise<ActionExecutionResDto>} A promise that resolves to the execution status and response data.
     * @throws {ApiError} If the request fails.
     */
 
    async executeRequest(data: ActionProxyRequestConfigDTO){
        try {
            const { data: res } = await apiClient.actionsV2.executeActionProxyV2({
                body: data as unknown as ActionProxyRequestConfigDTO
            });
            return res!;
        } catch (error) {
            throw CEG.handleAllError(error);
        }
    }
}