-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support custom actions in vercel toolkit (with type safety improvements) #1175
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { jsonSchema, tool } from "ai"; | ||
import { jsonSchema, tool, CoreTool } from "ai"; | ||
import { z } from "zod"; | ||
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset"; | ||
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry"; | ||
|
@@ -62,7 +62,7 @@ export class VercelAIToolSet extends BaseComposioToolSet { | |
useCase?: Optional<string>; | ||
usecaseLimit?: Optional<number>; | ||
filterByAvailableApps?: Optional<boolean>; | ||
}): Promise<{ [key: string]: RawActionData }> { | ||
}): Promise<{ [key: string]: CoreTool }> { | ||
TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, { | ||
method: "getTools", | ||
file: this.fileName, | ||
|
@@ -78,21 +78,19 @@ export class VercelAIToolSet extends BaseComposioToolSet { | |
actions, | ||
} = ZExecuteToolCallParams.parse(filters); | ||
|
||
const actionsList = await this.client.actions.list({ | ||
...(apps && { apps: apps?.join(",") }), | ||
...(tags && { tags: tags?.join(",") }), | ||
...(useCase && { useCase: useCase }), | ||
...(actions && { actions: actions?.join(",") }), | ||
...(usecaseLimit && { usecaseLimit: usecaseLimit }), | ||
filterByAvailableApps: filterByAvailableApps ?? undefined, | ||
}); | ||
const actionsList = await this.getToolsSchema({ | ||
apps, | ||
actions, | ||
tags, | ||
useCase, | ||
useCaseLimit: usecaseLimit, | ||
filterByAvailableApps | ||
}) | ||
|
||
const tools = {}; | ||
actionsList.items?.forEach((actionSchema) => { | ||
// @ts-ignore | ||
const tools: { [key: string]: CoreTool } = {}; | ||
actionsList.forEach((actionSchema) => { | ||
tools[actionSchema.name!] = this.generateVercelTool( | ||
// @ts-ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a type guard instead of non-null assertion on if (!actionSchema.name) {
console.warn('Action schema missing name, skipping...');
return;
}
tools[actionSchema.name] = this.generateVercelTool(actionSchema); |
||
actionSchema as ActionData | ||
actionSchema | ||
); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,25 @@ type RawExecuteRequestParam = { | |
}; | ||
}; | ||
|
||
export type CreateActionOptions = { | ||
|
||
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: ZodObject<{ [key: string]: ZodString | ZodOptional<ZodString> }>; | ||
inputParams?: P; | ||
callback: ( | ||
inputParams: Record<string, string>, | ||
inputParams: inferParameters<P>, | ||
authCredentials: Record<string, string> | undefined, | ||
executeRequest: ( | ||
data: RawExecuteRequestParam | ||
|
@@ -50,15 +62,19 @@ export class ActionRegistry { | |
client: Composio; | ||
customActions: Map< | ||
string, | ||
{ metadata: CreateActionOptions; schema: Record<string, unknown> } | ||
{ | ||
metadata: CreateActionOptions; | ||
schema: Record<string, unknown> | ||
} | ||
>; | ||
|
||
constructor(client: Composio) { | ||
this.client = client; | ||
this.customActions = new Map(); | ||
} | ||
|
||
async createAction(options: CreateActionOptions): Promise<RawActionData> { | ||
|
||
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"); | ||
|
@@ -67,7 +83,7 @@ export class ActionRegistry { | |
throw new Error("You must provide actionName for this action"); | ||
} | ||
if (!options.inputParams) { | ||
options.inputParams = z.object({}); | ||
options.inputParams = z.object({}) as P | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type assertion |
||
} | ||
const params = options.inputParams; | ||
const actionName = options.actionName || callback.name || ""; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { z } from "zod"; | ||
import { z, ZodObject } from "zod"; | ||
import { Composio } from "../sdk"; | ||
import { | ||
RawActionData, | ||
|
@@ -10,7 +10,7 @@ | |
} from "../types/base_toolset"; | ||
import type { Optional, Sequence } from "../types/util"; | ||
import { getEnvVariable } from "../utils/shared"; | ||
import { ActionRegistry, CreateActionOptions } from "./actionRegistry"; | ||
import { ActionRegistry, CreateActionOptions, Parameters } from "./actionRegistry"; | ||
import { ActionExecutionResDto } from "./client/types.gen"; | ||
import { ActionExecuteResponse, Actions } from "./models/actions"; | ||
import { ActiveTriggers } from "./models/activeTriggers"; | ||
|
@@ -54,10 +54,10 @@ | |
post: TPostProcessor[]; | ||
schema: TSchemaProcessor[]; | ||
} = { | ||
pre: [fileInputProcessor], | ||
post: [fileResponseProcessor], | ||
schema: [fileSchemaProcessor], | ||
}; | ||
pre: [fileInputProcessor], | ||
post: [fileResponseProcessor], | ||
schema: [fileSchemaProcessor], | ||
}; | ||
|
||
private userDefinedProcessors: { | ||
pre?: TPreProcessor; | ||
|
@@ -172,8 +172,8 @@ | |
}); | ||
} | ||
|
||
async createAction(options: CreateActionOptions) { | ||
return this.userActionRegistry.createAction(options); | ||
async createAction<P extends Parameters = z.ZodObject<{}>>(options: CreateActionOptions<P>) { | ||
return this.userActionRegistry.createAction<P>(options); | ||
} | ||
|
||
private isCustomAction(action: string) { | ||
Comment on lines
172
to
179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor: The change ensures type safety by using generics, aligning with the |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding error case tests for the callback function. The current test only covers the success case. Add tests for scenarios where the callback might fail or return unsuccessful results.