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>
112 lines
4.7 KiB
Python
112 lines
4.7 KiB
Python
"""Generate illustration images via an image/vision model, to support editing
|
|
and creating images inside files (e.g. a new picture for a slide, or a
|
|
standalone image file).
|
|
|
|
Uses the active provider's OpenAI-compatible ``/images/generations`` endpoint
|
|
(the internal gateway, OpenAI, or any compatible server) — the same base URL +
|
|
API key the chat model already uses. Best-effort and never raises: returns
|
|
``(ok, message_or_path)`` so the UI can fall back gracefully when the endpoint
|
|
or model isn't available. Pure logic (no Qt) → unit-testable with the HTTP
|
|
layer mocked.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from pathlib import Path
|
|
from typing import Optional, Tuple
|
|
|
|
_DEFAULT_MODEL = "gpt-image-1"
|
|
_TIMEOUT = (15, 180)
|
|
|
|
# Substrings (lowercased) that mark a model as image-GENERATION capable, across
|
|
# providers/gateways — so the AI-edit model picker can suggest one regardless of
|
|
# whether the endpoint is OpenAI, Anthropic-routed, or another gateway.
|
|
_IMAGE_MODEL_MARKERS = (
|
|
"image", "dall-e", "dalle", "imagen", "flux", "stable-diffusion", "sdxl",
|
|
"sd3", "sd-", "grok-2-image", "seedream", "firefly", "titan-image", "photon",
|
|
)
|
|
|
|
|
|
def looks_like_image_model(name: str) -> bool:
|
|
"""Đoán một model có sinh ảnh được không, dựa trên dấu hiệu trong tên.
|
|
|
|
Đoán theo tên vì không provider nào khai báo năng lực này qua API.
|
|
"""
|
|
n = (name or "").lower()
|
|
return any(m in n for m in _IMAGE_MODEL_MARKERS)
|
|
|
|
|
|
def suggest_image_model(models) -> Optional[str]:
|
|
"""Pick the most likely text-to-image model from a provider's model list
|
|
(any provider). Returns None if none look image-capable."""
|
|
for m in models or []:
|
|
if looks_like_image_model(m):
|
|
return m
|
|
return None
|
|
|
|
|
|
def _conf(config):
|
|
"""(base_url, api_key, model, ca_bundle) for image generation, from the
|
|
active provider + optional image_gen overrides in config."""
|
|
prov = config.provider_conf(config.active_provider) if config else {}
|
|
igen = (getattr(config, "data", {}) or {}).get("image_gen", {}) if config else {}
|
|
base = (igen.get("base_url") or prov.get("base_url") or "").rstrip("/")
|
|
key = igen.get("api_key") or prov.get("api_key") or ""
|
|
model = igen.get("model") or _DEFAULT_MODEL
|
|
ca = getattr(config, "ca_bundle", "") if config else ""
|
|
return base, key, model, ca
|
|
|
|
|
|
def is_configured(config) -> bool:
|
|
"""True when an image endpoint can be attempted (a base URL is set). The
|
|
actual call still degrades gracefully if the server/model can't generate."""
|
|
base, _key, _model, _ca = _conf(config)
|
|
return bool(base)
|
|
|
|
|
|
def generate_image(config, prompt: str, out_path: str,
|
|
model: Optional[str] = None, size: str = "1024x1024",
|
|
base_url: Optional[str] = None, api_key: Optional[str] = None) -> Tuple[bool, str]:
|
|
"""Generate an image for ``prompt`` and save it to ``out_path`` (PNG).
|
|
Returns ``(True, out_path)`` or ``(False, reason)``. Never raises.
|
|
|
|
By default the active provider's endpoint is used; pass ``base_url``/``api_key``
|
|
to target a DIFFERENT provider (e.g. an image model discovered on another
|
|
configured provider)."""
|
|
prompt = (prompt or "").strip()
|
|
if not prompt:
|
|
return False, "empty prompt"
|
|
base, key, cfg_model, ca = _conf(config)
|
|
if base_url: # explicit provider override (cross-provider image model)
|
|
base = base_url.rstrip("/")
|
|
key = api_key or ""
|
|
if not base:
|
|
return False, "no image endpoint configured (set a provider base URL or image_gen.base_url)"
|
|
from . import tls_trust
|
|
|
|
url = base + "/images/generations"
|
|
headers = {"Content-Type": "application/json"}
|
|
if key:
|
|
headers["Authorization"] = f"Bearer {key}"
|
|
payload = {"model": model or cfg_model, "prompt": prompt, "n": 1, "size": size}
|
|
try:
|
|
resp = tls_trust.request("post", url, ca_bundle=ca or None, json=payload,
|
|
headers=headers, timeout=_TIMEOUT)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
except Exception as exc: # noqa: BLE001 - endpoint/model unsupported, network, TLS…
|
|
return False, f"image generation failed: {exc}"
|
|
item = (data.get("data") or [{}])[0] if isinstance(data, dict) else {}
|
|
try:
|
|
if item.get("b64_json"):
|
|
Path(out_path).write_bytes(base64.b64decode(item["b64_json"]))
|
|
elif item.get("url"):
|
|
img = tls_trust.request("get", item["url"], ca_bundle=ca or None, timeout=_TIMEOUT)
|
|
img.raise_for_status()
|
|
Path(out_path).write_bytes(img.content)
|
|
else:
|
|
return False, "no image returned by the model"
|
|
except Exception as exc: # noqa: BLE001
|
|
return False, f"could not save image: {exc}"
|
|
return True, out_path
|