Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

825 B
Raw Blame
// Package jsonresponse extracts JSON payloads from model responses.
package jsonresponse

import (
	"encoding/json"
	"strings"
)

// ExtractObject returns the first valid JSON object embedded in a model
// response. Models sometimes wrap JSON in markdown, inline code spans, or a
// leading language tag, so callers should not rely on the response being a
// clean JSON document.
func ExtractObject(response string) (string, bool) {
	response = strings.TrimSpace(response)
	if response == "" {
		return "", false
	}

	for i, r := range response {
		if r != '{' {
			continue
		}

		decoder := json.NewDecoder(strings.NewReader(response[i:]))
		var raw json.RawMessage
		if err := decoder.Decode(&raw); err != nil {
			continue
		}
		if len(raw) > 0 && raw[0] == '{' {
			return string(raw), true
		}
	}

	return "", false
}