Skip to content

Commit

Permalink
add support for add in schema editor
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed May 20, 2024
1 parent ae15113 commit e030486
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 98 deletions.
252 changes: 242 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

59 changes: 46 additions & 13 deletions crates/litehouse-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ mod import;
mod manifest;
mod parallelism;

use std::{cmp::Ordering, collections::HashMap, num::NonZeroU8};
use std::{
cmp::Ordering,
collections::{hash_map::Entry, HashMap},
num::NonZeroU8,
};

use miette::{NamedSource, SourceOffset};
use schemars::JsonSchema;
Expand Down Expand Up @@ -118,7 +122,7 @@ impl LitehouseConfig {
/// kept
/// - if the target is equally specific as the existing plugin, and they are not equal, the
/// existing definition is kept, and the new one is ignored
pub fn add_import<'a>(&'a mut self, import: Import) -> ImportAddResult<'a> {
pub fn add_import(&mut self, import: Import) -> ImportAddResult {
// this is awkward but the borrow checker does not understand
// returning in the match statement
let mut ret_replace = None;
Expand Down Expand Up @@ -163,20 +167,49 @@ impl LitehouseConfig {
}
}

pub fn add_manifest(&mut self, manifest: Manifest) {
manifest.config.into_iter().for_each(|(k, v)| {
self.plugins.insert(
k,
PluginInstance {
plugin: manifest.import.clone(),
config: Some(serde_json::to_value(v).unwrap()),
},
);
});
self.add_import(manifest.import);
pub fn add_manifest(
&mut self,
manifest: Manifest,
replace: bool,
) -> impl Iterator<Item = ManifestAddResult> + '_ {
self.add_import(manifest.import.clone());
manifest
.config
.into_iter()
.map(move |(k, config)| match self.plugins.entry(k.clone()) {
Entry::Occupied(mut e) => {
// check if they are equal, and ignore
let val = e.get_mut();
if val.plugin == manifest.import && val.config == config {
ManifestAddResult::Ignored(k)
} else if replace {
let v_old = e.insert(PluginInstance {
plugin: manifest.import.clone(),
config,
});
ManifestAddResult::Replaced(k, v_old.config)
} else {
ManifestAddResult::WouldReplace(k, config)
}
}
Entry::Vacant(entry) => {
entry.insert(PluginInstance {
plugin: manifest.import.clone(),
config,
});
ManifestAddResult::Added(k)
}
})
}
}

pub enum ManifestAddResult {
WouldReplace(String, Option<serde_json::Value>),
Replaced(String, Option<serde_json::Value>),
Added(String),
Ignored(String),
}

