harn-vm 0.10.162

Async bytecode virtual machine for the Harn programming language
Documentation
use super::*;

// The two type blocks are pure literal text and together push this file past
// the source file-length cap, so they live beside it rather than in it.
mod swift_types;
mod typescript_types;

use swift_types::SWIFT_TYPES;
use typescript_types::TYPESCRIPT_TYPES;

pub fn typescript_declarations() -> String {
    typescript_type_block("typescript declarations")
}

fn typescript_type_block(language: &str) -> String {
    let chat_adapters = harn_provider_catalog::ChatApiAdapter::ALL
        .iter()
        .map(|adapter| serde_json::to_string(adapter).expect("chat adapter is serializable"))
        .collect::<Vec<_>>()
        .join(" | ");
    let dialects = llm_config::DataControlDialect::ALL
        .iter()
        .map(|dialect| serde_json::to_string(dialect).expect("dialect is serializable"))
        .collect::<Vec<_>>()
        .join(" | ");
    let operations = llm_config::ModelOperation::ALL
        .iter()
        .map(|operation| format!("\"{}\"", operation.as_str()))
        .collect::<Vec<_>>()
        .join(" | ");
    format!(
        "{}{}",
        generated_header("//", language),
        TYPESCRIPT_TYPES
            .replace("__HARN_CHAT_API_ADAPTERS__", &chat_adapters)
            .replace("__HARN_MODEL_OPERATIONS__", &operations)
            .replace("__HARN_DATA_CONTROL_DIALECTS__", &dialects)
    )
}

pub fn swift_binding() -> Result<String, serde_json::Error> {
    Ok(swift_binding_body())
}

/// Hermetic counterpart of [`swift_binding`] for generating the checked-in
/// `HarnProviderCatalog.swift` artifact. See [`artifact_embedded`].
pub fn swift_binding_embedded(
    _explicit_overlay: Option<&crate::llm_config::ProvidersConfig>,
    _explicit_capabilities: Option<&crate::llm::capabilities::CapabilitiesFile>,
) -> Result<String, serde_json::Error> {
    Ok(swift_binding_body())
}

fn swift_binding_body() -> String {
    let operations = llm_config::ModelOperation::ALL
        .iter()
        .map(|operation| {
            format!(
                "    case {} = \"{}\"",
                operation.as_str(),
                operation.as_str()
            )
        })
        .collect::<Vec<_>>()
        .join("\n");
    format!(
        "{}{}",
        generated_header("//", "swift"),
        SWIFT_TYPES.replace("__HARN_MODEL_OPERATIONS__", &operations)
    )
}

fn generated_header(comment: &str, language: &str) -> String {
    format!(
        "{comment} GENERATED by `{PROVIDER_CATALOG_GENERATOR}` - do not edit by hand.\n{comment} Source: Harn runtime provider catalog schema v{PROVIDER_CATALOG_SCHEMA_VERSION}.\n{comment} Language: {language}.\n\n"
    )
}