1
This commit is contained in:
54
server-rs/crates/platform-agent/src/callbacks.rs
Normal file
54
server-rs/crates/platform-agent/src/callbacks.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum CreativeAgentCallbackKind {
|
||||
Stage,
|
||||
ToolStarted,
|
||||
ToolCompleted,
|
||||
ModelRequestStarted,
|
||||
ModelRequestCompleted,
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CreativeAgentCallbackEvent {
|
||||
pub kind: CreativeAgentCallbackKind,
|
||||
pub label: String,
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
type CreativeAgentCallbackFn = Arc<dyn Fn(CreativeAgentCallbackEvent) + Send + Sync>;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct CreativeAgentCallbacks {
|
||||
on_event: Option<CreativeAgentCallbackFn>,
|
||||
}
|
||||
|
||||
impl CreativeAgentCallbacks {
|
||||
pub fn new<F>(on_event: F) -> Self
|
||||
where
|
||||
F: Fn(CreativeAgentCallbackEvent) + Send + Sync + 'static,
|
||||
{
|
||||
Self {
|
||||
on_event: Some(Arc::new(on_event)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn noop() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn emit(&self, event: CreativeAgentCallbackEvent) {
|
||||
if let Some(on_event) = &self.on_event {
|
||||
on_event(event);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stage(&self, label: impl Into<String>) {
|
||||
self.emit(CreativeAgentCallbackEvent {
|
||||
kind: CreativeAgentCallbackKind::Stage,
|
||||
label: label.into(),
|
||||
detail: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user