



























@@ -31,6 +31,25 @@ function buildAuthHeaders(token, cookie) {
3131return headers;
3232}
333334+function sleep(ms) {
35+return new Promise((resolve) => {
36+setTimeout(resolve, ms);
37+});
38+}
39+40+function extractModelIds(modelsJson) {
41+const models = Array.isArray(modelsJson)
42+ ? modelsJson
43+ : Array.isArray(modelsJson?.data)
44+ ? modelsJson.data
45+ : Array.isArray(modelsJson?.models)
46+ ? modelsJson.models
47+ : [];
48+return models
49+.map((entry) => entry?.id ?? entry?.model ?? entry?.name)
50+.filter((value) => typeof value === "string");
51+}
52+3453const signinRes = await fetch(`${baseUrl}/api/v1/auths/signin`, {
3554method: "POST",
3655headers: { "content-type": "application/json" },
@@ -50,25 +69,34 @@ const authHeaders = {
5069accept: "application/json",
5170};
527153-const modelsRes = await fetch(`${baseUrl}/api/models`, { headers: authHeaders });
54-if (!modelsRes.ok) {
55-throw new Error(`/api/models failed: HTTP ${modelsRes.status} ${await modelsRes.text()}`);
72+let modelIds = [];
73+let targetModel = "";
74+let lastModelsError = "";
75+for (let attempt = 1; attempt <= 24; attempt += 1) {
76+const modelsRes = await fetch(`${baseUrl}/api/models`, { headers: authHeaders }).catch(
77+(error) => {
78+lastModelsError = error instanceof Error ? error.message : String(error);
79+return undefined;
80+},
81+);
82+if (modelsRes?.ok) {
83+const modelsJson = await modelsRes.json();
84+modelIds = extractModelIds(modelsJson);
85+targetModel =
86+modelIds.find((id) => id === "openclaw/default") ?? modelIds.find((id) => id === "openclaw");
87+if (targetModel) {
88+break;
89+}
90+lastModelsError = `missing openclaw model: ${JSON.stringify(modelIds)}`;
91+} else if (modelsRes) {
92+lastModelsError = `HTTP ${modelsRes.status} ${await modelsRes.text()}`;
93+}
94+await sleep(5_000);
5695}
57-const modelsJson = await modelsRes.json();
58-const models = Array.isArray(modelsJson)
59- ? modelsJson
60- : Array.isArray(modelsJson?.data)
61- ? modelsJson.data
62- : Array.isArray(modelsJson?.models)
63- ? modelsJson.models
64- : [];
65-const modelIds = models
66-.map((entry) => entry?.id ?? entry?.model ?? entry?.name)
67-.filter((value) => typeof value === "string");
68-const targetModel =
69-modelIds.find((id) => id === "openclaw/default") ?? modelIds.find((id) => id === "openclaw");
7096if (!targetModel) {
71-throw new Error(`openclaw model missing from Open WebUI model list: ${JSON.stringify(modelIds)}`);
97+throw new Error(
98+`openclaw model missing from Open WebUI model list after retry: ${JSON.stringify(modelIds)} (${lastModelsError})`,
99+);
72100}
7310174102const chatRes = await fetch(`${baseUrl}/api/chat/completions`, {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。