fogies 0.0.0.dev11__tar.gz → 0.0.0.dev13__tar.gz
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.
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/PKG-INFO +3 -1
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/pyproject.toml +4 -1
- fogies-0.0.0.dev13/src/fogies/age_key.py +48 -0
- fogies-0.0.0.dev13/src/fogies/config.py +100 -0
- fogies-0.0.0.dev13/src/fogies/includes/templates/claude_sync.toml.template +18 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/access_key.py +5 -0
- fogies-0.0.0.dev13/src/fogies/tasks/claude_code.py +70 -0
- fogies-0.0.0.dev13/src/fogies/tasks/claude_desktop.py +140 -0
- fogies-0.0.0.dev13/src/fogies/tasks/claude_sync.py +345 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/format.py +3 -1
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/lint.py +3 -1
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/poetry.py +22 -11
- fogies-0.0.0.dev13/src/fogies/tasks/terminal.py +155 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/terraform.py +52 -28
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/terraform_backend.py +15 -26
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/test.py +3 -1
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/templates.py +4 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/backend.py +9 -7
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/aws_environ.py +25 -22
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/boto.py +73 -1
- fogies-0.0.0.dev13/src/fogies/tools/claude_code.py +152 -0
- fogies-0.0.0.dev13/src/fogies/tools/claude_desktop.py +198 -0
- fogies-0.0.0.dev13/src/fogies/tools/claude_sessions.py +53 -0
- fogies-0.0.0.dev13/src/fogies/tools/claude_sync.py +885 -0
- fogies-0.0.0.dev13/src/fogies/tools/claude_sync_deletions.py +292 -0
- fogies-0.0.0.dev13/src/fogies/tools/claude_sync_watch.py +179 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/terraform.py +4 -2
- fogies-0.0.0.dev11/src/fogies/tools/claude_sync.py +0 -130
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/README.md +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/__init__.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/aws_access_key.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/boto_clients.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/download_verified.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/includes/templates/aws.toml.template +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/includes/templates/backend_status.toml.template +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/includes/templates/poetry.toml.template +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/py.typed +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/pyfogies.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/ready_poll.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/retry.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tasks/__init__.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/__init__.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/alb.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/cloudwatch.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/ecr.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/ecs.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/hosted_zone.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/terraform/network.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/__init__.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/command.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/environ.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/go.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/ollama.py +0 -0
- {fogies-0.0.0.dev11 → fogies-0.0.0.dev13}/src/fogies/tools/terraform_backend.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fogies
|
|
3
|
-
Version: 0.0.0.
|
|
3
|
+
Version: 0.0.0.dev13
|
|
4
4
|
Summary:
|
|
5
5
|
Author: James Fogarty
|
|
6
6
|
Author-email: jayfo@users.noreply.github.com
|
|
@@ -8,10 +8,12 @@ Requires-Python: >=3.14,<4.0
|
|
|
8
8
|
Classifier: Programming Language :: Python :: 3
|
|
9
9
|
Classifier: Programming Language :: Python :: 3.14
|
|
10
10
|
Requires-Dist: boto3 (>=1.42.68,<2.0.0)
|
|
11
|
+
Requires-Dist: claude-agent-sdk (>=0.2.168,<0.3.0)
|
|
11
12
|
Requires-Dist: filelock (>=3.25.2,<4.0.0)
|
|
12
13
|
Requires-Dist: ollama (>=0.6.2,<0.7.0)
|
|
13
14
|
Requires-Dist: psutil (>=7.2.2,<8.0.0)
|
|
14
15
|
Requires-Dist: pydantic (>=2.12.5,<3.0.0)
|
|
16
|
+
Requires-Dist: pyrage (>=1.4.0,<2.0.0)
|
|
15
17
|
Requires-Dist: requests (>=2.33.0,<2.34.0)
|
|
16
18
|
Requires-Dist: tenacity (>=9.1.4,<10.0.0)
|
|
17
19
|
Requires-Dist: tomlkit (>=0.15.1,<0.16.0)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "fogies"
|
|
3
3
|
# Version follows semantic versioning <https://semver.org/>.
|
|
4
|
-
version = "0.0.0-dev.
|
|
4
|
+
version = "0.0.0-dev.13"
|
|
5
5
|
description = ""
|
|
6
6
|
authors = [
|
|
7
7
|
{name = "James Fogarty",email = "jayfo@users.noreply.github.com"}
|
|
@@ -18,6 +18,7 @@ include = [
|
|
|
18
18
|
{path = "src/fogies/includes/templates/aws.toml.template", format = ["sdist", "wheel"]},
|
|
19
19
|
{path = "src/fogies/includes/templates/poetry.toml.template", format = ["sdist", "wheel"]},
|
|
20
20
|
{path = "src/fogies/includes/templates/backend_status.toml.template", format = ["sdist", "wheel"]},
|
|
21
|
+
{path = "src/fogies/includes/templates/claude_sync.toml.template", format = ["sdist", "wheel"]},
|
|
21
22
|
]
|
|
22
23
|
|
|
23
24
|
[tool.poetry.dependencies]
|
|
@@ -26,10 +27,12 @@ include = [
|
|
|
26
27
|
# ~ allows only patch releases.
|
|
27
28
|
# Example: ~1.2.3 will match versions >=1.2.3 and <1.3.0.
|
|
28
29
|
boto3 = "^1.42.68"
|
|
30
|
+
claude-agent-sdk = "^0.2.168"
|
|
29
31
|
filelock = "^3.25.2"
|
|
30
32
|
ollama = "^0.6.2"
|
|
31
33
|
psutil = "^7.2.2"
|
|
32
34
|
pydantic = "^2.12.5"
|
|
35
|
+
pyrage = "^1.4.0"
|
|
33
36
|
requests = "~2.33.0"
|
|
34
37
|
tenacity = "^9.1.4"
|
|
35
38
|
tomlkit = "^0.15.1"
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Age secret keys, which encrypt what claude-sync uploads.
|
|
2
|
+
|
|
3
|
+
An age secret key is a string like AGE-SECRET-KEY-1...: see
|
|
4
|
+
<https://github.com/FiloSottile/age>. pyrage, which binds to a Rust
|
|
5
|
+
implementation of age, creates and checks them. It ships no type stubs, so the
|
|
6
|
+
little of it that is used is given types here and nowhere else.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Protocol, cast
|
|
10
|
+
|
|
11
|
+
import pyrage # pyright: ignore[reportMissingTypeStubs]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class _IdentityFactory(Protocol):
|
|
15
|
+
"""The parts of pyrage's x25519 Identity class that are used."""
|
|
16
|
+
|
|
17
|
+
def generate(self) -> object: ...
|
|
18
|
+
|
|
19
|
+
def from_str(self, value: str) -> object: ...
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
_IDENTITY = cast(
|
|
23
|
+
_IdentityFactory,
|
|
24
|
+
pyrage.x25519.Identity, # pyright: ignore[reportUnknownMemberType, reportAttributeAccessIssue]
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
_IDENTITY_ERROR = cast(
|
|
28
|
+
type[Exception],
|
|
29
|
+
pyrage.IdentityError, # pyright: ignore[reportAttributeAccessIssue]
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def generate_age_key() -> str:
|
|
34
|
+
"""Return a new, random age secret key."""
|
|
35
|
+
return str(_IDENTITY.generate())
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def validate_age_key(value: str) -> str:
|
|
39
|
+
"""Return value, if it is an age secret key.
|
|
40
|
+
|
|
41
|
+
Raises ValueError if it is not, including a key with a mistyped character,
|
|
42
|
+
since the checksum is checked.
|
|
43
|
+
"""
|
|
44
|
+
try:
|
|
45
|
+
_ = _IDENTITY.from_str(value)
|
|
46
|
+
except _IDENTITY_ERROR as exc:
|
|
47
|
+
raise ValueError("Not an age secret key") from exc
|
|
48
|
+
return value
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""Loading config files, in one place.
|
|
2
|
+
|
|
3
|
+
A config file is TOML at a path that a project chooses, and is loaded into a
|
|
4
|
+
pydantic model. The file at that path may instead be a redirect, so that
|
|
5
|
+
projects can share one config rather than each repeating it:
|
|
6
|
+
|
|
7
|
+
redirect = "../shared/aws.toml"
|
|
8
|
+
|
|
9
|
+
The path is relative to the file that has the redirect, and the file has
|
|
10
|
+
nothing else in it. Anything that is common to loading every config, such as
|
|
11
|
+
redirects, belongs here rather than in each loader.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import pathlib
|
|
15
|
+
import tomllib
|
|
16
|
+
from collections.abc import Callable
|
|
17
|
+
from typing import TypeVar
|
|
18
|
+
|
|
19
|
+
from pydantic import BaseModel
|
|
20
|
+
|
|
21
|
+
from fogies.templates import ensure_from_template
|
|
22
|
+
|
|
23
|
+
ConfigModel = TypeVar("ConfigModel", bound=BaseModel)
|
|
24
|
+
|
|
25
|
+
_REDIRECT_KEY = "redirect"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _read_toml(*, path: pathlib.Path) -> dict[str, object]:
|
|
29
|
+
with path.open("rb") as f:
|
|
30
|
+
return tomllib.load(f)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _require_toml(*, path: pathlib.Path) -> None:
|
|
34
|
+
if path.suffix != ".toml":
|
|
35
|
+
raise ValueError("Config file '{}' must have a .toml extension.".format(path))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _redirect_target(
|
|
39
|
+
*, path: pathlib.Path, data: dict[str, object]
|
|
40
|
+
) -> pathlib.Path | None:
|
|
41
|
+
"""Return where data, read from path, redirects to, or None if it doesn't."""
|
|
42
|
+
if _REDIRECT_KEY not in data:
|
|
43
|
+
return None
|
|
44
|
+
target = data[_REDIRECT_KEY]
|
|
45
|
+
if len(data) != 1 or not isinstance(target, str):
|
|
46
|
+
raise ValueError(
|
|
47
|
+
"'{}' has a '{}', so it must contain only that, as a path.".format(
|
|
48
|
+
path, _REDIRECT_KEY
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
return path.parent / target
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def resolve_config_path(*, path: pathlib.Path) -> pathlib.Path:
|
|
55
|
+
"""Return the file that holds the config at path.
|
|
56
|
+
|
|
57
|
+
That is path itself, or the file that it redirects to. Use this to write to
|
|
58
|
+
a config, so that the write goes to the shared file and not the redirect.
|
|
59
|
+
|
|
60
|
+
Raises ValueError if path, or the file it redirects to, is not a .toml file.
|
|
61
|
+
"""
|
|
62
|
+
_require_toml(path=path)
|
|
63
|
+
if not path.exists():
|
|
64
|
+
return path
|
|
65
|
+
|
|
66
|
+
target = _redirect_target(path=path, data=_read_toml(path=path))
|
|
67
|
+
if target is None:
|
|
68
|
+
return path
|
|
69
|
+
|
|
70
|
+
_require_toml(path=target)
|
|
71
|
+
if not target.exists():
|
|
72
|
+
raise FileNotFoundError(
|
|
73
|
+
"'{}' redirects to '{}', which was not found.".format(path, target)
|
|
74
|
+
)
|
|
75
|
+
if _redirect_target(path=target, data=_read_toml(path=target)) is not None:
|
|
76
|
+
raise ValueError(
|
|
77
|
+
"'{}' redirects to '{}', which is itself a redirect.".format(path, target)
|
|
78
|
+
)
|
|
79
|
+
return target
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def load_config(
|
|
83
|
+
*,
|
|
84
|
+
path: pathlib.Path,
|
|
85
|
+
model: type[ConfigModel],
|
|
86
|
+
template_factory: Callable[[], str],
|
|
87
|
+
) -> ConfigModel:
|
|
88
|
+
"""Load the config at path as a model.
|
|
89
|
+
|
|
90
|
+
If path doesn't exist, it is created from template_factory and
|
|
91
|
+
FileNotFoundError is raised, so that it is filled in before it is used; see
|
|
92
|
+
ensure_from_template(). If path redirects, the config is the file it
|
|
93
|
+
redirects to. Raises ValueError if path, or the file it redirects to, is not
|
|
94
|
+
a .toml file, and a pydantic ValidationError if the config does not match
|
|
95
|
+
model.
|
|
96
|
+
"""
|
|
97
|
+
# Before the template, so that a file with the wrong name is never created.
|
|
98
|
+
_require_toml(path=path)
|
|
99
|
+
ensure_from_template(path=path, template_factory=template_factory)
|
|
100
|
+
return model.model_validate(_read_toml(path=resolve_config_path(path=path)))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Config for claude-sync tasks.
|
|
2
|
+
# See https://github.com/tawanorg/claude-sync
|
|
3
|
+
|
|
4
|
+
# Profile in the AWS config, for an IAM user that can use the buckets.
|
|
5
|
+
aws_profile = "..."
|
|
6
|
+
|
|
7
|
+
# S3 bucket that claude-sync syncs to.
|
|
8
|
+
bucket = "..."
|
|
9
|
+
# S3 bucket for the record of deleted sessions, which claude-sync does not use.
|
|
10
|
+
deletions_bucket = "..."
|
|
11
|
+
region = "..."
|
|
12
|
+
|
|
13
|
+
# What to sync: "sessions" or "full".
|
|
14
|
+
scope = "sessions"
|
|
15
|
+
|
|
16
|
+
# Encryption key.
|
|
17
|
+
# claude-sync.configure will create, so that all machines have the same key.
|
|
18
|
+
# age_key = "AGE-SECRET-KEY-1..."
|
|
@@ -98,6 +98,7 @@ def get_task_create(
|
|
|
98
98
|
) -> Task[Callable[..., None]]:
|
|
99
99
|
@task(
|
|
100
100
|
name="create",
|
|
101
|
+
auto_shortflags=False,
|
|
101
102
|
positional=[],
|
|
102
103
|
help={
|
|
103
104
|
"prompt": "Prompt for admin credentials.",
|
|
@@ -125,6 +126,7 @@ def get_task_delete(
|
|
|
125
126
|
) -> Task[Callable[..., None]]:
|
|
126
127
|
@task(
|
|
127
128
|
name="delete",
|
|
129
|
+
auto_shortflags=False,
|
|
128
130
|
positional=[],
|
|
129
131
|
help={
|
|
130
132
|
"prompt": "Prompt for admin credentials.",
|
|
@@ -159,6 +161,7 @@ def get_task_delete_key(
|
|
|
159
161
|
) -> Task[Callable[..., None]]:
|
|
160
162
|
@task(
|
|
161
163
|
name="delete-key",
|
|
164
|
+
auto_shortflags=False,
|
|
162
165
|
positional=[],
|
|
163
166
|
help={
|
|
164
167
|
"prompt": "Prompt for admin credentials.",
|
|
@@ -206,6 +209,7 @@ def get_task_list(
|
|
|
206
209
|
) -> Task[Callable[..., None]]:
|
|
207
210
|
@task(
|
|
208
211
|
name="list",
|
|
212
|
+
auto_shortflags=False,
|
|
209
213
|
help={
|
|
210
214
|
"prompt": "Prompt for admin credentials.",
|
|
211
215
|
},
|
|
@@ -235,6 +239,7 @@ def get_task_rotate_key(
|
|
|
235
239
|
) -> Task[Callable[..., None]]:
|
|
236
240
|
@task(
|
|
237
241
|
name="rotate-key",
|
|
242
|
+
auto_shortflags=False,
|
|
238
243
|
positional=[],
|
|
239
244
|
help={
|
|
240
245
|
"prompt": "Prompt for admin credentials.",
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Tasks for signing in to Claude Code."""
|
|
2
|
+
|
|
3
|
+
import pathlib
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from typing import cast
|
|
6
|
+
|
|
7
|
+
from invoke.collection import Collection
|
|
8
|
+
from invoke.context import Context
|
|
9
|
+
from invoke.tasks import Task, task
|
|
10
|
+
|
|
11
|
+
from fogies.tools.claude_code import claude_code
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_task_login(*, binary_cache_path: pathlib.Path) -> Task[Callable[..., None]]:
|
|
15
|
+
@task(
|
|
16
|
+
name="login", auto_shortflags=False
|
|
17
|
+
) # pyright: ignore[reportUntypedFunctionDecorator]
|
|
18
|
+
def task_login(context: Context) -> None:
|
|
19
|
+
"""
|
|
20
|
+
Login to Claude Code.
|
|
21
|
+
"""
|
|
22
|
+
_ = context
|
|
23
|
+
with claude_code(binary_cache_path=binary_cache_path) as cc:
|
|
24
|
+
cc.auth_login()
|
|
25
|
+
|
|
26
|
+
return cast(Task[Callable[..., None]], task_login)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_task_logout(*, binary_cache_path: pathlib.Path) -> Task[Callable[..., None]]:
|
|
30
|
+
@task(
|
|
31
|
+
name="logout", auto_shortflags=False
|
|
32
|
+
) # pyright: ignore[reportUntypedFunctionDecorator]
|
|
33
|
+
def task_logout(context: Context) -> None:
|
|
34
|
+
"""
|
|
35
|
+
Logout of Claude Code.
|
|
36
|
+
"""
|
|
37
|
+
_ = context
|
|
38
|
+
with claude_code(binary_cache_path=binary_cache_path) as cc:
|
|
39
|
+
cc.auth_logout()
|
|
40
|
+
|
|
41
|
+
return cast(Task[Callable[..., None]], task_logout)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def get_task_status(*, binary_cache_path: pathlib.Path) -> Task[Callable[..., None]]:
|
|
45
|
+
@task(
|
|
46
|
+
name="status", auto_shortflags=False
|
|
47
|
+
) # pyright: ignore[reportUntypedFunctionDecorator]
|
|
48
|
+
def task_status(context: Context) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Show login status of Claude Code.
|
|
51
|
+
"""
|
|
52
|
+
_ = context
|
|
53
|
+
with claude_code(binary_cache_path=binary_cache_path) as cc:
|
|
54
|
+
if not cc.auth_status():
|
|
55
|
+
print("Not signed in. Sign in with the login task.")
|
|
56
|
+
|
|
57
|
+
return cast(Task[Callable[..., None]], task_status)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def get_collection(
|
|
61
|
+
*,
|
|
62
|
+
collection_name: str,
|
|
63
|
+
binary_cache_path: pathlib.Path,
|
|
64
|
+
) -> Collection:
|
|
65
|
+
"""Get a collection of tasks for signing in to Claude Code."""
|
|
66
|
+
collection = Collection(collection_name)
|
|
67
|
+
collection.add_task(get_task_login(binary_cache_path=binary_cache_path))
|
|
68
|
+
collection.add_task(get_task_logout(binary_cache_path=binary_cache_path))
|
|
69
|
+
collection.add_task(get_task_status(binary_cache_path=binary_cache_path))
|
|
70
|
+
return collection
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Tasks for the sessions that the Claude desktop app lists, and opening others in it.
|
|
2
|
+
|
|
3
|
+
The app lists only sessions it has a record of on this machine, as read by
|
|
4
|
+
fogies.tools.claude_desktop. A session synced here from another machine (e.g.,
|
|
5
|
+
by claude-sync) has a transcript but no record, so is not in the sidebar until it
|
|
6
|
+
is resumed in the app.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import pathlib
|
|
10
|
+
from collections.abc import Callable
|
|
11
|
+
from typing import cast
|
|
12
|
+
|
|
13
|
+
from claude_agent_sdk import list_sessions
|
|
14
|
+
from invoke.collection import Collection
|
|
15
|
+
from invoke.context import Context
|
|
16
|
+
from invoke.tasks import Task, task
|
|
17
|
+
|
|
18
|
+
from fogies.tools.claude_code import claude_code
|
|
19
|
+
from fogies.tools.claude_desktop import (
|
|
20
|
+
desktop_sessions,
|
|
21
|
+
desktop_sessions_directory,
|
|
22
|
+
desktop_statuses,
|
|
23
|
+
plan_resume,
|
|
24
|
+
resume_each,
|
|
25
|
+
)
|
|
26
|
+
from fogies.tools.claude_sessions import describe_session, find_session_id
|
|
27
|
+
|
|
28
|
+
_NOT_FOUND = "The Claude desktop app's sessions were not found here ({}).".format
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def get_task_sessions() -> Task[Callable[..., None]]:
|
|
32
|
+
@task(
|
|
33
|
+
name="sessions", auto_shortflags=False
|
|
34
|
+
) # pyright: ignore[reportUntypedFunctionDecorator]
|
|
35
|
+
def task_sessions(context: Context) -> None:
|
|
36
|
+
"""
|
|
37
|
+
List sessions on this machine, and whether the Claude desktop app lists them.
|
|
38
|
+
"""
|
|
39
|
+
_ = context
|
|
40
|
+
recorded = desktop_sessions()
|
|
41
|
+
if recorded is None:
|
|
42
|
+
raise ValueError(_NOT_FOUND(desktop_sessions_directory()))
|
|
43
|
+
|
|
44
|
+
sessions = {session.session_id: session for session in list_sessions()}
|
|
45
|
+
statuses = desktop_statuses(transcripts=sessions, recorded=recorded)
|
|
46
|
+
for session_id, status in statuses.items():
|
|
47
|
+
if session_id in sessions:
|
|
48
|
+
print(
|
|
49
|
+
"{:<10} {}".format(status, describe_session(sessions[session_id]))
|
|
50
|
+
)
|
|
51
|
+
else:
|
|
52
|
+
print(
|
|
53
|
+
"{:<10} {} {}".format(
|
|
54
|
+
status, session_id, recorded[session_id].title or ""
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
return cast(Task[Callable[..., None]], task_sessions)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def get_task_resume(*, binary_cache_path: pathlib.Path) -> Task[Callable[..., None]]:
|
|
62
|
+
@task(
|
|
63
|
+
name="resume",
|
|
64
|
+
auto_shortflags=False,
|
|
65
|
+
help={
|
|
66
|
+
"session_id": "Session to resume, from any part of its ID that is only in that one.",
|
|
67
|
+
"all": "Resume every session that the app does not list, except those that ran in a directory that is not here.",
|
|
68
|
+
},
|
|
69
|
+
) # pyright: ignore[reportUntypedFunctionDecorator]
|
|
70
|
+
def task_resume(
|
|
71
|
+
context: Context, session_id: str | None = None, all_: bool = False
|
|
72
|
+
) -> None:
|
|
73
|
+
"""
|
|
74
|
+
Resume a session, or all that are not listed, in Claude desktop.
|
|
75
|
+
"""
|
|
76
|
+
_ = context
|
|
77
|
+
if (session_id is not None) == all_:
|
|
78
|
+
raise ValueError("Give a session ID, or --all, and not both.")
|
|
79
|
+
|
|
80
|
+
sessions = {session.session_id: session for session in list_sessions()}
|
|
81
|
+
if all_:
|
|
82
|
+
recorded = desktop_sessions()
|
|
83
|
+
if recorded is None:
|
|
84
|
+
# Without knowing what is listed, all would be every session.
|
|
85
|
+
raise ValueError(_NOT_FOUND(desktop_sessions_directory()))
|
|
86
|
+
directories = {i: session.cwd for i, session in sessions.items()}
|
|
87
|
+
# Archived sessions are recorded, so are not resumed.
|
|
88
|
+
listed = recorded.keys()
|
|
89
|
+
else:
|
|
90
|
+
assert session_id is not None
|
|
91
|
+
resume_id = find_session_id(list(sessions), session_id)
|
|
92
|
+
directories = {resume_id: sessions[resume_id].cwd}
|
|
93
|
+
listed = ()
|
|
94
|
+
resumable, skipped = plan_resume(directories=directories, listed=listed)
|
|
95
|
+
|
|
96
|
+
# One session that is asked for is an error. Skipping a session is for
|
|
97
|
+
# when all of them are.
|
|
98
|
+
if skipped and not all_:
|
|
99
|
+
((skipped_id, problem),) = skipped.items()
|
|
100
|
+
raise ValueError("Session '{}' {}".format(skipped_id, problem))
|
|
101
|
+
for skipped_id, problem in skipped.items():
|
|
102
|
+
print("Skipping {}, as it {}.".format(skipped_id, problem))
|
|
103
|
+
|
|
104
|
+
with claude_code(binary_cache_path=binary_cache_path) as cc:
|
|
105
|
+
|
|
106
|
+
def resume(resume_id: str, cwd: pathlib.Path) -> None:
|
|
107
|
+
print("Resuming {}.".format(resume_id))
|
|
108
|
+
cc.resume_in_desktop(session_id=resume_id, cwd=cwd)
|
|
109
|
+
|
|
110
|
+
if not all_:
|
|
111
|
+
# One session that is asked for is an error, if it fails.
|
|
112
|
+
for resume_id, cwd in resumable.items():
|
|
113
|
+
resume(resume_id, cwd)
|
|
114
|
+
return
|
|
115
|
+
resumed, failed = resume_each(resumable=resumable, resume=resume)
|
|
116
|
+
|
|
117
|
+
for failed_id, reason in failed.items():
|
|
118
|
+
print("Could not resume {}: {}".format(failed_id, reason))
|
|
119
|
+
print(
|
|
120
|
+
"Resumed {} of {} sessions not listed.".format(
|
|
121
|
+
len(resumed), len(resumable) + len(skipped)
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
# Skipping is not a failure, but a session that was tried is.
|
|
125
|
+
if failed:
|
|
126
|
+
raise SystemExit(1)
|
|
127
|
+
|
|
128
|
+
return cast(Task[Callable[..., None]], task_resume)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def get_collection(
|
|
132
|
+
*,
|
|
133
|
+
collection_name: str,
|
|
134
|
+
binary_cache_path: pathlib.Path,
|
|
135
|
+
) -> Collection:
|
|
136
|
+
"""Get a collection of tasks for the sessions that the Claude desktop app lists."""
|
|
137
|
+
collection = Collection(collection_name)
|
|
138
|
+
collection.add_task(get_task_resume(binary_cache_path=binary_cache_path))
|
|
139
|
+
collection.add_task(get_task_sessions())
|
|
140
|
+
return collection
|