Documentation
¶
Overview ¶
Package zed speaks to Zed's hosted models, the way the Zed editor does for a Zed Pro (or trial, student, business) account.
Read from Zed's source (github.com/zed-industries/zed, 1.23.0):
- Sign-in (crates/client/src/client.rs, crates/rpc/src/auth.rs): the app makes an RSA-2048 key, listens on a port of 127.0.0.1 and opens zed.dev/native_app_signin?native_app_port=&native_app_public_key=, the key PKCS#1 DER in padded URL-safe base64. The browser comes back to that port, on any path, with user_id and access_token, the token encrypted to the key (RSA-OAEP SHA-256, PKCS#1 v1.5 before that). The pair is sent as "Authorization: <user_id> <access_token>" to cloud.zed.dev/client/* and lasts until a 401.
- GET /client/users/me is the account: its organizations, the plan.
- POST /client/llm_tokens {"organization_id"} trades the pair for a short-lived LLM token, renewed on a 401 or an x-zed-expired-token / x-zed-outdated-token reply.
- GET /models lists what the account may call; POST /completions takes {provider, model, provider_request} — the provider's own request: Anthropic Messages, OpenAI Responses, xAI chat completions, Gemini's generateContent — and streams newline-delimited JSON, each line {"event": <the provider's own stream event>} or a status.
Index ¶
- Constants
- Variables
- func CompletionBody(provider, model, threadID string, request json.RawMessage) []byte
- func CompletionHeaders(h http.Header, llmToken string)
- func CompletionURL(base string) string
- func Decrypt(k *rsa.PrivateKey, ciphertext string) (string, error)
- func Failure(status int, body []byte) string
- func FetchModels(ctx context.Context, c *http.Client, base, llmToken string) ([]byte, http.Header, error)
- func GuessProvider(model string) string
- func LLMToken(ctx context.Context, c *http.Client, base, uid, token, systemID, org string) (string, error)
- func ModelsHeaders(h http.Header, llmToken string)
- func NewKey() (*rsa.PrivateKey, string, error)
- func NewSystemID() string
- func ParseModels(raw []byte) ([]catalog.Model, error)
- func PlanName(id string) string
- func ProviderOf(raw []byte, model string) string
- func ReadLines(r io.Reader, wrapped bool, fn func(Line) error) error
- func SignInURL(site string, port int, publicKey, systemID string) string
- func TokenStale(status int, h http.Header) bool
- func UserAgent() string
- func Wire(provider string) string
- type Failed
- type HTTPStatusError
- type Line
- type Me
- type Model
- type Models
Constants ¶
const ( // SiteURL is where the browser signs in. SiteURL = "https://zed.dev" // CloudURL is the account and model API. CloudURL = "https://cloud.zed.dev" // Version is the Zed release the requests say they come from. Version = "1.23.0" // SucceededURL is where the browser is sent once the callback is read, // as Zed sends it. SucceededURL = SiteURL + "/native_app_signin_succeeded" )
Variables ¶
var ErrNoModels = errors.New("Zed lists no models for this account")
ErrNoModels is a reply that lists nothing callable.
Functions ¶
func CompletionBody ¶
func CompletionBody(provider, model, threadID string, request json.RawMessage) []byte
CompletionBody wraps a provider's own request.
func CompletionHeaders ¶
CompletionHeaders are what a /completions request carries.
func CompletionURL ¶
CompletionURL is where a completion is asked.
func Decrypt ¶
func Decrypt(k *rsa.PrivateKey, ciphertext string) (string, error)
Decrypt reads the access token the callback carried.
func Failure ¶
Failure is the message in an error body: {"code","message"}, else the body itself, else the status's name.
func FetchModels ¶
func FetchModels(ctx context.Context, c *http.Client, base, llmToken string) ([]byte, http.Header, error)
FetchModels reads GET /models as it came.
func GuessProvider ¶
GuessProvider is the provider of a model id that isn't in the list kept.
func LLMToken ¶
func LLMToken(ctx context.Context, c *http.Client, base, uid, token, systemID, org string) (string, error)
LLMToken trades the account's pair for a token the models take.
func ModelsHeaders ¶
ModelsHeaders are what a /models request carries beside its token.
func NewKey ¶
func NewKey() (*rsa.PrivateKey, string, error)
NewKey is the key the access token comes back encrypted to, and its public half as the sign-in page takes it.
func NewSystemID ¶
func NewSystemID() string
NewSystemID is a random UUID v4, what Zed calls the machine.
func ParseModels ¶
ParseModels reads /models as the catalog's models, the disabled ones left out.
func ProviderOf ¶
ProviderOf is the provider /models names for model, "" when it isn't listed.
func ReadLines ¶
ReadLines reads a completion's stream line by line. wrapped is whether the reply said it sends statuses (x-zed-server-supports-status-messages); without, each line is a bare event.
func TokenStale ¶
TokenStale says a model request was turned away for its LLM token, which a new one would fix.
Types ¶
type Failed ¶
type Failed struct {
Code string `json:"code"`
Message string `json:"message"`
RequestID string `json:"request_id"`
RetryAfter float64 `json:"retry_after"`
}
Failed is a failure the stream reports.
type HTTPStatusError ¶
HTTPStatusError is a reply that wasn't a success, its status kept typed.
func (*HTTPStatusError) Error ¶
func (e *HTTPStatusError) Error() string
type Line ¶
type Line struct {
Event json.RawMessage // the provider's own event, when it is one
Ended bool // "stream_ended": the reply is whole
Failed *Failed // the request failed along the way
}
Line is one line of a completion's stream.
type Me ¶
type Me struct {
User struct {
LegacyID int64 `json:"legacy_user_id"`
Username string `json:"username"`
GitHubLogin string `json:"github_login"`
Name string `json:"name"`
} `json:"user"`
Organizations []struct {
ID string `json:"id"`
Name string `json:"name"`
IsPersonal bool `json:"is_personal"`
} `json:"organizations"`
DefaultOrganizationID string `json:"default_organization_id"`
PlansByOrganization map[string]string `json:"plans_by_organization"`
ConfigByOrganization map[string]struct {
ModelsEnabled *bool `json:"is_zed_model_provider_enabled"`
} `json:"configuration_by_organization"`
Plan struct {
V3 string `json:"plan_v3"`
SubscriptionPeriod *struct {
StartedAt string `json:"started_at"`
EndedAt string `json:"ended_at"`
} `json:"subscription_period"`
TrialStartedAt string `json:"trial_started_at"`
AccountTooYoung bool `json:"is_account_too_young"`
HasOverdueInvoice bool `json:"has_overdue_invoices"`
} `json:"plan"`
}
Me is GET /client/users/me, as far as magpie reads it.
func (*Me) Org ¶
Org is the organization the account's models are asked under: its default one when it is among its organizations, else its first, as Zed picks it.
type Model ¶
type Model struct {
Provider string `json:"provider"` // anthropic, open_ai, google, x_ai
ID string `json:"id"`
DisplayName string `json:"display_name"`
MaxTokens int `json:"max_token_count"`
MaxOutputTokens int `json:"max_output_tokens"`
SupportsImages bool `json:"supports_images"`
SupportsThink bool `json:"supports_thinking"`
Efforts []struct {
Value string `json:"value"`
IsDefault bool `json:"is_default"`
} `json:"supported_effort_levels"`
Disabled bool `json:"is_disabled"`
}
Model is one model /models lists.