Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions packages/cli/src/utils/sessionUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,28 @@ describe('convertSessionToHistoryFormats', () => {
});
});

it('should filter out <session_context> from UI history', () => {
const messages: MessageRecord[] = [
{
id: '1',
timestamp: new Date().toISOString(),
type: 'user',
content:
'<session_context>\nThis is the Gemini CLI\n</session_context>',
},
{
id: '2',
timestamp: new Date().toISOString(),
type: 'user',
content: 'Real message',
},
];

const result = convertSessionToHistoryFormats(messages);
expect(result.uiHistory).toHaveLength(1);
expect(result.uiHistory[0].text).toBe('Real message');
});

it('should handle missing tool descriptions and displayNames', () => {
const messages: MessageRecord[] = [
{
Expand Down
11 changes: 10 additions & 1 deletion packages/cli/src/utils/sessionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,16 @@ export function convertSessionToHistoryFormats(
const contentString = partListUnionToString(msg.content);
const uiText = displayContentString || contentString;

if (uiText.trim()) {
// Skip internal context messages in the UI history
const trimmedText = uiText.trim();
if (
trimmedText.startsWith('<session_context>') ||
trimmedText.startsWith('<hook_context>')
) {
continue;
}

if (trimmedText) {
let messageType: MessageType;
switch (msg.type) {
case 'user':
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/utils/sessionUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ describe('convertSessionToClientHistory', () => {
]);
});

it('should ignore <session_context> and <hook_context>', () => {
const messages: ConversationRecord['messages'] = [
{
id: '1',
type: 'user',
timestamp: '2024-01-01T10:00:00Z',
content: '<session_context>\nOld context\n</session_context>',
},
{
id: '2',
type: 'user',
timestamp: '2024-01-01T10:01:00Z',
content: '<hook_context>\nOld hook context\n</hook_context>',
},
{
id: '3',
type: 'user',
timestamp: '2024-01-01T10:02:00Z',
content: 'Actual query',
},
];

const history = convertSessionToClientHistory(messages);

expect(history.map((h) => h.content)).toEqual([
{ role: 'user', parts: [{ text: 'Actual query' }] },
]);
});

it('should correctly map tool calls and their responses', () => {
const messages: ConversationRecord['messages'] = [
{
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/utils/sessionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ export function convertSessionToClientHistory(

if (msg.type === 'user') {
const contentString = partListUnionToString(msg.content);
const trimmedContent = contentString.trim();
if (
contentString.trim().startsWith('/') ||
contentString.trim().startsWith('?')
trimmedContent.startsWith('/') ||
trimmedContent.startsWith('?') ||
trimmedContent.startsWith('<session_context>') ||
trimmedContent.startsWith('<hook_context>')
) {
continue;
}
Expand Down
Loading