modelapi

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package modelapi is the model API codeaf serves each run of a program it carries (internal/delegate): an OpenAI-style chat-completions endpoint on this machine, opened by one token, whose every call goes through codeaf's own model funnel — refused at the run's ceiling, priced, logged, and written down as one turn of the program's conversation with codeaf.

IT LIVES UNDER internal/provider BECAUSE THAT IS THE ONLY PLACE A MODEL ROUTE MAY BE SPELLED (funnel_law_test.go). A program in codeaf's own tree builds its request URL with ChatURL rather than appending the route itself, so the route is written once, here, and the law holds for the program's code as for everything else.

Index

Constants

View Source
const DefaultKeepalive = 15 * time.Second

DefaultKeepalive is how often a waiting answer says it is still coming. It is well inside the two-minute idle timeout a program's HTTP client keeps, so a model thinking for half an hour never looks like a dead connection.

Variables

This section is empty.

Functions

func ChatURL

func ChatURL(base string) string

ChatURL is the chat-completions endpoint of an API whose base URL is base, the way every OpenAI client joins them: one slash between.

func Resolve

func Resolve(asked string, serves func(model string) bool, seats ...string) (model, served string)

Resolve is THE ONE PLACE a program's model becomes the model that answers it. asked is the id as the program wrote it, serves answers whether one of this person's services can take a call on a model (nil answers yes for every model), and seats are the models a call falls to, in order — the run's work seat first.

model is what the call goes out as; served is set exactly when it is not the model that was asked for, and it is what delegate.Turn.Served carries. A seat that cannot be served either is passed over for the next; when nothing can be served the call goes out as asked, so the funnel's own road answers it and says why, rather than this function inventing a refusal.

Types

type Charge

type Charge struct {
	// Model is the model that answered, in the funnel's own spelling.
	Model     string
	TokensIn  int
	TokensOut int
	Cached    int
	CostUSD   float64
	// Spent is the run's metered total with this charge in it. It only rises,
	// and charges are told one at a time in the order they were metered, so a
	// bank that keeps the latest figure is always right.
	Spent float64
	// Late says the charge is a receipt the provider fetched after its call had
	// already returned — a stream cut before its usage block.
	Late bool
}

Charge is one priced answer, as the funnel billed it.

type Completer

type Completer interface {
	CompleteWithMessages(ctx context.Context, messages []ai.Message, options ...ai.Option) (*ai.Response, error)
}

Completer is the funnel one call goes out through. It is provider.Client's own method, and internal/session's Completer is the same one method, so a run hands this the conversation's completer as it is and a test hands it a script.

type Config

type Config struct {
	// TaskDir is the task's record folder, where the conversation log is kept
	// (delegate.ConversationFile). Empty keeps no log.
	TaskDir string
	// CompleterFor answers the funnel a call on model goes out through. Nil is
	// a run with no model road: every call is answered with the sentence that
	// says so, and none is made.
	CompleterFor func(model string) Completer
	// Serves answers whether one of this person's services can take a call on
	// model — the account pool's own test, handed in (internal/session's
	// ServesModel). Nil answers yes for every model.
	Serves func(model string) bool
	// Seat is the run's own work seat: the model a call falls to when the one
	// the program asked for cannot be served on this machine ([Resolve]).
	Seat string
	// Ceiling is the run's dollar ceiling, zero for none.
	Ceiling float64
	// ModelPrice is the catalog's published input and output price per token.
	// Nil means a local or custom model with no known price.
	ModelPrice func(model string) (input, output float64, known bool)
	// Bank is told every charge as it is metered. It is called one charge at a
	// time and must not block on the program.
	Bank func(Charge)
	// Unbilled is told a call the provider charged for and could put no figure
	// on — a cut stream whose receipt never came.
	Unbilled func(model string)
	// Settling is told, once, when [Server.Close] begins to wait for the
	// receipts still owed on this run's cut calls, with how many there are, so
	// a person watching the run can be told why its end takes a moment. Nil
	// says nothing; nothing is told when nothing is owed.
	Settling func(owed int)
	// Role is the lane role the calls ride: an unattended leaf when nobody is
	// reading, which is a run's worker, and an attended one for a shell run a
	// person is watching. Empty is unattended.
	Role lanes.Role
	// AuthKeySource answers the safe credential source for the served model.
	// It is added to 401/403 failures, never the key itself. Nil says nothing.
	AuthKeySource func(model string) string
	// Node names the work the calls belong to in the model-call log — the
	// program's name — so `codeaf logs --node <name>` reads one program's calls.
	// Their tag is `task`, the word every call made inside a piece of work
	// carries (internal/session's purposeTask).
	Node string
	// Keepalive overrides [DefaultKeepalive], for a test that must not wait
	// fifteen seconds to see one.
	Keepalive time.Duration
}

Config is one run's API.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is one run's model API.

func Open

func Open(config Config) (*Server, error)

Open starts one run's API on an OS-chosen 127.0.0.1 port and mints its token.

127.0.0.1 AND NEVER 0.0.0.0, for the file door's reason: the token is the only thing between a caller and the person's model account, and a listener on every interface hands that account to anybody on the same network who can guess a port.

func (*Server) API

func (s *Server) API() delegate.ModelAPI

API is the address and the token a program is started with (delegate.ChildEnv). After Server.Close the token is empty: a closed API has nothing to hand out.

func (*Server) Close

func (s *Server) Close() error

Close ends the API: THE TOKEN DIES WITH THE RUN. The token is forgotten, every call in flight is ended, the listener and every connection are closed, and the calls that were running are given [closeWait] to write their last record. A grandchild the program left behind can no longer spend.

AND THE RUN'S BOOKS ARE CLOSED WITH EVERY RECEIPT IN THEM. A call cut in the middle — ended just now by this very close, or by the program's own stop a moment before — is priced by a receipt the provider fetches in the background about twenty seconds later. Close waits for every receipt still owed, for at most [receiptWait], so the Server.Spent a caller reads after it is the run's whole total and every charge has reached Config.Bank while the caller's books are still open. It measured: each of the three stopped runs of 2026-09-23 lost exactly that call from its task, its run total and its conversation's books, and only the machine's ledger heard of it.

func (*Server) RefusedAtCeiling

func (s *Server) RefusedAtCeiling() int

RefusedAtCeiling is how many calls were refused because the run's dollar ceiling had been reached.

IT IS WHAT TELLS THE CEILING FROM A CRASH. A program that budgets by its own sum of each answer's cost can be refused before that sum reaches the ceiling it was given — codeaf's meter counts every answer the funnel was charged for, retries included — and senior-dev then ends its run as `crashed`. The run was stopped by the limit a person set, and the worker reads this to say so (internal/run's DelegateWorker).

func (*Server) Spent

func (s *Server) Spent() float64

Spent is the run's metered total so far.

Jump to

Keyboard shortcuts

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