Skip to content

Commit

Permalink
chore: add confirm prompt for adding files to context
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Nov 28, 2024
1 parent 195c2df commit 3f56161
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 54 deletions.
69 changes: 42 additions & 27 deletions cmd/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,35 @@ func handleCodeCommand(rootDependencies *RootDependencies) {

// Helper function to process and send the current chunks
processEmbeddingsChunks := func() error {
if len(currentChunks) == 0 {
return nil // Nothing to process
}
if rootDependencies.Config.RAG {
if len(currentChunks) == 0 {
return nil // Nothing to process
}

// Extract the content from the currentChunks
var contents []string
for _, chunk := range currentChunks {
contents = append(contents, chunk.Content)
}
// Extract the content from the currentChunks
var contents []string
for _, chunk := range currentChunks {
contents = append(contents, chunk.Content)
}

// Delay before sending the request
time.Sleep(requestDelay)
// Delay before sending the request
time.Sleep(requestDelay)

// Make an embedding request for the current chunks
embedding, err := rootDependencies.CurrentEmbeddingProvider.EmbeddingRequest(ctx, contents)
if err != nil {
return err
}
// Make an embedding request for the current chunks
embedding, err := rootDependencies.CurrentEmbeddingProvider.EmbeddingRequest(ctx, contents)
if err != nil {
return err
}

// Save embeddings to the embedding store
for i, chunk := range currentChunks {
rootDependencies.Store.Save(chunk.RelativePath, chunk.Content, embedding[i])
}
// Save embeddings to the embedding store
for i, chunk := range currentChunks {
rootDependencies.Store.Save(chunk.RelativePath, chunk.Content, embedding[i])
}

// Reset the chunks and token count
currentChunks = []general_models.ChunkData{}
currentTokenCount = 0
// Reset the chunks and token count
currentChunks = []general_models.ChunkData{}
currentTokenCount = 0
}

return nil
}
Expand Down Expand Up @@ -272,13 +274,26 @@ startLoop: // Label for the start loop
if requestedContext != "" && err == nil {
aiResponseBuilder.Reset()

fmt.Println(lipgloss.BlueSky.Render("\nThese files need to changes...\n"))
fmt.Print("\n")

if err := chatRequestOperation(); err != nil {
fmt.Println(lipgloss.Red.Render(fmt.Sprintf("%v", err)))
displayTokens()
contextAccepted, err := utils.ConfirmAdditinalContext()
if err != nil {
fmt.Println(lipgloss.Red.Render(fmt.Sprintf("error getting user prompt: %v", err)))
continue
}

if contextAccepted {
fmt.Println(lipgloss.Green.Render("✔️ Context accepted!"))

if err := chatRequestOperation(); err != nil {
fmt.Println(lipgloss.Red.Render(fmt.Sprintf("%v", err)))
displayTokens()
continue
}

} else {
fmt.Println(lipgloss.Red.Render("❌ Context rejected."))
}
}
}

Expand All @@ -291,7 +306,7 @@ startLoop: // Label for the start loop
continue
}

fmt.Print("\n\n")
fmt.Print("\n")

// Try to apply changes
for _, change := range changes {
Expand Down
2 changes: 0 additions & 2 deletions code_analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ func (analyzer *CodeAnalyzer) ProcessFile(filePath string, sourceCode []byte) []
log.Fatalf("failed to parse JSON: %v", err)
}

elements = append(elements, filePath)

