use std::fmt::{self, Debug, Formatter};
use std::num::NonZeroU64;
use std::sync::Arc;
use crate::opaque::Envelope;
use crate::runtime::hook;
use crate::runtime::model::{Client, SamplingParams};
use crate::runtime::tool;
use crate::value::content::Part;
pub struct Config<M: Client> {
pub(super) model: Arc<M>,
pub(super) tools: tool::Registry,
pub(super) hooks: hook::RegistryBuilder,
pub(super) system_instruction: Vec<Part>,
pub(super) sampling: SamplingParams,
pub(super) model_extensions: Vec<Envelope>,
}
impl<M: Client> Debug for Config<M> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Config")
.field("tools", &self.tools)
.field("hooks", &self.hooks)
.field("system_instruction", &self.system_instruction)
.field("sampling", &self.sampling)
.field("model_extensions", &self.model_extensions)
.finish_non_exhaustive()
}
}
impl<M: Client> Config<M> {
#[must_use]
pub fn new(model: Arc<M>, tools: tool::Registry, hooks: hook::RegistryBuilder) -> Self {
Self {
model,
tools,
hooks,
system_instruction: Vec::new(),
sampling: SamplingParams::default(),
model_extensions: Vec::new(),
}
}
#[must_use]
pub fn system_instruction(&self) -> &[Part] {
&self.system_instruction
}
pub fn push_system_instruction(&mut self, part: Part) -> &mut Self {
self.system_instruction.push(part);
self
}
pub fn extend_system_instruction(
&mut self,
parts: impl IntoIterator<Item = Part>,
) -> &mut Self {
self.system_instruction.extend(parts);
self
}
pub fn clear_system_instruction(&mut self) -> &mut Self {
self.system_instruction.clear();
self
}
pub const fn sampling_mut(&mut self) -> &mut SamplingParams {
&mut self.sampling
}
#[must_use]
pub fn model_extensions(&self) -> &[Envelope] {
&self.model_extensions
}
pub fn push_model_extension(&mut self, extension: Envelope) -> &mut Self {
self.model_extensions.push(extension);
self
}
pub fn extend_model_extensions(
&mut self,
extensions: impl IntoIterator<Item = Envelope>,
) -> &mut Self {
self.model_extensions.extend(extensions);
self
}
pub fn clear_model_extensions(&mut self) -> &mut Self {
self.model_extensions.clear();
self
}
}
pub struct Revision<M: Client> {
pub(super) id: NonZeroU64,
pub(super) model: Arc<M>,
pub(super) tools: tool::Registry,
pub(super) hook_names: Arc<[String]>,
pub(super) hooks_require_session_state: bool,
pub(super) system_instruction: Arc<[Part]>,
pub(super) sampling: SamplingParams,
pub(super) model_extensions: Arc<[Envelope]>,
}
impl<M: Client> Debug for Revision<M> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Revision")
.field("id", &self.id)
.field("tools", &self.tools)
.field("hook_names", &self.hook_names)
.field(
"hooks_require_session_state",
&self.hooks_require_session_state,
)
.field("system_instruction", &self.system_instruction)
.field("sampling", &self.sampling)
.field("model_extensions", &self.model_extensions)
.finish_non_exhaustive()
}
}
impl<M: Client> Clone for Revision<M> {
fn clone(&self) -> Self {
Self {
id: self.id,
model: Arc::clone(&self.model),
tools: self.tools.clone(),
hook_names: Arc::clone(&self.hook_names),
hooks_require_session_state: self.hooks_require_session_state,
system_instruction: Arc::clone(&self.system_instruction),
sampling: self.sampling.clone(),
model_extensions: Arc::clone(&self.model_extensions),
}
}
}
impl<M: Client> Revision<M> {
#[must_use]
pub const fn id(&self) -> NonZeroU64 {
self.id
}
#[must_use]
pub const fn model(&self) -> &Arc<M> {
&self.model
}
#[must_use]
pub const fn tools(&self) -> &tool::Registry {
&self.tools
}
#[must_use]
pub fn hook_names(&self) -> &[String] {
&self.hook_names
}
#[must_use]
pub const fn hooks_require_session_state(&self) -> bool {
self.hooks_require_session_state
}
#[must_use]
pub fn system_instruction(&self) -> &[Part] {
&self.system_instruction
}
#[must_use]
pub const fn sampling(&self) -> &SamplingParams {
&self.sampling
}
#[must_use]
pub fn model_extensions(&self) -> &[Envelope] {
&self.model_extensions
}
}