forgex-agent 0.5.1 → 0.5.3
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 +14 -3
- package/main.js +472 -226
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,14 +18,25 @@ forgex --version
|
|
|
18
18
|
## 快速开始
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
#
|
|
21
|
+
# 方式一:直接启动。首次运行会自动进入连接向导(provider → baseUrl → API Key → model)
|
|
22
|
+
forgex
|
|
23
|
+
|
|
24
|
+
# 方式二:先单独配置,会做一次连通性测试
|
|
22
25
|
forgex connect
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
首次运行会在 `~/.forgex/config.json` 生成配置。API Key 不写入配置文件,而是保存到环境变量(Windows 用户级环境变量;Unix 会提示 export 命令)。
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
如果只想用环境变量,也可以完全不建配置文件——启动时会按 Key 自动识别 Provider:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
export DEEPSEEK_API_KEY=sk-xxx # 自动使用 https://api.deepseek.com + deepseek-flash
|
|
25
34
|
forgex
|
|
35
|
+
|
|
36
|
+
# 也支持 DASHSCOPE_API_KEY / OPENAI_API_KEY
|
|
37
|
+
# 自定义端点仍用 FORGEX_BASE_URL / FORGEX_MODEL / FORGEX_API_KEY(或 forgex connect)
|
|
26
38
|
```
|
|
27
39
|
|
|
28
|
-
首次运行会在 `~/.forgex/config.json` 生成配置。API Key 不写入配置文件,而是保存到环境变量(Windows 用户级环境变量;Unix 会提示 export 命令)。
|
|
29
40
|
|
|
30
41
|
## TUI 命令
|
|
31
42
|
|
package/main.js
CHANGED
|
@@ -6,7 +6,7 @@ var __export = (target, all) => {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
// apps/cli/src/main.ts
|
|
9
|
-
import { stderr as
|
|
9
|
+
import { stderr as stderr4, stdout as stdout8 } from "node:process";
|
|
10
10
|
|
|
11
11
|
// packages/shared/dist/errors/index.js
|
|
12
12
|
var ForgeError = class _ForgeError extends Error {
|
|
@@ -506,7 +506,9 @@ var BOOLEAN_FLAGS = /* @__PURE__ */ new Set([
|
|
|
506
506
|
"json",
|
|
507
507
|
"yes",
|
|
508
508
|
"full",
|
|
509
|
-
"connect"
|
|
509
|
+
"connect",
|
|
510
|
+
// `--plan`:一次性任务也走计划模式(只读分析 + 只输出计划),供 CI / 管道做 dry-run
|
|
511
|
+
"plan"
|
|
510
512
|
]);
|
|
511
513
|
var KNOWN_FLAGS = /* @__PURE__ */ new Set([...VALUE_FLAGS, ...BOOLEAN_FLAGS]);
|
|
512
514
|
function unknownFlags(flags) {
|
|
@@ -572,6 +574,7 @@ function flagNumber(flags, name) {
|
|
|
572
574
|
// apps/cli/src/commands/chat.ts
|
|
573
575
|
import React2 from "react";
|
|
574
576
|
import { render } from "ink";
|
|
577
|
+
import { stderr as stderr2 } from "node:process";
|
|
575
578
|
|
|
576
579
|
// apps/cli/src/config/load-config.ts
|
|
577
580
|
import { existsSync, readFileSync } from "node:fs";
|
|
@@ -4619,6 +4622,97 @@ var coerce = {
|
|
|
4619
4622
|
};
|
|
4620
4623
|
var NEVER = INVALID;
|
|
4621
4624
|
|
|
4625
|
+
// apps/cli/src/config/providers.ts
|
|
4626
|
+
var PROVIDERS = {
|
|
4627
|
+
deepseek: {
|
|
4628
|
+
name: "DeepSeek",
|
|
4629
|
+
defaultBaseUrl: "https://api.deepseek.com",
|
|
4630
|
+
defaultModel: "deepseek-flash",
|
|
4631
|
+
apiKeyEnv: "DEEPSEEK_API_KEY",
|
|
4632
|
+
contextWindow: 1e6,
|
|
4633
|
+
maxOutputTokens: 8192
|
|
4634
|
+
},
|
|
4635
|
+
qwen: {
|
|
4636
|
+
name: "Qwen (DashScope compatible-mode)",
|
|
4637
|
+
defaultBaseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
4638
|
+
defaultModel: "qwen-plus",
|
|
4639
|
+
apiKeyEnv: "DASHSCOPE_API_KEY",
|
|
4640
|
+
contextWindow: 131072,
|
|
4641
|
+
maxOutputTokens: 8192
|
|
4642
|
+
},
|
|
4643
|
+
openai: {
|
|
4644
|
+
name: "OpenAI",
|
|
4645
|
+
defaultBaseUrl: "https://api.openai.com/v1",
|
|
4646
|
+
defaultModel: "gpt-4o-mini",
|
|
4647
|
+
apiKeyEnv: "OPENAI_API_KEY",
|
|
4648
|
+
contextWindow: 128e3,
|
|
4649
|
+
maxOutputTokens: 16384
|
|
4650
|
+
},
|
|
4651
|
+
custom: {
|
|
4652
|
+
name: "\u81EA\u5B9A\u4E49\u517C\u5BB9\u7AEF\u70B9\uFF08vLLM / Ollama / \u5185\u7F51\u7F51\u5173\uFF09",
|
|
4653
|
+
defaultBaseUrl: "http://127.0.0.1:8000/v1",
|
|
4654
|
+
defaultModel: "local-model",
|
|
4655
|
+
apiKeyEnv: "FORGEX_API_KEY",
|
|
4656
|
+
contextWindow: 32768,
|
|
4657
|
+
maxOutputTokens: 4096
|
|
4658
|
+
}
|
|
4659
|
+
};
|
|
4660
|
+
var PROVIDERS_KEYS = Object.keys(PROVIDERS);
|
|
4661
|
+
|
|
4662
|
+
// apps/cli/src/config/permission-mode.ts
|
|
4663
|
+
var PERMISSION_MODES = ["readonly", "standard", "trusted", "yolo"];
|
|
4664
|
+
var PERMISSION_MODE_LABELS = {
|
|
4665
|
+
readonly: "\u53EA\u8BFB\uFF1A\u53EA\u80FD\u8BFB\uFF0C\u5199\u6587\u4EF6/\u6267\u884C\u547D\u4EE4\u4E00\u5F8B\u88AB\u7B56\u7565\u62D2\u7EDD",
|
|
4666
|
+
standard: "\u6807\u51C6\uFF1A\u8BFB\u653E\u884C\uFF0C\u5199\u6587\u4EF6\u4E0E\u6267\u884C\u547D\u4EE4\u9010\u6B21\u4EBA\u5DE5\u5BA1\u6279",
|
|
4667
|
+
trusted: "\u53D7\u4FE1\uFF1A\u5199\u6587\u4EF6\u81EA\u52A8\u653E\u884C\uFF0C\u6267\u884C\u547D\u4EE4\u4E0E\u9AD8\u5371\u64CD\u4F5C\u4ECD\u9700\u5BA1\u6279",
|
|
4668
|
+
yolo: "\u653E\u5F00\uFF1A\u5173\u95ED\u5168\u90E8\u5BA1\u6279\uFF0C\u5168\u90E8\u76F4\u63A5\u6267\u884C\uFF08\u98CE\u9669\u81EA\u8D1F\uFF09"
|
|
4669
|
+
};
|
|
4670
|
+
var ALIASES = {
|
|
4671
|
+
readonly: "readonly",
|
|
4672
|
+
"read-only": "readonly",
|
|
4673
|
+
ro: "readonly",
|
|
4674
|
+
read: "readonly",
|
|
4675
|
+
standard: "standard",
|
|
4676
|
+
std: "standard",
|
|
4677
|
+
default: "standard",
|
|
4678
|
+
normal: "standard",
|
|
4679
|
+
trusted: "trusted",
|
|
4680
|
+
trust: "trusted",
|
|
4681
|
+
yolo: "yolo",
|
|
4682
|
+
unsafe: "yolo",
|
|
4683
|
+
danger: "yolo"
|
|
4684
|
+
};
|
|
4685
|
+
function isPermissionMode(value) {
|
|
4686
|
+
return typeof value === "string" && PERMISSION_MODES.includes(value);
|
|
4687
|
+
}
|
|
4688
|
+
function parsePermissionMode(raw) {
|
|
4689
|
+
const key = raw.trim().toLowerCase();
|
|
4690
|
+
if (!key) return null;
|
|
4691
|
+
if (isPermissionMode(key)) return key;
|
|
4692
|
+
return ALIASES[key] ?? null;
|
|
4693
|
+
}
|
|
4694
|
+
function nextPermissionMode(current) {
|
|
4695
|
+
const index = PERMISSION_MODES.indexOf(current);
|
|
4696
|
+
if (index < 0) return PERMISSION_MODES[0];
|
|
4697
|
+
return PERMISSION_MODES[(index + 1) % PERMISSION_MODES.length];
|
|
4698
|
+
}
|
|
4699
|
+
function permissionModeList() {
|
|
4700
|
+
return PERMISSION_MODES.join(" / ");
|
|
4701
|
+
}
|
|
4702
|
+
function permissionModeHelp(current, planMode = false) {
|
|
4703
|
+
const lines = [
|
|
4704
|
+
"\u53EF\u7528\u6743\u9650\u6A21\u5F0F\uFF08\u7531\u4E25\u5230\u5BBD\uFF09\uFF1A",
|
|
4705
|
+
...PERMISSION_MODES.map(
|
|
4706
|
+
(mode) => ` ${mode.padEnd(9)} ${PERMISSION_MODE_LABELS[mode]}${mode === current ? " \u2190 \u5F53\u524D" : ""}`
|
|
4707
|
+
)
|
|
4708
|
+
];
|
|
4709
|
+
if (planMode) {
|
|
4710
|
+
lines.push("", "\u5F53\u524D\u5904\u4E8E\u8BA1\u5212\u6A21\u5F0F\uFF08\u6743\u9650\u6309 readonly \u6267\u884C\uFF0C\u5199\u64CD\u4F5C\u4F1A\u88AB\u76F4\u63A5\u62D2\u7EDD\uFF09\uFF1B/mode agent \u9000\u51FA\u8BA1\u5212\u6A21\u5F0F");
|
|
4711
|
+
}
|
|
4712
|
+
lines.push("\u5207\u6362\uFF1A/mode <\u6A21\u5F0F>\uFF0C\u6216\u6309 Shift+Tab \u4F9D\u6B21\u5FAA\u73AF");
|
|
4713
|
+
return lines.join("\n");
|
|
4714
|
+
}
|
|
4715
|
+
|
|
4622
4716
|
// apps/cli/src/config/load-config.ts
|
|
4623
4717
|
var DEFAULT_BASE_URL = "http://127.0.0.1:8000/v1";
|
|
4624
4718
|
var DEFAULT_MODEL = "local-model";
|
|
@@ -4701,7 +4795,15 @@ function loadConfig(options = {}) {
|
|
|
4701
4795
|
}
|
|
4702
4796
|
const envData = readEnvConfig(env);
|
|
4703
4797
|
if (Object.keys(envData).length > 0) sources.push("\u73AF\u5883\u53D8\u91CF");
|
|
4704
|
-
const
|
|
4798
|
+
const detected = detectProviderFromEnv(env, fileData);
|
|
4799
|
+
if (detected) sources.push("\u73AF\u5883\u53D8\u91CF\uFF08\u81EA\u52A8\u8BC6\u522B Provider\uFF09");
|
|
4800
|
+
const merged = deepMerge(deepMerge(fileData, detected ?? {}), envData);
|
|
4801
|
+
const modeOverride = resolveModeOverride(options.permissionMode);
|
|
4802
|
+
if (modeOverride) {
|
|
4803
|
+
const security = merged.security;
|
|
4804
|
+
merged.security = { ...isRecord(security) ? security : {}, mode: modeOverride };
|
|
4805
|
+
sources.push("\u547D\u4EE4\u884C\u53C2\u6570\uFF08--mode\uFF09");
|
|
4806
|
+
}
|
|
4705
4807
|
const parsed = ConfigSchema.safeParse(merged);
|
|
4706
4808
|
if (!parsed.success) {
|
|
4707
4809
|
const issues = parsed.error.issues.map((issue) => `${issue.path.join(".") || "(root)"}: ${issue.message}`).join("; ");
|
|
@@ -4817,6 +4919,31 @@ function readEnvConfig(env) {
|
|
|
4817
4919
|
if (Object.keys(agent).length > 0) out.agent = agent;
|
|
4818
4920
|
return out;
|
|
4819
4921
|
}
|
|
4922
|
+
function detectProviderFromEnv(env, fileData) {
|
|
4923
|
+
if (env.FORGEX_BASE_URL || env.FORGEX_MODEL || env.FORGEX_API_KEY || env.FORGEX_API_KEY_ENV) {
|
|
4924
|
+
return null;
|
|
4925
|
+
}
|
|
4926
|
+
const fileModel = fileData.model;
|
|
4927
|
+
if (isRecord(fileModel)) {
|
|
4928
|
+
if (typeof fileModel.baseUrl === "string" || typeof fileModel.model === "string") return null;
|
|
4929
|
+
if (typeof fileModel.apiKey === "string" || typeof fileModel.apiKeyEnv === "string") return null;
|
|
4930
|
+
}
|
|
4931
|
+
for (const preset of Object.values(PROVIDERS)) {
|
|
4932
|
+
if (preset.apiKeyEnv === "FORGEX_API_KEY") continue;
|
|
4933
|
+
const key = env[preset.apiKeyEnv];
|
|
4934
|
+
if (!key || key.trim() === "") continue;
|
|
4935
|
+
return {
|
|
4936
|
+
model: {
|
|
4937
|
+
baseUrl: preset.defaultBaseUrl,
|
|
4938
|
+
model: preset.defaultModel,
|
|
4939
|
+
apiKeyEnv: preset.apiKeyEnv,
|
|
4940
|
+
contextWindow: preset.contextWindow,
|
|
4941
|
+
maxOutputTokens: preset.maxOutputTokens
|
|
4942
|
+
}
|
|
4943
|
+
};
|
|
4944
|
+
}
|
|
4945
|
+
return null;
|
|
4946
|
+
}
|
|
4820
4947
|
function determineModelConfigured(fileData, env, parsed) {
|
|
4821
4948
|
if (env.FORGEX_BASE_URL || env.FORGEX_MODEL) return true;
|
|
4822
4949
|
const model = fileData.model;
|
|
@@ -4831,6 +4958,14 @@ function numberEnv(raw) {
|
|
|
4831
4958
|
const value = Number(raw);
|
|
4832
4959
|
return Number.isFinite(value) ? value : void 0;
|
|
4833
4960
|
}
|
|
4961
|
+
function resolveModeOverride(raw) {
|
|
4962
|
+
if (raw === void 0) return null;
|
|
4963
|
+
const mode = parsePermissionMode(raw);
|
|
4964
|
+
if (!mode) {
|
|
4965
|
+
throw new ForgeError("CONFIG_INVALID", `--mode \u53D6\u503C\u975E\u6CD5: ${raw}\uFF08\u53EF\u9009\uFF1A${permissionModeList()}\uFF09`);
|
|
4966
|
+
}
|
|
4967
|
+
return mode;
|
|
4968
|
+
}
|
|
4834
4969
|
function deepMerge(base, override) {
|
|
4835
4970
|
const out = { ...base };
|
|
4836
4971
|
for (const [key, value] of Object.entries(override)) {
|
|
@@ -4859,6 +4994,20 @@ function normalizeBaseUrl(baseUrl) {
|
|
|
4859
4994
|
return baseUrl.replace(/\/+$/, "");
|
|
4860
4995
|
}
|
|
4861
4996
|
|
|
4997
|
+
// apps/cli/src/config/setup-hint.ts
|
|
4998
|
+
function modelSetupHint(options) {
|
|
4999
|
+
const lines = [
|
|
5000
|
+
"\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\u3002",
|
|
5001
|
+
options.interactive ? "\u5728\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\u91CC\u76F4\u63A5\u8FD0\u884C `forgex` \u4F1A\u81EA\u52A8\u8FDB\u5165\u8FDE\u63A5\u5411\u5BFC\u3002" : "\u4EFB\u9009\u4E00\u79CD\u65B9\u5F0F\u5B8C\u6210\u914D\u7F6E\uFF1A",
|
|
5002
|
+
" 1. \u4EA4\u4E92\u5F0F\u5411\u5BFC\uFF1Aforgex connect",
|
|
5003
|
+
" 2. \u975E\u4EA4\u4E92\u5F0F\uFF1Aforgex connect --provider deepseek --api-key sk-xxx --model deepseek-flash",
|
|
5004
|
+
" 3. \u53EA\u8BBE\u7F6E Key\uFF1Aexport DEEPSEEK_API_KEY=sk-xxx\uFF08\u542F\u52A8\u65F6\u81EA\u52A8\u8BC6\u522B DeepSeek\uFF09",
|
|
5005
|
+
" 4. \u73AF\u5883\u53D8\u91CF\uFF1AFORGEX_BASE_URL / FORGEX_MODEL / FORGEX_API_KEY",
|
|
5006
|
+
" 5. \u914D\u7F6E\u6587\u4EF6\uFF1A~/.forgex/config.json \u6216\u9879\u76EE\u6839\u76EE\u5F55 forgex.config.json"
|
|
5007
|
+
];
|
|
5008
|
+
return lines.join("\n");
|
|
5009
|
+
}
|
|
5010
|
+
|
|
4862
5011
|
// packages/models/dist/openai-compatible.js
|
|
4863
5012
|
var DEFAULT_TIMEOUT_MS = 12e4;
|
|
4864
5013
|
var MAX_ERROR_BODY = 2e3;
|
|
@@ -6999,7 +7148,7 @@ function execFileText(program, args, options = {}) {
|
|
|
6999
7148
|
maxBuffer: options.maxBuffer ?? 8 * 1024 * 1024,
|
|
7000
7149
|
windowsHide: true,
|
|
7001
7150
|
encoding: "utf8"
|
|
7002
|
-
}, (error, stdout9,
|
|
7151
|
+
}, (error, stdout9, stderr5) => {
|
|
7003
7152
|
if (error) {
|
|
7004
7153
|
const errCode = error.code;
|
|
7005
7154
|
if (errCode === "ENOENT") {
|
|
@@ -7012,11 +7161,11 @@ function execFileText(program, args, options = {}) {
|
|
|
7012
7161
|
resolve5({
|
|
7013
7162
|
code: typeof errCode === "number" ? errCode : -1,
|
|
7014
7163
|
stdout: String(stdout9 ?? ""),
|
|
7015
|
-
stderr: String(
|
|
7164
|
+
stderr: String(stderr5 ?? error.message)
|
|
7016
7165
|
});
|
|
7017
7166
|
return;
|
|
7018
7167
|
}
|
|
7019
|
-
resolve5({ code: 0, stdout: String(stdout9 ?? ""), stderr: String(
|
|
7168
|
+
resolve5({ code: 0, stdout: String(stdout9 ?? ""), stderr: String(stderr5 ?? "") });
|
|
7020
7169
|
});
|
|
7021
7170
|
});
|
|
7022
7171
|
}
|
|
@@ -7704,7 +7853,7 @@ var SshClient = class {
|
|
|
7704
7853
|
return this.abortedResult(command, spec, started, "SSH \u8FDE\u63A5\u88AB\u53D6\u6D88");
|
|
7705
7854
|
}
|
|
7706
7855
|
let stdout9 = "";
|
|
7707
|
-
let
|
|
7856
|
+
let stderr5 = "";
|
|
7708
7857
|
let truncated = false;
|
|
7709
7858
|
let timedOut = false;
|
|
7710
7859
|
let exitCode = null;
|
|
@@ -7750,9 +7899,9 @@ var SshClient = class {
|
|
|
7750
7899
|
}).stderr.on("data", (data) => {
|
|
7751
7900
|
const chunk = data.toString("utf8");
|
|
7752
7901
|
const stderrLimit = Math.floor(maxOutputBytes * 0.5);
|
|
7753
|
-
if (byteLength(
|
|
7754
|
-
|
|
7755
|
-
if (byteLength(
|
|
7902
|
+
if (byteLength(stderr5) < stderrLimit) {
|
|
7903
|
+
stderr5 += chunk;
|
|
7904
|
+
if (byteLength(stderr5) >= stderrLimit)
|
|
7756
7905
|
truncated = true;
|
|
7757
7906
|
} else {
|
|
7758
7907
|
truncated = true;
|
|
@@ -7774,7 +7923,7 @@ var SshClient = class {
|
|
|
7774
7923
|
return {
|
|
7775
7924
|
exitCode,
|
|
7776
7925
|
stdout: truncated ? truncateWithNote(stdout9, maxOutputBytes, 0.9) : stdout9,
|
|
7777
|
-
stderr: truncated ? truncateWithNote(
|
|
7926
|
+
stderr: truncated ? truncateWithNote(stderr5, Math.floor(maxOutputBytes * 0.5), 0.9) : stderr5,
|
|
7778
7927
|
truncated,
|
|
7779
7928
|
timedOut,
|
|
7780
7929
|
durationMs: Date.now() - started,
|
|
@@ -8687,8 +8836,8 @@ function runViaExecutor(binary, spec, signal) {
|
|
|
8687
8836
|
cleanup();
|
|
8688
8837
|
const raw = Buffer.concat(outChunks).toString("utf8").trim();
|
|
8689
8838
|
if (!raw) {
|
|
8690
|
-
const
|
|
8691
|
-
finish(() => reject(new ForgeError("TOOL_FAILED", `\u6267\u884C\u5668\u672A\u8FD4\u56DE\u7ED3\u679C${
|
|
8839
|
+
const stderr5 = Buffer.concat(errChunks).toString("utf8").trim();
|
|
8840
|
+
finish(() => reject(new ForgeError("TOOL_FAILED", `\u6267\u884C\u5668\u672A\u8FD4\u56DE\u7ED3\u679C${stderr5 ? `: ${stderr5}` : ""}`)));
|
|
8692
8841
|
return;
|
|
8693
8842
|
}
|
|
8694
8843
|
let parsed;
|
|
@@ -11013,6 +11162,32 @@ ${PLAN_MODE_PROMPT}` : this.baseSystemPrompt);
|
|
|
11013
11162
|
ts: Date.now()
|
|
11014
11163
|
});
|
|
11015
11164
|
}
|
|
11165
|
+
/**
|
|
11166
|
+
* 运行时直接设置权限模式(TUI 的 Shift+Tab 与 `/mode`、纯文本模式的 `/mode` 都走这里)。
|
|
11167
|
+
*
|
|
11168
|
+
* 关键点:如果当前处于计划模式,先真正退出计划模式再落目标权限。
|
|
11169
|
+
* 否则 `settings.mode` 会被计划模式的 readonly 残留覆盖,用户以为切到了 trusted
|
|
11170
|
+
* 实际仍然只读——这类"看起来切了但没生效"的安全配置差异必须避免。
|
|
11171
|
+
* 与 setPlanMode 一样发 notice,TUI / 纯文本两种渲染都能立刻看到结果。
|
|
11172
|
+
*/
|
|
11173
|
+
setPermissionMode(mode) {
|
|
11174
|
+
const wasPlan = this.planMode;
|
|
11175
|
+
if (wasPlan) {
|
|
11176
|
+
this.planMode = false;
|
|
11177
|
+
this.modeBeforePlan = null;
|
|
11178
|
+
this.context.setSystemPrompt(this.baseSystemPrompt);
|
|
11179
|
+
}
|
|
11180
|
+
const previous = this.settings.mode;
|
|
11181
|
+
this.settings.mode = mode;
|
|
11182
|
+
if (!wasPlan && previous === mode)
|
|
11183
|
+
return;
|
|
11184
|
+
this.events.emit({
|
|
11185
|
+
type: "notice",
|
|
11186
|
+
level: mode === "yolo" ? "warn" : "info",
|
|
11187
|
+
message: wasPlan ? `\u5DF2\u9000\u51FA\u8BA1\u5212\u6A21\u5F0F\uFF0C\u6743\u9650\u6A21\u5F0F\u5207\u6362\u4E3A ${mode}` : `\u6743\u9650\u6A21\u5F0F\u5DF2\u5207\u6362\uFF1A${previous} \u2192 ${mode}`,
|
|
11188
|
+
ts: Date.now()
|
|
11189
|
+
});
|
|
11190
|
+
}
|
|
11016
11191
|
persist(message) {
|
|
11017
11192
|
if (!this.store)
|
|
11018
11193
|
return;
|
|
@@ -11316,41 +11491,6 @@ import { execFileSync } from "node:child_process";
|
|
|
11316
11491
|
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync } from "node:fs";
|
|
11317
11492
|
import { homedir as homedir2 } from "node:os";
|
|
11318
11493
|
import { dirname as dirname4, join as join8, resolve as resolve3 } from "node:path";
|
|
11319
|
-
var PROVIDERS = {
|
|
11320
|
-
deepseek: {
|
|
11321
|
-
name: "DeepSeek",
|
|
11322
|
-
defaultBaseUrl: "https://api.deepseek.com",
|
|
11323
|
-
defaultModel: "deepseek-flash",
|
|
11324
|
-
apiKeyEnv: "DEEPSEEK_API_KEY",
|
|
11325
|
-
contextWindow: 1e6,
|
|
11326
|
-
maxOutputTokens: 8192
|
|
11327
|
-
},
|
|
11328
|
-
qwen: {
|
|
11329
|
-
name: "Qwen (DashScope compatible-mode)",
|
|
11330
|
-
defaultBaseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
11331
|
-
defaultModel: "qwen-plus",
|
|
11332
|
-
apiKeyEnv: "DASHSCOPE_API_KEY",
|
|
11333
|
-
contextWindow: 131072,
|
|
11334
|
-
maxOutputTokens: 8192
|
|
11335
|
-
},
|
|
11336
|
-
openai: {
|
|
11337
|
-
name: "OpenAI",
|
|
11338
|
-
defaultBaseUrl: "https://api.openai.com/v1",
|
|
11339
|
-
defaultModel: "gpt-4o-mini",
|
|
11340
|
-
apiKeyEnv: "OPENAI_API_KEY",
|
|
11341
|
-
contextWindow: 128e3,
|
|
11342
|
-
maxOutputTokens: 16384
|
|
11343
|
-
},
|
|
11344
|
-
custom: {
|
|
11345
|
-
name: "\u81EA\u5B9A\u4E49\u517C\u5BB9\u7AEF\u70B9\uFF08vLLM / Ollama / \u5185\u7F51\u7F51\u5173\uFF09",
|
|
11346
|
-
defaultBaseUrl: "http://127.0.0.1:8000/v1",
|
|
11347
|
-
defaultModel: "local-model",
|
|
11348
|
-
apiKeyEnv: "FORGEX_API_KEY",
|
|
11349
|
-
contextWindow: 32768,
|
|
11350
|
-
maxOutputTokens: 4096
|
|
11351
|
-
}
|
|
11352
|
-
};
|
|
11353
|
-
var PROVIDERS_KEYS = Object.keys(PROVIDERS);
|
|
11354
11494
|
function resolveProvider(input) {
|
|
11355
11495
|
const trimmed = input.trim();
|
|
11356
11496
|
if (!trimmed) return "deepseek";
|
|
@@ -11569,7 +11709,7 @@ function switchModel(runtime, name) {
|
|
|
11569
11709
|
}
|
|
11570
11710
|
|
|
11571
11711
|
// apps/cli/src/version.ts
|
|
11572
|
-
var VERSION = "0.5.
|
|
11712
|
+
var VERSION = "0.5.3";
|
|
11573
11713
|
|
|
11574
11714
|
// apps/cli/src/ui/input-edit.ts
|
|
11575
11715
|
function applyInputKey(state, char, key) {
|
|
@@ -11609,6 +11749,7 @@ var THEME = {
|
|
|
11609
11749
|
var SPINNER = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
11610
11750
|
var COMMANDS = [
|
|
11611
11751
|
{ name: "/models", desc: "\u5207\u6362\u6A21\u578B\uFF08\u4FDD\u7559\u4F1A\u8BDD\u5386\u53F2\uFF09" },
|
|
11752
|
+
{ name: "/mode", desc: "\u5207\u6362\u6743\u9650\u6A21\u5F0F\uFF08readonly/standard/trusted/yolo\uFF09" },
|
|
11612
11753
|
{ name: "/plan", desc: "\u8BA1\u5212\u6A21\u5F0F\uFF1A\u53EA\u8BFB\uFF0C\u53EA\u51FA\u65B9\u6848\u4E0D\u6539\u4EE3\u7801" },
|
|
11613
11754
|
{ name: "/connect", desc: "\u8FDE\u63A5\u6A21\u578B / \u66F4\u65B0 API Key" },
|
|
11614
11755
|
{ name: "/context", desc: "\u67E5\u770B\u4E0A\u4E0B\u6587 token \u7528\u91CF" },
|
|
@@ -11619,6 +11760,7 @@ var COMMANDS = [
|
|
|
11619
11760
|
var HELP = [
|
|
11620
11761
|
"\u547D\u4EE4",
|
|
11621
11762
|
" /models \u5207\u6362\u6A21\u578B\uFF08\u4F1A\u8BDD\u5386\u53F2\u4FDD\u7559\uFF09",
|
|
11763
|
+
" /mode \u67E5\u770B\u6216\u5207\u6362\u6743\u9650\u6A21\u5F0F\uFF1A/mode standard\u3001/mode plan\u3001/mode agent",
|
|
11622
11764
|
" /plan \u8FDB\u5165/\u9000\u51FA\u8BA1\u5212\u6A21\u5F0F\uFF08\u53EA\u8BFB\uFF0C\u53EA\u51FA\u8BA1\u5212\u4E0D\u6539\u4EE3\u7801\uFF09",
|
|
11623
11765
|
" /connect \u8FDE\u63A5\u6A21\u578B\uFF1A\u6821\u9A8C Key\u3001\u6301\u4E45\u5316\u5E76\u7ACB\u5373\u751F\u6548",
|
|
11624
11766
|
" /context \u663E\u793A\u4E0A\u4E0B\u6587 token \u4F30\u7B97",
|
|
@@ -11627,6 +11769,7 @@ var HELP = [
|
|
|
11627
11769
|
" /exit \u9000\u51FA",
|
|
11628
11770
|
"",
|
|
11629
11771
|
"\u5FEB\u6377\u952E",
|
|
11772
|
+
" Shift+Tab \u5FAA\u73AF\u6743\u9650\u6A21\u5F0F\uFF08\u8BA1\u5212\u6A21\u5F0F\u4E0B\u5219\u9000\u51FA\u8BA1\u5212\u6A21\u5F0F\uFF09",
|
|
11630
11773
|
" \u2191/\u2193 \u5728\u547D\u4EE4\u9762\u677F\u4E2D\u9009\u62E9",
|
|
11631
11774
|
" Tab \u8865\u5168\u547D\u4EE4",
|
|
11632
11775
|
" \u2190/\u2192 \u79FB\u52A8\u5149\u6807",
|
|
@@ -11850,6 +11993,15 @@ function App({ runtime, bridge }) {
|
|
|
11850
11993
|
},
|
|
11851
11994
|
[finishConnect, push, switchModel2, wizard]
|
|
11852
11995
|
);
|
|
11996
|
+
const cyclePermissionMode = useCallback(() => {
|
|
11997
|
+
if (agent.isPlanMode) {
|
|
11998
|
+
agent.setPlanMode(false);
|
|
11999
|
+
} else {
|
|
12000
|
+
agent.setPermissionMode(nextPermissionMode(agent.permissionMode));
|
|
12001
|
+
}
|
|
12002
|
+
setPlanMode(agent.isPlanMode);
|
|
12003
|
+
setMode(agent.permissionMode);
|
|
12004
|
+
}, [agent]);
|
|
11853
12005
|
const handleSlash = useCallback(
|
|
11854
12006
|
(text) => {
|
|
11855
12007
|
const [name, ...rest] = text.split(/\s+/);
|
|
@@ -11882,6 +12034,37 @@ function App({ runtime, bridge }) {
|
|
|
11882
12034
|
setMode(agent.permissionMode);
|
|
11883
12035
|
return;
|
|
11884
12036
|
}
|
|
12037
|
+
case "/mode": {
|
|
12038
|
+
if (busy) {
|
|
12039
|
+
push("notice", "\u5F53\u524D\u4EFB\u52A1\u8FD0\u884C\u4E2D\uFF0C\u7ED3\u675F\u6216 Ctrl+C \u53D6\u6D88\u540E\u518D\u5207\u6362\u6743\u9650\u6A21\u5F0F");
|
|
12040
|
+
return;
|
|
12041
|
+
}
|
|
12042
|
+
if (!arg) {
|
|
12043
|
+
push("notice", permissionModeHelp(agent.permissionMode, agent.isPlanMode));
|
|
12044
|
+
return;
|
|
12045
|
+
}
|
|
12046
|
+
const target = arg.toLowerCase();
|
|
12047
|
+
if (target === "plan" || target === "agent") {
|
|
12048
|
+
const wantPlan = target === "plan";
|
|
12049
|
+
if (wantPlan === agent.isPlanMode) {
|
|
12050
|
+
push("notice", wantPlan ? "\u5DF2\u5904\u4E8E\u8BA1\u5212\u6A21\u5F0F" : `\u5F53\u524D\u5DF2\u662F\u6267\u884C\u6A21\u5F0F\uFF08${agent.permissionMode}\uFF09`);
|
|
12051
|
+
return;
|
|
12052
|
+
}
|
|
12053
|
+
agent.setPlanMode(wantPlan);
|
|
12054
|
+
setPlanMode(agent.isPlanMode);
|
|
12055
|
+
setMode(agent.permissionMode);
|
|
12056
|
+
return;
|
|
12057
|
+
}
|
|
12058
|
+
const parsedMode = parsePermissionMode(arg);
|
|
12059
|
+
if (!parsedMode) {
|
|
12060
|
+
push("error", `\u672A\u77E5\u6743\u9650\u6A21\u5F0F: ${arg}\uFF08\u53EF\u9009\uFF1A${permissionModeList()}\uFF0C\u6216 plan / agent\uFF09`);
|
|
12061
|
+
return;
|
|
12062
|
+
}
|
|
12063
|
+
agent.setPermissionMode(parsedMode);
|
|
12064
|
+
setPlanMode(agent.isPlanMode);
|
|
12065
|
+
setMode(agent.permissionMode);
|
|
12066
|
+
return;
|
|
12067
|
+
}
|
|
11885
12068
|
case "/models":
|
|
11886
12069
|
case "/model": {
|
|
11887
12070
|
if (busy) {
|
|
@@ -12012,6 +12195,14 @@ function App({ runtime, bridge }) {
|
|
|
12012
12195
|
editInput(char, key);
|
|
12013
12196
|
return;
|
|
12014
12197
|
}
|
|
12198
|
+
if (key.tab && key.shift) {
|
|
12199
|
+
if (busy || connecting) {
|
|
12200
|
+
push("notice", "\u5F53\u524D\u4EFB\u52A1\u8FD0\u884C\u4E2D\uFF0C\u7ED3\u675F\u6216 Ctrl+C \u53D6\u6D88\u540E\u518D\u5207\u6362\u6743\u9650\u6A21\u5F0F");
|
|
12201
|
+
return;
|
|
12202
|
+
}
|
|
12203
|
+
cyclePermissionMode();
|
|
12204
|
+
return;
|
|
12205
|
+
}
|
|
12015
12206
|
if (busy || connecting) return;
|
|
12016
12207
|
if (palette.length > 0) {
|
|
12017
12208
|
if (key.upArrow) {
|
|
@@ -12097,6 +12288,7 @@ function App({ runtime, bridge }) {
|
|
|
12097
12288
|
/* @__PURE__ */ jsx(Text, { color: THEME.accent, bold: true, children: modelName }),
|
|
12098
12289
|
/* @__PURE__ */ jsx(Text, { dimColor: true, children: " \xB7 " }),
|
|
12099
12290
|
/* @__PURE__ */ jsx(Text, { color: modeColor, children: modeLabel }),
|
|
12291
|
+
busy || connecting ? null : /* @__PURE__ */ jsx(Text, { dimColor: true, children: " \u21E7\u21E5" }),
|
|
12100
12292
|
/* @__PURE__ */ jsxs(Text, { dimColor: true, children: [
|
|
12101
12293
|
" \xB7 ctx ",
|
|
12102
12294
|
formatTokens(contextTokens)
|
|
@@ -12565,6 +12757,32 @@ async function runPlainChat(runtime, bridge) {
|
|
|
12565
12757
|
);
|
|
12566
12758
|
continue;
|
|
12567
12759
|
}
|
|
12760
|
+
if (text === "/mode" || text.startsWith("/mode ")) {
|
|
12761
|
+
const target = text.slice("/mode".length).trim().toLowerCase();
|
|
12762
|
+
if (!target) {
|
|
12763
|
+
stdout.write(`${permissionModeHelp(agent.permissionMode, agent.isPlanMode)}
|
|
12764
|
+
`);
|
|
12765
|
+
continue;
|
|
12766
|
+
}
|
|
12767
|
+
if (target === "plan" || target === "agent") {
|
|
12768
|
+
const wantPlan = target === "plan";
|
|
12769
|
+
if (wantPlan === agent.isPlanMode) {
|
|
12770
|
+
stdout.write(`[\u63D0\u793A] ${wantPlan ? "\u5DF2\u5904\u4E8E\u8BA1\u5212\u6A21\u5F0F" : `\u5F53\u524D\u5DF2\u662F\u6267\u884C\u6A21\u5F0F\uFF08${agent.permissionMode}\uFF09`}
|
|
12771
|
+
`);
|
|
12772
|
+
continue;
|
|
12773
|
+
}
|
|
12774
|
+
agent.setPlanMode(wantPlan);
|
|
12775
|
+
continue;
|
|
12776
|
+
}
|
|
12777
|
+
const mode = parsePermissionMode(target);
|
|
12778
|
+
if (!mode) {
|
|
12779
|
+
stdout.write(`[\u9519\u8BEF] \u672A\u77E5\u6743\u9650\u6A21\u5F0F: ${target}\uFF08\u53EF\u9009\uFF1A${permissionModeList()}\uFF0C\u6216 plan / agent\uFF09
|
|
12780
|
+
`);
|
|
12781
|
+
continue;
|
|
12782
|
+
}
|
|
12783
|
+
agent.setPermissionMode(mode);
|
|
12784
|
+
continue;
|
|
12785
|
+
}
|
|
12568
12786
|
if (text === "/models" || text.startsWith("/models ")) {
|
|
12569
12787
|
const target = text.slice("/models".length).trim();
|
|
12570
12788
|
if (!target) {
|
|
@@ -12665,6 +12883,7 @@ function printHelp() {
|
|
|
12665
12883
|
[
|
|
12666
12884
|
"\u53EF\u7528\u547D\u4EE4\uFF1A",
|
|
12667
12885
|
" /models [\u6A21\u578B\u540D] \u67E5\u770B\u53EF\u7528\u6A21\u578B / \u5207\u6362\u6A21\u578B",
|
|
12886
|
+
" /mode [\u6A21\u5F0F] \u67E5\u770B\u6216\u5207\u6362\u6743\u9650\u6A21\u5F0F\uFF08/mode plan\u3001/mode agent \u63A7\u5236\u8BA1\u5212\u6A21\u5F0F\uFF09",
|
|
12668
12887
|
" /plan \u8FDB\u5165\u6216\u9000\u51FA\u8BA1\u5212\u6A21\u5F0F\uFF08\u53EA\u8BFB\uFF0C\u53EA\u51FA\u8BA1\u5212\uFF09",
|
|
12669
12888
|
" /connect \u63D0\u793A\u9000\u51FA\u540E\u8FD0\u884C forgex connect",
|
|
12670
12889
|
" /session \u663E\u793A\u5F53\u524D\u4F1A\u8BDD id",
|
|
@@ -12675,16 +12894,157 @@ function printHelp() {
|
|
|
12675
12894
|
);
|
|
12676
12895
|
}
|
|
12677
12896
|
|
|
12897
|
+
// apps/cli/src/commands/connect.ts
|
|
12898
|
+
import { createInterface as createInterface3 } from "node:readline/promises";
|
|
12899
|
+
import { resolve as resolve4 } from "node:path";
|
|
12900
|
+
import { stderr, stdin as stdin2, stdout as stdout2 } from "node:process";
|
|
12901
|
+
async function connectCommand(args, _options) {
|
|
12902
|
+
const isInteractive = Boolean(stdin2.isTTY);
|
|
12903
|
+
const configPath = defaultConfigPath();
|
|
12904
|
+
const existing = readExistingConfig(configPath);
|
|
12905
|
+
if (!isInteractive) {
|
|
12906
|
+
const provider = flagString(args.flags, "provider") ?? "custom";
|
|
12907
|
+
const apiKey = flagString(args.flags, "api-key") ?? "";
|
|
12908
|
+
const preset = PROVIDERS[provider.toLowerCase()];
|
|
12909
|
+
const baseUrl = flagString(args.flags, "base-url") ?? preset?.defaultBaseUrl ?? "";
|
|
12910
|
+
const model = flagString(args.flags, "model") ?? preset?.defaultModel ?? "";
|
|
12911
|
+
if (!apiKey) {
|
|
12912
|
+
stderr.write("[connect] --api-key \u5728\u975E\u4EA4\u4E92\u6A21\u5F0F\u4E0B\u5FC5\u586B\n");
|
|
12913
|
+
return 2;
|
|
12914
|
+
}
|
|
12915
|
+
return await doConnect({ provider, apiKey, baseUrl, model, configPath, existing });
|
|
12916
|
+
}
|
|
12917
|
+
stdout2.write("ForgeX Connect \u2014 \u8FDE\u63A5\u4F60\u7684\u6A21\u578B\u63D0\u4F9B\u65B9\n\n");
|
|
12918
|
+
const rl = createInterface3({ input: stdin2, output: stderr });
|
|
12919
|
+
try {
|
|
12920
|
+
stdout2.write("\u9009\u62E9\u6A21\u578B\u63D0\u4F9B\u65B9\uFF08\u8F93\u5165\u7F16\u53F7\u6216\u540D\u79F0\uFF09\uFF1A\n");
|
|
12921
|
+
PROVIDERS_KEYS.forEach((key, i) => {
|
|
12922
|
+
const p = PROVIDERS[key];
|
|
12923
|
+
stdout2.write(` ${i + 1}. ${p.name} (${p.defaultBaseUrl})
|
|
12924
|
+
`);
|
|
12925
|
+
});
|
|
12926
|
+
stdout2.write("\n> ");
|
|
12927
|
+
const providerInput = (await rl.question("")).trim();
|
|
12928
|
+
const provider = resolveProvider(providerInput);
|
|
12929
|
+
const preset = PROVIDERS[provider];
|
|
12930
|
+
const baseUrlDefault = preset?.defaultBaseUrl ?? existing.model.baseUrl;
|
|
12931
|
+
stdout2.write(`baseUrl [${baseUrlDefault}]: `);
|
|
12932
|
+
const baseUrl = (await rl.question("")).trim() || baseUrlDefault;
|
|
12933
|
+
stdout2.write("\nAPI Key\uFF08\u8F93\u5165\u4E0D\u4F1A\u88AB\u9690\u85CF\uFF0C\u7C98\u8D34\u540E\u6309\u56DE\u8F66\uFF09: ");
|
|
12934
|
+
const apiKey = (await rl.question("")).trim();
|
|
12935
|
+
if (!apiKey) {
|
|
12936
|
+
stderr.write("[connect] API Key \u4E0D\u80FD\u4E3A\u7A7A\n");
|
|
12937
|
+
return 2;
|
|
12938
|
+
}
|
|
12939
|
+
const modelDefault = preset?.defaultModel ?? existing.model.model;
|
|
12940
|
+
stdout2.write(`
|
|
12941
|
+
\u6A21\u578B\u540D [${modelDefault}]: `);
|
|
12942
|
+
const model = (await rl.question("")).trim() || modelDefault;
|
|
12943
|
+
const saveDefault = configPath;
|
|
12944
|
+
stdout2.write(`
|
|
12945
|
+
\u4FDD\u5B58\u914D\u7F6E\u5230 [${saveDefault}]\uFF1A`);
|
|
12946
|
+
const saveInput = (await rl.question("")).trim();
|
|
12947
|
+
const finalConfigPath = saveInput ? resolve4(saveInput) : saveDefault;
|
|
12948
|
+
stdout2.write("\n--------------------------------------------\n");
|
|
12949
|
+
stdout2.write(`Provider : ${provider}
|
|
12950
|
+
`);
|
|
12951
|
+
stdout2.write(`Base URL : ${baseUrl}
|
|
12952
|
+
`);
|
|
12953
|
+
stdout2.write(`Model : ${model}
|
|
12954
|
+
`);
|
|
12955
|
+
stdout2.write(`API Key : ${maskKey(apiKey)}
|
|
12956
|
+
`);
|
|
12957
|
+
stdout2.write(`Config : ${finalConfigPath}
|
|
12958
|
+
`);
|
|
12959
|
+
stdout2.write("--------------------------------------------\n");
|
|
12960
|
+
stdout2.write("\u8FDE\u901A\u6027\u6D4B\u8BD5\u4E2D\uFF085s \u8D85\u65F6\uFF09...\n");
|
|
12961
|
+
return await doConnect({ provider, apiKey, baseUrl, model, configPath: finalConfigPath, existing });
|
|
12962
|
+
} finally {
|
|
12963
|
+
rl.close();
|
|
12964
|
+
}
|
|
12965
|
+
}
|
|
12966
|
+
async function doConnect(params) {
|
|
12967
|
+
const { provider, apiKey, baseUrl, model, configPath, existing } = params;
|
|
12968
|
+
const testResult = await testConnectivity({ baseUrl, apiKey, model });
|
|
12969
|
+
if (!testResult.ok) {
|
|
12970
|
+
stderr.write(`
|
|
12971
|
+
[FAIL] \u8FDE\u901A\u6027\u6D4B\u8BD5\u5931\u8D25\uFF1A${testResult.reason}
|
|
12972
|
+
`);
|
|
12973
|
+
if (testResult.detail) stderr.write(` ${testResult.detail}
|
|
12974
|
+
`);
|
|
12975
|
+
stderr.write("\n\u63D0\u793A\uFF1A\n");
|
|
12976
|
+
stderr.write(" \xB7 \u786E\u8BA4 API Key \u6709\u6548\uFF1A\u53BB provider \u63A7\u5236\u53F0\u65B0\u5EFA\u4E00\u4E2A\n");
|
|
12977
|
+
stderr.write(" \xB7 \u786E\u8BA4 baseUrl \u6B63\u786E\uFF08DeepSeek: https://api.deepseek.com\uFF09\n");
|
|
12978
|
+
stderr.write(" \xB7 \u786E\u8BA4\u6A21\u578B\u540D\uFF08DeepSeek: deepseek-flash / deepseek-v4-pro\uFF09\n");
|
|
12979
|
+
return 1;
|
|
12980
|
+
}
|
|
12981
|
+
const preset = PROVIDERS[provider];
|
|
12982
|
+
const envName = preset?.apiKeyEnv ?? provider.toUpperCase().replace(/[^A-Z0-9_]/g, "_") + "_API_KEY";
|
|
12983
|
+
const envPersisted = persistApiKey(envName, apiKey);
|
|
12984
|
+
process.env[envName] = apiKey;
|
|
12985
|
+
saveConnectionConfig(configPath, existing, {
|
|
12986
|
+
baseUrl,
|
|
12987
|
+
model,
|
|
12988
|
+
envName,
|
|
12989
|
+
...preset ? { contextWindow: preset.contextWindow, maxOutputTokens: preset.maxOutputTokens } : {}
|
|
12990
|
+
});
|
|
12991
|
+
stdout2.write(`
|
|
12992
|
+
[OK] \u8FDE\u901A\u6027\u6D4B\u8BD5\u901A\u8FC7 \u2705
|
|
12993
|
+
`);
|
|
12994
|
+
stdout2.write(` \u54CD\u5E94\u8017\u65F6: ${testResult.latencyMs}ms
|
|
12995
|
+
`);
|
|
12996
|
+
stdout2.write(`
|
|
12997
|
+
\u914D\u7F6E\u5DF2\u5199\u5165\uFF1A${configPath}
|
|
12998
|
+
`);
|
|
12999
|
+
stdout2.write(`API Key \u6301\u4E45\u5316\uFF1A`);
|
|
13000
|
+
if (envPersisted.ok) {
|
|
13001
|
+
stdout2.write(`\u2705 ${envName}\uFF08\u7528\u6237\u7EA7\u73AF\u5883\u53D8\u91CF\uFF09
|
|
13002
|
+
`);
|
|
13003
|
+
stdout2.write(` \u5F53\u524D\u4F1A\u8BDD\u5DF2\u751F\u6548 \u2705
|
|
13004
|
+
`);
|
|
13005
|
+
} else {
|
|
13006
|
+
stdout2.write(`\u26A0\uFE0F \u9700\u624B\u52A8\u8BBE\u7F6E\uFF1A
|
|
13007
|
+
`);
|
|
13008
|
+
stdout2.write(` ${envPersisted.hint}
|
|
13009
|
+
`);
|
|
13010
|
+
}
|
|
13011
|
+
stdout2.write(`
|
|
13012
|
+
\u73B0\u5728\u53EF\u4EE5\u8DD1\uFF1A
|
|
13013
|
+
`);
|
|
13014
|
+
stdout2.write(` forgex run "\u4F60\u597D\uFF0C\u4ECB\u7ECD\u4E00\u4E0B ForgeX"
|
|
13015
|
+
`);
|
|
13016
|
+
stdout2.write(` forgex doctor
|
|
13017
|
+
`);
|
|
13018
|
+
return 0;
|
|
13019
|
+
}
|
|
13020
|
+
|
|
12678
13021
|
// apps/cli/src/commands/chat.ts
|
|
12679
13022
|
async function chatCommand(args, options) {
|
|
12680
|
-
const
|
|
13023
|
+
const configFlag = flagString(args.flags, "config");
|
|
13024
|
+
const modeFlag = flagString(args.flags, "mode");
|
|
13025
|
+
const readConfig = () => loadConfig({
|
|
12681
13026
|
cwd: options.cwd,
|
|
12682
|
-
...
|
|
13027
|
+
...configFlag ? { configPath: configFlag } : {},
|
|
13028
|
+
...modeFlag ? { permissionMode: modeFlag } : {}
|
|
12683
13029
|
});
|
|
13030
|
+
let loaded = readConfig();
|
|
12684
13031
|
if (!loaded.modelConfigured) {
|
|
12685
|
-
|
|
12686
|
-
|
|
12687
|
-
|
|
13032
|
+
const interactive = Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
13033
|
+
if (!interactive) {
|
|
13034
|
+
throw new ForgeError("CONFIG_INVALID", modelSetupHint({ interactive: false }));
|
|
13035
|
+
}
|
|
13036
|
+
stderr2.write("\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\uFF0C\u5148\u8FDB\u5165\u8FDE\u63A5\u5411\u5BFC\uFF08Ctrl+C \u53EF\u53D6\u6D88\uFF09\n\n");
|
|
13037
|
+
const code = await connectCommand(args, { cwd: options.cwd });
|
|
13038
|
+
if (code !== 0) {
|
|
13039
|
+
throw new ForgeError("CONFIG_INVALID", "\u6A21\u578B\u8FDE\u63A5\u672A\u5B8C\u6210\uFF0C\u5DF2\u9000\u51FA\u3002\u7A0D\u540E\u53EF\u91CD\u65B0\u6267\u884C\uFF1Aforgex connect");
|
|
13040
|
+
}
|
|
13041
|
+
loaded = readConfig();
|
|
13042
|
+
if (!loaded.modelConfigured) {
|
|
13043
|
+
throw new ForgeError(
|
|
13044
|
+
"CONFIG_INVALID",
|
|
13045
|
+
`\u8FDE\u63A5\u5411\u5BFC\u6CA1\u6709\u751F\u6210\u53EF\u7528\u7684\u6A21\u578B\u914D\u7F6E\uFF08${loaded.configPath ?? "\u672A\u627E\u5230\u914D\u7F6E\u6587\u4EF6"}\uFF09\uFF0C\u8BF7\u68C0\u67E5\u4FDD\u5B58\u8DEF\u5F84\u540E\u91CD\u8BD5`
|
|
13046
|
+
);
|
|
13047
|
+
}
|
|
12688
13048
|
}
|
|
12689
13049
|
const bridge = new ApprovalBridge();
|
|
12690
13050
|
const runtime = createRuntime({
|
|
@@ -12693,6 +13053,7 @@ async function chatCommand(args, options) {
|
|
|
12693
13053
|
approvalHandler: bridge.handler,
|
|
12694
13054
|
...flagString(args.flags, "session") ? { sessionId: flagString(args.flags, "session") } : {}
|
|
12695
13055
|
});
|
|
13056
|
+
if (flagBool(args.flags, "plan")) runtime.agent.setPlanMode(true);
|
|
12696
13057
|
const plain = flagBool(args.flags, "plain") || !process.stdout.isTTY || !process.stdin.isTTY;
|
|
12697
13058
|
if (plain) {
|
|
12698
13059
|
await runPlainChat(runtime, bridge);
|
|
@@ -12713,18 +13074,16 @@ async function chatCommand(args, options) {
|
|
|
12713
13074
|
}
|
|
12714
13075
|
|
|
12715
13076
|
// apps/cli/src/commands/run.ts
|
|
12716
|
-
import { createInterface as
|
|
12717
|
-
import { stdin as
|
|
13077
|
+
import { createInterface as createInterface4 } from "node:readline/promises";
|
|
13078
|
+
import { stdin as stdin3, stdout as stdout3, stderr as stderr3 } from "node:process";
|
|
12718
13079
|
async function runCommand(args, options) {
|
|
12719
13080
|
const loaded = loadConfig({
|
|
12720
13081
|
cwd: options.cwd,
|
|
12721
|
-
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {}
|
|
13082
|
+
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {},
|
|
13083
|
+
...flagString(args.flags, "mode") ? { permissionMode: flagString(args.flags, "mode") } : {}
|
|
12722
13084
|
});
|
|
12723
13085
|
if (!loaded.modelConfigured) {
|
|
12724
|
-
throw new ForgeError(
|
|
12725
|
-
"CONFIG_INVALID",
|
|
12726
|
-
"\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\u3002\u8BF7\u521B\u5EFA forgex.config.json \u6216\u8BBE\u7F6E FORGEX_BASE_URL / FORGEX_MODEL\u3002"
|
|
12727
|
-
);
|
|
13086
|
+
throw new ForgeError("CONFIG_INVALID", modelSetupHint({ interactive: false }));
|
|
12728
13087
|
}
|
|
12729
13088
|
const prompt = (args.positionals.join(" ").trim() || await readStdin()).trim();
|
|
12730
13089
|
if (!prompt) {
|
|
@@ -12732,8 +13091,8 @@ async function runCommand(args, options) {
|
|
|
12732
13091
|
}
|
|
12733
13092
|
const json = flagBool(args.flags, "json");
|
|
12734
13093
|
const autoApprove = flagBool(args.flags, "yes");
|
|
12735
|
-
const interactive = Boolean(
|
|
12736
|
-
const rl = interactive ?
|
|
13094
|
+
const interactive = Boolean(stdin3.isTTY) && !json;
|
|
13095
|
+
const rl = interactive ? createInterface4({ input: stdin3, output: stderr3, terminal: true }) : null;
|
|
12737
13096
|
let approvalHandler;
|
|
12738
13097
|
if (autoApprove) {
|
|
12739
13098
|
approvalHandler = () => true;
|
|
@@ -12753,28 +13112,29 @@ async function runCommand(args, options) {
|
|
|
12753
13112
|
...approvalHandler ? { approvalHandler } : {},
|
|
12754
13113
|
...flagString(args.flags, "session") ? { sessionId: flagString(args.flags, "session") } : {}
|
|
12755
13114
|
});
|
|
13115
|
+
if (flagBool(args.flags, "plan")) runtime.agent.setPlanMode(true);
|
|
12756
13116
|
let sawDelta = false;
|
|
12757
13117
|
const off = runtime.agent.events.on((event) => {
|
|
12758
13118
|
if (json) return;
|
|
12759
13119
|
switch (event.type) {
|
|
12760
13120
|
case "assistant:delta":
|
|
12761
13121
|
sawDelta = true;
|
|
12762
|
-
|
|
13122
|
+
stdout3.write(event.text);
|
|
12763
13123
|
break;
|
|
12764
13124
|
case "tool:start":
|
|
12765
|
-
|
|
13125
|
+
stderr3.write(`[\u5DE5\u5177] \u8C03\u7528 ${event.name}
|
|
12766
13126
|
`);
|
|
12767
13127
|
break;
|
|
12768
13128
|
case "tool:end":
|
|
12769
|
-
|
|
13129
|
+
stderr3.write(`[\u5DE5\u5177] ${event.name} ${event.result.ok ? "\u6210\u529F" : "\u5931\u8D25"}\uFF1A${event.result.summary}
|
|
12770
13130
|
`);
|
|
12771
13131
|
break;
|
|
12772
13132
|
case "notice":
|
|
12773
|
-
|
|
13133
|
+
stderr3.write(`[\u63D0\u793A] ${event.message}
|
|
12774
13134
|
`);
|
|
12775
13135
|
break;
|
|
12776
13136
|
case "approval:resolved":
|
|
12777
|
-
|
|
13137
|
+
stderr3.write(`[\u5BA1\u6279] ${event.decision}
|
|
12778
13138
|
`);
|
|
12779
13139
|
break;
|
|
12780
13140
|
default:
|
|
@@ -12783,10 +13143,10 @@ async function runCommand(args, options) {
|
|
|
12783
13143
|
});
|
|
12784
13144
|
try {
|
|
12785
13145
|
const result = await runtime.agent.run(prompt, {});
|
|
12786
|
-
if (!json && !sawDelta && result.text)
|
|
13146
|
+
if (!json && !sawDelta && result.text) stdout3.write(`${result.text}
|
|
12787
13147
|
`);
|
|
12788
13148
|
if (json) {
|
|
12789
|
-
|
|
13149
|
+
stdout3.write(
|
|
12790
13150
|
`${JSON.stringify(
|
|
12791
13151
|
{
|
|
12792
13152
|
sessionId: result.sessionId,
|
|
@@ -12801,7 +13161,7 @@ async function runCommand(args, options) {
|
|
|
12801
13161
|
`
|
|
12802
13162
|
);
|
|
12803
13163
|
} else {
|
|
12804
|
-
|
|
13164
|
+
stderr3.write(
|
|
12805
13165
|
`[\u5B8C\u6210] \u7ED3\u675F\u539F\u56E0=${result.reason} \u7528\u65F6=${result.metrics.totalMs}ms \u6A21\u578B=${result.metrics.modelCalls} \u6B21 \u5DE5\u5177=${result.metrics.toolCalls} \u6B21 tokens=${result.metrics.totalTokens}
|
|
12806
13166
|
`
|
|
12807
13167
|
);
|
|
@@ -12814,9 +13174,9 @@ async function runCommand(args, options) {
|
|
|
12814
13174
|
}
|
|
12815
13175
|
}
|
|
12816
13176
|
async function readStdin() {
|
|
12817
|
-
if (
|
|
13177
|
+
if (stdin3.isTTY) return "";
|
|
12818
13178
|
const chunks = [];
|
|
12819
|
-
for await (const chunk of
|
|
13179
|
+
for await (const chunk of stdin3) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
|
|
12820
13180
|
return Buffer.concat(chunks).toString("utf8");
|
|
12821
13181
|
}
|
|
12822
13182
|
|
|
@@ -12825,7 +13185,7 @@ import { execFile as execFile3 } from "node:child_process";
|
|
|
12825
13185
|
import { accessSync, constants, existsSync as existsSync4 } from "node:fs";
|
|
12826
13186
|
import { DatabaseSync as DatabaseSync4 } from "node:sqlite";
|
|
12827
13187
|
import { dirname as dirname5 } from "node:path";
|
|
12828
|
-
import { stdout as
|
|
13188
|
+
import { stdout as stdout4 } from "node:process";
|
|
12829
13189
|
async function doctorCommand(args, options) {
|
|
12830
13190
|
const checks = [];
|
|
12831
13191
|
const full = flagBool(args.flags, "full");
|
|
@@ -12844,7 +13204,8 @@ async function doctorCommand(args, options) {
|
|
|
12844
13204
|
try {
|
|
12845
13205
|
loaded = loadConfig({
|
|
12846
13206
|
cwd: options.cwd,
|
|
12847
|
-
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {}
|
|
13207
|
+
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {},
|
|
13208
|
+
...flagString(args.flags, "mode") ? { permissionMode: flagString(args.flags, "mode") } : {}
|
|
12848
13209
|
});
|
|
12849
13210
|
checks.push({
|
|
12850
13211
|
name: "\u914D\u7F6E\u6587\u4EF6",
|
|
@@ -12856,6 +13217,11 @@ async function doctorCommand(args, options) {
|
|
|
12856
13217
|
status: loaded.modelConfigured ? "ok" : "warn",
|
|
12857
13218
|
detail: loaded.modelConfigured ? `${loaded.config.model.model} @ ${loaded.config.model.baseUrl}\uFF08API Key: ${loaded.config.model.apiKey ? "\u5DF2\u914D\u7F6E" : "\u672A\u914D\u7F6E"}\uFF09` : "\u672A\u914D\u7F6E baseUrl/model\uFF0Cchat/run \u4E0D\u53EF\u7528"
|
|
12858
13219
|
});
|
|
13220
|
+
checks.push({
|
|
13221
|
+
name: "\u6743\u9650\u6A21\u5F0F",
|
|
13222
|
+
status: loaded.config.security.mode === "yolo" ? "warn" : "ok",
|
|
13223
|
+
detail: `${loaded.config.security.mode} \u2014 ${PERMISSION_MODE_LABELS[loaded.config.security.mode]}`
|
|
13224
|
+
});
|
|
12859
13225
|
} catch (err) {
|
|
12860
13226
|
checks.push({ name: "\u914D\u7F6E\u6587\u4EF6", status: "fail", detail: err instanceof Error ? err.message : String(err) });
|
|
12861
13227
|
}
|
|
@@ -12946,14 +13312,14 @@ async function doctorCommand(args, options) {
|
|
|
12946
13312
|
detail: `mode=${loaded.config.security.mode} \u5199\u5BA1\u6279=${loaded.config.security.requireApprovalForWrite} \u6267\u884C\u5BA1\u6279=${loaded.config.security.requireApprovalForExecute}`
|
|
12947
13313
|
});
|
|
12948
13314
|
}
|
|
12949
|
-
|
|
13315
|
+
stdout4.write("ForgeX \u73AF\u5883\u81EA\u68C0\n");
|
|
12950
13316
|
for (const check of checks) {
|
|
12951
|
-
|
|
13317
|
+
stdout4.write(`[${label(check.status)}] ${check.name}: ${check.detail}
|
|
12952
13318
|
`);
|
|
12953
13319
|
}
|
|
12954
13320
|
const failed = checks.filter((check) => check.status === "fail").length;
|
|
12955
13321
|
const warned = checks.filter((check) => check.status === "warn").length;
|
|
12956
|
-
|
|
13322
|
+
stdout4.write(`
|
|
12957
13323
|
\u7ED3\u679C\uFF1A${checks.length} \u9879\u68C0\u67E5\uFF0C${failed} \u9879\u5931\u8D25\uFF0C${warned} \u9879\u8B66\u544A
|
|
12958
13324
|
`);
|
|
12959
13325
|
return failed > 0 ? 1 : 0;
|
|
@@ -12986,21 +13352,21 @@ function formatBytes2(bytes) {
|
|
|
12986
13352
|
}
|
|
12987
13353
|
|
|
12988
13354
|
// apps/cli/src/commands/tools.ts
|
|
12989
|
-
import { stdout as
|
|
13355
|
+
import { stdout as stdout5 } from "node:process";
|
|
12990
13356
|
function toolsCommand() {
|
|
12991
13357
|
const registry = new ToolRegistry(createBuiltinTools());
|
|
12992
13358
|
const tools = registry.list();
|
|
12993
|
-
|
|
13359
|
+
stdout5.write(`ForgeX \u5185\u7F6E\u5DE5\u5177\uFF08${tools.length} \u4E2A\uFF09
|
|
12994
13360
|
|
|
12995
13361
|
`);
|
|
12996
13362
|
for (const tool of tools) {
|
|
12997
13363
|
const tags = tool.tags && tool.tags.length > 0 ? ` \u6807\u7B7E=${tool.tags.join(",")}` : "";
|
|
12998
|
-
|
|
13364
|
+
stdout5.write(`- ${tool.name} [${tool.riskLevel}] \u8D85\u65F6=${tool.timeoutMs}ms \u8F93\u51FA\u4E0A\u9650=${tool.maxOutputBytes}B${tags}
|
|
12999
13365
|
`);
|
|
13000
|
-
|
|
13366
|
+
stdout5.write(` ${tool.description.split("\n")[0]}
|
|
13001
13367
|
`);
|
|
13002
13368
|
}
|
|
13003
|
-
|
|
13369
|
+
stdout5.write(
|
|
13004
13370
|
"\n\u8BF4\u660E\uFF1A\u98CE\u9669\u7B49\u7EA7\u51B3\u5B9A\u9ED8\u8BA4\u6743\u9650\u7B56\u7565\uFF1Bexecute \u7EA7\u547D\u4EE4\u9ED8\u8BA4\u9700\u8981\u4EBA\u5DE5\u5BA1\u6279\uFF0Cprivileged \u7EA7\u9ED8\u8BA4\u62D2\u7EDD\u3002\n"
|
|
13005
13371
|
);
|
|
13006
13372
|
return 0;
|
|
@@ -13008,7 +13374,7 @@ function toolsCommand() {
|
|
|
13008
13374
|
|
|
13009
13375
|
// apps/cli/src/commands/sessions.ts
|
|
13010
13376
|
import { existsSync as existsSync5 } from "node:fs";
|
|
13011
|
-
import { stdout as
|
|
13377
|
+
import { stdout as stdout6 } from "node:process";
|
|
13012
13378
|
function sessionsCommand(args, options) {
|
|
13013
13379
|
const loaded = loadConfig({
|
|
13014
13380
|
cwd: options.cwd,
|
|
@@ -13016,7 +13382,7 @@ function sessionsCommand(args, options) {
|
|
|
13016
13382
|
});
|
|
13017
13383
|
const dbPath = loaded.config.session.dbPath;
|
|
13018
13384
|
if (!existsSync5(dbPath)) {
|
|
13019
|
-
|
|
13385
|
+
stdout6.write(`\u4F1A\u8BDD\u6570\u636E\u5E93\u5C1A\u672A\u521B\u5EFA: ${dbPath}
|
|
13020
13386
|
`);
|
|
13021
13387
|
return 0;
|
|
13022
13388
|
}
|
|
@@ -13025,18 +13391,18 @@ function sessionsCommand(args, options) {
|
|
|
13025
13391
|
try {
|
|
13026
13392
|
const sessions = store.listSessions(limit);
|
|
13027
13393
|
if (sessions.length === 0) {
|
|
13028
|
-
|
|
13394
|
+
stdout6.write("\u6682\u65E0\u4F1A\u8BDD\u8BB0\u5F55\n");
|
|
13029
13395
|
return 0;
|
|
13030
13396
|
}
|
|
13031
|
-
|
|
13397
|
+
stdout6.write(`\u6700\u8FD1 ${sessions.length} \u4E2A\u4F1A\u8BDD\uFF08\u6570\u636E\u5E93: ${dbPath}\uFF09
|
|
13032
13398
|
|
|
13033
13399
|
`);
|
|
13034
13400
|
for (const session of sessions) {
|
|
13035
13401
|
const title = session.title ?? "(\u672A\u547D\u540D)";
|
|
13036
|
-
|
|
13402
|
+
stdout6.write(`${session.id} ${session.updatedAt} ${session.model} ${title}
|
|
13037
13403
|
`);
|
|
13038
13404
|
}
|
|
13039
|
-
|
|
13405
|
+
stdout6.write('\n\u4F7F\u7528 forgex chat --session <id> \u6062\u590D\u4F1A\u8BDD\uFF1Bforgex run --session <id> "..." \u7EE7\u7EED\u4EFB\u52A1\u3002\n');
|
|
13040
13406
|
return 0;
|
|
13041
13407
|
} finally {
|
|
13042
13408
|
store.close();
|
|
@@ -13044,14 +13410,15 @@ function sessionsCommand(args, options) {
|
|
|
13044
13410
|
}
|
|
13045
13411
|
|
|
13046
13412
|
// apps/cli/src/commands/config.ts
|
|
13047
|
-
import { stdout as
|
|
13413
|
+
import { stdout as stdout7 } from "node:process";
|
|
13048
13414
|
function configCommand(args, options) {
|
|
13049
13415
|
const loaded = loadConfig({
|
|
13050
13416
|
cwd: options.cwd,
|
|
13051
|
-
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {}
|
|
13417
|
+
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {},
|
|
13418
|
+
...flagString(args.flags, "mode") ? { permissionMode: flagString(args.flags, "mode") } : {}
|
|
13052
13419
|
});
|
|
13053
13420
|
const { apiKey, ...safeModel } = loaded.config.model;
|
|
13054
|
-
|
|
13421
|
+
stdout7.write(
|
|
13055
13422
|
`${JSON.stringify(
|
|
13056
13423
|
{
|
|
13057
13424
|
configPath: loaded.configPath,
|
|
@@ -13068,132 +13435,8 @@ function configCommand(args, options) {
|
|
|
13068
13435
|
`
|
|
13069
13436
|
);
|
|
13070
13437
|
if (!loaded.modelConfigured) {
|
|
13071
|
-
|
|
13072
|
-
}
|
|
13073
|
-
return 0;
|
|
13074
|
-
}
|
|
13075
|
-
|
|
13076
|
-
// apps/cli/src/commands/connect.ts
|
|
13077
|
-
import { createInterface as createInterface4 } from "node:readline/promises";
|
|
13078
|
-
import { resolve as resolve4 } from "node:path";
|
|
13079
|
-
import { stderr as stderr2, stdin as stdin3, stdout as stdout7 } from "node:process";
|
|
13080
|
-
async function connectCommand(args, _options) {
|
|
13081
|
-
const isInteractive = Boolean(stdin3.isTTY);
|
|
13082
|
-
const configPath = defaultConfigPath();
|
|
13083
|
-
const existing = readExistingConfig(configPath);
|
|
13084
|
-
if (!isInteractive) {
|
|
13085
|
-
const provider = flagString(args.flags, "provider") ?? "custom";
|
|
13086
|
-
const apiKey = flagString(args.flags, "api-key") ?? "";
|
|
13087
|
-
const preset = PROVIDERS[provider.toLowerCase()];
|
|
13088
|
-
const baseUrl = flagString(args.flags, "base-url") ?? preset?.defaultBaseUrl ?? "";
|
|
13089
|
-
const model = flagString(args.flags, "model") ?? preset?.defaultModel ?? "";
|
|
13090
|
-
if (!apiKey) {
|
|
13091
|
-
stderr2.write("[connect] --api-key \u5728\u975E\u4EA4\u4E92\u6A21\u5F0F\u4E0B\u5FC5\u586B\n");
|
|
13092
|
-
return 2;
|
|
13093
|
-
}
|
|
13094
|
-
return await doConnect({ provider, apiKey, baseUrl, model, configPath, existing });
|
|
13438
|
+
stdout7.write("\n[warn] \u5C1A\u672A\u914D\u7F6E\u6A21\u578B\uFF1A\u8BF7\u8BBE\u7F6E forgex.config.json \u6216 FORGEX_BASE_URL / FORGEX_MODEL\u3002\n");
|
|
13095
13439
|
}
|
|
13096
|
-
stdout7.write("ForgeX Connect \u2014 \u8FDE\u63A5\u4F60\u7684\u6A21\u578B\u63D0\u4F9B\u65B9\n\n");
|
|
13097
|
-
const rl = createInterface4({ input: stdin3, output: stderr2 });
|
|
13098
|
-
try {
|
|
13099
|
-
stdout7.write("\u9009\u62E9\u6A21\u578B\u63D0\u4F9B\u65B9\uFF08\u8F93\u5165\u7F16\u53F7\u6216\u540D\u79F0\uFF09\uFF1A\n");
|
|
13100
|
-
PROVIDERS_KEYS.forEach((key, i) => {
|
|
13101
|
-
const p = PROVIDERS[key];
|
|
13102
|
-
stdout7.write(` ${i + 1}. ${p.name} (${p.defaultBaseUrl})
|
|
13103
|
-
`);
|
|
13104
|
-
});
|
|
13105
|
-
stdout7.write("\n> ");
|
|
13106
|
-
const providerInput = (await rl.question("")).trim();
|
|
13107
|
-
const provider = resolveProvider(providerInput);
|
|
13108
|
-
const preset = PROVIDERS[provider];
|
|
13109
|
-
const baseUrlDefault = preset?.defaultBaseUrl ?? existing.model.baseUrl;
|
|
13110
|
-
stdout7.write(`baseUrl [${baseUrlDefault}]: `);
|
|
13111
|
-
const baseUrl = (await rl.question("")).trim() || baseUrlDefault;
|
|
13112
|
-
stdout7.write("\nAPI Key\uFF08\u8F93\u5165\u4E0D\u4F1A\u88AB\u9690\u85CF\uFF0C\u7C98\u8D34\u540E\u6309\u56DE\u8F66\uFF09: ");
|
|
13113
|
-
const apiKey = (await rl.question("")).trim();
|
|
13114
|
-
if (!apiKey) {
|
|
13115
|
-
stderr2.write("[connect] API Key \u4E0D\u80FD\u4E3A\u7A7A\n");
|
|
13116
|
-
return 2;
|
|
13117
|
-
}
|
|
13118
|
-
const modelDefault = preset?.defaultModel ?? existing.model.model;
|
|
13119
|
-
stdout7.write(`
|
|
13120
|
-
\u6A21\u578B\u540D [${modelDefault}]: `);
|
|
13121
|
-
const model = (await rl.question("")).trim() || modelDefault;
|
|
13122
|
-
const saveDefault = configPath;
|
|
13123
|
-
stdout7.write(`
|
|
13124
|
-
\u4FDD\u5B58\u914D\u7F6E\u5230 [${saveDefault}]\uFF1A`);
|
|
13125
|
-
const saveInput = (await rl.question("")).trim();
|
|
13126
|
-
const finalConfigPath = saveInput ? resolve4(saveInput) : saveDefault;
|
|
13127
|
-
stdout7.write("\n--------------------------------------------\n");
|
|
13128
|
-
stdout7.write(`Provider : ${provider}
|
|
13129
|
-
`);
|
|
13130
|
-
stdout7.write(`Base URL : ${baseUrl}
|
|
13131
|
-
`);
|
|
13132
|
-
stdout7.write(`Model : ${model}
|
|
13133
|
-
`);
|
|
13134
|
-
stdout7.write(`API Key : ${maskKey(apiKey)}
|
|
13135
|
-
`);
|
|
13136
|
-
stdout7.write(`Config : ${finalConfigPath}
|
|
13137
|
-
`);
|
|
13138
|
-
stdout7.write("--------------------------------------------\n");
|
|
13139
|
-
stdout7.write("\u8FDE\u901A\u6027\u6D4B\u8BD5\u4E2D\uFF085s \u8D85\u65F6\uFF09...\n");
|
|
13140
|
-
return await doConnect({ provider, apiKey, baseUrl, model, configPath: finalConfigPath, existing });
|
|
13141
|
-
} finally {
|
|
13142
|
-
rl.close();
|
|
13143
|
-
}
|
|
13144
|
-
}
|
|
13145
|
-
async function doConnect(params) {
|
|
13146
|
-
const { provider, apiKey, baseUrl, model, configPath, existing } = params;
|
|
13147
|
-
const testResult = await testConnectivity({ baseUrl, apiKey, model });
|
|
13148
|
-
if (!testResult.ok) {
|
|
13149
|
-
stderr2.write(`
|
|
13150
|
-
[FAIL] \u8FDE\u901A\u6027\u6D4B\u8BD5\u5931\u8D25\uFF1A${testResult.reason}
|
|
13151
|
-
`);
|
|
13152
|
-
if (testResult.detail) stderr2.write(` ${testResult.detail}
|
|
13153
|
-
`);
|
|
13154
|
-
stderr2.write("\n\u63D0\u793A\uFF1A\n");
|
|
13155
|
-
stderr2.write(" \xB7 \u786E\u8BA4 API Key \u6709\u6548\uFF1A\u53BB provider \u63A7\u5236\u53F0\u65B0\u5EFA\u4E00\u4E2A\n");
|
|
13156
|
-
stderr2.write(" \xB7 \u786E\u8BA4 baseUrl \u6B63\u786E\uFF08DeepSeek: https://api.deepseek.com\uFF09\n");
|
|
13157
|
-
stderr2.write(" \xB7 \u786E\u8BA4\u6A21\u578B\u540D\uFF08DeepSeek: deepseek-flash / deepseek-v4-pro\uFF09\n");
|
|
13158
|
-
return 1;
|
|
13159
|
-
}
|
|
13160
|
-
const preset = PROVIDERS[provider];
|
|
13161
|
-
const envName = preset?.apiKeyEnv ?? provider.toUpperCase().replace(/[^A-Z0-9_]/g, "_") + "_API_KEY";
|
|
13162
|
-
const envPersisted = persistApiKey(envName, apiKey);
|
|
13163
|
-
process.env[envName] = apiKey;
|
|
13164
|
-
saveConnectionConfig(configPath, existing, {
|
|
13165
|
-
baseUrl,
|
|
13166
|
-
model,
|
|
13167
|
-
envName,
|
|
13168
|
-
...preset ? { contextWindow: preset.contextWindow, maxOutputTokens: preset.maxOutputTokens } : {}
|
|
13169
|
-
});
|
|
13170
|
-
stdout7.write(`
|
|
13171
|
-
[OK] \u8FDE\u901A\u6027\u6D4B\u8BD5\u901A\u8FC7 \u2705
|
|
13172
|
-
`);
|
|
13173
|
-
stdout7.write(` \u54CD\u5E94\u8017\u65F6: ${testResult.latencyMs}ms
|
|
13174
|
-
`);
|
|
13175
|
-
stdout7.write(`
|
|
13176
|
-
\u914D\u7F6E\u5DF2\u5199\u5165\uFF1A${configPath}
|
|
13177
|
-
`);
|
|
13178
|
-
stdout7.write(`API Key \u6301\u4E45\u5316\uFF1A`);
|
|
13179
|
-
if (envPersisted.ok) {
|
|
13180
|
-
stdout7.write(`\u2705 ${envName}\uFF08\u7528\u6237\u7EA7\u73AF\u5883\u53D8\u91CF\uFF09
|
|
13181
|
-
`);
|
|
13182
|
-
stdout7.write(` \u5F53\u524D\u4F1A\u8BDD\u5DF2\u751F\u6548 \u2705
|
|
13183
|
-
`);
|
|
13184
|
-
} else {
|
|
13185
|
-
stdout7.write(`\u26A0\uFE0F \u9700\u624B\u52A8\u8BBE\u7F6E\uFF1A
|
|
13186
|
-
`);
|
|
13187
|
-
stdout7.write(` ${envPersisted.hint}
|
|
13188
|
-
`);
|
|
13189
|
-
}
|
|
13190
|
-
stdout7.write(`
|
|
13191
|
-
\u73B0\u5728\u53EF\u4EE5\u8DD1\uFF1A
|
|
13192
|
-
`);
|
|
13193
|
-
stdout7.write(` forgex run "\u4F60\u597D\uFF0C\u4ECB\u7ECD\u4E00\u4E0B ForgeX"
|
|
13194
|
-
`);
|
|
13195
|
-
stdout7.write(` forgex doctor
|
|
13196
|
-
`);
|
|
13197
13440
|
return 0;
|
|
13198
13441
|
}
|
|
13199
13442
|
|
|
@@ -13203,7 +13446,7 @@ async function main(argv) {
|
|
|
13203
13446
|
const unknown = unknownFlags(args.flags);
|
|
13204
13447
|
if (unknown.length > 0) {
|
|
13205
13448
|
const rendered = unknown.map((name) => name.length === 1 ? `-${name}` : `--${name}`).join(", ");
|
|
13206
|
-
|
|
13449
|
+
stderr4.write(`\u672A\u77E5\u53C2\u6570: ${rendered}
|
|
13207
13450
|
|
|
13208
13451
|
`);
|
|
13209
13452
|
printHelp2();
|
|
@@ -13212,7 +13455,7 @@ async function main(argv) {
|
|
|
13212
13455
|
const missingValue = flagsMissingValue(args.flags);
|
|
13213
13456
|
if (missingValue.length > 0) {
|
|
13214
13457
|
const rendered = missingValue.map((name) => `--${name}`).join(", ");
|
|
13215
|
-
|
|
13458
|
+
stderr4.write(`\u53C2\u6570\u7F3A\u5C11\u53D6\u503C: ${rendered}
|
|
13216
13459
|
|
|
13217
13460
|
`);
|
|
13218
13461
|
printHelp2();
|
|
@@ -13248,7 +13491,7 @@ async function main(argv) {
|
|
|
13248
13491
|
case "connect":
|
|
13249
13492
|
return await connectCommand(args, { cwd });
|
|
13250
13493
|
default:
|
|
13251
|
-
|
|
13494
|
+
stderr4.write(`\u672A\u77E5\u547D\u4EE4: ${args.command}
|
|
13252
13495
|
|
|
13253
13496
|
`);
|
|
13254
13497
|
printHelp2();
|
|
@@ -13264,24 +13507,27 @@ function printHelp2() {
|
|
|
13264
13507
|
"",
|
|
13265
13508
|
"\u547D\u4EE4\uFF1A",
|
|
13266
13509
|
" connect \u4EA4\u4E92\u5F0F\u8FDE\u63A5\u6A21\u578B\uFF08\u5199\u5165 ~/.forgex/config.json + \u6301\u4E45\u5316 API Key\uFF09",
|
|
13267
|
-
" chat [--plain] [--session <id>] [--config <path>]",
|
|
13510
|
+
" chat [--plain] [--session <id>] [--config <path>] [--mode <\u6A21\u5F0F>] [--plan]",
|
|
13268
13511
|
" \u8FDB\u5165\u4EA4\u4E92\u5F0F\u4F1A\u8BDD\uFF08TTY \u4E0B\u4F7F\u7528 Ink TUI\uFF0C\u7BA1\u9053/--plain \u4F7F\u7528\u7EAF\u6587\u672C\uFF09",
|
|
13269
|
-
' run "<\u4EFB\u52A1>" [--json] [--yes] [--session <id>] [--config <path>]',
|
|
13512
|
+
' run "<\u4EFB\u52A1>" [--json] [--yes] [--session <id>] [--config <path>] [--mode <\u6A21\u5F0F>] [--plan]',
|
|
13270
13513
|
" \u6267\u884C\u5355\u6B21\u4EFB\u52A1\uFF1B\u652F\u6301\u4ECE\u6807\u51C6\u8F93\u5165\u8BFB\u53D6\u4EFB\u52A1\u5185\u5BB9",
|
|
13271
|
-
" doctor [--full] [--config <path>]",
|
|
13514
|
+
" doctor [--full] [--config <path>] [--mode <\u6A21\u5F0F>]",
|
|
13272
13515
|
" \u68C0\u67E5 Node\u3001\u5DE5\u4F5C\u533A\u3001\u6570\u636E\u5E93\u3001rg/git\u3001\u7CFB\u7EDF\u8BCA\u65AD\u80FD\u529B",
|
|
13273
13516
|
" tools",
|
|
13274
13517
|
" \u5217\u51FA\u5185\u7F6E\u5DE5\u5177\u53CA\u98CE\u9669\u7B49\u7EA7",
|
|
13275
13518
|
" sessions [--limit <n>]",
|
|
13276
13519
|
" \u67E5\u770B\u5386\u53F2\u4F1A\u8BDD",
|
|
13277
|
-
" config [--config <path>]",
|
|
13520
|
+
" config [--config <path>] [--mode <\u6A21\u5F0F>]",
|
|
13278
13521
|
" \u6253\u5370\u6700\u7EC8\u751F\u6548\u914D\u7F6E\uFF08API Key \u8131\u654F\uFF09",
|
|
13279
13522
|
"",
|
|
13280
13523
|
"\u901A\u7528\u9009\u9879\uFF1A",
|
|
13524
|
+
" --mode <\u6A21\u5F0F> \u6743\u9650\u6A21\u5F0F\uFF1Areadonly | standard | trusted | yolo\uFF0C\u4F18\u5148\u7EA7\u9AD8\u4E8E\u914D\u7F6E\u6587\u4EF6\u4E0E\u73AF\u5883\u53D8\u91CF",
|
|
13525
|
+
" \u4F1A\u8BDD\u5185\u4E5F\u53EF\u7528 /mode \u6216 Shift+Tab \u5207\u6362",
|
|
13526
|
+
" --plan \u8BA1\u5212\u6A21\u5F0F\uFF1A\u53EA\u8BFB\u5206\u6790\u5E76\u8F93\u51FA\u8BA1\u5212\uFF0C\u4E0D\u4FEE\u6539\u4EFB\u4F55\u6587\u4EF6\uFF08\u9002\u5408 CI dry-run\uFF09",
|
|
13281
13527
|
" -h, --help \u663E\u793A\u5E2E\u52A9",
|
|
13282
13528
|
" -v, --version \u663E\u793A\u7248\u672C",
|
|
13283
13529
|
"",
|
|
13284
|
-
"\u914D\u7F6E\u4F18\u5148\u7EA7\uFF1A\u9ED8\u8BA4\u503C < \u914D\u7F6E\u6587\u4EF6\uFF08forgex.config.json\uFF09< \u73AF\u5883\u53D8\u91CF",
|
|
13530
|
+
"\u914D\u7F6E\u4F18\u5148\u7EA7\uFF1A\u9ED8\u8BA4\u503C < \u914D\u7F6E\u6587\u4EF6\uFF08forgex.config.json\uFF09< \u73AF\u5883\u53D8\u91CF < \u547D\u4EE4\u884C\u53C2\u6570",
|
|
13285
13531
|
"\u5E38\u7528\u73AF\u5883\u53D8\u91CF\uFF1AFORGEX_BASE_URL / FORGEX_API_KEY / FORGEX_MODEL / FORGEX_PERMISSION_MODE / FORGEX_WORKSPACE",
|
|
13286
13532
|
""
|
|
13287
13533
|
].join("\n")
|
|
@@ -13295,7 +13541,7 @@ function formatError(err) {
|
|
|
13295
13541
|
void main(process.argv.slice(2)).then((code) => {
|
|
13296
13542
|
process.exitCode = code;
|
|
13297
13543
|
}).catch((err) => {
|
|
13298
|
-
|
|
13544
|
+
stderr4.write(`[\u9519\u8BEF] ${formatError(err)}
|
|
13299
13545
|
`);
|
|
13300
13546
|
process.exitCode = 1;
|
|
13301
13547
|
});
|