diff --git a/Cargo.lock b/Cargo.lock index eac7c32deb..3bdab11425 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7963,6 +7963,7 @@ dependencies = [ "secrecy", "serde_json", "tablepro-account", + "tablepro-ai", "tablepro-core", "tablepro-driver-cassandra", "tablepro-driver-clickhouse", diff --git a/core/ARCHITECTURE.md b/core/ARCHITECTURE.md index fd8f9302aa..0531bdb497 100644 --- a/core/ARCHITECTURE.md +++ b/core/ARCHITECTURE.md @@ -49,7 +49,7 @@ core/crates/ | `cli` | no workspace crate | | `ai` | `core`, `net` | | `account` | `core`, `storage` | -| `ffi` | `account`, `core`, `session`, `ssh`, `storage`, and every driver behind its default `engines` feature | +| `ffi` | `account`, `ai`, `core`, `session`, `ssh`, `storage`, and every driver behind its default `engines` feature | | An app, such as `linux/crates/app` | every crate above, and its platform crate | Rules, enforced by review: @@ -114,6 +114,7 @@ Each boundary error has a stable `code()`, such as `driver.timeout` or `ssh.host - One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, opens a `QueryEditor`, and opens a `TableEditor`, which plans a table tab's typed edits into a `RowSave` and runs it (`docs/decisions/0019-row-edits-through-the-ffi.md`). `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. - `Core::open_account` opens the TablePro account as an `AccountService`, whose `AccountListener` only signals that `status()` changed. It also shares links: `create_share` builds a result's snapshot from a `ResultSet` in Rust, and a table page's `view_sql` is the query its view stands for ([ADR 0027](docs/decisions/0027-share-links.md)). `create_share` takes the share sheet's Keep, Mask or Remove for each column, and `ResultSet::share_masking`, `share_preview` and `share_summary` start the sheet's columns from the detectors and show and count what a share sends ([ADR 0028](docs/decisions/0028-data-masking.md)). `scan_sql_for_secrets`, `scan_columns_for_secrets`, `scan_sql_for_personal_data` and `share_token_from_link` are functions of their own. +- `Core::ai_assistant` gives an `AiAssistant` over the core's one AI client and its secret store: it says whether a provider's key is kept and keeps or removes one (the key never crosses back), lists the provider's models, reads a session's catalog into the system prompt of a conversation (`start_conversation`), and streams a reply as an `AiReply`, drained after an `AiReplyListener` call as a run is. Both refuse a saved connection whose AI policy is Never with `ai.blocked`; asking under Ask is the app's ([ADR 0032](docs/decisions/0032-ai-through-the-ffi.md)). - The grid reads a `ResultSet` through the C ABI in `src/window_abi.rs`: `tablepro_cursor_fetch` packs the display text of one visible range, and `tablepro_cursor_free` ends the cursor. - The library owns one tokio runtime. Each async export runs on it and is aborted when the foreign side drops the call; a `CancelToken` stops it sooner, since UniFFI cannot cancel. - A result set takes rows as the driver reads them. Each column keeps a kind byte, a raw `i64` and the exact text `tablepro_core::export::value_text` writes, or a binary value's bytes, in chunks of 65,536 rows. A window only cuts and copies. diff --git a/core/crates/ai/src/ai_error.rs b/core/crates/ai/src/ai_error.rs index 824cc0fd93..81cdb7800e 100644 --- a/core/crates/ai/src/ai_error.rs +++ b/core/crates/ai/src/ai_error.rs @@ -63,6 +63,39 @@ impl AiError { } impl AiError { + /// The error with `secret` taken out of every text a server or the + /// transport wrote, so a server that echoes the key it was sent cannot + /// put it on the user's screen. + pub(crate) fn without(self, secret: &str) -> Self { + if secret.is_empty() { + return self; + } + let scrub = |text: String| text.replace(secret, "[redacted]"); + match self { + Self::Tls(detail) => Self::Tls(scrub(detail)), + Self::Unreachable { host, detail } => Self::Unreachable { + host, + detail: scrub(detail), + }, + Self::Unauthorized { host, message } => Self::Unauthorized { + host, + message: message.map(scrub), + }, + Self::RateLimited { host, message } => Self::RateLimited { + host, + message: message.map(scrub), + }, + Self::Status { host, status, message } => Self::Status { + host, + status, + message: message.map(scrub), + }, + Self::Provider(message) => Self::Provider(scrub(message)), + Self::Protocol(detail) => Self::Protocol(scrub(detail)), + other => other, + } + } + /// What the server said, when it said anything. pub fn server_message(&self) -> Option<&str> { match self { @@ -79,6 +112,25 @@ impl AiError { mod tests { use super::*; + #[test] + fn a_key_the_server_echoes_is_taken_out_of_what_it_said() { + let echoed = AiError::Unauthorized { + host: "gpu-box".to_owned(), + message: Some("bad key sk-local, try again".to_owned()), + }; + let streamed = AiError::Provider("sk-local is revoked".to_owned()); + + assert_eq!( + echoed.without("sk-local").server_message(), + Some("bad key [redacted], try again") + ); + assert_eq!( + streamed.without("sk-local"), + AiError::Provider("[redacted] is revoked".to_owned()) + ); + assert_eq!(AiError::TooLong.without(""), AiError::TooLong); + } + #[test] fn each_error_names_its_variant_with_a_code_that_does_not_change() { assert_eq!(AiError::MissingModel.code(), "ai.missing_model"); diff --git a/core/crates/ai/src/endpoint.rs b/core/crates/ai/src/endpoint.rs index a239b50d2b..ca9edef8f8 100644 --- a/core/crates/ai/src/endpoint.rs +++ b/core/crates/ai/src/endpoint.rs @@ -82,6 +82,15 @@ impl Endpoint { self.key.as_ref() } + /// An error as it may leave the crate: without the key this endpoint + /// sends. + pub(crate) fn scrub(&self, error: AiError) -> AiError { + match &self.key { + Some(key) => error.without(key.expose_secret()), + None => error, + } + } + pub(crate) fn url(&self, path: &str) -> String { format!("{}{path}", self.base) } diff --git a/core/crates/ai/src/model_list.rs b/core/crates/ai/src/model_list.rs index 095464f2c3..cb083f18a3 100644 --- a/core/crates/ai/src/model_list.rs +++ b/core/crates/ai/src/model_list.rs @@ -11,6 +11,12 @@ const LISTING_LIMIT: usize = 4 * 1024 * 1024; /// The models the provider offers the key, sorted. pub async fn list_models(client: &AiClient, endpoint: &Endpoint) -> Result, AiError> { + read_models(client, endpoint) + .await + .map_err(|error| endpoint.scrub(error)) +} + +async fn read_models(client: &AiClient, endpoint: &Endpoint) -> Result, AiError> { let path = match Protocol::of(endpoint.kind()) { Protocol::AnthropicMessages => "/models?limit=1000", Protocol::ChatCompletions => "/models", diff --git a/core/crates/ai/src/reply_stream.rs b/core/crates/ai/src/reply_stream.rs index b9e7d45a55..dc2fda4be2 100644 --- a/core/crates/ai/src/reply_stream.rs +++ b/core/crates/ai/src/reply_stream.rs @@ -38,7 +38,7 @@ pub async fn stream_reply( }; let last = match outcome { Ok(end) => ReplyEvent::Finished(end), - Err(error) => ReplyEvent::Failed(error), + Err(error) => ReplyEvent::Failed(endpoint.scrub(error)), }; // The receiver may be gone already, which only means no one waits. let _ = events.send(last); @@ -56,7 +56,9 @@ pub async fn collect_reply( text.push_str(&part); true }; - let end = read_reply(client, endpoint, request, &mut on_text).await?; + let end = read_reply(client, endpoint, request, &mut on_text) + .await + .map_err(|error| endpoint.scrub(error))?; Ok((text, end)) } diff --git a/core/crates/ffi/Cargo.toml b/core/crates/ffi/Cargo.toml index 05f0eb5723..e9f4a01310 100644 --- a/core/crates/ffi/Cargo.toml +++ b/core/crates/ffi/Cargo.toml @@ -27,6 +27,7 @@ engines = [ [dependencies] tablepro-account.workspace = true +tablepro-ai.workspace = true tablepro-core.workspace = true tablepro-driver-cassandra = { workspace = true, optional = true } tablepro-driver-clickhouse = { workspace = true, optional = true } diff --git a/core/crates/ffi/src/ai/ai_assistant.rs b/core/crates/ffi/src/ai/ai_assistant.rs new file mode 100644 index 0000000000..859cfbe153 --- /dev/null +++ b/core/crates/ffi/src/ai/ai_assistant.rs @@ -0,0 +1,360 @@ +use std::sync::Arc; + +use secrecy::{ExposeSecret, SecretString}; +use tablepro_ai::{AiClient, ApiKeyVault, ChatRequest, Endpoint, chat_system_prompt}; +use tablepro_core::credentials::SecretItems; +use tablepro_core::{AiPolicy, AiProvider}; +use tablepro_session::runtime::Tasks; +use tablepro_storage::ConnectionStore; +use tokio::sync::{OnceCell, mpsc}; +use uuid::Uuid; + +use super::ai_reply::ReplyState; +use super::{ + AiActionTag, AiChatArg, AiConversationArg, AiConversationStart, AiProviderInfo, AiProviderTag, AiReply, + AiReplyEvent, AiReplyListener, AiSettingsArg, schema_reader, +}; +use crate::cancel_token::{CancelToken, token}; +use crate::error::CoreError; +use crate::library::secret_error; +use crate::runtime::on_runtime; +use crate::session::DatabaseSession; + +// The providers, their keys in the platform's store, and the replies. The key never crosses back to the app, and a +// saved connection whose AI policy is Never sends nothing; asking under Ask is the app's. +#[derive(uniffi::Object)] +pub struct AiAssistant { + inner: Arc, +} + +struct Inner { + items: Arc, + store: Option, + tasks: Tasks, + version: String, + // Built once, off the caller's thread: it reads the system's certificates. + client: Arc>, +} + +impl AiAssistant { + pub(crate) fn new( + items: Arc, + store: Option, + tasks: Tasks, + version: String, + client: Arc>, + ) -> Self { + Self { + inner: Arc::new(Inner { + items, + store, + tasks, + version, + client, + }), + } + } +} + +#[uniffi::export] +impl AiAssistant { + // Whether a key is kept for the provider; the key itself stays in the store. + pub async fn has_key(&self, provider: AiProviderTag) -> Result { + let items = self.inner.items.clone(); + on_runtime(&self.inner.tasks, async move { + let key = items.load_key(provider.into()).await.map_err(secret_error)?; + Ok(key.is_some_and(|key| !key.expose_secret().trim().is_empty())) + }) + .await + } + + // A blank key removes the one kept. Blanks around a pasted key are dropped, since a header cannot carry a break. + pub async fn set_key(&self, provider: AiProviderTag, key: SecretString) -> Result<(), CoreError> { + let items = self.inner.items.clone(); + on_runtime(&self.inner.tasks, async move { + let trimmed = key.expose_secret().trim(); + let provider = AiProvider::from(provider); + if trimmed.is_empty() { + items.delete_key(provider).await + } else { + items.store_key(provider, &SecretString::from(trimmed)).await + } + .map_err(secret_error) + }) + .await + } + + // The models the provider offers the key, sorted. + pub async fn list_models( + &self, + settings: AiSettingsArg, + cancel: Option>, + ) -> Result, CoreError> { + let inner = self.inner.clone(); + let cancel = token(cancel); + on_runtime(&self.inner.tasks, async move { + let (client, endpoint) = inner.connect(&settings).await?; + tokio::select! { + biased; + () = cancel.cancelled() => Err(CoreError::Cancelled), + listed = tablepro_ai::list_models(&client, &endpoint) => Ok(listed?), + } + }) + .await + } + + // Reads the catalog for the tables `asked` names and writes the system prompt the conversation keeps. + pub async fn start_conversation( + &self, + session: Arc, + conversation: AiConversationArg, + cancel: Option>, + ) -> Result { + let inner = self.inner.clone(); + let cancel = token(cancel); + on_runtime(&self.inner.tasks, async move { + inner.refuse_if_never(&session).await?; + let editor = conversation.editor.filter(|text| !text.trim().is_empty()); + let named_in = match &editor { + Some(editor) => format!("{editor}\n{}", conversation.asked), + None => conversation.asked, + }; + let read = schema_reader::read( + session.session().clone(), + conversation.engine, + &named_in, + conversation.database, + ); + let context = tokio::select! { + biased; + () = cancel.cancelled() => return Err(CoreError::Cancelled), + context = read => context, + }; + Ok(AiConversationStart { + system: chat_system_prompt(&context, editor.as_deref()), + described: context.described_names().into_iter().map(str::to_owned).collect(), + }) + }) + .await + } + + // Returns at once; a refusal, a missing key and a failed request arrive as the reply's Failed event. + pub fn reply( + &self, + session: Arc, + settings: AiSettingsArg, + chat: AiChatArg, + listener: Box, + ) -> Arc { + let reply = Arc::new(AiReply::new(listener)); + let state = reply.state(); + let inner = self.inner.clone(); + drop( + self.inner + .tasks + .spawn_task(async move { inner.stream(&session, &settings, chat, &state).await }), + ); + reply + } +} + +impl Inner { + // A reply stops with its session, as an editor does, and one asked of a closed session never starts. + async fn stream(&self, session: &DatabaseSession, settings: &AiSettingsArg, chat: AiChatArg, state: &ReplyState) { + let cancel = state.cancel.clone(); + let shutdown = session.session().shutdown_signal(); + let stopped = async { + tokio::select! { + () = cancel.cancelled() => {} + () = shutdown.cancelled() => {} + } + }; + tokio::pin!(stopped); + let ready = tokio::select! { + biased; + () = &mut stopped => None, + ready = self.ready_to_send(session, settings) => Some(ready), + }; + let (client, endpoint) = match ready { + None => return state.post(AiReplyEvent::Stopped), + Some(Err(error)) => return state.post(AiReplyEvent::failed(error)), + Some(Ok(ready)) => ready, + }; + let request = ChatRequest::from(chat); + let (sender, mut receiver) = mpsc::unbounded_channel(); + let streaming = tablepro_ai::stream_reply(&client, &endpoint, &request, cancel.clone(), sender); + let forwarding = async { + while let Some(event) = receiver.recv().await { + state.post(event.into()); + } + }; + // Dropping the stream on a stop ends its request. + tokio::select! { + biased; + () = &mut stopped => {} + _ = async { tokio::join!(streaming, forwarding) } => {} + } + // A stop ends the stream with neither Finished nor Failed; after either, this is not posted. + state.post(AiReplyEvent::Stopped); + } + + async fn ready_to_send( + &self, + session: &DatabaseSession, + settings: &AiSettingsArg, + ) -> Result<(AiClient, Endpoint), CoreError> { + self.refuse_if_never(session).await?; + self.connect(settings).await + } + + async fn connect(&self, settings: &AiSettingsArg) -> Result<(AiClient, Endpoint), CoreError> { + let endpoint = self.endpoint(settings).await?; + Ok((self.client().await?, endpoint)) + } + + async fn endpoint(&self, settings: &AiSettingsArg) -> Result { + let provider = AiProvider::from(settings.provider); + let key = match self.items.load_key(provider).await { + Ok(key) => key, + // A local server usually takes no key, so a store that cannot be read does not stop it. + Err(error) if !provider.requires_key() => { + tracing::debug!(%error, "no key read for the compatible server"); + None + } + Err(error) => return Err(secret_error(error)), + }; + Ok(Endpoint::new(provider, &settings.base_url(), &settings.model, key)?) + } + + async fn client(&self) -> Result { + let tasks = self.tasks.clone(); + let version = self.version.clone(); + self.client + .get_or_try_init(|| async move { + tasks + .spawn_blocking_task(move || AiClient::new(&version)) + .await + .map_err(|failure| CoreError::failed("ffi.panicked", failure.to_string()))? + .map_err(CoreError::from) + }) + .await + .cloned() + } + + // Read again before every request, so a policy changed in the connection form applies to the next one. + async fn refuse_if_never(&self, session: &DatabaseSession) -> Result<(), CoreError> { + let Some(store) = self.store.clone() else { + return Ok(()); + }; + let Ok(id) = Uuid::parse_str(&session.id()) else { + return Ok(()); + }; + let connections = self + .tasks + .spawn_blocking_task(move || store.load_blocking()) + .await + .map_err(|failure| CoreError::failed("ffi.panicked", failure.to_string()))??; + let never = connections + .iter() + .any(|connection| connection.id == id && connection.ai_policy == AiPolicy::Never); + if never { + return Err(CoreError::failed( + "ai.blocked", + "the connection's AI policy sends nothing from it", + )); + } + Ok(()) + } +} + +impl std::fmt::Debug for AiAssistant { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AiAssistant").finish_non_exhaustive() + } +} + +// In the order a settings page lists them. +#[uniffi::export] +pub fn ai_providers() -> Vec { + AiProvider::ALL + .into_iter() + .map(|provider| AiProviderInfo { + provider: provider.into(), + default_base_url: provider.default_base_url().to_owned(), + requires_key: provider.requires_key(), + takes_address: provider == AiProvider::OpenAiCompatible, + }) + .collect() +} + +// The user's message for an editor action on `statement`; `error` is the server's, for Fix. +#[uniffi::export] +pub fn ai_action_message(action: AiActionTag, statement: String, error: Option) -> String { + tablepro_ai::action_message(action.into(), &statement, error.as_deref()) +} + +// The host the settings send requests to, for naming where the data goes before anything is sent. +#[uniffi::export] +pub fn ai_host_of(settings: AiSettingsArg) -> Option { + Endpoint::host_of(&settings.base_url()) +} + +// Whether a compatible server's address is an http or https URL with a host and no query. +#[uniffi::export] +pub fn ai_address_is_valid(address: String) -> bool { + Endpoint::new(AiProvider::OpenAiCompatible, &address, "", None).is_ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn each_provider_says_where_it_goes_and_whether_it_needs_a_key() { + let providers = ai_providers(); + + assert_eq!( + providers.iter().map(|info| info.provider).collect::>(), + [ + AiProviderTag::Anthropic, + AiProviderTag::OpenAi, + AiProviderTag::OpenAiCompatible + ] + ); + assert!(providers[0].requires_key && !providers[0].takes_address); + assert!(!providers[2].requires_key && providers[2].takes_address); + } + + #[test] + fn the_host_named_is_the_one_the_settings_reach() { + let settings = |provider, address: &str| AiSettingsArg { + provider, + model: String::new(), + address: Some(address.to_owned()), + }; + + assert_eq!( + ai_host_of(settings(AiProviderTag::OpenAi, "http://elsewhere/v1")).as_deref(), + Some("api.openai.com") + ); + assert_eq!( + ai_host_of(settings(AiProviderTag::OpenAiCompatible, "http://gpu-box:11434/v1")).as_deref(), + Some("gpu-box") + ); + } + + #[test] + fn an_address_is_checked_as_a_request_would_take_it() { + assert!(ai_address_is_valid("http://localhost:11434/v1".to_owned())); + assert!(!ai_address_is_valid("localhost:11434/v1".to_owned())); + assert!(!ai_address_is_valid("http://host/v1?x=1".to_owned())); + } + + #[test] + fn an_action_message_carries_the_statement_and_the_error() { + let message = ai_action_message(AiActionTag::Fix, "SELEC 1".to_owned(), Some("syntax error".to_owned())); + + assert!(message.contains("SELEC 1"), "{message}"); + assert!(message.contains("syntax error"), "{message}"); + } +} diff --git a/core/crates/ffi/src/ai/ai_reply.rs b/core/crates/ffi/src/ai/ai_reply.rs new file mode 100644 index 0000000000..5bb83f48dc --- /dev/null +++ b/core/crates/ffi/src/ai/ai_reply.rs @@ -0,0 +1,221 @@ +use std::panic::{AssertUnwindSafe, catch_unwind}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; + +use tablepro_ai::{ReplyEnd, ReplyEvent}; +use tokio_util::sync::CancellationToken; + +use super::AiStopTag; +use crate::error::{BoxedErrorInfo, CoreError}; + +// In order: text as it arrives, then one of Finished, Failed or Stopped. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)] +pub enum AiReplyEvent { + // `part` and not `text`, since C# cannot give a record a member of its own name. + Text { + part: String, + }, + Finished { + stop: AiStopTag, + input_tokens: Option, + output_tokens: Option, + }, + Failed { + error: BoxedErrorInfo, + }, + // Cancelled before it ended. + Stopped, +} + +impl From for AiReplyEvent { + fn from(end: ReplyEnd) -> Self { + Self::Finished { + stop: end.stop.into(), + input_tokens: end.usage.input_tokens, + output_tokens: end.usage.output_tokens, + } + } +} + +impl From for AiReplyEvent { + fn from(event: ReplyEvent) -> Self { + match event { + ReplyEvent::Text(part) => Self::Text { part }, + ReplyEvent::Finished(end) => end.into(), + ReplyEvent::Failed(error) => Self::failed(error.into()), + } + } +} + +impl AiReplyEvent { + pub(crate) fn failed(error: CoreError) -> Self { + Self::Failed { + error: error.into_info(), + } + } + + fn ends(&self) -> bool { + !matches!(self, Self::Text { .. }) + } +} + +// Told that events are waiting, on a runtime thread. It only schedules a drain on the app's own thread. +#[uniffi::export(callback_interface)] +pub trait AiReplyListener: Send + Sync { + fn events_pending(&self); +} + +// The app's handle on a reply on its way. Dropping it stops the reply. +#[derive(uniffi::Object)] +pub struct AiReply { + state: Arc, +} + +// What the reply's task posts into and the app drains. A drain clears the pending wake before it takes the queue, +// so no event is lost and a busy app is woken once. +pub(crate) struct ReplyState { + listener: Box, + queue: Mutex, + wake_pending: AtomicBool, + pub(crate) cancel: CancellationToken, +} + +#[derive(Default)] +struct Queue { + events: Vec, + ended: bool, +} + +impl AiReply { + pub(crate) fn new(listener: Box) -> Self { + Self { + state: Arc::new(ReplyState { + listener, + queue: Mutex::new(Queue::default()), + wake_pending: AtomicBool::new(false), + cancel: CancellationToken::new(), + }), + } + } + + pub(crate) fn state(&self) -> Arc { + self.state.clone() + } +} + +impl ReplyState { + // Called on a runtime thread. Text that arrives while the app is busy joins the text it has not read. + pub(crate) fn post(&self, event: AiReplyEvent) { + { + let mut queue = self.queue.lock().unwrap_or_else(PoisonError::into_inner); + if queue.ended { + return; + } + queue.ended = event.ends(); + match (queue.events.last_mut(), event) { + (Some(AiReplyEvent::Text { part }), AiReplyEvent::Text { part: more }) => part.push_str(&more), + (_, event) => queue.events.push(event), + } + } + self.wake(); + } + + // Outside the lock. UniFFI panics when the app's callback throws, which loses this wake and not the reply. + fn wake(&self) { + if self.wake_pending.swap(true, Ordering::AcqRel) { + return; + } + if catch_unwind(AssertUnwindSafe(|| self.listener.events_pending())).is_err() { + tracing::warn!("the app's reply listener failed, so it was not told about waiting events"); + } + } + + fn drain(&self) -> Vec { + self.wake_pending.store(false, Ordering::Release); + std::mem::take(&mut self.queue.lock().unwrap_or_else(PoisonError::into_inner).events) + } +} + +#[uniffi::export] +impl AiReply { + // Every event since the last drain, which also acknowledges the wake. + pub fn drain_events(&self) -> Vec { + self.state.drain() + } + + pub fn cancel(&self) { + self.state.cancel.cancel(); + } +} + +impl Drop for AiReply { + fn drop(&mut self) { + self.state.cancel.cancel(); + } +} + +impl std::fmt::Debug for AiReply { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AiReply").finish_non_exhaustive() + } +} + +#[cfg(test)] +mod tests { + use std::sync::atomic::AtomicUsize; + + use super::*; + + #[derive(Default)] + struct Count(Arc); + + impl AiReplyListener for Count { + fn events_pending(&self) { + self.0.fetch_add(1, Ordering::SeqCst); + } + } + + fn text(text: &str) -> AiReplyEvent { + AiReplyEvent::Text { part: text.to_owned() } + } + + #[test] + fn text_the_app_has_not_read_comes_as_one_event_and_wakes_it_once() { + let wakes = Arc::new(AtomicUsize::new(0)); + let reply = AiReply::new(Box::new(Count(wakes.clone()))); + let state = reply.state(); + + state.post(text("SELECT ")); + state.post(text("1")); + let first = reply.drain_events(); + state.post(text(";")); + state.post(AiReplyEvent::Stopped); + let second = reply.drain_events(); + + assert_eq!(first, [text("SELECT 1")]); + assert_eq!(second, [text(";"), AiReplyEvent::Stopped]); + assert_eq!(wakes.load(Ordering::SeqCst), 2); + } + + #[test] + fn nothing_follows_the_event_that_ends_a_reply() { + let reply = AiReply::new(Box::new(Count::default())); + let state = reply.state(); + + state.post(AiReplyEvent::Stopped); + state.post(text("late")); + state.post(AiReplyEvent::Stopped); + + assert_eq!(reply.drain_events(), [AiReplyEvent::Stopped]); + } + + #[test] + fn dropping_the_handle_stops_the_reply() { + let reply = AiReply::new(Box::new(Count::default())); + let state = reply.state(); + + drop(reply); + + assert!(state.cancel.is_cancelled()); + } +} diff --git a/core/crates/ffi/src/ai/ai_types.rs b/core/crates/ffi/src/ai/ai_types.rs new file mode 100644 index 0000000000..dfa471258e --- /dev/null +++ b/core/crates/ffi/src/ai/ai_types.rs @@ -0,0 +1,212 @@ +use tablepro_ai::{Action, ChatMessage, ChatRequest, StopReason}; +use tablepro_core::AiProvider; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, uniffi::Enum)] +pub enum AiProviderTag { + Anthropic, + OpenAi, + // Ollama, and any other server that speaks OpenAI's Chat Completions. + OpenAiCompatible, +} + +impl From for AiProvider { + fn from(tag: AiProviderTag) -> Self { + match tag { + AiProviderTag::Anthropic => Self::Anthropic, + AiProviderTag::OpenAi => Self::OpenAi, + AiProviderTag::OpenAiCompatible => Self::OpenAiCompatible, + } + } +} + +impl From for AiProviderTag { + fn from(provider: AiProvider) -> Self { + match provider { + AiProvider::Anthropic => Self::Anthropic, + AiProvider::OpenAi => Self::OpenAi, + AiProvider::OpenAiCompatible => Self::OpenAiCompatible, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiProviderInfo { + pub provider: AiProviderTag, + // The address requests go to, version path included. + pub default_base_url: String, + pub requires_key: bool, + // Only the compatible server's address is the user's to change. + pub takes_address: bool, +} + +// What the settings name. `address` is read for the compatible server only; None there is its default. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiSettingsArg { + pub provider: AiProviderTag, + pub model: String, + pub address: Option, +} + +impl AiSettingsArg { + pub(crate) fn base_url(&self) -> String { + let provider = AiProvider::from(self.provider); + match (provider, self.address.as_deref().map(str::trim)) { + (AiProvider::OpenAiCompatible, Some(address)) if !address.is_empty() => address.to_owned(), + _ => provider.default_base_url().to_owned(), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum AiActionTag { + Explain, + Review, + Optimize, + // Correct a statement the server refused, with its error. + Fix, +} + +impl From for Action { + fn from(tag: AiActionTag) -> Self { + match tag { + AiActionTag::Explain => Self::Explain, + AiActionTag::Review => Self::Review, + AiActionTag::Optimize => Self::Optimize, + AiActionTag::Fix => Self::Fix, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum AiRoleTag { + User, + Assistant, +} + +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiMessageArg { + pub role: AiRoleTag, + pub text: String, +} + +// One turn's request: the system prompt the conversation started with, and every message so far. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiChatArg { + pub system: String, + pub messages: Vec, +} + +impl From for ChatRequest { + fn from(chat: AiChatArg) -> Self { + Self { + system: chat.system, + messages: chat + .messages + .into_iter() + .map(|message| match message.role { + AiRoleTag::User => ChatMessage::user(message.text), + AiRoleTag::Assistant => ChatMessage::assistant(message.text), + }) + .collect(), + max_tokens: ChatRequest::CHAT_MAX_TOKENS, + } + } +} + +// `asked` is the question or the statement, whose table names pick the structure sent; `editor` is the editor's +// text for a typed question. `database` is the one the editor runs in, None for the connection's. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiConversationArg { + // The engine as the user knows it, such as PostgreSQL. + pub engine: String, + pub asked: String, + pub editor: Option, + pub database: Option, +} + +// The system prompt a conversation keeps, and the tables whose structure it carries. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct AiConversationStart { + pub system: String, + pub described: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum AiStopTag { + Complete, + // The reply reached the token limit and stops mid-way. + LengthLimit, + // The model declined to answer. + Refused, +} + +impl From for AiStopTag { + fn from(stop: StopReason) -> Self { + match stop { + StopReason::Complete => Self::Complete, + StopReason::LengthLimit => Self::LengthLimit, + StopReason::Refused => Self::Refused, + } + } +} + +// The fields of an `ai.*` error: the host it is about, an HTTP status, the provider whose key is missing. +// `detail` is the transport's or parser's English, shown after the app's own sentence. +#[derive(Debug, Clone, Default, PartialEq, Eq, uniffi::Record)] +pub struct AiFailureInfo { + pub host: Option, + pub status: Option, + pub provider: Option, + pub detail: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_the_compatible_server_takes_an_address() { + let hosted = AiSettingsArg { + provider: AiProviderTag::Anthropic, + model: String::new(), + address: Some("http://elsewhere.example/v1".to_owned()), + }; + let compatible = AiSettingsArg { + provider: AiProviderTag::OpenAiCompatible, + model: String::new(), + address: Some(" http://gpu-box:11434/v1 ".to_owned()), + }; + let blank = AiSettingsArg { + address: Some(" ".to_owned()), + ..compatible.clone() + }; + + assert_eq!(hosted.base_url(), "https://api.anthropic.com/v1"); + assert_eq!(compatible.base_url(), "http://gpu-box:11434/v1"); + assert_eq!(blank.base_url(), "http://localhost:11434/v1"); + } + + #[test] + fn a_chat_keeps_its_turns_in_order_with_the_chat_limit() { + let request = ChatRequest::from(AiChatArg { + system: "system".to_owned(), + messages: vec![ + AiMessageArg { + role: AiRoleTag::User, + text: "first".to_owned(), + }, + AiMessageArg { + role: AiRoleTag::Assistant, + text: "one".to_owned(), + }, + ], + }); + + assert_eq!(request.system, "system"); + assert_eq!( + request.messages, + [ChatMessage::user("first"), ChatMessage::assistant("one")] + ); + assert_eq!(request.max_tokens, ChatRequest::CHAT_MAX_TOKENS); + } +} diff --git a/core/crates/ffi/src/ai/mod.rs b/core/crates/ffi/src/ai/mod.rs new file mode 100644 index 0000000000..099f3d4083 --- /dev/null +++ b/core/crates/ffi/src/ai/mod.rs @@ -0,0 +1,11 @@ +mod ai_assistant; +mod ai_reply; +mod ai_types; +mod schema_reader; + +pub use ai_assistant::{AiAssistant, ai_action_message, ai_address_is_valid, ai_host_of, ai_providers}; +pub use ai_reply::{AiReply, AiReplyEvent, AiReplyListener}; +pub use ai_types::{ + AiActionTag, AiChatArg, AiConversationArg, AiConversationStart, AiFailureInfo, AiMessageArg, AiProviderInfo, + AiProviderTag, AiRoleTag, AiSettingsArg, AiStopTag, +}; diff --git a/core/crates/ffi/src/ai/schema_reader.rs b/core/crates/ffi/src/ai/schema_reader.rs new file mode 100644 index 0000000000..d1a43cda64 --- /dev/null +++ b/core/crates/ffi/src/ai/schema_reader.rs @@ -0,0 +1,181 @@ +use std::collections::HashSet; +use std::sync::Arc; + +use tablepro_ai::{ColumnShape, MAX_DESCRIBED_TABLES, QueryLanguage, SchemaContext, TableShape}; +use tablepro_core::dialect::DatabaseLayout; +use tablepro_core::meta::{ObjectInfo, ObjectScope, TableRef}; +use tablepro_session::Session; + +// The schema the user is in, its tables, and the structure of the tables `text` names. Read from the +// session's catalog, which the tree and the filters share: never a row. +pub(crate) async fn read(session: Arc, engine: String, text: &str, database: Option) -> SchemaContext { + let dialect = session.current_dialect(); + let language = match &dialect { + Some(dialect) if !dialect.grammar().is_sql() => QueryLanguage::Commands, + _ => QueryLanguage::Sql, + }; + let own = session.database().filter(|name| !name.is_empty()).map(str::to_owned); + let elsewhere = database.filter(|name| !name.is_empty() && Some(name) != own.as_ref()); + let layout = dialect.map(|dialect| dialect.database_layout()); + let (scope, schema, database) = match elsewhere { + // Where the server's databases are the catalog's schemas, the editor's is read as one. + Some(name) if layout == Some(DatabaseLayout::AsSchemas) => { + (ObjectScope::Schema(name.clone()), Some(name.clone()), Some(name)) + } + // Nothing the connection's catalog holds is in that database. + Some(name) => { + return SchemaContext { + engine, + language, + database: Some(name), + ..SchemaContext::default() + }; + } + None => (ObjectScope::DefaultSchema, default_schema(&session).await, own), + }; + let objects = match session.objects(scope).await { + Ok(objects) => objects, + Err(error) => { + tracing::warn!(%error, "the tables for the assistant could not be read"); + Arc::from([]) + } + }; + let browsable: Vec<&ObjectInfo> = objects.iter().filter(|object| object.kind.is_browsable()).collect(); + let mut described = Vec::new(); + for object in named_in(text, &browsable).into_iter().take(MAX_DESCRIBED_TABLES) { + let table = TableRef::new(object.schema.clone(), object.name.clone()); + match session.columns(table).await { + Ok(columns) if !columns.is_empty() => described.push(TableShape { + name: object.name.clone(), + columns: columns + .iter() + .map(|column| ColumnShape { + name: column.name.clone(), + type_name: column.column_type.name.clone(), + nullable: column.nullable, + primary_key: column.primary_key, + }) + .collect(), + }), + Ok(_) => {} + Err(error) => tracing::warn!(%error, "a table's columns for the assistant could not be read"), + } + } + SchemaContext { + engine, + language, + database, + schema, + tables: browsable.iter().map(|object| object.name.clone()).collect(), + described, + } +} + +async fn default_schema(session: &Session) -> Option { + let schemas = session.schemas().await.ok()?; + schemas + .iter() + .find(|schema| schema.is_default) + .map(|schema| schema.name.clone()) +} + +// The objects whose names stand in `text` as words, ignoring case, in the order the catalog lists them. A question +// in words names tables as words too, so one rule serves a statement and a question. A name a statement has to quote, +// as `order-items`, is found where it stands whole. +fn named_in<'a>(text: &str, objects: &[&'a ObjectInfo]) -> Vec<&'a ObjectInfo> { + let lowered = text.to_lowercase(); + let words: HashSet<&str> = lowered + .split(|c: char| !is_word(c)) + .filter(|word| !word.is_empty()) + .collect(); + objects + .iter() + .copied() + .filter(|object| { + let name = object.name.to_lowercase(); + if name.chars().all(is_word) { + words.contains(name.as_str()) + } else { + stands_whole(&lowered, &name) + } + }) + .collect() +} + +fn is_word(c: char) -> bool { + c.is_alphanumeric() || c == '_' +} + +// `name` occurs in `text` with no word character joined to either end. +fn stands_whole(text: &str, name: &str) -> bool { + if name.trim().is_empty() { + return false; + } + text.match_indices(name).any(|(start, found)| { + let before = text[..start].chars().next_back(); + let after = text[start + found.len()..].chars().next(); + !before.is_some_and(is_word) && !after.is_some_and(is_word) + }) +} + +#[cfg(test)] +mod tests { + use tablepro_core::meta::ObjectKind; + + use super::*; + + fn object(name: &str, kind: ObjectKind) -> ObjectInfo { + ObjectInfo { + schema: Some("public".to_owned()), + name: name.to_owned(), + kind, + parent_table: None, + } + } + + #[test] + fn a_statement_or_a_question_names_the_tables_it_mentions_in_catalog_order() { + let objects = [ + object("orders", ObjectKind::Table), + object("Customers", ObjectKind::View), + object("items", ObjectKind::Table), + object("order_total", ObjectKind::Table), + ]; + let listed: Vec<&ObjectInfo> = objects.iter().collect(); + + let question = named_in("Which customers have open orders?", &listed); + let statement = named_in("SELECT * FROM public.items i JOIN \"Orders\" o ON true", &listed); + + assert_eq!( + question.iter().map(|object| object.name.as_str()).collect::>(), + ["orders", "Customers"] + ); + assert_eq!( + statement.iter().map(|object| object.name.as_str()).collect::>(), + ["orders", "items"] + ); + } + + #[test] + fn a_name_that_needs_quotes_is_found_where_it_stands_whole() { + let objects = [ + object("order-items", ObjectKind::Table), + object("Line Items", ObjectKind::Table), + object("price$", ObjectKind::View), + object("order", ObjectKind::Table), + ]; + let listed: Vec<&ObjectInfo> = objects.iter().collect(); + + let named = named_in("SELECT * FROM \"order-items\" JOIN [line items] ON true", &listed); + let partial = named_in("SELECT * FROM \"order-items2\", price$s", &listed); + + assert_eq!( + named.iter().map(|object| object.name.as_str()).collect::>(), + ["order-items", "Line Items", "order"] + ); + assert_eq!( + partial.iter().map(|object| object.name.as_str()).collect::>(), + ["order"] + ); + } +} diff --git a/core/crates/ffi/src/core_handle.rs b/core/crates/ffi/src/core_handle.rs index cd3380585c..5591c288a0 100644 --- a/core/crates/ffi/src/core_handle.rs +++ b/core/crates/ffi/src/core_handle.rs @@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex}; use futures::future::BoxFuture; use secrecy::SecretString; use tablepro_account::{AccountSettings, RefreshTokenVault}; +use tablepro_ai::AiClient; use tablepro_core::credentials::{CredentialInteraction, CredentialPrompter, SecretItems, SecretKind, SecretVault}; use tablepro_core::{ ConnectEnvironment, Credentials, DatabaseDriver, DriverError, DriverRegistry, FileEndpoint, LivenessPolicy, @@ -20,6 +21,7 @@ use tablepro_storage::{ConnectionStore, CredentialDemand, KeyringCredentials, Sa use tokio::sync::OnceCell; use uuid::Uuid; +use crate::ai::AiAssistant; use crate::cancel_token::{CancelToken, token}; use crate::drivers::{self, DriverInfo}; use crate::error::CoreError; @@ -50,6 +52,9 @@ pub struct Core { history: Arc>>, pub(crate) tasks: Tasks, pub(crate) ssh: Arc, + client_version: String, + // One AI client for the app, built on first use. + ai_client: Arc>, } #[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] @@ -99,6 +104,17 @@ impl Core { drivers } + // Every assistant shares the core's one AI client. + pub fn ai_assistant(&self) -> Arc { + Arc::new(AiAssistant::new( + self.items.clone(), + self.store.clone(), + self.tasks.clone(), + self.client_version.clone(), + self.ai_client.clone(), + )) + } + pub fn library(&self) -> Result, CoreError> { let store = self.store.clone().ok_or_else(no_storage)?; Ok(Arc::new(ConnectionLibrary::new( @@ -318,6 +334,7 @@ impl Core { let liveness = LivenessPolicy::DESKTOP; let deadlines = Arc::new(DefaultDeadlines::new(liveness)); let ssh = Arc::new(InProcessSsh::system()); + let client_version = config.client_version.clone(); let services = ConnectServices { environment: ConnectEnvironment::new(config.client_version), ssh: ssh.clone(), @@ -341,6 +358,8 @@ impl Core { history: Arc::default(), tasks, ssh, + client_version, + ai_client: Arc::default(), }) } diff --git a/core/crates/ffi/src/error.rs b/core/crates/ffi/src/error.rs index 6b85dc1ac9..7228edf64f 100644 --- a/core/crates/ffi/src/error.rs +++ b/core/crates/ffi/src/error.rs @@ -2,9 +2,11 @@ use std::path::Path; use std::sync::Arc; use tablepro_account::AccountError; +use tablepro_ai::AiError; use tablepro_core::{DriverError, ServerDiagnostics, SshFailure, TlsFailure, TransportError}; use tablepro_session::{ConnectError, OperationError}; +use crate::ai::AiFailureInfo; use crate::host::SecretKindTag; /// Why a call produced no value. A cancel is its own variant, so a @@ -34,6 +36,9 @@ pub struct ErrorInfo { pub secret: Option, pub host_key: Option, pub tls_failure: Option, + // What an `ai.*` error is about; the provider's own words are in `server`. + #[uniffi(default = None)] + pub ai: Option, } // Error details stay off the stack while bindings keep the same record values. @@ -113,6 +118,7 @@ impl CoreError { secret: None, host_key: None, tls_failure: None, + ai: None, }), } } @@ -154,6 +160,7 @@ impl ErrorInfo { DriverError::Tls { failure, .. } => Some((*failure).into()), _ => None, }, + ai: None, } } @@ -167,6 +174,7 @@ impl ErrorInfo { secret: None, host_key: None, tls_failure: None, + ai: None, } } } @@ -340,6 +348,42 @@ impl From for CoreError { } } +// The host, status and provider are typed fields the app words; what the provider said goes in `server`. +impl From for CoreError { + fn from(error: AiError) -> Self { + let server = error.server_message().map(named_only); + let mut ai = AiFailureInfo::default(); + match &error { + AiError::MissingKey(provider) => ai.provider = Some((*provider).into()), + AiError::Tls(detail) | AiError::Protocol(detail) => ai.detail = Some(detail.clone()), + AiError::Unreachable { host, detail } => { + ai.host = Some(host.clone()); + ai.detail = Some(detail.clone()); + } + AiError::TimedOut { host } | AiError::Unauthorized { host, .. } | AiError::RateLimited { host, .. } => { + ai.host = Some(host.clone()); + } + AiError::Status { host, status, .. } => { + ai.host = Some(host.clone()); + ai.status = Some(*status); + } + AiError::MissingModel + | AiError::InvalidAddress + | AiError::KeyOverPlainHttp + | AiError::InvalidKey + | AiError::Provider(_) + | AiError::TooLong => {} + } + Self::Failed { + info: Box::new(ErrorInfo { + server, + ai: Some(ai), + ..ErrorInfo::plain(&[error.code()], error.to_string()) + }), + } + } +} + #[cfg(test)] mod tests { use std::path::PathBuf; diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index ac16e653c4..106dbabf6f 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -1,6 +1,7 @@ // Foreign code is called only from a runtime thread, never under a lock (core/ARCHITECTURE.md, Bindings). mod account; +mod ai; mod cancel_token; mod catalog; mod catalog_change; @@ -48,6 +49,11 @@ pub use account::{ ShareRequestArg, ShareTeamInfo, SharedLinkInfo, TeamInfo, scan_columns_for_secrets, scan_sql_for_secrets, share_token_from_link, }; +pub use ai::{ + AiActionTag, AiAssistant, AiChatArg, AiConversationArg, AiConversationStart, AiFailureInfo, AiMessageArg, + AiProviderInfo, AiProviderTag, AiReply, AiReplyEvent, AiReplyListener, AiRoleTag, AiSettingsArg, AiStopTag, + ai_action_message, ai_address_is_valid, ai_host_of, ai_providers, +}; pub use cancel_token::CancelToken; pub use catalog::{ColumnEntry, ObjectEntry, ObjectKindTag, ObjectScopeArg, SchemaEntry, TableName}; pub use catalog_change::{ @@ -77,8 +83,8 @@ pub use import::{ }; pub use key_read::{KeyKindTag, KeyReader, KeyTtlTag, KeyWindowInfo}; pub use library::{ - ConnectionLibrary, ConnectionRecord, ConnectionSecrets, GroupRecord, ImportSummary, LibrarySnapshot, SafeModeTag, - SshAuthTag, SshSettings, TagRecord, TlsModeTag, TlsSettings, check_jump_hosts, + AiPolicyTag, ConnectionLibrary, ConnectionRecord, ConnectionSecrets, GroupRecord, ImportSummary, LibrarySnapshot, + SafeModeTag, SshAuthTag, SshSettings, TagRecord, TlsModeTag, TlsSettings, check_jump_hosts, }; pub use masking::{ MaskReason, MaskStyleTag, MaskSuggestionInfo, PersonalDataHitInfo, SensitiveKindTag, ShareColumnChoice, diff --git a/core/crates/ffi/src/library.rs b/core/crates/ffi/src/library.rs index 4068787aba..d5635676d4 100644 --- a/core/crates/ffi/src/library.rs +++ b/core/crates/ffi/src/library.rs @@ -5,7 +5,7 @@ use chrono::{DateTime, Utc}; use secrecy::SecretString; use tablepro_core::credentials::{SecretKind, SecretLookup, SecretVault}; use tablepro_core::{ - AuthMode, ClientIdentity, ConfigError, DriverError, NetworkOptions, SafeModeLevel, TlsConfig, TlsMode, + AiPolicy, AuthMode, ClientIdentity, ConfigError, DriverError, NetworkOptions, SafeModeLevel, TlsConfig, TlsMode, }; use tablepro_session::runtime::Tasks; use tablepro_storage::{ @@ -68,6 +68,34 @@ impl From for SafeModeLevel { } } +// What the AI assistant may send from the connection (core AiPolicy). +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum AiPolicyTag { + Ask, + Allow, + Never, +} + +impl From for AiPolicy { + fn from(tag: AiPolicyTag) -> Self { + match tag { + AiPolicyTag::Ask => Self::Ask, + AiPolicyTag::Allow => Self::Allow, + AiPolicyTag::Never => Self::Never, + } + } +} + +impl From for AiPolicyTag { + fn from(policy: AiPolicy) -> Self { + match policy { + AiPolicy::Ask => Self::Ask, + AiPolicy::Allow => Self::Allow, + AiPolicy::Never => Self::Never, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] pub struct TlsSettings { pub mode: TlsModeTag, @@ -116,6 +144,9 @@ pub struct ConnectionRecord { pub last_opened_ms: Option, // Read only: a save keeps the stored tags, which change through tag_connections. pub tags: Vec, + // Always set on a record read back; None on a save keeps the stored policy, Ask for a new connection. + #[uniffi(default = None)] + pub ai_policy: Option, } // Each secret as the form holds it: None keeps what is stored, an empty string is stored as one. @@ -867,6 +898,9 @@ pub(crate) fn apply( saved.query_timeout_secs = record.query_timeout_secs; saved.color = record.color.as_deref().and_then(ConnectionColor::from_id); saved.group_id = group_id; + if let Some(policy) = record.ai_policy { + saved.ai_policy = policy.into(); + } } fn path(text: Option<&String>) -> Option { @@ -985,6 +1019,7 @@ impl From<&SavedConnection> for ConnectionRecord { group_id: saved.group_id.map(|id| id.to_string()), last_opened_ms: saved.last_opened_at.map(|at: DateTime| at.timestamp_millis()), tags: saved.tags.iter().map(Uuid::to_string).collect(), + ai_policy: Some(saved.ai_policy.into()), } } } diff --git a/core/crates/ffi/tests/ai.rs b/core/crates/ffi/tests/ai.rs new file mode 100644 index 0000000000..6237953974 --- /dev/null +++ b/core/crates/ffi/tests/ai.rs @@ -0,0 +1,655 @@ +#![cfg(feature = "engines")] +#![expect( + clippy::unwrap_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::collections::HashMap; +use std::io::{BufRead, BufReader, Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::path::Path; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use secrecy::{ExposeSecret, SecretString}; +use tablepro_ffi::{ + AiChatArg, AiConversationArg, AiMessageArg, AiPolicyTag, AiProviderTag, AiReply, AiReplyEvent, AiReplyListener, + AiRoleTag, AiSettingsArg, AiStopTag, ConnectionRecord, ConnectionSecrets, Core, CoreError, DatabaseSession, + FileOpenRequest, HostConfig, HostSecretError, HostSecrets, ObjectScopeArg, RunListener, RunOutcome, RunSettings, + SafeModeTag, SecretItemTag, SecretKindTag, TlsModeTag, TlsSettings, +}; + +#[derive(Clone, Default)] +struct MemorySecrets { + items: Arc>>, +} + +impl HostSecrets for MemorySecrets { + fn load(&self, _: String, _: SecretKindTag) -> Result, HostSecretError> { + Ok(None) + } + + fn store(&self, _: String, _: SecretKindTag, _: SecretString, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn relabel(&self, _: String, _: SecretKindTag, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete(&self, _: String, _: SecretKindTag) -> Result<(), HostSecretError> { + Ok(()) + } + + fn delete_connection(&self, _: String) -> Result<(), HostSecretError> { + Ok(()) + } + + fn load_item(&self, item: SecretItemTag) -> Result, HostSecretError> { + Ok(self.items.lock().unwrap().get(&item).cloned().map(SecretString::from)) + } + + fn store_item(&self, item: SecretItemTag, secret: SecretString) -> Result<(), HostSecretError> { + self.items + .lock() + .unwrap() + .insert(item, secret.expose_secret().to_owned()); + Ok(()) + } + + fn delete_item(&self, item: SecretItemTag) -> Result<(), HostSecretError> { + self.items.lock().unwrap().remove(&item); + Ok(()) + } +} + +struct Quiet; + +impl RunListener for Quiet { + fn events_pending(&self) {} +} + +impl AiReplyListener for Quiet { + fn events_pending(&self) {} +} + +const SHOP: &str = "CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL); \ + CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers (id), total REAL); \ + CREATE TABLE audit (id INTEGER PRIMARY KEY, note TEXT); \ + INSERT INTO customers (id, name) VALUES (1, 'Ada Lovelace')"; + +fn core(dir: &Path, secrets: MemorySecrets) -> Arc { + Core::new( + HostConfig { + client_version: "test".to_owned(), + storage_dir: Some(dir.join("storage").display().to_string()), + }, + Box::new(secrets), + ) + .unwrap() +} + +async fn shop(core: &Core, path: &Path) -> Arc { + let session = core + .open_file( + FileOpenRequest { + path: path.display().to_string(), + driver_id: None, + read_only: false, + create: !path.exists(), + }, + None, + ) + .await + .unwrap(); + let editor = session.open_editor(None, None).await.unwrap(); + let run = editor.run(SHOP.to_owned(), RunSettings::default(), Box::new(Quiet)); + assert!(matches!(run.outcome().await.unwrap(), RunOutcome::Completed { .. })); + editor.close().await.unwrap(); + // The catalog does not see tables an editor made until it reads again, as the tree's refresh does. + session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap(); + session +} + +fn record(path: &Path, ai_policy: Option) -> ConnectionRecord { + ConnectionRecord { + id: String::new(), + name: "shop".to_owned(), + driver_id: "sqlite".to_owned(), + host: String::new(), + port: 0, + database: path.display().to_string(), + username: String::new(), + integrated_auth: false, + tls: TlsSettings { + mode: TlsModeTag::Disable, + ca_file: None, + client_certificate: None, + client_key: None, + server_name: None, + }, + ssh: None, + safe_mode: SafeModeTag::Off, + query_timeout_secs: None, + color: None, + group_id: None, + last_opened_ms: None, + tags: Vec::new(), + ai_policy, + } +} + +async fn connect(core: &Core, id: String) -> Arc { + core.connect_saved( + id, + None, + ConnectionSecrets::default(), + ConnectionSecrets::default(), + None, + None, + ) + .await + .unwrap() +} + +fn compatible(address: &str) -> AiSettingsArg { + AiSettingsArg { + provider: AiProviderTag::OpenAiCompatible, + model: "test-model".to_owned(), + address: Some(address.to_owned()), + } +} + +fn question(text: &str) -> AiChatArg { + AiChatArg { + system: "system".to_owned(), + messages: vec![AiMessageArg { + role: AiRoleTag::User, + text: text.to_owned(), + }], + } +} + +// Every event until the one that ends the reply. +async fn read_to_end(reply: &AiReply) -> Vec { + let deadline = Instant::now() + Duration::from_secs(20); + let mut events = Vec::new(); + loop { + events.extend(reply.drain_events()); + if events + .last() + .is_some_and(|event| !matches!(event, AiReplyEvent::Text { .. })) + { + return events; + } + assert!(Instant::now() < deadline, "the reply never ended: {events:?}"); + tokio::time::sleep(Duration::from_millis(10)).await; + } +} + +fn text_of(events: &[AiReplyEvent]) -> String { + events + .iter() + .filter_map(|event| match event { + AiReplyEvent::Text { part } => Some(part.as_str()), + _ => None, + }) + .collect() +} + +#[derive(Debug, Clone)] +struct Seen { + request_line: String, + headers: HashMap, + body: String, +} + +#[derive(Clone)] +enum Answer { + Ok { content_type: &'static str, body: String }, + Status(u16, String), + // The headers of a stream, then nothing until the client goes. + Stalls, +} + +struct Server { + address: String, + seen: Arc>>, +} + +impl Server { + // A thread per request, so one that stalls holds only its own connection. + fn start(answer: Answer) -> Self { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = format!("http://{}/v1", listener.local_addr().unwrap()); + let seen = Arc::new(Mutex::new(Vec::new())); + let recorded = seen.clone(); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let answer = answer.clone(); + let recorded = recorded.clone(); + std::thread::spawn(move || serve(stream, &answer, &recorded)); + } + }); + Self { address, seen } + } + + fn seen(&self) -> Vec { + self.seen.lock().unwrap().clone() + } +} + +fn serve(mut stream: TcpStream, answer: &Answer, recorded: &Mutex>) { + let request = read_request(&stream); + recorded.lock().unwrap().push(request); + let response = match answer { + Answer::Ok { content_type, body } => respond(200, content_type, body), + Answer::Status(status, body) => respond(*status, "application/json", body), + Answer::Stalls => { + let _ = stream.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n"); + std::thread::sleep(Duration::from_secs(60)); + return; + } + }; + let _ = stream.write_all(response.as_bytes()); +} + +fn respond(status: u16, content_type: &str, body: &str) -> String { + format!( + "HTTP/1.1 {status} X\r\ncontent-type: {content_type}\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}", + body.len() + ) +} + +fn read_request(stream: &TcpStream) -> Seen { + let mut reader = BufReader::new(stream.try_clone().unwrap()); + let mut request_line = String::new(); + reader.read_line(&mut request_line).unwrap(); + let mut headers = HashMap::new(); + loop { + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + if line.trim().is_empty() { + break; + } + if let Some((name, value)) = line.split_once(':') { + headers.insert(name.trim().to_ascii_lowercase(), value.trim().to_owned()); + } + } + let length: usize = headers + .get("content-length") + .and_then(|length| length.parse().ok()) + .unwrap_or(0); + let mut body = vec![0; length]; + reader.read_exact(&mut body).unwrap(); + Seen { + request_line: request_line.trim_end().to_owned(), + headers, + body: String::from_utf8_lossy(&body).into_owned(), + } +} + +fn chunks() -> String { + [ + r#"{"choices":[{"delta":{"content":"SELECT "}}]}"#, + r#"{"choices":[{"delta":{"content":"1;"},"finish_reason":"stop"}]}"#, + r#"{"choices":[],"usage":{"prompt_tokens":12,"completion_tokens":3}}"#, + "[DONE]", + ] + .iter() + .map(|data| format!("data: {data}\n\n")) + .collect() +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_saved_connection_keeps_the_ai_policy_it_was_given() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("shop.sqlite"); + let core = core(dir.path(), MemorySecrets::default()); + let library = core.library().unwrap(); + + let new = library + .save(record(&path, None), ConnectionSecrets::default()) + .await + .unwrap(); + let never = library + .save( + ConnectionRecord { + ai_policy: Some(AiPolicyTag::Never), + ..new.clone() + }, + ConnectionSecrets::default(), + ) + .await + .unwrap(); + let kept = library + .save( + ConnectionRecord { + ai_policy: None, + name: "shop (prod)".to_owned(), + ..never.clone() + }, + ConnectionSecrets::default(), + ) + .await + .unwrap(); + + assert_eq!(new.ai_policy, Some(AiPolicyTag::Ask)); + assert_eq!(never.ai_policy, Some(AiPolicyTag::Never)); + assert_eq!(kept.ai_policy, Some(AiPolicyTag::Never)); +} + +#[tokio::test(flavor = "multi_thread")] +async fn under_never_nothing_from_the_connection_reaches_the_provider() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("shop.sqlite"); + let core = core(dir.path(), MemorySecrets::default()); + shop(&core, &path).await.close().await.unwrap(); + let saved = core + .library() + .unwrap() + .save(record(&path, Some(AiPolicyTag::Never)), ConnectionSecrets::default()) + .await + .unwrap(); + let session = connect(&core, saved.id).await; + let server = Server::start(Answer::Ok { + content_type: "text/event-stream", + body: chunks(), + }); + let assistant = core.ai_assistant(); + + let started = assistant + .start_conversation( + session.clone(), + AiConversationArg { + engine: "SQLite".to_owned(), + asked: "orders".to_owned(), + editor: None, + database: None, + }, + None, + ) + .await; + let reply = assistant.reply( + session.clone(), + compatible(&server.address), + question("orders?"), + Box::new(Quiet), + ); + let events = read_to_end(&reply).await; + + assert!( + matches!(&started, Err(CoreError::Failed { info }) if info.code == "ai.blocked"), + "{started:?}" + ); + assert!( + matches!(events.as_slice(), [AiReplyEvent::Failed { error }] if error.code == "ai.blocked"), + "{events:?}" + ); + assert!(server.seen().is_empty()); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_conversation_carries_the_structure_of_the_tables_its_question_names() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("shop.sqlite"); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &path).await; + + let started = core + .ai_assistant() + .start_conversation( + session.clone(), + AiConversationArg { + engine: "SQLite".to_owned(), + asked: "Which customers have no orders?".to_owned(), + editor: Some("SELECT * FROM audit;".to_owned()), + database: None, + }, + None, + ) + .await + .unwrap(); + + assert_eq!(started.described, ["audit", "customers", "orders"]); + let system = started.system; + assert!(system.contains("connected to a SQLite database"), "{system}"); + assert!( + system.contains("\"orders\" (\"id\" INTEGER primary key, \"customer_id\" INTEGER, \"total\" REAL)"), + "{system}" + ); + assert!(system.contains("```sql\nSELECT * FROM audit;\n```"), "{system}"); + assert!(!system.contains("Ada Lovelace"), "a row reached the prompt: {system}"); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_reply_streams_with_the_key_from_the_store_and_ends_with_its_usage() { + let dir = tempfile::tempdir().unwrap(); + let secrets = MemorySecrets::default(); + let core = core(dir.path(), secrets.clone()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + let server = Server::start(Answer::Ok { + content_type: "text/event-stream", + body: chunks(), + }); + let assistant = core.ai_assistant(); + + assistant + .set_key(AiProviderTag::OpenAiCompatible, SecretString::from(" sk-local \n")) + .await + .unwrap(); + let has_key = assistant.has_key(AiProviderTag::OpenAiCompatible).await.unwrap(); + let reply = assistant.reply( + session.clone(), + compatible(&server.address), + question("one row?"), + Box::new(Quiet), + ); + let events = read_to_end(&reply).await; + assistant + .set_key(AiProviderTag::OpenAiCompatible, SecretString::from(" ")) + .await + .unwrap(); + let removed = !assistant.has_key(AiProviderTag::OpenAiCompatible).await.unwrap(); + + assert!(has_key); + assert!(removed); + assert!(secrets.items.lock().unwrap().is_empty()); + assert_eq!(text_of(&events), "SELECT 1;"); + assert_eq!( + events.last(), + Some(&AiReplyEvent::Finished { + stop: AiStopTag::Complete, + input_tokens: Some(12), + output_tokens: Some(3), + }) + ); + let seen = server.seen(); + let request = seen.first().expect("the request"); + assert_eq!(request.request_line, "POST /v1/chat/completions HTTP/1.1"); + assert_eq!( + request.headers.get("authorization").map(String::as_str), + Some("Bearer sk-local") + ); + assert!(request.body.contains("\"model\":\"test-model\""), "{}", request.body); + assert!(request.body.contains("one row?"), "{}", request.body); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_refused_key_names_the_host_and_keeps_the_providers_words() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + let server = Server::start(Answer::Status( + 401, + r#"{"error":{"message":"invalid api key"}}"#.to_owned(), + )); + + let reply = core.ai_assistant().reply( + session.clone(), + compatible(&server.address), + question("hello"), + Box::new(Quiet), + ); + let events = read_to_end(&reply).await; + + let [AiReplyEvent::Failed { error }] = events.as_slice() else { + panic!("{events:?}"); + }; + assert_eq!(error.code, "ai.unauthorized"); + assert_eq!(error.ai.as_ref().and_then(|ai| ai.host.as_deref()), Some("127.0.0.1")); + assert_eq!( + error.server.as_ref().map(|server| server.message.as_str()), + Some("invalid api key") + ); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_hosted_provider_with_no_key_is_refused_before_any_request() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + + let reply = core.ai_assistant().reply( + session.clone(), + AiSettingsArg { + provider: AiProviderTag::Anthropic, + model: "claude-opus-5-5".to_owned(), + address: None, + }, + question("hello"), + Box::new(Quiet), + ); + let events = read_to_end(&reply).await; + + let [AiReplyEvent::Failed { error }] = events.as_slice() else { + panic!("{events:?}"); + }; + assert_eq!(error.code, "ai.missing_key"); + assert_eq!( + error.ai.as_ref().and_then(|ai| ai.provider), + Some(AiProviderTag::Anthropic) + ); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_cancelled_reply_ends_as_stopped() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + let server = Server::start(Answer::Stalls); + + let reply = core.ai_assistant().reply( + session.clone(), + compatible(&server.address), + question("hello"), + Box::new(Quiet), + ); + let deadline = Instant::now() + Duration::from_secs(20); + while server.seen().is_empty() { + assert!(Instant::now() < deadline, "the request never arrived"); + tokio::time::sleep(Duration::from_millis(10)).await; + } + reply.cancel(); + let events = read_to_end(&reply).await; + + assert_eq!(events, [AiReplyEvent::Stopped]); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_models_listed_come_back_sorted() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let server = Server::start(Answer::Ok { + content_type: "application/json", + body: r#"{"object":"list","data":[{"id":"qwen3"},{"id":"llama3.2:latest"}]}"#.to_owned(), + }); + + let models = core + .ai_assistant() + .list_models(compatible(&server.address), None) + .await + .unwrap(); + + assert_eq!(models, ["llama3.2:latest", "qwen3"]); + assert_eq!(server.seen()[0].request_line, "GET /v1/models HTTP/1.1"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_key_the_server_echoes_never_reaches_the_app() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + let server = Server::start(Answer::Status( + 401, + r#"{"error":{"message":"the key sk-echoed-secret is not valid"}}"#.to_owned(), + )); + let assistant = core.ai_assistant(); + assistant + .set_key(AiProviderTag::OpenAiCompatible, SecretString::from("sk-echoed-secret")) + .await + .unwrap(); + + let events = read_to_end(&assistant.reply( + session.clone(), + compatible(&server.address), + question("hello"), + Box::new(Quiet), + )) + .await; + let listed = assistant.list_models(compatible(&server.address), None).await; + + let [AiReplyEvent::Failed { error }] = events.as_slice() else { + panic!("{events:?}"); + }; + assert_eq!( + error.server.as_ref().map(|server| server.message.as_str()), + Some("the key [redacted] is not valid") + ); + assert!(!format!("{error:?}").contains("sk-echoed-secret")); + assert!(!format!("{listed:?}").contains("sk-echoed-secret"), "{listed:?}"); + session.close().await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_reply_stops_with_its_session_and_a_closed_one_sends_nothing() { + let dir = tempfile::tempdir().unwrap(); + let core = core(dir.path(), MemorySecrets::default()); + let session = shop(&core, &dir.path().join("shop.sqlite")).await; + let server = Server::start(Answer::Stalls); + let assistant = core.ai_assistant(); + + let reply = assistant.reply( + session.clone(), + compatible(&server.address), + question("hello"), + Box::new(Quiet), + ); + let deadline = Instant::now() + Duration::from_secs(20); + while server.seen().is_empty() { + assert!(Instant::now() < deadline, "the request never arrived"); + tokio::time::sleep(Duration::from_millis(10)).await; + } + session.close().await.unwrap(); + let during = read_to_end(&reply).await; + let after = read_to_end(&assistant.reply( + session.clone(), + compatible(&server.address), + question("again"), + Box::new(Quiet), + )) + .await; + + assert_eq!(during, [AiReplyEvent::Stopped]); + assert_eq!(after, [AiReplyEvent::Stopped]); + assert_eq!(server.seen().len(), 1); +} diff --git a/core/crates/ffi/tests/cloud_library.rs b/core/crates/ffi/tests/cloud_library.rs index 83a58ea935..7471d19fee 100644 --- a/core/crates/ffi/tests/cloud_library.rs +++ b/core/crates/ffi/tests/cloud_library.rs @@ -98,6 +98,7 @@ async fn saved(library: &ConnectionLibrary, name: &str) -> String { group_id: None, last_opened_ms: None, tags: Vec::new(), + ai_policy: None, }; library.save(record, ConnectionSecrets::default()).await.unwrap().id } diff --git a/core/crates/ffi/tests/csv_import.rs b/core/crates/ffi/tests/csv_import.rs index 4daf3858a8..64f2da8828 100644 --- a/core/crates/ffi/tests/csv_import.rs +++ b/core/crates/ffi/tests/csv_import.rs @@ -345,6 +345,7 @@ async fn confirm_writes_asks_first_and_read_only_refuses() { group_id: None, last_opened_ms: None, tags: Vec::new(), + ai_policy: None, }; let saved = library.save(record, ConnectionSecrets::default()).await.unwrap(); let session = core diff --git a/core/crates/ffi/tests/library.rs b/core/crates/ffi/tests/library.rs index f35eeb6237..ad25a1ea25 100644 --- a/core/crates/ffi/tests/library.rs +++ b/core/crates/ffi/tests/library.rs @@ -188,6 +188,7 @@ fn record(name: &str, driver_id: &str, database: &str) -> ConnectionRecord { group_id: None, last_opened_ms: None, tags: Vec::new(), + ai_policy: None, } } diff --git a/core/crates/ffi/tests/library_copies.rs b/core/crates/ffi/tests/library_copies.rs index e13975a856..4234c215f3 100644 --- a/core/crates/ffi/tests/library_copies.rs +++ b/core/crates/ffi/tests/library_copies.rs @@ -128,6 +128,7 @@ fn record(name: &str) -> ConnectionRecord { group_id: None, last_opened_ms: None, tags: Vec::new(), + ai_policy: None, } } diff --git a/core/crates/ffi/tests/row_edits.rs b/core/crates/ffi/tests/row_edits.rs index cd80914278..b72df35e2b 100644 --- a/core/crates/ffi/tests/row_edits.rs +++ b/core/crates/ffi/tests/row_edits.rs @@ -165,6 +165,7 @@ async fn saved(core: &Core, path: &Path, safe_mode: SafeModeTag) -> Arc Arc Show AI assistant opens a pane where the query history sits, one at a time. It shows Off with Open Settings, the connection's Never, or the conversation: each question in a card, "Sent with the structure of" the tables described, the reply streaming in as paragraphs with bold and inline code in a RichTextBlock and code blocks with Copy and Insert in editor, then its tokens, Stopped, or the error with Retry. Enter sends and Shift+Enter starts a line; Stop ends a reply and keeps what came; New conversation starts again. A reply is painted at most every 80 ms. +- The editor's menu and the Query menu have Review, Explain and Optimize with AI, on the selection or the statement at the caret, enabled while the assistant may be asked and the engine has SQL. A refused run's error bar has Fix with AI, which sends the statement with the error as the results pane words it. Each starts a new conversation. +- Under Ask each time the first request asks in a ContentDialog that names the connection and the provider and host the data goes to, with Cancel the default. The answer lasts for that provider and address until the connection disconnects: the pane is built per session. The policy and the settings are read again before each request, and a change of provider or address, or the policy moving to Never, asks again or stops a reply in flight. +- Insert in editor replaces the selection or inserts at the caret of the query tab in front as one undo step, or opens a new query tab. +- Narrator hears a reply's end and an error through a notification on the pane. +- Each `ai.*` code has its sentence with the host, status or provider it names, and the provider's own words after it. + +## Rationale + +- Stock WinUI controls: SettingsCard, ComboBox, PasswordBox, TextBox, RichTextBlock, ContentDialog, CommandBar and InfoBar's action button. WinUI has no Markdown control and the Toolkit's is still in Labs, so the reply is read into RichTextBlock paragraphs and code blocks by `ReplyMarkdown` in the app model. +- The pane where the history and saved queries sit keeps the editor and the grid at their width, as the Linux pane beside the tabs does. + +## Consequences + +- No inline suggestions while typing, no agent mode, no saved conversations, no `@` context and no AI rules per connection (roadmap A-06), and three providers (A-07). +- Tables named in SQL outside the editor's schema are not described until the completion scope moves into the core (C-08). +- A connection opened from a file without saving it has no policy of its own, so the assistant asks under Ask. diff --git a/windows/docs/decisions/README.md b/windows/docs/decisions/README.md index 1786efcae4..526fda77d1 100644 --- a/windows/docs/decisions/README.md +++ b/windows/docs/decisions/README.md @@ -21,3 +21,4 @@ The format, the statuses and how a later ADR changes an earlier one follow [the | [0011](0011-saved-queries-csv-import-and-connection-copies.md) | Saved queries, history pins, CSV import and connection copies | Accepted | Query > Save query... and a Saved queries pane with folders where the history pane sits, Insert in editor as macOS; Pin on a history run; Import CSV... on a table with a field per column under the save's Safe Mode gate; Duplicate a connection with its passwords and Export the connections file without them. | | [0012](0012-catalog-operations-table-filters-and-export-progress.md) | Catalog operations, filters kept per table, filter by a cell and export progress | Accepted | Truncate and Drop in the tree, New and Drop schema and database, each showing the core's statements under Safe Mode; a table's last filter comes back when it opens again; a cell's Filter menu; SQL INSERT of a query result and a running row count while an export writes. | | [0013](0013-row-details-column-layout-and-selection-summary.md) | Row details, table info, column layout, selection summary and connection URLs | Accepted | A Details pane with the focused row's fields edited in place and the table's size and comment; column order, hidden and frozen columns kept per table; Sum, Average and Count of the selected cells; New from URL and Ctrl+V on the Connections page; jump hosts in the SSH section; a group's colour. | +| [0014](0014-ai-assistant.md) | The AI assistant | Accepted | Settings picks the provider, address, model and a key kept in Credential Manager; a pane beside the tabs streams a chat with the connection's structure; Review, Explain, Optimize and Fix with AI; the AI policy in the connection form, with a question under Ask each time that lasts until the connection disconnects (core 0032). | diff --git a/windows/src/TablePro.App.Model/AppSettings.cs b/windows/src/TablePro.App.Model/AppSettings.cs index 3eced073ff..6e65e4630e 100644 --- a/windows/src/TablePro.App.Model/AppSettings.cs +++ b/windows/src/TablePro.App.Model/AppSettings.cs @@ -1,5 +1,6 @@ using System.Text.Json; using System.Text.Json.Serialization; +using TablePro.Interop; namespace TablePro.App.Model; @@ -47,6 +48,11 @@ public sealed record AppSettings // As macOS offers them; 0 keeps as many as the store holds. public static IReadOnlyList HistoryMaxEntriesChoices { get; } = [100, 500, 1_000, 5_000, 10_000, 0]; + // The Linux app's default, so a key is all a new user needs. + public const string DefaultAnthropicModel = "claude-opus-5-5"; + + public const string DefaultCompatibleAddress = "http://localhost:11434/v1"; + public AppTheme Theme { get; set; } = AppTheme.System; public bool ReopenSession { get; set; } = true; @@ -84,6 +90,41 @@ public sealed record AppSettings // Results with rows a run keeps, the newest; 0 keeps them all. public int ResultSetsKeptPerRun { get; set; } + // Who answers the AI assistant; null while it is off, which is how it starts, so nothing is sent anywhere. + public AiProvider? AiProvider { get; set; } + + public string AnthropicModel { get; set; } = DefaultAnthropicModel; + + public string OpenAIModel { get; set; } = string.Empty; + + public string CompatibleModel { get; set; } = string.Empty; + + // An Ollama or other OpenAI-compatible server, version path included; the hosted providers' are fixed. + public string CompatibleAddress { get; set; } = DefaultCompatibleAddress; + + // What the chosen provider's requests take, or null while the assistant is off. + [JsonIgnore] + public AiSettings? Assistant => AiProvider is { } provider ? AssistantFor(provider) : null; + + public AiSettings AssistantFor(AiProvider provider) => new( + provider, + ModelOf(provider), + provider == Interop.AiProvider.OpenAICompatible ? CompatibleAddress : null); + + public string ModelOf(AiProvider provider) => provider switch + { + Interop.AiProvider.Anthropic => AnthropicModel, + Interop.AiProvider.OpenAI => OpenAIModel, + _ => CompatibleModel, + }; + + public AppSettings WithModel(AiProvider provider, string model) => provider switch + { + Interop.AiProvider.Anthropic => this with { AnthropicModel = model }, + Interop.AiProvider.OpenAI => this with { OpenAIModel = model }, + _ => this with { CompatibleModel = model }, + }; + public AppSettings Normalized() => this with { Theme = Enum.IsDefined(Theme) ? Theme : AppTheme.System, @@ -101,6 +142,13 @@ public AppSettings Normalized() => this with QueryTimeoutSeconds = Math.Clamp(QueryTimeoutSeconds, 0, 86_400), CountRowsIfEstimateLessThan = Math.Max(0, CountRowsIfEstimateLessThan), ResultSetsKeptPerRun = Math.Max(0, ResultSetsKeptPerRun), + AiProvider = AiProvider is { } provider && Enum.IsDefined(provider) ? provider : null, + AnthropicModel = (AnthropicModel ?? string.Empty).Trim(), + OpenAIModel = (OpenAIModel ?? string.Empty).Trim(), + CompatibleModel = (CompatibleModel ?? string.Empty).Trim(), + CompatibleAddress = string.IsNullOrWhiteSpace(CompatibleAddress) + ? DefaultCompatibleAddress + : CompatibleAddress.Trim(), }; } diff --git a/windows/src/TablePro.App.Model/AssistantAccess.cs b/windows/src/TablePro.App.Model/AssistantAccess.cs new file mode 100644 index 0000000000..6b3f9bfd67 --- /dev/null +++ b/windows/src/TablePro.App.Model/AssistantAccess.cs @@ -0,0 +1,91 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +public enum AssistantAvailability +{ + // No provider: nothing is sent anywhere. + Off, + + // The connection's policy is Never. + Blocked, + Ready, +} + +// What one workspace's assistant may send: the provider Settings names and the connection's policy, both read again +// before every request, and the answer under Ask each time, which holds for the place it named until the workspace +// disconnects. +public sealed class AssistantAccess(Func settings, Func policy) +{ + private Destination? _granted; + private bool _asking; + + public AssistantAvailability Availability => + settings() is null ? AssistantAvailability.Off + : policy() == AiPolicy.Never ? AssistantAvailability.Blocked + : AssistantAvailability.Ready; + + // A request may go without asking: the policy allows it, or the user did for where requests go now. + public bool IsGranted => + Availability == AssistantAvailability.Ready + && Current() is { } current + && (policy() == AiPolicy.Allow || _granted == current); + + // The last check before a request goes: settings may have changed while it was made. + public bool MaySendTo(AiSettings sent) + { + ArgumentNullException.ThrowIfNull(sent); + return IsGranted && Current() == Destination.Of(sent); + } + + // Under Ask each time the first request asks, naming where the data goes; one question at a time. + public async Task PermitAsync(Func> ask) + { + ArgumentNullException.ThrowIfNull(ask); + if (Availability != AssistantAvailability.Ready || settings() is not { } askedFor) + { + return false; + } + + if (IsGranted) + { + return true; + } + + if (_asking) + { + return false; + } + + _asking = true; + bool allowed; + try + { + allowed = await ask(askedFor).ConfigureAwait(true); + } + finally + { + _asking = false; + } + + // The answer is for the place the question named; the policy or the provider may have changed meanwhile. + if (allowed && Availability == AssistantAvailability.Ready && Current() == Destination.Of(askedFor)) + { + _granted = Destination.Of(askedFor); + } + + return IsGranted; + } + + private Destination? Current() => settings() is { } now ? Destination.Of(now) : null; + + // The provider and the address its requests go to; the model is not where the data goes. + private sealed record Destination(AiProvider Provider, string Address) + { + public static Destination Of(AiSettings settings) => new( + settings.Provider, + settings.Provider == AiProvider.OpenAICompatible + ? (settings.Address ?? string.Empty).Trim().TrimEnd('/') + : string.Empty); + } +} diff --git a/windows/src/TablePro.App.Model/AssistantConversation.cs b/windows/src/TablePro.App.Model/AssistantConversation.cs new file mode 100644 index 0000000000..b1a8f9f1bb --- /dev/null +++ b/windows/src/TablePro.App.Model/AssistantConversation.cs @@ -0,0 +1,72 @@ +using TablePro.Interop; + +namespace TablePro.App.Model; + +// What the user asked in one turn: a typed question, or an editor action on a statement. +public abstract record AssistantAsk +{ + private AssistantAsk() + { + } + + public sealed record Typed(string Text) : AssistantAsk; + + // Error is the server's, for Fix. + public sealed record Action(AiAction Kind, string Statement, string? Error = null) : AssistantAsk; +} + +// Message is what the provider receives; Answer stays null until a reply ends, and a failed one never gets one. +public sealed record AssistantTurn(AssistantAsk Asked, string Message) +{ + public string? Answer { get; set; } +} + +// The turns of one conversation, sent whole with each question. The system prompt is set when it starts. +public sealed class AssistantConversation(string system, IReadOnlyList described) +{ + private readonly List _turns = []; + + // The tables whose structure the system prompt carries. + public IReadOnlyList Described { get; } = described; + + public IReadOnlyList Turns => _turns; + + public AssistantTurn Ask(AssistantAsk asked, string message) + { + var turn = new AssistantTurn(asked, message); + _turns.Add(turn); + return turn; + } + + // The last question's reply, or what came of it before Stop. + public void Answer(string text) + { + if (_turns.Count > 0) + { + _turns[^1].Answer = text; + } + } + + // The request for the last question. A turn with no answer is left out, so questions and answers alternate. + public AiChat Request() + { + var messages = new List(); + for (var index = 0; index < _turns.Count; index++) + { + var turn = _turns[index]; + if (index == _turns.Count - 1) + { + messages.Add(new AiMessage(AiRole.User, turn.Message)); + break; + } + + if (!string.IsNullOrWhiteSpace(turn.Answer)) + { + messages.Add(new AiMessage(AiRole.User, turn.Message)); + messages.Add(new AiMessage(AiRole.Assistant, turn.Answer)); + } + } + + return new AiChat(system, messages); + } +} diff --git a/windows/src/TablePro.App.Model/ConnectionDraft.cs b/windows/src/TablePro.App.Model/ConnectionDraft.cs index dd88c7906a..559f0b56f9 100644 --- a/windows/src/TablePro.App.Model/ConnectionDraft.cs +++ b/windows/src/TablePro.App.Model/ConnectionDraft.cs @@ -34,6 +34,9 @@ public sealed class ConnectionDraft { private SshSettings? _savedSsh; + // The record's own, which a save leaves as it was unless the form changed it. + private AiPolicy? _savedAiPolicy; + public Guid? Id { get; set; } public string EngineId { get; set; } = string.Empty; @@ -91,6 +94,8 @@ public sealed class ConnectionDraft public SafeMode SafeMode { get; set; } = SafeMode.Off; + // What the AI assistant may send from the connection; a new one starts at Ask, as the core saves it. + public AiPolicy AiPolicy { get; set; } = AiPolicy.Ask; public string Timeout { get; set; } = string.Empty; public string? Color { get; set; } @@ -168,6 +173,8 @@ public static ConnectionDraft From(SavedConnection connection, DatabaseEngine en ClientCertificate = connection.Tls.ClientCertificate ?? string.Empty, ClientKey = connection.Tls.ClientKey ?? string.Empty, SafeMode = connection.SafeMode, + AiPolicy = connection.AiPolicy ?? AiPolicy.Ask, + _savedAiPolicy = connection.AiPolicy, Timeout = connection.QueryTimeoutSeconds?.ToString(CultureInfo.InvariantCulture) ?? string.Empty, Color = connection.Color, GroupId = connection.GroupId, @@ -294,7 +301,10 @@ public SavedConnection ToConnection() { return new SavedConnection( Id, name, EngineId, string.Empty, 0, Database.Trim(), string.Empty, TlsSettings.Disabled, - SafeMode, Color: Color, GroupId: GroupId, LastOpened: LastOpened); + SafeMode, Color: Color, GroupId: GroupId, LastOpened: LastOpened) + { + AiPolicy = ChosenAiPolicy(), + }; } return new SavedConnection( @@ -329,10 +339,15 @@ public SavedConnection ToConnection() : int.Parse(Timeout.Trim(), NumberStyles.None, CultureInfo.InvariantCulture), Color: Color, GroupId: GroupId, - LastOpened: LastOpened); + LastOpened: LastOpened) + { + AiPolicy = ChosenAiPolicy(), + }; } - // A rename, a colour or a group is nothing a connect reads, so a connect under way goes on without them. + private AiPolicy? ChosenAiPolicy() => AiPolicy == (_savedAiPolicy ?? AiPolicy.Ask) ? _savedAiPolicy : AiPolicy; + + // A rename, a colour, a group or the AI policy is nothing a connect reads, so a connect under way goes on. public static bool ChangesConnect(SavedConnection before, SavedConnection after) { ArgumentNullException.ThrowIfNull(before); @@ -348,6 +363,7 @@ public static bool ChangesConnect(SavedConnection before, SavedConnection after) GroupId = before.GroupId, LastOpened = before.LastOpened, Tags = before.Tags, + AiPolicy = before.AiPolicy, Ssh = ssh, } != before; } diff --git a/windows/src/TablePro.App.Model/QueryTab.cs b/windows/src/TablePro.App.Model/QueryTab.cs index 4f744e7d6b..f8a432d9bf 100644 --- a/windows/src/TablePro.App.Model/QueryTab.cs +++ b/windows/src/TablePro.App.Model/QueryTab.cs @@ -80,6 +80,9 @@ public interface IQueryFileView Task ShowReloadFailedAsync(string path, Exception failure); } +// A typed run the server refused: its SQL and the words the results pane showed, which Fix with AI sends. +public sealed record RunFailure(string Statement, string Error); + // The switcher's lists, user databases before the engine's own. public sealed record DatabaseChoices( IReadOnlyList UserDatabases, @@ -200,6 +203,43 @@ public QueryTab( // The typed SQL whose results the tab shows, which a result share sends; null after Commit or Roll back. public string? LastStatement => _statement; + // The last typed run, when the server refused it; null once another run starts. + public RunFailure? LastFailure { get; private set; } + + // What Explain, Review and Optimize with AI send: the selection, else the statement the caret is in, as Run + // statement takes it. Null when there is none. + public string? AssistantStatement() + { + var selected = _view.SelectedText; + if (!string.IsNullOrWhiteSpace(selected)) + { + return selected.Trim(); + } + + var text = _view.DocumentText; + if (_session.StatementAt(text, _view.Caret) is not { } statement) + { + return null; + } + + // The range stops before its terminator, which the assistant sees as the user wrote it. + var (start, length) = statement.GetOffsetAndLength(text.Length); + var end = start + length; + var after = end; + while (after < text.Length && text[after] is ' ' or '\t') + { + after++; + } + + if (after < text.Length && text[after] == ';') + { + end = after + 1; + } + + var found = text[start..end].Trim(); + return found.Length > 0 ? found : null; + } + // A scratch tab's text is kept in the session, so only a file tab's unsaved text is asked about. public async Task ConfirmCloseAsync(bool offerSave) { @@ -621,6 +661,11 @@ private async Task RunOnEditorAsync(string sql, bool record, CancellationToken c } _run?.Dispose(); + if (record) + { + LastFailure = null; + } + var startedAt = DateTimeOffset.Now; var started = Stopwatch.GetTimestamp(); var settings = _settings(); @@ -632,6 +677,9 @@ await _results.ShowRunAsync(_run, _sort, int.MaxValue, settings.ResultSetsKeptPe if (record) { ran = Ran(sql, startedAt, Stopwatch.GetElapsedTime(started)); + LastFailure = _results.Outcome is QueryOutcome.Failed failed + ? new RunFailure(sql, _view.WordError(failed.Error)) + : null; } await AfterRunAsync().ConfigureAwait(true); diff --git a/windows/src/TablePro.App.Model/ReplyMarkdown.cs b/windows/src/TablePro.App.Model/ReplyMarkdown.cs new file mode 100644 index 0000000000..8539e605c2 --- /dev/null +++ b/windows/src/TablePro.App.Model/ReplyMarkdown.cs @@ -0,0 +1,185 @@ +using System.Text; + +namespace TablePro.App.Model; + +public enum ReplySpanStyle +{ + Plain, + Bold, + Code, +} + +public sealed record ReplySpan(string Text, ReplySpanStyle Style); + +public abstract record ReplyBlock +{ + private ReplyBlock() + { + } + + // Lines in one paragraph keep their breaks, since a list is written a line per item. + public sealed record Paragraph(IReadOnlyList Spans, bool IsHeading = false) : ReplyBlock; + + // Language is the fence's info string, such as sql; empty for none. + public sealed record Code(string Language, string Text) : ReplyBlock; +} + +// The Markdown a model writes, read into what a RichTextBlock shows: paragraphs with bold and inline code, headings, +// and fenced code blocks, which get Copy and Insert. A fence still open, as a reply streams in, runs to the end. +public static class ReplyMarkdown +{ + public static IReadOnlyList Read(string markdown) + { + ArgumentNullException.ThrowIfNull(markdown); + var blocks = new List(); + var prose = new List(); + var lines = markdown.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n'); + for (var index = 0; index < lines.Length; index++) + { + var line = lines[index]; + if (Fence(line) is { } open) + { + Flush(prose, blocks); + var code = new List(); + index++; + while (index < lines.Length && !Closes(lines[index], open)) + { + code.Add(lines[index]); + index++; + } + + blocks.Add(new ReplyBlock.Code(open.Language, string.Join('\n', code))); + continue; + } + + if (string.IsNullOrWhiteSpace(line)) + { + Flush(prose, blocks); + continue; + } + + if (Heading(line) is { } heading) + { + Flush(prose, blocks); + blocks.Add(new ReplyBlock.Paragraph(Spans(heading), IsHeading: true)); + continue; + } + + prose.Add(Bullet(line)); + } + + Flush(prose, blocks); + return blocks; + } + + // Bold and inline code; a marker with no partner is text. + public static IReadOnlyList Spans(string text) + { + ArgumentNullException.ThrowIfNull(text); + var spans = new List(); + var plain = new StringBuilder(); + var index = 0; + while (index < text.Length) + { + if (text[index] == '`' && text.IndexOf('`', index + 1) is var end and > 0) + { + Add(spans, plain, ReplySpanStyle.Plain); + plain.Append(text, index + 1, end - index - 1); + Add(spans, plain, ReplySpanStyle.Code); + index = end + 1; + continue; + } + + if (text.AsSpan(index).StartsWith("**", StringComparison.Ordinal) + && text.IndexOf("**", index + 2, StringComparison.Ordinal) is var close and > 0 + && close > index + 2) + { + Add(spans, plain, ReplySpanStyle.Plain); + plain.Append(text, index + 2, close - index - 2); + Add(spans, plain, ReplySpanStyle.Bold); + index = close + 2; + continue; + } + + plain.Append(text[index]); + index++; + } + + Add(spans, plain, ReplySpanStyle.Plain); + return spans; + } + + private static void Add(List spans, StringBuilder text, ReplySpanStyle style) + { + if (text.Length > 0) + { + spans.Add(new ReplySpan(text.ToString(), style)); + text.Clear(); + } + } + + private static void Flush(List prose, List blocks) + { + if (prose.Count > 0) + { + blocks.Add(new ReplyBlock.Paragraph(Spans(string.Join('\n', prose)))); + prose.Clear(); + } + } + + private sealed record Opening(char Mark, int Length, string Language); + + // Up to three spaces before it, as CommonMark allows. + private static Opening? Fence(string line) + { + var trimmed = line.TrimStart(' '); + if (line.Length - trimmed.Length > 3 || trimmed.Length < 3 || trimmed[0] is not ('`' or '~')) + { + return null; + } + + var mark = trimmed[0]; + var length = 0; + while (length < trimmed.Length && trimmed[length] == mark) + { + length++; + } + + var info = trimmed[length..].Trim(); + if (length < 3 || (mark == '`' && info.Contains('`', StringComparison.Ordinal))) + { + return null; + } + + return new Opening(mark, length, info.Split(' ', 2)[0]); + } + + private static bool Closes(string line, Opening open) + { + var trimmed = line.Trim(); + return trimmed.Length >= open.Length && trimmed.All(c => c == open.Mark); + } + + private static string? Heading(string line) + { + var trimmed = line.TrimStart(); + var level = 0; + while (level < trimmed.Length && trimmed[level] == '#') + { + level++; + } + + return level is >= 1 and <= 6 && level < trimmed.Length && trimmed[level] == ' ' + ? trimmed[(level + 1)..].Trim() + : null; + } + + private static string Bullet(string line) + { + var trimmed = line.TrimStart(); + var indent = line[..(line.Length - trimmed.Length)]; + return trimmed.Length > 1 && trimmed[0] is ('-' or '*' or '+') && trimmed[1] == ' ' + ? indent + "• " + trimmed[2..] + : line; + } +} diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml index ee63e087db..68a154d80e 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml @@ -221,6 +221,14 @@ + + + + + + + + diff --git a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs index 6ae05e8f00..3c101b057d 100644 --- a/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs +++ b/windows/src/TablePro.App/ConnectionForm/ConnectionFormWindow.xaml.cs @@ -216,6 +216,7 @@ private void ShowDetails(DatabaseEngine engine, ConnectionDraft draft) ClientCertificateBox.Text = draft.ClientCertificate; ClientKeyBox.Text = draft.ClientKey; SafeModeBox.SelectedIndex = (int)draft.SafeMode; + AiPolicyBox.SelectedIndex = (int)draft.AiPolicy; TimeoutBox.Text = draft.Timeout; ColorBox.SelectedIndex = Math.Max(0, Array.IndexOf(Colors, draft.Color ?? string.Empty)); NameBox.PlaceholderText = draft.SuggestedName(); @@ -260,6 +261,7 @@ private ConnectionDraft ReadDraft() } draft.SafeMode = (SafeMode)Math.Max(0, SafeModeBox.SelectedIndex); + draft.AiPolicy = (AiPolicy)Math.Max(0, AiPolicyBox.SelectedIndex); draft.Timeout = TimeoutBox.Text; draft.Color = ColorBox.SelectedItem is ComboBoxItem { Tag: string color } && color.Length > 0 ? color : null; draft.GroupId = GroupBox.SelectedItem is ComboBoxItem { Tag: Guid group } ? group : null; @@ -530,6 +532,15 @@ private void UpdateSummaries() AppearanceExpander.Description = Strings.Format("AppearanceSummary", color, group); } + // The card says what the chosen policy sends, as the Linux connection dialog does. + private void OnAiPolicyChanged(object sender, SelectionChangedEventArgs e) => + AiPolicyCard.Description = Strings.Get(AiPolicyBox.SelectedIndex switch + { + 1 => "AiPolicyAllowDescription", + 2 => "AiPolicyNeverDescription", + _ => "AiPolicyAskDescription", + }); + private void OnSummaryChanged(object sender, SelectionChangedEventArgs e) { if (_draft is not null) diff --git a/windows/src/TablePro.App/MainWindow.xaml b/windows/src/TablePro.App/MainWindow.xaml index c94d61cfb4..18eddcab10 100644 --- a/windows/src/TablePro.App/MainWindow.xaml +++ b/windows/src/TablePro.App/MainWindow.xaml @@ -158,6 +158,7 @@ + @@ -209,6 +210,10 @@ + + + + diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index 2b3cc94b8e..7ef34e80cf 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -205,6 +205,7 @@ public void Connect(SavedConnection connection) EngineId = connection.EngineId, }); open.Mark(connection.EngineId, connection.Color); + open.SetAiPolicy(connection.AiPolicy ?? AiPolicy.Ask); // The tags come from the library's snapshot, which names each one. _ = RefreshMarksAsync(); } @@ -639,6 +640,7 @@ private void OpenDatabase(OpenConnection from, string database) Database = database, }); open.Mark(from.Origin.EngineId, from.Color); + open.SetAiPolicy(from.AiPolicy); open.SetTags(from.Tags); ShowCue(open); } @@ -662,6 +664,7 @@ private async Task RefreshMarksAsync() if (snapshot.Connections.FirstOrDefault(saved => saved.Id == open.ConnectionId) is { } saved) { open.Mark(saved.EngineId, saved.Color); + open.SetAiPolicy(saved.AiPolicy ?? AiPolicy.Ask); open.SetTags([.. saved.Tags .Select(id => snapshot.Tags.FirstOrDefault(tag => tag.Id == id)) .OfType()]); @@ -1541,6 +1544,12 @@ private void UpdateCommands() MenuShowHistory.IsChecked = workspace?.HistoryVisible ?? false; MenuShowSavedQueries.IsEnabled = workspace?.CanShowSavedQueries ?? false; MenuShowSavedQueries.IsChecked = workspace?.SavedQueriesVisible ?? false; + MenuShowAssistant.IsEnabled = workspace?.CanShowAssistant ?? false; + MenuShowAssistant.IsChecked = workspace?.AssistantVisible ?? false; + var canAsk = workspace?.CanAskAssistant ?? false; + MenuAiReview.IsEnabled = canAsk; + MenuAiExplain.IsEnabled = canAsk; + MenuAiOptimize.IsEnabled = canAsk; MenuSaveQuery.IsEnabled = workspace?.CanSaveQuery ?? false; MenuExportResults.IsEnabled = workspace?.CanExport ?? false; MenuShareResult.IsEnabled = workspace?.CanShareResult ?? false; @@ -1727,6 +1736,18 @@ private async void OnToggleSavedQueries(object sender, RoutedEventArgs e) UpdateCommands(); } + private void OnToggleAssistant(object sender, RoutedEventArgs e) + { + Workspace?.ToggleAssistant(); + UpdateCommands(); + } + + private void OnAiReview(object sender, RoutedEventArgs e) => Workspace?.AskAssistant(AiAction.Review); + + private void OnAiExplain(object sender, RoutedEventArgs e) => Workspace?.AskAssistant(AiAction.Explain); + + private void OnAiOptimize(object sender, RoutedEventArgs e) => Workspace?.AskAssistant(AiAction.Optimize); + private async void OnSaveQuery(object sender, RoutedEventArgs e) { if (Workspace is { } workspace) diff --git a/windows/src/TablePro.App/Services/AssistantText.cs b/windows/src/TablePro.App/Services/AssistantText.cs new file mode 100644 index 0000000000..ee359da283 --- /dev/null +++ b/windows/src/TablePro.App/Services/AssistantText.cs @@ -0,0 +1,74 @@ +using TablePro.Interop; + +namespace TablePro.App.Services; + +// The assistant's words: providers, where requests go, and each ai.* code with its host, status and provider. +internal static class AssistantText +{ + public static string ProviderName(AiProvider provider) => Strings.Get(provider switch + { + AiProvider.Anthropic => "AiProviderAnthropic", + AiProvider.OpenAI => "AiProviderOpenAI", + _ => "AiProviderCompatible", + }); + + // The provider and the host its requests go to, as the question before the first request names them. + public static string Destination(AiSettings settings) + { + ArgumentNullException.ThrowIfNull(settings); + var host = AiAssistant.HostOf(settings); + return (settings.Provider, host) switch + { + (AiProvider.OpenAICompatible, { } named) => Strings.Format("AiDestinationServer", named), + (_, { } named) => Strings.Format("AiDestinationProvider", ProviderName(settings.Provider), named), + _ => ProviderName(settings.Provider), + }; + } + + public static string Action(AiAction action) => Strings.Get(action switch + { + AiAction.Explain => "AiExplain", + AiAction.Review => "AiReview", + AiAction.Optimize => "AiOptimize", + _ => "AiFix", + }); + + // A code with no sentence of its own here, a secret.* one included, is worded as every other error is. + public static string Error(CoreError error) + { + ArgumentNullException.ThrowIfNull(error); + var ai = error.Ai; + var host = ai?.Host ?? string.Empty; + var detail = ai?.Detail ?? string.Empty; + var sentence = error.Code switch + { + "ai.missing_key" => Strings.Format( + "AiError_missing_key", + ai?.Provider is { } provider ? ProviderName(provider) : string.Empty), + "ai.missing_model" => Strings.Get("AiError_missing_model"), + "ai.invalid_address" => Strings.Get("AiError_invalid_address"), + "ai.key_over_plain_http" => Strings.Get("AiError_key_over_plain_http"), + "ai.invalid_key" => Strings.Get("AiError_invalid_key"), + "ai.tls" => Strings.Format("AiError_tls", detail), + "ai.unreachable" => Strings.Format("AiError_unreachable", host, detail), + "ai.timed_out" => Strings.Format("AiError_timed_out", host), + "ai.unauthorized" => Strings.Format("AiError_unauthorized", host), + "ai.rate_limited" => Strings.Format("AiError_rate_limited", host), + "ai.status" => Strings.Format("AiError_status", host, ai?.Status ?? 0), + // The provider's own words are the sentence. + "ai.provider" => Strings.Format("AiError_provider", error.Server?.Message ?? string.Empty), + "ai.protocol" => Strings.Format("AiError_protocol", detail), + "ai.too_long" => Strings.Get("AiError_too_long"), + "ai.blocked" => Strings.Get("AiError_blocked"), + _ => null, + }; + if (sentence is null) + { + return Strings.Error(error); + } + + return error.Code != "ai.provider" && error.Server?.Message is { Length: > 0 } said + ? Strings.Format("AiErrorServerSaid", sentence, said) + : sentence; + } +} diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index 32411e9d93..b2509bc273 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -4458,4 +4458,310 @@ New fingerprint: {2} Copy {0} + + AI assistant + + + Provider + + + AI provider + + + Off + + + Anthropic + + + OpenAI + + + Ollama or compatible server + + + Nothing is sent anywhere until you choose a provider. + + + Your questions, your SQL and the structure of the tables involved go to this provider. Rows never do. Each connection's AI policy decides whether anything is sent from it. + + + Server address + + + Server address + + + http://localhost:11434/v1 + + + An http or https address with its version path. A key goes over plain http only to this computer. + + + Enter an http or https address with its version path, such as http://localhost:11434/v1. + + + API key + + + API key + + + API key (optional) + + + API key + + + Paste a key + + + Save key + + + Remove key + + + A key is kept in Windows Credential Manager. + + + No key is kept. Paste the provider's API key and save it. + + + No key is kept. A server on this computer usually needs none. + + + The key is saved in Windows Credential Manager. + + + The key is removed. + + + Model + + + The model id the provider answers with. Open the list to read the models it offers. + + + Model + + + Model id + + + Reading the provider's models... + + + The models could not be read. {0} + + + The provider lists no models. + + + AI assistant + + + The assistant asks before it first sends this connection's table structure, and your answer lasts until it disconnects. + + + AI assistant policy + + + Ask each time + + + Always allow + + + Never + + + The assistant asks before it first sends this connection's table structure, and your answer lasts until it disconnects. + + + The assistant sends this connection's table structure and your SQL to the provider without asking. It reads no rows. + + + Nothing from this connection is sent to the AI provider, and the assistant is off here. + + + AI assistant + + + AI assistant + + + New conversation + + + New conversation + + + Close AI assistant + + + Close AI assistant + + + Choose a provider in Settings to use the assistant. + + + Open Settings + + + This connection's AI policy is Never, so the assistant sends nothing from it. Edit the connection to change it. + + + Ask about this connection + + + Question for the assistant + + + Send + + + Send (Enter) + + + Stop + + + Stop the reply + + + Questions go to {0} with this connection's table structure and the SQL in the editor. It reads no rows. + + + Sent with the structure of {0} + + + Waiting for the reply + + + Retry + + + Copy + + + Insert in editor + + + The reply reached its length limit. + + + The model declined to answer. + + + {0} tokens + + + Stopped. + + + The assistant answered. + + + The assistant may not send from this connection now. + + + Use the AI assistant here? + + + The assistant sends your questions, your SQL and the structure of the tables involved from “{0}” to {1}. It reads no rows. Your answer lasts until this connection disconnects. + + + Allow + + + the server at {0} + + + {0} ({1}) + + + Explain with AI + + + Review with AI + + + Optimize with AI + + + Fix with AI + + + Add your {0} API key in Settings. + + + Choose a model in Settings. + + + The server address in Settings is not an http or https address. + + + An API key goes to another computer over https only. Use an https address, or remove the key. + + + The API key holds characters a key cannot have. Enter it again in Settings. + + + A secure connection could not be set up: {0} + + + TablePro could not reach {0}: {1} + + + {0} did not answer in time. + + + {0} refused the API key. Check it in Settings. + + + {0} is limiting requests. Wait a moment and try again. + + + {0} answered with error {1}. + + + The provider stopped the reply: {0} + + + The reply could not be read: {0} + + + The reply grew longer than TablePro keeps, so it stopped. + + + This connection's AI policy is Never, so nothing is sent from it. + + + {0} The server said: {1} + + + Show AI assistant + + + A + + + Review with AI + + + V + + + Explain with AI + + + X + + + Optimize with AI + + + O + diff --git a/windows/src/TablePro.App/Views/AssistantPane.xaml b/windows/src/TablePro.App/Views/AssistantPane.xaml new file mode 100644 index 0000000000..a04038f167 --- /dev/null +++ b/windows/src/TablePro.App/Views/AssistantPane.xaml @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/windows/src/TablePro.App/Views/AssistantPane.xaml.cs b/windows/src/TablePro.App/Views/AssistantPane.xaml.cs new file mode 100644 index 0000000000..1cbe9b9b0d --- /dev/null +++ b/windows/src/TablePro.App/Views/AssistantPane.xaml.cs @@ -0,0 +1,785 @@ +using System.Globalization; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Automation.Peers; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Documents; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using TablePro.App.Model; +using TablePro.App.Services; +using TablePro.Interop; +using Windows.ApplicationModel.DataTransfer; +using Windows.System; + +namespace TablePro.App.Views; + +// What the pane reaches in its workspace. Policy and the editor are read again for each request. +internal sealed record AssistantHooks( + DatabaseSession Session, + string ConnectionName, + string EngineName, + Func Policy, + Func EditorText, + Func EditorDatabase, + Action Insert, + Action OpenSettings); + +// One conversation at a time about one session's tables, its replies streaming in. The workspace builds a new pane +// for each session, so an answer under Ask each time lasts until the connection disconnects. +public sealed partial class AssistantPane : UserControl +{ + private static readonly TimeSpan PaintEvery = TimeSpan.FromMilliseconds(80); + private static readonly FontFamily Monospace = new("Cascadia Mono, Consolas, Courier New"); + + private readonly Workbench _workbench; + private readonly AssistantHooks _hooks; + private readonly AssistantAccess _access; + private readonly Microsoft.UI.Dispatching.DispatcherQueueTimer _painter; + private AssistantConversation? _conversation; + private CancellationTokenSource? _work; + private AiReply? _reply; + + // Where the reply on its way was sent, so a change of provider or address stops it. + private AiSettings? _replySettings; + private TurnView? _streaming; + private TurnView? _lastTurn; + private bool _busy; + + // Bumped by each new conversation, so work for an older one that lands late is dropped. + private int _generation; + + internal AssistantPane(Workbench workbench, AssistantHooks hooks) + { + _workbench = workbench; + _hooks = hooks; + _access = new AssistantAccess(() => _workbench.Settings.Assistant, hooks.Policy); + InitializeComponent(); + _painter = DispatcherQueue.CreateTimer(); + _painter.IsRepeating = false; + _painter.Interval = PaintEvery; + _painter.Tick += (_, _) => _streaming?.Paint(); + Refresh(); + } + + public event EventHandler? CloseRequested; + + // Show what the assistant can do here now: off, kept from this connection, or the conversation. The workspace + // calls it as the settings or the connection's policy change. + public void Refresh() + { + var availability = _access.Availability; + // A reply on its way stops as Stop stops it, so its turn keeps what came, once it may no longer go where it went. + if (_reply is not null && (_replySettings is null || !_access.MaySendTo(_replySettings))) + { + _reply.Cancel(); + } + + OffPanel.Visibility = availability == AssistantAvailability.Off ? Visibility.Visible : Visibility.Collapsed; + BlockedPanel.Visibility = availability == AssistantAvailability.Blocked ? Visibility.Visible : Visibility.Collapsed; + ChatPanel.Visibility = availability == AssistantAvailability.Ready ? Visibility.Visible : Visibility.Collapsed; + ComposerBar.Visibility = ChatPanel.Visibility; + var settings = _workbench.Settings.Assistant; + ModelText.Text = settings?.Model ?? string.Empty; + EmptyText.Text = settings is null + ? string.Empty + : Strings.Format("AssistantEmpty", AssistantText.Destination(settings)); + SyncControls(); + } + + // Starts a new conversation on an editor's statement. + internal void RunAction(AiAction action, string statement, string? error = null) + { + NewConversation(); + Ask(new AssistantAsk.Action(action, statement, error)); + } + + // A pane shown for the first time has not loaded yet, and an unloaded box takes no focus. + public void FocusComposer() + { + if (ComposerBar.Visibility != Visibility.Visible) + { + return; + } + + if (Composer.IsLoaded) + { + Composer.Focus(FocusState.Programmatic); + return; + } + + void OnLoaded(object sender, RoutedEventArgs e) + { + Composer.Loaded -= OnLoaded; + Composer.Focus(FocusState.Programmatic); + } + + Composer.Loaded += OnLoaded; + } + + // The session ends: whatever is on its way stops, and work that lands after is dropped. + public void Shutdown() + { + _generation++; + StopWork(); + } + + private void OnClose(object sender, RoutedEventArgs e) => CloseRequested?.Invoke(this, EventArgs.Empty); + + private void OnOpenSettings(object sender, RoutedEventArgs e) => _hooks.OpenSettings(); + + private void OnNewConversation(object sender, RoutedEventArgs e) + { + NewConversation(); + FocusComposer(); + } + + private void OnComposerChanged(object sender, TextChangedEventArgs e) => SyncControls(); + + // Enter sends, as a chat does; Shift+Enter starts a new line. An input method's Enter comes as ProcessKey. + private void OnComposerKeyDown(object sender, KeyRoutedEventArgs e) + { + if (e.Key != VirtualKey.Enter) + { + return; + } + + var shift = Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift) + .HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down); + if (!shift) + { + e.Handled = true; + SendTyped(); + } + } + + private void OnSend(object sender, RoutedEventArgs e) => SendTyped(); + + private void OnStop(object sender, RoutedEventArgs e) => _reply?.Cancel(); + + private void SendTyped() + { + var text = Composer.Text.Trim(); + if (text.Length > 0 && !_busy) + { + Ask(new AssistantAsk.Typed(text)); + } + } + + private void SyncControls() + { + var replying = _reply is not null; + SendButton.Visibility = replying ? Visibility.Collapsed : Visibility.Visible; + StopButton.Visibility = replying ? Visibility.Visible : Visibility.Collapsed; + SendButton.IsEnabled = !_busy && !string.IsNullOrWhiteSpace(Composer.Text); + NewButton.IsEnabled = _conversation is not null || _busy; + var started = Messages.Children.Count > 0; + EmptyText.Visibility = started ? Visibility.Collapsed : Visibility.Visible; + Scroller.Visibility = started ? Visibility.Visible : Visibility.Collapsed; + } + + private void NewConversation() + { + StopWork(); + _generation++; + _conversation = null; + _lastTurn = null; + Messages.Children.Clear(); + SyncControls(); + } + + // Drops the request in flight, whatever stage it is at. + private void StopWork() + { + _work?.Cancel(); + _work = null; + _reply?.Dispose(); + _reply = null; + _replySettings = null; + _painter.Stop(); + _streaming = null; + _busy = false; + } + + private async void Ask(AssistantAsk asked) + { + if (_busy) + { + return; + } + + _busy = true; + SyncControls(); + var generation = _generation; + using var work = new CancellationTokenSource(); + _work = work; + try + { + var allowed = await _access.PermitAsync(settings => ConfirmAsync(settings, work.Token)).ConfigureAwait(true); + if (generation != _generation) + { + return; + } + + if (!allowed) + { + EndWork(generation); + Refresh(); + return; + } + + if (asked is AssistantAsk.Typed) + { + Composer.Text = string.Empty; + } + + await BeginTurnAsync(asked, generation, work.Token).ConfigureAwait(true); + } + catch (OperationCanceledException) + { + // A new conversation or the session's end stopped it; the work was let go there. + } + finally + { + if (ReferenceEquals(_work, work)) + { + _work = null; + } + } + } + + // Starts the conversation when there is none, shows the question and asks for the reply. + private async Task BeginTurnAsync(AssistantAsk asked, int generation, CancellationToken cancellationToken) + { + IReadOnlyList described = []; + if (_conversation is null) + { + var editor = asked is AssistantAsk.Typed ? _hooks.EditorText() : null; + var named = asked switch + { + AssistantAsk.Typed typed => typed.Text, + AssistantAsk.Action action => action.Statement, + _ => string.Empty, + }; + AiConversationStart start; + try + { + start = await _workbench.Core.Assistant.StartConversationAsync( + _hooks.Session, + new AiConversationRequest(_hooks.EngineName, named, editor, _hooks.EditorDatabase()), + cancellationToken) + .ConfigureAwait(true); + } + catch (TableProException exception) + { + if (generation == _generation) + { + var refused = ShowTurn(asked, []); + Failed(refused, generation, AssistantText.Error(exception.Error), retry: false); + Refresh(); + } + + return; + } + + if (generation != _generation) + { + return; + } + + _conversation = new AssistantConversation(start.System, start.Described); + described = start.Described; + } + + var message = asked switch + { + AssistantAsk.Action action => AiAssistant.ActionMessage(action.Kind, action.Statement, action.Error), + AssistantAsk.Typed typed => typed.Text, + _ => string.Empty, + }; + _conversation.Ask(asked, message); + var turn = ShowTurn(asked, described); + await ReplyAsync(turn, generation, cancellationToken).ConfigureAwait(true); + } + + private async Task ReplyAsync(TurnView turn, int generation, CancellationToken cancellationToken) + { + turn.Waiting(); + // Settings or the policy may have changed while the request was made, so it is asked again where it must be. + var settings = _workbench.Settings.Assistant; + if (settings is null || !_access.MaySendTo(settings)) + { + var allowed = await _access.PermitAsync(asked => ConfirmAsync(asked, cancellationToken)).ConfigureAwait(true); + settings = _workbench.Settings.Assistant; + if (generation != _generation) + { + return; + } + + if (!allowed || settings is null || !_access.MaySendTo(settings)) + { + Failed(turn, generation, Strings.Get("AssistantNotAllowedNow"), retry: false); + Refresh(); + return; + } + } + + if (_conversation is not { } conversation) + { + EndWork(generation); + return; + } + + // A stop that came while this was asked starts no request. + cancellationToken.ThrowIfCancellationRequested(); + var reply = _workbench.Core.Assistant.Reply(_hooks.Session, settings, conversation.Request()); + _reply = reply; + _replySettings = settings; + _streaming = turn; + SyncControls(); + await foreach (var replyEvent in reply.ReadEventsAsync(cancellationToken).ConfigureAwait(true)) + { + switch (replyEvent) + { + case AiReplyEvent.Text text: + turn.Append(text.Part); + if (!_painter.IsRunning) + { + _painter.Start(); + } + + break; + case AiReplyEvent.Finished finished: + Finished(turn, generation, finished); + break; + case AiReplyEvent.Failed failed: + Failed(turn, generation, AssistantText.Error(failed.Error), retry: true); + break; + default: + Stopped(turn, generation); + break; + } + } + } + + private void Finished(TurnView turn, int generation, AiReplyEvent.Finished end) + { + if (!EndReply(turn, generation)) + { + return; + } + + _conversation?.Answer(turn.Text); + var note = end.Stop switch + { + AiStop.LengthLimit => Strings.Get("AssistantLengthLimit"), + AiStop.Refused => Strings.Get("AssistantRefused"), + _ => null, + }; + var total = (end.InputTokens ?? 0) + (end.OutputTokens ?? 0); + var tokens = end.InputTokens is null && end.OutputTokens is null + ? null + : Strings.Format("AssistantTokens", total.ToString("N0", CultureInfo.CurrentCulture)); + turn.Ended(string.Join(' ', new[] { note, tokens }.OfType())); + Announce(Strings.Get("AssistantAnswered")); + } + + private void Stopped(TurnView turn, int generation) + { + if (!EndReply(turn, generation)) + { + return; + } + + if (turn.Text.Length > 0) + { + _conversation?.Answer(turn.Text); + } + + turn.Ended(Strings.Get("AssistantStopped")); + } + + private void Failed(TurnView turn, int generation, string message, bool retry) + { + if (!EndReply(turn, generation)) + { + return; + } + + turn.Failed(message, retry ? () => Retry(turn) : null); + Announce(message); + } + + // Whether the reply that ended belongs to the conversation shown; the pane is free for the next question either way. + private bool EndReply(TurnView turn, int generation) + { + if (generation != _generation) + { + return false; + } + + _painter.Stop(); + turn.Paint(); + EndWork(generation); + return true; + } + + private void EndWork(int generation) + { + if (generation != _generation) + { + return; + } + + _reply?.Dispose(); + _reply = null; + _replySettings = null; + _streaming = null; + _busy = false; + SyncControls(); + } + + // Asks again for the last turn's reply, which failed, after the same check a new question goes through. + private async void Retry(TurnView turn) + { + if (_busy || !ReferenceEquals(turn, _lastTurn) || _conversation is null) + { + return; + } + + _busy = true; + turn.Clear(); + SyncControls(); + var generation = _generation; + using var work = new CancellationTokenSource(); + _work = work; + try + { + await ReplyAsync(turn, generation, work.Token).ConfigureAwait(true); + } + catch (OperationCanceledException) + { + // A new conversation or the session's end stopped it. + } + finally + { + if (ReferenceEquals(_work, work)) + { + _work = null; + } + } + } + + // Cancel is the answer Enter gives, and the question names the connection and where its data goes. + private async Task ConfirmAsync(AiSettings settings, CancellationToken cancellationToken) + { + // An editor action shows the pane and asks at once, before the pane has joined the window. + if (!IsLoaded) + { + await LoadedAsync(cancellationToken).ConfigureAwait(true); + } + + var text = new TextBlock + { + Text = Strings.Format("AssistantConsentMessage", _hooks.ConnectionName, AssistantText.Destination(settings)), + TextWrapping = TextWrapping.Wrap, + }; + AutomationProperties.SetAutomationId(text, "AssistantConsentText"); + var dialog = new ContentDialog + { + XamlRoot = XamlRoot, + RequestedTheme = ActualTheme, + Title = Strings.Get("AssistantConsentTitle"), + Content = text, + PrimaryButtonText = Strings.Get("AssistantConsentAllow"), + CloseButtonText = Strings.Get("Cancel"), + DefaultButton = ContentDialogButton.Close, + }; + // A request stopped while it asks takes the question down with it. + using var registration = cancellationToken.Register(dialog.Hide); + var answer = await WindowDialogs.ShowAsync(dialog, this, cancellationToken).ConfigureAwait(true); + cancellationToken.ThrowIfCancellationRequested(); + return answer == ContentDialogResult.Primary; + } + + private Task LoadedAsync(CancellationToken cancellationToken) + { + var loaded = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + void OnLoaded(object sender, RoutedEventArgs args) + { + Loaded -= OnLoaded; + loaded.TrySetResult(); + } + + Loaded += OnLoaded; + return loaded.Task.WaitAsync(cancellationToken); + } + + private TurnView ShowTurn(AssistantAsk asked, IReadOnlyList described) + { + // Only the last turn's reply can be asked for again. + _lastTurn?.DropRetry(); + var turn = new TurnView(this, asked, described); + Messages.Children.Add(turn.Root); + _lastTurn = turn; + SyncControls(); + Scroller.UpdateLayout(); + Scroller.ChangeView(null, Scroller.ScrollableHeight, null, disableAnimation: true); + return turn; + } + + // Narrator says it whether or not the pane has focus, as a reply ends while the user reads elsewhere. + private void Announce(string message) => + (FrameworkElementAutomationPeer.FromElement(this) ?? FrameworkElementAutomationPeer.CreatePeerForElement(this)) + ?.RaiseNotificationEvent( + AutomationNotificationKind.ActionCompleted, + AutomationNotificationProcessing.ImportantMostRecent, + message, + "TableProAssistantReply"); + + private void Insert(string code) => _hooks.Insert(code); + + private static void Copy(string code) + { + var package = new DataPackage(); + package.SetText(code); + try + { + Clipboard.SetContent(package); + Clipboard.Flush(); + } + catch (COMException) + { + // Another program holds the clipboard; the code stays selectable in the block. + } + } + + // One question and its reply on screen. + private sealed class TurnView + { + private readonly AssistantPane _pane; + private readonly StackPanel _reply = new() { Spacing = 8 }; + private readonly StackPanel _footer = new() { Spacing = 8 }; + private readonly StringBuilder _text = new(); + private Button? _retry; + private int _painted; + + public TurnView(AssistantPane pane, AssistantAsk asked, IReadOnlyList described) + { + _pane = pane; + Root = new StackPanel { Spacing = 8 }; + Root.Children.Add(Question(asked)); + if (described.Count > 0) + { + Root.Children.Add(pane.Caption(Strings.Format("AssistantSentStructure", string.Join(", ", described)))); + } + + AutomationProperties.SetAutomationId(_reply, "AssistantReply"); + Root.Children.Add(_reply); + Root.Children.Add(_footer); + } + + public StackPanel Root { get; } + + public string Text => _text.ToString(); + + public void Append(string part) => _text.Append(part); + + public void Waiting() + { + _footer.Children.Clear(); + var ring = new ProgressRing { IsActive = true, Width = 16, Height = 16 }; + AutomationProperties.SetName(ring, Strings.Get("AssistantWaiting")); + _footer.Children.Add(ring); + } + + public void Clear() + { + _text.Clear(); + _painted = 0; + _retry = null; + _reply.Children.Clear(); + _footer.Children.Clear(); + } + + // Each paint reads the whole reply again, so the pane paints at most every PaintEvery while it streams. + public void Paint() + { + if (_text.Length == _painted) + { + return; + } + + _painted = _text.Length; + if (_footer.Children.FirstOrDefault() is ProgressRing) + { + _footer.Children.Clear(); + } + + _reply.Children.Clear(); + foreach (var block in ReplyMarkdown.Read(_text.ToString())) + { + _reply.Children.Add(block switch + { + ReplyBlock.Code code => _pane.CodeBlock(code.Text, actions: true), + ReplyBlock.Paragraph paragraph => Prose(paragraph), + _ => new TextBlock(), + }); + } + + _pane.FollowEnd(); + } + + public void Ended(string note) + { + _footer.Children.Clear(); + if (note.Length > 0) + { + _footer.Children.Add(_pane.Caption(note)); + } + } + + public void Failed(string message, Action? retry) + { + _footer.Children.Clear(); + var text = new TextBlock { Text = message, Style = _pane.Named("AssistantErrorStyle") }; + AutomationProperties.SetAutomationId(text, "AssistantError"); + _footer.Children.Add(text); + if (retry is not null) + { + var button = new Button { Content = Strings.Get("AssistantRetry") }; + AutomationProperties.SetAutomationId(button, "AssistantRetryButton"); + button.Click += (_, _) => retry(); + _footer.Children.Add(button); + _retry = button; + } + } + + public void DropRetry() + { + if (_retry is { } retry) + { + _footer.Children.Remove(retry); + _retry = null; + } + } + + private Border Question(AssistantAsk asked) + { + var card = new StackPanel { Spacing = 6 }; + switch (asked) + { + case AssistantAsk.Typed typed: + card.Children.Add(new TextBlock { Text = typed.Text, TextWrapping = TextWrapping.Wrap, IsTextSelectionEnabled = true }); + break; + case AssistantAsk.Action action: + card.Children.Add(new TextBlock + { + Text = AssistantText.Action(action.Kind), + Style = _pane.Named("AssistantTitleStyle"), + }); + card.Children.Add(_pane.CodeBlock(action.Statement, actions: false)); + if (action.Error is { Length: > 0 } error) + { + card.Children.Add(_pane.Caption(error)); + } + + break; + } + + return new Border { Child = card, Style = _pane.Named("AssistantQuestionStyle") }; + } + + private static RichTextBlock Prose(ReplyBlock.Paragraph paragraph) + { + var block = new Paragraph(); + foreach (var span in paragraph.Spans) + { + var lines = span.Text.Split('\n'); + for (var index = 0; index < lines.Length; index++) + { + if (index > 0) + { + block.Inlines.Add(new LineBreak()); + } + + if (lines[index].Length == 0) + { + continue; + } + + Inline run = span.Style switch + { + ReplySpanStyle.Bold => new Bold { Inlines = { new Run { Text = lines[index] } } }, + ReplySpanStyle.Code => new Run { Text = lines[index], FontFamily = Monospace }, + _ => new Run { Text = lines[index] }, + }; + block.Inlines.Add(run); + } + } + + var text = new RichTextBlock { IsTextSelectionEnabled = true, TextWrapping = TextWrapping.Wrap }; + if (paragraph.IsHeading) + { + text.FontWeight = FontWeights.SemiBold; + } + + text.Blocks.Add(block); + return text; + } + } + + private void FollowEnd() + { + // Follows the reply as it grows while the user is at the end, and leaves them where they scrolled to. + if (Scroller.VerticalOffset >= Scroller.ScrollableHeight - 24) + { + Scroller.UpdateLayout(); + Scroller.ChangeView(null, Scroller.ScrollableHeight, null, disableAnimation: true); + } + } + + // Code from a reply has Copy and Insert in editor; the question's statement is only shown. + private Border CodeBlock(string code, bool actions) + { + var text = new TextBlock + { + Text = code, + FontFamily = Monospace, + IsTextSelectionEnabled = true, + TextWrapping = TextWrapping.NoWrap, + }; + AutomationProperties.SetAutomationId(text, actions ? "AssistantCode" : "AssistantStatement"); + var panel = new StackPanel { Spacing = 6 }; + panel.Children.Add(new ScrollViewer + { + Content = text, + HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, + VerticalScrollBarVisibility = ScrollBarVisibility.Disabled, + HorizontalScrollMode = ScrollMode.Auto, + VerticalScrollMode = ScrollMode.Disabled, + }); + if (actions) + { + var buttons = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 4 }; + var copy = new Button { Content = Strings.Get("AssistantCopyCode") }; + AutomationProperties.SetAutomationId(copy, "AssistantCopyCode"); + copy.Click += (_, _) => Copy(code); + var insert = new Button { Content = Strings.Get("AssistantInsertCode") }; + AutomationProperties.SetAutomationId(insert, "AssistantInsertCode"); + insert.Click += (_, _) => Insert(code); + buttons.Children.Add(copy); + buttons.Children.Add(insert); + panel.Children.Add(buttons); + } + + return new Border { Child = panel, Style = Named("AssistantCodeStyle") }; + } + + private Style Named(string key) => (Style)Resources[key]; + + private TextBlock Caption(string text) => new() { Text = text, Style = Named("AssistantCaptionStyle") }; +} diff --git a/windows/src/TablePro.App/Views/IWorkspaceHost.cs b/windows/src/TablePro.App/Views/IWorkspaceHost.cs index 58c0e09f87..267eeb0143 100644 --- a/windows/src/TablePro.App/Views/IWorkspaceHost.cs +++ b/windows/src/TablePro.App/Views/IWorkspaceHost.cs @@ -22,4 +22,7 @@ internal interface IWorkspaceHost // At once and on this thread, before a table tab's save sends anything; a refused write is logged. void WriteSessionNow(); + + // The Settings page, in the window with the rail. + void ShowSettings(); } diff --git a/windows/src/TablePro.App/Views/OpenConnection.cs b/windows/src/TablePro.App/Views/OpenConnection.cs index 6e7789a877..a9427d59b1 100644 --- a/windows/src/TablePro.App/Views/OpenConnection.cs +++ b/windows/src/TablePro.App/Views/OpenConnection.cs @@ -72,6 +72,18 @@ public OpenConnection(Workbench workbench, SessionConnection origin) public void SetTags(IReadOnlyList tags) => Tags = tags; + // What the assistant may send from it, read from the library; a file opened without saving is asked about. + public AiPolicy AiPolicy { get; private set; } = AiPolicy.Ask; + + public void SetAiPolicy(AiPolicy policy) + { + if (policy != AiPolicy) + { + AiPolicy = policy; + View.AssistantChanged(); + } + } + // The engine's artwork in the connection's colour, as the macOS rail marks it. public void Mark(string engineId, string? color) { @@ -114,6 +126,7 @@ public OpenConnection Adopt() var adopted = new OpenConnection(_workbench, Origin with { Tabs = [], SelectedTab = 0 }); adopted.Mark(Origin.EngineId, Color); adopted.SetTags(Tags); + adopted.SetAiPolicy(AiPolicy); adopted._life.Adopt(_life); return adopted; } diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index a7c116e02c..825c3cb5fe 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -14,9 +14,15 @@ namespace TablePro.App.Views; +// What a query tab asks its workspace's assistant; Error is the server's, for Fix. +internal sealed record AssistantRequest(AiAction Action, string Statement, string? Error = null); + // The editor over the run's results; the run, the close and the transaction are QueryTab's. public sealed partial class QueryTabView : UserControl, ITabContent, IQueryTabView, IQueryFileView, IShareSource, IExportSource { + // In the order macOS lists them, after the editor's own commands. + private static readonly AiAction[] EditorActions = [AiAction.Review, AiAction.Explain, AiAction.Optimize]; + private readonly QueryTab _model; private EditTarget _target; private bool _listingDatabases; @@ -107,6 +113,15 @@ private async void LoadRestoredFile(SqlFileStamp? stamp, bool unsaved) => // Save query... on the editor's menu; the workspace asks its name and folder. public event EventHandler? SaveQueryRequested; + // Explain, Review or Optimize with AI on the editor's menu, or Fix with AI on a refused run; the workspace asks. + internal event EventHandler? AssistantRequested; + + // Set by the workspace: whether its assistant may be asked now, which the AI commands follow. + internal Func AssistantReady { get; set; } = static () => false; + + // The database the tab runs in; null for the connection's. + internal string? Database => _model.Database; + // Set by the workspace that holds the tab: only a saved connection keeps queries. public bool CanSaveQuery { get; set; } @@ -166,6 +181,32 @@ private async void LoadRestoredFile(SqlFileStamp? stamp, bool unsaved) => public void FocusContent() => SqlText.Focus(FocusState.Programmatic); + // Null with no statement at the caret, which the status line then says. + internal string? AssistantStatement() + { + var statement = _model.AssistantStatement(); + if (statement is null) + { + StatusText.Text = Strings.Get("StatusNoStatement"); + } + + return statement; + } + + internal void AskAssistant(AiAction action) + { + if (AssistantReady() && AssistantStatement() is { } statement) + { + AssistantRequested?.Invoke(this, new AssistantRequest(action, statement)); + } + } + + // After a run, and after the assistant's settings or the connection's policy changed. + internal void OfferFix() => + Results.OfferFix(_model.LastFailure is { } failure && AssistantReady() + ? () => AssistantRequested?.Invoke(this, new AssistantRequest(AiAction.Fix, failure.Statement, failure.Error)) + : null); + public SessionTab Snapshot() => _model.Snapshot(); public SessionTab WithPendingWork(SessionTab snapshot) => _model.WithPendingWork(snapshot); @@ -389,6 +430,7 @@ private void OnModelStateChanged() { AutomationProperties.SetName(SqlText, Strings.Format("EditorName", _model.Title)); ShowDatabase(); + OfferFix(); StateChanged?.Invoke(this, EventArgs.Empty); } @@ -537,6 +579,14 @@ private void OnEditorMenuOpening(object? sender, IList items items.Add(new MenuFlyoutSeparator()); items.Add(save); items.Add(share); + var ready = AssistantReady(); + items.Add(new MenuFlyoutSeparator()); + foreach (var action in EditorActions) + { + var item = new MenuFlyoutItem { Text = AssistantText.Action(action), IsEnabled = ready }; + item.Click += (_, _) => AskAssistant(action); + items.Add(item); + } } private void OnRun(object sender, RoutedEventArgs e) => Run(); diff --git a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs index bf02559676..482cab8645 100644 --- a/windows/src/TablePro.App/Views/ResultsPane.xaml.cs +++ b/windows/src/TablePro.App/Views/ResultsPane.xaml.cs @@ -231,6 +231,21 @@ public async Task SortHeldRowsAsync(GridSort sort) public void ShowError(CoreError failure) => ShowError(Strings.Error(failure)); + // Fix with AI on the error bar while a refused run's error shows; null takes it away. + internal void OfferFix(Action? fix) + { + if (fix is null) + { + ErrorBar.ActionButton = null; + return; + } + + var button = new Button { Content = Strings.Get("AiFix") }; + AutomationProperties.SetAutomationId(button, "FixWithAiButton"); + button.Click += (_, _) => fix(); + ErrorBar.ActionButton = button; + } + public void ShowError(string message) { ErrorBar.Severity = InfoBarSeverity.Error; diff --git a/windows/src/TablePro.App/Views/SettingsView.Assistant.cs b/windows/src/TablePro.App/Views/SettingsView.Assistant.cs new file mode 100644 index 0000000000..3247fe653e --- /dev/null +++ b/windows/src/TablePro.App/Views/SettingsView.Assistant.cs @@ -0,0 +1,273 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Input; +using TablePro.App.Services; +using TablePro.Interop; +using Windows.System; + +namespace TablePro.App.Views; + +// The AI section: the provider, the compatible server's address, the key in Credential Manager and the model. +public sealed partial class SettingsView +{ + // Counts key reads and writes, so one that lands after the provider changed, or after a later one, is not shown. + private int _keyRead; + private CancellationTokenSource? _listingModels; + + private AiProvider? ChosenProvider => (AiProviderBox.SelectedItem as ComboBoxItem)?.Tag as AiProvider?; + + // Called while _loading is set, so nothing here writes the settings back. + private void LoadAssistant() + { + var settings = _workbench.Settings; + AiProviderBox.Items.Clear(); + AiProviderBox.Items.Add(new ComboBoxItem { Content = Strings.Get("AiProviderOff") }); + foreach (var info in AiAssistant.Providers) + { + AiProviderBox.Items.Add(new ComboBoxItem { Content = AssistantText.ProviderName(info.Provider), Tag = info.Provider }); + } + + AiProviderBox.SelectedItem = AiProviderBox.Items + .OfType() + .First(item => (item.Tag as AiProvider?) == settings.AiProvider); + AiAddressBox.Text = settings.CompatibleAddress; + ShowProvider(settings.AiProvider); + } + + private void ShowProvider(AiProvider? provider) + { + var on = provider is not null; + var compatible = provider == AiProvider.OpenAICompatible; + AiProviderCard.Description = Strings.Get(on ? "AiProviderOnDescription" : "AiProviderOffDescription"); + AiAddressCard.Visibility = compatible ? Visibility.Visible : Visibility.Collapsed; + AiKeyCard.Visibility = on ? Visibility.Visible : Visibility.Collapsed; + AiModelCard.Visibility = on ? Visibility.Visible : Visibility.Collapsed; + AiKeyCard.Header = Strings.Get(compatible ? "AiKeyOptionalHeader" : "AiKeyHeader"); + AiKeyBox.Password = string.Empty; + AiKeyRemoveButton.Visibility = Visibility.Collapsed; + AiStatusText.Text = string.Empty; + _listingModels?.Cancel(); + AiModelBox.Items.Clear(); + AiModelBox.Text = provider is { } chosen ? _workbench.Settings.ModelOf(chosen) : string.Empty; + if (compatible) + { + CheckAddress(AiAddressBox.Text); + } + + if (provider is { } shown) + { + ReadKeyState(shown); + } + } + + private void OnAiProviderChanged(object sender, SelectionChangedEventArgs e) + { + if (_loading) + { + return; + } + + var provider = ChosenProvider; + _workbench.UpdateSettings(settings => settings with { AiProvider = provider }); + ShowProvider(provider); + } + + private void OnAiAddressChanged(object sender, TextChangedEventArgs e) + { + if (_loading) + { + return; + } + + var address = AiAddressBox.Text; + _workbench.UpdateSettings(settings => settings with { CompatibleAddress = address }); + CheckAddress(address); + // Models listed by the server before are not this one's; the model typed stays. + _listingModels?.Cancel(); + var model = AiModelBox.Text; + AiModelBox.Items.Clear(); + AiModelBox.Text = model; + } + + private void CheckAddress(string address) + { + var valid = AiAssistant.IsValidAddress(address.Trim()); + AiAddressCard.Description = Strings.Get(valid ? "AiAddressDescription" : "AiAddressInvalid"); + AutomationProperties.SetHelpText(AiAddressBox, valid ? string.Empty : Strings.Get("AiAddressInvalid")); + } + + // Read when the provider shows, so Credential Manager is asked only once the user looks here. + private async void ReadKeyState(AiProvider provider) + { + var read = ++_keyRead; + bool? kept; + string? problem = null; + try + { + kept = await _workbench.Core.Assistant.HasKeyAsync(provider).ConfigureAwait(true); + } + catch (TableProException exception) + { + kept = null; + problem = AssistantText.Error(exception.Error); + } + + if (read != _keyRead || ChosenProvider != provider) + { + return; + } + + AiKeyCard.Description = kept switch + { + true => Strings.Get("AiKeyKept"), + false => Strings.Get(provider == AiProvider.OpenAICompatible ? "AiKeyNoneOptional" : "AiKeyNone"), + null => problem ?? string.Empty, + }; + AiKeyRemoveButton.Visibility = kept == true ? Visibility.Visible : Visibility.Collapsed; + } + + private void OnAiKeyTyped(object sender, RoutedEventArgs e) => + AiKeySaveButton.IsEnabled = !string.IsNullOrWhiteSpace(AiKeyBox.Password); + + private async void OnAiKeyKeyDown(object sender, KeyRoutedEventArgs e) + { + if (e.Key == VirtualKey.Enter && AiKeySaveButton.IsEnabled) + { + e.Handled = true; + await SaveKeyAsync(AiKeyBox.Password).ConfigureAwait(true); + } + } + + private async void OnAiKeySave(object sender, RoutedEventArgs e) => + await SaveKeyAsync(AiKeyBox.Password).ConfigureAwait(true); + + private async void OnAiKeyRemove(object sender, RoutedEventArgs e) => + await SaveKeyAsync(string.Empty).ConfigureAwait(true); + + // A blank key removes the one kept. + private async Task SaveKeyAsync(string key) + { + if (ChosenProvider is not { } provider) + { + return; + } + + var read = ++_keyRead; + AiKeySaveButton.IsEnabled = false; + AiKeyRemoveButton.IsEnabled = false; + string status; + var saved = false; + try + { + await _workbench.Core.Assistant.SetKeyAsync(provider, key).ConfigureAwait(true); + status = Strings.Get(key.Length == 0 ? "AiKeyRemoved" : "AiKeySaved"); + saved = true; + } + catch (TableProException exception) + { + status = AssistantText.Error(exception.Error); + } + finally + { + AiKeyRemoveButton.IsEnabled = true; + AiKeySaveButton.IsEnabled = !string.IsNullOrWhiteSpace(AiKeyBox.Password); + } + + // A key typed for another provider, or after this save began, stays in the box. + if (read == _keyRead && ChosenProvider == provider) + { + if (saved && AiKeyBox.Password == key) + { + AiKeyBox.Password = string.Empty; + } + + AiStatusText.Text = status; + ReadKeyState(provider); + } + } + + // The list is read again each time it opens, since a server's models change as they are pulled. + private async void OnAiModelsOpened(object sender, object e) + { + if (ChosenProvider is not { } provider) + { + return; + } + + _listingModels?.Cancel(); + using var listing = new CancellationTokenSource(); + _listingModels = listing; + var asked = _workbench.Settings.AssistantFor(provider); + IReadOnlyList models; + try + { + AiStatusText.Text = Strings.Get("AiModelsReading"); + models = await _workbench.Core.Assistant + .ListModelsAsync(asked, listing.Token) + .ConfigureAwait(true); + } + catch (OperationCanceledException) + { + return; + } + catch (TableProException exception) + { + if (!listing.IsCancellationRequested) + { + AiStatusText.Text = Strings.Format("AiModelsFailed", AssistantText.Error(exception.Error)); + } + + return; + } + finally + { + if (ReferenceEquals(_listingModels, listing)) + { + _listingModels = null; + } + } + + if (listing.IsCancellationRequested + || ChosenProvider != provider + || _workbench.Settings.AssistantFor(provider).Address != asked.Address) + { + return; + } + + AiStatusText.Text = models.Count == 0 ? Strings.Get("AiModelsNone") : string.Empty; + // Read after the wait, so a model typed or picked while the list loaded stays. + var typed = AiModelBox.Text; + _loading = true; + AiModelBox.Items.Clear(); + foreach (var model in models) + { + AiModelBox.Items.Add(model); + } + + AiModelBox.Text = typed; + _loading = false; + } + + private void OnAiModelPicked(object sender, SelectionChangedEventArgs e) + { + if (!_loading && AiModelBox.SelectedItem is string model) + { + SaveModel(model); + } + } + + private void OnAiModelTyped(ComboBox sender, ComboBoxTextSubmittedEventArgs args) + { + SaveModel(args.Text); + } + + private void SaveModel(string model) + { + if (ChosenProvider is { } provider) + { + var trimmed = model.Trim(); + _workbench.UpdateSettings(settings => settings.WithModel(provider, trimmed)); + } + } +} diff --git a/windows/src/TablePro.App/Views/SettingsView.xaml b/windows/src/TablePro.App/Views/SettingsView.xaml index ab38f88de4..c47364b428 100644 --- a/windows/src/TablePro.App/Views/SettingsView.xaml +++ b/windows/src/TablePro.App/Views/SettingsView.xaml @@ -153,6 +153,45 @@