1package format
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7)
8
9// OutputFormat represents the output format type for non-interactive mode
10type OutputFormat string
11
12const (
13 // Text format outputs the AI response as plain text.
14 Text OutputFormat = "text"
15
16 // JSON format outputs the AI response wrapped in a JSON object.
17 JSON OutputFormat = "json"
18)
19
20// String returns the string representation of the OutputFormat
21func (f OutputFormat) String() string {
22 return string(f)
23}
24
25// SupportedFormats is a list of all supported output formats as strings
26var SupportedFormats = []string{
27 string(Text),
28 string(JSON),
29}
30
31// Parse converts a string to an OutputFormat
32func Parse(s string) (OutputFormat, error) {
33 s = strings.ToLower(strings.TrimSpace(s))
34
35 switch s {
36 case string(Text):
37 return Text, nil
38 case string(JSON):
39 return JSON, nil
40 default:
41 return "", fmt.Errorf("invalid format: %s", s)
42 }
43}
44
45// IsValid checks if the provided format string is supported
46func IsValid(s string) bool {
47 _, err := Parse(s)
48 return err == nil
49}
50
51// GetHelpText returns a formatted string describing all supported formats
52func GetHelpText() string {
53 return fmt.Sprintf(`Supported output formats:
54- %s: Plain text output (default)
55- %s: Output wrapped in a JSON object`,
56 Text, JSON)
57}
58
59// FormatOutput formats the AI response according to the specified format
60func FormatOutput(content string, formatStr string) (string, error) {
61 format, err := Parse(formatStr)
62 if err != nil {
63 format = Text
64 }
65
66 switch format {
67 case JSON:
68 return formatAsJSON(content)
69 case Text:
70 fallthrough
71 default:
72 return content, nil
73 }
74}
75
76// formatAsJSON wraps the content in a simple JSON object
77func formatAsJSON(content string) (string, error) {
78 // Use the JSON package to properly escape the content
79 response := struct {
80 Response string `json:"response"`
81 }{
82 Response: content,
83 }
84
85 jsonBytes, err := json.MarshalIndent(response, "", " ")
86 if err != nil {
87 return "", fmt.Errorf("failed to marshal output into JSON: %w", err)
88 }
89
90 return string(jsonBytes), nil
91}