Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/profile"
|
||||
)
|
||||
|
||||
type profileData struct {
|
||||
P *profile.Profile
|
||||
Products []profile.Product // existing plus one blank row
|
||||
Websites []profile.Website // existing plus one blank row
|
||||
Drafted bool
|
||||
DraftURL string
|
||||
}
|
||||
|
||||
func withBlankRows(p *profile.Profile) profileData {
|
||||
d := profileData{P: p}
|
||||
d.Products = append(append([]profile.Product{}, p.Products...), profile.Product{})
|
||||
d.Websites = append(append([]profile.Website{}, p.Websites...), profile.Website{})
|
||||
return d
|
||||
}
|
||||
|
||||
func (s *Server) profilePage(w http.ResponseWriter, r *http.Request) {
|
||||
p, err := profile.Load(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
d := withBlankRows(p)
|
||||
if len(p.Websites) > 0 {
|
||||
d.DraftURL = p.Websites[0].URL
|
||||
}
|
||||
s.render(w, r, "profile", page{Title: "Company profile", Nav: "profile", Data: d})
|
||||
}
|
||||
|
||||
// profileFromForm reads the profile form. Repeated rows (products, websites)
|
||||
// arrive as parallel lists, one value per row for each field.
|
||||
func profileFromForm(r *http.Request) *profile.Profile {
|
||||
f := r.PostForm
|
||||
p := &profile.Profile{
|
||||
Name: f.Get("name"),
|
||||
Description: f.Get("description"),
|
||||
Positioning: f.Get("positioning"),
|
||||
Voice: f.Get("voice"),
|
||||
Avoid: profile.Lines(f.Get("avoid")),
|
||||
Keywords: profile.Lines(f.Get("keywords")),
|
||||
Targets: profile.Targets{
|
||||
Industries: profile.Lines(f.Get("industries")),
|
||||
CompanySizes: profile.Lines(f.Get("company_sizes")),
|
||||
Roles: profile.Lines(f.Get("roles")),
|
||||
Regions: profile.Lines(f.Get("regions")),
|
||||
Problems: profile.Lines(f.Get("problems")),
|
||||
BuyingSignals: profile.Lines(f.Get("buying_signals")),
|
||||
Technologies: profile.Lines(f.Get("technologies")),
|
||||
},
|
||||
Accounts: profile.Accounts{
|
||||
X: f.Get("account_x"),
|
||||
LinkedIn: f.Get("account_linkedin"),
|
||||
Reddit: f.Get("account_reddit"),
|
||||
Subreddits: profile.Lines(f.Get("subreddits")),
|
||||
},
|
||||
}
|
||||
at := func(key string, i int) string {
|
||||
if v := f[key]; i < len(v) {
|
||||
return v[i]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
for i := range f["product_name"] {
|
||||
p.Products = append(p.Products, profile.Product{
|
||||
Name: at("product_name", i),
|
||||
Description: at("product_description", i),
|
||||
URL: at("product_url", i),
|
||||
Pricing: at("product_pricing", i),
|
||||
Benefits: profile.Lines(at("product_benefits", i)),
|
||||
})
|
||||
}
|
||||
for i := range f["website_url"] {
|
||||
p.Websites = append(p.Websites, profile.Website{
|
||||
URL: at("website_url", i),
|
||||
Repo: at("website_repo", i),
|
||||
Branch: at("website_branch", i),
|
||||
PlausibleSite: at("website_plausible", i),
|
||||
SearchConsoleProperty: at("website_gsc", i),
|
||||
BingSite: at("website_bing", i),
|
||||
})
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (s *Server) profileSave(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
p := profileFromForm(r)
|
||||
p.Clean()
|
||||
if !p.IsSet() {
|
||||
s.render(w, r, "profile", page{Title: "Company profile", Nav: "profile",
|
||||
Error: "The company's name and description are required.", Data: withBlankRows(p)})
|
||||
return
|
||||
}
|
||||
if err := profile.Save(s.DB, p); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/profile", "saved")
|
||||
}
|
||||
|
||||
// profileDraft drafts the profile from a website and shows it in the form,
|
||||
// unsaved. Fields the user already filled in keep their values.
|
||||
func (s *Server) profileDraft(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
siteURL := r.PostForm.Get("draft_url")
|
||||
current, err := profile.Load(s.DB)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
show := func(p *profile.Profile, errMsg string, drafted bool) {
|
||||
d := withBlankRows(p)
|
||||
d.Drafted = drafted
|
||||
d.DraftURL = siteURL
|
||||
s.render(w, r, "profile", page{Title: "Company profile", Nav: "profile", Error: errMsg, Data: d})
|
||||
}
|
||||
|
||||
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(current, err.Error()+".", false)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
|
||||
defer cancel()
|
||||
draft, err := profile.Draft(ctx, client, siteURL)
|
||||
if err != nil {
|
||||
show(current, "Couldn't draft the profile: "+err.Error(), false)
|
||||
return
|
||||
}
|
||||
show(merge(current, draft), "", true)
|
||||
}
|
||||
|
||||
// merge keeps what the user already entered and fills the gaps from the draft.
|
||||
func merge(cur, draft *profile.Profile) *profile.Profile {
|
||||
out := *cur
|
||||
pick := func(a, b string) string {
|
||||
if a != "" {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
pickList := func(a, b []string) []string {
|
||||
if len(a) > 0 {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
out.Name = pick(cur.Name, draft.Name)
|
||||
out.Description = pick(cur.Description, draft.Description)
|
||||
out.Positioning = pick(cur.Positioning, draft.Positioning)
|
||||
out.Voice = pick(cur.Voice, draft.Voice)
|
||||
out.Keywords = pickList(cur.Keywords, draft.Keywords)
|
||||
if len(cur.Products) == 0 {
|
||||
out.Products = draft.Products
|
||||
}
|
||||
if len(cur.Websites) == 0 {
|
||||
out.Websites = draft.Websites
|
||||
}
|
||||
t, dt := &out.Targets, draft.Targets
|
||||
t.Industries = pickList(t.Industries, dt.Industries)
|
||||
t.CompanySizes = pickList(t.CompanySizes, dt.CompanySizes)
|
||||
t.Roles = pickList(t.Roles, dt.Roles)
|
||||
t.Regions = pickList(t.Regions, dt.Regions)
|
||||
t.Problems = pickList(t.Problems, dt.Problems)
|
||||
t.BuyingSignals = pickList(t.BuyingSignals, dt.BuyingSignals)
|
||||
t.Technologies = pickList(t.Technologies, dt.Technologies)
|
||||
out.Accounts.X = pick(cur.Accounts.X, draft.Accounts.X)
|
||||
out.Accounts.LinkedIn = pick(cur.Accounts.LinkedIn, draft.Accounts.LinkedIn)
|
||||
return &out
|
||||
}
|
||||
Reference in New Issue
Block a user