Phase 5 part 3: build pages from a site's own sections
Sites → New pages: the AI composes a landing, product, alternative, comparison or blog page as the site's own sections, from the profile and optionally a product, competitor, buying signal or SEO brief. Code checks every section type, warns on unknown fields and missing pages, tags a signal's links with its campaign and finds experience claims. Pages are edited here, pushed to their own branch with a preview, and opened as pull requests after the claims are confirmed. Schema 22. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
package sites
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
// Page statuses.
|
||||
const (
|
||||
PageDraft = "draft" // composed; the user reviews it
|
||||
PageWorking = "working" // being pushed
|
||||
PagePushed = "pushed" // on its branch, with a preview; no pull request yet
|
||||
PagePR = "pr" // a pull request is open
|
||||
PageMerged = "merged" // merged: published
|
||||
PageClosed = "closed" // the pull request was closed without merging
|
||||
PageFailed = "failed" // the last push failed; Error says why
|
||||
)
|
||||
|
||||
// Kinds of page, with what each is for (shown to the user and the AI).
|
||||
var Kinds = []struct{ ID, Label, Guide string }{
|
||||
{"landing", "Landing page",
|
||||
"A landing page for one audience or campaign. One clear promise in the hero; the problem in the reader's words; how we solve it; proof we actually have; the objections answered (an FAQ); one call to action, repeated at the end. Short and focused: no tour of everything we do."},
|
||||
{"product", "Product page",
|
||||
"A page for one product: what it is and who it's for, its benefits, how it works, what it costs or where to see prices, an FAQ, and a call to action."},
|
||||
{"alternative", "Alternative to a competitor",
|
||||
"A \"<competitor> alternative\" page, for people searching for one. Fair and factual: why people look for an alternative, how we differ, a comparison (a table where the site has one), switching over, an FAQ, a call to action. Say only what you're given about the competitor; where a fact isn't known, leave it out."},
|
||||
{"comparison", "Comparison with a competitor",
|
||||
"A side-by-side \"<us> vs <competitor>\" page: a short verdict, a comparison table, who each one is best for (honestly, including when the competitor fits better), an FAQ, a call to action. Say only what you're given about the competitor."},
|
||||
{"blog", "Blog post",
|
||||
"A blog post that teaches something useful to our customers, mostly text sections, ending with one call to action."},
|
||||
}
|
||||
|
||||
// KindLabel is a kind's name for people.
|
||||
func KindLabel(id string) string {
|
||||
for _, k := range Kinds {
|
||||
if k.ID == id {
|
||||
return k.Label
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func kindGuide(id string) (string, bool) {
|
||||
for _, k := range Kinds {
|
||||
if k.ID == id {
|
||||
return k.Guide, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Source is what a page was built from.
|
||||
type Source struct {
|
||||
Product string `json:"product,omitempty"` // a product's name, from the profile
|
||||
Competitor int64 `json:"competitor,omitempty"` // an Inventory entry
|
||||
Signal int64 `json:"signal,omitempty"` // a buying signal
|
||||
Brief int64 `json:"brief,omitempty"` // an SEO brief
|
||||
}
|
||||
|
||||
// SitePage is a page built from the site's sections.
|
||||
type SitePage struct {
|
||||
ID int64
|
||||
Site string
|
||||
Repo string
|
||||
Kind string
|
||||
Source Source
|
||||
Notes string
|
||||
Path string
|
||||
Title string
|
||||
Description string
|
||||
Sections string // YAML: the front matter's sections list
|
||||
Needs []string
|
||||
Warnings []string
|
||||
Claims []string
|
||||
Status string
|
||||
Branch string
|
||||
File string
|
||||
PRNumber int64
|
||||
PRURL string
|
||||
Note string
|
||||
Error string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
MergedAt time.Time
|
||||
}
|
||||
|
||||
// Editable says whether the user can still change the page here.
|
||||
func (p *SitePage) Editable() bool {
|
||||
return p.Status == PageDraft || p.Status == PagePushed || p.Status == PagePR || p.Status == PageFailed
|
||||
}
|
||||
|
||||
const pageCols = `id, site, repo, kind, source, notes, path, title, description, sections, needs, warnings, claims,
|
||||
status, branch, file, pr_number, pr_url, note, error, created_at, updated_at, merged_at`
|
||||
|
||||
func scanPages(rows *sql.Rows) ([]SitePage, error) {
|
||||
defer rows.Close()
|
||||
var out []SitePage
|
||||
for rows.Next() {
|
||||
var p SitePage
|
||||
var src, needs, warns, claims, created, updated string
|
||||
var merged sql.NullString
|
||||
if err := rows.Scan(&p.ID, &p.Site, &p.Repo, &p.Kind, &src, &p.Notes, &p.Path, &p.Title, &p.Description, &p.Sections,
|
||||
&needs, &warns, &claims, &p.Status, &p.Branch, &p.File, &p.PRNumber, &p.PRURL, &p.Note, &p.Error,
|
||||
&created, &updated, &merged); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal([]byte(src), &p.Source)
|
||||
_ = json.Unmarshal([]byte(needs), &p.Needs)
|
||||
_ = json.Unmarshal([]byte(warns), &p.Warnings)
|
||||
_ = json.Unmarshal([]byte(claims), &p.Claims)
|
||||
p.CreatedAt, _ = time.Parse(time.RFC3339, created)
|
||||
p.UpdatedAt, _ = time.Parse(time.RFC3339, updated)
|
||||
if merged.Valid {
|
||||
p.MergedAt, _ = time.Parse(time.RFC3339, merged.String)
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func jsonText(v any) string {
|
||||
raw, _ := json.Marshal(v)
|
||||
if string(raw) == "null" {
|
||||
return "[]"
|
||||
}
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
// GetPage reads one page.
|
||||
func GetPage(d *db.DB, id int64) (*SitePage, error) {
|
||||
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ps, err := scanPages(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ps) == 0 {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return &ps[0], nil
|
||||
}
|
||||
|
||||
// Pages lists a website's pages, newest first.
|
||||
func Pages(d *db.DB, site string) ([]SitePage, error) {
|
||||
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE site = ? ORDER BY created_at DESC, id DESC`, site)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanPages(rows)
|
||||
}
|
||||
|
||||
// PagesIn lists pages in the given statuses, for every site.
|
||||
func PagesIn(d *db.DB, statuses ...string) ([]SitePage, error) {
|
||||
var all []SitePage
|
||||
for _, st := range statuses {
|
||||
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE status = ? ORDER BY id`, st)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ps, err := scanPages(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
all = append(all, ps...)
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// SavePage stores a page, new (ID 0) or not.
|
||||
func SavePage(d *db.DB, p *SitePage) error {
|
||||
now := time.Now().UTC()
|
||||
p.UpdatedAt = now
|
||||
var merged any
|
||||
if !p.MergedAt.IsZero() {
|
||||
merged = p.MergedAt.UTC().Format(time.RFC3339)
|
||||
}
|
||||
src, _ := json.Marshal(p.Source)
|
||||
if p.ID == 0 {
|
||||
if p.CreatedAt.IsZero() {
|
||||
p.CreatedAt = now
|
||||
}
|
||||
if p.Status == "" {
|
||||
p.Status = PageDraft
|
||||
}
|
||||
res, err := d.Exec(`INSERT INTO site_pages (site, repo, kind, source, notes, path, title, description, sections, needs, warnings,
|
||||
claims, status, branch, file, pr_number, pr_url, note, error, created_at, updated_at, merged_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
p.Site, p.Repo, p.Kind, string(src), p.Notes, p.Path, p.Title, p.Description, p.Sections, jsonText(p.Needs), jsonText(p.Warnings),
|
||||
jsonText(p.Claims), p.Status, p.Branch, p.File, p.PRNumber, p.PRURL, p.Note, p.Error,
|
||||
p.CreatedAt.UTC().Format(time.RFC3339), now.Format(time.RFC3339), merged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.ID, err = res.LastInsertId()
|
||||
return err
|
||||
}
|
||||
res, err := d.Exec(`UPDATE site_pages SET site = ?, repo = ?, kind = ?, source = ?, notes = ?, path = ?, title = ?, description = ?,
|
||||
sections = ?, needs = ?, warnings = ?, claims = ?, status = ?, branch = ?, file = ?, pr_number = ?, pr_url = ?, note = ?, error = ?,
|
||||
updated_at = ?, merged_at = ? WHERE id = ?`,
|
||||
p.Site, p.Repo, p.Kind, string(src), p.Notes, p.Path, p.Title, p.Description, p.Sections, jsonText(p.Needs), jsonText(p.Warnings),
|
||||
jsonText(p.Claims), p.Status, p.Branch, p.File, p.PRNumber, p.PRURL, p.Note, p.Error, now.Format(time.RFC3339), merged, p.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePage forgets a page. Its branch and pull request, if any, stay on
|
||||
// Gitea for the user to close.
|
||||
func DeletePage(d *db.DB, id int64) error {
|
||||
_, err := d.Exec(`DELETE FROM site_pages WHERE id = ?`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// RecoverPages puts pages left half-pushed (the app stopped) back to failed.
|
||||
func RecoverPages(d *db.DB) error {
|
||||
_, err := d.Exec(`UPDATE site_pages SET status = ?, error = ? WHERE status = ?`, PageFailed,
|
||||
"Supermarket stopped while pushing this page; push it again.", PageWorking)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user