Skip to content

Commit

Permalink
add session context for a user mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
crisjy committed Dec 27, 2024
1 parent 06f4de8 commit 511f3e1
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions libs/langchain-azure-cosmosdb/src/chat_histories/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface AzureCosmosDBMongoChatHistoryDBConfig {
readonly collectionName?: string;
}

export type ChatSession = {
id: string;
context: Record<string, unknown>;
};

const ID_KEY = "sessionId";

export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHistory {
Expand All @@ -33,6 +38,8 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis

private initPromise?: Promise<void>;

private context: Record<string, unknown> = {};

private readonly client: MongoClient | undefined;

private database: Db;
Expand Down Expand Up @@ -134,10 +141,12 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis
await this.initialize();

const messages = mapChatMessagesToStoredMessages([message]);
const context = await this.getContext();
await this.collection.updateOne(
{ [ID_KEY]: this.sessionId },
{
$push: { messages: { $each: messages } } as PushOperator<Document>,
$set: {context},
},
{ upsert: true }
);
Expand All @@ -152,4 +161,51 @@ export class AzureCosmosDBMongoChatMessageHistory extends BaseListChatMessageHis

await this.collection.deleteOne({ [ID_KEY]: this.sessionId });
}

async getAllSessions(): Promise<ChatSession[]> {
await this.initialize();
const documents = await this.collection.find().toArray();

const chatSessions: ChatSession[] = documents.map(doc => ({
id: doc[ID_KEY],
context: doc.context || {},
}));

return chatSessions;
}

async clearAllSessions() {
await this.initialize();
try {
await this.collection.deleteMany({});
} catch (error) {
console.log('Error clearing sessions:', error)
}
}

async getContext(): Promise<Record<string, unknown>> {
await this.initialize();

const document = await this.collection.findOne({
[ID_KEY]: this.sessionId,
});
this.context = document?.context || this.context;
return this.context;
}

async setContext(context: Record<string, unknown>): Promise<void> {
await this.initialize()

try {
await this.collection.updateOne(
{ [ID_KEY]: this.sessionId },
{
$set: { context },
},
{ upsert: true }
);
} catch (error) {
console.log('Error setting context', error)
}
}
}

0 comments on commit 511f3e1

Please sign in to comment.