forked from eclipse-theia/theia
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-56 WIP Ai History Persistance Service
Co-authored-by: Olaf Lessenich <[email protected]>
- Loading branch information
Showing
8 changed files
with
169 additions
and
3 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
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
41 changes: 41 additions & 0 deletions
41
packages/ai-history/src/browser/ai-history-frontend-contribution.ts
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,41 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 EclipseSource GmbH. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { CommunicationRecordingService } from '@theia/ai-core'; | ||
import { FrontendApplication, FrontendApplicationContribution } from '@theia/core/lib/browser'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { AiHistoryPersistenceService } from '../common/history-persistence'; | ||
|
||
@injectable() | ||
export class AIHistoryFrontendContribution implements FrontendApplicationContribution { | ||
|
||
@inject(AiHistoryPersistenceService) | ||
protected persistenceService: AiHistoryPersistenceService; | ||
@inject(CommunicationRecordingService) | ||
protected recordingService: CommunicationRecordingService; | ||
|
||
async onStart(app: FrontendApplication): Promise<void> { | ||
this.persistenceService.loadHistory(); | ||
} | ||
|
||
onStop(app: FrontendApplication): void { | ||
this.recordingService.getRecordedAgents().forEach(agentId => { | ||
const history = this.recordingService.getHistory(agentId); | ||
this.persistenceService.saveHistory(agentId, history); | ||
}); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 EclipseSource GmbH. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { CommunicationHistory } from '@theia/ai-core'; | ||
|
||
export const aiHistoryPersistenceServicePath = '/services/aiHistoryPersistenceService'; | ||
|
||
export const AiHistoryPersistenceService = Symbol('AiHistoryPersistenceService'); | ||
export interface AiHistoryPersistenceService { | ||
saveHistory(agentId: string, history: CommunicationHistory): Promise<void>; | ||
loadHistory(): Promise<void>; | ||
} |
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
60 changes: 60 additions & 0 deletions
60
packages/ai-history/src/node/communication-persistence-service.ts
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,60 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 EclipseSource GmbH. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
import { CommunicationHistory, CommunicationRecordingService } from '@theia/ai-core'; | ||
import { URI } from '@theia/core'; | ||
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { readdirSync, readFileSync, writeFileSync } from 'fs'; | ||
import { AiHistoryPersistenceService } from '../common/history-persistence'; | ||
|
||
@injectable() | ||
export class FileCommunicationPersistenceService implements AiHistoryPersistenceService { | ||
|
||
@inject(EnvVariablesServer) | ||
protected envServer: EnvVariablesServer; | ||
@inject(CommunicationRecordingService) | ||
protected recordingService: CommunicationRecordingService; | ||
|
||
async saveHistory(agentId: string, history: CommunicationHistory): Promise<void> { | ||
const historyDir = await this.getHistoryDirectoryPath(); | ||
const fileName = `${historyDir}/${agentId}.json`; | ||
writeFileSync(fileName, JSON.stringify(history, undefined, 2)); | ||
console.log(`Saving communication history for agent ${agentId} to ${fileName}`); | ||
} | ||
|
||
private async getHistoryDirectoryPath(): Promise<string> { | ||
const configDir = new URI(await this.envServer.getConfigDirUri()); | ||
const historyDir = `${configDir.path.fsPath()}/agent-communication`; | ||
return historyDir; | ||
} | ||
|
||
async loadHistory(): Promise<void> { | ||
const historyDir = await this.getHistoryDirectoryPath(); | ||
const fileNames = readdirSync(historyDir); | ||
for (const fileName of fileNames) { | ||
const agentId = fileName.replace('.json', ''); | ||
const filePath = `${historyDir}/${fileName}`; | ||
try { | ||
const historyJson = readFileSync(filePath, 'utf-8'); | ||
const communicationHistory = JSON.parse(historyJson); | ||
console.log(`Loaded communication history from ${agentId} from ${filePath}`); | ||
this.recordingService.setHistory(agentId, communicationHistory); | ||
} catch (error) { | ||
console.log(`Could not load communication history for agent ${agentId}. Returning empty history.`); | ||
} | ||
} | ||
} | ||
} |