chatterer 0.1.24__py3-none-any.whl → 0.1.26__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 +87 -93
  2. chatterer/common_types/__init__.py +21 -21
  3. chatterer/common_types/io.py +19 -19
  4. chatterer/examples/__main__.py +75 -75
  5. chatterer/examples/any2md.py +85 -85
  6. chatterer/examples/pdf2md.py +338 -338
  7. chatterer/examples/pdf2txt.py +54 -54
  8. chatterer/examples/ppt.py +486 -486
  9. chatterer/examples/pw.py +143 -137
  10. chatterer/examples/snippet.py +56 -55
  11. chatterer/examples/transcribe.py +192 -112
  12. chatterer/examples/upstage.py +89 -89
  13. chatterer/examples/web2md.py +80 -66
  14. chatterer/interactive.py +354 -354
  15. chatterer/language_model.py +536 -536
  16. chatterer/messages.py +21 -21
  17. chatterer/tools/__init__.py +46 -46
  18. chatterer/tools/caption_markdown_images.py +384 -384
  19. chatterer/tools/citation_chunking/__init__.py +3 -3
  20. chatterer/tools/citation_chunking/chunks.py +53 -53
  21. chatterer/tools/citation_chunking/citation_chunker.py +118 -118
  22. chatterer/tools/citation_chunking/citations.py +285 -285
  23. chatterer/tools/citation_chunking/prompt.py +157 -157
  24. chatterer/tools/citation_chunking/reference.py +26 -26
  25. chatterer/tools/citation_chunking/utils.py +138 -138
  26. chatterer/tools/convert_pdf_to_markdown.py +645 -625
  27. chatterer/tools/convert_to_text.py +446 -446
  28. chatterer/tools/upstage_document_parser.py +705 -705
  29. chatterer/tools/webpage_to_markdown.py +739 -739
  30. chatterer/tools/youtube.py +146 -146
  31. chatterer/utils/__init__.py +15 -15
  32. chatterer/utils/base64_image.py +350 -285
  33. chatterer/utils/bytesio.py +59 -59
  34. chatterer/utils/code_agent.py +237 -237
  35. chatterer/utils/imghdr.py +145 -148
  36. {chatterer-0.1.24.dist-info → chatterer-0.1.26.dist-info}/METADATA +390 -389
  37. chatterer-0.1.26.dist-info/RECORD +42 -0
  38. chatterer/strategies/__init__.py +0 -13
  39. chatterer/strategies/atom_of_thoughts.py +0 -975
  40. chatterer/strategies/base.py +0 -14
  41. chatterer-0.1.24.dist-info/RECORD +0 -45
  42. {chatterer-0.1.24.dist-info → chatterer-0.1.26.dist-info}/WHEEL +0 -0
  43. {chatterer-0.1.24.dist-info → chatterer-0.1.26.dist-info}/entry_points.txt +0 -0
  44. {chatterer-0.1.24.dist-info → chatterer-0.1.26.dist-info}/top_level.txt +0 -0
