Skip to content

Commit

Permalink
Merge pull request #1181 from ComposioHQ/develop
Browse files Browse the repository at this point in the history
feat: develop -> master
  • Loading branch information
himanshu-dixit authored Jan 14, 2025
2 parents 6ac3758 + 46b606e commit 545f220
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 26 deletions.
2 changes: 1 addition & 1 deletion js/src/frameworks/langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class LangchainToolSet extends BaseComposioToolSet {
super({
apiKey: config.apiKey || null,
baseUrl: config.baseUrl || COMPOSIO_BASE_URL,
runtime: config?.runtime || null,
runtime: config?.runtime || LangchainToolSet.FRAMEWORK_NAME,
entityId: config.entityId || LangchainToolSet.DEFAULT_ENTITY_ID,
});
}
Expand Down
49 changes: 49 additions & 0 deletions js/src/frameworks/vercel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { beforeAll, describe, expect, it } from "@jest/globals";
import { z } from "zod";
import { getTestConfig } from "../../config/getTestConfig";
import { VercelAIToolSet } from "./vercel";

describe("Apps class tests", () => {
let vercelAIToolSet: VercelAIToolSet;

beforeAll(() => {
vercelAIToolSet = new VercelAIToolSet({
apiKey: getTestConfig().COMPOSIO_API_KEY,
Expand All @@ -25,4 +27,51 @@ describe("Apps class tests", () => {
});
expect(Object.keys(tools).length).toBe(1);
});

describe("custom actions", () => {
let customAction: Awaited<ReturnType<typeof vercelAIToolSet.createAction>>;
let tools: Awaited<ReturnType<typeof vercelAIToolSet.getTools>>;

beforeAll(async () => {
const params = z.object({
owner: z.string().describe("The owner of the repository"),
repo: z
.string()
.describe("The name of the repository without the `.git` extension."),
});

customAction = await vercelAIToolSet.createAction({
actionName: "starRepositoryCustomAction",
toolName: "github",
description: "Star A Github Repository For Given `Repo` And `Owner`",
inputParams: params,
callback: async (inputParams) => ({
successful: true,
data: inputParams,
}),
});

tools = await vercelAIToolSet.getTools({
actions: ["starRepositoryCustomAction"],
});
});

it("check if custom actions are coming", async () => {
expect(Object.keys(tools).length).toBe(1);
expect(tools).toHaveProperty(customAction.name, tools[customAction.name]);
});

it("check if custom actions are executing", async () => {
const res = await vercelAIToolSet.executeAction({
action: customAction.name,
params: {
owner: "composioHQ",
repo: "composio",
},
});
expect(res.successful).toBe(true);
expect(res.data).toHaveProperty("owner", "composioHQ");
expect(res.data).toHaveProperty("repo", "composio");
});
});
});
28 changes: 12 additions & 16 deletions js/src/frameworks/vercel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jsonSchema, tool } from "ai";
import { CoreTool, jsonSchema, tool } from "ai";
import { z } from "zod";
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
Expand Down Expand Up @@ -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,
Expand All @@ -78,22 +78,18 @@ 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
tools[actionSchema.name!] = this.generateVercelTool(
// @ts-ignore
actionSchema as ActionData
);
const tools: { [key: string]: CoreTool } = {};
actionsList.forEach((actionSchema) => {
tools[actionSchema.name!] = this.generateVercelTool(actionSchema);
});

return tools;
Expand Down
27 changes: 21 additions & 6 deletions js/src/sdk/actionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ 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
Expand All @@ -50,15 +60,20 @@ 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");
Expand All @@ -67,7 +82,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;
}
const params = options.inputParams;
const actionName = options.actionName || callback.name || "";
Expand Down
12 changes: 9 additions & 3 deletions js/src/sdk/base.toolset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} 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";
Expand Down Expand Up @@ -172,8 +176,10 @@ export class ComposioToolSet {
});
}

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) {
Expand Down

0 comments on commit 545f220

Please sign in to comment.