MCPcopy Index your code
hub / github.com/simstudioai/sim / execute

Function execute

apps/sim/lib/copilot/tools/server/docs/search-documentation.ts:19–58  ·  view source on GitHub ↗
(params: DocsSearchParams)

Source from the content-addressed store, hash-verified

17export const searchDocumentationServerTool: BaseServerTool<DocsSearchParams, any> = {
18 name: SearchDocumentation.id,
19 async execute(params: DocsSearchParams): Promise<any> {
20 const logger = createLogger('SearchDocumentationServerTool')
21 const { query, topK = 10, threshold } = params
22 if (!query || typeof query !== 'string') throw new Error('query is required')
23
24 logger.info('Executing docs search', { query, topK })
25
26 const similarityThreshold = threshold ?? DEFAULT_DOCS_SIMILARITY_THRESHOLD
27
28 const { embedding: queryEmbedding } = await generateSearchEmbedding(query)
29 if (!queryEmbedding || queryEmbedding.length === 0) {
30 return { results: [], query, totalResults: 0 }
31 }
32
33 const results = await db
34 .select({
35 chunkId: docsEmbeddings.chunkId,
36 chunkText: docsEmbeddings.chunkText,
37 sourceDocument: docsEmbeddings.sourceDocument,
38 sourceLink: docsEmbeddings.sourceLink,
39 headerText: docsEmbeddings.headerText,
40 headerLevel: docsEmbeddings.headerLevel,
41 similarity: sql<number>`1 - (${docsEmbeddings.embedding} <=> ${JSON.stringify(queryEmbedding)}::vector)`,
42 })
43 .from(docsEmbeddings)
44 .orderBy(sql`${docsEmbeddings.embedding} <=> ${JSON.stringify(queryEmbedding)}::vector`)
45 .limit(topK)
46
47 const filteredResults = results.filter((r) => r.similarity >= similarityThreshold)
48 const documentationResults = filteredResults.map((r, idx) => ({
49 id: idx + 1,
50 title: String(r.headerText || 'Untitled Section'),
51 url: String(r.sourceLink || '#'),
52 content: String(r.chunkText || ''),
53 similarity: r.similarity,
54 }))
55
56 logger.info('Docs search complete', { count: documentationResults.length })
57 return { results: documentationResults, query, totalResults: documentationResults.length }
58 },
59}

Callers

nothing calls this directly

Calls 3

createLoggerFunction · 0.90
generateSearchEmbeddingFunction · 0.90
infoMethod · 0.80

Tested by

no test coverage detected