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
refactoring and cleanup of tell logic. better handling of 'continue' command when last message in convo is a user message.
Dane Schneiderbb34e883a303
diff --git a/app/cli/cmd/continue.go b/app/cli/cmd/continue.goindex f1498b31..f5a1274a 100644--- a/app/cli/cmd/continue.go+++ b/app/cli/cmd/continue.go@@ -11,11 +11,7 @@ import ("github.com/spf13/cobra")-// Variables to be used in the nextCmd-const continuePrompt = "Continue the plan."--// nextCmd represents the prompt command-var nextCmd = &cobra.Command{+var continueCmd = &cobra.Command{Use: "continue",Aliases: []string{"c"},Short: "Continue the plan.",@@ -23,10 +19,10 @@ var nextCmd = &cobra.Command{}func init() {- RootCmd.AddCommand(nextCmd)+ RootCmd.AddCommand(continueCmd)- nextCmd.Flags().BoolVarP(&tellStop, "stop", "s", false, "Stop after a single reply")- nextCmd.Flags().BoolVarP(&tellNoBuild, "no-build", "n", false, "Don't build files")+ continueCmd.Flags().BoolVarP(&tellStop, "stop", "s", false, "Stop after a single reply")+ continueCmd.Flags().BoolVarP(&tellNoBuild, "no-build", "n", false, "Don't build files")}func doContinue(cmd *cobra.Command, args []string) {@@ -44,5 +40,5 @@ func doContinue(cmd *cobra.Command, args []string) {return}- tell.TellPlan(continuePrompt, tellBg, tellStop, tellNoBuild)+ tell.TellPlan("", tellBg, tellStop, tellNoBuild, true)}diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.goindex 0f26a868..fce98527 100644--- a/app/cli/cmd/tell.go+++ b/app/cli/cmd/tell.go@@ -124,7 +124,7 @@ func doTell(cmd *cobra.Command, args []string) {lib.MustCheckOutdatedContextWithOutput()- tell.TellPlan(prompt, tellBg, tellStop, tellNoBuild)+ tell.TellPlan(prompt, tellBg, tellStop, tellNoBuild, false)}func prepareEditorCommand(editor string, filename string) *exec.Cmd {diff --git a/app/cli/tell/tell.go b/app/cli/tell/tell.goindex f54e3757..90ffef39 100644--- a/app/cli/tell/tell.go+++ b/app/cli/tell/tell.go@@ -15,7 +15,7 @@ import ("github.com/plandex/plandex/shared")-func TellPlan(prompt string, tellBg, tellStop, tellNoBuild bool) {+func TellPlan(prompt string, tellBg, tellStop, tellNoBuild, isUserContinue bool) {projectPaths, _, err := fs.GetProjectPaths()if err != nil {@@ -36,12 +36,13 @@ func TellPlan(prompt string, tellBg, tellStop, tellNoBuild bool) {term.StartSpinner("💬 Sending prompt...")apiErr := api.Client.TellPlan(lib.CurrentPlanId, lib.CurrentBranch, shared.TellPlanRequest{- Prompt: prompt,- ConnectStream: !tellBg,- AutoContinue: !tellStop,- ProjectPaths: projectPaths,- BuildMode: buildMode,- ApiKey: os.Getenv("OPENAI_API_KEY"),+ Prompt: prompt,+ ConnectStream: !tellBg,+ AutoContinue: !tellStop,+ ProjectPaths: projectPaths,+ BuildMode: buildMode,+ IsUserContinue: isUserContinue,+ ApiKey: os.Getenv("OPENAI_API_KEY"),}, stream.OnStreamPlan)term.StopSpinner()@@ -73,6 +74,11 @@ func TellPlan(prompt string, tellBg, tellStop, tellNoBuild bool) {fmt.Fprintln(os.Stderr, "Prompt error:", apiErr.Msg)return false+ } else if isUserContinue && apiErr.Type == shared.ApiErrorTypeContinueNoMessages {+ fmt.Println("🤷♂️ There's no plan yet to continue")+ fmt.Println()+ term.PrintCmds("", "tell")+ os.Exit(1)}if !tellBg {diff --git a/app/server/model/plan/tell.go b/app/server/model/plan/tell.godeleted file mode 100644index ace657f4..00000000--- a/app/server/model/plan/tell.go+++ /dev/null@@ -1,1259 +0,0 @@-package plan--import (- "errors"- "fmt"- "log"- "net/http"- "os"- "strings"- "time"-- "plandex-server/db"- "plandex-server/model"- "plandex-server/model/lib"- "plandex-server/model/prompts"- "plandex-server/types"-- "github.com/davecgh/go-spew/spew"- "github.com/google/uuid"- "github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"-)--func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {- log.Printf("Tell: Called with plan ID %s on branch %s\n", plan.Id, branch)-- _, err := activatePlan(client, plan, branch, auth, req.Prompt, false)-- if err != nil {- log.Printf("Error activating plan: %v\n", err)- return err- }-- go execTellPlan(- client,- plan,- branch,- auth,- req,- 0,- "",- req.BuildMode == shared.BuildModeAuto,- )-- log.Printf("Tell: Tell operation completed successfully for plan ID %s on branch %s\n", plan.Id, branch)- return nil-}--func execTellPlan(- client *openai.Client,- plan *db.Plan,- branch string,- auth *types.ServerAuth,- req *shared.TellPlanRequest,- iteration int,- missingFileResponse shared.RespondMissingFileChoice,- shouldBuildPending bool,-) {- log.Printf("execTellPlan: Called for plan ID %s on branch %s, iteration %d\n", plan.Id, branch, iteration)- currentUserId := auth.User.Id- currentOrgId := auth.OrgId-- active := GetActivePlan(plan.Id, branch)-- if os.Getenv("IS_CLOUD") != "" &&- missingFileResponse == "" {- log.Println("execTellPlan: IS_CLOUD environment variable is set")- if auth.User.IsTrial {- if plan.TotalReplies >= types.TrialMaxReplies {- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeTrialMessagesExceeded,- Status: http.StatusForbidden,- Msg: "Free trial message limit exceeded",- TrialMessagesExceededError: &shared.TrialMessagesExceededError{- MaxReplies: types.TrialMaxReplies,- },- }- return- }- }- }-- planId := plan.Id- err := db.SetPlanStatus(planId, branch, shared.PlanStatusReplying, "")- if err != nil {- log.Printf("Error setting plan %s status to replying: %v\n", planId, err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error setting plan status to replying",- }-- log.Printf("execTellPlan: execTellPlan operation completed for plan ID %s on branch %s, iteration %d\n", plan.Id, branch, iteration)- return- }-- lockScope := db.LockScopeWrite- if iteration > 0 || missingFileResponse != "" {- lockScope = db.LockScopeRead- }- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: auth.OrgId,- UserId: auth.User.Id,- PlanId: planId,- Branch: branch,- Scope: lockScope,- Ctx: active.ModelStreamCtx,- CancelFn: active.CancelModelStreamFn,- },- )-- if err != nil {- log.Printf("execTellPlan: Error locking repo for plan ID %s on branch %s: %v\n", plan.Id, branch, err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo",- }- return- }-- errCh := make(chan error)- var modelContext []*db.Context- var convo []*db.ConvoMessage- var summaries []*db.ConvoSummary-- // get name for plan and rename it's a draft- go func() {- if plan.Name == "draft" {- name, err := model.GenPlanName(client, req.Prompt)-- if err != nil {- log.Printf("Error generating plan name: %v\n", err)- errCh <- fmt.Errorf("error generating plan name: %v", err)- return- }-- tx, err := db.Conn.Begin()- if err != nil {- log.Printf("Error starting transaction: %v\n", err)- errCh <- fmt.Errorf("error starting transaction: %v", err)- }-- // Ensure that rollback is attempted in case of failure- defer func() {- if err != nil {- if rbErr := tx.Rollback(); rbErr != nil {- log.Printf("transaction rollback error: %v\n", rbErr)- } else {- log.Println("transaction rolled back")- }- }- }()-- err = db.RenamePlan(planId, name, tx)-- if err != nil {- log.Printf("Error renaming plan: %v\n", err)- errCh <- fmt.Errorf("error renaming plan: %v", err)- return- }-- err = db.IncNumNonDraftPlans(currentUserId, tx)-- if err != nil {- log.Printf("Error incrementing num non draft plans: %v\n", err)- errCh <- fmt.Errorf("error incrementing num non draft plans: %v", err)- return- }-- err = tx.Commit()- if err != nil {- log.Printf("Error committing transaction: %v\n", err)- errCh <- fmt.Errorf("error committing transaction: %v", err)- return- }- }-- errCh <- nil- }()-- go func() {- if iteration > 0 || missingFileResponse != "" {- modelContext = active.Contexts- } else {- res, err := db.GetPlanContexts(currentOrgId, planId, true)- if err != nil {- log.Printf("Error getting plan modelContext: %v\n", err)- errCh <- fmt.Errorf("error getting plan modelContext: %v", err)- return- }- modelContext = res- }- errCh <- nil- }()-- go func() {- res, err := db.GetPlanConvo(currentOrgId, planId)- if err != nil {- log.Printf("Error getting plan convo: %v\n", err)- errCh <- fmt.Errorf("error getting plan convo: %v", err)- return- }- convo = res-- promptTokens, err := shared.GetNumTokens(req.Prompt)- if err != nil {- log.Printf("Error getting prompt num tokens: %v\n", err)- errCh <- fmt.Errorf("error getting prompt num tokens: %v", err)- return- }-- innerErrCh := make(chan error)-- go func() {- if iteration == 0 && missingFileResponse == "" {- num := len(convo) + 1-- userMsg := db.ConvoMessage{- OrgId: currentOrgId,- PlanId: planId,- UserId: currentUserId,- Role: openai.ChatMessageRoleUser,- Tokens: promptTokens,- Num: num,- Message: req.Prompt,- }-- _, err = db.StoreConvoMessage(&userMsg, auth.User.Id, branch, true)-- if err != nil {- log.Printf("Error storing user message: %v\n", err)- innerErrCh <- fmt.Errorf("error storing user message: %v", err)- return- }-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.MessageNum = num- })- }-- innerErrCh <- nil- }()-- go func() {- var convoMessageIds []string-- for _, convoMessage := range convo {- convoMessageIds = append(convoMessageIds, convoMessage.Id)- }-- res, err := db.GetPlanSummaries(planId, convoMessageIds)- if err != nil {- log.Printf("Error getting plan summaries: %v\n", err)- innerErrCh <- fmt.Errorf("error getting plan summaries: %v", err)- return- }- summaries = res-- innerErrCh <- nil- }()-- for i := 0; i < 2; i++ {- err := <-innerErrCh- if err != nil {- errCh <- err- return- }- }-- errCh <- nil- }()-- err = func() error {- var err error- defer func() {- if err != nil {- log.Printf("Error: %v\n", err)- err = db.GitClearUncommittedChanges(auth.OrgId, planId)- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }- }-- err = db.UnlockRepo(repoLockId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error unlocking repo",- }- return- }- }()-- for i := 0; i < 3; i++ {- err = <-errCh- if err != nil {- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error getting plan, context, convo, or summaries",- }- return err- }- }-- return nil- }()-- if err != nil {- return- }-- if iteration == 0 && missingFileResponse == "" {- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.Contexts = modelContext-- for _, context := range modelContext {- if context.FilePath != "" {- ap.ContextsByPath[context.FilePath] = context- }- }- })- } else if missingFileResponse == "" {- // reset current reply content and num tokens- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.CurrentReplyContent = ""- ap.NumTokens = 0- })- }-- // if any skipped paths have since been added to context, remove them from skipped paths- if len(active.SkippedPaths) > 0 {- var toUnskipPaths []string- for contextPath := range active.ContextsByPath {- if active.SkippedPaths[contextPath] {- toUnskipPaths = append(toUnskipPaths, contextPath)- }- }- if len(toUnskipPaths) > 0 {- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- for _, path := range toUnskipPaths {- delete(ap.SkippedPaths, path)- }- })- }- }-- modelContextText, modelContextTokens, err := lib.FormatModelContext(modelContext)- if err != nil {- err = fmt.Errorf("error formatting model modelContext: %v", err)- log.Println(err)-- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error formatting model modelContext",- }- return- }-- systemMessageText := prompts.SysCreate + modelContextText- systemMessage := openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleSystem,- Content: systemMessageText,- }-- if len(active.SkippedPaths) > 0 {- systemMessageText += "\n\nSome files have been skipped by the user and *must not* be generated. The user will handle any updates to these files themselves. Skip any parts of the plan that require generating these files. You *must not* generate a file block for any of these files.\nSkipped files:\n"- for skippedPath := range active.SkippedPaths {- systemMessageText += fmt.Sprintf("- %s\n", skippedPath)- }- }-- messages := []openai.ChatCompletionMessage{- systemMessage,- }-- var (- numPromptTokens int- promptTokens int- )- if iteration == 0 && missingFileResponse == "" {- numPromptTokens, err = shared.GetNumTokens(req.Prompt)- if err != nil {- err = fmt.Errorf("error getting number of tokens in prompt: %v", err)- log.Println(err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error getting number of tokens in prompt",- }- return- }- promptTokens = prompts.PromptWrapperTokens + numPromptTokens- }-- tokensBeforeConvo := prompts.CreateSysMsgNumTokens + modelContextTokens + promptTokens-- // print out breakdown of token usage- log.Printf("System message tokens: %d\n", prompts.CreateSysMsgNumTokens)- log.Printf("Context tokens: %d\n", modelContextTokens)- log.Printf("Prompt tokens: %d\n", promptTokens)- log.Printf("Total tokens before convo: %d\n", tokensBeforeConvo)-- if tokensBeforeConvo > shared.MaxTokens {- // token limit already exceeded before adding conversation- err := fmt.Errorf("token limit exceeded before adding conversation")- log.Printf("Error: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Token limit exceeded before adding conversation",- }- return- }-- conversationTokens := 0- tokensUpToTimestamp := make(map[int64]int)- for _, convoMessage := range convo {- conversationTokens += convoMessage.Tokens- timestamp := convoMessage.CreatedAt.UnixNano() / int64(time.Millisecond)- tokensUpToTimestamp[timestamp] = conversationTokens- // log.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)- }-- log.Printf("Conversation tokens: %d\n", conversationTokens)- log.Printf("Max conversation tokens: %d\n", shared.MaxConvoTokens)-- // log.Println("Tokens up to timestamp:")- // spew.Dump(tokensUpToTimestamp)-- log.Printf("Total tokens: %d\n", tokensBeforeConvo+conversationTokens)- log.Printf("Max tokens: %d\n", shared.MaxTokens)-- var summary *db.ConvoSummary- var summarizedToMessageId string- if (tokensBeforeConvo+conversationTokens) > shared.MaxTokens ||- conversationTokens > shared.MaxConvoTokens {- log.Println("Token limit exceeded. Attempting to reduce via conversation summary.")-- // log.Printf("(tokensBeforeConvo+conversationTokens) > shared.MaxTokens: %v\n", (tokensBeforeConvo+conversationTokens) > shared.MaxTokens)- // log.Printf("conversationTokens > shared.MaxConvoTokens: %v\n", conversationTokens > shared.MaxConvoTokens)-- // token limit exceeded after adding conversation- // get summary for as much as the conversation as necessary to stay under the token limit- for _, s := range summaries {- timestamp := s.LatestConvoMessageCreatedAt.UnixNano() / int64(time.Millisecond)-- tokens, ok := tokensUpToTimestamp[timestamp]-- log.Printf("Last message timestamp: %d | found: %v\n", timestamp, ok)- log.Printf("Tokens up to timestamp: %d\n", tokens)-- if !ok {- err := fmt.Errorf("conversation summary timestamp not found in conversation")- log.Printf("Error: %v\n", err)-- log.Println("timestamp:", timestamp)-- log.Println("Conversation summary:")- spew.Dump(s)-- log.Println("tokensUpToTimestamp:")- spew.Dump(tokensUpToTimestamp)-- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Conversation summary timestamp not found in conversation",- }- return- }-- updatedConversationTokens := (conversationTokens - tokens) + s.Tokens- savedTokens := conversationTokens - updatedConversationTokens-- log.Printf("Conversation summary tokens: %d\n", tokens)- log.Printf("Updated conversation tokens: %d\n", updatedConversationTokens)- log.Printf("Saved tokens: %d\n", savedTokens)-- if updatedConversationTokens <= shared.MaxConvoTokens &&- (tokensBeforeConvo+updatedConversationTokens) <= shared.MaxTokens {- log.Printf("Summarizing up to %s | saving %d tokens\n", s.LatestConvoMessageCreatedAt.Format(time.RFC3339), savedTokens)- summary = s- break- }- }-- if summary == nil {- err := errors.New("couldn't get under token limit with conversation summary")- log.Printf("Error: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Couldn't get under token limit with conversation summary",- }- return- }- }-- if summary == nil {- for _, convoMessage := range convo {- messages = append(messages, openai.ChatCompletionMessage{- Role: convoMessage.Role,- Content: convoMessage.Message,- })- }- } else {- if (tokensBeforeConvo + summary.Tokens) > shared.MaxTokens {- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Token limit still exceeded after summarizing conversation",- }- return- }- summarizedToMessageId = summary.LatestConvoMessageId- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: summary.Summary,- })-- // add messages after the last message in the summary- for _, convoMessage := range convo {- if convoMessage.CreatedAt.After(summary.LatestConvoMessageCreatedAt) {- messages = append(messages, openai.ChatCompletionMessage{- Role: convoMessage.Role,- Content: convoMessage.Message,- })- }- }- }-- replyId := uuid.New().String()- replyParser := types.NewReplyParser()- replyFiles := []string{}- replyNumTokens := 0- chunksReceived := 0- maybeRedundantBacktickContent := ""-- var promptMessage *openai.ChatCompletionMessage- var prompt string- if iteration == 0 {- prompt = req.Prompt- } else {- prompt = "Continue the plan."- }-- promptMessage = &openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(prompts.PromptWrapperFormatStr, prompt),- }-- if missingFileResponse == "" {- messages = append(messages, *promptMessage)- }-- if missingFileResponse != "" {- log.Println("Missing file response:", missingFileResponse, "setting replyParser")-- replyParser.AddChunk(active.CurrentReplyContent, true)- res := replyParser.Read()- currentFile := res.CurrentFilePath-- log.Printf("Current file: %s\n", currentFile)- // log.Println("Current reply content:\n", active.CurrentReplyContent)-- replyContent := active.CurrentReplyContent- numTokens := active.NumTokens-- if missingFileResponse == shared.RespondMissingFileChoiceSkip {- replyBeforeCurrentFile := replyParser.GetReplyBeforeCurrentPath()- numTokens, err = shared.GetNumTokens(replyBeforeCurrentFile)- if err != nil {- log.Printf("Error getting num tokens for reply before current file: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error getting num tokens for reply before current file",- }- return- }-- replyContent = replyBeforeCurrentFile- replyParser = types.NewReplyParser()- replyParser.AddChunk(replyContent, true)-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.CurrentReplyContent = replyContent- ap.NumTokens = numTokens- ap.SkippedPaths[currentFile] = true- })-- } else {- if missingFileResponse == shared.RespondMissingFileChoiceOverwrite {- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.AllowOverwritePaths[currentFile] = true- })- }- }- }-- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: active.CurrentReplyContent,- })-- if missingFileResponse != "" {- if missingFileResponse == shared.RespondMissingFileChoiceSkip {- res := replyParser.Read()-- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(`You *must not* generate content for the file %s. Skip this file and continue with the plan according to the 'Your instructions' section if there are any remaining tasks or subtasks. Don't repeat any part of the previous message. If there are no remaining tasks or subtasks, say "All tasks have been completed." per your instructions`, res.CurrentFilePath),- })-- } else {- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: "Continue generating exactly where you left off in the previous message. Don't produce any other output before continuing or repeat any part of the previous message. Do *not* duplicate the last line of the previous response before continuing. Do *not* include triple backticks and a language name like '```python' or '```yaml' at the start of the response, since these have already been included in the previous message. Continue from where you left off seamlessly to generate the rest of the file block. You must include closing triple backticks at the end of the file block. When the file block is finished, continue with the plan according to the 'Your instructions' sections if there are any remaining tasks or subtasks. If there are no remaining tasks or subtasks, say 'All tasks have been completed.' per your instructions.",- })- }- }-- // log.Println("\n\nMessages:")- // for _, message := range messages {- // log.Printf("%s: %s\n", message.Role, message.Content)- // }-- modelReq := openai.ChatCompletionRequest{- Model: model.PlannerModel,- Messages: messages,- Stream: true,- Temperature: 0.6,- TopP: 0.7,- }-- stream, err := client.CreateChatCompletionStream(active.ModelStreamCtx, modelReq)- if err != nil {- log.Printf("Error creating proposal GPT4 stream: %v\n", err)- log.Println(err)-- errStr := err.Error()- if strings.Contains(errStr, "status code: 400") &&- strings.Contains(errStr, "reduce the length of the messages") {- log.Println("Token limit exceeded")- }-- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error creating proposal GPT4 stream",- }- return- }-- if shouldBuildPending {- go func() {- pendingBuildsByPath, err := active.PendingBuildsByPath(auth.OrgId, auth.User.Id, convo)-- if err != nil {- log.Printf("Error getting pending builds by path: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error getting pending builds by path",- }- return- }-- if len(pendingBuildsByPath) == 0 {- log.Println("Tell plan: no pending builds")- return- }-- for _, pendingBuilds := range pendingBuildsByPath {- queueBuilds(client, auth.OrgId, auth.User.Id, planId, branch, pendingBuilds)- }- }()- }-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.CurrentStreamingReplyId = replyId- ap.CurrentReplyDoneCh = make(chan bool, 1)- })-- storeAssistantReply := func() (*db.ConvoMessage, []string, string, error) {- num := len(convo) + 1- if iteration == 0 && missingFileResponse == "" {- num++- }-- log.Printf("Storing assistant reply num %d\n", num)-- assistantMsg := db.ConvoMessage{- Id: replyId,- OrgId: currentOrgId,- PlanId: planId,- UserId: currentUserId,- Role: openai.ChatMessageRoleAssistant,- Tokens: replyNumTokens,- Num: num,- Message: GetActivePlan(planId, branch).CurrentReplyContent,- }-- commitMsg, err := db.StoreConvoMessage(&assistantMsg, auth.User.Id, branch, false)-- if err != nil {- log.Printf("Error storing assistant message: %v\n", err)- return nil, replyFiles, "", err- }-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.MessageNum = num- })-- return &assistantMsg, replyFiles, commitMsg, err- }-- onError := func(streamErr error, storeDesc bool, convoMessageId, commitMsg string) {- log.Printf("\nStream error: %v\n", streamErr)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Stream error: " + streamErr.Error(),- }-- storedMessage := false- storedDesc := false-- if convoMessageId == "" {- assistantMsg, _, msg, err := storeAssistantReply()- if err == nil {- convoMessageId = assistantMsg.Id- commitMsg = msg- storedMessage = true- } else {- log.Printf("Error storing assistant message after stream error: %v\n", err)- }- }-- if storeDesc && convoMessageId != "" {- err = db.StoreDescription(&db.ConvoMessageDescription{- OrgId: currentOrgId,- PlanId: planId,- SummarizedToMessageId: summarizedToMessageId,- MadePlan: false,- ConvoMessageId: convoMessageId,- Error: streamErr.Error(),- })- if err == nil {- storedDesc = true- } else {- log.Printf("Error storing description after stream error: %v\n", err)- }- }-- if storedMessage || storedDesc {- err = db.GitAddAndCommit(currentOrgId, planId, branch, commitMsg)- if err != nil {- log.Printf("Error committing after stream error: %v\n", err)- }- }- }-- go func() {- defer stream.Close()-- // Create a timer that will trigger if no chunk is received within the specified duration- timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)- defer timer.Stop()-- for {- select {- case <-active.Ctx.Done():- // The main modelContext was canceled (not the timer)- log.Println("\nTell: stream canceled")- return- case <-timer.C:- // Timer triggered because no new chunk was received in time- log.Println("\nTell: stream timeout due to inactivity")- onError(fmt.Errorf("stream timeout due to inactivity"), true, "", "")- return- default:- response, err := stream.Recv()-- if err == nil {- // Successfully received a chunk, reset the timer- if !timer.Stop() {- <-timer.C- }- timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)- }-- if err != nil {- if err.Error() == "context canceled" {- log.Println("Tell: stream context canceled")- return- }-- onError(fmt.Errorf("stream error: %v", err), true, "", "")- return- }-- if len(response.Choices) == 0 {- onError(fmt.Errorf("stream finished with no choices"), true, "", "")- return- }-- if len(response.Choices) > 1 {- onError(fmt.Errorf("stream finished with more than one choice"), true, "", "")- return- }-- choice := response.Choices[0]-- if choice.FinishReason != "" {- log.Println("Model stream finished")-- active.Stream(shared.StreamMessage{- Type: shared.StreamMessageDescribing,- })-- err := db.SetPlanStatus(planId, branch, shared.PlanStatusDescribing, "")- if err != nil {- onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "", "")- return- }- // log.Println("summarize convo:", spew.Sdump(convo))-- if len(convo) > 0 {- // summarize in the background- go summarizeConvo(client, summarizeConvoParams{- planId: planId,- branch: branch,- convo: convo,- summaries: summaries,- promptMessage: promptMessage,- currentOrgId: currentOrgId,- })- }-- log.Println("Locking repo for assistant reply and description")-- repoLockId, err := db.LockRepo(- db.LockRepoParams{- OrgId: currentOrgId,- UserId: currentUserId,- PlanId: planId,- Branch: branch,- Scope: db.LockScopeWrite,- Ctx: active.Ctx,- CancelFn: active.CancelFn,- },- )-- if err != nil {- log.Printf("Error locking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error locking repo",- }- return- }-- log.Println("Locked repo for assistant reply and description")-- var shouldContinue bool- err = func() error {- defer func() {- if err != nil {- log.Printf("Error storing reply and description: %v\n", err)- err = db.GitClearUncommittedChanges(auth.OrgId, planId)- if err != nil {- log.Printf("Error clearing uncommitted changes: %v\n", err)- }- }-- log.Println("Unlocking repo for assistant reply and description")-- err = db.UnlockRepo(repoLockId)- if err != nil {- log.Printf("Error unlocking repo: %v\n", err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error unlocking repo",- }- }- }()-- assistantMsg, files, convoCommitMsg, err := storeAssistantReply()-- if err != nil {- onError(fmt.Errorf("failed to store assistant message: %v", err), true, "", "")- return err- }-- var description *db.ConvoMessageDescription-- errCh := make(chan error, 2)-- go func() {- if len(files) == 0 {- description = &db.ConvoMessageDescription{- OrgId: currentOrgId,- PlanId: planId,- ConvoMessageId: assistantMsg.Id,- SummarizedToMessageId: summarizedToMessageId,- MadePlan: false,- }- } else {- description, err = genPlanDescription(client, planId, branch, active.Ctx)- if err != nil {- onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)- return- }-- description.OrgId = currentOrgId- description.ConvoMessageId = assistantMsg.Id- description.SummarizedToMessageId = summarizedToMessageId- description.MadePlan = true- description.Files = files- }-- err = db.StoreDescription(description)-- if err != nil {- onError(fmt.Errorf("failed to store description: %v", err), false, assistantMsg.Id, convoCommitMsg)- errCh <- err- return- }-- errCh <- nil- }()-- go func() {- shouldContinue, err = ExecStatusShouldContinue(client, assistantMsg.Message, active.Ctx)- if err != nil {- onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)- errCh <- err- return- }-- log.Printf("Should continue: %v\n", shouldContinue)-- errCh <- nil- }()-- for i := 0; i < 2; i++ {- err = <-errCh- if err != nil {- return err- }- }-- log.Println("Comitting reply message and description")-- err = db.GitAddAndCommit(currentOrgId, planId, branch, convoCommitMsg)- if err != nil {- onError(fmt.Errorf("failed to commit: %v", err), false, assistantMsg.Id, convoCommitMsg)- return err- }-- log.Println("Assistant reply and description committed")-- return nil- }()-- if err != nil {- return- }-- log.Println("Sending active.CurrentReplyDoneCh <- true")-- active.CurrentReplyDoneCh <- true-- log.Println("Resetting active.CurrentReplyDoneCh")-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.CurrentStreamingReplyId = ""- ap.CurrentReplyDoneCh = nil- })-- if req.AutoContinue && shouldContinue {- log.Println("Auto continue plan")- // continue plan- execTellPlan(client, plan, branch, auth, req, iteration+1, "", false)- } else {- var buildFinished bool- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- buildFinished = ap.BuildFinished()- ap.RepliesFinished = true- })-- log.Printf("Won't continue plan. Build finished: %v\n", buildFinished)-- if buildFinished {- active.Stream(shared.StreamMessage{- Type: shared.StreamMessageFinished,- })- } else {- log.Println("Sending RepliesFinished stream message")- active.Stream(shared.StreamMessage{- Type: shared.StreamMessageRepliesFinished,- })- }- }-- return- }-- chunksReceived++- delta := choice.Delta- content := delta.Content-- if missingFileResponse != "" {- if maybeRedundantBacktickContent != "" {- if strings.Contains(content, "\n") {- maybeRedundantBacktickContent = ""- } else {- maybeRedundantBacktickContent += content- }- continue- } else if chunksReceived < 3 && strings.Contains(content, "```") {- // received closing triple backticks in first 3 chunks after missing file response- // means this is a redundant start of a new file block, so just ignore it-- maybeRedundantBacktickContent += content- continue- }- }-- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.CurrentReplyContent += content- ap.NumTokens++- })-- // log.Printf("Sending stream msg: %s", content)- active.Stream(shared.StreamMessage{- Type: shared.StreamMessageReply,- ReplyChunk: content,- })-- // log.Printf("content: %s\n", content)-- replyParser.AddChunk(content, true)- res := replyParser.Read()- files := res.Files- fileContents := res.FileContents- replyNumTokens = res.TotalTokens- currentFile := res.CurrentFilePath-- // log.Printf("currentFile: %s\n", currentFile)- // log.Println("files:")- // spew.Dump(files)-- // Handle file that is present in project paths but not in context- // Prompt user for what to do on the client side, stop the stream, and wait for user response before proceeding- if currentFile != "" &&- active.ContextsByPath[currentFile] == nil &&- req.ProjectPaths[currentFile] && !active.AllowOverwritePaths[currentFile] {- log.Printf("Attempting to overwrite a file that isn't in context: %s\n", currentFile)-- // attempting to overwrite a file that isn't in context- // we will stop the stream and ask the user what to do- err := db.SetPlanStatus(planId, branch, shared.PlanStatusPrompting, "")-- if err != nil {- log.Printf("Error setting plan %s status to prompting: %v\n", planId, err)- active.StreamDoneCh <- &shared.ApiError{- Type: shared.ApiErrorTypeOther,- Status: http.StatusInternalServerError,- Msg: "Error setting plan status to prompting",- }- return- }-- log.Printf("Prompting user for missing file: %s\n", currentFile)-- active.Stream(shared.StreamMessage{- Type: shared.StreamMessagePromptMissingFile,- MissingFilePath: currentFile,- })-- log.Printf("Stopping stream for missing file: %s\n", currentFile)-- // log.Printf("Current reply content: %s\n", active.CurrentReplyContent)-- // stop stream for now- active.CancelModelStreamFn()-- log.Printf("Stopped stream for missing file: %s\n", currentFile)-- // wait for user response to come in- userChoice := <-active.MissingFileResponseCh-- log.Printf("User choice for missing file: %s\n", userChoice)-- active.ResetModelCtx()-- log.Println("Continuing stream")-- // continue plan- execTellPlan(- client,- plan,- branch,- auth,- req,- iteration, // keep the same iteration- userChoice,- false,- )- return- }-- // log.Println("Content:", content)- // log.Println("Current reply content:", active.CurrentReplyContent)- // log.Println("Current file:", currentFile)- // log.Println("files:")- // spew.Dump(files)- // log.Println("replyFiles:")- // spew.Dump(replyFiles)-- if len(files) > len(replyFiles) {- log.Printf("%d new files\n", len(files)-len(replyFiles))-- for i, file := range files {- if i < len(replyFiles) {- continue- }-- log.Printf("New file: %s\n", file)- if req.BuildMode == shared.BuildModeAuto {- log.Printf("Queuing build for %s\n", file)- queueBuilds(client, currentOrgId, currentUserId, planId, branch, []*types.ActiveBuild{{- ReplyId: replyId,- ReplyContent: active.CurrentReplyContent,- FileContent: fileContents[i],- Path: file,- }})- }- replyFiles = append(replyFiles, file)- UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {- ap.Files = append(ap.Files, file)- })- }- }- }- }- }()-}--type summarizeConvoParams struct {- planId string- branch string- convo []*db.ConvoMessage- summaries []*db.ConvoSummary- promptMessage *openai.ChatCompletionMessage- currentOrgId string-}--func summarizeConvo(client *openai.Client, params summarizeConvoParams) error {- log.Printf("summarizeConvo: Called for plan ID %s on branch %s\n", params.planId, params.branch)- log.Printf("summarizeConvo: Starting summarizeConvo for planId: %s\n", params.planId)- planId := params.planId- branch := params.branch- convo := params.convo- summaries := params.summaries- promptMessage := params.promptMessage- currentOrgId := params.currentOrgId-- log.Println("Generating plan summary for planId:", planId)-- // log.Printf("planId: %s\n", planId)- // log.Printf("convo: ")- // spew.Dump(convo)- // log.Printf("summaries: ")- // spew.Dump(summaries)- // log.Printf("promptMessage: ")- // spew.Dump(promptMessage)- // log.Printf("currentOrgId: %s\n", currentOrgId)-- var summaryMessages []*openai.ChatCompletionMessage- var latestSummary *db.ConvoSummary- var numMessagesSummarized int = 0- var latestMessageSummarizedAt time.Time- var latestMessageId string- if len(summaries) > 0 {- latestSummary = summaries[len(summaries)-1]- numMessagesSummarized = latestSummary.NumMessages- }-- // log.Println("Latest summary:")- // spew.Dump(latestSummary)-- if latestSummary == nil {- for _, convoMessage := range convo {- summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{- Role: convoMessage.Role,- Content: convoMessage.Message,- })- latestMessageId = convoMessage.Id- latestMessageSummarizedAt = convoMessage.CreatedAt- }- } else {- summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: latestSummary.Summary,- })-- latestConvoMessage := convo[len(convo)-1]- latestMessageId = latestConvoMessage.Id- latestMessageSummarizedAt = latestConvoMessage.CreatedAt-- summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{- Role: latestConvoMessage.Role,- Content: latestConvoMessage.Message,- })- }-- if promptMessage != nil {- summaryMessages = append(summaryMessages, promptMessage, &openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: GetActivePlan(planId, branch).CurrentReplyContent,- })- }-- summary, err := model.PlanSummary(client, model.PlanSummaryParams{- Conversation: summaryMessages,- LatestConvoMessageId: latestMessageId,- LatestConvoMessageCreatedAt: latestMessageSummarizedAt,- NumMessages: numMessagesSummarized + 1,- OrgId: currentOrgId,- PlanId: planId,- })-- if err != nil {- log.Printf("summarizeConvo: Error generating plan summary for plan %s: %v\n", params.planId, err)- return err- }-- log.Printf("summarizeConvo: Summary generated and stored for plan %s\n", params.planId)-- err = db.StoreSummary(summary)-- if err != nil {- log.Printf("Error storing plan summary for plan %s: %v\n", planId, err)- return err- }-- return nil-}diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.gonew file mode 100644index 00000000..4422ca46--- /dev/null+++ b/app/server/model/plan/tell_exec.go@@ -0,0 +1,398 @@+package plan++import (+ "fmt"+ "log"+ "net/http"+ "os"+ "strings"++ "plandex-server/db"+ "plandex-server/model"+ "plandex-server/model/lib"+ "plandex-server/model/prompts"+ "plandex-server/types"++ "github.com/google/uuid"+ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {+ log.Printf("Tell: Called with plan ID %s on branch %s\n", plan.Id, branch)++ _, err := activatePlan(client, plan, branch, auth, req.Prompt, false)++ if err != nil {+ log.Printf("Error activating plan: %v\n", err)+ return err+ }++ go execTellPlan(+ client,+ plan,+ branch,+ auth,+ req,+ 0,+ "",+ req.BuildMode == shared.BuildModeAuto,+ )++ log.Printf("Tell: Tell operation completed successfully for plan ID %s on branch %s\n", plan.Id, branch)+ return nil+}++func execTellPlan(+ client *openai.Client,+ plan *db.Plan,+ branch string,+ auth *types.ServerAuth,+ req *shared.TellPlanRequest,+ iteration int,+ missingFileResponse shared.RespondMissingFileChoice,+ shouldBuildPending bool,+) {+ log.Printf("execTellPlan: Called for plan ID %s on branch %s, iteration %d\n", plan.Id, branch, iteration)+ currentUserId := auth.User.Id+ currentOrgId := auth.OrgId++ active := GetActivePlan(plan.Id, branch)++ if os.Getenv("IS_CLOUD") != "" &&+ missingFileResponse == "" {+ log.Println("execTellPlan: IS_CLOUD environment variable is set")+ if auth.User.IsTrial {+ if plan.TotalReplies >= types.TrialMaxReplies {+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeTrialMessagesExceeded,+ Status: http.StatusForbidden,+ Msg: "Free trial message limit exceeded",+ TrialMessagesExceededError: &shared.TrialMessagesExceededError{+ MaxReplies: types.TrialMaxReplies,+ },+ }+ return+ }+ }+ }++ planId := plan.Id+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusReplying, "")+ if err != nil {+ log.Printf("Error setting plan %s status to replying: %v\n", planId, err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error setting plan status to replying",+ }++ log.Printf("execTellPlan: execTellPlan operation completed for plan ID %s on branch %s, iteration %d\n", plan.Id, branch, iteration)+ return+ }++ state := &activeTellStreamState{+ client: client,+ req: req,+ auth: auth,+ currentOrgId: currentOrgId,+ currentUserId: currentUserId,+ plan: plan,+ branch: branch,+ iteration: iteration,+ missingFileResponse: missingFileResponse,+ }++ err = state.loadTellPlan()+ if err != nil {+ return+ }++ if iteration == 0 && missingFileResponse == "" {+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.Contexts = state.modelContext++ for _, context := range state.modelContext {+ if context.FilePath != "" {+ ap.ContextsByPath[context.FilePath] = context+ }+ }+ })+ } else if missingFileResponse == "" {+ // reset current reply content and num tokens+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.CurrentReplyContent = ""+ ap.NumTokens = 0+ })+ }++ // if any skipped paths have since been added to context, remove them from skipped paths+ if len(active.SkippedPaths) > 0 {+ var toUnskipPaths []string+ for contextPath := range active.ContextsByPath {+ if active.SkippedPaths[contextPath] {+ toUnskipPaths = append(toUnskipPaths, contextPath)+ }+ }+ if len(toUnskipPaths) > 0 {+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ for _, path := range toUnskipPaths {+ delete(ap.SkippedPaths, path)+ }+ })+ }+ }++ modelContextText, modelContextTokens, err := lib.FormatModelContext(state.modelContext)+ if err != nil {+ err = fmt.Errorf("error formatting model modelContext: %v", err)+ log.Println(err)++ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error formatting model modelContext",+ }+ return+ }++ systemMessageText := prompts.SysCreate + modelContextText+ systemMessage := openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: systemMessageText,+ }++ if len(active.SkippedPaths) > 0 {+ systemMessageText += prompts.SkippedPathsPrompt+ for skippedPath := range active.SkippedPaths {+ systemMessageText += fmt.Sprintf("- %s\n", skippedPath)+ }+ }++ state.messages = []openai.ChatCompletionMessage{+ systemMessage,+ }++ var (+ numPromptTokens int+ promptTokens int+ )+ if iteration == 0 && missingFileResponse == "" {+ numPromptTokens, err = shared.GetNumTokens(req.Prompt)+ if err != nil {+ err = fmt.Errorf("error getting number of tokens in prompt: %v", err)+ log.Println(err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting number of tokens in prompt",+ }+ return+ }+ promptTokens = prompts.PromptWrapperTokens + numPromptTokens+ }++ state.tokensBeforeConvo = prompts.CreateSysMsgNumTokens + modelContextTokens + promptTokens++ // print out breakdown of token usage+ log.Printf("System message tokens: %d\n", prompts.CreateSysMsgNumTokens)+ log.Printf("Context tokens: %d\n", modelContextTokens)+ log.Printf("Prompt tokens: %d\n", promptTokens)+ log.Printf("Total tokens before convo: %d\n", state.tokensBeforeConvo)++ if state.tokensBeforeConvo > shared.MaxTokens {+ // token limit already exceeded before adding conversation+ err := fmt.Errorf("token limit exceeded before adding conversation")+ log.Printf("Error: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Token limit exceeded before adding conversation",+ }+ return+ }++ if !state.summarizeMessagesIfNeeded() {+ return+ }++ state.replyId = uuid.New().String()+ state.replyParser = types.NewReplyParser()++ var promptMessage *openai.ChatCompletionMessage+ if missingFileResponse == "" {++ if req.IsUserContinue {+ if len(state.messages) == 0 {+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeContinueNoMessages,+ Status: http.StatusBadRequest,+ Msg: "No messages yet. Can't continue plan.",+ }+ return+ }++ // if the user is continuing the plan, we need to check whether the previous message was a user message or assistant message+ lastMessage := state.messages[len(state.messages)-1]++ if lastMessage.Role == openai.ChatMessageRoleUser {+ // if last message was a user message, we want to remove it from the messages array and then use that last message as the prompt so we can continue from where the user left off++ state.messages = state.messages[:len(state.messages)-1]+ promptMessage = &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.GetWrappedPrompt(lastMessage.Content),+ }+ } else {+ // otherwise we'll use the continue prompt+ promptMessage = &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.GetWrappedPrompt(prompts.UserContinuePrompt),+ }+ }++ state.messages = append(state.messages, *promptMessage)+ } else {+ var prompt string+ if iteration == 0 {+ prompt = req.Prompt+ } else {+ prompt = prompts.AutoContinuePrompt+ }++ promptMessage = &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.GetWrappedPrompt(prompt),+ }+ }++ state.messages = append(state.messages, *promptMessage)+ } else {+ log.Println("Missing file response:", missingFileResponse, "setting replyParser")++ state.replyParser.AddChunk(active.CurrentReplyContent, true)+ res := state.replyParser.Read()+ currentFile := res.CurrentFilePath++ log.Printf("Current file: %s\n", currentFile)+ // log.Println("Current reply content:\n", active.CurrentReplyContent)++ replyContent := active.CurrentReplyContent+ numTokens := active.NumTokens++ if missingFileResponse == shared.RespondMissingFileChoiceSkip {+ replyBeforeCurrentFile := state.replyParser.GetReplyBeforeCurrentPath()+ numTokens, err = shared.GetNumTokens(replyBeforeCurrentFile)+ if err != nil {+ log.Printf("Error getting num tokens for reply before current file: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting num tokens for reply before current file",+ }+ return+ }++ replyContent = replyBeforeCurrentFile+ state.replyParser = types.NewReplyParser()+ state.replyParser.AddChunk(replyContent, true)++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.CurrentReplyContent = replyContent+ ap.NumTokens = numTokens+ ap.SkippedPaths[currentFile] = true+ })++ } else {+ if missingFileResponse == shared.RespondMissingFileChoiceOverwrite {+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.AllowOverwritePaths[currentFile] = true+ })+ }+ }++ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: active.CurrentReplyContent,+ })++ if missingFileResponse == shared.RespondMissingFileChoiceSkip {+ res := state.replyParser.Read()++ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.GetSkipMissingFilePrompt(res.CurrentFilePath),+ })++ } else {+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.MissingFileContinueGeneratingPrompt,+ })+ }+ }++ // log.Println("\n\nMessages:")+ // for _, message := range messages {+ // log.Printf("%s: %s\n", message.Role, message.Content)+ // }++ modelReq := openai.ChatCompletionRequest{+ Model: model.PlannerModel,+ Messages: state.messages,+ Stream: true,+ Temperature: 0.6,+ TopP: 0.7,+ }++ stream, err := client.CreateChatCompletionStream(active.ModelStreamCtx, modelReq)+ if err != nil {+ log.Printf("Error creating proposal GPT4 stream: %v\n", err)+ log.Println(err)++ errStr := err.Error()+ if strings.Contains(errStr, "status code: 400") &&+ strings.Contains(errStr, "reduce the length of the messages") {+ log.Println("Token limit exceeded")+ }++ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error creating proposal GPT4 stream",+ }+ return+ }++ if shouldBuildPending {+ go func() {+ pendingBuildsByPath, err := active.PendingBuildsByPath(auth.OrgId, auth.User.Id, state.convo)++ if err != nil {+ log.Printf("Error getting pending builds by path: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting pending builds by path",+ }+ return+ }++ if len(pendingBuildsByPath) == 0 {+ log.Println("Tell plan: no pending builds")+ return+ }++ for _, pendingBuilds := range pendingBuildsByPath {+ queueBuilds(client, auth.OrgId, auth.User.Id, planId, branch, pendingBuilds)+ }+ }()+ }++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.CurrentStreamingReplyId = state.replyId+ ap.CurrentReplyDoneCh = make(chan bool, 1)+ })++ go state.listenStream(stream)+}diff --git a/app/server/model/plan/tell_load.go b/app/server/model/plan/tell_load.gonew file mode 100644index 00000000..90333a56--- /dev/null+++ b/app/server/model/plan/tell_load.go@@ -0,0 +1,254 @@+package plan++import (+ "fmt"+ "log"+ "net/http"+ "plandex-server/db"+ "plandex-server/model"+ "plandex-server/types"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func (state *activeTellStreamState) loadTellPlan() error {+ client := state.client+ req := state.req+ auth := state.auth+ plan := state.plan+ planId := plan.Id+ branch := state.branch+ currentUserId := state.currentUserId+ currentOrgId := state.currentOrgId+ iteration := state.iteration+ missingFileResponse := state.missingFileResponse++ active := GetActivePlan(plan.Id, branch)++ lockScope := db.LockScopeWrite+ if iteration > 0 || missingFileResponse != "" {+ lockScope = db.LockScopeRead+ }+ repoLockId, err := db.LockRepo(+ db.LockRepoParams{+ OrgId: auth.OrgId,+ UserId: auth.User.Id,+ PlanId: planId,+ Branch: branch,+ Scope: lockScope,+ Ctx: active.ModelStreamCtx,+ CancelFn: active.CancelModelStreamFn,+ },+ )++ if err != nil {+ log.Printf("execTellPlan: Error locking repo for plan ID %s on branch %s: %v\n", plan.Id, branch, err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error locking repo",+ }+ return err+ }++ errCh := make(chan error)+ var modelContext []*db.Context+ var convo []*db.ConvoMessage+ var summaries []*db.ConvoSummary++ // get name for plan and rename it's a draft+ go func() {+ if plan.Name == "draft" {+ name, err := model.GenPlanName(client, req.Prompt)++ if err != nil {+ log.Printf("Error generating plan name: %v\n", err)+ errCh <- fmt.Errorf("error generating plan name: %v", err)+ return+ }++ tx, err := db.Conn.Begin()+ if err != nil {+ log.Printf("Error starting transaction: %v\n", err)+ errCh <- fmt.Errorf("error starting transaction: %v", err)+ }++ // Ensure that rollback is attempted in case of failure+ defer func() {+ if err != nil {+ if rbErr := tx.Rollback(); rbErr != nil {+ log.Printf("transaction rollback error: %v\n", rbErr)+ } else {+ log.Println("transaction rolled back")+ }+ }+ }()++ err = db.RenamePlan(planId, name, tx)++ if err != nil {+ log.Printf("Error renaming plan: %v\n", err)+ errCh <- fmt.Errorf("error renaming plan: %v", err)+ return+ }++ err = db.IncNumNonDraftPlans(currentUserId, tx)++ if err != nil {+ log.Printf("Error incrementing num non draft plans: %v\n", err)+ errCh <- fmt.Errorf("error incrementing num non draft plans: %v", err)+ return+ }++ err = tx.Commit()+ if err != nil {+ log.Printf("Error committing transaction: %v\n", err)+ errCh <- fmt.Errorf("error committing transaction: %v", err)+ return+ }+ }++ errCh <- nil+ }()++ go func() {+ if iteration > 0 || missingFileResponse != "" {+ modelContext = active.Contexts+ } else {+ res, err := db.GetPlanContexts(currentOrgId, planId, true)+ if err != nil {+ log.Printf("Error getting plan modelContext: %v\n", err)+ errCh <- fmt.Errorf("error getting plan modelContext: %v", err)+ return+ }+ modelContext = res+ }+ errCh <- nil+ }()++ go func() {+ res, err := db.GetPlanConvo(currentOrgId, planId)+ if err != nil {+ log.Printf("Error getting plan convo: %v\n", err)+ errCh <- fmt.Errorf("error getting plan convo: %v", err)+ return+ }+ convo = res++ promptTokens, err := shared.GetNumTokens(req.Prompt)+ if err != nil {+ log.Printf("Error getting prompt num tokens: %v\n", err)+ errCh <- fmt.Errorf("error getting prompt num tokens: %v", err)+ return+ }++ innerErrCh := make(chan error)++ go func() {+ if iteration == 0 && missingFileResponse == "" {+ num := len(convo) + 1++ userMsg := db.ConvoMessage{+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleUser,+ Tokens: promptTokens,+ Num: num,+ Message: req.Prompt,+ }++ _, err = db.StoreConvoMessage(&userMsg, auth.User.Id, branch, true)++ if err != nil {+ log.Printf("Error storing user message: %v\n", err)+ innerErrCh <- fmt.Errorf("error storing user message: %v", err)+ return+ }++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.MessageNum = num+ })+ }++ innerErrCh <- nil+ }()++ go func() {+ var convoMessageIds []string++ for _, convoMessage := range convo {+ convoMessageIds = append(convoMessageIds, convoMessage.Id)+ }++ res, err := db.GetPlanSummaries(planId, convoMessageIds)+ if err != nil {+ log.Printf("Error getting plan summaries: %v\n", err)+ innerErrCh <- fmt.Errorf("error getting plan summaries: %v", err)+ return+ }+ summaries = res++ innerErrCh <- nil+ }()++ for i := 0; i < 2; i++ {+ err := <-innerErrCh+ if err != nil {+ errCh <- err+ return+ }+ }++ errCh <- nil+ }()++ err = func() error {+ var err error+ defer func() {+ if err != nil {+ log.Printf("Error: %v\n", err)+ err = db.GitClearUncommittedChanges(auth.OrgId, planId)+ if err != nil {+ log.Printf("Error clearing uncommitted changes: %v\n", err)+ }+ }++ err = db.UnlockRepo(repoLockId)+ if err != nil {+ log.Printf("Error unlocking repo: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error unlocking repo",+ }+ return+ }+ }()++ for i := 0; i < 3; i++ {+ err = <-errCh+ if err != nil {+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error getting plan, context, convo, or summaries",+ }+ return err+ }+ }++ return nil+ }()++ if err != nil {+ return err+ }++ state.modelContext = modelContext+ state.convo = convo+ state.summaries = summaries++ return nil+}diff --git a/app/server/model/plan/tell_stream.go b/app/server/model/plan/tell_stream.gonew file mode 100644index 00000000..5eddac1a--- /dev/null+++ b/app/server/model/plan/tell_stream.go@@ -0,0 +1,546 @@+package plan++import (+ "fmt"+ "log"+ "net/http"+ "plandex-server/db"+ "plandex-server/model"+ "plandex-server/types"+ "strings"+ "time"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++type activeTellStreamState struct {+ client *openai.Client+ req *shared.TellPlanRequest+ auth *types.ServerAuth+ currentOrgId string+ currentUserId string+ plan *db.Plan+ branch string+ iteration int+ replyId string+ modelContext []*db.Context+ convo []*db.ConvoMessage+ missingFileResponse shared.RespondMissingFileChoice+ summaries []*db.ConvoSummary+ summarizedToMessageId string+ promptMessage *openai.ChatCompletionMessage+ replyParser *types.ReplyParser+ replyNumTokens int+ messages []openai.ChatCompletionMessage+ tokensBeforeConvo int+}++func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStream) {+ defer stream.Close()++ client := state.client+ auth := state.auth+ req := state.req+ plan := state.plan+ planId := plan.Id+ branch := state.branch+ currentOrgId := state.currentOrgId+ currentUserId := state.currentUserId+ convo := state.convo+ summaries := state.summaries+ summarizedToMessageId := state.summarizedToMessageId+ promptMessage := state.promptMessage+ iteration := state.iteration+ missingFileResponse := state.missingFileResponse+ replyId := state.replyId+ replyParser := state.replyParser++ active := GetActivePlan(planId, branch)++ replyFiles := []string{}+ chunksReceived := 0+ maybeRedundantBacktickContent := ""++ // Create a timer that will trigger if no chunk is received within the specified duration+ timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ defer timer.Stop()++ for {+ select {+ case <-active.Ctx.Done():+ // The main modelContext was canceled (not the timer)+ log.Println("\nTell: stream canceled")+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ log.Println("\nTell: stream timeout due to inactivity")+ state.onError(fmt.Errorf("stream timeout due to inactivity"), true, "", "")+ return+ default:+ response, err := stream.Recv()++ if err == nil {+ // Successfully received a chunk, reset the timer+ if !timer.Stop() {+ <-timer.C+ }+ timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ }++ if err != nil {+ if err.Error() == "context canceled" {+ log.Println("Tell: stream context canceled")+ return+ }++ state.onError(fmt.Errorf("stream error: %v", err), true, "", "")+ return+ }++ if len(response.Choices) == 0 {+ state.onError(fmt.Errorf("stream finished with no choices"), true, "", "")+ return+ }++ if len(response.Choices) > 1 {+ state.onError(fmt.Errorf("stream finished with more than one choice"), true, "", "")+ return+ }++ choice := response.Choices[0]++ if choice.FinishReason != "" {+ log.Println("Model stream finished")++ active.Stream(shared.StreamMessage{+ Type: shared.StreamMessageDescribing,+ })++ err := db.SetPlanStatus(planId, branch, shared.PlanStatusDescribing, "")+ if err != nil {+ state.onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "", "")+ return+ }+ // log.Println("summarize convo:", spew.Sdump(convo))++ if len(convo) > 0 {+ // summarize in the background+ go summarizeConvo(client, summarizeConvoParams{+ planId: planId,+ branch: branch,+ convo: convo,+ summaries: summaries,+ promptMessage: promptMessage,+ currentOrgId: currentOrgId,+ })+ }++ log.Println("Locking repo for assistant reply and description")++ repoLockId, err := db.LockRepo(+ db.LockRepoParams{+ OrgId: currentOrgId,+ UserId: currentUserId,+ PlanId: planId,+ Branch: branch,+ Scope: db.LockScopeWrite,+ Ctx: active.Ctx,+ CancelFn: active.CancelFn,+ },+ )++ if err != nil {+ log.Printf("Error locking repo: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error locking repo",+ }+ return+ }++ log.Println("Locked repo for assistant reply and description")++ var shouldContinue bool+ err = func() error {+ defer func() {+ if err != nil {+ log.Printf("Error storing reply and description: %v\n", err)+ err = db.GitClearUncommittedChanges(auth.OrgId, planId)+ if err != nil {+ log.Printf("Error clearing uncommitted changes: %v\n", err)+ }+ }++ log.Println("Unlocking repo for assistant reply and description")++ err = db.UnlockRepo(repoLockId)+ if err != nil {+ log.Printf("Error unlocking repo: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error unlocking repo",+ }+ }+ }()++ assistantMsg, convoCommitMsg, err := state.storeAssistantReply()++ if err != nil {+ state.onError(fmt.Errorf("failed to store assistant message: %v", err), true, "", "")+ return err+ }++ var description *db.ConvoMessageDescription++ errCh := make(chan error, 2)++ go func() {+ if len(replyFiles) == 0 {+ description = &db.ConvoMessageDescription{+ OrgId: currentOrgId,+ PlanId: planId,+ ConvoMessageId: assistantMsg.Id,+ SummarizedToMessageId: summarizedToMessageId,+ MadePlan: false,+ }+ } else {+ description, err = genPlanDescription(client, planId, branch, active.Ctx)+ if err != nil {+ state.onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)+ return+ }++ description.OrgId = currentOrgId+ description.ConvoMessageId = assistantMsg.Id+ description.SummarizedToMessageId = summarizedToMessageId+ description.MadePlan = true+ description.Files = replyFiles+ }++ err = db.StoreDescription(description)++ if err != nil {+ state.onError(fmt.Errorf("failed to store description: %v", err), false, assistantMsg.Id, convoCommitMsg)+ errCh <- err+ return+ }++ errCh <- nil+ }()++ go func() {+ shouldContinue, err = ExecStatusShouldContinue(client, assistantMsg.Message, active.Ctx)+ if err != nil {+ state.onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)+ errCh <- err+ return+ }++ log.Printf("Should continue: %v\n", shouldContinue)++ errCh <- nil+ }()++ for i := 0; i < 2; i++ {+ err = <-errCh+ if err != nil {+ return err+ }+ }++ log.Println("Comitting reply message and description")++ err = db.GitAddAndCommit(currentOrgId, planId, branch, convoCommitMsg)+ if err != nil {+ state.onError(fmt.Errorf("failed to commit: %v", err), false, assistantMsg.Id, convoCommitMsg)+ return err+ }++ log.Println("Assistant reply and description committed")++ return nil+ }()++ if err != nil {+ return+ }++ log.Println("Sending active.CurrentReplyDoneCh <- true")++ active.CurrentReplyDoneCh <- true++ log.Println("Resetting active.CurrentReplyDoneCh")++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.CurrentStreamingReplyId = ""+ ap.CurrentReplyDoneCh = nil+ })++ if req.AutoContinue && shouldContinue {+ log.Println("Auto continue plan")+ // continue plan+ execTellPlan(client, plan, branch, auth, req, iteration+1, "", false)+ } else {+ var buildFinished bool+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ buildFinished = ap.BuildFinished()+ ap.RepliesFinished = true+ })++ log.Printf("Won't continue plan. Build finished: %v\n", buildFinished)++ if buildFinished {+ active.Stream(shared.StreamMessage{+ Type: shared.StreamMessageFinished,+ })+ } else {+ log.Println("Sending RepliesFinished stream message")+ active.Stream(shared.StreamMessage{+ Type: shared.StreamMessageRepliesFinished,+ })+ }+ }++ return+ }++ chunksReceived+++ delta := choice.Delta+ content := delta.Content++ if missingFileResponse != "" {+ if maybeRedundantBacktickContent != "" {+ if strings.Contains(content, "\n") {+ maybeRedundantBacktickContent = ""+ } else {+ maybeRedundantBacktickContent += content+ }+ continue+ } else if chunksReceived < 3 && strings.Contains(content, "```") {+ // received closing triple backticks in first 3 chunks after missing file response+ // means this is a redundant start of a new file block, so just ignore it++ maybeRedundantBacktickContent += content+ continue+ }+ }++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.CurrentReplyContent += content+ ap.NumTokens+++ })++ // log.Printf("Sending stream msg: %s", content)+ active.Stream(shared.StreamMessage{+ Type: shared.StreamMessageReply,+ ReplyChunk: content,+ })++ // log.Printf("content: %s\n", content)++ replyParser.AddChunk(content, true)+ res := replyParser.Read()+ files := res.Files+ fileContents := res.FileContents+ state.replyNumTokens = res.TotalTokens+ currentFile := res.CurrentFilePath++ // log.Printf("currentFile: %s\n", currentFile)+ // log.Println("files:")+ // spew.Dump(files)++ // Handle file that is present in project paths but not in context+ // Prompt user for what to do on the client side, stop the stream, and wait for user response before proceeding+ if currentFile != "" &&+ active.ContextsByPath[currentFile] == nil &&+ req.ProjectPaths[currentFile] && !active.AllowOverwritePaths[currentFile] {+ log.Printf("Attempting to overwrite a file that isn't in context: %s\n", currentFile)++ // attempting to overwrite a file that isn't in context+ // we will stop the stream and ask the user what to do+ err := db.SetPlanStatus(planId, branch, shared.PlanStatusPrompting, "")++ if err != nil {+ log.Printf("Error setting plan %s status to prompting: %v\n", planId, err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Error setting plan status to prompting",+ }+ return+ }++ log.Printf("Prompting user for missing file: %s\n", currentFile)++ active.Stream(shared.StreamMessage{+ Type: shared.StreamMessagePromptMissingFile,+ MissingFilePath: currentFile,+ })++ log.Printf("Stopping stream for missing file: %s\n", currentFile)++ // log.Printf("Current reply content: %s\n", active.CurrentReplyContent)++ // stop stream for now+ active.CancelModelStreamFn()++ log.Printf("Stopped stream for missing file: %s\n", currentFile)++ // wait for user response to come in+ userChoice := <-active.MissingFileResponseCh++ log.Printf("User choice for missing file: %s\n", userChoice)++ active.ResetModelCtx()++ log.Println("Continuing stream")++ // continue plan+ execTellPlan(+ client,+ plan,+ branch,+ auth,+ req,+ iteration, // keep the same iteration+ userChoice,+ false,+ )+ return+ }++ // log.Println("Content:", content)+ // log.Println("Current reply content:", active.CurrentReplyContent)+ // log.Println("Current file:", currentFile)+ // log.Println("files:")+ // spew.Dump(files)+ // log.Println("replyFiles:")+ // spew.Dump(replyFiles)++ if len(files) > len(replyFiles) {+ log.Printf("%d new files\n", len(files)-len(replyFiles))++ for i, file := range files {+ if i < len(replyFiles) {+ continue+ }++ log.Printf("New file: %s\n", file)+ if req.BuildMode == shared.BuildModeAuto {+ log.Printf("Queuing build for %s\n", file)+ queueBuilds(client, currentOrgId, currentUserId, planId, branch, []*types.ActiveBuild{{+ ReplyId: replyId,+ ReplyContent: active.CurrentReplyContent,+ FileContent: fileContents[i],+ Path: file,+ }})+ }+ replyFiles = append(replyFiles, file)+ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.Files = append(ap.Files, file)+ })+ }+ }+ }+ }+}++func (state *activeTellStreamState) storeAssistantReply() (*db.ConvoMessage, string, error) {+ currentOrgId := state.currentOrgId+ currentUserId := state.currentUserId+ planId := state.plan.Id+ branch := state.branch+ auth := state.auth+ replyNumTokens := state.replyNumTokens+ iteration := state.iteration+ replyId := state.replyId+ convo := state.convo+ missingFileResponse := state.missingFileResponse++ num := len(convo) + 1+ if iteration == 0 && missingFileResponse == "" {+ num+++ }++ log.Printf("Storing assistant reply num %d\n", num)++ assistantMsg := db.ConvoMessage{+ Id: replyId,+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleAssistant,+ Tokens: replyNumTokens,+ Num: num,+ Message: GetActivePlan(planId, branch).CurrentReplyContent,+ }++ commitMsg, err := db.StoreConvoMessage(&assistantMsg, auth.User.Id, branch, false)++ if err != nil {+ log.Printf("Error storing assistant message: %v\n", err)+ return nil, "", err+ }++ UpdateActivePlan(planId, branch, func(ap *types.ActivePlan) {+ ap.MessageNum = num+ })++ return &assistantMsg, commitMsg, err+}++func (state *activeTellStreamState) onError(streamErr error, storeDesc bool, convoMessageId, commitMsg string) {+ log.Printf("\nStream error: %v\n", streamErr)++ planId := state.plan.Id+ branch := state.branch+ currentOrgId := state.currentOrgId+ summarizedToMessageId := state.summarizedToMessageId++ active := GetActivePlan(planId, branch)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Stream error: " + streamErr.Error(),+ }++ storedMessage := false+ storedDesc := false++ if convoMessageId == "" {+ assistantMsg, msg, err := state.storeAssistantReply()+ if err == nil {+ convoMessageId = assistantMsg.Id+ commitMsg = msg+ storedMessage = true+ } else {+ log.Printf("Error storing assistant message after stream error: %v\n", err)+ }+ }++ if storeDesc && convoMessageId != "" {+ err := db.StoreDescription(&db.ConvoMessageDescription{+ OrgId: currentOrgId,+ PlanId: planId,+ SummarizedToMessageId: summarizedToMessageId,+ MadePlan: false,+ ConvoMessageId: convoMessageId,+ Error: streamErr.Error(),+ })+ if err == nil {+ storedDesc = true+ } else {+ log.Printf("Error storing description after stream error: %v\n", err)+ }+ }++ if storedMessage || storedDesc {+ err := db.GitAddAndCommit(currentOrgId, planId, branch, commitMsg)+ if err != nil {+ log.Printf("Error committing after stream error: %v\n", err)+ }+ }+}diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.gonew file mode 100644index 00000000..f74b29ca--- /dev/null+++ b/app/server/model/plan/tell_summary.go@@ -0,0 +1,242 @@+package plan++import (+ "errors"+ "fmt"+ "log"+ "net/http"+ "plandex-server/db"+ "plandex-server/model"+ "time"++ "github.com/davecgh/go-spew/spew"+ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++func (state *activeTellStreamState) summarizeMessagesIfNeeded() bool {+ convo := state.convo+ summaries := state.summaries+ tokensBeforeConvo := state.tokensBeforeConvo++ active := GetActivePlan(state.plan.Id, state.branch)++ conversationTokens := 0+ tokensUpToTimestamp := make(map[int64]int)+ for _, convoMessage := range convo {+ conversationTokens += convoMessage.Tokens+ timestamp := convoMessage.CreatedAt.UnixNano() / int64(time.Millisecond)+ tokensUpToTimestamp[timestamp] = conversationTokens+ // log.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)+ }++ log.Printf("Conversation tokens: %d\n", conversationTokens)+ log.Printf("Max conversation tokens: %d\n", shared.MaxConvoTokens)++ // log.Println("Tokens up to timestamp:")+ // spew.Dump(tokensUpToTimestamp)++ log.Printf("Total tokens: %d\n", tokensBeforeConvo+conversationTokens)+ log.Printf("Max tokens: %d\n", shared.MaxTokens)++ var summary *db.ConvoSummary+ if (tokensBeforeConvo+conversationTokens) > shared.MaxTokens ||+ conversationTokens > shared.MaxConvoTokens {+ log.Println("Token limit exceeded. Attempting to reduce via conversation summary.")++ // log.Printf("(tokensBeforeConvo+conversationTokens) > shared.MaxTokens: %v\n", (tokensBeforeConvo+conversationTokens) > shared.MaxTokens)+ // log.Printf("conversationTokens > shared.MaxConvoTokens: %v\n", conversationTokens > shared.MaxConvoTokens)++ // token limit exceeded after adding conversation+ // get summary for as much as the conversation as necessary to stay under the token limit+ for _, s := range summaries {+ timestamp := s.LatestConvoMessageCreatedAt.UnixNano() / int64(time.Millisecond)++ tokens, ok := tokensUpToTimestamp[timestamp]++ log.Printf("Last message timestamp: %d | found: %v\n", timestamp, ok)+ log.Printf("Tokens up to timestamp: %d\n", tokens)++ if !ok {+ err := fmt.Errorf("conversation summary timestamp not found in conversation")+ log.Printf("Error: %v\n", err)++ log.Println("timestamp:", timestamp)++ log.Println("Conversation summary:")+ spew.Dump(s)++ log.Println("tokensUpToTimestamp:")+ spew.Dump(tokensUpToTimestamp)++ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Conversation summary timestamp not found in conversation",+ }+ return false+ }++ updatedConversationTokens := (conversationTokens - tokens) + s.Tokens+ savedTokens := conversationTokens - updatedConversationTokens++ log.Printf("Conversation summary tokens: %d\n", tokens)+ log.Printf("Updated conversation tokens: %d\n", updatedConversationTokens)+ log.Printf("Saved tokens: %d\n", savedTokens)++ if updatedConversationTokens <= shared.MaxConvoTokens &&+ (tokensBeforeConvo+updatedConversationTokens) <= shared.MaxTokens {+ log.Printf("Summarizing up to %s | saving %d tokens\n", s.LatestConvoMessageCreatedAt.Format(time.RFC3339), savedTokens)+ summary = s+ break+ }+ }++ if summary == nil {+ err := errors.New("couldn't get under token limit with conversation summary")+ log.Printf("Error: %v\n", err)+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Couldn't get under token limit with conversation summary",+ }+ return false+ }+ }++ if summary == nil {+ for _, convoMessage := range convo {+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ }+ } else {+ if (tokensBeforeConvo + summary.Tokens) > shared.MaxTokens {+ active.StreamDoneCh <- &shared.ApiError{+ Type: shared.ApiErrorTypeOther,+ Status: http.StatusInternalServerError,+ Msg: "Token limit still exceeded after summarizing conversation",+ }+ return false+ }+ state.summarizedToMessageId = summary.LatestConvoMessageId+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: summary.Summary,+ })++ // add messages after the last message in the summary+ for _, convoMessage := range convo {+ if convoMessage.CreatedAt.After(summary.LatestConvoMessageCreatedAt) {+ state.messages = append(state.messages, openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ }+ }+ }++ return true+}++type summarizeConvoParams struct {+ planId string+ branch string+ convo []*db.ConvoMessage+ summaries []*db.ConvoSummary+ promptMessage *openai.ChatCompletionMessage+ currentOrgId string+}++func summarizeConvo(client *openai.Client, params summarizeConvoParams) error {+ log.Printf("summarizeConvo: Called for plan ID %s on branch %s\n", params.planId, params.branch)+ log.Printf("summarizeConvo: Starting summarizeConvo for planId: %s\n", params.planId)+ planId := params.planId+ branch := params.branch+ convo := params.convo+ summaries := params.summaries+ promptMessage := params.promptMessage+ currentOrgId := params.currentOrgId++ log.Println("Generating plan summary for planId:", planId)++ // log.Printf("planId: %s\n", planId)+ // log.Printf("convo: ")+ // spew.Dump(convo)+ // log.Printf("summaries: ")+ // spew.Dump(summaries)+ // log.Printf("promptMessage: ")+ // spew.Dump(promptMessage)+ // log.Printf("currentOrgId: %s\n", currentOrgId)++ var summaryMessages []*openai.ChatCompletionMessage+ var latestSummary *db.ConvoSummary+ var numMessagesSummarized int = 0+ var latestMessageSummarizedAt time.Time+ var latestMessageId string+ if len(summaries) > 0 {+ latestSummary = summaries[len(summaries)-1]+ numMessagesSummarized = latestSummary.NumMessages+ }++ // log.Println("Latest summary:")+ // spew.Dump(latestSummary)++ if latestSummary == nil {+ for _, convoMessage := range convo {+ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ latestMessageId = convoMessage.Id+ latestMessageSummarizedAt = convoMessage.CreatedAt+ }+ } else {+ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: latestSummary.Summary,+ })++ latestConvoMessage := convo[len(convo)-1]+ latestMessageId = latestConvoMessage.Id+ latestMessageSummarizedAt = latestConvoMessage.CreatedAt++ summaryMessages = append(summaryMessages, &openai.ChatCompletionMessage{+ Role: latestConvoMessage.Role,+ Content: latestConvoMessage.Message,+ })+ }++ if promptMessage != nil {+ summaryMessages = append(summaryMessages, promptMessage, &openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: GetActivePlan(planId, branch).CurrentReplyContent,+ })+ }++ summary, err := model.PlanSummary(client, model.PlanSummaryParams{+ Conversation: summaryMessages,+ LatestConvoMessageId: latestMessageId,+ LatestConvoMessageCreatedAt: latestMessageSummarizedAt,+ NumMessages: numMessagesSummarized + 1,+ OrgId: currentOrgId,+ PlanId: planId,+ })++ if err != nil {+ log.Printf("summarizeConvo: Error generating plan summary for plan %s: %v\n", params.planId, err)+ return err+ }++ log.Printf("summarizeConvo: Summary generated and stored for plan %s\n", params.planId)++ err = db.StoreSummary(summary)++ if err != nil {+ log.Printf("Error storing plan summary for plan %s: %v\n", planId, err)+ return err+ }++ return nil+}diff --git a/app/server/model/prompts/create.go b/app/server/model/prompts/create.goindex f995dad6..82cd5abe 100644--- a/app/server/model/prompts/create.go+++ b/app/server/model/prompts/create.go@@ -101,6 +101,16 @@ const SysCreate = Identity + ` A plan is a set of files with an attached contextvar CreateSysMsgNumTokens, _ = shared.GetNumTokens(SysCreate)-const PromptWrapperFormatStr = "The user's latest prompt:\n```\n%s\n```\n\n Please respond according to the 'Your instructions' section above. If you're making a plan, remember to precede code blocks with the file path *exactly* as described in 2a, and do not use any other formatting for file paths. **Do not include explanations or any other text apart from the file path in code block labels.** Always use triple backticks to start and end code blocks. Only list out subtasks once for the plan--after that, do not list or describe a subtask that can be implemented in code without including a code block that implements the subtask. Do not ask the user to do anything that you can do yourself with a code block. If you're making a plan, also remember to end every response with either " + `"All tasks have been completed.", "Next, " (plus a brief descripton of the next step), or "The plan cannot be continued." according to your instructions for ending a response.`+const promptWrapperFormatStr = "The user's latest prompt:\n```\n%s\n```\n\n Please respond according to the 'Your instructions' section above. If you're making a plan, remember to precede code blocks with the file path *exactly* as described in 2a, and do not use any other formatting for file paths. **Do not include explanations or any other text apart from the file path in code block labels.** Always use triple backticks to start and end code blocks. Only list out subtasks once for the plan--after that, do not list or describe a subtask that can be implemented in code without including a code block that implements the subtask. Do not ask the user to do anything that you can do yourself with a code block. If you're making a plan, also remember to end every response with either " + `"All tasks have been completed.", "Next, " (plus a brief descripton of the next step), or "The plan cannot be continued." according to your instructions for ending a response.`-var PromptWrapperTokens, _ = shared.GetNumTokens(fmt.Sprintf(PromptWrapperFormatStr, ""))+func GetWrappedPrompt(prompt string) string {+ return fmt.Sprintf(promptWrapperFormatStr, prompt)+}++var PromptWrapperTokens, _ = shared.GetNumTokens(fmt.Sprintf(promptWrapperFormatStr, ""))++const UserContinuePrompt = "Continue the plan."++const AutoContinuePrompt = "Continue the plan from where you left off in the previous response. Don't repeat any part of your previous response. Don't begin your response with 'Next,'. Continue seamlessly from where your previous response left off. Never begin your response with 'The plan cannot be continued.' or 'All tasks have been completed.'."++const SkippedPathsPrompt = "\n\nSome files have been skipped by the user and *must not* be generated. The user will handle any updates to these files themselves. Skip any parts of the plan that require generating these files. You *must not* generate a file block for any of these files.\nSkipped files:\n"diff --git a/app/server/model/prompts/missing_file.go b/app/server/model/prompts/missing_file.gonew file mode 100644index 00000000..0e946a2b--- /dev/null+++ b/app/server/model/prompts/missing_file.go@@ -0,0 +1,9 @@+package prompts++import "fmt"++func GetSkipMissingFilePrompt(path string) string {+ return fmt.Sprintf(`You *must not* generate content for the file %s. Skip this file and continue with the plan according to the 'Your instructions' section if there are any remaining tasks or subtasks. Don't repeat any part of the previous message. If there are no remaining tasks or subtasks, say "All tasks have been completed." per your instructions`, path)+}++const MissingFileContinueGeneratingPrompt = "Continue generating exactly where you left off in the previous message. Don't produce any other output before continuing or repeat any part of the previous message. Do *not* duplicate the last line of the previous response before continuing. Do *not* include triple backticks and a language name like '```python' or '```yaml' at the start of the response, since these have already been included in the previous message. Continue from where you left off seamlessly to generate the rest of the file block. You must include closing triple backticks at the end of the file block. When the file block is finished, continue with the plan according to the 'Your instructions' sections if there are any remaining tasks or subtasks. If there are no remaining tasks or subtasks, say 'All tasks have been completed.' per your instructions."diff --git a/app/server/types/reply.go b/app/server/types/reply.goindex 5eb21675..b7d26ca3 100644--- a/app/server/types/reply.go+++ b/app/server/types/reply.go@@ -14,7 +14,7 @@ type parserRes struct {TotalTokens int}-type replyParser struct {+type ReplyParser struct {lines []stringcurrentFileLines []stringlineIndex int@@ -27,8 +27,8 @@ type replyParser struct {numTokensByFile map[string]int}-func NewReplyParser() *replyParser {- info := &replyParser{+func NewReplyParser() *ReplyParser {+ info := &ReplyParser{lines: []string{""},currentFileLines: []string{},files: []string{},@@ -39,7 +39,7 @@ func NewReplyParser() *replyParser {return info}-func (r *replyParser) AddChunk(chunk string, addToTotal bool) {+func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {// log.Println("Adding chunk:", strconv.Quote(chunk)) // Logging the chunk that's being processedhasNewLine := false@@ -153,7 +153,7 @@ func (r *replyParser) AddChunk(chunk string, addToTotal bool) {}}-func (r *replyParser) Read() parserRes {+func (r *ReplyParser) Read() parserRes {return parserRes{CurrentFilePath: r.currentFilePath,Files: r.files,@@ -163,12 +163,12 @@ func (r *replyParser) Read() parserRes {}}-func (r *replyParser) FinishAndRead() parserRes {+func (r *ReplyParser) FinishAndRead() parserRes {r.AddChunk("\n", false)return r.Read()}-func (r *replyParser) GetReplyBeforeCurrentPath() string {+func (r *ReplyParser) GetReplyBeforeCurrentPath() string {if r.currentFilePath == "" {return strings.Join(r.lines, "\n")}diff --git a/app/shared/auth.go b/app/shared/auth.goindex 7634b67d..9f8dacbf 100644--- a/app/shared/auth.go+++ b/app/shared/auth.go@@ -13,6 +13,8 @@ const (ApiErrorTypeTrialMessagesExceeded ApiErrorType = "trial_messages_exceeded"ApiErrorTypeTrialActionNotAllowed ApiErrorType = "trial_action_not_allowed"+ ApiErrorTypeContinueNoMessages ApiErrorType = "continue_no_messages"+ApiErrorTypeOther ApiErrorType = "other")diff --git a/app/shared/req_res.go b/app/shared/req_res.goindex 4f37d37d..c683536d 100644--- a/app/shared/req_res.go+++ b/app/shared/req_res.go@@ -97,12 +97,13 @@ const ()type TellPlanRequest struct {- Prompt string `json:"prompt"`- BuildMode BuildMode `json:"buildMode"`- ConnectStream bool `json:"connectStream"`- AutoContinue bool `json:"autoContinue"`- ApiKey string `json:"apiKey"`- ProjectPaths map[string]bool `json:"projectPaths"`+ Prompt string `json:"prompt"`+ BuildMode BuildMode `json:"buildMode"`+ ConnectStream bool `json:"connectStream"`+ AutoContinue bool `json:"autoContinue"`+ IsUserContinue bool `json:"isUserContinue"`+ ApiKey string `json:"apiKey"`+ ProjectPaths map[string]bool `json:"projectPaths"`}type BuildPlanRequest struct {