Skip to content

Conversation

@StevenTCramer
Copy link
Contributor

@StevenTCramer StevenTCramer commented Jun 14, 2025

Related GitHub Issue

Closes: #4692

Description

This PR refactors the assistant message parsing logic to improve maintainability and clarity, addressing issue #4692. The original monolithic structure in src/core/assistant-message/parseAssistantMessage.ts was difficult to maintain. The changes introduce a modular architecture with:

  • Extraction of parsing logic into DirectiveStreamingParser with specialized parser classes: TextContentParser, ToolUseParser, and ParameterParser.
  • Organization of parsers into separate files under src/core/assistant-message/parsers/.
  • Renaming of types (e.g., AssistantMessageContent to Directive) with aliases for backward compatibility.
  • Simplification of method names to parse() and finalize().
  • Maintenance of original streaming behavior with no breaking changes.

Test Procedure

  • The refactoring has been tested by ensuring all existing tests pass without modification.
  • Reviewers can verify the changes by checking the updated and new files in src/core/assistant-message/ directory, specifically parseAssistantMessage.ts, DirectiveStreamingParser.ts, and the new parser files under parsers/.
  • Run
    pm test to confirm that all tests pass locally.

Type of Change

  • ♻️ Refactor: Code change that neither fixes a bug nor adds a feature.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see 'Related GitHub Issue' above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Code Quality:
    • My code adheres to the project's style guidelines.
    • There are no new linting errors or warnings (
      pm run lint).
    • All debug code (e.g., console.log) has been removed.
  • Testing:
    • New and/or updated tests have been added to cover my changes.
    • All tests pass locally (
      pm test).
    • The application builds successfully with my changes.
  • Branch Hygiene: My branch is up-to-date (rebased) with the main branch.
  • Documentation Impact: I have considered if my changes require documentation updates (see 'Documentation Updates' section below).
  • Changeset: A changeset has been created using
    pm run changeset if this PR includes user-facing changes or dependency updates.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

Not applicable for this change as it does not involve UI modifications.

Documentation Updates

  • No documentation updates are required.

Additional Notes

Please review the changes in src/core/assistant-message/ for the core refactoring of the parsing logic. The focus has been on ensuring backward compatibility and maintaining streaming behavior.

Get in Touch

Please provide your Discord username for reviewers or maintainers to reach you if they have questions about your PR.


Important

Refactor assistant message parsing by modularizing logic into specialized parsers, maintaining backward compatibility and streaming behavior.

  • Refactor Parsing Logic:
    • Extract parsing logic into DirectiveStreamingParser with specialized parsers: TextContentParser, ToolUseParser, ParameterParser.
    • Organize parsers into separate files under src/core/assistant-message/parsers/.
    • Maintain original streaming behavior with no breaking changes.
  • Type and Method Changes:
    • Rename AssistantMessageContent to Directive with alias for backward compatibility.
    • Simplify method names to parse() and finalize().
  • Miscellaneous:
    • Update parseAssistantMessage.ts to use DirectiveStreamingParser.
    • Minor changes in packages/evals files to suppress prompts and update ports.

This description was created by Ellipsis for 2993a3a. You can customize this summary. It will automatically update as commits are pushed.

StevenTCramer and others added 17 commits June 12, 2025 23:45
…conflict with numerous other things that just use the default.
- Add TextDirective alias for TextContent
- Add ToolDirective alias for ToolUse
- Add Directive union type for TextDirective | ToolDirective
- Maintain backward compatibility with AssistantMessageContent alias
- Update internal usage to use new type names
- Extract parsing logic from monolithic parseAssistantMessage function
- Create StreamingParser with specialized handler classes:
  - TextContentHandler: handles text content parsing
  - ToolUseHandler: manages tool directive detection and parsing
  - ParameterHandler: processes tool parameters
- Maintain original streaming behavior and backward compatibility
- All tests pass, no breaking changes
- Improve code organization and maintainability
- Extract TextContentHandler into TextContentHandler.ts
- Extract ParameterHandler into ParameterHandler.ts
- Extract ToolUseHandler into ToolUseHandler.ts
- Create types.ts for shared type definitions
- Update StreamingParser to import from separate files
- Update index.ts exports to reference correct files
- Update README.md to reflect new file structure
- Maintain backward compatibility and streaming behavior
- All tests pass, multi-contributor friendly structure
…d 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
- Changed imports in DirectiveStreamingParser.ts to use the index.ts file in parsers/ for a cleaner import structure
@StevenTCramer StevenTCramer requested review from cte, jr and mrubens as code owners June 14, 2025 10:53
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 14, 2025
const toolContent = state.accumulator.slice(state.currentToolUseStartIndex)
const contentStartTag = `<${contentParamName}>`
const contentEndTag = `</${contentParamName}>`
const contentStartIndex = toolContent.indexOf(contentStartTag) + contentStartTag.length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the write_to_file special case, compute the index for the start tag separately before adding its length. As is, if indexOf returns -1, adding tag length yields a false positive. Consider storing the raw index and checking it before computing contentStartIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a draft. I am reviewing more and I think this should be seriously refactored. PR coming after I am complete.

@StevenTCramer StevenTCramer marked this pull request as draft June 14, 2025 10:56
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jun 14, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jun 14, 2025
@hannesrudolph hannesrudolph removed the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jun 14, 2025
@StevenTCramer
Copy link
Contributor Author

StevenTCramer commented Jun 16, 2025

I rewrote the whole thing will submit under a new PR.

@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jun 16, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Refactor Assistant Message Parsing Logic for Improved Maintainability

2 participants