|
| 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