Merge mesh-catalog main (559939aa) into the registry branch: gate reads forgemsg, messenger narrows asks (K-0; issue 1966)
Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
This commit is contained in:
@@ -1479,3 +1479,76 @@ func (h *Holder) sayClosed(key string, rec AskRecord, outcome string, words stri
|
||||
h.Logf("[messenger] that the ask %s ended could not be said: %v", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
// AsksQuery narrows what the tool `operator-channel.asks` answers (issue 595): Key one ask, State open (open and held) or
|
||||
// ended, Since the asks opened at or after that time, Bound the most bytes of the answer's asks. The zero query keeps the
|
||||
// whole answer, as before.
|
||||
type AsksQuery struct {
|
||||
Key, State string
|
||||
Since time.Time
|
||||
Bound int
|
||||
}
|
||||
|
||||
// AsksAnswer is the answer of a narrowed query: the asks kept, how many the filters matched, and, when the bound cut the
|
||||
// answer short, that it did and how.
|
||||
type AsksAnswer struct {
|
||||
Asks []AskView `json:"asks"`
|
||||
Matched int `json:"matched"`
|
||||
Cut string `json:"cut,omitempty"`
|
||||
}
|
||||
|
||||
// AsksQueryOf reads the arguments of the tool; an empty set is the zero query.
|
||||
func AsksQueryOf(a map[string]any) (AsksQuery, error) {
|
||||
q := AsksQuery{Key: strArg(a, "key"), State: strArg(a, "state")}
|
||||
if q.State != "" && q.State != "open" && q.State != "ended" {
|
||||
return q, errorf("state is open or ended, not %q", q.State)
|
||||
}
|
||||
if since := strArg(a, "since"); since != "" {
|
||||
t, err := time.Parse(time.RFC3339, since)
|
||||
if err != nil {
|
||||
return q, errorf("since is a time like 2026-11-04T22:00:00Z: %v", err)
|
||||
}
|
||||
q.Since = t
|
||||
}
|
||||
if v, ok := a["bound"].(float64); ok {
|
||||
if v < 1 {
|
||||
return q, errorf("bound is the most bytes of the answer, at least 1")
|
||||
}
|
||||
q.Bound = int(v)
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
// Narrow keeps of the views (open first, newest first) the asks the query names, and cuts the answer at its byte bound: the
|
||||
// asks that fit, in order, and a said cut when one more did not.
|
||||
func (q AsksQuery) Narrow(all []AskView) AsksAnswer {
|
||||
var out AsksAnswer
|
||||
out.Asks = []AskView{}
|
||||
var kept []AskView
|
||||
for _, v := range all {
|
||||
open := v.State == StateOpen || v.State == StateHeld
|
||||
switch {
|
||||
case q.Key != "" && v.Key != q.Key,
|
||||
q.State == "open" && !open,
|
||||
q.State == "ended" && open,
|
||||
!q.Since.IsZero() && v.Opened < q.Since.UTC().Format(time.RFC3339):
|
||||
continue
|
||||
}
|
||||
kept = append(kept, v)
|
||||
}
|
||||
out.Matched = len(kept)
|
||||
used := 0
|
||||
for i, v := range kept {
|
||||
if q.Bound > 0 {
|
||||
raw, _ := json.Marshal(v)
|
||||
if used+len(raw)+1 > q.Bound {
|
||||
out.Cut = fmt.Sprintf("the answer is cut at its bound of %d bytes: %d of %d asks are shown, the %d after them are not "+
|
||||
"(narrow with key, state or since, or raise bound)", q.Bound, i, len(kept), len(kept)-i)
|
||||
break
|
||||
}
|
||||
used += len(raw) + 1
|
||||
}
|
||||
out.Asks = append(out.Asks, v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user