message.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { z } from "zod"
  2. import { kiloCodeMetaDataSchema } from "./kilocode/kilocode.js"
  3. /**
  4. * ClineAsk
  5. */
  6. /**
  7. * Array of possible ask types that the LLM can use to request user interaction or approval.
  8. * These represent different scenarios where the assistant needs user input to proceed.
  9. *
  10. * @constant
  11. * @readonly
  12. *
  13. * Ask type descriptions:
  14. * - `followup`: LLM asks a clarifying question to gather more information needed to complete the task
  15. * - `command`: Permission to execute a terminal/shell command
  16. * - `command_output`: Permission to read the output from a previously executed command
  17. * - `completion_result`: Task has been completed, awaiting user feedback or a new task
  18. * - `tool`: Permission to use a tool for file operations (read, write, search, etc.)
  19. * - `api_req_failed`: API request failed, asking user whether to retry
  20. * - `resume_task`: Confirmation needed to resume a previously paused task
  21. * - `resume_completed_task`: Confirmation needed to resume a task that was already marked as completed
  22. * - `mistake_limit_reached`: Too many errors encountered, needs user guidance on how to proceed
  23. * - `browser_action_launch`: Permission to open or interact with a browser
  24. * - `use_mcp_server`: Permission to use Model Context Protocol (MCP) server functionality
  25. * - `auto_approval_max_req_reached`: Auto-approval limit has been reached, manual approval required
  26. */
  27. export const clineAsks = [
  28. "followup",
  29. "command",
  30. "command_output",
  31. "completion_result",
  32. "tool",
  33. "api_req_failed",
  34. "resume_task",
  35. "resume_completed_task",
  36. "mistake_limit_reached",
  37. "browser_action_launch",
  38. "use_mcp_server",
  39. "auto_approval_max_req_reached",
  40. // kilocode_change start
  41. "payment_required_prompt", // Added for the low credits dialog
  42. "invalid_model",
  43. "report_bug",
  44. "condense",
  45. "checkpoint_restore", // Added for checkpoint restore approval
  46. // kilocode_change end
  47. ] as const
  48. export const clineAskSchema = z.enum(clineAsks)
  49. export type ClineAsk = z.infer<typeof clineAskSchema>
  50. /**
  51. * IdleAsk
  52. *
  53. * Asks that put the task into an "idle" state.
  54. */
  55. export const idleAsks = [
  56. // kilocode_change start
  57. "payment_required_prompt",
  58. "invalid_model",
  59. // kilocode_change end
  60. "completion_result",
  61. "api_req_failed",
  62. "resume_completed_task",
  63. "mistake_limit_reached",
  64. "auto_approval_max_req_reached",
  65. ] as const satisfies readonly ClineAsk[]
  66. export type IdleAsk = (typeof idleAsks)[number]
  67. export function isIdleAsk(ask: ClineAsk): ask is IdleAsk {
  68. return (idleAsks as readonly ClineAsk[]).includes(ask)
  69. }
  70. /**
  71. * ResumableAsk
  72. *
  73. * Asks that put the task into an "resumable" state.
  74. */
  75. export const resumableAsks = ["resume_task"] as const satisfies readonly ClineAsk[]
  76. export type ResumableAsk = (typeof resumableAsks)[number]
  77. export function isResumableAsk(ask: ClineAsk): ask is ResumableAsk {
  78. return (resumableAsks as readonly ClineAsk[]).includes(ask)
  79. }
  80. /**
  81. * InteractiveAsk
  82. *
  83. * Asks that put the task into an "user interaction required" state.
  84. */
  85. export const interactiveAsks = [
  86. // kilocode_change start
  87. "report_bug",
  88. "condense",
  89. "checkpoint_restore",
  90. // kilocode_change end
  91. "followup",
  92. "command",
  93. "tool",
  94. "browser_action_launch",
  95. "use_mcp_server",
  96. ] as const satisfies readonly ClineAsk[]
  97. export type InteractiveAsk = (typeof interactiveAsks)[number]
  98. export function isInteractiveAsk(ask: ClineAsk): ask is InteractiveAsk {
  99. return (interactiveAsks as readonly ClineAsk[]).includes(ask)
  100. }
  101. /**
  102. * NonBlockingAsk
  103. *
  104. * Asks that are not associated with an actual approval, and are only used
  105. * to update chat messages.
  106. */
  107. export const nonBlockingAsks = ["command_output"] as const satisfies readonly ClineAsk[]
  108. export type NonBlockingAsk = (typeof nonBlockingAsks)[number]
  109. export function isNonBlockingAsk(ask: ClineAsk): ask is NonBlockingAsk {
  110. return (nonBlockingAsks as readonly ClineAsk[]).includes(ask)
  111. }
  112. /**
  113. * ClineSay
  114. */
  115. /**
  116. * Array of possible say types that represent different kinds of messages the assistant can send.
  117. * These are used to categorize and handle various types of communication from the LLM to the user.
  118. *
  119. * @constant
  120. * @readonly
  121. *
  122. * Say type descriptions:
  123. * - `error`: General error message
  124. * - `api_req_started`: Indicates an API request has been initiated
  125. * - `api_req_finished`: Indicates an API request has completed successfully
  126. * - `api_req_retried`: Indicates an API request is being retried after a failure
  127. * - `api_req_retry_delayed`: Indicates an API request retry has been delayed
  128. * - `api_req_rate_limit_wait`: Indicates a configured rate-limit wait (not an error)
  129. * - `api_req_deleted`: Indicates an API request has been deleted/cancelled
  130. * - `text`: General text message or assistant response
  131. * - `reasoning`: Assistant's reasoning or thought process (often hidden from user)
  132. * - `completion_result`: Final result of task completion
  133. * - `user_feedback`: Message containing user feedback
  134. * - `user_feedback_diff`: Diff-formatted feedback from user showing requested changes
  135. * - `command_output`: Output from an executed command
  136. * - `shell_integration_warning`: Warning about shell integration issues or limitations
  137. * - `browser_action`: Action performed in the browser
  138. * - `browser_action_result`: Result of a browser action
  139. * - `mcp_server_request_started`: MCP server request has been initiated
  140. * - `mcp_server_response`: Response received from MCP server
  141. * - `subtask_result`: Result of a completed subtask
  142. * - `checkpoint_saved`: Indicates a checkpoint has been saved
  143. * - `rooignore_error`: Error related to .rooignore file processing
  144. * - `diff_error`: Error occurred while applying a diff/patch
  145. * - `condense_context`: Context condensation/summarization has started
  146. * - `condense_context_error`: Error occurred during context condensation
  147. * - `codebase_search_result`: Results from searching the codebase
  148. */
  149. export const clineSays = [
  150. "error",
  151. "api_req_started",
  152. "api_req_finished",
  153. "api_req_retried",
  154. "api_req_retry_delayed",
  155. "api_req_rate_limit_wait",
  156. "api_req_deleted",
  157. "text",
  158. "image",
  159. "reasoning",
  160. "completion_result",
  161. "user_feedback",
  162. "user_feedback_diff",
  163. "command_output",
  164. "shell_integration_warning",
  165. "browser_action",
  166. "browser_action_result",
  167. "browser_session_status",
  168. "mcp_server_request_started",
  169. "mcp_server_response",
  170. "subtask_result",
  171. "checkpoint_saved",
  172. "rooignore_error",
  173. "diff_error",
  174. "condense_context",
  175. "condense_context_error",
  176. "sliding_window_truncation",
  177. "codebase_search_result",
  178. "user_edit_todos",
  179. ] as const
  180. export const clineSaySchema = z.enum(clineSays)
  181. export type ClineSay = z.infer<typeof clineSaySchema>
  182. /**
  183. * ToolProgressStatus
  184. */
  185. export const toolProgressStatusSchema = z.object({
  186. icon: z.string().optional(),
  187. text: z.string().optional(),
  188. })
  189. export type ToolProgressStatus = z.infer<typeof toolProgressStatusSchema>
  190. /**
  191. * ContextCondense
  192. *
  193. * Data associated with a successful context condensation event.
  194. * This is attached to messages with `say: "condense_context"` when
  195. * the condensation operation completes successfully.
  196. *
  197. * @property cost - The API cost incurred for the condensation operation
  198. * @property prevContextTokens - Token count before condensation
  199. * @property newContextTokens - Token count after condensation
  200. * @property summary - The condensed summary that replaced the original context
  201. * @property condenseId - Optional unique identifier for this condensation operation
  202. */
  203. export const contextCondenseSchema = z.object({
  204. cost: z.number(),
  205. prevContextTokens: z.number(),
  206. newContextTokens: z.number(),
  207. summary: z.string(),
  208. condenseId: z.string().optional(),
  209. })
  210. export type ContextCondense = z.infer<typeof contextCondenseSchema>
  211. /**
  212. * ContextTruncation
  213. *
  214. * Data associated with a sliding window truncation event.
  215. * This is attached to messages with `say: "sliding_window_truncation"` when
  216. * messages are removed from the conversation history to stay within token limits.
  217. *
  218. * Unlike condensation, truncation simply removes older messages without
  219. * summarizing them. This is a faster but less context-preserving approach.
  220. *
  221. * @property truncationId - Unique identifier for this truncation operation
  222. * @property messagesRemoved - Number of conversation messages that were removed
  223. * @property prevContextTokens - Token count before truncation occurred
  224. * @property newContextTokens - Token count after truncation occurred
  225. */
  226. export const contextTruncationSchema = z.object({
  227. truncationId: z.string(),
  228. messagesRemoved: z.number(),
  229. prevContextTokens: z.number(),
  230. newContextTokens: z.number(),
  231. })
  232. export type ContextTruncation = z.infer<typeof contextTruncationSchema>
  233. /**
  234. * ClineMessage
  235. *
  236. * The main message type used for communication between the extension and webview.
  237. * Messages can either be "ask" (requiring user response) or "say" (informational).
  238. *
  239. * Context Management Fields:
  240. * - `contextCondense`: Present when `say: "condense_context"` and condensation succeeded
  241. * - `contextTruncation`: Present when `say: "sliding_window_truncation"` and truncation occurred
  242. *
  243. * Note: These fields are mutually exclusive - a message will have at most one of them.
  244. */
  245. export const clineMessageSchema = z.object({
  246. ts: z.number(),
  247. type: z.union([z.literal("ask"), z.literal("say")]),
  248. ask: clineAskSchema.optional(),
  249. say: clineSaySchema.optional(),
  250. text: z.string().optional(),
  251. images: z.array(z.string()).optional(),
  252. partial: z.boolean().optional(),
  253. reasoning: z.string().optional(),
  254. conversationHistoryIndex: z.number().optional(),
  255. checkpoint: z.record(z.string(), z.unknown()).optional(),
  256. progressStatus: toolProgressStatusSchema.optional(),
  257. /**
  258. * Data for successful context condensation.
  259. * Present when `say: "condense_context"` and `partial: false`.
  260. */
  261. contextCondense: contextCondenseSchema.optional(),
  262. /**
  263. * Data for sliding window truncation.
  264. * Present when `say: "sliding_window_truncation"`.
  265. */
  266. contextTruncation: contextTruncationSchema.optional(),
  267. isProtected: z.boolean().optional(),
  268. apiProtocol: z.union([z.literal("openai"), z.literal("anthropic")]).optional(),
  269. isAnswered: z.boolean().optional(),
  270. // kilocode_change start
  271. metadata: z
  272. .object({
  273. kiloCode: kiloCodeMetaDataSchema.optional(),
  274. })
  275. .optional(),
  276. // kilocode_change end
  277. })
  278. export type ClineMessage = z.infer<typeof clineMessageSchema>
  279. /**
  280. * TokenUsage
  281. */
  282. export const tokenUsageSchema = z.object({
  283. totalTokensIn: z.number(),
  284. totalTokensOut: z.number(),
  285. totalCacheWrites: z.number().optional(),
  286. totalCacheReads: z.number().optional(),
  287. totalCost: z.number(),
  288. contextTokens: z.number(),
  289. })
  290. export type TokenUsage = z.infer<typeof tokenUsageSchema>
  291. /**
  292. * QueuedMessage
  293. */
  294. export const queuedMessageSchema = z.object({
  295. timestamp: z.number(),
  296. id: z.string(),
  297. text: z.string(),
  298. images: z.array(z.string()).optional(),
  299. })
  300. export type QueuedMessage = z.infer<typeof queuedMessageSchema>