mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-06 14:56:23 +01:00
Restore told open editors to re-read the request before its change event had reached the store, so header and parameter rows could keep showing the old content. The restore RPC now returns the restored request, and the frontend puts it in the store before calling wasUpdatedExternally, the same way deleteModel applies its change locally once the RPC resolves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
8.7 KiB
Rust
227 lines
8.7 KiB
Rust
//! Reads and writes of models, keyed by the client's identity so the frontend
|
|
//! can suppress its own echoes.
|
|
|
|
use crate::error::Result;
|
|
use crate::host::{Host, PluginHost};
|
|
use yaak_models::models::{
|
|
AnyModel, GraphQlIntrospection, GrpcEvent, HttpRequestHeader, ModelVersion,
|
|
RequestVersionComparison, Settings, WebsocketEvent, WorkspaceMeta,
|
|
};
|
|
use yaak_models::queries::workspaces::default_headers;
|
|
use yaak_models::versions::version_document;
|
|
use yaak_rpc_schema::*;
|
|
|
|
pub async fn models_upsert<H: Host>(host: H, req: ModelsUpsertReq) -> Result<String> {
|
|
let blobs = host.blob_manager();
|
|
let source = host.update_source();
|
|
Ok(host
|
|
.query_manager()
|
|
.with_tx(|tx| yaak_models::models_ops::upsert_model(tx, blobs, req.model, &source))?)
|
|
}
|
|
|
|
/// Deletes cascade — a workspace can hold thousands of requests — and run in a
|
|
/// transaction, which holds a raw connection for the duration.
|
|
///
|
|
/// Whether that wants a blocking thread is the *host's* question, not the
|
|
/// delete's: a desktop with a multi-threaded runtime should keep this off the
|
|
/// runtime (see its adapter), while a single-threaded host has nothing to move
|
|
/// it to and runs it here. So this is the plain version, and a host that wants
|
|
/// to relocate it calls [`models_delete_blocking`] itself.
|
|
pub async fn models_delete<H: Host>(host: H, req: ModelsDeleteReq) -> Result<String> {
|
|
models_delete_blocking(&host, req)
|
|
}
|
|
|
|
/// The body of [`models_delete`], callable from a blocking context.
|
|
pub fn models_delete_blocking<H: Host>(host: &H, req: ModelsDeleteReq) -> Result<String> {
|
|
let source = host.update_source();
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
yaak_models::models_ops::delete_model(tx, host.blob_manager(), req.model, &source)
|
|
})?)
|
|
}
|
|
|
|
/// Duplicates recurse, so this runs in a transaction too.
|
|
pub async fn models_duplicate<H: Host>(host: H, req: ModelsDuplicateReq) -> Result<String> {
|
|
let source = host.update_source();
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
yaak_models::models_ops::duplicate_model(tx, &req.model_type, &req.model_id, &source)
|
|
})?)
|
|
}
|
|
|
|
/// Capture the request's current content, from an edit-session boundary the
|
|
/// frontend can see: switching away, losing focus, closing, or falling idle.
|
|
///
|
|
/// The frontend does not track whether anything actually changed — versions are
|
|
/// content-addressed, so an unchanged request returns the version it already
|
|
/// had and the trigger code stays a one-liner.
|
|
pub async fn models_snapshot_request<H: Host>(
|
|
host: H,
|
|
req: ModelsSnapshotRequestReq,
|
|
) -> Result<ModelVersion> {
|
|
Ok(host.query_manager().with_tx(|tx| tx.snapshot_request_by_id(&req.request_id, req.reason))?)
|
|
}
|
|
|
|
/// A version and the live request side by side, for the diff and for deciding
|
|
/// whether there is anything worth offering.
|
|
pub async fn models_request_version<H: Host>(
|
|
host: H,
|
|
req: ModelsRequestVersionReq,
|
|
) -> Result<RequestVersionComparison> {
|
|
let db = host.db();
|
|
let version = db.get_model_version(&req.version_id)?;
|
|
let current_document = version_document(&db.get_any_request(&version.model_id)?.to_value()?)?;
|
|
let differs = !db.request_matches_version(&version)?;
|
|
Ok(RequestVersionComparison { version, current_document, differs })
|
|
}
|
|
|
|
/// Returns the restored request, so the frontend can put it in its store
|
|
/// without waiting for the change event to come back around.
|
|
pub async fn models_restore_request_version<H: Host>(
|
|
host: H,
|
|
req: ModelsRestoreRequestVersionReq,
|
|
) -> Result<AnyModel> {
|
|
let source = host.update_source();
|
|
let restored =
|
|
host.query_manager().with_tx(|tx| tx.restore_request_version(&req.version_id, &source))?;
|
|
Ok(restored.into())
|
|
}
|
|
|
|
pub async fn models_websocket_events<H: Host>(
|
|
host: H,
|
|
req: ModelsWebsocketEventsReq,
|
|
) -> Result<Vec<WebsocketEvent>> {
|
|
Ok(host.db().list_websocket_events(&req.connection_id)?)
|
|
}
|
|
|
|
pub async fn models_grpc_events<H: Host>(
|
|
host: H,
|
|
req: ModelsGrpcEventsReq,
|
|
) -> Result<Vec<GrpcEvent>> {
|
|
Ok(host.db().list_grpc_events(&req.connection_id)?)
|
|
}
|
|
|
|
pub async fn models_get_settings<H: Host>(host: H, _req: ModelsGetSettingsReq) -> Result<Settings> {
|
|
Ok(host.db().get_settings())
|
|
}
|
|
|
|
pub async fn models_get_graphql_introspection<H: Host>(
|
|
host: H,
|
|
req: ModelsGetGraphqlIntrospectionReq,
|
|
) -> Result<Option<GraphQlIntrospection>> {
|
|
Ok(host.db().get_graphql_introspection(&req.request_id))
|
|
}
|
|
|
|
pub async fn models_upsert_graphql_introspection<H: Host>(
|
|
host: H,
|
|
req: ModelsUpsertGraphqlIntrospectionReq,
|
|
) -> Result<GraphQlIntrospection> {
|
|
let source = host.update_source();
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
tx.upsert_graphql_introspection(&req.workspace_id, &req.request_id, req.content, &source)
|
|
})?)
|
|
}
|
|
|
|
/// Everything the frontend's model store needs to boot, as one JSON string.
|
|
///
|
|
/// A string rather than a `Vec<AnyModel>` because the desktop has to escape
|
|
/// this payload before it crosses into the webview (see its adapter), and the
|
|
/// frontend `JSON.parse`s either form the same way.
|
|
pub async fn models_workspace_models<H: PluginHost>(
|
|
host: H,
|
|
req: ModelsWorkspaceModelsReq,
|
|
) -> Result<String> {
|
|
let mut l: Vec<AnyModel> = Vec::new();
|
|
|
|
// Add the global models
|
|
{
|
|
let db = host.db();
|
|
l.push(db.get_settings().into());
|
|
l.append(&mut db.list_workspaces()?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_key_values()?.into_iter().map(Into::into).collect());
|
|
}
|
|
|
|
let plugins = {
|
|
let db = host.db();
|
|
db.list_plugins()?
|
|
};
|
|
|
|
let plugins = host.resolve_plugins(plugins).await;
|
|
l.append(&mut plugins.into_iter().map(Into::into).collect());
|
|
|
|
// Add the workspace children
|
|
if let Some(wid) = req.workspace_id.as_deref() {
|
|
// Opening a workspace is where the rows it is assumed to have get created
|
|
host.query_manager().with_tx(|tx| {
|
|
tx.ensure_base_environment(wid)?;
|
|
tx.ensure_default_cookie_jar(wid)?;
|
|
tx.ensure_workspace_meta(wid)?;
|
|
Ok::<(), yaak_models::error::Error>(())
|
|
})?;
|
|
let db = host.db();
|
|
l.append(&mut db.list_cookie_jars(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_environments(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_folders(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_grpc_connections(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_grpc_requests(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_http_requests(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_http_responses(wid, None)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_websocket_connections(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_websocket_requests(wid)?.into_iter().map(Into::into).collect());
|
|
l.append(&mut db.list_workspace_metas(wid)?.into_iter().map(Into::into).collect());
|
|
}
|
|
|
|
Ok(serde_json::to_string(&l)?)
|
|
}
|
|
|
|
pub async fn cmd_get_workspace_meta<H: Host>(
|
|
host: H,
|
|
req: CmdGetWorkspaceMetaReq,
|
|
) -> Result<WorkspaceMeta> {
|
|
let workspace = host.db().get_workspace(&req.workspace_id)?;
|
|
Ok(host.query_manager().with_tx(|tx| tx.ensure_workspace_meta(&workspace.id))?)
|
|
}
|
|
|
|
pub async fn cmd_delete_all_grpc_connections<H: Host>(
|
|
host: H,
|
|
req: CmdDeleteAllGrpcConnectionsReq,
|
|
) -> Result<()> {
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
tx.delete_all_grpc_connections_for_request(&req.request_id, &host.update_source())
|
|
})?)
|
|
}
|
|
|
|
pub async fn cmd_delete_all_http_responses<H: Host>(
|
|
host: H,
|
|
req: CmdDeleteAllHttpResponsesReq,
|
|
) -> Result<()> {
|
|
host.query_manager().with_tx(|tx| {
|
|
tx.delete_all_http_responses_for_request(&req.request_id, &host.update_source())
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn cmd_ws_delete_connections<H: Host>(
|
|
host: H,
|
|
req: CmdWsDeleteConnectionsReq,
|
|
) -> Result<()> {
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
tx.delete_all_websocket_connections_for_request(&req.request_id, &host.update_source())
|
|
})?)
|
|
}
|
|
|
|
pub async fn cmd_delete_send_history<H: Host>(host: H, req: CmdDeleteSendHistoryReq) -> Result<()> {
|
|
Ok(host.query_manager().with_tx(|tx| {
|
|
let source = &host.update_source();
|
|
tx.delete_all_http_responses_for_workspace(&req.workspace_id, source)?;
|
|
tx.delete_all_grpc_connections_for_workspace(&req.workspace_id, source)?;
|
|
tx.delete_all_websocket_connections_for_workspace(&req.workspace_id, source)?;
|
|
Ok::<(), yaak_models::error::Error>(())
|
|
})?)
|
|
}
|
|
|
|
pub async fn cmd_default_headers<H: Host>(
|
|
_host: H,
|
|
_req: CmdDefaultHeadersReq,
|
|
) -> Result<Vec<HttpRequestHeader>> {
|
|
Ok(default_headers())
|
|
}
|