fix(dsh): probe xAI on its own endpoint and wait for a saved backend

- give xAI its own protocol: its credential carries an api host instead of
  a base URL, so the probe no longer demands LLM_BASE_URL for a
  configuration ReMe accepts
- say that the xAI probe is a REST-level approximation of the gRPC SDK
  ReMe uses for that provider
- hold the LLM test back while the chosen backend is not saved, because the
  probe reads the .env on disk
This commit is contained in:
LiangshouX 2026-10-29 17:42:22 +08:00
parent c07268b4a1
commit 9fa6cf4b9d
7 changed files with 99 additions and 19 deletions

View file

@ -104,13 +104,18 @@ DSH settings.
cannot lose unrelated keys.
- **Test LLM connection** runs a one-token request from inside the workspace with the selected interpreter, and
adds a hint for `401`/`403` (key), `404` (model or base URL) and network errors. The request follows the
protocol of the `LLM_BACKEND` in the `.env`: `openai`, `dashscope`, `deepseek`, `moonshot` and `xai` use the
protocol of the `LLM_BACKEND` in the `.env`: `openai`, `dashscope`, `deepseek` and `moonshot` use the
OpenAI-compatible `/chat/completions` (Bearer auth), `anthropic` uses `/v1/messages` (`x-api-key`), `gemini`
uses `/v1beta/models/<model>:generateContent` (`x-goog-api-key`) and `ollama` uses `/api/chat` (a local server
needs no key). Gemini and Ollama are probed with their own endpoint, because ReMe's credentials for them do not
read `LLM_BASE_URL`; a stale value left in the `.env` cannot send the probe to a host the service never calls.
The base URL is used as the provider's own SDK builds it, so a base that already carries the version suffix
fails in the probe exactly as it fails in the service.
uses `/v1beta/models/<model>:generateContent` (`x-goog-api-key`), `ollama` uses `/api/chat` (a local server
needs no key) and `xai` uses the OpenAI-compatible `/chat/completions` at `api.x.ai`.
- The base URL follows ReMe's own credentials: `LLM_BASE_URL` is read for the OpenAI-compatible providers and for
Anthropic, while Gemini, Ollama and xAI carry their own endpoint and ignore it. A stale value left in the `.env`
therefore cannot send the probe to a host the service never calls, and a configuration ReMe accepts is never
reported as broken. The base URL is used as the provider's own SDK builds it, so a base that already carries the
version suffix fails in the probe exactly as it fails in the service.
- ReMe reaches xAI through its gRPC SDK, so the xAI probe is a REST-level approximation of that channel.
- The probe reads the `.env` on disk, so a backend that is only picked in the form is not tested before the file is
saved; the button says so while it waits.
- Drafts are bound to the workspace they were typed in: switching the workspace drops them, so a key typed for
one workspace cannot be saved into another.

View file

