pub(crate) mod abi;
pub mod agents;
pub mod capabilities;
pub mod code;
pub mod crypto;
pub(crate) mod dot_segments;
pub mod embedding;
pub mod fs;
pub mod git;
pub mod http;
pub mod llm;
pub mod secrets;
pub mod security;
pub mod session;
pub mod shared;
pub mod skills;
pub mod test;
pub mod url;
pub mod uuid;
use wasmtime::Linker;
use crate::runtime::StoreData;
use crate::{MangledName, PackageDeclaration, Type};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum OptionalPackage {
Agents,
Skills,
}
impl OptionalPackage {
pub const ALL: &'static [OptionalPackage] = &[Self::Agents, Self::Skills];
pub const fn module_name(self) -> &'static str {
match self {
Self::Agents => agents::MODULE_NAME,
Self::Skills => skills::MODULE_NAME,
}
}
pub fn from_module_name(name: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|package| package.module_name() == name)
}
fn package_declaration(self) -> PackageDeclaration {
match self {
Self::Agents => agents::package_declaration(),
Self::Skills => skills::package_declaration(),
}
}
fn install(self, linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
match self {
Self::Agents => agents::install(linker),
Self::Skills => skills::install(linker),
}
}
const fn bit(self) -> u8 {
match self {
Self::Agents => 1,
Self::Skills => 1 << 1,
}
}
}
pub(crate) struct SchemaCheckedCall {
pub callee: &'static str,
pub example: &'static str,
pub checks: &'static str,
pub answerer: &'static str,
pub written_params: &'static str,
pub written_param_count: usize,
pub untyped_hint: &'static str,
pub untyped_hint_for_unknown: &'static str,
pub untyped_hint_for_no_schema: &'static str,
pub untyped_result: Type,
}
impl SchemaCheckedCall {
pub fn schema_argument_ordinal(&self) -> &'static str {
match self.written_param_count {
0 => "first",
1 => "second",
2 => "third",
3 => "fourth",
_ => "last",
}
}
}
pub(crate) fn schema_checked_call(mangled: &MangledName) -> Option<SchemaCheckedCall> {
if llm::declaration::is_checked_call(mangled) {
return Some(SchemaCheckedCall {
callee: "llm.call",
example: "llm.call<Severity>(model, prompt)",
checks: "the model's response",
answerer: "the model",
written_params: "the model and the prompt",
written_param_count: 2,
untyped_hint: "read the `Completion` envelope yourself",
untyped_hint_for_unknown: "read the `Completion` envelope's `ok` and `text` yourself",
untyped_hint_for_no_schema: "parse the `Completion` text yourself",
untyped_result: llm::declaration::untyped_result_type(mangled),
});
}
if agents::declaration::is_checked_run(mangled) {
return Some(SchemaCheckedCall {
callee: "agents.run",
example: "agents.run<Report>(agent, input)",
checks: "the agent's result",
answerer: "the agent",
written_params: "the agent and the input",
written_param_count: 2,
untyped_hint: "read the returned text yourself",
untyped_hint_for_unknown: "read the returned text yourself",
untyped_hint_for_no_schema: "parse the returned text yourself",
untyped_result: Type::String,
});
}
None
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Stdlib {
optional: u8,
}
impl Stdlib {
pub const fn core() -> Self {
Stdlib { optional: 0 }
}
#[must_use]
pub const fn with(self, package: OptionalPackage) -> Self {
Stdlib {
optional: self.optional | package.bit(),
}
}
pub const fn enables(self, package: OptionalPackage) -> bool {
self.optional & package.bit() != 0
}
pub fn optional_packages(self) -> impl Iterator<Item = OptionalPackage> {
OptionalPackage::ALL
.iter()
.copied()
.filter(move |package| self.enables(*package))
}
pub fn contains_module(self, name: &str) -> bool {
OptionalPackage::from_module_name(name).map_or_else(
|| {
core_package_declarations()
.iter()
.any(|defs| defs.package_name == name)
},
|package| self.enables(package),
)
}
pub fn package_declarations(self) -> Vec<PackageDeclaration> {
let mut declarations = core_package_declarations();
declarations.extend(
self.optional_packages()
.map(OptionalPackage::package_declaration),
);
declarations.sort_by(|a, b| a.package_name.cmp(&b.package_name));
declarations
}
pub fn install_host_functions(self, linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
install_core_host_functions(linker)?;
for package in self.optional_packages() {
package.install(linker)?;
}
Ok(())
}
}
pub(crate) fn unavailable_import(module: &str) -> Option<(String, Vec<String>)> {
if module == test::MODULE_NAME {
return Some((
format!("package `{module}` not found"),
vec![
"`submilli:test` is only available to test files run via `submilli build test`; it is not importable from a program run with `submilli run`"
.to_string(),
],
));
}
let package = OptionalPackage::from_module_name(module)?;
Some((
format!("package `{module}` is not available in this harness"),
vec![format!(
"`{}` exists only where the harness running the program provides it",
package.module_name()
)],
))
}
pub fn stdlib_package_declarations() -> Vec<PackageDeclaration> {
Stdlib::core().package_declarations()
}
pub fn install_host_functions(linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
Stdlib::core().install_host_functions(linker)
}
fn core_package_declarations() -> Vec<PackageDeclaration> {
vec![
code::package_declaration(),
crypto::package_declaration(),
embedding::package_declaration(),
fs::package_declaration(),
git::package_declaration(),
http::package_declaration(),
llm::package_declaration(),
secrets::package_declaration(),
security::package_declaration(),
session::package_declaration(),
url::package_declaration(),
uuid::package_declaration(),
]
}
fn install_core_host_functions(linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
code::install(linker)?;
crypto::install(linker)?;
embedding::install(linker)?;
fs::install(linker)?;
git::install(linker)?;
http::install(linker)?;
llm::install(linker)?;
secrets::install(linker)?;
security::install(linker)?;
session::install(linker)?;
url::install(linker)?;
uuid::install(linker)?;
Ok(())
}
pub(crate) fn install_store_bound(
linker: &mut Linker<StoreData>,
store: &mut wasmtime::Store<StoreData>,
) -> wasmtime::Result<()> {
git::class::install(linker, store)
}
#[cfg(test)]
mod optional_tests;
#[cfg(test)]
mod tests {
use super::*;
fn every_set() -> Vec<Stdlib> {
let mut sets = vec![Stdlib::core()];
for package in OptionalPackage::ALL {
let with: Vec<Stdlib> = sets.iter().map(|set| set.with(*package)).collect();
sets.extend(with);
}
sets
}
#[test]
fn declarations_are_sorted_for_every_set() {
for set in every_set() {
let names: Vec<String> = set
.package_declarations()
.into_iter()
.map(|defs| defs.package_name)
.collect();
let mut sorted = names.clone();
sorted.sort();
assert_eq!(names, sorted, "{set:?}");
}
}
#[test]
fn a_set_contains_exactly_its_packages() {
for set in every_set() {
let names: Vec<String> = set
.package_declarations()
.into_iter()
.map(|defs| defs.package_name)
.collect();
for name in &names {
assert!(set.contains_module(name), "{set:?} misses {name}");
}
for package in OptionalPackage::ALL {
assert_eq!(
names.iter().any(|name| name == package.module_name()),
set.enables(*package),
"{set:?} and {package:?}"
);
}
}
}
#[test]
fn core_is_the_global_list() {
let core: Vec<String> = Stdlib::core()
.package_declarations()
.into_iter()
.map(|defs| defs.package_name)
.collect();
let global: Vec<String> = stdlib_package_declarations()
.into_iter()
.map(|defs| defs.package_name)
.collect();
assert_eq!(core, global);
assert!(!Stdlib::core().contains_module(test::MODULE_NAME));
}
}