Documentation
¶
Overview ¶
Package chat is Flockdeck's own terminal chat client: the `flockdeck chat` subcommand, run inside a pane's pseudo-terminal, talking straight to a model API.
It exists so that a model reached over HTTP is a first-class agent rather than something Flockdeck shells out to. There is no wrapper CLI, no node and no Python: the pane runs this binary, and this binary speaks the vendor's own protocol. Everything Flockdeck does around a pane -- status, resume, the history overlay, a fan-out reading a plan -- is fed from in here using the machinery the `claude` CLI already drives, so the workspace has nothing new to learn.
Index ¶
- Constants
- Variables
- func Dir() (string, error)
- func Path(session string) string
- func Replies(session string, maxTurns int) []string
- func Run(ctx context.Context, o Options) error
- type AlwaysApprover
- type Conversation
- type Entry
- type Event
- type EventKind
- type Log
- type Message
- type Options
- type Request
- type Role
- type Schema
- type Tool
- type ToolCall
- type Usage
- type Wire
Constants ¶
const NoPrompt = "(no prompt recorded)"
NoPrompt stands in for a conversation that says nothing about itself, the way the same label does for a Claude Code transcript.
Variables ¶
var ErrBadFlags = errors.New("run `flockdeck chat -h` for the flags it takes")
ErrBadFlags means the command line was wrong. The flag set has already said which part of it, so this only points at where the rest is written down -- repeating the complaint would bury it.
var ErrHelpShown = errors.New("usage shown")
ErrHelpShown means the usage was asked for and printed: there is nothing wrong and nothing left to do.
var KeyStore func(agent string) string
KeyStore is asked for an API key when the environment does not have one. It is a variable so that the credential store can supply it without the chat client depending on it, and so that a key reaches exactly one process: this one.
Functions ¶
func Path ¶
Path returns the transcript for a session, or "" when there is nothing to read.
Emptiness counts as nothing, the same way it does for Claude Code: a pane that was opened and never prompted leaves a file behind with no conversation in it, and resuming that is not resuming anything.
func Replies ¶
Replies returns what the model said in its last few turns, newest first and at most maxTurns of them.
This is the shape session.RecentReplies has, and for the same reason: it is where a fan-out finds the plan it is about to split up. An API pane's plan is then no different from any other agent's.
Types ¶
type AlwaysApprover ¶
type AlwaysApprover interface {
// AlwaysKey returns a key standing for every call the user would be
// agreeing to, or "" when this call cannot be approved in bulk.
AlwaysKey(args json.RawMessage) string
}
AlwaysApprover is implemented by a tool that can be approved once for a whole family of calls rather than one at a time.
It is an interface rather than a rule in the loop because what makes two calls the same family is the tool's own business: for `run_command` it is the command prefix, and nothing else in v1 offers it at all.
type Conversation ¶
type Conversation struct {
ID string
Cwd string
Summary string
Title string
Modified time.Time
Messages int
Size int64
}
Conversation is a stored chat that can be resumed. It carries the same facts as a Claude Code conversation, so the history overlay can list the two side by side.
func Conversations ¶
func Conversations(cwd string) ([]Conversation, error)
Conversations lists the stored chats belonging to a working directory, newest first. An empty cwd lists them all.
type Entry ¶
type Entry struct {
Type string `json:"type"`
TS timestamp `json:"ts"`
Text string `json:"text"`
// Tool names the tool a "tool" entry is the output of.
Tool string `json:"tool,omitempty"`
Cwd string `json:"cwd,omitempty"`
Model string `json:"model,omitempty"`
In int `json:"in,omitempty"`
Out int `json:"out,omitempty"`
}
Entry is one line of a chat transcript.
The three types are what the conversation is made of; the rest are additions that cost a few bytes a line and answer questions the file otherwise cannot: which directory the conversation belongs to, which model wrote an answer, and what it cost. A reader that knows only about type, ts and text is unaffected by them.
func ReadEntries ¶
ReadEntries reads a whole transcript. A line that does not parse is skipped rather than failing the read: the last line of a file being written to is half-written as often as not.
type EventKind ¶
type EventKind int
EventKind is what a streamed event carries.
const ( // EventText is a piece of the answer, as it arrives. EventText EventKind = iota // EventThinking is a piece of the model's reasoning, where a model returns // it. It is drawn differently and never recorded: it is the model working // something out, not what it decided. EventThinking // EventCall is a completed request to run a tool. EventCall // EventUsage is the token count for the turn, sent once, at the end. EventUsage )
type Log ¶
type Log struct {
// contains filtered or unexported fields
}
Log appends to one session's transcript.
It is written through on every entry rather than buffered, because the two things that read it -- a fan-out looking for a plan, the history overlay -- read it while the pane is still running, and an answer still sitting in a buffer is an answer they cannot see.
type Message ¶
type Message struct {
Role Role
Text string
// Calls are the tools an assistant message asked for.
Calls []ToolCall
// Call is the call a tool message answers.
Call ToolCall
}
Message is one entry in a conversation, in the single shape all three wires are translated from. Keeping one shape is what lets the transcript, the renderer and the tool loop be written once.
func Messages ¶
Messages turns a transcript back into a conversation to resume from.
A tool's output comes back as part of what the user said rather than as the answer to a call, because the transcript records what was said and not the call ids the wires pair an answer with -- and an answer to a call that is not in the request is rejected outright by every one of them. Consecutive entries on the same side are joined for the same reason: an API that insists the two sides alternate should not be handed two user entries in a row.
type Options ¶
type Options struct {
// Agent is the catalog id of the agent this pane was started as. It names
// the key to look for and is what the header says.
Agent string
Model string
Session string
Resume bool
// Task is the opening prompt, which arrives in the argv rather than being
// typed, because typing into a program means guessing when it is ready.
Task string
// Wire, BaseURL and KeyEnv are the agent's APISpec, passed in rather than
// looked up: what the pane runs is decided by whoever built the argv, and
// the chat client does not need a catalog of its own to be told.
Wire string
BaseURL string
KeyEnv []string
MaxTokens int
Cwd string
// Dir is where transcripts are kept. Empty means the state directory,
// which is where they belong; a test says otherwise.
Dir string
// API and Token are the pane's callback address and secret.
API string
Token string
In io.Reader
Out io.Writer
Width int
Colour bool
// Tools are what the model can do besides talk. The loop below already
// handles asking for a call, the approval, the lifecycle events and feeding
// the answer back; the tools themselves are supplied from outside so that
// they and the loop can be built apart.
Tools []Tool
// Signals delivers interrupts. Empty means the client subscribes to them
// itself, which is what it does in a pane.
Signals <-chan os.Signal
// contains filtered or unexported fields
}
Options are what one run of the chat client needs. Everything here is either a command line flag or something the pane's environment already carries, so that `flockdeck chat` behaves the same run from a pane and run by hand.
func ParseArgs ¶
ParseArgs turns the arguments of `flockdeck chat` into the options to run with.
It lives here rather than beside main so that what the command accepts is tested where it is defined. Every flag has an environment variable behind it, because a pane's argv is built from an agent's Spec -- which can say `--wire openai` as a literal argument, or put the same thing in the pane's environment through Spec.Env -- and neither should be the only way.
type Role ¶
type Role string
Role is who an entry in a conversation came from.
There are three rather than the four or five a given vendor names, because these are the ones the transcript records and every wire can express: a system prompt is a field on the request, not an entry in the conversation.
type Schema ¶
type Schema struct {
Description string
// Params is the JSON Schema object for the arguments -- `{"type":"object",
// "properties":{...}}`. A tool that takes none may leave it empty.
Params json.RawMessage
}
Schema is a tool's arguments described as JSON Schema. Each wire translates it into the shape that vendor asks for; the schema itself is the same.
type Tool ¶
type Tool interface {
Name() string
Describe() Schema
// Approval returns the question to put to the user before the call runs,
// or "" when the call needs no asking.
Approval(args json.RawMessage) string
Run(ctx context.Context, args json.RawMessage) (string, error)
}
Tool is one thing the model can do beyond talking. The chat loop knows only this much about a tool, so the loop and the tools it runs can be built apart.
type ToolCall ¶
type ToolCall struct {
// ID is the vendor's identifier for the call, which the answer must quote.
// Gemini has none, so it is filled in from the name there.
ID string
Name string
// Args are the arguments as the model wrote them. They stay raw JSON all
// the way to the tool because a tool's own schema is the only thing that
// knows their shape.
Args json.RawMessage
}
ToolCall is one tool the model asked for.
type Wire ¶
type Wire interface {
// Name is the wire's name as an agent's APISpec spells it.
Name() string
// Stream sends one request and calls emit for each event until the answer
// is complete. A cancelled context ends the turn; emit is only ever called
// from the calling goroutine.
Stream(ctx context.Context, req Request, emit func(Event)) error
}
Wire speaks one vendor's HTTP protocol.
A wire's whole job is translation: it turns a Request into that vendor's JSON and its stream back into Events. It renders nothing, records nothing and runs no tools, which is why three of them fit in a few hundred lines each.