use crate::{
CoreError, SessionId, ToolSpec,
trajectory::serialization::{optional_u64_as_string, u64_as_string},
};
pub use prompt::{TrajectoryPromptBlock, TrajectoryPromptSnapshot};
pub use record::{
TrajectoryLane, TrajectoryPayload, TrajectoryPayloadKind, TrajectoryRecord,
TrajectoryRecordDetails, TrajectoryRecordKind, TrajectoryRecordStatus, TrajectoryToolDetails,
};
use schemars::JsonSchema;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
mod prompt;
mod record;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema)]
#[serde(transparent)]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
pub struct TrajectoryTurnId(u64);
impl TrajectoryTurnId {
pub fn new(value: u64) -> Result<Self, CoreError> {
if value == 0 {
return Err(CoreError::InvalidIdentifier {
kind: "TrajectoryTurnId",
value: value.to_string(),
reason: "must be greater than zero",
});
}
Ok(Self(value))
}
#[must_use]
pub fn value(self) -> u64 {
self.0
}
}
impl Serialize for TrajectoryTurnId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&self.0)
}
}
impl<'de> Deserialize<'de> for TrajectoryTurnId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = u64_as_string::deserialize(deserializer)?;
Self::new(value).map_err(de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TrajectorySnapshot {
session_id: SessionId,
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
revision: u64,
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
latest_sequence: u64,
#[serde(default)]
#[schemars(extend("x-merry-output-required" = true))]
closed: bool,
#[serde(with = "optional_u64_as_string")]
#[schemars(
with = "Option<String>",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
history_truncated_before: Option<u64>,
prompt: TrajectoryPromptSnapshot,
tool_specs: Vec<ToolSpec>,
records: Vec<TrajectoryRecord>,
}
impl TrajectorySnapshot {
#[must_use]
pub fn new(session_id: SessionId) -> Self {
Self {
session_id,
revision: 0,
latest_sequence: 0,
closed: false,
history_truncated_before: None,
prompt: TrajectoryPromptSnapshot::default(),
tool_specs: Vec::new(),
records: Vec::new(),
}
}
#[must_use]
pub fn session_id(&self) -> &SessionId {
&self.session_id
}
#[must_use]
pub fn revision(&self) -> u64 {
self.revision
}
#[must_use]
pub fn latest_sequence(&self) -> u64 {
self.latest_sequence
}
#[must_use]
pub fn is_closed(&self) -> bool {
self.closed
}
pub fn mark_closed(&mut self) {
self.closed = true;
}
pub fn reopen(&mut self) {
self.closed = false;
}
#[must_use]
pub fn history_truncated_before(&self) -> Option<u64> {
self.history_truncated_before
}
#[must_use]
pub fn records(&self) -> &[TrajectoryRecord] {
&self.records
}
#[must_use]
pub fn prompt(&self) -> &TrajectoryPromptSnapshot {
&self.prompt
}
#[must_use]
pub fn tool_specs(&self) -> &[ToolSpec] {
&self.tool_specs
}
pub fn set_tool_specs(&mut self, mut tool_specs: Vec<ToolSpec>) {
tool_specs.sort_by(|left, right| left.name().as_str().cmp(right.name().as_str()));
self.tool_specs = tool_specs;
}
pub fn upsert_prompt_block(&mut self, block: TrajectoryPromptBlock) -> bool {
self.prompt.upsert_stable_block(block)
}
pub fn add_dynamic_context(&mut self, count: u64, sequence: u64) {
self.prompt.add_dynamic_context(count, sequence);
}
pub fn upsert_record(&mut self, record: TrajectoryRecord) -> bool {
if let Some(existing) = self
.records
.iter_mut()
.find(|existing| existing.id() == record.id())
{
if *existing == record {
return false;
}
*existing = record;
self.sort_records();
return true;
}
self.records.push(record);
self.sort_records();
true
}
fn sort_records(&mut self) {
self.records.sort_by_key(|record| {
(
record.start_sequence(),
record.sequence_order(),
record.id().as_str().to_owned(),
)
});
}
pub fn advance_latest_sequence(&mut self, sequence: u64) {
self.latest_sequence = self.latest_sequence.max(sequence);
}
pub fn advance_revision(&mut self) {
self.revision = self.revision.saturating_add(1);
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum TrajectoryEvent {
Snapshot { snapshot: TrajectorySnapshot },
RecordUpsert {
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
revision: u64,
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
latest_sequence: u64,
record: Box<TrajectoryRecord>,
},
PromptUpdated {
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
revision: u64,
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
latest_sequence: u64,
prompt: TrajectoryPromptSnapshot,
},
SessionClosed {
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
revision: u64,
#[serde(with = "u64_as_string")]
#[schemars(
with = "String",
extend(
"x-merry-wire-type" = "u64",
"x-merry-output-required" = true
)
)]
latest_sequence: u64,
},
}
mod serialization;
#[cfg(test)]
#[path = "trajectory_tests.rs"]
mod tests;