Skip to content

feat(acp): LLM-based message rewrite middleware with custom prompts #3189

@zhangxy-zju

Description

@zhangxy-zju

Motivation

In ACP scenarios, raw model output often contains technical details (file paths, SQL, code snippets, tool call info, etc.) that are not user-friendly for business users. We need a configurable mechanism that allows users to define custom rewrite rules to transform model output into formats and styles suited for their target audience.

Solution

Introduce a Message Rewrite Middleware that, at the end of each model turn, collects accumulated output (thoughts + messages) and passes it to an LLM rewriter. The rewritten result is then emitted as a separate assistant message.

Key Capabilities

  • Configurable: Controlled via messageRewrite field in settings.json — toggle on/off, set custom prompt, choose rewrite target (message/thought/both)
  • Custom prompts: Users can write rewrite prompts tailored to their business scenario (e.g., data analysis, code generation, document writing)
  • Full path coverage: Supported in both interactive mode (Session) and non-interactive mode (CLI/eval), including cron-triggered scenarios
  • Safe degradation: Rewrite failures or timeouts (30s) are silently skipped without affecting the original output

Configuration Example

{
  "messageRewrite": {
    "enabled": true,
    "target": "both",
    "prompt": "Your custom rewrite prompt..."
  }
}

Modules Involved

  • packages/cli/src/acp-integration/session/rewrite/ — TurnBuffer, LlmRewriter, MessageRewriteMiddleware, config, types
  • packages/cli/src/acp-integration/session/Session.ts — Interactive mode integration
  • packages/cli/src/nonInteractiveCli.ts — Non-interactive mode integration

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions