Skip to content

Commit

Permalink
chore(chat.go): remove unused import and banner code to improve code …
Browse files Browse the repository at this point in the history
…cleanliness and reduce unnecessary dependencies

feat(chat.go): remove setting of content in viewport to improve code readability and remove unnecessary visual element
refactor(chat.go): remove unnecessary initialization of modelStruct and move reset function call to improve code readability and code organization
refactor(chat.go): remove unnecessary return of tea.EnterAltScreen in Init function to improve code readability and remove unnecessary command
refactor(chat.go): remove unnecessary condition for rendering aiResponse in markdown mode to improve code readability and remove unnecessary check
refactor(chatUpdate.go): refactor reset function to improve code readability and code organization, and add additional information in aiResponse
refactor(chatUpdate.go): refactor changeResponseUp function to handle edge case when there are no previous messages and reset the model
refactor(chatUpdate.go): refactor changeResponseDown function to handle edge case when there are no next messages and reset the model
  • Loading branch information
MohammadBnei committed Feb 3, 2024
1 parent 31c95d5 commit aff2c46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
10 changes: 4 additions & 6 deletions prompt/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/samber/lo"

"moul.io/banner"
)

var (
Expand Down Expand Up @@ -95,8 +93,6 @@ func initialChatModel(pc *service.PromptConfig) chatModel {

vp := viewport.New(0, 0)

vp.SetContent(banner.Inline("go ai cli - prompt"))

ta.KeyMap.InsertNewline.SetEnabled(false)

modelStruct := chatModel{
Expand All @@ -115,11 +111,13 @@ func initialChatModel(pc *service.PromptConfig) chatModel {
errorList: []error{},
}

reset(&modelStruct)

return modelStruct
}

func (m chatModel) Init() tea.Cmd {
return tea.EnterAltScreen
return nil
}

var commandSelectionFn = CommandSelectionFactory()
Expand Down Expand Up @@ -267,7 +265,7 @@ func (m chatModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

if m.userPrompt != "" {
aiRes := m.aiResponse
if m.promptConfig.MdMode {
if m.promptConfig.MdMode && m.userPrompt != "Infos" {
aiRes = string(markdown.Render(m.aiResponse, m.size.Width, 0))
}
userPrompt := m.userPrompt
Expand Down
28 changes: 21 additions & 7 deletions prompt/chatUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ type ChatUpdateFunc func(m *chatModel) (tea.Model, tea.Cmd)

func reset(m *chatModel) (tea.Model, tea.Cmd) {
m.textarea.Reset()
paragraphStyle := lipgloss.NewStyle().Padding(2)
m.aiResponse = lipgloss.JoinHorizontal(lipgloss.Center,
paragraphStyle.Render("Tokens : "+fmt.Sprintf("%d", m.promptConfig.ChatMessages.TotalTokens)),
paragraphStyle.Render("Messages : "+fmt.Sprintf("%d", len(m.promptConfig.ChatMessages.Messages))),
paragraphStyle := lipgloss.NewStyle().Margin(2).Width(m.viewport.Width)
m.aiResponse = lipgloss.JoinVertical(lipgloss.Left,
lipgloss.NewStyle().AlignHorizontal(lipgloss.Center).Bold(true).Faint(true).Padding(2).MarginLeft(4).Render("GO-AI-CLI"),
lipgloss.JoinHorizontal(lipgloss.Left,
paragraphStyle.Render("API : "+viper.GetString("API_TYPE")),
paragraphStyle.Render("Model : "+viper.GetString("model")),
),
lipgloss.JoinHorizontal(lipgloss.Left,
paragraphStyle.Render("Tokens : "+fmt.Sprintf("%d", m.promptConfig.ChatMessages.TotalTokens)),
paragraphStyle.Render("Messages : "+fmt.Sprintf("%d", len(m.promptConfig.ChatMessages.Messages))),
),
)

m.userPrompt = "Infos"
m.currentChatIndices = &currentChatIndexes{
user: -1,
Expand Down Expand Up @@ -97,11 +105,16 @@ func changeResponseUp(m *chatModel) (tea.Model, tea.Cmd) {
if len(m.promptConfig.ChatMessages.Messages) == 0 {
return m, nil
}
minIndex := lo.Min(lo.Filter[int]([]int{m.currentChatIndices.user, m.currentChatIndices.assistant}, func(i int, _ int) bool { return i >= 0 }))
currentIndexes := lo.Filter[int]([]int{m.currentChatIndices.user, m.currentChatIndices.assistant}, func(i int, _ int) bool { return i >= 0 })
minIndex := lo.Min(currentIndexes)
previous := minIndex - 1
if len(currentIndexes) == 0 {
previous = len(m.promptConfig.ChatMessages.Messages) - 1
}
c := m.promptConfig.ChatMessages.FindById(previous)
if c == nil {
c = &m.promptConfig.ChatMessages.Messages[len(m.promptConfig.ChatMessages.Messages)-1]
reset(m)
return m, event.UpdateContent
}
m.changeCurrentChatHelper(c)
m.viewport.GotoTop()
Expand All @@ -116,7 +129,8 @@ func changeResponseDown(m *chatModel) (tea.Model, tea.Cmd) {
next := maxIndex + 1
c := m.promptConfig.ChatMessages.FindById(next)
if c == nil {
c = &m.promptConfig.ChatMessages.Messages[0]
reset(m)
return m, event.UpdateContent
}
m.changeCurrentChatHelper(c)
m.viewport.GotoTop()
Expand Down

0 comments on commit aff2c46

Please sign in to comment.