-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from SAGE-RAI/main
WIP
- Loading branch information
Showing
18 changed files
with
422 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { MongoClient } from 'mongodb'; | ||
import { BaseCache } from '../interfaces/base-cache.js'; | ||
|
||
export class MongoCache implements BaseCache { | ||
private readonly uri: string; | ||
private readonly dbName: string; | ||
private readonly collectionName: string; | ||
private client: MongoClient; | ||
|
||
constructor({ uri, dbName, collectionName }: { uri: string, dbName: string, collectionName: string }) { | ||
this.uri = uri; | ||
this.dbName = dbName; | ||
this.collectionName = collectionName; | ||
} | ||
|
||
async init(): Promise<void> { | ||
this.client = new MongoClient(this.uri); | ||
await this.client.connect(); | ||
|
||
// Create index on loaderId field | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
try { | ||
await collection.createIndex({ loaderId: 1 }, { unique: true }); | ||
} catch (error: any) { | ||
//this.debug('Index on loaderId already exists.'); | ||
} | ||
} | ||
|
||
async addLoader(loaderId: string, chunkCount: number): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.insertOne({ loaderId, chunkCount }); | ||
} | ||
|
||
async getLoader(loaderId: string): Promise<{ chunkCount: number }> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.findOne({ loaderId }); | ||
return { chunkCount: result ? result.chunkCount : 0 }; // Assuming a default value of 0 if result is null | ||
} | ||
|
||
async hasLoader(loaderId: string): Promise<boolean> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.findOne({ loaderId }); | ||
return !!result; | ||
} | ||
|
||
async loaderCustomSet<T extends Record<string, unknown>>(loaderCombinedId: string, value: T): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.updateOne( | ||
{ loaderId: loaderCombinedId }, | ||
{ $setOnInsert: { loaderId: loaderCombinedId, value } }, | ||
{ upsert: false } | ||
); | ||
|
||
if (result.matchedCount === 0) { | ||
await collection.insertOne({ loaderId: loaderCombinedId, value }); | ||
} | ||
} | ||
|
||
async loaderCustomGet<T extends Record<string, unknown>>(loaderCombinedId: string): Promise<T> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.findOne({ loaderId: loaderCombinedId }); | ||
return result?.value; | ||
} | ||
|
||
async loaderCustomHas(loaderCombinedId: string): Promise<boolean> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.findOne({ loaderId: loaderCombinedId }); | ||
return !!result; | ||
} | ||
|
||
async clear(): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.deleteMany({}); | ||
} | ||
|
||
async deleteLoader(loaderId: string): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.deleteOne({ loaderId }); | ||
} | ||
|
||
async loaderCustomDelete(loaderCombinedId: string): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.deleteOne({ loaderId: loaderCombinedId }); | ||
} | ||
|
||
async close(): Promise<void> { | ||
await this.client.close(); | ||
} | ||
} |
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,44 @@ | ||
// InMemoryConversations.ts | ||
import { Conversation, ConversationEntry } from '../global/types.js'; | ||
import { BaseConversations } from '../interfaces/base-conversations.js'; | ||
|
||
class InMemoryConversations implements BaseConversations { | ||
private conversations: Map<string, Conversation> = new Map(); | ||
|
||
async init(): Promise<void> { | ||
this.conversations.clear(); | ||
} | ||
|
||
async addConversation(conversationId: string): Promise<void> { | ||
if (!this.conversations.has(conversationId)) { | ||
this.conversations.set(conversationId, { conversationId, entries: [] }); | ||
} | ||
} | ||
|
||
async getConversation(conversationId: string): Promise<Conversation> { | ||
if (!this.conversations.has(conversationId)) { | ||
// Automatically create a new conversation if it does not exist | ||
this.conversations.set(conversationId, { conversationId, entries: [] }); | ||
} | ||
return this.conversations.get(conversationId)!; | ||
} | ||
|
||
async hasConversation(conversationId: string): Promise<boolean> { | ||
return this.conversations.has(conversationId); | ||
} | ||
|
||
async deleteConversation(conversationId: string): Promise<void> { | ||
this.conversations.delete(conversationId); | ||
} | ||
|
||
async addEntryToConversation(conversationId: string, entry: ConversationEntry): Promise<void> { | ||
const conversation = await this.getConversation(conversationId); | ||
conversation.entries.push(entry); | ||
} | ||
|
||
async clearConversations(): Promise<void> { | ||
this.conversations.clear(); | ||
} | ||
} | ||
|
||
export { InMemoryConversations }; |
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,81 @@ | ||
import { MongoClient } from 'mongodb'; | ||
import { Conversation, ConversationEntry } from '../global/types.js'; | ||
import { BaseConversations } from '../interfaces/base-conversations.js'; | ||
|
||
interface ConversationDocument { | ||
_id?: string; // optional MongoDB ID field | ||
conversationId: string; | ||
entries: ConversationEntry[]; // Explicitly stating this is an array of ConversationHistory | ||
} | ||
|
||
export class MongoConversations implements BaseConversations { | ||
private readonly uri: string; | ||
private readonly dbName: string; | ||
private readonly collectionName: string; | ||
private client: MongoClient; | ||
|
||
constructor({ uri, dbName, collectionName }: { uri: string, dbName: string, collectionName: string }) { | ||
this.uri = uri; | ||
this.dbName = dbName; | ||
this.collectionName = collectionName; | ||
} | ||
|
||
async init(): Promise<void> { | ||
this.client = new MongoClient(this.uri); | ||
await this.client.connect(); | ||
const collection = this.client.db(this.dbName).collection<ConversationDocument>(this.collectionName); | ||
await collection.createIndex({ conversationId: 1 }); | ||
await collection.createIndex({ "entries._id": 1 }); | ||
} | ||
|
||
async addConversation(conversationId: string): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
// Check if conversation already exists to prevent duplication | ||
const exists = await this.hasConversation(conversationId); | ||
if (!exists) { | ||
await collection.insertOne({ conversationId, entries: [] }); | ||
} | ||
} | ||
|
||
async getConversation(conversationId: string): Promise<Conversation> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const document = await collection.findOne({ conversationId }); | ||
if (!document) { | ||
// If not found, create a new one automatically | ||
await this.addConversation(conversationId); | ||
return { conversationId, entries: [] }; | ||
} | ||
return { | ||
conversationId: document.conversationId, | ||
entries: document.entries as ConversationEntry[] | ||
}; | ||
} | ||
|
||
async hasConversation(conversationId: string): Promise<boolean> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
const result = await collection.findOne({ conversationId }); | ||
return !!result; | ||
} | ||
|
||
async deleteConversation(conversationId: string): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.deleteOne({ conversationId }); | ||
} | ||
|
||
async addEntryToConversation(conversationId: string, entry: ConversationEntry): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection<ConversationDocument>(this.collectionName); | ||
await collection.updateOne( | ||
{ conversationId }, | ||
{ $push: { entries: entry } } // Correctly structured $push operation | ||
); | ||
} | ||
|
||
async clearConversations(): Promise<void> { | ||
const collection = this.client.db(this.dbName).collection(this.collectionName); | ||
await collection.deleteMany({}); | ||
} | ||
|
||
async close(): Promise<void> { | ||
await this.client.close(); | ||
} | ||
} |
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
Oops, something went wrong.