feat: a2ui interface to enablee the LLM to give structured information
This commit is contained in:
@@ -60,6 +60,10 @@ pub(crate) struct ChatMessage {
|
||||
pub(super) reasoning_open: bool,
|
||||
pub(super) content: String,
|
||||
pub(super) markdown: markdown::Content,
|
||||
pub(super) a2ui_lines_processed: usize,
|
||||
pub(super) a2ui_errors: Vec<String>,
|
||||
pub(super) a2ui_replies: Vec<serde_json::Value>,
|
||||
pub(super) a2ui_open_urls: Vec<String>,
|
||||
}
|
||||
|
||||
impl ChatMessage {
|
||||
@@ -75,10 +79,11 @@ impl ChatMessage {
|
||||
pub(super) fn refresh_markdown(&mut self) {
|
||||
if !self.user && !self.tool {
|
||||
let visible = crate::agent::visible_content(&self.content);
|
||||
let visible = crate::a2ui::transcript_fallback(visible);
|
||||
let content = if self.reasoning.is_some() {
|
||||
visible.trim_start()
|
||||
} else {
|
||||
visible
|
||||
&visible
|
||||
};
|
||||
self.markdown = markdown::Content::parse(content);
|
||||
}
|
||||
@@ -99,12 +104,59 @@ impl From<StoredMessage> for ChatMessage {
|
||||
reasoning_open: false,
|
||||
content: message.content,
|
||||
markdown: iced::widget::markdown::Content::new(),
|
||||
a2ui_lines_processed: 0,
|
||||
a2ui_errors: Vec::new(),
|
||||
a2ui_replies: Vec::new(),
|
||||
a2ui_open_urls: Vec::new(),
|
||||
};
|
||||
message.refresh_markdown();
|
||||
message
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_a2ui_message(
|
||||
store: &mut crate::a2ui::Store,
|
||||
database: &mut Option<Database>,
|
||||
session_id: Option<i32>,
|
||||
message: &mut ChatMessage,
|
||||
) {
|
||||
let lines = crate::a2ui::extract_lines(&message.content);
|
||||
for (index, line) in lines.iter().enumerate().skip(message.a2ui_lines_processed) {
|
||||
let applied = match &line.value {
|
||||
Ok(value) => store.apply(value.clone(), line.raw.clone(), message.id),
|
||||
Err(error) => Err(error.clone()),
|
||||
};
|
||||
match applied {
|
||||
Ok(applied) => {
|
||||
if let Some(reply) = applied.reply {
|
||||
message.a2ui_replies.push(reply);
|
||||
}
|
||||
if let Some(url) = applied.open_url {
|
||||
message.a2ui_open_urls.push(url);
|
||||
}
|
||||
if let (Some(session_id), Some(database)) = (session_id, database.as_mut()) {
|
||||
for raw in applied.raws {
|
||||
if let Err(error) =
|
||||
database.insert_a2ui_message(session_id, message.id, &raw)
|
||||
{
|
||||
message.a2ui_errors.push(format!(
|
||||
"line {} could not be persisted: {error}",
|
||||
index + 1
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => message
|
||||
.a2ui_errors
|
||||
.push(format!("line {}: {error}", index + 1)),
|
||||
}
|
||||
}
|
||||
message.a2ui_lines_processed = lines.len();
|
||||
message.refresh_markdown();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn chat_turn(message: &ChatMessage) -> ChatTurn {
|
||||
ChatTurn {
|
||||
@@ -168,6 +220,15 @@ fn has_chat_after_last_compaction(messages: &[ChatMessage]) -> bool {
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn chat_system_prompt(&self, model: ModelChoice, prompt: &str) -> String {
|
||||
let mut prompt = crate::agent::system_prompt(model, prompt);
|
||||
if self.config.a2ui_enabled {
|
||||
prompt.push_str("\n\n");
|
||||
prompt.push_str(crate::a2ui::SYSTEM_PROMPT);
|
||||
}
|
||||
prompt
|
||||
}
|
||||
|
||||
pub(super) fn can_compact_session(&self, session_id: i32) -> bool {
|
||||
!self.generating
|
||||
&& self.selected_session == Some(session_id)
|
||||
@@ -238,13 +299,22 @@ impl App {
|
||||
}
|
||||
};
|
||||
effective.turn.system_prompt =
|
||||
crate::agent::system_prompt(model, &effective.turn.system_prompt);
|
||||
self.chat_system_prompt(model, &effective.turn.system_prompt);
|
||||
effective.turn.system_prompt = crate::compaction::summary_system_prompt(
|
||||
&effective.turn.system_prompt,
|
||||
self.compaction_summary(),
|
||||
);
|
||||
let assistant_reasoning = effective.turn.reasoning_mode != ReasoningMode::Direct;
|
||||
#[cfg(target_os = "macos")]
|
||||
let model_prompt = if self.config.a2ui_enabled {
|
||||
format!(
|
||||
"{prompt}\n\nA2UI client metadata:\n{}",
|
||||
self.a2ui.client_metadata()
|
||||
)
|
||||
} else {
|
||||
prompt.clone()
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let opening_turn = self.selected_session.is_none();
|
||||
#[cfg(target_os = "macos")]
|
||||
let mut injected_system = Vec::new();
|
||||
@@ -282,7 +352,7 @@ impl App {
|
||||
skip_previous_eos: false,
|
||||
reasoning: None,
|
||||
reasoning_complete: true,
|
||||
content: prompt.clone(),
|
||||
content: model_prompt,
|
||||
});
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -374,6 +444,9 @@ impl App {
|
||||
|
||||
fn system_prompt_reminders(&self, model: ModelChoice) -> Vec<String> {
|
||||
let mut reminders = vec![crate::agent::system_prompt_reminder(model)];
|
||||
if self.config.a2ui_enabled {
|
||||
reminders.push(crate::a2ui::SYSTEM_PROMPT.to_owned());
|
||||
}
|
||||
if !self.config.generation.system_prompt.trim().is_empty() {
|
||||
reminders.push(self.config.generation.system_prompt.clone());
|
||||
}
|
||||
@@ -475,6 +548,10 @@ impl App {
|
||||
#[cfg(target_os = "macos")]
|
||||
let mut start_queued = false;
|
||||
#[cfg(target_os = "macos")]
|
||||
let mut a2ui_feedback = None;
|
||||
#[cfg(target_os = "macos")]
|
||||
let mut a2ui_changed = false;
|
||||
#[cfg(target_os = "macos")]
|
||||
loop {
|
||||
match active.events.try_recv() {
|
||||
Ok(GenerationEvent::Loading) => {}
|
||||
@@ -500,6 +577,18 @@ impl App {
|
||||
}
|
||||
transcript_changed = true;
|
||||
}
|
||||
if !reasoning
|
||||
&& self.config.a2ui_enabled
|
||||
&& let Some(message) = self.conversation.last_mut()
|
||||
{
|
||||
sync_a2ui_message(
|
||||
&mut self.a2ui,
|
||||
&mut self.database,
|
||||
self.selected_session,
|
||||
message,
|
||||
);
|
||||
a2ui_changed = true;
|
||||
}
|
||||
}
|
||||
Ok(GenerationEvent::Context {
|
||||
used,
|
||||
@@ -530,6 +619,69 @@ impl App {
|
||||
!self.queued_inputs.is_empty() || self.manual_compaction_queued;
|
||||
}
|
||||
Ok(_) => {
|
||||
let (validation_errors, replies, open_urls, error_surface_id) = self
|
||||
.conversation
|
||||
.last_mut()
|
||||
.map(|message| {
|
||||
let surface_id = crate::a2ui::extract_lines(&message.content)
|
||||
.into_iter()
|
||||
.rev()
|
||||
.filter_map(|line| line.value.ok())
|
||||
.find_map(|value| {
|
||||
crate::a2ui::message_surface_id(&value)
|
||||
.map(str::to_owned)
|
||||
})
|
||||
.unwrap_or_else(|| "unknown".to_owned());
|
||||
(
|
||||
std::mem::take(&mut message.a2ui_errors),
|
||||
std::mem::take(&mut message.a2ui_replies),
|
||||
std::mem::take(&mut message.a2ui_open_urls),
|
||||
surface_id,
|
||||
)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
for url in open_urls {
|
||||
if let Err(error) =
|
||||
std::process::Command::new("open").arg(url).spawn()
|
||||
{
|
||||
self.error =
|
||||
Some(format!("Could not open the A2UI link: {error}"));
|
||||
}
|
||||
}
|
||||
if !validation_errors.is_empty() {
|
||||
self.generating = false;
|
||||
self.activity = Some("Correcting A2UI…".into());
|
||||
self.tool_cards.clear();
|
||||
a2ui_feedback = Some(
|
||||
serde_json::json!({
|
||||
"version": crate::a2ui::VERSION,
|
||||
"error": {
|
||||
"code": "VALIDATION_FAILED",
|
||||
"surfaceId": error_surface_id,
|
||||
"path": "/",
|
||||
"message": validation_errors.join("; ")
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
);
|
||||
self.active_generation = None;
|
||||
break;
|
||||
}
|
||||
if !replies.is_empty() {
|
||||
self.generating = false;
|
||||
self.activity = Some("Continuing A2UI function call…".into());
|
||||
self.tool_cards.clear();
|
||||
a2ui_feedback = Some(format!(
|
||||
"A2UI client response:\n{}",
|
||||
replies
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
));
|
||||
self.active_generation = None;
|
||||
break;
|
||||
}
|
||||
let model = self.config.model;
|
||||
let content = self
|
||||
.conversation
|
||||
@@ -586,6 +738,10 @@ impl App {
|
||||
}
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
if a2ui_changed {
|
||||
self.sync_a2ui_renderer_state();
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
if transcript_changed && let Some(message) = self.conversation.last_mut() {
|
||||
message.refresh_markdown();
|
||||
}
|
||||
@@ -633,6 +789,14 @@ impl App {
|
||||
self.start_next_queued();
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(feedback) = a2ui_feedback
|
||||
&& let Err(error) = self.continue_after_tool_result(&feedback)
|
||||
{
|
||||
self.generating = false;
|
||||
self.activity = Some("Failed".into());
|
||||
self.error = Some(error);
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
return transcript_changed;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
false
|
||||
@@ -698,7 +862,7 @@ impl App {
|
||||
&models_path(),
|
||||
)?;
|
||||
effective.turn.system_prompt =
|
||||
crate::agent::system_prompt(model, &effective.turn.system_prompt);
|
||||
self.chat_system_prompt(model, &effective.turn.system_prompt);
|
||||
effective.turn.system_prompt = crate::compaction::summary_system_prompt(
|
||||
&effective.turn.system_prompt,
|
||||
self.compaction_summary(),
|
||||
@@ -792,7 +956,7 @@ impl App {
|
||||
&models_path(),
|
||||
)?;
|
||||
effective.turn.system_prompt =
|
||||
crate::agent::system_prompt(model, &effective.turn.system_prompt);
|
||||
self.chat_system_prompt(model, &effective.turn.system_prompt);
|
||||
effective.turn.system_prompt = crate::compaction::summary_system_prompt(
|
||||
&effective.turn.system_prompt,
|
||||
self.compaction_summary(),
|
||||
@@ -941,7 +1105,7 @@ impl App {
|
||||
&models_path(),
|
||||
)?;
|
||||
effective.turn.system_prompt =
|
||||
crate::agent::system_prompt(model, &effective.turn.system_prompt);
|
||||
self.chat_system_prompt(model, &effective.turn.system_prompt);
|
||||
let rebuild_system_prompt = effective.turn.system_prompt.clone();
|
||||
effective.turn.system_prompt = crate::compaction::summary_system_prompt(
|
||||
&effective.turn.system_prompt,
|
||||
@@ -1364,6 +1528,10 @@ mod tests {
|
||||
content: "### Core / Setup\n\n| File | Lines |\n|---|---:|\n| `src/app.rs` | **1,750** |\n| `src/engine.rs` | 2,400 |\n\n### Summary\n\nDone."
|
||||
.to_owned(),
|
||||
markdown: iced::widget::markdown::Content::new(),
|
||||
a2ui_lines_processed: 0,
|
||||
a2ui_errors: Vec::new(),
|
||||
a2ui_replies: Vec::new(),
|
||||
a2ui_open_urls: Vec::new(),
|
||||
};
|
||||
|
||||
message.refresh_markdown();
|
||||
@@ -1394,6 +1562,10 @@ mod tests {
|
||||
reasoning_open: false,
|
||||
content: format!("message {id}"),
|
||||
markdown: iced::widget::markdown::Content::new(),
|
||||
a2ui_lines_processed: 0,
|
||||
a2ui_errors: Vec::new(),
|
||||
a2ui_replies: Vec::new(),
|
||||
a2ui_open_urls: Vec::new(),
|
||||
};
|
||||
let history = vec![
|
||||
message(1, false, None),
|
||||
@@ -1426,6 +1598,10 @@ mod tests {
|
||||
reasoning_open: false,
|
||||
content: format!("message {id}"),
|
||||
markdown: iced::widget::markdown::Content::new(),
|
||||
a2ui_lines_processed: 0,
|
||||
a2ui_errors: Vec::new(),
|
||||
a2ui_replies: Vec::new(),
|
||||
a2ui_open_urls: Vec::new(),
|
||||
};
|
||||
let mut history = vec![
|
||||
message(1, true, false, false, false),
|
||||
|
||||
Reference in New Issue
Block a user