@theia/ai-anthropic 1.57.0-next.112
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.
- package/README.md +31 -0
- package/lib/browser/anthropic-frontend-application-contribution.d.ts +15 -0
- package/lib/browser/anthropic-frontend-application-contribution.d.ts.map +1 -0
- package/lib/browser/anthropic-frontend-application-contribution.js +99 -0
- package/lib/browser/anthropic-frontend-application-contribution.js.map +1 -0
- package/lib/browser/anthropic-frontend-module.d.ts +4 -0
- package/lib/browser/anthropic-frontend-module.d.ts.map +1 -0
- package/lib/browser/anthropic-frontend-module.js +32 -0
- package/lib/browser/anthropic-frontend-module.js.map +1 -0
- package/lib/browser/anthropic-preferences.d.ts +5 -0
- package/lib/browser/anthropic-preferences.d.ts.map +1 -0
- package/lib/browser/anthropic-preferences.js +42 -0
- package/lib/browser/anthropic-preferences.js.map +1 -0
- package/lib/common/anthropic-language-models-manager.d.ts +33 -0
- package/lib/common/anthropic-language-models-manager.d.ts.map +1 -0
- package/lib/common/anthropic-language-models-manager.js +21 -0
- package/lib/common/anthropic-language-models-manager.js.map +1 -0
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +20 -0
- package/lib/common/index.js.map +1 -0
- package/lib/node/anthropic-backend-module.d.ts +4 -0
- package/lib/node/anthropic-backend-module.d.ts.map +1 -0
- package/lib/node/anthropic-backend-module.js +32 -0
- package/lib/node/anthropic-backend-module.js.map +1 -0
- package/lib/node/anthropic-language-model.d.ts +26 -0
- package/lib/node/anthropic-language-model.d.ts.map +1 -0
- package/lib/node/anthropic-language-model.js +247 -0
- package/lib/node/anthropic-language-model.js.map +1 -0
- package/lib/node/anthropic-language-models-manager-impl.d.ts +11 -0
- package/lib/node/anthropic-language-models-manager-impl.d.ts.map +1 -0
- package/lib/node/anthropic-language-models-manager-impl.js +77 -0
- package/lib/node/anthropic-language-models-manager-impl.js.map +1 -0
- package/lib/package.spec.d.ts +1 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +26 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +50 -0
- package/src/browser/anthropic-frontend-application-contribution.ts +107 -0
- package/src/browser/anthropic-frontend-module.ts +31 -0
- package/src/browser/anthropic-preferences.ts +42 -0
- package/src/common/anthropic-language-models-manager.ts +45 -0
- package/src/common/index.ts +16 -0
- package/src/node/anthropic-backend-module.ts +34 -0
- package/src/node/anthropic-language-model.ts +299 -0
- package/src/node/anthropic-language-models-manager-impl.ts +81 -0
- package/src/package.spec.ts +28 -0
|
@@ -0,0 +1,247 @@
|
|
|
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.AnthropicModel = exports.AnthropicModelIdentifier = void 0;
|
|
19
|
+
const core_1 = require("@theia/core");
|
|
20
|
+
const sdk_1 = require("@anthropic-ai/sdk");
|
|
21
|
+
const DEFAULT_MAX_TOKENS_STREAMING = 4096;
|
|
22
|
+
const DEFAULT_MAX_TOKENS_NON_STREAMING = 2048;
|
|
23
|
+
const EMPTY_INPUT_SCHEMA = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {},
|
|
26
|
+
required: []
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Transforms Theia language model messages to Anthropic API format
|
|
30
|
+
* @param messages Array of LanguageModelRequestMessage to transform
|
|
31
|
+
* @returns Object containing transformed messages and optional system message
|
|
32
|
+
*/
|
|
33
|
+
function transformToAnthropicParams(messages) {
|
|
34
|
+
// Extract the system message (if any), as it is a separate parameter in the Anthropic API.
|
|
35
|
+
const systemMessageObj = messages.find(message => message.actor === 'system');
|
|
36
|
+
const systemMessage = systemMessageObj === null || systemMessageObj === void 0 ? void 0 : systemMessageObj.query;
|
|
37
|
+
const convertedMessages = messages
|
|
38
|
+
.filter(message => message.actor !== 'system')
|
|
39
|
+
.map(message => ({
|
|
40
|
+
role: toAnthropicRole(message),
|
|
41
|
+
content: message.query || '',
|
|
42
|
+
}));
|
|
43
|
+
return {
|
|
44
|
+
messages: convertedMessages,
|
|
45
|
+
systemMessage,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
exports.AnthropicModelIdentifier = Symbol('AnthropicModelIdentifier');
|
|
49
|
+
/**
|
|
50
|
+
* Converts Theia message actor to Anthropic role
|
|
51
|
+
* @param message The message to convert
|
|
52
|
+
* @returns Anthropic role ('user' or 'assistant')
|
|
53
|
+
*/
|
|
54
|
+
function toAnthropicRole(message) {
|
|
55
|
+
switch (message.actor) {
|
|
56
|
+
case 'ai':
|
|
57
|
+
return 'assistant';
|
|
58
|
+
default:
|
|
59
|
+
return 'user';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Implements the Anthropic language model integration for Theia
|
|
64
|
+
*/
|
|
65
|
+
class AnthropicModel {
|
|
66
|
+
constructor(id, model, enableStreaming, apiKey, defaultRequestSettings) {
|
|
67
|
+
this.id = id;
|
|
68
|
+
this.model = model;
|
|
69
|
+
this.enableStreaming = enableStreaming;
|
|
70
|
+
this.apiKey = apiKey;
|
|
71
|
+
this.defaultRequestSettings = defaultRequestSettings;
|
|
72
|
+
}
|
|
73
|
+
getSettings(request) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
return (_b = (_a = request.settings) !== null && _a !== void 0 ? _a : this.defaultRequestSettings) !== null && _b !== void 0 ? _b : {};
|
|
76
|
+
}
|
|
77
|
+
async request(request, cancellationToken) {
|
|
78
|
+
var _a;
|
|
79
|
+
if (!((_a = request.messages) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
80
|
+
throw new Error('Request must contain at least one message');
|
|
81
|
+
}
|
|
82
|
+
const anthropic = this.initializeAnthropic();
|
|
83
|
+
try {
|
|
84
|
+
if (this.enableStreaming) {
|
|
85
|
+
return this.handleStreamingRequest(anthropic, request, cancellationToken);
|
|
86
|
+
}
|
|
87
|
+
return this.handleNonStreamingRequest(anthropic, request);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
91
|
+
throw new Error(`Anthropic API request failed: ${errorMessage}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
formatToolCallResult(result) {
|
|
95
|
+
if (typeof result === 'object' && result && 'content' in result && Array.isArray(result.content) &&
|
|
96
|
+
result.content.every(item => typeof item === 'object' && item && 'type' in item && 'text' in item)) {
|
|
97
|
+
return result.content;
|
|
98
|
+
}
|
|
99
|
+
if ((0, core_1.isArray)(result)) {
|
|
100
|
+
return result.map(r => ({ type: 'text', text: r }));
|
|
101
|
+
}
|
|
102
|
+
if (typeof result === 'object') {
|
|
103
|
+
return JSON.stringify(result);
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
async handleStreamingRequest(anthropic, request, cancellationToken, toolMessages) {
|
|
108
|
+
const settings = this.getSettings(request);
|
|
109
|
+
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
110
|
+
const tools = this.createTools(request);
|
|
111
|
+
const params = {
|
|
112
|
+
max_tokens: DEFAULT_MAX_TOKENS_STREAMING,
|
|
113
|
+
messages: [...messages, ...(toolMessages !== null && toolMessages !== void 0 ? toolMessages : [])],
|
|
114
|
+
tools,
|
|
115
|
+
model: this.model,
|
|
116
|
+
...(systemMessage && { system: systemMessage }),
|
|
117
|
+
...settings
|
|
118
|
+
};
|
|
119
|
+
const stream = anthropic.messages.stream(params);
|
|
120
|
+
cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.onCancellationRequested(() => {
|
|
121
|
+
stream.abort();
|
|
122
|
+
});
|
|
123
|
+
const that = this;
|
|
124
|
+
const asyncIterator = {
|
|
125
|
+
async *[Symbol.asyncIterator]() {
|
|
126
|
+
const toolCalls = [];
|
|
127
|
+
let toolCall;
|
|
128
|
+
for await (const event of stream) {
|
|
129
|
+
if (event.type === 'content_block_start') {
|
|
130
|
+
const contentBlock = event.content_block;
|
|
131
|
+
if (contentBlock.type === 'text') {
|
|
132
|
+
yield { content: contentBlock.text };
|
|
133
|
+
}
|
|
134
|
+
if (contentBlock.type === 'tool_use') {
|
|
135
|
+
toolCall = { name: contentBlock.name, args: '', id: contentBlock.id, index: event.index };
|
|
136
|
+
yield { tool_calls: [{ finished: false, id: toolCall.id, function: { name: toolCall.name, arguments: toolCall.args } }] };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else if (event.type === 'content_block_delta') {
|
|
140
|
+
const delta = event.delta;
|
|
141
|
+
if (delta.type === 'text_delta') {
|
|
142
|
+
yield { content: delta.text };
|
|
143
|
+
}
|
|
144
|
+
if (toolCall && delta.type === 'input_json_delta') {
|
|
145
|
+
toolCall.args += delta.partial_json;
|
|
146
|
+
yield { tool_calls: [{ function: { arguments: delta.partial_json } }] };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (event.type === 'content_block_stop') {
|
|
150
|
+
if (toolCall && toolCall.index === event.index) {
|
|
151
|
+
toolCalls.push(toolCall);
|
|
152
|
+
toolCall = undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else if (event.type === 'message_delta') {
|
|
156
|
+
if (event.delta.stop_reason === 'max_tokens') {
|
|
157
|
+
if (toolCall) {
|
|
158
|
+
yield { tool_calls: [{ finished: true, id: toolCall.id }] };
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`The response was stopped because it exceeded the max token limit of ${event.usage.output_tokens}.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (toolCalls.length > 0) {
|
|
165
|
+
const toolResult = await Promise.all(toolCalls.map(async (tc) => {
|
|
166
|
+
var _a;
|
|
167
|
+
const tool = (_a = request.tools) === null || _a === void 0 ? void 0 : _a.find(t => t.name === tc.name);
|
|
168
|
+
const argsObject = tc.args.length === 0 ? '{}' : tc.args;
|
|
169
|
+
return { name: tc.name, result: (await (tool === null || tool === void 0 ? void 0 : tool.handler(argsObject))), id: tc.id, arguments: argsObject };
|
|
170
|
+
}));
|
|
171
|
+
const calls = toolResult.map(tr => {
|
|
172
|
+
const resultAsString = typeof tr.result === 'string' ? tr.result : JSON.stringify(tr.result);
|
|
173
|
+
return { finished: true, id: tr.id, result: resultAsString, function: { name: tr.name, arguments: tr.arguments } };
|
|
174
|
+
});
|
|
175
|
+
yield { tool_calls: calls };
|
|
176
|
+
const toolRequestMessage = {
|
|
177
|
+
role: 'assistant',
|
|
178
|
+
content: toolResult.map(call => ({
|
|
179
|
+
type: 'tool_use',
|
|
180
|
+
id: call.id,
|
|
181
|
+
name: call.name,
|
|
182
|
+
input: JSON.parse(call.arguments)
|
|
183
|
+
}))
|
|
184
|
+
};
|
|
185
|
+
const toolResponseMessage = {
|
|
186
|
+
role: 'user',
|
|
187
|
+
content: toolResult.map(call => ({
|
|
188
|
+
type: 'tool_result',
|
|
189
|
+
tool_use_id: call.id,
|
|
190
|
+
content: that.formatToolCallResult(call.result)
|
|
191
|
+
}))
|
|
192
|
+
};
|
|
193
|
+
const result = await that.handleStreamingRequest(anthropic, request, cancellationToken, [...(toolMessages !== null && toolMessages !== void 0 ? toolMessages : []), toolRequestMessage, toolResponseMessage]);
|
|
194
|
+
for await (const nestedEvent of result.stream) {
|
|
195
|
+
yield nestedEvent;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
stream.on('error', (error) => {
|
|
201
|
+
console.error('Error in Anthropic streaming:', error);
|
|
202
|
+
});
|
|
203
|
+
return { stream: asyncIterator };
|
|
204
|
+
}
|
|
205
|
+
createTools(request) {
|
|
206
|
+
var _a;
|
|
207
|
+
return (_a = request.tools) === null || _a === void 0 ? void 0 : _a.map(tool => {
|
|
208
|
+
var _a;
|
|
209
|
+
return ({
|
|
210
|
+
name: tool.name,
|
|
211
|
+
description: tool.description,
|
|
212
|
+
input_schema: (_a = tool.parameters) !== null && _a !== void 0 ? _a : EMPTY_INPUT_SCHEMA
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
async handleNonStreamingRequest(anthropic, request) {
|
|
217
|
+
const settings = this.getSettings(request);
|
|
218
|
+
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
219
|
+
const params = {
|
|
220
|
+
max_tokens: DEFAULT_MAX_TOKENS_NON_STREAMING,
|
|
221
|
+
messages,
|
|
222
|
+
model: this.model,
|
|
223
|
+
...(systemMessage && { system: systemMessage }),
|
|
224
|
+
...settings,
|
|
225
|
+
};
|
|
226
|
+
try {
|
|
227
|
+
const response = await anthropic.messages.create(params);
|
|
228
|
+
const textContent = response.content[0];
|
|
229
|
+
if ((textContent === null || textContent === void 0 ? void 0 : textContent.type) === 'text') {
|
|
230
|
+
return { text: textContent.text };
|
|
231
|
+
}
|
|
232
|
+
return { text: '' };
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
throw new Error(`Failed to get response from Anthropic API: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
initializeAnthropic() {
|
|
239
|
+
const apiKey = this.apiKey();
|
|
240
|
+
if (!apiKey) {
|
|
241
|
+
throw new Error('Please provide ANTHROPIC_API_KEY in preferences or via environment variable');
|
|
242
|
+
}
|
|
243
|
+
return new sdk_1.Anthropic({ apiKey });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
exports.AnthropicModel = AnthropicModel;
|
|
247
|
+
//# sourceMappingURL=anthropic-language-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-language-model.js","sourceRoot":"","sources":["../../src/node/anthropic-language-model.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;;;AAWhF,sCAAyD;AACzD,2CAA8C;AAG9C,MAAM,4BAA4B,GAAG,IAAI,CAAC;AAC1C,MAAM,gCAAgC,GAAG,IAAI,CAAC;AAC9C,MAAM,kBAAkB,GAAG;IACvB,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE,EAAE;IACd,QAAQ,EAAE,EAAE;CACN,CAAC;AASX;;;;GAIG;AACH,SAAS,0BAA0B,CAC/B,QAAgD;IAEhD,2FAA2F;IAC3F,MAAM,gBAAgB,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC;IAC9E,MAAM,aAAa,GAAG,gBAAgB,aAAhB,gBAAgB,uBAAhB,gBAAgB,CAAE,KAAK,CAAC;IAE9C,MAAM,iBAAiB,GAAG,QAAQ;SAC7B,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ,CAAC;SAC7C,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACb,IAAI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AAE,IAAI,MAAK,MAAM,EAAE,CAAC;gBAC/B,OAAO,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,EAAE,CAAC;YACtC,CAAC;YAED,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACxB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,8CAA8C,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC;QAC9H,CAAC;IACL,CAAC;IAES,mBAAmB;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC7B,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,6EAA6E,CAAC,CAAC;QACnG,CAAC;QAED,OAAO,IAAI,eAAS,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IACrC,CAAC;CACJ;AAlND,wCAkNC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { LanguageModelRegistry } from '@theia/ai-core';
|
|
2
|
+
import { AnthropicLanguageModelsManager, AnthropicModelDescription } from '../common';
|
|
3
|
+
export declare class AnthropicLanguageModelsManagerImpl implements AnthropicLanguageModelsManager {
|
|
4
|
+
protected _apiKey: string | undefined;
|
|
5
|
+
protected readonly languageModelRegistry: LanguageModelRegistry;
|
|
6
|
+
get apiKey(): string | undefined;
|
|
7
|
+
createOrUpdateLanguageModels(...modelDescriptions: AnthropicModelDescription[]): Promise<void>;
|
|
8
|
+
removeLanguageModels(...modelIds: string[]): void;
|
|
9
|
+
setApiKey(apiKey: string | undefined): void;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=anthropic-language-models-manager-impl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-language-models-manager-impl.d.ts","sourceRoot":"","sources":["../../src/node/anthropic-language-models-manager-impl.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAEtF,qBACa,kCAAmC,YAAW,8BAA8B;IAErF,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAGtC,SAAS,CAAC,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB,CAAC;IAEhE,IAAI,MAAM,IAAI,MAAM,GAAG,SAAS,CAE/B;IAEK,4BAA4B,CAAC,GAAG,iBAAiB,EAAE,yBAAyB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAoCpG,oBAAoB,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI;IAIjD,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;CAO9C"}
|
|
@@ -0,0 +1,77 @@
|
|
|
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.AnthropicLanguageModelsManagerImpl = void 0;
|
|
19
|
+
const tslib_1 = require("tslib");
|
|
20
|
+
const ai_core_1 = require("@theia/ai-core");
|
|
21
|
+
const inversify_1 = require("@theia/core/shared/inversify");
|
|
22
|
+
const anthropic_language_model_1 = require("./anthropic-language-model");
|
|
23
|
+
let AnthropicLanguageModelsManagerImpl = class AnthropicLanguageModelsManagerImpl {
|
|
24
|
+
get apiKey() {
|
|
25
|
+
var _a;
|
|
26
|
+
return (_a = this._apiKey) !== null && _a !== void 0 ? _a : process.env.ANTHROPIC_API_KEY;
|
|
27
|
+
}
|
|
28
|
+
async createOrUpdateLanguageModels(...modelDescriptions) {
|
|
29
|
+
for (const modelDescription of modelDescriptions) {
|
|
30
|
+
const model = await this.languageModelRegistry.getLanguageModel(modelDescription.id);
|
|
31
|
+
const apiKeyProvider = () => {
|
|
32
|
+
if (modelDescription.apiKey === true) {
|
|
33
|
+
return this.apiKey;
|
|
34
|
+
}
|
|
35
|
+
if (modelDescription.apiKey) {
|
|
36
|
+
return modelDescription.apiKey;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
};
|
|
40
|
+
if (model) {
|
|
41
|
+
if (!(model instanceof anthropic_language_model_1.AnthropicModel)) {
|
|
42
|
+
console.warn(`Anthropic: model ${modelDescription.id} is not an Anthropic model`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
model.model = modelDescription.model;
|
|
46
|
+
model.enableStreaming = modelDescription.enableStreaming;
|
|
47
|
+
model.apiKey = apiKeyProvider;
|
|
48
|
+
model.defaultRequestSettings = modelDescription.defaultRequestSettings;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.languageModelRegistry.addLanguageModels([
|
|
52
|
+
new anthropic_language_model_1.AnthropicModel(modelDescription.id, modelDescription.model, modelDescription.enableStreaming, apiKeyProvider, modelDescription.defaultRequestSettings)
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
removeLanguageModels(...modelIds) {
|
|
58
|
+
this.languageModelRegistry.removeLanguageModels(modelIds);
|
|
59
|
+
}
|
|
60
|
+
setApiKey(apiKey) {
|
|
61
|
+
if (apiKey) {
|
|
62
|
+
this._apiKey = apiKey;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this._apiKey = undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
exports.AnthropicLanguageModelsManagerImpl = AnthropicLanguageModelsManagerImpl;
|
|
70
|
+
tslib_1.__decorate([
|
|
71
|
+
(0, inversify_1.inject)(ai_core_1.LanguageModelRegistry),
|
|
72
|
+
tslib_1.__metadata("design:type", Object)
|
|
73
|
+
], AnthropicLanguageModelsManagerImpl.prototype, "languageModelRegistry", void 0);
|
|
74
|
+
exports.AnthropicLanguageModelsManagerImpl = AnthropicLanguageModelsManagerImpl = tslib_1.__decorate([
|
|
75
|
+
(0, inversify_1.injectable)()
|
|
76
|
+
], AnthropicLanguageModelsManagerImpl);
|
|
77
|
+
//# sourceMappingURL=anthropic-language-models-manager-impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-language-models-manager-impl.js","sourceRoot":"","sources":["../../src/node/anthropic-language-models-manager-impl.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,4CAAuD;AACvD,4DAAkE;AAClE,yEAA4D;AAIrD,IAAM,kCAAkC,GAAxC,MAAM,kCAAkC;IAO3C,IAAI,MAAM;;QACN,OAAO,MAAA,IAAI,CAAC,OAAO,mCAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IACzD,CAAC;IAED,KAAK,CAAC,4BAA4B,CAAC,GAAG,iBAA8C;QAChF,KAAK,MAAM,gBAA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,CAAC,MAA0B;QAChC,IAAI,MAAM,EAAE,CAAC;YACT,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QAC1B,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;QAC7B,CAAC;IACL,CAAC;CACJ,CAAA;AA1DY,gFAAkC;AAKxB;IADlB,IAAA,kBAAM,EAAC,+BAAqB,CAAC;;iFACkC;6CALvD,kCAAkC;IAD9C,IAAA,sBAAU,GAAE;GACA,kCAAkC,CA0D9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=package.spec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.spec.d.ts","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2024 EclipseSource GmbH and others.
|
|
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
|
+
/* note: this bogus test file is required so that
|
|
17
|
+
we are able to run mocha unit tests on this
|
|
18
|
+
package, without having any actual unit tests in it.
|
|
19
|
+
This way a coverage report will be generated,
|
|
20
|
+
showing 0% coverage, instead of no report.
|
|
21
|
+
This file can be removed once we have real unit
|
|
22
|
+
tests in place. */
|
|
23
|
+
describe('ai-anthropic package', () => {
|
|
24
|
+
it('support code coverage statistics', () => true);
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=package.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.spec.js","sourceRoot":"","sources":["../src/package.spec.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,oDAAoD;AACpD,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;;;;;;qBAMqB;AAErB,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IAElC,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC;AACvD,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theia/ai-anthropic",
|
|
3
|
+
"version": "1.57.0-next.112+f4778c273",
|
|
4
|
+
"description": "Theia - Anthropic Integration",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@anthropic-ai/sdk": "^0.32.1",
|
|
7
|
+
"@theia/ai-core": "1.57.0-next.112+f4778c273",
|
|
8
|
+
"@theia/core": "1.57.0-next.112+f4778c273"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"theiaExtensions": [
|
|
14
|
+
{
|
|
15
|
+
"frontend": "lib/browser/anthropic-frontend-module",
|
|
16
|
+
"backend": "lib/node/anthropic-backend-module"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"theia-extension"
|
|
21
|
+
],
|
|
22
|
+
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/eclipse-theia/theia.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/eclipse-theia/theia/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/eclipse-theia/theia",
|
|
31
|
+
"files": [
|
|
32
|
+
"lib",
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "theiaext build",
|
|
37
|
+
"clean": "theiaext clean",
|
|
38
|
+
"compile": "theiaext compile",
|
|
39
|
+
"lint": "theiaext lint",
|
|
40
|
+
"test": "theiaext test",
|
|
41
|
+
"watch": "theiaext watch"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@theia/ext-scripts": "1.58.0"
|
|
45
|
+
},
|
|
46
|
+
"nyc": {
|
|
47
|
+
"extends": "../../configs/nyc.json"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "f4778c2737bb75613f0e1f99da8996bad91f6e17"
|
|
50
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
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 { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser';
|
|
18
|
+
import { inject, injectable } from '@theia/core/shared/inversify';
|
|
19
|
+
import { AnthropicLanguageModelsManager, AnthropicModelDescription } from '../common';
|
|
20
|
+
import { API_KEY_PREF, MODELS_PREF } from './anthropic-preferences';
|
|
21
|
+
import { PREFERENCE_NAME_REQUEST_SETTINGS, RequestSetting } from '@theia/ai-core/lib/browser/ai-core-preferences';
|
|
22
|
+
|
|
23
|
+
const ANTHROPIC_PROVIDER_ID = 'anthropic';
|
|
24
|
+
|
|
25
|
+
@injectable()
|
|
26
|
+
export class AnthropicFrontendApplicationContribution implements FrontendApplicationContribution {
|
|
27
|
+
|
|
28
|
+
@inject(PreferenceService)
|
|
29
|
+
protected preferenceService: PreferenceService;
|
|
30
|
+
|
|
31
|
+
@inject(AnthropicLanguageModelsManager)
|
|
32
|
+
protected manager: AnthropicLanguageModelsManager;
|
|
33
|
+
|
|
34
|
+
protected prevModels: string[] = [];
|
|
35
|
+
|
|
36
|
+
onStart(): void {
|
|
37
|
+
this.preferenceService.ready.then(() => {
|
|
38
|
+
const apiKey = this.preferenceService.get<string>(API_KEY_PREF, undefined);
|
|
39
|
+
this.manager.setApiKey(apiKey);
|
|
40
|
+
|
|
41
|
+
const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
|
|
42
|
+
const requestSettings = this.getRequestSettingsPref();
|
|
43
|
+
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createAnthropicModelDescription(modelId, requestSettings)));
|
|
44
|
+
this.prevModels = [...models];
|
|
45
|
+
|
|
46
|
+
this.preferenceService.onPreferenceChanged(event => {
|
|
47
|
+
if (event.preferenceName === API_KEY_PREF) {
|
|
48
|
+
this.manager.setApiKey(event.newValue);
|
|
49
|
+
} else if (event.preferenceName === MODELS_PREF) {
|
|
50
|
+
this.handleModelChanges(event.newValue as string[]);
|
|
51
|
+
} else if (event.preferenceName === PREFERENCE_NAME_REQUEST_SETTINGS) {
|
|
52
|
+
this.handleRequestSettingsChanges(event.newValue as RequestSetting[]);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected handleModelChanges(newModels: string[]): void {
|
|
59
|
+
const oldModels = new Set(this.prevModels);
|
|
60
|
+
const updatedModels = new Set(newModels);
|
|
61
|
+
|
|
62
|
+
const modelsToRemove = [...oldModels].filter(model => !updatedModels.has(model));
|
|
63
|
+
const modelsToAdd = [...updatedModels].filter(model => !oldModels.has(model));
|
|
64
|
+
|
|
65
|
+
this.manager.removeLanguageModels(...modelsToRemove.map(model => `${ANTHROPIC_PROVIDER_ID}/${model}`));
|
|
66
|
+
const requestSettings = this.getRequestSettingsPref();
|
|
67
|
+
this.manager.createOrUpdateLanguageModels(...modelsToAdd.map(modelId => this.createAnthropicModelDescription(modelId, requestSettings)));
|
|
68
|
+
this.prevModels = newModels;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private getRequestSettingsPref(): RequestSetting[] {
|
|
72
|
+
return this.preferenceService.get<RequestSetting[]>(PREFERENCE_NAME_REQUEST_SETTINGS, []);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected handleRequestSettingsChanges(newSettings: RequestSetting[]): void {
|
|
76
|
+
const models = this.preferenceService.get<string[]>(MODELS_PREF, []);
|
|
77
|
+
this.manager.createOrUpdateLanguageModels(...models.map(modelId => this.createAnthropicModelDescription(modelId, newSettings)));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected createAnthropicModelDescription(modelId: string, requestSettings: RequestSetting[]): AnthropicModelDescription {
|
|
81
|
+
const id = `${ANTHROPIC_PROVIDER_ID}/${modelId}`;
|
|
82
|
+
const modelRequestSetting = this.getMatchingRequestSetting(modelId, ANTHROPIC_PROVIDER_ID, requestSettings);
|
|
83
|
+
return {
|
|
84
|
+
id: id,
|
|
85
|
+
model: modelId,
|
|
86
|
+
apiKey: true,
|
|
87
|
+
enableStreaming: true,
|
|
88
|
+
defaultRequestSettings: modelRequestSetting?.requestSettings
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected getMatchingRequestSetting(
|
|
93
|
+
modelId: string,
|
|
94
|
+
providerId: string,
|
|
95
|
+
requestSettings: RequestSetting[]
|
|
96
|
+
): RequestSetting | undefined {
|
|
97
|
+
const matchingSettings = requestSettings.filter(
|
|
98
|
+
setting => (!setting.providerId || setting.providerId === providerId) && setting.modelId === modelId
|
|
99
|
+
);
|
|
100
|
+
if (matchingSettings.length > 1) {
|
|
101
|
+
console.warn(
|
|
102
|
+
`Multiple entries found for provider "${providerId}" and model "${modelId}". Using the first match.`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return matchingSettings[0];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 { ContainerModule } from '@theia/core/shared/inversify';
|
|
18
|
+
import { AnthropicPreferencesSchema } from './anthropic-preferences';
|
|
19
|
+
import { FrontendApplicationContribution, PreferenceContribution, RemoteConnectionProvider, ServiceConnectionProvider } from '@theia/core/lib/browser';
|
|
20
|
+
import { AnthropicFrontendApplicationContribution } from './anthropic-frontend-application-contribution';
|
|
21
|
+
import { ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, AnthropicLanguageModelsManager } from '../common';
|
|
22
|
+
|
|
23
|
+
export default new ContainerModule(bind => {
|
|
24
|
+
bind(PreferenceContribution).toConstantValue({ schema: AnthropicPreferencesSchema });
|
|
25
|
+
bind(AnthropicFrontendApplicationContribution).toSelf().inSingletonScope();
|
|
26
|
+
bind(FrontendApplicationContribution).toService(AnthropicFrontendApplicationContribution);
|
|
27
|
+
bind(AnthropicLanguageModelsManager).toDynamicValue(ctx => {
|
|
28
|
+
const provider = ctx.container.get<ServiceConnectionProvider>(RemoteConnectionProvider);
|
|
29
|
+
return provider.createProxy<AnthropicLanguageModelsManager>(ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH);
|
|
30
|
+
}).inSingletonScope();
|
|
31
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
|
|
18
|
+
import { AI_CORE_PREFERENCES_TITLE } from '@theia/ai-core/lib/browser/ai-core-preferences';
|
|
19
|
+
|
|
20
|
+
export const API_KEY_PREF = 'ai-features.anthropic.AnthropicApiKey';
|
|
21
|
+
export const MODELS_PREF = 'ai-features.anthropic.AnthropicModels';
|
|
22
|
+
|
|
23
|
+
export const AnthropicPreferencesSchema: PreferenceSchema = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
[API_KEY_PREF]: {
|
|
27
|
+
type: 'string',
|
|
28
|
+
markdownDescription: 'Enter an API Key of your official Anthropic Account. **Please note:** By using this preference the Anthropic API key will be stored in clear text\
|
|
29
|
+
on the machine running Theia. Use the environment variable `ANTHROPIC_API_KEY` to set the key securely.',
|
|
30
|
+
title: AI_CORE_PREFERENCES_TITLE,
|
|
31
|
+
},
|
|
32
|
+
[MODELS_PREF]: {
|
|
33
|
+
type: 'array',
|
|
34
|
+
description: 'Official Anthropic models to use',
|
|
35
|
+
title: AI_CORE_PREFERENCES_TITLE,
|
|
36
|
+
default: ['claude-3-5-sonnet-latest', 'claude-3-5-haiku-latest', 'claude-3-opus-latest'],
|
|
37
|
+
items: {
|
|
38
|
+
type: 'string'
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
};
|