|
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 | | -} |
| 1 | +import { Directive, ParsingState } from "./types" |
| 2 | +import { TextContentHandler } from "./TextContentHandler" |
| 3 | +import { ToolUseHandler } from "./ToolUseHandler" |
| 4 | +import { ParameterHandler } from "./ParameterHandler" |
156 | 5 |
|
157 | 6 | export class StreamingParser { |
158 | 7 | static parse(assistantMessage: string): Directive[] { |
|
0 commit comments