This commit is contained in:
richarjiang
2026-09-24 10:04:41 +08:00
commit eae4df7d51
24 changed files with 2212 additions and 0 deletions

106
README.md Normal file
View File

@@ -0,0 +1,106 @@
# Laya Router Hook
本地常驻 [Laya](https://github.com/NandhaKishorM/laya) multilingual 模型,在 Codex 和 Claude Code 每次提交 prompt 时注入一条 System-1 路由提示。它不切换底层模型,只补充任务复杂度、是否需要计划、读仓库、查文档和深度推理的判断。
```text
Prompt
-> UserPromptSubmit hook(标准库,失败则放行)
-> http://127.0.0.1:8765
-> Laya multilingual
-> additionalContext
-> Codex / Claude Code
```
Cursor 会加载 Claude Code 的 hook 配置,但当前事件无法把上下文注入对话。Hook 发现 `CURSOR_VERSION` 时直接返回,不增加延迟。
## 安装
需要 macOS、[uv](https://docs.astral.sh/uv/getting-started/installation/) 和 Python 3.10+。
```bash
./install.sh
```
脚本会:
1. 在 `~/.laya-router/.venv` 创建虚拟环境,并把本仓库安装进去
2. 下载 `convaiinnovations/laya` 的 `multilingual` 权重
3. 注册 launchd 用户服务 `com.laya-router`(登录自启,进程退出后拉起)
4. 检测到 Codex / Claude Code 时写入 hook
安装的是一份普通副本,不是指向仓库的可编辑安装。仓库如果在 `~/Documents` 下,launchd 直接 import 那个目录会卡在文件打开上。`laya-router restart` 会先把当前仓库同步进虚拟环境再重启。
只注册一边:
```bash
./install.sh --only codex
./install.sh --only claude
```
Codex 第一次会要求信任 hook。启动 `codex` 后运行 `/hooks` 确认。命令行本身不变时,修改 Python 不会再次触发信任审查。
## 日常命令
命令来自 `~/.laya-router/.venv`。`./install.sh` 之后可以把这个 `bin` 加到 `PATH`,或直接用绝对路径。
浏览器打开 [http://127.0.0.1:8765/](http://127.0.0.1:8765/) 可以按会话看每次判断。页面通过服务端事件实时追加,左边是会话,右边是这一路 prompt 的判定过程。
```bash
laya-router status
laya-router logs
laya-router logs -f
laya-router route "帮我把这个函数的变量名改掉"
laya-router restart
laya-router stop
laya-router start
```
卸载 hook 和服务,但保留运行目录:
```bash
./uninstall.sh
```
连同配置、日志、虚拟环境一起删除:
```bash
./uninstall.sh --purge
```
## 配置
`~/.laya-router/config.toml` 在首次安装时生成,之后不会被覆盖。
| 键 | 含义 |
| --- | --- |
| `device` | `auto`、`mps`、`cuda` 或 `cpu`。`auto` 预热失败会回退 |
| `log_prompts` | 是否把原文写入 `data/routes.jsonl` |
| `request_timeout` | Hook 等待服务的秒数,默认 2.5 |
| `max_prompt_chars` | 送入模型的最大字符数,超长保留头尾 |
改完配置或服务端代码后执行 `laya-router restart`。
## 迭代
改完保存后执行 `laya-router restart`。它会把仓库同步进 `~/.laya-router` 再拉起服务。Hook 是每次 prompt 新起的进程,同步完成后下一条 prompt 就会用到新的 `hook.py` 和 `render.py`。
| 文件 | 作用 | 生效方式 |
| --- | --- | --- |
| `src/laya_router/questions.py` | 路由问题。改措辞时把 `QUESTIONS_VERSION` 加一 | `laya-router restart` |
| `src/laya_router/render.py` | 注入给 Agent 的提示文本 | `laya-router restart`,然后下一条 prompt |
| `src/laya_router/hook.py` | 超时、跳过斜杠命令、失败放行 | `laya-router restart`,然后下一条 prompt |
| `src/laya_router/server.py` | 模型加载与 `/route` | `laya-router restart` |
不要给服务加 `--reload`。开发模式会再起进程,把模型加载多次。
路由记录在 `~/.laya-router/data/routes.jsonl`,包含判断、耗时和问题版本,用来以后对照真实任务做微调。`laya-multilingual` 没有自带温度校准,注入文本里的概率只是提示。
服务挂掉时 hook 输出 `{}` 并退出 0,Codex 和 Claude Code 照常工作。原因写在 `~/.laya-router/logs/hook.log`。
## 开发
```bash
PYTHONPATH=src uv run --no-project --with pytest python -m pytest
```
单元测试不加载模型。装好服务后用 `laya-router route` 做真实推理检查。