Skip to content

Commit 9e70691

Browse files
committed
Refactor assistant message parsing: rename classes and simplify method names
- Moved StreamingParser to DirectiveStreamingParser in assistant-message/ - Renamed directives/ to parsers/ - Created new parser classes with updated names: TextContentParser, ToolUseParser, ParameterParser - Simplified method names to parse() and finalize() - Updated documentation in README.md
1 parent 47c1dd7 commit 9e70691

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

src/core/assistant-message/directives/StreamingParser.ts renamed to src/core/assistant-message/DirectiveStreamingParser.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Directive, ParsingState } from "./types"
2-
import { TextContentHandler } from "./TextContentHandler"
3-
import { ToolUseHandler } from "./ToolUseHandler"
4-
import { ParameterHandler } from "./ParameterHandler"
1+
import { Directive, ParsingState } from "./parsers/types"
2+
import { TextContentParser } from "./parsers/TextContentParser"
3+
import { ToolUseParser } from "./parsers/ToolUseParser"
4+
import { ParameterParser } from "./parsers/ParameterParser"
55

6-
export class StreamingParser {
6+
export class DirectiveStreamingParser {
77
static parse(assistantMessage: string): Directive[] {
88
const state: ParsingState = {
99
contentBlocks: [],
@@ -21,18 +21,18 @@ export class StreamingParser {
2121
state.accumulator += char
2222

2323
// There should not be a param without a tool use.
24-
if (ParameterHandler.handleParameter(state)) {
24+
if (ParameterParser.parse(state)) {
2525
continue
2626
}
2727

2828
// No currentParamName.
29-
if (ToolUseHandler.handleToolUse(state)) {
29+
if (ToolUseParser.parse(state)) {
3030
continue
3131
}
3232

3333
// No currentToolUse.
34-
const didStartToolUse = ToolUseHandler.checkForToolStart(state)
35-
TextContentHandler.handleTextContent(state, i, didStartToolUse)
34+
const didStartToolUse = ToolUseParser.checkForToolStart(state)
35+
TextContentParser.parse(state, i, didStartToolUse)
3636
}
3737

3838
// Handle remaining partial content

src/core/assistant-message/directives/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { StreamingParser } from "./directives/StreamingParser"
2-
import type { Directive } from "./directives"
1+
import { DirectiveStreamingParser } from "./DirectiveStreamingParser"
2+
import type { Directive } from "./parsers/types"
33

44
// Re-export types for backward compatibility
5-
export type { TextDirective, ToolDirective, Directive } from "./directives"
5+
export type { TextDirective, ToolDirective, Directive } from "./parsers/types"
66

77
// Backward compatibility alias
88
export type AssistantMessageContent = Directive
99

1010
export function parseAssistantMessage(assistantMessage: string): Directive[] {
11-
return StreamingParser.parse(assistantMessage)
11+
return DirectiveStreamingParser.parse(assistantMessage)
1212
}

src/core/assistant-message/directives/ParameterHandler.ts renamed to src/core/assistant-message/parsers/ParameterParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ParsingState } from "./types"
22

3-
export class ParameterHandler {
4-
static handleParameter(state: ParsingState): boolean {
3+
export class ParameterParser {
4+
static parse(state: ParsingState): boolean {
55
if (!state.currentToolUse || !state.currentParamName) return false
66

77
const currentParamValue = state.accumulator.slice(state.currentParamValueStartIndex)

src/core/assistant-message/directives/TextContentHandler.ts renamed to src/core/assistant-message/parsers/TextContentParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ParsingState } from "./types"
22

3-
export class TextContentHandler {
4-
static handleTextContent(state: ParsingState, currentIndex: number, didStartToolUse: boolean): void {
3+
export class TextContentParser {
4+
static parse(state: ParsingState, currentIndex: number, didStartToolUse: boolean): void {
55
if (!didStartToolUse) {
66
// No tool use, so it must be text either at the beginning or between tools.
77
if (state.currentTextContent === undefined) {
@@ -16,7 +16,7 @@ export class TextContentHandler {
1616
}
1717
}
1818

19-
static finalizeTextContent(state: ParsingState, toolUseOpeningTag: string): void {
19+
static finalize(state: ParsingState, toolUseOpeningTag: string): void {
2020
if (state.currentTextContent) {
2121
state.currentTextContent.partial = false
2222

src/core/assistant-message/directives/ToolUseHandler.ts renamed to src/core/assistant-message/parsers/ToolUseParser.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type ToolName, toolNames } from "@roo-code/types"
22
import { ToolParamName, toolParamNames } from "../../../shared/tools"
33
import { ParsingState } from "./types"
4-
import { TextContentHandler } from "./TextContentHandler"
4+
import { TextContentParser } from "./TextContentParser"
55

6-
export class ToolUseHandler {
6+
export class ToolUseParser {
77
static checkForToolStart(state: ParsingState): boolean {
88
let didStartToolUse = false
99
const possibleToolUseOpeningTags = toolNames.map((name) => `<${name}>`)
@@ -21,7 +21,7 @@ export class ToolUseHandler {
2121
state.currentToolUseStartIndex = state.accumulator.length
2222

2323
// This also indicates the end of the current text content.
24-
TextContentHandler.finalizeTextContent(state, toolUseOpeningTag)
24+
TextContentParser.finalize(state, toolUseOpeningTag)
2525

2626
didStartToolUse = true
2727
break
@@ -31,7 +31,7 @@ export class ToolUseHandler {
3131
return didStartToolUse
3232
}
3333

34-
static handleToolUse(state: ParsingState): boolean {
34+
static parse(state: ParsingState): boolean {
3535
if (!state.currentToolUse) return false
3636

3737
const currentToolValue = state.accumulator.slice(state.currentToolUseStartIndex)
@@ -44,13 +44,13 @@ export class ToolUseHandler {
4444
state.currentToolUse = undefined
4545
return true
4646
} else {
47-
this.handleParameterParsing(state)
48-
this.handleSpecialCases(state)
47+
this.parseParameter(state)
48+
this.parseSpecialCases(state)
4949
return true // Continue processing
5050
}
5151
}
5252

53-
private static handleParameterParsing(state: ParsingState): void {
53+
private static parseParameter(state: ParsingState): void {
5454
const possibleParamOpeningTags = toolParamNames.map((name) => `<${name}>`)
5555
for (const paramOpeningTag of possibleParamOpeningTags) {
5656
if (state.accumulator.endsWith(paramOpeningTag)) {
@@ -62,7 +62,7 @@ export class ToolUseHandler {
6262
}
6363
}
6464

65-
private static handleSpecialCases(state: ParsingState): void {
65+
private static parseSpecialCases(state: ParsingState): void {
6666
if (!state.currentToolUse) return
6767

6868
// Special case for write_to_file where file contents could

0 commit comments

Comments
 (0)