From 34e9ec7d47adcf08866005a3a014cc2c001eecd2 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Wed, 4 Nov 2026 18:10:58 +0100 Subject: [PATCH] test(cli-rs): prove API requests and decoding against the TS client A P-ORACLE fixture records the requests the TypeScript Promise client builds; the generated Rust client must build the same ones, and query::append must match appendQuery. Unit tests cover tagged unions (known, rpc.*, unknown), literal enums, double options, declared errors, binary bodies, auth, and the event stream through F.3's SSE reader. --- packages/cli-rs/crates/api/src/error.rs | 10 + .../cli-rs/crates/api/src/generated/types.rs | 746 +++++++++++++++++- packages/cli-rs/crates/api/src/transport.rs | 22 + packages/cli-rs/crates/api/tests/api.rs | 217 +++++ packages/cli-rs/crates/api/tests/oracle.rs | 144 ++++ .../cli-rs/parity/fixtures/F.2/requests.json | 158 ++++ packages/cli-rs/parity/oracle/F.2-requests.ts | 93 +++ packages/cli-rs/script/rust/types.ts | 10 +- 8 files changed, 1375 insertions(+), 25 deletions(-) create mode 100644 packages/cli-rs/crates/api/tests/api.rs create mode 100644 packages/cli-rs/crates/api/tests/oracle.rs create mode 100644 packages/cli-rs/parity/fixtures/F.2/requests.json create mode 100644 packages/cli-rs/parity/oracle/F.2-requests.ts diff --git a/packages/cli-rs/crates/api/src/error.rs b/packages/cli-rs/crates/api/src/error.rs index 9ce3d5f6195..388d506200e 100644 --- a/packages/cli-rs/crates/api/src/error.rs +++ b/packages/cli-rs/crates/api/src/error.rs @@ -48,6 +48,16 @@ impl Error { } } +impl From for Error { + fn from(error: crate::sse::Error) -> Self { + match error { + crate::sse::Error::Transport(cause) => Self::Transport(cause), + crate::sse::Error::MalformedResponse(cause) => Self::MalformedResponse(cause), + crate::sse::Error::EventTooLarge => Self::SseEventTooLarge, + } + } +} + /// A declared error body. The tag is `_tag`, else `name`; the message is `message`, else `data.message`, /// as `declared` in the TypeScript client. #[derive(Debug, Clone, PartialEq)] diff --git a/packages/cli-rs/crates/api/src/generated/types.rs b/packages/cli-rs/crates/api/src/generated/types.rs index fd32c18b3ad..762ad14a152 100644 --- a/packages/cli-rs/crates/api/src/generated/types.rs +++ b/packages/cli-rs/crates/api/src/generated/types.rs @@ -262,6 +262,20 @@ impl<'de> Deserialize<'de> for ProviderCompaction { } } +impl ProviderCompaction { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Summary(value) => value.r#type.as_str(), + Self::Native(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// `Provider.CacheTTL` #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize)] #[serde(transparent)] @@ -690,6 +704,22 @@ impl<'de> Deserialize<'de> for PluginSource { } } +impl PluginSource { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Builtin(value) => value.r#type.as_str(), + Self::Package(value) => value.r#type.as_str(), + Self::Local(value) => value.r#type.as_str(), + Self::Sdk(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct PluginFeatures { #[serde(default, skip_serializing_if = "Option::is_none")] @@ -736,6 +766,20 @@ impl<'de> Deserialize<'de> for PluginState { } } +impl PluginState { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Active(value) => value.status.as_str(), + Self::Failed(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct PluginInfo { #[serde(default, skip_serializing_if = "Option::is_none")] @@ -996,6 +1040,20 @@ impl<'de> Deserialize<'de> for SessionForkBoundary { } } +impl SessionForkBoundary { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Before(value) => value.r#type.as_str(), + Self::Through(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SessionInfoFork { #[serde(rename = "sessionID")] @@ -1385,6 +1443,20 @@ impl<'de> Deserialize<'de> for PromptFileSource { } } +impl PromptFileSource { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Inline(value) => value.r#type.as_str(), + Self::Uri(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct PromptMention { pub start: f64, @@ -2013,6 +2085,22 @@ impl<'de> Deserialize<'de> for SessionMessageAssistantToolState { } } +impl SessionMessageAssistantToolState { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Streaming(value) => value.status.as_str(), + Self::Running(value) => value.status.as_str(), + Self::Completed(value) => value.status.as_str(), + Self::Error(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct SessionMessageAssistantToolTime { pub created: f64, @@ -2057,6 +2145,21 @@ impl<'de> Deserialize<'de> for SessionMessageAssistantContentItem { } } +impl SessionMessageAssistantContentItem { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Text(value) => value.r#type.as_str(), + Self::Reasoning(value) => value.r#type.as_str(), + Self::Tool(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct SessionMessageAssistantSnapshot { #[serde(default, skip_serializing_if = "Option::is_none")] @@ -2265,6 +2368,21 @@ impl<'de> Deserialize<'de> for SessionMessageCompaction { } } +impl SessionMessageCompaction { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Running(value) => value.status.as_str(), + Self::Completed(value) => value.status.as_str(), + Self::Failed(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SessionMessageIdle { pub id: SessionMessageID, @@ -2362,6 +2480,22 @@ impl<'de> Deserialize<'de> for SessionInboxInfo { } } +impl SessionInboxInfo { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::User(value) => value.r#type.as_str(), + Self::Synthetic(value) => value.r#type.as_str(), + Self::Compaction(value) => value.r#type.as_str(), + Self::Move(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Output of [`Client::session_inbox_list`]. pub type SessionInboxListOutput = Vec; @@ -2554,6 +2688,21 @@ impl<'de> Deserialize<'de> for FormState { } } +impl FormState { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Pending(value) => value.status.as_str(), + Self::Answered(value) => value.status.as_str(), + Self::Cancelled(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct FormDetail { pub id: FormID, @@ -2737,6 +2886,21 @@ impl<'de> Deserialize<'de> for SessionStatsTools { } } +impl SessionStatsTools { + /// The `mode` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::None(value) => value.mode.as_str(), + Self::Summary(value) => value.mode.as_str(), + Self::Detail(value) => value.mode.as_str(), + Self::Unknown(value) => value + .get("mode") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct SessionStatsActivity { pub date: String, @@ -3372,6 +3536,22 @@ impl<'de> Deserialize<'de> for SessionInboxItem { } } +impl SessionInboxItem { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::User(value) => value.r#type.as_str(), + Self::Synthetic(value) => value.r#type.as_str(), + Self::Compaction(value) => value.r#type.as_str(), + Self::Move(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SessionInboxEnqueuedData { #[serde(rename = "sessionID")] @@ -4293,6 +4473,22 @@ impl<'de> Deserialize<'de> for SessionMessageAssistantTool1State { } } +impl SessionMessageAssistantTool1State { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Streaming(value) => value.status.as_str(), + Self::Running(value) => value.status.as_str(), + Self::Completed(value) => value.status.as_str(), + Self::Error(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SessionMessageAssistantTool1 { pub r#type: String, @@ -4329,6 +4525,21 @@ impl<'de> Deserialize<'de> for SessionMessageAssistantContentEncoded { } } +impl SessionMessageAssistantContentEncoded { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Text(value) => value.r#type.as_str(), + Self::Reasoning(value) => value.r#type.as_str(), + Self::Tool(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct SessionMessageContentUpdatedData { #[serde(rename = "sessionID")] @@ -4518,6 +4729,65 @@ impl<'de> Deserialize<'de> for SessionEventDurable { } } +impl SessionEventDurable { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::SessionCreated(value) => value.r#type.as_str(), + Self::SessionAgentSelected(value) => value.r#type.as_str(), + Self::SessionModelSelected(value) => value.r#type.as_str(), + Self::SessionMoved(value) => value.r#type.as_str(), + Self::SessionRenamed(value) => value.r#type.as_str(), + Self::SessionMetadataUpdated(value) => value.r#type.as_str(), + Self::SessionPermissions(value) => value.r#type.as_str(), + Self::SessionViewed(value) => value.r#type.as_str(), + Self::SessionArchived(value) => value.r#type.as_str(), + Self::SessionUnarchived(value) => value.r#type.as_str(), + Self::SessionDeleted(value) => value.r#type.as_str(), + Self::SessionForked(value) => value.r#type.as_str(), + Self::SessionInboxDelivered(value) => value.r#type.as_str(), + Self::SessionInboxEnqueued(value) => value.r#type.as_str(), + Self::SessionInboxCancelled(value) => value.r#type.as_str(), + Self::SessionInboxDeliveryChanged(value) => value.r#type.as_str(), + Self::SessionExecutionStarted(value) => value.r#type.as_str(), + Self::SessionExecutionSucceeded(value) => value.r#type.as_str(), + Self::SessionExecutionFailed(value) => value.r#type.as_str(), + Self::SessionExecutionInterrupted(value) => value.r#type.as_str(), + Self::SessionInstructionsUpdated(value) => value.r#type.as_str(), + Self::SessionSynthetic(value) => value.r#type.as_str(), + Self::SessionSkillActivated(value) => value.r#type.as_str(), + Self::SessionShellStarted(value) => value.r#type.as_str(), + Self::SessionShellEnded(value) => value.r#type.as_str(), + Self::SessionStepStarted(value) => value.r#type.as_str(), + Self::SessionStepStreamed(value) => value.r#type.as_str(), + Self::SessionStepEnded(value) => value.r#type.as_str(), + Self::SessionStepFailed(value) => value.r#type.as_str(), + Self::SessionTextStarted(value) => value.r#type.as_str(), + Self::SessionTextEnded(value) => value.r#type.as_str(), + Self::SessionReasoningStarted(value) => value.r#type.as_str(), + Self::SessionReasoningEnded(value) => value.r#type.as_str(), + Self::SessionToolInputStarted(value) => value.r#type.as_str(), + Self::SessionToolInputEnded(value) => value.r#type.as_str(), + Self::SessionToolCalled(value) => value.r#type.as_str(), + Self::SessionToolSuccess(value) => value.r#type.as_str(), + Self::SessionToolFailed(value) => value.r#type.as_str(), + Self::SessionRetryScheduled(value) => value.r#type.as_str(), + Self::SessionCompactionStarted(value) => value.r#type.as_str(), + Self::SessionCompactionEnded(value) => value.r#type.as_str(), + Self::SessionCompactionFailed(value) => value.r#type.as_str(), + Self::SessionRevertStaged(value) => value.r#type.as_str(), + Self::SessionRevertCleared(value) => value.r#type.as_str(), + Self::SessionRevertCommitted(value) => value.r#type.as_str(), + Self::SessionUsageRecorded(value) => value.r#type.as_str(), + Self::SessionMessageContentUpdated(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct EventLogSynced { pub r#type: String, @@ -5200,6 +5470,21 @@ impl<'de> Deserialize<'de> for GenerateDecisionPart { } } +impl GenerateDecisionPart { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Text(value) => value.r#type.as_str(), + Self::Json(value) => value.r#type.as_str(), + Self::File(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct GeneratePredicateQuestion { pub r#type: String, @@ -5270,6 +5555,21 @@ impl<'de> Deserialize<'de> for GenerateDecisionQuestion { } } +impl GenerateDecisionQuestion { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Predicate(value) => value.r#type.as_str(), + Self::Choice(value) => value.r#type.as_str(), + Self::Score(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Input of [`Client::experimental_generate_decisions`]. #[derive(Debug, Clone, PartialEq, Default)] pub struct ExperimentalGenerateDecisionsInput { @@ -5350,6 +5650,22 @@ impl<'de> Deserialize<'de> for GenerateDecisionAnswer { } } +impl GenerateDecisionAnswer { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Predicate(value) => value.r#type.as_str(), + Self::Choice(value) => value.r#type.as_str(), + Self::Score(value) => value.r#type.as_str(), + Self::Error(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct GenerateDecisionResult { pub answers: Vec, @@ -5454,6 +5770,20 @@ impl<'de> Deserialize<'de> for VoiceTransportInput { } } +impl VoiceTransportInput { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Webrtc(value) => value.r#type.as_str(), + Self::Websocket(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Input of [`Client::experimental_voice_session`]. #[derive(Debug, Clone, PartialEq)] pub struct ExperimentalVoiceSessionInput { @@ -5498,6 +5828,20 @@ impl<'de> Deserialize<'de> for VoiceTransport { } } +impl VoiceTransport { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Webrtc(value) => value.r#type.as_str(), + Self::Websocket(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ExperimentalVoiceSessionOutput { pub transport: VoiceTransport, @@ -5785,6 +6129,23 @@ impl<'de> Deserialize<'de> for IntegrationMethod { } } +impl IntegrationMethod { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Oauth(value) => value.r#type.as_str(), + Self::Command(value) => value.r#type.as_str(), + Self::Key(value) => value.r#type.as_str(), + Self::External(value) => value.r#type.as_str(), + Self::Env(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// `Credential.ID` #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize)] #[serde(transparent)] @@ -5904,6 +6265,20 @@ impl<'de> Deserialize<'de> for ConnectionInfo { } } +impl ConnectionInfo { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Credential(value) => value.r#type.as_str(), + Self::Env(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct IntegrationInfo { pub id: IntegrationID, @@ -6112,6 +6487,22 @@ impl<'de> Deserialize<'de> for IntegrationAttemptStatus { } } +impl IntegrationAttemptStatus { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Pending(value) => value.status.as_str(), + Self::Complete(value) => value.status.as_str(), + Self::Failed(value) => value.status.as_str(), + Self::Expired(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct IntegrationOauthStatusOutput { pub location: LocationPublicRef, @@ -6223,6 +6614,22 @@ impl<'de> Deserialize<'de> for IntegrationCommandAttemptStatus { } } +impl IntegrationCommandAttemptStatus { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Pending(value) => value.status.as_str(), + Self::Complete(value) => value.status.as_str(), + Self::Failed(value) => value.status.as_str(), + Self::Expired(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct IntegrationCommandStatusOutput { pub location: LocationPublicRef, @@ -6303,6 +6710,23 @@ impl<'de> Deserialize<'de> for McpServerStatus { } } +impl McpServerStatus { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Connected(value) => value.status.as_str(), + Self::Pending(value) => value.status.as_str(), + Self::Disabled(value) => value.status.as_str(), + Self::Failed(value) => value.status.as_str(), + Self::NeedsAuth(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct McpServer { pub name: String, @@ -6499,6 +6923,20 @@ impl<'de> Deserialize<'de> for ExperimentalMcpAddInputConfig { } } +impl ExperimentalMcpAddInputConfig { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Local(value) => value.r#type.as_str(), + Self::Remote(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Input of [`Client::experimental_mcp_add`]. #[derive(Debug, Clone, PartialEq)] pub struct ExperimentalMcpAddInput { @@ -6600,6 +7038,21 @@ impl<'de> Deserialize<'de> for CredentialValue { } } +impl CredentialValue { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Oauth(value) => value.r#type.as_str(), + Self::Key(value) => value.r#type.as_str(), + Self::External(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct CredentialEntry { pub id: CredentialID, @@ -7152,18 +7605,10 @@ pub struct RpcOutput { /// Output of [`Client::rpc_call`]. pub type RpcCallOutput = RpcOutput; -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(untagged)] -pub enum EventSubscribeInputTypesItem { - String(String), - /// A shape this client does not know, kept as it arrived. - Unknown(serde_json::Value), -} - /// Input of [`Client::event_subscribe`]. #[derive(Debug, Clone, PartialEq, Default)] pub struct EventSubscribeInput { - pub types: Option>, + pub types: Option>, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -7181,6 +7626,9 @@ pub struct LocationShutdown { pub data: LocationShutdownData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ModelsDevRefreshedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ModelsDevRefreshed { pub id: EventID, @@ -7190,9 +7638,12 @@ pub struct ModelsDevRefreshed { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ModelsDevRefreshedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct CredentialUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct CredentialUpdated { pub id: EventID, @@ -7202,7 +7653,7 @@ pub struct CredentialUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: CredentialUpdatedData, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -7225,6 +7676,9 @@ pub struct CredentialSwitched { pub data: CredentialSwitchedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct IntegrationUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct IntegrationUpdated { pub id: EventID, @@ -7234,9 +7688,12 @@ pub struct IntegrationUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: IntegrationUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ProviderUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ProviderUpdated { pub id: EventID, @@ -7246,9 +7703,12 @@ pub struct ProviderUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ProviderUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ModelUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ModelUpdated { pub id: EventID, @@ -7258,9 +7718,12 @@ pub struct ModelUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ModelUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct AgentUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct AgentUpdated { pub id: EventID, @@ -7270,7 +7733,7 @@ pub struct AgentUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: AgentUpdatedData, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -7451,6 +7914,9 @@ pub struct FilesystemChanged { pub data: FilesystemChangedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ReferenceUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ReferenceUpdated { pub id: EventID, @@ -7460,7 +7926,7 @@ pub struct ReferenceUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ReferenceUpdatedData, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -7513,6 +7979,9 @@ pub struct PermissionReplied { pub data: PermissionRepliedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct PluginUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct PluginUpdated { pub id: EventID, @@ -7522,7 +7991,7 @@ pub struct PluginUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: PluginUpdatedData, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -7594,6 +8063,9 @@ pub struct WorktreeResolved { pub data: WorktreeResolvedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct CommandUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct CommandUpdated { pub id: EventID, @@ -7603,9 +8075,12 @@ pub struct CommandUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: CommandUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ConfigUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ConfigUpdated { pub id: EventID, @@ -7615,9 +8090,12 @@ pub struct ConfigUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ConfigUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct SkillUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct SkillUpdated { pub id: EventID, @@ -7627,7 +8105,7 @@ pub struct SkillUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: SkillUpdatedData, } /// `Pty.ID` @@ -7988,6 +8466,9 @@ pub struct FormCancelled { pub data: FormCancelledData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct WebsearchUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct WebsearchUpdated { pub id: EventID, @@ -7997,9 +8478,12 @@ pub struct WebsearchUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: WebsearchUpdatedData, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct ArtifactUpdatedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ArtifactUpdated { pub id: EventID, @@ -8009,7 +8493,7 @@ pub struct ArtifactUpdated { pub r#type: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, - pub data: LocationShutdownData, + pub data: ArtifactUpdatedData, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] @@ -8066,6 +8550,21 @@ impl<'de> Deserialize<'de> for SessionStatus { } } +impl SessionStatus { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Idle(value) => value.r#type.as_str(), + Self::Retry(value) => value.r#type.as_str(), + Self::Busy(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SessionStatusUpdatedData { #[serde(rename = "sessionID")] @@ -8298,6 +8797,9 @@ pub struct V2EventRpc { pub data: BTreeMap, } +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] +pub struct V2EventServerConnectedData {} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct V2EventServerConnected { pub id: EventID, @@ -8306,7 +8808,7 @@ pub struct V2EventServerConnected { #[serde(default, skip_serializing_if = "Option::is_none")] pub location: Option, pub r#type: String, - pub data: LocationShutdownData, + pub data: V2EventServerConnectedData, } /// `V2Event`, tagged by `type`. @@ -8594,6 +9096,116 @@ impl<'de> Deserialize<'de> for V2Event { } } +impl V2Event { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::LocationShutdown(value) => value.r#type.as_str(), + Self::ModelsDevRefreshed(value) => value.r#type.as_str(), + Self::CredentialUpdated(value) => value.r#type.as_str(), + Self::CredentialSwitched(value) => value.r#type.as_str(), + Self::IntegrationUpdated(value) => value.r#type.as_str(), + Self::ProviderUpdated(value) => value.r#type.as_str(), + Self::ModelUpdated(value) => value.r#type.as_str(), + Self::AgentUpdated(value) => value.r#type.as_str(), + Self::SessionCreated(value) => value.r#type.as_str(), + Self::SessionAgentSelected(value) => value.r#type.as_str(), + Self::SessionModelSelected(value) => value.r#type.as_str(), + Self::SessionMoved(value) => value.r#type.as_str(), + Self::SessionRenamed(value) => value.r#type.as_str(), + Self::SessionMetadataUpdated(value) => value.r#type.as_str(), + Self::SessionPermissions(value) => value.r#type.as_str(), + Self::SessionViewed(value) => value.r#type.as_str(), + Self::SessionArchived(value) => value.r#type.as_str(), + Self::SessionUnarchived(value) => value.r#type.as_str(), + Self::SessionUsageUpdated(value) => value.r#type.as_str(), + Self::SessionDeleted(value) => value.r#type.as_str(), + Self::SessionForked(value) => value.r#type.as_str(), + Self::SessionInboxDelivered(value) => value.r#type.as_str(), + Self::SessionInboxEnqueued(value) => value.r#type.as_str(), + Self::SessionInboxCancelled(value) => value.r#type.as_str(), + Self::SessionInboxDeliveryChanged(value) => value.r#type.as_str(), + Self::SessionExecutionStarted(value) => value.r#type.as_str(), + Self::SessionExecutionSucceeded(value) => value.r#type.as_str(), + Self::SessionExecutionFailed(value) => value.r#type.as_str(), + Self::SessionExecutionInterrupted(value) => value.r#type.as_str(), + Self::SessionInstructionsUpdated(value) => value.r#type.as_str(), + Self::SessionSynthetic(value) => value.r#type.as_str(), + Self::SessionSkillActivated(value) => value.r#type.as_str(), + Self::SessionShellStarted(value) => value.r#type.as_str(), + Self::SessionShellEnded(value) => value.r#type.as_str(), + Self::SessionStepStarted(value) => value.r#type.as_str(), + Self::SessionStepStreamed(value) => value.r#type.as_str(), + Self::SessionStepEnded(value) => value.r#type.as_str(), + Self::SessionStepFailed(value) => value.r#type.as_str(), + Self::SessionTextStarted(value) => value.r#type.as_str(), + Self::SessionTextDelta(value) => value.r#type.as_str(), + Self::SessionTextEnded(value) => value.r#type.as_str(), + Self::SessionReasoningStarted(value) => value.r#type.as_str(), + Self::SessionReasoningDelta(value) => value.r#type.as_str(), + Self::SessionReasoningEnded(value) => value.r#type.as_str(), + Self::SessionToolInputStarted(value) => value.r#type.as_str(), + Self::SessionToolInputDelta(value) => value.r#type.as_str(), + Self::SessionToolInputEnded(value) => value.r#type.as_str(), + Self::SessionToolCalled(value) => value.r#type.as_str(), + Self::SessionToolProgress(value) => value.r#type.as_str(), + Self::SessionToolSuccess(value) => value.r#type.as_str(), + Self::SessionToolFailed(value) => value.r#type.as_str(), + Self::SessionRetryScheduled(value) => value.r#type.as_str(), + Self::SessionCompactionStarted(value) => value.r#type.as_str(), + Self::SessionCompactionDelta(value) => value.r#type.as_str(), + Self::SessionCompactionEnded(value) => value.r#type.as_str(), + Self::SessionCompactionFailed(value) => value.r#type.as_str(), + Self::SessionRevertStaged(value) => value.r#type.as_str(), + Self::SessionRevertCleared(value) => value.r#type.as_str(), + Self::SessionRevertCommitted(value) => value.r#type.as_str(), + Self::FilesystemChanged(value) => value.r#type.as_str(), + Self::ReferenceUpdated(value) => value.r#type.as_str(), + Self::PermissionAsked(value) => value.r#type.as_str(), + Self::PermissionReplied(value) => value.r#type.as_str(), + Self::PluginUpdated(value) => value.r#type.as_str(), + Self::ProjectUpdated(value) => value.r#type.as_str(), + Self::WorktreeUpdated(value) => value.r#type.as_str(), + Self::WorktreeResolved(value) => value.r#type.as_str(), + Self::CommandUpdated(value) => value.r#type.as_str(), + Self::ConfigUpdated(value) => value.r#type.as_str(), + Self::SkillUpdated(value) => value.r#type.as_str(), + Self::PtyCreated(value) => value.r#type.as_str(), + Self::PtyUpdated(value) => value.r#type.as_str(), + Self::PtyExited(value) => value.r#type.as_str(), + Self::PtyDeleted(value) => value.r#type.as_str(), + Self::PersistentPtyAdded(value) => value.r#type.as_str(), + Self::PersistentPtyRemoved(value) => value.r#type.as_str(), + Self::PersistentPtyUpdated(value) => value.r#type.as_str(), + Self::ShellCreated(value) => value.r#type.as_str(), + Self::ShellExited(value) => value.r#type.as_str(), + Self::ShellDeleted(value) => value.r#type.as_str(), + Self::FormCreated(value) => value.r#type.as_str(), + Self::FormReplied(value) => value.r#type.as_str(), + Self::FormCancelled(value) => value.r#type.as_str(), + Self::WebsearchUpdated(value) => value.r#type.as_str(), + Self::ArtifactUpdated(value) => value.r#type.as_str(), + Self::SessionStatus(value) => value.r#type.as_str(), + Self::SessionIdle(value) => value.r#type.as_str(), + Self::TuiPromptAppend(value) => value.r#type.as_str(), + Self::TuiCommandExecute(value) => value.r#type.as_str(), + Self::TuiToastShow(value) => value.r#type.as_str(), + Self::TuiSessionSelect(value) => value.r#type.as_str(), + Self::InstallationUpdated(value) => value.r#type.as_str(), + Self::InstallationUpdateAvailable(value) => value.r#type.as_str(), + Self::VcsBranchUpdated(value) => value.r#type.as_str(), + Self::McpStatusChanged(value) => value.r#type.as_str(), + Self::McpResourcesChanged(value) => value.r#type.as_str(), + Self::Rpc(value) => value.r#type.as_str(), + Self::ServerConnected(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Output of [`Client::event_subscribe`]. pub type EventSubscribeOutput = V2Event; @@ -9051,6 +9663,22 @@ impl<'de> Deserialize<'de> for ScheduleWhen { } } +impl ScheduleWhen { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::In(value) => value.r#type.as_str(), + Self::At(value) => value.r#type.as_str(), + Self::Every(value) => value.r#type.as_str(), + Self::Cron(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ScheduleConditionShell { pub r#type: String, @@ -9090,6 +9718,20 @@ impl<'de> Deserialize<'de> for ScheduleCondition { } } +impl ScheduleCondition { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Shell(value) => value.r#type.as_str(), + Self::Code(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)] pub struct ScheduleActionPrompt { pub r#type: String, @@ -9689,6 +10331,21 @@ impl<'de> Deserialize<'de> for JobInfo { } } +impl JobInfo { + /// The `kind` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Shell(value) => value.kind.as_str(), + Self::Subagent(value) => value.kind.as_str(), + Self::Image(value) => value.kind.as_str(), + Self::Unknown(value) => value + .get("kind") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Output of [`Client::experimental_job_list`]. pub type ExperimentalJobListOutput = Vec; @@ -9814,6 +10471,20 @@ impl<'de> Deserialize<'de> for ReferenceSource { } } +impl ReferenceSource { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Local(value) => value.r#type.as_str(), + Self::Git(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ReferenceInfo { pub name: String, @@ -10236,6 +10907,21 @@ impl<'de> Deserialize<'de> for ExperimentalMigrationV1StatusOutput2 { } } +impl ExperimentalMigrationV1StatusOutput2 { + /// The `status` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Variant1(value) => value.status.as_str(), + Self::Running(value) => value.status.as_str(), + Self::Error(value) => value.status.as_str(), + Self::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Output of [`Client::experimental_migration_v1_status`]. pub type ExperimentalMigrationV1StatusOutput = ExperimentalMigrationV1StatusOutput2; @@ -11379,6 +12065,20 @@ impl<'de> Deserialize<'de> for ConfigEntry { } } +impl ConfigEntry { + /// The `type` tag, also of an unknown member. + pub fn tag(&self) -> &str { + match self { + Self::Document(value) => value.r#type.as_str(), + Self::Directory(value) => value.r#type.as_str(), + Self::Unknown(value) => value + .get("type") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(), + } + } +} + /// Output of [`Client::config_get`]. pub type ConfigGetOutput = Vec; diff --git a/packages/cli-rs/crates/api/src/transport.rs b/packages/cli-rs/crates/api/src/transport.rs index 4fb4988d289..fa82a2bdca2 100644 --- a/packages/cli-rs/crates/api/src/transport.rs +++ b/packages/cli-rs/crates/api/src/transport.rs @@ -6,6 +6,10 @@ use std::pin::Pin; use bytes::Bytes; use futures_util::{Stream, StreamExt}; +use serde::de::DeserializeOwned; + +use crate::error::Error; +use crate::generated::V2Event; /// A future a transport returns. pub type BoxFuture<'a, T> = Pin + Send + 'a>>; @@ -80,6 +84,24 @@ impl SseResponse { } } +impl SseResponse { + /// The events, decoded by [`crate::sse::events`]. `activity` runs for every received chunk, + /// keepalive comments included. The stream ends after the first error. + pub fn events( + self, + activity: impl FnMut() + Send + 'static, + ) -> impl Stream> + Send { + crate::sse::events(self.response.body, activity).map(|item| item.map_err(Error::from)) + } +} + +/// Lets F.3's connection loop and shared subscription carry the generated event type. +impl crate::events::Event for V2Event { + fn kind(&self) -> &str { + self.tag() + } +} + impl std::fmt::Debug for SseResponse { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("SseResponse").field(&self.response).finish() diff --git a/packages/cli-rs/crates/api/tests/api.rs b/packages/cli-rs/crates/api/tests/api.rs new file mode 100644 index 00000000000..75a02047b79 --- /dev/null +++ b/packages/cli-rs/crates/api/tests/api.rs @@ -0,0 +1,217 @@ +//! Decoding rules of docs/F.2.md and the request core, over a scripted transport. + +use std::sync::{Arc, Mutex}; + +use bytes::Bytes; +use futures_util::StreamExt; +use opencode_api::client::{Auth, Client, RequestOptions}; +use opencode_api::error::Error; +use opencode_api::events::Event; +use opencode_api::generated::*; +use opencode_api::transport::{BoxFuture, Request, Response, Transport}; +use serde_json::{Value, json}; + +/// Answers every request with one canned response and records the requests. +struct Script { + status: u16, + content_type: &'static str, + body: Vec, + requests: Mutex>, +} + +impl Script { + fn new(status: u16, content_type: &'static str, body: impl Into>) -> Arc { + Arc::new(Self { + status, + content_type, + body: body.into(), + requests: Mutex::default(), + }) + } +} + +impl Transport for Script { + fn send(&self, request: Request) -> BoxFuture<'_, Result> { + self.requests.lock().unwrap().push(request); + // Two chunks, so readers cannot assume one. + let (head, tail) = self.body.split_at(self.body.len() / 2); + let chunks = vec![Ok(Bytes::copy_from_slice(head)), Ok(Bytes::copy_from_slice(tail))]; + let response = Response { + status: self.status, + headers: vec![("content-type".to_owned(), self.content_type.to_owned())], + body: Box::pin(futures_util::stream::iter(chunks)), + }; + Box::pin(async move { Ok(response) }) + } +} + +fn client(script: &Arc