@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,177 @@
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/chatVariables.ts
21
+
22
+ import { ContributionProvider, Disposable, Emitter, ILogger, MaybePromise, Prioritizeable, Event } from '@theia/core';
23
+ import { inject, injectable, named } from '@theia/core/shared/inversify';
24
+
25
+ export interface AIVariable {
26
+ /** provider id */
27
+ id: string;
28
+ /** variable name */
29
+ name: string;
30
+ /** variable description */
31
+ description: string;
32
+ args?: AIVariableDescription[];
33
+ }
34
+
35
+ export interface AIVariableDescription {
36
+ name: string;
37
+ description: string;
38
+ }
39
+
40
+ export interface ResolvedAIVariable {
41
+ variable: AIVariable;
42
+ value: string;
43
+ }
44
+
45
+ export interface AIVariableResolutionRequest {
46
+ variable: AIVariable;
47
+ arg?: string;
48
+ }
49
+
50
+ export interface AIVariableContext {
51
+ }
52
+
53
+ export type AIVariableArg = string | { variable: string, arg?: string } | AIVariableResolutionRequest;
54
+
55
+ export interface AIVariableResolver {
56
+ canResolve(request: AIVariableResolutionRequest, context: AIVariableContext): MaybePromise<number>,
57
+ resolve(request: AIVariableResolutionRequest, context: AIVariableContext): Promise<ResolvedAIVariable | undefined>;
58
+ }
59
+
60
+ export const AIVariableService = Symbol('AIVariableService');
61
+ export interface AIVariableService {
62
+ hasVariable(name: string): boolean;
63
+ getVariable(name: string): Readonly<AIVariable> | undefined;
64
+ getVariables(): Readonly<AIVariable>[];
65
+ unregisterVariable(name: string): void;
66
+ readonly onDidChangeVariables: Event<void>;
67
+
68
+ registerResolver(variable: AIVariable, resolver: AIVariableResolver): Disposable;
69
+ unregisterResolver(variable: AIVariable, resolver: AIVariableResolver): void;
70
+ getResolver(name: string, arg: string | undefined, context: AIVariableContext): Promise<AIVariableResolver | undefined>;
71
+
72
+ resolveVariable(variable: AIVariableArg, context: AIVariableContext): Promise<ResolvedAIVariable | undefined>;
73
+ }
74
+
75
+ export const AIVariableContribution = Symbol('AIVariableContribution');
76
+ export interface AIVariableContribution {
77
+ registerVariables(service: AIVariableService): void;
78
+ }
79
+
80
+ @injectable()
81
+ export class DefaultAIVariableService implements AIVariableService {
82
+ protected variables = new Map<string, AIVariable>();
83
+ protected resolvers = new Map<string, AIVariableResolver[]>();
84
+
85
+ protected readonly onDidChangeVariablesEmitter = new Emitter<void>();
86
+ readonly onDidChangeVariables: Event<void> = this.onDidChangeVariablesEmitter.event;
87
+
88
+ @inject(ILogger) protected logger: ILogger;
89
+
90
+ constructor(
91
+ @inject(ContributionProvider) @named(AIVariableContribution)
92
+ protected readonly contributionProvider: ContributionProvider<AIVariableContribution>
93
+ ) {
94
+ }
95
+
96
+ protected initContributions(): void {
97
+ this.contributionProvider.getContributions().forEach(contribution => contribution.registerVariables(this));
98
+ }
99
+
100
+ protected getKey(name: string): string {
101
+ return `${name.toLowerCase()}`;
102
+ }
103
+
104
+ async getResolver(name: string, arg: string | undefined, context: AIVariableContext): Promise<AIVariableResolver | undefined> {
105
+ const resolvers = await this.prioritize(name, arg, context);
106
+ return resolvers[0];
107
+ }
108
+
109
+ protected getResolvers(name: string): AIVariableResolver[] {
110
+ return this.resolvers.get(this.getKey(name)) ?? [];
111
+ }
112
+
113
+ protected async prioritize(name: string, arg: string | undefined, context: AIVariableContext): Promise<AIVariableResolver[]> {
114
+ const variable = this.getVariable(name);
115
+ if (!variable) {
116
+ return [];
117
+ }
118
+ const prioritized = await Prioritizeable.prioritizeAll(this.getResolvers(name), async resolver => {
119
+ try {
120
+ return await resolver.canResolve({ variable, arg }, context);
121
+ } catch {
122
+ return 0;
123
+ }
124
+ });
125
+ return prioritized.map(p => p.value);
126
+ }
127
+
128
+ hasVariable(name: string): boolean {
129
+ return !!this.getVariable(name);
130
+ }
131
+
132
+ getVariable(name: string): Readonly<AIVariable> | undefined {
133
+ return this.variables.get(this.getKey(name));
134
+ }
135
+
136
+ getVariables(): Readonly<AIVariable>[] {
137
+ return [...this.variables.values()];
138
+ }
139
+
140
+ registerResolver(variable: AIVariable, resolver: AIVariableResolver): Disposable {
141
+ const key = this.getKey(variable.name);
142
+ if (!this.variables.get(key)) {
143
+ this.variables.set(key, variable);
144
+ this.onDidChangeVariablesEmitter.fire();
145
+ }
146
+ const resolvers = this.resolvers.get(key) ?? [];
147
+ resolvers.push(resolver);
148
+ this.resolvers.set(key, resolvers);
149
+ return Disposable.create(() => this.unregisterResolver(variable, resolver));
150
+ }
151
+
152
+ unregisterResolver(variable: AIVariable, resolver: AIVariableResolver): void {
153
+ const key = this.getKey(variable.name);
154
+ const registeredResolvers = this.resolvers.get(key);
155
+ registeredResolvers?.splice(registeredResolvers.indexOf(resolver), 1);
156
+ if (registeredResolvers?.length === 0) {
157
+ this.unregisterVariable(variable.name);
158
+ }
159
+ }
160
+
161
+ unregisterVariable(name: string): void {
162
+ this.variables.delete(this.getKey(name));
163
+ this.resolvers.delete(this.getKey(name));
164
+ this.onDidChangeVariablesEmitter.fire();
165
+ }
166
+
167
+ async resolveVariable(request: AIVariableArg, context: AIVariableContext): Promise<ResolvedAIVariable | undefined> {
168
+ const variableName = typeof request === 'string' ? request : typeof request.variable === 'string' ? request.variable : request.variable.name;
169
+ const variable = this.getVariable(variableName);
170
+ if (!variable) {
171
+ return undefined;
172
+ }
173
+ const arg = typeof request === 'string' ? undefined : request.arg;
174
+ const resolver = await this.getResolver(variableName, arg, context);
175
+ return resolver?.resolve({ variable, arg }, context);
176
+ }
177
+ }
@@ -0,0 +1,83 @@
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 { ContainerModule } from '@theia/core/shared/inversify';
17
+ import {
18
+ LanguageModelFrontendDelegateImpl,
19
+ LanguageModelRegistryFrontendDelegateImpl,
20
+ } from './language-model-frontend-delegate';
21
+ import {
22
+ ConnectionHandler,
23
+ RpcConnectionHandler,
24
+ bindContributionProvider,
25
+ } from '@theia/core';
26
+ import {
27
+ LanguageModelRegistry,
28
+ LanguageModelProvider,
29
+ PromptService,
30
+ PromptServiceImpl,
31
+ LanguageModelDelegateClient,
32
+ LanguageModelFrontendDelegate,
33
+ LanguageModelRegistryFrontendDelegate,
34
+ languageModelDelegatePath,
35
+ languageModelRegistryDelegatePath,
36
+ LanguageModelRegistryClient
37
+ } from '../common';
38
+ import { BackendLanguageModelRegistry } from './backend-language-model-registry';
39
+
40
+ export default new ContainerModule(bind => {
41
+ bindContributionProvider(bind, LanguageModelProvider);
42
+ bind(BackendLanguageModelRegistry).toSelf().inSingletonScope();
43
+ bind(LanguageModelRegistry).toService(BackendLanguageModelRegistry);
44
+
45
+ bind(LanguageModelRegistryFrontendDelegate).to(LanguageModelRegistryFrontendDelegateImpl).inSingletonScope();
46
+ bind(ConnectionHandler)
47
+ .toDynamicValue(
48
+ ctx =>
49
+ new RpcConnectionHandler<LanguageModelRegistryClient>(
50
+ languageModelRegistryDelegatePath,
51
+ client => {
52
+ const registryDelegate = ctx.container.get<LanguageModelRegistryFrontendDelegateImpl>(
53
+ LanguageModelRegistryFrontendDelegate
54
+ );
55
+ registryDelegate.setClient(client);
56
+ return registryDelegate;
57
+ }
58
+ )
59
+ )
60
+ .inSingletonScope();
61
+
62
+ bind(LanguageModelFrontendDelegateImpl).toSelf().inSingletonScope();
63
+ bind(LanguageModelFrontendDelegate).toService(LanguageModelFrontendDelegateImpl);
64
+ bind(ConnectionHandler)
65
+ .toDynamicValue(
66
+ ({ container }) =>
67
+ new RpcConnectionHandler<LanguageModelDelegateClient>(
68
+ languageModelDelegatePath,
69
+ client => {
70
+ const service =
71
+ container.get<LanguageModelFrontendDelegateImpl>(
72
+ LanguageModelFrontendDelegateImpl
73
+ );
74
+ service.setClient(client);
75
+ return service;
76
+ }
77
+ )
78
+ )
79
+ .inSingletonScope();
80
+
81
+ bind(PromptServiceImpl).toSelf().inSingletonScope();
82
+ bind(PromptService).toService(PromptServiceImpl);
83
+ });
@@ -0,0 +1,59 @@
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 { injectable } from '@theia/core/shared/inversify';
18
+ import { DefaultLanguageModelRegistryImpl, LanguageModel, LanguageModelMetaData, LanguageModelRegistryClient } from '../common';
19
+
20
+ /**
21
+ * Notifies a client whenever a model is added or removed
22
+ */
23
+ @injectable()
24
+ export class BackendLanguageModelRegistry extends DefaultLanguageModelRegistryImpl {
25
+
26
+ private client: LanguageModelRegistryClient | undefined;
27
+
28
+ setClient(client: LanguageModelRegistryClient): void {
29
+ this.client = client;
30
+ }
31
+
32
+ override addLanguageModels(models: LanguageModel[]): void {
33
+ const modelsLength = this.languageModels.length;
34
+ super.addLanguageModels(models);
35
+ // only notify for models which were really added
36
+ for (let i = modelsLength; i < this.languageModels.length; i++) {
37
+ this.client?.languageModelAdded(this.mapToMetaData(this.languageModels[i]));
38
+ }
39
+ }
40
+
41
+ override removeLanguageModels(ids: string[]): void {
42
+ super.removeLanguageModels(ids);
43
+ for (const id of ids) {
44
+ this.client?.languageModelRemoved(id);
45
+ }
46
+ }
47
+
48
+ mapToMetaData(model: LanguageModel): LanguageModelMetaData {
49
+ return {
50
+ id: model.id,
51
+ name: model.name,
52
+ vendor: model.vendor,
53
+ version: model.version,
54
+ family: model.family,
55
+ maxInputTokens: model.maxInputTokens,
56
+ maxOutputTokens: model.maxOutputTokens,
57
+ };
58
+ }
59
+ }
@@ -0,0 +1,116 @@
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 { CancellationToken, CancellationTokenSource, ILogger, generateUuid } from '@theia/core';
19
+ import {
20
+ LanguageModelMetaData,
21
+ LanguageModelRegistry,
22
+ LanguageModelRequest,
23
+ isLanguageModelStreamResponse,
24
+ isLanguageModelTextResponse,
25
+ LanguageModelStreamResponsePart,
26
+ LanguageModelDelegateClient,
27
+ LanguageModelFrontendDelegate,
28
+ LanguageModelRegistryFrontendDelegate,
29
+ LanguageModelResponseDelegate,
30
+ LanguageModelRegistryClient,
31
+ isLanguageModelParsedResponse,
32
+ } from '../common';
33
+ import { BackendLanguageModelRegistry } from './backend-language-model-registry';
34
+
35
+ @injectable()
36
+ export class LanguageModelRegistryFrontendDelegateImpl implements LanguageModelRegistryFrontendDelegate {
37
+
38
+ @inject(LanguageModelRegistry)
39
+ private registry: BackendLanguageModelRegistry;
40
+
41
+ setClient(client: LanguageModelRegistryClient): void {
42
+ this.registry.setClient(client);
43
+ }
44
+
45
+ async getLanguageModelDescriptions(): Promise<LanguageModelMetaData[]> {
46
+ return (await this.registry.getLanguageModels()).map(model => this.registry.mapToMetaData(model));
47
+ }
48
+ }
49
+
50
+ @injectable()
51
+ export class LanguageModelFrontendDelegateImpl implements LanguageModelFrontendDelegate {
52
+
53
+ @inject(LanguageModelRegistry)
54
+ private registry: LanguageModelRegistry;
55
+
56
+ @inject(ILogger)
57
+ private logger: ILogger;
58
+
59
+ private frontendDelegateClient: LanguageModelDelegateClient;
60
+ private requestCancellationTokenMap: Map<string, CancellationTokenSource> = new Map();
61
+
62
+ setClient(client: LanguageModelDelegateClient): void {
63
+ this.frontendDelegateClient = client;
64
+ }
65
+
66
+ cancel(requestId: string): void {
67
+ this.requestCancellationTokenMap.get(requestId)?.cancel();
68
+ }
69
+
70
+ async request(
71
+ modelId: string,
72
+ request: LanguageModelRequest,
73
+ requestId: string,
74
+ cancellationToken?: CancellationToken
75
+ ): Promise<LanguageModelResponseDelegate> {
76
+ const model = await this.registry.getLanguageModel(modelId);
77
+ if (!model) {
78
+ throw new Error(
79
+ `Request was sent to non-existent language model ${modelId}`
80
+ );
81
+ }
82
+ request.tools?.forEach(tool => {
83
+ tool.handler = async args_string => this.frontendDelegateClient.toolCall(requestId, tool.id, args_string);
84
+ });
85
+ if (cancellationToken) {
86
+ const tokenSource = new CancellationTokenSource();
87
+ cancellationToken = tokenSource.token;
88
+ this.requestCancellationTokenMap.set(requestId, tokenSource);
89
+ }
90
+ const response = await model.request(request, cancellationToken);
91
+ if (isLanguageModelTextResponse(response) || isLanguageModelParsedResponse(response)) {
92
+ return response;
93
+ }
94
+ if (isLanguageModelStreamResponse(response)) {
95
+ const delegate = {
96
+ streamId: generateUuid(),
97
+ };
98
+ this.sendTokens(delegate.streamId, response.stream);
99
+ return delegate;
100
+ }
101
+ this.logger.error(
102
+ `Received unexpected response from language model ${modelId}. Trying to continue without touching the response.`,
103
+ response
104
+ );
105
+ return response;
106
+ }
107
+
108
+ protected sendTokens(id: string, stream: AsyncIterable<LanguageModelStreamResponsePart>): void {
109
+ (async () => {
110
+ for await (const token of stream) {
111
+ this.frontendDelegateClient.send(id, token);
112
+ }
113
+ this.frontendDelegateClient.send(id, undefined);
114
+ })();
115
+ }
116
+ }