@neuralnomads/codenomad 0.16.0 → 0.18.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/README.md +50 -4
- package/dist/background-processes/manager.js +0 -1
- package/dist/config/schema.js +1 -0
- package/dist/index.js +9 -6
- package/dist/launcher.js +12 -1
- package/dist/opencode-plugin/codenomad-opencode-plugin.tgz +0 -0
- package/dist/server/__tests__/listener-base-url.test.js +30 -0
- package/dist/server/http-server.js +6 -138
- package/dist/server/listener-base-url.js +17 -0
- package/dist/server/routes/config-files.js +92 -0
- package/dist/server/routes/config-files.test.js +115 -0
- package/dist/server/routes/workspaces.js +15 -0
- package/dist/workspaces/__tests__/git-clone.test.js +3 -2
- package/dist/workspaces/__tests__/spawn.test.js +1 -1
- package/dist/workspaces/manager.js +19 -1
- package/dist/workspaces/worktree-map.js +16 -0
- package/package.json +1 -1
- package/public/assets/FilesTab-CFF9uJuc.js +2 -0
- package/public/assets/GitChangesTab-DyRStL3G.js +2 -0
- package/public/assets/{SplitFilePanel-CiZrHI3o.js → SplitFilePanel-C4FztlTR.js} +1 -1
- package/public/assets/StatusTab-CcoPFya8.js +1 -0
- package/public/assets/{align-justify-CKbOni6U.js → align-justify-GNs_d9nT.js} +1 -1
- package/public/assets/{bundle-full-B_MbXXHv.js → bundle-full-B0vc8NGv.js} +2 -2
- package/public/assets/{diff-viewer-CersJruo.js → diff-viewer-BmSzmlPC.js} +1 -1
- package/public/assets/index--bYE3fA1.js +1 -0
- package/public/assets/index-B-9c-wPM.css +1 -0
- package/public/assets/index-BDJqHo1E.js +1 -0
- package/public/assets/index-BEXDNmDA.js +1 -0
- package/public/assets/index-BWX6Cyx3.js +2 -0
- package/public/assets/index-Bv867yIC.js +1 -0
- package/public/assets/index-CUB1cngT.js +1 -0
- package/public/assets/index-ChDVvMF4.js +1 -0
- package/public/assets/index-CyGPk9Qx.js +1 -0
- package/public/assets/index-D9T8sYiH.js +1 -0
- package/public/assets/index-DQ8RfkwK.js +1 -0
- package/public/assets/index-udaOwtFG.js +1 -0
- package/public/assets/{loading-hkCubi3k.js → loading-DRzViX8M.js} +1 -1
- package/public/assets/main-DUP17Bqi.js +68 -0
- package/public/assets/{markdown-Cy9tCAXp.js → markdown-BlxzERp2.js} +3 -3
- package/public/assets/monaco-viewer-BbohfXZk.js +26 -0
- package/public/assets/tool-call-B0Ri40qI.js +59 -0
- package/public/assets/{unified-picker-C47f5lFi.js → unified-picker-CGg8mlSo.js} +1 -1
- package/public/assets/{wrap-text-hwXbREuU.js → wrap-text-CMFBVbBi.js} +1 -1
- package/public/index.html +4 -4
- package/public/loading.html +4 -4
- package/public/sw.js +1 -1
- package/public/ui-version.json +1 -1
- package/public/assets/ChangesTab-CxrJwz2Z.js +0 -2
- package/public/assets/DiffToolbar-CqJttTgx.js +0 -1
- package/public/assets/FilesTab-tBrdrLID.js +0 -2
- package/public/assets/GitChangesTab-DUUJn4Va.js +0 -2
- package/public/assets/StatusTab-DN31GijL.js +0 -1
- package/public/assets/index-BTenW4sL.js +0 -1
- package/public/assets/index-BsYORY4z.js +0 -1
- package/public/assets/index-BvfQjuig.js +0 -1
- package/public/assets/index-CK51N0B7.js +0 -1
- package/public/assets/index-Chghci_R.js +0 -1
- package/public/assets/index-DYa3o0_M.js +0 -1
- package/public/assets/index-DskBWxXS.js +0 -2
- package/public/assets/index-DslkXxdb.js +0 -1
- package/public/assets/index-UCZ7MxNK.js +0 -1
- package/public/assets/index-i5UooSrU.css +0 -1
- package/public/assets/main-CXJTHRp1.js +0 -62
- package/public/assets/monaco-viewer-B3iAKbO0.js +0 -26
- package/public/assets/tool-call-DhWimeRu.js +0 -59
package/README.md
CHANGED
|
@@ -32,9 +32,11 @@
|
|
|
32
32
|
You can run CodeNomad directly without installing it:
|
|
33
33
|
|
|
34
34
|
```sh
|
|
35
|
-
npx @neuralnomads/codenomad --launch
|
|
35
|
+
npx @neuralnomads/codenomad --password <your-password> --launch
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
> **Authentication required:** The server requires a password. Pass it via `--password`, the `CODENOMAD_SERVER_PASSWORD` environment variable, or create an `auth.json` file (see [Authentication](#authentication) below).
|
|
39
|
+
|
|
38
40
|
To list all CLI options:
|
|
39
41
|
|
|
40
42
|
```sh
|
|
@@ -52,7 +54,7 @@ Or install it globally to use the `codenomad` command:
|
|
|
52
54
|
|
|
53
55
|
```sh
|
|
54
56
|
npm install -g @neuralnomads/codenomad
|
|
55
|
-
codenomad --launch
|
|
57
|
+
codenomad --password <your-password> --launch
|
|
56
58
|
```
|
|
57
59
|
|
|
58
60
|
### Install Locally (per-project)
|
|
@@ -61,7 +63,7 @@ If you prefer to install CodeNomad into a project and run the local binary:
|
|
|
61
63
|
|
|
62
64
|
```sh
|
|
63
65
|
npm install @neuralnomads/codenomad
|
|
64
|
-
npx codenomad --launch
|
|
66
|
+
npx codenomad --password <your-password> --launch
|
|
65
67
|
```
|
|
66
68
|
|
|
67
69
|
(`npx codenomad ...` will use `./node_modules/.bin/codenomad` when present.)
|
|
@@ -102,7 +104,7 @@ You can configure the server using flags or environment variables:
|
|
|
102
104
|
If you want the latest bleeding-edge builds (published as GitHub pre-releases), use the dev package:
|
|
103
105
|
|
|
104
106
|
```sh
|
|
105
|
-
npx @neuralnomads/codenomad-dev --launch
|
|
107
|
+
npx @neuralnomads/codenomad-dev --password <your-password> --launch
|
|
106
108
|
```
|
|
107
109
|
|
|
108
110
|
These environment variables control how CodeNomad checks for dev updates:
|
|
@@ -148,6 +150,14 @@ Certificates are valid for about 30 days and rotate automatically on startup whe
|
|
|
148
150
|
codenomad --tlsSANs "localhost,127.0.0.1,my-hostname,192.168.1.10"
|
|
149
151
|
```
|
|
150
152
|
|
|
153
|
+
> **Browser warning:** Self-signed certificates trigger a "Your connection is not private" warning in browsers on first visit. This is expected and safe for local development (127.0.0.1 / localhost):
|
|
154
|
+
>
|
|
155
|
+
> 1. **Chrome/Brave/Edge:** Click **Advanced** → **Proceed to 127.0.0.1 (unsafe)**
|
|
156
|
+
> 2. **Firefox:** Click **Advanced** → **Accept the Risk and Continue**
|
|
157
|
+
> 3. **Alternative:** For local-only development without the warning, run with `--https=false --http=true`
|
|
158
|
+
>
|
|
159
|
+
> **Note:** Only accept self-signed certificates for localhost/127.0.0.1 that you control. For remote hosts, use proper TLS certificates.
|
|
160
|
+
|
|
151
161
|
### Authentication
|
|
152
162
|
|
|
153
163
|
- Default behavior: CodeNomad requires a login (username/password) and stores a session cookie in the browser.
|
|
@@ -155,6 +165,42 @@ codenomad --tlsSANs "localhost,127.0.0.1,my-hostname,192.168.1.10"
|
|
|
155
165
|
Use this only when access is already protected by another layer (SSO proxy, VPN, Coder workspace auth, etc.).
|
|
156
166
|
If you bind to `0.0.0.0` while skipping auth, anyone who can reach the port can access the API.
|
|
157
167
|
|
|
168
|
+
#### Setting a password
|
|
169
|
+
|
|
170
|
+
**Practical setup options:**
|
|
171
|
+
|
|
172
|
+
1. **Runtime password (every start):** Use `--password <your-password>` or set `CODENOMAD_SERVER_PASSWORD=<your-password>` environment variable
|
|
173
|
+
2. **Persistent password (UI setup):** Launch with `--generate-token`, complete the local bootstrap flow in your browser, then set a password through the UI settings
|
|
174
|
+
|
|
175
|
+
The `--password` flag and `CODENOMAD_SERVER_PASSWORD` env var are **runtime credentials** — they must be provided on every server start and are not persisted to disk.
|
|
176
|
+
|
|
177
|
+
**Advanced: `auth.json` internals**
|
|
178
|
+
|
|
179
|
+
The `auth.json` file (`~/.config/codenomad/auth.json`) is automatically created and managed by CodeNomad when you set a password through the UI. You generally don't need to edit this file manually. For reference, it uses the following scrypt-based schema:
|
|
180
|
+
|
|
181
|
+
```json
|
|
182
|
+
{
|
|
183
|
+
"version": 1,
|
|
184
|
+
"username": "codenomad",
|
|
185
|
+
"password": {
|
|
186
|
+
"algorithm": "scrypt",
|
|
187
|
+
"saltBase64": "<base64-salt>",
|
|
188
|
+
"hashBase64": "<base64-hash>",
|
|
189
|
+
"keyLength": 64,
|
|
190
|
+
"params": {
|
|
191
|
+
"N": 16384,
|
|
192
|
+
"r": 8,
|
|
193
|
+
"p": 1,
|
|
194
|
+
"maxmem": 33554432
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
"userProvided": true,
|
|
198
|
+
"updatedAt": "2026-05-18T12:00:00.000Z"
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Manual creation of this file is not recommended unless you have a helper to generate a valid scrypt `PasswordHashRecord`.
|
|
203
|
+
|
|
158
204
|
### Progressive Web App (PWA)
|
|
159
205
|
|
|
160
206
|
When running as a server CodeNomad can also be installed as a PWA from any supported browser, giving you a native app experience just like the Electron installation but executing on the remote server instead.
|
|
@@ -521,7 +521,6 @@ export class BackgroundProcessManager {
|
|
|
521
521
|
const targetUrl = `http://127.0.0.1:${port}/session/${encodeURIComponent(notify.sessionID)}/prompt_async`;
|
|
522
522
|
const headers = {
|
|
523
523
|
"content-type": "application/json",
|
|
524
|
-
"x-opencode-directory": /[^\x00-\x7F]/.test(notify.directory) ? encodeURIComponent(notify.directory) : notify.directory,
|
|
525
524
|
};
|
|
526
525
|
const authorization = this.deps.workspaceManager.getInstanceAuthorizationHeader(workspaceId);
|
|
527
526
|
if (authorization) {
|
package/dist/config/schema.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ import { AuthManager, BOOTSTRAP_TOKEN_STDOUT_PREFIX, DEFAULT_AUTH_COOKIE_NAME, D
|
|
|
22
22
|
import { resolveHttpsOptions } from "./server/tls";
|
|
23
23
|
import { RemoteProxySessionManager } from "./server/remote-proxy";
|
|
24
24
|
import { resolveNetworkAddresses, resolveRemoteAddresses } from "./server/network-addresses";
|
|
25
|
+
import { resolvePluginBaseUrl } from "./server/listener-base-url";
|
|
25
26
|
import { startDevReleaseMonitor } from "./releases/dev-release-monitor";
|
|
26
27
|
import { SpeechService } from "./speech/service";
|
|
27
28
|
import { SideCarManager } from "./sidecars/manager";
|
|
@@ -365,13 +366,7 @@ async function main() {
|
|
|
365
366
|
throw new Error("No listeners started");
|
|
366
367
|
}
|
|
367
368
|
const remoteStart = httpsStart ?? httpStart;
|
|
368
|
-
const localProtocol = httpStart ? "http" : "https";
|
|
369
369
|
const remoteProtocol = httpsStart ? "https" : "http";
|
|
370
|
-
// Use an explicit IPv4 loopback address for the "local" URL.
|
|
371
|
-
// On macOS, `localhost` often resolves to ::1 first, and it is possible to have
|
|
372
|
-
// another instance bound on IPv6 while this instance binds IPv4 (or vice versa),
|
|
373
|
-
// which can lead clients to talk to the wrong process.
|
|
374
|
-
const localUrl = `${localProtocol}://127.0.0.1:${localStart.port}`;
|
|
375
370
|
let remoteUrl;
|
|
376
371
|
let remoteAddresses = [];
|
|
377
372
|
if (remoteStart) {
|
|
@@ -391,6 +386,14 @@ async function main() {
|
|
|
391
386
|
remoteUrl = `${remoteProtocol}://${remoteHost}:${remoteStart.port}`;
|
|
392
387
|
}
|
|
393
388
|
}
|
|
389
|
+
// Prefer an explicit IPv4 loopback address only when one of the bound listeners
|
|
390
|
+
// accepts loopback. Concrete LAN bindings do not, so plugins need the reachable
|
|
391
|
+
// bound/listener URL instead of an unreachable 127.0.0.1 URL.
|
|
392
|
+
const localUrl = resolvePluginBaseUrl({
|
|
393
|
+
httpStart: httpStart ? { protocol: "http", bindHost: httpBindHost, port: httpStart.port } : null,
|
|
394
|
+
httpsStart: httpsStart ? { protocol: "https", bindHost: httpsBindHost, port: httpsStart.port } : null,
|
|
395
|
+
remoteUrl,
|
|
396
|
+
});
|
|
394
397
|
serverMeta.localUrl = localUrl;
|
|
395
398
|
serverMeta.localPort = localStart.port;
|
|
396
399
|
serverMeta.remoteUrl = remoteUrl;
|
package/dist/launcher.js
CHANGED
|
@@ -119,11 +119,19 @@ function buildLinuxCandidates() {
|
|
|
119
119
|
"chromium",
|
|
120
120
|
"chromium-browser",
|
|
121
121
|
"brave-browser",
|
|
122
|
+
"brave-browser-stable",
|
|
123
|
+
"brave",
|
|
124
|
+
"/snap/bin/brave",
|
|
122
125
|
"microsoft-edge",
|
|
123
126
|
"microsoft-edge-stable",
|
|
124
127
|
"vivaldi",
|
|
128
|
+
"vivaldi-stable",
|
|
129
|
+
"/snap/bin/vivaldi",
|
|
130
|
+
];
|
|
131
|
+
return [
|
|
132
|
+
...names.map((name) => ({ name, command: name, args: APP_ARGS })),
|
|
133
|
+
{ name: "xdg-open", command: "xdg-open", args: (url) => [url] },
|
|
125
134
|
];
|
|
126
|
-
return names.map((name) => ({ name, command: name, args: APP_ARGS }));
|
|
127
135
|
}
|
|
128
136
|
function buildMacManualExamples(url) {
|
|
129
137
|
return [
|
|
@@ -144,6 +152,9 @@ function buildLinuxManualExamples(url) {
|
|
|
144
152
|
`google-chrome --app="${url}" --new-window`,
|
|
145
153
|
`chromium --app="${url}" --new-window`,
|
|
146
154
|
`brave-browser --app="${url}" --new-window`,
|
|
155
|
+
`brave-browser-stable --app="${url}" --new-window`,
|
|
147
156
|
`microsoft-edge --app="${url}" --new-window`,
|
|
157
|
+
`vivaldi-stable --app="${url}" --new-window`,
|
|
158
|
+
`xdg-open "${url}"`,
|
|
148
159
|
];
|
|
149
160
|
}
|
|
Binary file
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { resolvePluginBaseUrl } from "../listener-base-url";
|
|
4
|
+
describe("resolvePluginBaseUrl", () => {
|
|
5
|
+
it("keeps loopback URLs for default local listeners", () => {
|
|
6
|
+
assert.equal(resolvePluginBaseUrl({
|
|
7
|
+
httpsStart: { protocol: "https", bindHost: "127.0.0.1", port: 9898 },
|
|
8
|
+
remoteUrl: "https://localhost:9898",
|
|
9
|
+
}), "https://127.0.0.1:9898");
|
|
10
|
+
});
|
|
11
|
+
it("uses the concrete LAN listener when no loopback listener exists", () => {
|
|
12
|
+
assert.equal(resolvePluginBaseUrl({
|
|
13
|
+
httpsStart: { protocol: "https", bindHost: "192.168.1.25", port: 9898 },
|
|
14
|
+
remoteUrl: "https://192.168.1.25:9898",
|
|
15
|
+
}), "https://192.168.1.25:9898");
|
|
16
|
+
});
|
|
17
|
+
it("prefers loopback for wildcard listeners because 0.0.0.0 accepts loopback", () => {
|
|
18
|
+
assert.equal(resolvePluginBaseUrl({
|
|
19
|
+
httpsStart: { protocol: "https", bindHost: "0.0.0.0", port: 9898 },
|
|
20
|
+
remoteUrl: "https://192.168.1.25:9898",
|
|
21
|
+
}), "https://127.0.0.1:9898");
|
|
22
|
+
});
|
|
23
|
+
it("keeps loopback HTTP when remote HTTPS also exists", () => {
|
|
24
|
+
assert.equal(resolvePluginBaseUrl({
|
|
25
|
+
httpStart: { protocol: "http", bindHost: "127.0.0.1", port: 9899 },
|
|
26
|
+
httpsStart: { protocol: "https", bindHost: "192.168.1.25", port: 9898 },
|
|
27
|
+
remoteUrl: "https://192.168.1.25:9898",
|
|
28
|
+
}), "http://127.0.0.1:9899");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -7,11 +7,10 @@ import { connect as connectTcp } from "net";
|
|
|
7
7
|
import path from "path";
|
|
8
8
|
import { connect as connectTls } from "tls";
|
|
9
9
|
import { fetch } from "undici";
|
|
10
|
-
import { isValidWorktreeSlug } from "../workspaces/git-worktrees";
|
|
11
|
-
import { resolveWorktreeDirectory } from "../workspaces/worktree-directory";
|
|
12
10
|
import { registerWorkspaceRoutes } from "./routes/workspaces";
|
|
13
11
|
import { registerSettingsRoutes } from "./routes/settings";
|
|
14
12
|
import { registerFilesystemRoutes } from "./routes/filesystem";
|
|
13
|
+
import { registerConfigFileRoutes } from "./routes/config-files";
|
|
15
14
|
import { registerMetaRoutes } from "./routes/meta";
|
|
16
15
|
import { registerEventRoutes } from "./routes/events";
|
|
17
16
|
import { registerStorageRoutes } from "./routes/storage";
|
|
@@ -194,6 +193,7 @@ export function createHttpServer(deps) {
|
|
|
194
193
|
registerWorkspaceRoutes(app, { workspaceManager: deps.workspaceManager });
|
|
195
194
|
registerSettingsRoutes(app, { settings: deps.settings, logger: apiLogger });
|
|
196
195
|
registerFilesystemRoutes(app, { fileSystemBrowser: deps.fileSystemBrowser });
|
|
196
|
+
registerConfigFileRoutes(app);
|
|
197
197
|
registerMetaRoutes(app, { serverMeta: deps.serverMeta });
|
|
198
198
|
registerEventRoutes(app, {
|
|
199
199
|
eventBus: deps.eventBus,
|
|
@@ -385,7 +385,6 @@ function registerInstanceProxyRoutes(app, deps) {
|
|
|
385
385
|
request,
|
|
386
386
|
reply,
|
|
387
387
|
workspaceManager: deps.workspaceManager,
|
|
388
|
-
worktreeSlug: request.params.slug,
|
|
389
388
|
pathSuffix: "",
|
|
390
389
|
logger: deps.logger,
|
|
391
390
|
});
|
|
@@ -395,32 +394,17 @@ function registerInstanceProxyRoutes(app, deps) {
|
|
|
395
394
|
request,
|
|
396
395
|
reply,
|
|
397
396
|
workspaceManager: deps.workspaceManager,
|
|
398
|
-
worktreeSlug: request.params.slug,
|
|
399
397
|
pathSuffix: request.params["*"] ?? "",
|
|
400
398
|
logger: deps.logger,
|
|
401
399
|
});
|
|
402
400
|
};
|
|
403
|
-
instance.all("/workspaces/:id/
|
|
404
|
-
instance.all("/workspaces/:id/
|
|
401
|
+
instance.all("/workspaces/:id/instance", proxyBaseHandler);
|
|
402
|
+
instance.all("/workspaces/:id/instance/*", proxyWildcardHandler);
|
|
405
403
|
});
|
|
406
404
|
}
|
|
407
405
|
const INSTANCE_PROXY_HOST = "127.0.0.1";
|
|
408
|
-
// Special-case OpenCode directory override.
|
|
409
|
-
//
|
|
410
|
-
// UI clients may need to scope certain requests to an arbitrary directory that is not
|
|
411
|
-
// part of the Git worktree list. Since the OpenCode SDK does not reliably support
|
|
412
|
-
// injecting per-request headers, we encode an override into the *path* and strip it
|
|
413
|
-
// before proxying to the instance.
|
|
414
|
-
//
|
|
415
|
-
// Example proxied request path:
|
|
416
|
-
// /workspaces/:id/worktrees/:slug/instance/__dir/<base64url>/session/create
|
|
417
|
-
//
|
|
418
|
-
// The server will decode <base64url> -> absolute directory, validate it, then set
|
|
419
|
-
// x-opencode-directory accordingly and forward the request to /session/create.
|
|
420
|
-
const OPENCODE_DIR_OVERRIDE_PREFIX = "__dir/";
|
|
421
|
-
const OPENCODE_DIR_OVERRIDE_MAX_LEN = 4096;
|
|
422
406
|
async function proxyWorkspaceRequest(args) {
|
|
423
|
-
const { request, reply, workspaceManager, logger
|
|
407
|
+
const { request, reply, workspaceManager, logger } = args;
|
|
424
408
|
const workspaceId = request.params.id;
|
|
425
409
|
const workspace = workspaceManager.get(workspaceId);
|
|
426
410
|
const bodyToJson = (body) => {
|
|
@@ -490,47 +474,7 @@ async function proxyWorkspaceRequest(args) {
|
|
|
490
474
|
reply.code(502).send({ error: "Workspace instance is not ready" });
|
|
491
475
|
return;
|
|
492
476
|
}
|
|
493
|
-
|
|
494
|
-
reply.code(400).send({ error: "Invalid worktree slug" });
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
let extracted;
|
|
498
|
-
try {
|
|
499
|
-
extracted = extractOpencodeDirectoryOverride(args.pathSuffix);
|
|
500
|
-
}
|
|
501
|
-
catch (error) {
|
|
502
|
-
const message = error instanceof Error ? error.message : "Invalid directory override";
|
|
503
|
-
reply.code(400).send({ error: message });
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
let directory = null;
|
|
507
|
-
let forwardedSuffix = extracted.forwardedSuffix;
|
|
508
|
-
if (extracted.overrideDirectory) {
|
|
509
|
-
try {
|
|
510
|
-
directory = validateAndNormalizeOverrideDirectory({
|
|
511
|
-
overrideDirectory: extracted.overrideDirectory,
|
|
512
|
-
workspaceRoot: workspace.path,
|
|
513
|
-
});
|
|
514
|
-
}
|
|
515
|
-
catch (error) {
|
|
516
|
-
const message = error instanceof Error ? error.message : "Invalid directory override";
|
|
517
|
-
reply.code(400).send({ error: message });
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
else {
|
|
522
|
-
directory = await resolveWorktreeDirectory({
|
|
523
|
-
workspaceId,
|
|
524
|
-
workspacePath: workspace.path,
|
|
525
|
-
worktreeSlug,
|
|
526
|
-
logger,
|
|
527
|
-
});
|
|
528
|
-
if (!directory) {
|
|
529
|
-
reply.code(404).send({ error: "Worktree not found" });
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
const normalizedSuffix = normalizeInstanceSuffix(forwardedSuffix);
|
|
477
|
+
const normalizedSuffix = normalizeInstanceSuffix(args.pathSuffix);
|
|
534
478
|
const queryIndex = (request.raw.url ?? "").indexOf("?");
|
|
535
479
|
const search = queryIndex >= 0 ? (request.raw.url ?? "").slice(queryIndex) : "";
|
|
536
480
|
const targetUrl = `http://${INSTANCE_PROXY_HOST}:${port}${normalizedSuffix}${search}`;
|
|
@@ -544,10 +488,6 @@ async function proxyWorkspaceRequest(args) {
|
|
|
544
488
|
if (instanceAuthHeader) {
|
|
545
489
|
headers.authorization = instanceAuthHeader;
|
|
546
490
|
}
|
|
547
|
-
// OpenCode expects the *full* path; we send it via header to avoid query tampering.
|
|
548
|
-
const isNonASCII = /[^\x00-\x7F]/.test(directory);
|
|
549
|
-
const encodedDirectory = isNonASCII ? encodeURIComponent(directory) : directory;
|
|
550
|
-
headers["x-opencode-directory"] = encodedDirectory;
|
|
551
491
|
if (logger.isLevelEnabled("trace")) {
|
|
552
492
|
const outgoing = {};
|
|
553
493
|
for (const [key, value] of Object.entries(headers)) {
|
|
@@ -564,8 +504,6 @@ async function proxyWorkspaceRequest(args) {
|
|
|
564
504
|
workspaceId,
|
|
565
505
|
method: request.method,
|
|
566
506
|
targetUrl,
|
|
567
|
-
worktreeSlug,
|
|
568
|
-
directory,
|
|
569
507
|
contentType: request.headers["content-type"],
|
|
570
508
|
body: bodyToJson(request.body),
|
|
571
509
|
headers: outgoing,
|
|
@@ -581,76 +519,6 @@ async function proxyWorkspaceRequest(args) {
|
|
|
581
519
|
},
|
|
582
520
|
});
|
|
583
521
|
}
|
|
584
|
-
function extractOpencodeDirectoryOverride(pathSuffix) {
|
|
585
|
-
if (!pathSuffix) {
|
|
586
|
-
return { overrideDirectory: null, forwardedSuffix: pathSuffix };
|
|
587
|
-
}
|
|
588
|
-
// Fastify wildcard param does not include a leading slash.
|
|
589
|
-
const trimmed = pathSuffix.replace(/^\/+/, "");
|
|
590
|
-
if (!trimmed.startsWith(OPENCODE_DIR_OVERRIDE_PREFIX)) {
|
|
591
|
-
return { overrideDirectory: null, forwardedSuffix: pathSuffix };
|
|
592
|
-
}
|
|
593
|
-
const rest = trimmed.slice(OPENCODE_DIR_OVERRIDE_PREFIX.length);
|
|
594
|
-
const slashIndex = rest.indexOf("/");
|
|
595
|
-
const encoded = (slashIndex >= 0 ? rest.slice(0, slashIndex) : rest).trim();
|
|
596
|
-
const remaining = slashIndex >= 0 ? rest.slice(slashIndex + 1) : "";
|
|
597
|
-
if (!encoded) {
|
|
598
|
-
throw new Error("Missing directory override");
|
|
599
|
-
}
|
|
600
|
-
if (encoded.length > OPENCODE_DIR_OVERRIDE_MAX_LEN) {
|
|
601
|
-
throw new Error("Directory override too large");
|
|
602
|
-
}
|
|
603
|
-
let overrideDirectory = "";
|
|
604
|
-
try {
|
|
605
|
-
overrideDirectory = decodeBase64Url(encoded);
|
|
606
|
-
}
|
|
607
|
-
catch {
|
|
608
|
-
throw new Error("Invalid directory override");
|
|
609
|
-
}
|
|
610
|
-
const forwardedSuffix = remaining;
|
|
611
|
-
return { overrideDirectory, forwardedSuffix };
|
|
612
|
-
}
|
|
613
|
-
function decodeBase64Url(input) {
|
|
614
|
-
// base64url -> base64
|
|
615
|
-
const normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
616
|
-
const padding = normalized.length % 4 === 0 ? "" : "=".repeat(4 - (normalized.length % 4));
|
|
617
|
-
const base64 = `${normalized}${padding}`;
|
|
618
|
-
return Buffer.from(base64, "base64").toString("utf-8");
|
|
619
|
-
}
|
|
620
|
-
function validateAndNormalizeOverrideDirectory(params) {
|
|
621
|
-
const raw = params.overrideDirectory.trim();
|
|
622
|
-
if (!raw) {
|
|
623
|
-
throw new Error("Override directory is empty");
|
|
624
|
-
}
|
|
625
|
-
if (!path.isAbsolute(raw)) {
|
|
626
|
-
throw new Error("Override directory must be an absolute path");
|
|
627
|
-
}
|
|
628
|
-
if (!fs.existsSync(raw)) {
|
|
629
|
-
throw new Error(`Override directory does not exist: ${raw}`);
|
|
630
|
-
}
|
|
631
|
-
const stats = fs.statSync(raw);
|
|
632
|
-
if (!stats.isDirectory()) {
|
|
633
|
-
throw new Error(`Override path is not a directory: ${raw}`);
|
|
634
|
-
}
|
|
635
|
-
const normalizedOverride = fs.realpathSync(raw);
|
|
636
|
-
const normalizedRoot = fs.realpathSync(params.workspaceRoot);
|
|
637
|
-
if (!isSubpath(normalizedOverride, normalizedRoot)) {
|
|
638
|
-
throw new Error("Override directory must be within the workspace root");
|
|
639
|
-
}
|
|
640
|
-
return normalizedOverride;
|
|
641
|
-
}
|
|
642
|
-
function isSubpath(candidate, root) {
|
|
643
|
-
const rel = path.relative(root, candidate);
|
|
644
|
-
if (rel === "")
|
|
645
|
-
return true;
|
|
646
|
-
if (rel === "..")
|
|
647
|
-
return false;
|
|
648
|
-
if (rel.startsWith(`..${path.sep}`))
|
|
649
|
-
return false;
|
|
650
|
-
if (path.isAbsolute(rel))
|
|
651
|
-
return false;
|
|
652
|
-
return true;
|
|
653
|
-
}
|
|
654
522
|
function normalizeInstanceSuffix(pathSuffix) {
|
|
655
523
|
if (!pathSuffix || pathSuffix === "/") {
|
|
656
524
|
return "/";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function resolvePluginBaseUrl(input) {
|
|
2
|
+
const loopbackListener = [input.httpStart, input.httpsStart].find((listener) => listener && acceptsLoopback(listener.bindHost));
|
|
3
|
+
if (loopbackListener) {
|
|
4
|
+
return `${loopbackListener.protocol}://127.0.0.1:${loopbackListener.port}`;
|
|
5
|
+
}
|
|
6
|
+
if (input.remoteUrl) {
|
|
7
|
+
return input.remoteUrl;
|
|
8
|
+
}
|
|
9
|
+
const fallbackListener = input.httpStart ?? input.httpsStart;
|
|
10
|
+
if (!fallbackListener) {
|
|
11
|
+
throw new Error("No listeners started");
|
|
12
|
+
}
|
|
13
|
+
return `${fallbackListener.protocol}://${fallbackListener.bindHost}:${fallbackListener.port}`;
|
|
14
|
+
}
|
|
15
|
+
function acceptsLoopback(bindHost) {
|
|
16
|
+
return bindHost === "0.0.0.0" || bindHost === "::" || bindHost === "localhost" || bindHost === "::1" || bindHost.startsWith("127.");
|
|
17
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import fs from "fs/promises";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
const ConfigFileContentBodySchema = z.object({
|
|
6
|
+
contents: z.string(),
|
|
7
|
+
});
|
|
8
|
+
function resolveOpenCodeGlobalConfigPaths() {
|
|
9
|
+
if (process.platform === "win32") {
|
|
10
|
+
const basePath = path.join(os.homedir(), ".config", "opencode");
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
id: "opencode-global-config",
|
|
14
|
+
label: "OpenCode Global Config",
|
|
15
|
+
path: "%USERPROFILE%\\.config\\opencode\\opencode.json",
|
|
16
|
+
absolutePath: path.join(basePath, "opencode.json"),
|
|
17
|
+
language: "json",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: "opencode-global-config-jsonc",
|
|
21
|
+
label: "OpenCode Global Config (JSONC)",
|
|
22
|
+
path: "%USERPROFILE%\\.config\\opencode\\opencode.jsonc",
|
|
23
|
+
absolutePath: path.join(basePath, "opencode.jsonc"),
|
|
24
|
+
language: "jsonc",
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
id: "opencode-global-config",
|
|
31
|
+
label: "OpenCode Global Config",
|
|
32
|
+
path: "~/.config/opencode/opencode.json",
|
|
33
|
+
absolutePath: path.join(os.homedir(), ".config", "opencode", "opencode.json"),
|
|
34
|
+
language: "json",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: "opencode-global-config-jsonc",
|
|
38
|
+
label: "OpenCode Global Config (JSONC)",
|
|
39
|
+
path: "~/.config/opencode/opencode.jsonc",
|
|
40
|
+
absolutePath: path.join(os.homedir(), ".config", "opencode", "opencode.jsonc"),
|
|
41
|
+
language: "jsonc",
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
function defaultConfigFileEntries() {
|
|
46
|
+
return resolveOpenCodeGlobalConfigPaths();
|
|
47
|
+
}
|
|
48
|
+
function listConfigFiles(files) {
|
|
49
|
+
return files.map(({ absolutePath: _absolutePath, ...file }) => file);
|
|
50
|
+
}
|
|
51
|
+
function getConfigFile(files, id) {
|
|
52
|
+
return files.find((file) => file.id === id) ?? null;
|
|
53
|
+
}
|
|
54
|
+
export function registerConfigFileRoutes(app, options = {}) {
|
|
55
|
+
const configFiles = options.files ?? defaultConfigFileEntries();
|
|
56
|
+
app.get("/api/config-files", async () => listConfigFiles(configFiles));
|
|
57
|
+
app.get("/api/config-files/:id/content", async (request, reply) => {
|
|
58
|
+
const file = getConfigFile(configFiles, request.params.id);
|
|
59
|
+
if (!file) {
|
|
60
|
+
reply.code(404);
|
|
61
|
+
return { error: "Config file not found" };
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const contents = await fs.readFile(file.absolutePath, "utf-8");
|
|
65
|
+
return { id: file.id, path: file.path, contents, exists: true };
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (error.code === "ENOENT") {
|
|
69
|
+
return { id: file.id, path: file.path, contents: "", exists: false };
|
|
70
|
+
}
|
|
71
|
+
reply.code(400);
|
|
72
|
+
return { error: error instanceof Error ? error.message : "Failed to read config file" };
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
app.put("/api/config-files/:id/content", async (request, reply) => {
|
|
76
|
+
const file = getConfigFile(configFiles, request.params.id);
|
|
77
|
+
if (!file) {
|
|
78
|
+
reply.code(404);
|
|
79
|
+
return { error: "Config file not found" };
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const body = ConfigFileContentBodySchema.parse(request.body ?? {});
|
|
83
|
+
await fs.mkdir(path.dirname(file.absolutePath), { recursive: true });
|
|
84
|
+
await fs.writeFile(file.absolutePath, body.contents, "utf-8");
|
|
85
|
+
reply.code(204);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
reply.code(400);
|
|
89
|
+
return { error: error instanceof Error ? error.message : "Failed to save config file" };
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { afterEach, describe, it } from "node:test";
|
|
6
|
+
import Fastify from "fastify";
|
|
7
|
+
import { registerConfigFileRoutes } from "./config-files";
|
|
8
|
+
const tempDirs = new Set();
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
for (const dir of tempDirs) {
|
|
11
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
12
|
+
}
|
|
13
|
+
tempDirs.clear();
|
|
14
|
+
});
|
|
15
|
+
describe("config file routes", () => {
|
|
16
|
+
it("lists only allowlisted config file descriptors", async () => {
|
|
17
|
+
const tempDir = createTempDir();
|
|
18
|
+
const app = createApp([
|
|
19
|
+
createConfigEntry(path.join(tempDir, "opencode", "opencode.json"), "~/.config/opencode/opencode.json"),
|
|
20
|
+
createConfigEntry(path.join(tempDir, "opencode", "opencode.jsonc"), "~/.config/opencode/opencode.jsonc", {
|
|
21
|
+
id: "test-config-jsonc",
|
|
22
|
+
label: "Test Config JSONC",
|
|
23
|
+
language: "jsonc",
|
|
24
|
+
}),
|
|
25
|
+
]);
|
|
26
|
+
const response = await app.inject({ method: "GET", url: "/api/config-files" });
|
|
27
|
+
assert.equal(response.statusCode, 200);
|
|
28
|
+
assert.deepEqual(response.json(), [
|
|
29
|
+
{
|
|
30
|
+
id: "test-config",
|
|
31
|
+
label: "Test Config",
|
|
32
|
+
path: "~/.config/opencode/opencode.json",
|
|
33
|
+
language: "json",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "test-config-jsonc",
|
|
37
|
+
label: "Test Config JSONC",
|
|
38
|
+
path: "~/.config/opencode/opencode.jsonc",
|
|
39
|
+
language: "jsonc",
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
await app.close();
|
|
43
|
+
});
|
|
44
|
+
it("returns empty content for an allowlisted missing file", async () => {
|
|
45
|
+
const tempDir = createTempDir();
|
|
46
|
+
const app = createApp([createConfigEntry(path.join(tempDir, "missing", "config.json"), "display/config.json")]);
|
|
47
|
+
const response = await app.inject({ method: "GET", url: "/api/config-files/test-config/content" });
|
|
48
|
+
assert.equal(response.statusCode, 200);
|
|
49
|
+
assert.deepEqual(response.json(), {
|
|
50
|
+
id: "test-config",
|
|
51
|
+
path: "display/config.json",
|
|
52
|
+
contents: "",
|
|
53
|
+
exists: false,
|
|
54
|
+
});
|
|
55
|
+
await app.close();
|
|
56
|
+
});
|
|
57
|
+
it("creates parent directories when writing an allowlisted file", async () => {
|
|
58
|
+
const tempDir = createTempDir();
|
|
59
|
+
const targetPath = path.join(tempDir, "nested", "opencode.json");
|
|
60
|
+
const app = createApp([createConfigEntry(targetPath, "display/opencode.json")]);
|
|
61
|
+
const response = await app.inject({
|
|
62
|
+
method: "PUT",
|
|
63
|
+
url: "/api/config-files/test-config/content",
|
|
64
|
+
payload: { contents: '{"model":"test"}' },
|
|
65
|
+
});
|
|
66
|
+
assert.equal(response.statusCode, 204);
|
|
67
|
+
assert.equal(fs.readFileSync(targetPath, "utf-8"), '{"model":"test"}');
|
|
68
|
+
await app.close();
|
|
69
|
+
});
|
|
70
|
+
it("rejects unknown config file ids", async () => {
|
|
71
|
+
const tempDir = createTempDir();
|
|
72
|
+
const app = createApp([createConfigEntry(path.join(tempDir, "opencode.json"), "display/opencode.json")]);
|
|
73
|
+
const readResponse = await app.inject({ method: "GET", url: "/api/config-files/unknown/content" });
|
|
74
|
+
const writeResponse = await app.inject({
|
|
75
|
+
method: "PUT",
|
|
76
|
+
url: "/api/config-files/unknown/content",
|
|
77
|
+
payload: { contents: "{}" },
|
|
78
|
+
});
|
|
79
|
+
assert.equal(readResponse.statusCode, 404);
|
|
80
|
+
assert.equal(writeResponse.statusCode, 404);
|
|
81
|
+
await app.close();
|
|
82
|
+
});
|
|
83
|
+
it("returns a client error when writes fail", async () => {
|
|
84
|
+
const tempDir = createTempDir();
|
|
85
|
+
const blockedParent = path.join(tempDir, "not-a-directory");
|
|
86
|
+
fs.writeFileSync(blockedParent, "occupied");
|
|
87
|
+
const app = createApp([createConfigEntry(path.join(blockedParent, "opencode.json"), "display/opencode.json")]);
|
|
88
|
+
const response = await app.inject({
|
|
89
|
+
method: "PUT",
|
|
90
|
+
url: "/api/config-files/test-config/content",
|
|
91
|
+
payload: { contents: "{}" },
|
|
92
|
+
});
|
|
93
|
+
assert.equal(response.statusCode, 400);
|
|
94
|
+
await app.close();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
function createTempDir() {
|
|
98
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "codenomad-config-files-test-"));
|
|
99
|
+
tempDirs.add(dir);
|
|
100
|
+
return dir;
|
|
101
|
+
}
|
|
102
|
+
function createConfigEntry(absolutePath, displayPath, overrides = {}) {
|
|
103
|
+
return {
|
|
104
|
+
id: overrides.id ?? "test-config",
|
|
105
|
+
label: overrides.label ?? "Test Config",
|
|
106
|
+
path: displayPath,
|
|
107
|
+
absolutePath,
|
|
108
|
+
language: overrides.language ?? "json",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function createApp(files) {
|
|
112
|
+
const app = Fastify({ logger: false });
|
|
113
|
+
registerConfigFileRoutes(app, { files });
|
|
114
|
+
return app;
|
|
115
|
+
}
|