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>
156 lines
5.5 KiB
Python
156 lines
5.5 KiB
Python
"""Low-level MCP stdio adapter around the transport-agnostic Project Context core."""
|
|
|
|
# ruff: noqa: UP045 -- Optional keeps the template importable with Pydantic on Python 3.9.
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any, Optional
|
|
from uuid import uuid4
|
|
|
|
from pydantic import ValidationError
|
|
|
|
from .foundation import (
|
|
DispatchResult,
|
|
ProjectContextRuntime,
|
|
ProviderError,
|
|
error_result,
|
|
)
|
|
from .registry import TOOLS_BY_NAME, tool_declarations
|
|
from .runtime import default_runtime, require_supported_python
|
|
|
|
|
|
def dispatch(
|
|
name: str,
|
|
arguments: dict[str, Any],
|
|
runtime: ProjectContextRuntime,
|
|
) -> DispatchResult:
|
|
"""Validate → authorize → resolve provider → execute → validate output."""
|
|
correlation_id = str(uuid4())
|
|
tool = TOOLS_BY_NAME.get(name)
|
|
if tool is None:
|
|
return error_result(
|
|
"NOT_FOUND",
|
|
category="NOT_FOUND",
|
|
retryable=False,
|
|
message="The requested MCP tool is not registered.",
|
|
suggested_action="Refresh the tool list and choose one of the advertised tools.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
|
|
try:
|
|
validated_input = tool.input_model.model_validate(arguments or {})
|
|
except ValidationError:
|
|
return error_result(
|
|
"INVALID_INPUT",
|
|
category="INVALID_INPUT",
|
|
retryable=False,
|
|
message="The tool arguments do not match the published input contract.",
|
|
suggested_action="Correct the required fields and value bounds, then call again.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
|
|
project_id = str(validated_input.project_id)
|
|
if not runtime.policy.decide(runtime.identity, name, project_id):
|
|
return error_result(
|
|
"DENIED",
|
|
category="DENIED",
|
|
retryable=False,
|
|
message="The project is outside the caller's approved scope.",
|
|
suggested_action="Use an approved project or ask the project owner for access.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
|
|
try:
|
|
provider = runtime.credential_resolver.resolve(runtime.identity, name)
|
|
raw_output = tool.handler(validated_input, provider)
|
|
except ProviderError as exc:
|
|
return error_result(
|
|
exc.code,
|
|
category=exc.code,
|
|
retryable=exc.retryable,
|
|
message=exc.safe_message,
|
|
suggested_action="Check the approved provider configuration and retry if allowed.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
except Exception: # noqa: BLE001 - provider failures must not crash or leak into the agent turn
|
|
return error_result(
|
|
"UPSTREAM_ERROR",
|
|
category="UPSTREAM_ERROR",
|
|
retryable=False,
|
|
message="The approved provider could not complete the request.",
|
|
suggested_action="Check the correlation ID in server logs; do not resend credentials.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
|
|
try:
|
|
output_with_trace = {**raw_output, "correlation_id": correlation_id}
|
|
validated_output = tool.output_model.model_validate(output_with_trace)
|
|
except ValidationError:
|
|
return error_result(
|
|
"UPSTREAM_ERROR",
|
|
category="UPSTREAM_ERROR",
|
|
retryable=False,
|
|
message="The provider response did not match the published output contract.",
|
|
suggested_action="Fix the provider mapping before retrying the request.",
|
|
correlation_id=correlation_id,
|
|
)
|
|
return DispatchResult(ok=True, payload=validated_output.model_dump(mode="json"))
|
|
|
|
|
|
def build_server(runtime: Optional[ProjectContextRuntime] = None):
|
|
"""Dựng máy chủ MCP Project Context.
|
|
|
|
``runtime`` để trống thì lấy bộ mặc định đọc từ biến môi trường; test
|
|
truyền vào bộ giả để không cần cấu hình thật.
|
|
"""
|
|
from mcp import types
|
|
from mcp.server.lowlevel import Server
|
|
|
|
require_supported_python()
|
|
app_runtime = runtime or default_runtime()
|
|
app = Server("project_context")
|
|
|
|
@app.list_tools()
|
|
async def list_tools() -> list[types.Tool]:
|
|
"""Trả về khai báo của mọi tool đã đăng ký."""
|
|
return [types.Tool(**declaration) for declaration in tool_declarations()]
|
|
|
|
@app.call_tool()
|
|
async def call_tool(name: str, arguments: dict[str, Any]) -> types.CallToolResult:
|
|
"""Chạy một tool và trả kết quả.
|
|
|
|
Luôn kèm payload dạng văn bản JSON; chỉ khi thành công mới đính thêm
|
|
``structuredContent``, còn lỗi thì bật ``isError``.
|
|
"""
|
|
result = dispatch(name, arguments or {}, app_runtime)
|
|
return types.CallToolResult(
|
|
content=[types.TextContent(
|
|
type="text",
|
|
text=json.dumps(result.payload, ensure_ascii=False, separators=(",", ":")),
|
|
)],
|
|
structuredContent=result.payload if result.ok else None,
|
|
isError=not result.ok,
|
|
)
|
|
|
|
return app
|
|
|
|
|
|
def main() -> None:
|
|
"""Điểm vào khi chạy như tiến trình con: phục vụ MCP qua stdio."""
|
|
import anyio
|
|
from mcp.server.stdio import stdio_server
|
|
|
|
app = build_server()
|
|
|
|
async def _run() -> None:
|
|
"""Vòng lặp phục vụ, đọc/ghi trên stdio cho tới khi tiến trình cha đóng."""
|
|
async with stdio_server() as (read, write):
|
|
await app.run(read, write, app.create_initialization_options())
|
|
|
|
anyio.run(_run)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|