@theia/ai-core 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 (209) hide show
  1. package/README.md +31 -0
  2. package/data/prompttemplate.tmLanguage.json +107 -0
  3. package/lib/browser/ai-activation-service.d.ts +17 -0
  4. package/lib/browser/ai-activation-service.d.ts.map +1 -0
  5. package/lib/browser/ai-activation-service.js +63 -0
  6. package/lib/browser/ai-activation-service.js.map +1 -0
  7. package/lib/browser/ai-command-handler-factory.d.ts +4 -0
  8. package/lib/browser/ai-command-handler-factory.d.ts.map +1 -0
  9. package/lib/browser/ai-command-handler-factory.js +20 -0
  10. package/lib/browser/ai-command-handler-factory.js.map +1 -0
  11. package/lib/browser/ai-configuration/agent-configuration-widget.d.ts +28 -0
  12. package/lib/browser/ai-configuration/agent-configuration-widget.d.ts.map +1 -0
  13. package/lib/browser/ai-configuration/agent-configuration-widget.js +242 -0
  14. package/lib/browser/ai-configuration/agent-configuration-widget.js.map +1 -0
  15. package/lib/browser/ai-configuration/ai-configuration-service.d.ts +13 -0
  16. package/lib/browser/ai-configuration/ai-configuration-service.d.ts.map +1 -0
  17. package/lib/browser/ai-configuration/ai-configuration-service.js +44 -0
  18. package/lib/browser/ai-configuration/ai-configuration-service.js.map +1 -0
  19. package/lib/browser/ai-configuration/ai-configuration-view-contribution.d.ts +12 -0
  20. package/lib/browser/ai-configuration/ai-configuration-view-contribution.d.ts.map +1 -0
  21. package/lib/browser/ai-configuration/ai-configuration-view-contribution.js +41 -0
  22. package/lib/browser/ai-configuration/ai-configuration-view-contribution.js.map +1 -0
  23. package/lib/browser/ai-configuration/ai-configuration-widget.d.ts +20 -0
  24. package/lib/browser/ai-configuration/ai-configuration-widget.d.ts.map +1 -0
  25. package/lib/browser/ai-configuration/ai-configuration-widget.js +88 -0
  26. package/lib/browser/ai-configuration/ai-configuration-widget.js.map +1 -0
  27. package/lib/browser/ai-configuration/language-model-renderer.d.ts +13 -0
  28. package/lib/browser/ai-configuration/language-model-renderer.d.ts.map +1 -0
  29. package/lib/browser/ai-configuration/language-model-renderer.js +104 -0
  30. package/lib/browser/ai-configuration/language-model-renderer.js.map +1 -0
  31. package/lib/browser/ai-configuration/template-settings-renderer.d.ts +13 -0
  32. package/lib/browser/ai-configuration/template-settings-renderer.d.ts.map +1 -0
  33. package/lib/browser/ai-configuration/template-settings-renderer.js +71 -0
  34. package/lib/browser/ai-configuration/template-settings-renderer.js.map +1 -0
  35. package/lib/browser/ai-configuration/variable-configuration-widget.d.ts +20 -0
  36. package/lib/browser/ai-configuration/variable-configuration-widget.d.ts.map +1 -0
  37. package/lib/browser/ai-configuration/variable-configuration-widget.js +99 -0
  38. package/lib/browser/ai-configuration/variable-configuration-widget.js.map +1 -0
  39. package/lib/browser/ai-core-frontend-application-contribution.d.ts +10 -0
  40. package/lib/browser/ai-core-frontend-application-contribution.d.ts.map +1 -0
  41. package/lib/browser/ai-core-frontend-application-contribution.js +46 -0
  42. package/lib/browser/ai-core-frontend-application-contribution.js.map +1 -0
  43. package/lib/browser/ai-core-frontend-module.d.ts +4 -0
  44. package/lib/browser/ai-core-frontend-module.d.ts.map +1 -0
  45. package/lib/browser/ai-core-frontend-module.js +122 -0
  46. package/lib/browser/ai-core-frontend-module.js.map +1 -0
  47. package/lib/browser/ai-core-preferences.d.ts +29 -0
  48. package/lib/browser/ai-core-preferences.d.ts.map +1 -0
  49. package/lib/browser/ai-core-preferences.js +98 -0
  50. package/lib/browser/ai-core-preferences.js.map +1 -0
  51. package/lib/browser/ai-settings-service.d.ts +14 -0
  52. package/lib/browser/ai-settings-service.d.ts.map +1 -0
  53. package/lib/browser/ai-settings-service.js +56 -0
  54. package/lib/browser/ai-settings-service.js.map +1 -0
  55. package/lib/browser/ai-view-contribution.d.ts +14 -0
  56. package/lib/browser/ai-view-contribution.d.ts.map +1 -0
  57. package/lib/browser/ai-view-contribution.js +71 -0
  58. package/lib/browser/ai-view-contribution.js.map +1 -0
  59. package/lib/browser/frontend-language-model-registry.d.ts +39 -0
  60. package/lib/browser/frontend-language-model-registry.d.ts.map +1 -0
  61. package/lib/browser/frontend-language-model-registry.js +321 -0
  62. package/lib/browser/frontend-language-model-registry.js.map +1 -0
  63. package/lib/browser/frontend-prompt-customization-service.d.ts +33 -0
  64. package/lib/browser/frontend-prompt-customization-service.d.ts.map +1 -0
  65. package/lib/browser/frontend-prompt-customization-service.js +237 -0
  66. package/lib/browser/frontend-prompt-customization-service.js.map +1 -0
  67. package/lib/browser/frontend-variable-service.d.ts +6 -0
  68. package/lib/browser/frontend-variable-service.d.ts.map +1 -0
  69. package/lib/browser/frontend-variable-service.js +31 -0
  70. package/lib/browser/frontend-variable-service.js.map +1 -0
  71. package/lib/browser/index.d.ts +11 -0
  72. package/lib/browser/index.d.ts.map +1 -0
  73. package/lib/browser/index.js +29 -0
  74. package/lib/browser/index.js.map +1 -0
  75. package/lib/browser/prompttemplate-contribution.d.ts +29 -0
  76. package/lib/browser/prompttemplate-contribution.d.ts.map +1 -0
  77. package/lib/browser/prompttemplate-contribution.js +208 -0
  78. package/lib/browser/prompttemplate-contribution.js.map +1 -0
  79. package/lib/browser/theia-variable-contribution.d.ts +13 -0
  80. package/lib/browser/theia-variable-contribution.d.ts.map +1 -0
  81. package/lib/browser/theia-variable-contribution.js +64 -0
  82. package/lib/browser/theia-variable-contribution.js.map +1 -0
  83. package/lib/common/agent-service.d.ts +66 -0
  84. package/lib/common/agent-service.d.ts.map +1 -0
  85. package/lib/common/agent-service.js +92 -0
  86. package/lib/common/agent-service.js.map +1 -0
  87. package/lib/common/agent.d.ts +45 -0
  88. package/lib/common/agent.d.ts.map +1 -0
  89. package/lib/common/agent.js +20 -0
  90. package/lib/common/agent.js.map +1 -0
  91. package/lib/common/agents-variable-contribution.d.ts +19 -0
  92. package/lib/common/agents-variable-contribution.d.ts.map +1 -0
  93. package/lib/common/agents-variable-contribution.js +56 -0
  94. package/lib/common/agents-variable-contribution.js.map +1 -0
  95. package/lib/common/communication-recording-service.d.ts +29 -0
  96. package/lib/common/communication-recording-service.d.ts.map +1 -0
  97. package/lib/common/communication-recording-service.js +20 -0
  98. package/lib/common/communication-recording-service.js.map +1 -0
  99. package/lib/common/index.d.ts +16 -0
  100. package/lib/common/index.d.ts.map +1 -0
  101. package/lib/common/index.js +34 -0
  102. package/lib/common/index.js.map +1 -0
  103. package/lib/common/language-model-delegate.d.ts +24 -0
  104. package/lib/common/language-model-delegate.d.ts.map +1 -0
  105. package/lib/common/language-model-delegate.js +26 -0
  106. package/lib/common/language-model-delegate.js.map +1 -0
  107. package/lib/common/language-model-util.d.ts +16 -0
  108. package/lib/common/language-model-util.d.ts.map +1 -0
  109. package/lib/common/language-model-util.js +92 -0
  110. package/lib/common/language-model-util.js.map +1 -0
  111. package/lib/common/language-model.d.ts +144 -0
  112. package/lib/common/language-model.d.ts.map +1 -0
  113. package/lib/common/language-model.js +142 -0
  114. package/lib/common/language-model.js.map +1 -0
  115. package/lib/common/language-model.spec.d.ts +2 -0
  116. package/lib/common/language-model.spec.d.ts.map +1 -0
  117. package/lib/common/language-model.spec.js +62 -0
  118. package/lib/common/language-model.spec.js.map +1 -0
  119. package/lib/common/prompt-service-util.d.ts +8 -0
  120. package/lib/common/prompt-service-util.d.ts.map +1 -0
  121. package/lib/common/prompt-service-util.js +34 -0
  122. package/lib/common/prompt-service-util.js.map +1 -0
  123. package/lib/common/prompt-service.d.ts +161 -0
  124. package/lib/common/prompt-service.d.ts.map +1 -0
  125. package/lib/common/prompt-service.js +204 -0
  126. package/lib/common/prompt-service.js.map +1 -0
  127. package/lib/common/prompt-service.spec.d.ts +2 -0
  128. package/lib/common/prompt-service.spec.d.ts.map +1 -0
  129. package/lib/common/prompt-service.spec.js +254 -0
  130. package/lib/common/prompt-service.spec.js.map +1 -0
  131. package/lib/common/protocol.d.ts +7 -0
  132. package/lib/common/protocol.d.ts.map +1 -0
  133. package/lib/common/protocol.js +20 -0
  134. package/lib/common/protocol.js.map +1 -0
  135. package/lib/common/settings-service.d.ts +23 -0
  136. package/lib/common/settings-service.d.ts.map +1 -0
  137. package/lib/common/settings-service.js +5 -0
  138. package/lib/common/settings-service.js.map +1 -0
  139. package/lib/common/today-variable-contribution.d.ts +17 -0
  140. package/lib/common/today-variable-contribution.d.ts.map +1 -0
  141. package/lib/common/today-variable-contribution.js +48 -0
  142. package/lib/common/today-variable-contribution.js.map +1 -0
  143. package/lib/common/tomorrow-variable-contribution.d.ts +17 -0
  144. package/lib/common/tomorrow-variable-contribution.d.ts.map +1 -0
  145. package/lib/common/tomorrow-variable-contribution.js +48 -0
  146. package/lib/common/tomorrow-variable-contribution.js.map +1 -0
  147. package/lib/common/tool-invocation-registry.d.ts +23 -0
  148. package/lib/common/tool-invocation-registry.d.ts.map +1 -0
  149. package/lib/common/tool-invocation-registry.js +72 -0
  150. package/lib/common/tool-invocation-registry.js.map +1 -0
  151. package/lib/common/variable-service.d.ts +70 -0
  152. package/lib/common/variable-service.d.ts.map +1 -0
  153. package/lib/common/variable-service.js +122 -0
  154. package/lib/common/variable-service.js.map +1 -0
  155. package/lib/node/ai-core-backend-module.d.ts +4 -0
  156. package/lib/node/ai-core-backend-module.d.ts.map +1 -0
  157. package/lib/node/ai-core-backend-module.js +47 -0
  158. package/lib/node/ai-core-backend-module.js.map +1 -0
  159. package/lib/node/backend-language-model-registry.d.ts +12 -0
  160. package/lib/node/backend-language-model-registry.d.ts.map +1 -0
  161. package/lib/node/backend-language-model-registry.js +61 -0
  162. package/lib/node/backend-language-model-registry.js.map +1 -0
  163. package/lib/node/language-model-frontend-delegate.d.ts +18 -0
  164. package/lib/node/language-model-frontend-delegate.d.ts.map +1 -0
  165. package/lib/node/language-model-frontend-delegate.js +100 -0
  166. package/lib/node/language-model-frontend-delegate.js.map +1 -0
  167. package/package.json +61 -0
  168. package/src/browser/ai-activation-service.ts +56 -0
  169. package/src/browser/ai-command-handler-factory.ts +20 -0
  170. package/src/browser/ai-configuration/agent-configuration-widget.tsx +324 -0
  171. package/src/browser/ai-configuration/ai-configuration-service.ts +43 -0
  172. package/src/browser/ai-configuration/ai-configuration-view-contribution.ts +54 -0
  173. package/src/browser/ai-configuration/ai-configuration-widget.tsx +80 -0
  174. package/src/browser/ai-configuration/language-model-renderer.tsx +113 -0
  175. package/src/browser/ai-configuration/template-settings-renderer.tsx +128 -0
  176. package/src/browser/ai-configuration/variable-configuration-widget.tsx +110 -0
  177. package/src/browser/ai-core-frontend-application-contribution.ts +39 -0
  178. package/src/browser/ai-core-frontend-module.ts +161 -0
  179. package/src/browser/ai-core-preferences.ts +115 -0
  180. package/src/browser/ai-settings-service.ts +50 -0
  181. package/src/browser/ai-view-contribution.ts +77 -0
  182. package/src/browser/frontend-language-model-registry.ts +405 -0
  183. package/src/browser/frontend-prompt-customization-service.ts +243 -0
  184. package/src/browser/frontend-variable-service.ts +26 -0
  185. package/src/browser/index.ts +26 -0
  186. package/src/browser/prompttemplate-contribution.ts +252 -0
  187. package/src/browser/style/index.css +127 -0
  188. package/src/browser/theia-variable-contribution.ts +58 -0
  189. package/src/common/agent-service.ts +137 -0
  190. package/src/common/agent.ts +70 -0
  191. package/src/common/agents-variable-contribution.ts +64 -0
  192. package/src/common/communication-recording-service.ts +53 -0
  193. package/src/common/index.ts +30 -0
  194. package/src/common/language-model-delegate.ts +45 -0
  195. package/src/common/language-model-util.ts +84 -0
  196. package/src/common/language-model.spec.ts +86 -0
  197. package/src/common/language-model.ts +242 -0
  198. package/src/common/prompt-service-util.ts +31 -0
  199. package/src/common/prompt-service.spec.ts +301 -0
  200. package/src/common/prompt-service.ts +334 -0
  201. package/src/common/protocol.ts +23 -0
  202. package/src/common/settings-service.ts +38 -0
  203. package/src/common/today-variable-contribution.ts +67 -0
  204. package/src/common/tomorrow-variable-contribution.ts +66 -0
  205. package/src/common/tool-invocation-registry.ts +79 -0
  206. package/src/common/variable-service.ts +177 -0
  207. package/src/node/ai-core-backend-module.ts +83 -0
  208. package/src/node/backend-language-model-registry.ts +59 -0
  209. package/src/node/language-model-frontend-delegate.ts +116 -0
