Text generation over interchangeable backends, behind a two-method interface.
Two backends ship with draft: session providers (Claude, Codex, Copilot,
Cursor and more), each driven through its own CLI in headless mode using the
user's already-authenticated session — no API token — and Ollama, the local
HTTP server, used offline or when no session CLI is available. Callers depend
only on Engine, so the pipeline is identical regardless of which one runs.
go get github.com/sebastienrousseau/draft@latestimport "github.com/sebastienrousseau/draft/engine"Implementing the seam is the whole contract:
package main
import (
"context"
"fmt"
"strings"
"github.com/sebastienrousseau/draft/engine"
)
// echoEngine satisfies engine.Engine. Accept interfaces, return structs.
type echoEngine struct{ maxChars int }
func (echoEngine) Name() string { return "echo" }
func (e echoEngine) Generate(ctx context.Context, req engine.Request) (engine.Result, error) {
if err := ctx.Err(); err != nil {
return engine.Result{}, err // honour cancellation before doing work
}
text := strings.ToUpper(req.Prompt)
if e.maxChars > 0 && len(text) > e.maxChars {
// Truncated tells the pipeline to continue generation rather than
// save a mid-sentence article.
return engine.Result{Text: text[:e.maxChars], Truncated: true}, nil
}
return engine.Result{Text: text}, nil
}
func main() {
var eng engine.Engine = echoEngine{maxChars: 12}
res, err := eng.Generate(context.Background(), engine.Request{
Kind: engine.KindWrite, // KindExtract, KindWrite or KindEdit
Prompt: "write something grounded",
})
if err != nil {
fmt.Println("generate failed:", err)
return
}
fmt.Printf("%s: %q truncated=%v\n", eng.Name(), res.Text, res.Truncated)
// Output: echo: "WRITE SOMETH" truncated=true
}Inspecting what is installed, without generating anything:
package main
import (
"fmt"
"github.com/sebastienrousseau/draft/engine"
)
func main() {
for _, name := range engine.ProviderNames() { // preference order
p, ok := engine.LookupProvider(name)
if !ok {
continue
}
status := "stable"
if p.Experimental {
status = "experimental"
}
fmt.Printf("%-13s %s\n", p.Name, status)
}
// false skips experimental providers, matching auto mode's default.
if p, ok := engine.FirstAvailableProvider(false); ok {
fmt.Println("auto would pick:", p.Name)
} else {
fmt.Println("no session CLI installed; auto falls back to Ollama")
}
}| Symbol | Signature | Purpose |
|---|---|---|
Engine |
interface{ Name() string; Generate(context.Context, Request) (Result, error) } |
The seam every backend implements |
Request |
struct{ Kind; Prompt string; Temperature float64; NumPredict int; OnChunk func(string) } |
One generation call |
Result |
struct{ Text string; Truncated bool } |
Its outcome |
Kind |
KindExtract, KindWrite, KindEdit |
Which stage the request belongs to, so a backend can pick a model |
Chain |
func(cfg config.Config) []Engine |
The ordered list of engines to try |
Validate |
func(cfg config.Config) error |
Rejects an engine name that does not exist; call once before Chain |
NewOllama |
func(cfg config.Config) *Ollama |
Local HTTP backend |
NewSession |
func(name string, cfg config.Config) (*Session, bool) |
Named session-provider backend; false if unknown |
Providers |
[]Provider |
The registry, in auto-selection preference order |
ProviderNames |
func() []string |
Every registered name, in that order |
LookupProvider |
func(name string) (Provider, bool) |
Spec for one provider |
FirstAvailableProvider |
func(includeExperimental bool) (Provider, bool) |
First registered provider whose CLI is on PATH |
ResolveModel |
func(cfg config.Config, e Engine) string |
The model label an engine will use, for display |
Request.OnChunk, when set, receives streamed text as it arrives — that is what
drives the dashboard's live preview. Request.NumPredict caps output tokens for
one call; session providers manage their own generation and ignore it.
The provider table, with each CLI's headless invocation and what it is
allowed to do, is kept once, in the repository's
README; go run ./examples/providers shows which are
installed on your machine. The registry in providers.go is the source of
truth for both.
Chain resolves the ordered list of engines to try. The pipeline uses the first
that succeeds and sticks with it.
"ollama"— just the local backend.- a provider name — that session provider, then Ollama.
"auto"(the default) — every installed session provider in preference order, then Ollama.
There is deliberately no up-front network probe. A flaky connectivity check must never be what downgrades an online machine to the local model. Instead, if a session call fails — offline, or not logged in — the pipeline advances to the next engine in the chain and stays there for the rest of the run.
Cancellation is not a failure. A context that is cancelled or times out stops the run where it is rather than falling over to the next backend, which would retry work the user just abandoned.
Chain cannot report an unknown engine name — it returns no error, so it
degrades to Ollama. Call Validate first, or a typo produces a local-model
draft the user believes came from a session provider:
if err := engine.Validate(cfg); err != nil {
log.Fatal(err) // unknown engine "claud" (want auto, ollama, or one of: ...)
}
chain := engine.Chain(cfg)Every generation call is bounded by config.Config.CallTimeout (30 minutes by
default, DRAFT_CALL_TIMEOUT=0 to disable). The ceiling is generous because
generation is legitimately slow; its job is to stop a wedged provider CLI or a
black-holed HTTP connection from hanging a run forever.
The Ollama backend uses its own http.Client, not http.DefaultClient — that
one is process-global and has no timeout at all. A blanket Client.Timeout
would be wrong here because it bounds the streamed body too, so the limits sit
on the dial and on the wait for response headers, with the overall call bounded
by the context.
Result.Truncated tells the pipeline to continue generation rather than save a
mid-sentence article. Ollama reports it from done_reason, and the stream-json
providers from the message_delta stop reason. Providers that return plain
text cannot report it at all, so the pipeline also treats an ending that does
not close a sentence as a truncation — otherwise continuation would be dead
code for most backends.
Licensed under either of Apache License 2.0 or MIT License, at your option. © Sebastien Rousseau.