Skip to content

Commit 3e59e81

Browse files
committed
WIP
1 parent 8042866 commit 3e59e81

File tree

10 files changed

+168
-127
lines changed

10 files changed

+168
-127
lines changed

rust-sdk/crates/ag-ui-client/src/agent.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
use futures::stream::StreamExt;
12
use std::collections::HashSet;
23
use std::sync::Arc;
3-
use futures::stream::StreamExt;
44
use thiserror::Error;
55

6+
use crate::event::EventExt;
7+
use crate::stream::EventStream;
8+
use crate::subscriber::AgentSubscriber;
69
use ag_ui_core::event::Event;
710
use ag_ui_core::types::context::Context;
8-
use ag_ui_core::types::ids::{AgentId, RunId, ThreadId};
11+
use ag_ui_core::types::ids::{AgentId, MessageId, RunId, ThreadId};
912
use ag_ui_core::types::input::RunAgentInput;
10-
use ag_ui_core::types::messages::Message;
13+
use ag_ui_core::types::message::Message;
1114
use ag_ui_core::types::tool::Tool;
1215
use ag_ui_core::{FwdProps, JsonValue, State};
13-
use crate::event::EventExt;
14-
use crate::stream::EventStream;
15-
use crate::subscriber::AgentSubscriber;
1616

1717
#[derive(Debug, Clone)]
1818
pub struct AgentConfig<StateT = JsonValue> {
@@ -86,7 +86,6 @@ pub enum AgentError {
8686
},
8787
}
8888

89-
9089
/// Agent trait
9190
#[async_trait::async_trait]
9291
pub trait Agent<StateT = JsonValue, FwdPropsT = JsonValue>: Send + Sync
@@ -132,11 +131,9 @@ where
132131

133132
let input = self.prepare_run_agent_input(params);
134133
let messages = self.messages().to_vec();
135-
let current_message_ids: HashSet<_> =
136-
messages.iter().map(|m| m.id()).collect();
134+
let current_message_ids: HashSet<&MessageId> = messages.iter().map(|m| m.id()).collect();
137135
let mut result_val = JsonValue::Null;
138136

139-
140137
let mut stream = self.run(&input).fuse();
141138

142139
while let Some(event_result) = stream.next().await {
@@ -145,7 +142,7 @@ where
145142
let (mutation, value) = event
146143
.apply_and_process_event(&input, &messages, &input.state, &subscribers)
147144
.await?;
148-
result_val = JsonValue::from(value);
145+
result_val = JsonValue::from(value);
149146
}
150147
Err(e) => {
151148
// self.on_error(&input, &e, &subscribers).await?;
@@ -197,9 +194,15 @@ where
197194
// This is a simplified stand-in for the logic from `defaultApplyEvents` in TS.
198195
// A full implementation would handle each event type to create the correct state mutation.
199196
let (mutation, result) = match event {
200-
Event::RunFinished ( e ) => {
197+
Event::RunFinished(e) => {
201198
for sub in subscribers {
202-
sub.on_run_finished(&e.result.clone().unwrap(), self.messages(), self.state(), input).await?;
199+
sub.on_run_finished(
200+
&e.result.clone().unwrap(),
201+
self.messages(),
202+
self.state(),
203+
input,
204+
)
205+
.await?;
203206
}
204207
(AgentStateMutation::default(), e.result)
205208
}
@@ -310,4 +313,4 @@ where
310313
}
311314
Ok(())
312315
}
313-
}
316+
}

rust-sdk/crates/ag-ui-client/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::agent::{AgentError, AgentStateMutation};
22
use crate::subscriber::AgentSubscriber;
33
use ag_ui_core::event::Event;
44
use ag_ui_core::types::input::RunAgentInput;
5-
use ag_ui_core::types::messages::Message;
5+
use ag_ui_core::types::message::Message;
66
use ag_ui_core::{FwdProps, JsonValue, State};
77
use std::sync::Arc;
88

rust-sdk/crates/ag-ui-client/src/subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde_json::Value as JsonValue;
22
use ag_ui_core::{FwdProps, State};
33
use ag_ui_core::types::input::RunAgentInput;
4-
use ag_ui_core::types::messages::Message;
4+
use ag_ui_core::types::message::Message;
55
use crate::agent::{AgentError, AgentStateMutation};
66

77
/// Subscriber trait for handling agent events

rust-sdk/crates/ag-ui-core/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::types::messages::Message;
1+
use crate::types::message::Message;
22
use serde::{Deserialize, Serialize};
33
use crate::JsonValue;
44
use crate::types::ids::ToolCallId;
Lines changed: 98 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,113 @@
11
use uuid::Uuid;
22
use serde::{Deserialize, Serialize};
33

