microsoft / TaskWeaver

A code-first agent framework for seamlessly planning and executing data analytics tasks.
https://microsoft.github.io/TaskWeaver/
MIT License
5.37k stars 690 forks source link

Liqun/fix update session var #312

Closed liqul closed 7 months ago

liqul commented 7 months ago

Fix the broken feature of update_session_var for sessions.

shivakharbanda commented 7 months ago

REFERENCE ISSUE #311 i took pull from this branch as i wanted to test the issue and there is a small error in the code in the line 434 i had to remove this check session_dir is not None

it was causing an issue because session_dir was none and it was raising error

web-1    | ERROR:daphne.server:Exception inside application: 'NoneType' object has no attribute 'session_var'
web-1    | Traceback (most recent call last):
web-1    |   File "/usr/local/lib/python3.11/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
web-1    |     return await self.application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
web-1    |     return await application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/security/websocket.py", line 37, in __call__
web-1    |     return await self.application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/sessions.py", line 47, in __call__
web-1    |     return await self.inner(dict(scope, cookies=cookies), receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/sessions.py", line 263, in __call__
web-1    |     return await self.inner(wrapper.scope, receive, wrapper.send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/auth.py", line 185, in __call__
web-1    |     return await super().__call__(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/middleware.py", line 24, in __call__
web-1    |     return await self.inner(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/routing.py", line 132, in __call__
web-1    |     return await application(
web-1    |            ^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 94, in app
web-1    |     return await consumer(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 58, in __call__
web-1    |     await await_many_dispatch(
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/utils.py", line 50, in await_many_dispatch
web-1    |     await dispatch(result)
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 73, in dispatch
web-1    |     await handler(message)
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/generic/websocket.py", line 179, in websocket_connect
web-1    |     await self.connect()
web-1    |   File "/app/metadata/consumers.py", line 40, in connect
web-1    |     user_sessions[self.session_id].ai_client.update_session_var(variables = {"datasource_id": self.datasource_id})
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/module/tracing.py", line 186, in wrapper
web-1    |     return func(self, *args, **kwargs)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/session/session.py", line 148, in update_session_var
web-1    |     code_interpreter_instance.update_session_variables(variables)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/code_interpreter/code_interpreter/code_interpreter.py", line 125, in update_session_variables
web-1    |     self.executor.exec_client.update_session_var(session_variables)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/ces/manager/sub_proc.py", line 49, in update_session_var
web-1    |     self.mgr.env.update_session_var(self.session_id, session_var_dict)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/ces/environment.py", line 385, in update_session_var
web-1    |     session.session_var.update(session_var)
web-1    |     ^^^^^^^^^^^^^^^^^^^
web-1    | AttributeError: 'NoneType' object has no attribute 'session_var'
web-1    | WebSocket DISCONNECT /ws/chat/550e8400-e29b-41d4-a716-446655440000/34/ [192.168.16.1:35458]
def _get_session(
        self,
        session_id: str,
        session_dir: Optional[str] = None,
    ) -> EnvSession:
        if session_id not in self.session_dict:
            new_session = EnvSession(session_id)
            new_session.session_dir = (
                session_dir if session_dir is not None else self._get_default_session_dir(session_id)
            )
            os.makedirs(new_session.session_dir, exist_ok=True)
            self.session_dict[session_id] = new_session

        return self.session_dict.get(session_id, None)

after removing the None check the code works fine and i am now able to get set variables from session dict inside the plugin.

@liqul please check

liqul commented 7 months ago

REFERENCE ISSUE #311 i took pull from this branch as i wanted to test the issue and there is a small error in the code in the line 434 i had to remove this check session_dir is not None

it was causing an issue because session_dir was none and it was raising error

web-1    | ERROR:daphne.server:Exception inside application: 'NoneType' object has no attribute 'session_var'
web-1    | Traceback (most recent call last):
web-1    |   File "/usr/local/lib/python3.11/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
web-1    |     return await self.application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
web-1    |     return await application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/security/websocket.py", line 37, in __call__
web-1    |     return await self.application(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/sessions.py", line 47, in __call__
web-1    |     return await self.inner(dict(scope, cookies=cookies), receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/sessions.py", line 263, in __call__
web-1    |     return await self.inner(wrapper.scope, receive, wrapper.send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/auth.py", line 185, in __call__
web-1    |     return await super().__call__(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/middleware.py", line 24, in __call__
web-1    |     return await self.inner(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/routing.py", line 132, in __call__
web-1    |     return await application(
web-1    |            ^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 94, in app
web-1    |     return await consumer(scope, receive, send)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 58, in __call__
web-1    |     await await_many_dispatch(
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/utils.py", line 50, in await_many_dispatch
web-1    |     await dispatch(result)
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/consumer.py", line 73, in dispatch
web-1    |     await handler(message)
web-1    |   File "/usr/local/lib/python3.11/site-packages/channels/generic/websocket.py", line 179, in websocket_connect
web-1    |     await self.connect()
web-1    |   File "/app/metadata/consumers.py", line 40, in connect
web-1    |     user_sessions[self.session_id].ai_client.update_session_var(variables = {"datasource_id": self.datasource_id})
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/module/tracing.py", line 186, in wrapper
web-1    |     return func(self, *args, **kwargs)
web-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/session/session.py", line 148, in update_session_var
web-1    |     code_interpreter_instance.update_session_variables(variables)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/code_interpreter/code_interpreter/code_interpreter.py", line 125, in update_session_variables
web-1    |     self.executor.exec_client.update_session_var(session_variables)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/ces/manager/sub_proc.py", line 49, in update_session_var
web-1    |     self.mgr.env.update_session_var(self.session_id, session_var_dict)
web-1    |   File "/app/pkgs/TaskWeaver/taskweaver/ces/environment.py", line 385, in update_session_var
web-1    |     session.session_var.update(session_var)
web-1    |     ^^^^^^^^^^^^^^^^^^^
web-1    | AttributeError: 'NoneType' object has no attribute 'session_var'
web-1    | WebSocket DISCONNECT /ws/chat/550e8400-e29b-41d4-a716-446655440000/34/ [192.168.16.1:35458]
def _get_session(
        self,
        session_id: str,
        session_dir: Optional[str] = None,
    ) -> EnvSession:
        if session_id not in self.session_dict:
            new_session = EnvSession(session_id)
            new_session.session_dir = (
                session_dir if session_dir is not None else self._get_default_session_dir(session_id)
            )
            os.makedirs(new_session.session_dir, exist_ok=True)
            self.session_dict[session_id] = new_session

        return self.session_dict.get(session_id, None)

after removing the None check the code works fine and i am now able to get set variables from session dict inside the plugin.

@liqul please check

This issue cannot be fixed in this way. It would introduce other issues.

liqul commented 7 months ago

@shivakharbanda could you check this branch again?

shivakharbanda commented 7 months ago

@shivakharbanda could you check this branch again?

Sure. I’ll test again and let you know soon.

shivakharbanda commented 7 months ago

@shivakharbanda could you check this branch again?

it is working with your recent changes. thank you for fixing the problem i was facing