Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support maxInputTokens for agent #4397

Merged
merged 7 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ai-native/src/browser/chat/chat-proxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export class ChatProxyService extends Disposable {
} else {
apiKey = this.preferenceService.get<string>(AINativeSettingSectionsId.AnthropicApiKey, '');
}

const MAX_INPUT_TOKENS = 30720;
const stream = await this.aiBackService.requestStream(
prompt,
{
requestId: request.requestId,
sessionId: request.sessionId,
history: this.aiChatService.getHistoryMessages(),
history: this.aiChatService.getHistoryMessages(MAX_INPUT_TOKENS),
clientId: this.applicationService.clientId,
apiKey,
model,
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-native/src/browser/chat/chat.api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class ChatService extends Disposable {
this._onChatMessageListLaunch.fire(list);
}

public getHistoryMessages(): IHistoryChatMessage[] {
return this.chatInternalService.sessionModel?.history.getMessages() || [];
public getHistoryMessages(maxInputTokens?: number): IHistoryChatMessage[] {
return this.chatInternalService.sessionModel?.history.getMessages(maxInputTokens) || [];
}

public scrollToBottom(): void {
Expand Down
9 changes: 9 additions & 0 deletions packages/ai-native/src/browser/chat/chat.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,12 @@
}
}
}

.chat_tips_container {
display: flex;
align-items: center;
justify-content: center;
color: var(--tab-inactiveForeground);
font-size: 12px;
margin-top: 16px;
}
8 changes: 8 additions & 0 deletions packages/ai-native/src/browser/chat/chat.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
IChatComponent,
IChatContent,
MessageType,
formatLocalize,
localize,
uuid,
} from '@opensumi/ide-core-common';
Expand Down Expand Up @@ -689,6 +690,13 @@ export const AIChatView = () => {
dataSource={messageListData}
/>
</div>
{msgHistoryManager.slicedMessageCount ? (
<div className={styles.chat_tips_text}>
<div className={styles.chat_tips_container}>
{formatLocalize('aiNative.chat.ai.assistant.limit.message', msgHistoryManager.slicedMessageCount)}
</div>
</div>
) : null}
<div className={styles.chat_input_wrap}>
<ChatContext />
<div className={styles.header_operate}>
Expand Down
23 changes: 22 additions & 1 deletion packages/ai-native/src/browser/model/msg-history-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class MsgHistoryManager extends Disposable {
public clearMessages() {
this.messageMap.clear();
this.messageAdditionalMap.clear();
this.startIndex = 0;
}

private doAddMessage(message: IExcludeMessage): string {
Expand All @@ -43,10 +44,30 @@ export class MsgHistoryManager extends Disposable {
return id;
}

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

private startIndex = 0;

private get totalTokens(): number {
const list = this.messageList.slice(this.startIndex);
return list.reduce((acc, msg) => acc + (msg.content.length || 0), 0) / 3;
}

public get slicedMessageCount(): number {
return this.startIndex;
}

public getMessages(maxTokens?: number): IHistoryChatMessage[] {
if (maxTokens && this.totalTokens > maxTokens) {
while (this.totalTokens > maxTokens) {
this.startIndex++;
}
}
return this.messageList.slice(this.startIndex);
}

public addUserMessage(
message: Required<Pick<IExcludeMessage, 'agentId' | 'agentCommand' | 'content' | 'relationId'>>,
): string {
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/en-US.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ export const localizationBundle = {
'aiNative.operate.clear.title': 'Clear',

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

'preference.ai.native.inlineChat.title': 'Inline Chat',
'preference.ai.native.chat.title': 'Chat',
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/zh-CN.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,7 @@ export const localizationBundle = {
'aiNative.operate.clear.title': '清空',

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

'preference.ai.native.inlineChat.title': 'Inline Chat',
'preference.ai.native.chat.title': 'Chat',
Expand Down
Loading