-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cody-chat-memory): Enhance chat memory management
This PR introduces the following improvements to the CodyChatMemory module: - Transition to a static utility class with a Map-based storage approach - Implement timestamp-based memory organization and automatic trimming to maintain the 10 most recent items - Enhance the retrieve() method to return a formatted ContextItem with the memory content - Add support for REMOVE command to clear the chat memory - Introduce comprehensive unit tests to cover various chat session workflows ## Test plan - Run the unit tests for the CodyChatMemory module - Verify the chat memory functionality in the Cody extension, including adding, retrieving, and clearing memories ## Changelog - Improved chat memory management with timestamp-based organization and automatic trimming - Added support for REMOVE command to clear the chat memory - Enhanced unit test coverage for chat memory workflows
- Loading branch information
Showing
3 changed files
with
213 additions
and
28 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,157 @@ | ||
import { ContextItemSource } from '@sourcegraph/cody-shared' | ||
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { URI } from 'vscode-uri' | ||
import { localStorage } from '../../services/LocalStorageProvider' | ||
import { CodyChatMemory } from './CodyChatMemory' | ||
|
||
// Mock localStorage | ||
vi.mock('../../services/LocalStorageProvider', () => ({ | ||
localStorage: { | ||
getChatMemory: vi.fn(), | ||
setChatMemory: vi.fn(), | ||
}, | ||
})) | ||
|
||
describe('CodyChatMemory Workflows', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterAll(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
describe('Chat Session Workflows', () => { | ||
interface TestScenario { | ||
name: string | ||
actions: Array<{ | ||
type: 'initialize' | 'load' | 'retrieve' | 'unload' | ||
input?: string | ||
expectedContent?: string | null | ||
expectedStorageCall?: boolean | ||
}> | ||
} | ||
|
||
const scenarios: TestScenario[] = [ | ||
{ | ||
name: 'New user first chat session', | ||
actions: [ | ||
{ | ||
type: 'initialize', | ||
expectedContent: null, | ||
expectedStorageCall: true, | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'User prefers TypeScript', | ||
// Update to match new timestamp + content format | ||
expectedContent: | ||
'## \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z\\nUser prefers TypeScript', | ||
expectedStorageCall: true, | ||
}, | ||
{ | ||
type: 'retrieve', | ||
expectedContent: 'User prefers TypeScript', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Multiple chat interactions in one session', | ||
actions: [ | ||
{ | ||
type: 'load', | ||
input: 'User likes unit testing', | ||
expectedContent: 'User likes unit testing', | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'User works on VS Code extensions', | ||
expectedContent: 'User works on VS Code extensions', | ||
}, | ||
{ | ||
type: 'retrieve', | ||
// Update regex to match new Map-based format with timestamps | ||
expectedContent: | ||
'## \\d{4}.*User likes unit testing.*## \\d{4}.*User works on VS Code extensions', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Memory capacity management with timestamps', | ||
actions: [ | ||
...Array.from({ length: 10 }, (_, i) => ({ | ||
type: 'load' as const, | ||
input: `Memory item ${i}`, | ||
// Verify only last 8 items are kept | ||
expectedContent: i >= 2 ? `Memory item ${i}` : null, | ||
})), | ||
{ | ||
type: 'retrieve', | ||
// Verify chronological order is maintained | ||
expectedContent: 'Memory item 2.*Memory item 9', | ||
}, | ||
], | ||
}, | ||
// Add new test scenario for timestamp ordering | ||
{ | ||
name: 'Timestamp ordering verification', | ||
actions: [ | ||
{ | ||
type: 'load', | ||
input: 'First message', | ||
}, | ||
{ | ||
type: 'load', | ||
input: 'Second message', | ||
}, | ||
{ | ||
type: 'retrieve', | ||
// Verify messages appear in chronological order with timestamps | ||
expectedContent: '.*First message.*Second message', | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
for (const scenario of scenarios) { | ||
it(scenario.name, () => { | ||
for (const action of scenario.actions) { | ||
switch (action.type) { | ||
case 'load': | ||
// Advance by 1 second to ensure unique timestamps | ||
vi.advanceTimersByTime(1000) | ||
CodyChatMemory.load(action.input!) | ||
if (action.expectedStorageCall) { | ||
expect(localStorage.setChatMemory).toHaveBeenCalled() | ||
} | ||
break | ||
|
||
case 'retrieve': { | ||
const retrieved = CodyChatMemory.retrieve() | ||
if (action.expectedContent === null) { | ||
expect(retrieved).toBeUndefined() | ||
} else { | ||
if (action.expectedContent) { | ||
expect(retrieved?.content).toMatch( | ||
new RegExp(action.expectedContent, 's') | ||
) | ||
} | ||
expect(retrieved?.source).toBe(ContextItemSource.Agentic) | ||
expect(retrieved?.uri).toEqual(URI.file('Cody Memory')) | ||
} | ||
break | ||
} | ||
case 'unload': { | ||
const lastState = CodyChatMemory.reset() | ||
if (action.expectedContent) { | ||
expect(lastState?.content).toContain(action.expectedContent) | ||
} | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
}) |
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