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 | import type { UserContent } from 'ai'; |
| 2 | import { none } from 'eve/channels/auth'; |
| 3 | import { defaultEveAuth, eveChannel } from 'eve/channels/eve'; |
| 4 | import { buildEveSafetyContext, eveMessageToText } from '../lib/safety'; |
| 5 | import { searchDocs, shouldRunEagerDocsSearch, type SearchDocsResult } from '../lib/docs-search'; |
| 6 | |
| 7 | /** |
| 8 | * HTTP channel for the docs assistant. |
| 9 | * |
| 10 | * The docs are public and any visitor can open the chat, so the session routes |
| 11 | * are unauthenticated (`none()`). This intentionally exposes the agent endpoint |
| 12 | * publicly; before production we should add rate limiting and abuse protection |
| 13 | * (or gate it behind the site's own auth). |
| 14 | */ |
| 15 | |
| 16 | const EAGER_SEARCH_LIMIT = 3; |
| 17 | const EAGER_CONTENT_RESULTS = 2; |
| 18 | const EAGER_MAX_CONTENT_CHARS = 6000; |
| 19 | const EAGER_MAX_SECTIONS = 6; |
| 20 | const MAX_CONTEXT_SECTIONS = EAGER_MAX_SECTIONS; |
| 21 | |
| 22 | function shouldEagerSearch(text: string): boolean { |
| 23 | return shouldRunEagerDocsSearch(text); |
| 24 | } |
| 25 | |
| 26 | function formatSections(result: SearchDocsResult['results'][number]): string { |
| 27 | const sections = result.sections?.slice(0, MAX_CONTEXT_SECTIONS) ?? []; |
| 28 | if (sections.length === 0) return ''; |
| 29 | |
| 30 | return sections.map(section => `[${section.title}](${result.url}${section.anchor})`).join(', '); |
| 31 | } |
| 32 | |
| 33 | function formatEagerSearchContext(result: SearchDocsResult): string | undefined { |
| 34 | if (result.results.length === 0) return undefined; |
| 35 | |
| 36 | const docs = result.results |
| 37 | .map((page, index) => { |
| 38 | const sections = formatSections(page); |
| 39 | const content = page.content |
| 40 | ? `${page.content}${page.contentTruncated ? '\n\n…(content truncated)' : ''}` |
| 41 | : page.snippet; |
| 42 | |
| 43 | return [ |
| 44 | `### ${index + 1}. ${page.title}`, |
| 45 | `URL: ${page.url}`, |
| 46 | page.description ? `Description: ${page.description}` : undefined, |
| 47 | sections ? `Sections: ${sections}` : undefined, |
| 48 | 'Content:', |
| 49 | content, |
| 50 | ] |
| 51 | .filter(Boolean) |
| 52 | .join('\n'); |
| 53 | }) |
| 54 | .join('\n\n---\n\n'); |
| 55 | |
| 56 | return `Eager Composio docs search context for the user's latest message. |
| 57 | |
| 58 | Use this context when it answers the question. You may still call \`search_docs\` or \`read_doc\` if this context is weak, missing, ambiguous, or you need more detail. Cite only the included docs URLs/section anchors. |
| 59 | |
| 60 | <docs_search_context retrieval="${result.retrieval}"> |
| 61 | ${docs} |
| 62 | </docs_search_context>`; |
| 63 | } |
| 64 | |
| 65 | function buildEagerSearchContext(message: string | UserContent): string[] | undefined { |
| 66 | const text = eveMessageToText(message); |
| 67 | if (!shouldEagerSearch(text)) return undefined; |
| 68 | |
| 69 | try { |
| 70 | const result = searchDocs(text, { |
| 71 | limit: EAGER_SEARCH_LIMIT, |
| 72 | contentResultCount: EAGER_CONTENT_RESULTS, |
| 73 | maxContentChars: EAGER_MAX_CONTENT_CHARS, |
| 74 | maxSections: EAGER_MAX_SECTIONS, |
| 75 | invocation: 'eager_context', |
| 76 | }); |
| 77 | const context = formatEagerSearchContext(result); |
| 78 | return context ? [context] : undefined; |
| 79 | } catch (error) { |
| 80 | console.warn('[docs-agent:eager_search] failed', { |
| 81 | error: error instanceof Error ? error.message : String(error), |
| 82 | }); |
| 83 | return undefined; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | export default eveChannel({ |
| 88 | auth: [none()], |
| 89 | onMessage(ctx, message) { |
| 90 | const auth = defaultEveAuth(ctx); |
| 91 | const safety = buildEveSafetyContext(message); |
| 92 | const context = [safety.context, ...(buildEagerSearchContext(message) ?? [])]; |
| 93 | return { auth, context }; |
| 94 | }, |
| 95 | }); |