1 parent
cf8268d17b
commit
a01d0cbb5b
15 files changed
+723
-3
No files matched your search
@@ -33,6 +33,48 @@ pub enum Event {
|
||||
Info(String),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
/// Stable JSON form (NDJSON mode, RPC notifications, extensions). Schema: README "Event stream".
|
||||
pub fn to_json(&self) -> Value {
|
||||
use serde_json::json;
|
||||
match self {
|
||||
Event::SessionStart => json!({"type": "session_start"}),
|
||||
Event::TurnStart => json!({"type": "turn_start"}),
|
||||
Event::ToolCall { id, name, args } => {
|
||||
json!({"type": "tool_call", "id": id, "name": name, "args": args})
|
||||
}
|
||||
Event::ToolResult {
|
||||
id,
|
||||
name,
|
||||
args,
|
||||
content,
|
||||
is_error,
|
||||
} => json!({"type": "tool_result", "id": id, "name": name, "args": args,
|
||||
"content": content, "is_error": is_error}),
|
||||
Event::TurnEnd => json!({"type": "turn_end"}),
|
||||
Event::AgentEnd => json!({"type": "agent_end"}),
|
||||
Event::Stream(e) => json!({"type": "stream", "event": e}),
|
||||
Event::Message(m) => json!({"type": "message", "message": m}),
|
||||
Event::Info(t) => json!({"type": "info", "text": t}),
|
||||
}
|
||||
}
|
||||
|
||||
/// `type` of `to_json`, for subscriptions.
|
||||
pub fn kind(&self) -> &'static str {
|
||||
match self {
|
||||
Event::SessionStart => "session_start",
|
||||
Event::TurnStart => "turn_start",
|
||||
Event::ToolCall { .. } => "tool_call",
|
||||
Event::ToolResult { .. } => "tool_result",
|
||||
Event::TurnEnd => "turn_end",
|
||||
Event::AgentEnd => "agent_end",
|
||||
Event::Stream(_) => "stream",
|
||||
Event::Message(_) => "message",
|
||||
Event::Info(_) => "info",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum HookAction {
|
||||
Continue,
|
||||
@@ -60,6 +102,10 @@ pub struct Outcome {
|
||||
}
|
||||
|
||||
impl Bus {
|
||||
pub fn add_arc(&mut self, h: Arc<dyn Hook>) -> &mut Self {
|
||||
self.hooks.push(h);
|
||||
self
|
||||
}
|
||||
pub fn add(&mut self, h: impl Hook + 'static) -> &mut Self {
|
||||
self.hooks.push(Arc::new(h));
|
||||
self
|
||||
@@ -94,6 +140,34 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn event_json_schema() {
|
||||
let e = Event::ToolCall {
|
||||
id: "1".into(),
|
||||
name: "bash".into(),
|
||||
args: json!({"cmd": "ls"}),
|
||||
};
|
||||
assert_eq!(
|
||||
e.to_json(),
|
||||
json!({"type": "tool_call", "id": "1", "name": "bash", "args": {"cmd": "ls"}})
|
||||
);
|
||||
assert_eq!(e.kind(), "tool_call");
|
||||
let s = Event::Stream(StreamEvent::TextDelta { text: "hi".into() });
|
||||
assert_eq!(
|
||||
s.to_json(),
|
||||
json!({"type": "stream", "event": {"type": "text_delta", "text": "hi"}})
|
||||
);
|
||||
for ev in [
|
||||
Event::SessionStart,
|
||||
Event::TurnStart,
|
||||
Event::TurnEnd,
|
||||
Event::AgentEnd,
|
||||
Event::Info("x".into()),
|
||||
] {
|
||||
assert_eq!(ev.to_json()["type"], ev.kind());
|
||||
}
|
||||
}
|
||||
|
||||
struct NoRm;
|
||||
#[async_trait]
|
||||
impl Hook for NoRm {
|
||||
|
||||
Reference in new issue
Block a user