Documentation
¶
Overview ¶
Package jev answers the spreadsheet's JEV functions with TypeSafe's hosted model. It caches answers by question, so recalculating never asks twice, and hands queued questions to the UI to send in the background. It is only used when an API key is configured.
Index ¶
- Constants
- func Ask(ctx context.Context, client Client, c sheet.RemoteCall) sheet.RemoteAnswer
- func Check(ctx context.Context, client Client) error
- func Describe(c sheet.RemoteCall, a sheet.RemoteAnswer) string
- func DotEnvHasKey(dir string) bool
- func Key(c sheet.RemoteCall) string
- func ResolveKey(ctx context.Context, s KeySources) (key, from string, err error)
- type Cache
- func (m *Cache) Answer(c sheet.RemoteCall) (sheet.RemoteAnswer, bool)
- func (m *Cache) Busy() (inFlight, queued int)
- func (m *Cache) Forget(calls []sheet.RemoteCall)
- func (m *Cache) Lookup(c sheet.RemoteCall) (sheet.RemoteAnswer, bool)
- func (m *Cache) Store(c sheet.RemoteCall, a sheet.RemoteAnswer)
- func (m *Cache) Take(n int) []sheet.RemoteCall
- type Client
- type Config
- type KeySources
Constants ¶
const CheckQuestion = "Is this arithmetic correct?"
CheckQuestion is the question Check asks: fixed, trivial and about nothing the user owns, so checking a key costs one small request and sends no data.
const KeyCommandTimeout = 10 * time.Second
KeyCommandTimeout bounds jev-api-key-command.
const KeyEnv = "TYPESAFE_API_KEY"
KeyEnv is the environment variable that holds the key.
Variables ¶
This section is empty.
Functions ¶
func Ask ¶
func Ask(ctx context.Context, client Client, c sheet.RemoteCall) sheet.RemoteAnswer
Ask sends one question and converts the reply. Failures are answers too, with Failed set, so they're shown rather than retried in a loop.
func Check ¶
Check asks the service CheckQuestion with client, to see that its key works. The error says why not, in words for the context line, and never holds the key.
func Describe ¶
func Describe(c sheet.RemoteCall, a sheet.RemoteAnswer) string
Describe summarizes an answer for the context line.
func DotEnvHasKey ¶
DotEnvHasKey reports whether dir/.env sets TYPESAFE_API_KEY. It reads only the variable names: the key itself is never read into memory past the scanner's line, kept or shown. 012 no longer reads .env files (a .env beside a downloaded sheet could send the key elsewhere); this is only to suggest `012 config set-key` to people who used one.
func Key ¶
func Key(c sheet.RemoteCall) string
Key identifies a question by its content; see sheet.RemoteCall.Key.
func ResolveKey ¶
func ResolveKey(ctx context.Context, s KeySources) (key, from string, err error)
ResolveKey finds the API key. from says where it came from: "TYPESAFE_API_KEY", the store's name, or "jev-api-key-command". With no key anywhere it returns "", "", nil. A store or command that fails is skipped, and its error is returned only if no later source has a key.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache holds answers and the questions still to ask. It implements sheet.RemoteSource.
func (*Cache) Answer ¶
func (m *Cache) Answer(c sheet.RemoteCall) (sheet.RemoteAnswer, bool)
Answer returns the answer to c without queuing it.
func (*Cache) Forget ¶
func (m *Cache) Forget(calls []sheet.RemoteCall)
Forget drops answers so the questions are asked again.
func (*Cache) Lookup ¶
func (m *Cache) Lookup(c sheet.RemoteCall) (sheet.RemoteAnswer, bool)
Lookup returns the answer to c, queuing c if it hasn't been asked.
func (*Cache) Store ¶
func (m *Cache) Store(c sheet.RemoteCall, a sheet.RemoteAnswer)
Store records the answer to a question that was in flight.
type Client ¶
type Client interface {
SystemOne(context.Context, typesafe.SystemOneRequest) (*typesafe.SystemOneResponse, error)
}
Client is the part of the TypeSafe SDK used here, so tests can fake it.
type Config ¶
type Config struct {
APIKey, BaseURL, Model string
}
Config is how to reach the service. The key comes from ResolveKey; the base URL and model from the config file or environment (internal/config), never from files next to a sheet.
type KeySources ¶
type KeySources struct {
Getenv func(string) string
Store keyring.Store // nil skips the credential store
Command string // jev-api-key-command; empty skips it
Timeout time.Duration // for Command; KeyCommandTimeout when zero
// contains filtered or unexported fields
}
KeySources are the places ResolveKey looks.