@@ -0,0 +1,58 @@
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
+ import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
17
+ import { inject, injectable } from '@theia/core/shared/inversify';
18
+ import { VariableRegistry, VariableResolverService } from '@theia/variable-resolver/lib/browser';
19
+ import { AIVariableContribution, AIVariableResolver, AIVariableService, AIVariableResolutionRequest, AIVariableContext, ResolvedAIVariable } from '../common';
20
+
21
+ @injectable()
22
+ export class TheiaVariableContribution implements AIVariableContribution, AIVariableResolver {
23
+ @inject(VariableResolverService)
24
+ protected readonly variableResolverService: VariableResolverService;
25
+
26
+ @inject(VariableRegistry)
27
+ protected readonly variableRegistry: VariableRegistry;
28
+
29
+ @inject(FrontendApplicationStateService)
30
+ protected readonly stateService: FrontendApplicationStateService;
31
+
32
+ registerVariables(service: AIVariableService): void {
33
+ this.stateService.reachedState('initialized_layout').then(() => {
34
+ // some variable contributions in Theia are done as part of the onStart, same as our AI variable contributions
35
+ // we therefore wait for all of them to be registered before we register we map them to our own
36
+ this.variableRegistry.getVariables().forEach(variable => {
37
+ service.registerResolver({ id: `theia-${variable.name}`, name: variable.name, description: variable.description ?? 'Theia Built-in Variable' }, this);
38
+ });
39
+ });
40
+ }
41
+
42
+ protected toTheiaVariable(request: AIVariableResolutionRequest): string {
43
+ return `$\{${request.variable.name}${request.arg ? ':' + request.arg : ''}}`;
44
+ }
45
+
46
+ async canResolve(request: AIVariableResolutionRequest, context: AIVariableContext): Promise<number> {
47
+ // some variables are not resolvable without providing a specific context
48
+ // this may be expensive but was not a problem for Theia's built-in variables
49
+ const resolved = await this.variableResolverService.resolve(this.toTheiaVariable(request), context);
50
+ return !resolved ? 0 : 1;
51
+ }
52
+
53
+ async resolve(request: AIVariableResolutionRequest, context: AIVariableContext): Promise<ResolvedAIVariable | undefined> {
54
+ const resolved = await this.variableResolverService.resolve(this.toTheiaVariable(request), context);
55
+ return resolved ? { value: resolved, variable: request.variable } : undefined;
56
+ }
57
+ }
58
+
@@ -0,0 +1,137 @@
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
+ import { inject, injectable, optional, postConstruct } from '@theia/core/shared/inversify';
17
+ import { Emitter, Event } from '@theia/core';
18
+ import { Agent } from './agent';
19
+ import { AISettingsService } from './settings-service';
20
+ import { PromptService } from './prompt-service';
21
+
22
+ export const AgentService = Symbol('AgentService');
23
+
24
+ /**
25
+ * Service to access the list of known Agents.
26
+ */
27
+ export interface AgentService {
28
+ /**
29
+ * Retrieves a list of all available agents, i.e. agents which are not disabled
30
+ */
31
+ getAgents(): Agent[];
32
+ /**
33
+ * Retrieves a list of all agents, including disabled ones.
34
+ */
35
+ getAllAgents(): Agent[];
36
+ /**
37
+ * Enable the agent with the specified id.
38
+ * @param agentId the agent id.
39
+ */
40
+ enableAgent(agentId: string): void;
41
+ /**
42
+ * disable the agent with the specified id.
43
+ * @param agentId the agent id.
44
+ */
45
+ disableAgent(agentId: string): void;
46
+ /**
47
+ * query whether this agent is currently enabled or disabled.
48
+ * @param agentId the agent id.
49
+ * @return true if the agent is enabled, false otherwise.
50
+ */
51
+ isEnabled(agentId: string): boolean;
52
+
53
+ /**
54
+ * Allows to register an agent programmatically.
55
+ * @param agent the agent to register
56
+ */
57
+ registerAgent(agent: Agent): void;
58
+
59
+ /**
60
+ * Allows to unregister an agent programmatically.
61
+ * @param agentId the agent id to unregister
62
+ */
63
+ unregisterAgent(agentId: string): void;
64
+
65
+ /**
66
+ * Emitted when the list of agents changes.
67
+ * This can be used to update the UI when agents are added or removed.
68
+ */
69
+ onDidChangeAgents: Event<void>;
70
+ }
71
+
72
+ @injectable()
73
+ export class AgentServiceImpl implements AgentService {
74
+
75
+ @inject(AISettingsService) @optional()
76
+ protected readonly aiSettingsService: AISettingsService | undefined;
77
+
78
+ @inject(PromptService)
79
+ protected readonly promptService: PromptService;
80
+
81
+ protected disabledAgents = new Set<string>();
82
+
83
+ protected _agents: Agent[] = [];
84
+
85
+ private readonly onDidChangeAgentsEmitter = new Emitter<void>();
86
+ readonly onDidChangeAgents = this.onDidChangeAgentsEmitter.event;
87
+
88
+ @postConstruct()
89
+ protected init(): void {
90
+ this.aiSettingsService?.getSettings().then(settings => {
91
+ Object.entries(settings).forEach(([agentId, agentSettings]) => {
92
+ if (agentSettings.enable === false) {
93
+ this.disabledAgents.add(agentId);
94
+ }
95
+ });
96
+ });
97
+ }
98
+
99
+ registerAgent(agent: Agent): void {
100
+ this._agents.push(agent);
101
+ agent.promptTemplates.forEach(
102
+ template => this.promptService.storePromptTemplate(template)
103
+ );
104
+ this.onDidChangeAgentsEmitter.fire();
105
+ }
106
+
107
+ unregisterAgent(agentId: string): void {
108
+ const agent = this._agents.find(a => a.id === agentId);
109
+ this._agents = this._agents.filter(a => a.id !== agentId);
110
+ this.onDidChangeAgentsEmitter.fire();
111
+ agent?.promptTemplates.forEach(
112
+ template => this.promptService.removePrompt(template.id)
113
+ );
114
+ }
115
+
116
+ getAgents(): Agent[] {
117
+ return this._agents.filter(agent => this.isEnabled(agent.id));
118
+ }
119
+
120
+ getAllAgents(): Agent[] {
121
+ return this._agents;
122
+ }
123
+
124
+ enableAgent(agentId: string): void {
125
+ this.disabledAgents.delete(agentId);
126
+ this.aiSettingsService?.updateAgentSettings(agentId, { enable: true });
127
+ }
128
+
129
+ disableAgent(agentId: string): void {
130
+ this.disabledAgents.add(agentId);
131
+ this.aiSettingsService?.updateAgentSettings(agentId, { enable: false });
132
+ }
133
+
134
+ isEnabled(agentId: string): boolean {
135
+ return !this.disabledAgents.has(agentId);
136
+ }
137
+ }
@@ -0,0 +1,70 @@
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 { LanguageModelRequirement } from './language-model';
18
+ import { PromptTemplate } from './prompt-service';
19
+
20
+ export interface AgentSpecificVariables {
21
+ name: string;
22
+ description: string;
23
+ usedInPrompt: boolean;
24
+ }
25
+
26
+ export const Agent = Symbol('Agent');
27
+ /**
28
+ * Agents represent the main functionality of the AI system. They are responsible for processing user input, collecting information from the environment,
29
+ * invoking and processing LLM responses, and providing the final response to the user while recording their actions in the AI history.
30
+ *
31
+ * Agents are meant to cover all use cases, from specialized scenarios to general purpose chat bots.
32
+ *
33
+ * Agents are encouraged to provide a detailed description of their functionality and their processed inputs.
34
+ * They can also declare their used prompt templates, which makes them configurable for the user.
35
+ */
36
+ export interface Agent {
37
+ /**
38
+ * Used to identify an agent, e.g. when it is requesting language models, etc.
39
+ *
40
+ * @note This parameter might be removed in favor of `name`. Therefore, it is recommended to set `id` to the same value as `name` for now.
41
+ */
42
+ readonly id: string;
43
+
44
+ /**
45
+ * Human-readable name shown to users to identify the agent. Must be unique.
46
+ * Use short names without "Agent" or "Chat" (see `tags` for adding further properties).
47
+ */
48
+ readonly name: string;
49
+
50
+ /** A markdown description of its functionality and its privacy-relevant requirements, including function call handlers that access some data autonomously. */
51
+ readonly description: string;
52
+
53
+ /** The list of global variable identifiers this agent needs to clarify its context requirements. See #39. */
54
+ readonly variables: string[];
55
+
56
+ /** The prompt templates introduced and used by this agent. */
57
+ readonly promptTemplates: PromptTemplate[];
58
+
59
+ /** Required language models. This includes the purpose and optional language model selector arguments. See #47. */
60
+ readonly languageModelRequirements: LanguageModelRequirement[];
61
+
62
+ /** A list of tags to filter agents and to display capabilities in the UI */
63
+ readonly tags?: string[];
64
+
65
+ /** The list of local variable identifiers this agent needs to clarify its context requirements. */
66
+ readonly agentSpecificVariables: AgentSpecificVariables[];
67
+
68
+ /** The list of global function identifiers this agent needs to clarify its context requirements. */
69
+ readonly functions: string[];
70
+ }
@@ -0,0 +1,64 @@
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
+ import { inject, injectable } from '@theia/core/shared/inversify';
17
+ import { AIVariable, AIVariableContext, AIVariableContribution, AIVariableResolutionRequest, AIVariableResolver, AIVariableService, ResolvedAIVariable } from './variable-service';
18
+ import { MaybePromise } from '@theia/core';
19
+ import { AgentService } from './agent-service';
20
+
21
+ export const AGENTS_VARIABLE: AIVariable = {
22
+ id: 'agents',
23
+ name: 'agents',
24
+ description: 'Returns the list of agents available in the system'
25
+ };
26
+
27
+ export interface ResolvedAgentsVariable extends ResolvedAIVariable {
28
+ agents: AgentDescriptor[];
29
+ }
30
+
31
+ export interface AgentDescriptor {
32
+ id: string;
33
+ name: string;
34
+ description: string;
35
+ }
36
+
37
+ @injectable()
38
+ export class AgentsVariableContribution implements AIVariableContribution, AIVariableResolver {
39
+
40
+ @inject(AgentService)
41
+ protected readonly agentService: AgentService;
42
+
43
+ registerVariables(service: AIVariableService): void {
44
+ service.registerResolver(AGENTS_VARIABLE, this);
45
+ }
46
+
47
+ canResolve(request: AIVariableResolutionRequest, _context: AIVariableContext): MaybePromise<number> {
48
+ if (request.variable.name === AGENTS_VARIABLE.name) {
49
+ return 1;
50
+ }
51
+ return -1;
52
+ }
53
+
54
+ async resolve(request: AIVariableResolutionRequest, context: AIVariableContext): Promise<ResolvedAgentsVariable | undefined> {
55
+ if (request.variable.name === AGENTS_VARIABLE.name) {
56
+ const agents = this.agentService.getAgents().map(agent => ({
57
+ id: agent.id,
58
+ name: agent.name,
59
+ description: agent.description
60
+ }));
61
+ return { variable: AGENTS_VARIABLE, agents, value: JSON.stringify(agents) };
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,53 @@
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 { Event } from '@theia/core';
18
+
19
+ export type CommunicationHistory = CommunicationHistoryEntry[];
20
+
21
+ export interface CommunicationHistoryEntry {
22
+ agentId: string;
23
+ sessionId: string;
24
+ timestamp: number;
25
+ requestId: string;
26
+ request?: string;
27
+ response?: string;
28
+ responseTime?: number;
29
+ systemMessage?: string;
30
+ messages?: unknown[];
31
+ }
32
+
33
+ export type CommunicationRequestEntry = Omit<CommunicationHistoryEntry, 'response' | 'responseTime'>;
34
+ export type CommunicationResponseEntry = Omit<CommunicationHistoryEntry, 'request'>;
35
+
36
+ export type CommunicationRequestEntryParam = Omit<CommunicationRequestEntry, 'timestamp'> & Partial<Pick<CommunicationHistoryEntry, 'timestamp'>>;
37
+ export type CommunicationResponseEntryParam = Omit<CommunicationResponseEntry, 'timestamp'> & Partial<Pick<CommunicationHistoryEntry, 'timestamp'>>;
38
+
39
+ export const CommunicationRecordingService = Symbol('CommunicationRecordingService');
40
+ export interface CommunicationRecordingService {
41
+ recordRequest(requestEntry: CommunicationRequestEntryParam): void;
42
+ readonly onDidRecordRequest: Event<CommunicationRequestEntry>;
43
+
44
+ recordResponse(responseEntry: CommunicationResponseEntryParam): void;
45
+ readonly onDidRecordResponse: Event<CommunicationResponseEntry>;
46
+
47
+ getHistory(agentId: string): CommunicationHistory;
48
+
49
+ getSessionHistory(sessionId: string): CommunicationHistory;
50
+
51
+ clearHistory(): void;
52
+ readonly onStructuralChange: Event<void>;
53
+ }
@@ -0,0 +1,30 @@
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 './agent-service';
17
+ export * from './agent';
18
+ export * from './agents-variable-contribution';
19
+ export * from './communication-recording-service';
20
+ export * from './tool-invocation-registry';
21
+ export * from './language-model-delegate';
22
+ export * from './language-model-util';
23
+ export * from './language-model';
24
+ export * from './prompt-service';
25
+ export * from './prompt-service-util';
26
+ export * from './protocol';
27
+ export * from './today-variable-contribution';
28
+ export * from './tomorrow-variable-contribution';
29
+ export * from './variable-service';
30
+ export * from './settings-service';
@@ -0,0 +1,45 @@
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 { CancellationToken } from '@theia/core';
18
+ import { LanguageModelMetaData, LanguageModelParsedResponse, LanguageModelRequest, LanguageModelStreamResponsePart, LanguageModelTextResponse } from './language-model';
19
+
20
+ export const LanguageModelDelegateClient = Symbol('LanguageModelDelegateClient');
21
+ export interface LanguageModelDelegateClient {
22
+ toolCall(requestId: string, toolId: string, args_string: string): Promise<unknown>;
23
+ send(id: string, token: LanguageModelStreamResponsePart | undefined): void;
24
+ }
25
+ export const LanguageModelRegistryFrontendDelegate = Symbol('LanguageModelRegistryFrontendDelegate');
26
+ export interface LanguageModelRegistryFrontendDelegate {
27
+ getLanguageModelDescriptions(): Promise<LanguageModelMetaData[]>;
28
+ }
29
+
30
+ export interface LanguageModelStreamResponseDelegate {
31
+ streamId: string;
32
+ }
33
+ export const isLanguageModelStreamResponseDelegate = (obj: unknown): obj is LanguageModelStreamResponseDelegate =>
34
+ !!(obj && typeof obj === 'object' && 'streamId' in obj && typeof (obj as { streamId: unknown }).streamId === 'string');
35
+
36
+ export type LanguageModelResponseDelegate = LanguageModelTextResponse | LanguageModelParsedResponse | LanguageModelStreamResponseDelegate;
37
+
38
+ export const LanguageModelFrontendDelegate = Symbol('LanguageModelFrontendDelegate');
39
+ export interface LanguageModelFrontendDelegate {
40
+ cancel(requestId: string): void;
41
+ request(modelId: string, request: LanguageModelRequest, requestId: string, cancellationToken?: CancellationToken): Promise<LanguageModelResponseDelegate>;
42
+ }
43
+
44
+ export const languageModelRegistryDelegatePath = '/services/languageModelRegistryDelegatePath';
45
+ export const languageModelDelegatePath = '/services/languageModelDelegatePath';
@@ -0,0 +1,84 @@
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 { isLanguageModelParsedResponse, isLanguageModelStreamResponse, isLanguageModelTextResponse, LanguageModelResponse, ToolRequest } from './language-model';
18
+
19
+ /**
20
+ * Retrieves the text content from a `LanguageModelResponse` object.
21
+ *
22
+ * **Important:** For stream responses, the stream can only be consumed once. Calling this function multiple times on the same stream response will return an empty string (`''`)
23
+ * on subsequent calls, as the stream will have already been consumed.
24
+ *
25
+ * @param {LanguageModelResponse} response - The response object, which may contain a text, stream, or parsed response.
26
+ * @returns {Promise<string>} - A promise that resolves to the text content of the response.
27
+ * @throws {Error} - Throws an error if the response type is not supported or does not contain valid text content.
28
+ */
29
+ export const getTextOfResponse = async (response: LanguageModelResponse): Promise<string> => {
30
+ if (isLanguageModelTextResponse(response)) {
31
+ return response.text;
32
+ } else if (isLanguageModelStreamResponse(response)) {
33
+ let result = '';
34
+ for await (const chunk of response.stream) {
35
+ result += chunk.content ?? '';
36
+ }
37
+ return result;
38
+ } else if (isLanguageModelParsedResponse(response)) {
39
+ return response.content;
40
+ }
41
+ throw new Error(`Invalid response type ${response}`);
42
+ };
43
+
44
+ export const getJsonOfResponse = async (response: LanguageModelResponse): Promise<unknown> => {
45
+ const text = await getTextOfResponse(response);
46
+ return getJsonOfText(text);
47
+ };
48
+
49
+ export const getJsonOfText = (text: string): unknown => {
50
+ if (text.startsWith('```json')) {
51
+ const regex = /```json\s*([\s\S]*?)\s*```/g;
52
+ let match;
53
+ // eslint-disable-next-line no-null/no-null
54
+ while ((match = regex.exec(text)) !== null) {
55
+ try {
56
+ return JSON.parse(match[1]);
57
+ } catch (error) {
58
+ console.error('Failed to parse JSON:', error);
59
+ }
60
+ }
61
+ } else if (text.startsWith('{') || text.startsWith('[')) {
62
+ return JSON.parse(text);
63
+ }
64
+ throw new Error('Invalid response format');
65
+ };
66
+
67
+ export const toolRequestToPromptText = (toolRequest: ToolRequest): string => {
68
+ const parameters = toolRequest.parameters;
69
+ let paramsText = '';
70
+ // parameters are supposed to be as a JSON schema. Thus, derive the parameters from its properties definition
71
+ if (parameters) {
72
+ const properties = parameters.properties;
73
+ paramsText = Object.keys(properties)
74
+ .map(key => {
75
+ const param = properties[key];
76
+ return `${key}: ${param.type}`;
77
+ })
78
+ .join(', ');
79
+ }
80
+ const descriptionText = toolRequest.description
81
+ ? `: ${toolRequest.description}`
82
+ : '';
83
+ return `You can call function: ${toolRequest.id}(${paramsText})${descriptionText}`;
84
+ };
@@ -0,0 +1,86 @@
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 { isModelMatching, LanguageModel, LanguageModelSelector } from './language-model';
18
+ import { expect } from 'chai';
19
+
20
+ describe('isModelMatching', () => {
21
+ it('returns false with one of two parameter mismatches', () => {
22
+ expect(
23
+ isModelMatching(
24
+ <LanguageModelSelector>{
25
+ name: 'XXX',
26
+ family: 'YYY',
27
+ },
28
+ <LanguageModel>{
29
+ name: 'gpt-4o',
30
+ family: 'YYY',
31
+ }
32
+ )
33
+ ).eql(false);
34
+ });
35
+ it('returns false with two parameter mismatches', () => {
36
+ expect(
37
+ isModelMatching(
38
+ <LanguageModelSelector>{
39
+ name: 'XXX',
40
+ family: 'YYY',
41
+ },
42
+ <LanguageModel>{
43
+ name: 'gpt-4o',
44
+ family: 'ZZZ',
45
+ }
46
+ )
47
+ ).eql(false);
48
+ });
49
+ it('returns true with one parameter match', () => {
50
+ expect(
51
+ isModelMatching(
52
+ <LanguageModelSelector>{
53
+ name: 'gpt-4o',
54
+ },
55
+ <LanguageModel>{
56
+ name: 'gpt-4o',
57
+ }
58
+ )
59
+ ).eql(true);
60
+ });
61
+ it('returns true with two parameter matches', () => {
62
+ expect(
63
+ isModelMatching(
64
+ <LanguageModelSelector>{
65
+ name: 'gpt-4o',
66
+ family: 'YYY',
67
+ },
68
+ <LanguageModel>{
69
+ name: 'gpt-4o',
70
+ family: 'YYY',
71
+ }
72
+ )
73
+ ).eql(true);
74
+ });
75
+ it('returns true if there are no parameters in selector', () => {
76
+ expect(
77
+ isModelMatching(
78
+ <LanguageModelSelector>{},
79
+ <LanguageModel>{
80
+ name: 'gpt-4o',
81
+ family: 'YYY',
82
+ }
83
+ )
84
+ ).eql(true);
85
+ });
86
+ });