Repository navigation
feat(opencode): add experimental HttpApi coverage #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
packages/opencode/src/server/routes/instance/httpapi/groups/experimental.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { ConsoleState } from "@/config/console-state" | ||
| import { Schema } from "effect" | ||
| import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi" | ||
| import { BadRequestError } from "./common" | ||
|
|
||
| const root = "/experimental" | ||
|
|
||
| const ConsoleOrgOption = Schema.Struct({ | ||
| accountID: Schema.String, | ||
| accountEmail: Schema.String, | ||
| accountUrl: Schema.String, | ||
| orgID: Schema.String, | ||
| orgName: Schema.String, | ||
| active: Schema.Boolean, | ||
| }) | ||
|
|
||
| const ConsoleOrgList = Schema.Struct({ | ||
| orgs: Schema.Array(ConsoleOrgOption), | ||
| }) | ||
|
|
||
| const ConsoleSwitchPayload = Schema.Struct({ | ||
| accountID: Schema.String, | ||
| orgID: Schema.String, | ||
| }) | ||
|
|
||
| const ToolListQuery = Schema.Struct({ | ||
| provider: Schema.String, | ||
| model: Schema.String, | ||
| }) | ||
|
|
||
| const ToolListItem = Schema.Struct({ | ||
| id: Schema.String, | ||
| description: Schema.String, | ||
| parameters: Schema.Any, | ||
| }) | ||
|
|
||
| const McpResource = Schema.Struct({ | ||
| name: Schema.String, | ||
| uri: Schema.String, | ||
| description: Schema.optionalKey(Schema.String), | ||
| mimeType: Schema.optionalKey(Schema.String), | ||
| client: Schema.String, | ||
| }) | ||
|
|
||
| const WorktreeInfo = Schema.Struct({ | ||
| name: Schema.String, | ||
| branch: Schema.String, | ||
| directory: Schema.String, | ||
| source: Schema.optionalKey(Schema.Literals(["created", "existing"])), | ||
| }) | ||
|
|
||
| const WorktreeCreatePayload = Schema.Struct({ | ||
| name: Schema.optionalKey(Schema.String), | ||
| startCommand: Schema.optionalKey(Schema.String), | ||
| }) | ||
|
|
||
| const WorktreeDirectoryPayload = Schema.Struct({ | ||
| directory: Schema.String, | ||
| }) | ||
|
|
||
| export const ExperimentalPaths = { | ||
| console: `${root}/console`, | ||
| consoleOrgs: `${root}/console/orgs`, | ||
| consoleSwitch: `${root}/console/switch`, | ||
| tool: `${root}/tool`, | ||
| toolIds: `${root}/tool/ids`, | ||
| resource: `${root}/resource`, | ||
| worktree: `${root}/worktree`, | ||
| worktreeReset: `${root}/worktree/reset`, | ||
| } as const | ||
|
|
||
| export const ExperimentalApi = HttpApi.make("experimental") | ||
| .add( | ||
| HttpApiGroup.make("experimental") | ||
| .add( | ||
| HttpApiEndpoint.get("console", ExperimentalPaths.console, { | ||
| success: ConsoleState, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "experimental.console.get", | ||
| summary: "Get active Console provider metadata", | ||
| description: "Get the active Console org name and the set of provider IDs managed by that Console org.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.get("consoleOrgs", ExperimentalPaths.consoleOrgs, { | ||
| success: ConsoleOrgList, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "experimental.console.listOrgs", | ||
| summary: "List switchable Console orgs", | ||
| description: "Get the available Console orgs across logged-in accounts, including the current active org.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.post("consoleSwitch", ExperimentalPaths.consoleSwitch, { | ||
| payload: ConsoleSwitchPayload, | ||
| success: Schema.Boolean, | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "experimental.console.switchOrg", | ||
| summary: "Switch active Console org", | ||
| description: "Persist a new active Console account/org selection for the current local OpenCode state.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.get("tool", ExperimentalPaths.tool, { | ||
| query: ToolListQuery, | ||
| success: Schema.Array(ToolListItem), | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "tool.list", | ||
| summary: "List tools", | ||
| description: | ||
| "Get a list of available tools with their JSON schema parameters for a specific provider and model combination.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.get("toolIds", ExperimentalPaths.toolIds, { | ||
| success: Schema.Array(Schema.String), | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "tool.ids", | ||
| summary: "List tool IDs", | ||
| description: | ||
| "Get a list of all available tool IDs, including both built-in tools and dynamically registered tools.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.get("resource", ExperimentalPaths.resource, { | ||
| success: Schema.Record(Schema.String, McpResource), | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "experimental.resource.list", | ||
| summary: "Get MCP resources", | ||
| description: "Get all available MCP resources from connected servers. Optionally filter by name.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.post("worktreeCreate", ExperimentalPaths.worktree, { | ||
| payload: Schema.optional(WorktreeCreatePayload), | ||
| success: WorktreeInfo, | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "worktree.create", | ||
| summary: "Create worktree", | ||
| description: "Create a new git worktree for the current project and run any configured startup scripts.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.get("worktreeList", ExperimentalPaths.worktree, { | ||
| success: Schema.Array(WorktreeInfo), | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "worktree.list", | ||
| summary: "List worktrees", | ||
| description: "List all sandbox worktrees for the current project.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.delete("worktreeRemove", ExperimentalPaths.worktree, { | ||
| payload: WorktreeDirectoryPayload, | ||
| success: Schema.Boolean, | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "worktree.remove", | ||
| summary: "Remove worktree", | ||
| description: "Remove a git worktree and delete its branch.", | ||
| }), | ||
| ), | ||
| HttpApiEndpoint.post("worktreeReset", ExperimentalPaths.worktreeReset, { | ||
| payload: WorktreeDirectoryPayload, | ||
| success: Schema.Boolean, | ||
| error: BadRequestError, | ||
| }).annotateMerge( | ||
| OpenApi.annotations({ | ||
| identifier: "worktree.reset", | ||
| summary: "Reset worktree", | ||
| description: "Reset a worktree branch to the primary default branch.", | ||
| }), | ||
| ), | ||
| ) | ||
| .annotateMerge( | ||
| OpenApi.annotations({ | ||
| title: "experimental", | ||
| description: "HttpApi experimental JSON routes.", | ||
| }), | ||
| ), | ||
| ) | ||
| .annotateMerge( | ||
| OpenApi.annotations({ | ||
| title: "opencode experimental HttpApi", | ||
| version: "0.0.1", | ||
| description: "HttpApi surface for ordinary experimental JSON routes.", | ||
| }), | ||
| ) |
119 changes: 119 additions & 0 deletions
119
packages/opencode/src/server/routes/instance/httpapi/handlers/experimental.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { | ||
| createWorktree, | ||
| getConsoleState, | ||
| listConsoleOrgs, | ||
| listResources, | ||
| listToolIDs, | ||
| listTools, | ||
| listWorktrees, | ||
| removeWorktree, | ||
| resetWorktree, | ||
| switchConsoleOrg, | ||
| } from "@/server/instance/experimental" | ||
| import { Worktree } from "@/worktree" | ||
| import { NamedError } from "@opencode-ai/util/error" | ||
| import { Effect } from "effect" | ||
| import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http" | ||
| import { HttpApiBuilder } from "effect/unstable/httpapi" | ||
| import z from "zod" | ||
| import { ExperimentalApi } from "../groups/experimental" | ||
|
|
||
| const ConsoleSwitchBody = z.object({ | ||
| accountID: z.string(), | ||
| orgID: z.string(), | ||
| }) | ||
|
|
||
| const WorktreeCreateBody = Worktree.CreateInput.optional() | ||
| const WorktreeDirectoryBody = z.object({ | ||
| directory: z.string(), | ||
| }) | ||
|
|
||
| function isJsonContentType(contentType: string | undefined) { | ||
| // Mirrors hono/validator's jsonRegex, reached through hono-openapi's validator("json"). | ||
| return /^application\/([a-z-.]+\+)?json(?:;\s*[a-zA-Z0-9-]+=([^;]+))*$/.test(contentType ?? "") | ||
| } | ||
|
|
||
| function badRequestJson(body: unknown) { | ||
| return HttpServerResponse.jsonUnsafe(body, { status: 400 }) | ||
| } | ||
|
|
||
| function parseJsonBody<T>(request: HttpServerRequest.HttpServerRequest, schema: z.ZodType<T>) { | ||
| return Effect.gen(function* () { | ||
| const body = isJsonContentType(request.headers["content-type"]) | ||
| ? yield* request.json.pipe( | ||
| Effect.catch(() => Effect.succeed(HttpServerResponse.raw("Malformed JSON in request body", { status: 400 }))), | ||
| ) | ||
| : {} | ||
| if (HttpServerResponse.isHttpServerResponse(body)) return body | ||
|
|
||
| const parsed = schema.safeParse(body) | ||
| if (!parsed.success) return badRequestJson({ data: body, error: parsed.error.issues, success: false }) | ||
| return parsed.data | ||
| }) | ||
| } | ||
|
|
||
| function experimentalFailure(error: unknown) { | ||
| if (error instanceof NamedError) { | ||
| const status = error.name.startsWith("Worktree") ? 400 : 500 | ||
| return Effect.succeed(HttpServerResponse.jsonUnsafe(error.toObject(), { status })) | ||
| } | ||
| return Effect.succeed( | ||
| HttpServerResponse.jsonUnsafe( | ||
| new NamedError.Unknown({ message: "Unexpected server error. Check server logs for details." }).toObject(), | ||
| { status: 500 }, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| function jsonResponse<A>(effect: Effect.Effect<A, unknown, unknown>) { | ||
| return effect.pipe( | ||
| Effect.map((value) => HttpServerResponse.jsonUnsafe(value)), | ||
| Effect.catch(experimentalFailure), | ||
| Effect.catchDefect(experimentalFailure), | ||
| ) | ||
| } | ||
|
|
||
| export const experimentalHandlers = HttpApiBuilder.group(ExperimentalApi, "experimental", (handlers) => | ||
| handlers | ||
| .handleRaw("console", () => jsonResponse(getConsoleState())) | ||
| .handleRaw("consoleOrgs", () => jsonResponse(listConsoleOrgs())) | ||
| .handleRaw("consoleSwitch", (ctx) => | ||
| Effect.gen(function* () { | ||
| const payload = yield* parseJsonBody(ctx.request, ConsoleSwitchBody) | ||
| if (HttpServerResponse.isHttpServerResponse(payload)) return payload | ||
| return yield* jsonResponse(switchConsoleOrg(payload)) | ||
| }), | ||
| ) | ||
| .handleRaw("tool", (ctx) => | ||
| jsonResponse( | ||
| listTools({ | ||
| provider: ctx.query.provider, | ||
| model: ctx.query.model, | ||
| }), | ||
| ), | ||
| ) | ||
| .handleRaw("toolIds", () => jsonResponse(listToolIDs())) | ||
| .handleRaw("resource", () => jsonResponse(listResources())) | ||
| .handleRaw("worktreeCreate", (ctx) => | ||
| Effect.gen(function* () { | ||
| const payload = yield* parseJsonBody(ctx.request, WorktreeCreateBody) | ||
| if (HttpServerResponse.isHttpServerResponse(payload)) return payload | ||
| return yield* jsonResponse(createWorktree(payload)) | ||
| }), | ||
| ) | ||
| .handleRaw("worktreeList", () => jsonResponse(listWorktrees())) | ||
| .handleRaw("worktreeRemove", (ctx) => | ||
| Effect.gen(function* () { | ||
| const payload = yield* parseJsonBody(ctx.request, WorktreeDirectoryBody) | ||
| if (HttpServerResponse.isHttpServerResponse(payload)) return payload | ||
| return yield* jsonResponse(removeWorktree(payload)) | ||
| }), | ||
| ) | ||
| .handleRaw("worktreeReset", (ctx) => | ||
| Effect.gen(function* () { | ||
| const payload = yield* parseJsonBody(ctx.request, WorktreeDirectoryBody) | ||
| if (HttpServerResponse.isHttpServerResponse(payload)) return payload | ||
| return yield* jsonResponse(resetWorktree(payload)) | ||
| }), | ||
| ), | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.