Skip to content

Commit 1dd257b

Browse files
authored
refactor: use instance state in small services (#23022)
1 parent 5fa1673 commit 1dd257b

4 files changed

Lines changed: 13 additions & 21 deletions

File tree

packages/opencode/src/mcp/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { ConfigMCP } from "../config/mcp"
1414
import { Log } from "../util"
1515
import { NamedError } from "@opencode-ai/shared/util/error"
1616
import z from "zod/v4"
17-
import { Instance } from "../project/instance"
1817
import { Installation } from "../installation"
1918
import { InstallationVersion } from "../installation/version"
2019
import { withTimeout } from "@/util/timeout"
@@ -391,7 +390,7 @@ export const layer = Layer.effect(
391390
mcp: ConfigMCP.Info & { type: "local" },
392391
) {
393392
const [cmd, ...args] = mcp.command
394-
const cwd = Instance.directory
393+
const cwd = yield* InstanceState.directory
395394
const transport = new StdioClientTransport({
396395
stderr: "pipe",
397396
command: cmd,

packages/opencode/src/project/vcs.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { AppFileSystem } from "@opencode-ai/shared/filesystem"
88
import { FileWatcher } from "@/file/watcher"
99
import { Git } from "@/git"
1010
import { Log } from "@/util"
11-
import { Instance } from "./instance"
1211
import z from "zod"
1312

1413
const log = Log.create({ service: "vcs" })
@@ -205,21 +204,17 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Git.Serv
205204
}),
206205
diff: Effect.fn("Vcs.diff")(function* (mode: Mode) {
207206
const value = yield* InstanceState.get(state)
208-
if (Instance.project.vcs !== "git") return []
207+
const ctx = yield* InstanceState.context
208+
if (ctx.project.vcs !== "git") return []
209209
if (mode === "git") {
210-
return yield* track(
211-
fs,
212-
git,
213-
Instance.directory,
214-
(yield* git.hasHead(Instance.directory)) ? "HEAD" : undefined,
215-
)
210+
return yield* track(fs, git, ctx.directory, (yield* git.hasHead(ctx.directory)) ? "HEAD" : undefined)
216211
}
217212

218213
if (!value.root) return []
219214
if (value.current && value.current === value.root.name) return []
220-
const ref = yield* git.mergeBase(Instance.directory, value.root.ref)
215+
const ref = yield* git.mergeBase(ctx.directory, value.root.ref)
221216
if (!ref) return []
222-
return yield* compare(fs, git, Instance.directory, ref)
217+
return yield* compare(fs, git, ctx.directory, ref)
223218
}),
224219
})
225220
}),

packages/opencode/src/provider/provider.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { type LanguageModelV3 } from "@ai-sdk/provider"
1313
import * as ModelsDev from "./models"
1414
import { Auth } from "../auth"
1515
import { Env } from "../env"
16-
import { Instance } from "../project/instance"
1716
import { InstallationVersion } from "../installation/version"
1817
import { Flag } from "../flag/flag"
1918
import { zod } from "@/util/effect-zod"
@@ -537,6 +536,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
537536
const token = apiKey ?? (yield* dep.get("GITLAB_TOKEN"))
538537

539538
const providerConfig = (yield* dep.config()).provider?.["gitlab"]
539+
const directory = yield* InstanceState.directory
540540

541541
const aiGatewayHeaders = {
542542
"User-Agent": `opencode/${InstallationVersion} gitlab-ai-provider/${GITLAB_PROVIDER_VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`,
@@ -591,10 +591,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
591591
auth?.type === "api" ? { "PRIVATE-TOKEN": token } : { Authorization: `Bearer ${token}` }
592592

593593
log.info("gitlab model discovery starting", { instanceUrl })
594-
const result = await discoverWorkflowModels(
595-
{ instanceUrl, getHeaders },
596-
{ workingDirectory: Instance.directory },
597-
)
594+
const result = await discoverWorkflowModels({ instanceUrl, getHeaders }, { workingDirectory: directory })
598595

599596
if (!result.models.length) {
600597
log.info("gitlab model discovery skipped: no models found", {

packages/opencode/src/session/instruction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Flag } from "@/flag/flag"
88
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
99
import { withTransientReadRetry } from "@/util/effect-http-client"
1010
import { Global } from "../global"
11-
import { Instance } from "../project/instance"
1211
import { Log } from "../util"
1312
import type { MessageV2 } from "./message-v2"
1413
import type { MessageID } from "./schema"
@@ -82,9 +81,10 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
8281
)
8382

8483
const relative = Effect.fnUntraced(function* (instruction: string) {
84+
const ctx = yield* InstanceState.context
8585
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
8686
return yield* fs
87-
.globUp(instruction, Instance.directory, Instance.worktree)
87+
.globUp(instruction, ctx.directory, ctx.worktree)
8888
.pipe(Effect.catch(() => Effect.succeed([] as string[])))
8989
}
9090
if (!Flag.OPENCODE_CONFIG_DIR) {
@@ -119,12 +119,13 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
119119

120120
const systemPaths = Effect.fn("Instruction.systemPaths")(function* () {
121121
const config = yield* cfg.get()
122+
const ctx = yield* InstanceState.context
122123
const paths = new Set<string>()
123124

124125
// The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor.
125126
if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) {
126127
for (const file of FILES) {
127-
const matches = yield* fs.findUp(file, Instance.directory, Instance.worktree)
128+
const matches = yield* fs.findUp(file, ctx.directory, ctx.worktree)
128129
if (matches.length > 0) {
129130
matches.forEach((item) => paths.add(path.resolve(item)))
130131
break
@@ -191,9 +192,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.S
191192
const already = extract(messages)
192193
const results: { filepath: string; content: string }[] = []
193194
const s = yield* InstanceState.get(state)
195+
const root = path.resolve(yield* InstanceState.directory)
194196

195197
const target = path.resolve(filepath)
196-
const root = path.resolve(Instance.directory)
197198
let current = path.dirname(target)
198199

199200
// Walk upward from the file being read and attach nearby instruction files once per message.

0 commit comments

Comments
 (0)