util

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EmojiDefault = "⭐"
	EmojiCustom  = "✏️"
	EmojiOpenAI  = "🤖"
	EmojiExit    = "👋"
	EmojiTool    = "🛠️"
	EmojiUpdate  = "🚀"
	EmojiBack    = "🔙"
)

Menu-specific emojis

View Source
const (
	ColorReset     = "\033[0m"
	ColorRed       = "\033[31m"
	ColorGreen     = "\033[32m"
	ColorYellow    = "\033[33m"
	ColorBlue      = "\033[36m"
	ColorGray      = "\033[90m"
	ColorBold      = "\033[1m"
	ColorLightBlue = "\033[94m"
)

Colors

View Source
const (
	EmojiRocket  = "🚀"
	EmojiStar    = "⭐"
	EmojiError   = "❌"
	EmojiCheck   = "✅"
	EmojiKey     = "🔑"
	EmojiGear    = "⚙️"
	EmojiLink    = "🔗"
	EmojiWave    = "👋"
	EmojiWarning = "⚠️"
	EmojiAPI     = "🔍"
	EmojiDone    = "🎯"
	EmojiSelect  = "🔘"

	// emoji for loading
	EmojiLoading = "🌐"

	EmojiCongratulation = "🎉"
	EmojiDiamond        = "💎"
)

Emojis

View Source
const (
	SeparatorChar  = "-"
	SeparatorWidth = 80
)

Common formatting

Variables

View Source
var (
	MainMenu = Menu{
		Title:      "GPT 检测工具",
		TitleEmoji: EmojiTool,
	}

	MenuKey = Menu{
		Title:      "请选择测试模型",
		TitleEmoji: EmojiGear,
		Items: []MenuItem{
			{ID: 1, Label: "使用默认模型", Emoji: EmojiDefault},
			{ID: 2, Label: "自定义测试模型", Emoji: EmojiCustom},
		},
		Prompt: "请输入选择或输入自定义模型(多个模型用空格分隔): ",
		ValidChoice: func(choice string) bool {
			return choice == "1" || choice == "2" || strings.Contains(choice, " ")
		},
	}

	MenuMain = Menu{
		Title:      "GPT 检测工具",
		TitleEmoji: EmojiTool,
		Description: fmt.Sprintf("%s项目地址: %shttps://github.com/go-coders/check-gpt%s",
			ColorGray, ColorBlue, ColorReset),
		Items: []MenuItem{
			{ID: 1, Label: "API Key 可用性测试", Emoji: EmojiKey},
			{ID: 2, Label: "API 中转链路检测", Emoji: EmojiLink},
			{ID: 3, Label: "检查更新", Emoji: EmojiGear},
			{ID: 4, Label: "退出", Emoji: EmojiExit},
		},
		Prompt: "请选择功能 (1-4): ",
		ValidChoice: func(choice string) bool {
			return choice >= "1" && choice <= "4"
		},
	}

	MenuUpdate = Menu{
		Title:      "检查更新",
		TitleEmoji: EmojiGear,
		Items: []MenuItem{
			{ID: 1, Label: "立即更新", Emoji: EmojiUpdate},
			{ID: 2, Label: "返回主菜单", Emoji: EmojiBack},
		},
		Prompt: "请选择操作: ",
		ValidChoice: func(choice string) bool {
			return choice == "1" || choice == "2"
		},
	}
)

Menu constants

