@theia/ai-chat 1.54.0

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.
Files changed (77) hide show
  1. package/README.md +30 -0
  2. package/lib/browser/ai-chat-frontend-module.d.ts +4 -0
  3. package/lib/browser/ai-chat-frontend-module.d.ts.map +1 -0
  4. package/lib/browser/ai-chat-frontend-module.js +51 -0
  5. package/lib/browser/ai-chat-frontend-module.js.map +1 -0
  6. package/lib/browser/ai-chat-preferences.d.ts +4 -0
  7. package/lib/browser/ai-chat-preferences.d.ts.map +1 -0
  8. package/lib/browser/ai-chat-preferences.js +32 -0
  9. package/lib/browser/ai-chat-preferences.js.map +1 -0
  10. package/lib/browser/frontend-chat-service.d.ts +8 -0
  11. package/lib/browser/frontend-chat-service.d.ts.map +1 -0
  12. package/lib/browser/frontend-chat-service.js +67 -0
  13. package/lib/browser/frontend-chat-service.js.map +1 -0
  14. package/lib/common/chat-agent-service.d.ts +34 -0
  15. package/lib/common/chat-agent-service.d.ts.map +1 -0
  16. package/lib/common/chat-agent-service.js +75 -0
  17. package/lib/common/chat-agent-service.js.map +1 -0
  18. package/lib/common/chat-agents-variable-contribution.d.ts +17 -0
  19. package/lib/common/chat-agents-variable-contribution.d.ts.map +1 -0
  20. package/lib/common/chat-agents-variable-contribution.js +51 -0
  21. package/lib/common/chat-agents-variable-contribution.js.map +1 -0
  22. package/lib/common/chat-agents.d.ts +86 -0
  23. package/lib/common/chat-agents.d.ts.map +1 -0
  24. package/lib/common/chat-agents.js +307 -0
  25. package/lib/common/chat-agents.js.map +1 -0
  26. package/lib/common/chat-model.d.ts +319 -0
  27. package/lib/common/chat-model.d.ts.map +1 -0
  28. package/lib/common/chat-model.js +527 -0
  29. package/lib/common/chat-model.js.map +1 -0
  30. package/lib/common/chat-request-parser.d.ts +20 -0
  31. package/lib/common/chat-request-parser.d.ts.map +1 -0
  32. package/lib/common/chat-request-parser.js +158 -0
  33. package/lib/common/chat-request-parser.js.map +1 -0
  34. package/lib/common/chat-request-parser.spec.d.ts +2 -0
  35. package/lib/common/chat-request-parser.spec.d.ts.map +1 -0
  36. package/lib/common/chat-request-parser.spec.js +108 -0
  37. package/lib/common/chat-request-parser.spec.js.map +1 -0
  38. package/lib/common/chat-service.d.ts +72 -0
  39. package/lib/common/chat-service.d.ts.map +1 -0
  40. package/lib/common/chat-service.js +170 -0
  41. package/lib/common/chat-service.js.map +1 -0
  42. package/lib/common/command-chat-agents.d.ts +33 -0
  43. package/lib/common/command-chat-agents.d.ts.map +1 -0
  44. package/lib/common/command-chat-agents.js +327 -0
  45. package/lib/common/command-chat-agents.js.map +1 -0
  46. package/lib/common/index.d.ts +10 -0
  47. package/lib/common/index.d.ts.map +1 -0
  48. package/lib/common/index.js +28 -0
  49. package/lib/common/index.js.map +1 -0
  50. package/lib/common/orchestrator-chat-agent.d.ts +22 -0
  51. package/lib/common/orchestrator-chat-agent.d.ts.map +1 -0
  52. package/lib/common/orchestrator-chat-agent.js +140 -0
  53. package/lib/common/orchestrator-chat-agent.js.map +1 -0
  54. package/lib/common/parsed-chat-request.d.ts +66 -0
  55. package/lib/common/parsed-chat-request.d.ts.map +1 -0
  56. package/lib/common/parsed-chat-request.js +83 -0
  57. package/lib/common/parsed-chat-request.js.map +1 -0
  58. package/lib/common/universal-chat-agent.d.ts +15 -0
  59. package/lib/common/universal-chat-agent.d.ts.map +1 -0
  60. package/lib/common/universal-chat-agent.js +102 -0
  61. package/lib/common/universal-chat-agent.js.map +1 -0
  62. package/package.json +54 -0
  63. package/src/browser/ai-chat-frontend-module.ts +66 -0
  64. package/src/browser/ai-chat-preferences.ts +32 -0
  65. package/src/browser/frontend-chat-service.ts +66 -0
  66. package/src/common/chat-agent-service.ts +85 -0
  67. package/src/common/chat-agents-variable-contribution.ts +81 -0
  68. package/src/common/chat-agents.ts +384 -0
  69. package/src/common/chat-model.ts +776 -0
  70. package/src/common/chat-request-parser.spec.ts +120 -0
  71. package/src/common/chat-request-parser.ts +220 -0
  72. package/src/common/chat-service.ts +236 -0
  73. package/src/common/command-chat-agents.ts +352 -0
  74. package/src/common/index.ts +24 -0
  75. package/src/common/orchestrator-chat-agent.ts +151 -0
  76. package/src/common/parsed-chat-request.ts +112 -0
  77. package/src/common/universal-chat-agent.ts +109 -0
