summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authoradamdottv <[email protected]>2025-06-04 11:47:57 -0500
committeradamdottv <[email protected]>2025-06-04 11:48:01 -0500
commita89028890093bebafd498be90a576e1b08eda742 (patch)
tree75c5335a77a9cba9842ba11ac7fbfcdf85e467ba
parent31d6e303a60493cbb15f728e57f3f1a4f236fe50 (diff)
downloadopencode-a89028890093bebafd498be90a576e1b08eda742.tar.gz
opencode-a89028890093bebafd498be90a576e1b08eda742.zip
wip: refactoring tui
-rw-r--r--packages/tui/internal/app/app.go94
-rw-r--r--packages/tui/internal/tui/tui.go24
2 files changed, 84 insertions, 34 deletions
diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.go
index 61ef95e2b..ca57961d1 100644
--- a/packages/tui/internal/app/app.go
+++ b/packages/tui/internal/app/app.go
@@ -121,24 +121,79 @@ func (a *App) SaveConfig() {
config.SaveConfig(a.ConfigPath, a.Config)
}
-// Create creates a new session
+func (a *App) InitializeProject(ctx context.Context) tea.Cmd {
+ cmds := []tea.Cmd{}
+
+ session, err := a.CreateSession(ctx)
+ if err != nil {
+ status.Error(err.Error())
+ return nil
+ }
+
+ a.Session = session
+ cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(session)))
+
+ go func() {
+ // TODO: Handle no provider or model setup, yet
+ response, err := a.Client.PostSessionInitialize(ctx, client.PostSessionInitializeJSONRequestBody{
+ SessionID: a.Session.Id,
+ ProviderID: a.Provider.Id,
+ ModelID: a.Model.Id,
+ })
+ if err != nil {
+ status.Error(err.Error())
+ }
+ if response.StatusCode != 200 {
+ status.Error(fmt.Sprintf("failed to initialize project: %d", response.StatusCode))
+ }
+ }()
+
+ return tea.Batch(cmds...)
+}
+
+func (a *App) MarkProjectInitialized(ctx context.Context) error {
+ response, err := a.Client.PostAppInitialize(ctx)
+ if err != nil {
+ slog.Error("Failed to mark project as initialized", "error", err)
+ return err
+ }
+ if response.StatusCode != 200 {
+ return fmt.Errorf("failed to initialize project: %d", response.StatusCode)
+ }
+ return nil
+}
+
+func (a *App) IsBusy() bool {
+ for _, message := range a.Messages {
+ if message.Metadata.Time.Completed == nil {
+ return true
+ }
+ }
+ return false
+}
+
+func (a *App) CreateSession(ctx context.Context) (*client.SessionInfo, error) {
+ resp, err := a.Client.PostSessionCreateWithResponse(ctx)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode() != 200 {
+ return nil, fmt.Errorf("failed to create session: %d", resp.StatusCode())
+ }
+ session := resp.JSON200
+ return session, nil
+}
+
func (a *App) SendChatMessage(ctx context.Context, text string, attachments []Attachment) tea.Cmd {
var cmds []tea.Cmd
if a.Session.Id == "" {
- resp, err := a.Client.PostSessionCreateWithResponse(ctx)
+ session, err := a.CreateSession(ctx)
if err != nil {
status.Error(err.Error())
return nil
}
- if resp.StatusCode() != 200 {
- status.Error(fmt.Sprintf("failed to create session: %d", resp.StatusCode()))
- return nil
- }
-
- info := resp.JSON200
- a.Session = info
-
- cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(info)))
+ a.Session = session
+ cmds = append(cmds, util.CmdHandler(state.SessionSelectedMsg(session)))
}
// TODO: Handle attachments when API supports them
@@ -154,12 +209,17 @@ func (a *App) SendChatMessage(ctx context.Context, text string, attachments []At
})
parts := []client.MessagePart{part}
- go a.Client.PostSessionChatWithResponse(ctx, client.PostSessionChatJSONRequestBody{
- SessionID: a.Session.Id,
- Parts: parts,
- ProviderID: a.Provider.Id,
- ModelID: a.Model.Id,
- })
+ go func() {
+ _, err := a.Client.PostSessionChat(ctx, client.PostSessionChatJSONRequestBody{
+ SessionID: a.Session.Id,
+ Parts: parts,
+ ProviderID: a.Provider.Id,
+ ModelID: a.Model.Id,
+ })
+ if err != nil {
+ status.Error(err.Error())
+ }
+ }()
// The actual response will come through SSE
// For now, just return success
diff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.go
index 393344a2f..66888d87f 100644
--- a/packages/tui/internal/tui/tui.go
+++ b/packages/tui/internal/tui/tui.go
@@ -333,21 +333,14 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case dialog.CloseInitDialogMsg:
a.showInitDialog = false
if msg.Initialize {
- // Run the initialization command
- for _, cmd := range a.commands {
- if cmd.ID == "init" {
- return a, cmd.Handler(cmd)
- }
+ return a, a.app.InitializeProject(context.Background())
+ } else {
+ // Mark the project as initialized without running the command
+ if err := a.app.MarkProjectInitialized(context.Background()); err != nil {
+ status.Error(err.Error())
+ return a, nil
}
}
- // TODO: should we not ask again?
- // else {
- // // Mark the project as initialized without running the command
- // if err := config.MarkProjectInitialized(); err != nil {
- // status.Error(err.Error())
- // return a, nil
- // }
- // }
return a, nil
case dialog.CommandSelectedMsg:
@@ -928,10 +921,7 @@ func NewModel(app *app.App) tea.Model {
Title: "Initialize Project",
Description: "Create/Update the AGENTS.md memory file",
Handler: func(cmd dialog.Command) tea.Cmd {
- model.app.Client.PostSessionInitialize(context.Background(), client.PostSessionInitializeJSONRequestBody{
- SessionID: model.app.Session.Id,
- })
- return nil
+ return app.InitializeProject(context.Background())
},
})