Builds: design 47 G §677, §700, lead answers A-219, A-224, A-228 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
91 lines
3.7 KiB
Go
91 lines
3.7 KiB
Go
package operatorchannelclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/approval"
|
|
"git.novox.be/novox/mesh-sdk/go/operatorask"
|
|
)
|
|
|
|
// GateChannel is the operator channel as mesh-sdk's approval gate reaches it (design 47 G lead answers A-219, A-228): it
|
|
// translates the gate's own forms to and from the generated contract's, asking under the marked module's name through a
|
|
// Client whose asks are kept in the gate's state under KeyPrefix.
|
|
type GateChannel struct {
|
|
// State is the gate's state approvals; KeyPrefix is put before every key the client keeps there.
|
|
State Store
|
|
KeyPrefix string
|
|
Now func() time.Time
|
|
// Publish says an accept on the seat under module's name; ReadRecord reads the router's record `<module>.<id>` of the
|
|
// seat's state asks, both on the gate's holder's own account (design 47 G §700).
|
|
Publish func(module, subject string, body any, id string) error
|
|
ReadRecord func(module, id string) (*stdio.StateEntry, error)
|
|
}
|
|
|
|
var _ approval.Channel = (*GateChannel)(nil)
|
|
|
|
func (g *GateChannel) client(module string) *Client {
|
|
return &Client{Module: module, State: g.State, KeyPrefix: g.KeyPrefix, Now: g.Now,
|
|
Publish: func(subject string, body any, id string) error { return g.Publish(module, subject, body, id) },
|
|
Record: func(id string) (*stdio.StateEntry, error) { return g.ReadRecord(module, id) }}
|
|
}
|
|
|
|
// Ask raises the gate's ask to the operator, every option at the level approve.
|
|
func (g *GateChannel) Ask(module string, a approval.Ask) error {
|
|
ask := seat.Ask{ID: a.ID, About: a.About, Who: Operator, Headline: a.Headline, Explanation: a.Explanation,
|
|
Expires: a.Expires, OnExpiry: a.OnExpiry}
|
|
for _, o := range a.Options {
|
|
ask.Options = append(ask.Options, seat.Option{ID: o.ID, Label: o.Label, Does: o.Does, Level: Approve, Binds: o.Binds})
|
|
}
|
|
return g.client(module).Ask(ask)
|
|
}
|
|
|
|
// Record is the router's record of module's ask in the gate's form, nil while there is none.
|
|
func (g *GateChannel) Record(module, id string) (*approval.Record, error) {
|
|
e, err := g.ReadRecord(module, id)
|
|
if err != nil || e == nil {
|
|
return nil, err
|
|
}
|
|
var rec seat.AsksByAskerIDValue
|
|
if err := json.Unmarshal(e.Value, &rec); err != nil {
|
|
return nil, fmt.Errorf("the router's record of %s cannot be read: %w", id, err)
|
|
}
|
|
out := &approval.Record{State: rec.State, Answer: rec.Answer, Opened: rec.Opened, Expires: rec.Expires, OfferedOn: rec.OfferedOn}
|
|
if w := rec.Warrant; w != nil {
|
|
whole, err := json.Marshal(w)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out.Warrant = &approval.Warrant{Ask: w.Ask, Asker: w.Asker, Option: w.Option, Channel: w.Channel, At: w.At, Whole: whole}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Claim takes the warrant once with Take, holding the chosen option to act before run.
|
|
func (g *GateChannel) Claim(module string, w approval.Warrant, act operatorask.Act, run func()) (approval.Claim, error) {
|
|
var sw seat.Warrant
|
|
if err := json.Unmarshal(w.Whole, &sw); err != nil {
|
|
return approval.ClaimRefused, fmt.Errorf("the warrant for the ask %s cannot be read: %w", w.Ask, err)
|
|
}
|
|
h, err := g.client(module).Take(sw, func(o seat.Option, _ seat.Warrant) error {
|
|
if err := Performs(o, act); err != nil {
|
|
return err
|
|
}
|
|
run()
|
|
return nil
|
|
})
|
|
return approval.Claim(h), err
|
|
}
|
|
|
|
// Cancel takes module's ask back.
|
|
func (g *GateChannel) Cancel(module, id string) error { return g.client(module).Cancel(id) }
|
|
|
|
// Decided is the subject of the router's `decided` for module.
|
|
func (g *GateChannel) Decided(module string) string { return seat.SubjectDecided(module) }
|
|
|
|
// Seat is the seat the gate's asks are raised on.
|
|
func (g *GateChannel) Seat() string { return seat.Seat }
|