chatterer/examples/pw.py CHANGED
@@ -1,137 +1,143 @@
1
- import json
2
- import logging
3
- import sys
4
- from pathlib import Path
5
-
6
- from spargear import BaseArguments, RunnableArguments, SubcommandSpec
7
-
8
- from chatterer import PlayWrightBot
9
-
10
- logger = logging.getLogger(__name__)
11
-
12
-
13
- # Define the default path location relative to this script file
14
- DEFAULT_JSON_PATH = Path(__file__).resolve().parent / "session_state.json"
15
-
16
-
17
- class ReadArgs(RunnableArguments[None]):
18
- """Arguments for the 'read' subcommand."""
19
-
20
- URL: str
21
- """URL (potentially protected) to navigate to using the saved session."""
22
- jsonpath: Path = DEFAULT_JSON_PATH
23
- """Path to the session state JSON file to load."""
24
-
25
- def run(self) -> None:
26
- """
27
- Loads the session state from the specified JSON file, then navigates
28
- to a protected_url that normally requires login. If the stored session
29
- is valid, it should open without re-entering credentials.
30
-
31
- Correction: Loads the JSON content into a dict first to satisfy type hints.
32
- """
33
- url = self.URL
34
- jsonpath = self.jsonpath
35
- logger.info(f"Loading session from {jsonpath} and navigating to {url} ...")
36
-
37
- if not jsonpath.exists():
38
- logger.error(f"Session file not found at {jsonpath}")
39
- sys.exit(1)
40
-
41
- # Load the storage state from the JSON file into a dictionary
42
- logger.info(f"Reading storage state content from {jsonpath} ...")
43
- try:
44
- with open(jsonpath, "r", encoding="utf-8") as f:
45
- # This dictionary should match the 'StorageState' type expected by Playwright/chatterer
46
- storage_state_dict = json.load(f)
47
- except json.JSONDecodeError:
48
- logger.error(f"Failed to decode JSON from {jsonpath}")
49
- sys.exit(1)
50
- except Exception as e:
51
- logger.error(f"Error reading file {jsonpath}: {e}")
52
- sys.exit(1)
53
-
54
- logger.info("Launching browser with loaded session state...")
55
- with PlayWrightBot(
56
- playwright_launch_options={"headless": False},
57
- # Pass the loaded dictionary, which should match the expected 'StorageState' type
58
- playwright_persistency_options={"storage_state": storage_state_dict},
59
- ) as bot:
60
- bot.get_page(url)
61
-
62
- logger.info("Press Enter in the console when you're done checking the protected page.")
63
- input(" >> Press Enter to exit: ")
64
-
65
- logger.info("Done! Browser is now closed.")
66
-
67
-
68
- class WriteArgs(RunnableArguments[None]):
69
- """Arguments for the 'write' subcommand."""
70
-
71
- URL: str
72
- """URL to navigate to for manual login."""
73
- jsonpath: Path = DEFAULT_JSON_PATH
74
- """Path to save the session state JSON file."""
75
-
76
- def run(self) -> None:
77
- """
78
- Launches a non-headless browser and navigates to the login_url.
79
- The user can manually log in, then press Enter in the console
80
- to store the current session state into a JSON file.
81
- """
82
- url = self.URL
83
- jsonpath = self.jsonpath
84
- logger.info(f"Launching browser and navigating to {url} ... Please log in manually.")
85
-
86
- # Ensure jsonpath directory exists
87
- jsonpath.parent.mkdir(parents=True, exist_ok=True)
88
-
89
- with PlayWrightBot(playwright_launch_options={"headless": False}) as bot:
90
- bot.get_page(url)
91
-
92
- logger.info("After completing the login in the browser, press Enter here to save the session.")
93
- input(" >> Press Enter when ready: ")
94
-
95
- # get_sync_browser() returns the BrowserContext internally
96
- context = bot.get_sync_browser()
97
-
98
- # Save the current session (cookies, localStorage) to a JSON file
99
- logger.info(f"Saving storage state to {jsonpath} ...")
100
- context.storage_state(path=jsonpath) # Pass Path object directly
101
-
102
- logger.info("Done! Browser is now closed.")
103
-
104
-
105
- class Arguments(BaseArguments):
106
- """
107
- A simple CLI tool for saving and using Playwright sessions via storage_state.
108
- Uses spargear for declarative argument parsing.
109
- """
110
-
111
- read: SubcommandSpec[ReadArgs] = SubcommandSpec(
112
- name="read",
113
- argument_class=ReadArgs,
114
- help="Use a saved session to view a protected page.",
115
- description="Loads session state from the specified JSON file and navigates to the URL.",
116
- )
117
- write: SubcommandSpec[WriteArgs] = SubcommandSpec(
118
- name="write",
119
- argument_class=WriteArgs,
120
- help="Save a new session by manually logging in.",
121
- description="Launches a browser to the specified URL. Log in manually, then press Enter to save session state.",
122
- )
123
-
124
- def run(self) -> None:
125
- """Parses arguments using spargear and executes the corresponding command."""
126
- if isinstance(last_subcommand := self.last_command, RunnableArguments):
127
- last_subcommand.run()
128
- else:
129
- self.get_parser().print_help()
130
-
131
-
132
- def main() -> None:
133
- Arguments().run()
134
-
135
-
136
- if __name__ == "__main__":
137
- main()
1
+ import json
2
+ import logging
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ from spargear import ArgumentSpec, BaseArguments, RunnableArguments, SubcommandSpec
7
+
8
+ from chatterer import PlayWrightBot
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ def generate_json_path() -> Path:
14
+ return Path("session_state.json").resolve()
15
+
16
+
17
+ class ReadArgs(RunnableArguments[None]):
18
+ """Arguments for the 'read' subcommand."""
19
+
20
+ URL: str
21
+ """URL (potentially protected) to navigate to using the saved session."""
22
+ json: ArgumentSpec[Path] = ArgumentSpec(
23
+ ["--json", "-j"],
24
+ default_factory=generate_json_path,
25
+ help="Path to the session state JSON file to load.",
26
+ )
27
+
28
+ def run(self) -> None:
29
+ """
30
+ Loads the session state from the specified JSON file, then navigates
31
+ to a protected_url that normally requires login. If the stored session
32
+ is valid, it should open without re-entering credentials.
33
+
34
+ Correction: Loads the JSON content into a dict first to satisfy type hints.
35
+ """
36
+ url = self.URL
37
+ jsonpath = self.json.unwrap()
38
+ logger.info(f"Loading session from {jsonpath} and navigating to {url} ...")
39
+
40
+ if not jsonpath.exists():
41
+ logger.error(f"Session file not found at {jsonpath}")
42
+ sys.exit(1)
43
+
44
+ # Load the storage state from the JSON file into a dictionary
45
+ logger.info(f"Reading storage state content from {jsonpath} ...")
46
+ try:
47
+ with open(jsonpath, "r", encoding="utf-8") as f:
48
+ # This dictionary should match the 'StorageState' type expected by Playwright/chatterer
49
+ storage_state_dict = json.load(f)
50
+ except json.JSONDecodeError:
51
+ logger.error(f"Failed to decode JSON from {jsonpath}")
52
+ sys.exit(1)
53
+ except Exception as e:
54
+ logger.error(f"Error reading file {jsonpath}: {e}")
55
+ sys.exit(1)
56
+
57
+ logger.info("Launching browser with loaded session state...")
58
+ with PlayWrightBot(
59
+ playwright_launch_options={"headless": False},
60
+ # Pass the loaded dictionary, which should match the expected 'StorageState' type
61
+ playwright_persistency_options={"storage_state": storage_state_dict},
62
+ ) as bot:
63
+ bot.get_page(url)
64
+
65
+ logger.info("Press Enter in the console when you're done checking the protected page.")
66
+ input(" >> Press Enter to exit: ")
67
+
68
+ logger.info("Done! Browser is now closed.")
69
+
70
+
71
+ class WriteArgs(RunnableArguments[None]):
72
+ """Arguments for the 'write' subcommand."""
73
+
74
+ URL: str
75
+ """URL to navigate to for manual login."""
76
+ json: ArgumentSpec[Path] = ArgumentSpec(
77
+ ["--json", "-j"],
78
+ default_factory=generate_json_path,
79
+ help="Path to save the session state JSON file.",
80
+ )
81
+
82
+ def run(self) -> None:
83
+ """
84
+ Launches a non-headless browser and navigates to the login_url.
85
+ The user can manually log in, then press Enter in the console
86
+ to store the current session state into a JSON file.
87
+ """
88
+ url = self.URL
89
+ jsonpath = self.json.unwrap()
90
+ logger.info(f"Launching browser and navigating to {url} ... Please log in manually.")
91
+
92
+ # Ensure jsonpath directory exists
93
+ jsonpath.parent.mkdir(parents=True, exist_ok=True)
94
+
95
+ with PlayWrightBot(playwright_launch_options={"headless": False}) as bot:
96
+ bot.get_page(url)
97
+
98
+ logger.info("After completing the login in the browser, press Enter here to save the session.")
99
+ input(" >> Press Enter when ready: ")
100
+
101
+ # get_sync_browser() returns the BrowserContext internally
102
+ context = bot.get_sync_browser()
103
+
104
+ # Save the current session (cookies, localStorage) to a JSON file
105
+ logger.info(f"Saving storage state to {jsonpath} ...")
106
+ context.storage_state(path=jsonpath) # Pass Path object directly
107
+
108
+ logger.info("Done! Browser is now closed.")
109
+
110
+
111
+ class Arguments(BaseArguments):
112
+ """
113
+ A simple CLI tool for saving and using Playwright sessions via storage_state.
114
+ Uses spargear for declarative argument parsing.
115
+ """
116
+
117
+ read: SubcommandSpec[ReadArgs] = SubcommandSpec(
118
+ name="read",
119
+ argument_class=ReadArgs,
120
+ help="Use a saved session to view a protected page.",
121
+ description="Loads session state from the specified JSON file and navigates to the URL.",
122
+ )
123
+ write: SubcommandSpec[WriteArgs] = SubcommandSpec(
124
+ name="write",
125
+ argument_class=WriteArgs,
126
+ help="Save a new session by manually logging in.",
127
+ description="Launches a browser to the specified URL. Log in manually, then press Enter to save session state.",
128
+ )
129
+
130
+ def run(self) -> None:
131
+ """Parses arguments using spargear and executes the corresponding command."""
132
+ if isinstance(last_subcommand := self.last_command, RunnableArguments):
133
+ last_subcommand.run()
134
+ else:
135
+ self.get_parser().print_help()
136
+
137
+
138
+ def main() -> None:
139
+ Arguments().run()
140
+
141
+
142
+ if __name__ == "__main__":
143
+ main()
@@ -1,55 +1,56 @@
1
- import logging
2
- from pathlib import Path
3
- from typing import Optional
4
-
5
- from spargear import RunnableArguments
6
-
7
- from chatterer import CodeSnippets
8
-
9
- logger = logging.getLogger(__name__)
10
-
11
-
12
- class Arguments(RunnableArguments[CodeSnippets]):
13
- PATH_OR_PACKAGE_NAME: str
14
- """Path to the package or file from which to extract code snippets."""
15
- output: Optional[str] = None
16
- """Output path for the extracted code snippets. If not provided, defaults to a file with the same name as the input."""
17
- ban_file_patterns: list[str] = [".venv/*", Path(__file__).relative_to(Path.cwd()).as_posix()]
18
- """List of file patterns to ignore."""
19
- glob_patterns: list[str] = ["*.py"]
20
- """List of glob patterns to include."""
21
- case_sensitive: bool = False
22
- """Enable case-sensitive matching for glob patterns."""
23
- prevent_save_file: bool = False
24
- """Prevent saving the extracted code snippets to a file."""
25
-
26
- def run(self) -> CodeSnippets:
27
- if not self.prevent_save_file:
28
- if not self.output:
29
- output = Path(__file__).with_suffix(".txt")
30
- else:
31
- output = Path(self.output)
32
- else:
33
- output = None
34
-
35
- cs = CodeSnippets.from_path_or_pkgname(
36
- path_or_pkgname=self.PATH_OR_PACKAGE_NAME,
37
- ban_file_patterns=self.ban_file_patterns,
38
- glob_patterns=self.glob_patterns,
39
- case_sensitive=self.case_sensitive,
40
- )
41
- if output is not None:
42
- output.parent.mkdir(parents=True, exist_ok=True)
43
- output.write_text(cs.snippets_text, encoding="utf-8")
44
- logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}` and saved to `{output}`.")
45
- else:
46
- logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}`.")
47
- return cs
48
-
49
-
50
- def main() -> None:
51
- Arguments().run()
52
-
53
-
54
- if __name__ == "__main__":
55
- main()
1
+ import logging
2
+ from datetime import datetime
3
+ from pathlib import Path
4
+ from typing import Optional
5
+
6
+ from spargear import RunnableArguments
7
+
8
+ from chatterer import CodeSnippets
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ class Arguments(RunnableArguments[CodeSnippets]):
14
+ PATH_OR_PACKAGE_NAME: str
15
+ """Path to the package or file from which to extract code snippets."""
16
+ output: Optional[str] = None
17
+ """Output path for the extracted code snippets. If not provided, defaults to a file with the current timestamp."""
18
+ ban_file_patterns: list[str] = [".venv/*", Path(__file__).relative_to(Path.cwd()).as_posix()]
19
+ """List of file patterns to ignore."""
20
+ glob_patterns: list[str] = ["*.py"]
21
+ """List of glob patterns to include."""
22
+ case_sensitive: bool = False
23
+ """Enable case-sensitive matching for glob patterns."""
24
+ prevent_save_file: bool = False
25
+ """Prevent saving the extracted code snippets to a file."""
26
+
27
+ def run(self) -> CodeSnippets:
28
+ if not self.prevent_save_file:
29
+ if not self.output:
30
+ output = Path(datetime.now().strftime("%Y%m%d_%H%M%S") + "_snippets.txt")
31
+ else:
32
+ output = Path(self.output)
33
+ else:
34
+ output = None
35
+
36
+ cs = CodeSnippets.from_path_or_pkgname(
37
+ path_or_pkgname=self.PATH_OR_PACKAGE_NAME,
38
+ ban_file_patterns=self.ban_file_patterns,
39
+ glob_patterns=self.glob_patterns,
40
+ case_sensitive=self.case_sensitive,
41
+ )
42
+ if output is not None:
43
+ output.parent.mkdir(parents=True, exist_ok=True)
44
+ output.write_text(cs.snippets_text, encoding="utf-8")
45
+ logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}` and saved to `{output}`.")
46
+ else:
47
+ logger.info(f"Extracted code snippets from `{self.PATH_OR_PACKAGE_NAME}`.")
48
+ return cs
49
+
50
+
51
+ def main() -> None:
52
+ Arguments().run()
53
+
54
+
55
+ if __name__ == "__main__":
56
+ main()