Skip to content

Commit

Permalink
update hey UI (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 28, 2023
1 parent 2634e4d commit 55ef982
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 72 deletions.
57 changes: 6 additions & 51 deletions ui/cmd/hey/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"github.com/JunNishimura/Chatify/ai/prompt"
"github.com/JunNishimura/Chatify/auth"
"github.com/JunNishimura/Chatify/config"
"github.com/JunNishimura/Chatify/ui/cmd/base"
"github.com/JunNishimura/Chatify/ui/style"
"github.com/JunNishimura/Chatify/utils"
"github.com/JunNishimura/spotify/v2"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/lipgloss"
"github.com/sashabaranov/go-openai"
)
Expand Down Expand Up @@ -59,52 +58,33 @@ func (i Item) Title() string {
func (i Item) Description() string { return strings.Join(i.artists, ", ") }
func (i Item) FilterValue() string { return i.album }

type Speaker int

const (
Bot Speaker = iota
User
)

type Message struct {
content string
speaker Speaker
}

type Model struct {
ctx context.Context
window *utils.Window
base *base.Model
state sessionState
textInput textinput.Model
list list.Model
selectedItem Item
cfg *config.Config
user *model.User
spotifyClient *spotify.Client
openaiClient *openai.Client
questionIndex int
functionCall any
chatCompMessages []openai.ChatCompletionMessage
conversation []*Message
functions []openai.FunctionDefinition
recommendItems []list.Item
err error
}

func NewModel() (*Model, error) {
ctx := context.Background()

window := utils.NewWindow()

cfg, err := loadConfig()
func NewModel(ctx context.Context) (*Model, error) {
base, err := base.NewModel()
if err != nil {
return nil, err
}

openAIAPIkey := cfg.GetClientValue(config.OpenAIAPIKey)
openAIAPIkey := base.Cfg.GetClientValue(config.OpenAIAPIKey)
openAIAPIclient := openai.NewClient(openAIAPIkey)

spotifyClient, err := getSpotifyClient(ctx, cfg)
spotifyClient, err := getSpotifyClient(ctx, base.Cfg)
if err != nil {
return nil, err
}
Expand All @@ -121,10 +101,7 @@ func NewModel() (*Model, error) {

return &Model{
ctx: ctx,
window: window,
textInput: newTextInput(window.Width),
list: newListModel([]list.Item{}, 0, 0),
cfg: cfg,
user: user,
spotifyClient: spotifyClient,
openaiClient: openAIAPIclient,
Expand All @@ -140,19 +117,6 @@ func NewModel() (*Model, error) {
}, nil
}

func loadConfig() (*config.Config, error) {
cfg, err := config.New()
if err != nil {
return nil, err
}

if err := cfg.Load(); err != nil {
return nil, err
}

return cfg, nil
}

func getSpotifyClient(ctx context.Context, cfg *config.Config) (*spotify.Client, error) {
a := auth.NewAuth(cfg)

Expand Down Expand Up @@ -197,12 +161,3 @@ func newListModel(items []list.Item, width, height int) list.Model {
newList.Styles.Title.Background(lipgloss.Color(style.HighlightColor))
return newList
}

func newTextInput(width int) textinput.Model {
ti := textinput.New()
ti.Focus()
ti.CharLimit = 100
ti.Width = width

return ti
}
13 changes: 7 additions & 6 deletions ui/cmd/hey/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/JunNishimura/Chatify/ai/functions"
"github.com/JunNishimura/Chatify/ui/cmd/base"
"github.com/JunNishimura/spotify/v2"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -21,7 +22,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.window.UpdateSize()
m.base.Window.UpdateSize()
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
Expand All @@ -31,14 +32,14 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
switch m.state {
case chatView:
answer := m.textInput.Value()
answer := m.base.TextInput.Value()
m.chatCompMessages = append(m.chatCompMessages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: answer,
})
m.conversation = append(m.conversation, &Message{content: fmt.Sprintf("> %s", answer), speaker: User})
m.base.Conversation = append(m.base.Conversation, &base.Message{Content: fmt.Sprintf("> %s", answer), Speaker: base.User})

m.textInput.Reset()
m.base.TextInput.Reset()

if m.questionIndex == 0 {
// genres don't have to be converted into quantitative values
Expand All @@ -65,7 +66,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Role: openai.ChatMessageRoleAssistant,
Content: content,
})
m.conversation = append(m.conversation, &Message{content: content, speaker: Bot})
m.base.Conversation = append(m.base.Conversation, &base.Message{Content: content, Speaker: base.Bot})
}
case guessedMsg:
content := msg.resp.Choices[0].Message.Content
Expand All @@ -89,7 +90,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.err = msg.err
}

m.textInput, inputCmd = m.textInput.Update(msg)
m.base.TextInput, inputCmd = m.base.TextInput.Update(msg)
m.list, listCmd = m.list.Update(msg)

return m, tea.Batch(inputCmd, listCmd)
Expand Down
31 changes: 16 additions & 15 deletions ui/cmd/hey/view.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package hey

import (
"github.com/JunNishimura/Chatify/ui/cmd/base"
"github.com/JunNishimura/Chatify/ui/style"
"github.com/charmbracelet/lipgloss"
)

func (m *Model) View() string {
if m.err != nil {
return style.ErrorView(m.window.Width, m.window.Height)
return style.ErrorView(m.base.Window.Width, m.base.Window.Height)
}

var s string
if m.state == chatView {
s += lipgloss.Place(m.window.Width, m.window.Height, lipgloss.Center, lipgloss.Center,
s += lipgloss.Place(m.base.Window.Width, m.base.Window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinHorizontal(
lipgloss.Center,
style.ChatFocused(m.getViewWidth(), m.getViewHeight()).Render(m.chatView()),
style.RecommendationNormal(m.getViewWidth(), m.getViewHeight()).Render(m.recommendationView()),
),
)
} else {
s += lipgloss.Place(m.window.Width, m.window.Height, lipgloss.Center, lipgloss.Center,
s += lipgloss.Place(m.base.Window.Width, m.base.Window.Height, lipgloss.Center, lipgloss.Center,
lipgloss.JoinHorizontal(
lipgloss.Center,
style.ChatNormal(m.getViewWidth(), m.getViewHeight()).Render(m.chatView()),
Expand All @@ -32,30 +33,30 @@ func (m *Model) View() string {
}

func (m *Model) getViewWidth() int {
wholeWidth := m.window.Width - 4
wholeWidth := m.base.Window.Width - 4
halfWidth := wholeWidth / 2
return halfWidth
}

func (m *Model) getViewHeight() int {
height := m.window.Height - 5
height := m.base.Window.Height - 5
return height
}

func (m *Model) chatView() string {
var s string
var lastSpeaker Speaker
for _, message := range m.conversation {
if message.speaker == Bot {
s += style.BotChat(m.getViewWidth()).Render(message.content) + "\n\n"
lastSpeaker = Bot
} else if message.speaker == User {
s += style.UserChat().Render(message.content) + "\n\n"
lastSpeaker = User
var lastSpeaker base.Speaker
for _, message := range m.base.Conversation {
if message.Speaker == base.Bot {
s += style.BotChat(m.getViewWidth()).Render(message.Content) + "\n\n"
lastSpeaker = base.Bot
} else if message.Speaker == base.User {
s += style.UserChat().Render(message.Content) + "\n\n"
lastSpeaker = base.User
}
}
if s != "" && lastSpeaker == Bot {
s += style.TextInput().Render(m.textInput.View())
if s != "" && lastSpeaker == base.Bot {
s += style.TextInput().Render(m.base.TextInput.View())
}
return s
}
Expand Down

0 comments on commit 55ef982

Please sign in to comment.