use crate::error::RuntimeError;
use crate::loaded::ExtensionId;
use crate::runtime::ExtensionRuntime;
const COMPOSER_VERSIONS: &[&str] = &["0.2.0", "0.1.0"];
const COMPOSER_IFACE_BASE: &str = "greentic:dw-composer/composer";
impl ExtensionRuntime {
pub fn invoke_composer_metadata(&self, ext_id: &str) -> Result<String, RuntimeError> {
let loaded = self
.loaded()
.get(&ExtensionId(ext_id.to_string()))
.cloned()
.ok_or_else(|| RuntimeError::NotFound(ext_id.to_string()))?;
let (mut store, instance) = loaded
.build_store_and_instance(
self.engine(),
self.host_overrides().clone(),
&crate::host_ports::HostCallContext::default(),
)
.map_err(RuntimeError::Wasmtime)?;
let (iface_idx, iface_name, _version) = resolve_dw_composer_iface(&mut store, &instance)?;
let func_idx = instance
.get_export_index(&mut store, Some(&iface_idx), "metadata")
.ok_or_else(|| {
RuntimeError::Wasmtime(anyhow::anyhow!(
"interface '{iface_name}' does not export 'metadata'"
))
})?;
let func = instance
.get_typed_func::<(), (String,)>(&mut store, &func_idx)
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
let (result,) = func
.call(&mut store, ())
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
Ok(result)
}
pub fn invoke_composer_compose(
&self,
ext_id: &str,
intent_json: &str,
) -> Result<Result<String, crate::types::HostExtensionError>, RuntimeError> {
let loaded = self
.loaded()
.get(&ExtensionId(ext_id.to_string()))
.cloned()
.ok_or_else(|| RuntimeError::NotFound(ext_id.to_string()))?;
let (mut store, instance) = loaded
.build_store_and_instance(
self.engine(),
self.host_overrides().clone(),
&crate::host_ports::HostCallContext::default(),
)
.map_err(RuntimeError::Wasmtime)?;
let (iface_idx, iface_name, version) = resolve_dw_composer_iface(&mut store, &instance)?;
let func_idx = instance
.get_export_index(&mut store, Some(&iface_idx), "compose")
.ok_or_else(|| {
RuntimeError::Wasmtime(anyhow::anyhow!(
"interface '{iface_name}' does not export 'compose'"
))
})?;
let result: Result<String, crate::types::HostExtensionError> = if version == "0.2.0" {
use crate::host_bindings::dw_composer_v02::greentic::extension_base0_2_0::types::ExtensionError as E2;
let func = instance
.get_typed_func::<(String,), (Result<String, E2>,)>(&mut store, &func_idx)
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
let (r,) = func
.call(&mut store, (intent_json.to_string(),))
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
r.map_err(crate::ext_error::from_composer_v02)
} else {
let func = instance
.get_typed_func::<(String,), (Result<String, String>,)>(&mut store, &func_idx)
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
let (r,) = func
.call(&mut store, (intent_json.to_string(),))
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
r.map_err(crate::ext_error::from_composer_v01)
};
Ok(result)
}
pub fn invoke_composer_validate(
&self,
ext_id: &str,
manifest_json: &str,
) -> Result<String, RuntimeError> {
let loaded = self
.loaded()
.get(&ExtensionId(ext_id.to_string()))
.cloned()
.ok_or_else(|| RuntimeError::NotFound(ext_id.to_string()))?;
let (mut store, instance) = loaded
.build_store_and_instance(
self.engine(),
self.host_overrides().clone(),
&crate::host_ports::HostCallContext::default(),
)
.map_err(RuntimeError::Wasmtime)?;
let (iface_idx, iface_name, _version) = resolve_dw_composer_iface(&mut store, &instance)?;
let func_idx = instance
.get_export_index(&mut store, Some(&iface_idx), "validate")
.ok_or_else(|| {
RuntimeError::Wasmtime(anyhow::anyhow!(
"interface '{iface_name}' does not export 'validate'"
))
})?;
let func = instance
.get_typed_func::<(String,), (String,)>(&mut store, &func_idx)
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
let (result,) = func
.call(&mut store, (manifest_json.to_string(),))
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
Ok(result)
}
pub fn invoke_composer_templates(&self, ext_id: &str) -> Result<String, RuntimeError> {
let loaded = self
.loaded()
.get(&ExtensionId(ext_id.to_string()))
.cloned()
.ok_or_else(|| RuntimeError::NotFound(ext_id.to_string()))?;
let (mut store, instance) = loaded
.build_store_and_instance(
self.engine(),
self.host_overrides().clone(),
&crate::host_ports::HostCallContext::default(),
)
.map_err(RuntimeError::Wasmtime)?;
let (iface_idx, iface_name, _version) = resolve_dw_composer_iface(&mut store, &instance)?;
let func_idx = instance
.get_export_index(&mut store, Some(&iface_idx), "templates")
.ok_or_else(|| {
RuntimeError::Wasmtime(anyhow::anyhow!(
"interface '{iface_name}' does not export 'templates'"
))
})?;
let func = instance
.get_typed_func::<(), (String,)>(&mut store, &func_idx)
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
let (result,) = func
.call(&mut store, ())
.map_err(|e| RuntimeError::Wasmtime(e.into()))?;
Ok(result)
}
}
fn resolve_dw_composer_iface(
store: &mut wasmtime::Store<crate::host_state::HostState>,
instance: &wasmtime::component::Instance,
) -> Result<
(
wasmtime::component::ComponentExportIndex,
String,
&'static str,
),
RuntimeError,
> {
crate::runtime::resolve_iface_versions(store, instance, COMPOSER_IFACE_BASE, COMPOSER_VERSIONS)
}