Skip to content

Unify ChatMessage and ChatMessageInput into a single message type #1325

Closed

Description

@malibio

Overview

The local-agent stack defines the same concept — a chat message — twice, and bridges the two representations by hand. This hand-mapping has already silently dropped a field and contributed to malformed conversation history during inference re-prompts. This issue consolidates the two types so fields cannot be dropped in the bridge.

Problem Statement

  • ChatMessage is defined in packages/agent/src/agent_types.rs.
  • ChatMessageInput is defined in packages/nlp-engine/src/chat/types.rs.
  • They represent the same thing and are manually mapped in packages/agent/src/local_agent/inference.rs (the ChatMessage → ChatMessageInput conversion).

Because the mapping is a hand-written .map(), a missing field is invisible: a tool_calls field was absent from both structs, so when the agent re-prompted after a tool call, the assistant's structured tool calls were dropped, producing an orphan tool result in the history (no matching tool_calls in the preceding assistant turn). A tool_calls field has since been added to both as a stopgap, but the structural duplication remains and will keep producing this class of bug.

Proposed Solution

Establish a single source-of-truth message type, or generate one representation from the other, so that adding a field to a message is reflected everywhere by construction. Eliminate the manual map in inference.rs, or make it total and compiler-checked (e.g. exhaustive From/Into with no field defaulted silently).

Acceptance Criteria

  • A single message type is the source of truth (or one type is mechanically derived from the other)
  • The hand-written field-by-field map in inference.rs is removed or replaced with a compiler-checked total conversion
  • Adding a new field to a chat message cannot compile without being represented in both inference and templating paths
  • Existing ai-chat inference behavior is unchanged (assistant tool_calls still round-trip correctly)
  • Code passes `bun run quality:fix`
  • Tests pass (`bun run test:all`)

Technical Specifications

Reference Files

  • ChatMessage: `packages/agent/src/agent_types.rs` - agent-side message type
  • ChatMessageInput / ToolCallInput: `packages/nlp-engine/src/chat/types.rs` - nlp-engine message type
  • Bridge: `packages/agent/src/local_agent/inference.rs` - manual ChatMessage → ChatMessageInput conversion

Implementation Notes

  • The nlp-engine crate must not depend on the agent crate; if unifying on a single type, it likely lives in nlp-engine (lower layer) and the agent re-exports/uses it, or a shared trait/newtype bridges them with a derive.

Parallelization

This issue is scoped to the message-type representation and inference.rs. It does not modify the tool registry, prompt assembly, or daemon service wiring, so it can proceed in parallel with the related single-source-of-truth issues. Minor coordination note: edits land in the agent crate alongside those issues — rebase as needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions