@neuralnomads/codenomad 0.15.0 → 0.16.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.
Files changed (131) hide show
  1. package/dist/filesystem/__tests__/browser.test.js +65 -0
  2. package/dist/filesystem/__tests__/search-cache.test.js +2 -2
  3. package/dist/filesystem/browser.js +25 -4
  4. package/dist/index.js +4 -0
  5. package/dist/opencode-plugin/codenomad-opencode-plugin.tgz +0 -0
  6. package/dist/opencode-plugin.js +146 -0
  7. package/dist/opencode-plugin.test.js +28 -0
  8. package/dist/previews/manager.js +63 -0
  9. package/dist/server/http-server.js +372 -14
  10. package/dist/server/routes/filesystem.js +13 -0
  11. package/dist/server/routes/previews.js +25 -0
  12. package/dist/server/routes/workspaces.js +23 -1
  13. package/dist/workspaces/__tests__/git-clone.test.js +141 -0
  14. package/dist/workspaces/__tests__/spawn.test.js +26 -8
  15. package/dist/workspaces/git-clone.js +148 -0
  16. package/dist/workspaces/manager.js +8 -5
  17. package/dist/workspaces/spawn.js +35 -9
  18. package/package.json +3 -3
  19. package/public/assets/{ChangesTab-ovdqLHc3.js → ChangesTab-CxrJwz2Z.js} +2 -2
  20. package/public/assets/{DiffToolbar-B12EfXMX.js → DiffToolbar-CqJttTgx.js} +1 -1
  21. package/public/assets/{FilesTab-nb_itEXG.js → FilesTab-tBrdrLID.js} +2 -2
  22. package/public/assets/GitChangesTab-DUUJn4Va.js +2 -0
  23. package/public/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  24. package/public/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  25. package/public/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  26. package/public/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  27. package/public/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  28. package/public/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  29. package/public/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  30. package/public/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  31. package/public/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  32. package/public/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  33. package/public/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  34. package/public/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  35. package/public/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  36. package/public/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  37. package/public/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  38. package/public/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  39. package/public/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  40. package/public/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  41. package/public/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  42. package/public/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  43. package/public/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  44. package/public/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  45. package/public/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  46. package/public/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  47. package/public/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  48. package/public/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  49. package/public/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  50. package/public/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  51. package/public/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  52. package/public/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  53. package/public/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  54. package/public/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  55. package/public/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  56. package/public/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  57. package/public/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  58. package/public/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  59. package/public/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  60. package/public/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  61. package/public/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  62. package/public/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  63. package/public/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  64. package/public/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  65. package/public/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  66. package/public/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  67. package/public/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  68. package/public/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  69. package/public/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  70. package/public/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  71. package/public/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  72. package/public/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  73. package/public/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  74. package/public/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  75. package/public/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  76. package/public/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  77. package/public/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  78. package/public/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  79. package/public/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  80. package/public/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  81. package/public/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  82. package/public/assets/{SplitFilePanel-BOHH3F--.js → SplitFilePanel-CiZrHI3o.js} +1 -1
  83. package/public/assets/StatusTab-DN31GijL.js +1 -0
  84. package/public/assets/{align-justify-9zpyaAdy.js → align-justify-CKbOni6U.js} +1 -1
  85. package/public/assets/{bundle-full-xFW13Lny.js → bundle-full-B_MbXXHv.js} +1 -1
  86. package/public/assets/{diff-viewer-xG9yjpN8.js → diff-viewer-CersJruo.js} +1 -1
  87. package/public/assets/index-BTenW4sL.js +1 -0
  88. package/public/assets/index-BsYORY4z.js +1 -0
  89. package/public/assets/index-BvfQjuig.js +1 -0
  90. package/public/assets/{index-CCAZZY0l.js → index-CK51N0B7.js} +1 -1
  91. package/public/assets/index-Chghci_R.js +1 -0
  92. package/public/assets/{index-Cz_fl8aY.js → index-DYa3o0_M.js} +1 -1
  93. package/public/assets/index-DskBWxXS.js +2 -0
  94. package/public/assets/index-DslkXxdb.js +1 -0
  95. package/public/assets/index-UCZ7MxNK.js +1 -0
  96. package/public/assets/index-i5UooSrU.css +1 -0
  97. package/public/assets/{loading-HUhLEgb_.js → loading-hkCubi3k.js} +1 -1
  98. package/public/assets/main-CXJTHRp1.js +62 -0
  99. package/public/assets/markdown-Cy9tCAXp.js +315 -0
  100. package/public/assets/monaco-viewer-B3iAKbO0.js +26 -0
  101. package/public/assets/tool-call-DhWimeRu.js +59 -0
  102. package/public/assets/{unified-picker-DuNuUpLy.js → unified-picker-C47f5lFi.js} +1 -1
  103. package/public/assets/{wrap-text-zghCl6wN.js → wrap-text-hwXbREuU.js} +1 -1
  104. package/public/index.html +4 -4
  105. package/public/loading.html +4 -4
  106. package/public/sw.js +1 -1
  107. package/public/ui-version.json +1 -1
  108. package/dist/opencode-config/README.md +0 -32
  109. package/dist/opencode-config/opencode.jsonc +0 -3
  110. package/dist/opencode-config/package-lock.json +0 -380
  111. package/dist/opencode-config/package.json +0 -9
  112. package/dist/opencode-config/plugin/codenomad.ts +0 -62
  113. package/dist/opencode-config/plugin/lib/background-process.ts +0 -265
  114. package/dist/opencode-config/plugin/lib/client.ts +0 -133
  115. package/dist/opencode-config/plugin/lib/request.ts +0 -214
  116. package/dist/opencode-config.js +0 -26
  117. package/public/assets/GitChangesTab-DcT_4BL2.js +0 -2
  118. package/public/assets/StatusTab-Dyt_EG33.js +0 -1
  119. package/public/assets/index-7DpT014-.js +0 -1
  120. package/public/assets/index-BDK4V9L0.js +0 -1
  121. package/public/assets/index-Bx-Bezt2.js +0 -1
  122. package/public/assets/index-CEcZq5WB.js +0 -1
  123. package/public/assets/index-CQCmSDHL.js +0 -1
  124. package/public/assets/index-Dcgftm1m.css +0 -1
  125. package/public/assets/index-m4C3yx3J.js +0 -1
  126. package/public/assets/index-mh_z9p40.js +0 -2
  127. package/public/assets/main-CKhfaeKI.js +0 -48
  128. package/public/assets/markdown-BeXU9Z-x.js +0 -58
  129. package/public/assets/monaco-viewer-BgP6xpZ6.js +0 -26
  130. package/public/assets/todo-D6PVVp_f.js +0 -1
  131. package/public/assets/tool-call-Dgn3NlA8.js +0 -60
