@hfyjq/crannia 1.0.0 → 1.1.0
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 +201 -201
- package/NOTICE +45 -45
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON.md +162 -153
- package/THIRD-PARTY.md +316 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +671 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/skill_md.py +676 -676
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +881 -881
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +813 -813
- package/channels/telegram_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/media.py +231 -231
- package/channels/textfmt.py +58 -58
- package/channels/wechat_channel/__init__.py +1 -1
- package/channels/wechat_channel/auth.py +23 -5
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/config.yaml +2 -2
- package/core/activity.py +3 -0
- package/core/agent_loop.py +390 -55
- package/core/approval.py +17 -6
- package/core/artifacts.py +7 -0
- package/core/auto_review.py +23 -5
- package/core/channel_configs.py +34 -2
- package/core/channel_turns.py +759 -759
- package/core/child_approval.py +150 -0
- package/core/config_requirements.py +205 -0
- package/core/cron/runner.py +118 -6
- package/core/cron/store.py +14 -5
- package/core/cron/tools.py +17 -5
- package/core/endpoint.py +141 -0
- package/core/events.py +23 -0
- package/core/i18n.py +329 -329
- package/core/ice/store.py +191 -191
- package/core/inbound.py +193 -193
- package/core/jobs.py +19 -3
- package/core/last_good.py +150 -150
- package/core/llm_errors.py +227 -227
- package/core/llm_retry.py +230 -230
- package/core/memory/compressor.py +1214 -1214
- package/core/memory/manager.py +14 -0
- package/core/model_configs.py +80 -0
- package/core/pipeline/scheduler.py +2 -2
- package/core/pipeline/stages/agent_loop.py +24 -4
- package/core/pipeline/stages/pre_process.py +58 -7
- package/core/prompt_assembler.py +680 -680
- package/core/rg.py +255 -255
- package/core/security.py +1544 -1544
- package/core/settings_baseline.py +70 -1
- package/core/shell.py +5 -17
- package/core/skill_settings.py +7 -2
- package/core/state_registry.py +19 -0
- package/core/subagent.py +64 -3
- package/core/task_tool.py +1 -1
- package/core/tasks_notify.py +69 -4
- package/core/tiers.py +9 -3
- package/core/tool_outcome.py +108 -0
- package/core/update_tui.py +158 -0
- package/core/version.py +2 -2
- package/core/web_cache.py +152 -0
- package/crannia_tray.py +278 -278
- package/creators/models.json +4 -4
- package/data/settings.example.json +67 -0
- package/licenses/GPL-3.0.txt +619 -619
- package/licenses/LGPL-2.1.txt +502 -502
- package/licenses/LGPL-3.0.txt +165 -165
- package/licenses/MIT-chat-on-wechat.txt +18 -18
- package/licenses/MIT-deepseek-harness.txt +21 -21
- package/licenses/Molio-LICENSE.txt +44 -44
- package/locales/en.json +9 -4
- package/main.py +154 -154
- package/mcpx/manager.py +522 -522
- package/mcpx/servers/__init__.py +22 -22
- package/mcpx/servers/web_search.py +321 -33
- package/package.json +14 -9
- package/providers/anthropic/client.py +772 -772
- package/providers/cost_table.json +3779 -3779
- package/providers/deepseek/client.py +20 -20
- package/providers/deepseek/manifest.yaml +2 -2
- package/providers/factory.py +136 -136
- package/providers/lmstudio/client.py +2 -2
- package/providers/metadata.py +80 -80
- package/providers/model_capabilities.json +6665 -6665
- package/providers/moonshot/client.py +12 -12
- package/providers/ollama/client.py +2 -2
- package/providers/openai/client.py +13 -13
- package/providers/openrouter/client.py +2 -2
- package/providers/qwen/__init__.py +1 -1
- package/providers/qwen/client.py +2 -2
- package/providers/qwen/manifest.yaml +16 -16
- package/providers/rate_limiter.py +144 -144
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/window_table.json +8909 -8909
- package/sbom.json +4413 -4296
- package/skills/entertainment/SKILL.md +35 -35
- package/skills/entertainment/__init__.py +1 -1
- package/skills/entertainment/handler.py +91 -91
- package/skills/entertainment/manifest.yaml +26 -26
- package/skills/file_parser/GUIDANCE.md +10 -10
- package/skills/file_parser/handler.py +177 -4
- package/skills/file_parser/manifest.yaml +22 -1
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/github/manifest.yaml +5 -0
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +806 -806
- package/skills/mail_parser/manifest.yaml +5 -0
- package/skills/media_gen/GUIDANCE.md +19 -19
- package/skills/media_gen/SKILL.md +43 -43
- package/skills/media_gen/__init__.py +1 -1
- package/skills/media_gen/handler.py +430 -430
- package/skills/media_gen/manifest.yaml +93 -88
- package/skills/office_parser/cell_validation.py +127 -127
- package/skills/office_parser/excel_transform.py +936 -936
- package/skills/office_parser/layout_spec.py +243 -243
- package/skills/office_parser/pptx_export/console_encoding.py +27 -27
- package/skills/office_parser/pptx_export/resource_paths.py +88 -88
- package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
- package/skills/office_parser/pptx_export/svg_finalize/align_embed_images.py +575 -575
- package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
- package/skills/office_parser/pptx_export/svg_finalize/embed_icons.py +498 -498
- package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
- package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
- package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
- package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
- package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/elements.py +2490 -2490
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/styles.py +656 -656
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/media.py +141 -141
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
- package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
- package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
- package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
- package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
- package/skills/office_parser/references/layout_library.md +816 -816
- package/skills/office_parser/references/ppt-master/LICENSE +21 -21
- package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
- package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
- package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
- package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
- package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
- package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
- package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
- package/skills/office_parser/references/spec_lock.md +235 -235
- package/skills/office_parser/soffice.py +68 -68
- package/skills/office_parser/word_tools.py +2909 -2909
- package/tui/crannia.cmd +134 -0
- package/tui/crannia_tui/__init__.py +1 -0
- package/tui/crannia_tui/__main__.py +223 -0
- package/tui/crannia_tui/app.py +1318 -0
- package/tui/crannia_tui/app.tcss +153 -0
- package/tui/crannia_tui/approval.py +85 -0
- package/tui/crannia_tui/backend.py +279 -0
- package/tui/crannia_tui/channel.py +472 -0
- package/tui/crannia_tui/client.py +125 -0
- package/tui/crannia_tui/clipboard.py +62 -0
- package/tui/crannia_tui/commands.py +65 -0
- package/tui/crannia_tui/doctor.py +304 -0
- package/tui/crannia_tui/events.py +84 -0
- package/tui/crannia_tui/headless.py +152 -0
- package/tui/crannia_tui/i18n.py +164 -0
- package/tui/crannia_tui/locales/en.json +263 -0
- package/tui/crannia_tui/outcome.py +62 -0
- package/tui/crannia_tui/paths.py +181 -0
- package/tui/crannia_tui/probe.py +53 -0
- package/tui/crannia_tui/profile.py +169 -0
- package/tui/crannia_tui/render.py +209 -0
- package/tui/crannia_tui/settings.py +134 -0
- package/tui/crannia_tui/setup.py +380 -0
- package/tui/crannia_tui/term.py +116 -0
- package/tui/crannia_tui/ui.py +1236 -0
- package/tui/crannia_tui/update.py +389 -0
- package/vendor/ripgrep/COPYING.txt +3 -3
- package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
- package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
- package/vendor/ripgrep/README.md +51 -51
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/package-source.json +19 -19
- package/web/__main__.py +15 -0
- package/web/server.py +168 -19
- package/web/static/img/channel-telegram.svg +1 -1
- package/web/static/img/channel-wechat.svg +1 -1
- package/web/templates/logs.html +49 -49
- package/web/templates/models.html +46 -46
package/core/llm_errors.py
CHANGED
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
"""上游模型报错的用户面 —— 别让用户看到一行 `error`。
|
|
2
|
-
|
|
3
|
-
## 为什么要有这一份
|
|
4
|
-
|
|
5
|
-
网络断了一下时,聊天里不该出现:
|
|
6
|
-
|
|
7
|
-
(error: Connection error.)
|
|
8
|
-
|
|
9
|
-
它看起来像模型说的话,既没说"这一轮什么都没产出",也没说"下一步做什么";
|
|
10
|
-
而 `task.end.ok` 还可能被记成 true(流式那条会把错误吞成正文)。
|
|
11
|
-
|
|
12
|
-
## 三条判据
|
|
13
|
-
|
|
14
|
-
1. 说人话 + 说下一步:每一类都给「发生了什么 / 这一轮有没有产出 /
|
|
15
|
-
现在可以做什么」。原始错误放在最后一行(诊断要留,但别当头一句)。
|
|
16
|
-
2. 同步与流式走同一个产者:`explain()` 是唯一一处。
|
|
17
|
-
两条路各自内联一份的话,同一个故障会有两种表现。
|
|
18
|
-
3. 失败要被记成失败:流式那条在正文前面打一个 `MARK`,由
|
|
19
|
-
`AgentLoopStage` 剥掉并写 `ctx.metadata["error"]`,`task.end.ok=false`。
|
|
20
|
-
标记只在进程内的一跳上存在(stage 之后就没有了),用户看不到它。
|
|
21
|
-
|
|
22
|
-
## 分类
|
|
23
|
-
|
|
24
|
-
`classify_api_error` 管三类按报文的(overflow / auth / rate_limit);
|
|
25
|
-
连接与超时按异常类型判(`classify_transport_error`)。两类正交,这里合并。
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
from __future__ import annotations
|
|
29
|
-
|
|
30
|
-
__all__ = ["MARK", "HEADLINES", "headline", "explain", "strip_mark", "is_marked"]
|
|
31
|
-
|
|
32
|
-
#: 进程内标记 —— 不是给用户看的。`AgentLoopStage` 见到它就把正文剥掉、
|
|
33
|
-
#: 并把 `ctx.metadata["error"]` 写上(`task.end.ok` 因此是 false)。
|
|
34
|
-
MARK = "\x00[LLM_ERROR]\x00"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def is_marked(text) -> bool:
|
|
38
|
-
return str(text or "").startswith(MARK)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def strip_mark(text) -> str:
|
|
42
|
-
s = str(text or "")
|
|
43
|
-
return s[len(MARK):] if s.startswith(MARK) else s
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def _host_of(base_url: str) -> str:
|
|
47
|
-
"""从 base_url 取一个人能认的主机名(取不到就原样回)。"""
|
|
48
|
-
u = str(base_url or "").strip()
|
|
49
|
-
if not u:
|
|
50
|
-
return ""
|
|
51
|
-
try:
|
|
52
|
-
from urllib.parse import urlparse
|
|
53
|
-
host = urlparse(u).hostname or ""
|
|
54
|
-
return host or u
|
|
55
|
-
except Exception: # noqa: BLE001
|
|
56
|
-
return u
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def _upstream_error_message(text: str) -> str:
|
|
60
|
-
"""从 400 的原文里抠出 `error.message`。抠不到回空串。
|
|
61
|
-
|
|
62
|
-
形状(OpenAI SDK 的 `str(exc)`,DeepSeek 的原话)::
|
|
63
|
-
|
|
64
|
-
Error code: 400 - {'error': {'message': 'The supported API model names are
|
|
65
|
-
deepseek-flash, deepseek-v4-pro, but you passed deepseek-v4.1-flash',
|
|
66
|
-
'type': 'invalid_request_error', 'param': None,
|
|
67
|
-
'code': 'invalid_request_error'}}
|
|
68
|
-
|
|
69
|
-
只认结构不猜语义:`ast.literal_eval` 解不开就当没抠到(调用方退回旧形状)。
|
|
70
|
-
用户的报错原文是模型之外的人写的,所以这里只做解析、不回显整段。
|
|
71
|
-
"""
|
|
72
|
-
import ast
|
|
73
|
-
import re as _re
|
|
74
|
-
m = _re.search(r"\{.*\}", text, _re.S)
|
|
75
|
-
if not m:
|
|
76
|
-
return ""
|
|
77
|
-
try:
|
|
78
|
-
data = ast.literal_eval(m.group(0))
|
|
79
|
-
except Exception: # noqa: BLE001
|
|
80
|
-
return ""
|
|
81
|
-
if not isinstance(data, dict):
|
|
82
|
-
return ""
|
|
83
|
-
err = data.get("error")
|
|
84
|
-
if isinstance(err, dict) and isinstance(err.get("message"), str):
|
|
85
|
-
return err["message"].strip()
|
|
86
|
-
if isinstance(data.get("message"), str):
|
|
87
|
-
return str(data["message"]).strip()
|
|
88
|
-
return ""
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
#: 每一类的一句话。横幅与聊天正文共用这张表(一处定义)。
|
|
92
|
-
#:
|
|
93
|
-
#: 措辞按"用户下一步能做什么"写:充值 / 等一会儿 / 换 key / 压缩 / 换说法。
|
|
94
|
-
#: 连接与超时那两句保持原样(横幅上一直是这两句,用户已经认得)。
|
|
95
|
-
HEADLINES: dict[str, str] = {
|
|
96
|
-
"balance": "API余额不足,请检查账号资金",
|
|
97
|
-
"request": "请求参数异常",
|
|
98
|
-
"rate_limit": "API请求频率超限,请稍后重试",
|
|
99
|
-
"server_fault": "上游服务器故障",
|
|
100
|
-
"server_busy": "上游服务器繁忙,请稍后重试",
|
|
101
|
-
"server_other": "上游异常",
|
|
102
|
-
"content": "上游拒绝请求:含有非法内容",
|
|
103
|
-
"region": "上游异常:当前地区不支持服务",
|
|
104
|
-
"too_large": "请求错误:消息过大,尝试压缩或重启应用以继续对话",
|
|
105
|
-
"auth": "api密钥错误,请前往端配置页检查您的api可用性",
|
|
106
|
-
"connection": "网络连接异常--请检查宽带互联网连接",
|
|
107
|
-
"timeout": "网络连接超时——请检查网络或代理",
|
|
108
|
-
"context_overflow": "请求过长,超出模型上下文",
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def kind_of(err, kind: str = "", exc_name: str = "") -> str:
|
|
113
|
-
"""归一成一张表里的类别名。认不出回空串。
|
|
114
|
-
|
|
115
|
-
判类的地方只有一处(`providers/base.py` 那两个函数);这里只做兜底,
|
|
116
|
-
好让 `explain()`/`headline()` 在没拿到 kind 时也说得出话。
|
|
117
|
-
"""
|
|
118
|
-
k = str(kind or "").strip().lower()
|
|
119
|
-
if k:
|
|
120
|
-
return k
|
|
121
|
-
try:
|
|
122
|
-
from providers.base import classify_api_error, classify_transport_error
|
|
123
|
-
except Exception: # noqa: BLE001
|
|
124
|
-
return ""
|
|
125
|
-
try:
|
|
126
|
-
return (classify_api_error(err)
|
|
127
|
-
or classify_transport_error(err)
|
|
128
|
-
or "")
|
|
129
|
-
except Exception: # noqa: BLE001
|
|
130
|
-
return ""
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def headline(err, *, kind: str = "", exc_name: str = "") -> str:
|
|
134
|
-
"""横幅上那一句(短、不含原文)。
|
|
135
|
-
|
|
136
|
-
认不出的类别不装懂:退到 400 的两种形状,再退到"这一轮出错了"。
|
|
137
|
-
"""
|
|
138
|
-
k = kind_of(err, kind=kind, exc_name=exc_name)
|
|
139
|
-
if k in HEADLINES:
|
|
140
|
-
return HEADLINES[k]
|
|
141
|
-
return ""
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def _model_name_mismatch(reason: str):
|
|
145
|
-
"""上游说的是不是「模型名填错了」?是就回 `(支持的名字, 你填的)`。
|
|
146
|
-
|
|
147
|
-
判据用的是上游的原句:``The supported API model names are A, B,
|
|
148
|
-
but you passed C``。这句比任何本地规则都准 —— 它带着上游此刻认的名字。
|
|
149
|
-
"""
|
|
150
|
-
import re as _re
|
|
151
|
-
m = _re.search(r"supported API model names are (.+?),?\s*but you passed\s+([^\s,]+)",
|
|
152
|
-
reason, _re.I)
|
|
153
|
-
if not m:
|
|
154
|
-
return None
|
|
155
|
-
return m.group(1).strip().rstrip(","), m.group(2).strip("'\"`.")
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
def explain(err, *, kind: str = "", model: str = "", base_url: str = "",
|
|
159
|
-
stage: str = "", exc_name: str = "") -> str:
|
|
160
|
-
"""把一次上游/内部异常变成用户能照着做的一段话。
|
|
161
|
-
|
|
162
|
-
*err* 可以是异常对象,也可以是 `str`(流式那条拿到的是字符串)。
|
|
163
|
-
*kind* 优先于关键词(`connection` / `timeout` / `context_overflow` /
|
|
164
|
-
`auth` / `rate_limit`);没给就按原文猜(与 `classify_api_error` 同口径)。
|
|
165
|
-
"""
|
|
166
|
-
text = str(err or "")
|
|
167
|
-
name = str(exc_name or "") or type(err).__name__
|
|
168
|
-
#: 判类只有一处(`providers/base.py`)。这里不再抄第二张表 ——
|
|
169
|
-
#: 抄一份就会与它各自漂,而用户看到的正是这第二份。
|
|
170
|
-
k = kind_of(err, kind=kind, exc_name=exc_name)
|
|
171
|
-
|
|
172
|
-
where = _host_of(base_url)
|
|
173
|
-
tail = "\n\n原始错误:%s" % (text[:300] if text else "(上游没给原文)")
|
|
174
|
-
|
|
175
|
-
if k == "connection":
|
|
176
|
-
head = ("⚠️ **没能连上模型服务**" + ("(%s)" % where if where else "")
|
|
177
|
-
+ (",模型 `%s`" % model if model else "") + "。\n\n"
|
|
178
|
-
"**这一轮没有产生任何结果** —— 没有调用工具,也没有花掉模型额度。\n\n"
|
|
179
|
-
"可能原因:网络断开 / 代理或 VPN / 上游临时故障。")
|
|
180
|
-
steps = ("\n- 直接**重发这条消息**重试(客户端已经自动重试过几轮了);"
|
|
181
|
-
"\n- 连续失败:先看网络,或在「端配置」换一条模型。")
|
|
182
|
-
return head + steps + tail
|
|
183
|
-
|
|
184
|
-
if k == "timeout":
|
|
185
|
-
head = ("⚠️ **模型服务超时**" + ("(%s)" % where if where else "") + "。\n\n"
|
|
186
|
-
"**这一轮没有产生任何结果。**")
|
|
187
|
-
steps = ("\n- 直接重发一次;连续超时:检查网络 / 代理,"
|
|
188
|
-
"或在「端配置」换一条模型。")
|
|
189
|
-
return head + steps + tail
|
|
190
|
-
|
|
191
|
-
if k == "context_overflow":
|
|
192
|
-
return ("⚠️ **这次的请求太长了** —— 上游拒绝处理(不是网络、也不是配额)。\n\n"
|
|
193
|
-
"**这一轮没有产生结果。** 可以:把需求拆小一点、先 `/compact` "
|
|
194
|
-
"压缩上下文,或换一个窗口更大的模型。" + tail)
|
|
195
|
-
|
|
196
|
-
#: 其余每一类都是一句话(`HEADLINES`)+ 原文 —— 那句话就是用户要的
|
|
197
|
-
#: "下一步":充值 / 等一会儿 / 换 key / 压缩 / 换个说法。
|
|
198
|
-
if k in HEADLINES:
|
|
199
|
-
return HEADLINES[k] + tail
|
|
200
|
-
|
|
201
|
-
if "400" in text or name == "BadRequestError":
|
|
202
|
-
#: 先试着把上游那句话抠出来。整段 JSON 倒给用户是噪音:
|
|
203
|
-
#: 它就是一大坨 `{'error': {...}}`,而真正的原因是
|
|
204
|
-
#: 一句话 ——「模型名填错了,上游只认 deepseek-flash / deepseek-v4-pro」。
|
|
205
|
-
#: 抠不出来就退回旧形状(整段原文 + 那句提醒)——
|
|
206
|
-
#: 那条判据没变:400 的真因也可能是工具表重名,不能一律说成配置问题。
|
|
207
|
-
reason = _upstream_error_message(text)
|
|
208
|
-
if reason:
|
|
209
|
-
_model = _model_name_mismatch(reason)
|
|
210
|
-
if _model:
|
|
211
|
-
supported, passed = _model
|
|
212
|
-
return ("⚠️ **模型名不对** —— 上游只支持 %s,你配置的是 `%s`。\n\n"
|
|
213
|
-
"到「端配置页」把「模型名」改成上面其中一个,再发一次。"
|
|
214
|
-
% (supported, passed))
|
|
215
|
-
return "上游拒绝了这次请求(400):\n%s" % reason[:300]
|
|
216
|
-
#: 原文是「检查模型名称和参数配置」—— 会误导(400 的真因也可能是
|
|
217
|
-
#: 工具表重名)。带上游原文,让真因自己说话。
|
|
218
|
-
return ("模型请求被上游拒绝(400)。上游原文:\n%s\n\n"
|
|
219
|
-
"⚠️ 不一定是配置问题 —— 400 也可能来自请求本身"
|
|
220
|
-
"(例如工具表重名、消息格式)。" % (text[:800] or "(没有原文)"))
|
|
221
|
-
|
|
222
|
-
#: 其它 —— 不装懂:说清是哪一层炸的,把原文交出去。
|
|
223
|
-
where2 = (" [%s]" % stage) if stage else ""
|
|
224
|
-
return ("⚠️ 这一轮出错了%s(%s)。\n\n"
|
|
225
|
-
"**这一轮可能没有产生完整结果。** 可以重发一次;"
|
|
226
|
-
"同一症状反复出现就带上下面这段原文反馈。%s"
|
|
227
|
-
% (where2, name or "未知错误", tail))
|
|
1
|
+
"""上游模型报错的用户面 —— 别让用户看到一行 `error`。
|
|
2
|
+
|
|
3
|
+
## 为什么要有这一份
|
|
4
|
+
|
|
5
|
+
网络断了一下时,聊天里不该出现:
|
|
6
|
+
|
|
7
|
+
(error: Connection error.)
|
|
8
|
+
|
|
9
|
+
它看起来像模型说的话,既没说"这一轮什么都没产出",也没说"下一步做什么";
|
|
10
|
+
而 `task.end.ok` 还可能被记成 true(流式那条会把错误吞成正文)。
|
|
11
|
+
|
|
12
|
+
## 三条判据
|
|
13
|
+
|
|
14
|
+
1. 说人话 + 说下一步:每一类都给「发生了什么 / 这一轮有没有产出 /
|
|
15
|
+
现在可以做什么」。原始错误放在最后一行(诊断要留,但别当头一句)。
|
|
16
|
+
2. 同步与流式走同一个产者:`explain()` 是唯一一处。
|
|
17
|
+
两条路各自内联一份的话,同一个故障会有两种表现。
|
|
18
|
+
3. 失败要被记成失败:流式那条在正文前面打一个 `MARK`,由
|
|
19
|
+
`AgentLoopStage` 剥掉并写 `ctx.metadata["error"]`,`task.end.ok=false`。
|
|
20
|
+
标记只在进程内的一跳上存在(stage 之后就没有了),用户看不到它。
|
|
21
|
+
|
|
22
|
+
## 分类
|
|
23
|
+
|
|
24
|
+
`classify_api_error` 管三类按报文的(overflow / auth / rate_limit);
|
|
25
|
+
连接与超时按异常类型判(`classify_transport_error`)。两类正交,这里合并。
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
__all__ = ["MARK", "HEADLINES", "headline", "explain", "strip_mark", "is_marked"]
|
|
31
|
+
|
|
32
|
+
#: 进程内标记 —— 不是给用户看的。`AgentLoopStage` 见到它就把正文剥掉、
|
|
33
|
+
#: 并把 `ctx.metadata["error"]` 写上(`task.end.ok` 因此是 false)。
|
|
34
|
+
MARK = "\x00[LLM_ERROR]\x00"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def is_marked(text) -> bool:
|
|
38
|
+
return str(text or "").startswith(MARK)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def strip_mark(text) -> str:
|
|
42
|
+
s = str(text or "")
|
|
43
|
+
return s[len(MARK):] if s.startswith(MARK) else s
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _host_of(base_url: str) -> str:
|
|
47
|
+
"""从 base_url 取一个人能认的主机名(取不到就原样回)。"""
|
|
48
|
+
u = str(base_url or "").strip()
|
|
49
|
+
if not u:
|
|
50
|
+
return ""
|
|
51
|
+
try:
|
|
52
|
+
from urllib.parse import urlparse
|
|
53
|
+
host = urlparse(u).hostname or ""
|
|
54
|
+
return host or u
|
|
55
|
+
except Exception: # noqa: BLE001
|
|
56
|
+
return u
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _upstream_error_message(text: str) -> str:
|
|
60
|
+
"""从 400 的原文里抠出 `error.message`。抠不到回空串。
|
|
61
|
+
|
|
62
|
+
形状(OpenAI SDK 的 `str(exc)`,DeepSeek 的原话)::
|
|
63
|
+
|
|
64
|
+
Error code: 400 - {'error': {'message': 'The supported API model names are
|
|
65
|
+
deepseek-flash, deepseek-v4-pro, but you passed deepseek-v4.1-flash',
|
|
66
|
+
'type': 'invalid_request_error', 'param': None,
|
|
67
|
+
'code': 'invalid_request_error'}}
|
|
68
|
+
|
|
69
|
+
只认结构不猜语义:`ast.literal_eval` 解不开就当没抠到(调用方退回旧形状)。
|
|
70
|
+
用户的报错原文是模型之外的人写的,所以这里只做解析、不回显整段。
|
|
71
|
+
"""
|
|
72
|
+
import ast
|
|
73
|
+
import re as _re
|
|
74
|
+
m = _re.search(r"\{.*\}", text, _re.S)
|
|
75
|
+
if not m:
|
|
76
|
+
return ""
|
|
77
|
+
try:
|
|
78
|
+
data = ast.literal_eval(m.group(0))
|
|
79
|
+
except Exception: # noqa: BLE001
|
|
80
|
+
return ""
|
|
81
|
+
if not isinstance(data, dict):
|
|
82
|
+
return ""
|
|
83
|
+
err = data.get("error")
|
|
84
|
+
if isinstance(err, dict) and isinstance(err.get("message"), str):
|
|
85
|
+
return err["message"].strip()
|
|
86
|
+
if isinstance(data.get("message"), str):
|
|
87
|
+
return str(data["message"]).strip()
|
|
88
|
+
return ""
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
#: 每一类的一句话。横幅与聊天正文共用这张表(一处定义)。
|
|
92
|
+
#:
|
|
93
|
+
#: 措辞按"用户下一步能做什么"写:充值 / 等一会儿 / 换 key / 压缩 / 换说法。
|
|
94
|
+
#: 连接与超时那两句保持原样(横幅上一直是这两句,用户已经认得)。
|
|
95
|
+
HEADLINES: dict[str, str] = {
|
|
96
|
+
"balance": "API余额不足,请检查账号资金",
|
|
97
|
+
"request": "请求参数异常",
|
|
98
|
+
"rate_limit": "API请求频率超限,请稍后重试",
|
|
99
|
+
"server_fault": "上游服务器故障",
|
|
100
|
+
"server_busy": "上游服务器繁忙,请稍后重试",
|
|
101
|
+
"server_other": "上游异常",
|
|
102
|
+
"content": "上游拒绝请求:含有非法内容",
|
|
103
|
+
"region": "上游异常:当前地区不支持服务",
|
|
104
|
+
"too_large": "请求错误:消息过大,尝试压缩或重启应用以继续对话",
|
|
105
|
+
"auth": "api密钥错误,请前往端配置页检查您的api可用性",
|
|
106
|
+
"connection": "网络连接异常--请检查宽带互联网连接",
|
|
107
|
+
"timeout": "网络连接超时——请检查网络或代理",
|
|
108
|
+
"context_overflow": "请求过长,超出模型上下文",
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def kind_of(err, kind: str = "", exc_name: str = "") -> str:
|
|
113
|
+
"""归一成一张表里的类别名。认不出回空串。
|
|
114
|
+
|
|
115
|
+
判类的地方只有一处(`providers/base.py` 那两个函数);这里只做兜底,
|
|
116
|
+
好让 `explain()`/`headline()` 在没拿到 kind 时也说得出话。
|
|
117
|
+
"""
|
|
118
|
+
k = str(kind or "").strip().lower()
|
|
119
|
+
if k:
|
|
120
|
+
return k
|
|
121
|
+
try:
|
|
122
|
+
from providers.base import classify_api_error, classify_transport_error
|
|
123
|
+
except Exception: # noqa: BLE001
|
|
124
|
+
return ""
|
|
125
|
+
try:
|
|
126
|
+
return (classify_api_error(err)
|
|
127
|
+
or classify_transport_error(err)
|
|
128
|
+
or "")
|
|
129
|
+
except Exception: # noqa: BLE001
|
|
130
|
+
return ""
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def headline(err, *, kind: str = "", exc_name: str = "") -> str:
|
|
134
|
+
"""横幅上那一句(短、不含原文)。
|
|
135
|
+
|
|
136
|
+
认不出的类别不装懂:退到 400 的两种形状,再退到"这一轮出错了"。
|
|
137
|
+
"""
|
|
138
|
+
k = kind_of(err, kind=kind, exc_name=exc_name)
|
|
139
|
+
if k in HEADLINES:
|
|
140
|
+
return HEADLINES[k]
|
|
141
|
+
return ""
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _model_name_mismatch(reason: str):
|
|
145
|
+
"""上游说的是不是「模型名填错了」?是就回 `(支持的名字, 你填的)`。
|
|
146
|
+
|
|
147
|
+
判据用的是上游的原句:``The supported API model names are A, B,
|
|
148
|
+
but you passed C``。这句比任何本地规则都准 —— 它带着上游此刻认的名字。
|
|
149
|
+
"""
|
|
150
|
+
import re as _re
|
|
151
|
+
m = _re.search(r"supported API model names are (.+?),?\s*but you passed\s+([^\s,]+)",
|
|
152
|
+
reason, _re.I)
|
|
153
|
+
if not m:
|
|
154
|
+
return None
|
|
155
|
+
return m.group(1).strip().rstrip(","), m.group(2).strip("'\"`.")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def explain(err, *, kind: str = "", model: str = "", base_url: str = "",
|
|
159
|
+
stage: str = "", exc_name: str = "") -> str:
|
|
160
|
+
"""把一次上游/内部异常变成用户能照着做的一段话。
|
|
161
|
+
|
|
162
|
+
*err* 可以是异常对象,也可以是 `str`(流式那条拿到的是字符串)。
|
|
163
|
+
*kind* 优先于关键词(`connection` / `timeout` / `context_overflow` /
|
|
164
|
+
`auth` / `rate_limit`);没给就按原文猜(与 `classify_api_error` 同口径)。
|
|
165
|
+
"""
|
|
166
|
+
text = str(err or "")
|
|
167
|
+
name = str(exc_name or "") or type(err).__name__
|
|
168
|
+
#: 判类只有一处(`providers/base.py`)。这里不再抄第二张表 ——
|
|
169
|
+
#: 抄一份就会与它各自漂,而用户看到的正是这第二份。
|
|
170
|
+
k = kind_of(err, kind=kind, exc_name=exc_name)
|
|
171
|
+
|
|
172
|
+
where = _host_of(base_url)
|
|
173
|
+
tail = "\n\n原始错误:%s" % (text[:300] if text else "(上游没给原文)")
|
|
174
|
+
|
|
175
|
+
if k == "connection":
|
|
176
|
+
head = ("⚠️ **没能连上模型服务**" + ("(%s)" % where if where else "")
|
|
177
|
+
+ (",模型 `%s`" % model if model else "") + "。\n\n"
|
|
178
|
+
"**这一轮没有产生任何结果** —— 没有调用工具,也没有花掉模型额度。\n\n"
|
|
179
|
+
"可能原因:网络断开 / 代理或 VPN / 上游临时故障。")
|
|
180
|
+
steps = ("\n- 直接**重发这条消息**重试(客户端已经自动重试过几轮了);"
|
|
181
|
+
"\n- 连续失败:先看网络,或在「端配置」换一条模型。")
|
|
182
|
+
return head + steps + tail
|
|
183
|
+
|
|
184
|
+
if k == "timeout":
|
|
185
|
+
head = ("⚠️ **模型服务超时**" + ("(%s)" % where if where else "") + "。\n\n"
|
|
186
|
+
"**这一轮没有产生任何结果。**")
|
|
187
|
+
steps = ("\n- 直接重发一次;连续超时:检查网络 / 代理,"
|
|
188
|
+
"或在「端配置」换一条模型。")
|
|
189
|
+
return head + steps + tail
|
|
190
|
+
|
|
191
|
+
if k == "context_overflow":
|
|
192
|
+
return ("⚠️ **这次的请求太长了** —— 上游拒绝处理(不是网络、也不是配额)。\n\n"
|
|
193
|
+
"**这一轮没有产生结果。** 可以:把需求拆小一点、先 `/compact` "
|
|
194
|
+
"压缩上下文,或换一个窗口更大的模型。" + tail)
|
|
195
|
+
|
|
196
|
+
#: 其余每一类都是一句话(`HEADLINES`)+ 原文 —— 那句话就是用户要的
|
|
197
|
+
#: "下一步":充值 / 等一会儿 / 换 key / 压缩 / 换个说法。
|
|
198
|
+
if k in HEADLINES:
|
|
199
|
+
return HEADLINES[k] + tail
|
|
200
|
+
|
|
201
|
+
if "400" in text or name == "BadRequestError":
|
|
202
|
+
#: 先试着把上游那句话抠出来。整段 JSON 倒给用户是噪音:
|
|
203
|
+
#: 它就是一大坨 `{'error': {...}}`,而真正的原因是
|
|
204
|
+
#: 一句话 ——「模型名填错了,上游只认 deepseek-flash / deepseek-v4-pro」。
|
|
205
|
+
#: 抠不出来就退回旧形状(整段原文 + 那句提醒)——
|
|
206
|
+
#: 那条判据没变:400 的真因也可能是工具表重名,不能一律说成配置问题。
|
|
207
|
+
reason = _upstream_error_message(text)
|
|
208
|
+
if reason:
|
|
209
|
+
_model = _model_name_mismatch(reason)
|
|
210
|
+
if _model:
|
|
211
|
+
supported, passed = _model
|
|
212
|
+
return ("⚠️ **模型名不对** —— 上游只支持 %s,你配置的是 `%s`。\n\n"
|
|
213
|
+
"到「端配置页」把「模型名」改成上面其中一个,再发一次。"
|
|
214
|
+
% (supported, passed))
|
|
215
|
+
return "上游拒绝了这次请求(400):\n%s" % reason[:300]
|
|
216
|
+
#: 原文是「检查模型名称和参数配置」—— 会误导(400 的真因也可能是
|
|
217
|
+
#: 工具表重名)。带上游原文,让真因自己说话。
|
|
218
|
+
return ("模型请求被上游拒绝(400)。上游原文:\n%s\n\n"
|
|
219
|
+
"⚠️ 不一定是配置问题 —— 400 也可能来自请求本身"
|
|
220
|
+
"(例如工具表重名、消息格式)。" % (text[:800] or "(没有原文)"))
|
|
221
|
+
|
|
222
|
+
#: 其它 —— 不装懂:说清是哪一层炸的,把原文交出去。
|
|
223
|
+
where2 = (" [%s]" % stage) if stage else ""
|
|
224
|
+
return ("⚠️ 这一轮出错了%s(%s)。\n\n"
|
|
225
|
+
"**这一轮可能没有产生完整结果。** 可以重发一次;"
|
|
226
|
+
"同一症状反复出现就带上下面这段原文反馈。%s"
|
|
227
|
+
% (where2, name or "未知错误", tail))
|