index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import type { ToolName, ModeConfig } from "@roo-code/types"
  2. import { TOOL_GROUPS, ALWAYS_AVAILABLE_TOOLS, DiffStrategy } from "../../../shared/tools"
  3. import { McpHub } from "../../../services/mcp/McpHub"
  4. import { Mode, getModeConfig, isToolAllowedForMode, getGroupName } from "../../../shared/modes"
  5. import { ToolArgs } from "./types"
  6. import { getExecuteCommandDescription } from "./execute-command"
  7. import { getReadFileDescription } from "./read-file"
  8. import { getFetchInstructionsDescription } from "./fetch-instructions"
  9. import { getWriteToFileDescription } from "./write-to-file"
  10. import { getSearchFilesDescription } from "./search-files"
  11. import { getListFilesDescription } from "./list-files"
  12. import { getInsertContentDescription } from "./insert-content"
  13. import { getSearchAndReplaceDescription } from "./search-and-replace"
  14. import { getListCodeDefinitionNamesDescription } from "./list-code-definition-names"
  15. import { getBrowserActionDescription } from "./browser-action"
  16. import { getAskFollowupQuestionDescription } from "./ask-followup-question"
  17. import { getAttemptCompletionDescription } from "./attempt-completion"
  18. import { getUseMcpToolDescription } from "./use-mcp-tool"
  19. import { getAccessMcpResourceDescription } from "./access-mcp-resource"
  20. import { getSwitchModeDescription } from "./switch-mode"
  21. import { getNewTaskDescription } from "./new-task"
  22. import { getCodebaseSearchDescription } from "./codebase-search"
  23. import { CodeIndexManager } from "../../../services/code-index/manager"
  24. // Map of tool names to their description functions
  25. const toolDescriptionMap: Record<string, (args: ToolArgs) => string | undefined> = {
  26. execute_command: (args) => getExecuteCommandDescription(args),
  27. read_file: (args) => getReadFileDescription(args),
  28. fetch_instructions: () => getFetchInstructionsDescription(),
  29. write_to_file: (args) => getWriteToFileDescription(args),
  30. search_files: (args) => getSearchFilesDescription(args),
  31. list_files: (args) => getListFilesDescription(args),
  32. list_code_definition_names: (args) => getListCodeDefinitionNamesDescription(args),
  33. browser_action: (args) => getBrowserActionDescription(args),
  34. ask_followup_question: () => getAskFollowupQuestionDescription(),
  35. attempt_completion: (args) => getAttemptCompletionDescription(args),
  36. use_mcp_tool: (args) => getUseMcpToolDescription(args),
  37. access_mcp_resource: (args) => getAccessMcpResourceDescription(args),
  38. codebase_search: () => getCodebaseSearchDescription(),
  39. switch_mode: () => getSwitchModeDescription(),
  40. new_task: (args) => getNewTaskDescription(args),
  41. insert_content: (args) => getInsertContentDescription(args),
  42. search_and_replace: (args) => getSearchAndReplaceDescription(args),
  43. apply_diff: (args) =>
  44. args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "",
  45. }
  46. export function getToolDescriptionsForMode(
  47. mode: Mode,
  48. cwd: string,
  49. supportsComputerUse: boolean,
  50. codeIndexManager?: CodeIndexManager,
  51. diffStrategy?: DiffStrategy,
  52. browserViewportSize?: string,
  53. mcpHub?: McpHub,
  54. customModes?: ModeConfig[],
  55. experiments?: Record<string, boolean>,
  56. partialReadsEnabled?: boolean,
  57. settings?: Record<string, any>,
  58. ): string {
  59. const config = getModeConfig(mode, customModes)
  60. const args: ToolArgs = {
  61. cwd,
  62. supportsComputerUse,
  63. diffStrategy,
  64. browserViewportSize,
  65. mcpHub,
  66. partialReadsEnabled,
  67. settings,
  68. experiments,
  69. }
  70. const tools = new Set<string>()
  71. // Add tools from mode's groups
  72. config.groups.forEach((groupEntry) => {
  73. const groupName = getGroupName(groupEntry)
  74. const toolGroup = TOOL_GROUPS[groupName]
  75. if (toolGroup) {
  76. toolGroup.tools.forEach((tool) => {
  77. if (
  78. isToolAllowedForMode(
  79. tool as ToolName,
  80. mode,
  81. customModes ?? [],
  82. undefined,
  83. undefined,
  84. experiments ?? {},
  85. )
  86. ) {
  87. tools.add(tool)
  88. }
  89. })
  90. }
  91. })
  92. // Add always available tools
  93. ALWAYS_AVAILABLE_TOOLS.forEach((tool) => tools.add(tool))
  94. // Conditionally exclude codebase_search if feature is disabled or not configured
  95. if (
  96. !codeIndexManager ||
  97. !(codeIndexManager.isFeatureEnabled && codeIndexManager.isFeatureConfigured && codeIndexManager.isInitialized)
  98. ) {
  99. tools.delete("codebase_search")
  100. }
  101. // Map tool descriptions for allowed tools
  102. const descriptions = Array.from(tools).map((toolName) => {
  103. const descriptionFn = toolDescriptionMap[toolName]
  104. if (!descriptionFn) {
  105. return undefined
  106. }
  107. return descriptionFn({
  108. ...args,
  109. toolOptions: undefined, // No tool options in group-based approach
  110. })
  111. })
  112. return `# Tools\n\n${descriptions.filter(Boolean).join("\n\n")}`
  113. }
  114. // Export individual description functions for backward compatibility
  115. export {
  116. getExecuteCommandDescription,
  117. getReadFileDescription,
  118. getFetchInstructionsDescription,
  119. getWriteToFileDescription,
  120. getSearchFilesDescription,
  121. getListFilesDescription,
  122. getListCodeDefinitionNamesDescription,
  123. getBrowserActionDescription,
  124. getAskFollowupQuestionDescription,
  125. getAttemptCompletionDescription,
  126. getUseMcpToolDescription,
  127. getAccessMcpResourceDescription,
  128. getSwitchModeDescription,
  129. getInsertContentDescription,
  130. getSearchAndReplaceDescription,
  131. getCodebaseSearchDescription,
  132. }