@hjd-hitsz/claudex-web 0.0.0-stage → 4.0.1
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/.env.example +27 -0
- package/CHANGELOG.md +449 -0
- package/LICENSE +674 -0
- package/README.md +128 -2
- package/bin/claudex-web.mjs +129 -0
- package/build-info.json +8 -0
- package/client/dist/_baseMerge.js +1 -0
- package/client/dist/abnfDiagram-WEYOIA3C.js +1 -0
- package/client/dist/apl.js +1 -0
- package/client/dist/app.js +393 -0
- package/client/dist/apple-touch-icon.png +0 -0
- package/client/dist/arc.js +1 -0
- package/client/dist/architectureDiagram-ZHVYKT53.js +36 -0
- package/client/dist/asciiarmor.js +1 -0
- package/client/dist/asn1.js +1 -0
- package/client/dist/asterisk.js +1 -0
- package/client/dist/blockDiagram-RJAE7QA3.js +140 -0
- package/client/dist/brainfuck.js +1 -0
- package/client/dist/c4Diagram-XDPPPE54.js +38 -0
- package/client/dist/channel.js +1 -0
- package/client/dist/chunk-2E7WHVMD.js +88 -0
- package/client/dist/chunk-2Q5K7J3B.js +1 -0
- package/client/dist/chunk-4EMNKR2T.js +1 -0
- package/client/dist/chunk-55XCU76K.js +1 -0
- package/client/dist/chunk-5VM5RSS4.js +15 -0
- package/client/dist/chunk-JWPE2WC7.js +1 -0
- package/client/dist/chunk-VQ5W62QI.js +1 -0
- package/client/dist/chunk-XXDRQBXY.js +1 -0
- package/client/dist/chunk-ZCHMQSOR.js +2 -0
- package/client/dist/classDiagram-v2-4MYWR3UA.js +217 -0
- package/client/dist/clike.js +1 -0
- package/client/dist/clojure.js +1 -0
- package/client/dist/cmake.js +1 -0
- package/client/dist/cobol.js +1 -0
- package/client/dist/code-highlight-style.js +13 -0
- package/client/dist/coffeescript.js +1 -0
- package/client/dist/commonlisp.js +1 -0
- package/client/dist/cose-bilkent-JH36ORCC.js +1 -0
- package/client/dist/crystal.js +1 -0
- package/client/dist/css.js +1 -0
- package/client/dist/cynefin-EF2NZ3EQ.js +141 -0
- package/client/dist/cynefinDiagram-WF24UDRP.js +62 -0
- package/client/dist/cypher.js +1 -0
- package/client/dist/cytoscape.esm.js +321 -0
- package/client/dist/d.js +1 -0
- package/client/dist/dagre-NK4GIZVX.js +4 -0
- package/client/dist/defaultLocale.js +1 -0
- package/client/dist/diagram-CBK3MWTC.js +24 -0
- package/client/dist/diagram-I5XIIV2D.js +24 -0
- package/client/dist/diagram-ODIL2UCW.js +30 -0
- package/client/dist/diagram-PFBEJUXE.js +3 -0
- package/client/dist/diagram-RJA3I4VV.js +41 -0
- package/client/dist/diagram-TYUNFSNR.js +200 -0
- package/client/dist/diff.js +1 -0
- package/client/dist/dockerfile.js +1 -0
- package/client/dist/document-editor.js +1040 -0
- package/client/dist/dtd.js +1 -0
- package/client/dist/dylan.js +1 -0
- package/client/dist/ebnf.js +1 -0
- package/client/dist/ebnfDiagram-V4P3TTPT.js +1 -0
- package/client/dist/ecl.js +1 -0
- package/client/dist/eiffel.js +1 -0
- package/client/dist/elk-QONTBCTQ.js +27 -0
- package/client/dist/elm.js +1 -0
- package/client/dist/erDiagram-KYQIGQAA.js +99 -0
- package/client/dist/erlang.js +1 -0
- package/client/dist/factor.js +1 -0
- package/client/dist/favicon.ico +0 -0
- package/client/dist/favicon.png +0 -0
- package/client/dist/fcl.js +1 -0
- package/client/dist/file-panel.js +1 -0
- package/client/dist/flowDiagram-IUGLLWBU.js +213 -0
- package/client/dist/forth.js +1 -0
- package/client/dist/fortran.js +1 -0
- package/client/dist/ganttDiagram-R7GPNTJY.js +292 -0
- package/client/dist/gas.js +1 -0
- package/client/dist/gherkin.js +1 -0
- package/client/dist/gitGraphDiagram-PDYL7XHY.js +106 -0
- package/client/dist/graph.js +1 -0
- package/client/dist/groovy.js +1 -0
- package/client/dist/haskell.js +1 -0
- package/client/dist/haxe.js +1 -0
- package/client/dist/http.js +1 -0
- package/client/dist/idl.js +1 -0
- package/client/dist/index.html +25 -0
- package/client/dist/index.js +2 -0
- package/client/dist/index10.js +1 -0
- package/client/dist/index11.js +1 -0
- package/client/dist/index12.js +1 -0
- package/client/dist/index13.js +7 -0
- package/client/dist/index14.js +1 -0
- package/client/dist/index15.js +1 -0
- package/client/dist/index16.js +6 -0
- package/client/dist/index17.js +1 -0
- package/client/dist/index18.js +3 -0
- package/client/dist/index19.js +1 -0
- package/client/dist/index2.js +1 -0
- package/client/dist/index20.js +1 -0
- package/client/dist/index21.js +1 -0
- package/client/dist/index22.js +1 -0
- package/client/dist/index3.js +1 -0
- package/client/dist/index4.js +1 -0
- package/client/dist/index5.js +1 -0
- package/client/dist/index6.js +13 -0
- package/client/dist/index7.js +1 -0
- package/client/dist/index8.js +1 -0
- package/client/dist/index9.js +1 -0
- package/client/dist/infoDiagram-MVDRKAX5.js +2 -0
- package/client/dist/init.js +1 -0
- package/client/dist/isArrayLikeObject.js +1 -0
- package/client/dist/ishikawaDiagram-HPEHV2GE.js +70 -0
- package/client/dist/javascript.js +1 -0
- package/client/dist/journeyDiagram-BOSHW7O7.js +139 -0
- package/client/dist/julia.js +1 -0
- package/client/dist/kanban-definition-FNSA4HC5.js +89 -0
- package/client/dist/katex.js +257 -0
- package/client/dist/linear.js +1 -0
- package/client/dist/livescript.js +1 -0
- package/client/dist/lua.js +1 -0
- package/client/dist/mathematica.js +1 -0
- package/client/dist/mbox.js +1 -0
- package/client/dist/mermaid.core.js +310 -0
- package/client/dist/mindmap-definition-6OY57UG3.js +98 -0
- package/client/dist/mirc.js +1 -0
- package/client/dist/mllike.js +1 -0
- package/client/dist/modelica.js +1 -0
- package/client/dist/mscgen.js +1 -0
- package/client/dist/mumps.js +1 -0
- package/client/dist/nginx.js +1 -0
- package/client/dist/notification-worker.js +45 -0
- package/client/dist/nsis.js +1 -0
- package/client/dist/ntriples.js +1 -0
- package/client/dist/octave.js +1 -0
- package/client/dist/ordinal.js +1 -0
- package/client/dist/oz.js +1 -0
- package/client/dist/pascal.js +1 -0
- package/client/dist/pegDiagram-ECIUJNBX.js +1 -0
- package/client/dist/perl.js +1 -0
- package/client/dist/pieDiagram-GNMPY74T.js +39 -0
- package/client/dist/pig.js +1 -0
- package/client/dist/powershell.js +1 -0
- package/client/dist/properties.js +1 -0
- package/client/dist/protobuf.js +1 -0
- package/client/dist/pug.js +1 -0
- package/client/dist/puppet.js +1 -0
- package/client/dist/purify.es.js +3 -0
- package/client/dist/python.js +1 -0
- package/client/dist/q.js +1 -0
- package/client/dist/quadrantDiagram-D57K4MC3.js +7 -0
- package/client/dist/r.js +1 -0
- package/client/dist/railroadDiagram-Y72NRBL7.js +1 -0
- package/client/dist/requirementDiagram-EQYSW5RA.js +84 -0
- package/client/dist/rpm.js +1 -0
- package/client/dist/ruby.js +1 -0
- package/client/dist/sankeyDiagram-SY24JHO5.js +40 -0
- package/client/dist/sas.js +1 -0
- package/client/dist/scheme.js +1 -0
- package/client/dist/sequenceDiagram-BLEHNHQU.js +169 -0
- package/client/dist/shell.js +1 -0
- package/client/dist/sieve.js +1 -0
- package/client/dist/simple-mode.js +1 -0
- package/client/dist/sizeCapture-INFHLROL.js +1 -0
- package/client/dist/smalltalk.js +1 -0
- package/client/dist/solr.js +1 -0
- package/client/dist/sparql.js +1 -0
- package/client/dist/spreadsheet.js +1 -0
- package/client/dist/sql.js +1 -0
- package/client/dist/stateDiagram-v2-A6IESKWZ.js +291 -0
- package/client/dist/stex.js +1 -0
- package/client/dist/style.css +1 -0
- package/client/dist/stylus.js +1 -0
- package/client/dist/swift.js +1 -0
- package/client/dist/swimlanes-B3ENRKFQ.js +1 -0
- package/client/dist/swimlanesDiagram-AYQRV4AK.js +8 -0
- package/client/dist/tcl.js +1 -0
- package/client/dist/terminal-panel.js +18 -0
- package/client/dist/textile.js +1 -0
- package/client/dist/tiddlywiki.js +1 -0
- package/client/dist/tiki.js +1 -0
- package/client/dist/timeline-definition-JXYKGLGB.js +120 -0
- package/client/dist/toml.js +1 -0
- package/client/dist/troff.js +1 -0
- package/client/dist/ttcn-cfg.js +1 -0
- package/client/dist/ttcn.js +1 -0
- package/client/dist/turtle.js +1 -0
- package/client/dist/usecaseDiagram-WPOLBNNX.js +370 -0
- package/client/dist/vb.js +1 -0
- package/client/dist/vbscript.js +1 -0
- package/client/dist/velocity.js +1 -0
- package/client/dist/vennDiagram-Q3Y3IDCW.js +34 -0
- package/client/dist/verilog.js +1 -0
- package/client/dist/vhdl.js +1 -0
- package/client/dist/wardleyDiagram-IVAIDKWA.js +78 -0
- package/client/dist/webidl.js +1 -0
- package/client/dist/xquery.js +1 -0
- package/client/dist/xychartDiagram-4URNHDU4.js +7 -0
- package/client/dist/yacas.js +1 -0
- package/client/dist/z80.js +1 -0
- package/docs/architecture/backend-regressions.md +53 -0
- package/docs/architecture/backends.md +113 -0
- package/docs/architecture/behavior.md +206 -0
- package/docs/architecture.md +306 -0
- package/docs/background-tasks.md +39 -0
- package/docs/cli-command-coverage.md +60 -0
- package/docs/deployment.md +269 -0
- package/docs/development.md +385 -0
- package/docs/images/desktop-dark.png +0 -0
- package/docs/images/desktop-light.png +0 -0
- package/docs/images/mobile-sidebar.png +0 -0
- package/docs/images/mobile.png +0 -0
- package/docs/releases/v2.0.0.md +5 -0
- package/docs/releases/v2.1.0.md +6 -0
- package/docs/releases/v2.1.1.md +6 -0
- package/docs/releases/v2.1.2.md +8 -0
- package/docs/releases/v2.2.0.md +11 -0
- package/docs/releases/v2.3.0.md +12 -0
- package/docs/releases/v2.3.1.md +8 -0
- package/docs/releases/v2.4.0.md +8 -0
- package/docs/releases/v2.4.1.md +8 -0
- package/docs/releases/v2.4.2.md +8 -0
- package/docs/releases/v3.0.0.md +9 -0
- package/docs/releases/v3.1.0.md +10 -0
- package/docs/releases/v3.1.1.md +7 -0
- package/docs/releases/v4.0.0-beta.1.md +10 -0
- package/docs/releases/v4.0.0.md +20 -0
- package/docs/releases/v4.0.1.md +10 -0
- package/docs/versioning.md +119 -0
- package/package.json +70 -4
- package/scripts/install-services.mjs +134 -0
- package/scripts/restart-when-idle.mjs +125 -0
- package/scripts/start-backend.mjs +29 -0
- package/server/src/agent-messages.mjs +124 -0
- package/server/src/application.mjs +133 -0
- package/server/src/auth.mjs +328 -0
- package/server/src/backends/claude/account.mjs +171 -0
- package/server/src/backends/claude/adapter.mjs +366 -0
- package/server/src/backends/claude/agent-history.mjs +70 -0
- package/server/src/backends/claude/background-tasks.mjs +168 -0
- package/server/src/backends/claude/catalogue.mjs +266 -0
- package/server/src/backends/claude/execution-errors.mjs +44 -0
- package/server/src/backends/claude/execution-session.mjs +390 -0
- package/server/src/backends/claude/git-tool-changes.mjs +77 -0
- package/server/src/backends/claude/goals.mjs +319 -0
- package/server/src/backends/claude/history-actions.mjs +170 -0
- package/server/src/backends/claude/history.mjs +65 -0
- package/server/src/backends/claude/hooks.mjs +232 -0
- package/server/src/backends/claude/input-stream.mjs +24 -0
- package/server/src/backends/claude/messages-request.mjs +34 -0
- package/server/src/backends/claude/messages.mjs +300 -0
- package/server/src/backends/claude/model-options.mjs +18 -0
- package/server/src/backends/claude/native-commands.mjs +48 -0
- package/server/src/backends/claude/native-records.mjs +76 -0
- package/server/src/backends/claude/process-ownership.mjs +82 -0
- package/server/src/backends/claude/project-instructions.mjs +40 -0
- package/server/src/backends/claude/provider-balance.mjs +40 -0
- package/server/src/backends/claude/provider-environment.mjs +93 -0
- package/server/src/backends/claude/provider-transport.mjs +70 -0
- package/server/src/backends/claude/recovery.mjs +54 -0
- package/server/src/backends/claude/runtime.mjs +463 -0
- package/server/src/backends/claude/session-worker.mjs +32 -0
- package/server/src/backends/claude/sessions.mjs +347 -0
- package/server/src/backends/claude/token-usage.mjs +98 -0
- package/server/src/backends/claude/worker-client.mjs +344 -0
- package/server/src/backends/claude/worker-launcher.mjs +119 -0
- package/server/src/backends/claude/worker-location.mjs +31 -0
- package/server/src/backends/claude/worker-revision.mjs +45 -0
- package/server/src/backends/claude/worker-server.mjs +290 -0
- package/server/src/backends/cli-installation.mjs +140 -0
- package/server/src/backends/codex/adapter.mjs +153 -0
- package/server/src/backends/codex/compaction-recovery.mjs +78 -0
- package/server/src/backends/codex/events.mjs +125 -0
- package/server/src/backends/codex/methods.mjs +17 -0
- package/server/src/backends/codex/reference-mcp.mjs +97 -0
- package/server/src/backends/codex/reference-tools.mjs +71 -0
- package/server/src/backends/codex/requests.mjs +170 -0
- package/server/src/backends/codex/subscriptions.mjs +91 -0
- package/server/src/backends/codex/upstream.mjs +148 -0
- package/server/src/backends/configuration.mjs +122 -0
- package/server/src/backends/identity-mapping.mjs +87 -0
- package/server/src/backends/manager.mjs +412 -0
- package/server/src/backends/network-proxy.mjs +55 -0
- package/server/src/backends/provider-presets.mjs +45 -0
- package/server/src/backends/providers.mjs +132 -0
- package/server/src/backends/storage.mjs +35 -0
- package/server/src/bridge/conversation-policy.mjs +29 -0
- package/server/src/bridge/file-operations.mjs +47 -0
- package/server/src/bridge/product-methods.mjs +66 -0
- package/server/src/compaction.mjs +82 -0
- package/server/src/config.mjs +57 -0
- package/server/src/file-content.mjs +99 -0
- package/server/src/file-undo.mjs +142 -0
- package/server/src/http/router.mjs +80 -0
- package/server/src/http/static.mjs +62 -0
- package/server/src/product-config.mjs +80 -0
- package/server/src/project-models.mjs +59 -0
- package/server/src/rpc-transport.mjs +21 -0
- package/server/src/server.mjs +20 -0
- package/server/src/summary-translation.mjs +82 -0
- package/server/src/system-auth.mjs +45 -0
- package/server/src/thread-folders.mjs +76 -0
- package/server/src/thread-names.mjs +264 -0
- package/server/src/thread-projects.mjs +36 -0
- package/server/src/tls.mjs +87 -0
- package/server/src/version.mjs +55 -0
- package/server/src/web-config-migration.mjs +48 -0
- package/server/src/workspace/commands.mjs +33 -0
- package/server/src/workspace/files.mjs +44 -0
- package/server/src/workspace/service.mjs +27 -0
- package/server/src/workspace/terminals.mjs +122 -0
- package/shared/backend-activity.mjs +13 -0
- package/shared/backend-capabilities.mjs +33 -0
- package/shared/backend-identity.mjs +17 -0
- package/shared/compaction-state.mjs +49 -0
- package/shared/file-types.mjs +82 -0
- package/shared/product-contract.mjs +26 -0
- package/shared/rpc-limits.mjs +2 -0
- package/shared/terminal-workspace.mjs +26 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import { createThreadFolders } from "../thread-folders.mjs";
|
|
2
|
+
import { createCliInstallation } from "./cli-installation.mjs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { readProjectlessDirectories } from "../thread-projects.mjs";
|
|
5
|
+
import { createWorkspace } from "../workspace/service.mjs";
|
|
6
|
+
import { hasBackendWork } from "../../../shared/backend-activity.mjs";
|
|
7
|
+
import {
|
|
8
|
+
resourceTarget,
|
|
9
|
+
resourceId,
|
|
10
|
+
} from "../../../shared/backend-identity.mjs";
|
|
11
|
+
import {
|
|
12
|
+
createBackendConfiguration,
|
|
13
|
+
validateBackend,
|
|
14
|
+
} from "./configuration.mjs";
|
|
15
|
+
import { createProviders } from "./providers.mjs";
|
|
16
|
+
import { createCodexAdapter } from "./codex/adapter.mjs";
|
|
17
|
+
import { createClaudeWorkerAdapter } from "./claude/worker-client.mjs";
|
|
18
|
+
import { mapResult, mapState, nativeParams } from "./identity-mapping.mjs";
|
|
19
|
+
|
|
20
|
+
const adapterFactories = {
|
|
21
|
+
codex: createCodexAdapter,
|
|
22
|
+
claude: createClaudeWorkerAdapter,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function createBackendManager({
|
|
26
|
+
config,
|
|
27
|
+
version,
|
|
28
|
+
send,
|
|
29
|
+
broadcast,
|
|
30
|
+
factories = {},
|
|
31
|
+
}) {
|
|
32
|
+
const folders = createThreadFolders(config.configDir);
|
|
33
|
+
const workspace = createWorkspace({ broadcast, configDir: config.configDir });
|
|
34
|
+
const configuration = createBackendConfiguration(config);
|
|
35
|
+
const providers = createProviders(configuration);
|
|
36
|
+
const instances = new Map();
|
|
37
|
+
const installationJobs = new Map();
|
|
38
|
+
const installationErrors = new Map();
|
|
39
|
+
const cli =
|
|
40
|
+
factories.installation ||
|
|
41
|
+
createCliInstallation({
|
|
42
|
+
changed: () => {
|
|
43
|
+
if (initialized) emitState();
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
let initialized = false,
|
|
47
|
+
closed = false,
|
|
48
|
+
sequence = 0;
|
|
49
|
+
let changing = Promise.resolve();
|
|
50
|
+
const publicBackends = () =>
|
|
51
|
+
[...instances.values()].map(({ backend, adapter, error }) => ({
|
|
52
|
+
...backend,
|
|
53
|
+
installation: {
|
|
54
|
+
...cli.state(backend.id),
|
|
55
|
+
installing: installationJobs.has(backend.id),
|
|
56
|
+
installError:
|
|
57
|
+
installationErrors.get(backend.id) ||
|
|
58
|
+
cli.state(backend.id).installError,
|
|
59
|
+
},
|
|
60
|
+
ready: Boolean(adapter?.ready),
|
|
61
|
+
capabilities: adapter?.capabilities || {},
|
|
62
|
+
error: error || adapter?.state().params.error || null,
|
|
63
|
+
}));
|
|
64
|
+
function state() {
|
|
65
|
+
const states = [...instances.values()]
|
|
66
|
+
.filter((i) => i.adapter)
|
|
67
|
+
.map((i) => mapState(i.adapter.state().params, i.backend.id));
|
|
68
|
+
const merge = (name) => Object.assign({}, ...states.map((s) => s[name]));
|
|
69
|
+
return {
|
|
70
|
+
method: "lan/state",
|
|
71
|
+
params: {
|
|
72
|
+
ready: [...instances.values()].some((i) => i.adapter?.ready),
|
|
73
|
+
bridgeReady: initialized,
|
|
74
|
+
protocolVersion: 2,
|
|
75
|
+
backends: publicBackends(),
|
|
76
|
+
defaultBackendId:
|
|
77
|
+
[...instances.values()].find((i) => i.backend.enabled)?.backend.id ||
|
|
78
|
+
"codex",
|
|
79
|
+
version,
|
|
80
|
+
defaultCwd: config.defaultCwd,
|
|
81
|
+
projectlessRoot: config.projectlessRoot,
|
|
82
|
+
projectlessRoots: [
|
|
83
|
+
config.projectlessRoot,
|
|
84
|
+
...(config.legacyConfigDir
|
|
85
|
+
? [join(config.legacyConfigDir, "workspaces")]
|
|
86
|
+
: []),
|
|
87
|
+
join(
|
|
88
|
+
instances.get("codex")?.backend.dataDir || config.codexHome,
|
|
89
|
+
"codex-web",
|
|
90
|
+
"workspaces",
|
|
91
|
+
),
|
|
92
|
+
],
|
|
93
|
+
projectModels: true,
|
|
94
|
+
questions: states.flatMap((s) => s.questions),
|
|
95
|
+
activeTurns: merge("activeTurns"),
|
|
96
|
+
executionStatuses: merge("executionStatuses"),
|
|
97
|
+
compactions: merge("compactions"),
|
|
98
|
+
terminals: { ...merge("terminals"), ...workspace.snapshot() },
|
|
99
|
+
undoneFiles: states.flatMap((s) => s.undoneFiles),
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function emitState() {
|
|
104
|
+
if (!closed) broadcast(state());
|
|
105
|
+
}
|
|
106
|
+
async function install(backend) {
|
|
107
|
+
const entry = { backend, adapter: null, error: null, proxies: new Map() };
|
|
108
|
+
instances.set(backend.id, entry);
|
|
109
|
+
if (!backend.enabled) return;
|
|
110
|
+
try {
|
|
111
|
+
const factory = factories[backend.type] || adapterFactories[backend.type];
|
|
112
|
+
entry.adapter = factory({
|
|
113
|
+
backend,
|
|
114
|
+
config,
|
|
115
|
+
version,
|
|
116
|
+
providers,
|
|
117
|
+
send(client, message) {
|
|
118
|
+
client.deliver(message);
|
|
119
|
+
},
|
|
120
|
+
broadcast(message) {
|
|
121
|
+
if (instances.get(backend.id) !== entry || closed) return;
|
|
122
|
+
if (message.method === "lan/state") return emitState();
|
|
123
|
+
broadcast({
|
|
124
|
+
...message,
|
|
125
|
+
...(backend.id !== "codex" && { backendId: backend.id }),
|
|
126
|
+
...(message.id !== undefined && {
|
|
127
|
+
id: resourceId(backend.id, message.id),
|
|
128
|
+
}),
|
|
129
|
+
params: mapResult(message.params, backend.id, message.method),
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
await entry.adapter.connect();
|
|
134
|
+
} catch (error) {
|
|
135
|
+
entry.error = error.message;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async function connect() {
|
|
139
|
+
await folders.read();
|
|
140
|
+
const data = await configuration.read();
|
|
141
|
+
await Promise.all(data.backends.map(install));
|
|
142
|
+
initialized = true;
|
|
143
|
+
cli.start?.();
|
|
144
|
+
emitState();
|
|
145
|
+
}
|
|
146
|
+
function instance(id) {
|
|
147
|
+
const entry = instances.get(id);
|
|
148
|
+
if (entry?.changing) throw new Error("后端配置正在更新,请稍后重试。");
|
|
149
|
+
if (!entry?.adapter)
|
|
150
|
+
throw new Error(entry?.error || "后端未启用,请在设置中检查配置。");
|
|
151
|
+
return entry;
|
|
152
|
+
}
|
|
153
|
+
function proxy(entry, client) {
|
|
154
|
+
if (!entry.proxies.has(client)) {
|
|
155
|
+
const methods = new Map();
|
|
156
|
+
entry.proxies.set(client, {
|
|
157
|
+
get readyState() {
|
|
158
|
+
return client.readyState;
|
|
159
|
+
},
|
|
160
|
+
methods,
|
|
161
|
+
deliver(message) {
|
|
162
|
+
const method = methods.get(message.id);
|
|
163
|
+
methods.delete(message.id);
|
|
164
|
+
send(client, {
|
|
165
|
+
...message,
|
|
166
|
+
...(message.result && {
|
|
167
|
+
result: folders.result(
|
|
168
|
+
mapResult(message.result, entry.backend.id, method),
|
|
169
|
+
method,
|
|
170
|
+
),
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return entry.proxies.get(client);
|
|
177
|
+
}
|
|
178
|
+
function call(entry, method, params) {
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
const id = `manager-${++sequence}`;
|
|
181
|
+
const timer = setTimeout(() => reject(new Error("后端读取超时")), 120000);
|
|
182
|
+
const client = {
|
|
183
|
+
readyState: 3,
|
|
184
|
+
deliver(message) {
|
|
185
|
+
clearTimeout(timer);
|
|
186
|
+
if (message.error) reject(new Error(message.error.message));
|
|
187
|
+
else
|
|
188
|
+
resolve(
|
|
189
|
+
folders.result(
|
|
190
|
+
mapResult(message.result, entry.backend.id, method),
|
|
191
|
+
method,
|
|
192
|
+
),
|
|
193
|
+
);
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
Promise.resolve(
|
|
197
|
+
entry.adapter.receive(client, { id, method, params }),
|
|
198
|
+
).catch((error) => {
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
reject(error);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async function listThreads(params) {
|
|
205
|
+
const ids = [...instances.values()]
|
|
206
|
+
.filter((i) => i.backend.enabled)
|
|
207
|
+
.map((i) => i.backend.id);
|
|
208
|
+
const position = params.cursor
|
|
209
|
+
? JSON.parse(decodeURIComponent(params.cursor))
|
|
210
|
+
: { index: 0 };
|
|
211
|
+
const errors = [];
|
|
212
|
+
while (position.index < ids.length) {
|
|
213
|
+
const backendId = ids[position.index];
|
|
214
|
+
try {
|
|
215
|
+
const entry = instance(backendId);
|
|
216
|
+
const page = await call(entry, "thread/list", {
|
|
217
|
+
...params,
|
|
218
|
+
cursor: position.cursor,
|
|
219
|
+
});
|
|
220
|
+
const next = page.nextCursor
|
|
221
|
+
? { ...position, cursor: page.nextCursor }
|
|
222
|
+
: { index: position.index + 1 };
|
|
223
|
+
return {
|
|
224
|
+
...page,
|
|
225
|
+
backendErrors: errors,
|
|
226
|
+
nextCursor:
|
|
227
|
+
next.index < ids.length
|
|
228
|
+
? encodeURIComponent(JSON.stringify(next))
|
|
229
|
+
: null,
|
|
230
|
+
};
|
|
231
|
+
} catch (error) {
|
|
232
|
+
errors.push({ backendId, message: error.message });
|
|
233
|
+
position.index++;
|
|
234
|
+
delete position.cursor;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { data: [], nextCursor: null, backendErrors: errors };
|
|
238
|
+
}
|
|
239
|
+
async function saveBackend(params, prepare) {
|
|
240
|
+
validateBackend(params, config);
|
|
241
|
+
const old = instances.get(params.id);
|
|
242
|
+
if (params.hiddenModels !== undefined && params.enabled === undefined) {
|
|
243
|
+
if (params.id !== "codex") throw new Error("模型显示配置仅用于 Codex");
|
|
244
|
+
old.backend = await configuration.save({
|
|
245
|
+
...old.backend,
|
|
246
|
+
hiddenModels: params.hiddenModels,
|
|
247
|
+
});
|
|
248
|
+
emitState();
|
|
249
|
+
return { backends: publicBackends() };
|
|
250
|
+
}
|
|
251
|
+
const snapshot = old?.adapter?.state().params;
|
|
252
|
+
if (hasBackendWork(snapshot))
|
|
253
|
+
throw new Error("请等待此后端的任务结束后修改配置。");
|
|
254
|
+
if (old) old.changing = true;
|
|
255
|
+
try {
|
|
256
|
+
await old?.adapter?.shutdown?.();
|
|
257
|
+
if (prepare) await prepare();
|
|
258
|
+
const backend = await configuration.save(params);
|
|
259
|
+
old?.adapter?.close();
|
|
260
|
+
await install(backend);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (old) {
|
|
263
|
+
old.changing = false;
|
|
264
|
+
if (!old.adapter?.ready) {
|
|
265
|
+
old.adapter?.close();
|
|
266
|
+
await install(old.backend);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
emitState();
|
|
272
|
+
return { backends: publicBackends() };
|
|
273
|
+
}
|
|
274
|
+
async function receive(client, message) {
|
|
275
|
+
await folders.read();
|
|
276
|
+
const method = message.method,
|
|
277
|
+
params = message.params || {};
|
|
278
|
+
const reply = (result) => send(client, { id: message.id, result });
|
|
279
|
+
if (method?.startsWith("lan/terminal/"))
|
|
280
|
+
return reply(await workspace.request(method, params));
|
|
281
|
+
const fallback =
|
|
282
|
+
params.threadId != null
|
|
283
|
+
? "codex"
|
|
284
|
+
: params.backendId || state().params.defaultBackendId;
|
|
285
|
+
const target = resourceTarget(
|
|
286
|
+
params.threadId ?? params.processId ?? (!method ? message.id : null),
|
|
287
|
+
fallback,
|
|
288
|
+
);
|
|
289
|
+
if (method === "lan/thread/move") {
|
|
290
|
+
const entry = instance(target.backendId);
|
|
291
|
+
const { thread } = await call(entry, "thread/read", {
|
|
292
|
+
threadId: target.nativeId,
|
|
293
|
+
includeTurns: false,
|
|
294
|
+
});
|
|
295
|
+
const moved = await folders.move(thread, params.folderPath);
|
|
296
|
+
broadcast({ method: "thread/folder/updated", params: { thread: moved } });
|
|
297
|
+
return reply({ thread: moved });
|
|
298
|
+
}
|
|
299
|
+
if (method === "lan/projectless/read")
|
|
300
|
+
return reply({
|
|
301
|
+
directories: await readProjectlessDirectories(
|
|
302
|
+
instances.get("codex")?.backend.dataDir || config.codexHome,
|
|
303
|
+
),
|
|
304
|
+
});
|
|
305
|
+
if (method === "lan/backends/versions") {
|
|
306
|
+
if (!instances.has(params.id)) throw new Error("未知后端");
|
|
307
|
+
return reply({ backends: publicBackends() });
|
|
308
|
+
}
|
|
309
|
+
if (method === "lan/backends/install") {
|
|
310
|
+
const entry = instances.get(params.id);
|
|
311
|
+
if (!entry) throw new Error("未知后端");
|
|
312
|
+
if (!installationJobs.has(params.id)) {
|
|
313
|
+
installationErrors.delete(params.id);
|
|
314
|
+
const operation = changing.then(() =>
|
|
315
|
+
saveBackend(instances.get(params.id).backend, () =>
|
|
316
|
+
cli.install(params.id),
|
|
317
|
+
),
|
|
318
|
+
);
|
|
319
|
+
installationJobs.set(params.id, operation);
|
|
320
|
+
changing = operation.catch(() => {});
|
|
321
|
+
operation
|
|
322
|
+
.catch((error) => installationErrors.set(params.id, error.message))
|
|
323
|
+
.finally(() => {
|
|
324
|
+
installationJobs.delete(params.id);
|
|
325
|
+
emitState();
|
|
326
|
+
});
|
|
327
|
+
emitState();
|
|
328
|
+
}
|
|
329
|
+
return reply({ backends: publicBackends() });
|
|
330
|
+
}
|
|
331
|
+
if (method === "lan/backends/list")
|
|
332
|
+
return reply({ backends: publicBackends() });
|
|
333
|
+
if (
|
|
334
|
+
[
|
|
335
|
+
"lan/backends/save",
|
|
336
|
+
"lan/backends/disable",
|
|
337
|
+
"lan/backends/check",
|
|
338
|
+
].includes(method)
|
|
339
|
+
) {
|
|
340
|
+
const input =
|
|
341
|
+
method === "lan/backends/save"
|
|
342
|
+
? params
|
|
343
|
+
: {
|
|
344
|
+
...instances.get(params.id)?.backend,
|
|
345
|
+
...(method.endsWith("disable") && { enabled: false }),
|
|
346
|
+
};
|
|
347
|
+
const operation = changing.then(() => saveBackend(input));
|
|
348
|
+
changing = operation.catch(() => {});
|
|
349
|
+
return reply(await operation);
|
|
350
|
+
}
|
|
351
|
+
if (method === "lan/providers/list")
|
|
352
|
+
return reply({
|
|
353
|
+
providers: await providers.list(params.backendId, {
|
|
354
|
+
includeDisabled: params.includeDisabled === true,
|
|
355
|
+
}),
|
|
356
|
+
});
|
|
357
|
+
if (method === "lan/providers/save")
|
|
358
|
+
return reply({ providers: await providers.save(params) });
|
|
359
|
+
if (method === "lan/providers/remove")
|
|
360
|
+
return reply({ providers: await providers.remove(params) });
|
|
361
|
+
if (method === "thread/list" && !params.backendId)
|
|
362
|
+
return reply(await listThreads(params));
|
|
363
|
+
const entry = instance(target.backendId);
|
|
364
|
+
if (workspace.handles(method) && entry.adapter.workspaceOwner === "bridge")
|
|
365
|
+
return reply(await workspace.request(method, params));
|
|
366
|
+
const recipient = proxy(entry, client);
|
|
367
|
+
recipient.methods.set(message.id, method);
|
|
368
|
+
await entry.adapter.receive(recipient, {
|
|
369
|
+
...message,
|
|
370
|
+
...(!method && { id: target.nativeId }),
|
|
371
|
+
...(method && {
|
|
372
|
+
params: nativeParams(folders.params(method, params), entry.backend.id),
|
|
373
|
+
}),
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
function releaseClient(client) {
|
|
377
|
+
for (const entry of instances.values()) {
|
|
378
|
+
const recipient = entry.proxies.get(client);
|
|
379
|
+
entry.proxies.delete(client);
|
|
380
|
+
if (recipient) entry.adapter?.releaseClient(recipient);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return {
|
|
384
|
+
state,
|
|
385
|
+
receive,
|
|
386
|
+
connect,
|
|
387
|
+
releaseClient,
|
|
388
|
+
releaseUnusedThreads() {
|
|
389
|
+
for (const i of instances.values()) i.adapter?.releaseUnusedThreads();
|
|
390
|
+
},
|
|
391
|
+
maintenanceState() {
|
|
392
|
+
const { params } = state();
|
|
393
|
+
return {
|
|
394
|
+
method: "lan/state",
|
|
395
|
+
params: {
|
|
396
|
+
ready: initialized,
|
|
397
|
+
activeTurns: params.activeTurns,
|
|
398
|
+
compactions: params.compactions,
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
},
|
|
402
|
+
close() {
|
|
403
|
+
closed = true;
|
|
404
|
+
cli.close?.();
|
|
405
|
+
workspace.close();
|
|
406
|
+
for (const i of instances.values()) i.adapter?.close();
|
|
407
|
+
},
|
|
408
|
+
get ready() {
|
|
409
|
+
return state().params.ready;
|
|
410
|
+
},
|
|
411
|
+
};
|
|
412
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const proxyKeys = [
|
|
2
|
+
"HTTP_PROXY",
|
|
3
|
+
"HTTPS_PROXY",
|
|
4
|
+
"ALL_PROXY",
|
|
5
|
+
"NO_PROXY",
|
|
6
|
+
"http_proxy",
|
|
7
|
+
"https_proxy",
|
|
8
|
+
"all_proxy",
|
|
9
|
+
"no_proxy",
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export function validateNetworkProxy(input) {
|
|
13
|
+
if (input === undefined) return undefined;
|
|
14
|
+
if (!input || !["system", "off", "http", "https"].includes(input.mode))
|
|
15
|
+
throw new Error("请选择有效的代理协议或关闭代理");
|
|
16
|
+
if (["system", "off"].includes(input.mode)) return { mode: input.mode };
|
|
17
|
+
// 旧版 HTTPS 选项迁移为通过 HTTP CONNECT 访问 HTTPS 网页。
|
|
18
|
+
const mode = "http";
|
|
19
|
+
const host = String(input.host || "").trim();
|
|
20
|
+
const port = Number(input.port);
|
|
21
|
+
if (!host || /[\s/@?#\\]/.test(host)) throw new Error("请填写有效的代理地址");
|
|
22
|
+
let url;
|
|
23
|
+
try {
|
|
24
|
+
url = new URL(`${mode}://${host}:${port}`);
|
|
25
|
+
} catch {
|
|
26
|
+
throw new Error("请填写有效的代理地址和端口");
|
|
27
|
+
}
|
|
28
|
+
if (
|
|
29
|
+
url.hostname !== host.toLowerCase() ||
|
|
30
|
+
!Number.isInteger(port) ||
|
|
31
|
+
port < 1 ||
|
|
32
|
+
port > 65535
|
|
33
|
+
)
|
|
34
|
+
throw new Error("代理端口须为 1~65535 的整数,地址只填写主机名或 IP");
|
|
35
|
+
return { mode, host: url.hostname, port };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function networkProxyEnvironment(proxy) {
|
|
39
|
+
proxy = validateNetworkProxy(proxy);
|
|
40
|
+
if (!proxy || proxy.mode === "system") return {};
|
|
41
|
+
const cleared = Object.fromEntries(proxyKeys.map((key) => [key, ""]));
|
|
42
|
+
if (proxy.mode === "off") return { ...cleared, NODE_USE_ENV_PROXY: "0" };
|
|
43
|
+
const url = `${proxy.mode}://${proxy.host}:${proxy.port}`;
|
|
44
|
+
const bypass = "localhost,127.0.0.1,::1";
|
|
45
|
+
return {
|
|
46
|
+
...cleared,
|
|
47
|
+
HTTP_PROXY: url,
|
|
48
|
+
HTTPS_PROXY: url,
|
|
49
|
+
http_proxy: url,
|
|
50
|
+
https_proxy: url,
|
|
51
|
+
NO_PROXY: bypass,
|
|
52
|
+
no_proxy: bypass,
|
|
53
|
+
NODE_USE_ENV_PROXY: "1",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// 默认供应商统一使用现有 Messages 运行时;新增服务在此登记连接与模型信息。
|
|
2
|
+
export const providerPresets = [
|
|
3
|
+
{
|
|
4
|
+
id: "deepseek",
|
|
5
|
+
backendId: "claude",
|
|
6
|
+
name: "DeepSeek",
|
|
7
|
+
icon: "deepseek",
|
|
8
|
+
protocol: "messages",
|
|
9
|
+
baseUrl: "https://api.deepseek.com/anthropic",
|
|
10
|
+
models: ["deepseek-flash", "deepseek-v4-pro"],
|
|
11
|
+
modelAliases: {
|
|
12
|
+
"deepseek-flash": "DeepSeek Flash",
|
|
13
|
+
"deepseek-v4-pro": "DeepSeek V4 Pro",
|
|
14
|
+
},
|
|
15
|
+
// https://api-docs.deepseek.com/api/list-models/
|
|
16
|
+
modelCapabilities: Object.fromEntries(
|
|
17
|
+
["deepseek-flash", "deepseek-v4-pro"].map((model) => [
|
|
18
|
+
model,
|
|
19
|
+
{
|
|
20
|
+
contextWindow: 1048576,
|
|
21
|
+
supportedEffortLevels: ["low", "high", "max"],
|
|
22
|
+
supportsAdaptiveThinking: true,
|
|
23
|
+
supportsFastMode: false,
|
|
24
|
+
},
|
|
25
|
+
]),
|
|
26
|
+
),
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
export function configuredProviders(saved) {
|
|
31
|
+
return [
|
|
32
|
+
...providerPresets.map((preset) => {
|
|
33
|
+
const provider = saved.find((p) => p.id === preset.id);
|
|
34
|
+
return {
|
|
35
|
+
...preset,
|
|
36
|
+
apiKey: provider?.apiKey || "",
|
|
37
|
+
preset: true,
|
|
38
|
+
enabled: Boolean(provider?.apiKey?.trim()),
|
|
39
|
+
};
|
|
40
|
+
}),
|
|
41
|
+
...saved.filter(
|
|
42
|
+
(provider) => !providerPresets.some((p) => p.id === provider.id),
|
|
43
|
+
),
|
|
44
|
+
];
|
|
45
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { configuredProviders, providerPresets } from "./provider-presets.mjs";
|
|
3
|
+
|
|
4
|
+
export function createProviders(configuration) {
|
|
5
|
+
const publicProvider = ({ apiKey, ...provider }) => ({
|
|
6
|
+
...provider,
|
|
7
|
+
hasApiKey: Boolean(apiKey),
|
|
8
|
+
});
|
|
9
|
+
async function list(backendId, { includeDisabled = false } = {}) {
|
|
10
|
+
const providers = await configuration.readProviders();
|
|
11
|
+
return configuredProviders(providers)
|
|
12
|
+
.filter(
|
|
13
|
+
(p) =>
|
|
14
|
+
p.backendId === "claude" &&
|
|
15
|
+
(!backendId || p.backendId === backendId) &&
|
|
16
|
+
(includeDisabled || p.enabled !== false),
|
|
17
|
+
)
|
|
18
|
+
.map(publicProvider);
|
|
19
|
+
}
|
|
20
|
+
async function get(backendId, providerId) {
|
|
21
|
+
const providers = configuredProviders(
|
|
22
|
+
await configuration.readProviders(),
|
|
23
|
+
).filter((p) => p.backendId === backendId);
|
|
24
|
+
if (providerId == null || providerId === "") return null;
|
|
25
|
+
const provider = providers.find((p) => p.id === providerId);
|
|
26
|
+
if (!provider) throw new Error("供应商已移除,请重新选择");
|
|
27
|
+
if (provider.enabled === false)
|
|
28
|
+
throw new Error("供应商已停用,请填写 API Key 后重试");
|
|
29
|
+
return provider;
|
|
30
|
+
}
|
|
31
|
+
async function save(input) {
|
|
32
|
+
const preset = providerPresets.find((p) => p.id === input.id);
|
|
33
|
+
if (input.backendId !== "claude")
|
|
34
|
+
throw new Error("供应商配置仅用于 Claude Code");
|
|
35
|
+
if (preset) input = { ...preset, apiKey: input.apiKey };
|
|
36
|
+
const id = input.id || randomUUID();
|
|
37
|
+
if (!/^[a-zA-Z0-9_-]{1,80}$/.test(id)) throw new Error("供应商标识无效");
|
|
38
|
+
const url = new URL(input.baseUrl);
|
|
39
|
+
if (
|
|
40
|
+
!["http:", "https:"].includes(url.protocol) ||
|
|
41
|
+
url.username ||
|
|
42
|
+
url.password ||
|
|
43
|
+
url.search ||
|
|
44
|
+
url.hash
|
|
45
|
+
)
|
|
46
|
+
throw new Error("请填写有效的 API 地址");
|
|
47
|
+
if (!input.name?.trim()) throw new Error("请填写供应商名称");
|
|
48
|
+
const protocol = input.protocol || "messages";
|
|
49
|
+
if (!["messages", "anthropic"].includes(protocol))
|
|
50
|
+
throw new Error("请选择支持的 API 协议");
|
|
51
|
+
const models = (input.models || []).map((m) =>
|
|
52
|
+
typeof m === "string" ? m.trim() : m.id?.trim(),
|
|
53
|
+
);
|
|
54
|
+
if (
|
|
55
|
+
!models.length ||
|
|
56
|
+
models.some((m) => !m) ||
|
|
57
|
+
new Set(models).size !== models.length
|
|
58
|
+
)
|
|
59
|
+
throw new Error("请填写不重复的模型标识");
|
|
60
|
+
const modelAliases = {};
|
|
61
|
+
for (const [model, alias] of Object.entries(input.modelAliases || {})) {
|
|
62
|
+
if (!models.includes(model) || typeof alias !== "string")
|
|
63
|
+
throw new Error("模型显示别名无效");
|
|
64
|
+
if (alias.trim()) modelAliases[model] = alias.trim();
|
|
65
|
+
}
|
|
66
|
+
const modelCapabilities = input.modelCapabilities || {};
|
|
67
|
+
for (const [model, metadata] of Object.entries(modelCapabilities)) {
|
|
68
|
+
if (
|
|
69
|
+
!models.includes(model) ||
|
|
70
|
+
!Array.isArray(metadata.supportedEffortLevels) ||
|
|
71
|
+
metadata.supportedEffortLevels.some(
|
|
72
|
+
(level) => !["low", "medium", "high", "xhigh", "max"].includes(level),
|
|
73
|
+
) ||
|
|
74
|
+
(metadata.contextWindow !== undefined &&
|
|
75
|
+
(!Number.isSafeInteger(metadata.contextWindow) ||
|
|
76
|
+
metadata.contextWindow <= 0)) ||
|
|
77
|
+
[metadata.supportsFastMode, metadata.supportsAdaptiveThinking].some(
|
|
78
|
+
(value) => value !== undefined && typeof value !== "boolean",
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
throw new Error("模型参数配置无效");
|
|
82
|
+
}
|
|
83
|
+
await configuration.update((data) => {
|
|
84
|
+
const backend = data.backends.find((b) => b.id === input.backendId);
|
|
85
|
+
if (!backend) throw new Error("后端不存在");
|
|
86
|
+
const previous = data.providers.find((p) => p.id === id);
|
|
87
|
+
if (previous && previous.backendId !== input.backendId)
|
|
88
|
+
throw new Error("供应商所属后端不可更改");
|
|
89
|
+
const provider = {
|
|
90
|
+
id,
|
|
91
|
+
backendId: backend.id,
|
|
92
|
+
name: input.name.trim(),
|
|
93
|
+
protocol,
|
|
94
|
+
baseUrl: url.href.replace(/\/$/, ""),
|
|
95
|
+
models,
|
|
96
|
+
modelAliases:
|
|
97
|
+
input.modelAliases === undefined
|
|
98
|
+
? Object.fromEntries(
|
|
99
|
+
Object.entries(previous?.modelAliases || {}).filter(([model]) =>
|
|
100
|
+
models.includes(model),
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
: modelAliases,
|
|
104
|
+
modelCapabilities:
|
|
105
|
+
input.modelCapabilities === undefined
|
|
106
|
+
? Object.fromEntries(
|
|
107
|
+
Object.entries(previous?.modelCapabilities || {}).filter(
|
|
108
|
+
([model]) => models.includes(model),
|
|
109
|
+
),
|
|
110
|
+
)
|
|
111
|
+
: modelCapabilities,
|
|
112
|
+
apiKey:
|
|
113
|
+
input.apiKey === undefined
|
|
114
|
+
? previous?.apiKey || ""
|
|
115
|
+
: String(input.apiKey).trim(),
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
...data,
|
|
119
|
+
providers: [...data.providers.filter((p) => p.id !== id), provider],
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
return list(input.backendId);
|
|
123
|
+
}
|
|
124
|
+
async function remove({ id }) {
|
|
125
|
+
await configuration.update((data) => ({
|
|
126
|
+
...data,
|
|
127
|
+
providers: data.providers.filter((p) => p.id !== id),
|
|
128
|
+
}));
|
|
129
|
+
return list();
|
|
130
|
+
}
|
|
131
|
+
return { list, get, save, remove };
|
|
132
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile, rename, rm } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
|
|
5
|
+
export function createStorage(path, initial) {
|
|
6
|
+
let writing = Promise.resolve();
|
|
7
|
+
async function read() {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (error.code === "ENOENT") return structuredClone(initial);
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function update(change) {
|
|
16
|
+
const operation = writing.then(async () => {
|
|
17
|
+
const next = await change(await read());
|
|
18
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
19
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
20
|
+
try {
|
|
21
|
+
await writeFile(temporary, JSON.stringify(next, null, 2) + "\n", {
|
|
22
|
+
mode: 0o600,
|
|
23
|
+
flag: "wx",
|
|
24
|
+
});
|
|
25
|
+
await rename(temporary, path);
|
|
26
|
+
} finally {
|
|
27
|
+
await rm(temporary, { force: true });
|
|
28
|
+
}
|
|
29
|
+
return next;
|
|
30
|
+
});
|
|
31
|
+
writing = operation.catch(() => {});
|
|
32
|
+
return operation;
|
|
33
|
+
}
|
|
34
|
+
return { read: () => writing.then(read), update };
|
|
35
|
+
}
|