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

next
ts 45 lines 1.97 KB
Raw
1 import { ToolkitVersion, ToolkitVersionParam } from '../types/tool.types';
2
3 /**
4 * Canonicalizes a toolkit slug into the form used as a version-map key.
5 *
6 * Toolkit slugs are matched case-insensitively. This is the single source of
7 * truth for that rule: every write into a version map (env vars, user-supplied
8 * objects — see `getToolkitVersionsFromEnv`) and every read out of one MUST go
9 * through this helper so the two sides can never drift apart and silently miss a
10 * configured pin.
11 *
12 * Kept intentionally equivalent to the Python SDK's `normalize_toolkit_slug`
13 * (see python/composio/utils/toolkit_version.py).
14 *
15 * @param toolkitSlug - The slug/name of the toolkit, in any casing
16 * @returns The normalized (lowercase) slug used as a version-map key
17 */
18 export const normalizeToolkitSlug = (toolkitSlug: string): string => toolkitSlug.toLowerCase();
19
20 /**
21 * Gets the version for a specific toolkit based on the provided toolkit versions configuration.
22 *
23 * @param toolkitSlug - The slug/name of the toolkit to get the version for
24 * @param toolkitVersions - Optional toolkit versions configuration (string for global version or object mapping toolkit slugs to versions)
25 * @returns The toolkit version to use - either the specific version from config, or 'latest' as fallback
26 */
27 export const getToolkitVersion = (
28 toolkitSlug: string,
29 toolkitVersions?: ToolkitVersionParam
30 ): ToolkitVersion => {
31 // If toolkitVersions is a string, use it as a global version for all toolkits
32 if (typeof toolkitVersions === 'string') {
33 return toolkitVersions;
34 }
35 // If toolkitVersions is an object mapping, look up the specific toolkit version.
36 // The map is keyed by normalized slugs, so normalize the lookup too
37 // (see normalizeToolkitSlug for why).
38 const hasToolkitVersion = toolkitVersions && Object.keys(toolkitVersions).length > 0;
39 if (hasToolkitVersion) {
40 return toolkitVersions[normalizeToolkitSlug(toolkitSlug)] ?? 'latest';
41 }
42
43 // Else use 'latest'
44 return 'latest';
45 };