Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional userinfo ttl #671

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lambda/fulfillment/lib/middleware/2_preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions templates/master/default-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 4 additions & 0 deletions templates/master/dynamodb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ module.exports = {
KeyType: 'HASH',
},
],
TimeToLiveSpecification: {
AttributeName: "ttl",
Enabled: true
},
},
Metadata: util.cfnNag(['W74']),
},
Expand Down