@theia/ai-openai 1.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +31 -0
  2. package/lib/browser/openai-frontend-application-contribution.d.ts +10 -0
  3. package/lib/browser/openai-frontend-application-contribution.d.ts.map +1 -0
  4. package/lib/browser/openai-frontend-application-contribution.js +99 -0
  5. package/lib/browser/openai-frontend-application-contribution.js.map +1 -0
  6. package/lib/browser/openai-frontend-module.d.ts +4 -0
  7. package/lib/browser/openai-frontend-module.d.ts.map +1 -0
  8. package/lib/browser/openai-frontend-module.js +32 -0
  9. package/lib/browser/openai-frontend-module.js.map +1 -0
  10. package/lib/browser/openai-preferences.d.ts +6 -0
  11. package/lib/browser/openai-preferences.d.ts.map +1 -0
  12. package/lib/browser/openai-preferences.js +67 -0
  13. package/lib/browser/openai-preferences.js.map +1 -0
  14. package/lib/common/index.d.ts +2 -0
  15. package/lib/common/index.d.ts.map +1 -0
  16. package/lib/common/index.js +20 -0
  17. package/lib/common/index.js.map +1 -0
  18. package/lib/common/openai-language-models-manager.d.ts +23 -0
  19. package/lib/common/openai-language-models-manager.d.ts.map +1 -0
  20. package/lib/common/openai-language-models-manager.js +21 -0
  21. package/lib/common/openai-language-models-manager.js.map +1 -0
  22. package/lib/node/openai-backend-module.d.ts +5 -0
  23. package/lib/node/openai-backend-module.d.ts.map +1 -0
  24. package/lib/node/openai-backend-module.js +29 -0
  25. package/lib/node/openai-backend-module.js.map +1 -0
  26. package/lib/node/openai-language-model.d.ts +24 -0
  27. package/lib/node/openai-language-model.d.ts.map +1 -0
  28. package/lib/node/openai-language-model.js +174 -0
  29. package/lib/node/openai-language-model.js.map +1 -0
  30. package/lib/node/openai-language-models-manager-impl.d.ts +11 -0
  31. package/lib/node/openai-language-models-manager-impl.d.ts.map +1 -0
  32. package/lib/node/openai-language-models-manager-impl.js +77 -0
  33. package/lib/node/openai-language-models-manager-impl.js.map +1 -0
  34. package/lib/package.spec.d.ts +1 -0
  35. package/lib/package.spec.d.ts.map +1 -0
  36. package/lib/package.spec.js +26 -0
  37. package/lib/package.spec.js.map +1 -0
  38. package/package.json +54 -0
  39. package/src/browser/openai-frontend-application-contribution.ts +98 -0
  40. package/src/browser/openai-frontend-module.ts +31 -0
  41. package/src/browser/openai-preferences.ts +67 -0
  42. package/src/common/index.ts +16 -0
  43. package/src/common/openai-language-models-manager.ts +37 -0
  44. package/src/node/openai-backend-module.ts +30 -0
  45. package/src/node/openai-language-model.ts +187 -0
  46. package/src/node/openai-language-models-manager-impl.ts +73 -0
  47. package/src/package.spec.ts +28 -0
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ <div align='center'>
2
+
3
+ <br />
4
+
5
+ <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
+
7
+ <h2>ECLIPSE THEIA - Open AI EXTENSION</h2>
8
+
9
+ <hr />
10
+
11
+ </div>
12
+
13
+ ## Description
14
+
15
+ The `@theia/ai-openai` integrates OpenAI's models with Theia AI.
16
+ The OpenAI API key and the models to use can be configured via preferences.
17
+ Alternatively the OpenAI API key can also be handed in via an environment variable.
18
+
19
+ ## Additional Information
20
+
21
+ - [Theia - GitHub](https://github.com/eclipse-theia/theia)
22
+ - [Theia - Website](https://theia-ide.org/)
23
+
24
+ ## License
25
+
26
+ - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
27
+ - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
28
+
29
+ ## Trademark
30
+ "Theia" is a trademark of the Eclipse Foundation
31
+ https://www.eclipse.org/theia
@@ -0,0 +1,10 @@
1
+ import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser';
2
+ import { OpenAiLanguageModelsManager, OpenAiModelDescription } from '../common';
3
+ export declare class OpenAiFrontendApplicationContribution implements FrontendApplicationContribution {
4
+ protected preferenceService: PreferenceService;
5
+ protected manager: OpenAiLanguageModelsManager;
6
+ protected prevModels: string[];
7
+ protected prevCustomModels: Partial<OpenAiModelDescription>[];
8
+ onStart(): void;
9
+ }
10
+ //# sourceMappingURL=openai-frontend-application-contribution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-frontend-application-contribution.d.ts","sourceRoot":"","sources":["../../src/browser/openai-frontend-application-contribution.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,+BAA+B,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE7F,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAGhF,qBACa,qCAAsC,YAAW,+BAA+B;IAGzF,SAAS,CAAC,iBAAiB,EAAE,iBAAiB,CAAC;IAG/C,SAAS,CAAC,OAAO,EAAE,2BAA2B,CAAC;IAG/C,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,CAAM;IACpC,SAAS,CAAC,gBAAgB,EAAE,OAAO,CAAC,sBAAsB,CAAC,EAAE,CAAM;IAEnE,OAAO,IAAI,IAAI;CAwClB"}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 EclipseSource GmbH.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.OpenAiFrontendApplicationContribution = void 0;
19
+ const tslib_1 = require("tslib");
20
+ const browser_1 = require("@theia/core/lib/browser");
21
+ const inversify_1 = require("@theia/core/shared/inversify");
22
+ const common_1 = require("../common");
23
+ const openai_preferences_1 = require("./openai-preferences");
24
+ let OpenAiFrontendApplicationContribution = class OpenAiFrontendApplicationContribution {
25
+ constructor() {
26
+ // The preferenceChange.oldValue is always undefined for some reason
27
+ this.prevModels = [];
28
+ this.prevCustomModels = [];
29
+ }
30
+ onStart() {
31
+ this.preferenceService.ready.then(() => {
32
+ const apiKey = this.preferenceService.get(openai_preferences_1.API_KEY_PREF, undefined);
33
+ this.manager.setApiKey(apiKey);
34
+ const models = this.preferenceService.get(openai_preferences_1.MODELS_PREF, []);
35
+ this.manager.createOrUpdateLanguageModels(...models.map(createOpenAIModelDescription));
36
+ this.prevModels = [...models];
37
+ const customModels = this.preferenceService.get(openai_preferences_1.CUSTOM_ENDPOINTS_PREF, []);
38
+ this.manager.createOrUpdateLanguageModels(...createCustomModelDescriptionsFromPreferences(customModels));
39
+ this.prevCustomModels = [...customModels];
40
+ this.preferenceService.onPreferenceChanged(event => {
41
+ if (event.preferenceName === openai_preferences_1.API_KEY_PREF) {
42
+ this.manager.setApiKey(event.newValue);
43
+ }
44
+ else if (event.preferenceName === openai_preferences_1.MODELS_PREF) {
45
+ const oldModels = new Set(this.prevModels);
46
+ const newModels = new Set(event.newValue);
47
+ const modelsToRemove = [...oldModels].filter(model => !newModels.has(model));
48
+ const modelsToAdd = [...newModels].filter(model => !oldModels.has(model));
49
+ this.manager.removeLanguageModels(...modelsToRemove.map(model => `openai/${model}`));
50
+ this.manager.createOrUpdateLanguageModels(...modelsToAdd.map(createOpenAIModelDescription));
51
+ this.prevModels = [...event.newValue];
52
+ }
53
+ else if (event.preferenceName === openai_preferences_1.CUSTOM_ENDPOINTS_PREF) {
54
+ const oldModels = createCustomModelDescriptionsFromPreferences(this.prevCustomModels);
55
+ const newModels = createCustomModelDescriptionsFromPreferences(event.newValue);
56
+ const modelsToRemove = oldModels.filter(model => !newModels.some(newModel => newModel.id === model.id));
57
+ const modelsToAddOrUpdate = newModels.filter(newModel => !oldModels.some(model => model.id === newModel.id && model.model === newModel.model && model.url === newModel.url));
58
+ this.manager.removeLanguageModels(...modelsToRemove.map(model => model.id));
59
+ this.manager.createOrUpdateLanguageModels(...modelsToAddOrUpdate);
60
+ }
61
+ });
62
+ });
63
+ }
64
+ };
65
+ exports.OpenAiFrontendApplicationContribution = OpenAiFrontendApplicationContribution;
66
+ tslib_1.__decorate([
67
+ (0, inversify_1.inject)(browser_1.PreferenceService),
68
+ tslib_1.__metadata("design:type", Object)
69
+ ], OpenAiFrontendApplicationContribution.prototype, "preferenceService", void 0);
70
+ tslib_1.__decorate([
71
+ (0, inversify_1.inject)(common_1.OpenAiLanguageModelsManager),
72
+ tslib_1.__metadata("design:type", Object)
73
+ ], OpenAiFrontendApplicationContribution.prototype, "manager", void 0);
74
+ exports.OpenAiFrontendApplicationContribution = OpenAiFrontendApplicationContribution = tslib_1.__decorate([
75
+ (0, inversify_1.injectable)()
76
+ ], OpenAiFrontendApplicationContribution);
77
+ function createOpenAIModelDescription(modelId) {
78
+ return {
79
+ id: `openai/${modelId}`,
80
+ model: modelId
81
+ };
82
+ }
83
+ function createCustomModelDescriptionsFromPreferences(preferences) {
84
+ return preferences.reduce((acc, pref) => {
85
+ var _a;
86
+ if (!pref.model || !pref.url) {
87
+ return acc;
88
+ }
89
+ return [
90
+ ...acc,
91
+ {
92
+ id: (_a = pref.id) !== null && _a !== void 0 ? _a : pref.model,
93
+ model: pref.model,
94
+ url: pref.url
95
+ }
96
+ ];
97
+ }, []);
98
+ }
99
+ //# sourceMappingURL=openai-frontend-application-contribution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-frontend-application-contribution.js","sourceRoot":"","sources":["../../src/browser/openai-frontend-application-contribution.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,E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CAAI,IAAI,CAAC,KAAK;gBACzB,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,GAAG,EAAE,IAAI,CAAC,GAAG;aAChB;SACJ,CAAC;IACN,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { ContainerModule } from '@theia/core/shared/inversify';
2
+ declare const _default: ContainerModule;
3
+ export default _default;
4
+ //# sourceMappingURL=openai-frontend-module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-frontend-module.d.ts","sourceRoot":"","sources":["../../src/browser/openai-frontend-module.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;;AAM/D,wBAQG"}
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 EclipseSource GmbH.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const inversify_1 = require("@theia/core/shared/inversify");
19
+ const openai_preferences_1 = require("./openai-preferences");
20
+ const browser_1 = require("@theia/core/lib/browser");
21
+ const openai_frontend_application_contribution_1 = require("./openai-frontend-application-contribution");
22
+ const common_1 = require("../common");
23
+ exports.default = new inversify_1.ContainerModule(bind => {
24
+ bind(browser_1.PreferenceContribution).toConstantValue({ schema: openai_preferences_1.OpenAiPreferencesSchema });
25
+ bind(openai_frontend_application_contribution_1.OpenAiFrontendApplicationContribution).toSelf().inSingletonScope();
26
+ bind(browser_1.FrontendApplicationContribution).toService(openai_frontend_application_contribution_1.OpenAiFrontendApplicationContribution);
27
+ bind(common_1.OpenAiLanguageModelsManager).toDynamicValue(ctx => {
28
+ const provider = ctx.container.get(browser_1.RemoteConnectionProvider);
29
+ return provider.createProxy(common_1.OPENAI_LANGUAGE_MODELS_MANAGER_PATH);
30
+ }).inSingletonScope();
31
+ });
32
+ //# sourceMappingURL=openai-frontend-module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-frontend-module.js","sourceRoot":"","sources":["../../src/browser/openai-frontend-module.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;AAEhF,4DAA+D;AAC/D,6DAA+D;AAC/D,qDAAuJ;AACvJ,yGAAmG;AACnG,sCAA6F;AAE7F,kBAAe,IAAI,2BAAe,CAAC,IAAI,CAAC,EAAE;IACtC,IAAI,CAAC,gCAAsB,CAAC,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,4CAAuB,EAAE,CAAC,CAAC;IAClF,IAAI,CAAC,gFAAqC,CAAC,CAAC,MAAM,EAAE,CAAC,gBAAgB,EAAE,CAAC;IACxE,IAAI,CAAC,yCAA+B,CAAC,CAAC,SAAS,CAAC,gFAAqC,CAAC,CAAC;IACvF,IAAI,CAAC,oCAA2B,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;QACnD,MAAM,QAAQ,GAAG,GAAG,CAAC,SAAS,CAAC,GAAG,CAA4B,kCAAwB,CAAC,CAAC;QACxF,OAAO,QAAQ,CAAC,WAAW,CAA8B,4CAAmC,CAAC,CAAC;IAClG,CAAC,CAAC,CAAC,gBAAgB,EAAE,CAAC;AAC1B,CAAC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
2
+ export declare const API_KEY_PREF = "ai-features.openAiOfficial.openAiApiKey";
3
+ export declare const MODELS_PREF = "ai-features.openAiOfficial.officialOpenAiModels";
4
+ export declare const CUSTOM_ENDPOINTS_PREF = "ai-features.openAiCustom.customOpenAiModels";
5
+ export declare const OpenAiPreferencesSchema: PreferenceSchema;
6
+ //# sourceMappingURL=openai-preferences.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-preferences.d.ts","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6DAA6D,CAAC;AAG/F,eAAO,MAAM,YAAY,4CAA4C,CAAC;AACtE,eAAO,MAAM,WAAW,oDAAoD,CAAC;AAC7E,eAAO,MAAM,qBAAqB,gDAAgD,CAAC;AAEnF,eAAO,MAAM,uBAAuB,EAAE,gBA2CrC,CAAC"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 EclipseSource GmbH.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.OpenAiPreferencesSchema = exports.CUSTOM_ENDPOINTS_PREF = exports.MODELS_PREF = exports.API_KEY_PREF = void 0;
19
+ const ai_core_preferences_1 = require("@theia/ai-core/lib/browser/ai-core-preferences");
20
+ exports.API_KEY_PREF = 'ai-features.openAiOfficial.openAiApiKey';
21
+ exports.MODELS_PREF = 'ai-features.openAiOfficial.officialOpenAiModels';
22
+ exports.CUSTOM_ENDPOINTS_PREF = 'ai-features.openAiCustom.customOpenAiModels';
23
+ exports.OpenAiPreferencesSchema = {
24
+ type: 'object',
25
+ properties: {
26
+ [exports.API_KEY_PREF]: {
27
+ type: 'string',
28
+ markdownDescription: 'Enter an API Key of your official OpenAI Account. **Please note:** By using this preference the Open AI API key will be stored in clear text\
29
+ on the machine running Theia. Use the environment variable `OPENAI_API_KEY` to set the key securely.',
30
+ title: ai_core_preferences_1.AI_CORE_PREFERENCES_TITLE,
31
+ },
32
+ [exports.MODELS_PREF]: {
33
+ type: 'array',
34
+ description: 'Official OpenAI models to use',
35
+ title: ai_core_preferences_1.AI_CORE_PREFERENCES_TITLE,
36
+ default: ['gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-2024-05-13', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'],
37
+ items: {
38
+ type: 'string'
39
+ }
40
+ },
41
+ [exports.CUSTOM_ENDPOINTS_PREF]: {
42
+ type: 'array',
43
+ title: ai_core_preferences_1.AI_CORE_PREFERENCES_TITLE,
44
+ markdownDescription: 'Integrate custom models compatible with the OpenAI API, for example via `vllm`. The required attributes are `model` and `url`.\
45
+ Optionally, you can provide a unique `id` to identify the custom model in the UI. If none is given `model` will be used as `id`.',
46
+ default: [],
47
+ items: {
48
+ type: 'object',
49
+ properties: {
50
+ model: {
51
+ type: 'string',
52
+ title: 'Model ID'
53
+ },
54
+ url: {
55
+ type: 'string',
56
+ title: 'The Open AI API compatible endpoint where the model is hosted'
57
+ },
58
+ id: {
59
+ type: 'string',
60
+ title: 'A unique identifier which is used in the UI to identify the custom model',
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+ };
67
+ //# sourceMappingURL=openai-preferences.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-preferences.js","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;AAGhF,wFAA2F;AAE9E,QAAA,YAAY,GAAG,yCAAyC,CAAC;AACzD,QAAA,WAAW,GAAG,iDAAiD,CAAC;AAChE,QAAA,qBAAqB,GAAG,6CAA6C,CAAC;AAEtE,QAAA,uBAAuB,GAAqB;IACrD,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACR,CAAC,oBAAY,CAAC,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,mBAAmB,EAAE;iHACgF;YACrG,KAAK,EAAE,+CAAyB;SACnC;QACD,CAAC,mBAAW,CAAC,EAAE;YACX,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,+BAA+B;YAC5C,KAAK,EAAE,+CAAyB;YAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,CAAC;YACrH,KAAK,EAAE;gBACH,IAAI,EAAE,QAAQ;aACjB;SACJ;QACD,CAAC,6BAAqB,CAAC,EAAE;YACrB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,+CAAyB;YAChC,mBAAmB,EAAE;6IAC4G;YACjI,OAAO,EAAE,EAAE;YACX,KAAK,EAAE;gBACH,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACR,KAAK,EAAE;wBACH,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,UAAU;qBACpB;oBACD,GAAG,EAAE;wBACD,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,+DAA+D;qBACzE;oBACD,EAAE,EAAE;wBACA,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,0EAA0E;qBACpF;iBACJ;aACJ;SACJ;KACJ;CACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './openai-language-models-manager';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAeA,cAAc,kCAAkC,CAAC"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ // *****************************************************************************
5
+ // Copyright (C) 2024 EclipseSource GmbH.
6
+ //
7
+ // This program and the accompanying materials are made available under the
8
+ // terms of the Eclipse Public License v. 2.0 which is available at
9
+ // http://www.eclipse.org/legal/epl-2.0.
10
+ //
11
+ // This Source Code may also be made available under the following Secondary
12
+ // Licenses when the conditions for such availability set forth in the Eclipse
13
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
14
+ // with the GNU Classpath Exception which is available at
15
+ // https://www.gnu.org/software/classpath/license.html.
16
+ //
17
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
18
+ // *****************************************************************************
19
+ tslib_1.__exportStar(require("./openai-language-models-manager"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":";;;AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,2EAAiD"}
@@ -0,0 +1,23 @@
1
+ export declare const OPENAI_LANGUAGE_MODELS_MANAGER_PATH = "/services/open-ai/language-model-manager";
2
+ export declare const OpenAiLanguageModelsManager: unique symbol;
3
+ export interface OpenAiModelDescription {
4
+ /**
5
+ * The identifier of the model which will be shown in the UI.
6
+ */
7
+ id: string;
8
+ /**
9
+ * The model ID as used by the OpenAI API.
10
+ */
11
+ model: string;
12
+ /**
13
+ * The OpenAI API compatible endpoint where the model is hosted. If not provided the default OpenAI endpoint will be used.
14
+ */
15
+ url?: string;
16
+ }
17
+ export interface OpenAiLanguageModelsManager {
18
+ apiKey: string | undefined;
19
+ setApiKey(key: string | undefined): void;
20
+ createOrUpdateLanguageModels(...models: OpenAiModelDescription[]): Promise<void>;
21
+ removeLanguageModels(...modelIds: string[]): void;
22
+ }
23
+ //# sourceMappingURL=openai-language-models-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-language-models-manager.d.ts","sourceRoot":"","sources":["../../src/common/openai-language-models-manager.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,mCAAmC,6CAA6C,CAAC;AAC9F,eAAO,MAAM,2BAA2B,eAAwC,CAAC;AACjF,MAAM,WAAW,sBAAsB;IACnC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB;AACD,MAAM,WAAW,2BAA2B;IACxC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACzC,4BAA4B,CAAC,GAAG,MAAM,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjF,oBAAoB,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;CACpD"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenAiLanguageModelsManager = exports.OPENAI_LANGUAGE_MODELS_MANAGER_PATH = void 0;
4
+ // *****************************************************************************
5
+ // Copyright (C) 2024 EclipseSource GmbH.
6
+ //
7
+ // This program and the accompanying materials are made available under the
8
+ // terms of the Eclipse Public License v. 2.0 which is available at
9
+ // http://www.eclipse.org/legal/epl-2.0.
10
+ //
11
+ // This Source Code may also be made available under the following Secondary
12
+ // Licenses when the conditions for such availability set forth in the Eclipse
13
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
14
+ // with the GNU Classpath Exception which is available at
15
+ // https://www.gnu.org/software/classpath/license.html.
16
+ //
17
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
18
+ // *****************************************************************************
19
+ exports.OPENAI_LANGUAGE_MODELS_MANAGER_PATH = '/services/open-ai/language-model-manager';
20
+ exports.OpenAiLanguageModelsManager = Symbol('OpenAiLanguageModelsManager');
21
+ //# sourceMappingURL=openai-language-models-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-language-models-manager.js","sourceRoot":"","sources":["../../src/common/openai-language-models-manager.ts"],"names":[],"mappings":";;;AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AACnE,QAAA,mCAAmC,GAAG,0CAA0C,CAAC;AACjF,QAAA,2BAA2B,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import { ContainerModule } from '@theia/core/shared/inversify';
2
+ export declare const OpenAiModelFactory: unique symbol;
3
+ declare const _default: ContainerModule;
4
+ export default _default;
5
+ //# sourceMappingURL=openai-backend-module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-backend-module.d.ts","sourceRoot":"","sources":["../../src/node/openai-backend-module.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAK/D,eAAO,MAAM,kBAAkB,eAA+B,CAAC;;AAE/D,wBAMG"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 EclipseSource GmbH.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.OpenAiModelFactory = void 0;
19
+ const inversify_1 = require("@theia/core/shared/inversify");
20
+ const openai_language_models_manager_1 = require("../common/openai-language-models-manager");
21
+ const core_1 = require("@theia/core");
22
+ const openai_language_models_manager_impl_1 = require("./openai-language-models-manager-impl");
23
+ exports.OpenAiModelFactory = Symbol('OpenAiModelFactory');
24
+ exports.default = new inversify_1.ContainerModule(bind => {
25
+ bind(openai_language_models_manager_impl_1.OpenAiLanguageModelsManagerImpl).toSelf().inSingletonScope();
26
+ bind(openai_language_models_manager_1.OpenAiLanguageModelsManager).toService(openai_language_models_manager_impl_1.OpenAiLanguageModelsManagerImpl);
27
+ bind(core_1.ConnectionHandler).toDynamicValue(ctx => new core_1.RpcConnectionHandler(openai_language_models_manager_1.OPENAI_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(openai_language_models_manager_1.OpenAiLanguageModelsManager))).inSingletonScope();
28
+ });
29
+ //# sourceMappingURL=openai-backend-module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-backend-module.js","sourceRoot":"","sources":["../../src/node/openai-backend-module.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;AAEhF,4DAA+D;AAC/D,6FAA4H;AAC5H,sCAAsE;AACtE,+FAAwF;AAE3E,QAAA,kBAAkB,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAE/D,kBAAe,IAAI,2BAAe,CAAC,IAAI,CAAC,EAAE;IACtC,IAAI,CAAC,qEAA+B,CAAC,CAAC,MAAM,EAAE,CAAC,gBAAgB,EAAE,CAAC;IAClE,IAAI,CAAC,4DAA2B,CAAC,CAAC,SAAS,CAAC,qEAA+B,CAAC,CAAC;IAC7E,IAAI,CAAC,wBAAiB,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CACzC,IAAI,2BAAoB,CAAC,oEAAmC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,4DAA2B,CAAC,CAAC,CACtH,CAAC,gBAAgB,EAAE,CAAC;AACzB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { LanguageModel, LanguageModelParsedResponse, LanguageModelRequest, LanguageModelResponse } from '@theia/ai-core';
2
+ import { CancellationToken } from '@theia/core';
3
+ import OpenAI from 'openai';
4
+ import { RunnableToolFunctionWithoutParse } from 'openai/lib/RunnableFunction';
5
+ export declare const OpenAiModelIdentifier: unique symbol;
6
+ export declare class OpenAiModel implements LanguageModel {
7
+ readonly id: string;
8
+ model: string;
9
+ protected apiKey: (() => string | undefined) | undefined;
10
+ url: string | undefined;
11
+ /**
12
+ * @param id the unique id for this language model. It will be used to identify the model in the UI.
13
+ * @param model the model id as it is used by the OpenAI API
14
+ * @param openAIInitializer initializer for the OpenAI client, used for each request.
15
+ */
16
+ constructor(id: string, model: string, apiKey: (() => string | undefined) | undefined, url: string | undefined);
17
+ request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse>;
18
+ protected supportsStructuredOutput(): boolean;
19
+ protected handleStructuredOutputRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelParsedResponse>;
20
+ private getCompletionContent;
21
+ protected createTools(request: LanguageModelRequest): RunnableToolFunctionWithoutParse[] | undefined;
22
+ protected initializeOpenAi(): OpenAI;
23
+ }
24
+ //# sourceMappingURL=openai-language-model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-language-model.d.ts","sourceRoot":"","sources":["../../src/node/openai-language-model.ts"],"names":[],"mappings":"AAgBA,OAAO,EACH,aAAa,EACb,2BAA2B,EAC3B,oBAAoB,EAEpB,qBAAqB,EAExB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,gCAAgC,EAAE,MAAM,6BAA6B,CAAC;AAG/E,eAAO,MAAM,qBAAqB,eAAkC,CAAC;AAoBrE,qBAAa,WAAY,YAAW,aAAa;aAOjB,EAAE,EAAE,MAAM;IAAS,KAAK,EAAE,MAAM;IAAE,SAAS,CAAC,MAAM,EAAE,CAAC,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,SAAS;IAAS,GAAG,EAAE,MAAM,GAAG,SAAS;IALtJ;;;;OAIG;gBACyB,EAAE,EAAE,MAAM,EAAS,KAAK,EAAE,MAAM,EAAY,MAAM,EAAE,CAAC,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,SAAS,EAAS,GAAG,EAAE,MAAM,GAAG,SAAS;IAEhJ,OAAO,CAAC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IA4EnH,SAAS,CAAC,wBAAwB,IAAI,OAAO;cAM7B,6BAA6B,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,2BAA2B,CAAC;IAkBlI,OAAO,CAAC,oBAAoB;IAO5B,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,oBAAoB,GAAG,gCAAgC,EAAE,GAAG,SAAS;IAYpG,SAAS,CAAC,gBAAgB,IAAI,MAAM;CAQvC"}
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 EclipseSource GmbH.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.OpenAiModel = exports.OpenAiModelIdentifier = void 0;
19
+ const openai_1 = require("openai");
20
+ exports.OpenAiModelIdentifier = Symbol('OpenAiModelIdentifier');
21
+ function toOpenAIMessage(message) {
22
+ return {
23
+ role: toOpenAiRole(message),
24
+ content: message.query || ''
25
+ };
26
+ }
27
+ function toOpenAiRole(message) {
28
+ switch (message.actor) {
29
+ case 'system':
30
+ return 'system';
31
+ case 'ai':
32
+ return 'assistant';
33
+ default:
34
+ return 'user';
35
+ }
36
+ }
37
+ class OpenAiModel {
38
+ /**
39
+ * @param id the unique id for this language model. It will be used to identify the model in the UI.
40
+ * @param model the model id as it is used by the OpenAI API
41
+ * @param openAIInitializer initializer for the OpenAI client, used for each request.
42
+ */
43
+ constructor(id, model, apiKey, url) {
44
+ this.id = id;
45
+ this.model = model;
46
+ this.apiKey = apiKey;
47
+ this.url = url;
48
+ }
49
+ async request(request, cancellationToken) {
50
+ var _a;
51
+ const openai = this.initializeOpenAi();
52
+ if (((_a = request.response_format) === null || _a === void 0 ? void 0 : _a.type) === 'json_schema' && this.supportsStructuredOutput()) {
53
+ return this.handleStructuredOutputRequest(openai, request);
54
+ }
55
+ let runner;
56
+ const tools = this.createTools(request);
57
+ if (tools) {
58
+ runner = openai.beta.chat.completions.runTools({
59
+ model: this.model,
60
+ messages: request.messages.map(toOpenAIMessage),
61
+ stream: true,
62
+ tools: tools,
63
+ tool_choice: 'auto',
64
+ ...request.settings
65
+ });
66
+ }
67
+ else {
68
+ runner = openai.beta.chat.completions.stream({
69
+ model: this.model,
70
+ messages: request.messages.map(toOpenAIMessage),
71
+ stream: true,
72
+ ...request.settings
73
+ });
74
+ }
75
+ cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.onCancellationRequested(() => {
76
+ runner.abort();
77
+ });
78
+ let runnerEnd = false;
79
+ let resolve;
80
+ runner.on('error', error => {
81
+ console.error('Error in OpenAI chat completion stream:', error);
82
+ runnerEnd = true;
83
+ resolve({ content: error.message });
84
+ });
85
+ // we need to also listen for the emitted errors, as otherwise any error actually thrown by the API will not be caught
86
+ runner.emitted('error').then(error => {
87
+ console.error('Error in OpenAI chat completion stream:', error);
88
+ runnerEnd = true;
89
+ resolve({ content: error.message });
90
+ });
91
+ runner.emitted('abort').then(() => {
92
+ // do nothing, as the abort event is only emitted when the runner is aborted by us
93
+ });
94
+ runner.on('message', message => {
95
+ if (message.role === 'tool') {
96
+ resolve({ tool_calls: [{ id: message.tool_call_id, finished: true, result: this.getCompletionContent(message) }] });
97
+ }
98
+ console.debug('Received Open AI message', JSON.stringify(message));
99
+ });
100
+ runner.once('end', () => {
101
+ runnerEnd = true;
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ resolve(runner.finalChatCompletion);
104
+ });
105
+ const asyncIterator = {
106
+ async *[Symbol.asyncIterator]() {
107
+ runner.on('chunk', chunk => {
108
+ var _a, _b;
109
+ if ((_a = chunk.choices[0]) === null || _a === void 0 ? void 0 : _a.delta) {
110
+ resolve({ ...(_b = chunk.choices[0]) === null || _b === void 0 ? void 0 : _b.delta });
111
+ }
112
+ });
113
+ while (!runnerEnd) {
114
+ const promise = new Promise((res, rej) => {
115
+ resolve = res;
116
+ });
117
+ yield promise;
118
+ }
119
+ }
120
+ };
121
+ return { stream: asyncIterator };
122
+ }
123
+ supportsStructuredOutput() {
124
+ // currently only the lastest 4o and 4o-mini models support structured output
125
+ // see https://platform.openai.com/docs/guides/structured-outputs
126
+ return this.model === 'gpt-4o-2024-08-06' || this.model === 'gpt-4o-mini';
127
+ }
128
+ async handleStructuredOutputRequest(openai, request) {
129
+ var _a;
130
+ // TODO implement tool support for structured output (parse() seems to require different tool format)
131
+ const result = await openai.beta.chat.completions.parse({
132
+ model: this.model,
133
+ messages: request.messages.map(toOpenAIMessage),
134
+ response_format: request.response_format,
135
+ ...request.settings
136
+ });
137
+ const message = result.choices[0].message;
138
+ if (message.refusal || message.parsed === undefined) {
139
+ console.error('Error in OpenAI chat completion stream:', JSON.stringify(message));
140
+ }
141
+ return {
142
+ content: (_a = message.content) !== null && _a !== void 0 ? _a : '',
143
+ parsed: message.parsed
144
+ };
145
+ }
146
+ getCompletionContent(message) {
147
+ if (Array.isArray(message.content)) {
148
+ return message.content.join('');
149
+ }
150
+ return message.content;
151
+ }
152
+ createTools(request) {
153
+ var _a;
154
+ return (_a = request.tools) === null || _a === void 0 ? void 0 : _a.map(tool => ({
155
+ type: 'function',
156
+ function: {
157
+ name: tool.name,
158
+ description: tool.description,
159
+ parameters: tool.parameters,
160
+ function: (args_string) => tool.handler(args_string)
161
+ }
162
+ }));
163
+ }
164
+ initializeOpenAi() {
165
+ const apiKey = this.apiKey && this.apiKey();
166
+ if (!apiKey && !(this.url)) {
167
+ throw new Error('Please provide OPENAI_API_KEY in preferences or via environment variable');
168
+ }
169
+ // do not hand over API key to custom urls
170
+ return new openai_1.default({ apiKey: this.url ? 'no-key' : apiKey, baseURL: this.url });
171
+ }
172
+ }
173
+ exports.OpenAiModel = OpenAiModel;
174
+ //# sourceMappingURL=openai-language-model.js.map