Repository navigation
Description
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
ChatMessageis defined inpackages/agent/src/agent_types.rs.ChatMessageInputis defined inpackages/nlp-engine/src/chat/types.rs.- They represent the same thing and are manually mapped in
packages/agent/src/local_agent/inference.rs(theChatMessage → ChatMessageInputconversion).
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.rsis 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.
Activity
ai-chat agent quality: fix runaway/hang + tool-call template leaks, t…
Unify ChatMessage and ChatMessageInput into a single message type (#1339
ai-chat agent quality: fix runaway/hang + tool-call template leaks, t…
Unify ChatMessage and ChatMessageInput into a single message type (#1339