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, 03:37 UTC
| 1 | export interface DescriptionPart { |
| 2 | text: string; |
| 3 | href?: string; |
| 4 | } |
| 5 | |
| 6 | /** Linkify web addresses in plain-text toolkit descriptions without interpreting HTML. */ |
| 7 | export function descriptionLinks(description: string): DescriptionPart[] { |
| 8 | const parts: DescriptionPart[] = []; |
| 9 | const addresses = /(?<![\w@./-])(?:https?:\/\/[^\s<>"']+|(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,63}(?::\d+)?(?:[/?#][^\s<>"']*)?)/gi; |
| 10 | let cursor = 0; |
| 11 | |
| 12 | for (const match of description.matchAll(addresses)) { |
| 13 | let text = match[0].replace(/[.,!?:;]+$/, ''); |
| 14 | // Keep balanced parentheses in URL paths, but leave prose delimiters outside links. |
| 15 | while (text.endsWith(')') && text.split(')').length > text.split('(').length) { |
| 16 | text = text.slice(0, -1); |
| 17 | } |
| 18 | text = text.replace(/[\]}]+$/, ''); |
| 19 | const explicitUrl = /^https?:\/\//i.test(text); |
| 20 | // Bare .js names refer to JavaScript libraries (for example, Chart.js and Node.js). |
| 21 | if (!explicitUrl && /^[^/?#:]+\.js(?:[/?#:]|$)/i.test(text)) continue; |
| 22 | const href = explicitUrl ? text : `https://${text}`; |
| 23 | try { |
| 24 | new URL(href); |
| 25 | } catch { |
| 26 | continue; |
| 27 | } |
| 28 | |
| 29 | if (match.index > cursor) parts.push({ text: description.slice(cursor, match.index) }); |
| 30 | parts.push({ text, href }); |
| 31 | cursor = match.index + text.length; |
| 32 | } |
| 33 | |
| 34 | if (cursor < description.length) parts.push({ text: description.slice(cursor) }); |
| 35 | return parts; |
| 36 | } |