command_chat

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

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

ANSI color codes

Variables

View Source
var ChatCmd = &cli.Command{
	Name:  "chat",
	Usage: "Start an interactive chat session with the AI assistant",
	Description: `The chat command allows you to have an interactive conversation with the AI assistant.

Type your messages and press Enter to send them. The assistant will respond in real-time.
Type 'exit' or 'quit' to end the session.`,
	MaxArgs: cli.NoArgs,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "server",
			Aliases: []string{"s"},
			Usage:   "The address of the remote server to connect to.",
			EnvVars: []string{config.CONFIG_ENV_PREFIX + "_SERVER"},
		},
		&cli.StringFlag{
			Name:    "token",
			Aliases: []string{"t"},
			Usage:   "The token to use for authentication.",
			EnvVars: []string{config.CONFIG_ENV_PREFIX + "_TOKEN"},
		},
		&cli.BoolFlag{
			Name:         "tls-skip-verify",
			Usage:        "Skip TLS verification when talking to server.",
			ConfigPath:   []string{"tls.skip_verify"},
			EnvVars:      []string{config.CONFIG_ENV_PREFIX + "_TLS_SKIP_VERIFY"},
			DefaultValue: true,
		},
		&cli.StringFlag{
			Name:         "alias",
			Aliases:      []string{"a"},
			Usage:        "The server alias to use.",
			DefaultValue: "default",
		},
		&cli.BoolFlag{
			Name:         "show-thinking",
			Usage:        "Show the AI's thinking process instead of animation.",
			DefaultValue: false,
		},
	},
	Run: func(ctx context.Context, cmd *cli.Command) error {
		alias := cmd.GetString("alias")
		cfg := config.GetServerAddr(alias, cmd)

		client, err := rest.NewClient(cfg.HttpServer, cfg.ApiToken, cmd.GetBool("tls-skip-verify"))
		if err != nil {
			return fmt.Errorf("failed to create REST client: %w", err)
		}
		client.SetTimeout(5 * time.Minute)
		client.SetHeader("X-Knot-Api-Version", "2025-03-10")

		fmt.Printf("%sKnot AI Assistant%s\n", ColorBold+ColorCyan, ColorReset)
		fmt.Printf("%sType your message and press Enter. Type 'exit' or 'quit' to end the session.%s\n", ColorGray, ColorReset)
		fmt.Println()

		scanner := bufio.NewScanner(os.Stdin)
		var messages []ChatMessage

		for {
			fmt.Printf("%s%sYou:%s ", ColorBold, ColorBlue, ColorReset)
			if !scanner.Scan() {
				break
			}

			input := strings.TrimSpace(scanner.Text())
			if input == "" {
				continue
			}

			if input == "exit" || input == "quit" {
				fmt.Printf("%sGoodbye! 👋%s\n", ColorYellow, ColorReset)
				break
			}

			messages = append(messages, ChatMessage{
				Role:      "user",
				Content:   input,
				Timestamp: time.Now().Unix(),
			})

			assistantMessage, err := sendChatRequest(client, messages, cmd.GetBool("show-thinking"))
			if err != nil {
				fmt.Printf("%sError:%s %v\n", ColorRed, ColorReset, err)
				continue
			}

			assistantMessage = strings.TrimSpace(assistantMessage)
			if assistantMessage != "" {

				cleanContent := stripThinkTags(assistantMessage)
				if cleanContent != "" {
					messages = append(messages, ChatMessage{
						Role:      "assistant",
						Content:   cleanContent,
						Timestamp: time.Now().Unix(),
					})
				}
			}

			fmt.Println()
		}

		return nil
	},
}

Functions

This section is empty.

Types

type ChatMessage

type ChatMessage struct {
	Role      string `json:"role"`
	Content   string `json:"content"`
	Timestamp int64  `json:"timestamp"`
}

type ChatRequest

type ChatRequest struct {
	Messages []ChatMessage `json:"messages"`
}

type SSEEvent

type SSEEvent struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
}

Jump to

Keyboard shortcuts

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