parseAssistantMessage.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { type ToolName, toolNames } from "@roo-code/types"
  2. import { TextContent, ToolUse, ToolParamName, toolParamNames } from "../../shared/tools"
  3. export type AssistantMessageContent = TextContent | ToolUse
  4. export function parseAssistantMessage(assistantMessage: string): AssistantMessageContent[] {
  5. let contentBlocks: AssistantMessageContent[] = []
  6. let currentTextContent: TextContent | undefined = undefined
  7. let currentTextContentStartIndex = 0
  8. let currentToolUse: ToolUse | undefined = undefined
  9. let currentToolUseStartIndex = 0
  10. let currentParamName: ToolParamName | undefined = undefined
  11. let currentParamValueStartIndex = 0
  12. let accumulator = ""
  13. for (let i = 0; i < assistantMessage.length; i++) {
  14. const char = assistantMessage[i]
  15. accumulator += char
  16. // There should not be a param without a tool use.
  17. if (currentToolUse && currentParamName) {
  18. const currentParamValue = accumulator.slice(currentParamValueStartIndex)
  19. const paramClosingTag = `</${currentParamName}>`
  20. if (currentParamValue.endsWith(paramClosingTag)) {
  21. // End of param value.
  22. // Don't trim content parameters to preserve newlines, but strip first and last newline only
  23. const paramValue = currentParamValue.slice(0, -paramClosingTag.length)
  24. currentToolUse.params[currentParamName] =
  25. currentParamName === "content"
  26. ? paramValue.replace(/^\n/, "").replace(/\n$/, "")
  27. : paramValue.trim()
  28. currentParamName = undefined
  29. continue
  30. } else {
  31. // Partial param value is accumulating.
  32. continue
  33. }
  34. }
  35. // No currentParamName.
  36. if (currentToolUse) {
  37. const currentToolValue = accumulator.slice(currentToolUseStartIndex)
  38. const toolUseClosingTag = `</${currentToolUse.name}>`
  39. if (currentToolValue.endsWith(toolUseClosingTag)) {
  40. // End of a tool use.
  41. currentToolUse.partial = false
  42. contentBlocks.push(currentToolUse)
  43. currentToolUse = undefined
  44. continue
  45. } else {
  46. const possibleParamOpeningTags = toolParamNames.map((name) => `<${name}>`)
  47. for (const paramOpeningTag of possibleParamOpeningTags) {
  48. if (accumulator.endsWith(paramOpeningTag)) {
  49. // Start of a new parameter.
  50. currentParamName = paramOpeningTag.slice(1, -1) as ToolParamName
  51. currentParamValueStartIndex = accumulator.length
  52. break
  53. }
  54. }
  55. // There's no current param, and not starting a new param.
  56. // Special case for write_to_file where file contents could
  57. // contain the closing tag, in which case the param would have
  58. // closed and we end up with the rest of the file contents here.
  59. // To work around this, we get the string between the starting
  60. // content tag and the LAST content tag.
  61. const contentParamName: ToolParamName = "content"
  62. if (currentToolUse.name === "write_to_file" && accumulator.endsWith(`</${contentParamName}>`)) {
  63. const toolContent = accumulator.slice(currentToolUseStartIndex)
  64. const contentStartTag = `<${contentParamName}>`
  65. const contentEndTag = `</${contentParamName}>`
  66. const contentStartIndex = toolContent.indexOf(contentStartTag) + contentStartTag.length
  67. const contentEndIndex = toolContent.lastIndexOf(contentEndTag)
  68. if (contentStartIndex !== -1 && contentEndIndex !== -1 && contentEndIndex > contentStartIndex) {
  69. // Don't trim content to preserve newlines, but strip first and last newline only
  70. currentToolUse.params[contentParamName] = toolContent
  71. .slice(contentStartIndex, contentEndIndex)
  72. .replace(/^\n/, "")
  73. .replace(/\n$/, "")
  74. }
  75. }
  76. // Partial tool value is accumulating.
  77. continue
  78. }
  79. }
  80. // No currentToolUse.
  81. let didStartToolUse = false
  82. const possibleToolUseOpeningTags = toolNames.map((name) => `<${name}>`)
  83. for (const toolUseOpeningTag of possibleToolUseOpeningTags) {
  84. if (accumulator.endsWith(toolUseOpeningTag)) {
  85. // Start of a new tool use.
  86. currentToolUse = {
  87. type: "tool_use",
  88. name: toolUseOpeningTag.slice(1, -1) as ToolName,
  89. params: {},
  90. partial: true,
  91. }
  92. currentToolUseStartIndex = accumulator.length
  93. // This also indicates the end of the current text content.
  94. if (currentTextContent) {
  95. currentTextContent.partial = false
  96. // Remove the partially accumulated tool use tag from the
  97. // end of text (<tool).
  98. currentTextContent.content = currentTextContent.content
  99. .slice(0, -toolUseOpeningTag.slice(0, -1).length)
  100. .trim()
  101. contentBlocks.push(currentTextContent)
  102. currentTextContent = undefined
  103. }
  104. didStartToolUse = true
  105. break
  106. }
  107. }
  108. if (!didStartToolUse) {
  109. // No tool use, so it must be text either at the beginning or
  110. // between tools.
  111. if (currentTextContent === undefined) {
  112. currentTextContentStartIndex = i
  113. }
  114. currentTextContent = {
  115. type: "text",
  116. content: accumulator.slice(currentTextContentStartIndex).trim(),
  117. partial: true,
  118. }
  119. }
  120. }
  121. if (currentToolUse) {
  122. // Stream did not complete tool call, add it as partial.
  123. if (currentParamName) {
  124. // Tool call has a parameter that was not completed.
  125. // Don't trim content parameters to preserve newlines, but strip first and last newline only
  126. const paramValue = accumulator.slice(currentParamValueStartIndex)
  127. currentToolUse.params[currentParamName] =
  128. currentParamName === "content" ? paramValue.replace(/^\n/, "").replace(/\n$/, "") : paramValue.trim()
  129. }
  130. contentBlocks.push(currentToolUse)
  131. }
  132. // NOTE: It doesn't matter if check for currentToolUse or
  133. // currentTextContent, only one of them will be defined since only one can
  134. // be partial at a time.
  135. if (currentTextContent) {
  136. // Stream did not complete text content, add it as partial.
  137. contentBlocks.push(currentTextContent)
  138. }
  139. return contentBlocks
  140. }