package wire import ( "fmt" "slices" "time" ) // Specialists defined as data (Research spec ยง6): the coordinator // proposes one from the closed menu (internal/specialist), an owner adds // it on a card, and the instance keeps the grant. // ToolProposeSpecialist asks the person's owner to add a specialist from // the closed menu: profile, name, brief, and the profile's capabilities // and tools exactly. It raises one specialist card; nothing is granted // until an owner answers Add. const ToolProposeSpecialist = "propose_specialist" // ResearchTools are the coordinator's tools that only an instance at // ProtocolResearch knows. Encode refuses a call to one below it, and the // runner answers it itself on an older connection. var ResearchTools = []string{ToolProposeSpecialist, ToolResearch} // CardSpecialist asks an owner to add a proposed specialist; its answers // are ChoiceAdd and ChoiceNotNow (the merge card's "not_now"). const ( CardSpecialist = "specialist" ChoiceAdd = "add" ) // Specialist states. A proposal is proposed until an owner answers its // card (active or declined); an active specialist is revoked by an owner. const ( SpecialistProposed = "proposed" SpecialistActive = "active" SpecialistDeclined = "declined" SpecialistRevoked = "revoked" ) // CardSpecialistView is what a specialist card shows: the proposal, and each // capability in plain words. type CardSpecialistView struct { ID string `json:"id"` Profile string `json:"profile"` Name string `json:"name"` Brief string `json:"brief"` State string `json:"state"` Capabilities []SpecialistCapability `json:"capabilities"` } // SpecialistCapability is a capability on the menu and what it lets the // specialist do, in the instance's words. type SpecialistCapability struct { ID string `json:"id"` Words string `json:"words"` } // Specialist is one data-defined specialist, read only: GET // /api/v1/specialists lists them. Capabilities and Tools are its // profile's; Approver is the owner whose Add granted them. Project is its // scope (ProtocolDesign): one project's id, or "" for the whole team. type Specialist struct { ID string `json:"id"` Revision int64 `json:"revision"` Profile string `json:"profile"` Project string `json:"project"` Name string `json:"name"` Brief string `json:"brief"` Model string `json:"model"` State string `json:"state"` Capabilities []string `json:"capabilities"` Tools []string `json:"tools"` ProposedBy string `json:"proposed_by"` Approver string `json:"approver"` ApprovedAt *time.Time `json:"approved_at"` RevokedBy string `json:"revoked_by"` RevokedAt *time.Time `json:"revoked_at"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // SpecialistList is GET /api/v1/specialists. type SpecialistList struct { Items []Specialist `json:"items"` } // RevokeSpecialist is the body of POST /api/v1/specialists/{id}/revoke, // at the specialist's current revision. type RevokeSpecialist struct { Revision int64 `json:"revision"` } // downgradeResearch makes a frame speak a protocol below // ProtocolResearch: a call to a research tool is refused (ErrProtocol; // the runner has already answered it itself), and so is a Research // step's dispatch (an older runner never gets one); any other dispatch // loses the research field. Later Research slices add their frames and // fields here. func downgradeResearch(body any, protocol int) (bool, error) { switch v := body.(type) { case *ToolCall: if slices.Contains(ResearchTools, v.Name) { return false, fmt.Errorf("%w: %s at protocol %d", ErrProtocol, v.Name, protocol) } case *DispatchCommand: if v.Specialist == SpecialistResearch { return false, fmt.Errorf("%w: a research step at protocol %d", ErrProtocol, protocol) } changed := v.Research != nil || v.ResearchNote != nil v.Research, v.ResearchNote = nil, nil return changed, nil case *StepReport: if v.Research != nil { v.Research = nil return true, nil } case *Hello: if v.Capabilities != nil && v.Capabilities.Research != nil { v.Capabilities.Research = nil return true, nil } case *ResearchFile: return false, fmt.Errorf("%w: research_file at protocol %d", ErrProtocol, protocol) } return false, nil }