// Execute each query and capture results
for tag, queryStr := range queries {
query, err := sitter.NewQuery([]byte(queryStr), lang) // Use the appropriate language
Expand Down
4 changes: 2 additions & 2 deletions constants/lipgloss/lipgloss_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lipgloss
import "github.com/charmbracelet/lipgloss"

var (
BlueSky = lipgloss.NewStyle().Foreground(lipgloss.Color("#00BFFF"))
BlueSky = lipgloss.NewStyle().Foreground(lipgloss.Color("#00BFFF")).Bold(true)
LightBlue = lipgloss.NewStyle().Foreground(lipgloss.Color("#2b7fec")).Bold(true)
LightBlueB = lipgloss.NewStyle().Background(lipgloss.Color("#E5E7E9")).Foreground(lipgloss.Color("#2b7fec")).Bold(true)
Red = lipgloss.NewStyle().Foreground(lipgloss.Color("197"))
Expand All @@ -12,5 +12,5 @@ var (
Violet = lipgloss.NewStyle().Foreground(lipgloss.Color("#7F00FF"))
Charm = lipgloss.NewStyle().Foreground(lipgloss.Color("205")).Bold(true)
CharmB = lipgloss.NewStyle().Background(lipgloss.Color("#E5E7E9")).Foreground(lipgloss.Color("205")).Bold(true)
Gray = lipgloss.NewStyle().Foreground(lipgloss.Color("#bcbcbc"))
Gray = lipgloss.NewStyle().Foreground(lipgloss.Color("#bcbcbc")).Bold(true)
)
20 changes: 8 additions & 12 deletions embed_data/prompts/summarize_full_context_prompt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@
> Your tasks are according to these steps:

## PRIORITY: Check for Specific Context in Code
- **You just have the signature of the full context and if you need a file for doing task just request full files from the user**.
- **If I request the **specific context of code**, such as a **method**, **class**, or any **part of codes** that is an **empty body** or **incomplete** code, I will provide **full file of code** in the **next request** and base on that you can do your task and you **must** follow these steps:**
- **These examples of context of code are incomplete**:
```go

---
function AddProduct(){
}
```
```go

function DeleteProduct(){
```
```go

struct: Product
```
```go

class: Product
```
---

- **I you see the incomplete code like above example just return the relative paths of the relevant files that are incomplete as a JSON array of strings in the following format:**
```json
{
"files": ["relative path1", "relative path2"]
}
- **Skip all other tasks and return only this JSON response. Do not proceed to any additional prompt processing.**

## General Instructions for Code Modifications
If the request does not fall under the above condition, proceed with the following steps:

- **If you have requested full files for doing your task you can move forward for other prompts, otherwise just ignore other prompts.**

## Context Understanding:
- Read and Analyze the code context carefully to identify where the requested changes should be added or modified.
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.2
require (
github.com/alecthomas/chroma/v2 v2.14.0
github.com/charmbracelet/lipgloss v1.0.0
github.com/pkoukk/tiktoken-go v0.1.7
github.com/pterm/pterm v0.12.79
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82
github.com/spf13/cobra v1.8.1
Expand All @@ -17,7 +18,7 @@ require (
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.4.2 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
Expand All @@ -35,7 +36,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkoukk/tiktoken-go v0.1.7 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand All @@ -50,8 +50,8 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
github.com/charmbracelet/x/ansi v0.4.2 h1:0JM6Aj/g/KC154/gOP4vfxun0ff6itogDYk41kof+qk=
github.com/charmbracelet/x/ansi v0.4.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -173,15 +173,15 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
26 changes: 25 additions & 1 deletion utils/confirm_prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

// ConfirmPrompt PromptUser prompts the user to accept or reject the changes with a charming interface
// ConfirmPrompt prompts the user to accept or reject the changes in a file path
func ConfirmPrompt(path string) (bool, error) {
reader := bufio.NewReader(os.Stdin)

Expand All @@ -31,3 +31,27 @@ func ConfirmPrompt(path string) (bool, error) {
}
}
}

// ConfirmAdditinalContext prompts the user to accept or reject additional context
func ConfirmAdditinalContext() (bool, error) {
reader := bufio.NewReader(os.Stdin)

// Styled prompt message
fmt.Printf(lipgloss.Gray.Render(fmt.Sprintf("Do you want to add above files to context %s", lipgloss.Gray.Render("? (y/n): "))))

for {
// Read user input
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)

if input == "" {
continue
}

if input == "y" || input == "Y" {
return true, nil
} else if input == "n" || input == "N" {
return false, nil
}
}
}

0 comments on commit 3f56161

Please sign in to comment.