fix: tokens usage is nil (#2322)

This commit is contained in:
yetone
2025-06-24 20:44:04 +08:00
committed by GitHub
parent d6940f79d3
commit 270c11da57
2 changed files with 5 additions and 1 deletions

View File

@@ -335,7 +335,7 @@ function M.generate_prompts(opts)
Utils.debug("Context window", context_window)
if opts.get_tokens_usage then
local tokens_usage = opts.get_tokens_usage()
if tokens_usage then
if tokens_usage and tokens_usage.prompt_tokens ~= nil and tokens_usage.completion_tokens ~= nil then
local target_tokens = context_window * 0.9
local tokens_count = tokens_usage.prompt_tokens + tokens_usage.completion_tokens
Utils.debug("Tokens count", tokens_count)

View File

@@ -2782,7 +2782,11 @@ function Sidebar:create_input_container()
return history and history.todos or {}
end,
session_ctx = {},
---@param usage avante.LLMTokenUsage
update_tokens_usage = function(usage)
if not usage then return end
if usage.completion_tokens == nil then return end
if usage.prompt_tokens == nil then return end
self.chat_history.tokens_usage = usage
self:save_history()
end,