-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(copilot): add context7 #2779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
apps/sim/lib/copilot/tools/client/other/search-library-docs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { BookOpen, Loader2, MinusCircle, XCircle } from 'lucide-react' | ||
| import { | ||
| BaseClientTool, | ||
| type BaseClientToolMetadata, | ||
| ClientToolCallState, | ||
| } from '@/lib/copilot/tools/client/base-tool' | ||
| import { ExecuteResponseSuccessSchema } from '@/lib/copilot/tools/shared/schemas' | ||
|
|
||
| interface SearchLibraryDocsArgs { | ||
| library_name: string | ||
| query: string | ||
| version?: string | ||
| } | ||
|
|
||
| export class SearchLibraryDocsClientTool extends BaseClientTool { | ||
| static readonly id = 'search_library_docs' | ||
|
|
||
| constructor(toolCallId: string) { | ||
| super(toolCallId, SearchLibraryDocsClientTool.id, SearchLibraryDocsClientTool.metadata) | ||
| } | ||
|
|
||
| static readonly metadata: BaseClientToolMetadata = { | ||
| displayNames: { | ||
| [ClientToolCallState.generating]: { text: 'Reading docs', icon: Loader2 }, | ||
| [ClientToolCallState.pending]: { text: 'Reading docs', icon: Loader2 }, | ||
| [ClientToolCallState.executing]: { text: 'Reading docs', icon: Loader2 }, | ||
| [ClientToolCallState.success]: { text: 'Read docs', icon: BookOpen }, | ||
| [ClientToolCallState.error]: { text: 'Failed to read docs', icon: XCircle }, | ||
| [ClientToolCallState.aborted]: { text: 'Aborted reading docs', icon: XCircle }, | ||
| [ClientToolCallState.rejected]: { text: 'Skipped reading docs', icon: MinusCircle }, | ||
| }, | ||
| getDynamicText: (params, state) => { | ||
| const libraryName = params?.library_name | ||
| if (libraryName && typeof libraryName === 'string') { | ||
| switch (state) { | ||
| case ClientToolCallState.success: | ||
| return `Read ${libraryName} docs` | ||
| case ClientToolCallState.executing: | ||
| case ClientToolCallState.generating: | ||
| case ClientToolCallState.pending: | ||
| return `Reading ${libraryName} docs` | ||
| case ClientToolCallState.error: | ||
| return `Failed to read ${libraryName} docs` | ||
| case ClientToolCallState.aborted: | ||
| return `Aborted reading ${libraryName} docs` | ||
| case ClientToolCallState.rejected: | ||
| return `Skipped reading ${libraryName} docs` | ||
| } | ||
| } | ||
| return undefined | ||
| }, | ||
| } | ||
|
|
||
| async execute(args?: SearchLibraryDocsArgs): Promise<void> { | ||
| const logger = createLogger('SearchLibraryDocsClientTool') | ||
| try { | ||
| this.setState(ClientToolCallState.executing) | ||
| const res = await fetch('/api/copilot/execute-copilot-server-tool', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ toolName: 'search_library_docs', payload: args || {} }), | ||
| }) | ||
| if (!res.ok) { | ||
| const txt = await res.text().catch(() => '') | ||
| throw new Error(txt || `Server error (${res.status})`) | ||
| } | ||
| const json = await res.json() | ||
| const parsed = ExecuteResponseSuccessSchema.parse(json) | ||
| this.setState(ClientToolCallState.success) | ||
| await this.markToolComplete( | ||
| 200, | ||
| `Library documentation search complete for ${args?.library_name || 'unknown'}`, | ||
| parsed.result | ||
| ) | ||
| this.setState(ClientToolCallState.success) | ||
| } catch (e: any) { | ||
| logger.error('execute failed', { message: e?.message }) | ||
| this.setState(ClientToolCallState.error) | ||
| await this.markToolComplete(500, e?.message || 'Library documentation search failed') | ||
| } | ||
| } | ||
| } | ||
156 changes: 156 additions & 0 deletions
156
apps/sim/lib/copilot/tools/server/docs/search-library-docs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import type { BaseServerTool } from '@/lib/copilot/tools/server/base-tool' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { executeTool } from '@/tools' | ||
|
|
||
| interface SearchLibraryDocsParams { | ||
| library_name: string | ||
| query: string | ||
| version?: string | ||
| } | ||
|
|
||
| interface SearchLibraryDocsResult { | ||
| results: Array<{ | ||
| title: string | ||
| link: string | ||
| snippet: string | ||
| position?: number | ||
| }> | ||
| query: string | ||
| library: string | ||
| version?: string | ||
| totalResults: number | ||
| } | ||
|
|
||
| export const searchLibraryDocsServerTool: BaseServerTool< | ||
| SearchLibraryDocsParams, | ||
| SearchLibraryDocsResult | ||
| > = { | ||
| name: 'search_library_docs', | ||
| async execute(params: SearchLibraryDocsParams): Promise<SearchLibraryDocsResult> { | ||
| const logger = createLogger('SearchLibraryDocsServerTool') | ||
| const { library_name, query, version } = params | ||
|
|
||
| if (!library_name || typeof library_name !== 'string') { | ||
| throw new Error('library_name is required') | ||
| } | ||
| if (!query || typeof query !== 'string') { | ||
| throw new Error('query is required') | ||
| } | ||
|
|
||
| // Build a search query that targets the library's documentation | ||
| const searchQuery = version | ||
| ? `${library_name} ${version} documentation ${query}` | ||
| : `${library_name} documentation ${query}` | ||
|
|
||
| logger.info('Searching library documentation', { | ||
| library: library_name, | ||
| query, | ||
| version, | ||
| fullSearchQuery: searchQuery, | ||
| }) | ||
|
|
||
| // Check which API keys are available | ||
| const hasExaApiKey = Boolean(env.EXA_API_KEY && String(env.EXA_API_KEY).length > 0) | ||
| const hasSerperApiKey = Boolean(env.SERPER_API_KEY && String(env.SERPER_API_KEY).length > 0) | ||
|
|
||
| // Try Exa first if available (better for documentation searches) | ||
| if (hasExaApiKey) { | ||
| try { | ||
| logger.debug('Attempting exa_search for library docs', { library: library_name }) | ||
| const exaResult = await executeTool('exa_search', { | ||
| query: searchQuery, | ||
| numResults: 10, | ||
| type: 'auto', | ||
| apiKey: env.EXA_API_KEY || '', | ||
| }) | ||
|
|
||
| const exaResults = (exaResult as any)?.output?.results || [] | ||
| const count = Array.isArray(exaResults) ? exaResults.length : 0 | ||
|
|
||
| logger.info('exa_search for library docs completed', { | ||
| success: exaResult.success, | ||
| resultsCount: count, | ||
| library: library_name, | ||
| }) | ||
|
|
||
| if (exaResult.success && count > 0) { | ||
| const transformedResults = exaResults.map((result: any, idx: number) => ({ | ||
| title: result.title || '', | ||
| link: result.url || '', | ||
| snippet: result.text || result.summary || '', | ||
| position: idx + 1, | ||
| })) | ||
|
|
||
| return { | ||
| results: transformedResults, | ||
| query, | ||
| library: library_name, | ||
| version, | ||
| totalResults: count, | ||
| } | ||
| } | ||
|
|
||
| logger.warn('exa_search returned no results for library docs, falling back to Serper', { | ||
| library: library_name, | ||
| }) | ||
| } catch (exaError: any) { | ||
| logger.warn('exa_search failed for library docs, falling back to Serper', { | ||
| error: exaError?.message, | ||
| library: library_name, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Fall back to Serper if Exa failed or wasn't available | ||
| if (!hasSerperApiKey) { | ||
| throw new Error('No search API keys available (EXA_API_KEY or SERPER_API_KEY required)') | ||
| } | ||
|
|
||
| try { | ||
| logger.debug('Calling serper_search for library docs', { library: library_name }) | ||
| const result = await executeTool('serper_search', { | ||
| query: searchQuery, | ||
| num: 10, | ||
| type: 'search', | ||
| apiKey: env.SERPER_API_KEY || '', | ||
| }) | ||
|
|
||
| const results = (result as any)?.output?.searchResults || [] | ||
| const count = Array.isArray(results) ? results.length : 0 | ||
|
|
||
| logger.info('serper_search for library docs completed', { | ||
| success: result.success, | ||
| resultsCount: count, | ||
| library: library_name, | ||
| }) | ||
|
|
||
| if (!result.success) { | ||
| logger.error('serper_search failed for library docs', { error: (result as any)?.error }) | ||
| throw new Error((result as any)?.error || 'Library documentation search failed') | ||
| } | ||
|
|
||
| // Transform serper results to match expected format | ||
| const transformedResults = results.map((result: any, idx: number) => ({ | ||
| title: result.title || '', | ||
| link: result.link || '', | ||
| snippet: result.snippet || '', | ||
| position: idx + 1, | ||
| })) | ||
|
|
||
| return { | ||
| results: transformedResults, | ||
| query, | ||
| library: library_name, | ||
| version, | ||
| totalResults: count, | ||
| } | ||
| } catch (e: any) { | ||
| logger.error('search_library_docs execution error', { | ||
| message: e?.message, | ||
| library: library_name, | ||
| }) | ||
| throw e | ||
| } | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The state is set to
ClientToolCallState.successtwice (lines 70 and 76). While this follows the existing pattern in other similar tools likeSearchOnlineClientToolandSearchDocumentationClientTool, this redundancy could be removed for cleaner code.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI