feat(gui): Add auto-scroll, blinking history, and reactive API events
This commit is contained in:
56
ai_client.py
56
ai_client.py
@@ -1266,15 +1266,18 @@ def send(
|
||||
return _send_anthropic(md_content, user_message, base_dir, file_items, discussion_history)
|
||||
raise ValueError(f"unknown provider: {_provider}")
|
||||
|
||||
def get_history_bleed_stats() -> dict:
|
||||
def get_history_bleed_stats(md_content: str | None = None) -> dict:
|
||||
"""
|
||||
Calculates how close the current conversation history is to the token limit.
|
||||
If md_content is provided and no chat session exists, it estimates based on md_content.
|
||||
"""
|
||||
if _provider == "anthropic":
|
||||
# For Anthropic, we have a robust estimator
|
||||
with _anthropic_history_lock:
|
||||
history_snapshot = list(_anthropic_history)
|
||||
current_tokens = _estimate_prompt_tokens([], history_snapshot)
|
||||
if md_content:
|
||||
current_tokens += max(1, int(len(md_content) / _CHARS_PER_TOKEN))
|
||||
limit_tokens = _ANTHROPIC_MAX_PROMPT_TOKENS
|
||||
percentage = (current_tokens / limit_tokens) * 100 if limit_tokens > 0 else 0
|
||||
return {
|
||||
@@ -1287,21 +1290,42 @@ def get_history_bleed_stats() -> dict:
|
||||
if _gemini_chat:
|
||||
try:
|
||||
_ensure_gemini_client()
|
||||
history = _get_gemini_history_list(_gemini_chat)
|
||||
if history:
|
||||
resp = _gemini_client.models.count_tokens(
|
||||
model=_model,
|
||||
contents=history
|
||||
)
|
||||
current_tokens = resp.total_tokens
|
||||
limit_tokens = _GEMINI_MAX_INPUT_TOKENS
|
||||
percentage = (current_tokens / limit_tokens) * 100 if limit_tokens > 0 else 0
|
||||
return {
|
||||
"provider": "gemini",
|
||||
"limit": limit_tokens,
|
||||
"current": current_tokens,
|
||||
"percentage": percentage,
|
||||
}
|
||||
history = list(_get_gemini_history_list(_gemini_chat))
|
||||
if md_content:
|
||||
# Prepend context as a user part for counting
|
||||
history.insert(0, types.Content(role="user", parts=[types.Part.from_text(text=md_content)]))
|
||||
|
||||
resp = _gemini_client.models.count_tokens(
|
||||
model=_model,
|
||||
contents=history
|
||||
)
|
||||
current_tokens = resp.total_tokens
|
||||
limit_tokens = _GEMINI_MAX_INPUT_TOKENS
|
||||
percentage = (current_tokens / limit_tokens) * 100 if limit_tokens > 0 else 0
|
||||
return {
|
||||
"provider": "gemini",
|
||||
"limit": limit_tokens,
|
||||
"current": current_tokens,
|
||||
"percentage": percentage,
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
elif md_content:
|
||||
try:
|
||||
_ensure_gemini_client()
|
||||
resp = _gemini_client.models.count_tokens(
|
||||
model=_model,
|
||||
contents=[types.Content(role="user", parts=[types.Part.from_text(text=md_content)])]
|
||||
)
|
||||
current_tokens = resp.total_tokens
|
||||
limit_tokens = _GEMINI_MAX_INPUT_TOKENS
|
||||
percentage = (current_tokens / limit_tokens) * 100 if limit_tokens > 0 else 0
|
||||
return {
|
||||
"provider": "gemini",
|
||||
"limit": limit_tokens,
|
||||
"current": current_tokens,
|
||||
"percentage": percentage,
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user