Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/lsp"

Package lsp checks files with language servers: after an agent edits a file, a Pool asks the server for its language what is wrong with it, so the agent learns of a compile error in the same tool result as the edit (docs/design/language-servers.md).

Servers run as processes in the session’s env.Environment, under the same sandbox as the agent’s commands. A server starts the first time a file of its language is checked, once per workspace root, and runs until the pool closes. A check is bounded by a budget: a server still loading, or one too slow, reports nothing rather than hold up the edit.

const DefaultBudget = 3 * time.Second

DefaultBudget is how long a check waits for a server by default: long enough for a warm server to answer, short enough not to hold up an edit when it is still loading.

type Diagnostic struct {
// Line and Column are where it starts, from 1; Column counts UTF-16
// code units, as the protocol does.
Line, Column int
Severity Severity
Message string
// Source is what found it, such as "compiler", if the server says.
Source string
}

Diagnostic is one problem a server found in a file.

type Option func(*Pool)

Option configures a Pool.

func WithBudget(d time.Duration) Option

WithBudget sets how long a check waits for a server; DefaultBudget by default.

func WithLogger(l *slog.Logger) Option

WithLogger sets where the pool logs servers starting and stopping.

type Pool struct {
// contains filtered or unexported fields
}

Pool checks files with the servers it was given, starting each on first use. It is safe for concurrent use; checks of files that one server serves run one at a time.

func NewPool(servers []Server, opts ...Option) (*Pool, error)

NewPool returns a pool of servers. Each needs a name, a command with an absolute path, and at least one extension, which no other server claims.

func (p *Pool) Check(ctx context.Context, e env.Environment, root, rel string, text []byte) (Report, error)

Check asks the server for rel’s kind what is wrong with text, rel’s content, in the workspace root, starting the server in e if it isn’t running. rel is relative to root, which is the workspace’s absolute path as e’s processes see it. Check waits at most the pool’s budget, or until ctx is done, whose error it then returns; a server’s failure is never an error, only a report of nothing checked.

Example

A pool checks a Go file the agent just wrote with the server for Go, started in the session’s environment on first use.

gopls := lsp.Server{
Name: "gopls",
Command: env.Cmd{Path: "/usr/local/bin/gopls"},
Languages: map[string]string{".go": "go"},
}
pool, err := lsp.NewPool([]lsp.Server{gopls})
if err != nil {
fmt.Println(err)
return
}
defer pool.Close(context.Background())
var e env.Environment = &fakeEnv{script: script{diagnose: undefinedFoo}} // the session's environment
r, err := pool.Check(context.Background(), e, "/work", "add.go", []byte("package add\n\nvar x = foo\n"))
if err != nil {
fmt.Println(err)
return
}
for _, d := range r.Errors() {
fmt.Printf("%s: %d:%d %s\n", r.Server, d.Line, d.Column, d.Message)
}
// Output: gopls: 3:9 undefined: foo

Output:

gopls: 3:9 undefined: foo
func (p *Pool) Close(ctx context.Context) error

Close stops every server, waiting for each, and keeps any later check from starting one. ctx bounds asking each server to shut down; one that doesn’t is killed.

func (p *Pool) Release(ctx context.Context, root string) error

Release stops the servers of the workspace root, waiting for each, and forgets them: a later check there starts afresh, with nothing counted against it. A host calls it before closing an environment it checked files in, since a server that environment ends hasn’t stopped by itself. ctx bounds asking each server to shut down; one that doesn’t is killed.

func (p *Pool) Serves(name string) bool

Serves reports whether a server checks files named like name.

type Report struct {
// Server names the server that checked the file; it is empty when no
// server checks files of its kind, or one can't run.
Server string
// Checked says the server answered within the budget. When it didn't,
// Diagnostics is empty and says nothing about the file.
Checked bool
Diagnostics []Diagnostic
// Problem says, once, that the server can't check files any more:
// it failed to start, or stopped twice.
Problem string
}

Report is what a check found.

func (r Report) Errors() []Diagnostic

Errors returns the diagnostics of error severity.

type Server struct {
// Name names the server in reports, such as "gopls".
Name string
// Command starts it, speaking the protocol on its standard streams.
// Its Dir is relative to the workspace root; its output is kept as
// the pool reads it.
Command env.Cmd
// Languages maps the file extensions it checks, such as ".go", to the
// protocol's language identifiers, such as "go".
Languages map[string]string
}

Server is a language server a pool may start.

type Severity int

Severity is how serious a diagnostic is, as the protocol numbers it.

const (
SeverityError Severity = 1
SeverityWarning Severity = 2
SeverityInformation Severity = 3
SeverityHint Severity = 4
)

The protocol’s severities.