terminal

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package terminal provides an interface to interact with a Generative AI model in a terminal-based chat application. It manages the chat session, user input, AI communication, and displays the chat history.

The package is designed to be simple to use with a focus on a clean user experience. It includes functionality to handle graceful shutdowns, manage chat history, simulate typing effects for AI responses, and supports executing various commands such as summarization, translation, and token counting.

This package also includes error handling and logging mechanisms to ensure robust application performance and ease of debugging.

Copyright (c) 2024 H0llyW00dzZ

Index

Constants

View Source
const (
	// GitHubAPIURL is the endpoint for the latest release information of the application.
	GitHubAPIURL      = "https://api.github.com/repos/H0llyW00dzZ/GoGenAI-Terminal-Chat/releases/latest"
	GitHubReleaseFUll = "https://api.github.com/repos/H0llyW00dzZ/GoGenAI-Terminal-Chat/releases/tags/%s"
	// CurrentVersion represents the current version of the application.
	CurrentVersion = "v0.9.0"
)

Defined List of GitHub API

View Source
const (
	SignalMessage                    = " Received an interrupt, shutting down gracefully..." // fix formatting ^C in linux/unix
	RecoverGopher                    = "%s - %s - %sRecovered from panic:%s %s%v%s"
	StackTracePanic                  = "\n%sStack Trace:\n%s%s"
	StackPossiblyTruncated           = "...stack trace possibly truncated...\n"
	ObjectHighLevelString            = "%s %s"   // Catch High level string
	ObjectHighLevelStringWithSpace   = "%s %s "  // Catch High level string with space
	ObjectHighLevelStringWithNewLine = "%s %s\n" // Catch High level string With NewLine
	ObjectTripleHighLevelString      = "%%%s%%"  // Catch High level triple string
	ObjectHighLevelContextString     = "%s\n%s"  // Catch High level context string
	ObjectHighLevelFMT               = "%s: %s"
	ObjectHighLevelTripleString      = "%s %s %s"
	// TimeFormat is tailored for AI responses, providing a layout conducive to formatting chat transcripts.
	TimeFormat      = "2006/01/02 15:04:05"
	OtherTimeFormat = "January 2, 2006 at 15:04:05"
	StripChars      = "---"
	NewLineChars    = '\n'
	// this animated chars is magic, it used to show the user that the AI is typing just like human would type
	AnimatedChars = "%c"
	// this model is subject to changed in future
	GeminiPro       = "gemini-1.0-pro"
	GeminiProVision = "gemini-pro-vision"
	// this may subject to changed in future for example can customize the delay
	TypingDelay = 60 * time.Millisecond
	// this clearing chat history in secret storage
	ChatHistoryClear = ColorHex95b806 + "All Chat history cleared." + ColorReset
	// reset total token usage
	ResetTotalTokenUsage = ColorHex95b806 + "Total token usage has been reset." + ColorReset
	// clear sys summary messagess
	ChatSysSummaryMessages = ColorHex95b806 + "All System Summary Messages have been cleared." + ColorReset
)

Defined constants for the terminal package

View Source
const (
	YouNerd = "🤓 You:"
	AiNerd  = "🤖 AI:"

	TokenEmoji             = "🪙  Token count:"
	StatisticsEmoji        = "📈 Total Token:"
	ShieldEmoji            = "☠️  Safety:"
	ContextPrompt          = "Hello! How can I assist you today?"
	ShutdownMessage        = "Shutting down gracefully..."
	ContextCancel          = "Context canceled, shutting down..." // sending a messages to gopher officer
	ANewVersionIsAvailable = StripChars + "\nA newer version is available: %s\n\n"
	ReleaseName            = "- %s\n\n"
	FullChangeLog          = DoubleAsterisk + "%s" + DoubleAsterisk + "\n"
	DummyMessages          = "Hello, AI! from @H0llyW00dzZ"
	// Better prompt instead of typing manually hahaha
	ApplicationName = "GoGenAI Terminal Chat"
	// Check Version Prompt commands
	YouAreusingLatest = StripChars + "\nThe user invoked the command: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"The current version of " + DoubleAsterisk + "%s" + DoubleAsterisk + " is: " + DoubleAsterisk + "%s" + DoubleAsterisk + ".\n" +
		"This is the latest version available.\n" +
		"Please inform the user that no update is necessary at this time." // Better Response for AI
	ReleaseNotesPrompt = StripChars + "\nThe user invoked the command: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"The current version of the application " + DoubleAsterisk + "%s" + DoubleAsterisk + " is: " + DoubleAsterisk + "%s" + DoubleAsterisk + ".\n" +
		"There is a newer version available: " + DoubleAsterisk + "%s" + DoubleAsterisk + ".\n\n" +
		"Details of the latest release:\n" +
		"- Release Name: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"- Published Date: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n\n" +
		"Release Notes:\n%s\n" // Better Response for AI
	// Quit Prompt commands
	ContextPromptShutdown = StripChars + "\nThe user has attempted the command: " + DoubleAsterisk + "%s" + DoubleAsterisk + " in " + DoubleAsterisk + "%s" + DoubleAsterisk + ".\n" +
		"AI, please provide an appropriate shutdown message."
	// Help Prompt commands
	HelpCommandPrompt = StripChars + "\n" + DoubleAsterisk + "This a System messages" + DoubleAsterisk + ":" + DoubleAsterisk + "%s" + DoubleAsterisk + "\n\n" +
		"The user attempted an command: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"Can you provide help information for the available commands?\n\n" +
		"List of Available Commands:\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " or " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Quit the application.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " or " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Show this help information.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + ": Check the application version.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + ": Set the safety level - " + DoubleAsterisk + "%s" + DoubleAsterisk + " (low), " + DoubleAsterisk + "%s" + DoubleAsterisk + " (default), " +
		DoubleAsterisk + "%s" + DoubleAsterisk + " (high), " + DoubleAsterisk + "%s" + DoubleAsterisk + " (unspecified), " + DoubleAsterisk + "%s" + DoubleAsterisk + " (none).\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " <text> " + DoubleAsterisk + "%s" + DoubleAsterisk + " <target language>: Translate text to the specified language.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + " <number>: Generate a random string of the specified length.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + ": Summarize a current conversation\n\n" +
		DoubleAsterisk + "Note" + DoubleAsterisk + ": When you summarize a current conversation, it will be displayed at the top of the chat history.\n\n" +
		DoubleAsterisk + "%s %s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Show the chat history.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Show the chat statistic.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Clear all system summary messages from the chat history.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + ": Clear all chat history and reset the total token usage count if enabled.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " <" + DoubleAsterisk + "model-name" + DoubleAsterisk + ">: Check the details of a specific AI model.\n" +
		DoubleAsterisk + "%s" + DoubleAsterisk + " " + DoubleAsterisk + "%s" + DoubleAsterisk + " <" + DoubleAsterisk + "path/file/data.txt" + DoubleAsterisk + "> or <" +
		DoubleAsterisk + "data.txt" + DoubleAsterisk + ">: Counts a token from the specified file.\n\n" +
		DoubleAsterisk + "Note" + DoubleAsterisk + ": The token count file feature supports multiple files simultaneously with the following extensions: " +
		dotMD + dotStringComma + dotTxt + dotStringComma + dotPng + dotStringComma +
		dotJpg + dotStringComma + dotJpeg + dotStringComma + dotWebp + dotStringComma +
		dotHeic + dotStringComma + dotHeif + ".\n\n" +
		DoubleAsterisk + "Additional Note" + DoubleAsterisk + ": There are no additional commands or HTML Markdown available " +
		"because this is a terminal application and is limited.\n"
	// TranslateCommandPrompt commands
	AITranslateCommandPrompt = DoubleAsterisk + "This a System messages" + DoubleAsterisk + ":" + DoubleAsterisk + "%s" + DoubleAsterisk + "\n\n" +
		"The user attempted an command: " + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"Can you translate requested by user?\n" +
		"Text:\n" + DoubleAsterisk + "%s" + DoubleAsterisk + "\n" +
		"Translate To:\n " + DoubleAsterisk + "%s" + DoubleAsterisk
)

Defined constants for language

View Source
const (
	QuitCommand        = ":quit"
	ShortQuitCommand   = ":q" // Short quit command
	VersionCommand     = ":checkversion"
	HelpCommand        = ":help"
	ShortHelpCommand   = ":h" // Short help command
	SafetyCommand      = ":safety"
	AITranslateCommand = ":aitranslate"
	LangArgs           = ":lang"
	CryptoRandCommand  = ":cryptorand"
	LengthArgs         = ":length"
	ShowCommands       = ":show"
	ChatCommands       = ":chat"
	SummarizeCommands  = ":summarize"
	ClearCommand       = ":clear"
	StatsCommand       = ":stats"
	TokenCountCommands = ":tokencount"
	FileCommands       = ":file"
	CheckModelCommands = ":checkmodel"
	PingCommand        = ":ping" // Currently marked as TODO
	PrefixChar         = ":"
	// List args
	ChatHistoryArgs = "history"
)

Defined constants for commands

Note: will add more in future based on the need, for example, to change the model, or to change the delay, another thing is syncing ai with goroutine (known as gopher)

