@theia/ai-anthropic 1.57.0 → 1.58.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.
- package/lib/node/anthropic-backend-module.d.ts.map +1 -1
- package/lib/node/anthropic-backend-module.js +6 -1
- package/lib/node/anthropic-backend-module.js.map +1 -1
- package/lib/node/anthropic-language-model.d.ts +12 -8
- package/lib/node/anthropic-language-model.d.ts.map +1 -1
- package/lib/node/anthropic-language-model.js +137 -22
- package/lib/node/anthropic-language-model.js.map +1 -1
- package/package.json +5 -5
- package/src/node/anthropic-backend-module.ts +7 -1
- package/src/node/anthropic-language-model.ts +154 -27
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-backend-module.d.ts","sourceRoot":"","sources":["../../src/node/anthropic-backend-module.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;;
|
|
1
|
+
{"version":3,"file":"anthropic-backend-module.d.ts","sourceRoot":"","sources":["../../src/node/anthropic-backend-module.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;;AAe/D,wBAEG"}
|
|
@@ -19,9 +19,14 @@ const inversify_1 = require("@theia/core/shared/inversify");
|
|
|
19
19
|
const anthropic_language_models_manager_1 = require("../common/anthropic-language-models-manager");
|
|
20
20
|
const core_1 = require("@theia/core");
|
|
21
21
|
const anthropic_language_models_manager_impl_1 = require("./anthropic-language-models-manager-impl");
|
|
22
|
-
|
|
22
|
+
const connection_container_module_1 = require("@theia/core/lib/node/messaging/connection-container-module");
|
|
23
|
+
// We use a connection module to handle AI services separately for each frontend.
|
|
24
|
+
const anthropicConnectionModule = connection_container_module_1.ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
|
|
23
25
|
bind(anthropic_language_models_manager_impl_1.AnthropicLanguageModelsManagerImpl).toSelf().inSingletonScope();
|
|
24
26
|
bind(anthropic_language_models_manager_1.AnthropicLanguageModelsManager).toService(anthropic_language_models_manager_impl_1.AnthropicLanguageModelsManagerImpl);
|
|
25
27
|
bind(core_1.ConnectionHandler).toDynamicValue(ctx => new core_1.RpcConnectionHandler(anthropic_language_models_manager_1.ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(anthropic_language_models_manager_1.AnthropicLanguageModelsManager))).inSingletonScope();
|
|
26
28
|
});
|
|
29
|
+
exports.default = new inversify_1.ContainerModule(bind => {
|
|
30
|
+
bind(connection_container_module_1.ConnectionContainerModule).toConstantValue(anthropicConnectionModule);
|
|
31
|
+
});
|
|
27
32
|
//# sourceMappingURL=anthropic-backend-module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-backend-module.js","sourceRoot":"","sources":["../../src/node/anthropic-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,mGAAqI;AACrI,sCAAsE;AACtE,qGAA8F;
|
|
1
|
+
{"version":3,"file":"anthropic-backend-module.js","sourceRoot":"","sources":["../../src/node/anthropic-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,mGAAqI;AACrI,sCAAsE;AACtE,qGAA8F;AAC9F,4GAAuG;AAEvG,iFAAiF;AACjF,MAAM,yBAAyB,GAAG,uDAAyB,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,EAAE,EAAE;IACrH,IAAI,CAAC,2EAAkC,CAAC,CAAC,MAAM,EAAE,CAAC,gBAAgB,EAAE,CAAC;IACrE,IAAI,CAAC,kEAA8B,CAAC,CAAC,SAAS,CAAC,2EAAkC,CAAC,CAAC;IACnF,IAAI,CAAC,wBAAiB,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CACzC,IAAI,2BAAoB,CAAC,0EAAsC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,kEAA8B,CAAC,CAAC,CAC5H,CAAC,gBAAgB,EAAE,CAAC;AACzB,CAAC,CAAC,CAAC;AAEH,kBAAe,IAAI,2BAAe,CAAC,IAAI,CAAC,EAAE;IACtC,IAAI,CAAC,uDAAyB,CAAC,CAAC,eAAe,CAAC,yBAAyB,CAAC,CAAC;AAC/E,CAAC,CAAC,CAAC"}
|
|
@@ -2,20 +2,24 @@ import { LanguageModel, LanguageModelRequest, LanguageModelResponse, LanguageMod
|
|
|
2
2
|
import { CancellationToken } from '@theia/core';
|
|
3
3
|
import { Anthropic } from '@anthropic-ai/sdk';
|
|
4
4
|
export declare const AnthropicModelIdentifier: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* Implements the Anthropic language model integration for Theia
|
|
7
|
+
*/
|
|
5
8
|
export declare class AnthropicModel implements LanguageModel {
|
|
6
9
|
readonly id: string;
|
|
7
10
|
model: string;
|
|
8
11
|
enableStreaming: boolean;
|
|
9
12
|
apiKey: () => string | undefined;
|
|
10
|
-
defaultRequestSettings?:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
constructor(id: string, model: string, enableStreaming: boolean, apiKey: () => string | undefined, defaultRequestSettings?: {
|
|
14
|
-
[key: string]: unknown;
|
|
15
|
-
} | undefined);
|
|
16
|
-
protected getSettings(request: LanguageModelRequest): Record<string, unknown>;
|
|
13
|
+
defaultRequestSettings?: Readonly<Record<string, unknown>> | undefined;
|
|
14
|
+
constructor(id: string, model: string, enableStreaming: boolean, apiKey: () => string | undefined, defaultRequestSettings?: Readonly<Record<string, unknown>> | undefined);
|
|
15
|
+
protected getSettings(request: LanguageModelRequest): Readonly<Record<string, unknown>>;
|
|
17
16
|
request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse>;
|
|
18
|
-
protected
|
|
17
|
+
protected formatToolCallResult(result: unknown): string | Array<{
|
|
18
|
+
type: 'text';
|
|
19
|
+
text: string;
|
|
20
|
+
}>;
|
|
21
|
+
protected handleStreamingRequest(anthropic: Anthropic, request: LanguageModelRequest, cancellationToken?: CancellationToken, toolMessages?: readonly Anthropic.Messages.MessageParam[]): Promise<LanguageModelStreamResponse>;
|
|
22
|
+
private createTools;
|
|
19
23
|
protected handleNonStreamingRequest(anthropic: Anthropic, request: LanguageModelRequest): Promise<LanguageModelTextResponse>;
|
|
20
24
|
protected initializeAnthropic(): Anthropic;
|
|
21
25
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-language-model.d.ts","sourceRoot":"","sources":["../../src/node/anthropic-language-model.ts"],"names":[],"mappings":"AAgBA,OAAO,EACH,aAAa,EACb,oBAAoB,EAEpB,qBAAqB,EACrB,2BAA2B,EAE3B,yBAAyB,EAC5B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,iBAAiB,
|
|
1
|
+
{"version":3,"file":"anthropic-language-model.d.ts","sourceRoot":"","sources":["../../src/node/anthropic-language-model.ts"],"names":[],"mappings":"AAgBA,OAAO,EACH,aAAa,EACb,oBAAoB,EAEpB,qBAAqB,EACrB,2BAA2B,EAE3B,yBAAyB,EAC5B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAW,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AA2C9C,eAAO,MAAM,wBAAwB,eAAqC,CAAC;AAgB3E;;GAEG;AACH,qBAAa,cAAe,YAAW,aAAa;aAG5B,EAAE,EAAE,MAAM;IACnB,KAAK,EAAE,MAAM;IACb,eAAe,EAAE,OAAO;IACxB,MAAM,EAAE,MAAM,MAAM,GAAG,SAAS;IAChC,sBAAsB,CAAC;gBAJd,EAAE,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EACb,eAAe,EAAE,OAAO,EACxB,MAAM,EAAE,MAAM,MAAM,GAAG,SAAS,EAChC,sBAAsB,CAAC,+CAAmC;IAGrE,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,oBAAoB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAIjF,OAAO,CAAC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAkBnH,SAAS,CAAC,oBAAoB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;cAiB/E,sBAAsB,CAClC,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE,oBAAoB,EAC7B,iBAAiB,CAAC,EAAE,iBAAiB,EACrC,YAAY,CAAC,EAAE,SAAS,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,GAC1D,OAAO,CAAC,2BAA2B,CAAC;IA8GvC,OAAO,CAAC,WAAW;cAQH,yBAAyB,CACrC,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE,oBAAoB,GAC9B,OAAO,CAAC,yBAAyB,CAAC;IA2BrC,SAAS,CAAC,mBAAmB,IAAI,SAAS;CAQ7C"}
|
|
@@ -16,8 +16,20 @@
|
|
|
16
16
|
// *****************************************************************************
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.AnthropicModel = exports.AnthropicModelIdentifier = void 0;
|
|
19
|
+
const core_1 = require("@theia/core");
|
|
19
20
|
const sdk_1 = require("@anthropic-ai/sdk");
|
|
20
|
-
|
|
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
|
+
*/
|
|
21
33
|
function transformToAnthropicParams(messages) {
|
|
22
34
|
// Extract the system message (if any), as it is a separate parameter in the Anthropic API.
|
|
23
35
|
const systemMessageObj = messages.find(message => message.actor === 'system');
|
|
@@ -33,6 +45,12 @@ function transformToAnthropicParams(messages) {
|
|
|
33
45
|
systemMessage,
|
|
34
46
|
};
|
|
35
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
|
+
*/
|
|
36
54
|
function toAnthropicRole(message) {
|
|
37
55
|
switch (message.actor) {
|
|
38
56
|
case 'ai':
|
|
@@ -41,6 +59,9 @@ function toAnthropicRole(message) {
|
|
|
41
59
|
return 'user';
|
|
42
60
|
}
|
|
43
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Implements the Anthropic language model integration for Theia
|
|
64
|
+
*/
|
|
44
65
|
class AnthropicModel {
|
|
45
66
|
constructor(id, model, enableStreaming, apiKey, defaultRequestSettings) {
|
|
46
67
|
this.id = id;
|
|
@@ -50,25 +71,47 @@ class AnthropicModel {
|
|
|
50
71
|
this.defaultRequestSettings = defaultRequestSettings;
|
|
51
72
|
}
|
|
52
73
|
getSettings(request) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return {};
|
|
56
|
-
}
|
|
57
|
-
return settings;
|
|
74
|
+
var _a, _b;
|
|
75
|
+
return (_b = (_a = request.settings) !== null && _a !== void 0 ? _a : this.defaultRequestSettings) !== null && _b !== void 0 ? _b : {};
|
|
58
76
|
}
|
|
59
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
|
+
}
|
|
60
82
|
const anthropic = this.initializeAnthropic();
|
|
61
|
-
|
|
62
|
-
|
|
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}`);
|
|
63
92
|
}
|
|
64
|
-
return this.handleNonStreamingRequest(anthropic, request);
|
|
65
93
|
}
|
|
66
|
-
|
|
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) {
|
|
67
108
|
const settings = this.getSettings(request);
|
|
68
109
|
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
110
|
+
const tools = this.createTools(request);
|
|
69
111
|
const params = {
|
|
70
|
-
max_tokens:
|
|
71
|
-
messages,
|
|
112
|
+
max_tokens: DEFAULT_MAX_TOKENS_STREAMING,
|
|
113
|
+
messages: [...messages, ...(toolMessages !== null && toolMessages !== void 0 ? toolMessages : [])],
|
|
114
|
+
tools,
|
|
72
115
|
model: this.model,
|
|
73
116
|
...(systemMessage && { system: systemMessage }),
|
|
74
117
|
...settings
|
|
@@ -77,20 +120,79 @@ class AnthropicModel {
|
|
|
77
120
|
cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.onCancellationRequested(() => {
|
|
78
121
|
stream.abort();
|
|
79
122
|
});
|
|
123
|
+
const that = this;
|
|
80
124
|
const asyncIterator = {
|
|
81
125
|
async *[Symbol.asyncIterator]() {
|
|
126
|
+
const toolCalls = [];
|
|
127
|
+
let toolCall;
|
|
82
128
|
for await (const event of stream) {
|
|
83
129
|
if (event.type === 'content_block_start') {
|
|
84
130
|
const contentBlock = event.content_block;
|
|
85
131
|
if (contentBlock.type === 'text') {
|
|
86
132
|
yield { content: contentBlock.text };
|
|
87
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
|
+
}
|
|
88
138
|
}
|
|
89
139
|
else if (event.type === 'content_block_delta') {
|
|
90
140
|
const delta = event.delta;
|
|
91
141
|
if (delta.type === 'text_delta') {
|
|
92
142
|
yield { content: delta.text };
|
|
93
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;
|
|
94
196
|
}
|
|
95
197
|
}
|
|
96
198
|
},
|
|
@@ -100,32 +202,45 @@ class AnthropicModel {
|
|
|
100
202
|
});
|
|
101
203
|
return { stream: asyncIterator };
|
|
102
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
|
+
}
|
|
103
216
|
async handleNonStreamingRequest(anthropic, request) {
|
|
104
217
|
const settings = this.getSettings(request);
|
|
105
218
|
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
106
219
|
const params = {
|
|
107
|
-
max_tokens:
|
|
220
|
+
max_tokens: DEFAULT_MAX_TOKENS_NON_STREAMING,
|
|
108
221
|
messages,
|
|
109
222
|
model: this.model,
|
|
110
223
|
...(systemMessage && { system: systemMessage }),
|
|
111
224
|
...settings,
|
|
112
225
|
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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'}`);
|
|
118
236
|
}
|
|
119
|
-
return {
|
|
120
|
-
text: '',
|
|
121
|
-
};
|
|
122
237
|
}
|
|
123
238
|
initializeAnthropic() {
|
|
124
239
|
const apiKey = this.apiKey();
|
|
125
240
|
if (!apiKey) {
|
|
126
241
|
throw new Error('Please provide ANTHROPIC_API_KEY in preferences or via environment variable');
|
|
127
242
|
}
|
|
128
|
-
return new sdk_1.Anthropic({ apiKey
|
|
243
|
+
return new sdk_1.Anthropic({ apiKey });
|
|
129
244
|
}
|
|
130
245
|
}
|
|
131
246
|
exports.AnthropicModel = AnthropicModel;
|
|
@@ -1 +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;;;
|
|
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,IA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{
|
|
2
2
|
"name": "@theia/ai-anthropic",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.58.0",
|
|
4
4
|
"description": "Theia - Anthropic Integration",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@anthropic-ai/sdk": "^0.32.1",
|
|
7
|
-
"@theia/ai-core": "1.
|
|
8
|
-
"@theia/core": "1.
|
|
7
|
+
"@theia/ai-core": "1.58.0",
|
|
8
|
+
"@theia/core": "1.58.0"
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
@@ -41,10 +41,10 @@
|
|
|
41
41
|
"watch": "theiaext watch"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@theia/ext-scripts": "1.
|
|
44
|
+
"@theia/ext-scripts": "1.58.0"
|
|
45
45
|
},
|
|
46
46
|
"nyc": {
|
|
47
47
|
"extends": "../../configs/nyc.json"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "6594f32b9727aea20d88934bf386dee06d08fa5e"
|
|
50
50
|
}
|
|
@@ -18,11 +18,17 @@ import { ContainerModule } from '@theia/core/shared/inversify';
|
|
|
18
18
|
import { ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, AnthropicLanguageModelsManager } from '../common/anthropic-language-models-manager';
|
|
19
19
|
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
|
|
20
20
|
import { AnthropicLanguageModelsManagerImpl } from './anthropic-language-models-manager-impl';
|
|
21
|
+
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
// We use a connection module to handle AI services separately for each frontend.
|
|
24
|
+
const anthropicConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
|
|
23
25
|
bind(AnthropicLanguageModelsManagerImpl).toSelf().inSingletonScope();
|
|
24
26
|
bind(AnthropicLanguageModelsManager).toService(AnthropicLanguageModelsManagerImpl);
|
|
25
27
|
bind(ConnectionHandler).toDynamicValue(ctx =>
|
|
26
28
|
new RpcConnectionHandler(ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(AnthropicLanguageModelsManager))
|
|
27
29
|
).inSingletonScope();
|
|
28
30
|
});
|
|
31
|
+
|
|
32
|
+
export default new ContainerModule(bind => {
|
|
33
|
+
bind(ConnectionContainerModule).toConstantValue(anthropicConnectionModule);
|
|
34
|
+
});
|
|
@@ -23,14 +23,32 @@ import {
|
|
|
23
23
|
LanguageModelStreamResponsePart,
|
|
24
24
|
LanguageModelTextResponse
|
|
25
25
|
} from '@theia/ai-core';
|
|
26
|
-
import { CancellationToken } from '@theia/core';
|
|
26
|
+
import { CancellationToken, isArray } from '@theia/core';
|
|
27
27
|
import { Anthropic } from '@anthropic-ai/sdk';
|
|
28
28
|
import { MessageParam } from '@anthropic-ai/sdk/resources';
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
const DEFAULT_MAX_TOKENS_STREAMING = 4096;
|
|
31
|
+
const DEFAULT_MAX_TOKENS_NON_STREAMING = 2048;
|
|
32
|
+
const EMPTY_INPUT_SCHEMA = {
|
|
33
|
+
type: 'object',
|
|
34
|
+
properties: {},
|
|
35
|
+
required: []
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
interface ToolCallback {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly index: number;
|
|
42
|
+
args: string;
|
|
43
|
+
}
|
|
31
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Transforms Theia language model messages to Anthropic API format
|
|
47
|
+
* @param messages Array of LanguageModelRequestMessage to transform
|
|
48
|
+
* @returns Object containing transformed messages and optional system message
|
|
49
|
+
*/
|
|
32
50
|
function transformToAnthropicParams(
|
|
33
|
-
messages: LanguageModelRequestMessage[]
|
|
51
|
+
messages: readonly LanguageModelRequestMessage[]
|
|
34
52
|
): { messages: MessageParam[]; systemMessage?: string } {
|
|
35
53
|
// Extract the system message (if any), as it is a separate parameter in the Anthropic API.
|
|
36
54
|
const systemMessageObj = messages.find(message => message.actor === 'system');
|
|
@@ -49,6 +67,13 @@ function transformToAnthropicParams(
|
|
|
49
67
|
};
|
|
50
68
|
}
|
|
51
69
|
|
|
70
|
+
export const AnthropicModelIdentifier = Symbol('AnthropicModelIdentifier');
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Converts Theia message actor to Anthropic role
|
|
74
|
+
* @param message The message to convert
|
|
75
|
+
* @returns Anthropic role ('user' or 'assistant')
|
|
76
|
+
*/
|
|
52
77
|
function toAnthropicRole(message: LanguageModelRequestMessage): 'user' | 'assistant' {
|
|
53
78
|
switch (message.actor) {
|
|
54
79
|
case 'ai':
|
|
@@ -58,6 +83,9 @@ function toAnthropicRole(message: LanguageModelRequestMessage): 'user' | 'assist
|
|
|
58
83
|
}
|
|
59
84
|
}
|
|
60
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Implements the Anthropic language model integration for Theia
|
|
88
|
+
*/
|
|
61
89
|
export class AnthropicModel implements LanguageModel {
|
|
62
90
|
|
|
63
91
|
constructor(
|
|
@@ -65,36 +93,61 @@ export class AnthropicModel implements LanguageModel {
|
|
|
65
93
|
public model: string,
|
|
66
94
|
public enableStreaming: boolean,
|
|
67
95
|
public apiKey: () => string | undefined,
|
|
68
|
-
public defaultRequestSettings?:
|
|
96
|
+
public defaultRequestSettings?: Readonly<Record<string, unknown>>
|
|
69
97
|
) { }
|
|
70
98
|
|
|
71
|
-
protected getSettings(request: LanguageModelRequest): Record<string, unknown
|
|
72
|
-
|
|
73
|
-
if (!settings) {
|
|
74
|
-
return {};
|
|
75
|
-
}
|
|
76
|
-
return settings;
|
|
99
|
+
protected getSettings(request: LanguageModelRequest): Readonly<Record<string, unknown>> {
|
|
100
|
+
return request.settings ?? this.defaultRequestSettings ?? {};
|
|
77
101
|
}
|
|
78
102
|
|
|
79
103
|
async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
|
|
104
|
+
if (!request.messages?.length) {
|
|
105
|
+
throw new Error('Request must contain at least one message');
|
|
106
|
+
}
|
|
107
|
+
|
|
80
108
|
const anthropic = this.initializeAnthropic();
|
|
81
|
-
|
|
82
|
-
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
if (this.enableStreaming) {
|
|
112
|
+
return this.handleStreamingRequest(anthropic, request, cancellationToken);
|
|
113
|
+
}
|
|
114
|
+
return this.handleNonStreamingRequest(anthropic, request);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
|
|
117
|
+
throw new Error(`Anthropic API request failed: ${errorMessage}`);
|
|
83
118
|
}
|
|
84
|
-
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
protected formatToolCallResult(result: unknown): string | Array<{ type: 'text', text: string }> {
|
|
122
|
+
if (typeof result === 'object' && result && 'content' in result && Array.isArray(result.content) &&
|
|
123
|
+
result.content.every(item => typeof item === 'object' && item && 'type' in item && 'text' in item)) {
|
|
124
|
+
return result.content;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (isArray(result)) {
|
|
128
|
+
return result.map(r => ({ type: 'text', text: r as string }));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (typeof result === 'object') {
|
|
132
|
+
return JSON.stringify(result);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return result as string;
|
|
85
136
|
}
|
|
86
137
|
|
|
87
138
|
protected async handleStreamingRequest(
|
|
88
139
|
anthropic: Anthropic,
|
|
89
140
|
request: LanguageModelRequest,
|
|
90
|
-
cancellationToken?: CancellationToken
|
|
141
|
+
cancellationToken?: CancellationToken,
|
|
142
|
+
toolMessages?: readonly Anthropic.Messages.MessageParam[]
|
|
91
143
|
): Promise<LanguageModelStreamResponse> {
|
|
92
144
|
const settings = this.getSettings(request);
|
|
93
145
|
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
94
|
-
|
|
146
|
+
const tools = this.createTools(request);
|
|
95
147
|
const params: Anthropic.MessageCreateParams = {
|
|
96
|
-
max_tokens:
|
|
97
|
-
messages,
|
|
148
|
+
max_tokens: DEFAULT_MAX_TOKENS_STREAMING,
|
|
149
|
+
messages: [...messages, ...(toolMessages ?? [])],
|
|
150
|
+
tools,
|
|
98
151
|
model: this.model,
|
|
99
152
|
...(systemMessage && { system: systemMessage }),
|
|
100
153
|
...settings
|
|
@@ -105,9 +158,14 @@ export class AnthropicModel implements LanguageModel {
|
|
|
105
158
|
cancellationToken?.onCancellationRequested(() => {
|
|
106
159
|
stream.abort();
|
|
107
160
|
});
|
|
161
|
+
const that = this;
|
|
108
162
|
|
|
109
163
|
const asyncIterator = {
|
|
110
164
|
async *[Symbol.asyncIterator](): AsyncIterator<LanguageModelStreamResponsePart> {
|
|
165
|
+
|
|
166
|
+
const toolCalls: ToolCallback[] = [];
|
|
167
|
+
let toolCall: ToolCallback | undefined;
|
|
168
|
+
|
|
111
169
|
for await (const event of stream) {
|
|
112
170
|
if (event.type === 'content_block_start') {
|
|
113
171
|
const contentBlock = event.content_block;
|
|
@@ -115,12 +173,71 @@ export class AnthropicModel implements LanguageModel {
|
|
|
115
173
|
if (contentBlock.type === 'text') {
|
|
116
174
|
yield { content: contentBlock.text };
|
|
117
175
|
}
|
|
176
|
+
if (contentBlock.type === 'tool_use') {
|
|
177
|
+
toolCall = { name: contentBlock.name!, args: '', id: contentBlock.id!, index: event.index };
|
|
178
|
+
yield { tool_calls: [{ finished: false, id: toolCall.id, function: { name: toolCall.name, arguments: toolCall.args } }] };
|
|
179
|
+
}
|
|
118
180
|
} else if (event.type === 'content_block_delta') {
|
|
119
181
|
const delta = event.delta;
|
|
120
182
|
|
|
121
183
|
if (delta.type === 'text_delta') {
|
|
122
184
|
yield { content: delta.text };
|
|
123
185
|
}
|
|
186
|
+
if (toolCall && delta.type === 'input_json_delta') {
|
|
187
|
+
toolCall.args += delta.partial_json;
|
|
188
|
+
yield { tool_calls: [{ function: { arguments: delta.partial_json } }] };
|
|
189
|
+
}
|
|
190
|
+
} else if (event.type === 'content_block_stop') {
|
|
191
|
+
if (toolCall && toolCall.index === event.index) {
|
|
192
|
+
toolCalls.push(toolCall);
|
|
193
|
+
toolCall = undefined;
|
|
194
|
+
}
|
|
195
|
+
} else if (event.type === 'message_delta') {
|
|
196
|
+
if (event.delta.stop_reason === 'max_tokens') {
|
|
197
|
+
if (toolCall) {
|
|
198
|
+
yield { tool_calls: [{ finished: true, id: toolCall.id }] };
|
|
199
|
+
}
|
|
200
|
+
throw new Error(`The response was stopped because it exceeded the max token limit of ${event.usage.output_tokens}.`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (toolCalls.length > 0) {
|
|
205
|
+
const toolResult = await Promise.all(toolCalls.map(async tc => {
|
|
206
|
+
const tool = request.tools?.find(t => t.name === tc.name);
|
|
207
|
+
const argsObject = tc.args.length === 0 ? '{}' : tc.args;
|
|
208
|
+
|
|
209
|
+
return { name: tc.name, result: (await tool?.handler(argsObject)), id: tc.id, arguments: argsObject };
|
|
210
|
+
|
|
211
|
+
}));
|
|
212
|
+
|
|
213
|
+
const calls = toolResult.map(tr => {
|
|
214
|
+
const resultAsString = typeof tr.result === 'string' ? tr.result : JSON.stringify(tr.result);
|
|
215
|
+
return { finished: true, id: tr.id, result: resultAsString, function: { name: tr.name, arguments: tr.arguments } };
|
|
216
|
+
});
|
|
217
|
+
yield { tool_calls: calls };
|
|
218
|
+
|
|
219
|
+
const toolRequestMessage: Anthropic.Messages.MessageParam = {
|
|
220
|
+
role: 'assistant',
|
|
221
|
+
content: toolResult.map(call => ({
|
|
222
|
+
|
|
223
|
+
type: 'tool_use',
|
|
224
|
+
id: call.id,
|
|
225
|
+
name: call.name,
|
|
226
|
+
input: JSON.parse(call.arguments)
|
|
227
|
+
}))
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const toolResponseMessage: Anthropic.Messages.MessageParam = {
|
|
231
|
+
role: 'user',
|
|
232
|
+
content: toolResult.map(call => ({
|
|
233
|
+
type: 'tool_result',
|
|
234
|
+
tool_use_id: call.id!,
|
|
235
|
+
content: that.formatToolCallResult(call.result)
|
|
236
|
+
}))
|
|
237
|
+
};
|
|
238
|
+
const result = await that.handleStreamingRequest(anthropic, request, cancellationToken, [...(toolMessages ?? []), toolRequestMessage, toolResponseMessage]);
|
|
239
|
+
for await (const nestedEvent of result.stream) {
|
|
240
|
+
yield nestedEvent;
|
|
124
241
|
}
|
|
125
242
|
}
|
|
126
243
|
},
|
|
@@ -133,6 +250,14 @@ export class AnthropicModel implements LanguageModel {
|
|
|
133
250
|
return { stream: asyncIterator };
|
|
134
251
|
}
|
|
135
252
|
|
|
253
|
+
private createTools(request: LanguageModelRequest): Anthropic.Messages.Tool[] | undefined {
|
|
254
|
+
return request.tools?.map(tool => ({
|
|
255
|
+
name: tool.name,
|
|
256
|
+
description: tool.description,
|
|
257
|
+
input_schema: tool.parameters ?? EMPTY_INPUT_SCHEMA
|
|
258
|
+
} as Anthropic.Messages.Tool));
|
|
259
|
+
}
|
|
260
|
+
|
|
136
261
|
protected async handleNonStreamingRequest(
|
|
137
262
|
anthropic: Anthropic,
|
|
138
263
|
request: LanguageModelRequest
|
|
@@ -142,23 +267,25 @@ export class AnthropicModel implements LanguageModel {
|
|
|
142
267
|
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
|
|
143
268
|
|
|
144
269
|
const params: Anthropic.MessageCreateParams = {
|
|
145
|
-
max_tokens:
|
|
270
|
+
max_tokens: DEFAULT_MAX_TOKENS_NON_STREAMING,
|
|
146
271
|
messages,
|
|
147
272
|
model: this.model,
|
|
148
273
|
...(systemMessage && { system: systemMessage }),
|
|
149
274
|
...settings,
|
|
150
275
|
};
|
|
151
276
|
|
|
152
|
-
|
|
277
|
+
try {
|
|
278
|
+
const response = await anthropic.messages.create(params);
|
|
279
|
+
const textContent = response.content[0];
|
|
153
280
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
281
|
+
if (textContent?.type === 'text') {
|
|
282
|
+
return { text: textContent.text };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return { text: '' };
|
|
286
|
+
} catch (error) {
|
|
287
|
+
throw new Error(`Failed to get response from Anthropic API: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
158
288
|
}
|
|
159
|
-
return {
|
|
160
|
-
text: '',
|
|
161
|
-
};
|
|
162
289
|
}
|
|
163
290
|
|
|
164
291
|
protected initializeAnthropic(): Anthropic {
|
|
@@ -167,6 +294,6 @@ export class AnthropicModel implements LanguageModel {
|
|
|
167
294
|
throw new Error('Please provide ANTHROPIC_API_KEY in preferences or via environment variable');
|
|
168
295
|
}
|
|
169
296
|
|
|
170
|
-
return new Anthropic({ apiKey
|
|
297
|
+
return new Anthropic({ apiKey });
|
|
171
298
|
}
|
|
172
299
|
}
|