Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

working on convo summaries, fair amount of refactoring

Dane Schneider655185460c43
diff --git a/cli/cmd/convo.go b/cli/cmd/convo.go
new file mode 100644
index 00000000..cea8d9f1
--- /dev/null
+++ b/cli/cmd/convo.go
@@ -0,0 +1,66 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "plandex/lib"
+ "strings"
+
+ "github.com/fatih/color"
+ "github.com/spf13/cobra"
+)
+
+// convoCmd represents the convo command
+var convoCmd = &cobra.Command{
+ Use: "convo",
+ Short: "Display complete conversation history.",
+ Run: convo,
+}
+
+func init() {
+ RootCmd.AddCommand(convoCmd)
+}
+
+func convo(cmd *cobra.Command, args []string) {
+ conversation, err := lib.LoadConversation()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error loading conversation:", err)
+ return
+ }
+
+ if len(conversation) == 0 {
+ fmt.Println("No conversation history.")
+ return
+ }
+
+ var convMarkdown []string
+ var totalTokens int
+ for _, msg := range conversation {
+ var author string
+ if msg.Message.Role == "assistant" {
+ author = "Plandex"
+ } else if msg.Message.Role == "user" {
+ author = "You"
+ } else {
+ author = msg.Message.Role
+ }
+
+ header := fmt.Sprintf("#### %s | %s | %d πŸͺ™",
+ author, msg.Timestamp, msg.Tokens)
+ convMarkdown = append(convMarkdown, header, msg.Message.Content, "")
+ totalTokens += msg.Tokens
+ }
+
+ markdownString, err := lib.GetMarkdown(strings.Join(convMarkdown, "\n"))
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error creating markdown representation:", err)
+ return
+ }
+
+ fmt.Print(markdownString)
+
+ fmt.Println(lib.GetDivisionLine())
+ fmt.Println()
+ fmt.Println(color.New(color.Bold, color.FgCyan).Sprint(" Conversation size β†’") + fmt.Sprintf(" %d πŸͺ™", totalTokens))
+ fmt.Println()
+}
diff --git a/cli/lib/api.go b/cli/lib/api.go
index 389fb5f0..9627302d 100644
--- a/cli/lib/api.go
+++ b/cli/lib/api.go
@@ -1,267 +1,21 @@
package lib
import (
- "bufio"
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
+ "os"
"plandex/types"
-
- "github.com/plandex/plandex/shared"
)
-const apiHost = "http://localhost:8088"
+var apiHost string
// Check that API implements the types.APIHandler interface
var Api types.APIHandler = (*API)(nil)
type API struct{}
-func (api *API) Propose(prompt, parentProposalId string, onStream types.OnStreamPlan) (*shared.PromptRequest, error) {
- serverUrl := apiHost + "/proposal"
-
- // Channels to receive data and errors
- contextChan := make(chan shared.ModelContext, 1) // Buffered channels to prevent deadlock
- contextErrChan := make(chan error, 1)
-
- conversationChan := make(chan []shared.ConversationMessage, 1)
- conversationErrChan := make(chan error, 1)
-
- planChan := make(chan shared.CurrentPlanFiles, 1)
- planErrChan := make(chan error, 1)
-
- // Goroutine for loading context
- go func() {
- modelContext, err := GetAllContext(false)
- if err != nil {
- fmt.Println("Error loading context:" + err.Error())
- contextErrChan <- err
- return
- }
- contextChan <- modelContext
- }()
-
- // Goroutine for loading conversation
- go func() {
- conversation, err := loadConversation()
- if err != nil {
- fmt.Println("Error loading conversation")
- conversationErrChan <- err
- return
- }
- conversationChan <- conversation
- }()
-
- // Goroutine for loading plan
- go func() {
- plan, err := getCurrentPlanFiles()
- if err != nil {
- fmt.Println("Error loading plan")
- planErrChan <- err
- return
- }
- planChan <- plan
- }()
-
- var modelContext shared.ModelContext
- var currentPlan shared.CurrentPlanFiles
- var conversation []shared.ConversationMessage
- var err error
-
- // Using select to receive from either data or error channel for context
- select {
- case modelContext = <-contextChan:
- case err = <-contextErrChan:
- return nil, err
- }
-
- // Using select to receive from either data or error channel for conversation
- select {
- case conversation = <-conversationChan:
- case err = <-conversationErrChan:
- return nil, err
- }
-
- // Using select to receive from either data or error channel for plan
- select {
- case plan := <-planChan:
- currentPlan = plan
- case err = <-planErrChan:
- return nil, err
- }
-
- payload := shared.PromptRequest{
- Prompt: prompt,
- ModelContext: modelContext,
- Conversation: conversation,
- CurrentPlan: currentPlan,
- ParentProposalId: parentProposalId,
- }
-
- jsonData, err := json.Marshal(payload)
- if err != nil {
- return nil, err
+func init() {
+ var port = os.Getenv("PORT")
+ if port == "" {
+ port = "8088"
}
-
- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
- if err != nil {
- return nil, err
- }
-
- // Check the HTTP status code
- if resp.StatusCode >= 400 {
- // Read the error message from the body
- errorBody, _ := io.ReadAll(resp.Body)
- return &payload, fmt.Errorf("server returned an error %d: %s", resp.StatusCode, string(errorBody))
- }
-
- reader := bufio.NewReader(resp.Body)
- streamState := shared.NewPlanStreamState()
-
- go func() {
- for {
- s, err := readUntilSeparator(reader, shared.STREAM_MESSAGE_SEPARATOR)
- if err != nil {
- fmt.Println("Error reading line:", err)
- streamState.Event(context.Background(), shared.EVENT_ERROR)
- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
- resp.Body.Close()
- return
- }
-
- if s == shared.STREAM_FINISHED || s == shared.STREAM_ABORTED {
- var evt string
- if s == shared.STREAM_FINISHED {
- evt = shared.EVENT_FINISH
- } else {
- evt = shared.STATE_ABORTED
- }
- err := streamState.Event(context.Background(), evt)
- if err != nil {
- fmt.Printf("Error triggering state change %s: %s\n", evt, err)
- }
- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
- resp.Body.Close()
- return
- }
-
- if s == shared.STREAM_DESCRIPTION_PHASE {
- err = streamState.Event(context.Background(), shared.EVENT_DESCRIBE)
- } else if s == shared.STREAM_BUILD_PHASE {
- err = streamState.Event(context.Background(), shared.EVENT_BUILD)
- }
-
- if err != nil {
- fmt.Println("Error setting state:", err)
- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
- resp.Body.Close()
- return
- }
-
- onStream(types.OnStreamPlanParams{Content: s, State: streamState, Err: nil})
- }
- }()
-
- return &payload, nil
-}
-
-func (api *API) Abort(proposalId string) error {
- serverUrl := apiHost + "/abort"
-
- req, err := http.NewRequest("GET", serverUrl, nil)
- if err != nil {
- return fmt.Errorf("failed to create request: %s\n", err)
- }
-
- q := req.URL.Query()
- q.Add("proposalId", proposalId)
- req.URL.RawQuery = q.Encode()
-
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- return fmt.Errorf("failed to send request to server: %s\n", err)
- }
-
- defer resp.Body.Close()
-
- // Check the response status code
- if resp.StatusCode != http.StatusOK {
- // Read the error message from the body
- errorBody, _ := io.ReadAll(resp.Body)
- return fmt.Errorf("server returned an error %d: %s", resp.StatusCode,
- string(errorBody))
- }
-
- return nil
-}
-
-func (api *API) ShortSummary(text string) (*shared.ShortSummaryResponse, error) {
- serverUrl := apiHost + "/summarize"
-
- payload := shared.ShortSummaryRequest{
- Text: text,
- }
- jsonData, err := json.Marshal(payload)
- if err != nil {
- return nil, err
- }
-
- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
-
- // fmt.Println("ShortSummary response body:")
- // fmt.Println(string(body))
-
- var summarized shared.ShortSummaryResponse
- err = json.Unmarshal(body, &summarized)
- if err != nil {
- return nil, err
- }
-
- return &summarized, nil
-}
-
-func (api *API) FileName(text string) (*shared.FileNameResponse, error) {
- serverUrl := apiHost + "/filename"
-
- payload := shared.FileNameRequest{
- Text: text,
- }
- jsonData, err := json.Marshal(payload)
- if err != nil {
- return nil, err
- }
-
- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
-
- // fmt.Println("FileName response body:")
- // fmt.Println(string(body))
-
- var fileName shared.FileNameResponse
- err = json.Unmarshal(body, &fileName)
- if err != nil {
- return nil, err
- }
-
- return &fileName, nil
+ apiHost = "http://localhost:" + port
}
diff --git a/cli/lib/api_abort.go b/cli/lib/api_abort.go
new file mode 100644
index 00000000..fd6b1d42
--- /dev/null
+++ b/cli/lib/api_abort.go
@@ -0,0 +1,37 @@
+package lib
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+)
+
+func (api *API) Abort(proposalId string) error {
+ serverUrl := apiHost + "/abort"
+
+ req, err := http.NewRequest("GET", serverUrl, nil)
+ if err != nil {
+ return fmt.Errorf("failed to create request: %s\n", err)
+ }
+
+ q := req.URL.Query()
+ q.Add("proposalId", proposalId)
+ req.URL.RawQuery = q.Encode()
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return fmt.Errorf("failed to send request to server: %s\n", err)
+ }
+
+ defer resp.Body.Close()
+
+ // Check the response status code
+ if resp.StatusCode != http.StatusOK {
+ // Read the error message from the body
+ errorBody, _ := io.ReadAll(resp.Body)
+ return fmt.Errorf("server returned an error %d: %s", resp.StatusCode,
+ string(errorBody))
+ }
+
+ return nil
+}
diff --git a/cli/lib/api_convo.go b/cli/lib/api_convo.go
new file mode 100644
index 00000000..477c6c89
--- /dev/null
+++ b/cli/lib/api_convo.go
@@ -0,0 +1,57 @@
+package lib
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func (api *API) ConvoSummary(rootId, latestTimestamp string) (*shared.ConversationSummary, error) {
+ serverUrl := apiHost + "/convo-summary/" + rootId
+
+ req, err := http.NewRequest("GET", serverUrl, nil)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %s", err)
+ }
+
+ if latestTimestamp != "" {
+ q := req.URL.Query()
+ q.Add("latestTimestamp", latestTimestamp)
+ req.URL.RawQuery = q.Encode()
+ }
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("failed to send request to server: %s", err)
+ }
+
+ defer resp.Body.Close()
+
+ if resp.StatusCode == http.StatusNoContent {
+ return nil, nil
+ }
+
+ // Check the response status code
+ if resp.StatusCode != http.StatusOK {
+ // Read the error message from the body
+ errorBody, _ := io.ReadAll(resp.Body)
+ return nil, fmt.Errorf("server returned an error %d: %s", resp.StatusCode,
+ string(errorBody))
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response body: %s", err)
+ }
+
+ var convoSummary shared.ConversationSummary
+ err = json.Unmarshal(body, &convoSummary)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse response body: %s", err)
+ }
+
+ return &convoSummary, nil
+}
diff --git a/cli/lib/api_meta.go b/cli/lib/api_meta.go
new file mode 100644
index 00000000..f39ea10b
--- /dev/null
+++ b/cli/lib/api_meta.go
@@ -0,0 +1,78 @@
+package lib
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "net/http"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func (api *API) FileName(text string) (*shared.FileNameResponse, error) {
+ serverUrl := apiHost + "/filename"
+
+ payload := shared.FileNameRequest{
+ Text: text,
+ }
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return nil, err
+ }
+
+ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ // fmt.Println("FileName response body:")
+ // fmt.Println(string(body))
+
+ var fileName shared.FileNameResponse
+ err = json.Unmarshal(body, &fileName)
+ if err != nil {
+ return nil, err
+ }
+
+ return &fileName, nil
+}
+
+func (api *API) ShortSummary(text string) (*shared.ShortSummaryResponse, error) {
+ serverUrl := apiHost + "/summarize"
+
+ payload := shared.ShortSummaryRequest{
+ Text: text,
+ }
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return nil, err
+ }
+
+ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ // fmt.Println("ShortSummary response body:")
+ // fmt.Println(string(body))
+
+ var summarized shared.ShortSummaryResponse
+ err = json.Unmarshal(body, &summarized)
+ if err != nil {
+ return nil, err
+ }
+
+ return &summarized, nil
+}
diff --git a/cli/lib/api_propose.go b/cli/lib/api_propose.go
new file mode 100644
index 00000000..7ae9218d
--- /dev/null
+++ b/cli/lib/api_propose.go
@@ -0,0 +1,186 @@
+package lib
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "plandex/types"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func (api *API) Propose(prompt, parentProposalId, rootId string, onStream types.OnStreamPlan) (*shared.PromptRequest, error) {
+ serverUrl := apiHost + "/proposal"
+
+ // Channels to receive data and errors
+ contextChan := make(chan shared.ModelContext, 1) // Buffered channels to prevent deadlock
+ contextErrChan := make(chan error, 1)
+
+ conversationChan := make(chan []shared.ConversationMessage, 1)
+ conversationErrChan := make(chan error, 1)
+
+ planChan := make(chan shared.CurrentPlanFiles, 1)
+ planErrChan := make(chan error, 1)
+
+ summaryChan := make(chan []shared.ConversationSummary, 1)
+ summaryErrChan := make(chan error, 1)
+
+ // Goroutine for loading context
+ go func() {
+ modelContext, err := GetAllContext(false)
+ if err != nil {
+ fmt.Println("Error loading context:" + err.Error())
+ contextErrChan <- err
+ return
+ }
+ contextChan <- modelContext
+ }()
+
+ // Goroutine for loading conversation
+ go func() {
+ conversation, err := LoadConversation()
+ if err != nil {
+ fmt.Println("Error loading conversation")
+ conversationErrChan <- err
+ return
+ }
+ conversationChan <- conversation
+ }()
+
+ // Goroutine for loading plan
+ go func() {
+ plan, err := getCurrentPlanFiles()
+ if err != nil {
+ fmt.Println("Error loading plan")
+ planErrChan <- err
+ return
+ }
+ planChan <- plan
+ }()
+
+ // Goroutine for loading summaries
+ go func() {
+ summaries, err := LoadSummaries()
+ if err != nil {
+ fmt.Println("Error loading summaries")
+ summaryErrChan <- err
+ return
+ }
+ summaryChan <- summaries
+ }()
+
+ var modelContext shared.ModelContext
+ var currentPlan shared.CurrentPlanFiles
+ var conversation []shared.ConversationMessage
+ var summaries []shared.ConversationSummary
+ var err error
+
+ // Using select to receive from either data or error channel for context
+ select {
+ case modelContext = <-contextChan:
+ case err = <-contextErrChan:
+ return nil, err
+ }
+
+ // Using select to receive from either data or error channel for conversation
+ select {
+ case conversation = <-conversationChan:
+ case err = <-conversationErrChan:
+ return nil, err
+ }
+
+ // Using select to receive from either data or error channel for plan
+ select {
+ case plan := <-planChan:
+ currentPlan = plan
+ case err = <-planErrChan:
+ return nil, err
+ }
+
+ // Using select to receive from either data or error channel for summaries
+ select {
+ case summaries = <-summaryChan:
+ case err = <-summaryErrChan:
+ return nil, err
+ }
+
+ payload := shared.PromptRequest{
+ Prompt: prompt,
+ ModelContext: modelContext,
+ Conversation: conversation,
+ ConversationSummaries: summaries,
+ CurrentPlan: currentPlan,
+ ParentProposalId: parentProposalId,
+ RootProposalId: rootId,
+ }
+
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return nil, err
+ }
+
+ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+
+ // Check the HTTP status code
+ if resp.StatusCode >= 400 {
+ // Read the error message from the body
+ errorBody, _ := io.ReadAll(resp.Body)
+ return &payload, fmt.Errorf("server returned an error %d: %s", resp.StatusCode, string(errorBody))
+ }
+
+ reader := bufio.NewReader(resp.Body)
+ streamState := shared.NewPlanStreamState()
+
+ go func() {
+ for {
+ s, err := readUntilSeparator(reader, shared.STREAM_MESSAGE_SEPARATOR)
+ if err != nil {
+ fmt.Println("Error reading line:", err)
+ streamState.Event(context.Background(), shared.EVENT_ERROR)
+ onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
+ resp.Body.Close()
+ return
+ }
+
+ if s == shared.STREAM_FINISHED || s == shared.STREAM_ABORTED {
+ var evt string
+ if s == shared.STREAM_FINISHED {
+ evt = shared.EVENT_FINISH
+ } else {
+ evt = shared.STATE_ABORTED
+ }
+ err := streamState.Event(context.Background(), evt)
+ if err != nil {
+ fmt.Printf("Error triggering state change %s: %s\n", evt, err)
+ }
+ onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
+ resp.Body.Close()
+ return
+ }
+
+ if s == shared.STREAM_DESCRIPTION_PHASE {
+ err = streamState.Event(context.Background(), shared.EVENT_DESCRIBE)
+ } else if s == shared.STREAM_BUILD_PHASE {
+ err = streamState.Event(context.Background(), shared.EVENT_BUILD)
+ }
+
+ if err != nil {
+ fmt.Println("Error setting state:", err)
+ onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})
+ resp.Body.Close()
+ return
+ }
+
+ onStream(types.OnStreamPlanParams{Content: s, State: streamState, Err: nil})
+ }
+ }()
+
+ return &payload, nil
+}
diff --git a/cli/lib/context_load.go b/cli/lib/context_load.go
index 381ff2a3..a2023109 100644
--- a/cli/lib/context_load.go
+++ b/cli/lib/context_load.go
@@ -123,7 +123,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
Body: body,
Sha: sha,
NumTokens: numTokens,
- UpdatedAt: StringTs(),
+ UpdatedAt: shared.StringTs(),
}
contextPartsMutex.Lock()
@@ -200,7 +200,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
Body: body,
Sha: sha,
NumTokens: numTokens,
- UpdatedAt: StringTs(),
+ UpdatedAt: shared.StringTs(),
}
contextPartsMutex.Lock()
@@ -279,7 +279,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
Body: body,
Sha: sha,
NumTokens: numTokens,
- UpdatedAt: StringTs(),
+ UpdatedAt: shared.StringTs(),
}
contextPartsMutex.Lock()
@@ -342,7 +342,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
FilePath: path,
Sha: sha,
NumTokens: numTokens,
- UpdatedAt: StringTs(),
+ UpdatedAt: shared.StringTs(),
}
contextPartsMutex.Lock()
@@ -402,18 +402,13 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
hash := sha256.Sum256([]byte(body))
sha := hex.EncodeToString(hash[:])
- // fileNameResp, err := Api.FileName(body)
- // if err != nil {
- // log.Fatalf("Failed to summarize content from URL %s: %v", url, err)
- // }
-
contextPart := shared.ModelContextPart{
Name: SanitizeAndClipURL(url, 70),
Url: url,
Body: body,
Sha: sha,
NumTokens: numTokens,
- UpdatedAt: StringTs(),
+ UpdatedAt: shared.StringTs(),
}
contextPartsMutex.Lock()
diff --git a/cli/lib/conversation.go b/cli/lib/conversation.go
index 6cfb9be8..aa48c783 100644
--- a/cli/lib/conversation.go
+++ b/cli/lib/conversation.go
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"plandex/types"
+ "sort"
"strconv"
"strings"
@@ -13,7 +14,7 @@ import (
openai "github.com/sashabaranov/go-openai"
)
-func loadConversation() ([]shared.ConversationMessage, error) {
+func LoadConversation() ([]shared.ConversationMessage, error) {
var messages []shared.ConversationMessage
files, err := os.ReadDir(ConversationSubdir)
@@ -23,115 +24,222 @@ func loadConversation() ([]shared.ConversationMessage, error) {
for _, file := range files {
// Ensure we are only reading files, not directories
- if !file.IsDir() {
- filePath := ConversationSubdir + "/" + file.Name()
- file, err := os.Open(filePath)
- if err != nil {
- return nil, err
- }
+ if file.IsDir() {
+ continue
+ }
+ filePath := ConversationSubdir + "/" + file.Name()
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, err
+ }
- scanner := bufio.NewScanner(file)
- var currentRole string
- var currentTokens int
- var contentBuffer []string
-
- for scanner.Scan() {
- line := scanner.Text()
-
- // Check if the line starts with user or response indicator
- if strings.HasPrefix(line, "@@@!>user|") {
- if currentRole != "" {
- // Save the previous message before starting a new one
- messages = append(messages, shared.ConversationMessage{
- Message: openai.ChatCompletionMessage{
- Role: currentRole,
- Content: strings.Join(contentBuffer, "\n"),
- },
- Tokens: currentTokens,
- })
- contentBuffer = []string{}
- }
- currentRole = openai.ChatMessageRoleUser
- // Parse the number of tokens from the line (tokens only)
- currentTokensStr := strings.Split(line, "|")[2]
- currentTokens, err = strconv.Atoi(currentTokensStr)
- if err != nil {
- return nil, err
- }
-
- continue
- } else if strings.HasPrefix(line, "@@@!>response|") {
- if currentRole != "" {
- // Save the previous message before starting a new one
- messages = append(messages, shared.ConversationMessage{
- Message: openai.ChatCompletionMessage{
- Role: currentRole,
- Content: strings.Join(contentBuffer, "\n"),
- }})
- contentBuffer = []string{}
- }
- currentRole = openai.ChatMessageRoleAssistant
- continue
+ scanner := bufio.NewScanner(file)
+ var currentRole string
+ var currentTokens int
+ var currentTimestamp string
+ var contentBuffer []string
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ // Check if the line starts with user or response indicator
+ if strings.HasPrefix(line, "@@@!>user|") {
+ if currentRole != "" {
+ // Save the previous message before starting a new one
+ messages = append(messages, shared.ConversationMessage{
+ Message: openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ },
+ Tokens: currentTokens,
+ Timestamp: currentTimestamp,
+ })
+ contentBuffer = []string{}
+ }
+ currentRole = openai.ChatMessageRoleUser
+ // Parse the number of tokens from the line (tokens only)
+ split := strings.Split(line, "|")
+ currentTimestamp = split[1]
+ currentTokensStr := split[2]
+ currentTokens, err = strconv.Atoi(currentTokensStr)
+ if err != nil {
+ return nil, err
}
- // Add content to the buffer
- contentBuffer = append(contentBuffer, line)
+ continue
+ } else if strings.HasPrefix(line, "@@@!>response|") {
+ if currentRole != "" {
+ // Save the previous message before starting a new one
+ messages = append(messages, shared.ConversationMessage{
+ Message: openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ },
+ Tokens: currentTokens,
+ Timestamp: currentTimestamp,
+ })
+ contentBuffer = []string{}
+ }
+ currentRole = openai.ChatMessageRoleAssistant
+ split := strings.Split(line, "|")
+ currentTimestamp = split[1]
+ currentTokensStr := split[2]
+ currentTokens, err = strconv.Atoi(currentTokensStr)
+ if err != nil {
+ return nil, err
+ }
+ continue
}
- // Add the last message in the file
- if currentRole != "" && len(contentBuffer) > 0 {
- messages = append(messages, shared.ConversationMessage{
- Message: openai.ChatCompletionMessage{
- Role: currentRole,
- Content: strings.Join(contentBuffer, "\n"),
- },
- Tokens: currentTokens,
- })
- }
+ // Add content to the buffer
+ contentBuffer = append(contentBuffer, line)
+ }
- file.Close()
+ // Add the last message in the file
+ if currentRole != "" && len(contentBuffer) > 0 {
+ messages = append(messages, shared.ConversationMessage{
+ Message: openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ },
+ Tokens: currentTokens,
+ Timestamp: currentTimestamp,
+ })
}
+
+ file.Close()
}
return messages, nil
}
+func LoadSummaries() ([]shared.ConversationSummary, error) {
+ // check if summaries subdirectory exists
+ summariesPath := filepath.Join(ConversationSubdir, "summaries")
+ exists := false
+ _, err := os.Stat(summariesPath)
+ if err == nil {
+ exists = true
+ } else if !os.IsNotExist(err) {
+ return nil, err
+ }
+
+ var summaries []shared.ConversationSummary
+ if exists {
+
+ files, err := os.ReadDir(summariesPath)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read summaries directory: %s", err)
+ }
+
+ for _, file := range files {
+ // Ensure we are only reading files, not directories
+ if file.IsDir() {
+ continue
+ }
+ filePath := summariesPath + "/" + file.Name()
+
+ bytes, err := os.ReadFile(filePath)
+
+ if err != nil {
+ return nil, fmt.Errorf("failed to read summary file: %s", err)
+ }
+
+ content := string(bytes)
+
+ header := strings.Split(content, "\n")[0]
+ summary := strings.Join(strings.Split(content, "\n")[2:], "\n")
+
+ headerSplit := strings.Split(header, "|")
+ timestamp := headerSplit[1]
+ tokensStr := headerSplit[2]
+ tokens, err := strconv.Atoi(tokensStr)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse tokens: %s", err)
+ }
+
+ summaries = append(summaries, shared.ConversationSummary{
+ Summary: summary,
+ LastMessageTimestamp: timestamp,
+ Tokens: tokens,
+ })
+ }
+ }
+
+ // sort by timestamp
+ sort.Slice(summaries, func(i, j int) bool {
+ return summaries[i].LastMessageTimestamp < summaries[j].LastMessageTimestamp
+ })
+
+ return summaries, nil
+}
+
func appendConversation(params types.AppendConversationParams) error {
// Create or append to conversation file
- responseTimestamp := StringTs()
conversationFilePath := filepath.Join(ConversationSubdir, fmt.Sprintf("%s.md", params.Timestamp))
userHeader := fmt.Sprintf("@@@!>user|%s|%d\n\n", params.Timestamp, params.PromptTokens)
- responseHeader := fmt.Sprintf("@@@!>response|%s|%d\n\n", responseTimestamp, params.ReplyTokens)
+ responseHeader := fmt.Sprintf("@@@!>response|%s|%d\n\n", params.ResponseTimestamp, params.ReplyTokens)
conversationFileContents := fmt.Sprintf("%s%s\n\n%s%s", userHeader, params.Prompt, responseHeader, params.Reply)
conversationFile, err := os.OpenFile(conversationFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
- return fmt.Errorf("failed to write conversation file: %s\n", err)
+ return fmt.Errorf("failed to write conversation file: %s", err)
}
defer conversationFile.Close()
_, err = conversationFile.WriteString(conversationFileContents)
if err != nil {
- return fmt.Errorf("failed to write conversation file: %s\n", err)
+ return fmt.Errorf("failed to write conversation file: %s", err)
}
return nil
}
-func setSummary(params types.ConversationSummaryParams) error {
- // Create or append to summary file
- summaryFilePath := filepath.Join(ConversationSubdir, fmt.Sprintf("%s.summary.md", params.MessageTimestamp))
- summaryFileContents := fmt.Sprintf("@@@!>response-summary|%s|%d\n\n%s", params.CurrentTimestamp, params.SummaryTokens, params.Summary)
- summaryFile, err := os.OpenFile(summaryFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+func saveLatestConvoSummary(rootId string) error {
+ summariesPath := filepath.Join(ConversationSubdir, "summaries")
+ exists := false
+ _, err := os.Stat(summariesPath)
+ if err == nil {
+ exists = true
+ } else if !os.IsNotExist(err) {
+ return err
+ }
+
+ if !exists {
+ err = os.Mkdir(summariesPath, 0755)
+ if err != nil {
+ return err
+ }
+ }
+
+ // get latest summary from directory
+ files, err := os.ReadDir(summariesPath)
if err != nil {
- return fmt.Errorf("failed to write summary file: %s\n", err)
+ return fmt.Errorf("failed to read summaries directory: %s", err)
+ }
+
+ var latestTimestamp string
+ if len(files) > 0 {
+ sort.Slice(files, func(i, j int) bool {
+ return files[i].Name() < files[j].Name()
+ })
+ latestFile := files[len(files)-1]
+ latestTimestamp = strings.Split(latestFile.Name(), ".")[0]
}
- defer summaryFile.Close()
- _, err = summaryFile.WriteString(summaryFileContents)
+ summary, err := Api.ConvoSummary(rootId, latestTimestamp)
if err != nil {
- return fmt.Errorf("failed to write summary file: %s\n", err)
+ return fmt.Errorf("failed to get convo summary: %s", err)
}
- return nil
+ if summary == nil {
+ return nil
+ }
+
+ summaryFilePath := filepath.Join(summariesPath, fmt.Sprintf("%s.md", summary.LastMessageTimestamp))
+ summaryFileContents := fmt.Sprintf("@@@!>summary|%s|%d\n\n%s", summary.LastMessageTimestamp, summary.Tokens, summary.Summary)
+
+ err = os.WriteFile(summaryFilePath, []byte(summaryFileContents), 0644)
+
+ return err
}
diff --git a/cli/lib/proposal.go b/cli/lib/proposal.go
index 22e7ba18..85f5fa43 100644
--- a/cli/lib/proposal.go
+++ b/cli/lib/proposal.go
@@ -38,7 +38,7 @@ func Propose(prompt string) error {
RenameCurrentDraftPlan(GetFileNameWithoutExt(fileNameResp.FileName))
}
- timestamp := StringTs()
+ timestamp := shared.StringTs()
reply := ""
done := make(chan struct{})
@@ -63,14 +63,9 @@ func Propose(prompt string) error {
numStreamedTokensByPath := make(map[string]int)
replyTokenCounter := shared.NewReplyInfo()
- // var tokensAddedByFile map[string]int
-
- // currentPlanTokensByFilePath, err := loadCurrentPlanTokensByFilePath()
- // if err != nil {
- // return fmt.Errorf("failed to load token counts: %s\n", err)
- // }
var parentProposalId string
+ var rootId string
var planState types.PlanState
// get plan state from [CurrentPlanRootDir]/plan.json
planStatePath := filepath.Join(CurrentPlanRootDir, "plan.json")
@@ -79,13 +74,21 @@ func Propose(prompt string) error {
} else {
fileBytes, err := os.ReadFile(planStatePath)
if err != nil {
- return fmt.Errorf("failed to open plan state file: %s\n", err)
+ return fmt.Errorf("failed to open plan state file: %s", err)
}
err = json.Unmarshal(fileBytes, &planState)
if err != nil {
- return fmt.Errorf("failed to parse plan state json: %s\n", err)
+ return fmt.Errorf("failed to parse plan state json: %s", err)
}
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)
+ }
}
var promptNumTokens int
@@ -98,7 +101,7 @@ func Propose(prompt string) error {
MoveCursorToTopLeft()
mdFull, _ := GetMarkdown(reply)
fmt.Println(mdFull)
- fmt.Printf(displayHotkeys())
+ fmt.Print(displayHotkeys())
termState = mdFull
}
@@ -124,20 +127,23 @@ func Propose(prompt string) error {
s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)
s.Prefix = " "
s.Start()
+ endedReply = true
+ }
+
+ appendConvo := func() {
var totalTokens int
_, _, _, totalTokens = replyTokenCounter.FinishAndRead()
err := appendConversation(types.AppendConversationParams{
- Timestamp: timestamp,
- Prompt: prompt,
- PromptTokens: promptNumTokens,
- Reply: reply,
- ReplyTokens: totalTokens,
+ Timestamp: timestamp,
+ ResponseTimestamp: desc.ResponseTimestamp,
+ Prompt: prompt,
+ PromptTokens: promptNumTokens,
+ Reply: reply,
+ ReplyTokens: totalTokens,
})
if err != nil {
- fmt.Printf("failed to append conversation: %s\n", err)
+ fmt.Printf("failed to append conversation: %s", err)
}
- endedReply = true
-
}
contextByFilePath := make(map[string]shared.ModelContextPart)
@@ -185,20 +191,24 @@ func Propose(prompt string) error {
return
} else {
proposalId = content
+ if rootId == "" {
+ rootId = proposalId
+ }
// Save proposal id to [CurrentPlanRootDir]/plan.json
planState = types.PlanState{
ProposalId: proposalId,
+ RootId: rootId,
}
planStatePath := filepath.Join(CurrentPlanRootDir, "plan.json")
planStateBytes, err := json.Marshal(planState)
if err != nil {
- onError(fmt.Errorf("failed to marshal plan state: %s\n", err))
+ onError(fmt.Errorf("failed to marshal plan state: %s", err))
return
}
err = os.WriteFile(planStatePath, planStateBytes, 0644)
if err != nil {
- onError(fmt.Errorf("failed to write plan state: %s\n", err))
+ onError(fmt.Errorf("failed to write plan state: %s", err))
return
}
@@ -226,6 +236,7 @@ func Propose(prompt string) error {
case shared.STATE_FINISHED:
if !endedReply {
endReply()
+ appendConvo()
}
s.Stop()
streamFinished = true
@@ -247,6 +258,8 @@ func Propose(prompt string) error {
return
}
+ appendConvo()
+
if desc.MadePlan && (len(desc.Files) > 0) {
s.Stop()
fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" πŸ— ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))
@@ -325,10 +338,10 @@ func Propose(prompt string) error {
}
- apiReq, err = Api.Propose(prompt, parentProposalId, handleStream)
+ apiReq, err = Api.Propose(prompt, parentProposalId, rootId, handleStream)
if err != nil {
BackToMain()
- return fmt.Errorf("failed to send prompt to server: %s\n", err)
+ return fmt.Errorf("failed to send prompt to server: %s", err)
}
for _, part := range apiReq.ModelContext {
contextByFilePath[part.FilePath] = part
@@ -371,20 +384,22 @@ Loop:
}
}
- fmt.Println()
+ if desc != nil {
- if desc.MadePlan && len(desc.Files) > 0 {
- for _, cmd := range []string{"apply", "diffs", "preview"} {
- ClearCurrentLine()
- PrintCmds(" ", cmd)
+ if desc.MadePlan && len(desc.Files) > 0 {
+ fmt.Println()
+ for _, cmd := range []string{"apply", "diffs", "preview"} {
+ ClearCurrentLine()
+ PrintCmds(" ", cmd)
+ }
}
- }
- ClearCurrentLine()
- PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")
+ ClearCurrentLine()
+ PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")
- ClearCurrentLine()
- PrintCmds(" ", "next")
+ ClearCurrentLine()
+ PrintCmds(" ", "next")
+ }
return nil
}
diff --git a/cli/lib/term.go b/cli/lib/term.go
index b1d20a1e..f25df412 100644
--- a/cli/lib/term.go
+++ b/cli/lib/term.go
@@ -118,15 +118,18 @@ func getTerminalWidth() (int, error) {
return width, nil
}
-func displayHotkeys() string {
+func GetDivisionLine() string {
// Get the terminal width
terminalWidth, err := getTerminalWidth()
if err != nil {
fmt.Println("Error fetching terminal size:", err)
terminalWidth = 50 // default width if unable to fetch width
}
- // Creating the terminal width long division line
- divisionLine := strings.Repeat("─", terminalWidth)
+ return strings.Repeat("─", terminalWidth)
+}
+
+func displayHotkeys() string {
+ divisionLine := GetDivisionLine()
return divisionLine + "\n" +
" \x1b[1m(s)\x1b[0m" + `top
diff --git a/cli/lib/utils.go b/cli/lib/utils.go
index 37d345a2..2fede454 100644
--- a/cli/lib/utils.go
+++ b/cli/lib/utils.go
@@ -11,10 +11,6 @@ import (
"github.com/eiannone/keyboard"
)
-func StringTs() string {
- return time.Now().Format("2006-01-02T15:04:05.999Z")
-}
-
func EnsureMinDuration(start time.Time, minDuration time.Duration) {
elapsed := time.Since(start)
if elapsed < minDuration {
diff --git a/cli/types/types.go b/cli/types/types.go
index 3ef150d7..153fd549 100644
--- a/cli/types/types.go
+++ b/cli/types/types.go
@@ -25,29 +25,25 @@ type OnStreamPlan func(params OnStreamPlanParams)
// APIHandler is an interface that represents the public API functions
type APIHandler interface {
- Propose(prompt, parentProposalId string, onStream OnStreamPlan) (*shared.PromptRequest, error)
+ Propose(prompt, parentProposalId, rootId string, onStream OnStreamPlan) (*shared.PromptRequest, error)
ShortSummary(text string) (*shared.ShortSummaryResponse, error)
Abort(proposalId string) error
FileName(text string) (*shared.FileNameResponse, error)
+ ConvoSummary(rootId, latestTimestamp string) (*shared.ConversationSummary, error)
}
type AppendConversationParams struct {
- Timestamp string
- Prompt string
- Reply string
- PromptTokens int
- ReplyTokens int
-}
-
-type ConversationSummaryParams struct {
- CurrentTimestamp string
- MessageTimestamp string
- Summary string
- SummaryTokens int
+ Timestamp string
+ ResponseTimestamp string
+ Prompt string
+ Reply string
+ PromptTokens int
+ ReplyTokens int
}
type PlanState struct {
ProposalId string
+ RootId string
}
type PlanSettings struct {
diff --git a/server/handlers/convo.go b/server/handlers/convo.go
new file mode 100644
index 00000000..f1852940
--- /dev/null
+++ b/server/handlers/convo.go
@@ -0,0 +1,50 @@
+package handlers
+
+import (
+ "encoding/json"
+ "log"
+ "net/http"
+ "plandex-server/model/proposal"
+
+ "github.com/gorilla/mux"
+)
+
+func ConvoSummaryHandler(w http.ResponseWriter, r *http.Request) {
+ log.Println("Received a request for ConvoSummaryHandler")
+
+ // get the rootId from the request
+ vars := mux.Vars(r)
+ rootId := vars["rootId"]
+
+ if rootId == "" {
+ log.Println("Received empty rootId field")
+ http.Error(w, "rootId field is required", http.StatusBadRequest)
+ return
+ }
+
+ latestTimestamp := r.URL.Query().Get("latestTimestamp")
+
+ summary := proposal.GetConvoSummary(rootId)
+
+ if summary == nil {
+ log.Printf("No summary found for rootId %s\n", rootId)
+ w.WriteHeader(http.StatusNoContent)
+ return
+ }
+
+ if latestTimestamp != "" && latestTimestamp == summary.LastMessageTimestamp {
+ log.Printf("No new messages found for rootId %s\n", rootId)
+ w.WriteHeader(http.StatusNoContent)
+ return
+ }
+
+ jsonBytes, err := json.Marshal(summary)
+ if err != nil {
+ log.Printf("Error marshalling summary: %v\n", err)
+ http.Error(w, "Error marshalling summary: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ log.Println("Successfully processed ConvoSummaryHandler request")
+ w.Write(jsonBytes)
+}
diff --git a/server/handlers/summarize.go b/server/handlers/summarize.go
index 67a03a81..a1dc5a9f 100644
--- a/server/handlers/summarize.go
+++ b/server/handlers/summarize.go
@@ -36,7 +36,7 @@ func ShortSummaryHandler(w http.ResponseWriter, r *http.Request) {
return
}
- modelResp, _, err := model.ShortSummary(requestBody.Text)
+ modelResp, err := model.ShortSummary(requestBody.Text)
if err != nil {
log.Printf("Error summarizing text: %v\n", err)
http.Error(w, "Error summarizing text: "+err.Error(), http.StatusInternalServerError)
diff --git a/server/main.go b/server/main.go
index 1e08e708..f6e61754 100644
--- a/server/main.go
+++ b/server/main.go
@@ -26,6 +26,7 @@ func main() {
r.HandleFunc("/abort-proposal", handlers.AbortProposalHandler).Methods("DELETE")
r.HandleFunc("/short-summary", handlers.ShortSummaryHandler).Methods("POST")
r.HandleFunc("/filename", handlers.FileNameHandler).Methods("POST")
+ r.HandleFunc("/convo-summary/{rootId}", handlers.ConvoSummaryHandler).Methods("GET")
// Get port from the environment variable or default to 8088
port := os.Getenv("PORT")
diff --git a/server/model/proposal/confirm.go b/server/model/proposal/confirm.go
index 134e5653..e3a840c0 100644
--- a/server/model/proposal/confirm.go
+++ b/server/model/proposal/confirm.go
@@ -311,9 +311,9 @@ func confirmProposal(proposalId string, fileContents map[string]string, numToken
}
fmt.Println("Calling model for file: " + filePath)
- for _, msg := range fileMessages {
- fmt.Printf("%s: %s\n", msg.Role, msg.Content)
- }
+ // for _, msg := range fileMessages {
+ // fmt.Printf("%s: %s\n", msg.Role, msg.Content)
+ // }
functions := []openai.FunctionDefinition{
{
diff --git a/server/model/proposal/create.go b/server/model/proposal/create.go
index be3cedd1..d5511b3e 100644
--- a/server/model/proposal/create.go
+++ b/server/model/proposal/create.go
@@ -12,7 +12,6 @@ import (
"plandex-server/model/lib"
"plandex-server/types"
- "github.com/davecgh/go-spew/spew"
"github.com/google/uuid"
"github.com/plandex/plandex/shared"
"github.com/sashabaranov/go-openai"
@@ -72,6 +71,33 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
return nil
}
+ proposalUUID, err := uuid.NewRandom()
+ if err != nil {
+ fmt.Printf("Failed to generate proposal id: %v\n", err)
+ return err
+ }
+ proposalId := proposalUUID.String()
+
+ ctx, cancel := context.WithCancel(context.Background())
+
+ rootId := req.RootProposalId
+ if rootId == "" {
+ rootId = proposalId
+ }
+ proposal := types.Proposal{
+ Id: proposalId,
+ ParentId: req.ParentProposalId,
+ IsRoot: req.ParentProposalId == "",
+ RootId: rootId,
+ Request: &req,
+ Content: "",
+ ProposalStage: types.ProposalStage{
+ CancelFn: &cancel,
+ },
+ }
+
+ onStream(proposalId, nil)
+
contextText, contextTokens := lib.FormatModelContext(req.ModelContext)
systemMessageText := systemMessageHead + contextText
systemMessage := openai.ChatCompletionMessage{
@@ -84,7 +110,6 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
}
promptTokens := promptWrapperTokens + shared.GetNumTokens(req.Prompt)
-
totalTokens := systemHeadNumTokens + contextTokens + promptTokens
// print out breakdown of token usage
@@ -100,44 +125,66 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
return err
}
- if len(req.Conversation) > 0 {
- for _, convoMessage := range req.Conversation {
- messages = append(messages, convoMessage.Message)
+ conversationTokens := 0
+ tokensUpToTimestamp := make(map[string]int)
+ for _, convoMessage := range req.Conversation {
+ conversationTokens += convoMessage.Tokens
+ tokensUpToTimestamp[convoMessage.Timestamp] = conversationTokens
+ }
+
+ var summary *shared.ConversationSummary
+ if (totalTokens + conversationTokens) > shared.MaxTokens {
+ // 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 req.ConversationSummaries {
+ tokens, ok := tokensUpToTimestamp[s.LastMessageTimestamp]
+ if !ok {
+ err := fmt.Errorf("conversation summary timestamp not found in conversation")
+ fmt.Printf("Error: %v\n", err)
+ return err
+ }
+ updatedConversationTokens := (conversationTokens - tokens) + s.Tokens
+ if updatedConversationTokens <= shared.MaxTokens {
+ summary = &s
+ break
+ }
}
}
- messages = append(messages, openai.ChatCompletionMessage{
- Role: openai.ChatMessageRoleUser,
- Content: fmt.Sprintf(promptWrapperFormatStr, req.Prompt),
- })
+ if summary == nil {
+ if len(req.Conversation) > 0 {
+ for _, convoMessage := range req.Conversation {
+ messages = append(messages, convoMessage.Message)
+ }
+ }
+ } else {
- fmt.Println("\n\nMessages:")
- for _, message := range messages {
- fmt.Printf("%s: %s\n", message.Role, message.Content)
- }
+ if (totalTokens + summary.Tokens) > shared.MaxTokens {
+ err := fmt.Errorf("token limit still exceeded after summarizing conversation")
+ return err
+ }
- proposalUUID, err := uuid.NewRandom()
- if err != nil {
- fmt.Printf("Failed to generate proposal id: %v\n", err)
- return err
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleAssistant,
+ Content: fmt.Sprintf(`
+ Summary of the plan so far:
+
+ %s`, summary.Summary),
+ })
}
- proposalId := proposalUUID.String()
- onStream(proposalId, nil)
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleUser,
+ Content: fmt.Sprintf(promptWrapperFormatStr, req.Prompt),
+ })
- ctx, cancel := context.WithCancel(context.Background())
+ // fmt.Println("\n\nMessages:")
+ // for _, message := range messages {
+ // fmt.Printf("%s: %s\n", message.Role, message.Content)
+ // }
// store the proposal
- proposals.Set(proposalId, &types.Proposal{
- Id: proposalId,
- ParentId: req.ParentProposalId,
- IsRoot: req.ParentProposalId == "",
- Request: &req,
- Content: "",
- ProposalStage: types.ProposalStage{
- CancelFn: &cancel,
- },
- })
+ proposals.Set(proposalId, &proposal)
replyInfo := shared.NewReplyInfo()
@@ -150,14 +197,12 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)
if err != nil {
fmt.Printf("Error creating proposal GPT4 stream: %v\n", err)
-
- spew.Dump(err)
+ fmt.Println(err)
errStr := err.Error()
if strings.Contains(errStr, "status code: 400") &&
strings.Contains(errStr, "reduce the length of the messages") {
fmt.Println("Token limit exceeded")
-
}
proposals.Delete(proposalId)
@@ -220,13 +265,58 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
if choice.FinishReason != "" {
onStream(shared.STREAM_DESCRIPTION_PHASE, nil)
+ responseTs := shared.StringTs()
+
+ if len(req.Conversation) > 0 {
+ summaryCh := make(chan *shared.ConversationSummary)
+ errCh := make(chan error)
+
+ summaryProc := types.ConvoSummaryProc{
+ SummaryCh: summaryCh,
+ ErrCh: errCh,
+ }
+
+ convoSummaryProcs.Set(proposal.RootId, &summaryProc)
+
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleUser,
+ Content: proposal.Content,
+ })
+
+ go func() {
+ fmt.Println("Generating plan summary for rootId:", proposal.RootId)
+
+ defer func() {
+ close(summaryCh)
+ close(errCh)
+ fmt.Println("Closing summary channels for root", proposal.RootId)
+ }()
+ summary, err := model.PlanSummary(messages, responseTs)
+ if err != nil {
+ fmt.Printf("Error generating plan summary for root %s: %v\n", proposal.RootId, err)
+
+ convoSummaryProcs.Update(proposal.RootId, func(proc *types.ConvoSummaryProc) {
+ proc.Err = err
+ })
+ errCh <- err
+ return
+ }
+
+ fmt.Println("Generated plan summary for root", proposal.RootId, ":", summary.Summary)
+
+ convoSummaries.Set(proposal.RootId, summary)
+ summaryCh <- summary
+
+ }()
+ }
files, fileContents, numTokensByFile, _ := replyInfo.FinishAndRead()
if len(files) == 0 {
planDescription := &shared.PlanDescription{
- Files: []string{},
- MadePlan: false,
+ Files: []string{},
+ MadePlan: false,
+ ResponseTimestamp: responseTs,
}
bytes, err := json.Marshal(planDescription)
if err != nil {
@@ -254,6 +344,7 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
planDescription.MadePlan = true
planDescription.Files = files
+ planDescription.ResponseTimestamp = responseTs
bytes, err := json.Marshal(planDescription)
if err != nil {
diff --git a/server/model/proposal/state.go b/server/model/proposal/state.go
index 78a84bb6..d2fad38c 100644
--- a/server/model/proposal/state.go
+++ b/server/model/proposal/state.go
@@ -2,10 +2,17 @@ package proposal
import (
"plandex-server/types"
+
+ "github.com/plandex/plandex/shared"
)
var (
- proposals types.SafeMap[types.Proposal] = *types.NewSafeMap[types.Proposal]()
- plans types.SafeMap[types.Plan] = *types.NewSafeMap[types.Plan]()
- convoSummaries types.SafeMap[types.ConversationSummary] = *types.NewSafeMap[types.ConversationSummary]()
+ proposals types.SafeMap[*types.Proposal] = *types.NewSafeMap[*types.Proposal]()
+ plans types.SafeMap[*types.Plan] = *types.NewSafeMap[*types.Plan]()
+ convoSummaryProcs types.SafeMap[*types.ConvoSummaryProc] = *types.NewSafeMap[*types.ConvoSummaryProc]()
+ convoSummaries types.SafeMap[*shared.ConversationSummary] = *types.NewSafeMap[*shared.ConversationSummary]()
)
+
+func GetConvoSummary(rootId string) *shared.ConversationSummary {
+ return convoSummaries.Get(rootId)
+}
diff --git a/server/model/summarize.go b/server/model/summarize.go
index d7f50ecd..2a83f168 100644
--- a/server/model/summarize.go
+++ b/server/model/summarize.go
@@ -11,7 +11,7 @@ import (
"github.com/sashabaranov/go-openai/jsonschema"
)
-func ShortSummary(text string) ([]byte, int, error) {
+func ShortSummary(text string) ([]byte, error) {
resp, err := Client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
@@ -48,7 +48,7 @@ func ShortSummary(text string) ([]byte, int, error) {
)
if err != nil {
- return nil, 0, err
+ return nil, err
}
var byteRes []byte
@@ -65,31 +65,39 @@ func ShortSummary(text string) ([]byte, int, error) {
}
if len(byteRes) == 0 {
- return nil, 0, fmt.Errorf("no summarize function call found in response")
+ return nil, fmt.Errorf("no summarize function call found in response")
}
// validate the JSON response
var summarizeResp shared.ShortSummaryResponse
if err := json.Unmarshal(byteRes, &summarizeResp); err != nil {
- return nil, resp.Usage.CompletionTokens, err
+ return nil, err
}
- return byteRes, resp.Usage.CompletionTokens, nil
+ return byteRes, nil
}
const convoSummaryPrompt = `
Based on the conversation so far, make a summary of the current state of the plan.
-- It should begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages.
+- It should begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages. If the user has sent messages like 'continue to the next step of the plan' that don't contain any new information relevant to the plan, you should omit them.
- Next, if the plan includes a statement from the assistant to the effect of "I will break this large task into subtasks" and the plan has been broken down into subtasks, include those subtasks in the summary as a numbered list. Condense these as much as possible while still being clear and retaining the meaning of each subtask. At the end of the list, state which subtask is currently being worked on (unless they are all finished, in which case state that they are all finished).
-- Last, summarize the latest version of the plan and any changes you have suggested. If the some of the older changes have been overridden by newer changes, you should only include the newest changes and omit the older ones. If your responses include code blocks labelled with file paths, include the latest state of your modifications to each file's code and label them with file paths in the same format as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.
+- Last, summarize the latest version of the plan and any changes you have suggested. If the some of the older changes have been overridden by newer changes, you should only include the newest changes and omit the older ones.
+
+- If your responses include code blocks labelled with file paths, include the latest state of your modifications to each file's code and label them with file paths in the same format as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.
+
+- If code blocks can be converted to a textual summary without losing meaning or precision, summarize them. Otherwise, leave them as code.
+
+- If part of the plan has already been summarized, update the summary based on the latest messages while continuing the follow the above guidelines.
+
+- The summary should condense the conversation to save tokens, but still contain enough information about the state of the plan to be actionable and apply the suggested changes.
Output only the summary of the current state of the plan and nothing else.
`
-func PlanSummary(conversation []*shared.ConversationMessage) ([]byte, int, error) {
+func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimestamp string) (*shared.ConversationSummary, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
@@ -97,9 +105,7 @@ func PlanSummary(conversation []*shared.ConversationMessage) ([]byte, int, error
},
}
- for _, msg := range conversation {
- messages = append(messages, msg.Message)
- }
+ messages = append(messages, conversation...)
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
@@ -115,31 +121,21 @@ func PlanSummary(conversation []*shared.ConversationMessage) ([]byte, int, error
)
if err != nil {
- return nil, 0, err
- }
+ fmt.Println("PlanSummary err:", err)
- var byteRes []byte
- for _, choice := range resp.Choices {
- if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "summarize" {
- fnCall := choice.Message.FunctionCall
-
- if strings.HasSuffix(fnCall.Arguments, ",\n}") { // remove trailing comma
- fnCall.Arguments = strings.TrimSuffix(fnCall.Arguments, ",\n}") + "\n}"
- }
-
- byteRes = []byte(fnCall.Arguments)
- }
+ return nil, err
}
- if len(byteRes) == 0 {
- return nil, 0, fmt.Errorf("no summarize function call found in response")
+ if len(resp.Choices) == 0 {
+ return nil, fmt.Errorf("no response from GPT-4")
}
- // validate the JSON response
- var summarizeResp shared.ShortSummaryResponse
- if err := json.Unmarshal(byteRes, &summarizeResp); err != nil {
- return nil, resp.Usage.CompletionTokens, err
- }
+ content := resp.Choices[0].Message.Content
+
+ return &shared.ConversationSummary{
+ Summary: content,
+ Tokens: resp.Usage.CompletionTokens,
+ LastMessageTimestamp: lastMessageTimestamp,
+ }, nil
- return byteRes, resp.Usage.CompletionTokens, nil
}
diff --git a/server/types/conversation.go b/server/types/conversation.go
index 6299afbc..89787bcf 100644
--- a/server/types/conversation.go
+++ b/server/types/conversation.go
@@ -1,8 +1,11 @@
package types
-type ConversationSummary struct {
- ProposalId string
- Summary string
- SummaryTokens int
- UpdatedAt int
+import (
+ "github.com/plandex/plandex/shared"
+)
+
+type ConvoSummaryProc struct {
+ Err error
+ SummaryCh chan *shared.ConversationSummary
+ ErrCh chan error
}
diff --git a/server/types/safe_map.go b/server/types/safe_map.go
index 11d423ba..3ef0c44c 100644
--- a/server/types/safe_map.go
+++ b/server/types/safe_map.go
@@ -3,21 +3,21 @@ package types
import "sync"
type SafeMap[V any] struct {
- items map[string]*V
+ items map[string]V
mu sync.Mutex
}
func NewSafeMap[V any]() *SafeMap[V] {
- return &SafeMap[V]{items: make(map[string]*V)}
+ return &SafeMap[V]{items: make(map[string]V)}
}
-func (sm *SafeMap[V]) Get(key string) *V {
+func (sm *SafeMap[V]) Get(key string) V {
sm.mu.Lock()
defer sm.mu.Unlock()
return sm.items[key]
}
-func (sm *SafeMap[V]) Set(key string, value *V) {
+func (sm *SafeMap[V]) Set(key string, value V) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.items[key] = value
@@ -29,7 +29,7 @@ func (sm *SafeMap[V]) Delete(key string) {
delete(sm.items, key)
}
-func (sm *SafeMap[V]) Update(key string, fn func(*V)) {
+func (sm *SafeMap[V]) Update(key string, fn func(V)) {
sm.mu.Lock()
defer sm.mu.Unlock()
item := sm.items[key]
diff --git a/shared/types.go b/shared/types.go
index a7f40619..b118aa32 100644
--- a/shared/types.go
+++ b/shared/types.go
@@ -9,16 +9,20 @@ type CurrentPlanFiles struct {
}
type ConversationMessage struct {
- Message openai.ChatCompletionMessage `json:"message"`
- Tokens int `json:"tokens"`
+ Message openai.ChatCompletionMessage `json:"message"`
+ Tokens int `json:"tokens"`
+ Timestamp string `json:"timestamp"`
}
type PromptRequest struct {
- Prompt string `json:"prompt"`
- ModelContext ModelContext `json:"modelContext"`
- CurrentPlan CurrentPlanFiles `json:"currentPlan"`
- Conversation []ConversationMessage `json:"conversation"`
- ParentProposalId string `json:"parentProposalId"`
+ Timestamp string `json:"timestamp"`
+ Prompt string `json:"prompt"`
+ ModelContext ModelContext `json:"modelContext"`
+ CurrentPlan CurrentPlanFiles `json:"currentPlan"`
+ Conversation []ConversationMessage `json:"conversation"`
+ ConversationSummaries []ConversationSummary `json:"conversationSummaries"`
+ ParentProposalId string `json:"parentProposalId"`
+ RootProposalId string `json:"rootProposalId"`
}
type ShortSummaryRequest struct {
@@ -55,7 +59,14 @@ type PlanChunk struct {
}
type PlanDescription struct {
- MadePlan bool `json:"madePlan"`
- CommitMsg string `json:"commitMsg"`
- Files []string `json:"files"`
+ MadePlan bool `json:"madePlan"`
+ CommitMsg string `json:"commitMsg"`
+ Files []string `json:"files"`
+ ResponseTimestamp string `json:"responseTimestamp"`
+}
+
+type ConversationSummary struct {
+ Summary string
+ Tokens int
+ LastMessageTimestamp string
}
diff --git a/shared/utils.go b/shared/utils.go
new file mode 100644
index 00000000..e66fb4b4
--- /dev/null
+++ b/shared/utils.go
@@ -0,0 +1,7 @@
+package shared
+
+import "time"
+
+func StringTs() string {
+ return time.Now().Format("2006-01-02T15:04:05.999Z")
+}