|
| 1 | +import { type ToolName, toolNames } from "@roo-code/types" |
| 2 | +import { TextContent, ToolUse, ToolParamName, toolParamNames } from "../../../shared/tools" |
| 3 | + |
| 4 | +// Type aliases for directive parsing |
| 5 | +export type TextDirective = TextContent |
| 6 | +export type ToolDirective = ToolUse |
| 7 | +export type Directive = TextDirective | ToolDirective |
| 8 | + |
| 9 | +export interface ParsingState { |
| 10 | + contentBlocks: Directive[] |
| 11 | + currentTextContent?: TextDirective |
| 12 | + currentTextContentStartIndex: number |
| 13 | + currentToolUse?: ToolDirective |
| 14 | + currentToolUseStartIndex: number |
| 15 | + currentParamName?: ToolParamName |
| 16 | + currentParamValueStartIndex: number |
| 17 | + accumulator: string |
| 18 | +} |
| 19 | + |
| 20 | +export class TextContentHandler { |
| 21 | + static handleTextContent(state: ParsingState, currentIndex: number, didStartToolUse: boolean): void { |
| 22 | + if (!didStartToolUse) { |
| 23 | + // No tool use, so it must be text either at the beginning or between tools. |
| 24 | + if (state.currentTextContent === undefined) { |
| 25 | + state.currentTextContentStartIndex = currentIndex |
| 26 | + } |
| 27 | + |
| 28 | + state.currentTextContent = { |
| 29 | + type: "text", |
| 30 | + content: state.accumulator.slice(state.currentTextContentStartIndex).trim(), |
| 31 | + partial: true, |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static finalizeTextContent(state: ParsingState, toolUseOpeningTag: string): void { |
| 37 | + if (state.currentTextContent) { |
| 38 | + state.currentTextContent.partial = false |
| 39 | + |
| 40 | + // Remove the partially accumulated tool use tag from the end of text (<tool). |
| 41 | + state.currentTextContent.content = state.currentTextContent.content |
| 42 | + .slice(0, -toolUseOpeningTag.slice(0, -1).length) |
| 43 | + .trim() |
| 44 | + |
| 45 | + state.contentBlocks.push(state.currentTextContent) |
| 46 | + state.currentTextContent = undefined |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export class ToolUseHandler { |
| 52 | + static checkForToolStart(state: ParsingState): boolean { |
| 53 | + let didStartToolUse = false |
| 54 | + const possibleToolUseOpeningTags = toolNames.map((name) => `<${name}>`) |
| 55 | + |
| 56 | + for (const toolUseOpeningTag of possibleToolUseOpeningTags) { |
| 57 | + if (state.accumulator.endsWith(toolUseOpeningTag)) { |
| 58 | + // Start of a new tool use. |
| 59 | + state.currentToolUse = { |
| 60 | + type: "tool_use", |
| 61 | + name: toolUseOpeningTag.slice(1, -1) as ToolName, |
| 62 | + params: {}, |
| 63 | + partial: true, |
| 64 | + } |
| 65 | + |
| 66 | + state.currentToolUseStartIndex = state.accumulator.length |
| 67 | + |
| 68 | + // This also indicates the end of the current text content. |
| 69 | + TextContentHandler.finalizeTextContent(state, toolUseOpeningTag) |
| 70 | + |
| 71 | + didStartToolUse = true |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return didStartToolUse |
| 77 | + } |
| 78 | + |
| 79 | + static handleToolUse(state: ParsingState): boolean { |
| 80 | + if (!state.currentToolUse) return false |
| 81 | + |
| 82 | + const currentToolValue = state.accumulator.slice(state.currentToolUseStartIndex) |
| 83 | + const toolUseClosingTag = `</${state.currentToolUse.name}>` |
| 84 | + |
| 85 | + if (currentToolValue.endsWith(toolUseClosingTag)) { |
| 86 | + // End of a tool use. |
| 87 | + state.currentToolUse.partial = false |
| 88 | + state.contentBlocks.push(state.currentToolUse) |
| 89 | + state.currentToolUse = undefined |
| 90 | + return true |
| 91 | + } else { |
| 92 | + this.handleParameterParsing(state) |
| 93 | + this.handleSpecialCases(state) |
| 94 | + return true // Continue processing |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static handleParameterParsing(state: ParsingState): void { |
| 99 | + const possibleParamOpeningTags = toolParamNames.map((name) => `<${name}>`) |
| 100 | + for (const paramOpeningTag of possibleParamOpeningTags) { |
| 101 | + if (state.accumulator.endsWith(paramOpeningTag)) { |
| 102 | + // Start of a new parameter. |
| 103 | + state.currentParamName = paramOpeningTag.slice(1, -1) as ToolParamName |
| 104 | + state.currentParamValueStartIndex = state.accumulator.length |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static handleSpecialCases(state: ParsingState): void { |
| 111 | + if (!state.currentToolUse) return |
| 112 | + |
| 113 | + // Special case for write_to_file where file contents could |
| 114 | + // contain the closing tag, in which case the param would have |
| 115 | + // closed and we end up with the rest of the file contents here. |
| 116 | + // To work around this, we get the string between the starting |
| 117 | + // content tag and the LAST content tag. |
| 118 | + const contentParamName: ToolParamName = "content" |
| 119 | + |
| 120 | + if (state.currentToolUse.name === "write_to_file" && state.accumulator.endsWith(`</${contentParamName}>`)) { |
| 121 | + const toolContent = state.accumulator.slice(state.currentToolUseStartIndex) |
| 122 | + const contentStartTag = `<${contentParamName}>` |
| 123 | + const contentEndTag = `</${contentParamName}>` |
| 124 | + const contentStartIndex = toolContent.indexOf(contentStartTag) + contentStartTag.length |
| 125 | + const contentEndIndex = toolContent.lastIndexOf(contentEndTag) |
| 126 | + |
| 127 | + if (contentStartIndex !== -1 && contentEndIndex !== -1 && contentEndIndex > contentStartIndex) { |
| 128 | + state.currentToolUse.params[contentParamName] = toolContent |
| 129 | + .slice(contentStartIndex, contentEndIndex) |
| 130 | + .trim() |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +export class ParameterHandler { |
| 137 | + static handleParameter(state: ParsingState): boolean { |
| 138 | + if (!state.currentToolUse || !state.currentParamName) return false |
| 139 | + |
| 140 | + const currentParamValue = state.accumulator.slice(state.currentParamValueStartIndex) |
| 141 | + const paramClosingTag = `</${state.currentParamName}>` |
| 142 | + |
| 143 | + if (currentParamValue.endsWith(paramClosingTag)) { |
| 144 | + // End of param value. |
| 145 | + state.currentToolUse.params[state.currentParamName] = currentParamValue |
| 146 | + .slice(0, -paramClosingTag.length) |
| 147 | + .trim() |
| 148 | + state.currentParamName = undefined |
| 149 | + return true |
| 150 | + } else { |
| 151 | + // Partial param value is accumulating. |
| 152 | + return true |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +export class StreamingParser { |
| 158 | + static parse(assistantMessage: string): Directive[] { |
| 159 | + const state: ParsingState = { |
| 160 | + contentBlocks: [], |
| 161 | + currentTextContent: undefined, |
| 162 | + currentTextContentStartIndex: 0, |
| 163 | + currentToolUse: undefined, |
| 164 | + currentToolUseStartIndex: 0, |
| 165 | + currentParamName: undefined, |
| 166 | + currentParamValueStartIndex: 0, |
| 167 | + accumulator: "", |
| 168 | + } |
| 169 | + |
| 170 | + for (let i = 0; i < assistantMessage.length; i++) { |
| 171 | + const char = assistantMessage[i] |
| 172 | + state.accumulator += char |
| 173 | + |
| 174 | + // There should not be a param without a tool use. |
| 175 | + if (ParameterHandler.handleParameter(state)) { |
| 176 | + continue |
| 177 | + } |
| 178 | + |
| 179 | + // No currentParamName. |
| 180 | + if (ToolUseHandler.handleToolUse(state)) { |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + // No currentToolUse. |
| 185 | + const didStartToolUse = ToolUseHandler.checkForToolStart(state) |
| 186 | + TextContentHandler.handleTextContent(state, i, didStartToolUse) |
| 187 | + } |
| 188 | + |
| 189 | + // Handle remaining partial content |
| 190 | + this.handlePartialContent(state) |
| 191 | + |
| 192 | + return state.contentBlocks |
| 193 | + } |
| 194 | + |
| 195 | + private static handlePartialContent(state: ParsingState): void { |
| 196 | + if (state.currentToolUse) { |
| 197 | + // Stream did not complete tool call, add it as partial. |
| 198 | + if (state.currentParamName) { |
| 199 | + // Tool call has a parameter that was not completed. |
| 200 | + state.currentToolUse.params[state.currentParamName] = state.accumulator |
| 201 | + .slice(state.currentParamValueStartIndex) |
| 202 | + .trim() |
| 203 | + } |
| 204 | + |
| 205 | + state.contentBlocks.push(state.currentToolUse) |
| 206 | + } |
| 207 | + |
| 208 | + // NOTE: It doesn't matter if check for currentToolUse or |
| 209 | + // currentTextContent, only one of them will be defined since only one can |
| 210 | + // be partial at a time. |
| 211 | + if (state.currentTextContent) { |
| 212 | + // Stream did not complete text content, add it as partial. |
| 213 | + state.contentBlocks.push(state.currentTextContent) |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments