Fill in an inventory entry's empty boxes from its website
On the entry form, "Fill in the empty boxes" reads their website. Links, social accounts and feeds come straight from the pages (no guessing); the AI drafts the name if missing, a description, keywords, hashtags, and talking points set against the company profile for the relationship chosen. Typed values stay, nothing is saved until Save, and the form says which boxes were filled. Website reading moves from the profile draft into internal/site, shared by both, and now also collects links and declared RSS/Atom feeds (comment feeds skipped; Fill takes up to three). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+76
-10
@@ -1,13 +1,16 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/examples"
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/profile"
|
||||
)
|
||||
@@ -29,6 +32,18 @@ func (s *Server) inventoryList(w http.ResponseWriter, r *http.Request) {
|
||||
type entryData struct {
|
||||
E *inventory.Entry
|
||||
Relationships []string
|
||||
NoAI bool // no AI provider yet: filling in from the website can't work
|
||||
DraftURL string // the address to fill in from
|
||||
Filled []string // after filling in: what was filled
|
||||
Drafted bool
|
||||
}
|
||||
|
||||
func (s *Server) entryForm(e *inventory.Entry) entryData {
|
||||
d := entryData{E: e, Relationships: inventory.Relationships, NoAI: !s.aiReady()}
|
||||
if len(e.Websites) > 0 {
|
||||
d.DraftURL = e.Websites[0]
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (s *Server) inventoryEdit(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -53,7 +68,7 @@ func (s *Server) inventoryEdit(w http.ResponseWriter, r *http.Request) {
|
||||
if e.ID != 0 {
|
||||
title = e.Name
|
||||
}
|
||||
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Data: entryData{E: e, Relationships: inventory.Relationships}})
|
||||
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Data: s.entryForm(e)})
|
||||
}
|
||||
|
||||
func (s *Server) inventorySave(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -61,9 +76,22 @@ func (s *Server) inventorySave(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
e := entryFromForm(r)
|
||||
if err := inventory.Save(s.DB, e); err != nil {
|
||||
title := e.Name
|
||||
if title == "" {
|
||||
title = "New entry"
|
||||
}
|
||||
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Error: err.Error(), Data: s.entryForm(e)})
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/inventory", "saved")
|
||||
}
|
||||
|
||||
func entryFromForm(r *http.Request) *inventory.Entry {
|
||||
f := r.PostForm
|
||||
id, _ := strconv.ParseInt(f.Get("id"), 10, 64)
|
||||
e := &inventory.Entry{
|
||||
return &inventory.Entry{
|
||||
ID: id,
|
||||
Name: f.Get("name"),
|
||||
Relationship: f.Get("relationship"),
|
||||
@@ -80,16 +108,54 @@ func (s *Server) inventorySave(w http.ResponseWriter, r *http.Request) {
|
||||
TalkingPoints: f.Get("talking_points"),
|
||||
MentionPolicy: f.Get("mention_policy"),
|
||||
}
|
||||
if err := inventory.Save(s.DB, e); err != nil {
|
||||
title := e.Name
|
||||
if title == "" {
|
||||
title = "New entry"
|
||||
}
|
||||
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Error: err.Error(),
|
||||
Data: entryData{E: e, Relationships: inventory.Relationships}})
|
||||
}
|
||||
|
||||
// inventoryFill fills the form's empty boxes from the entry's website, with
|
||||
// the AI, and shows the form again, unsaved.
|
||||
func (s *Server) inventoryFill(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/inventory", "saved")
|
||||
e := entryFromForm(r)
|
||||
d := s.entryForm(e)
|
||||
d.DraftURL = strings.TrimSpace(r.PostForm.Get("draft_url"))
|
||||
title := e.Name
|
||||
if title == "" {
|
||||
title = "New entry"
|
||||
}
|
||||
show := func(errMsg string) {
|
||||
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Error: errMsg, Data: d})
|
||||
}
|
||||
if d.DraftURL == "" {
|
||||
show("Enter their website to fill in from.")
|
||||
return
|
||||
}
|
||||
cfg, err := ai.LoadConfig(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
client, err := ai.New(cfg, s.Secrets)
|
||||
if err != nil {
|
||||
show(err.Error() + ".")
|
||||
return
|
||||
}
|
||||
ours, err := profile.Load(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
|
||||
defer cancel()
|
||||
filled, err := inventory.Fill(ctx, client, e, d.DraftURL, ours)
|
||||
d.Filled, d.Drafted = filled, true
|
||||
if err != nil {
|
||||
// Links read before the model failed are kept.
|
||||
show("Couldn't fill in everything: " + err.Error())
|
||||
return
|
||||
}
|
||||
show("")
|
||||
}
|
||||
|
||||
func (s *Server) inventoryDelete(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user