Compare commits

..
7 Commits
52 changed files with 2706 additions and 706 deletions
+1
View File
@@ -25,6 +25,7 @@ kommander
-opencodeengineer bot that spams issues -opencodeengineer bot that spams issues
r44vc0rp r44vc0rp
rekram1-node rekram1-node
-ricardo-m-l
-robinmordasiewicz -robinmordasiewicz
simonklee simonklee
-spider-yamet clawdbot/llm psychosis, spam pinging the team -spider-yamet clawdbot/llm psychosis, spam pinging the team
@@ -0,0 +1,16 @@
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_workspace` (
`id` text PRIMARY KEY,
`type` text NOT NULL,
`name` text DEFAULT '' NOT NULL,
`branch` text,
`directory` text,
`extra` text,
`project_id` text NOT NULL,
CONSTRAINT `fk_workspace_project_id_project_id_fk` FOREIGN KEY (`project_id`) REFERENCES `project`(`id`) ON DELETE CASCADE
);
--> statement-breakpoint
INSERT INTO `__new_workspace`(`id`, `type`, `branch`, `name`, `directory`, `extra`, `project_id`) SELECT `id`, `type`, `branch`, `name`, `directory`, `extra`, `project_id` FROM `workspace`;--> statement-breakpoint
DROP TABLE `workspace`;--> statement-breakpoint
ALTER TABLE `__new_workspace` RENAME TO `workspace`;--> statement-breakpoint
PRAGMA foreign_keys=ON;
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -25,7 +25,7 @@ const seed = async () => {
directory: dir, directory: dir,
init: () => AppRuntime.runPromise(InstanceBootstrap), init: () => AppRuntime.runPromise(InstanceBootstrap),
fn: async () => { fn: async () => {
await Config.waitForDependencies() await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.waitForDependencies()))
await AppRuntime.runPromise( await AppRuntime.runPromise(
Effect.gen(function* () { Effect.gen(function* () {
const registry = yield* ToolRegistry.Service const registry = yield* ToolRegistry.Service
@@ -1,5 +1,6 @@
import { EOL } from "os" import { EOL } from "os"
import { Config } from "../../../config/config" import { Config } from "../../../config/config"
import { AppRuntime } from "@/effect/app-runtime"
import { bootstrap } from "../../bootstrap" import { bootstrap } from "../../bootstrap"
import { cmd } from "../cmd" import { cmd } from "../cmd"
@@ -9,7 +10,7 @@ export const ConfigCommand = cmd({
builder: (yargs) => yargs, builder: (yargs) => yargs,
async handler() { async handler() {
await bootstrap(process.cwd(), async () => { await bootstrap(process.cwd(), async () => {
const config = await Config.get() const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.get()))
process.stdout.write(JSON.stringify(config, null, 2) + EOL) process.stdout.write(JSON.stringify(config, null, 2) + EOL)
}) })
}, },
+20 -4
View File
@@ -17,7 +17,11 @@ const FileSearchCommand = cmd({
}), }),
async handler(args) { async handler(args) {
await bootstrap(process.cwd(), async () => { await bootstrap(process.cwd(), async () => {
const results = await AppRuntime.runPromise(File.Service.use((svc) => svc.search({ query: args.query }))) const results = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.search({ query: args.query }))
}),
)
process.stdout.write(results.join(EOL) + EOL) process.stdout.write(results.join(EOL) + EOL)
}) })
}, },
@@ -34,7 +38,11 @@ const FileReadCommand = cmd({
}), }),
async handler(args) { async handler(args) {
await bootstrap(process.cwd(), async () => { await bootstrap(process.cwd(), async () => {
const content = await AppRuntime.runPromise(File.Service.use((svc) => svc.read(args.path))) const content = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.read(args.path))
}),
)
process.stdout.write(JSON.stringify(content, null, 2) + EOL) process.stdout.write(JSON.stringify(content, null, 2) + EOL)
}) })
}, },
@@ -46,7 +54,11 @@ const FileStatusCommand = cmd({
builder: (yargs) => yargs, builder: (yargs) => yargs,
async handler() { async handler() {
await bootstrap(process.cwd(), async () => { await bootstrap(process.cwd(), async () => {
const status = await AppRuntime.runPromise(File.Service.use((svc) => svc.status())) const status = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.status())
}),
)
process.stdout.write(JSON.stringify(status, null, 2) + EOL) process.stdout.write(JSON.stringify(status, null, 2) + EOL)
}) })
}, },
@@ -63,7 +75,11 @@ const FileListCommand = cmd({
}), }),
async handler(args) { async handler(args) {
await bootstrap(process.cwd(), async () => { await bootstrap(process.cwd(), async () => {
const files = await AppRuntime.runPromise(File.Service.use((svc) => svc.list(args.path))) const files = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.list(args.path))
}),
)
process.stdout.write(JSON.stringify(files, null, 2) + EOL) process.stdout.write(JSON.stringify(files, null, 2) + EOL)
}) })
}, },
+74 -38
View File
@@ -15,6 +15,8 @@ import { Global } from "../../global"
import { modify, applyEdits } from "jsonc-parser" import { modify, applyEdits } from "jsonc-parser"
import { Filesystem } from "../../util/filesystem" import { Filesystem } from "../../util/filesystem"
import { Bus } from "../../bus" import { Bus } from "../../bus"
import { AppRuntime } from "../../effect/app-runtime"
import { Effect } from "effect"
function getAuthStatusIcon(status: MCP.AuthStatus): string { function getAuthStatusIcon(status: MCP.AuthStatus): string {
switch (status) { switch (status) {
@@ -50,6 +52,47 @@ function isMcpRemote(config: McpEntry): config is McpRemote {
return isMcpConfigured(config) && config.type === "remote" return isMcpConfigured(config) && config.type === "remote"
} }
function configuredServers(config: Config.Info) {
return Object.entries(config.mcp ?? {}).filter((entry): entry is [string, McpConfigured] => isMcpConfigured(entry[1]))
}
function oauthServers(config: Config.Info) {
return configuredServers(config).filter(
(entry): entry is [string, McpRemote] => isMcpRemote(entry[1]) && entry[1].oauth !== false,
)
}
async function listState() {
return AppRuntime.runPromise(
Effect.gen(function* () {
const cfg = yield* Config.Service
const mcp = yield* MCP.Service
const config = yield* cfg.get()
const statuses = yield* mcp.status()
const stored = yield* Effect.all(
Object.fromEntries(configuredServers(config).map(([name]) => [name, mcp.hasStoredTokens(name)])),
{ concurrency: "unbounded" },
)
return { config, statuses, stored }
}),
)
}
async function authState() {
return AppRuntime.runPromise(
Effect.gen(function* () {
const cfg = yield* Config.Service
const mcp = yield* MCP.Service
const config = yield* cfg.get()
const auth = yield* Effect.all(
Object.fromEntries(oauthServers(config).map(([name]) => [name, mcp.getAuthStatus(name)])),
{ concurrency: "unbounded" },
)
return { config, auth }
}),
)
}
export const McpCommand = cmd({ export const McpCommand = cmd({
command: "mcp", command: "mcp",
describe: "manage MCP (Model Context Protocol) servers", describe: "manage MCP (Model Context Protocol) servers",
@@ -75,13 +118,8 @@ export const McpListCommand = cmd({
UI.empty() UI.empty()
prompts.intro("MCP Servers") prompts.intro("MCP Servers")
const config = await Config.get() const { config, statuses, stored } = await listState()
const mcpServers = config.mcp ?? {} const servers = configuredServers(config)
const statuses = await MCP.status()
const servers = Object.entries(mcpServers).filter((entry): entry is [string, McpConfigured] =>
isMcpConfigured(entry[1]),
)
if (servers.length === 0) { if (servers.length === 0) {
prompts.log.warn("No MCP servers configured") prompts.log.warn("No MCP servers configured")
@@ -92,7 +130,7 @@ export const McpListCommand = cmd({
for (const [name, serverConfig] of servers) { for (const [name, serverConfig] of servers) {
const status = statuses[name] const status = statuses[name]
const hasOAuth = isMcpRemote(serverConfig) && !!serverConfig.oauth const hasOAuth = isMcpRemote(serverConfig) && !!serverConfig.oauth
const hasStoredTokens = await MCP.hasStoredTokens(name) const hasStoredTokens = stored[name]
let statusIcon: string let statusIcon: string
let statusText: string let statusText: string
@@ -152,15 +190,11 @@ export const McpAuthCommand = cmd({
UI.empty() UI.empty()
prompts.intro("MCP OAuth Authentication") prompts.intro("MCP OAuth Authentication")
const config = await Config.get() const { config, auth } = await authState()
const mcpServers = config.mcp ?? {} const mcpServers = config.mcp ?? {}
const servers = oauthServers(config)
// Get OAuth-capable servers (remote servers with oauth not explicitly disabled) if (servers.length === 0) {
const oauthServers = Object.entries(mcpServers).filter(
(entry): entry is [string, McpRemote] => isMcpRemote(entry[1]) && entry[1].oauth !== false,
)
if (oauthServers.length === 0) {
prompts.log.warn("No OAuth-capable MCP servers configured") prompts.log.warn("No OAuth-capable MCP servers configured")
prompts.log.info("Remote MCP servers support OAuth by default. Add a remote server in opencode.json:") prompts.log.info("Remote MCP servers support OAuth by default. Add a remote server in opencode.json:")
prompts.log.info(` prompts.log.info(`
@@ -177,9 +211,8 @@ export const McpAuthCommand = cmd({
let serverName = args.name let serverName = args.name
if (!serverName) { if (!serverName) {
// Build options with auth status // Build options with auth status
const options = await Promise.all( const options = servers.map(([name, cfg]) => {
oauthServers.map(async ([name, cfg]) => { const authStatus = auth[name]
const authStatus = await MCP.getAuthStatus(name)
const icon = getAuthStatusIcon(authStatus) const icon = getAuthStatusIcon(authStatus)
const statusText = getAuthStatusText(authStatus) const statusText = getAuthStatusText(authStatus)
const url = cfg.url const url = cfg.url
@@ -188,8 +221,7 @@ export const McpAuthCommand = cmd({
value: name, value: name,
hint: url, hint: url,
} }
}), })
)
const selected = await prompts.select({ const selected = await prompts.select({
message: "Select MCP server to authenticate", message: "Select MCP server to authenticate",
@@ -213,7 +245,8 @@ export const McpAuthCommand = cmd({
} }
// Check if already authenticated // Check if already authenticated
const authStatus = await MCP.getAuthStatus(serverName) const authStatus =
auth[serverName] ?? (await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.getAuthStatus(serverName))))
if (authStatus === "authenticated") { if (authStatus === "authenticated") {
const confirm = await prompts.confirm({ const confirm = await prompts.confirm({
message: `${serverName} already has valid credentials. Re-authenticate?`, message: `${serverName} already has valid credentials. Re-authenticate?`,
@@ -240,7 +273,7 @@ export const McpAuthCommand = cmd({
}) })
try { try {
const status = await MCP.authenticate(serverName) const status = await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.authenticate(serverName)))
if (status.status === "connected") { if (status.status === "connected") {
spinner.stop("Authentication successful!") spinner.stop("Authentication successful!")
@@ -289,22 +322,17 @@ export const McpAuthListCommand = cmd({
UI.empty() UI.empty()
prompts.intro("MCP OAuth Status") prompts.intro("MCP OAuth Status")
const config = await Config.get() const { config, auth } = await authState()
const mcpServers = config.mcp ?? {} const servers = oauthServers(config)
// Get OAuth-capable servers if (servers.length === 0) {
const oauthServers = Object.entries(mcpServers).filter(
(entry): entry is [string, McpRemote] => isMcpRemote(entry[1]) && entry[1].oauth !== false,
)
if (oauthServers.length === 0) {
prompts.log.warn("No OAuth-capable MCP servers configured") prompts.log.warn("No OAuth-capable MCP servers configured")
prompts.outro("Done") prompts.outro("Done")
return return
} }
for (const [name, serverConfig] of oauthServers) { for (const [name, serverConfig] of servers) {
const authStatus = await MCP.getAuthStatus(name) const authStatus = auth[name]
const icon = getAuthStatusIcon(authStatus) const icon = getAuthStatusIcon(authStatus)
const statusText = getAuthStatusText(authStatus) const statusText = getAuthStatusText(authStatus)
const url = serverConfig.url const url = serverConfig.url
@@ -312,7 +340,7 @@ export const McpAuthListCommand = cmd({
prompts.log.info(`${icon} ${name} ${UI.Style.TEXT_DIM}${statusText}\n ${UI.Style.TEXT_DIM}${url}`) prompts.log.info(`${icon} ${name} ${UI.Style.TEXT_DIM}${statusText}\n ${UI.Style.TEXT_DIM}${url}`)
} }
prompts.outro(`${oauthServers.length} OAuth-capable server(s)`) prompts.outro(`${servers.length} OAuth-capable server(s)`)
}, },
}) })
}, },
@@ -334,7 +362,7 @@ export const McpLogoutCommand = cmd({
prompts.intro("MCP OAuth Logout") prompts.intro("MCP OAuth Logout")
const authPath = path.join(Global.Path.data, "mcp-auth.json") const authPath = path.join(Global.Path.data, "mcp-auth.json")
const credentials = await McpAuth.all() const credentials = await AppRuntime.runPromise(McpAuth.Service.use((auth) => auth.all()))
const serverNames = Object.keys(credentials) const serverNames = Object.keys(credentials)
if (serverNames.length === 0) { if (serverNames.length === 0) {
@@ -372,7 +400,7 @@ export const McpLogoutCommand = cmd({
return return
} }
await MCP.removeAuth(serverName) await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.removeAuth(serverName)))
prompts.log.success(`Removed OAuth credentials for ${serverName}`) prompts.log.success(`Removed OAuth credentials for ${serverName}`)
prompts.outro("Done") prompts.outro("Done")
}, },
@@ -595,7 +623,7 @@ export const McpDebugCommand = cmd({
UI.empty() UI.empty()
prompts.intro("MCP OAuth Debug") prompts.intro("MCP OAuth Debug")
const config = await Config.get() const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.get()))
const mcpServers = config.mcp ?? {} const mcpServers = config.mcp ?? {}
const serverName = args.name const serverName = args.name
@@ -622,10 +650,18 @@ export const McpDebugCommand = cmd({
prompts.log.info(`URL: ${serverConfig.url}`) prompts.log.info(`URL: ${serverConfig.url}`)
// Check stored auth status // Check stored auth status
const authStatus = await MCP.getAuthStatus(serverName) const { authStatus, entry } = await AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* MCP.Service
const auth = yield* McpAuth.Service
return {
authStatus: yield* mcp.getAuthStatus(serverName),
entry: yield* auth.get(serverName),
}
}),
)
prompts.log.info(`Auth status: ${getAuthStatusIcon(authStatus)} ${getAuthStatusText(authStatus)}`) prompts.log.info(`Auth status: ${getAuthStatusIcon(authStatus)} ${getAuthStatusText(authStatus)}`)
const entry = await McpAuth.get(serverName)
if (entry?.tokens) { if (entry?.tokens) {
prompts.log.info(` Access token: ${entry.tokens.accessToken.substring(0, 20)}...`) prompts.log.info(` Access token: ${entry.tokens.accessToken.substring(0, 20)}...`)
if (entry.tokens.expiresAt) { if (entry.tokens.expiresAt) {
+1 -1
View File
@@ -326,7 +326,7 @@ export const ProvidersLoginCommand = cmd({
} }
await ModelsDev.refresh(true).catch(() => {}) await ModelsDev.refresh(true).catch(() => {})
const config = await Config.get() const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.get()))
const disabled = new Set(config.disabled_providers ?? []) const disabled = new Set(config.disabled_providers ?? [])
const enabled = config.enabled_providers ? new Set(config.enabled_providers) : undefined const enabled = config.enabled_providers ? new Set(config.enabled_providers) : undefined
@@ -9,6 +9,12 @@ import { setTimeout as sleep } from "node:timers/promises"
import { useSDK } from "../context/sdk" import { useSDK } from "../context/sdk"
import { useToast } from "../ui/toast" import { useToast } from "../ui/toast"
type Adaptor = {
type: string
name: string
description: string
}
function scoped(sdk: ReturnType<typeof useSDK>, sync: ReturnType<typeof useSync>, workspaceID: string) { function scoped(sdk: ReturnType<typeof useSDK>, sync: ReturnType<typeof useSync>, workspaceID: string) {
return createOpencodeClient({ return createOpencodeClient({
baseUrl: sdk.url, baseUrl: sdk.url,
@@ -63,9 +69,27 @@ export function DialogWorkspaceCreate(props: { onSelect: (workspaceID: string) =
const sdk = useSDK() const sdk = useSDK()
const toast = useToast() const toast = useToast()
const [creating, setCreating] = createSignal<string>() const [creating, setCreating] = createSignal<string>()
const [adaptors, setAdaptors] = createSignal<Adaptor[]>()
onMount(() => { onMount(() => {
dialog.setSize("medium") dialog.setSize("medium")
void (async () => {
const dir = sync.path.directory || sdk.directory
const url = new URL("/experimental/workspace/adaptor", sdk.url)
if (dir) url.searchParams.set("directory", dir)
const res = await sdk
.fetch(url)
.then((x) => x.json() as Promise<Adaptor[]>)
.catch(() => undefined)
if (!res) {
toast.show({
message: "Failed to load workspace adaptors",
variant: "error",
})
return
}
setAdaptors(res)
})()
}) })
const options = createMemo(() => { const options = createMemo(() => {
@@ -79,13 +103,21 @@ export function DialogWorkspaceCreate(props: { onSelect: (workspaceID: string) =
}, },
] ]
} }
const list = adaptors()
if (!list) {
return [ return [
{ {
title: "Worktree", title: "Loading workspaces...",
value: "worktree" as const, value: "loading" as const,
description: "Create a local git worktree", description: "Fetching available workspace adaptors",
}, },
] ]
}
return list.map((item) => ({
title: item.name,
value: item.type,
description: item.description,
}))
}) })
const create = async (type: string) => { const create = async (type: string) => {
@@ -113,7 +145,7 @@ export function DialogWorkspaceCreate(props: { onSelect: (workspaceID: string) =
skipFilter={true} skipFilter={true}
options={options()} options={options()}
onSelect={(option) => { onSelect={(option) => {
if (option.value === "creating") return if (option.value === "creating" || option.value === "loading") return
void create(option.value) void create(option.value)
}} }}
/> />
+1 -1
View File
@@ -81,7 +81,7 @@ export const rpc = {
}) })
}, },
async reload() { async reload() {
await Config.invalidate(true) await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.invalidate(true)))
}, },
async shutdown() { async shutdown() {
Log.Default.info("worker shutting down") Log.Default.info("worker shutting down")
+2 -1
View File
@@ -1,5 +1,6 @@
import type { Argv, InferredOptionTypes } from "yargs" import type { Argv, InferredOptionTypes } from "yargs"
import { Config } from "../config/config" import { Config } from "../config/config"
import { AppRuntime } from "@/effect/app-runtime"
const options = { const options = {
port: { port: {
@@ -37,7 +38,7 @@ export function withNetworkOptions<T>(yargs: Argv<T>) {
} }
export async function resolveNetworkOptions(args: NetworkOptions) { export async function resolveNetworkOptions(args: NetworkOptions) {
const config = await Config.getGlobal() const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal()))
const portExplicitlySet = process.argv.includes("--port") const portExplicitlySet = process.argv.includes("--port")
const hostnameExplicitlySet = process.argv.includes("--hostname") const hostnameExplicitlySet = process.argv.includes("--hostname")
const mdnsExplicitlySet = process.argv.includes("--mdns") const mdnsExplicitlySet = process.argv.includes("--mdns")
+1 -1
View File
@@ -5,7 +5,7 @@ import { Flag } from "@/flag/flag"
import { Installation } from "@/installation" import { Installation } from "@/installation"
export async function upgrade() { export async function upgrade() {
const config = await Config.getGlobal() const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal()))
const method = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method())) const method = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method()))
const latest = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.latest(method))).catch(() => {}) const latest = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.latest(method))).catch(() => {})
if (!latest) return if (!latest) return
-39
View File
@@ -33,7 +33,6 @@ import { ConfigPaths } from "./paths"
import type { ConsoleState } from "./console-state" import type { ConsoleState } from "./console-state"
import { AppFileSystem } from "@/filesystem" import { AppFileSystem } from "@/filesystem"
import { InstanceState } from "@/effect/instance-state" import { InstanceState } from "@/effect/instance-state"
import { makeRuntime } from "@/effect/run-service"
import { Context, Duration, Effect, Exit, Fiber, Layer, Option } from "effect" import { Context, Duration, Effect, Exit, Fiber, Layer, Option } from "effect"
import { Flock } from "@/util/flock" import { Flock } from "@/util/flock"
import { isPathPluginSpec, parsePluginSpecifier, resolvePathPluginTarget } from "@/plugin/shared" import { isPathPluginSpec, parsePluginSpecifier, resolvePathPluginTarget } from "@/plugin/shared"
@@ -1661,42 +1660,4 @@ export namespace Config {
Layer.provide(Auth.defaultLayer), Layer.provide(Auth.defaultLayer),
Layer.provide(Account.defaultLayer), Layer.provide(Account.defaultLayer),
) )
const { runPromise } = makeRuntime(Service, defaultLayer)
export async function get() {
return runPromise((svc) => svc.get())
}
export async function getGlobal() {
return runPromise((svc) => svc.getGlobal())
}
export async function getConsoleState() {
return runPromise((svc) => svc.getConsoleState())
}
export async function installDependencies(dir: string, input?: InstallInput) {
return runPromise((svc) => svc.installDependencies(dir, input))
}
export async function update(config: Info) {
return runPromise((svc) => svc.update(config))
}
export async function updateGlobal(config: Info) {
return runPromise((svc) => svc.updateGlobal(config))
}
export async function invalidate(wait = false) {
return runPromise((svc) => svc.invalidate(wait))
}
export async function directories() {
return runPromise((svc) => svc.directories())
}
export async function waitForDependencies() {
return runPromise((svc) => svc.waitForDependencies())
}
} }
+2 -1
View File
@@ -10,6 +10,7 @@ import { Flag } from "@/flag/flag"
import { Log } from "@/util/log" import { Log } from "@/util/log"
import { isRecord } from "@/util/record" import { isRecord } from "@/util/record"
import { Global } from "@/global" import { Global } from "@/global"
import { AppRuntime } from "@/effect/app-runtime"
export namespace TuiConfig { export namespace TuiConfig {
const log = Log.create({ service: "tui.config" }) const log = Log.create({ service: "tui.config" })
@@ -51,7 +52,7 @@ export namespace TuiConfig {
} }
function installDeps(dir: string): Promise<void> { function installDeps(dir: string): Promise<void> {
return Config.installDependencies(dir) return AppRuntime.runPromise(Config.Service.use((cfg) => cfg.installDependencies(dir)))
} }
async function mergeFile(acc: Acc, file: string) { async function mergeFile(acc: Acc, file: string) {
@@ -1,20 +1,52 @@
import { lazy } from "@/util/lazy" import { lazy } from "@/util/lazy"
import type { Adaptor } from "../types" import type { ProjectID } from "@/project/schema"
import type { WorkspaceAdaptor } from "../types"
const ADAPTORS: Record<string, () => Promise<Adaptor>> = { export type WorkspaceAdaptorEntry = {
type: string
name: string
description: string
}
const BUILTIN: Record<string, () => Promise<WorkspaceAdaptor>> = {
worktree: lazy(async () => (await import("./worktree")).WorktreeAdaptor), worktree: lazy(async () => (await import("./worktree")).WorktreeAdaptor),
} }
export function getAdaptor(type: string): Promise<Adaptor> { const state = new Map<ProjectID, Map<string, WorkspaceAdaptor>>()
return ADAPTORS[type]()
export async function getAdaptor(projectID: ProjectID, type: string): Promise<WorkspaceAdaptor> {
const custom = state.get(projectID)?.get(type)
if (custom) return custom
const builtin = BUILTIN[type]
if (builtin) return builtin()
throw new Error(`Unknown workspace adaptor: ${type}`)
} }
export function installAdaptor(type: string, adaptor: Adaptor) { export async function listAdaptors(projectID: ProjectID): Promise<WorkspaceAdaptorEntry[]> {
// This is experimental: mostly used for testing right now, but we const builtin = await Promise.all(
// will likely allow this in the future. Need to figure out the Object.entries(BUILTIN).map(async ([type, init]) => {
// TypeScript story const adaptor = await init()
return {
type,
name: adaptor.name,
description: adaptor.description,
}
}),
)
const custom = [...(state.get(projectID)?.entries() ?? [])].map(([type, adaptor]) => ({
type,
name: adaptor.name,
description: adaptor.description,
}))
return [...builtin, ...custom]
}
// @ts-expect-error we force the builtin types right now, but we // Plugins can be loaded per-project so we need to scope them. If you
// will implement a way to extend the types for custom adaptors // want to install a global one pass `ProjectID.global`
ADAPTORS[type] = () => adaptor export function registerAdaptor(projectID: ProjectID, type: string, adaptor: WorkspaceAdaptor) {
const adaptors = state.get(projectID) ?? new Map<string, WorkspaceAdaptor>()
adaptors.set(type, adaptor)
state.set(projectID, adaptors)
} }
@@ -1,18 +1,18 @@
import z from "zod" import z from "zod"
import { Worktree } from "@/worktree" import { Worktree } from "@/worktree"
import { type Adaptor, WorkspaceInfo } from "../types" import { type WorkspaceAdaptor, WorkspaceInfo } from "../types"
const Config = WorkspaceInfo.extend({ const WorktreeConfig = z.object({
name: WorkspaceInfo.shape.name.unwrap(), name: WorkspaceInfo.shape.name,
branch: WorkspaceInfo.shape.branch.unwrap(), branch: WorkspaceInfo.shape.branch.unwrap(),
directory: WorkspaceInfo.shape.directory.unwrap(), directory: WorkspaceInfo.shape.directory.unwrap(),
}) })
type Config = z.infer<typeof Config> export const WorktreeAdaptor: WorkspaceAdaptor = {
name: "Worktree",
export const WorktreeAdaptor: Adaptor = { description: "Create a git worktree",
async configure(info) { async configure(info) {
const worktree = await Worktree.makeWorktreeInfo(info.name ?? undefined) const worktree = await Worktree.makeWorktreeInfo(undefined)
return { return {
...info, ...info,
name: worktree.name, name: worktree.name,
@@ -21,7 +21,7 @@ export const WorktreeAdaptor: Adaptor = {
} }
}, },
async create(info) { async create(info) {
const config = Config.parse(info) const config = WorktreeConfig.parse(info)
await Worktree.createFromInfo({ await Worktree.createFromInfo({
name: config.name, name: config.name,
directory: config.directory, directory: config.directory,
@@ -29,11 +29,11 @@ export const WorktreeAdaptor: Adaptor = {
}) })
}, },
async remove(info) { async remove(info) {
const config = Config.parse(info) const config = WorktreeConfig.parse(info)
await Worktree.remove({ directory: config.directory }) await Worktree.remove({ directory: config.directory })
}, },
target(info) { target(info) {
const config = Config.parse(info) const config = WorktreeConfig.parse(info)
return { return {
type: "local", type: "local",
directory: config.directory, directory: config.directory,
+8 -6
View File
@@ -5,8 +5,8 @@ import { WorkspaceID } from "./schema"
export const WorkspaceInfo = z.object({ export const WorkspaceInfo = z.object({
id: WorkspaceID.zod, id: WorkspaceID.zod,
type: z.string(), type: z.string(),
name: z.string(),
branch: z.string().nullable(), branch: z.string().nullable(),
name: z.string().nullable(),
directory: z.string().nullable(), directory: z.string().nullable(),
extra: z.unknown().nullable(), extra: z.unknown().nullable(),
projectID: ProjectID.zod, projectID: ProjectID.zod,
@@ -24,9 +24,11 @@ export type Target =
headers?: HeadersInit headers?: HeadersInit
} }
export type Adaptor = { export type WorkspaceAdaptor = {
configure(input: WorkspaceInfo): WorkspaceInfo | Promise<WorkspaceInfo> name: string
create(config: WorkspaceInfo, from?: WorkspaceInfo): Promise<void> description: string
remove(config: WorkspaceInfo): Promise<void> configure(info: WorkspaceInfo): WorkspaceInfo | Promise<WorkspaceInfo>
target(config: WorkspaceInfo): Target | Promise<Target> create(info: WorkspaceInfo, from?: WorkspaceInfo): Promise<void>
remove(info: WorkspaceInfo): Promise<void>
target(info: WorkspaceInfo): Target | Promise<Target>
} }
@@ -6,8 +6,8 @@ import type { WorkspaceID } from "./schema"
export const WorkspaceTable = sqliteTable("workspace", { export const WorkspaceTable = sqliteTable("workspace", {
id: text().$type<WorkspaceID>().primaryKey(), id: text().$type<WorkspaceID>().primaryKey(),
type: text().notNull(), type: text().notNull(),
name: text().notNull().default(""),
branch: text(), branch: text(),
name: text(),
directory: text(), directory: text(),
extra: text({ mode: "json" }), extra: text({ mode: "json" }),
project_id: text() project_id: text()
@@ -9,6 +9,7 @@ import { SyncEvent } from "@/sync"
import { Log } from "@/util/log" import { Log } from "@/util/log"
import { Filesystem } from "@/util/filesystem" import { Filesystem } from "@/util/filesystem"
import { ProjectID } from "@/project/schema" import { ProjectID } from "@/project/schema"
import { Slug } from "@opencode-ai/util/slug"
import { WorkspaceTable } from "./workspace.sql" import { WorkspaceTable } from "./workspace.sql"
import { getAdaptor } from "./adaptors" import { getAdaptor } from "./adaptors"
import { WorkspaceInfo } from "./types" import { WorkspaceInfo } from "./types"
@@ -66,9 +67,9 @@ export namespace Workspace {
export const create = fn(CreateInput, async (input) => { export const create = fn(CreateInput, async (input) => {
const id = WorkspaceID.ascending(input.id) const id = WorkspaceID.ascending(input.id)
const adaptor = await getAdaptor(input.type) const adaptor = await getAdaptor(input.projectID, input.type)
const config = await adaptor.configure({ ...input, id, name: null, directory: null }) const config = await adaptor.configure({ ...input, id, name: Slug.create(), directory: null })
const info: Info = { const info: Info = {
id, id,
@@ -124,7 +125,7 @@ export namespace Workspace {
stopSync(id) stopSync(id)
const info = fromRow(row) const info = fromRow(row)
const adaptor = await getAdaptor(row.type) const adaptor = await getAdaptor(info.projectID, row.type)
adaptor.remove(info) adaptor.remove(info)
Database.use((db) => db.delete(WorkspaceTable).where(eq(WorkspaceTable.id, id)).run()) Database.use((db) => db.delete(WorkspaceTable).where(eq(WorkspaceTable.id, id)).run())
return info return info
@@ -162,7 +163,7 @@ export namespace Workspace {
log.info("connecting to sync: " + space.id) log.info("connecting to sync: " + space.id)
setStatus(space.id, "connecting") setStatus(space.id, "connecting")
const adaptor = await getAdaptor(space.type) const adaptor = await getAdaptor(space.projectID, space.type)
const target = await adaptor.target(space) const target = await adaptor.target(space)
if (target.type === "local") return if (target.type === "local") return
+1
View File
@@ -13,6 +13,7 @@ export namespace Identifier {
pty: "pty", pty: "pty",
tool: "tool", tool: "tool",
workspace: "wrk", workspace: "wrk",
entry: "ent",
} as const } as const
export function schema(prefix: keyof typeof prefixes) { export function schema(prefix: keyof typeof prefixes) {
-34
View File
@@ -27,7 +27,6 @@ import open from "open"
import { Effect, Exit, Layer, Option, Context, Stream } from "effect" import { Effect, Exit, Layer, Option, Context, Stream } from "effect"
import { EffectLogger } from "@/effect/logger" import { EffectLogger } from "@/effect/logger"
import { InstanceState } from "@/effect/instance-state" import { InstanceState } from "@/effect/instance-state"
import { makeRuntime } from "@/effect/run-service"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process" import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner" import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
@@ -890,37 +889,4 @@ export namespace MCP {
Layer.provide(CrossSpawnSpawner.defaultLayer), Layer.provide(CrossSpawnSpawner.defaultLayer),
Layer.provide(AppFileSystem.defaultLayer), Layer.provide(AppFileSystem.defaultLayer),
) )
const { runPromise } = makeRuntime(Service, defaultLayer)
// --- Async facade functions ---
export const status = async () => runPromise((svc) => svc.status())
export const tools = async () => runPromise((svc) => svc.tools())
export const prompts = async () => runPromise((svc) => svc.prompts())
export const resources = async () => runPromise((svc) => svc.resources())
export const add = async (name: string, mcp: Config.Mcp) => runPromise((svc) => svc.add(name, mcp))
export const connect = async (name: string) => runPromise((svc) => svc.connect(name))
export const disconnect = async (name: string) => runPromise((svc) => svc.disconnect(name))
export const startAuth = async (mcpName: string) => runPromise((svc) => svc.startAuth(mcpName))
export const authenticate = async (mcpName: string) => runPromise((svc) => svc.authenticate(mcpName))
export const finishAuth = async (mcpName: string, authorizationCode: string) =>
runPromise((svc) => svc.finishAuth(mcpName, authorizationCode))
export const removeAuth = async (mcpName: string) => runPromise((svc) => svc.removeAuth(mcpName))
export const supportsOAuth = async (mcpName: string) => runPromise((svc) => svc.supportsOAuth(mcpName))
export const hasStoredTokens = async (mcpName: string) => runPromise((svc) => svc.hasStoredTokens(mcpName))
export const getAuthStatus = async (mcpName: string) => runPromise((svc) => svc.getAuthStatus(mcpName))
} }
+14 -1
View File
@@ -1,4 +1,10 @@
import type { Hooks, PluginInput, Plugin as PluginInstance, PluginModule } from "@opencode-ai/plugin" import type {
Hooks,
PluginInput,
Plugin as PluginInstance,
PluginModule,
WorkspaceAdaptor as PluginWorkspaceAdaptor,
} from "@opencode-ai/plugin"
import { Config } from "../config/config" import { Config } from "../config/config"
import { Bus } from "../bus" import { Bus } from "../bus"
import { Log } from "../util/log" import { Log } from "../util/log"
@@ -18,6 +24,8 @@ import { makeRuntime } from "@/effect/run-service"
import { errorMessage } from "@/util/error" import { errorMessage } from "@/util/error"
import { PluginLoader } from "./loader" import { PluginLoader } from "./loader"
import { parsePluginSpecifier, readPluginId, readV1Plugin, resolvePluginId } from "./shared" import { parsePluginSpecifier, readPluginId, readV1Plugin, resolvePluginId } from "./shared"
import { registerAdaptor } from "@/control-plane/adaptors"
import type { WorkspaceAdaptor } from "@/control-plane/types"
export namespace Plugin { export namespace Plugin {
const log = Log.create({ service: "plugin" }) const log = Log.create({ service: "plugin" })
@@ -132,6 +140,11 @@ export namespace Plugin {
project: ctx.project, project: ctx.project,
worktree: ctx.worktree, worktree: ctx.worktree,
directory: ctx.directory, directory: ctx.directory,
experimental_workspace: {
register(type: string, adaptor: PluginWorkspaceAdaptor) {
registerAdaptor(ctx.project.id, type, adaptor as WorkspaceAdaptor)
},
},
get serverUrl(): URL { get serverUrl(): URL {
return Server.url ?? new URL("http://localhost:4096") return Server.url ?? new URL("http://localhost:4096")
}, },
@@ -32,7 +32,7 @@ export const ConfigRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
return c.json(await Config.get()) return c.json(await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.get())))
}, },
) )
.patch( .patch(
@@ -56,7 +56,7 @@ export const ConfigRoutes = lazy(() =>
validator("json", Config.Info), validator("json", Config.Info),
async (c) => { async (c) => {
const config = c.req.valid("json") const config = c.req.valid("json")
await Config.update(config) await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.update(config)))
return c.json(config) return c.json(config)
}, },
) )
@@ -408,7 +408,14 @@ export const ExperimentalRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
return c.json(await MCP.resources()) return c.json(
await AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* MCP.Service
return yield* mcp.resources()
}),
),
)
}, },
), ),
) )
+19 -5
View File
@@ -74,14 +74,16 @@ export const FileRoutes = lazy(() =>
const type = c.req.valid("query").type const type = c.req.valid("query").type
const limit = c.req.valid("query").limit const limit = c.req.valid("query").limit
const results = await AppRuntime.runPromise( const results = await AppRuntime.runPromise(
File.Service.use((svc) => Effect.gen(function* () {
return yield* File.Service.use((svc) =>
svc.search({ svc.search({
query, query,
limit: limit ?? 10, limit: limit ?? 10,
dirs: dirs !== "false", dirs: dirs !== "false",
type, type,
}), }),
), )
}),
) )
return c.json(results) return c.json(results)
}, },
@@ -138,7 +140,11 @@ export const FileRoutes = lazy(() =>
), ),
async (c) => { async (c) => {
const path = c.req.valid("query").path const path = c.req.valid("query").path
const content = await AppRuntime.runPromise(File.Service.use((svc) => svc.list(path))) const content = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.list(path))
}),
)
return c.json(content) return c.json(content)
}, },
) )
@@ -167,7 +173,11 @@ export const FileRoutes = lazy(() =>
), ),
async (c) => { async (c) => {
const path = c.req.valid("query").path const path = c.req.valid("query").path
const content = await AppRuntime.runPromise(File.Service.use((svc) => svc.read(path))) const content = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.read(path))
}),
)
return c.json(content) return c.json(content)
}, },
) )
@@ -189,7 +199,11 @@ export const FileRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
const content = await AppRuntime.runPromise(File.Service.use((svc) => svc.status())) const content = await AppRuntime.runPromise(
Effect.gen(function* () {
return yield* File.Service.use((svc) => svc.status())
}),
)
return c.json(content) return c.json(content)
}, },
), ),
@@ -199,7 +199,7 @@ export const GlobalRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
return c.json(await Config.getGlobal()) return c.json(await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal())))
}, },
) )
.patch( .patch(
@@ -223,7 +223,7 @@ export const GlobalRoutes = lazy(() =>
validator("json", Config.Info), validator("json", Config.Info),
async (c) => { async (c) => {
const config = c.req.valid("json") const config = c.req.valid("json")
const next = await Config.updateGlobal(config) const next = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.updateGlobal(config)))
return c.json(next) return c.json(next)
}, },
) )
+34 -14
View File
@@ -3,8 +3,10 @@ import { describeRoute, validator, resolver } from "hono-openapi"
import z from "zod" import z from "zod"
import { MCP } from "../../mcp" import { MCP } from "../../mcp"
import { Config } from "../../config/config" import { Config } from "../../config/config"
import { AppRuntime } from "../../effect/app-runtime"
import { errors } from "../error" import { errors } from "../error"
import { lazy } from "../../util/lazy" import { lazy } from "../../util/lazy"
import { Effect } from "effect"
export const McpRoutes = lazy(() => export const McpRoutes = lazy(() =>
new Hono() new Hono()
@@ -26,7 +28,7 @@ export const McpRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
return c.json(await MCP.status()) return c.json(await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.status())))
}, },
) )
.post( .post(
@@ -56,7 +58,7 @@ export const McpRoutes = lazy(() =>
), ),
async (c) => { async (c) => {
const { name, config } = c.req.valid("json") const { name, config } = c.req.valid("json")
const result = await MCP.add(name, config) const result = await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.add(name, config)))
return c.json(result.status) return c.json(result.status)
}, },
) )
@@ -84,12 +86,21 @@ export const McpRoutes = lazy(() =>
}), }),
async (c) => { async (c) => {
const name = c.req.param("name") const name = c.req.param("name")
const supportsOAuth = await MCP.supportsOAuth(name) const result = await AppRuntime.runPromise(
if (!supportsOAuth) { Effect.gen(function* () {
const mcp = yield* MCP.Service
const supports = yield* mcp.supportsOAuth(name)
if (!supports) return { supports }
return {
supports,
auth: yield* mcp.startAuth(name),
}
}),
)
if (!result.supports) {
return c.json({ error: `MCP server ${name} does not support OAuth` }, 400) return c.json({ error: `MCP server ${name} does not support OAuth` }, 400)
} }
const result = await MCP.startAuth(name) return c.json(result.auth)
return c.json(result)
}, },
) )
.post( .post(
@@ -120,7 +131,7 @@ export const McpRoutes = lazy(() =>
async (c) => { async (c) => {
const name = c.req.param("name") const name = c.req.param("name")
const { code } = c.req.valid("json") const { code } = c.req.valid("json")
const status = await MCP.finishAuth(name, code) const status = await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.finishAuth(name, code)))
return c.json(status) return c.json(status)
}, },
) )
@@ -144,12 +155,21 @@ export const McpRoutes = lazy(() =>
}), }),
async (c) => { async (c) => {
const name = c.req.param("name") const name = c.req.param("name")
const supportsOAuth = await MCP.supportsOAuth(name) const result = await AppRuntime.runPromise(
if (!supportsOAuth) { Effect.gen(function* () {
const mcp = yield* MCP.Service
const supports = yield* mcp.supportsOAuth(name)
if (!supports) return { supports }
return {
supports,
status: yield* mcp.authenticate(name),
}
}),
)
if (!result.supports) {
return c.json({ error: `MCP server ${name} does not support OAuth` }, 400) return c.json({ error: `MCP server ${name} does not support OAuth` }, 400)
} }
const status = await MCP.authenticate(name) return c.json(result.status)
return c.json(status)
}, },
) )
.delete( .delete(
@@ -172,7 +192,7 @@ export const McpRoutes = lazy(() =>
}), }),
async (c) => { async (c) => {
const name = c.req.param("name") const name = c.req.param("name")
await MCP.removeAuth(name) await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.removeAuth(name)))
return c.json({ success: true as const }) return c.json({ success: true as const })
}, },
) )
@@ -195,7 +215,7 @@ export const McpRoutes = lazy(() =>
validator("param", z.object({ name: z.string() })), validator("param", z.object({ name: z.string() })),
async (c) => { async (c) => {
const { name } = c.req.valid("param") const { name } = c.req.valid("param")
await MCP.connect(name) await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.connect(name)))
return c.json(true) return c.json(true)
}, },
) )
@@ -218,7 +238,7 @@ export const McpRoutes = lazy(() =>
validator("param", z.object({ name: z.string() })), validator("param", z.object({ name: z.string() })),
async (c) => { async (c) => {
const { name } = c.req.valid("param") const { name } = c.req.valid("param")
await MCP.disconnect(name) await AppRuntime.runPromise(MCP.Service.use((mcp) => mcp.disconnect(name)))
return c.json(true) return c.json(true)
}, },
), ),
@@ -95,7 +95,7 @@ export function WorkspaceRouterMiddleware(upgrade: UpgradeWebSocket): Middleware
}) })
} }
const adaptor = await getAdaptor(workspace.type) const adaptor = await getAdaptor(workspace.projectID, workspace.type)
const target = await adaptor.target(workspace) const target = await adaptor.target(workspace)
if (target.type === "local") { if (target.type === "local") {
@@ -44,7 +44,8 @@ export const ProviderRoutes = lazy(() =>
const result = await AppRuntime.runPromise( const result = await AppRuntime.runPromise(
Effect.gen(function* () { Effect.gen(function* () {
const svc = yield* Provider.Service const svc = yield* Provider.Service
const config = yield* Effect.promise(() => Config.get()) const cfg = yield* Config.Service
const config = yield* cfg.get()
const all = yield* Effect.promise(() => ModelsDev.get()) const all = yield* Effect.promise(() => ModelsDev.get())
const disabled = new Set(config.disabled_providers ?? []) const disabled = new Set(config.disabled_providers ?? [])
const enabled = config.enabled_providers ? new Set(config.enabled_providers) : undefined const enabled = config.enabled_providers ? new Set(config.enabled_providers) : undefined
@@ -1,13 +1,41 @@
import { Hono } from "hono" import { Hono } from "hono"
import { describeRoute, resolver, validator } from "hono-openapi" import { describeRoute, resolver, validator } from "hono-openapi"
import z from "zod" import z from "zod"
import { listAdaptors } from "../../control-plane/adaptors"
import { Workspace } from "../../control-plane/workspace" import { Workspace } from "../../control-plane/workspace"
import { Instance } from "../../project/instance" import { Instance } from "../../project/instance"
import { errors } from "../error" import { errors } from "../error"
import { lazy } from "../../util/lazy" import { lazy } from "../../util/lazy"
const WorkspaceAdaptor = z.object({
type: z.string(),
name: z.string(),
description: z.string(),
})
export const WorkspaceRoutes = lazy(() => export const WorkspaceRoutes = lazy(() =>
new Hono() new Hono()
.get(
"/adaptor",
describeRoute({
summary: "List workspace adaptors",
description: "List all available workspace adaptors for the current project.",
operationId: "experimental.workspace.adaptor.list",
responses: {
200: {
description: "Workspace adaptors",
content: {
"application/json": {
schema: resolver(z.array(WorkspaceAdaptor)),
},
},
},
},
}),
async (c) => {
return c.json(await listAdaptors(Instance.project.id))
},
)
.post( .post(
"/", "/",
describeRoute({ describeRoute({
+31 -1
View File
@@ -1,10 +1,11 @@
import { NotFoundError, eq, and } from "../storage/db" import { NotFoundError, eq, and, sql } from "../storage/db"
import { SyncEvent } from "@/sync" import { SyncEvent } from "@/sync"
import { Session } from "./index" import { Session } from "./index"
import { MessageV2 } from "./message-v2" import { MessageV2 } from "./message-v2"
import { SessionTable, MessageTable, PartTable } from "./session.sql" import { SessionTable, MessageTable, PartTable } from "./session.sql"
import { ProjectTable } from "../project/project.sql" import { ProjectTable } from "../project/project.sql"
import { Log } from "../util/log" import { Log } from "../util/log"
import { DateTime } from "effect"
const log = Log.create({ service: "session.projector" }) const log = Log.create({ service: "session.projector" })
@@ -132,4 +133,33 @@ export default [
log.warn("ignored late part update", { partID: id, messageID, sessionID }) log.warn("ignored late part update", { partID: id, messageID, sessionID })
} }
}), }),
// Experimental
SyncEvent.project(MessageV2.Event.PartUpdated, (db, data) => {
/*
const id = SessionEntry.ID.make(data.part.id.replace("prt", "ent"))
switch (data.part.type) {
case "text":
db.insert(SessionEntryTable)
.values({
id,
session_id: data.sessionID,
type: "text",
data: new SessionEntry.Text({
id,
text: data.part.text,
type: "text",
time: {
created: DateTime.makeUnsafe(data.part.time?.start ?? Date.now()),
completed: data.part.time?.end ? DateTime.makeUnsafe(data.part.time.end) : undefined,
},
}),
time_created: Date.now(),
time_updated: Date.now(),
})
.onConflictDoUpdate({ target: SessionEntryTable.id, set: { data: sql`excluded.data` } })
.run()
}
*/
}),
] ]
@@ -1,6 +1,7 @@
import { sqliteTable, text, integer, index, primaryKey } from "drizzle-orm/sqlite-core" import { sqliteTable, text, integer, index, primaryKey } from "drizzle-orm/sqlite-core"
import { ProjectTable } from "../project/project.sql" import { ProjectTable } from "../project/project.sql"
import type { MessageV2 } from "./message-v2" import type { MessageV2 } from "./message-v2"
import type { SessionEntry } from "../v2/session-entry"
import type { Snapshot } from "../snapshot" import type { Snapshot } from "../snapshot"
import type { Permission } from "../permission" import type { Permission } from "../permission"
import type { ProjectID } from "../project/schema" import type { ProjectID } from "../project/schema"
@@ -10,6 +11,7 @@ import { Timestamps } from "../storage/schema.sql"
type PartData = Omit<MessageV2.Part, "id" | "sessionID" | "messageID"> type PartData = Omit<MessageV2.Part, "id" | "sessionID" | "messageID">
type InfoData = Omit<MessageV2.Info, "id" | "sessionID"> type InfoData = Omit<MessageV2.Info, "id" | "sessionID">
type EntryData = Omit<SessionEntry.Entry, "id" | "type">
export const SessionTable = sqliteTable( export const SessionTable = sqliteTable(
"session", "session",
@@ -94,6 +96,27 @@ export const TodoTable = sqliteTable(
], ],
) )
/*
export const SessionEntryTable = sqliteTable(
"session_entry",
{
id: text().$type<SessionEntry.ID>().primaryKey(),
session_id: text()
.$type<SessionID>()
.notNull()
.references(() => SessionTable.id, { onDelete: "cascade" }),
type: text().notNull(),
...Timestamps,
data: text({ mode: "json" }).notNull().$type<SessionEntry.Entry>(),
},
(table) => [
index("session_entry_session_idx").on(table.session_id),
index("session_entry_session_type_idx").on(table.session_id, table.type),
index("session_entry_time_created_idx").on(table.time_created),
],
)
*/
export const PermissionTable = sqliteTable("permission", { export const PermissionTable = sqliteTable("permission", {
project_id: text() project_id: text()
.primaryKey() .primaryKey()
-115
View File
@@ -1,115 +0,0 @@
import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema"
import { DateTime, Effect, Schema } from "effect"
export namespace Message {
export const ID = Schema.String.pipe(Schema.brand("Message.ID")).pipe(
withStatics((s) => ({
create: () => s.make(Identifier.ascending("message")),
prefix: "msg",
})),
)
export class Source extends Schema.Class<Source>("Message.Source")({
start: Schema.Number,
end: Schema.Number,
text: Schema.String,
}) {}
export class FileAttachment extends Schema.Class<FileAttachment>("Message.File.Attachment")({
uri: Schema.String,
mime: Schema.String,
name: Schema.String.pipe(Schema.optional),
description: Schema.String.pipe(Schema.optional),
source: Source.pipe(Schema.optional),
}) {
static create(url: string) {
return new FileAttachment({
uri: url,
mime: "text/plain",
})
}
}
export class AgentAttachment extends Schema.Class<AgentAttachment>("Message.Agent.Attachment")({
name: Schema.String,
source: Source.pipe(Schema.optional),
}) {}
export class User extends Schema.Class<User>("Message.User")({
id: ID,
type: Schema.Literal("user"),
text: Schema.String,
files: Schema.Array(FileAttachment).pipe(Schema.optional),
agents: Schema.Array(AgentAttachment).pipe(Schema.optional),
time: Schema.Struct({
created: Schema.DateTimeUtc,
}),
}) {
static create(input: { text: User["text"]; files?: User["files"]; agents?: User["agents"] }) {
const msg = new User({
id: ID.create(),
type: "user",
...input,
time: {
created: Effect.runSync(DateTime.now),
},
})
return msg
}
}
export class Synthetic extends Schema.Class<Synthetic>("Message.Synthetic")({
id: ID,
type: Schema.Literal("synthetic"),
text: Schema.String,
time: Schema.Struct({
created: Schema.DateTimeUtc,
}),
}) {}
export class Request extends Schema.Class<Request>("Message.Request")({
id: ID,
type: Schema.Literal("start"),
model: Schema.Struct({
id: Schema.String,
providerID: Schema.String,
variant: Schema.String.pipe(Schema.optional),
}),
time: Schema.Struct({
created: Schema.DateTimeUtc,
}),
}) {}
export class Text extends Schema.Class<Text>("Message.Text")({
id: ID,
type: Schema.Literal("text"),
text: Schema.String,
time: Schema.Struct({
created: Schema.DateTimeUtc,
completed: Schema.DateTimeUtc.pipe(Schema.optional),
}),
}) {}
export class Complete extends Schema.Class<Complete>("Message.Complete")({
id: ID,
type: Schema.Literal("complete"),
time: Schema.Struct({
created: Schema.DateTimeUtc,
}),
cost: Schema.Number,
tokens: Schema.Struct({
total: Schema.Number,
input: Schema.Number,
output: Schema.Number,
reasoning: Schema.Number,
cache: Schema.Struct({
read: Schema.Number,
write: Schema.Number,
}),
}),
}) {}
export const Info = Schema.Union([User, Text])
export type Info = Schema.Schema.Type<typeof Info>
}
+186
View File
@@ -0,0 +1,186 @@
import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema"
import { DateTime, Effect, Schema } from "effect"
export namespace SessionEntry {
export const ID = Schema.String.pipe(Schema.brand("Session.Entry.ID")).pipe(
withStatics((s) => ({
create: () => s.make(Identifier.ascending("entry")),
prefix: "ent",
})),
)
export type ID = Schema.Schema.Type<typeof ID>
const Base = {
id: ID,
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
time: Schema.Struct({
created: Schema.DateTimeUtc,
}),
}
export class Source extends Schema.Class<Source>("Session.Entry.Source")({
start: Schema.Number,
end: Schema.Number,
text: Schema.String,
}) {}
export class FileAttachment extends Schema.Class<FileAttachment>("Session.Entry.File.Attachment")({
uri: Schema.String,
mime: Schema.String,
name: Schema.String.pipe(Schema.optional),
description: Schema.String.pipe(Schema.optional),
source: Source.pipe(Schema.optional),
}) {
static create(url: string) {
return new FileAttachment({
uri: url,
mime: "text/plain",
})
}
}
export class AgentAttachment extends Schema.Class<AgentAttachment>("Session.Entry.Agent.Attachment")({
name: Schema.String,
source: Source.pipe(Schema.optional),
}) {}
export class User extends Schema.Class<User>("Session.Entry.User")({
...Base,
type: Schema.Literal("user"),
text: Schema.String,
files: Schema.Array(FileAttachment).pipe(Schema.optional),
agents: Schema.Array(AgentAttachment).pipe(Schema.optional),
}) {
static create(input: { text: User["text"]; files?: User["files"]; agents?: User["agents"] }) {
const msg = new User({
id: ID.create(),
type: "user",
...input,
time: {
created: Effect.runSync(DateTime.now),
},
})
return msg
}
}
export class Synthetic extends Schema.Class<Synthetic>("Session.Entry.Synthetic")({
...Base,
type: Schema.Literal("synthetic"),
text: Schema.String,
}) {}
export class Request extends Schema.Class<Request>("Session.Entry.Request")({
...Base,
type: Schema.Literal("start"),
model: Schema.Struct({
id: Schema.String,
providerID: Schema.String,
variant: Schema.String.pipe(Schema.optional),
}),
}) {}
export class Text extends Schema.Class<Text>("Session.Entry.Text")({
...Base,
type: Schema.Literal("text"),
text: Schema.String,
time: Schema.Struct({
...Base.time.fields,
completed: Schema.DateTimeUtc.pipe(Schema.optional),
}),
}) {}
export class Reasoning extends Schema.Class<Reasoning>("Session.Entry.Reasoning")({
...Base,
type: Schema.Literal("reasoning"),
text: Schema.String,
time: Schema.Struct({
...Base.time.fields,
completed: Schema.DateTimeUtc.pipe(Schema.optional),
}),
}) {}
export class ToolStatePending extends Schema.Class<ToolStatePending>("Session.Entry.ToolState.Pending")({
status: Schema.Literal("pending"),
input: Schema.Record(Schema.String, Schema.Unknown),
raw: Schema.String,
}) {}
export class ToolStateRunning extends Schema.Class<ToolStateRunning>("Session.Entry.ToolState.Running")({
status: Schema.Literal("running"),
input: Schema.Record(Schema.String, Schema.Unknown),
title: Schema.String.pipe(Schema.optional),
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
}) {}
export class ToolStateCompleted extends Schema.Class<ToolStateCompleted>("Session.Entry.ToolState.Completed")({
status: Schema.Literal("completed"),
input: Schema.Record(Schema.String, Schema.Unknown),
output: Schema.String,
title: Schema.String,
metadata: Schema.Record(Schema.String, Schema.Unknown),
attachments: Schema.Array(FileAttachment).pipe(Schema.optional),
}) {}
export class ToolStateError extends Schema.Class<ToolStateError>("Session.Entry.ToolState.Error")({
status: Schema.Literal("error"),
input: Schema.Record(Schema.String, Schema.Unknown),
error: Schema.String,
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
time: Schema.Struct({
start: Schema.Number,
end: Schema.Number,
}),
}) {}
export const ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
export type ToolState = Schema.Schema.Type<typeof ToolState>
export class Tool extends Schema.Class<Tool>("Session.Entry.Tool")({
...Base,
type: Schema.Literal("tool"),
callID: Schema.String,
name: Schema.String,
state: ToolState,
time: Schema.Struct({
...Base.time.fields,
ran: Schema.DateTimeUtc.pipe(Schema.optional),
completed: Schema.DateTimeUtc.pipe(Schema.optional),
pruned: Schema.DateTimeUtc.pipe(Schema.optional),
}),
}) {}
export class Complete extends Schema.Class<Complete>("Session.Entry.Complete")({
...Base,
type: Schema.Literal("complete"),
cost: Schema.Number,
reason: Schema.String,
tokens: Schema.Struct({
input: Schema.Number,
output: Schema.Number,
reasoning: Schema.Number,
cache: Schema.Struct({
read: Schema.Number,
write: Schema.Number,
}),
}),
}) {}
export class Retry extends Schema.Class<Retry>("Session.Entry.Retry")({
...Base,
type: Schema.Literal("retry"),
attempt: Schema.Number,
error: Schema.String,
}) {}
export class Compaction extends Schema.Class<Compaction>("Session.Entry.Compaction")({
...Base,
type: Schema.Literal("compaction"),
auto: Schema.Boolean,
overflow: Schema.Boolean.pipe(Schema.optional),
}) {}
export const Entry = Schema.Union([User, Synthetic, Request, Tool, Text, Reasoning, Complete, Retry, Compaction])
export type Entry = Schema.Schema.Type<typeof Entry>
}
+4 -4
View File
@@ -1,5 +1,5 @@
import { Context, Layer, Schema, Effect } from "effect" import { Context, Layer, Schema, Effect } from "effect"
import { Message } from "./message" import { SessionEntry } from "./session-entry"
import { Struct } from "effect" import { Struct } from "effect"
import { Identifier } from "@/id/id" import { Identifier } from "@/id/id"
import { withStatics } from "@/util/schema" import { withStatics } from "@/util/schema"
@@ -12,8 +12,8 @@ export namespace SessionV2 {
export type ID = Schema.Schema.Type<typeof ID> export type ID = Schema.Schema.Type<typeof ID>
export class PromptInput extends Schema.Class<PromptInput>("Session.PromptInput")({ export class PromptInput extends Schema.Class<PromptInput>("Session.PromptInput")({
...Struct.omit(Message.User.fields, ["time", "type"]), ...Struct.omit(SessionEntry.User.fields, ["time", "type"]),
id: Schema.optionalKey(Message.ID), id: Schema.optionalKey(SessionEntry.ID),
sessionID: SessionV2.ID, sessionID: SessionV2.ID,
}) {} }) {}
@@ -33,7 +33,7 @@ export namespace SessionV2 {
export interface Interface { export interface Interface {
fromID: (id: SessionV2.ID) => Effect.Effect<Info> fromID: (id: SessionV2.ID) => Effect.Effect<Info>
create: (input: CreateInput) => Effect.Effect<Info> create: (input: CreateInput) => Effect.Effect<Info>
prompt: (input: PromptInput) => Effect.Effect<Message.User> prompt: (input: PromptInput) => Effect.Effect<SessionEntry.User>
} }
export class Service extends Context.Service<Service, Interface>()("Session.Service") {} export class Service extends Context.Service<Service, Interface>()("Session.Service") {}
@@ -5,6 +5,9 @@ import { Instance } from "../../src/project/instance"
import { Config } from "../../src/config/config" import { Config } from "../../src/config/config"
import { Agent as AgentSvc } from "../../src/agent/agent" import { Agent as AgentSvc } from "../../src/agent/agent"
import { Color } from "../../src/util/color" import { Color } from "../../src/util/color"
import { AppRuntime } from "../../src/effect/app-runtime"
const load = () => AppRuntime.runPromise(Config.Service.use((svc) => svc.get()))
test("agent color parsed from project config", async () => { test("agent color parsed from project config", async () => {
await using tmp = await tmpdir({ await using tmp = await tmpdir({
@@ -24,7 +27,7 @@ test("agent color parsed from project config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const cfg = await Config.get() const cfg = await load()
expect(cfg.agent?.["build"]?.color).toBe("#FFA500") expect(cfg.agent?.["build"]?.color).toBe("#FFA500")
expect(cfg.agent?.["plan"]?.color).toBe("primary") expect(cfg.agent?.["plan"]?.color).toBe("primary")
}, },
+73 -63
View File
@@ -33,15 +33,25 @@ const emptyAuth = Layer.mock(Auth.Service)({
all: () => Effect.succeed({}), all: () => Effect.succeed({}),
}) })
const it = testEffect( const layer = Config.layer.pipe(
Config.layer.pipe(
Layer.provide(AppFileSystem.defaultLayer), Layer.provide(AppFileSystem.defaultLayer),
Layer.provide(emptyAuth), Layer.provide(emptyAuth),
Layer.provide(emptyAccount), Layer.provide(emptyAccount),
Layer.provideMerge(infra), Layer.provideMerge(infra),
),
) )
const it = testEffect(layer)
const load = () => Effect.runPromise(Config.Service.use((svc) => svc.get()).pipe(Effect.scoped, Effect.provide(layer)))
const save = (config: Config.Info) =>
Effect.runPromise(Config.Service.use((svc) => svc.update(config)).pipe(Effect.scoped, Effect.provide(layer)))
const clear = (wait = false) =>
Effect.runPromise(Config.Service.use((svc) => svc.invalidate(wait)).pipe(Effect.scoped, Effect.provide(layer)))
const listDirs = () =>
Effect.runPromise(Config.Service.use((svc) => svc.directories()).pipe(Effect.scoped, Effect.provide(layer)))
const ready = () =>
Effect.runPromise(Config.Service.use((svc) => svc.waitForDependencies()).pipe(Effect.scoped, Effect.provide(layer)))
const installDeps = (dir: string, input?: Config.InstallInput) => const installDeps = (dir: string, input?: Config.InstallInput) =>
Config.Service.use((svc) => svc.installDependencies(dir, input)) Config.Service.use((svc) => svc.installDependencies(dir, input))
@@ -49,12 +59,12 @@ const installDeps = (dir: string, input?: Config.InstallInput) =>
const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR! const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
beforeEach(async () => { beforeEach(async () => {
await Config.invalidate(true) await clear(true)
}) })
afterEach(async () => { afterEach(async () => {
await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {}) await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
await Config.invalidate(true) await clear(true)
}) })
async function writeManagedSettings(settings: object, filename = "opencode.json") { async function writeManagedSettings(settings: object, filename = "opencode.json") {
@@ -72,7 +82,7 @@ async function check(map: (dir: string) => string) {
await using tmp = await tmpdir({ git: true, config: { snapshot: true } }) await using tmp = await tmpdir({ git: true, config: { snapshot: true } })
const prev = Global.Path.config const prev = Global.Path.config
;(Global.Path as { config: string }).config = globalTmp.path ;(Global.Path as { config: string }).config = globalTmp.path
await Config.invalidate() await clear()
try { try {
await writeConfig(globalTmp.path, { await writeConfig(globalTmp.path, {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
@@ -81,7 +91,7 @@ async function check(map: (dir: string) => string) {
await Instance.provide({ await Instance.provide({
directory: map(tmp.path), directory: map(tmp.path),
fn: async () => { fn: async () => {
const cfg = await Config.get() const cfg = await load()
expect(cfg.snapshot).toBe(true) expect(cfg.snapshot).toBe(true)
expect(Instance.directory).toBe(Filesystem.resolve(tmp.path)) expect(Instance.directory).toBe(Filesystem.resolve(tmp.path))
expect(Instance.project.id).not.toBe(ProjectID.global) expect(Instance.project.id).not.toBe(ProjectID.global)
@@ -90,7 +100,7 @@ async function check(map: (dir: string) => string) {
} finally { } finally {
await Instance.disposeAll() await Instance.disposeAll()
;(Global.Path as { config: string }).config = prev ;(Global.Path as { config: string }).config = prev
await Config.invalidate() await clear()
} }
} }
@@ -99,7 +109,7 @@ test("loads config with defaults when no files exist", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBeDefined() expect(config.username).toBeDefined()
}, },
}) })
@@ -118,7 +128,7 @@ test("loads JSON config file", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("test/model") expect(config.model).toBe("test/model")
expect(config.username).toBe("testuser") expect(config.username).toBe("testuser")
}, },
@@ -156,7 +166,7 @@ test("ignores legacy tui keys in opencode config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("test/model") expect(config.model).toBe("test/model")
expect((config as Record<string, unknown>).theme).toBeUndefined() expect((config as Record<string, unknown>).theme).toBeUndefined()
expect((config as Record<string, unknown>).tui).toBeUndefined() expect((config as Record<string, unknown>).tui).toBeUndefined()
@@ -181,7 +191,7 @@ test("loads JSONC config file", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("test/model") expect(config.model).toBe("test/model")
expect(config.username).toBe("testuser") expect(config.username).toBe("testuser")
}, },
@@ -209,7 +219,7 @@ test("jsonc overrides json in the same directory", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("base") expect(config.model).toBe("base")
expect(config.username).toBe("base") expect(config.username).toBe("base")
}, },
@@ -232,7 +242,7 @@ test("handles environment variable substitution", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("test-user") expect(config.username).toBe("test-user")
}, },
}) })
@@ -264,7 +274,7 @@ test("preserves env variables when adding $schema to config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("secret_value") expect(config.username).toBe("secret_value")
// Read the file to verify the env variable was preserved // Read the file to verify the env variable was preserved
@@ -358,7 +368,7 @@ test("handles file inclusion substitution", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("test-user") expect(config.username).toBe("test-user")
}, },
}) })
@@ -377,7 +387,7 @@ test("handles file inclusion with replacement tokens", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("const out = await Bun.$`echo hi`") expect(config.username).toBe("const out = await Bun.$`echo hi`")
}, },
}) })
@@ -396,7 +406,7 @@ test("validates config schema and throws on invalid fields", async () => {
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
// Strict schema should throw an error for invalid fields // Strict schema should throw an error for invalid fields
await expect(Config.get()).rejects.toThrow() await expect(load()).rejects.toThrow()
}, },
}) })
}) })
@@ -410,7 +420,7 @@ test("throws error for invalid JSON", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
await expect(Config.get()).rejects.toThrow() await expect(load()).rejects.toThrow()
}, },
}) })
}) })
@@ -433,7 +443,7 @@ test("handles agent configuration", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test_agent"]).toEqual( expect(config.agent?.["test_agent"]).toEqual(
expect.objectContaining({ expect.objectContaining({
model: "test/model", model: "test/model",
@@ -464,7 +474,7 @@ test("treats agent variant as model-scoped setting (not provider option)", async
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const agent = config.agent?.["test_agent"] const agent = config.agent?.["test_agent"]
expect(agent?.variant).toBe("xhigh") expect(agent?.variant).toBe("xhigh")
@@ -494,7 +504,7 @@ test("handles command configuration", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.command?.["test_command"]).toEqual({ expect(config.command?.["test_command"]).toEqual({
template: "test template", template: "test template",
description: "test command", description: "test command",
@@ -519,7 +529,7 @@ test("migrates autoshare to share field", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.share).toBe("auto") expect(config.share).toBe("auto")
expect(config.autoshare).toBe(true) expect(config.autoshare).toBe(true)
}, },
@@ -546,7 +556,7 @@ test("migrates mode field to agent field", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test_mode"]).toEqual({ expect(config.agent?.["test_mode"]).toEqual({
model: "test/model", model: "test/model",
temperature: 0.5, temperature: 0.5,
@@ -578,7 +588,7 @@ Test agent prompt`,
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]).toEqual( expect(config.agent?.["test"]).toEqual(
expect.objectContaining({ expect.objectContaining({
name: "test", name: "test",
@@ -622,7 +632,7 @@ Nested agent prompt`,
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["helper"]).toMatchObject({ expect(config.agent?.["helper"]).toMatchObject({
name: "helper", name: "helper",
@@ -671,7 +681,7 @@ Nested command template`,
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.command?.["hello"]).toEqual({ expect(config.command?.["hello"]).toEqual({
description: "Test command", description: "Test command",
@@ -716,7 +726,7 @@ Nested command template`,
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.command?.["hello"]).toEqual({ expect(config.command?.["hello"]).toEqual({
description: "Test command", description: "Test command",
@@ -737,7 +747,7 @@ test("updates config and writes to file", async () => {
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const newConfig = { model: "updated/model" } const newConfig = { model: "updated/model" }
await Config.update(newConfig as any) await save(newConfig as any)
const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json")) const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json"))
expect(writtenConfig.model).toBe("updated/model") expect(writtenConfig.model).toBe("updated/model")
@@ -750,7 +760,7 @@ test("gets config directories", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const dirs = await Config.directories() const dirs = await listDirs()
expect(dirs.length).toBeGreaterThanOrEqual(1) expect(dirs.length).toBeGreaterThanOrEqual(1)
}, },
}) })
@@ -780,7 +790,7 @@ test("does not try to install dependencies in read-only OPENCODE_CONFIG_DIR", as
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
await Config.get() await load()
}, },
}) })
} finally { } finally {
@@ -814,8 +824,8 @@ test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
await Config.get() await load()
await Config.waitForDependencies() await ready()
}, },
}) })
@@ -996,7 +1006,7 @@ test("resolves scoped npm plugins in config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const pluginEntries = config.plugin ?? [] const pluginEntries = config.plugin ?? []
expect(pluginEntries).toContain("@scope/plugin") expect(pluginEntries).toContain("@scope/plugin")
}, },
@@ -1034,7 +1044,7 @@ test("merges plugin arrays from global and local configs", async () => {
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const plugins = config.plugin ?? [] const plugins = config.plugin ?? []
// Should contain both global and local plugins // Should contain both global and local plugins
@@ -1070,7 +1080,7 @@ Helper subagent prompt`,
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["helper"]).toMatchObject({ expect(config.agent?.["helper"]).toMatchObject({
name: "helper", name: "helper",
model: "test/model", model: "test/model",
@@ -1109,7 +1119,7 @@ test("merges instructions arrays from global and local configs", async () => {
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const instructions = config.instructions ?? [] const instructions = config.instructions ?? []
expect(instructions).toContain("global-instructions.md") expect(instructions).toContain("global-instructions.md")
@@ -1148,7 +1158,7 @@ test("deduplicates duplicate instructions from global and local configs", async
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const instructions = config.instructions ?? [] const instructions = config.instructions ?? []
expect(instructions).toContain("global-only.md") expect(instructions).toContain("global-only.md")
@@ -1193,7 +1203,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const plugins = config.plugin ?? [] const plugins = config.plugin ?? []
// Should contain all unique plugins // Should contain all unique plugins
@@ -1242,7 +1252,7 @@ test("keeps plugin origins aligned with merged plugin list", async () => {
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const cfg = await Config.get() const cfg = await load()
const plugins = cfg.plugin ?? [] const plugins = cfg.plugin ?? []
const origins = cfg.plugin_origins ?? [] const origins = cfg.plugin_origins ?? []
const names = plugins.map((item) => Config.pluginSpecifier(item)) const names = plugins.map((item) => Config.pluginSpecifier(item))
@@ -1283,7 +1293,7 @@ test("migrates legacy tools config to permissions - allow", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
bash: "allow", bash: "allow",
read: "allow", read: "allow",
@@ -1314,7 +1324,7 @@ test("migrates legacy tools config to permissions - deny", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
bash: "deny", bash: "deny",
webfetch: "deny", webfetch: "deny",
@@ -1344,7 +1354,7 @@ test("migrates legacy write tool to edit permission", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
edit: "allow", edit: "allow",
}) })
@@ -1376,7 +1386,7 @@ test("managed settings override user settings", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("managed/model") expect(config.model).toBe("managed/model")
expect(config.share).toBe("disabled") expect(config.share).toBe("disabled")
expect(config.username).toBe("testuser") expect(config.username).toBe("testuser")
@@ -1404,7 +1414,7 @@ test("managed settings override project settings", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.autoupdate).toBe(false) expect(config.autoupdate).toBe(false)
expect(config.disabled_providers).toEqual(["openai"]) expect(config.disabled_providers).toEqual(["openai"])
}, },
@@ -1424,7 +1434,7 @@ test("missing managed settings file is not an error", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.model).toBe("user/model") expect(config.model).toBe("user/model")
}, },
}) })
@@ -1451,7 +1461,7 @@ test("migrates legacy edit tool to edit permission", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
edit: "deny", edit: "deny",
}) })
@@ -1480,7 +1490,7 @@ test("migrates legacy patch tool to edit permission", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
edit: "allow", edit: "allow",
}) })
@@ -1509,7 +1519,7 @@ test("migrates legacy multiedit tool to edit permission", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
edit: "deny", edit: "deny",
}) })
@@ -1541,7 +1551,7 @@ test("migrates mixed legacy tools config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
bash: "allow", bash: "allow",
edit: "allow", edit: "allow",
@@ -1576,7 +1586,7 @@ test("merges legacy tools with existing permission config", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.agent?.["test"]?.permission).toEqual({ expect(config.agent?.["test"]?.permission).toEqual({
glob: "allow", glob: "allow",
bash: "allow", bash: "allow",
@@ -1611,7 +1621,7 @@ test("permission config preserves key order", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(Object.keys(config.permission!)).toEqual([ expect(Object.keys(config.permission!)).toEqual([
"*", "*",
"edit", "edit",
@@ -1671,7 +1681,7 @@ test("project config can override MCP server enabled status", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
// jira should be enabled (overridden by project config) // jira should be enabled (overridden by project config)
expect(config.mcp?.jira).toEqual({ expect(config.mcp?.jira).toEqual({
type: "remote", type: "remote",
@@ -1727,7 +1737,7 @@ test("MCP config deep merges preserving base config properties", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.mcp?.myserver).toEqual({ expect(config.mcp?.myserver).toEqual({
type: "remote", type: "remote",
url: "https://myserver.example.com/mcp", url: "https://myserver.example.com/mcp",
@@ -1778,7 +1788,7 @@ test("local .opencode config can override MCP from project config", async () =>
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.mcp?.docs?.enabled).toBe(true) expect(config.mcp?.docs?.enabled).toBe(true)
}, },
}) })
@@ -2029,7 +2039,7 @@ describe("deduplicatePluginOrigins", () => {
await Instance.provide({ await Instance.provide({
directory: path.join(tmp.path, "project"), directory: path.join(tmp.path, "project"),
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const plugins = config.plugin ?? [] const plugins = config.plugin ?? []
expect(plugins.some((p) => Config.pluginSpecifier(p) === "my-plugin@1.0.0")).toBe(true) expect(plugins.some((p) => Config.pluginSpecifier(p) === "my-plugin@1.0.0")).toBe(true)
@@ -2061,7 +2071,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
// Project config should NOT be loaded - model should be default, not "project/model" // Project config should NOT be loaded - model should be default, not "project/model"
expect(config.model).not.toBe("project/model") expect(config.model).not.toBe("project/model")
expect(config.username).not.toBe("project-user") expect(config.username).not.toBe("project-user")
@@ -2092,7 +2102,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const directories = await Config.directories() const directories = await listDirs()
// Project .opencode should NOT be in directories list // Project .opencode should NOT be in directories list
const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path)) const hasProjectOpencode = directories.some((d) => d.startsWith(tmp.path))
expect(hasProjectOpencode).toBe(false) expect(hasProjectOpencode).toBe(false)
@@ -2117,7 +2127,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
// Should still get default config (from global or defaults) // Should still get default config (from global or defaults)
const config = await Config.get() const config = await load()
expect(config).toBeDefined() expect(config).toBeDefined()
expect(config.username).toBeDefined() expect(config.username).toBeDefined()
}, },
@@ -2160,7 +2170,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
fn: async () => { fn: async () => {
// The relative instruction should be skipped without error // The relative instruction should be skipped without error
// We're mainly verifying this doesn't throw and the config loads // We're mainly verifying this doesn't throw and the config loads
const config = await Config.get() const config = await load()
expect(config).toBeDefined() expect(config).toBeDefined()
// The instruction should have been skipped (warning logged) // The instruction should have been skipped (warning logged)
// We can't easily test the warning was logged, but we verify // We can't easily test the warning was logged, but we verify
@@ -2218,7 +2228,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await Instance.provide({ await Instance.provide({
directory: projectTmp.path, directory: projectTmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
// Should load from OPENCODE_CONFIG_DIR, not project // Should load from OPENCODE_CONFIG_DIR, not project
expect(config.model).toBe("configdir/model") expect(config.model).toBe("configdir/model")
}, },
@@ -2253,7 +2263,7 @@ describe("OPENCODE_CONFIG_CONTENT token substitution", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("test_api_key_12345") expect(config.username).toBe("test_api_key_12345")
}, },
}) })
@@ -2287,7 +2297,7 @@ describe("OPENCODE_CONFIG_CONTENT token substitution", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
expect(config.username).toBe("secret_key_from_file") expect(config.username).toBe("secret_key_from_file")
}, },
}) })
+6 -3
View File
@@ -7,12 +7,15 @@ import { Config } from "../../src/config/config"
import { TuiConfig } from "../../src/config/tui" import { TuiConfig } from "../../src/config/tui"
import { Global } from "../../src/global" import { Global } from "../../src/global"
import { Filesystem } from "../../src/util/filesystem" import { Filesystem } from "../../src/util/filesystem"
import { AppRuntime } from "../../src/effect/app-runtime"
const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR! const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
const wintest = process.platform === "win32" ? test : test.skip const wintest = process.platform === "win32" ? test : test.skip
const clear = (wait = false) => AppRuntime.runPromise(Config.Service.use((svc) => svc.invalidate(wait)))
const load = () => AppRuntime.runPromise(Config.Service.use((svc) => svc.get()))
beforeEach(async () => { beforeEach(async () => {
await Config.invalidate(true) await clear(true)
}) })
afterEach(async () => { afterEach(async () => {
@@ -23,7 +26,7 @@ afterEach(async () => {
await fs.rm(path.join(Global.Path.config, "tui.json"), { force: true }).catch(() => {}) await fs.rm(path.join(Global.Path.config, "tui.json"), { force: true }).catch(() => {})
await fs.rm(path.join(Global.Path.config, "tui.jsonc"), { force: true }).catch(() => {}) await fs.rm(path.join(Global.Path.config, "tui.jsonc"), { force: true }).catch(() => {})
await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {}) await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {})
await Config.invalidate(true) await clear(true)
}) })
test("keeps server and tui plugin merge semantics aligned", async () => { test("keeps server and tui plugin merge semantics aligned", async () => {
@@ -79,7 +82,7 @@ test("keeps server and tui plugin merge semantics aligned", async () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const server = await Config.get() const server = await load()
const tui = await TuiConfig.get() const tui = await TuiConfig.get()
const serverPlugins = (server.plugin ?? []).map((item) => Config.pluginSpecifier(item)) const serverPlugins = (server.plugin ?? []).map((item) => Config.pluginSpecifier(item))
const tuiPlugins = (tui.plugin ?? []).map((item) => Config.pluginSpecifier(item)) const tuiPlugins = (tui.plugin ?? []).map((item) => Config.pluginSpecifier(item))
@@ -0,0 +1,71 @@
import { describe, expect, test } from "bun:test"
import { getAdaptor, registerAdaptor } from "../../src/control-plane/adaptors"
import { ProjectID } from "../../src/project/schema"
import type { WorkspaceInfo } from "../../src/control-plane/types"
function info(projectID: WorkspaceInfo["projectID"], type: string): WorkspaceInfo {
return {
id: "workspace-test" as WorkspaceInfo["id"],
type,
name: "workspace-test",
branch: null,
directory: null,
extra: null,
projectID,
}
}
function adaptor(dir: string) {
return {
name: dir,
description: dir,
configure(input: WorkspaceInfo) {
return input
},
async create() {},
async remove() {},
target() {
return {
type: "local" as const,
directory: dir,
}
},
}
}
describe("control-plane/adaptors", () => {
test("isolates custom adaptors by project", async () => {
const type = `demo-${Math.random().toString(36).slice(2)}`
const one = ProjectID.make(`project-${Math.random().toString(36).slice(2)}`)
const two = ProjectID.make(`project-${Math.random().toString(36).slice(2)}`)
registerAdaptor(one, type, adaptor("/one"))
registerAdaptor(two, type, adaptor("/two"))
expect(await (await getAdaptor(one, type)).target(info(one, type))).toEqual({
type: "local",
directory: "/one",
})
expect(await (await getAdaptor(two, type)).target(info(two, type))).toEqual({
type: "local",
directory: "/two",
})
})
test("latest install wins within a project", async () => {
const type = `demo-${Math.random().toString(36).slice(2)}`
const id = ProjectID.make(`project-${Math.random().toString(36).slice(2)}`)
registerAdaptor(id, type, adaptor("/one"))
expect(await (await getAdaptor(id, type)).target(info(id, type))).toEqual({
type: "local",
directory: "/one",
})
registerAdaptor(id, type, adaptor("/two"))
expect(await (await getAdaptor(id, type)).target(info(id, type))).toEqual({
type: "local",
directory: "/two",
})
})
})
+31 -6
View File
@@ -1,4 +1,6 @@
import { test, expect, mock, beforeEach } from "bun:test" import { test, expect, mock, beforeEach } from "bun:test"
import { Effect } from "effect"
import type { MCP as MCPNS } from "../../src/mcp/index"
// Track what options were passed to each transport constructor // Track what options were passed to each transport constructor
const transportCalls: Array<{ const transportCalls: Array<{
@@ -44,8 +46,10 @@ beforeEach(() => {
// Import MCP after mocking // Import MCP after mocking
const { MCP } = await import("../../src/mcp/index") const { MCP } = await import("../../src/mcp/index")
const { AppRuntime } = await import("../../src/effect/app-runtime")
const { Instance } = await import("../../src/project/instance") const { Instance } = await import("../../src/project/instance")
const { tmpdir } = await import("../fixture/fixture") const { tmpdir } = await import("../fixture/fixture")
const service = MCP.Service as unknown as Effect.Effect<MCPNS.Interface, never, never>
test("headers are passed to transports when oauth is enabled (default)", async () => { test("headers are passed to transports when oauth is enabled (default)", async () => {
await using tmp = await tmpdir({ await using tmp = await tmpdir({
@@ -73,14 +77,21 @@ test("headers are passed to transports when oauth is enabled (default)", async (
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
// Trigger MCP initialization - it will fail to connect but we can check the transport options // Trigger MCP initialization - it will fail to connect but we can check the transport options
await MCP.add("test-server", { await AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
yield* mcp
.add("test-server", {
type: "remote", type: "remote",
url: "https://example.com/mcp", url: "https://example.com/mcp",
headers: { headers: {
Authorization: "Bearer test-token", Authorization: "Bearer test-token",
"X-Custom-Header": "custom-value", "X-Custom-Header": "custom-value",
}, },
}).catch(() => {}) })
.pipe(Effect.catch(() => Effect.void))
}),
)
// Both transports should have been created with headers // Both transports should have been created with headers
expect(transportCalls.length).toBeGreaterThanOrEqual(1) expect(transportCalls.length).toBeGreaterThanOrEqual(1)
@@ -106,14 +117,21 @@ test("headers are passed to transports when oauth is explicitly disabled", async
fn: async () => { fn: async () => {
transportCalls.length = 0 transportCalls.length = 0
await MCP.add("test-server-no-oauth", { await AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
yield* mcp
.add("test-server-no-oauth", {
type: "remote", type: "remote",
url: "https://example.com/mcp", url: "https://example.com/mcp",
oauth: false, oauth: false,
headers: { headers: {
Authorization: "Bearer test-token", Authorization: "Bearer test-token",
}, },
}).catch(() => {}) })
.pipe(Effect.catch(() => Effect.void))
}),
)
expect(transportCalls.length).toBeGreaterThanOrEqual(1) expect(transportCalls.length).toBeGreaterThanOrEqual(1)
@@ -137,10 +155,17 @@ test("no requestInit when headers are not provided", async () => {
fn: async () => { fn: async () => {
transportCalls.length = 0 transportCalls.length = 0
await MCP.add("test-server-no-headers", { await AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
yield* mcp
.add("test-server-no-headers", {
type: "remote", type: "remote",
url: "https://example.com/mcp", url: "https://example.com/mcp",
}).catch(() => {}) })
.pipe(Effect.catch(() => Effect.void))
}),
)
expect(transportCalls.length).toBeGreaterThanOrEqual(1) expect(transportCalls.length).toBeGreaterThanOrEqual(1)
+109 -75
View File
@@ -1,4 +1,6 @@
import { test, expect, mock, beforeEach } from "bun:test" import { test, expect, mock, beforeEach } from "bun:test"
import { Effect } from "effect"
import type { MCP as MCPNS } from "../../src/mcp/index"
// --- Mock infrastructure --- // --- Mock infrastructure ---
@@ -170,7 +172,10 @@ const { tmpdir } = await import("../fixture/fixture")
// --- Helper --- // --- Helper ---
function withInstance(config: Record<string, any>, fn: () => Promise<void>) { function withInstance(
config: Record<string, unknown>,
fn: (mcp: MCPNS.Interface) => Effect.Effect<void, unknown, never>,
) {
return async () => { return async () => {
await using tmp = await tmpdir({ await using tmp = await tmpdir({
init: async (dir) => { init: async (dir) => {
@@ -187,7 +192,7 @@ function withInstance(config: Record<string, any>, fn: () => Promise<void>) {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
await fn() await Effect.runPromise(MCP.Service.use(fn).pipe(Effect.provide(MCP.defaultLayer)))
// dispose instance to clean up state between tests // dispose instance to clean up state between tests
await Instance.dispose() await Instance.dispose()
}, },
@@ -201,7 +206,8 @@ function withInstance(config: Record<string, any>, fn: () => Promise<void>) {
test( test(
"tools() reuses cached tool definitions after connect", "tools() reuses cached tool definitions after connect",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "my-server" lastCreatedClientName = "my-server"
const serverState = getOrCreateClientState("my-server") const serverState = getOrCreateClientState("my-server")
serverState.tools = [ serverState.tools = [
@@ -209,7 +215,7 @@ test(
] ]
// First: add the server successfully // First: add the server successfully
const addResult = await MCP.add("my-server", { const addResult = yield* mcp.add("my-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
@@ -217,12 +223,13 @@ test(
expect(serverState.listToolsCalls).toBe(1) expect(serverState.listToolsCalls).toBe(1)
const toolsA = await MCP.tools() const toolsA = yield* mcp.tools()
const toolsB = await MCP.tools() const toolsB = yield* mcp.tools()
expect(Object.keys(toolsA).length).toBeGreaterThan(0) expect(Object.keys(toolsA).length).toBeGreaterThan(0)
expect(Object.keys(toolsB).length).toBeGreaterThan(0) expect(Object.keys(toolsB).length).toBeGreaterThan(0)
expect(serverState.listToolsCalls).toBe(1) expect(serverState.listToolsCalls).toBe(1)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -231,16 +238,17 @@ test(
test( test(
"tool change notifications refresh cached tool definitions", "tool change notifications refresh cached tool definitions",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "status-server" lastCreatedClientName = "status-server"
const serverState = getOrCreateClientState("status-server") const serverState = getOrCreateClientState("status-server")
await MCP.add("status-server", { yield* mcp.add("status-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const before = await MCP.tools() const before = yield* mcp.tools()
expect(Object.keys(before).some((key) => key.includes("test_tool"))).toBe(true) expect(Object.keys(before).some((key) => key.includes("test_tool"))).toBe(true)
expect(serverState.listToolsCalls).toBe(1) expect(serverState.listToolsCalls).toBe(1)
@@ -248,13 +256,14 @@ test(
const handler = Array.from(serverState.notificationHandlers.values())[0] const handler = Array.from(serverState.notificationHandlers.values())[0]
expect(handler).toBeDefined() expect(handler).toBeDefined()
await handler?.() yield* Effect.promise(() => handler?.())
const after = await MCP.tools() const after = yield* mcp.tools()
expect(Object.keys(after).some((key) => key.includes("next_tool"))).toBe(true) expect(Object.keys(after).some((key) => key.includes("next_tool"))).toBe(true)
expect(Object.keys(after).some((key) => key.includes("test_tool"))).toBe(false) expect(Object.keys(after).some((key) => key.includes("test_tool"))).toBe(false)
expect(serverState.listToolsCalls).toBe(2) expect(serverState.listToolsCalls).toBe(2)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -270,28 +279,29 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "disc-server" lastCreatedClientName = "disc-server"
getOrCreateClientState("disc-server") getOrCreateClientState("disc-server")
await MCP.add("disc-server", { yield* mcp.add("disc-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const statusBefore = await MCP.status() const statusBefore = yield* mcp.status()
expect(statusBefore["disc-server"]?.status).toBe("connected") expect(statusBefore["disc-server"]?.status).toBe("connected")
await MCP.disconnect("disc-server") yield* mcp.disconnect("disc-server")
const statusAfter = await MCP.status() const statusAfter = yield* mcp.status()
expect(statusAfter["disc-server"]?.status).toBe("disabled") expect(statusAfter["disc-server"]?.status).toBe("disabled")
// Tools should be empty after disconnect // Tools should be empty after disconnect
const tools = await MCP.tools() const tools = yield* mcp.tools()
const serverTools = Object.keys(tools).filter((k) => k.startsWith("disc-server")) const serverTools = Object.keys(tools).filter((k) => k.startsWith("disc-server"))
expect(serverTools.length).toBe(0) expect(serverTools.length).toBe(0)
}, }),
), ),
) )
@@ -304,26 +314,29 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "reconn-server" lastCreatedClientName = "reconn-server"
const serverState = getOrCreateClientState("reconn-server") const serverState = getOrCreateClientState("reconn-server")
serverState.tools = [{ name: "my_tool", description: "a tool", inputSchema: { type: "object", properties: {} } }] serverState.tools = [
{ name: "my_tool", description: "a tool", inputSchema: { type: "object", properties: {} } },
]
await MCP.add("reconn-server", { yield* mcp.add("reconn-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
await MCP.disconnect("reconn-server") yield* mcp.disconnect("reconn-server")
expect((await MCP.status())["reconn-server"]?.status).toBe("disabled") expect((yield* mcp.status())["reconn-server"]?.status).toBe("disabled")
// Reconnect // Reconnect
await MCP.connect("reconn-server") yield* mcp.connect("reconn-server")
expect((await MCP.status())["reconn-server"]?.status).toBe("connected") expect((yield* mcp.status())["reconn-server"]?.status).toBe("connected")
const tools = await MCP.tools() const tools = yield* mcp.tools()
expect(Object.keys(tools).some((k) => k.includes("my_tool"))).toBe(true) expect(Object.keys(tools).some((k) => k.includes("my_tool"))).toBe(true)
}, }),
), ),
) )
@@ -335,11 +348,12 @@ test(
"add() closes the old client when replacing a server", "add() closes the old client when replacing a server",
// Don't put the server in config — add it dynamically so we control // Don't put the server in config — add it dynamically so we control
// exactly which client instance is "first" vs "second". // exactly which client instance is "first" vs "second".
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "replace-server" lastCreatedClientName = "replace-server"
const firstState = getOrCreateClientState("replace-server") const firstState = getOrCreateClientState("replace-server")
await MCP.add("replace-server", { yield* mcp.add("replace-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
@@ -351,7 +365,7 @@ test(
const secondState = getOrCreateClientState("replace-server") const secondState = getOrCreateClientState("replace-server")
// Re-add should close the first client // Re-add should close the first client
await MCP.add("replace-server", { yield* mcp.add("replace-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
@@ -359,6 +373,7 @@ test(
expect(firstState.closed).toBe(true) expect(firstState.closed).toBe(true)
expect(secondState.closed).toBe(false) expect(secondState.closed).toBe(false)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -378,7 +393,8 @@ test(
command: ["echo", "bad"], command: ["echo", "bad"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
// Set up good server // Set up good server
const goodState = getOrCreateClientState("good-server") const goodState = getOrCreateClientState("good-server")
goodState.tools = [{ name: "good_tool", description: "works", inputSchema: { type: "object", properties: {} } }] goodState.tools = [{ name: "good_tool", description: "works", inputSchema: { type: "object", properties: {} } }]
@@ -389,26 +405,26 @@ test(
// Add good server first // Add good server first
lastCreatedClientName = "good-server" lastCreatedClientName = "good-server"
await MCP.add("good-server", { yield* mcp.add("good-server", {
type: "local", type: "local",
command: ["echo", "good"], command: ["echo", "good"],
}) })
// Add bad server - should fail but not affect good server // Add bad server - should fail but not affect good server
lastCreatedClientName = "bad-server" lastCreatedClientName = "bad-server"
await MCP.add("bad-server", { yield* mcp.add("bad-server", {
type: "local", type: "local",
command: ["echo", "bad"], command: ["echo", "bad"],
}) })
const status = await MCP.status() const status = yield* mcp.status()
expect(status["good-server"]?.status).toBe("connected") expect(status["good-server"]?.status).toBe("connected")
expect(status["bad-server"]?.status).toBe("failed") expect(status["bad-server"]?.status).toBe("failed")
// Good server's tools should still be available // Good server's tools should still be available
const tools = await MCP.tools() const tools = yield* mcp.tools()
expect(Object.keys(tools).some((k) => k.includes("good_tool"))).toBe(true) expect(Object.keys(tools).some((k) => k.includes("good_tool"))).toBe(true)
}, }),
), ),
) )
@@ -426,10 +442,11 @@ test(
enabled: false, enabled: false,
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
const countBefore = clientCreateCount const countBefore = clientCreateCount
await MCP.add("disabled-server", { yield* mcp.add("disabled-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
enabled: false, enabled: false,
@@ -438,9 +455,9 @@ test(
// No client should have been created // No client should have been created
expect(clientCreateCount).toBe(countBefore) expect(clientCreateCount).toBe(countBefore)
const status = await MCP.status() const status = yield* mcp.status()
expect(status["disabled-server"]?.status).toBe("disabled") expect(status["disabled-server"]?.status).toBe("disabled")
}, }),
), ),
) )
@@ -457,22 +474,23 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "prompt-server" lastCreatedClientName = "prompt-server"
const serverState = getOrCreateClientState("prompt-server") const serverState = getOrCreateClientState("prompt-server")
serverState.prompts = [{ name: "my-prompt", description: "A test prompt" }] serverState.prompts = [{ name: "my-prompt", description: "A test prompt" }]
await MCP.add("prompt-server", { yield* mcp.add("prompt-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const prompts = await MCP.prompts() const prompts = yield* mcp.prompts()
expect(Object.keys(prompts).length).toBe(1) expect(Object.keys(prompts).length).toBe(1)
const key = Object.keys(prompts)[0] const key = Object.keys(prompts)[0]
expect(key).toContain("prompt-server") expect(key).toContain("prompt-server")
expect(key).toContain("my-prompt") expect(key).toContain("my-prompt")
}, }),
), ),
) )
@@ -485,22 +503,23 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "resource-server" lastCreatedClientName = "resource-server"
const serverState = getOrCreateClientState("resource-server") const serverState = getOrCreateClientState("resource-server")
serverState.resources = [{ name: "my-resource", uri: "file:///test.txt", description: "A test resource" }] serverState.resources = [{ name: "my-resource", uri: "file:///test.txt", description: "A test resource" }]
await MCP.add("resource-server", { yield* mcp.add("resource-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const resources = await MCP.resources() const resources = yield* mcp.resources()
expect(Object.keys(resources).length).toBe(1) expect(Object.keys(resources).length).toBe(1)
const key = Object.keys(resources)[0] const key = Object.keys(resources)[0]
expect(key).toContain("resource-server") expect(key).toContain("resource-server")
expect(key).toContain("my-resource") expect(key).toContain("my-resource")
}, }),
), ),
) )
@@ -513,21 +532,22 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "prompt-disc-server" lastCreatedClientName = "prompt-disc-server"
const serverState = getOrCreateClientState("prompt-disc-server") const serverState = getOrCreateClientState("prompt-disc-server")
serverState.prompts = [{ name: "hidden-prompt", description: "Should not appear" }] serverState.prompts = [{ name: "hidden-prompt", description: "Should not appear" }]
await MCP.add("prompt-disc-server", { yield* mcp.add("prompt-disc-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
await MCP.disconnect("prompt-disc-server") yield* mcp.disconnect("prompt-disc-server")
const prompts = await MCP.prompts() const prompts = yield* mcp.prompts()
expect(Object.keys(prompts).length).toBe(0) expect(Object.keys(prompts).length).toBe(0)
}, }),
), ),
) )
@@ -537,12 +557,14 @@ test(
test( test(
"connect() on nonexistent server does not throw", "connect() on nonexistent server does not throw",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
// Should not throw // Should not throw
await MCP.connect("nonexistent") yield* mcp.connect("nonexistent")
const status = await MCP.status() const status = yield* mcp.status()
expect(status["nonexistent"]).toBeUndefined() expect(status["nonexistent"]).toBeUndefined()
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -551,10 +573,12 @@ test(
test( test(
"disconnect() on nonexistent server does not throw", "disconnect() on nonexistent server does not throw",
withInstance({}, async () => { withInstance({}, (mcp) =>
await MCP.disconnect("nonexistent") Effect.gen(function* () {
yield* mcp.disconnect("nonexistent")
// Should complete without error // Should complete without error
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -563,10 +587,12 @@ test(
test( test(
"tools() returns empty when no MCP servers are configured", "tools() returns empty when no MCP servers are configured",
withInstance({}, async () => { withInstance({}, (mcp) =>
const tools = await MCP.tools() Effect.gen(function* () {
const tools = yield* mcp.tools()
expect(Object.keys(tools).length).toBe(0) expect(Object.keys(tools).length).toBe(0)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -582,27 +608,28 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "fail-connect" lastCreatedClientName = "fail-connect"
getOrCreateClientState("fail-connect") getOrCreateClientState("fail-connect")
connectShouldFail = true connectShouldFail = true
connectError = "Connection refused" connectError = "Connection refused"
await MCP.add("fail-connect", { yield* mcp.add("fail-connect", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const status = await MCP.status() const status = yield* mcp.status()
expect(status["fail-connect"]?.status).toBe("failed") expect(status["fail-connect"]?.status).toBe("failed")
if (status["fail-connect"]?.status === "failed") { if (status["fail-connect"]?.status === "failed") {
expect(status["fail-connect"].error).toContain("Connection refused") expect(status["fail-connect"].error).toContain("Connection refused")
} }
// No tools should be available // No tools should be available
const tools = await MCP.tools() const tools = yield* mcp.tools()
expect(Object.keys(tools).length).toBe(0) expect(Object.keys(tools).length).toBe(0)
}, }),
), ),
) )
@@ -648,7 +675,8 @@ test(
command: ["echo", "test"], command: ["echo", "test"],
}, },
}, },
async () => { (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "my.special-server" lastCreatedClientName = "my.special-server"
const serverState = getOrCreateClientState("my.special-server") const serverState = getOrCreateClientState("my.special-server")
serverState.tools = [ serverState.tools = [
@@ -656,12 +684,12 @@ test(
{ name: "tool.b", description: "Tool B", inputSchema: { type: "object", properties: {} } }, { name: "tool.b", description: "Tool B", inputSchema: { type: "object", properties: {} } },
] ]
await MCP.add("my.special-server", { yield* mcp.add("my.special-server", {
type: "local", type: "local",
command: ["echo", "test"], command: ["echo", "test"],
}) })
const tools = await MCP.tools() const tools = yield* mcp.tools()
const keys = Object.keys(tools) const keys = Object.keys(tools)
// Server name dots should be replaced with underscores // Server name dots should be replaced with underscores
@@ -669,7 +697,7 @@ test(
// Tool name dots should be replaced with underscores // Tool name dots should be replaced with underscores
expect(keys.some((k) => k.endsWith("tool_b"))).toBe(true) expect(keys.some((k) => k.endsWith("tool_b"))).toBe(true)
expect(keys.length).toBe(2) expect(keys.length).toBe(2)
}, }),
), ),
) )
@@ -679,12 +707,13 @@ test(
test( test(
"local stdio transport is closed when connect times out (no process leak)", "local stdio transport is closed when connect times out (no process leak)",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "hanging-server" lastCreatedClientName = "hanging-server"
getOrCreateClientState("hanging-server") getOrCreateClientState("hanging-server")
connectShouldHang = true connectShouldHang = true
const addResult = await MCP.add("hanging-server", { const addResult = yield* mcp.add("hanging-server", {
type: "local", type: "local",
command: ["node", "fake.js"], command: ["node", "fake.js"],
timeout: 100, timeout: 100,
@@ -696,6 +725,7 @@ test(
// Transport must be closed to avoid orphaned child process // Transport must be closed to avoid orphaned child process
expect(transportCloseCount).toBeGreaterThanOrEqual(1) expect(transportCloseCount).toBeGreaterThanOrEqual(1)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -704,12 +734,13 @@ test(
test( test(
"remote transport is closed when connect times out", "remote transport is closed when connect times out",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "hanging-remote" lastCreatedClientName = "hanging-remote"
getOrCreateClientState("hanging-remote") getOrCreateClientState("hanging-remote")
connectShouldHang = true connectShouldHang = true
const addResult = await MCP.add("hanging-remote", { const addResult = yield* mcp.add("hanging-remote", {
type: "remote", type: "remote",
url: "http://localhost:9999/mcp", url: "http://localhost:9999/mcp",
timeout: 100, timeout: 100,
@@ -721,6 +752,7 @@ test(
// Transport must be closed to avoid leaked HTTP connections // Transport must be closed to avoid leaked HTTP connections
expect(transportCloseCount).toBeGreaterThanOrEqual(1) expect(transportCloseCount).toBeGreaterThanOrEqual(1)
}), }),
),
) )
// ======================================================================== // ========================================================================
@@ -729,13 +761,14 @@ test(
test( test(
"failed remote transport is closed before trying next transport", "failed remote transport is closed before trying next transport",
withInstance({}, async () => { withInstance({}, (mcp) =>
Effect.gen(function* () {
lastCreatedClientName = "fail-remote" lastCreatedClientName = "fail-remote"
getOrCreateClientState("fail-remote") getOrCreateClientState("fail-remote")
connectShouldFail = true connectShouldFail = true
connectError = "Connection refused" connectError = "Connection refused"
const addResult = await MCP.add("fail-remote", { const addResult = yield* mcp.add("fail-remote", {
type: "remote", type: "remote",
url: "http://localhost:9999/mcp", url: "http://localhost:9999/mcp",
timeout: 5000, timeout: 5000,
@@ -747,4 +780,5 @@ test(
// Both StreamableHTTP and SSE transports should be closed // Both StreamableHTTP and SSE transports should be closed
expect(transportCloseCount).toBeGreaterThanOrEqual(2) expect(transportCloseCount).toBeGreaterThanOrEqual(2)
}), }),
),
) )
@@ -1,4 +1,6 @@
import { test, expect, mock, beforeEach } from "bun:test" import { test, expect, mock, beforeEach } from "bun:test"
import { Effect } from "effect"
import type { MCP as MCPNS } from "../../src/mcp/index"
// Mock UnauthorizedError to match the SDK's class // Mock UnauthorizedError to match the SDK's class
class MockUnauthorizedError extends Error { class MockUnauthorizedError extends Error {
@@ -122,10 +124,14 @@ test("first connect to OAuth server shows needs_auth instead of failed", async (
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const result = await MCP.add("test-oauth", { const result = await Effect.runPromise(
MCP.Service.use((mcp) =>
mcp.add("test-oauth", {
type: "remote", type: "remote",
url: "https://example.com/mcp", url: "https://example.com/mcp",
}) }),
).pipe(Effect.provide(MCP.defaultLayer)),
)
const serverStatus = result.status as Record<string, { status: string; error?: string }> const serverStatus = result.status as Record<string, { status: string; error?: string }>
@@ -1,5 +1,7 @@
import { test, expect, mock, beforeEach } from "bun:test" import { test, expect, mock, beforeEach } from "bun:test"
import { EventEmitter } from "events" import { EventEmitter } from "events"
import { Effect } from "effect"
import type { MCP as MCPNS } from "../../src/mcp/index"
// Track open() calls and control failure behavior // Track open() calls and control failure behavior
let openShouldFail = false let openShouldFail = false
@@ -100,10 +102,12 @@ beforeEach(() => {
// Import modules after mocking // Import modules after mocking
const { MCP } = await import("../../src/mcp/index") const { MCP } = await import("../../src/mcp/index")
const { AppRuntime } = await import("../../src/effect/app-runtime")
const { Bus } = await import("../../src/bus") const { Bus } = await import("../../src/bus")
const { McpOAuthCallback } = await import("../../src/mcp/oauth-callback") const { McpOAuthCallback } = await import("../../src/mcp/oauth-callback")
const { Instance } = await import("../../src/project/instance") const { Instance } = await import("../../src/project/instance")
const { tmpdir } = await import("../fixture/fixture") const { tmpdir } = await import("../fixture/fixture")
const service = MCP.Service as unknown as Effect.Effect<MCPNS.Interface, never, never>
test("BrowserOpenFailed event is published when open() throws", async () => { test("BrowserOpenFailed event is published when open() throws", async () => {
await using tmp = await tmpdir({ await using tmp = await tmpdir({
@@ -136,7 +140,12 @@ test("BrowserOpenFailed event is published when open() throws", async () => {
// Run authenticate with a timeout to avoid waiting forever for the callback // Run authenticate with a timeout to avoid waiting forever for the callback
// Attach a handler immediately so callback shutdown rejections // Attach a handler immediately so callback shutdown rejections
// don't show up as unhandled between tests. // don't show up as unhandled between tests.
const authPromise = MCP.authenticate("test-oauth-server").catch(() => undefined) const authPromise = AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
return yield* mcp.authenticate("test-oauth-server")
}),
).catch(() => undefined)
// Config.get() can be slow in tests, so give it plenty of time. // Config.get() can be slow in tests, so give it plenty of time.
await new Promise((resolve) => setTimeout(resolve, 2_000)) await new Promise((resolve) => setTimeout(resolve, 2_000))
@@ -185,7 +194,12 @@ test("BrowserOpenFailed event is NOT published when open() succeeds", async () =
}) })
// Run authenticate with a timeout to avoid waiting forever for the callback // Run authenticate with a timeout to avoid waiting forever for the callback
const authPromise = MCP.authenticate("test-oauth-server-2").catch(() => undefined) const authPromise = AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
return yield* mcp.authenticate("test-oauth-server-2")
}),
).catch(() => undefined)
// Config.get() can be slow in tests; also covers the ~500ms open() error-detection window. // Config.get() can be slow in tests; also covers the ~500ms open() error-detection window.
await new Promise((resolve) => setTimeout(resolve, 2_000)) await new Promise((resolve) => setTimeout(resolve, 2_000))
@@ -230,7 +244,12 @@ test("open() is called with the authorization URL", async () => {
openCalledWith = undefined openCalledWith = undefined
// Run authenticate with a timeout to avoid waiting forever for the callback // Run authenticate with a timeout to avoid waiting forever for the callback
const authPromise = MCP.authenticate("test-oauth-server-3").catch(() => undefined) const authPromise = AppRuntime.runPromise(
Effect.gen(function* () {
const mcp = yield* service
return yield* mcp.authenticate("test-oauth-server-3")
}),
).catch(() => undefined)
// Config.get() can be slow in tests; also covers the ~500ms open() error-detection window. // Config.get() can be slow in tests; also covers the ~500ms open() error-detection window.
await new Promise((resolve) => setTimeout(resolve, 2_000)) await new Promise((resolve) => setTimeout(resolve, 2_000))
@@ -3,6 +3,9 @@ import { Permission } from "../src/permission"
import { Config } from "../src/config/config" import { Config } from "../src/config/config"
import { Instance } from "../src/project/instance" import { Instance } from "../src/project/instance"
import { tmpdir } from "./fixture/fixture" import { tmpdir } from "./fixture/fixture"
import { AppRuntime } from "../src/effect/app-runtime"
const load = () => AppRuntime.runPromise(Config.Service.use((svc) => svc.get()))
afterEach(async () => { afterEach(async () => {
await Instance.disposeAll() await Instance.disposeAll()
@@ -158,7 +161,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
// general and orchestrator-fast should be allowed, code-reviewer denied // general and orchestrator-fast should be allowed, code-reviewer denied
expect(Permission.evaluate("task", "general", ruleset).action).toBe("allow") expect(Permission.evaluate("task", "general", ruleset).action).toBe("allow")
@@ -183,7 +186,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
// general and code-reviewer should be ask, orchestrator-* denied // general and code-reviewer should be ask, orchestrator-* denied
expect(Permission.evaluate("task", "general", ruleset).action).toBe("ask") expect(Permission.evaluate("task", "general", ruleset).action).toBe("ask")
@@ -208,7 +211,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
expect(Permission.evaluate("task", "general", ruleset).action).toBe("allow") expect(Permission.evaluate("task", "general", ruleset).action).toBe("allow")
expect(Permission.evaluate("task", "code-reviewer", ruleset).action).toBe("deny") expect(Permission.evaluate("task", "code-reviewer", ruleset).action).toBe("deny")
@@ -235,7 +238,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
// Verify task permissions // Verify task permissions
@@ -273,7 +276,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
// Last matching rule wins - "*" deny is last, so all agents are denied // Last matching rule wins - "*" deny is last, so all agents are denied
@@ -304,7 +307,7 @@ describe("permission.task with real config files", () => {
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const config = await Config.get() const config = await load()
const ruleset = Permission.fromConfig(config.permission ?? {}) const ruleset = Permission.fromConfig(config.permission ?? {})
// Evaluate uses findLast - "general" allow comes after "*" deny // Evaluate uses findLast - "general" allow comes after "*" deny
@@ -125,6 +125,9 @@ test("remaps fallback oauth model urls to the enterprise host", async () => {
project: {} as never, project: {} as never,
directory: "", directory: "",
worktree: "", worktree: "",
experimental_workspace: {
register() {},
},
serverUrl: new URL("https://example.com"), serverUrl: new URL("https://example.com"),
$: {} as never, $: {} as never,
}) })
@@ -0,0 +1,99 @@
import { afterAll, afterEach, describe, expect, test } from "bun:test"
import path from "path"
import { pathToFileURL } from "url"
import { tmpdir } from "../fixture/fixture"
const disableDefault = process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS
process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = "1"
const { Plugin } = await import("../../src/plugin/index")
const { Workspace } = await import("../../src/control-plane/workspace")
const { Instance } = await import("../../src/project/instance")
afterEach(async () => {
await Instance.disposeAll()
})
afterAll(() => {
if (disableDefault === undefined) {
delete process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS
return
}
process.env.OPENCODE_DISABLE_DEFAULT_PLUGINS = disableDefault
})
describe("plugin.workspace", () => {
test("plugin can install a workspace adaptor", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
const type = `plug-${Math.random().toString(36).slice(2)}`
const file = path.join(dir, "plugin.ts")
const mark = path.join(dir, "created.json")
const space = path.join(dir, "space")
await Bun.write(
file,
[
"export default async ({ experimental_workspace }) => {",
` experimental_workspace.register(${JSON.stringify(type)}, {`,
' name: "plug",',
' description: "plugin workspace adaptor",',
" configure(input) {",
` return { ...input, name: \"plug\", branch: \"plug/main\", directory: ${JSON.stringify(space)} }`,
" },",
" async create(input) {",
` await Bun.write(${JSON.stringify(mark)}, JSON.stringify(input))`,
" },",
" async remove() {},",
" target(input) {",
' return { type: "local", directory: input.directory }',
" },",
" })",
" return {}",
"}",
"",
].join("\n"),
)
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify(
{
$schema: "https://opencode.ai/config.json",
plugin: [pathToFileURL(file).href],
},
null,
2,
),
)
return { mark, space, type }
},
})
const info = await Instance.provide({
directory: tmp.path,
fn: async () => {
await Plugin.init()
return Workspace.create({
type: tmp.extra.type,
branch: null,
extra: { key: "value" },
projectID: Instance.project.id,
})
},
})
expect(info.type).toBe(tmp.extra.type)
expect(info.name).toBe("plug")
expect(info.branch).toBe("plug/main")
expect(info.directory).toBe(tmp.extra.space)
expect(info.extra).toEqual({ key: "value" })
expect(JSON.parse(await Bun.file(tmp.extra.mark).text())).toMatchObject({
type: tmp.extra.type,
name: "plug",
branch: "plug/main",
directory: tmp.extra.space,
extra: { key: "value" },
})
})
})
+34
View File
@@ -0,0 +1,34 @@
import type { Plugin } from "@opencode-ai/plugin"
import { mkdir, rm } from "node:fs/promises"
export const FolderWorkspacePlugin: Plugin = async ({ experimental_workspace }) => {
experimental_workspace.register("folder", {
name: "Folder",
description: "Create a blank folder",
configure(config) {
const rand = "" + Math.random()
return {
...config,
directory: `/tmp/folder/folder-${rand}`,
}
},
async create(config) {
if (!config.directory) return
await mkdir(config.directory, { recursive: true })
},
async remove(config) {
await rm(config.directory!, { recursive: true, force: true })
},
target(config) {
return {
type: "local",
directory: config.directory!,
}
},
})
return {}
}
export default FolderWorkspacePlugin
+33
View File
@@ -24,11 +24,44 @@ export type ProviderContext = {
options: Record<string, any> options: Record<string, any>
} }
export type WorkspaceInfo = {
id: string
type: string
name: string
branch: string | null
directory: string | null
extra: unknown | null
projectID: string
}
export type WorkspaceTarget =
| {
type: "local"
directory: string
}
| {
type: "remote"
url: string | URL
headers?: HeadersInit
}
export type WorkspaceAdaptor = {
name: string
description: string
configure(config: WorkspaceInfo): WorkspaceInfo | Promise<WorkspaceInfo>
create(config: WorkspaceInfo, from?: WorkspaceInfo): Promise<void>
remove(config: WorkspaceInfo): Promise<void>
target(config: WorkspaceInfo): WorkspaceTarget | Promise<WorkspaceTarget>
}
export type PluginInput = { export type PluginInput = {
client: ReturnType<typeof createOpencodeClient> client: ReturnType<typeof createOpencodeClient>
project: Project project: Project
directory: string directory: string
worktree: string worktree: string
experimental_workspace: {
register(type: string, adaptor: WorkspaceAdaptor): void
}
serverUrl: URL serverUrl: URL
$: BunShell $: BunShell
} }
+1
View File
@@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig.json", "$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "@tsconfig/node22/tsconfig.json", "extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": { "compilerOptions": {
"rootDir": "src",
"outDir": "dist", "outDir": "dist",
"module": "nodenext", "module": "nodenext",
"declaration": true, "declaration": true,
+38
View File
@@ -29,6 +29,7 @@ import type {
ExperimentalConsoleSwitchOrgResponses, ExperimentalConsoleSwitchOrgResponses,
ExperimentalResourceListResponses, ExperimentalResourceListResponses,
ExperimentalSessionListResponses, ExperimentalSessionListResponses,
ExperimentalWorkspaceAdaptorListResponses,
ExperimentalWorkspaceCreateErrors, ExperimentalWorkspaceCreateErrors,
ExperimentalWorkspaceCreateResponses, ExperimentalWorkspaceCreateResponses,
ExperimentalWorkspaceListResponses, ExperimentalWorkspaceListResponses,
@@ -1086,6 +1087,38 @@ export class Console extends HeyApiClient {
} }
} }
export class Adaptor extends HeyApiClient {
/**
* List workspace adaptors
*
* List all available workspace adaptors for the current project.
*/
public list<ThrowOnError extends boolean = false>(
parameters?: {
directory?: string
workspace?: string
},
options?: Options<never, ThrowOnError>,
) {
const params = buildClientParams(
[parameters],
[
{
args: [
{ in: "query", key: "directory" },
{ in: "query", key: "workspace" },
],
},
],
)
return (options?.client ?? this.client).get<ExperimentalWorkspaceAdaptorListResponses, unknown, ThrowOnError>({
url: "/experimental/workspace/adaptor",
...options,
...params,
})
}
}
export class Workspace extends HeyApiClient { export class Workspace extends HeyApiClient {
/** /**
* List workspaces * List workspaces
@@ -1229,6 +1262,11 @@ export class Workspace extends HeyApiClient {
...params, ...params,
}) })
} }
private _adaptor?: Adaptor
get adaptor(): Adaptor {
return (this._adaptor ??= new Adaptor({ client: this.client }))
}
} }
export class Session extends HeyApiClient { export class Session extends HeyApiClient {
+25 -1
View File
@@ -1772,8 +1772,8 @@ export type ToolList = Array<ToolListItem>
export type Workspace = { export type Workspace = {
id: string id: string
type: string type: string
name: string
branch: string | null branch: string | null
name: string | null
directory: string | null directory: string | null
extra: unknown | null extra: unknown | null
projectID: string projectID: string
@@ -2812,6 +2812,30 @@ export type ToolListResponses = {
export type ToolListResponse = ToolListResponses[keyof ToolListResponses] export type ToolListResponse = ToolListResponses[keyof ToolListResponses]
export type ExperimentalWorkspaceAdaptorListData = {
body?: never
path?: never
query?: {
directory?: string
workspace?: string
}
url: "/experimental/workspace/adaptor"
}
export type ExperimentalWorkspaceAdaptorListResponses = {
/**
* Workspace adaptors
*/
200: Array<{
type: string
name: string
description: string
}>
}
export type ExperimentalWorkspaceAdaptorListResponse =
ExperimentalWorkspaceAdaptorListResponses[keyof ExperimentalWorkspaceAdaptorListResponses]
export type ExperimentalWorkspaceListData = { export type ExperimentalWorkspaceListData = {
body?: never body?: never
path?: never path?: never
+59 -10
View File
@@ -1526,6 +1526,62 @@
] ]
} }
}, },
"/experimental/workspace/adaptor": {
"get": {
"operationId": "experimental.workspace.adaptor.list",
"parameters": [
{
"in": "query",
"name": "directory",
"schema": {
"type": "string"
}
},
{
"in": "query",
"name": "workspace",
"schema": {
"type": "string"
}
}
],
"summary": "List workspace adaptors",
"description": "List all available workspace adaptors for the current project.",
"responses": {
"200": {
"description": "Workspace adaptors",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"required": ["type", "name", "description"]
}
}
}
}
}
},
"x-codeSamples": [
{
"lang": "js",
"source": "import { createOpencodeClient } from \"@opencode-ai/sdk\n\nconst client = createOpencodeClient()\nawait client.experimental.workspace.adaptor.list({\n ...\n})"
}
]
}
},
"/experimental/workspace": { "/experimental/workspace": {
"post": { "post": {
"operationId": "experimental.workspace.create", "operationId": "experimental.workspace.create",
@@ -11885,17 +11941,10 @@
"type": { "type": {
"type": "string" "type": "string"
}, },
"branch": { "name": {
"anyOf": [
{
"type": "string" "type": "string"
}, },
{ "branch": {
"type": "null"
}
]
},
"name": {
"anyOf": [ "anyOf": [
{ {
"type": "string" "type": "string"
@@ -11927,7 +11976,7 @@
"type": "string" "type": "string"
} }
}, },
"required": ["id", "type", "branch", "name", "directory", "extra", "projectID"] "required": ["id", "type", "name", "branch", "directory", "extra", "projectID"]
}, },
"Worktree": { "Worktree": {
"type": "object", "type": "object",