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
9 changed files
with
194 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
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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
// ***************************************************************************** | ||
// 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'; | ||
|
||
@injectable() | ||
export class AIHistoryFrontendContribution implements FrontendApplicationContribution { | ||
|
||
@inject(CommunicationRecordingService) | ||
protected recordingService: CommunicationRecordingService; | ||
|
||
async onStart(app: FrontendApplication): Promise<void> { | ||
this.recordingService.loadHistory(); | ||
} | ||
} |
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,26 @@ | ||
// ***************************************************************************** | ||
// 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(agentId: string): Promise<CommunicationHistory>; | ||
getRecordedAgents(): Promise<string[]>; | ||
} |
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,38 @@ | ||
// ***************************************************************************** | ||
// 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 { ConnectionHandler, RpcConnectionHandler } from '@theia/core'; | ||
import { ContainerModule } from '@theia/core/shared/inversify'; | ||
import { DefaultCommunicationRecordingService } from '../common'; | ||
import { AiHistoryPersistenceService, aiHistoryPersistenceServicePath } from '../common/history-persistence'; | ||
import { FileCommunicationPersistenceService } from './communication-persistence-service'; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(DefaultCommunicationRecordingService).toSelf().inSingletonScope(); | ||
bind(CommunicationRecordingService).toService(DefaultCommunicationRecordingService); | ||
|
||
bind(FileCommunicationPersistenceService).toSelf().inSingletonScope(); | ||
bind(AiHistoryPersistenceService).to(FileCommunicationPersistenceService); | ||
bind(ConnectionHandler) | ||
.toDynamicValue( | ||
ctx => | ||
new RpcConnectionHandler(aiHistoryPersistenceServicePath, () => | ||
ctx.container.get(AiHistoryPersistenceService) | ||
) | ||
) | ||
.inSingletonScope(); | ||
|
||
}); |
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
// ***************************************************************************** | ||
// 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'; | ||
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; | ||
|
||
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(agentId: string): Promise<CommunicationHistory> { | ||
const historyDir = await this.getHistoryDirectoryPath(); | ||
const filePath = `${historyDir}/${agentId}.json`; | ||
try { | ||
const historyJson = readFileSync(filePath, 'utf-8'); | ||
const communicationHistory = JSON.parse(historyJson); | ||
console.log(`Loaded communication history from ${agentId} from ${filePath}`); | ||
return communicationHistory; | ||
} catch (error) { | ||
console.log(`Could not load communication history for agent ${agentId}. Returning empty history.`); | ||
} | ||
return []; | ||
} | ||
|
||
async getRecordedAgents(): Promise<string[]> { | ||
const historyDir = await this.getHistoryDirectoryPath(); | ||
const files = readdirSync(historyDir); | ||
return files.map((file: string) => file.replace('.json', '')); | ||
} | ||
} |