Skip to content

Commit 2e45581

Browse files
committed
Add context files support to edit and issue commands
1 parent f65324b commit 2e45581

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

cmd/edit.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,21 @@ Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
8686

8787
var prompt string
8888
if editCode != "" {
89-
prompt = constants.EDIT_CODE_PROMPT + "\nEDIT: " + editCode + "\n\nGOAL: " + chatPrompt + "\n\nFILE: " + contents
89+
prompt = constants.EDIT_CODE_PROMPT + "\nEDIT: " + editCode + "\n\nGOAL: " + chatPrompt + "\n\nFILE: " + filePath + "\n\n" + contents
9090
} else {
91-
prompt = constants.EDIT_CODE_PROMPT + "\nGOAL: " + chatPrompt + "\n\nFILE: " + contents
91+
prompt = constants.EDIT_CODE_PROMPT + "\nGOAL: " + chatPrompt + "\n\nFILE: " + filePath + "\n\n" + contents
92+
}
93+
94+
if len(contextFiles) > 0 {
95+
var contextContent string
96+
for _, contextFile := range contextFiles {
97+
contextContent, err = utils.LoadFile(contextFile)
98+
if err != nil {
99+
log.Errorf("Error loading context file: %s", err)
100+
continue
101+
}
102+
prompt += "\n\nCONTEXT: " + contextFile + "\n\n" + contextContent
103+
}
92104
}
93105

94106
stream, err := ai.SimpleStreamRequest(prompt, c)
@@ -154,4 +166,5 @@ func init() {
154166
editCmd.Flags().IntVarP(&startLine, "start", "s", 1, "Start line")
155167
editCmd.Flags().IntVarP(&endLine, "end", "e", 0, "End line")
156168
editCmd.Flags().StringVarP(&chatPrompt, "goal", "g", "", "Goal of the edit")
169+
editCmd.Flags().StringSliceVarP(&contextFiles, "context", "c", []string{}, "Context files")
157170
}

cmd/issue.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ var issueCmd = &cobra.Command{
133133
prompt += comment
134134
tokens += commentTokens
135135
}
136+
} else if len(contextFiles) > 0 {
137+
log.Debug("Using specified files...")
138+
var contextContent string
139+
for _, contextFile := range contextFiles {
140+
contextContent, err = utils.LoadFile(contextFile)
141+
if err != nil {
142+
log.Errorf("Error loading context file: %s", err)
143+
continue
144+
}
145+
prompt += "\n\nFile: " + contextFile + "\n\n" + contextContent
146+
fileTokens, err := calc.PreciseTokens(contextContent)
147+
if err != nil {
148+
log.Errorf("Error getting tokens: %s", err)
149+
os.Exit(1)
150+
}
151+
if tokens+fileTokens > calc.GetMaxTokens(c.Model) {
152+
break
153+
}
154+
tokens += fileTokens
155+
}
136156
} else {
137157
log.Debug("Using repo contents...")
138158
// get the repo context here
@@ -246,6 +266,7 @@ func init() {
246266

247267
issueCmd.Flags().IntVarP(&issuePRNumber, "number", "n", 0, "the number of the issue to get")
248268
issueCmd.Flags().StringVarP(&question, "question", "q", "", "the question to ask Otto")
269+
issueCmd.Flags().StringSliceVarP(&contextFiles, "context", "f", []string{}, "the files to use as context")
249270
issueCmd.Flags().BoolVarP(&useComments, "comments", "c", false, "use comments instead of git repo for context")
250271
issueCmd.Flags().BoolVarP(&promptOnly, "prompt-only", "p", false, "only generate a prompt, don't ask Otto")
251272
issueCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

cmd/vars.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var appendFile bool
4747
var previousTag string
4848
var currentTag string
4949

50+
var contextFiles []string
51+
5052
var log = l.NewWithOptions(os.Stderr, l.Options{
5153
Level: l.InfoLevel,
5254
ReportTimestamp: false,

pkg/constants/prompts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var PR_BODY_PROMPT string = "You are a helpful assistant who writes pull request
4040

4141
var COMPRESS_DIFF_PROMPT string = "You are a helpful assistant who describes git diff changes. You will be given a Git diff and you should use it to create a description of the changes. The description should be no longer than 75 characters long and should describe the changes in the diff. Do not include the file names in the description."
4242

43-
var EDIT_CODE_PROMPT string = `You are a helpful assistant who edits code. You will be given a file what you are trying to accomplish in the edit and you should edit it to the best of your abilities. Only edit the code you are told. All code to edit will be preceded by "EDIT:". If "EDIT:" is omitted, assume you must edit the entire file. The goal of the edit will be preceded by "GOAL:". The entire file will be preceded by "FILE:". If there is not content after "FILE:", assume you are writing new code. Make sure to use the language specified in the task. The output code should be unformatted. Use no markdown.\n`
43+
var EDIT_CODE_PROMPT string = `You are a helpful assistant who edits code. You will be given a file what you are trying to accomplish in the edit and you should edit it to the best of your abilities. Only edit the code you are told. All code to edit will be preceded by "EDIT:". If "EDIT:" is omitted, assume you must edit the entire file. The goal of the edit will be preceded by "GOAL:". The entire file will be preceded by "FILE:". If there is not content after "FILE:", assume you are writing new code. There may also be additional files added to give you more information about the project, those will be preceded by 'CONTEXT: '. DO NOT EDIT THOSE FILES. Make sure to use the language specified in the task. The output code should be unformatted. Use no markdown.\n`
4444

4545
var RELEASE_PROMPT string = `You are a helpful assistant who creates GitHub release notes from git commit logs. You will be given a series of git commit messages and your task is to summarize these messages into release notes. The release notes should:
4646
- Be concise and informative about the changes made in the release.

0 commit comments

Comments
 (0)