Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
working on tell command/stream ui
Dane Schneider747ea26a0302
diff --git a/cli/api/stream.go b/cli/api/stream.goindex 35266162..9a94623f 100644--- a/cli/api/stream.go+++ b/cli/api/stream.go@@ -45,8 +45,6 @@ func connectPlanRespStream(body io.ReadCloser, onStream OnStreamPlan) {err = streamState.Event(context.Background(), shared.EVENT_DESCRIBE)} else if s == shared.STREAM_BUILD_PHASE {err = streamState.Event(context.Background(), shared.EVENT_BUILD)- } else if s == shared.STREAM_WRITE_PHASE {- err = streamState.Event(context.Background(), shared.EVENT_WRITE)}if err != nil {diff --git a/cli/cmd/continue.go b/cli/cmd/continue.goindex e7be02b8..1e711880 100644--- a/cli/cmd/continue.go+++ b/cli/cmd/continue.go@@ -3,14 +3,19 @@ package cmdimport ("fmt""os"+ "plandex/api""plandex/lib"+ streamtui "plandex/stream_tui"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")// Variables to be used in the nextCmdconst continuePrompt = "continue the plan"+var continueBg bool+// nextCmd represents the prompt commandvar nextCmd = &cobra.Command{Use: "continue",@@ -21,14 +26,30 @@ var nextCmd = &cobra.Command{func init() {RootCmd.AddCommand(nextCmd)++ tellCmd.Flags().BoolVar(&continueBg, "bg", false, "Execute autonomously in the background")}func next(cmd *cobra.Command, args []string) {lib.MustResolveProject()- err := lib.Propose(continuePrompt)+ lib.MustCheckOutdatedContextWithOutput()++ err := api.Client.TellPlan(lib.CurrentPlanId, shared.TellPlanRequest{+ Prompt: continuePrompt,+ ConnectStream: !continueBg,+ }, lib.OnStreamPlan)if err != nil {fmt.Fprintln(os.Stderr, "Prompt error:", err)return}++ if !continueBg {+ err = streamtui.StartStreamUI()++ if err != nil {+ fmt.Fprintln(os.Stderr, "Error starting stream UI:", err)+ return+ }+ }}diff --git a/cli/cmd/tell.go b/cli/cmd/tell.goindex f7c6069c..f081b67f 100644--- a/cli/cmd/tell.go+++ b/cli/cmd/tell.go@@ -4,16 +4,20 @@ import ("fmt""os""os/exec"+ "plandex/api""plandex/lib"+ streamtui "plandex/stream_tui""plandex/term""strings"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")const defaultEditor = "vim"-var promptFile string+var tellPromptFile string+var tellBg bool// tellCmd represents the prompt commandvar tellCmd = &cobra.Command{@@ -28,7 +32,9 @@ var tellCmd = &cobra.Command{func init() {RootCmd.AddCommand(tellCmd)- tellCmd.Flags().StringVarP(&promptFile, "file", "f", "", "File containing prompt")+ tellCmd.Flags().StringVarP(&tellPromptFile, "file", "f", "", "File containing prompt")++ tellCmd.Flags().BoolVar(&tellBg, "bg", false, "Execute autonomously in the background")}func tell(cmd *cobra.Command, args []string) {@@ -36,8 +42,8 @@ func tell(cmd *cobra.Command, args []string) {if len(args) > 0 {prompt = args[0]- } else if promptFile != "" {- bytes, err := os.ReadFile(promptFile)+ } else if tellPromptFile != "" {+ bytes, err := os.ReadFile(tellPromptFile)if err != nil {fmt.Fprintln(os.Stderr, "Error reading prompt file:", err)return@@ -109,11 +115,25 @@ func tell(cmd *cobra.Command, args []string) {return}- err := lib.Propose(prompt)+ lib.MustCheckOutdatedContextWithOutput()++ err := api.Client.TellPlan(lib.CurrentPlanId, shared.TellPlanRequest{+ Prompt: prompt,+ ConnectStream: !tellBg,+ }, lib.OnStreamPlan)if err != nil {fmt.Fprintln(os.Stderr, "Prompt error:", err)return}++ if !tellBg {+ err = streamtui.StartStreamUI()++ if err != nil {+ fmt.Fprintln(os.Stderr, "Error starting stream UI:", err)+ return+ }+ }}func prepareEditorCommand(editor string, filename string) *exec.Cmd {diff --git a/cli/lib/context_update.go b/cli/lib/context_update.goindex 0b34a48d..d8a4ba04 100644--- a/cli/lib/context_update.go+++ b/cli/lib/context_update.go@@ -9,18 +9,114 @@ import ("plandex/term""plandex/types""plandex/url"+ "strconv""strings""sync""time"- "github.com/briandowns/spinner"+ "github.com/fatih/color"+ "github.com/olekukonko/tablewriter""github.com/plandex/plandex/shared")+func MustCheckOutdatedContextWithOutput() {+ s := term.Spinner+ start := time.Now()+ s.Prefix = "π¬ Checking context... "+ s.Start()++ stopSpinner := func() {+ s.Stop()+ term.ClearCurrentLine()+ }++ outdatedRes, err := CheckOutdatedContext()+ if err != nil {+ stopSpinner()+ panic(fmt.Errorf("failed to check outdated context: %s", err))+ }++ elapsed := time.Since(start)+ if elapsed < 700*time.Millisecond {+ time.Sleep(700*time.Millisecond - elapsed)+ }++ stopSpinner()++ if len(outdatedRes.UpdatedContexts) == 0 {+ fmt.Println("β Context is up to date")+ return+ }+ types := []string{}+ if outdatedRes.NumFiles > 0 {+ lbl := "file"+ if outdatedRes.NumFiles > 1 {+ lbl = "files"+ }+ lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl+ types = append(types, lbl)+ }+ if outdatedRes.NumUrls > 0 {+ lbl := "url"+ if outdatedRes.NumUrls > 1 {+ lbl = "urls"+ }+ lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl+ types = append(types, lbl)+ }+ if outdatedRes.NumTrees > 0 {+ lbl := "directory tree"+ if outdatedRes.NumTrees > 1 {+ lbl = "directory trees"+ }+ lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl+ types = append(types, lbl)+ }++ var msg string+ if len(types) <= 2 {+ msg += strings.Join(types, " and ")+ } else {+ for i, add := range types {+ if i == len(types)-1 {+ msg += ", and " + add+ } else {+ msg += ", " + add+ }+ }+ }++ phrase := "have been"+ if len(outdatedRes.UpdatedContexts) == 1 {+ phrase = "has been"+ }+ color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)++ tableString := tableForContextOutdated(outdatedRes)+ fmt.Println(tableString)++ fmt.Println()++ confirmed, canceled, err := term.ConfirmYesNoCancel("Update context now?")++ if err != nil {+ panic(fmt.Errorf("failed to get user input: %s", err))+ }++ if confirmed {+ MustUpdateContextWithOuput()+ }++ if canceled {+ os.Exit(0)+ }++}+func MustUpdateContextWithOuput() {timeStart := time.Now()- s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)+ s := term.Spinners.Prefix = "π Updating context... "s.Start()@@ -66,6 +162,7 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedRereq := shared.UpdateContextRequest{}var updatedContexts []*shared.Context+ var tokenDiffsById = map[string]int{}var numFiles intvar numUrls intvar numTrees int@@ -91,11 +188,18 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedResha := hex.EncodeToString(hash[:])if sha != context.Sha {+ body := string(fileContent)++ numTokens, err := shared.GetNumTokens(body)+ if err != nil {+ errs = append(errs, fmt.Errorf("failed to get the number of tokens in the file %s: %v", context.FilePath, err))+ return+ }+ tokenDiffsById[context.Id] = numTokens - context.NumTokens+numFiles++updatedContexts = append(updatedContexts, context)- body := string(fileContent)-req[context.Id] = &shared.UpdateContextParams{Body: body,}@@ -124,6 +228,13 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedResha := hex.EncodeToString(hash[:])if sha != context.Sha {+ numTokens, err := shared.GetNumTokens(body)+ if err != nil {+ errs = append(errs, fmt.Errorf("failed to get the number of tokens in the file %s: %v", context.FilePath, err))+ return+ }+ tokenDiffsById[context.Id] = numTokens - context.NumTokens+numTrees++updatedContexts = append(updatedContexts, context)req[context.Id] = &shared.UpdateContextParams{@@ -134,25 +245,32 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedRe} else if context.ContextType == shared.ContextURLType {wg.Add(1)- go func(part *shared.Context) {+ go func(context *shared.Context) {defer wg.Done()- body, err := url.FetchURLContent(part.Url)+ body, err := url.FetchURLContent(context.Url)mu.Lock()defer mu.Unlock()if err != nil {- errs = append(errs, fmt.Errorf("failed to fetch the URL %s: %v", part.Url, err))+ errs = append(errs, fmt.Errorf("failed to fetch the URL %s: %v", context.Url, err))return}hash := sha256.Sum256([]byte(body))sha := hex.EncodeToString(hash[:])- if sha != part.Sha {+ if sha != context.Sha {+ numTokens, err := shared.GetNumTokens(body)+ if err != nil {+ errs = append(errs, fmt.Errorf("failed to get the number of tokens in the file %s: %v", context.FilePath, err))+ return+ }+ tokenDiffsById[context.Id] = numTokens - context.NumTokens+numUrls++- updatedContexts = append(updatedContexts, part)- req[part.Id] = &shared.UpdateContextParams{+ updatedContexts = append(updatedContexts, context)+ req[context.Id] = &shared.UpdateContextParams{Body: body,}}@@ -184,8 +302,52 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedRereturn &types.ContextOutdatedResult{Msg: msg,UpdatedContexts: updatedContexts,+ TokenDiffsById: tokenDiffsById,NumFiles: numFiles,NumUrls: numUrls,NumTrees: numTrees,}, nil}++func tableForContextOutdated(updateRes *types.ContextOutdatedResult) string {+ updatedContexts := updateRes.UpdatedContexts+ tokenDiffsById := updateRes.TokenDiffsById++ if len(updatedContexts) == 0 {+ return ""+ }++ tableString := &strings.Builder{}+ table := tablewriter.NewWriter(tableString)+ table.SetHeader([]string{"Name", "Type", "πͺ"})+ table.SetAutoWrapText(false)++ for _, context := range updatedContexts {+ t, icon := GetContextTypeAndIcon(context)+ diff := tokenDiffsById[context.Id]++ diffStr := "+" + strconv.Itoa(diff)+ tableColor := tablewriter.FgHiGreenColor++ if diff < 0 {+ diffStr = strconv.Itoa(diff)+ tableColor = tablewriter.FgHiRedColor+ }++ row := []string{+ " " + icon + " " + context.Name,+ t,+ diffStr,+ }++ table.Rich(row, []tablewriter.Colors{+ {tableColor, tablewriter.Bold},+ {tableColor},+ {tableColor},+ })+ }++ table.Render()++ return tableString.String()+}diff --git a/cli/lib/proposal.go b/cli/lib/proposal.godeleted file mode 100644index cff94dd5..00000000--- a/cli/lib/proposal.go+++ /dev/null@@ -1,615 +0,0 @@-package lib--import (- "time"-)--const replyStreamThrottle = 70 * time.Millisecond--type key struct {- value rune-}--func Propose(prompt string) error {- // var err error-- // planState, err := GetPlanState()- // if err != nil {- // return fmt.Errorf("failed to get plan state: %s", err)- // }-- // s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)- // if planState.ContextUpdatableTokens > 0 {- // shouldContinue, err := checkOutdatedContext(s)- // if err != nil {- // fmt.Fprintf(os.Stderr, "Failed to check outdated context: %v\n", err)- // return err- // }-- // if !shouldContinue {- // return nil- // }- // }-- // start := time.Now()- // s.Prefix = "π¬ Sending prompt... "- // s.Start()-- // if CurrentPlanIsDraft() {- // fileNameResp, err := Api.FileName(prompt)- // if err != nil {- // fmt.Fprintln(os.Stderr, "\nError getting file name for prompt:", err)- // return err- // }- // err = RenameCurrentDraftPlan(planState, format.GetFileNameWithoutExt(fileNameResp.FileName))- // if err != nil {- // fmt.Fprintln(os.Stderr, "\nError renaming draft plan:", err)- // return err- // }- // }-- // // fmt.Println("Checked current plan is draft")-- // timestamp := shared.StringTs()- // reply := ""- // done := make(chan struct{})- // closedDone := false-- // termState := ""-- // replyUpdateTimer := time.NewTimer(150 * time.Millisecond)- // defer replyUpdateTimer.Stop()-- // var proposalId string- // var replyStarted bool- // var terminalHasPendingUpdate bool- // var desc *shared.PlanDescription- // var state *fsm.FSM- // var streamFinished bool- // var filesFinished bool- // var lastReplyTokenAdded time.Time- // finishedByPath := make(map[string]bool)- // var buildMu sync.Mutex-- // numStreamedTokensByPath := make(map[string]int)-- // replyTokenCounter := shared.NewReplyInfo()-- // var parentProposalId string- // var rootId string-- // parentProposalId = planState.ProposalId- // rootId = planState.RootId-- // if rootId != "" {- // err = saveLatestConvoSummary(rootId)- // if err != nil {- // return fmt.Errorf("failed to save latest convo summary: %s", err)- // }- // }-- // // fmt.Println("Initialized locals")-- // promptNumTokens, err := shared.GetNumTokens(prompt)- // if err != nil {- // return fmt.Errorf("failed to get number of tokens in prompt: %s", err)- // }-- // // fmt.Println("Got prompt num tokens")-- // err = appendConversation(types.AppendConversationParams{- // Timestamp: timestamp,- // PlanState: planState,- // PromptParams: &types.AppendConversationPromptParams{- // Prompt: prompt,- // PromptTokens: promptNumTokens,- // },- // })-- // // fmt.Println("Appended conversation")-- // if err != nil {- // return fmt.Errorf("failed to append prompt to conversation: %s", err)- // }-- // printReply := func() {- // term.ClearScreen()- // term.MoveCursorToTopLeft()- // mdFull, _ := term.GetMarkdown(reply)- // fmt.Println(mdFull)- // fmt.Println(displayHotkeys())- // termState = mdFull- // }-- // go func() {- // for range replyUpdateTimer.C {- // if replyStarted && terminalHasPendingUpdate {- // printReply()- // terminalHasPendingUpdate = false- // }- // replyUpdateTimer.Reset(100 * time.Millisecond)- // }- // }()-- // // fmt.Println("Starting proposal")-- // keyChan := make(chan *key, 1)- // ctx, cancelKeywatch := context.WithCancel(context.Background())- // errChn := make(chan error, 1)-- // endReply := func() {- // replyUpdateTimer.Stop()- // printReply()- // term.BackToMain()- // fmt.Print(termState)- // }-- // appendConvoReply := func() error {- // var totalTokens int- // _, _, _, totalTokens = replyTokenCounter.FinishAndRead()-- // return appendConversation(types.AppendConversationParams{- // Timestamp: timestamp,- // PlanState: planState,- // ReplyParams: &types.AppendConversationReplyParams{- // ResponseTimestamp: desc.ResponseTimestamp,- // Reply: reply,- // ReplyTokens: totalTokens,- // },- // })- // }-- // writeFileProgress := func() {- // files := desc.Files-- // if len(files) == 0 {- // return- // }-- // // Clear previous lines- // numLines := len(files) + 4-- // term.MoveUpLines(numLines)-- // for _, filePath := range files {- // numStreamedTokens := numStreamedTokensByPath[filePath]-- // fmtStr := " π %s | %d πͺ"- // fmtArgs := []interface{}{filePath, numStreamedTokens}-- // _, finished := finishedByPath[filePath]-- // if finished {- // fmtStr += " | done β "- // }-- // term.ClearCurrentLine()-- // fmt.Printf(fmtStr+"\n", fmtArgs...)- // }-- // if !(filesFinished && streamFinished) {- // fmt.Println()- // fmt.Printf(displayHotkeys() + "\n")- // }- // }-- // contextByFilePath := make(map[string]*shared.ModelContextPart)-- // running := false- // queue := make(chan types.OnStreamPlanParams, 1)-- // var apiReq *shared.PromptRequest-- // var handleStream types.OnStreamPlan-- // // fmt.Println("Starting handleStream")-- // handleStream = func(params types.OnStreamPlanParams) {- // if running {- // queue <- params- // return- // }-- // defer func() {- // if len(queue) > 0 {- // params := <-queue- // handleStream(params)- // } else {- // running = false- // }- // }()-- // state = params.State- // err := params.Err- // content := params.Content-- // onError := func(err error) {- // term.BackToMain()- // fmt.Fprintln(os.Stderr, "Error:", err)- // cancelKeywatch()- // if !closedDone {- // close(done)- // closedDone = true- // }- // }-- // if err != nil {- // onError(err)- // return- // }-- // if proposalId == "" {- // if content == "" {- // onError(fmt.Errorf("proposal id not sent in first chunk"))- // return- // } else {- // proposalId = content- // if rootId == "" {- // rootId = proposalId- // }-- // // Save proposal id to [CurrentPlanRootDir]/plan.json- // planState.ProposalId = proposalId- // planState.RootId = rootId- // err = SetPlanState(planState, shared.StringTs())- // if err != nil {- // onError(fmt.Errorf("failed to update plan state: %s", err))- // return- // }- // return- // }- // } else if !replyStarted {- // elapsed := time.Since(start)- // if elapsed < 700*time.Millisecond {- // time.Sleep(700*time.Millisecond - elapsed)- // }-- // s.Stop()- // term.ClearCurrentLine()- // term.AlternateScreen()-- // replyStarted = true- // lastReplyTokenAdded = time.Now()- // }-- // switch state.Current() {- // case shared.STATE_REPLYING:- // elapsed := time.Since(lastReplyTokenAdded)-- // if elapsed < replyStreamThrottle {- // time.Sleep(replyStreamThrottle - elapsed)- // }-- // reply += content- // replyTokenCounter.AddToken(content, true)- // terminalHasPendingUpdate = true- // lastReplyTokenAdded = time.Now()-- // case shared.STATE_FINISHED:- // s.Stop()- // buildMu.Lock()- // defer buildMu.Unlock()- // streamFinished = true- // if filesFinished && !closedDone {- // close(done)- // closedDone = true- // writeFileProgress()- // fmt.Println()- // fmt.Println()- // }-- // case shared.STATE_DESCRIBING:- // if content == shared.STREAM_DESCRIPTION_PHASE {- // return- // }-- // describeStart := time.Now()-- // bytes := []byte(content)-- // err := json.Unmarshal(bytes, &desc)- // if err != nil {- // onError(fmt.Errorf("error parsing plan description: %v", err))- // return- // }-- // if len(desc.Files) > 0 {- // s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)- // s.Prefix = " "- // s.Start()- // }-- // err = os.MkdirAll(DescriptionsSubdir, os.ModePerm)- // if err != nil {- // onError(fmt.Errorf("failed to create plan descriptions directory: %s", err))- // return- // }- // descriptionsPath := filepath.Join(DescriptionsSubdir, desc.ResponseTimestamp+".json")- // err = os.WriteFile(descriptionsPath, bytes, 0644)- // if err != nil {- // onError(fmt.Errorf("failed to write plan description to file: %s", err))- // return- // }-- // // fmt.Println("appending convo reply")-- // err = appendConvoReply()- // if err != nil {- // onError(fmt.Errorf("failed to append reply to conversation: %s", err))- // return- // }-- // // fmt.Println("appended convo reply")-- // // wait a bit if necessary to avoid jarring output of conversation as soon as stream finishes- // elapsed := time.Since(describeStart)- // if elapsed < 1500*time.Millisecond {- // time.Sleep(1500*time.Millisecond - elapsed)- // }-- // // fmt.Println("waited elapsed")-- // endReply()-- // // fmt.Println("ended reply")- // s.Stop()-- // if desc.MadePlan && (len(desc.Files) > 0) {- // fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))- // for _, filePath := range desc.Files {- // fmt.Printf(" π %s\n", filePath)- // }- // fmt.Println()- // fmt.Printf(displayHotkeys() + "\n")- // } else {- // filesFinished = true- // }-- // case shared.STATE_BUILDING:- // if content == shared.STREAM_BUILD_PHASE {- // return-- // }- // err := updateTokenCounts(content, numStreamedTokensByPath, finishedByPath)-- // if err != nil {- // onError(err)- // return- // }-- // writeFileProgress()-- // case shared.STATE_WRITING:- // if content == shared.STREAM_WRITE_PHASE {- // // write phase started- // return- // }-- // err := writePlanRes(content)-- // if err != nil {- // onError(fmt.Errorf("failed to write plan file: %s", err))- // return- // }-- // // fmt.Printf("Wrote %d / %d files", len(finishedByPath), len(files))- // if len(finishedByPath) == len(desc.Files) {- // buildMu.Lock()- // defer buildMu.Unlock()- // filesFinished = true-- // err = GitCommitPlanUpdate(desc.CommitMsg)- // if err != nil {- // onError(fmt.Errorf("failed to commit root update: %s", err))- // return- // }-- // if streamFinished && !closedDone {- // close(done)- // closedDone = true- // writeFileProgress()- // fmt.Println()- // fmt.Println()- // }- // }- // }-- // }-- // apiReq, err = Api.Propose(prompt, parentProposalId, rootId, handleStream)- // if err != nil {- // term.BackToMain()- // return fmt.Errorf("failed to send prompt to server: %s", err)- // }-- // // fmt.Println("Sent prompt to server")-- // for _, part := range apiReq.ModelContext {- // contextByFilePath[part.FilePath] = part- // }-- // // fmt.Println("Got context")-- // go func(ctx context.Context, errChn chan error) {- // for {- // select {- // case <-ctx.Done():- // return- // default:- // k, err := term.GetUserInput()- // if err != nil {- // errChn <- err- // return- // }- // keyChan <- &key{k}- // }- // }- // }(ctx, errChn)-- // handleKey := func(k *key) error {- // return handleKeyPress(k.value, proposalId)- // }-- // Loop:- // for {- // select {- // case k := <-keyChan:- // if err := handleKey(k); err != nil {- // cancelKeywatch()- // return err- // }- // case <-done: // Evidence of operation completion- // cancelKeywatch()- // break Loop- // case err := <-errChn: // Listening for errors- // cancelKeywatch()- // return err- // }- // }-- // // fmt.Println("Started key loop")-- // if desc != nil {- // if desc.MadePlan && len(desc.Files) > 0 {- // fmt.Println()- // for _, cmd := range []string{"apply", "diffs", "preview"} {- // term.ClearCurrentLine()- // term.PrintCmds(" ", cmd)- // }- // }-- // term.ClearCurrentLine()- // term.PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")-- // term.ClearCurrentLine()- // term.PrintCmds(" ", "continue")-- // term.ClearCurrentLine()- // term.PrintCmds(" ", "rewind")- // }-- // // fmt.Println("Finished proposal")-- // return nil- // }-- // func Abort(proposalId string) error {- // err := Api.Abort(proposalId)- // return err- // }-- // func checkOutdatedContext(s *spinner.Spinner) (bool, error) {- // start := time.Now()- // s.Prefix = "π¬ Checking context... "- // s.Start()-- // stopSpinner := func() {- // s.Stop()- // term.ClearCurrentLine()- // }-- // outdatedRes, err := CheckOutdatedContext()- // if err != nil {- // stopSpinner()- // return false, fmt.Errorf("failed to check outdated context: %s", err)- // }-- // elapsed := time.Since(start)- // if elapsed < 700*time.Millisecond {- // time.Sleep(700*time.Millisecond - elapsed)- // }-- // stopSpinner()-- // if len(outdatedRes.UpdatedContexts) == 0 {- // fmt.Println("β Context is up to date")- // return true, nil- // }- // types := []string{}- // if outdatedRes.NumFiles > 0 {- // lbl := "file"- // if outdatedRes.NumFiles > 1 {- // lbl = "files"- // }- // lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl- // types = append(types, lbl)- // }- // if outdatedRes.NumUrls > 0 {- // lbl := "url"- // if outdatedRes.NumUrls > 1 {- // lbl = "urls"- // }- // lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl- // types = append(types, lbl)- // }- // if outdatedRes.NumTrees > 0 {- // lbl := "directory tree"- // if outdatedRes.NumTrees > 1 {- // lbl = "directory trees"- // }- // lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl- // types = append(types, lbl)- // }-- // var msg string- // if len(types) <= 2 {- // msg += strings.Join(types, " and ")- // } else {- // for i, add := range types {- // if i == len(types)-1 {- // msg += ", and " + add- // } else {- // msg += ", " + add- // }- // }- // }-- // phrase := "have been"- // if len(outdatedRes.UpdatedContexts) == 1 {- // phrase = "has been"- // }- // color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)-- // tableString := TableForContextUpdateRes(outdatedRes)- // fmt.Println(tableString)-- // fmt.Println()-- // confirmed, canceled, err := term.ConfirmYesNoCancel("Update context now?")-- // if err != nil {- // return false, fmt.Errorf("failed to get user input: %s", err)- // }-- // if confirmed {- // MustUpdateContextWithOuput()- // }-- // shouldContinue := !canceled-- // return shouldContinue, nil-- return nil-}--// func handleAbortKey(proposalId string) error {-// return Abort(proposalId)-// }--// func handleKeyPress(input rune, proposalId string) error {-// switch input {-// case 's':-// return handleAbortKey(proposalId)-// default:-// return fmt.Errorf("invalid key pressed: %s", string(input))-// }-// }--// func displayHotkeys() string {-// divisionLine := term.GetDivisionLine()--// return divisionLine + "\n" +-// " \x1b[1m(s)\x1b[0m" + `top-// ` + divisionLine-// }diff --git a/cli/lib/proposal.go.old b/cli/lib/proposal.go.oldnew file mode 100644index 00000000..be98cf82--- /dev/null+++ b/cli/lib/proposal.go.old@@ -0,0 +1,615 @@+// package lib++// import (+// "time"+// )++// const replyStreamThrottle = 70 * time.Millisecond++// type key struct {+// value rune+// }++// func Propose(prompt string) error {+// // var err error++// // planState, err := GetPlanState()+// // if err != nil {+// // return fmt.Errorf("failed to get plan state: %s", err)+// // }++// // s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)+// // if planState.ContextUpdatableTokens > 0 {+// // shouldContinue, err := checkOutdatedContext(s)+// // if err != nil {+// // fmt.Fprintf(os.Stderr, "Failed to check outdated context: %v\n", err)+// // return err+// // }++// // if !shouldContinue {+// // return nil+// // }+// // }++// // start := time.Now()+// // s.Prefix = "π¬ Sending prompt... "+// // s.Start()++// // if CurrentPlanIsDraft() {+// // fileNameResp, err := Api.FileName(prompt)+// // if err != nil {+// // fmt.Fprintln(os.Stderr, "\nError getting file name for prompt:", err)+// // return err+// // }+// // err = RenameCurrentDraftPlan(planState, format.GetFileNameWithoutExt(fileNameResp.FileName))+// // if err != nil {+// // fmt.Fprintln(os.Stderr, "\nError renaming draft plan:", err)+// // return err+// // }+// // }++// // // fmt.Println("Checked current plan is draft")++// // timestamp := shared.StringTs()+// // reply := ""+// // done := make(chan struct{})+// // closedDone := false++// // termState := ""++// // replyUpdateTimer := time.NewTimer(150 * time.Millisecond)+// // defer replyUpdateTimer.Stop()++// // var proposalId string+// // var replyStarted bool+// // var terminalHasPendingUpdate bool+// // var desc *shared.PlanDescription+// // var state *fsm.FSM+// // var streamFinished bool+// // var filesFinished bool+// // var lastReplyTokenAdded time.Time+// // finishedByPath := make(map[string]bool)+// // var buildMu sync.Mutex++// // numStreamedTokensByPath := make(map[string]int)++// // replyTokenCounter := shared.NewReplyInfo()++// // var parentProposalId string+// // var rootId string++// // parentProposalId = planState.ProposalId+// // rootId = planState.RootId++// // if rootId != "" {+// // err = saveLatestConvoSummary(rootId)+// // if err != nil {+// // return fmt.Errorf("failed to save latest convo summary: %s", err)+// // }+// // }++// // // fmt.Println("Initialized locals")++// // promptNumTokens, err := shared.GetNumTokens(prompt)+// // if err != nil {+// // return fmt.Errorf("failed to get number of tokens in prompt: %s", err)+// // }++// // // fmt.Println("Got prompt num tokens")++// // err = appendConversation(types.AppendConversationParams{+// // Timestamp: timestamp,+// // PlanState: planState,+// // PromptParams: &types.AppendConversationPromptParams{+// // Prompt: prompt,+// // PromptTokens: promptNumTokens,+// // },+// // })++// // // fmt.Println("Appended conversation")++// // if err != nil {+// // return fmt.Errorf("failed to append prompt to conversation: %s", err)+// // }++// // printReply := func() {+// // term.ClearScreen()+// // term.MoveCursorToTopLeft()+// // mdFull, _ := term.GetMarkdown(reply)+// // fmt.Println(mdFull)+// // fmt.Println(displayHotkeys())+// // termState = mdFull+// // }++// // go func() {+// // for range replyUpdateTimer.C {+// // if replyStarted && terminalHasPendingUpdate {+// // printReply()+// // terminalHasPendingUpdate = false+// // }+// // replyUpdateTimer.Reset(100 * time.Millisecond)+// // }+// // }()++// // // fmt.Println("Starting proposal")++// // keyChan := make(chan *key, 1)+// // ctx, cancelKeywatch := context.WithCancel(context.Background())+// // errChn := make(chan error, 1)++// // endReply := func() {+// // replyUpdateTimer.Stop()+// // printReply()+// // term.BackToMain()+// // fmt.Print(termState)+// // }++// // appendConvoReply := func() error {+// // var totalTokens int+// // _, _, _, totalTokens = replyTokenCounter.FinishAndRead()++// // return appendConversation(types.AppendConversationParams{+// // Timestamp: timestamp,+// // PlanState: planState,+// // ReplyParams: &types.AppendConversationReplyParams{+// // ResponseTimestamp: desc.ResponseTimestamp,+// // Reply: reply,+// // ReplyTokens: totalTokens,+// // },+// // })+// // }++// // writeFileProgress := func() {+// // files := desc.Files++// // if len(files) == 0 {+// // return+// // }++// // // Clear previous lines+// // numLines := len(files) + 4++// // term.MoveUpLines(numLines)++// // for _, filePath := range files {+// // numStreamedTokens := numStreamedTokensByPath[filePath]++// // fmtStr := " π %s | %d πͺ"+// // fmtArgs := []interface{}{filePath, numStreamedTokens}++// // _, finished := finishedByPath[filePath]++// // if finished {+// // fmtStr += " | done β "+// // }++// // term.ClearCurrentLine()++// // fmt.Printf(fmtStr+"\n", fmtArgs...)+// // }++// // if !(filesFinished && streamFinished) {+// // fmt.Println()+// // fmt.Printf(displayHotkeys() + "\n")+// // }+// // }++// // contextByFilePath := make(map[string]*shared.ModelContextPart)++// // running := false+// // queue := make(chan types.OnStreamPlanParams, 1)++// // var apiReq *shared.PromptRequest++// // var handleStream types.OnStreamPlan++// // // fmt.Println("Starting handleStream")++// // handleStream = func(params types.OnStreamPlanParams) {+// // if running {+// // queue <- params+// // return+// // }++// // defer func() {+// // if len(queue) > 0 {+// // params := <-queue+// // handleStream(params)+// // } else {+// // running = false+// // }+// // }()++// // state = params.State+// // err := params.Err+// // content := params.Content++// // onError := func(err error) {+// // term.BackToMain()+// // fmt.Fprintln(os.Stderr, "Error:", err)+// // cancelKeywatch()+// // if !closedDone {+// // close(done)+// // closedDone = true+// // }+// // }++// // if err != nil {+// // onError(err)+// // return+// // }++// // if proposalId == "" {+// // if content == "" {+// // onError(fmt.Errorf("proposal id not sent in first chunk"))+// // return+// // } else {+// // proposalId = content+// // if rootId == "" {+// // rootId = proposalId+// // }++// // // Save proposal id to [CurrentPlanRootDir]/plan.json+// // planState.ProposalId = proposalId+// // planState.RootId = rootId+// // err = SetPlanState(planState, shared.StringTs())+// // if err != nil {+// // onError(fmt.Errorf("failed to update plan state: %s", err))+// // return+// // }+// // return+// // }+// // } else if !replyStarted {+// // elapsed := time.Since(start)+// // if elapsed < 700*time.Millisecond {+// // time.Sleep(700*time.Millisecond - elapsed)+// // }++// // s.Stop()+// // term.ClearCurrentLine()+// // term.AlternateScreen()++// // replyStarted = true+// // lastReplyTokenAdded = time.Now()+// // }++// // switch state.Current() {+// // case shared.STATE_REPLYING:+// // elapsed := time.Since(lastReplyTokenAdded)++// // if elapsed < replyStreamThrottle {+// // time.Sleep(replyStreamThrottle - elapsed)+// // }++// // reply += content+// // replyTokenCounter.AddToken(content, true)+// // terminalHasPendingUpdate = true+// // lastReplyTokenAdded = time.Now()++// // case shared.STATE_FINISHED:+// // s.Stop()+// // buildMu.Lock()+// // defer buildMu.Unlock()+// // streamFinished = true+// // if filesFinished && !closedDone {+// // close(done)+// // closedDone = true+// // writeFileProgress()+// // fmt.Println()+// // fmt.Println()+// // }++// // case shared.STATE_DESCRIBING:+// // if content == shared.STREAM_DESCRIPTION_PHASE {+// // return+// // }++// // describeStart := time.Now()++// // bytes := []byte(content)++// // err := json.Unmarshal(bytes, &desc)+// // if err != nil {+// // onError(fmt.Errorf("error parsing plan description: %v", err))+// // return+// // }++// // if len(desc.Files) > 0 {+// // s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)+// // s.Prefix = " "+// // s.Start()+// // }++// // err = os.MkdirAll(DescriptionsSubdir, os.ModePerm)+// // if err != nil {+// // onError(fmt.Errorf("failed to create plan descriptions directory: %s", err))+// // return+// // }+// // descriptionsPath := filepath.Join(DescriptionsSubdir, desc.ResponseTimestamp+".json")+// // err = os.WriteFile(descriptionsPath, bytes, 0644)+// // if err != nil {+// // onError(fmt.Errorf("failed to write plan description to file: %s", err))+// // return+// // }++// // // fmt.Println("appending convo reply")++// // err = appendConvoReply()+// // if err != nil {+// // onError(fmt.Errorf("failed to append reply to conversation: %s", err))+// // return+// // }++// // // fmt.Println("appended convo reply")++// // // wait a bit if necessary to avoid jarring output of conversation as soon as stream finishes+// // elapsed := time.Since(describeStart)+// // if elapsed < 1500*time.Millisecond {+// // time.Sleep(1500*time.Millisecond - elapsed)+// // }++// // // fmt.Println("waited elapsed")++// // endReply()++// // // fmt.Println("ended reply")+// // s.Stop()++// // if desc.MadePlan && (len(desc.Files) > 0) {+// // fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))+// // for _, filePath := range desc.Files {+// // fmt.Printf(" π %s\n", filePath)+// // }+// // fmt.Println()+// // fmt.Printf(displayHotkeys() + "\n")+// // } else {+// // filesFinished = true+// // }++// // case shared.STATE_BUILDING:+// // if content == shared.STREAM_BUILD_PHASE {+// // return++// // }+// // err := updateTokenCounts(content, numStreamedTokensByPath, finishedByPath)++// // if err != nil {+// // onError(err)+// // return+// // }++// // writeFileProgress()++// // case shared.STATE_WRITING:+// // if content == shared.STREAM_WRITE_PHASE {+// // // write phase started+// // return+// // }++// // err := writePlanRes(content)++// // if err != nil {+// // onError(fmt.Errorf("failed to write plan file: %s", err))+// // return+// // }++// // // fmt.Printf("Wrote %d / %d files", len(finishedByPath), len(files))+// // if len(finishedByPath) == len(desc.Files) {+// // buildMu.Lock()+// // defer buildMu.Unlock()+// // filesFinished = true++// // err = GitCommitPlanUpdate(desc.CommitMsg)+// // if err != nil {+// // onError(fmt.Errorf("failed to commit root update: %s", err))+// // return+// // }++// // if streamFinished && !closedDone {+// // close(done)+// // closedDone = true+// // writeFileProgress()+// // fmt.Println()+// // fmt.Println()+// // }+// // }+// // }++// // }++// // apiReq, err = Api.Propose(prompt, parentProposalId, rootId, handleStream)+// // if err != nil {+// // term.BackToMain()+// // return fmt.Errorf("failed to send prompt to server: %s", err)+// // }++// // // fmt.Println("Sent prompt to server")++// // for _, part := range apiReq.ModelContext {+// // contextByFilePath[part.FilePath] = part+// // }++// // // fmt.Println("Got context")++// // go func(ctx context.Context, errChn chan error) {+// // for {+// // select {+// // case <-ctx.Done():+// // return+// // default:+// // k, err := term.GetUserInput()+// // if err != nil {+// // errChn <- err+// // return+// // }+// // keyChan <- &key{k}+// // }+// // }+// // }(ctx, errChn)++// // handleKey := func(k *key) error {+// // return handleKeyPress(k.value, proposalId)+// // }++// // Loop:+// // for {+// // select {+// // case k := <-keyChan:+// // if err := handleKey(k); err != nil {+// // cancelKeywatch()+// // return err+// // }+// // case <-done: // Evidence of operation completion+// // cancelKeywatch()+// // break Loop+// // case err := <-errChn: // Listening for errors+// // cancelKeywatch()+// // return err+// // }+// // }++// // // fmt.Println("Started key loop")++// // if desc != nil {+// // if desc.MadePlan && len(desc.Files) > 0 {+// // fmt.Println()+// // for _, cmd := range []string{"apply", "diffs", "preview"} {+// // term.ClearCurrentLine()+// // term.PrintCmds(" ", cmd)+// // }+// // }++// // term.ClearCurrentLine()+// // term.PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")++// // term.ClearCurrentLine()+// // term.PrintCmds(" ", "continue")++// // term.ClearCurrentLine()+// // term.PrintCmds(" ", "rewind")+// // }++// // // fmt.Println("Finished proposal")++// // return nil+// // }++// // func Abort(proposalId string) error {+// // err := Api.Abort(proposalId)+// // return err+// // }++ // func checkOutdatedContext(s *spinner.Spinner) (bool, error) {+ // start := time.Now()+ // s.Prefix = "π¬ Checking context... "+ // s.Start()++ // stopSpinner := func() {+ // s.Stop()+ // term.ClearCurrentLine()+ // }++ // outdatedRes, err := CheckOutdatedContext()+ // if err != nil {+ // stopSpinner()+ // return false, fmt.Errorf("failed to check outdated context: %s", err)+ // }++ // elapsed := time.Since(start)+ // if elapsed < 700*time.Millisecond {+ // time.Sleep(700*time.Millisecond - elapsed)+ // }++ // stopSpinner()++ // if len(outdatedRes.UpdatedContexts) == 0 {+ // fmt.Println("β Context is up to date")+ // return true, nil+ // }+ // types := []string{}+ // if outdatedRes.NumFiles > 0 {+ // lbl := "file"+ // if outdatedRes.NumFiles > 1 {+ // lbl = "files"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl+ // types = append(types, lbl)+ // }+ // if outdatedRes.NumUrls > 0 {+ // lbl := "url"+ // if outdatedRes.NumUrls > 1 {+ // lbl = "urls"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl+ // types = append(types, lbl)+ // }+ // if outdatedRes.NumTrees > 0 {+ // lbl := "directory tree"+ // if outdatedRes.NumTrees > 1 {+ // lbl = "directory trees"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl+ // types = append(types, lbl)+ // }++ // var msg string+ // if len(types) <= 2 {+ // msg += strings.Join(types, " and ")+ // } else {+ // for i, add := range types {+ // if i == len(types)-1 {+ // msg += ", and " + add+ // } else {+ // msg += ", " + add+ // }+ // }+ // }++ // phrase := "have been"+ // if len(outdatedRes.UpdatedContexts) == 1 {+ // phrase = "has been"+ // }+ // color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)++ // tableString := TableForContextUpdateRes(outdatedRes)+ // fmt.Println(tableString)++ // fmt.Println()++ // confirmed, canceled, err := term.ConfirmYesNoCancel("Update context now?")++ // if err != nil {+ // return false, fmt.Errorf("failed to get user input: %s", err)+ // }++ // if confirmed {+ // MustUpdateContextWithOuput()+ // }++ // shouldContinue := !canceled++ // return shouldContinue, nil++ return nil+}++// func handleAbortKey(proposalId string) error {+// return Abort(proposalId)+// }++// func handleKeyPress(input rune, proposalId string) error {+// switch input {+// case 's':+// return handleAbortKey(proposalId)+// default:+// return fmt.Errorf("invalid key pressed: %s", string(input))+// }+// }++// func displayHotkeys() string {+// divisionLine := term.GetDivisionLine()++// return divisionLine + "\n" ++// " \x1b[1m(s)\x1b[0m" + `top+// ` + divisionLine+// }diff --git a/cli/lib/stream.go b/cli/lib/stream.goindex 4b20cfdf..2235a7da 100644--- a/cli/lib/stream.go+++ b/cli/lib/stream.go@@ -2,22 +2,36 @@ package libimport ("encoding/json"- "fmt"+ "log"+ "plandex/api"+ streamtui "plandex/stream_tui"+ "plandex/types""github.com/plandex/plandex/shared")-func updateTokenCounts(content string, numStreamedTokensByPath map[string]int, finishedByPath map[string]bool) error {- var planTokenCount shared.PlanTokenCount- err := json.Unmarshal([]byte(content), &planTokenCount)- if err != nil {- return fmt.Errorf("error parsing plan token count update: %v", err)- }- numStreamedTokensByPath[planTokenCount.Path] += planTokenCount.NumTokens+var OnStreamPlan api.OnStreamPlan = func(params api.OnStreamPlanParams) {++ switch params.State.Current() {+ case shared.STATE_REPLYING:+ streamtui.Send(types.StreamTUIUpdate{+ ReplyChunk: params.Content,+ })+ case shared.STATE_DESCRIBING:+ streamtui.Send(types.StreamTUIUpdate{+ Processing: true,+ })- if planTokenCount.Finished {- finishedByPath[planTokenCount.Path] = true+ case shared.STATE_BUILDING:+ var tokenCount shared.PlanTokenCount+ err := json.Unmarshal([]byte(params.Content), &tokenCount)+ if err != nil {+ log.Println("error parsing plan token count update:", err)+ return+ }+ streamtui.Send(types.StreamTUIUpdate{+ PlanTokenCount: &tokenCount,+ })}- return nil}diff --git a/cli/stream_tui/model.go b/cli/stream_tui/model.gonew file mode 100644index 00000000..dc098c36--- /dev/null+++ b/cli/stream_tui/model.go@@ -0,0 +1,61 @@+package streamtui++import (+ "time"++ bubbleKey "github.com/charmbracelet/bubbles/key"+ "github.com/charmbracelet/bubbles/spinner"+ "github.com/charmbracelet/bubbles/viewport"+ tea "github.com/charmbracelet/bubbletea"+ "github.com/charmbracelet/lipgloss"+)++type streamUIModel struct {+ keymap keymap++ reply string+ replyDisplay string+ replyDisplayUpdatedAt time.Time++ replyViewport viewport.Model++ processing bool+ spinner spinner.Model++ building bool+ tokensByPath map[string]int+ finishedByPath map[string]bool++ ready bool+ width int+ height int+}++type keymap = struct {+ quit bubbleKey.Binding+}++func (m streamUIModel) Init() tea.Cmd {+ return nil+}++func initialModel() *streamUIModel {+ s := spinner.New()+ s.Spinner = spinner.Dot+ s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))++ initialState := streamUIModel{+ keymap: keymap{+ quit: bubbleKey.NewBinding(+ bubbleKey.WithKeys("q", "ctrl+c"),+ bubbleKey.WithHelp("q", "quit"),+ ),+ },++ tokensByPath: make(map[string]int),+ finishedByPath: make(map[string]bool),+ spinner: s,+ }++ return &initialState+}diff --git a/cli/stream_tui/run.go b/cli/stream_tui/run.gonew file mode 100644index 00000000..150fa303--- /dev/null+++ b/cli/stream_tui/run.go@@ -0,0 +1,41 @@+package streamtui++import (+ "fmt"+ "log"++ tea "github.com/charmbracelet/bubbletea"+)++var ui *tea.Program++func StartStreamUI() error {+ initial := initialModel()++ ui = tea.NewProgram(initial, tea.WithAltScreen())++ _, err := ui.Run()++ if err != nil {+ return fmt.Errorf("error running changes UI: %v", err)+ }++ return nil+}++func Quit() {+ if ui == nil {+ log.Println("stream ui is nil, can't quit")+ return+ }+ ui.Quit()+}++func Send(msg tea.Msg) {+ if ui == nil {+ log.Println("stream ui is nil, can't send message")+ return+ }++ ui.Send(msg)+}diff --git a/cli/stream_tui/update.go b/cli/stream_tui/update.gonew file mode 100644index 00000000..bb57fbef--- /dev/null+++ b/cli/stream_tui/update.go@@ -0,0 +1,99 @@+package streamtui++import (+ "log"+ "plandex/term"+ "plandex/types"+ "time"++ bubbleKey "github.com/charmbracelet/bubbles/key"+ "github.com/charmbracelet/bubbles/spinner"+ "github.com/charmbracelet/bubbles/viewport"+ tea "github.com/charmbracelet/bubbletea"+ "github.com/charmbracelet/lipgloss"+)++const displayThrottle = 150 * time.Millisecond++func (m streamUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {++ switch msg := msg.(type) {++ case spinner.TickMsg:+ if m.processing {+ spinnerModel, cmd := m.spinner.Update(msg)+ m.spinner = spinnerModel+ return m, cmd+ }++ case tea.WindowSizeMsg:+ m.windowResized(msg.Width, msg.Height)++ case types.StreamTUIUpdate:+ now := time.Now()++ if msg.PlanTokenCount != nil {+ m.processing = false+ m.building = true++ if m.replyDisplay != m.reply {+ m.updateReplyDisplay(now)+ }++ m.tokensByPath[msg.PlanTokenCount.Path] += msg.PlanTokenCount.NumTokens+ if msg.PlanTokenCount.Finished {+ m.finishedByPath[msg.PlanTokenCount.Path] = true+ }+ } else if msg.ReplyChunk != "" {+ m.processing = false++ elapsed := now.Sub(m.replyDisplayUpdatedAt)++ m.reply += msg.ReplyChunk++ if elapsed >= displayThrottle {+ m.updateReplyDisplay(now)+ }++ } else if msg.Processing {+ m.processing = true+ if m.replyDisplay != m.reply {+ m.updateReplyDisplay(now)+ }+ return m, m.spinner.Tick+ }++ case tea.KeyMsg:+ switch {+ case bubbleKey.Matches(msg, m.keymap.quit):+ return m, tea.Quit+ }+ }++ return m, nil+}++func (m *streamUIModel) windowResized(w, h int) {+ m.width = w+ m.height = h+ if m.ready {+ m.replyViewport.Width = w+ m.replyViewport.Height = h+ } else {+ m.replyViewport = viewport.New(w, h)+ m.replyViewport.Style = lipgloss.NewStyle().Padding(0, 1, 0, 1)+ m.ready = true+ }+}++func (m *streamUIModel) updateReplyDisplay(now time.Time) {+ m.replyDisplay = m.reply+ md, err := term.GetMarkdown(m.reply)+ if err != nil {+ log.Println("error parsing markdown:", err)+ return+ }++ m.replyViewport.SetContent(md)+ m.replyDisplayUpdatedAt = now+}diff --git a/cli/stream_tui/view.go b/cli/stream_tui/view.gonew file mode 100644index 00000000..ea392cbf--- /dev/null+++ b/cli/stream_tui/view.go@@ -0,0 +1,66 @@+package streamtui++import (+ "fmt"+ "strings"++ "github.com/charmbracelet/lipgloss"+ "github.com/fatih/color"+)++var borderColor = lipgloss.Color("#444")+var helpTextColor = lipgloss.Color("#ddd")++func (m streamUIModel) View() string {+ return lipgloss.JoinVertical(lipgloss.Left,+ m.renderMainView(),+ m.renderProcessing(),+ m.renderBuild(),+ m.renderHelp(),+ )+}++func (m streamUIModel) renderMainView() string {+ return m.replyViewport.View()+}++func (m streamUIModel) renderHelp() string {+ style := lipgloss.NewStyle().Width(m.width).Foreground(lipgloss.Color(helpTextColor)).BorderTop(true).BorderForeground(lipgloss.Color(borderColor))++ return style.Render("(s)top")+}++func (m streamUIModel) renderProcessing() string {+ if m.processing {+ style := lipgloss.NewStyle().Width(m.width).BorderTop(true).BorderForeground(lipgloss.Color(borderColor))++ return style.Render(m.spinner.View())+ } else {+ return ""+ }+}++func (m streamUIModel) renderBuild() string {+ if !m.building {+ return ""+ }++ style := lipgloss.NewStyle().Width(m.width).BorderTop(true).BorderForeground(lipgloss.Color(borderColor))++ head := color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan ")++ var lines []string+ for filePath, tokens := range m.tokensByPath {+ finished := m.finishedByPath[filePath]+ line := fmt.Sprintf(" π %s", filePath)+ if tokens > 0 {+ line += fmt.Sprintf(" | %d πͺ", tokens)+ }+ if finished {+ line += " | β "+ }+ lines = append(lines, line)+ }++ return style.Render(head + strings.Join(lines, "\n"))+}diff --git a/cli/term/term.go b/cli/term/term.goindex 431508bf..67bc8ef7 100644--- a/cli/term/term.go+++ b/cli/term/term.go@@ -5,7 +5,9 @@ import ("os""os/exec""strings"+ "time"+ "github.com/briandowns/spinner""github.com/eiannone/keyboard""github.com/fatih/color""golang.org/x/term"@@ -31,6 +33,8 @@ var CmdDesc = map[string][2]string{"update": {"u", "update outdated context"},}+var Spinner = spinner.New(spinner.CharSets[33], 100*time.Millisecond)+func PrintCmds(prefix string, cmds ...string) {for _, cmd := range cmds {config, ok := CmdDesc[cmd]diff --git a/cli/types/types.go b/cli/types/types.goindex 8c645876..eb087805 100644--- a/cli/types/types.go+++ b/cli/types/types.go@@ -11,6 +11,7 @@ type LoadContextParams struct {type ContextOutdatedResult struct {Msg stringUpdatedContexts []*shared.Context+ TokenDiffsById map[string]intNumFiles intNumUrls intNumTrees int@@ -32,3 +33,9 @@ type ProjectSettings struct {Id string `json:"id"`OrgId string `json:"orgId"`}++type StreamTUIUpdate struct {+ ReplyChunk string+ Processing bool+ PlanTokenCount *shared.PlanTokenCount+}diff --git a/server/model/plan/build.go b/server/model/plan/build.goindex 3de03178..262d5d19 100644--- a/server/model/plan/build.go+++ b/server/model/plan/build.go@@ -45,32 +45,44 @@ func Build(currentOrgId, planId string) *types.ActivePlan {}func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {+ for _, filePath := range active.Files {+ tokenCount := &shared.PlanTokenCount{+ Path: filePath,+ NumTokens: 0,+ Finished: false,+ }+ json, err := json.Marshal(tokenCount)+ if err != nil {+ log.Printf("Error marshalling plan token count: %v\n", err)+ active.StreamDoneCh <- err+ return+ }+ active.StreamCh <- string(json)+ }+replyInfo := types.NewReplyInfo()replyInfo.AddToken(active.Content, true)_, fileContents, numTokensByFile, _ := replyInfo.FinishAndRead()- buildCh := make(chan *db.PlanBuild)- updateStatusCh := make(chan bool)- currentPlanCh := make(chan *shared.CurrentPlanState)errCh := make(chan error)var build *db.PlanBuildvar currentPlan *shared.CurrentPlanStatego func() {- build := db.PlanBuild{+ build = &db.PlanBuild{OrgId: currentOrgId,PlanId: planId,ConvoMessageId: active.AssistantMessageId,}- err := db.StorePlanBuild(&build)+ err := db.StorePlanBuild(build)if err != nil {errCh <- fmt.Errorf("error storing plan build: %v", err)return}- buildCh <- &build+ errCh <- nil}()go func() {@@ -79,11 +91,11 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {errCh <- fmt.Errorf("error setting plan status to building: %v", err)return}- updateStatusCh <- true+ errCh <- nil}()go func() {- currentPlan, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{+ res, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{OrgId: currentOrgId,PlanId: planId,Contexts: active.Contexts,@@ -92,18 +104,16 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {errCh <- fmt.Errorf("error getting current plan state: %v", err)return}- currentPlanCh <- currentPlan+ currentPlan = res+ errCh <- nil}()for i := 0; i < 3; i++ {- select {- case err := <-errCh:+ err := <-errCh+ if err != nil {log.Printf("Error building plan %s: %v\n", planId, err)active.StreamDoneCh <- errreturn- case <-updateStatusCh:- case build = <-buildCh:- case currentPlan = <-currentPlanCh:}}@@ -381,7 +391,7 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {log.Println("finish reason: " + choice.FinishReason)active := Active.Get(planId)- if active.BuiltFiles[filePath] == false {+ if !active.BuiltFiles[filePath] {log.Printf("Stream finished before replacements parsed. File: %s\n", filePath)log.Println("Buffer:")log.Println(active.BuildBuffers[filePath])diff --git a/shared/stream.go b/shared/stream.goindex 7a8c603c..ce2cc4ca 100644--- a/shared/stream.go+++ b/shared/stream.go@@ -6,12 +6,10 @@ const STREAM_MESSAGE_SEPARATOR = "@@PX@@"const STREAM_FINISHED = "@@PXEND@@"const STREAM_DESCRIPTION_PHASE = "@@PXDESC@@"const STREAM_BUILD_PHASE = "@@PXBUILD@@"-const STREAM_WRITE_PHASE = "@@PXWRITE@@"const STREAM_ABORTED = "@@PXABORT@@"const EVENT_DESCRIBE = "describe"const EVENT_BUILD = "build"-const EVENT_WRITE = "write"const EVENT_FINISH = "finish"const EVENT_ABORT = "abort"const EVENT_REVISE = "revise"@@ -21,7 +19,6 @@ const EVENT_ERROR = "error"const STATE_REPLYING = "replying"const STATE_DESCRIBING = "describing"const STATE_BUILDING = "building"-const STATE_WRITING = "writing"const STATE_FINISHED = "finished"const STATE_ABORTED = "aborted"const STATE_CANCELED = "canceled"@@ -34,13 +31,12 @@ func NewPlanStreamState() *fsm.FSM {// Define state transitions{Name: EVENT_DESCRIBE, Src: []string{STATE_REPLYING}, Dst: STATE_DESCRIBING},{Name: EVENT_BUILD, Src: []string{STATE_DESCRIBING}, Dst: STATE_BUILDING},- {Name: EVENT_WRITE, Src: []string{STATE_BUILDING}, Dst: STATE_WRITING},- {Name: EVENT_FINISH, Src: []string{STATE_DESCRIBING, STATE_WRITING}, Dst: STATE_FINISHED},+ {Name: EVENT_FINISH, Src: []string{STATE_DESCRIBING, STATE_BUILDING}, Dst: STATE_FINISHED},{Name: EVENT_ABORT, Src: []string{STATE_REPLYING, STATE_DESCRIBING, STATE_BUILDING},Dst: STATE_ABORTED},{Name: EVENT_REVISE, Src: []string{STATE_ABORTED}, Dst: STATE_REPLYING},{Name: EVENT_CANCEL, Src: []string{STATE_ABORTED}, Dst: STATE_CANCELED},- {Name: EVENT_ERROR, Src: []string{STATE_REPLYING, STATE_DESCRIBING, STATE_BUILDING, STATE_WRITING}},+ {Name: EVENT_ERROR, Src: []string{STATE_REPLYING, STATE_DESCRIBING, STATE_BUILDING}},},fsm.Callbacks{},)