#[derive(Debug, PartialEq, Eq)]
pub enum ImportAddResult<'a> {
Added(&'a Import),
Expand Down
3 changes: 2 additions & 1 deletion crates/litehouse-config/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub enum ManifestImport {
#[derive(Debug, Clone)]
pub struct Manifest {
pub import: Import,
pub config: HashMap<String, serde_json::value::Map<String, serde_json::Value>>,
/// A map of instance names to their configuration
pub config: HashMap<String, Option<serde_json::Value>>,
}

#[derive(thiserror::Error, Debug)]
Expand Down
1 change: 1 addition & 0 deletions crates/litehouse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ reqsign = "0.14.9"
anyhow = "1.0.81"
bytes = "1.5.0"
which = "6.0.1"
inquire = "0.7.5"
130 changes: 92 additions & 38 deletions crates/litehouse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use jsonc_parser::CollectOptions;
use jsonschema::error::ValidationErrorKind;
use jsonschema::paths::{JSONPointer, PathChunk};
use litehouse_config::{
ConfigError, Import, ImportAddResult, LitehouseConfig, ManifestImport, ParseError,
PluginInstance, SandboxStrategy,
ConfigError, Import, ImportAddResult, LitehouseConfig, Manifest, ManifestAddResult,
ManifestImport, ParseError, PluginInstance, SandboxStrategy,
};
use litehouse_plugin::serde_json::{self, Value};
use miette::{miette, Diagnostic, IntoDiagnostic, NamedSource, Result, SourceSpan};
Expand Down Expand Up @@ -99,7 +99,7 @@ enum Subcommand {
/// Add a new plugin to the imports field in the settings file.
Add {
/// The package to add.
package: litehouse_config::ManifestImport,
package: Vec<litehouse_config::ManifestImport>,
/// The path to look for wasm files in.
#[clap(default_value = "./wasm", short, long)]
wasm_path: PathBuf,
Expand Down Expand Up @@ -222,33 +222,78 @@ async fn main_inner() -> Result<()> {

Ok(())
}
Subcommand::Add { package, wasm_path } => {
Subcommand::Add {
package: packages,
wasm_path,
} => {
// search the package, and add it to the settings' imports
let mut config = LitehouseConfig::load()?;
match package {
ManifestImport::Import(package) => {
println!("adding package {}", package);
match config.add_import(package) {
ImportAddResult::Added(_) => {
println!("package added");
}
ImportAddResult::Ignored(i) => {
println!("package already added ({i})");
}
ImportAddResult::Replaced(r) => {
println!("replaced {r}");
for package in packages {
match package {
ManifestImport::Import(package) => {
println!("adding package {}", package);
match config.add_import(package) {
ImportAddResult::Added(_) => {
println!("package added");
}
ImportAddResult::Ignored(i) => {
println!("package already added ({i})");
}
ImportAddResult::Replaced(r) => {
println!("replaced {r}");
}
}
}
}
ManifestImport::Manifest(manifest) => {
println!("adding manifest");
config.add_manifest(manifest);
ManifestImport::Manifest(manifest) => {
println!("adding manifest");
let import = manifest.import.clone();
let to_replace = config
.add_manifest(manifest, false)
.filter_map(|result| match result {
ManifestAddResult::Added(k) => {
println!("- {k}: manifest added");
None
}
ManifestAddResult::Ignored(k) => {
println!("- {k}: identical manifest already exists");
None
}
ManifestAddResult::WouldReplace(k, v) => {
let ans = inquire::Confirm::new(&format!(
" `{k}` already exists, replace it?"
))
.with_default(false)
.prompt();
if let Ok(true) = ans {
Some((k, v))
} else {
None
}
}
ManifestAddResult::Replaced(k, _old) => {
println!("- {k}: manifest replaced");
None
}
})
.fold(
Manifest {
import,
config: Default::default(),
},
|mut acc, (k, v)| {
acc.config.insert(k, v);
acc
},
);

config.add_manifest(to_replace, true).for_each(drop);
}
}
}

let cache_dir = litehouse_config::directories().map(|d| d.cache_dir().to_owned());

println!("downloading ");
println!("downloading");

let pass = fetch(
&config,
Expand Down Expand Up @@ -302,7 +347,7 @@ async fn main_inner() -> Result<()> {

let jobs = hosts
.into_iter()
.map(|(a, instance, host, mut store, _)| async move {
.map(|(a, _, host, mut store, _)| async move {
let store = store.enter().await;
let metadata = host
.litehouse_plugin_plugin()
Expand Down Expand Up @@ -453,15 +498,18 @@ async fn main_inner() -> Result<()> {
package,
access_key,
secret_key,
} => Ok(publish(
package,
&registry
.with_upload(access_key, secret_key)
.build()
.await
.wrap_err("can't download")?,
)
.await),
} => {
publish(
package,
&registry
.with_upload(access_key, secret_key)
.build()
.await
.wrap_err("can't download")?,
)
.await;
Ok(())
}
Subcommand::Fetch { wasm_path } => {
let cache_dir = litehouse_config::directories().map(|d| d.cache_dir().to_owned());
let config = LitehouseConfig::load()?;
Expand All @@ -482,7 +530,10 @@ async fn main_inner() -> Result<()> {
wasm_path,
package,
debug,
} => Ok(build(&package, &wasm_path, debug).await),
} => {
build(&package, &wasm_path, debug).await;
Ok(())
}
Subcommand::Search { query } => {
let prefix = query.map(|q| Import {
plugin: q,
Expand All @@ -497,7 +548,10 @@ async fn main_inner() -> Result<()> {
}
Ok(())
}
Subcommand::Lock { wasm_path } => Ok(lock(&wasm_path).await),
Subcommand::Lock { wasm_path } => {
lock(&wasm_path).await;
Ok(())
}
Subcommand::Feedback { message: feedback } => {
// create a message and send it using reqwest

Expand Down Expand Up @@ -700,7 +754,7 @@ async fn start(wasm_path: &Path, cache: bool) -> Result<()> {
let config = serde_json::to_string(&instance.config).expect("can't fail");

let (mut plugin, subs) =
instantiate_plugin(&mut store, &runner, &nickname, &config).await?;
instantiate_plugin(&mut store, &runner, nickname, &config).await?;

let mut listen_types = vec![];

Expand Down Expand Up @@ -774,12 +828,12 @@ async fn start(wasm_path: &Path, cache: bool) -> Result<()> {
// if we have trapped we should restart the plugin
if let Some(trap) = e.downcast_ref::<Trap>() {
tracing::error!("plugin has crashed, restarting {}", trap);
store = store_strategy.reset(&nickname).await;
store = store_strategy.reset(nickname).await;
host = instantiate_plugin_host(&mut store, &linker, &component)
.await?;
runner = host.litehouse_plugin_plugin().runner();
(plugin, _) =
instantiate_plugin(&mut store, &runner, &nickname, &config)
instantiate_plugin(&mut store, &runner, nickname, &config)
.await
.expect("can't fail");
tracing::debug!("plugin {} restarted", nickname);
Expand Down Expand Up @@ -814,7 +868,7 @@ async fn instantiate_plugin<T: Send>(
) -> Result<(ResourceAny, Vec<Subscription>)> {
let mut store = store.enter().await;
let instance = runner
.call_constructor(&mut store, nickname, Some(&config))
.call_constructor(&mut store, nickname, Some(config))
.await
.map_err(|e| miette!("failed to construct plugin: {:?}", e))?;
let subs = runner
Expand Down Expand Up @@ -846,7 +900,7 @@ async fn set_up_engine(
.async_support(true)
.async_stack_size(1 << 20); // 1MiB

let cache = if cache == true {
let cache = if cache {
let cache = Arc::new(ModuleCache::load().await?.unwrap_or_default());
wasm_config
.enable_incremental_compilation(cache.clone())
Expand Down
2 changes: 1 addition & 1 deletion crates/samsung/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
base64 = "0.21.7"
litehouse-plugin = { version = "0.1.0", path = "../plugin" }
rustls = "0.22.2"
rustls = "0.23.7"
schemars = "0.8.16"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
Expand Down
2 changes: 2 additions & 0 deletions site/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Toaster } from "@/components/ui/sonner";
import "./global.css";
import { RootProvider } from "fumadocs-ui/provider";
import { AxiomWebVitals } from "next-axiom";
Expand All @@ -13,6 +14,7 @@ export default function Layout({ children }: { children: ReactNode }) {
<html lang="en" className={inter.className}>
<AxiomWebVitals />
<body>
<Toaster />
<RootProvider>{children}</RootProvider>
</body>
</html>
Expand Down
Binary file modified site/bun.lockb
Binary file not shown.
Loading

0 comments on commit e030486

Please sign in to comment.