feat(httpapi): bridge project git init endpoint (#24394)

This commit is contained in:
Kit Langton 2026-04-25 18:42:02 -04:00 committed by GitHub
parent df9e1d9854
commit 5904f599a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 93 additions and 10 deletions

View file

@ -110,7 +110,7 @@ Good near-term candidates:
- top-level reads: `GET /path`, `GET /vcs`, `GET /vcs/diff`, `GET /command`, `GET /agent`, `GET /skill`, `GET /lsp`, `GET /formatter`
- simple mutations: `POST /instance/dispose`
- experimental JSON reads: console, tool, worktree list, resource list
- deferred JSON mutations: `PATCH /config`, project git init, workspace/worktree create/remove/reset, file search, MCP auth flows
- deferred JSON mutations: workspace/worktree create/remove/reset, file search, MCP auth flows
Keep large or stateful groups for later:
@ -176,7 +176,7 @@ Use raw Effect HTTP routes where `HttpApi` does not fit. The goal is deleting Ho
| `permission` | `bridged` | list and reply |
| `provider` | `bridged` | list, auth, OAuth authorize/callback |
| `config` | `bridged` | read, providers, update |
| `project` | `bridged` partial | reads only; git-init remains Hono |
| `project` | `bridged` | list, current, git init |
| `file` | `bridged` partial | find text/file/symbol, list/content/status |
| `mcp` | `bridged` partial | status only |
| `workspace` | `bridged` | list, get, enter |

View file

@ -3,6 +3,10 @@ import { Effect } from "effect"
import { HttpEffect, HttpMiddleware, HttpServerRequest } from "effect/unstable/http"
const disposeAfterResponse = new WeakMap<object, InstanceContext>()
const reloadAfterResponse = new WeakMap<
object,
InstanceContext & { next: Parameters<typeof Instance.reload>[0] }
>()
export const markInstanceForDisposal = (ctx: InstanceContext) =>
HttpEffect.appendPreResponseHandler((request, response) =>
@ -12,10 +16,25 @@ export const markInstanceForDisposal = (ctx: InstanceContext) =>
}),
)
export const markInstanceForReload = (ctx: InstanceContext, next: Parameters<typeof Instance.reload>[0]) =>
HttpEffect.appendPreResponseHandler((request, response) =>
Effect.sync(() => {
reloadAfterResponse.set(request.source, { ...ctx, next })
return response
}),
)
export const disposeMiddleware: HttpMiddleware.HttpMiddleware = (effect) =>
Effect.gen(function* () {
const response = yield* effect
const request = yield* HttpServerRequest.HttpServerRequest
const reload = reloadAfterResponse.get(request.source)
if (reload) {
reloadAfterResponse.delete(request.source)
yield* Effect.promise(() => Instance.restore(reload, () => Instance.reload(reload.next)))
return response
}
const ctx = disposeAfterResponse.get(request.source)
if (!ctx) return response
disposeAfterResponse.delete(request.source)

View file

@ -1,8 +1,11 @@
import * as InstanceState from "@/effect/instance-state"
import { AppRuntime } from "@/effect/app-runtime"
import { Project } from "@/project"
import { InstanceBootstrap } from "@/project/bootstrap"
import { Effect, Layer, Schema } from "effect"
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
import { Authorization } from "./auth"
import { markInstanceForReload } from "./lifecycle"
const root = "/project"
@ -28,6 +31,15 @@ export const ProjectApi = HttpApi.make("project")
description: "Retrieve the currently active project that OpenCode is working with.",
}),
),
HttpApiEndpoint.post("initGit", `${root}/git/init`, {
success: Project.Info,
}).annotateMerge(
OpenApi.annotations({
identifier: "project.initGit",
summary: "Initialize git repository",
description: "Create a git repository for the current project and return the refreshed project info.",
}),
),
)
.annotateMerge(
OpenApi.annotations({
@ -57,8 +69,21 @@ export const projectHandlers = Layer.unwrap(
return (yield* InstanceState.context).project
})
const initGit = Effect.fn("ProjectHttpApi.initGit")(function* () {
const ctx = yield* InstanceState.context
const next = yield* svc.initGit({ directory: ctx.directory, project: ctx.project })
if (next.id === ctx.project.id && next.vcs === ctx.project.vcs && next.worktree === ctx.project.worktree) return next
yield* markInstanceForReload(ctx, {
directory: ctx.directory,
worktree: ctx.directory,
project: next,
init: () => AppRuntime.runPromise(InstanceBootstrap),
})
return next
})
return HttpApiBuilder.group(ProjectApi, "project", (handlers) =>
handlers.handle("list", list).handle("current", current),
handlers.handle("list", list).handle("current", current).handle("initGit", initGit),
)
}),
).pipe(Layer.provide(Project.defaultLayer))

View file

@ -61,6 +61,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
app.post("/provider/:providerID/oauth/callback", (c) => handler(c.req.raw, context))
app.get("/project", (c) => handler(c.req.raw, context))
app.get("/project/current", (c) => handler(c.req.raw, context))
app.post("/project/git/init", (c) => handler(c.req.raw, context))
app.get(FilePaths.findText, (c) => handler(c.req.raw, context))
app.get(FilePaths.findFile, (c) => handler(c.req.raw, context))
app.get(FilePaths.findSymbol, (c) => handler(c.req.raw, context))

View file

@ -107,14 +107,16 @@ describe("experimental HttpApi", () => {
expect(listed.status).toBe(200)
expect(await listed.json()).toContain(info.directory)
const reset = await app().request(ExperimentalPaths.worktreeReset, {
method: "POST",
headers,
body: JSON.stringify({ directory: info.directory }),
})
if (process.platform !== "win32") {
const reset = await app().request(ExperimentalPaths.worktreeReset, {
method: "POST",
headers,
body: JSON.stringify({ directory: info.directory }),
})
expect(reset.status).toBe(200)
expect(await reset.json()).toBe(true)
expect(reset.status).toBe(200)
expect(await reset.json()).toBe(true)
}
const removed = await app().request(ExperimentalPaths.worktree, {
method: "DELETE",

View file

@ -20,6 +20,24 @@ function app() {
return InstanceRoutes(websocket)
}
async function waitDisposed(directory: string) {
return await new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => {
GlobalBus.off("event", onEvent)
reject(new Error("timed out waiting for instance disposal"))
}, 10_000)
function onEvent(event: { directory?: string; payload: { type?: string } }) {
if (event.payload.type !== "server.instance.disposed" || event.directory !== directory) return
clearTimeout(timer)
GlobalBus.off("event", onEvent)
resolve()
}
GlobalBus.on("event", onEvent)
})
}
afterEach(async () => {
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = original
await Instance.disposeAll()
@ -79,6 +97,24 @@ describe("instance HttpApi", () => {
expect(await formatter.json()).toEqual([])
})
test("serves project git init through Hono bridge", async () => {
await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
const disposed = waitDisposed(tmp.path)
const response = await app().request("/project/git/init", {
method: "POST",
headers: { "x-opencode-directory": tmp.path },
})
expect(response.status).toBe(200)
expect(await response.json()).toMatchObject({ vcs: "git", worktree: tmp.path })
await disposed
const current = await app().request("/project/current", { headers: { "x-opencode-directory": tmp.path } })
expect(current.status).toBe(200)
expect(await current.json()).toMatchObject({ vcs: "git", worktree: tmp.path })
})
test("serves instance dispose through Hono bridge", async () => {
await using tmp = await tmpdir()