chatterer 0.1.18__py3-none-any.whl → 0.1.19__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.
Files changed (44) hide show
  1. chatterer/__init__.py +93 -93
  2. chatterer/common_types/__init__.py +21 -21
  3. chatterer/common_types/io.py +19 -19
  4. chatterer/examples/__init__.py +0 -0
  5. chatterer/examples/anything_to_markdown.py +95 -91
  6. chatterer/examples/get_code_snippets.py +64 -62
  7. chatterer/examples/login_with_playwright.py +171 -167
  8. chatterer/examples/make_ppt.py +499 -497
  9. chatterer/examples/pdf_to_markdown.py +107 -107
  10. chatterer/examples/pdf_to_text.py +60 -56
  11. chatterer/examples/transcription_api.py +127 -123
  12. chatterer/examples/upstage_parser.py +95 -100
  13. chatterer/examples/webpage_to_markdown.py +79 -79
  14. chatterer/interactive.py +354 -354
  15. chatterer/language_model.py +533 -533
  16. chatterer/messages.py +21 -21
  17. chatterer/strategies/__init__.py +13 -13
  18. chatterer/strategies/atom_of_thoughts.py +975 -975
  19. chatterer/strategies/base.py +14 -14
  20. chatterer/tools/__init__.py +46 -46
  21. chatterer/tools/caption_markdown_images.py +384 -384
  22. chatterer/tools/citation_chunking/__init__.py +3 -3
  23. chatterer/tools/citation_chunking/chunks.py +53 -53
  24. chatterer/tools/citation_chunking/citation_chunker.py +118 -118
  25. chatterer/tools/citation_chunking/citations.py +285 -285
  26. chatterer/tools/citation_chunking/prompt.py +157 -157
  27. chatterer/tools/citation_chunking/reference.py +26 -26
  28. chatterer/tools/citation_chunking/utils.py +138 -138
  29. chatterer/tools/convert_pdf_to_markdown.py +302 -302
  30. chatterer/tools/convert_to_text.py +447 -447
  31. chatterer/tools/upstage_document_parser.py +705 -705
  32. chatterer/tools/webpage_to_markdown.py +739 -739
  33. chatterer/tools/youtube.py +146 -146
  34. chatterer/utils/__init__.py +15 -15
  35. chatterer/utils/base64_image.py +285 -285
  36. chatterer/utils/bytesio.py +59 -59
  37. chatterer/utils/code_agent.py +237 -237
  38. chatterer/utils/imghdr.py +148 -148
  39. {chatterer-0.1.18.dist-info → chatterer-0.1.19.dist-info}/METADATA +392 -392
  40. chatterer-0.1.19.dist-info/RECORD +44 -0
  41. {chatterer-0.1.18.dist-info → chatterer-0.1.19.dist-info}/WHEEL +1 -1
  42. chatterer-0.1.19.dist-info/entry_points.txt +10 -0
  43. chatterer-0.1.18.dist-info/RECORD +0 -42
  44. {chatterer-0.1.18.dist-info → chatterer-0.1.19.dist-info}/top_level.txt +0 -0
