diff --git a/lambda/fulfillment/lib/middleware/2_preprocess.js b/lambda/fulfillment/lib/middleware/2_preprocess.js index d4b53b4c..86cfb771 100644 --- a/lambda/fulfillment/lib/middleware/2_preprocess.js +++ b/lambda/fulfillment/lib/middleware/2_preprocess.js @@ -73,12 +73,18 @@ async function get_userInfo(userId, idattrs, userPrefs = undefined) { return req_userInfo; } -async function update_userInfo(userId, req_userInfo) { +async function update_userInfo(userId, req_userInfo, ttlDays) { const res_userInfo = _.cloneDeep(req_userInfo); const dt = new Date(); res_userInfo.FirstSeen = req_userInfo.FirstSeen || dt.toString(); res_userInfo.LastSeen = dt.toString(); res_userInfo.InteractionCount = req_userInfo.InteractionCount + 1; + // set userInfo record TTL + if (ttlDays > 0) { + const secondsPerDay = 24 * 60 * 60; + const ttlDate = new Date(dt.getTime() + ttlDays * secondsPerDay * 1000); + res_userInfo.ttl = Math.floor(ttlDate.getTime() / 1000); + } return res_userInfo; } @@ -211,11 +217,11 @@ module.exports = async function preprocess(req, res) { _.set(req, '_userInfo', req_userInfo); // set the userPrefs session attribute to the value returned from DDB _.set(req, 'session.userPrefs', _.get(req_userInfo, 'userPrefs', {})); - // Add _userInfo to res, with updated timestamps // May be further modified by lambda hooks // Will be saved back to DynamoDB in userInfo.js - const res_userInfo = await update_userInfo(userId, req_userInfo); + const ttlDays = _.get(req, '_settings.USERINFO_TTL_DAYS', 0) + const res_userInfo = await update_userInfo(userId, req_userInfo, ttlDays); _.set(res, '_userInfo', res_userInfo); if (_.get(req, '_settings.REMOVE_ID_TOKENS_FROM_SESSION', false)) { diff --git a/templates/master/default-settings.js b/templates/master/default-settings.js index b1947f92..5d76ce00 100644 --- a/templates/master/default-settings.js +++ b/templates/master/default-settings.js @@ -109,6 +109,7 @@ const default_settings = { LLM_QA_NO_HITS_REGEX: 'Sorry, //remove comment to enable custom no match (no_hits) when LLM does not know the answer.', LLM_PROMPT_MAX_TOKEN_LIMIT: '${LLM_PROMPT_MAX_TOKEN_LIMIT}', + USERINFO_TTL_DAYS: 0, // Set to 0 to disable setting a ttl property in DyanmoDB }; const defaultGenerateQueryPromptTemplate = diff --git a/templates/master/dynamodb/index.js b/templates/master/dynamodb/index.js index 6067fdee..9dd2be7a 100644 --- a/templates/master/dynamodb/index.js +++ b/templates/master/dynamodb/index.js @@ -33,6 +33,10 @@ module.exports = { KeyType: 'HASH', }, ], + TimeToLiveSpecification: { + AttributeName: "ttl", + Enabled: true + }, }, Metadata: util.cfnNag(['W74']), },