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 variable support with #{} syntax in prompttemplate #15026

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions packages/ai-core/data/prompttemplate.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@
"match": "[a-zA-Z_][a-zA-Z0-9_\\-]*"
}
]
},
{
"name": "variable.parameter.prompttemplate",
"begin": "#{",
"beginCaptures": {
"0": {
"name": "punctuation.definition.variable.begin"
}
},
"end": "}",
"endCaptures": {
"0": {
"name": "punctuation.definition.variable.end"
}
},
"patterns": [
{
"name": "keyword.control",
"match": "[a-zA-Z_][a-zA-Z0-9_\\-]*"
}
]
}
],
"repository": {},
Expand Down
30 changes: 28 additions & 2 deletions packages/ai-core/src/browser/prompttemplate-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { codicon, Widget } from '@theia/core/lib/browser';
import { EditorWidget, ReplaceOperation } from '@theia/editor/lib/browser';
import { PromptCustomizationService, PromptService, ToolInvocationRegistry } from '../common';
import { ProviderResult } from '@theia/monaco-editor-core/esm/vs/editor/common/languages';
import { AIVariableService } from '../common/variable-service';

const PROMPT_TEMPLATE_LANGUAGE_ID = 'theia-ai-prompt-template';
const PROMPT_TEMPLATE_TEXTMATE_SCOPE = 'source.prompttemplate';
Expand Down Expand Up @@ -59,19 +60,25 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
@inject(ToolInvocationRegistry)
protected readonly toolInvocationRegistry: ToolInvocationRegistry;

@inject(AIVariableService)
protected readonly variableService: AIVariableService;

readonly config: monaco.languages.LanguageConfiguration =
{
'brackets': [
['${', '}'],
['~{', '}']
['~{', '}'],
['#{', '}']
],
'autoClosingPairs': [
{ 'open': '${', 'close': '}' },
{ 'open': '~{', 'close': '}' },
{ 'open': '#{', 'close': '}' }
],
'surroundingPairs': [
{ 'open': '${', 'close': '}' },
{ 'open': '~{', 'close': '}' }
{ 'open': '~{', 'close': '}' },
{ 'open': '#{', 'close': '}' }
]
};

Expand All @@ -95,6 +102,12 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
provideCompletionItems: (model, position, _context, _token): ProviderResult<monaco.languages.CompletionList> => this.provideFunctionCompletions(model, position),
});

monaco.languages.registerCompletionItemProvider(PROMPT_TEMPLATE_LANGUAGE_ID, {
// Monaco only supports single character trigger characters
triggerCharacters: ['{'],
provideCompletionItems: (model, position, _context, _token): ProviderResult<monaco.languages.CompletionList> => this.provideVariableCompletions(model, position),
});

const textmateGrammar = require('../../data/prompttemplate.tmLanguage.json');
const grammarDefinitionProvider: GrammarDefinitionProvider = {
getGrammarDefinition: function (): Promise<GrammarDefinition> {
Expand Down Expand Up @@ -122,6 +135,19 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
);
}

provideVariableCompletions(model: monaco.editor.ITextModel, position: monaco.Position): ProviderResult<monaco.languages.CompletionList> {
return this.getSuggestions(
model,
position,
'#{',
this.variableService.getVariables(),
monaco.languages.CompletionItemKind.Variable,
variable => variable.id,
variable => variable.name,
variable => variable.description ?? ''
);
}

getCompletionRange(model: monaco.editor.ITextModel, position: monaco.Position, triggerCharacters: string): monaco.Range | undefined {
// Check if the characters before the current position are the trigger characters
const lineContent = model.getLineContent(position.lineNumber);
Expand Down
Loading