mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-27 13:41:34 -04:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 651c23ba1d | |||
| ca318328ea | |||
| cb32c42e6f |
@@ -2,9 +2,9 @@ export * as Database from "./database"
|
||||
|
||||
import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
|
||||
import { layer as sqliteLayer } from "#sqlite"
|
||||
import { Context, Effect, Layer } from "effect"
|
||||
import { Config, Context, Effect, Layer } from "effect"
|
||||
import { Global } from "../global"
|
||||
import { Flag } from "../flag/flag"
|
||||
import { truthyConfig } from "../flag/flag"
|
||||
import { isAbsolute, join } from "path"
|
||||
import { DatabaseMigration } from "./migration"
|
||||
import { InstallationChannel } from "../installation/version"
|
||||
@@ -40,18 +40,28 @@ export function layerFromPath(filename: string) {
|
||||
return layer.pipe(Layer.provide(sqliteLayer({ filename })))
|
||||
}
|
||||
|
||||
export function path() {
|
||||
if (Flag.OPENCODE_DB) {
|
||||
if (Flag.OPENCODE_DB === ":memory:" || isAbsolute(Flag.OPENCODE_DB)) return Flag.OPENCODE_DB
|
||||
return join(Global.Path.data, Flag.OPENCODE_DB)
|
||||
function resolvePath(input: { readonly file: string | undefined; readonly disableChannelDb: boolean }) {
|
||||
if (input.file) {
|
||||
if (input.file === ":memory:" || isAbsolute(input.file)) return input.file
|
||||
return join(Global.Path.data, input.file)
|
||||
}
|
||||
if (
|
||||
["latest", "beta", "prod"].includes(InstallationChannel) ||
|
||||
process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" ||
|
||||
process.env.OPENCODE_DISABLE_CHANNEL_DB === "true"
|
||||
)
|
||||
if (["latest", "beta", "prod"].includes(InstallationChannel) || input.disableChannelDb)
|
||||
return join(Global.Path.data, "opencode.db")
|
||||
return join(Global.Path.data, `opencode-${InstallationChannel.replace(/[^a-zA-Z0-9._-]/g, "-")}.db`)
|
||||
}
|
||||
|
||||
export const node = makeGlobalNode({ service: Service, layer: layerFromPath(path()), deps: [] })
|
||||
/**
|
||||
* The database placement every consumer shares, resolved through Effect
|
||||
* Config so tests and tooling can override it with a ConfigProvider. Used by
|
||||
* the layer below and by external tooling such as `opencode db`.
|
||||
*/
|
||||
export const configuredPath = Effect.gen(function* () {
|
||||
const file = yield* Config.string("OPENCODE_DB").pipe(Config.withDefault(undefined))
|
||||
const disableChannelDb = yield* truthyConfig("OPENCODE_DISABLE_CHANNEL_DB")
|
||||
return resolvePath({ file, disableChannelDb })
|
||||
}).pipe(Effect.orDie)
|
||||
|
||||
// Placement is resolved when the layer is built, not at module import.
|
||||
const configuredLayer = Layer.unwrap(Effect.map(configuredPath, layerFromPath))
|
||||
|
||||
export const node = makeGlobalNode({ service: Service, layer: configuredLayer, deps: [] })
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
import { Config } from "effect"
|
||||
|
||||
export function truthy(key: string) {
|
||||
const value = process.env[key]?.toLowerCase()
|
||||
return value === "true" || value === "1"
|
||||
function truthyValue(value: string) {
|
||||
const lower = value.toLowerCase()
|
||||
return lower === "true" || lower === "1"
|
||||
}
|
||||
|
||||
export function truthy(key: string) {
|
||||
const value = process.env[key]
|
||||
return value !== undefined && truthyValue(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* The `truthy` grammar read through Effect Config, so layer builds can be
|
||||
* steered by a ConfigProvider. Missing or malformed values are false, exactly
|
||||
* like `truthy`; strict boolean decoding would turn a stray env value into a
|
||||
* startup defect.
|
||||
*/
|
||||
export const truthyConfig = (name: string) =>
|
||||
Config.string(name).pipe(Config.map(truthyValue), Config.withDefault(false))
|
||||
|
||||
const copy = process.env["OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"]
|
||||
const fff = process.env["OPENCODE_DISABLE_FFF"]
|
||||
|
||||
@@ -44,7 +58,6 @@ export const Flag = {
|
||||
copy === undefined ? process.platform === "win32" : truthy("OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"),
|
||||
OPENCODE_MODELS_URL: process.env["OPENCODE_MODELS_URL"],
|
||||
OPENCODE_MODELS_PATH: process.env["OPENCODE_MODELS_PATH"],
|
||||
OPENCODE_DB: process.env["OPENCODE_DB"],
|
||||
|
||||
OPENCODE_WORKSPACE_ID: process.env["OPENCODE_WORKSPACE_ID"],
|
||||
OPENCODE_EXPERIMENTAL_WORKSPACES: enabledByExperimental("OPENCODE_EXPERIMENTAL_WORKSPACES"),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
export * as WebSearchTool from "./websearch"
|
||||
|
||||
import { ToolFailure } from "@opencode-ai/llm"
|
||||
import { Context, Duration, Effect, Layer, Schema } from "effect"
|
||||
import { Config, Context, Duration, Effect, Layer, Schema } from "effect"
|
||||
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
|
||||
import { makeLocationNode } from "../effect/app-node"
|
||||
import { LayerNodePlatform } from "../effect/app-node-platform"
|
||||
import { truthy } from "../flag/flag"
|
||||
import { truthyConfig } from "../flag/flag"
|
||||
import { InstallationVersion } from "../installation/version"
|
||||
import { PositiveInt } from "../schema"
|
||||
import { PermissionV2 } from "../permission"
|
||||
@@ -69,19 +69,39 @@ export interface Config {
|
||||
|
||||
export class ConfigService extends Context.Service<ConfigService, Config>()("@opencode/v2/WebSearchConfig") {}
|
||||
|
||||
/** Isolates the retained product environment contract from the generic tool implementation. */
|
||||
export const defaultConfigLayer = Layer.sync(ConfigService, () =>
|
||||
ConfigService.of({
|
||||
provider:
|
||||
process.env.OPENCODE_WEBSEARCH_PROVIDER === "exa" || process.env.OPENCODE_WEBSEARCH_PROVIDER === "parallel"
|
||||
? process.env.OPENCODE_WEBSEARCH_PROVIDER
|
||||
: undefined,
|
||||
enableExa: truthy("OPENCODE_EXPERIMENTAL") || truthy("OPENCODE_ENABLE_EXA") || truthy("OPENCODE_EXPERIMENTAL_EXA"),
|
||||
enableParallel: truthy("OPENCODE_ENABLE_PARALLEL") || truthy("OPENCODE_EXPERIMENTAL_PARALLEL"),
|
||||
exaApiKey: process.env.EXA_API_KEY,
|
||||
parallelApiKey: process.env.PARALLEL_API_KEY,
|
||||
/**
|
||||
* Isolates the retained product environment contract from the generic tool
|
||||
* implementation. Reads through Effect `Config` when the layer is built, so
|
||||
* tests can override values with a `ConfigProvider` instead of mutating
|
||||
* `process.env`. Parsing keeps the legacy lenient grammar: missing or
|
||||
* malformed values disable rather than fail, because this layer builds at
|
||||
* Location boot where a defect would surface as opaque session failures.
|
||||
*/
|
||||
export const defaultConfigLayer = Layer.effect(
|
||||
ConfigService,
|
||||
Effect.gen(function* () {
|
||||
const env = yield* Config.all({
|
||||
provider: Config.string("OPENCODE_WEBSEARCH_PROVIDER").pipe(Config.withDefault(undefined)),
|
||||
experimental: truthyConfig("OPENCODE_EXPERIMENTAL"),
|
||||
enableExa: truthyConfig("OPENCODE_ENABLE_EXA"),
|
||||
experimentalExa: truthyConfig("OPENCODE_EXPERIMENTAL_EXA"),
|
||||
enableParallel: truthyConfig("OPENCODE_ENABLE_PARALLEL"),
|
||||
experimentalParallel: truthyConfig("OPENCODE_EXPERIMENTAL_PARALLEL"),
|
||||
exaApiKey: Config.string("EXA_API_KEY").pipe(Config.withDefault(undefined)),
|
||||
parallelApiKey: Config.string("PARALLEL_API_KEY").pipe(Config.withDefault(undefined)),
|
||||
})
|
||||
const provider = env.provider !== undefined && Schema.is(Provider)(env.provider) ? env.provider : undefined
|
||||
if (env.provider !== undefined && provider === undefined)
|
||||
yield* Effect.logWarning("ignoring invalid OPENCODE_WEBSEARCH_PROVIDER", { value: env.provider })
|
||||
return ConfigService.of({
|
||||
provider,
|
||||
enableExa: env.experimental || env.enableExa || env.experimentalExa,
|
||||
enableParallel: env.enableParallel || env.experimentalParallel,
|
||||
exaApiKey: env.exaApiKey,
|
||||
parallelApiKey: env.parallelApiKey,
|
||||
})
|
||||
}),
|
||||
)
|
||||
).pipe(Layer.orDie)
|
||||
|
||||
export const configNode = makeLocationNode({ service: ConfigService, layer: defaultConfigLayer, deps: [] })
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import path from "path"
|
||||
import { ConfigProvider, Effect, Layer } from "effect"
|
||||
import { Database } from "@opencode-ai/core/database/database"
|
||||
import { Global } from "@opencode-ai/core/global"
|
||||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
import { tmpdir } from "./fixture/tmpdir"
|
||||
|
||||
const resolve = (env: Record<string, string>) =>
|
||||
Effect.runPromise(Effect.provide(Database.configuredPath, ConfigProvider.layer(ConfigProvider.fromUnknown(env))))
|
||||
|
||||
describe("Database placement", () => {
|
||||
test("resolves explicit files, relative names, and the channel default", async () => {
|
||||
expect(await resolve({ OPENCODE_DB: ":memory:" })).toBe(":memory:")
|
||||
expect(await resolve({ OPENCODE_DB: "/tmp/explicit.db" })).toBe("/tmp/explicit.db")
|
||||
expect(await resolve({ OPENCODE_DB: "relative.db" })).toBe(path.join(Global.Path.data, "relative.db"))
|
||||
expect(await resolve({ OPENCODE_DISABLE_CHANNEL_DB: "true" })).toBe(path.join(Global.Path.data, "opencode.db"))
|
||||
})
|
||||
|
||||
test("reads placement from the active ConfigProvider when the layer is built", async () => {
|
||||
await using tmp = await tmpdir()
|
||||
const file = path.join(tmp.path, "config-seam.sqlite")
|
||||
|
||||
// The preload sets OPENCODE_DB=":memory:" in the process environment, so a
|
||||
// database appearing at this path proves the layer reads through the
|
||||
// replaced ConfigProvider rather than the environment snapshot.
|
||||
await Effect.runPromise(
|
||||
Layer.build(
|
||||
LayerNode.compile(LayerNode.group([Database.node])).pipe(
|
||||
Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ OPENCODE_DB: file }))),
|
||||
),
|
||||
).pipe(Effect.scoped, Effect.asVoid),
|
||||
)
|
||||
|
||||
expect(await Bun.file(file).exists()).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { beforeEach, describe, expect, test } from "bun:test"
|
||||
import { Effect, Layer, Schema } from "effect"
|
||||
import { ConfigProvider, Effect, Layer, Schema } from "effect"
|
||||
import { HttpClient, HttpClientResponse } from "effect/unstable/http"
|
||||
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
@@ -47,6 +47,57 @@ describe("WebSearchTool provider selection", () => {
|
||||
})
|
||||
})
|
||||
|
||||
const readDefaultConfig = (env: Record<string, string>) =>
|
||||
Effect.provide(
|
||||
WebSearchTool.ConfigService,
|
||||
WebSearchTool.defaultConfigLayer.pipe(Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown(env)))),
|
||||
)
|
||||
|
||||
describe("WebSearchTool default config", () => {
|
||||
test("decodes an empty environment to defaults", async () => {
|
||||
expect(await Effect.runPromise(readDefaultConfig({}))).toEqual({
|
||||
provider: undefined,
|
||||
enableExa: false,
|
||||
enableParallel: false,
|
||||
exaApiKey: undefined,
|
||||
parallelApiKey: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
test("decodes provider, truthy flags, and credentials from the active ConfigProvider", async () => {
|
||||
expect(
|
||||
await Effect.runPromise(
|
||||
readDefaultConfig({
|
||||
OPENCODE_WEBSEARCH_PROVIDER: "parallel",
|
||||
OPENCODE_ENABLE_EXA: "1",
|
||||
OPENCODE_EXPERIMENTAL_PARALLEL: "true",
|
||||
EXA_API_KEY: "exa-key",
|
||||
PARALLEL_API_KEY: "parallel-key",
|
||||
}),
|
||||
),
|
||||
).toEqual({
|
||||
provider: "parallel",
|
||||
enableExa: true,
|
||||
enableParallel: true,
|
||||
exaApiKey: "exa-key",
|
||||
parallelApiKey: "parallel-key",
|
||||
})
|
||||
})
|
||||
|
||||
test("keeps the legacy lenient truthiness grammar", async () => {
|
||||
const decoded = await Effect.runPromise(
|
||||
readDefaultConfig({ OPENCODE_ENABLE_EXA: "TRUE", OPENCODE_ENABLE_PARALLEL: "banana" }),
|
||||
)
|
||||
expect(decoded.enableExa).toBe(true)
|
||||
expect(decoded.enableParallel).toBe(false)
|
||||
})
|
||||
|
||||
test("ignores an invalid provider instead of failing the Location layer", async () => {
|
||||
const decoded = await Effect.runPromise(readDefaultConfig({ OPENCODE_WEBSEARCH_PROVIDER: "bing" }))
|
||||
expect(decoded.provider).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe("WebSearchTool MCP response parser", () => {
|
||||
test("parses plain JSON-RPC responses", async () => {
|
||||
expect(await Effect.runPromise(WebSearchTool.parseResponse(payload("search results")))).toBe("search results")
|
||||
|
||||
@@ -35,7 +35,7 @@ const QueryCommand = effectCmd({
|
||||
}
|
||||
return
|
||||
}
|
||||
const child = spawn("sqlite3", [Database.path()], {
|
||||
const child = spawn("sqlite3", [yield* Database.configuredPath], {
|
||||
stdio: "inherit",
|
||||
})
|
||||
yield* Effect.promise(() => new Promise((resolve) => child.on("close", resolve)))
|
||||
@@ -47,7 +47,7 @@ const PathCommand = effectCmd({
|
||||
describe: "print the database path",
|
||||
instance: false,
|
||||
handler: Effect.fn("Cli.db.path")(function* () {
|
||||
console.log(Database.path())
|
||||
console.log(yield* Database.configuredPath)
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { rm } from "fs/promises"
|
||||
import { Database } from "@opencode-ai/core/database/database"
|
||||
import { Effect } from "effect"
|
||||
import { disposeAllInstances } from "./fixture"
|
||||
|
||||
export async function resetDatabase() {
|
||||
await disposeAllInstances().catch(() => undefined)
|
||||
const dbPath = Database.path()
|
||||
const dbPath = await Effect.runPromise(Database.configuredPath)
|
||||
await rm(dbPath, { force: true }).catch(() => undefined)
|
||||
await rm(`${dbPath}-wal`, { force: true }).catch(() => undefined)
|
||||
await rm(`${dbPath}-shm`, { force: true }).catch(() => undefined)
|
||||
|
||||
@@ -18,8 +18,10 @@ const preserveExerciseDatabase = !!process.env.OPENCODE_HTTPAPI_EXERCISE_DB
|
||||
export const exerciseDatabasePath =
|
||||
process.env.OPENCODE_HTTPAPI_EXERCISE_DB ??
|
||||
path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-exercise-${process.pid}.db`)
|
||||
// Must run before the first Effect Config read anywhere in the process: the
|
||||
// default ConfigProvider snapshots process.env on first use, so a Config read
|
||||
// during an earlier module import would freeze the wrong database path.
|
||||
process.env.OPENCODE_DB = exerciseDatabasePath
|
||||
Flag.OPENCODE_DB = exerciseDatabasePath
|
||||
|
||||
export const original = {
|
||||
OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD,
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { afterAll, expect, test } from "bun:test"
|
||||
import { mkdtemp, rm } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { Flag } from "@opencode-ai/core/flag/flag"
|
||||
import { Deferred, Effect, Latch, Option, Schema, Stream } from "effect"
|
||||
import type { OpenCodeEvent } from "../src"
|
||||
|
||||
// The database layer resolves OPENCODE_DB through Effect Config, and the
|
||||
// default ConfigProvider snapshots the process environment on first use, so
|
||||
// database placement is process-wide. Point every embedded host in this file
|
||||
// at one shared temporary database before anything builds a runtime.
|
||||
const databaseDirectory = await mkdtemp(join(tmpdir(), "opencode-embedded-db-"))
|
||||
process.env.OPENCODE_DB = join(databaseDirectory, "opencode.sqlite")
|
||||
afterAll(() => rm(databaseDirectory, { recursive: true, force: true }))
|
||||
|
||||
test("embedded client uses the real router and handlers", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-"))
|
||||
const database = Flag.OPENCODE_DB
|
||||
Flag.OPENCODE_DB = join(directory, "opencode.sqlite")
|
||||
const { AbsolutePath, Agent, Location, Model, OpenCode, Prompt, Provider, Session, Tool } = await import("../src")
|
||||
const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`)
|
||||
const model = Model.Ref.make({ id: Model.ID.make("embedded"), providerID: Provider.ID.make("test") })
|
||||
@@ -99,15 +104,12 @@ test("embedded client uses the real router and handlers", async () => {
|
||||
})
|
||||
await Effect.runPromise(Effect.scoped(program))
|
||||
} finally {
|
||||
Flag.OPENCODE_DB = database
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test("Location-owned runner events reach the ready global client", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-events-"))
|
||||
const database = Flag.OPENCODE_DB
|
||||
Flag.OPENCODE_DB = join(directory, "opencode.sqlite")
|
||||
const { AbsolutePath, Location, OpenCode, Prompt, Session } = await import("../src")
|
||||
const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`)
|
||||
|
||||
@@ -138,15 +140,12 @@ test("Location-owned runner events reach the ready global client", async () => {
|
||||
})
|
||||
await Effect.runPromise(Effect.scoped(program))
|
||||
} finally {
|
||||
Flag.OPENCODE_DB = database
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
}, 10_000)
|
||||
|
||||
test("independent embedded hosts do not share live notifications", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-hosts-"))
|
||||
const database = Flag.OPENCODE_DB
|
||||
Flag.OPENCODE_DB = join(directory, "opencode.sqlite")
|
||||
const { AbsolutePath, Agent, Location, OpenCode, Session } = await import("../src")
|
||||
const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`)
|
||||
|
||||
@@ -181,15 +180,12 @@ test("independent embedded hosts do not share live notifications", async () => {
|
||||
})
|
||||
await Effect.runPromise(Effect.scoped(program))
|
||||
} finally {
|
||||
Flag.OPENCODE_DB = database
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
}, 10_000)
|
||||
|
||||
test("embedded client is available as a Layer service", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-layer-"))
|
||||
const database = Flag.OPENCODE_DB
|
||||
Flag.OPENCODE_DB = join(directory, "opencode.sqlite")
|
||||
const { AbsolutePath, Location, OpenCode, Session } = await import("../src")
|
||||
const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`)
|
||||
|
||||
@@ -206,7 +202,6 @@ test("embedded client is available as a Layer service", async () => {
|
||||
|
||||
expect(created.id).toBe(sessionID)
|
||||
} finally {
|
||||
Flag.OPENCODE_DB = database
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user