File size: 5,837 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | import Fuse from 'fuse.js'
import { basename } from 'path'
import type { SuggestionItem } from 'src/components/PromptInput/PromptInputFooterSuggestions.js'
import { generateFileSuggestions } from 'src/hooks/fileSuggestions.js'
import type { ServerResource } from 'src/services/mcp/types.js'
import { getAgentColor } from 'src/tools/AgentTool/agentColorManager.js'
import type { AgentDefinition } from 'src/tools/AgentTool/loadAgentsDir.js'
import { truncateToWidth } from 'src/utils/format.js'
import { logError } from 'src/utils/log.js'
import type { Theme } from 'src/utils/theme.js'
type FileSuggestionSource = {
type: 'file'
displayText: string
description?: string
path: string
filename: string
score?: number
}
type McpResourceSuggestionSource = {
type: 'mcp_resource'
displayText: string
description: string
server: string
uri: string
name: string
}
type AgentSuggestionSource = {
type: 'agent'
displayText: string
description: string
agentType: string
color?: keyof Theme
}
type SuggestionSource =
| FileSuggestionSource
| McpResourceSuggestionSource
| AgentSuggestionSource
/**
* Creates a unified suggestion item from a source
*/
function createSuggestionFromSource(source: SuggestionSource): SuggestionItem {
switch (source.type) {
case 'file':
return {
id: `file-${source.path}`,
displayText: source.displayText,
description: source.description,
}
case 'mcp_resource':
return {
id: `mcp-resource-${source.server}__${source.uri}`,
displayText: source.displayText,
description: source.description,
}
case 'agent':
return {
id: `agent-${source.agentType}`,
displayText: source.displayText,
description: source.description,
color: source.color,
}
}
}
const MAX_UNIFIED_SUGGESTIONS = 15
const DESCRIPTION_MAX_LENGTH = 60
function truncateDescription(description: string): string {
return truncateToWidth(description, DESCRIPTION_MAX_LENGTH)
}
function generateAgentSuggestions(
agents: AgentDefinition[],
query: string,
showOnEmpty = false,
): AgentSuggestionSource[] {
if (!query && !showOnEmpty) {
return []
}
try {
const agentSources: AgentSuggestionSource[] = agents.map(agent => ({
type: 'agent' as const,
displayText: `${agent.agentType} (agent)`,
description: truncateDescription(agent.whenToUse),
agentType: agent.agentType,
color: getAgentColor(agent.agentType),
}))
if (!query) {
return agentSources
}
const queryLower = query.toLowerCase()
return agentSources.filter(
agent =>
agent.agentType.toLowerCase().includes(queryLower) ||
agent.displayText.toLowerCase().includes(queryLower),
)
} catch (error) {
logError(error as Error)
return []
}
}
export async function generateUnifiedSuggestions(
query: string,
mcpResources: Record<string, ServerResource[]>,
agents: AgentDefinition[],
showOnEmpty = false,
): Promise<SuggestionItem[]> {
if (!query && !showOnEmpty) {
return []
}
const [fileSuggestions, agentSources] = await Promise.all([
generateFileSuggestions(query, showOnEmpty),
Promise.resolve(generateAgentSuggestions(agents, query, showOnEmpty)),
])
const fileSources: FileSuggestionSource[] = fileSuggestions.map(
suggestion => ({
type: 'file' as const,
displayText: suggestion.displayText,
description: suggestion.description,
path: suggestion.displayText, // Use displayText as path for files
filename: basename(suggestion.displayText),
score: (suggestion.metadata as { score?: number } | undefined)?.score,
}),
)
const mcpSources: McpResourceSuggestionSource[] = Object.values(mcpResources)
.flat()
.map(resource => ({
type: 'mcp_resource' as const,
displayText: `${resource.server}:${resource.uri}`,
description: truncateDescription(
resource.description || resource.name || resource.uri,
),
server: resource.server,
uri: resource.uri,
name: resource.name || resource.uri,
}))
if (!query) {
const allSources = [...fileSources, ...mcpSources, ...agentSources]
return allSources
.slice(0, MAX_UNIFIED_SUGGESTIONS)
.map(createSuggestionFromSource)
}
const nonFileSources: SuggestionSource[] = [...mcpSources, ...agentSources]
// Score non-file sources with Fuse.js
// File sources are already scored by Rust/nucleo
type ScoredSource = { source: SuggestionSource; score: number }
const scoredResults: ScoredSource[] = []
// Add file sources with their nucleo scores (already 0-1, lower is better)
for (const fileSource of fileSources) {
scoredResults.push({
source: fileSource,
score: fileSource.score ?? 0.5, // Default to middle score if missing
})
}
// Score non-file sources with Fuse.js and add them
if (nonFileSources.length > 0) {
const fuse = new Fuse(nonFileSources, {
includeScore: true,
threshold: 0.6, // Allow more matches through, we'll sort by score
keys: [
{ name: 'displayText', weight: 2 },
{ name: 'name', weight: 3 },
{ name: 'server', weight: 1 },
{ name: 'description', weight: 1 },
{ name: 'agentType', weight: 3 },
],
})
const fuseResults = fuse.search(query, { limit: MAX_UNIFIED_SUGGESTIONS })
for (const result of fuseResults) {
scoredResults.push({
source: result.item,
score: result.score ?? 0.5,
})
}
}
// Sort all results by score (lower is better) and return top results
scoredResults.sort((a, b) => a.score - b.score)
return scoredResults
.slice(0, MAX_UNIFIED_SUGGESTIONS)
.map(r => r.source)
.map(createSuggestionFromSource)
}
|