summaryrefslogtreecommitdiffhomepage
path: root/packages/opencode/src/server/routes/instance/httpapi/control.ts
blob: 14cbdf7c45397e6cb8bee9a142eb61719ac5b68a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Auth } from "@/auth"
import { ProviderID } from "@/provider/schema"
import { Schema } from "effect"
import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"

const AuthParams = Schema.Struct({
  providerID: ProviderID,
})

const LogQuery = Schema.Struct({
  directory: Schema.optional(Schema.String),
  workspace: Schema.optional(Schema.String),
})

const LogInput = Schema.Struct({
  service: Schema.String.annotate({ description: "Service name for the log entry" }),
  level: Schema.Union([
    Schema.Literal("debug"),
    Schema.Literal("info"),
    Schema.Literal("error"),
    Schema.Literal("warn"),
  ]).annotate({ description: "Log level" }),
  message: Schema.String.annotate({ description: "Log message" }),
  extra: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)).annotate({
    description: "Additional metadata for the log entry",
  }),
}).annotate({ identifier: "AppLogInput" })

export const ControlPaths = {
  auth: "/auth/:providerID",
  log: "/log",
} as const

export const ControlApi = HttpApi.make("control")
  .add(
    HttpApiGroup.make("control")
      .add(
        HttpApiEndpoint.put("authSet", ControlPaths.auth, {
          params: AuthParams,
          payload: Auth.Info,
          success: Schema.Boolean,
        }).annotateMerge(
          OpenApi.annotations({
            identifier: "auth.set",
            summary: "Set auth credentials",
            description: "Set authentication credentials",
          }),
        ),
        HttpApiEndpoint.delete("authRemove", ControlPaths.auth, {
          params: AuthParams,
          success: Schema.Boolean,
        }).annotateMerge(
          OpenApi.annotations({
            identifier: "auth.remove",
            summary: "Remove auth credentials",
            description: "Remove authentication credentials",
          }),
        ),
        HttpApiEndpoint.post("log", ControlPaths.log, {
          query: LogQuery,
          payload: LogInput,
          success: Schema.Boolean,
        }).annotateMerge(
          OpenApi.annotations({
            identifier: "app.log",
            summary: "Write log",
            description: "Write a log entry to the server logs with specified level and metadata.",
          }),
        ),
      )
      .annotateMerge(OpenApi.annotations({ title: "control", description: "Control plane routes." })),
  )