@@ -1,167 +1,171 @@
1
- def resolve_import_path_and_get_logger():
2
- # ruff: noqa: E402
3
- import logging
4
- import sys
5
-
6
- if __name__ == "__main__" and "." not in sys.path:
7
- sys.path.append(".")
8
-
9
- logger = logging.getLogger(__name__)
10
- return logger
11
-
12
-
13
- logger = resolve_import_path_and_get_logger()
14
- import json
15
- import sys
16
- from pathlib import Path
17
-
18
- from spargear import BaseArguments, SubcommandSpec
19
-
20
- from chatterer import PlayWrightBot
21
-
22
-
23
- def read_session(url: str, jsonpath: Path) -> None:
24
- """
25
- Loads the session state from the specified JSON file, then navigates
26
- to a protected_url that normally requires login. If the stored session
27
- is valid, it should open without re-entering credentials.
28
-
29
- Correction: Loads the JSON content into a dict first to satisfy type hints.
30
- """
31
- logger.info(f"Loading session from {jsonpath} and navigating to {url} ...")
32
-
33
- if not jsonpath.exists():
34
- logger.error(f"Session file not found at {jsonpath}")
35
- sys.exit(1)
36
-
37
- # Load the storage state from the JSON file into a dictionary
38
- logger.info(f"Reading storage state content from {jsonpath} ...")
39
- try:
40
- with open(jsonpath, "r", encoding="utf-8") as f:
41
- # This dictionary should match the 'StorageState' type expected by Playwright/chatterer
42
- storage_state_dict = json.load(f)
43
- except json.JSONDecodeError:
44
- logger.error(f"Failed to decode JSON from {jsonpath}")
45
- sys.exit(1)
46
- except Exception as e:
47
- logger.error(f"Error reading file {jsonpath}: {e}")
48
- sys.exit(1)
49
-
50
- logger.info("Launching browser with loaded session state...")
51
- with PlayWrightBot(
52
- playwright_launch_options={"headless": False},
53
- # Pass the loaded dictionary, which should match the expected 'StorageState' type
54
- playwright_persistency_options={"storage_state": storage_state_dict},
55
- ) as bot:
56
- bot.get_page(url)
57
-
58
- logger.info("Press Enter in the console when you're done checking the protected page.")
59
- input(" >> Press Enter to exit: ")
60
-
61
- logger.info("Done! Browser is now closed.")
62
-
63
-
64
- def write_session(url: str, jsonpath: Path) -> None:
65
- """
66
- Launches a non-headless browser and navigates to the login_url.
67
- The user can manually log in, then press Enter in the console
68
- to store the current session state into a JSON file.
69
- """
70
- logger.info(f"Launching browser and navigating to {url} ... Please log in manually.")
71
-
72
- # Ensure jsonpath directory exists
73
- jsonpath.parent.mkdir(parents=True, exist_ok=True)
74
-
75
- with PlayWrightBot(playwright_launch_options={"headless": False}) as bot:
76
- bot.get_page(url)
77
-
78
- logger.info("After completing the login in the browser, press Enter here to save the session.")
79
- input(" >> Press Enter when ready: ")
80
-
81
- # get_sync_browser() returns the BrowserContext internally
82
- context = bot.get_sync_browser()
83
-
84
- # Save the current session (cookies, localStorage) to a JSON file
85
- logger.info(f"Saving storage state to {jsonpath} ...")
86
- context.storage_state(path=jsonpath) # Pass Path object directly
87
-
88
- logger.info("Done! Browser is now closed.")
89
-
90
-
91
- # --- Spargear Declarative CLI Definition ---
92
-
93
- # Define the default path location relative to this script file
94
- DEFAULT_JSON_PATH = Path(__file__).resolve().parent / "session_state.json"
95
-
96
-
97
- class ReadArgs(BaseArguments):
98
- """Arguments for the 'read' subcommand."""
99
-
100
- url: str
101
- """URL (potentially protected) to navigate to using the saved session."""
102
- jsonpath: Path = DEFAULT_JSON_PATH
103
- """Path to the session state JSON file to load."""
104
-
105
-
106
- class WriteArgs(BaseArguments):
107
- """Arguments for the 'write' subcommand."""
108
-
109
- url: str
110
- """URL to navigate to for manual login."""
111
- jsonpath: Path = DEFAULT_JSON_PATH
112
- """Path to save the session state JSON file."""
113
-
114
-
115
- class LoginWithPlaywrightArgs(BaseArguments):
116
- """
117
- A simple CLI tool for saving and using Playwright sessions via storage_state.
118
- Uses spargear for declarative argument parsing.
119
- """
120
-
121
- read: SubcommandSpec[ReadArgs] = SubcommandSpec(
122
- name="read",
123
- argument_class=ReadArgs,
124
- help="Use a saved session to view a protected page.",
125
- description="Loads session state from the specified JSON file and navigates to the URL.",
126
- )
127
- write: SubcommandSpec[WriteArgs] = SubcommandSpec(
128
- name="write",
129
- argument_class=WriteArgs,
130
- help="Save a new session by manually logging in.",
131
- description="Launches a browser to the specified URL. Log in manually, then press Enter to save session state.",
132
- )
133
-
134
- def run(self) -> None:
135
- """Parses arguments using spargear and executes the corresponding command."""
136
- try:
137
- if (read := self.read.argument_class).url:
138
- # Access attributes directly from the returned instance
139
- logger.info("Running READ command:")
140
- logger.info(f" URL: {read.url}")
141
- logger.info(f" JSON Path: {read.jsonpath}")
142
- read_session(url=read.url, jsonpath=read.jsonpath)
143
- elif (write := self.write.argument_class).url:
144
- # Access attributes directly from the returned instance
145
- logger.info("Running WRITE command:")
146
- logger.info(f" URL: {write.url}")
147
- logger.info(f" JSON Path: {write.jsonpath}")
148
- write_session(url=write.url, jsonpath=write.jsonpath)
149
- else:
150
- logger.error("No valid subcommand provided. Use 'read' or 'write'.")
151
- sys.exit(1)
152
-
153
- except SystemExit as e:
154
- # Handle cases like -h/--help or argparse errors that exit
155
- sys.exit(e.code)
156
- except Exception as e:
157
- logger.error(f"\nAn error occurred: {e}")
158
- # from traceback import print_exc # Uncomment for full traceback
159
- # print_exc() # Uncomment for full traceback
160
- sys.exit(1)
161
-
162
-
163
- # --- Main Execution Logic ---
164
-
165
-
166
- if __name__ == "__main__":
167
- LoginWithPlaywrightArgs().run()
1
+ def resolve_import_path_and_get_logger():
2
+ # ruff: noqa: E402
3
+ import logging
4
+ import sys
5
+
6
+ if __name__ == "__main__" and "." not in sys.path:
7
+ sys.path.append(".")
8
+
9
+ logger = logging.getLogger(__name__)
10
+ return logger
11
+
12
+
13
+ logger = resolve_import_path_and_get_logger()
14
+ import json
15
+ import sys
16
+ from pathlib import Path
17
+
18
+ from spargear import BaseArguments, SubcommandSpec
19
+
20
+ from chatterer import PlayWrightBot
21
+
22
+
23
+ def read_session(url: str, jsonpath: Path) -> None:
24
+ """
25
+ Loads the session state from the specified JSON file, then navigates
26
+ to a protected_url that normally requires login. If the stored session
27
+ is valid, it should open without re-entering credentials.
28
+
29
+ Correction: Loads the JSON content into a dict first to satisfy type hints.
30
+ """
31
+ logger.info(f"Loading session from {jsonpath} and navigating to {url} ...")
32
+
33
+ if not jsonpath.exists():
34
+ logger.error(f"Session file not found at {jsonpath}")
35
+ sys.exit(1)
36
+
37
+ # Load the storage state from the JSON file into a dictionary
38
+ logger.info(f"Reading storage state content from {jsonpath} ...")
39
+ try:
40
+ with open(jsonpath, "r", encoding="utf-8") as f:
41
+ # This dictionary should match the 'StorageState' type expected by Playwright/chatterer
42
+ storage_state_dict = json.load(f)
43
+ except json.JSONDecodeError:
44
+ logger.error(f"Failed to decode JSON from {jsonpath}")
45
+ sys.exit(1)
46
+ except Exception as e:
47
+ logger.error(f"Error reading file {jsonpath}: {e}")
48
+ sys.exit(1)
49
+
50
+ logger.info("Launching browser with loaded session state...")
51
+ with PlayWrightBot(
52
+ playwright_launch_options={"headless": False},
53
+ # Pass the loaded dictionary, which should match the expected 'StorageState' type
54
+ playwright_persistency_options={"storage_state": storage_state_dict},
55
+ ) as bot:
56
+ bot.get_page(url)
57
+
58
+ logger.info("Press Enter in the console when you're done checking the protected page.")
59
+ input(" >> Press Enter to exit: ")
60
+
61
+ logger.info("Done! Browser is now closed.")
62
+
63
+
64
+ def write_session(url: str, jsonpath: Path) -> None:
65
+ """
66
+ Launches a non-headless browser and navigates to the login_url.
67
+ The user can manually log in, then press Enter in the console
68
+ to store the current session state into a JSON file.
69
+ """
70
+ logger.info(f"Launching browser and navigating to {url} ... Please log in manually.")
71
+
72
+ # Ensure jsonpath directory exists
73
+ jsonpath.parent.mkdir(parents=True, exist_ok=True)
74
+
75
+ with PlayWrightBot(playwright_launch_options={"headless": False}) as bot:
76
+ bot.get_page(url)
77
+
78
+ logger.info("After completing the login in the browser, press Enter here to save the session.")
79
+ input(" >> Press Enter when ready: ")
80
+
81
+ # get_sync_browser() returns the BrowserContext internally
82
+ context = bot.get_sync_browser()
83
+
84
+ # Save the current session (cookies, localStorage) to a JSON file
85
+ logger.info(f"Saving storage state to {jsonpath} ...")
86
+ context.storage_state(path=jsonpath) # Pass Path object directly
87
+
88
+ logger.info("Done! Browser is now closed.")
89
+
90
+
91
+ # --- Spargear Declarative CLI Definition ---
92
+
93
+ # Define the default path location relative to this script file
94
+ DEFAULT_JSON_PATH = Path(__file__).resolve().parent / "session_state.json"
95
+
96
+
97
+ class ReadArgs(BaseArguments):
98
+ """Arguments for the 'read' subcommand."""
99
+
100
+ url: str
101
+ """URL (potentially protected) to navigate to using the saved session."""
102
+ jsonpath: Path = DEFAULT_JSON_PATH
103
+ """Path to the session state JSON file to load."""
104
+
105
+
106
+ class WriteArgs(BaseArguments):
107
+ """Arguments for the 'write' subcommand."""
108
+
109
+ url: str
110
+ """URL to navigate to for manual login."""
111
+ jsonpath: Path = DEFAULT_JSON_PATH
112
+ """Path to save the session state JSON file."""
113
+
114
+
115
+ class LoginWithPlaywrightArgs(BaseArguments):
116
+ """
117
+ A simple CLI tool for saving and using Playwright sessions via storage_state.
118
+ Uses spargear for declarative argument parsing.
119
+ """
120
+
121
+ read: SubcommandSpec[ReadArgs] = SubcommandSpec(
122
+ name="read",
123
+ argument_class=ReadArgs,
124
+ help="Use a saved session to view a protected page.",
125
+ description="Loads session state from the specified JSON file and navigates to the URL.",
126
+ )
127
+ write: SubcommandSpec[WriteArgs] = SubcommandSpec(
128
+ name="write",
129
+ argument_class=WriteArgs,
130
+ help="Save a new session by manually logging in.",
131
+ description="Launches a browser to the specified URL. Log in manually, then press Enter to save session state.",
132
+ )
133
+
134
+ def run(self) -> None:
135
+ """Parses arguments using spargear and executes the corresponding command."""
136
+ try:
137
+ if (read := self.read.argument_class).url:
138
+ # Access attributes directly from the returned instance
139
+ logger.info("Running READ command:")
140
+ logger.info(f" URL: {read.url}")
141
+ logger.info(f" JSON Path: {read.jsonpath}")
142
+ read_session(url=read.url, jsonpath=read.jsonpath)
143
+ elif (write := self.write.argument_class).url:
144
+ # Access attributes directly from the returned instance
145
+ logger.info("Running WRITE command:")
146
+ logger.info(f" URL: {write.url}")
147
+ logger.info(f" JSON Path: {write.jsonpath}")
148
+ write_session(url=write.url, jsonpath=write.jsonpath)
149
+ else:
150
+ logger.error("No valid subcommand provided. Use 'read' or 'write'.")
151
+ sys.exit(1)
152
+
153
+ except SystemExit as e:
154
+ # Handle cases like -h/--help or argparse errors that exit
155
+ sys.exit(e.code)
156
+ except Exception as e:
157
+ logger.error(f"\nAn error occurred: {e}")
158
+ # from traceback import print_exc # Uncomment for full traceback
159
+ # print_exc() # Uncomment for full traceback
160
+ sys.exit(1)
161
+
162
+
163
+ # --- Main Execution Logic ---
164
+
165
+
166
+ def main() -> None:
167
+ LoginWithPlaywrightArgs().run()
168
+
169
+
170
+ if __name__ == "__main__":
171
+ main()