Skip to content

Commit cf1fa7b

Browse files
committed
refactor: split up routes from server.ts into individual files
1 parent 4b0f7b8 commit cf1fa7b

File tree

14 files changed

+2667
-2554
lines changed

14 files changed

+2667
-2554
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { Hono } from "hono"
2+
import { describeRoute, validator, resolver } from "hono-openapi"
3+
import z from "zod"
4+
import { Config } from "../../config/config"
5+
import { Provider } from "../../provider/provider"
6+
import { mapValues } from "remeda"
7+
import { errors } from "../error"
8+
import { Log } from "../../util/log"
9+
import { lazy } from "../../util/lazy"
10+
11+
const log = Log.create({ service: "server" })
12+
13+
export const ConfigRoutes = lazy(() =>
14+
new Hono()
15+
.get(
16+
"/",
17+
describeRoute({
18+
summary: "Get configuration",
19+
description: "Retrieve the current OpenCode configuration settings and preferences.",
20+
operationId: "config.get",
21+
responses: {
22+
200: {
23+
description: "Get config info",
24+
content: {
25+
"application/json": {
26+
schema: resolver(Config.Info),
27+
},
28+
},
29+
},
30+
},
31+
}),
32+
async (c) => {
33+
return c.json(await Config.get())
34+
},
35+
)
36+
.patch(
37+
"/",
38+
describeRoute({
39+
summary: "Update configuration",
40+
description: "Update OpenCode configuration settings and preferences.",
41+
operationId: "config.update",
42+
responses: {
43+
200: {
44+
description: "Successfully updated config",
45+
content: {
46+
"application/json": {
47+
schema: resolver(Config.Info),
48+
},
49+
},
50+
},
51+
...errors(400),
52+
},
53+
}),
54+
validator("json", Config.Info),
55+
async (c) => {
56+
const config = c.req.valid("json")
57+
await Config.update(config)
58+
return c.json(config)
59+
},
60+
)
61+
.get(
62+
"/providers",
63+
describeRoute({
64+
summary: "List config providers",
65+
description: "Get a list of all configured AI providers and their default models.",
66+
operationId: "config.providers",
67+
responses: {
68+
200: {
69+
description: "List of providers",
70+
content: {
71+
"application/json": {
72+
schema: resolver(
73+
z.object({
74+
providers: Provider.Info.array(),
75+
default: z.record(z.string(), z.string()),
76+
}),
77+
),
78+
},
79+
},
80+
},
81+
},
82+
}),
83+
async (c) => {
84+
using _ = log.time("providers")
85+
const providers = await Provider.list().then((x) => mapValues(x, (item) => item))
86+
return c.json({
87+
providers: Object.values(providers),
88+
default: mapValues(providers, (item) => Provider.sort(Object.values(item.models))[0].id),
89+
})
90+
},
91+
),
92+
)
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { Hono } from "hono"
2+
import { describeRoute, validator, resolver } from "hono-openapi"
3+
import z from "zod"
4+
import { ToolRegistry } from "../../tool/registry"
5+
import { Worktree } from "../../worktree"
6+
import { Instance } from "../../project/instance"
7+
import { Project } from "../../project/project"
8+
import { MCP } from "../../mcp"
9+
import { zodToJsonSchema } from "zod-to-json-schema"
10+
import { errors } from "../error"
11+
import { lazy } from "../../util/lazy"
12+
13+
export const ExperimentalRoutes = lazy(() => new Hono()
14+
.get(
15+
"/tool/ids",
16+
describeRoute({
17+
summary: "List tool IDs",
18+
description:
19+
"Get a list of all available tool IDs, including both built-in tools and dynamically registered tools.",
20+
operationId: "tool.ids",
21+
responses: {
22+
200: {
23+
description: "Tool IDs",
24+
content: {
25+
"application/json": {
26+
schema: resolver(z.array(z.string()).meta({ ref: "ToolIDs" })),
27+
},
28+
},
29+
},
30+
...errors(400),
31+
},
32+
}),
33+
async (c) => {
34+
return c.json(await ToolRegistry.ids())
35+
},
36+
)
37+
.get(
38+
"/tool",
39+
describeRoute({
40+
summary: "List tools",
41+
description:
42+
"Get a list of available tools with their JSON schema parameters for a specific provider and model combination.",
43+
operationId: "tool.list",
44+
responses: {
45+
200: {
46+
description: "Tools",
47+
content: {
48+
"application/json": {
49+
schema: resolver(
50+
z
51+
.array(
52+
z
53+
.object({
54+
id: z.string(),
55+
description: z.string(),
56+
parameters: z.any(),
57+
})
58+
.meta({ ref: "ToolListItem" }),
59+
)
60+
.meta({ ref: "ToolList" }),
61+
),
62+
},
63+
},
64+
},
65+
...errors(400),
66+
},
67+
}),
68+
validator(
69+
"query",
70+
z.object({
71+
provider: z.string(),
72+
model: z.string(),
73+
}),
74+
),
75+
async (c) => {
76+
const { provider } = c.req.valid("query")
77+
const tools = await ToolRegistry.tools(provider)
78+
return c.json(
79+
tools.map((t) => ({
80+
id: t.id,
81+
description: t.description,
82+
// Handle both Zod schemas and plain JSON schemas
83+
parameters: (t.parameters as any)?._def ? zodToJsonSchema(t.parameters as any) : t.parameters,
84+
})),
85+
)
86+
},
87+
)
88+
.post(
89+
"/worktree",
90+
describeRoute({
91+
summary: "Create worktree",
92+
description: "Create a new git worktree for the current project.",
93+
operationId: "worktree.create",
94+
responses: {
95+
200: {
96+
description: "Worktree created",
97+
content: {
98+
"application/json": {
99+
schema: resolver(Worktree.Info),
100+
},
101+
},
102+
},
103+
...errors(400),
104+
},
105+
}),
106+
validator("json", Worktree.create.schema),
107+
async (c) => {
108+
const body = c.req.valid("json")
109+
const worktree = await Worktree.create(body)
110+
return c.json(worktree)
111+
},
112+
)
113+
.get(
114+
"/worktree",
115+
describeRoute({
116+
summary: "List worktrees",
117+
description: "List all sandbox worktrees for the current project.",
118+
operationId: "worktree.list",
119+
responses: {
120+
200: {
121+
description: "List of worktree directories",
122+
content: {
123+
"application/json": {
124+
schema: resolver(z.array(z.string())),
125+
},
126+
},
127+
},
128+
},
129+
}),
130+
async (c) => {
131+
const sandboxes = await Project.sandboxes(Instance.project.id)
132+
return c.json(sandboxes)
133+
},
134+
)
135+
.get(
136+
"/resource",
137+
describeRoute({
138+
summary: "Get MCP resources",
139+
description: "Get all available MCP resources from connected servers. Optionally filter by name.",
140+
operationId: "experimental.resource.list",
141+
responses: {
142+
200: {
143+
description: "MCP resources",
144+
content: {
145+
"application/json": {
146+
schema: resolver(z.record(z.string(), MCP.Resource)),
147+
},
148+
},
149+
},
150+
},
151+
}),
152+
async (c) => {
153+
return c.json(await MCP.resources())
154+
},
155+
)
156+
)

0 commit comments

Comments
 (0)