-
Notifications
You must be signed in to change notification settings - Fork 1.5k
refactor: extract AppEvent to crates/ironclaw_common #1615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ilblackdragon
merged 4 commits into
staging
from
refactor/extract-app-event-to-ironclaw-common
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
059445d
refactor: extract AppEvent to crates/ironclaw_common
ilblackdragon c046401
refactor: add AppEvent::event_type() helper, deduplicate match blocks
ilblackdragon 3e8afb1
refactor: rename leftover sse vars/tests to match AppEvent rename
ilblackdragon e0cad3b
refactor: add Deserialize to AppEvent, round-trip test, fix stale com…
ilblackdragon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "ironclaw_common" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| rust-version = "1.92" | ||
| description = "Shared types and utilities for the IronClaw workspace" | ||
| authors = ["NEAR AI <support@near.ai>"] | ||
| license = "MIT OR Apache-2.0" | ||
| homepage = "https://github.com/nearai/ironclaw" | ||
| repository = "https://github.com/nearai/ironclaw" | ||
| publish = false | ||
|
|
||
| [package.metadata.dist] | ||
| dist = false | ||
|
|
||
| [dependencies] | ||
| serde = { version = "1", features = ["derive"] } | ||
| serde_json = "1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,338 @@ | ||
| //! Application-wide event types. | ||
| //! | ||
| //! `AppEvent` is the real-time event protocol used across the entire | ||
| //! application. The web gateway serialises these to SSE / WebSocket | ||
| //! frames, but other subsystems (agent loop, orchestrator, extensions) | ||
| //! produce and consume them too. | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| #[serde(tag = "type")] | ||
| pub enum AppEvent { | ||
| #[serde(rename = "response")] | ||
| Response { content: String, thread_id: String }, | ||
| #[serde(rename = "thinking")] | ||
| Thinking { | ||
| message: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "tool_started")] | ||
| ToolStarted { | ||
| name: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "tool_completed")] | ||
| ToolCompleted { | ||
| name: String, | ||
| success: bool, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| error: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| parameters: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "tool_result")] | ||
| ToolResult { | ||
| name: String, | ||
| preview: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "stream_chunk")] | ||
| StreamChunk { | ||
| content: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "status")] | ||
| Status { | ||
| message: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "job_started")] | ||
| JobStarted { | ||
| job_id: String, | ||
| title: String, | ||
| browse_url: String, | ||
| }, | ||
| #[serde(rename = "approval_needed")] | ||
| ApprovalNeeded { | ||
| request_id: String, | ||
| tool_name: String, | ||
| description: String, | ||
| parameters: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| /// Whether the "always" auto-approve option should be shown. | ||
| allow_always: bool, | ||
| }, | ||
| #[serde(rename = "auth_required")] | ||
| AuthRequired { | ||
| extension_name: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| instructions: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| auth_url: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| setup_url: Option<String>, | ||
| }, | ||
| #[serde(rename = "auth_completed")] | ||
| AuthCompleted { | ||
| extension_name: String, | ||
| success: bool, | ||
| message: String, | ||
| }, | ||
| #[serde(rename = "error")] | ||
| Error { | ||
| message: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
| #[serde(rename = "heartbeat")] | ||
| Heartbeat, | ||
|
|
||
| // Sandbox job streaming events (worker + Claude Code bridge) | ||
| #[serde(rename = "job_message")] | ||
| JobMessage { | ||
| job_id: String, | ||
| role: String, | ||
| content: String, | ||
| }, | ||
| #[serde(rename = "job_tool_use")] | ||
| JobToolUse { | ||
| job_id: String, | ||
| tool_name: String, | ||
| input: serde_json::Value, | ||
| }, | ||
| #[serde(rename = "job_tool_result")] | ||
| JobToolResult { | ||
| job_id: String, | ||
| tool_name: String, | ||
| output: String, | ||
| }, | ||
| #[serde(rename = "job_status")] | ||
| JobStatus { job_id: String, message: String }, | ||
| #[serde(rename = "job_result")] | ||
| JobResult { | ||
| job_id: String, | ||
| status: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| session_id: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| fallback_deliverable: Option<serde_json::Value>, | ||
| }, | ||
|
|
||
| /// An image was generated by a tool. | ||
| #[serde(rename = "image_generated")] | ||
| ImageGenerated { | ||
| data_url: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| path: Option<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
|
|
||
| /// Suggested follow-up messages for the user. | ||
| #[serde(rename = "suggestions")] | ||
| Suggestions { | ||
| suggestions: Vec<String>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
|
|
||
| /// Per-turn token usage and cost summary. | ||
| #[serde(rename = "turn_cost")] | ||
| TurnCost { | ||
| input_tokens: u64, | ||
| output_tokens: u64, | ||
| cost_usd: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| thread_id: Option<String>, | ||
| }, | ||
|
|
||
| /// Extension activation status change (WASM channels). | ||
| #[serde(rename = "extension_status")] | ||
| ExtensionStatus { | ||
| extension_name: String, | ||
| status: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| message: Option<String>, | ||
| }, | ||
| } | ||
|
|
||
| impl AppEvent { | ||
| /// The wire-format event type string (matches the `#[serde(rename)]` value). | ||
| pub fn event_type(&self) -> &'static str { | ||
| match self { | ||
| Self::Response { .. } => "response", | ||
| Self::Thinking { .. } => "thinking", | ||
| Self::ToolStarted { .. } => "tool_started", | ||
| Self::ToolCompleted { .. } => "tool_completed", | ||
| Self::ToolResult { .. } => "tool_result", | ||
| Self::StreamChunk { .. } => "stream_chunk", | ||
| Self::Status { .. } => "status", | ||
| Self::JobStarted { .. } => "job_started", | ||
| Self::ApprovalNeeded { .. } => "approval_needed", | ||
| Self::AuthRequired { .. } => "auth_required", | ||
| Self::AuthCompleted { .. } => "auth_completed", | ||
| Self::Error { .. } => "error", | ||
| Self::Heartbeat => "heartbeat", | ||
| Self::JobMessage { .. } => "job_message", | ||
| Self::JobToolUse { .. } => "job_tool_use", | ||
| Self::JobToolResult { .. } => "job_tool_result", | ||
| Self::JobStatus { .. } => "job_status", | ||
| Self::JobResult { .. } => "job_result", | ||
| Self::ImageGenerated { .. } => "image_generated", | ||
| Self::Suggestions { .. } => "suggestions", | ||
| Self::TurnCost { .. } => "turn_cost", | ||
| Self::ExtensionStatus { .. } => "extension_status", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// Verify that `event_type()` returns the same string as the serde | ||
| /// `"type"` field for every variant. This catches drift between the | ||
| /// `#[serde(rename)]` attributes and the manual match arms. | ||
| #[test] | ||
| fn event_type_matches_serde_type_field() { | ||
| let variants: Vec<AppEvent> = vec![ | ||
| AppEvent::Response { | ||
| content: String::new(), | ||
| thread_id: String::new(), | ||
| }, | ||
| AppEvent::Thinking { | ||
| message: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::ToolStarted { | ||
| name: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::ToolCompleted { | ||
| name: String::new(), | ||
| success: true, | ||
| error: None, | ||
| parameters: None, | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::ToolResult { | ||
| name: String::new(), | ||
| preview: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::StreamChunk { | ||
| content: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::Status { | ||
| message: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::JobStarted { | ||
| job_id: String::new(), | ||
| title: String::new(), | ||
| browse_url: String::new(), | ||
| }, | ||
| AppEvent::ApprovalNeeded { | ||
| request_id: String::new(), | ||
| tool_name: String::new(), | ||
| description: String::new(), | ||
| parameters: String::new(), | ||
| thread_id: None, | ||
| allow_always: false, | ||
| }, | ||
| AppEvent::AuthRequired { | ||
| extension_name: String::new(), | ||
| instructions: None, | ||
| auth_url: None, | ||
| setup_url: None, | ||
| }, | ||
| AppEvent::AuthCompleted { | ||
| extension_name: String::new(), | ||
| success: true, | ||
| message: String::new(), | ||
| }, | ||
| AppEvent::Error { | ||
| message: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::Heartbeat, | ||
| AppEvent::JobMessage { | ||
| job_id: String::new(), | ||
| role: String::new(), | ||
| content: String::new(), | ||
| }, | ||
| AppEvent::JobToolUse { | ||
| job_id: String::new(), | ||
| tool_name: String::new(), | ||
| input: serde_json::Value::Null, | ||
| }, | ||
| AppEvent::JobToolResult { | ||
| job_id: String::new(), | ||
| tool_name: String::new(), | ||
| output: String::new(), | ||
| }, | ||
| AppEvent::JobStatus { | ||
| job_id: String::new(), | ||
| message: String::new(), | ||
| }, | ||
| AppEvent::JobResult { | ||
| job_id: String::new(), | ||
| status: String::new(), | ||
| session_id: None, | ||
| fallback_deliverable: None, | ||
| }, | ||
| AppEvent::ImageGenerated { | ||
| data_url: String::new(), | ||
| path: None, | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::Suggestions { | ||
| suggestions: vec![], | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::TurnCost { | ||
| input_tokens: 0, | ||
| output_tokens: 0, | ||
| cost_usd: String::new(), | ||
| thread_id: None, | ||
| }, | ||
| AppEvent::ExtensionStatus { | ||
| extension_name: String::new(), | ||
| status: String::new(), | ||
| message: None, | ||
| }, | ||
| ]; | ||
|
|
||
| for variant in &variants { | ||
| let json: serde_json::Value = serde_json::to_value(variant).unwrap(); | ||
| let serde_type = json["type"].as_str().unwrap(); | ||
| assert_eq!( | ||
| variant.event_type(), | ||
| serde_type, | ||
| "event_type() mismatch for variant: {:?}", | ||
| variant | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn round_trip_deserialize() { | ||
| let original = AppEvent::Response { | ||
| content: "hello".to_string(), | ||
| thread_id: "t1".to_string(), | ||
| }; | ||
| let json = serde_json::to_string(&original).unwrap(); | ||
| let deserialized: AppEvent = serde_json::from_str(&json).unwrap(); | ||
| assert_eq!(deserialized.event_type(), "response"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce code duplication that this refactoring surfaces, you can add a helper method on
AppEventto get the event type string. This can then be used insse.rsandtypes.rsto avoid repeating the largematchstatement, as I'll suggest in other comments.References