mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-26 04:04:22 +01:00
wip: refactoring tui
This commit is contained in:
@@ -16,10 +16,10 @@ import (
|
||||
"github.com/sst/opencode/internal/tui/styles"
|
||||
"github.com/sst/opencode/internal/tui/theme"
|
||||
"github.com/sst/opencode/pkg/client"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type uiMessageType int
|
||||
|
||||
const (
|
||||
maxResultHeight = 10
|
||||
)
|
||||
@@ -149,7 +149,8 @@ func renderAssistantMessage(
|
||||
switch toolInvocation.(type) {
|
||||
case client.MessageToolInvocationToolCall:
|
||||
toolCall := toolInvocation.(client.MessageToolInvocationToolCall)
|
||||
toolName := toolName(toolCall.ToolName)
|
||||
toolName := renderToolName(toolCall.ToolName)
|
||||
|
||||
var toolArgs []string
|
||||
toolMap, _ := convertToMap(toolCall.Args)
|
||||
for _, arg := range toolMap {
|
||||
@@ -166,7 +167,7 @@ func renderAssistantMessage(
|
||||
|
||||
case client.MessageToolInvocationToolResult:
|
||||
toolInvocationResult := toolInvocation.(client.MessageToolInvocationToolResult)
|
||||
toolName := toolName(toolInvocationResult.ToolName)
|
||||
toolName := renderToolName(toolInvocationResult.ToolName)
|
||||
var toolArgs []string
|
||||
toolMap, _ := convertToMap(toolInvocationResult.Args)
|
||||
for _, arg := range toolMap {
|
||||
@@ -258,35 +259,18 @@ func findToolResponse(toolCallID string, futureMessages []message.Message) *mess
|
||||
return nil
|
||||
}
|
||||
|
||||
func toolName(name string) string {
|
||||
func renderToolName(name string) string {
|
||||
switch name {
|
||||
// case agent.AgentToolName:
|
||||
// return "Task"
|
||||
case tools.BashToolName:
|
||||
return "Bash"
|
||||
case tools.EditToolName:
|
||||
return "Edit"
|
||||
case tools.FetchToolName:
|
||||
return "Fetch"
|
||||
case tools.GlobToolName:
|
||||
return "Glob"
|
||||
case tools.GrepToolName:
|
||||
return "Grep"
|
||||
case tools.LSToolName:
|
||||
case "ls":
|
||||
return "List"
|
||||
case tools.ViewToolName:
|
||||
return "View"
|
||||
case tools.WriteToolName:
|
||||
return "Write"
|
||||
case tools.PatchToolName:
|
||||
return "Patch"
|
||||
case tools.BatchToolName:
|
||||
return "Batch"
|
||||
default:
|
||||
return cases.Title(language.English).String(name)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func getToolAction(name string) string {
|
||||
func renderToolAction(name string) string {
|
||||
switch name {
|
||||
// case agent.AgentToolName:
|
||||
// return "Preparing prompt..."
|
||||
@@ -570,7 +554,7 @@ func renderToolResponse(toolCall message.ToolCall, response message.ToolResult,
|
||||
|
||||
var toolCalls []string
|
||||
for i, result := range batchResult.Results {
|
||||
toolName := toolName(result.ToolName)
|
||||
toolName := renderToolName(result.ToolName)
|
||||
|
||||
// Format the tool input as a string
|
||||
inputStr := string(result.ToolInput)
|
||||
@@ -628,11 +612,11 @@ func renderToolMessage(
|
||||
|
||||
response := findToolResponse(toolCall.ID, allMessages)
|
||||
toolNameText := baseStyle.Foreground(t.TextMuted()).
|
||||
Render(fmt.Sprintf("%s: ", toolName(toolCall.Name)))
|
||||
Render(fmt.Sprintf("%s: ", renderToolName(toolCall.Name)))
|
||||
|
||||
if !toolCall.Finished {
|
||||
// Get a brief description of what the tool is doing
|
||||
toolAction := getToolAction(toolCall.Name)
|
||||
toolAction := renderToolAction(toolCall.Name)
|
||||
|
||||
progressText := baseStyle.
|
||||
Width(width - 2 - lipgloss.Width(toolNameText)).
|
||||
|
||||
@@ -23,7 +23,6 @@ type messagesCmp struct {
|
||||
app *app.App
|
||||
width, height int
|
||||
viewport viewport.Model
|
||||
currentMsgID string
|
||||
spinner spinner.Model
|
||||
rendering bool
|
||||
attachments viewport.Model
|
||||
@@ -63,8 +62,6 @@ func (m *messagesCmp) Init() tea.Cmd {
|
||||
}
|
||||
|
||||
func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
// m.renderView()
|
||||
|
||||
var cmds []tea.Cmd
|
||||
switch msg := msg.(type) {
|
||||
case dialog.ThemeChangedMsg:
|
||||
@@ -72,16 +69,12 @@ func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
case ToggleToolMessagesMsg:
|
||||
m.showToolMessages = !m.showToolMessages
|
||||
// Clear the cache to force re-rendering of all messages
|
||||
// m.cachedContent = make(map[string]cacheItem)
|
||||
m.renderView()
|
||||
return m, nil
|
||||
case state.SessionSelectedMsg:
|
||||
cmd := m.Reload(msg)
|
||||
return m, cmd
|
||||
case state.SessionClearedMsg:
|
||||
// m.messages = make([]message.Message, 0)
|
||||
m.currentMsgID = ""
|
||||
m.rendering = false
|
||||
return m, nil
|
||||
case tea.KeyMsg:
|
||||
@@ -119,17 +112,10 @@ func (m *messagesCmp) renderView() {
|
||||
for _, msg := range m.app.Messages {
|
||||
switch msg.Role {
|
||||
case client.User:
|
||||
content := renderUserMessage(
|
||||
msg,
|
||||
m.width,
|
||||
)
|
||||
content := renderUserMessage(msg, m.width)
|
||||
messages = append(messages, content+"\n")
|
||||
case client.Assistant:
|
||||
content := renderAssistantMessage(
|
||||
msg,
|
||||
m.width,
|
||||
m.showToolMessages,
|
||||
)
|
||||
content := renderAssistantMessage(msg, m.width, m.showToolMessages)
|
||||
messages = append(messages, content+"\n")
|
||||
}
|
||||
}
|
||||
@@ -328,18 +314,6 @@ func (m *messagesCmp) GetSize() (int, int) {
|
||||
}
|
||||
|
||||
func (m *messagesCmp) Reload(session *session.Session) tea.Cmd {
|
||||
// messages := m.app.Messages
|
||||
// messages, err := m.app.MessagesOLD.List(context.Background(), session.ID)
|
||||
// if err != nil {
|
||||
// status.Error(err.Error())
|
||||
// return nil
|
||||
// }
|
||||
// m.messages = messages
|
||||
|
||||
if len(m.app.Messages) > 0 {
|
||||
m.currentMsgID = m.app.Messages[len(m.app.Messages)-1].Id
|
||||
}
|
||||
// delete(m.cachedContent, m.currentMsgID)
|
||||
m.rendering = true
|
||||
return func() tea.Msg {
|
||||
m.renderView()
|
||||
|
||||
Reference in New Issue
Block a user