@qmingjun/dsh-eda-mcp 0.0.0-stage → 1.1.12
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/AGENTS.md +63 -0
- package/CHANGELOG.md +792 -0
- package/CONTRIBUTING.md +154 -0
- package/LICENSE +55 -0
- package/README.md +290 -2
- package/README.zh.md +449 -0
- package/cordis.patch.yml +7 -0
- package/docs/design-rules.md +254 -0
- package/docs/dsh-version-sweep.md +73 -0
- package/docs/pitfalls.md +99 -0
- package/docs/project-status.md +100 -0
- package/docs/release.md +215 -0
- package/docs/schematic-workflow.md +297 -0
- package/docs/tools.md +184 -0
- package/jlc-extension/api-bridge.js +879 -0
- package/jlc-extension/extension.json +104 -0
- package/lib/bridge.mjs +4179 -0
- package/lib/client.js +1605 -0
- package/lib/extension-digest.json +6 -0
- package/lib/index.js +1261 -0
- package/package.json +144 -4
- package/src/bridge/design-intake.ts +133 -0
- package/src/bridge/design-rules.ts +228 -0
- package/src/bridge/eda-launch.ts +267 -0
- package/src/bridge/extension-package.ts +35 -0
- package/src/bridge/index.ts +3576 -0
- package/src/bridge/netlist-erc.ts +305 -0
- package/src/bridge/schematic-audit.ts +131 -0
- package/src/bridge/workflow-guide.ts +65 -0
- package/src/bridge-diagnostics.ts +185 -0
- package/src/client/EdaMcpSettingsCard.tsx +571 -0
- package/src/client/index.ts +40 -0
- package/src/client/locales.ts +83 -0
- package/src/client/settings-scope.ts +160 -0
- package/src/config-values.ts +60 -0
- package/src/guidance.ts +35 -0
- package/src/index.ts +365 -0
- package/src/loopback.ts +48 -0
- package/src/node-runtime.ts +120 -0
- package/src/routes.ts +377 -0
- package/src/skills-content.ts +309 -0
- package/src/skills.ts +91 -0
- package/test/config-values.test.ts +60 -0
- package/test/design-intake.test.ts +40 -0
- package/test/design-rules.test.ts +72 -0
- package/test/eda-launch.test.ts +114 -0
- package/test/extension-package.test.ts +27 -0
- package/test/extension-routes.test.ts +116 -0
- package/test/netlist-erc.test.ts +90 -0
- package/test/node-runtime.test.ts +65 -0
- package/test/schematic-audit.test.ts +71 -0
- package/test/seams.test.ts +164 -0
- package/test/skills.test.ts +68 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { delimiter, dirname, join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* How the bridge child process is launched. `source` names the rule that chose
|
|
6
|
+
* the executable, so a failed spawn can be diagnosed from the route response.
|
|
7
|
+
*/
|
|
8
|
+
export interface BridgeRuntime {
|
|
9
|
+
/** Executable that runs the bridge script. */
|
|
10
|
+
command: string
|
|
11
|
+
/** Environment that must be added for `command` to behave as Node. */
|
|
12
|
+
extraEnv: Record<string, string>
|
|
13
|
+
/** Human-readable rule that produced this executable. */
|
|
14
|
+
source: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Inputs the resolution reads; injected so the rules are unit-testable. */
|
|
18
|
+
export interface RuntimeProbe {
|
|
19
|
+
env?: Record<string, string | undefined>
|
|
20
|
+
execPath?: string
|
|
21
|
+
versions?: { electron?: string }
|
|
22
|
+
exists?: (path: string) => boolean
|
|
23
|
+
pathDelimiter?: string
|
|
24
|
+
/** Home directory used to locate the per-user DSH runtime; defaults to the environment. */
|
|
25
|
+
home?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Node locations inside the per-user DSH runtime directory (`~/.dsh/dsh-runtimes`).
|
|
30
|
+
*
|
|
31
|
+
* This copy is the same payload as the one inside the application directory, but
|
|
32
|
+
* the installer never rewrites it. A bridge that keeps running on the
|
|
33
|
+
* application copy holds a lock on a file the updater must replace, and the
|
|
34
|
+
* desktop app then refuses the update ("update not installed") — so the user
|
|
35
|
+
* copy is probed first whenever it exists.
|
|
36
|
+
*/
|
|
37
|
+
const USER_NODE_CANDIDATES = [
|
|
38
|
+
['dsh-runtimes', 'dsh-primary-runtime', 'dependencies', 'node', 'bin', 'node.exe'],
|
|
39
|
+
['dsh-runtimes', 'dsh-primary-runtime', 'dependencies', 'node', 'bin', 'node'],
|
|
40
|
+
] as const
|
|
41
|
+
|
|
42
|
+
/** Bundled Node locations shipped inside the DSH desktop payload. */
|
|
43
|
+
const DESKTOP_NODE_CANDIDATES = [
|
|
44
|
+
['resources', 'runtime', 'primary-runtime', 'dependencies', 'node', 'bin', 'node.exe'],
|
|
45
|
+
['resources', 'runtime', 'primary-runtime', 'dependencies', 'node', 'bin', 'node'],
|
|
46
|
+
['resources', 'runtime', 'bin', 'node.exe'],
|
|
47
|
+
['resources', 'runtime', 'node', 'node.exe'],
|
|
48
|
+
['resources', 'node', 'node.exe'],
|
|
49
|
+
] as const
|
|
50
|
+
|
|
51
|
+
/** Executable names to look for when falling back to `PATH`. */
|
|
52
|
+
const PATH_NODE_NAMES = process.platform === 'win32' ? ['node.exe'] : ['node']
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Pick the executable that must run `lib/bridge.mjs`.
|
|
56
|
+
*
|
|
57
|
+
* The bridge is a plain Node program, so on a Node host `process.execPath` is
|
|
58
|
+
* exact. Under the DSH desktop app the host process is Electron, where
|
|
59
|
+
* `process.execPath` is `DeepSeek Harness.exe`: spawning it starts (or
|
|
60
|
+
* single-instance-aborts) a second app instead of running the script, which is
|
|
61
|
+
* why the bridge never listened. The desktop payload ships a real Node, so it is
|
|
62
|
+
* used; the per-user runtime copy is preferred over the application copy because
|
|
63
|
+
* only the latter is part of the installer payload (see USER_NODE_CANDIDATES).
|
|
64
|
+
* The platform launcher variables and an explicit override come before both, and
|
|
65
|
+
* Electron-as-Node (`ELECTRON_RUN_AS_NODE=1`) is the last resort.
|
|
66
|
+
*/
|
|
67
|
+
export function resolveBridgeRuntime(probe: RuntimeProbe = {}): BridgeRuntime {
|
|
68
|
+
const env = probe.env ?? process.env
|
|
69
|
+
const execPath = probe.execPath ?? process.execPath
|
|
70
|
+
const exists = probe.exists ?? existsSync
|
|
71
|
+
const isElectron = (probe.versions ?? process.versions).electron !== undefined
|
|
72
|
+
|
|
73
|
+
const override = env.DSH_EDA_MCP_NODE
|
|
74
|
+
if (override !== undefined && override !== '' && exists(override)) {
|
|
75
|
+
return { command: override, extraEnv: {}, source: 'DSH_EDA_MCP_NODE override' }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The desktop launcher exports the app binary under these names and expects
|
|
79
|
+
// callers to set ELECTRON_RUN_AS_NODE (see its own runtime/bin/node.cmd).
|
|
80
|
+
for (const name of ['DSH_NODE_EXECUTABLE', 'DSH_DESKTOP_NODE_EXECUTABLE']) {
|
|
81
|
+
const value = env[name]
|
|
82
|
+
if (value !== undefined && value !== '' && exists(value)) {
|
|
83
|
+
return { command: value, extraEnv: { ELECTRON_RUN_AS_NODE: '1' }, source: `${name}` }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!isElectron) {
|
|
88
|
+
return { command: execPath, extraEnv: {}, source: 'process.execPath (Node host)' }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const home = probe.home ?? env.DSH_HOME ?? env.USERPROFILE ?? env.HOME
|
|
92
|
+
if (home !== undefined && home !== '') {
|
|
93
|
+
for (const parts of USER_NODE_CANDIDATES) {
|
|
94
|
+
const candidate = join(home, '.dsh', ...parts)
|
|
95
|
+
if (exists(candidate)) return { command: candidate, extraEnv: {}, source: 'per-user DSH runtime Node' }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const root = dirname(execPath)
|
|
100
|
+
for (const parts of DESKTOP_NODE_CANDIDATES) {
|
|
101
|
+
const candidate = join(root, ...parts)
|
|
102
|
+
if (exists(candidate)) return { command: candidate, extraEnv: {}, source: 'desktop runtime Node' }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const pathValue = env.PATH ?? env.Path ?? ''
|
|
106
|
+
for (const entry of pathValue.split(probe.pathDelimiter ?? delimiter)) {
|
|
107
|
+
const dir = entry.replace(/^"|"$/g, '').trim()
|
|
108
|
+
if (dir === '') continue
|
|
109
|
+
for (const name of PATH_NODE_NAMES) {
|
|
110
|
+
const candidate = join(dir, name)
|
|
111
|
+
if (exists(candidate)) return { command: candidate, extraEnv: {}, source: 'PATH Node' }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
command: execPath,
|
|
117
|
+
extraEnv: { ELECTRON_RUN_AS_NODE: '1' },
|
|
118
|
+
source: 'process.execPath + ELECTRON_RUN_AS_NODE (Electron as Node)',
|
|
119
|
+
}
|
|
120
|
+
}
|
package/src/routes.ts
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { basename } from 'node:path'
|
|
3
|
+
import type { ServerResponse } from 'node:http'
|
|
4
|
+
import type { WebRoute } from '@deepseek-ai/dsh-host-webserver'
|
|
5
|
+
import { SettingsConflictError, type SettingsPathOp } from '@deepseek-ai/dsh-settings'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* One namespace view as the settings service serializes it. Structural on
|
|
9
|
+
* purpose: 0.1.x `SettingsProvider` and 0.2 `SettingsForms` both return these
|
|
10
|
+
* fields (0.2 adds `autoGenerate`/`applies`), so one build spans generations.
|
|
11
|
+
*/
|
|
12
|
+
export interface SettingsBridgeView {
|
|
13
|
+
ns: unknown
|
|
14
|
+
schema?: unknown
|
|
15
|
+
value?: unknown
|
|
16
|
+
base?: unknown
|
|
17
|
+
user?: unknown
|
|
18
|
+
revision: number
|
|
19
|
+
secrets?: Array<{ path: readonly string[]; set: boolean }>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The provider face this loopback bridge uses, on every settings generation. */
|
|
23
|
+
export interface SettingsBridge {
|
|
24
|
+
readonly writable?: boolean
|
|
25
|
+
describe: (options?: { redactSecrets?: boolean }) => SettingsBridgeView[]
|
|
26
|
+
mutate: (ns: string, ops: readonly SettingsPathOp[], expectedRevision?: number) => Promise<void>
|
|
27
|
+
}
|
|
28
|
+
import type { EdaMcpConfig } from './index.ts'
|
|
29
|
+
import { isLoopbackRequest } from './loopback.ts'
|
|
30
|
+
import { compareDigest, extensionDownloadUrl, sha256Of } from './bridge/extension-package.ts'
|
|
31
|
+
|
|
32
|
+
export const EDA_MCP_API = {
|
|
33
|
+
test: '/api/dsh-eda-mcp/test',
|
|
34
|
+
reconnect: '/api/dsh-eda-mcp/reconnect',
|
|
35
|
+
extensionZip: '/api/dsh-eda-mcp/extension.zip',
|
|
36
|
+
extensionVerify: '/api/dsh-eda-mcp/extension/verify',
|
|
37
|
+
extensionInfo: '/api/dsh-eda-mcp/extension/info',
|
|
38
|
+
diagnostics: '/api/dsh-eda-mcp/diagnostics',
|
|
39
|
+
selfTest: '/api/dsh-eda-mcp/self-test',
|
|
40
|
+
settingsDescribe: '/api/dsh-eda-mcp/settings/describe',
|
|
41
|
+
settingsMutate: '/api/dsh-eda-mcp/settings/mutate',
|
|
42
|
+
} as const
|
|
43
|
+
|
|
44
|
+
const MAX_BODY_BYTES = 64 * 1024
|
|
45
|
+
|
|
46
|
+
function json(res: ServerResponse, status: number, body: unknown): void {
|
|
47
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' })
|
|
48
|
+
res.end(JSON.stringify(body))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function readJsonBody(req: Parameters<WebRoute['handler']>[0]): Promise<unknown> {
|
|
52
|
+
const chunks: Buffer[] = []
|
|
53
|
+
let size = 0
|
|
54
|
+
for await (const chunk of req) {
|
|
55
|
+
const buffer = chunk as Buffer
|
|
56
|
+
size += buffer.length
|
|
57
|
+
if (size > MAX_BODY_BYTES) return undefined
|
|
58
|
+
chunks.push(buffer)
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
62
|
+
} catch {
|
|
63
|
+
return undefined
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Whether a settings write was refused as stale. Class check plus the stable
|
|
69
|
+
* machine code, because the throwing host may run a different copy of
|
|
70
|
+
* `@deepseek-ai/dsh-settings` than this plugin's own dependency — a cross-copy
|
|
71
|
+
* `instanceof` is false even when both copies are the same version.
|
|
72
|
+
*/
|
|
73
|
+
function isSettingsConflict(error: unknown): error is SettingsConflictError {
|
|
74
|
+
if (error instanceof SettingsConflictError) return true
|
|
75
|
+
return (error as { code?: unknown } | null)?.code === 'SETTINGS_CONFLICT'
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function toView(descriptor: SettingsBridgeView): unknown {
|
|
79
|
+
return {
|
|
80
|
+
ns: String(descriptor.ns),
|
|
81
|
+
schema: descriptor.schema,
|
|
82
|
+
value: descriptor.value,
|
|
83
|
+
...(descriptor.base === undefined ? {} : { base: descriptor.base }),
|
|
84
|
+
...(descriptor.user === undefined ? {} : { user: descriptor.user }),
|
|
85
|
+
...(descriptor.secrets === undefined ? {} : { secrets: descriptor.secrets.map(secret => ({ path: [...secret.path], set: secret.set })) }),
|
|
86
|
+
revision: descriptor.revision,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface EdaMcpRouteDeps {
|
|
91
|
+
getConfig: () => EdaMcpConfig
|
|
92
|
+
extensionDigestPath: string
|
|
93
|
+
/** Wire version of the bundled extension zip (from jlc-extension/extension.json). */
|
|
94
|
+
extensionVersion: string
|
|
95
|
+
/** Base URL of the download server; override with DSH_EDA_EXTENSION_BASE. */
|
|
96
|
+
extensionDownloadBase: string
|
|
97
|
+
/** Dispose and recreate the MCP bridge child, then return immediately. */
|
|
98
|
+
reconnect: () => void
|
|
99
|
+
/** Which executable and script the bridge child is started with (diagnostics). */
|
|
100
|
+
bridgeLaunch?: () => { command: string; args: string[]; source: string }
|
|
101
|
+
/** Live record of the MCP client mount and the bridge health probes. */
|
|
102
|
+
diagnostics?: () => unknown
|
|
103
|
+
/** The exact config handed to the MCP client (a validation failure is visible here). */
|
|
104
|
+
launchConfig?: () => unknown
|
|
105
|
+
/** Spawn the bridge from the plugin host process itself and report what happened. */
|
|
106
|
+
selfTest?: () => Promise<unknown>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function makeEdaMcpRoutes(deps: EdaMcpRouteDeps): WebRoute[] {
|
|
110
|
+
const diagnostics: WebRoute = {
|
|
111
|
+
kind: 'exact',
|
|
112
|
+
path: EDA_MCP_API.diagnostics,
|
|
113
|
+
handler: async (req, res): Promise<void> => {
|
|
114
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
115
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
116
|
+
json(res, 200, {
|
|
117
|
+
ok: true,
|
|
118
|
+
launch: deps.bridgeLaunch?.() ?? null,
|
|
119
|
+
diagnostics: deps.diagnostics?.() ?? null,
|
|
120
|
+
config: deps.getConfig(),
|
|
121
|
+
launchConfig: deps.launchConfig?.() ?? null,
|
|
122
|
+
})
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const selfTest: WebRoute = {
|
|
127
|
+
kind: 'exact',
|
|
128
|
+
path: EDA_MCP_API.selfTest,
|
|
129
|
+
handler: async (req, res): Promise<void> => {
|
|
130
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
131
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
132
|
+
if (deps.selfTest === undefined) return json(res, 501, { ok: false, error: 'not-supported' })
|
|
133
|
+
const result = await deps.selfTest()
|
|
134
|
+
json(res, 200, { ok: true, result })
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const test: WebRoute = {
|
|
139
|
+
kind: 'exact',
|
|
140
|
+
path: EDA_MCP_API.test,
|
|
141
|
+
handler: async (req, res): Promise<void> => {
|
|
142
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
143
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
144
|
+
const port = 39009
|
|
145
|
+
const launch = deps.bridgeLaunch?.()
|
|
146
|
+
try {
|
|
147
|
+
const response = await fetch(`http://127.0.0.1:${port}/healthz`, { signal: AbortSignal.timeout(3000) })
|
|
148
|
+
if (!response.ok) return json(res, 502, { ok: false, error: 'bridge-unhealthy', port, launch })
|
|
149
|
+
const health = await response.json() as { status?: string; clients?: number }
|
|
150
|
+
json(res, 200, { ok: true, bridgePort: port, health, launch })
|
|
151
|
+
} catch (error) {
|
|
152
|
+
json(res, 503, {
|
|
153
|
+
ok: false, error: 'bridge-unreachable', port, launch,
|
|
154
|
+
message: error instanceof Error ? error.message : String(error),
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const reconnect: WebRoute = {
|
|
161
|
+
kind: 'exact',
|
|
162
|
+
path: EDA_MCP_API.reconnect,
|
|
163
|
+
handler: async (req, res): Promise<void> => {
|
|
164
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
165
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
166
|
+
const port = 39009
|
|
167
|
+
const launch = deps.bridgeLaunch?.()
|
|
168
|
+
try {
|
|
169
|
+
deps.reconnect()
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return json(res, 500, { ok: false, error: 'reconnect-failed', launch, message: error instanceof Error ? error.message : String(error) })
|
|
172
|
+
}
|
|
173
|
+
// Give the freshly spawned bridge child a moment to bind its socket.
|
|
174
|
+
await new Promise(resolve => setTimeout(resolve, 1200))
|
|
175
|
+
try {
|
|
176
|
+
const response = await fetch(`http://127.0.0.1:${port}/healthz`, { signal: AbortSignal.timeout(3000) })
|
|
177
|
+
if (!response.ok) return json(res, 502, { ok: false, error: 'bridge-unhealthy', port, launch, message: 'reconnect completed but the bridge health endpoint is not OK' })
|
|
178
|
+
const health = await response.json() as { status?: string; version?: string; clients?: number }
|
|
179
|
+
json(res, 200, { ok: true, bridgePort: port, health, launch, message: 'reconnect completed' })
|
|
180
|
+
} catch (error) {
|
|
181
|
+
json(res, 503, {
|
|
182
|
+
ok: false, error: 'bridge-unreachable', port, launch,
|
|
183
|
+
message: error instanceof Error ? error.message : String(error),
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The package ships no zip any more (only lib/extension-digest.json), so the card downloads
|
|
190
|
+
// from the download server. Every served copy is compared against the build-recorded digest
|
|
191
|
+
// first: a different wire version is refused rather than handed to the user, and a 429 is
|
|
192
|
+
// reported as throttling instead of looking like a broken artifact.
|
|
193
|
+
const readExtensionDigest = async (): Promise<{ version: string; file: string; bytes: number; sha256: string } | null> => {
|
|
194
|
+
try {
|
|
195
|
+
const parsed = JSON.parse(await readFile(deps.extensionDigestPath, 'utf8')) as {
|
|
196
|
+
version?: string; file?: string; bytes?: number; sha256?: string
|
|
197
|
+
}
|
|
198
|
+
if (typeof parsed.version !== 'string' || typeof parsed.sha256 !== 'string' || typeof parsed.file !== 'string') return null
|
|
199
|
+
return { version: parsed.version, file: parsed.file, bytes: Number(parsed.bytes ?? 0), sha256: parsed.sha256 }
|
|
200
|
+
} catch {
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const extensionZip: WebRoute = {
|
|
206
|
+
kind: 'exact',
|
|
207
|
+
path: EDA_MCP_API.extensionZip,
|
|
208
|
+
handler: async (req, res): Promise<void> => {
|
|
209
|
+
if (req.method !== 'GET') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
210
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
211
|
+
const digest = await readExtensionDigest()
|
|
212
|
+
if (digest === null) {
|
|
213
|
+
return json(res, 503, { ok: false, error: 'lib/extension-digest.json is missing; run the build before installing the plugin' })
|
|
214
|
+
}
|
|
215
|
+
const requested = new URL(req.url ?? '/', 'http://127.0.0.1').searchParams.get('source')
|
|
216
|
+
const url = extensionDownloadUrl(deps.extensionDownloadBase, digest.version)
|
|
217
|
+
let response: Response
|
|
218
|
+
try {
|
|
219
|
+
response = await fetch(url, { signal: AbortSignal.timeout(30000) })
|
|
220
|
+
} catch (error) {
|
|
221
|
+
return json(res, 502, {
|
|
222
|
+
ok: false,
|
|
223
|
+
error: 'could not reach the download server: ' + (error instanceof Error ? error.message : String(error)),
|
|
224
|
+
url,
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
if (!response.ok) {
|
|
228
|
+
const throttled = response.status === 429
|
|
229
|
+
return json(res, throttled ? 429 : 502, {
|
|
230
|
+
ok: false,
|
|
231
|
+
httpStatus: response.status,
|
|
232
|
+
url,
|
|
233
|
+
error: throttled
|
|
234
|
+
? 'the download server throttled this IP (5 downloads/min per IP). Wait about a minute and click download again.'
|
|
235
|
+
: 'the download server answered HTTP ' + response.status,
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
const bytes = Buffer.from(new Uint8Array(await response.arrayBuffer()))
|
|
239
|
+
const servedSha256 = sha256Of(bytes)
|
|
240
|
+
const verdict = compareDigest(digest.sha256, servedSha256)
|
|
241
|
+
if (verdict !== 'match') {
|
|
242
|
+
return json(res, 502, {
|
|
243
|
+
ok: false,
|
|
244
|
+
url,
|
|
245
|
+
verified: verdict,
|
|
246
|
+
expected: { version: digest.version, sha256: digest.sha256, bytes: digest.bytes },
|
|
247
|
+
served: { sha256: servedSha256, bytes: bytes.byteLength },
|
|
248
|
+
error: 'the served zip is not the build this plugin ships (wire version ' + digest.version + '); '
|
|
249
|
+
+ 'refusing to hand out a different version. Re-upload ' + digest.file + ' to the download server.',
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
res.writeHead(200, {
|
|
253
|
+
'content-type': 'application/zip',
|
|
254
|
+
'content-disposition': 'attachment; filename="' + digest.file + '"',
|
|
255
|
+
'cache-control': 'no-store',
|
|
256
|
+
'content-length': String(bytes.byteLength),
|
|
257
|
+
'x-extension-source': 'server',
|
|
258
|
+
'x-extension-version': digest.version,
|
|
259
|
+
'x-extension-sha256': servedSha256,
|
|
260
|
+
...(requested === 'local' ? { 'x-extension-note': 'source=local requested; the served copy was verified and used' } : {}),
|
|
261
|
+
})
|
|
262
|
+
res.end(bytes)
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const extensionInfo: WebRoute = {
|
|
267
|
+
kind: 'exact',
|
|
268
|
+
path: EDA_MCP_API.extensionInfo,
|
|
269
|
+
handler: async (req, res): Promise<void> => {
|
|
270
|
+
if (req.method !== 'GET') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
271
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
272
|
+
const digest = await readExtensionDigest()
|
|
273
|
+
if (digest === null) return json(res, 503, { ok: false, error: 'lib/extension-digest.json is missing' })
|
|
274
|
+
json(res, 200, {
|
|
275
|
+
version: digest.version,
|
|
276
|
+
file: digest.file,
|
|
277
|
+
bytes: digest.bytes,
|
|
278
|
+
sha256: digest.sha256,
|
|
279
|
+
downloadPath: EDA_MCP_API.extensionZip,
|
|
280
|
+
serverUrl: extensionDownloadUrl(deps.extensionDownloadBase, digest.version),
|
|
281
|
+
source: 'server',
|
|
282
|
+
})
|
|
283
|
+
},
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const extensionVerify: WebRoute = {
|
|
287
|
+
kind: 'exact',
|
|
288
|
+
path: EDA_MCP_API.extensionVerify,
|
|
289
|
+
handler: async (req, res): Promise<void> => {
|
|
290
|
+
if (req.method !== 'GET') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
291
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
292
|
+
const digest = await readExtensionDigest()
|
|
293
|
+
if (digest === null) return json(res, 503, { ok: false, error: 'lib/extension-digest.json is missing' })
|
|
294
|
+
const url = extensionDownloadUrl(deps.extensionDownloadBase, digest.version)
|
|
295
|
+
const report = { version: digest.version, file: digest.file, expected: { bytes: digest.bytes, sha256: digest.sha256 }, url }
|
|
296
|
+
let response: Response
|
|
297
|
+
try {
|
|
298
|
+
response = await fetch(url, { signal: AbortSignal.timeout(30000) })
|
|
299
|
+
} catch (error) {
|
|
300
|
+
return json(res, 200, { ...report, verified: 'unverified', error: 'download failed: ' + (error instanceof Error ? error.message : String(error)) })
|
|
301
|
+
}
|
|
302
|
+
if (!response.ok) {
|
|
303
|
+
return json(res, 200, {
|
|
304
|
+
...report,
|
|
305
|
+
verified: 'unverified',
|
|
306
|
+
httpStatus: response.status,
|
|
307
|
+
error: response.status === 429
|
|
308
|
+
? 'the download server throttled this IP (HTTP 429, 5 downloads/min). Wait about a minute and verify again.'
|
|
309
|
+
: 'download failed: HTTP ' + response.status,
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
const servedBytes = new Uint8Array(await response.arrayBuffer())
|
|
313
|
+
const servedSha256 = sha256Of(servedBytes)
|
|
314
|
+
return json(res, 200, {
|
|
315
|
+
...report,
|
|
316
|
+
served: { bytes: servedBytes.byteLength, sha256: servedSha256 },
|
|
317
|
+
verified: compareDigest(digest.sha256, servedSha256),
|
|
318
|
+
})
|
|
319
|
+
},
|
|
320
|
+
}
|
|
321
|
+
return [test, reconnect, extensionZip, extensionInfo, extensionVerify, diagnostics, selfTest]
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function makeEdaMcpSettingsRoutes(settings: SettingsBridge): WebRoute[] {
|
|
325
|
+
const ns = 'eda-mcp'
|
|
326
|
+
const view = (): SettingsBridgeView | undefined => {
|
|
327
|
+
const descriptors = settings.describe({ redactSecrets: true })
|
|
328
|
+
return descriptors.find(descriptor => String(descriptor.ns) === 'eda-mcp')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const describe: WebRoute = {
|
|
332
|
+
kind: 'exact',
|
|
333
|
+
path: EDA_MCP_API.settingsDescribe,
|
|
334
|
+
handler: async (req, res): Promise<void> => {
|
|
335
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
336
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
337
|
+
const descriptor = view()
|
|
338
|
+
json(res, 200, {
|
|
339
|
+
ok: true,
|
|
340
|
+
value: {
|
|
341
|
+
namespaces: descriptor === undefined ? [] : [toView(descriptor)],
|
|
342
|
+
writable: settings.writable !== false,
|
|
343
|
+
},
|
|
344
|
+
})
|
|
345
|
+
},
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const mutate: WebRoute = {
|
|
349
|
+
kind: 'exact',
|
|
350
|
+
path: EDA_MCP_API.settingsMutate,
|
|
351
|
+
handler: async (req, res): Promise<void> => {
|
|
352
|
+
if (req.method !== 'POST') return json(res, 405, { ok: false, error: 'method-not-allowed' })
|
|
353
|
+
if (!isLoopbackRequest(req)) return json(res, 403, { ok: false, error: 'forbidden' })
|
|
354
|
+
const body = await readJsonBody(req)
|
|
355
|
+
if (body === undefined || typeof body !== 'object' || body === null) {
|
|
356
|
+
return json(res, 400, { ok: false, code: 'settings-rejected', message: 'unreadable JSON body' })
|
|
357
|
+
}
|
|
358
|
+
const record = body as { ns?: unknown; ops?: unknown; expectedRevision?: unknown }
|
|
359
|
+
if (record.ns !== 'eda-mcp' || !Array.isArray(record.ops)) {
|
|
360
|
+
return json(res, 400, { ok: false, code: 'settings-rejected', message: 'bad settings mutation' })
|
|
361
|
+
}
|
|
362
|
+
const ops = record.ops as SettingsPathOp[]
|
|
363
|
+
try {
|
|
364
|
+
await settings.mutate(ns, ops, typeof record.expectedRevision === 'number' ? record.expectedRevision : undefined)
|
|
365
|
+
const descriptor = view()
|
|
366
|
+
json(res, 200, { ok: true, value: descriptor === undefined ? undefined : toView(descriptor) })
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (isSettingsConflict(error)) {
|
|
369
|
+
return json(res, 409, { ok: false, code: error.code, message: error.message, expected: error.expected, actual: error.actual })
|
|
370
|
+
}
|
|
371
|
+
json(res, 400, { ok: false, code: 'settings-rejected', message: error instanceof Error ? error.message : String(error) })
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return [describe, mutate]
|
|
377
|
+
}
|