natshost

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 14 Imported by: ? 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 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

View Source
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

func New(o Options) (*Host, error)

New opens the instance and builds the authenticated router the NATS bridge dispatches to. Nothing is served until Serve.

func (*Host) Accounts

func (h *Host) Accounts() []string

Accounts returns the IDs of the accounts currently served.

func (*Host) Close

func (h *Host) Close() error

Close stops serving, cancels and joins every active run, then closes the instance (memory, sessions, usage). It is idempotent.

func (*Host) Handler

func (h *Host) Handler() http.Handler

Handler returns the authenticated router the bridge dispatches to, for a host that also wants to serve it over HTTP, or for tests.

func (*Host) Instance

func (h *Host) Instance() *instance.Instance

Instance returns the hosted instance (its store for metrics, its tools).

func (*Host) Serve

func (h *Host) Serve(accountID string, nc *nats.Conn) error

Serve answers conversation requests arriving on nc, pinned to accountID. Serving an account twice is a no-op.

func (*Host) Stop

func (h *Host) Stop(accountID string)

Stop stops serving an account and cancels its active runs. The connection stays open; the gateway owns it.

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.

Jump to

Keyboard shortcuts

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