Files
Genarrative/server-rs/crates/platform-agent/src/callbacks.rs
2026-05-08 11:44:42 +08:00

55 lines
1.2 KiB
Rust

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,
});
}
}