use super::follow::{Delegation, Following, follow};
use crate::channels::{Empty, YieldClone, YieldCloneResourceRequested, YieldCloneTokenRequested};
use crate::client::Links;
use crate::errors::SemiontError;
use crate::media_types::{clone_format, derive_storage_uri};
use crate::resources::primary_media_type;
use crate::timing::{
GENERATION_STALL_ASSUMED_TOKENS_COUNT, GENERATION_STALL_FLOOR, GENERATION_STALL_PER_TOKEN,
};
use crate::transport::{ContentTransport, Envelope, PutBinaryRequest, Upload};
use crate::types::ResourceId;
use crate::types::{
CloneResourceWithTokenResponse, CreateResourceResponse, GatheredContextFocus,
GenerationJobParams, ResourceDescriptor, YieldCloneResourceRequest, YieldCloneTokenRequest,
YieldJobCompleteCommand, YieldJobCreateCommand,
};
use bytes::Bytes;
use std::sync::Arc;
use std::time::Duration;
pub fn stall_deadline(max_tokens: Option<f64>) -> Duration {
let tokens = max_tokens.unwrap_or(GENERATION_STALL_ASSUMED_TOKENS_COUNT as f64);
GENERATION_STALL_FLOOR.max(GENERATION_STALL_PER_TOKEN.mul_f64(tokens.max(0.0)))
}
#[derive(Debug, Clone)]
pub struct CreateFromTokenOptions {
pub token: String,
pub name: String,
pub content: String,
pub archive_original: Option<bool>,
}
pub struct YieldNamespace {
links: Links,
content: Arc<dyn ContentTransport>,
}
impl YieldNamespace {
pub(crate) fn new(links: Links, content: Arc<dyn ContentTransport>) -> YieldNamespace {
YieldNamespace { links, content }
}
pub fn resource(&self, data: PutBinaryRequest) -> Upload {
self.content.put_binary(data)
}
pub fn delegate(
&self,
params: GenerationJobParams,
stall_deadline: Option<Duration>,
) -> Delegation<YieldJobCompleteCommand> {
let within = stall_deadline.unwrap_or_else(|| self::stall_deadline(params.max_tokens));
let resource_id = match ¶ms.context.focus {
GatheredContextFocus::Resource(focus) => focus.resource.id.clone(),
GatheredContextFocus::Annotation(focus) => focus.source_resource.id.clone(),
};
follow(
self.links.clone(),
Following {
create: YieldJobCreateCommand::new(params).into(),
resource_id,
stall: Some(within),
},
)
}
pub async fn clone_token(
&self,
resource_id: &ResourceId,
) -> Result<CloneResourceWithTokenResponse, SemiontError> {
let answer = self
.links
.request::<YieldCloneTokenRequested>(&YieldCloneTokenRequest {
resource_id: resource_id.clone(),
})
.await?;
Ok(answer.response)
}
pub async fn from_token(&self, token: &str) -> Result<ResourceDescriptor, SemiontError> {
let answer = self
.links
.request::<YieldCloneResourceRequested>(&YieldCloneResourceRequest {
token: token.to_owned(),
})
.await?;
Ok(answer.response.source_resource)
}
pub async fn create_from_token(
&self,
options: CreateFromTokenOptions,
) -> Result<CreateResourceResponse, SemiontError> {
let source = self.from_token(&options.token).await?;
let format = clone_format(primary_media_type(&source));
let storage_uri = derive_storage_uri(&options.name, format);
let created = self
.content
.put_binary(PutBinaryRequest {
clone_token: Some(options.token),
archive_original: options.archive_original,
..PutBinaryRequest::new(
options.name,
Bytes::from(options.content),
format.media_type,
storage_uri,
)
})
.await?;
Ok(created)
}
pub fn clone(&self) {
self.links
.signal::<YieldClone>(&Empty {}, Envelope::default());
}
}