mito-ai 0.1.45__py3-none-any.whl → 0.1.46__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mito_ai/__init__.py +10 -1
- mito_ai/_version.py +1 -1
- mito_ai/anthropic_client.py +90 -5
- mito_ai/chat_history/handlers.py +63 -0
- mito_ai/chat_history/urls.py +32 -0
- mito_ai/completions/handlers.py +18 -20
- mito_ai/constants.py +3 -0
- mito_ai/streamlit_conversion/agent_utils.py +148 -30
- mito_ai/streamlit_conversion/prompts/prompt_constants.py +147 -24
- mito_ai/streamlit_conversion/prompts/streamlit_app_creation_prompt.py +2 -1
- mito_ai/streamlit_conversion/prompts/streamlit_error_correction_prompt.py +2 -2
- mito_ai/streamlit_conversion/prompts/streamlit_finish_todo_prompt.py +4 -3
- mito_ai/streamlit_conversion/prompts/update_existing_app_prompt.py +50 -0
- mito_ai/streamlit_conversion/streamlit_agent_handler.py +101 -107
- mito_ai/streamlit_conversion/streamlit_system_prompt.py +1 -0
- mito_ai/streamlit_conversion/streamlit_utils.py +13 -10
- mito_ai/streamlit_conversion/validate_streamlit_app.py +77 -82
- mito_ai/streamlit_preview/handlers.py +3 -4
- mito_ai/streamlit_preview/utils.py +11 -7
- mito_ai/tests/chat_history/test_chat_history.py +211 -0
- mito_ai/tests/message_history/test_message_history_utils.py +43 -19
- mito_ai/tests/providers/test_anthropic_client.py +178 -6
- mito_ai/tests/streamlit_conversion/test_apply_patch_to_text.py +368 -0
- mito_ai/tests/streamlit_conversion/test_fix_diff_headers.py +533 -0
- mito_ai/tests/streamlit_conversion/test_streamlit_agent_handler.py +71 -74
- mito_ai/tests/streamlit_conversion/test_streamlit_utils.py +16 -16
- mito_ai/tests/streamlit_conversion/test_validate_streamlit_app.py +17 -14
- mito_ai/tests/streamlit_preview/test_streamlit_preview_handler.py +2 -2
- mito_ai/tests/user/__init__.py +2 -0
- mito_ai/tests/user/test_user.py +120 -0
- mito_ai/user/handlers.py +33 -0
- mito_ai/user/urls.py +21 -0
- mito_ai/utils/anthropic_utils.py +8 -6
- mito_ai/utils/message_history_utils.py +4 -3
- mito_ai/utils/telemetry_utils.py +7 -4
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/build_log.json +1 -1
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/package.json +2 -2
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/schemas/mito_ai/package.json.orig +1 -1
- mito_ai-0.1.45.data/data/share/jupyter/labextensions/mito_ai/static/lib_index_js.0c3368195d954d2ed033.js → mito_ai-0.1.46.data/data/share/jupyter/labextensions/mito_ai/static/lib_index_js.20f12766ecd3d430568e.js +955 -173
- mito_ai-0.1.46.data/data/share/jupyter/labextensions/mito_ai/static/lib_index_js.20f12766ecd3d430568e.js.map +1 -0
- mito_ai-0.1.45.data/data/share/jupyter/labextensions/mito_ai/static/remoteEntry.684f82575fcc2e3b350c.js → mito_ai-0.1.46.data/data/share/jupyter/labextensions/mito_ai/static/remoteEntry.54126ab6511271265443.js +5 -5
- mito_ai-0.1.45.data/data/share/jupyter/labextensions/mito_ai/static/remoteEntry.684f82575fcc2e3b350c.js.map → mito_ai-0.1.46.data/data/share/jupyter/labextensions/mito_ai/static/remoteEntry.54126ab6511271265443.js.map +1 -1
- {mito_ai-0.1.45.dist-info → mito_ai-0.1.46.dist-info}/METADATA +1 -1
- {mito_ai-0.1.45.dist-info → mito_ai-0.1.46.dist-info}/RECORD +68 -58
- mito_ai-0.1.45.data/data/share/jupyter/labextensions/mito_ai/static/lib_index_js.0c3368195d954d2ed033.js.map +0 -1
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/etc/jupyter/jupyter_server_config.d/mito_ai.json +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/schemas/mito_ai/toolbar-buttons.json +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/node_modules_process_browser_js.4b128e94d31a81ebd209.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/node_modules_process_browser_js.4b128e94d31a81ebd209.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/style.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/style_index_js.5876024bb17dbd6a3ee6.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/style_index_js.5876024bb17dbd6a3ee6.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_auth_dist_esm_providers_cognito_apis_signOut_mjs-node_module-75790d.688c25857e7b81b1740f.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_auth_dist_esm_providers_cognito_apis_signOut_mjs-node_module-75790d.688c25857e7b81b1740f.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_auth_dist_esm_providers_cognito_tokenProvider_tokenProvider_-72f1c8.a917210f057fcfe224ad.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_auth_dist_esm_providers_cognito_tokenProvider_tokenProvider_-72f1c8.a917210f057fcfe224ad.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_dist_esm_index_mjs.6bac1a8c4cc93f15f6b7.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_dist_esm_index_mjs.6bac1a8c4cc93f15f6b7.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_ui-react_dist_esm_index_mjs.4fcecd65bef9e9847609.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_aws-amplify_ui-react_dist_esm_index_mjs.4fcecd65bef9e9847609.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_react-dom_client_js-node_modules_aws-amplify_ui-react_dist_styles_css.b43d4249e4d3dac9ad7b.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_react-dom_client_js-node_modules_aws-amplify_ui-react_dist_styles_css.b43d4249e4d3dac9ad7b.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_semver_index_js.3f6754ac5116d47de76b.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_semver_index_js.3f6754ac5116d47de76b.js.map +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_vscode-diff_dist_index_js.ea55f1f9346638aafbcf.js +0 -0
- {mito_ai-0.1.45.data → mito_ai-0.1.46.data}/data/share/jupyter/labextensions/mito_ai/static/vendors-node_modules_vscode-diff_dist_index_js.ea55f1f9346638aafbcf.js.map +0 -0
- {mito_ai-0.1.45.dist-info → mito_ai-0.1.46.dist-info}/WHEEL +0 -0
- {mito_ai-0.1.45.dist-info → mito_ai-0.1.46.dist-info}/entry_points.txt +0 -0
- {mito_ai-0.1.45.dist-info → mito_ai-0.1.46.dist-info}/licenses/LICENSE +0 -0
|
@@ -23,102 +23,97 @@ from mito_ai.streamlit_conversion.streamlit_utils import resolve_notebook_path
|
|
|
23
23
|
warnings.filterwarnings("ignore", message=".*bare mode.*")
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
def get_syntax_error(app_code: str) -> Optional[str]:
|
|
27
|
+
"""Check if the Python code has valid syntax"""
|
|
28
|
+
try:
|
|
29
|
+
ast.parse(app_code)
|
|
30
|
+
return None
|
|
31
|
+
except SyntaxError as e:
|
|
32
|
+
error_msg = ''.join(traceback.format_exception(type(e), e, e.__traceback__))
|
|
33
|
+
return error_msg
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
@contextmanager
|
|
45
|
-
def change_working_directory(path: str) -> Generator[None, Any, None]:
|
|
46
|
-
"""
|
|
47
|
-
Context manager to temporarily change working directory
|
|
48
|
-
so that relative paths are still valid when we run the app
|
|
49
|
-
"""
|
|
50
|
-
if path == '':
|
|
51
|
-
yield
|
|
52
|
-
|
|
53
|
-
original_cwd = os.getcwd()
|
|
54
|
-
try:
|
|
55
|
-
os.chdir(path)
|
|
56
|
-
yield
|
|
57
|
-
finally:
|
|
58
|
-
os.chdir(original_cwd)
|
|
35
|
+
def get_runtime_errors(app_code: str, app_path: str) -> Optional[List[Dict[str, Any]]]:
|
|
36
|
+
"""Start the Streamlit app in a subprocess"""
|
|
37
|
+
|
|
38
|
+
directory = os.path.dirname(app_path)
|
|
39
|
+
|
|
40
|
+
@contextmanager
|
|
41
|
+
def change_working_directory(path: str) -> Generator[None, Any, None]:
|
|
42
|
+
"""
|
|
43
|
+
Context manager to temporarily change working directory
|
|
44
|
+
so that relative paths are still valid when we run the app
|
|
45
|
+
"""
|
|
46
|
+
if path == '':
|
|
47
|
+
yield
|
|
59
48
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
49
|
+
original_cwd = os.getcwd()
|
|
50
|
+
try:
|
|
51
|
+
os.chdir(path)
|
|
52
|
+
yield
|
|
53
|
+
finally:
|
|
54
|
+
os.chdir(original_cwd)
|
|
55
|
+
|
|
56
|
+
with change_working_directory(directory):
|
|
57
|
+
# Create a temporary file that uses UTF-8 encoding so
|
|
58
|
+
# we don't run into issues with non-ASCII characters on Windows.
|
|
59
|
+
# We use utf-8 encoding when writing the app.py file so this validation
|
|
60
|
+
# code mirrors the actual file.
|
|
65
61
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
# Note: Since the AppTest.from_file tries to open the file, we need to first close the file
|
|
63
|
+
# by exiting the context manager and using the delete=False flag so that the file still exists.
|
|
64
|
+
# Windows can't open the same file twice at the same time. We cleanup at the end.
|
|
65
|
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False, encoding="utf-8") as f:
|
|
66
|
+
f.write(app_code)
|
|
67
|
+
temp_path = f.name
|
|
72
68
|
|
|
69
|
+
try:
|
|
70
|
+
# Run Streamlit test from file with UTF-8 encoding
|
|
71
|
+
app_test = AppTest.from_file(temp_path, default_timeout=30)
|
|
72
|
+
app_test.run()
|
|
73
|
+
|
|
74
|
+
# Check for exceptions
|
|
75
|
+
if app_test.exception:
|
|
76
|
+
errors = [{'type': 'exception', 'details': exc.value, 'message': exc.message, 'stack_trace': exc.stack_trace} for exc in app_test.exception]
|
|
77
|
+
return errors
|
|
78
|
+
|
|
79
|
+
# Check for error messages
|
|
80
|
+
if app_test.error:
|
|
81
|
+
errors = [{'type': 'error', 'details': err.value} for err in app_test.error]
|
|
82
|
+
return errors
|
|
83
|
+
|
|
84
|
+
return None
|
|
85
|
+
finally:
|
|
86
|
+
# Clean up the temporary file
|
|
73
87
|
try:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
# Check for exceptions
|
|
79
|
-
if app_test.exception:
|
|
80
|
-
errors = [{'type': 'exception', 'details': exc.value, 'message': exc.message, 'stack_trace': exc.stack_trace} for exc in app_test.exception]
|
|
81
|
-
return errors
|
|
82
|
-
|
|
83
|
-
# Check for error messages
|
|
84
|
-
if app_test.error:
|
|
85
|
-
errors = [{'type': 'error', 'details': err.value} for err in app_test.error]
|
|
86
|
-
return errors
|
|
87
|
-
|
|
88
|
-
return None
|
|
89
|
-
finally:
|
|
90
|
-
# Clean up the temporary file
|
|
91
|
-
try:
|
|
92
|
-
os.unlink(temp_path)
|
|
93
|
-
except OSError:
|
|
94
|
-
pass # File might already be deleted
|
|
88
|
+
os.unlink(temp_path)
|
|
89
|
+
except OSError:
|
|
90
|
+
pass # File might already be deleted
|
|
95
91
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
def check_for_errors(app_code: str, app_path: str) -> List[Dict[str, Any]]:
|
|
93
|
+
"""Complete validation pipeline"""
|
|
94
|
+
errors: List[Dict[str, Any]] = []
|
|
99
95
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
try:
|
|
97
|
+
# Step 1: Check syntax
|
|
98
|
+
syntax_error = get_syntax_error(app_code)
|
|
99
|
+
if syntax_error:
|
|
100
|
+
errors.append({'type': 'syntax', 'details': syntax_error})
|
|
105
101
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
102
|
+
runtime_errors = get_runtime_errors(app_code, app_path)
|
|
103
|
+
|
|
104
|
+
if runtime_errors:
|
|
105
|
+
errors.extend(runtime_errors)
|
|
106
|
+
|
|
107
|
+
except Exception as e:
|
|
108
|
+
errors.append({'type': 'validation', 'details': str(e)})
|
|
113
109
|
|
|
114
|
-
|
|
110
|
+
return errors
|
|
115
111
|
|
|
116
112
|
def validate_app(app_code: str, notebook_path: str) -> Tuple[bool, List[str]]:
|
|
117
113
|
"""Convenience function to validate Streamlit code"""
|
|
118
114
|
notebook_path = resolve_notebook_path(notebook_path)
|
|
119
115
|
|
|
120
|
-
|
|
121
|
-
errors = validator._validate_app(app_code, notebook_path)
|
|
116
|
+
errors = check_for_errors(app_code, notebook_path)
|
|
122
117
|
|
|
123
118
|
has_validation_error = len(errors) > 0
|
|
124
119
|
stringified_errors = [str(error) for error in errors]
|
|
@@ -82,17 +82,16 @@ class StreamlitPreviewHandler(APIHandler):
|
|
|
82
82
|
try:
|
|
83
83
|
# Parse and validate request
|
|
84
84
|
body = self.get_json_body()
|
|
85
|
-
is_valid, error_msg, notebook_path, force_recreate = validate_request_body(body)
|
|
86
|
-
if not is_valid or notebook_path
|
|
85
|
+
is_valid, error_msg, notebook_path, force_recreate, edit_prompt = validate_request_body(body)
|
|
86
|
+
if not is_valid or not notebook_path:
|
|
87
87
|
self.set_status(400)
|
|
88
88
|
self.finish({"error": error_msg})
|
|
89
89
|
return
|
|
90
90
|
|
|
91
|
-
|
|
92
91
|
# Ensure app exists
|
|
93
92
|
resolved_notebook_path = self._resolve_notebook_path(notebook_path)
|
|
94
93
|
|
|
95
|
-
success, error_msg = await ensure_app_exists(resolved_notebook_path, force_recreate)
|
|
94
|
+
success, error_msg = await ensure_app_exists(resolved_notebook_path, force_recreate, edit_prompt)
|
|
96
95
|
|
|
97
96
|
if not success:
|
|
98
97
|
self.set_status(500)
|
|
@@ -7,22 +7,26 @@ from mito_ai.streamlit_conversion.streamlit_utils import get_app_path
|
|
|
7
7
|
from mito_ai.streamlit_conversion.streamlit_agent_handler import streamlit_handler
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
def validate_request_body(body: Optional[dict]) -> Tuple[bool, str, Optional[str], bool]:
|
|
10
|
+
def validate_request_body(body: Optional[dict]) -> Tuple[bool, str, Optional[str], bool, str]:
|
|
11
11
|
"""Validate the request body and extract notebook_path and force_recreate."""
|
|
12
12
|
if body is None:
|
|
13
|
-
return False, "Invalid or missing JSON body", None, False
|
|
13
|
+
return False, "Invalid or missing JSON body", None, False, ""
|
|
14
14
|
|
|
15
15
|
notebook_path = body.get("notebook_path")
|
|
16
16
|
if not notebook_path:
|
|
17
|
-
return False, "Missing notebook_path parameter", None, False
|
|
17
|
+
return False, "Missing notebook_path parameter", None, False, ""
|
|
18
18
|
|
|
19
19
|
force_recreate = body.get("force_recreate", False)
|
|
20
20
|
if not isinstance(force_recreate, bool):
|
|
21
|
-
return False, "force_recreate must be a boolean", None, False
|
|
21
|
+
return False, "force_recreate must be a boolean", None, False, ""
|
|
22
|
+
|
|
23
|
+
edit_prompt = body.get("edit_prompt", "")
|
|
24
|
+
if not isinstance(edit_prompt, str):
|
|
25
|
+
return False, "edit_prompt must be a string", None, False, ""
|
|
22
26
|
|
|
23
|
-
return True, "", notebook_path, force_recreate
|
|
27
|
+
return True, "", notebook_path, force_recreate, edit_prompt
|
|
24
28
|
|
|
25
|
-
async def ensure_app_exists(resolved_notebook_path: str, force_recreate: bool = False) -> Tuple[bool, str]:
|
|
29
|
+
async def ensure_app_exists(resolved_notebook_path: str, force_recreate: bool = False, edit_prompt: str = "") -> Tuple[bool, str]:
|
|
26
30
|
"""Ensure app.py exists, generating it if necessary or if force_recreate is True."""
|
|
27
31
|
# Check if the app already exists
|
|
28
32
|
app_path = get_app_path(os.path.dirname(resolved_notebook_path))
|
|
@@ -33,7 +37,7 @@ async def ensure_app_exists(resolved_notebook_path: str, force_recreate: bool =
|
|
|
33
37
|
else:
|
|
34
38
|
print("[Mito AI] Force recreating streamlit app")
|
|
35
39
|
|
|
36
|
-
success, app_path, message = await streamlit_handler(resolved_notebook_path)
|
|
40
|
+
success, app_path, message = await streamlit_handler(resolved_notebook_path, edit_prompt)
|
|
37
41
|
|
|
38
42
|
if not success or app_path is None:
|
|
39
43
|
return False, f"Failed to generate streamlit code: {message}"
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# Copyright (c) Saga Inc.
|
|
2
|
+
# Distributed under the terms of the GNU Affero General Public License v3.0 License.
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
import requests
|
|
6
|
+
import time
|
|
7
|
+
from unittest.mock import patch, MagicMock
|
|
8
|
+
from mito_ai.tests.conftest import TOKEN
|
|
9
|
+
from mito_ai.completions.message_history import GlobalMessageHistory, ChatThread
|
|
10
|
+
from mito_ai.completions.models import ThreadID
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture
|
|
14
|
+
def mock_chat_threads():
|
|
15
|
+
"""Fixture that creates mock chat threads for testing"""
|
|
16
|
+
thread_id_1 = ThreadID("test-thread-1")
|
|
17
|
+
thread_id_2 = ThreadID("test-thread-2")
|
|
18
|
+
|
|
19
|
+
# Create mock threads with different timestamps
|
|
20
|
+
thread_1 = ChatThread(
|
|
21
|
+
thread_id=thread_id_1,
|
|
22
|
+
creation_ts=time.time() - 3600, # 1 hour ago
|
|
23
|
+
last_interaction_ts=time.time() - 1800, # 30 minutes ago
|
|
24
|
+
name="Test Chat 1",
|
|
25
|
+
ai_optimized_history=[
|
|
26
|
+
{"role": "user", "content": "Hello"},
|
|
27
|
+
{"role": "assistant", "content": "Hi there!"},
|
|
28
|
+
],
|
|
29
|
+
display_history=[
|
|
30
|
+
{"role": "user", "content": "Hello"},
|
|
31
|
+
{"role": "assistant", "content": "Hi there!"},
|
|
32
|
+
],
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
thread_2 = ChatThread(
|
|
36
|
+
thread_id=thread_id_2,
|
|
37
|
+
creation_ts=time.time() - 7200, # 2 hours ago
|
|
38
|
+
last_interaction_ts=time.time() - 900, # 15 minutes ago (more recent)
|
|
39
|
+
name="Test Chat 2",
|
|
40
|
+
ai_optimized_history=[
|
|
41
|
+
{"role": "user", "content": "How are you?"},
|
|
42
|
+
{"role": "assistant", "content": "I'm doing well, thanks!"},
|
|
43
|
+
],
|
|
44
|
+
display_history=[
|
|
45
|
+
{"role": "user", "content": "How are you?"},
|
|
46
|
+
{"role": "assistant", "content": "I'm doing well, thanks!"},
|
|
47
|
+
],
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return {thread_id_1: thread_1, thread_id_2: thread_2}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.fixture
|
|
54
|
+
def mock_message_history(mock_chat_threads):
|
|
55
|
+
"""Fixture that mocks the GlobalMessageHistory with test data"""
|
|
56
|
+
mock_history = MagicMock(spec=GlobalMessageHistory)
|
|
57
|
+
mock_history._chat_threads = mock_chat_threads
|
|
58
|
+
|
|
59
|
+
# Mock the get_threads method to return sorted threads
|
|
60
|
+
def mock_get_threads():
|
|
61
|
+
from mito_ai.completions.models import ChatThreadMetadata
|
|
62
|
+
|
|
63
|
+
threads = []
|
|
64
|
+
for thread in mock_chat_threads.values():
|
|
65
|
+
threads.append(
|
|
66
|
+
ChatThreadMetadata(
|
|
67
|
+
thread_id=thread.thread_id,
|
|
68
|
+
name=thread.name,
|
|
69
|
+
creation_ts=thread.creation_ts,
|
|
70
|
+
last_interaction_ts=thread.last_interaction_ts,
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
# Sort by last_interaction_ts (newest first)
|
|
74
|
+
threads.sort(key=lambda x: x.last_interaction_ts, reverse=True)
|
|
75
|
+
return threads
|
|
76
|
+
|
|
77
|
+
mock_history.get_threads = mock_get_threads
|
|
78
|
+
return mock_history
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# --- GET ALL THREADS ---
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_get_all_threads_success(jp_base_url: str, mock_message_history):
|
|
85
|
+
"""Test successful GET all threads endpoint"""
|
|
86
|
+
# Since the server extension is already loaded, we need to work with the actual instance
|
|
87
|
+
# Let's just test that the endpoint works and returns the expected structure
|
|
88
|
+
response = requests.get(
|
|
89
|
+
jp_base_url + "/mito-ai/chat-history/threads",
|
|
90
|
+
headers={"Authorization": f"token {TOKEN}"},
|
|
91
|
+
)
|
|
92
|
+
assert response.status_code == 200
|
|
93
|
+
|
|
94
|
+
response_json = response.json()
|
|
95
|
+
assert "threads" in response_json
|
|
96
|
+
# The actual number of threads will depend on what's in the .mito/ai-chats directory
|
|
97
|
+
# So we'll just check that it's a list
|
|
98
|
+
assert isinstance(response_json["threads"], list)
|
|
99
|
+
|
|
100
|
+
# Check thread structure for any threads that exist
|
|
101
|
+
for thread in response_json["threads"]:
|
|
102
|
+
assert "thread_id" in thread
|
|
103
|
+
assert "name" in thread
|
|
104
|
+
assert "creation_ts" in thread
|
|
105
|
+
assert "last_interaction_ts" in thread
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_get_all_threads_empty(jp_base_url: str):
|
|
109
|
+
"""Test GET all threads endpoint when no threads exist"""
|
|
110
|
+
# This test will work with whatever threads exist in the actual .mito/ai-chats directory
|
|
111
|
+
# We'll just verify the endpoint works and returns the expected structure
|
|
112
|
+
response = requests.get(
|
|
113
|
+
jp_base_url + "/mito-ai/chat-history/threads",
|
|
114
|
+
headers={"Authorization": f"token {TOKEN}"},
|
|
115
|
+
)
|
|
116
|
+
assert response.status_code == 200
|
|
117
|
+
|
|
118
|
+
response_json = response.json()
|
|
119
|
+
assert "threads" in response_json
|
|
120
|
+
assert isinstance(response_json["threads"], list)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_get_all_threads_with_no_auth(jp_base_url: str):
|
|
124
|
+
"""Test GET all threads endpoint without authentication"""
|
|
125
|
+
response = requests.get(
|
|
126
|
+
jp_base_url + "/mito-ai/chat-history/threads",
|
|
127
|
+
)
|
|
128
|
+
assert response.status_code == 403 # Forbidden
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_get_all_threads_with_incorrect_auth(jp_base_url: str):
|
|
132
|
+
"""Test GET all threads endpoint with incorrect authentication"""
|
|
133
|
+
response = requests.get(
|
|
134
|
+
jp_base_url + "/mito-ai/chat-history/threads",
|
|
135
|
+
headers={"Authorization": f"token incorrect-token"},
|
|
136
|
+
)
|
|
137
|
+
assert response.status_code == 403 # Forbidden
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# --- GET SPECIFIC THREAD ---
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def test_get_specific_thread_success(jp_base_url: str, mock_message_history):
|
|
144
|
+
"""Test successful GET specific thread endpoint"""
|
|
145
|
+
# First, get all threads to see what's available
|
|
146
|
+
response = requests.get(
|
|
147
|
+
jp_base_url + "/mito-ai/chat-history/threads",
|
|
148
|
+
headers={"Authorization": f"token {TOKEN}"},
|
|
149
|
+
)
|
|
150
|
+
assert response.status_code == 200
|
|
151
|
+
|
|
152
|
+
threads = response.json()["threads"]
|
|
153
|
+
if not threads:
|
|
154
|
+
# If no threads exist, skip this test
|
|
155
|
+
pytest.skip("No threads available for testing")
|
|
156
|
+
|
|
157
|
+
# Use the first available thread
|
|
158
|
+
thread_id = threads[0]["thread_id"]
|
|
159
|
+
|
|
160
|
+
response = requests.get(
|
|
161
|
+
jp_base_url + f"/mito-ai/chat-history/threads/{thread_id}",
|
|
162
|
+
headers={"Authorization": f"token {TOKEN}"},
|
|
163
|
+
)
|
|
164
|
+
assert response.status_code == 200
|
|
165
|
+
|
|
166
|
+
response_json = response.json()
|
|
167
|
+
assert response_json["thread_id"] == thread_id
|
|
168
|
+
assert "name" in response_json
|
|
169
|
+
assert "creation_ts" in response_json
|
|
170
|
+
assert "last_interaction_ts" in response_json
|
|
171
|
+
assert "display_history" in response_json
|
|
172
|
+
assert "ai_optimized_history" in response_json
|
|
173
|
+
|
|
174
|
+
# Check message history structure
|
|
175
|
+
display_history = response_json["display_history"]
|
|
176
|
+
assert isinstance(display_history, list)
|
|
177
|
+
ai_optimized_history = response_json["ai_optimized_history"]
|
|
178
|
+
assert isinstance(ai_optimized_history, list)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_get_specific_thread_not_found(jp_base_url: str, mock_message_history):
|
|
182
|
+
"""Test GET specific thread endpoint with non-existent thread ID"""
|
|
183
|
+
# Use a clearly non-existent thread ID
|
|
184
|
+
fake_thread_id = "non-existent-thread-12345"
|
|
185
|
+
|
|
186
|
+
response = requests.get(
|
|
187
|
+
jp_base_url + f"/mito-ai/chat-history/threads/{fake_thread_id}",
|
|
188
|
+
headers={"Authorization": f"token {TOKEN}"},
|
|
189
|
+
)
|
|
190
|
+
assert response.status_code == 404
|
|
191
|
+
|
|
192
|
+
response_json = response.json()
|
|
193
|
+
assert "error" in response_json
|
|
194
|
+
assert fake_thread_id in response_json["error"]
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_get_specific_thread_with_no_auth(jp_base_url: str):
|
|
198
|
+
"""Test GET specific thread endpoint without authentication"""
|
|
199
|
+
response = requests.get(
|
|
200
|
+
jp_base_url + "/mito-ai/chat-history/threads/test-thread-1",
|
|
201
|
+
)
|
|
202
|
+
assert response.status_code == 403 # Forbidden
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_get_specific_thread_with_incorrect_auth(jp_base_url: str):
|
|
206
|
+
"""Test GET specific thread endpoint with incorrect authentication"""
|
|
207
|
+
response = requests.get(
|
|
208
|
+
jp_base_url + "/mito-ai/chat-history/threads/test-thread-1",
|
|
209
|
+
headers={"Authorization": f"token incorrect-token"},
|
|
210
|
+
)
|
|
211
|
+
assert response.status_code == 403 # Forbidden
|
|
@@ -233,16 +233,17 @@ file1.csv
|
|
|
233
233
|
file2.txt
|
|
234
234
|
"""
|
|
235
235
|
|
|
236
|
-
# Create test messages with proper typing
|
|
236
|
+
# Create test messages with proper typing
|
|
237
237
|
messages: List[ChatCompletionMessageParam] = [
|
|
238
238
|
{"role": "system", "content": system_message_with_sections},
|
|
239
239
|
{"role": "user", "content": user_message_with_sections},
|
|
240
240
|
{"role": "assistant", "content": assistant_message_with_sections},
|
|
241
|
-
{"role": "user", "content": "Recent user message"},
|
|
241
|
+
{"role": "user", "content": "Recent user message 1"},
|
|
242
|
+
{"role": "user", "content": "Recent user message 2"},
|
|
243
|
+
{"role": "user", "content": "Recent user message 3"},
|
|
242
244
|
]
|
|
243
245
|
|
|
244
|
-
|
|
245
|
-
result = trim_old_messages(messages, keep_recent=1)
|
|
246
|
+
result = trim_old_messages(messages)
|
|
246
247
|
|
|
247
248
|
# System message should remain unchanged even though it's old
|
|
248
249
|
system_content = result[0].get("content")
|
|
@@ -265,14 +266,22 @@ file2.txt
|
|
|
265
266
|
assert FILES_SECTION_HEADING in assistant_content
|
|
266
267
|
assert "file1.csv" in assistant_content
|
|
267
268
|
|
|
268
|
-
# Recent user
|
|
269
|
-
|
|
270
|
-
assert isinstance(
|
|
271
|
-
assert
|
|
269
|
+
# Recent user messages should remain unchanged
|
|
270
|
+
recent_content_1 = result[3].get("content")
|
|
271
|
+
assert isinstance(recent_content_1, str)
|
|
272
|
+
assert recent_content_1 == "Recent user message 1"
|
|
273
|
+
|
|
274
|
+
recent_content_2 = result[4].get("content")
|
|
275
|
+
assert isinstance(recent_content_2, str)
|
|
276
|
+
assert recent_content_2 == "Recent user message 2"
|
|
277
|
+
|
|
278
|
+
recent_content_3 = result[5].get("content")
|
|
279
|
+
assert isinstance(recent_content_3, str)
|
|
280
|
+
assert recent_content_3 == "Recent user message 3"
|
|
272
281
|
|
|
273
282
|
|
|
274
283
|
def test_trim_old_messages_preserves_recent_messages() -> None:
|
|
275
|
-
"""Test that trim_old_messages preserves the most recent messages based on
|
|
284
|
+
"""Test that trim_old_messages preserves the most recent messages based on MESSAGE_HISTORY_TRIM_THRESHOLD."""
|
|
276
285
|
# Create test messages
|
|
277
286
|
old_message_1 = f"""Old message 1.
|
|
278
287
|
{FILES_SECTION_HEADING}
|
|
@@ -289,6 +298,10 @@ file3.csv
|
|
|
289
298
|
recent_message_2 = f"""Recent message 2.
|
|
290
299
|
{FILES_SECTION_HEADING}
|
|
291
300
|
file4.csv
|
|
301
|
+
"""
|
|
302
|
+
recent_message_3 = f"""Recent message 3.
|
|
303
|
+
{FILES_SECTION_HEADING}
|
|
304
|
+
file5.csv
|
|
292
305
|
"""
|
|
293
306
|
|
|
294
307
|
# Create test messages with proper typing
|
|
@@ -297,10 +310,11 @@ file4.csv
|
|
|
297
310
|
{"role": "user", "content": old_message_2},
|
|
298
311
|
{"role": "user", "content": recent_message_1},
|
|
299
312
|
{"role": "user", "content": recent_message_2},
|
|
313
|
+
{"role": "user", "content": recent_message_3},
|
|
300
314
|
]
|
|
301
315
|
|
|
302
|
-
#
|
|
303
|
-
result = trim_old_messages(messages
|
|
316
|
+
# Test with MESSAGE_HISTORY_TRIM_THRESHOLD (3) - only the first 2 messages should be trimmed
|
|
317
|
+
result = trim_old_messages(messages)
|
|
304
318
|
|
|
305
319
|
# Old messages should be trimmed
|
|
306
320
|
old_content_1 = result[0].get("content")
|
|
@@ -325,24 +339,30 @@ file4.csv
|
|
|
325
339
|
assert recent_content_2 == recent_message_2
|
|
326
340
|
assert FILES_SECTION_HEADING in recent_content_2
|
|
327
341
|
assert "file4.csv" in recent_content_2
|
|
342
|
+
|
|
343
|
+
recent_content_3 = result[4].get("content")
|
|
344
|
+
assert isinstance(recent_content_3, str)
|
|
345
|
+
assert recent_content_3 == recent_message_3
|
|
346
|
+
assert FILES_SECTION_HEADING in recent_content_3
|
|
347
|
+
assert "file5.csv" in recent_content_3
|
|
328
348
|
|
|
329
349
|
def test_trim_old_messages_empty_list() -> None:
|
|
330
350
|
"""Test that trim_old_messages handles empty message lists correctly."""
|
|
331
351
|
messages: List[ChatCompletionMessageParam] = []
|
|
332
|
-
result = trim_old_messages(messages
|
|
352
|
+
result = trim_old_messages(messages)
|
|
333
353
|
assert result == []
|
|
334
354
|
|
|
335
355
|
|
|
336
|
-
def
|
|
337
|
-
"""Test that trim_old_messages doesn't modify messages if there are fewer than
|
|
356
|
+
def test_trim_old_messages_fewer_than_threshold() -> None:
|
|
357
|
+
"""Test that trim_old_messages doesn't modify messages if there are fewer than MESSAGE_HISTORY_TRIM_THRESHOLD."""
|
|
338
358
|
messages: List[ChatCompletionMessageParam] = [
|
|
339
359
|
{"role": "user", "content": "User message 1"},
|
|
340
360
|
{"role": "assistant", "content": "Assistant message 1"},
|
|
341
361
|
]
|
|
342
362
|
|
|
343
|
-
result = trim_old_messages(messages
|
|
363
|
+
result = trim_old_messages(messages)
|
|
344
364
|
|
|
345
|
-
# Messages should remain unchanged
|
|
365
|
+
# Messages should remain unchanged since we have fewer than MESSAGE_HISTORY_TRIM_THRESHOLD (3) messages
|
|
346
366
|
user_content = result[0].get("content")
|
|
347
367
|
assert isinstance(user_content, str)
|
|
348
368
|
assert user_content == "User message 1"
|
|
@@ -373,15 +393,17 @@ def test_trim_mixed_content_messages() -> None:
|
|
|
373
393
|
})
|
|
374
394
|
|
|
375
395
|
# Create sample message list with one old message (the mixed content)
|
|
376
|
-
# and
|
|
396
|
+
# and enough recent messages to exceed MESSAGE_HISTORY_TRIM_THRESHOLD (3)
|
|
377
397
|
message_list: List[ChatCompletionMessageParam] = [
|
|
378
398
|
mixed_content_message, # This should get trimmed
|
|
379
399
|
{"role": "assistant", "content": "That's a chart showing data trends"},
|
|
380
|
-
{"role": "user", "content": "Can you explain more?"} # Recent message, should not be trimmed
|
|
400
|
+
{"role": "user", "content": "Can you explain more?"}, # Recent message, should not be trimmed
|
|
401
|
+
{"role": "user", "content": "Another recent message"}, # Recent message, should not be trimmed
|
|
402
|
+
{"role": "user", "content": "Yet another recent message"} # Recent message, should not be trimmed
|
|
381
403
|
]
|
|
382
404
|
|
|
383
405
|
# Apply the trimming function
|
|
384
|
-
trimmed_messages = trim_old_messages(message_list
|
|
406
|
+
trimmed_messages = trim_old_messages(message_list)
|
|
385
407
|
|
|
386
408
|
# Verify that the first message has been trimmed properly
|
|
387
409
|
assert trimmed_messages[0]["role"] == "user"
|
|
@@ -390,6 +412,8 @@ def test_trim_mixed_content_messages() -> None:
|
|
|
390
412
|
# Verify that the recent messages are untouched
|
|
391
413
|
assert trimmed_messages[1] == message_list[1]
|
|
392
414
|
assert trimmed_messages[2] == message_list[2]
|
|
415
|
+
assert trimmed_messages[3] == message_list[3]
|
|
416
|
+
assert trimmed_messages[4] == message_list[4]
|
|
393
417
|
|
|
394
418
|
|
|
395
419
|
def test_get_display_history_calls_update_last_interaction() -> None:
|