utils

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const TempPrefix = "gua-"

TempPrefix is the filename prefix for all gua temp files.

Variables

This section is empty.

Functions

func CleanFileName

func CleanFileName(name string) string

CleanFileName strips the "gua-{uuid}." prefix from temp file names.

func DetectImageFormat

func DetectImageFormat(data []byte) string

DetectImageFormat checks magic bytes to determine image format.

func DetectMIMEType

func DetectMIMEType(path string) string

DetectMIMEType returns the MIME type for a file path based on extension.

func IsRasterImage

func IsRasterImage(mimeType string) bool

IsRasterImage returns true for MIME types supported as images by most platforms.

func MergeJSONFile

func MergeJSONFile(path string, keys map[string]any, perm os.FileMode) error

MergeJSONFile reads an existing JSON file, deep-merges the provided keys into it, and writes back. If the file does not exist or is invalid JSON, starts fresh. Nested maps are recursively merged; other types are overwritten.

func NormalizeID

func NormalizeID(raw string) string

NormalizeID replaces @, ., and : with - for filesystem safety.

func ReadJSONFile

func ReadJSONFile[T any](path string) (*T, error)

ReadJSONFile reads a JSON file and unmarshals it into a value of type T.

func ShortID

func ShortID() string

ShortID returns a short random hex identifier (8 chars from UUID).

func TempDir

func TempDir() string

TempDir returns the OS temp directory used for gua temp files.

func TempFileRegex

func TempFileRegex() *regexp.Regexp

TempFileRegex returns a compiled regex matching gua temp file paths. Used by server/formatter to extract file paths from agent responses.

func TempFileRule

func TempFileRule() string

TempFileRule returns the file naming rule for agent prompts.

func TempPath

func TempPath(name, ext string) string

TempPath returns a unique temp file path: <os.TempDir()>/gua-<name>.<ext>

func TempPathRandom

func TempPathRandom(ext string) string

TempPathRandom returns a unique temp file path with a random ID.

func UnmergeJSONFile

func UnmergeJSONFile(path string, keyPaths ...[]string)

UnmergeJSONFile removes the specified keys from a JSON file. Keys are paths like ["mcpServers", "gua"] meaning delete existing["mcpServers"]["gua"]. If the parent map becomes empty after removal, it is also removed. If the file becomes empty or doesn't exist, the file is deleted.

func WriteJSONFile

func WriteJSONFile[T any](path string, v *T, perm os.FileMode) error

WriteJSONFile marshals a value to JSON and writes it to path. Parent directories are created as needed.

Types

type SyncQueue

type SyncQueue[T any] struct {
	// contains filtered or unexported fields
}

SyncQueue is a thread-safe FIFO queue.

func (*SyncQueue[T]) Drain

func (q *SyncQueue[T]) Drain() []T

Drain removes all items and returns them.

func (*SyncQueue[T]) Len

func (q *SyncQueue[T]) Len() int

Len returns the number of items in the queue.

func (*SyncQueue[T]) Peek

func (q *SyncQueue[T]) Peek() (T, bool)

Peek returns the front item without removing it. Returns zero value and false if the queue is empty.

func (*SyncQueue[T]) Pop

func (q *SyncQueue[T]) Pop() (T, bool)

Pop removes and returns the front item. Returns zero value and false if the queue is empty.

func (*SyncQueue[T]) Push

func (q *SyncQueue[T]) Push(v T)

Push appends an item to the back of the queue.

func (*SyncQueue[T]) Remove

func (q *SyncQueue[T]) Remove(match func(T) bool) bool

Remove removes the first item matching the predicate. Returns true if an item was removed.

type SyncValue

type SyncValue[T any] struct {
	// contains filtered or unexported fields
}

SyncValue is a thread-safe wrapper for a value of type T.

func (*SyncValue[T]) Clear

func (v *SyncValue[T]) Clear()

Clear resets the value to its zero value.

func (*SyncValue[T]) Get

func (v *SyncValue[T]) Get() T

Get returns the current value.

func (*SyncValue[T]) Set

func (v *SyncValue[T]) Set(val T)

Set updates the value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL