Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .opencode/opencode.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"$schema": "https://opencode.ai/config.json",
"provider": {},
"permission": {},
"reference": {
"effect": "github.com/Effect-TS/effect-smol",
},
"mcp": {},
"tools": {
"github-triage": false,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@
"@aws-sdk/credential-providers": "3.993.0",
"@effect/opentelemetry": "catalog:",
"@effect/platform-node": "catalog:",
"@effect/sql-sqlite-bun": "catalog:",
"@npmcli/arborist": "9.4.0",
"@npmcli/config": "10.8.1",
"@opencode-ai/effect-drizzle-sqlite": "workspace:*",
"@opentelemetry/api": "1.9.0",
"@opentelemetry/context-async-hooks": "2.6.1",
"@opentelemetry/exporter-trace-otlp-http": "0.214.0",
"@opentelemetry/sdk-trace-base": "2.6.1",
"@openrouter/ai-sdk-provider": "2.8.1",
"ai-gateway-provider": "3.1.2",
"cross-spawn": "catalog:",
"drizzle-orm": "catalog:",
"effect": "catalog:",
"gitlab-ai-provider": "6.7.0",
"glob": "13.0.5",
Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/database/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export * as Database from "./database"

import { SqliteClient } from "@effect/sql-sqlite-bun"
import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
import { Context, Effect, Layer } from "effect"
import { Global } from "../global"
import { Flag } from "../flag/flag"
import path from "path"
import { DatabaseMigration } from "./migration"

const makeDatabase = EffectDrizzleSqlite.makeWithDefaults()
type DatabaseShape = Effect.Success<typeof makeDatabase>

export class Service extends Context.Service<Service, DatabaseShape>()("@opencode/v2/storage/Database") {}

const layer = Layer.effect(
Service,
Effect.gen(function* () {
const db = yield* makeDatabase

yield* db.run("PRAGMA journal_mode = WAL")
yield* db.run("PRAGMA synchronous = NORMAL")
yield* db.run("PRAGMA busy_timeout = 5000")
yield* db.run("PRAGMA cache_size = -64000")
yield* db.run("PRAGMA foreign_keys = ON")
yield* db.run("PRAGMA wal_checkpoint(PASSIVE)")

console.log(DatabaseMigration.ensure


return db
}),
)

export function layerFromPath(filename: string) {
return layer.pipe(Layer.provide(SqliteClient.layer({ filename })))
}

export const defaultLayer = Layer.unwrap(
Effect.gen(function* () {
return layerFromPath(
!Flag.OPENCODE_DB
? path.join(Global.Path.data, "opencode.db")
: Flag.OPENCODE_DB === ":memory:" || path.isAbsolute(Flag.OPENCODE_DB)
? Flag.OPENCODE_DB
: path.join(Global.Path.data, Flag.OPENCODE_DB),
)
}),
).pipe(Layer.provide(Global.defaultLayer))
248 changes: 248 additions & 0 deletions packages/core/src/database/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
export * as DatabaseMigration from "./migration"

import type { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite"
import { Effect } from "effect"
import { getTableName, sql, type SQL, type Table } from "drizzle-orm"
import { getTableConfig, type AnySQLiteTable, type Index, type SQLiteColumn } from "drizzle-orm/sqlite-core"

export type SchemaAst = {
tables: Record<string, TableAst>
}

export type TableAst = {
name: string
columns: Record<string, ColumnAst>
indexes: Record<string, IndexAst>
}

export type ColumnAst = {
name: string
type: string
notNull: boolean
primaryKey: boolean
default?: string
}

export type IndexAst = {
name: string
table: string
columns: IndexColumnAst[]
unique: boolean
where?: string
}

export type IndexColumnAst = { type: "column"; name: string } | { type: "expression"; sql: string }

export type Operation =
| { type: "create_table"; table: TableAst }
| { type: "add_column"; table: string; column: ColumnAst }
| { type: "create_index"; index: IndexAst }

export function diff(db: EffectDrizzleSqlite.EffectSQLiteDatabase, tables: Table[]) {
return read(db).pipe(Effect.map((actual) => diffSchema(actual, fromTables(tables))))
}

export function apply(db: EffectDrizzleSqlite.EffectSQLiteDatabase, operations: Operation[]) {
return Effect.forEach(operations, (operation) => db.run(toSql(operation))).pipe(Effect.asVoid)
}

function fromTables(tables: Table[]): SchemaAst {
return {
tables: Object.fromEntries(tables.map((table) => {
const config = getTableConfig(table as AnySQLiteTable)
const name = getTableName(table)
return [name, tableFromConfig(name, config.columns, config.indexes)]
})),
}
}

function diffSchema(actual: SchemaAst, desired: SchemaAst): Operation[] {
return Object.values(desired.tables).flatMap<Operation>((table) => {
const current = actual.tables[table.name]
if (!current) {
return [createTableOperation(table), ...Object.values(table.indexes).map(createIndexOperation)]
}
return [
...Object.values(table.columns)
.filter((column) => current.columns[column.name] === undefined)
.map((column) => addColumnOperation(table.name, column)),
...Object.values(table.indexes)
.filter((index) => current.indexes[index.name] === undefined)
.map(createIndexOperation),
]
})
}

function createTableOperation(table: TableAst): Operation {
return { type: "create_table", table }
}

function addColumnOperation(table: string, column: ColumnAst): Operation {
return { type: "add_column", table, column }
}

function createIndexOperation(index: IndexAst): Operation {
return { type: "create_index", index }
}

function toSql(operation: Operation) {
if (operation.type === "create_table") {
return `CREATE TABLE ${quoteIdentifier(operation.table.name)} (${Object.values(operation.table.columns)
.map((column) => columnSql(column, true))
.join(", ")})`
}
if (operation.type === "add_column") {
return `ALTER TABLE ${quoteIdentifier(operation.table)} ADD COLUMN ${columnSql(operation.column, false)}`
}
return [
"CREATE",
operation.index.unique ? "UNIQUE" : undefined,
"INDEX",
quoteIdentifier(operation.index.name),
"ON",
quoteIdentifier(operation.index.table),
`(${operation.index.columns.map(indexColumnSql).join(", ")})`,
operation.index.where === undefined ? undefined : `WHERE ${operation.index.where}`,
]
.filter((part) => part !== undefined)
.join(" ")
}

function read(db: EffectDrizzleSqlite.EffectSQLiteDatabase) {
return Effect.gen(function* () {
const rows = yield* db.all<{ name: string }>(sql`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`)
const tables = yield* Effect.forEach(rows, (row) => readTable(db, row.name))
return { tables: Object.fromEntries(tables.map((table) => [table.name, table])) }
})
}

function readTable(db: EffectDrizzleSqlite.EffectSQLiteDatabase, name: string) {
return Effect.gen(function* () {
const columns = yield* db.all<{
name: string
type: string
notnull: number
pk: number
dflt_value: string | null
}>(`PRAGMA table_info(${quoteIdentifier(name)})`)
const indexes = yield* db.all<{ name: string; unique: number }>(`PRAGMA index_list(${quoteIdentifier(name)})`)
const indexEntries = yield* Effect.forEach(indexes, (index) =>
Effect.gen(function* () {
const statement = yield* db.get<{ sql: string | null }>(sql`SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ${index.name}`)
if (statement?.sql === null || statement?.sql === undefined) return undefined
const columns = yield* db.all<{ seqno: number; name: string | null }>(`PRAGMA index_info(${quoteIdentifier(index.name)})`)
return [
index.name,
{
name: index.name,
table: name,
columns: columns.map((column) =>
column.name === null
? ({ type: "expression", sql: "" } as const)
: ({ type: "column", name: column.name } as const),
),
unique: index.unique === 1,
},
] as const
}),
)
return {
name,
columns: Object.fromEntries(columns.map((column) => [
column.name,
{
name: column.name,
type: column.type,
notNull: column.notnull === 1,
primaryKey: column.pk > 0,
...(column.dflt_value === null ? {} : { default: column.dflt_value }),
},
])),
indexes: Object.fromEntries(indexEntries.filter((entry) => entry !== undefined)),
}
})
}

function tableFromConfig(name: string, columns: SQLiteColumn[], indexes: Index[]): TableAst {
return {
name,
columns: Object.fromEntries(columns.map((column) => [column.name, columnFromConfig(column)])),
indexes: Object.fromEntries(indexes.map((index) => [index.config.name, indexFromConfig(index)])),
}
}

function columnFromConfig(column: SQLiteColumn): ColumnAst {
return {
name: column.name,
type: column.getSQLType(),
notNull: column.notNull,
primaryKey: column.primary,
...defaultFromColumn(column),
}
}

function defaultFromColumn(column: SQLiteColumn) {
if (column.default !== undefined) return { default: literal(column.default) }
if (column.defaultFn !== undefined) return { default: literal(column.defaultFn()) }
return {}
}

function indexFromConfig(index: Index): IndexAst {
return {
name: index.config.name,
table: getTableName(index.config.table),
columns: index.config.columns.map(indexColumnName),
unique: index.config.unique,
...(index.config.where === undefined ? {} : { where: compileSql(index.config.where) }),
}
}

function indexColumnName(column: SQLiteColumn | SQL) {
if ("name" in column) return { type: "column", name: column.name } as const
return { type: "expression", sql: compileSql(column) } as const
}

function compileSql(value: SQL) {
return value.getSQL().toQuery(new SQLiteCompiler()).sql.replace(/"(?:""|[^"])*"\./g, "")
}

function indexColumnSql(column: IndexColumnAst) {
if (column.type === "column") return quoteIdentifier(column.name)
return column.sql
}

function columnSql(column: ColumnAst, includePrimaryKey: boolean) {
return [
quoteIdentifier(column.name),
column.type,
includePrimaryKey && column.primaryKey ? "PRIMARY KEY" : undefined,
column.notNull ? "NOT NULL" : undefined,
column.default === undefined ? undefined : `DEFAULT ${column.default}`,
]
.filter((part) => part !== undefined)
.join(" ")
}

class SQLiteCompiler {
inlineParams = true
escapeName = (name: string) => {
return quoteIdentifier(name)
}
escapeParam = () => {
return "?"
}
escapeString = (value: string) => {
return `'${value.replaceAll("'", "''")}'`
}
}

function literal(value: unknown) {
if (typeof value === "number") return String(value)
if (typeof value === "boolean") return value ? "1" : "0"
if (value === null) return "NULL"
return `'${String(value).replaceAll("'", "''")}'`
}

function quoteIdentifier(value: string) {
return `"${value.replaceAll('"', '""')}"`
}
3 changes: 2 additions & 1 deletion packages/core/src/location.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Context, Schema } from "effect"
import { AbsolutePath } from "./schema"

export * as Location from "./location"

export const Ref = Schema.Struct({
directory: Schema.String,
directory: AbsolutePath,
workspaceID: Schema.optional(Schema.String),
}).annotate({ identifier: "Location.Ref" })
export type Ref = typeof Ref.Type
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/plugin/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { AccountV2 } from "../account"
import { EventV2 } from "../event"
import { PluginV2 } from "../plugin"

// Depending on what account is active, enable matching providers for that
// service
export const AccountPlugin = PluginV2.define({
id: PluginV2.ID.make("account"),
effect: Effect.gen(function* () {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/plugin/models-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export const ModelsDevPlugin = PluginV2.define({
const catalog = yield* Catalog.Service
const modelsDev = yield* ModelsDev.Service
const events = yield* EventV2.Service
const scope = yield* Scope.Scope
const load = yield* catalog.loader()
const refresh = Effect.fn("ModelsDevPlugin.refresh")(function* () {
const data = yield* modelsDev.get()
Expand Down Expand Up @@ -114,7 +113,7 @@ export const ModelsDevPlugin = PluginV2.define({
yield* refresh()
yield* events.subscribe(ModelsDev.Event.Refreshed).pipe(
Stream.runForEach(() => refresh()),
Effect.forkIn(scope, { startImmediately: true }),
Effect.forkScoped({ startImmediately: true }),
)
}).pipe(Effect.provide(ModelsDev.defaultLayer)),
})
Loading
Loading