docsite

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 24 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 docsite serves a built documentation site (Astro Starlight, or any static site laid out the same way) from an fs.FS embedded in a product binary, behind the product's own sign-in, and reads the site's pages into sections the assistant can search and cite.

A Site is loaded once from the built output (Load): every page under the tree is parsed, its main content split at the headings Starlight gave ids, and the text of each section kept with its heading path, the page's path and the anchor the browser scrolls to. The sections carry a keyword index (Search) that needs no model or credential, so the documentation is searchable the moment the process starts; package ai/docsearch adds semantic retrieval over the same sections and turns them into assistant tools.

Handler serves the same tree to browsers. Pages are gated by a session cookie the SPA establishes with the user's bearer (POST {prefix}/_session), because a link in a chat reply or a bookmark carries no bearer; a page request without a session is redirected to the SPA's documentation gate, which signs the user in if needed and comes back.

The Starlight side of the contract (base path, link rewriting, theme) is @bboxsys/ui/starlight; docs/DOCSITE.md describes both halves.

Index

Constants

View Source
const (
	// SessionPath is the route under the prefix that establishes and ends
	// the documentation session: POST with the user's bearer sets the
	// cookie, DELETE clears it, GET reports whether one is set.
	SessionPath = "_session"
	// DefaultCookieName is the session cookie's name.
	DefaultCookieName = "docs_session"
	// DefaultMaxAge bounds a session, however long the bearer lasts.
	DefaultMaxAge = 24 * time.Hour
	// DefaultMaxSessions bounds the session table.
	DefaultMaxSessions = 10000
	// AccountHeader is the header the SPA sends the account id in.
	AccountHeader = "X-Blackbox-AccountID"
)
View Source
const SnippetRunes = 320

SnippetRunes is the length of a Hit's snippet.

Variables

This section is empty.

Functions

func Excerpt

func Excerpt(text, query string) string

Excerpt returns a window of text around the first term of query found in it, or its start: the snippet Search attaches to a hit, for callers ranking sections another way.

func Handler

func Handler(site *Site, o HandlerOptions) (http.Handler, error)

Handler serves site under o.Prefix to browsers that hold a session.

A session is established by the SPA: POST {prefix}/_session with the user's bearer (and the account header the kit's request helper sends) answers 204 and sets an HttpOnly, SameSite=Lax cookie scoped to the prefix; DELETE clears it. Page requests without a session are redirected to SignIn(next); asset requests answer 401. Pages are served as a built static site: directory indexes, a redirect onto the trailing slash, the site's 404.html for a missing page, immutable caching under _astro/, and precompressed variants when the build produced them.

Types

type HandlerOptions

type HandlerOptions struct {
	// Prefix is the path the site is mounted at, without a trailing slash
	// ("/docs"). Required; the Starlight build must use the same base.
	Prefix string
	// Auth decides which accounts' tokens are accepted when a session is
	// established. Required unless Validate is set.
	Auth TrustedAccounts
	// Users fills Identity.Roles for Authorize. A NATS user JWT carries no
	// roles, so without a lookup Roles is empty and Authorize can only
	// decide by account and user. Optional.
	Users UserLookup
	// Validate replaces the default bearer validation (signature, trusted
	// issuer, lifetime, and the account header when present). Optional.
	Validate func(r *http.Request, token string) (*Identity, error)
	// Authorize decides whether an authenticated identity may read the
	// site; nil admits every user of a trusted account. It runs when the
	// session is established and on every page request.
	Authorize func(r *http.Request, id *Identity) bool
	// SignIn returns where a browser without a session is sent for a page
	// request, given the path (and query) it asked for: normally the SPA's
	// documentation gate, "/session/docs?next=...". Nil answers 401.
	SignIn func(next string) string
	// CookieName defaults to DefaultCookieName.
	CookieName string
	// MaxAge bounds a session's life; zero means DefaultMaxAge.
	MaxAge time.Duration
	// MaxSessions bounds the session table; zero means DefaultMaxSessions.
	MaxSessions int
	// Logger receives operational lines; nil logs as docsite/{project}.
	Logger *logger.Logger
}

HandlerOptions configure Handler.

type Hit

