@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
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""把"crannia 在哪、用哪个解释器"收敛到一处。
|
|
2
|
+
|
|
3
|
+
为什么不写死路径:这个包要能被人 clone 下来就跑。开发机上 crannia 与
|
|
4
|
+
本包通常是兄弟目录,但别人的目录结构不保证一样。所以按下面顺序找:
|
|
5
|
+
|
|
6
|
+
1. `CRANNIA_HOME` 环境变量(明确指定,最高优先)
|
|
7
|
+
2. 本文件所在的那棵树本身(源码位于 crannia 根下的 `tui/` 时)
|
|
8
|
+
3. 上一级目录里叫 `crannia` 的兄弟目录(独立仓的开发布局)
|
|
9
|
+
4. 都没有 → 报错,并告诉调用者该设哪个变量
|
|
10
|
+
|
|
11
|
+
解释器同理:`CRANNIA_PYTHON` 优先,否则用当前解释器。后者在
|
|
12
|
+
"TUI 与 crannia 共用一个 venv"时是对的;分开装时前者的用途就体现出来了。
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from .i18n import t
|
|
22
|
+
|
|
23
|
+
#: crannia 根目录的标志文件。用 `main.py` + `core/` 一起判,
|
|
24
|
+
#: 单独一个文件可能在任何 python 项目里撞到。
|
|
25
|
+
_MARKERS = ("main.py", "core", "web")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _looks_like_crannia(p: Path) -> bool:
|
|
29
|
+
return all((p / m).exists() for m in _MARKERS)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
_DOTENV_DONE = False
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def load_local_env() -> None:
|
|
36
|
+
"""读本包目录下的 `.env`,只填还没设过的变量。
|
|
37
|
+
|
|
38
|
+
存在的理由很窄但很硬:`CRANNIA_PYTHON` 是"每台机器都不一样"的绝对路径。
|
|
39
|
+
写进启动脚本,等于把开发机的目录结构随仓库发布出去;而每次开新窗口都
|
|
40
|
+
`set` 一遍又不现实。放 `.env` 里,脚本保持通用,机器相关的只有一个文件。
|
|
41
|
+
|
|
42
|
+
不覆盖已设的变量:命令行显式 `set` 的优先级应当高于文件。
|
|
43
|
+
"""
|
|
44
|
+
global _DOTENV_DONE
|
|
45
|
+
if _DOTENV_DONE:
|
|
46
|
+
return
|
|
47
|
+
_DOTENV_DONE = True
|
|
48
|
+
|
|
49
|
+
p = Path(__file__).resolve().parent.parent / ".env"
|
|
50
|
+
try:
|
|
51
|
+
lines = p.read_text(encoding="utf-8").splitlines()
|
|
52
|
+
except OSError:
|
|
53
|
+
return
|
|
54
|
+
for line in lines:
|
|
55
|
+
line = line.strip()
|
|
56
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
57
|
+
continue
|
|
58
|
+
key, value = line.split("=", 1)
|
|
59
|
+
key = key.strip()
|
|
60
|
+
value = value.strip().strip('"').strip("'")
|
|
61
|
+
if key and key not in os.environ:
|
|
62
|
+
os.environ[key] = value
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def crannia_home() -> Path:
|
|
66
|
+
"""返回 crannia 的根目录。找不到就抛 `RuntimeError`(不返回 None:
|
|
67
|
+
调用方拿 None 之后每个地方都要判空,这个错应该在启动那一刻就炸掉)。"""
|
|
68
|
+
load_local_env()
|
|
69
|
+
env = os.environ.get("CRANNIA_HOME", "").strip()
|
|
70
|
+
if env:
|
|
71
|
+
p = Path(env).expanduser().resolve()
|
|
72
|
+
if not _looks_like_crannia(p):
|
|
73
|
+
raise RuntimeError(t("CRANNIA_HOME 指向 %s,"
|
|
74
|
+
"但未在目标位置读取到 main.py / core / web")
|
|
75
|
+
% p)
|
|
76
|
+
return p
|
|
77
|
+
|
|
78
|
+
# 先看"我自己在不在 crannia 树里"(源码在主仓 `tui/` 下时就是这个形状),
|
|
79
|
+
# 再看兄弟目录(独立仓的开发布局)。顺序不能反:主仓里也可能有一个
|
|
80
|
+
# 名字恰好叫 crannia 的子目录,先判自己更不容易认错。
|
|
81
|
+
here = Path(__file__).resolve()
|
|
82
|
+
for base in (here.parent.parent, here.parent.parent.parent):
|
|
83
|
+
if _looks_like_crannia(base):
|
|
84
|
+
return base
|
|
85
|
+
cand = base / "crannia"
|
|
86
|
+
if _looks_like_crannia(cand):
|
|
87
|
+
return cand
|
|
88
|
+
|
|
89
|
+
raise RuntimeError(
|
|
90
|
+
t("找不到 crannia。设 CRANNIA_HOME 指向它的根目录"))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def update_root() -> Path:
|
|
94
|
+
"""装新版本的那个根 —— `releases/` 与 `current` 住在它下面。
|
|
95
|
+
|
|
96
|
+
它不总是"代码根":独立包里运行中的代码在 `releases/<版本>/`,
|
|
97
|
+
而指针在启动器旁边(启动器只读它自己那一层的 `current`)。
|
|
98
|
+
启动器把"我在哪"放进 `CRANNIA_LAUNCH_ROOT`;开发树与整合包里
|
|
99
|
+
那个目录没有 `releases/` / `current`,落回代码根(整合包本身不自己更新)。
|
|
100
|
+
"""
|
|
101
|
+
env = os.environ.get("CRANNIA_LAUNCH_ROOT", "").strip()
|
|
102
|
+
if env:
|
|
103
|
+
p = Path(env).expanduser()
|
|
104
|
+
try:
|
|
105
|
+
p = p.resolve()
|
|
106
|
+
except OSError:
|
|
107
|
+
pass
|
|
108
|
+
if p.is_dir() and ((p / "releases").is_dir() or (p / "current").is_file()):
|
|
109
|
+
return p
|
|
110
|
+
return crannia_home()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _has_flask(py: Path) -> bool:
|
|
114
|
+
"""这个解释器跑得动 crannia 吗。
|
|
115
|
+
|
|
116
|
+
判据选 flask 不是随手挑的:crannia 的 web 入口第一行就 import 它,
|
|
117
|
+
缺了它后端必然起不来。用子进程问而不是看 site-packages 目录 ——
|
|
118
|
+
后者的布局(venv / conda / --user / zipapp)不止一种。
|
|
119
|
+
"""
|
|
120
|
+
import subprocess
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
r = subprocess.run([str(py), "-c", "import flask"],
|
|
124
|
+
capture_output=True, timeout=20)
|
|
125
|
+
return r.returncode == 0
|
|
126
|
+
except (OSError, subprocess.SubprocessError):
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def python_exe() -> Path:
|
|
131
|
+
"""跑后端用的解释器。
|
|
132
|
+
|
|
133
|
+
优先级:`CRANNIA_PYTHON` → 当前解释器(如果它跑得动 crannia)→
|
|
134
|
+
crannia 目录下的 `.venv`。
|
|
135
|
+
|
|
136
|
+
第二条是有原因的:TUI 与 crannia 共用一个 venv 时,当前解释器就是对的,
|
|
137
|
+
不必再让用户配一遍。而 TUI 自己独立建 venv 时(只有 textual / rich),
|
|
138
|
+
当前解释器跑不了后端,这时往下走找 crannia 的 venv。
|
|
139
|
+
|
|
140
|
+
"跑不动就报错"比"随便挑一个然后启动失败"好:后者会在十几秒后
|
|
141
|
+
吐一段 traceback,而原因其实在启动的第一秒就确定了。
|
|
142
|
+
"""
|
|
143
|
+
load_local_env()
|
|
144
|
+
env = os.environ.get("CRANNIA_PYTHON", "").strip()
|
|
145
|
+
if env:
|
|
146
|
+
p = Path(env).expanduser()
|
|
147
|
+
if not p.exists():
|
|
148
|
+
raise RuntimeError(
|
|
149
|
+
t("CRANNIA_PYTHON 指向 %s,但那里没有可执行文件") % p)
|
|
150
|
+
# 显式指定也要验。不验的话,这个变量的唯一作用就是"把错误推迟到
|
|
151
|
+
# 十几秒后"—— 而它存在的意义本来是省掉那次排查。
|
|
152
|
+
if not _has_flask(p):
|
|
153
|
+
raise RuntimeError(
|
|
154
|
+
t("CRANNIA_PYTHON 指向 %s,但 `import flask` 失败,"
|
|
155
|
+
"后端异常\n"
|
|
156
|
+
"它应当指向已安装 crannia 依赖的解释器"
|
|
157
|
+
"(通常是未安装 TUI 依赖的 venv),"
|
|
158
|
+
"而非本程序自己的 venv。") % p)
|
|
159
|
+
return p
|
|
160
|
+
|
|
161
|
+
import importlib.util
|
|
162
|
+
|
|
163
|
+
me = Path(sys.executable)
|
|
164
|
+
if importlib.util.find_spec("flask") is not None:
|
|
165
|
+
return me
|
|
166
|
+
|
|
167
|
+
try:
|
|
168
|
+
home = crannia_home()
|
|
169
|
+
except RuntimeError:
|
|
170
|
+
home = None
|
|
171
|
+
if home is not None:
|
|
172
|
+
for rel in ("Scripts/python.exe", "bin/python3", "bin/python"):
|
|
173
|
+
cand = home / ".venv" / rel
|
|
174
|
+
if cand.exists() and _has_flask(cand):
|
|
175
|
+
return cand
|
|
176
|
+
|
|
177
|
+
raise RuntimeError(
|
|
178
|
+
t("找不到运行所需 Python:当前解释器没有 flask,"
|
|
179
|
+
"crannia 目录下也没有可用的 .venv。\n"
|
|
180
|
+
"设 CRANNIA_PYTHON 指向一个装了 crannia 依赖的解释器"
|
|
181
|
+
"(通常是 `crannia/.venv/Scripts/python.exe`)。"))
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""按键诊断:`python -m crannia_tui --keys`。
|
|
2
|
+
|
|
3
|
+
只在"某个键按了没反应"时用。它把收到的每个 Key 事件同时写进一个日志文件
|
|
4
|
+
(键名、字符、是否可打印),不连后端、不读配置。
|
|
5
|
+
|
|
6
|
+
判据:按下去日志里一行都不加 = 键在终端/输入法那层就被吞了;
|
|
7
|
+
加了但字符为空 = "键到了、字没有",由 `ui.Composer` 按键盘面补字。
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import tempfile
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from textual.app import App, ComposeResult
|
|
16
|
+
from textual.widgets import Static
|
|
17
|
+
|
|
18
|
+
from .i18n import t
|
|
19
|
+
|
|
20
|
+
LOG = Path(tempfile.gettempdir()) / "crannia-keys.log"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def head() -> str:
|
|
24
|
+
"""开屏那三行。写成函数:语言在入口才定,模块级常量会冻在导入那一刻。"""
|
|
25
|
+
return (t("按下小键盘的 /,Ctrl+Q 退出。\n"
|
|
26
|
+
"记录:%s\n\n") % LOG)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class KeyProbeApp(App):
|
|
30
|
+
CSS = "#log { padding: 1 2; }"
|
|
31
|
+
BINDINGS = [("ctrl+q", "quit", "退出")]
|
|
32
|
+
|
|
33
|
+
def __init__(self) -> None:
|
|
34
|
+
super().__init__()
|
|
35
|
+
self._lines: list[str] = []
|
|
36
|
+
try:
|
|
37
|
+
LOG.write_text("", encoding="utf-8")
|
|
38
|
+
except OSError:
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
def compose(self) -> ComposeResult:
|
|
42
|
+
yield Static(head(), id="log")
|
|
43
|
+
|
|
44
|
+
def on_key(self, event) -> None:
|
|
45
|
+
line = (f"{event.key!r} | char={event.character!r} | "
|
|
46
|
+
f"printable={event.is_printable}")
|
|
47
|
+
self._lines = (self._lines + [line])[-6:]
|
|
48
|
+
try:
|
|
49
|
+
with LOG.open("a", encoding="utf-8") as fh:
|
|
50
|
+
fh.write(line + "\n")
|
|
51
|
+
except OSError:
|
|
52
|
+
pass
|
|
53
|
+
self.query_one("#log", Static).update(head() + "\n".join(self._lines))
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""`crannia profile`:列出与切换模型配置。
|
|
2
|
+
|
|
3
|
+
不启动界面,也不 import `core`。有后端在跑就走它的
|
|
4
|
+
`/api/model-configs` —— 那是唯一会把配置写进文件的进程,
|
|
5
|
+
在别的进程里直接改文件会让它的内存缓存与磁盘打架;
|
|
6
|
+
没有后端才走一次子进程查询 / 调用。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import urllib.error
|
|
13
|
+
import urllib.request
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from . import backend
|
|
17
|
+
from .doctor import PREPARE, make_data_root_query, query
|
|
18
|
+
from .i18n import t
|
|
19
|
+
|
|
20
|
+
#: 没有后端时的一次性查询:默认条目 + 全部条目的摘要。
|
|
21
|
+
_LIST_CODE = PREPARE + (
|
|
22
|
+
"import json; from core import model_configs as mc;"
|
|
23
|
+
"print(json.dumps({'default': mc.default_id(), 'entries': mc.names_payload()},"
|
|
24
|
+
" ensure_ascii=False))")
|
|
25
|
+
|
|
26
|
+
#: 没有后端时的切换:先试装,装得上才落盘(与 API 那条同一条判据)。
|
|
27
|
+
_SWITCH_CODE = PREPARE + (
|
|
28
|
+
"import json; from core import model_configs as mc;"
|
|
29
|
+
"reason = mc.preflight(%r) or '';"
|
|
30
|
+
"if not reason: mc.set_default(%r);"
|
|
31
|
+
"print(json.dumps({'reason': reason}, ensure_ascii=False))")
|
|
32
|
+
|
|
33
|
+
_OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def http_json(ep: dict, path: str, payload: dict | None = None,
|
|
37
|
+
timeout: float = 3.0) -> dict | None:
|
|
38
|
+
"""向端点要一份 JSON;GET(payload 为 None)或 POST。失败返回 None。"""
|
|
39
|
+
data = None if payload is None else json.dumps(payload).encode("utf-8")
|
|
40
|
+
req = urllib.request.Request(
|
|
41
|
+
"http://127.0.0.1:%d%s" % (ep["port"], path), data=data,
|
|
42
|
+
method="POST" if data else "GET",
|
|
43
|
+
headers={"X-Crannia-Token": ep["token"],
|
|
44
|
+
**({"Content-Type": "application/json"} if data else {})})
|
|
45
|
+
try:
|
|
46
|
+
with _OPENER.open(req, timeout=timeout) as resp:
|
|
47
|
+
if resp.status != 200:
|
|
48
|
+
return None
|
|
49
|
+
body = json.loads(resp.read().decode("utf-8"))
|
|
50
|
+
except (urllib.error.URLError, OSError, ValueError):
|
|
51
|
+
return None
|
|
52
|
+
return body if isinstance(body, dict) else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def endpoint(python: Path, home: Path) -> dict | None:
|
|
56
|
+
"""活着的端点(没有就是 None)。数据根与判活复用接入那一套。"""
|
|
57
|
+
try:
|
|
58
|
+
root = make_data_root_query(python, home)()
|
|
59
|
+
except Exception: # noqa: BLE001
|
|
60
|
+
return None
|
|
61
|
+
ep = backend._read_endpoint(root)
|
|
62
|
+
if ep is None or backend._probe(ep) is None:
|
|
63
|
+
return None
|
|
64
|
+
return ep
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _entries_from_cards(payload: dict) -> list[dict]:
|
|
68
|
+
out: list[dict] = []
|
|
69
|
+
for card in payload.get("cards") or []:
|
|
70
|
+
for row in card.get("configs") or []:
|
|
71
|
+
out.append({
|
|
72
|
+
"id": str(row.get("id") or ""),
|
|
73
|
+
"name": str(row.get("name") or row.get("id") or ""),
|
|
74
|
+
"model": str(row.get("model") or ""),
|
|
75
|
+
"is_default": bool(row.get("is_default")),
|
|
76
|
+
})
|
|
77
|
+
return out
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def read(python: Path, home: Path, *, ep: dict | None = None,
|
|
81
|
+
query_fn=None) -> tuple[list[dict], str]:
|
|
82
|
+
"""返回 `(条目, 默认 id)`。两条路都读得到,形状先统一成一种。"""
|
|
83
|
+
if ep is not None:
|
|
84
|
+
payload = http_json(ep, "/api/model-configs")
|
|
85
|
+
if payload is not None:
|
|
86
|
+
return _entries_from_cards(payload), str(payload.get("default") or "")
|
|
87
|
+
run = query_fn or (lambda code: query(python, home, code))
|
|
88
|
+
data = json.loads(run(_LIST_CODE))
|
|
89
|
+
entries = [{"id": str(e.get("id") or ""),
|
|
90
|
+
"name": str(e.get("name") or e.get("id") or ""),
|
|
91
|
+
"model": str(e.get("model") or ""),
|
|
92
|
+
"is_default": str(e.get("id") or "") == str(data.get("default") or "")}
|
|
93
|
+
for e in data.get("entries") or []]
|
|
94
|
+
return entries, str(data.get("default") or "")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def render(entries: list[dict]) -> list[str]:
|
|
98
|
+
"""人看的三列:名字 / 模型 / id,默认那条前面一个星。"""
|
|
99
|
+
if not entries:
|
|
100
|
+
return [t("尚未拥有模型配置 —— 请前往 GUI 端配置内新建。")]
|
|
101
|
+
width = max(len(e["name"]) for e in entries)
|
|
102
|
+
lines = []
|
|
103
|
+
for e in entries:
|
|
104
|
+
mark = "*" if e["is_default"] else " "
|
|
105
|
+
lines.append("%s %-*s %-24s (%s)"
|
|
106
|
+
% (mark, width, e["name"],
|
|
107
|
+
e["model"] or t("(未填模型)"), e["id"]))
|
|
108
|
+
return lines
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _resolve(entries: list[dict], ref: str) -> tuple[dict | None, str]:
|
|
112
|
+
"""按 id 或名字找一条;撞名 / 找不到都返回一句带候选的话。"""
|
|
113
|
+
key = ref.strip().lower()
|
|
114
|
+
if not key:
|
|
115
|
+
return None, ""
|
|
116
|
+
by_id = [e for e in entries if e["id"].lower() == key]
|
|
117
|
+
if by_id:
|
|
118
|
+
return by_id[0], ""
|
|
119
|
+
same = [e for e in entries if e["name"].lower() == key]
|
|
120
|
+
if len(same) == 1:
|
|
121
|
+
return same[0], ""
|
|
122
|
+
if len(same) > 1:
|
|
123
|
+
return None, t("「%s」撞名了,用 id 指定:%s") % (
|
|
124
|
+
ref, " · ".join("%s(%s)" % (e["name"], e["id"]) for e in same))
|
|
125
|
+
return None, t("没有「%s」。现有:%s") % (
|
|
126
|
+
ref, " · ".join(e["name"] for e in entries) or t("(空)"))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def switch(python: Path, home: Path, ref: str, *, ep: dict | None = None,
|
|
130
|
+
query_fn=None) -> tuple[bool, str]:
|
|
131
|
+
"""把 `ref` 设为默认。返回 `(成功, 给人看的一句话)`。"""
|
|
132
|
+
entries, _ = read(python, home, ep=ep, query_fn=query_fn)
|
|
133
|
+
hit, why = _resolve(entries, ref)
|
|
134
|
+
if hit is None:
|
|
135
|
+
return False, why or t("请提供一个配置名或 id")
|
|
136
|
+
if hit["is_default"]:
|
|
137
|
+
return True, t("「%s」已经是默认了。") % hit["name"]
|
|
138
|
+
|
|
139
|
+
if ep is not None:
|
|
140
|
+
payload = http_json(ep, "/api/model-configs/default", {"id": hit["id"]})
|
|
141
|
+
if payload is None:
|
|
142
|
+
return False, t("后端无应答 —— 先用 `crannia doctor` 看它还在不在。")
|
|
143
|
+
if not payload.get("ok"):
|
|
144
|
+
return False, str(payload.get("error") or t("切换失败"))
|
|
145
|
+
return True, str(payload.get("message")
|
|
146
|
+
or t("已设为默认:%s") % hit["name"])
|
|
147
|
+
|
|
148
|
+
run = query_fn or (lambda code: query(python, home, code))
|
|
149
|
+
data = json.loads(run(_SWITCH_CODE % (hit["id"], hit["id"])))
|
|
150
|
+
reason = str(data.get("reason") or "")
|
|
151
|
+
if reason:
|
|
152
|
+
return False, t("此配置暂无法使用,未设为默认。原因:%s") % reason
|
|
153
|
+
return True, (t("已设为默认:「%s」。新对话生效;当前这个需重启。")
|
|
154
|
+
% hit["name"])
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def profile_command(ref: str | None, *, python: Path, home: Path,
|
|
158
|
+
ep: dict | None = None, query_fn=None) -> int:
|
|
159
|
+
"""`crannia profile [名字|id]` 的入口。返回退出码。"""
|
|
160
|
+
if ep is None:
|
|
161
|
+
ep = endpoint(python, home)
|
|
162
|
+
if ref:
|
|
163
|
+
ok, message = switch(python, home, ref, ep=ep, query_fn=query_fn)
|
|
164
|
+
print(message)
|
|
165
|
+
return 0 if ok else 1
|
|
166
|
+
entries, _ = read(python, home, ep=ep, query_fn=query_fn)
|
|
167
|
+
for line in render(entries):
|
|
168
|
+
print(line)
|
|
169
|
+
return 0
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"""scrollback 渲染:把已经定稿的事件变成一次性输出。
|
|
2
|
+
|
|
3
|
+
一条规则贯穿全部:**只打印不会再变的东西。**
|
|
4
|
+
|
|
5
|
+
工具调用在 `start` 时还不知道结果,所以按住不表,等 `end` 到了再整块输出。
|
|
6
|
+
理由不是"代码简单",而是 scrollback 的语义:写出去的行改不回来。
|
|
7
|
+
一旦某一行进入终端的历史区,它就归终端管了 —— 那里能翻、能搜、能选中复制,
|
|
8
|
+
但也意味着我们不能再动它。所以"什么时候允许输出"和"什么时候内容定型"
|
|
9
|
+
必须是同一个时刻。
|
|
10
|
+
|
|
11
|
+
"正在跑"的状态留在 Live 区(后续阶段),不属于这里。
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from rich.console import Console, Group
|
|
17
|
+
from rich.markdown import Markdown
|
|
18
|
+
from rich.text import Text
|
|
19
|
+
|
|
20
|
+
from . import events as E
|
|
21
|
+
from . import outcome as O
|
|
22
|
+
from .i18n import t
|
|
23
|
+
|
|
24
|
+
#: 工具行的两个符号。第一个是"一个动作",第二个是"这个动作的产出一行"。
|
|
25
|
+
#: 用符号而不是"- " 之类的文本前缀,是为了在长转写里能一眼分出层级。
|
|
26
|
+
TOOL_MARK = "⏺"
|
|
27
|
+
RESULT_MARK = "⎿"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def fmt_chars(n: int) -> str:
|
|
31
|
+
"""1000 → '1,000';12000 → '12,000'。
|
|
32
|
+
|
|
33
|
+
千分位不是装饰:工具回执动辄五六位数,不带分隔符时读不出量级。
|
|
34
|
+
"""
|
|
35
|
+
return f"{n:,}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def fmt_ms(ms: int) -> str:
|
|
39
|
+
"""毫秒按量级换单位。950 显示 '950ms',3200 显示 '3.2s'。
|
|
40
|
+
|
|
41
|
+
不统一成秒:几百毫秒的工具很多,写成 '0.4s' 反而要多读一位小数。
|
|
42
|
+
"""
|
|
43
|
+
if ms < 1000:
|
|
44
|
+
return f"{ms}ms"
|
|
45
|
+
return f"{ms / 1000:.1f}s"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ScrollbackRenderer:
|
|
49
|
+
"""吃事件,往 console 打定稿内容。
|
|
50
|
+
|
|
51
|
+
有状态,但状态只有一处:尚未配对的工具调起事件。
|
|
52
|
+
除此之外它不记任何东西 —— 会话历史在 crannia 那边,
|
|
53
|
+
这里记一份的话两边迟早不一致。
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(self, console: Console | None = None) -> None:
|
|
57
|
+
self.console = console or Console()
|
|
58
|
+
#: call_id → TOOL_START 事件。等 TOOL_END 来配对后删掉。
|
|
59
|
+
#: 用 dict 而不是 list:工具可以并发调起,顺序不定。
|
|
60
|
+
self._pending: dict[str, E.Event] = {}
|
|
61
|
+
#: 最近一次 agent 说话的原文。用来挡住 task.end 里的重复兜底。
|
|
62
|
+
self._last_say = ""
|
|
63
|
+
#: 本轮是否已经输出过 agent 内容(决定 task.end 要不要兜底)
|
|
64
|
+
self._said_this_turn = False
|
|
65
|
+
|
|
66
|
+
# -- 对外 -----------------------------------------------------
|
|
67
|
+
|
|
68
|
+
def feed(self, ev: E.Event) -> bool:
|
|
69
|
+
"""处理一条事件。返回 True 表示往终端写了东西。
|
|
70
|
+
|
|
71
|
+
返回值不是给用户的,是给测试和 Live 区用的:
|
|
72
|
+
"这一条有没有产生输出"决定了要不要清屏重画。
|
|
73
|
+
"""
|
|
74
|
+
if ev.type == E.TOOL_START:
|
|
75
|
+
self._pending[ev.s("call_id")] = ev
|
|
76
|
+
return False
|
|
77
|
+
if ev.type == E.TOOL_END:
|
|
78
|
+
return self._tool_end(ev)
|
|
79
|
+
if ev.type == E.SAY:
|
|
80
|
+
return self._say(ev)
|
|
81
|
+
if ev.type == E.ERROR:
|
|
82
|
+
return self._error(ev)
|
|
83
|
+
if ev.type == E.TURN_START:
|
|
84
|
+
self._said_this_turn = False
|
|
85
|
+
# 跨轮也要清:上一轮说的最后一句如果和新一轮的第一句恰好相同
|
|
86
|
+
# ("好的"这种),不清就会把新一轮的话吞掉。
|
|
87
|
+
self._last_say = ""
|
|
88
|
+
return False
|
|
89
|
+
if ev.type == E.TASK_END:
|
|
90
|
+
return self._task_end(ev)
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
def flush_unclosed(self) -> None:
|
|
94
|
+
"""把没等到 END 的工具调起打成一行"未完成"。
|
|
95
|
+
|
|
96
|
+
什么时候会缺 END:后端在这一轮中途崩了、或者我们在工具跑到一半时
|
|
97
|
+
才连上事件流。两条路都真实存在,所以不能静默丢掉 ——
|
|
98
|
+
用户会以为那个工具压根没跑过。
|
|
99
|
+
"""
|
|
100
|
+
for call_id, start in self._pending.items():
|
|
101
|
+
name = start.s("name", "?")
|
|
102
|
+
self.console.print(
|
|
103
|
+
Text.assemble((f"{TOOL_MARK} ", "dim"), (name, "bold"),
|
|
104
|
+
(" " + t("(没有收到结束事件)"), "yellow"))
|
|
105
|
+
)
|
|
106
|
+
self._pending.clear()
|
|
107
|
+
|
|
108
|
+
# -- 各类型 ---------------------------------------------------
|
|
109
|
+
|
|
110
|
+
def _tool_end(self, ev: E.Event) -> bool:
|
|
111
|
+
call_id = ev.s("call_id")
|
|
112
|
+
start = self._pending.pop(call_id, None)
|
|
113
|
+
name = ev.s("name") or (start.s("name") if start else "") or "?"
|
|
114
|
+
# activity 在 START 那边(end 事件里没有这个字段)——
|
|
115
|
+
# 所以必须配对,不能只靠 end 自己画。
|
|
116
|
+
activity = start.s("activity") if start else ""
|
|
117
|
+
ok = ev.b("ok", True)
|
|
118
|
+
outcome = ev.s("outcome") or ("completed" if ok else "error")
|
|
119
|
+
reason = ev.s("reason")
|
|
120
|
+
ms = ev.i("duration_ms")
|
|
121
|
+
chars = ev.i("result_chars")
|
|
122
|
+
err = ev.s("error")
|
|
123
|
+
|
|
124
|
+
head = Text()
|
|
125
|
+
head.append(f"{TOOL_MARK} ", style="dim")
|
|
126
|
+
head.append(name, style="bold")
|
|
127
|
+
if activity:
|
|
128
|
+
head.append(" ")
|
|
129
|
+
head.append(activity, style="")
|
|
130
|
+
|
|
131
|
+
detail = Text()
|
|
132
|
+
detail.append(f" {RESULT_MARK} ", style="dim")
|
|
133
|
+
if outcome == "completed":
|
|
134
|
+
parts = []
|
|
135
|
+
if chars:
|
|
136
|
+
parts.append(t("%s 字符") % fmt_chars(chars))
|
|
137
|
+
if ms:
|
|
138
|
+
parts.append(fmt_ms(ms))
|
|
139
|
+
detail.append(" · ".join(parts) if parts else t("完成"), style="dim")
|
|
140
|
+
elif outcome != "error":
|
|
141
|
+
# 等审批 / 被拦 / 被拒:没有"体积 · 耗时"可报 —— 那几格里
|
|
142
|
+
# 最关键的是"它到底跑没跑",一句话说清就够。
|
|
143
|
+
detail.append(O.label(outcome, reason), style="dim")
|
|
144
|
+
elif not ok and err.strip():
|
|
145
|
+
# 错误只给一行:完整 traceback 在上游日志里,
|
|
146
|
+
# 终端里铺开会把上下文冲掉。
|
|
147
|
+
detail.append("✗ ", style="red")
|
|
148
|
+
detail.append(err.splitlines()[0][:160], style="red")
|
|
149
|
+
else:
|
|
150
|
+
# 框架拒绝(工具不存在 / 计划模式 / 白名单 …):没有错误正文,
|
|
151
|
+
# 只有结局本身。
|
|
152
|
+
detail.append(O.label(outcome, reason), style="dim")
|
|
153
|
+
|
|
154
|
+
# 卡片之间留一行。不带它时,连续几个工具会糊成一片 ——
|
|
155
|
+
# 而"一轮里连着跑七八个工具"正是最常见的形态。
|
|
156
|
+
self.console.print(Text(""))
|
|
157
|
+
self.console.print(Group(head, detail))
|
|
158
|
+
return True
|
|
159
|
+
|
|
160
|
+
def said(self, text: str) -> bool:
|
|
161
|
+
"""打一段 agent 说的话,同一段只打一次。
|
|
162
|
+
|
|
163
|
+
两个入口共用它:事件流里的 `task.end.response`,和 `--once` 那条
|
|
164
|
+
HTTP 返回值的兜底。它们会同时到达,而且谁先到并不固定 —— 实际跑下来
|
|
165
|
+
HTTP 常常先返回、事件流后到。各自判各自的标记,就必然打两遍;
|
|
166
|
+
共用一份"上一次说过什么",第二遍才挡得住。
|
|
167
|
+
|
|
168
|
+
判据是"这段文本和上一段一样",不是"说过话没有":一轮里 agent 可能
|
|
169
|
+
连说几段,只有末尾那一小段会和 `response` 重复。
|
|
170
|
+
"""
|
|
171
|
+
text = (text or "").strip()
|
|
172
|
+
if not text or text == self._last_say:
|
|
173
|
+
return False
|
|
174
|
+
self._last_say = text
|
|
175
|
+
self._said_this_turn = True
|
|
176
|
+
self.console.print(Text(""))
|
|
177
|
+
self.console.print(Markdown(text))
|
|
178
|
+
return True
|
|
179
|
+
|
|
180
|
+
def _say(self, ev: E.Event) -> bool:
|
|
181
|
+
return self.said(ev.s("text"))
|
|
182
|
+
|
|
183
|
+
def _error(self, ev: E.Event) -> bool:
|
|
184
|
+
msg = ev.s("message") or ev.s("error") or t("未知错误")
|
|
185
|
+
stage = ev.s("stage")
|
|
186
|
+
first = msg.splitlines()[0]
|
|
187
|
+
# 截断要留痕:切在半个 JSON 上的 "…param": None, 'co" 会被当成乱码,
|
|
188
|
+
# 而带省略号的短句一眼就能看出"这里还有、被我省了"。
|
|
189
|
+
if len(first) > 140:
|
|
190
|
+
first = first[:140] + "…"
|
|
191
|
+
line = Text()
|
|
192
|
+
line.append("✗ ", style="red")
|
|
193
|
+
if stage:
|
|
194
|
+
line.append(f"[{stage}] ", style="dim")
|
|
195
|
+
line.append(first, style="red")
|
|
196
|
+
self.console.print(line)
|
|
197
|
+
return True
|
|
198
|
+
|
|
199
|
+
def _task_end(self, ev: E.Event) -> bool:
|
|
200
|
+
"""一轮结束。
|
|
201
|
+
|
|
202
|
+
正常路径下 agent 的话已经由 `say` 输出过了,这里什么都不做。
|
|
203
|
+
但有些路径只把话放在 `response` 里、不发 `say` —— 那种情况下不补打,
|
|
204
|
+
用户就只能看到一行红字,看不到后端给的说明。所以补一次,走
|
|
205
|
+
`said()` 的去重,避免和 `say` 或 HTTP 返回值撞成三遍。
|
|
206
|
+
"""
|
|
207
|
+
if self._said_this_turn:
|
|
208
|
+
return False
|
|
209
|
+
return self.said(ev.s("response"))
|