-
Notifications
You must be signed in to change notification settings - Fork 25
feat(assistants): Pass chat history to CodeMie agents #140
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
Open
Dark-Sun
wants to merge
5
commits into
codemie-ai:main
Choose a base branch
from
Dark-Sun:feature/assistants-chat-history
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0de2be
feat(assistants): pass the history from the session
Dark-Sun c2acc03
feat(assistants): fix the prompt
Dark-Sun ef9b94a
feat(assistants): fix the history passing
Dark-Sun 9f3e59b
feat(assistants): include pending messages
Dark-Sun 6250130
feat(assistants): refactor conversaton sync module
Dark-Sun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| /** | ||
| * Conversation History Loader | ||
| * | ||
| * Loads conversation history from session files stored in ~/.codemie/sessions | ||
| */ | ||
|
|
||
| import { existsSync } from 'fs'; | ||
|
|
||
| /** | ||
| * Maximum number of history messages to load from previous sessions | ||
| * This prevents sending excessively large context to the API | ||
| */ | ||
| const MAX_HISTORY_MESSAGES = 20; | ||
| import { logger } from '@/utils/logger.js'; | ||
| import { getSessionConversationPath } from '@/agents/core/session/session-config.js'; | ||
| import { readJSONL } from '@/providers/plugins/sso/session/utils/jsonl-reader.js'; | ||
| import { | ||
| type ConversationPayloadRecord, | ||
| CONVERSATION_SYNC_STATUS | ||
| } from '@/providers/plugins/sso/session/processors/conversations/types.js'; | ||
| import type { HistoryMessage } from '../constants.js'; | ||
|
|
||
| /** | ||
| * Load conversation history from session files | ||
| * | ||
| * @param conversationId - Optional conversation ID to load history for | ||
| * @returns Array of history messages, or empty array if none found or on error | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const history = await loadConversationHistory('abc-123'); | ||
| * console.log(`Loaded ${history.length} messages`); | ||
| * ``` | ||
| */ | ||
| export async function loadConversationHistory( | ||
| conversationId: string | undefined | ||
| ): Promise<HistoryMessage[]> { | ||
| if (!conversationId) return []; | ||
|
|
||
| try { | ||
| const filePath = getSessionConversationPath(conversationId); | ||
|
|
||
| // File doesn't exist yet - normal for first-time conversations | ||
| if (!existsSync(filePath)) { | ||
| logger.debug('Conversation history file not found (first-time conversation)', { | ||
| conversationId, | ||
| filePath | ||
| }); | ||
| return []; | ||
| } | ||
|
|
||
| const records = await readJSONL<ConversationPayloadRecord>(filePath); | ||
| const validRecords = records.filter( | ||
| record => record.status === CONVERSATION_SYNC_STATUS.SUCCESS || | ||
| record.status === CONVERSATION_SYNC_STATUS.PENDING | ||
| ); | ||
|
|
||
| if (validRecords.length === 0) { | ||
| logger.debug('No valid conversation records found', { | ||
| conversationId, | ||
| totalRecords: records.length | ||
| }); | ||
| return []; | ||
| } | ||
|
|
||
| const allMessages = validRecords | ||
| .flatMap(record => record.payload?.history ?? []) | ||
| .reduce((map, msg) => { | ||
| const key = `${msg.role}:${msg.message}:${msg.history_index ?? 0}`; | ||
| if (!map.has(key)) { | ||
| map.set(key, { | ||
| role: msg.role, | ||
| message: msg.message, | ||
| message_raw: msg.message | ||
| }); | ||
| } | ||
| return map; | ||
| }, new Map<string, HistoryMessage>()); | ||
|
|
||
| if (allMessages.size === 0) { | ||
| logger.debug('No history messages found in conversation records', { | ||
| conversationId | ||
| }); | ||
| return []; | ||
| } | ||
|
|
||
| const allHistory: HistoryMessage[] = Array.from(allMessages.values()); | ||
|
|
||
| return allHistory.slice(-MAX_HISTORY_MESSAGES); | ||
| } catch (error) { | ||
| logger.error('Failed to load conversation history', { | ||
| conversationId, | ||
| error: error instanceof Error ? error.message : String(error) | ||
| }); | ||
| return []; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
need to move it to config