jev

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 17 Imported by: 0

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

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

View Source
const KeyCommandTimeout = 10 * time.Second

KeyCommandTimeout bounds jev-api-key-command.

View Source
const KeyEnv = "TYPESAFE_API_KEY"

KeyEnv is the environment variable that holds the key.

Variables

This section is empty.

Functions

func Ask

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

func Check(ctx context.Context, client Client) error

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

func DotEnvHasKey(dir string) bool

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 NewCache

func NewCache() *Cache

NewCache returns an empty cache.

func (*Cache) Answer

func (m *Cache) Answer(c sheet.RemoteCall) (sheet.RemoteAnswer, bool)

Answer returns the answer to c without queuing it.

func (*Cache) Busy

func (m *Cache) Busy() (inFlight, queued int)

Busy returns how many questions are in flight and queued.

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.

func (*Cache) Take

func (m *Cache) Take(n int) []sheet.RemoteCall

Take removes up to n queued questions, counting them as 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.

func Lazy

func Lazy(connect func() (Client, error)) Client

Lazy is a Client that connects the first time a question is asked, with connect, and remembers the result, error included. 012 uses it for jev-api-key-command, so a password manager is only asked for the key when a sheet actually uses JEV.

func NewClient

func NewClient(c Config) (Client, error)

NewClient connects to the service with c. The base URL must be https (or http on this machine), so the key is never sent in the clear or to an address a stray file chose.

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.

Jump to

Keyboard shortcuts

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