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 | 7x 7x 7x 7x 7x 13x 13x 6x 6x 1x 5x 1x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x 2x 2x 8x 15x 3x 3x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 1x 1x 1x 2x 1x | import { z, ZodType, ZodObject, ZodString, AnyZodObject, ZodOptional, } from "zod"; import { zodToJsonSchema, JsonSchema7Type } from "zod-to-json-schema"; import { ActionProxyRequestConfigDTO } from "./client"; import { Composio } from "."; import apiClient from "../sdk/client/client"; import { CEG } from "./utils/error"; type ExecuteRequest = Omit<ActionProxyRequestConfigDTO, "connectedAccountId">; 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, executeRequest: (data: ExecuteRequest) => Promise<any> ) => 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 = {}; 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` ); } 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"); } const executeRequest = async (data: ExecuteRequest) => { try { const { data: res } = await apiClient.actionsV2.executeActionProxyV2({ body: { ...data, connectedAccountId: metadata?.connectionId, } as ActionProxyRequestConfigDTO, }); return res!; } catch (error) { throw CEG.handleAllError(error); } }; return await callback( inputParams, authCredentials, (data: ExecuteRequest) => executeRequest(data) ); } } |