Skip to content

Commit 526a8ea

Browse files
thdxropencode
andcommitted
Refactor application path handling and data storage architecture
Replace simple directory-based path system with git-aware data management that uses global data directories and proper workspace detection. 🤖 Generated with opencode Co-Authored-By: opencode <noreply@opencode.ai>
1 parent 4be9f7a commit 526a8ea

File tree

17 files changed

+69
-42
lines changed

17 files changed

+69
-42
lines changed

packages/opencode/src/app/app.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import fs from "fs/promises"
2-
import { AppPath } from "./path"
32
import { Log } from "../util/log"
43
import { Context } from "../util/context"
4+
import { Filesystem } from "../util/filesystem"
5+
import { Global } from "../global"
6+
import path from "path"
57

68
export namespace App {
79
const log = Log.create({ service: "app" })
@@ -10,12 +12,13 @@ export namespace App {
1012

1113
const ctx = Context.create<Info>("app")
1214

13-
async function create(input: { directory: string }) {
14-
const dataDir = AppPath.data(input.directory)
15-
await fs.mkdir(dataDir, { recursive: true })
16-
await Log.file(input.directory)
15+
async function create(input: { cwd: string; version: string }) {
16+
let root = await Filesystem.findUp(".git", input.cwd).then((x) =>
17+
x ? path.dirname(x) : input.cwd,
18+
)
1719

18-
log.info("created", { path: dataDir })
20+
const data = path.join(Global.data(), root)
21+
await Bun.write(path.join(data, "version"), input.version)
1922

2023
const services = new Map<
2124
any,
@@ -25,14 +28,16 @@ export namespace App {
2528
}
2629
>()
2730

28-
const result = {
29-
get services() {
30-
return services
31-
},
32-
get root() {
33-
return input.directory
31+
await Log.file(path.join(data, "log"))
32+
33+
const result = Object.freeze({
34+
services,
35+
path: {
36+
data,
37+
root,
38+
cwd: input.cwd,
3439
},
35-
}
40+
})
3641

3742
return result
3843
}
@@ -61,7 +66,7 @@ export namespace App {
6166
}
6267

6368
export async function provide<T extends (app: Info) => any>(
64-
input: { directory: string },
69+
input: { cwd: string; version: string },
6570
cb: T,
6671
) {
6772
const app = await create(input)

packages/opencode/src/app/path.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/opencode/src/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export namespace Config {
88
const log = Log.create({ service: "config" })
99

1010
export const state = App.state("config", async (app) => {
11-
const result = await load(app.root)
11+
const result = await load(app.path.root)
1212
return result
1313
})
1414

packages/opencode/src/global/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export namespace Global {
1717
export function cache() {
1818
return paths.cache
1919
}
20+
21+
export function data() {
22+
return paths.data
23+
}
2024
}

packages/opencode/src/id/id.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export namespace Identifier {
55
const prefixes = {
66
session: "ses",
77
message: "msg",
8+
user: "usr",
89
} as const
910

1011
export function schema(prefix: keyof typeof prefixes) {

packages/opencode/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ declare global {
1616
}
1717

1818
const cli = cac("opencode")
19+
const version = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev"
1920

2021
cli.command("", "Start the opencode in interactive mode").action(async () => {
21-
await App.provide({ directory: process.cwd() }, async () => {
22+
await App.provide({ cwd: process.cwd(), version }, async () => {
2223
await Share.init()
2324
const server = Server.listen()
2425

@@ -66,14 +67,14 @@ cli
6667
.command("run [...message]", "Run a chat message")
6768
.option("--session <id>", "Session ID")
6869
.action(async (message: string[], options) => {
69-
await App.provide({ directory: process.cwd() }, async () => {
70+
await App.provide({ cwd: process.cwd(), version }, async () => {
7071
await Share.init()
7172
const session = options.session
7273
? await Session.get(options.session)
7374
: await Session.create()
7475
console.log("Session:", session.id)
7576

76-
Bus.subscribe(Message.Event.Updated, async (message) => {
77+
Bus.subscribe(Message.Event.Updated, async () => {
7778
console.log("Thinking...")
7879
})
7980

packages/opencode/src/lsp/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export namespace LSPClient {
3636
const [command, ...args] = input.cmd
3737
const server = spawn(command, args, {
3838
stdio: ["pipe", "pipe", "pipe"],
39-
cwd: app.root,
39+
cwd: app.path.cwd,
4040
})
4141

4242
const connection = createMessageConnection(
@@ -64,7 +64,7 @@ export namespace LSPClient {
6464
workspaceFolders: [
6565
{
6666
name: "workspace",
67-
uri: "file://" + app.root,
67+
uri: "file://" + app.path.cwd,
6868
},
6969
],
7070
tsserver: {

packages/opencode/src/session/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export namespace Session {
220220
tool: {},
221221
},
222222
}
223-
const contextFile = Bun.file(path.join(app.root, "CONTEXT.md"))
223+
const contextFile = Bun.file(path.join(app.path.root, "CONTEXT.md"))
224224
if (await contextFile.exists()) {
225225
const context = await contextFile.text()
226226
system.parts.push({

packages/opencode/src/storage/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { LocalStorageAdapter } from "@flystorage/local-fs"
33
import fs from "fs/promises"
44
import { Log } from "../util/log"
55
import { App } from "../app/app"
6-
import { AppPath } from "../app/path"
76
import { Bus } from "../bus"
7+
import path from "path"
88
import z from "zod"
99

1010
export namespace Storage {
@@ -19,7 +19,7 @@ export namespace Storage {
1919

2020
const state = App.state("storage", async () => {
2121
const app = await App.use()
22-
const storageDir = AppPath.storage(app.root)
22+
const storageDir = path.join(app.path.data, "storage")
2323
await fs.mkdir(storageDir, { recursive: true })
2424
const storage = new FileStorage(new LocalStorageAdapter(storageDir))
2525
log.info("created", { path: storageDir })

packages/opencode/src/tool/glob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const GlobTool = Tool.define({
5151
}),
5252
async execute(params) {
5353
const app = await App.use()
54-
const search = params.path || app.root
54+
const search = params.path || app.path.cwd
5555
const limit = 100
5656
const glob = new Bun.Glob(params.pattern)
5757
const files = []

0 commit comments

Comments
 (0)