2026-06-25 02:15:31 +00:00
|
|
|
import { Context } from "effect"
|
|
|
|
|
import { HttpApi, HttpApiGroup, HttpApiMiddleware, OpenApi } from "effect/unstable/httpapi"
|
2026-07-01 17:53:09 +00:00
|
|
|
import { SchemaErrorMiddleware } from "./middleware/schema-error.js"
|
|
|
|
|
import { GenerateGroup } from "./groups/generate.js"
|
|
|
|
|
import { MessageGroup } from "./groups/message.js"
|
|
|
|
|
import { ModelGroup } from "./groups/model.js"
|
|
|
|
|
import { ProviderGroup } from "./groups/provider.js"
|
|
|
|
|
import { makeSessionGroup } from "./groups/session.js"
|
|
|
|
|
import { makePermissionGroup } from "./groups/permission.js"
|
|
|
|
|
import { FileSystemGroup } from "./groups/fs.js"
|
2026-07-03 07:40:44 +00:00
|
|
|
import { makeFormGroup } from "./groups/form.js"
|
2026-07-01 17:53:09 +00:00
|
|
|
import { CommandGroup } from "./groups/command.js"
|
|
|
|
|
import { SkillGroup } from "./groups/skill.js"
|
|
|
|
|
import { EventGroup, makeEventGroup } from "./groups/event.js"
|
2026-06-25 02:15:31 +00:00
|
|
|
import type { Definition } from "@opencode-ai/schema/event"
|
2026-07-01 17:53:09 +00:00
|
|
|
import { AgentGroup } from "./groups/agent.js"
|
|
|
|
|
import { PluginGroup } from "./groups/plugin.js"
|
|
|
|
|
import { HealthGroup } from "./groups/health.js"
|
2026-07-08 22:41:08 +00:00
|
|
|
import { ServerGroup } from "./groups/server.js"
|
2026-07-05 19:58:43 +00:00
|
|
|
import { DebugGroup } from "./groups/debug.js"
|
2026-07-01 17:53:09 +00:00
|
|
|
import { PtyGroup } from "./groups/pty.js"
|
|
|
|
|
import { ShellGroup } from "./groups/shell.js"
|
2026-07-03 05:32:10 +00:00
|
|
|
import { makeQuestionGroup } from "./groups/question.js"
|
2026-07-01 17:53:09 +00:00
|
|
|
import { ReferenceGroup } from "./groups/reference.js"
|
|
|
|
|
import { Authorization } from "./middleware/authorization.js"
|
|
|
|
|
import { LocationGroup } from "./groups/location.js"
|
|
|
|
|
import { IntegrationGroup } from "./groups/integration.js"
|
|
|
|
|
import { McpGroup } from "./groups/mcp.js"
|
|
|
|
|
import { CredentialGroup } from "./groups/credential.js"
|
|
|
|
|
import { ProjectGroup } from "./groups/project.js"
|
|
|
|
|
import { ProjectCopyGroup } from "./groups/project-copy.js"
|
2026-07-03 11:19:03 +00:00
|
|
|
import { VcsGroup } from "./groups/vcs.js"
|
2026-07-01 17:53:09 +00:00
|
|
|
|
|
|
|
|
type LocationGroups<LocationId extends HttpApiMiddleware.AnyId> =
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof LocationGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof AgentGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof PluginGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ModelGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof GenerateGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ProviderGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof IntegrationGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof McpGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof CredentialGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ProjectGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof FileSystemGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof CommandGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof SkillGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof PtyGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ShellGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ReferenceGroup, LocationId>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof ProjectCopyGroup, LocationId>
|
2026-07-03 11:19:03 +00:00
|
|
|
| HttpApiGroup.AddMiddleware<typeof VcsGroup, LocationId>
|
2026-07-01 17:53:09 +00:00
|
|
|
|
|
|
|
|
type SessionGroups<SessionLocationId extends HttpApiMiddleware.AnyId, SessionLocationService> =
|
|
|
|
|
| ReturnType<typeof makeSessionGroup<SessionLocationId, SessionLocationService>>
|
|
|
|
|
| HttpApiGroup.AddMiddleware<typeof MessageGroup, SessionLocationId>
|
|
|
|
|
|
2026-07-03 08:55:15 +00:00
|
|
|
type FormGroups<
|
|
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
|
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
|
|
|
|
> = ReturnType<typeof makeFormGroup<LocationId, LocationService, FormLocationId, FormLocationService>>
|
|
|
|
|
|
2026-07-01 17:53:09 +00:00
|
|
|
type MixedMiddlewareGroups<
|
|
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
|
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
> =
|
2026-07-17 14:31:27 +00:00
|
|
|
| ReturnType<typeof makePermissionGroup<LocationId, LocationService, SessionLocationId, SessionLocationService>>
|
2026-07-03 05:32:10 +00:00
|
|
|
| ReturnType<typeof makeQuestionGroup<LocationId, LocationService, SessionLocationId, SessionLocationService>>
|
2026-07-01 17:53:09 +00:00
|
|
|
|
|
|
|
|
type ApiGroups<
|
|
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
2026-07-03 08:55:15 +00:00
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
2026-07-01 17:53:09 +00:00
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
2026-07-17 14:31:27 +00:00
|
|
|
Event extends HttpApiGroup.Constraint,
|
2026-07-01 17:53:09 +00:00
|
|
|
> =
|
|
|
|
|
| typeof HealthGroup
|
2026-07-08 22:41:08 +00:00
|
|
|
| typeof ServerGroup
|
2026-07-05 19:58:43 +00:00
|
|
|
| typeof DebugGroup
|
2026-07-01 17:53:09 +00:00
|
|
|
| LocationGroups<LocationId>
|
2026-07-03 08:55:15 +00:00
|
|
|
| FormGroups<LocationId, LocationService, FormLocationId, FormLocationService>
|
2026-07-01 17:53:09 +00:00
|
|
|
| SessionGroups<SessionLocationId, SessionLocationService>
|
|
|
|
|
| MixedMiddlewareGroups<LocationId, LocationService, SessionLocationId, SessionLocationService>
|
|
|
|
|
| Event
|
|
|
|
|
|
|
|
|
|
type EventGroupFor<Definitions extends ReadonlyArray<Definition>> = ReturnType<typeof makeEventGroup<Definitions>>
|
|
|
|
|
|
|
|
|
|
export type Api<
|
|
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
2026-07-03 08:55:15 +00:00
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
2026-07-01 17:53:09 +00:00
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
2026-07-17 14:31:27 +00:00
|
|
|
Event extends HttpApiGroup.Constraint,
|
2026-07-01 17:53:09 +00:00
|
|
|
> = HttpApi.HttpApi<
|
|
|
|
|
"server",
|
|
|
|
|
HttpApiGroup.AddMiddleware<
|
|
|
|
|
HttpApiGroup.AddMiddleware<
|
2026-07-03 08:55:15 +00:00
|
|
|
ApiGroups<
|
|
|
|
|
LocationId,
|
|
|
|
|
LocationService,
|
|
|
|
|
FormLocationId,
|
|
|
|
|
FormLocationService,
|
|
|
|
|
SessionLocationId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
Event
|
|
|
|
|
>,
|
2026-07-01 17:53:09 +00:00
|
|
|
Authorization
|
|
|
|
|
>,
|
|
|
|
|
SchemaErrorMiddleware
|
|
|
|
|
>
|
|
|
|
|
>
|
2026-06-25 02:15:31 +00:00
|
|
|
|
|
|
|
|
// Protocol owns middleware placement, while Server injects concrete keys so Core service identities stay downstream.
|
|
|
|
|
const makeApiFromGroup = <
|
2026-07-17 14:31:27 +00:00
|
|
|
const Group extends HttpApiGroup.Constraint,
|
2026-06-25 02:15:31 +00:00
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
2026-07-03 08:55:15 +00:00
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
2026-06-25 02:15:31 +00:00
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
>(
|
|
|
|
|
eventGroup: Group,
|
|
|
|
|
locationMiddleware: Context.Key<LocationId, LocationService>,
|
2026-07-03 08:55:15 +00:00
|
|
|
formLocationMiddleware: Context.Key<FormLocationId, FormLocationService>,
|
2026-06-25 02:15:31 +00:00
|
|
|
sessionLocationMiddleware: Context.Key<SessionLocationId, SessionLocationService>,
|
2026-07-03 08:55:15 +00:00
|
|
|
): Api<
|
|
|
|
|
LocationId,
|
|
|
|
|
LocationService,
|
|
|
|
|
FormLocationId,
|
|
|
|
|
FormLocationService,
|
|
|
|
|
SessionLocationId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
Group
|
|
|
|
|
> =>
|
2026-06-25 02:15:31 +00:00
|
|
|
HttpApi.make("server")
|
|
|
|
|
.add(HealthGroup)
|
2026-07-08 22:41:08 +00:00
|
|
|
.add(ServerGroup)
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(LocationGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(AgentGroup.middleware(locationMiddleware))
|
2026-07-01 04:53:22 +00:00
|
|
|
.add(PluginGroup.middleware(locationMiddleware))
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(makeSessionGroup(sessionLocationMiddleware))
|
|
|
|
|
.add(MessageGroup.middleware(sessionLocationMiddleware))
|
|
|
|
|
.add(ModelGroup.middleware(locationMiddleware))
|
2026-06-29 02:43:58 +00:00
|
|
|
.add(GenerateGroup.middleware(locationMiddleware))
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(ProviderGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(IntegrationGroup.middleware(locationMiddleware))
|
2026-06-30 13:37:44 +00:00
|
|
|
.add(McpGroup.middleware(locationMiddleware))
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(CredentialGroup.middleware(locationMiddleware))
|
2026-06-29 18:10:11 +00:00
|
|
|
.add(ProjectGroup.middleware(locationMiddleware))
|
2026-07-03 08:55:15 +00:00
|
|
|
.add(makeFormGroup(locationMiddleware, formLocationMiddleware))
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(makePermissionGroup(locationMiddleware, sessionLocationMiddleware))
|
|
|
|
|
.add(FileSystemGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(CommandGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(SkillGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(eventGroup)
|
|
|
|
|
.add(PtyGroup.middleware(locationMiddleware))
|
2026-06-29 04:43:04 +00:00
|
|
|
.add(ShellGroup.middleware(locationMiddleware))
|
2026-07-03 05:32:10 +00:00
|
|
|
.add(makeQuestionGroup(locationMiddleware, sessionLocationMiddleware))
|
2026-06-25 02:15:31 +00:00
|
|
|
.add(ReferenceGroup.middleware(locationMiddleware))
|
|
|
|
|
.add(ProjectCopyGroup.middleware(locationMiddleware))
|
2026-07-03 11:19:03 +00:00
|
|
|
.add(VcsGroup.middleware(locationMiddleware))
|
2026-07-05 19:58:43 +00:00
|
|
|
.add(DebugGroup)
|
2026-06-25 02:15:31 +00:00
|
|
|
.annotateMerge(
|
|
|
|
|
OpenApi.annotations({
|
|
|
|
|
title: "opencode HttpApi",
|
|
|
|
|
version: "0.0.1",
|
|
|
|
|
description: "Experimental HttpApi surface for selected instance routes.",
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.middleware(Authorization)
|
|
|
|
|
.middleware(SchemaErrorMiddleware)
|
|
|
|
|
|
|
|
|
|
export const makeApi = <
|
2026-07-01 17:53:09 +00:00
|
|
|
const Definitions extends ReadonlyArray<Definition>,
|
2026-06-25 02:15:31 +00:00
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
2026-07-03 08:55:15 +00:00
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
2026-06-25 02:15:31 +00:00
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
>(options: {
|
2026-07-01 17:53:09 +00:00
|
|
|
readonly definitions: Definitions
|
2026-06-25 02:15:31 +00:00
|
|
|
readonly locationMiddleware: Context.Key<LocationId, LocationService>
|
2026-07-03 08:55:15 +00:00
|
|
|
readonly formLocationMiddleware: Context.Key<FormLocationId, FormLocationService>
|
2026-06-25 02:15:31 +00:00
|
|
|
readonly sessionLocationMiddleware: Context.Key<SessionLocationId, SessionLocationService>
|
2026-07-03 08:55:15 +00:00
|
|
|
}): Api<
|
|
|
|
|
LocationId,
|
|
|
|
|
LocationService,
|
|
|
|
|
FormLocationId,
|
|
|
|
|
FormLocationService,
|
|
|
|
|
SessionLocationId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
EventGroupFor<Definitions>
|
|
|
|
|
> =>
|
|
|
|
|
makeApiFromGroup(
|
|
|
|
|
makeEventGroup(options.definitions),
|
|
|
|
|
options.locationMiddleware,
|
|
|
|
|
options.formLocationMiddleware,
|
|
|
|
|
options.sessionLocationMiddleware,
|
|
|
|
|
)
|
2026-06-25 02:15:31 +00:00
|
|
|
|
|
|
|
|
export const makeDefaultApi = <
|
|
|
|
|
LocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
LocationService,
|
2026-07-03 08:55:15 +00:00
|
|
|
FormLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
FormLocationService,
|
2026-06-25 02:15:31 +00:00
|
|
|
SessionLocationId extends HttpApiMiddleware.AnyId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
>(options: {
|
|
|
|
|
readonly locationMiddleware: Context.Key<LocationId, LocationService>
|
2026-07-03 08:55:15 +00:00
|
|
|
readonly formLocationMiddleware: Context.Key<FormLocationId, FormLocationService>
|
2026-06-25 02:15:31 +00:00
|
|
|
readonly sessionLocationMiddleware: Context.Key<SessionLocationId, SessionLocationService>
|
2026-07-03 08:55:15 +00:00
|
|
|
}): Api<
|
|
|
|
|
LocationId,
|
|
|
|
|
LocationService,
|
|
|
|
|
FormLocationId,
|
|
|
|
|
FormLocationService,
|
|
|
|
|
SessionLocationId,
|
|
|
|
|
SessionLocationService,
|
|
|
|
|
typeof EventGroup
|
|
|
|
|
> =>
|
|
|
|
|
makeApiFromGroup(
|
|
|
|
|
EventGroup,
|
|
|
|
|
options.locationMiddleware,
|
|
|
|
|
options.formLocationMiddleware,
|
|
|
|
|
options.sessionLocationMiddleware,
|
|
|
|
|
)
|