pub(crate) mod secret;
use serde::{Deserialize, Serialize};
pub use crate::config::{EmbedConfig, Header, TokenSourceConfig};
pub use crate::error::{EmbedError, ErrorKind};
pub use crate::secret::Secret;
pub type RequestId = u64;
pub type SessionId = u64;
pub type DialogueId = u64;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
macro_rules! data {
($($item:item)*) => { $(
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
$item
)* };
}
macro_rules! word {
($($item:item)*) => { $(
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
$item
)* };
}
mod dialogue_graph;
pub use dialogue_graph::*;
pub(crate) mod update;
pub use update::UpdateAvailable;
data! {
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Command {
ProvideToken { token: Secret, expires_at: String, #[serde(default, deserialize_with = "update::lenient")] update_available: Option<UpdateAvailable> },
ProvideTokenFor { token: Secret, expires_in_secs: u32, #[serde(default, deserialize_with = "update::lenient")] update_available: Option<UpdateAvailable> },
TokenUnavailable { reason: String },
SetTokenEndpoint { url: String, #[serde(default)] headers: Vec<Header> },
StreamPause,
StreamResume,
SendContext { context: WorldContext },
PracticeStart { source: PracticeSource },
PracticeSnapshot { session: SessionId },
PracticeAnswer { session: SessionId, answer: String },
PracticeChoose { session: SessionId, option: u32 },
PracticeReveal { session: SessionId },
PracticeSelfGrade { session: SessionId, correct: bool },
PracticeNext { session: SessionId },
PracticeFinish { session: SessionId },
DialogueOpen { config: DialogueConfig },
DialogueSay { dialogue: DialogueId, line: String },
DialogueSetLevel { dialogue: DialogueId, level: u8 },
DialogueSetScene { dialogue: DialogueId, #[serde(default)] scene: Option<String> },
DialogueCancel { dialogue: DialogueId },
DialogueClose { dialogue: DialogueId },
DialogueSnapshot { dialogue: DialogueId },
DialogueLoad { source: DialogueSource, session: GraphSession },
DialogueSetFact { dialogue: DialogueId, key: String, value: FactValue },
DialogueValidate { ir_json: String },
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum CommandOutcome {
Done {},
Id { id: u64 },
Practice { snapshot: Option<PracticeSnapshot> },
Dialogue { snapshot: Option<DialogueSnapshot> },
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum EmbedEvent {
TokenNeeded { request: RequestId, reason: TokenReason },
Stream { state: StreamState, cursor: Option<String> },
Lingara { event: LingaraEvent },
WorldSent { request: RequestId, event_id: String, reaction: Option<Reaction> },
Practice { session: SessionId, snapshot: PracticeSnapshot },
NpcTurn { dialogue: DialogueId, request: RequestId, update: NpcTurnUpdate },
CheckpointReached { dialogue: DialogueId, checkpoint: String, edge: Option<String>, via: CrossedVia },
DialogueEnded { dialogue: DialogueId, outcome: String, checkpoint: String, edge: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] trace: Option<Vec<TracedBeat>> },
DialoguePlanned { dialogue: DialogueId, outcome: PlanOutcome, checkpoints: Vec<String>, #[serde(default, skip_serializing_if = "Option::is_none")] reason: Option<PlanKeptReason> },
DialogueLoaded { dialogue: DialogueId, dialogue_id: String, rev: u64 },
DialogueLoadFailed { dialogue: DialogueId, error: EmbedError },
Error { error: EmbedError },
Log { level: LogLevel, target: String, message: String },
UpdateAvailable { latest: Option<String>, deprecated: bool, message: Option<String> },
Closed { reason: CloseReason },
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum StreamState {
Connecting,
Live,
Paused,
Down { error: EmbedError, retry_in_ms: u64 },
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum NpcTurnUpdate {
Opened,
Fragment { text: String },
Notice { code: String, message: String },
Finished { text: String },
Cancelled,
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum PracticeSource {
Request { topic: String, source_lang: String, target_lang: String, level: u8, #[serde(default)] tags: Vec<String> },
Create { context: String, source_lang: String, target_lang: String, level: u8 },
Plan { plan_id: String, #[serde(default = "yes")] report: bool },
}
pub struct LingaraEvent {
pub id: String,
pub event_type: String,
pub created_at: String,
pub data_json: String,
pub detail: LingaraEventDetail,
}
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum LingaraEventDetail {
LessonPlanReady { plan_id: String, title: Option<String>, level: u8, source_lang: String, target_lang: String, complete: bool },
LessonPlanFailed { plan_id: String, reason: String },
Other,
}
pub struct Reaction {
pub status: String,
#[serde(default)]
pub plan_id: Option<String>,
#[serde(default)]
pub plan_status: Option<String>,
#[serde(default)]
pub code: Option<String>,
#[serde(default)]
pub error: Option<String>,
}
pub struct WorldContext {
pub scene: String,
pub source_lang: String,
pub target_lang: String,
pub level: u8,
#[serde(default)]
pub npc: Option<Npc>,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub generate: bool,
}
pub struct Npc {
pub name: String,
#[serde(default)]
pub persona: Option<String>,
}
pub struct PracticeSnapshot {
pub phase: PracticePhase,
pub plan: Option<PlanSummary>,
pub set_index: u32,
pub set_count: u32,
pub question_index: u32,
pub question_count: u32,
pub question: Option<PracticeQuestion>,
pub last_result: Option<AnswerResult>,
pub failure: Option<EmbedError>,
}
pub struct PlanSummary {
pub id: String,
pub title: Option<String>,
pub level: u8,
pub source_lang: String,
pub target_lang: String,
pub vocabulary: Vec<PlanWord>,
}
pub struct PlanWord {
pub word: String,
pub translation: String,
#[serde(default)]
pub pronunciation: Option<String>,
}
pub struct PracticeQuestion {
pub kind: String,
pub prompt: String,
pub options: Option<Vec<String>>,
pub hint: Option<String>,
pub self_graded: bool,
}
pub struct AnswerResult {
pub outcome: Option<PracticeOutcome>,
pub expected: String,
pub explanation: String,
}
pub struct WindowEntry {
pub speaker: Speaker,
pub text: String,
}
pub struct EmbedStatus {
pub stream: StreamState,
pub cursor: Option<String>,
pub needs_token: bool,
pub closed: bool,
}
}
word! {
pub enum TokenReason { Initial, Expiring, Rejected }
#[derive(PartialOrd, Ord)]
pub enum LogLevel { Error, Warn, Info, Debug, Trace }
pub enum CloseReason { Requested, Internal }
pub enum PracticePhase {
Requesting, Generating, Creating, Loading, Unanswered, AwaitingSelfGrade, Reviewed, SetComplete,
Reporting, Reported, ReportFailed, Failed, Ended,
}
pub enum PracticeOutcome { Correct, Incorrect, Revealed, SelfCorrect, SelfIncorrect, Skipped }
pub enum DialoguePhase { Idle, Sending, Streaming, Ended, Loading }
pub enum Speaker { Player, Npc }
}
fn yes() -> bool {
true
}