package wire import ( "fmt" "slices" "strings" "time" ) // Lanes (spec 2026-11-12-equipo-assistant-design.md). A turn, message, // event, card, tool call and runner session belongs to one lane: empty // is the Coordinator, LaneAssistant is the Assistant Base's chat talks // to, and a job lane (LaneJobPrefix and a hand-off id, protocol 22) is // the Coordinator thread of one job Base handed off. All are the same // person's, on the same runner. const ( LaneCoordinator = "" LaneAssistant = "assistant" LaneJobPrefix = "job:" ) // JobLane is the lane of the job thread for a hand-off. func JobLane(handoff string) string { return LaneJobPrefix + handoff } // JobOf is the hand-off a job lane belongs to. ok is false unless lane // is LaneJobPrefix and a hand-off id: h_ and 1 to 64 of [a-z0-9], which // is also safe as a directory name. func JobOf(lane string) (handoff string, ok bool) { id, found := strings.CutPrefix(lane, LaneJobPrefix) rest, isHandoff := strings.CutPrefix(id, "h_") if !found || !isHandoff || len(rest) < 1 || len(rest) > 64 { return "", false } for _, c := range rest { if (c < 'a' || c > 'z') && (c < '0' || c > '9') { return "", false } } return id, true } // ValidLane says lane is the Coordinator's, the Assistant's or a valid // job lane. func ValidLane(lane string) bool { return lane == LaneAssistant || CoordinatorRole(lane) } // CoordinatorRole says a turn in lane is a Coordinator turn: the // person's own conversation or a job thread. Only the first is the // person's conversation. func CoordinatorRole(lane string) bool { _, job := JobOf(lane) return lane == LaneCoordinator || job } // RoleAssistant is the role of the Assistant's own messages. const RoleAssistant = "assistant" // TurnTeamUpdate is an Assistant turn the instance starts itself, to // tell the person what the team did with something they asked for. const TurnTeamUpdate = "team_update" // The Assistant's own tools. look finds what exists, describe says what // an operation takes, use runs one; the rest are the coordinator's. const ( ToolLook = "look" ToolDescribe = "describe" ToolUse = "use" // ToolReadAttachment reads one of the files the instance named for the // turn (its own and the earlier ones), on the runner, from the // person's uploads (assistant attachments spec A3). It never reaches // the instance. ToolReadAttachment = "read_attachment" ) // AssistTools is the Assistant's tool set in a conversation. A // team_update turn lists the same tools (the list cannot differ by turn) // but names no files, so read_attachment finds none there. var AssistTools = []string{ToolLook, ToolDescribe, ToolUse, ToolAskPerson, ToolWriteNote, ToolReadNotes, ToolReadAttachment} // TeamUpdateTools are the tools a team_update turn may use: it looks and // tells, and never asks the person or takes notes. var TeamUpdateTools = []string{ToolLook, ToolDescribe, ToolUse, ToolReadNotes} // Assistant card kinds. CardQuestion is shared with the coordinator, and // so is CardResolved, the state of a card the instance settled itself. const ( CardConfirm = "confirm" CardOutcome = "outcome" CardConnect = "connect" // CardJob is a job card: one per hand-off, updated in place. Its // question is the job's title, its result the short status. CardJob = "job" ) // A job card's states. const ( JobWorking = "working" JobAsking = "asking" JobReady = "ready" JobDone = "done" JobStopped = "stopped" // JobPaused: the work waits and carries on by itself (paused work, // 2026-11-24). Sent only to a client that asks for it (job_states); // any other reads it as working. JobPaused = "paused" ) // LinkEquipo is the rel of the link to a job's page in Equipo. const LinkEquipo = "equipo" // CardOption is one answer an Assistant card offers. type CardOption struct { Value string `json:"value"` Label string `json:"label"` } // CardLink is a link an Assistant card shows. type CardLink struct { Label string `json:"label"` URL string `json:"url"` // Rel says what the link is to (LinkEquipo), when that matters. Rel string `json:"rel,omitempty"` } // AssistantCard is a card in the Assistant lane. State is open, answered // or resolved, or a job card's Job* state. Options and Links are never // null: the store always fills both. Job is the job card a go-ahead or // Publish card belongs to; Detail and StartedAt are a job card's only. type AssistantCard struct { ID string `json:"id"` Revision int64 `json:"revision"` Kind string `json:"kind"` State string `json:"state"` Question string `json:"question"` Options []CardOption `json:"options"` Links []CardLink `json:"links"` Result string `json:"result"` Job string `json:"job,omitempty"` Detail string `json:"detail,omitempty"` StartedAt *time.Time `json:"started_at,omitempty"` // CarryOn: a paused job card's "Carry on now" is available (POST // cards/{id}/carry_on): another of the person's plans has room at no // extra cost. CarryOn bool `json:"carry_on,omitempty"` } // AssistantSnapshot is the Assistant lane as Base first reads it. // Available says the person has a runner that can answer the Assistant. type AssistantSnapshot struct { Me Me `json:"me"` Messages []Message `json:"messages"` Turns []Turn `json:"turns"` Cards []AssistantCard `json:"cards"` Cursor int64 `json:"cursor"` Available bool `json:"available"` // CanAttach says a send with files would be taken now: the person's // primary runner was last seen at ProtocolAssistantFiles or later. CanAttach bool `json:"can_attach"` } // AssistantAnswered answers a card answer: the card, the person's answer // as a message and the turn that carries it. type AssistantAnswered struct { Card AssistantCard `json:"card"` Message Message `json:"message"` Turn Turn `json:"turn"` } // AssistantConflict refuses a card answer, with the card as it is now. type AssistantConflict struct { Code string `json:"code"` CopyKey string `json:"copy_key"` RequestID string `json:"request_id"` Card AssistantCard `json:"card"` } // AssistantHost is the Base host the instance calls for the person. type AssistantHost struct { Origin string `json:"origin"` Token string `json:"token,omitempty"` Revision int64 `json:"revision"` } // downgradeAssistant says f as a runner older than ProtocolAssistant // speaks it: it has no Assistant, so a message for one is refused, a // sync loses every session in a lane (in a new slice), and a forget_task // loses keep_preview. It reports whether anything changed. func downgradeAssistant(body any, protocol int) (bool, error) { switch v := body.(type) { case *MessageCommand: if v.Lane != LaneCoordinator { return false, fmt.Errorf("%w: a %s message at protocol %d", ErrProtocol, v.Lane, protocol) } case *ForgetTaskCommand: set := v.KeepPreview v.KeepPreview = false return set, nil case *Sync: inLane := func(s SessionState) bool { return s.Lane != LaneCoordinator } if !slices.ContainsFunc(v.Sessions, inLane) { return false, nil } v.Sessions = slices.DeleteFunc(slices.Clone(v.Sessions), inLane) return true, nil } return false, nil } // downgradeAssistantFiles says a message as a runner older than // ProtocolAssistantFiles reads it: the Assistant took words only, so an // Assistant message with files is refused (the send check and fenceLanes // keep one from being made), and earlier and earlier_more are dropped // (on a copy of the list: the caller's is untouched). It reports whether // anything changed. func downgradeAssistantFiles(body any, protocol int) (bool, error) { v, ok := body.(*MessageCommand) if !ok { return false, nil } if v.Lane == LaneAssistant && len(v.Attachments) > 0 { return false, fmt.Errorf("%w: an Assistant message with files at protocol %d", ErrProtocol, protocol) } if v.Earlier == nil && v.EarlierMore == 0 { return false, nil } v.Earlier, v.EarlierMore = nil, 0 return true, nil } // downgradeJobs says f as a runner older than ProtocolJobs speaks it: it // knows the Coordinator's and the Assistant's lanes and no other, so a // message in any other lane is refused and a sync loses every session in // one (in a new slice). It reports whether anything changed. func downgradeJobs(body any, protocol int) (bool, error) { unknown := func(lane string) bool { return lane != LaneCoordinator && lane != LaneAssistant } switch v := body.(type) { case *MessageCommand: if unknown(v.Lane) { return false, fmt.Errorf("%w: a %s message at protocol %d", ErrProtocol, v.Lane, protocol) } case *Sync: inJob := func(s SessionState) bool { return unknown(s.Lane) } if !slices.ContainsFunc(v.Sessions, inJob) { return false, nil } v.Sessions = slices.DeleteFunc(slices.Clone(v.Sessions), inJob) return true, nil } return false, nil } // CarryOn is POST /api/v1/assistant/cards/{id}/carry_on: Carry on now on // a paused job card at the revision the person saw, once per key (1 to // 128 bytes). type CarryOn struct { Revision int64 `json:"revision"` IdempotencyKey string `json:"idempotency_key"` } // AssistantCarriedOn answers Carry on now: the job card as it is now. type AssistantCarriedOn struct { Card AssistantCard `json:"card"` } // AssistantTimeZone is PUT /api/v1/assistant/time-zone: Base's zone for // the person. OnlyIfUnset keeps a zone the person's own browser or device // set; the answer is the zone in effect (TimeZone). type AssistantTimeZone struct { TimeZone string `json:"time_zone"` OnlyIfUnset bool `json:"only_if_unset,omitempty"` } // TimeZone is PUT /api/v1/time-zone and PUT /api/v1/assistant/time-zone, // and their answer: the person's zone, an IANA name. Times the instance // words for the person (a reset, when paused work carries on) are in it. type TimeZone struct { TimeZone string `json:"time_zone"` }