haoyuan 1.1.1 → 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/README.md +7 -2
- package/api/configs/models.py +12 -0
- package/api/configs/secrets.py +8 -0
- package/api/tests/test_secrets_config.py +23 -0
- package/bin/haoyuan.js +135 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -128,6 +128,7 @@ haoyuan web
|
|
|
128
128
|
| 命令 | 作用 |
|
|
129
129
|
|---|---|
|
|
130
130
|
| `haoyuan` | 终端对话(流式输出、工具调用、审批确认) |
|
|
131
|
+
| `haoyuan --model x/y` | 以指定模型启动对话(如 `--model deepseek/deepseek-flash`) |
|
|
131
132
|
| `haoyuan web` | 启动服务并自动打开网页 |
|
|
132
133
|
| `haoyuan web --no-open` | 启动服务但不打开浏览器 |
|
|
133
134
|
| `haoyuan --version` / `-V` | 查看版本号 |
|
|
@@ -136,7 +137,7 @@ haoyuan web
|
|
|
136
137
|
| `haoyuan setup` | 仅预装依赖 |
|
|
137
138
|
| `haoyuan help` | 显示帮助 |
|
|
138
139
|
|
|
139
|
-
终端内命令:`/new` 新建会话、`/web` 在浏览器打开当前会话、`/cancel` 取消当前任务、`/exit` 退出(Ctrl+C 同样有效)。
|
|
140
|
+
终端内命令:`/models` 查看/选择模型(序号选择,`/model x/y` 直选)、`/new` 新建会话、`/web` 在浏览器打开当前会话、`/cancel` 取消当前任务、`/exit` 退出(Ctrl+C 同样有效)。
|
|
140
141
|
|
|
141
142
|
网页默认地址 `http://127.0.0.1:8030`,端口被占用自动 +1。未配置模型 Key 时使用内置 `mock` 模型,会话、工具调用、审批与前端均可完整运行。
|
|
142
143
|
|
|
@@ -210,10 +211,14 @@ haoyuan config --open # 直接在编辑器中打开
|
|
|
210
211
|
"DEEPSEEK_BASE_URL": "https://api.deepseek.com",
|
|
211
212
|
"DEEPSEEK_MODEL": "deepseek-flash",
|
|
212
213
|
"ZHIPU_API_KEY": "",
|
|
213
|
-
"MIMO_API_KEY": ""
|
|
214
|
+
"MIMO_API_KEY": "",
|
|
215
|
+
"PROVIDER": "",
|
|
216
|
+
"MODEL": ""
|
|
214
217
|
}
|
|
215
218
|
```
|
|
216
219
|
|
|
220
|
+
`PROVIDER` / `MODEL` 可锁定默认模型(如 `"MODEL": "deepseek-v4-pro"`,留空则按已配置 Key 自动选择)。
|
|
221
|
+
|
|
217
222
|
**包内配置(可选)**:
|
|
218
223
|
|
|
219
224
|
```bash
|
package/api/configs/models.py
CHANGED
|
@@ -12,6 +12,7 @@ from configs.secrets import (
|
|
|
12
12
|
MIMO_MODEL,
|
|
13
13
|
ZHIPU_API_KEY,
|
|
14
14
|
ZHIPU_BASE_URL,
|
|
15
|
+
global_config,
|
|
15
16
|
)
|
|
16
17
|
|
|
17
18
|
|
|
@@ -102,6 +103,17 @@ def price_for(provider: str, model: str | None = None) -> dict[str, float]:
|
|
|
102
103
|
def auto_default() -> tuple[str, str]:
|
|
103
104
|
if os.getenv("HAOYUAN_FORCE_MOCK", "").strip().lower() in {"1", "true", "yes"}:
|
|
104
105
|
return "mock", "mock-deterministic"
|
|
106
|
+
gp = global_config("PROVIDER")
|
|
107
|
+
gm = global_config("MODEL")
|
|
108
|
+
if gp and gp != "auto" and gp in PROVIDERS:
|
|
109
|
+
conf = PROVIDERS[gp]
|
|
110
|
+
if gm and gm != "auto" and gm in conf.models:
|
|
111
|
+
return gp, gm
|
|
112
|
+
return gp, conf.default_model
|
|
113
|
+
if gm and gm != "auto":
|
|
114
|
+
for name, conf in PROVIDERS.items():
|
|
115
|
+
if gm in conf.models:
|
|
116
|
+
return name, gm
|
|
105
117
|
if DEEPSEEK_API_KEY:
|
|
106
118
|
return "deepseek", DEEPSEEK_MODEL
|
|
107
119
|
if ZHIPU_API_KEY:
|
package/api/configs/secrets.py
CHANGED
|
@@ -30,6 +30,14 @@ def _read_global_config() -> dict:
|
|
|
30
30
|
_GLOBAL_CONFIG = _read_global_config()
|
|
31
31
|
|
|
32
32
|
|
|
33
|
+
def global_config(name: str, default: str = "") -> str:
|
|
34
|
+
value = _GLOBAL_CONFIG.get(name)
|
|
35
|
+
if value is None:
|
|
36
|
+
return default
|
|
37
|
+
text = str(value).strip()
|
|
38
|
+
return text or default
|
|
39
|
+
|
|
40
|
+
|
|
33
41
|
def _get(name: str, default: str = "") -> str:
|
|
34
42
|
env_value = os.getenv(name)
|
|
35
43
|
if env_value is not None and env_value.strip():
|
|
@@ -59,3 +59,26 @@ def test_local_file_fallback(tmp_path, monkeypatch):
|
|
|
59
59
|
mod = _reload()
|
|
60
60
|
assert mod.DEEPSEEK_API_KEY != "sk-from-nowhere"
|
|
61
61
|
assert mod.secret_source("DEEPSEEK_API_KEY") in {"configs/secrets_local.py", "default"}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_auto_default_from_global_config(tmp_path, monkeypatch):
|
|
65
|
+
import importlib
|
|
66
|
+
|
|
67
|
+
from configs import models as models_mod
|
|
68
|
+
|
|
69
|
+
cfg_file = tmp_path / "config.json"
|
|
70
|
+
cfg_file.write_text(json.dumps({"PROVIDER": "zhipu"}), encoding="utf-8")
|
|
71
|
+
monkeypatch.setenv("HAOYUAN_CONFIG", str(cfg_file))
|
|
72
|
+
monkeypatch.delenv("HAOYUAN_FORCE_MOCK", raising=False)
|
|
73
|
+
_reload()
|
|
74
|
+
models = importlib.reload(models_mod)
|
|
75
|
+
assert models.auto_default() == ("zhipu", "glm-5.3-flash")
|
|
76
|
+
|
|
77
|
+
cfg_file.write_text(json.dumps({"MODEL": "deepseek-v4-pro"}), encoding="utf-8")
|
|
78
|
+
_reload()
|
|
79
|
+
models = importlib.reload(models_mod)
|
|
80
|
+
assert models.auto_default() == ("deepseek", "deepseek-v4-pro")
|
|
81
|
+
|
|
82
|
+
monkeypatch.delenv("HAOYUAN_CONFIG")
|
|
83
|
+
_reload()
|
|
84
|
+
importlib.reload(models_mod)
|
package/bin/haoyuan.js
CHANGED
|
@@ -172,14 +172,14 @@ function readApiPort() {
|
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
async function healthyPort(port) {
|
|
175
|
-
if (!port) return
|
|
175
|
+
if (!port) return 0;
|
|
176
176
|
try {
|
|
177
177
|
const res = await fetch(`http://127.0.0.1:${port}/api/health`, {
|
|
178
178
|
signal: AbortSignal.timeout(2000),
|
|
179
179
|
});
|
|
180
|
-
return res.ok;
|
|
180
|
+
return res.ok ? port : 0;
|
|
181
181
|
} catch {
|
|
182
|
-
return
|
|
182
|
+
return 0;
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -425,6 +425,8 @@ const CONFIG_TEMPLATE = JSON.stringify(
|
|
|
425
425
|
DEEPSEEK_MODEL: "deepseek-flash",
|
|
426
426
|
ZHIPU_API_KEY: "",
|
|
427
427
|
MIMO_API_KEY: "",
|
|
428
|
+
PROVIDER: "",
|
|
429
|
+
MODEL: "",
|
|
428
430
|
},
|
|
429
431
|
null,
|
|
430
432
|
2
|
|
@@ -464,7 +466,7 @@ function configCommand({ open = false } = {}) {
|
|
|
464
466
|
);
|
|
465
467
|
}
|
|
466
468
|
|
|
467
|
-
async function chat() {
|
|
469
|
+
async function chat(options = {}) {
|
|
468
470
|
const { port, child: serverChild } = await ensureServer();
|
|
469
471
|
|
|
470
472
|
const die = (msg) => {
|
|
@@ -480,8 +482,10 @@ async function chat() {
|
|
|
480
482
|
|
|
481
483
|
const state = {
|
|
482
484
|
sessionId: "",
|
|
485
|
+
model: "",
|
|
483
486
|
busy: false,
|
|
484
487
|
approval: null,
|
|
488
|
+
picking: null,
|
|
485
489
|
inText: false,
|
|
486
490
|
inReasoning: false,
|
|
487
491
|
streamReq: null,
|
|
@@ -710,11 +714,20 @@ async function chat() {
|
|
|
710
714
|
}
|
|
711
715
|
|
|
712
716
|
async function newSession() {
|
|
713
|
-
const
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
+
const body = { kind: "auto", workspace: process.cwd() };
|
|
718
|
+
const wanted = (options.model || "").trim();
|
|
719
|
+
if (wanted) {
|
|
720
|
+
const slash = wanted.indexOf("/");
|
|
721
|
+
if (slash > 0) {
|
|
722
|
+
body.provider = wanted.slice(0, slash);
|
|
723
|
+
body.model = wanted.slice(slash + 1);
|
|
724
|
+
} else {
|
|
725
|
+
body.model = wanted;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
const data = await apiJson(port, "POST", "/api/sessions", body);
|
|
717
729
|
state.sessionId = data.id;
|
|
730
|
+
state.model = `${data.provider || "mock"}/${data.model || "auto"}`;
|
|
718
731
|
state.lastEventId = 0;
|
|
719
732
|
openStream();
|
|
720
733
|
return data;
|
|
@@ -748,15 +761,95 @@ async function chat() {
|
|
|
748
761
|
process.stdout.write(
|
|
749
762
|
[
|
|
750
763
|
`${C.bold}终端命令${C.reset}`,
|
|
751
|
-
" /
|
|
752
|
-
" /
|
|
753
|
-
" /
|
|
754
|
-
" /
|
|
764
|
+
" /models [x/y] 查看/选择模型(如 /model deepseek/deepseek-flash)",
|
|
765
|
+
" /new 新建会话",
|
|
766
|
+
" /web 在浏览器打开当前会话",
|
|
767
|
+
" /cancel 取消当前任务",
|
|
768
|
+
" /exit 退出(也可 Ctrl+C)",
|
|
755
769
|
"",
|
|
756
770
|
].join("\n")
|
|
757
771
|
);
|
|
758
772
|
}
|
|
759
773
|
|
|
774
|
+
function buildModelOptions(providers) {
|
|
775
|
+
const options = [{ provider: "mock", model: "mock-deterministic", configured: true }];
|
|
776
|
+
for (const p of providers || []) {
|
|
777
|
+
for (const m of p.models || []) {
|
|
778
|
+
options.push({
|
|
779
|
+
provider: p.provider,
|
|
780
|
+
model: m,
|
|
781
|
+
configured: !!p.configured,
|
|
782
|
+
default: m === p.default_model,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return options;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
function printModelOptions(options) {
|
|
790
|
+
process.stdout.write(`${C.bold}可用模型:${C.reset}\n`);
|
|
791
|
+
options.forEach((opt, i) => {
|
|
792
|
+
const label = `${opt.provider}/${opt.model}`;
|
|
793
|
+
const parts = [` ${i + 1}) ${label}`];
|
|
794
|
+
if (label === state.model) parts.push(`${C.green}* 当前${C.reset}`);
|
|
795
|
+
if (opt.provider === "mock") parts.push(`${C.dim}(本地调试)${C.reset}`);
|
|
796
|
+
else if (!opt.configured) parts.push(`${C.dim}(Key 未配置,选择后无法调用)${C.reset}`);
|
|
797
|
+
else if (opt.default) parts.push(`${C.dim}(该厂商默认)${C.reset}`);
|
|
798
|
+
process.stdout.write(parts.join(" ") + "\n");
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
async function setModel(opt) {
|
|
803
|
+
try {
|
|
804
|
+
await apiJson(port, "PATCH", `/api/sessions/${encodeURIComponent(state.sessionId)}`, {
|
|
805
|
+
provider: opt.provider,
|
|
806
|
+
model: opt.model,
|
|
807
|
+
});
|
|
808
|
+
state.model = `${opt.provider}/${opt.model}`;
|
|
809
|
+
process.stdout.write(`${C.green}[已切换模型] ${state.model}${C.reset}\n`);
|
|
810
|
+
} catch (err) {
|
|
811
|
+
warn(`切换失败: ${err.message}`);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
async function handlePick(text) {
|
|
816
|
+
const pick = state.picking;
|
|
817
|
+
if (!text) {
|
|
818
|
+
state.picking = null;
|
|
819
|
+
process.stdout.write(`${C.dim}[已取消选择]${C.reset}\n`);
|
|
820
|
+
return prompt();
|
|
821
|
+
}
|
|
822
|
+
const idx = Number.parseInt(text, 10);
|
|
823
|
+
if (!Number.isFinite(idx) || idx < 1 || idx > pick.options.length) {
|
|
824
|
+
process.stdout.write(`${C.dim}请输入 1-${pick.options.length} 的序号,或回车取消:${C.reset} `);
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
state.picking = null;
|
|
828
|
+
await setModel(pick.options[idx - 1]);
|
|
829
|
+
return prompt();
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
async function modelsCommand(argModel) {
|
|
833
|
+
const data = await apiJson(port, "GET", "/api/models");
|
|
834
|
+
const options = buildModelOptions(data && data.providers);
|
|
835
|
+
if (argModel) {
|
|
836
|
+
const slash = argModel.indexOf("/");
|
|
837
|
+
const opt = options.find((o) =>
|
|
838
|
+
slash > 0 ? `${o.provider}/${o.model}` === argModel : o.model === argModel
|
|
839
|
+
);
|
|
840
|
+
if (!opt) {
|
|
841
|
+
warn(`未知模型: ${argModel}(/models 查看可用列表)`);
|
|
842
|
+
return prompt();
|
|
843
|
+
}
|
|
844
|
+
await setModel(opt);
|
|
845
|
+
return prompt();
|
|
846
|
+
}
|
|
847
|
+
state.picking = { options };
|
|
848
|
+
printModelOptions(options);
|
|
849
|
+
rl.setPrompt(`${C.cyan}选择序号>${C.reset} `);
|
|
850
|
+
rl.prompt();
|
|
851
|
+
}
|
|
852
|
+
|
|
760
853
|
async function handleLine(line) {
|
|
761
854
|
const text = line.trim();
|
|
762
855
|
if (!text) {
|
|
@@ -771,6 +864,11 @@ async function chat() {
|
|
|
771
864
|
return;
|
|
772
865
|
}
|
|
773
866
|
|
|
867
|
+
if (state.picking) {
|
|
868
|
+
await handlePick(text);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
|
|
774
872
|
if (text.startsWith("/")) {
|
|
775
873
|
const cmd = text.split(/\s+/, 1)[0].toLowerCase();
|
|
776
874
|
if (cmd === "/exit" || cmd === "/quit") return shutdown(0);
|
|
@@ -778,6 +876,16 @@ async function chat() {
|
|
|
778
876
|
chatHelp();
|
|
779
877
|
return prompt();
|
|
780
878
|
}
|
|
879
|
+
if (cmd === "/models" || cmd === "/model") {
|
|
880
|
+
const arg = text.slice(cmd.length).trim();
|
|
881
|
+
try {
|
|
882
|
+
await modelsCommand(arg);
|
|
883
|
+
} catch (err) {
|
|
884
|
+
warn(`获取模型失败: ${err.message}`);
|
|
885
|
+
prompt();
|
|
886
|
+
}
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
781
889
|
if (cmd === "/new") {
|
|
782
890
|
try {
|
|
783
891
|
const session = await newSession();
|
|
@@ -828,19 +936,16 @@ async function chat() {
|
|
|
828
936
|
}
|
|
829
937
|
}
|
|
830
938
|
|
|
831
|
-
const
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
health && health.data && health.data.default_provider === "mock"
|
|
835
|
-
? `${C.dim}提示: 未配置模型 Key,当前使用本地 mock。运行 haoyuan config 进行全局配置。${C.reset}`
|
|
836
|
-
: "";
|
|
939
|
+
const mockHint = state.model.startsWith("mock/")
|
|
940
|
+
? `${C.dim}提示: 未配置模型 Key,当前使用本地 mock。运行 haoyuan config 进行全局配置,/models 切换模型。${C.reset}`
|
|
941
|
+
: "";
|
|
837
942
|
|
|
838
943
|
process.stdout.write(
|
|
839
944
|
[
|
|
840
945
|
"",
|
|
841
946
|
`${C.bold}豪源 HaoYuan v${VERSION}${C.reset} · 终端对话`,
|
|
842
|
-
`${C.dim}模型 ${model} · 工作区 ${process.cwd()}${C.reset}`,
|
|
843
|
-
`${C.dim}会话 ${session.id} ·
|
|
947
|
+
`${C.dim}模型 ${state.model} · 工作区 ${process.cwd()}${C.reset}`,
|
|
948
|
+
`${C.dim}会话 ${session.id} · /models 选模型 · /web 网页 · /help 帮助 · /exit 退出${C.reset}`,
|
|
844
949
|
mockHint,
|
|
845
950
|
"",
|
|
846
951
|
]
|
|
@@ -895,6 +1000,7 @@ function printHelp() {
|
|
|
895
1000
|
"",
|
|
896
1001
|
"选项:",
|
|
897
1002
|
" -V, --version 显示版本号",
|
|
1003
|
+
" --model x/y 指定模型(如 --model deepseek/deepseek-flash)",
|
|
898
1004
|
" --no-open web 启动后不打开浏览器",
|
|
899
1005
|
" --no-build setup 时跳过前端构建",
|
|
900
1006
|
"",
|
|
@@ -911,6 +1017,13 @@ const command = argv[0] || "";
|
|
|
911
1017
|
const flags = new Set(argv.slice(1));
|
|
912
1018
|
const noOpen = flags.has("--no-open");
|
|
913
1019
|
const noBuild = flags.has("--no-build");
|
|
1020
|
+
let modelFlag = "";
|
|
1021
|
+
const mi = argv.indexOf("--model");
|
|
1022
|
+
if (mi >= 0 && argv[mi + 1] && !argv[mi + 1].startsWith("--")) {
|
|
1023
|
+
modelFlag = argv[mi + 1].trim();
|
|
1024
|
+
}
|
|
1025
|
+
const modelEq = argv.find((a) => a.startsWith("--model="));
|
|
1026
|
+
if (modelEq) modelFlag = modelEq.slice(8).trim();
|
|
914
1027
|
|
|
915
1028
|
async function main() {
|
|
916
1029
|
if (command === "--version" || command === "-V" || command === "-v" || command === "version") {
|
|
@@ -921,8 +1034,8 @@ async function main() {
|
|
|
921
1034
|
printHelp();
|
|
922
1035
|
return;
|
|
923
1036
|
}
|
|
924
|
-
if (command === "" || command === "chat") {
|
|
925
|
-
await chat();
|
|
1037
|
+
if (command === "" || command === "chat" || command === "--model") {
|
|
1038
|
+
await chat({ model: modelFlag });
|
|
926
1039
|
return;
|
|
927
1040
|
}
|
|
928
1041
|
if (command === "web" || command === "start" || command === "serve") {
|