View Source
var BasicColors = []ColorInfo{
	{Color: color.RGBA{R: 255, G: 0, B: 0, A: 255}, Name: "Red", ChineseName: "红色"},
	{Color: color.RGBA{R: 0, G: 255, B: 0, A: 255}, Name: "Green", ChineseName: "绿色"},
	{Color: color.RGBA{R: 0, G: 0, B: 255, A: 255}, Name: "Blue", ChineseName: "蓝色"},
	{Color: color.RGBA{R: 255, G: 255, B: 0, A: 255}, Name: "Yellow", ChineseName: "黄色"},
	{Color: color.RGBA{R: 255, G: 0, B: 255, A: 255}, Name: "Magenta", ChineseName: "品红色"},
	{Color: color.RGBA{R: 0, G: 255, B: 255, A: 255}, Name: "Cyan", ChineseName: "青色"},
	{Color: color.RGBA{R: 255, G: 165, B: 0, A: 255}, Name: "Orange", ChineseName: "橙色"},
	{Color: color.RGBA{R: 128, G: 0, B: 128, A: 255}, Name: "Purple", ChineseName: "紫色"},
	{Color: color.RGBA{R: 165, G: 42, B: 42, A: 255}, Name: "Brown", ChineseName: "棕色"},
}

BasicColors provides a list of basic colors with their names

Functions

func ClearConsole

func ClearConsole()

func FindAvailablePort

func FindAvailablePort(startPort int) int

FindAvailablePort finds an available port starting from the given port

func GenerateRandomDigits

func GenerateRandomDigits(length int) string

GenerateRandomDigits generates a string of random digits with the specified length

func GenerateRandomString

func GenerateRandomString(length int) string

GenerateRandomString generates a random string with the specified length

func GetPlatformInfo

func GetPlatformInfo(userAgent string, ip string, cidr []string) string

GetPlatformInfo extracts platform information from User-Agent

func GetSeparator

func GetSeparator() string

GetSeparator returns a separator line of standard width

func IsIPInCidr

func IsIPInCidr(ip string, cidr string) bool

func IsPortAvailable

func IsPortAvailable(port int) bool

IsPortAvailable checks if a port is available

func IsValidURL

func IsValidURL(input string) bool

func MaskKey

func MaskKey(key string, firstN, lastN int) string

MaskKey masks a key string by showing only the first and last n characters

func MaskString

func MaskString(s string) string

MaskString masks a string by showing only the first and last few characters

func Min

func Min(a, b int) int

Min returns the minimum of two integers

func NormalizeURL

func NormalizeURL(url string) string

normalizeURL ensures the URL ends with /v1/chat/completions for OpenAI-compatible APIs

Types

type APIResponse

type APIResponse struct {
	StatusCode int
	Error      error
	Response   string
}

APIResponse represents an API response

type AddressInfo

type AddressInfo struct {
	Original    string      // 原始输入
	Type        AddressType // 地址类型
	Scheme      string      // 协议 (http/https)
	Host        string      // 主机部分
	Port        string      // 端口
	IsValid     bool        // 是否有效
	ErrorDetail string      // 错误详情
}

AddressInfo 存储地址解析结果

type AddressType

type AddressType int

AddressType 表示地址类型

const (
	InvalidAddress AddressType = iota
	IPv4Address
	IPv6Address
	DomainAddress
	LocalhostAddress
)

func (AddressType) String

func (t AddressType) String() string

type ChatResponse

type ChatResponse struct {
	Choices []struct {
		Message struct {
			Content string `json:"content"`
		} `json:"message"`
		Delta struct {
			Content string `json:"content"`
		} `json:"delta"`
	} `json:"choices"`
}

ChatResponse represents a chat completion response

type Choice

type Choice struct {
	Message struct {
		Content string `json:"content"`
	} `json:"message"`
}

Choice represents a response choice

type Client

type Client struct {
	URL       string
	Key       string
	MaxTokens int
	Stream    bool
	Timeout   time.Duration
}

Client represents an API client

func NewClient

func NewClient(maxTokens int, stream bool, timeout time.Duration) *Client

NewClient creates a new API client

func (*Client) ChatRequest

func (c *Client) ChatRequest(ctx context.Context, contxt string, url, imageURL, key, model string) *APIResponse

ChatRequest sends a chat request to the API and returns the response

type ColorInfo

type ColorInfo struct {
	Color       color.RGBA
	Name        string
	ChineseName string
}

ColorInfo represents a basic color with its name ColorInfo represents a basic color with its name

