Compare commits

..
Author SHA1 Message Date
Kit Langton 55f2a9b3f0 refactor: switch Skill to direct self-reexport imports 2026-04-16 11:42:18 -04:00
17 changed files with 13 additions and 222 deletions
-1
View File
@@ -450,7 +450,6 @@
"@typescript/native-preview": "catalog:",
"drizzle-kit": "catalog:",
"drizzle-orm": "catalog:",
"rollup": "4.60.1",
"typescript": "catalog:",
"vscode-languageserver-types": "3.17.5",
"why-is-node-running": "3.2.2",
-1
View File
@@ -1,5 +1,4 @@
research
.rollup-tmp
dist
dist-*
gen
-1
View File
@@ -69,7 +69,6 @@
"@typescript/native-preview": "catalog:",
"drizzle-kit": "catalog:",
"drizzle-orm": "catalog:",
"rollup": "4.60.1",
"typescript": "catalog:",
"vscode-languageserver-types": "3.17.5",
"why-is-node-running": "3.2.2",
+1 -21
View File
@@ -5,7 +5,6 @@ import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"
import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
import { treeshakePrepass } from "./treeshake-prepass"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@@ -51,23 +50,9 @@ console.log(`Loaded ${migrations.length} migrations`)
const singleFlag = process.argv.includes("--single")
const baselineFlag = process.argv.includes("--baseline")
const skipInstall = process.argv.includes("--skip-install")
const skipTreeshake = process.argv.includes("--skip-treeshake")
const plugin = createSolidTransformPlugin()
const skipEmbedWebUi = process.argv.includes("--skip-embed-web-ui")
// Run Rollup tree-shaking pre-pass on the main entrypoint.
// Bun/esbuild can't tree-shake `export * as X` barrels (evanw/esbuild#1420).
// Rollup can — it does AST-level analysis to drop unused exports and their
// transitive imports. Workers are excluded since they're separate bundles.
const rollupTmpDir = path.join(dir, ".rollup-tmp")
let treeshakenEntry: string | undefined
if (!skipTreeshake) {
const entryMap = await treeshakePrepass(["./src/index.ts"], rollupTmpDir)
treeshakenEntry = entryMap.get("index")
} else {
console.log("[treeshake] Skipped (--skip-treeshake)")
}
const createEmbeddedWebUIBundle = async () => {
console.log(`Building Web UI to embed in the binary`)
const appDir = path.join(import.meta.dirname, "../../app")
@@ -228,7 +213,7 @@ for (const item of targets) {
},
files: embeddedFileMap ? { "opencode-web-ui.gen.ts": embeddedFileMap } : {},
entrypoints: [
treeshakenEntry ?? "./src/index.ts",
"./src/index.ts",
parserWorker,
workerPath,
rgPath,
@@ -285,9 +270,4 @@ if (Script.release) {
await $`gh release upload v${Script.version} ./dist/*.zip ./dist/*.tar.gz --clobber --repo ${process.env.GH_REPO}`
}
// Clean up Rollup temp directory
if (fs.existsSync(rollupTmpDir)) {
fs.rmSync(rollupTmpDir, { recursive: true })
}
export { binaries }
@@ -1,187 +0,0 @@
#!/usr/bin/env bun
/**
* Rollup tree-shaking pre-pass for the opencode build.
*
* Bun's bundler cannot tree-shake `export * as X from "./mod"` barrels
* (nor can esbuild — see evanw/esbuild#1420). Rollup can.
*
* This script runs Rollup on the source entrypoints to eliminate unused
* exports and their transitive imports, then writes the tree-shaken ESM
* to .rollup-tmp/ for Bun to compile into the final binary.
*
* Usage:
* bun script/treeshake-prepass.ts [entrypoints...]
*
* If no entrypoints are given, defaults to ./src/index.ts.
* Output goes to .rollup-tmp/ preserving the entry filename.
*/
import { rollup, type Plugin as RollupPlugin } from "rollup"
import path from "path"
import fs from "fs"
const dir = path.resolve(import.meta.dirname, "..")
const srcDir = path.join(dir, "src")
// Path alias mappings from tsconfig.json
const aliases: Record<string, string> = {
"@/": path.join(srcDir, "/"),
"@tui/": path.join(srcDir, "cli/cmd/tui/"),
}
// Conditional imports from package.json "#imports"
const hashImports: Record<string, string> = {
"#db": path.join(srcDir, "storage/db.bun.ts"),
"#pty": path.join(srcDir, "pty/pty.bun.ts"),
"#hono": path.join(srcDir, "server/adapter.bun.ts"),
}
function resolveWithAliases(source: string, importerDir: string): string | null {
// Handle hash imports
if (hashImports[source]) return hashImports[source]
// Handle path aliases
for (const [alias, target] of Object.entries(aliases)) {
if (source.startsWith(alias)) {
return target + source.slice(alias.length)
}
}
// Handle relative imports
if (source.startsWith(".")) {
return path.resolve(importerDir, source)
}
return null
}
// Binary/asset extensions that Bun imports natively but Rollup can't parse
const assetExtensions = new Set([".wav", ".wasm", ".node", ".png", ".jpg", ".gif", ".svg", ".css"])
function tryResolveFile(base: string): string | null {
// Try exact file, then .ts, then .tsx, then /index.ts, then /index.tsx
for (const suffix of ["", ".ts", ".tsx", "/index.ts", "/index.tsx"]) {
const p = base + suffix
if (fs.existsSync(p) && fs.statSync(p).isFile()) return p
}
// Bun.Transpiler rewrites .ts → .js in import paths, so try .ts for .js
if (base.endsWith(".js")) {
const tsBase = base.slice(0, -3)
for (const suffix of [".ts", ".tsx"]) {
const p = tsBase + suffix
if (fs.existsSync(p) && fs.statSync(p).isFile()) return p
}
}
return null
}
/**
* Rollup plugin that resolves TypeScript paths and transpiles TS/TSX.
* Uses Bun.Transpiler for speed — no separate TS compilation step.
*/
const bunTranspilePlugin: RollupPlugin = {
name: "bun-transpile",
resolveId(source, importer) {
if (!importer) return null
const importerDir = path.dirname(importer)
const resolved = resolveWithAliases(source, importerDir)
if (!resolved) return null // external (node_modules, node builtins)
const file = tryResolveFile(resolved)
if (file) return file
// If it's a local import we can't resolve (generated file, missing, etc.),
// mark it external so Bun handles it later
return { id: source, external: true }
},
load(id) {
if (id.endsWith(".ts") || id.endsWith(".tsx")) {
return fs.readFileSync(id, "utf-8")
}
// Handle non-JS assets that Bun imports natively
if (id.endsWith(".txt")) {
const content = fs.readFileSync(id, "utf-8")
return `export default ${JSON.stringify(content)};`
}
if (id.endsWith(".json")) {
const content = fs.readFileSync(id, "utf-8")
return `export default ${content};`
}
if (id.endsWith(".sql")) {
const content = fs.readFileSync(id, "utf-8")
return `export default ${JSON.stringify(content)};`
}
// Binary assets — return a placeholder (Bun handles the real import)
const ext = path.extname(id)
if (assetExtensions.has(ext)) {
return `export default "asset:${path.basename(id)}";`
}
return null
},
transform(code, id) {
if (!id.endsWith(".ts") && !id.endsWith(".tsx")) return null
const loader = id.endsWith(".tsx") ? "tsx" : "ts"
const t = new Bun.Transpiler({ loader, tsconfig: JSON.stringify({ compilerOptions: { jsx: "preserve" } }) })
return { code: t.transformSync(code), map: null }
},
}
export async function treeshakePrepass(entrypoints: string[], outDir: string) {
const absEntries = entrypoints.map((e) => path.resolve(dir, e))
const startTime = performance.now()
console.log(`[treeshake] Running Rollup pre-pass on ${absEntries.length} entrypoint(s)...`)
const bundle = await rollup({
input: absEntries,
plugins: [bunTranspilePlugin],
treeshake: {
moduleSideEffects: false, // equivalent to sideEffects: false
},
// Mark everything that isn't local source as external.
// Bun handles node_modules resolution + bundling in the compile step.
external: (id) => {
if (id.startsWith(".") || id.startsWith("/") || id.startsWith("@/") || id.startsWith("@tui/") || id.startsWith("#"))
return false
return true
},
logLevel: "warn",
})
fs.mkdirSync(outDir, { recursive: true })
const { output } = await bundle.write({
dir: outDir,
format: "esm",
preserveModules: false,
entryFileNames: "[name].js",
})
await bundle.close()
const elapsed = (performance.now() - startTime).toFixed(0)
const totalSize = output.reduce((sum, chunk) => sum + ("code" in chunk ? chunk.code.length : 0), 0)
console.log(`[treeshake] Done in ${elapsed}ms — ${output.length} chunks, ${(totalSize / 1024).toFixed(0)}KB total`)
// Return a mapping of original entry basenames to output paths
const entryMap = new Map<string, string>()
for (const chunk of output) {
if (chunk.type === "chunk" && chunk.isEntry) {
entryMap.set(chunk.name, path.join(outDir, chunk.fileName))
}
}
return entryMap
}
// CLI mode: run directly
if (import.meta.main) {
const args = process.argv.slice(2)
const entries = args.length > 0 ? args : ["./src/index.ts"]
const outDir = path.join(dir, ".rollup-tmp")
const result = await treeshakePrepass(entries, outDir)
for (const [name, out] of result) {
console.log(` ${name}${path.relative(dir, out)}`)
}
}
+1 -1
View File
@@ -18,7 +18,7 @@ import { mergeDeep, pipe, sortBy, values } from "remeda"
import { Global } from "@/global"
import path from "path"
import { Plugin } from "@/plugin"
import { Skill } from "../skill"
import { Skill } from "../skill/skill"
import { Effect, Context, Layer } from "effect"
import { InstanceState } from "@/effect"
import * as Option from "effect/Option"
+1 -1
View File
@@ -7,7 +7,7 @@ import { Effect, Layer, Context } from "effect"
import z from "zod"
import { Config } from "../config"
import { MCP } from "../mcp"
import { Skill } from "../skill"
import { Skill } from "../skill/skill"
import PROMPT_INITIALIZE from "./template/initialize.txt"
import PROMPT_REVIEW from "./template/review.txt"
+1 -1
View File
@@ -18,7 +18,7 @@ import { Plugin } from "@/plugin"
import { Provider } from "@/provider"
import { ProviderAuth } from "@/provider"
import { Agent } from "@/agent/agent"
import { Skill } from "@/skill"
import { Skill } from "@/skill/skill"
import { Discovery } from "@/skill/discovery"
import { Question } from "@/question"
import { Permission } from "@/permission"
@@ -8,7 +8,7 @@ import { TuiRoutes } from "./tui"
import { Instance } from "../../project/instance"
import { Vcs } from "../../project"
import { Agent } from "../../agent/agent"
import { Skill } from "../../skill"
import { Skill } from "../../skill/skill"
import { Global } from "../../global"
import { LSP } from "../../lsp"
import { Command } from "../../command"
+1 -1
View File
@@ -14,7 +14,7 @@ import PROMPT_TRINITY from "./prompt/trinity.txt"
import type { Provider } from "@/provider"
import type { Agent } from "@/agent/agent"
import { Permission } from "@/permission"
import { Skill } from "@/skill"
import { Skill } from "@/skill/skill"
export namespace SystemPrompt {
export function provider(model: Provider.Model) {
-1
View File
@@ -1 +0,0 @@
export * as Skill from "./skill"
+2
View File
@@ -260,3 +260,5 @@ export function fmt(list: Info[], opts: { verbose: boolean }) {
.map((skill) => `- **${skill.name}**: ${skill.description}`),
].join("\n")
}
export * as Skill from "./skill"
+1 -1
View File
@@ -44,7 +44,7 @@ import { Instruction } from "../session/instruction"
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
import { Bus } from "../bus"
import { Agent } from "../agent/agent"
import { Skill } from "../skill"
import { Skill } from "../skill/skill"
import { Permission } from "@/permission"
const log = Log.create({ service: "tool.registry" })
+1 -1
View File
@@ -5,7 +5,7 @@ import { Effect } from "effect"
import * as Stream from "effect/Stream"
import { EffectLogger } from "@/effect"
import { Ripgrep } from "../file/ripgrep"
import { Skill } from "../skill"
import { Skill } from "../skill/skill"
import * as Tool from "./tool"
const Parameters = z.object({
@@ -30,7 +30,7 @@ import { SessionRevert } from "../../src/session/revert"
import { SessionRunState } from "../../src/session/run-state"
import { MessageID, PartID, SessionID } from "../../src/session/schema"
import { SessionStatus } from "../../src/session/status"
import { Skill } from "../../src/skill"
import { Skill } from "../../src/skill/skill"
import { SystemPrompt } from "../../src/session/system"
import { Shell } from "../../src/shell/shell"
import { Snapshot } from "../../src/snapshot"
@@ -41,7 +41,7 @@ import { Plugin } from "../../src/plugin"
import { Provider as ProviderSvc } from "../../src/provider"
import { Env } from "../../src/env"
import { Question } from "../../src/question"
import { Skill } from "../../src/skill"
import { Skill } from "../../src/skill/skill"
import { SystemPrompt } from "../../src/session/system"
import { Todo } from "../../src/session/todo"
import { SessionCompaction } from "../../src/session/compaction"
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect } from "bun:test"
import { Effect, Layer } from "effect"
import { Skill } from "../../src/skill"
import { Skill } from "../../src/skill/skill"
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
import { provideInstance, provideTmpdirInstance, tmpdir } from "../fixture/fixture"
import { testEffect } from "../lib/effect"