kash-shell 0.3.9__py3-none-any.whl → 0.3.10__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 (135) hide show
  1. kash/actions/__init__.py +4 -4
  2. kash/actions/core/markdownify.py +5 -2
  3. kash/actions/core/readability.py +5 -2
  4. kash/actions/core/render_as_html.py +18 -0
  5. kash/actions/core/webpage_config.py +12 -4
  6. kash/commands/__init__.py +8 -20
  7. kash/commands/base/basic_file_commands.py +15 -0
  8. kash/commands/base/debug_commands.py +13 -0
  9. kash/commands/base/general_commands.py +21 -16
  10. kash/commands/base/logs_commands.py +4 -2
  11. kash/commands/base/model_commands.py +8 -8
  12. kash/commands/base/search_command.py +3 -2
  13. kash/commands/base/show_command.py +5 -3
  14. kash/commands/extras/parse_uv_lock.py +186 -0
  15. kash/commands/help/doc_commands.py +2 -31
  16. kash/commands/help/welcome.py +33 -0
  17. kash/commands/workspace/selection_commands.py +11 -6
  18. kash/commands/workspace/workspace_commands.py +18 -15
  19. kash/config/colors.py +2 -0
  20. kash/config/env_settings.py +14 -1
  21. kash/config/init.py +2 -2
  22. kash/config/logger.py +59 -56
  23. kash/config/logger_basic.py +3 -3
  24. kash/config/settings.py +116 -57
  25. kash/config/setup.py +28 -12
  26. kash/config/text_styles.py +3 -13
  27. kash/docs/load_api_docs.py +2 -1
  28. kash/docs/markdown/topics/a3_getting_started.md +3 -2
  29. kash/{concepts → embeddings}/text_similarity.py +2 -2
  30. kash/exec/__init__.py +20 -3
  31. kash/exec/action_decorators.py +18 -4
  32. kash/exec/action_exec.py +41 -23
  33. kash/exec/action_registry.py +13 -48
  34. kash/exec/command_registry.py +2 -1
  35. kash/exec/fetch_url_metadata.py +4 -6
  36. kash/exec/importing.py +56 -0
  37. kash/exec/llm_transforms.py +6 -7
  38. kash/exec/precondition_registry.py +2 -1
  39. kash/exec/preconditions.py +16 -1
  40. kash/exec/shell_callable_action.py +33 -19
  41. kash/file_storage/file_store.py +23 -10
  42. kash/file_storage/item_file_format.py +5 -2
  43. kash/file_storage/metadata_dirs.py +11 -2
  44. kash/help/assistant.py +1 -1
  45. kash/help/assistant_instructions.py +2 -1
  46. kash/help/help_embeddings.py +2 -2
  47. kash/help/help_printing.py +7 -11
  48. kash/llm_utils/clean_headings.py +1 -1
  49. kash/llm_utils/llm_api_keys.py +4 -4
  50. kash/llm_utils/llm_features.py +68 -0
  51. kash/llm_utils/llm_messages.py +1 -2
  52. kash/llm_utils/llm_names.py +1 -1
  53. kash/llm_utils/llms.py +8 -3
  54. kash/local_server/__init__.py +5 -2
  55. kash/local_server/local_server.py +8 -5
  56. kash/local_server/local_server_commands.py +2 -2
  57. kash/local_server/local_url_formatters.py +1 -1
  58. kash/mcp/__init__.py +5 -2
  59. kash/mcp/mcp_cli.py +5 -5
  60. kash/mcp/mcp_server_commands.py +5 -5
  61. kash/mcp/mcp_server_routes.py +5 -5
  62. kash/mcp/mcp_server_sse.py +4 -2
  63. kash/media_base/media_cache.py +8 -8
  64. kash/media_base/media_services.py +1 -1
  65. kash/media_base/media_tools.py +6 -6
  66. kash/media_base/services/local_file_media.py +2 -2
  67. kash/media_base/{speech_transcription.py → transcription_deepgram.py} +25 -110
  68. kash/media_base/transcription_format.py +73 -0
  69. kash/media_base/transcription_whisper.py +38 -0
  70. kash/model/__init__.py +73 -5
  71. kash/model/actions_model.py +38 -4
  72. kash/model/concept_model.py +30 -0
  73. kash/model/items_model.py +44 -7
  74. kash/model/params_model.py +24 -0
  75. kash/shell/completions/completion_scoring.py +37 -5
  76. kash/shell/output/kerm_codes.py +1 -2
  77. kash/shell/output/shell_formatting.py +14 -4
  78. kash/shell/shell_main.py +2 -2
  79. kash/shell/utils/exception_printing.py +6 -0
  80. kash/shell/utils/native_utils.py +26 -20
  81. kash/text_handling/custom_sliding_transforms.py +12 -4
  82. kash/text_handling/doc_normalization.py +6 -2
  83. kash/text_handling/markdown_render.py +117 -0
  84. kash/text_handling/markdown_utils.py +204 -0
  85. kash/utils/common/import_utils.py +12 -3
  86. kash/utils/common/type_utils.py +0 -29
  87. kash/utils/common/url.py +27 -3
  88. kash/utils/errors.py +6 -0
  89. kash/utils/file_utils/file_formats.py +2 -2
  90. kash/utils/file_utils/file_formats_model.py +3 -0
  91. kash/web_content/dir_store.py +1 -2
  92. kash/web_content/file_cache_utils.py +37 -10
  93. kash/web_content/file_processing.py +68 -0
  94. kash/web_content/local_file_cache.py +12 -9
  95. kash/web_content/web_extract.py +8 -3
  96. kash/web_content/web_fetch.py +12 -4
  97. kash/web_gen/tabbed_webpage.py +5 -2
  98. kash/web_gen/templates/base_styles.css.jinja +120 -14
  99. kash/web_gen/templates/base_webpage.html.jinja +60 -13
  100. kash/web_gen/templates/content_styles.css.jinja +4 -2
  101. kash/web_gen/templates/item_view.html.jinja +2 -2
  102. kash/web_gen/templates/tabbed_webpage.html.jinja +1 -2
  103. kash/workspaces/__init__.py +15 -2
  104. kash/workspaces/selections.py +18 -3
  105. kash/workspaces/source_items.py +0 -1
  106. kash/workspaces/workspaces.py +5 -11
  107. kash/xonsh_custom/command_nl_utils.py +40 -19
  108. kash/xonsh_custom/custom_shell.py +43 -11
  109. kash/xonsh_custom/customize_prompt.py +39 -21
  110. kash/xonsh_custom/load_into_xonsh.py +22 -25
  111. kash/xonsh_custom/shell_load_commands.py +2 -2
  112. kash/xonsh_custom/xonsh_completers.py +2 -249
  113. kash/xonsh_custom/xonsh_keybindings.py +282 -0
  114. kash/xonsh_custom/xonsh_modern_tools.py +3 -3
  115. kash/xontrib/kash_extension.py +5 -6
  116. {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/METADATA +8 -6
  117. {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/RECORD +122 -123
  118. kash/concepts/concept_formats.py +0 -23
  119. kash/shell/clideps/api_keys.py +0 -100
  120. kash/shell/clideps/dotenv_setup.py +0 -115
  121. kash/shell/clideps/dotenv_utils.py +0 -98
  122. kash/shell/clideps/pkg_deps.py +0 -257
  123. kash/shell/clideps/platforms.py +0 -11
  124. kash/shell/clideps/terminal_features.py +0 -56
  125. kash/shell/utils/osc_utils.py +0 -95
  126. kash/shell/utils/terminal_images.py +0 -133
  127. kash/text_handling/markdown_util.py +0 -167
  128. kash/utils/common/atomic_var.py +0 -171
  129. kash/utils/common/string_replace.py +0 -93
  130. kash/utils/common/string_template.py +0 -101
  131. /kash/{concepts → embeddings}/cosine.py +0 -0
  132. /kash/{concepts → embeddings}/embeddings.py +0 -0
  133. {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/WHEEL +0 -0
  134. {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/entry_points.txt +0 -0
  135. {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/licenses/LICENSE +0 -0
@@ -27,8 +27,9 @@ def select(
27
27
  previous: bool = False,
28
28
  next: bool = False,
29
29
  pop: bool = False,
30
- clear: bool = False,
30
+ clear_all: bool = False,
31
31
  clear_future: bool = False,
32
+ refresh: bool = False,
32
33
  ) -> ShellResult:
33
34
  """
34
35
  Set or show the current selection.
@@ -47,12 +48,13 @@ def select(
47
48
  :param previous: Move back in the selection history to the previous selection.
48
49
  :param next: Move forward in the selection history to the next selection.
49
50
  :param pop: Pop the current selection from the history.
50
- :param clear: Clear the full selection history.
51
+ :param clear_all: Clear the full selection history.
51
52
  :param clear_future: Clear all selections from history after the current one.
53
+ :param refresh: Refresh the current selection to drop any paths that no longer exist.
52
54
  """
53
55
  ws = current_ws()
54
56
 
55
- # TODO: It would be nice to be able to read stdin from a pipe but this isn't working rn.
57
+ # FIXME: It would be nice to be able to read stdin from a pipe but this isn't working rn.
56
58
  # You could then run `... | select --stdin` to select the piped input.
57
59
  # Globally we have THREAD_SUBPROCS=False to avoid hard-to-interrupt subprocesses.
58
60
  # But xonsh seems to hang with stdin unless we modify the spec to be threadable?
@@ -61,7 +63,7 @@ def select(
61
63
  # if stdin:
62
64
  # paths = tuple(sys.stdin.read().splitlines())
63
65
 
64
- exclusive_flags = [history, last, back, forward, previous, next, pop, clear, clear_future]
66
+ exclusive_flags = [history, last, back, forward, previous, next, pop, clear_all, clear_future]
65
67
  if sum(bool(f) for f in exclusive_flags) > 1:
66
68
  raise InvalidInput("Cannot combine multiple flags")
67
69
  if paths and any(exclusive_flags):
@@ -96,12 +98,15 @@ def select(
96
98
  elif pop:
97
99
  ws.selections.pop()
98
100
  return ShellResult(show_selection=True)
99
- elif clear:
100
- ws.selections.clear()
101
+ elif clear_all:
102
+ ws.selections.clear_all()
101
103
  return ShellResult(show_selection=True)
102
104
  elif clear_future:
103
105
  ws.selections.clear_future()
104
106
  return ShellResult(show_selection=True)
107
+ elif refresh:
108
+ ws.selections.refresh_current(ws.base_dir)
109
+ return ShellResult(show_selection=True)
105
110
  else:
106
111
  return ShellResult(show_selection=True)
107
112
 
@@ -15,7 +15,6 @@ from kash.config.text_styles import (
15
15
  EMOJI_WARN,
16
16
  STYLE_EMPH,
17
17
  STYLE_HINT,
18
- format_success_emoji,
19
18
  )
20
19
  from kash.exec import (
21
20
  assemble_path_args,
@@ -36,7 +35,11 @@ from kash.model.items_model import Item, ItemType
36
35
  from kash.model.params_model import GLOBAL_PARAMS
37
36
  from kash.model.paths_model import StorePath, fmt_store_path
38
37
  from kash.shell.input.param_inputs import input_param_name, input_param_value
39
- from kash.shell.output.shell_formatting import format_name_and_description, format_name_and_value
38
+ from kash.shell.output.shell_formatting import (
39
+ format_name_and_description,
40
+ format_name_and_value,
41
+ format_success_emoji,
42
+ )
40
43
  from kash.shell.output.shell_output import (
41
44
  PrintHooks,
42
45
  Wrap,
@@ -170,14 +173,15 @@ def cache_media(*urls: str) -> None:
170
173
 
171
174
 
172
175
  @kash_command
173
- def cache_content(*urls_or_paths: str) -> None:
176
+ def cache_content(*urls_or_paths: str, refetch: bool = False) -> None:
174
177
  """
175
178
  Cache the given file in the content cache. Downloads any URL or copies a local file.
176
179
  """
180
+ expiration_sec = 0 if refetch else None
177
181
  PrintHooks.spacer()
178
182
  for url_or_path in urls_or_paths:
179
183
  locator = resolve_locator_arg(url_or_path)
180
- cache_path, was_cached = cache_file(locator)
184
+ cache_path, was_cached = cache_file(locator, expiration_sec=expiration_sec)
181
185
  cache_str = " (already cached)" if was_cached else ""
182
186
  cprint(f"{fmt_loc(url_or_path)}{cache_str}:", style=STYLE_EMPH, text_wrap=Wrap.NONE)
183
187
  cprint(f"{cache_path}", text_wrap=Wrap.INDENT_ONLY)
@@ -185,10 +189,13 @@ def cache_content(*urls_or_paths: str) -> None:
185
189
 
186
190
 
187
191
  @kash_command
188
- def download(*urls_or_paths: str) -> None:
192
+ def download(*urls_or_paths: str, refetch: bool = False) -> None:
189
193
  """
190
- Download a URL or resource. Inputs can be URLs or paths to URL resources.
194
+ Download a URL or resource. Uses cached content if available, unless `refetch` is true.
195
+ Inputs can be URLs or paths to URL resources.
191
196
  """
197
+ expiration_sec = 0 if refetch else None
198
+
192
199
  # TODO: Add option to include frontmatter metadata for text files.
193
200
  ws = current_ws()
194
201
  for url_or_path in urls_or_paths:
@@ -211,7 +218,7 @@ def download(*urls_or_paths: str) -> None:
211
218
  media_tools.cache_media(url)
212
219
  else:
213
220
  log.message("Will cache file and save to workspace: %s", fmt_loc(url))
214
- cache_path, _was_cached = cache_file(url)
221
+ cache_path, _was_cached = cache_file(url, expiration_sec=expiration_sec)
215
222
  item = Item.from_external_path(cache_path, item_type=ItemType.resource)
216
223
  store_path = ws.save(item)
217
224
 
@@ -402,12 +409,10 @@ def set_params(*key_vals: str) -> None:
402
409
 
403
410
 
404
411
  @kash_command
405
- def list_params(full: bool = False) -> None:
412
+ def params(full: bool = False) -> None:
406
413
  """
407
- Show or set currently set of workspace parameters, which are settings that may be used
414
+ List currently set workspace parameters, which are settings that may be used
408
415
  by commands and actions or to override default parameters.
409
-
410
- Run with no args to interactively set parameters.
411
416
  """
412
417
  ws: Workspace = current_ws()
413
418
  settable_params = GLOBAL_PARAMS
@@ -461,9 +466,7 @@ def import_item(
461
466
 
462
467
 
463
468
  @kash_command
464
- def fetch_metadata(
465
- *files_or_urls: str, no_cache: bool = False, refetch: bool = False
466
- ) -> ShellResult:
469
+ def fetch_metadata(*files_or_urls: str, refetch: bool = False) -> ShellResult:
467
470
  """
468
471
  Fetch metadata for the given URLs or resources. Imports new URLs and saves back
469
472
  the fetched metadata for existing resources.
@@ -483,7 +486,7 @@ def fetch_metadata(
483
486
  try:
484
487
  if isinstance(locator, Path):
485
488
  raise InvalidInput()
486
- fetched_item = fetch_url_metadata(locator, use_cache=not no_cache, refetch=refetch)
489
+ fetched_item = fetch_url_metadata(locator, refetch=refetch)
487
490
  store_paths.append(fetched_item.store_path)
488
491
  except InvalidInput:
489
492
  log.warning("Not a URL or URL resource, will not fetch metadata: %s", fmt_loc(locator))
kash/config/colors.py CHANGED
@@ -134,8 +134,10 @@ web_light_translucent = SimpleNamespace(
134
134
  primary_light=hsl_to_hex("hsl(188, 40%, 62%)"),
135
135
  secondary=hsl_to_hex("hsl(188, 12%, 28%)"),
136
136
  bg=hsl_to_hex("hsla(44, 6%, 100%, 0.75)"),
137
+ bg_solid=hsl_to_hex("hsla(44, 6%, 100%, 1)"),
137
138
  bg_header=hsl_to_hex("hsla(188, 42%, 70%, 0.2)"),
138
139
  bg_alt=hsl_to_hex("hsla(44, 28%, 90%, 0.3)"),
140
+ bg_alt_solid=hsl_to_hex("hsla(44, 28%, 97%, 1)"),
139
141
  text=hsl_to_hex("hsl(188, 39%, 11%)"),
140
142
  border=hsl_to_hex("hsl(188, 8%, 50%)"),
141
143
  border_hint=hsl_to_hex("hsla(188, 8%, 72%, 0.7)"),
@@ -28,6 +28,12 @@ class KashEnv(str, Enum):
28
28
  KASH_MCP_WS = "KASH_MCP_WS"
29
29
  """The directory for the workspace for MCP servers."""
30
30
 
31
+ KASH_SHOW_TRACEBACK = "KASH_SHOW_TRACEBACK"
32
+ """Whether to show tracebacks on actions and commands in the shell."""
33
+
34
+ KASH_USER_AGENT = "KASH_USER_AGENT"
35
+ """The user agent to use for HTTP requests."""
36
+
31
37
  @overload
32
38
  def read_str(self) -> str | None: ...
33
39
 
@@ -42,7 +48,7 @@ class KashEnv(str, Enum):
42
48
  return os.environ.get(self.value, default)
43
49
 
44
50
  @overload
45
- def read_path(self, default: None) -> None: ...
51
+ def read_path(self) -> Path | None: ...
46
52
 
47
53
  @overload
48
54
  def read_path(self, default: Path) -> Path: ...
@@ -57,3 +63,10 @@ class KashEnv(str, Enum):
57
63
  return Path(value).expanduser().resolve()
58
64
  else:
59
65
  return default.expanduser().resolve() if default else None
66
+
67
+ def read_bool(self, default: bool = False) -> bool:
68
+ """
69
+ Get the value of the environment variable as a boolean.
70
+ """
71
+ value = str(os.environ.get(self.value, default) or "").lower()
72
+ return bool(value and value != "0" and value != "false" and value != "no")
kash/config/init.py CHANGED
@@ -9,10 +9,10 @@ def kash_reload_all() -> tuple[dict[str, Callable], dict[str, type["Action"]]]:
9
9
  """
10
10
  Import all kash modules that define actions and commands.
11
11
  """
12
- from kash.exec.action_registry import reload_all_action_classes
12
+ from kash.exec.action_registry import refresh_action_classes
13
13
  from kash.exec.command_registry import get_all_commands
14
14
 
15
15
  commands = get_all_commands()
16
- actions = reload_all_action_classes()
16
+ actions = refresh_action_classes()
17
17
 
18
18
  return commands, actions
kash/config/logger.py CHANGED
@@ -2,7 +2,6 @@ import contextvars
2
2
  import logging
3
3
  import os
4
4
  import re
5
- import threading
6
5
  from collections.abc import Generator
7
6
  from contextlib import contextmanager
8
7
  from dataclasses import dataclass
@@ -17,7 +16,7 @@ from rich._null_file import NULL_FILE
17
16
  from rich.console import Console
18
17
  from rich.logging import RichHandler
19
18
  from rich.theme import Theme
20
- from strif import atomic_output_file, new_timestamped_uid
19
+ from strif import AtomicVar, atomic_output_file, new_timestamped_uid
21
20
  from typing_extensions import override
22
21
 
23
22
  import kash.config.suppress_warnings # noqa: F401
@@ -38,55 +37,41 @@ from kash.utils.common.stack_traces import current_stack_traces
38
37
  from kash.utils.common.task_stack import task_stack_prefix_str
39
38
 
40
39
 
41
- @dataclass(frozen=True)
40
+ @dataclass
42
41
  class LogSettings:
43
42
  log_console_level: LogLevel
44
43
  log_file_level: LogLevel
45
- # Always the same global log directory.
44
+
46
45
  global_log_dir: Path
46
+ """Global directory for log files."""
47
47
 
48
48
  # These directories can change based on the current workspace:
49
49
  log_dir: Path
50
- log_objects_dir: Path
51
- log_file_path: Path
52
-
53
-
54
- _log_dir = get_system_logs_dir()
55
- """
56
- Parent of the "logs" directory. Initially the global kash workspace.
57
- """
58
-
59
-
60
- LOG_NAME_GLOBAL = "global"
50
+ """Parent of the "logs" directory. Initially the global kash workspace."""
61
51
 
62
- _log_name = LOG_NAME_GLOBAL
63
- """
64
- Name of the log file. By default the workspace name or "global" if
65
- for the global workspace.
66
- """
52
+ log_name: str
53
+ """Name of the log file. Typically the workspace name or "workspace" if for the global workspace."""
67
54
 
68
- _log_lock = threading.RLock()
55
+ log_objects_dir: Path
56
+ log_file_path: Path
69
57
 
70
58
 
71
- def make_valid_log_name(name: str) -> str:
72
- name = str(name).strip().rstrip("/").removesuffix(".log")
73
- name = re.sub(r"[^\w-]", "_", name)
74
- return name
59
+ LOG_NAME_GLOBAL = "workspace"
75
60
 
76
61
 
77
62
  def _read_log_settings() -> LogSettings:
78
- global _log_dir, _log_name
79
63
  return LogSettings(
80
64
  log_console_level=global_settings().console_log_level,
81
65
  log_file_level=global_settings().file_log_level,
82
66
  global_log_dir=get_system_logs_dir(),
83
- log_dir=_log_dir,
84
- log_objects_dir=_log_dir / "objects" / _log_name,
85
- log_file_path=_log_dir / f"{_log_name}.log",
67
+ log_dir=get_system_logs_dir(),
68
+ log_name=LOG_NAME_GLOBAL,
69
+ log_objects_dir=get_system_logs_dir() / "objects" / LOG_NAME_GLOBAL,
70
+ log_file_path=get_system_logs_dir() / f"{LOG_NAME_GLOBAL}.log",
86
71
  )
87
72
 
88
73
 
89
- _log_settings: LogSettings = _read_log_settings()
74
+ _log_settings: AtomicVar[LogSettings] = AtomicVar(_read_log_settings())
90
75
 
91
76
  _setup_done = False
92
77
 
@@ -95,19 +80,13 @@ def get_log_settings() -> LogSettings:
95
80
  """
96
81
  Currently active log settings.
97
82
  """
98
- return _log_settings
83
+ return _log_settings.copy()
99
84
 
100
85
 
101
- def reset_log_root(log_root: Path | None = None, log_name: str | None = None):
102
- """
103
- Reset the logging root or log name, if it has changed. None means no change
104
- and global default values.
105
- """
106
- global _log_lock, _log_base, _log_name
107
- with _log_lock:
108
- _log_base = log_root or get_system_logs_dir()
109
- _log_name = make_valid_log_name(log_name or LOG_NAME_GLOBAL)
110
- reload_rich_logging_setup()
86
+ def make_valid_log_name(name: str) -> str:
87
+ name = str(name).strip().rstrip("/").removesuffix(".log")
88
+ name = re.sub(r"[^\w-]", "_", name)
89
+ return name
111
90
 
112
91
 
113
92
  console_context_var: contextvars.ContextVar[Console | None] = contextvars.ContextVar(
@@ -169,6 +148,30 @@ _file_handler: logging.FileHandler
169
148
  _console_handler: logging.Handler
170
149
 
171
150
 
151
+ def reset_rich_logging(
152
+ log_root: Path | None = None,
153
+ log_name: str | None = None,
154
+ log_path: Path | None = None,
155
+ ):
156
+ """
157
+ Set or reset the logging root or log name, if it has changed. None means no change
158
+ and global default values. `log_name` is the name of the log, excluding
159
+ the `.log` extension. If `log_path` is provided, it will be used to infer
160
+ the log root and name.
161
+ """
162
+ if log_path:
163
+ if not log_path.parent.exists():
164
+ log_path.parent.mkdir(parents=True, exist_ok=True)
165
+ log_root = log_path.parent
166
+ log_name = log_path.name
167
+
168
+ global _log_settings
169
+ with _log_settings.updates() as settings:
170
+ settings.log_dir = log_root or get_system_logs_dir()
171
+ settings.log_name = make_valid_log_name(log_name or LOG_NAME_GLOBAL)
172
+ reload_rich_logging_setup()
173
+
174
+
172
175
  def reload_rich_logging_setup():
173
176
  """
174
177
  Set up or reset logging setup. This is for rich/formatted console logging and
@@ -176,12 +179,12 @@ def reload_rich_logging_setup():
176
179
  Call at initial run and again if log directory changes. Replaces all previous
177
180
  loggers and handlers. Can be called to reset with different settings.
178
181
  """
179
- global _log_lock, _log_settings, _setup_done
180
- with _log_lock:
182
+ global _setup_done, _log_settings
183
+ with _log_settings.lock:
181
184
  new_log_settings = _read_log_settings()
182
- if not _setup_done or new_log_settings != _log_settings:
185
+ if not _setup_done or new_log_settings != _log_settings.value:
183
186
  _do_logging_setup(new_log_settings)
184
- _log_settings = new_log_settings
187
+ _log_settings.set(new_log_settings)
185
188
  _setup_done = True
186
189
 
187
190
  # get_console().print(
@@ -190,6 +193,15 @@ def reload_rich_logging_setup():
190
193
  # )
191
194
 
192
195
 
196
+ @cache
197
+ def _init_rich_logging():
198
+ rich.reconfigure(theme=get_theme(), highlighter=get_highlighter())
199
+
200
+ logging.setLoggerClass(CustomLogger)
201
+
202
+ reload_rich_logging_setup()
203
+
204
+
193
205
  def _do_logging_setup(log_settings: LogSettings):
194
206
  from kash.config.suppress_warnings import demote_warnings, filter_warnings
195
207
 
@@ -319,7 +331,7 @@ class CustomLogger(logging.Logger):
319
331
  filename = (
320
332
  f"{prefix}{slugify_snake(description)}.{new_timestamped_uid()}.{file_ext.lstrip('.')}"
321
333
  )
322
- path = _log_settings.log_objects_dir / filename
334
+ path = _log_settings.copy().log_objects_dir / filename
323
335
  with atomic_output_file(path, make_parents=True) as tmp_filename:
324
336
  if isinstance(obj, bytes):
325
337
  with open(tmp_filename, "wb") as f:
@@ -346,7 +358,7 @@ def get_logger(name: str) -> CustomLogger:
346
358
  Get a logger that's compatible with system logging but has our additional custom
347
359
  methods.
348
360
  """
349
- init_rich_logging()
361
+ _init_rich_logging()
350
362
  logger = logging.getLogger(name)
351
363
  # print("Logger is", logger)
352
364
  return cast(CustomLogger, logger)
@@ -354,12 +366,3 @@ def get_logger(name: str) -> CustomLogger:
354
366
 
355
367
  def get_log_file_stream():
356
368
  return _file_handler.stream
357
-
358
-
359
- @cache
360
- def init_rich_logging():
361
- rich.reconfigure(theme=get_theme(), highlighter=get_highlighter())
362
-
363
- logging.setLoggerClass(CustomLogger)
364
-
365
- reload_rich_logging_setup()
@@ -30,7 +30,7 @@ def basic_stderr_handler(level: LogLevel) -> logging.StreamHandler:
30
30
  return handler
31
31
 
32
32
 
33
- def basic_logging_setup(file_log_path: Path | None, level: LogLevel):
33
+ def basic_logging_setup(log_path: Path | None, level: LogLevel):
34
34
  """
35
35
  Set up basic logging to a file and to stderr.
36
36
  """
@@ -38,8 +38,8 @@ def basic_logging_setup(file_log_path: Path | None, level: LogLevel):
38
38
  for h in root_logger.handlers[:]:
39
39
  root_logger.removeHandler(h)
40
40
 
41
- if file_log_path:
42
- file_handler: FileHandler = basic_file_handler(file_log_path, level)
41
+ if log_path:
42
+ file_handler: FileHandler = basic_file_handler(log_path, level)
43
43
  root_logger.addHandler(file_handler)
44
44
 
45
45
  stderr_handler = basic_stderr_handler(level)
kash/config/settings.py CHANGED
@@ -5,15 +5,16 @@ from logging import DEBUG, ERROR, INFO, WARNING
5
5
  from pathlib import Path
6
6
 
7
7
  from pydantic.dataclasses import dataclass
8
+ from strif import AtomicVar
8
9
 
9
10
  from kash.config.env_settings import KashEnv
10
- from kash.utils.common.atomic_var import AtomicVar
11
11
 
12
12
  APP_NAME = "kash"
13
13
 
14
14
  DOT_DIR = ".kash"
15
15
 
16
- GLOBAL_WS_NAME = "global"
16
+ GLOBAL_WS_NAME = "workspace"
17
+ """Name of the global workspace."""
17
18
 
18
19
  RECOMMENDED_API_KEYS = [
19
20
  "OPENAI_API_KEY",
@@ -23,51 +24,28 @@ RECOMMENDED_API_KEYS = [
23
24
  ]
24
25
 
25
26
 
26
- def get_ws_root_dir() -> Path:
27
- """Default root directory for kash workspaces."""
28
- return KashEnv.KASH_WS_ROOT.read_path(Path("~/Kash"))
29
-
30
-
31
- def get_global_ws_dir() -> Path:
32
- """Default global workspace directory."""
33
- kash_ws_dir = KashEnv.KASH_GLOBAL_WS.read_path(None)
34
- if kash_ws_dir:
35
- return kash_ws_dir
36
- else:
37
- return get_ws_root_dir() / GLOBAL_WS_NAME
38
-
39
-
40
- def get_system_config_dir() -> Path:
41
- return Path("~/.config/kash").expanduser().resolve()
42
-
43
-
44
- def get_rcfile_path() -> Path:
45
- return get_system_config_dir() / "kashrc"
46
-
47
-
48
- def get_system_logs_dir() -> Path:
49
- """Default global and system logs directory (for server logs, etc)."""
50
- return KashEnv.KASH_SYSTEM_LOGS_DIR.read_path(get_ws_root_dir() / "logs")
51
-
52
-
53
- def get_system_cache_dir() -> Path:
54
- """Default global and system cache directory (for global media, content, etc)."""
55
- return KashEnv.KASH_SYSTEM_CACHE_DIR.read_path(get_ws_root_dir() / "cache")
56
-
57
-
58
- def get_system_env_path() -> Path:
59
- return get_system_config_dir() / "env.local"
60
-
61
-
62
- def get_mcp_ws_dir() -> Path | None:
27
+ def get_all_common_api_env_vars() -> list[str]:
63
28
  """
64
- Get the directory for the MCP workspace, if set.
29
+ Get all the common environment variables that are recommended to be set.
65
30
  """
66
- mcp_dir = KashEnv.KASH_MCP_WS.read_str()
67
- if mcp_dir:
68
- return Path(mcp_dir).expanduser().resolve()
69
- else:
70
- return None
31
+ from clideps.env_vars.env_names import get_all_common_env_names
32
+
33
+ return list(set(get_all_common_env_names() + RECOMMENDED_API_KEYS))
34
+
35
+
36
+ RECOMMENDED_PKGS = [
37
+ "less",
38
+ "eza",
39
+ "ripgrep",
40
+ "bat",
41
+ "zoxide",
42
+ "dust",
43
+ "duf",
44
+ "pygmentize",
45
+ "hexyl",
46
+ "ffmpeg",
47
+ "imagemagick",
48
+ ]
71
49
 
72
50
 
73
51
  MEDIA_CACHE_NAME = "media"
@@ -86,11 +64,6 @@ LOCAL_SERVER_PORTS_MAX = 30
86
64
  LOCAL_SERVER_LOG_NAME = "local_server"
87
65
 
88
66
 
89
- @cache
90
- def local_server_log_path() -> Path:
91
- return resolve_and_create_dirs(get_system_logs_dir() / f"{LOCAL_SERVER_LOG_NAME}.log")
92
-
93
-
94
67
  class LogLevel(Enum):
95
68
  debug = DEBUG
96
69
  info = INFO
@@ -146,11 +119,15 @@ def find_in_cwd_or_parents(filename: Path | str) -> Path | None:
146
119
  return None
147
120
 
148
121
 
122
+ def _get_rcfile_path() -> Path:
123
+ return _get_system_config_dir() / "kashrc"
124
+
125
+
149
126
  def find_rcfiles() -> list[Path]:
150
127
  """
151
128
  Find active rcfiles. Currently only supports one.
152
129
  """
153
- rcfile_path = get_rcfile_path()
130
+ rcfile_path = _get_rcfile_path()
154
131
  if rcfile_path.exists():
155
132
  return [rcfile_path]
156
133
  else:
@@ -159,9 +136,30 @@ def find_rcfiles() -> list[Path]:
159
136
 
160
137
  @dataclass
161
138
  class Settings:
139
+ ws_root_dir: Path
140
+ """A default root directory for kash workspaces (typically `~/Kash`)."""
141
+
142
+ global_ws_dir: Path
143
+ """The directory for the default global workspace."""
144
+
145
+ system_config_dir: Path
146
+ """The directory for system-wide configuration files."""
147
+
162
148
  media_cache_dir: Path
163
149
  """The workspace media cache directory, for caching audio, video, and transcripts."""
164
150
 
151
+ system_logs_dir: Path
152
+ """Default global and system logs directory (for server logs, etc)."""
153
+
154
+ system_cache_dir: Path
155
+ """Default global and system cache directory (for global media, content, etc)."""
156
+
157
+ mcp_ws_dir: Path | None
158
+ """The directory for the MCP workspace, if set."""
159
+
160
+ local_server_log_path: Path
161
+ """The path to the local server log."""
162
+
165
163
  content_cache_dir: Path
166
164
  """The workspace content cache directory, for caching web or local files."""
167
165
  # TODO: Separate workspace cached content (e.g. thumbnails) vs global files
@@ -198,12 +196,60 @@ class Settings:
198
196
  """If true, use Nerd Icons in file listings. Requires a compatible font."""
199
197
 
200
198
 
201
- # Initial default settings.
202
- _settings = AtomicVar(
203
- Settings(
199
+ ws_root_dir = Path("~/Kash").expanduser()
200
+
201
+
202
+ def _get_ws_root_dir() -> Path:
203
+ """Default root directory for kash workspaces."""
204
+ return KashEnv.KASH_WS_ROOT.read_path(default=ws_root_dir)
205
+
206
+
207
+ def _get_global_ws_dir() -> Path:
208
+ kash_ws_dir = KashEnv.KASH_GLOBAL_WS.read_path()
209
+ if kash_ws_dir:
210
+ return kash_ws_dir
211
+ else:
212
+ return _get_ws_root_dir() / GLOBAL_WS_NAME
213
+
214
+
215
+ def get_system_logs_dir() -> Path:
216
+ return KashEnv.KASH_SYSTEM_LOGS_DIR.read_path(default=_get_ws_root_dir() / "logs")
217
+
218
+
219
+ def _get_system_config_dir() -> Path:
220
+ return Path("~/.config/kash").expanduser().resolve()
221
+
222
+
223
+ def _get_system_cache_dir() -> Path:
224
+ return KashEnv.KASH_SYSTEM_CACHE_DIR.read_path(default=_get_ws_root_dir() / "cache")
225
+
226
+
227
+ def _get_mcp_ws_dir() -> Path | None:
228
+ mcp_dir = KashEnv.KASH_MCP_WS.read_str()
229
+ if mcp_dir:
230
+ return Path(mcp_dir).expanduser().resolve()
231
+ else:
232
+ return None
233
+
234
+
235
+ @cache
236
+ def _get_local_server_log_path() -> Path:
237
+ return resolve_and_create_dirs(get_system_logs_dir() / f"{LOCAL_SERVER_LOG_NAME}.log")
238
+
239
+
240
+ def _read_settings():
241
+ return Settings(
242
+ # Essential system settings for logs, workspaces, and configs:
243
+ ws_root_dir=_get_ws_root_dir(),
244
+ global_ws_dir=_get_global_ws_dir(),
245
+ system_config_dir=_get_system_config_dir(),
246
+ system_logs_dir=get_system_logs_dir(),
247
+ system_cache_dir=_get_system_cache_dir(),
248
+ mcp_ws_dir=_get_mcp_ws_dir(),
249
+ local_server_log_path=_get_local_server_log_path(),
204
250
  # These default to the global but can be overridden by workspace settings.
205
- media_cache_dir=get_system_cache_dir() / MEDIA_CACHE_NAME,
206
- content_cache_dir=get_system_cache_dir() / CONTENT_CACHE_NAME,
251
+ media_cache_dir=_get_system_cache_dir() / MEDIA_CACHE_NAME,
252
+ content_cache_dir=_get_system_cache_dir() / CONTENT_CACHE_NAME,
207
253
  debug_assistant=True,
208
254
  default_editor="nano",
209
255
  file_log_level=LogLevel.info,
@@ -215,7 +261,20 @@ _settings = AtomicVar(
215
261
  use_kerm_codes=False,
216
262
  use_nerd_icons=True,
217
263
  )
218
- )
264
+
265
+
266
+ # Initial default settings.
267
+ _settings = AtomicVar(_read_settings())
268
+
269
+
270
+ def configure_ws_and_settings(root_dir: Path):
271
+ """
272
+ Reset and reload all settings, deriving all paths from the new workspace
273
+ root. Good if embedding kash in another app.
274
+ """
275
+ global ws_root_dir
276
+ ws_root_dir = root_dir
277
+ _settings.set(_read_settings())
219
278
 
220
279
 
221
280
  def atomic_global_settings() -> AtomicVar[Settings]: