pub struct Kernel { /* private fields */ }Expand description
The Kernel (核) — executes kaish code.
This is the primary interface for running kaish commands. It owns all the runtime state: variables, tools, VFS, jobs, and persistence.
Implementations§
Source§impl Kernel
impl Kernel
Sourcepub fn new(config: KernelConfig) -> Result<Self>
pub fn new(config: KernelConfig) -> Result<Self>
Create a new kernel with the given configuration.
Sourcepub fn with_backend(
backend: Arc<dyn KernelBackend>,
config: KernelConfig,
configure_vfs: impl FnOnce(&mut VfsRouter),
) -> Result<Self>
pub fn with_backend( backend: Arc<dyn KernelBackend>, config: KernelConfig, configure_vfs: impl FnOnce(&mut VfsRouter), ) -> Result<Self>
Create a kernel with a custom backend and /v/* virtual path support.
This is the constructor for embedding kaish in other systems that provide their own storage backend (e.g., CRDT-backed storage in kaijutsu).
A VirtualOverlayBackend routes paths automatically:
/v/*→ Internal VFS (JobFs at/v/jobs, MemoryFs at/v/blobs)- Everything else → Your custom backend
The optional configure_vfs closure lets you add additional virtual mounts
(e.g., /v/docs for CRDT blocks) after the built-in mounts are set up.
Note: The config’s vfs_mode is ignored — all non-/v/* path routing
is handled by your custom backend. The config is only used for name, cwd,
skip_validation, and interactive.
§Example
// Simple: default /v/* mounts only
let kernel = Kernel::with_backend(backend, config, |_| {})?;
// With custom mounts
let kernel = Kernel::with_backend(backend, config, |vfs| {
vfs.mount_arc("/v/docs", docs_fs);
vfs.mount_arc("/v/g", git_fs);
})?;Sourcepub fn init_terminal(&mut self)
pub fn init_terminal(&mut self)
Initialize terminal state for interactive job control.
Call this after kernel creation when running as an interactive REPL and stdin is a TTY. Sets up process groups and signal handling.
Sourcepub async fn execute(&self, input: &str) -> Result<ExecResult>
pub async fn execute(&self, input: &str) -> Result<ExecResult>
Execute kaish source code.
Returns the result of the last statement executed.
Sourcepub async fn execute_streaming(
&self,
input: &str,
on_output: &mut dyn FnMut(&ExecResult),
) -> Result<ExecResult>
pub async fn execute_streaming( &self, input: &str, on_output: &mut dyn FnMut(&ExecResult), ) -> Result<ExecResult>
Execute kaish source code with a per-statement callback.
Each statement’s result is passed to on_output as it completes,
allowing callers to flush output incrementally (e.g., print builtin
output immediately rather than buffering until the script finishes).
External commands in interactive mode already stream to the terminal
via Stdio::inherit(), so the callback mainly handles builtins.
Sourcepub async fn set_positional(
&self,
script_name: impl Into<String>,
args: Vec<String>,
)
pub async fn set_positional( &self, script_name: impl Into<String>, args: Vec<String>, )
Set positional parameters ($0 script name and $1-$9 args).
Sourcepub async fn last_result(&self) -> ExecResult
pub async fn last_result(&self) -> ExecResult
Get the last result ($?).
Sourcepub async fn has_function(&self, name: &str) -> bool
pub async fn has_function(&self, name: &str) -> bool
Check if a user-defined function exists.
Sourcepub fn tool_schemas(&self) -> Vec<ToolSchema>
pub fn tool_schemas(&self) -> Vec<ToolSchema>
Get available tool schemas.
Sourcepub fn jobs(&self) -> Arc<JobManager>
pub fn jobs(&self) -> Arc<JobManager>
Get job manager.
Trait Implementations§
Source§impl CommandDispatcher for Kernel
impl CommandDispatcher for Kernel
Source§fn dispatch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
cmd: &'life1 Command,
ctx: &'life2 mut ExecContext,
) -> Pin<Box<dyn Future<Output = Result<ExecResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn dispatch<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
cmd: &'life1 Command,
ctx: &'life2 mut ExecContext,
) -> Pin<Box<dyn Future<Output = Result<ExecResult>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Dispatch a command through the Kernel’s full resolution chain.
This is the single path for all command execution when called from the pipeline runner. It provides the full dispatch chain: user tools → builtins → .kai scripts → external commands → backend tools.