fix running prompt without implicit context

This commit is contained in:
Martin Aeschlimann
2025-04-15 22:07:34 +02:00
committed by Oleg Solomko
parent 64f8ad98cd
commit 09ef9f2e4c
3 changed files with 20 additions and 15 deletions

View File

@@ -8,7 +8,6 @@ import { IChatWidget } from '../../../../../chat.js';
import { getChatWidgetObject } from './attachInstructions.js';
import { URI } from '../../../../../../../../../base/common/uri.js';
import { extUri } from '../../../../../../../../../base/common/resources.js';
import { assertDefined } from '../../../../../../../../../base/common/types.js';
import { IViewsService } from '../../../../../../../../services/views/common/viewsService.js';
import { ICommandService } from '../../../../../../../../../platform/commands/common/commands.js';
@@ -48,7 +47,7 @@ export const runPromptFile = async (
const widget = await getChatWidgetObject(options);
let wasAlreadyAttached = true;
if (isAttachedAsCurrentPrompt(file, widget) === false) {
if (isSetInImplicitContext(file, widget) === false) {
wasAlreadyAttached = widget
.attachmentModel
.promptInstructions
@@ -68,10 +67,9 @@ export const runPromptFile = async (
};
/**
* Check if provided uri is already attached to chat
* input as an implicit "current file" context.
* Check if provided uri is already set as the implicit context
*/
const isAttachedAsCurrentPrompt = (
const isSetInImplicitContext = (
promptUri: URI,
widget: IChatWidget,
): boolean => {
@@ -80,18 +78,19 @@ const isAttachedAsCurrentPrompt = (
return false;
}
if (implicitContext.isPromptFile === false) {
if (implicitContext.value === undefined) {
// the user turned off implicit context in the settings
return false;
}
if (implicitContext.enabled === false) {
// the user disabled the implicit context in the chat view
return false;
}
assertDefined(
implicitContext.value,
'Prompt value must always be defined.',
);
if (implicitContext.isPromptFile === false) {
return false;
}
const uri = URI.isUri(implicitContext.value)
? implicitContext.value

View File

@@ -9,9 +9,11 @@ import { ResourceLabels } from '../../../../../browser/labels.js';
import { Disposable } from '../../../../../../base/common/lifecycle.js';
import { ILogService } from '../../../../../../platform/log/common/log.js';
import { InstructionsAttachmentWidget } from './promptInstructionsWidget.js';
import { getPromptFileType } from '../../../../../../platform/prompts/common/constants.js';
import { IInstantiationService } from '../../../../../../platform/instantiation/common/instantiation.js';
import { ChatPromptAttachmentsCollection } from '../../chatAttachmentModel/chatPromptAttachmentsCollection.js';
import { ILanguageService } from '../../../../../../editor/common/languages/language.js';
import { IModelService } from '../../../../../../editor/common/services/model.js';
import { PROMPT_LANGUAGE_ID } from '../../../common/promptSyntax/constants.js';
/**
* Widget for a collection of prompt instructions attachments.
@@ -72,7 +74,9 @@ export class PromptInstructionsAttachmentsCollectionWidget extends Disposable {
*/
public get hasPromptFile(): boolean {
return this.references.some((uri) => {
return getPromptFileType(uri) === 'prompt';
const model = this.modelService.getModel(uri);
const languageId = model ? model.getLanguageId() : this.languageService.guessLanguageIdByFilepathOrFirstLine(uri);
return languageId === PROMPT_LANGUAGE_ID;
});
}
@@ -80,6 +84,8 @@ export class PromptInstructionsAttachmentsCollectionWidget extends Disposable {
private readonly model: ChatPromptAttachmentsCollection,
private readonly resourceLabels: ResourceLabels,
@IInstantiationService private readonly initService: IInstantiationService,
@ILanguageService private readonly languageService: ILanguageService,
@IModelService private readonly modelService: IModelService,
@ILogService private readonly logService: ILogService,
) {
super();

View File

@@ -224,11 +224,11 @@ export class ChatImplicitContextContribution extends Disposable implements IWork
const setting = this._implicitContextEnablement[widget.location];
const isFirstInteraction = widget.viewModel?.getItems().length === 0;
if (setting === 'first' && !isFirstInteraction) {
widget.input.implicitContext.setValue(undefined, false, languageId);
widget.input.implicitContext.setValue(undefined, false, undefined);
} else if (setting === 'always' || setting === 'first' && isFirstInteraction) {
widget.input.implicitContext.setValue(newValue, isSelection, languageId);
} else if (setting === 'never') {
widget.input.implicitContext.setValue(undefined, false, languageId);
widget.input.implicitContext.setValue(undefined, false, undefined);
}
}
}
@@ -305,7 +305,7 @@ export class ChatImplicitContext extends Disposable implements IChatRequestImpli
return this._isSelection;
}
private _onDidChangeValue = new Emitter<void>();
private _onDidChangeValue = this._register(new Emitter<void>());
readonly onDidChangeValue = this._onDidChangeValue.event;
private _value: Location | URI | undefined;