@hjd-hitsz/claudex-web 0.0.0-stage → 4.1.0

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.
Files changed (340) hide show
  1. package/.env.example +27 -0
  2. package/CHANGELOG.md +495 -0
  3. package/LICENSE +674 -0
  4. package/README.md +106 -2
  5. package/bin/claudex-web.mjs +129 -0
  6. package/build-info.json +8 -0
  7. package/client/dist/_baseMerge.js +1 -0
  8. package/client/dist/abnfDiagram-WEYOIA3C.js +1 -0
  9. package/client/dist/apl.js +1 -0
  10. package/client/dist/app.js +392 -0
  11. package/client/dist/apple-touch-icon.png +0 -0
  12. package/client/dist/arc.js +1 -0
  13. package/client/dist/architectureDiagram-ZHVYKT53.js +36 -0
  14. package/client/dist/asciiarmor.js +1 -0
  15. package/client/dist/asn1.js +1 -0
  16. package/client/dist/asterisk.js +1 -0
  17. package/client/dist/blockDiagram-RJAE7QA3.js +140 -0
  18. package/client/dist/brainfuck.js +1 -0
  19. package/client/dist/c4Diagram-XDPPPE54.js +38 -0
  20. package/client/dist/channel.js +1 -0
  21. package/client/dist/chunk-2E7WHVMD.js +88 -0
  22. package/client/dist/chunk-2Q5K7J3B.js +1 -0
  23. package/client/dist/chunk-4EMNKR2T.js +1 -0
  24. package/client/dist/chunk-55XCU76K.js +1 -0
  25. package/client/dist/chunk-5VM5RSS4.js +15 -0
  26. package/client/dist/chunk-JWPE2WC7.js +1 -0
  27. package/client/dist/chunk-VQ5W62QI.js +1 -0
  28. package/client/dist/chunk-XXDRQBXY.js +1 -0
  29. package/client/dist/chunk-ZCHMQSOR.js +2 -0
  30. package/client/dist/classDiagram-v2-4MYWR3UA.js +217 -0
  31. package/client/dist/clike.js +1 -0
  32. package/client/dist/clojure.js +1 -0
  33. package/client/dist/cmake.js +1 -0
  34. package/client/dist/cobol.js +1 -0
  35. package/client/dist/code-highlight-style.js +13 -0
  36. package/client/dist/coffeescript.js +1 -0
  37. package/client/dist/commonlisp.js +1 -0
  38. package/client/dist/cose-bilkent-JH36ORCC.js +1 -0
  39. package/client/dist/crystal.js +1 -0
  40. package/client/dist/css.js +1 -0
  41. package/client/dist/cynefin-EF2NZ3EQ.js +141 -0
  42. package/client/dist/cynefinDiagram-WF24UDRP.js +62 -0
  43. package/client/dist/cypher.js +1 -0
  44. package/client/dist/cytoscape.esm.js +321 -0
  45. package/client/dist/d.js +1 -0
  46. package/client/dist/dagre-NK4GIZVX.js +4 -0
  47. package/client/dist/defaultLocale.js +1 -0
  48. package/client/dist/diagram-CBK3MWTC.js +24 -0
  49. package/client/dist/diagram-I5XIIV2D.js +24 -0
  50. package/client/dist/diagram-ODIL2UCW.js +30 -0
  51. package/client/dist/diagram-PFBEJUXE.js +3 -0
  52. package/client/dist/diagram-RJA3I4VV.js +41 -0
  53. package/client/dist/diagram-TYUNFSNR.js +200 -0
  54. package/client/dist/diff.js +1 -0
  55. package/client/dist/dockerfile.js +1 -0
  56. package/client/dist/document-editor.js +1040 -0
  57. package/client/dist/dtd.js +1 -0
  58. package/client/dist/dylan.js +1 -0
  59. package/client/dist/ebnf.js +1 -0
  60. package/client/dist/ebnfDiagram-V4P3TTPT.js +1 -0
  61. package/client/dist/ecl.js +1 -0
  62. package/client/dist/eiffel.js +1 -0
  63. package/client/dist/elk-QONTBCTQ.js +27 -0
  64. package/client/dist/elm.js +1 -0
  65. package/client/dist/erDiagram-KYQIGQAA.js +99 -0
  66. package/client/dist/erlang.js +1 -0
  67. package/client/dist/factor.js +1 -0
  68. package/client/dist/favicon.ico +0 -0
  69. package/client/dist/favicon.png +0 -0
  70. package/client/dist/fcl.js +1 -0
  71. package/client/dist/file-panel.js +1 -0
  72. package/client/dist/flowDiagram-IUGLLWBU.js +213 -0
  73. package/client/dist/forth.js +1 -0
  74. package/client/dist/fortran.js +1 -0
  75. package/client/dist/ganttDiagram-R7GPNTJY.js +292 -0
  76. package/client/dist/gas.js +1 -0
  77. package/client/dist/gherkin.js +1 -0
  78. package/client/dist/gitGraphDiagram-PDYL7XHY.js +106 -0
  79. package/client/dist/graph.js +1 -0
  80. package/client/dist/groovy.js +1 -0
  81. package/client/dist/haskell.js +1 -0
  82. package/client/dist/haxe.js +1 -0
  83. package/client/dist/http.js +1 -0
  84. package/client/dist/idl.js +1 -0
  85. package/client/dist/index.html +25 -0
  86. package/client/dist/index.js +2 -0
  87. package/client/dist/index10.js +1 -0
  88. package/client/dist/index11.js +1 -0
  89. package/client/dist/index12.js +1 -0
  90. package/client/dist/index13.js +7 -0
  91. package/client/dist/index14.js +1 -0
  92. package/client/dist/index15.js +1 -0
  93. package/client/dist/index16.js +6 -0
  94. package/client/dist/index17.js +1 -0
  95. package/client/dist/index18.js +3 -0
  96. package/client/dist/index19.js +1 -0
  97. package/client/dist/index2.js +1 -0
  98. package/client/dist/index20.js +1 -0
  99. package/client/dist/index21.js +1 -0
  100. package/client/dist/index22.js +1 -0
  101. package/client/dist/index3.js +1 -0
  102. package/client/dist/index4.js +1 -0
  103. package/client/dist/index5.js +1 -0
  104. package/client/dist/index6.js +13 -0
  105. package/client/dist/index7.js +1 -0
  106. package/client/dist/index8.js +1 -0
  107. package/client/dist/index9.js +1 -0
  108. package/client/dist/infoDiagram-MVDRKAX5.js +2 -0
  109. package/client/dist/init.js +1 -0
  110. package/client/dist/isArrayLikeObject.js +1 -0
  111. package/client/dist/ishikawaDiagram-HPEHV2GE.js +70 -0
  112. package/client/dist/javascript.js +1 -0
  113. package/client/dist/journeyDiagram-BOSHW7O7.js +139 -0
  114. package/client/dist/julia.js +1 -0
  115. package/client/dist/kanban-definition-FNSA4HC5.js +89 -0
  116. package/client/dist/katex.js +257 -0
  117. package/client/dist/linear.js +1 -0
  118. package/client/dist/livescript.js +1 -0
  119. package/client/dist/lua.js +1 -0
  120. package/client/dist/mathematica.js +1 -0
  121. package/client/dist/mbox.js +1 -0
  122. package/client/dist/mermaid.core.js +310 -0
  123. package/client/dist/mindmap-definition-6OY57UG3.js +98 -0
  124. package/client/dist/mirc.js +1 -0
  125. package/client/dist/mllike.js +1 -0
  126. package/client/dist/modelica.js +1 -0
  127. package/client/dist/mscgen.js +1 -0
  128. package/client/dist/mumps.js +1 -0
  129. package/client/dist/nginx.js +1 -0
  130. package/client/dist/notification-worker.js +45 -0
  131. package/client/dist/nsis.js +1 -0
  132. package/client/dist/ntriples.js +1 -0
  133. package/client/dist/octave.js +1 -0
  134. package/client/dist/ordinal.js +1 -0
  135. package/client/dist/oz.js +1 -0
  136. package/client/dist/pascal.js +1 -0
  137. package/client/dist/pegDiagram-ECIUJNBX.js +1 -0
  138. package/client/dist/perl.js +1 -0
  139. package/client/dist/pieDiagram-GNMPY74T.js +39 -0
  140. package/client/dist/pig.js +1 -0
  141. package/client/dist/powershell.js +1 -0
  142. package/client/dist/properties.js +1 -0
  143. package/client/dist/protobuf.js +1 -0
  144. package/client/dist/pug.js +1 -0
  145. package/client/dist/puppet.js +1 -0
  146. package/client/dist/purify.es.js +3 -0
  147. package/client/dist/python.js +1 -0
  148. package/client/dist/q.js +1 -0
  149. package/client/dist/quadrantDiagram-D57K4MC3.js +7 -0
  150. package/client/dist/r.js +1 -0
  151. package/client/dist/railroadDiagram-Y72NRBL7.js +1 -0
  152. package/client/dist/requirementDiagram-EQYSW5RA.js +84 -0
  153. package/client/dist/rpm.js +1 -0
  154. package/client/dist/ruby.js +1 -0
  155. package/client/dist/sankeyDiagram-SY24JHO5.js +40 -0
  156. package/client/dist/sas.js +1 -0
  157. package/client/dist/scheme.js +1 -0
  158. package/client/dist/sequenceDiagram-BLEHNHQU.js +169 -0
  159. package/client/dist/shell.js +1 -0
  160. package/client/dist/sieve.js +1 -0
  161. package/client/dist/simple-mode.js +1 -0
  162. package/client/dist/sizeCapture-INFHLROL.js +1 -0
  163. package/client/dist/smalltalk.js +1 -0
  164. package/client/dist/solr.js +1 -0
  165. package/client/dist/sparql.js +1 -0
  166. package/client/dist/spreadsheet.js +1 -0
  167. package/client/dist/sql.js +1 -0
  168. package/client/dist/stateDiagram-v2-A6IESKWZ.js +291 -0
  169. package/client/dist/stex.js +1 -0
  170. package/client/dist/style.css +1 -0
  171. package/client/dist/stylus.js +1 -0
  172. package/client/dist/swift.js +1 -0
  173. package/client/dist/swimlanes-B3ENRKFQ.js +1 -0
  174. package/client/dist/swimlanesDiagram-AYQRV4AK.js +8 -0
  175. package/client/dist/tcl.js +1 -0
  176. package/client/dist/terminal-panel.js +18 -0
  177. package/client/dist/textile.js +1 -0
  178. package/client/dist/tiddlywiki.js +1 -0
  179. package/client/dist/tiki.js +1 -0
  180. package/client/dist/timeline-definition-JXYKGLGB.js +120 -0
  181. package/client/dist/toml.js +1 -0
  182. package/client/dist/troff.js +1 -0
  183. package/client/dist/ttcn-cfg.js +1 -0
  184. package/client/dist/ttcn.js +1 -0
  185. package/client/dist/turtle.js +1 -0
  186. package/client/dist/usecaseDiagram-WPOLBNNX.js +370 -0
  187. package/client/dist/vb.js +1 -0
  188. package/client/dist/vbscript.js +1 -0
  189. package/client/dist/velocity.js +1 -0
  190. package/client/dist/vennDiagram-Q3Y3IDCW.js +34 -0
  191. package/client/dist/verilog.js +1 -0
  192. package/client/dist/vhdl.js +1 -0
  193. package/client/dist/wardleyDiagram-IVAIDKWA.js +78 -0
  194. package/client/dist/webidl.js +1 -0
  195. package/client/dist/xquery.js +1 -0
  196. package/client/dist/xychartDiagram-4URNHDU4.js +7 -0
  197. package/client/dist/yacas.js +1 -0
  198. package/client/dist/z80.js +1 -0
  199. package/docs/architecture/backend-regressions.md +53 -0
  200. package/docs/architecture/backends.md +115 -0
  201. package/docs/architecture/behavior.md +228 -0
  202. package/docs/architecture.md +335 -0
  203. package/docs/background-tasks.md +39 -0
  204. package/docs/cli-command-coverage.md +60 -0
  205. package/docs/deployment.md +289 -0
  206. package/docs/development.md +396 -0
  207. package/docs/images/desktop-dark.png +0 -0
  208. package/docs/images/desktop-light.png +0 -0
  209. package/docs/images/mobile-sidebar.png +0 -0
  210. package/docs/images/mobile.png +0 -0
  211. package/docs/releases/v2.0.0.md +5 -0
  212. package/docs/releases/v2.1.0.md +6 -0
  213. package/docs/releases/v2.1.1.md +6 -0
  214. package/docs/releases/v2.1.2.md +8 -0
  215. package/docs/releases/v2.2.0.md +11 -0
  216. package/docs/releases/v2.3.0.md +12 -0
  217. package/docs/releases/v2.3.1.md +8 -0
  218. package/docs/releases/v2.4.0.md +8 -0
  219. package/docs/releases/v2.4.1.md +8 -0
  220. package/docs/releases/v2.4.2.md +8 -0
  221. package/docs/releases/v3.0.0.md +9 -0
  222. package/docs/releases/v3.1.0.md +10 -0
  223. package/docs/releases/v3.1.1.md +7 -0
  224. package/docs/releases/v4.0.0-beta.1.md +10 -0
  225. package/docs/releases/v4.0.0.md +20 -0
  226. package/docs/releases/v4.0.1.md +10 -0
  227. package/docs/releases/v4.1.0.md +21 -0
  228. package/docs/versioning.md +129 -0
  229. package/package.json +87 -4
  230. package/packages/translation-engine/LICENSE +674 -0
  231. package/packages/translation-engine/README.md +69 -0
  232. package/packages/translation-engine/bin/tokenizer-linux-x64 +0 -0
  233. package/packages/translation-engine/bin/translate.mjs +29 -0
  234. package/packages/translation-engine/native/build.mjs +15 -0
  235. package/packages/translation-engine/native/tokenizer.c +73 -0
  236. package/packages/translation-engine/package.json +29 -0
  237. package/packages/translation-engine/src/catalog.mjs +103 -0
  238. package/packages/translation-engine/src/cpu-runtime.mjs +90 -0
  239. package/packages/translation-engine/src/download.mjs +94 -0
  240. package/packages/translation-engine/src/index.mjs +70 -0
  241. package/packages/translation-engine/src/inference.mjs +177 -0
  242. package/packages/translation-engine/src/model-store.mjs +197 -0
  243. package/packages/translation-engine/src/tokenizer.mjs +77 -0
  244. package/packages/translation-engine/src/worker.mjs +83 -0
  245. package/scripts/install-services.mjs +135 -0
  246. package/scripts/restart-when-idle.mjs +125 -0
  247. package/scripts/start-backend.mjs +29 -0
  248. package/server/src/agent-messages.mjs +124 -0
  249. package/server/src/application.mjs +168 -0
  250. package/server/src/auth.mjs +328 -0
  251. package/server/src/backends/claude/account.mjs +171 -0
  252. package/server/src/backends/claude/adapter.mjs +366 -0
  253. package/server/src/backends/claude/agent-history.mjs +70 -0
  254. package/server/src/backends/claude/background-tasks.mjs +168 -0
  255. package/server/src/backends/claude/catalogue.mjs +268 -0
  256. package/server/src/backends/claude/execution-errors.mjs +44 -0
  257. package/server/src/backends/claude/execution-session.mjs +396 -0
  258. package/server/src/backends/claude/git-tool-changes.mjs +77 -0
  259. package/server/src/backends/claude/goals.mjs +319 -0
  260. package/server/src/backends/claude/history-actions.mjs +170 -0
  261. package/server/src/backends/claude/history.mjs +65 -0
  262. package/server/src/backends/claude/hooks.mjs +232 -0
  263. package/server/src/backends/claude/input-stream.mjs +24 -0
  264. package/server/src/backends/claude/messages-request.mjs +34 -0
  265. package/server/src/backends/claude/messages.mjs +301 -0
  266. package/server/src/backends/claude/model-options.mjs +18 -0
  267. package/server/src/backends/claude/native-commands.mjs +48 -0
  268. package/server/src/backends/claude/native-records.mjs +76 -0
  269. package/server/src/backends/claude/process-ownership.mjs +82 -0
  270. package/server/src/backends/claude/project-instructions.mjs +40 -0
  271. package/server/src/backends/claude/provider-balance.mjs +40 -0
  272. package/server/src/backends/claude/provider-environment.mjs +95 -0
  273. package/server/src/backends/claude/provider-transport.mjs +70 -0
  274. package/server/src/backends/claude/recovery.mjs +54 -0
  275. package/server/src/backends/claude/runtime.mjs +463 -0
  276. package/server/src/backends/claude/session-worker.mjs +32 -0
  277. package/server/src/backends/claude/sessions.mjs +348 -0
  278. package/server/src/backends/claude/token-usage.mjs +98 -0
  279. package/server/src/backends/claude/worker-client.mjs +351 -0
  280. package/server/src/backends/claude/worker-launcher.mjs +119 -0
  281. package/server/src/backends/claude/worker-location.mjs +43 -0
  282. package/server/src/backends/claude/worker-revision.mjs +45 -0
  283. package/server/src/backends/claude/worker-server.mjs +290 -0
  284. package/server/src/backends/cli-installation.mjs +140 -0
  285. package/server/src/backends/codex/adapter.mjs +155 -0
  286. package/server/src/backends/codex/compaction-recovery.mjs +78 -0
  287. package/server/src/backends/codex/events.mjs +125 -0
  288. package/server/src/backends/codex/methods.mjs +17 -0
  289. package/server/src/backends/codex/reference-mcp.mjs +97 -0
  290. package/server/src/backends/codex/reference-tools.mjs +71 -0
  291. package/server/src/backends/codex/requests.mjs +170 -0
  292. package/server/src/backends/codex/subscriptions.mjs +91 -0
  293. package/server/src/backends/codex/token-usage.mjs +140 -0
  294. package/server/src/backends/codex/upstream.mjs +148 -0
  295. package/server/src/backends/configuration.mjs +122 -0
  296. package/server/src/backends/identity-mapping.mjs +87 -0
  297. package/server/src/backends/manager.mjs +478 -0
  298. package/server/src/backends/network-proxy.mjs +55 -0
  299. package/server/src/backends/provider-presets.mjs +45 -0
  300. package/server/src/backends/providers.mjs +135 -0
  301. package/server/src/backends/storage.mjs +39 -0
  302. package/server/src/bridge/conversation-policy.mjs +29 -0
  303. package/server/src/bridge/file-operations.mjs +47 -0
  304. package/server/src/bridge/product-methods.mjs +66 -0
  305. package/server/src/compaction.mjs +82 -0
  306. package/server/src/config.mjs +64 -0
  307. package/server/src/file-content.mjs +99 -0
  308. package/server/src/file-undo.mjs +142 -0
  309. package/server/src/http/maintenance.mjs +28 -0
  310. package/server/src/http/router.mjs +98 -0
  311. package/server/src/http/static.mjs +62 -0
  312. package/server/src/product-config.mjs +80 -0
  313. package/server/src/project-models.mjs +59 -0
  314. package/server/src/rpc-transport.mjs +21 -0
  315. package/server/src/server.mjs +29 -0
  316. package/server/src/service-maintenance-worker.mjs +124 -0
  317. package/server/src/service-maintenance.mjs +222 -0
  318. package/server/src/summary-translation.mjs +71 -0
  319. package/server/src/system-auth.mjs +45 -0
  320. package/server/src/thread-folders.mjs +76 -0
  321. package/server/src/thread-names.mjs +264 -0
  322. package/server/src/thread-projects.mjs +36 -0
  323. package/server/src/tls.mjs +87 -0
  324. package/server/src/token-usage-reconciliation.mjs +105 -0
  325. package/server/src/token-usage.mjs +208 -0
  326. package/server/src/translation-service.mjs +133 -0
  327. package/server/src/version.mjs +55 -0
  328. package/server/src/web-config-migration.mjs +48 -0
  329. package/server/src/workspace/commands.mjs +33 -0
  330. package/server/src/workspace/files.mjs +44 -0
  331. package/server/src/workspace/service.mjs +27 -0
  332. package/server/src/workspace/terminals.mjs +122 -0
  333. package/shared/backend-activity.mjs +13 -0
  334. package/shared/backend-capabilities.mjs +33 -0
  335. package/shared/backend-identity.mjs +17 -0
  336. package/shared/compaction-state.mjs +49 -0
  337. package/shared/file-types.mjs +82 -0
  338. package/shared/product-contract.mjs +26 -0
  339. package/shared/rpc-limits.mjs +2 -0
  340. package/shared/terminal-workspace.mjs +26 -0
