diff --git a/packages/ai-core/src/common/communication-recording-service.ts b/packages/ai-core/src/common/communication-recording-service.ts index 4ae26c47e0e2e..c0521031d2908 100644 --- a/packages/ai-core/src/common/communication-recording-service.ts +++ b/packages/ai-core/src/common/communication-recording-service.ts @@ -42,6 +42,8 @@ export interface CommunicationRecordingService { getHistory(agentId: string): CommunicationHistory; + getSessionHistory(sessionId: string): CommunicationHistory; + clearHistory(): void; readonly onStructuralChange: Event; } diff --git a/packages/ai-history/src/common/communication-recording-service.spec.ts b/packages/ai-history/src/common/communication-recording-service.spec.ts index 662d2d58c2706..f932f1dc4a172 100644 --- a/packages/ai-history/src/common/communication-recording-service.spec.ts +++ b/packages/ai-history/src/common/communication-recording-service.spec.ts @@ -34,4 +34,29 @@ describe('DefaultCommunicationRecordingService', () => { expect(history2[0].response).to.eq('dummy response'); }); + it('returns session history', () => { + const service = new DefaultCommunicationRecordingService(); + (service as unknown as { logger: ILogger }).logger = new MockLogger(); + // some requests and responses for session 1 + service.recordRequest({ agentId: 'agent', requestId: '1', sessionId: '1', timestamp: 100, request: 'session 1 request 1' }); + service.recordResponse({ agentId: 'agent', requestId: '1', sessionId: '1', timestamp: 200, response: 'session 1 response 1' }); + service.recordRequest({ agentId: 'agent2', requestId: '2', sessionId: '1', timestamp: 100, request: 'session 1 request 2' }); + service.recordResponse({ agentId: 'agent2', requestId: '2', sessionId: '1', timestamp: 200, response: 'session 1 response 2' }); + // some requests and responses for session 2 + service.recordRequest({ agentId: 'agent', requestId: '3', sessionId: '2', timestamp: 100, request: 'different session request' }); + service.recordResponse({ agentId: 'agent', requestId: '3', sessionId: '2', timestamp: 200, response: 'different session request' }); + + const history1 = service.getSessionHistory('1'); + expect(history1.length).to.eq(2); + expect(history1[0].request).to.eq('session 1 request 1'); + expect(history1[0].response).to.eq('session 1 response 1'); + expect(history1[1].request).to.eq('session 1 request 2'); + expect(history1[1].response).to.eq('session 1 response 2'); + + const history2 = service.getSessionHistory('2'); + expect(history2.length).to.eq(1); + expect(history2[0].request).to.eq('different session request'); + expect(history2[0].response).to.eq('different session request'); + }); + }); diff --git a/packages/ai-history/src/common/communication-recording-service.ts b/packages/ai-history/src/common/communication-recording-service.ts index d32eb6ffc9121..e87e62cf0736b 100644 --- a/packages/ai-history/src/common/communication-recording-service.ts +++ b/packages/ai-history/src/common/communication-recording-service.ts @@ -38,6 +38,14 @@ export class DefaultCommunicationRecordingService implements CommunicationRecord return this.history.get(agentId) || []; } + getSessionHistory(sessionId: string): CommunicationHistory { + return Array.from( + this.history.values() + ).reduce((acc, current) => + acc.concat(current.filter(entry => entry.sessionId === sessionId)), [] + ); + } + recordRequest(requestEntry: CommunicationHistoryEntry): void { this.logger.debug('Recording request:', requestEntry.request); if (this.history.has(requestEntry.agentId)) {