tool.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { z } from "zod"
  2. /**
  3. * ToolGroup
  4. */
  5. export const toolGroups = ["read", "edit", "browser", "command", "mcp", "modes"] as const
  6. export const toolGroupsSchema = z.enum(toolGroups)
  7. export type ToolGroup = z.infer<typeof toolGroupsSchema>
  8. /**
  9. * ToolName
  10. */
  11. export const toolNames = [
  12. "execute_command",
  13. "read_file",
  14. "write_to_file",
  15. "apply_diff",
  16. "search_and_replace",
  17. "search_replace",
  18. "edit_file",
  19. "apply_patch",
  20. "search_files",
  21. "list_files",
  22. "browser_action",
  23. "use_mcp_tool",
  24. "access_mcp_resource",
  25. "ask_followup_question",
  26. "attempt_completion",
  27. "switch_mode",
  28. "new_task",
  29. "fetch_instructions",
  30. "codebase_search",
  31. // kilocode_change start
  32. "fast_edit_file",
  33. "new_rule",
  34. "report_bug",
  35. "condense",
  36. "delete_file",
  37. // kilocode_change end
  38. "update_todo_list",
  39. "run_slash_command",
  40. "generate_image",
  41. "custom_tool",
  42. ] as const
  43. export const toolNamesSchema = z.enum(toolNames)
  44. export type ToolName = z.infer<typeof toolNamesSchema>
  45. /**
  46. * ToolUsage
  47. */
  48. export const toolUsageSchema = z.record(
  49. toolNamesSchema,
  50. z.object({
  51. attempts: z.number(),
  52. failures: z.number(),
  53. }),
  54. )
  55. export type ToolUsage = z.infer<typeof toolUsageSchema>
  56. /**
  57. * Tool protocol constants
  58. */
  59. export const TOOL_PROTOCOL = {
  60. XML: "xml",
  61. NATIVE: "native",
  62. } as const
  63. /**
  64. * Tool protocol type for system prompt generation
  65. * Derived from TOOL_PROTOCOL constants to ensure type safety
  66. */
  67. export type ToolProtocol = (typeof TOOL_PROTOCOL)[keyof typeof TOOL_PROTOCOL]
  68. export const toolProtocolSchema = z.enum([TOOL_PROTOCOL.XML, TOOL_PROTOCOL.NATIVE]) // kilocode_change
  69. /**
  70. * Default model info properties for native tool support.
  71. * Used to merge with cached model info that may lack these fields.
  72. * Router providers (Requesty, Unbound, LiteLLM) assume all models support native tools.
  73. */
  74. export const NATIVE_TOOL_DEFAULTS = {
  75. supportsNativeTools: true,
  76. defaultToolProtocol: TOOL_PROTOCOL.NATIVE,
  77. } as const
  78. /**
  79. * Checks if the protocol is native (non-XML).
  80. *
  81. * @param protocol - The tool protocol to check
  82. * @returns True if protocol is native
  83. */
  84. export function isNativeProtocol(protocol: ToolProtocol): boolean {
  85. return protocol === TOOL_PROTOCOL.NATIVE
  86. }
  87. /**
  88. * Gets the effective protocol from settings or falls back to the default XML.
  89. * This function is safe to use in webview-accessible code as it doesn't depend on vscode module.
  90. *
  91. * @param toolProtocol - Optional tool protocol from settings
  92. * @returns The effective tool protocol (defaults to "xml")
  93. */
  94. export function getEffectiveProtocol(toolProtocol?: ToolProtocol): ToolProtocol {
  95. return toolProtocol || TOOL_PROTOCOL.XML
  96. }