dsh-reverse-engine 0.0.0-stage → 0.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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +124 -2
- package/README.zh-CN.md +108 -0
- package/client.js +1509 -0
- package/cordis.patch.yml +6 -0
- package/docs/ARCHITECTURE.md +114 -0
- package/docs/CONTRACT.md +91 -0
- package/docs/VERIFICATION.md +55 -0
- package/icon.svg +23 -0
- package/index.js +218 -0
- package/lib/catalog.js +123 -0
- package/lib/clients.js +206 -0
- package/lib/engine.js +488 -0
- package/lib/http-endpoint.js +153 -0
- package/lib/rea-catalog.json +671 -0
- package/lib/rea-clients.json +177 -0
- package/lib/routes.js +212 -0
- package/lib/runtime.js +299 -0
- package/lib/service.js +454 -0
- package/lib/settings.js +165 -0
- package/lib/tools.js +245 -0
- package/lib/util.js +312 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
{
|
|
2
|
+
"count": 43,
|
|
3
|
+
"clients": [
|
|
4
|
+
{
|
|
5
|
+
"id": "claude_code",
|
|
6
|
+
"label": "Claude Code"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"id": "claude_desktop",
|
|
10
|
+
"label": "Claude Desktop"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"id": "codex",
|
|
14
|
+
"label": "Codex"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"id": "cursor",
|
|
18
|
+
"label": "Cursor"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"id": "codebuddy",
|
|
22
|
+
"label": "CodeBuddy"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"id": "cline",
|
|
26
|
+
"label": "Cline CLI"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"id": "gemini_cli",
|
|
30
|
+
"label": "Gemini CLI"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"id": "windsurf",
|
|
34
|
+
"label": "Windsurf"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"id": "devin",
|
|
38
|
+
"label": "Devin"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"id": "opencode",
|
|
42
|
+
"label": "OpenCode"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"id": "antigravity",
|
|
46
|
+
"label": "Antigravity"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"id": "copilot_cli",
|
|
50
|
+
"label": "GitHub Copilot"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"id": "commandcode",
|
|
54
|
+
"label": "Command Code"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"id": "qwen_code",
|
|
58
|
+
"label": "Qwen Code"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"id": "kimi",
|
|
62
|
+
"label": "Kimi Code"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": "vscode",
|
|
66
|
+
"label": "VS Code (Local harness)"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"id": "grok_build",
|
|
70
|
+
"label": "Grok Build"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"id": "omp",
|
|
74
|
+
"label": "OMP"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"id": "pi",
|
|
78
|
+
"label": "Pi"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"id": "hermes",
|
|
82
|
+
"label": "Hermes"
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"id": "qoder",
|
|
86
|
+
"label": "Qoder"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"id": "droid",
|
|
90
|
+
"label": "Factory Droid"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"id": "trae",
|
|
94
|
+
"label": "Trae"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"id": "workbuddy",
|
|
98
|
+
"label": "WorkBuddy"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"id": "lm_studio",
|
|
102
|
+
"label": "LM Studio"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"id": "zcode",
|
|
106
|
+
"label": "ZCode"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"id": "dsh",
|
|
110
|
+
"label": "DeepSeek Harness"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"id": "freebuff",
|
|
114
|
+
"label": "FreeBuff"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
"id": "cline_vscode",
|
|
118
|
+
"label": "Cline VS Code extension"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
"id": "cline_vscode_insiders",
|
|
122
|
+
"label": "Cline in VS Code Insiders"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"id": "cline_vscodium",
|
|
126
|
+
"label": "Cline in VSCodium"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
"id": "cline_cursor",
|
|
130
|
+
"label": "Cline in Cursor"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"id": "cline_windsurf",
|
|
134
|
+
"label": "Cline in Windsurf"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"id": "kiro",
|
|
138
|
+
"label": "Kiro"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"id": "mastracode",
|
|
142
|
+
"label": "Mastra Code"
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"id": "zed",
|
|
146
|
+
"label": "Zed"
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
"id": "amp",
|
|
150
|
+
"label": "Amp"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"id": "mcporter",
|
|
154
|
+
"label": "MCPorter"
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"id": "fx",
|
|
158
|
+
"label": "fx"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"id": "junie",
|
|
162
|
+
"label": "Junie"
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"id": "goose",
|
|
166
|
+
"label": "Goose"
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
"id": "kilo",
|
|
170
|
+
"label": "Kilo Code"
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
"id": "vibe",
|
|
174
|
+
"label": "Mistral Vibe CLI"
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
}
|
package/lib/routes.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-facing JSON API for the sidebar console.
|
|
3
|
+
*
|
|
4
|
+
* Every route answers one envelope: `{ ok: true, ... }` or
|
|
5
|
+
* `{ ok: false, error: { code, message } }`. The routes never accept a command
|
|
6
|
+
* string or a path that the console does not already know: tool names come from
|
|
7
|
+
* the live REA catalog, client ids from the bundled supported list.
|
|
8
|
+
*/
|
|
9
|
+
import { clampInteger, errorResponse, json, readJsonBody, ReaError, shortError } from './util.js'
|
|
10
|
+
import { familyOf } from './catalog.js'
|
|
11
|
+
import { REA_REQUIREMENT } from './runtime.js'
|
|
12
|
+
|
|
13
|
+
function guarded(handler, logger) {
|
|
14
|
+
return async request => {
|
|
15
|
+
try {
|
|
16
|
+
return await handler(request)
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (!(error instanceof ReaError)) logger?.warn?.(`[dsh-reverse-engine] route error: ${error?.stack ?? error}`)
|
|
19
|
+
return errorResponse(error)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function query(request) {
|
|
25
|
+
return new URL(request.url).searchParams
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createHandlers(service, options = {}) {
|
|
29
|
+
const logger = options.logger
|
|
30
|
+
|
|
31
|
+
const state = guarded(async () => json({ ok: true, ...(await service.state()) }), logger)
|
|
32
|
+
|
|
33
|
+
const runtime = guarded(async () => {
|
|
34
|
+
const state = await service.state()
|
|
35
|
+
return json({ ok: true, runtime: state.runtime, notices: state.notices, install: state.installSkill, dataDirectory: state.settings.dataDirectory })
|
|
36
|
+
}, logger)
|
|
37
|
+
|
|
38
|
+
const engine = guarded(async request => {
|
|
39
|
+
const body = await readJsonBody(request, 8 * 1024)
|
|
40
|
+
const action = String(body.action ?? '').trim()
|
|
41
|
+
if (action === 'start') return json({ ok: true, ...(await service.startEngine().then(snapshot => ({ engine: snapshot }))), ...(await service.state()) })
|
|
42
|
+
if (action === 'stop') {
|
|
43
|
+
await service.stopEngine()
|
|
44
|
+
return json({ ok: true, ...(await service.state()) })
|
|
45
|
+
}
|
|
46
|
+
if (action === 'restart') {
|
|
47
|
+
await service.restartEngine()
|
|
48
|
+
return json({ ok: true, ...(await service.state()) })
|
|
49
|
+
}
|
|
50
|
+
throw new ReaError('unknown-action', `Unknown engine action: ${action || '(empty)'}`)
|
|
51
|
+
}, logger)
|
|
52
|
+
|
|
53
|
+
const tools = guarded(async request => {
|
|
54
|
+
const params = query(request)
|
|
55
|
+
const page = service.toolsPage(params.get('query') ?? '', {
|
|
56
|
+
family: params.get('family') ?? undefined,
|
|
57
|
+
limit: clampInteger(params.get('limit'), 1, 500, 500),
|
|
58
|
+
offset: clampInteger(params.get('offset'), 0, 100_000, 0),
|
|
59
|
+
})
|
|
60
|
+
const exposure = service.settings.toolExposure
|
|
61
|
+
return json({ ok: true, ...page, exposure, exposed: service.exposedToolNames() })
|
|
62
|
+
}, logger)
|
|
63
|
+
|
|
64
|
+
const tool = guarded(async request => {
|
|
65
|
+
const name = query(request).get('name') ?? ''
|
|
66
|
+
const found = service.tool(name)
|
|
67
|
+
if (!found) throw new ReaError('not-found', `No REA tool named ${name || '(empty)'}.`)
|
|
68
|
+
return json({ ok: true, tool: { ...found, family: familyOf(found.name) } })
|
|
69
|
+
}, logger)
|
|
70
|
+
|
|
71
|
+
const call = guarded(async request => {
|
|
72
|
+
const body = await readJsonBody(request, 1024 * 1024)
|
|
73
|
+
const name = String(body.tool ?? '').trim()
|
|
74
|
+
if (!name) throw new ReaError('invalid-tool', 'A tool name is required.')
|
|
75
|
+
const args = body.arguments && typeof body.arguments === 'object' && !Array.isArray(body.arguments) ? body.arguments : {}
|
|
76
|
+
const timeoutMs = clampInteger(body.timeoutMs, 5_000, 3_600_000, service.settings.toolCallTimeoutMs)
|
|
77
|
+
const result = await service.callTool(name, args, { timeoutMs })
|
|
78
|
+
return json({
|
|
79
|
+
ok: true,
|
|
80
|
+
tool: name,
|
|
81
|
+
family: familyOf(name),
|
|
82
|
+
durationMs: result.durationMs,
|
|
83
|
+
structuredContent: result.structuredContent,
|
|
84
|
+
text: result.text,
|
|
85
|
+
content: result.content,
|
|
86
|
+
})
|
|
87
|
+
}, logger)
|
|
88
|
+
|
|
89
|
+
const prompts = guarded(async () => json({ ok: true, prompts: service.prompts() }), logger)
|
|
90
|
+
|
|
91
|
+
const prompt = guarded(async request => {
|
|
92
|
+
const body = await readJsonBody(request, 256 * 1024)
|
|
93
|
+
const name = String(body.name ?? '').trim()
|
|
94
|
+
if (!name) throw new ReaError('invalid-prompt', 'A workflow name is required.')
|
|
95
|
+
const result = await service.getPrompt(name, body.arguments && typeof body.arguments === 'object' ? body.arguments : {})
|
|
96
|
+
const text = result.messages
|
|
97
|
+
.map(message => {
|
|
98
|
+
if (message?.content?.type === 'text') return String(message.content.text ?? '')
|
|
99
|
+
return JSON.stringify(message?.content ?? message)
|
|
100
|
+
})
|
|
101
|
+
.join('\n\n')
|
|
102
|
+
return json({ ok: true, name, description: result.description, text })
|
|
103
|
+
}, logger)
|
|
104
|
+
|
|
105
|
+
const clients = guarded(async request => {
|
|
106
|
+
const refresh = query(request).get('refresh') === '1'
|
|
107
|
+
const list = await service.clients({ refresh })
|
|
108
|
+
return json({ ok: true, ...list })
|
|
109
|
+
}, logger)
|
|
110
|
+
|
|
111
|
+
const clientAction = guarded(async request => {
|
|
112
|
+
const body = await readJsonBody(request, 64 * 1024)
|
|
113
|
+
const action = String(body.action ?? '').trim()
|
|
114
|
+
if (action === 'preview') return json({ ok: true, client: body.client, plan: await service.previewClient(body.client) })
|
|
115
|
+
if (action === 'register') return json({ ok: true, client: body.client, result: await service.registerClient(body.client) })
|
|
116
|
+
if (action === 'register-detected') return json({ ok: true, result: await service.registerDetected() })
|
|
117
|
+
throw new ReaError('unknown-action', `Unknown client action: ${action || '(empty)'}`)
|
|
118
|
+
}, logger)
|
|
119
|
+
|
|
120
|
+
const providers = guarded(async request => {
|
|
121
|
+
const refresh = query(request).get('refresh') === '1'
|
|
122
|
+
return json({ ok: true, ...(await service.providers({ refresh })) })
|
|
123
|
+
}, logger)
|
|
124
|
+
|
|
125
|
+
const doctor = guarded(async request => {
|
|
126
|
+
const refresh = query(request).get('refresh') === '1'
|
|
127
|
+
return json({ ok: true, ...(await service.doctor({ force: refresh })) })
|
|
128
|
+
}, logger)
|
|
129
|
+
|
|
130
|
+
const cli = guarded(async request => {
|
|
131
|
+
const refresh = query(request).get('refresh') === '1'
|
|
132
|
+
return json({ ok: true, ...(await service.cliCatalog({ refresh })) })
|
|
133
|
+
}, logger)
|
|
134
|
+
|
|
135
|
+
const logs = guarded(async () => {
|
|
136
|
+
const state = await service.state()
|
|
137
|
+
return json({ ok: true, activity: state.activity, logs: state.logs, stderr: service.engine.stderr.slice(-120), engine: state.engine })
|
|
138
|
+
}, logger)
|
|
139
|
+
|
|
140
|
+
const settings = guarded(async request => {
|
|
141
|
+
const body = await readJsonBody(request, 256 * 1024)
|
|
142
|
+
const patch = body.settings && typeof body.settings === 'object' ? body.settings : body
|
|
143
|
+
const saved = await service.updateSettings(patch)
|
|
144
|
+
return json({ ok: true, settings: saved, notices: await service.notices() })
|
|
145
|
+
}, logger)
|
|
146
|
+
|
|
147
|
+
const install = guarded(async request => {
|
|
148
|
+
if (request.method === 'POST') return json({ ok: true, install: await service.installRuntime() })
|
|
149
|
+
return json({ ok: true, install: service.installSnapshot() })
|
|
150
|
+
}, logger)
|
|
151
|
+
|
|
152
|
+
const skill = guarded(async request => {
|
|
153
|
+
const body = await readJsonBody(request, 8 * 1024)
|
|
154
|
+
if (String(body.action ?? 'install') !== 'install') throw new ReaError('unknown-action', 'Only action=install is supported.')
|
|
155
|
+
return json({ ok: true, result: await service.installSkill() })
|
|
156
|
+
}, logger)
|
|
157
|
+
|
|
158
|
+
const httpEndpoint = guarded(async request => {
|
|
159
|
+
const body = await readJsonBody(request, 8 * 1024)
|
|
160
|
+
const action = String(body.action ?? 'status').trim()
|
|
161
|
+
return json({ ok: true, endpoint: await service.httpEndpoint(action) })
|
|
162
|
+
}, logger)
|
|
163
|
+
|
|
164
|
+
const info = guarded(async () => {
|
|
165
|
+
return json({
|
|
166
|
+
ok: true,
|
|
167
|
+
requirement: REA_REQUIREMENT,
|
|
168
|
+
routes: [
|
|
169
|
+
'GET /api/rea/state',
|
|
170
|
+
'POST /api/rea/engine',
|
|
171
|
+
'GET /api/rea/tools',
|
|
172
|
+
'GET /api/rea/tool',
|
|
173
|
+
'POST /api/rea/call',
|
|
174
|
+
'GET /api/rea/prompts',
|
|
175
|
+
'POST /api/rea/prompt',
|
|
176
|
+
'GET /api/rea/clients',
|
|
177
|
+
'POST /api/rea/clients',
|
|
178
|
+
'GET /api/rea/providers',
|
|
179
|
+
'GET /api/rea/doctor',
|
|
180
|
+
'GET /api/rea/cli',
|
|
181
|
+
'GET /api/rea/logs',
|
|
182
|
+
'POST /api/rea/settings',
|
|
183
|
+
'POST /api/rea/install',
|
|
184
|
+
'POST /api/rea/skill',
|
|
185
|
+
'POST /api/rea/http',
|
|
186
|
+
'GET /api/rea/runtime',
|
|
187
|
+
],
|
|
188
|
+
})
|
|
189
|
+
}, logger)
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
info,
|
|
193
|
+
state,
|
|
194
|
+
runtime,
|
|
195
|
+
engine,
|
|
196
|
+
tools,
|
|
197
|
+
tool,
|
|
198
|
+
call,
|
|
199
|
+
prompts,
|
|
200
|
+
prompt,
|
|
201
|
+
clients,
|
|
202
|
+
clientAction,
|
|
203
|
+
providers,
|
|
204
|
+
doctor,
|
|
205
|
+
cli,
|
|
206
|
+
logs,
|
|
207
|
+
settings,
|
|
208
|
+
install,
|
|
209
|
+
skill,
|
|
210
|
+
httpEndpoint,
|
|
211
|
+
}
|
|
212
|
+
}
|
package/lib/runtime.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locate the two external pieces the plugin needs at runtime: a Node.js binary
|
|
3
|
+
* REA supports and the `rea-agents` package that exposes `scripts/rea.mjs`.
|
|
4
|
+
*
|
|
5
|
+
* REA 7.x requires Node.js 22.23.4+, 24.22.1+, or 26.12.1+. The Node.js embedded
|
|
6
|
+
* in DSH Desktop does not satisfy that range, so the plugin deliberately prefers
|
|
7
|
+
* a supported system Node.js instead of `process.execPath`.
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from 'node:module'
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import os from 'node:os'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { fileURLToPath } from 'node:url'
|
|
14
|
+
import {
|
|
15
|
+
compareVersions,
|
|
16
|
+
existsSync,
|
|
17
|
+
isDirectorySync,
|
|
18
|
+
nodeSupport,
|
|
19
|
+
parseVersion,
|
|
20
|
+
ReaError,
|
|
21
|
+
resolveUserPath,
|
|
22
|
+
runCommand,
|
|
23
|
+
shortError,
|
|
24
|
+
which,
|
|
25
|
+
} from './util.js'
|
|
26
|
+
|
|
27
|
+
export const REA_PACKAGE = 'rea-agents'
|
|
28
|
+
export const SUPPORTED_REA_MAJOR = 7
|
|
29
|
+
export const REA_REQUIREMENT = 'Node.js 22.23.4+, 24.22.1+, or 26.12.1+'
|
|
30
|
+
|
|
31
|
+
const require = createRequire(import.meta.url)
|
|
32
|
+
|
|
33
|
+
function unique(values) {
|
|
34
|
+
return [...new Set(values.filter(Boolean))]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function nodeVersion(file) {
|
|
38
|
+
try {
|
|
39
|
+
const result = await runCommand(file, ['--version'], { timeoutMs: 8_000, maxOutputBytes: 16 * 1024 })
|
|
40
|
+
if (result.code !== 0) return null
|
|
41
|
+
return result.stdout.trim().replace(/^v/i, '')
|
|
42
|
+
} catch {
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function dshRuntimeNodes() {
|
|
48
|
+
const roots = [
|
|
49
|
+
path.join(process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh'), 'dsh-runtimes'),
|
|
50
|
+
]
|
|
51
|
+
const found = []
|
|
52
|
+
for (const root of roots) {
|
|
53
|
+
if (!isDirectorySync(root)) continue
|
|
54
|
+
let entries = []
|
|
55
|
+
try {
|
|
56
|
+
entries = fs.readdirSync(root)
|
|
57
|
+
} catch {
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
for (const entry of entries) {
|
|
61
|
+
const base = path.join(root, entry, 'dependencies', 'node')
|
|
62
|
+
found.push(path.join(base, 'bin', process.platform === 'win32' ? 'node.exe' : 'node'))
|
|
63
|
+
found.push(path.join(base, process.platform === 'win32' ? 'node.exe' : 'bin/node'))
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return found
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function commonNodePaths() {
|
|
70
|
+
if (process.platform === 'win32') {
|
|
71
|
+
const roots = [process.env.ProgramFiles, process.env['ProgramFiles(x86)'], process.env.LOCALAPPDATA && path.join(process.env.LOCALAPPDATA, 'Programs')]
|
|
72
|
+
return roots.filter(Boolean).map(root => path.join(root, 'nodejs', 'node.exe'))
|
|
73
|
+
}
|
|
74
|
+
return ['/opt/homebrew/bin/node', '/usr/local/bin/node', '/usr/bin/node']
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function npmExecutable() {
|
|
78
|
+
const found = await which(process.platform === 'win32' ? 'npm.cmd' : 'npm')
|
|
79
|
+
if (found) return found
|
|
80
|
+
for (const candidate of process.platform === 'win32'
|
|
81
|
+
? [path.join(process.env.ProgramFiles ?? 'C:\\Program Files', 'nodejs', 'npm.cmd'), path.join(path.dirname(process.execPath), 'npm.cmd')]
|
|
82
|
+
: [path.join(path.dirname(process.execPath), 'npm'), '/usr/local/bin/npm', '/usr/bin/npm']) {
|
|
83
|
+
if (existsSync(candidate)) return candidate
|
|
84
|
+
}
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Resolve one supported Node.js runtime.
|
|
90
|
+
* @returns {Promise<{path: string, version: string, source: string, supported: boolean, candidates: Array<object>}>}
|
|
91
|
+
*/
|
|
92
|
+
export async function findNodeRuntime(settings = {}) {
|
|
93
|
+
const candidates = []
|
|
94
|
+
const add = (file, source) => {
|
|
95
|
+
if (file && existsSync(file)) candidates.push({ path: file, source })
|
|
96
|
+
}
|
|
97
|
+
add(resolveUserPath(settings.nodePath), 'settings')
|
|
98
|
+
add(resolveUserPath(process.env.DSH_REA_NODE), 'DSH_REA_NODE')
|
|
99
|
+
add(resolveUserPath(process.env.REA_NODE_BINARY), 'REA_NODE_BINARY')
|
|
100
|
+
add(await which('node'), 'PATH')
|
|
101
|
+
for (const file of dshRuntimeNodes()) add(file, 'dsh-runtime')
|
|
102
|
+
for (const file of commonNodePaths()) add(file, 'common')
|
|
103
|
+
if (!process.versions.electron) add(process.execPath, 'host')
|
|
104
|
+
|
|
105
|
+
const seen = new Set()
|
|
106
|
+
const inspected = []
|
|
107
|
+
for (const candidate of unique(candidates.map(item => `${item.path}\u0000${item.source}`)).map(key => {
|
|
108
|
+
const [file, source] = key.split('\u0000')
|
|
109
|
+
return { path: file, source }
|
|
110
|
+
})) {
|
|
111
|
+
const key = candidate.path.toLowerCase()
|
|
112
|
+
if (seen.has(key)) continue
|
|
113
|
+
seen.add(key)
|
|
114
|
+
const version = await nodeVersion(candidate.path)
|
|
115
|
+
const support = nodeSupport(version)
|
|
116
|
+
inspected.push({ ...candidate, version, supported: support.supported, reason: support.supported ? null : support.reason })
|
|
117
|
+
if (support.supported) {
|
|
118
|
+
return { ...candidate, version, supported: true, candidates: inspected }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const rejected = inspected.map(item => `${item.path} (${item.version ?? 'unreadable'})`).join(', ')
|
|
122
|
+
throw new ReaError(
|
|
123
|
+
'runtime-missing',
|
|
124
|
+
`No supported Node.js runtime found. ${REA_REQUIREMENT} is required${rejected ? `; inspected ${rejected}` : ''}.`,
|
|
125
|
+
{ candidates: inspected },
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function packageRootFromPath(value) {
|
|
130
|
+
const resolved = resolveUserPath(value)
|
|
131
|
+
if (!resolved) return null
|
|
132
|
+
if (isDirectorySync(resolved)) {
|
|
133
|
+
if (existsSync(path.join(resolved, 'package.json'))) return resolved
|
|
134
|
+
if (existsSync(path.join(resolved, 'scripts', 'rea.mjs'))) return path.dirname(resolved)
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
if (/rea\.mjs$/i.test(resolved)) return path.dirname(path.dirname(resolved))
|
|
138
|
+
if (/package\.json$/i.test(resolved)) return path.dirname(resolved)
|
|
139
|
+
return null
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function inspectPackage(root, source) {
|
|
143
|
+
if (!root) return null
|
|
144
|
+
const script = path.join(root, 'scripts', 'rea.mjs')
|
|
145
|
+
if (!existsSync(script)) return null
|
|
146
|
+
let version = null
|
|
147
|
+
try {
|
|
148
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
|
|
149
|
+
if (manifest.name !== REA_PACKAGE) return null
|
|
150
|
+
version = typeof manifest.version === 'string' ? manifest.version : null
|
|
151
|
+
} catch {
|
|
152
|
+
// Some managed Windows volumes hand an unreadable manifest to this
|
|
153
|
+
// process while the engine's own Node can still read it. Accept the
|
|
154
|
+
// directory only when its layout is unambiguous; the engine's
|
|
155
|
+
// `initialize` reply supplies the authoritative version.
|
|
156
|
+
if (path.basename(root) !== REA_PACKAGE) return null
|
|
157
|
+
}
|
|
158
|
+
return { dir: root, script, version, source }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function npmRootGlobal() {
|
|
162
|
+
const npm = await npmExecutable()
|
|
163
|
+
if (!npm) return null
|
|
164
|
+
try {
|
|
165
|
+
const result = await runCommand(npm, ['root', '-g'], { timeoutMs: 20_000, maxOutputBytes: 64 * 1024 })
|
|
166
|
+
if (result.code !== 0) return null
|
|
167
|
+
return result.stdout.split(/\r?\n/).map(line => line.trim()).find(Boolean) ?? null
|
|
168
|
+
} catch {
|
|
169
|
+
return null
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Resolve `rea-agents` without importing its internals. Order: explicit setting,
|
|
175
|
+
* the plugin's own dependency (normal npm install), an auto-installed runtime
|
|
176
|
+
* under the plugin data directory, then a global npm installation.
|
|
177
|
+
*/
|
|
178
|
+
export async function findReaPackage(settings = {}, dataDirectory) {
|
|
179
|
+
const attempts = []
|
|
180
|
+
const custom = packageRootFromPath(settings.reaPackagePath)
|
|
181
|
+
if (custom) {
|
|
182
|
+
const found = inspectPackage(custom, 'settings')
|
|
183
|
+
if (found) return found
|
|
184
|
+
attempts.push(`settings path ${settings.reaPackagePath} is not a ${REA_PACKAGE} package`)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Manual sibling lookups come first so a manifest this process cannot read
|
|
188
|
+
// does not make `require.resolve` fail the whole resolution.
|
|
189
|
+
const pluginRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
|
190
|
+
for (const candidate of [
|
|
191
|
+
path.join(pluginRoot, 'node_modules', REA_PACKAGE),
|
|
192
|
+
path.resolve(pluginRoot, '..', REA_PACKAGE),
|
|
193
|
+
path.resolve(pluginRoot, '..', '..', REA_PACKAGE),
|
|
194
|
+
]) {
|
|
195
|
+
const found = inspectPackage(candidate, 'dependency')
|
|
196
|
+
if (found) return found
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const manifestPath = require.resolve(`${REA_PACKAGE}/package.json`)
|
|
201
|
+
const found = inspectPackage(path.dirname(manifestPath), 'dependency')
|
|
202
|
+
if (found) return found
|
|
203
|
+
} catch {
|
|
204
|
+
// Not installed next to the plugin; fall through to the other locations.
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (dataDirectory) {
|
|
208
|
+
const found = inspectPackage(path.join(dataDirectory, 'runtime', 'node_modules', REA_PACKAGE), 'installed')
|
|
209
|
+
if (found) return found
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const globalRoot = await npmRootGlobal()
|
|
213
|
+
if (globalRoot) {
|
|
214
|
+
const found = inspectPackage(path.join(globalRoot, REA_PACKAGE), 'global')
|
|
215
|
+
if (found) return found
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
throw new ReaError(
|
|
219
|
+
'runtime-missing',
|
|
220
|
+
`The ${REA_PACKAGE} package was not found. Install the plugin from npm, or run the console action that installs REA into the plugin data directory.`,
|
|
221
|
+
{ attempts },
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Probe both runtime halves without starting the engine. */
|
|
226
|
+
export async function inspectRuntime(settings, dataDirectory) {
|
|
227
|
+
const result = { node: null, rea: null, errors: [] }
|
|
228
|
+
try {
|
|
229
|
+
result.node = await findNodeRuntime(settings)
|
|
230
|
+
} catch (error) {
|
|
231
|
+
result.errors.push({ part: 'node', code: error.code ?? 'runtime-missing', message: shortError(error), details: error.details })
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
result.rea = await findReaPackage(settings, dataDirectory)
|
|
235
|
+
if (result.rea && result.rea.version && parseVersion(result.rea.version)?.major !== SUPPORTED_REA_MAJOR) {
|
|
236
|
+
result.errors.push({
|
|
237
|
+
part: 'rea',
|
|
238
|
+
code: 'version-mismatch',
|
|
239
|
+
message: `Found ${REA_PACKAGE} ${result.rea.version}; this plugin is built for ${REA_PACKAGE} ${SUPPORTED_REA_MAJOR}.x.`,
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
} catch (error) {
|
|
243
|
+
result.errors.push({ part: 'rea', code: error.code ?? 'runtime-missing', message: shortError(error), details: error.details })
|
|
244
|
+
}
|
|
245
|
+
return result
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Install the pinned engine into `<dataDir>/runtime`. This is the fallback for
|
|
250
|
+
* linked local checkouts, where the profile install does not resolve the
|
|
251
|
+
* plugin's own dependency tree.
|
|
252
|
+
*/
|
|
253
|
+
export async function installReaPackage(dataDirectory, version, options = {}) {
|
|
254
|
+
const npm = await npmExecutable()
|
|
255
|
+
if (!npm) {
|
|
256
|
+
throw new ReaError('npm-missing', 'npm was not found. Install Node.js with npm, or set a plugin dependency path in Settings.')
|
|
257
|
+
}
|
|
258
|
+
const runtimeDirectory = path.join(dataDirectory, 'runtime')
|
|
259
|
+
await fs.promises.mkdir(runtimeDirectory, { recursive: true })
|
|
260
|
+
const manifest = path.join(runtimeDirectory, 'package.json')
|
|
261
|
+
if (!existsSync(manifest)) {
|
|
262
|
+
await fs.promises.writeFile(manifest, `${JSON.stringify({ name: 'dsh-reverse-engine-runtime', private: true }, null, 2)}\n`, 'utf8')
|
|
263
|
+
}
|
|
264
|
+
options.onLog?.(`Installing ${REA_PACKAGE}@${version} into ${runtimeDirectory}\n`)
|
|
265
|
+
const result = await runCommand(
|
|
266
|
+
npm,
|
|
267
|
+
['install', `${REA_PACKAGE}@${version}`, '--no-audit', '--no-fund', '--loglevel=error', '--save-exact'],
|
|
268
|
+
{
|
|
269
|
+
cwd: runtimeDirectory,
|
|
270
|
+
timeoutMs: 15 * 60_000,
|
|
271
|
+
signal: options.signal,
|
|
272
|
+
onStdout: options.onLog,
|
|
273
|
+
onStderr: options.onLog,
|
|
274
|
+
},
|
|
275
|
+
)
|
|
276
|
+
if (result.code !== 0) {
|
|
277
|
+
throw new ReaError('install-failed', `npm install ${REA_PACKAGE}@${version} failed with exit code ${result.code}.`, {
|
|
278
|
+
output: (result.stderr || result.stdout).slice(-8000),
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
const found = inspectPackage(path.join(runtimeDirectory, 'node_modules', REA_PACKAGE), 'installed')
|
|
282
|
+
if (!found) throw new ReaError('install-failed', 'npm finished but the installed package could not be resolved.')
|
|
283
|
+
return found
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** The launcher command other MCP clients should use for the same engine. */
|
|
287
|
+
export function mcpCommand(node, rea) {
|
|
288
|
+
return { command: node.path, args: [rea.script, 'mcp'] }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Whether the found package is older/newer than the pinned release. */
|
|
292
|
+
export function versionNotice(reaVersion, pinned = '7.0.0') {
|
|
293
|
+
if (!reaVersion) return null
|
|
294
|
+
const comparison = compareVersions(reaVersion, pinned)
|
|
295
|
+
if (comparison === 0) return null
|
|
296
|
+
return comparison < 0
|
|
297
|
+
? `Installed ${REA_PACKAGE} ${reaVersion} is older than the pinned ${pinned}; some console operations may be missing.`
|
|
298
|
+
: `Installed ${REA_PACKAGE} ${reaVersion} is newer than the pinned ${pinned}; the catalog is read live, so most operations keep working.`
|
|
299
|
+
}
|