transcript
import "github.com/barevoidco/zelk/core/transcript"Package transcript reads a session’s log as a person sees the conversation: messages, tool calls with their results, the requests they wait on, and where runs start and end. It is the reader view a TUI renders. It folds the log with the reducer and only reads: tool results that exist only in the fold, such as a denial’s or a hook’s replacement, appear as the model saw them.
Example
ctx := context.Background()p, _ := replay.New(provider.Caps{}, replay.Reported("met", "Hello."))logs := mem.New()a, _ := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModePlan), zelk.WithStore(logs), zelk.WithInputDeny())defer a.Close(ctx)s, _ := a.Session(ctx, "s")x, _ := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Hi")))_, _ = x.Wait(ctx)
b, err := transcript.ReadLog(ctx, logs, "s")if err != nil { fmt.Println(err) return}for _, e := range b.Entries() { switch { case e.RunStarted != nil: fmt.Println("run", e.Run, "started") case e.Message != nil && e.Message.Origin == history.OriginNote: // Harness notes are for the model, such as plan mode's // instructions; hosts show them only on request. case e.Message != nil: fmt.Printf("%s: %s\n", e.Message.Role, e.Message.Text) case e.Call != nil: fmt.Println("call", e.Call.Title, e.Call.Status) case e.RunEnded != nil: fmt.Println("run", e.Run, "ended:", e.RunEnded.Outcome) }}// Output:// run 2 started// user: Hi// assistant: Hello.// call ReportOutcome done// run 2 ended: successOutput:
run 2 starteduser: Hiassistant: Hello.call ReportOutcome donerun 2 ended: successMarkdown
Section titled “Markdown”func Markdown(title string, entries []Entry) stringMarkdown renders entries as a markdown document for a person to read or share: what the user said and what the model answered in full, each tool call on one line with how it ended if not done, the questions the model asked with their answers, and plans it presented. Reasoning, harness notes, skill text and host notices are left out. A non-empty title heads the document.
Example
entries := []transcript.Entry{ {Message: &transcript.Message{Role: history.RoleUser, Origin: history.OriginUser, Text: "Is the build green?"}}, {Call: &transcript.Call{Tool: "Bash", Title: "go build ./...", Status: transcript.StatusDone}}, {Message: &transcript.Message{Role: history.RoleAssistant, Text: "Yes, it builds."}},}fmt.Print(transcript.Markdown("Build check", entries))// Output:// # Build check//// **You**//// Is the build green?//// - go build ./...//// **zelk**//// Yes, it builds.Output:
# Build check
**You**
Is the build green?
- go build ./...
**zelk**
Yes, it builds.Builder
Section titled “Builder”type Builder struct { // contains filtered or unexported fields}Builder builds a session’s transcript from its events in order. It is incremental: a host following an execution adds each event as it arrives and renders Entries again. A Builder is not safe for concurrent use.
func New(id string) *BuilderNew returns a builder for session id’s transcript.
func Read(id string, evs iter.Seq2[event.Event, error]) (*Builder, error)Read builds a transcript from a whole log.
ReadLog
Section titled “ReadLog”func ReadLog(ctx context.Context, logs Reader, id string) (*Builder, error)ReadLog builds session id’s transcript from logs, such as a store.
Builder.Add
Section titled “Builder.Add”func (b *Builder) Add(ev event.Event) errorAdd adds the next event of the log. An event that doesn’t follow from the log so far is an error, and the builder must not be used further. The builder may hold the last events added until the next ones or Flush show their record group is whole (loop.Replay): call Flush once the events there are have been added, before reading the transcript.
Builder.Entries
Section titled “Builder.Entries”func (b *Builder) Entries() []EntryEntries returns the transcript so far, in order. The entries are the caller’s: later events don’t change them. A process notice withdrawn before any model call carried it is left out, even if an earlier call to Entries showed it.
Builder.Flush
Section titled “Builder.Flush”func (b *Builder) Flush() errorFlush adds the events Add holds. Call it only where the log ended when read, as at an Update marked Settled: a group cut short folds without the decisions recorded after it, and the next fails to fold.
Builder.LastCompaction
Section titled “Builder.LastCompaction”func (b *Builder) LastCompaction() uint64LastCompaction is the sequence number of the event that last compacted the conversation, replacing turns with a summary, or 0 if none has. A compaction call that failed, its summary refused, compacted nothing.
Builder.Running
Section titled “Builder.Running”func (b *Builder) Running() boolRunning reports whether a run is under way as of the last event added: from the event that starts it, which its RunStarted names, until its RunFinished.
Builder.Seq
Section titled “Builder.Seq”func (b *Builder) Seq() uint64Seq is the sequence number of the last event added.
type Call struct { ID string Tool string Input json.RawMessage // Title and Kind present the call to a person. Title string Kind present.Kind // Incomplete says the output limit cut the call's arguments off; it // never runs. Incomplete bool Status Status // Result is what the model got back, once it has; IsError says it // is an error. Result []history.Part IsError bool // Denial says why the call was denied, when it was. Denial string // Edited is the input a person changed the call to, if they did. Edited json.RawMessage // Request is the request for a person the call made or waited on: // an approval, a question or a plan. Request *Request // Attempts counts the call's dispatches. Attempts int // Started is when the call was first dispatched, and Finished when its // result was recorded; each is zero until then. Started, Finished time.Time // Changes are the changes the call made to files, as a built-in file // tool records them, each with its patch: what a Write over a file // replaced, which its input alone doesn't say. Changes []event.FileChanged}Call is a tool call and what became of it.
Call.Exit
Section titled “Call.Exit”func (c Call) Exit() (int, bool)Exit returns the code a command the call ran exited with, when its result notes one other than 0.
Call.Output
Section titled “Call.Output”func (c Call) Output() stringOutput returns the text of the call’s result as a person reads it: without the labels zelk adds to a failure for the model, or the note of a command’s exit code, which Exit returns on its own.
Call.Text
Section titled “Call.Text”func (c Call) Text() stringText returns the text of the call’s result.
type Entry struct { // Seq is the event that added the entry; Run the run it belongs to, // zero outside any. Seq uint64 Run uint64 // Time is when that event was recorded. Time time.Time // Compacted says compaction has removed the entry from the model's // window: the model now sees a summary instead. Compacted bool
Message *Message Call *Call RunStarted *RunStarted RunEnded *RunEnded // Notice is the host's word of something outside the conversation, // such as a command the user ran that failed, which the model reads // framed as the host's; or a hook's message to the user (a recorded // HookResult.Message), which the model never sees. Notice *event.HostNotice // contains filtered or unexported fields}Entry is one thing in the conversation: exactly one of its pointer fields is set.
Message
Section titled “Message”type Message struct { // Provisional marks an assistant update whose run has not accepted it as // its final answer. Completion continuations remain updates in history. Provisional bool Role history.Role // Origin says where a user-role message came from: the user, a steer, // a skill, a harness note, a compaction summary or a file the user // attached. Origin history.Origin // File is the path of a file the user attached, as they named it, or // the name of an image, when Origin is history.OriginFile. Text is then // a text file as the model saw it, and Image an image. File string Image *history.Image Text string // Reasoning is the model's readable reasoning before the text. Reasoning string // Background is the notice a background job's end sent, when Origin // is history.OriginBackground: which job, and for a process how it // ended, so a view needn't read it from Text. Background *event.BackgroundNotice}Message is text from the user, the model or the harness.
Reader
Section titled “Reader”type Reader interface { Read(ctx context.Context, session string, fromSeq uint64) iter.Seq2[event.Event, error]}Reader reads a session’s log.
Request
Section titled “Request”type Request struct { ID string AwaitInput event.AwaitInput // Answer is how it was answered; Expired says no one answered in // time. Neither is set while it waits. Answer *event.AnswerInput Expired bool}Request is a request for a person and, once it has one, its answer.
RunEnded
Section titled “RunEnded”type RunEnded struct { event.RunFinished Report *event.Report // Answer is the run's answer when no message of the transcript shows // it: the report's summary, given in place of a final message. A run's // answer (zelk.Result.Text) is its last message's text, or the summary // when that message has none; Answer is empty in the first case, so a // host shows each answer once. Answer string}RunEnded is how a run ended, with its report if the session keeps them.
RunStarted
Section titled “RunStarted”type RunStarted struct{}RunStarted marks where a run began.
Status
Section titled “Status”type Status stringStatus is where a tool call stands.
const ( // StatusRequested: the model asked for the call; it hasn't run. StatusRequested Status = "requested" // StatusWaiting: the call waits for a person, Request says for what. StatusWaiting Status = "waiting" StatusRunning Status = "running" StatusDone Status = "done" // StatusFailed: the call ran, or was refused, and its result is an // error. StatusFailed Status = "failed" // StatusDenied: a rule, a hook, the classifier or a person denied it. StatusDenied Status = "denied" StatusCancelled Status = "cancelled")The statuses of a call.