Agent tools: plant lookup, plant creation, journal entries (#55) (#69)
Build image / build-and-push (push) Successful in 11s
Build image / build-and-push (push) Successful in 11s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #69.
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/pansy/internal/domain"
|
||||
@@ -69,6 +70,124 @@ func (s *Service) ListPlants(ctx context.Context, actorID int64) ([]domain.Plant
|
||||
return s.store.ListPlantsForActor(ctx, actorID)
|
||||
}
|
||||
|
||||
// PlantMatch is a candidate from FindPlants: the plant, plus what seed the actor
|
||||
// has left of it, so an agent can say "you only have enough for half that bed"
|
||||
// instead of confidently planting seed that doesn't exist.
|
||||
type PlantMatch struct {
|
||||
domain.Plant
|
||||
// SeedRemaining is the total left across the actor's lots of this plant, and
|
||||
// SeedUnit the unit they're counted in. Both are omitted when there are no
|
||||
// lots — and also when the lots disagree about the unit, because adding
|
||||
// grams to packets produces a number that means nothing. SeedLots still
|
||||
// reports how many there are, so "several lots, no single total" is
|
||||
// distinguishable from "no seed at all".
|
||||
SeedRemaining *float64 `json:"seedRemaining,omitempty"`
|
||||
SeedUnit string `json:"seedUnit,omitempty"`
|
||||
SeedLots int `json:"seedLots,omitempty"`
|
||||
}
|
||||
|
||||
// maxPlantMatches caps FindPlants. A model given fifty candidates is not being
|
||||
// helped; if the query is that broad the answer is to ask a better one.
|
||||
const maxPlantMatches = 10
|
||||
|
||||
// FindPlants returns the plants in the actor's visible catalog (built-ins plus
|
||||
// their own) whose name or category matches the query, best match first.
|
||||
//
|
||||
// It deliberately returns SEVERAL candidates rather than one guess. "garlic"
|
||||
// against a catalog holding both "Garlic" and "German Red Garlic" is genuinely
|
||||
// ambiguous, and a caller with the surrounding conversation is far better placed
|
||||
// to disambiguate than a fuzzy-match heuristic here. An empty query returns the
|
||||
// catalog head rather than nothing, so a caller can browse.
|
||||
func (s *Service) FindPlants(ctx context.Context, actorID int64, query string) ([]PlantMatch, error) {
|
||||
plants, err := s.store.ListPlantsForActor(ctx, actorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q := strings.ToLower(strings.TrimSpace(query))
|
||||
|
||||
type scored struct {
|
||||
plant domain.Plant
|
||||
rank int
|
||||
}
|
||||
ranked := make([]scored, 0, len(plants))
|
||||
for _, p := range plants {
|
||||
name := strings.ToLower(p.Name)
|
||||
switch {
|
||||
case q == "":
|
||||
ranked = append(ranked, scored{p, 3})
|
||||
case name == q:
|
||||
ranked = append(ranked, scored{p, 0})
|
||||
case strings.HasPrefix(name, q):
|
||||
ranked = append(ranked, scored{p, 1})
|
||||
case strings.Contains(name, q):
|
||||
ranked = append(ranked, scored{p, 2})
|
||||
case strings.Contains(strings.ToLower(p.Category), q):
|
||||
ranked = append(ranked, scored{p, 3})
|
||||
}
|
||||
}
|
||||
// Stable by rank then name, so the same query always answers the same way —
|
||||
// a tool whose results reshuffle between calls is one a model can't reason
|
||||
// about across turns.
|
||||
sort.SliceStable(ranked, func(i, j int) bool {
|
||||
if ranked[i].rank != ranked[j].rank {
|
||||
return ranked[i].rank < ranked[j].rank
|
||||
}
|
||||
return ranked[i].plant.Name < ranked[j].plant.Name
|
||||
})
|
||||
if len(ranked) > maxPlantMatches {
|
||||
ranked = ranked[:maxPlantMatches]
|
||||
}
|
||||
|
||||
matches := make([]PlantMatch, 0, len(ranked))
|
||||
for _, r := range ranked {
|
||||
matches = append(matches, PlantMatch{Plant: r.plant})
|
||||
}
|
||||
if err := s.attachSeedRemaining(ctx, actorID, matches); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return matches, nil
|
||||
}
|
||||
|
||||
// attachSeedRemaining fills SeedRemaining/SeedUnit from the actor's lots.
|
||||
func (s *Service) attachSeedRemaining(ctx context.Context, actorID int64, matches []PlantMatch) error {
|
||||
if len(matches) == 0 {
|
||||
return nil
|
||||
}
|
||||
lots, err := s.ListSeedLots(ctx, actorID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
byPlant := map[int64][]domain.SeedLot{}
|
||||
for _, l := range lots {
|
||||
byPlant[l.PlantID] = append(byPlant[l.PlantID], l)
|
||||
}
|
||||
for i := range matches {
|
||||
ls := byPlant[matches[i].ID]
|
||||
if len(ls) == 0 {
|
||||
continue
|
||||
}
|
||||
matches[i].SeedLots = len(ls)
|
||||
unit := ls[0].Unit
|
||||
mixed := false
|
||||
total := 0.0
|
||||
for _, l := range ls {
|
||||
total += l.Remaining
|
||||
if l.Unit != unit {
|
||||
mixed = true
|
||||
}
|
||||
}
|
||||
if mixed {
|
||||
// Dropping only the LABEL would leave a number that reads as a
|
||||
// quantity and isn't one. Drop the total with it; SeedLots still says
|
||||
// there is seed here, just not one figure for it.
|
||||
continue
|
||||
}
|
||||
matches[i].SeedRemaining = &total
|
||||
matches[i].SeedUnit = unit
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreatePlant adds a plant owned by the actor. To "clone" a built-in the client
|
||||
// simply POSTs a copy — there is no dedicated endpoint, and the copy is owned by
|
||||
// (and editable by) the actor.
|
||||
|
||||
Reference in New Issue
Block a user