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
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# dsh-reverse-engine bundle: the row loads the Host half (runtime bridge,
|
|
2
|
+
# JSON routes, and the agent tools); `dsh.client` in package.json makes the Web
|
|
3
|
+
# app load client.js, which registers the sidebar console.
|
|
4
|
+
- insert:
|
|
5
|
+
- id: reverse-engineer
|
|
6
|
+
name: dsh-reverse-engine
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
`dsh-reverse-engine` is one DSH bundle with a Host half and a Client half. It
|
|
4
|
+
owns one external REA process and never imports REA internals.
|
|
5
|
+
|
|
6
|
+
```text
|
|
7
|
+
┌──────────────────────────── DeepSeek Harness profile ────────────────────────────┐
|
|
8
|
+
│ │
|
|
9
|
+
│ Agent ── tools ──► index.js ──► lib/service.js ──► lib/engine.js ──┐ │
|
|
10
|
+
│ │ │ │ │
|
|
11
|
+
│ │ ├── lib/clients.js ──► rea setup / doctor │
|
|
12
|
+
│ │ ├── lib/http-endpoint.js ──► rea mcp --http│
|
|
13
|
+
│ │ └── lib/runtime.js ──► Node + rea-agents │
|
|
14
|
+
│ │ │ │
|
|
15
|
+
│ Web UI ── fetch ──► /api/rea/* ──► lib/routes.js │ │
|
|
16
|
+
│ ▲ ▼ │
|
|
17
|
+
│ └── client.js (sidebar console) rea mcp (stdio JSON-RPC) │
|
|
18
|
+
│ │
|
|
19
|
+
└──────────────────────────────────────────────────────────────────────────────────┘
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Host half
|
|
23
|
+
|
|
24
|
+
| File | Responsibility |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `index.js` | Cordis entry. Resolves the data directory, registers routes and tools, installs the prompt section, and owns teardown. |
|
|
27
|
+
| `lib/service.js` | Orchestrator: settings, runtime inspection, install job, providers, CLI catalog, HTTP endpoint, and the console state snapshot. |
|
|
28
|
+
| `lib/engine.js` | Minimal MCP stdio client: newline JSON framing, initialize, paginated `tools/list` and `prompts/list`, serialized `tools/call`, cancellation, timeouts, restart, and a bounded log ring. |
|
|
29
|
+
| `lib/runtime.js` | Finds a supported Node.js and the `rea-agents` package. Installs the pinned package into the data directory when a linked checkout has no dependency tree. |
|
|
30
|
+
| `lib/settings.js` | Persisted console settings under `<DSH_HOME>/dsh-reverse-engine/settings.json`, defaults, clamping, and the provider env allow-list. |
|
|
31
|
+
| `lib/catalog.js` | Family lookup, search ranking, exposure selection, and schema-size estimate over the live catalog. |
|
|
32
|
+
| `lib/tools.js` | Tool definitions: three dispatchers, the workflow reader, and generated wrappers for the exposed native tools. |
|
|
33
|
+
| `lib/clients.js` | 43-client list, `rea doctor` state parsing, dry-run preview, registration, and skill install. |
|
|
34
|
+
| `lib/http-endpoint.js` | Optional second REA process in `--http` mode, with URL discovery from its stderr announcement. |
|
|
35
|
+
| `lib/routes.js` | JSON routes for the console. No route accepts a raw command or an arbitrary path. |
|
|
36
|
+
|
|
37
|
+
### Lifecycle
|
|
38
|
+
|
|
39
|
+
1. `apply()` resolves the data directory and loads settings.
|
|
40
|
+
2. `service.init()` inspects the runtime. If a supported Node.js and
|
|
41
|
+
`rea-agents` are present and `connectOnStart` is on, it starts the engine in
|
|
42
|
+
the background; client detection runs concurrently.
|
|
43
|
+
3. The engine spawns `<node> <rea>/scripts/rea.mjs mcp` with the process
|
|
44
|
+
environment plus allow-listed settings, `REA_MCP_TOOL_CATALOG_PROFILE=full`,
|
|
45
|
+
and the plugin data directory as cwd.
|
|
46
|
+
4. `initialize` → `notifications/initialized` → paginated `tools/list` and
|
|
47
|
+
`prompts/list`. The catalog event re-registers model tools.
|
|
48
|
+
5. On unload, the plugin disposes tools and routes, then stops both engine
|
|
49
|
+
processes with a bounded SIGTERM/SIGKILL sequence.
|
|
50
|
+
|
|
51
|
+
### Tool registration
|
|
52
|
+
|
|
53
|
+
Three dispatchers are always registered:
|
|
54
|
+
|
|
55
|
+
- `rea_status` — engine, runtime, exposure, catalog summary, MCP command.
|
|
56
|
+
- `rea_tools` — ranked search over names and descriptions, family filter, paging.
|
|
57
|
+
- `rea_call` — execute any canonical tool with a JSON arguments object.
|
|
58
|
+
- `rea_workflow` — read one guided workflow.
|
|
59
|
+
|
|
60
|
+
The default `core` exposure additionally registers 11 first-hop REA tools under
|
|
61
|
+
their canonical names. `families` and `all` widen that set. Registration is
|
|
62
|
+
effect-owned: changing exposure disposes the previous generation and registers a
|
|
63
|
+
new one. A name collision is logged and skipped, never fatal.
|
|
64
|
+
|
|
65
|
+
### Concurrency and cancellation
|
|
66
|
+
|
|
67
|
+
`rea mcp` serves one stateful session with one active target, so `callTool()`
|
|
68
|
+
chains every request through a promise queue. UI calls and agent calls share it.
|
|
69
|
+
The DSH execution signal is wired to a JSON-RPC `notifications/cancelled` for
|
|
70
|
+
the pending id; per-call deadlines default to 450 seconds. Engine stderr is
|
|
71
|
+
parsed as JSON logs when possible and retained as a bounded ring otherwise.
|
|
72
|
+
|
|
73
|
+
### Runtime resolution
|
|
74
|
+
|
|
75
|
+
Node.js candidates, first supported wins:
|
|
76
|
+
|
|
77
|
+
1. `settings.nodePath`
|
|
78
|
+
2. `DSH_REA_NODE`, `REA_NODE_BINARY`
|
|
79
|
+
3. `node` on `PATH`
|
|
80
|
+
4. DSH's bundled runtimes under `<DSH_HOME>/dsh-runtimes/*/dependencies/node`
|
|
81
|
+
5. Common install locations
|
|
82
|
+
6. The Host executable only when it is a plain Node.js
|
|
83
|
+
|
|
84
|
+
`process.execPath` is deliberately not preferred: DSH Desktop embeds an Electron
|
|
85
|
+
Node.js version that REA may reject.
|
|
86
|
+
|
|
87
|
+
`rea-agents` candidates:
|
|
88
|
+
|
|
89
|
+
1. `settings.reaPackagePath`
|
|
90
|
+
2. The plugin's own dependency (`require.resolve`)
|
|
91
|
+
3. `<data>/runtime/node_modules/rea-agents` (installed by the console)
|
|
92
|
+
4. Global npm root
|
|
93
|
+
|
|
94
|
+
### Data and security
|
|
95
|
+
|
|
96
|
+
- Settings: `<DSH_HOME>/dsh-reverse-engine/settings.json`.
|
|
97
|
+
- Optional installed runtime: `<DSH_HOME>/dsh-reverse-engine/runtime/`.
|
|
98
|
+
- No credentials are read or stored. Provider environment entries are limited to
|
|
99
|
+
an allow-list of `REA_*` and engine path variables.
|
|
100
|
+
- The HTTP MCP endpoint is unauthenticated; the console warns for non-loopback
|
|
101
|
+
binds and defaults to `127.0.0.1`.
|
|
102
|
+
|
|
103
|
+
## Client half
|
|
104
|
+
|
|
105
|
+
`client.js` registers through `window.__ModuleLoader__`:
|
|
106
|
+
|
|
107
|
+
- A lazy factory whose id is the package name.
|
|
108
|
+
- Localized dictionaries through `ctx.locale.register`.
|
|
109
|
+
- A root-scoped panel component into `main` under `reverse-engine` and a
|
|
110
|
+
`sidebar.panellist` row with the same id, order 12.
|
|
111
|
+
- Styles are local React elements; containers use `--dsw-alias-*` theme tokens.
|
|
112
|
+
|
|
113
|
+
The console polls `GET /api/rea/state` every five seconds while visible. Tab
|
|
114
|
+
data (catalog, clients, workflows, logs) is fetched on demand.
|
package/docs/CONTRACT.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# HTTP and tool contract
|
|
2
|
+
|
|
3
|
+
All console routes are under `/api/rea`. JSON requests are size-bounded and must
|
|
4
|
+
be objects. Every response is one envelope:
|
|
5
|
+
|
|
6
|
+
```json
|
|
7
|
+
{ "ok": true, "...": "payload" }
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```json
|
|
11
|
+
{ "ok": false, "error": { "code": "engine-not-ready", "message": "…", "details": {} } }
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Routes
|
|
15
|
+
|
|
16
|
+
| Method | Path | Body / query | Success payload |
|
|
17
|
+
|---|---|---|---|
|
|
18
|
+
| GET | `/api/rea/info` | — | Route inventory and Node.js requirement. |
|
|
19
|
+
| GET | `/api/rea/state` | — | `engine`, `runtime`, `settings`, `catalog`, `http`, `install`, `clients`, `installSkill`, `mcp`, `notices`, `activity`, `logs`. |
|
|
20
|
+
| GET | `/api/rea/runtime` | — | `runtime`, `notices`, `install`, `dataDirectory`. |
|
|
21
|
+
| POST | `/api/rea/engine` | `{ action: "start" \| "stop" \| "restart" }` | Fresh state. |
|
|
22
|
+
| GET | `/api/rea/tools` | `query`, `family`, `limit` (≤500), `offset` | `total`, `tools[]`, `families[]`, `exposure`, `exposed[]`. |
|
|
23
|
+
| GET | `/api/rea/tool` | `name` | Full tool: schema, annotations, family. |
|
|
24
|
+
| POST | `/api/rea/call` | `{ tool, arguments, timeoutMs? }` | `structuredContent`, `text`, `content`, `durationMs`. |
|
|
25
|
+
| GET | `/api/rea/prompts` | — | Guided workflow list. |
|
|
26
|
+
| POST | `/api/rea/prompt` | `{ name, arguments? }` | Rendered workflow `text`. |
|
|
27
|
+
| GET | `/api/rea/clients` | `refresh=1` | 43 clients with `state`, `detected`, `configPath`; skill status. |
|
|
28
|
+
| POST | `/api/rea/clients` | `{ action: "preview" \| "register", client }` or `{ action: "register-detected" }` | REA dry-run plan or setup result. |
|
|
29
|
+
| GET | `/api/rea/providers` | `refresh=1` | Provider candidates, selected provider, capability groups. |
|
|
30
|
+
| GET | `/api/rea/doctor` | `refresh=1` | Summarized `rea doctor`: checks, skill, registrations, catalog. |
|
|
31
|
+
| GET | `/api/rea/cli` | `refresh=1` | 217 CLI commands parsed from `rea --llms`. |
|
|
32
|
+
| GET | `/api/rea/logs` | — | `activity`, structured `logs`, `stderr`, engine snapshot. |
|
|
33
|
+
| POST | `/api/rea/settings` | `{ settings: { … } }` | Saved public settings and notices. |
|
|
34
|
+
| POST | `/api/rea/install` | — | Runtime install job state and log tail. |
|
|
35
|
+
| POST | `/api/rea/skill` | `{ action: "install" }` | `rea setup --skill` result. |
|
|
36
|
+
| POST | `/api/rea/http` | `{ action: "status" \| "start" \| "stop" \| "restart" }` | Endpoint snapshot. |
|
|
37
|
+
|
|
38
|
+
### Error codes
|
|
39
|
+
|
|
40
|
+
`invalid-json`, `body-too-large`, `unknown-action`, `unknown-client`,
|
|
41
|
+
`invalid-tool`, `invalid-prompt`, `not-found`, `runtime-missing`,
|
|
42
|
+
`version-mismatch`, `engine-not-ready`, `engine-stopped`, `engine-timeout`,
|
|
43
|
+
`engine-rpc-error`, `tool-failed`, `spawn-failed`, `install-failed`,
|
|
44
|
+
`setup-failed`, `doctor-failed`, `capabilities-failed`, `busy`, `aborted`.
|
|
45
|
+
|
|
46
|
+
## Settings
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"reaPackagePath": "",
|
|
51
|
+
"nodePath": "",
|
|
52
|
+
"toolExposure": "core",
|
|
53
|
+
"toolFamilies": [],
|
|
54
|
+
"toolCallTimeoutMs": 450000,
|
|
55
|
+
"startupTimeoutMs": 120000,
|
|
56
|
+
"connectOnStart": true,
|
|
57
|
+
"httpEnabled": false,
|
|
58
|
+
"httpHost": "127.0.0.1",
|
|
59
|
+
"httpPort": 0,
|
|
60
|
+
"env": {}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`toolExposure` is `core`, `families`, or `all`. `toolFamilies` only applies to
|
|
65
|
+
`families`. `env` accepts an allow-list (`REA_*`, `GHIDRA_INSTALL_DIR`,
|
|
66
|
+
`HOPPER_LAUNCHER_PATH`, `JAVA_HOME`, and provider path variables); unknown keys
|
|
67
|
+
are dropped.
|
|
68
|
+
|
|
69
|
+
## Model tools
|
|
70
|
+
|
|
71
|
+
| Tool | Arguments | Result |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| `rea_status` | `{}` | Engine, runtime, exposure, catalog summary, MCP command. |
|
|
74
|
+
| `rea_tools` | `{ query?, family?, limit?, offset? }` | Ranked compact tool list with total and families. |
|
|
75
|
+
| `rea_call` | `{ tool, arguments?, timeout_ms? }` | Canonical `structuredContent`, text, content blocks, duration. |
|
|
76
|
+
| `rea_workflow` | `{ name, arguments? }` | Rendered guided workflow Markdown. |
|
|
77
|
+
| core REA tools | That tool's own schema | Same envelope as `rea_call`. |
|
|
78
|
+
|
|
79
|
+
Tool failures return `{ ok: false, error: { code, message, details? } }` rather
|
|
80
|
+
than throwing, so the model can recover in the same turn.
|
|
81
|
+
|
|
82
|
+
## Client MCP command
|
|
83
|
+
|
|
84
|
+
The same engine is reachable outside DSH:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
"<supported-node>" "<rea-agents>/scripts/rea.mjs" mcp
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The console shows the exact absolute command under **Overview → MCP launch
|
|
91
|
+
command** and offers per-client registration under **Clients**.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Verification
|
|
2
|
+
|
|
3
|
+
This release was exercised on Windows 11 with DeepSeek Harness Desktop
|
|
4
|
+
(`0.2.2-rc.5`), Node.js 26.13.1, and REA 7.0.0.
|
|
5
|
+
|
|
6
|
+
## Automated
|
|
7
|
+
|
|
8
|
+
| Check | Result |
|
|
9
|
+
|---|---|
|
|
10
|
+
| `node --test tests/` | 28 passing tests: engine handshake/catalog/call/timeout/cancel, settings, catalog search and exposure, JSON routes, Host registration, client module and dictionaries |
|
|
11
|
+
| `node scripts/validate.mjs` | Package manifest, exports, locales, icon, catalog metadata, client list, and patch validated |
|
|
12
|
+
| `npm pack` | 26 files, 68.5 kB tarball, no tests or scratch files |
|
|
13
|
+
| Packaged install | `npm install ./dsh-reverse-engine-0.1.0.tgz` in a clean consumer project installed 34 packages and resolved `rea-agents` 7.0.0 through the plugin's dependency |
|
|
14
|
+
| React + jsdom harness | Mounted the real client module with real React: Overview, Tools, Clients, Workflows, Settings, and Logs tabs render; a tool runs and shows its structured result |
|
|
15
|
+
|
|
16
|
+
## Live Harness
|
|
17
|
+
|
|
18
|
+
- Installed from the local checkout with `plugin_manager install_bundle`;
|
|
19
|
+
the bundle row activated without warnings.
|
|
20
|
+
- The sidebar label and prompt section appeared only while the bundle was
|
|
21
|
+
enabled, and disappeared when it was disabled.
|
|
22
|
+
- The Host spawned exactly one `rea mcp` child using the auto-detected Node.js
|
|
23
|
+
and the runtime installed under `<DSH_HOME>/dsh-reverse-engine/runtime`.
|
|
24
|
+
- `rea_status` returned engine `ready`, 274 tools, 6 prompts, and the 11-tool
|
|
25
|
+
core exposure.
|
|
26
|
+
- `rea_tools` searched the catalog (32 matches for "javascript application
|
|
27
|
+
asar").
|
|
28
|
+
- `inspect_artifact` returned complete ZIP inventory Evidence for a local
|
|
29
|
+
sample archive, with Evidence ID and coverage.
|
|
30
|
+
- The installed runtime was produced by the plugin's own install action, and
|
|
31
|
+
auto-discovery then found it without an explicit path.
|
|
32
|
+
|
|
33
|
+
## Not verified here
|
|
34
|
+
|
|
35
|
+
- The rendered sidebar page could not be inspected through browser automation:
|
|
36
|
+
the Desktop renderer fetches over its authenticated IPC bridge, and no headless
|
|
37
|
+
browser profile with that session was available. The client module was instead
|
|
38
|
+
rendered and interacted with under React + jsdom, and its slot registration
|
|
39
|
+
follows the documented `main` + `sidebar.panellist` contract. Refresh the
|
|
40
|
+
Desktop window after installation and open **Reverse engineering** to confirm
|
|
41
|
+
the visual result.
|
|
42
|
+
- Ghidra, IDA, Hopper, and Kuna were not installed on this machine, so the
|
|
43
|
+
provider panel reports their real "unavailable" states but no native
|
|
44
|
+
decompilation was run.
|
|
45
|
+
- No third-party agent client was reconfigured during verification; previews
|
|
46
|
+
were generated with `rea setup --dry-run` and checked, but registration was
|
|
47
|
+
left to the user from the Clients tab.
|
|
48
|
+
|
|
49
|
+
## Reproduce
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
node scripts/validate.mjs
|
|
53
|
+
node --test --test-timeout=60000 tests/
|
|
54
|
+
npm pack
|
|
55
|
+
```
|
package/icon.svg
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36" role="img" aria-label="Reverse engineering">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="dshre-ring" x1="6" y1="4" x2="30" y2="32" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop offset="0" stop-color="#5b7cfa"/>
|
|
5
|
+
<stop offset="1" stop-color="#3f5bd6"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="dshre-node" x1="10" y1="9" x2="21" y2="22" gradientUnits="userSpaceOnUse">
|
|
8
|
+
<stop offset="0" stop-color="#2ec4b6"/>
|
|
9
|
+
<stop offset="1" stop-color="#159a8e"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
</defs>
|
|
12
|
+
<g fill="none" stroke-linecap="round" stroke-linejoin="round">
|
|
13
|
+
<path d="M14.6 22.4 11.9 27.9" stroke="#9fb2f4" stroke-width="2.2" opacity="0.55"/>
|
|
14
|
+
<path d="M18.2 24.1 15.9 28.7" stroke="#9fb2f4" stroke-width="2.2" opacity="0.55"/>
|
|
15
|
+
<circle cx="15" cy="14.6" r="8.6" stroke="url(#dshre-ring)" stroke-width="2.6"/>
|
|
16
|
+
<path d="M21.4 21 28.3 27.9" stroke="url(#dshre-ring)" stroke-width="3.1"/>
|
|
17
|
+
<path d="M10.9 14.1h8.2M15 9.9v8.4" stroke="url(#dshre-node)" stroke-width="1.6" opacity="0.75"/>
|
|
18
|
+
<circle cx="10.9" cy="14.1" r="1.75" fill="url(#dshre-node)" stroke="none"/>
|
|
19
|
+
<circle cx="19.1" cy="14.1" r="1.75" fill="url(#dshre-node)" stroke="none"/>
|
|
20
|
+
<circle cx="15" cy="9.9" r="1.75" fill="url(#dshre-node)" stroke="none"/>
|
|
21
|
+
<circle cx="15" cy="18.3" r="1.75" fill="#ffffff" stroke="url(#dshre-node)" stroke-width="1.5"/>
|
|
22
|
+
</g>
|
|
23
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-reverse-engine — Host half.
|
|
3
|
+
*
|
|
4
|
+
* Owns one local REA engine process, registers its tools for the agent, and
|
|
5
|
+
* serves the JSON API used by the sidebar console. The Client half lives in
|
|
6
|
+
* client.js and is loaded by `dsh.client` in package.json.
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import os from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { createHandlers } from './lib/routes.js'
|
|
12
|
+
import { ReaService } from './lib/service.js'
|
|
13
|
+
import { SettingsStore } from './lib/settings.js'
|
|
14
|
+
import { buildNativeTool, buildPromptTool, buildToolDefinitions } from './lib/tools.js'
|
|
15
|
+
import { shortError } from './lib/util.js'
|
|
16
|
+
|
|
17
|
+
export const name = 'reverse-engineer'
|
|
18
|
+
|
|
19
|
+
/** Host services this plugin needs; everything else is optional. */
|
|
20
|
+
export const inject = ['connection', 'tools']
|
|
21
|
+
|
|
22
|
+
function resolveDataDirectory(ctx) {
|
|
23
|
+
try {
|
|
24
|
+
const homePath = ctx.get('dshHomePath')
|
|
25
|
+
if (typeof homePath === 'function') return homePath('dsh-reverse-engine')
|
|
26
|
+
} catch {
|
|
27
|
+
// fall through to the environment
|
|
28
|
+
}
|
|
29
|
+
const home = process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh')
|
|
30
|
+
return path.join(home, 'dsh-reverse-engine')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeLogger(ctx) {
|
|
34
|
+
const bind = level => {
|
|
35
|
+
const logger = ctx.logger
|
|
36
|
+
return typeof logger?.[level] === 'function' ? logger[level].bind(logger) : () => {}
|
|
37
|
+
}
|
|
38
|
+
return { info: bind('info'), warn: bind('warn'), error: bind('error'), debug: bind('debug') }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function disposeRegistration(registration) {
|
|
42
|
+
if (typeof registration === 'function') return registration
|
|
43
|
+
if (registration && typeof registration.dispose === 'function') return () => registration.dispose()
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Register all routes; a duplicate path is logged instead of failing activation. */
|
|
48
|
+
function registerRoutes(ctx, service, logger) {
|
|
49
|
+
const handlers = createHandlers(service, { logger })
|
|
50
|
+
const get = (path, fetch) => ({ path, methods: ['GET', 'HEAD'], requestBody: 'buffered', fetch })
|
|
51
|
+
const post = (path, fetch) => ({ path, methods: ['POST'], requestBody: 'buffered', fetch })
|
|
52
|
+
const routes = [
|
|
53
|
+
get('/api/rea/info', handlers.info),
|
|
54
|
+
get('/api/rea/state', handlers.state),
|
|
55
|
+
get('/api/rea/runtime', handlers.runtime),
|
|
56
|
+
post('/api/rea/engine', handlers.engine),
|
|
57
|
+
get('/api/rea/tools', handlers.tools),
|
|
58
|
+
get('/api/rea/tool', handlers.tool),
|
|
59
|
+
post('/api/rea/call', handlers.call),
|
|
60
|
+
get('/api/rea/prompts', handlers.prompts),
|
|
61
|
+
post('/api/rea/prompt', handlers.prompt),
|
|
62
|
+
get('/api/rea/clients', handlers.clients),
|
|
63
|
+
post('/api/rea/clients', handlers.clientAction),
|
|
64
|
+
get('/api/rea/providers', handlers.providers),
|
|
65
|
+
get('/api/rea/doctor', handlers.doctor),
|
|
66
|
+
get('/api/rea/cli', handlers.cli),
|
|
67
|
+
get('/api/rea/logs', handlers.logs),
|
|
68
|
+
post('/api/rea/settings', handlers.settings),
|
|
69
|
+
post('/api/rea/install', handlers.install),
|
|
70
|
+
post('/api/rea/skill', handlers.skill),
|
|
71
|
+
post('/api/rea/http', handlers.httpEndpoint),
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
ctx.effect(() => {
|
|
75
|
+
const disposers = []
|
|
76
|
+
for (const route of routes) {
|
|
77
|
+
try {
|
|
78
|
+
const disposer = ctx.connection.fetch.register(route)
|
|
79
|
+
const dispose = disposeRegistration(disposer)
|
|
80
|
+
if (dispose) disposers.push(dispose)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
logger.warn(`[dsh-reverse-engine] cannot register ${route.path}: ${shortError(error)}`)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
logger.info(`[dsh-reverse-engine] ${disposers.length}/${routes.length} routes registered`)
|
|
86
|
+
return () => {
|
|
87
|
+
for (const dispose of disposers) {
|
|
88
|
+
try {
|
|
89
|
+
Promise.resolve(dispose()).catch(() => {})
|
|
90
|
+
} catch {
|
|
91
|
+
// ignore
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}, 'dsh-reverse-engine: fetch routes')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function createToolRegistry(ctx, service, logger) {
|
|
99
|
+
let disposers = []
|
|
100
|
+
let nativeNames = []
|
|
101
|
+
|
|
102
|
+
const disposeAll = () => {
|
|
103
|
+
const current = disposers
|
|
104
|
+
disposers = []
|
|
105
|
+
for (const dispose of current) {
|
|
106
|
+
try {
|
|
107
|
+
dispose()
|
|
108
|
+
} catch (error) {
|
|
109
|
+
logger.warn(`[dsh-reverse-engine] tool dispose failed: ${shortError(error)}`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const register = definition => {
|
|
115
|
+
try {
|
|
116
|
+
const registration = ctx.tools.register(definition)
|
|
117
|
+
const dispose = disposeRegistration(registration)
|
|
118
|
+
if (dispose) disposers.push(dispose)
|
|
119
|
+
return true
|
|
120
|
+
} catch (error) {
|
|
121
|
+
logger.warn(`[dsh-reverse-engine] cannot register tool ${definition.name}: ${shortError(error)}`)
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const sync = () => {
|
|
127
|
+
disposeAll()
|
|
128
|
+
if (service.engine.tools.length === 0) {
|
|
129
|
+
nativeNames = []
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
const names = service.exposedToolNames()
|
|
133
|
+
const settings = service.settings
|
|
134
|
+
const extra = {
|
|
135
|
+
nativeToolNames: names,
|
|
136
|
+
mcpCommand: service.engine.runtime
|
|
137
|
+
? { command: service.engine.runtime.node.path, args: [service.engine.runtime.rea.script, 'mcp'] }
|
|
138
|
+
: null,
|
|
139
|
+
}
|
|
140
|
+
for (const definition of buildToolDefinitions({ engine: service.engine, settings, extra })) register(definition)
|
|
141
|
+
register(buildPromptTool(service.engine))
|
|
142
|
+
let registered = 0
|
|
143
|
+
for (const toolName of names) {
|
|
144
|
+
const tool = service.tool(toolName)
|
|
145
|
+
if (!tool) continue
|
|
146
|
+
if (register(buildNativeTool({ engine: service.engine, settings, tool }))) registered += 1
|
|
147
|
+
}
|
|
148
|
+
nativeNames = names.filter(name => service.tool(name))
|
|
149
|
+
logger.info(`[dsh-reverse-engine] ${registered + 4} tools registered (${registered} native REA tools)`)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
sync,
|
|
154
|
+
dispose: disposeAll,
|
|
155
|
+
get nativeNames() {
|
|
156
|
+
return nativeNames
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Add a short routing section only while the engine can actually serve tools. */
|
|
162
|
+
function registerPromptSection(ctx, logger) {
|
|
163
|
+
try {
|
|
164
|
+
ctx.inject(['systemPrompt'], scope => {
|
|
165
|
+
scope.effect(() => {
|
|
166
|
+
const section = scope.systemPrompt.section({
|
|
167
|
+
name: 'dsh-reverse-engine',
|
|
168
|
+
order: 8200,
|
|
169
|
+
text:
|
|
170
|
+
'Local reverse engineering is available through the dsh-reverse-engine plugin. '
|
|
171
|
+
+ 'Use `rea_tools` to search the complete REA catalog and `rea_call` to run any operation by name; '
|
|
172
|
+
+ 'core operations such as `binary_session`, `open_binary`, `inspect_artifact`, and `analyze_function` are also registered directly under their canonical REA names. '
|
|
173
|
+
+ 'The `reverse-engineer-anything` skill documents the investigation and Evidence workflow. '
|
|
174
|
+
+ 'Targets and results stay on this machine; native providers (Ghidra, IDA, Hopper, Kuna) must be installed and configured by the user, and the sidebar console reports their readiness.',
|
|
175
|
+
})
|
|
176
|
+
return () => {
|
|
177
|
+
try {
|
|
178
|
+
if (typeof section === 'function') section()
|
|
179
|
+
else section?.dispose?.()
|
|
180
|
+
} catch {
|
|
181
|
+
// ignore
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}, 'dsh-reverse-engine: prompt section')
|
|
185
|
+
})
|
|
186
|
+
} catch (error) {
|
|
187
|
+
logger.warn(`[dsh-reverse-engine] prompt section unavailable: ${shortError(error)}`)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
193
|
+
*/
|
|
194
|
+
export async function apply(ctx) {
|
|
195
|
+
const logger = makeLogger(ctx)
|
|
196
|
+
const dataDirectory = resolveDataDirectory(ctx)
|
|
197
|
+
// The engine child uses this directory as its cwd, so it must exist before a
|
|
198
|
+
// fresh profile starts the plugin.
|
|
199
|
+
fs.mkdirSync(dataDirectory, { recursive: true })
|
|
200
|
+
const store = new SettingsStore(dataDirectory, { logger })
|
|
201
|
+
const service = new ReaService({ logger, store, dataDirectory })
|
|
202
|
+
const registry = createToolRegistry(ctx, service, logger)
|
|
203
|
+
service.onToolsChanged(() => registry.sync())
|
|
204
|
+
|
|
205
|
+
await service.init()
|
|
206
|
+
registerRoutes(ctx, service, logger)
|
|
207
|
+
registerPromptSection(ctx, logger)
|
|
208
|
+
registry.sync()
|
|
209
|
+
|
|
210
|
+
ctx.effect(() => {
|
|
211
|
+
return () => {
|
|
212
|
+
registry.dispose()
|
|
213
|
+
service.shutdown().catch(error => logger.warn(`[dsh-reverse-engine] shutdown: ${shortError(error)}`))
|
|
214
|
+
}
|
|
215
|
+
}, 'dsh-reverse-engine: lifecycle')
|
|
216
|
+
|
|
217
|
+
logger.info(`[dsh-reverse-engine] host ready (engine: ${service.engine.status}, data: ${dataDirectory})`)
|
|
218
|
+
}
|
package/lib/catalog.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog helpers over the live `tools/list` result. Family labels come from a
|
|
3
|
+
* snapshot generated from the pinned rea-agents release; unknown or newer tools
|
|
4
|
+
* fall back to `other` instead of disappearing.
|
|
5
|
+
*/
|
|
6
|
+
import { readJsonFileSync } from './util.js'
|
|
7
|
+
import { CORE_TOOLS } from './settings.js'
|
|
8
|
+
|
|
9
|
+
const META = readJsonFileSync(new URL('./rea-catalog.json', import.meta.url), { families: [], toolFamily: {} })
|
|
10
|
+
|
|
11
|
+
export const CATALOG_META = META
|
|
12
|
+
|
|
13
|
+
/** Family id for one live tool name. */
|
|
14
|
+
export function familyOf(name) {
|
|
15
|
+
return META.toolFamily?.[name] ?? 'other'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Families with the count the live catalog currently advertises. */
|
|
19
|
+
export function familiesOf(tools) {
|
|
20
|
+
const counts = new Map()
|
|
21
|
+
for (const tool of tools) {
|
|
22
|
+
const family = familyOf(tool.name)
|
|
23
|
+
counts.set(family, (counts.get(family) ?? 0) + 1)
|
|
24
|
+
}
|
|
25
|
+
const order = Array.isArray(META.families) ? META.families.map(family => family.id) : []
|
|
26
|
+
const ids = [...counts.keys()].sort((left, right) => {
|
|
27
|
+
const leftIndex = order.indexOf(left)
|
|
28
|
+
const rightIndex = order.indexOf(right)
|
|
29
|
+
if (leftIndex === -1 && rightIndex === -1) return left.localeCompare(right)
|
|
30
|
+
if (leftIndex === -1) return 1
|
|
31
|
+
if (rightIndex === -1) return -1
|
|
32
|
+
return leftIndex - rightIndex
|
|
33
|
+
})
|
|
34
|
+
return ids.map(id => ({ id, count: counts.get(id) }))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function compact(tool) {
|
|
38
|
+
return {
|
|
39
|
+
name: tool.name,
|
|
40
|
+
title: tool.title,
|
|
41
|
+
family: familyOf(tool.name),
|
|
42
|
+
description: tool.description,
|
|
43
|
+
readOnly: tool.annotations?.readOnlyHint === true,
|
|
44
|
+
destructive: tool.annotations?.destructiveHint === true,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Search canonical names and descriptions. Every word must occur in the haystack
|
|
50
|
+
* for a tool to rank first; partial matches still appear after full matches.
|
|
51
|
+
*/
|
|
52
|
+
export function searchTools(tools, query, options = {}) {
|
|
53
|
+
const limit = Math.min(500, Math.max(1, Number(options.limit) || 20))
|
|
54
|
+
const offset = Math.max(0, Number(options.offset) || 0)
|
|
55
|
+
const family = options.family && options.family !== 'all' ? String(options.family) : null
|
|
56
|
+
const words = String(query ?? '').toLowerCase().split(/\s+/).filter(Boolean).slice(0, 8)
|
|
57
|
+
const indexed = (tools ?? []).filter(tool => !family || familyOf(tool.name) === family)
|
|
58
|
+
const scored = []
|
|
59
|
+
for (const tool of indexed) {
|
|
60
|
+
const name = tool.name.toLowerCase()
|
|
61
|
+
const title = String(tool.title ?? '').toLowerCase()
|
|
62
|
+
const description = String(tool.description ?? '').toLowerCase()
|
|
63
|
+
if (words.length === 0) {
|
|
64
|
+
scored.push({ tool, score: 0, matched: 0 })
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
let matched = 0
|
|
68
|
+
let score = 0
|
|
69
|
+
for (const word of words) {
|
|
70
|
+
const inName = name.includes(word)
|
|
71
|
+
const inTitle = title.includes(word)
|
|
72
|
+
const inDescription = description.includes(word)
|
|
73
|
+
if (!inName && !inTitle && !inDescription) continue
|
|
74
|
+
matched += 1
|
|
75
|
+
if (name === word) score += 100
|
|
76
|
+
else if (name.startsWith(word)) score += 40
|
|
77
|
+
else if (inName) score += 20
|
|
78
|
+
if (inTitle) score += 10
|
|
79
|
+
if (inDescription) score += 4
|
|
80
|
+
}
|
|
81
|
+
if (matched > 0) scored.push({ tool, score: score + matched * 3, matched })
|
|
82
|
+
}
|
|
83
|
+
scored.sort((left, right) => {
|
|
84
|
+
if (right.matched !== left.matched) return right.matched - left.matched
|
|
85
|
+
if (right.score !== left.score) return right.score - left.score
|
|
86
|
+
return left.tool.name.localeCompare(right.tool.name)
|
|
87
|
+
})
|
|
88
|
+
const page = scored.slice(offset, offset + limit)
|
|
89
|
+
return {
|
|
90
|
+
total: scored.length,
|
|
91
|
+
offset,
|
|
92
|
+
limit,
|
|
93
|
+
hasMore: offset + page.length < scored.length,
|
|
94
|
+
tools: page.map(item => compact(item.tool)),
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The tool names registered directly on ctx.tools for one exposure mode. */
|
|
99
|
+
export function exposedToolNames(tools, settings) {
|
|
100
|
+
const available = new Set((tools ?? []).map(tool => tool.name))
|
|
101
|
+
if (settings.toolExposure === 'all') return [...available]
|
|
102
|
+
if (settings.toolExposure === 'families') {
|
|
103
|
+
const selected = new Set(settings.toolFamilies ?? [])
|
|
104
|
+
if (selected.size === 0) return CORE_TOOLS.filter(name => available.has(name))
|
|
105
|
+
return [...available].filter(name => selected.has(familyOf(name)))
|
|
106
|
+
}
|
|
107
|
+
return CORE_TOOLS.filter(name => available.has(name))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Estimated JSON bytes of the schemas that will enter the model context. */
|
|
111
|
+
export function schemaBytes(tools, names) {
|
|
112
|
+
const wanted = new Set(names)
|
|
113
|
+
let bytes = 0
|
|
114
|
+
for (const tool of tools ?? []) {
|
|
115
|
+
if (!wanted.has(tool.name)) continue
|
|
116
|
+
try {
|
|
117
|
+
bytes += Buffer.byteLength(JSON.stringify({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema }), 'utf8')
|
|
118
|
+
} catch {
|
|
119
|
+
// Ignore an unencodable schema; it will be skipped at registration.
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return bytes
|
|
123
|
+
}
|