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 | 8x 8x 8x 8x 8x 8x 14x 14x 8x 8x 1x 7x 1x 6x 6x 6x 6x 6x 6x 6x 6x 8x 8x 8x 8x 3x 3x 8x 26x 3x 4x 4x 1x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 3x 3x 1x 1x 1x 3x 1x | import { ZodObject, ZodOptional, ZodString, z } from "zod"; import { JsonSchema7Type, zodToJsonSchema } from "zod-to-json-schema"; import { Composio } from "."; import apiClient from "../sdk/client/client"; import { RawActionData } from "../types/base_toolset"; import { ActionProxyRequestConfigDTO, Parameter } from "./client"; import { ActionExecuteResponse } from "./models/actions"; import { CEG } from "./utils/error"; import { COMPOSIO_SDK_ERROR_CODES } from "./utils/errors/src/constants"; type RawExecuteRequestParam = { connectedAccountId?: string; endpoint: string; method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; parameters: Array<Parameter>; body?: { [key: string]: unknown; }; }; type ValidParameters = ZodObject<{ [key: string]: ZodString | ZodOptional<ZodString>; }>; export type Parameters = ValidParameters | z.ZodObject<{}>; type inferParameters<PARAMETERS extends Parameters> = PARAMETERS extends ValidParameters ? z.infer<PARAMETERS> : z.infer<z.ZodObject<{}>>; export type CreateActionOptions<P extends Parameters = z.ZodObject<{}>> = { actionName?: string; toolName?: string; description?: string; inputParams?: P; callback: ( inputParams: inferParameters<P>, authCredentials: Record<string, string> | undefined, executeRequest: ( data: RawExecuteRequestParam ) => Promise<ActionExecuteResponse> ) => Promise<ActionExecuteResponse>; }; 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: Record<string, unknown>; } >; constructor(client: Composio) { this.client = client; this.customActions = new Map(); } async createAction<P extends Parameters = z.ZodObject<{}>>( options: CreateActionOptions<P> ): Promise<RawActionData> { 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({}) as P; } 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 as unknown as RawActionData; } async getActions({ actions, }: { actions: Array<string>; }): Promise<Array<RawActionData>> { const actionsArr: Array<RawActionData> = []; for (const name of actions) { const lowerCaseName = name.toLowerCase(); if (this.customActions.has(lowerCaseName)) { const action = this.customActions.get(lowerCaseName); actionsArr.push(action!.schema as RawActionData); } } return actionsArr; } async getAllActions(): Promise<Array<RawActionData>> { return Array.from(this.customActions.values()).map( (action) => action.schema as RawActionData ); } async executeAction( name: string, inputParams: Record<string, unknown>, metadata: ExecuteMetadata ): Promise<ActionExecuteResponse> { 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 = {}; if (toolName) { const entity = await this.client.getEntity(metadata.entityId); const connection = await entity.getConnection({ app: toolName, connectedAccountId: metadata.connectionId, }); Iif (!connection) { throw new Error( `Connection with app name ${toolName} and entityId ${metadata.entityId} not found` ); } const connectionParams = ( connection as unknown as Record<string, unknown> ).connectionParams as Record<string, unknown>; authCredentials = { headers: connectionParams?.headers, queryParams: connectionParams?.queryParams, baseUrl: connectionParams?.baseUrl || connectionParams?.base_url, }; } Iif (typeof callback !== "function") { throw CEG.getCustomError( COMPOSIO_SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, { message: "Callback must be a function", description: "Please provide a valid callback function", } ); } const executeRequest = async (data: RawExecuteRequestParam) => { try { const { data: res } = await apiClient.actionsV2.executeWithHttpClient({ client: this.client.backendClient.instance, body: { ...data, connectedAccountId: metadata?.connectionId, } as ActionProxyRequestConfigDTO, }); return res!; } catch (error) { throw CEG.handleAllError(error); } }; return await callback( inputParams as Record<string, string>, authCredentials, (data: RawExecuteRequestParam) => executeRequest(data) ); } } |