Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
Fix typings
haxzief15719753820
diff --git a/README.md b/README.mdindex 7d917018c..35eae696d 100644--- a/README.md+++ b/README.md@@ -29,6 +29,8 @@ const tool = await composio.getTool("HACKERNEWS_SEARCH_POSTS");console.log(tool);```+For more examples, please check the `/examples` directory.+## Creating a new toolsetTo create a new Toolset, you need to extend the `BaseComposioToolset<YourToolType>` and implement the `_wrapTool` method to return the tool type of your choice.diff --git a/packages/examples/sample/package.json b/examples/openai/package.jsonsimilarity index 79%rename from packages/examples/sample/package.jsonrename to examples/openai/package.jsonindex 5f21c0f40..381693e57 100644--- a/packages/examples/sample/package.json+++ b/examples/openai/package.json@@ -4,6 +4,7 @@"description": "","main": "index.js","scripts": {+ "start": "bun src/index.ts","test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],@@ -12,7 +13,8 @@"packageManager": "[email protected]","dependencies": {"@composio/core": "workspace:*",- "@composio/openai-toolset": "workspace:*"+ "@composio/openai-toolset": "workspace:*",+ "openai": "^4.94.0"},"devDependencies": {"@types/bun": "^1.2.9",diff --git a/examples/openai/src/index.ts b/examples/openai/src/index.tsnew file mode 100644index 000000000..0ac296ce3--- /dev/null+++ b/examples/openai/src/index.ts@@ -0,0 +1,38 @@+import { Composio } from "@composio/core";+import { OpenAIToolset } from "@composio/openai-toolset";+import { OpenAI } from "openai";++const openai = new OpenAI({+ apiKey: process.env.OPENAI_API_KEY,+});++// Initialize Composio with OpenAI Toolset+const composio = new Composio({+ apiKey: process.env.COMPOSIO_API_KEY,+ toolset: new OpenAIToolset(),+});++// Fetch all the tools from the Composio API+// these tools are automatically typed and wrapped with the OpenAI Toolset+const tool = await composio.getTool("HACKERNEWS_GET_USER");+++const task = "Fetch the details of the user 'haxzie'";++const messages: OpenAI.ChatCompletionMessageParam[] = [+ { role: "system", content: "You are a helpful assistant that can help with tasks." },+ { role: "user", content: task },+]++const response = await openai.chat.completions.create({+ model: "gpt-4o-mini",+ messages,+ tools: [tool],+ tool_choice: "auto",+});+++if (response.choices[0].message.tool_calls) {+ const result = await composio.toolset.executeToolCall(response.choices[0].message.tool_calls[0]);+ console.log(JSON.stringify(result, null, 2));+}\ No newline at end of filediff --git a/packages/examples/sample/tsconfig.json b/examples/openai/tsconfig.jsonsimilarity index 100%rename from packages/examples/sample/tsconfig.jsonrename to examples/openai/tsconfig.jsondiff --git a/packages/core/src/composio.ts b/packages/core/src/composio.tsindex 1b97b7076..b08b99bd3 100644--- a/packages/core/src/composio.ts+++ b/packages/core/src/composio.ts@@ -11,12 +11,12 @@ import { ToolListParams } from "@composio/client/resources/tools";import { BaseComposioToolset } from "./toolset/BaseToolset";-export type ComposioConfig<TTool, T extends BaseComposioToolset<TTool>> = {+export type ComposioConfig<TToolCollection, TTool, TToolset extends BaseComposioToolset<TToolCollection, TTool>> = {apiKey?: string;baseURL?: string;allowTracking?: boolean;allowTracing?: boolean;- toolset?: T;+ toolset?: TToolset;userId?: string;connectedAccountIds?: Record<string, string>;};@@ -25,7 +25,7 @@ export type ComposioConfig<TTool, T extends BaseComposioToolset<TTool>> = {* This is the core class for Composio.* It is used to initialize the Composio SDK and provide a global configuration.*/-export class Composio<TTool, T extends BaseComposioToolset<TTool>> {+export class Composio<TToolCollection, TTool, TToolset extends BaseComposioToolset<TToolCollection, TTool>> {private readonly DEFAULT_USER_ID = "default";private static readonly FILE_NAME = "core/composio.ts";@@ -38,9 +38,9 @@ export class Composio<TTool, T extends BaseComposioToolset<TTool>> {/*** The configuration for the Composio SDK.- * @type {ComposioConfig<TTool, T>}+ * @type {ComposioConfig<TTool, TToolset>}*/- private config: ComposioConfig<TTool, T>;+ private config: ComposioConfig<TToolCollection, TTool, TToolset>;/*** Context variables for the Composio SDK.@@ -54,7 +54,7 @@ export class Composio<TTool, T extends BaseComposioToolset<TTool>> {tools: Tools;toolkits: Toolkits;triggers: Triggers;- toolset: T;+ toolset: TToolset;authConfigs: AuthConfigs;connectedAccounts: ConnectedAccounts;@@ -67,7 +67,7 @@ export class Composio<TTool, T extends BaseComposioToolset<TTool>> {* @param {boolean} config.allowTracing Whether to allow tracing. Defaults to true.* @param {TS} config.toolset The toolset to use for this Composio instance.*/- constructor(config: ComposioConfig<TTool,T>) {+ constructor(config: ComposioConfig<TToolCollection,TTool,TToolset>) {/*** Initialize the Composio SDK client.* The client is used to make API calls to the Composio API.@@ -92,7 +92,7 @@ export class Composio<TTool, T extends BaseComposioToolset<TTool>> {/*** Set the default toolset, if not provided by the user.*/- this.toolset = config.toolset ?? new ComposioToolset() as unknown as T;+ this.toolset = config.toolset ?? new ComposioToolset() as unknown as TToolset;this.toolset.setClient(this);/**@@ -129,18 +129,19 @@ export class Composio<TTool, T extends BaseComposioToolset<TTool>> {/*** Generic function to get all the tools- * @returns {Promise<T[]>} Promise with list of tools+ * @returns {Promise<ReturnType<TToolset["getTool"]>>} Promise with list of tools*/- async getTool(slug: string): Promise<WrappedTool<T>> {- return this.toolset.getTool(slug) as Promise<WrappedTool<T>>;+ async getTool(slug: string): Promise<ReturnType<TToolset["getTool"]>> {+ return this.toolset.getTool(slug) as Promise<ReturnType<TToolset["getTool"]>>;}/*** Generic function to get all the tools* @param query - Query parameters for the tools- * @returns {Promise<T[]>} Promise with list of tools+ * @returns {Promise<ReturnType<TToolset["getTools">} Promise with list/records of tools*/- async getTools(query?: ToolListParams): Promise<WrappedTool<T>[] | Record<string, WrappedTool<T>>> {- return this.toolset.getTools(query) as Promise<WrappedTool<T>[]>;+ async getTools(query?: ToolListParams): Promise<ReturnType<TToolset["getTools"]>> {+ return this.toolset.getTools(query) as Promise<ReturnType<TToolset["getTools"]>>;}+}diff --git a/packages/core/src/toolset/BaseToolset.ts b/packages/core/src/toolset/BaseToolset.tsindex 9d1c04786..60dd3b123 100644--- a/packages/core/src/toolset/BaseToolset.ts+++ b/packages/core/src/toolset/BaseToolset.ts@@ -11,25 +11,16 @@ import { Composio } from "../composio";* these extended toolsets can add their own functionality/methods to the toolset.** eg:- * ```ts- * // Using default BaseTool type- * class DefaultToolset extends BaseComposioToolset {}- *- * // Using a custom tool type- * class MyToolset extends BaseComposioToolset<MyCustomTool> {}- *- * // Using a third-party tool type- * class ThirdPartyToolset extends BaseComposioToolset<ThirdPartyTool> {}- * ```+ * class YourToolSet extends BaseComposioToolset<CustomToolCollection, CustomTool> {}*/-export abstract class BaseComposioToolset<TTool = BaseTool> implements Toolset<TTool> {- protected client: Composio<TTool, this> | undefined;+export abstract class BaseComposioToolset<TToolCollection, TTool = BaseTool> implements Toolset<TTool, TToolCollection> {+ protected client: Composio<TToolCollection, TTool, this> | undefined;/*** Set the client for the toolset. This is automatically done by the Composio class.* @param client - The Composio client.*/- setClient(client: Composio<TTool, this>): void {+ setClient(client: Composio<TToolCollection, TTool, this>): void {this.client = client;}@@ -39,10 +30,7 @@ export abstract class BaseComposioToolset<TTool = BaseTool> implements Toolset<T* @param params - The parameters for the tool list.* @returns The tools.*/- async getTools(params?: ToolListParams): Promise<TTool[] | Record<string, TTool>> {- const tools = await this.client?.tools.list(params);- return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];- }+ abstract getTools(params?: ToolListParams): Promise<TToolCollection>;/*** Get a tool from the client.diff --git a/packages/core/src/toolset/ComposioToolset.ts b/packages/core/src/toolset/ComposioToolset.tsindex ddc8f787a..77175b592 100644--- a/packages/core/src/toolset/ComposioToolset.ts+++ b/packages/core/src/toolset/ComposioToolset.ts@@ -1,5 +1,4 @@-import { Composio } from "../composio";-import { Tool } from "../types/tool.types";+import { Tool, ToolListParams } from "../types/tool.types";import { BaseComposioToolset } from "./BaseToolset";/**@@ -12,11 +11,16 @@ import { BaseComposioToolset } from "./BaseToolset";interface CustomTool {name: string;}-export class ComposioToolset extends BaseComposioToolset<CustomTool> {+export class ComposioToolset extends BaseComposioToolset<Array<CustomTool>, CustomTool> {_wrapTool = (tool: Tool): CustomTool => {return tool as CustomTool;};+ async getTools(params?: ToolListParams): Promise<Array<CustomTool>> {+ const tools = await this.client?.tools.list(params);+ return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];+ }+async test() {}}\ No newline at end of filediff --git a/packages/core/src/types/Toolset.types..ts b/packages/core/src/types/Toolset.types..tsindex 7a202af27..4c0277174 100644--- a/packages/core/src/types/Toolset.types..ts+++ b/packages/core/src/types/Toolset.types..ts@@ -1,12 +1,13 @@import { Composio } from "../composio";-import { Tool, BaseTool } from "./tool.types";+import { Tool, BaseTool, ToolListParams } from "./tool.types";/*** Base toolset implementation, which needs to be implemented by the extended class.* This class is used to create a base toolset by implementing this class.*/-export interface Toolset<TTool = BaseTool> {+export interface Toolset<TTool = BaseTool, TToolCollection = TTool[]> {_wrapTool: (tool: Tool) => TTool;+ getTools: (params?: ToolListParams) => Promise<TToolCollection>;}@@ -16,4 +17,5 @@ export interface Toolset<TTool = BaseTool> {*/export type WrappedTool<TToolset> = TToolset extends {_wrapTool: (tool: Tool) => infer TTool;-} ? TTool : never;\ No newline at end of file+} ? TTool : never;+diff --git a/packages/examples/sample/src/index.ts b/packages/examples/sample/src/index.tsdeleted file mode 100644index 7926ce123..000000000--- a/packages/examples/sample/src/index.ts+++ /dev/null@@ -1,11 +0,0 @@-import { Composio } from "@composio/core";-import { OpenAIToolset } from "@composio/openai-toolset";---const composio = new Composio({- apiKey: process.env.COMPOSIO_API_KEY,- toolset: new OpenAIToolset(),-});--const tool = await composio.getTool("HACKERNEWS_SEARCH_POSTS");-console.log(tool);diff --git a/packages/toolsets/cloudflare/src/index.ts b/packages/toolsets/cloudflare/src/index.tsindex bd572e6e6..a4ea02884 100644--- a/packages/toolsets/cloudflare/src/index.ts+++ b/packages/toolsets/cloudflare/src/index.ts@@ -1,7 +1,9 @@import { AiTextGenerationToolInput } from "@cloudflare/workers-types";import { BaseComposioToolset, Tool, ToolListParams } from "@composio/core";-export class CloudflareToolset extends BaseComposioToolset<AiTextGenerationToolInput> {+type AiToolCollection = Record<string, AiTextGenerationToolInput>;++export class CloudflareToolset extends BaseComposioToolset<AiToolCollection, AiTextGenerationToolInput> {static FRAMEWORK_NAME = "cloudflare";private DEFAULT_ENTITY_ID = "default";static fileName: string = "toolsets/cloudflare/src/index.ts";@@ -14,7 +16,7 @@ export class CloudflareToolset extends BaseComposioToolset<AiTextGenerationToolI*/_wrapTool(tool: Tool): AiTextGenerationToolInput {const formattedSchema: AiTextGenerationToolInput["function"] = {- name: tool.name!,+ name: tool.slug!,description: tool.description!,parameters: tool.input_parameters as unknown as {type: "object";@@ -39,7 +41,7 @@ export class CloudflareToolset extends BaseComposioToolset<AiTextGenerationToolI* @param {ToolListParams} query - The query parameters for the tools.* @returns {Promise<Record<string, AiTextGenerationToolInput>>} The tools from the Cloudflare API.*/- override async getTools(query?: ToolListParams): Promise<Record<string, AiTextGenerationToolInput>> {+ override async getTools(query?: ToolListParams): Promise<AiToolCollection> {if (!this.client) {throw new Error("Client not set");}@@ -47,7 +49,7 @@ export class CloudflareToolset extends BaseComposioToolset<AiTextGenerationToolIconst tools = await this.client.tools.list(query);return tools.items.reduce((tools, tool) => ({...tools,- [tool.name]: this._wrapTool(tool as Tool),+ [tool.slug]: this._wrapTool(tool as Tool),}), {});}@@ -64,13 +66,14 @@ export class CloudflareToolset extends BaseComposioToolset<AiTextGenerationToolI}const toolSchema = await this.client.tools.get(tool.name);+ const appName = toolSchema?.toolkit?.name.toLowerCase();const args = typeof tool.arguments === "string"? JSON.parse(tool.arguments): tool.arguments;- const results = await this.client?.tools.execute(toolSchema.name, {+ const results = await this.client?.tools.execute(toolSchema.slug, {arguments: args,entity_id: this.DEFAULT_ENTITY_ID,- connected_account_id: this.client?.getConnectedAccountId(toolSchema.name),+ connected_account_id: this.client?.getConnectedAccountId(appName),});return JSON.stringify(results);diff --git a/packages/toolsets/langchain/src/index.ts b/packages/toolsets/langchain/src/index.tsindex 88895b2f3..576f0a7b6 100644--- a/packages/toolsets/langchain/src/index.ts+++ b/packages/toolsets/langchain/src/index.ts@@ -1,9 +1,9 @@-import { BaseComposioToolset, jsonSchemaToModel, Tool } from "@composio/core";+import { BaseComposioToolset, jsonSchemaToModel, Tool, ToolListParams } from "@composio/core";import { DynamicStructuredTool } from "@langchain/core/tools";-export class LangchainToolset extends BaseComposioToolset<DynamicStructuredTool> {+export class LangchainToolset extends BaseComposioToolset<Array<DynamicStructuredTool>, DynamicStructuredTool> {static FRAMEWORK_NAME = "langchain";private DEFAULT_ENTITY_ID = "default";static fileName: string = "toolsets/langchain/src/index.ts";@@ -15,14 +15,14 @@ export class LangchainToolset extends BaseComposioToolset<DynamicStructuredTool>* @returns The wrapped tool.*/_wrapTool = (tool: Tool): DynamicStructuredTool => {- const action = tool.name+ const toolName = tool.slugconst description = tool.description;const appName = tool.toolkit.name?.toLowerCase();const func = async (...kwargs: unknown[]): Promise<unknown> => {const connectedAccountId = this.client?.getConnectedAccountId(appName)return JSON.stringify(- await this.client?.tools.execute(tool.name, {+ await this.client?.tools.execute(toolName, {arguments: kwargs[0] as Record<string, unknown>,entity_id: this.DEFAULT_ENTITY_ID,connected_account_id: connectedAccountId,@@ -35,10 +35,20 @@ export class LangchainToolset extends BaseComposioToolset<DynamicStructuredTool>// @TODO: Add escriiption an other stuff herereturn new DynamicStructuredTool({- name: action,+ name: toolName,description,schema: parameters,func: func,});}++ /**+ * Get all the tools from the Composio in Langchain format.+ * @param params - The parameters for the tool list.+ * @returns The tools.+ */+ async getTools(params?: ToolListParams): Promise<Array<DynamicStructuredTool>> {+ const tools = await this.client?.tools.list(params);+ return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];+ }}diff --git a/packages/toolsets/openai/src/index.ts b/packages/toolsets/openai/src/index.tsindex 92799ed2b..555cde93c 100644--- a/packages/toolsets/openai/src/index.ts+++ b/packages/toolsets/openai/src/index.ts@@ -1,9 +1,10 @@-import { BaseComposioToolset } from "@composio/core";+import { BaseComposioToolset, ToolListParams } from "@composio/core";import { Tool } from "@composio/core";import { OpenAI } from "openai";import { Stream } from "openai/streaming";-export class OpenAIToolset extends BaseComposioToolset<OpenAI.ChatCompletionTool> {+type OpenAiToolCollection = Array<OpenAI.ChatCompletionTool>;+export class OpenAIToolset extends BaseComposioToolset<OpenAiToolCollection, OpenAI.ChatCompletionTool> {static readonly FRAMEWORK_NAME = "openai";private static readonly FILE_NAME = "toolsets/openai/src/index.ts";@@ -19,7 +20,7 @@ export class OpenAIToolset extends BaseComposioToolset<OpenAI.ChatCompletionTool*/_wrapTool = (tool: Tool): OpenAI.ChatCompletionTool => {const formattedSchema: OpenAI.FunctionDefinition = {- name: tool.name!,+ name: tool.slug!,description: tool.description!,parameters: tool.input_parameters!,};@@ -29,6 +30,16 @@ export class OpenAIToolset extends BaseComposioToolset<OpenAI.ChatCompletionTool};};+ /**+ * Get all the tools from the Composio in OpenAI format.+ * @param params - The parameters for the tool list.+ * @returns The tools.+ */+ async getTools(params?: ToolListParams): Promise<Array<OpenAI.ChatCompletionTool>> {+ const tools = await this.client?.tools.list(params);+ return tools?.items.map((tool) => this._wrapTool(tool as Tool)) ?? [];+ }+/*** @TODO include the connectedAccountId / app name in the tool call* @param tool@@ -44,12 +55,18 @@ export class OpenAIToolset extends BaseComposioToolset<OpenAI.ChatCompletionToolthrow new Error("Client not set");}+ /**+ * @TODO: This is redundant, we should be able to get the tool name from the tool call+ */const toolSchema = await this.client.tools.get(tool.function.name);- const appName = toolSchema?.name.toLowerCase();- const results = await this.client.tools.execute(toolSchema.name, {+ const appName = toolSchema?.toolkit?.name.toLowerCase();+ const connectedAccountId = this.client.getConnectedAccountId(appName);+++ const results = await this.client.tools.execute(toolSchema.slug, {arguments: JSON.parse(tool.function.arguments),entity_id: userId ?? this.client.userId,- connected_account_id: this.client.getConnectedAccountId(appName)+ // connected_account_id: connectedAccountId,});return JSON.stringify(results);diff --git a/packages/toolsets/vercel/src/index.ts b/packages/toolsets/vercel/src/index.tsindex d79381a10..47807d6e6 100644--- a/packages/toolsets/vercel/src/index.ts+++ b/packages/toolsets/vercel/src/index.ts@@ -2,7 +2,8 @@ import { BaseComposioToolset, Tool as ComposioTool, ToolListParams } from "@compimport type { Tool as VercelTool } from "ai";import { jsonSchema, tool } from "ai";-export class VercelToolset extends BaseComposioToolset<VercelTool> {+type VercelToolCollection = Record<string, VercelTool>;+export class VercelToolset extends BaseComposioToolset<VercelToolCollection, VercelTool> {private static readonly FILE_NAME = "toolsets/vercel/src/index.ts";static readonly FRAMEWORK_NAME = "vercel";@@ -14,12 +15,15 @@ export class VercelToolset extends BaseComposioToolset<VercelTool> {* @param params - The parameters for the tool list.* @returns The tools.*/- override async getTools(params?: ToolListParams): Promise<Record<string, VercelTool>> {+ override async getTools(params?: ToolListParams): Promise<VercelToolCollection> {if (!this.client) {throw new Error("Client not initialized");}const tools = await this.client.tools.list(params);- return tools.items.reduce((tools, tool) => ({ ...tools, [tool.name]: this._wrapTool(tool as ComposioTool) }), {});+ return tools.items.reduce((tools, tool) => ({+ ...tools,+ [tool.slug]: this._wrapTool(tool as ComposioTool)+ }), {});}/**@@ -40,7 +44,7 @@ export class VercelToolset extends BaseComposioToolset<VercelTool> {const appName = toolSchema?.name.toLowerCase();const args = typeof tool.arguments === "string" ? JSON.parse(tool.arguments) : tool.arguments;- const results = await this.client.tools.execute(toolSchema.name, {+ const results = await this.client.tools.execute(toolSchema.slug, {arguments: args,entity_id: userId ?? this.client.userId,connected_account_id: this.client.getConnectedAccountId(appName)@@ -57,7 +61,7 @@ export class VercelToolset extends BaseComposioToolset<VercelTool> {execute: async (params) => {return await this.executeToolCall({- name: composioTool.name,+ name: composioTool.slug,arguments: JSON.stringify(params),},this.client?.userIddiff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f2649034a..fd2123114 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -8,6 +8,25 @@ importers:.: {}+ examples/openai:+ dependencies:+ '@composio/core':+ specifier: workspace:*+ version: link:../../packages/core+ '@composio/openai-toolset':+ specifier: workspace:*+ version: link:../../packages/toolsets/openai+ openai:+ specifier: ^4.94.0+ version: 4.94.0([email protected])+ devDependencies:+ '@types/bun':+ specifier: ^1.2.9+ version: 1.2.9+ typescript:+ specifier: ^5.8.3+ version: 5.8.3+packages/core:dependencies:'@composio/client':@@ -33,22 +52,6 @@ importers:specifier: ^5.8.3version: 5.8.3- packages/examples/sample:- dependencies:- '@composio/core':- specifier: workspace:*- version: link:../../core- '@composio/openai-toolset':- specifier: workspace:*- version: link:../../toolsets/openai- devDependencies:- '@types/bun':- specifier: ^1.2.9- version: 1.2.9- typescript:- specifier: ^5.8.3- version: 5.8.3-packages/toolsets/cloudflare:dependencies:'@cloudflare/workers-types':diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex f40f59094..5c64f4ba2 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -1,4 +1,4 @@packages:- 'packages/core'- 'packages/toolsets/*'- - 'packages/examples/*'\ No newline at end of file+ - 'examples/*'\ No newline at end of file