Skip to content

Commit

Permalink
refactor(actions): refactor createActionTool to use request executor …
Browse files Browse the repository at this point in the history
…pattern
  • Loading branch information
danny-avila committed Oct 28, 2024
1 parent 1b4ea1f commit 1f0f84c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions api/server/services/ActionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,24 @@ async function loadActionSets(searchParams) {
* @param {string | undefined} [params.name] - The name of the tool.
* @param {string | undefined} [params.description] - The description for the tool.
* @param {import('zod').ZodTypeAny | undefined} [params.zodSchema] - The Zod schema for tool input validation/definition
* @returns { Promsie<typeof tool | { _call: (toolInput: Object | string) => unknown}> } An object with `_call` method to execute the tool input.
* @returns { Promise<typeof tool | { _call: (toolInput: Object | string) => unknown}> } An object with `_call` method to execute the tool input.
*/
async function createActionTool({ action, requestBuilder, zodSchema, name, description }) {
action.metadata = await decryptMetadata(action.metadata);
/** @type {(toolInput: Object | string) => Promise<unknown>} */
const _call = async (toolInput) => {
try {
requestBuilder.setParams(toolInput);
const executor = requestBuilder.createExecutor();

// Chain the operations
const preparedExecutor = executor.setParams(toolInput);

if (action.metadata.auth && action.metadata.auth.type !== AuthTypeEnum.None) {
await requestBuilder.setAuth(action.metadata);
await preparedExecutor.setAuth(action.metadata);
}
const res = await requestBuilder.execute();

const res = await preparedExecutor.execute();

if (typeof res.data === 'object') {
return JSON.stringify(res.data);
}
Expand Down

0 comments on commit 1f0f84c

Please sign in to comment.