(response string, prettyPrint bool)
| 509 | } |
| 510 | |
| 511 | func printResponse(response string, prettyPrint bool) error { |
| 512 | if !prettyPrint { |
| 513 | fmt.Println(response) |
| 514 | return nil |
| 515 | } |
| 516 | |
| 517 | // Parse the JSON response |
| 518 | var resp Response |
| 519 | if err := json.Unmarshal([]byte(response), &resp); err != nil { |
| 520 | return fmt.Errorf("failed to parse JSON: %w", err) |
| 521 | } |
| 522 | |
| 523 | // Extract text from content items of type "text" |
| 524 | for _, content := range resp.Result.Content { |
| 525 | if content.Type == "text" { |
| 526 | var textContentObj map[string]any |
| 527 | err := json.Unmarshal([]byte(content.Text), &textContentObj) |
| 528 | |
| 529 | if err == nil { |
| 530 | prettyText, err := json.MarshalIndent(textContentObj, "", " ") |
| 531 | if err != nil { |
| 532 | return fmt.Errorf("failed to pretty print text content: %w", err) |
| 533 | } |
| 534 | fmt.Println(string(prettyText)) |
| 535 | continue |
| 536 | } |
| 537 | |
| 538 | // Fallback parsing as JSONL |
| 539 | var textContentList []map[string]any |
| 540 | if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil { |
| 541 | return fmt.Errorf("failed to parse text content as a list: %w", err) |
| 542 | } |
| 543 | prettyText, err := json.MarshalIndent(textContentList, "", " ") |
| 544 | if err != nil { |
| 545 | return fmt.Errorf("failed to pretty print array content: %w", err) |
| 546 | } |
| 547 | fmt.Println(string(prettyText)) |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // If no text content found, print the original response |
| 552 | if len(resp.Result.Content) == 0 { |
| 553 | fmt.Println(response) |
| 554 | } |
| 555 | |
| 556 | return nil |
| 557 | } |
no outgoing calls
no test coverage detected