Documentation
¶
Overview ¶
Package natshost serves an AI instance's conversation API over NATS (package natsapi) instead of a listener, for a product service whose assistant is reached through the admin console's gateway (admin.AssistantGateway) rather than by the browser directly. The host adds what a relayed conversation needs on top of ai/instance:
- request identity: the gateway names each request in RequestHeader, scoped to the caller's account and user, so a second request with the same id is refused (409) and a cancel names exactly one run;
- cancellation: POST {prefix}/requests/{id}/cancel ends the run, also when it arrives before the request itself was dispatched (NATS handlers run concurrently), and waits briefly for the run to end;
- a bounded run duration (MaxRunDuration);
- account removal (Stop) and an orderly Close that cancels and joins every active run before the instance's stores close.
Model credentials never travel over NATS: the host resolves them in process through Options.Instance.Credentials, as any instance does.
host, err := natshost.New(natshost.Options{
Instance: instance.Options{Name: "inventory_service", Dir: dir, Credentials: resolver,
OpenAPI: spec, Handler: srv.Srv.Handler, ToolFilter: include, ToolPrefix: "inventory_"},
Auth: auth, AdminAccountID: adminAccount,
})
gateway.Register(admin.AssistantService{ID: "inventory", Name: "Inventory", Serve: host.Serve, Stop: host.Stop})
defer host.Close()
Index ¶
Constants ¶
const ( // RequestHeader names a conversation request (a UUID) so it can be // cancelled. The gateway sets it; a request without it gets one. RequestHeader = "X-Blackbox-AI-Request" // CancelPathSuffix is appended to the mount prefix for the cancel // route: POST {prefix}/requests/{id}/cancel. CancelPathSuffix = "/requests/{id}/cancel" // DefaultMaxRunDuration bounds one request, however long the model // keeps calling tools. DefaultMaxRunDuration = 30 * time.Minute // CancelRetention is how long an early cancellation (one arriving // before its request) is remembered. CancelRetention = time.Minute // CancelWait is how long a cancel request waits for the run to end // before answering. CancelWait = 5 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host is one instance served over NATS.
func New ¶
New opens the instance and builds the authenticated router the NATS bridge dispatches to. Nothing is served until Serve.
func (*Host) Close ¶
Close stops serving, cancels and joins every active run, then closes the instance (memory, sessions, usage). It is idempotent.
func (*Host) Handler ¶
Handler returns the authenticated router the bridge dispatches to, for a host that also wants to serve it over HTTP, or for tests.
type Options ¶
type Options struct {
// Instance configures the hosted instance. Name is also the natsapi
// service name (api.{Name}.{METHOD}.{resource}), so it must be a valid
// natsapi service name such as "inventory_service".
Instance instance.Options
// Bearer authenticates every conversation route. Required unless Auth
// is set, in which case it defaults to Auth.Bearer.
Bearer func(http.HandlerFunc) http.HandlerFunc
// RequireAdmin wraps the administrator-only route (document ingestion).
// Defaults to Auth.RequireAdmin(AdminAccountID) when Auth is set,
// otherwise to Bearer.
RequireAdmin func(http.HandlerFunc) http.HandlerFunc
// Auth is the service's auth, the usual source of Bearer and
// RequireAdmin. Optional when both are given.
Auth *admin.Auth
// AdminAccountID is the platform admin account, for RequireAdmin.
AdminAccountID string
// MountPrefix is where the conversation routes live on the NATS side.
// Default ai.DefaultMountPrefix; the gateway relays to the default.
MountPrefix string
// MaxRunDuration bounds one request. Zero means DefaultMaxRunDuration.
MaxRunDuration time.Duration
// NATSAPI tunes the bridge (Version, Description, QueueGroup, chunking);
// Service and Handler are set by the host.
NATSAPI natsapi.Options
// Logger receives operational lines; nil uses Instance.Logger.
Logger *logger.Logger
}
Options configures a Host.