View Source
const (
	ErrorGettingShutdownMessage                     = "Error getting shutdown message from AI: %v"
	ErrorHandlingCommand                            = "Error handling command: %v"
	ErrorCountingTokens                             = "Error counting tokens: %v\n"
	ErrorSendingMessage                             = "Error sending message to AI: %v"
	ErrorReadingUserInput                           = "Error reading user input: %v"
	ErrorFailedToFetchReleaseInfo                   = "Failed to fetch the latest release %s info: %v"
	ErrorReceivedNon200StatusCode                   = "[Retry Policy] [Github] [Check Version] Received non-200 status code: %v Skip Retrying" // Github non 500 lmao
	ErrorFailedToReadTheResponseBody                = "Failed to read the response body: %v"
	ErrorFaileduUnmarshalTheReleaseData             = "Failed to unmarshal the release data: %v"
	ErrorFailedTagToFetchReleaseInfo                = "Failed to fetch release info for tag '%s': %v"
	ErrorFailedTagUnmarshalTheReleaseData           = "Failed to unmarshal release data for tag '%s': %v"
	ErrorFailedTosendmessagesToAI                   = "Failed to send messages to AI: %v"
	ErrorFailedToCreateNewAiClient                  = "Failed to create new AI client: %v"
	ErrorFailedToStartAIChatSessionAttempt          = "Failed to start AI chat session, attempt %d/%d"
	ErrorFailedtoStartAiChatSessionAfter            = "Failed to start AI chat session after %d attempts"
	ErrorChatSessionisnill                          = "chat session is nil"
	ErrorFailedtoStartAiChatSession                 = "failed to start AI chat session"
	ErrorFailedToRenewSession                       = "Failed to renew session: %v"
	ErrorAiChatSessionStillNill                     = "AI chat session is still nil after renewal attempt"
	ErrorLowLevelFailedtoStartAiChatSession         = "failed to start a new AI chat session: %w"
	ErrorUserAttemptUnrecognizedCommandPrompt       = "**From System**:**%s**\n\nThe user attempted an unrecognized command: **%s**" // Better Response for AI
	ErrorFailedtoSendUnrecognizedCommandToAI        = "Failed to send unrecognized command to AI: %v"
	ErrorWhileTypingCommandArgs                     = "Invalid %s Command Arguments: %v"
	ErrorPingFailed                                 = "Ping failed: %v"
	ErrorUnrecognizedCommand                        = "Unrecognized command: %s"
	ErrorUnrecognizedSubCommand                     = "Unrecognized %s command sub commands/args : %s"
	ErrorLowLevelCommand                            = "command cannot be empty"
	ErrorUnknown                                    = "An error occurred: %v"
	ErrorUnknownSafetyLevel                         = "Unknown safety level: %s"
	ErrorInvalidAPIKey                              = "Invalid API key: %v"
	ErrorFailedToStartSession                       = "Failed To Start Session: %v"
	ErrorLowLevelNoResponse                         = "no response from AI service"
	ErrorLowLevelMaximumRetries                     = "[Retry Policy] maximum retries reached without success - %v" // low level
	ErrorLowLevelFailedToCountTokensAfterRetries    = "failed to count tokens after retries"                        // low level
	ErrorNonretryableerror                          = "[Retry Policy] Retry attempt failed due to a non-retryable error: %v"
	ErrorFailedToSendHelpMessage                    = "Failed to send help message: %v"
	ErrorFailedToSendHelpMessagesAfterRetries       = "Failed to send help message after retries" // low level
	ErrorFailedToSendShutdownMessage                = "Failed to send shutdown message: %v"
	ErrorFailedToSendVersionCheckMessage            = "Failed to send version check message: %v"
	ErrorFailedToSendVersionCheckMessageAfterReties = "Failed to send version check message after retries" // low level
	ErrorFailedToSendTranslationMessage             = "Failed to send translation message: %v"
	ErrorFailedToSendTranslationMessageAfterRetries = "Failed to send translation message after retries" // low level
	ErrorFailedToApplyModelConfiguration            = "failed to apply model configuration"              // low level
	ErrorMaxOutputTokenMustbe                       = "maxOutputTokens must be %d or higher, got %d"     // low level
	ErrorFailedToSendSummarizeMessage               = "Failed To Send Summarize Message: %v"
	ErrorFailedToSendSummarizeMessageAfterRetries   = "failed to send summarize message after retries" // low level
	ErrorFailedToReadFile                           = "Failed to read the file at %s: %v"
	ErrorFailedToCountTokens                        = "Failed to count tokens in the file at %s: %v"
	ErrorUnrecognizedSubcommandForTokenCount        = "Unrecognized subcommand for token count: %s"
	ErrorInvalidFileExtension                       = "Invalid file extension: %v"
	ErrorFileTypeNotSupported                       = "file type not supported: only %s files are allowed." // Low level error
	ErrorFailedToSendCommandToAI                    = "Failed to send command to AI: %v"
	ErrorVariableImageFileTypeNotSupported          = "image file type not supported: only %s files are allowed." // Low level error
	ErrorNoInputProvideForTokenCounting             = "no input provided for token counting"                      // low level error
	ErrorGopherEncounteredAnError                   = "Goroutine %d encountered an error: %w"
	ErrorFailedToRetriveModelInfo                   = "Failed to retrieve model info: %v"

	// List Error not because of this go codes, it literally google apis issue
	// that so bad can't handle this a powerful terminal
	Error500GoogleAPI    = "googleapi: Error 500:"
	ErrorGoogleInternal  = "Google Internal Error: %s"
	ErrorGenAiReceiveNil = "received a nil option function" // low level
	ErrorGenAI           = "GenAI Error: %v"
	// List Error Figlet include high and low level error
	ErrorStyleIsEmpty             = "style is empty"                   // low level
	ErrorCharacterNotFoundinStyle = "character %q not found in style"  // low level
	ErrorToASCIIArtbuildOutput    = "ToASCIIArt buildOutput error: %v" // High Level
	ErrorToASCIIArtcheckstyle     = "ToASCIIArt checkStyle error: %v"  // High Level
	// List Error Tools
	ErrorInvalidLengthArgs = "Invalid length argument: %v" // high level

	ErrorFailedtoGenerateRandomString = "Failed to generate random string: %v" // high level

	// List Other Error not because of this go codes
	// ErrorOtherAPI represents an error received from an external API server.
	// It indicates non-client-related issues, such as server-side errors (e.g., HTTP 500 errors) indicate that so fucking bad hahaha.
	ErrorOtherAPI = "Error: %s API server error: %v"
)

Defined List error message

View Source
const (
	SingleAsterisk          = "*"
	DoubleAsterisk          = "**"
	SingleBacktick          = "`"
	TripleBacktick          = "```"
	SingleUnderscore        = "_"
	SingleMinusSign         = "-"
	StringNewLine           = "\n"
	BinaryAnsiChar          = '\x1b'
	BinaryLeftSquareBracket = '['
	BinaryAnsiSquenseChar   = 'm'
	BinaryAnsiSquenseString = "m"
	BinaryRegexAnsi         = `\x1b\[[0-9;]*m`
	CodeBlockRegex          = "```\\w+"
	SanitizeTextAIResponse  = "\n---\n"
	// This regex pattern will match:
	//
	// 1. An asterisk followed by a space (e.g., "* ")
	//
	// 2. Italic text surrounded by single asterisks (e.g., "*italic*")
	//
	// 3. A leading asterisk followed by space and italic text (e.g., "* *italic*")
	//
	// Ref: https://go.dev/play/p/3APEsqfgUV-
	//
	// It uses a non-greedy match for the italic text and optional groups.
	ItalicTextRegex = `\*(\s\*\S.*?\S\*|\s|\S.*?\S\*)`
	// TODO
	StandaloneAsteriskAnsiRegexPattern = `(?m)(^|\s)\*(\s|$)`
)

Defined List of characters

View Source
const (
	DebugMode   = "DEBUG_MODE"
	DEBUGPREFIX = "🔎 DEBUG:"
	// Note: Currently only executing CMD,RetryPolicy, will add more later
	DEBUGEXECUTINGCMD = "Executing " +

		ColorHex95b806 + "%s" + ColorReset +
		" command with parts: " +

		ColorHex95b806 + "%#v" + ColorReset
	DEBUGRETRYPOLICY   = "Retry Policy Attempt %d: error occurred - %v"
	ShowPromptFeedBack = "SHOW_PROMPT_FEEDBACK"
	PROMPTFEEDBACK     = "Rating for category " + ColorHex95b806 + "%s" + ColorReset + ": " +
		ColorHex95b806 + "%s" + ColorReset
	ShowTokenCount  = "SHOW_TOKEN_COUNT"
	TokenCount      = ColorHex95b806 + "%d" + ColorReset + " tokens\n"
	TotalTokenCount = "usage of this Session " + ColorHex95b806 + "%d" + ColorReset + " tokens"
	// Note: This is separate from the main package and is used for the token counter. The token counter is external and not a part of the Gemini session.
	APIKey = "API_KEY"
)

Defined List of Environment variables

