mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 22:35:51 +00:00
* feat: 第一版大重构 * fix: 修复类型问题 * chore: 更新版本到 1.3.2 * Add brave as alternative WebSearchTool * fix: 修正顺序 * fix: 修复对穷鬼模式的 auto dream 和 session memory 越过 * feat: 穷鬼模式去除 session-summary * feat: 创建 builtin-tools 包,搬运所有工具实现 将 src/tools/ 下的全部 60 个工具目录迁移至 packages/builtin-tools/src/tools/, 内部导入路径已更新为 src/ alias 模式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 更新 src/ 中所有工具引用至 builtin-tools 包,删除 src/tools/ - src/tools.ts 及 178 个 src/ 文件的 import 路径从 ./tools/ 改为 builtin-tools/tools/ - 删除 src/tools/ 整个目录(已迁移至 packages/builtin-tools/) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: 添加 builtin-tools 路径别名至 tsconfig,更新 bun.lock - tsconfig.json 新增 builtin-tools/* 和 builtin-tools 路径映射 - 新增 packages/builtin-tools/src 至 include Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 为 builtin-tools、mcp-client、agent-tools 添加 @claude-code-best 作用域前缀 所有包名及 import 路径统一添加 @claude-code-best/ 前缀: - builtin-tools → @claude-code-best/builtin-tools - mcp-client → @claude-code-best/mcp-client - agent-tools → @claude-code-best/agent-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复 node 环境没有 bun 的问题 --------- Co-authored-by: Eric-Guo <eric.guocz@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
203 lines
5.8 KiB
TypeScript
203 lines
5.8 KiB
TypeScript
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 '@claude-code-best/builtin-tools/tools/AgentTool/agentColorManager.js'
|
|
import type { AgentDefinition } from '@claude-code-best/builtin-tools/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)
|
|
}
|