Documentation
¶
Overview ¶
Package plugin runs a model family in this process: a Rust core compiled to wasm32-wasip1 and from there to a Go package by wasm2go, called directly with typed envelopes rather than over a network.
A plugin is pure. It reaches nothing but this host: every upstream request it wants made, the host makes (hz_open), with the key it names resolved here so no key ever enters the plugin's memory, and every byte of its answer it hands to the host (hz_reply, hz_write, hz_end), which hands them to the caller at the caller's pace. One instance holds the plugin's state for every request: its own executor steps them all, and returns to the host whenever none can move (hz_poll). The host calls it again when one can.
The ABI ¶
Every name carries the hz_ prefix, so nothing a plugin links (wasi-libc's malloc, free, read and write) can collide with it.
The plugin exports:
hz_alloc(len) -> ptr bytes for the host to write into hz_free(ptr, len) hz_init(cfg, len) -> 0 | code its configuration, once, first hz_begin(ask, len, body, len) -> call | code hz_poll(now_ms) -> wake_ms | -1 step every call that can move hz_cancel(call) the caller left
The host provides, in import module "hanzo":
hz_open(call, meta, len, body, len) -> handle | code hz_head(handle, buf, cap) -> n | Pending hz_read(handle, buf, cap) -> n | 0 at the end | Pending | Cut hz_close(handle) hz_reply(call, meta, len) -> 0 | Gone | Invalid hz_write(call, buf, len) -> n | Pending | Gone hz_end(call, meta, len) -> 0 | Gone | Invalid hz_now() -> ms
Pointers are into the plugin's memory and are read or written only during the call that passes them: a buffer hz_begin is given is the host's again once hz_begin returns. Every meta is JSON (Ask, Open, Head, Reply, End); every body is bytes. hz_head writes nothing and returns the length it needs when the head is longer than cap. hz_write takes what fits and returns how much: Pending is a full buffer, the caller's pace, and the plugin writes the rest after the host wakes it.
hz_poll is called after hz_begin, after anything a plugin waits on moves (an upstream's head or bytes, a caller who read, a caller who left) and at the wake time it returned. It returns once nothing can move.
Index ¶
- Constants
- func Register(p Plugin)
- func Sandbox[W wasi](w W, name string) W
- type Answer
- type Ask
- type End
- type Guest
- type Head
- type Host
- type Imports
- func (i Imports[M]) Hz_close(_ M, handle int32)
- func (i Imports[M]) Hz_end(m M, call, meta, n int32) int32
- func (i Imports[M]) Hz_head(m M, handle, buf, capacity int32) int32
- func (i Imports[M]) Hz_now(_ M) int64
- func (i Imports[M]) Hz_open(m M, call, meta, metaLen, body, bodyLen int32) int32
- func (i Imports[M]) Hz_read(m M, handle, buf, capacity int32) int32
- func (i Imports[M]) Hz_reply(m M, call, meta, n int32) int32
- func (i Imports[M]) Hz_write(m M, call, buf, n int32) int32
- type Open
- type Plugin
- type Reply
- type Tells
- type Transport
Constants ¶
const ( // Pending: not yet. The host calls hz_poll when it is. Pending int32 = -1 // Cut: a body that broke before its end. Cut int32 = -2 // Gone: the caller left; stop the call. Gone int32 = -3 // Invalid: an argument the host cannot read. Invalid int32 = -4 // Denied: a key this plugin may not use, or one that holds nothing. Denied int32 = -5 )
Codes a call into the host returns besides a length or a handle.
const Scheme = "plugin"
Scheme is the address scheme a plugin answers at: a family whose address is plugin://zen is the plugin registered as "zen", in this process. The address decides the transport, so the code that relays to a family is the same code whichever answers it.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(p Plugin)
Register makes p answer plugin://<p.Name>, in place of any it replaces. The instance starts on its first request.
func Sandbox ¶
func Sandbox[W wasi](w W, name string) W
Sandbox closes a plugin's WASI to everything but its clocks, its randomness and its own output. It gets no environment, because the keys live there; no files, no sockets and no processes, because everything it reaches it reaches through hz_open. What it writes is logged a line at a time under its name.
zen.NewWithWASI(plugin.Sandbox(zen.DefaultWASI(), "zen"), plugin.Imports[*zen.Module]{H: h})
Types ¶
type Answer ¶
type Answer struct {
Reply Reply
Body io.ReadCloser
// End is what the plugin stated after the body, valid once Body has
// returned io.EOF.
End func() End
}
Answer is a plugin's answer to one request: its head, its body as the plugin writes it, and what it states at the end.
type Ask ¶
type Ask struct {
Method string `json:"method"`
// Path is the route with its query: /v1/chat/completions, /v1/models.
Path string `json:"path"`
// Org is who the call is for (X-Org-Id).
Org string `json:"org,omitempty"`
// RequestID is the id the caller was answered under (X-Request-Id).
RequestID string `json:"request_id,omitempty"`
// Capabilities are the org's published capabilities a decision attaches
// (X-Org-Capabilities), as JSON.
Capabilities json.RawMessage `json:"capabilities,omitempty"`
// Capture asks Kai for each question's pooled state (X-Capture).
Capture bool `json:"capture,omitempty"`
// Spend is what the caller's plan holds for the family's paid rungs, in USD
// (X-Hanzo-Spend).
Spend string `json:"spend,omitempty"`
// Fronted says ai fronts the call and settles it (X-Hanzo-Fronted-By).
Fronted bool `json:"fronted,omitempty"`
Headers [][2]string `json:"headers,omitempty"`
}
Ask is one request a plugin answers, beside its body. The typed fields are what the families and Kai read off a request; Headers carries the rest (Accept, anthropic-version, …). No credential is ever in it.
type End ¶
End is what an answer states after its body (hz_end): what the request cost Hanzo upstream (Cogs, every arm it ran), what a committed plan answer's paid rung cost (Cost), both in USD, and the tells as they stood at the end.
type Guest ¶
type Guest interface {
Memory() []byte
HzAlloc(n int32) int32
HzFree(ptr, n int32)
HzInit(cfg, n int32) int32
HzBegin(ask, askLen, body, bodyLen int32) int32
HzPoll(now int64) int64
HzCancel(call int32)
}
Guest is a plugin's exports as wasm2go generates them: its memory and its hz_ functions. A generated *Module is one.
type Head ¶
type Head struct {
Status int `json:"status"`
Headers [][2]string `json:"headers,omitempty"`
Error string `json:"error,omitempty"`
}
Head is an upstream answer's head as hz_head writes it. A request that never reached an answer is a head with status 0 and the error.
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host runs one plugin: one instance, stepped by one goroutine. Requests reach it with Do; the plugin's upstream requests and answers reach the host through Imports.
type Imports ¶
Imports are the host's functions as a wasm2go'd plugin of type M calls them. One generic value serves every plugin, because every plugin imports the same hz_ functions:
zen.New(plugin.Imports[*zen.Module]{H: h})
type Open ¶
type Open struct {
Method string `json:"method"`
URL string `json:"url"`
Headers [][2]string `json:"headers,omitempty"`
Key string `json:"key,omitempty"`
Auth string `json:"auth,omitempty"`
// Timeout bounds the whole exchange, body included; Head the wait for its
// head. Milliseconds; zero is the host's default.
Timeout int64 `json:"timeout_ms,omitempty"`
Head int64 `json:"head_ms,omitempty"`
}
Open is an upstream request a plugin asks the host to make (hz_open). Key is the NAME of the key the request goes under; the host resolves it and writes it into the request as Auth says: "bearer" (the default), "x-api-key", or "header:<Name>".
type Plugin ¶
type Plugin struct {
// Name is how an address reaches it: plugin://<name>.
Name string
// New builds a fresh instance over the host's imports:
//
// New: func(h *plugin.Host) plugin.Guest {
// return zen.New(plugin.Imports[*zen.Module]{H: h})
// }
New func(*Host) Guest
// Config is hz_init's argument: the plugin's own configuration (a catalog,
// a family), never a key.
Config []byte
// Keys resolves a key the plugin names to its value; "" is a key it may not
// use or one that holds nothing. The host writes the value into the upstream
// request; the plugin never sees it.
Keys func(name string) string
// Reply bounds the wait for a call's head; zero waits as long as the
// caller's context does.
Reply time.Duration
// Client makes the plugin's upstream requests; nil is a default.
Client *http.Client
// Now is the clock hz_now and hz_poll read; nil is the wall clock.
Now func() time.Time
}
Plugin is one model family run in this process.
type Reply ¶
type Reply struct {
Status int `json:"status"`
Headers [][2]string `json:"headers,omitempty"`
Tells
Trailers []string `json:"trailers,omitempty"`
}
Reply is an answer's head (hz_reply). Trailers names what its End will state, declared now as an HTTP answer declares them.
type Tells ¶
type Tells struct {
Served string `json:"served,omitempty"`
Arm string `json:"arm,omitempty"`
Provider string `json:"provider,omitempty"`
Free bool `json:"free,omitempty"`
Failover []string `json:"failover,omitempty"`
Bound string `json:"bound,omitempty"`
}
Tells is what a family says about an answer beyond its body: the Hanzo SKU that answered, the upstream arm and the provider that ran it, whether that arm bills nothing, each arm that failed first (`<upstream> (<provider>): <why>`), and on a committed plan answer the most it can cost, in USD.
type Transport ¶
type Transport struct{}
Transport answers plugin:// requests in process, as an http.RoundTripper: the request becomes an Ask, the plugin's answer an http.Response whose tells are the X-Hanzo-* headers a family service sends and whose End arrives as its trailers. Register it on an http.Transport:
t.RegisterProtocol(plugin.Scheme, plugin.Transport{})