View Source
const (
	// Note: This is a prefix for the system
	SYSTEMPREFIX     = "⚙️ SYSTEM:"
	SystemSafety     = "Safety level set to " + ColorHex95b806 + "%s" + ColorReset + "."
	Low              = "low"
	Default          = "default"
	High             = "high"
	Unspecified      = "unspecified"
	None             = "none"
	MonitoringSignal = "Received signal: %v.\n"
	ShowChatHistory  = "Chat History:\n\n%s"
	SummarizePrompt  = StripChars + "\nIn 200 words or less, provide a brief summary of the ongoing discussion.\n" +
		"This summary will serve as a prompt for contextual reference in future interactions:\n\n"

	ListChatStats = statsEmoji + " List of Chat Statistics for This Session:\n\n" +
		youNerd + " User messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + "\n" +
		aiNerd + " AI messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + "\n" +
		sysEmoji + " System messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset
	InfoTokenCountFile = "The file " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset +
		" contains " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + " tokens."
	RetryingStupid500Error = "[Retry Policy] Retrying (" + ColorRed + "last error: %v" + ColorReset + ")" +
		" attempt number " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset
	// ModelFormat defines a template for displaying model information with color and bold formatting for placeholders.
	ModelFormat = "Model Name: " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + "\n" +
		"Model ID: " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + "\n" +
		"Version: " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + "\n" +
		"Description: " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + "\n" +
		"Supported Generation Methods: " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + "\n" +
		"Input Token Limit: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + "\n" +
		"Output Token Limit: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset
)

Defined Prefix System

View Source
const (
	CryptoRandLength    = "Length: %s"
	CryptoRandStringRes = "Random String: %s"
	CryptoRandRes       = "Length: %s\n\nRandom String: %s"
)

Defined Tools

View Source
const (
	// NOTE: ' is rune not a string
	G = 'G'
	V = 'V'
	N = 'N'
	P = 'P'
	D = 'D'

	A = "/ /_/ / /_/ / /_/ /  __/ / / / ___ |_/ /     "
	I = "\\____/\\____/\\____/\\___/_/ /_/_/  |_/___/     "
	// Blank Art
	Blank = "                                      "
)

ASCII Art

View Source
const (
	CurrentVersioN = "Current Version: " + ColorHex95b806 + CurrentVersion + ColorReset
	// Acknowledgment of the original author is appreciated as this project is developed in an open-source environment.
	Copyright = "Copyright (©️) 2024 @H0llyW00dzZ All rights reserved."
	TIP       = SingleAsterisk + ColorHex95b806 + " Use the commands " + ColorReset +
		BoldText + ColorYellow + ShortHelpCommand + ColorYellow +
		BoldText + ColorHex95b806 + " or " + ColorReset + BoldText + ColorYellow + HelpCommand + ColorReset +
		BoldText + ColorHex95b806 + " to display a list of available commands." + ColorReset
)

Text

View Source
const (
	ContextUserInvokeTranslateCommands = "Translating to %s: %s"
	SummaryPrefix                      = aiNerd + " 📝 📌 Summary of this discussion:\n\n"
)

Context RAM's labyrinth

View Source
const (
	// Note: By replacing the ANSI escape sequence from "\033" to "\x1b", might can avoid a rare bug that sometimes occurs on different machines,
	// although the original code works fine on mine (Author: @H0llyW00dzZ).
	ColorRed    = "\x1b[31m"
	ColorGreen  = "\x1b[32m"
	ColorYellow = "\x1b[33m"
	ColorBlue   = "\x1b[34m"
	ColorPurple = "\x1b[35m"
	ColorCyan   = "\x1b[36m"
	// ColorHex95b806 represents the color #95b806 using an ANSI escape sequence for 24-bit color.
	ColorHex95b806 = "\x1b[38;2;149;184;6m"
	// ColorCyan24Bit represents the color #11F0F7 using an ANSI escape sequence for 24-bit color.
	ColorCyan24Bit   = "\x1b[38;2;17;240;247m"
	ColorPurple24Bit = "\x1b[38;2;255;0;255m"
	ColorReset       = "\x1b[0m"
)

ANSI color codes

View Source
const (
	// bold text.
	BoldText = "\x1b[1m"
	// reset bold text formatting.
	ResetBoldText = "\x1b[22m"
	// italic text
	ItalicText = "\x1B[3m"
	// reset italic text formatting.
	ResetItalicText = "\x1B[23m"
)

ANSI Text Formatting.

View Source
const (
	FormatJPEG = "jpeg"
	FormatPNG  = "png"
	FormatHEIC = "heic"
	FormatHEIF = "heif"
	FormatWEBP = "webp"
)

mime formatting

View Source
const (
	Code500 = "500" // indicate that server so bad hahaha
)

List RestfulAPI Error

View Source
const (
	MinOutputTokens int32 = 20 // Define the minimum number of tokens as a constant
)

model configuration

Variables

View Source
var ASCIIColors = map[rune]string{
	G: BoldText + colors.ColorHex95b806,
	V: BoldText + colors.ColorCyan24Bit,
}

ASCIIColors Define a map for character Ascii colors

Deprecated: This variable is no longer used, and was replaced by NewASCIIArtStyle(). Consult NewASCIIArtStyle() for ASCII color configurations.

View Source
var ASCIIPatterns = map[rune][]string{

	G: {
		_G,
		_O,
		_GEN,
		A,
		I,
	},
	V: {
		Blank,
		Blank,
		Blank,

		CurrentVersioN,
		Copyright,
	},
}

ASCIIPatterns Define the ASCII patterns for the 'slant' font for the characters

Deprecated: This variable is no longer used, and was replaced by NewASCIIArtStyle(). Use NewASCIIArtStyle() for ASCII art styles and colors.

Functions

func ApplyBold deprecated added in v0.3.7

func ApplyBold(text string, delimiter string, color string) string

ApplyBold applies bold formatting to the provided text if the delimiter indicates bold.

Deprecated: This method is no longer used, and was replaced by ApplyFormatting. Refer to ApplyFormatting for text formatting options.

func ApplyFormatting added in v0.3.7

func ApplyFormatting(options FormattingOptions) string

ApplyFormatting applies text formatting based on the provided FormattingOptions. If the delimiter is recognized, it applies the appropriate ANSI formatting codes.

Parameters:

options FormattingOptions: The struct that contains the formatting options.

Returns:

string: The formatted text.

func ApplyItalic added in v0.8.1

func ApplyItalic(text string) string

ApplyItalic applies italic formatting to text surrounded by single asterisks. It uses the global italicPattern variable to identify and format italic text.

func ApplyOptions added in v0.5.6

func ApplyOptions(m *genai.GenerativeModel, configs ...ModelConfig) (bool, error)

ApplyOptions is a convenience function that applies a series of configuration options to a GenerativeModel. This allows for flexible and dynamic model configuration at runtime.

Parameters:

m *genai.GenerativeModel: The generative AI model to configure.
configs ...ModelConfig: A variadic number of configuration options.

Returns:

bool: A boolean indicating whether the options were applied successfully.
error: An error if any of the configuration options are nil.

func CheckLatestVersion added in v0.2.1

func CheckLatestVersion(currentVersion string) (isLatest bool, latestVersion string, err error)

CheckLatestVersion compares the current application version against the latest version available on GitHub. It fetches the latest release information from the repository specified by GitHubAPIURL and determines if an update is available.

Parameters:

currentVersion string: The version string of the currently running application.

Returns:

isLatest bool: A boolean indicating if the current version is the latest available.
latestVersion string: The tag name of the latest release, if newer than current; otherwise, an empty string.
err error: An error if the request fails or if there is an issue parsing the response.

func Colorize added in v0.1.6

func Colorize(options ColorizationOptions) string

Colorize applies ANSI color codes to the text surrounded by specified delimiters. It can process multiple delimiters, each with a corresponding color. The function can also conditionally retain or remove the delimiters in the final output.

Parameters:

options ColorizationOptions: A struct containing all the necessary options, including:
	- Text: The text to be colorized.
	- ColorPairs: A slice where each pair of elements represents a delimiter and its color.
	- KeepDelimiters: A map to indicate whether to keep the delimiter in the output.
	- Formatting: A map of delimiters to their corresponding ANSI formatting codes.

Returns:

string: The colorized text.

Note: This function may not work as expected in Windows Command Prompt due to its limited support for ANSI color codes. It is designed for terminals that support ANSI, such as those in Linux/Unix environments.

func ConfigureModel added in v0.5.8

func ConfigureModel(model *genai.GenerativeModel, opts ...ModelConfig)

ConfigureModel applies a series of configuration options to a GenerativeModel. This function is variadic, meaning it can accept multiple configuration options that will be applied in the order they are provided.

Parameters:

model *genai.GenerativeModel: The generative AI model to configure.
opts ...ModelConfig: A variadic number of configuration options.

Note: While currently unused, this function is marked as TODO. It will be utilized in the future to enhance scalability.

func DisplayModelInfo added in v0.8.9

func DisplayModelInfo(info *genai.ModelInfo)

DisplayModelInfo formats and logs the information about a generative AI model. It compiles the model's details into a single string and logs it using the logger.Any method for a consistent logging experience.

Parameters:

info *genai.ModelInfo: A pointer to the ModelInfo struct containing the model's metadata.

func FilterLanguageFromCodeBlock added in v0.4.9

func FilterLanguageFromCodeBlock(text string) string

