2
0

format.go 2.4 KB

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