opencode/packages/cli-rs/docs/F.2.md
Simon Klee 6954ec9bf2
docs(cli-rs): add the F.2 API type mapping note
How script/gen/api.ts maps ClientApi schemas to Rust types, unions, and methods.
2026-11-04 18:20:48 +01:00

4.9 KiB

F.2: type mapping of the generated API client

script/gen/api.ts calls compile(ClientApi, …) with the options of the Promise client (packages/client/script/generate.ts:56-61). It projects every schema to its encoded (wire) form, or to its decoded form for query fields as the Promise client does, normalizes declarations as codegenAsts does (packages/httpapi-codegen/src/index.ts:1507-1550), and feeds them to SchemaRepresentation.toRepresentations. The emitter in script/rust/ walks the representations and writes crates/api/src/generated/{types,methods}.rs.

Types

Representation Rust
Named reference to an object pub struct named by identifierPart($ref) (Session.Info becomes SessionInfo)
Named reference to a string (branded IDs, paths) newtype pub struct SessionID(pub String), #[serde(transparent)]
Named reference to anything else pub type alias, or the enum below
Anonymous object struct named <Parent><Field> (for union members <Parent><Tag>), deduplicated by name and shape
Object with index signatures only BTreeMap<String, T>
Object with fields and an index signature struct with #[serde(flatten)] extra: BTreeMap<String, T>
String, template literal, single string literal String
Number with an isInt check i64, else f64
Boolean bool
Unknown, Any, Schema.Json, other declarations serde_json::Value
Uint8Array (binary bodies and responses) bytes::Bytes
Array with rest only Vec<T>; a tuple becomes a Rust tuple
Fields of type Never dropped (they can only be absent)

Fields:

  • An optional field becomes Option<T> with default and skip_serializing_if = "Option::is_none".
  • A nullable field (T | null) becomes Option<T>. Optional and nullable together becomes Option<Option<T>> with de::double_option, so absent (None) and null (Some(None)) stay distinct.
  • Unknown fields are ignored (no deny_unknown_fields), so a newer server's additions decode.

Unions

Nested unions are flattened, and null and undefined members become Option. The rest falls in one of four kinds:

  1. String literals only. An enum with one unit variant per literal and Unknown(String). Its Deserialize and Serialize are written out by the emitter, so an unknown literal decodes.

  2. String literals and String (string suggestions). String.

  3. Objects that share a discriminator field whose value in every member is a literal, a union of literals, or a template literal with a literal prefix. Candidate keys in order: type, _tag, and then the first shared field that separates the members. The enum has one newtype variant per member and Unknown(serde_json::Value). Its Deserialize is generated by hand: it buffers a Value, reads the tag, matches exact literals first and then template prefixes (rpc. becomes the Rpc variant, which keeps the full name in its type field), and decodes the member with serde_json::from_value. A known tag that fails to decode is an error, not Unknown, so emitter mistakes show; an unknown tag or a missing tag is Unknown. Serialize writes the member as it is. Member structs keep the tag field as a String, so a variant shared by several tags still knows its tag.

    #[serde(tag = "...")] is not used: serde rejects a payload on #[serde(other)], and it cannot route a prefix (plan.review-carbon.md, carbon-3).

  4. Anything else (String | Object, Boolean | Object, objects told apart by required fields). An #[serde(untagged)] enum with variants in schema order and a last Unknown(serde_json::Value).

Methods

  • One async fn per endpoint on Client, named by the client path in snake case (session.message.get becomes session_message_get, experimental.session.log becomes experimental_session_log).
  • Inputs are flattened as in the Promise client: one <Prefix>Input struct with every params, query, headers, and payload field. An opaque payload is one payload field. A trailing * path adds a path field encoded per segment. An input whose fields are all optional derives Default.
  • Every method takes &RequestOptions (per-call headers).
  • { data } envelopes are unwrapped. A no-content endpoint returns (). A binary endpoint returns Bytes; a binary payload is sent as application/octet-stream.
  • An SSE endpoint returns SseResponse<T>: the checked response body and the event type. F.3 parses it into a stream of T.
  • Query values serialize to JSON values and are bracket-encoded like appendQuery (packages/client/src/promise/generated/client.ts:2795-2810).
  • A response with a declared status decodes into Error::Declared with the status, _tag, message, and the body as a Value; any other status is Error::UnexpectedStatus. Other failures map to the remaining ClientError reasons.