Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prompt snippets support #234220

Open
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

legomushroom
Copy link
Member

@legomushroom legomushroom self-assigned this Nov 19, 2024
@vs-code-engineering vs-code-engineering bot added this to the November 2024 milestone Nov 19, 2024
@@ -396,18 +405,30 @@ class WriteableStreamImpl<T> implements WriteableStream<T> {
}

private flowData(): void {
if (this.buffer.data.length > 0) {
// if buffer is empty, nothing to do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to remove 1 level of indentation.

@legomushroom legomushroom force-pushed the legomushroom/prompt-snippet-completions branch from 6f5026f to a927e4e Compare November 19, 2024 21:45
@@ -75,7 +75,15 @@ export class ImplicitContextAttachmentWidget extends Disposable {
});
this.domNode.ariaLabel = ariaLabel;
this.domNode.tabIndex = 0;
const hintElement = dom.append(this.domNode, dom.$('span.chat-implicit-hint', undefined, 'Current file'));

const hintElement = dom.append(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it a bit more readable by splitting params to separate lines.

if (this.implicitContext?.value) {
const implicitPart = store.add(this.instantiationService.createInstance(ImplicitContextAttachmentWidget, this.implicitContext, this._contextResourceLabels));
if (this._implicitContext?.value) {
const implicitPart = store.add(this.instantiationService.createInstance(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put params on separate lines only to improve readability.


prompt.parts
.forEach((part, i) => {
public async resolveVariables(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly readability improvements in this file, the logic remains the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I'd prefer to keep PRs focused more narrowly, and the word "mostly" makes it a little harder to review :)

// to go first so that an replacement logic is simple
resolvedVariables
.sort((left, right) => {
assertDefined(
Copy link
Member Author

@legomushroom legomushroom Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These asserts are to omit TS hacks ☺️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the ! because we know that these are parts of a prompt and have ranges, but the assert is fine too

) {
super();
this._register(widget.inputEditor.onDidChangeModelContent(e => {
e.changes.forEach(c => {
// Don't mutate entries in _variables, since they will be returned from the getter
this._variables = coalesce(this._variables.map(ref => {
this._variables = coalesce(this._variables.map((ref) => {
if (c.text === `#file:${ref.filenameWithReferences}`) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only way I made it work and its not ideal, any better ideas? cc @roblourens @joyceerhl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this trying to do? Is this triggered when that text is inserted all at once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we add the (+N more) label to a filename after nested references are resolved, this is run and as the result the variables are removed. The logic below infers that the variables text have changed and are hence the variables are no longer valid and need to be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we update the variable objects when we update the labels, this if check works, - if the variable text is really the same as the variable should have, do nothing.

endLineNumber: ref.range.endLineNumber,
endColumn: ref.range.endColumn + delta
}
ref.range = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roblourens do you recall the main reason for the // Don't mutate entries in _variables.. above? Because the ref is now a class instance with event listeners now, I do need to mutate the variables here 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because those objects will have been previously returned, and other objects will be holding on to them, and not expecting them to change. Maybe there is somewhere that we diff the previous object with the new one, and mutating the object breaks that. There are other ways we can solve that. But I don't quite understand the problem for you

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is somewhere that we diff the previous object with the new one

But that would break only if the diff logic relies on the object pointers, doing diffing by-attribute should be unaffected in either case?

There is no really a problem for me, just wanted to make sure that mutating the objects is fine here. We do need to mutate them now because the ref variable is a class instance now rather than a simple object it was before. The class instance can have event listeners registered on it by other code, and it handles nested file references resolution which we don't want to again over and over 🤷

const refAtThisPosition = references.find(r =>
r.range.startLineNumber === position.lineNumber &&
r.range.startColumn === position.column);
if (refAtThisPosition) {
const length = refAtThisPosition.range.endColumn - refAtThisPosition.range.startColumn;
const text = message.substring(0, length);
const range = new OffsetRange(offset, offset + length);
return new ChatRequestDynamicVariablePart(range, refAtThisPosition.range, text, refAtThisPosition.id, refAtThisPosition.modelDescription, refAtThisPosition.data, refAtThisPosition.fullName, refAtThisPosition.icon, refAtThisPosition.isFile);
return new ChatRequestDynamicVariablePart(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to reduce the number of params used.

(part as ChatRequestDynamicVariablePart).fullName,
(part as ChatRequestDynamicVariablePart).icon,
(part as ChatRequestDynamicVariablePart).isFile
variable,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to reduce the number of params on the constructor.

…changes, added unit test for the `assertDefined` util
…ence-recursion-proof and add approptiate tests
…eference object only if `prompt-snippets` config value is set
…ile reference object only if `prompt-snippets` config value is set
@legomushroom legomushroom force-pushed the legomushroom/prompt-snippet-completions branch from a927e4e to 2fa00b9 Compare November 19, 2024 22:18
Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not done reviewing but wanted to drop the first round of comments. Overall, when I'm working on a large feature, I usually try to start pushing small PRs to main as early as possible. That's the best way to avoid merge conflicts and get feedback early. Don't be afraid of pushing an incomplete feature behind a hidden setting, as long as it won't break anything. We do that often on this team. I also would rather avoid PRs that make multiple unrelated changes. It just gets a bit hard to review.

* assertDefined(null, new Error('Should throw this error.'))
* ```
*/
export function assertDefined<T>(value: T, error: string | NonNullable<Error>): asserts value is NonNullable<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this in types.ts, assertIsDefined

@@ -186,7 +186,7 @@ export interface ITransformer<Original, Transformed> {
error?: IErrorTransformer;
}

export function newWriteableStream<T>(reducer: IReducer<T>, options?: WriteableStreamOptions): WriteableStream<T> {
export function newWriteableStream<T>(reducer: IReducer<T> | null, options?: WriteableStreamOptions): WriteableStream<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional argument? We generally use undefined over null

const currentFile = localize('openEditor', "Current file context");
const inactive = localize('enableHint', "disabled");
const currentFileHint = currentFile + (this.attachment.enabled ? '' : ` (${inactive})`);
const title = `${currentFileHint}\n${uriLabel}`;
const title = `${currentFileHint}${this.getUriLabel(file)}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this returns an array, concatenated to a string, is that right?

state: WorkingSetEntryState.Attached,
kind: 'reference',
});

seenEntries.add(child);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a correct fix (cc @joyceerhl) but is this method even related to your new feature? I prefer not grouping unrelated fixes into a PR that's already really big


prompt.parts
.forEach((part, i) => {
public async resolveVariables(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I'd prefer to keep PRs focused more narrowly, and the word "mostly" makes it a little harder to review :)

@@ -193,7 +195,18 @@ export class ChatWidget extends Disposable implements IChatWidget {
return { text: '', parts: [] };
}

this.parsedChatRequest = this.instantiationService.createInstance(ChatRequestParser).parseChatRequest(this.viewModel!.sessionId, this.getInput(), this.location, { selectedAgent: this._lastSelectedAgent });
assertDefined(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary, this is right after a !this.viewModel check

@@ -1046,6 +1065,29 @@ export class ChatWidget extends Disposable implements IChatWidget {
this.telemetryService.publicLog2<ChatEditingWorkingSetEvent, ChatEditingWorkingSetClassification>('chatEditing/workingSetSize', { originalSize: this.inputPart.attemptedWorkingSetEntriesCount, actualSize: uniqueWorkingSetEntries.size });
}

// factor in nested references of dynamic variables into the implicit attached context
const variableModel = this.getContrib<ChatDynamicVariableModel>(ChatDynamicVariableModel.ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the ChatWidget calls to a contrib directly, then it's no longer a "contrib". The point of that model is to have something that can implement a feature on top of an object using its public interface, but is totally decoupled from the object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So either this should no longer be a 'contrib' or something else needs to change, I think I need to understand the overall thing better to decide.

/**
* A file reference token inside a prompt.
*/
export class FileReference extends BaseToken {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure workbench/common is the right place for these files, it all seems specific to the chat feature, so should it stay under workbench/contrib/chat/...?

* fileReference.dispose();
* ```
*/
export class PromptFileReference extends Disposable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is sort of a model object that doesn't depend on anything else in browser/ so can it go in the common/ layer?

* );
* ```
*/
export const randomInt = (max: number, min: number = 0): number => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good one for src/vs/base/common/numbers.ts?

}

if (typeof value === 'string') {
return value.trim().toLowerCase() === 'true';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this also expecting a string vs boolean?

@@ -51,7 +51,9 @@ class InputEditorDecorations extends Disposable {

this.updateInputEditorDecorations();
this._register(this.widget.inputEditor.onDidChangeModelContent(() => this.updateInputEditorDecorations()));
this._register(this.widget.onDidChangeParsedInput(() => this.updateInputEditorDecorations()));
this._register(this.widget.onDidChangeParsedInput(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessarily an improvement, we use the single-line version a lot

}

// get all file references in the file contents
const references = await this.codec.decode(fileStream.value).consumeAll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering whether this needs to be a stream? Do you use it as a stream?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just nice to have the stream since you are working from a file stream, just wondering if there is a case where you would use it that way

@roblourens
Copy link
Member

I can't get this to work- I set the setting, should I see intellisense for #file? If I manually type #file:./test.js, it doesn't include that file.

Also, it seems like this might break normal implicit context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants