@theia/ai-core 1.61.0-next.8 → 1.62.0-next.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/ai-core-frontend-module.d.ts.map +1 -1
- package/lib/browser/ai-core-frontend-module.js +7 -0
- package/lib/browser/ai-core-frontend-module.js.map +1 -1
- package/lib/browser/ai-variable-uri-label-provider.d.ts +17 -0
- package/lib/browser/ai-variable-uri-label-provider.d.ts.map +1 -0
- package/lib/browser/ai-variable-uri-label-provider.js +85 -0
- package/lib/browser/ai-variable-uri-label-provider.js.map +1 -0
- package/lib/browser/file-variable-contribution.d.ts +9 -3
- package/lib/browser/file-variable-contribution.d.ts.map +1 -1
- package/lib/browser/file-variable-contribution.js +26 -8
- package/lib/browser/file-variable-contribution.js.map +1 -1
- package/lib/browser/frontend-prompt-customization-service.d.ts +22 -1
- package/lib/browser/frontend-prompt-customization-service.d.ts.map +1 -1
- package/lib/browser/frontend-prompt-customization-service.js +63 -25
- package/lib/browser/frontend-prompt-customization-service.js.map +1 -1
- package/lib/browser/frontend-variable-service.d.ts +28 -4
- package/lib/browser/frontend-variable-service.d.ts.map +1 -1
- package/lib/browser/frontend-variable-service.js +84 -1
- package/lib/browser/frontend-variable-service.js.map +1 -1
- package/lib/common/ai-variable-resource.d.ts +18 -0
- package/lib/common/ai-variable-resource.d.ts.map +1 -0
- package/lib/common/ai-variable-resource.js +103 -0
- package/lib/common/ai-variable-resource.js.map +1 -0
- package/lib/common/configurable-in-memory-resources.d.ts +45 -0
- package/lib/common/configurable-in-memory-resources.d.ts.map +1 -0
- package/lib/common/configurable-in-memory-resources.js +147 -0
- package/lib/common/configurable-in-memory-resources.js.map +1 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +2 -0
- package/lib/common/index.js.map +1 -1
- package/lib/common/prompt-service.d.ts +16 -4
- package/lib/common/prompt-service.d.ts.map +1 -1
- package/lib/common/prompt-service.js +1 -1
- package/lib/common/prompt-service.js.map +1 -1
- package/lib/common/prompt-variable-contribution.d.ts +2 -1
- package/lib/common/prompt-variable-contribution.d.ts.map +1 -1
- package/lib/common/prompt-variable-contribution.js +10 -1
- package/lib/common/prompt-variable-contribution.js.map +1 -1
- package/lib/common/variable-service.d.ts +17 -2
- package/lib/common/variable-service.d.ts.map +1 -1
- package/lib/common/variable-service.js +43 -32
- package/lib/common/variable-service.js.map +1 -1
- package/package.json +11 -10
- package/src/browser/ai-core-frontend-module.ts +12 -5
- package/src/browser/ai-variable-uri-label-provider.ts +66 -0
- package/src/browser/file-variable-contribution.ts +34 -14
- package/src/browser/frontend-prompt-customization-service.ts +72 -25
- package/src/browser/frontend-variable-service.ts +115 -5
- package/src/common/ai-variable-resource.ts +86 -0
- package/src/common/configurable-in-memory-resources.ts +156 -0
- package/src/common/index.ts +2 -0
- package/src/common/prompt-service.ts +14 -4
- package/src/common/prompt-variable-contribution.ts +10 -2
- package/src/common/variable-service.ts +58 -44
|
@@ -519,46 +519,93 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati
|
|
|
519
519
|
}
|
|
520
520
|
|
|
521
521
|
async getCustomAgents(): Promise<CustomAgentDescription[]> {
|
|
522
|
-
const
|
|
522
|
+
const agentsById = new Map<string, CustomAgentDescription>();
|
|
523
|
+
// First, process additional (workspace) template directories to give them precedence
|
|
524
|
+
for (const dirPath of this.additionalTemplateDirs) {
|
|
525
|
+
const dirURI = URI.fromFilePath(dirPath);
|
|
526
|
+
await this.loadCustomAgentsFromDirectory(dirURI, agentsById);
|
|
527
|
+
}
|
|
528
|
+
// Then process global template directory (only adding agents that don't conflict)
|
|
529
|
+
const globalTemplateDir = await this.getTemplatesDirectoryURI();
|
|
530
|
+
await this.loadCustomAgentsFromDirectory(globalTemplateDir, agentsById);
|
|
531
|
+
// Return the merged list of agents
|
|
532
|
+
return Array.from(agentsById.values());
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Load custom agents from a specific directory
|
|
537
|
+
* @param directoryURI The URI of the directory to load from
|
|
538
|
+
* @param agentsById Map to store the loaded agents by ID
|
|
539
|
+
*/
|
|
540
|
+
protected async loadCustomAgentsFromDirectory(
|
|
541
|
+
directoryURI: URI,
|
|
542
|
+
agentsById: Map<string, CustomAgentDescription>
|
|
543
|
+
): Promise<void> {
|
|
544
|
+
const customAgentYamlUri = directoryURI.resolve('customAgents.yml');
|
|
523
545
|
const yamlExists = await this.fileService.exists(customAgentYamlUri);
|
|
524
546
|
if (!yamlExists) {
|
|
525
|
-
return
|
|
547
|
+
return;
|
|
526
548
|
}
|
|
527
|
-
|
|
549
|
+
|
|
528
550
|
try {
|
|
551
|
+
const fileContent = await this.fileService.read(customAgentYamlUri, { encoding: 'utf-8' });
|
|
529
552
|
const doc = load(fileContent.value);
|
|
553
|
+
|
|
530
554
|
if (!Array.isArray(doc) || !doc.every(entry => CustomAgentDescription.is(entry))) {
|
|
531
|
-
console.debug(
|
|
532
|
-
return
|
|
555
|
+
console.debug(`Invalid customAgents.yml file content in ${directoryURI.toString()}`);
|
|
556
|
+
return;
|
|
533
557
|
}
|
|
558
|
+
|
|
534
559
|
const readAgents = doc as CustomAgentDescription[];
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
return;
|
|
560
|
+
|
|
561
|
+
// Add agents to the map if they don't already exist
|
|
562
|
+
for (const agent of readAgents) {
|
|
563
|
+
if (!agentsById.has(agent.id)) {
|
|
564
|
+
agentsById.set(agent.id, agent);
|
|
541
565
|
}
|
|
542
|
-
|
|
543
|
-
uniqueAgents.push(agent);
|
|
544
|
-
});
|
|
545
|
-
return uniqueAgents;
|
|
566
|
+
}
|
|
546
567
|
} catch (e) {
|
|
547
|
-
console.debug(e.message
|
|
548
|
-
return [];
|
|
568
|
+
console.debug(`Error loading customAgents.yml from ${directoryURI.toString()}: ${e.message}`, e);
|
|
549
569
|
}
|
|
550
570
|
}
|
|
551
571
|
|
|
552
|
-
|
|
553
|
-
|
|
572
|
+
/**
|
|
573
|
+
* Returns all locations of existing customAgents.yml files and potential locations where
|
|
574
|
+
* new customAgents.yml files could be created.
|
|
575
|
+
*
|
|
576
|
+
* @returns An array of objects containing the URI and whether the file exists
|
|
577
|
+
*/
|
|
578
|
+
async getCustomAgentsLocations(): Promise<{ uri: URI, exists: boolean }[]> {
|
|
579
|
+
const locations: { uri: URI, exists: boolean }[] = [];
|
|
580
|
+
// Check global template directory
|
|
581
|
+
const globalTemplateDir = await this.getTemplatesDirectoryURI();
|
|
582
|
+
const globalAgentsUri = globalTemplateDir.resolve('customAgents.yml');
|
|
583
|
+
const globalExists = await this.fileService.exists(globalAgentsUri);
|
|
584
|
+
locations.push({ uri: globalAgentsUri, exists: globalExists });
|
|
585
|
+
// Check additional (workspace) template directories
|
|
586
|
+
for (const dirPath of this.additionalTemplateDirs) {
|
|
587
|
+
const dirURI = URI.fromFilePath(dirPath);
|
|
588
|
+
const agentsUri = dirURI.resolve('customAgents.yml');
|
|
589
|
+
const exists = await this.fileService.exists(agentsUri);
|
|
590
|
+
locations.push({ uri: agentsUri, exists: exists });
|
|
591
|
+
}
|
|
592
|
+
return locations;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Opens an existing customAgents.yml file at the given URI, or creates a new one if it doesn't exist.
|
|
597
|
+
*
|
|
598
|
+
* @param uri The URI of the customAgents.yml file to open or create
|
|
599
|
+
*/
|
|
600
|
+
async openCustomAgentYaml(uri: URI): Promise<void> {
|
|
554
601
|
const content = dump([templateEntry]);
|
|
555
|
-
if (! await this.fileService.exists(
|
|
556
|
-
await this.fileService.createFile(
|
|
602
|
+
if (! await this.fileService.exists(uri)) {
|
|
603
|
+
await this.fileService.createFile(uri, BinaryBuffer.fromString(content));
|
|
557
604
|
} else {
|
|
558
|
-
const fileContent = (await this.fileService.readFile(
|
|
559
|
-
await this.fileService.writeFile(
|
|
605
|
+
const fileContent = (await this.fileService.readFile(uri)).value;
|
|
606
|
+
await this.fileService.writeFile(uri, BinaryBuffer.concat([fileContent, BinaryBuffer.fromString(content)]));
|
|
560
607
|
}
|
|
561
|
-
const openHandler = await this.openerService.getOpener(
|
|
562
|
-
openHandler.open(
|
|
608
|
+
const openHandler = await this.openerService.getOpener(uri);
|
|
609
|
+
openHandler.open(uri);
|
|
563
610
|
}
|
|
564
611
|
}
|
|
@@ -14,10 +14,21 @@
|
|
|
14
14
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
15
|
// *****************************************************************************
|
|
16
16
|
|
|
17
|
-
import { Disposable } from '@theia/core';
|
|
18
|
-
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
|
|
19
|
-
import { injectable } from '@theia/core/shared/inversify';
|
|
20
|
-
import {
|
|
17
|
+
import { Disposable, MessageService, Prioritizeable } from '@theia/core';
|
|
18
|
+
import { FrontendApplicationContribution, OpenerService, open } from '@theia/core/lib/browser';
|
|
19
|
+
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
20
|
+
import {
|
|
21
|
+
AIVariable,
|
|
22
|
+
AIVariableArg,
|
|
23
|
+
AIVariableContext,
|
|
24
|
+
AIVariableOpener,
|
|
25
|
+
AIVariableResolutionRequest,
|
|
26
|
+
AIVariableResourceResolver,
|
|
27
|
+
AIVariableService,
|
|
28
|
+
DefaultAIVariableService,
|
|
29
|
+
PromptText
|
|
30
|
+
} from '../common';
|
|
31
|
+
import * as monaco from '@theia/monaco-editor-core';
|
|
21
32
|
|
|
22
33
|
export type AIVariableDropHandler = (event: DragEvent, context: AIVariableContext) => Promise<AIVariableDropResult | undefined>;
|
|
23
34
|
|
|
@@ -26,11 +37,52 @@ export interface AIVariableDropResult {
|
|
|
26
37
|
text?: string
|
|
27
38
|
};
|
|
28
39
|
|
|
40
|
+
export interface AIVariableCompletionContext {
|
|
41
|
+
/** Portion of user input to be used for filtering completion candidates. */
|
|
42
|
+
userInput: string;
|
|
43
|
+
/** The range of suggestion completions. */
|
|
44
|
+
range: monaco.Range
|
|
45
|
+
/** A prefix to be applied to each completion item's text */
|
|
46
|
+
prefix: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export namespace AIVariableCompletionContext {
|
|
50
|
+
export function get(
|
|
51
|
+
variableName: string,
|
|
52
|
+
model: monaco.editor.ITextModel,
|
|
53
|
+
position: monaco.Position,
|
|
54
|
+
matchString?: string
|
|
55
|
+
): AIVariableCompletionContext | undefined {
|
|
56
|
+
const lineContent = model.getLineContent(position.lineNumber);
|
|
57
|
+
const indexOfVariableTrigger = lineContent.lastIndexOf(matchString ?? PromptText.VARIABLE_CHAR, position.column - 1);
|
|
58
|
+
|
|
59
|
+
// check if there is a variable trigger and no space typed between the variable trigger and the cursor
|
|
60
|
+
if (indexOfVariableTrigger === -1 || lineContent.substring(indexOfVariableTrigger).includes(' ')) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// determine whether we are providing completions before or after the variable argument separator
|
|
65
|
+
const indexOfVariableArgSeparator = lineContent.lastIndexOf(PromptText.VARIABLE_SEPARATOR_CHAR, position.column - 1);
|
|
66
|
+
const triggerCharIndex = Math.max(indexOfVariableTrigger, indexOfVariableArgSeparator);
|
|
67
|
+
|
|
68
|
+
const userInput = lineContent.substring(triggerCharIndex + 1, position.column - 1);
|
|
69
|
+
const range = new monaco.Range(position.lineNumber, triggerCharIndex + 2, position.lineNumber, position.column);
|
|
70
|
+
const matchVariableChar = lineContent[triggerCharIndex] === (matchString ? matchString : PromptText.VARIABLE_CHAR);
|
|
71
|
+
const prefix = matchVariableChar ? variableName + PromptText.VARIABLE_SEPARATOR_CHAR : '';
|
|
72
|
+
return { range, userInput, prefix };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
29
76
|
export const FrontendVariableService = Symbol('FrontendVariableService');
|
|
30
77
|
export interface FrontendVariableService extends AIVariableService {
|
|
31
78
|
registerDropHandler(handler: AIVariableDropHandler): Disposable;
|
|
32
79
|
unregisterDropHandler(handler: AIVariableDropHandler): void;
|
|
33
80
|
getDropResult(event: DragEvent, context: AIVariableContext): Promise<AIVariableDropResult>;
|
|
81
|
+
|
|
82
|
+
registerOpener(variable: AIVariable, opener: AIVariableOpener): Disposable;
|
|
83
|
+
unregisterOpener(variable: AIVariable, opener: AIVariableOpener): void;
|
|
84
|
+
getOpener(name: string, arg: string | undefined, context: AIVariableContext): Promise<AIVariableOpener | undefined>;
|
|
85
|
+
open(variable: AIVariableArg, context?: AIVariableContext): Promise<void>
|
|
34
86
|
}
|
|
35
87
|
|
|
36
88
|
export interface FrontendVariableContribution {
|
|
@@ -38,9 +90,13 @@ export interface FrontendVariableContribution {
|
|
|
38
90
|
}
|
|
39
91
|
|
|
40
92
|
@injectable()
|
|
41
|
-
export class DefaultFrontendVariableService extends DefaultAIVariableService implements FrontendApplicationContribution {
|
|
93
|
+
export class DefaultFrontendVariableService extends DefaultAIVariableService implements FrontendApplicationContribution, FrontendVariableService {
|
|
42
94
|
protected dropHandlers = new Set<AIVariableDropHandler>();
|
|
43
95
|
|
|
96
|
+
@inject(MessageService) protected readonly messageService: MessageService;
|
|
97
|
+
@inject(AIVariableResourceResolver) protected readonly aiResourceResolver: AIVariableResourceResolver;
|
|
98
|
+
@inject(OpenerService) protected readonly openerService: OpenerService;
|
|
99
|
+
|
|
44
100
|
onStart(): void {
|
|
45
101
|
this.initContributions();
|
|
46
102
|
}
|
|
@@ -68,4 +124,58 @@ export class DefaultFrontendVariableService extends DefaultAIVariableService imp
|
|
|
68
124
|
}
|
|
69
125
|
return { variables, text };
|
|
70
126
|
}
|
|
127
|
+
|
|
128
|
+
registerOpener(variable: AIVariable, opener: AIVariableOpener): Disposable {
|
|
129
|
+
const key = this.getKey(variable.name);
|
|
130
|
+
if (!this.variables.get(key)) {
|
|
131
|
+
this.variables.set(key, variable);
|
|
132
|
+
this.onDidChangeVariablesEmitter.fire();
|
|
133
|
+
}
|
|
134
|
+
const openers = this.openers.get(key) ?? [];
|
|
135
|
+
openers.push(opener);
|
|
136
|
+
this.openers.set(key, openers);
|
|
137
|
+
return Disposable.create(() => this.unregisterOpener(variable, opener));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
unregisterOpener(variable: AIVariable, opener: AIVariableOpener): void {
|
|
141
|
+
const key = this.getKey(variable.name);
|
|
142
|
+
const registeredOpeners = this.openers.get(key);
|
|
143
|
+
registeredOpeners?.splice(registeredOpeners.indexOf(opener), 1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async getOpener(name: string, arg: string | undefined, context: AIVariableContext = {}): Promise<AIVariableOpener | undefined> {
|
|
147
|
+
const variable = this.getVariable(name);
|
|
148
|
+
return variable && Prioritizeable.prioritizeAll(
|
|
149
|
+
this.openers.get(this.getKey(name)) ?? [],
|
|
150
|
+
opener => (async () => opener.canOpen({ variable, arg }, context))().catch(() => 0)
|
|
151
|
+
)
|
|
152
|
+
.then(prioritized => prioritized.at(0)?.value);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async open(request: AIVariableArg, context?: AIVariableContext | undefined): Promise<void> {
|
|
156
|
+
const { variableName, arg } = this.parseRequest(request);
|
|
157
|
+
const variable = this.getVariable(variableName);
|
|
158
|
+
if (!variable) {
|
|
159
|
+
this.messageService.warn('No variable found for open request.');
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const opener = await this.getOpener(variableName, arg, context);
|
|
163
|
+
try {
|
|
164
|
+
return opener ? opener.open({ variable, arg }, context ?? {}) : this.openReadonly({ variable, arg }, context);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
console.error('Unable to open variable:', err);
|
|
167
|
+
this.messageService.error('Unable to display variable value.');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
protected async openReadonly(request: AIVariableResolutionRequest, context: AIVariableContext = {}): Promise<void> {
|
|
172
|
+
const resolved = await this.resolveVariable(request, context);
|
|
173
|
+
if (resolved === undefined) {
|
|
174
|
+
this.messageService.warn('Unable to resolve variable.');
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const resource = this.aiResourceResolver.getOrCreate(request, context, resolved.value);
|
|
178
|
+
await open(this.openerService, resource.uri);
|
|
179
|
+
resource.dispose();
|
|
180
|
+
}
|
|
71
181
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2025 EclipseSource GmbH and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import * as deepEqual from 'fast-deep-equal';
|
|
18
|
+
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
|
|
19
|
+
import { Resource, URI, generateUuid } from '@theia/core';
|
|
20
|
+
import { AIVariableContext, AIVariableResolutionRequest } from './variable-service';
|
|
21
|
+
import stableJsonStringify = require('fast-json-stable-stringify');
|
|
22
|
+
import { ConfigurableInMemoryResources, ConfigurableMutableReferenceResource } from './configurable-in-memory-resources';
|
|
23
|
+
|
|
24
|
+
export const AI_VARIABLE_RESOURCE_SCHEME = 'ai-variable';
|
|
25
|
+
export const NO_CONTEXT_AUTHORITY = 'context-free';
|
|
26
|
+
|
|
27
|
+
@injectable()
|
|
28
|
+
export class AIVariableResourceResolver {
|
|
29
|
+
@inject(ConfigurableInMemoryResources) protected readonly inMemoryResources: ConfigurableInMemoryResources;
|
|
30
|
+
|
|
31
|
+
@postConstruct()
|
|
32
|
+
protected init(): void {
|
|
33
|
+
this.inMemoryResources.onWillDispose(resource => this.cache.delete(resource.uri.toString()));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected readonly cache = new Map<string, [Resource, AIVariableContext]>();
|
|
37
|
+
|
|
38
|
+
getOrCreate(request: AIVariableResolutionRequest, context: AIVariableContext, value: string): ConfigurableMutableReferenceResource {
|
|
39
|
+
const uri = this.toUri(request, context);
|
|
40
|
+
try {
|
|
41
|
+
const existing = this.inMemoryResources.resolve(uri);
|
|
42
|
+
existing.update({ contents: value });
|
|
43
|
+
return existing;
|
|
44
|
+
} catch { /* No-op */ }
|
|
45
|
+
const fresh = this.inMemoryResources.add(uri, { contents: value, readOnly: true, initiallyDirty: false });
|
|
46
|
+
const key = uri.toString();
|
|
47
|
+
this.cache.set(key, [fresh, context]);
|
|
48
|
+
return fresh;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected toUri(request: AIVariableResolutionRequest, context: AIVariableContext): URI {
|
|
52
|
+
return URI.fromComponents({
|
|
53
|
+
scheme: AI_VARIABLE_RESOURCE_SCHEME,
|
|
54
|
+
query: stableJsonStringify({ arg: request.arg, name: request.variable.name }),
|
|
55
|
+
path: '/',
|
|
56
|
+
authority: this.toAuthority(context),
|
|
57
|
+
fragment: ''
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected toAuthority(context: AIVariableContext): string {
|
|
62
|
+
try {
|
|
63
|
+
if (deepEqual(context, {})) { return NO_CONTEXT_AUTHORITY; }
|
|
64
|
+
for (const [resource, cachedContext] of this.cache.values()) {
|
|
65
|
+
if (deepEqual(context, cachedContext)) {
|
|
66
|
+
return resource.uri.authority;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
} catch (err) {
|
|
70
|
+
// Mostly that deep equal could overflow the stack, but it should run into === or inequality before that.
|
|
71
|
+
console.warn('Problem evaluating context in AIVariableResourceResolver', err);
|
|
72
|
+
}
|
|
73
|
+
return generateUuid();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fromUri(uri: URI): { variableName: string, arg: string | undefined } | undefined {
|
|
77
|
+
if (uri.scheme !== AI_VARIABLE_RESOURCE_SCHEME) { return undefined; }
|
|
78
|
+
try {
|
|
79
|
+
const { name: variableName, arg } = JSON.parse(uri.query);
|
|
80
|
+
return variableName ? {
|
|
81
|
+
variableName,
|
|
82
|
+
arg,
|
|
83
|
+
} : undefined;
|
|
84
|
+
} catch { return undefined; }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2025 EclispeSource GmbH and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { injectable } from '@theia/core/shared/inversify';
|
|
18
|
+
import { SyncReferenceCollection, Reference, ResourceResolver, Resource, Event, Emitter, URI } from '@theia/core';
|
|
19
|
+
import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
|
|
20
|
+
|
|
21
|
+
@injectable()
|
|
22
|
+
/** For creating highly configurable in-memory resources */
|
|
23
|
+
export class ConfigurableInMemoryResources implements ResourceResolver {
|
|
24
|
+
|
|
25
|
+
protected readonly resources = new SyncReferenceCollection<string, ConfigurableMutableResource>(uri => new ConfigurableMutableResource(new URI(uri)));
|
|
26
|
+
|
|
27
|
+
get onWillDispose(): Event<ConfigurableMutableResource> {
|
|
28
|
+
return this.resources.onWillDispose;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
add(uri: URI, options: ResourceInitializationOptions): ConfigurableMutableReferenceResource {
|
|
32
|
+
const resourceUri = uri.toString();
|
|
33
|
+
if (this.resources.has(resourceUri)) {
|
|
34
|
+
throw new Error(`Cannot add already existing in-memory resource '${resourceUri}'`);
|
|
35
|
+
}
|
|
36
|
+
const resource = this.acquire(resourceUri);
|
|
37
|
+
resource.update(options);
|
|
38
|
+
return resource;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
update(uri: URI, options: ResourceInitializationOptions): Resource {
|
|
42
|
+
const resourceUri = uri.toString();
|
|
43
|
+
const resource = this.resources.get(resourceUri);
|
|
44
|
+
if (!resource) {
|
|
45
|
+
throw new Error(`Cannot update non-existent in-memory resource '${resourceUri}'`);
|
|
46
|
+
}
|
|
47
|
+
resource.update(options);
|
|
48
|
+
return resource;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
resolve(uri: URI): ConfigurableMutableReferenceResource {
|
|
52
|
+
const uriString = uri.toString();
|
|
53
|
+
if (!this.resources.has(uriString)) {
|
|
54
|
+
throw new Error(`In memory '${uriString}' resource does not exist.`);
|
|
55
|
+
}
|
|
56
|
+
return this.acquire(uriString);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected acquire(uri: string): ConfigurableMutableReferenceResource {
|
|
60
|
+
const reference = this.resources.acquire(uri);
|
|
61
|
+
return new ConfigurableMutableReferenceResource(reference);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type ResourceInitializationOptions = Pick<Resource, 'autosaveable' | 'initiallyDirty' | 'readOnly'>
|
|
66
|
+
& { contents?: string | Promise<string>, onSave?: Resource['saveContents'] };
|
|
67
|
+
|
|
68
|
+
export class ConfigurableMutableResource implements Resource {
|
|
69
|
+
protected readonly onDidChangeContentsEmitter = new Emitter<void>();
|
|
70
|
+
readonly onDidChangeContents = this.onDidChangeContentsEmitter.event;
|
|
71
|
+
protected fireDidChangeContents(): void {
|
|
72
|
+
this.onDidChangeContentsEmitter.fire();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected readonly onDidChangeReadonlyEmitter = new Emitter<boolean | MarkdownString>();
|
|
76
|
+
readonly onDidChangeReadOnly = this.onDidChangeReadonlyEmitter.event;
|
|
77
|
+
|
|
78
|
+
constructor(readonly uri: URI, protected options?: ResourceInitializationOptions) { }
|
|
79
|
+
|
|
80
|
+
get readOnly(): Resource['readOnly'] {
|
|
81
|
+
return this.options?.readOnly;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get autosaveable(): boolean {
|
|
85
|
+
return this.options?.autosaveable !== false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get initiallyDirty(): boolean {
|
|
89
|
+
return !!this.options?.initiallyDirty;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
readContents(): Promise<string> {
|
|
93
|
+
return Promise.resolve(this.options?.contents ?? '');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async saveContents(contents: string): Promise<void> {
|
|
97
|
+
await this.options?.onSave?.(contents);
|
|
98
|
+
this.update({ contents });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
update(options: ResourceInitializationOptions): void {
|
|
102
|
+
const didContentsChange = 'contents' in options && options.contents !== this.options?.contents;
|
|
103
|
+
const didReadOnlyChange = 'readOnly' in options && options.readOnly !== this.options?.readOnly;
|
|
104
|
+
this.options = { ...this.options, ...options };
|
|
105
|
+
if (didContentsChange) {
|
|
106
|
+
this.onDidChangeContentsEmitter.fire();
|
|
107
|
+
}
|
|
108
|
+
if (didReadOnlyChange) {
|
|
109
|
+
this.onDidChangeReadonlyEmitter.fire(this.readOnly ?? false);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
dispose(): void {
|
|
114
|
+
this.onDidChangeContentsEmitter.dispose();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export class ConfigurableMutableReferenceResource implements Resource {
|
|
119
|
+
constructor(protected reference: Reference<ConfigurableMutableResource>) { }
|
|
120
|
+
|
|
121
|
+
get uri(): URI {
|
|
122
|
+
return this.reference.object.uri;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
get onDidChangeContents(): Event<void> {
|
|
126
|
+
return this.reference.object.onDidChangeContents;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
dispose(): void {
|
|
130
|
+
this.reference.dispose();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
readContents(): Promise<string> {
|
|
134
|
+
return this.reference.object.readContents();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
saveContents(contents: string): Promise<void> {
|
|
138
|
+
return this.reference.object.saveContents(contents);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
update(options: ResourceInitializationOptions): void {
|
|
142
|
+
this.reference.object.update(options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get readOnly(): Resource['readOnly'] {
|
|
146
|
+
return this.reference.object.readOnly;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get initiallyDirty(): boolean {
|
|
150
|
+
return this.reference.object.initiallyDirty;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
get autosaveable(): boolean {
|
|
154
|
+
return this.reference.object.autosaveable;
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/common/index.ts
CHANGED
|
@@ -84,7 +84,7 @@ export interface PromptService {
|
|
|
84
84
|
*
|
|
85
85
|
* @param id the id of the prompt
|
|
86
86
|
* @param args the object with placeholders, mapping the placeholder key to the value
|
|
87
|
-
* @param context the {@link AIVariableContext} to use during variable
|
|
87
|
+
* @param context the {@link AIVariableContext} to use during variable resolution
|
|
88
88
|
* @param resolveVariable the variable resolving method. Fall back to using the {@link AIVariableService} if not given.
|
|
89
89
|
*/
|
|
90
90
|
getPromptFragment(
|
|
@@ -202,9 +202,19 @@ export interface PromptCustomizationService {
|
|
|
202
202
|
readonly onDidChangeCustomAgents: Event<void>;
|
|
203
203
|
|
|
204
204
|
/**
|
|
205
|
-
*
|
|
205
|
+
* Returns all locations of existing customAgents.yml files and potential locations where
|
|
206
|
+
* new customAgents.yml files could be created.
|
|
207
|
+
*
|
|
208
|
+
* @returns An array of objects containing the URI and whether the file exists
|
|
209
|
+
*/
|
|
210
|
+
getCustomAgentsLocations(): Promise<{ uri: URI, exists: boolean }[]>;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Opens an existing customAgents.yml file at the given URI, or creates a new one if it doesn't exist.
|
|
214
|
+
*
|
|
215
|
+
* @param uri The URI of the customAgents.yml file to open or create
|
|
206
216
|
*/
|
|
207
|
-
openCustomAgentYaml(): void
|
|
217
|
+
openCustomAgentYaml(uri: URI): Promise<void>;
|
|
208
218
|
}
|
|
209
219
|
|
|
210
220
|
@injectable()
|
|
@@ -331,7 +341,7 @@ export class PromptServiceImpl implements PromptService {
|
|
|
331
341
|
*
|
|
332
342
|
* @param template the unresolved template text
|
|
333
343
|
* @param args the object with placeholders, mapping the placeholder key to the value
|
|
334
|
-
* @param context the {@link AIVariableContext} to use during variable
|
|
344
|
+
* @param context the {@link AIVariableContext} to use during variable resolution
|
|
335
345
|
* @param resolveVariable the variable resolving method. Fall back to using the {@link AIVariableService} if not given.
|
|
336
346
|
*/
|
|
337
347
|
protected async getVariableAndArgReplacements(
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
//
|
|
14
14
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
15
|
// *****************************************************************************
|
|
16
|
-
import { CommandService, nls } from '@theia/core';
|
|
16
|
+
import { CommandService, ILogger, nls } from '@theia/core';
|
|
17
17
|
import { injectable, inject, optional } from '@theia/core/shared/inversify';
|
|
18
18
|
import * as monaco from '@theia/monaco-editor-core';
|
|
19
19
|
import {
|
|
@@ -50,6 +50,9 @@ export class PromptVariableContribution implements AIVariableContribution, AIVar
|
|
|
50
50
|
@inject(PromptCustomizationService) @optional()
|
|
51
51
|
protected readonly promptCustomizationService: PromptCustomizationService;
|
|
52
52
|
|
|
53
|
+
@inject(ILogger)
|
|
54
|
+
protected logger: ILogger;
|
|
55
|
+
|
|
53
56
|
registerVariables(service: AIVariableService): void {
|
|
54
57
|
service.registerResolver(PROMPT_VARIABLE, this);
|
|
55
58
|
service.registerArgumentPicker(PROMPT_VARIABLE, this.triggerArgumentPicker.bind(this));
|
|
@@ -81,7 +84,12 @@ export class PromptVariableContribution implements AIVariableContribution, AIVar
|
|
|
81
84
|
}
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
|
-
|
|
87
|
+
this.logger.debug(`Could not resolve prompt variable '${request.variable.name}' with arg '${request.arg}'. Returning empty string.`);
|
|
88
|
+
return {
|
|
89
|
+
variable: request.variable,
|
|
90
|
+
value: '',
|
|
91
|
+
allResolvedDependencies: []
|
|
92
|
+
};
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
protected async triggerArgumentPicker(): Promise<string | undefined> {
|