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) {
|
||||
|
||||
@@ -82,6 +82,7 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
mux.HandleFunc("GET /inventory/new", s.inventoryEdit)
|
||||
mux.HandleFunc("GET /inventory/{id}", s.inventoryEdit)
|
||||
mux.HandleFunc("POST /inventory/save", s.inventorySave)
|
||||
mux.HandleFunc("POST /inventory/fill", s.inventoryFill)
|
||||
mux.HandleFunc("POST /inventory/{id}/delete", s.inventoryDelete)
|
||||
mux.HandleFunc("GET /inventory/import", s.inventoryImportPage)
|
||||
mux.HandleFunc("POST /inventory/import", s.inventoryImport)
|
||||
|
||||
@@ -327,6 +327,24 @@ func TestMCPNeedsTokenAndChecksIn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInventoryFillNeedsAI(t *testing.T) {
|
||||
_, h := newTestServer(t, nil)
|
||||
if body := do(h, http.MethodGet, "/inventory/new", nil, nil).Body.String(); !strings.Contains(body, `<a href="/settings/ai">set up the AI</a>`) {
|
||||
t.Error("new entry without AI: no warning")
|
||||
}
|
||||
// The form comes back with what was typed, and says why.
|
||||
rec := postForm(h, "/inventory/fill", url.Values{"name": {"Rival"}, "relationship": {"competitor"},
|
||||
"description": {"Typed."}, "draft_url": {"https://rival.example"}})
|
||||
body := rec.Body.String()
|
||||
if rec.Code != http.StatusOK || !strings.Contains(body, "AI provider not configured") || !strings.Contains(body, "Typed.") {
|
||||
t.Errorf("fill without AI: %d", rec.Code)
|
||||
}
|
||||
rec = postForm(h, "/inventory/fill", url.Values{"name": {"Rival"}})
|
||||
if !strings.Contains(rec.Body.String(), "Enter their website") {
|
||||
t.Error("fill without a website: no message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfileDraftNeedsAI(t *testing.T) {
|
||||
s, h := newTestServer(t, nil)
|
||||
body := do(h, http.MethodGet, "/settings/profile", nil, nil).Body.String()
|
||||
|
||||
@@ -2,8 +2,20 @@
|
||||
<p><a href="/inventory">← Inventory</a></p>
|
||||
<h1>{{if $e.ID}}{{$e.Name}}{{else}}New entry{{end}}</h1>
|
||||
|
||||
<form method="post" action="/inventory/save">
|
||||
<form method="post" action="/inventory/save" data-busy-on="fill" data-busy="Reading their website…">
|
||||
<input type="hidden" name="id" value="{{$e.ID}}">
|
||||
<section class="card">
|
||||
<h2>Fill in with AI</h2>
|
||||
<p class="help">Reads their website and fills in the boxes that are still empty: links and accounts straight from the pages, and a description, talking points, keywords and hashtags from the AI, set against your <a href="/settings/profile">company profile</a>. What you've typed stays. Nothing is saved until you press Save.</p>
|
||||
{{if .NoAI}}<p class="warn-text">This needs the AI: first <a href="/settings/ai">set up the AI</a>, then come back. Or fill in the form by hand.</p>{{end}}
|
||||
<div class="row">
|
||||
<input name="draft_url" type="text" inputmode="url" value="{{.DraftURL}}" placeholder="https://example.com" aria-label="Their website">
|
||||
<button type="submit" name="action" value="fill" class="secondary" formaction="/inventory/fill" formnovalidate {{if .NoAI}}disabled{{end}}>Fill in the empty boxes</button>
|
||||
</div>
|
||||
<p class="help">Takes up to a minute or two.</p>
|
||||
{{if .Drafted}}{{if .Filled}}<p class="flash">Filled in: {{join .Filled ", "}}. Check them, correct what's wrong, then Save.</p>{{else}}<p class="muted">Nothing to fill in: every box the website could answer is already filled, or the site doesn't say.</p>{{end}}{{end}}
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<div class="cols">
|
||||
<label>Name <input name="name" value="{{$e.Name}}" required></label>
|
||||
|
||||
Reference in New Issue
Block a user