#OpenCode Server SDK for MoonBit

    totto2727/opencode-sdk/server starts and stops a native opencode serve process and returns its announced HTTP base URL.

    Consumer prerequisites, dependencies, imports, and the common SDK usage are documented in the root Setup and Usage.

    #Package role

    • Starts opencode serve with hostname, port, timeout, and JSON configuration options on the native target.
    • Accepts supported legacy and v2 readiness output, then exposes the validated HTTP URL through Server::url.
    • Reports startup, timeout, malformed-output, exit, and cleanup failures as typed ServerError values.
    • Owns process waiting and temporary log cleanup through idempotent Server::close.

    #Usage

    See the managed Server health example for the complete create, announced-URL, and close lifecycle.

    #API

    This README was generated from the share-artifact skill and README template.

    ServerError

    pub suberror ServerError {
    StartFailed(String)
    StartTimeout(timeout_ms~ : Int, output~ : String)
    MalformedOutput(line~ : String)
    ServerExited(code~ : Int, output~ : String)
    CleanupFailed(String)
    } derive(
    Debug
    )

    Failures that can occur while starting or stopping an OpenCode server.

    Server

    pub struct Server {
    url : String
    process :
    Process

    log_dir : String
    closed : Bool
    }

    A running OpenCode server owned by the task group passed at creation.

    Server::close

    async fn Server::close(self : Server) -> Unit raise ServerError

    Stop the server, wait for its process, and remove its temporary logs.

    Calling close again after a successful close has no effect.

    Server::url

    fn Server::url(self : Server) -> String

    Return the HTTP base URL announced by the OpenCode server.

    ServerOptions

    pub struct ServerOptions {
    hostname : String
    port : Int
    timeout_ms : Int
    config :
    Json

    } derive(
    Debug
    )

    Options for starting an OpenCode server.

    When port is zero, OpenCode selects an available port. timeout_ms bounds the readiness wait, and config is serialized to OPENCODE_CONFIG_CONTENT.

    ServerOptions::ServerOptions

    fn ServerOptions::ServerOptions(hostname? : String, port? : Int, timeout_ms? : Int, config? :
    Json
    ) -> ServerOptions

    Construct server options.

    create_opencode_server

    async fn[X] create_opencode_server(group :
    TaskGroup
    [X], options? : ServerOptions, command? : String, extra_env? : Map[String, String]) -> Server raise ServerError

    Start an OpenCode server in group.

    The task group must outlive the returned server. The process is started as opencode serve, receives OPENCODE_CONFIG_CONTENT, and is hard-cancelled when startup fails or close is called.

    The returned Server::url is the HTTP base URL announced by OpenCode.

    Example:
    async fn example {
    @async.with_task_group() <| group => {
    let server = @opencode.create_opencode_server(group)
    println(server.url())
    server.close()
    }
    }