Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { configure } from 'safe-stable-stringify'; |
| 2 | |
| 3 | const HTTPS_PREFIX = 'https://'; |
| 4 | const CONNECT_LINK_PREFIX = 'https://connect.composio.dev/'; |
| 5 | const COMPOSIO_MARKER = 'composio'; |
| 6 | const LINK_PATH_MARKER = '/link/'; |
| 7 | const URL_BOUNDARIES = new Set(['<', '>', ')', '"', "'"]); |
| 8 | const TRAILING_PUNCTUATION = new Set(['.', ',', ';', ':', '!', '?']); |
| 9 | const stringify = configure({ deterministic: false }); |
| 10 | |
| 11 | const stringifyUnknown = (value: unknown): string => { |
| 12 | if (typeof value === 'string') return value; |
| 13 | |
| 14 | try { |
| 15 | const serialized = stringify(value); |
| 16 | if (serialized === undefined) return ''; |
| 17 | return serialized; |
| 18 | } catch { |
| 19 | return ''; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | const isUrlBoundary = (character: string): boolean => |
| 24 | URL_BOUNDARIES.has(character) || character.trim().length === 0; |
| 25 | |
| 26 | const trimTrailingPunctuation = (url: string): string => { |
| 27 | let end = url.length; |
| 28 | |
| 29 | while (end > 0 && TRAILING_PUNCTUATION.has(url[end - 1]!)) { |
| 30 | end -= 1; |
| 31 | } |
| 32 | |
| 33 | return url.slice(0, end); |
| 34 | }; |
| 35 | |
| 36 | const classifyToken = ( |
| 37 | token: string, |
| 38 | connectLinks: Set<string>, |
| 39 | genericLinks: Set<string> |
| 40 | ): void => { |
| 41 | const normalizedToken = token.toLowerCase(); |
| 42 | const httpsStart = normalizedToken.indexOf(HTTPS_PREFIX); |
| 43 | if (httpsStart === -1) return; |
| 44 | |
| 45 | const connectStart = normalizedToken.indexOf(CONNECT_LINK_PREFIX, httpsStart); |
| 46 | if (connectStart !== -1 && connectStart + CONNECT_LINK_PREFIX.length < token.length) { |
| 47 | connectLinks.add(trimTrailingPunctuation(token.slice(connectStart))); |
| 48 | } |
| 49 | |
| 50 | const composioStart = normalizedToken.indexOf(COMPOSIO_MARKER, httpsStart + HTTPS_PREFIX.length); |
| 51 | if (composioStart === -1) return; |
| 52 | |
| 53 | const linkPathStart = normalizedToken.indexOf( |
| 54 | LINK_PATH_MARKER, |
| 55 | composioStart + COMPOSIO_MARKER.length |
| 56 | ); |
| 57 | if (linkPathStart === -1 || linkPathStart + LINK_PATH_MARKER.length >= token.length) return; |
| 58 | |
| 59 | genericLinks.add(trimTrailingPunctuation(token.slice(httpsStart))); |
| 60 | }; |
| 61 | |
| 62 | export const extractComposioConnectLinks = (value: unknown): string[] => { |
| 63 | const text = stringifyUnknown(value); |
| 64 | const connectLinks = new Set<string>(); |
| 65 | const genericLinks = new Set<string>(); |
| 66 | let tokenStart = 0; |
| 67 | |
| 68 | for (let index = 0; index <= text.length; index += 1) { |
| 69 | const character = text[index]; |
| 70 | if (character !== undefined && !isUrlBoundary(character)) continue; |
| 71 | |
| 72 | if (index > tokenStart) { |
| 73 | classifyToken(text.slice(tokenStart, index), connectLinks, genericLinks); |
| 74 | } |
| 75 | |
| 76 | tokenStart = index + 1; |
| 77 | } |
| 78 | |
| 79 | return Array.from(new Set([...connectLinks, ...genericLinks])); |
| 80 | }; |