ffihost

package
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ffihost hosts the Copilot runtime in-process by loading its native library and driving JSON-RPC over the runtime's C ABI.

It pumps opaque LSP Content-Length-framed JSON-RPC bytes across the boundary:

  • client → server frames go to copilot_runtime_connection_write
  • server → client frames arrive on a native callback that feeds a thread-safe receive buffer read by the JSON-RPC client

The existing internal/jsonrpc2 client handles framing unchanged — this is a transport swap, not a new protocol. Host exposes an io.WriteCloser (client → server) and io.ReadCloser (server → client) that plug straight into jsonrpc2.NewClient.

The C ABI (shared with the .NET, Node.js, Python, and Rust SDKs):

uint32 copilot_runtime_host_start(uint8 *argv, size_t argv_len,
                                  uint8 *env, size_t env_len);
bool   copilot_runtime_host_shutdown(uint32 server_id);
uint32 copilot_runtime_connection_open(uint32 server_id, outbound cb,
                                       void *user_data,
                                       uint8 *a, size_t a_len,
                                       uint8 *b, size_t b_len,
                                       uint8 *c, size_t c_len);
bool   copilot_runtime_connection_write(uint32 conn_id, uint8 *bytes, size_t len);
bool   copilot_runtime_connection_close(uint32 conn_id);
// outbound callback:
void   outbound(void *user_data, uint8 *bytes, size_t len);

The native binding uses github.com/ebitengine/purego so the library is loaded at runtime with CGO disabled, preserving the SDK's pure-Go build and cross-compilation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NaturalLibraryName

func NaturalLibraryName() string

NaturalLibraryName is the natural platform shared-library file name for the runtime cdylib — the `.node` file renamed to what a Rust cdylib would be called on this OS. The library is loaded by absolute path, so the on-disk name is ours to choose; this matches the flat name the bundler installs next to the CLI binary and the name the other SDKs use.

func PrebuildsFolder

func PrebuildsFolder() string

PrebuildsFolder returns the napi-rs `<node-platform>-<arch>` folder name the runtime package ships under prebuilds/ (e.g. linux-x64, darwin-arm64, win32-x64, including the musl variant on Alpine). Returns "" for unsupported platforms.

func PrepareForChildProcessWait

func PrepareForChildProcessWait()

PrepareForChildProcessWait repairs signal handlers that the embedded runtime may have replaced before the Go process stops or waits for its own child.

func ProtectChildProcessWait

func ProtectChildProcessWait() func()

ProtectChildProcessWait keeps SIGCHLD compatible with the Go runtime while a child process is being stopped and reaped.

func ResolveLibraryPath

func ResolveLibraryPath(cliEntrypoint string) (string, error)

ResolveLibraryPath resolves the native runtime library next to the given CLI entrypoint. It checks, in order:

  1. The natural platform library name next to the CLI (bundled/flat layout).
  2. runtime.node next to the CLI (out-of-process wrapper layout).
  3. prebuilds/<platform>/runtime.node next to the CLI (dev/package layout).

It returns an error when neither exists.

func WaitForCleanup

func WaitForCleanup(timeout time.Duration) bool

WaitForCleanup waits for all deferred connection cleanup to finish. In-process test harnesses use this before releasing host-owned fixtures.

Types

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host hosts the Copilot runtime in-process via its native C ABI.

Construct with Create, call Start to open the FFI connection, wire Writer/Reader into jsonrpc2.NewClient, and call Dispose to tear everything down.

func Create

func Create(runtimeEntrypoint, cliEntrypoint string, environment map[string]string, args []string) (*Host, error)

Create resolves the native library and prepares the host. environment and args contain the effective per-host environment and SDK-managed runtime options.

func (*Host) ConfigureUiThread

func (h *Host) ConfigureUiThread(enabled bool) error

ConfigureUiThread registers this SDK connection's native UI scheduling capability.

func (*Host) Dispose

func (h *Host) Dispose()

Dispose closes the receive side immediately and releases native resources after connectionClose confirms that outbound callbacks are quiescent.

func (*Host) Reader

func (h *Host) Reader() io.ReadCloser

Reader returns the server → client frame source (plug into jsonrpc2 as stdout).

func (*Host) RunUiAction

func (h *Host) RunUiAction(actionID uint32) error

RunUiAction enters synchronously on the caller's UI thread, not an FFI worker.

func (*Host) Start

func (h *Host) Start() error

Start opens the FFI connection. Native startup may block, so callers should run it off any latency-sensitive goroutine.

func (*Host) Writer

func (h *Host) Writer() io.WriteCloser

Writer returns the client → server frame sink (plug into jsonrpc2 as stdin).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL