Skip to content

Commit 2a827ab

Browse files
authored
Merge branch 'main' into chore/improve-chat-agent-styles
2 parents 2d6f85b + ebc1030 commit 2a827ab

9 files changed

Lines changed: 48 additions & 6 deletions

File tree

packages/ai-native/src/browser/chat/chat-proxy.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ export class ChatProxyService extends Disposable {
125125
} else {
126126
apiKey = this.preferenceService.get<string>(AINativeSettingSectionsId.AnthropicApiKey, '');
127127
}
128-
128+
const MAX_INPUT_TOKENS = 30720;
129129
const stream = await this.aiBackService.requestStream(
130130
prompt,
131131
{
132132
requestId: request.requestId,
133133
sessionId: request.sessionId,
134-
history: this.aiChatService.getHistoryMessages(),
134+
history: this.aiChatService.getHistoryMessages(MAX_INPUT_TOKENS),
135135
clientId: this.applicationService.clientId,
136136
apiKey,
137137
model,

packages/ai-native/src/browser/chat/chat.api.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export class ChatService extends Disposable {
6565
this._onChatMessageListLaunch.fire(list);
6666
}
6767

68-
public getHistoryMessages(): IHistoryChatMessage[] {
69-
return this.chatInternalService.sessionModel?.history.getMessages() || [];
68+
public getHistoryMessages(maxInputTokens?: number): IHistoryChatMessage[] {
69+
return this.chatInternalService.sessionModel?.history.getMessages(maxInputTokens) || [];
7070
}
7171

7272
public scrollToBottom(): void {

packages/ai-native/src/browser/chat/chat.module.less

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@
279279
}
280280
}
281281

282+
283+
.chat_tips_container {
284+
display: flex;
285+
align-items: center;
286+
justify-content: center;
287+
color: var(--tab-inactiveForeground);
288+
font-size: 12px;
289+
margin-top: 16px;
290+
}
291+
282292
.chat_history {
283293
width: calc(100% - 60px);
284294
color: var(--design-text-foreground);

packages/ai-native/src/browser/chat/chat.view.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
IChatComponent,
2727
IChatContent,
2828
MessageType,
29+
formatLocalize,
2930
localize,
3031
uuid,
3132
} from '@opensumi/ide-core-common';
@@ -673,6 +674,13 @@ export const AIChatView = () => {
673674
dataSource={messageListData}
674675
/>
675676
</div>
677+
{msgHistoryManager.slicedMessageCount ? (
678+
<div className={styles.chat_tips_text}>
679+
<div className={styles.chat_tips_container}>
680+
{formatLocalize('aiNative.chat.ai.assistant.limit.message', msgHistoryManager.slicedMessageCount)}
681+
</div>
682+
</div>
683+
) : null}
676684
<div className={styles.chat_input_wrap}>
677685
<ChatContext />
678686
<div className={styles.header_operate}>

packages/ai-native/src/browser/model/msg-history-manager.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class MsgHistoryManager extends Disposable {
3131
public clearMessages() {
3232
this.messageMap.clear();
3333
this.messageAdditionalMap.clear();
34+
this.startIndex = 0;
3435
}
3536

3637
private doAddMessage(message: IExcludeMessage): string {
@@ -50,10 +51,30 @@ export class MsgHistoryManager extends Disposable {
5051
return id;
5152
}
5253

53-
public getMessages(): IHistoryChatMessage[] {
54+
private get messageList(): IHistoryChatMessage[] {
5455
return Array.from(this.messageMap.values()).sort((a, b) => a.order - b.order);
5556
}
5657

58+
private startIndex = 0;
59+
60+
private get totalTokens(): number {
61+
const list = this.messageList.slice(this.startIndex);
62+
return list.reduce((acc, msg) => acc + (msg.content.length || 0), 0) / 3;
63+
}
64+
65+
public get slicedMessageCount(): number {
66+
return this.startIndex;
67+
}
68+
69+
public getMessages(maxTokens?: number): IHistoryChatMessage[] {
70+
if (maxTokens && this.totalTokens > maxTokens) {
71+
while (this.totalTokens > maxTokens) {
72+
this.startIndex++;
73+
}
74+
}
75+
return this.messageList.slice(this.startIndex);
76+
}
77+
5778
public addUserMessage(
5879
message: Required<Pick<IExcludeMessage, 'agentId' | 'agentCommand' | 'content' | 'relationId'>>,
5980
): string {

packages/ai-native/src/node/base-language-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export abstract class BaseLanguageModel {
4545
throw new Error('clientId is required');
4646
}
4747
const registry = this.toolInvocationRegistryManager.getRegistry(clientId);
48-
const allFunctions = registry.getAllFunctions();
48+
const allFunctions = options.noTool ? [] : registry.getAllFunctions();
4949
return this.handleStreamingRequest(
5050
provider,
5151
request,

packages/core-common/src/types/ai-native/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export interface IAIBackServiceOption {
182182
topP?: number;
183183
topK?: number;
184184
providerOptions?: any;
185+
noTool?: boolean;
185186
}
186187

187188
/**

packages/i18n/src/common/en-US.lang.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ export const localizationBundle = {
14921492
'aiNative.operate.chatHistory.delete': 'Delete',
14931493

14941494
'aiNative.chat.welcome.loading.text': 'Initializing...',
1495+
'aiNative.chat.ai.assistant.limit.message': '{0} earliest messages are dropped due to the input token limit',
14951496

14961497
'preference.ai.native.inlineChat.title': 'Inline Chat',
14971498
'preference.ai.native.chat.title': 'Chat',

packages/i18n/src/common/zh-CN.lang.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ export const localizationBundle = {
12601260
'aiNative.operate.chatHistory.delete': '删除',
12611261

12621262
'aiNative.chat.welcome.loading.text': '初始化中...',
1263+
'aiNative.chat.ai.assistant.limit.message': '{0} 条最早的消息因输入 Tokens 限制而被丢弃',
12631264

12641265
'preference.ai.native.inlineChat.title': 'Inline Chat',
12651266
'preference.ai.native.chat.title': 'Chat',

0 commit comments

Comments
 (0)