@xiaobuyu/nodesign 0.0.1 → 0.0.2
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/README.md +156 -106
- package/bin/nodesign.js +0 -0
- package/package.json +1 -1
- package/server/api/README.md +45 -45
- package/server/api/_guard.js +75 -75
- package/server/api/admin.js +187 -187
- package/server/api/assets/docx-page.js +103 -103
- package/server/api/assets/helpers.js +22 -22
- package/server/api/assets/notes.js +144 -144
- package/server/api/assets.js +896 -896
- package/server/api/board.js +57 -57
- package/server/api/browse.js +138 -138
- package/server/api/canvas.js +444 -444
- package/server/api/chatai.js +324 -324
- package/server/api/exports/build-standalone.js +980 -980
- package/server/api/exports/cards.js +118 -118
- package/server/api/exports/docx-pdf.js +46 -46
- package/server/api/exports/export-page.js +142 -142
- package/server/api/exports/handoff.js +147 -147
- package/server/api/exports.js +690 -690
- package/server/api/instruction.js +75 -75
- package/server/api/me.js +116 -116
- package/server/api/memory.js +185 -185
- package/server/api/pending-changes.js +366 -366
- package/server/api/plugins.js +164 -164
- package/server/api/projects.js +220 -220
- package/server/api/publish.js +64 -64
- package/server/api/recent.js +73 -73
- package/server/api/sessions.js +589 -589
- package/server/api/skills.js +77 -77
- package/server/api/standalone-fit.js +168 -168
- package/server/api/turn-compose.js +180 -180
- package/server/api/turn-inflight.js +44 -44
- package/server/auth/README.md +17 -17
- package/server/auth/middleware.js +138 -138
- package/server/auth/origin-guard.js +71 -71
- package/server/auth/session.js +108 -108
- package/server/auth/tier.js +113 -113
- package/server/auth/users-store.js +348 -348
- package/server/edit/README.md +29 -29
- package/server/engine/README.md +113 -113
- package/server/engine/agent/agent-shared.js +550 -550
- package/server/engine/agent/auto-mode-default-hard-deny.txt +4 -4
- package/server/engine/agent/auto-mode-rules.js +78 -78
- package/server/engine/agent/context.js +418 -418
- package/server/engine/agent/events.js +464 -464
- package/server/engine/agent/hooks/canvas-validate.js +199 -199
- package/server/engine/agent/hooks/failure.js +120 -120
- package/server/engine/agent/hooks/file-events.js +75 -75
- package/server/engine/agent/hooks/lifecycle.js +233 -233
- package/server/engine/agent/hooks/post-canvas-focus.js +80 -80
- package/server/engine/agent/hooks/post-guidance.js +206 -206
- package/server/engine/agent/hooks/post-subagent-report.js +104 -104
- package/server/engine/agent/hooks/post-trim.js +58 -58
- package/server/engine/agent/hooks/pre-board-neighborhood.js +34 -34
- package/server/engine/agent/hooks/pre-defaults.js +84 -84
- package/server/engine/agent/hooks/pre-injectors.js +277 -277
- package/server/engine/agent/hooks/pre-performance-log-guard.js +78 -78
- package/server/engine/agent/hooks/pre-starter-files.js +61 -61
- package/server/engine/agent/hooks/pre-workspace-scope-guard.js +78 -78
- package/server/engine/agent/hooks/site-validate.js +94 -94
- package/server/engine/agent/hooks/tool-prompts.js +31 -31
- package/server/engine/agent/hooks/turn-state-memory.js +56 -56
- package/server/engine/agent/hooks/user-prompt-submit.js +215 -215
- package/server/engine/agent/hooks.js +354 -354
- package/server/engine/agent/init-contract.js +69 -69
- package/server/engine/agent/isolation.js +159 -159
- package/server/engine/agent/model-table.js +292 -292
- package/server/engine/agent/plugin-loader.js +266 -266
- package/server/engine/agent/prompts/nodesign-prelude.md +336 -336
- package/server/engine/agent/prompts/tools/ask-user-question-protocol.md +71 -71
- package/server/engine/agent/prompts/tools/direct-edit-protocol.md +98 -98
- package/server/engine/agent/prompts/tools/generate-image-cookbook.gemini-gateway.md +583 -583
- package/server/engine/agent/prompts/tools/generate-image-cookbook.md +483 -483
- package/server/engine/agent/prompts/tools/hybrid-reference.md +100 -100
- package/server/engine/agent/prompts/tools/paint-still-cookbook.md +270 -270
- package/server/engine/agent/prompts/tools/roll-film-cookbook.md +70 -70
- package/server/engine/agent/prompts/tools/site-reference.md +106 -106
- package/server/engine/agent/prompts/tools/tweaks-syntax.md +125 -125
- package/server/engine/agent/prompts/tools/vision-checker-dispatch.md +48 -48
- package/server/engine/agent/session-loop.js +1024 -1024
- package/server/engine/agent/session-model.js +164 -164
- package/server/engine/agent/skill.js +204 -204
- package/server/engine/agent/task-events.js +78 -78
- package/server/engine/agents/ds-extractor.md +132 -132
- package/server/engine/agents/explorer.md +190 -190
- package/server/engine/agents/index.js +230 -230
- package/server/engine/agents/schemas/design-system.json +156 -156
- package/server/engine/agents/schemas/tweak-schema.json +113 -113
- package/server/engine/agents/tweak-proposer.md +127 -127
- package/server/engine/agents/vision-checker.md +254 -254
- package/server/engine/browse/capture.js +422 -422
- package/server/engine/browse/card.js +137 -137
- package/server/engine/browse/handover.js +59 -59
- package/server/engine/browse/page-digest.js +119 -119
- package/server/engine/browse/refs.js +196 -196
- package/server/engine/browse/registry.js +307 -307
- package/server/engine/browse/screencast.js +170 -170
- package/server/engine/browse/state.js +121 -121
- package/server/engine/chatai/chat-log.js +79 -79
- package/server/engine/chatai/index.js +193 -193
- package/server/engine/chatai/openai-compat.js +152 -152
- package/server/engine/chatai/orchestrate.js +309 -309
- package/server/engine/chatai/perform.js +40 -40
- package/server/engine/chatai/summarize.js +79 -79
- package/server/engine/mcp/capability-gate.js +52 -52
- package/server/engine/mcp/index.js +339 -339
- package/server/engine/mcp/param-sanitizer.js +142 -142
- package/server/engine/mcp/tools/arrange-on-board.js +124 -124
- package/server/engine/mcp/tools/artifact-session.js +278 -278
- package/server/engine/mcp/tools/browse-computer.js +421 -421
- package/server/engine/mcp/tools/browse-find-batch.js +183 -183
- package/server/engine/mcp/tools/browse-screenshot.js +157 -157
- package/server/engine/mcp/tools/browse.js +480 -480
- package/server/engine/mcp/tools/build-docx.js +101 -101
- package/server/engine/mcp/tools/clear-pending-changes.js +99 -99
- package/server/engine/mcp/tools/create-on-board.js +155 -155
- package/server/engine/mcp/tools/crystallize-skill.js +168 -168
- package/server/engine/mcp/tools/explain-style.js +237 -237
- package/server/engine/mcp/tools/export-handoff.js +133 -133
- package/server/engine/mcp/tools/expose-tweaks.js +149 -149
- package/server/engine/mcp/tools/generate-image.js +842 -842
- package/server/engine/mcp/tools/get-computed-styles.js +164 -164
- package/server/engine/mcp/tools/get-pending-changes.js +205 -205
- package/server/engine/mcp/tools/h3box-ssh.js +84 -84
- package/server/engine/mcp/tools/helpers/acquire-page.js +82 -82
- package/server/engine/mcp/tools/helpers/motion-lab.js +554 -554
- package/server/engine/mcp/tools/helpers/motion-scroll.js +112 -112
- package/server/engine/mcp/tools/helpers/perception-page.js +215 -215
- package/server/engine/mcp/tools/helpers/reference-download.js +103 -103
- package/server/engine/mcp/tools/helpers/rembg-bridge.py +80 -80
- package/server/engine/mcp/tools/helpers/shot-pipeline.js +280 -280
- package/server/engine/mcp/tools/highlight.js +68 -68
- package/server/engine/mcp/tools/list-pages.js +222 -222
- package/server/engine/mcp/tools/lookup-tags.js +59 -59
- package/server/engine/mcp/tools/navigate-to-page.js +60 -60
- package/server/engine/mcp/tools/organize-board.js +64 -64
- package/server/engine/mcp/tools/paint-still.js +434 -434
- package/server/engine/mcp/tools/pin-to-board.js +130 -130
- package/server/engine/mcp/tools/preview-deck.js +130 -130
- package/server/engine/mcp/tools/profile-scroll.js +305 -305
- package/server/engine/mcp/tools/publish-site.js +102 -102
- package/server/engine/mcp/tools/query-elements.js +195 -195
- package/server/engine/mcp/tools/read-board.js +103 -103
- package/server/engine/mcp/tools/read-document.js +93 -93
- package/server/engine/mcp/tools/read-page.js +264 -264
- package/server/engine/mcp/tools/read-tavern-json.js +143 -143
- package/server/engine/mcp/tools/record-decision.js +140 -140
- package/server/engine/mcp/tools/relate-on-board.js +110 -110
- package/server/engine/mcp/tools/report-issue.js +115 -115
- package/server/engine/mcp/tools/roll-film.js +266 -266
- package/server/engine/mcp/tools/screenshot-docx.js +138 -138
- package/server/engine/mcp/tools/screenshot-url.js +195 -195
- package/server/engine/mcp/tools/screenshot.js +583 -583
- package/server/engine/mcp/tools/tier-gate.js +96 -96
- package/server/engine/mcp/tools/trace-motion.js +215 -215
- package/server/engine/mcp/tools/web-search.js +548 -548
- package/server/engine/motion/inventory.js +349 -349
- package/server/engine/perception/session.js +235 -235
- package/server/engine/plugins/nodesign/.claude-plugin/plugin.json +5 -5
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/SKILL.md +192 -192
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/canvas.template.html +667 -667
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/hybrid-grid.md +22 -22
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/image-led-cover.md +25 -25
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/portrait.md +23 -23
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/quote-backdrop.md +21 -21
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/section-divider.md +23 -23
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/text-led.md +21 -21
- package/server/engine/plugins/nodesign/skills/docx-craft/SKILL.md +162 -162
- package/server/engine/plugins/nodesign/skills/docx-craft/references/token-schema.md +407 -407
- package/server/engine/plugins/nodesign/skills/docx-craft//346/226/207/346/241/243.template.json +80 -80
- package/server/engine/plugins/nodesign/skills/rp-craft/SKILL.md +228 -228
- package/server/engine/plugins/nodesign/skills/rp-craft/patterns//346/274/224/345/207/272/351/241/265-/346/234/200/345/260/217/345/256/236/347/216/260.html +78 -78
- package/server/engine/plugins/nodesign/skills/rp-craft//346/274/224/345/207/272.template.js +268 -268
- package/server/engine/plugins/nodesign/skills/site-craft/SKILL.md +323 -323
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/build-lane.md +134 -134
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/cutout-collage.md +61 -61
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/mock-app.md +163 -163
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/page-transitions.md +86 -86
- package/server/engine/runs/active-runs.js +698 -698
- package/server/engine/runs/live-turn.js +252 -252
- package/server/engine/runs/store.js +364 -364
- package/server/engine/runs/turn-relay.js +217 -217
- package/server/engine/runtime/workspace.js +164 -164
- package/server/lib/artifact-file-path.js +80 -80
- package/server/lib/artifact-target.js +284 -284
- package/server/lib/asset-refs.js +239 -239
- package/server/lib/async-queue.js +104 -104
- package/server/lib/auto-relations.js +130 -130
- package/server/lib/binding-types.js +60 -60
- package/server/lib/board-kind-sizes.js +62 -62
- package/server/lib/board-relations.js +133 -133
- package/server/lib/browse-proxy.js +180 -180
- package/server/lib/canvas-id.js +36 -36
- package/server/lib/cover.js +227 -227
- package/server/lib/danbooru-tags.js +291 -291
- package/server/lib/doc-extract.js +156 -156
- package/server/lib/docx/build-from-source.js +260 -260
- package/server/lib/docx/build.js +490 -490
- package/server/lib/docx/dump-styles.js +287 -287
- package/server/lib/docx/fonts/nodesign-cjk.conf +164 -164
- package/server/lib/docx/merge-runs.js +129 -129
- package/server/lib/docx/numbering.js +218 -218
- package/server/lib/docx/order.js +152 -152
- package/server/lib/docx/rawzip.js +172 -172
- package/server/lib/docx/render.js +92 -92
- package/server/lib/docx/text-lint.js +225 -225
- package/server/lib/docx/tokens.js +335 -335
- package/server/lib/docx/units.js +29 -29
- package/server/lib/docx/xml.js +291 -291
- package/server/lib/docx-pages.js +187 -187
- package/server/lib/export-collect.js +289 -289
- package/server/lib/export-package.js +210 -210
- package/server/lib/html-srcset.js +145 -145
- package/server/lib/image-variant.js +460 -460
- package/server/lib/ingress/forward-openai-chat.js +305 -305
- package/server/lib/ingress/openai-chat.js +491 -491
- package/server/lib/ingress/session-notice.js +66 -66
- package/server/lib/ingress/session-routes.js +68 -68
- package/server/lib/ingress/slot-probe.js +130 -130
- package/server/lib/ingress/upstream-billing.js +52 -52
- package/server/lib/ingress/upstream-fail-streak.js +72 -72
- package/server/lib/ingress/upstream-truncation.js +48 -48
- package/server/lib/issues-store.js +182 -182
- package/server/lib/kinds/deck.js +72 -72
- package/server/lib/kinds/docx.js +283 -283
- package/server/lib/kinds/file-kinds.js +113 -113
- package/server/lib/kinds/index.js +202 -202
- package/server/lib/kinds/site.js +161 -161
- package/server/lib/model-ingress.js +543 -543
- package/server/lib/moderation.js +255 -255
- package/server/lib/notice-store.js +103 -103
- package/server/lib/plugin-install.js +104 -104
- package/server/lib/plugin-validator.js +721 -721
- package/server/lib/publish-store.js +115 -115
- package/server/lib/quick-summary.js +52 -52
- package/server/lib/quota.js +270 -270
- package/server/lib/rate-window.js +29 -29
- package/server/lib/reference-assets.js +92 -92
- package/server/lib/region-shot.js +135 -135
- package/server/lib/safe-path.js +73 -73
- package/server/lib/sdk-session.js +33 -33
- package/server/lib/showcase-store.js +92 -92
- package/server/lib/site-publish.js +465 -465
- package/server/lib/ssrf-guard.js +432 -432
- package/server/lib/task-scan.js +158 -158
- package/server/lib/tavern-json.js +154 -154
- package/server/lib/video-variant.js +237 -237
- package/server/lib/workspace-path.js +33 -33
- package/server/ops/fix-sdk-musl.mjs +40 -40
- package/server/ops/install-macos-fonts.sh +114 -114
- package/server/ops/macos-fonts.conf +336 -336
- package/server/ops/sandbox-shim/bwrap +57 -57
- package/server/projects/assets-summary.js +119 -119
- package/server/projects/auto-name.js +53 -53
- package/server/projects/board-store.js +640 -640
- package/server/projects/move-entry.js +103 -103
- package/server/projects/store.js +270 -270
- package/server/projects/ui-config.js +54 -54
- package/server/projects/workspace-templates.js +53 -53
- package/server/projects/workspace.js +1025 -1025
- package/server/runtime/local-config.js +177 -177
- package/server/runtime/profile.js +72 -72
- package/server/services/rembg-service.py +334 -334
- package/server/shared/README.md +25 -25
- package/server/shared/deck.js +54 -54
- package/server/shared/time.js +49 -49
- package/server/style-pipeline/README.md +53 -53
- package/server/ws/broker.js +36 -36
- package/server/ws/browse-channel.js +177 -177
- package/server/ws/index.js +386 -386
- package/web/README.md +89 -89
- package/web/dist/assets/BrowserWindow-BYCgizHw.js +11 -0
- package/web/dist/assets/{DeckWindow-4G0xX7P3.js → DeckWindow-pNydEM4L.js} +1 -1
- package/web/dist/assets/{DocxWindow-BXQsoPkh.js → DocxWindow-PPgzh3Gq.js} +1 -1
- package/web/dist/assets/{SiteWindow-D6eS-2gM.js → SiteWindow-DM0LQPCO.js} +1 -1
- package/web/dist/assets/{index-Cbl3ATvp.js → index-BHTBJlpY.js} +32 -32
- package/web/dist/assets/{pending-edit-apply-DTjo2geW.js → pending-edit-apply-C0I13CbD.js} +1 -1
- package/web/dist/assets/{rotate-cw-LMT3x1Zj.js → rotate-cw-B1UQ8Hld.js} +1 -1
- package/web/dist/assets/{square-dashed-mouse-pointer-CBoAkfhi.js → square-dashed-mouse-pointer-8lrgqmfv.js} +1 -1
- package/web/dist/index.html +17 -17
- package/web/dist/assets/BrowserWindow-Dc7J7CYc.js +0 -11
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* server/runtime/local-config.js — 用户自己的模型插槽(本地分发版)。
|
|
3
|
-
*
|
|
4
|
-
* 文件:<dataRoot>/config.json(local profile;hosted 下不读,除非 NODESIGN_MODELS_CONFIG 指了路径——给测试用)。
|
|
5
|
-
* 形状故意跟 model-table.js 的内置行**同一套字段名**:一个事实一种写法,配置页的表单、内置表的注释、
|
|
6
|
-
* 这里的 schema 三处说的是同一个东西。
|
|
7
|
-
*
|
|
8
|
-
* {
|
|
9
|
-
* "upstreams": {
|
|
10
|
-
* "myrelay": { "label": "我的中转站", "baseUrl": "https://api.example.com", "protocol": "anthropic" | "openai-chat",
|
|
11
|
-
* "authStyle": "x-api-key" | "bearer" | "none", "key": "sk-..."(或 "keyEnv": "MY_KEY"), "countTokens": false }
|
|
12
|
-
* },
|
|
13
|
-
* "models": [
|
|
14
|
-
* { "id": "kimi-k2", "label": "Kimi K2", "desc": "…", "window": 262144, "upstream": "myrelay", "wireModel": "kimi-k2-0905",
|
|
15
|
-
* "thinking": "strip", "reasoningEffort": "high", "maxOutput": 32000, "prices": { "input": 0.6, "output": 2.5 },
|
|
16
|
-
* "emptyRetries": 2, "retryBudgetMs": 120000, "fastModel": "kimi-k2", "brand": "custom" }
|
|
17
|
-
* ]
|
|
18
|
-
* }
|
|
19
|
-
*
|
|
20
|
-
* 分级校验(用户拍板「断言降级」):内置表的错仍然在 model-context.js 加载时炸;外部行的错**不炸进程**,
|
|
21
|
-
* 整条丢掉并把原因收进 errors(启动日志 + GET /api/local/config 都能看到),别的行照常可用。
|
|
22
|
-
* 配置页保存时走同一个 validate,所以用户看到的红字和启动时日志里的是同一句话。
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import fs from 'node:fs';
|
|
26
|
-
import path from 'node:path';
|
|
27
|
-
import { z } from 'zod';
|
|
28
|
-
import { profile } from './profile.js';
|
|
29
|
-
import { BRANDS, UPSTREAMS_BUILTIN, MODELS_BUILTIN } from '../engine/agent/model-table.js';
|
|
30
|
-
|
|
31
|
-
export const PROTOCOLS = Object.freeze(['anthropic', 'openai-chat']);
|
|
32
|
-
export const AUTH_STYLES = Object.freeze(['x-api-key', 'bearer', 'none']);
|
|
33
|
-
export const THINKING_MODES = Object.freeze(['strip', 'enabled8k', 'passthrough']);
|
|
34
|
-
export const REASONING_EFFORTS = Object.freeze(['low', 'medium', 'high', 'max']);
|
|
35
|
-
/** 预算上限:CLI 流式请求总超时 600s − 单发最长挂起 185s(upstream-truncation.test.js 钉的同一条线),留余量取 400s */
|
|
36
|
-
export const MAX_RETRY_BUDGET_MS = 400_000;
|
|
37
|
-
|
|
38
|
-
const ID_RE = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
|
|
39
|
-
const RESERVED_UPSTREAMS = new Set(Object.keys(UPSTREAMS_BUILTIN));
|
|
40
|
-
const RESERVED_MODELS = new Set(MODELS_BUILTIN.map((m) => m.id));
|
|
41
|
-
|
|
42
|
-
const PricesSchema = z.object({
|
|
43
|
-
input: z.number().min(0), output: z.number().min(0),
|
|
44
|
-
cacheRead: z.number().min(0).optional(), cacheWrite: z.number().min(0).optional(),
|
|
45
|
-
}).strict();
|
|
46
|
-
|
|
47
|
-
const UpstreamSchema = z.object({
|
|
48
|
-
label: z.string().trim().min(1).max(80).optional(),
|
|
49
|
-
baseUrl: z.string().trim().url().refine((u) => /^https?:\/\//.test(u), 'baseUrl 必须是 http(s)://'),
|
|
50
|
-
protocol: z.enum(PROTOCOLS).default('anthropic'),
|
|
51
|
-
authStyle: z.enum(AUTH_STYLES).optional(),
|
|
52
|
-
key: z.string().trim().min(1).optional(),
|
|
53
|
-
keyEnv: z.string().trim().regex(/^[A-Z][A-Z0-9_]*$/, 'keyEnv 必须是大写 env 变量名').optional(),
|
|
54
|
-
countTokens: z.boolean().default(false),
|
|
55
|
-
imageFormats: z.array(z.string()).optional(),
|
|
56
|
-
}).strict();
|
|
57
|
-
|
|
58
|
-
const ModelSchema = z.object({
|
|
59
|
-
id: z.string().trim().regex(ID_RE, 'id 只能是字母数字 . _ -,64 字以内'),
|
|
60
|
-
label: z.string().trim().min(1).max(60),
|
|
61
|
-
desc: z.string().trim().max(200).default(''),
|
|
62
|
-
brand: z.enum(BRANDS).default('custom'),
|
|
63
|
-
window: z.number().int().min(8_000).max(10_000_000),
|
|
64
|
-
upstream: z.string().trim().min(1),
|
|
65
|
-
wireModel: z.string().trim().min(1),
|
|
66
|
-
fastModel: z.string().trim().min(1).optional(),
|
|
67
|
-
thinking: z.enum(THINKING_MODES).default('strip'),
|
|
68
|
-
reasoningEffort: z.enum(REASONING_EFFORTS).optional(),
|
|
69
|
-
helperReasoningEffort: z.enum(REASONING_EFFORTS).optional(),
|
|
70
|
-
maxOutput: z.number().int().min(256).max(1_000_000).optional(),
|
|
71
|
-
liftImages: z.boolean().default(false),
|
|
72
|
-
prices: PricesSchema.optional(),
|
|
73
|
-
emptyRetries: z.number().int().min(0).max(10).optional(),
|
|
74
|
-
retryBudgetMs: z.number().int().min(0).max(MAX_RETRY_BUDGET_MS).optional(),
|
|
75
|
-
uncensored: z.boolean().default(false),
|
|
76
|
-
}).strict();
|
|
77
|
-
|
|
78
|
-
export const ConfigSchema = z.object({
|
|
79
|
-
upstreams: z.record(z.string(), UpstreamSchema).default({}),
|
|
80
|
-
models: z.array(ModelSchema).default([]),
|
|
81
|
-
}).strict();
|
|
82
|
-
|
|
83
|
-
/** 给配置页用的字段清单(表单按它生成,不再手写第二份) */
|
|
84
|
-
export const CONFIG_ENUMS = Object.freeze({ PROTOCOLS, AUTH_STYLES, THINKING_MODES, REASONING_EFFORTS, BRANDS, MAX_RETRY_BUDGET_MS });
|
|
85
|
-
|
|
86
|
-
function issueText(issue) {
|
|
87
|
-
return `${issue.path.join('.') || '(根)'}: ${issue.message}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 纯校验 + 归一化。不读文件、不看 profile,所以配置页保存和启动加载用的是同一个判据。
|
|
92
|
-
* @returns {{ upstreams: Record<string, object>, models: object[], errors: {where: string, message: string}[] }}
|
|
93
|
-
* upstreams 值已是 UPSTREAMS 条目形状(label/baseUrl/keyEnv/key/authStyle/protocol/countTokens/imageFormats/external)
|
|
94
|
-
* models 值是归一化后的配置条目(还不是表行;转表行在 model-context.js toExternalRow)
|
|
95
|
-
*/
|
|
96
|
-
export function validateLocalConfig(raw) {
|
|
97
|
-
const errors = [];
|
|
98
|
-
const out = { upstreams: {}, models: [], errors };
|
|
99
|
-
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
100
|
-
errors.push({ where: '(根)', message: '配置必须是一个对象 { upstreams, models }' });
|
|
101
|
-
return out;
|
|
102
|
-
}
|
|
103
|
-
// 顶层形状:分别校验每个 upstream / model,一个坏了不连坐别的
|
|
104
|
-
const top = z.object({ upstreams: z.record(z.string(), z.unknown()).default({}), models: z.array(z.unknown()).default([]) }).strict().safeParse(raw);
|
|
105
|
-
if (!top.success) {
|
|
106
|
-
for (const i of top.error.issues) errors.push({ where: '(根)', message: issueText(i) });
|
|
107
|
-
return out;
|
|
108
|
-
}
|
|
109
|
-
for (const [id, u] of Object.entries(top.data.upstreams)) {
|
|
110
|
-
const where = `upstreams.${id}`;
|
|
111
|
-
if (!ID_RE.test(id)) { errors.push({ where, message: 'upstream id 只能是字母数字 . _ -' }); continue; }
|
|
112
|
-
if (RESERVED_UPSTREAMS.has(id)) { errors.push({ where, message: `'${id}' 是内置上游名,换一个` }); continue; }
|
|
113
|
-
const r = UpstreamSchema.safeParse(u);
|
|
114
|
-
if (!r.success) { for (const i of r.error.issues) errors.push({ where, message: issueText(i) }); continue; }
|
|
115
|
-
const d = r.data;
|
|
116
|
-
const authStyle = d.authStyle || (d.protocol === 'openai-chat' ? 'bearer' : 'x-api-key');
|
|
117
|
-
if (authStyle !== 'none' && !d.key && !d.keyEnv) { errors.push({ where, message: 'key 或 keyEnv 至少填一个(authStyle 不是 none)' }); continue; }
|
|
118
|
-
out.upstreams[id] = Object.freeze({
|
|
119
|
-
label: d.label || id, baseUrl: d.baseUrl.replace(/\/+$/, ''), protocol: d.protocol, authStyle,
|
|
120
|
-
key: d.key || null, keyEnv: d.keyEnv || null, countTokens: d.countTokens,
|
|
121
|
-
...(d.imageFormats ? { imageFormats: Object.freeze(d.imageFormats) } : {}),
|
|
122
|
-
external: true,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
const seen = new Set();
|
|
126
|
-
top.data.models.forEach((m, i) => {
|
|
127
|
-
const where = `models[${i}]${m && typeof m === 'object' && m.id ? ` (${m.id})` : ''}`;
|
|
128
|
-
const r = ModelSchema.safeParse(m);
|
|
129
|
-
if (!r.success) { for (const iss of r.error.issues) errors.push({ where, message: issueText(iss) }); return; }
|
|
130
|
-
const d = r.data;
|
|
131
|
-
if (RESERVED_MODELS.has(d.id)) { errors.push({ where, message: `'${d.id}' 是内置模型名,换一个` }); return; }
|
|
132
|
-
if (seen.has(d.id)) { errors.push({ where, message: `id '${d.id}' 重复` }); return; }
|
|
133
|
-
if (!out.upstreams[d.upstream]) { errors.push({ where, message: `upstream '${d.upstream}' 不存在或没通过校验(外部模型只能指向本文件里的 upstream)` }); return; }
|
|
134
|
-
if (d.emptyRetries === 0 && d.retryBudgetMs) { errors.push({ where, message: 'emptyRetries=0 时 retryBudgetMs 没有意义' }); return; }
|
|
135
|
-
seen.add(d.id);
|
|
136
|
-
out.models.push(Object.freeze(d));
|
|
137
|
-
});
|
|
138
|
-
// fastModel 指向:只能是本文件里通过校验的模型(或自己)。在这里查而不是留给 model-context 的断言,
|
|
139
|
-
// 让用户在配置页就看见,而不是重启后日志里一行丢行
|
|
140
|
-
out.models = out.models.filter((d) => {
|
|
141
|
-
if (d.fastModel && !seen.has(d.fastModel)) { errors.push({ where: `models (${d.id})`, message: `fastModel '${d.fastModel}' 不是本文件里的模型` }); return false; }
|
|
142
|
-
return true;
|
|
143
|
-
});
|
|
144
|
-
return out;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export const configPath = process.env.NODESIGN_MODELS_CONFIG
|
|
148
|
-
? path.resolve(process.env.NODESIGN_MODELS_CONFIG)
|
|
149
|
-
: (profile.isLocal ? path.join(profile.dataRoot, 'config.json') : null);
|
|
150
|
-
|
|
151
|
-
/** 读文件 + 校验。文件不存在 = 空配置(不是错);JSON 坏 = 一条错、当空配置(进程照起,别让一个逗号把整站拉下来) */
|
|
152
|
-
export function loadLocalConfig() {
|
|
153
|
-
const empty = { upstreams: {}, models: [], errors: [], path: configPath, exists: false, raw: null };
|
|
154
|
-
if (!configPath) return empty;
|
|
155
|
-
let text;
|
|
156
|
-
try { text = fs.readFileSync(configPath, 'utf8'); } catch (err) {
|
|
157
|
-
if (err.code === 'ENOENT') return empty;
|
|
158
|
-
return { ...empty, errors: [{ where: '(文件)', message: `读不了 ${configPath}: ${err.message}` }] };
|
|
159
|
-
}
|
|
160
|
-
let raw;
|
|
161
|
-
try { raw = JSON.parse(text); } catch (err) {
|
|
162
|
-
return { ...empty, exists: true, errors: [{ where: '(文件)', message: `${configPath} 不是合法 JSON: ${err.message}` }] };
|
|
163
|
-
}
|
|
164
|
-
const v = validateLocalConfig(raw);
|
|
165
|
-
return { ...v, path: configPath, exists: true, raw };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** 配置页保存:先校验(错了也照存——用户在编辑半成品,但返回 errors 让页面标红),原子写 */
|
|
169
|
-
export function saveLocalConfig(raw) {
|
|
170
|
-
if (!configPath) throw new Error('hosted profile 没有本地配置文件');
|
|
171
|
-
const v = validateLocalConfig(raw);
|
|
172
|
-
const tmp = `${configPath}.tmp-${process.pid}`;
|
|
173
|
-
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
|
174
|
-
fs.writeFileSync(tmp, JSON.stringify(raw, null, 2) + '\n', { mode: 0o600 });
|
|
175
|
-
fs.renameSync(tmp, configPath);
|
|
176
|
-
return { ...v, path: configPath, exists: true, raw };
|
|
177
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* server/runtime/local-config.js — 用户自己的模型插槽(本地分发版)。
|
|
3
|
+
*
|
|
4
|
+
* 文件:<dataRoot>/config.json(local profile;hosted 下不读,除非 NODESIGN_MODELS_CONFIG 指了路径——给测试用)。
|
|
5
|
+
* 形状故意跟 model-table.js 的内置行**同一套字段名**:一个事实一种写法,配置页的表单、内置表的注释、
|
|
6
|
+
* 这里的 schema 三处说的是同一个东西。
|
|
7
|
+
*
|
|
8
|
+
* {
|
|
9
|
+
* "upstreams": {
|
|
10
|
+
* "myrelay": { "label": "我的中转站", "baseUrl": "https://api.example.com", "protocol": "anthropic" | "openai-chat",
|
|
11
|
+
* "authStyle": "x-api-key" | "bearer" | "none", "key": "sk-..."(或 "keyEnv": "MY_KEY"), "countTokens": false }
|
|
12
|
+
* },
|
|
13
|
+
* "models": [
|
|
14
|
+
* { "id": "kimi-k2", "label": "Kimi K2", "desc": "…", "window": 262144, "upstream": "myrelay", "wireModel": "kimi-k2-0905",
|
|
15
|
+
* "thinking": "strip", "reasoningEffort": "high", "maxOutput": 32000, "prices": { "input": 0.6, "output": 2.5 },
|
|
16
|
+
* "emptyRetries": 2, "retryBudgetMs": 120000, "fastModel": "kimi-k2", "brand": "custom" }
|
|
17
|
+
* ]
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* 分级校验(用户拍板「断言降级」):内置表的错仍然在 model-context.js 加载时炸;外部行的错**不炸进程**,
|
|
21
|
+
* 整条丢掉并把原因收进 errors(启动日志 + GET /api/local/config 都能看到),别的行照常可用。
|
|
22
|
+
* 配置页保存时走同一个 validate,所以用户看到的红字和启动时日志里的是同一句话。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import fs from 'node:fs';
|
|
26
|
+
import path from 'node:path';
|
|
27
|
+
import { z } from 'zod';
|
|
28
|
+
import { profile } from './profile.js';
|
|
29
|
+
import { BRANDS, UPSTREAMS_BUILTIN, MODELS_BUILTIN } from '../engine/agent/model-table.js';
|
|
30
|
+
|
|
31
|
+
export const PROTOCOLS = Object.freeze(['anthropic', 'openai-chat']);
|
|
32
|
+
export const AUTH_STYLES = Object.freeze(['x-api-key', 'bearer', 'none']);
|
|
33
|
+
export const THINKING_MODES = Object.freeze(['strip', 'enabled8k', 'passthrough']);
|
|
34
|
+
export const REASONING_EFFORTS = Object.freeze(['low', 'medium', 'high', 'max']);
|
|
35
|
+
/** 预算上限:CLI 流式请求总超时 600s − 单发最长挂起 185s(upstream-truncation.test.js 钉的同一条线),留余量取 400s */
|
|
36
|
+
export const MAX_RETRY_BUDGET_MS = 400_000;
|
|
37
|
+
|
|
38
|
+
const ID_RE = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
|
|
39
|
+
const RESERVED_UPSTREAMS = new Set(Object.keys(UPSTREAMS_BUILTIN));
|
|
40
|
+
const RESERVED_MODELS = new Set(MODELS_BUILTIN.map((m) => m.id));
|
|
41
|
+
|
|
42
|
+
const PricesSchema = z.object({
|
|
43
|
+
input: z.number().min(0), output: z.number().min(0),
|
|
44
|
+
cacheRead: z.number().min(0).optional(), cacheWrite: z.number().min(0).optional(),
|
|
45
|
+
}).strict();
|
|
46
|
+
|
|
47
|
+
const UpstreamSchema = z.object({
|
|
48
|
+
label: z.string().trim().min(1).max(80).optional(),
|
|
49
|
+
baseUrl: z.string().trim().url().refine((u) => /^https?:\/\//.test(u), 'baseUrl 必须是 http(s)://'),
|
|
50
|
+
protocol: z.enum(PROTOCOLS).default('anthropic'),
|
|
51
|
+
authStyle: z.enum(AUTH_STYLES).optional(),
|
|
52
|
+
key: z.string().trim().min(1).optional(),
|
|
53
|
+
keyEnv: z.string().trim().regex(/^[A-Z][A-Z0-9_]*$/, 'keyEnv 必须是大写 env 变量名').optional(),
|
|
54
|
+
countTokens: z.boolean().default(false),
|
|
55
|
+
imageFormats: z.array(z.string()).optional(),
|
|
56
|
+
}).strict();
|
|
57
|
+
|
|
58
|
+
const ModelSchema = z.object({
|
|
59
|
+
id: z.string().trim().regex(ID_RE, 'id 只能是字母数字 . _ -,64 字以内'),
|
|
60
|
+
label: z.string().trim().min(1).max(60),
|
|
61
|
+
desc: z.string().trim().max(200).default(''),
|
|
62
|
+
brand: z.enum(BRANDS).default('custom'),
|
|
63
|
+
window: z.number().int().min(8_000).max(10_000_000),
|
|
64
|
+
upstream: z.string().trim().min(1),
|
|
65
|
+
wireModel: z.string().trim().min(1),
|
|
66
|
+
fastModel: z.string().trim().min(1).optional(),
|
|
67
|
+
thinking: z.enum(THINKING_MODES).default('strip'),
|
|
68
|
+
reasoningEffort: z.enum(REASONING_EFFORTS).optional(),
|
|
69
|
+
helperReasoningEffort: z.enum(REASONING_EFFORTS).optional(),
|
|
70
|
+
maxOutput: z.number().int().min(256).max(1_000_000).optional(),
|
|
71
|
+
liftImages: z.boolean().default(false),
|
|
72
|
+
prices: PricesSchema.optional(),
|
|
73
|
+
emptyRetries: z.number().int().min(0).max(10).optional(),
|
|
74
|
+
retryBudgetMs: z.number().int().min(0).max(MAX_RETRY_BUDGET_MS).optional(),
|
|
75
|
+
uncensored: z.boolean().default(false),
|
|
76
|
+
}).strict();
|
|
77
|
+
|
|
78
|
+
export const ConfigSchema = z.object({
|
|
79
|
+
upstreams: z.record(z.string(), UpstreamSchema).default({}),
|
|
80
|
+
models: z.array(ModelSchema).default([]),
|
|
81
|
+
}).strict();
|
|
82
|
+
|
|
83
|
+
/** 给配置页用的字段清单(表单按它生成,不再手写第二份) */
|
|
84
|
+
export const CONFIG_ENUMS = Object.freeze({ PROTOCOLS, AUTH_STYLES, THINKING_MODES, REASONING_EFFORTS, BRANDS, MAX_RETRY_BUDGET_MS });
|
|
85
|
+
|
|
86
|
+
function issueText(issue) {
|
|
87
|
+
return `${issue.path.join('.') || '(根)'}: ${issue.message}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 纯校验 + 归一化。不读文件、不看 profile,所以配置页保存和启动加载用的是同一个判据。
|
|
92
|
+
* @returns {{ upstreams: Record<string, object>, models: object[], errors: {where: string, message: string}[] }}
|
|
93
|
+
* upstreams 值已是 UPSTREAMS 条目形状(label/baseUrl/keyEnv/key/authStyle/protocol/countTokens/imageFormats/external)
|
|
94
|
+
* models 值是归一化后的配置条目(还不是表行;转表行在 model-context.js toExternalRow)
|
|
95
|
+
*/
|
|
96
|
+
export function validateLocalConfig(raw) {
|
|
97
|
+
const errors = [];
|
|
98
|
+
const out = { upstreams: {}, models: [], errors };
|
|
99
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
100
|
+
errors.push({ where: '(根)', message: '配置必须是一个对象 { upstreams, models }' });
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
// 顶层形状:分别校验每个 upstream / model,一个坏了不连坐别的
|
|
104
|
+
const top = z.object({ upstreams: z.record(z.string(), z.unknown()).default({}), models: z.array(z.unknown()).default([]) }).strict().safeParse(raw);
|
|
105
|
+
if (!top.success) {
|
|
106
|
+
for (const i of top.error.issues) errors.push({ where: '(根)', message: issueText(i) });
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
for (const [id, u] of Object.entries(top.data.upstreams)) {
|
|
110
|
+
const where = `upstreams.${id}`;
|
|
111
|
+
if (!ID_RE.test(id)) { errors.push({ where, message: 'upstream id 只能是字母数字 . _ -' }); continue; }
|
|
112
|
+
if (RESERVED_UPSTREAMS.has(id)) { errors.push({ where, message: `'${id}' 是内置上游名,换一个` }); continue; }
|
|
113
|
+
const r = UpstreamSchema.safeParse(u);
|
|
114
|
+
if (!r.success) { for (const i of r.error.issues) errors.push({ where, message: issueText(i) }); continue; }
|
|
115
|
+
const d = r.data;
|
|
116
|
+
const authStyle = d.authStyle || (d.protocol === 'openai-chat' ? 'bearer' : 'x-api-key');
|
|
117
|
+
if (authStyle !== 'none' && !d.key && !d.keyEnv) { errors.push({ where, message: 'key 或 keyEnv 至少填一个(authStyle 不是 none)' }); continue; }
|
|
118
|
+
out.upstreams[id] = Object.freeze({
|
|
119
|
+
label: d.label || id, baseUrl: d.baseUrl.replace(/\/+$/, ''), protocol: d.protocol, authStyle,
|
|
120
|
+
key: d.key || null, keyEnv: d.keyEnv || null, countTokens: d.countTokens,
|
|
121
|
+
...(d.imageFormats ? { imageFormats: Object.freeze(d.imageFormats) } : {}),
|
|
122
|
+
external: true,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const seen = new Set();
|
|
126
|
+
top.data.models.forEach((m, i) => {
|
|
127
|
+
const where = `models[${i}]${m && typeof m === 'object' && m.id ? ` (${m.id})` : ''}`;
|
|
128
|
+
const r = ModelSchema.safeParse(m);
|
|
129
|
+
if (!r.success) { for (const iss of r.error.issues) errors.push({ where, message: issueText(iss) }); return; }
|
|
130
|
+
const d = r.data;
|
|
131
|
+
if (RESERVED_MODELS.has(d.id)) { errors.push({ where, message: `'${d.id}' 是内置模型名,换一个` }); return; }
|
|
132
|
+
if (seen.has(d.id)) { errors.push({ where, message: `id '${d.id}' 重复` }); return; }
|
|
133
|
+
if (!out.upstreams[d.upstream]) { errors.push({ where, message: `upstream '${d.upstream}' 不存在或没通过校验(外部模型只能指向本文件里的 upstream)` }); return; }
|
|
134
|
+
if (d.emptyRetries === 0 && d.retryBudgetMs) { errors.push({ where, message: 'emptyRetries=0 时 retryBudgetMs 没有意义' }); return; }
|
|
135
|
+
seen.add(d.id);
|
|
136
|
+
out.models.push(Object.freeze(d));
|
|
137
|
+
});
|
|
138
|
+
// fastModel 指向:只能是本文件里通过校验的模型(或自己)。在这里查而不是留给 model-context 的断言,
|
|
139
|
+
// 让用户在配置页就看见,而不是重启后日志里一行丢行
|
|
140
|
+
out.models = out.models.filter((d) => {
|
|
141
|
+
if (d.fastModel && !seen.has(d.fastModel)) { errors.push({ where: `models (${d.id})`, message: `fastModel '${d.fastModel}' 不是本文件里的模型` }); return false; }
|
|
142
|
+
return true;
|
|
143
|
+
});
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const configPath = process.env.NODESIGN_MODELS_CONFIG
|
|
148
|
+
? path.resolve(process.env.NODESIGN_MODELS_CONFIG)
|
|
149
|
+
: (profile.isLocal ? path.join(profile.dataRoot, 'config.json') : null);
|
|
150
|
+
|
|
151
|
+
/** 读文件 + 校验。文件不存在 = 空配置(不是错);JSON 坏 = 一条错、当空配置(进程照起,别让一个逗号把整站拉下来) */
|
|
152
|
+
export function loadLocalConfig() {
|
|
153
|
+
const empty = { upstreams: {}, models: [], errors: [], path: configPath, exists: false, raw: null };
|
|
154
|
+
if (!configPath) return empty;
|
|
155
|
+
let text;
|
|
156
|
+
try { text = fs.readFileSync(configPath, 'utf8'); } catch (err) {
|
|
157
|
+
if (err.code === 'ENOENT') return empty;
|
|
158
|
+
return { ...empty, errors: [{ where: '(文件)', message: `读不了 ${configPath}: ${err.message}` }] };
|
|
159
|
+
}
|
|
160
|
+
let raw;
|
|
161
|
+
try { raw = JSON.parse(text); } catch (err) {
|
|
162
|
+
return { ...empty, exists: true, errors: [{ where: '(文件)', message: `${configPath} 不是合法 JSON: ${err.message}` }] };
|
|
163
|
+
}
|
|
164
|
+
const v = validateLocalConfig(raw);
|
|
165
|
+
return { ...v, path: configPath, exists: true, raw };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 配置页保存:先校验(错了也照存——用户在编辑半成品,但返回 errors 让页面标红),原子写 */
|
|
169
|
+
export function saveLocalConfig(raw) {
|
|
170
|
+
if (!configPath) throw new Error('hosted profile 没有本地配置文件');
|
|
171
|
+
const v = validateLocalConfig(raw);
|
|
172
|
+
const tmp = `${configPath}.tmp-${process.pid}`;
|
|
173
|
+
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
|
174
|
+
fs.writeFileSync(tmp, JSON.stringify(raw, null, 2) + '\n', { mode: 0o600 });
|
|
175
|
+
fs.renameSync(tmp, configPath);
|
|
176
|
+
return { ...v, path: configPath, exists: true, raw };
|
|
177
|
+
}
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* server/runtime/profile.js — 部署形态(profile)的单一来源。**必须是 server/index.js 的第一个 import**。
|
|
3
|
-
*
|
|
4
|
-
* 两种形态(2026-08-22 用户拍板):
|
|
5
|
-
* - hosted:线上多用户站(登录墙 / 档位 / 额度 / 外审 / 邀请码 / 管理台)。不设 NODESIGN_PROFILE 就是它,
|
|
6
|
-
* 生产 .env 一个字不用改。
|
|
7
|
-
* - local :本地分发版(npx 起在用户自己机器上)。**默认单租户**:登录墙钉死关闭、请求者恒为本机 admin
|
|
8
|
-
* (auth/users-store.js LOCAL_OWNER)、SaaS 那套界面由前端按 /api/auth/status 的 profile 藏起来。
|
|
9
|
-
* 代码不删,只是藏 —— 同一份代码两种形态,靠这里一个开关分岔。
|
|
10
|
-
*
|
|
11
|
-
* 为什么要抢在所有 import 之前跑:DB_PATH / PROJECTS_DATA_DIR / WORKSPACE_DIR 这些都是各模块**加载时**从
|
|
12
|
-
* process.env 读一次就冻结的(store.js / workspace.js / runtime/workspace.js)。本地版要把它们整体挪到
|
|
13
|
-
* 用户数据目录(默认 ~/.nodesign),唯一不改那些模块的办法就是在它们加载前把 env 的默认值填好。
|
|
14
|
-
* ESM 按 import 顺序深度优先求值,所以 index.js 第一行 import 本文件即可保证顺序。
|
|
15
|
-
*
|
|
16
|
-
* 本地版的 .env 住在数据目录(<dataRoot>/.env),不住仓库根:npx 装出来的包目录不是用户该碰的地方。
|
|
17
|
-
* 已在进程 env 里的值不被文件覆盖(跟 node --env-file 同语义),命令行/bin 传的参数优先。
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import fs from 'node:fs';
|
|
21
|
-
import os from 'node:os';
|
|
22
|
-
import path from 'node:path';
|
|
23
|
-
import { fileURLToPath } from 'node:url';
|
|
24
|
-
|
|
25
|
-
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
26
|
-
|
|
27
|
-
const PROFILES = Object.freeze(['hosted', 'local']);
|
|
28
|
-
const raw = process.env.NODESIGN_PROFILE || 'hosted';
|
|
29
|
-
if (!PROFILES.includes(raw)) {
|
|
30
|
-
// 拼错不许静默落成 hosted:local 拼成 locl 会把一台笔记本的 4001 端口开到 0.0.0.0 且没有登录墙
|
|
31
|
-
throw new Error(`NODESIGN_PROFILE=${raw} 不认识,只能是 ${PROFILES.join(' | ')}`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const profileName = raw;
|
|
35
|
-
export const isLocal = raw === 'local';
|
|
36
|
-
|
|
37
|
-
/** 本地版数据根;hosted 下不用(各路径仍按老规矩各自取 env / 仓库相对路径) */
|
|
38
|
-
export const dataRoot = isLocal
|
|
39
|
-
? path.resolve(process.env.NODESIGN_DATA_DIR || path.join(os.homedir(), '.nodesign'))
|
|
40
|
-
: null;
|
|
41
|
-
|
|
42
|
-
function setDefault(name, value) {
|
|
43
|
-
if (process.env[name] === undefined || process.env[name] === '') process.env[name] = value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (isLocal) {
|
|
47
|
-
fs.mkdirSync(dataRoot, { recursive: true });
|
|
48
|
-
// 用户配置(钥匙等):<dataRoot>/.env。不存在就算了,存在但格式坏要炸(别静默跑成没钥匙)
|
|
49
|
-
const envFile = path.join(dataRoot, '.env');
|
|
50
|
-
if (fs.existsSync(envFile)) process.loadEnvFile(envFile);
|
|
51
|
-
setDefault('DB_PATH', path.join(dataRoot, 'nodesign.db'));
|
|
52
|
-
setDefault('PROJECTS_DATA_DIR', path.join(dataRoot, 'projects'));
|
|
53
|
-
setDefault('WORKSPACE_DIR', path.join(dataRoot, 'runs'));
|
|
54
|
-
setDefault('ARTIFACT_DIR', path.join(dataRoot, 'runs'));
|
|
55
|
-
setDefault('NODESIGN_CACHE_DIR', path.join(dataRoot, 'cache'));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** 服务端缓存根(封面 / 图片变体 / 视频变体 / docx 页图)。hosted = 仓库 server/.cache(与 pm2 cwd=仓库根时的老值相同) */
|
|
59
|
-
export const cacheRoot = process.env.NODESIGN_CACHE_DIR
|
|
60
|
-
? path.resolve(process.env.NODESIGN_CACHE_DIR)
|
|
61
|
-
: path.join(repoRoot, 'server', '.cache');
|
|
62
|
-
|
|
63
|
-
/** 监听地址。本地版只绑环回(没有登录墙,绝不能上 0.0.0.0);hosted 不传 = Node 默认全地址,nginx 在前面 */
|
|
64
|
-
export const listenHost = process.env.NODESIGN_HOST || (isLocal ? '127.0.0.1' : undefined);
|
|
65
|
-
|
|
66
|
-
/** 是否由 Node 直接托管前端构建产物(hosted 由 nginx 发 dist,这里关;本地版没有 nginx,开) */
|
|
67
|
-
export const serveWeb = process.env.NODESIGN_SERVE_WEB
|
|
68
|
-
? process.env.NODESIGN_SERVE_WEB === '1'
|
|
69
|
-
: isLocal;
|
|
70
|
-
export const webDistDir = path.join(repoRoot, 'web', 'dist');
|
|
71
|
-
|
|
72
|
-
export const profile = Object.freeze({ name: profileName, isLocal, dataRoot, cacheRoot, listenHost, serveWeb, webDistDir });
|
|
1
|
+
/**
|
|
2
|
+
* server/runtime/profile.js — 部署形态(profile)的单一来源。**必须是 server/index.js 的第一个 import**。
|
|
3
|
+
*
|
|
4
|
+
* 两种形态(2026-08-22 用户拍板):
|
|
5
|
+
* - hosted:线上多用户站(登录墙 / 档位 / 额度 / 外审 / 邀请码 / 管理台)。不设 NODESIGN_PROFILE 就是它,
|
|
6
|
+
* 生产 .env 一个字不用改。
|
|
7
|
+
* - local :本地分发版(npx 起在用户自己机器上)。**默认单租户**:登录墙钉死关闭、请求者恒为本机 admin
|
|
8
|
+
* (auth/users-store.js LOCAL_OWNER)、SaaS 那套界面由前端按 /api/auth/status 的 profile 藏起来。
|
|
9
|
+
* 代码不删,只是藏 —— 同一份代码两种形态,靠这里一个开关分岔。
|
|
10
|
+
*
|
|
11
|
+
* 为什么要抢在所有 import 之前跑:DB_PATH / PROJECTS_DATA_DIR / WORKSPACE_DIR 这些都是各模块**加载时**从
|
|
12
|
+
* process.env 读一次就冻结的(store.js / workspace.js / runtime/workspace.js)。本地版要把它们整体挪到
|
|
13
|
+
* 用户数据目录(默认 ~/.nodesign),唯一不改那些模块的办法就是在它们加载前把 env 的默认值填好。
|
|
14
|
+
* ESM 按 import 顺序深度优先求值,所以 index.js 第一行 import 本文件即可保证顺序。
|
|
15
|
+
*
|
|
16
|
+
* 本地版的 .env 住在数据目录(<dataRoot>/.env),不住仓库根:npx 装出来的包目录不是用户该碰的地方。
|
|
17
|
+
* 已在进程 env 里的值不被文件覆盖(跟 node --env-file 同语义),命令行/bin 传的参数优先。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import os from 'node:os';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
|
|
25
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
26
|
+
|
|
27
|
+
const PROFILES = Object.freeze(['hosted', 'local']);
|
|
28
|
+
const raw = process.env.NODESIGN_PROFILE || 'hosted';
|
|
29
|
+
if (!PROFILES.includes(raw)) {
|
|
30
|
+
// 拼错不许静默落成 hosted:local 拼成 locl 会把一台笔记本的 4001 端口开到 0.0.0.0 且没有登录墙
|
|
31
|
+
throw new Error(`NODESIGN_PROFILE=${raw} 不认识,只能是 ${PROFILES.join(' | ')}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const profileName = raw;
|
|
35
|
+
export const isLocal = raw === 'local';
|
|
36
|
+
|
|
37
|
+
/** 本地版数据根;hosted 下不用(各路径仍按老规矩各自取 env / 仓库相对路径) */
|
|
38
|
+
export const dataRoot = isLocal
|
|
39
|
+
? path.resolve(process.env.NODESIGN_DATA_DIR || path.join(os.homedir(), '.nodesign'))
|
|
40
|
+
: null;
|
|
41
|
+
|
|
42
|
+
function setDefault(name, value) {
|
|
43
|
+
if (process.env[name] === undefined || process.env[name] === '') process.env[name] = value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (isLocal) {
|
|
47
|
+
fs.mkdirSync(dataRoot, { recursive: true });
|
|
48
|
+
// 用户配置(钥匙等):<dataRoot>/.env。不存在就算了,存在但格式坏要炸(别静默跑成没钥匙)
|
|
49
|
+
const envFile = path.join(dataRoot, '.env');
|
|
50
|
+
if (fs.existsSync(envFile)) process.loadEnvFile(envFile);
|
|
51
|
+
setDefault('DB_PATH', path.join(dataRoot, 'nodesign.db'));
|
|
52
|
+
setDefault('PROJECTS_DATA_DIR', path.join(dataRoot, 'projects'));
|
|
53
|
+
setDefault('WORKSPACE_DIR', path.join(dataRoot, 'runs'));
|
|
54
|
+
setDefault('ARTIFACT_DIR', path.join(dataRoot, 'runs'));
|
|
55
|
+
setDefault('NODESIGN_CACHE_DIR', path.join(dataRoot, 'cache'));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 服务端缓存根(封面 / 图片变体 / 视频变体 / docx 页图)。hosted = 仓库 server/.cache(与 pm2 cwd=仓库根时的老值相同) */
|
|
59
|
+
export const cacheRoot = process.env.NODESIGN_CACHE_DIR
|
|
60
|
+
? path.resolve(process.env.NODESIGN_CACHE_DIR)
|
|
61
|
+
: path.join(repoRoot, 'server', '.cache');
|
|
62
|
+
|
|
63
|
+
/** 监听地址。本地版只绑环回(没有登录墙,绝不能上 0.0.0.0);hosted 不传 = Node 默认全地址,nginx 在前面 */
|
|
64
|
+
export const listenHost = process.env.NODESIGN_HOST || (isLocal ? '127.0.0.1' : undefined);
|
|
65
|
+
|
|
66
|
+
/** 是否由 Node 直接托管前端构建产物(hosted 由 nginx 发 dist,这里关;本地版没有 nginx,开) */
|
|
67
|
+
export const serveWeb = process.env.NODESIGN_SERVE_WEB
|
|
68
|
+
? process.env.NODESIGN_SERVE_WEB === '1'
|
|
69
|
+
: isLocal;
|
|
70
|
+
export const webDistDir = path.join(repoRoot, 'web', 'dist');
|
|
71
|
+
|
|
72
|
+
export const profile = Object.freeze({ name: profileName, isLocal, dataRoot, cacheRoot, listenHost, serveWeb, webDistDir });
|