fix(codex): support packaged lazycodex installs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -46,6 +47,7 @@ export async function installMarketplaceLocally(options = {}) {
|
||||
const platform = options.platform ?? process.platform;
|
||||
const runCommand = options.runCommand ?? defaultRunCommand;
|
||||
const log = options.log ?? console.log;
|
||||
const buildSource = await shouldBuildSourcePackages(repoRoot);
|
||||
const gitBashResolution = await prepareGitBashForInstall({
|
||||
platform,
|
||||
env,
|
||||
@@ -79,6 +81,7 @@ export async function installMarketplaceLocally(options = {}) {
|
||||
|
||||
log(`Building ${entry.name}@${version}`);
|
||||
const plugin = await installCachedPlugin({
|
||||
buildSource,
|
||||
codexHome,
|
||||
marketplaceName: marketplace.name,
|
||||
name: entry.name,
|
||||
@@ -191,6 +194,14 @@ function legacyCacheMarketplaces(marketplaceName) {
|
||||
return marketplaceName === "sisyphuslabs" ? SISYPHUS_LEGACY_CACHE_MARKETPLACES : [];
|
||||
}
|
||||
|
||||
async function shouldBuildSourcePackages(repoRoot) {
|
||||
if (existsSync(join(repoRoot, "src", "index.ts"))) return true;
|
||||
const packageJsonPath = join(repoRoot, "package.json");
|
||||
if (!existsSync(packageJsonPath)) return true;
|
||||
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
|
||||
return !["@code-yeongyu/lazycodex", "lazycodex", "oh-my-opencode", "oh-my-openagent"].includes(packageJson?.name);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const repoRoot = process.argv[2] ? resolve(process.argv[2]) : process.cwd();
|
||||
const result = await installMarketplaceLocally({ repoRoot });
|
||||
|
||||
@@ -188,3 +188,46 @@ test("#given structurally valid external MCP package without mcp suffix #when in
|
||||
assert.deepEqual(cachedMcp.mcpServers.language_tools.args, [copiedCli, "mcp", join(sourceRoot, "..", "local-config.json")]);
|
||||
assert.equal((await stat(copiedCli)).isFile(), true);
|
||||
});
|
||||
|
||||
test("#given packaged external MCP runtime has only dist files #when installing cached plugin #then runtime is copied into the plugin cache", async () => {
|
||||
// given
|
||||
const repoRoot = await makeTempDir();
|
||||
const codexHome = await makeTempDir();
|
||||
const sourceRoot = join(repoRoot, "packages", "omo-codex", "plugin");
|
||||
const lspPackageRoot = join(repoRoot, "packages", "lsp-tools-mcp");
|
||||
|
||||
await writeJson(join(sourceRoot, "package.json"), {
|
||||
name: "@example/omo",
|
||||
version: "0.1.0",
|
||||
});
|
||||
await writeJson(join(sourceRoot, ".mcp.json"), {
|
||||
mcpServers: {
|
||||
lsp: {
|
||||
command: "node",
|
||||
args: ["../../lsp-tools-mcp/dist/cli.js", "mcp"],
|
||||
cwd: ".",
|
||||
},
|
||||
},
|
||||
});
|
||||
await writeJson(join(lspPackageRoot, "dist", "cli.js"), { executable: true });
|
||||
await writeJson(join(lspPackageRoot, "dist", "lsp", "manager.js"), { copied: true });
|
||||
|
||||
// when
|
||||
const result = await installCachedPlugin({
|
||||
codexHome,
|
||||
marketplaceName: "sisyphuslabs",
|
||||
name: "omo",
|
||||
runCommand: async () => {},
|
||||
sourcePath: sourceRoot,
|
||||
version: "0.1.0",
|
||||
});
|
||||
|
||||
// then
|
||||
const cachedMcp = JSON.parse(await readFile(join(result.path, ".mcp.json"), "utf8"));
|
||||
const copiedCli = join(result.path, "mcp", "lsp", "dist", "cli.js");
|
||||
assert.deepEqual(cachedMcp.mcpServers.lsp.args, [copiedCli, "mcp"]);
|
||||
assert.equal(Object.hasOwn(cachedMcp.mcpServers.lsp, "cwd"), false);
|
||||
assert.equal((await stat(copiedCli)).isFile(), true);
|
||||
assert.equal((await stat(join(result.path, "mcp", "lsp", "dist", "lsp", "manager.js"))).isFile(), true);
|
||||
assert.notEqual(cachedMcp.mcpServers.lsp.args[0], join(lspPackageRoot, "dist", "cli.js"));
|
||||
});
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
import { installMarketplaceLocally } from "./install-local.mjs";
|
||||
import { makeTempDir, writeJson, writePluginAt } from "./install-test-fixtures.mjs";
|
||||
|
||||
test("#given packaged lazycodex adapter #when installing locally #then uses bundled artifacts without source builds", async () => {
|
||||
// given
|
||||
const repoRoot = await makeTempDir();
|
||||
const codexHome = await makeTempDir();
|
||||
const binDir = await makeTempDir();
|
||||
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
|
||||
const pluginRoot = join(codexPackageRoot, "plugin");
|
||||
const lspRuntimeRoot = join(repoRoot, "packages", "lsp-tools-mcp");
|
||||
|
||||
await writeJson(join(repoRoot, "package.json"), {
|
||||
name: "@code-yeongyu/lazycodex",
|
||||
version: "0.1.2",
|
||||
});
|
||||
await writeJson(join(codexPackageRoot, "marketplace.json"), {
|
||||
name: "sisyphuslabs",
|
||||
plugins: [{ name: "omo", source: "./plugins/omo" }],
|
||||
});
|
||||
await writePluginAt(pluginRoot, "omo", "0.1.0");
|
||||
await writeJson(join(pluginRoot, ".mcp.json"), {
|
||||
mcpServers: {
|
||||
lsp: {
|
||||
command: "node",
|
||||
args: ["../../lsp-tools-mcp/dist/cli.js", "mcp"],
|
||||
cwd: ".",
|
||||
},
|
||||
},
|
||||
});
|
||||
await mkdir(join(pluginRoot, "dist"), { recursive: true });
|
||||
await writeFile(join(pluginRoot, "dist", "cli.js"), "#!/usr/bin/env node\nconsole.log('prebuilt')\n");
|
||||
await writeJson(join(lspRuntimeRoot, "dist", "cli.js"), { executable: true });
|
||||
|
||||
const commands = [];
|
||||
|
||||
// when
|
||||
const result = await installMarketplaceLocally({
|
||||
repoRoot,
|
||||
codexHome,
|
||||
binDir,
|
||||
platform: "linux",
|
||||
runCommand: async (command, args, options) => {
|
||||
commands.push([command, args.join(" "), options.cwd]);
|
||||
},
|
||||
log: () => {},
|
||||
});
|
||||
|
||||
// then
|
||||
const pluginCacheRoot = join(codexHome, "plugins", "cache", "sisyphuslabs", "omo", "0.1.0");
|
||||
const cachedMcp = JSON.parse(await readFile(join(pluginCacheRoot, ".mcp.json"), "utf8"));
|
||||
const cachedLspCli = join(pluginCacheRoot, "mcp", "lsp", "dist", "cli.js");
|
||||
|
||||
assert.deepEqual(result.installed.map((plugin) => `${plugin.name}@${plugin.version}`), ["omo@0.1.0"]);
|
||||
assert.deepEqual(
|
||||
commands.map(([command, args, cwd]) => [command, args, cwd]),
|
||||
[["npm", "install --omit=dev", pluginCacheRoot]],
|
||||
);
|
||||
assert.deepEqual(cachedMcp.mcpServers.lsp.args, [cachedLspCli, "mcp"]);
|
||||
assert.equal((await stat(cachedLspCli)).isFile(), true);
|
||||
assert.notEqual(cachedMcp.mcpServers.lsp.args[0], join(lspRuntimeRoot, "dist", "cli.js"));
|
||||
});
|
||||
@@ -6,9 +6,11 @@ import { exists, isRecord } from "./utils.mjs";
|
||||
import { COMMAND_SHIM_MARKER } from "./command-shim.mjs";
|
||||
import { removeLegacyCodexComponentBins } from "./legacy-bins.mjs";
|
||||
|
||||
export async function installCachedPlugin({ codexHome, marketplaceName, name, runCommand, sourcePath, version }) {
|
||||
await maybeRunNpmInstall(sourcePath, runCommand);
|
||||
await maybeRunNpmBuild(sourcePath, runCommand);
|
||||
export async function installCachedPlugin({ buildSource = true, codexHome, marketplaceName, name, runCommand, sourcePath, version }) {
|
||||
if (buildSource) {
|
||||
await maybeRunNpmInstall(sourcePath, runCommand);
|
||||
await maybeRunNpmBuild(sourcePath, runCommand);
|
||||
}
|
||||
|
||||
const targetPath = join(codexHome, "plugins", "cache", marketplaceName, name, version);
|
||||
await replaceDirectory(sourcePath, targetPath, shouldCopyPluginPath);
|
||||
|
||||
@@ -40,7 +40,7 @@ function resolveExternalMcpPackageRoot(runtimePath, sourceRoot) {
|
||||
if (!isPathInside(runtimePath, packagesRoot)) return undefined;
|
||||
let packageRoot = dirname(runtimePath);
|
||||
while (packageRoot !== packagesRoot) {
|
||||
if (existsSync(join(packageRoot, "package.json")) && isPathInside(runtimePath, join(packageRoot, "dist"))) {
|
||||
if (isPathInside(runtimePath, join(packageRoot, "dist")) && isRuntimePackageRoot(packageRoot)) {
|
||||
return packageRoot;
|
||||
}
|
||||
const parent = dirname(packageRoot);
|
||||
@@ -50,6 +50,10 @@ function resolveExternalMcpPackageRoot(runtimePath, sourceRoot) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function isRuntimePackageRoot(packageRoot) {
|
||||
return existsSync(join(packageRoot, "package.json")) || existsSync(join(packageRoot, "dist"));
|
||||
}
|
||||
|
||||
function findPackagesRoot(path) {
|
||||
let current = resolve(path);
|
||||
for (let index = 0; index < 8; index++) {
|
||||
|
||||
Reference in New Issue
Block a user