func GenerateRandomImage

func GenerateRandomImage(width, height int) (image.Image, []ColorInfo)

GenerateRandomImage creates a random colored image with a pattern

func GetRandomUniqueColors

func GetRandomUniqueColors(n int) []ColorInfo

GetRandomUniqueColors returns n unique random colors from the basic colors

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		Message string `json:"message"`
		Type    string `json:"type"`
		Code    string `json:"code"`
	} `json:"error"`
}

ErrorResponse represents an API error response

type IPInfo

type IPInfo struct {
	Status     string `json:"status"`
	Country    string `json:"country"`
	RegionName string `json:"regionName"`
	City       string `json:"city"`
	ISP        string `json:"isp"`
	Query      string `json:"query"`
	Org        string `json:"org"`
}

IPInfo represents the response from IP-API

func GetIPInfo

func GetIPInfo(ip string) (*IPInfo, error)

GetIPInfo retrieves location and ISP information for an IP address

type ImageURL

type ImageURL struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

ImageURL represents an image URL

type Menu struct {
	Title       string
	TitleEmoji  string
	Description string
	Items       []MenuItem
	Prompt      string
	ValidChoice func(string) bool
}

Menu represents a menu with title and items

type MenuItem struct {
	ID       int
	Label    string
	Emoji    string
	Selected bool
}

MenuItem represents a menu item

func ShowMainMenu

func ShowMainMenu(in io.Reader, out io.Writer) (MenuItem, error)

ShowMainMenu displays the main menu and returns the user's choice

func ShowMenu

func ShowMenu(menu Menu, input io.Reader, output io.Writer) (MenuItem, error)

ShowMenu displays a menu and returns user's choice

type Message

type Message struct {
	Role    string           `json:"role"`
	Content []MessageContent `json:"content"`
}

Message represents a chat message

type MessageContent

type MessageContent struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	ImageURL *ImageURL `json:"image_url,omitempty"`
}

MessageContent represents the content of a message

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer handles output formatting with configurable writer

func NewPrinter

func NewPrinter(w io.Writer) *Printer

NewPrinter creates a new Printer with the given writer

func (*Printer) FormatTitle

func (p *Printer) FormatTitle(title string, emoji string) string

FormatTitle formats a title with an emoji

func (*Printer) Print

func (p *Printer) Print(args ...interface{})

Print prints a message

func (*Printer) PrintError

func (p *Printer) PrintError(message string)

PrintError prints an error message

func (*Printer) PrintSeparator

func (p *Printer) PrintSeparator()

PrintSeparator prints a separator line

func (*Printer) PrintSuccess

func (p *Printer) PrintSuccess(message string)

PrintSuccess prints a success message

func (*Printer) PrintTesting

func (p *Printer) PrintTesting()

func (*Printer) PrintTitle

func (p *Printer) PrintTitle(title string, emoji string)

PrintTitle prints a title with an emoji and separator

func (*Printer) PrintWarning

func (p *Printer) PrintWarning(message string)

PrintWarning prints a warning message

func (*Printer) Printf

func (p *Printer) Printf(format string, args ...interface{})

Printf formats and prints a message

func (*Printer) Println

func (p *Printer) Println(args ...interface{})

Println prints a message with a newline

type Request

type Request struct {
	Model       string    `json:"model"`
	Messages    []Message `json:"messages"`
	MaxTokens   int       `json:"max_tokens"`
	Stream      bool      `json:"stream"`
	Temperature float64   `json:"temperature"`
}

Request represents a chat request

type Response

type Response struct {
	Choices []Choice `json:"choices"`
}

Response represents a chat response

type StreamChoice

type StreamChoice struct {
	Delta struct {
		Content string `json:"content"`
	} `json:"delta"`
}

StreamChoice represents a streaming choice

type StreamResponse

type StreamResponse struct {
	Choices []StreamChoice `json:"choices"`
}

StreamResponse represents a streaming response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