zed

package
v0.1.554 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 18 Imported by: 0

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

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

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

func CompletionHeaders(h http.Header, llmToken string)

CompletionHeaders are what a /completions request carries.

func CompletionURL

func CompletionURL(base string) string

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

func Failure(status int, body []byte) string

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

func GuessProvider(model string) string

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

func ModelsHeaders(h http.Header, llmToken string)

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

func ParseModels(raw []byte) ([]catalog.Model, error)

ParseModels reads /models as the catalog's models, the disabled ones left out.

func PlanName

func PlanName(id string) string

PlanName is a plan's id as a reader would name it.

func ProviderOf

func ProviderOf(raw []byte, model string) string

ProviderOf is the provider /models names for model, "" when it isn't listed.

func ReadLines

func ReadLines(r io.Reader, wrapped bool, fn func(Line) error) error

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 SignInURL

func SignInURL(site string, port int, publicKey, systemID string) string

SignInURL is the page to open for a sign-in coming back to port.

func TokenStale

func TokenStale(status int, h http.Header) bool

TokenStale says a model request was turned away for its LLM token, which a new one would fix.

func UserAgent

func UserAgent() string

UserAgent is Zed's: Zed/<version> (<os>; <arch>), in Rust's names.

func Wire

func Wire(provider string) string

Wire is the API a provider's requests are written in: "anthropic", "responses", "chat" or "gemini"; "" for one magpie can't write.

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.

func (*Failed) Error

func (f *Failed) Error() string

func (*Failed) Status

func (f *Failed) Status() int

Status is the HTTP status a failure stands for: upstream_http_<n> or http_<n>, else what its code names.

type HTTPStatusError

type HTTPStatusError struct {
	StatusCode int
	Body       string
	Header     http.Header
}

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 FetchMe

func FetchMe(ctx context.Context, c *http.Client, base, uid, token, systemID string) (*Me, error)

FetchMe reads the account.

func (*Me) ModelsOff

func (m *Me) ModelsOff(org string) bool

ModelsOff is set when the organization has Zed's models turned off.

func (*Me) Org

func (m *Me) Org() string

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.

func (*Me) PlanID

func (m *Me) PlanID(org string) string

PlanID is the organization's plan, else the account's.

func (*Me) Who

func (m *Me) Who() string

Who names the account: its GitHub login, its username, its name, its id.

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.

type Models

type Models struct {
	Models      []Model  `json:"models"`
	Default     string   `json:"default_model"`
	Recommended []string `json:"recommended_models"`
}

Models is GET /models's reply.

Jump to

Keyboard shortcuts

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