Skip to content
Source on GitHub (opens in a new tab)

The Go library is the module github.com/barevoidco/zelk/core, package zelk. It is the loop and nothing else: your program owns the process, the storage, the isolation, the transport and the identity, and passes each to zelk explicitly. The zelk command is a program built this way. Hosts import the package under the name zelk: zelk "github.com/barevoidco/zelk/core".

New here? The Go quickstart gets a first agent running, and How zelk works explains the model behind it. The guides below are in reading order; each ends with a link to the next.

Run an agent

Decide what it may do

Keep it running

Extend it

See and check it

The reference for every exported name is generated from the source: the zelk package and the component packages beside it.

Most hosts use the agent. zelk also keeps its parts usable on their own, so a host that must own more of the execution can. These are the entry points; the embedding contract lists every public package and who owns what.

Your own driver. The agent runs a session through a driver that supervises the lease, adopts the log and dispatches effects. A host with its own executor, such as one that runs effects in another process or fits them into an existing job system, can use the same parts. driver.Ensure adopts a log and initializes it only when it is empty:

core/driver/example_test.go
ctx := context.Background()
logs := mem.New()
auth, err := driver.Acquire(ctx, &lease.Local{}, "job")
if err != nil {
panic(err)
}
defer func() { _ = auth.Release(context.Background()) }()
model, err := replay.New(provider.Caps{}, replay.Text("Hello."))
if err != nil {
panic(err)
}
d, err := driver.New(logs, model)
if err != nil {
panic(err)
}
resolved := 0
configure := func(context.Context) (event.Config, []event.Warning, error) {
resolved++
return event.Config{Provider: history.FamilyOpenAICompat, ReducerVersion: loop.Version, Model: "m1", Cwd: "."}, nil, nil
}
for range 2 {
if err := d.Ensure(ctx, auth, configure); err != nil {
panic(err)
}
}
fmt.Println("configurations resolved:", resolved)
// Output: configurations resolved: 1

The log’s record groups are protocol, not an implementation detail. Before an append, loop.Records prepares the whole group, including the decisions its payloads cause. Append the complete group, and dispatch effects only after it is durable:

core/loop/example_test.go
st := loop.New("session")
records, err := loop.Records(st, time.Time{}, event.SessionStarted{Config: event.Config{ReducerVersion: loop.Version}})
if err != nil {
panic(err)
}
// Append this complete group atomically; fold only after the append succeeds.
for _, ev := range records {
fmt.Println(ev.Seq, ev.Kind())
}
fmt.Println("acknowledged sequence:", st.Seq())
// Output:
// 1 session_started
// acknowledged sequence: 0

examples/external-driver in the repository is a complete executor written against the public API only. It acquires the lease, folds the log, drives fenced attempts, and recovers after its process is killed. Read the durability contract before writing one.

Scheduling that you own. zelk starts no timer and no goroutine for sessions that aren’t running. A host schedules a message or a cron expression on a session through a catalog, and calls Agent.DispatchWakes from its own tick, while it has the workspace open, to admit what is due. The agent offers the scheduling tools to the model only with zelk.WithScheduling. Creating a schedule starts nothing:

core/schedules_example_test.go
ctx := context.Background()
logs := mem.New()
leases, _ := lease.NewLocal()
p, _ := replay.New(provider.Caps{}, replay.Reported("met", "Ready."))
a, err := zelk.New(ctx, p, zelk.WithModel("example"), zelk.WithPermissionMode(permission.ModePlan), zelk.WithStore(logs), zelk.WithLease(leases), zelk.WithScheduling())
if err != nil {
panic(err)
}
defer a.Close(ctx)
s, _ := a.Session(ctx, "conversation")
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Say ready.")))
if err != nil {
panic(err)
}
if _, err := x.Wait(ctx); err != nil {
panic(err)
}
catalog, err := zelk.NewCatalog(logs, leases)
if err != nil {
panic(err)
}
due := time.Date(2030, 1, 1, 3, 30, 0, 0, time.UTC)
if err := catalog.ScheduleMessage(ctx, s.ID(), "inspect-worker", due, "Inspect the worker."); err != nil {
panic(err)
}
jobs, err := s.ScheduledMessages(ctx)
if err != nil {
panic(err)
}
fmt.Println(jobs[0].ID, jobs[0].State)
if err := catalog.CancelScheduledMessage(ctx, s.ID(), jobs[0].ID); err != nil {
panic(err)
}
jobs, err = catalog.ScheduledMessages(ctx, s.ID())
if err != nil {
panic(err)
}
fmt.Println(jobs[0].State)
// A host that keeps the workspace open calls a.DispatchWakes(ctx,
// workspace) on its tick to admit due messages. Creating a schedule
// starts nothing.
// Output:
// inspect-worker pending
// cancelled

Observer sources. A source is something outside the conversation that the host watches, such as a price, a queue or a build. The host creates it with zelk.CreateSource, and its observer records each occurrence with Session.RecordOccurrence. Recording appends to the session’s inbox without the lease and wakes the session, so the model reads it:

core/sources_example_test.go
ctx := context.Background()
p, _ := replay.New(provider.Caps{}, replay.Text("Observed."))
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeDefault), zelk.WithConversationalCompletion())
if err != nil {
panic(err)
}
defer func() { _ = a.Close(ctx) }()
s, _ := a.Session(ctx, "page")
x, err := s.Start(ctx, zelk.WithCommands(zelk.CreateSource(event.Source{ID: "price", Kind: "watch", Description: "Price changes"})))
if err != nil {
panic(err)
}
if _, err := x.Wait(ctx); err != nil {
panic(err)
}
if err := s.RecordOccurrence(ctx, "price", event.OccurrencePayload{Text: "Price changed to 12."}); err != nil {
panic(err)
}
// Recording pokes admission, which wakes the conversation.
if x, ok := a.Execution("page"); ok {
if _, err := x.Wait(ctx); err != nil {
panic(err)
}
}
x, ack, err := s.Send(ctx, zelk.EndSource("price"))
if err != nil {
panic(err)
}
if _, err := ack.Wait(ctx); err != nil {
panic(err)
}
if _, err := x.Wait(ctx); err != nil {
panic(err)
}
sources, err := s.Sources(ctx)
if err != nil {
panic(err)
}
fmt.Println(sources[0].State, sources[0].Cursor)
// Output: ended 1

An accepted occurrence means it is durable, not that the model has run.