mirror of
https://github.com/openai/codex.git
synced 2026-04-24 14:45:27 +00:00
This PR addresses a current hole in the TypeScript code generation for
the API server protocol. Fields that are marked as "Optional<>" in the
Rust code are serialized such that the value is omitted when it is
deserialized — appearing as `undefined`, but the TS type indicates
(incorrectly) that it is always defined but possibly `null`. This can
lead to subtle errors that the TypeScript compiler doesn't catch. The
fix is to include the `#[ts(optional_fields = nullable)]` macro for all
protocol structs that contain one or more `Optional<>` fields.
This PR also includes a new test that validates that all TS protocol
code containing "| null" in its type is marked optional ("?") to catch
cases where `#[ts(optional_fields = nullable)]` is omitted.
72 lines
2.1 KiB
Rust
72 lines
2.1 KiB
Rust
//! We do not do true JSON-RPC 2.0, as we neither send nor expect the
|
|
//! "jsonrpc": "2.0" field.
|
|
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use ts_rs::TS;
|
|
|
|
pub const JSONRPC_VERSION: &str = "2.0";
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Hash, Eq, JsonSchema, TS)]
|
|
#[serde(untagged)]
|
|
pub enum RequestId {
|
|
String(String),
|
|
#[ts(type = "number")]
|
|
Integer(i64),
|
|
}
|
|
|
|
pub type Result = serde_json::Value;
|
|
|
|
/// Refers to any valid JSON-RPC object that can be decoded off the wire, or encoded to be sent.
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
#[serde(untagged)]
|
|
pub enum JSONRPCMessage {
|
|
Request(JSONRPCRequest),
|
|
Notification(JSONRPCNotification),
|
|
Response(JSONRPCResponse),
|
|
Error(JSONRPCError),
|
|
}
|
|
|
|
/// A request that expects a response.
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
#[ts(optional_fields = nullable)]
|
|
pub struct JSONRPCRequest {
|
|
pub id: RequestId,
|
|
pub method: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub params: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// A notification which does not expect a response.
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
#[ts(optional_fields = nullable)]
|
|
pub struct JSONRPCNotification {
|
|
pub method: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub params: Option<serde_json::Value>,
|
|
}
|
|
|
|
/// A successful (non-error) response to a request.
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
pub struct JSONRPCResponse {
|
|
pub id: RequestId,
|
|
pub result: Result,
|
|
}
|
|
|
|
/// A response to a request that indicates an error occurred.
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
pub struct JSONRPCError {
|
|
pub error: JSONRPCErrorError,
|
|
pub id: RequestId,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema, TS)]
|
|
#[ts(optional_fields = nullable)]
|
|
pub struct JSONRPCErrorError {
|
|
pub code: i64,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub data: Option<serde_json::Value>,
|
|
pub message: String,
|
|
}
|