GeezerVison is showing github.com — docs(protocol): describe API request parameters (#65841) · erickwill/opencode@91365c8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/protocol/openapi.json +414-236Lines changed: 414 additions & 236 deletions Collapse file Copy file name to clipboardExpand all lines: packages/protocol/script/generate-openapi.ts +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,12 +2,16 @@ import { OpenApi } from "effect/http-api" | 2 | 2 | import { format } from "prettier" | 3 | 3 | import { fileURLToPath } from "url" | 4 | 4 | import { ClientApi } from "../src/client.js" | | 5 | +import { liftParameterDescriptions } from "./openapi-parameters.js" | 5 | 6 | import { stabilizeOpenApi } from "./openapi-stabilize.js" | 6 | 7 |
| 7 | | -const document = await format(JSON.stringify(stabilizeOpenApi(OpenApi.fromApi(ClientApi)), null, 2), { | 8 | | - parser: "json", | 9 | | - printWidth: 120, | 10 | | -}) | | 8 | +const document = await format( | | 9 | + JSON.stringify(liftParameterDescriptions(stabilizeOpenApi(OpenApi.fromApi(ClientApi))), null, 2), | | 10 | + { | | 11 | + parser: "json", | | 12 | + printWidth: 120, | | 13 | + }, | | 14 | +) | 11 | 15 | const target = fileURLToPath(new URL("../openapi.json", import.meta.url)) | 12 | 16 |
| 13 | 17 | if (process.argv.includes("--check")) { | |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +type Schema = { description?: string; allOf?: Array<Record<string, unknown>>; [key: string]: unknown } | | 2 | +type Parameter = { description?: string; schema?: Schema; [key: string]: unknown } | | 3 | +type Document = { paths?: Record<string, Record<string, { parameters?: Array<Parameter> }>> } | | 4 | + | | 5 | +// Effect writes a parameter's description into its schema: `.annotate` sets `schema.description`, and `.annotateKey` | | 6 | +// (used where annotating would rename a shared component) adds a `{ description }` member to `schema.allOf`. OpenAPI | | 7 | +// documents parameters by `parameters[].description`, so move it there. | | 8 | +export function liftParameterDescriptions<T extends object>(document: T): T { | | 9 | + for (const operations of Object.values((document as Document).paths ?? {})) | | 10 | + for (const operation of Object.values(operations)) | | 11 | + operation.parameters = operation.parameters?.map((parameter) => { | | 12 | + if (parameter.schema === undefined || parameter.description !== undefined) return parameter | | 13 | + const description = parameter.schema.allOf?.find(isDescription)?.description ?? parameter.schema.description | | 14 | + if (description === undefined) return parameter | | 15 | + return { ...parameter, description, schema: withoutDescription(parameter.schema) } | | 16 | + }) | | 17 | + return document | | 18 | +} | | 19 | + | | 20 | +function isDescription(member: Record<string, unknown>): member is { description: string } { | | 21 | + return Object.keys(member).length === 1 && typeof member.description === "string" | | 22 | +} | | 23 | + | | 24 | +function withoutDescription(schema: Schema): Schema { | | 25 | + const rest = Object.fromEntries(Object.entries(schema).filter(([key]) => key !== "description" && key !== "allOf")) | | 26 | + const allOf = schema.allOf?.filter((member) => !isDescription(member)) ?? [] | | 27 | + if (allOf.length === 0) return rest | | 28 | + if (allOf.length === 1 && Object.keys(rest).length === 0) return allOf[0] | | 29 | + return { ...rest, allOf } | | 30 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/location.ts +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,9 +6,12 @@ import { ServiceUnavailableError } from "../errors.js" | 6 | 6 | export const LocationQuery = Schema.Struct({ | 7 | 7 | location: Schema.optional( | 8 | 8 | Schema.Struct({ | 9 | | - directory: Schema.optional(Schema.String), | | 9 | + directory: Schema.optional(Schema.String).annotate({ description: "Absolute path of the directory." }), | 10 | 10 | }), | 11 | | - ), | | 11 | + ).annotateKey({ | | 12 | + description: | | 13 | + "Location to run the request in, sent as location[directory]. Omit to use the server's default location.", | | 14 | + }), | 12 | 15 | }).annotate({ identifier: "LocationQuery" }) | 13 | 16 |
| 14 | 17 | export const locationQueryOpenApi = OpenApi.annotations({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/mcp.ts +16-4Lines changed: 16 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,7 @@ import { Context, Schema } from "effect" | 5 | 5 | import { HttpApiEndpoint, HttpApiGroup, HttpApiMiddleware, HttpApiSchema, OpenApi } from "effect/http-api" | 6 | 6 | import { ForbiddenError, LocationNotFoundError, McpServerNotFoundError } from "../errors.js" | 7 | 7 | import { LocationQuery, locationQueryOpenApi } from "./location.js" | 8 | | -import { PTY_CONNECT_TICKET_QUERY, PTY_CONNECT_TOKEN_HEADER } from "./pty.js" | | 8 | +import { PTY_CONNECT_TICKET_QUERY, PTY_CONNECT_TOKEN_HEADER, PTY_CONNECT_TOKEN_HEADER_VALUE } from "./pty.js" | 9 | 9 |
| 10 | 10 | export const MCP_CLIENT_PATH = "/api/experimental/client-mcp" | 11 | 11 |
| @@ -15,7 +15,9 @@ export function hasMcpClientTicketURL(url: URL) { | 15 | 15 |
| 16 | 16 | const McpClientQuery = Schema.Struct({ | 17 | 17 | ...LocationQuery.fields, | 18 | | - name: Schema.String.check(Schema.isPattern(/^[a-zA-Z0-9_-]+$/)), | | 18 | + name: Schema.String.check(Schema.isPattern(/^[a-zA-Z0-9_-]+$/)).annotateKey({ | | 19 | + description: "Name of the MCP server the client hosts.", | | 20 | + }), | 19 | 21 | }) | 20 | 22 |
| 21 | 23 | export const makeMcpGroup = <LocationId extends HttpApiMiddleware.AnyId, LocationService>( | @@ -119,7 +121,11 @@ export const makeMcpGroup = <LocationId extends HttpApiMiddleware.AnyId, Locatio | 119 | 121 | .add( | 120 | 122 | HttpApiEndpoint.post("mcp.clientToken", `${MCP_CLIENT_PATH}/token`, { | 121 | 123 | query: McpClientQuery, | 122 | | - headers: Schema.Struct({ [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String) }), | | 124 | + headers: Schema.Struct({ | | 125 | + [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String).annotateKey({ | | 126 | + description: `Must be ${PTY_CONNECT_TOKEN_HEADER_VALUE}; requests without it are refused.`, | | 127 | + }), | | 128 | + }), | 123 | 129 | success: Location.response(PtyTicket.ConnectToken), | 124 | 130 | error: ForbiddenError, | 125 | 131 | }) | @@ -137,7 +143,13 @@ export const makeMcpGroup = <LocationId extends HttpApiMiddleware.AnyId, Locatio | 137 | 143 | .middleware(locationMiddleware) | 138 | 144 | .add( | 139 | 145 | HttpApiEndpoint.get("mcp.client", MCP_CLIENT_PATH, { | 140 | | - query: Schema.Struct({ ...McpClientQuery.fields, [PTY_CONNECT_TICKET_QUERY]: Schema.optional(Schema.String) }), | | 146 | + query: Schema.Struct({ | | 147 | + ...McpClientQuery.fields, | | 148 | + [PTY_CONNECT_TICKET_QUERY]: Schema.optional(Schema.String).annotateKey({ | | 149 | + description: | | 150 | + "Single-use ticket from experimental.mcp.client.token, which authorizes the connection without the usual credentials.", | | 151 | + }), | | 152 | + }), | 141 | 153 | success: Schema.Boolean, | 142 | 154 | error: [ForbiddenError, LocationNotFoundError], | 143 | 155 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/permission.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,11 @@ export const makePermissionGroup = < | 42 | 42 | ) | 43 | 43 | .add( | 44 | 44 | HttpApiEndpoint.get("permission.saved.list", "/api/permission/saved", { | 45 | | - query: Schema.Struct({ projectID: Project.ID.pipe(Schema.optional) }), | | 45 | + query: Schema.Struct({ | | 46 | + projectID: Project.ID.pipe(Schema.optional).annotateKey({ | | 47 | + description: "Project whose saved permissions to list. Defaults to the project of the requested location.", | | 48 | + }), | | 49 | + }), | 46 | 50 | success: Schema.Struct({ data: Schema.Array(PermissionSaved.Info) }), | 47 | 51 | }) | 48 | 52 | .middleware(locationMiddleware) | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/persistent-pty.ts +18-5Lines changed: 18 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,10 @@ export const PersistentPtyGroup = HttpApiGroup.make("server.experimental") | 31 | 31 | HttpApiEndpoint.get("persistentPty.read", "/api/experimental/session/:sessionID/terminal/read", { | 32 | 32 | params: { sessionID: Session.ID }, | 33 | 33 | query: { | 34 | | - lines: Schema.NumberFromString.pipe(Schema.decodeTo(PersistentPty.ReadLines), Schema.optional), | | 34 | + lines: Schema.NumberFromString.pipe(Schema.decodeTo(PersistentPty.ReadLines), Schema.optional).annotateKey({ | | 35 | + description: | | 36 | + "Number of trailing rows to read, up to 65535. Defaults to the live terminal height; larger counts include retained history.", | | 37 | + }), | 35 | 38 | }, | 36 | 39 | success: Schema.Struct({ data: Schema.NullOr(PersistentPty.ReadResult) }), | 37 | 40 | error: [ServiceUnavailableError], | @@ -109,7 +112,11 @@ export const PersistentPtyGroup = HttpApiGroup.make("server.experimental") | 109 | 112 | .add( | 110 | 113 | HttpApiEndpoint.post("persistentPty.connectToken", "/api/experimental/persistent-pty/:ptyID/connect-token", { | 111 | 114 | params: { ptyID: Pty.ID }, | 112 | | - headers: Schema.Struct({ [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String) }), | | 115 | + headers: Schema.Struct({ | | 116 | + [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String).annotateKey({ | | 117 | + description: `Must be ${PTY_CONNECT_TOKEN_HEADER_VALUE}; requests without it are refused.`, | | 118 | + }), | | 119 | + }), | 113 | 120 | success: Schema.Struct({ data: PtyTicket.ConnectToken }), | 114 | 121 | error: [ForbiddenError, PtyNotFoundError, ServiceUnavailableError], | 115 | 122 | }).annotateMerge(OpenApi.annotations({ identifier: "experimental.persistentPty.connectToken" })), | @@ -129,9 +136,15 @@ export const PersistentPtyGroup = HttpApiGroup.make("server.experimental") | 129 | 136 | "x-websocket": true, | 130 | 137 | parameters: [ | 131 | 138 | ...(operation.parameters ?? []), | 132 | | - ...["cursor", "role", "attachment_id", "takeover", "input_protocol", PTY_CONNECT_TICKET_QUERY].map( | 133 | | - (name) => ({ in: "query", name, schema: { type: "string" } }), | 134 | | - ), | | 139 | + { in: "query", name: "attachment_id", schema: { type: "string" } }, | | 140 | + ...Object.entries({ | | 141 | + cursor: "Output offset to replay from. Defaults to 0.", | | 142 | + role: "Attach as controller (the default) or observer.", | | 143 | + takeover: "Set to true to take over control of the terminal.", | | 144 | + input_protocol: "Set to 1 to send framed input messages instead of raw terminal input.", | | 145 | + [PTY_CONNECT_TICKET_QUERY]: | | 146 | + "Single-use ticket from experimental.persistentPty.connectToken, which authorizes the connection without the usual credentials.", | | 147 | + }).map(([name, description]) => ({ in: "query", name, description, schema: { type: "string" } })), | 135 | 148 | ], | 136 | 149 | }), | 137 | 150 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/project.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,11 @@ const BooleanFromString = Schema.Literals(["true", "false"]).pipe( | 15 | 15 | export const ProjectGroup = HttpApiGroup.make("server.project") | 16 | 16 | .add( | 17 | 17 | HttpApiEndpoint.get("project.list", root, { | 18 | | - query: Schema.Struct({ archived: BooleanFromString.pipe(Schema.optional) }), | | 18 | + query: Schema.Struct({ | | 19 | + archived: BooleanFromString.pipe(Schema.optional).annotateKey({ | | 20 | + description: "Set to true to include archived projects.", | | 21 | + }), | | 22 | + }), | 19 | 23 | success: Schema.Array(Project.Info), | 20 | 24 | }).annotateMerge( | 21 | 25 | OpenApi.annotations({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/pty.ts +13-6Lines changed: 13 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,7 +103,11 @@ export const makePtyGroup = <LocationId extends HttpApiMiddleware.AnyId, Locatio | 103 | 103 | HttpApiEndpoint.post("pty.connectToken", "/api/pty/:ptyID/connect-token", { | 104 | 104 | params: { ptyID: Pty.ID }, | 105 | 105 | query: LocationQuery, | 106 | | - headers: Schema.Struct({ [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String) }), | | 106 | + headers: Schema.Struct({ | | 107 | + [PTY_CONNECT_TOKEN_HEADER]: Schema.optional(Schema.String).annotateKey({ | | 108 | + description: `Must be ${PTY_CONNECT_TOKEN_HEADER_VALUE}; requests without it are refused.`, | | 109 | + }), | | 110 | + }), | 107 | 111 | success: Location.response(PtyTicket.ConnectToken), | 108 | 112 | error: [ForbiddenError, PtyNotFoundError], | 109 | 113 | }) | @@ -133,11 +137,14 @@ export const makePtyGroup = <LocationId extends HttpApiMiddleware.AnyId, Locatio | 133 | 137 | "x-websocket": true, | 134 | 138 | parameters: [ | 135 | 139 | ...(operation.parameters ?? []), | 136 | | - ...["location[directory]", "cursor", PTY_CONNECT_TICKET_QUERY].map((name) => ({ | 137 | | - in: "query", | 138 | | - name, | 139 | | - schema: { type: "string" }, | 140 | | - })), | | 140 | + ...Object.entries({ | | 141 | + "location[directory]": | | 142 | + "Absolute path of the location directory. Omit to use the server's default location.", | | 143 | + cursor: | | 144 | + "Output offset to replay from. Use -1 to start at the current end; omit to replay all retained output.", | | 145 | + [PTY_CONNECT_TICKET_QUERY]: | | 146 | + "Single-use ticket from pty.connect.token, which authorizes the connection without the usual credentials.", | | 147 | + }).map(([name, description]) => ({ in: "query", name, description, schema: { type: "string" } })), | 141 | 148 | ], | 142 | 149 | }), | 143 | 150 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/protocol/src/groups/session.ts +53-10Lines changed: 53 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -116,7 +116,23 @@ export const SessionsQuery = Schema.Struct({ | 116 | 116 | description: "Maximum number of sessions to return. Defaults to the newest 50 sessions.", | 117 | 117 | }), | 118 | 118 | ...Session.ListFilter.fields, | 119 | | - cursor: Session.ListCursor.pipe(Schema.optional), | | 119 | + search: Session.ListFilter.fields.search.annotateKey({ | | 120 | + description: "Only sessions whose title contains this text, ignoring case. % and _ act as wildcards.", | | 121 | + }), | | 122 | + archived: Session.ListFilter.fields.archived.annotateKey({ | | 123 | + description: | | 124 | + "Filter by archive state. Use true to return only archived sessions or false to exclude them. Omit to include both.", | | 125 | + }), | | 126 | + directory: Session.ListFilter.fields.directory.annotateKey({ | | 127 | + description: "Only sessions whose directory is exactly this absolute path.", | | 128 | + }), | | 129 | + subpath: Session.ListFilter.fields.subpath.annotateKey({ | | 130 | + description: "With project, only sessions at this path relative to the project directory.", | | 131 | + }), | | 132 | + cursor: Session.ListCursor.pipe(Schema.optional).annotateKey({ | | 133 | + description: | | 134 | + "Opaque cursor from a previous page's cursor.next or cursor.previous. It carries that page's filters and order, so other filters are ignored.", | | 135 | + }), | 120 | 136 | }).annotate({ identifier: "SessionsQuery" }) | 121 | 137 |
| 122 | 138 | export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI extends HttpApiMiddleware.AnyId, FormS>( | @@ -147,11 +163,21 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext | 147 | 163 | .add( | 148 | 164 | HttpApiEndpoint.get("session.stats", "/api/experimental/session/stats", { | 149 | 165 | query: Schema.Struct({ | 150 | | - from: Schema.NumberFromString.pipe(Schema.optional), | 151 | | - to: Schema.NumberFromString.pipe(Schema.optional), | | 166 | + from: Schema.NumberFromString.pipe(Schema.optional).annotateKey({ | | 167 | + description: | | 168 | + "Start of the range in Unix milliseconds, inclusive. Defaults to the earliest recorded message.", | | 169 | + }), | | 170 | + to: Schema.NumberFromString.pipe(Schema.optional).annotateKey({ | | 171 | + description: "End of the range in Unix milliseconds, exclusive. Defaults to now.", | | 172 | + }), | 152 | 173 | project: Project.ID.pipe(Schema.optional), | 153 | | - timezone: Schema.String.pipe(Schema.optional), | 154 | | - tools: SessionStats.ToolMode.pipe(Schema.optional), | | 174 | + timezone: Schema.String.pipe(Schema.optional).annotateKey({ | | 175 | + description: "IANA time zone used to group activity by day. Defaults to UTC.", | | 176 | + }), | | 177 | + tools: SessionStats.ToolMode.pipe(Schema.optional).annotateKey({ | | 178 | + description: | | 179 | + "How much tool usage to report: none, summary totals, or per-tool detail. Defaults to summary.", | | 180 | + }), | 155 | 181 | }), | 156 | 182 | success: Schema.Struct({ data: SessionStats.Info }), | 157 | 183 | error: InvalidRequestError, | @@ -219,7 +245,11 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext | 219 | 245 | .add( | 220 | 246 | HttpApiEndpoint.get("session.export", "/api/experimental/session/:sessionID/export", { | 221 | 247 | params: { sessionID: Session.ID }, | 222 | | - query: Schema.Struct({ sanitize: BooleanFromString.pipe(Schema.optional) }), | | 248 | + query: Schema.Struct({ | | 249 | + sanitize: BooleanFromString.pipe(Schema.optional).annotateKey({ | | 250 | + description: "Set to true to redact titles, message text, file data, paths and metadata from the export.", | | 251 | + }), | | 252 | + }), | 223 | 253 | success: Schema.Struct({ data: PublicSessionTransfer }), | 224 | 254 | error: [SessionNotFoundError, UnknownError], | 225 | 255 | }).annotateMerge( | @@ -697,8 +727,12 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext | 697 | 727 | HttpApiEndpoint.get("session.log", "/api/experimental/session/:sessionID/log", { | 698 | 728 | params: { sessionID: Session.ID }, | 699 | 729 | query: { | 700 | | - after: Schema.NumberFromString.pipe(Schema.decodeTo(Event.Seq), Schema.optional), | 701 | | - follow: BooleanFromString.pipe(Schema.optional), | | 730 | + after: Schema.NumberFromString.pipe(Schema.decodeTo(Event.Seq), Schema.optional).annotateKey({ | | 731 | + description: "Only return events after this sequence number.", | | 732 | + }), | | 733 | + follow: BooleanFromString.pipe(Schema.optional).annotateKey({ | | 734 | + description: "Set to true to keep the stream open and send live events after the stored ones.", | | 735 | + }), | 702 | 736 | }, | 703 | 737 | success: HttpApiSchema.StreamSse({ | 704 | 738 | data: Schema.Union([SessionEvent.Durable, EventLog.Synced]).annotate({ identifier: "SessionLogItem" }), | @@ -716,7 +750,12 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext | 716 | 750 | .add( | 717 | 751 | HttpApiEndpoint.post("session.interrupt", "/api/session/:sessionID/interrupt", { | 718 | 752 | params: { sessionID: Session.ID }, | 719 | | - query: { resume: BooleanFromString.pipe(Schema.optional) }, | | 753 | + query: { | | 754 | + resume: BooleanFromString.pipe(Schema.optional).annotateKey({ | | 755 | + description: | | 756 | + "Set to true to resume pending steering input and next-in-line control items after interrupting.", | | 757 | + }), | | 758 | + }, | 720 | 759 | success: Schema.Struct({ | 721 | 760 | interrupted: Schema.Boolean.annotate({ | 722 | 761 | description: "Whether an active execution owned by this OpenCode process was interrupted.", | @@ -824,7 +863,11 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext | 824 | 863 | .add( | 825 | 864 | HttpApiEndpoint.delete("session.form.cancel", "/api/session/:sessionID/form/:formID", { | 826 | 865 | params: { sessionID: Schema.String, formID: Form.ID }, | 827 | | - query: Schema.Struct({ message: Schema.optional(Schema.String) }), | | 866 | + query: Schema.Struct({ | | 867 | + message: Schema.optional(Schema.String).annotateKey({ | | 868 | + description: "Why the form was not answered, passed on to the asker.", | | 869 | + }), | | 870 | + }), | 828 | 871 | success: HttpApiSchema.NoContent, | 829 | 872 | error: [SessionNotFoundError, FormAlreadySettledError, FormNotFoundError], | 830 | 873 | }) | |
|