local-triage 0.0.0-stage → 0.10.0-dev.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +90 -2
- package/THIRD-PARTY-NOTICES.md +48 -0
- package/app/__init__.py +3 -0
- package/app/affiliation.py +137 -0
- package/app/ask.py +300 -0
- package/app/build_info.py +98 -0
- package/app/capec.py +168 -0
- package/app/capec_view.py +27 -0
- package/app/codecmp.py +59 -0
- package/app/config.py +411 -0
- package/app/context.py +1451 -0
- package/app/cvss.py +357 -0
- package/app/db.py +6234 -0
- package/app/defect.py +670 -0
- package/app/defect_check.py +323 -0
- package/app/defect_drafts.py +612 -0
- package/app/defect_dto.py +149 -0
- package/app/defect_fields.py +248 -0
- package/app/defect_form.py +124 -0
- package/app/defect_links.py +107 -0
- package/app/defect_local_text.py +103 -0
- package/app/defect_references.py +164 -0
- package/app/defect_where.py +147 -0
- package/app/ecosystem.py +51 -0
- package/app/executors.py +660 -0
- package/app/feeds.py +1231 -0
- package/app/guard.py +164 -0
- package/app/hub_block_builder.py +200 -0
- package/app/hub_block_version.py +220 -0
- package/app/hub_defects.py +4469 -0
- package/app/hub_effects.py +421 -0
- package/app/hub_entity_versions.py +108 -0
- package/app/hub_import.py +2054 -0
- package/app/hub_onboarding.py +114 -0
- package/app/hub_ops.py +330 -0
- package/app/hub_pull.py +1195 -0
- package/app/hub_state.py +255 -0
- package/app/hub_status.py +237 -0
- package/app/hub_sync.py +619 -0
- package/app/hub_sync_api.py +241 -0
- package/app/hub_sync_compare.py +1054 -0
- package/app/hub_sync_contract.py +75 -0
- package/app/hub_template_model.py +74 -0
- package/app/hub_text_scale.py +159 -0
- package/app/journal.py +250 -0
- package/app/llm.py +850 -0
- package/app/llm_chat.py +596 -0
- package/app/llm_http.py +319 -0
- package/app/llm_messages.py +401 -0
- package/app/llm_net.py +764 -0
- package/app/llm_ollama.py +571 -0
- package/app/lockfile.py +505 -0
- package/app/logsafe.py +54 -0
- package/app/main.py +367 -0
- package/app/mask_display.py +132 -0
- package/app/masking.py +814 -0
- package/app/metrics.py +194 -0
- package/app/model_traffic.py +519 -0
- package/app/models.py +483 -0
- package/app/navigation.py +121 -0
- package/app/osv.py +279 -0
- package/app/package_identity.py +189 -0
- package/app/package_transition.py +824 -0
- package/app/profile_gate.py +71 -0
- package/app/prompts.py +1108 -0
- package/app/provider_keys.py +189 -0
- package/app/providers.py +511 -0
- package/app/routes/__init__.py +10 -0
- package/app/routes/api.py +153 -0
- package/app/routes/capec.py +186 -0
- package/app/routes/common.py +407 -0
- package/app/routes/components.py +1499 -0
- package/app/routes/cwe.py +168 -0
- package/app/routes/decision_selected.py +153 -0
- package/app/routes/defect_add.py +223 -0
- package/app/routes/defect_card_routes.py +363 -0
- package/app/routes/defect_selected.py +342 -0
- package/app/routes/feeds.py +155 -0
- package/app/routes/findings.py +2736 -0
- package/app/routes/hub.py +402 -0
- package/app/routes/hub_bulk.py +288 -0
- package/app/routes/hub_defect_routes.py +1945 -0
- package/app/routes/hub_status_routes.py +1132 -0
- package/app/routes/hub_sync_routes.py +463 -0
- package/app/routes/journal.py +132 -0
- package/app/routes/projects.py +1601 -0
- package/app/routes/providers.py +336 -0
- package/app/routes/references.py +26 -0
- package/app/routes/runs.py +219 -0
- package/app/routes/sca.py +2722 -0
- package/app/routes/settings.py +163 -0
- package/app/run_errors.py +151 -0
- package/app/sca.py +1662 -0
- package/app/sca_contract.py +1289 -0
- package/app/sca_research.py +1089 -0
- package/app/sca_research_net.py +1007 -0
- package/app/sca_text.py +965 -0
- package/app/serve.py +70 -0
- package/app/settings_store.py +262 -0
- package/app/static/app.css +2802 -0
- package/app/static/app.js +3772 -0
- package/app/static/capec-license.txt +9 -0
- package/app/static/favicon.svg +1 -0
- package/app/static/fonts/Golos-Text_DemiBold.woff2 +0 -0
- package/app/static/fonts/Golos-Text_Regular.woff2 +0 -0
- package/app/static/fonts/OFL.txt +90 -0
- package/app/static/navigation.js +234 -0
- package/app/static/observability.js +121 -0
- package/app/static/theme.js +49 -0
- package/app/storage/__init__.py +16 -0
- package/app/storage/local_state.py +246 -0
- package/app/storage/migrate.py +110 -0
- package/app/storage/migrations/postgresql/0001_baseline.sql +896 -0
- package/app/storage/migrations/postgresql/0002_feeds_and_secrets.sql +55 -0
- package/app/storage/migrations/postgresql/0003_project_mask_display.sql +5 -0
- package/app/storage/migrations/postgresql/0004_package_identity.sql +36 -0
- package/app/storage/postgresql.py +573 -0
- package/app/storage/schema.py +619 -0
- package/app/storage/sql.py +57 -0
- package/app/storage/tools.py +254 -0
- package/app/telemetry.py +307 -0
- package/app/templates/base.html +91 -0
- package/app/templates/capec/context.html +3 -0
- package/app/templates/capec/detail.html +27 -0
- package/app/templates/capec/index.html +128 -0
- package/app/templates/components/_archive.html +11 -0
- package/app/templates/components/archived.html +19 -0
- package/app/templates/components/batch.html +35 -0
- package/app/templates/components/detail.html +332 -0
- package/app/templates/components/index.html +338 -0
- package/app/templates/components/research_preview.html +80 -0
- package/app/templates/components/research_run.html +54 -0
- package/app/templates/cwe/bdu.html +56 -0
- package/app/templates/cwe/detail.html +233 -0
- package/app/templates/cwe/index.html +97 -0
- package/app/templates/error.html +8 -0
- package/app/templates/feeds/index.html +65 -0
- package/app/templates/findings/card.html +639 -0
- package/app/templates/findings/decision_selected.html +41 -0
- package/app/templates/findings/defect_add.html +46 -0
- package/app/templates/findings/defect_selected.html +47 -0
- package/app/templates/findings/list.html +153 -0
- package/app/templates/findings/send_selected.html +53 -0
- package/app/templates/findings/trace.html +72 -0
- package/app/templates/journal/detail.html +19 -0
- package/app/templates/journal/index.html +19 -0
- package/app/templates/macros.html +440 -0
- package/app/templates/navigation-fields.html +1 -0
- package/app/templates/navigation.html +11 -0
- package/app/templates/projects/_identity_summary.html +42 -0
- package/app/templates/projects/defect_card.html +153 -0
- package/app/templates/projects/defect_create_preview.html +17 -0
- package/app/templates/projects/defect_fields_diff.html +6 -0
- package/app/templates/projects/defect_fields_form.html +40 -0
- package/app/templates/projects/defect_references.html +17 -0
- package/app/templates/projects/defect_sync_preview.html +28 -0
- package/app/templates/projects/defect_update_actions.html +6 -0
- package/app/templates/projects/defect_update_preview.html +24 -0
- package/app/templates/projects/defects.html +126 -0
- package/app/templates/projects/delete.html +31 -0
- package/app/templates/projects/external.html +61 -0
- package/app/templates/projects/folders.html +64 -0
- package/app/templates/projects/hub_accepted.html +10 -0
- package/app/templates/projects/hub_assignment.html +50 -0
- package/app/templates/projects/hub_pending.html +20 -0
- package/app/templates/projects/hub_pending_list.html +17 -0
- package/app/templates/projects/hub_state_note.html +23 -0
- package/app/templates/projects/hub_step_note.html +5 -0
- package/app/templates/projects/hub_sync.html +105 -0
- package/app/templates/projects/hub_sync_confirm.html +32 -0
- package/app/templates/projects/hub_sync_worker.html +5 -0
- package/app/templates/projects/hub_text_rules.html +64 -0
- package/app/templates/projects/identity_report.html +74 -0
- package/app/templates/projects/list.html +90 -0
- package/app/templates/projects/project.html +433 -0
- package/app/templates/reference-missing.html +14 -0
- package/app/templates/runs/run.html +166 -0
- package/app/templates/sca/card.html +476 -0
- package/app/templates/sca/index.html +321 -0
- package/app/templates/settings/hub_url.html +31 -0
- package/app/templates/settings/index.html +216 -0
- package/app/templates/settings/provider.html +229 -0
- package/app/templates/settings/providers.html +37 -0
- package/app/templates/worklists.html +52 -0
- package/app/textsafe.py +117 -0
- package/app/timing.py +245 -0
- package/app/triage.py +1743 -0
- package/app/view.py +430 -0
- package/app/worklists.py +204 -0
- package/examples/demo/demo-hub-report-v2.xlsx +0 -0
- package/examples/demo/demo-hub-report.xlsx +0 -0
- package/examples/demo/repos/billing-core/pom.xml +30 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/Invoice.java +9 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/InvoiceRepository.java +51 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/InvoiceStatus.java +11 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/config/DatabaseSettings.java +18 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/payment/PaymentTokenService.java +35 -0
- package/examples/demo/repos/billing-core/src/main/java/org/example/billing/web/InvoiceExportController.java +57 -0
- package/examples/demo/repos/catalog-api/catalog_api/__init__.py +1 -0
- package/examples/demo/repos/catalog-api/catalog_api/products.py +24 -0
- package/examples/demo/repos/catalog-api/catalog_api/tasks.py +24 -0
- package/examples/demo/repos/catalog-api/catalog_api/views.py +17 -0
- package/examples/demo/repos/catalog-api/requirements.txt +3 -0
- package/npm/build.json +5 -0
- package/npm/cli.js +346 -0
- package/npm/files.manifest +224 -0
- package/npm/lib/binding.js +64 -0
- package/npm/lib/cluster.js +330 -0
- package/npm/lib/env.js +88 -0
- package/npm/lib/fsx.js +125 -0
- package/npm/lib/http.js +53 -0
- package/npm/lib/metrics.js +73 -0
- package/npm/lib/paths.js +150 -0
- package/npm/lib/pg.js +351 -0
- package/npm/lib/proc.js +101 -0
- package/npm/lib/python.js +133 -0
- package/npm/lib/stand.js +46 -0
- package/npm/lib/supervisor.js +1141 -0
- package/npm/lib/version.js +21 -0
- package/npm/requirements.txt +476 -0
- package/npm-shrinkwrap.json +111 -0
- package/package.json +37 -4
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"""Клиент сервера модели по Messages API (0.9.14, ADR-0106): второй формат внешних провайдеров. Наследует клиент
|
|
2
|
+
API чат-завершений (адреса по классу доверия, разбор статусов, бюджет отправок, поисковый API) и переопределяет
|
|
3
|
+
то, что у протокола другое:
|
|
4
|
+
* `POST /messages`: системное сообщение – отдельным полем `system`, содержимое – блоками (`text`, `tool_use`,
|
|
5
|
+
`tool_result`), `max_tokens` обязателен; секрет ключа – заголовок `x-api-key` (app/provider_keys.key_headers),
|
|
6
|
+
служебный заголовок `anthropic-version` – через protocol_headers() общего транспорта (не секрет);
|
|
7
|
+
* ответ по схеме (chat_json) – инструментом `answer` с `input_schema` и принудительным выбором (режимы `tool`,
|
|
8
|
+
`tool_strict`), либо текстом с извлечением JSON приложением (`none`); проверка приложением всегда;
|
|
9
|
+
* ход с инструментами (chat_turn) – вызовы нормализуются в формат приложения (`function.arguments` – строка JSON),
|
|
10
|
+
результаты одного хода уходят одним сообщением user из блоков `tool_result`, ответ ассистента воспроизводится
|
|
11
|
+
исходными блоками (`_blocks` в сообщении приложения);
|
|
12
|
+
* `stop_reason` → `finish_reason` приложения; `refusal`, `pause_turn`, `model_context_window_exceeded` и
|
|
13
|
+
неизвестные причины – известные ошибки (LlmError.reason) без повторной генерации;
|
|
14
|
+
* рассуждение (`thinking`, `reasoning_effort`) не передаётся; входящие блоки thinking текста не дают и
|
|
15
|
+
воспроизводятся при следующем ходе; неподдерживаемые блоки – известная ошибка;
|
|
16
|
+
* `/models` недоступен (404/405/501) – сервер считается живым, список – из резервного имени модели
|
|
17
|
+
(fallback_model: из формы, профиля или снимка); 401/403 – ошибка ключа как есть.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import json
|
|
23
|
+
import time
|
|
24
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
from app.llm import RAW_KEEP, LlmError, answer_schema, check_answer, estimate_tokens, extract_answer, retry_message
|
|
28
|
+
from app.llm_chat import (MAX_SENDS, ChatCompletionsClient, TurnResult, _scrub_error, _scrub_value, message_tokens,
|
|
29
|
+
track_sent, tracked, usage_stats)
|
|
30
|
+
from app.models import LlmStats
|
|
31
|
+
|
|
32
|
+
API_VERSION = "2023-06-01" # версия Messages API в заголовке anthropic-version (служебный, не секрет)
|
|
33
|
+
RESPONSE_MODES = ("tool", "tool_strict", "none") # формат ответа chat_json по профилю
|
|
34
|
+
RETRY_STATUSES = (429, 500, 503, 529) # 529 overloaded_error – как 429 (один повтор с ожиданием)
|
|
35
|
+
MODELS_UNAVAILABLE = (404, 405, 501) # /models у шлюза нет – список из резервного имени (п. 2.7 плана)
|
|
36
|
+
STOP_MAP = {"end_turn": "stop", "stop_sequence": "stop", "tool_use": "tool_calls", "max_tokens": "length"}
|
|
37
|
+
KNOWN_STOP_ERRORS = {
|
|
38
|
+
"refusal": ("refusal", "модель отказалась отвечать (stop_reason refusal)"),
|
|
39
|
+
"pause_turn": ("pause_turn", "провайдер прервал ход (stop_reason pause_turn): серверные инструменты не "
|
|
40
|
+
"поддерживаются"),
|
|
41
|
+
"model_context_window_exceeded": ("context_window", "запрос не поместился в окно модели (stop_reason "
|
|
42
|
+
"model_context_window_exceeded)"),
|
|
43
|
+
}
|
|
44
|
+
ASSISTANT_BLOCKS = ("text", "tool_use", "thinking", "redacted_thinking")
|
|
45
|
+
ANSWER_TOOL = "answer"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _blocks_of(message: Mapping[str, Any]) -> list[dict[str, Any]]:
|
|
49
|
+
blocks = message.get("_blocks")
|
|
50
|
+
return [b for b in blocks if isinstance(b, dict)] if isinstance(blocks, list) else []
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _text_of(blocks: Iterable[Mapping[str, Any]]) -> str:
|
|
54
|
+
return "\n".join(str(b.get("text") or "") for b in blocks if b.get("type") == "text").strip()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _usage_of(data: Any) -> dict[str, int]:
|
|
58
|
+
raw = data.get("usage") if isinstance(data, dict) and isinstance(data.get("usage"), dict) else {}
|
|
59
|
+
|
|
60
|
+
def count(value: Any) -> int:
|
|
61
|
+
return int(value) if isinstance(value, int) and not isinstance(value, bool) and value > 0 else 0
|
|
62
|
+
return {"prompt_tokens": count(raw.get("input_tokens")), "completion_tokens": count(raw.get("output_tokens")),
|
|
63
|
+
"reasoning_tokens": 0, "cache_hit_tokens": count(raw.get("cache_read_input_tokens"))}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def to_app_message(data: Mapping[str, Any]) -> tuple[dict[str, Any], str]:
|
|
67
|
+
"""Ответ Messages API -> (сообщение ассистента в формате приложения, finish_reason приложения). Известные
|
|
68
|
+
причины-ошибки и неподдерживаемые блоки – LlmError с reason (исход известен)."""
|
|
69
|
+
# ответ получен (HTTP 200): любое нарушение протокола – известный исход (reason), а не «результат неизвестен»;
|
|
70
|
+
# блоки не отбрасываются молча (ревью круг 15, находка 6)
|
|
71
|
+
if not isinstance(data, Mapping):
|
|
72
|
+
raise LlmError("Ответ сервера – не объект: ответ не по протоколу Messages API.", reason="bad_response")
|
|
73
|
+
content = data.get("content")
|
|
74
|
+
if not isinstance(content, list):
|
|
75
|
+
raise LlmError("В ответе сервера нет списка блоков content: ответ не по протоколу Messages API.",
|
|
76
|
+
reason="bad_response")
|
|
77
|
+
if not all(isinstance(b, dict) for b in content):
|
|
78
|
+
raise LlmError("В списке блоков ответа есть элемент, который не является блоком: ответ не по протоколу "
|
|
79
|
+
"Messages API.", reason="bad_response")
|
|
80
|
+
blocks = list(content)
|
|
81
|
+
for b in blocks:
|
|
82
|
+
if b.get("type") not in ASSISTANT_BLOCKS:
|
|
83
|
+
raise LlmError(f"Ответ содержит неподдерживаемый блок «{b.get('type')}»: такие ответы приложение не "
|
|
84
|
+
"разбирает.", reason="unsupported_block")
|
|
85
|
+
if b.get("type") == "text" and not isinstance(b.get("text"), str):
|
|
86
|
+
raise LlmError("Текстовый блок ответа без строки text: ответ не по протоколу Messages API.",
|
|
87
|
+
reason="bad_response")
|
|
88
|
+
stop = str(data.get("stop_reason") or "")
|
|
89
|
+
if stop in KNOWN_STOP_ERRORS:
|
|
90
|
+
reason, text = KNOWN_STOP_ERRORS[stop]
|
|
91
|
+
raise LlmError(f"Модель не ответила: {text}.", detail=stop, reason=reason)
|
|
92
|
+
if stop not in STOP_MAP:
|
|
93
|
+
raise LlmError(f"Неизвестная причина завершения ответа: «{stop or 'нет'}» – ответ не принят.", detail=stop,
|
|
94
|
+
reason="unknown_stop")
|
|
95
|
+
calls = []
|
|
96
|
+
seen: set[str] = set()
|
|
97
|
+
for b in blocks:
|
|
98
|
+
if b.get("type") != "tool_use":
|
|
99
|
+
continue
|
|
100
|
+
call_id, name, payload = str(b.get("id") or ""), str(b.get("name") or ""), b.get("input")
|
|
101
|
+
if not call_id or not name or not isinstance(payload, dict):
|
|
102
|
+
raise LlmError("Вызов инструмента без id, имени или объекта аргументов: ответ не принят.", reason="bad_tool")
|
|
103
|
+
if call_id in seen:
|
|
104
|
+
raise LlmError(f"Повторный идентификатор вызова инструмента «{call_id}»: ответ не принят.", reason="bad_tool")
|
|
105
|
+
seen.add(call_id)
|
|
106
|
+
calls.append({"id": call_id, "type": "function",
|
|
107
|
+
"function": {"name": name, "arguments": json.dumps(payload, ensure_ascii=False)}})
|
|
108
|
+
message: dict[str, Any] = {"role": "assistant", "content": _text_of(blocks), "_blocks": blocks}
|
|
109
|
+
if calls:
|
|
110
|
+
message["tool_calls"] = calls
|
|
111
|
+
return message, STOP_MAP[stop]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def to_wire_messages(messages: Iterable[Mapping[str, Any]]) -> tuple[str, list[dict[str, Any]]]:
|
|
115
|
+
"""История приложения -> (system, messages Messages API): системные сообщения – в поле system; подряд идущие
|
|
116
|
+
сообщения role=tool одного хода – одно сообщение user из блоков tool_result (перед текстом); подряд идущие
|
|
117
|
+
user – одно сообщение; ассистент – исходными блоками (_blocks), иначе из текста и tool_calls."""
|
|
118
|
+
system: list[str] = []
|
|
119
|
+
out: list[dict[str, Any]] = []
|
|
120
|
+
|
|
121
|
+
def push_user(blocks: list[dict[str, Any]]) -> None:
|
|
122
|
+
if out and out[-1]["role"] == "user":
|
|
123
|
+
existing = out[-1]["content"]
|
|
124
|
+
results = [b for b in existing + blocks if b.get("type") == "tool_result"]
|
|
125
|
+
texts = [b for b in existing + blocks if b.get("type") != "tool_result"]
|
|
126
|
+
out[-1]["content"] = results + texts
|
|
127
|
+
else:
|
|
128
|
+
out.append({"role": "user", "content": blocks})
|
|
129
|
+
|
|
130
|
+
for m in messages:
|
|
131
|
+
role = str(m.get("role") or "")
|
|
132
|
+
if role == "system":
|
|
133
|
+
system.append(str(m.get("content") or ""))
|
|
134
|
+
elif role == "tool":
|
|
135
|
+
push_user([{"type": "tool_result", "tool_use_id": str(m.get("tool_call_id") or ""),
|
|
136
|
+
"content": str(m.get("content") or "")}])
|
|
137
|
+
elif role == "user":
|
|
138
|
+
content = m.get("content")
|
|
139
|
+
blocks = [b for b in content if isinstance(b, dict)] if isinstance(content, list) else [
|
|
140
|
+
{"type": "text", "text": str(content or "")}]
|
|
141
|
+
push_user(blocks)
|
|
142
|
+
elif role == "assistant":
|
|
143
|
+
blocks = _blocks_of(m)
|
|
144
|
+
if not blocks:
|
|
145
|
+
text = str(m.get("content") or "")
|
|
146
|
+
blocks = [{"type": "text", "text": text}] if text else []
|
|
147
|
+
for call in m.get("tool_calls") or []:
|
|
148
|
+
if not isinstance(call, dict):
|
|
149
|
+
continue
|
|
150
|
+
fn = call.get("function") or {}
|
|
151
|
+
try:
|
|
152
|
+
payload = json.loads(str(fn.get("arguments") or "{}"))
|
|
153
|
+
except ValueError:
|
|
154
|
+
payload = {}
|
|
155
|
+
blocks.append({"type": "tool_use", "id": str(call.get("id") or ""), "name": str(fn.get("name") or ""),
|
|
156
|
+
"input": payload if isinstance(payload, dict) else {}})
|
|
157
|
+
if blocks:
|
|
158
|
+
out.append({"role": "assistant", "content": blocks})
|
|
159
|
+
else:
|
|
160
|
+
raise LlmError(f"Сообщение с ролью «{role}» протоколу Messages API не передаётся.")
|
|
161
|
+
return "\n\n".join(s for s in system if s), out
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class MessagesClient(ChatCompletionsClient):
|
|
165
|
+
"""Клиент Messages API. Параметры профиля задаёт вызывающий: response_mode (tool, tool_strict, none),
|
|
166
|
+
max_output, context_window, fallback_model; рассуждение не передаётся."""
|
|
167
|
+
|
|
168
|
+
def __init__(self, url: str, timeout: float = 600) -> None:
|
|
169
|
+
super().__init__(url, timeout)
|
|
170
|
+
self.response_mode = "tool"
|
|
171
|
+
self.generate_path = "/messages"
|
|
172
|
+
self.retry_statuses = RETRY_STATUSES # + 529 overloaded_error
|
|
173
|
+
self.protocol_name = "Messages API"
|
|
174
|
+
self.fallback_model = "" # имя модели из формы, профиля или снимка – список при недоступном /models
|
|
175
|
+
self.models_unavailable = False # последний запрос /models ответил 404/405/501
|
|
176
|
+
|
|
177
|
+
def protocol_headers(self) -> dict[str, str]:
|
|
178
|
+
return {"anthropic-version": API_VERSION}
|
|
179
|
+
|
|
180
|
+
# ---------------------------------------------------------------- сведения о сервере
|
|
181
|
+
|
|
182
|
+
def _models(self) -> list[dict[str, Any]]:
|
|
183
|
+
try:
|
|
184
|
+
data = self._request("/models")
|
|
185
|
+
except LlmError as e:
|
|
186
|
+
if e.status in MODELS_UNAVAILABLE:
|
|
187
|
+
self.models_unavailable = True
|
|
188
|
+
name = str(self.fallback_model or "").strip()
|
|
189
|
+
if not name:
|
|
190
|
+
raise LlmError("Список моделей у этого сервера недоступен, а имя модели не задано: укажите имя "
|
|
191
|
+
"модели в профиле.", status=e.status) from None
|
|
192
|
+
return [{"id": name, "source": "profile"}]
|
|
193
|
+
raise
|
|
194
|
+
self.models_unavailable = False
|
|
195
|
+
items = data.get("data") if isinstance(data, dict) else None
|
|
196
|
+
if not isinstance(items, list):
|
|
197
|
+
raise LlmError(f"В ответе /models нет списка моделей: по адресу {self.check_url()}, похоже, не Messages API.")
|
|
198
|
+
return [item for item in items if isinstance(item, dict) and str(item.get("id") or "").strip()]
|
|
199
|
+
|
|
200
|
+
@tracked
|
|
201
|
+
def version(self) -> str:
|
|
202
|
+
models = self._models_once()
|
|
203
|
+
if self.models_unavailable:
|
|
204
|
+
return "Messages API, список моделей недоступен – имя из профиля"
|
|
205
|
+
return f"Messages API, моделей: {len(models)}"
|
|
206
|
+
|
|
207
|
+
@tracked
|
|
208
|
+
def list_models(self) -> list[dict[str, Any]]:
|
|
209
|
+
result = [{"name": str(item["id"]).strip(), "size": 0, "digest": "", "params": "", "quant": "",
|
|
210
|
+
"family": str(item.get("display_name") or ""), "context_length": None, "capabilities": None,
|
|
211
|
+
"source": str(item.get("source") or "server")} for item in self._models_once()]
|
|
212
|
+
return sorted(result, key=lambda x: x["name"].casefold())
|
|
213
|
+
|
|
214
|
+
def unload_model(self, model: str) -> dict[str, Any]:
|
|
215
|
+
raise LlmError("Выгрузка модели у провайдера по Messages API не поддерживается.")
|
|
216
|
+
|
|
217
|
+
# ---------------------------------------------------------------- запрос модели
|
|
218
|
+
|
|
219
|
+
def _body(self, model: str, messages: list[dict[str, Any]], max_tokens: int,
|
|
220
|
+
response_format: dict[str, Any] | None, tools: list[dict[str, Any]] | None,
|
|
221
|
+
tool_choice: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
222
|
+
system, wire = to_wire_messages(messages)
|
|
223
|
+
body: dict[str, Any] = {"model": model, "max_tokens": int(max_tokens), "messages": wire}
|
|
224
|
+
if system:
|
|
225
|
+
body["system"] = system
|
|
226
|
+
if tools:
|
|
227
|
+
body["tools"] = [self._tool(t) for t in tools]
|
|
228
|
+
body["tool_choice"] = tool_choice or {"type": "auto"}
|
|
229
|
+
return body
|
|
230
|
+
|
|
231
|
+
def _answer_tool(self, fields: Mapping[str, str], required: Iterable[str], enums: Mapping[str, Iterable[str]],
|
|
232
|
+
types: Mapping[str, str] | None = None) -> dict[str, Any] | None:
|
|
233
|
+
"""Инструмент answer chat_json по режиму профиля (tool, tool_strict); у режима none – None."""
|
|
234
|
+
if self.response_mode not in ("tool", "tool_strict"):
|
|
235
|
+
return None
|
|
236
|
+
tool: dict[str, Any] = {
|
|
237
|
+
"name": ANSWER_TOOL, "description": "Ответ по правилам системного сообщения: один объект с полями "
|
|
238
|
+
+ ", ".join(fields), "input_schema": answer_schema(fields, list(required),
|
|
239
|
+
{k: list(v) for k, v in enums.items()}, types)}
|
|
240
|
+
if self.response_mode == "tool_strict":
|
|
241
|
+
tool["strict"] = True
|
|
242
|
+
return tool
|
|
243
|
+
|
|
244
|
+
@staticmethod
|
|
245
|
+
def _answer_choice(tool: Mapping[str, Any] | None) -> dict[str, Any] | None:
|
|
246
|
+
return {"type": "tool", "name": ANSWER_TOOL} if tool else None
|
|
247
|
+
|
|
248
|
+
def json_overhead(self, fields: Mapping[str, str], required: Iterable[str], enums: Mapping[str, Iterable[str]],
|
|
249
|
+
types: Mapping[str, str] | None = None) -> int:
|
|
250
|
+
"""Сколько токенов (оценка сверху) chat_json добавит к сообщениям в проверке размера (_check_size):
|
|
251
|
+
инструмент answer и выбор инструмента. Подгонка среза вычитает это из окна профиля (KIMI-01)."""
|
|
252
|
+
tool = self._answer_tool(fields, required, enums, types)
|
|
253
|
+
if tool is None:
|
|
254
|
+
return 0
|
|
255
|
+
return estimate_tokens(json.dumps([[tool], self._answer_choice(tool)], ensure_ascii=False))
|
|
256
|
+
|
|
257
|
+
@staticmethod
|
|
258
|
+
def _tool(tool: Mapping[str, Any]) -> dict[str, Any]:
|
|
259
|
+
"""Инструмент приложения ({"type": "function", "function": {name, description, parameters}}) или уже в
|
|
260
|
+
формате Messages ({name, description, input_schema}) -> формат Messages."""
|
|
261
|
+
if "input_schema" in tool:
|
|
262
|
+
return dict(tool)
|
|
263
|
+
fn = tool.get("function") or {}
|
|
264
|
+
return {"name": str(fn.get("name") or ""), "description": str(fn.get("description") or ""),
|
|
265
|
+
"input_schema": dict(fn.get("parameters") or {"type": "object"})}
|
|
266
|
+
|
|
267
|
+
def _turn(self, model: str, messages: list[dict[str, Any]], max_tokens: int, tools: list[dict[str, Any]] | None,
|
|
268
|
+
tool_choice: dict[str, Any] | None, budget: dict[str, Any],
|
|
269
|
+
allowed: Iterable[str] | None = None) -> tuple[dict[str, Any], str, dict[str, int]]:
|
|
270
|
+
"""Одна отправка с бюджетом: (сообщение приложения, finish_reason, usage). Известные ошибки после ответа
|
|
271
|
+
несут usage в stats. allowed – имена инструментов хода исследования (вызов другого – bad_tool)."""
|
|
272
|
+
self._check_size(messages, max_tokens, [tools, tool_choice])
|
|
273
|
+
data = self._post(self._body(model, messages, max_tokens, None, tools, tool_choice), budget)
|
|
274
|
+
usage = _usage_of(data)
|
|
275
|
+
try:
|
|
276
|
+
message, finish = to_app_message(data)
|
|
277
|
+
if allowed is not None: # ход исследования: инструмент вне переданного набора – известная ошибка (п. 2.5)
|
|
278
|
+
unknown = sorted({c["function"]["name"] for c in message.get("tool_calls") or []} - set(allowed))
|
|
279
|
+
if unknown:
|
|
280
|
+
raise LlmError(f"Модель вызвала инструмент, которого нет в запросе: {', '.join(unknown)} – ответ не "
|
|
281
|
+
"принят.", reason="bad_tool")
|
|
282
|
+
except LlmError as e:
|
|
283
|
+
if e.stats is None:
|
|
284
|
+
e.stats = usage_stats(model, usage, budget["sends"])
|
|
285
|
+
raise
|
|
286
|
+
return message, finish, usage
|
|
287
|
+
|
|
288
|
+
def chat_turn(self, model: str, messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None = None,
|
|
289
|
+
max_tokens: int | None = None, response_format: dict[str, Any] | None = None,
|
|
290
|
+
max_sends: int | None = None, deadline: float | None = None,
|
|
291
|
+
allow_http_retry: bool = True) -> TurnResult:
|
|
292
|
+
"""Один ход диалога с инструментами (исследование, ADR-0105): выбор инструмента – за моделью (auto)."""
|
|
293
|
+
limit = int(max_tokens or self.max_output)
|
|
294
|
+
own = time.monotonic() + self.timeout
|
|
295
|
+
budget = {"sends": 0, "http_retried": not allow_http_retry, "http_retries": 0,
|
|
296
|
+
"deadline": min(own, deadline) if deadline else own,
|
|
297
|
+
"max_sends": max(1, min(MAX_SENDS, int(max_sends))) if max_sends else MAX_SENDS}
|
|
298
|
+
started = time.monotonic()
|
|
299
|
+
try:
|
|
300
|
+
with track_sent():
|
|
301
|
+
message, finish, usage = self._turn(str(model), messages, limit, tools, None, budget,
|
|
302
|
+
allowed=[self._tool(t)["name"] for t in tools or []])
|
|
303
|
+
except LlmError as e:
|
|
304
|
+
e.sends = budget["sends"]
|
|
305
|
+
e.http_retries = budget["http_retries"] # повтор транспорта внутри хода был – на строку он один (К3)
|
|
306
|
+
raise _scrub_error(e, self._secrets) from None
|
|
307
|
+
return TurnResult(message=message, finish_reason=finish, usage=usage,
|
|
308
|
+
seconds=round(time.monotonic() - started, 3), sends=budget["sends"],
|
|
309
|
+
http_retries=budget["http_retries"])
|
|
310
|
+
|
|
311
|
+
def chat_json(self, model: str, messages: list[dict[str, Any]], fields: Mapping[str, str],
|
|
312
|
+
required: Iterable[str], enums: Mapping[str, Iterable[str]], options: Mapping[str, Any] | None = None,
|
|
313
|
+
think: Any = None, types: Mapping[str, str] | None = None,
|
|
314
|
+
validate: Callable[[dict[str, Any]], Iterable[str]] | None = None, progress=None,
|
|
315
|
+
json_schema: str = "", max_tokens: int | None = None) -> tuple[dict[str, Any], LlmStats]:
|
|
316
|
+
"""Ответ объектом JSON – тот же контракт, что у Ollama и чат-завершений. Режим tool/tool_strict – инструмент
|
|
317
|
+
answer с принудительным выбором, проверка приложением; none – текст и извлечение JSON. Предел ответа –
|
|
318
|
+
max_output профиля (снимка) или явный max_tokens вызывающего; options.num_predict – параметр Ollama, здесь не
|
|
319
|
+
применяется (ADR-0106 п. 9)."""
|
|
320
|
+
name = str(model or "").strip()
|
|
321
|
+
required = list(required)
|
|
322
|
+
enums = {k: list(v) for k, v in enums.items()}
|
|
323
|
+
limit = int(max_tokens or self.max_output)
|
|
324
|
+
tool = self._answer_tool(fields, required, enums, types)
|
|
325
|
+
use_tool = tool is not None
|
|
326
|
+
history = [dict(m) for m in messages]
|
|
327
|
+
budget = {"sends": 0, "http_retried": False, "deadline": time.monotonic() + self.timeout}
|
|
328
|
+
started = time.monotonic()
|
|
329
|
+
totals = {"prompt_tokens": 0, "completion_tokens": 0, "reasoning_tokens": 0, "cache_hit_tokens": 0}
|
|
330
|
+
|
|
331
|
+
def stats() -> LlmStats:
|
|
332
|
+
return LlmStats(model=name, prompt_tokens=totals["prompt_tokens"], output_tokens=totals["completion_tokens"],
|
|
333
|
+
eval_tps=0.0, prompt_tps=0.0, total_s=round(time.monotonic() - started, 3), load_s=0.0,
|
|
334
|
+
attempts=budget["sends"], think="-", reasoning_tokens=0,
|
|
335
|
+
cache_hit_tokens=totals["cache_hit_tokens"], timing="app")
|
|
336
|
+
|
|
337
|
+
try:
|
|
338
|
+
with track_sent():
|
|
339
|
+
while True:
|
|
340
|
+
try:
|
|
341
|
+
message, finish, usage = self._turn(name, history, limit, [tool] if tool else None,
|
|
342
|
+
self._answer_choice(tool), budget)
|
|
343
|
+
except LlmError as e:
|
|
344
|
+
if e.stats is not None:
|
|
345
|
+
totals["prompt_tokens"] += int(e.stats.prompt_tokens or 0)
|
|
346
|
+
totals["completion_tokens"] += int(e.stats.output_tokens or 0)
|
|
347
|
+
totals["cache_hit_tokens"] += int(e.stats.cache_hit_tokens or 0)
|
|
348
|
+
e.stats = stats()
|
|
349
|
+
raise
|
|
350
|
+
for key in totals:
|
|
351
|
+
totals[key] += usage[key]
|
|
352
|
+
calls = [c for c in message.get("tool_calls") or []]
|
|
353
|
+
answers = [c for c in calls if c["function"]["name"] == ANSWER_TOOL]
|
|
354
|
+
content = str(message.get("content") or "")
|
|
355
|
+
obj: dict[str, Any] | None = None
|
|
356
|
+
problems: list[str] = []
|
|
357
|
+
if use_tool:
|
|
358
|
+
if answers:
|
|
359
|
+
parsed = json.loads(answers[0]["function"]["arguments"])
|
|
360
|
+
obj = parsed if isinstance(parsed, dict) else None
|
|
361
|
+
if obj is None:
|
|
362
|
+
problems = ["аргумент инструмента answer – не объект"]
|
|
363
|
+
else:
|
|
364
|
+
problems = ["ответ нужен вызовом инструмента answer, а не текстом"
|
|
365
|
+
if not calls else "вызван не тот инструмент: нужен answer"]
|
|
366
|
+
else:
|
|
367
|
+
obj, why = extract_answer(content, [*fields, *required])
|
|
368
|
+
if obj is None:
|
|
369
|
+
problems = [why or ("пустой ответ" if not content.strip() else "в ответе нет объекта JSON")]
|
|
370
|
+
if obj is not None and not problems:
|
|
371
|
+
obj, problems = check_answer(obj, required, enums, types)
|
|
372
|
+
if not problems and validate is not None:
|
|
373
|
+
problems = [str(p) for p in validate(obj) if str(p).strip()]
|
|
374
|
+
if not problems and obj is not None:
|
|
375
|
+
return _scrub_value(obj, self._secrets), stats()
|
|
376
|
+
raw = content if not use_tool else (answers[0]["function"]["arguments"] if answers else content)
|
|
377
|
+
if finish == "length":
|
|
378
|
+
raise LlmError(f"Ответ модели оборван: исчерпан предел длины ответа ({limit} токенов). "
|
|
379
|
+
"Увеличьте предел ответа в профиле.", stats=stats(), reason="length", raw=raw[:RAW_KEEP])
|
|
380
|
+
if budget["sends"] >= MAX_SENDS:
|
|
381
|
+
raise LlmError(f"Модель ответила не по формату: {'; '.join(problems[:5])}.", stats=stats(), reason="bad_format",
|
|
382
|
+
raw=raw[:RAW_KEEP])
|
|
383
|
+
note = retry_message(problems, fields, enums)
|
|
384
|
+
if use_tool:
|
|
385
|
+
results = [{"type": "tool_result", "tool_use_id": c["id"],
|
|
386
|
+
"content": note if c is (answers[0] if answers else None) else "не использован"}
|
|
387
|
+
for c in calls]
|
|
388
|
+
history = [dict(m) for m in messages] + [message, {"role": "user", "content": results + [
|
|
389
|
+
{"type": "text", "text": "Исправьте ответ вызовом инструмента answer: " + note}]}]
|
|
390
|
+
else:
|
|
391
|
+
history = [dict(m) for m in messages] + [{"role": "assistant", "content": content[:RAW_KEEP]},
|
|
392
|
+
{"role": "user", "content": note}]
|
|
393
|
+
except LlmError as e:
|
|
394
|
+
if e.stats is None:
|
|
395
|
+
e.stats = stats()
|
|
396
|
+
raise _scrub_error(e, self._secrets) from None
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
__all__ = ["API_VERSION", "MessagesClient", "RESPONSE_MODES", "RETRY_STATUSES", "to_app_message", "to_wire_messages",
|
|
401
|
+
"message_tokens"]
|