| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // Specialists defined as data (Research spec §6): the coordinator |
| 10 | // proposes one from the closed menu (internal/specialist), an owner adds |
| 11 | // it on a card, and the instance keeps the grant. |
| 12 | |
| 13 | // ToolProposeSpecialist asks the person's owner to add a specialist from |
| 14 | // the closed menu: profile, name, brief, and the profile's capabilities |
| 15 | // and tools exactly. It raises one specialist card; nothing is granted |
| 16 | // until an owner answers Add. |
| 17 | const ToolProposeSpecialist = "propose_specialist" |
| 18 | |
| 19 | // ResearchTools are the coordinator's tools that only an instance at |
| 20 | // ProtocolResearch knows. Encode refuses a call to one below it, and the |
| 21 | // runner answers it itself on an older connection. |
| 22 | var ResearchTools = []string{ToolProposeSpecialist, ToolResearch} |
| 23 | |
| 24 | // CardSpecialist asks an owner to add a proposed specialist; its answers |
| 25 | // are ChoiceAdd and ChoiceNotNow (the merge card's "not_now"). |
| 26 | const ( |
| 27 | CardSpecialist = "specialist" |
| 28 | ChoiceAdd = "add" |
| 29 | ) |
| 30 | |
| 31 | // Specialist states. A proposal is proposed until an owner answers its |
| 32 | // card (active or declined); an active specialist is revoked by an owner. |
| 33 | const ( |
| 34 | SpecialistProposed = "proposed" |
| 35 | SpecialistActive = "active" |
| 36 | SpecialistDeclined = "declined" |
| 37 | SpecialistRevoked = "revoked" |
| 38 | ) |
| 39 | |
| 40 | // CardSpecialistView is what a specialist card shows: the proposal, and each |
| 41 | // capability in plain words. |
| 42 | type CardSpecialistView struct { |
| 43 | ID string `json:"id"` |
| 44 | Profile string `json:"profile"` |
| 45 | Name string `json:"name"` |
| 46 | Brief string `json:"brief"` |
| 47 | State string `json:"state"` |
| 48 | Capabilities []SpecialistCapability `json:"capabilities"` |
| 49 | } |
| 50 | |
| 51 | // SpecialistCapability is a capability on the menu and what it lets the |
| 52 | // specialist do, in the instance's words. |
| 53 | type SpecialistCapability struct { |
| 54 | ID string `json:"id"` |
| 55 | Words string `json:"words"` |
| 56 | } |
| 57 | |
| 58 | // Specialist is one data-defined specialist, read only: GET |
| 59 | // /api/v1/specialists lists them. Capabilities and Tools are its |
| 60 | // profile's; Approver is the owner whose Add granted them. Project is its |
| 61 | // scope (ProtocolDesign): one project's id, or "" for the whole team. |
| 62 | type Specialist struct { |
| 63 | ID string `json:"id"` |
| 64 | Revision int64 `json:"revision"` |
| 65 | Profile string `json:"profile"` |
| 66 | Project string `json:"project"` |
| 67 | Name string `json:"name"` |
| 68 | Brief string `json:"brief"` |
| 69 | Model string `json:"model"` |
| 70 | State string `json:"state"` |
| 71 | Capabilities []string `json:"capabilities"` |
| 72 | Tools []string `json:"tools"` |
| 73 | ProposedBy string `json:"proposed_by"` |
| 74 | Approver string `json:"approver"` |
| 75 | ApprovedAt *time.Time `json:"approved_at"` |
| 76 | RevokedBy string `json:"revoked_by"` |
| 77 | RevokedAt *time.Time `json:"revoked_at"` |
| 78 | CreatedAt time.Time `json:"created_at"` |
| 79 | UpdatedAt time.Time `json:"updated_at"` |
| 80 | } |
| 81 | |
| 82 | // SpecialistList is GET /api/v1/specialists. |
| 83 | type SpecialistList struct { |
| 84 | Items []Specialist `json:"items"` |
| 85 | } |
| 86 | |
| 87 | // RevokeSpecialist is the body of POST /api/v1/specialists/{id}/revoke, |
| 88 | // at the specialist's current revision. |
| 89 | type RevokeSpecialist struct { |
| 90 | Revision int64 `json:"revision"` |
| 91 | } |
| 92 | |
| 93 | // downgradeResearch makes a frame speak a protocol below |
| 94 | // ProtocolResearch: a call to a research tool is refused (ErrProtocol; |
| 95 | // the runner has already answered it itself), and so is a Research |
| 96 | // step's dispatch (an older runner never gets one); any other dispatch |
| 97 | // loses the research field. Later Research slices add their frames and |
| 98 | // fields here. |
| 99 | func downgradeResearch(body any, protocol int) (bool, error) { |
| 100 | switch v := body.(type) { |
| 101 | case *ToolCall: |
| 102 | if slices.Contains(ResearchTools, v.Name) { |
| 103 | return false, fmt.Errorf("%w: %s at protocol %d", ErrProtocol, v.Name, protocol) |
| 104 | } |
| 105 | case *DispatchCommand: |
| 106 | if v.Specialist == SpecialistResearch { |
| 107 | return false, fmt.Errorf("%w: a research step at protocol %d", ErrProtocol, protocol) |
| 108 | } |
| 109 | changed := v.Research != nil || v.ResearchNote != nil |
| 110 | v.Research, v.ResearchNote = nil, nil |
| 111 | return changed, nil |
| 112 | case *StepReport: |
| 113 | if v.Research != nil { |
| 114 | v.Research = nil |
| 115 | return true, nil |
| 116 | } |
| 117 | case *Hello: |
| 118 | if v.Capabilities != nil && v.Capabilities.Research != nil { |
| 119 | v.Capabilities.Research = nil |
| 120 | return true, nil |
| 121 | } |
| 122 | case *ResearchFile: |
| 123 | return false, fmt.Errorf("%w: research_file at protocol %d", ErrProtocol, protocol) |
| 124 | } |
| 125 | return false, nil |
| 126 | } |
| 127 | |