Skip to content

Commit 999d865

Browse files
authored
feat(server): wrap remaining route handlers in request spans (#23169)
1 parent ed0f022 commit 999d865

13 files changed

Lines changed: 550 additions & 600 deletions

File tree

packages/opencode/src/server/routes/instance/config.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Config } from "@/config"
55
import { Provider } from "@/provider"
66
import { errors } from "../../error"
77
import { lazy } from "@/util/lazy"
8-
import { AppRuntime } from "@/effect/app-runtime"
98
import { jsonRequest } from "./trace"
109

1110
export const ConfigRoutes = lazy(() =>
@@ -52,11 +51,13 @@ export const ConfigRoutes = lazy(() =>
5251
},
5352
}),
5453
validator("json", Config.Info),
55-
async (c) => {
56-
const config = c.req.valid("json")
57-
await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.update(config)))
58-
return c.json(config)
59-
},
54+
async (c) =>
55+
jsonRequest("ConfigRoutes.update", c, function* () {
56+
const config = c.req.valid("json")
57+
const cfg = yield* Config.Service
58+
yield* cfg.update(config)
59+
return config
60+
}),
6061
)
6162
.get(
6263
"/providers",

packages/opencode/src/server/routes/instance/experimental.ts

Lines changed: 79 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { Config } from "@/config"
1212
import { ConsoleState } from "@/config/console-state"
1313
import { Account } from "@/account/account"
1414
import { AccountID, OrgID } from "@/account/schema"
15-
import { AppRuntime } from "@/effect/app-runtime"
1615
import { errors } from "../../error"
1716
import { lazy } from "@/util/lazy"
1817
import { Effect, Option } from "effect"
1918
import { Agent } from "@/agent/agent"
19+
import { jsonRequest, runRequest } from "./trace"
2020

2121
const ConsoleOrgOption = z.object({
2222
accountID: z.string(),
@@ -55,22 +55,18 @@ export const ExperimentalRoutes = lazy(() =>
5555
},
5656
},
5757
}),
58-
async (c) => {
59-
const result = await AppRuntime.runPromise(
60-
Effect.gen(function* () {
61-
const config = yield* Config.Service
62-
const account = yield* Account.Service
63-
const [state, groups] = yield* Effect.all([config.getConsoleState(), account.orgsByAccount()], {
64-
concurrency: "unbounded",
65-
})
66-
return {
67-
...state,
68-
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0),
69-
}
70-
}),
71-
)
72-
return c.json(result)
73-
},
58+
async (c) =>
59+
jsonRequest("ExperimentalRoutes.console.get", c, function* () {
60+
const config = yield* Config.Service
61+
const account = yield* Account.Service
62+
const [state, groups] = yield* Effect.all([config.getConsoleState(), account.orgsByAccount()], {
63+
concurrency: "unbounded",
64+
})
65+
return {
66+
...state,
67+
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0),
68+
}
69+
}),
7470
)
7571
.get(
7672
"/console/orgs",
@@ -89,28 +85,25 @@ export const ExperimentalRoutes = lazy(() =>
8985
},
9086
},
9187
}),
92-
async (c) => {
93-
const orgs = await AppRuntime.runPromise(
94-
Effect.gen(function* () {
95-
const account = yield* Account.Service
96-
const [groups, active] = yield* Effect.all([account.orgsByAccount(), account.active()], {
97-
concurrency: "unbounded",
98-
})
99-
const info = Option.getOrUndefined(active)
100-
return groups.flatMap((group) =>
101-
group.orgs.map((org) => ({
102-
accountID: group.account.id,
103-
accountEmail: group.account.email,
104-
accountUrl: group.account.url,
105-
orgID: org.id,
106-
orgName: org.name,
107-
active: !!info && info.id === group.account.id && info.active_org_id === org.id,
108-
})),
109-
)
110-
}),
111-
)
112-
return c.json({ orgs })
113-
},
88+
async (c) =>
89+
jsonRequest("ExperimentalRoutes.console.listOrgs", c, function* () {
90+
const account = yield* Account.Service
91+
const [groups, active] = yield* Effect.all([account.orgsByAccount(), account.active()], {
92+
concurrency: "unbounded",
93+
})
94+
const info = Option.getOrUndefined(active)
95+
const orgs = groups.flatMap((group) =>
96+
group.orgs.map((org) => ({
97+
accountID: group.account.id,
98+
accountEmail: group.account.email,
99+
accountUrl: group.account.url,
100+
orgID: org.id,
101+
orgName: org.name,
102+
active: !!info && info.id === group.account.id && info.active_org_id === org.id,
103+
})),
104+
)
105+
return { orgs }
106+
}),
114107
)
115108
.post(
116109
"/console/switch",
@@ -130,16 +123,13 @@ export const ExperimentalRoutes = lazy(() =>
130123
},
131124
}),
132125
validator("json", ConsoleSwitchBody),
133-
async (c) => {
134-
const body = c.req.valid("json")
135-
await AppRuntime.runPromise(
136-
Effect.gen(function* () {
137-
const account = yield* Account.Service
138-
yield* account.use(AccountID.make(body.accountID), Option.some(OrgID.make(body.orgID)))
139-
}),
140-
)
141-
return c.json(true)
142-
},
126+
async (c) =>
127+
jsonRequest("ExperimentalRoutes.console.switchOrg", c, function* () {
128+
const body = c.req.valid("json")
129+
const account = yield* Account.Service
130+
yield* account.use(AccountID.make(body.accountID), Option.some(OrgID.make(body.orgID)))
131+
return true
132+
}),
143133
)
144134
.get(
145135
"/tool/ids",
@@ -160,15 +150,11 @@ export const ExperimentalRoutes = lazy(() =>
160150
...errors(400),
161151
},
162152
}),
163-
async (c) => {
164-
const ids = await AppRuntime.runPromise(
165-
Effect.gen(function* () {
166-
const registry = yield* ToolRegistry.Service
167-
return yield* registry.ids()
168-
}),
169-
)
170-
return c.json(ids)
171-
},
153+
async (c) =>
154+
jsonRequest("ExperimentalRoutes.tool.ids", c, function* () {
155+
const registry = yield* ToolRegistry.Service
156+
return yield* registry.ids()
157+
}),
172158
)
173159
.get(
174160
"/tool",
@@ -210,7 +196,9 @@ export const ExperimentalRoutes = lazy(() =>
210196
),
211197
async (c) => {
212198
const { provider, model } = c.req.valid("query")
213-
const tools = await AppRuntime.runPromise(
199+
const tools = await runRequest(
200+
"ExperimentalRoutes.tool.list",
201+
c,
214202
Effect.gen(function* () {
215203
const agents = yield* Agent.Service
216204
const registry = yield* ToolRegistry.Service
@@ -249,11 +237,12 @@ export const ExperimentalRoutes = lazy(() =>
249237
},
250238
}),
251239
validator("json", Worktree.CreateInput.optional()),
252-
async (c) => {
253-
const body = c.req.valid("json")
254-
const worktree = await AppRuntime.runPromise(Worktree.Service.use((svc) => svc.create(body)))
255-
return c.json(worktree)
256-
},
240+
async (c) =>
241+
jsonRequest("ExperimentalRoutes.worktree.create", c, function* () {
242+
const body = c.req.valid("json")
243+
const svc = yield* Worktree.Service
244+
return yield* svc.create(body)
245+
}),
257246
)
258247
.get(
259248
"/worktree",
@@ -272,10 +261,11 @@ export const ExperimentalRoutes = lazy(() =>
272261
},
273262
},
274263
}),
275-
async (c) => {
276-
const sandboxes = await AppRuntime.runPromise(Project.Service.use((svc) => svc.sandboxes(Instance.project.id)))
277-
return c.json(sandboxes)
278-
},
264+
async (c) =>
265+
jsonRequest("ExperimentalRoutes.worktree.list", c, function* () {
266+
const svc = yield* Project.Service
267+
return yield* svc.sandboxes(Instance.project.id)
268+
}),
279269
)
280270
.delete(
281271
"/worktree",
@@ -296,14 +286,15 @@ export const ExperimentalRoutes = lazy(() =>
296286
},
297287
}),
298288
validator("json", Worktree.RemoveInput),
299-
async (c) => {
300-
const body = c.req.valid("json")
301-
await AppRuntime.runPromise(Worktree.Service.use((svc) => svc.remove(body)))
302-
await AppRuntime.runPromise(
303-
Project.Service.use((svc) => svc.removeSandbox(Instance.project.id, body.directory)),
304-
)
305-
return c.json(true)
306-
},
289+
async (c) =>
290+
jsonRequest("ExperimentalRoutes.worktree.remove", c, function* () {
291+
const body = c.req.valid("json")
292+
const worktree = yield* Worktree.Service
293+
const project = yield* Project.Service
294+
yield* worktree.remove(body)
295+
yield* project.removeSandbox(Instance.project.id, body.directory)
296+
return true
297+
}),
307298
)
308299
.post(
309300
"/worktree/reset",
@@ -324,11 +315,13 @@ export const ExperimentalRoutes = lazy(() =>
324315
},
325316
}),
326317
validator("json", Worktree.ResetInput),
327-
async (c) => {
328-
const body = c.req.valid("json")
329-
await AppRuntime.runPromise(Worktree.Service.use((svc) => svc.reset(body)))
330-
return c.json(true)
331-
},
318+
async (c) =>
319+
jsonRequest("ExperimentalRoutes.worktree.reset", c, function* () {
320+
const body = c.req.valid("json")
321+
const svc = yield* Worktree.Service
322+
yield* svc.reset(body)
323+
return true
324+
}),
332325
)
333326
.get(
334327
"/session",
@@ -406,15 +399,10 @@ export const ExperimentalRoutes = lazy(() =>
406399
},
407400
},
408401
}),
409-
async (c) => {
410-
return c.json(
411-
await AppRuntime.runPromise(
412-
Effect.gen(function* () {
413-
const mcp = yield* MCP.Service
414-
return yield* mcp.resources()
415-
}),
416-
),
417-
)
418-
},
402+
async (c) =>
403+
jsonRequest("ExperimentalRoutes.resource.list", c, function* () {
404+
const mcp = yield* MCP.Service
405+
return yield* mcp.resources()
406+
}),
419407
),
420408
)

0 commit comments

Comments
 (0)