Moved api.ChatCompletionProvider, api.Chunk to api/provider
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"git.mlow.ca/mlow/lmcli/pkg/agents"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/api"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/api/provider"
|
||||
cmdutil "git.mlow.ca/mlow/lmcli/pkg/cmd/util"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
@@ -24,7 +25,7 @@ type AppModel struct {
|
||||
Messages []api.Message
|
||||
Model string
|
||||
ProviderName string
|
||||
Provider api.ChatCompletionProvider
|
||||
Provider provider.ChatCompletionProvider
|
||||
}
|
||||
|
||||
func NewAppModel(ctx *lmcli.Context, initialConversation *api.Conversation) *AppModel {
|
||||
@@ -151,6 +152,28 @@ func (a *AppModel) UpdateMessageContent(message *api.Message) error {
|
||||
return a.Ctx.Store.UpdateMessage(message)
|
||||
}
|
||||
|
||||
func cycleSelectedMessage(selected *api.Message, choices []api.Message, dir MessageCycleDirection) (*api.Message, error) {
|
||||
currentIndex := -1
|
||||
for i, reply := range choices {
|
||||
if reply.ID == selected.ID {
|
||||
currentIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if currentIndex < 0 {
|
||||
return nil, fmt.Errorf("Selected message %d not found in choices, this is a bug", selected.ID)
|
||||
}
|
||||
|
||||
var next int
|
||||
if dir == CyclePrev {
|
||||
next = (currentIndex - 1 + len(choices)) % len(choices)
|
||||
} else {
|
||||
next = (currentIndex + 1) % len(choices)
|
||||
}
|
||||
return &choices[next], nil
|
||||
}
|
||||
|
||||
func (a *AppModel) CycleSelectedRoot(conv *api.Conversation, rootMessages []api.Message, dir MessageCycleDirection) (*api.Message, error) {
|
||||
if len(rootMessages) < 2 {
|
||||
return nil, nil
|
||||
@@ -225,13 +248,13 @@ func (a *AppModel) ExecuteToolCalls(toolCalls []api.ToolCall) ([]api.ToolResult,
|
||||
return agents.ExecuteToolCalls(toolCalls, agent.Toolbox)
|
||||
}
|
||||
|
||||
func (a *AppModel) PromptLLM(messages []api.Message, chatReplyChunks chan api.Chunk, stopSignal chan struct{}) (*api.Message, error) {
|
||||
model, _, provider, err := a.Ctx.GetModelProvider(a.Model, a.ProviderName)
|
||||
func (a *AppModel) PromptLLM(messages []api.Message, chatReplyChunks chan provider.Chunk, stopSignal chan struct{}) (*api.Message, error) {
|
||||
model, _, p, err := a.Ctx.GetModelProvider(a.Model, a.ProviderName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := api.RequestParameters{
|
||||
params := provider.RequestParameters{
|
||||
Model: model,
|
||||
MaxTokens: *a.Ctx.Config.Defaults.MaxTokens,
|
||||
Temperature: *a.Ctx.Config.Defaults.Temperature,
|
||||
@@ -251,29 +274,7 @@ func (a *AppModel) PromptLLM(messages []api.Message, chatReplyChunks chan api.Ch
|
||||
}
|
||||
}()
|
||||
|
||||
return provider.CreateChatCompletionStream(
|
||||
return p.CreateChatCompletionStream(
|
||||
ctx, params, messages, chatReplyChunks,
|
||||
)
|
||||
}
|
||||
|
||||
func cycleSelectedMessage(selected *api.Message, choices []api.Message, dir MessageCycleDirection) (*api.Message, error) {
|
||||
currentIndex := -1
|
||||
for i, reply := range choices {
|
||||
if reply.ID == selected.ID {
|
||||
currentIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if currentIndex < 0 {
|
||||
return nil, fmt.Errorf("Selected message %d not found in choices, this is a bug", selected.ID)
|
||||
}
|
||||
|
||||
var next int
|
||||
if dir == CyclePrev {
|
||||
next = (currentIndex - 1 + len(choices)) % len(choices)
|
||||
} else {
|
||||
next = (currentIndex + 1) % len(choices)
|
||||
}
|
||||
return &choices[next], nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.mlow.ca/mlow/lmcli/pkg/api"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/api/provider"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/tui/model"
|
||||
"github.com/charmbracelet/bubbles/cursor"
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
@@ -33,7 +34,7 @@ type (
|
||||
Err error
|
||||
}
|
||||
// sent on each chunk received from LLM
|
||||
msgChatResponseChunk api.Chunk
|
||||
msgChatResponseChunk provider.Chunk
|
||||
// sent on each completed reply
|
||||
msgChatResponse *api.Message
|
||||
// sent when the response is canceled
|
||||
@@ -84,7 +85,7 @@ type Model struct {
|
||||
editorTarget editorTarget
|
||||
stopSignal chan struct{}
|
||||
replyChan chan api.Message
|
||||
chatReplyChunks chan api.Chunk
|
||||
chatReplyChunks chan provider.Chunk
|
||||
persistence bool // whether we will save new messages in the conversation
|
||||
|
||||
// UI state
|
||||
@@ -115,7 +116,7 @@ func Chat(app *model.AppModel) *Model {
|
||||
|
||||
stopSignal: make(chan struct{}),
|
||||
replyChan: make(chan api.Message),
|
||||
chatReplyChunks: make(chan api.Chunk),
|
||||
chatReplyChunks: make(chan provider.Chunk),
|
||||
|
||||
wrap: true,
|
||||
selectedMessage: -1,
|
||||
|
||||
@@ -199,10 +199,10 @@ func (m *Model) renderMessage(i int) string {
|
||||
|
||||
// render the conversation into a string
|
||||
func (m *Model) conversationMessagesView() string {
|
||||
sb := strings.Builder{}
|
||||
|
||||
m.messageOffsets = make([]int, len(m.App.Messages))
|
||||
lineCnt := 1
|
||||
|
||||
sb := strings.Builder{}
|
||||
for i, message := range m.App.Messages {
|
||||
m.messageOffsets[i] = lineCnt
|
||||
|
||||
@@ -227,7 +227,6 @@ func (m *Model) conversationMessagesView() string {
|
||||
sb.WriteString(messageStyle.Width(0).Render(m.replyCursor.View()))
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user