@ -84,11 +84,16 @@ ReMe 在服务启动时读取 workspace 目录下的 `.env`;标签页从不把
- 写入是原子且仅属主可读写的(POSIX 上为 0600);旧文件会保留为 `.env.bak`,因此一次失败的保存不会丢掉其它键。
- **测试 LLM 连接**在 workspace 内、用所选解释器发送一次只需 1 个 token 的请求,并对 `401`/`403`
(密钥)、`404`(模型名或 Base URL)与网络错误给出提示。请求按 `.env` 里的 `LLM_BACKEND` 选择协议:
`openai`、`dashscope`、`deepseek`、`moonshot`、`xai` 走 OpenAI 兼容的 `/chat/completions`(Bearer 认证),
`openai`、`dashscope`、`deepseek`、`moonshot` 走 OpenAI 兼容的 `/chat/completions`(Bearer 认证),
`anthropic` 走 `/v1/messages`(`x-api-key`),`gemini` 走 `/v1beta/models/<model>:generateContent`
(`x-goog-api-key`),`ollama` 走 `/api/chat`(本地服务无需密钥)。Gemini 与 Ollama 一律使用服务商自有地址探测,
因为 ReMe 为它们生成的凭据并不读取 `LLM_BASE_URL`;`.env` 里残留的旧值不会把探测引到服务根本不会访问的主机。
Base URL 的拼接方式与服务商 SDK 完全一致,因此已经带版本后缀的地址在探测与服务中都会同样失败。
(`x-goog-api-key`),`ollama` 走 `/api/chat`(本地服务无需密钥),`xai` 走 `api.x.ai` 上 OpenAI 兼容的
`/chat/completions`。
- Base URL 的来源与 ReMe 自身的凭据保持一致:`LLM_BASE_URL` 只对 OpenAI 兼容族与 Anthropic 生效,Gemini、Ollama
与 xAI 自带地址、并不读取该变量。因此 `.env` 里残留的旧值不会把探测引到服务根本不会访问的主机,ReMe 能接受的
配置也不会被报成故障。Base URL 的拼接方式与服务商 SDK 完全一致,因此已经带版本后缀的地址在探测与服务中都会
同样失败。
- ReMe 通过 gRPC SDK 连接 xAI,因此 xAI 的探测只是该通道在 REST 层面的近似。
- 探测读取的是磁盘上的 `.env`,所以只在表单里切换后端而未保存时不会发起测试;等待期间按钮会给出提示。
- 草稿与输入它的 workspace 绑定:切换 workspace 会丢弃草稿,因此为某个 workspace 输入的密钥不会被保存到另一个。
## 连接测试

View file

