oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire
2
3import (
4 "fmt"
5 "slices"
6 "strings"
7 "time"
8)
9
10// Lanes (spec 2026-11-12-equipo-assistant-design.md). A turn, message,
11// event, card, tool call and runner session belongs to one lane: empty
12// is the Coordinator, LaneAssistant is the Assistant Base's chat talks
13// to, and a job lane (LaneJobPrefix and a hand-off id, protocol 22) is
14// the Coordinator thread of one job Base handed off. All are the same
15// person's, on the same runner.
16const (
17 LaneCoordinator = ""
18 LaneAssistant = "assistant"
19 LaneJobPrefix = "job:"
20)
21
22// JobLane is the lane of the job thread for a hand-off.
23func JobLane(handoff string) string { return LaneJobPrefix + handoff }
24
25// JobOf is the hand-off a job lane belongs to. ok is false unless lane
26// is LaneJobPrefix and a hand-off id: h_ and 1 to 64 of [a-z0-9], which
27// is also safe as a directory name.
28func JobOf(lane string) (handoff string, ok bool) {
29 id, found := strings.CutPrefix(lane, LaneJobPrefix)
30 rest, isHandoff := strings.CutPrefix(id, "h_")
31 if !found || !isHandoff || len(rest) < 1 || len(rest) > 64 {
32 return "", false
33 }
34 for _, c := range rest {
35 if (c < 'a' || c > 'z') && (c < '0' || c > '9') {
36 return "", false
37 }
38 }
39 return id, true
40}
41
42// ValidLane says lane is the Coordinator's, the Assistant's or a valid
43// job lane.
44func ValidLane(lane string) bool { return lane == LaneAssistant || CoordinatorRole(lane) }
45
46// CoordinatorRole says a turn in lane is a Coordinator turn: the
47// person's own conversation or a job thread. Only the first is the
48// person's conversation.
49func CoordinatorRole(lane string) bool {
50 _, job := JobOf(lane)
51 return lane == LaneCoordinator || job
52}
53
54// RoleAssistant is the role of the Assistant's own messages.
55const RoleAssistant = "assistant"
56
57// TurnTeamUpdate is an Assistant turn the instance starts itself, to
58// tell the person what the team did with something they asked for.
59const TurnTeamUpdate = "team_update"
60
61// The Assistant's own tools. look finds what exists, describe says what
62// an operation takes, use runs one; the rest are the coordinator's.
63const (
64 ToolLook = "look"
65 ToolDescribe = "describe"
66 ToolUse = "use"
67 // ToolReadAttachment reads one of the files the instance named for the
68 // turn (its own and the earlier ones), on the runner, from the
69 // person's uploads (assistant attachments spec A3). It never reaches
70 // the instance.
71 ToolReadAttachment = "read_attachment"
72)
73
74// AssistTools is the Assistant's tool set in a conversation. A
75// team_update turn lists the same tools (the list cannot differ by turn)
76// but names no files, so read_attachment finds none there.
77var AssistTools = []string{ToolLook, ToolDescribe, ToolUse, ToolAskPerson, ToolWriteNote, ToolReadNotes, ToolReadAttachment}
78
79// TeamUpdateTools are the tools a team_update turn may use: it looks and
80// tells, and never asks the person or takes notes.
81var TeamUpdateTools = []string{ToolLook, ToolDescribe, ToolUse, ToolReadNotes}
82
83// Assistant card kinds. CardQuestion is shared with the coordinator, and
84// so is CardResolved, the state of a card the instance settled itself.
85const (
86 CardConfirm = "confirm"
87 CardOutcome = "outcome"
88 CardConnect = "connect"
89 // CardJob is a job card: one per hand-off, updated in place. Its
90 // question is the job's title, its result the short status.
91 CardJob = "job"
92)
93
94// A job card's states.
95const (
96 JobWorking = "working"
97 JobAsking = "asking"
98 JobReady = "ready"
99 JobDone = "done"
100 JobStopped = "stopped"
101 // JobPaused: the work waits and carries on by itself (paused work,
102 // 2026-11-24). Sent only to a client that asks for it (job_states);
103 // any other reads it as working.
104 JobPaused = "paused"
105)
106
107// LinkEquipo is the rel of the link to a job's page in Equipo.
108const LinkEquipo = "equipo"
109
110// CardOption is one answer an Assistant card offers.
111type CardOption struct {
112 Value string `json:"value"`
113 Label string `json:"label"`
114}
115
116// CardLink is a link an Assistant card shows.
117type CardLink struct {
118 Label string `json:"label"`
119 URL string `json:"url"`
120 // Rel says what the link is to (LinkEquipo), when that matters.
121 Rel string `json:"rel,omitempty"`
122}
123
124// AssistantCard is a card in the Assistant lane. State is open, answered
125// or resolved, or a job card's Job* state. Options and Links are never
126// null: the store always fills both. Job is the job card a go-ahead or
127// Publish card belongs to; Detail and StartedAt are a job card's only.
128type AssistantCard struct {
129 ID string `json:"id"`
130 Revision int64 `json:"revision"`
131 Kind string `json:"kind"`
132 State string `json:"state"`
133 Question string `json:"question"`
134 Options []CardOption `json:"options"`
135 Links []CardLink `json:"links"`
136 Result string `json:"result"`
137 Job string `json:"job,omitempty"`
138 Detail string `json:"detail,omitempty"`
139 StartedAt *time.Time `json:"started_at,omitempty"`
140 // CarryOn: a paused job card's "Carry on now" is available (POST
141 // cards/{id}/carry_on): another of the person's plans has room at no
142 // extra cost.
143 CarryOn bool `json:"carry_on,omitempty"`
144}
145
146// AssistantSnapshot is the Assistant lane as Base first reads it.
147// Available says the person has a runner that can answer the Assistant.
148type AssistantSnapshot struct {
149 Me Me `json:"me"`
150 Messages []Message `json:"messages"`
151 Turns []Turn `json:"turns"`
152 Cards []AssistantCard `json:"cards"`
153 Cursor int64 `json:"cursor"`
154 Available bool `json:"available"`
155 // CanAttach says a send with files would be taken now: the person's
156 // primary runner was last seen at ProtocolAssistantFiles or later.
157 CanAttach bool `json:"can_attach"`
158}
159
160// AssistantAnswered answers a card answer: the card, the person's answer
161// as a message and the turn that carries it.
162type AssistantAnswered struct {
163 Card AssistantCard `json:"card"`
164 Message Message `json:"message"`
165 Turn Turn `json:"turn"`
166}
167
168// AssistantConflict refuses a card answer, with the card as it is now.
169type AssistantConflict struct {
170 Code string `json:"code"`
171 CopyKey string `json:"copy_key"`
172 RequestID string `json:"request_id"`
173 Card AssistantCard `json:"card"`
174}
175
176// AssistantHost is the Base host the instance calls for the person.
177type AssistantHost struct {
178 Origin string `json:"origin"`
179 Token string `json:"token,omitempty"`
180 Revision int64 `json:"revision"`
181}
182
183// downgradeAssistant says f as a runner older than ProtocolAssistant
184// speaks it: it has no Assistant, so a message for one is refused, a
185// sync loses every session in a lane (in a new slice), and a forget_task
186// loses keep_preview. It reports whether anything changed.
187func downgradeAssistant(body any, protocol int) (bool, error) {
188 switch v := body.(type) {
189 case *MessageCommand:
190 if v.Lane != LaneCoordinator {
191 return false, fmt.Errorf("%w: a %s message at protocol %d", ErrProtocol, v.Lane, protocol)
192 }
193 case *ForgetTaskCommand:
194 set := v.KeepPreview
195 v.KeepPreview = false
196 return set, nil
197 case *Sync:
198 inLane := func(s SessionState) bool { return s.Lane != LaneCoordinator }
199 if !slices.ContainsFunc(v.Sessions, inLane) {
200 return false, nil
201 }
202 v.Sessions = slices.DeleteFunc(slices.Clone(v.Sessions), inLane)
203 return true, nil
204 }
205 return false, nil
206}
207
208// downgradeAssistantFiles says a message as a runner older than
209// ProtocolAssistantFiles reads it: the Assistant took words only, so an
210// Assistant message with files is refused (the send check and fenceLanes
211// keep one from being made), and earlier and earlier_more are dropped
212// (on a copy of the list: the caller's is untouched). It reports whether
213// anything changed.
214func downgradeAssistantFiles(body any, protocol int) (bool, error) {
215 v, ok := body.(*MessageCommand)
216 if !ok {
217 return false, nil
218 }
219 if v.Lane == LaneAssistant && len(v.Attachments) > 0 {
220 return false, fmt.Errorf("%w: an Assistant message with files at protocol %d", ErrProtocol, protocol)
221 }
222 if v.Earlier == nil && v.EarlierMore == 0 {
223 return false, nil
224 }
225 v.Earlier, v.EarlierMore = nil, 0
226 return true, nil
227}
228
229// downgradeJobs says f as a runner older than ProtocolJobs speaks it: it
230// knows the Coordinator's and the Assistant's lanes and no other, so a
231// message in any other lane is refused and a sync loses every session in
232// one (in a new slice). It reports whether anything changed.
233func downgradeJobs(body any, protocol int) (bool, error) {
234 unknown := func(lane string) bool { return lane != LaneCoordinator && lane != LaneAssistant }
235 switch v := body.(type) {
236 case *MessageCommand:
237 if unknown(v.Lane) {
238 return false, fmt.Errorf("%w: a %s message at protocol %d", ErrProtocol, v.Lane, protocol)
239 }
240 case *Sync:
241 inJob := func(s SessionState) bool { return unknown(s.Lane) }
242 if !slices.ContainsFunc(v.Sessions, inJob) {
243 return false, nil
244 }
245 v.Sessions = slices.DeleteFunc(slices.Clone(v.Sessions), inJob)
246 return true, nil
247 }
248 return false, nil
249}
250
251// CarryOn is POST /api/v1/assistant/cards/{id}/carry_on: Carry on now on
252// a paused job card at the revision the person saw, once per key (1 to
253// 128 bytes).
254type CarryOn struct {
255 Revision int64 `json:"revision"`
256 IdempotencyKey string `json:"idempotency_key"`
257}
258
259// AssistantCarriedOn answers Carry on now: the job card as it is now.
260type AssistantCarriedOn struct {
261 Card AssistantCard `json:"card"`
262}
263
264// AssistantTimeZone is PUT /api/v1/assistant/time-zone: Base's zone for
265// the person. OnlyIfUnset keeps a zone the person's own browser or device
266// set; the answer is the zone in effect (TimeZone).
267type AssistantTimeZone struct {
268 TimeZone string `json:"time_zone"`
269 OnlyIfUnset bool `json:"only_if_unset,omitempty"`
270}
271
272// TimeZone is PUT /api/v1/time-zone and PUT /api/v1/assistant/time-zone,
273// and their answer: the person's zone, an IANA name. Times the instance
274// words for the person (a reset, when paused work carries on) are in it.
275type TimeZone struct {
276 TimeZone string `json:"time_zone"`
277}
278