|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2024 EclipseSource GmbH. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | +import { CommunicationHistory } from '@theia/ai-core'; |
| 17 | +import { URI } from '@theia/core'; |
| 18 | +import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; |
| 19 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 20 | +import { readdirSync, readFileSync, writeFileSync } from 'fs'; |
| 21 | +import { AiHistoryPersistenceService } from '../common/history-persistence'; |
| 22 | + |
| 23 | +@injectable() |
| 24 | +export class FileCommunicationPersistenceService implements AiHistoryPersistenceService { |
| 25 | + |
| 26 | + @inject(EnvVariablesServer) |
| 27 | + protected envServer: EnvVariablesServer; |
| 28 | + |
| 29 | + async saveHistory(agentId: string, history: CommunicationHistory): Promise<void> { |
| 30 | + const historyDir = await this.getHistoryDirectoryPath(); |
| 31 | + const fileName = `${historyDir}/${agentId}.json`; |
| 32 | + writeFileSync(fileName, JSON.stringify(history, undefined, 2)); |
| 33 | + console.log(`Saving communication history for agent ${agentId} to ${fileName}`); |
| 34 | + } |
| 35 | + |
| 36 | + private async getHistoryDirectoryPath(): Promise<string> { |
| 37 | + const configDir = new URI(await this.envServer.getConfigDirUri()); |
| 38 | + const historyDir = `${configDir.path.fsPath()}/agent-communication`; |
| 39 | + return historyDir; |
| 40 | + } |
| 41 | + |
| 42 | + async loadHistory(agentId: string): Promise<CommunicationHistory> { |
| 43 | + const historyDir = await this.getHistoryDirectoryPath(); |
| 44 | + const filePath = `${historyDir}/${agentId}.json`; |
| 45 | + try { |
| 46 | + const historyJson = readFileSync(filePath, 'utf-8'); |
| 47 | + const communicationHistory = JSON.parse(historyJson); |
| 48 | + console.log(`Loaded communication history from ${agentId} from ${filePath}`); |
| 49 | + return communicationHistory; |
| 50 | + } catch (error) { |
| 51 | + console.log(`Could not load communication history for agent ${agentId}. Returning empty history.`); |
| 52 | + } |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + async getRecordedAgents(): Promise<string[]> { |
| 57 | + const historyDir = await this.getHistoryDirectoryPath(); |
| 58 | + const files = readdirSync(historyDir); |
| 59 | + return files.map((file: string) => file.replace('.json', '')); |
| 60 | + } |
| 61 | +} |
0 commit comments