forgex-agent 0.5.0 → 0.5.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 +16 -3
- package/main.js +303 -262
- 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
|
|
|
@@ -43,6 +54,8 @@ forgex
|
|
|
43
54
|
|
|
44
55
|
任务运行中按 `Ctrl+C` 取消任务;空闲时按 `Ctrl+C` 退出。审批面板中按 `Ctrl+C` 视为拒绝。
|
|
45
56
|
|
|
57
|
+
输入框编辑:`←`/`→` 移动光标,`Backspace`/`Delete` 删除光标前一个字符,`Ctrl+A`/`Ctrl+E` 跳到行首/行尾,`Ctrl+U` 清空整行。
|
|
58
|
+
|
|
46
59
|
## CLI 子命令
|
|
47
60
|
|
|
48
61
|
```bash
|
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 {
|
|
@@ -572,6 +572,7 @@ function flagNumber(flags, name) {
|
|
|
572
572
|
// apps/cli/src/commands/chat.ts
|
|
573
573
|
import React2 from "react";
|
|
574
574
|
import { render } from "ink";
|
|
575
|
+
import { stderr as stderr2 } from "node:process";
|
|
575
576
|
|
|
576
577
|
// apps/cli/src/config/load-config.ts
|
|
577
578
|
import { existsSync, readFileSync } from "node:fs";
|
|
@@ -4619,6 +4620,43 @@ var coerce = {
|
|
|
4619
4620
|
};
|
|
4620
4621
|
var NEVER = INVALID;
|
|
4621
4622
|
|
|
4623
|
+
// apps/cli/src/config/providers.ts
|
|
4624
|
+
var PROVIDERS = {
|
|
4625
|
+
deepseek: {
|
|
4626
|
+
name: "DeepSeek",
|
|
4627
|
+
defaultBaseUrl: "https://api.deepseek.com",
|
|
4628
|
+
defaultModel: "deepseek-flash",
|
|
4629
|
+
apiKeyEnv: "DEEPSEEK_API_KEY",
|
|
4630
|
+
contextWindow: 1e6,
|
|
4631
|
+
maxOutputTokens: 8192
|
|
4632
|
+
},
|
|
4633
|
+
qwen: {
|
|
4634
|
+
name: "Qwen (DashScope compatible-mode)",
|
|
4635
|
+
defaultBaseUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
4636
|
+
defaultModel: "qwen-plus",
|
|
4637
|
+
apiKeyEnv: "DASHSCOPE_API_KEY",
|
|
4638
|
+
contextWindow: 131072,
|
|
4639
|
+
maxOutputTokens: 8192
|
|
4640
|
+
},
|
|
4641
|
+
openai: {
|
|
4642
|
+
name: "OpenAI",
|
|
4643
|
+
defaultBaseUrl: "https://api.openai.com/v1",
|
|
4644
|
+
defaultModel: "gpt-4o-mini",
|
|
4645
|
+
apiKeyEnv: "OPENAI_API_KEY",
|
|
4646
|
+
contextWindow: 128e3,
|
|
4647
|
+
maxOutputTokens: 16384
|
|
4648
|
+
},
|
|
4649
|
+
custom: {
|
|
4650
|
+
name: "\u81EA\u5B9A\u4E49\u517C\u5BB9\u7AEF\u70B9\uFF08vLLM / Ollama / \u5185\u7F51\u7F51\u5173\uFF09",
|
|
4651
|
+
defaultBaseUrl: "http://127.0.0.1:8000/v1",
|
|
4652
|
+
defaultModel: "local-model",
|
|
4653
|
+
apiKeyEnv: "FORGEX_API_KEY",
|
|
4654
|
+
contextWindow: 32768,
|
|
4655
|
+
maxOutputTokens: 4096
|
|
4656
|
+
}
|
|
4657
|
+
};
|
|
4658
|
+
var PROVIDERS_KEYS = Object.keys(PROVIDERS);
|
|
4659
|
+
|
|
4622
4660
|
// apps/cli/src/config/load-config.ts
|
|
4623
4661
|
var DEFAULT_BASE_URL = "http://127.0.0.1:8000/v1";
|
|
4624
4662
|
var DEFAULT_MODEL = "local-model";
|
|
@@ -4701,7 +4739,9 @@ function loadConfig(options = {}) {
|
|
|
4701
4739
|
}
|
|
4702
4740
|
const envData = readEnvConfig(env);
|
|
4703
4741
|
if (Object.keys(envData).length > 0) sources.push("\u73AF\u5883\u53D8\u91CF");
|
|
4704
|
-
const
|
|
4742
|
+
const detected = detectProviderFromEnv(env, fileData);
|
|
4743
|
+
if (detected) sources.push("\u73AF\u5883\u53D8\u91CF\uFF08\u81EA\u52A8\u8BC6\u522B Provider\uFF09");
|
|
4744
|
+
const merged = deepMerge(deepMerge(fileData, detected ?? {}), envData);
|
|
4705
4745
|
const parsed = ConfigSchema.safeParse(merged);
|
|
4706
4746
|
if (!parsed.success) {
|
|
4707
4747
|
const issues = parsed.error.issues.map((issue) => `${issue.path.join(".") || "(root)"}: ${issue.message}`).join("; ");
|
|
@@ -4817,6 +4857,31 @@ function readEnvConfig(env) {
|
|
|
4817
4857
|
if (Object.keys(agent).length > 0) out.agent = agent;
|
|
4818
4858
|
return out;
|
|
4819
4859
|
}
|
|
4860
|
+
function detectProviderFromEnv(env, fileData) {
|
|
4861
|
+
if (env.FORGEX_BASE_URL || env.FORGEX_MODEL || env.FORGEX_API_KEY || env.FORGEX_API_KEY_ENV) {
|
|
4862
|
+
return null;
|
|
4863
|
+
}
|
|
4864
|
+
const fileModel = fileData.model;
|
|
4865
|
+
if (isRecord(fileModel)) {
|
|
4866
|
+
if (typeof fileModel.baseUrl === "string" || typeof fileModel.model === "string") return null;
|
|
4867
|
+
if (typeof fileModel.apiKey === "string" || typeof fileModel.apiKeyEnv === "string") return null;
|
|
4868
|
+
}
|
|
4869
|
+
for (const preset of Object.values(PROVIDERS)) {
|
|
4870
|
+
if (preset.apiKeyEnv === "FORGEX_API_KEY") continue;
|
|
4871
|
+
const key = env[preset.apiKeyEnv];
|
|
4872
|
+
if (!key || key.trim() === "") continue;
|
|
4873
|
+
return {
|
|
4874
|
+
model: {
|
|
4875
|
+
baseUrl: preset.defaultBaseUrl,
|
|
4876
|
+
model: preset.defaultModel,
|
|
4877
|
+
apiKeyEnv: preset.apiKeyEnv,
|
|
4878
|
+
contextWindow: preset.contextWindow,
|
|
4879
|
+
maxOutputTokens: preset.maxOutputTokens
|
|
4880
|
+
}
|
|
4881
|
+
};
|
|
4882
|
+
}
|
|
4883
|
+
return null;
|
|
4884
|
+
}
|
|
4820
4885
|
function determineModelConfigured(fileData, env, parsed) {
|
|
4821
4886
|
if (env.FORGEX_BASE_URL || env.FORGEX_MODEL) return true;
|
|
4822
4887
|
const model = fileData.model;
|
|
@@ -4859,6 +4924,20 @@ function normalizeBaseUrl(baseUrl) {
|
|
|
4859
4924
|
return baseUrl.replace(/\/+$/, "");
|
|
4860
4925
|
}
|
|
4861
4926
|
|
|
4927
|
+
// apps/cli/src/config/setup-hint.ts
|
|
4928
|
+
function modelSetupHint(options) {
|
|
4929
|
+
const lines = [
|
|
4930
|
+
"\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\u3002",
|
|
4931
|
+
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",
|
|
4932
|
+
" 1. \u4EA4\u4E92\u5F0F\u5411\u5BFC\uFF1Aforgex connect",
|
|
4933
|
+
" 2. \u975E\u4EA4\u4E92\u5F0F\uFF1Aforgex connect --provider deepseek --api-key sk-xxx --model deepseek-flash",
|
|
4934
|
+
" 3. \u53EA\u8BBE\u7F6E Key\uFF1Aexport DEEPSEEK_API_KEY=sk-xxx\uFF08\u542F\u52A8\u65F6\u81EA\u52A8\u8BC6\u522B DeepSeek\uFF09",
|
|
4935
|
+
" 4. \u73AF\u5883\u53D8\u91CF\uFF1AFORGEX_BASE_URL / FORGEX_MODEL / FORGEX_API_KEY",
|
|
4936
|
+
" 5. \u914D\u7F6E\u6587\u4EF6\uFF1A~/.forgex/config.json \u6216\u9879\u76EE\u6839\u76EE\u5F55 forgex.config.json"
|
|
4937
|
+
];
|
|
4938
|
+
return lines.join("\n");
|
|
4939
|
+
}
|
|
4940
|
+
|
|
4862
4941
|
// packages/models/dist/openai-compatible.js
|
|
4863
4942
|
var DEFAULT_TIMEOUT_MS = 12e4;
|
|
4864
4943
|
var MAX_ERROR_BODY = 2e3;
|
|
@@ -6999,7 +7078,7 @@ function execFileText(program, args, options = {}) {
|
|
|
6999
7078
|
maxBuffer: options.maxBuffer ?? 8 * 1024 * 1024,
|
|
7000
7079
|
windowsHide: true,
|
|
7001
7080
|
encoding: "utf8"
|
|
7002
|
-
}, (error, stdout9,
|
|
7081
|
+
}, (error, stdout9, stderr5) => {
|
|
7003
7082
|
if (error) {
|
|
7004
7083
|
const errCode = error.code;
|
|
7005
7084
|
if (errCode === "ENOENT") {
|
|
@@ -7012,11 +7091,11 @@ function execFileText(program, args, options = {}) {
|
|
|
7012
7091
|
resolve5({
|
|
7013
7092
|
code: typeof errCode === "number" ? errCode : -1,
|
|
7014
7093
|
stdout: String(stdout9 ?? ""),
|
|
7015
|
-
stderr: String(
|
|
7094
|
+
stderr: String(stderr5 ?? error.message)
|
|
7016
7095
|
});
|
|
7017
7096
|
return;
|
|
7018
7097
|
}
|
|
7019
|
-
resolve5({ code: 0, stdout: String(stdout9 ?? ""), stderr: String(
|
|
7098
|
+
resolve5({ code: 0, stdout: String(stdout9 ?? ""), stderr: String(stderr5 ?? "") });
|
|
7020
7099
|
});
|
|
7021
7100
|
});
|
|
7022
7101
|
}
|
|
@@ -7704,7 +7783,7 @@ var SshClient = class {
|
|
|
7704
7783
|
return this.abortedResult(command, spec, started, "SSH \u8FDE\u63A5\u88AB\u53D6\u6D88");
|
|
7705
7784
|
}
|
|
7706
7785
|
let stdout9 = "";
|
|
7707
|
-
let
|
|
7786
|
+
let stderr5 = "";
|
|
7708
7787
|
let truncated = false;
|
|
7709
7788
|
let timedOut = false;
|
|
7710
7789
|
let exitCode = null;
|
|
@@ -7750,9 +7829,9 @@ var SshClient = class {
|
|
|
7750
7829
|
}).stderr.on("data", (data) => {
|
|
7751
7830
|
const chunk = data.toString("utf8");
|
|
7752
7831
|
const stderrLimit = Math.floor(maxOutputBytes * 0.5);
|
|
7753
|
-
if (byteLength(
|
|
7754
|
-
|
|
7755
|
-
if (byteLength(
|
|
7832
|
+
if (byteLength(stderr5) < stderrLimit) {
|
|
7833
|
+
stderr5 += chunk;
|
|
7834
|
+
if (byteLength(stderr5) >= stderrLimit)
|
|
7756
7835
|
truncated = true;
|
|
7757
7836
|
} else {
|
|
7758
7837
|
truncated = true;
|
|
@@ -7774,7 +7853,7 @@ var SshClient = class {
|
|
|
7774
7853
|
return {
|
|
7775
7854
|
exitCode,
|
|
7776
7855
|
stdout: truncated ? truncateWithNote(stdout9, maxOutputBytes, 0.9) : stdout9,
|
|
7777
|
-
stderr: truncated ? truncateWithNote(
|
|
7856
|
+
stderr: truncated ? truncateWithNote(stderr5, Math.floor(maxOutputBytes * 0.5), 0.9) : stderr5,
|
|
7778
7857
|
truncated,
|
|
7779
7858
|
timedOut,
|
|
7780
7859
|
durationMs: Date.now() - started,
|
|
@@ -8687,8 +8766,8 @@ function runViaExecutor(binary, spec, signal) {
|
|
|
8687
8766
|
cleanup();
|
|
8688
8767
|
const raw = Buffer.concat(outChunks).toString("utf8").trim();
|
|
8689
8768
|
if (!raw) {
|
|
8690
|
-
const
|
|
8691
|
-
finish(() => reject(new ForgeError("TOOL_FAILED", `\u6267\u884C\u5668\u672A\u8FD4\u56DE\u7ED3\u679C${
|
|
8769
|
+
const stderr5 = Buffer.concat(errChunks).toString("utf8").trim();
|
|
8770
|
+
finish(() => reject(new ForgeError("TOOL_FAILED", `\u6267\u884C\u5668\u672A\u8FD4\u56DE\u7ED3\u679C${stderr5 ? `: ${stderr5}` : ""}`)));
|
|
8692
8771
|
return;
|
|
8693
8772
|
}
|
|
8694
8773
|
let parsed;
|
|
@@ -11316,41 +11395,6 @@ import { execFileSync } from "node:child_process";
|
|
|
11316
11395
|
import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync } from "node:fs";
|
|
11317
11396
|
import { homedir as homedir2 } from "node:os";
|
|
11318
11397
|
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
11398
|
function resolveProvider(input) {
|
|
11355
11399
|
const trimmed = input.trim();
|
|
11356
11400
|
if (!trimmed) return "deepseek";
|
|
@@ -11569,7 +11613,32 @@ function switchModel(runtime, name) {
|
|
|
11569
11613
|
}
|
|
11570
11614
|
|
|
11571
11615
|
// apps/cli/src/version.ts
|
|
11572
|
-
var VERSION = "0.5.
|
|
11616
|
+
var VERSION = "0.5.2";
|
|
11617
|
+
|
|
11618
|
+
// apps/cli/src/ui/input-edit.ts
|
|
11619
|
+
function applyInputKey(state, char, key) {
|
|
11620
|
+
const chars = Array.from(state.text);
|
|
11621
|
+
const at = Math.min(Math.max(0, state.cursor), chars.length);
|
|
11622
|
+
if (key.leftArrow) return { text: state.text, cursor: Math.max(0, at - 1) };
|
|
11623
|
+
if (key.rightArrow) return { text: state.text, cursor: Math.min(chars.length, at + 1) };
|
|
11624
|
+
if (key.ctrl && (char === "a" || char === "A")) return { text: state.text, cursor: 0 };
|
|
11625
|
+
if (key.ctrl && (char === "e" || char === "E")) return { text: state.text, cursor: chars.length };
|
|
11626
|
+
if (key.ctrl && (char === "u" || char === "U")) return { text: "", cursor: 0 };
|
|
11627
|
+
if (key.backspace || key.delete) {
|
|
11628
|
+
if (at === 0) return { text: state.text, cursor: 0 };
|
|
11629
|
+
chars.splice(at - 1, 1);
|
|
11630
|
+
return { text: chars.join(""), cursor: at - 1 };
|
|
11631
|
+
}
|
|
11632
|
+
if (key.ctrl || key.meta || key.tab || key.escape || key.upArrow || key.downArrow) {
|
|
11633
|
+
return { text: state.text, cursor: at };
|
|
11634
|
+
}
|
|
11635
|
+
if (!char) return null;
|
|
11636
|
+
const clean = char.replace(/[\r\n]+/g, " ");
|
|
11637
|
+
if (!clean) return { text: state.text, cursor: at };
|
|
11638
|
+
const inserted = Array.from(clean);
|
|
11639
|
+
chars.splice(at, 0, ...inserted);
|
|
11640
|
+
return { text: chars.join(""), cursor: at + inserted.length };
|
|
11641
|
+
}
|
|
11573
11642
|
|
|
11574
11643
|
// apps/cli/src/ui/App.tsx
|
|
11575
11644
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -11924,51 +11993,11 @@ function App({ runtime, bridge }) {
|
|
|
11924
11993
|
}, [advanceWizard, agent, busy, connecting, handleSlash, input, push, wizard]);
|
|
11925
11994
|
const editInput = useCallback(
|
|
11926
11995
|
(char, key) => {
|
|
11927
|
-
const
|
|
11928
|
-
|
|
11929
|
-
if (
|
|
11930
|
-
|
|
11931
|
-
|
|
11932
|
-
}
|
|
11933
|
-
if (key.rightArrow) {
|
|
11934
|
-
setCursor(Math.min(chars.length, at + 1));
|
|
11935
|
-
return true;
|
|
11936
|
-
}
|
|
11937
|
-
if (key.ctrl && (char === "a" || char === "A")) {
|
|
11938
|
-
setCursor(0);
|
|
11939
|
-
return true;
|
|
11940
|
-
}
|
|
11941
|
-
if (key.ctrl && (char === "e" || char === "E")) {
|
|
11942
|
-
setCursor(chars.length);
|
|
11943
|
-
return true;
|
|
11944
|
-
}
|
|
11945
|
-
if (key.backspace) {
|
|
11946
|
-
if (at > 0) {
|
|
11947
|
-
chars.splice(at - 1, 1);
|
|
11948
|
-
setInput(chars.join(""));
|
|
11949
|
-
setCursor(at - 1);
|
|
11950
|
-
}
|
|
11951
|
-
return true;
|
|
11952
|
-
}
|
|
11953
|
-
if (key.delete) {
|
|
11954
|
-
if (at < chars.length) {
|
|
11955
|
-
chars.splice(at, 1);
|
|
11956
|
-
setInput(chars.join(""));
|
|
11957
|
-
}
|
|
11958
|
-
return true;
|
|
11959
|
-
}
|
|
11960
|
-
if (key.ctrl || key.meta || key.tab || key.escape || key.upArrow || key.downArrow) {
|
|
11961
|
-
return true;
|
|
11962
|
-
}
|
|
11963
|
-
if (char) {
|
|
11964
|
-
const clean = char.replace(/[\r\n]+/g, " ");
|
|
11965
|
-
if (!clean) return true;
|
|
11966
|
-
chars.splice(at, 0, clean);
|
|
11967
|
-
setInput(chars.join(""));
|
|
11968
|
-
setCursor(at + Array.from(clean).length);
|
|
11969
|
-
return true;
|
|
11970
|
-
}
|
|
11971
|
-
return false;
|
|
11996
|
+
const next = applyInputKey({ text: input, cursor }, char, key);
|
|
11997
|
+
if (!next) return false;
|
|
11998
|
+
if (next.text !== input) setInput(next.text);
|
|
11999
|
+
if (next.cursor !== cursor) setCursor(next.cursor);
|
|
12000
|
+
return true;
|
|
11972
12001
|
},
|
|
11973
12002
|
[cursor, input]
|
|
11974
12003
|
);
|
|
@@ -12690,16 +12719,155 @@ function printHelp() {
|
|
|
12690
12719
|
);
|
|
12691
12720
|
}
|
|
12692
12721
|
|
|
12722
|
+
// apps/cli/src/commands/connect.ts
|
|
12723
|
+
import { createInterface as createInterface3 } from "node:readline/promises";
|
|
12724
|
+
import { resolve as resolve4 } from "node:path";
|
|
12725
|
+
import { stderr, stdin as stdin2, stdout as stdout2 } from "node:process";
|
|
12726
|
+
async function connectCommand(args, _options) {
|
|
12727
|
+
const isInteractive = Boolean(stdin2.isTTY);
|
|
12728
|
+
const configPath = defaultConfigPath();
|
|
12729
|
+
const existing = readExistingConfig(configPath);
|
|
12730
|
+
if (!isInteractive) {
|
|
12731
|
+
const provider = flagString(args.flags, "provider") ?? "custom";
|
|
12732
|
+
const apiKey = flagString(args.flags, "api-key") ?? "";
|
|
12733
|
+
const preset = PROVIDERS[provider.toLowerCase()];
|
|
12734
|
+
const baseUrl = flagString(args.flags, "base-url") ?? preset?.defaultBaseUrl ?? "";
|
|
12735
|
+
const model = flagString(args.flags, "model") ?? preset?.defaultModel ?? "";
|
|
12736
|
+
if (!apiKey) {
|
|
12737
|
+
stderr.write("[connect] --api-key \u5728\u975E\u4EA4\u4E92\u6A21\u5F0F\u4E0B\u5FC5\u586B\n");
|
|
12738
|
+
return 2;
|
|
12739
|
+
}
|
|
12740
|
+
return await doConnect({ provider, apiKey, baseUrl, model, configPath, existing });
|
|
12741
|
+
}
|
|
12742
|
+
stdout2.write("ForgeX Connect \u2014 \u8FDE\u63A5\u4F60\u7684\u6A21\u578B\u63D0\u4F9B\u65B9\n\n");
|
|
12743
|
+
const rl = createInterface3({ input: stdin2, output: stderr });
|
|
12744
|
+
try {
|
|
12745
|
+
stdout2.write("\u9009\u62E9\u6A21\u578B\u63D0\u4F9B\u65B9\uFF08\u8F93\u5165\u7F16\u53F7\u6216\u540D\u79F0\uFF09\uFF1A\n");
|
|
12746
|
+
PROVIDERS_KEYS.forEach((key, i) => {
|
|
12747
|
+
const p = PROVIDERS[key];
|
|
12748
|
+
stdout2.write(` ${i + 1}. ${p.name} (${p.defaultBaseUrl})
|
|
12749
|
+
`);
|
|
12750
|
+
});
|
|
12751
|
+
stdout2.write("\n> ");
|
|
12752
|
+
const providerInput = (await rl.question("")).trim();
|
|
12753
|
+
const provider = resolveProvider(providerInput);
|
|
12754
|
+
const preset = PROVIDERS[provider];
|
|
12755
|
+
const baseUrlDefault = preset?.defaultBaseUrl ?? existing.model.baseUrl;
|
|
12756
|
+
stdout2.write(`baseUrl [${baseUrlDefault}]: `);
|
|
12757
|
+
const baseUrl = (await rl.question("")).trim() || baseUrlDefault;
|
|
12758
|
+
stdout2.write("\nAPI Key\uFF08\u8F93\u5165\u4E0D\u4F1A\u88AB\u9690\u85CF\uFF0C\u7C98\u8D34\u540E\u6309\u56DE\u8F66\uFF09: ");
|
|
12759
|
+
const apiKey = (await rl.question("")).trim();
|
|
12760
|
+
if (!apiKey) {
|
|
12761
|
+
stderr.write("[connect] API Key \u4E0D\u80FD\u4E3A\u7A7A\n");
|
|
12762
|
+
return 2;
|
|
12763
|
+
}
|
|
12764
|
+
const modelDefault = preset?.defaultModel ?? existing.model.model;
|
|
12765
|
+
stdout2.write(`
|
|
12766
|
+
\u6A21\u578B\u540D [${modelDefault}]: `);
|
|
12767
|
+
const model = (await rl.question("")).trim() || modelDefault;
|
|
12768
|
+
const saveDefault = configPath;
|
|
12769
|
+
stdout2.write(`
|
|
12770
|
+
\u4FDD\u5B58\u914D\u7F6E\u5230 [${saveDefault}]\uFF1A`);
|
|
12771
|
+
const saveInput = (await rl.question("")).trim();
|
|
12772
|
+
const finalConfigPath = saveInput ? resolve4(saveInput) : saveDefault;
|
|
12773
|
+
stdout2.write("\n--------------------------------------------\n");
|
|
12774
|
+
stdout2.write(`Provider : ${provider}
|
|
12775
|
+
`);
|
|
12776
|
+
stdout2.write(`Base URL : ${baseUrl}
|
|
12777
|
+
`);
|
|
12778
|
+
stdout2.write(`Model : ${model}
|
|
12779
|
+
`);
|
|
12780
|
+
stdout2.write(`API Key : ${maskKey(apiKey)}
|
|
12781
|
+
`);
|
|
12782
|
+
stdout2.write(`Config : ${finalConfigPath}
|
|
12783
|
+
`);
|
|
12784
|
+
stdout2.write("--------------------------------------------\n");
|
|
12785
|
+
stdout2.write("\u8FDE\u901A\u6027\u6D4B\u8BD5\u4E2D\uFF085s \u8D85\u65F6\uFF09...\n");
|
|
12786
|
+
return await doConnect({ provider, apiKey, baseUrl, model, configPath: finalConfigPath, existing });
|
|
12787
|
+
} finally {
|
|
12788
|
+
rl.close();
|
|
12789
|
+
}
|
|
12790
|
+
}
|
|
12791
|
+
async function doConnect(params) {
|
|
12792
|
+
const { provider, apiKey, baseUrl, model, configPath, existing } = params;
|
|
12793
|
+
const testResult = await testConnectivity({ baseUrl, apiKey, model });
|
|
12794
|
+
if (!testResult.ok) {
|
|
12795
|
+
stderr.write(`
|
|
12796
|
+
[FAIL] \u8FDE\u901A\u6027\u6D4B\u8BD5\u5931\u8D25\uFF1A${testResult.reason}
|
|
12797
|
+
`);
|
|
12798
|
+
if (testResult.detail) stderr.write(` ${testResult.detail}
|
|
12799
|
+
`);
|
|
12800
|
+
stderr.write("\n\u63D0\u793A\uFF1A\n");
|
|
12801
|
+
stderr.write(" \xB7 \u786E\u8BA4 API Key \u6709\u6548\uFF1A\u53BB provider \u63A7\u5236\u53F0\u65B0\u5EFA\u4E00\u4E2A\n");
|
|
12802
|
+
stderr.write(" \xB7 \u786E\u8BA4 baseUrl \u6B63\u786E\uFF08DeepSeek: https://api.deepseek.com\uFF09\n");
|
|
12803
|
+
stderr.write(" \xB7 \u786E\u8BA4\u6A21\u578B\u540D\uFF08DeepSeek: deepseek-flash / deepseek-v4-pro\uFF09\n");
|
|
12804
|
+
return 1;
|
|
12805
|
+
}
|
|
12806
|
+
const preset = PROVIDERS[provider];
|
|
12807
|
+
const envName = preset?.apiKeyEnv ?? provider.toUpperCase().replace(/[^A-Z0-9_]/g, "_") + "_API_KEY";
|
|
12808
|
+
const envPersisted = persistApiKey(envName, apiKey);
|
|
12809
|
+
process.env[envName] = apiKey;
|
|
12810
|
+
saveConnectionConfig(configPath, existing, {
|
|
12811
|
+
baseUrl,
|
|
12812
|
+
model,
|
|
12813
|
+
envName,
|
|
12814
|
+
...preset ? { contextWindow: preset.contextWindow, maxOutputTokens: preset.maxOutputTokens } : {}
|
|
12815
|
+
});
|
|
12816
|
+
stdout2.write(`
|
|
12817
|
+
[OK] \u8FDE\u901A\u6027\u6D4B\u8BD5\u901A\u8FC7 \u2705
|
|
12818
|
+
`);
|
|
12819
|
+
stdout2.write(` \u54CD\u5E94\u8017\u65F6: ${testResult.latencyMs}ms
|
|
12820
|
+
`);
|
|
12821
|
+
stdout2.write(`
|
|
12822
|
+
\u914D\u7F6E\u5DF2\u5199\u5165\uFF1A${configPath}
|
|
12823
|
+
`);
|
|
12824
|
+
stdout2.write(`API Key \u6301\u4E45\u5316\uFF1A`);
|
|
12825
|
+
if (envPersisted.ok) {
|
|
12826
|
+
stdout2.write(`\u2705 ${envName}\uFF08\u7528\u6237\u7EA7\u73AF\u5883\u53D8\u91CF\uFF09
|
|
12827
|
+
`);
|
|
12828
|
+
stdout2.write(` \u5F53\u524D\u4F1A\u8BDD\u5DF2\u751F\u6548 \u2705
|
|
12829
|
+
`);
|
|
12830
|
+
} else {
|
|
12831
|
+
stdout2.write(`\u26A0\uFE0F \u9700\u624B\u52A8\u8BBE\u7F6E\uFF1A
|
|
12832
|
+
`);
|
|
12833
|
+
stdout2.write(` ${envPersisted.hint}
|
|
12834
|
+
`);
|
|
12835
|
+
}
|
|
12836
|
+
stdout2.write(`
|
|
12837
|
+
\u73B0\u5728\u53EF\u4EE5\u8DD1\uFF1A
|
|
12838
|
+
`);
|
|
12839
|
+
stdout2.write(` forgex run "\u4F60\u597D\uFF0C\u4ECB\u7ECD\u4E00\u4E0B ForgeX"
|
|
12840
|
+
`);
|
|
12841
|
+
stdout2.write(` forgex doctor
|
|
12842
|
+
`);
|
|
12843
|
+
return 0;
|
|
12844
|
+
}
|
|
12845
|
+
|
|
12693
12846
|
// apps/cli/src/commands/chat.ts
|
|
12694
12847
|
async function chatCommand(args, options) {
|
|
12695
|
-
const
|
|
12848
|
+
const configFlag = flagString(args.flags, "config");
|
|
12849
|
+
const readConfig = () => loadConfig({
|
|
12696
12850
|
cwd: options.cwd,
|
|
12697
|
-
...
|
|
12851
|
+
...configFlag ? { configPath: configFlag } : {}
|
|
12698
12852
|
});
|
|
12853
|
+
let loaded = readConfig();
|
|
12699
12854
|
if (!loaded.modelConfigured) {
|
|
12700
|
-
|
|
12701
|
-
|
|
12702
|
-
|
|
12855
|
+
const interactive = Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
12856
|
+
if (!interactive) {
|
|
12857
|
+
throw new ForgeError("CONFIG_INVALID", modelSetupHint({ interactive: false }));
|
|
12858
|
+
}
|
|
12859
|
+
stderr2.write("\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\uFF0C\u5148\u8FDB\u5165\u8FDE\u63A5\u5411\u5BFC\uFF08Ctrl+C \u53EF\u53D6\u6D88\uFF09\n\n");
|
|
12860
|
+
const code = await connectCommand(args, { cwd: options.cwd });
|
|
12861
|
+
if (code !== 0) {
|
|
12862
|
+
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");
|
|
12863
|
+
}
|
|
12864
|
+
loaded = readConfig();
|
|
12865
|
+
if (!loaded.modelConfigured) {
|
|
12866
|
+
throw new ForgeError(
|
|
12867
|
+
"CONFIG_INVALID",
|
|
12868
|
+
`\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`
|
|
12869
|
+
);
|
|
12870
|
+
}
|
|
12703
12871
|
}
|
|
12704
12872
|
const bridge = new ApprovalBridge();
|
|
12705
12873
|
const runtime = createRuntime({
|
|
@@ -12728,18 +12896,15 @@ async function chatCommand(args, options) {
|
|
|
12728
12896
|
}
|
|
12729
12897
|
|
|
12730
12898
|
// apps/cli/src/commands/run.ts
|
|
12731
|
-
import { createInterface as
|
|
12732
|
-
import { stdin as
|
|
12899
|
+
import { createInterface as createInterface4 } from "node:readline/promises";
|
|
12900
|
+
import { stdin as stdin3, stdout as stdout3, stderr as stderr3 } from "node:process";
|
|
12733
12901
|
async function runCommand(args, options) {
|
|
12734
12902
|
const loaded = loadConfig({
|
|
12735
12903
|
cwd: options.cwd,
|
|
12736
12904
|
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {}
|
|
12737
12905
|
});
|
|
12738
12906
|
if (!loaded.modelConfigured) {
|
|
12739
|
-
throw new ForgeError(
|
|
12740
|
-
"CONFIG_INVALID",
|
|
12741
|
-
"\u5C1A\u672A\u914D\u7F6E\u6A21\u578B\u3002\u8BF7\u521B\u5EFA forgex.config.json \u6216\u8BBE\u7F6E FORGEX_BASE_URL / FORGEX_MODEL\u3002"
|
|
12742
|
-
);
|
|
12907
|
+
throw new ForgeError("CONFIG_INVALID", modelSetupHint({ interactive: false }));
|
|
12743
12908
|
}
|
|
12744
12909
|
const prompt = (args.positionals.join(" ").trim() || await readStdin()).trim();
|
|
12745
12910
|
if (!prompt) {
|
|
@@ -12747,8 +12912,8 @@ async function runCommand(args, options) {
|
|
|
12747
12912
|
}
|
|
12748
12913
|
const json = flagBool(args.flags, "json");
|
|
12749
12914
|
const autoApprove = flagBool(args.flags, "yes");
|
|
12750
|
-
const interactive = Boolean(
|
|
12751
|
-
const rl = interactive ?
|
|
12915
|
+
const interactive = Boolean(stdin3.isTTY) && !json;
|
|
12916
|
+
const rl = interactive ? createInterface4({ input: stdin3, output: stderr3, terminal: true }) : null;
|
|
12752
12917
|
let approvalHandler;
|
|
12753
12918
|
if (autoApprove) {
|
|
12754
12919
|
approvalHandler = () => true;
|
|
@@ -12774,22 +12939,22 @@ async function runCommand(args, options) {
|
|
|
12774
12939
|
switch (event.type) {
|
|
12775
12940
|
case "assistant:delta":
|
|
12776
12941
|
sawDelta = true;
|
|
12777
|
-
|
|
12942
|
+
stdout3.write(event.text);
|
|
12778
12943
|
break;
|
|
12779
12944
|
case "tool:start":
|
|
12780
|
-
|
|
12945
|
+
stderr3.write(`[\u5DE5\u5177] \u8C03\u7528 ${event.name}
|
|
12781
12946
|
`);
|
|
12782
12947
|
break;
|
|
12783
12948
|
case "tool:end":
|
|
12784
|
-
|
|
12949
|
+
stderr3.write(`[\u5DE5\u5177] ${event.name} ${event.result.ok ? "\u6210\u529F" : "\u5931\u8D25"}\uFF1A${event.result.summary}
|
|
12785
12950
|
`);
|
|
12786
12951
|
break;
|
|
12787
12952
|
case "notice":
|
|
12788
|
-
|
|
12953
|
+
stderr3.write(`[\u63D0\u793A] ${event.message}
|
|
12789
12954
|
`);
|
|
12790
12955
|
break;
|
|
12791
12956
|
case "approval:resolved":
|
|
12792
|
-
|
|
12957
|
+
stderr3.write(`[\u5BA1\u6279] ${event.decision}
|
|
12793
12958
|
`);
|
|
12794
12959
|
break;
|
|
12795
12960
|
default:
|
|
@@ -12798,10 +12963,10 @@ async function runCommand(args, options) {
|
|
|
12798
12963
|
});
|
|
12799
12964
|
try {
|
|
12800
12965
|
const result = await runtime.agent.run(prompt, {});
|
|
12801
|
-
if (!json && !sawDelta && result.text)
|
|
12966
|
+
if (!json && !sawDelta && result.text) stdout3.write(`${result.text}
|
|
12802
12967
|
`);
|
|
12803
12968
|
if (json) {
|
|
12804
|
-
|
|
12969
|
+
stdout3.write(
|
|
12805
12970
|
`${JSON.stringify(
|
|
12806
12971
|
{
|
|
12807
12972
|
sessionId: result.sessionId,
|
|
@@ -12816,7 +12981,7 @@ async function runCommand(args, options) {
|
|
|
12816
12981
|
`
|
|
12817
12982
|
);
|
|
12818
12983
|
} else {
|
|
12819
|
-
|
|
12984
|
+
stderr3.write(
|
|
12820
12985
|
`[\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}
|
|
12821
12986
|
`
|
|
12822
12987
|
);
|
|
@@ -12829,9 +12994,9 @@ async function runCommand(args, options) {
|
|
|
12829
12994
|
}
|
|
12830
12995
|
}
|
|
12831
12996
|
async function readStdin() {
|
|
12832
|
-
if (
|
|
12997
|
+
if (stdin3.isTTY) return "";
|
|
12833
12998
|
const chunks = [];
|
|
12834
|
-
for await (const chunk of
|
|
12999
|
+
for await (const chunk of stdin3) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
|
|
12835
13000
|
return Buffer.concat(chunks).toString("utf8");
|
|
12836
13001
|
}
|
|
12837
13002
|
|
|
@@ -12840,7 +13005,7 @@ import { execFile as execFile3 } from "node:child_process";
|
|
|
12840
13005
|
import { accessSync, constants, existsSync as existsSync4 } from "node:fs";
|
|
12841
13006
|
import { DatabaseSync as DatabaseSync4 } from "node:sqlite";
|
|
12842
13007
|
import { dirname as dirname5 } from "node:path";
|
|
12843
|
-
import { stdout as
|
|
13008
|
+
import { stdout as stdout4 } from "node:process";
|
|
12844
13009
|
async function doctorCommand(args, options) {
|
|
12845
13010
|
const checks = [];
|
|
12846
13011
|
const full = flagBool(args.flags, "full");
|
|
@@ -12961,14 +13126,14 @@ async function doctorCommand(args, options) {
|
|
|
12961
13126
|
detail: `mode=${loaded.config.security.mode} \u5199\u5BA1\u6279=${loaded.config.security.requireApprovalForWrite} \u6267\u884C\u5BA1\u6279=${loaded.config.security.requireApprovalForExecute}`
|
|
12962
13127
|
});
|
|
12963
13128
|
}
|
|
12964
|
-
|
|
13129
|
+
stdout4.write("ForgeX \u73AF\u5883\u81EA\u68C0\n");
|
|
12965
13130
|
for (const check of checks) {
|
|
12966
|
-
|
|
13131
|
+
stdout4.write(`[${label(check.status)}] ${check.name}: ${check.detail}
|
|
12967
13132
|
`);
|
|
12968
13133
|
}
|
|
12969
13134
|
const failed = checks.filter((check) => check.status === "fail").length;
|
|
12970
13135
|
const warned = checks.filter((check) => check.status === "warn").length;
|
|
12971
|
-
|
|
13136
|
+
stdout4.write(`
|
|
12972
13137
|
\u7ED3\u679C\uFF1A${checks.length} \u9879\u68C0\u67E5\uFF0C${failed} \u9879\u5931\u8D25\uFF0C${warned} \u9879\u8B66\u544A
|
|
12973
13138
|
`);
|
|
12974
13139
|
return failed > 0 ? 1 : 0;
|
|
@@ -13001,21 +13166,21 @@ function formatBytes2(bytes) {
|
|
|
13001
13166
|
}
|
|
13002
13167
|
|
|
13003
13168
|
// apps/cli/src/commands/tools.ts
|
|
13004
|
-
import { stdout as
|
|
13169
|
+
import { stdout as stdout5 } from "node:process";
|
|
13005
13170
|
function toolsCommand() {
|
|
13006
13171
|
const registry = new ToolRegistry(createBuiltinTools());
|
|
13007
13172
|
const tools = registry.list();
|
|
13008
|
-
|
|
13173
|
+
stdout5.write(`ForgeX \u5185\u7F6E\u5DE5\u5177\uFF08${tools.length} \u4E2A\uFF09
|
|
13009
13174
|
|
|
13010
13175
|
`);
|
|
13011
13176
|
for (const tool of tools) {
|
|
13012
13177
|
const tags = tool.tags && tool.tags.length > 0 ? ` \u6807\u7B7E=${tool.tags.join(",")}` : "";
|
|
13013
|
-
|
|
13178
|
+
stdout5.write(`- ${tool.name} [${tool.riskLevel}] \u8D85\u65F6=${tool.timeoutMs}ms \u8F93\u51FA\u4E0A\u9650=${tool.maxOutputBytes}B${tags}
|
|
13014
13179
|
`);
|
|
13015
|
-
|
|
13180
|
+
stdout5.write(` ${tool.description.split("\n")[0]}
|
|
13016
13181
|
`);
|
|
13017
13182
|
}
|
|
13018
|
-
|
|
13183
|
+
stdout5.write(
|
|
13019
13184
|
"\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"
|
|
13020
13185
|
);
|
|
13021
13186
|
return 0;
|
|
@@ -13023,7 +13188,7 @@ function toolsCommand() {
|
|
|
13023
13188
|
|
|
13024
13189
|
// apps/cli/src/commands/sessions.ts
|
|
13025
13190
|
import { existsSync as existsSync5 } from "node:fs";
|
|
13026
|
-
import { stdout as
|
|
13191
|
+
import { stdout as stdout6 } from "node:process";
|
|
13027
13192
|
function sessionsCommand(args, options) {
|
|
13028
13193
|
const loaded = loadConfig({
|
|
13029
13194
|
cwd: options.cwd,
|
|
@@ -13031,7 +13196,7 @@ function sessionsCommand(args, options) {
|
|
|
13031
13196
|
});
|
|
13032
13197
|
const dbPath = loaded.config.session.dbPath;
|
|
13033
13198
|
if (!existsSync5(dbPath)) {
|
|
13034
|
-
|
|
13199
|
+
stdout6.write(`\u4F1A\u8BDD\u6570\u636E\u5E93\u5C1A\u672A\u521B\u5EFA: ${dbPath}
|
|
13035
13200
|
`);
|
|
13036
13201
|
return 0;
|
|
13037
13202
|
}
|
|
@@ -13040,18 +13205,18 @@ function sessionsCommand(args, options) {
|
|
|
13040
13205
|
try {
|
|
13041
13206
|
const sessions = store.listSessions(limit);
|
|
13042
13207
|
if (sessions.length === 0) {
|
|
13043
|
-
|
|
13208
|
+
stdout6.write("\u6682\u65E0\u4F1A\u8BDD\u8BB0\u5F55\n");
|
|
13044
13209
|
return 0;
|
|
13045
13210
|
}
|
|
13046
|
-
|
|
13211
|
+
stdout6.write(`\u6700\u8FD1 ${sessions.length} \u4E2A\u4F1A\u8BDD\uFF08\u6570\u636E\u5E93: ${dbPath}\uFF09
|
|
13047
13212
|
|
|
13048
13213
|
`);
|
|
13049
13214
|
for (const session of sessions) {
|
|
13050
13215
|
const title = session.title ?? "(\u672A\u547D\u540D)";
|
|
13051
|
-
|
|
13216
|
+
stdout6.write(`${session.id} ${session.updatedAt} ${session.model} ${title}
|
|
13052
13217
|
`);
|
|
13053
13218
|
}
|
|
13054
|
-
|
|
13219
|
+
stdout6.write('\n\u4F7F\u7528 forgex chat --session <id> \u6062\u590D\u4F1A\u8BDD\uFF1Bforgex run --session <id> "..." \u7EE7\u7EED\u4EFB\u52A1\u3002\n');
|
|
13055
13220
|
return 0;
|
|
13056
13221
|
} finally {
|
|
13057
13222
|
store.close();
|
|
@@ -13059,14 +13224,14 @@ function sessionsCommand(args, options) {
|
|
|
13059
13224
|
}
|
|
13060
13225
|
|
|
13061
13226
|
// apps/cli/src/commands/config.ts
|
|
13062
|
-
import { stdout as
|
|
13227
|
+
import { stdout as stdout7 } from "node:process";
|
|
13063
13228
|
function configCommand(args, options) {
|
|
13064
13229
|
const loaded = loadConfig({
|
|
13065
13230
|
cwd: options.cwd,
|
|
13066
13231
|
...flagString(args.flags, "config") ? { configPath: flagString(args.flags, "config") } : {}
|
|
13067
13232
|
});
|
|
13068
13233
|
const { apiKey, ...safeModel } = loaded.config.model;
|
|
13069
|
-
|
|
13234
|
+
stdout7.write(
|
|
13070
13235
|
`${JSON.stringify(
|
|
13071
13236
|
{
|
|
13072
13237
|
configPath: loaded.configPath,
|
|
@@ -13083,142 +13248,18 @@ function configCommand(args, options) {
|
|
|
13083
13248
|
`
|
|
13084
13249
|
);
|
|
13085
13250
|
if (!loaded.modelConfigured) {
|
|
13086
|
-
|
|
13251
|
+
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");
|
|
13087
13252
|
}
|
|
13088
13253
|
return 0;
|
|
13089
13254
|
}
|
|
13090
13255
|
|
|
13091
|
-
// apps/cli/src/commands/connect.ts
|
|
13092
|
-
import { createInterface as createInterface4 } from "node:readline/promises";
|
|
13093
|
-
import { resolve as resolve4 } from "node:path";
|
|
13094
|
-
import { stderr as stderr2, stdin as stdin3, stdout as stdout7 } from "node:process";
|
|
13095
|
-
async function connectCommand(args, _options) {
|
|
13096
|
-
const isInteractive = Boolean(stdin3.isTTY);
|
|
13097
|
-
const configPath = defaultConfigPath();
|
|
13098
|
-
const existing = readExistingConfig(configPath);
|
|
13099
|
-
if (!isInteractive) {
|
|
13100
|
-
const provider = flagString(args.flags, "provider") ?? "custom";
|
|
13101
|
-
const apiKey = flagString(args.flags, "api-key") ?? "";
|
|
13102
|
-
const preset = PROVIDERS[provider.toLowerCase()];
|
|
13103
|
-
const baseUrl = flagString(args.flags, "base-url") ?? preset?.defaultBaseUrl ?? "";
|
|
13104
|
-
const model = flagString(args.flags, "model") ?? preset?.defaultModel ?? "";
|
|
13105
|
-
if (!apiKey) {
|
|
13106
|
-
stderr2.write("[connect] --api-key \u5728\u975E\u4EA4\u4E92\u6A21\u5F0F\u4E0B\u5FC5\u586B\n");
|
|
13107
|
-
return 2;
|
|
13108
|
-
}
|
|
13109
|
-
return await doConnect({ provider, apiKey, baseUrl, model, configPath, existing });
|
|
13110
|
-
}
|
|
13111
|
-
stdout7.write("ForgeX Connect \u2014 \u8FDE\u63A5\u4F60\u7684\u6A21\u578B\u63D0\u4F9B\u65B9\n\n");
|
|
13112
|
-
const rl = createInterface4({ input: stdin3, output: stderr2 });
|
|
13113
|
-
try {
|
|
13114
|
-
stdout7.write("\u9009\u62E9\u6A21\u578B\u63D0\u4F9B\u65B9\uFF08\u8F93\u5165\u7F16\u53F7\u6216\u540D\u79F0\uFF09\uFF1A\n");
|
|
13115
|
-
PROVIDERS_KEYS.forEach((key, i) => {
|
|
13116
|
-
const p = PROVIDERS[key];
|
|
13117
|
-
stdout7.write(` ${i + 1}. ${p.name} (${p.defaultBaseUrl})
|
|
13118
|
-
`);
|
|
13119
|
-
});
|
|
13120
|
-
stdout7.write("\n> ");
|
|
13121
|
-
const providerInput = (await rl.question("")).trim();
|
|
13122
|
-
const provider = resolveProvider(providerInput);
|
|
13123
|
-
const preset = PROVIDERS[provider];
|
|
13124
|
-
const baseUrlDefault = preset?.defaultBaseUrl ?? existing.model.baseUrl;
|
|
13125
|
-
stdout7.write(`baseUrl [${baseUrlDefault}]: `);
|
|
13126
|
-
const baseUrl = (await rl.question("")).trim() || baseUrlDefault;
|
|
13127
|
-
stdout7.write("\nAPI Key\uFF08\u8F93\u5165\u4E0D\u4F1A\u88AB\u9690\u85CF\uFF0C\u7C98\u8D34\u540E\u6309\u56DE\u8F66\uFF09: ");
|
|
13128
|
-
const apiKey = (await rl.question("")).trim();
|
|
13129
|
-
if (!apiKey) {
|
|
13130
|
-
stderr2.write("[connect] API Key \u4E0D\u80FD\u4E3A\u7A7A\n");
|
|
13131
|
-
return 2;
|
|
13132
|
-
}
|
|
13133
|
-
const modelDefault = preset?.defaultModel ?? existing.model.model;
|
|
13134
|
-
stdout7.write(`
|
|
13135
|
-
\u6A21\u578B\u540D [${modelDefault}]: `);
|
|
13136
|
-
const model = (await rl.question("")).trim() || modelDefault;
|
|
13137
|
-
const saveDefault = configPath;
|
|
13138
|
-
stdout7.write(`
|
|
13139
|
-
\u4FDD\u5B58\u914D\u7F6E\u5230 [${saveDefault}]\uFF1A`);
|
|
13140
|
-
const saveInput = (await rl.question("")).trim();
|
|
13141
|
-
const finalConfigPath = saveInput ? resolve4(saveInput) : saveDefault;
|
|
13142
|
-
stdout7.write("\n--------------------------------------------\n");
|
|
13143
|
-
stdout7.write(`Provider : ${provider}
|
|
13144
|
-
`);
|
|
13145
|
-
stdout7.write(`Base URL : ${baseUrl}
|
|
13146
|
-
`);
|
|
13147
|
-
stdout7.write(`Model : ${model}
|
|
13148
|
-
`);
|
|
13149
|
-
stdout7.write(`API Key : ${maskKey(apiKey)}
|
|
13150
|
-
`);
|
|
13151
|
-
stdout7.write(`Config : ${finalConfigPath}
|
|
13152
|
-
`);
|
|
13153
|
-
stdout7.write("--------------------------------------------\n");
|
|
13154
|
-
stdout7.write("\u8FDE\u901A\u6027\u6D4B\u8BD5\u4E2D\uFF085s \u8D85\u65F6\uFF09...\n");
|
|
13155
|
-
return await doConnect({ provider, apiKey, baseUrl, model, configPath: finalConfigPath, existing });
|
|
13156
|
-
} finally {
|
|
13157
|
-
rl.close();
|
|
13158
|
-
}
|
|
13159
|
-
}
|
|
13160
|
-
async function doConnect(params) {
|
|
13161
|
-
const { provider, apiKey, baseUrl, model, configPath, existing } = params;
|
|
13162
|
-
const testResult = await testConnectivity({ baseUrl, apiKey, model });
|
|
13163
|
-
if (!testResult.ok) {
|
|
13164
|
-
stderr2.write(`
|
|
13165
|
-
[FAIL] \u8FDE\u901A\u6027\u6D4B\u8BD5\u5931\u8D25\uFF1A${testResult.reason}
|
|
13166
|
-
`);
|
|
13167
|
-
if (testResult.detail) stderr2.write(` ${testResult.detail}
|
|
13168
|
-
`);
|
|
13169
|
-
stderr2.write("\n\u63D0\u793A\uFF1A\n");
|
|
13170
|
-
stderr2.write(" \xB7 \u786E\u8BA4 API Key \u6709\u6548\uFF1A\u53BB provider \u63A7\u5236\u53F0\u65B0\u5EFA\u4E00\u4E2A\n");
|
|
13171
|
-
stderr2.write(" \xB7 \u786E\u8BA4 baseUrl \u6B63\u786E\uFF08DeepSeek: https://api.deepseek.com\uFF09\n");
|
|
13172
|
-
stderr2.write(" \xB7 \u786E\u8BA4\u6A21\u578B\u540D\uFF08DeepSeek: deepseek-flash / deepseek-v4-pro\uFF09\n");
|
|
13173
|
-
return 1;
|
|
13174
|
-
}
|
|
13175
|
-
const preset = PROVIDERS[provider];
|
|
13176
|
-
const envName = preset?.apiKeyEnv ?? provider.toUpperCase().replace(/[^A-Z0-9_]/g, "_") + "_API_KEY";
|
|
13177
|
-
const envPersisted = persistApiKey(envName, apiKey);
|
|
13178
|
-
process.env[envName] = apiKey;
|
|
13179
|
-
saveConnectionConfig(configPath, existing, {
|
|
13180
|
-
baseUrl,
|
|
13181
|
-
model,
|
|
13182
|
-
envName,
|
|
13183
|
-
...preset ? { contextWindow: preset.contextWindow, maxOutputTokens: preset.maxOutputTokens } : {}
|
|
13184
|
-
});
|
|
13185
|
-
stdout7.write(`
|
|
13186
|
-
[OK] \u8FDE\u901A\u6027\u6D4B\u8BD5\u901A\u8FC7 \u2705
|
|
13187
|
-
`);
|
|
13188
|
-
stdout7.write(` \u54CD\u5E94\u8017\u65F6: ${testResult.latencyMs}ms
|
|
13189
|
-
`);
|
|
13190
|
-
stdout7.write(`
|
|
13191
|
-
\u914D\u7F6E\u5DF2\u5199\u5165\uFF1A${configPath}
|
|
13192
|
-
`);
|
|
13193
|
-
stdout7.write(`API Key \u6301\u4E45\u5316\uFF1A`);
|
|
13194
|
-
if (envPersisted.ok) {
|
|
13195
|
-
stdout7.write(`\u2705 ${envName}\uFF08\u7528\u6237\u7EA7\u73AF\u5883\u53D8\u91CF\uFF09
|
|
13196
|
-
`);
|
|
13197
|
-
stdout7.write(` \u5F53\u524D\u4F1A\u8BDD\u5DF2\u751F\u6548 \u2705
|
|
13198
|
-
`);
|
|
13199
|
-
} else {
|
|
13200
|
-
stdout7.write(`\u26A0\uFE0F \u9700\u624B\u52A8\u8BBE\u7F6E\uFF1A
|
|
13201
|
-
`);
|
|
13202
|
-
stdout7.write(` ${envPersisted.hint}
|
|
13203
|
-
`);
|
|
13204
|
-
}
|
|
13205
|
-
stdout7.write(`
|
|
13206
|
-
\u73B0\u5728\u53EF\u4EE5\u8DD1\uFF1A
|
|
13207
|
-
`);
|
|
13208
|
-
stdout7.write(` forgex run "\u4F60\u597D\uFF0C\u4ECB\u7ECD\u4E00\u4E0B ForgeX"
|
|
13209
|
-
`);
|
|
13210
|
-
stdout7.write(` forgex doctor
|
|
13211
|
-
`);
|
|
13212
|
-
return 0;
|
|
13213
|
-
}
|
|
13214
|
-
|
|
13215
13256
|
// apps/cli/src/main.ts
|
|
13216
13257
|
async function main(argv) {
|
|
13217
13258
|
const args = parseArgs(argv);
|
|
13218
13259
|
const unknown = unknownFlags(args.flags);
|
|
13219
13260
|
if (unknown.length > 0) {
|
|
13220
13261
|
const rendered = unknown.map((name) => name.length === 1 ? `-${name}` : `--${name}`).join(", ");
|
|
13221
|
-
|
|
13262
|
+
stderr4.write(`\u672A\u77E5\u53C2\u6570: ${rendered}
|
|
13222
13263
|
|
|
13223
13264
|
`);
|
|
13224
13265
|
printHelp2();
|
|
@@ -13227,7 +13268,7 @@ async function main(argv) {
|
|
|
13227
13268
|
const missingValue = flagsMissingValue(args.flags);
|
|
13228
13269
|
if (missingValue.length > 0) {
|
|
13229
13270
|
const rendered = missingValue.map((name) => `--${name}`).join(", ");
|
|
13230
|
-
|
|
13271
|
+
stderr4.write(`\u53C2\u6570\u7F3A\u5C11\u53D6\u503C: ${rendered}
|
|
13231
13272
|
|
|
13232
13273
|
`);
|
|
13233
13274
|
printHelp2();
|
|
@@ -13263,7 +13304,7 @@ async function main(argv) {
|
|
|
13263
13304
|
case "connect":
|
|
13264
13305
|
return await connectCommand(args, { cwd });
|
|
13265
13306
|
default:
|
|
13266
|
-
|
|
13307
|
+
stderr4.write(`\u672A\u77E5\u547D\u4EE4: ${args.command}
|
|
13267
13308
|
|
|
13268
13309
|
`);
|
|
13269
13310
|
printHelp2();
|
|
@@ -13310,7 +13351,7 @@ function formatError(err) {
|
|
|
13310
13351
|
void main(process.argv.slice(2)).then((code) => {
|
|
13311
13352
|
process.exitCode = code;
|
|
13312
13353
|
}).catch((err) => {
|
|
13313
|
-
|
|
13354
|
+
stderr4.write(`[\u9519\u8BEF] ${formatError(err)}
|
|
13314
13355
|
`);
|
|
13315
13356
|
process.exitCode = 1;
|
|
13316
13357
|
});
|