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 ¶
ResolveLibraryPath resolves the native runtime library next to the given CLI entrypoint. It checks, in order:
- The natural platform library name next to the CLI (bundled/flat layout).
- runtime.node next to the CLI (out-of-process wrapper layout).
- prebuilds/<platform>/runtime.node next to the CLI (dev/package layout).
It returns an error when neither exists.
func WaitForCleanup ¶
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 ¶
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 ¶
RunUiAction enters synchronously on the caller's UI thread, not an FFI worker.
func (*Host) Start ¶
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).