-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from encorelab/625-feature-add-ai-assistant-c…
…hat-log-downloads Added download chat history button for AI Assistant
- Loading branch information
Showing
8 changed files
with
211 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// backend/src/api/chatHistory.ts | ||
|
||
import { Router } from 'express'; | ||
import dalChatMessage from '../repository/dalChatMessage'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/', async (req, res) => { | ||
try { | ||
const { boardId, userId } = req.body; | ||
const chatHistory = await dalChatMessage.getByBoardIdAndUserId( | ||
boardId, | ||
userId | ||
); | ||
|
||
// Format the chat history as a CSV string | ||
const csvString = formatChatHistoryAsCSV(chatHistory); | ||
|
||
res.setHeader('Content-Type', 'text/csv'); | ||
res.setHeader( | ||
'Content-Disposition', | ||
'attachment; filename="chat_history.csv"' | ||
); | ||
res.send(csvString); | ||
} catch (error) { | ||
console.error('Error fetching or formatting chat history:', error); | ||
res.status(500).json({ error: 'Failed to download chat history.' }); | ||
} | ||
}); | ||
|
||
function formatChatHistoryAsCSV(chatHistory: any[]): string { | ||
// 1. Create header row | ||
let csvString = 'Timestamp,Role,Content\n'; | ||
|
||
// 2. Add data rows | ||
for (const message of chatHistory) { | ||
csvString += `"${message.createdAt.toLocaleString()}","${ | ||
message.role | ||
}","${message.content.replace(/"/g, '""')}"\n`; | ||
} | ||
|
||
return csvString; | ||
} | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { prop, getModelForClass, modelOptions } from '@typegoose/typegoose'; | ||
|
||
@modelOptions({ | ||
schemaOptions: { collection: 'chatMessages', timestamps: true }, | ||
}) | ||
export class ChatMessageModel { | ||
@prop({ required: true }) | ||
public userId!: string; | ||
|
||
@prop({ required: true }) | ||
public boardId!: string; | ||
|
||
@prop({ required: true, enum: ['user', 'assistant'] }) | ||
public role!: string; | ||
|
||
@prop({ required: true }) | ||
public content!: string; | ||
} | ||
|
||
const ChatMessage = getModelForClass(ChatMessageModel); | ||
export default ChatMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import ChatMessage, { ChatMessageModel } from '../models/ChatMessage'; | ||
|
||
export const save = async (message: ChatMessageModel) => { | ||
try { | ||
const savedMessage = await ChatMessage.create(message); | ||
return savedMessage; | ||
} catch (err) { | ||
throw new Error(JSON.stringify(err, null, ' ')); | ||
} | ||
}; | ||
|
||
export const getByBoardIdAndUserId = async ( | ||
boardId: string, | ||
userId: string | ||
) => { | ||
try { | ||
const messages = await ChatMessage.find({ boardId, userId }).sort({ | ||
createdAt: 1, | ||
}); // Sort by createdAt in descending order (oldest to most recent) | ||
return messages; | ||
} catch (err) { | ||
throw new Error(JSON.stringify(err, null, ' ')); | ||
} | ||
}; | ||
|
||
const dalChatMessage = { | ||
save, | ||
getByBoardIdAndUserId, | ||
}; | ||
|
||
export default dalChatMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.