UNPKG

@langchain/community

Version:
161 lines (160 loc) 5.78 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_runtime = require("../../_virtual/_rolldown/runtime.cjs"); let _langchain_core_messages = require("@langchain/core/messages"); let _langchain_core_chat_history = require("@langchain/core/chat_history"); let _aws_sdk_client_dynamodb = require("@aws-sdk/client-dynamodb"); //#region src/stores/message/dynamodb.ts var dynamodb_exports = /* @__PURE__ */ require_runtime.__exportAll({ DynamoDBChatMessageHistory: () => DynamoDBChatMessageHistory }); /** * Class providing methods to interact with a DynamoDB table to store and * retrieve chat messages. It extends the `BaseListChatMessageHistory` * class. */ var DynamoDBChatMessageHistory = class extends _langchain_core_chat_history.BaseListChatMessageHistory { lc_namespace = [ "langchain", "stores", "message", "dynamodb" ]; get lc_secrets() { return { "config.credentials.accessKeyId": "AWS_ACCESS_KEY_ID", "config.credentials.secretAccessKey": "AWS_SECRETE_ACCESS_KEY", "config.credentials.sessionToken": "AWS_SESSION_TOKEN" }; } tableName; sessionId; client; partitionKey = "id"; sortKey; messageAttributeName = "messages"; dynamoKey = {}; /** * Transforms a `StoredMessage` into a `DynamoDBSerializedChatMessage`. * The `DynamoDBSerializedChatMessage` format is suitable for storing in DynamoDB. * * @param message - The `StoredMessage` to be transformed. * @returns The transformed `DynamoDBSerializedChatMessage`. */ createDynamoDBSerializedChatMessage(message) { const { type, data: { content, role, additional_kwargs } } = message; const isAdditionalKwargs = additional_kwargs && Object.keys(additional_kwargs).length; const dynamoSerializedMessage = { M: { type: { S: type }, text: { S: content }, additional_kwargs: isAdditionalKwargs ? { S: JSON.stringify(additional_kwargs) } : { S: "{}" } } }; if (role) dynamoSerializedMessage.M.role = { S: role }; return dynamoSerializedMessage; } constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key = {} }) { super(); this.tableName = tableName; this.sessionId = sessionId; this.client = new _aws_sdk_client_dynamodb.DynamoDBClient(config ?? {}); this.partitionKey = partitionKey ?? this.partitionKey; this.sortKey = sortKey; this.messageAttributeName = messageAttributeName ?? this.messageAttributeName; this.dynamoKey = key; if (Object.keys(this.dynamoKey).length === 0) { this.dynamoKey[this.partitionKey] = { S: this.sessionId }; if (this.sortKey) this.dynamoKey[this.sortKey] = { S: this.sortKey }; } } /** * Retrieves all messages from the DynamoDB table and returns them as an * array of `BaseMessage` instances. * @returns Array of stored messages */ async getMessages() { try { const params = { TableName: this.tableName, Key: this.dynamoKey }; const response = await this.client.send(new _aws_sdk_client_dynamodb.GetItemCommand(params)); return (0, _langchain_core_messages.mapStoredMessagesToChatMessages)((response.Item ? response.Item[this.messageAttributeName]?.L ?? [] : []).filter((item) => item.M !== void 0).map((item) => { const data = { role: item.M?.role?.S, content: item.M?.text.S, additional_kwargs: item.M?.additional_kwargs?.S ? JSON.parse(item.M?.additional_kwargs.S) : void 0 }; return { type: item.M?.type.S, data }; }).filter((x) => x.type !== void 0 && x.data.content !== void 0)); } catch (error) { throw new Error(`Error getting messages: ${error.message}`); } } /** * Adds a new message to the DynamoDB table. * @param message The message to be added to the DynamoDB table. */ async addMessage(message) { try { const messages = (0, _langchain_core_messages.mapChatMessagesToStoredMessages)([message]); const params = { TableName: this.tableName, Key: this.dynamoKey, ExpressionAttributeNames: { "#m": this.messageAttributeName }, ExpressionAttributeValues: { ":empty_list": { L: [] }, ":m": { L: messages.map(this.createDynamoDBSerializedChatMessage) } }, UpdateExpression: "SET #m = list_append(if_not_exists(#m, :empty_list), :m)" }; await this.client.send(new _aws_sdk_client_dynamodb.UpdateItemCommand(params)); } catch (error) { throw new Error(`Error adding message: ${error.message}`); } } /** * Adds new messages to the DynamoDB table. * @param messages The messages to be added to the DynamoDB table. */ async addMessages(messages) { try { const dynamoMessages = (0, _langchain_core_messages.mapChatMessagesToStoredMessages)(messages).map(this.createDynamoDBSerializedChatMessage); const params = { TableName: this.tableName, Key: this.dynamoKey, ExpressionAttributeNames: { "#m": this.messageAttributeName }, ExpressionAttributeValues: { ":empty_list": { L: [] }, ":m": { L: dynamoMessages } }, UpdateExpression: "SET #m = list_append(if_not_exists(#m, :empty_list), :m)" }; await this.client.send(new _aws_sdk_client_dynamodb.UpdateItemCommand(params)); } catch (error) { throw new Error(`Error adding messages: ${error.message}`); } } /** * Deletes all messages from the DynamoDB table. */ async clear() { try { const params = { TableName: this.tableName, Key: this.dynamoKey }; await this.client.send(new _aws_sdk_client_dynamodb.DeleteItemCommand(params)); } catch (error) { throw new Error(`Error clearing messages: ${error.message}`); } } }; //#endregion exports.DynamoDBChatMessageHistory = DynamoDBChatMessageHistory; Object.defineProperty(exports, "dynamodb_exports", { enumerable: true, get: function() { return dynamodb_exports; } }); //# sourceMappingURL=dynamodb.cjs.map