-
-
Notifications
You must be signed in to change notification settings - Fork 18
Backend ai conversation history #1549
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,7 @@ import { | |||||||||||||
| isValidMongoDbCommand, | ||||||||||||||
| wrapQueryWithLimit, | ||||||||||||||
| AIProviderType, | ||||||||||||||
| AIProviderConfig, | ||||||||||||||
| } from '../../../ai-core/index.js'; | ||||||||||||||
| import { UserAiChatEntity } from '../ai-conversation-history/user-ai-chat/user-ai-chat.entity.js'; | ||||||||||||||
| import { MessageRole } from '../ai-conversation-history/ai-chat-messages/message-role.enum.js'; | ||||||||||||||
|
|
@@ -94,24 +95,36 @@ export class RequestInfoFromTableWithAIUseCaseV7 | |||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const messages = new MessageBuilder().system(systemPrompt).human(user_message).build(); | ||||||||||||||
| const { messages, previousResponseId } = await this.buildMessagesWithHistory( | ||||||||||||||
| systemPrompt, | ||||||||||||||
| user_message, | ||||||||||||||
| foundUserAiChat.id, | ||||||||||||||
| isNewChat, | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| const { accumulatedResponse } = await this.processWithToolLoop( | ||||||||||||||
| const config: AIProviderConfig = {}; | ||||||||||||||
| if (this.aiProvider === AIProviderType.OPENAI && previousResponseId) { | ||||||||||||||
| config.previousResponseId = previousResponseId; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const { accumulatedResponse, lastResponseId } = await this.processWithToolLoop( | ||||||||||||||
| messages, | ||||||||||||||
| tools, | ||||||||||||||
| response, | ||||||||||||||
| dataAccessObject, | ||||||||||||||
| tableName, | ||||||||||||||
| userEmail, | ||||||||||||||
| foundConnection, | ||||||||||||||
| config, | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| if (accumulatedResponse) { | ||||||||||||||
| await this._dbContext.aiChatMessageRepository.saveMessage( | ||||||||||||||
| foundUserAiChat.id, | ||||||||||||||
| accumulatedResponse, | ||||||||||||||
| MessageRole.ai, | ||||||||||||||
| lastResponseId, | ||||||||||||||
| ); | ||||||||||||||
|
Comment on lines
122
to
128
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -133,15 +146,22 @@ export class RequestInfoFromTableWithAIUseCaseV7 | |||||||||||||
| inputTableName: string, | ||||||||||||||
| userEmail: string, | ||||||||||||||
| foundConnection: ConnectionEntity, | ||||||||||||||
| config: AIProviderConfig = {}, | ||||||||||||||
| ): Promise<{ lastResponseId: string | null; accumulatedResponse: string }> { | ||||||||||||||
| let currentMessages = [...messages]; | ||||||||||||||
| let lastResponseId: string | null = null; | ||||||||||||||
| let depth = 0; | ||||||||||||||
| let totalAccumulatedResponse = ''; | ||||||||||||||
| let currentConfig = { ...config }; | ||||||||||||||
|
|
||||||||||||||
| while (depth < this.maxDepth) { | ||||||||||||||
| try { | ||||||||||||||
| const stream = await this.aiCoreService.streamChatWithToolsAndProvider(this.aiProvider, currentMessages, tools); | ||||||||||||||
| const stream = await this.aiCoreService.streamChatWithToolsAndProvider( | ||||||||||||||
| this.aiProvider, | ||||||||||||||
| currentMessages, | ||||||||||||||
| tools, | ||||||||||||||
| currentConfig, | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| let pendingToolCalls: AIToolCall[] = []; | ||||||||||||||
| let accumulatedContent = ''; | ||||||||||||||
|
|
@@ -174,6 +194,10 @@ export class RequestInfoFromTableWithAIUseCaseV7 | |||||||||||||
| foundConnection, | ||||||||||||||
| ); | ||||||||||||||
|
|
||||||||||||||
| if (this.aiProvider === AIProviderType.OPENAI && lastResponseId) { | ||||||||||||||
|
||||||||||||||
| if (this.aiProvider === AIProviderType.OPENAI && lastResponseId) { | |
| if (lastResponseId) { |
Copilot
AI
Feb 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conditional check for OpenAI will never be true since aiProvider is hardcoded to AIProviderType.BEDROCK (line 40). The entire if-block (lines 385-390) will never execute, meaning this OpenAI-specific optimization is dead code.
| if (this.aiProvider === AIProviderType.OPENAI) { | |
| const lastAiMessage = await this._dbContext.aiChatMessageRepository.findLastAiMessageForChat(chatId); | |
| const previousResponseId = lastAiMessage?.response_id || null; | |
| const messages = new MessageBuilder().system(systemPrompt).human(userMessage).build(); | |
| return { messages, previousResponseId }; | |
| } |
Copilot
AI
Feb 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For non-OpenAI providers with existing chats, the user message is saved to the database first (line 90), then retrieved as part of the history (line 392), and then added again to the message builder (line 403). This results in the current user message being included twice in the conversation context sent to the AI provider. This duplication could confuse the AI and waste tokens. Consider either excluding the most recent user message from the history retrieval, or not saving it until after building the messages.
| builder.human(userMessage); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class AddResponseIdToAiChatMessage1769790101930 implements MigrationInterface { | ||
| name = 'AddResponseIdToAiChatMessage1769790101930'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "ai_chat_message" ADD "response_id" character varying(255)`); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "ai_chat_message" DROP COLUMN "response_id"`); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The aiProvider is hardcoded to AIProviderType.BEDROCK (line 40), but there's conditional logic checking for AIProviderType.OPENAI (lines 107-109). Unless the aiProvider can be changed at runtime or through configuration, this OpenAI-specific code will never execute. If OpenAI support is intended, consider making the aiProvider configurable. If not, the OpenAI-specific logic should be removed to avoid confusion.