FilterLanguageFromCodeBlock searches for Markdown code block delimiters with a language identifier (e.g., "```go") and removes the language identifier, leaving just the code block delimiters. This function is useful when the language identifier is not required, such as when rendering plain text or when the syntax highlighting is not supported.

The function uses a precompiled regular expression `filterCodeBlock` that matches the pattern of triple backticks followed by any word characters (representing the language identifier). It replaces this pattern with just the triple backticks, effectively stripping the language identifier from the code block.

Parameters:

text (string): The input text containing Markdown code blocks with language identifiers.

Returns:

string: The modified text with language identifiers removed from all code blocks.

Example:

input := "Here is some Go code:\n```go\nfmt.Println(\"Hello, World!\")\n```"
output := FilterLanguageFromCodeBlock(input)
// output now contains "Here is some Go code:\n```\nfmt.Println(\"Hello, World!\")\n```"

func GetEmbedding added in v0.1.2

func GetEmbedding(ctx context.Context, client *genai.Client, modelID, text string) ([]float32, error)

GetEmbedding computes the numerical embedding for a given piece of text using the specified generative AI model. Embeddings are useful for a variety of machine learning tasks, such as semantic search, where they can represent the meaning of text in a form that can be processed by algorithms.

Parameters:

ctx     context.Context: The context for controlling the lifetime of the request. It allows
                         the function to be canceled or to time out, and it carries request-scoped values.
client  *genai.Client:   The client used to interact with the generative AI service. It should be
                         already initialized and authenticated before calling this function.
modelID string:          The identifier for the embedding model to be used. This specifies which
                         AI model will generate the embeddings.
text    string:          The input text to be converted into an embedding.

Returns:

[]float32: An array of floating-point numbers representing the embedding of the input text.
error:     An error that may occur during the embedding process. If the operation is successful,
           the error is nil.

The function delegates the embedding task to the genai client's EmbeddingModel method and retrieves the embedding values from the response. It is the caller's responsibility to manage the lifecycle of the genai.Client, including its creation and closure.

Note: This function marked as TODO for now, since it is not used in the main because, a current version of chat system it's consider fully stable with better logic.

func HandleCommand added in v0.1.3

func HandleCommand(input string, session *Session) (bool, error)

HandleCommand interprets the user input as a command and executes the associated action. It uses a map of command strings to their corresponding handler functions to manage different commands and their execution. If the command is recognized, the respective handler is called; otherwise, an unknown command message is displayed.

Parameters:

input     string: The user input to be checked for commands.
session *Session: The current chat session for context.

Returns:

bool: A boolean indicating if the input was a command and was handled.
error: An error that may occur while handling the command.

func HandleUnrecognizedCommand deprecated added in v0.4.1

func HandleUnrecognizedCommand(command string, session *Session, parts []string) (bool, error)

HandleUnrecognizedCommand takes an unrecognized command and the current session, constructs a prompt to inform the AI about the unrecognized command, and sends this information to the AI service. This function is typically called when a user input is detected as a command but does not match any of the known command handlers.

Parameters:

command string: The unrecognized command input by the user.
session *Session: The current chat session containing state and context, including the AI client.

Returns:

bool: Always returns false as this function does not result in a command execution.
error: Returns an error if sending the message to the AI fails; otherwise, nil.

The function constructs an error prompt using the application's name and the unrecognized command, retrieves the current chat history, and sends this information to the AI service. If an error occurs while sending the message, the function logs the error and returns an error to the caller.

Deprecated: This method is no longer used, and was replaced by CommandRegistry. See CommandRegistry for handling unrecognized commands.

func IsANSISequence deprecated added in v0.3.5

func IsANSISequence(runes []rune, index int) bool

IsANSISequence checks if the current index in the rune slice is the start of an ANSI sequence.

Deprecated: This method is no longer used, and was replaced by SanitizeMessage. Consult SanitizeMessage for handling ANSI sequences in messages.

func IsLastMessage deprecated added in v0.5.9

func IsLastMessage(index int, messages []string) bool

IsLastMessage checks if the current index is the last message in the slice

Deprecated: This method is no longer used, and was replaced by separateSystemMessages. See separateSystemMessages for message handling logic.

func NewASCIIArtError added in v0.8.10

func NewASCIIArtError(message string) error

NewASCIIArtError creates a new ErrorASCIIArt with the given message.

func PrintANSISequence deprecated added in v0.3.5

func PrintANSISequence(runes []rune, index int) int

PrintANSISequence prints the full ANSI sequence without delay and returns the new index.

Deprecated: This method is no longer used, and was replaced by SanitizeMessage. See SanitizeMessage for current message processing.

func PrintAnotherVisualSeparator deprecated added in v0.4.9

func PrintAnotherVisualSeparator()

PrintAnotherVisualSeparator prints a visual separator to the standard output.

Deprecated: This method is no longer used, and was replaced by NewASCIIArtStyle(). NewASCIIArtStyle() should be used for visual separators.

func PrintPrefixWithTimeStamp added in v0.2.2

func PrintPrefixWithTimeStamp(prefix, message string)

PrintPrefixWithTimeStamp prints a message to the terminal, prefixed with a formatted timestamp. The timestamp is formatted according to the TimeFormat constant.

For example, with TimeFormat set to "2006/01/02 15:04:05" and the prefix "🤓 You: ", the output might be "2024/01/10 16:30:00 🤓 You:".

This function is designed for terminal outputs that benefit from a timestamped context, providing clarity and temporal reference for the message displayed.

The prefix parameter is appended to the timestamp and can be a log level, a descriptor, or any other string that aids in categorizing or highlighting the message.

func PrintTypingChat

func PrintTypingChat(message string, delay time.Duration)

PrintTypingChat simulates the visual effect of typing out a message character by character. It prints each character of a message to the standard output with a delay between each character to give the appearance of real-time typing.

Parameters:

message string: The message to be displayed with the typing effect.
delay time.Duration: The duration to wait between printing each character.

This function does not return any value. It directly prints to the standard output.

Note: This is particularly useful for simulating the Gopher's lifecycle (Known as Goroutines) events in a user-friendly manner. For instance, when a Gopher completes a task or job and transitions to a resting state, this function can print a message with a typing effect to visually represent the Gopher's "sleeping" activities.

func RecoverFromPanic deprecated added in v0.2.2

func RecoverFromPanic() func()

RecoverFromPanic returns a deferred function that recovers from panics within a goroutine or function, preventing the panic from propagating and potentially causing the program to crash. Instead, it logs the panic information using the standard logger, allowing for post-mortem analysis without interrupting the program's execution flow.

Usage:

defer terminal.RecoverFromPanic()()

The function returned by RecoverFromPanic should be called by deferring it at the start of a goroutine or function. When a panic occurs, the deferred function will handle the panic by logging its message and stack trace, as provided by the recover built-in function.

Deprecated: This method is deprecated and was replaced by logger.RecoverFromPanic. Refer to logger.RecoverFromPanic for the updated implementation.

func ReplaceTripleBackticks deprecated added in v0.4.10

func ReplaceTripleBackticks(text, placeholder string) string

ReplaceTripleBackticks replaces all occurrences of triple backticks with a placeholder.

Deprecated: This method is no longer used, and was replaced by Colorize(). Refer to Colorize() for replacing triple backticks in text.

func SendDummyMessage added in v0.4.2

func SendDummyMessage(client *genai.Client) (bool, error)

SendDummyMessage verifies the validity of the API key by sending a dummy message.

Parameters:

client *genai.Client: The AI client used to send the message.

Returns:

A boolean indicating the validity of the API key.
An error if sending the dummy message fails.

func SingleCharColorize added in v0.1.8

func SingleCharColorize(text string, delimiter string, color string) string

SingleCharColorize applies ANSI color codes to text surrounded by single-character delimiters. It is particularly useful when dealing with text that contains list items or other elements that should be highlighted, and it ensures that the colorization is only applied to the specified delimiter at the beginning of a line.

Parameters:

text      string: The text containing elements to be colorized.
delimiter string: The single-character delimiter indicating the start of a colorizable element.
color     string: The ANSI color code to be applied to the elements starting with the delimiter.

Returns:

string: The resulting string with colorized elements as specified by the delimiter.

This function handles each line separately and checks for the presence of the delimiter at the beginning after trimming whitespace. If the delimiter is found, it colorizes the delimiter and the following character (typically a space). The rest of the line remains unaltered. If the delimiter is not at the beginning of a line, the line is added to the result without colorization.

Note: As with the Colorize function, SingleCharColorize may not function correctly in Windows Command Prompt or other environments that do not support ANSI color codes. It is best used in terminals that support these codes, such as most Linux/Unix terminals.

func ToASCIIArt added in v0.4.5

func ToASCIIArt(input string, style ASCIIArtStyle) (string, error)

ToASCIIArt converts a string to its ASCII art representation using a given style. Each character in the input string is mapped to an ASCIIArtChar based on the provided style, and the resulting ASCII art is constructed line by line. If a character in the input string does not have an ASCII art representation in the style, it will be omitted from the output.

The function returns the complete ASCII art as a string, where each line of the art is separated by a newline character. If the style is empty or a character is not found in the style, an error is returned.

Example usage:

art, err := ToASCIIArt("G", slantStyle)
if err != nil {
    log.Fatal(err)
}
fmt.Println(art)

Parameters:

