2026-04-24 22:03:51 +00:00
|
|
|
import * as InstanceState from "@/effect/instance-state"
|
2026-04-25 22:42:02 +00:00
|
|
|
import { AppRuntime } from "@/effect/app-runtime"
|
2026-04-17 03:48:12 +00:00
|
|
|
import { Project } from "@/project"
|
2026-04-25 22:42:02 +00:00
|
|
|
import { InstanceBootstrap } from "@/project/bootstrap"
|
2026-04-17 03:48:12 +00:00
|
|
|
import { Effect, Layer, Schema } from "effect"
|
|
|
|
|
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
2026-04-24 21:11:07 +00:00
|
|
|
import { Authorization } from "./auth"
|
2026-04-25 22:42:02 +00:00
|
|
|
import { markInstanceForReload } from "./lifecycle"
|
2026-04-17 03:48:12 +00:00
|
|
|
|
|
|
|
|
const root = "/project"
|
|
|
|
|
|
|
|
|
|
export const ProjectApi = HttpApi.make("project")
|
|
|
|
|
.add(
|
|
|
|
|
HttpApiGroup.make("project")
|
|
|
|
|
.add(
|
|
|
|
|
HttpApiEndpoint.get("list", root, {
|
|
|
|
|
success: Schema.Array(Project.Info),
|
|
|
|
|
}).annotateMerge(
|
|
|
|
|
OpenApi.annotations({
|
|
|
|
|
identifier: "project.list",
|
|
|
|
|
summary: "List all projects",
|
|
|
|
|
description: "Get a list of projects that have been opened with OpenCode.",
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
HttpApiEndpoint.get("current", `${root}/current`, {
|
|
|
|
|
success: Project.Info,
|
|
|
|
|
}).annotateMerge(
|
|
|
|
|
OpenApi.annotations({
|
|
|
|
|
identifier: "project.current",
|
|
|
|
|
summary: "Get current project",
|
|
|
|
|
description: "Retrieve the currently active project that OpenCode is working with.",
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-04-25 22:42:02 +00:00
|
|
|
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.",
|
|
|
|
|
}),
|
|
|
|
|
),
|
2026-04-17 03:48:12 +00:00
|
|
|
)
|
|
|
|
|
.annotateMerge(
|
|
|
|
|
OpenApi.annotations({
|
|
|
|
|
title: "project",
|
|
|
|
|
description: "Experimental HttpApi project routes.",
|
|
|
|
|
}),
|
2026-04-24 21:11:07 +00:00
|
|
|
)
|
|
|
|
|
.middleware(Authorization),
|
2026-04-17 03:48:12 +00:00
|
|
|
)
|
|
|
|
|
.annotateMerge(
|
|
|
|
|
OpenApi.annotations({
|
|
|
|
|
title: "opencode experimental HttpApi",
|
|
|
|
|
version: "0.0.1",
|
|
|
|
|
description: "Experimental HttpApi surface for selected instance routes.",
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const projectHandlers = Layer.unwrap(
|
|
|
|
|
Effect.gen(function* () {
|
|
|
|
|
const svc = yield* Project.Service
|
|
|
|
|
|
|
|
|
|
const list = Effect.fn("ProjectHttpApi.list")(function* () {
|
|
|
|
|
return yield* svc.list()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const current = Effect.fn("ProjectHttpApi.current")(function* () {
|
2026-04-24 22:03:51 +00:00
|
|
|
return (yield* InstanceState.context).project
|
2026-04-17 03:48:12 +00:00
|
|
|
})
|
|
|
|
|
|
2026-04-25 22:42:02 +00:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-17 03:48:12 +00:00
|
|
|
return HttpApiBuilder.group(ProjectApi, "project", (handlers) =>
|
2026-04-25 22:42:02 +00:00
|
|
|
handlers.handle("list", list).handle("current", current).handle("initGit", initGit),
|
2026-04-17 03:48:12 +00:00
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
).pipe(Layer.provide(Project.defaultLayer))
|