@devries-cameron20 / composio Mirror · read-only / commits / ac27d1239
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 23, 04:15 UTC

First stable version of adapter implementation

haxzie committed Apr 15, 2025 at 16:48 UTC ac27d123947595966ba4ffc03e37a1b60e001fec
10 files changed +174 -136
packages/core/src/composio.ts
+42 -17
@@ -1,53 +1,63 @@
1 import ComposioClient from "@composio/client";
2 -import { Tool, Tools } from "./models/Tools";
2 +import { Tools } from "./models/Tools";
3 import { Toolkits } from "./models/Toolkits";
4 import { Triggers } from "./models/Triggers";
5 import { Org } from "./models/Org";
6 -import { ComposioToolset } from "./toolset/Toolset";
6 +import { ComposioToolset } from "./toolset/ComposioToolset";
7 +import { Toolset } from "./types/Toolset.types.";
8 +import { Tool } from "./types/Tool.types";
9
8 -// helper type to extract the tool type from a toolset
9 -export type ExtractToolType<TS> = TS extends ComposioToolset<infer T> ? T : Tool;
10 -
11 -export type ComposioConfig<TS extends ComposioToolset<Tool>> = {
10 +export type ComposioConfig<
11 + TTool extends Tool,
12 + TToolset extends Toolset<TTool>
13 +> = {
14 apiKey?: string;
15 baseURL?: string;
16 runtime?: string;
17 allowTracking?: boolean;
18 allowTracing?: boolean;
17 - toolset?: TS;
19 + toolset?: TToolset;
20 };
21 +
22 /**
23 * This is the core class for Composio.
24 * It is used to initialize the Composio SDK and provide a global configuration.
25 */
23 -export class Composio<TS extends ComposioToolset<any>> {
26 +export class Composio<TTool extends Tool, TToolset extends Toolset<TTool>> {
27 /**
28 * The Composio API client.
29 * @type {ComposioClient}
30 */
31 private client: ComposioClient;
32
30 - private config: ComposioConfig<TS>
33 + private config: ComposioConfig<TTool, TToolset>;
34
35 /**
36 * Core models for Composio.
37 */
35 - tools: Tools<ExtractToolType<TS>, TS>;
38 + tools: Tools<TTool, TToolset>;
39 toolkits: Toolkits;
40 triggers: Triggers;
41 org: Org;
39 - toolset: TS;
42 + toolset: TToolset;
43
44 /**
42 - *
45 * @param {Object} config Configuration for the Composio SDK.
46 * @param {string} config.apiKey The API key for the Composio SDK.
47 * @param {string} config.baseURL The base URL for the Composio SDK.
48 * @param {string} config.runtime The runtime for the Composio SDK.
49 * @param {boolean} config.allowTracking Whether to allow analytics / tracking. Defaults to true.
50 * @param {boolean} config.allowTracing Whether to allow tracing. Defaults to true.
51 + * @param {TS} config.toolset The toolset to use for this Composio instance.
52 */
50 - constructor(config: ComposioConfig<TS>) {
53 + constructor(config: {
54 + apiKey?: string;
55 + baseURL?: string;
56 + runtime?: string;
57 + allowTracking?: boolean;
58 + allowTracing?: boolean;
59 + toolset?: TToolset;
60 + }) {
61 this.client = new ComposioClient({
62 apiKey: config.apiKey,
63 baseURL: config.baseURL,
@@ -55,11 +65,11 @@ export class Composio<TS extends ComposioToolset<any>> {
65
66 this.config = config;
67
58 - const defaultToolset = new ComposioToolset() as TS;
59 - this.toolset = config.toolset ?? defaultToolset;
68 + const defaultToolset = new ComposioToolset();
69 + this.toolset = config.toolset ?? (defaultToolset as unknown as TToolset);
70
61 - // initialize models
62 - this.tools = new Tools<ExtractToolType<TS>, TS>(this.client, this.toolset);
71 + // Initialize models
72 + this.tools = new Tools(this.client, this.toolset);
73 this.toolkits = new Toolkits(this.client);
74 this.triggers = new Triggers(this.client);
75 this.org = new Org(this.client);
@@ -77,4 +87,19 @@ export class Composio<TS extends ComposioToolset<any>> {
87 }
88 return this.client;
89 }
90 +
91 + /**
92 + * Generic function to get all the tools
93 + * @returns {Promise<T[]>} Promise with list of tools
94 + */
95 + getTools(): Promise<TTool[]> {
96 + console.log("Tools");
97 + return Promise.resolve([]);
98 + // return this.tools.get();
99 + }
100 +
101 + getEntity(): Promise<void> {
102 + console.log("Entity");
103 + return Promise.resolve();
104 + }
105 }
packages/core/src/index.ts
+5 -4
@@ -1,6 +1,7 @@
1 import { Composio } from "./composio";
2 -import type { Tool } from "./models/Tools";
3 -import { ComposioToolset } from "./toolset/Toolset";
2 +import type { ComposioToolset } from "./toolset/ComposioToolset";
3 +import type { BaseComposioToolset } from "./toolset/BaseToolset";
4 +import type { Tool } from "./types/Tool.types";
5 +import type { Toolset } from "./types/Toolset.types.";
6
5 -export { Composio, Tool };
6 -export { ComposioToolset };
7 +export { Composio, ComposioToolset, BaseComposioToolset, Tool, Toolset };
packages/core/src/models/Tools.ts
+30 -29
@@ -1,36 +1,37 @@
1 import ComposioSDK from "@composio/client";
2 -import { ComposioToolset } from "../toolset/Toolset";
3 -import { ToolRetrieveResponse } from "@composio/client/resources/tools";
2 +import { Tool } from "../types/Tool.types";
3 +import { Toolset } from "../types/Toolset.types.";
4
5 -export type Tool = ToolRetrieveResponse;
5 +export class Tools<
6 + TTool extends Tool,
7 + TToolset extends Toolset<TTool>
8 +> {
9 + private client: ComposioSDK;
10 + private toolset: TToolset;
11
7 -export class Tools<T extends Tool, TS extends ComposioToolset<T>> {
8 - private client: ComposioSDK;
9 - private toolset: TS;
12 + constructor(client: ComposioSDK, toolset: TToolset) {
13 + this.client = client;
14 + this.toolset = toolset;
15 + }
16
11 - constructor(client: ComposioSDK, toolset: TS) {
12 - this.client = client;
13 - this.toolset = toolset;
14 - }
17 + async list() {
18 + return this.client.tools.list();
19 + }
20
16 - async list() {
17 - return this.client.tools.list();
18 - }
21 + async get(toolId: string): Promise<ReturnType<TToolset["_wrapTool"]>> {
22 + const tool: Tool = await this.client.tools.retrieve(toolId);
23 + return this.toolset._wrapTool(tool) as ReturnType<TToolset["_wrapTool"]>;
24 + }
25
20 - async get(toolId: string): Promise<T> {
21 - const tool = await this.client.tools.retrieve(toolId);
22 - return this.toolset.wrap(tool);
23 - }
26 + async execute(tool: string, body: { [key: string]: any }) {
27 + return this.client.tools.execute(tool, body);
28 + }
29
25 - async execute(tool: string, body: { [key: string]: any }) {
26 - return this.client.tools.execute(tool, body);
27 - }
28 -
29 - async getToolsByEnum(toolEnum: string) {
30 - return this.client.tools.retrieveEnum({
31 - body: {
32 - toolEnum
33 - }
34 - });
35 - }
36 -}
\ No newline at end of file
30 + async getToolsByEnum(toolEnum: string) {
31 + return this.client.tools.retrieveEnum({
32 + body: {
33 + toolEnum,
34 + },
35 + });
36 + }
37 +}
packages/core/src/toolset/BaseToolset.ts new
+42
@@ -0,0 +1,42 @@
1 +import { Toolset } from "../types/Toolset.types.";
2 +import { Tool } from "../types/Tool.types";
3 +
4 +/**
5 + * Base toolset implementation with proper generic defaults
6 + * This class is used to create a different toolsets by extending this class.
7 + *
8 + * This class provides basic functionality to get tools, pre-process and post-process tools.
9 + * Every toolset should extend this class and implement the `_wrapTool` method,
10 + * these extended toolsets can add their own functionality/methods to the toolset.
11 + *
12 + * eg:
13 + * ```ts
14 + * class MyToolset extends BaseComposioToolset<MyTool> {
15 + * _wrapTool(tool: BaseTool): MyTool {}
16 + * }
17 + * ```
18 + */
19 +export abstract class BaseComposioToolset<TTool extends Tool> implements Toolset<TTool> {
20 +
21 + /**
22 + * Wraps the tool to a specific type
23 + * This method should be implemented by the extended class to wrap the tool to a specific type.
24 + * @param tool The tool to wrap (This will be the tool retrieved from the API)
25 + * @returns The wrapped tool
26 + */
27 + abstract _wrapTool: (tool: Tool) => TTool;
28 +
29 + getTools(): TTool[] {
30 + return [];
31 + }
32 +
33 + // pre process the tool before executing it
34 + preProcessTool(tool: TTool): TTool {
35 + return tool;
36 + }
37 +
38 + // post process the tool after executing it
39 + postProcessTool(tool: TTool): TTool {
40 + return tool;
41 + }
42 +}
\ No newline at end of file
packages/core/src/toolset/ComposioToolset.ts new
+14
@@ -0,0 +1,14 @@
1 +import { Tool } from "../types/Tool.types";
2 +import { BaseComposioToolset } from "./BaseToolset";
3 +
4 +/**
5 + * This is a default toolset implementation for Composio.
6 + * This class is used as a default toolset for Composio, if the user does not provide a toolset.
7 + *
8 + * This class shouldn't be used directly or to be extended.
9 + */
10 +export class ComposioToolset extends BaseComposioToolset<Tool> {
11 + _wrapTool = (tool: Tool): Tool => {
12 + return tool;
13 + };
14 +}
packages/core/src/toolset/Toolset.ts deleted
-29
@@ -1,29 +0,0 @@
1 -import { Tool } from "../models/Tools";
2 -
3 -/**
4 - * Base toolset implementation with proper generic defaults
5 - */
6 -export class ComposioToolset<T extends Tool = Tool> {
7 - constructor() {
8 - // Constructor implementation
9 - }
10 -
11 - async processResponse() {}
12 -
13 - async getToolsSchema() {}
14 -
15 - async addSchemaProcessor() {}
16 -
17 - async addPreProcessor() {}
18 -
19 - async addPostProcessor() {}
20 -
21 - /**
22 - * Wraps a standard Tool into the specific tool type T
23 - * Default implementation just casts the tool
24 - */
25 - wrap(tool: Tool): T {
26 - console.log("Wrapping tool in ComposioToolset...");
27 - return tool as T;
28 - }
29 -}
\ No newline at end of file
packages/core/src/types/Tool.types.ts new
+3
@@ -0,0 +1,3 @@
1 +import { ToolRetrieveResponse } from "@composio/client/resources/tools";
2 +
3 +export type Tool = ToolRetrieveResponse;
packages/core/src/types/Toolset.types..ts new
+9
@@ -0,0 +1,9 @@
1 +import { Tool } from "./Tool.types";
2 +
3 +/**
4 + * Base toolset implementation, which needs to be implemented by the extended class.
5 + * This class is used to create a base toolset by implementing this class.
6 + */
7 +export interface Toolset<TTool extends Tool> {
8 + _wrapTool: (tool: Tool) => TTool;
9 +}
packages/examples/sample/src/index.ts
+10 -24
@@ -1,34 +1,20 @@
1 import { Composio } from "@composio/core";
2 -import { OpenAIToolSet } from "@composio/openai-toolset";
2 +import { OpenAIToolset } from "@composio/openai-toolset";
3
4 /**
5 * General example
6 */
7 async function constructorExample() {
8 - const composio = new Composio({
9 - apiKey: process.env.COMPOSIO_API_KEY,
10 - toolset: new OpenAIToolSet(),
11 - });
8 + const composio = new Composio({
9 + apiKey: process.env.COMPOSIO_API_KEY,
10 + toolset: new OpenAIToolset(),
11 + });
12
13 - const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
14 -
15 - composio.toolset.handleToolCall([tool]);
16 -}
13 + const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
14
18 -constructorExample();
19 -/**
20 - * Builder example
21 - */
22 -// async function builderExample() {
23 -// const composio = Composio.init()
24 -// .withToolset(new OpenAIToolSet())
25 -// .withApiKey(process.env.COMPOSIO_API_KEY as string)
26 -// .build();
15 + composio.toolset.handleToolCall(tool);
16
28 -// const tool = await composio.tools.get("HACKERNEWS_SEARCH_POSTS");
29 -
30 -// tool.execute();
31 -// }
17 + tool.execute({});
18 +}
19
33 -// constructorExample();
34 -// builderExample();
\ No newline at end of file
20 +constructorExample();
packages/toolsets/openai/src/index.ts
+19 -33
@@ -1,40 +1,26 @@
1 +import { BaseComposioToolset } from "@composio/core";
2 import { Tool } from "@composio/core";
2 -import { ComposioToolset } from "@composio/core";
3
4 -export interface OpenAIToolActions extends Tool {
5 - execute: () => Promise<any>;
4 +export interface OpenAITool extends Tool {
5 + execute: (args: any) => Promise<any>;
6 }
7 +export class OpenAIToolset extends BaseComposioToolset<OpenAITool> {
8 + handleToolCall = async (tool: OpenAITool) => {
9 + console.log(`Handling OpenAI tool call: ${tool.name}`);
10 + // Here you would implement the logic to handle the tool call
11 + // For example, calling an API or performing some computation
12 + return tool.execute({});
13 + };
14
8 -export class OpenAIToolSet extends ComposioToolset<OpenAIToolActions> {
9 - constructor() {
10 - super();
11 - }
12 -
13 - /**
14 - * Handles execution of multiple tools in sequence
15 - * @param tools Array of tools to execute
16 - * @returns Promise that resolves when all tools have executed
17 - */
18 - async handleToolCall(tools: OpenAIToolActions[]): Promise<void> {
19 - // Using Promise.all to properly handle all async operations
20 - await Promise.all(
21 - tools.map(tool => tool.execute())
22 - );
23 - }
24 -
25 - /**
26 - * Wraps a standard Tool into an OpenAIToolActions
27 - * @param tool The base tool to wrap
28 - * @returns A tool with OpenAI-specific functionality
29 - */
30 - override wrap(tool: Tool): OpenAIToolActions {
31 - const openAITool: OpenAIToolActions = {
15 + _wrapTool = (tool: Tool): OpenAITool => {
16 + return {
17 ...tool,
33 - execute: async () => {
34 - console.log(`Executing tool: ${tool.name}`);
18 + execute: async (args: any) => {
19 + console.log(`Executing OpenAI tool: ${tool.name}`);
20 + // Here you would implement the actual execution logic
21 + // For example, calling an API or performing some computation
22 + return Promise.resolve(args);
23 },
24 };
37 -
38 - return openAITool;
39 - }
40 -}
\ No newline at end of file
25 + };
26 +}