@sakanano-moe/agenthelm 6.8.0 → 6.11.0-rc.2
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/README.md +1 -1
- package/README.zh-CN.md +2 -0
- package/package.json +1 -1
- package/runtime/index.json +6 -6
- package/runtime/plugins/agent-pack/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/agent-pack/hooks/hooks.json +32 -0
- package/runtime/plugins/agent-pack/plugin.json +1 -1
- package/runtime/plugins/agent-pack/references/local-only.md +126 -0
- package/runtime/plugins/agent-pack/references/nudge.en.md +58 -0
- package/runtime/plugins/agent-pack/references/nudge.md +58 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_nudge.py +613 -0
- package/runtime/plugins/agent-pack/runtime/agenthelm.py +1271 -221
- package/runtime/plugins/agent-pack/runtime/agenthelm_runtime.py +114 -65
- package/runtime/plugins/agent-pack/runtime/i18n_agent_pack.py +78 -0
- package/runtime/plugins/agent-pack/runtime/local_only.py +1132 -0
- package/runtime/plugins/agent-pack/runtime/locale/en.json +569 -0
- package/runtime/plugins/agent-pack/runtime/mcp_server.py +25 -0
- package/runtime/plugins/agent-pack/runtime/nudge_text.py +127 -0
- package/runtime/plugins/agent-pack/runtime/user_settings.py +275 -0
- package/runtime/plugins/agent-pack/skills/agent-pack/SKILL.md +11 -1
- package/runtime/plugins/agents-init/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/agents-init/content.json +4 -4
- package/runtime/plugins/agents-init/plugin.json +1 -1
- package/runtime/plugins/agents-init/skills/agents-init/AGENT.template.md +141 -5
- package/runtime/plugins/agents-init/skills/agents-init/SKILL.md +41 -8
- package/runtime/plugins/agents-init/skills/agents-init/project.schema.json +408 -80
- package/runtime/plugins/agents-init/skills/agents-init/scripts/adopt.py +615 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/baseline.py +221 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/i18n_init.py +78 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/init_project.py +748 -159
- package/runtime/plugins/agents-init/skills/agents-init/scripts/locale/en.json +289 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/merge3.py +149 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/regions.py +99 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/scaffold.py +594 -90
- package/runtime/plugins/agents-init/skills/agents-init/skeleton.json +2 -0
- package/runtime/plugins/agents-init/skills/agents-init/skill.json +1 -1
- package/runtime/plugins/opinion-manager/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/opinion-manager/plugin.json +1 -1
- package/runtime/plugins/opinion-manager/references/inbox.en.md +48 -0
- package/runtime/plugins/opinion-manager/references/inbox.md +48 -0
- package/runtime/plugins/opinion-manager/runtime/context.py +31 -26
- package/runtime/plugins/opinion-manager/runtime/i18n_opinion.py +78 -0
- package/runtime/plugins/opinion-manager/runtime/inbox.py +398 -0
- package/runtime/plugins/opinion-manager/runtime/inbox_text.py +114 -0
- package/runtime/plugins/opinion-manager/runtime/lifecycle.py +45 -44
- package/runtime/plugins/opinion-manager/runtime/locale/en.json +193 -0
- package/runtime/plugins/opinion-manager/runtime/opinion_manager.py +55 -50
- package/runtime/plugins/opinion-manager/runtime/versioned.py +90 -85
- package/runtime/plugins/opinion-manager/skills/opinion-manager/SKILL.md +15 -2
- package/runtime/plugins/project-orchestrator/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/project-orchestrator/content.json +30 -3
- package/runtime/plugins/project-orchestrator/plugin.json +1 -1
- package/runtime/plugins/project-orchestrator/references/case-workspaces.md +9 -1
- package/runtime/plugins/project-orchestrator/references/case.schema.json +1 -0
- package/runtime/plugins/project-orchestrator/references/sop-interview.md +83 -0
- package/runtime/plugins/project-orchestrator/references/sop.md +156 -0
- package/runtime/plugins/project-orchestrator/runtime/assets.py +52 -57
- package/runtime/plugins/project-orchestrator/runtime/case.py +139 -80
- package/runtime/plugins/project-orchestrator/runtime/case_structure.py +71 -0
- package/runtime/plugins/project-orchestrator/runtime/case_workspace.py +47 -46
- package/runtime/plugins/project-orchestrator/runtime/i18n_orchestrator.py +78 -0
- package/runtime/plugins/project-orchestrator/runtime/locale/en.json +1450 -0
- package/runtime/plugins/project-orchestrator/runtime/memory.py +45 -45
- package/runtime/plugins/project-orchestrator/runtime/private_paths.py +119 -0
- package/runtime/plugins/project-orchestrator/runtime/project_base.py +37 -19
- package/runtime/plugins/project-orchestrator/runtime/project_cli.py +307 -195
- package/runtime/plugins/project-orchestrator/runtime/project_demo.py +130 -48
- package/runtime/plugins/project-orchestrator/runtime/project_maintain.py +328 -221
- package/runtime/plugins/project-orchestrator/runtime/project_menu.py +167 -89
- package/runtime/plugins/project-orchestrator/runtime/project_real.py +187 -105
- package/runtime/plugins/project-orchestrator/runtime/project_write.py +58 -55
- package/runtime/plugins/project-orchestrator/runtime/sop.py +1670 -0
- package/runtime/plugins/project-orchestrator/runtime/sop_structure.py +226 -0
- package/runtime/plugins/project-orchestrator/runtime/wf.py +74 -70
- package/runtime/plugins/project-orchestrator/skills/project-orchestrator/SKILL.md +22 -0
- package/runtime/plugins/project-orchestrator/sop-templates/blank/SOP.en.md +57 -0
- package/runtime/plugins/project-orchestrator/sop-templates/blank/SOP.md +57 -0
- package/runtime/plugins/project-orchestrator/sop-templates/knowledge-digest/SOP.en.md +77 -0
- package/runtime/plugins/project-orchestrator/sop-templates/knowledge-digest/SOP.md +77 -0
- package/runtime/plugins/project-orchestrator/sop-templates/knowledge-digest/templates/digest.en.md +11 -0
- package/runtime/plugins/project-orchestrator/sop-templates/knowledge-digest/templates/digest.md +11 -0
- package/runtime/plugins/project-orchestrator/sop-templates/video-to-transcript/SOP.en.md +87 -0
- package/runtime/plugins/project-orchestrator/sop-templates/video-to-transcript/SOP.md +87 -0
- package/runtime/plugins/project-orchestrator/sop-templates/weekly-review/SOP.en.md +75 -0
- package/runtime/plugins/project-orchestrator/sop-templates/weekly-review/SOP.md +75 -0
- package/runtime/plugins/project-orchestrator/sop-templates/weekly-review/templates/review.en.md +11 -0
- package/runtime/plugins/project-orchestrator/sop-templates/weekly-review/templates/review.md +12 -0
- package/runtime/plugins/project-orchestrator/templates/CASE.en.md +39 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/test_init_project.py +0 -678
- package/runtime/plugins/agents-init/skills/agents-init/scripts/test_scaffold.py +0 -551
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""取得内容包的历史版本,用来重新渲染升级前的基线正文。
|
|
2
|
+
|
|
3
|
+
来源按这个顺序找,前一个没有就找下一个,全部取不到时返回空(调用方退回“保留 / 采用 / 追加”的选择):
|
|
4
|
+
|
|
5
|
+
1. 本机运行时缓存:``<数据目录>/agenthelm/runtimes/<版本>/plugins/agents-init/``;
|
|
6
|
+
2. 本地 git tag:随检出带来的 ``v*`` tag 里的 ``plugins/agents-init/``;
|
|
7
|
+
3. GitHub Release:发布包 ``agenthelm-<tag>.tar.gz``,下载后核对 ``release.json`` 里的 SHA-256。
|
|
8
|
+
|
|
9
|
+
每个来源都要通过内容包自己的摘要校验(``content.json`` 里每个源文件的 SHA-256);渲染出的基线是否可信,由调用方拿内容锁
|
|
10
|
+
里记录的文件摘要再核对一次。网络访问只经 ``fetch`` 注入点,测试用本地夹具替换;``AGENTHELM_NO_UPDATE_CHECK=1`` 关闭联网。
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import io
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
import subprocess
|
|
19
|
+
import tarfile
|
|
20
|
+
import urllib.request
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Callable, Iterator
|
|
24
|
+
|
|
25
|
+
REPOSITORY = "lc4t/agenthelm"
|
|
26
|
+
PLUGIN = "agents-init"
|
|
27
|
+
TEMPLATE_NAME = "AGENT.template.md"
|
|
28
|
+
LAUNCHER_NAME = "launcher.sh"
|
|
29
|
+
MAX_RELEASES = 20 # 联网最多看最近这么多个发布
|
|
30
|
+
MAX_ARCHIVE_BYTES = 64 * 1024 * 1024
|
|
31
|
+
|
|
32
|
+
Fetch = Callable[[str], bytes] # 抓取失败抛 OSError
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class PackSource:
|
|
37
|
+
label: str # 给人看的来源,如 “git tag v6.8.0”
|
|
38
|
+
kind: str # cache | git-tag | release
|
|
39
|
+
version: str # 内容包版本(content.json 里的 version)
|
|
40
|
+
template: str
|
|
41
|
+
launcher: str | None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def sha256(data: bytes) -> str:
|
|
45
|
+
return hashlib.sha256(data).hexdigest()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _verified(content: object, pack_id: str, read: Callable[[str], bytes | None], label: str, kind: str) -> PackSource | None:
|
|
49
|
+
"""content.json 里的内容包 + 它声明的源文件;任何一个文件缺失或摘要不符都丢弃这个来源。"""
|
|
50
|
+
if not isinstance(content, dict):
|
|
51
|
+
return None
|
|
52
|
+
pack = next((item for item in content.get("packs", []) if isinstance(item, dict) and item.get("id") == pack_id), None)
|
|
53
|
+
if not pack or not pack.get("version"):
|
|
54
|
+
return None
|
|
55
|
+
texts: dict[str, bytes] = {}
|
|
56
|
+
for source in pack.get("sources", []):
|
|
57
|
+
data = read(source)
|
|
58
|
+
expected = (pack.get("files") or {}).get(source)
|
|
59
|
+
if data is None or not expected or sha256(data) != expected:
|
|
60
|
+
return None
|
|
61
|
+
texts[Path(source).name] = data
|
|
62
|
+
if TEMPLATE_NAME not in texts:
|
|
63
|
+
return None
|
|
64
|
+
try:
|
|
65
|
+
launcher = texts[LAUNCHER_NAME].decode("utf-8") if LAUNCHER_NAME in texts else None
|
|
66
|
+
return PackSource(label, kind, str(pack["version"]), texts[TEMPLATE_NAME].decode("utf-8"), launcher)
|
|
67
|
+
except UnicodeDecodeError:
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _json(data: bytes | None) -> object:
|
|
72
|
+
try:
|
|
73
|
+
return json.loads(data.decode("utf-8")) if data is not None else None
|
|
74
|
+
except (UnicodeDecodeError, ValueError):
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _order(sources: Iterator[PackSource], wanted: list[str]) -> Iterator[PackSource]:
|
|
79
|
+
"""想要的版本先给,其余的随后(调用方靠摘要核对,所以其余版本也可能命中)。"""
|
|
80
|
+
rest = []
|
|
81
|
+
for source in sources:
|
|
82
|
+
if source.version in wanted:
|
|
83
|
+
yield source
|
|
84
|
+
else:
|
|
85
|
+
rest.append(source)
|
|
86
|
+
yield from rest
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------- 1. runtime cache
|
|
90
|
+
|
|
91
|
+
def runtime_cache_dir(environ: dict | None = None) -> Path:
|
|
92
|
+
environ = os.environ if environ is None else environ
|
|
93
|
+
base = environ.get("XDG_DATA_HOME")
|
|
94
|
+
return (Path(base) if base else Path.home() / ".local" / "share") / "agenthelm" / "runtimes"
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def from_runtime_cache(pack_id: str, wanted: list[str], directory: Path | None = None) -> Iterator[PackSource]:
|
|
98
|
+
directory = directory or runtime_cache_dir()
|
|
99
|
+
|
|
100
|
+
def sources() -> Iterator[PackSource]:
|
|
101
|
+
try:
|
|
102
|
+
versions = sorted((item for item in directory.iterdir() if item.is_dir() and not item.name.startswith(".")),
|
|
103
|
+
key=lambda item: item.name, reverse=True)
|
|
104
|
+
except OSError:
|
|
105
|
+
return
|
|
106
|
+
for version in versions:
|
|
107
|
+
root = version / "plugins" / PLUGIN
|
|
108
|
+
if root.is_symlink():
|
|
109
|
+
continue
|
|
110
|
+
|
|
111
|
+
def read(source: str, root=root) -> bytes | None:
|
|
112
|
+
path = root / source
|
|
113
|
+
try:
|
|
114
|
+
return path.read_bytes() if path.is_file() and not path.is_symlink() else None
|
|
115
|
+
except OSError:
|
|
116
|
+
return None
|
|
117
|
+
|
|
118
|
+
found = _verified(_json(read("content.json")), pack_id, read, f"runtime cache {version.name}", "cache")
|
|
119
|
+
if found:
|
|
120
|
+
yield found
|
|
121
|
+
|
|
122
|
+
yield from _order(sources(), wanted)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
# ---------------------------------------------------------------- 2. local git tags
|
|
126
|
+
|
|
127
|
+
def _git(repo: Path, *args: str) -> bytes | None:
|
|
128
|
+
try:
|
|
129
|
+
done = subprocess.run(["git", "-C", str(repo), *args], capture_output=True, check=False, timeout=15)
|
|
130
|
+
except (OSError, subprocess.SubprocessError):
|
|
131
|
+
return None
|
|
132
|
+
return done.stdout if done.returncode == 0 else None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def from_git_tags(pack_id: str, wanted: list[str], repo: Path | None = None) -> Iterator[PackSource]:
|
|
136
|
+
repo = repo or Path(__file__).resolve().parents[3]
|
|
137
|
+
prefix = _git(repo, "rev-parse", "--show-prefix")
|
|
138
|
+
tags = _git(repo, "tag", "--list", "v*", "--sort=-v:refname")
|
|
139
|
+
if prefix is None or tags is None:
|
|
140
|
+
return
|
|
141
|
+
base = prefix.decode("utf-8", "replace").strip() # 如 plugins/agents-init/
|
|
142
|
+
if not base:
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
def sources() -> Iterator[PackSource]:
|
|
146
|
+
for tag in tags.decode("utf-8", "replace").split():
|
|
147
|
+
def read(source: str, tag=tag) -> bytes | None:
|
|
148
|
+
return _git(repo, "show", f"{tag}:{base}{source}")
|
|
149
|
+
|
|
150
|
+
found = _verified(_json(read("content.json")), pack_id, read, f"git tag {tag}", "git-tag")
|
|
151
|
+
if found:
|
|
152
|
+
yield found
|
|
153
|
+
|
|
154
|
+
yield from _order(sources(), wanted)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# ---------------------------------------------------------------- 3. GitHub Release
|
|
158
|
+
|
|
159
|
+
def urlopen_fetch(url: str) -> bytes:
|
|
160
|
+
request = urllib.request.Request(url, headers={"User-Agent": "agenthelm"})
|
|
161
|
+
with urllib.request.urlopen(request, timeout=20) as response: # noqa: S310 - https URL built from REPOSITORY
|
|
162
|
+
data = response.read(MAX_ARCHIVE_BYTES + 1)
|
|
163
|
+
if len(data) > MAX_ARCHIVE_BYTES:
|
|
164
|
+
raise OSError("response too large")
|
|
165
|
+
return data
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _tar_reader(archive: bytes, root: str) -> Callable[[str], bytes | None]:
|
|
169
|
+
"""只读发布包里 ``<顶层目录>/plugins/agents-init/<文件>``,不解压到磁盘。"""
|
|
170
|
+
def read(source: str) -> bytes | None:
|
|
171
|
+
try:
|
|
172
|
+
with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as handle:
|
|
173
|
+
for member in handle:
|
|
174
|
+
parts = Path(member.name).parts
|
|
175
|
+
if member.isfile() and len(parts) >= 3 and parts[1:3] == ("plugins", PLUGIN) \
|
|
176
|
+
and "/".join(parts[3:]) == source and ".." not in parts:
|
|
177
|
+
extracted = handle.extractfile(member)
|
|
178
|
+
return extracted.read() if extracted else None
|
|
179
|
+
except (tarfile.TarError, OSError, EOFError):
|
|
180
|
+
return None
|
|
181
|
+
return None
|
|
182
|
+
|
|
183
|
+
return read
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def from_github_release(pack_id: str, wanted: list[str], fetch: Fetch | None = None,
|
|
187
|
+
repository: str = REPOSITORY, environ: dict | None = None) -> Iterator[PackSource]:
|
|
188
|
+
"""只下载 content.json 的版本在 ``wanted`` 里的发布;发布包摘要来自同一发布的 release.json。"""
|
|
189
|
+
environ = os.environ if environ is None else environ
|
|
190
|
+
if environ.get("AGENTHELM_NO_UPDATE_CHECK") == "1" or not wanted:
|
|
191
|
+
return
|
|
192
|
+
fetch = fetch or urlopen_fetch
|
|
193
|
+
try:
|
|
194
|
+
releases = json.loads(fetch(f"https://api.github.com/repos/{repository}/releases?per_page={MAX_RELEASES}"))
|
|
195
|
+
except (OSError, ValueError):
|
|
196
|
+
return
|
|
197
|
+
for release in releases if isinstance(releases, list) else []:
|
|
198
|
+
tag = release.get("tag_name") if isinstance(release, dict) else None
|
|
199
|
+
if not isinstance(tag, str) or not tag:
|
|
200
|
+
continue
|
|
201
|
+
try:
|
|
202
|
+
peek = _json(fetch(f"https://raw.githubusercontent.com/{repository}/{tag}/plugins/{PLUGIN}/content.json"))
|
|
203
|
+
pack = next((item for item in (peek or {}).get("packs", []) if isinstance(item, dict) and item.get("id") == pack_id), None) \
|
|
204
|
+
if isinstance(peek, dict) else None
|
|
205
|
+
if not pack or str(pack.get("version")) not in wanted:
|
|
206
|
+
continue
|
|
207
|
+
info = json.loads(fetch(f"https://github.com/{repository}/releases/download/{tag}/release.json"))
|
|
208
|
+
archive = fetch(info.get("tarball_url") or f"https://github.com/{repository}/releases/download/{tag}/agenthelm-{tag}.tar.gz")
|
|
209
|
+
except (OSError, ValueError, AttributeError):
|
|
210
|
+
continue
|
|
211
|
+
if not isinstance(info, dict) or sha256(archive) != info.get("tarball_sha256"):
|
|
212
|
+
continue
|
|
213
|
+
found = _verified(_json(_tar_reader(archive, tag)("content.json")), pack_id,
|
|
214
|
+
_tar_reader(archive, tag), f"GitHub Release {tag}", "release")
|
|
215
|
+
if found:
|
|
216
|
+
yield found
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def default_providers(fetch: Fetch | None = None) -> list[Callable[[str, list[str]], Iterator[PackSource]]]:
|
|
220
|
+
"""缓存 → git tag → GitHub Release;每个提供者接收 ``(内容包 id, 想要的版本)``。"""
|
|
221
|
+
return [from_runtime_cache, from_git_tags, lambda pack_id, wanted: from_github_release(pack_id, wanted, fetch)]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""运行时文案的多语言:源语言是中文,``_("中文原文")`` 在中文下原样返回,在英文下查 ``locale/en.json``。
|
|
2
|
+
|
|
3
|
+
语言按这个顺序决定:调用方指定(如 ``nudge.language``)→ 环境变量 ``AGENTHELM_LANG`` → 用户配置
|
|
4
|
+
``~/.config/agenthelm/config.json`` 的 ``language`` → 系统 locale(``LC_ALL``、``LC_MESSAGES``、``LANG``)→ 英文。
|
|
5
|
+
``auto``、空值和 C/POSIX 视为未指定;中文 locale 用中文,其他语言的 locale 暂用英文。
|
|
6
|
+
没有英文译文的文案原样输出(中文),测试守住译文完整。四个插件各带一份相同的本模块(模块名不同,以免
|
|
7
|
+
同一进程里互相覆盖),``tests/test_i18n_parity.py`` 守住它们行为一致。模块级常量里的文案用 ``N_`` 标记
|
|
8
|
+
(原样返回,仅供提取),使用处再调用 ``_``,这样语言在运行时而不是导入时决定。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
import os
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Mapping
|
|
17
|
+
|
|
18
|
+
LANGUAGES = ("zh", "en")
|
|
19
|
+
DEFAULT_LANGUAGE = "en"
|
|
20
|
+
LOCALE_DIR = Path(__file__).resolve().parent / "locale"
|
|
21
|
+
_catalogs: dict[str, dict[str, str]] = {}
|
|
22
|
+
_configured: dict[str, tuple[tuple, str | None]] = {}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def normalize(value: object) -> str | None:
|
|
26
|
+
text = str(value or "").strip().lower().replace("_", "-")
|
|
27
|
+
if not text or text in ("auto", "c", "posix") or text.startswith(("c.", "posix.")):
|
|
28
|
+
return None
|
|
29
|
+
return "zh" if text.startswith("zh") else "en"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def configured_language(environ: Mapping[str, str]) -> str | None:
|
|
33
|
+
"""用户配置里的 language;按文件修改时间缓存,读不了就当没有。"""
|
|
34
|
+
base = environ.get("XDG_CONFIG_HOME") or (str(Path(environ["HOME"]) / ".config") if environ.get("HOME") else "")
|
|
35
|
+
if not base:
|
|
36
|
+
return None
|
|
37
|
+
path = Path(base) / "agenthelm" / "config.json"
|
|
38
|
+
try:
|
|
39
|
+
info = path.stat()
|
|
40
|
+
signature = (info.st_mtime_ns, info.st_size)
|
|
41
|
+
cached = _configured.get(str(path))
|
|
42
|
+
if cached and cached[0] == signature:
|
|
43
|
+
return cached[1]
|
|
44
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
45
|
+
value = normalize(data.get("language")) if isinstance(data, dict) else None
|
|
46
|
+
except (OSError, ValueError):
|
|
47
|
+
return None
|
|
48
|
+
_configured[str(path)] = (signature, value)
|
|
49
|
+
return value
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def resolve_language(requested: str | None = None, environ: Mapping[str, str] | None = None) -> str:
|
|
53
|
+
environ = os.environ if environ is None else environ
|
|
54
|
+
for candidate in (requested, environ.get("AGENTHELM_LANG"), configured_language(environ),
|
|
55
|
+
environ.get("LC_ALL"), environ.get("LC_MESSAGES"), environ.get("LANG")):
|
|
56
|
+
language = normalize(candidate)
|
|
57
|
+
if language:
|
|
58
|
+
return language
|
|
59
|
+
return DEFAULT_LANGUAGE
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def catalog(language: str) -> dict[str, str]:
|
|
63
|
+
if language not in _catalogs:
|
|
64
|
+
try:
|
|
65
|
+
data = json.loads((LOCALE_DIR / f"{language}.json").read_text(encoding="utf-8"))
|
|
66
|
+
except (OSError, ValueError):
|
|
67
|
+
data = {}
|
|
68
|
+
_catalogs[language] = data if isinstance(data, dict) else {}
|
|
69
|
+
return _catalogs[language]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _(message: str) -> str:
|
|
73
|
+
language = resolve_language()
|
|
74
|
+
return message if language == "zh" else catalog(language).get(message, message)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def N_(message: str) -> str:
|
|
78
|
+
return message
|