input - The string to be converted into ASCII art.
style - The ASCIIArtStyle used to style the ASCII art representation.

Returns:

A string representing the ASCII art of the input text, and an error if the style is empty
or a character is not found in the style.

Types

type ANSIColorCodes added in v0.2.9

type ANSIColorCodes struct {
	ColorRed         string
	ColorGreen       string
	ColorYellow      string
	ColorBlue        string
	ColorPurple      string
	ColorCyan        string
	ColorHex95b806   string // 24-bit color
	ColorCyan24Bit   string // 24-bit color
	ColorPurple24Bit string // 24-bit color
	ColorReset       string
}

ANSIColorCodes defines a struct for holding ANSI color escape sequences.

type ASCIIArtChar added in v0.4.6

type ASCIIArtChar struct {
	Pattern []string // Lines of the ASCII representation of the character.
	Color   string   // Color code or label for the character's color.
}

ASCIIArtChar represents a styled ASCII character with its pattern and color. The Pattern field is a slice of strings, with each string representing a line of the character's ASCII representation. The Color field specifies the color to be used when displaying the character.

type ASCIIArtStyle added in v0.4.6

type ASCIIArtStyle map[rune]ASCIIArtChar

ASCIIArtStyle maps runes to their corresponding ASCIIArtChar representations. It defines the styling for each character that can be rendered in ASCII art.

func MergeStyles added in v0.4.6

func MergeStyles(styles ...ASCIIArtStyle) ASCIIArtStyle

MergeStyles combines multiple ASCIIArtStyle objects into a single style. In case of overlapping characters, the patterns from the styles appearing later in the arguments will overwrite those from earlier ones. This function is useful when you want to create a composite style that includes patterns from multiple sources or to override specific characters' styles in a base style.

Parameters:

styles ...ASCIIArtStyle - A variadic slice of ASCIIArtStyle objects to be merged.

Returns:

A new ASCIIArtStyle object that is the union of all provided styles.

func NewASCIIArtStyle added in v0.4.6

func NewASCIIArtStyle() ASCIIArtStyle

NewASCIIArtStyle creates and returns a new ASCIIArtStyle map. It initializes an empty map that can be populated with ASCII art characters using the AddChar method or by direct assignment.

func (ASCIIArtStyle) AddChar added in v0.4.6

func (style ASCIIArtStyle) AddChar(char rune, pattern []string, color string)

AddChar adds a new character with its ASCII art representation to the style. If the character already exists in the style, its pattern and color are updated with the new values provided.

Parameters:

char    - The rune representing the character to add or update.
pattern - A slice of strings representing the ASCII art pattern for the character.
color   - A string representing the color for the character.

type BinaryAnsiChars added in v0.3.0

type BinaryAnsiChars struct {
	BinaryAnsiChar          rune
	BinaryAnsiSquenseChar   rune
	BinaryAnsiSquenseString string
	BinaryLeftSquareBracket rune
}

BinaryAnsiChars is a struct that contains the ANSI characters used to print the typing effect.

type ChatConfig added in v0.5.5

type ChatConfig struct {
	// HistorySize specifies the total number of chat messages to retain in the session's history.
	// This helps in limiting the memory footprint and ensures that only recent interactions
	// are considered for maintaining context.
	HistorySize int

	// HistorySendToAI indicates the number of recent messages from the history to be included
	// when sending context to the AI. This allows the AI to generate responses that are
	// relevant to the current conversation flow without being overwhelmed by too much history.
	HistorySendToAI int
}

ChatConfig encapsulates settings that affect the management of chat history during a session with the generative AI. It determines the amount of chat history retained in memory and the portion of that history used to provide context to the AI.

func DefaultChatConfig added in v0.5.5

func DefaultChatConfig() *ChatConfig

DefaultChatConfig constructs a new ChatConfig with pre-defined default values. These defaults are chosen to balance the need for context awareness by the AI and efficient memory usage. The function is useful for initializing chat sessions with standard behavior without requiring manual configuration.

Returns:

*ChatConfig: A pointer to a ChatConfig instance populated with default settings.

type ChatHistory

type ChatHistory struct {
	Messages           []string       // Messages contains all the chat messages in chronological order.
	Hashes             map[string]int // Hashes maps the SHA-256 hash of each message to its index in Messages.
	UserMessageCount   int            // UserMessageCount holds the total number of user messages.
	AIMessageCount     int            // AIMessageCount holds the total number of AI messages.
	SystemMessageCount int            // SystemMessageCount holds the total number of system messages.
	// contains filtered or unexported fields
}

ChatHistory manages the state of chat messages exchanged during a session. It tracks the messages, their unique hashes, and counts of different types of messages (user, AI, system). This struct also ensures concurrent access safety using a read-write mutex.

func NewChatHistory added in v0.4.8

func NewChatHistory() *ChatHistory

NewChatHistory creates and initializes a new ChatHistory struct. It sets up an empty slice for storing messages and initializes the hash map used to track unique messages. A new, random seed is generated for hashing to ensure the uniqueness of hash values across different instances.

Returns:

*ChatHistory: A pointer to the newly created ChatHistory struct ready for use.

func (*ChatHistory) AddMessage

func (h *ChatHistory) AddMessage(user string, text string, config *ChatConfig)

AddMessage appends a new message to the chat history. It takes the username and the text of the message as inputs and formats them before adding to the Messages slice.

Parameters:

user string: The username of the individual sending the message.
text string: The content of the message to be added to the history.
config *ChatConfig: Configuration parameters for the chat session, including history size.

This method does not return any value or error. It assumes that all input is valid and safe to add to the chat history.

func (*ChatHistory) Clear added in v0.3.2

func (h *ChatHistory) Clear()

Clear removes all messages from the chat history, effectively resetting it.

func (*ChatHistory) ClearAllSystemMessages added in v0.5.10

func (h *ChatHistory) ClearAllSystemMessages()

ClearAllSystemMessages removes all system messages from the chat history.

func (*ChatHistory) FilterMessages added in v0.4.8

func (h *ChatHistory) FilterMessages(predicate func(string) bool) []string

FilterMessages returns a slice of messages that match the predicate function.

Parameters:

predicate func(string) bool: A function that returns true for messages that should be included.

Returns:

[]string: A slice of messages that match the predicate.

TODO: Filtering Messages

func (*ChatHistory) GetHistory

func (h *ChatHistory) GetHistory(config *ChatConfig) string

GetHistory concatenates all messages in the chat history into a single string, with each message separated by a newline character. This provides a simple way to view the entire chat history as a single text block.

Returns:

string: A newline-separated string of all messages in the chat history.

func (*ChatHistory) GetMessageStats added in v0.6.3

func (h *ChatHistory) GetMessageStats() *MessageStats

GetMessageStats safely retrieves the message counts from the ChatHistory instance. It returns a MessageStats struct containing the counts of user, AI, and system messages. Access to the ChatHistory's message counts is read-locked to ensure thread safety.

func (*ChatHistory) HasSystemMessages added in v0.7.4

func (h *ChatHistory) HasSystemMessages() bool

HasSystemMessages checks if there are any system messages in the chat history.

func (*ChatHistory) IncrementMessageTypeCount deprecated added in v0.6.0

func (h *ChatHistory) IncrementMessageTypeCount(messageType MessageType) bool

IncrementMessageTypeCount updates the count of messages for the given type. It specifically flags if a SystemMessage is encountered, as it may require special handling. Returns true if the incremented message type is a system message.

Deprecated: This method is no longer used, and was replaced by ChatHistory.AddMessage. Consult ChatHistory.AddMessage for message type counting and handling.

func (*ChatHistory) PrintHistory deprecated

func (h *ChatHistory) PrintHistory()

PrintHistory outputs all messages in the chat history to the standard output, one message per line. This method is useful for displaying the chat history directly to the terminal.

Each message is printed in the order it was added, preserving the conversation flow. This method does not return any value or error.

Deprecated: This method is deprecated and was replaced by GetHistory. See GetHistory for current functionality.

func (*ChatHistory) RemoveMessages added in v0.2.5

func (h *ChatHistory) RemoveMessages(numMessages int, messageContent string)

RemoveMessages removes messages from the chat history. If a specific message is provided, it removes messages that contain that text; otherwise, it removes the specified number of most recent messages.

Parameters:

numMessages int: The number of most recent messages to remove. If set to 0 and a specific
                 message is provided, all instances of that message are removed.
messageContent string: The specific content of messages to remove. If empty, it removes
                       the number of most recent messages specified by numMessages.

This method does not return any value. It updates the chat history in place.

Note: This currently marked as TODO since it's not used anywhere in the code. It's a good idea to add this feature in the future. Also it used to be maintain the RAM's labyrinth hahaha and automated handle by Garbage Collector.

func (*ChatHistory) SanitizeMessage added in v0.3.3

func (h *ChatHistory) SanitizeMessage(message string) string

SanitizeMessage removes ANSI color codes and other non-content prefixes from a message.

Parameters:

message string: The message to be sanitized.

Returns:

string: The sanitized message.

type ChatWorker added in v0.4.4

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

ChatWorker is responsible for handling background tasks related to chat sessions.

func NewChatWorker added in v0.4.4

func NewChatWorker(session *Session) *ChatWorker

