Initial push to gitea
This commit is contained in:
123
internal/summarize/anthropic.go
Normal file
123
internal/summarize/anthropic.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package summarize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Anthropic talks to the Claude Messages API directly via net/http to avoid an
|
||||
// SDK dependency. Requires ANTHROPIC_API_KEY (or APIKey set explicitly).
|
||||
type Anthropic struct {
|
||||
APIKey string
|
||||
Model string
|
||||
MaxTokens int
|
||||
BaseURL string // optional override; defaults to https://api.anthropic.com
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
func (a *Anthropic) Name() string { return "anthropic-api" }
|
||||
|
||||
type anthroMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type anthroRequest struct {
|
||||
Model string `json:"model"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
System string `json:"system,omitempty"`
|
||||
Messages []anthroMessage `json:"messages"`
|
||||
}
|
||||
|
||||
type anthroContentBlock struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type anthroResponse struct {
|
||||
Content []anthroContentBlock `json:"content"`
|
||||
Error *struct {
|
||||
Type string `json:"type"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (a *Anthropic) Summarize(ctx context.Context, systemPrompt, userContent string) (string, error) {
|
||||
key := a.APIKey
|
||||
if key == "" {
|
||||
key = os.Getenv("ANTHROPIC_API_KEY")
|
||||
}
|
||||
if key == "" {
|
||||
return "", fmt.Errorf("ANTHROPIC_API_KEY is not set")
|
||||
}
|
||||
model := a.Model
|
||||
if model == "" {
|
||||
model = "claude-sonnet-4-6"
|
||||
}
|
||||
maxTokens := a.MaxTokens
|
||||
if maxTokens == 0 {
|
||||
maxTokens = 4096
|
||||
}
|
||||
baseURL := a.BaseURL
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api.anthropic.com"
|
||||
}
|
||||
client := a.Client
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 5 * time.Minute}
|
||||
}
|
||||
|
||||
body := anthroRequest{
|
||||
Model: model,
|
||||
MaxTokens: maxTokens,
|
||||
System: systemPrompt,
|
||||
Messages: []anthroMessage{
|
||||
{Role: "user", Content: userContent},
|
||||
},
|
||||
}
|
||||
buf, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", baseURL+"/v1/messages", bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("x-api-key", key)
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return "", fmt.Errorf("anthropic API %d: %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
var out anthroResponse
|
||||
if err := json.Unmarshal(respBody, &out); err != nil {
|
||||
return "", fmt.Errorf("decoding anthropic response: %w", err)
|
||||
}
|
||||
if out.Error != nil {
|
||||
return "", fmt.Errorf("anthropic error: %s: %s", out.Error.Type, out.Error.Message)
|
||||
}
|
||||
|
||||
var text bytes.Buffer
|
||||
for _, c := range out.Content {
|
||||
if c.Type == "text" {
|
||||
text.WriteString(c.Text)
|
||||
}
|
||||
}
|
||||
return text.String(), nil
|
||||
}
|
||||
49
internal/summarize/claudecli.go
Normal file
49
internal/summarize/claudecli.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package summarize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ClaudeCLI shells out to the `claude` CLI in print mode. The transcript is
|
||||
// sent on stdin so we don't bump into ARG_MAX for very long services.
|
||||
type ClaudeCLI struct {
|
||||
// Bin is the binary name; defaults to "claude".
|
||||
Bin string
|
||||
// Model passes through to `claude --model`. Empty leaves the CLI default.
|
||||
Model string
|
||||
// ExtraArgs are appended verbatim before the prompt arg.
|
||||
ExtraArgs []string
|
||||
}
|
||||
|
||||
func (c *ClaudeCLI) Name() string { return "claude-cli" }
|
||||
|
||||
func (c *ClaudeCLI) Summarize(ctx context.Context, systemPrompt, userContent string) (string, error) {
|
||||
bin := c.Bin
|
||||
if bin == "" {
|
||||
bin = "claude"
|
||||
}
|
||||
if _, err := exec.LookPath(bin); err != nil {
|
||||
return "", fmt.Errorf("%q not on PATH: %w", bin, err)
|
||||
}
|
||||
|
||||
args := []string{"-p"}
|
||||
if c.Model != "" {
|
||||
args = append(args, "--model", c.Model)
|
||||
}
|
||||
args = append(args, c.ExtraArgs...)
|
||||
args = append(args, systemPrompt)
|
||||
|
||||
cmd := exec.CommandContext(ctx, bin, args...)
|
||||
cmd.Stdin = strings.NewReader(userContent)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", fmt.Errorf("%s: %w (stderr: %s)", bin, err, strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
return strings.TrimSpace(stdout.String()), nil
|
||||
}
|
||||
13
internal/summarize/summarize.go
Normal file
13
internal/summarize/summarize.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Package summarize turns a transcript + system prompt into a markdown summary.
|
||||
package summarize
|
||||
|
||||
import "context"
|
||||
|
||||
// Summarizer produces a markdown summary (or other generation) guided by
|
||||
// systemPrompt and given the user-message body. The body is passed verbatim:
|
||||
// callers are responsible for any framing like "Transcript:", "Producer's
|
||||
// notes:", or timestamped segment formatting.
|
||||
type Summarizer interface {
|
||||
Summarize(ctx context.Context, systemPrompt, userContent string) (string, error)
|
||||
Name() string
|
||||
}
|
||||
Reference in New Issue
Block a user