Skip to content

Commit

Permalink
Fix bot's state skipped properties in cache hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-joelmut committed Jan 3, 2024
1 parent 709a823 commit b340adc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions libraries/botbuilder-core/src/botState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export class BotState implements PropertyManager {
/**
* Skips properties from the cached state object.
*
* @remarks Primarily used to skip properties before calculating the hash value in the calculateChangeHash function.
* @param state Dictionary of state values.
* @returns Dictionary of state values, without the skipped properties.
*/
Expand All @@ -217,23 +218,23 @@ export class BotState implements PropertyManager {
};

const inner = ([key, value], skip = []) => {
if (skip.includes(key)) {
if (value === null || value === undefined || skip.includes(key)) {
return;
}

if (Array.isArray(value)) {
return value.map((e) => inner([null, e], skip));
}

if (value === null || typeof value !== 'object') {
return value;
if (typeof value !== 'object') {
return value.valueOf();
}

return Object.entries(value).reduce((acc, [k, v]) => {
const skipResult = skipHandler(k) ?? [];
acc[k] = inner([k, v], [...skip, ...skipResult]);
return acc;
}, value);
}, {});
};

return inner([null, state]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ describe('ActionTests', function () {
const [, { state: beginSkillState }] = actionScope.dialogStack;
const options = beginSkillState['BeginSkill.dialogOptionsData'];

assert.equal(options.conversationIdFactory, null);
assert.equal(options.conversationState, null);
assert.notEqual(options.conversationIdFactory, null);
assert.notEqual(options.conversationState, null);
assert.notEqual(beginSkillDialog.dialogOptions.conversationIdFactory, null);
assert.notEqual(beginSkillDialog.dialogOptions.conversationState, null);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ export class BeginSkill extends SkillDialog implements BeginSkillConfiguration {

// Store the initialized dialogOptions in state so we can restore these values when the dialog is resumed.
dc.activeDialog.state[this._dialogOptionsStateKey] = this.dialogOptions;
// Skip properties from the bot's state cache hash due to unwanted conversationState behavior.
const skipProperties = dc.context.turnState.get(CACHED_BOT_STATE_SKIP_PROPERTIES_HANDLER_KEY);
const props: (keyof SkillDialogOptions)[] = ['conversationIdFactory', 'conversationState'];
const props: (keyof SkillDialogOptions)[] = ['conversationIdFactory', 'conversationState', 'skillClient'];
skipProperties(this._dialogOptionsStateKey, props);

// Get the activity to send to the skill.
Expand Down

0 comments on commit b340adc

Please sign in to comment.