@hfyjq/crannia 1.1.2 → 1.2.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 +48 -45
- package/README.md +1 -1
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON-linux.md +174 -0
- package/THIRD-PARTY-PYTHON.md +176 -162
- package/THIRD-PARTY.md +323 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +677 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/provider_loader.py +4 -7
- package/bus/loaders/skill_loader.py +46 -27
- package/bus/loaders/skill_md.py +9 -9
- package/bus/mcp_disabled.py +4 -6
- package/bus/quarantine.py +10 -13
- package/bus/registry.py +2315 -2284
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/handler.py +3 -3
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/handler.py +32 -32
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/handler.py +33 -33
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +24 -24
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +21 -21
- 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/converter.py +1 -1
- package/channels/wechat_channel/handler.py +8 -8
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/core/activity.py +753 -709
- package/core/agent_loop.py +7673 -7522
- package/core/allowlist.py +2 -3
- package/core/approval.py +34 -24
- package/core/approval_timeout.py +256 -228
- package/core/artifacts.py +3 -3
- package/core/attachment_gc.py +21 -0
- package/core/auto_review.py +2 -2
- package/core/browser/cdp.py +5 -2
- package/core/browser/discover.py +55 -19
- package/core/browser/session.py +4 -4
- package/core/cache_break_detector.py +97 -0
- package/core/cache_breakpoints.py +103 -0
- package/core/cache_policies.py +255 -0
- package/core/cancellation.py +3 -3
- package/core/channel_configs.py +3 -3
- package/core/channel_turns.py +13 -13
- package/core/child_approval.py +2 -3
- package/core/code_intel.py +11 -11
- package/core/command_policy.py +13 -7
- package/core/computer/annotate.py +231 -11
- package/core/computer/capture.py +140 -1
- package/core/computer/guard.py +7 -4
- package/core/computer/input.py +452 -26
- package/core/computer/session.py +563 -381
- package/core/computer/tools.py +2170 -928
- package/core/computer/uia.py +100 -1
- package/core/computer/watch.py +1 -1
- package/core/computer/win32.py +302 -7
- package/core/config_requirements.py +205 -205
- package/core/cron/runner.py +4 -4
- package/core/daily_log.py +2 -2
- package/core/dedup.py +2 -2
- package/core/egress_channels.py +326 -0
- package/core/egress_gate.py +96 -8
- package/core/egress_preapproved.py +59 -3
- package/core/endpoint.py +141 -141
- package/core/events.py +17 -8
- package/core/extension.py +2 -2
- package/core/file_history.py +10 -10
- package/core/file_tools.py +3 -3
- package/core/github_config.py +1 -1
- package/core/group_digest.py +3 -4
- package/core/group_minutes.py +5 -5
- package/core/group_reply.py +24 -0
- package/core/groups.py +5 -9
- package/core/hooks.py +15 -15
- package/core/i18n.py +2 -2
- package/core/ice/canary.py +3 -3
- package/core/ice/net_heuristics.py +2 -2
- package/core/im_routing.py +2 -2
- package/core/image_input.py +183 -43
- package/core/inbound.py +3 -3
- package/core/jobs.py +6 -6
- package/core/last_good.py +150 -150
- package/core/leaf_md.py +3 -3
- package/core/llm_errors.py +36 -27
- package/core/mail_config.py +2 -2
- package/core/memory/compressor.py +8 -8
- package/core/memory/manager.py +30 -45
- package/core/memory/session_memory.py +1 -2
- package/core/memory/short_term.py +4 -4
- package/core/memory_extract.py +1 -1
- package/core/memory_organize.py +8 -8
- package/core/memory_store.py +7 -9
- package/core/memory_transfer.py +2 -2
- package/core/model_capabilities.py +2 -2
- package/core/model_configs.py +219 -68
- package/core/model_profiles.py +333 -0
- package/core/modes.py +12 -11
- package/core/net_proxy.py +3 -3
- package/core/pipeline/scheduler.py +48 -43
- package/core/pipeline/stage.py +3 -0
- package/core/pipeline/stages/agent_loop.py +36 -15
- package/core/pipeline/stages/group_reply.py +22 -20
- package/core/pipeline/stages/post_process.py +7 -7
- package/core/pipeline/stages/pre_process.py +360 -68
- package/core/prompt_assembler.py +13 -4
- package/core/recycle.py +227 -16
- package/core/response_language.py +1 -1
- package/core/retention.py +2 -2
- package/core/rg.py +4 -3
- package/core/security.py +26 -8
- package/core/services.py +8 -8
- package/core/session_model.py +3 -5
- package/core/session_pool.py +35 -5
- package/core/session_recap.py +40 -3
- package/core/session_workspace.py +2 -3
- package/core/settings_baseline.py +2 -2
- package/core/settings_transfer.py +25 -1
- package/core/shell.py +52 -20
- package/core/side_call.py +3 -3
- package/core/skill_config.py +164 -0
- package/core/skill_factory.py +2 -2
- package/core/skill_guidance.py +2 -4
- package/core/skill_index.py +3 -4
- package/core/skill_settings.py +1 -1
- package/core/skill_sources.py +7 -7
- package/core/ssh_config.py +129 -0
- package/core/state_registry.py +67 -5
- package/core/subagent.py +28 -5
- package/core/subagent_defs.py +3 -3
- package/core/summarizer.py +4 -5
- package/core/tasks_notify.py +6 -6
- package/core/tiers.py +2 -2
- package/core/todos.py +1 -1
- package/core/token_calibrator.py +1 -4
- package/core/tool_outcome.py +108 -108
- package/core/touched_files.py +1 -1
- package/core/trace.py +12 -3
- package/core/unlock.py +4 -4
- package/core/update_tui.py +184 -158
- package/core/updater.py +5 -3
- package/core/version.py +14 -14
- package/core/vision_budget.py +235 -0
- package/core/web_cache.py +152 -152
- package/core/web_request.py +474 -0
- package/core/window_estimate.py +1 -1
- package/core/workspace.py +56 -26
- package/crannia_tray.py +278 -278
- 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 +1876 -1755
- package/main.py +154 -154
- package/mcpx/client.py +12 -1
- package/mcpx/manager.py +530 -522
- package/mcpx/servers/web_search.py +5 -13
- package/package.json +15 -23
- package/providers/anthropic/client.py +35 -33
- package/providers/base.py +13 -13
- package/providers/cache_policies.json +294 -0
- package/providers/cost_table.json +3779 -3779
- package/providers/lmstudio/client.py +2 -2
- package/providers/model_capabilities.json +6665 -6665
- package/providers/model_profiles.json +629 -0
- package/providers/ollama/client.py +2 -2
- package/providers/openai_compat.py +37 -16
- 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 +2 -2
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/vision_profiles.json +89 -0
- package/providers/window_table.json +8909 -8909
- package/requirements.txt +59 -51
- package/sbom-linux-x64.json +4580 -0
- package/sbom.json +4600 -4413
- package/skills/diagram/SKILL.md +47 -0
- package/skills/diagram/__init__.py +1 -0
- package/skills/diagram/handler.py +294 -0
- package/skills/diagram/layout.py +174 -0
- package/skills/diagram/manifest.yaml +106 -0
- package/skills/diagram/mermaid.py +170 -0
- package/skills/diagram/svg.py +223 -0
- 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 +33 -11
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +1 -1
- 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 +1 -1
- package/skills/media_gen/manifest.yaml +93 -93
- package/skills/netops/SKILL.md +45 -0
- package/skills/netops/__init__.py +1 -0
- package/skills/netops/handler.py +418 -0
- package/skills/netops/manifest.yaml +118 -0
- package/skills/office_parser/_excel_io.py +3 -3
- package/skills/office_parser/handler.py +2 -2
- package/skills/office_parser/ppt_tools.py +1 -1
- 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/crop_images.py +366 -366
- 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/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/utils.py +40 -21
- 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/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 +3 -3
- package/skills/ssh/SKILL.md +81 -0
- package/skills/ssh/__init__.py +1 -0
- package/skills/ssh/handler.py +609 -0
- package/skills/ssh/manifest.yaml +182 -0
- package/tui/crannia.cmd +134 -134
- package/tui/crannia.sh +82 -0
- package/tui/crannia_tui/__init__.py +1 -1
- package/tui/crannia_tui/app.py +240 -17
- package/tui/crannia_tui/app.tcss +21 -0
- package/tui/crannia_tui/clipboard.py +78 -10
- package/tui/crannia_tui/commands.py +37 -0
- package/tui/crannia_tui/events.py +6 -0
- package/tui/crannia_tui/i18n.py +164 -164
- package/tui/crannia_tui/locales/en.json +31 -6
- package/tui/crannia_tui/paths.py +4 -1
- package/tui/crannia_tui/profile.py +178 -169
- package/tui/crannia_tui/settings.py +134 -134
- package/tui/crannia_tui/setup.py +44 -5
- package/tui/crannia_tui/ui.py +156 -16
- package/tui/crannia_tui/update.py +393 -389
- 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 +30 -14
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/linux-x64/rg +0 -0
- package/vendor/ripgrep/package-source.json +15 -16
- package/web/__main__.py +12 -15
- package/web/server.py +8481 -8594
- 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/endpoint.py
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
"""一个数据根只许一个后端:端点文件的读写、判活与清理。
|
|
2
|
-
|
|
3
|
-
写这份文件是因为"两个都装"(GUI 与 TUI 共用 `data/settings.json` 与同一个
|
|
4
|
-
数据根)需要一个双方都认的地址。共享数据根意味着两套安装必须接同一个后端;
|
|
5
|
-
而"谁是那个后端"由端点文件回答,不由"谁先启动"猜。
|
|
6
|
-
|
|
7
|
-
三条判据:
|
|
8
|
-
|
|
9
|
-
1. 写与删都只认自己那份。退出时按 pid 比对,文件已经被后来的进程换掉
|
|
10
|
-
就不删,否则后起的后端会失去自己的端点。
|
|
11
|
-
2. 判活以 HTTP 为准,不以 pid 为准。pid 还在不等于它在听;反过来,
|
|
12
|
-
pid 复用会让"活着"误判成"死了"。pid 只用于清理坏文件时解释原因。
|
|
13
|
-
3. 不用 `os.kill(pid, 0)` 判活。Windows 上 `os.kill` 走
|
|
14
|
-
`TerminateProcess`,判活会变成杀进程,即检测器改变了被测对象。
|
|
15
|
-
|
|
16
|
-
文件位置:`<数据根>/data/runtime/endpoint.json`。走 `data/runtime/` 而不是
|
|
17
|
-
数据根直属的 `runtime/`,与运行数据的既有口径一致(开发树里数据根就是仓库,
|
|
18
|
-
写 `runtime/` 会往源码目录里加东西)。
|
|
19
|
-
"""
|
|
20
|
-
|
|
21
|
-
from __future__ import annotations
|
|
22
|
-
|
|
23
|
-
import json
|
|
24
|
-
import os
|
|
25
|
-
import urllib.error
|
|
26
|
-
import urllib.request
|
|
27
|
-
from pathlib import Path
|
|
28
|
-
|
|
29
|
-
NAME = "endpoint.json"
|
|
30
|
-
|
|
31
|
-
#: 本机地址不走环境变量里的代理 —— 代理通常不该拦 127.0.0.1,
|
|
32
|
-
#: 但 `http_proxy` 一设,urllib 默认就会去问它。
|
|
33
|
-
_OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def endpoint_path(root: Path) -> Path:
|
|
37
|
-
"""端点文件的绝对路径。`root` 是数据根(`core.paths.data_root()`)。"""
|
|
38
|
-
return Path(root) / "data" / "runtime" / NAME
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def write(root: Path, *, port: int, token: str, version: str,
|
|
42
|
-
pid: int | None = None) -> Path:
|
|
43
|
-
"""原子写入端点文件,返回路径。
|
|
44
|
-
|
|
45
|
-
"原子"是给读侧的:TUI 与壳会在本进程写到一半时来读,半截 JSON
|
|
46
|
-
会被判成"没有端点",于是它们各起一个后端 —— 正是这层要防的事。
|
|
47
|
-
"""
|
|
48
|
-
path = endpoint_path(root)
|
|
49
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
50
|
-
data = {
|
|
51
|
-
"port": int(port),
|
|
52
|
-
"token": str(token),
|
|
53
|
-
"pid": int(pid if pid is not None else os.getpid()),
|
|
54
|
-
"version": str(version),
|
|
55
|
-
}
|
|
56
|
-
tmp = path.with_suffix(".json.tmp")
|
|
57
|
-
tmp.write_text(json.dumps(data, ensure_ascii=False, indent=2),
|
|
58
|
-
encoding="utf-8")
|
|
59
|
-
os.replace(tmp, path)
|
|
60
|
-
return path
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def read(root: Path) -> dict | None:
|
|
64
|
-
"""读端点文件。读不到、坏 JSON、字段不像样 —— 一律返回 None。
|
|
65
|
-
|
|
66
|
-
"没有端点"与"端点坏了"在这一层的处置相同:调用方当作没人,
|
|
67
|
-
然后自己起后端;`write()` 会把坏文件覆盖掉。
|
|
68
|
-
"""
|
|
69
|
-
try:
|
|
70
|
-
data = json.loads(endpoint_path(root).read_text(encoding="utf-8"))
|
|
71
|
-
except (OSError, ValueError):
|
|
72
|
-
return None
|
|
73
|
-
if not isinstance(data, dict):
|
|
74
|
-
return None
|
|
75
|
-
try:
|
|
76
|
-
port = int(data.get("port") or 0)
|
|
77
|
-
pid = int(data.get("pid") or 0)
|
|
78
|
-
except (TypeError, ValueError):
|
|
79
|
-
return None
|
|
80
|
-
token = str(data.get("token") or "")
|
|
81
|
-
if not (0 < port < 65536) or not token or pid <= 0:
|
|
82
|
-
return None
|
|
83
|
-
return {
|
|
84
|
-
"port": port,
|
|
85
|
-
"token": token,
|
|
86
|
-
"pid": pid,
|
|
87
|
-
"version": str(data.get("version") or ""),
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def clear(root: Path, *, pid: int | None = None) -> bool:
|
|
92
|
-
"""删端点文件。给了 `pid` 就只删自己那份(文件里的 pid 对得上才删)。
|
|
93
|
-
|
|
94
|
-
返回是否真的删了。删不掉不抛 —— 一个残留的坏文件由下一次 `probe()`
|
|
95
|
-
判死,再被 `write()` 覆盖;为此让进程退出时崩掉不值得。
|
|
96
|
-
"""
|
|
97
|
-
path = endpoint_path(root)
|
|
98
|
-
data = read(root)
|
|
99
|
-
if data is None:
|
|
100
|
-
try:
|
|
101
|
-
path.unlink()
|
|
102
|
-
return True
|
|
103
|
-
except OSError:
|
|
104
|
-
return False
|
|
105
|
-
if pid is not None and data["pid"] != int(pid):
|
|
106
|
-
return False
|
|
107
|
-
try:
|
|
108
|
-
path.unlink()
|
|
109
|
-
return True
|
|
110
|
-
except OSError:
|
|
111
|
-
return False
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def probe(root: Path, timeout: float = 2.0) -> dict | None:
|
|
115
|
-
"""端点文件上真的有人应答吗?活着返回合并后的字典,否则 None。
|
|
116
|
-
|
|
117
|
-
返回 `{port, token, pid, version, status}`:`status` 是 `/api/status`
|
|
118
|
-
的响应体,版本握手与"框架起没起"都从它来。
|
|
119
|
-
|
|
120
|
-
超时给 2 秒:这是启动路径上的同步调用,宁可判"没活着"也不要卡住;
|
|
121
|
-
真正的连接失败(拒绝 / 超时 / 403)统一按"没人"处理 —— 调用方那时
|
|
122
|
-
会自己起后端,代价只是多一次冷启动。
|
|
123
|
-
"""
|
|
124
|
-
data = read(root)
|
|
125
|
-
if data is None:
|
|
126
|
-
return None
|
|
127
|
-
url = f"http://127.0.0.1:{data['port']}/api/status"
|
|
128
|
-
req = urllib.request.Request(
|
|
129
|
-
url, headers={"X-Crannia-Token": data["token"]})
|
|
130
|
-
try:
|
|
131
|
-
with _OPENER.open(req, timeout=timeout) as resp:
|
|
132
|
-
if resp.status != 200:
|
|
133
|
-
return None
|
|
134
|
-
body = json.loads(resp.read().decode("utf-8"))
|
|
135
|
-
except (urllib.error.URLError, OSError, ValueError):
|
|
136
|
-
return None
|
|
137
|
-
if not isinstance(body, dict):
|
|
138
|
-
return None
|
|
139
|
-
out = dict(data)
|
|
140
|
-
out["status"] = body
|
|
141
|
-
return out
|
|
1
|
+
"""一个数据根只许一个后端:端点文件的读写、判活与清理。
|
|
2
|
+
|
|
3
|
+
写这份文件是因为"两个都装"(GUI 与 TUI 共用 `data/settings.json` 与同一个
|
|
4
|
+
数据根)需要一个双方都认的地址。共享数据根意味着两套安装必须接同一个后端;
|
|
5
|
+
而"谁是那个后端"由端点文件回答,不由"谁先启动"猜。
|
|
6
|
+
|
|
7
|
+
三条判据:
|
|
8
|
+
|
|
9
|
+
1. 写与删都只认自己那份。退出时按 pid 比对,文件已经被后来的进程换掉
|
|
10
|
+
就不删,否则后起的后端会失去自己的端点。
|
|
11
|
+
2. 判活以 HTTP 为准,不以 pid 为准。pid 还在不等于它在听;反过来,
|
|
12
|
+
pid 复用会让"活着"误判成"死了"。pid 只用于清理坏文件时解释原因。
|
|
13
|
+
3. 不用 `os.kill(pid, 0)` 判活。Windows 上 `os.kill` 走
|
|
14
|
+
`TerminateProcess`,判活会变成杀进程,即检测器改变了被测对象。
|
|
15
|
+
|
|
16
|
+
文件位置:`<数据根>/data/runtime/endpoint.json`。走 `data/runtime/` 而不是
|
|
17
|
+
数据根直属的 `runtime/`,与运行数据的既有口径一致(开发树里数据根就是仓库,
|
|
18
|
+
写 `runtime/` 会往源码目录里加东西)。
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import json
|
|
24
|
+
import os
|
|
25
|
+
import urllib.error
|
|
26
|
+
import urllib.request
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
NAME = "endpoint.json"
|
|
30
|
+
|
|
31
|
+
#: 本机地址不走环境变量里的代理 —— 代理通常不该拦 127.0.0.1,
|
|
32
|
+
#: 但 `http_proxy` 一设,urllib 默认就会去问它。
|
|
33
|
+
_OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def endpoint_path(root: Path) -> Path:
|
|
37
|
+
"""端点文件的绝对路径。`root` 是数据根(`core.paths.data_root()`)。"""
|
|
38
|
+
return Path(root) / "data" / "runtime" / NAME
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def write(root: Path, *, port: int, token: str, version: str,
|
|
42
|
+
pid: int | None = None) -> Path:
|
|
43
|
+
"""原子写入端点文件,返回路径。
|
|
44
|
+
|
|
45
|
+
"原子"是给读侧的:TUI 与壳会在本进程写到一半时来读,半截 JSON
|
|
46
|
+
会被判成"没有端点",于是它们各起一个后端 —— 正是这层要防的事。
|
|
47
|
+
"""
|
|
48
|
+
path = endpoint_path(root)
|
|
49
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
50
|
+
data = {
|
|
51
|
+
"port": int(port),
|
|
52
|
+
"token": str(token),
|
|
53
|
+
"pid": int(pid if pid is not None else os.getpid()),
|
|
54
|
+
"version": str(version),
|
|
55
|
+
}
|
|
56
|
+
tmp = path.with_suffix(".json.tmp")
|
|
57
|
+
tmp.write_text(json.dumps(data, ensure_ascii=False, indent=2),
|
|
58
|
+
encoding="utf-8")
|
|
59
|
+
os.replace(tmp, path)
|
|
60
|
+
return path
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def read(root: Path) -> dict | None:
|
|
64
|
+
"""读端点文件。读不到、坏 JSON、字段不像样 —— 一律返回 None。
|
|
65
|
+
|
|
66
|
+
"没有端点"与"端点坏了"在这一层的处置相同:调用方当作没人,
|
|
67
|
+
然后自己起后端;`write()` 会把坏文件覆盖掉。
|
|
68
|
+
"""
|
|
69
|
+
try:
|
|
70
|
+
data = json.loads(endpoint_path(root).read_text(encoding="utf-8"))
|
|
71
|
+
except (OSError, ValueError):
|
|
72
|
+
return None
|
|
73
|
+
if not isinstance(data, dict):
|
|
74
|
+
return None
|
|
75
|
+
try:
|
|
76
|
+
port = int(data.get("port") or 0)
|
|
77
|
+
pid = int(data.get("pid") or 0)
|
|
78
|
+
except (TypeError, ValueError):
|
|
79
|
+
return None
|
|
80
|
+
token = str(data.get("token") or "")
|
|
81
|
+
if not (0 < port < 65536) or not token or pid <= 0:
|
|
82
|
+
return None
|
|
83
|
+
return {
|
|
84
|
+
"port": port,
|
|
85
|
+
"token": token,
|
|
86
|
+
"pid": pid,
|
|
87
|
+
"version": str(data.get("version") or ""),
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def clear(root: Path, *, pid: int | None = None) -> bool:
|
|
92
|
+
"""删端点文件。给了 `pid` 就只删自己那份(文件里的 pid 对得上才删)。
|
|
93
|
+
|
|
94
|
+
返回是否真的删了。删不掉不抛 —— 一个残留的坏文件由下一次 `probe()`
|
|
95
|
+
判死,再被 `write()` 覆盖;为此让进程退出时崩掉不值得。
|
|
96
|
+
"""
|
|
97
|
+
path = endpoint_path(root)
|
|
98
|
+
data = read(root)
|
|
99
|
+
if data is None:
|
|
100
|
+
try:
|
|
101
|
+
path.unlink()
|
|
102
|
+
return True
|
|
103
|
+
except OSError:
|
|
104
|
+
return False
|
|
105
|
+
if pid is not None and data["pid"] != int(pid):
|
|
106
|
+
return False
|
|
107
|
+
try:
|
|
108
|
+
path.unlink()
|
|
109
|
+
return True
|
|
110
|
+
except OSError:
|
|
111
|
+
return False
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def probe(root: Path, timeout: float = 2.0) -> dict | None:
|
|
115
|
+
"""端点文件上真的有人应答吗?活着返回合并后的字典,否则 None。
|
|
116
|
+
|
|
117
|
+
返回 `{port, token, pid, version, status}`:`status` 是 `/api/status`
|
|
118
|
+
的响应体,版本握手与"框架起没起"都从它来。
|
|
119
|
+
|
|
120
|
+
超时给 2 秒:这是启动路径上的同步调用,宁可判"没活着"也不要卡住;
|
|
121
|
+
真正的连接失败(拒绝 / 超时 / 403)统一按"没人"处理 —— 调用方那时
|
|
122
|
+
会自己起后端,代价只是多一次冷启动。
|
|
123
|
+
"""
|
|
124
|
+
data = read(root)
|
|
125
|
+
if data is None:
|
|
126
|
+
return None
|
|
127
|
+
url = f"http://127.0.0.1:{data['port']}/api/status"
|
|
128
|
+
req = urllib.request.Request(
|
|
129
|
+
url, headers={"X-Crannia-Token": data["token"]})
|
|
130
|
+
try:
|
|
131
|
+
with _OPENER.open(req, timeout=timeout) as resp:
|
|
132
|
+
if resp.status != 200:
|
|
133
|
+
return None
|
|
134
|
+
body = json.loads(resp.read().decode("utf-8"))
|
|
135
|
+
except (urllib.error.URLError, OSError, ValueError):
|
|
136
|
+
return None
|
|
137
|
+
if not isinstance(body, dict):
|
|
138
|
+
return None
|
|
139
|
+
out = dict(data)
|
|
140
|
+
out["status"] = body
|
|
141
|
+
return out
|
package/core/events.py
CHANGED
|
@@ -96,7 +96,10 @@ EVT_APPROVAL_REQUEST = "approval.request" # 有新的待审批:{request_id,
|
|
|
96
96
|
EVT_APPROVAL_RESOLVED = "approval.resolved" # 已结算/已失效:{request_id,choice}
|
|
97
97
|
|
|
98
98
|
# 异常
|
|
99
|
-
|
|
99
|
+
#
|
|
100
|
+
# `retry` 这个常量已删(2026-11-24,接线复核的 C 组):它从写下那天起零 emit 点,
|
|
101
|
+
# 唯一的重试通道是下面 `llm.retry` 那一对。删掉而不是留着,是判据本身的要求 ——
|
|
102
|
+
# 一个声明了却永远不会发生的事件类型,会让读事件流的人以为这条路上有东西。
|
|
100
103
|
EVT_ERROR = "error" # 阶段抛异常
|
|
101
104
|
EVT_RESYNC = "resync" # 客户端 seq 缺口过大,必须重建
|
|
102
105
|
|
|
@@ -357,7 +360,6 @@ DURABLE_TYPES: frozenset[str] = frozenset({
|
|
|
357
360
|
EVT_SAY,
|
|
358
361
|
EVT_TOOL_START,
|
|
359
362
|
EVT_TOOL_END,
|
|
360
|
-
EVT_RETRY,
|
|
361
363
|
EVT_ERROR,
|
|
362
364
|
# 审批:进 DURABLE_TYPES 是因为它们成对出现
|
|
363
365
|
# (request/resolved)。落盘之后"有 request 无 resolved"是可检测的孤儿 ——
|
|
@@ -551,7 +553,11 @@ class _DurableWriter(threading.Thread):
|
|
|
551
553
|
self._fp = None
|
|
552
554
|
self._day = ""
|
|
553
555
|
self.dropped = 0
|
|
554
|
-
|
|
556
|
+
# 不能叫 `_stop`:那是 threading.Thread 的私有方法。3.11 的
|
|
557
|
+
# `join()` 会经由 `_wait_for_tstate_lock` 调它,被 Event 实例盖掉
|
|
558
|
+
# 之后 join 直接抛 TypeError —— 而那时正在收尾,队列里还剩事件。
|
|
559
|
+
# 3.12 起 threading 重写过,不再调这个方法,所以这个坑只在旧版踩得到。
|
|
560
|
+
self._stop_flag = threading.Event()
|
|
555
561
|
|
|
556
562
|
def offer(self, ev: Event) -> None:
|
|
557
563
|
if self._q.qsize() >= self.MAX_QUEUED:
|
|
@@ -560,14 +566,14 @@ class _DurableWriter(threading.Thread):
|
|
|
560
566
|
self._q.put_nowait(ev)
|
|
561
567
|
|
|
562
568
|
def stop(self) -> None:
|
|
563
|
-
self.
|
|
569
|
+
self._stop_flag.set()
|
|
564
570
|
self._q.put_nowait(None)
|
|
565
571
|
|
|
566
572
|
# -- thread body ------------------------------------------------
|
|
567
573
|
|
|
568
574
|
def run(self) -> None:
|
|
569
575
|
self._prune_old()
|
|
570
|
-
# 注意:不要用 while not self.
|
|
576
|
+
# 注意:不要用 while not self._stop_flag.is_set() 做循环条件。
|
|
571
577
|
# 那样 stop() 一置位就会立刻跳出,把队列里还没落盘的事件全丢掉。
|
|
572
578
|
# 正确做法是靠哨兵值退出 —— Queue 是 FIFO 的,所以哨兵之前入队的
|
|
573
579
|
# 事件必定已经处理完。
|
|
@@ -701,8 +707,11 @@ class EventBus:
|
|
|
701
707
|
if writer is not None:
|
|
702
708
|
writer.offer(ev)
|
|
703
709
|
except Exception as exc: # noqa: BLE001 — 兜底是刻意的
|
|
710
|
+
# 不许写 `type(exc)`:本函数的参数就叫 `type`(事件类型串),
|
|
711
|
+
# Python 会先绑参数,`type` 是个 str,这句自己抛 TypeError,
|
|
712
|
+
# 兜底反而把 emit 的调用方弄挂(Pyright 照出来的)。
|
|
704
713
|
print(f"[events] emit failed for {type!r}: "
|
|
705
|
-
f"{
|
|
714
|
+
f"{exc.__class__.__name__}: {exc}")
|
|
706
715
|
|
|
707
716
|
def _get_writer(self) -> _DurableWriter | None:
|
|
708
717
|
"""懒启动落盘线程。没有 durable 事件就永远不启动。"""
|
|
@@ -883,7 +892,7 @@ def _emit_bg(evt_type: str, kind: str, session_key: str, ts: float,
|
|
|
883
892
|
data["duration_ms"] = int((ts - started) * 1000)
|
|
884
893
|
get_event_bus().emit(evt_type, session_key=session_key, **data)
|
|
885
894
|
except Exception as exc: # noqa: BLE001
|
|
886
|
-
print(f"[bg] {evt_type}
|
|
895
|
+
print(f"[bg] {evt_type} event could not be sent ({type(exc).__name__}: {exc})")
|
|
887
896
|
|
|
888
897
|
|
|
889
898
|
__all__ = [
|
|
@@ -893,7 +902,7 @@ __all__ = [
|
|
|
893
902
|
"EVT_TASK_START", "EVT_TASK_END", "EVT_STEP_START", "EVT_STEP_END",
|
|
894
903
|
"EVT_TURN_START", "EVT_TURN_END", "EVT_THINKING", "EVT_SAY",
|
|
895
904
|
"EVT_TOOL_START", "EVT_TOOL_END", "EVT_DELTA",
|
|
896
|
-
"
|
|
905
|
+
"EVT_ERROR", "EVT_RESYNC",
|
|
897
906
|
"EVT_LLM_RETRY", "EVT_LLM_RETRY_OK",
|
|
898
907
|
"EVT_APPROVAL_REQUEST", "EVT_APPROVAL_RESOLVED",
|
|
899
908
|
"EVT_CONTEXT_STATE",
|
package/core/extension.py
CHANGED
|
@@ -88,11 +88,11 @@ class StoreFacade:
|
|
|
88
88
|
except FileNotFoundError:
|
|
89
89
|
return self.version, {}
|
|
90
90
|
except (OSError, ValueError) as exc:
|
|
91
|
-
print("[skill-store] %s
|
|
91
|
+
print("[skill-store] %s read failed (%s: %s), treating as empty"
|
|
92
92
|
% (self.path, type(exc).__name__, exc))
|
|
93
93
|
return self.version, {}
|
|
94
94
|
if not isinstance(raw, dict):
|
|
95
|
-
print("[skill-store] %s
|
|
95
|
+
print("[skill-store] %s is not an object, treating as empty" % self.path)
|
|
96
96
|
return self.version, {}
|
|
97
97
|
ver = raw.get("_version")
|
|
98
98
|
# bool 是 int 的子类,需要单独排除。
|
package/core/file_history.py
CHANGED
|
@@ -227,10 +227,10 @@ def _make_room(incoming: int) -> int:
|
|
|
227
227
|
total -= size
|
|
228
228
|
removed += 1
|
|
229
229
|
if removed:
|
|
230
|
-
print("[file_history]
|
|
230
|
+
print("[file_history] over total space cap %d MiB, removed %d oldest"
|
|
231
231
|
% (cap_mb, removed))
|
|
232
232
|
except Exception as exc: # noqa: BLE001
|
|
233
|
-
print("[file_history]
|
|
233
|
+
print("[file_history] make room failed (write unaffected): %s: %s"
|
|
234
234
|
% (type(exc).__name__, exc))
|
|
235
235
|
return removed
|
|
236
236
|
|
|
@@ -371,7 +371,7 @@ def _save_index(session_id: str, data: dict) -> None:
|
|
|
371
371
|
json.dumps(data, ensure_ascii=False).encode("utf-8"),
|
|
372
372
|
)
|
|
373
373
|
except OSError as exc:
|
|
374
|
-
print("[file_history]
|
|
374
|
+
print("[file_history] turn index write failed (write unaffected): %s" % exc)
|
|
375
375
|
|
|
376
376
|
|
|
377
377
|
def _record_version(session_id: str, path, version: int, turn: int) -> None:
|
|
@@ -382,7 +382,7 @@ def _record_version(session_id: str, path, version: int, turn: int) -> None:
|
|
|
382
382
|
{"v": int(version), "turn": int(turn), "after": None})
|
|
383
383
|
_save_index(session_id, idx)
|
|
384
384
|
except Exception as exc: # noqa: BLE001
|
|
385
|
-
print("[file_history]
|
|
385
|
+
print("[file_history] turn record failed (write unaffected): %s" % exc)
|
|
386
386
|
|
|
387
387
|
|
|
388
388
|
def _sha256_file(p: Path) -> Optional[str]:
|
|
@@ -429,7 +429,7 @@ def snapshot_before(session_id: str, path, turn: int = 0) -> Optional[int]:
|
|
|
429
429
|
# 不截断(截断出来的 diff 会撒谎:看起来像"只改了前半篇")
|
|
430
430
|
#留一个标记,让界面上如实说"拿不到 diff"(判据 ③)。
|
|
431
431
|
base.with_suffix(".big").write_bytes(b"")
|
|
432
|
-
print("[file_history] %s
|
|
432
|
+
print("[file_history] %s has %d bytes > cap %d - marker only, content not stored"
|
|
433
433
|
% (src.name, size, MAX_BYTES))
|
|
434
434
|
_record_version(sid, str(path), version, turn)
|
|
435
435
|
return version
|
|
@@ -443,7 +443,7 @@ def snapshot_before(session_id: str, path, turn: int = 0) -> Optional[int]:
|
|
|
443
443
|
_record_version(sid, str(path), version, turn)
|
|
444
444
|
return version
|
|
445
445
|
except Exception as exc: # noqa: BLE001
|
|
446
|
-
print("[file_history]
|
|
446
|
+
print("[file_history] archive failed (write unaffected): %s: %s"
|
|
447
447
|
% (type(exc).__name__, exc))
|
|
448
448
|
return None
|
|
449
449
|
|
|
@@ -580,7 +580,7 @@ def seal_turn(session_id: str, turn: int = 0) -> int:
|
|
|
580
580
|
_save_index(sid, idx)
|
|
581
581
|
return n
|
|
582
582
|
except Exception as exc: # noqa: BLE001
|
|
583
|
-
print("[file_history]
|
|
583
|
+
print("[file_history] end-of-turn seal failed (turn unaffected): %s" % exc)
|
|
584
584
|
return 0
|
|
585
585
|
|
|
586
586
|
|
|
@@ -952,9 +952,9 @@ def prune(days: Optional[int] = None, now: Optional[datetime] = None) -> int:
|
|
|
952
952
|
# 保留期读不到时不提它,只报空间那一半。
|
|
953
953
|
why = ("超过 %d 天" % days) if days is not None else None
|
|
954
954
|
if why:
|
|
955
|
-
print("[file_history]
|
|
955
|
+
print("[file_history] removed %d version archives (%s / or over total space cap)"
|
|
956
956
|
% (removed, why))
|
|
957
957
|
else:
|
|
958
|
-
print("[file_history]
|
|
959
|
-
"
|
|
958
|
+
print("[file_history] removed %d version archives (retention unreadable, "
|
|
959
|
+
"total space cap only)" % removed)
|
|
960
960
|
return removed
|
package/core/file_tools.py
CHANGED
|
@@ -283,7 +283,7 @@ def note_observed(path, session_id: str | None = None) -> None:
|
|
|
283
283
|
_note_read(_sid() if session_id is None else session_id, Path(path))
|
|
284
284
|
except Exception as exc: # noqa: BLE001
|
|
285
285
|
# 记账失败不许影响读取本身(同 `security._note_touched` 的规矩)
|
|
286
|
-
print("[observe]
|
|
286
|
+
print("[observe] read-state record failed (read/write unaffected): %s: %s"
|
|
287
287
|
% (type(exc).__name__, exc))
|
|
288
288
|
|
|
289
289
|
|
|
@@ -811,7 +811,7 @@ def write_file(file_path: str = "", content=None) -> dict:
|
|
|
811
811
|
|
|
812
812
|
_note_read(session_id, path) # 纪律 ②:写完把新 stat 记回去
|
|
813
813
|
print("[file] write_file %s (%d bytes, %s)"
|
|
814
|
-
% (_posix(path), len(data), "
|
|
814
|
+
% (_posix(path), len(data), "overwrite" if existed else "created"))
|
|
815
815
|
|
|
816
816
|
lines = payload.count("\n") + (0 if payload.endswith("\n") or not payload else 1)
|
|
817
817
|
out = {
|
|
@@ -1081,7 +1081,7 @@ def _finish_edit(file_path, path, session_id, new_text, enc, replaced_bytes,
|
|
|
1081
1081
|
|
|
1082
1082
|
_note_read(session_id, path) # 纪律 ②
|
|
1083
1083
|
|
|
1084
|
-
print("[file] edit_file %s (%d
|
|
1084
|
+
print("[file] edit_file %s (%d places, %s)"
|
|
1085
1085
|
% (_posix(path), n, _eol_name(used_eol or eol)))
|
|
1086
1086
|
|
|
1087
1087
|
out = {
|
package/core/github_config.py
CHANGED
|
@@ -38,7 +38,7 @@ def load(path: Optional[Path] = None) -> dict:
|
|
|
38
38
|
return {}
|
|
39
39
|
raw = json.loads(p.read_text(encoding="utf-8"))
|
|
40
40
|
except Exception as exc: # noqa: BLE001
|
|
41
|
-
print("[github]
|
|
41
|
+
print("[github] cannot read token from settings (%s) - treated as unconfigured" % type(exc).__name__)
|
|
42
42
|
return {}
|
|
43
43
|
cfg = (raw.get("skill_configs") or {}).get(SCOPE) or {}
|
|
44
44
|
return cfg if isinstance(cfg, dict) else {}
|
package/core/group_digest.py
CHANGED
|
@@ -50,7 +50,7 @@ def _read() -> dict[str, dict]:
|
|
|
50
50
|
except FileNotFoundError:
|
|
51
51
|
return {}
|
|
52
52
|
except Exception as exc: # noqa: BLE001
|
|
53
|
-
print(f"[group_digest]
|
|
53
|
+
print(f"[group_digest] ledger unreadable ({type(exc).__name__}: {exc}) - treating as empty")
|
|
54
54
|
return {}
|
|
55
55
|
return raw if isinstance(raw, dict) else {}
|
|
56
56
|
|
|
@@ -112,8 +112,7 @@ def set(storage_id: str, *, group_id: str, items: list[dict]) -> None: # noqa:
|
|
|
112
112
|
try:
|
|
113
113
|
_write(data)
|
|
114
114
|
except Exception as exc: # noqa: BLE001
|
|
115
|
-
print(f"[group_digest]
|
|
116
|
-
f" —— 本次运行**仍然生效** '{sid}'")
|
|
115
|
+
print(f"[group_digest] ledger write failed ({type(exc).__name__}: {exc}) - still active for this run '{sid}'")
|
|
117
116
|
|
|
118
117
|
|
|
119
118
|
def clear(storage_id: str) -> bool:
|
|
@@ -129,7 +128,7 @@ def clear(storage_id: str) -> bool:
|
|
|
129
128
|
try:
|
|
130
129
|
_write(data)
|
|
131
130
|
except Exception as exc: # noqa: BLE001
|
|
132
|
-
print(f"[group_digest]
|
|
131
|
+
print(f"[group_digest] ledger write failed ({type(exc).__name__}: {exc})")
|
|
133
132
|
return True
|
|
134
133
|
|
|
135
134
|
|
package/core/group_minutes.py
CHANGED
|
@@ -77,7 +77,7 @@ def _read() -> dict[str, dict]:
|
|
|
77
77
|
except FileNotFoundError:
|
|
78
78
|
return {}
|
|
79
79
|
except Exception as exc: # noqa: BLE001
|
|
80
|
-
print("[group_minutes]
|
|
80
|
+
print("[group_minutes] ledger unreadable (%s: %s) - treating as empty"
|
|
81
81
|
% (type(exc).__name__, exc))
|
|
82
82
|
return {}
|
|
83
83
|
return raw if isinstance(raw, dict) else {}
|
|
@@ -298,17 +298,17 @@ def update_now(group: dict, storage_id: str, messages: list[dict],
|
|
|
298
298
|
)
|
|
299
299
|
text = parse_minutes(getattr(resp, "content", "") or "")
|
|
300
300
|
except Exception as exc: # noqa: BLE001
|
|
301
|
-
print("[group_minutes]
|
|
301
|
+
print("[group_minutes] generation failed (%s: %s) - keeping previous minutes"
|
|
302
302
|
% (type(exc).__name__, exc))
|
|
303
303
|
return ""
|
|
304
304
|
if not text:
|
|
305
|
-
print("[group_minutes]
|
|
305
|
+
print("[group_minutes] model returned no minutes - keeping previous minutes")
|
|
306
306
|
return ""
|
|
307
307
|
try:
|
|
308
308
|
set(storage_id, group_id=str((group or {}).get("id") or ""),
|
|
309
309
|
text=text, turns=int(turn_no or 0))
|
|
310
310
|
except Exception as exc: # noqa: BLE001
|
|
311
|
-
print("[group_minutes]
|
|
311
|
+
print("[group_minutes] persist failed (%s: %s)" % (type(exc).__name__, exc))
|
|
312
312
|
return ""
|
|
313
313
|
return text
|
|
314
314
|
|
|
@@ -345,5 +345,5 @@ def spawn_update(group: dict, storage_id: str, messages: list[dict],
|
|
|
345
345
|
t = threading.Thread(target=_run, name="group-minutes", daemon=True)
|
|
346
346
|
t.start()
|
|
347
347
|
except Exception as exc: # noqa: BLE001
|
|
348
|
-
print("[group_minutes]
|
|
348
|
+
print("[group_minutes] background thread could not start (%s: %s)"
|
|
349
349
|
% (type(exc).__name__, exc))
|
package/core/group_reply.py
CHANGED
|
@@ -469,6 +469,30 @@ def member_reply_content(name: str, content: str) -> str:
|
|
|
469
469
|
return "【%s】%s" % (name, str(content or "").strip())
|
|
470
470
|
|
|
471
471
|
|
|
472
|
+
#: 只给界面看的字段 —— 群历史走过四家供应商的 wire 之前要摘掉。
|
|
473
|
+
_UI_ONLY_FIELDS = frozenset({"kind", "speaker", "avatar", "artifacts",
|
|
474
|
+
"tokens", "model"})
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def normalize_history(messages: list[dict]) -> list[dict]:
|
|
478
|
+
"""群聊历史 → 所有供应商同形的副本。
|
|
479
|
+
|
|
480
|
+
为什么需要它:四位成员可能各用一家模型,历史里混着上一家特有的字段
|
|
481
|
+
(`reasoning_content` / `tool_calls` / `model`)—— 这些对下一家没有意义,
|
|
482
|
+
而"每条消息长什么样"应当只由群聊协议决定,不由上一位发言者的供应商决定。
|
|
483
|
+
|
|
484
|
+
普通历史只留 `role` / `content`(content 原样,可能是图片块列表)。
|
|
485
|
+
真的出现工具协议消息时退回"去掉界面字段后的透传" —— 剥掉
|
|
486
|
+
`tool_call_id` 会让 tool 结果变成悬空消息,那是另一种坏形状。
|
|
487
|
+
"""
|
|
488
|
+
items = [m for m in (messages or []) if isinstance(m, dict)]
|
|
489
|
+
if any(m.get("role") == "tool" or m.get("tool_calls") for m in items):
|
|
490
|
+
return [{k: v for k, v in m.items() if k not in _UI_ONLY_FIELDS}
|
|
491
|
+
for m in items]
|
|
492
|
+
return [{"role": str(m.get("role") or ""),
|
|
493
|
+
"content": m.get("content", "")} for m in items]
|
|
494
|
+
|
|
495
|
+
|
|
472
496
|
def strip_member_prefix(name: str, content: str) -> str:
|
|
473
497
|
"""给 GUI 用:把 `【名字】` 前缀剥掉(只剥自己那一层)。"""
|
|
474
498
|
prefix = "【%s】" % (name or "")
|
package/core/groups.py
CHANGED
|
@@ -99,7 +99,7 @@ def _read() -> dict[str, dict]:
|
|
|
99
99
|
# 账本坏了,当没有群。判据同 `bus/mcp_disabled._read`:
|
|
100
100
|
# 宁可让用户重建一个群,不可凭一个读不动的文件把界面上那 8 个头像
|
|
101
101
|
# 渲染成半截(那看起来像"群还在,只是坏了")。
|
|
102
|
-
print(f"[groups]
|
|
102
|
+
print(f"[groups] ledger unreadable ({type(exc).__name__}: {exc}) - treating as empty")
|
|
103
103
|
return {}
|
|
104
104
|
return raw if isinstance(raw, dict) else {}
|
|
105
105
|
|
|
@@ -384,8 +384,7 @@ def create(*, name: str = "", members=None, brief="",
|
|
|
384
384
|
except Exception as exc: # noqa: BLE001
|
|
385
385
|
# 写不进去也照样算建成了(本次运行内是真的)—— 同
|
|
386
386
|
# `mcp_disabled.disable` 那条;但要说出来。
|
|
387
|
-
print(f"[groups]
|
|
388
|
-
f" —— 本次运行**仍然有**这个群 '{gid}'")
|
|
387
|
+
print(f"[groups] ledger write failed ({type(exc).__name__}: {exc}) - group '{gid}' still present for this run")
|
|
389
388
|
return dict(entry)
|
|
390
389
|
|
|
391
390
|
|
|
@@ -420,8 +419,7 @@ def update(gid: str, *, name=None, members=None, brief=None,
|
|
|
420
419
|
try:
|
|
421
420
|
_write(data)
|
|
422
421
|
except Exception as exc: # noqa: BLE001
|
|
423
|
-
print(f"[groups]
|
|
424
|
-
f" —— 本次运行**改动仍然生效** '{gid}'")
|
|
422
|
+
print(f"[groups] ledger write failed ({type(exc).__name__}: {exc}) - changes remain active for this run '{gid}'")
|
|
425
423
|
return dict(next_entry)
|
|
426
424
|
|
|
427
425
|
|
|
@@ -479,8 +477,7 @@ def update_member(gid: str, index: int, *, name: str = "",
|
|
|
479
477
|
try:
|
|
480
478
|
_write(data)
|
|
481
479
|
except Exception as exc: # noqa: BLE001
|
|
482
|
-
print(f"[groups]
|
|
483
|
-
f" —— 本次运行**改动仍然生效** '{gid}'")
|
|
480
|
+
print(f"[groups] ledger write failed ({type(exc).__name__}: {exc}) - changes remain active for this run '{gid}'")
|
|
484
481
|
return dict(next_entry)
|
|
485
482
|
|
|
486
483
|
|
|
@@ -501,8 +498,7 @@ def delete(gid: str) -> bool:
|
|
|
501
498
|
try:
|
|
502
499
|
_write(data)
|
|
503
500
|
except Exception as exc: # noqa: BLE001
|
|
504
|
-
print(f"[groups]
|
|
505
|
-
f" —— 本次运行**仍然删掉了** '{gid}'")
|
|
501
|
+
print(f"[groups] ledger write failed ({type(exc).__name__}: {exc}) - still deleted for this run '{gid}'")
|
|
506
502
|
return True
|
|
507
503
|
|
|
508
504
|
|