@theia/ai-core 1.60.1 → 1.61.0-next.8
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/browser/ai-core-frontend-module.d.ts.map +1 -1
- package/lib/browser/ai-core-frontend-module.js +9 -0
- package/lib/browser/ai-core-frontend-module.js.map +1 -1
- package/lib/browser/prompttemplate-contribution.d.ts +0 -3
- package/lib/browser/prompttemplate-contribution.d.ts.map +1 -1
- package/lib/browser/prompttemplate-contribution.js +5 -24
- package/lib/browser/prompttemplate-contribution.js.map +1 -1
- package/lib/browser/token-usage-frontend-service-impl.d.ts +25 -0
- package/lib/browser/token-usage-frontend-service-impl.d.ts.map +1 -0
- package/lib/browser/token-usage-frontend-service-impl.js +120 -0
- package/lib/browser/token-usage-frontend-service-impl.js.map +1 -0
- package/lib/browser/token-usage-frontend-service.d.ts +29 -0
- package/lib/browser/token-usage-frontend-service.d.ts.map +1 -0
- package/lib/browser/token-usage-frontend-service.js +23 -0
- package/lib/browser/token-usage-frontend-service.js.map +1 -0
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +1 -0
- package/lib/common/index.js.map +1 -1
- package/lib/common/language-model.d.ts +7 -2
- package/lib/common/language-model.d.ts.map +1 -1
- package/lib/common/language-model.js +5 -1
- package/lib/common/language-model.js.map +1 -1
- package/lib/common/protocol.d.ts +14 -0
- package/lib/common/protocol.d.ts.map +1 -1
- package/lib/common/protocol.js +3 -1
- package/lib/common/protocol.js.map +1 -1
- package/lib/common/token-usage-service.d.ts +35 -0
- package/lib/common/token-usage-service.d.ts.map +1 -0
- package/lib/common/token-usage-service.js +20 -0
- package/lib/common/token-usage-service.js.map +1 -0
- package/lib/node/ai-core-backend-module.d.ts.map +1 -1
- package/lib/node/ai-core-backend-module.js +9 -0
- package/lib/node/ai-core-backend-module.js.map +1 -1
- package/lib/node/language-model-frontend-delegate.d.ts +2 -2
- package/lib/node/language-model-frontend-delegate.d.ts.map +1 -1
- package/lib/node/language-model-frontend-delegate.js.map +1 -1
- package/lib/node/token-usage-service-impl.d.ts +23 -0
- package/lib/node/token-usage-service-impl.d.ts.map +1 -0
- package/lib/node/token-usage-service-impl.js +65 -0
- package/lib/node/token-usage-service-impl.js.map +1 -0
- package/package.json +10 -10
- package/src/browser/ai-core-frontend-module.ts +15 -1
- package/src/browser/prompttemplate-contribution.ts +5 -26
- package/src/browser/token-usage-frontend-service-impl.ts +117 -0
- package/src/browser/token-usage-frontend-service.ts +47 -0
- package/src/common/index.ts +1 -0
- package/src/common/language-model.ts +10 -2
- package/src/common/protocol.ts +18 -0
- package/src/common/token-usage-service.ts +56 -0
- package/src/node/ai-core-backend-module.ts +21 -1
- package/src/node/language-model-frontend-delegate.ts +2 -2
- package/src/node/token-usage-service-impl.ts +65 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2025 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 { TokenUsageServiceClient } from './protocol';
|
|
18
|
+
|
|
19
|
+
export const TokenUsageService = Symbol('TokenUsageService');
|
|
20
|
+
|
|
21
|
+
export interface TokenUsage {
|
|
22
|
+
/** The input token count */
|
|
23
|
+
inputTokens: number;
|
|
24
|
+
/** The output token count */
|
|
25
|
+
outputTokens: number;
|
|
26
|
+
/** The model identifier */
|
|
27
|
+
model: string;
|
|
28
|
+
/** The timestamp of when the tokens were used */
|
|
29
|
+
timestamp: Date;
|
|
30
|
+
/** Request identifier */
|
|
31
|
+
requestId: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface TokenUsageParams {
|
|
35
|
+
/** The input token count */
|
|
36
|
+
inputTokens: number;
|
|
37
|
+
/** The output token count */
|
|
38
|
+
outputTokens: number;
|
|
39
|
+
/** Request identifier */
|
|
40
|
+
requestId: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface TokenUsageService {
|
|
44
|
+
/**
|
|
45
|
+
* Records token usage for a model interaction.
|
|
46
|
+
*
|
|
47
|
+
* @param model The identifier of the model that was used
|
|
48
|
+
* @param params Object containing token usage information
|
|
49
|
+
* @returns A promise that resolves when the token usage has been recorded
|
|
50
|
+
*/
|
|
51
|
+
recordTokenUsage(model: string, params: TokenUsageParams): Promise<void>;
|
|
52
|
+
|
|
53
|
+
getTokenUsages(): Promise<TokenUsage[]>;
|
|
54
|
+
|
|
55
|
+
setClient(tokenUsageClient: TokenUsageServiceClient): void;
|
|
56
|
+
}
|
|
@@ -36,9 +36,13 @@ import {
|
|
|
36
36
|
LanguageModelRegistryFrontendDelegate,
|
|
37
37
|
languageModelDelegatePath,
|
|
38
38
|
languageModelRegistryDelegatePath,
|
|
39
|
-
LanguageModelRegistryClient
|
|
39
|
+
LanguageModelRegistryClient,
|
|
40
|
+
TokenUsageService,
|
|
41
|
+
TokenUsageServiceClient,
|
|
42
|
+
TOKEN_USAGE_SERVICE_PATH
|
|
40
43
|
} from '../common';
|
|
41
44
|
import { BackendLanguageModelRegistry } from './backend-language-model-registry';
|
|
45
|
+
import { TokenUsageServiceImpl } from './token-usage-service-impl';
|
|
42
46
|
|
|
43
47
|
// We use a connection module to handle AI services separately for each frontend.
|
|
44
48
|
const aiCoreConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
|
|
@@ -46,6 +50,22 @@ const aiCoreConnectionModule = ConnectionContainerModule.create(({ bind, bindBac
|
|
|
46
50
|
bind(BackendLanguageModelRegistry).toSelf().inSingletonScope();
|
|
47
51
|
bind(LanguageModelRegistry).toService(BackendLanguageModelRegistry);
|
|
48
52
|
|
|
53
|
+
bind(TokenUsageService).to(TokenUsageServiceImpl).inSingletonScope();
|
|
54
|
+
|
|
55
|
+
bind(ConnectionHandler)
|
|
56
|
+
.toDynamicValue(
|
|
57
|
+
({ container }) =>
|
|
58
|
+
new RpcConnectionHandler<TokenUsageServiceClient>(
|
|
59
|
+
TOKEN_USAGE_SERVICE_PATH,
|
|
60
|
+
client => {
|
|
61
|
+
const service = container.get<TokenUsageService>(TokenUsageService);
|
|
62
|
+
service.setClient(client);
|
|
63
|
+
return service;
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
.inSingletonScope();
|
|
68
|
+
|
|
49
69
|
bind(LanguageModelRegistryFrontendDelegate).to(LanguageModelRegistryFrontendDelegateImpl).inSingletonScope();
|
|
50
70
|
bind(ConnectionHandler)
|
|
51
71
|
.toDynamicValue(
|
|
@@ -19,7 +19,6 @@ import { CancellationToken, CancellationTokenSource, ILogger, generateUuid } fro
|
|
|
19
19
|
import {
|
|
20
20
|
LanguageModelMetaData,
|
|
21
21
|
LanguageModelRegistry,
|
|
22
|
-
LanguageModelRequest,
|
|
23
22
|
isLanguageModelStreamResponse,
|
|
24
23
|
isLanguageModelTextResponse,
|
|
25
24
|
LanguageModelStreamResponsePart,
|
|
@@ -29,6 +28,7 @@ import {
|
|
|
29
28
|
LanguageModelResponseDelegate,
|
|
30
29
|
LanguageModelRegistryClient,
|
|
31
30
|
isLanguageModelParsedResponse,
|
|
31
|
+
UserRequest,
|
|
32
32
|
} from '../common';
|
|
33
33
|
import { BackendLanguageModelRegistry } from './backend-language-model-registry';
|
|
34
34
|
|
|
@@ -69,7 +69,7 @@ export class LanguageModelFrontendDelegateImpl implements LanguageModelFrontendD
|
|
|
69
69
|
|
|
70
70
|
async request(
|
|
71
71
|
modelId: string,
|
|
72
|
-
request:
|
|
72
|
+
request: UserRequest,
|
|
73
73
|
requestId: string,
|
|
74
74
|
cancellationToken?: CancellationToken
|
|
75
75
|
): Promise<LanguageModelResponseDelegate> {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2025 EclipseSource GmbH.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { injectable } from '@theia/core/shared/inversify';
|
|
18
|
+
import { TokenUsage, TokenUsageParams, TokenUsageService } from '../common/token-usage-service';
|
|
19
|
+
import { TokenUsageServiceClient } from '../common/protocol';
|
|
20
|
+
|
|
21
|
+
@injectable()
|
|
22
|
+
export class TokenUsageServiceImpl implements TokenUsageService {
|
|
23
|
+
private client: TokenUsageServiceClient | undefined;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sets the client to notify about token usage changes
|
|
27
|
+
*/
|
|
28
|
+
setClient(client: TokenUsageServiceClient | undefined): void {
|
|
29
|
+
this.client = client;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private readonly tokenUsages: TokenUsage[] = [];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Records token usage for a model interaction.
|
|
36
|
+
*
|
|
37
|
+
* @param model The model identifier
|
|
38
|
+
* @param params Token usage parameters
|
|
39
|
+
* @returns A promise that resolves when the token usage has been recorded
|
|
40
|
+
*/
|
|
41
|
+
async recordTokenUsage(model: string, params: TokenUsageParams): Promise<void> {
|
|
42
|
+
const usage: TokenUsage = {
|
|
43
|
+
inputTokens: params.inputTokens,
|
|
44
|
+
outputTokens: params.outputTokens,
|
|
45
|
+
model,
|
|
46
|
+
timestamp: new Date(),
|
|
47
|
+
requestId: params.requestId
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
this.tokenUsages.push(usage);
|
|
51
|
+
this.client?.notifyTokenUsage(usage);
|
|
52
|
+
|
|
53
|
+
console.log(`Input Tokens: ${params.inputTokens}; Output Tokens: ${params.outputTokens}; Model: ${model}${params.requestId ? `; RequestId: ${params.requestId}` : ''}`);
|
|
54
|
+
// For now we just store in memory
|
|
55
|
+
// In the future, this could be persisted to disk, a database, or sent to a service
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Gets all token usage records stored in this service.
|
|
61
|
+
*/
|
|
62
|
+
async getTokenUsages(): Promise<TokenUsage[]> {
|
|
63
|
+
return [...this.tokenUsages];
|
|
64
|
+
}
|
|
65
|
+
}
|