Skip to content

Commit 14ccff4

Browse files
authored
refactor(agent): remove async facade exports (#22341)
1 parent 5b8b874 commit 14ccff4

File tree

11 files changed

+87
-83
lines changed

11 files changed

+87
-83
lines changed

packages/opencode/src/acp/agent.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import type { ACPConfig } from "./types"
4040
import { Provider } from "../provider/provider"
4141
import { ModelID, ProviderID } from "../provider/schema"
4242
import { Agent as AgentModule } from "../agent/agent"
43+
import { AppRuntime } from "@/effect/app-runtime"
4344
import { Installation } from "@/installation"
4445
import { MessageV2 } from "@/session/message-v2"
4546
import { Config } from "@/config/config"
@@ -1166,7 +1167,7 @@ export namespace ACP {
11661167
this.sessionManager.get(sessionId).modeId ||
11671168
(await (async () => {
11681169
if (!availableModes.length) return undefined
1169-
const defaultAgentName = await AgentModule.defaultAgent()
1170+
const defaultAgentName = await AppRuntime.runPromise(AgentModule.Service.use((svc) => svc.defaultAgent()))
11701171
const resolvedModeId =
11711172
availableModes.find((mode) => mode.name === defaultAgentName)?.id ?? availableModes[0].id
11721173
this.sessionManager.setMode(sessionId, resolvedModeId)
@@ -1367,7 +1368,8 @@ export namespace ACP {
13671368
if (!current) {
13681369
this.sessionManager.setModel(session.id, model)
13691370
}
1370-
const agent = session.modeId ?? (await AgentModule.defaultAgent())
1371+
const agent =
1372+
session.modeId ?? (await AppRuntime.runPromise(AgentModule.Service.use((svc) => svc.defaultAgent())))
13711373

13721374
const parts: Array<
13731375
| { type: "text"; text: string; synthetic?: boolean; ignored?: boolean }

packages/opencode/src/agent/agent.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { Plugin } from "@/plugin"
2121
import { Skill } from "../skill"
2222
import { Effect, Context, Layer } from "effect"
2323
import { InstanceState } from "@/effect/instance-state"
24-
import { makeRuntime } from "@/effect/run-service"
2524

2625
export namespace Agent {
2726
export const Info = z
@@ -404,22 +403,4 @@ export namespace Agent {
404403
Layer.provide(Config.defaultLayer),
405404
Layer.provide(Skill.defaultLayer),
406405
)
407-
408-
const { runPromise } = makeRuntime(Service, defaultLayer)
409-
410-
export async function get(agent: string) {
411-
return runPromise((svc) => svc.get(agent))
412-
}
413-
414-
export async function list() {
415-
return runPromise((svc) => svc.list())
416-
}
417-
418-
export async function defaultAgent() {
419-
return runPromise((svc) => svc.defaultAgent())
420-
}
421-
422-
export async function generate(input: { description: string; model?: { providerID: ProviderID; modelID: ModelID } }) {
423-
return runPromise((svc) => svc.generate(input))
424-
}
425406
}

packages/opencode/src/cli/cmd/agent.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { cmd } from "./cmd"
22
import * as prompts from "@clack/prompts"
3+
import { AppRuntime } from "@/effect/app-runtime"
34
import { UI } from "../ui"
45
import { Global } from "../../global"
56
import { Agent } from "../../agent/agent"
@@ -110,7 +111,9 @@ const AgentCreateCommand = cmd({
110111
const spinner = prompts.spinner()
111112
spinner.start("Generating agent configuration...")
112113
const model = args.model ? Provider.parseModel(args.model) : undefined
113-
const generated = await Agent.generate({ description, model }).catch((error) => {
114+
const generated = await AppRuntime.runPromise(
115+
Agent.Service.use((svc) => svc.generate({ description, model })),
116+
).catch((error) => {
114117
spinner.stop(`LLM failed to generate agent: ${error.message}`, 1)
115118
if (isFullyNonInteractive) process.exit(1)
116119
throw new UI.CancelledError()
@@ -220,7 +223,7 @@ const AgentListCommand = cmd({
220223
await Instance.provide({
221224
directory: process.cwd(),
222225
async fn() {
223-
const agents = await Agent.list()
226+
const agents = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.list()))
224227
const sortedAgents = agents.sort((a, b) => {
225228
if (a.native !== b.native) {
226229
return a.native ? -1 : 1

packages/opencode/src/cli/cmd/debug/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const AgentCommand = cmd({
3535
async handler(args) {
3636
await bootstrap(process.cwd(), async () => {
3737
const agentName = args.name as string
38-
const agent = await Agent.get(agentName)
38+
const agent = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.get(agentName)))
3939
if (!agent) {
4040
process.stderr.write(
4141
`Agent ${agentName} not found, run '${basename(process.execPath)} agent list' to get an agent list` + EOL,

packages/opencode/src/cli/cmd/run.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { SkillTool } from "../../tool/skill"
2727
import { BashTool } from "../../tool/bash"
2828
import { TodoWriteTool } from "../../tool/todo"
2929
import { Locale } from "../../util/locale"
30+
import { AppRuntime } from "@/effect/app-runtime"
3031

3132
type ToolProps<T> = {
3233
input: Tool.InferParameters<T>
@@ -573,6 +574,7 @@ export const RunCommand = cmd({
573574
// Validate agent if specified
574575
const agent = await (async () => {
575576
if (!args.agent) return undefined
577+
const name = args.agent
576578

577579
// When attaching, validate against the running server instead of local Instance state.
578580
if (args.attach) {
@@ -590,12 +592,12 @@ export const RunCommand = cmd({
590592
return undefined
591593
}
592594

593-
const agent = modes.find((a) => a.name === args.agent)
595+
const agent = modes.find((a) => a.name === name)
594596
if (!agent) {
595597
UI.println(
596598
UI.Style.TEXT_WARNING_BOLD + "!",
597599
UI.Style.TEXT_NORMAL,
598-
`agent "${args.agent}" not found. Falling back to default agent`,
600+
`agent "${name}" not found. Falling back to default agent`,
599601
)
600602
return undefined
601603
}
@@ -604,32 +606,32 @@ export const RunCommand = cmd({
604606
UI.println(
605607
UI.Style.TEXT_WARNING_BOLD + "!",
606608
UI.Style.TEXT_NORMAL,
607-
`agent "${args.agent}" is a subagent, not a primary agent. Falling back to default agent`,
609+
`agent "${name}" is a subagent, not a primary agent. Falling back to default agent`,
608610
)
609611
return undefined
610612
}
611613

612-
return args.agent
614+
return name
613615
}
614616

615-
const entry = await Agent.get(args.agent)
617+
const entry = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.get(name)))
616618
if (!entry) {
617619
UI.println(
618620
UI.Style.TEXT_WARNING_BOLD + "!",
619621
UI.Style.TEXT_NORMAL,
620-
`agent "${args.agent}" not found. Falling back to default agent`,
622+
`agent "${name}" not found. Falling back to default agent`,
621623
)
622624
return undefined
623625
}
624626
if (entry.mode === "subagent") {
625627
UI.println(
626628
UI.Style.TEXT_WARNING_BOLD + "!",
627629
UI.Style.TEXT_NORMAL,
628-
`agent "${args.agent}" is a subagent, not a primary agent. Falling back to default agent`,
630+
`agent "${name}" is a subagent, not a primary agent. Falling back to default agent`,
629631
)
630632
return undefined
631633
}
632-
return args.agent
634+
return name
633635
})()
634636

635637
const sessionID = await session(sdk)

packages/opencode/src/server/instance/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono =>
207207
},
208208
}),
209209
async (c) => {
210-
const modes = await Agent.list()
210+
const modes = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.list()))
211211
return c.json(modes)
212212
},
213213
)

packages/opencode/src/server/instance/session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,12 @@ export const SessionRoutes = lazy(() =>
550550
const session = await Session.get(sessionID)
551551
await SessionRevert.cleanup(session)
552552
const msgs = await Session.messages({ sessionID })
553-
let currentAgent = await Agent.defaultAgent()
553+
const defaultAgent = await AppRuntime.runPromise(Agent.Service.use((svc) => svc.defaultAgent()))
554+
let currentAgent = defaultAgent
554555
for (let i = msgs.length - 1; i >= 0; i--) {
555556
const info = msgs[i].info
556557
if (info.role === "user") {
557-
currentAgent = info.agent || (await Agent.defaultAgent())
558+
currentAgent = info.agent || defaultAgent
558559
break
559560
}
560561
}

packages/opencode/src/tool/registry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export namespace ToolRegistry {
121121
const greptool = yield* GrepTool
122122
const patchtool = yield* ApplyPatchTool
123123
const skilltool = yield* SkillTool
124+
const agent = yield* Agent.Service
124125

125126
const state = yield* InstanceState.make<State>(
126127
Effect.fn("ToolRegistry.state")(function* (ctx) {
@@ -140,8 +141,8 @@ export namespace ToolRegistry {
140141
worktree: ctx.worktree,
141142
}
142143
const result = yield* Effect.promise(() => def.execute(args as any, pluginCtx))
143-
const agent = yield* Effect.promise(() => Agent.get(toolCtx.agent))
144-
const out = yield* truncate.output(result, {}, agent)
144+
const info = yield* agent.get(toolCtx.agent)
145+
const out = yield* truncate.output(result, {}, info)
145146
return {
146147
title: "",
147148
output: out.truncated ? out.content : result,

0 commit comments

Comments
 (0)