Skip to content

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

2.0 KB
Raw Blame
import { ToolkitVersion, ToolkitVersionParam } from '../types/tool.types';

/**
 * Canonicalizes a toolkit slug into the form used as a version-map key.
 *
 * Toolkit slugs are matched case-insensitively. This is the single source of
 * truth for that rule: every write into a version map (env vars, user-supplied
 * objects — see `getToolkitVersionsFromEnv`) and every read out of one MUST go
 * through this helper so the two sides can never drift apart and silently miss a
 * configured pin.
 *
 * Kept intentionally equivalent to the Python SDK's `normalize_toolkit_slug`
 * (see python/composio/utils/toolkit_version.py).
 *
 * @param toolkitSlug - The slug/name of the toolkit, in any casing
 * @returns The normalized (lowercase) slug used as a version-map key
 */
export const normalizeToolkitSlug = (toolkitSlug: string): string => toolkitSlug.toLowerCase();

/**
 * Gets the version for a specific toolkit based on the provided toolkit versions configuration.
 *
 * @param toolkitSlug - The slug/name of the toolkit to get the version for
 * @param toolkitVersions - Optional toolkit versions configuration (string for global version or object mapping toolkit slugs to versions)
 * @returns The toolkit version to use - either the specific version from config, or 'latest' as fallback
 */
export const getToolkitVersion = (
  toolkitSlug: string,
  toolkitVersions?: ToolkitVersionParam
): ToolkitVersion => {
  // If toolkitVersions is a string, use it as a global version for all toolkits
  if (typeof toolkitVersions === 'string') {
    return toolkitVersions;
  }
  // If toolkitVersions is an object mapping, look up the specific toolkit version.
  // The map is keyed by normalized slugs, so normalize the lookup too
  // (see normalizeToolkitSlug for why).
  const hasToolkitVersion = toolkitVersions && Object.keys(toolkitVersions).length > 0;
  if (hasToolkitVersion) {
    return toolkitVersions[normalizeToolkitSlug(toolkitSlug)] ?? 'latest';
  }

  // Else use 'latest'
  return 'latest';
};