-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplatform-clients.js
More file actions
125 lines (117 loc) · 4.37 KB
/
Copy pathplatform-clients.js
File metadata and controls
125 lines (117 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(function () {
function jsonHeaders(body, headers = {}) {
return {
...(body !== undefined ? { "Content-Type": "application/json" } : {}),
...headers,
};
}
async function readJsonResponse(response, fallbackError) {
const result = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(result.error || `${fallbackError}:${response.status}`);
}
return result;
}
function createCloudApiClient(options = {}) {
const fetchImpl = options.fetchImpl || fetch;
return {
async request(path, requestOptions = {}) {
const init = {
method: requestOptions.method || "GET",
credentials: "include",
headers: jsonHeaders(requestOptions.body, requestOptions.headers),
};
if (requestOptions.body !== undefined) {
init.body = typeof requestOptions.body === "string" ? requestOptions.body : JSON.stringify(requestOptions.body);
}
const result = await readJsonResponse(await fetchImpl(path, init), "请求失败");
if (result.ok === false) throw new Error(result.error || "请求失败");
return result;
},
};
}
function helperBaseCandidates(startPort = 18766, count = 5) {
return Array.from({ length: count }, (_, index) => `http://127.0.0.1:${startPort + index}`);
}
function isKnownHelperHealth(result) {
const mode = result?.mode || "";
return Boolean(result?.ok && (mode === "local-helper" || mode === "native-helper" || mode === "codex-plus-helper"));
}
function createHelperClient(base, options = {}) {
const fetchImpl = options.fetchImpl || fetch;
const root = String(base || "").replace(/\/+$/, "");
if (!root) throw new Error("Dock Agent 未连接");
async function request(path, requestOptions = {}) {
const { allowAppError, body, headers, ...rest } = requestOptions;
const init = {
method: rest.method || "GET",
cache: rest.cache,
headers: jsonHeaders(body, headers),
};
if (body !== undefined) {
init.body = typeof body === "string" ? body : JSON.stringify(body);
}
const result = await readJsonResponse(await fetchImpl(`${root}${path}`, init), "Agent 请求失败");
if (result.ok === false && !allowAppError) throw new Error(result.error || "Agent 执行失败");
return result;
}
return {
base: root,
health() {
return request("/api/health", { cache: "no-store" });
},
repairTray() {
return request("/api/tray/repair", { method: "POST" });
},
diagnosticsExport() {
return request("/api/diagnostics/export", { cache: "no-store" });
},
updateCheck() {
return request("/api/update/check", { cache: "no-store" });
},
openUpdateDownload() {
return request("/api/update/open-download", { method: "POST" });
},
currentAuth() {
return request("/api/current-auth", { cache: "no-store" });
},
oauthCallbackLatest(state) {
const query = state ? `?state=${encodeURIComponent(state)}` : "";
return request(`/api/oauth/callback/latest${query}`, { cache: "no-store" });
},
pair(payload) {
return request("/api/pair", { method: "POST", body: payload });
},
configureProxy(action) {
return request(`/api/codex/proxy/${encodeURIComponent(action)}`, { method: "POST" });
},
codexStatus() {
return request("/api/codex/status", { cache: "no-store" });
},
restoreTarget() {
return request("/api/codex/restore-target", { cache: "no-store" });
},
configureAutoSwitch(config) {
return request("/api/auto-switch/configure", { method: "POST", body: config });
},
resumeAutoSwitch() {
return request("/api/auto-switch/resume", { method: "POST" });
},
applyAuth(payload) {
return request("/api/apply-auth", { method: "POST", body: payload });
},
previewUsage(payload) {
return request("/api/usage/preview", { method: "POST", body: payload, allowAppError: true });
},
migrateCacheUrl(targetOrigin) {
return `${root}/migrate-cache?target=${encodeURIComponent(targetOrigin)}`;
},
};
}
window.CodexPlatformClients = Object.freeze({
createCloudApiClient,
createHelperClient,
helperBaseCandidates,
isKnownHelperHealth,
});
})();