Rename shared.State to shared.Shared

This commit is contained in:
Matt Low 2024-06-08 22:01:16 +00:00
parent c9e92e186e
commit c1ead83939
7 changed files with 36 additions and 36 deletions

View File

@ -9,7 +9,7 @@ type Values struct {
ConvShortname string
}
type State struct {
type Shared struct {
Ctx *lmcli.Context
Values *Values
Width int

View File

@ -18,7 +18,7 @@ import (
// Application model
type Model struct {
shared.State
shared.Shared
state shared.View
chat chat.Model
@ -27,15 +27,15 @@ type Model struct {
func initialModel(ctx *lmcli.Context, values shared.Values) Model {
m := Model{
State: shared.State{
Shared: shared.Shared{
Ctx: ctx,
Values: &values,
},
}
m.state = shared.StateChat
m.chat = chat.Chat(m.State)
m.conversations = conversations.Conversations(m.State)
m.chat = chat.Chat(m.Shared)
m.conversations = conversations.Conversations(m.Shared)
return m
}

View File

@ -70,7 +70,7 @@ const (
)
type Model struct {
shared.State
shared.Shared
shared.Sections
// app state
@ -104,9 +104,9 @@ type Model struct {
elapsed time.Duration
}
func Chat(state shared.State) Model {
func Chat(shared shared.Shared) Model {
m := Model{
State: state,
Shared: shared,
state: idle,
conversation: &models.Conversation{},
@ -140,7 +140,7 @@ func Chat(state shared.State) Model {
m.replyCursor.SetChar(" ")
m.replyCursor.Focus()
system := state.Ctx.GetSystemPrompt()
system := shared.Ctx.GetSystemPrompt()
if system != "" {
m.messages = []models.Message{{
Role: models.MessageRoleSystem,

View File

@ -53,14 +53,14 @@ func (m *Model) loadConversation(shortname string) tea.Cmd {
if shortname == "" {
return nil
}
c, err := m.State.Ctx.Store.ConversationByShortName(shortname)
c, err := m.Shared.Ctx.Store.ConversationByShortName(shortname)
if err != nil {
return shared.MsgError(fmt.Errorf("Could not lookup conversation: %v", err))
}
if c.ID == 0 {
return shared.MsgError(fmt.Errorf("Conversation not found: %s", shortname))
}
rootMessages, err := m.State.Ctx.Store.RootMessages(c.ID)
rootMessages, err := m.Shared.Ctx.Store.RootMessages(c.ID)
if err != nil {
return shared.MsgError(fmt.Errorf("Could not load conversation root messages: %v\n", err))
}
@ -70,7 +70,7 @@ func (m *Model) loadConversation(shortname string) tea.Cmd {
func (m *Model) loadConversationMessages() tea.Cmd {
return func() tea.Msg {
messages, err := m.State.Ctx.Store.PathToLeaf(m.conversation.SelectedRoot)
messages, err := m.Shared.Ctx.Store.PathToLeaf(m.conversation.SelectedRoot)
if err != nil {
return shared.MsgError(fmt.Errorf("Could not load conversation messages: %v\n", err))
}
@ -80,7 +80,7 @@ func (m *Model) loadConversationMessages() tea.Cmd {
func (m *Model) generateConversationTitle() tea.Cmd {
return func() tea.Msg {
title, err := cmdutil.GenerateTitle(m.State.Ctx, m.messages)
title, err := cmdutil.GenerateTitle(m.Shared.Ctx, m.messages)
if err != nil {
return shared.MsgError(err)
}
@ -90,7 +90,7 @@ func (m *Model) generateConversationTitle() tea.Cmd {
func (m *Model) updateConversationTitle(conversation *models.Conversation) tea.Cmd {
return func() tea.Msg {
err := m.State.Ctx.Store.UpdateConversation(conversation)
err := m.Shared.Ctx.Store.UpdateConversation(conversation)
if err != nil {
return shared.WrapError(err)
}
@ -110,10 +110,10 @@ func (m *Model) cloneMessage(message models.Message, selected bool) tea.Cmd {
if selected {
if msg.Parent == nil {
msg.Conversation.SelectedRoot = msg
err = m.State.Ctx.Store.UpdateConversation(&msg.Conversation)
err = m.Shared.Ctx.Store.UpdateConversation(&msg.Conversation)
} else {
msg.Parent.SelectedReply = msg
err = m.State.Ctx.Store.UpdateMessage(msg.Parent)
err = m.Shared.Ctx.Store.UpdateMessage(msg.Parent)
}
if err != nil {
return shared.WrapError(fmt.Errorf("Could not update selected message: %v", err))
@ -125,7 +125,7 @@ func (m *Model) cloneMessage(message models.Message, selected bool) tea.Cmd {
func (m *Model) updateMessageContent(message *models.Message) tea.Cmd {
return func() tea.Msg {
err := m.State.Ctx.Store.UpdateMessage(message)
err := m.Shared.Ctx.Store.UpdateMessage(message)
if err != nil {
return shared.WrapError(fmt.Errorf("Could not update message: %v", err))
}
@ -170,7 +170,7 @@ func (m *Model) cycleSelectedRoot(conv *models.Conversation, dir MessageCycleDir
}
conv.SelectedRoot = nextRoot
err = m.State.Ctx.Store.UpdateConversation(conv)
err = m.Shared.Ctx.Store.UpdateConversation(conv)
if err != nil {
return shared.WrapError(fmt.Errorf("Could not update conversation SelectedRoot: %v", err))
}
@ -190,7 +190,7 @@ func (m *Model) cycleSelectedReply(message *models.Message, dir MessageCycleDire
}
message.SelectedReply = nextReply
err = m.State.Ctx.Store.UpdateMessage(message)
err = m.Shared.Ctx.Store.UpdateMessage(message)
if err != nil {
return shared.WrapError(fmt.Errorf("Could not update message SelectedReply: %v", err))
}
@ -206,7 +206,7 @@ func (m *Model) persistConversation() tea.Cmd {
if m.conversation.ID == 0 {
return func() tea.Msg {
// Start a new conversation with all messages so far
conversation, messages, err = m.State.Ctx.Store.StartConversation(messages...)
conversation, messages, err = m.Shared.Ctx.Store.StartConversation(messages...)
if err != nil {
return shared.MsgError(fmt.Errorf("Could not start new conversation: %v", err))
}
@ -219,7 +219,7 @@ func (m *Model) persistConversation() tea.Cmd {
for i := range messages {
if messages[i].ID > 0 {
// message has an ID, update its contents
err := m.State.Ctx.Store.UpdateMessage(&messages[i])
err := m.Shared.Ctx.Store.UpdateMessage(&messages[i])
if err != nil {
return shared.MsgError(err)
}
@ -228,7 +228,7 @@ func (m *Model) persistConversation() tea.Cmd {
continue
}
// messages is new, so add it as a reply to previous message
saved, err := m.State.Ctx.Store.Reply(&messages[i-1], messages[i])
saved, err := m.Shared.Ctx.Store.Reply(&messages[i-1], messages[i])
if err != nil {
return shared.MsgError(err)
}
@ -252,16 +252,16 @@ func (m *Model) promptLLM() tea.Cmd {
m.elapsed = 0
return func() tea.Msg {
model, provider, err := m.State.Ctx.GetModelProvider(*m.State.Ctx.Config.Defaults.Model)
model, provider, err := m.Shared.Ctx.GetModelProvider(*m.Shared.Ctx.Config.Defaults.Model)
if err != nil {
return shared.MsgError(err)
}
requestParams := models.RequestParameters{
Model: model,
MaxTokens: *m.State.Ctx.Config.Defaults.MaxTokens,
Temperature: *m.State.Ctx.Config.Defaults.Temperature,
ToolBag: m.State.Ctx.EnabledTools,
MaxTokens: *m.Shared.Ctx.Config.Defaults.MaxTokens,
Temperature: *m.Shared.Ctx.Config.Defaults.Temperature,
ToolBag: m.Shared.Ctx.EnabledTools,
}
replyHandler := func(msg models.Message) {

View File

@ -42,11 +42,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// wake up spinners and cursors
cmds = append(cmds, cursor.Blink, m.spinner.Tick)
if m.State.Values.ConvShortname != "" {
if m.Shared.Values.ConvShortname != "" {
// (re)load conversation contents
cmds = append(cmds, m.loadConversation(m.State.Values.ConvShortname))
cmds = append(cmds, m.loadConversation(m.Shared.Values.ConvShortname))
if m.conversation.ShortName.String != m.State.Values.ConvShortname {
if m.conversation.ShortName.String != m.Shared.Values.ConvShortname {
// clear existing messages if we're loading a new conversation
m.messages = []models.Message{}
m.selectedMessage = 0
@ -151,7 +151,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.updateContent()
case msgResponseError:
m.state = idle
m.State.Err = error(msg)
m.Shared.Err = error(msg)
m.updateContent()
case msgConversationTitleGenerated:
title := string(msg)

View File

@ -131,7 +131,7 @@ func (m *Model) renderMessage(i int) string {
sb := &strings.Builder{}
sb.Grow(len(msg.Content) * 2)
if msg.Content != "" {
err := m.State.Ctx.Chroma.Highlight(sb, msg.Content)
err := m.Shared.Ctx.Chroma.Highlight(sb, msg.Content)
if err != nil {
sb.Reset()
sb.WriteString(msg.Content)
@ -195,7 +195,7 @@ func (m *Model) renderMessage(i int) string {
if msg.Content != "" {
sb.WriteString("\n\n")
}
_ = m.State.Ctx.Chroma.HighlightLang(sb, toolString, "yaml")
_ = m.Shared.Ctx.Chroma.HighlightLang(sb, toolString, "yaml")
}
content := strings.TrimRight(sb.String(), "\n")
@ -308,7 +308,7 @@ func (m *Model) footerView() string {
rightSegments = append(rightSegments, segmentStyle.Render(throughput))
}
model := fmt.Sprintf("Model: %s", *m.State.Ctx.Config.Defaults.Model)
model := fmt.Sprintf("Model: %s", *m.Shared.Ctx.Config.Defaults.Model)
rightSegments = append(rightSegments, segmentStyle.Render(model))
left := strings.Join(leftSegments, segmentSeparator)

View File

@ -28,7 +28,7 @@ type (
)
type Model struct {
shared.State
shared.Shared
shared.Sections
conversations []loadedConversation
@ -38,9 +38,9 @@ type Model struct {
content viewport.Model
}
func Conversations(state shared.State) Model {
func Conversations(shared shared.Shared) Model {
m := Model{
State: state,
Shared: shared,
content: viewport.New(0, 0),
}
return m