-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix monorepo package entrypoints #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "caplets": patch | ||
| "@caplets/core": patch | ||
| "@caplets/opencode": patch | ||
| --- | ||
|
|
||
| Fix monorepo package entrypoints so the CLI resolves MCP SDK subpaths on Node ESM, reports the CLI package version, and the OpenCode plugin exposes only its default plugin export. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { resolve } from "node:path"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| resolve: { | ||
| alias: { | ||
| "@caplets/core": resolve(import.meta.dirname, "../core/src/index.ts"), | ||
| }, | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import { execFile } from "node:child_process"; | ||||||||||||||||||||||||||||||||||||||||||
| import { dirname, resolve } from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||
| import { fileURLToPath } from "node:url"; | ||||||||||||||||||||||||||||||||||||||||||
| import { promisify } from "node:util"; | ||||||||||||||||||||||||||||||||||||||||||
| import { describe, expect, it } from "vitest"; | ||||||||||||||||||||||||||||||||||||||||||
| import { version as packageVersion } from "../package.json"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const execFileAsync = promisify(execFile); | ||||||||||||||||||||||||||||||||||||||||||
| const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); | ||||||||||||||||||||||||||||||||||||||||||
| const repoRoot = resolve(packageRoot, "../.."); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| describe("caplets package entrypoint", () => { | ||||||||||||||||||||||||||||||||||||||||||
| it("can start far enough to print its version from the built bin", async () => { | ||||||||||||||||||||||||||||||||||||||||||
| await execFileAsync("pnpm", ["--filter", "@caplets/core", "build"], { cwd: repoRoot }); | ||||||||||||||||||||||||||||||||||||||||||
| await execFileAsync("pnpm", ["build"], { cwd: packageRoot }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const { stdout } = await execFileAsync(process.execPath, ["dist/index.js", "--version"], { | ||||||||||||||||||||||||||||||||||||||||||
| cwd: packageRoot, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| expect(stdout.trim()).toBe(packageVersion); | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+13
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test runs two sequential
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { readdirSync, readFileSync } from "node:fs"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { relative, resolve, sep } from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { describe, expect, it } from "vitest"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import corePackage from "../package.json"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const repoRoot = resolve(import.meta.dirname, "../../.."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const packagesRoot = resolve(repoRoot, "packages"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const scannedExtensions = new Set([".ts", ".mjs"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ignoredDirectories = new Set(["dist", "dist-schema", "node_modules"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe("package boundaries", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("uses Node ESM-safe MCP SDK subpath imports", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const violations = scanFiles([packagesRoot]).flatMap((filePath) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const source = readFileSync(filePath, "utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Array.from(source.matchAll(/["'](@modelcontextprotocol\/sdk\/[^"']+)["']/g)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((match) => match[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((specifier): specifier is string => Boolean(specifier)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((specifier) => !specifier.endsWith(".js")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((specifier) => `${formatPath(filePath)} imports ${specifier}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(violations).toEqual([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it("imports workspace packages through declared package exports", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const exportedCoreSpecifiers = new Set( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.keys(corePackage.exports).map((specifier) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| specifier === "." ? "@caplets/core" : `@caplets/core/${specifier.slice(2)}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const violations = scanFiles([packagesRoot]).flatMap((filePath) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const packageName = packageNameForFile(filePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const source = readFileSync(filePath, "utf8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const specifiers = importSpecifiers(source); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const relativeCrossPackageImports = specifiers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((specifier) => /^\.\.\/(core|cli|opencode|pi|benchmarks)(?:\/|$)/.test(specifier)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((specifier) => `${formatPath(filePath)} imports ${specifier}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relative cross-package import detection misses nested traversals. Line 37 only matches a single Proposed fix- .filter((specifier) => /^\.\.\/(core|cli|opencode|pi|benchmarks)(?:\/|$)/.test(specifier))
+ .filter((specifier) =>
+ /^(\.\.\/)+(core|cli|opencode|pi|benchmarks)(?:\/|$)/.test(specifier),
+ )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Comment on lines
+37
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The regex
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const undeclaredCoreExports = specifiers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((specifier) => specifier.startsWith("@caplets/core")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((specifier) => packageName !== "core" && !exportedCoreSpecifiers.has(specifier)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((specifier) => `${formatPath(filePath)} imports undeclared export ${specifier}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return [...relativeCrossPackageImports, ...undeclaredCoreExports]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(violations).toEqual([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function scanFiles(roots: string[]): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const files: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const root of roots) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| collectFiles(root, files); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return files; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function collectFiles(directory: string, files: string[]): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const entry of readdirSync(directory, { withFileTypes: true })) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = resolve(directory, entry.name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (entry.isDirectory()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!ignoredDirectories.has(entry.name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| collectFiles(path, files); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ([...scannedExtensions].some((extension) => entry.name.endsWith(extension))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files.push(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function importSpecifiers(source: string): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const specifiers: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const match of source.matchAll(/\bfrom\s+["']([^"']+)["']/g)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (match[1]) specifiers.push(match[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const match of source.matchAll(/\bimport\(\s*["']([^"']+)["']\s*\)/g)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (match[1]) specifiers.push(match[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const match of source.matchAll(/\bvi\.mock\(\s*["']([^"']+)["']/g)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (match[1]) specifiers.push(match[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+74
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Current parsing ignores side-effect imports, so boundary violations in that form are not scanned. Proposed fix function importSpecifiers(source: string): string[] {
const specifiers: string[] = [];
+ for (const match of source.matchAll(/\bimport\s+["']([^"']+)["']/g)) {
+ if (match[1]) specifiers.push(match[1]);
+ }
for (const match of source.matchAll(/\bfrom\s+["']([^"']+)["']/g)) {
if (match[1]) specifiers.push(match[1]);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return specifiers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function packageNameForFile(filePath: string): string | undefined { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parts = relative(packagesRoot, filePath).split(sep); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return parts[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function formatPath(filePath: string): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return relative(repoRoot, filePath).split(sep).join("/"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { tool, type Hooks } from "@opencode-ai/plugin"; | ||
| import { nativeCapletsSystemGuidance, type NativeCapletsService } from "@caplets/core/native"; | ||
| import { capletsOpenCodeArgs } from "./schema.js"; | ||
|
|
||
| export async function createCapletsOpenCodeHooks(service: NativeCapletsService): Promise<Hooks> { | ||
| const capletTools = service.listTools(); | ||
| const registeredToolNames = new Set(capletTools.map((caplet) => caplet.toolName)); | ||
|
|
||
| return { | ||
| tool: Object.fromEntries( | ||
| capletTools.map((caplet) => [ | ||
| caplet.toolName, | ||
| tool({ | ||
| description: caplet.description, | ||
| args: capletsOpenCodeArgs(), | ||
| async execute(args) { | ||
| const result = await service.execute(caplet.caplet, args); | ||
| if (typeof result === "string") return result; | ||
| try { | ||
| return JSON.stringify(result, null, 2) ?? "null"; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| return `[Serialization error: ${message}]`; | ||
| } | ||
| }, | ||
| }), | ||
| ]), | ||
| ), | ||
| "experimental.chat.system.transform": async (_input, output) => { | ||
| output.system.push( | ||
| nativeCapletsSystemGuidance( | ||
| service | ||
| .listTools() | ||
| .map((caplet) => caplet.toolName) | ||
| .filter((toolName) => registeredToolNames.has(toolName)), | ||
| ), | ||
| ); | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,14 @@ | ||
| import { tool, type Hooks, type Plugin, type PluginInput } from "@opencode-ai/plugin"; | ||
| import { type Plugin, type PluginInput } from "@opencode-ai/plugin"; | ||
| import { | ||
| createNativeCapletsService, | ||
| nativeCapletsSystemGuidance, | ||
| registerNativeCapletsProcessCleanup, | ||
| type NativeCapletsService, | ||
| } from "@caplets/core/native"; | ||
| import { capletsOpenCodeArgs } from "./schema.js"; | ||
| import { createCapletsOpenCodeHooks } from "./hooks.js"; | ||
|
|
||
| const plugin: Plugin = async (_ctx: PluginInput) => { | ||
| const service = createNativeCapletsService(); | ||
| registerNativeCapletsProcessCleanup(service); | ||
| return createCapletsOpenCodeHooks(service); | ||
| }; | ||
|
|
||
| export async function createCapletsOpenCodeHooks(service: NativeCapletsService): Promise<Hooks> { | ||
| const capletTools = service.listTools(); | ||
| const registeredToolNames = new Set(capletTools.map((caplet) => caplet.toolName)); | ||
|
|
||
| return { | ||
| tool: Object.fromEntries( | ||
| capletTools.map((caplet) => [ | ||
| caplet.toolName, | ||
| tool({ | ||
| description: caplet.description, | ||
| args: capletsOpenCodeArgs(), | ||
| async execute(args) { | ||
| const result = await service.execute(caplet.caplet, args); | ||
| if (typeof result === "string") return result; | ||
| try { | ||
| return JSON.stringify(result, null, 2) ?? "null"; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| return `[Serialization error: ${message}]`; | ||
| } | ||
| }, | ||
| }), | ||
| ]), | ||
| ), | ||
| "experimental.chat.system.transform": async (_input, output) => { | ||
| output.system.push( | ||
| nativeCapletsSystemGuidance( | ||
| service | ||
| .listTools() | ||
| .map((caplet) => caplet.toolName) | ||
| .filter((toolName) => registeredToolNames.has(toolName)), | ||
| ), | ||
| ); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export default plugin; |
Uh oh!
There was an error while loading. Please reload this page.