Skip to content

Commit

Permalink
update action request type
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bonez committed Sep 11, 2024
1 parent 8996fd3 commit 69ce889
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 54 deletions.
4 changes: 2 additions & 2 deletions core/startos/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl fmt::Display for ActionResultV0 {
}
}

fn display_action_result<T: Serialize>(params: WithIoFormat<T>, result: Option<ActionResult>) {
pub fn display_action_result<T: Serialize>(params: WithIoFormat<T>, result: Option<ActionResult>) {
let Some(result) = result else {
return;
};
Expand All @@ -115,7 +115,7 @@ pub struct RunActionParams {
pub input: Option<Value>,
}

pub fn action<C: Context>() -> ParentHandler<C> {
pub fn action_api<C: Context>() -> ParentHandler<C> {
ParentHandler::new()
.subcommand(
"get-input",
Expand Down
27 changes: 22 additions & 5 deletions core/startos/src/db/model/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,14 @@ pub struct ActionMetadata {
#[serde(default)]
pub visibility: ActionVisibility,
pub allowed_statuses: AllowedStatuses,
pub has_input: bool,
pub group: Option<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, TS)]
#[ts(export)]
#[serde(rename_all = "kebab-case")]
#[serde(rename_all_fields = "camelCase")]
pub enum ActionVisibility {
Hidden,
Disabled { reason: String },
Expand Down Expand Up @@ -361,8 +363,8 @@ pub struct PackageDataEntry {
pub last_backup: Option<DateTime<Utc>>,
pub current_dependencies: CurrentDependencies,
pub actions: BTreeMap<ActionId, ActionMetadata>,
#[ts(as = "BTreeMap::<String, ActionRequest>")]
pub requested_actions: BTreeMap<InternedString, ActionRequest>,
#[ts(as = "BTreeMap::<String, ActionRequestEntry>")]
pub requested_actions: BTreeMap<InternedString, ActionRequestEntry>,
pub service_interfaces: BTreeMap<ServiceInterfaceId, ServiceInterface>,
pub hosts: Hosts,
#[ts(type = "string[]")]
Expand Down Expand Up @@ -409,8 +411,8 @@ pub struct CurrentDependencyInfo {
pub kind: CurrentDependencyKind,
#[ts(type = "string")]
pub version_range: VersionRange,
#[ts(as = "BTreeMap::<String, ActionRequest>")]
pub requested_actions: BTreeMap<InternedString, ActionRequest>,
#[ts(as = "BTreeMap::<String, ActionRequestEntry>")]
pub requested_actions: BTreeMap<InternedString, ActionRequestEntry>,
}
impl CurrentDependencyInfo {
pub fn update(&mut self, new: Self) {
Expand Down Expand Up @@ -443,14 +445,29 @@ impl Default for CurrentDependencyKind {
}
}

#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
pub struct ActionRequestEntry {
pub request: ActionRequest,
pub active: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
pub struct ActionRequest {
pub id: ActionId,
pub r#if: Option<ActionRequestCondition>,
pub when: Option<ActionRequestTrigger>,
pub input: Option<ActionRequestInput>,
}

#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
pub struct ActionRequestTrigger {
#[serde(default)]
pub once: bool,
pub condition: ActionRequestCondition,
}

#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "kebab-case")]
pub enum ActionRequestCondition {
Expand Down
2 changes: 1 addition & 1 deletion core/startos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn server<C: Context>() -> ParentHandler<C> {

pub fn package<C: Context>() -> ParentHandler<C> {
ParentHandler::new()
.subcommand("action", action::action::<C>())
.subcommand("action", action::action_api::<C>())
.subcommand(
"install",
from_fn_async(install::install)
Expand Down
94 changes: 79 additions & 15 deletions core/startos/src/service/effects/action.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeSet;

use models::{ActionId, PackageId};
use rpc_toolkit::{from_fn_async, Context, HandlerExt, ParentHandler};

use crate::action::ActionResult;
use crate::action::{display_action_result, ActionInput, ActionResult};
use crate::db::model::package::ActionMetadata;
use crate::rpc_continuations::Guid;
use crate::service::cli::ContainerCliContext;
use crate::service::effects::prelude::*;
use crate::util::serde::HandlerExtSerde;

pub fn action_api<C: Context>() -> ParentHandler<C> {
ParentHandler::new()
.subcommand("export", from_fn_async(export_action).no_cli())
.subcommand(
"clear",
from_fn_async(clear_actions)
.no_display()
.with_call_remote::<ContainerCliContext>(),
)
.subcommand(
"get-input",
from_fn_async(get_action_input)
.with_display_serializable()
.with_call_remote::<ContainerCliContext>(),
)
.subcommand(
"run",
from_fn_async(run_action)
.with_display_serializable()
.with_custom_display_fn(|args, res| Ok(display_action_result(args.params, res)))
.with_call_remote::<ContainerCliContext>(),
)
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct ExportActionParams {
#[ts(optional)]
package_id: Option<PackageId>,
id: ActionId,
metadata: ActionMetadata,
}
pub async fn export_action(context: EffectContext, data: ExportActionParams) -> Result<(), Error> {
pub async fn export_action(
context: EffectContext,
ExportActionParams { id, metadata }: ExportActionParams,
) -> Result<(), Error> {
let context = context.deref()?;
let package_id = context.seed.id.clone();
context
Expand All @@ -31,10 +59,7 @@ pub async fn export_action(context: EffectContext, data: ExportActionParams) ->
.or_not_found(&package_id)?
.as_actions_mut();
let mut value = model.de()?;
value
.insert(data.id, data.metadata)
.map(|_| ())
.unwrap_or_default();
value.insert(id, metadata);
model.ser(&value)
})
.await?;
Expand All @@ -49,7 +74,7 @@ pub struct ClearActionsParams {
pub except: Vec<ActionId>,
}

pub async fn clear_actions(
async fn clear_actions(
context: EffectContext,
ClearActionsParams { except }: ClearActionsParams,
) -> Result<(), Error> {
Expand All @@ -72,27 +97,66 @@ pub async fn clear_actions(
Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[derive(Debug, Clone, Serialize, Deserialize, TS, Parser)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct GetActionInputParams {
#[serde(default)]
#[ts(skip)]
#[arg(skip)]
procedure_id: Guid,
#[ts(optional)]
package_id: Option<PackageId>,
action_id: ActionId,
}
async fn get_action_input(
context: EffectContext,
GetActionInputParams {
procedure_id,
package_id,
action_id,
}: GetActionInputParams,
) -> Result<Option<ActionInput>, Error> {
let context = context.deref()?;

if let Some(package_id) = package_id {
context
.seed
.ctx
.services
.get(&package_id)
.await
.as_ref()
.or_not_found(&package_id)?
.get_action_input(procedure_id, action_id)
.await
} else {
context.get_action_input(procedure_id, action_id).await
}
}

#[derive(Debug, Clone, Serialize, Deserialize, TS, Parser)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct ExecuteAction {
pub struct RunActionParams {
#[serde(default)]
#[ts(skip)]
#[arg(skip)]
procedure_id: Guid,
#[ts(optional)]
package_id: Option<PackageId>,
action_id: ActionId,
#[ts(type = "any")]
input: Value,
}
pub async fn execute_action(
async fn run_action(
context: EffectContext,
ExecuteAction {
RunActionParams {
procedure_id,
package_id,
action_id,
input,
}: ExecuteAction,
}: RunActionParams,
) -> Result<Option<ActionResult>, Error> {
let context = context.deref()?;

Expand Down
7 changes: 4 additions & 3 deletions core/startos/src/service/effects/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use patch_db::json_ptr::JsonPointer;
use tokio::process::Command;

use crate::db::model::package::{
ActionRequest, CurrentDependencyInfo, CurrentDependencyKind, ManifestPreference,
ActionRequest, ActionRequestEntry, CurrentDependencyInfo, CurrentDependencyKind,
ManifestPreference,
};
use crate::disk::mount::filesystem::bind::Bind;
use crate::disk::mount::filesystem::idmapped::IdMapped;
Expand Down Expand Up @@ -321,8 +322,8 @@ pub struct CheckDependenciesResult {
#[ts(type = "string[]")]
satisfies: BTreeSet<VersionString>,
is_running: bool,
#[ts(as = "BTreeMap::<String, ActionRequest>")]
requested_actions: BTreeMap<InternedString, ActionRequest>,
#[ts(as = "BTreeMap::<String, ActionRequestEntry>")]
requested_actions: BTreeMap<InternedString, ActionRequestEntry>,
#[ts(as = "BTreeMap::<HealthCheckId, NamedHealthCheckResult>")]
health_checks: OrdMap<HealthCheckId, NamedHealthCheckResult>,
}
Expand Down
13 changes: 1 addition & 12 deletions core/startos/src/service/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ pub fn handler<C: Context>() -> ParentHandler<C> {
from_fn(echo::<EffectContext>).with_call_remote::<ContainerCliContext>(),
)
// action
.subcommand(
"execute-action",
from_fn_async(action::execute_action).no_cli(),
)
.subcommand(
"export-action",
from_fn_async(action::export_action).no_cli(),
)
.subcommand(
"clear-actions",
from_fn_async(action::clear_actions).no_cli(),
)
.subcommand("action", action::action_api::<C>())
// callbacks
.subcommand(
"clear-callbacks",
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/osBindings/ActionMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export type ActionMetadata = {
warning: string | null
visibility: ActionVisibility
allowedStatuses: AllowedStatuses
hasInput: boolean
group: string | null
}
4 changes: 2 additions & 2 deletions sdk/lib/osBindings/ActionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionId } from "./ActionId"
import type { ActionRequestCondition } from "./ActionRequestCondition"
import type { ActionRequestInput } from "./ActionRequestInput"
import type { ActionRequestTrigger } from "./ActionRequestTrigger"

export type ActionRequest = {
id: ActionId
if: ActionRequestCondition | null
when: ActionRequestTrigger | null
input: ActionRequestInput | null
}
4 changes: 4 additions & 0 deletions sdk/lib/osBindings/ActionRequestEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionRequest } from "./ActionRequest"

export type ActionRequestEntry = { request: ActionRequest; active: boolean }
7 changes: 7 additions & 0 deletions sdk/lib/osBindings/ActionRequestTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionRequestCondition } from "./ActionRequestCondition"

export type ActionRequestTrigger = {
once: boolean
condition: ActionRequestCondition
}
4 changes: 2 additions & 2 deletions sdk/lib/osBindings/CheckDependenciesResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionRequest } from "./ActionRequest"
import type { ActionRequestEntry } from "./ActionRequestEntry"
import type { HealthCheckId } from "./HealthCheckId"
import type { NamedHealthCheckResult } from "./NamedHealthCheckResult"
import type { PackageId } from "./PackageId"
Expand All @@ -10,6 +10,6 @@ export type CheckDependenciesResult = {
installedVersion: string | null
satisfies: string[]
isRunning: boolean
requestedActions: { [key: string]: ActionRequest }
requestedActions: { [key: string]: ActionRequestEntry }
healthChecks: { [key: HealthCheckId]: NamedHealthCheckResult }
}
4 changes: 2 additions & 2 deletions sdk/lib/osBindings/CurrentDependencyInfo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionRequest } from "./ActionRequest"
import type { ActionRequestEntry } from "./ActionRequestEntry"
import type { DataUrl } from "./DataUrl"

export type CurrentDependencyInfo = {
title: string | null
icon: DataUrl | null
versionRange: string
requestedActions: { [key: string]: ActionRequest }
requestedActions: { [key: string]: ActionRequestEntry }
} & (
| { kind: "optional" }
| { kind: "exists" }
Expand Down
7 changes: 1 addition & 6 deletions sdk/lib/osBindings/ExportActionParams.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionId } from "./ActionId"
import type { ActionMetadata } from "./ActionMetadata"
import type { PackageId } from "./PackageId"

export type ExportActionParams = {
packageId?: PackageId
id: ActionId
metadata: ActionMetadata
}
export type ExportActionParams = { id: ActionId; metadata: ActionMetadata }
5 changes: 5 additions & 0 deletions sdk/lib/osBindings/GetActionInputParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionId } from "./ActionId"
import type { PackageId } from "./PackageId"

export type GetActionInputParams = { packageId?: PackageId; actionId: ActionId }
4 changes: 2 additions & 2 deletions sdk/lib/osBindings/PackageDataEntry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ActionId } from "./ActionId"
import type { ActionMetadata } from "./ActionMetadata"
import type { ActionRequest } from "./ActionRequest"
import type { ActionRequestEntry } from "./ActionRequestEntry"
import type { CurrentDependencies } from "./CurrentDependencies"
import type { DataUrl } from "./DataUrl"
import type { Hosts } from "./Hosts"
Expand All @@ -21,7 +21,7 @@ export type PackageDataEntry = {
lastBackup: string | null
currentDependencies: CurrentDependencies
actions: { [key: ActionId]: ActionMetadata }
requestedActions: { [key: string]: ActionRequest }
requestedActions: { [key: string]: ActionRequestEntry }
serviceInterfaces: { [key: ServiceInterfaceId]: ServiceInterface }
hosts: Hosts
storeExposedDependents: string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { ActionId } from "./ActionId"
import type { PackageId } from "./PackageId"

export type ExecuteAction = {
export type RunActionParams = {
packageId?: PackageId
actionId: ActionId
input: any
Expand Down
Loading

0 comments on commit 69ce889

Please sign in to comment.