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.
- kash/actions/__init__.py +4 -4
- kash/actions/core/markdownify.py +5 -2
- kash/actions/core/readability.py +5 -2
- kash/actions/core/render_as_html.py +18 -0
- kash/actions/core/webpage_config.py +12 -4
- kash/commands/__init__.py +8 -20
- kash/commands/base/basic_file_commands.py +15 -0
- kash/commands/base/debug_commands.py +13 -0
- kash/commands/base/general_commands.py +21 -16
- kash/commands/base/logs_commands.py +4 -2
- kash/commands/base/model_commands.py +8 -8
- kash/commands/base/search_command.py +3 -2
- kash/commands/base/show_command.py +5 -3
- kash/commands/extras/parse_uv_lock.py +186 -0
- kash/commands/help/doc_commands.py +2 -31
- kash/commands/help/welcome.py +33 -0
- kash/commands/workspace/selection_commands.py +11 -6
- kash/commands/workspace/workspace_commands.py +18 -15
- kash/config/colors.py +2 -0
- kash/config/env_settings.py +14 -1
- kash/config/init.py +2 -2
- kash/config/logger.py +59 -56
- kash/config/logger_basic.py +3 -3
- kash/config/settings.py +116 -57
- kash/config/setup.py +28 -12
- kash/config/text_styles.py +3 -13
- kash/docs/load_api_docs.py +2 -1
- kash/docs/markdown/topics/a3_getting_started.md +3 -2
- kash/{concepts → embeddings}/text_similarity.py +2 -2
- kash/exec/__init__.py +20 -3
- kash/exec/action_decorators.py +18 -4
- kash/exec/action_exec.py +41 -23
- kash/exec/action_registry.py +13 -48
- kash/exec/command_registry.py +2 -1
- kash/exec/fetch_url_metadata.py +4 -6
- kash/exec/importing.py +56 -0
- kash/exec/llm_transforms.py +6 -7
- kash/exec/precondition_registry.py +2 -1
- kash/exec/preconditions.py +16 -1
- kash/exec/shell_callable_action.py +33 -19
- kash/file_storage/file_store.py +23 -10
- kash/file_storage/item_file_format.py +5 -2
- kash/file_storage/metadata_dirs.py +11 -2
- kash/help/assistant.py +1 -1
- kash/help/assistant_instructions.py +2 -1
- kash/help/help_embeddings.py +2 -2
- kash/help/help_printing.py +7 -11
- kash/llm_utils/clean_headings.py +1 -1
- kash/llm_utils/llm_api_keys.py +4 -4
- kash/llm_utils/llm_features.py +68 -0
- kash/llm_utils/llm_messages.py +1 -2
- kash/llm_utils/llm_names.py +1 -1
- kash/llm_utils/llms.py +8 -3
- kash/local_server/__init__.py +5 -2
- kash/local_server/local_server.py +8 -5
- kash/local_server/local_server_commands.py +2 -2
- kash/local_server/local_url_formatters.py +1 -1
- kash/mcp/__init__.py +5 -2
- kash/mcp/mcp_cli.py +5 -5
- kash/mcp/mcp_server_commands.py +5 -5
- kash/mcp/mcp_server_routes.py +5 -5
- kash/mcp/mcp_server_sse.py +4 -2
- kash/media_base/media_cache.py +8 -8
- kash/media_base/media_services.py +1 -1
- kash/media_base/media_tools.py +6 -6
- kash/media_base/services/local_file_media.py +2 -2
- kash/media_base/{speech_transcription.py → transcription_deepgram.py} +25 -110
- kash/media_base/transcription_format.py +73 -0
- kash/media_base/transcription_whisper.py +38 -0
- kash/model/__init__.py +73 -5
- kash/model/actions_model.py +38 -4
- kash/model/concept_model.py +30 -0
- kash/model/items_model.py +44 -7
- kash/model/params_model.py +24 -0
- kash/shell/completions/completion_scoring.py +37 -5
- kash/shell/output/kerm_codes.py +1 -2
- kash/shell/output/shell_formatting.py +14 -4
- kash/shell/shell_main.py +2 -2
- kash/shell/utils/exception_printing.py +6 -0
- kash/shell/utils/native_utils.py +26 -20
- kash/text_handling/custom_sliding_transforms.py +12 -4
- kash/text_handling/doc_normalization.py +6 -2
- kash/text_handling/markdown_render.py +117 -0
- kash/text_handling/markdown_utils.py +204 -0
- kash/utils/common/import_utils.py +12 -3
- kash/utils/common/type_utils.py +0 -29
- kash/utils/common/url.py +27 -3
- kash/utils/errors.py +6 -0
- kash/utils/file_utils/file_formats.py +2 -2
- kash/utils/file_utils/file_formats_model.py +3 -0
- kash/web_content/dir_store.py +1 -2
- kash/web_content/file_cache_utils.py +37 -10
- kash/web_content/file_processing.py +68 -0
- kash/web_content/local_file_cache.py +12 -9
- kash/web_content/web_extract.py +8 -3
- kash/web_content/web_fetch.py +12 -4
- kash/web_gen/tabbed_webpage.py +5 -2
- kash/web_gen/templates/base_styles.css.jinja +120 -14
- kash/web_gen/templates/base_webpage.html.jinja +60 -13
- kash/web_gen/templates/content_styles.css.jinja +4 -2
- kash/web_gen/templates/item_view.html.jinja +2 -2
- kash/web_gen/templates/tabbed_webpage.html.jinja +1 -2
- kash/workspaces/__init__.py +15 -2
- kash/workspaces/selections.py +18 -3
- kash/workspaces/source_items.py +0 -1
- kash/workspaces/workspaces.py +5 -11
- kash/xonsh_custom/command_nl_utils.py +40 -19
- kash/xonsh_custom/custom_shell.py +43 -11
- kash/xonsh_custom/customize_prompt.py +39 -21
- kash/xonsh_custom/load_into_xonsh.py +22 -25
- kash/xonsh_custom/shell_load_commands.py +2 -2
- kash/xonsh_custom/xonsh_completers.py +2 -249
- kash/xonsh_custom/xonsh_keybindings.py +282 -0
- kash/xonsh_custom/xonsh_modern_tools.py +3 -3
- kash/xontrib/kash_extension.py +5 -6
- {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/METADATA +8 -6
- {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/RECORD +122 -123
- kash/concepts/concept_formats.py +0 -23
- kash/shell/clideps/api_keys.py +0 -100
- kash/shell/clideps/dotenv_setup.py +0 -115
- kash/shell/clideps/dotenv_utils.py +0 -98
- kash/shell/clideps/pkg_deps.py +0 -257
- kash/shell/clideps/platforms.py +0 -11
- kash/shell/clideps/terminal_features.py +0 -56
- kash/shell/utils/osc_utils.py +0 -95
- kash/shell/utils/terminal_images.py +0 -133
- kash/text_handling/markdown_util.py +0 -167
- kash/utils/common/atomic_var.py +0 -171
- kash/utils/common/string_replace.py +0 -93
- kash/utils/common/string_template.py +0 -101
- /kash/{concepts → embeddings}/cosine.py +0 -0
- /kash/{concepts → embeddings}/embeddings.py +0 -0
- {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/WHEEL +0 -0
- {kash_shell-0.3.9.dist-info → kash_shell-0.3.10.dist-info}/entry_points.txt +0 -0
- {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
|
-
|
|
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
|
|
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
|
-
#
|
|
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,
|
|
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
|
|
100
|
-
ws.selections.
|
|
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
|
|
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.
|
|
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
|
|
412
|
+
def params(full: bool = False) -> None:
|
|
406
413
|
"""
|
|
407
|
-
|
|
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,
|
|
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)"),
|
kash/config/env_settings.py
CHANGED
|
@@ -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
|
|
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
|
|
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 =
|
|
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
|
|
40
|
+
@dataclass
|
|
42
41
|
class LogSettings:
|
|
43
42
|
log_console_level: LogLevel
|
|
44
43
|
log_file_level: LogLevel
|
|
45
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
+
log_objects_dir: Path
|
|
56
|
+
log_file_path: Path
|
|
69
57
|
|
|
70
58
|
|
|
71
|
-
|
|
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=
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
102
|
-
"""
|
|
103
|
-
|
|
104
|
-
|
|
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
|
|
180
|
-
with
|
|
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
|
|
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
|
-
|
|
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()
|
kash/config/logger_basic.py
CHANGED
|
@@ -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(
|
|
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
|
|
42
|
-
file_handler: FileHandler = basic_file_handler(
|
|
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 = "
|
|
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
|
|
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
|
|
29
|
+
Get all the common environment variables that are recommended to be set.
|
|
65
30
|
"""
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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 =
|
|
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
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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=
|
|
206
|
-
content_cache_dir=
|
|
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]:
|