Suggest subreddits when filling in an inventory entry, checked with Reddit
If the Subreddits box is empty, the AI suggests up to eight, by the relationship, each with who is there. Each is looked up on Reddit (with the app's client ID and secret) and left out if it doesn't exist, is private, 18+, restricted or archived; the first five kept fill the box. The form lists every suggestion with members, post types and why any was left out. Lookups are cached for drafting. Without Reddit keys the suggestions are kept and the form says they weren't checked. The Reddit client now returns ErrNotFound and ErrForbidden, and treats an empty about listing as not found. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+96
-13
@@ -2,10 +2,12 @@ package inventory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/site"
|
||||
)
|
||||
@@ -13,6 +15,28 @@ import (
|
||||
// maxFeeds is how many of a site's feeds Fill takes.
|
||||
const maxFeeds = 3
|
||||
|
||||
// maxSubreddits is how many suggested subreddits Fill puts in the box.
|
||||
const maxSubreddits = 5
|
||||
|
||||
// SubredditCheck looks a subreddit up on Reddit. It returns reddit.ErrNotFound
|
||||
// or reddit.ErrForbidden for one that doesn't exist or is private.
|
||||
type SubredditCheck func(ctx context.Context, name string) (*reddit.Subreddit, error)
|
||||
|
||||
// Suggestion is a subreddit the AI suggested, and what Reddit said about it.
|
||||
type Suggestion struct {
|
||||
Name string
|
||||
Why string // the model's reason
|
||||
Info *reddit.Subreddit // nil if not checked
|
||||
Kept bool // put in the Subreddits box
|
||||
Problem string // why it was left out, or why it's unchecked
|
||||
}
|
||||
|
||||
// FillResult says what Fill did.
|
||||
type FillResult struct {
|
||||
Filled []string // labels of the fields filled
|
||||
Suggestions []Suggestion // subreddits the AI suggested, if it was asked
|
||||
}
|
||||
|
||||
// Fill fills the entry's empty fields from its website, for the user to check
|
||||
// before saving. Fields that already have a value are kept as they are.
|
||||
//
|
||||
@@ -20,24 +44,26 @@ const maxFeeds = 3
|
||||
// YouTube and Instagram accounts, subreddits and feeds the site links to.
|
||||
// The model writes what needs judgement: the name if missing, description,
|
||||
// talking points (set against ours, the user's own company profile, if
|
||||
// there is one), keywords and hashtags. It returns the labels of the fields
|
||||
// it filled.
|
||||
func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours *profile.Profile) ([]string, error) {
|
||||
// there is one), keywords and hashtags. If the site links to no subreddit,
|
||||
// the model suggests some, and check (if not nil) drops the ones that don't
|
||||
// exist or that can't be posted to; models invent believable names.
|
||||
func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours *profile.Profile, check SubredditCheck) (*FillResult, error) {
|
||||
res := &FillResult{}
|
||||
st, err := site.Read(ctx, siteURL, "Supermarket/1 (inventory draft)")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return res, err
|
||||
}
|
||||
var filled []string
|
||||
filled := &res.Filled
|
||||
setStr := func(label string, field *string, v string) {
|
||||
if strings.TrimSpace(*field) == "" && strings.TrimSpace(v) != "" {
|
||||
*field = strings.TrimSpace(v)
|
||||
filled = append(filled, label)
|
||||
*filled = append(*filled, label)
|
||||
}
|
||||
}
|
||||
setList := func(label string, field *[]string, v []string) {
|
||||
if len(profile.CleanList(*field)) == 0 && len(profile.CleanList(v)) > 0 {
|
||||
*field = profile.CleanList(v)
|
||||
filled = append(filled, label)
|
||||
*filled = append(*filled, label)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,11 +87,12 @@ func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours
|
||||
setList("feeds", &e.RSS, feeds)
|
||||
|
||||
// The rest from the model, only if something is still empty.
|
||||
wantSubs := len(profile.CleanList(e.Subreddits)) == 0
|
||||
needs := strings.TrimSpace(e.Name) == "" || strings.TrimSpace(e.Description) == "" ||
|
||||
strings.TrimSpace(e.TalkingPoints) == "" || len(profile.CleanList(e.Keywords)) == 0 ||
|
||||
len(profile.CleanList(e.Hashtags)) == 0
|
||||
len(profile.CleanList(e.Hashtags)) == 0 || wantSubs
|
||||
if !needs {
|
||||
return filled, nil
|
||||
return res, nil
|
||||
}
|
||||
var d struct {
|
||||
Name string `json:"name"`
|
||||
@@ -73,6 +100,10 @@ func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours
|
||||
TalkingPoints string `json:"talking_points"`
|
||||
Keywords []string `json:"keywords"`
|
||||
Hashtags []string `json:"hashtags"`
|
||||
Subreddits []struct {
|
||||
Name string `json:"name"`
|
||||
Why string `json:"why"`
|
||||
} `json:"subreddits"`
|
||||
}
|
||||
err = ai.GenerateJSON(ctx, client, ai.Request{
|
||||
Task: ai.TaskProfile,
|
||||
@@ -82,14 +113,62 @@ func Fill(ctx context.Context, client ai.Client, e *Entry, siteURL string, ours
|
||||
Schema: fillSchema(),
|
||||
}, &d)
|
||||
if err != nil {
|
||||
return filled, err
|
||||
return res, err
|
||||
}
|
||||
setStr("name", &e.Name, d.Name)
|
||||
setStr("description", &e.Description, d.Description)
|
||||
setStr("talking points", &e.TalkingPoints, d.TalkingPoints)
|
||||
setList("keywords", &e.Keywords, d.Keywords)
|
||||
setList("hashtags", &e.Hashtags, d.Hashtags)
|
||||
return filled, nil
|
||||
|
||||
if wantSubs {
|
||||
var keep []string
|
||||
seen := map[string]bool{}
|
||||
for _, sub := range d.Subreddits {
|
||||
name := reddit.CleanName(sub.Name)
|
||||
if name == "" || seen[strings.ToLower(name)] {
|
||||
continue
|
||||
}
|
||||
seen[strings.ToLower(name)] = true
|
||||
sg := checkSubreddit(ctx, check, name, sub.Why)
|
||||
if sg.Kept && len(keep) < maxSubreddits {
|
||||
keep = append(keep, sg.Name)
|
||||
} else if sg.Kept {
|
||||
sg.Kept, sg.Problem = false, "more than enough already"
|
||||
}
|
||||
res.Suggestions = append(res.Suggestions, sg)
|
||||
}
|
||||
setList("subreddits", &e.Subreddits, keep)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// checkSubreddit asks Reddit about a suggested subreddit. One Reddit can't
|
||||
// be asked about (no keys, rate limit) is kept, marked unchecked.
|
||||
func checkSubreddit(ctx context.Context, check SubredditCheck, name, why string) Suggestion {
|
||||
sg := Suggestion{Name: name, Why: strings.TrimSpace(why), Kept: true}
|
||||
if check == nil {
|
||||
sg.Problem = "not checked"
|
||||
return sg
|
||||
}
|
||||
info, err := check(ctx, name)
|
||||
switch {
|
||||
case errors.Is(err, reddit.ErrNotFound):
|
||||
sg.Kept, sg.Problem = false, "doesn't exist, or was banned"
|
||||
case errors.Is(err, reddit.ErrForbidden):
|
||||
sg.Kept, sg.Problem = false, "private"
|
||||
case err != nil:
|
||||
sg.Problem = "not checked: " + err.Error()
|
||||
case info.Type == "private":
|
||||
sg.Kept, sg.Problem = false, "private"
|
||||
case info.Over18:
|
||||
sg.Kept, sg.Problem = false, "marked 18+"
|
||||
case info.Type == "restricted" || info.Type == "archived":
|
||||
sg.Kept, sg.Problem = false, info.Type+": only approved accounts can post"
|
||||
default:
|
||||
sg.Name, sg.Info = info.Name, info // Reddit's spelling of the name
|
||||
}
|
||||
return sg
|
||||
}
|
||||
|
||||
const fillSystem = `You fill in an entry in a company's marketing inventory: another company or project it posts about, as a partner, a competitor, or one to watch. The marketing team reviews and corrects what you write.
|
||||
@@ -105,7 +184,8 @@ Rules:
|
||||
- watch: what they cover that matters to the company's audience.
|
||||
Without a company profile, leave talking_points empty.
|
||||
- keywords: up to eight phrases they own or compete on.
|
||||
- hashtags: up to five, only ones their pages use or that name them or their product.`
|
||||
- hashtags: up to five, only ones their pages use or that name them or their product.
|
||||
- subreddits: up to eight subreddits where talking about them would reach the company's audience: for a partner, where their users and the company's customers talk; for a competitor, where people compare or ask about tools like theirs; for one to watch, where their field is discussed. Names without r/, real subreddits you're confident exist, active and on topic; general ones (like technology) only if nothing narrower fits. why: one short phrase on who is there. They'll be checked against Reddit before use.`
|
||||
|
||||
func fillPrompt(e *Entry, ours *profile.Profile, st *site.Site) string {
|
||||
var b strings.Builder
|
||||
@@ -132,7 +212,10 @@ func fillPrompt(e *Entry, ours *profile.Profile, st *site.Site) string {
|
||||
func fillSchema() map[string]any {
|
||||
str := map[string]any{"type": "string"}
|
||||
list := map[string]any{"type": "array", "items": str}
|
||||
props := map[string]any{"name": str, "description": str, "talking_points": str, "keywords": list, "hashtags": list}
|
||||
sub := map[string]any{"type": "object", "properties": map[string]any{"name": str, "why": str},
|
||||
"required": []string{"name", "why"}, "additionalProperties": false}
|
||||
props := map[string]any{"name": str, "description": str, "talking_points": str, "keywords": list, "hashtags": list,
|
||||
"subreddits": map[string]any{"type": "array", "items": sub}}
|
||||
req := make([]string, 0, len(props))
|
||||
for k := range props {
|
||||
req = append(req, k)
|
||||
|
||||
Reference in New Issue
Block a user