This commit is contained in:
Gregory Schier
2025-01-26 13:13:45 -08:00
committed by GitHub
parent 82b1ad35ff
commit f678593903
99 changed files with 3492 additions and 1583 deletions

View File

@@ -20,6 +20,9 @@ pub enum Error {
#[error("JSON error: {0}")]
JsonErr(#[from] serde_json::Error),
#[error("Timeout elapsed: {0}")]
TimeoutElapsed(#[from] tokio::time::error::Elapsed),
#[error("Plugin not found: {0}")]
PluginNotFoundErr(String),

View File

@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use tauri::{Runtime, WebviewWindow};
use ts_rs::TS;
@@ -8,7 +7,7 @@ use yaak_models::models::{Environment, Folder, GrpcRequest, HttpRequest, HttpRes
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct InternalEvent {
pub id: String,
pub plugin_ref_id: String,
@@ -29,12 +28,12 @@ pub(crate) struct InternalEventRawPayload {
pub plugin_name: String,
pub reply_id: Option<String>,
pub window_context: WindowContext,
pub payload: Value,
pub payload: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum WindowContext {
None,
Label { label: String },
@@ -50,7 +49,7 @@ impl WindowContext {
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum InternalEventPayload {
BootRequest(BootRequest),
BootResponse(BootResponse),
@@ -84,20 +83,38 @@ pub enum InternalEventPayload {
CallTemplateFunctionRequest(CallTemplateFunctionRequest),
CallTemplateFunctionResponse(CallTemplateFunctionResponse),
GetHttpAuthenticationRequest(EmptyPayload),
GetHttpAuthenticationResponse(GetHttpAuthenticationResponse),
// Http Authentication
GetHttpAuthenticationSummaryRequest(EmptyPayload),
GetHttpAuthenticationSummaryResponse(GetHttpAuthenticationSummaryResponse),
GetHttpAuthenticationConfigRequest(GetHttpAuthenticationConfigRequest),
GetHttpAuthenticationConfigResponse(GetHttpAuthenticationConfigResponse),
CallHttpAuthenticationRequest(CallHttpAuthenticationRequest),
CallHttpAuthenticationResponse(CallHttpAuthenticationResponse),
CallHttpAuthenticationActionRequest(CallHttpAuthenticationActionRequest),
CallHttpAuthenticationActionResponse(EmptyPayload),
CopyTextRequest(CopyTextRequest),
CopyTextResponse(EmptyPayload),
RenderHttpRequestRequest(RenderHttpRequestRequest),
RenderHttpRequestResponse(RenderHttpRequestResponse),
GetKeyValueRequest(GetKeyValueRequest),
GetKeyValueResponse(GetKeyValueResponse),
SetKeyValueRequest(SetKeyValueRequest),
SetKeyValueResponse(SetKeyValueResponse),
DeleteKeyValueRequest(DeleteKeyValueRequest),
DeleteKeyValueResponse(DeleteKeyValueResponse),
OpenWindowRequest(OpenWindowRequest),
WindowNavigateEvent(WindowNavigateEvent),
CloseWindowRequest(CloseWindowRequest),
TemplateRenderRequest(TemplateRenderRequest),
TemplateRenderResponse(TemplateRenderResponse),
ShowToastRequest(ShowToastRequest),
ShowToastResponse(EmptyPayload),
PromptTextRequest(PromptTextRequest),
PromptTextResponse(PromptTextResponse),
@@ -117,19 +134,19 @@ pub enum InternalEventPayload {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, type = "{}", export_to = "events.ts")]
#[ts(export, type = "{}", export_to = "gen_events.ts")]
pub struct EmptyPayload {}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ErrorResponse {
pub error: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct BootRequest {
pub dir: String,
pub watch: bool,
@@ -137,7 +154,7 @@ pub struct BootRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct BootResponse {
pub name: String,
pub version: String,
@@ -145,21 +162,21 @@ pub struct BootResponse {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ImportRequest {
pub content: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ImportResponse {
pub resources: ImportResources,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FilterRequest {
pub content: String,
pub filter: String,
@@ -167,49 +184,50 @@ pub struct FilterRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FilterResponse {
pub content: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ExportHttpRequestRequest {
pub http_request: HttpRequest,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ExportHttpRequestResponse {
pub content: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct SendHttpRequestRequest {
#[ts(type = "Partial<HttpRequest>")]
pub http_request: HttpRequest,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct SendHttpRequestResponse {
pub http_response: HttpResponse,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CopyTextRequest {
pub text: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct RenderHttpRequestRequest {
pub http_request: HttpRequest,
pub purpose: RenderPurpose,
@@ -217,14 +235,14 @@ pub struct RenderHttpRequestRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct RenderHttpRequestResponse {
pub http_request: HttpRequest,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct TemplateRenderRequest {
pub data: serde_json::Value,
pub purpose: RenderPurpose,
@@ -232,25 +250,62 @@ pub struct TemplateRenderRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct TemplateRenderResponse {
pub data: serde_json::Value,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct OpenWindowRequest {
pub url: String,
/// Label for the window. If not provided, a random one will be generated.
pub label: String,
#[ts(optional)]
pub title: Option<String>,
#[ts(optional)]
pub size: Option<WindowSize>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct WindowSize {
pub width: f64,
pub height: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CloseWindowRequest {
pub label: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct WindowNavigateEvent {
pub url: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ShowToastRequest {
pub message: String,
#[ts(optional)]
pub color: Option<Color>,
#[ts(optional)]
pub icon: Option<Icon>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct PromptTextRequest {
// A unique ID to identify the prompt (eg. "enter-password")
pub id: String,
@@ -277,17 +332,15 @@ pub struct PromptTextRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct PromptTextResponse {
pub value: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum Color {
Custom,
Default,
Primary,
Secondary,
Info,
@@ -299,18 +352,23 @@ pub enum Color {
impl Default for Color {
fn default() -> Self {
Color::Default
Color::Secondary
}
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum Icon {
AlertTriangle,
Check,
CheckCircle,
ChevronDown,
Copy,
Info,
CheckCircle,
AlertTriangle,
Pin,
Search,
Trash,
#[serde(untagged)]
#[ts(type = "\"_unknown\"")]
@@ -319,17 +377,45 @@ pub enum Icon {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
pub struct GetHttpAuthenticationResponse {
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpAuthenticationSummaryResponse {
pub name: String,
pub label: String,
pub short_label: String,
pub config: Vec<FormInput>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct HttpAuthenticationAction {
pub label: String,
#[ts(optional)]
pub icon: Option<Icon>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpAuthenticationConfigRequest {
pub context_id: String,
pub values: HashMap<String, JsonPrimitive>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpAuthenticationConfigResponse {
pub args: Vec<FormInput>,
pub plugin_ref_id: String,
#[ts(optional)]
pub actions: Option<Vec<HttpAuthenticationAction>>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct HttpHeader {
pub name: String,
pub value: String,
@@ -337,9 +423,10 @@ pub struct HttpHeader {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpAuthenticationRequest {
pub config: serde_json::Map<String, serde_json::Value>,
pub context_id: String,
pub values: HashMap<String, JsonPrimitive>,
pub method: String,
pub url: String,
pub headers: Vec<HttpHeader>,
@@ -347,7 +434,34 @@ pub struct CallHttpAuthenticationRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpAuthenticationActionRequest {
pub index: i32,
pub plugin_ref_id: String,
pub args: CallHttpAuthenticationActionArgs,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpAuthenticationActionArgs {
pub context_id: String,
pub values: HashMap<String, JsonPrimitive>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(untagged)]
#[ts(export, export_to = "gen_events.ts")]
pub enum JsonPrimitive {
String(String),
Number(f64),
Boolean(bool),
Null,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpAuthenticationResponse {
/// HTTP headers to add to the request. Existing headers will be replaced, while
/// new headers will be added.
@@ -356,7 +470,7 @@ pub struct CallHttpAuthenticationResponse {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetTemplateFunctionsResponse {
pub functions: Vec<TemplateFunction>,
pub plugin_ref_id: String,
@@ -364,9 +478,10 @@ pub struct GetTemplateFunctionsResponse {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct TemplateFunction {
pub name: String,
#[ts(optional)]
pub description: Option<String>,
@@ -379,7 +494,7 @@ pub struct TemplateFunction {
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum FormInput {
Text(FormInputText),
Editor(FormInputEditor),
@@ -387,14 +502,23 @@ pub enum FormInput {
Checkbox(FormInputCheckbox),
File(FormInputFile),
HttpRequest(FormInputHttpRequest),
Accordion(FormInputAccordion),
Banner(FormInputBanner),
Markdown(FormInputMarkdown),
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputBase {
/// The name of the input. The value will be stored at this object attribute in the resulting data
pub name: String,
/// Whether this input is visible for the given configuration. Use this to
/// make branching forms.
#[ts(optional)]
pub hidden: Option<bool>,
/// Whether the user must fill in the argument
#[ts(optional)]
pub optional: Option<bool>,
@@ -410,11 +534,14 @@ pub struct FormInputBase {
/// The default value
#[ts(optional)]
pub default_value: Option<String>,
#[ts(optional)]
pub disabled: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputText {
#[serde(flatten)]
pub base: FormInputBase,
@@ -426,11 +553,18 @@ pub struct FormInputText {
/// Placeholder for the text input
#[ts(optional)]
pub password: Option<bool>,
/// Whether to allow newlines in the input, like a <textarea/>
#[ts(optional)]
pub multi_line: Option<bool>,
#[ts(optional)]
pub completion_options: Option<Vec<GenericCompletionOption>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum EditorLanguage {
Text,
Javascript,
@@ -449,7 +583,7 @@ impl Default for EditorLanguage {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputEditor {
#[serde(flatten)]
pub base: FormInputBase,
@@ -465,11 +599,45 @@ pub struct FormInputEditor {
/// Language for syntax highlighting
#[ts(optional)]
pub language: Option<EditorLanguage>,
#[ts(optional)]
pub read_only: Option<bool>,
#[ts(optional)]
pub completion_options: Option<Vec<GenericCompletionOption>>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GenericCompletionOption {
label: String,
#[ts(optional)]
detail: Option<String>,
#[ts(optional)]
info: Option<String>,
#[ts(optional)]
#[serde(rename = "type")]
pub type_: Option<CompletionOptionType>,
#[ts(optional)]
pub boost: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "gen_events.ts")]
pub enum CompletionOptionType {
Constant,
Variable,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputHttpRequest {
#[serde(flatten)]
pub base: FormInputBase,
@@ -477,7 +645,7 @@ pub struct FormInputHttpRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputFile {
#[serde(flatten)]
pub base: FormInputBase,
@@ -504,7 +672,7 @@ pub struct FormInputFile {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FileFilter {
pub name: String,
/// File extensions to require
@@ -513,7 +681,7 @@ pub struct FileFilter {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputSelect {
#[serde(flatten)]
pub base: FormInputBase,
@@ -524,7 +692,7 @@ pub struct FormInputSelect {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputCheckbox {
#[serde(flatten)]
pub base: FormInputBase,
@@ -532,15 +700,68 @@ pub struct FormInputCheckbox {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputSelectOption {
pub name: String,
pub label: String,
pub value: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputAccordion {
pub label: String,
#[ts(optional)]
pub inputs: Option<Vec<FormInput>>,
#[ts(optional)]
pub hidden: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputBanner {
#[ts(optional)]
pub inputs: Option<Vec<FormInput>>,
#[ts(optional)]
pub hidden: Option<bool>,
#[ts(optional)]
pub color: Option<Color>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputMarkdown {
pub content: String,
#[ts(optional)]
pub hidden: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "gen_events.ts")]
pub enum Content {
Text { content: String },
Markdown { content: String },
}
impl Default for Content {
fn default() -> Self {
Self::Text {
content: String::default(),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallTemplateFunctionRequest {
pub name: String,
pub args: CallTemplateFunctionArgs,
@@ -548,14 +769,14 @@ pub struct CallTemplateFunctionRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallTemplateFunctionResponse {
pub value: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallTemplateFunctionArgs {
pub purpose: RenderPurpose,
pub values: HashMap<String, String>,
@@ -563,7 +784,7 @@ pub struct CallTemplateFunctionArgs {
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub enum RenderPurpose {
Send,
Preview,
@@ -577,12 +798,12 @@ impl Default for RenderPurpose {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpRequestActionsRequest {}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpRequestActionsResponse {
pub actions: Vec<HttpRequestAction>,
pub plugin_ref_id: String,
@@ -590,9 +811,8 @@ pub struct GetHttpRequestActionsResponse {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct HttpRequestAction {
pub key: String,
pub label: String,
#[ts(optional)]
pub icon: Option<Icon>,
@@ -600,37 +820,37 @@ pub struct HttpRequestAction {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpRequestActionRequest {
pub key: String,
pub index: i32,
pub plugin_ref_id: String,
pub args: CallHttpRequestActionArgs,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct CallHttpRequestActionArgs {
pub http_request: HttpRequest,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpRequestByIdRequest {
pub id: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetHttpRequestByIdResponse {
pub http_request: Option<HttpRequest>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FindHttpResponsesRequest {
pub request_id: String,
#[ts(optional)]
@@ -639,14 +859,14 @@ pub struct FindHttpResponsesRequest {
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FindHttpResponsesResponse {
pub http_responses: Vec<HttpResponse>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "events.ts")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ImportResources {
pub workspaces: Vec<Workspace>,
pub environments: Vec<Environment>,
@@ -654,3 +874,45 @@ pub struct ImportResources {
pub http_requests: Vec<HttpRequest>,
pub grpc_requests: Vec<GrpcRequest>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetKeyValueRequest {
pub key: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct GetKeyValueResponse {
#[ts(optional)]
pub value: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct SetKeyValueRequest {
pub key: String,
pub value: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, type = "{}", export_to = "gen_events.ts")]
pub struct SetKeyValueResponse {}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct DeleteKeyValueRequest {
pub key: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export, export_to = "gen_events.ts")]
pub struct DeleteKeyValueResponse {
pub deleted: bool,
}

View File

@@ -3,12 +3,13 @@ use crate::error::Error::{
};
use crate::error::Result;
use crate::events::{
BootRequest, CallHttpAuthenticationRequest, CallHttpAuthenticationResponse,
CallHttpRequestActionRequest, CallTemplateFunctionArgs, CallTemplateFunctionRequest,
CallTemplateFunctionResponse, EmptyPayload, FilterRequest, FilterResponse, FormInput,
GetHttpAuthenticationResponse, GetHttpRequestActionsResponse, GetTemplateFunctionsResponse,
ImportRequest, ImportResponse, InternalEvent, InternalEventPayload, RenderPurpose,
WindowContext,
BootRequest, CallHttpAuthenticationActionArgs, CallHttpAuthenticationActionRequest,
CallHttpAuthenticationRequest, CallHttpAuthenticationResponse, CallHttpRequestActionRequest,
CallTemplateFunctionArgs, CallTemplateFunctionRequest, CallTemplateFunctionResponse,
EmptyPayload, FilterRequest, FilterResponse, GetHttpAuthenticationConfigRequest,
GetHttpAuthenticationConfigResponse, GetHttpAuthenticationSummaryResponse,
GetHttpRequestActionsResponse, GetTemplateFunctionsResponse, ImportRequest, ImportResponse,
InternalEvent, InternalEventPayload, JsonPrimitive, RenderPurpose, WindowContext,
};
use crate::nodejs::start_nodejs_plugin_runtime;
use crate::plugin_handle::PluginHandle;
@@ -24,6 +25,7 @@ use tauri::{AppHandle, Manager, Runtime, WebviewWindow};
use tokio::fs::read_dir;
use tokio::net::TcpListener;
use tokio::sync::{mpsc, Mutex};
use tokio::time::timeout;
use yaak_models::queries::{generate_id, list_plugins};
#[derive(Clone)]
@@ -86,7 +88,7 @@ impl PluginManager {
let addr = listener.local_addr().expect("Failed to get local address");
// 1. Reload all plugins when the Node.js runtime connects
{
let init_plugins_task = {
let plugin_manager = plugin_manager.clone();
let app_handle = app_handle.clone();
tauri::async_runtime::spawn(async move {
@@ -94,7 +96,7 @@ impl PluginManager {
Ok(_) => {
info!("Plugin runtime client connected!");
plugin_manager
.initialize_all_plugins(&app_handle, WindowContext::None)
.initialize_all_plugins(&app_handle, &WindowContext::None)
.await
.expect("Failed to reload plugins");
}
@@ -102,7 +104,7 @@ impl PluginManager {
warn!("Failed to receive from client connection rx {e:?}");
}
}
});
})
};
// 1. Spawn server in the background
@@ -114,8 +116,13 @@ impl PluginManager {
// 2. Start Node.js runtime and initialize plugins
tauri::async_runtime::block_on(async move {
start_nodejs_plugin_runtime(&app_handle, addr, &kill_server_rx).await.unwrap();
info!("Waiting for plugins to initialize");
init_plugins_task.await.unwrap();
});
// 3. Block waiting for plugins to initialize
tauri::async_runtime::block_on(async move {});
plugin_manager
}
@@ -160,14 +167,14 @@ impl PluginManager {
[bundled_plugin_dirs, installed_plugin_dirs].concat()
}
pub async fn uninstall(&self, window_context: WindowContext, dir: &str) -> Result<()> {
pub async fn uninstall(&self, window_context: &WindowContext, dir: &str) -> Result<()> {
let plugin = self.get_plugin_by_dir(dir).await.ok_or(PluginNotFoundErr(dir.to_string()))?;
self.remove_plugin(window_context, &plugin).await
}
async fn remove_plugin(
&self,
window_context: WindowContext,
window_context: &WindowContext,
plugin: &PluginHandle,
) -> Result<()> {
// Terminate the plugin
@@ -185,7 +192,7 @@ impl PluginManager {
pub async fn add_plugin_by_dir(
&self,
window_context: WindowContext,
window_context: &WindowContext,
dir: &str,
watch: bool,
) -> Result<()> {
@@ -197,20 +204,22 @@ impl PluginManager {
};
let plugin_handle = PluginHandle::new(dir, tx.clone());
// Add the new plugin
self.plugins.lock().await.push(plugin_handle.clone());
// Boot the plugin
let event = self
.send_to_plugin_and_wait(
let event = timeout(
Duration::from_secs(1),
self.send_to_plugin_and_wait(
window_context,
&plugin_handle,
&InternalEventPayload::BootRequest(BootRequest {
dir: dir.to_string(),
watch,
}),
)
.await?;
),
)
.await??;
// Add the new plugin
self.plugins.lock().await.push(plugin_handle.clone());
let resp = match event.payload {
InternalEventPayload::BootResponse(resp) => resp,
@@ -226,20 +235,21 @@ impl PluginManager {
pub async fn initialize_all_plugins<R: Runtime>(
&self,
app_handle: &AppHandle<R>,
window_context: WindowContext,
window_context: &WindowContext,
) -> Result<()> {
let dirs = self.list_plugin_dirs(app_handle).await;
for d in dirs.clone() {
let candidates = self.list_plugin_dirs(app_handle).await;
for candidate in candidates.clone() {
// First remove the plugin if it exists
if let Some(plugin) = self.get_plugin_by_dir(d.dir.as_str()).await {
if let Err(e) = self.remove_plugin(window_context.to_owned(), &plugin).await {
warn!("Failed to remove plugin {} {e:?}", d.dir);
if let Some(plugin) = self.get_plugin_by_dir(candidate.dir.as_str()).await {
if let Err(e) = self.remove_plugin(window_context, &plugin).await {
warn!("Failed to remove plugin {} {e:?}", candidate.dir);
}
}
if let Err(e) =
self.add_plugin_by_dir(window_context.to_owned(), d.dir.as_str(), d.watch).await
if let Err(e) = self
.add_plugin_by_dir(window_context, candidate.dir.as_str(), candidate.watch)
.await
{
warn!("Failed to add plugin {} {e:?}", d.dir);
warn!("Failed to add plugin {} {e:?}", candidate.dir);
}
}
@@ -280,13 +290,13 @@ impl PluginManager {
source_event: &InternalEvent,
payload: &InternalEventPayload,
) -> Result<()> {
let window_label = source_event.to_owned().window_context;
let window_context = source_event.to_owned().window_context;
let reply_id = Some(source_event.to_owned().id);
let plugin = self
.get_plugin_by_ref_id(source_event.plugin_ref_id.as_str())
.await
.ok_or(PluginNotFoundErr(source_event.plugin_ref_id.to_string()))?;
let event = plugin.build_event_to_send_raw(window_label, &payload, reply_id);
let event = plugin.build_event_to_send_raw(&window_context, &payload, reply_id);
plugin.send(&event).await
}
@@ -310,7 +320,7 @@ impl PluginManager {
async fn send_to_plugin_and_wait(
&self,
window_context: WindowContext,
window_context: &WindowContext,
plugin: &PluginHandle,
payload: &InternalEventPayload,
) -> Result<InternalEvent> {
@@ -321,7 +331,7 @@ impl PluginManager {
async fn send_and_wait(
&self,
window_context: WindowContext,
window_context: &WindowContext,
payload: &InternalEventPayload,
) -> Result<Vec<InternalEvent>> {
let plugins = { self.plugins.lock().await.clone() };
@@ -330,7 +340,7 @@ impl PluginManager {
async fn send_to_plugins_and_wait(
&self,
window_context: WindowContext,
window_context: &WindowContext,
payload: &InternalEventPayload,
plugins: Vec<PluginHandle>,
) -> Result<Vec<InternalEvent>> {
@@ -340,7 +350,7 @@ impl PluginManager {
// 1. Build the events with IDs and everything
let events_to_send = plugins
.iter()
.map(|p| p.build_event_to_send(window_context.to_owned(), payload, None))
.map(|p| p.build_event_to_send(window_context, payload, None))
.collect::<Vec<InternalEvent>>();
// 2. Spawn thread to subscribe to incoming events and check reply ids
@@ -358,9 +368,9 @@ impl PluginManager {
if matched_sent_event {
found_events.push(event.clone());
};
let found_them_all = found_events.len() == events_to_send.len();
if found_them_all{
if found_them_all {
break;
}
}
@@ -393,7 +403,7 @@ impl PluginManager {
) -> Result<Vec<GetHttpRequestActionsResponse>> {
let reply_events = self
.send_and_wait(
WindowContext::from_window(window),
&WindowContext::from_window(window),
&InternalEventPayload::GetHttpRequestActionsRequest(EmptyPayload {}),
)
.await?;
@@ -412,12 +422,12 @@ impl PluginManager {
&self,
window: &WebviewWindow<R>,
) -> Result<Vec<GetTemplateFunctionsResponse>> {
self.get_template_functions_with_context(WindowContext::from_window(window)).await
self.get_template_functions_with_context(&WindowContext::from_window(window)).await
}
pub async fn get_template_functions_with_context(
&self,
window_context: WindowContext,
window_context: &WindowContext,
) -> Result<Vec<GetTemplateFunctionsResponse>> {
let reply_events = self
.send_and_wait(window_context, &InternalEventPayload::GetTemplateFunctionsRequest)
@@ -442,7 +452,7 @@ impl PluginManager {
let plugin =
self.get_plugin_by_ref_id(ref_id.as_str()).await.ok_or(PluginNotFoundErr(ref_id))?;
let event = plugin.build_event_to_send(
WindowContext::from_window(window),
&WindowContext::from_window(window),
&InternalEventPayload::CallHttpRequestActionRequest(req),
None,
);
@@ -450,21 +460,22 @@ impl PluginManager {
Ok(())
}
pub async fn get_http_authentication<R: Runtime>(
pub async fn get_http_authentication_summaries<R: Runtime>(
&self,
window: &WebviewWindow<R>,
) -> Result<Vec<(PluginHandle, GetHttpAuthenticationResponse)>> {
) -> Result<Vec<(PluginHandle, GetHttpAuthenticationSummaryResponse)>> {
let window_context = WindowContext::from_window(window);
let reply_events = self
.send_and_wait(
window_context,
&InternalEventPayload::GetHttpAuthenticationRequest(EmptyPayload {}),
&window_context,
&InternalEventPayload::GetHttpAuthenticationSummaryRequest(EmptyPayload {}),
)
.await?;
let mut results = Vec::new();
for event in reply_events {
if let InternalEventPayload::GetHttpAuthenticationResponse(resp) = event.payload {
if let InternalEventPayload::GetHttpAuthenticationSummaryResponse(resp) = event.payload
{
let plugin = self
.get_plugin_by_ref_id(&event.plugin_ref_id)
.await
@@ -476,43 +487,84 @@ impl PluginManager {
Ok(results)
}
pub async fn get_http_authentication_config<R: Runtime>(
&self,
window: &WebviewWindow<R>,
auth_name: &str,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
) -> Result<GetHttpAuthenticationConfigResponse> {
let results = self.get_http_authentication_summaries(window).await?;
let plugin = results
.iter()
.find_map(|(p, r)| if r.name == auth_name { Some(p) } else { None })
.ok_or(PluginNotFoundErr(auth_name.into()))?;
let context_id = format!("{:x}", md5::compute(request_id.to_string()));
let event = self
.send_to_plugin_and_wait(
&WindowContext::from_window(window),
&plugin,
&InternalEventPayload::GetHttpAuthenticationConfigRequest(
GetHttpAuthenticationConfigRequest { values, context_id },
),
)
.await?;
match event.payload {
InternalEventPayload::GetHttpAuthenticationConfigResponse(resp) => Ok(resp),
InternalEventPayload::EmptyResponse(_) => {
Err(PluginErr("Auth plugin returned empty".to_string()))
}
e => Err(PluginErr(format!("Auth plugin returned invalid event {:?}", e))),
}
}
pub async fn call_http_authentication_action<R: Runtime>(
&self,
window: &WebviewWindow<R>,
auth_name: &str,
action_index: i32,
values: HashMap<String, JsonPrimitive>,
request_id: &str,
) -> Result<()> {
let results = self.get_http_authentication_summaries(window).await?;
let plugin = results
.iter()
.find_map(|(p, r)| if r.name == auth_name { Some(p) } else { None })
.ok_or(PluginNotFoundErr(auth_name.into()))?;
let context_id = format!("{:x}", md5::compute(request_id.to_string()));
self
.send_to_plugin_and_wait(
&WindowContext::from_window(window),
&plugin,
&InternalEventPayload::CallHttpAuthenticationActionRequest(
CallHttpAuthenticationActionRequest {
index: action_index,
plugin_ref_id: plugin.clone().ref_id,
args: CallHttpAuthenticationActionArgs { context_id, values },
},
),
)
.await?;
Ok(())
}
pub async fn call_http_authentication<R: Runtime>(
&self,
window: &WebviewWindow<R>,
auth_name: &str,
req: CallHttpAuthenticationRequest,
) -> Result<CallHttpAuthenticationResponse> {
let handlers = self.get_http_authentication(window).await?;
let (plugin, authentication) = handlers
let handlers = self.get_http_authentication_summaries(window).await?;
let (plugin, _) = handlers
.iter()
.find(|(_, a)| a.name == auth_name)
.ok_or(AuthPluginNotFound(auth_name.to_string()))?;
// Clone for mutability
let mut req = req.clone();
// Fill in default values
for arg in authentication.config.clone() {
let base = match arg {
FormInput::Text(a) => a.base,
FormInput::Editor(a) => a.base,
FormInput::Select(a) => a.base,
FormInput::Checkbox(a) => a.base,
FormInput::File(a) => a.base,
FormInput::HttpRequest(a) => a.base,
};
if let None = req.config.get(base.name.as_str()) {
let default = match base.default_value {
None => serde_json::Value::Null,
Some(s) => serde_json::Value::String(s),
};
req.config.insert(base.name, default);
}
}
let event = self
.send_to_plugin_and_wait(
WindowContext::from_window(window),
&WindowContext::from_window(window),
&plugin,
&InternalEventPayload::CallHttpAuthenticationRequest(req),
)
@@ -528,7 +580,7 @@ impl PluginManager {
pub async fn call_template_function(
&self,
window_context: WindowContext,
window_context: &WindowContext,
fn_name: &str,
args: HashMap<String, String>,
purpose: RenderPurpose,
@@ -562,7 +614,7 @@ impl PluginManager {
) -> Result<(ImportResponse, String)> {
let reply_events = self
.send_and_wait(
WindowContext::from_window(window),
&WindowContext::from_window(window),
&InternalEventPayload::ImportRequest(ImportRequest {
content: content.to_string(),
}),
@@ -607,7 +659,7 @@ impl PluginManager {
let event = self
.send_to_plugin_and_wait(
WindowContext::from_window(window),
&WindowContext::from_window(window),
&plugin,
&InternalEventPayload::FilterRequest(FilterRequest {
filter: filter.to_string(),

View File

@@ -26,6 +26,10 @@ impl PluginHandle {
}
}
pub async fn name(&self) -> String {
self.boot_resp.lock().await.name.clone()
}
pub async fn info(&self) -> BootResponse {
let resp = &*self.boot_resp.lock().await;
resp.clone()
@@ -33,7 +37,7 @@ impl PluginHandle {
pub fn build_event_to_send(
&self,
window_context: WindowContext,
window_context: &WindowContext,
payload: &InternalEventPayload,
reply_id: Option<String>,
) -> InternalEvent {
@@ -42,7 +46,7 @@ impl PluginHandle {
pub(crate) fn build_event_to_send_raw(
&self,
window_context: WindowContext,
window_context: &WindowContext,
payload: &InternalEventPayload,
reply_id: Option<String>,
) -> InternalEvent {
@@ -53,18 +57,18 @@ impl PluginHandle {
plugin_name: dir.file_name().unwrap().to_str().unwrap().to_string(),
reply_id,
payload: payload.clone(),
window_context,
window_context: window_context.clone(),
}
}
pub async fn terminate(&self, window_context: WindowContext) -> Result<()> {
pub async fn terminate(&self, window_context: &WindowContext) -> Result<()> {
info!("Terminating plugin {}", self.dir);
let event =
self.build_event_to_send(window_context, &InternalEventPayload::TerminateRequest, None);
self.send(&event).await
}
pub(crate) async fn send(&self, event: &InternalEvent) -> Result<()> {
pub async fn send(&self, event: &InternalEvent) -> Result<()> {
self.to_plugin_tx.lock().await.send(event.to_owned()).await?;
Ok(())
}

View File

@@ -85,8 +85,9 @@ impl PluginRuntimeServerWebsocket {
};
// Parse everything but the payload so we can catch errors on that, specifically
let payload = serde_json::from_value::<InternalEventPayload>(event.payload)
let payload = serde_json::from_value::<InternalEventPayload>(event.payload.clone())
.unwrap_or_else(|e| {
warn!("Plugin error from {}: {:?} {}", event.plugin_name, e, event.payload);
InternalEventPayload::ErrorResponse(ErrorResponse {
error: format!("Plugin error from {}: {e:?}", event.plugin_name),
})