refactor: streamline token counting and message shaping logic
This commit is contained in:
@@ -13,6 +13,7 @@ use sha2::Digest;
|
||||
use zesdex_cms::domain::repository::EditLogRepository;
|
||||
|
||||
use crate::app::guard::Verdict;
|
||||
use crate::app::runtime::context::tokens::count_tokens;
|
||||
use crate::app::state::runtime::TurnEvent;
|
||||
use zesdex_cms::domain::repository::MemoryRepository;
|
||||
use crate::dto::chat::message::ChatMessage;
|
||||
@@ -326,12 +327,11 @@ pub(super) fn run_agent_turn(
|
||||
let mut todo_retry_count = 0usize;
|
||||
|
||||
loop {
|
||||
let total_chars: usize = msgs
|
||||
let token_estimate: usize = msgs
|
||||
.iter()
|
||||
.filter_map(|m| m.content.as_deref())
|
||||
.map(str::len)
|
||||
.map(count_tokens)
|
||||
.sum();
|
||||
let token_estimate = total_chars / 4;
|
||||
let max_wire_tokens = tc.context_window;
|
||||
|
||||
// Skip message compaction if abort was requested — the non-streaming
|
||||
@@ -352,6 +352,7 @@ pub(super) fn run_agent_turn(
|
||||
max_wire_tokens,
|
||||
false,
|
||||
Some(&tc.client),
|
||||
Some(&tc.abort_flag),
|
||||
);
|
||||
|
||||
// Dispatch the compacted messages to the main thread so the local session history
|
||||
@@ -488,12 +489,12 @@ pub(super) fn run_agent_turn(
|
||||
|
||||
let (mut tok_in, mut tok_out) = final_usage.unwrap_or((0, 0));
|
||||
if tok_in == 0 {
|
||||
let total_chars: usize = wire_msgs
|
||||
let total_tokens: usize = wire_msgs
|
||||
.iter()
|
||||
.filter_map(|m| m.content.as_deref())
|
||||
.map(str::len)
|
||||
.map(count_tokens)
|
||||
.sum();
|
||||
tok_in = (total_chars / 4).max(1) as u64;
|
||||
tok_in = total_tokens.max(1) as u64;
|
||||
}
|
||||
if tok_out == 0 {
|
||||
let response_chars = response.content.as_deref().map_or(0, str::len);
|
||||
|
||||
Reference in New Issue
Block a user