Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
52 changes: 52 additions & 0 deletions core/crates/ai/src/ai_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions core/crates/ai/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions core/crates/ai/src/model_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>, AiError> {
read_models(client, endpoint)
.await
.map_err(|error| endpoint.scrub(error))
}

async fn read_models(client: &AiClient, endpoint: &Endpoint) -> Result<Vec<String>, AiError> {
let path = match Protocol::of(endpoint.kind()) {
Protocol::AnthropicMessages => "/models?limit=1000",
Protocol::ChatCompletions => "/models",
Expand Down
6 changes: 4 additions & 2 deletions core/crates/ai/src/reply_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
}

Expand Down
1 change: 1 addition & 0 deletions core/crates/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading
Loading