|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from .bot_context import BotContext |
| 5 | +from .middleware_set import Middleware |
| 6 | +from .storage import calculate_change_hash, StoreItem, StorageKeyFactory, Storage |
| 7 | + |
| 8 | + |
| 9 | +class CachedBotState(StoreItem): |
| 10 | + def __init__(self): |
| 11 | + super(CachedBotState, self).__init__() |
| 12 | + self.state = None |
| 13 | + self.hash: str = None |
| 14 | + |
| 15 | + |
| 16 | +class BotState(Middleware): |
| 17 | + def __init__(self, storage: Storage, storage_key: StorageKeyFactory): |
| 18 | + self.state_key = 'state' |
| 19 | + self.storage = storage |
| 20 | + self.storage_key = storage_key |
| 21 | + pass |
| 22 | + |
| 23 | + async def on_process_request(self, context, next_middleware): |
| 24 | + """ |
| 25 | + Reads and writes state for your bot to storage. |
| 26 | + :param context: |
| 27 | + :param next_middleware: |
| 28 | + :return: |
| 29 | + """ |
| 30 | + await self.read(context, True) |
| 31 | + # For libraries like aiohttp, the web.Response need to be bubbled up from the process_activity logic, which is |
| 32 | + # why we store the value from next_middleware() |
| 33 | + logic_results = await next_middleware() |
| 34 | + |
| 35 | + # print('Printing after log ran') |
| 36 | + # print(context.services['state']) # This is appearing as a dict which doesn't seem right |
| 37 | + # print(context.services['state']['state']) |
| 38 | + |
| 39 | + await self.write(context, True) # Both of these probably shouldn't be True |
| 40 | + return logic_results |
| 41 | + |
| 42 | + async def read(self, context: BotContext, force: bool=False): |
| 43 | + """ |
| 44 | + Reads in and caches the current state object for a turn. |
| 45 | + :param context: |
| 46 | + :param force: |
| 47 | + :return: |
| 48 | + """ |
| 49 | + cached = context.services.get(self.state_key) |
| 50 | + |
| 51 | + if force or cached is None or ('state' in cached and cached['state'] is None): |
| 52 | + key = self.storage_key(context) |
| 53 | + items = await self.storage.read([key]) |
| 54 | + |
| 55 | + # state = items.get(key, {}) # This is a problem, we need to decide how the data is going to be handled: |
| 56 | + # Is it going to be serialized the entire way down or only partially serialized? |
| 57 | + # The current code is a bad implementation... |
| 58 | + |
| 59 | + state = items.get(key, StoreItem()) |
| 60 | + hash_state = calculate_change_hash(state) |
| 61 | + # context.services.set(self.state_key, {'state': state, 'hash': hash_state}) # <-- dict.set() doesn't exist |
| 62 | + context.
6854
services[self.state_key] = {'state': state, 'hash': hash_state} |
| 63 | + return state |
| 64 | + |
| 65 | + return cached['state'] |
| 66 | + |
| 67 | + async def write(self, context: BotContext, force: bool=False): |
| 68 | + """ |
| 69 | + Saves the cached state object if it's been changed. |
| 70 | + :param context: |
| 71 | + :param force: |
| 72 | + :return: |
| 73 | + """ |
| 74 | + cached = context.services.get(self.state_key) |
| 75 | + |
| 76 | + if force or (cached is not None and cached.get('hash', None) != calculate_change_hash(cached['state'])): |
| 77 | + key = self.storage_key(context) |
| 78 | + |
| 79 | + if cached is None: |
| 80 | + cached = {'state': StoreItem(e_tag='*'), 'hash': ''} |
| 81 | + changes = {key: cached['state']} # this doesn't seem right |
| 82 | + # changes.key = cached['state'] # Wtf is this going to be used for? |
| 83 | + await self.storage.write(changes) |
| 84 | + |
| 85 | + cached['hash'] = calculate_change_hash(cached['state']) |
| 86 | + context.services[self.state_key] = cached # instead of `cached` shouldn't this be |
| 87 | + # context.services[self.state_key][key] = cached |
| 88 | + |
| 89 | + async def clear(self, context: BotContext): |
| 90 | + """ |
| 91 | + Clears the current state object for a turn. |
| 92 | + :param context: |
| 93 | + :return: |
| 94 | + """ |
| 95 | + cached = context.services.get(self.state_key) |
| 96 | + if cached is not None: |
| 97 | + cached['state'] = {} |
| 98 | + context.services[self.state_key] = cached |
| 99 | + |
| 100 | + async def get(self, context: BotContext): |
| 101 | + """ |
| 102 | + Returns a cached state object or undefined if not cached. |
| 103 | + :param context: |
| 104 | + :return: |
| 105 | + """ |
| 106 | + cached = context.services.get(self.state_key) |
| 107 | + state = None |
| 108 | + if isinstance(cached, dict) and isinstance(cached['state'], StoreItem): |
| 109 | + state = cached['state'] |
| 110 | + return state |
0 commit comments