Documentation
¶
Overview ¶
Package rpc is api's server of the contract novatrail.api.v1, on Connect v2 (R17, R18, R33): the methods of the contract, its codecs, and the interceptors every request goes through — its line of the log, its time limit and the checks, which run before any handler. A method with no handler answers unimplemented.
Index ¶
- Constants
- func AccountGone(ctx context.Context) error
- func Across[S any, Res proto.Message](ctx context.Context, db store.Beginner, k Key, req Keyed, p Phases[S, Res]) (Res, error)
- func Failed(ctx context.Context, err error) error
- func Keep(refusal error) error
- func Kept(err error) (refusal error, ok bool)
- func Learner(ctx context.Context, db store.Querier, profileID string) (string, error)
- func Malformed(field string) error
- func Mount(mux *http.ServeMux, server *connect.Server)
- func NewServer(d Deps, h Handlers) *connect.Server
- func NoteAICall(ctx context.Context, c AICall)
- func NoteFailure(ctx context.Context, err error)
- func NoteKeyCheck(ctx context.Context, check string)
- func NoteOutcome(ctx context.Context, cause v1.Cause)
- func NotePlatformLimit(ctx context.Context)
- func NoteQuotaSpent(ctx context.Context, spent string)
- func NoteWordsCheck(ctx context.Context, check string)
- func Once[Res proto.Message](ctx context.Context, db store.Beginner, k Key, req Keyed, ...) (Res, error)
- func Refuse(ctx context.Context, code connect.Code, cause v1.Cause, ...) error
- type AICall
- type AIUsage
- type Caller
- type DB
- type Deps
- type Handlers
- type Key
- type Keyed
- type Phases
- type Rights
Constants ¶
const MaxMessage = 11 << 20
MaxMessage is the largest message of a request the server reads, in bytes (R33, R46, R52): 11 MiB. The largest message of the contract is the scan of a parent's form of consent, up to 10 MiB by the contract, with the rest of its request; a stored work is at most 4 MiB, and the crop of a handwriting sheet, at most 1568 px a side in grey of 8 bits, is under 2.5 MB even if PNG compressed nothing. The JSON of debugging writes the scan in base64, a third larger, and is no way to send one.
Variables ¶
This section is empty.
Functions ¶
func AccountGone ¶ added in v0.1.5
AccountGone is the account marked "deleting" or deleted: the checks give it, and so does a handler that finds the account gone after them (R20).
func Across ¶ added in v0.1.17
func Across[S any, Res proto.Message](ctx context.Context, db store.Beginner, k Key, req Keyed, p Phases[S, Res]) (Res, error)
Across runs the phases of a request under its key, as Once runs one action, with the record going between its two transactions: a repeat beside it is CAUSE_STILL_GOING while it calls AI, and one after it gets its answer, with no call. A record going past k.GoingFor is of a request that fell, and the repeat goes again (R17). A refusal of Prepare records nothing, and one of Keep commits what Prepare wrote, as in Once. A refusal of Finish rolls back what Finish wrote, unless it is one of Keep, and either way drops the record going, so that a repeat is checked again.
func Failed ¶ added in v0.1.5
Failed is a check or a handler that did not finish — the sign-in service, the database, the rights. The client sees the code alone; the log names the failure by its kind.
func Keep ¶ added in v0.1.15
Keep is a refusal of act that commits what act wrote before it, with no record of the key: the close of an attempt that the check of a request found abandoned, or on a package the image no longer gives (R07, R10). A repeat is checked again, and finds what was written.
func Kept ¶ added in v0.1.15
Kept is the refusal err keeps, when it is one of Keep: for a request with no key, which commits what it wrote and answers the refusal after it.
func Learner ¶ added in v0.1.15
Learner is the check of consent, for the handlers of the learning requests: the profile's consent is confirmed, or the request is refused "no consent" (R116 spec), and its age is not to be given again, or it is refused "age needed": its country raised its age of digital consent above the one its category was given by (R134 spec). It gives the profile's age category, read with the consent in one query, by which a module and a task suit the learner (R07). It runs after the checks, on the profile they let through: one gone meanwhile is "no profile".
func Malformed ¶ added in v0.1.15
Malformed is a request whose form the contract refuses: invalid_argument with no Refusal, naming the field.
func Mount ¶
Mount puts the server's methods on mux, with the contract's codecs: binary protobuf, and JSON for debugging, written with the protobuf names (R18). The JSON codec of Connect discards unknown fields already. A message larger than MaxMessage is refused resource_exhausted before it is decoded.
func NewServer ¶
NewServer is the server of the contract, with every method registered and every interceptor in its order: the log's line outermost, so that it sees every refusal, then the time limit, then the checks.
func NoteAICall ¶ added in v0.1.17
NoteAICall adds a call of AI to the request's log: a line of each model's usage beside the request's line, or one line of how it ended when no model answered.
func NoteFailure ¶ added in v0.1.8
NoteFailure names, in the request's line of the log, a failure that its answer does not show: a deletion done in the database whose rest the job finishes (R21). The line is an error, with the failure's kind.
func NoteKeyCheck ¶ added in v0.1.42
NoteKeyCheck names, in the request's line of the log, how the check of an own key ended, in a word of logging.KeyChecks: never the key or its provider (R73).
func NoteOutcome ¶ added in v0.1.17
NoteOutcome names, in the request's line of the log, the cause of the outcome of an answer that is no refusal: a call of AI that gave no reading (R46).
func NotePlatformLimit ¶ added in v0.1.23
NotePlatformLimit names, in the request's log, the refusal of the provider by its limit on the platform's key that closed platform AI to all: a line of its own, with the method and no ID, which is the operator's signal (R11).
func NoteQuotaSpent ¶ added in v0.1.23
NoteQuotaSpent names, in the request's log, a call of platform AI that the quota did not hold, by the cap it met, in a word of quota.Spent: a line of its own beside the request's, with the method and no ID, which the unit economics count by the day (R12).
func NoteWordsCheck ¶ added in v0.1.23
NoteWordsCheck names, in the request's line of the log, the check that turned the tutor's words down, in a word of logging.WordsChecks: the learner read the fallback text, and the line says why, without the words (R47).
func Once ¶ added in v0.1.15
func Once[Res proto.Message](ctx context.Context, db store.Beginner, k Key, req Keyed, act func(tx pgx.Tx, accountID string) (Res, store.KeyTarget, error)) (Res, error)
Once runs act under the request's idempotency key, in one transaction with the record of the key, under the account of the caller (R17, R35). The account's row is held FOR NO KEY UPDATE first, so that a repeat beside its first waits for it and finds its record. A record of this method and this request gives its answer, and act does nothing; of another method or request, CAUSE_KEY_ALREADY_USED; one still going, CAUSE_STILL_GOING. Otherwise act acts, and its target is written with the record: the answer, unless it names the submission a repeat reads instead. A refusal of act rolls everything back and is recorded nowhere, but one of Keep commits what act wrote: either way a repeat is checked again. The fingerprint is made before the transaction, so that the account's row and a connection of the pool are held for no more than the hash that checks a repeat. Any error but a refusal of the key is given back as it came, for the caller to answer.
func Refuse ¶
func Refuse(ctx context.Context, code connect.Code, cause v1.Cause, data ...func(*v1.Refusal)) error
Refuse is a refusal of the contract: an error of Connect with its code and the Refusal of its cause as its detail (R17; R97 spec). data, when given, fills the Refusal's data. The message is the cause's name, which holds nothing of the request. The cause goes to the request's line of the log.
Types ¶
type AICall ¶ added in v0.1.17
AICall is a call of AI as the log writes it (R12, R46): how it ended, in one word of logging.AIOutcomes, how long it took, and what each model of it took — no content, no key, no identifier.
type AIUsage ¶ added in v0.1.17
type AIUsage struct {
Provider, Model string
InputTokens, CacheWriteTokens, CacheReadTokens int64
OutputTokens, ThinkingTokens int64
// CostMicroUSD is what it cost by tutor's prices, at its worst when Billed is
// false: its tokens are not its whole bill (R12).
CostMicroUSD int64
Billed bool
// Took is the model's own time, its retries counted in; FirstToken, 0 where
// no text came.
Took, FirstToken time.Duration
Retries int
}
AIUsage is what one call of a model took: its provider and model, its tokens — the input without the cache, written to the cache and read from it, which add up to its input, the output and the thinking (R94) — and what they cost, and how long it took at tutor, how many times its SDK sent it again and when its first text came (R107 tutor).
type Caller ¶
type Caller struct {
// UID is the sign-in service's ID of the token's user.
UID string
// AuthTime is when that user signed in.
AuthTime time.Time
// AccountID is the account of the UID; empty only on CreateAccount, for a UID
// with no account yet.
AccountID string
}
Caller is who a request is from, as the checks found it: the handler reads it with CallerFrom.
type Deps ¶
type Deps struct {
// Signin is the sign-in service (R32).
Signin authn.Port
// DB is the pool of the database api (R29).
DB DB
// Rights is api's rights in OpenFGA (R31).
Rights Rights
// Log is the service's log.
Log *slog.Logger
// RequestTimeout is the longest a request may run (R33).
RequestTimeout time.Duration
}
Deps is what the server runs on.
type Handlers ¶
type Handlers struct {
Account apiv1connect.AccountServiceHandler
Attempt apiv1connect.AttemptServiceHandler
Learning apiv1connect.LearningServiceHandler
Module apiv1connect.ModuleServiceHandler
Package apiv1connect.PackageServiceHandler
Progress apiv1connect.ProgressServiceHandler
Legal apiv1connect.LegalServiceHandler
Work apiv1connect.WorkServiceHandler
}
Handlers are the methods of the contract that api serves. A service left unset answers unimplemented; the tests set the one they need.
type Key ¶ added in v0.1.15
type Key struct {
// Method is the procedure of the contract.
Method string
// Slow is a fingerprint by argon2id, of a request that carries a PIN or an
// age; SHA-256 otherwise (R35).
Slow bool
// Repeat is the answer of a repeat, read in the repeat's transaction, when
// the record does not hold the answer itself: the submission it names, or the
// words the attempt keeps, which the record does not (R47). Nil for a method
// whose records hold their answers.
Repeat func(ctx context.Context, tx pgx.Tx, record store.KeyRecord) (proto.Message, error)
// GoingFor is the longest a request may run (R33): a record going older than
// that is of a request that fell, and a repeat goes again. Zero for a method
// that never records a key going.
GoingFor time.Duration
}
Key is how a method records its requests under their keys.
type Phases ¶ added in v0.1.17
type Phases[S any, Res proto.Message] struct { Prepare func(tx pgx.Tx, accountID string) (S, store.KeyTarget, error) Call func(ctx context.Context, s S) S Finish func(tx pgx.Tx, s S) (Res, store.KeyTarget, error) }
Phases are the three parts of a request under a key that calls AI between two transactions (R17, R46): Prepare checks what the request acts on and names its target, in the transaction that records the key going; Call calls AI outside any transaction, holding no row and no connection; and Finish writes what came of it, in the transaction that records the key done. S is what one part gives the next.