-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add EditHandler for 'edit' and 'insert' modes
- Loading branch information
Showing
6 changed files
with
193 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { | ||
DefaultEditCommands, | ||
PromptString, | ||
inputTextWithoutContextChipsFromPromptEditorState, | ||
} from '@sourcegraph/cody-shared' | ||
import { executeCodyCommand } from '../../../commands/CommandsController' | ||
import type { ChatControllerOptions } from '../ChatController' | ||
import type { ContextRetriever } from '../ContextRetriever' | ||
import { computeContextAlternatives } from './ChatHandler' | ||
import type { AgentHandler, AgentHandlerDelegate, AgentRequest } from './interfaces' | ||
|
||
export class EditHandler implements AgentHandler { | ||
constructor( | ||
private mode: 'edit' | 'insert', | ||
private contextRetriever: Pick<ContextRetriever, 'retrieveContext'>, | ||
private readonly editor: ChatControllerOptions['editor'] | ||
) {} | ||
|
||
async handle( | ||
{ | ||
requestID, | ||
inputText, | ||
mentions, | ||
editorState, | ||
span, | ||
signal, | ||
chatBuilder, | ||
recorder, | ||
}: AgentRequest, | ||
delegate: AgentHandlerDelegate | ||
): Promise<void> { | ||
const contextAlternatives = await computeContextAlternatives( | ||
this.contextRetriever, | ||
this.editor, | ||
{ text: inputText, mentions }, | ||
editorState, | ||
span, | ||
signal | ||
) | ||
signal.throwIfAborted() | ||
const context = contextAlternatives[0].items | ||
|
||
chatBuilder.setLastMessageContext(context, contextAlternatives) | ||
|
||
const inputTextWithoutContextChips = editorState | ||
? PromptString.unsafe_fromUserQuery( | ||
inputTextWithoutContextChipsFromPromptEditorState(editorState) | ||
) | ||
: inputText | ||
|
||
recorder.recordChatQuestionExecuted(context, { addMetadata: true, current: span }) | ||
|
||
const result = await executeCodyCommand(DefaultEditCommands.Edit, { | ||
requestID, | ||
runInChatMode: true, | ||
userContextFiles: context, | ||
configuration: { | ||
instruction: inputTextWithoutContextChips, | ||
mode: this.mode, | ||
// Only document code uses non-edit (insert mode), set doc intent for Document code prompt | ||
// to specialize cody command runner for document code case. | ||
intent: this.mode === 'edit' ? 'edit' : 'doc', | ||
}, | ||
}) | ||
if (result?.type !== 'edit' || !result.task) { | ||
delegate.postError(new Error('Failed to execute edit command'), 'transcript') | ||
delegate.postDone() | ||
return | ||
} | ||
|
||
const task = result.task | ||
|
||
let responseMessage = `Here is the response for the ${task.intent} instruction:\n` | ||
|
||
if (!task.diff && task.replacement) { | ||
task.diff = [ | ||
{ | ||
type: 'insertion', | ||
text: task.replacement, | ||
range: task.originalRange, | ||
}, | ||
] | ||
} | ||
|
||
task.diff?.map(diff => { | ||
responseMessage += '\n```diff\n' | ||
if (diff.type === 'deletion') { | ||
responseMessage += task.document | ||
.getText(diff.range) | ||
.split('\n') | ||
.map(line => `- ${line}`) | ||
.join('\n') | ||
} | ||
if (diff.type === 'decoratedReplacement') { | ||
responseMessage += diff.oldText | ||
.split('\n') | ||
.map(line => `- ${line}`) | ||
.join('\n') | ||
responseMessage += diff.text | ||
.split('\n') | ||
.map(line => `+ ${line}`) | ||
.join('\n') | ||
} | ||
if (diff.type === 'insertion') { | ||
responseMessage += diff.text | ||
.split('\n') | ||
.map(line => `+ ${line}`) | ||
.join('\n') | ||
} | ||
responseMessage += '\n```' | ||
}) | ||
|
||
delegate.postMessageInProgress({ | ||
speaker: 'assistant', | ||
text: PromptString.unsafe_fromLLMResponse(responseMessage), | ||
}) | ||
delegate.postDone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters