2025-10-20 13:34:44 -07:00
|
|
|
|
use schemars::JsonSchema;
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
use ts_rs::TS;
|
|
|
|
|
|
|
2026-02-25 22:23:51 -08:00
|
|
|
|
/// Conservative cap so one user message cannot monopolize a large context window.
|
|
|
|
|
|
pub const MAX_USER_INPUT_TEXT_CHARS: usize = 1 << 20;
|
|
|
|
|
|
|
2025-10-20 13:34:44 -07:00
|
|
|
|
/// User input
|
|
|
|
|
|
#[non_exhaustive]
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, TS, JsonSchema)]
|
|
|
|
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
|
|
|
|
|
pub enum UserInput {
|
|
|
|
|
|
Text {
|
|
|
|
|
|
text: String,
|
2026-01-14 16:41:50 -08:00
|
|
|
|
/// UI-defined spans within `text` that should be treated as special elements.
|
|
|
|
|
|
/// These are byte ranges into the UTF-8 `text` buffer and are used to render
|
|
|
|
|
|
/// or persist rich input markers (e.g., image placeholders) across history
|
|
|
|
|
|
/// and resume without mutating the literal text.
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
|
text_elements: Vec<TextElement>,
|
2025-10-20 13:34:44 -07:00
|
|
|
|
},
|
|
|
|
|
|
/// Pre‑encoded data: URI image.
|
2026-01-14 16:41:50 -08:00
|
|
|
|
Image { image_url: String },
|
2025-10-20 13:34:44 -07:00
|
|
|
|
|
|
|
|
|
|
/// Local image path provided by the user. This will be converted to an
|
|
|
|
|
|
/// `Image` variant (base64 data URL) during request serialization.
|
2026-01-14 16:41:50 -08:00
|
|
|
|
LocalImage { path: std::path::PathBuf },
|
2025-12-10 13:59:17 -08:00
|
|
|
|
|
|
|
|
|
|
/// Skill selected by the user (name + path to SKILL.md).
|
|
|
|
|
|
Skill {
|
|
|
|
|
|
name: String,
|
|
|
|
|
|
path: std::path::PathBuf,
|
|
|
|
|
|
},
|
2026-01-28 19:51:58 -08:00
|
|
|
|
/// Explicit mention selected by the user (name + app://connector id).
|
|
|
|
|
|
Mention { name: String, path: String },
|
2025-10-20 13:34:44 -07:00
|
|
|
|
}
|
2026-01-14 16:41:50 -08:00
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, TS, JsonSchema)]
|
|
|
|
|
|
pub struct TextElement {
|
|
|
|
|
|
/// Byte range in the parent `text` buffer that this element occupies.
|
|
|
|
|
|
pub byte_range: ByteRange,
|
|
|
|
|
|
/// Optional human-readable placeholder for the element, displayed in the UI.
|
2026-01-20 14:04:11 -08:00
|
|
|
|
placeholder: Option<String>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl TextElement {
|
|
|
|
|
|
pub fn new(byte_range: ByteRange, placeholder: Option<String>) -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
byte_range,
|
|
|
|
|
|
placeholder,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 18:30:20 -08:00
|
|
|
|
/// Returns a copy of this element with a remapped byte range.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// The placeholder is preserved as-is; callers must ensure the new range
|
|
|
|
|
|
/// still refers to the same logical element (and same placeholder)
|
|
|
|
|
|
/// within the new text.
|
|
|
|
|
|
pub fn map_range<F>(&self, map: F) -> Self
|
|
|
|
|
|
where
|
|
|
|
|
|
F: FnOnce(ByteRange) -> ByteRange,
|
|
|
|
|
|
{
|
|
|
|
|
|
Self {
|
|
|
|
|
|
byte_range: map(self.byte_range),
|
|
|
|
|
|
placeholder: self.placeholder.clone(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 14:04:11 -08:00
|
|
|
|
pub fn set_placeholder(&mut self, placeholder: Option<String>) {
|
|
|
|
|
|
self.placeholder = placeholder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the stored placeholder without falling back to the text buffer.
|
|
|
|
|
|
///
|
2026-01-20 18:30:20 -08:00
|
|
|
|
/// This must only be used inside `From<TextElement>` implementations on equivalent
|
|
|
|
|
|
/// protocol types where the source text is unavailable. Prefer `placeholder(text)`
|
|
|
|
|
|
/// everywhere else.
|
2026-01-20 14:04:11 -08:00
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
|
pub fn _placeholder_for_conversion_only(&self) -> Option<&str> {
|
|
|
|
|
|
self.placeholder.as_deref()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn placeholder<'a>(&'a self, text: &'a str) -> Option<&'a str> {
|
|
|
|
|
|
self.placeholder
|
|
|
|
|
|
.as_deref()
|
|
|
|
|
|
.or_else(|| text.get(self.byte_range.start..self.byte_range.end))
|
|
|
|
|
|
}
|
2026-01-14 16:41:50 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, TS, JsonSchema)]
|
|
|
|
|
|
pub struct ByteRange {
|
|
|
|
|
|
/// Start byte offset (inclusive) within the UTF-8 text buffer.
|
|
|
|
|
|
pub start: usize,
|
|
|
|
|
|
/// End byte offset (exclusive) within the UTF-8 text buffer.
|
|
|
|
|
|
pub end: usize,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<std::ops::Range<usize>> for ByteRange {
|
|
|
|
|
|
fn from(range: std::ops::Range<usize>) -> Self {
|
|
|
|
|
|
Self {
|
|
|
|
|
|
start: range.start,
|
|
|
|
|
|
end: range.end,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|