@composio/langchain 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,211 +1,193 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ let _composio_core = require("@composio/core");
2
+ let _langchain_core_tools = require("@langchain/core/tools");
19
3
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- LangchainProvider: () => LangchainProvider
24
- });
25
- module.exports = __toCommonJS(index_exports);
26
- var import_core = require("@composio/core");
27
- var import_tools = require("@langchain/core/tools");
28
- var LangchainProvider = class extends import_core.BaseAgenticProvider {
29
- name = "langchain";
30
- /**
31
- * Creates a new instance of the LangchainProvider.
32
- *
33
- * This provider enables integration with the Langchain framework,
34
- * allowing Composio tools to be used with Langchain agents and chains.
35
- *
36
- * @example
37
- * ```typescript
38
- * // Initialize the Langchain provider
39
- * const provider = new LangchainProvider();
40
- *
41
- * // Use with Composio
42
- * const composio = new Composio({
43
- * apiKey: 'your-api-key',
44
- * provider: new LangchainProvider()
45
- * });
46
- *
47
- * // Use the provider to wrap tools for Langchain
48
- * const langchainTools = provider.wrapTools(composioTools, composio.tools.execute);
49
- * ```
50
- */
51
- constructor() {
52
- super();
53
- }
54
- /**
55
- * Transform MCP URL response into Anthropic-specific format.
56
- * By default, Anthropic uses the standard format (same as default),
57
- * but this method is here to show providers can customize if needed.
58
- *
59
- * @param data - The MCP URL response data
60
- * @returns Standard MCP server response format
61
- */
62
- wrapMcpServerResponse(data) {
63
- return data.map((item) => ({
64
- url: new URL(item.url),
65
- name: item.name
66
- }));
67
- }
68
- /**
69
- * Wraps a Composio tool in the Langchain DynamicStructuredTool format.
70
- *
71
- * This method transforms a Composio tool definition into a Langchain
72
- * DynamicStructuredTool that can be used with Langchain agents and chains.
73
- *
74
- * @param tool - The Composio tool to wrap
75
- * @param executeTool - Function to execute the tool
76
- * @returns The wrapped tool as a Langchain DynamicStructuredTool
77
- *
78
- * @example
79
- * ```typescript
80
- * // Wrap a single tool for use with Langchain
81
- * const composioTool = {
82
- * slug: 'SEARCH_TOOL',
83
- * description: 'Search for information',
84
- * toolkit: { name: 'search_toolkit' },
85
- * inputParameters: {
86
- * type: 'object',
87
- * properties: {
88
- * query: { type: 'string' }
89
- * },
90
- * required: ['query']
91
- * }
92
- * };
93
- *
94
- * // Create a Langchain tool using the provider
95
- * const langchainTool = provider.wrapTool(
96
- * composioTool,
97
- * composio.tools.execute
98
- * );
99
- *
100
- * // Use with Langchain
101
- * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
102
- * import { ChatOpenAI } from '@langchain/openai';
103
- *
104
- * const model = new ChatOpenAI({ temperature: 0 });
105
- * const agent = await createOpenAIFunctionsAgent({
106
- * llm: model,
107
- * tools: [langchainTool]
108
- * });
109
- *
110
- * const executor = new AgentExecutor({
111
- * agent,
112
- * tools: [langchainTool]
113
- * });
114
- *
115
- * const result = await executor.invoke({
116
- * input: "Search for information about Composio"
117
- * });
118
- * ```
119
- */
120
- wrapTool(tool, executeTool) {
121
- const toolName = tool.slug;
122
- const description = tool.description;
123
- const appName = tool.toolkit?.name?.toLowerCase();
124
- if (!appName) {
125
- throw new Error("App name is not defined");
126
- }
127
- const func = async (...args) => {
128
- const result = await executeTool(toolName, args[0]);
129
- return JSON.stringify(result);
130
- };
131
- if (!tool.inputParameters) {
132
- throw new Error("Tool input parameters are not defined");
133
- }
134
- const parameters = (0, import_core.jsonSchemaToZodSchema)(tool.inputParameters);
135
- return new import_tools.DynamicStructuredTool({
136
- name: toolName,
137
- description: description || "",
138
- schema: parameters,
139
- func
140
- });
141
- }
142
- /**
143
- * Wraps a list of Composio tools in the Langchain DynamicStructuredTool format.
144
- *
145
- * This method transforms multiple Composio tool definitions into Langchain
146
- * DynamicStructuredTools that can be used with Langchain agents and chains.
147
- *
148
- * @param tools - Array of Composio tools to wrap
149
- * @param executeTool - Function to execute the tools
150
- * @returns Array of wrapped tools as Langchain DynamicStructuredTools
151
- *
152
- * @example
153
- * ```typescript
154
- * // Wrap multiple tools for use with Langchain
155
- * const composioTools = [
156
- * {
157
- * slug: 'SEARCH_TOOL',
158
- * description: 'Search for information',
159
- * toolkit: { name: 'search_toolkit' },
160
- * inputParameters: {
161
- * type: 'object',
162
- * properties: {
163
- * query: { type: 'string' }
164
- * },
165
- * required: ['query']
166
- * }
167
- * },
168
- * {
169
- * slug: 'WEATHER_TOOL',
170
- * description: 'Get weather information',
171
- * toolkit: { name: 'weather_toolkit' },
172
- * inputParameters: {
173
- * type: 'object',
174
- * properties: {
175
- * location: { type: 'string' }
176
- * },
177
- * required: ['location']
178
- * }
179
- * }
180
- * ];
181
- *
182
- * // Create Langchain tools using the provider
183
- * const langchainTools = provider.wrapTools(
184
- * composioTools,
185
- * composio.tools.execute
186
- * );
187
- *
188
- * // Use with Langchain
189
- * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
190
- * import { ChatOpenAI } from '@langchain/openai';
191
- *
192
- * const model = new ChatOpenAI({ temperature: 0 });
193
- * const agent = await createOpenAIFunctionsAgent({
194
- * llm: model,
195
- * tools: langchainTools
196
- * });
197
- *
198
- * const executor = new AgentExecutor({
199
- * agent,
200
- * tools: langchainTools
201
- * });
202
- * ```
203
- */
204
- wrapTools(tools, executeTool) {
205
- return tools.map((tool) => this.wrapTool(tool, executeTool));
206
- }
4
+ //#region src/index.ts
5
+ /**
6
+ * Langchain Provider
7
+ *
8
+ * Author: Musthaq Ahamad <musthaq@composio.dev>
9
+ * Reference: https://github.com/ComposioHQ/composio/blob/master/js/src/frameworks/langchain.ts
10
+ *
11
+ * This provider provides a set of tools for interacting with Langchain.
12
+ *
13
+ * @packageDocumentation
14
+ * @module providers/langchain
15
+ */
16
+ var LangchainProvider = class extends _composio_core.BaseAgenticProvider {
17
+ name = "langchain";
18
+ /**
19
+ * Creates a new instance of the LangchainProvider.
20
+ *
21
+ * This provider enables integration with the Langchain framework,
22
+ * allowing Composio tools to be used with Langchain agents and chains.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Initialize the Langchain provider
27
+ * const provider = new LangchainProvider();
28
+ *
29
+ * // Use with Composio
30
+ * const composio = new Composio({
31
+ * apiKey: 'your-api-key',
32
+ * provider: new LangchainProvider()
33
+ * });
34
+ *
35
+ * // Use the provider to wrap tools for Langchain
36
+ * const langchainTools = provider.wrapTools(composioTools, composio.tools.execute);
37
+ * ```
38
+ */
39
+ constructor() {
40
+ super();
41
+ }
42
+ /**
43
+ * Transform MCP URL response into Anthropic-specific format.
44
+ * By default, Anthropic uses the standard format (same as default),
45
+ * but this method is here to show providers can customize if needed.
46
+ *
47
+ * @param data - The MCP URL response data
48
+ * @returns Standard MCP server response format
49
+ */
50
+ wrapMcpServerResponse(data) {
51
+ return data.map((item) => ({
52
+ url: new URL(item.url),
53
+ name: item.name
54
+ }));
55
+ }
56
+ /**
57
+ * Wraps a Composio tool in the Langchain DynamicStructuredTool format.
58
+ *
59
+ * This method transforms a Composio tool definition into a Langchain
60
+ * DynamicStructuredTool that can be used with Langchain agents and chains.
61
+ *
62
+ * @param tool - The Composio tool to wrap
63
+ * @param executeTool - Function to execute the tool
64
+ * @returns The wrapped tool as a Langchain DynamicStructuredTool
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * // Wrap a single tool for use with Langchain
69
+ * const composioTool = {
70
+ * slug: 'SEARCH_TOOL',
71
+ * description: 'Search for information',
72
+ * toolkit: { name: 'search_toolkit' },
73
+ * inputParameters: {
74
+ * type: 'object',
75
+ * properties: {
76
+ * query: { type: 'string' }
77
+ * },
78
+ * required: ['query']
79
+ * }
80
+ * };
81
+ *
82
+ * // Create a Langchain tool using the provider
83
+ * const langchainTool = provider.wrapTool(
84
+ * composioTool,
85
+ * composio.tools.execute
86
+ * );
87
+ *
88
+ * // Use with Langchain
89
+ * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
90
+ * import { ChatOpenAI } from '@langchain/openai';
91
+ *
92
+ * const model = new ChatOpenAI({ temperature: 0 });
93
+ * const agent = await createOpenAIFunctionsAgent({
94
+ * llm: model,
95
+ * tools: [langchainTool]
96
+ * });
97
+ *
98
+ * const executor = new AgentExecutor({
99
+ * agent,
100
+ * tools: [langchainTool]
101
+ * });
102
+ *
103
+ * const result = await executor.invoke({
104
+ * input: "Search for information about Composio"
105
+ * });
106
+ * ```
107
+ */
108
+ wrapTool(tool, executeTool) {
109
+ const toolName = tool.slug;
110
+ const description = tool.description;
111
+ if (!tool.toolkit?.name?.toLowerCase()) throw new Error("App name is not defined");
112
+ const func = async (...args) => {
113
+ const result = await executeTool(toolName, args[0]);
114
+ return JSON.stringify(result);
115
+ };
116
+ if (!tool.inputParameters) throw new Error("Tool input parameters are not defined");
117
+ const parameters = (0, _composio_core.jsonSchemaToZodSchema)(tool.inputParameters);
118
+ return new _langchain_core_tools.DynamicStructuredTool({
119
+ name: toolName,
120
+ description: description || "",
121
+ schema: parameters,
122
+ func
123
+ });
124
+ }
125
+ /**
126
+ * Wraps a list of Composio tools in the Langchain DynamicStructuredTool format.
127
+ *
128
+ * This method transforms multiple Composio tool definitions into Langchain
129
+ * DynamicStructuredTools that can be used with Langchain agents and chains.
130
+ *
131
+ * @param tools - Array of Composio tools to wrap
132
+ * @param executeTool - Function to execute the tools
133
+ * @returns Array of wrapped tools as Langchain DynamicStructuredTools
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * // Wrap multiple tools for use with Langchain
138
+ * const composioTools = [
139
+ * {
140
+ * slug: 'SEARCH_TOOL',
141
+ * description: 'Search for information',
142
+ * toolkit: { name: 'search_toolkit' },
143
+ * inputParameters: {
144
+ * type: 'object',
145
+ * properties: {
146
+ * query: { type: 'string' }
147
+ * },
148
+ * required: ['query']
149
+ * }
150
+ * },
151
+ * {
152
+ * slug: 'WEATHER_TOOL',
153
+ * description: 'Get weather information',
154
+ * toolkit: { name: 'weather_toolkit' },
155
+ * inputParameters: {
156
+ * type: 'object',
157
+ * properties: {
158
+ * location: { type: 'string' }
159
+ * },
160
+ * required: ['location']
161
+ * }
162
+ * }
163
+ * ];
164
+ *
165
+ * // Create Langchain tools using the provider
166
+ * const langchainTools = provider.wrapTools(
167
+ * composioTools,
168
+ * composio.tools.execute
169
+ * );
170
+ *
171
+ * // Use with Langchain
172
+ * import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
173
+ * import { ChatOpenAI } from '@langchain/openai';
174
+ *
175
+ * const model = new ChatOpenAI({ temperature: 0 });
176
+ * const agent = await createOpenAIFunctionsAgent({
177
+ * llm: model,
178
+ * tools: langchainTools
179
+ * });
180
+ *
181
+ * const executor = new AgentExecutor({
182
+ * agent,
183
+ * tools: langchainTools
184
+ * });
185
+ * ```
186
+ */
187
+ wrapTools(tools, executeTool) {
188
+ return tools.map((tool) => this.wrapTool(tool, executeTool));
189
+ }
207
190
  };
208
- // Annotate the CommonJS export names for ESM import in node:
209
- 0 && (module.exports = {
210
- LangchainProvider
211
- });
191
+
192
+ //#endregion
193
+ exports.LangchainProvider = LangchainProvider;