- Untrack config.yaml (now gitignored) and add config.example.yaml with blank credentials; install.sh copies the example and sets mode 600 - Reserve room for the appended source link when asking Claude for a post, count length the way X does, and refuse over-length tweets before posting - Return https://x.com/i/status/<id> for posted tweets - Track which platforms each queued post reached so a partial failure can be retried without double-posting - Write config, state, posts and dismissed files atomically with 0600 - Keep refreshed Twitter tokens in memory if saving them fails, and log the failure instead of writing to the hidden stderr - Exit with a clear message on corrupt data files instead of panicking - Surface save errors in the UI; show status/error messages on home - Add tests for X length counting, the post store, and token updates Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
4.0 KiB
Go
113 lines
4.0 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
|
|
logFile string
|
|
}
|
|
|
|
func New(apiKey, model, stateDir string) *Client {
|
|
c := anthropic.NewClient(option.WithAPIKey(apiKey))
|
|
return &Client{
|
|
client: c,
|
|
model: model,
|
|
logFile: stateDir + "/coremarketing.log",
|
|
}
|
|
}
|
|
|
|
// GeneratePost creates a post in the voice of a product manager based on
|
|
// selected content. maxLength is the space available for the post text itself,
|
|
// excluding anything appended at posting time (such as the source URL).
|
|
func (c *Client) GeneratePost(ctx context.Context, app *models.Application, items []models.ContentItem, maxLength int) (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, maxLength)
|
|
|
|
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, maxLength int) 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", 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.",
|
|
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, 0600)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close()
|
|
fmt.Fprintf(f, "[%s] %s\n", time.Now().Format(time.RFC3339), fmt.Sprintf(format, args...))
|
|
}
|