NewChatWorker creates a new ChatWorker for a given chat session.

func (*ChatWorker) Start added in v0.4.4

func (cw *ChatWorker) Start(ctx context.Context) error

Start begins the background work loop of the ChatWorker.

func (*ChatWorker) Stop added in v0.4.4

func (cw *ChatWorker) Stop() error

Stop signals the ChatWorker to stop its work loop.

type ColorizationOptions added in v0.7.5

type ColorizationOptions struct {
	Text           string            // Text is the original string that will be colorized.
	ColorPairs     []string          // ColorPairs is a slice where each pair consists of a delimiter and its associated ANSI color code.
	KeepDelimiters map[string]bool   // KeepDelimiters dictates whether each delimiter should be kept in the final output.
	Formatting     map[string]string // Formatting is a map associating delimiters with their ANSI formatting codes for additional text styling.
}

ColorizationOptions encapsulates the settings necessary for the Colorize function to apply color to text. It includes the text to be colorized, pairs of delimiters and their corresponding colors, a map to determine if delimiters should be retained, and a map defining additional ANSI formatting codes.

type ColorizationPartOptions added in v0.7.5

type ColorizationPartOptions struct {
	Text           string            // Text is the string that will be processed for colorization.
	Delimiter      string            // Delimiter is the string that marks the beginning and end of text to be colorized.
	Color          string            // Color is the ANSI color code that will be applied to the text within delimiters.
	KeepDelimiters map[string]bool   // KeepDelimiters is a map indicating whether to keep each delimiter in the output text.
	Formatting     map[string]string // Formatting is a map of delimiters to their corresponding ANSI formatting codes.
}

ColorizationPartOptions holds the options for colorizing parts of a text. It contains the text to be colorized, the delimiter that marks the text to be colorized, the color to apply, and maps that determine whether to keep delimiters and how to format the text.

type CommandHandler added in v0.1.10

type CommandHandler interface {
	// Note: The list of command handlers here does not use os.Args; instead, it employs advanced idiomatic Go practices. 🤪
	Execute(session *Session, parts []string) (bool, error)                             // new method
	IsValid(parts []string) bool                                                        // new method
	HandleSubcommand(subcommand string, session *Session, parts []string) (bool, error) // New method
}

CommandHandler defines the function signature for handling chat commands. Each command handler function must conform to this signature.

type CommandRegistry added in v0.4.1

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

CommandRegistry is a centralized registry to manage chat commands. It maps command names to their corresponding CommandHandler implementations. This allows for a scalable and maintainable way to manage chat commands and their execution within a chat session.

func NewCommandRegistry added in v0.4.1

func NewCommandRegistry() *CommandRegistry

NewCommandRegistry initializes a new instance of CommandRegistry. It creates a CommandRegistry with an empty map ready to register command handlers.

Returns:

*CommandRegistry: A pointer to a newly created CommandRegistry with initialized command map.

func (*CommandRegistry) ExecuteCommand added in v0.4.1

func (r *CommandRegistry) ExecuteCommand(name string, session *Session, parts []string) (bool, error)

ExecuteCommand looks up and executes a command based on its name. It first validates the command arguments using the IsValid method of the command handler. If the command is valid, it executes the command using the Execute method. If the command name is not registered, it logs an error.

Parameters:

name    string: The name of the command to execute.
session *Session: The current chat session, providing context for the command execution.
parts   []string: The arguments passed along with the command.

Returns:

bool: A boolean indicating if the command execution should terminate the session.
error: An error if one occurs during command validation or execution. Returns nil if no error occurs.

Note:

If the command is unrecognized, it logs an error but does not return it,
as the error is already handled within the method.

func (*CommandRegistry) Register added in v0.4.1

func (r *CommandRegistry) Register(name string, cmd CommandHandler)

Register adds a new command and its associated handler to the registry. If a command with the same name is already registered, it will be overwritten.

Parameters:

name string: The name of the command to register.
cmd  CommandHandler: The handler that will be associated with the command.

func (*CommandRegistry) RegisterSubcommand added in v0.6.2

func (r *CommandRegistry) RegisterSubcommand(baseCommand, subcommand string, handler CommandHandler)

RegisterSubcommand for a base command.

type DebugOrErrorLogger added in v0.1.9

type DebugOrErrorLogger struct {
	PrintTypingChat func(string, time.Duration)
	// contains filtered or unexported fields
}

DebugOrErrorLogger provides a simple logger with support for debug and error logging. It encapsulates a standard log.Logger and adds functionality for conditional debug logging and colorized error output.

func NewDebugOrErrorLogger added in v0.1.9

func NewDebugOrErrorLogger() *DebugOrErrorLogger

NewDebugOrErrorLogger initializes a new DebugOrErrorLogger with a logger that writes to os.Stderr with the standard log flags.

Returns:

*DebugOrErrorLogger: A pointer to a newly created DebugOrErrorLogger.

func (*DebugOrErrorLogger) Any added in v0.6.5

func (l *DebugOrErrorLogger) Any(format string, v ...interface{})

Any logs a general message without any colorization. It behaves like Println and allows for formatted messages.

Parameters:

format string: The format string for the general message.
v ...interface{}: The values to be formatted according to the format string.

func (*DebugOrErrorLogger) Debug added in v0.1.9

func (l *DebugOrErrorLogger) Debug(format string, v ...interface{})

Debug logs a formatted debug message if the DEBUG_MODE environment variable is set to "true". It behaves like Printf and allows for formatted messages.

Parameters:

format string: The format string for the debug message.
v ...interface{}: The values to be formatted according to the format string.

func (*DebugOrErrorLogger) Error added in v0.1.9

func (l *DebugOrErrorLogger) Error(format string, v ...interface{})

Error logs a formatted error message in red color to signify error conditions. It behaves like Println and allows for formatted messages.

Parameters:

format string: The format string for the error message.
v ...interface{}: The values to be formatted according to the format string.

func (*DebugOrErrorLogger) HandleGoogleAPIError added in v0.4.4

func (l *DebugOrErrorLogger) HandleGoogleAPIError(err error) bool

HandleGoogleAPIError checks for Google API server errors and logs them. If it's a server error, it returns true, indicating a retry might be warranted.

Parameters:

err error: The error returned from a Google API call.

Returns:

bool: Indicates whether the error is a server error (500).

func (*DebugOrErrorLogger) HandleOtherStupidAPIError added in v0.5.0

func (l *DebugOrErrorLogger) HandleOtherStupidAPIError(err error, apiName string) bool

HandleOtherStupidAPIError checks for non-Google API 500 server errors and logs them. If it's a server error, it returns true, indicating a retry might be warranted.

Parameters:

err error: The error returned from an API call.
apiName string: The name of the API for logging purposes.

Returns:

bool: Indicates whether the error is a bad server error (500).

func (*DebugOrErrorLogger) Info added in v0.4.7

func (l *DebugOrErrorLogger) Info(format string, v ...interface{})

Info logs a formatted information message. It behaves like Println and allows for formatted messages.

Parameters:

format string: The format string for the information message.
v ...interface{}: The values to be formatted according to the format string.

func (*DebugOrErrorLogger) RecoverFromPanic added in v0.2.2

func (l *DebugOrErrorLogger) RecoverFromPanic()

RecoverFromPanic should be deferred at the beginning of a function or goroutine to handle any panics that may occur. It logs the panic information with a colorized output to distinguish the log message clearly in the terminal.

The message "Recovered from panic:" is displayed in green, followed by the panic value in red and the stack trace. This method ensures that the panic does not cause the program to crash and provides a clear indication in the logs that a panic was caught and handled.

Usage:

func someFunction() {
    logger := terminal.NewDebugOrErrorLogger()
    defer logger.RecoverFromPanic()

    // ... function logic that might panic ...
}

It is essential to call this method using defer right after obtaining a logger instance. This ensures that it can catch and handle panics from anywhere within the scope of the function or goroutine.

type ErrorASCIIArt added in v0.8.10

type ErrorASCIIArt struct {
	Message string
}

ErrorASCIIArt is a custom error type for errors related to ASCII art conversion.

func (ErrorASCIIArt) Error added in v0.8.10

func (e ErrorASCIIArt) Error() string

Error returns the error message associated with the ErrorASCIIArt error.

type ErrorHandlerFunc added in v0.5.0

type ErrorHandlerFunc func(error) bool

ErrorHandlerFunc is a type that represents a function that handles an error and decides whether the operation should be retried.

type FormattingOptions added in v0.7.5

type FormattingOptions struct {
	Text       string            // Text is the string to which formatting will be applied.
	Delimiter  string            // Delimiter is the string used to identify segments of text for formatting.
	Color      string            // Color is the ANSI color code that will be applied to the text within the delimiter.
	Formatting map[string]string // Formatting is a map that associates delimiters with their respective ANSI formatting codes.
}

FormattingOptions encapsulates the settings required to apply formatting to a text segment. It includes the text to be formatted, the delimiter used to identify the text segment, the color to apply, and a map specifying the ANSI formatting codes associated with different delimiters.

type GitHubRelease added in v0.2.1

type GitHubRelease struct {
	TagName string `json:"tag_name"`     // The tag associated with the release, e.g., "v1.2.3"
	Name    string `json:"name"`         // The official name of the release
	Body    string `json:"body"`         // Detailed description or changelog for the release
	Date    string `json:"published_at"` // Published Date
}

