ds4

    MoonBit native bindings for the local DwarfStar 4 engine

    ds4
    ffi
    native
    Download zip
    Version
    0.1.1
    License
    MIT
    Last updated
    5 months ago
    Downloads
    82

    #ds4.mbt

    MoonBit native bindings for DwarfStar 4.

    DS4 source files are vendored directly in this package.

    The MoonBit package builds the DS4 sources directly as native stubs: ds4_stub.c, ds4.c, and ds4_metal.m on macOS.

    #Model weights

    This package does not vendor or download model weights.

    Download a compatible DwarfStar 4 GGUF model using the upstream DS4 instructions:

    https://github.com/antirez/ds4#model-weights

    Then pass the downloaded .gguf file explicitly with -m /path/to/model.gguf.

    Validate the package:

    moon test --target native

    Run the DS4 command:

    moon build cmd/ds4 --target native _build/native/debug/build/cmd/ds4/ds4.exe -m /path/to/model.gguf -p "Write a short haiku about MoonBit"

    Pass the GGUF model explicitly with -m /path/to/model.gguf; the CLI does not use a built-in default model path.

    Like the upstream ./ds4 CLI, cmd/ds4 samples by default with --temp 1, --top-p 1, and a time-based seed. For deterministic comparisons, pass the same seed to both commands:

    _build/native/debug/build/cmd/ds4/ds4.exe -m /path/to/model.gguf --seed 42 -p "Write a short haiku about MoonBit"

    Run the MoonBit coding micro agent:

    moon build cmd/ds4mbt --target native _build/native/debug/build/cmd/ds4mbt/ds4mbt.exe -m /path/to/model.gguf -p "Add focused tests for the parser"

    cmd/ds4mbt embeds the MoonBit agent guide from ~/Documents/moonbit-agent-guide/moonbit-agent-guide in its system prompt and executes four tool blocks emitted by the model: read, write, edit, and bash. Use --cwd /path/to/workspace to point the tools at a different MoonBit codebase.

    Ds4Error

    pub(all) suberror Ds4Error {
    OpenFailed(Int)
    SessionCreateFailed(Int, String)
    OperationFailed(String, Int, String)
    } derive(Eq,
    Debug
    )

    Backend

    Backend::context_memory_estimate

    fn Backend::context_memory_estimate(self : Backend, ctx_size : Int) -> ContextMemory

    Backend::cpu

    fn Backend::cpu() -> Backend

    Backend::cuda

    fn Backend::cuda() -> Backend

    Backend::default

    fn Backend::default() -> Backend

    Backend::metal

    fn Backend::metal() -> Backend

    Backend::name

    fn Backend::name(self : Backend) -> String

    ContextMemory

    pub(all) struct ContextMemory {
    total_bytes : UInt64
    raw_bytes : UInt64
    compressed_bytes : UInt64
    scratch_bytes : UInt64
    prefill_cap : UInt
    raw_cap : UInt
    comp_cap : UInt
    } derive(Eq,
    Debug
    )

    Engine

    type Engine

    Engine::chat_append_assistant_prefix

    fn Engine::chat_append_assistant_prefix(self : Engine, tokens : Tokens, think_mode? : ThinkMode) -> Unit

    Engine::chat_append_max_effort_prefix

    fn Engine::chat_append_max_effort_prefix(self : Engine, tokens : Tokens) -> Unit

    Engine::chat_append_message

    fn Engine::chat_append_message(self : Engine, tokens : Tokens, role? : StringView, content : StringView) -> Unit

    Engine::chat_begin

    fn Engine::chat_begin(self : Engine, tokens : Tokens) -> Unit

    Engine::create_session

    fn Engine::create_session(self : Engine, ctx_size : Int) -> Session raise Ds4Error

    Engine::encode_chat_prompt

    fn Engine::encode_chat_prompt(self : Engine, system? : StringView, prompt : StringView, think_mode? : ThinkMode) -> Tokens

    Engine::has_mtp

    fn Engine::has_mtp(self : Engine) -> Bool

    Engine::mtp_draft_tokens

    fn Engine::mtp_draft_tokens(self : Engine) -> Int

    Engine::open

    fn Engine::open(config : EngineConfig) -> Engine raise Ds4Error

    Engine::routed_quant_bits

    fn Engine::routed_quant_bits(self : Engine) -> Int

    Engine::summary

    fn Engine::summary(self : Engine) -> Unit

    Engine::token_eos

    fn Engine::token_eos(self : Engine) -> Int

    Engine::token_text

    fn Engine::token_text(self : Engine, token : Int) -> String

    Engine::tokenize_rendered_chat

    fn Engine::tokenize_rendered_chat(self : Engine, text : StringView) -> Tokens

    Engine::tokenize_text

    fn Engine::tokenize_text(self : Engine, text : StringView) -> Tokens

    EngineConfig

    pub(all) struct EngineConfig {
    model_path : String
    mtp_path : String
    backend : Backend
    n_threads : Int
    mtp_draft_tokens : Int
    mtp_margin : Float
    directional_steering_file : String
    directional_steering_attn : Float
    directional_steering_ffn : Float
    warm_weights : Bool
    quality : Bool
    inspect_only : Bool
    } derive(Eq,
    Debug
    )

    EngineConfig::new

    fn EngineConfig::new(model_path : String, backend? : Backend, mtp_path? : String, n_threads? : Int, mtp_draft_tokens? : Int, mtp_margin? : Float, directional_steering_file? : String, directional_steering_attn? : Float, directional_steering_ffn? : Float, warm_weights? : Bool, quality? : Bool, inspect_only? : Bool) -> EngineConfig

    Sampler

    type Sampler

    Sampler::new

    fn Sampler::new(seed : UInt64) -> Sampler

    Session

    type Session

    Session::argmax

    fn Session::argmax(self : Session) -> Int

    Session::argmax_excluding

    fn Session::argmax_excluding(self : Session, excluded_id : Int) -> Int

    Session::ctx

    fn Session::ctx(self : Session) -> Int

    Session::eval

    fn Session::eval(self : Session, token : Int) -> Unit raise Ds4Error

    Session::invalidate

    fn Session::invalidate(self : Session) -> Unit

    Session::pos

    fn Session::pos(self : Session) -> Int

    Session::rewind

    fn Session::rewind(self : Session, pos : Int) -> Unit

    Session::sample

    fn Session::sample(self : Session, sampler : Sampler, temperature? : Float, top_k? : Int, top_p? : Float, min_p? : Float) -> Int

    Session::sync

    fn Session::sync(self : Session, prompt : Tokens) -> Unit raise Ds4Error

    Session::tokens

    fn Session::tokens(self : Session) -> Tokens

    ThinkMode

    type ThinkMode derive(Eq,
    Debug
    )

    ThinkMode::enabled

    fn ThinkMode::enabled(self : ThinkMode) -> Bool

    ThinkMode::for_context

    fn ThinkMode::for_context(self : ThinkMode, ctx_size : Int) -> ThinkMode

    ThinkMode::high

    fn ThinkMode::high() -> ThinkMode

    ThinkMode::max

    fn ThinkMode::max() -> ThinkMode

    ThinkMode::max_min_context

    fn ThinkMode::max_min_context() -> UInt

    ThinkMode::max_prefix

    fn ThinkMode::max_prefix() -> String

    ThinkMode::name

    fn ThinkMode::name(self : ThinkMode) -> String

    ThinkMode::none

    fn ThinkMode::none() -> ThinkMode

    Tokens

    type Tokens

    Tokens::copy

    fn Tokens::copy(self : Tokens) -> Tokens

    Tokens::get

    fn Tokens::get(self : Tokens, index : Int) -> Int?

    Tokens::length

    fn Tokens::length(self : Tokens) -> Int

    Tokens::new

    fn Tokens::new() -> Tokens

    Tokens::op_get

    fn Tokens::op_get(self : Tokens, index : Int) -> Int

    Tokens::push

    fn Tokens::push(self : Tokens, token : Int) -> Unit

    Tokens::starts_with

    fn Tokens::starts_with(self : Tokens, prefix : Tokens) -> Bool

    Tokens::to_fixed_array

    fn Tokens::to_fixed_array(self : Tokens) -> FixedArray[Int]

    Source Files