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

fix: BotState cache hash skipped properties #4601

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
7 changes: 4 additions & 3 deletions libraries/botbuilder-core/src/botState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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 @@ -221,7 +222,7 @@ export class BotState implements PropertyManager {
};

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

Expand All @@ -230,14 +231,14 @@ export class BotState implements PropertyManager {
}

if (typeof value !== 'object') {
return value;
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 @@ -201,6 +201,7 @@ 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', 'skillClient'];
skipProperties(this._dialogOptionsStateKey, props);
Expand Down
7 changes: 2 additions & 5 deletions libraries/botbuilder-dialogs/src/skillDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,9 @@ export class SkillDialog extends Dialog<Partial<BeginSkillDialogOptions>> {
// after saveChanges, this.dialogOptions is missing some required values.
// this work`around allows this method to work, but 'interceptOAuthCards' (below)
// could encounter problems (not currently used by UHG).
const skillClient = this.dialogOptions.skillClient;
const conversationIdFactory = this.dialogOptions.conversationIdFactory;

await this.dialogOptions.conversationState.saveChanges(context, true);

const response = await skillClient.postActivity<ExpectedReplies>(
const response = await this.dialogOptions.skillClient.postActivity<ExpectedReplies>(
this.dialogOptions.botId,
skillInfo.appId,
skillInfo.skillEndpoint,
Expand Down Expand Up @@ -300,7 +297,7 @@ export class SkillDialog extends Dialog<Partial<BeginSkillDialogOptions>> {
console.log(
`SkillHandlerImpl.sendToSkill, deleteConversationReference skillConversationId=${skillConversationId}`
);
await conversationIdFactory.deleteConversationReference(skillConversationId);
await this.dialogOptions.conversationIdFactory.deleteConversationReference(skillConversationId);
} else if (
!sentInvokeResponses &&
(await this.interceptOAuthCards(context, activityFromSkill, this.dialogOptions.connectionName))
Expand Down
Loading