Skip to content

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

continuing to experiment with new build approach

Dane Schneidercac07d92cf68
diff --git a/app/server/model/plan/build_fix_result.go b/app/server/model/plan/build_fix_result.go
index 1d3b7b13..488d0b30 100644
--- a/app/server/model/plan/build_fix_result.go
+++ b/app/server/model/plan/build_fix_result.go
@@ -41,7 +41,7 @@ func (fileState *activeBuildStreamFileState) onFixResult(res types.ChangesWithLi
var jStartLine int
// Convert the line number part to an integer
- iStartLine, _, err := sorted[i].GetLines()
+ iStartLine, _, err := sorted[i].Old.GetLines()
if err != nil {
log.Printf("listenStream - Error getting start line for change %v: %v\n", sorted[i], err)
@@ -49,7 +49,7 @@ func (fileState *activeBuildStreamFileState) onFixResult(res types.ChangesWithLi
return false
}
- jStartLine, _, err = sorted[j].GetLines()
+ jStartLine, _, err = sorted[j].Old.GetLines()
if err != nil {
log.Printf("listenStream - Error getting start line for change %v: %v\n", sorted[j], err)
diff --git a/app/server/model/plan/build_result.go b/app/server/model/plan/build_result.go
index 9e220867..d75a17e8 100644
--- a/app/server/model/plan/build_result.go
+++ b/app/server/model/plan/build_result.go
@@ -42,7 +42,27 @@ type PlanResultParams struct {
FixEpoch int
}
-func GetPlanResult(ctx context.Context, params PlanResultParams) (*db.PlanFileResult, string, bool, error) {
+type PlanResultParamsUpdated struct {
+ OrgId string
+ PlanId string
+ PlanBuildId string
+ ConvoMessageId string
+ FilePath string
+ PreBuildState string
+ ProposedChanges string
+ EntireFile bool
+ OverlapStrategy OverlapStrategy
+ ChangesWithLineNums []*shared.StreamedChangeWithLineNumsUpdated
+
+ CheckSyntax bool
+
+ IsFix bool
+ IsSyntaxFix bool
+ IsOtherFix bool
+ FixEpoch int
+}
+
+func GetPlanResultUpdated(ctx context.Context, params PlanResultParamsUpdated) (*db.PlanFileResult, string, bool, error) {
orgId := params.OrgId
planId := params.PlanId
planBuildId := params.PlanBuildId
@@ -51,97 +71,234 @@ func GetPlanResult(ctx context.Context, params PlanResultParams) (*db.PlanFileRe
streamedChangesWithLineNums := params.ChangesWithLineNums
preBuildState = shared.AddLineNums(preBuildState)
-
preBuildStateLines := strings.Split(preBuildState, "\n")
+ proposedChangesLines := strings.Split(params.ProposedChanges, "\n")
var replacements []*shared.Replacement
- var highestEndLine int = 0
+ if params.EntireFile {
+ replacements = append(replacements, &shared.Replacement{
+ Old: preBuildState,
+ New: params.ProposedChanges,
+ StreamedChangeUpdated: nil,
+ EntireFile: true,
+ })
+ } else {
- for _, streamedChange := range streamedChangesWithLineNums {
- if !streamedChange.HasChange {
- continue
- }
+ var highestEndLineOld int = 0
+ var highestEndLineNew int = 0
- var old string
- new := streamedChange.New
+ for _, streamedChange := range streamedChangesWithLineNums {
+ if !streamedChange.HasChange {
+ continue
+ }
- if streamedChange.Old.EntireFile {
- replacements = append(replacements, &shared.Replacement{
- EntireFile: true,
- Old: old,
- New: new,
- StreamedChange: streamedChange,
- })
- continue
- }
+ getNewSection := func(highestEndLineNewPointer *int) func() (string, error) {
+ highestEndLineNew := *highestEndLineNewPointer
+
+ return func() (string, error) {
+ log.Printf("getNewSection - File %s: Getting new section\n", filePath)
+ startLineNew, endLineNew, err := streamedChange.New.GetLinesWithPrefix("pdx-new-")
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error getting lines from streamedChange: %v\n", filePath, err)
+
+ spew.Dump(streamedChange.New)
+
+ return "", fmt.Errorf("error getting lines from streamedChange: %v", err)
+ }
+
+ if startLineNew > len(proposedChangesLines) {
+ log.Printf("getPlanResult - File %s: Start line is greater than proposedChangesLines length: %d > %d\n", filePath, startLineNew, len(proposedChangesLines))
+ return "", fmt.Errorf("start line is greater than proposedChangesLines length: %d > %d", startLineNew, len(proposedChangesLines))
+ }
+
+ if endLineNew < 1 {
+ log.Printf("getPlanResult - File %s: End line is less than 1: %d\n", filePath, endLineNew)
+ return "", fmt.Errorf("end line is less than 1: %d", endLineNew)
+ }
+ if endLineNew > len(proposedChangesLines) {
+ log.Printf("getPlanResult - File %s: End line is greater than proposedChangesLines length: %d > %d\n", filePath, endLineNew, len(proposedChangesLines))
+ return "", fmt.Errorf("end line is greater than proposedChangesLines length: %d > %d", endLineNew, len(proposedChangesLines))
+ }
+
+ if startLineNew < highestEndLineNew {
+ log.Printf("getPlanResult - File %s: Start line is less than highestEndLine: %d < %d\n", filePath, startLineNew, highestEndLineNew)
+
+ // log.Printf("getPlanResult - File %s: streamedChange:\n", filePath)
+ // log.Println(spew.Sdump(streamedChangesWithLineNums))
+
+ spew.Dump(streamedChangesWithLineNums)
+
+ if params.OverlapStrategy == OverlapStrategyError {
+ return "", fmt.Errorf("start line is less than highestEndLine: %d < %d", startLineNew,
+ highestEndLineNew)
+ } else {
+ return "", nil
+ }
+ }
+
+ if endLineNew < highestEndLineNew {
+ if params.OverlapStrategy == OverlapStrategyError {
+ log.Printf("getPlanResult - File %s: End line is less than highestEndLine: %d < %d\n", filePath, endLineNew, highestEndLineNew)
+ return "", fmt.Errorf("end line is less than highestEndLine: %d < %d", endLineNew, highestEndLineNew)
+ } else {
+ return "", nil
+ }
+ }
+
+ if endLineNew > highestEndLineNew {
+ // log.Printf("getPlanResult - File %s: End line is greater than highestEndLine: %d > %d\n", filePath, endLineNew, highestEndLineNew)
+ // log.Println("Setting highestEndLineNew to endLineNew:", endLineNew)
+ highestEndLineNew = endLineNew
+ }
+
+ var new string
+ if startLineNew == endLineNew {
+ new = proposedChangesLines[startLineNew-1]
+ } else {
+ new = strings.Join(proposedChangesLines[startLineNew-1:endLineNew], "\n")
+ }
+
+ return new, nil
+ }
+ }(&highestEndLineNew)
+
+ var old string
+ var new string
+
+ newSection, err := getNewSection()
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error getting new section: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error getting new section: %v", err)
+ } else if newSection == "" {
+ continue
+ }
- startLine, endLine, err := streamedChange.GetLines()
- if err != nil {
- log.Printf("getPlanResult - File %s: Error getting lines from streamedChange: %v\n", filePath, err)
- return nil, "", false, fmt.Errorf("error getting lines from streamedChange: %v", err)
- }
+ if streamedChange.InsertBefore != nil && streamedChange.InsertBefore.ShouldInsertBefore {
+ lineNum, err := shared.ExtractLineNumber(streamedChange.InsertBefore.Line)
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error extracting line number from firstLine: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error extracting line number from firstLine: %v", err)
+ }
+ line := preBuildStateLines[lineNum-1]
- if startLine > len(preBuildStateLines) {
- log.Printf("getPlanResult - File %s: Start line is greater than preBuildStateLines length: %d > %d\n", filePath, startLine, len(preBuildStateLines))
- return nil, "", false, fmt.Errorf("start line is greater than preBuildStateLines length: %d > %d", startLine, len(preBuildStateLines))
- }
+ old = line
+ new = newSection + "\n" + line
- if endLine < 1 {
- log.Printf("getPlanResult - File %s: End line is less than 1: %d\n", filePath, endLine)
- return nil, "", false, fmt.Errorf("end line is less than 1: %d", endLine)
- }
- if endLine > len(preBuildStateLines) {
- log.Printf("getPlanResult - File %s: End line is greater than preBuildStateLines length: %d > %d\n", filePath, endLine, len(preBuildStateLines))
- return nil, "", false, fmt.Errorf("end line is greater than preBuildStateLines length: %d > %d", endLine, len(preBuildStateLines))
- }
+ replacements = append(replacements, &shared.Replacement{
+ Old: old,
+ New: new,
+ StreamedChangeUpdated: streamedChange,
+ })
- if startLine < highestEndLine {
- log.Printf("getPlanResult - File %s: Start line is less than highestEndLine: %d < %d\n", filePath, startLine, highestEndLine)
+ continue
+ }
- log.Printf("getPlanResult - File %s: streamedChange:\n", filePath)
- log.Println(spew.Sdump(streamedChangesWithLineNums))
+ if streamedChange.InsertAfter != nil && streamedChange.InsertAfter.ShouldInsertAfter {
+ lineNum, err := shared.ExtractLineNumber(streamedChange.InsertAfter.Line)
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error extracting line number from firstLine: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error extracting line number from firstLine: %v", err)
+ }
+ line := preBuildStateLines[lineNum-1]
+
+ old = line
+ new = line + "\n" + newSection
+
+ replacements = append(replacements, &shared.Replacement{
+ Old: old,
+ New: new,
+ StreamedChangeUpdated: streamedChange,
+ })
- if params.OverlapStrategy == OverlapStrategyError {
- return nil, "", false, fmt.Errorf("start line is less than highestEndLine: %d < %d", startLine,
- highestEndLine)
- } else {
continue
}
- }
- if endLine < highestEndLine {
- if params.OverlapStrategy == OverlapStrategyError {
- log.Printf("getPlanResult - File %s: End line is less than highestEndLine: %d < %d\n", filePath, endLine, highestEndLine)
- return nil, "", false, fmt.Errorf("end line is less than highestEndLine: %d < %d", endLine, highestEndLine)
- } else {
- continue
+ startLineOld, endLineOld, err := streamedChange.Old.GetLines()
+ if err != nil {
+ spew.Dump(streamedChange)
+
+ log.Printf("getPlanResult - File %s: Error getting lines from streamedChange: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error getting lines from streamedChange: %v", err)
}
- }
- if endLine > highestEndLine {
- highestEndLine = endLine
- }
+ if startLineOld > len(preBuildStateLines) {
+ log.Printf("getPlanResult - File %s: Start line is greater than preBuildStateLines length: %d > %d\n", filePath, startLineOld, len(preBuildStateLines))
+ return nil, "", false, fmt.Errorf("start line is greater than preBuildStateLines length: %d > %d", startLineOld, len(preBuildStateLines))
+ }
- if startLine == endLine {
- old = preBuildStateLines[startLine-1]
- } else {
- old = strings.Join(preBuildStateLines[startLine-1:endLine], "\n")
- }
+ if endLineOld < 1 {
+ log.Printf("getPlanResult - File %s: End line is less than 1: %d\n", filePath, endLineOld)
+ return nil, "", false, fmt.Errorf("end line is less than 1: %d", endLineOld)
+ }
+ if endLineOld > len(preBuildStateLines) {
+ log.Printf("getPlanResult - File %s: End line is greater than preBuildStateLines length: %d > %d\n", filePath, endLineOld, len(preBuildStateLines))
+ return nil, "", false, fmt.Errorf("end line is greater than preBuildStateLines length: %d > %d", endLineOld, len(preBuildStateLines))
+ }
- replacement := &shared.Replacement{
- Old: old,
- New: new,
- StreamedChange: streamedChange,
- }
+ if startLineOld < highestEndLineOld {
+ log.Printf("getPlanResult - File %s: Start line is less than highestEndLine: %d < %d\n", filePath, startLineOld, highestEndLineOld)
- replacements = append(replacements, replacement)
+ log.Printf("getPlanResult - File %s: streamedChange:\n", filePath)
+ log.Println(spew.Sdump(streamedChangesWithLineNums))
+
+ if params.OverlapStrategy == OverlapStrategyError {
+ return nil, "", false, fmt.Errorf("start line is less than highestEndLine: %d < %d", startLineOld,
+ highestEndLineOld)
+ } else {
+ continue
+ }
+ }
+
+ if endLineOld < highestEndLineOld {
+ if params.OverlapStrategy == OverlapStrategyError {
+ // log.Printf("getPlanResult - File %s: End line is less than highestEndLine: %d < %d\n", filePath, endLineOld, highestEndLineOld)
+ // return nil, "", false, fmt.Errorf("end line is less than highestEndLine: %d < %d", endLineOld, highestEndLineOld)
+ } else {
+ continue
+ }
+ }
+
+ if endLineOld > highestEndLineOld {
+ highestEndLineOld = endLineOld
+ }
+
+ if startLineOld == endLineOld {
+ old = preBuildStateLines[startLineOld-1]
+ } else {
+ old = strings.Join(preBuildStateLines[startLineOld-1:endLineOld], "\n")
+ }
+
+ replacement := &shared.Replacement{
+ Old: old,
+ New: newSection,
+ StreamedChangeUpdated: streamedChange,
+ }
+
+ replacements = append(replacements, replacement)
+ }
}
log.Printf("getPlanResult - File %s: Will apply replacements\n", filePath)
+ // spew.Dump(replacements)
+
+ // sort replacements by index of the 'old' string property in the original file
+ sort.Slice(replacements, func(i, j int) bool {
+ oldI := replacements[i].Old
+ oldJ := replacements[j].Old
+
+ idxI := strings.Index(preBuildState, oldI)
+ idxJ := strings.Index(preBuildState, oldJ)
+
+ return idxI < idxJ
+ })
+
updated, allSucceeded := shared.ApplyReplacements(preBuildState, replacements, true)
+ // log.Printf("getPlanResult - File %s: updated:\n", filePath)
+ // log.Println(updated)
+
updated = shared.RemoveLineNums(updated)
for _, replacement := range replacements {
@@ -212,7 +369,7 @@ func (fileState *activeBuildStreamFileState) onBuildResult(res types.ChangesWith
var iStartLine int
var jStartLine int
- iStartLine, _, err := sorted[i].GetLines()
+ iStartLine, _, err := sorted[i].Old.GetLines()
if err != nil {
log.Printf("onBuildResult - File %s: Error getting start line for change %v: %v\n", filePath, sorted[i], err)
@@ -220,7 +377,7 @@ func (fileState *activeBuildStreamFileState) onBuildResult(res types.ChangesWith
return false
}
- jStartLine, _, err = sorted[j].GetLines()
+ jStartLine, _, err = sorted[j].Old.GetLines()
if err != nil {
log.Printf("onBuildResult - File %s: Error getting start line for change %v: %v\n", filePath, sorted[j], err)
@@ -320,3 +477,143 @@ func (fileState *activeBuildStreamFileState) lineNumsRetryOrError(err error) {
fileState.onBuildFileError(err)
}
}
+
+func GetPlanResult(ctx context.Context, params PlanResultParams) (*db.PlanFileResult, string, bool, error) {
+ orgId := params.OrgId
+ planId := params.PlanId
+ planBuildId := params.PlanBuildId
+ filePath := params.FilePath
+ preBuildState := params.PreBuildState
+ streamedChangesWithLineNums := params.ChangesWithLineNums
+
+ preBuildState = shared.AddLineNums(preBuildState)
+
+ preBuildStateLines := strings.Split(preBuildState, "\n")
+
+ var replacements []*shared.Replacement
+
+ var highestEndLine int = 0
+
+ for _, streamedChange := range streamedChangesWithLineNums {
+ if !streamedChange.HasChange {
+ continue
+ }
+
+ var old string
+ new := streamedChange.New
+
+ if streamedChange.Old.EntireFile {
+ replacements = append(replacements, &shared.Replacement{
+ EntireFile: true,
+ Old: old,
+ New: new,
+ StreamedChange: streamedChange,
+ })
+ continue
+ }
+
+ startLine, endLine, err := streamedChange.Old.GetLines()
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error getting lines from streamedChange: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error getting lines from streamedChange: %v", err)
+ }
+
+ if startLine > len(preBuildStateLines) {
+ log.Printf("getPlanResult - File %s: Start line is greater than preBuildStateLines length: %d > %d\n", filePath, startLine, len(preBuildStateLines))
+ return nil, "", false, fmt.Errorf("start line is greater than preBuildStateLines length: %d > %d", startLine, len(preBuildStateLines))
+ }
+
+ if endLine < 1 {
+ log.Printf("getPlanResult - File %s: End line is less than 1: %d\n", filePath, endLine)
+ return nil, "", false, fmt.Errorf("end line is less than 1: %d", endLine)
+ }
+ if endLine > len(preBuildStateLines) {
+ log.Printf("getPlanResult - File %s: End line is greater than preBuildStateLines length: %d > %d\n", filePath, endLine, len(preBuildStateLines))
+ return nil, "", false, fmt.Errorf("end line is greater than preBuildStateLines length: %d > %d", endLine, len(preBuildStateLines))
+ }
+
+ if startLine < highestEndLine {
+ log.Printf("getPlanResult - File %s: Start line is less than highestEndLine: %d < %d\n", filePath, startLine, highestEndLine)
+
+ log.Printf("getPlanResult - File %s: streamedChange:\n", filePath)
+ log.Println(spew.Sdump(streamedChangesWithLineNums))
+
+ if params.OverlapStrategy == OverlapStrategyError {
+ return nil, "", false, fmt.Errorf("start line is less than highestEndLine: %d < %d", startLine,
+ highestEndLine)
+ } else {
+ continue
+ }
+ }
+
+ if endLine < highestEndLine {
+ if params.OverlapStrategy == OverlapStrategyError {
+ log.Printf("getPlanResult - File %s: End line is less than highestEndLine: %d < %d\n", filePath, endLine, highestEndLine)
+ return nil, "", false, fmt.Errorf("end line is less than highestEndLine: %d < %d", endLine, highestEndLine)
+ } else {
+ continue
+ }
+ }
+
+ if endLine > highestEndLine {
+ highestEndLine = endLine
+ }
+
+ if startLine == endLine {
+ old = preBuildStateLines[startLine-1]
+ } else {
+ old = strings.Join(preBuildStateLines[startLine-1:endLine], "\n")
+ }
+
+ replacement := &shared.Replacement{
+ Old: old,
+ New: new,
+ StreamedChange: streamedChange,
+ }
+
+ replacements = append(replacements, replacement)
+ }
+
+ log.Printf("getPlanResult - File %s: Will apply replacements\n", filePath)
+
+ updated, allSucceeded := shared.ApplyReplacements(preBuildState, replacements, true)
+
+ updated = shared.RemoveLineNums(updated)
+
+ for _, replacement := range replacements {
+ id := uuid.New().String()
+ replacement.Id = id
+ }
+
+ res := db.PlanFileResult{
+ TypeVersion: 1,
+ ReplaceWithLineNums: true,
+ OrgId: orgId,
+ PlanId: planId,
+ PlanBuildId: planBuildId,
+ ConvoMessageId: params.ConvoMessageId,
+ Content: "",
+ Path: filePath,
+ Replacements: replacements,
+ AnyFailed: !allSucceeded,
+ CanVerify: !params.IsOtherFix,
+ IsFix: params.IsFix,
+ IsSyntaxFix: params.IsSyntaxFix,
+ IsOtherFix: params.IsOtherFix,
+ FixEpoch: params.FixEpoch,
+ }
+
+ if params.CheckSyntax {
+ validationRes, err := syntax.Validate(ctx, filePath, updated)
+ if err != nil {
+ log.Printf("getPlanResult - File %s: Error validating syntax: %v\n", filePath, err)
+ return nil, "", false, fmt.Errorf("error validating syntax: %v", err)
+ }
+
+ res.WillCheckSyntax = validationRes.HasParser && !validationRes.TimedOut
+ res.SyntaxValid = validationRes.Valid
+ res.SyntaxErrors = validationRes.Errors
+ }
+
+ return &res, updated, allSucceeded, nil
+}
diff --git a/app/server/model/prompts/build.go b/app/server/model/prompts/build.go
index 31400af4..5dfbebd3 100644
--- a/app/server/model/prompts/build.go
+++ b/app/server/model/prompts/build.go
@@ -16,7 +16,27 @@ func GetBuildLineNumbersSysPrompt(filePath, preBuildState, changes string) strin
preBuildStateWithLineNums := shared.AddLineNums(preBuildState)
- return getListChangesLineNumsPrompt() + "\n\n" + getPreBuildStatePrompt(filePath, preBuildStateWithLineNums) + "\n\n" + getBuildPromptWithLineNums(changes)
+ return getListChangesLineNumsPrompt(false) + "\n\n" + getPreBuildStatePrompt(filePath, preBuildStateWithLineNums) + "\n\n" + getBuildPromptWithLineNums(changes)
+}
+
+func GetBuildLineNumbersSysPromptUpdated(filePath, preBuildState, changesFile, changesDesc, nonExpandedRefs string) string {
+ // hash := sha256.Sum256([]byte(currentState))
+ // sha := hex.EncodeToString(hash[:])
+
+ // log.Println("GetBuildLineNumbersSysPrompt currentState sha:", sha)
+
+ preBuildStateWithLineNums := shared.AddLineNums(preBuildState)
+ changesWithLineNums := shared.AddLineNumsWithPrefix(changesFile, "pdx-new-")
+
+ res := getListChangesLineNumsPrompt(nonExpandedRefs != "") + "\n\n" + getPreBuildStatePrompt(filePath, preBuildStateWithLineNums) + "\n\n" + "**Proposed updates:**\n" + changesDesc
+
+ if nonExpandedRefs == "" {
+ res += "\n```\n" + changesWithLineNums + "\n```"
+ } else {
+ res += "\n\nWithout expanded references:\n\n```\n" + nonExpandedRefs + "\n```" + "\n\n" + "With expanded references:\n\n```\n" + changesWithLineNums + "\n```"
+ }
+
+ return res
}
func GetBuildFixesLineNumbersSysPrompt(original, changes, updated, reasoning string) string {
@@ -67,84 +87,189 @@ const replacementIntro = `
You are an AI that analyzes a code file and an AI-generated plan to update the code file and produces a list of changes.
`
+const entireFilePrompt = `
+In the 'entireFileReasoning' key, evaluate whether the *proposed updates* contain the final state of the ENTIRE updated file *with all changes applied* and with nothing missing. Evaluate whether the *proposed updates* contain the file updated state of the entire file. Think carefully through the sections of the file and evaluate whether every section that should be included in the final result is present in the *proposed updates*. Evaluate whether the *original file* contains any code that is *NOT* inlcluded in the *proposed updates* AND *SHOULD* be included in the final result.
+
+Note that here you are *not* evaluating which sections have changed or not changed, but only whether the *proposed updates* contains the entire updated file.
+
+You are also NOT evaluating whether the *proposed updates* contains any new code that is not part of the *original file*. You are only evaluating whether ALL the code from both the *original file* and *proposed updates* that should be present in the final result when the two are combined is present in the *proposed updates*.
+
+The 'entireFile' key should be set to true if the *proposed updates* contain the final state of the ENTIRE updated file *with all changes applied* and with nothing missing. If the *proposed updates* do not contain the final state of the ENTIRE updated file *with all changes applied* and with nothing missing, set 'entireFile' to false.
+
+If 'entireFile' is set to true, 'changes' must be an empty array and 'problems' must be an empty string.
+`
+
const changesKeyPrompt = `
-'changes': An array of NON-OVERLAPPING changes. Each change is an object with properties: 'summary', 'hasChange', 'old', 'startLineIncludedReasoning', 'startLineIncluded', 'endLineIncludedReasoning', 'endLineIncluded', and 'new'.
+'changes': An array of changes. Each change is an object with properties: 'section', 'summary', 'newReasoning', 'reasoning', 'structureReasoning', 'closingSyntaxReasoning', 'orderReasoning', 'hasChange', 'insertBefore', 'insertAfter', 'old', 'new'
+
+If 'entireFile' is set to true, 'changes' must be an empty array.
+
+Note: all line numbers that are used below are prefixed with 'pdx-' when referencing lines in the *original file*, like this 'pdx-5: for i := 0; i < 10; i++ {'. This is to help you identify the line numbers in the *original file*. You *must* include the 'pdx-' prefix in the line numbers in the 'old' property.
+
+'insertBefore' is an object with properties: 'shouldInsertBefore' (boolean), 'line' (string).
+
+ If 'shouldInsertBefore' is true:
+ - 'line' must be *entire line* that *exactly match* a line from the *original file* including the line number with 'pdx-' prefix.
+ - The 'old' property of the change should be filled with empty strings.
+ - The 'new' key of the change should contain the content to be inserted before the specified line.
+
+'insertAfter' is an object with properties: 'shouldInsertAfter' (boolean), 'line' (string).
-Note: all line numbers that are used below are prefixed with 'pdx-', like this 'pdx-5: for i := 0; i < 10; i++ {'. This is to help you identify the line numbers in the file. You *must* include the 'pdx-' prefix in the line numbers in the 'old' property.
+ If 'shouldInsertAfter' is true:
+ - 'line' must be *entire line* that *exactly match* a line from the *original file* including the line number with 'pdx-' prefix.
+ - The 'old' property of the change should be filled with empty strings.
+ - The 'new' key of the change should contain the content to be inserted after the specified line.
+
+ If 'shouldInsertAfter' is false, then 'line' should be an empty string. 'old' and 'new' should be filled according to your other instructions.
+
+If 'insertBefore', or 'insertAfter' is set to true, the 'old' property of the change ABSOLUTELY MUST be filled by empty strings. Do NOT fill the 'old' property with any other values if 'insertBefore', or 'insertAfter' is set to true. The 'new' property should be filled according to your other instructions to the exact, correct section of the *proposed updates* that should be prepended, appended, inserted between, inserted before, or inserted after the specified lines.
+
+Expand the lines the 'new' property refers to in the *proposed updates* where needed to include line breaks and preserve formatting.
+
+Note: all line numbers that are used below are prefixed with 'pdx-' when referencing lines in the *original file*, like this 'pdx-5: for i := 0; i < 10; i++ {'. This is to help you identify the line numbers in the *original file*. You *must* include the 'pdx-' prefix in the line numbers in the 'old' property.
+
+Line numbers that are used below are prefixed with 'pdx-new-' when referencing lines in the *proposed updates*, like this 'pdx-new-5: for i := 0; i < 10; i++ {'. This is to help you identify the line numbers in the *proposed updates*. You *must* include the 'pdx-new-' prefix in the line numbers in the 'new' property.
`
const summaryChangePrompt = `
-The 'summary' property is a brief summary of the change. At the end of the summary, consider if this change will overlap with any ensuing changes. If it will, include those changes in *this* change instead. Continue the summary and includes those ensuing changes that would otherwise overlap. Changes that remove code are especially likely to overlap with ensuing changes.
+The 'section' property is the name of the section from the 'originalSections' array that the change applies to. Each change must apply to a single section.
+
+The 'summary' property is a brief summary of the change.
'summary' examples:
- 'Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar.'
- 'Update the Org model to include StripeCustomerId and StripeSubscriptionId fields.'
- 'Add function ExecQuery to execute a query.'
-
-'summary' that is larger to avoid overlap:
- - 'Insert function ExecQuery after GetResults function in loop body. Update loop that aggregates the results to iterate 10 times instead of 5 and log the value of someVar. Add function ExecQuery to execute a query.'
+
+The 'newReasoning' property evaluates what needs to be included in the 'new' section to ensure that *ALL* the code from the *proposed changes* are correctly applied to the resulting file. Carefully consider whether the code from the *proposed updates* contain code structures that need to be properly closed in the resulting file. For example, "the <Route> tag needs to be closed properly in the resulting file, so the 'new' section should end on the closing tag at pdx-new-10: </Route>".
+
+The 'structureReasoning' property list the code structures that are involved in the change. Explain what kind of change should be used to ensure that the code structures are closed properly. For example, if a new method should be added to the end of a class, explain that the change should use the 'insertBefore' property in order to ensure that the method is added *before* the closing brace of the class so that the method stays inside the class and both the method and the class are closed properly.
+
+If you are adding new code to the *end* of an existing code structure (and not replacing any existing code), you must use the 'insertBefore' property to add the new code before the closing symbol of the existing code structure.
+
+If you are adding new code to the *beginning* of an existing code structure (and not replacing any existing code), you must use the 'insertAfter' property to add the new code after the opening symbol of the existing code structure.
+
+If you are replacing existing code, you must always use the 'old' property to specify the code that should be replaced.
+
+The 'closingSyntaxReasoning' property, for each code structure related to the change, list what is the BOTTOM-MOST, RIGHT-MOST, *FINAL* closing symbol after which *ALL* code structures in the section are closed.
+
+For example, if there is a section like:
+
+---
+pdx-new-32: <Route
+pdx-new-33: path="/instructor-grid"
+pdx-new-33: element={loggedIn ? <InstructorGridPage /> : <Navigate to=\"/\" />} />
+pdx-new-34: />
+--
+
+The 'closingSyntaxReasoning' property should explain that the final closing symbol is /> on line pdx-new-34. Even though the 'InstructorGridPage' and 'Navigate' code structures are closed properly on pdx-new-33, it is NOT the final closing symbol because the 'Route' code structure is not closed. The 'closingSyntaxReasoning' property in this example would explain that the final closing symbol that closes all code structures in the section is /> on line pdx-new-34.
+
+The 'closingSyntaxReasoning' section ABSOLUTELY MUST include a mention of each section that is specified in the 'structureReasoning' property. If the 'insertAfter' property is used, the 'closingSyntaxReasoning' section MUST include a mention of the 'insertAfter' section. And the 'closingSyntaxReasoning' section MUST ALWAYS include a mention of the 'new' section.
+
+The same applies to the 'line' property of the 'insertAfter' section. For example, if in the *original file* there is a section like:
+
+---
+pdx-20: <Route
+pdx-21: path="/instructor-grid"
+pdx-22: element={loggedIn ? <InstructorGridPage /> : <Navigate to=\"/\" />} />
+pdx-23: />
+--
+
+For example, if the plan is to insert code from the *proposed updates* after the 'Route' code structure using the 'insertAfter' property, then 'closingSyntaxReasoning' property should explain that the final closing symbol is /> on line pdx-23. Even though the 'InstructorGridPage' and 'Navigate' code structures are closed properly on pdx-22, it is NOT the final closing symbol because the 'Route' code structure is not closed. The 'closingSyntaxReasoning' property in this example would explain that the final closing symbol that closes all code structures in the section is /> on line pdx-23.
+
+'closingSyntaxReasoning' must use this template: 'Old: [syntax reasoning for old section] | New: [syntax reasoning for new section]'.
+
+The 'orderReasoning' property evaluates how the change should be applied in order to preserve the order of code from the *proposed updates*. State at what position the new code would need to be inserted (or to overwrite) in order to preserve the order of code from the *proposed updates*. For example, "To ensure the order is preserved, it's important that the new import of 'fmt' ('pdx-new-3: import "fmt"') should be added after the import of 'os' ('pdx-1: import "os"') and before the import of 'log' ('pdx-2: import "log"')." Do not state in this key what should be the start and end lines of the change, either in the *original file* or the *proposed updates*. Instead, make note of which invariants need to be preserved in terms of the order of the code in order for the *proposed updates* to be applied correctly.
The 'hasChange' property is a boolean that indicates whether there is anything to change. If there is nothing to change, set 'hasChange' to false. If there is something to change, set 'hasChange' to true.
+
+The 'reasoning' property is an explanation of how the change relates to the strategy outlined in the 'problems' key and in the 'orginalSections' array from above. Which 'originalSections' does this change apply to?
+
+If this is a partial change , where should the change begin and end in the *original file* in order to avoid removing or overwriting code that should not be removed? Include specific line numbers prefixed with 'pdx-'. This explanation MUST be include in the 'reasoning' key for partial changes, and the explanation must begin with "To avoid removing". Example: "To avoid removing any existing imports, the change should be begin on line pdx-5 to avoid removing the existing imports."
+
+You must then consider what the first and last line of the 'new' section should be. Should the 'new' section start on a function signature and end on a closing bracket? Start on a variable declaration and end on a newline? Start on an opening tag and end on a closing tag? What needs to be included in the 'new' section to ensure the code will be syntactically correct and the plan will be applied as intended? Answer these questions.
+
+You MUST include closing symbols in the 'new' section such that EVERY code structure in the *proposed updates* is closed properly. ALWAYS expand the 'new' section to include closing syntax that is related to the code structures present in the 'new' section.
+
+Next, state your reasoning in the format "Because [reason(s)] the last line of the new section should be the [line of code explained in words] on [line number in the *proposed updates* prefixed with 'pdx-new-']. And because [reason(s)], the first line of the new section should be the [line of code explained in words] on [line number in the *proposed updates* prefixed with 'pdx-new-'].". The 'reasoning' key ABSOLUTELY MUST include your stated reasoning in the afore-stated format. You must never leave it out.
+
+If the next non-newline character after the change in the *original file* is a *closing symbol* for a code structure like a closing bracket, parenthesis, quote, brace, tag, etc., explicitly list each closing symbol and state whether it closes a code structure that is present in the 'old' section. If it does, state that the 'old' section must now be expaned to include this closing symbol, and state what the new last line of the 'old' section should be, including the line number prefixed with 'pdx-'.
+
+If the next non-newline character after the change in the *proposed updates* is an *closing symbol* for a code structure like a closing bracket, parenthesis, quote, brace, tag, etc., explicitly list each closing symbol and state whether it closes a code structure that is present in the 'new' section. If it does, state that the 'new' section must now be expanded to include this closing symbol, and state what the new last line of the 'new' section should be, including the line number prefixed with 'pdx-new-'.
+
+You must not try to make the reasoning in any reasoning key too succinct. It needs to properly address these issues in order for the change to be correctly applied in complex scenarios. Make sure you have fully evaluated the considerations and consequences of how this change will be applied. Be thorough, meticulous, and perfectionist. Use the space needed to fully explain the reasoning and spell out in detail all the factors that need to be considered, even if the reasoning seems obvious.
`
const lineNumsOldPrompt = `
-The 'old' property is an object with 3 properties: 'entireFile', 'startLineString' and 'endLineString'.
-
- 'entireFile' is a boolean that indicates whether the **entire file** is being replaced. If 'entireFile' is true, 'startLineString' and 'endLineString' must be empty strings. If 'entireFile' is false, 'startLineString' and 'endLineString' must be valid strings that exactly match lines from the original file. If 'entireFile' is false, 'startLineString' and 'endLineString' MUST NEVER be empty strings.
+The 'old' property is an object with 2 properties: 'startLineString' and 'endLineString'.
- 'startLineString' is the **entire, exact line** where the section to be replaced begins in the original file, including the line number. Unless it's the first change, 'startLineString' ABSOLUTELY MUST begin with a line number that is HIGHER than both the 'endLineString' of the previous change and the 'startLineString' of the previous change. **The line number and line MUST EXACTLY MATCH a line from the original file.**
+ 'startLineString' is the **entire, exact line** where the section to be replaced begins in the *original file*, including the line number. Unless it's the first change, 'startLineString' ABSOLUTELY MUST begin with a line number that is HIGHER than both the 'endLineString' of the previous change and the 'startLineString' of the previous change. **The line number and line MUST EXACTLY MATCH a line from the *original file*.** It must be only a *SINGLE LINE* and not contain any line breaks.
- If the previous change's 'endLineString' starts with 'pdx-75: ', then the current change's 'startLineString' MUST start with 'pdx-76: ' or higher. It MUST NOT be 'pdx-75: ' or lower. If the previous change's 'startLineString' starts with 'pdx-88: ' and the previous change's 'endLineString' is an empty string, then the current change's 'startLineString' MUST start with 'pdx-89: ' or higher. If the previous change's 'startLineString' starts with 'pdx-100: ' and the previous change's 'endLineString' starts with 'pdx-105: ', then the current change's 'startLineString' MUST start with 'pdx-106: ' or higher.
+ If the previous change's 'old' property 'endLineString' starts with 'pdx-75: ', then the current change's 'startLineString' MUST start with 'pdx-76: ' or higher. It MUST NOT be 'pdx-75: ' or lower. If the previous change's 'old' property 'startLineString' starts with 'pdx-88: ' and the previous change's 'old' property 'endLineString' is an empty string, then the current change's 'startLineString' MUST start with 'pdx-89: ' or higher. If the previous change's 'old' property 'startLineString' starts with 'pdx-100: ' and the previous change's 'old' property 'endLineString' starts with 'pdx-105: ', then the current change's 'startLineString' MUST start with 'pdx-106: ' or higher.
- 'endLineString' is the **entire, exact line** where the section to be replaced ends in the original file. Pay careful attention to spaces and indentation. 'startLineString' and 'endLineString' must be *entire lines* and *not partial lines*. Even if a line is very long, you must include the entire line, including the line number and all text on the line. **The line number and line MUST EXACTLY MATCH a line from the original file.**
+ 'endLineString' is the **entire, exact line** where the section to be replaced ends in the *original file*. Pay careful attention to spaces and indentation. 'startLineString' and 'endLineString' must be *entire lines* and *not partial lines*. Even if a line is very long, you must include the entire line, including the line number and all text on the line. **The line number and line MUST EXACTLY MATCH a line from the *original file*.** It must be only a *SINGLE LINE* and not contain any line breaks.
**For a single line replacement, 'endLineString' MUST be an empty string.**
- 'endLineString' MUST ALWAYS come *after* 'startLineString' in the original file. It must start with a line number that is HIGHER than the 'startLineString' line number. If 'startLineString' starts with 'pdx-22: ', then 'endLineString' MUST either be an empty string (for a single line replacement) or start with 'pdx-23: ' or higher (for a multi-line replacement).
+ If the 'reasoning' key above includes guidance on where to start and/or end the change, use that guidance to determine the 'startLineString' and 'endLineString'. If the reasoning key states that the change should begin at a specific line number to avoid removing or overwriting code that should not be removed, ensure that 'startLineString' starts with that line number. If the reasoning key states that the change should end at a specific line number to avoid removing or overwriting code that should not be removed, ensure that 'endLineString' ends with that line number. You MUST follow the guidance in the 'reasoning' key.
- If 'hasChange' is false, both 'startLineString' and 'endLineString' must be empty strings. If 'hasChange' is true, 'startLineString' and 'endLineString' must be valid strings that exactly match lines from the original file. If 'hasChange' is true, 'startLineString' and 'endLineString' MUST NEVER be empty strings.
+ 'endLineString' MUST ALWAYS come *after* 'startLineString' in the *original file*. It must start with a line number that is HIGHER than the 'startLineString' line number. If 'startLineString' starts with 'pdx-22: ', then 'endLineString' MUST either be an empty string (for a single line replacement) or start with 'pdx-23: ' or higher (for a multi-line replacement).
- If you are replacing the entire file, 'startLineString' MUST be the first line of the original file and 'endLineString' MUST be the last line of the original file.
+ If 'hasChange' is false both 'startLineString' and 'endLineString' must be empty strings. If 'hasChange' is true, 'startLineString' and 'endLineString' must be valid strings that exactly match lines from the *original file*. If 'hasChange' is true, 'startLineString' and 'endLineString' MUST NEVER be empty strings.
`
const changeLineInclusionAndNewPrompt = `
-The 'startLineIncludedReasoning' property is a string that very briefly explains whether 'startLineString' should be included in the 'new' property. For example, if the 'startLineString' is the closing bracket of a function and you are adding another function after it, you *MUST* include the 'startLineString' in the 'new' property, or the previous function will lose its closing bracket when the change is applied. Similarly, if the 'startLineString' is a function definition and you are updating the body of the function, you *MUST* also include 'startLineString' so that they function definition is not removed. The only time 'startLineString' should not be included in 'new' is if it is a line that should be removed or replaced. Generalize the above to all types of code blocks, changes, and syntax to ensure the 'new' property will not remove or overwrite code that should not be removed or overwritten. That also includes newlines, line breaks, and indentation.
+The 'new' property is an object with 2 properties: 'startLineString' and 'endLineString'.
-'startLineIncluded' is a boolean that indicates whether 'startLineString' should be included in the 'new' property. If 'startLineIncluded' is true, 'startLineString' MUST be included in the 'new' property. If 'startLineIncluded' is false, 'startLineString' MUST not be included in the 'new' property.
-
-The 'endLineIncludedReasoning' property is a string that very briefly explains whether 'endLineString' should be included in the 'new' property. For example, if the 'endLineString' is the opening bracket of a function and you are adding another function before it, you *MUST* include the 'endLineString' in the 'new' property, or the subsequent function will lose its opening bracket when the change is applied. Similarly, if the 'endLineString' is the closing bracket of a function and you are updating the body of the function, you *MUST* also include 'endLineString' so that the closing bracket not removed. The only time 'endLineString' should not be included in 'new' is if it is a line that should be removed or replaced. Generalize the above to all types of code blocks, changes, and syntax to ensure the 'new' property will not remove or overwrite code that should not be removed or overwritten. That also includes newlines, line breaks, and indentation.
+ 'startLineString' is the **entire, exact line** where the updated code that the 'old' section will be replaced with begins *in the *proposed updates**, including the line number. Unless it's the first change, 'startLineString' ABSOLUTELY MUST begin with a line number that is HIGHER than both the 'new' property 'endLineString' of the previous change and the 'startLineString' of the previous change. **The line number and line MUST EXACTLY MATCH a line from the *proposed updates*.** It must be only a *SINGLE LINE* and not contain any line breaks. IT ABSOLUTELY MUST match a line from the **proposed updates**.
+
+ If the previous change's 'new' property 'endLineString' starts with 'pdx-new-75: ', then the current change's 'startLineString' MUST start with 'pdx-new-76: ' or higher. It MUST NOT be 'pdx-new-75: ' or lower. If the previous change's 'new' property 'startLineString' starts with 'pdx-new-88: ' and the previous change's 'new' property 'endLineString' is an empty string, then the current change's 'startLineString' MUST start with 'pdx-new-89: ' or higher. If the previous change's 'new' property 'startLineString' starts with 'pdx-new-100: ' and the previous change's 'new' property 'endLineString' starts with 'pdx-new-105: ', then the current change's 'startLineString' MUST start with 'pdx-new-106: ' or higher.
+
+ 'endLineString' is the **entire, exact line** where the section to be replaced ends in the *proposed updates*. Pay careful attention to spaces and indentation. 'startLineString' and 'endLineString' must be *entire lines* and *not partial lines*. Even if a line is very long, you must include the entire line, including the line number and all text on the line. **The line number and line MUST EXACTLY MATCH a line from the *proposed updates*.** It must be only a *SINGLE LINE* and not contain any line breaks. IT ABSOLUTELY MUST match a line from the **proposed updates**. For multi-line changes, the 'endLineString' of the 'new' section MUST NOT be empty.
+
+ **For a single line replacement, 'endLineString' MUST be an empty string.**
-'endLineIncluded' is a boolean that indicates whether 'endLineString' should be included in the 'new' property. If 'endLineIncluded' is true, 'endLineString' MUST be included in the 'new' property. If 'endLineIncluded' is false, 'endLineString' MUST not be included in the 'new' property.
+ 'endLineString' MUST ALWAYS come *after* 'startLineString' in the *proposed updates*. It must start with a line number that is HIGHER than the 'startLineString' line number. If 'startLineString' starts with 'pdx-new-22: ', then 'endLineString' MUST either be an empty string (for a single line replacement) or start with 'pdx-new-23: ' or higher (for a multi-line replacement).
-The 'new' property is a string that represents the new code that will replace the old code. The new code must be valid and consistent with the intention of the plan. If the proposed update is to remove code, the 'new' property should be an empty string. Be precise about newlines, line breaks, and indentation. 'new' must include only full lines of code and *no partial lines*. Do NOT include line numbers in the 'new' property.
+ If 'hasChange' is false, both 'startLineString' and 'endLineString' must be empty strings. If 'hasChange' is true, 'startLineString' and 'endLineString' must be valid strings that exactly match lines from the *proposed updates*. If 'hasChange' is true, 'startLineString' and 'endLineString' MUST NEVER be empty strings.
-If the proposed update includes references to the original code in comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or **any other reference to the original code,** you *MUST* ensure that the comment making the reference is *NOT* included in the 'new' property. Instead, include the **exact code** from the original file that the comment is referencing. Do not be overly strict in identifying references. If there is a comment that seems like it could plausibly be a reference and there is code in the original file that could plausibly be the code being referenced, then treat that as a reference and handle it accordingly by including the code from the original file in the 'new' property instead of the comment. YOU MUST NOT MISS ANY REFERENCES.
+If the 'hasChange' property is false, the 'new' property must be an empty string. If the 'hasChange' property is true, the 'new' property must be a valid string.
-If the 'startLineIncluded' property is true, the 'startLineString' MUST be the first line of 'new'. If the 'startLineIncluded' property is false, the 'startLineString' MUST NOT be included in 'new'. If the 'endLineIncluded' property is true, the 'endLineString' MUST be the last line of 'new'. If the 'endLineIncluded' property is false, the 'endLineString' MUST NOT be included in 'new'.
+Expand the lines the 'new' property refers to in the *proposed updates* where needed to include line breaks and preserve formatting.
-If the 'hasChange' property is false, the 'new' property must be an empty string. If the 'hasChange' property is true, the 'new' property must be a valid string.
+'includeOldReasoning' is a string. Evaluate whether the section of code from the *original file* that the old property refers to should be included in the final result.
-If *any* change has the 'entireFile' key in the 'old' property set to true, the corresponding 'new' key MUST be the entire updated file, and there MUST only be a single change in the 'changes' array.
-`
+If the code from the 'new' section should fully replace the code from the 'old' property, say "Fully replace" and stop.
-const lineNumsRulesPrompt = `
-You ABSOLUTELY MUST NOT generate overlapping changes. Group smaller changes together into larger changes where necessary to avoid overlap. Only generate multiple changes when you are ABSOLUTELY CERTAIN that they do not overlap--otherwise group them together into a single change. If changes are close to each other (within several lines), group them together into a single change. You MUST group changes together and make fewer, larger changes rather than many small changes, unless the changes are completely independent of each other and not close to each other in the file. You MUST NEVER generate changes that are adjacent or close to adjacent. Adjacent or closely adjacent changes MUST ALWAYS be grouped into a single larger change.
+If the 'old' section spans multiple lines of the *original file*, say "Multiple lins, don't include." and stop.
-Furthermore, unless doing so would require a very large change because some changes are far apart in the file, it's ideal to call the 'listChangesWithLineNums' with just a SINGLE change.
+Otherwise, if the code from the 'old' property is a *single line* and it's not in the *proposed updates*, state this. In that case, proceed to evaluate whether the code from the old property makes more sense at the beginning or end of the change from the new property. State which makes more sense and why. For example, "The closing </Routes> tag should be included in the final result. It should be added *after* the 'new' code from the *proposed updates*.
-Changes must be ordered in the array according to the order they appear in the file. The 'startLineString' of each 'old' property must come after the 'endLineString' of the previous 'old' property. Changes MUST NOT overlap. If a change is dependent on another change or intersects with it, group those changes together into a single change.
+'appendOld' must be true if 'includeOldReasoning' determined that the code from the old property should be included in the final result and that it makes the most sense to include it at the end of the change from the new property.
-You MUST NOT repeat changes to the same block of lines multiple teams. You MUST NOT duplicate changes. It is extremely important that a given change is only applied *once*.
+'prependOld' must be true if 'includeOldReasoning' determined that the code from the old property should be included in the final result and that it makes the most sense to include it at the beginning of the change from the new property.
`
const changeRulesPrompt = `
-Apply changes intelligently **in order** to avoid syntax errors, breaking code, or removing code from the original file that should not be removed. Consider the reason behind the update and make sure the result is consistent with the intention of the plan.
+Apply changes intelligently **in order** to avoid syntax errors, breaking code, or removing code from the *original file* that should not be removed. Consider the reason behind the update and make sure the result is consistent with the intention of the plan.
+
+Changes MUST be ordered based on their position in the *original file*. ALWAYS go from top to bottom IN ORDER when generating changes.
+
+When generating changes, you MUST consider the list of sections from the 'originalSections' array. If a section is marked as 'shouldChange', there MUST be at least one corresponding change in the 'changes' array that addresses that section.
+
+You ABSOLUTELY MUST NOT overwrite or delete code from the *original file* unless the plan *clearly intends* for the code to be overwritten or removed. Do NOT replace a full section of code with only new code unless that is the clear intention of the plan. Instead, merge the original code and the *proposed updates* together intelligently according to the intention of the plan.
+
+Pay *EXTREMELY close attention* to opening and closing brackets, parentheses, and braces. Never leave them unbalanced when the changes are applied. Also pay *EXTREMELY close attention* to newlines and indentation. Make sure that the indentation of the new code is consistent with the indentation of the original code, and syntactically correct. Do NOT remove extraneous newlines from the *original file* unless it is explicitly mentioned in the *proposed updates*.
+
+You ABSOLUTELY MUST NOT generate overlapping changes.
+
+Changes must be ordered in the array according to the order they appear in the *original file*. The 'startLineString' of each 'old' property ABSOLUTELY MUST come after the 'endLineString' of the previous 'old' property. If the 'problems' key includes guidance on the order that changes should be applied, you MUST follow that guidance and generate the changes in the order specified.
-Changes MUST be ordered based on their position in the original file. ALWAYS go from top to bottom IN ORDER when generating replacements. DO NOT EVER GENERATE AN OVERLAPPING CHANGE. If a change would fall within OR overlap a prior change in the list, SKIP that change and move on to the next one.
+You MUST NOT repeat changes to the same block of lines multiple teams. You MUST NOT duplicate changes. It is extremely important that a given change is only applied *once*.
-You ABSOLUTELY MUST NOT overwrite or delete code from the original file unless the plan *clearly intends* for the code to be overwritten or removed. Do NOT replace a full section of code with only new code unless that is the clear intention of the plan. Instead, merge the original code and the proposed updates together intelligently according to the intention of the plan.
+ALL THE CODE in the *proposed updates* must be accounted for in the 'changes' array. You MUST NOT leave out any new code, changes, or deletions that are introduced in the *proposed updates*.
-Pay *EXTREMELY close attention* to opening and closing brackets, parentheses, and braces. Never leave them unbalanced when the changes are applied. Also pay *EXTREMELY close attention* to newlines and indentation. Make sure that the indentation of the new code is consistent with the indentation of the original code, and syntactically correct.
+Break up changes when they apply to different sections of the file, as listed in the 'originalSections' array.
`
const lineNumsJsonPrompt = `
@@ -152,48 +277,99 @@ The 'listChangesWithLineNums' function MUST be called *valid JSON*. Double quote
`
const commentsPrompt = `
-The 'comments' key is an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." or "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.
+The 'comments' key is an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." or "// ... existing code ..." or "// ..." or other comments which reference code from the *original file*. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.
-In 'comments', you must list EVERY comment included in the proposed updates. Only list *code comments* that are valid comments for the programming language being used. Do not list logging statements or any other non-comment text that is not a valid code comment. If there are no code comments in the proposed updates, 'comments' must be an empty array.
+In 'comments', you must list EVERY comment included in the *proposed updates*. Only list *code comments* that are valid comments for the programming language being used. Do not list logging statements or any other non-comment text that is not a valid code comment. If there are no code comments in the *proposed updates*, 'comments' must be an empty array.
-If there are multiple identical comments in the proposed updates, you MUST list them *all* in the 'comments' array--list each identical comment as a separate object in the array.
+If there are multiple identical comments in the *proposed updates*, you MUST list them *all* in the 'comments' array--list each identical comment as a separate object in the array.
`
const problemsStrategyPrompt = `
-In the 'problems' key, you MUST explain how you will strategically generate changes in order to avoid any problems in the updated file. You should explain which changes you will make and how you will *avoid* making any overlapping or invalid changes. Consider whether any changes are close together or whether any change is potentially contained by another. If so, group those changes together into a single change.
+In the 'problems' key, you MUST explain how you will strategically generate changes in order to avoid any problems in the updated file.
+
+If 'entireFile' is set to true, 'problems' must be an empty string.
+
+You must consider whether you will apply partial changes, prepend or append to the file, or insert before or after specific lines. Choose whichever is the simplest, most effective, and least error-prone method of making your changes. Before choosing, evaluate the consequences of each option and choose the one that will cause the fewest errors and the easiest changes.
-You must consider whether you will apply partial changes or replace the entire file. If the original file is long, you MUST NOT replace the entire file with a single change. Instead, you should apply changes to specific sections of the file. If the original file is short and the changes are complex, you may consider replacing the entire file with a single change.
+You must consider how you will avoid *incorrectly removing or overwriting code* from the *original file*. Explain whether any code from the *original file* needs to be merged with the *proposed updates* in order to avoid removing or overwriting code that should not be removed.
-You must consider how you will avoid *incorrectly removing or overwriting code* from the original file. Explain whether any code from the original file needs to be merged with the proposed updates in order to avoid removing or overwriting code that should not be removed. It is ABSOLUTELY CRITICAL that no pre-existing code or functionality is removed or overwritten unless the plan explicitly intends for it to be removed or overwritten. New code and functionality introduced in the proposed updates MUST be *merged* with existing code and functionality in the original file. Explain how you will achieve this.
+It is ABSOLUTELY CRITICAL that no pre-existing code or functionality is removed or overwritten unless the plan explicitly intends for it to be removed or overwritten. New code and functionality introduced in the *proposed updates* MUST be *merged* with existing code and functionality in the *original file*. Explain how you will achieve this.
-You must consider how you will avoid including any references in the updated file if any are present in the proposed updates.
+You must consider how you will *avoid incorrect duplication* in making your changes. For example if a 'main' function is present in the *original file* and the *proposed updates* include update code for the 'main' function, you must ensure the changes are applied within the existing 'main' function rather than incorrectly adding a duplicate 'main' function.
-You must consider how you will *avoid incorrect duplication* in making your changes. For example if a 'main' function is present in the original file and the proposed updates include update code for the 'main' function, you must ensure the changes are applied within the existing 'main' function rather than incorrectly adding a duplicate 'main' function.
+If the *proposed updates* include large sections that are identical to the *original file*, consider whether the changes can be made more minimal in order to only replace sections of code that are *changing*. If you are making the changes more minimal and specific, explain how you will do this.
-If the proposed updates include large sections that are identical to the original file, consider whether the changes can be made more minimal in order to only replace sections of code that are *changing*. If you are making the changes more minimal and specific, explain how you will do this without generating any overlapping changes or introducing any new problems.
+You MUST also explicitly state the *order* that all the changes you've listed will be applied. Changes MUST be applied in the order they appear in the *original file*. ALWAYS go from top to bottom in order when making your changes.
`
-func getListChangesLineNumsPrompt() string {
+const originalSectionsPrompt = `
+The 'originalSections' key is an array of objects with four properties: 'description', 'reasoning', 'sectionStartLine', 'sectionEndLine', 'shouldChange', and 'shouldRemove'. In the 'originalSections' key you must logically divide the *original file* into sections based on functionality, logic, code structure, and general organization.
+
+You must list every section that exists in the *original file*. When large sections of the *original file* are not changing, combine them into a single section. Only include sections from the *original file*. Do NOT include sections from the *proposed updates*.
+
+Don't make the sections overly small and granular unless there is a clear semantic reason to do so. For example, if there are many small functions in a file, don't create a section for each section. Sections should be larger than that and instead reflect the general structure of a file, rather than being a long list of every top-level code structure.
+
+'description' is a brief summary of the section and its purpose.
+
+'reasoning' is a brief evaluation of how this section relates to the *proposed updates*, and whether all or any part of it will be changed, removed, or preserved as is. If only part of the section should be changed, explain which part(s) will change and which will remain the same, and further explain how you will avoid incorrectly removing or overwriting code that should not be removed.
+
+Anytime there is a partial change to a section, it is CRITICAL that you explain BOTH which parts of the section should be *changed* and which parts will be *preserved* and how you will ensure this when generating changes below. You MUST include this explanation in the 'reasoning' key when there's a partial change. It must begin with "To avoid removing". If new code is being added, note *where* in the section it will be added, and include line numbers prefixed with 'pdx-'. For example, "To avoid removing any existing imports, the new imports will be added after the existing imports, which end on pdx-5." or "To avoid removing any existing init code, the new code for checking the GOENV environment variable will be added before the existing init code, which starts on pdx-10."
+
+'sectionStartLine' is the line number, prefixed with 'pdx-', where the section begins in the *original file*. Include only the line number, not the line itself.
+
+'sectionEndLine' is the line number, prefixed with 'pdx-', where the section ends in the *original file*. Include only the line number, not the line itself.
+
+'shouldChange' is a boolean that indicates whether the section should be changed.
+
+'shouldRemove' is a boolean that indicates whether the section should be removed.
- return replacementIntro + `
+A section should only be changed or removed if the *proposed updates* clearly intend for it to be changed or removed.
+`
+
+func getListChangesLineNumsPrompt(expandedRefs bool) string {
+ res := replacementIntro + `
[YOUR INSTRUCTIONS]
- Call the 'listChangesWithLineNums' function with a valid JSON object that includes the 'comments', 'problems', and 'changes' keys.
+ Call the 'listChangesWithLineNums' function with a valid JSON object that includes the 'originalSections', 'entireFileReasoning', 'entireFile', 'problems', and 'changes' keys.
- ` + lineNumsRulesPrompt + `
+ ` + originalSectionsPrompt + `
- ` + commentsPrompt + `
-
- ` + problemsStrategyPrompt + `
+ ` + entireFilePrompt + `
+ ` + problemsStrategyPrompt + `
+
` + changesKeyPrompt + `
` + summaryChangePrompt + `
` + lineNumsOldPrompt + `
- ` + changeLineInclusionAndNewPrompt + `
+ ` + changeLineInclusionAndNewPrompt
+
+ if expandedRefs {
+ res += `
+ While the code changes *without* expanded references have been included to help you understand the scope of the change, the 'new' key of each change object *ABSOLUTELY MUST ALWAYS* refer to the *proposed updates* *with* expanded references and pdx-new- prefixed line numbers. When the *proposed updates* are mentioned above, this refers to the *proposed updates* *with* expanded references and pdx-new- prefixed line numbers.
+
+ Use the changes *without* expanded references to understand specifically which code is changing and what the *order* of the code structures in the final updated file should be. It's extremely important that the code is ordered exactly as it is intended in the *proposed updates*.
+
+ If the changes *without* expanded references look like this:
+
+ ---
+ class MyClass {
+ // ... existing code ...
+
+ someNewMethod() {
+ doStuff();
+ }
+ }
+ ---
+
+ Then the newly added 'someNewMethod' method MUST go at the *end* of the 'MyClass' class.
+ `
+ }
+
+ res += `
Example change object:
---
@@ -203,7 +379,10 @@ func getListChangesLineNumsPrompt() string {
startLineString: "pdx-5: for i := 0; i < 10; i++ { ",
endLineString: "pdx-7: }",
},
- new: "for i := 0; i < 10; i++ {\n execQuery()\n }\n }\n}",
+ new: {
+ startLineString: "pdx-new-5: for i := 0; i < 10; i++ { ",
+ endLineString: "pdx-new-7: }",
+ },
}
---
@@ -215,11 +394,13 @@ func getListChangesLineNumsPrompt() string {
[END YOUR INSTRUCTIONS]
`
+
+ return res
}
func getFixChangesLineNumsPrompt() string {
return `
- You are an AI that analyzes an original file (if present), an incorrectly updated file, the changes that were proposed, and a description of the problems with the file, and then produces a list of changes to apply to the *incorrectly updated file* that will fix *ALL* the problems.
+ You are an AI that analyzes an *original file* (if present), an incorrectly updated file, the changes that were proposed, and a description of the problems with the file, and then produces a list of changes to apply to the *incorrectly updated file* that will fix *ALL* the problems.
Problems you MUST fix include:
- Syntax errors, including unbalanced brackets, parentheses, braces, quotes, indentation, and other code structure errors
@@ -231,7 +412,7 @@ func getFixChangesLineNumsPrompt() string {
- Incorrectly duplicated code
- Incorrectly applied comments that reference the original code
- If the updated file includes references to the original code in comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods..." **any other reference to the original code, the file is incorrect. References like these must be handled by including the exact code from the original file that the comment is referencing.
+ If the updated file includes references to the original code in comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods..." **any other reference to the original code, the file is incorrect. References like these must be handled by including the exact code from the *original file* that the comment is referencing.
[YOUR INSTRUCTIONS]
Call the 'listChangesWithLineNums' function with a valid JSON object that includes the 'comments','problems' and 'changes' keys.
@@ -268,8 +449,6 @@ func getFixChangesLineNumsPrompt() string {
}
---
- ` + lineNumsRulesPrompt + `
-
` + changeRulesPrompt + `
` + lineNumsJsonPrompt + `
@@ -362,17 +541,17 @@ var ListReplacementsFn = openai.FunctionDefinition{
func GetVerifyPrompt(preBuildState, updated, changes, diff string) string {
s := `
-Based on an original file (if one exists), an AI-generated plan, an updated file, and a diff between the original and updated file, determine whether the updated file's syntax is correct and whether the proposed updates were applied correctly to the updated file.
+Based on an *original file* (if one exists), an AI-generated plan, an updated file, and a diff between the original and updated file, determine whether the updated file's syntax is correct and whether the *proposed updates* were applied correctly to the updated file.
You must consider whether any of the following problems are present in the updated file:
- Syntax errors, including unbalanced brackets, parentheses, braces, quotes, indentation, and other code structure errors
- Missing or incorrectly scoped declarations
- Any other errors that make the code invalid and would prevent it from being run as-is for the programming language being used
-- Code from the original file was incorrectly removed or overwritten.
+- Code from the *original file* was incorrectly removed or overwritten.
- Code was incorrectly duplicated. For example, if a file should have a single main function, but instead of updating the existing main function, the updated file includes multiple main functions, then the file is incorrect. The same applies to any other functions or elements that should not be duplicated.
-- Incorrectly included comments that reference the original code.. If the updated file includes comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods...", "// Existing code..." **any other reference to the original code**, the file is incorrect. References like these must be handled by including the exact code from the original file that the comment is referencing.
+- Incorrectly included comments that reference the original code.. If the updated file includes comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods...", "// Existing code..." **any other reference to the original code**, the file is incorrect. References like these must be handled by including the exact code from the *original file* that the comment is referencing.
-If there is no original file, it means that a new file was created from scratch based on the AI-generated plan. In this case, the syntax in the new file must be valid and consistent with the intention of the plan. You must ensure there are no syntax errors or other clear mistakes in the new file.
+If there is no *original file*, it means that a new file was created from scratch based on the AI-generated plan. In this case, the syntax in the new file must be valid and consistent with the intention of the plan. You must ensure there are no syntax errors or other clear mistakes in the new file.
Call the 'verifyOutput' function with a valid JSON object that include the following keys:
@@ -382,7 +561,7 @@ Call the 'verifyOutput' function with a valid JSON object that include the follo
'removed': an array of objects with three properties: 'code', 'reasoning', and 'correct'.
- 'code' is a string. It shows the section of code that was removed or overwritten in the updated file. This can be abbreviated by detailing how the section starts and end and describing the purpose of the code. If the section is longer than a few lines, rather than reproducding a long section of code verbatim, provide a summary of the code that was removed or overwritten, as well as the exact code which starts and ends the section. The summary and start/end code should be details enough to disambiguate the section of code that was removed or overwritten from any other similar sections of code in the file.
- - 'reasoning' is a string that explains whether, based on the proposed changes, this section was deliberately removed consistent with the intention of the plan, or whether the plan did NOT specify that this section should be removed, and the code was therefore removed incorrectly. Also consider whether this removal breaks the syntax or functionality of the code in the updated file based on the programming language being used--if it does, explain this and state that the removal was incorrect. If the removal either wasn't intended by the proposed updates *or* breaks the syntax or functionality of the code, the removal is incorrect. If the removal was consistent with the intention of the plan *and* did not break the syntax or functionality of the code, the removal is correct.
+ - 'reasoning' is a string that explains whether, based on the *proposed updates*, this section was deliberately removed consistent with the intention of the plan, or whether the plan did NOT specify that this section should be removed, and the code was therefore removed incorrectly. Also consider whether this removal breaks the syntax or functionality of the code in the updated file based on the programming language being used--if it does, explain this and state that the removal was incorrect. If the removal either wasn't intended by the *proposed updates* *or* breaks the syntax or functionality of the code, the removal is incorrect. If the removal was consistent with the intention of the plan *and* did not break the syntax or functionality of the code, the removal is correct.
- 'correct' is a boolean that indicates whether the removal or overwriting was correct based on the 'reasoning' provided. If the removal was correct, set 'correct' to true. If the removal was incorrect, set 'correct' to false.
Based on supplied diffs, you must list EVERY code section that was removed or overwritten in the updated file in 'removed'. If there are no code sections that were removed or overwritten, 'removed' must be an empty array. If multiple code sections were removed or overwritten, list each one as a separate object in the 'removed' array. If multiple identical code sections were removed or overwritten, you MUST list them *all* in the 'removed' array--list each identical code section as a separate object in the array. Do NOT include removals that only modify whitespace. Do NOT include sections that were moved or refactored, only sections that were fully removed or overwritten.
@@ -395,15 +574,15 @@ Based on supplied diffs, you must list EVERY code section that was removed or ov
'hasDuplicationErrors': A boolean that indicates whether any code was *incorrectly* duplicated in the updated file, based on the reasoning provided in 'duplicationErrorsReasoning'. If code was *incorrectly* duplicated, set 'hasDuplicationErrors' to true. If code was *correctly* duplicated, consistent with the intention of the plan, set 'hasDuplicationErrors' to false.
-'comments': an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." "// ... existing code ..." or "// ..." or other comments which reference code from the original file. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.
+'comments': an array of objects with two properties: 'txt' and 'reference'. 'txt' is the exact text of a code comment. 'reference' is a boolean that indicates whether the comment is a placeholder of or reference to the original code, like "// rest of the function..." or "# existing init code...", or "// rest of the main function" or "// rest of your function..." or "// Existing methods..." or "// Remaining methods" or "// Existing code..." or "// ... existing setup code ..." or "// Existing code..." "// ... existing code ..." or "// ..." or other comments which reference code from the *original file*. References DO NOT need to exactly match any of the previous examples. Use your judgement to determine whether each comment is a reference. If 'reference' is true, the comment is a placeholder or reference to the original code. If 'reference' is false, the comment is not a placeholder or reference to the original code.
In 'comments', you must list EVERY comment included in the *updated file*. Only list *code comments* that are valid comments for the programming language being used. Do not list logging statements or any other non-comment text that is not a valid code comment. If there are no code comments in the *updated file*, 'comments' must be an empty array.
If there are multiple identical comments in the *updated file*, you MUST list them *all* in the 'comments' array--list each identical comment as a separate object in the array.
-'referenceErrorsReasoning': A string that succinctly explains whether any comments in the updated file are placeholders/references that should have been replaced with code from the original file. These are comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods...", "// Existing code..." or other comments which reference code from the original file. Only include comments that *are not* present in the original file and *are* present in the proposed updates. If there are no such comments, explain that there are no reference errors.
+'referenceErrorsReasoning': A string that succinctly explains whether any comments in the updated file are placeholders/references that should have been replaced with code from the *original file*. These are comments like "// rest of the function..." or "# existing init code...", or "// rest of the main function..." or "// rest of your function..." or "// Existing methods...", "// Existing code..." or other comments which reference code from the *original file*. Only include comments that *are not* present in the *original file* and *are* present in the *proposed updates*. If there are no such comments, explain that there are no reference errors.
-'hasReferenceErrors': A boolean that indicates whether any comments in the updated file are placeholders/references that should be replaced with code from the original file, based on the reasoning provided in 'referenceErrorsReasoning'.
+'hasReferenceErrors': A boolean that indicates whether any comments in the updated file are placeholders/references that should be replaced with code from the *original file*, based on the reasoning provided in 'referenceErrorsReasoning'.
In each of the reasoning keys above, be exhaustive and include *every* problem that is present in the file. But if there are no problems in a reasoning key, do NOT invent problems--explain according to your instructions for each key that there are no problems in that category.
`
diff --git a/app/server/model/prompts/build_update.go b/app/server/model/prompts/build_update.go
index f8afdb3c..003adb2d 100644
--- a/app/server/model/prompts/build_update.go
+++ b/app/server/model/prompts/build_update.go
@@ -4,203 +4,337 @@ import (
"fmt"
"github.com/plandex/plandex/shared"
+ "github.com/sashabaranov/go-openai"
+ "github.com/sashabaranov/go-openai/jsonschema"
)
-func GetUpdatedBuildPrompt(filePath, preBuildState, changesFile, changesDesc string) string {
+func GetReferencesPrompt(filePath, preBuildState, changesFile, changesDesc string) string {
preBuildStateWithLineNums := shared.AddLineNums(preBuildState)
- changesWithLineNums := shared.AddLineNums(changesFile)
+ changesWithLineNums := shared.AddLineNumsWithPrefix(changesFile, "pdx-new-")
- s := UpdatedBuildPrompt + "\n\n" + getPreBuildStatePrompt(filePath, preBuildStateWithLineNums) + "\n\n"
+ s := ReferencesPrompt + "\n\n" + getPreBuildStatePrompt(filePath, preBuildStateWithLineNums) + "\n\n"
s += fmt.Sprintf("Proposed updates:\n%s\n```\n%s\n```", changesDesc, changesWithLineNums)
return s
}
-var UpdatedBuildPrompt = `
- You are an AI that takes a file and a set of proposed changes to that file and determines exactly how the changes should be applied in order to produce a valid result. To do this you will produce a map of the resulting file, showing which sections remain unchanged from the original file and which sections will be updated by the proposed changes.
+var ReferencesPrompt = `
+You are an AI that analyzes an *original file* and *proposed updates* to that file and then identifies *all* *reference comments* present in the *proposed updates*.
+
+A reference comment is a comment that references code in the *original file* for the purpose of making it clear where a change should be applied. Examples of reference comments include:
+
+ - // ... existing code...
+ - # Existing code...
+ - /* ... */
+ - // Rest of the function...
+ - <!-- rest of div tag -->
+ - // ... rest of function ...
+ - // rest of component...
+ - # other methods...
+ - // ... rest of init code...
+ - // rest of the class...
+ - // other properties
+ - // other methods
+
+ Comments often won't exactly match one of the above examples, but they will always be referencing a block of code from the *original file* that is left out of the *proposed updates* for the sake of focusing on the specific change that is being made.
- In order to do this, you will go through the following process:
+ *NOT EVERY COMMENT IS A REFERENCE.* If a comment refers to code that is present in the *proposed updates* then it is *not* a reference. Similarly, if a comment explains something about the change being made in the *proposed updates*, it is also *not* a reference.
+
+ A reference comment MUST EXIST in the *proposed updates*. Do not include a reference comment unless it exists VERBATIM in the *proposed updates*.
- **1.** Identify all *anchors* in the proposed changes and list them all. An anchor a line or group of lines from the proposed changes that can be unambigulously matched with a line or group of lines in the pre-change file. Anchors must be exact matches between the proposed changes and the original file.
+ Before outputting the references, first output a section that lists *EVERY* comment in the *proposed updates*, including the line number of each comment prefixed by 'pdx-new-'. Below each comment, evaluate whether it is a reference comment. Focus on whether the comment is clearly referencing a block of code in the *original file*, whether it is explaining a change being made, or whether it is a comment that was carried over from the *original file* but does *not* reference any code that was left out of the *proposed updates*. After this evaluation, state whether each comment is a reference comment or not. Only list valid *comments* for the given programming language in the comments section. Do not include non-comment lines of code in the comments section.
- For each anchor:
- - Briefly identify it with a description
- - Output the starting line number prefixed by 'pdx-' in the proposed changes. If the anchor spans multiple lines, also output the ending line number prefixed by 'pdx-'.
- - Output the starting line number in the original file that corresponds to the anchor's starting line number in the proposed changes. If the anchor spans multiple lines, also output the ending line number in the original file prefixed by 'pdx-' that corresponds to the anchor's ending line number in the proposed changes.
-
Example:
---
- ## Anchors
+ Comments:
- 1. Signature and opening brace of the 'update' function
- Changes pdx-5 to pdx-6 > Original pdx-456 to pdx-457
+ pdx-new-2: // ... existing code to start transaction ...
+ Evaluation: refers the code at the beginning of the 'update' function that starts the database transaction.
+ Reference: true
- 2. 'Commit db transaction' comment
- Changes pdx-10 > Original pdx-456
+ pdx-new-6: // ... existing update code ...
+ Evaluation: refers the code inside the 'update' function that updates the user.
+ Reference: true
- 3. Closing brace of the 'update' function
- Changes pdx-7 > Original pdx-458
+ pdx-new-9: // ... existing code to commit db transaction...
+ Evaluation: refers the code inside the 'update' function that commits the database transaction.
+ Reference: true
- ---
+ pdx-new-4: // verify user permission before performing update
+ Evaluation: describes the change being made. Does not refer to any code in the *original file*.
+ Reference: false
- **2.** Identify all *references* in the proposed changes. A reference is a comment like:
-
- - "// ... existing code..."
- - "# Existing code..."
- - "/* ... */"
- - "// Rest of the function..."
- - "<!-- rest of div tag -->"
- - "// ... rest of function ..."
- - "// rest of component..."
- - "# other methods..."
- - "// ... rest of init code..."
- - "// rest of the class..."
- - "// other properties"
- - "// other methods"
-
- These are examples of reference comments, but there are many other variations a reference could take. Identify all reference comments based on the context of the change. Use your judgement to determine what is a reference. A reference must clearly be intented to reference code in the original file for the purpose of making it clear where a change should be applied.
+ pdx-new-85: // Rest of the main function...
+ Evaluation: refers to the rest of the main function that is left unchanged.
+ Reference: true
- For each reference:
- - Briefly identify it with a description
- - Output the line number of the reference prefixed by 'pdx-'
- - Output the starting line number prefixed by 'pdx-' in the original file that the reference is referring to. If the reference spans multiple lines, also output the ending line number prefixed by 'pdx-' in the original file that the reference is referring to.
-
- Ensure no *anchors* are included in the list of references. The referenced section of the original file must not overlap with any lines that are present in the proposed updates. For example, if there's a reference immediately after an opening brace in the proposed changes, the line with the opening brace must *NOT* be included in the referenced section of the original file. Similarly if there's a reference immediately before a closing brace in the proposed changes, the line with the closing brace must *NOT* be included in the referenced section of the original file since this would produce overlap.
-
- Example:
+ pdx-new-25: # Delete the object
+ Evaluation: describes the change being made. Does not refer to any code in the *original file*.
+ Reference: false
---
- ## References
- 1. "// ... existing imports..." reference to import statements in original file
- Changes pdx-3 > Original pdx-1 to pdx-10
+ Next, for each reference (if there are any), output valid xml with this structure:
- 2. "// ... rest of the function..." reference to code after the addition of the conditional statment in the 'update' function
- Changes pdx-10 > Original pdx-456 to pdx-460
+ <references>
+ <reference
+ comment="// ... existing code..."
+ description="Code after the addition of the conditional statement in the 'update' function"
+ proposedLine="pdx-new-10"
+ originalStart="pdx-456"
+ originalEnd="pdx-460"
+ />
+ </references>
- ---
+ Explanation of 'reference' tag attributes:
- **3.** Identify insertions in the proposed changes. An insertion is code from the proposed changes that should be added to the original file *and doesn't replace any existing code in the original file*. Insertions are for *new code*. They are *not* for changes to existing code. They are also *not* for deletions of existing code.
-
- For each insertion:
- - Briefly identify it with a description
- - Output the starting line of the code that will be insert in the proposed changes prefixed by 'pdx-'. If the insertion spans multiple lines, also output the ending line of the code that will be inserted in the proposed changes prefixed by 'pdx-'.
- - Output the line number in the original file that the new code should be inserted *after* prefixed by 'pdx-'. If the new code should be prepended to the original file, output 'pdx-0'. If the new code should be appended to the original file, output 'pdx-n' where n is the last line of the original file.
-
- Example:
+ **comment**: The comment that includes the reference.
- ---
- ## Insertions
+ **description**: A brief description of the reference. Make a special note of where the reference *begins* and *ends in the *original file* so that it does *not* include any lines that are already present in the *proposed updates*.
- 1. New imports
- Changes pdx-10 to pdx-15 > Original pdx-0
+ **proposedLine**: The line number, prefixed by 'pdx-new-', in the *proposed updates* that the reference is referring to. MUST be a line that exists in the *proposed updates* and contains the reference comment in 'comment' attribute.
- 2. New validation check
- Changes pdx-30 to pdx-35 > Original pdx-323
-
- ---
-
- **4.** Identify replacements in the proposed changes. A replacement is code from the proposed changes that should replace code in the original file. Replacements are for *changes* to existing code that must overwrite existing code in the original file. They are *not* for new code that does not replace any existing code in the original file. They are also *not* for deletions of existing code.
+ **originalStart**: The starting line number, prefixed by 'pdx-', in the *original file* that the reference is referring to. MUST be less than or equal to the 'originalEnd' line number and MUST be greater than or equal to 1.
- For each replacement:
- - Briefly identify it with a description
- - Output the starting line of the code that will be replaced in the proposed changes prefixed by 'pdx-'. If the replacement spans multiple lines, also output the ending line of the code that will be replaced in the proposed changes prefixed by 'pdx-'.
- - Output the starting line number in the original file that the replacement is replacing. If the replacement spans multiple lines, also output the ending line number in the original file prefixed by 'pdx-' that the replacement is replacing.
-
- Example:
+ **originalEnd**: The ending line number, prefixed by 'pdx-', in the *original file* that the reference is referring to. If the referenced code in the *original file* is a single line, the originalStart and originalEnd must be the same. 'originalEnd' MUST be greater than or equal to 'originalStart' and MUST be less than or equal to the last line number in the *original file*.
+
+ **Example:**
---
- ## Replacements
- 1. Update the db transaction commit
- Changes pdx-10 to pdx-15 > Original pdx-456 to pdx-461
+ **Original file:**
+
+ ` + "```" + `
+ pdx-1: function update(name string, id string) {
+ pdx-2: const tx = await client.startTransaction();
+ pdx-3: tx.setMode(TransactionMode.Serializable);
+ pdx-4:
+ pdx-5: const updateQuery = 'UPDATE users SET name = $1 WHERE id = $2';
+ pdx-6: await tx.execute(updateQuery, name, id);
+ pdx-7:
+ pdx-8: try {
+ pdx-9: await client.commit();
+ pdx-10: } catch (error) {
+ pdx-11: if (isRetryableError(error)) {
+ pdx-12: await client.rollback();
+ pdx-13: update();
+ pdx-14: } else {
+ pdx-15: throw error;
+ pdx-16: }
+ pdx-17: }
+ pdx-18: }
+ ` + "```" + `
+
+ **Proposed changes:**
+
+ Now we'll add a permission check so that the update only runs if the user has the necessary permissions.
+
+ ` + "```" + `
+ pdx-new-1: function update(name string, id string) {
+ pdx-new-2: // ... existing code to start transaction ...
+ pdx-new-3:
+ pdx-new-4: // verify user permission before performing update
+ pdx-new-5: if (canUpdate()) {
+ pdx-new-6: // ... existing update code ...
+ pdx-new-7: }
+ pdx-new-8:
+ pdx-new-9: // ... existing code to commit db transaction...
+ pdx-new-10: }
+ ` + "```" + `
+
+ References:
+
+ <references>
+ <reference
+ comment="// ... existing code to start transaction ..."
+ description="Code for starting database transaction"
+ proposedLine="pdx-new-2"
+ originalStart="pdx-2"
+ originalEnd="pdx-3"
+ />
+ <reference
+ comment="// ... existing update code ..."
+ description="Code inside the canUpdate() conditional for updating the user"
+ proposedLine="pdx-new-6"
+ originalStart="pdx-5"
+ originalEnd="pdx-6"
+ />
+ <reference
+ comment="// ... existing code to commit db transaction..."
+ description="Code for committing the database transaction and handling errors"
+ proposedLine="pdx-new-9"
+ originalStart="pdx-8"
+ originalEnd="pdx-17"
+ />
+ </references>
- 2. Improve the validation check
- Changes pdx-30 to pdx-40 > Original pdx-323 to pdx-328
-
---
-
- **5.** Identify deletions in the proposed changes. A deletion is code that should be removed from the original file. They are marked by comments like:
- - "// Remove the validation check"
- - "# Remove the 'update' function"
- - "<!-- Remove the header -->"
- - "// Remove the footer"
-
- Deletions are for *deletions* of existing code. They are *not* for changes to existing code. They are also *not* for new code that does not replace any existing code in the original file.
-
- Use *deletions* for these kinds of comments rather than replacements so that the deletion comment does not end up in the result.
- For each deletion:
- - Briefly identify it with a description
- - Output the line number of the deletion in the proposed changes prefixed by 'pdx-'
- - Output the starting line number in the original file that the deletion is deleting. If the deletion spans multiple lines, also output the ending line number in the original file prefixed by 'pdx-' that the deletion is deleting.
-
- Example:
+ Note that in the example, the "// verify user permission before performing update" comment is *not* a reference since it refers to code that is included in the *proposed updates* and does *not* refer to code in the *original file* that was left out of the *proposed updates*.
- ---
- ## Deletions
+ In description attributes, make a special note of where the reference *begins* and *ends in the *original file* so that it does *not* include any lines that are already present in the *proposed updates*. Note that in the above example, the function signature and opening brace of the 'update' function is *not included* in the reference since it's already present in the *proposed updates*. And node in the last reference, the closing brace of the 'update' function is *not included* since it's already present in the *proposed updates*.
- 1. Remove the old validation check
- Changes pdx-30 > Original pdx-323 to pdx-328
+ If there are no clear references in the *proposed updates*, output an empty <references> element. The *proposed updates* often will not inlcude any reference comments. In that case, output an empty <references> element. Do NOT include reference comments that are not present in the *proposed updates* or comments that are not clearly references to a block of code in the *original file* that is left out of the *proposed updates* for the sake of focusing on the specific change that is being made.
- 2. Remove the 'update' function
- Changes pdx-10 > Original pdx-456 to pdx-461
-
- ---
-
- **6.** Now that you've identified all the anchors, references, insertions, replacements, and deletions in the proposed changes, you will output a map of the ENTIRE resulting file as a series of sections that refer to either the original file or the proposed changes. Take into account anchors, references, insertions, replacements, and deletions when building the map.
-
- Use xml tags for the sections with these attributes:
- - 'type': 'original' or 'proposed'
- - 'description': a brief description of the section. Be clear, precise, and unambiguous about where the section begins and ends.
- - 'anchor': optional anchor number from step 1. Do NOT repeat an anchor number in multiple sections—each anchor should be unique.
- - 'reference': optional reference number from step 2. Do NOT repeat a reference number in multiple sections—each reference should be unique.
- - 'insertion': optional insertion number from step 3. Do NOT repeat an insertion number in multiple sections—each insertion should be unique.
- - 'replacement': optional replacement number from step 4. Do NOT repeat a replacement number in multiple sections—each replacement should be unique.
- - 'start': the starting line number in the original file or proposed changes
- - 'end': the ending line number in the original file or proposed changes. If the section is a single line, the start and end should be the same.
- - 'newlineBefore': optional boolean. Set to 'true' if a newline should be added *before* the section to preserve the original file's formatting. Omit this if the section already includes all necessary newlines.
- - 'newlineAfter': optional boolean. Set to 'true' if a newline should be added *after* the section to preserve the original file's formatting. Omit this if the section already includes all necessary newlines.
-
- Follow these rules for the map:
- - Always give anchors, references, inserts, replacements, and deletions their *own sections* in the map. Do not include multiple anchors, references, inserts, replacements, and deletions in a single section.
- - Anchors should always have type "proposed". Even though they are present in both the original file and the proposed changes, always include them as "proposed" sections in the map for consistency. Anchors that are added as "proposed" sections must *NOT* be included in the map as "original" sections.
- - References should always have type "original".
- - Insertions should always have type "proposed".
- - Replacements should always have type "proposed".
- - Lines that have been *deleted* in the 'deletions' list must *NOT* be included in the map.
- - Lines that have been *replaces* in the 'replacements' list must have a corresponding section in the map with type "proposed", and the lines from the original file that have been replaced must *NOT* be included in the map.
- - Lines that have been *inserted* in the 'insertions' list must have a corresponding section in the map with type "proposed".
- - Every anchor, reference, insertion, and replacement *must* have a corresponding section in the map. 'deletion' sections must *NOT* be included in the map.
- - Do not include the same line of code in multiple sections. Each line should be assigned to *only one* section.
- - Be precise about section boundaries and DO NOT incorrectly duplicate or omit a single line of code.
- - The map, when assembled from all the sections *must* include the *complete* update file with all changes correctly applied, no change omitted, and no references incorrectly included.
- - When producing the map, do not change or omit any lines from the original file unless they are explicitly delete or replaced. This includes comments, whitespace, and newlines.
- - You MUST NOT omit any sections from the original file that were not *explictly* deleted or replaced in the proposed changes.
- - You MUST ensure newlines are preserved from both the original file and the proposed changes. Do NOT add or remove newlines unless it is explicitly intended by the proposed changes. Even if there is a large block of newlines in the original file, you MUST NOT omit them unless they are explicitly deleted in the proposed changes.
- - The start and end of each section *must* be the same line if the section is a single line.
- - The start and end of each section *must* be a line that exists in the original file or proposed changes.
- - The end line of each section must always be equal to or greater than the start line.
+ You MUST carefully consider code structures when determining the 'originalStart' and 'originalEnd' lines for a reference. If a reference looks like this:
- Example:
+ pdx-1: class MyClass {
+ pdx-2: constructor() {
+ pdx-3: this.initialize();
+ pdx-4: }
+ pdx-5:
+ pdx-6: // ... existing code ...
+ pdx-7: }
- ---
- ## Result map
-
- <result>
- <section type="original" description="unchanged import statements prior to the newly added imports" start="pdx-1" end="pdx-10" />
- <section type="proposed" description="new imports" insertion="1" start="pdx-2" end="pdx-10" />
- <section type="original" description="unchanged code before the 'update' function" start="pdx-11" end="pdx-300" newlineBefore="true" />
- <section type="proposed" description="function signature and opening brace of the 'update' function" start="pdx-301" end="pdx-302" anchor="1" newlineAfter="true" />
- <section type="proposed" description="new code added to the 'update' function" insertion="2" start="pdx-1" end="pdx-5" />
- <section type="proposed" description="changes to the loop body in the 'update' function" replacement="1" start="pdx-6" end="pdx-10" />
- <section type="original" description="rest of the unchanged 'update' function" reference="1" start="pdx-11" end="pdx-300" />
- <section type="proposed" description="closing brace of the 'update' function" start="pdx-301" end="pdx-302" anchor="2" newlineBefore="true" />
- <section type="original" description="unchanged code following the 'update' function" reference="2" start="pdx-303" end="pdx-620" />
- </result>
- ---
+ The the reference ONLY refers to the code within the 'MyClass' class. If there is additional code before or after the 'MyClass' class, it must not be included. The same applies to other code structures, like functions, loops, conditionals, etc.
- Do not include any additional text in your final output. Only output the process sections and the result map.
+ Do not include any additional text in your final output. Only output the references.
`
+
+var UpdatedListReplacementsFn = openai.FunctionDefinition{
+ Name: "listChangesWithLineNums",
+ Parameters: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "problems": {
+ Type: jsonschema.String,
+ },
+ "originalSections": {
+ Type: jsonschema.Array,
+ Items: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "description": {
+ Type: jsonschema.String,
+ },
+ "reasoning": {
+ Type: jsonschema.String,
+ },
+ "sectionStartLine": {
+ Type: jsonschema.String,
+ },
+ "sectionEndLine": {
+ Type: jsonschema.String,
+ },
+ "shouldChange": {
+ Type: jsonschema.Boolean,
+ },
+ "shouldRemove": {
+ Type: jsonschema.Boolean,
+ },
+ },
+ Required: []string{
+ "description",
+ "reasoning",
+ "sectionStartLine",
+ "sectionEndLine",
+ "shouldChange",
+ "shouldRemove",
+ },
+ },
+ },
+ "entireFileReasoning": {
+ Type: jsonschema.String,
+ },
+ "entireFile": {
+ Type: jsonschema.Boolean,
+ },
+ "changes": {
+ Type: jsonschema.Array,
+ Items: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "section": {
+ Type: jsonschema.String,
+ },
+ "summary": {
+ Type: jsonschema.String,
+ },
+ "newReasoning": {
+ Type: jsonschema.String,
+ },
+ "reasoning": {
+ Type: jsonschema.String,
+ },
+ "structureReasoning": {
+ Type: jsonschema.String,
+ },
+ "closingSyntaxReasoning": {
+ Type: jsonschema.String,
+ },
+ "orderReasoning": {
+ Type: jsonschema.String,
+ },
+ "hasChange": {
+ Type: jsonschema.Boolean,
+ },
+ "insertBefore": {
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "shouldInsertBefore": {
+ Type: jsonschema.Boolean,
+ },
+ "line": {
+ Type: jsonschema.String,
+ },
+ },
+ Required: []string{"shouldInsertBefore", "firstLine"},
+ },
+ "insertAfter": {
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "shouldInsertAfter": {
+ Type: jsonschema.Boolean,
+ },
+ "line": {
+ Type: jsonschema.String,
+ },
+ },
+ Required: []string{"shouldInsertAfter", "firstLine"},
+ },
+ "new": {
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "startLineString": {
+ Type: jsonschema.String,
+ },
+ "endLineString": {
+ Type: jsonschema.String,
+ },
+ },
+ Required: []string{"startLineString", "endLineString"},
+ },
+ "old": {
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "startLineString": {
+ Type: jsonschema.String,
+ },
+ "endLineString": {
+ Type: jsonschema.String,
+ },
+ },
+ Required: []string{"startLineString", "endLineString"},
+ },
+ },
+ Required: []string{
+ "section",
+ "summary",
+ "newReasoning",
+ "structureReasoning",
+ "closingSyntaxReasoning",
+ "orderReasoning",
+ "hasChange",
+ "insertBefore",
+ "insertAfter",
+ "new",
+ "old",
+ },
+ },
+ },
+ },
+ Required: []string{"originalSections", "entireFileReasoning", "entireFile", "problems", "changes"},
+ },
+}
diff --git a/app/server/types/model.go b/app/server/types/model.go
index 3759ae21..f8d83779 100644
--- a/app/server/types/model.go
+++ b/app/server/types/model.go
@@ -20,6 +20,21 @@ type ChangesWithLineNums struct {
Changes []*shared.StreamedChangeWithLineNums `json:"changes"`
}
+type UpdatedChangesWithLineNums struct {
+ EntireFileReasoning string `json:"entireFileReasoning"`
+ EntireFile bool `json:"entireFile"`
+ Problems string `json:"problems"`
+ OriginalSections []struct {
+ Description string `json:"description"`
+ Reasoning string `json:"reasoning"`
+ SectionStartLine string `json:"sectionStartLine"`
+ SectionEndLine string `json:"sectionEndLine"`
+ ShouldChange bool `json:"shouldChange"`
+ ShouldRemove bool `json:"shouldRemove"`
+ } `json:"originalSections"`
+ Changes []*shared.StreamedChangeWithLineNumsUpdated `json:"changes"`
+}
+
// type StreamedChangesFull struct {
// Changes []*shared.StreamedChangeFull `json:"changes"`
// }
diff --git a/app/shared/data_models.go b/app/shared/data_models.go
index a9975791..524febe1 100644
--- a/app/shared/data_models.go
+++ b/app/shared/data_models.go
@@ -153,13 +153,14 @@ type PlanBuild struct {
}
type Replacement struct {
- Id string `json:"id"`
- Old string `json:"old"`
- EntireFile bool `json:"entireFile"`
- New string `json:"new"`
- Failed bool `json:"failed"`
- RejectedAt *time.Time `json:"rejectedAt,omitempty"`
- StreamedChange *StreamedChangeWithLineNums `json:"streamedChange"`
+ Id string `json:"id"`
+ Old string `json:"old"`
+ EntireFile bool `json:"entireFile"`
+ New string `json:"new"`
+ Failed bool `json:"failed"`
+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`
+ StreamedChange *StreamedChangeWithLineNums `json:"streamedChange"`
+ StreamedChangeUpdated *StreamedChangeWithLineNumsUpdated `json:"streamedChangeUpdated"`
}
type PlanFileResult struct {
diff --git a/app/shared/plan_result_replacements.go b/app/shared/plan_result_replacements.go
index 41f4c648..529e15e7 100644
--- a/app/shared/plan_result_replacements.go
+++ b/app/shared/plan_result_replacements.go
@@ -50,8 +50,8 @@ func ApplyReplacements(content string, replacements []*Replacement, setFailed bo
log.Println("replacement.Old:")
log.Println(replacement.Old)
- // log.Println("Updated:")
- // log.Println(updated)
+ log.Println("Updated:")
+ log.Println(updated)
// log.Println("All replacements:")
// log.Println(spew.Sdump(replacements))
diff --git a/app/shared/streamed_change.go b/app/shared/streamed_change.go
index 560ed83e..99e43d91 100644
--- a/app/shared/streamed_change.go
+++ b/app/shared/streamed_change.go
@@ -26,6 +26,35 @@ type StreamedChangeWithLineNums struct {
New string `json:"new"`
}
+type StreamedChangeWithLineNumsUpdated struct {
+ Summary string `json:"summary"`
+ Reasoning string `json:"reasoning"`
+ Section string `json:"section"`
+ NewReasoning string `json:"newReasoning"`
+ OrderReasoning string `json:"orderReasoning"`
+ StructureReasoning string `json:"structureReasoning"`
+ ClosingSyntaxReasoning string `json:"closingSyntaxReasoning"`
+ InsertBefore *InsertBefore `json:"insertBefore"`
+ InsertAfter *InsertAfter `json:"insertAfter"`
+ HasChange bool `json:"hasChange"`
+ Old StreamedChangeSection `json:"old"`
+ StartLineIncludedReasoning string `json:"startLineIncludedReasoning"`
+ StartLineIncluded bool `json:"startLineIncluded"`
+ EndLineIncludedReasoning string `json:"endLineIncludedReasoning"`
+ EndLineIncluded bool `json:"endLineIncluded"`
+ New StreamedChangeSection `json:"new"`
+}
+
+type InsertBefore struct {
+ ShouldInsertBefore bool `json:"shouldInsertBefore"`
+ Line string `json:"line"`
+}
+
+type InsertAfter struct {
+ ShouldInsertAfter bool `json:"shouldInsertAfter"`
+ Line string `json:"line"`
+}
+
// type StreamedChangeFull struct {
// Summary string `json:"summary"`
// Old string `json:"old"`
@@ -37,18 +66,24 @@ type StreamedVerifyFunction struct {
IsCorrect bool `json:"isCorrect"`
}
-func (streamedChange StreamedChangeWithLineNums) GetLines() (int, int, error) {
+func (streamedChangeSection StreamedChangeSection) GetLines() (int, int, error) {
+ return streamedChangeSection.GetLinesWithPrefix("pdx-")
+}
+
+func (streamedChangeSection StreamedChangeSection) GetLinesWithPrefix(prefix string) (int, int, error) {
var startLine, endLine int
var err error
- if streamedChange.Old.EntireFile {
+ if streamedChangeSection.EntireFile {
return 1, -1, nil
}
- if streamedChange.Old.StartLineString == "" {
- startLine = streamedChange.Old.StartLine
+ if streamedChangeSection.StartLineString == "" {
+ log.Printf("StartLineString is empty\n")
+ // spew.Dump(streamedChangeSection)
+ startLine = streamedChangeSection.StartLine
} else {
- startLine, err = ExtractLineNumber(streamedChange.Old.StartLineString)
+ startLine, err = ExtractLineNumberWithPrefix(streamedChangeSection.StartLineString, prefix)
if err != nil {
log.Printf("Error extracting start line number: %v\n", err)
@@ -56,14 +91,16 @@ func (streamedChange StreamedChangeWithLineNums) GetLines() (int, int, error) {
}
}
- if streamedChange.Old.EndLineString == "" {
- if streamedChange.Old.EndLine > 0 {
- endLine = streamedChange.Old.EndLine
+ if streamedChangeSection.EndLineString == "" {
+ log.Printf("EndLineString is empty\n")
+ // spew.Dump(streamedChangeSection)
+ if streamedChangeSection.EndLine > 0 {
+ endLine = streamedChangeSection.EndLine
} else {
endLine = startLine
}
} else {
- endLine, err = ExtractLineNumber(streamedChange.Old.EndLineString)
+ endLine, err = ExtractLineNumberWithPrefix(streamedChangeSection.EndLineString, prefix)
if err != nil {
log.Printf("Error extracting end line number: %v\n", err)
@@ -71,6 +108,8 @@ func (streamedChange StreamedChangeWithLineNums) GetLines() (int, int, error) {
}
}
+ log.Printf("StartLine: %d, EndLine: %d\n", startLine, endLine)
+
if startLine > endLine {
log.Printf("Start line is greater than end line: %d > %d\n", startLine, endLine)
return 0, 0, fmt.Errorf("start line is greater than end line: %d > %d", startLine, endLine)
@@ -85,12 +124,16 @@ func (streamedChange StreamedChangeWithLineNums) GetLines() (int, int, error) {
}
func ExtractLineNumber(line string) (int, error) {
+ return ExtractLineNumberWithPrefix(line, "pdx-")
+}
+
+func ExtractLineNumberWithPrefix(line, prefix string) (int, error) {
// Split the line at the first space to isolate the line number
parts := strings.SplitN(line, " ", 2)
// Remove the colon from the line number part
lineNumberStr := strings.TrimSuffix(parts[0], ":")
- lineNumberStr = strings.TrimPrefix(lineNumberStr, "pdx-")
+ lineNumberStr = strings.TrimPrefix(lineNumberStr, prefix)
if lineNumberStr == "" {
return 0, fmt.Errorf("no line number found")
}
diff --git a/app/shared/utils.go b/app/shared/utils.go
index d8fd25bc..1ab477c9 100644
--- a/app/shared/utils.go
+++ b/app/shared/utils.go
@@ -66,15 +66,23 @@ func Capitalize(s string) string {
}
func AddLineNums(s string) string {
+ return AddLineNumsWithPrefix(s, "pdx-")
+}
+
+func AddLineNumsWithPrefix(s, prefix string) string {
var res string
for i, line := range strings.Split(s, "\n") {
- res += fmt.Sprintf("pdx-%d: %s\n", i+1, line)
+ res += fmt.Sprintf("%s%d: %s\n", prefix, i+1, line)
}
return res
}
func RemoveLineNums(s string) string {
- return regexp.MustCompile(`(?m)^pdx-\d+: `).ReplaceAllString(s, "")
+ return RemoveLineNumsWithPrefix(s, "pdx-")
+}
+
+func RemoveLineNumsWithPrefix(s, prefix string) string {
+ return regexp.MustCompile(fmt.Sprintf(`(?m)^%s\d+: `, prefix)).ReplaceAllString(s, "")
}
// indexRunes searches for the slice of runes `needle` in the slice of runes `haystack`