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

Allow adding variant via prompttemplate files #14509

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@ export const TemplateRenderer: React.FC<TemplateRendererProps> = ({
promptService,
aiSettingsService,
}) => {
const [variantIds, setVariantIds] = React.useState<string[]>([]);
const variantIds = [DEFAULT_VARIANT, ...promptService.getVariantIds(template.id)];
const [selectedVariant, setSelectedVariant] = React.useState<string>(DEFAULT_VARIANT);

React.useEffect(() => {
React.useMemo(() => {
sdirix marked this conversation as resolved.
Show resolved Hide resolved
(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<HTMLSelectElement>) => {
const newVariant = event.target.value;
Expand All @@ -68,16 +67,16 @@ export const TemplateRenderer: React.FC<TemplateRendererProps> = ({
});
};

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 (
<div className="template-renderer">
Expand All @@ -92,10 +91,15 @@ export const TemplateRenderer: React.FC<TemplateRendererProps> = ({
</label>
<select
id={`variant-selector-${template.id}`}
className="theia-select template-variant-selector"
value={selectedVariant}
className={`theia-select template-variant-selector ${isInvalidVariant ? 'error' : ''}`}
value={isInvalidVariant ? 'invalid' : selectedVariant}
onChange={handleVariantChange}
>
{isInvalidVariant && (
<option value="invalid" disabled>
The selected variant is no longer available
</option>
)}
{variantIds.map(variantId => (
<option key={variantId} value={variantId}>
{variantId}
Expand All @@ -104,10 +108,18 @@ export const TemplateRenderer: React.FC<TemplateRendererProps> = ({
</select>
</>
)}
<button className="theia-button main" onClick={openTemplate}>
<button
className="theia-button main"
onClick={openTemplate}
disabled={isInvalidVariant}
>
Edit
</button>
<button className="theia-button secondary" onClick={resetTemplate}>
<button
className="theia-button secondary"
onClick={resetTemplate}
disabled={isInvalidVariant}
>
Reset
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const editorUri = await this.getTemplateURI(id);
if (! await this.fileService.exists(editorUri)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/ai-core/src/browser/style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion packages/ai-core/src/common/prompt-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Loading