The open source coding agent.
TypeScript
wip: refactoring tui
adamdottva89028890093
diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.goindex 61ef95e2b25..ca57961d174 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.Cmdif 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 successdiff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.goindex 393344a2f59..66888d87fdb 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 = falseif 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, nilcase 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())},})