From 906a9b1e9ac95b5360ad323a930f28aa185ddd84 Mon Sep 17 00:00:00 2001 From: Jonas Helming Date: Sun, 24 Nov 2024 01:49:25 +0100 Subject: [PATCH 1/4] Allow adding variant via prompttemplate files fixed #14507 Signed-off-by: Jonas Helming --- .../browser/frontend-prompt-customization-service.ts | 4 ++++ packages/ai-core/src/common/prompt-service.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/ai-core/src/browser/frontend-prompt-customization-service.ts b/packages/ai-core/src/browser/frontend-prompt-customization-service.ts index 6ffe3969df378..7a2691d4206a5 100644 --- a/packages/ai-core/src/browser/frontend-prompt-customization-service.ts +++ b/packages/ai-core/src/browser/frontend-prompt-customization-service.ts @@ -169,6 +169,10 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati return this.templates.get(id); } + getCustomPromptTemplateIDs(): string[] { + return Array.from(this.templates.keys()); + } + async editTemplate(id: string, defaultContent?: string): Promise { const editorUri = await this.getTemplateURI(id); if (! await this.fileService.exists(editorUri)) { diff --git a/packages/ai-core/src/common/prompt-service.ts b/packages/ai-core/src/common/prompt-service.ts index aef16da8fdc50..8d89c6274e8e4 100644 --- a/packages/ai-core/src/common/prompt-service.ts +++ b/packages/ai-core/src/common/prompt-service.ts @@ -136,6 +136,7 @@ export interface PromptCustomizationService { */ getCustomizedPromptTemplate(id: string): string | undefined + getCustomPromptTemplateIDs(): string[]; /** * Edit the template. If the content is specified, is will be * used to customize the template. Otherwise, the behavior depends @@ -314,9 +315,18 @@ export class PromptServiceImpl implements PromptService { delete this._prompts[id]; } getVariantIds(id: string): string[] { - return Object.values(this._prompts) + const allCustomPromptTemplateIds = this.customizationService?.getCustomPromptTemplateIDs() || []; + const knownPromptIds = Object.keys(this._prompts); + + // We filter out known IDs from the custom prompt template IDs, these are no variants, but customizations. Then we retain IDs that start with the main ID + const customVariantIds = allCustomPromptTemplateIds.filter(customId => + !knownPromptIds.includes(customId) && customId.startsWith(id) + ); + const variantIds = Object.values(this._prompts) .filter(prompt => prompt.variantOf === id) .map(variant => variant.id); + + return [...variantIds, ...customVariantIds]; } storePromptTemplate(promptTemplate: PromptTemplate): void { this._prompts[promptTemplate.id] = promptTemplate; From eb68af56c987380ff9341b6c2a4bba93410cd0ac Mon Sep 17 00:00:00 2001 From: Jonas Helming Date: Wed, 27 Nov 2024 21:57:29 +0100 Subject: [PATCH 2/4] Adress review feedback bug when deleting prompt variants Signed-off-by: Jonas Helming --- .../template-settings-renderer.tsx | 40 ++++++++++++------- packages/ai-core/src/browser/style/index.css | 6 ++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx b/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx index 3465e68e7286a..42f0af277be3b 100644 --- a/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx +++ b/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx @@ -34,20 +34,19 @@ export const TemplateRenderer: React.FC = ({ promptService, aiSettingsService, }) => { - const [variantIds, setVariantIds] = React.useState([]); + const variantIds = [DEFAULT_VARIANT, ...promptService.getVariantIds(template.id)]; const [selectedVariant, setSelectedVariant] = React.useState(DEFAULT_VARIANT); - React.useEffect(() => { + React.useMemo(() => { (async () => { - const variants = promptService.getVariantIds(template.id); - setVariantIds([DEFAULT_VARIANT, ...variants]); - const agentSettings = await aiSettingsService.getAgentSettings(agentId); const currentVariant = agentSettings?.selectedVariants?.[template.id] || DEFAULT_VARIANT; setSelectedVariant(currentVariant); })(); - }, [template.id, promptService, aiSettingsService, agentId]); + }, [template.id, aiSettingsService, agentId]); + + const isInvalidVariant = !variantIds.includes(selectedVariant); const handleVariantChange = async (event: React.ChangeEvent) => { const newVariant = event.target.value; @@ -68,16 +67,16 @@ export const TemplateRenderer: React.FC = ({ }); }; - const openTemplate = React.useCallback(async () => { + const openTemplate = () => { const templateId = selectedVariant === DEFAULT_VARIANT ? template.id : selectedVariant; const selectedTemplate = promptService.getRawPrompt(templateId); promptCustomizationService.editTemplate(templateId, selectedTemplate?.template || ''); - }, [selectedVariant, template.id, promptService, promptCustomizationService]); + }; - const resetTemplate = React.useCallback(async () => { + const resetTemplate = () => { const templateId = selectedVariant === DEFAULT_VARIANT ? template.id : selectedVariant; promptCustomizationService.resetTemplate(templateId); - }, [selectedVariant, template.id, promptCustomizationService]); + }; return (
@@ -92,10 +91,15 @@ export const TemplateRenderer: React.FC = ({ )} - -
diff --git a/packages/ai-core/src/browser/style/index.css b/packages/ai-core/src/browser/style/index.css index a42e41b29a9a2..bcd7437046a30 100644 --- a/packages/ai-core/src/browser/style/index.css +++ b/packages/ai-core/src/browser/style/index.css @@ -48,7 +48,11 @@ min-width: 120px; } - +.template-variant-selector.error { + border-color: var(--theia-errorForeground); + background-color: var(--theia-errorBackground, rgba(255, 0, 0, 0.1)); + color: var(--theia-errorForeground); +} #ai-variable-configuration-container-widget, #ai-agent-configuration-container-widget { From 1bda65c40036d4bb91276aa669aa4c11a5c1578a Mon Sep 17 00:00:00 2001 From: Jonas Helming Date: Wed, 27 Nov 2024 22:51:02 +0100 Subject: [PATCH 3/4] Fix case if last custom variant gets deleted Signed-off-by: Jonas Helming --- .../src/browser/ai-configuration/template-settings-renderer.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx b/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx index 42f0af277be3b..593bcbf556885 100644 --- a/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx +++ b/packages/ai-core/src/browser/ai-configuration/template-settings-renderer.tsx @@ -84,7 +84,7 @@ export const TemplateRenderer: React.FC = ({ {template.id}
- {variantIds.length > 1 && ( + {(variantIds.length > 1 || isInvalidVariant) && ( <>