@theia/ai-chat 1.46.0-next.241

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