@hfyjq/crannia 1.0.0 → 1.1.1

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.
Files changed (222) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +45 -45
  3. package/THIRD-PARTY-FRONTEND.md +5266 -5266
  4. package/THIRD-PARTY-PYTHON.md +162 -153
  5. package/THIRD-PARTY.md +316 -316
  6. package/apps/desktop/quotes.txt +56 -56
  7. package/bus/core.py +671 -671
  8. package/bus/loaders/channel_loader.py +64 -64
  9. package/bus/loaders/skill_md.py +676 -676
  10. package/bus/skill_state.py +124 -124
  11. package/channels/cli_channel/manifest.yaml +5 -5
  12. package/channels/dingtalk_channel/manifest.yaml +12 -12
  13. package/channels/feishu_channel/manifest.yaml +12 -12
  14. package/channels/qq_bot_channel/__init__.py +1 -1
  15. package/channels/qq_bot_channel/handler.py +881 -881
  16. package/channels/qq_bot_channel/manifest.yaml +4 -4
  17. package/channels/telegram_channel/__init__.py +1 -1
  18. package/channels/telegram_channel/handler.py +813 -813
  19. package/channels/telegram_channel/manifest.yaml +4 -4
  20. package/channels/telegram_channel/media.py +231 -231
  21. package/channels/textfmt.py +58 -58
  22. package/channels/wechat_channel/__init__.py +1 -1
  23. package/channels/wechat_channel/auth.py +23 -5
  24. package/channels/wechat_channel/manifest.yaml +5 -5
  25. package/channels/wechat_channel/media.py +178 -178
  26. package/config.yaml +2 -2
  27. package/core/activity.py +3 -0
  28. package/core/agent_loop.py +390 -55
  29. package/core/approval.py +17 -6
  30. package/core/artifacts.py +7 -0
  31. package/core/auto_review.py +23 -5
  32. package/core/channel_configs.py +34 -2
  33. package/core/channel_turns.py +759 -759
  34. package/core/child_approval.py +150 -0
  35. package/core/config_requirements.py +205 -0
  36. package/core/cron/runner.py +118 -6
  37. package/core/cron/store.py +14 -5
  38. package/core/cron/tools.py +17 -5
  39. package/core/endpoint.py +141 -0
  40. package/core/events.py +23 -0
  41. package/core/i18n.py +329 -329
  42. package/core/ice/store.py +191 -191
  43. package/core/inbound.py +193 -193
  44. package/core/jobs.py +19 -3
  45. package/core/last_good.py +150 -150
  46. package/core/llm_errors.py +227 -227
  47. package/core/llm_retry.py +230 -230
  48. package/core/memory/compressor.py +1214 -1214
  49. package/core/memory/manager.py +14 -0
  50. package/core/model_configs.py +80 -0
  51. package/core/pipeline/scheduler.py +2 -2
  52. package/core/pipeline/stages/agent_loop.py +24 -4
  53. package/core/pipeline/stages/pre_process.py +58 -7
  54. package/core/prompt_assembler.py +680 -680
  55. package/core/rg.py +255 -255
  56. package/core/security.py +1544 -1544
  57. package/core/settings_baseline.py +70 -1
  58. package/core/shell.py +5 -17
  59. package/core/skill_settings.py +7 -2
  60. package/core/state_registry.py +19 -0
  61. package/core/subagent.py +64 -3
  62. package/core/task_tool.py +1 -1
  63. package/core/tasks_notify.py +69 -4
  64. package/core/tiers.py +9 -3
  65. package/core/tool_outcome.py +108 -0
  66. package/core/update_tui.py +158 -0
  67. package/core/updater.py +229 -40
  68. package/core/version.py +2 -2
  69. package/core/web_cache.py +152 -0
  70. package/crannia_tray.py +278 -278
  71. package/creators/models.json +4 -4
  72. package/data/settings.example.json +67 -0
  73. package/licenses/GPL-3.0.txt +619 -619
  74. package/licenses/LGPL-2.1.txt +502 -502
  75. package/licenses/LGPL-3.0.txt +165 -165
  76. package/licenses/MIT-chat-on-wechat.txt +18 -18
  77. package/licenses/MIT-deepseek-harness.txt +21 -21
  78. package/licenses/Molio-LICENSE.txt +44 -44
  79. package/locales/en.json +9 -4
  80. package/main.py +154 -154
  81. package/mcpx/manager.py +522 -522
  82. package/mcpx/servers/__init__.py +22 -22
  83. package/mcpx/servers/web_search.py +321 -33
  84. package/package.json +24 -10
  85. package/providers/anthropic/client.py +772 -772
  86. package/providers/cost_table.json +3779 -3779
  87. package/providers/deepseek/client.py +20 -20
  88. package/providers/deepseek/manifest.yaml +2 -2
  89. package/providers/factory.py +136 -136
  90. package/providers/lmstudio/client.py +2 -2
  91. package/providers/metadata.py +80 -80
  92. package/providers/model_capabilities.json +6665 -6665
  93. package/providers/moonshot/client.py +12 -12
  94. package/providers/ollama/client.py +2 -2
  95. package/providers/openai/client.py +13 -13
  96. package/providers/openrouter/client.py +2 -2
  97. package/providers/qwen/__init__.py +1 -1
  98. package/providers/qwen/client.py +2 -2
  99. package/providers/qwen/manifest.yaml +16 -16
  100. package/providers/rate_limiter.py +144 -144
  101. package/providers/siliconflow/client.py +2 -2
  102. package/providers/validation.py +171 -171
  103. package/providers/window_table.json +8909 -8909
  104. package/sbom.json +4413 -4296
  105. package/skills/entertainment/SKILL.md +35 -35
  106. package/skills/entertainment/__init__.py +1 -1
  107. package/skills/entertainment/handler.py +91 -91
  108. package/skills/entertainment/manifest.yaml +26 -26
  109. package/skills/file_parser/GUIDANCE.md +10 -10
  110. package/skills/file_parser/handler.py +177 -4
  111. package/skills/file_parser/manifest.yaml +22 -1
  112. package/skills/github/SKILL.md +32 -32
  113. package/skills/github/__init__.py +1 -1
  114. package/skills/github/manifest.yaml +5 -0
  115. package/skills/mail_parser/SKILL.md +68 -68
  116. package/skills/mail_parser/__init__.py +1 -1
  117. package/skills/mail_parser/handler.py +806 -806
  118. package/skills/mail_parser/manifest.yaml +5 -0
  119. package/skills/media_gen/GUIDANCE.md +19 -19
  120. package/skills/media_gen/SKILL.md +43 -43
  121. package/skills/media_gen/__init__.py +1 -1
  122. package/skills/media_gen/handler.py +430 -430
  123. package/skills/media_gen/manifest.yaml +93 -88
  124. package/skills/office_parser/cell_validation.py +127 -127
  125. package/skills/office_parser/excel_transform.py +936 -936
  126. package/skills/office_parser/layout_spec.py +243 -243
  127. package/skills/office_parser/pptx_export/console_encoding.py +27 -27
  128. package/skills/office_parser/pptx_export/resource_paths.py +88 -88
  129. package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
  130. package/skills/office_parser/pptx_export/svg_finalize/align_embed_images.py +575 -575
  131. package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
  132. package/skills/office_parser/pptx_export/svg_finalize/embed_icons.py +498 -498
  133. package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
  134. package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
  135. package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
  136. package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
  137. package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
  138. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
  139. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
  140. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
  141. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/elements.py +2490 -2490
  142. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
  143. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/styles.py +656 -656
  144. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
  145. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
  146. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
  147. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
  148. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/media.py +141 -141
  149. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
  150. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
  151. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
  152. package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
  153. package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
  154. package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
  155. package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
  156. package/skills/office_parser/references/layout_library.md +816 -816
  157. package/skills/office_parser/references/ppt-master/LICENSE +21 -21
  158. package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
  159. package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
  160. package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
  161. package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
  162. package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
  163. package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
  164. package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
  165. package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
  166. package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
  167. package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
  168. package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
  169. package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
  170. package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
  171. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
  172. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
  173. package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
  174. package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
  175. package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
  176. package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
  177. package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
  178. package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
  179. package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
  180. package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
  181. package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
  182. package/skills/office_parser/references/spec_lock.md +235 -235
  183. package/skills/office_parser/soffice.py +68 -68
  184. package/skills/office_parser/word_tools.py +2909 -2909
  185. package/tui/crannia.cmd +134 -0
  186. package/tui/crannia_tui/__init__.py +1 -0
  187. package/tui/crannia_tui/__main__.py +223 -0
  188. package/tui/crannia_tui/app.py +1318 -0
  189. package/tui/crannia_tui/app.tcss +153 -0
  190. package/tui/crannia_tui/approval.py +85 -0
  191. package/tui/crannia_tui/backend.py +279 -0
  192. package/tui/crannia_tui/channel.py +472 -0
  193. package/tui/crannia_tui/client.py +125 -0
  194. package/tui/crannia_tui/clipboard.py +62 -0
  195. package/tui/crannia_tui/commands.py +65 -0
  196. package/tui/crannia_tui/doctor.py +304 -0
  197. package/tui/crannia_tui/events.py +84 -0
  198. package/tui/crannia_tui/headless.py +152 -0
  199. package/tui/crannia_tui/i18n.py +164 -0
  200. package/tui/crannia_tui/locales/en.json +263 -0
  201. package/tui/crannia_tui/outcome.py +62 -0
  202. package/tui/crannia_tui/paths.py +181 -0
  203. package/tui/crannia_tui/probe.py +53 -0
  204. package/tui/crannia_tui/profile.py +169 -0
  205. package/tui/crannia_tui/render.py +209 -0
  206. package/tui/crannia_tui/settings.py +134 -0
  207. package/tui/crannia_tui/setup.py +380 -0
  208. package/tui/crannia_tui/term.py +116 -0
  209. package/tui/crannia_tui/ui.py +1236 -0
  210. package/tui/crannia_tui/update.py +389 -0
  211. package/vendor/ripgrep/COPYING.txt +3 -3
  212. package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
  213. package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
  214. package/vendor/ripgrep/README.md +51 -51
  215. package/vendor/ripgrep/UNLICENSE.txt +24 -24
  216. package/vendor/ripgrep/package-source.json +19 -19
  217. package/web/__main__.py +15 -0
  218. package/web/server.py +178 -20
  219. package/web/static/img/channel-telegram.svg +1 -1
  220. package/web/static/img/channel-wechat.svg +1 -1
  221. package/web/templates/logs.html +49 -49
  222. package/web/templates/models.html +46 -46
