use super::attention::TabUpdate;
use super::logic::{agent_options, mode_option, model_options};
use super::sign_in::SignIn;
use super::tabs::{LastTurn, Queued, Tab, TabState};
use super::toolbar::ToolbarModel;
use super::{
AgentHandle, ChatCommand, ChatId, ChatPanel, Command, ContextRef, Entry, Event, InputIntent,
Pending, Submission, TranscriptEntry,
};
const WAITING_ROW: &str = "waiting-for-chat-engine";
impl ChatPanel {
pub(super) fn connected(&mut self, result: Result<AgentHandle, String>) {
self.reset_tabs(Tab::new(1));
match result {
Ok(handle) => {
self.engine = handle.engine();
self.handle = Some(handle);
self.error = None;
self.errors.legacy = None;
}
Err(error) => {
self.handle = None;
self.set_error(error);
}
}
}
pub(super) fn start(&mut self, cx: &mut super::Context<Self>) {
if self.is_disabled() {
return;
}
#[cfg(debug_assertions)]
if let Some(fixture) = super::failure_view::fixture::from_env() {
self.show_fixture(fixture);
self.log_accepted_errors(cx);
return;
}
let tools = self
.providers
.tools
.as_ref()
.and_then(|provider| provider(cx));
self.connected(super::interaction::start_runtime(tools, cx));
self.log_accepted_errors(cx);
}
fn reset_tabs(&mut self, tab: Tab) {
self.tabs = vec![tab];
self.active = 0;
self.next_chat = super::FIRST_CHAT;
self.sign_in = None;
}
#[cfg(debug_assertions)]
pub(super) fn show_fixture(&mut self, fixture: super::failure_view::fixture::Fixture) {
self.reset_tabs(fixture.tab());
self.handle = None;
self.error = None;
self.errors.legacy = None;
}
pub(super) fn replace_current(&mut self, context: Option<ContextRef>) -> bool {
let changed = self.current != context;
self.current = context;
changed
}
pub(super) fn command_result(&mut self, result: Result<(), String>) -> bool {
if let Err(error) = result {
self.tab_mut().set_error(error);
return false;
}
true
}
pub(super) fn toolbar_model(&self) -> ToolbarModel {
let tab = self.tab();
let capabilities = self.capabilities();
ToolbarModel {
copilot_controls: capabilities.copilot_controls,
show_ai_units: capabilities.ai_units(),
models: model_options(&self.models),
model: tab.model.clone().map(Into::into),
effort: tab.effort.clone().map(Into::into),
mode: mode_option(tab.mode),
approve_all: tab.yolo,
agents: agent_options(&self.agents),
agent: tab.agent.clone().map(Into::into),
auto_context: self.auto_context,
show_tools: self.show_tools,
show_thinking: self.show_thinking,
busy: tab.is_busy(),
ready: tab.is_ready() && !tab.read_only,
has_selection: self.current.is_some(),
usage: tab.usage,
}
}
pub(super) fn prepare_submission(
&mut self,
intent: InputIntent,
context: Vec<ContextRef>,
) -> Submission {
let mut submission = Submission {
command: None,
clear: true,
starts_turn: false,
steering: false,
answer_id: None,
last_turn: None,
};
match intent {
InputIntent::AddContext => self.tab_mut().context = context,
InputIntent::EnableYolo => {
if self.capabilities().copilot_controls {
self.tab_mut().confirm_yolo = true;
} else {
self.tab_mut().set_error(
"YOLO is unavailable with Pi. Choose Copilot in Settings to use it.".into(),
);
submission.clear = false;
}
}
InputIntent::DisableYolo => {
if self.capabilities().copilot_controls {
self.tab_mut().confirm_yolo = false;
submission.command = Some(self.chat_command(ChatCommand::SetYolo(false)));
} else {
self.tab_mut().set_error(
"YOLO is unavailable with Pi. Choose Copilot in Settings to use it.".into(),
);
submission.clear = false;
}
}
InputIntent::Rename(name) => {
submission.command = Some(self.chat_command(ChatCommand::Rename(name)));
}
InputIntent::RunSkill { name, args } => {
submission.starts_turn = true;
submission.command =
Some(self.chat_command(ChatCommand::RunCommand { name, args }));
}
InputIntent::UnknownCommand => {
let commands = if self.capabilities().copilot_controls {
"Commands: /new, /close, /rename <name>, /yolo on, /yolo off, /add-context, /sessions, /model, or a listed skill."
} else {
"Commands: /new, /close, /rename <name>, /add-context, /sessions, or /model."
};
self.tab_mut().set_error(commands.into());
submission.clear = false;
}
InputIntent::NewChat
| InputIntent::CloseChat
| InputIntent::OpenSessions
| InputIntent::OpenModels => {}
InputIntent::Message(text) => self.message(&mut submission, text, context),
}
submission
}
fn message(&mut self, submission: &mut Submission, text: String, context: Vec<ContextRef>) {
let tab = self.tab();
match tab.pending.first() {
Some(Pending::Question { id, .. }) => {
submission.answer_id = Some(id.clone());
submission.command = Some(Command::Answer {
request_id: id.clone(),
answer: text,
});
}
_ if !tab.is_ready() => self.queue(text, context),
_ => {
submission.starts_turn = true;
submission.steering = tab.is_busy();
let turn = LastTurn {
text,
context,
images: tab.images.clone(),
};
submission.command = Some(self.chat_command(turn.clone().into_command()));
submission.last_turn = (!submission.steering).then_some(turn);
}
}
}
fn queue(&mut self, text: String, context: Vec<ContextRef>) {
let waiting = format!(
"Waiting for {} to start. Your message goes as soon as it does.",
self.engine_label()
);
let tab = self.tab_mut();
tab.entries.push(TranscriptEntry {
id: WAITING_ROW.to_owned(),
entry: Entry::Info(waiting),
});
let images = tab.images.clone();
tab.queued = Some(Queued {
text,
context,
images,
});
}
pub(super) fn chat_command(&self, command: ChatCommand) -> Command {
Command::Chat {
chat: self.tab().id,
command,
}
}
pub(super) fn finish_submission(&mut self, submission: Submission, accepted: bool) -> bool {
if !accepted {
return false;
}
if submission.starts_turn {
self.tab_mut().begin();
}
if let Some(turn) = submission.last_turn {
self.tab_mut().last_turn = Some(turn);
}
submission.clear
}
pub(super) fn apply_event(&mut self, event: Event) -> Option<(ChatId, TabUpdate)> {
match event {
Event::Error(error) => self.set_error(error),
Event::SignInRequired(offer) => self.sign_in = Some(SignIn::offered(offer)),
Event::Notice(notice) => self.view.notice = Some(notice),
Event::Restore { .. } => {
self.strip().receive(event);
let carried = std::mem::take(&mut self.carried);
super::carry::restore(&mut self.tabs, carried);
}
Event::Chat { .. } => return self.strip().receive(event),
catalog => self.apply_catalog(catalog),
}
None
}
pub(super) fn strip(&mut self) -> TabState<'_> {
TabState {
tabs: &mut self.tabs,
active: &mut self.active,
next_chat: &mut self.next_chat,
}
}
fn apply_catalog(&mut self, event: Event) {
match event {
Event::Models(models) => self.models = models,
Event::Agents(agents) => self.agents = agents,
Event::Sessions(sessions) => self.sessions = sessions,
Event::Commands(commands) => self.commands = commands,
_ => {}
}
}
}