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 131 lines 4.26 KB
Raw
1 import { Config, Effect, Option, String } from 'effect';
2 import { APP_CONFIG } from 'src/config';
3
4 /**
5 * Represents a toolkit with its version specification.
6 * The `toolkitSlug` is guaranteed to be lowercase at the type level.
7 */
8 export interface ToolkitVersionSpec {
9 readonly toolkitSlug: Lowercase<string>;
10 readonly toolkitVersion: string;
11 }
12
13 /**
14 * Map type with lowercase toolkit slugs as keys.
15 */
16 export type ToolkitVersionOverrides = Map<Lowercase<string>, string>;
17
18 /**
19 * Regex pattern for valid version strings.
20 * Only allows: alphanumeric characters (a-z, A-Z, 0-9), hyphens, underscores, and dots.
21 */
22 const VALID_VERSION_PATTERN = /^[a-zA-Z0-9._-]+$/;
23
24 /**
25 * Sanitizes a version string by removing invalid characters.
26 * Only keeps: alphanumeric characters (a-z, A-Z, 0-9), hyphens, underscores, and dots.
27 *
28 * @param version - The version string to sanitize
29 * @returns The sanitized version string, or null if it becomes empty after sanitization
30 */
31 export const sanitizeVersionString = (version: string): string | null => {
32 // If the version already matches the valid pattern, return as-is
33 if (VALID_VERSION_PATTERN.test(version)) {
34 return version;
35 }
36
37 // Remove invalid characters
38 const sanitized = version.replace(/[^a-zA-Z0-9._-]/g, '');
39
40 // Return null if the sanitized version is empty
41 return sanitized.length > 0 ? sanitized : null;
42 };
43
44 /**
45 * Reads `COMPOSIO_TOOLKIT_VERSION_<TOOLKIT>=<version>` pins into a map keyed by lowercase
46 * toolkit slug. `latest` and blank values are skipped; versions are sanitized.
47 *
48 * @example
49 * // Given: COMPOSIO_TOOLKIT_VERSION_GMAIL=20250901_00
50 * // Returns: Map { "gmail" => "20250901_00" }
51 */
52 export const getToolkitVersionOverrides: Effect.Effect<
53 ToolkitVersionOverrides,
54 Config.ConfigError
55 > = Effect.map(APP_CONFIG.TOOLKIT_VERSIONS, maybeOverrides => {
56 const result = new Map<Lowercase<string>, string>();
57 for (const [key, value] of Object.entries(Option.getOrElse(maybeOverrides, () => ({})))) {
58 if (!value || value === 'latest') continue;
59 const sanitizedVersion = sanitizeVersionString(value);
60 if (sanitizedVersion) {
61 result.set(String.toLowerCase(key), sanitizedVersion);
62 }
63 }
64 return result;
65 });
66
67 /**
68 * Builds an array of ToolkitVersionSpec from toolkit slugs and version overrides.
69 *
70 * @example
71 * // Given slugs: ['gmail', 'slack', 'github']
72 * // Given overrides: Map { "gmail" => "20250901_00" }
73 * // Returns: [
74 * // { toolkitSlug: 'gmail', toolkitVersion: '20250901_00' },
75 * // { toolkitSlug: 'slack', toolkitVersion: 'latest' },
76 * // { toolkitSlug: 'github', toolkitVersion: 'latest' }
77 * // ]
78 */
79 export const buildToolkitVersionSpecs = (
80 toolkitSlugs: ReadonlyArray<string>,
81 overrides: ToolkitVersionOverrides
82 ): ReadonlyArray<ToolkitVersionSpec> =>
83 toolkitSlugs.map(slug => {
84 const normalizedSlug = String.toLowerCase(slug);
85 return {
86 toolkitSlug: normalizedSlug,
87 toolkitVersion: overrides.get(normalizedSlug) ?? 'latest',
88 };
89 });
90
91 /**
92 * Groups ToolkitVersionSpecs by version for efficient API batching.
93 *
94 * @example
95 * // Given specs with mixed versions
96 * // Returns: Map { "20250901_00" => ["gmail"], "latest" => ["slack", "github"] }
97 */
98 export const groupByVersion = (
99 specs: ReadonlyArray<ToolkitVersionSpec>
100 ): Map<string, Lowercase<string>[]> => {
101 const grouped = new Map<string, Lowercase<string>[]>();
102 for (const spec of specs) {
103 const existing = grouped.get(spec.toolkitVersion);
104 if (existing) {
105 existing.push(spec.toolkitSlug);
106 } else {
107 grouped.set(spec.toolkitVersion, [spec.toolkitSlug]);
108 }
109 }
110 return grouped;
111 };
112
113 /**
114 * Builds a ToolkitVersionOverrides map from version specs, excluding 'latest' versions.
115 * This is the single source of truth for building version maps from specs.
116 *
117 * @example
118 * // Given specs: [{ toolkitSlug: 'gmail', toolkitVersion: '20250901_00' }, { toolkitSlug: 'slack', toolkitVersion: 'latest' }]
119 * // Returns: Map { "gmail" => "20250901_00" }
120 */
121 export const buildVersionMapFromSpecs = (
122 specs: ReadonlyArray<ToolkitVersionSpec>
123 ): ToolkitVersionOverrides => {
124 const versionMap = new Map<Lowercase<string>, string>();
125 for (const spec of specs) {
126 if (spec.toolkitVersion !== 'latest') {
127 versionMap.set(spec.toolkitSlug, spec.toolkitVersion);
128 }
129 }
130 return versionMap;
131 };