type Hit struct {
	Section *Section
	// Score is the BM25 score; only hits above zero are returned.
	Score float64
	// Snippet is a window of the section text around the first query term
	// found, or the start of the text.
	Snippet string
}

Hit is one keyword search result.

type Identity

type Identity struct {
	AccountID string
	// UserID is the user's NATS public key (the token subject).
	UserID string
	Roles  []string
	// Expires is when the session ends; the token's expiry, bounded by
	// HandlerOptions.MaxAge.
	Expires time.Time
}

Identity is who a documentation session belongs to.

type Options

type Options struct {
	// Project identifies the documentation set: the product's id
	// ("acme"), [a-z0-9_-]+. Required.
	Project string
	// Title names the documentation to readers and the assistant ("the
	// Acme documentation"); empty takes the site title from the root
	// page's <title>.
	Title string
	// Version is the product version the documentation describes, if the
	// host knows it; it is reported with the site, never required.
	Version string
}

Options configure Load.

type Page

type Page struct {
	// Path is the page's path relative to the site root, with a trailing
	// slash for a directory index ("planning/retirement/") and "" for the
	// root page.
	Path        string
	Title       string
	Description string
	Sections    []*Section
	Hash        string
}

Page is one HTML page of the site.

type Section

type Section struct {
	// ID is the section's stable id: the page path plus "#anchor", or the
	// page path alone for the text above the first heading. The root
	// page's sections are "/" and "/#anchor".
	ID string
	// Page is the owning page's Path.
	Page string
	// Anchor is the heading's id, the fragment a browser scrolls to; "" for
	// the page's lead section.
	Anchor string
	// Level is the heading level, 1 for the page title.
	Level int
	// Title is the heading text.
	Title string
	// Headings is the heading path from the page title down to Title.
	Headings []string
	// Text is the section's content as plain text: paragraphs separated by
	// blank lines, code blocks fenced, list items and table rows one per
	// line.
	Text string
	// Hash identifies the section's content.
	Hash string
}

Section is one heading of a page and the text under it, up to the next heading of any level.

func (*Section) HeadingPath

func (sec *Section) HeadingPath() string

HeadingPath renders the heading path as "Page title > Heading > Sub".

func (*Section) URL

func (sec *Section) URL(siteURL string) string

URL returns the browser URL of a section under siteURL, the absolute URL the site is served at (no trailing slash).

type Site

type Site struct {
	Project  string
	Title    string
	Version  string
	Pages    []*Page
	Sections []*Section
	// Revision identifies the content: a hash over every section, so two
	// builds of the same sources produce the same revision.
	Revision string
	// contains filtered or unexported fields
}

Site is a loaded documentation site: its pages in path order, every section of every page in reading order, and a keyword index over them.

func Load

func Load(fsys fs.FS, o Options) (*Site, error)

Load reads a built site from fsys: every *.html file except 404.html, in path order. A page without a <main> element is skipped. The result is immutable; load again for a new build.

func (*Site) FS

func (s *Site) FS() fs.FS

FS returns the built site the pages were read from, for Handler.

func (*Site) Page

func (s *Site) Page(p string) *Page

Page returns a page by path, or nil. A trailing "#anchor" is ignored and a missing trailing slash on a directory index is tolerated.

func (*Site) Search

func (s *Site) Search(query string, n int) []Hit

Search ranks sections by keyword relevance (BM25 over the section text, with the heading path counted twice) and returns at most n hits, best first. Commands, configuration keys, and paths are indexed both whole ("ai.allow_writes", "api/v1/queue") and by their parts, so an exact identifier finds its section and so does a word from it. It needs no embedding credential.

func (*Site) Section

func (s *Site) Section(id string) *Section

Section returns a section by id, or nil.

type TrustedAccounts

type TrustedAccounts interface {
	IsPublicKeyTrusted(publicKey string) bool
}

TrustedAccounts reports whether a token issuer (an account public key) is one this host accepts tokens from. *admin/client.Auth satisfies it.

type UserLookup

type UserLookup interface {
	GetUserByPublicKey(accountID, publicKey string) (*user.User, error)
}

UserLookup finds a user by account and public key, for Identity.Roles. *admin.Store and any admin/client.IdentityStore satisfy it.

Jump to

Keyboard shortcuts

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