Files
supermarket/internal/api/claude/client.go
T
2026-04-29 06:55:00 -06:00

113 lines
3.9 KiB
Go

package claude
import (
"context"
"fmt"
"os"
"strings"
"time"
anthropic "github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
"coremarketing/internal/models"
)
const systemPrompt = `You are a product manager at a company that champions open source software. You write posts for X.com (Twitter) to help users get more value from open source SaaS applications.
Your voice:
- Knowledgeable but approachable — you know this product deeply and want to share that knowledge
- Practical — you share specific workflows, features, and tips users can immediately apply
- Community-minded — you reference the broader ecosystem and encourage engagement
- Authentic — no corporate buzzwords, write like a real PM sharing genuine insights
Your posts should:
- Lead with the most valuable insight or tip
- Be specific rather than generic
- Include a call to action when appropriate (question, try it yourself, share your experience)
- Stay within the character limit INCLUDING hashtags
- End with 2-3 relevant hashtags
- May include 1-2 emojis for visual interest
Output ONLY the post text. No explanations, no alternatives — just the post.`
type Client struct {
client *anthropic.Client
model string
maxLength int
logFile string
}
func New(apiKey, model, stateDir string, maxLength int) *Client {
c := anthropic.NewClient(option.WithAPIKey(apiKey))
return &Client{
client: c,
model: model,
maxLength: maxLength,
logFile: stateDir + "/coremarketing.log",
}
}
// GeneratePost creates a post in the voice of a product manager based on selected content.
func (c *Client) GeneratePost(ctx context.Context, app *models.Application, items []models.ContentItem) (string, error) {
// Combine system instructions with the user prompt in a single user message.
// This avoids SDK version ambiguity around the System parameter content block type.
fullPrompt := systemPrompt + "\n\n---\n\n" + c.buildUserPrompt(app, items)
msg, err := c.client.Messages.New(ctx, anthropic.MessageNewParams{
Model: anthropic.F(c.model),
MaxTokens: anthropic.F(int64(512)),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock(fullPrompt)),
}),
})
if err != nil {
c.log("GeneratePost error: %v", err)
return "", fmt.Errorf("claude API: %w", err)
}
for _, block := range msg.Content {
if block.Type == "text" {
return strings.TrimSpace(block.Text), nil
}
}
c.log("GeneratePost: no text block in response (stop_reason=%s)", msg.StopReason)
return "", fmt.Errorf("no text content in claude response (stop_reason=%s)", msg.StopReason)
}
func (c *Client) buildUserPrompt(app *models.Application, items []models.ContentItem) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Application: %s\n", app.Name))
sb.WriteString(fmt.Sprintf("Description: %s\n\n", app.Description))
sb.WriteString(fmt.Sprintf("Character limit: %d\n\n", c.maxLength))
sb.WriteString("Selected content from social media and community:\n\n")
for i, item := range items {
sb.WriteString(fmt.Sprintf("--- Item %d [%s] ---\n", i+1, string(item.Source)))
if item.Title != "" {
sb.WriteString(fmt.Sprintf("Title: %s\n", item.Title))
}
sb.WriteString(fmt.Sprintf("Author: @%s\n", item.AuthorHandle))
sb.WriteString(fmt.Sprintf("Date: %s\n", item.PublishedAt.Format("Jan 2, 2006")))
sb.WriteString(fmt.Sprintf("Content: %s\n", item.Body))
sb.WriteString(fmt.Sprintf("URL: %s\n\n", item.URL))
}
sb.WriteString(fmt.Sprintf(
"Based on this content, write ONE X.com post (max %d characters including hashtags) in the voice of a product manager helping users better use %s.",
c.maxLength, app.Name,
))
return sb.String()
}
func (c *Client) log(format string, args ...any) {
f, err := os.OpenFile(c.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
defer f.Close()
fmt.Fprintf(f, "[%s] %s\n", time.Now().Format(time.RFC3339), fmt.Sprintf(format, args...))
}