Phase 1, part 3: Reddit
- Sign in with a Reddit web app; access tokens refresh automatically, and an app-only token reads rules before sign-in. - Reddit drafts: a title and text for one subreddit, chosen when writing. The subreddit's guidelines and rules go into the prompt, which asks for a community member's post that discloses the affiliation and avoids marketing language. - The Inbox shows the subreddit's rules (cached for a day, with Refresh) and warns, without blocking, about promotion rules, restricted subreddits, link-only subreddits, posting in the same subreddit within a week, more than 1 in 10 recent posts linking to the company's sites, and new accounts. - Publishing makes a text post with the source link at the end, or a link post where the subreddit takes only links. - Migration 4 adds the draft's title and subreddit, and the rules cache. Publishers now take a Post, which carries the title and subreddit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,11 +2,13 @@ package content
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/api/reddit"
|
||||
"supermarket/internal/api/twitter"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/profile"
|
||||
@@ -18,6 +20,10 @@ type Request struct {
|
||||
Entry *inventory.Entry
|
||||
Items []Item
|
||||
Channels []string // channel IDs, in Channels order
|
||||
// Reddit: the subreddit to write for, and what's known about it (nil
|
||||
// if its rules couldn't be read).
|
||||
Subreddit string
|
||||
SubredditInfo *reddit.Subreddit
|
||||
}
|
||||
|
||||
// SourceURL is the link the drafts carry: the first item's.
|
||||
@@ -55,15 +61,30 @@ func Generate(ctx context.Context, client ai.Client, req Request) ([]Draft, erro
|
||||
if _, ok := ChannelByID(ch); !ok {
|
||||
return nil, fmt.Errorf("unknown channel %q", ch)
|
||||
}
|
||||
if ch == ChannelReddit && req.Subreddit == "" {
|
||||
return nil, errors.New("choose a subreddit to write the Reddit post for")
|
||||
}
|
||||
}
|
||||
src := req.SourceURL()
|
||||
|
||||
props := map[string]any{}
|
||||
for _, ch := range req.Channels {
|
||||
c, _ := ChannelByID(ch)
|
||||
if ch == ChannelReddit {
|
||||
props[ch] = map[string]any{
|
||||
"type": "object",
|
||||
"properties": map[string]any{
|
||||
"title": map[string]any{"type": "string", "description": "The post's title"},
|
||||
"body": map[string]any{"type": "string", "description": "The post's text"},
|
||||
},
|
||||
"required": []string{"title", "body"},
|
||||
"additionalProperties": false,
|
||||
}
|
||||
continue
|
||||
}
|
||||
props[ch] = map[string]any{"type": "string", "description": "The " + c.Name + " post"}
|
||||
}
|
||||
var out map[string]string
|
||||
var out map[string]json.RawMessage
|
||||
err := ai.GenerateJSON(ctx, client, ai.Request{
|
||||
Task: ai.TaskContent,
|
||||
System: systemPrompt(req.Profile),
|
||||
@@ -86,7 +107,24 @@ func Generate(ctx context.Context, client ai.Client, req Request) ([]Draft, erro
|
||||
}
|
||||
var drafts []Draft
|
||||
for _, ch := range req.Channels {
|
||||
text := strings.TrimSpace(out[ch])
|
||||
if ch == ChannelReddit {
|
||||
var rp struct{ Title, Body string }
|
||||
_ = json.Unmarshal(out[ch], &rp)
|
||||
if strings.TrimSpace(rp.Body) == "" || strings.TrimSpace(rp.Title) == "" {
|
||||
return nil, errors.New("the model wrote no Reddit title or text")
|
||||
}
|
||||
title := strings.Join(strings.Fields(rp.Title), " ")
|
||||
if r := []rune(title); len(r) > RedditTitleLimit {
|
||||
title = string(r[:RedditTitleLimit-1]) + "…"
|
||||
}
|
||||
drafts = append(drafts, Draft{InventoryName: req.Entry.Name, Kind: req.Entry.Relationship, Channel: ch,
|
||||
Title: title, Target: reddit.CleanName(req.Subreddit), Content: strings.TrimSpace(rp.Body),
|
||||
SourceURL: src, ItemIDs: ids, Status: StatusDraft})
|
||||
continue
|
||||
}
|
||||
var text string
|
||||
_ = json.Unmarshal(out[ch], &text)
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
return nil, fmt.Errorf("the model wrote nothing for %s", ch)
|
||||
}
|
||||
@@ -227,6 +265,8 @@ func userPrompt(req Request, src string) string {
|
||||
case ChannelLinkedIn:
|
||||
fmt.Fprintf(&b, "- linkedin: a LinkedIn post, usually 400 to 1,300 characters and never more than %d. "+
|
||||
"A strong first line, short paragraphs, then up to 3 hashtags on the last line. Plain text, no markdown.\n", limit)
|
||||
case ChannelReddit:
|
||||
b.WriteString(redditInstructions(req))
|
||||
}
|
||||
}
|
||||
if src != "" {
|
||||
@@ -238,3 +278,40 @@ func userPrompt(req Request, src string) string {
|
||||
func oneLine(s string) string {
|
||||
return strings.Join(strings.Fields(s), " ")
|
||||
}
|
||||
|
||||
// redditInstructions asks for a post that a community would welcome: Reddit
|
||||
// punishes marketing, so the rules are stricter than for other channels.
|
||||
func redditInstructions(req Request) string {
|
||||
var b strings.Builder
|
||||
sub := reddit.CleanName(req.Subreddit)
|
||||
fmt.Fprintf(&b, "- reddit: an object with a title (at most %d characters) and a body, for r/%s. "+
|
||||
"Write as a member of the community who works at %s, and say so plainly in the body (\"I work at %s\"). "+
|
||||
"Lead with what's useful or interesting to this community, not with the company. No hashtags, no emoji, no marketing language, "+
|
||||
"no calls to action. Usually 80 to 250 words, in Reddit's markdown. The title must read like a normal post title, not a headline ad. "+
|
||||
"Follow the subreddit's rules below; if they forbid promotion, write a genuine discussion post that doesn't promote anything.\n",
|
||||
RedditTitleLimit, sub, req.Profile.Name, req.Profile.Name)
|
||||
if info := req.SubredditInfo; info != nil {
|
||||
b.WriteString("<subreddit>\n")
|
||||
if info.Description != "" {
|
||||
fmt.Fprintf(&b, "About r/%s: %s\n", sub, oneLine(info.Description))
|
||||
}
|
||||
if info.SubmitText != "" {
|
||||
fmt.Fprintf(&b, "Posting guidelines: %s\n", oneLine(truncateRunes(info.SubmitText, 1500)))
|
||||
}
|
||||
for i, r := range info.Rules {
|
||||
if r.Kind == "comment" {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "Rule %d: %s. %s\n", i+1, r.Name, oneLine(truncateRunes(r.Description, 400)))
|
||||
}
|
||||
b.WriteString("</subreddit>\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func truncateRunes(s string, n int) string {
|
||||
if r := []rune(s); len(r) > n {
|
||||
return string(r[:n]) + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user