@@ -0,0 +1,135 @@
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
+ protocol: provider.protocol || "messages",
8
+ hasApiKey: Boolean(apiKey),
9
+ });
10
+ async function list(backendId, { includeDisabled = false } = {}) {
11
+ const providers = await configuration.readProviders();
12
+ return configuredProviders(providers)
13
+ .filter(
14
+ (p) =>
15
+ p.backendId === "claude" &&
16
+ (!backendId || p.backendId === backendId) &&
17
+ (includeDisabled || p.enabled !== false),
18
+ )
19
+ .map(publicProvider);
20
+ }
21
+ async function get(backendId, providerId) {
22
+ const providers = configuredProviders(
23
+ await configuration.readProviders(),
24
+ ).filter((p) => p.backendId === backendId);
25
+ if (providerId == null || providerId === "") return null;
26
+ const provider = providers.find((p) => p.id === providerId);
27
+ if (!provider) throw new Error("供应商已移除,请重新选择");
28
+ if (provider.enabled === false)
29
+ throw new Error("供应商已停用,请填写 API Key 后重试");
30
+ return provider;
31
+ }
32
+ async function save(input) {
33
+ const preset = providerPresets.find((p) => p.id === input.id);
34
+ if (input.backendId !== "claude")
35
+ throw new Error("供应商配置仅用于 Claude Code");
36
+ if (preset) input = { ...preset, apiKey: input.apiKey };
37
+ const id = input.id || randomUUID();
38
+ if (!/^[a-zA-Z0-9_-]{1,80}$/.test(id)) throw new Error("供应商标识无效");
39
+ const url = new URL(input.baseUrl);
40
+ if (
41
+ !["http:", "https:"].includes(url.protocol) ||
42
+ url.username ||
43
+ url.password ||
44
+ url.search ||
45
+ url.hash
46
+ )
47
+ throw new Error("请填写有效的 API 地址");
48
+ if (!input.name?.trim()) throw new Error("请填写供应商名称");
49
+ if (input.protocol && !["messages", "anthropic"].includes(input.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
+ input.protocol ||
95
+ previous?.protocol ||
96
+ (previous ? "messages" : "anthropic"),
97
+ baseUrl: url.href.replace(/\/$/, ""),
98
+ models,
99
+ modelAliases:
100
+ input.modelAliases === undefined
101
+ ? Object.fromEntries(
102
+ Object.entries(previous?.modelAliases || {}).filter(([model]) =>
103
+ models.includes(model),
104
+ ),
105
+ )
106
+ : modelAliases,
107
+ modelCapabilities:
108
+ input.modelCapabilities === undefined
109
+ ? Object.fromEntries(
110
+ Object.entries(previous?.modelCapabilities || {}).filter(
111
+ ([model]) => models.includes(model),
112
+ ),
113
+ )
114
+ : modelCapabilities,
115
+ apiKey:
116
+ input.apiKey === undefined
117
+ ? previous?.apiKey || ""
118
+ : String(input.apiKey).trim(),
119
+ };
120
+ return {
121
+ ...data,
122
+ providers: [...data.providers.filter((p) => p.id !== id), provider],
123
+ };
124
+ });
125
+ return list(input.backendId);
126
+ }
127
+ async function remove({ id }) {
128
+ await configuration.update((data) => ({
129
+ ...data,
130
+ providers: data.providers.filter((p) => p.id !== id),
131
+ }));
132
+ return list();
133
+ }
134
+ return { list, get, save, remove };
135
+ }
@@ -0,0 +1,39 @@
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, readCurrent = true) {
16
+ const operation = writing.then(async () => {
17
+ const next = await change(readCurrent ? await read() : undefined);
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 {
35
+ read: () => writing.then(read),
36
+ update,
37
+ replace: (value) => update(() => value, false),
38
+ };
39
+ }
@@ -0,0 +1,29 @@
1
+ const writes = new Set([
2
+ "thread/revert",
3
+ "thread/rollback",
4
+ "turn/start",
5
+ "turn/steer",
6
+ "thread/compact/start",
7
+ "review/start",
8
+ ]);
9
+ const fileWrites = new Set([
10
+ ...writes,
11
+ "thread/goal/get",
12
+ "thread/goal/set",
13
+ "thread/goal/clear",
14
+ "thread/backgroundTerminals/list",
15
+ "thread/backgroundTerminals/clean",
16
+ ]);
17
+
18
+ // 只读取执行方提供的即时占用;原生订阅、队列与变更锁继续由执行方持有。
19
+ export function assertConversationOperation(
20
+ method,
21
+ { compacting, undoing, active, pending },
22
+ ) {
23
+ if (writes.has(method) && compacting)
24
+ throw new Error("正在整理上下文,完成后可继续发送。");
25
+ if (method === "thread/compact/start" && (active || pending))
26
+ throw new Error("请等待当前任务结束后整理上下文。");
27
+ if (fileWrites.has(method) && undoing)
28
+ throw new Error("正在撤销文件修改,完成后可继续发送。");
29
+ }
@@ -0,0 +1,47 @@
1
+ import { changesForPath, undoFileChanges } from "../file-undo.mjs";
2
+
3
+ export function createFileOperations({
4
+ call,
5
+ activeTurns,
6
+ activeThreads,
7
+ broadcast,
8
+ }) {
9
+ const undoing = new Set();
10
+ const undoneFiles = new Map();
11
+
12
+ async function undo({ threadId, turnId, path }) {
13
+ const key = JSON.stringify([threadId, turnId, path]);
14
+ if (undoneFiles.has(key)) {
15
+ const result = undoneFiles.get(key);
16
+ broadcast({ method: "lan/files/undone", params: result });
17
+ return result;
18
+ }
19
+ const isActive = () =>
20
+ activeTurns.has(threadId) || activeThreads.has(threadId);
21
+ if (isActive()) throw new Error("任务正在运行,请在结束后撤销修改。");
22
+ if (undoing.has(threadId)) throw new Error("正在撤销文件修改,请稍候。");
23
+ undoing.add(threadId);
24
+ try {
25
+ const { thread } = await call("thread/read", {
26
+ threadId,
27
+ includeTurns: true,
28
+ });
29
+ const turn = thread.turns.find((turn) => turn.id === turnId);
30
+ if (!turn || turn.status === "inProgress")
31
+ throw new Error("请重新加载此轮记录后再撤销。");
32
+ await undoFileChanges({
33
+ cwd: thread.cwd,
34
+ changes: changesForPath(turn.items, thread.cwd, path),
35
+ isActive,
36
+ });
37
+ const result = { threadId, turnId, path };
38
+ undoneFiles.set(key, result);
39
+ broadcast({ method: "lan/files/undone", params: result });
40
+ return result;
41
+ } finally {
42
+ undoing.delete(threadId);
43
+ }
44
+ }
45
+
46
+ return { undo, undoing, undoneFiles };
47
+ }
@@ -0,0 +1,66 @@
1
+ import { createProjectModels } from "../project-models.mjs";
2
+ import { createFileOperations } from "./file-operations.mjs";
3
+ import { prepareProjectlessDirectory } from "../thread-projects.mjs";
4
+
5
+ // 网页操作在所属后端的执行边界内组合;运行状态与原生历史由调用方提供。
6
+ export function createProductMethods({
7
+ config,
8
+ projectModelsDirectory,
9
+ legacyProjectModelsDirectory,
10
+ call,
11
+ activeTurns,
12
+ activeThreads,
13
+ checkCompaction,
14
+ readAgentMessages,
15
+ broadcast,
16
+ }) {
17
+ const projectModels = createProjectModels(
18
+ projectModelsDirectory,
19
+ legacyProjectModelsDirectory,
20
+ );
21
+ const files = createFileOperations({
22
+ call,
23
+ activeTurns,
24
+ activeThreads,
25
+ broadcast,
26
+ });
27
+ const offline = new Map([
28
+ [
29
+ "lan/projectModels/read",
30
+ async () => ({ projects: await projectModels.read() }),
31
+ ],
32
+ [
33
+ "lan/projectModels/set",
34
+ async (params) => {
35
+ const projects = await projectModels.set(params);
36
+ broadcast({
37
+ method: "lan/projectModels/updated",
38
+ params: { projects },
39
+ });
40
+ return { projects };
41
+ },
42
+ ],
43
+ ]);
44
+ const online = new Map([
45
+ ["lan/agents/messages", ({ threadId }) => readAgentMessages(threadId)],
46
+ [
47
+ "lan/compaction/check",
48
+ async ({ threadId }) => ({ compaction: await checkCompaction(threadId) }),
49
+ ],
50
+ [
51
+ "lan/projectless/prepare",
52
+ async () => {
53
+ try {
54
+ return {
55
+ cwd: await prepareProjectlessDirectory(config.projectlessRoot),
56
+ root: config.projectlessRoot,
57
+ };
58
+ } catch (error) {
59
+ throw new Error("创建对话工作目录失败:" + error.message);
60
+ }
61
+ },
62
+ ],
63
+ ["lan/files/undo", files.undo],
64
+ ]);
65
+ return { offline, online, files };
66
+ }
@@ -0,0 +1,82 @@
1
+ import { isCompacting } from "../../shared/compaction-state.mjs";
2
+ export { isCompacting } from "../../shared/compaction-state.mjs";
3
+
4
+ // 手动整理的 RPC 响应仅确认提交;轮次结束才释放整理占用。
5
+ export function createCompactions(notify, now = () => Date.now() / 1000) {
6
+ const entries = new Map();
7
+ function update(threadId, value) {
8
+ entries.set(threadId, value);
9
+ notify({
10
+ method: "lan/compaction",
11
+ params: { threadId, compaction: value },
12
+ });
13
+ return value;
14
+ }
15
+ function finish(threadId, status, error, completedAt = now()) {
16
+ const entry = entries.get(threadId);
17
+ return update(threadId, { ...entry, status, error, completedAt });
18
+ }
19
+ return {
20
+ entries,
21
+ update,
22
+ finish,
23
+ busy: (threadId) => isCompacting(entries.get(threadId)),
24
+ start(threadId, id) {
25
+ return update(threadId, {
26
+ id: String(id),
27
+ status: "starting",
28
+ startedAt: now(),
29
+ });
30
+ },
31
+ reject(threadId, id, error) {
32
+ const entry = entries.get(threadId);
33
+ if (entry?.id === String(id) && isCompacting(entry))
34
+ finish(threadId, "failed", error);
35
+ },
36
+ event(method, p) {
37
+ const entry = entries.get(p.threadId);
38
+ if (!isCompacting(entry)) return;
39
+ if (
40
+ method === "turn/started" &&
41
+ entry.status === "starting" &&
42
+ !entry.turnId
43
+ )
44
+ update(p.threadId, { ...entry, turnId: p.turn.id, status: "running" });
45
+ if (
46
+ (method === "item/started" || method === "item/completed") &&
47
+ p.item.type === "contextCompaction" &&
48
+ (!entry.turnId || entry.turnId === p.turnId)
49
+ )
50
+ update(p.threadId, { ...entry, turnId: p.turnId, status: "running" });
51
+ if (method === "turn/completed" && entry.turnId === p.turn.id) {
52
+ const status =
53
+ p.turn.status === "completed"
54
+ ? "completed"
55
+ : p.turn.status === "interrupted"
56
+ ? "interrupted"
57
+ : "failed";
58
+ finish(
59
+ p.threadId,
60
+ status,
61
+ p.turn.error?.message,
62
+ p.turn.completedAt ?? now(),
63
+ );
64
+ }
65
+ if (method === "thread/closed")
66
+ update(p.threadId, {
67
+ ...entry,
68
+ status: "reconnecting",
69
+ error: "整理订阅已关闭,需要重新核对任务状态。",
70
+ });
71
+ },
72
+ disconnected() {
73
+ for (const [threadId, entry] of entries)
74
+ if (isCompacting(entry))
75
+ update(threadId, {
76
+ ...entry,
77
+ status: "reconnecting",
78
+ error: "与 Codex 后端的连接中断,正在确认整理结果。",
79
+ });
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,64 @@
1
+ import { homedir } from "node:os";
2
+ import { join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ import { productDirectories } from "./product-config.mjs";
6
+
7
+ export function readConfig(env = process.env) {
8
+ const port = Number(env.PORT || 8766);
9
+ if (!Number.isInteger(port) || port < 0 || port > 65535)
10
+ throw new Error("PORT 必须为 0 到 65535 之间的整数");
11
+ const upstreamUrl = env.CODEX_APP_SERVER_URL || "ws://127.0.0.1:8765";
12
+ if (!["ws:", "wss:"].includes(new URL(upstreamUrl).protocol))
13
+ throw new Error("CODEX_APP_SERVER_URL 必须是 WebSocket 地址");
14
+ const codexHome = resolve(env.CODEX_HOME || join(homedir(), ".codex"));
15
+ const { configDir, legacyConfigDir } = productDirectories(env);
16
+ const claudeHome = resolve(
17
+ env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"),
18
+ );
19
+ const backend = new URL(upstreamUrl);
20
+ if (
21
+ backend.protocol === "ws:" &&
22
+ !["127.0.0.1", "localhost", "[::1]"].includes(backend.hostname)
23
+ )
24
+ throw new Error("跨主机 CODEX_APP_SERVER_URL 必须使用 wss://");
25
+ if (!!env.TLS_CERT !== !!env.TLS_KEY)
26
+ throw new Error("TLS_CERT 与 TLS_KEY 必须同时配置");
27
+ const sessionDays = Number(env.AUTH_SESSION_DAYS || 30);
28
+ if (!Number.isInteger(sessionDays) || sessionDays < 1 || sessionDays > 90)
29
+ throw new Error("AUTH_SESSION_DAYS 必须是 1 到 90 之间的整数");
30
+ const pamService = env.AUTH_PAM_SERVICE || "login";
31
+ if (!/^[a-zA-Z0-9_-]+$/.test(pamService))
32
+ throw new Error("AUTH_PAM_SERVICE 必须是 PAM 服务名称");
33
+ return {
34
+ backendsFile: env.CLAUDEX_WEB_BACKENDS_FILE
35
+ ? resolve(env.CLAUDEX_WEB_BACKENDS_FILE)
36
+ : join(configDir, "backends.json"),
37
+ translationModelDir: join(
38
+ homedir(),
39
+ ".local",
40
+ "share",
41
+ "claudex-web",
42
+ "models",
43
+ ),
44
+ sessionDays,
45
+ pamService,
46
+ pamtesterPath: env.AUTH_PAMTESTER
47
+ ? resolve(env.AUTH_PAMTESTER)
48
+ : "pamtester",
49
+ tlsCert: env.TLS_CERT ? resolve(env.TLS_CERT) : null,
50
+ tlsKey: env.TLS_KEY ? resolve(env.TLS_KEY) : null,
51
+ host: env.HOST || "0.0.0.0",
52
+ port,
53
+ upstreamUrl,
54
+ codexHome,
55
+ claudeHome,
56
+ configDir,
57
+ legacyConfigDir,
58
+ projectlessRoot: join(configDir, "workspaces"),
59
+ defaultCwd: resolve(env.DEFAULT_CWD || homedir()),
60
+ staticDir: env.STATIC_DIR
61
+ ? resolve(env.STATIC_DIR)
62
+ : fileURLToPath(new URL("../../client/dist/", import.meta.url)),
63
+ };
64
+ }
@@ -0,0 +1,99 @@
1
+ import { open } from "node:fs/promises";
2
+ import { basename, isAbsolute } from "node:path";
3
+ import { pipeline } from "node:stream/promises";
4
+ import { filePresentation } from "../../shared/file-types.mjs";
5
+
6
+ export async function serveFile(request, response) {
7
+ response.setHeader("Cache-Control", "no-store");
8
+ response.setHeader("X-Content-Type-Options", "nosniff");
9
+ if (!["GET", "HEAD"].includes(request.method)) {
10
+ response.writeHead(405, { Allow: "GET, HEAD" }).end();
11
+ return;
12
+ }
13
+ const url = new URL(request.url, "http://localhost");
14
+ const path = url.searchParams.get("path");
15
+ if (!path || !isAbsolute(path) || path.includes("\0")) {
16
+ response.writeHead(400).end("Invalid file path");
17
+ return;
18
+ }
19
+ const presentation = filePresentation(path);
20
+ const download =
21
+ url.searchParams.get("download") === "1" ||
22
+ !presentation ||
23
+ presentation.kind === "download";
24
+ const type = presentation?.mime || "application/octet-stream";
25
+ const filename = encodeURIComponent(basename(path)).replace(
26
+ /['()*]/g,
27
+ (character) => "%" + character.charCodeAt(0).toString(16).toUpperCase(),
28
+ );
29
+ let file;
30
+ try {
31
+ file = await open(path, "r");
32
+ const metadata = await file.stat();
33
+ const { size } = metadata;
34
+ if (!metadata.isFile()) {
35
+ response.writeHead(404).end("File not found");
36
+ return;
37
+ }
38
+ let start = 0;
39
+ let end = size - 1;
40
+ const range = request.method === "GET" && request.headers.range;
41
+ if (range) {
42
+ const match = /^bytes=(\d*)-(\d*)$/.exec(range);
43
+ if (match && (match[1] || match[2])) {
44
+ if (match[1]) {
45
+ start = Number(match[1]);
46
+ if (match[2]) end = Math.min(Number(match[2]), end);
47
+ } else {
48
+ start = Math.max(0, size - Number(match[2]));
49
+ if (Number(match[2]) === 0) start = size;
50
+ }
51
+ }
52
+ if (
53
+ !match ||
54
+ !(match[1] || match[2]) ||
55
+ !Number.isSafeInteger(start) ||
56
+ !Number.isSafeInteger(end) ||
57
+ start < 0 ||
58
+ start >= size ||
59
+ end < start
60
+ ) {
61
+ response.writeHead(416, { "Content-Range": `bytes */${size}` }).end();
62
+ return;
63
+ }
64
+ }
65
+ response.writeHead(range ? 206 : 200, {
66
+ "Content-Type": type,
67
+ "Content-Length": size ? end - start + 1 : 0,
68
+ "Accept-Ranges": "bytes",
69
+ "Content-Disposition": `${download ? "attachment" : "inline"}; filename*=UTF-8''${filename}`,
70
+ ...(type === "image/svg+xml"
71
+ ? {
72
+ "Content-Security-Policy":
73
+ "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
74
+ }
75
+ : {}),
76
+ ...(range ? { "Content-Range": `bytes ${start}-${end}/${size}` } : {}),
77
+ });
78
+ if (request.method === "HEAD" || !size) response.end();
79
+ else
80
+ await pipeline(
81
+ file.createReadStream({ start, end, autoClose: false }),
82
+ response,
83
+ );
84
+ } catch (error) {
85
+ if (response.headersSent) response.destroy();
86
+ else
87
+ response
88
+ .writeHead(
89
+ ["ENOENT", "ENOTDIR"].includes(error.code)
90
+ ? 404
91
+ : error.code === "EACCES"
92
+ ? 403
93
+ : 500,
94
+ )
95
+ .end("Unable to load file");
96
+ } finally {
97
+ await file?.close();
98
+ }
99
+ }