@@ -0,0 +1,65 @@
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 { FileSystemBrowser } from "../browser";
7
+ import { WINDOWS_DRIVES_ROOT } from "../../api-types";
8
+ const tempRoots = [];
9
+ describe("FileSystemBrowser", () => {
10
+ afterEach(() => {
11
+ for (const directory of tempRoots.splice(0)) {
12
+ fs.rmSync(directory, { recursive: true, force: true });
13
+ }
14
+ });
15
+ it("starts unrestricted browsing from the configured root", () => {
16
+ const rootDir = createTempRoot();
17
+ fs.mkdirSync(path.join(rootDir, "project"));
18
+ const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
19
+ const listing = browser.browse();
20
+ assert.equal(listing.metadata.scope, "unrestricted");
21
+ assert.equal(listing.metadata.currentPath, rootDir);
22
+ assert.equal(listing.metadata.rootPath, rootDir);
23
+ assert.equal(listing.metadata.displayPath, rootDir);
24
+ assert.equal(listing.metadata.pathKind, "absolute");
25
+ assert.ok(listing.entries.some((entry) => entry.name === "project" && entry.absolutePath === path.join(rootDir, "project")));
26
+ });
27
+ it("treats dot as the configured unrestricted root", () => {
28
+ const rootDir = createTempRoot();
29
+ const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
30
+ const listing = browser.browse(".");
31
+ assert.equal(listing.metadata.currentPath, rootDir);
32
+ assert.equal(listing.metadata.rootPath, rootDir);
33
+ });
34
+ it("allows unrestricted browsing outside the configured root", () => {
35
+ const rootDir = createTempRoot();
36
+ const parentDir = path.dirname(rootDir);
37
+ const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
38
+ const listing = browser.browse(parentDir);
39
+ assert.equal(listing.metadata.currentPath, parentDir);
40
+ assert.equal(listing.metadata.rootPath, rootDir);
41
+ assert.ok(listing.entries.some((entry) => entry.absolutePath === rootDir));
42
+ });
43
+ it("creates folders under the configured unrestricted root by default", () => {
44
+ const rootDir = createTempRoot();
45
+ const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
46
+ const created = browser.createFolder(undefined, "created-folder");
47
+ assert.equal(created.path, path.join(rootDir, "created-folder"));
48
+ assert.equal(created.absolutePath, path.join(rootDir, "created-folder"));
49
+ assert.equal(fs.statSync(created.absolutePath).isDirectory(), true);
50
+ });
51
+ it("reports the configured root for the Windows drives pseudo-root", () => {
52
+ const rootDir = createTempRoot();
53
+ const browser = new FileSystemBrowser({ rootDir, unrestricted: true, platform: "win32" });
54
+ const listing = browser.browse(WINDOWS_DRIVES_ROOT);
55
+ assert.equal(listing.metadata.scope, "unrestricted");
56
+ assert.equal(listing.metadata.currentPath, WINDOWS_DRIVES_ROOT);
57
+ assert.equal(listing.metadata.rootPath, rootDir);
58
+ assert.equal(listing.metadata.pathKind, "drives");
59
+ });
60
+ });
61
+ function createTempRoot() {
62
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "codenomad-browser-"));
63
+ tempRoots.push(root);
64
+ return fs.realpathSync(root);
65
+ }
@@ -19,11 +19,11 @@ describe("workspace search cache", () => {
19
19
  it("replaces cached entries when manually refreshed", () => {
20
20
  const workspacePath = "/tmp/workspace";
21
21
  refreshWorkspaceCandidates(workspacePath, () => [createEntry("file-a")], 5000);
22
- const initial = getWorkspaceCandidates(workspacePath);
22
+ const initial = getWorkspaceCandidates(workspacePath, 5001);
23
23
  assert.ok(initial);
24
24
  assert.equal(initial[0].name, "file-a");
25
25
  refreshWorkspaceCandidates(workspacePath, () => [createEntry("file-b")], 6000);
26
- const refreshed = getWorkspaceCandidates(workspacePath);
26
+ const refreshed = getWorkspaceCandidates(workspacePath, 6001);
27
27
  assert.ok(refreshed);
28
28
  assert.equal(refreshed[0].name, "file-b");
29
29
  });
@@ -3,12 +3,13 @@ import os from "os";
3
3
  import path from "path";
4
4
  import { WINDOWS_DRIVES_ROOT, } from "../api-types";
5
5
  const WINDOWS_DRIVE_LETTERS = Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i));
