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
converted server to multi-step proposal model with streaming responses. revise-proposal not yet implemented, but create, confirm, and abort are
Dane Schneider5613a1768fd5
diff --git a/server/go.mod b/server/go.modindex f93b0571..76f1d012 100644--- a/server/go.mod+++ b/server/go.mod@@ -4,6 +4,7 @@ go 1.21.0require (github.com/davecgh/go-spew v1.1.1+ github.com/google/uuid v1.3.1github.com/gorilla/mux v1.8.0github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000github.com/sashabaranov/go-openai v1.15.1diff --git a/server/go.sum b/server/go.sumindex 31c778d0..85290fa0 100644--- a/server/go.sum+++ b/server/go.sum@@ -1,5 +1,7 @@github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=+github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=+github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=github.com/sashabaranov/go-openai v1.15.1 h1:BAV5LCVEzvZ3rN/Lh5NRVs2z6AahPt/jn5s2/cEEG0M=diff --git a/server/handlers/prompt.go b/server/handlers/prompt.godeleted file mode 100644index 05ccf9f1..00000000--- a/server/handlers/prompt.go+++ /dev/null@@ -1,54 +0,0 @@-package handlers--import (- "encoding/json"- "fmt"- "io"- "log"- "net/http"- "plandex-server/model"-- "github.com/plandex/plandex/shared"-)--func PromptHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for PromptHandler")-- // Read the request body- body, err := io.ReadAll(r.Body)- if err != nil {- log.Printf("Error reading request body: %v\n", err)- http.Error(w, "Error reading request body", http.StatusInternalServerError)- return- }- defer r.Body.Close()-- // Unmarshal the request body to get the 'prompt'- var requestBody shared.PromptRequest- if err := json.Unmarshal(body, &requestBody); err != nil {- log.Printf("Error parsing request body: %v\n", err)- http.Error(w, "Error parsing request body", http.StatusBadRequest)- return- }-- if requestBody.Prompt == "" {- log.Println("Received empty prompt field")- http.Error(w, "prompt field is required", http.StatusBadRequest)- return- }-- modelRes, err := model.Prompt(requestBody)- if err != nil {- log.Printf("Error sending prompt to model: %v\n", err)- http.Error(w, "Error sending prompt to model: "+err.Error(), http.StatusInternalServerError)- return- }-- log.Println("Successfully processed prompt request")-- fmt.Println("Final response from model:")- fmt.Println(string(modelRes))-- // Return the response from OpenAI to the client- w.Write(modelRes)-}diff --git a/server/handlers/proposal.go b/server/handlers/proposal.gonew file mode 100644index 00000000..7c3fbdd7--- /dev/null+++ b/server/handlers/proposal.go@@ -0,0 +1,240 @@+package handlers++import (+ "context"+ "encoding/json"+ "fmt"+ "io"+ "log"+ "net/http"+ "plandex-server/model/proposal"++ "github.com/plandex/plandex/shared"+)++func ProposalHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received a request for ProposalHandler")++ // Read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ // Unmarshal the request body to get the 'prompt'+ var requestBody shared.PromptRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ if requestBody.Prompt == "" {+ log.Println("Received empty prompt field")+ http.Error(w, "prompt field is required", http.StatusBadRequest)+ return+ }++ w.Header().Set("Transfer-Encoding", "chunked")+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")++ var cancel *context.CancelFunc+ var isResponseClosed bool++ onStream := func(content string, finished bool, err error) {+ if isResponseClosed {+ return+ }++ if err != nil {+ isResponseClosed = true+ if cancel != nil {+ (*cancel)()+ }++ log.Printf("Error writing stream content to client: %v\n", err)++ w.WriteHeader(http.StatusInternalServerError)+ w.Write([]byte(err.Error()))++ return+ }++ if finished {+ isResponseClosed = true+ if cancel != nil {+ (*cancel)()+ }+ w.WriteHeader(http.StatusOK)+ w.Write([]byte(shared.STREAM_FINISHED))++ return+ }++ // stream content to client+ if content != "" {+ _, err = w.Write([]byte(content))++ if err != nil {+ isResponseClosed = true+ log.Printf("Error writing stream content to client: %v\n", err)+ if cancel != nil {+ (*cancel)()+ }+ return+ }+ }++ }++ cancel, err = proposal.CreateProposal(requestBody, onStream)+}++func ConfirmProposalHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received a request for ConfirmProposalHandler")++ proposalId := r.URL.Query().Get("proposalId")++ if proposalId == "" {+ log.Println("Received empty proposalId field")+ http.Error(w, "proposalId field is required", http.StatusBadRequest)+ return+ }++ w.Header().Set("Transfer-Encoding", "chunked")+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")++ var cancel *context.CancelFunc+ var err error+ var isResponseClosed bool++ onStream := func(planChunk *shared.PlanChunk, finished bool, err error) {+ if isResponseClosed {+ return+ }++ if err != nil {+ isResponseClosed = true+ if cancel != nil {+ (*cancel)()+ }+ log.Printf("Error writing plan chunk to client. chunk: , err: %v\n", planChunk, err)+ w.WriteHeader(http.StatusInternalServerError)+ w.Write([]byte(fmt.Sprintf("%v: %v", planChunk, err)))+ return+ }++ if finished {+ isResponseClosed = true+ if cancel != nil {+ (*cancel)()+ }+ w.WriteHeader(http.StatusOK)+ w.Write([]byte(shared.STREAM_FINISHED))++ return+ }++ // stream content to client+ if planChunk != nil {+ // convert plan chunk to json+ json, err := json.Marshal(planChunk)+ if err != nil {+ isResponseClosed = true+ if cancel != nil {+ (*cancel)()+ }+ log.Printf("Error marshalling plan chunk to json. chunk: , err: %v\n", planChunk, err)+ w.WriteHeader(http.StatusInternalServerError)+ w.Write([]byte(fmt.Sprintf("%v: %v", planChunk, err)))+ return+ }++ _, err = w.Write([]byte(json))++ if err != nil {+ isResponseClosed = true+ log.Printf("Error writing stream content to client: %v\n", err)+ if cancel != nil {+ (*cancel)()+ }+ return+ }+ }+ }++ cancel, err = proposal.ConfirmProposal(proposalId, onStream)++ if err != nil {+ log.Printf("Error confirming proposal: %v\n", err)+ http.Error(w, "Error confirming proposal: "+err.Error(), http.StatusInternalServerError)+ return+ }++}++func AbortProposalHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received a request for AbortProposalHandler")++ proposalId := r.URL.Query().Get("proposalId")++ if proposalId == "" {+ log.Println("Received empty proposalId field")+ http.Error(w, "proposalId field is required", http.StatusBadRequest)+ return+ }++ err := proposal.AbortProposal(proposalId)+ if err != nil {+ log.Printf("Error aborting proposal: %v\n", err)+ http.Error(w, "Error aborting proposal: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully aborted proposal")++ w.WriteHeader(http.StatusOK)+ w.Write([]byte("Ok"))+}++// func ReviseProposalHandler(w http.ResponseWriter, r *http.Request) {+// log.Println("Received a request for ReviseProposalHandler")++// // Read the request body+// body, err := io.ReadAll(r.Body)+// if err != nil {+// log.Printf("Error reading request body: %v\n", err)+// http.Error(w, "Error reading request body", http.StatusInternalServerError)+// return+// }+// defer r.Body.Close()++// // Unmarshal the request body to get the 'prompt'+// var requestBody shared.PromptRequest+// if err := json.Unmarshal(body, &requestBody); err != nil {+// log.Printf("Error parsing request body: %v\n", err)+// http.Error(w, "Error parsing request body", http.StatusBadRequest)+// return+// }++// if requestBody.Prompt == "" {+// log.Println("Received empty prompt field")+// http.Error(w, "prompt field is required", http.StatusBadRequest)+// return+// }++// modelRes, err := model.ReviseProposal(requestBody.ProposalId, requestBody.NewPrompt)+// if err != nil {+// log.Printf("Error revising proposal: %v\n", err)+// http.Error(w, "Error revising proposal: "+err.Error(), http.StatusInternalServerError)+// return+// }++// log.Println("Successfully revised proposal")++// // Return the response from OpenAI to the client+// w.Write(modelRes)+// }diff --git a/server/main.go b/server/main.goindex 6dd6cbea..897c0a83 100644--- a/server/main.go+++ b/server/main.go@@ -18,7 +18,11 @@ var client *openai.Clientfunc main() {r := mux.NewRouter()- r.HandleFunc("/prompt", handlers.PromptHandler).Methods("POST")+ r.HandleFunc("/proposal", handlers.ProposalHandler).Methods("POST")+ r.HandleFunc("/confirm-proposal", handlers.ConfirmProposalHandler).Methods("PATCH")+ r.HandleFunc("/abort-proposal", handlers.AbortProposalHandler).Methods("DELETE")+ // r.HandleFunc("/revise-proposal", handlers.ReviseProposalHandler).Methods("PATCH")+r.HandleFunc("/summarize", handlers.SummarizeHandler).Methods("POST")// Get port from the environment variable or default to 8088diff --git a/server/model/client.go b/server/model/client.goindex 14707070..340f20ce 100644--- a/server/model/client.go+++ b/server/model/client.go@@ -2,12 +2,15 @@ package modelimport ("os"+ "time""github.com/sashabaranov/go-openai")-var client *openai.Client+const OPENAI_STREAM_CHUNK_TIMEOUT = time.Duration(30) * time.Second++var Client *openai.Clientfunc init() {- client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))+ Client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))}diff --git a/server/model/context.go b/server/model/context.goindex ec4f2776..1cfb2d06 100644--- a/server/model/context.go+++ b/server/model/context.go@@ -7,7 +7,7 @@ import ("github.com/plandex/plandex/shared")-func formatModelContext(context shared.ModelContext) string {+func FormatModelContext(context shared.ModelContext) string {var contextMessages []stringfor _, part := range context {var message string@@ -24,7 +24,7 @@ func formatModelContext(context shared.ModelContext) string {return strings.Join(contextMessages, "\n")}-func formatCurrentPlan(plan shared.CurrentPlanFiles) string {+func FormatCurrentPlan(plan shared.CurrentPlanFiles) string {var planMessages []stringfor path, content := range plan.Files {planMessages = append(planMessages, fmt.Sprintf("\n\n-file: %s\n\n```%s```", path, content))diff --git a/server/model/prompt.go b/server/model/prompt.godeleted file mode 100644index d3a763ff..00000000--- a/server/model/prompt.go+++ /dev/null@@ -1,389 +0,0 @@-package model--import (- "context"- "encoding/json"- "fmt"- "strings"-- "github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"- "github.com/sashabaranov/go-openai/jsonschema"-)--type fileResult struct {- filePath string- content string- isExec bool-}--type firstParseResponse struct {- Reply string `json:"reply"`- CommitMsg string `json:"commitMsg"`- Files []string `json:"files"`-}--type parseFileResponse struct {- Content string `json:"content"`-}--func Prompt(req shared.PromptRequest) ([]byte, error) {- contextText := formatModelContext(req.ModelContext)-- systemMessageText := `- You are Plandex, an AI programming and system administration assistant.- You help programmers with tasks, especially those that involve multiple files and shell commands. You offer a structured, versioned, and iterative approach to AI-driven development.- You and the programmer collaborate to create a 'plan' for the task at hand. A plan is a set of files and an 'exec' script with an attached context.- Based on user-provided context, please create a plan for the task. When suggesting changes that would modify files from the context or create new files, always precede them with the file path.- Context from the user:` + contextText + `- Current state of the plan:` + formatCurrentPlan(req.CurrentPlan)-- systemMessage := openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleSystem,- Content: systemMessageText,- }-- messages := []openai.ChatCompletionMessage{- systemMessage,- }-- if len(req.Conversation) > 0 {- messages = append(messages, req.Conversation...)- }-- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: req.Prompt,- })-- for _, message := range messages {- fmt.Printf("%s: %s\n", message.Role, message.Content)- }-- initialResp, err := client.CreateChatCompletion(- context.Background(),- openai.ChatCompletionRequest{- Model: openai.GPT4,- Messages: messages,- },- )-- if err != nil {- fmt.Printf("Error while creating chat completion: %v\n", err)- return nil, err- }-- initialStrRes := initialResp.Choices[0].Message.Content- fmt.Println("Initial response from model:")- fmt.Println(initialStrRes)-- if req.ChatOnly {- chatResp := shared.PromptResponse{- Reply: initialStrRes,- }- chatByteRes, err := json.Marshal(chatResp)- if err != nil {- fmt.Printf("Error marshalling chat response: %v\n", err)- return nil, err- }- return chatByteRes, nil- }-- firstParseResp, err := client.CreateChatCompletion(- context.Background(),- openai.ChatCompletionRequest{- Model: openai.GPT3Dot5Turbo,- Functions: []openai.FunctionDefinition{{- Name: "parse",- Parameters: &jsonschema.Definition{- Type: jsonschema.Object,- Properties: map[string]jsonschema.Definition{- "reply": {- Type: jsonschema.String,- Description: "Your previous response without code or commands.",- },- "commitMsg": {- Type: jsonschema.String,- Description: "A brief commit message for the changes proposed in your previous response.",- },- "files": {- Type: jsonschema.Array,- Description: "An array of file paths from your previous response that should be created or updated.",- Items: &jsonschema.Definition{- Type: jsonschema.String,- },- },- },- Required: []string{"reply", "commitMsg", "files"},- },- }},- Messages: []openai.ChatCompletionMessage{- {- Role: openai.ChatMessageRoleSystem,- Content: "You are an AI parser...",- },- {- Role: openai.ChatMessageRoleAssistant,- Content: initialStrRes,- },- },- },- )-- if err != nil {- fmt.Printf("Error during first parse completion: %v\n", err)- return nil, err- }-- var firstParseStrRes string- for _, choice := range firstParseResp.Choices {- if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "parse" {- fnCall := choice.Message.FunctionCall- firstParseStrRes = fnCall.Arguments- }- }-- if firstParseStrRes == "" {- return nil, fmt.Errorf("no parse function call found in response")- }-- firstParseByteRes := []byte(firstParseStrRes)- var firstParsePromptResp firstParseResponse- err = json.Unmarshal(firstParseByteRes, &firstParsePromptResp)- if err != nil {- fmt.Printf("Error unmarshalling first parse response: %v\n", err)- fmt.Printf("JSON causing the error: %s\n", firstParseByteRes)- return nil, err- }-- finalResp := shared.PromptResponse{- Reply: firstParsePromptResp.Reply,- CommitMsg: firstParsePromptResp.CommitMsg,- Files: map[string]string{},- }-- // Channel to receive results from goroutines- results := make(chan fileResult, len(firstParsePromptResp.Files))-- // Create a cancellable context- ctx, cancel := context.WithCancel(context.Background())- defer cancel() // Make sure all resources are cleaned up-- // Error channel to catch the first occurring error- errChan := make(chan error, 1)-- // Channel to signal completion of goroutines- done := make(chan bool, len(firstParsePromptResp.Files))-- for _, filePath := range firstParsePromptResp.Files {- go func(filePath string) {- // Listen for context cancellation- select {- case <-ctx.Done():- done <- true- return- default:- }-- fmt.Println("Getting file from model: " + filePath)-- // get relevant file context (if any)- var fileContext *shared.ModelContextPart- for _, part := range req.ModelContext {- if part.FilePath == filePath {- fileContext = &part- break- }- }-- fileMessages := []openai.ChatCompletionMessage{}- if fileContext != nil {- fileMessages = append(fileMessages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleSystem,- Content: fmt.Sprintf("Original file %s:\n```\n%s\n```", filePath, fileContext.Body),- })- }-- fileMessages = append(fileMessages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: initialStrRes,- },- openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(`- Based on your previous response, call the 'writeFile' function with the full content of the file %s as raw text, including any updates. You must include the entire file and not leave anything out, even if it is already present in the original file. Do not include any placeholders or references to the original file. Output the updated entire file. Only call the 'writeFile' function in your reponse. Don't call any other function.- `, filePath),- })-- fileResp, err := client.CreateChatCompletion(- ctx,- openai.ChatCompletionRequest{- Model: openai.GPT4,- // Temperature: 0,- Functions: []openai.FunctionDefinition{{- Name: "writeFile",- Parameters: &jsonschema.Definition{- Type: jsonschema.Object,- Properties: map[string]jsonschema.Definition{- "content": {- Type: jsonschema.String,- Description: "The full content of the file, including any updates from the previous response, as raw text",- },- },- Required: []string{"content"},- },- }},- Messages: fileMessages,- },- )-- if err != nil {- fmt.Println("Error getting file from model: " + filePath + ": " + err.Error())-- // Send error to error channel and cancel the context- select {- case errChan <- err:- default:- }- cancel()- }-- var fileResStr string- for _, choice := range fileResp.Choices {- if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "writeFile" {- fnCall := choice.Message.FunctionCall- fileResStr = fnCall.Arguments- break- }- }-- if fileResStr == "" {- fmt.Println("No response from model for file " + filePath)- } else {- var fileRes parseFileResponse- if err := json.Unmarshal([]byte(fileResStr), &fileRes); err != nil {- // Send error to error channel and cancel the context- select {- case errChan <- err:- default:- }- cancel()- }-- fmt.Println("File response from model for file " + filePath + ":")- fmt.Println(fileRes.Content)-- // Send result to channel- results <- fileResult{- filePath: filePath,- content: fileRes.Content,- }- }-- done <- true-- }(filePath)- }-- go func() {- // Listen for context cancellation- select {- case <-ctx.Done():- done <- true- return- default:- }-- fmt.Println("Getting exec.sh from model: ")-- execResp, err := client.CreateChatCompletion(- ctx,- openai.ChatCompletionRequest{- Model: openai.GPT4,- Messages: []openai.ChatCompletionMessage{- {- Role: openai.ChatMessageRoleAssistant,- Content: initialStrRes,- },- {- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(`- Output a shell script that includes any commands from the previous message that should be executed after any relevant files are created and/or updated.-- If no commands should be executed, output only 'no exec commands'. Otherwise, output the script as raw text and nothing else.- `),- },- },- },- )-- if err != nil {- fmt.Println("Error getting shell script from model: " + err.Error())-- // Send error to error channel and cancel the context- select {- case errChan <- err:- default:- }- cancel()- }-- if err == nil && len(execResp.Choices) > 0 {- content := execResp.Choices[0].Message.Content-- fmt.Println("Exec res from model:")- fmt.Println(content)-- // Check if content includes phrase 'No commands'- if strings.Contains(strings.ToLower(content), "no exec commands") {- fmt.Println("No commands to execute")- done <- true- return- }-- // Send result to channel- results <- fileResult{- content: content,- isExec: true,- }- } else {- fmt.Println("No response from model for shell script")- }-- done <- true- }()-- // Wait for all goroutines to finish or an error to occur- for i := 0; i < len(firstParsePromptResp.Files)+1; i++ {- select {- case <-done:- case err := <-errChan:- return nil, err- }- }- close(results) // Close the results channel after all routines are done-- // Process the results- for result := range results {- fmt.Println("File response from model for file " + result.filePath + ":")- fmt.Println(result.content)-- content := strings.TrimPrefix(result.content, "```")- content = strings.TrimSuffix(content, "```")-- if result.isExec {- finalResp.Exec = content- } else {- finalResp.Files[result.filePath] = content- }- }-- // convert final response to JSON- finalByteRes, err := json.Marshal(finalResp)-- if err != nil {- return nil, err- }-- return finalByteRes, nil-}diff --git a/server/model/proposal/abort.go b/server/model/proposal/abort.gonew file mode 100644index 00000000..eac80b37--- /dev/null+++ b/server/model/proposal/abort.go@@ -0,0 +1,23 @@+package proposal++import "fmt"++func AbortProposal(proposalId string) error {+ mu.Lock()+ proposal, ok := proposalsMap[proposalId]++ if !ok {+ mu.Unlock()+ return fmt.Errorf("proposal not found")+ }++ if proposal.Cancel != nil {+ (*proposal.Cancel)()+ }++ proposal.Aborted = true++ mu.Unlock()++ return nil+}diff --git a/server/model/proposal/confirm.go b/server/model/proposal/confirm.gonew file mode 100644index 00000000..4353b77d--- /dev/null+++ b/server/model/proposal/confirm.go@@ -0,0 +1,399 @@+package proposal++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "plandex-server/model"+ "plandex-server/types"+ "strings"+ "sync"+ "time"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+ "github.com/sashabaranov/go-openai/jsonschema"+)++type firstParseResponse struct {+ Summary string `json:"summary"`+ CommitMsg string `json:"commitMsg"`+ Files []string `json:"files"`+}++type parseFileResponse struct {+ Content string `json:"content"`+}++func ConfirmProposal(proposalId string, onStream types.OnStreamPlanFunc) (*context.CancelFunc, error) {+ mu.Lock()+ proposal, ok := proposalsMap[proposalId]+ mu.Unlock()+ if !ok {+ return nil, errors.New("proposal not found")+ }++ ctx, cancel := context.WithCancel(context.Background())+ var wg sync.WaitGroup++ var proposalContent string+ mu.Lock()+ proposal.Cancel = &cancel+ proposalsMap[proposalId] = proposal+ proposalContent = proposal.Content+ mu.Unlock()++ setError := func(err error) {+ mu.Lock()+ proposal := proposalsMap[proposalId]+ proposal.ProposalError = err+ proposalsMap[proposalId] = proposal+ mu.Unlock()+ }++ firstParseResp, err := model.Client.CreateChatCompletion(+ ctx,+ openai.ChatCompletionRequest{+ Model: openai.GPT3Dot5Turbo,+ Functions: []openai.FunctionDefinition{{+ Name: "parse",+ Parameters: &jsonschema.Definition{+ Type: jsonschema.Object,+ Properties: map[string]jsonschema.Definition{+ "summary": {+ Type: jsonschema.String,+ Description: "High-level summary of changed proposed in previous response without code or commands.",+ },+ "commitMsg": {+ Type: jsonschema.String,+ Description: "A brief commit message for the changes proposed in AI's plan from previous response.",+ },+ "files": {+ Type: jsonschema.Array,+ Description: "An array of file paths in AI's plan from previous response that should be created or updated.",+ Items: &jsonschema.Definition{+ Type: jsonschema.String,+ },+ },+ },+ Required: []string{"summary", "commitMsg", "files"},+ },+ }},+ Messages: []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: "You are an AI parser. You turn an AI's plan for a programming task into a high-level summary, a set of files and an exec script. You call the 'parse' function with these arguments: summary, commitMsg, files. Only call the 'parse' function in your response. Don't call any other function.",+ },+ {+ Role: openai.ChatMessageRoleAssistant,+ Content: proposalContent,+ },+ },+ },+ )++ if err != nil {+ fmt.Printf("Error during first parse completion: %v\n", err)+ setError(err)+ return nil, err+ }++ var firstParseStrRes string+ for _, choice := range firstParseResp.Choices {+ if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "parse" {+ fnCall := choice.Message.FunctionCall+ firstParseStrRes = fnCall.Arguments+ }+ }++ if firstParseStrRes == "" {+ err = fmt.Errorf("no parse function call found in response")+ setError(err)+ return nil, err+ }++ firstParseByteRes := []byte(firstParseStrRes)+ var firstParsePromptResp firstParseResponse+ err = json.Unmarshal(firstParseByteRes, &firstParsePromptResp)+ if err != nil {+ fmt.Printf("Error unmarshalling first parse response: %v\n", err)+ fmt.Printf("JSON causing the error: %s\n", firstParseByteRes)+ setError(err)+ return nil, err+ }++ for _, filePath := range firstParsePromptResp.Files {+ wg.Add(1)++ onError := func(err error) {+ setError(err)+ onStream(&shared.PlanChunk{FilePath: filePath}, true, err)+ }++ go func(filePath string) {+ defer wg.Done()++ fmt.Println("Getting file from model: " + filePath)++ // get relevant file context (if any)+ var fileContext *shared.ModelContextPart+ for _, part := range *proposal.ModelContext {+ if part.FilePath == filePath {+ fileContext = &part+ break+ }+ }++ fileMessages := []openai.ChatCompletionMessage{}+ if fileContext != nil {+ fileMessages = append(fileMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: fmt.Sprintf("Original file %s:\n```\n%s\n```", filePath, fileContext.Body),+ })+ }++ fileMessages = append(fileMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: proposalContent,+ },+ openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: fmt.Sprintf(`+ Based on your previous response, call the 'writeFile' function with the full content of the file %s as raw text, including any updates. You must include the entire file and not leave anything out, even if it is already present in the original file. Do not include any placeholders or references to the original file. Output the updated entire file. Only call the 'writeFile' function in your reponse. Don't call any other function.+ `, filePath),+ })++ modelReq := openai.ChatCompletionRequest{+ Model: openai.GPT4,+ Functions: []openai.FunctionDefinition{{+ Name: "writeFile",+ Parameters: &jsonschema.Definition{+ Type: jsonschema.Object,+ Properties: map[string]jsonschema.Definition{+ "content": {+ Type: jsonschema.String,+ Description: "The full content of the file, including any updates from the previous response, as raw text",+ },+ },+ Required: []string{"content"},+ },+ }},+ Messages: fileMessages,+ }++ stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)+ if err != nil {+ fmt.Printf("Error creating plan file stream for path %s: %v\n", filePath, err)+ onError(err)+ return+ }++ 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 <-ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fmt.Println("\nStream timeout due to inactivity")+ err = fmt.Errorf("stream timeout due to inactivity")+ onError(err)+ 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 errors.Is(err, io.EOF) {+ fmt.Println("\nStream finished")+ return+ }++ if err != nil {+ fmt.Printf("\nStream error: %v\n", err)+ onError(err)+ return+ }++ if len(response.Choices) == 0 {+ fmt.Println("\nStream finished")+ return+ }++ // TODO handle different finish reasons+ if response.Choices[0].FinishReason != "" {+ fmt.Println("\nStream finished")+ return+ }++ var content string+ delta := response.Choices[0].Delta+ if delta.FunctionCall != nil && delta.FunctionCall.Name == "writeFile" {+ content = delta.FunctionCall.Arguments+ } else {++ }++ chunk := &shared.PlanChunk{+ FilePath: filePath,+ Content: content,+ IsExec: false,+ }++ fmt.Printf("%s: %s", filePath, content)+ onStream(chunk, false, nil)+ }+ }+ }()+ }(filePath)+ }++ wg.Add(1)+ onExecErr := func(err error) {+ setError(err)+ onStream(&shared.PlanChunk{IsExec: true}, true, err)+ }+ go func() {+ defer wg.Done()+ fmt.Println("Getting exec.sh from model: ")++ // Define the model request for exec.sh+ modelReq := openai.ChatCompletionRequest{+ Model: openai.GPT4,+ Functions: []openai.FunctionDefinition{{+ Name: "writeExec",+ Parameters: &jsonschema.Definition{+ Type: jsonschema.Object,+ Properties: map[string]jsonschema.Definition{+ "content": {+ Type: jsonschema.String,+ Description: "The shell script from the previous message, including any updates",+ },+ },+ Required: []string{"content"},+ },+ }},+ Messages: []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: "You are an AI parser. You turn an AI's plan for a programming task into executable shell commands. Call the 'writeExec' function with the entire shell script as raw text, based on your previous response. Only call the 'writeExec' function in your response. Don't call any other function.",+ },+ {+ Role: openai.ChatMessageRoleAssistant,+ Content: proposalContent,+ },+ {+ Role: openai.ChatMessageRoleUser,+ Content: fmt.Sprintf(`+ Based on your previous response, call the 'writeExec' function with the shell script that includes any commands that should be executed after any relevant files are created and/or updated. If no commands should be executed, pass only 'no exec commands' to the 'writeExec' function. Otherwise, pass the full script as raw text to the function and output nothing else.+ `),+ },+ },+ }++ stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)+ if err != nil {+ fmt.Println("Error creating shell script stream: " + err.Error())+ onExecErr(err)+ return+ }++ 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 <-ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fmt.Println("\nStream timeout due to inactivity")+ err = fmt.Errorf("stream timeout due to inactivity")+ onExecErr(err)+ 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 errors.Is(err, io.EOF) {+ fmt.Println("\nStream finished for exec.sh")+ return+ }++ if err != nil {+ fmt.Printf("\nStream error for exec.sh: %v\n", err)+ onExecErr(err)+ return+ }++ // TODO handle different finish reasons+ if response.Choices[0].FinishReason != "" {+ fmt.Println("\nStream finished")+ return+ }++ if len(response.Choices) == 0 {+ fmt.Println("\nStream finished for exec.sh")+ return+ }++ var content string+ delta := response.Choices[0].Delta+ if delta.FunctionCall != nil && delta.FunctionCall.Name == "writeExec" {+ content = delta.FunctionCall.Arguments+ }++ fmt.Printf("exec.sh content: %s", content)++ // Check if content includes phrase 'No commands'+ if strings.Contains(strings.ToLower(content), "no exec commands") {+ fmt.Println("No commands to execute for exec.sh")+ } else {+ fmt.Println("Commands to execute for exec.sh")+ onStream(&shared.PlanChunk{+ IsExec: true,+ Content: content,+ }, false, nil)+ }+ }+ }+ }()++ // Wait for all streams to finish and then inform the caller+ go func() {+ wg.Wait()+ mu.Lock()+ delete(proposalsMap, proposalId)+ mu.Unlock()+ onStream(nil, true, nil)+ }()++ return &cancel, nil++}diff --git a/server/model/proposal/create.go b/server/model/proposal/create.gonew file mode 100644index 00000000..8606acd1--- /dev/null+++ b/server/model/proposal/create.go@@ -0,0 +1,177 @@+package proposal++import (+ "context"+ "errors"+ "fmt"+ "io"+ "sync"+ "time"++ "plandex-server/model"+ "plandex-server/types"++ "github.com/google/uuid"+ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++// map to store the proposals+var (+ proposalsMap = make(map[string]*types.Proposal)+ mu sync.Mutex+)++// Proposal function to create a new proposal+func CreateProposal(req shared.PromptRequest, onStream types.OnStreamProposalFunc) (*context.CancelFunc, error) {+ contextText := model.FormatModelContext(req.ModelContext)++ systemMessageText := `+ You are Plandex, an AI programming and system administration assistant.+ You help programmers with tasks, especially those that involve multiple files and shell commands. You offer a structured, versioned, and iterative approach to AI-driven development.+ You and the programmer collaborate to create a 'plan' for the task at hand. A plan is a set of files and an 'exec' script with an attached context.+ Based on user-provided context, please create a plan for the task. When suggesting changes that would modify files from the context or create new files, always precede them with the file path.+ Context from the user:` + contextText++ systemMessage := openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: systemMessageText,+ }++ messages := []openai.ChatCompletionMessage{+ systemMessage,+ }++ if len(req.Conversation) > 0 {+ messages = append(messages, req.Conversation...)+ }++ messages = append(messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: req.Prompt,+ })++ for _, message := range messages {+ fmt.Printf("%s: %s\n", message.Role, message.Content)+ }++ proposalId, err := uuid.NewRandom()+ if err != nil {+ fmt.Printf("Failed to generate proposal id: %v\n", err)+ return nil, err+ }++ ctx, cancel := context.WithCancel(context.Background())++ // store the proposal+ mu.Lock()+ proposalsMap[proposalId.String()] = &types.Proposal{+ Id: proposalId.String(),+ Cancel: &cancel,+ ModelContext: &req.ModelContext,+ Content: "",+ }+ mu.Unlock()++ modelReq := openai.ChatCompletionRequest{+ Model: openai.GPT4,+ Messages: messages,+ Stream: true,+ }++ stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)+ if err != nil {+ fmt.Printf("Error creating proposal GPT4 stream: %v\n", err)+ mu.Lock()+ delete(proposalsMap, proposalId.String()) // Remove the proposal from the map+ mu.Unlock()+ return nil, err+ }++ onFinish := func() {+ mu.Lock()+ proposal := proposalsMap[proposalId.String()]+ proposal.FinishedProposal = true+ proposalsMap[proposalId.String()] = proposal+ mu.Unlock()+ onStream("", true, nil)+ }++ onError := func(err error) {+ fmt.Printf("\nStream error: %v\n", err)+ mu.Lock()+ proposal := proposalsMap[proposalId.String()]+ proposal.ProposalError = err+ proposalsMap[proposalId.String()] = proposal+ mu.Unlock()+ onStream("", true, 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 <-ctx.Done():+ // The main context was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ fmt.Println("\nStream timeout due to inactivity")+ onError(fmt.Errorf("stream timeout due to inactivity"))+ 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 errors.Is(err, io.EOF) {+ fmt.Println("\nStream finished")+ onFinish()+ return+ }++ if err != nil {+ onError(err)+ return+ }++ if len(response.Choices) == 0 {+ fmt.Println("\nStream finished")+ onFinish()+ return+ }++ // TODO handle different finish reasons+ if response.Choices[0].FinishReason != "" {+ fmt.Println("\nStream finished")+ onFinish()+ }++ content := response.Choices[0].Delta.Content++ // add to the proposal+ mu.Lock()+ proposal := proposalsMap[proposalId.String()]+ proposal.Content += content+ proposalsMap[proposalId.String()] = proposal+ mu.Unlock()++ fmt.Printf("%s", content)+ onStream(content, false, nil)+ }+ }+ }()++ return &cancel, nil+}diff --git a/server/model/proposal/revise.go b/server/model/proposal/revise.gonew file mode 100644index 00000000..5b0a1556--- /dev/null+++ b/server/model/proposal/revise.go@@ -0,0 +1 @@+package proposaldiff --git a/server/model/summarize.go b/server/model/summarize.goindex 3adfda96..8e9c220a 100644--- a/server/model/summarize.go+++ b/server/model/summarize.go@@ -12,7 +12,7 @@ import ()func Summarize(text string) ([]byte, error) {- resp, err := client.CreateChatCompletion(+ resp, err := Client.CreateChatCompletion(context.Background(),openai.ChatCompletionRequest{Model: openai.GPT3Dot5Turbo,diff --git a/server/types/types.go b/server/types/types.gonew file mode 100644index 00000000..1810f6a2--- /dev/null+++ b/server/types/types.go@@ -0,0 +1,30 @@+package types++import (+ "context"++ "github.com/plandex/plandex/shared"+)++type Proposal struct {+ Id string+ Cancel *context.CancelFunc+ ModelContext *shared.ModelContext+ Content string+ FinishedProposal bool+ ProposalError error+ Aborted bool+ ConvertPlanError error+}++type OnStreamProposalFunc func(+ content string,+ finished bool,+ err error,+)++type OnStreamPlanFunc func(+ chunk *shared.PlanChunk,+ finished bool,+ err error,+)diff --git a/shared/const.go b/shared/const.gonew file mode 100644index 00000000..b20378d6--- /dev/null+++ b/shared/const.go@@ -0,0 +1,3 @@+package shared++const STREAM_FINISHED = "STREAM_FINISHED"diff --git a/shared/types.go b/shared/types.goindex 387880c1..b74e3a13 100644--- a/shared/types.go+++ b/shared/types.go@@ -28,7 +28,7 @@ type SummarizeResponse struct {}type PromptResponse struct {- Reply string `json:"reply"`+ Summary string `json:"summary"`CommitMsg string `json:"commitMsg"`Files map[string]string `json:"files"`Exec string `json:"exec"`@@ -58,6 +58,12 @@ type PlanSettings struct {Name string `json:"name"`}+type PlanChunk struct {+ FilePath string `json:"filePath"`+ Content string `json:"content"`+ IsExec bool `json:"isExec"`+}+// type Plan struct {// Id string// Name string