@hjd-hitsz/claudex-web 4.1.2 → 4.1.4
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/CHANGELOG.md +42 -0
- package/build-info.json +4 -4
- package/client/dist/_baseMerge.js +1 -1
- package/client/dist/app.js +89 -88
- package/client/dist/code-highlight-style.js +48 -13
- package/client/dist/dagre-NK4GIZVX.js +2 -2
- package/client/dist/document-editor.js +101 -136
- package/client/dist/file-panel.js +5 -1
- package/client/dist/graph.js +1 -1
- package/client/dist/index.js +2 -2
- package/client/dist/index10.js +1 -1
- package/client/dist/index11.js +1 -1
- package/client/dist/index12.js +1 -1
- package/client/dist/index13.js +2 -2
- package/client/dist/index14.js +1 -1
- package/client/dist/index15.js +1 -1
- package/client/dist/index16.js +2 -2
- package/client/dist/index17.js +1 -1
- package/client/dist/index18.js +2 -2
- package/client/dist/index19.js +1 -1
- package/client/dist/index20.js +1 -1
- package/client/dist/index21.js +1 -1
- package/client/dist/index22.js +1 -1
- package/client/dist/index3.js +1 -1
- package/client/dist/index4.js +1 -1
- package/client/dist/index5.js +1 -1
- package/client/dist/index6.js +2 -2
- package/client/dist/index7.js +1 -1
- package/client/dist/index8.js +1 -1
- package/client/dist/index9.js +1 -1
- package/client/dist/isArrayLikeObject.js +1 -1
- package/client/dist/style.css +1 -1
- package/docs/architecture/backend-regressions.md +2 -1
- package/docs/architecture/behavior.md +25 -7
- package/docs/architecture.md +20 -5
- package/docs/cli-command-coverage.md +1 -1
- package/docs/deployment.md +7 -3
- package/docs/development.md +25 -1
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/releases/v4.1.3.md +8 -0
- package/docs/releases/v4.1.4.md +7 -0
- package/package.json +1 -1
- package/packages/translation-engine/package.json +1 -1
- package/server/src/backends/claude/adapter.mjs +2 -0
- package/server/src/backends/claude/execution-session.mjs +55 -4
- package/server/src/backends/claude/native-provider.mjs +35 -0
- package/server/src/backends/claude/provider-environment.mjs +66 -11
- package/server/src/backends/claude/provider-transport.mjs +61 -9
- package/server/src/backends/claude/runtime.mjs +27 -5
- package/server/src/backends/claude/worker-client.mjs +14 -5
- package/server/src/backends/claude/worker-server.mjs +3 -1
- package/server/src/backends/codex/adapter.mjs +14 -4
- package/server/src/backends/codex/methods.mjs +0 -1
- package/server/src/backends/codex/requests.mjs +6 -2
- package/server/src/backends/codex/resume-lifecycle.mjs +38 -0
- package/server/src/backends/codex/subagent-model.mjs +19 -0
- package/server/src/backends/codex/upstream.mjs +9 -0
- package/server/src/backends/configuration.mjs +43 -0
- package/server/src/backends/manager.mjs +20 -2
- package/server/src/backends/subagent-settings.mjs +14 -0
- package/server/src/service-maintenance-process.mjs +69 -0
- package/server/src/service-maintenance-worker.mjs +60 -25
- package/server/src/service-maintenance.mjs +66 -27
- package/shared/backend-activity.mjs +2 -0
- package/shared/subagent-settings.mjs +18 -0
|
@@ -2,18 +2,33 @@ import { homedir } from "node:os";
|
|
|
2
2
|
import { join, resolve } from "node:path";
|
|
3
3
|
import { networkProxyEnvironment } from "../network-proxy.mjs";
|
|
4
4
|
|
|
5
|
+
export function subagentModelEnvironment(backend, proxy) {
|
|
6
|
+
return {
|
|
7
|
+
...(backend.subagentModel && {
|
|
8
|
+
CLAUDE_CODE_SUBAGENT_MODEL: proxy?.subagentModel || backend.subagentModel,
|
|
9
|
+
CLAUDE_CODE_SUBAGENT_MODEL_FORCE: "1",
|
|
10
|
+
}),
|
|
11
|
+
...(backend.subagentEffort && {
|
|
12
|
+
CLAUDE_CODE_SUBAGENT_EFFORT: backend.subagentEffort,
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
export function claudeRuntimeEnvironment(backend) {
|
|
6
|
-
const env = {
|
|
18
|
+
const env = {
|
|
19
|
+
...process.env,
|
|
20
|
+
...networkProxyEnvironment(backend.proxy),
|
|
21
|
+
...subagentModelEnvironment(backend),
|
|
22
|
+
};
|
|
7
23
|
const original = resolve(env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"));
|
|
8
24
|
if (resolve(backend.dataDir) !== original)
|
|
9
25
|
env.CLAUDE_CONFIG_DIR = backend.dataDir;
|
|
10
26
|
return env;
|
|
11
27
|
}
|
|
12
28
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
([name, metadata]) => {
|
|
29
|
+
function modelCapabilityRules(metadataByModel) {
|
|
30
|
+
return Object.entries(metadataByModel)
|
|
31
|
+
.flatMap(([name, metadata]) => {
|
|
17
32
|
const capabilities = [];
|
|
18
33
|
if (typeof metadata.supportsAdaptiveThinking === "boolean")
|
|
19
34
|
capabilities.push(
|
|
@@ -31,14 +46,20 @@ export function providerModelEnvironment(provider, model, effort) {
|
|
|
31
46
|
);
|
|
32
47
|
}
|
|
33
48
|
return capabilities.length ? [`${name}=${capabilities.join(",")}`] : [];
|
|
34
|
-
}
|
|
35
|
-
|
|
49
|
+
})
|
|
50
|
+
.join(";");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function providerModelEnvironment(provider, model, effort) {
|
|
54
|
+
const selectedModel = model || provider.models[0];
|
|
36
55
|
return {
|
|
37
56
|
ANTHROPIC_MODEL: selectedModel,
|
|
38
57
|
ANTHROPIC_DEFAULT_OPUS_MODEL: selectedModel,
|
|
39
58
|
ANTHROPIC_DEFAULT_SONNET_MODEL: selectedModel,
|
|
40
59
|
ANTHROPIC_DEFAULT_HAIKU_MODEL: selectedModel,
|
|
41
|
-
CLAUDE_CODE_MODEL_CAPABILITIES:
|
|
60
|
+
CLAUDE_CODE_MODEL_CAPABILITIES: modelCapabilityRules(
|
|
61
|
+
provider.modelCapabilities || {},
|
|
62
|
+
),
|
|
42
63
|
CLAUDE_CODE_MAX_CONTEXT_TOKENS: String(
|
|
43
64
|
provider.modelCapabilities?.[selectedModel]?.contextWindow || "",
|
|
44
65
|
),
|
|
@@ -55,7 +76,26 @@ export function providerRuntimeEnvironment(
|
|
|
55
76
|
) {
|
|
56
77
|
const env = claudeRuntimeEnvironment(backend);
|
|
57
78
|
delete env.CLAUDECODE;
|
|
58
|
-
if (
|
|
79
|
+
if (proxy) {
|
|
80
|
+
env.ANTHROPIC_BASE_URL = proxy.url;
|
|
81
|
+
env.ANTHROPIC_CUSTOM_HEADERS = [
|
|
82
|
+
env.ANTHROPIC_CUSTOM_HEADERS,
|
|
83
|
+
`x-claudex-proxy-token: ${proxy.token}`,
|
|
84
|
+
]
|
|
85
|
+
.filter(Boolean)
|
|
86
|
+
.join("\n");
|
|
87
|
+
Object.assign(env, subagentModelEnvironment(backend, proxy));
|
|
88
|
+
}
|
|
89
|
+
if (!provider) {
|
|
90
|
+
if (proxy?.subagentModel)
|
|
91
|
+
env.CLAUDE_CODE_MODEL_CAPABILITIES = [
|
|
92
|
+
env.CLAUDE_CODE_MODEL_CAPABILITIES,
|
|
93
|
+
modelCapabilityRules(proxy.modelCapabilities),
|
|
94
|
+
]
|
|
95
|
+
.filter(Boolean)
|
|
96
|
+
.join(";");
|
|
97
|
+
return env;
|
|
98
|
+
}
|
|
59
99
|
for (const key of [
|
|
60
100
|
"ANTHROPIC_AUTH_TOKEN",
|
|
61
101
|
"ANTHROPIC_API_KEY",
|
|
@@ -67,7 +107,17 @@ export function providerRuntimeEnvironment(
|
|
|
67
107
|
delete env[key];
|
|
68
108
|
Object.assign(
|
|
69
109
|
env,
|
|
70
|
-
providerModelEnvironment(
|
|
110
|
+
providerModelEnvironment(
|
|
111
|
+
{
|
|
112
|
+
...provider,
|
|
113
|
+
modelCapabilities: {
|
|
114
|
+
...provider.modelCapabilities,
|
|
115
|
+
...proxy?.modelCapabilities,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
model || provider.models[0],
|
|
119
|
+
effort,
|
|
120
|
+
),
|
|
71
121
|
);
|
|
72
122
|
env.ANTHROPIC_BASE_URL = proxy?.url || provider.baseUrl;
|
|
73
123
|
env.ANTHROPIC_API_KEY = proxy?.token || provider.apiKey;
|
|
@@ -78,12 +128,17 @@ export function providerRuntimeEnvironment(
|
|
|
78
128
|
export function providerSettingsEnvironment(provider, backend, options) {
|
|
79
129
|
const env = providerRuntimeEnvironment(provider, backend, options);
|
|
80
130
|
return {
|
|
81
|
-
|
|
131
|
+
CLAUDE_CODE_SUBAGENT_MODEL: "",
|
|
132
|
+
CLAUDE_CODE_SUBAGENT_MODEL_FORCE: "",
|
|
133
|
+
CLAUDE_CODE_SUBAGENT_EFFORT: "",
|
|
134
|
+
...(provider && { ANTHROPIC_AUTH_TOKEN: "" }),
|
|
82
135
|
...networkProxyEnvironment(backend.proxy),
|
|
83
136
|
...Object.fromEntries(
|
|
84
137
|
Object.entries(env).filter(
|
|
85
138
|
([key]) =>
|
|
86
139
|
key.startsWith("ANTHROPIC_") ||
|
|
140
|
+
key.startsWith("CLAUDE_CODE_SUBAGENT_MODEL") ||
|
|
141
|
+
key === "CLAUDE_CODE_SUBAGENT_EFFORT" ||
|
|
87
142
|
[
|
|
88
143
|
"CLAUDE_CODE_MODEL_CAPABILITIES",
|
|
89
144
|
"CLAUDE_CODE_MAX_CONTEXT_TOKENS",
|
|
@@ -4,11 +4,26 @@ import http from "node:http";
|
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
import { Readable } from "node:stream";
|
|
6
6
|
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import { nativeProviderHeaders } from "./native-provider.mjs";
|
|
7
8
|
|
|
8
|
-
export async function createProviderTransport(provider) {
|
|
9
|
+
export async function createProviderTransport(provider, subagent) {
|
|
9
10
|
const token = randomUUID();
|
|
11
|
+
const targets = new Map();
|
|
12
|
+
let subagentModel;
|
|
13
|
+
let targetKey;
|
|
14
|
+
function setSubagent(target) {
|
|
15
|
+
const key = JSON.stringify(target);
|
|
16
|
+
if (key === targetKey) return;
|
|
17
|
+
targetKey = key;
|
|
18
|
+
subagentModel = target ? `claudex-subagent-${randomUUID()}` : null;
|
|
19
|
+
if (target) targets.set(subagentModel, target);
|
|
20
|
+
}
|
|
21
|
+
setSubagent(subagent);
|
|
22
|
+
const nativeBaseUrl =
|
|
23
|
+
process.env.ANTHROPIC_BASE_URL || "https://api.anthropic.com";
|
|
10
24
|
const server = http.createServer(async (request, response) => {
|
|
11
25
|
if (
|
|
26
|
+
request.headers["x-claudex-proxy-token"] !== token &&
|
|
12
27
|
request.headers["x-api-key"] !== token &&
|
|
13
28
|
request.headers.authorization !== `Bearer ${token}`
|
|
14
29
|
) {
|
|
@@ -21,13 +36,36 @@ export async function createProviderTransport(provider) {
|
|
|
21
36
|
body += chunk;
|
|
22
37
|
if (body.length > 64 * 1024 * 1024) throw new Error("请求过大");
|
|
23
38
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
let selected = provider;
|
|
40
|
+
if (body) {
|
|
41
|
+
let input = JSON.parse(body);
|
|
42
|
+
const target = targets.get(input.model);
|
|
43
|
+
if (target) {
|
|
44
|
+
selected = target.provider;
|
|
45
|
+
input = { ...input, model: target.model };
|
|
46
|
+
if (target.effort && !request.url.includes("/count_tokens"))
|
|
47
|
+
input.output_config = {
|
|
48
|
+
...input.output_config,
|
|
49
|
+
effort: target.effort,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (selected && (selected.protocol || "messages") === "messages")
|
|
53
|
+
input = messagesRequest(input);
|
|
54
|
+
body = JSON.stringify(input);
|
|
55
|
+
}
|
|
56
|
+
const headers = { ...request.headers };
|
|
57
|
+
if (selected) {
|
|
58
|
+
delete headers["x-api-key"];
|
|
59
|
+
delete headers.authorization;
|
|
60
|
+
delete headers["anthropic-organization-id"];
|
|
61
|
+
if (selected.native)
|
|
62
|
+
Object.assign(headers, await nativeProviderHeaders(selected));
|
|
63
|
+
else {
|
|
64
|
+
headers["x-api-key"] = selected.apiKey;
|
|
65
|
+
headers.authorization = `Bearer ${selected.apiKey}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
delete headers["x-claudex-proxy-token"];
|
|
31
69
|
delete headers.host;
|
|
32
70
|
delete headers["content-length"];
|
|
33
71
|
delete headers.connection;
|
|
@@ -35,7 +73,9 @@ export async function createProviderTransport(provider) {
|
|
|
35
73
|
response.on("close", () => {
|
|
36
74
|
if (!response.writableEnded) abort.abort();
|
|
37
75
|
});
|
|
38
|
-
const target = new URL(
|
|
76
|
+
const target = new URL(
|
|
77
|
+
(selected?.baseUrl || nativeBaseUrl) + request.url,
|
|
78
|
+
);
|
|
39
79
|
const options = {
|
|
40
80
|
method: request.method,
|
|
41
81
|
headers,
|
|
@@ -62,6 +102,18 @@ export async function createProviderTransport(provider) {
|
|
|
62
102
|
return {
|
|
63
103
|
url: `http://127.0.0.1:${server.address().port}`,
|
|
64
104
|
token,
|
|
105
|
+
setSubagent,
|
|
106
|
+
get subagentModel() {
|
|
107
|
+
return subagentModel;
|
|
108
|
+
},
|
|
109
|
+
get modelCapabilities() {
|
|
110
|
+
return Object.fromEntries(
|
|
111
|
+
[...targets].map(([model, target]) => [
|
|
112
|
+
model,
|
|
113
|
+
target.provider.modelCapabilities?.[target.model] || {},
|
|
114
|
+
]),
|
|
115
|
+
);
|
|
116
|
+
},
|
|
65
117
|
close() {
|
|
66
118
|
server.close();
|
|
67
119
|
server.closeAllConnections();
|
|
@@ -5,12 +5,13 @@ import { mkdir } from "node:fs/promises";
|
|
|
5
5
|
import { executionError } from "./execution-errors.mjs";
|
|
6
6
|
import { inputMessage, convertMessage } from "./messages.mjs";
|
|
7
7
|
import { createExecutionSession } from "./execution-session.mjs";
|
|
8
|
+
import { nativeSubagentProvider } from "./native-provider.mjs";
|
|
8
9
|
import { createBackgroundTasks } from "./background-tasks.mjs";
|
|
9
10
|
import { createGoals } from "./goals.mjs";
|
|
10
11
|
import { modelOptions } from "./model-options.mjs";
|
|
11
|
-
import { providerModelEnvironment } from "./provider-environment.mjs";
|
|
12
12
|
import { recoverClaudeSessions } from "./recovery.mjs";
|
|
13
13
|
import { assertConversationOperation } from "../../bridge/conversation-policy.mjs";
|
|
14
|
+
import { subagentSettings } from "../../../../shared/subagent-settings.mjs";
|
|
14
15
|
|
|
15
16
|
export function createClaudeRuntime({
|
|
16
17
|
backend,
|
|
@@ -20,6 +21,7 @@ export function createClaudeRuntime({
|
|
|
20
21
|
compactions,
|
|
21
22
|
questions,
|
|
22
23
|
observeMessage,
|
|
24
|
+
readSubagentSettings = async () => subagentSettings(backend),
|
|
23
25
|
}) {
|
|
24
26
|
const running = new Map();
|
|
25
27
|
const queries = new Map();
|
|
@@ -208,6 +210,22 @@ export function createClaudeRuntime({
|
|
|
208
210
|
backend.id,
|
|
209
211
|
queued.providerId ?? session.providerId,
|
|
210
212
|
);
|
|
213
|
+
const settings = await readSubagentSettings();
|
|
214
|
+
const executionBackend = { ...backend, ...settings };
|
|
215
|
+
const subagentProvider =
|
|
216
|
+
settings.subagentProviderId === "@native"
|
|
217
|
+
? provider || settings.subagentEffort
|
|
218
|
+
? nativeSubagentProvider(backend)
|
|
219
|
+
: null
|
|
220
|
+
: settings.subagentProviderId
|
|
221
|
+
? await providers.get(backend.id, settings.subagentProviderId)
|
|
222
|
+
: null;
|
|
223
|
+
if (
|
|
224
|
+
subagentProvider &&
|
|
225
|
+
!subagentProvider.native &&
|
|
226
|
+
!subagentProvider.models.includes(settings.subagentModel)
|
|
227
|
+
)
|
|
228
|
+
throw new Error("子代理模型已从供应商移除,请重新配置");
|
|
211
229
|
const options = modelOptions(
|
|
212
230
|
provider,
|
|
213
231
|
queued.model || session.model || provider?.models[0],
|
|
@@ -233,10 +251,11 @@ export function createClaudeRuntime({
|
|
|
233
251
|
}
|
|
234
252
|
if (!process) {
|
|
235
253
|
process = await createExecutionSession({
|
|
236
|
-
backend,
|
|
254
|
+
backend: executionBackend,
|
|
237
255
|
session,
|
|
238
256
|
queued,
|
|
239
257
|
providers,
|
|
258
|
+
subagentProvider,
|
|
240
259
|
save,
|
|
241
260
|
emit,
|
|
242
261
|
broadcast,
|
|
@@ -257,6 +276,7 @@ export function createClaudeRuntime({
|
|
|
257
276
|
});
|
|
258
277
|
queries.set(threadId, process);
|
|
259
278
|
} else {
|
|
279
|
+
await process.configureSubagent(executionBackend, subagentProvider);
|
|
260
280
|
await process.query.setPermissionMode(
|
|
261
281
|
queued.mode === "plan" || queued.review
|
|
262
282
|
? "plan"
|
|
@@ -270,14 +290,16 @@ export function createClaudeRuntime({
|
|
|
270
290
|
await process.query.applyFlagSettings({
|
|
271
291
|
effortLevel: queued.effort || null,
|
|
272
292
|
fastMode: options.settings.fastMode,
|
|
273
|
-
...(provider
|
|
274
|
-
|
|
275
|
-
|
|
293
|
+
...((provider ||
|
|
294
|
+
subagentProvider ||
|
|
295
|
+
process.subagentSettingsDirty) && {
|
|
296
|
+
env: process.settingsEnvironment(
|
|
276
297
|
model || process.model,
|
|
277
298
|
queued.effort,
|
|
278
299
|
),
|
|
279
300
|
}),
|
|
280
301
|
});
|
|
302
|
+
process.subagentSettingsDirty = false;
|
|
281
303
|
process.effort = queued.effort;
|
|
282
304
|
}
|
|
283
305
|
active.query = process.query;
|
|
@@ -8,6 +8,7 @@ import { hasBackendWork } from "../../../../shared/backend-activity.mjs";
|
|
|
8
8
|
import { launchWorker, workerLocation } from "./worker-launcher.mjs";
|
|
9
9
|
import { workerRevision } from "./worker-revision.mjs";
|
|
10
10
|
import { networkProxyEnvironment } from "../network-proxy.mjs";
|
|
11
|
+
import { executionBackend } from "../../../../shared/subagent-settings.mjs";
|
|
11
12
|
import {
|
|
12
13
|
captureNativeProcesses,
|
|
13
14
|
nativeProcessesAlive,
|
|
@@ -35,6 +36,8 @@ export function createClaudeWorkerAdapter({
|
|
|
35
36
|
"ANTHROPIC_API_KEY",
|
|
36
37
|
"ANTHROPIC_BASE_URL",
|
|
37
38
|
"ANTHROPIC_MODEL",
|
|
39
|
+
"ANTHROPIC_CUSTOM_HEADERS",
|
|
40
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
38
41
|
"CLAUDE_CODE_EFFORT_LEVEL",
|
|
39
42
|
"CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS",
|
|
40
43
|
"HTTP_PROXY",
|
|
@@ -214,11 +217,17 @@ export function createClaudeWorkerAdapter({
|
|
|
214
217
|
initialized.revision?.entry !== options.revision.entry ||
|
|
215
218
|
initialized.revision?.hash !== options.revision.hash ||
|
|
216
219
|
(initialized.configuration &&
|
|
217
|
-
!isDeepStrictEqual(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
220
|
+
!isDeepStrictEqual(
|
|
221
|
+
{
|
|
222
|
+
...initialized.configuration,
|
|
223
|
+
backend: executionBackend(initialized.configuration.backend),
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
backend: executionBackend(options.backend),
|
|
227
|
+
config: options.config,
|
|
228
|
+
runtimeEnv: options.runtimeEnv,
|
|
229
|
+
},
|
|
230
|
+
));
|
|
222
231
|
mayUpgrade =
|
|
223
232
|
!initialized.generation || options.generation > initialized.generation;
|
|
224
233
|
publish();
|
|
@@ -112,10 +112,12 @@ async function initialize(value) {
|
|
|
112
112
|
const temporary = `${optionsPath}.${process.pid}.tmp`;
|
|
113
113
|
await writeFile(temporary, JSON.stringify(options), { mode: 0o600 });
|
|
114
114
|
await rename(temporary, optionsPath);
|
|
115
|
-
const
|
|
115
|
+
const configuration = createBackendConfiguration(value.config);
|
|
116
|
+
const providers = createProviders(configuration);
|
|
116
117
|
adapter = createClaudeAdapter({
|
|
117
118
|
...value,
|
|
118
119
|
providers,
|
|
120
|
+
readSubagentSettings: () => configuration.readSubagentSettings("claude"),
|
|
119
121
|
send: (client, message) => client.deliver(message),
|
|
120
122
|
broadcast,
|
|
121
123
|
workspace: { handles: () => false, snapshot: () => ({}) },
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { referenceToolParams } from "./reference-tools.mjs";
|
|
2
|
+
import { subagentModelParams } from "./subagent-model.mjs";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { codexCapabilities } from "../../../../shared/backend-capabilities.mjs";
|
|
4
5
|
import { readAgentMessages } from "../../agent-messages.mjs";
|
|
@@ -18,6 +19,7 @@ export function createCodexAdapter({
|
|
|
18
19
|
version,
|
|
19
20
|
send,
|
|
20
21
|
broadcast,
|
|
22
|
+
readSubagentSettings = async () => backend,
|
|
21
23
|
}) {
|
|
22
24
|
config = { ...config, upstreamUrl: backend.url, codexHome: backend.dataDir };
|
|
23
25
|
const questions = new Map();
|
|
@@ -50,8 +52,16 @@ export function createCodexAdapter({
|
|
|
50
52
|
},
|
|
51
53
|
});
|
|
52
54
|
const nativeCall = transport.call;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
const sessionParams = async (method, params) =>
|
|
56
|
+
subagentModelParams(
|
|
57
|
+
method,
|
|
58
|
+
referenceToolParams(method, params, config),
|
|
59
|
+
["thread/start", "thread/resume", "thread/fork"].includes(method)
|
|
60
|
+
? await readSubagentSettings()
|
|
61
|
+
: backend,
|
|
62
|
+
);
|
|
63
|
+
transport.call = async (method, params, options) =>
|
|
64
|
+
nativeCall(method, await sessionParams(method, params), options);
|
|
55
65
|
const threadNaming = createThreadNaming({
|
|
56
66
|
call: transport.call,
|
|
57
67
|
generate: (params) =>
|
|
@@ -131,13 +141,13 @@ export function createCodexAdapter({
|
|
|
131
141
|
readTokenUsage: (thread) => readCodexTokenUsage(thread, transport.call),
|
|
132
142
|
workspaceOwner: "backend",
|
|
133
143
|
state,
|
|
134
|
-
receive(client, message) {
|
|
144
|
+
async receive(client, message) {
|
|
135
145
|
if (!message.method) return receive(client, message);
|
|
136
146
|
const { providerId, ...params } = message.params || {};
|
|
137
147
|
if (providerId) throw new Error("Codex 使用现有账户与模型配置");
|
|
138
148
|
return receive(client, {
|
|
139
149
|
...message,
|
|
140
|
-
params:
|
|
150
|
+
params: await sessionParams(message.method, params),
|
|
141
151
|
});
|
|
142
152
|
},
|
|
143
153
|
connect: transport.connect,
|
|
@@ -109,8 +109,9 @@ export function createRequestHandler({
|
|
|
109
109
|
const timer = processId
|
|
110
110
|
? null
|
|
111
111
|
: setTimeout(() => {
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
const request = transport.pending.get(id);
|
|
113
|
+
if (threadMethods.has(message.method) && !request?.waitingForClose)
|
|
114
|
+
request.timedOut = true;
|
|
114
115
|
else {
|
|
115
116
|
transport.pending.delete(id);
|
|
116
117
|
releaseUnusedThreads();
|
|
@@ -130,6 +131,9 @@ export function createRequestHandler({
|
|
|
130
131
|
processId,
|
|
131
132
|
method: message.method,
|
|
132
133
|
threadId,
|
|
134
|
+
...(message.method === "thread/resume" && {
|
|
135
|
+
resumeParams: message.params,
|
|
136
|
+
}),
|
|
133
137
|
...(message.method === "turn/start" && {
|
|
134
138
|
nameText: (message.params?.input || [])
|
|
135
139
|
.filter((item) => item.type === "text")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Codex 0.169 的取消订阅响应先于实际关闭,恢复请求可在这段间隔被拒绝。
|
|
2
|
+
// 只在原生明确报告目标线程正在关闭时等待其关闭事件,沿用原请求期限。
|
|
3
|
+
export function createResumeLifecycle({ pending, send }) {
|
|
4
|
+
function resume(id, request) {
|
|
5
|
+
request.waitingForClose = false;
|
|
6
|
+
request.closedSinceResume = false;
|
|
7
|
+
send({ id, method: "thread/resume", params: request.resumeParams });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
defer(id, request, error) {
|
|
12
|
+
const threadId = request.resumeParams?.threadId;
|
|
13
|
+
if (
|
|
14
|
+
request.method !== "thread/resume" ||
|
|
15
|
+
!threadId ||
|
|
16
|
+
request.timedOut ||
|
|
17
|
+
request.resumeRetried ||
|
|
18
|
+
error?.code !== -32600 ||
|
|
19
|
+
![
|
|
20
|
+
`thread ${threadId} is closing; retry thread/resume after the thread is closed`,
|
|
21
|
+
`thread ${threadId} is closing; retry after the thread is closed`,
|
|
22
|
+
].includes(error.message)
|
|
23
|
+
)
|
|
24
|
+
return false;
|
|
25
|
+
request.resumeRetried = true;
|
|
26
|
+
request.waitingForClose = true;
|
|
27
|
+
if (request.closedSinceResume) resume(id, request);
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
closed(threadId) {
|
|
31
|
+
for (const [id, request] of pending) {
|
|
32
|
+
if (request.resumeParams?.threadId !== threadId) continue;
|
|
33
|
+
request.closedSinceResume = true;
|
|
34
|
+
if (request.waitingForClose) resume(id, request);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function subagentModelParams(method, params, backend) {
|
|
2
|
+
if (
|
|
3
|
+
(!backend.subagentModel && !backend.subagentEffort) ||
|
|
4
|
+
!["thread/start", "thread/resume", "thread/fork"].includes(method)
|
|
5
|
+
)
|
|
6
|
+
return params;
|
|
7
|
+
return {
|
|
8
|
+
...params,
|
|
9
|
+
config: {
|
|
10
|
+
...params.config,
|
|
11
|
+
...(backend.subagentModel && {
|
|
12
|
+
"agents.default_subagent_model": backend.subagentModel,
|
|
13
|
+
}),
|
|
14
|
+
...(backend.subagentEffort && {
|
|
15
|
+
"agents.default_subagent_reasoning_effort": backend.subagentEffort,
|
|
16
|
+
}),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import WebSocket from "ws";
|
|
2
2
|
import { sendRpcText } from "../../rpc-transport.mjs";
|
|
3
|
+
import { createResumeLifecycle } from "./resume-lifecycle.mjs";
|
|
3
4
|
|
|
4
5
|
export function createUpstream({
|
|
5
6
|
config,
|
|
@@ -18,6 +19,10 @@ export function createUpstream({
|
|
|
18
19
|
let reconnectTimer;
|
|
19
20
|
let closed = false;
|
|
20
21
|
const nextId = () => sequence++;
|
|
22
|
+
const resumption = createResumeLifecycle({
|
|
23
|
+
pending,
|
|
24
|
+
send: (message) => sendRpcText(socket, JSON.stringify(message)),
|
|
25
|
+
});
|
|
21
26
|
|
|
22
27
|
function rejectPending(message) {
|
|
23
28
|
for (const request of pending.values()) {
|
|
@@ -48,6 +53,7 @@ export function createUpstream({
|
|
|
48
53
|
reject,
|
|
49
54
|
timer,
|
|
50
55
|
method,
|
|
56
|
+
...(method === "thread/resume" && { resumeParams: params }),
|
|
51
57
|
...(trackThread && { threadId: params.threadId }),
|
|
52
58
|
});
|
|
53
59
|
sendRpcText(socket, JSON.stringify({ id, method, params }));
|
|
@@ -91,10 +97,13 @@ export function createUpstream({
|
|
|
91
97
|
}
|
|
92
98
|
if (message.method) {
|
|
93
99
|
onEvent(message);
|
|
100
|
+
if (message.method === "thread/closed")
|
|
101
|
+
resumption.closed(message.params.threadId);
|
|
94
102
|
return;
|
|
95
103
|
}
|
|
96
104
|
const request = pending.get(message.id);
|
|
97
105
|
if (!request) return;
|
|
106
|
+
if (resumption.defer(message.id, request, message.error)) return;
|
|
98
107
|
clearTimeout(request.timer);
|
|
99
108
|
pending.delete(message.id);
|
|
100
109
|
if (request.resolve) {
|
|
@@ -1,15 +1,53 @@
|
|
|
1
1
|
import { resolve, join } from "node:path";
|
|
2
2
|
import { createStorage } from "./storage.mjs";
|
|
3
3
|
import { validateNetworkProxy } from "./network-proxy.mjs";
|
|
4
|
+
import { subagentSettings } from "../../../shared/subagent-settings.mjs";
|
|
4
5
|
|
|
5
6
|
export function validateBackend(input, config) {
|
|
6
7
|
if (!["codex", "claude"].includes(input.id) || input.id !== input.type)
|
|
7
8
|
throw new Error("仅支持 Codex 与 Claude Code 两个固定后端");
|
|
9
|
+
if (
|
|
10
|
+
input.subagentModel !== undefined &&
|
|
11
|
+
(typeof input.subagentModel !== "string" ||
|
|
12
|
+
/[\s\x00-\x1f\x7f]/u.test(input.subagentModel.trim()) ||
|
|
13
|
+
input.subagentModel.length > 256)
|
|
14
|
+
)
|
|
15
|
+
throw new Error("子代理模型须为不含空白的模型 ID,留空使用原生默认");
|
|
16
|
+
if (
|
|
17
|
+
input.subagentProviderId !== undefined &&
|
|
18
|
+
(typeof input.subagentProviderId !== "string" ||
|
|
19
|
+
(input.subagentProviderId !== "" &&
|
|
20
|
+
input.subagentProviderId !== "@native" &&
|
|
21
|
+
!/^[a-zA-Z0-9_-]{1,80}$/.test(input.subagentProviderId)))
|
|
22
|
+
)
|
|
23
|
+
throw new Error("子代理供应商标识无效");
|
|
24
|
+
if (input.subagentProviderId && input.type !== "claude")
|
|
25
|
+
throw new Error("子代理跨供应商配置仅用于 Claude Code");
|
|
26
|
+
if (input.subagentProviderId && !input.subagentModel?.trim())
|
|
27
|
+
throw new Error("选择子代理供应商后须指定模型");
|
|
28
|
+
if (
|
|
29
|
+
input.subagentEffort !== undefined &&
|
|
30
|
+
![
|
|
31
|
+
"",
|
|
32
|
+
"low",
|
|
33
|
+
"medium",
|
|
34
|
+
"high",
|
|
35
|
+
"xhigh",
|
|
36
|
+
"max",
|
|
37
|
+
...(input.type === "codex" ? ["none", "minimal", "ultra"] : []),
|
|
38
|
+
].includes(input.subagentEffort)
|
|
39
|
+
)
|
|
40
|
+
throw new Error("子代理思考强度无效");
|
|
8
41
|
const backend = {
|
|
9
42
|
id: input.id,
|
|
10
43
|
name: input.type === "codex" ? "Codex" : "Claude Code",
|
|
11
44
|
type: input.type,
|
|
12
45
|
enabled: input.enabled !== false,
|
|
46
|
+
subagentModel: input.subagentModel?.trim() || "",
|
|
47
|
+
subagentEffort: input.subagentEffort || "",
|
|
48
|
+
...(input.type === "claude" && {
|
|
49
|
+
subagentProviderId: input.subagentProviderId || "",
|
|
50
|
+
}),
|
|
13
51
|
dataDir: resolve(
|
|
14
52
|
input.type === "codex" ? config.codexHome : config.claudeHome,
|
|
15
53
|
),
|
|
@@ -115,6 +153,11 @@ export function createBackendConfiguration(config) {
|
|
|
115
153
|
...storage,
|
|
116
154
|
read,
|
|
117
155
|
save,
|
|
156
|
+
async readSubagentSettings(id) {
|
|
157
|
+
return subagentSettings(
|
|
158
|
+
(await storage.read()).backends.find((b) => b.id === id) || {},
|
|
159
|
+
);
|
|
160
|
+
},
|
|
118
161
|
async readProviders() {
|
|
119
162
|
return (await storage.read()).providers;
|
|
120
163
|
},
|