Browse Source

wip: refactoring tui

adamdottv 8 months ago
parent
commit
a890288900
2 changed files with 84 additions and 34 deletions
  1. 77 17
      packages/tui/internal/app/app.go
  2. 7 17
      packages/tui/internal/tui/tui.go

+ 77 - 17
packages/tui/internal/app/app.go

@@ -121,24 +121,79 @@ func (a *App) SaveConfig() {
 	config.SaveConfig(a.ConfigPath, a.Config)
 	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 {
 func (a *App) SendChatMessage(ctx context.Context, text string, attachments []Attachment) tea.Cmd {
 	var cmds []tea.Cmd
 	var cmds []tea.Cmd
 	if a.Session.Id == "" {
 	if a.Session.Id == "" {
-		resp, err := a.Client.PostSessionCreateWithResponse(ctx)
+		session, err := a.CreateSession(ctx)
 		if err != nil {
 		if err != nil {
 			status.Error(err.Error())
 			status.Error(err.Error())
 			return nil
 			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
 	// 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}
 	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
 	// The actual response will come through SSE
 	// For now, just return success
 	// For now, just return success

+ 7 - 17
packages/tui/internal/tui/tui.go

@@ -333,21 +333,14 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case dialog.CloseInitDialogMsg:
 	case dialog.CloseInitDialogMsg:
 		a.showInitDialog = false
 		a.showInitDialog = false
 		if msg.Initialize {
 		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
 		return a, nil
 
 
 	case dialog.CommandSelectedMsg:
 	case dialog.CommandSelectedMsg:
@@ -928,10 +921,7 @@ func NewModel(app *app.App) tea.Model {
 		Title:       "Initialize Project",
 		Title:       "Initialize Project",
 		Description: "Create/Update the AGENTS.md memory file",
 		Description: "Create/Update the AGENTS.md memory file",
 		Handler: func(cmd dialog.Command) tea.Cmd {
 		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())
 		},
 		},
 	})
 	})