6
+ const MAX_READABLE_FILE_BYTES = 5 * 1024 * 1024;
6
7
  export class FileSystemBrowser {
7
8
  constructor(options) {
8
9
  this.root = path.resolve(options.rootDir);
9
10
  this.unrestricted = Boolean(options.unrestricted);
10
11
  this.homeDir = os.homedir();
11
- this.isWindows = process.platform === "win32";
12
+ this.isWindows = (options.platform ?? process.platform) === "win32";
12
13
  }
13
14
  list(relativePath = ".", options = {}) {
14
15
  if (this.unrestricted) {
@@ -64,6 +65,26 @@ export class FileSystemBrowser {
64
65
  const resolved = this.toRestrictedAbsolute(relativePath);
65
66
  return fs.readFileSync(resolved, "utf-8");
66
67
  }
68
+ readFileBase64(relativePath) {
69
+ if (this.unrestricted) {
70
+ throw new Error("readFileBase64 is not available in unrestricted mode");
71
+ }
72
+ const resolved = this.toRestrictedAbsolute(relativePath);
73
+ return fs.readFileSync(resolved).toString("base64");
74
+ }
75
+ readFileContent(targetPath, options) {
76
+ const encoding = options?.encoding ?? "utf-8";
77
+ const resolved = this.unrestricted ? this.resolveUnrestrictedPath(targetPath) : this.toRestrictedAbsolute(targetPath);
78
+ const stats = fs.statSync(resolved);
79
+ if (!stats.isFile()) {
80
+ throw new Error("Selected path is not a file");
81
+ }
82
+ if (stats.size > MAX_READABLE_FILE_BYTES) {
83
+ throw new Error("Selected file is too large to attach");
84
+ }
85
+ const contents = encoding === "base64" ? fs.readFileSync(resolved).toString("base64") : fs.readFileSync(resolved, "utf-8");
86
+ return { path: targetPath, contents, encoding };
87
+ }
67
88
  listRestrictedWithMetadata(relativePath, includeFiles) {
68
89
  const normalizedPath = this.normalizeRelativePath(relativePath);
69
90
  const absolutePath = this.toRestrictedAbsolute(normalizedPath);
@@ -98,7 +119,7 @@ export class FileSystemBrowser {
98
119
  scope: "unrestricted",
99
120
  currentPath: resolvedPath,
100
121
  parentPath,
101
- rootPath: this.homeDir,
122
+ rootPath: this.root,
102
123
  homePath: this.homeDir,
103
124
  displayPath: resolvedPath,
104
125
  pathKind: "absolute",
@@ -137,7 +158,7 @@ export class FileSystemBrowser {
137
158
  scope: "unrestricted",
138
159
  currentPath: WINDOWS_DRIVES_ROOT,
139
160
  parentPath: undefined,
140
- rootPath: this.homeDir,
161
+ rootPath: this.root,
141
162
  homePath: this.homeDir,
142
163
  displayPath: "Drives",
143
164
  pathKind: "drives",
@@ -253,7 +274,7 @@ export class FileSystemBrowser {
253
274
  }
254
275
  resolveUnrestrictedPath(input) {
255
276
  if (!input || input === "." || input === "./") {
256
- return this.homeDir;
277
+ return this.root;
257
278
  }
258
279
  if (this.isWindows) {
259
280
  if (input === WINDOWS_DRIVES_ROOT) {
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ import { resolveNetworkAddresses, resolveRemoteAddresses } from "./server/networ
25
25
  import { startDevReleaseMonitor } from "./releases/dev-release-monitor";
26
26
  import { SpeechService } from "./speech/service";
27
27
  import { SideCarManager } from "./sidecars/manager";
28
+ import { PreviewManager } from "./previews/manager";
28
29
  import { ClientConnectionManager } from "./clients/connection-manager";
29
30
  import { PluginChannelManager } from "./plugins/channel";
30
31
  import { VoiceModeManager } from "./plugins/voice-mode";
@@ -230,6 +231,7 @@ async function main() {
230
231
  eventBus,
231
232
  logger: logger.child({ component: "sidecars" }),
232
233
  });
234
+ const previewManager = new PreviewManager();
233
235
  const instanceEventBridge = new InstanceEventBridge({
234
236
  workspaceManager,
235
237
  eventBus,
@@ -313,6 +315,7 @@ async function main() {
313
315
  instanceStore,
314
316
  speechService,
315
317
  sidecarManager,
318
+ previewManager,
316
319
  authManager,
317
320
  clientConnectionManager,
318
321
  pluginChannel,
@@ -338,6 +341,7 @@ async function main() {
338
341
  instanceStore,
339
342
  speechService,
340
343
  sidecarManager,
344
+ previewManager,
341
345
  authManager,
342
346
  clientConnectionManager,
343
347
  pluginChannel,
@@ -0,0 +1,146 @@
1
+ import { existsSync, readdirSync } from "fs";
2
+ import path from "path";
3
+ import { fileURLToPath, pathToFileURL } from "url";
4
+ import { createLogger } from "./logger";
5
+ const log = createLogger({ component: "opencode-plugin" });
6
+ const pluginPackageName = "@codenomad/codenomad-opencode-plugin";
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+ const resourcesPath = process.resourcesPath;
10
+ const devPluginEntry = path.resolve(__dirname, "../../opencode-plugin/plugin/codenomad.ts");
11
+ const prodPluginDirs = [
12
+ resourcesPath ? path.resolve(resourcesPath, "opencode-plugin") : undefined,
13
+ resourcesPath ? path.resolve(resourcesPath, "server/dist/opencode-plugin") : undefined,
14
+ path.resolve(__dirname, "opencode-plugin"),
15
+ ].filter((dir) => Boolean(dir));
16
+ const isDevBuild = Boolean(process.env.CODENOMAD_DEV ??
17
+ process.env.CLI_UI_DEV_SERVER ??
18
+ process.env.VITE_DEV_SERVER_URL ??
19
+ process.env.ELECTRON_RENDERER_URL);
20
+ const isSourceRun = path.basename(__dirname) === "src" && existsSync(devPluginEntry);
21
+ export function getCodeNomadPluginUrl() {
22
+ if (isDevBuild || isSourceRun) {
23
+ if (!existsSync(devPluginEntry)) {
24
+ throw new Error(`CodeNomad OpenCode plugin entry missing at ${devPluginEntry}`);
25
+ }
26
+ log.debug({ pluginEntry: devPluginEntry }, "Using OpenCode plugin source directly (dev mode)");
27
+ return pathToFileURL(devPluginEntry).href;
28
+ }
29
+ for (const dir of prodPluginDirs) {
30
+ const tarball = findPluginTarball(dir);
31
+ if (tarball) {
32
+ return toNpmFileSpecifier(tarball);
33
+ }
34
+ }
35
+ throw new Error(`CodeNomad OpenCode plugin package missing in ${prodPluginDirs.join(", ")}`);
36
+ }
37
+ export function buildOpencodeConfigContent(existingContent, pluginUrl) {
38
+ const config = existingContent?.trim() ? parseJsoncObject(existingContent) : {};
39
+ const existingPlugins = normalizePluginEntries(config.plugin);
40
+ if (!existingPlugins.includes(pluginUrl)) {
41
+ existingPlugins.push(pluginUrl);
42
+ }
43
+ return JSON.stringify({
44
+ "$schema": typeof config["$schema"] === "string" ? config["$schema"] : "https://opencode.ai/config.json",
45
+ ...config,
46
+ plugin: existingPlugins,
47
+ }, null, 2);
48
+ }
49
+ export function resolveExistingOpencodeConfigContent(userEnvironment) {
50
+ const userValue = normalizeConfigContentValue(userEnvironment.OPENCODE_CONFIG_CONTENT);
51
+ if (userValue) {
52
+ return userValue;
53
+ }
54
+ return normalizeConfigContentValue(process.env.OPENCODE_CONFIG_CONTENT);
55
+ }
56
+ function toNpmFileSpecifier(filePath) {
57
+ return `${pluginPackageName}@file:${filePath.replace(/\\/g, "/")}`;
58
+ }
59
+ function findPluginTarball(dir) {
60
+ if (!existsSync(dir)) {
61
+ return null;
62
+ }
63
+ const tarballs = readdirSync(dir)
64
+ .filter((name) => name.endsWith(".tgz"))
65
+ .sort();
66
+ return tarballs.length > 0 ? path.resolve(dir, tarballs[tarballs.length - 1]) : null;
67
+ }
68
+ function normalizeConfigContentValue(value) {
69
+ return typeof value === "string" && value.trim().length > 0 ? value : undefined;
70
+ }
71
+ function parseJsoncObject(content) {
72
+ try {
73
+ const parsed = JSON.parse(stripJsonc(content));
74
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
75
+ throw new Error("OPENCODE_CONFIG_CONTENT must be a JSON object");
76
+ }
77
+ return parsed;
78
+ }
79
+ catch (error) {
80
+ const reason = error instanceof Error ? error.message : String(error);
81
+ throw new Error(`Failed to parse OPENCODE_CONFIG_CONTENT: ${reason}`);
82
+ }
83
+ }
84
+ function normalizePluginEntries(value) {
85
+ if (value === undefined) {
86
+ return [];
87
+ }
88
+ if (typeof value === "string") {
89
+ return [value];
90
+ }
91
+ if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
92
+ return [...value];
93
+ }
94
+ throw new Error("OPENCODE_CONFIG_CONTENT plugin field must be a string or string array");
95
+ }
96
+ function stripJsonc(input) {
97
+ let output = "";
98
+ let inString = false;
99
+ let escape = false;
100
+ for (let index = 0; index < input.length; index += 1) {
101
+ const char = input[index];
102
+ const next = input[index + 1];
103
+ if (escape) {
104
+ output += char;
105
+ escape = false;
106
+ continue;
107
+ }
108
+ if (char === "\\" && inString) {
109
+ output += char;
110
+ escape = true;
111
+ continue;
112
+ }
113
+ if (char === '"') {
114
+ output += char;
115
+ inString = !inString;
116
+ continue;
117
+ }
118
+ if (!inString && char === "/" && next === "/") {
119
+ while (index < input.length && input[index] !== "\n") {
120
+ index += 1;
121
+ }
122
+ output += "\n";
123
+ continue;
124
+ }
125
+ if (!inString && char === "/" && next === "*") {
126
+ index += 2;
127
+ while (index < input.length && !(input[index] === "*" && input[index + 1] === "/")) {
128
+ output += input[index] === "\n" ? "\n" : "";
129
+ index += 1;
130
+ }
131
+ index += 1;
132
+ continue;
133
+ }
134
+ if (!inString && char === ",") {
135
+ let lookahead = index + 1;
136
+ while (lookahead < input.length && /\s/.test(input[lookahead])) {
137
+ lookahead += 1;
138
+ }
139
+ if (input[lookahead] === "}" || input[lookahead] === "]") {
140
+ continue;
141
+ }
142
+ }
143
+ output += char;
144
+ }
145
+ return output;
146
+ }
@@ -0,0 +1,28 @@
1
+ import assert from "node:assert/strict";
2
+ import { describe, it } from "node:test";
3
+ import { buildOpencodeConfigContent } from "./opencode-plugin";
4
+ describe("buildOpencodeConfigContent", () => {
5
+ it("creates config content with the CodeNomad plugin", () => {
6
+ const content = buildOpencodeConfigContent(undefined, "file:///plugin.tgz");
7
+ assert.deepEqual(JSON.parse(content), {
8
+ "$schema": "https://opencode.ai/config.json",
9
+ plugin: ["file:///plugin.tgz"],
10
+ });
11
+ });
12
+ it("merges with existing JSONC content", () => {
13
+ const content = buildOpencodeConfigContent(`{
14
+ // user plugin
15
+ "plugin": ["npm:user-plugin",],
16
+ "model": "test-model",
17
+ }`, "file:///plugin.tgz");
18
+ assert.deepEqual(JSON.parse(content), {
19
+ "$schema": "https://opencode.ai/config.json",
20
+ plugin: ["npm:user-plugin", "file:///plugin.tgz"],
21
+ model: "test-model",
22
+ });
23
+ });
24
+ it("does not duplicate the CodeNomad plugin", () => {
25
+ const content = buildOpencodeConfigContent('{"plugin":["file:///plugin.tgz"]}', "file:///plugin.tgz");
26
+ assert.deepEqual(JSON.parse(content).plugin, ["file:///plugin.tgz"]);
27
+ });
28
+ });
@@ -0,0 +1,63 @@
1
+ import { randomUUID } from "crypto";
2
+ export class PreviewManager {
3
+ constructor() {
4
+ this.previews = new Map();
5
+ }
6
+ create(sessionId, rawUrl) {
7
+ const target = this.normalizeTargetUrl(rawUrl);
8
+ const token = randomUUID();
9
+ const record = {
10
+ token,
11
+ sessionId,
12
+ target,
13
+ createdAt: new Date().toISOString(),
14
+ };
15
+ this.previews.set(token, record);
16
+ return this.toPreviewSession(record);
17
+ }
18
+ get(token) {
19
+ const record = this.previews.get(token);
20
+ return record ? this.toPreviewSession(record) : undefined;
21
+ }
22
+ delete(token) {
23
+ return this.previews.delete(token);
24
+ }
25
+ buildTargetUrl(token, incomingPath, search = "") {
26
+ const record = this.previews.get(token);
27
+ if (!record)
28
+ return undefined;
29
+ const publicBase = this.buildProxyBasePath(token);
30
+ let targetPath = incomingPath.startsWith(publicBase) ? incomingPath.slice(publicBase.length) : incomingPath;
31
+ if (!targetPath || targetPath === "/") {
32
+ targetPath = record.target.pathname || "/";
33
+ }
34
+ else if (!targetPath.startsWith("/")) {
35
+ targetPath = `/${targetPath}`;
36
+ }
37
+ return new URL(`${targetPath}${search}`, record.target.origin);
38
+ }
39
+ buildProxyBasePath(token) {
40
+ return `/previews/${encodeURIComponent(token)}`;
41
+ }
42
+ normalizeTargetUrl(rawUrl) {
43
+ const trimmed = rawUrl.trim();
44
+ const withProtocol = /^[a-z][a-z0-9+.-]*:/i.test(trimmed) ? trimmed : `https://${trimmed}`;
45
+ const target = new URL(withProtocol);
46
+ if (target.protocol !== "http:" && target.protocol !== "https:") {
47
+ throw new Error("Preview URL must use HTTP or HTTPS");
48
+ }
49
+ if (target.username || target.password) {
50
+ throw new Error("Preview URL cannot include credentials");
51
+ }
52
+ return target;
53
+ }
54
+ toPreviewSession(record) {
55
+ return {
56
+ token: record.token,
57
+ sessionId: record.sessionId,
58
+ targetUrl: record.target.toString(),
59
+ proxyUrl: `${this.buildProxyBasePath(record.token)}${record.target.pathname}${record.target.search}${record.target.hash}`,
60
+ createdAt: record.createdAt,
61
+ };
62
+ }
63
+ }