GitHubRelease represents the metadata of a software release from GitHub. It includes information such as the tag name, release name, and a description body, typically containing the changelog or release notes.

func (*GitHubRelease) GetFullReleaseInfo added in v0.8.3

func (r *GitHubRelease) GetFullReleaseInfo(tagName string) error

GetFullReleaseInfo retrieves detailed information about a specific release from GitHub. It constructs the request URL based on the provided tag name and fetches the data from the GitHub API.

Parameters:

tagName: The name of the tag for which release information is requested.

Returns:

error: An error if the request fails or if there is an issue parsing the response.

type MessageStats added in v0.6.3

type MessageStats struct {
	UserMessages   int // UserMessages is the count of messages sent by users.
	AIMessages     int // AIMessages is the count of messages sent by the AI.
	SystemMessages int // SystemMessages is the count of system-generated messages.
}

MessageStats encapsulates the counts of different types of messages in the chat history. It holds separate counts for user messages, AI messages, and system messages.

type MessageType added in v0.6.0

type MessageType int

MessageType categorizes the source of a chat message.

const (
	// UserMessage indicates a message that originates from a human user.
	UserMessage MessageType = iota // magic
	// AIMessage indicates a message that originates from an AI or bot.
	AIMessage
	// SystemMessage indicates a message that provides system-level information.
	SystemMessage
)

func DetermineMessageType added in v0.6.0

func DetermineMessageType(message string) MessageType

DetermineMessageType analyzes the content of a message to classify its type. It returns the MessageType based on predefined criteria for identifying user, AI, and system messages.

type ModelConfig added in v0.5.8

type ModelConfig func(m *genai.GenerativeModel)

ModelConfig defines a function type for configuring a GenerativeModel. Functions of this type take a pointer to a GenerativeModel and apply specific settings to it.

func WithMaxOutputTokens added in v0.5.6

func WithMaxOutputTokens(maxOutputTokens int32) (ModelConfig, error)

WithMaxOutputTokens creates a ModelConfig function to set the maximum number of output tokens for a GenerativeModel. This parameter limits the length of the AI's responses.

Parameters:

maxOutputTokens int32: The maximum number of tokens to set.

Returns:

ModelConfig: A function that sets the maximum number of output tokens when applied to a model.
error: An error if maxOutputTokens is below 20.

func WithSafetyOptions added in v0.7.1

func WithSafetyOptions(safety *SafetySettings, modelName string) ModelConfig

WithSafetyOptions creates a ModelConfig function to set the safety options for a GenerativeModel. It allows the application of model-specific safety settings based on the provided modelName, enabling fine-grained control over content safety for different AI models.

Parameters:

safety *SafetySettings: The safety settings to apply to the model.
modelName string: The identifier for the generative AI model to which the safety settings
                   will be applied. This allows for different safety configurations for
                   different models.

Returns:

ModelConfig: A function that, when applied to a model, sets the safety options based on
             the provided safety settings and model name.

Note: This is currently marked as TODO, as it's not used anywhere in the code. However, it would be advantageous to implement this feature in the future. For instance, it could be used with Vertex AI that require model-specific safety configurations.

func WithTemperature added in v0.5.6

func WithTemperature(temperature float32) ModelConfig

WithTemperature creates a ModelConfig function to set the temperature of a GenerativeModel. Temperature controls the randomness of the AI's responses, with higher values leading to more varied output.

Parameters:

temperature float32: The temperature value to set.

Returns:

ModelConfig: A function that sets the temperature when applied to a model.

func WithTopK added in v0.5.6

func WithTopK(topK int32) ModelConfig

WithTopK creates a ModelConfig function to set the top_k parameter of a GenerativeModel. Top_k restricts the sampling pool to the k most likely tokens, where a lower value increases the likelihood of high-probability tokens.

Parameters:

topK int32: The top_k value to set.

Returns:

ModelConfig: A function that sets the top_k value when applied to a model.

func WithTopP added in v0.5.6

func WithTopP(topP float32) ModelConfig

WithTopP creates a ModelConfig function to set the top_p parameter of a GenerativeModel. Top_p controls the nucleus sampling strategy, where a smaller value leads to less randomness in token selection.

Parameters:

topP float32: The top_p value to set.

Returns:

ModelConfig: A function that sets the top_p value when applied to a model.

type NewLineChar added in v0.3.0

type NewLineChar struct {
	NewLineChars rune
}

NewLineChar is a struct that containt Rune for New Line Character

type RetryableFunc added in v0.4.10

type RetryableFunc func() (bool, error)

RetryableFunc is a type that represents a function that can be retried.

type RetryableOperation added in v0.8.3

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

RetryableOperation encapsulates an operation that may need to be retried upon failure. It contains a retryFunc of type RetryableFunc, which is a function that performs the actual operation and returns a success flag along with an error if one occurred.

The retryFunc is designed to be idempotent, meaning that it can be called multiple times without causing unintended side effects, which is essential for a function that may be retried.

The RetryableOperation struct is typically used with a retry strategy function such as retryWithExponentialBackoff, which takes the operation and applies a backoff algorithm to perform retries with increasing delays, reducing the load on the system and increasing the chance of recovery from transient errors.

Example:

operation := RetryableOperation{
    retryFunc: func() (bool, error) {
        // Perform some operation that can fail transiently.
        result, err := SomeOperationThatMightFail()
        return result != nil, err
    },
}

success, err := operation.retryWithExponentialBackoff(standardAPIErrorHandler)

if err != nil {
    // Handle error after final retry attempt.
}

Use cases for RetryableOperation include network requests, database transactions, or any other operations that might fail temporarily due to external factors.

type SafetyOption added in v0.4.3

type SafetyOption struct {
	Setter func(s *SafetySettings)
	Valid  bool
}

SafetyOption is a function type that takes a pointer to a SafetySettings instance and applies a specific safety configuration to it. It is used to abstract the different safety level settings (e.g., low, high, default) and allows for a flexible and scalable way to manage safety configurations through function mapping.

type SafetySetter deprecated added in v0.4.2

type SafetySetter func(*SafetySettings)

SafetySetter defines a function signature for functions that modify the safety settings of a generative AI model. It takes a pointer to a SafetySettings struct and applies specific safety configurations to it. This type is used to abstract the modification of safety settings so that different configurations can be applied without changing the struct directly.

Deprecated: This type is deprecated and has been replaced by the SafetyOption struct. Refer to SafetyOption for safety settings configurations.

type SafetySettings added in v0.3.8

type SafetySettings struct {
	// DangerousContentThreshold defines the threshold for filtering dangerous content.
	DangerousContentThreshold genai.HarmBlockThreshold
	// HarassmentContentThreshold defines the threshold for filtering harassment-related content.
	HarassmentContentThreshold genai.HarmBlockThreshold
	// SexuallyExplicitContentThreshold defines the threshold for filtering sexually explicit content.
	SexuallyExplicitContentThreshold genai.HarmBlockThreshold
	// MedicalThreshold defines the threshold for filtering medical-related content.
	MedicalThreshold genai.HarmBlockThreshold
	// ViolenceThreshold defines the threshold for filtering violent content.
	ViolenceThreshold genai.HarmBlockThreshold
	// HateSpeechThreshold defines the threshold for filtering hate speech.
	HateSpeechThreshold genai.HarmBlockThreshold
	// ToxicityThreshold defines the threshold for filtering toxic content.
	ToxicityThreshold genai.HarmBlockThreshold
	// DerogatoryThershold defines the threshold for filtering derogatory content.
	DerogatoryThershold genai.HarmBlockThreshold
}

SafetySettings encapsulates the content safety configuration for the AI model. It defines thresholds for various categories of potentially harmful content, allowing users to set the desired level of content filtering based on the application's requirements and user preferences.

func DefaultSafetySettings added in v0.3.8

func DefaultSafetySettings() *SafetySettings

DefaultSafetySettings returns a SafetySettings instance with a default configuration where all categories are set to block medium and above levels of harmful content. This default setting provides a balanced approach to content safety, suitable for general use cases.

func (*SafetySettings) ApplyToModel added in v0.3.8

func (s *SafetySettings) ApplyToModel(model *genai.GenerativeModel, modelName string)

ApplyToModel applies the configured safety settings to a given generative AI model. This method updates the model's safety settings to match the thresholds specified in the SafetySettings instance, affecting how the model filters generated content.

func (*SafetySettings) SetHighSafety added in v0.3.8

func (s *SafetySettings) SetHighSafety()

SetHighSafety raises the safety settings to a higher threshold, providing stricter content filtering. This setting is useful in environments that require a high degree of content moderation to ensure user safety or to comply with strict regulatory standards.

func (*SafetySettings) SetLowSafety added in v0.3.8

func (s *SafetySettings) SetLowSafety()

SetLowSafety adjusts the safety settings to a lower threshold, allowing more content through the filter. This setting may be appropriate for environments where content restrictions can be more relaxed, or where users are expected to handle a wider range of content types.

func (*SafetySettings) SetNoneSafety added in v0.8.4

func (s *SafetySettings) SetNoneSafety()

SetNoneSafety sets the safety settings to no threshold, which means no content is blocked. This setting may be appropriate for environments where content moderation is not required or where users are expected to handle all types of content without any filtering.

