Skip to main content

attach_usage_to_last_completion_message

Function attach_usage_to_last_completion_message 

Source
pub fn attach_usage_to_last_completion_message(
    messages: &mut [ChatMessage],
    usage: MessageUsage,
)
Expand description

Attach a [MessageUsage] to the chat message most likely produced by the completion that generated it.

Walks backward and attaches to the first Assistant or ToolCall message without a usage already set. If every recent candidate is already tagged (e.g. a repeat UsageReport for the same turn), the usage is silently dropped rather than clobbering an earlier tag.

ยงExamples

use codetether_agent::tui::chat::message::{ChatMessage, MessageType, MessageUsage};
use codetether_agent::tui::app::session_events::attach_usage_to_last_completion_message;

let mut msgs = vec![
    ChatMessage::new(MessageType::User, "hi"),
    ChatMessage::new(MessageType::Assistant, "hello!"),
];
attach_usage_to_last_completion_message(
    &mut msgs,
    MessageUsage {
        model: "test/model".into(),
        prompt_tokens: 10,
        completion_tokens: 3,
        duration_ms: 120,
    },
);
assert!(msgs[1].usage.is_some());