Skip to main content

harn_hostlib/
lib.rs

1//! `harn-hostlib`: opt-in host builtins for code intelligence (tree-sitter,
2//! repo scanning, deterministic indexing) and tool execution (search, file
3//! I/O, git, process lifecycle, file watcher).
4//!
5//! This crate is the Rust home of two classes of optional host capabilities:
6//!
7//! 1. **Code intelligence** — `ast/`, `code_index/`, `scanner/`, `fs_watch/`.
8//! 2. **Deterministic tools** — `tools/` (search, fs, git, process).
9//!
10//! These don't belong inside `harn-vm` — pulling tree-sitter grammars,
11//! ripgrep, and `notify` into the VM would balloon the footprint of every
12//! pipeline that doesn't index host code. Instead, this crate exposes a
13//! single [`HostlibCapability`] trait. Embedders such as `harn-cli`'s ACP
14//! server) compose the modules they need via [`HostlibRegistry`] and wire
15//! the resulting builtins into the VM through [`harn_vm::Vm::register_builtin`]
16//! / [`harn_vm::Vm::register_async_builtin`].
17//!
18//! ## Status
19//!
20//! The AST, scanner, code-index, and deterministic-tool surfaces are
21//! implemented. `fs_watch/` still registers its public contract with
22//! [`HostlibError::Unimplemented`] handlers. Module names, method names,
23//! and JSON schemas under `schemas/` are the source of truth for hostlib
24//! request/response compatibility, so they must stay stable while module
25//! bodies evolve.
26
27#![deny(rust_2018_idioms)]
28#![warn(missing_docs)]
29
30#[cfg(feature = "ast")]
31pub mod ast;
32#[cfg(feature = "ast")]
33pub mod code_index;
34pub mod error;
35pub mod fs;
36pub mod fs_snapshot;
37pub mod fs_watch;
38pub mod process;
39pub mod sandbox;
40pub mod scanner;
41pub mod schemas;
42pub mod secret_store;
43pub mod tools;
44
45mod registry;
46
47pub use error::HostlibError;
48pub use registry::{BuiltinRegistry, HostlibCapability, HostlibRegistry, RegisteredBuiltin};
49
50/// Convenience: build a `HostlibRegistry` populated with every capability
51/// the crate ships, register them on the supplied VM, and return the
52/// registry so callers can introspect (e.g. for schema-drift tests).
53///
54/// This is the canonical entry point for embedders that want the full
55/// hostlib surface; pick-and-choose embedders should construct
56/// [`HostlibRegistry`] directly.
57pub fn install_default(vm: &mut harn_vm::Vm) -> HostlibRegistry {
58    let mut registry = HostlibRegistry::new();
59    // The code-intelligence capabilities (`ast` + `code_index`) are only
60    // compiled when the `ast` feature is on. Lean clients that omit it get
61    // the deterministic tool surface without tree-sitter or any grammar.
62    #[cfg(feature = "ast")]
63    {
64        let code_index = code_index::CodeIndexCapability::new();
65        registry = registry
66            .with(ast::AstCapabilityWithCodeIndex::new(code_index.shared()))
67            .with(code_index);
68    }
69    registry = registry
70        .with(scanner::ScannerCapability)
71        .with(fs::FsCapability)
72        .with(fs_snapshot::FsSnapshotCapability)
73        .with(fs_watch::FsWatchCapability)
74        .with(tools::ToolsCapability)
75        .with(secret_store::SecretStoreCapability);
76    registry.register_into_vm(vm);
77    registry
78}