@@ -0,0 +1,134 @@
1
+ @echo off
2
+ rem crannia-tui launcher: type `crannia` anywhere to start.
3
+ rem
4
+ rem NOTE: this file is ASCII-only on purpose. cmd.exe reads .cmd files using the
5
+ rem system code page (GBK on a Chinese Windows), so UTF-8 Chinese in here turns
6
+ rem into garbage and breaks the rem lines themselves. Keep it ASCII.
7
+ rem
8
+ rem Why the shell dance below: the full-screen UI draws with ANSI cursor
9
+ rem addressing. cmd's legacy console does not interpret escapes, so it would
10
+ rem show a screenful of `?[2m`. Windows Terminal sets WT_SESSION; if that is
11
+ rem missing we relaunch ourselves inside it, so nobody has to remember which
12
+ rem window to use.
13
+ rem
14
+ rem `--once` is the exception: that path prints to a pipe or a log, needs no
15
+ rem UI, and must not pop a new window.
16
+ rem
17
+ rem Three layouts share this one file:
18
+ rem A) next to Crannia.exe (TUI ships inside the GUI installer)
19
+ rem <install>\crannia.cmd
20
+ rem <install>\resources\app\tui\crannia_tui
21
+ rem <install>\resources\primary-runtime\python\python.exe
22
+ rem B) the dev tree: this file lives in <repo>\tui\
23
+ rem <repo>\tui\crannia_tui
24
+ rem <repo>\tui\.venv\Scripts\python.exe
25
+ rem C) the standalone package (no GUI, no system Python)
26
+ rem <pkg>\crannia.cmd + <pkg>\runtime\python\python.exe
27
+ rem <pkg>\releases\<current>\crannia_tui
28
+ rem Layout C is exactly what `crannia update --from` produces, so the launcher
29
+ rem and the updater agree on where code lives.
30
+
31
+ setlocal
32
+
33
+ set "HERE=%~dp0"
34
+
35
+ if "%~1"=="doctor" goto run
36
+ if "%~1"=="config" goto run
37
+ if "%~1"=="profile" goto run
38
+ if "%~1"=="channel" goto run
39
+ if "%~1"=="setup" goto run
40
+ if "%~1"=="update" goto run
41
+ if "%~1"=="--version" goto run
42
+ if "%~1"=="--help" goto run
43
+ if "%~1"=="-h" goto run
44
+
45
+ echo %* | findstr /c:"--once" >nul && goto run
46
+
47
+ if defined WT_SESSION goto run
48
+
49
+ where wt.exe >nul 2>&1
50
+ if errorlevel 1 (
51
+ echo [crannia] this console does not support ANSI; the UI would be garbled.
52
+ echo [crannia] install Windows Terminal and retry, or set CRANNIA_TUI_FORCE=1
53
+ if not defined CRANNIA_TUI_FORCE exit /b 1
54
+ goto run
55
+ )
56
+
57
+ rem Relaunch inside Windows Terminal. `-d` keeps the current directory --
58
+ rem otherwise the new window starts in the home directory and "which repo am
59
+ rem I in" silently changes.
60
+ rem
61
+ rem `call` is not decoration: without it, a path containing a space is split
62
+ rem by the time the inner cmd sees it. (The `cmd /c ""%~f0" ..."` form works
63
+ rem when invoked directly, but not through wt.exe's argument parsing.)
64
+ wt.exe -d "%CD%" cmd /c call "%~f0" %*
65
+ exit /b 0
66
+
67
+ :run
68
+ rem Window title: without this the tab shows the full path of cmd.exe, and a
69
+ rem row of them is unreadable. The app sets it too (see app._set_console_title),
70
+ rem but only after Python starts -- this covers the startup second.
71
+ title Crannia
72
+
73
+ rem The directory we were called from is the session's working directory, so
74
+ rem remember it before pushd changes it -- the UI shows it and session lookup
75
+ rem is keyed by it.
76
+ set "CRANNIA_LAUNCH_CWD=%CD%"
77
+
78
+ rem Where the update chain lives (`releases\` + `current`). This is the
79
+ rem directory this file sits in, not the code root we pushd into below:
80
+ rem in the standalone layout the running code is under releases\<current>,
81
+ rem and updates must land next to this file or the launcher never sees them.
82
+ set "CRANNIA_LAUNCH_ROOT=%HERE%"
83
+
84
+ rem Layout A0: the shim installed on PATH lives in
85
+ rem %LOCALAPPDATA%\Microsoft\WindowsApps and points back here:
86
+ rem <install>\bin\crannia.cmd -> ..\resources\app\tui + ..\resources\primary-runtime
87
+ if exist "%HERE%..\resources\app\tui\crannia_tui" (
88
+ set "PY=%HERE%..\resources\primary-runtime\python\python.exe"
89
+ set "RUN=%HERE%..\resources\app\tui"
90
+ goto have_layout
91
+ )
92
+
93
+ rem A) inside the GUI installation: this file sits next to Crannia.exe.
94
+ if exist "%HERE%resources\app\tui\crannia_tui" (
95
+ set "PY=%HERE%resources\primary-runtime\python\python.exe"
96
+ set "RUN=%HERE%resources\app\tui"
97
+ goto have_layout
98
+ )
99
+
100
+ rem B) dev tree, or C) standalone package. Dev prefers its own venv; the
101
+ rem standalone package has runtime\ next to this file.
102
+ if exist "%HERE%.venv\Scripts\python.exe" set "PY=%HERE%.venv\Scripts\python.exe"
103
+ if not defined PY if exist "%HERE%runtime\python\python.exe" (
104
+ set "PY=%HERE%runtime\python\python.exe"
105
+ rem Standalone is an installation too, but its code root is
106
+ rem releases\<version>, not <install>\resources\app -- the path-based
107
+ rem check in core.paths cannot tell. Say it explicitly, otherwise user
108
+ rem data lands inside releases\<version>\data instead of %APPDATA%.
109
+ set "CRANNIA_INSTALLED=1"
110
+ )
111
+ if not defined PY (
112
+ echo [crannia] no Python runtime found next to %~f0
113
+ echo [crannia] expected .venv\Scripts\python.exe or runtime\python\python.exe
114
+ exit /b 1
115
+ )
116
+
117
+ rem Release pointer: `current` names a directory under `releases\`. If it is
118
+ rem there, run that copy; otherwise run this tree (dev layout / first start).
119
+ set "RUN=%HERE%"
120
+ if exist "%HERE%current" set /p CUR=<"%HERE%current"
121
+ if defined CUR if exist "%HERE%releases\%CUR%\crannia_tui" set "RUN=%HERE%releases\%CUR%"
122
+
123
+ :have_layout
124
+ rem `-m` needs the package importable from the current directory. pushd to the
125
+ rem code root, otherwise running `crannia` from any other folder fails with
126
+ rem "No module named crannia_tui".
127
+ pushd "%RUN%"
128
+ "%PY%" -m crannia_tui %*
129
+ rem popd clears ERRORLEVEL on success, so take the code before it. The
130
+ rem non-interactive commands (doctor / --version / --once) exist to be
131
+ rem checked by scripts; losing their exit code would make them useless.
132
+ set "RC=%ERRORLEVEL%"
133
+ popd
134
+ exit /b %RC%
@@ -0,0 +1 @@
1
+ """crannia 的终端界面(全屏)。"""
@@ -0,0 +1,223 @@
1
+ """入口:`python -m crannia_tui`。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import os
7
+ import sys
8
+
9
+ from . import term
10
+ from .app import CranniaApp
11
+ from .backend import Backend
12
+ from .headless import run_once
13
+ from .i18n import t
14
+ from .paths import crannia_home, python_exe, update_root
15
+
16
+ #: 问后端"现在界面用哪一门"。判据只住 `core/i18n.py` 一处:设置里的
17
+ #: `interface_language`、"跟随系统"、系统探测、坏值回落全在那边 ——
18
+ #: TUI 抄一份就是第二套真相,而两边答得不一样时没人看得出。
19
+ #:
20
+ #: 先 `paths.prepare()`:`i18n` 读的是相对路径 `data/settings.json`,安装版
21
+ #: 靠入口那一下 chdir 才指到用户数据根;子进程不是入口,不 prepare 就读到
22
+ #: 安装目录里那份模板(开发树两者重合,看不出来)。
23
+ _LANG_CODE = ("from core import paths as _paths; _paths.prepare()\n"
24
+ "from core import i18n; print(i18n.current())")
25
+
26
+
27
+ def _use_saved_language(python, home) -> None:
28
+ """把界面语言定下来;问不到就保持源码语言(原文照常显示)。"""
29
+ try:
30
+ from . import i18n
31
+ from .doctor import query
32
+
33
+ i18n.configure(query(python, home, _LANG_CODE).strip(), home=home)
34
+ except Exception: # noqa: BLE001
35
+ pass
36
+
37
+
38
+ def _configure_language() -> None:
39
+ """在构造 argparse 之前定语言 —— help 也是解析参数之前就拼好的。
40
+
41
+ 问不到就留在源码语言:解析器照常能用,只是那一屏是中文。
42
+ `--version` 跳过(那一路只打一个 ASCII 版本号,不值得多两次子进程)。
43
+ """
44
+ try:
45
+ _use_saved_language(python_exe(), crannia_home())
46
+ except Exception: # noqa: BLE001
47
+ pass
48
+
49
+
50
+ def main(argv: list[str] | None = None) -> int:
51
+ raw = list(sys.argv[1:] if argv is None else argv)
52
+ if "--version" not in raw:
53
+ _configure_language()
54
+ ap = argparse.ArgumentParser(
55
+ prog="crannia-tui",
56
+ description=t("crannia 的终端界面"))
57
+ ap.add_argument("--once", metavar="TEXT",
58
+ help=t("发送一条消息而不做交互(脚本 / CI 用)"))
59
+ ap.add_argument("--port", type=int, default=None,
60
+ help=t("固定后端端口。默认让系统分配空闲端口"))
61
+ ap.add_argument("--keys", action="store_true",
62
+ help=t("按键诊断:只显示收到的按键,不启动后端"))
63
+ # 三个只读命令:都不启动后端,所以要在构造 Backend 之前分流。
64
+ # 用可选位置参数而不是改老参数的形状,--once / --port / --keys 原样不动。
65
+ ap.add_argument("command", nargs="?",
66
+ choices=("doctor", "config", "profile", "channel", "setup",
67
+ "update"),
68
+ help=t("doctor:逐项自检;config:配合 path 打印设置文件路径;"
69
+ "profile:列出或切换模型配置;channel:通道的配置与启停;"
70
+ "setup:配置一条模型 API(首次引导);update:安装一个新版本"))
71
+ ap.add_argument("subcommand", nargs="?",
72
+ help=t("config 后接 path;profile 后接配置名或 id;"
73
+ "channel 后接 list / add / config / remove / default / "
74
+ "start / stop / login"))
75
+ ap.add_argument("rest", nargs="*", help=t("channel 子命令的参数"))
76
+ ap.add_argument("--version", action="store_true",
77
+ help=t("打印 crannia 版本后退出"))
78
+ # update 的三个参数(其它命令不用它们)。
79
+ ap.add_argument("--from", dest="update_from", metavar="PATH", default="",
80
+ help=t("update:从本地 zip 或从目录装载"))
81
+ ap.add_argument("--to", metavar="VERSION", default="",
82
+ help=t("update:当前版本版本名"))
83
+ ap.add_argument("--sha256", metavar="HEX", default="",
84
+ help=t("update:zip 的 sha256(对不上就拒)"))
85
+ ap.add_argument("--force", action="store_true",
86
+ help=t("update:覆盖已存在的同版本目录"))
87
+ ap.epilog = t("附加命令:doctor(自检) · config path(打印设置文件绝对路径)"
88
+ " · profile [名字](列出或切换模型配置)"
89
+ " · channel (通道的配置与启停)"
90
+ " · --version(打印版本)——都不启动界面")
91
+ args = ap.parse_args(raw)
92
+
93
+ if args.version:
94
+ from .doctor import app_version
95
+
96
+ print(app_version())
97
+ return 0
98
+
99
+ if args.command == "doctor":
100
+ from .doctor import doctor
101
+
102
+ code, lines = doctor(port=args.port)
103
+ for line in lines:
104
+ print(line)
105
+ return code
106
+
107
+ if args.command == "config":
108
+ from .doctor import settings_path
109
+
110
+ if args.subcommand != "path":
111
+ ap.error(t("config 后需要 path"))
112
+ try:
113
+ print(settings_path())
114
+ except (OSError, RuntimeError, ValueError) as exc:
115
+ print(exc, file=sys.stderr)
116
+ return 1
117
+ return 0
118
+
119
+ if args.command == "profile":
120
+ from .profile import profile_command
121
+
122
+ try:
123
+ return profile_command(args.subcommand,
124
+ python=python_exe(), home=crannia_home())
125
+ except RuntimeError as exc:
126
+ print(exc, file=sys.stderr)
127
+ return 2
128
+
129
+ if args.command == "update":
130
+ from pathlib import Path as _Path
131
+
132
+ from .update import command as update_command
133
+
134
+ payload = _Path(args.update_from).expanduser() if args.update_from else None
135
+ return update_command(root=crannia_home(), payload=payload,
136
+ version=args.to, sha256=args.sha256,
137
+ force=args.force, install_root=update_root())
138
+
139
+ if args.command == "setup":
140
+ from .setup import setup_command
141
+
142
+ try:
143
+ python, home = python_exe(), crannia_home()
144
+ return setup_command(python=python, home=home)
145
+ except KeyboardInterrupt:
146
+ print("\n" + t("已取消。"))
147
+ return 1
148
+ except RuntimeError as exc:
149
+ print(exc, file=sys.stderr)
150
+ return 2
151
+
152
+ if args.command == "channel":
153
+ from .channel import channel_command
154
+
155
+ argv = ([args.subcommand] if args.subcommand else []) + list(args.rest)
156
+ try:
157
+ return channel_command(argv, python=python_exe(),
158
+ home=crannia_home())
159
+ except RuntimeError as exc:
160
+ print(exc, file=sys.stderr)
161
+ return 2
162
+
163
+ if args.keys:
164
+ from .probe import KeyProbeApp
165
+
166
+ KeyProbeApp().run()
167
+ return 0
168
+
169
+ # 首次引导:还没有默认模型配置时,进界面前先配一条。
170
+ # 只在交互终端里做 —— `--once` 是脚本入口,不能卡在提示上。
171
+ if not args.once and sys.stdin.isatty():
172
+ try:
173
+ from .setup import needs_setup, setup_command
174
+
175
+ python, home = python_exe(), crannia_home()
176
+ if needs_setup(python, home):
177
+ print(t("[setup] 未识别的可用配置,进入启动引导"))
178
+ if setup_command(python=python, home=home) != 0:
179
+ print(t("[setup] 引导未完成"))
180
+ except Exception as exc: # noqa: BLE001
181
+ print(t("[setup] 检查失败(%s)—— 继续启动") % exc)
182
+
183
+ # 设置文件:首次启动播种、坏文件先备份再重建、坏值出声。
184
+ # 失败只打一行 —— 一个读不出来的设置文件不该挡住用户用程序。
185
+ try:
186
+ from . import settings as _settings
187
+ from .doctor import settings_path
188
+
189
+ for _line in _settings.ensure(python_exe(), crannia_home()):
190
+ print(_line)
191
+ _values, _problems = _settings.read(
192
+ settings_path(home=crannia_home(), python=python_exe()),
193
+ _settings.load_baseline(crannia_home()))
194
+ for _line in _problems:
195
+ print(t("[settings] %s") % _line)
196
+ except Exception as exc: # noqa: BLE001
197
+ print(t("[settings] 检查失败:%s") % exc)
198
+
199
+ try:
200
+ backend = Backend(crannia_home(), python_exe(), port=args.port)
201
+ except RuntimeError as exc:
202
+ print(exc, file=sys.stderr)
203
+ return 2
204
+
205
+ if args.once:
206
+ return run_once(backend, args.once)
207
+
208
+ # 全屏布局靠光标定位和整屏重绘,跳不出 ANSI。cmd 的旧控制台不解释转义,
209
+ # 在那里进去只会看到一片乱码 —— 提前说清楚,而不是让人对着花屏猜。
210
+ if sys.stdout.isatty() and not term.ansi_passthrough_ok():
211
+ term.warn_no_ansi()
212
+
213
+ # 呼出的目录 = 这一次会话的工作目录。启动器在 pushd 之前把它放进
214
+ # `CRANNIA_LAUNCH_CWD`;直接 `python -m crannia_tui` 时就是进程 cwd。
215
+ # 不能用后端报的 `project_root`:那是代码根,不是用户站在哪。
216
+ launch_cwd = (os.environ.get("CRANNIA_LAUNCH_CWD") or "").strip() or os.getcwd()
217
+
218
+ CranniaApp(backend, launch_cwd=launch_cwd, check_updates=True).run()
219
+ return 0
220
+
221
+
222
+ if __name__ == "__main__":
223
+ sys.exit(main())