Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Stable version with any
haxzie committed
Apr 16, 2025 at 13:06 UTC
e650c3b1349892463cbab708482a183427882847
5 files changed
+39
-51
packages/core/src/composio.ts
+11
-24
@@ -9,33 +9,27 @@ import { AuthConfigs } from "./models/AuthConfigs";
9
import { ConnectedAccounts } from "./models/ConnectedAccounts";
10
import { ToolListParams } from "@composio/client/resources/tools";
11
12
-export type ComposioConfig<
13
- TTool,
14
- TToolset extends Toolset<TTool>
15
-> = {
12
+export type ComposioConfig<T extends Toolset<any>> = {
13
apiKey?: string;
14
baseURL?: string;
15
runtime?: string;
16
allowTracking?: boolean;
17
allowTracing?: boolean;
21
- toolset?: TToolset;
18
+ toolset?: T;
19
};
20
21
/**
22
* This is the core class for Composio.
23
* It is used to initialize the Composio SDK and provide a global configuration.
24
*/
28
-export class Composio<
29
- TTool,
30
- TToolset extends Toolset<TTool> = Toolset<TTool>
31
-> {
25
+export class Composio<T extends Toolset<any>> {
26
/**
27
* The Composio API client.
28
* @type {ComposioClient}
29
*/
30
private client: ComposioClient;
31
38
- private config: ComposioConfig<TTool, TToolset>;
32
+ private config: ComposioConfig<T>;
33
34
/**
35
* Core models for Composio.
@@ -43,7 +37,7 @@ export class Composio<
37
tools: Tools;
38
toolkits: Toolkits;
39
triggers: Triggers;
46
- toolset: TToolset;
40
+ toolset: T;
41
authConfigs: AuthConfigs;
42
connectedAccounts: ConnectedAccounts;
43
@@ -56,14 +50,7 @@ export class Composio<
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
*/
59
- constructor(config: {
60
- apiKey?: string;
61
- baseURL?: string;
62
- runtime?: string;
63
- allowTracking?: boolean;
64
- allowTracing?: boolean;
65
- toolset?: TToolset;
66
- }) {
53
+ constructor(config: ComposioConfig<T>) {
54
/**
55
* Initialize the Composio SDK client.
56
* The client is used to make API calls to the Composio API.
@@ -83,7 +70,7 @@ export class Composio<
70
* Set the default toolset, if not provided by the user.
71
*/
72
const defaultToolset = new ComposioToolset();
86
- this.toolset = config.toolset ?? (defaultToolset as unknown as TToolset);
73
+ this.toolset = config.toolset ?? (defaultToolset as unknown as T);
74
this.toolset.setClient(this);
75
76
/**
@@ -113,9 +100,9 @@ export class Composio<
100
* Generic function to get all the tools
101
* @returns {Promise<T[]>} Promise with list of tools
102
*/
116
- async getTool(id: string): Promise<WrappedTool<TToolset>> {
103
+ async getTool(id: string): Promise<WrappedTool<T>> {
104
const tool = await this.tools.get(id);
118
- return this.toolset._wrapTool(tool) as WrappedTool<TToolset>;
105
+ return this.toolset._wrapTool(tool) as WrappedTool<T>;
106
}
107
108
/**
@@ -123,8 +110,8 @@ export class Composio<
110
* @param query - Query parameters for the tools
111
* @returns {Promise<T[]>} Promise with list of tools
112
*/
126
- async getTools(query?: ToolListParams): Promise<WrappedTool<TToolset>[]> {
113
+ async getTools(query?: ToolListParams): Promise<WrappedTool<T>[]> {
114
const tools = await this.tools.list(query);
128
- return tools.items.map((tool) => this.toolset._wrapTool(tool as Tool)) as WrappedTool<TToolset>[];
115
+ return tools.items.map((tool) => this.toolset._wrapTool(tool as Tool)) as WrappedTool<T>[];
116
}
117
}
packages/core/src/toolset/BaseToolset.ts
+10
-13
@@ -18,20 +18,17 @@ import { Composio } from "../composio";
18
* ```
19
*/
20
export abstract class BaseComposioToolset<TTool> implements Toolset<TTool> {
21
+ protected client: Composio<Toolset<TTool>> | undefined;
22
22
- /**
23
- * Sets the client for the toolset
24
- * @param client
25
- */
26
- abstract setClient(client: Composio<TTool, Toolset<TTool>>): void;
23
+ setClient(client: Composio<Toolset<TTool>>): void {
24
+ this.client = client;
25
+ }
26
28
- /**
29
- * Wraps the tool to a specific type
30
- * This method should be implemented by the extended class to wrap the tool to a specific type.
31
- * @param tool The tool to wrap (This will be the tool retrieved from the API)
32
- * @returns The wrapped tool
33
- */
34
- abstract _wrapTool: (tool: Tool) => TTool;
27
+ abstract _wrapTool(tool: Tool): TTool;
28
36
- // @TODO add pre-process and post-process methods
29
+ protected ensureClient(): void {
30
+ if (!this.client) {
31
+ throw new Error('Client not initialized. Make sure the toolset is properly initialized with Composio.');
32
+ }
33
+ }
34
}
\ No newline at end of file
packages/core/src/toolset/ComposioToolset.ts
+8
-13
@@ -1,7 +1,6 @@
1
import { Composio } from "../composio";
2
import { Tool } from "../types/tool.types";
3
import { BaseComposioToolset } from "./BaseToolset";
4
-import { Toolset } from "../types/toolset.types.";
4
5
/**
6
* This is a default toolset implementation for Composio.
@@ -10,24 +9,20 @@ import { Toolset } from "../types/toolset.types.";
9
* This class shouldn't be used directly or to be extended.
10
*/
11
13
-interface CustomTool {
14
- name: string;
15
-}
16
-export class ComposioToolset extends BaseComposioToolset<CustomTool> {
17
- private client: Composio<CustomTool, Toolset<CustomTool>> | undefined;
18
-
19
- setClient(client: Composio<CustomTool, Toolset<CustomTool>>) {
20
- this.client = client;
21
- }
12
+export class ComposioToolset extends BaseComposioToolset<Tool> {
13
23
- _wrapTool = (tool: Tool): CustomTool => {
24
- return tool as unknown as CustomTool;
14
+ _wrapTool = (tool: Tool): Tool => {
15
+ return tool as Tool;
16
};
17
+
18
+ async test() {}
19
}
20
21
+
22
const composio = new Composio({
23
apiKey: process.env.COMPOSIO_API_KEY,
24
toolset: new ComposioToolset(),
25
});
26
33
-
27
+composio.toolset.test();
28
+composio.getTool("123");
\ No newline at end of file
packages/core/src/types/Toolset.types..ts
+1
-1
@@ -7,7 +7,7 @@ import { Tool } from "./tool.types";
7
*/
8
export interface Toolset<TTool> {
9
_wrapTool: (tool: Tool) => TTool;
10
- setClient: (client: Composio<TTool, Toolset<TTool>>) => void;
10
+ setClient<T extends Toolset<TTool>>(client: Composio<T>): void;
11
}
12
13
packages/core/src/types/toolset.types.ts
new
+9
@@ -0,0 +1,9 @@
1
+import { Tool } from "./tool.types";
2
+import { Composio } from "../composio";
3
+
4
+export interface Toolset<TTool> {
5
+ _wrapTool(tool: Tool): TTool;
6
+ setClient(client: Composio<Toolset<TTool>>): void;
7
+}
8
+
9
+export type WrappedTool<T> = T extends Toolset<infer U> ? U : never;
\ No newline at end of file