|
| 1 | +import { Tool } from '@langchain/core/tools' |
| 2 | +import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface' |
| 3 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 4 | +import { LangchainToolSet } from 'composio-core' |
| 5 | + |
| 6 | +class ComposioTool extends Tool { |
| 7 | + name = 'composio' |
| 8 | + description = 'Tool for interacting with Composio applications and performing actions' |
| 9 | + toolset: any |
| 10 | + appName: string |
| 11 | + actions: string[] |
| 12 | + |
| 13 | + constructor(toolset: any, appName: string, actions: string[]) { |
| 14 | + super() |
| 15 | + this.toolset = toolset |
| 16 | + this.appName = appName |
| 17 | + this.actions = actions |
| 18 | + } |
| 19 | + |
| 20 | + async _call(input: string): Promise<string> { |
| 21 | + try { |
| 22 | + return `Executed action on ${this.appName} with input: ${input}` |
| 23 | + } catch (error) { |
| 24 | + return 'Failed to execute action' |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class Composio_Tools implements INode { |
| 30 | + label: string |
| 31 | + name: string |
| 32 | + version: number |
| 33 | + type: string |
| 34 | + icon: string |
| 35 | + category: string |
| 36 | + description: string |
| 37 | + baseClasses: string[] |
| 38 | + credential: INodeParams |
| 39 | + inputs: INodeParams[] |
| 40 | + |
| 41 | + constructor() { |
| 42 | + this.label = 'Composio' |
| 43 | + this.name = 'composio' |
| 44 | + this.version = 1.0 |
| 45 | + this.type = 'Composio' |
| 46 | + this.icon = 'composio.svg' |
| 47 | + this.category = 'Tools' |
| 48 | + this.description = 'Toolset with over 250+ Apps for building AI-powered applications' |
| 49 | + this.baseClasses = [this.type, ...getBaseClasses(ComposioTool)] |
| 50 | + this.credential = { |
| 51 | + label: 'Connect Credential', |
| 52 | + name: 'credential', |
| 53 | + type: 'credential', |
| 54 | + credentialNames: ['composioApi'] |
| 55 | + } |
| 56 | + this.inputs = [ |
| 57 | + { |
| 58 | + label: 'App Name', |
| 59 | + name: 'appName', |
| 60 | + type: 'asyncOptions', |
| 61 | + loadMethod: 'listApps', |
| 62 | + description: 'Select the app to connect with', |
| 63 | + refresh: true |
| 64 | + }, |
| 65 | + { |
| 66 | + label: 'Auth Status', |
| 67 | + name: 'authStatus', |
| 68 | + type: 'asyncOptions', |
| 69 | + loadMethod: 'authStatus', |
| 70 | + placeholder: 'Connection status will appear here', |
| 71 | + refresh: true |
| 72 | + }, |
| 73 | + { |
| 74 | + label: 'Actions to Use', |
| 75 | + name: 'actions', |
| 76 | + type: 'asyncOptions', |
| 77 | + loadMethod: 'listActions', |
| 78 | + description: 'Select the actions you want to use', |
| 79 | + refresh: true |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + |
| 84 | + //@ts-ignore |
| 85 | + loadMethods = { |
| 86 | + listApps: async (nodeData: INodeData, options?: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 87 | + try { |
| 88 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options ?? {}) |
| 89 | + const composioApiKey = getCredentialParam('composioApi', credentialData, nodeData) |
| 90 | + |
| 91 | + if (!composioApiKey) { |
| 92 | + return [ |
| 93 | + { |
| 94 | + label: 'API Key Required', |
| 95 | + name: 'placeholder', |
| 96 | + description: 'Enter Composio API key in the credential field' |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + |
| 101 | + const toolset = new LangchainToolSet({ apiKey: composioApiKey }) |
| 102 | + const apps = await toolset.client.apps.list() |
| 103 | + apps.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 104 | + |
| 105 | + return apps.map(({ name, ...rest }) => ({ |
| 106 | + label: name.toUpperCase(), |
| 107 | + name: name, |
| 108 | + description: rest.description || name |
| 109 | + })) |
| 110 | + } catch (error) { |
| 111 | + console.error('Error loading apps:', error) |
| 112 | + return [ |
| 113 | + { |
| 114 | + label: 'Error Loading Apps', |
| 115 | + name: 'error', |
| 116 | + description: 'Failed to load apps. Please check your API key and try again' |
| 117 | + } |
| 118 | + ] |
| 119 | + } |
| 120 | + }, |
| 121 | + listActions: async (nodeData: INodeData, options?: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 122 | + try { |
| 123 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options ?? {}) |
| 124 | + const composioApiKey = getCredentialParam('composioApi', credentialData, nodeData) |
| 125 | + const appName = nodeData.inputs?.appName as string |
| 126 | + |
| 127 | + if (!composioApiKey) { |
| 128 | + return [ |
| 129 | + { |
| 130 | + label: 'API Key Required', |
| 131 | + name: 'placeholder', |
| 132 | + description: 'Enter Composio API key in the credential field' |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + |
| 137 | + if (!appName) { |
| 138 | + return [ |
| 139 | + { |
| 140 | + label: 'Select an App first', |
| 141 | + name: 'placeholder', |
| 142 | + description: 'Select an app from the dropdown to view available actions' |
| 143 | + } |
| 144 | + ] |
| 145 | + } |
| 146 | + |
| 147 | + const toolset = new LangchainToolSet({ apiKey: composioApiKey }) |
| 148 | + const actions = await toolset.getTools({ apps: [appName] }) |
| 149 | + actions.sort((a: any, b: any) => a.name.localeCompare(b.name)) |
| 150 | + |
| 151 | + return actions.map(({ name, ...rest }) => ({ |
| 152 | + label: name.toUpperCase(), |
| 153 | + name: name, |
| 154 | + description: rest.description || name |
| 155 | + })) |
| 156 | + } catch (error) { |
| 157 | + console.error('Error loading actions:', error) |
| 158 | + return [ |
| 159 | + { |
| 160 | + label: 'Error Loading Actions', |
| 161 | + name: 'error', |
| 162 | + description: 'Failed to load actions. Please check your API key and try again' |
| 163 | + } |
| 164 | + ] |
| 165 | + } |
| 166 | + }, |
| 167 | + authStatus: async (nodeData: INodeData, options?: ICommonObject): Promise<INodeOptionsValue[]> => { |
| 168 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options ?? {}) |
| 169 | + const composioApiKey = getCredentialParam('composioApi', credentialData, nodeData) |
| 170 | + const appName = nodeData.inputs?.appName as string |
| 171 | + |
| 172 | + if (!composioApiKey) { |
| 173 | + return [ |
| 174 | + { |
| 175 | + label: 'API Key Required', |
| 176 | + name: 'placeholder', |
| 177 | + description: 'Enter Composio API key in the credential field' |
| 178 | + } |
| 179 | + ] |
| 180 | + } |
| 181 | + |
| 182 | + if (!appName) { |
| 183 | + return [ |
| 184 | + { |
| 185 | + label: 'Select an App first', |
| 186 | + name: 'placeholder', |
| 187 | + description: 'Select an app from the dropdown to view available actions' |
| 188 | + } |
| 189 | + ] |
| 190 | + } |
| 191 | + |
| 192 | + const toolset = new LangchainToolSet({ apiKey: composioApiKey }) |
| 193 | + const authStatus = await toolset.client.getEntity('default').getConnection({ app: appName.toLowerCase() }) |
| 194 | + return [ |
| 195 | + { |
| 196 | + label: authStatus ? 'Connected' : 'Not Connected', |
| 197 | + name: authStatus ? 'Connected' : 'Not Connected', |
| 198 | + description: authStatus ? 'Selected app has an active connection' : 'Please connect the app on app.composio.dev' |
| 199 | + } |
| 200 | + ] |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 205 | + if (!nodeData.inputs) nodeData.inputs = {} |
| 206 | + |
| 207 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 208 | + const composioApiKey = getCredentialParam('composioApi', credentialData, nodeData) |
| 209 | + |
| 210 | + if (!composioApiKey) { |
| 211 | + nodeData.inputs = { |
| 212 | + appName: undefined, |
| 213 | + authStatus: '', |
| 214 | + actions: [] |
| 215 | + } |
| 216 | + throw new Error('API Key Required') |
| 217 | + } |
| 218 | + |
| 219 | + const toolset = new LangchainToolSet({ apiKey: composioApiKey }) |
| 220 | + const tools = await toolset.getTools({ actions: [nodeData.inputs?.actions as string] }) |
| 221 | + return tools |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +module.exports = { nodeClass: Composio_Tools } |
0 commit comments