func (*SafetySettings) SetUnspecifiedSafety added in v0.8.4

func (s *SafetySettings) SetUnspecifiedSafety()

SetUnspecifiedSafety sets the safety settings to an unspecified threshold, which means no specific blocking level is set. This setting may be appropriate for environments where the default AI model behavior is desired without additional filters.

type Session

type Session struct {
	Client         *genai.Client      // Client is the generative AI client used to communicate with the AI model.
	ChatHistory    *ChatHistory       // ChatHistory stores the history of the chat session.
	ChatConfig     *ChatConfig        // ChatConfig contains the settings for managing the chat history size.
	Ctx            context.Context    // Ctx is the context governing the session, used for cancellation.
	Cancel         context.CancelFunc // Cancel is a function to cancel the context, used for cleanup.
	Ended          bool               // Ended indicates whether the session has ended.
	SafetySettings *SafetySettings    // Holds the current safety settings for the session.
	// contains filtered or unexported fields
}

Session encapsulates the state and functionality for a chat session with a generative AI model. It holds the AI client, chat history, and context for managing the session lifecycle.

func NewSession

func NewSession(apiKey string) *Session

NewSession creates a new chat session with the provided API key for authentication. It initializes the generative AI client and sets up a context for managing the session.

Parameters:

apiKey string: The API key used for authenticating requests to the AI service.

Returns:

*Session: A pointer to the newly created Session object.

func (*Session) ConfigureModelForSession added in v0.8.2

func (s *Session) ConfigureModelForSession(ctx context.Context, client *genai.Client, modelName string) *genai.GenerativeModel

ConfigureModelForSession prepares and configures a generative AI model for use in a chat session. It applies safety settings from the session to the model and sets additional configuration options such as temperature. This function is essential for ensuring that the AI model behaves according to the desired safety guidelines and operational parameters before engaging in a chat session.

Parameters:

ctx context.Context: A context.Context that carries deadlines, cancellation signals, and other request-scoped
       values across API boundaries and between processes.
client *genai.Client: A pointer to a genai.Client, which provides the functionality to interact with the
          generative AI service.
modelName string: The identifier for the generative AI model to be used in the session. This name
                   is used to apply specific configurations and safety settings tailored to the
                   particular AI model.

Returns:

*genai.GenerativeModel: A pointer to a generative AI model that is configured and ready for
                          initiating a chat session.

Note: The function assumes that the client has been properly initialized and that the session contains valid safety settings. If no safety settings are present in the session, default safety settings are applied. The modelName parameter allows for model-specific configuration, enabling more granular control over the behavior and safety of different AI models.

func (*Session) HasEnded added in v0.2.2

func (s *Session) HasEnded() (ended bool)

HasEnded reports whether the chat session has ended. It can be called at any point to check the session's state without altering it.

Return value:

ended bool: A boolean indicating true if the session has ended, or false if it is still active.

TODO: Utilize this in multiple goroutines, such as for task queues, terminal control, etc.

func (*Session) RenewSession added in v0.2.2

func (s *Session) RenewSession(apiKey string) error

RenewSession attempts to renew the client session with the AI service by reinitializing the genai.Client with the provided API key. This method is useful when the existing client session has expired or is no longer valid and a new session needs to be established to continue communication with the AI service.

The method ensures thread-safe access by using a mutex lock during the client reinitialization process. If a client session already exists, it is properly closed and a new client is created.

Parameters:

apiKey string: The API key used for authenticating requests to the AI service.

Returns:

error: An error object if reinitializing the client fails. If the operation is successful,
       the error is nil.

Upon successful completion, the Session's Client field is updated to reference the new genai.Client instance. In case of failure, an error is returned and the Client field is set to nil.

func (*Session) SendMessage added in v0.7.8

func (s *Session) SendMessage(ctx context.Context, client *genai.Client, chatContext string) (string, error)

SendMessage sends a chat message to the generative AI model and retrieves the response. It constructs a chat session using the provided `genai.Client`, which is used to communicate with the AI service. The function simulates a chat interaction by sending the chat context, which may include a portion of the previous chat history determined by the session's ChatConfig, to the AI model for generating a response.

Parameters:

ctx context.Context: The context for controlling the cancellation of the request.
client *genai.Client: The client instance used to create a generative model session and send messages to the AI model.
chatContext string: The chat context or message to be sent to the AI model.

Returns:

string: The AI's response as a string, which includes the AI's message with a simulated typing effect.
error: An error message if the message sending or response retrieval fails. If the operation is successful,
       the error is nil.

The function initializes a new chat session and sends the chat context, along with the portion of chat history specified by the session's ChatConfig, to the generative AI model. It then calls `printResponse` to process and print the AI's response. The final AI response is returned as a concatenated string of all parts from the AI response.

func (*Session) Start

func (s *Session) Start()

Start begins the chat session, managing user input and AI responses. It sets up a signal listener for graceful shutdown and enters a loop to read user input and fetch AI responses indefinitely until an interrupt signal is received.

This method handles user input errors and AI communication errors by logging them and exiting. It ensures resources are cleaned up properly on exit by deferring the cancellation of the session's context and the closure of the AI client.

type TokenCountParams added in v0.7.2

type TokenCountParams struct {
	// Authentication key for the AI service.
	APIKey string
	// Name of the AI model to use.
	ModelName string
	// Text input for token counting.
	Input string
	// Format of the image if provided (e.g., "png", "jpeg").
	ImageFormat string
	// Image data as a byte slice.
	ImageData [][]byte // Image data as a slice of byte slices, each representing an image.
}

TokenCountParams encapsulates the parameters needed for counting tokens using a generative AI model.

func (*TokenCountParams) CountTokens added in v0.8.3

func (p *TokenCountParams) CountTokens() (int, error)

CountTokens uses a generative AI model to count the number of tokens in the provided text input or image data. It returns the token count and any error encountered in the process. A new client is created and closed within the function.

type TokenCountRequest added in v0.8.7

type TokenCountRequest struct {
	Ctx    context.Context
	Model  *genai.GenerativeModel
	Texts  []string
	Images [][]byte
}

TokenCountRequest encapsulates the parameters required for concurrent token counting in text and images using a generative AI model. It is designed to be passed to functions that initiate multiple goroutines for processing text and image data in parallel.

Fields:

Ctx:    A context.Context that carries deadlines, cancellation signals, and other
        request-scoped values across API boundaries and between processes.
        It is used to control the cancellation of the token counting process.

Model:  A pointer to an instance of genai.GenerativeModel, which provides methods
        for counting tokens in text and image data. The model is expected to be
        pre-initialized and ready for use.

Texts:  A slice of strings, where each string represents a piece of text for which
        the token count is to be determined. Each element in the slice will be
        processed concurrently by a separate goroutine.

Images: A slice of byte slices, where each byte slice represents image data for which
        the token count is to be determined. The image data format should be compatible
        with the generative AI model's expectations. Similar to 'Texts', each image
        will be processed concurrently by its own goroutine.

Example Usage:

req := TokenCountRequest{
    Ctx:    ctx,
    Model:  model,
    Texts:  []string{"Hello, world!", "Go is awesome."},
    Images: [][]byte{imageData1, imageData2},
}
// Pass 'req' to functions that process the request.

Note:

The caller is responsible for ensuring that the context and the model provided are
valid and that the context has not been canceled before initiating the token counting
process. The 'Images' field should contain properly formatted image data that the
model can process. If either 'Texts' or 'Images' is nil or empty, the corresponding
token counting operations for that data type will not be performed.

type TypingChars added in v0.3.0

type TypingChars struct {
	AnimatedChars string
}

TypingChars is a struct that contains the Animated Chars used to print the typing effect.

type TypingPrinter added in v0.8.10

type TypingPrinter struct {
	// PrintFunc is a function that, when called, prints the provided message with a delay
	// between each character to simulate typing. The function signature matches that of
	// PrintTypingChat, allowing it to be set as the default implementation.
	PrintFunc func(string, time.Duration)
}

TypingPrinter encapsulates the functionality for simulating the typing of text output. It allows for different typing effect implementations to be used interchangeably.

func NewTypingPrinter added in v0.8.10

func NewTypingPrinter() *TypingPrinter

NewTypingPrinter creates a new instance of TypingPrinter with a default print function. This constructor function is idiomatic in Go, providing a way to set up the struct with default values, which in this case is the PrintTypingChat function.

func (*TypingPrinter) Print added in v0.8.10

func (tp *TypingPrinter) Print(message string, delay time.Duration)

Print executes the typing effect for the given message using the configured PrintFunc. This method offers the flexibility to change the typing effect logic at runtime by assigning a different function to PrintFunc. The delay parameter controls the speed of the typing effect, making it adaptable to various use cases.

type Worker added in v0.4.4

type Worker interface {
	Start(ctx context.Context) error
	Stop() error
}

Worker defines the interface for a background worker in the terminal application.

Directories

Path Synopsis
Package tools encapsulates a collection of utility functions designed to support various common operations throughout the application.
Package tools encapsulates a collection of utility functions designed to support various common operations throughout the application.
Package tour provides a collection of string manipulation utilities.
Package tour provides a collection of string manipulation utilities.

Jump to

Keyboard shortcuts

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