@@ -0,0 +1,352 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 EclipseSource GmbH.
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 { inject, injectable } from '@theia/core/shared/inversify';
18
+ import { AbstractTextToModelParsingChatAgent, ChatAgent, SystemMessageDescription } from './chat-agents';
19
+ import {
20
+ PromptTemplate,
21
+ AgentSpecificVariables
22
+ } from '@theia/ai-core';
23
+ import {
24
+ ChatRequestModelImpl,
25
+ ChatResponseContent,
26
+ CommandChatResponseContentImpl,
27
+ CustomCallback,
28
+ HorizontalLayoutChatResponseContentImpl,
29
+ MarkdownChatResponseContentImpl,
30
+ } from './chat-model';
31
+ import {
32
+ CommandRegistry,
33
+ MessageService,
34
+ generateUuid,
35
+ } from '@theia/core';
36
+
37
+ export const commandTemplate: PromptTemplate = {
38
+ id: 'command-system',
39
+ template: `# System Prompt
40
+
41
+ You are a service that helps users find commands to execute in an IDE.
42
+ You reply with stringified JSON Objects that tell the user which command to execute and its arguments, if any.
43
+
44
+ # Examples
45
+
46
+ The examples start with a short explanation of the return object.
47
+ The response can be found within the markdown \`\`\`json and \`\`\` markers.
48
+ Please include these markers in the reply.
49
+
50
+ Never under any circumstances may you reply with just the command-id!
51
+
52
+ ## Example 1
53
+
54
+ This reply is to tell the user to execute the \`theia-ai-prompt-template:show-prompts-command\` command that is available in the Theia command registry.
55
+
56
+ \`\`\`json
57
+ {
58
+ "type": "theia-command",
59
+ "commandId": "theia-ai-prompt-template:show-prompts-command"
60
+ }
61
+ \`\`\`
62
+
63
+ ## Example 2
64
+
65
+ This reply is to tell the user to execute the \`theia-ai-prompt-template:show-prompts-command\` command that is available in the theia command registry,
66
+ when the user want to pass arguments to the command.
67
+
68
+ \`\`\`json
69
+ {
70
+ "type": "theia-command",
71
+ "commandId": "theia-ai-prompt-template:show-prompts-command",
72
+ "arguments": ["foo"]
73
+ }
74
+ \`\`\`
75
+
76
+ ## Example 3
77
+
78
+ This reply is for custom commands that are not registered in the Theia command registry.
79
+ These commands always have the command id \`ai-chat.command-chat-response.generic\`.
80
+ The arguments are an array and may differ, depending on the user's instructions.
81
+
82
+ \`\`\`json
83
+ {
84
+ "type": "custom-handler",
85
+ "commandId": "ai-chat.command-chat-response.generic",
86
+ "arguments": ["foo", "bar"]
87
+ }
88
+ \`\`\`
89
+
90
+ ## Example 4
91
+
92
+ This reply of type no-command is for cases where you can't find a proper command.
93
+ You may use the message to explain the situation to the user.
94
+
95
+ \`\`\`json
96
+ {
97
+ "type": "no-command",
98
+ "message": "a message explaining what is wrong"
99
+ }
100
+ \`\`\`
101
+
102
+ # Rules
103
+
104
+ ## Theia Commands
105
+
106
+ If a user asks for a Theia command, or the context implies it is about a command in Theia, return a response with \`"type": "theia-command"\`.
107
+ You need to exchange the "commandId".
108
+ The available command ids in Theia are in the list below. The list of commands is formatted like this:
109
+
110
+ command-id1: Label1
111
+ command-id2: Label2
112
+ command-id3:
113
+ command-id4: Label4
114
+
115
+ The Labels may be empty, but there is always a command-id.
116
+
117
+ Suggest a command that probably fits the user's message based on the label and the command ids you know.
118
+ If you have multiple commands that fit, return the one that fits best. We only want a single command in the reply.
119
+ If the user says that the last command was not right, try to return the next best fit based on the conversation history with the user.
120
+
121
+ If there are no more command ids that seem to fit, return a response of \`"type": "no-command"\` explaining the situation.
122
+
123
+ Here are the known Theia commands:
124
+
125
+ Begin List:
126
+ {{command-ids}}
127
+ End List
128
+
129
+ You may only use commands from this list when responding with \`"type": "theia-command"\`.
130
+ Do not come up with command ids that are not in this list.
131
+ If you need to do this, use the \`"type": "no-command"\`. instead
132
+
133
+ ## Custom Handlers
134
+
135
+ If the user asks for a command that is not a Theia command, return a response with \`"type": "custom-handler"\`.
136
+
137
+ ## Other Cases
138
+
139
+ In all other cases, return a reply of \`"type": "no-command"\`.
140
+
141
+ # Examples of Invalid Responses
142
+
143
+ ## Invalid Response Example 1
144
+
145
+ This example is invalid because it returns text and two commands.
146
+ Only one command should be replied, and it must be parseable JSON.
147
+
148
+ ### The Example
149
+
150
+ Yes, there are a few more theme-related commands. Here is another one:
151
+
152
+ \`\`\`json
153
+ {
154
+ "type": "theia-command",
155
+ "commandId": "workbench.action.selectIconTheme"
156
+ }
157
+ \`\`\`
158
+
159
+ And another one:
160
+
161
+ \`\`\`json
162
+ {
163
+ "type": "theia-command",
164
+ "commandId": "core.close.right.tabs"
165
+ }
166
+ \`\`\`
167
+
168
+ ## Invalid Response Example 2
169
+
170
+ The following example is invalid because it only returns the command id and is not parseable JSON:
171
+
172
+ ### The Example
173
+
174
+ workbench.action.selectIconTheme
175
+
176
+ ## Invalid Response Example 3
177
+
178
+ The following example is invalid because it returns a message with the command id. We need JSON objects based on the above rules.
179
+ Do not respond like this in any case! We need a command of \`"type": "theia-command"\`.
180
+
181
+ The expected response would be:
182
+ \`\`\`json
183
+ {
184
+ "type": "theia-command",
185
+ "commandId": "core.close.right.tabs"
186
+ }
187
+ \`\`\`
188
+
189
+ ### The Example
190
+
191
+ I found this command that might help you: core.close.right.tabs
192
+
193
+ ## Invalid Response Example 4
194
+
195
+ The following example is invalid because it has an explanation string before the JSON.
196
+ We only want the JSON!
197
+
198
+ ### The Example
199
+
200
+ You can toggle high contrast mode with this command:
201
+
202
+ \`\`\`json
203
+ {
204
+ "type": "theia-command",
205
+ "commandId": "editor.action.toggleHighContrast"
206
+ }
207
+ \`\`\`
208
+
209
+ ## Invalid Response Example 5
210
+
211
+ The following example is invalid because it explains that no command was found.
212
+ We want a response of \`"type": "no-command"\` and have the message there.
213
+
214
+ ### The Example
215
+
216
+ There is no specific command available to "open the windows" in the provided Theia command list.
217
+
218
+ ## Invalid Response Example 6
219
+
220
+ In this example we were using the following theia id command list:
221
+
222
+ Begin List:
223
+ container--theia-open-editors-widget: Hello
224
+ foo:toggle-visibility-explorer-view-container--files: Label 1
225
+ foo:toggle-visibility-explorer-view-container--plugin-view: Label 2
226
+ End List
227
+
228
+ The problem is that workbench.action.toggleHighContrast is not in this list.
229
+ theia-command types may only use commandIds from this list.
230
+ This should have been of \`"type": "no-command"\`.
231
+
232
+ ### The Example
233
+
234
+ \`\`\`json
235
+ {
236
+ "type": "theia-command",
237
+ "commandId": "workbench.action.toggleHighContrast"
238
+ }
239
+ \`\`\`
240
+
241
+ `};
242
+
243
+ interface ParsedCommand {
244
+ type: 'theia-command' | 'custom-handler' | 'no-command'
245
+ commandId: string;
246
+ arguments?: string[];
247
+ message?: string;
248
+ }
249
+
250
+ @injectable()
251
+ export class CommandChatAgent extends AbstractTextToModelParsingChatAgent<ParsedCommand> implements ChatAgent {
252
+ @inject(CommandRegistry)
253
+ protected commandRegistry: CommandRegistry;
254
+ @inject(MessageService)
255
+ protected messageService: MessageService;
256
+ readonly name: string;
257
+ readonly description: string;
258
+ readonly variables: string[];
259
+ readonly promptTemplates: PromptTemplate[];
260
+ readonly functions: string[];
261
+ readonly agentSpecificVariables: AgentSpecificVariables[];
262
+
263
+ constructor(
264
+ ) {
265
+ super('Command', [{
266
+ purpose: 'command',
267
+ identifier: 'openai/gpt-4o',
268
+ }], 'command');
269
+ this.name = 'Command';
270
+ this.description = 'This agent is aware of all commands that the user can execute within the Theia IDE, the tool that the user is currently working with. \
271
+ Based on the user request, it can find the right command and then let the user execute it.';
272
+ this.variables = [];
273
+ this.promptTemplates = [commandTemplate];
274
+ this.functions = [];
275
+ this.agentSpecificVariables = [{
276
+ name: 'command-ids',
277
+ description: 'The list of available commands in Theia.',
278
+ usedInPrompt: true
279
+ }];
280
+ }
281
+
282
+ protected async getSystemMessageDescription(): Promise<SystemMessageDescription | undefined> {
283
+ const knownCommands: string[] = [];
284
+ for (const command of this.commandRegistry.getAllCommands()) {
285
+ knownCommands.push(`${command.id}: ${command.label}`);
286
+ }
287
+ const systemPrompt = await this.promptService.getPrompt(commandTemplate.id, {
288
+ 'command-ids': knownCommands.join('\n')
289
+ });
290
+ if (systemPrompt === undefined) {
291
+ throw new Error('Couldn\'t get system prompt ');
292
+ }
293
+ return SystemMessageDescription.fromResolvedPromptTemplate(systemPrompt);
294
+ }
295
+
296
+ /**
297
+ * @param text the text received from the language model
298
+ * @returns the parsed command if the text contained a valid command.
299
+ * If there was no json in the text, return a no-command response.
300
+ */
301
+ protected async parseTextResponse(text: string): Promise<ParsedCommand> {
302
+ const jsonMatch = text.match(/(\{[\s\S]*\})/);
303
+ const jsonString = jsonMatch ? jsonMatch[1] : `{
304
+ "type": "no-command",
305
+ "message": "Please try again."
306
+ }`;
307
+ const parsedCommand = JSON.parse(jsonString) as ParsedCommand;
308
+ return parsedCommand;
309
+ }
310
+
311
+ protected createResponseContent(parsedCommand: ParsedCommand, request: ChatRequestModelImpl): ChatResponseContent {
312
+ if (parsedCommand.type === 'theia-command') {
313
+ const theiaCommand = this.commandRegistry.getCommand(parsedCommand.commandId);
314
+ if (theiaCommand === undefined) {
315
+ console.error(`No Theia Command with id ${parsedCommand.commandId}`);
316
+ request.response.cancel();
317
+ }
318
+ const args = parsedCommand.arguments !== undefined &&
319
+ parsedCommand.arguments.length > 0
320
+ ? parsedCommand.arguments
321
+ : undefined;
322
+
323
+ return new HorizontalLayoutChatResponseContentImpl([
324
+ new MarkdownChatResponseContentImpl(
325
+ 'I found this command that might help you:'
326
+ ),
327
+ new CommandChatResponseContentImpl(theiaCommand, undefined, args),
328
+ ]);
329
+ } else if (parsedCommand.type === 'custom-handler') {
330
+ const id = `ai-command-${generateUuid()}`;
331
+ const commandArgs = parsedCommand.arguments !== undefined && parsedCommand.arguments.length > 0 ? parsedCommand.arguments : [];
332
+ const args = [id, ...commandArgs];
333
+ const customCallback: CustomCallback = {
334
+ label: 'AI command',
335
+ callback: () => this.commandCallback(...args),
336
+ };
337
+ return new HorizontalLayoutChatResponseContentImpl([
338
+ new MarkdownChatResponseContentImpl(
339
+ 'Try executing this:'
340
+ ),
341
+ new CommandChatResponseContentImpl(undefined, customCallback, args),
342
+ ]);
343
+ } else {
344
+ return new MarkdownChatResponseContentImpl(parsedCommand.message ?? 'Sorry, I can\'t find such a command');
345
+ }
346
+ }
347
+
348
+ protected async commandCallback(...commandArgs: unknown[]): Promise<void> {
349
+ this.messageService.info(`Executing callback with args ${commandArgs.join(', ')}. The first arg is the command id registered for the dynamically registered command.
350
+ The other args are the actual args for the handler.`, 'Got it');
351
+ }
352
+ }
@@ -0,0 +1,24 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 EclipseSource GmbH.
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
+ export * from './chat-agent-service';
17
+ export * from './chat-agents';
18
+ export * from './chat-model';
19
+ export * from './parsed-chat-request';
20
+ export * from './chat-request-parser';
21
+ export * from './chat-service';
22
+ export * from './command-chat-agents';
23
+ export * from './universal-chat-agent';
24
+ export * from './orchestrator-chat-agent';
@@ -0,0 +1,151 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 EclipseSource GmbH.
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 { AgentSpecificVariables, getJsonOfResponse, LanguageModelResponse } from '@theia/ai-core';
18
+ import {
19
+ PromptTemplate
20
+ } from '@theia/ai-core/lib/common';
21
+ import { inject, injectable } from '@theia/core/shared/inversify';
22
+ import { ChatAgentService } from './chat-agent-service';
23
+ import { AbstractStreamParsingChatAgent, ChatAgent, SystemMessageDescription } from './chat-agents';
24
+ import { ChatRequestModelImpl, InformationalChatResponseContentImpl } from './chat-model';
25
+
26
+ export const orchestratorTemplate: PromptTemplate = {
27
+ id: 'orchestrator-system',
28
+ template: `# Instructions
29
+
30
+ Your task is to identify which Chat Agent(s) should best reply a given user's message.
31
+ You consider all messages of the conversation to ensure consistency and avoid agent switches without a clear context change.
32
+ You should select the best Chat Agent based on the name and description of the agents, matching them to the user message.
33
+
34
+ ## Constraints
35
+
36
+ Your response must be a JSON array containing the id(s) of the selected Chat Agent(s).
37
+
38
+ * Do not use ids that are not provided in the list below.
39
+ * Do not include any additional information, explanations, or questions for the user.
40
+ * If there is no suitable choice, pick \`Universal\`.
41
+ * If there are multiple good choices, return all of them.
42
+
43
+ Unless there is a more specific agent available, select \`Universal\`, especially for general programming-related questions.
44
+ You must only use the \`id\` attribute of the agent, never the name.
45
+
46
+ ### Example Results
47
+
48
+ \`\`\`json
49
+ ["Universal"]
50
+ \`\`\`
51
+
52
+ \`\`\`json
53
+ ["AnotherChatAgent", "Universal"]
54
+ \`\`\`
55
+
56
+ ## List of Currently Available Chat Agents
57
+
58
+ {{chatAgents}}
59
+ `};
60
+
61
+ export const OrchestratorChatAgentId = 'Orchestrator';
62
+
63
+ @injectable()
64
+ export class OrchestratorChatAgent extends AbstractStreamParsingChatAgent implements ChatAgent {
65
+ name: string;
66
+ description: string;
67
+ readonly variables: string[];
68
+ promptTemplates: PromptTemplate[];
69
+ fallBackChatAgentId: string;
70
+ readonly functions: string[] = [];
71
+ readonly agentSpecificVariables: AgentSpecificVariables[] = [];
72
+
73
+ constructor() {
74
+ super(OrchestratorChatAgentId, [{
75
+ purpose: 'agent-selection',
76
+ identifier: 'openai/gpt-4o',
77
+ }], 'agent-selection', 'codicon codicon-symbol-boolean');
78
+ this.name = OrchestratorChatAgentId;
79
+ this.description = 'This agent analyzes the user request against the description of all available chat agents and selects the best fitting agent to answer the request \
80
+ (by using AI).The user\'s request will be directly delegated to the selected agent without further confirmation.';
81
+ this.variables = ['chatAgents'];
82
+ this.promptTemplates = [orchestratorTemplate];
83
+ this.fallBackChatAgentId = 'Universal';
84
+ this.functions = [];
85
+ this.agentSpecificVariables = [];
86
+ }
87
+
88
+ @inject(ChatAgentService)
89
+ protected chatAgentService: ChatAgentService;
90
+
91
+ override invoke(request: ChatRequestModelImpl): Promise<void> {
92
+ request.response.addProgressMessage({ content: 'Determining the most appropriate agent', status: 'inProgress' });
93
+ return super.invoke(request);
94
+ }
95
+
96
+ protected async getSystemMessageDescription(): Promise<SystemMessageDescription | undefined> {
97
+ const resolvedPrompt = await this.promptService.getPrompt(orchestratorTemplate.id);
98
+ return resolvedPrompt ? SystemMessageDescription.fromResolvedPromptTemplate(resolvedPrompt) : undefined;
99
+ }
100
+
101
+ protected override async addContentsToResponse(response: LanguageModelResponse, request: ChatRequestModelImpl): Promise<void> {
102
+ let agentIds: string[] = [];
103
+ try {
104
+ const jsonResponse = await getJsonOfResponse(response);
105
+ if (Array.isArray(jsonResponse)) {
106
+ agentIds = jsonResponse.filter((id: string) => id !== this.id);
107
+ }
108
+ } catch (error: unknown) {
109
+ // The llm sometimes does not return a parseable result
110
+ this.logger.error('Failed to parse JSON response', error);
111
+ }
112
+
113
+ if (agentIds.length < 1) {
114
+ this.logger.error('No agent was selected, delegating to fallback chat agent');
115
+ request.response.progressMessages.forEach(progressMessage =>
116
+ request.response.updateProgressMessage({ ...progressMessage, status: 'failed' })
117
+ );
118
+ agentIds = [this.fallBackChatAgentId];
119
+ }
120
+
121
+ // check if selected (or fallback) agent exists
122
+ if (!this.chatAgentService.getAgent(agentIds[0])) {
123
+ this.logger.error(`Chat agent ${agentIds[0]} not found. Falling back to first registered agent.`);
124
+ const firstRegisteredAgent = this.chatAgentService.getAgents().filter(a => a.id !== this.id)[0]?.id;
125
+ if (firstRegisteredAgent) {
126
+ agentIds = [firstRegisteredAgent];
127
+ } else {
128
+ throw new Error('No chat agent available to handle request. Please check your configuration whether any are enabled.');
129
+ }
130
+ }
131
+
132
+ // TODO support delegating to more than one agent
133
+ const delegatedToAgent = agentIds[0];
134
+ request.response.response.addContent(new InformationalChatResponseContentImpl(
135
+ `*Orchestrator*: Delegating to \`@${delegatedToAgent}\`
136
+
137
+ ---
138
+
139
+ `
140
+ ));
141
+ request.response.overrideAgentId(delegatedToAgent);
142
+ request.response.progressMessages.forEach(progressMessage =>
143
+ request.response.updateProgressMessage({ ...progressMessage, status: 'completed' })
144
+ );
145
+ const agent = this.chatAgentService.getAgent(delegatedToAgent);
146
+ if (!agent) {
147
+ throw new Error(`Chat agent ${delegatedToAgent} not found.`);
148
+ }
149
+ await agent.invoke(request);
150
+ }
151
+ }
@@ -0,0 +1,112 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 EclipseSource GmbH.
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
+ * Copyright (c) Microsoft Corporation. All rights reserved.
18
+ * Licensed under the MIT License. See License.txt in the project root for license information.
19
+ *--------------------------------------------------------------------------------------------*/
20
+ // Partially copied from https://github.com/microsoft/vscode/blob/a2cab7255c0df424027be05d58e1b7b941f4ea60/src/vs/workbench/contrib/chat/common/chatParserTypes.ts
21
+ // Partially copied from https://github.com/microsoft/vscode/blob/a2cab7255c0df424027be05d58e1b7b941f4ea60/src/vs/editor/common/core/offsetRange.ts
22
+
23
+ import { AIVariable, ResolvedAIVariable, ToolRequest, toolRequestToPromptText } from '@theia/ai-core';
24
+ import { ChatRequest } from './chat-model';
25
+
26
+ export const chatVariableLeader = '#';
27
+ export const chatAgentLeader = '@';
28
+ export const chatFunctionLeader = '~';
29
+ export const chatSubcommandLeader = '/';
30
+
31
+ /**********************
32
+ * INTERFACES AND TYPE GUARDS
33
+ **********************/
34
+
35
+ export interface OffsetRange {
36
+ readonly start: number;
37
+ readonly endExclusive: number;
38
+ }
39
+
40
+ export interface ParsedChatRequest {
41
+ readonly request: ChatRequest;
42
+ readonly parts: ParsedChatRequestPart[];
43
+ readonly toolRequests: Map<string, ToolRequest>;
44
+ readonly variables: Map<string, AIVariable>;
45
+ }
46
+
47
+ export interface ParsedChatRequestPart {
48
+ readonly kind: string;
49
+ /**
50
+ * The text as represented in the ChatRequest
51
+ */
52
+ readonly text: string;
53
+ /**
54
+ * The text as will be sent to the LLM
55
+ */
56
+ readonly promptText: string;
57
+
58
+ readonly range: OffsetRange;
59
+ }
60
+
61
+ export class ParsedChatRequestTextPart implements ParsedChatRequestPart {
62
+ readonly kind: 'text';
63
+
64
+ constructor(readonly range: OffsetRange, readonly text: string) { }
65
+
66
+ get promptText(): string {
67
+ return this.text;
68
+ }
69
+ }
70
+
71
+ export class ParsedChatRequestVariablePart implements ParsedChatRequestPart {
72
+ readonly kind: 'var';
73
+
74
+ public resolution: ResolvedAIVariable;
75
+
76
+ constructor(readonly range: OffsetRange, readonly variableName: string, readonly variableArg: string | undefined) { }
77
+
78
+ get text(): string {
79
+ const argPart = this.variableArg ? `:${this.variableArg}` : '';
80
+ return `${chatVariableLeader}${this.variableName}${argPart}`;
81
+ }
82
+
83
+ get promptText(): string {
84
+ return this.resolution?.value ?? this.text;
85
+ }
86
+ }
87
+
88
+ export class ParsedChatRequestFunctionPart implements ParsedChatRequestPart {
89
+ readonly kind: 'function';
90
+ constructor(readonly range: OffsetRange, readonly toolRequest: ToolRequest) { }
91
+
92
+ get text(): string {
93
+ return `${chatFunctionLeader}${this.toolRequest.id}`;
94
+ }
95
+
96
+ get promptText(): string {
97
+ return toolRequestToPromptText(this.toolRequest);
98
+ }
99
+ }
100
+
101
+ export class ParsedChatRequestAgentPart implements ParsedChatRequestPart {
102
+ readonly kind: 'agent';
103
+ constructor(readonly range: OffsetRange, readonly agentId: string, readonly agentName: string) { }
104
+
105
+ get text(): string {
106
+ return `${chatAgentLeader}${this.agentName}`;
107
+ }
108
+
109
+ get promptText(): string {
110
+ return '';
111
+ }
112
+ }