Updated with error handling and multiple answer responses

This commit is contained in:
2026-01-30 17:57:21 -06:00
parent ce5814b1f7
commit 102ff765cc

View File

@@ -33,9 +33,7 @@ type Response struct {
ID string `json:"id"` ID string `json:"id"`
Status string `json:"status"` Status string `json:"status"`
Output []struct { Output []struct {
Content []struct { Text string `json:"text"`
Text string `json:"text"`
} `json:"content"`
} `json:"output"` } `json:"output"`
} }
@@ -45,23 +43,59 @@ type Messenger struct {
prevID string prevID string
} }
func (m *Messenger) Send(input string) (string, error) { func (m *Messenger) Send(input string) ([]string, error) {
data, _ := json.Marshal(Request{input, m.model, m.prevID, map[string]string{"channel": "text"}}) data, err := json.Marshal(Request{input, m.model, m.prevID, map[string]string{"channel": "text"}})
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
resp, err := m.client.Post(apiURL, "application/json", bytes.NewBuffer(data)) resp, err := m.client.Post(apiURL, "application/json", bytes.NewBuffer(data))
if err != nil { if err != nil {
return "", err return nil, fmt.Errorf("failed to send request: %w", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) // Check HTTP status code
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("HTTP %d: %s - Response body: %s", resp.StatusCode, resp.Status, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var r Response var r Response
json.Unmarshal(body, &r) if err := json.Unmarshal(body, &r); err != nil {
return nil, fmt.Errorf("failed to parse JSON response: %w - Raw body: %s", err, string(body))
}
m.prevID = r.ID m.prevID = r.ID
if r.Status == "completed" && len(r.Output) > 0 && len(r.Output[0].Content) > 0 { // Detailed validation of response structure
return r.Output[0].Content[0].Text, nil if r.Status != "completed" {
return nil, fmt.Errorf("response status is '%s' (expected 'completed') - Response ID: %s, Raw body: %s", r.Status, r.ID, string(body))
} }
return "", fmt.Errorf("invalid response")
if len(r.Output) == 0 {
return nil, fmt.Errorf("response has no output - Response ID: %s, Status: %s, Output length: %d, Raw body: %s", r.ID, r.Status, len(r.Output), string(body))
}
// Collect all Output Text values
var texts []string
for i, content := range r.Output {
if content.Text == "" {
fmt.Printf("Warning: Output[%d] has empty text field\n", i)
}
texts = append(texts, content.Text)
}
if len(texts) == 0 {
return nil, fmt.Errorf("no text content found in response - Response ID: %s, Output items: %d", r.ID, len(r.Output))
}
return texts, nil
} }
func getConversations() []string { func getConversations() []string {
@@ -121,11 +155,11 @@ func showHistory(id string) {
} }
func main() { func main() {
var id, model string var id, model string
fmt.Println("\n░▀█▀░█░█░█▀█░░░█▄█░█▀▀░█▀▀░█▀▀░█▀▀░█▀█░█▀▀░█▀▀░█▀▄\n░░█░░▀▄▀░█▀█░░░█░█░█▀▀░▀▀█░▀▀█░█▀▀░█░█░█░█░█▀▀░█▀▄\n░▀▀▀░░▀░░▀░▀░░░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀░▀") fmt.Println("\n░▀█▀░█░█░█▀█░░░█▄█░█▀▀░█▀▀░█▀▀░█▀▀░█▀█░█▀▀░█▀▀░█▀▄\n░░█░░▀▄▀░█▀█░░░█░█░█▀▀░▀▀█░▀▀█░█▀▀░█░█░█░█░█▀▀░█▀▄\n░▀▀▀░░▀░░▀░▀░░░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀░▀")
// Check for recent conversations // Check for recent conversations
convs := getConversations() convs := getConversations()
if len(convs) > 0 { if len(convs) > 0 {
fmt.Println("Recent conversations:") fmt.Println("Recent conversations:")
@@ -155,8 +189,6 @@ fmt.Println("\n░▀█▀░█░█░█▀█░░░█▄█░█▀
m := &Messenger{&http.Client{Timeout: 10 * time.Second}, model, id} m := &Messenger{&http.Client{Timeout: 10 * time.Second}, model, id}
fmt.Println("Use ↑/↓ for history, 'quit' to exit\n") fmt.Println("Use ↑/↓ for history, 'quit' to exit\n")
histFile := "/tmp/messenger_temp.tmp" histFile := "/tmp/messenger_temp.tmp"
@@ -178,12 +210,15 @@ fmt.Println("\n░▀█▀░█░█░█▀█░░░█▄█░█▀
continue continue
} }
response, err := m.Send(input) responses, err := m.Send(input)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Printf("\n"+reset+"Error sending message:\n%s\n\n", err)
continue continue
} }
// Join all responses into a single string
response := strings.Join(responses, "\n")
if first && m.prevID != "" { if first && m.prevID != "" {
first = false first = false
rl.Close() rl.Close()
@@ -205,4 +240,4 @@ fmt.Println("\n░▀█▀░█░█░█▀█░░░█▄█░█▀
} }
fmt.Println("Goodbye!") fmt.Println("Goodbye!")
} }