@ -94,6 +94,8 @@ export const envEn = {
saveEnvFailed: "Saving failed",
testLlmButton: "Test LLM connection",
testingLlm: "Testing…",
testLlmSaveFirst:
"Save the .env first: the test uses the backend that is saved on disk.",
llmOk: "LLM reachable ({ms} ms)",
llmFailed: "LLM test failed",
serviceHint:
@ -291,6 +293,7 @@ export const envZh: typeof envEn = {
saveEnvFailed: "保存失败",
testLlmButton: "测试 LLM 连接",
testingLlm: "正在测试…",
testLlmSaveFirst: "请先保存 .env:测试使用的是磁盘上已保存的后端。",
llmOk: "LLM 连接正常({ms} ms)",
llmFailed: "LLM 测试失败",
serviceHint: "启动或停止本地 ReMe 服务;DSH 关闭后服务仍会保持运行。",

View file

@ -415,6 +415,13 @@ export function WorkspaceSection({
}): JSX.Element {
const [open, setOpen] = useState(false);
const { env, llmTest, result } = workspace;
// The probe reads the saved .env, so a backend that is only picked in the
// form would be tested against the one still on disk. The button says so
// instead of quietly testing the old backend.
const savedBackend =
env.data?.entries.find((entry) => entry.key === "LLM_BACKEND")?.value ?? "";
const backendChanged =
workspace.value("LLM_BACKEND").trim() !== savedBackend.trim();
return (
<section className={`reme-env-card${open ? "" : " collapsed"}`}>
@ -507,7 +514,9 @@ export function WorkspaceSection({
<button
type="button"
className="reme-settings-button"
disabled={!pythonPath || llmTest.phase === "running"}
disabled={
!pythonPath || llmTest.phase === "running" || backendChanged
}
onClick={() => void workspace.testLlm(pythonPath)}
>
{t(
@ -516,6 +525,11 @@ export function WorkspaceSection({
: "testLlmButton",
)}
</button>
{backendChanged ? (
<span className="reme-settings-muted">
{t("testLlmSaveFirst")}
</span>
) : null}
<span className="reme-settings-result" role="status">
{result.kind === "saved" ? (
<span className="reme-settings-success">

View file

@ -202,10 +202,11 @@ export interface LlmProtocol {
*
* ReMe's credentials decide this. The OpenAI-compatible providers and
* Anthropic read `LLM_BASE_URL` from the workspace `.env`, so the probe
* reads it too. Gemini and Ollama ignore that variable and use their own
* endpoint, so the probe must use that one as well: a stale value left in
* the `.env` would otherwise send the probe to a host the service never
* uses, and report a working configuration as broken.
* reads it too. Gemini, Ollama and xAI ignore that variable — their
* credentials carry their own endpoint — so the probe must use that one as
* well: a stale value left in the `.env` would otherwise send the probe to
* a host the service never uses, and report a working configuration as
* broken.
*/
baseSource: "env" | "provider";
/** The provider's own endpoint; used when `baseSource` is `provider`. */
@ -238,7 +239,6 @@ export const LLM_PROTOCOLS: Record<string, LlmProtocol> = {
dashscope: OPENAI_PROTOCOL,
deepseek: OPENAI_PROTOCOL,
moonshot: OPENAI_PROTOCOL,
xai: OPENAI_PROTOCOL,
anthropic: {
path: "/v1/messages",
authHeader: "x-api-key",
@ -268,6 +268,20 @@ export const LLM_PROTOCOLS: Record<string, LlmProtocol> = {
baseSource: "provider",
providerBase: "http://localhost:11434",
},
xai: {
// ReMe reaches xAI through its gRPC SDK, so this OpenAI-compatible
// endpoint is a connectivity approximation for that backend. It is still
// the documented REST API of the same provider and the same key, and it
// keeps the probe from reporting a working configuration as broken.
path: "/chat/completions",
authHeader: "Authorization",
authPrefix: "Bearer ",
headers: {},
body: OPENAI_CHAT_BODY,
keyRequired: true,
baseSource: "provider",
providerBase: "https://api.x.ai/v1",
},
};
/** The backend ReMe itself falls back to when `.env` names none. */

View file

@ -1508,6 +1508,37 @@ test("a running job is restored for the selected interpreter", async (t) => {
assert.equal(lookups[0].input.pythonPath, SUGGESTED);
});
test("the LLM test waits for the backend that is saved on disk", async (t) => {
const { renderer, restore } = await mount();
t.after(restore);
await act(async () => {
buttonByText(renderer, envZh.expand).props.onClick();
});
// The saved backend is the one the probe would use.
assert.equal(
buttonByText(renderer, envZh.testLlmButton).props.disabled,
false,
);
// Picking another backend in the form does not change the file on disk, so
// the test would probe the old one.
const backend = renderer.root
.findAll((node) => node.type === "select")
.find((node) => node.props.value === "dashscope");
assert.ok(backend, "the LLM backend select");
await act(async () => {
backend.props.onChange({ target: { value: "anthropic" } });
});
assert.ok(
texts(renderer.toJSON()).join(" ").includes(envZh.testLlmSaveFirst),
"the button explains that the .env has to be saved first",
);
assert.equal(
buttonByText(renderer, envZh.testLlmButton).props.disabled,
true,
);
});
test("a restored job is matched by the interpreter's own path semantics", async (t) => {
// The Host reports the interpreter with its own spelling; a Windows path
// folds case and separators, so the job is still the one on screen.

View file

@ -7,6 +7,7 @@ import { join } from "node:path";
import {
envSummary,
LLM_PROTOCOLS,
llmProbeSnippet,
llmProtocolFor,
maskSecret,
mergeEnvFile,
@ -294,9 +295,10 @@ test("the probe builds a valid request for every backend", async (t) => {
protocol.path.startsWith("/"),
`${backend} path is appended to the base`,
);
assert.equal(
protocol.trimSuffix,
undefined,
const snippet = llmProbeSnippet(protocol);
assert.doesNotMatch(
snippet,
/base\[:|endswith/,
`${backend} does not rewrite the configured base`,
);
}
@ -347,6 +349,12 @@ test("a backend that ignores LLM_BASE_URL is probed with its own endpoint", asyn
assert.match(stale, /localhost:11434/);
assert.doesNotMatch(stale, /stale\.invalid/);
// xAI is the same case: its credential carries an api host, not a base URL.
const xai = await probe("xai", "https://stale.invalid");
assert.match(xai, /api\.x\.ai/);
assert.doesNotMatch(xai, /stale\.invalid/);
assert.doesNotMatch(xai, /env\.get\("LLM_BASE_URL"/);
// Ollama needs neither a base URL nor a key.
const ollama = await probe("ollama");
assert.match(ollama, /localhost:11434/);