summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authoradamdottv <[email protected]>2025-05-29 06:32:41 -0500
committeradamdottv <[email protected]>2025-05-29 06:32:44 -0500
commit2b77a7f71473c868fd0472c9284ddff18c83388a (patch)
treeb48cd300acb739f67c3b1814f9a0dfc04c4f79cc
parent5974a530717a799960300088c51e0cc7c2876f57 (diff)
downloadopencode-2b77a7f71473c868fd0472c9284ddff18c83388a.tar.gz
opencode-2b77a7f71473c868fd0472c9284ddff18c83388a.zip
wip: refactoring tui
-rw-r--r--internal/tui/app/app.go79
-rw-r--r--internal/tui/page/chat.go26
2 files changed, 66 insertions, 39 deletions
diff --git a/internal/tui/app/app.go b/internal/tui/app/app.go
index 7acf3021d..c7ea0cc66 100644
--- a/internal/tui/app/app.go
+++ b/internal/tui/app/app.go
@@ -8,12 +8,16 @@ import (
"log/slog"
+ tea "github.com/charmbracelet/bubbletea"
"github.com/sst/opencode/internal/config"
"github.com/sst/opencode/internal/fileutil"
"github.com/sst/opencode/internal/lsp"
+ "github.com/sst/opencode/internal/message"
"github.com/sst/opencode/internal/session"
"github.com/sst/opencode/internal/status"
+ "github.com/sst/opencode/internal/tui/state"
"github.com/sst/opencode/internal/tui/theme"
+ "github.com/sst/opencode/internal/tui/util"
"github.com/sst/opencode/pkg/client"
)
@@ -94,27 +98,66 @@ func New(ctx context.Context) (*App, error) {
// Initialize theme based on configuration
app.initTheme()
- // TODO: Remove this once agent is fully replaced by API
- // app.PrimaryAgent, err = agent.NewAgent(
- // config.AgentPrimary,
- // app.Sessions,
- // app.Messages,
- // agent.PrimaryAgentTools(
- // app.Permissions,
- // app.Sessions,
- // app.Messages,
- // app.History,
- // app.LSPClients,
- // ),
- // )
- // if err != nil {
- // slog.Error("Failed to create primary agent", "error", err)
- // return nil, err
- // }
-
return app, nil
}
+// Create creates a new session
+func (a *App) SendChatMessage(ctx context.Context, text string, attachments []message.Attachment) tea.Cmd {
+ var cmds []tea.Cmd
+ if a.CurrentSession.ID == "" {
+ resp, err := a.Client.PostSessionCreateWithResponse(ctx)
+ if err != nil {
+ // return session.Session{}, err
+ }
+ if resp.StatusCode() != 200 {
+ // return session.Session{}, fmt.Errorf("failed to create session: %d", resp.StatusCode())
+ }
+ info := resp.JSON200
+
+ // Convert to old session type
+ newSession := session.Session{
+ ID: info.Id,
+ Title: info.Title,
+ CreatedAt: time.Now(), // API doesn't provide this yet
+ UpdatedAt: time.Now(), // API doesn't provide this yet
+ }
+
+ if err != nil {
+ status.Error(err.Error())
+ return nil
+ }
+
+ a.CurrentSession = &newSession
+
+ cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(&newSession)))
+ }
+
+ // TODO: Handle attachments when API supports them
+ if len(attachments) > 0 {
+ // For now, ignore attachments
+ // return "", fmt.Errorf("attachments not supported yet")
+ }
+
+ part := client.SessionMessagePart{}
+ part.FromSessionMessagePartText(client.SessionMessagePartText{
+ Type: "text",
+ Text: text,
+ })
+ parts := []client.SessionMessagePart{part}
+
+ go a.Client.PostSessionChatWithResponse(ctx, client.PostSessionChatJSONRequestBody{
+ SessionID: a.CurrentSession.ID,
+ Parts: parts,
+ ProviderID: "anthropic",
+ ModelID: "claude-sonnet-4-20250514",
+ })
+
+ // The actual response will come through SSE
+ // For now, just return success
+
+ return tea.Batch(cmds...)
+}
+
// initTheme sets the application theme based on the configuration
func (app *App) initTheme() {
cfg := config.Get()
diff --git a/internal/tui/page/chat.go b/internal/tui/page/chat.go
index 65ac8afe9..96847fee7 100644
--- a/internal/tui/page/chat.go
+++ b/internal/tui/page/chat.go
@@ -180,28 +180,12 @@ func (p *chatPage) clearSidebar() tea.Cmd {
func (p *chatPage) sendMessage(text string, attachments []message.Attachment) tea.Cmd {
var cmds []tea.Cmd
- if p.app.CurrentSession.ID == "" {
- newSession, err := p.app.Sessions.Create(context.Background(), "New Session")
- if err != nil {
- status.Error(err.Error())
- return nil
- }
-
- p.app.CurrentSession = &newSession
-
- cmd := p.setSidebar()
- if cmd != nil {
- cmds = append(cmds, cmd)
- }
- cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(&newSession)))
- }
-
- _, err := p.app.PrimaryAgent.Run(context.Background(), p.app.CurrentSession.ID, text, attachments...)
- if err != nil {
- status.Error(err.Error())
- return nil
+ cmd := p.app.SendChatMessage(context.Background(), text, attachments)
+ cmds = append(cmds, cmd)
+ cmd = p.setSidebar()
+ if cmd != nil {
+ cmds = append(cmds, cmd)
}
-
return tea.Batch(cmds...)
}