Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

First stable version of adapter implementation

haxzieac27d1239475
diff --git a/packages/core/src/composio.ts b/packages/core/src/composio.ts
index ba157aa45..b50ec4469 100644
--- a/packages/core/src/composio.ts
+++ b/packages/core/src/composio.ts
@@ -1,53 +1,63 @@
import ComposioClient from "@composio/client";
-import { Tool, Tools } from "./models/Tools";
+import { Tools } from "./models/Tools";
import { Toolkits } from "./models/Toolkits";
import { Triggers } from "./models/Triggers";
import { Org } from "./models/Org";
-import { ComposioToolset } from "./toolset/Toolset";
+import { ComposioToolset } from "./toolset/ComposioToolset";
+import { Toolset } from "./types/Toolset.types.";
+import { Tool } from "./types/Tool.types";
-// helper type to extract the tool type from a toolset
-export type ExtractToolType<TS> = TS extends ComposioToolset<infer T> ? T : Tool;
-
-export type ComposioConfig<TS extends ComposioToolset<Tool>> = {
+export type ComposioConfig<
+ TTool extends Tool,
+ TToolset extends Toolset<TTool>
+> = {
apiKey?: string;
baseURL?: string;
runtime?: string;
allowTracking?: boolean;
allowTracing?: boolean;
- toolset?: TS;
+ toolset?: TToolset;
};
+
/**
* This is the core class for Composio.
* It is used to initialize the Composio SDK and provide a global configuration.
*/
-export class Composio<TS extends ComposioToolset<any>> {
+export class Composio<TTool extends Tool, TToolset extends Toolset<TTool>> {
/**
* The Composio API client.
* @type {ComposioClient}
*/
private client: ComposioClient;
- private config: ComposioConfig<TS>
+ private config: ComposioConfig<TTool, TToolset>;
/**
* Core models for Composio.
*/
- tools: Tools<ExtractToolType<TS>, TS>;
+ tools: Tools<TTool, TToolset>;
toolkits: Toolkits;
triggers: Triggers;
org: Org;
- toolset: TS;
+ toolset: TToolset;
/**
- *
* @param {Object} config Configuration for the Composio SDK.
* @param {string} config.apiKey The API key for the Composio SDK.
* @param {string} config.baseURL The base URL for the Composio SDK.
* @param {string} config.runtime The runtime for the Composio SDK.
* @param {boolean} config.allowTracking Whether to allow analytics / tracking. Defaults to true.
* @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<TS>) {
+ constructor(config: {
+ apiKey?: string;
+ baseURL?: string;
+ runtime?: string;
+ allowTracking?: boolean;
+ allowTracing?: boolean;
+ toolset?: TToolset;
+ }) {
this.client = new ComposioClient({
apiKey: config.apiKey,
baseURL: config.baseURL,
@@ -55,11 +65,11 @@ export class Composio<TS extends ComposioToolset<any>> {
this.config = config;
- const defaultToolset = new ComposioToolset() as TS;
- this.toolset = config.toolset ?? defaultToolset;
+ const defaultToolset = new ComposioToolset();
+ this.toolset = config.toolset ?? (defaultToolset as unknown as TToolset);
- // initialize models
- this.tools = new Tools<ExtractToolType<TS>, TS>(this.client, this.toolset);
+ // Initialize models
+ this.tools = new Tools(this.client, this.toolset);
this.toolkits = new Toolkits(this.client);
this.triggers = new Triggers(this.client);
this.org = new Org(this.client);
@@ -77,4 +87,19 @@ export class Composio<TS extends ComposioToolset<any>> {
}
return this.client;
}
+
+ /**
+ * Generic function to get all the tools
+ * @returns {Promise<T[]>} Promise with list of tools
+ */
+ getTools(): Promise<TTool[]> {
+ console.log("Tools");
+ return Promise.resolve([]);
+ // return this.tools.get();
+ }
+
+ getEntity(): Promise<void> {
+ console.log("Entity");
+ return Promise.resolve();
+ }
}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index ee67b81d3..62b6cdfd0 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,6 +1,7 @@
import { Composio } from "./composio";
-import type { Tool } from "./models/Tools";
-import { ComposioToolset } from "./toolset/Toolset";
+import type { ComposioToolset } from "./toolset/ComposioToolset";
+import type { BaseComposioToolset } from "./toolset/BaseToolset";
+import type { Tool } from "./types/Tool.types";
+import type { Toolset } from "./types/Toolset.types.";
-export { Composio, Tool };
-export { ComposioToolset };
+export { Composio, ComposioToolset, BaseComposioToolset, Tool, Toolset };
diff --git a/packages/core/src/models/Tools.ts b/packages/core/src/models/Tools.ts
index 7d6fcb61a..a9017efc7 100644
--- a/packages/core/src/models/Tools.ts
+++ b/packages/core/src/models/Tools.ts
@@ -1,36 +1,37 @@
import ComposioSDK from "@composio/client";
-import { ComposioToolset } from "../toolset/Toolset";
-import { ToolRetrieveResponse } from "@composio/client/resources/tools";
+import { Tool } from "../types/Tool.types";
+import { Toolset } from "../types/Toolset.types.";
-export type Tool = ToolRetrieveResponse;
+export class Tools<
+ TTool extends Tool,
+ TToolset extends Toolset<TTool>
+> {
+ private client: ComposioSDK;
+ private toolset: TToolset;
-export class Tools<T extends Tool, TS extends ComposioToolset<T>> {
- private client: ComposioSDK;
- private toolset: TS;
+ constructor(client: ComposioSDK, toolset: TToolset) {
+ this.client = client;
+ this.toolset = toolset;
+ }
- constructor(client: ComposioSDK, toolset: TS) {
- this.client = client;
- this.toolset = toolset;
- }
+ async list() {
+ return this.client.tools.list();
+ }
- async list() {
- return this.client.tools.list();
- }
+ async get(toolId: string): Promise<ReturnType<TToolset["_wrapTool"]>> {
+ const tool: Tool = await this.client.tools.retrieve(toolId);
+ return this.toolset._wrapTool(tool) as ReturnType<TToolset["_wrapTool"]>;
+ }
- async get(toolId: string): Promise<T> {
- const tool = await this.client.tools.retrieve(toolId);
- return this.toolset.wrap(tool);
- }
+ async execute(tool: string, body: { [key: string]: any }) {
+ return this.client.tools.execute(tool, body);
+ }
- async execute(tool: string, body: { [key: string]: any }) {
- return this.client.tools.execute(tool, body);
- }
-
- async getToolsByEnum(toolEnum: string) {
- return this.client.tools.retrieveEnum({
- body: {
- toolEnum
- }
- });
- }
-}
\ No newline at end of file
+ async getToolsByEnum(toolEnum: string) {
+ return this.client.tools.retrieveEnum({
+ body: {
+ toolEnum,
+ },
+ });
+ }
+}
diff --git a/packages/core/src/toolset/BaseToolset.ts b/packages/core/src/toolset/BaseToolset.ts
new file mode 100644
index 000000000..d63cdd294
--- /dev/null
+++ b/packages/core/src/toolset/BaseToolset.ts
@@ -0,0 +1,42 @@
+import { Toolset } from "../types/Toolset.types.";
+import { Tool } from "../types/Tool.types";
+
+/**
+ * Base toolset implementation with proper generic defaults
+ * This class is used to create a different toolsets by extending this class.
+ *
+ * This class provides basic functionality to get tools, pre-process and post-process tools.
+ * Every toolset should extend this class and implement the `_wrapTool` method,
+ * these extended toolsets can add their own functionality/methods to the toolset.
+ *
+ * eg:
+ * ```ts
+ * class MyToolset extends BaseComposioToolset<MyTool> {
+ * _wrapTool(tool: BaseTool): MyTool {}
+ * }
+ * ```
+ */
+export abstract class BaseComposioToolset<TTool extends Tool> implements Toolset<TTool> {
+
+ /**
+ * Wraps the tool to a specific type
+ * This method should be implemented by the extended class to wrap the tool to a specific type.
+ * @param tool The tool to wrap (This will be the tool retrieved from the API)
+ * @returns The wrapped tool
+ */
+ abstract _wrapTool: (tool: Tool) => TTool;
+
+ getTools(): TTool[] {
+ return [];
+ }
+
+ // pre process the tool before executing it
+ preProcessTool(tool: TTool): TTool {
+ return tool;
+ }
+
+ // post process the tool after executing it
+ postProcessTool(tool: TTool): TTool {
+ return tool;
+ }
+}
\ No newline at end of file
diff --git a/packages/core/src/toolset/ComposioToolset.ts b/packages/core/src/toolset/ComposioToolset.ts
new file mode 100644
index 000000000..530f95cf2
--- /dev/null
+++ b/packages/core/src/toolset/ComposioToolset.ts
@@ -0,0 +1,14 @@
+import { Tool } from "../types/Tool.types";
+import { BaseComposioToolset } from "./BaseToolset";
+
+/**
+ * This is a default toolset implementation for Composio.
+ * This class is used as a default toolset for Composio, if the user does not provide a toolset.
+ *
+ * This class shouldn't be used directly or to be extended.
+ */
+export class ComposioToolset extends BaseComposioToolset<Tool> {
+ _wrapTool = (tool: Tool): Tool => {
+ return tool;
+ };
+}
diff --git a/packages/core/src/toolset/Toolset.ts b/packages/core/src/toolset/Toolset.ts
deleted file mode 100644
index 44a0807bb..000000000
--- a/packages/core/src/toolset/Toolset.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { Tool } from "../models/Tools";
-
-/**
- * Base toolset implementation with proper generic defaults
- */
-export class ComposioToolset<T extends Tool = Tool> {
- constructor() {
- // Constructor implementation
- }
-
- async processResponse() {}
-
- async getToolsSchema() {}
-
- async addSchemaProcessor() {}
-
- async addPreProcessor() {}
-
- async addPostProcessor() {}
-
- /**
- * Wraps a standard Tool into the specific tool type T
- * Default implementation just casts the tool
- */
- wrap(tool: Tool): T {
- console.log("Wrapping tool in ComposioToolset...");
- return tool as T;
- }
-}
\ No newline at end of file
diff --git a/packages/core/src/types/Tool.types.ts b/packages/core/src/types/Tool.types.ts
new file mode 100644
index 000000000..5c6844221
--- /dev/null
+++ b/packages/core/src/types/Tool.types.ts
@@ -0,0 +1,3 @@
+import { ToolRetrieveResponse } from "@composio/client/resources/tools";
+
+export type Tool = ToolRetrieveResponse;
diff --git a/packages/core/src/types/Toolset.types..ts b/packages/core/src/types/Toolset.types..ts
new file mode 100644
index 000000000..1177af786
--- /dev/null
+++ b/packages/core/src/types/Toolset.types..ts
@@ -0,0 +1,9 @@
+import { Tool } 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 extends Tool> {
+ _wrapTool: (tool: Tool) => TTool;
+}
diff --git a/packages/examples/sample/src/index.ts b/packages/examples/sample/src/index.ts
index 2609d09e0..dce81cfe0 100644
--- a/packages/examples/sample/src/index.ts
+++ b/packages/examples/sample/src/index.ts
@@ -1,34 +1,20 @@
import { Composio } from "@composio/core";
-import { OpenAIToolSet } from "@composio/openai-toolset";
+import { OpenAIToolset } from "@composio/openai-toolset";
/**
* General example
*/
async function constructorExample() {
- const composio = new Composio({
- apiKey: process.env.COMPOSIO_API_KEY,
- toolset: new OpenAIToolSet(),
- });
+ const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+ toolset: new OpenAIToolset(),
+ });
- const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
-
- composio.toolset.handleToolCall([tool]);
-}
+ const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
-constructorExample();
-/**
- * Builder example
- */
-// async function builderExample() {
-// const composio = Composio.init()
-// .withToolset(new OpenAIToolSet())
-// .withApiKey(process.env.COMPOSIO_API_KEY as string)
-// .build();
+ composio.toolset.handleToolCall(tool);
-// const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
-
-// tool.execute();
-// }
+ tool.execute({});
+}
-// constructorExample();
-// builderExample();
\ No newline at end of file
+constructorExample();
diff --git a/packages/toolsets/openai/src/index.ts b/packages/toolsets/openai/src/index.ts
index 0d43179ec..b1f519c2c 100644
--- a/packages/toolsets/openai/src/index.ts
+++ b/packages/toolsets/openai/src/index.ts
@@ -1,40 +1,26 @@
+import { BaseComposioToolset } from "@composio/core";
import { Tool } from "@composio/core";
-import { ComposioToolset } from "@composio/core";
-export interface OpenAIToolActions extends Tool {
- execute: () => Promise<any>;
+export interface OpenAITool extends Tool {
+ execute: (args: any) => Promise<any>;
}
+export class OpenAIToolset extends BaseComposioToolset<OpenAITool> {
+ handleToolCall = async (tool: OpenAITool) => {
+ console.log(`Handling OpenAI tool call: ${tool.name}`);
+ // Here you would implement the logic to handle the tool call
+ // For example, calling an API or performing some computation
+ return tool.execute({});
+ };
-export class OpenAIToolSet extends ComposioToolset<OpenAIToolActions> {
- constructor() {
- super();
- }
-
- /**
- * Handles execution of multiple tools in sequence
- * @param tools Array of tools to execute
- * @returns Promise that resolves when all tools have executed
- */
- async handleToolCall(tools: OpenAIToolActions[]): Promise<void> {
- // Using Promise.all to properly handle all async operations
- await Promise.all(
- tools.map(tool => tool.execute())
- );
- }
-
- /**
- * Wraps a standard Tool into an OpenAIToolActions
- * @param tool The base tool to wrap
- * @returns A tool with OpenAI-specific functionality
- */
- override wrap(tool: Tool): OpenAIToolActions {
- const openAITool: OpenAIToolActions = {
+ _wrapTool = (tool: Tool): OpenAITool => {
+ return {
...tool,
- execute: async () => {
- console.log(`Executing tool: ${tool.name}`);
+ execute: async (args: any) => {
+ console.log(`Executing OpenAI tool: ${tool.name}`);
+ // Here you would implement the actual execution logic
+ // For example, calling an API or performing some computation
+ return Promise.resolve(args);
},
};
-
- return openAITool;
- }
-}
\ No newline at end of file
+ };
+}