Hồi quy đã vá
-------------
F-12 Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
`chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
`composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
gắn 1 tệp, khớp bản trước refactor.
F-01 Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
`folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
`ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
tiên hỏng thì đổi provider chính là lúc phải thử lại.
F-07 Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
`check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.
F-11 Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
và không bao giờ chạy.
Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:
graph_renderer.py -> graph_scene_builder.py + graph_export.py
co4e_workflow_service.py -> co4e_run_history.py
json_config_repository.py -> config_sections.py
agents_admin_tab.py -> shared/agent_kind_visuals.py
File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.
Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.
Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.
859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
95 lines
3.5 KiB
Python
95 lines
3.5 KiB
Python
"""A thin, unified calling surface over the app's existing Provider layer.
|
|
|
|
The task asks for a ``clients.py`` abstraction that talks to Anthropic / OpenAI
|
|
behind one interface. This app **already has** that — ``providers/`` with
|
|
``build_provider`` and a canonical ``chat()`` that streams text and returns the
|
|
final assistant message. Rather than duplicate it (and re-solve TLS trust,
|
|
429-retry, gateway config…), this module adapts it to the shape the prober
|
|
wants: a single blocking ``complete()`` that returns text + token estimate.
|
|
|
|
Tests inject a fake :class:`ProbeClient` so assessment never hits a real API.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, Dict, List, Optional, Protocol
|
|
|
|
|
|
@dataclass
|
|
class CompletionResult:
|
|
"""Outcome of one non-streaming completion used for probing."""
|
|
|
|
text: str = ""
|
|
tokens_out: int = 0
|
|
error: Optional[str] = None
|
|
|
|
@property
|
|
def ok(self) -> bool:
|
|
"""Lượt dò có thành công không (không có lỗi)."""
|
|
return self.error is None
|
|
|
|
|
|
class ProbeClient(Protocol):
|
|
"""Minimal interface the prober/judge depend on (so they're mockable)."""
|
|
|
|
def complete(
|
|
self,
|
|
provider: str,
|
|
model_id: str,
|
|
messages: List[Dict[str, Any]],
|
|
) -> CompletionResult:
|
|
"""Gọi một model và trả về kết quả kèm số token, độ trễ và lỗi (nếu có)."""
|
|
...
|
|
|
|
|
|
def _estimate_tokens(text: str) -> int:
|
|
"""Rough output-token count. Uses the app's estimator when importable
|
|
(keeps the number consistent with the usage tracker), else ~4 chars/token."""
|
|
try:
|
|
from ..usage_tracker import estimate_tokens
|
|
return int(estimate_tokens(text or ""))
|
|
except Exception: # noqa: BLE001
|
|
return max(0, len(text or "") // 4)
|
|
|
|
|
|
class AppProbeClient:
|
|
"""Real :class:`ProbeClient` backed by :class:`AppContext`.
|
|
|
|
Builds a fresh provider per call via ``ctx.build_provider_for`` — the same
|
|
path interactive chat uses — so the internal gateway, per-host TLS trust and
|
|
rate-limit retry all apply to assessment calls too.
|
|
"""
|
|
|
|
def __init__(self, ctx: Any) -> None:
|
|
"""Giữ ``AppContext`` để dựng provider lúc cần thăm dò."""
|
|
self.ctx = ctx
|
|
|
|
def complete(
|
|
self,
|
|
provider: str,
|
|
model_id: str,
|
|
messages: List[Dict[str, Any]],
|
|
) -> CompletionResult:
|
|
"""Gọi model qua provider thật; lỗi được gói vào kết quả chứ không ném ra —
|
|
một model hỏng không được làm dừng cả lượt chấm điểm danh mục.
|
|
"""
|
|
try:
|
|
prov = self.ctx.build_provider_for(provider, model_id or None)
|
|
# Non-streaming: no on_text/on_reasoning callbacks. cancel=None.
|
|
result = prov.chat(messages, tools=None, on_text=None, cancel=None)
|
|
except Exception as exc: # noqa: BLE001 — surfaced as a failed probe
|
|
return CompletionResult(error=str(exc))
|
|
content = ""
|
|
if isinstance(result, dict):
|
|
content = result.get("content") or ""
|
|
# Strip any inline <think> block a reasoning model may have inlined.
|
|
try:
|
|
from ...providers.base import Provider
|
|
content = Provider.strip_think(content)
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
return CompletionResult(text=content, tokens_out=_estimate_tokens(content))
|
|
|
|
|
|
__all__ = ["CompletionResult", "ProbeClient", "AppProbeClient"]
|