Skip to content

[Feature]: Strip ANSI escape sequences from tool_result in messages.transform to reduce token waste #1796

@ciuccipippindd930-eng

Description

@ciuccipippindd930-eng

Prerequisites

  • I will write this issue in English (see our Language Policy)
  • I have searched existing issues and discussions to avoid duplicates
  • This feature request is specific to oh-my-opencode (not OpenCode core)
  • I have read the documentation or asked an AI coding agent with this project's GitHub URL loaded and couldn't find the answer

Problem Description

Problem

Bash tool outputs containing ANSI escape sequences (spinner animations, color codes, cursor movements) are stored verbatim in conversation history as tool_result content. These sequences are pure noise that waste significant tokens on every API request.

Example

Running npx skills find ... or npx skills add ... produces outputs like:
\u001b[35m◒\u001b[39m Cloning repository... \u001b[999D\u001b[J\u001b[35m◐\u001b[39m Cloning repository... \u001b[999D\u001b[J\u001b[35m◓\u001b[39m Cloning repository...

This spinner animation repeats for hundreds of lines, all stored in tool_result and sent with every subsequent API request.

Impact

In a real conversation, a single npx skills add command contributed ~300+ lines of ANSI garbage to the conversation history, accounting for ~25% of total token usage.

Proposed Solution

Proposed Solution

Add ANSI stripping logic in experimental.chat.messages.transform hook (currently at src/plugin/messages-transform.ts).
For every tool_result part in the message history, apply:

const ANSI_REGEX = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
text.replace(ANSI_REGEX, '');
This would also remove redundant cursor control sequences like \u001b[999D\u001b[J which serve no purpose in API context.

Current behavior
createMessagesTransformHandler
 (line ~67035 in dist/index.js) only calls:

contextInjectorMessagesTransform  context injection
thinkingBlockValidator  thinking block validation
No sanitization of tool output content.

Expected behavior
Tool result content in conversation history should have ANSI escape sequences stripped before being sent to the API, reducing token waste significantly.

Environment
oh-my-opencode version: latest (npm)
OS: Windows
OpenCode: latest

### Alternatives Considered

_No response_

### Doctor Output (Optional)

```shell

Additional Context

No response

Feature Type

New Tool

Contribution

  • I'm willing to submit a PR for this feature
  • I can help with testing
  • I can help with documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions