fix(cron): count silent-run token usage in settings total

A silent (no-op) cron run still makes a full LLM call, but the marker
message was persisted via the token-free saveMessage overload, so the
settings-page Token total (which aggregates MessageEntity token columns)
under-counted cron spend by exactly the silent runs.

Carry chatResult's prompt/completion tokens onto the marker message,
matching the non-silent branch. Closes #284.
This commit is contained in:
倪程伟 2026-06-07 23:35:20 +08:00 committed by matevip
parent 37e9afa9de
commit 453997eb9c

View File

@ -211,7 +211,17 @@ public class CronJobLifecycleService {
// real content to deliver or to learn from.
String marker = i18n != null ? i18n.msg("cron.run.silent")
: "(本次定时任务无新内容,已跳过)";
conversationService.saveMessage(convId, "assistant", marker);
// A silent run still made a full LLM call, so carry its token usage
// onto the marker message otherwise the settings-page total (which
// aggregates MessageEntity token columns) under-counts cron spend.
if (chatResult != null
&& (chatResult.promptTokens() > 0 || chatResult.completionTokens() > 0)) {
conversationService.saveMessage(convId, "assistant", marker, null, "completed",
chatResult.promptTokens(), chatResult.completionTokens(),
chatResult.runtimeModel(), chatResult.runtimeProvider());
} else {
conversationService.saveMessage(convId, "assistant", marker);
}
return;
}