Current section
Files
Jump to
Current section
Files
src/lightspeed/agent/typestate.gleam
//// Typestate lifecycle for Lightspeed session agents.
/// Marker type for a disconnected agent.
pub type Disconnected {
Disconnected
}
/// Marker type for an agent performing protocol handshake.
pub type Handshaking {
Handshaking
}
/// Marker type for an agent whose component has mounted.
pub type Mounted {
Mounted
}
/// Marker type for an active live agent.
pub type Live {
Live
}
/// Marker type for an agent draining before shutdown.
pub type Draining {
Draining
}
/// Marker type for a terminated agent.
pub type Terminated {
Terminated
}
/// Lifecycle labels useful for logs and protocol diagnostics.
pub type Lifecycle {
DisconnectedLabel
HandshakingLabel
MountedLabel
LiveLabel
DrainingLabel
TerminatedLabel
}
/// A session agent parameterized by lifecycle state.
pub opaque type Agent(state) {
Agent(id: String, label: Lifecycle)
}
/// Create a disconnected agent.
pub fn new(id: String) -> Agent(Disconnected) {
Agent(id: id, label: DisconnectedLabel)
}
/// Transition from disconnected to handshaking.
pub fn handshake(agent: Agent(Disconnected)) -> Agent(Handshaking) {
Agent(id: agent.id, label: HandshakingLabel)
}
/// Transition from handshaking to mounted.
pub fn mount(agent: Agent(Handshaking)) -> Agent(Mounted) {
Agent(id: agent.id, label: MountedLabel)
}
/// Transition from mounted to live.
pub fn go_live(agent: Agent(Mounted)) -> Agent(Live) {
Agent(id: agent.id, label: LiveLabel)
}
/// Transition from live to draining.
pub fn drain(agent: Agent(Live)) -> Agent(Draining) {
Agent(id: agent.id, label: DrainingLabel)
}
/// Transition from draining to terminated.
pub fn terminate(agent: Agent(Draining)) -> Agent(Terminated) {
Agent(id: agent.id, label: TerminatedLabel)
}
/// Return the agent id.
pub fn id(agent: Agent(state)) -> String {
agent.id
}
/// Return the lifecycle label.
pub fn lifecycle(agent: Agent(state)) -> Lifecycle {
agent.label
}
/// Convert a lifecycle label to a stable string.
pub fn lifecycle_to_string(lifecycle: Lifecycle) -> String {
case lifecycle {
DisconnectedLabel -> "disconnected"
HandshakingLabel -> "handshaking"
MountedLabel -> "mounted"
LiveLabel -> "live"
DrainingLabel -> "draining"
TerminatedLabel -> "terminated"
}
}