4-
/// Agent ID
5-
///
6-
/// A newtype is used to prevent mixing them with other ID values.
7-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8-
pub struct AgentId(Uuid);
4+
/// Macro to define a newtype ID based on Uuid.
5+
/// Accepts doc comments and other derive-able attributes.
6+
macro_rules! define_id_type {
7+
// This arm handles calls that do specify extra derives (like Eq).
8+
($(#[$attr:meta])* $name:ident, $($extra_derive:ident),*) => {
9+
$(#[$attr])*
10+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq, Hash, $($extra_derive),*)]
11+
pub struct $name(Uuid);
912

10-
impl AgentId {
11-
pub fn new() -> Self {
12-
Self(Uuid::new_v4())
13-
}
14-
}
13+
impl $name {
14+
/// Creates a new random ID.
15+
pub fn new() -> Self {
16+
Self(Uuid::new_v4())
17+
}
18+
}
1519

16-
impl From<Uuid> for AgentId {
17-
fn from(uuid: Uuid) -> Self {
18-
Self(uuid)
19-
}
20-
}
20+
/// Allows creating an ID from a Uuid.
21+
impl From<Uuid> for $name {
22+
fn from(uuid: Uuid) -> Self {
23+
Self(uuid)
24+
}
25+
}
2126

22-
/// Thread ID
23-
///
24-
/// A newtype is used to prevent mixing them with other ID values.
25-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26-
pub struct ThreadId(Uuid);
27+
/// Allows converting an ID back into a Uuid.
28+
impl From<$name> for Uuid {
29+
fn from(id: $name) -> Self {
30+
id.0
31+
}
32+
}
2733

28-
impl ThreadId {
29-
pub fn new() -> Self {
30-
Self(Uuid::new_v4())
31-
}
32-
}
34+
/// Allows getting a reference to the inner Uuid.
35+
impl AsRef<Uuid> for $name {
36+
fn as_ref(&self) -> &Uuid {
37+
&self.0
38+
}
39+
}
3340

34-
impl From<Uuid> for ThreadId {
35-
fn from(uuid: Uuid) -> Self {
36-
Self(uuid)
37-
}
38-
}
41+
/// Allows printing the ID.
42+
impl std::fmt::Display for $name {
43+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44+
write!(f, "{}", self.0)
45+
}
46+
}
3947

40-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
41-
pub struct RunId(Uuid);
48+
/// Allows parsing an ID from a string slice.
49+
impl std::str::FromStr for $name {
50+
type Err = uuid::Error;
4251

43-
/// Run ID
44-
///
45-
/// A newtype is used to prevent mixing them with other ID values.
46-
impl RunId {
47-
pub fn new() -> Self {
48-
Self(Uuid::new_v4())
49-
}
50-
}
52+
fn from_str(s: &str) -> Result<Self, Self::Err> {
53+
Ok(Self(Uuid::parse_str(s)?))
54+
}
55+
}
5156

52-
impl From<Uuid> for RunId {
53-
fn from(uuid: Uuid) -> Self {
54-
Self(uuid)
55-
}
57+
/// Allows comparing the ID with a Uuid.
58+
impl PartialEq<Uuid> for $name {
59+
fn eq(&self, other: &Uuid) -> bool {
60+
self.0 == *other
61+
}
62+
}
63+
64+
/// Allows comparing the ID with a string slice.
65+
impl PartialEq<str> for $name {
66+
fn eq(&self, other: &str) -> bool {
67+
if let Ok(uuid) = Uuid::parse_str(other) {
68+
self.0 == uuid
69+
} else {
70+
false
71+
}
72+
}
73+
}
74+
};
75+
($(#[$attr:meta])* $name:ident) => {
76+
define_id_type!($(#[$attr])* $name,);
77+
};
5678
}
5779

58-
/// Tool Call ID
59-
///
60-
/// A newtype is used to prevent mixing them with other ID values.
61-
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
62-
pub struct ToolCallId(Uuid);
80+
define_id_type!(
81+
/// Agent ID
82+
///
83+
/// A newtype is used to prevent mixing them with other ID values.
84+
AgentId
85+
);
6386

64-
impl ToolCallId {
65-
pub fn new() -> Self {
66-
Self(Uuid::new_v4())
67-
}
68-
}
87+
define_id_type!(
88+
/// Thread ID
89+
///
90+
/// A newtype is used to prevent mixing them with other ID values.
91+
ThreadId
92+
);
93+
94+
define_id_type!(
95+
/// Run ID
96+
///
97+
/// A newtype is used to prevent mixing them with other ID values.
98+
RunId
99+
);
100+
101+
define_id_type!(
102+
/// Tool Call ID
103+
///
104+
/// A newtype is used to prevent mixing them with other ID values.
105+
ToolCallId
106+
);
69107

70-
impl From<Uuid> for ToolCallId {
71-
fn from(uuid: Uuid) -> Self {
72-
Self(uuid)
73-
}
74-
}
108+
define_id_type!(
109+
/// Message ID
110+
///
111+
/// A newtype is used to prevent mixing them with other ID values.
112+
MessageId
113+
);

rust-sdk/crates/ag-ui-core/src/types/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::types::context::Context;
2-
use crate::types::messages::Message;
2+
use crate::types::message::Message;
33
use crate::types::tool::Tool;
44
use serde::{Deserialize, Serialize};
55
use crate::JsonValue;

0 commit comments

Comments
 (0)