alexein-cli 0.3.0__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.
- alexein_cli/__init__.py +127 -0
- alexein_cli/_version.py +1 -0
- alexein_cli/create.py +178 -0
- alexein_cli/create_crewai.py +138 -0
- alexein_cli/crews.py +120 -0
- alexein_cli/doctor.py +182 -0
- alexein_cli/env.py +125 -0
- alexein_cli/project.py +312 -0
- alexein_cli/render.py +40 -0
- alexein_cli/scaffold.py +369 -0
- alexein_cli/stop.py +12 -0
- alexein_cli/templates/crewai/memory.py.tmpl +55 -0
- alexein_cli/templates/crewai/project/README.md.tmpl +29 -0
- alexein_cli/templates/crewai/project/agents.yaml.tmpl +8 -0
- alexein_cli/templates/crewai/project/crew.py.tmpl +43 -0
- alexein_cli/templates/crewai/project/env.example.tmpl +8 -0
- alexein_cli/templates/crewai/project/gitignore.tmpl +3 -0
- alexein_cli/templates/crewai/project/main.py.tmpl +36 -0
- alexein_cli/templates/crewai/project/pyproject.toml.tmpl +20 -0
- alexein_cli/templates/crewai/project/tasks.yaml.tmpl +6 -0
- alexein_cli/templates/langgraph/memory.py.tmpl +45 -0
- alexein_cli/templates/langgraph/project/README.md.tmpl +31 -0
- alexein_cli/templates/langgraph/project/chat.py.tmpl +46 -0
- alexein_cli/templates/langgraph/project/env.example.tmpl +8 -0
- alexein_cli/templates/langgraph/project/graph.py.tmpl +61 -0
- alexein_cli/templates/langgraph/project/langgraph.json.tmpl +7 -0
- alexein_cli/templates/openai_agents/memory.py.tmpl +47 -0
- alexein_cli/templates/openai_agents/project/README.md.tmpl +25 -0
- alexein_cli/templates/openai_agents/project/agent.py.tmpl +43 -0
- alexein_cli/templates/openai_agents/project/chat.py.tmpl +38 -0
- alexein_cli/templates/openai_agents/project/env.example.tmpl +6 -0
- alexein_cli/templates/project/gitignore.tmpl +6 -0
- alexein_cli/templates/project/pyproject.toml.tmpl +14 -0
- alexein_cli/ui.py +75 -0
- alexein_cli/wiring.py +61 -0
- alexein_cli-0.3.0.dist-info/METADATA +208 -0
- alexein_cli-0.3.0.dist-info/RECORD +40 -0
- alexein_cli-0.3.0.dist-info/WHEEL +4 -0
- alexein_cli-0.3.0.dist-info/entry_points.txt +2 -0
- alexein_cli-0.3.0.dist-info/licenses/LICENSE +21 -0
alexein_cli/doctor.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""`alexein-cli doctor`: whether a project is set up for memory, one line per check, each with
|
|
2
|
+
how to fix it when it is not. Reads everything and changes nothing; the one request it makes
|
|
3
|
+
is the key's check, `memory.me()`, which is free.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
import subprocess
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from alexein_cli import crews, env, project
|
|
13
|
+
from alexein_cli._version import __version__
|
|
14
|
+
from alexein_cli.scaffold import CLIENT, EXTRAS, MINIMUMS, MODULES, NAMES, requirement
|
|
15
|
+
from alexein_cli.ui import Console
|
|
16
|
+
|
|
17
|
+
_GENERATED = re.compile(r"Generated by `alexein-cli init ([a-z-]+)[^`]*` \(alexein-cli ([^)]+)\)")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def doctor(cwd: Path, console: Console) -> int:
|
|
21
|
+
"""0 when every check passes, 1 when any fails."""
|
|
22
|
+
root = project.find_root(cwd)
|
|
23
|
+
if root is None:
|
|
24
|
+
console.fail("no Python project here; run it from your project.")
|
|
25
|
+
return 1
|
|
26
|
+
console.line(f"alexein-cli {__version__} - checking {root}")
|
|
27
|
+
found = project.crew(root)
|
|
28
|
+
if found is not None and found.kind == "flow":
|
|
29
|
+
console.fail("this is a CrewAI Flow; Flows are not supported yet.")
|
|
30
|
+
return 1
|
|
31
|
+
used = (f for f in ("langgraph", "openai-agents") if project.uses(root, f))
|
|
32
|
+
framework = "crewai" if found is not None else next(used, "langgraph")
|
|
33
|
+
written = _written(root, framework, console)
|
|
34
|
+
frameworks = sorted({f for _, f in written} or {framework})
|
|
35
|
+
minimums = _minimums_for(frameworks)
|
|
36
|
+
results = [bool(written), _dependencies(root, frameworks, console)]
|
|
37
|
+
results.append(_versions(root, minimums, console))
|
|
38
|
+
results.append(_importable(root, frameworks, console))
|
|
39
|
+
results.append(_key(root, console))
|
|
40
|
+
console.line()
|
|
41
|
+
console.line("All good." if all(results) else "Fix what is marked ✗, then run doctor again.")
|
|
42
|
+
return 0 if all(results) else 1
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _mark(console: Console, ok: bool | None, what: str, fix: str = "") -> bool:
|
|
46
|
+
sign = {True: "✓", False: "✗", None: "-"}[ok]
|
|
47
|
+
console.line(f" {sign} {what}")
|
|
48
|
+
if ok is False and fix:
|
|
49
|
+
console.line(f" {fix}")
|
|
50
|
+
return ok is not False
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _written(root: Path, framework: str, console: Console) -> list[tuple[Path, str]]:
|
|
54
|
+
"""The memory files `init` wrote here, each marked with what it is for; none is a failure."""
|
|
55
|
+
found = [
|
|
56
|
+
(path, match.group(1), match.group(2))
|
|
57
|
+
for path in project.sources(root)
|
|
58
|
+
if (match := _GENERATED.search(path.read_text(errors="ignore")[:2000]))
|
|
59
|
+
]
|
|
60
|
+
if not found:
|
|
61
|
+
_mark(console, False, "no memory file", f"run `alexein-cli init {framework}`")
|
|
62
|
+
found = [(path, kind, version) for path, kind, version in found if kind in MINIMUMS]
|
|
63
|
+
for path, kind, version in found:
|
|
64
|
+
where = path.relative_to(root)
|
|
65
|
+
_mark(console, True, f"memory file {where} (written by {version}, for {NAMES[kind]})")
|
|
66
|
+
return [(path, kind) for path, kind, _ in found]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _minimums_for(frameworks: list[str]) -> dict[str, str]:
|
|
70
|
+
"""What the memory files here need installed: each framework's, together, and CrewAI
|
|
71
|
+
itself for a crew's."""
|
|
72
|
+
needed = {p: v for f in frameworks for p, v in MINIMUMS[f].items()}
|
|
73
|
+
if "crewai" in frameworks:
|
|
74
|
+
needed["crewai"] = crews.CREWAI_MINIMUM
|
|
75
|
+
return needed
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _dependencies(root: Path, frameworks: list[str], console: Console) -> bool:
|
|
79
|
+
"""The client is a dependency, with each framework's extra."""
|
|
80
|
+
wanted = [requirement(f) for f in frameworks]
|
|
81
|
+
missing = [
|
|
82
|
+
named
|
|
83
|
+
for named, f in zip(wanted, frameworks, strict=True)
|
|
84
|
+
if not project.declares(root, CLIENT, EXTRAS[f])
|
|
85
|
+
]
|
|
86
|
+
if missing:
|
|
87
|
+
fix = project.installer(root, missing).hint
|
|
88
|
+
return _mark(console, False, f"not dependencies: {', '.join(missing)}", fix)
|
|
89
|
+
return _mark(console, True, f"dependencies: {', '.join(wanted)}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _versions(root: Path, minimums: dict[str, str], console: Console) -> bool:
|
|
93
|
+
site = project.environment(root)
|
|
94
|
+
if site is None:
|
|
95
|
+
return _mark(console, None, "installed versions not checked: no .venv in the project")
|
|
96
|
+
ok = True
|
|
97
|
+
for package, minimum in minimums.items():
|
|
98
|
+
version = project.installed(site, package)
|
|
99
|
+
if version is None:
|
|
100
|
+
fix = "install the project's dependencies"
|
|
101
|
+
ok = _mark(console, False, f"{package} is not installed", fix) and ok
|
|
102
|
+
elif project.older(version, minimum):
|
|
103
|
+
fix = f"upgrade it to >={minimum}"
|
|
104
|
+
ok = _mark(console, False, f"{package} {version} is older than {minimum}", fix) and ok
|
|
105
|
+
elif package == "crewai" and crews.newer_than_tested(version):
|
|
106
|
+
tested = crews.CREWAI_TESTED
|
|
107
|
+
_mark(
|
|
108
|
+
console,
|
|
109
|
+
None,
|
|
110
|
+
f"crewai {version} is newer than this release was tested with ({tested}); "
|
|
111
|
+
"it should work - if tasks stop recalling or saving memory, "
|
|
112
|
+
"look for warnings on the `alexein` logger",
|
|
113
|
+
)
|
|
114
|
+
else:
|
|
115
|
+
_mark(console, True, f"{package} {version}")
|
|
116
|
+
return ok
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _importable(root: Path, frameworks: list[str], console: Console) -> bool:
|
|
120
|
+
"""Each integration module imports in the project's own Python: the client is there, and
|
|
121
|
+
so is everything its extra brings. Run there, not here: this CLI never imports a
|
|
122
|
+
framework."""
|
|
123
|
+
python = project.interpreter(root)
|
|
124
|
+
if python is None:
|
|
125
|
+
return _mark(console, None, "imports not checked: no .venv in the project")
|
|
126
|
+
ok = True
|
|
127
|
+
for framework in frameworks:
|
|
128
|
+
module = MODULES[framework]
|
|
129
|
+
try:
|
|
130
|
+
done = subprocess.run(
|
|
131
|
+
[str(python), "-c", f"import {module}"],
|
|
132
|
+
cwd=root,
|
|
133
|
+
capture_output=True,
|
|
134
|
+
text=True,
|
|
135
|
+
timeout=120,
|
|
136
|
+
check=False,
|
|
137
|
+
)
|
|
138
|
+
except (OSError, subprocess.TimeoutExpired) as error:
|
|
139
|
+
ok = _mark(console, False, f"{module} could not be checked: {error}") and ok
|
|
140
|
+
continue
|
|
141
|
+
if done.returncode == 0:
|
|
142
|
+
_mark(console, True, f"{module} imports")
|
|
143
|
+
continue
|
|
144
|
+
last = (done.stderr.strip().splitlines() or ["it failed"])[-1]
|
|
145
|
+
fix = project.installer(root, [requirement(framework)]).hint
|
|
146
|
+
ok = _mark(console, False, f"{module} does not import: {last}", fix) and ok
|
|
147
|
+
return ok
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _key(root: Path, console: Console) -> bool:
|
|
151
|
+
key, base_url = env.resolve(root)
|
|
152
|
+
if not key:
|
|
153
|
+
fix = f"put a key in .env as {env.KEY}= (console, under API keys)"
|
|
154
|
+
return _mark(console, False, f"no {env.KEY}", fix)
|
|
155
|
+
_mark(console, True, f"{env.KEY} from {env.origin(root, env.KEY)}")
|
|
156
|
+
if base_url:
|
|
157
|
+
_mark(console, True, f"{env.BASE_URL} from {env.origin(root, env.BASE_URL)}: {base_url}")
|
|
158
|
+
for name in (env.KEY, env.BASE_URL):
|
|
159
|
+
if env.differs(root, name):
|
|
160
|
+
_mark(
|
|
161
|
+
console,
|
|
162
|
+
None,
|
|
163
|
+
f"{name} is set in .env and in the environment, differently; this check uses "
|
|
164
|
+
".env's, and load_dotenv() keeps the environment's",
|
|
165
|
+
)
|
|
166
|
+
where = env.address(base_url) # the client's, so it is where the check goes
|
|
167
|
+
kept_out = env.ignored(root) or _mark(
|
|
168
|
+
console, False, ".env is not in .gitignore", "add .env to .gitignore"
|
|
169
|
+
)
|
|
170
|
+
checked = env.check(key, base_url)
|
|
171
|
+
if not checked.ok:
|
|
172
|
+
fix = f"check {env.KEY} and {env.BASE_URL}"
|
|
173
|
+
return _mark(console, False, f"key at {where}: {checked.detail}", fix)
|
|
174
|
+
_mark(console, True, f"key at {where}: {checked.detail}")
|
|
175
|
+
if checked.writes is False:
|
|
176
|
+
_mark(
|
|
177
|
+
console,
|
|
178
|
+
None,
|
|
179
|
+
"the key only reads: remember, memory_add and saving what a crew learned "
|
|
180
|
+
"need one that writes",
|
|
181
|
+
)
|
|
182
|
+
return kept_out
|
alexein_cli/env.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""The project's `.env`: the key and the address memory is reached at, read and set one line at
|
|
2
|
+
a time so every other line in the file stays exactly as it was. And the check that a key works.
|
|
3
|
+
|
|
4
|
+
Not a dotenv parser: `KEY=value` lines, optionally quoted, are all it reads, which is all it
|
|
5
|
+
ever writes.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import contextlib
|
|
11
|
+
import os
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from alexein import AlexeinError, AuthenticationError, Memory, NotFoundError
|
|
16
|
+
|
|
17
|
+
KEY = "ALEXEIN_API_KEY"
|
|
18
|
+
BASE_URL = "ALEXEIN_API_URL"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def read(env: Path) -> dict[str, str]:
|
|
22
|
+
"""The `KEY=value` lines of a `.env`, unquoted; empty when there is none."""
|
|
23
|
+
values: dict[str, str] = {}
|
|
24
|
+
try:
|
|
25
|
+
lines = env.read_text().splitlines()
|
|
26
|
+
except OSError:
|
|
27
|
+
return values
|
|
28
|
+
for line in lines:
|
|
29
|
+
name, sep, value = line.partition("=")
|
|
30
|
+
name = name.strip().removeprefix("export ").strip()
|
|
31
|
+
if sep and name and not name.startswith("#"):
|
|
32
|
+
values[name] = value.strip().strip("'\"")
|
|
33
|
+
return values
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def put(env: Path, name: str, value: str, *, replace: bool) -> str:
|
|
37
|
+
"""Set `name` in `.env`: added when absent, replaced when `replace` or empty, else kept.
|
|
38
|
+
Returns what happened: "added", "replaced" or "kept"."""
|
|
39
|
+
text = env.read_text() if env.exists() else ""
|
|
40
|
+
lines = text.splitlines()
|
|
41
|
+
for n, line in enumerate(lines):
|
|
42
|
+
if line.partition("=")[0].strip().removeprefix("export ").strip() != name:
|
|
43
|
+
continue
|
|
44
|
+
if not replace and read(env).get(name):
|
|
45
|
+
return "kept"
|
|
46
|
+
lines[n] = f"{name}={value}"
|
|
47
|
+
env.write_text("\n".join(lines) + "\n")
|
|
48
|
+
return "replaced"
|
|
49
|
+
separator = "" if not text or text.endswith("\n") else "\n"
|
|
50
|
+
env.write_text(f"{text}{separator}{name}={value}\n")
|
|
51
|
+
return "added"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def ignored(root: Path) -> bool:
|
|
55
|
+
"""Whether `.gitignore` keeps `.env` out of the repository."""
|
|
56
|
+
try:
|
|
57
|
+
lines = (root / ".gitignore").read_text().splitlines()
|
|
58
|
+
except OSError:
|
|
59
|
+
return False
|
|
60
|
+
return any(line.strip() in (".env", "/.env", ".env*", "*.env", ".env.*") for line in lines)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class Check:
|
|
65
|
+
"""What trying a key found. `ok` is whether it works; `writes` is None when it was not
|
|
66
|
+
checked; `detail` is the line to print."""
|
|
67
|
+
|
|
68
|
+
ok: bool
|
|
69
|
+
writes: bool | None
|
|
70
|
+
detail: str
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def address(base_url: str | None) -> str:
|
|
74
|
+
"""Where the client sends requests: `base_url`, else the environment's, else its own
|
|
75
|
+
default. Nothing is sent."""
|
|
76
|
+
with Memory("tm_unsent", base_url=base_url) as memory: # the client is made with a key
|
|
77
|
+
return memory.base_url
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def check(key: str, base_url: str | None, timeout: float = 10.0) -> Check:
|
|
81
|
+
"""Try a key with `memory.me()`: its organisation and project, and whether it writes. Should
|
|
82
|
+
that answer 404, a free fast search for an app user nobody has written for stands in: a 404
|
|
83
|
+
there means the key works, and `writes` is then None."""
|
|
84
|
+
try:
|
|
85
|
+
with Memory(key, base_url=base_url, timeout=timeout, max_retries=0) as memory:
|
|
86
|
+
try:
|
|
87
|
+
me = memory.me()
|
|
88
|
+
except NotFoundError:
|
|
89
|
+
return _probe(memory)
|
|
90
|
+
except AuthenticationError:
|
|
91
|
+
return Check(False, None, "the service does not know this key")
|
|
92
|
+
except AlexeinError as error:
|
|
93
|
+
return Check(False, None, f"could not check it: {error}")
|
|
94
|
+
org = me.org_name or me.org
|
|
95
|
+
project = me.project_name or me.project
|
|
96
|
+
where = f"{org}/{project}" if org and project else (org or "this key")
|
|
97
|
+
access = "reads and writes" if me.writes else "reads only"
|
|
98
|
+
return Check(True, me.writes, f"{where}, {access}")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _probe(memory: Memory) -> Check:
|
|
102
|
+
with contextlib.suppress(NotFoundError): # a working key, and nobody written for
|
|
103
|
+
memory.user("alexein-key-check").search("key check", fast=True)
|
|
104
|
+
return Check(True, None, "works; whether it writes was not checked")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def origin(root: Path, name: str) -> str | None:
|
|
108
|
+
"""Where the project's value of `name` comes from: ".env", "the environment", or None."""
|
|
109
|
+
if read(root / ".env").get(name):
|
|
110
|
+
return ".env"
|
|
111
|
+
return "the environment" if os.environ.get(name) else None
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def differs(root: Path, name: str) -> bool:
|
|
115
|
+
"""Whether `.env` and the environment both set `name`, to different values."""
|
|
116
|
+
kept, given = read(root / ".env").get(name), os.environ.get(name)
|
|
117
|
+
return bool(kept and given and kept != given)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def resolve(root: Path) -> tuple[str, str | None]:
|
|
121
|
+
"""The key and address the project will run with: its `.env`, else the environment."""
|
|
122
|
+
values = read(root / ".env")
|
|
123
|
+
key = values.get(KEY) or os.environ.get(KEY, "")
|
|
124
|
+
base_url = values.get(BASE_URL) or os.environ.get(BASE_URL) or None
|
|
125
|
+
return key, base_url
|
alexein_cli/project.py
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""What the CLI finds out about the project it is run in: where its root is, which graphs it
|
|
2
|
+
has, where generated code should go, whether it uses a framework, which versions are installed,
|
|
3
|
+
and how it adds a dependency. It only reads; the commands do the writing.
|
|
4
|
+
|
|
5
|
+
Not a general project detector: the checks are the few files that decide those answers, in the
|
|
6
|
+
order the tools themselves give them precedence.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import shlex
|
|
15
|
+
import shutil
|
|
16
|
+
from collections.abc import Iterator
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any, Literal, cast
|
|
20
|
+
|
|
21
|
+
# A directory holding any of these is a Python project's root.
|
|
22
|
+
_MARKERS = ("pyproject.toml", "langgraph.json", "requirements.txt", "setup.py", "setup.cfg")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class Installer:
|
|
27
|
+
"""How this project adds dependencies. `command` is run; with none, `hint` is printed for the
|
|
28
|
+
developer to run, and `requirements` (if set) is the file the packages are appended to."""
|
|
29
|
+
|
|
30
|
+
name: str
|
|
31
|
+
command: list[str] | None
|
|
32
|
+
hint: str
|
|
33
|
+
requirements: Path | None = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def find_root(start: Path) -> Path | None:
|
|
37
|
+
"""The nearest directory at or above `start` that is a Python project."""
|
|
38
|
+
for directory in (start, *start.parents):
|
|
39
|
+
if any((directory / marker).exists() for marker in _MARKERS):
|
|
40
|
+
return directory
|
|
41
|
+
if list(directory.glob("requirements*.txt")):
|
|
42
|
+
return directory
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True)
|
|
47
|
+
class Pyproject:
|
|
48
|
+
"""What is read from `pyproject.toml`: the project's name, which tools configure themselves in
|
|
49
|
+
it (`[tool.uv]`, `[tool.poetry]`, ...), and CrewAI's own table, `[tool.crewai]`."""
|
|
50
|
+
|
|
51
|
+
name: str
|
|
52
|
+
tools: frozenset[str]
|
|
53
|
+
crewai: dict[str, str] = field(default_factory=dict[str, str])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# Line by line rather than tomllib, which Python 3.10 does not have; these two shapes are all
|
|
57
|
+
# that is read, and both are one line in any pyproject a tool writes.
|
|
58
|
+
_TABLE = re.compile(r"^\[([A-Za-z0-9_.-]+)\]\s*$")
|
|
59
|
+
_NAME = re.compile(r"""^name\s*=\s*["']([^"']+)["']""")
|
|
60
|
+
_SETTING = re.compile(r"""^([A-Za-z_][\w-]*)\s*=\s*["']([^"']*)["']""")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def pyproject(root: Path) -> Pyproject:
|
|
64
|
+
try:
|
|
65
|
+
lines = (root / "pyproject.toml").read_text().splitlines()
|
|
66
|
+
except OSError:
|
|
67
|
+
return Pyproject("", frozenset())
|
|
68
|
+
name, tools, table, crewai = "", set[str](), "", dict[str, str]()
|
|
69
|
+
for line in lines:
|
|
70
|
+
header = _TABLE.match(line.strip())
|
|
71
|
+
if header:
|
|
72
|
+
table = header.group(1)
|
|
73
|
+
if table.startswith("tool."):
|
|
74
|
+
tools.add(table.split(".")[1])
|
|
75
|
+
continue
|
|
76
|
+
named = _NAME.match(line.strip())
|
|
77
|
+
if table == "project" and named and not name:
|
|
78
|
+
name = named.group(1)
|
|
79
|
+
setting = _SETTING.match(line.strip())
|
|
80
|
+
if table == "tool.crewai" and setting:
|
|
81
|
+
crewai[setting.group(1)] = setting.group(2)
|
|
82
|
+
return Pyproject(name, frozenset(tools), crewai)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True)
|
|
86
|
+
class Crew:
|
|
87
|
+
"""A CrewAI project: `classic` (a `@CrewBase` class in `src/<pkg>/crew.py`), `json` (a crew
|
|
88
|
+
defined in `crew.jsonc`), or a `flow`. `home` is where the memory file goes: beside `crew.py`,
|
|
89
|
+
beside the JSON definition, or the root for a flow."""
|
|
90
|
+
|
|
91
|
+
kind: Literal["classic", "json", "flow"]
|
|
92
|
+
home: Path
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
_CREW_BASE = re.compile(r"^\s*@CrewBase\b", re.M)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def crew(root: Path) -> Crew | None:
|
|
99
|
+
"""The CrewAI crew at `root`, from `[tool.crewai]` and the files it names; None when the
|
|
100
|
+
project is not a CrewAI one."""
|
|
101
|
+
table = pyproject(root).crewai
|
|
102
|
+
if table.get("type") == "flow":
|
|
103
|
+
return Crew("flow", root)
|
|
104
|
+
if table.get("definition"):
|
|
105
|
+
return Crew("json", (root / table["definition"]).parent)
|
|
106
|
+
if table or declares(root, "crewai"):
|
|
107
|
+
home = _crew_package(root)
|
|
108
|
+
if home is not None:
|
|
109
|
+
return Crew("classic", home)
|
|
110
|
+
return None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _crew_package(root: Path) -> Path | None:
|
|
114
|
+
"""The package whose `crew.py` holds a `@CrewBase` class: the project's own first, else the
|
|
115
|
+
only one under `src/`."""
|
|
116
|
+
name = pyproject(root).name.replace("-", "_")
|
|
117
|
+
candidates = [root / "src" / name] if name else []
|
|
118
|
+
candidates += sorted(p.parent for p in root.glob("src/*/crew.py"))
|
|
119
|
+
for home in dict.fromkeys(candidates):
|
|
120
|
+
try:
|
|
121
|
+
if _CREW_BASE.search((home / "crew.py").read_text(errors="ignore")):
|
|
122
|
+
return home
|
|
123
|
+
except OSError:
|
|
124
|
+
continue
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def graphs(root: Path) -> dict[str, Path]:
|
|
129
|
+
"""Each graph `langgraph.json` names, and the directory its file is in. A graph is a
|
|
130
|
+
`"./app/graph.py:graph"` string, or an object with that as its `path`."""
|
|
131
|
+
try:
|
|
132
|
+
config: object = json.loads((root / "langgraph.json").read_text())
|
|
133
|
+
except (OSError, ValueError):
|
|
134
|
+
return {}
|
|
135
|
+
if not isinstance(config, dict):
|
|
136
|
+
return {}
|
|
137
|
+
named: object = cast("dict[str, Any]", config).get("graphs")
|
|
138
|
+
if not isinstance(named, dict):
|
|
139
|
+
return {}
|
|
140
|
+
found: dict[str, Path] = {}
|
|
141
|
+
for name, spec in cast("dict[str, Any]", named).items():
|
|
142
|
+
where: object = cast("dict[str, Any]", spec).get("path") if isinstance(spec, dict) else spec
|
|
143
|
+
if isinstance(where, str) and ":" in where:
|
|
144
|
+
found[name] = (root / where.split(":", 1)[0]).parent.resolve()
|
|
145
|
+
return found
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def target_dir(root: Path, graph: Path | None = None) -> Path:
|
|
149
|
+
"""Where generated code goes: beside the chosen graph, else in the project's own package
|
|
150
|
+
(`src/<name>/`, the one package under `src/`, or `app/`), else the root."""
|
|
151
|
+
if graph is not None:
|
|
152
|
+
return graph
|
|
153
|
+
name = pyproject(root).name.replace("-", "_")
|
|
154
|
+
if name and (root / "src" / name).is_dir():
|
|
155
|
+
return root / "src" / name
|
|
156
|
+
packages = sorted(p.parent for p in root.glob("src/*/__init__.py"))
|
|
157
|
+
if len(packages) == 1:
|
|
158
|
+
return packages[0]
|
|
159
|
+
if (root / "app").is_dir():
|
|
160
|
+
return root / "app"
|
|
161
|
+
return root
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# Directories that hold other people's code, never the project's own.
|
|
165
|
+
_SKIPPED = {".venv", "venv", "env", ".git", "node_modules", "site-packages", "__pycache__", ".tox"}
|
|
166
|
+
# What each framework's package is imported as.
|
|
167
|
+
_IMPORTED_AS = {"langgraph": "langgraph", "openai-agents": "agents"}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def uses(root: Path, framework: str) -> bool:
|
|
171
|
+
"""Whether the project depends on the framework or imports it anywhere of its own. An
|
|
172
|
+
import of `agents` in a project with an `agents` package of its own is its own."""
|
|
173
|
+
if declares(root, framework):
|
|
174
|
+
return True
|
|
175
|
+
if framework == "langgraph" and (root / "langgraph.json").exists():
|
|
176
|
+
return True
|
|
177
|
+
if framework == "openai-agents" and shadows_agents(root):
|
|
178
|
+
return False
|
|
179
|
+
imports = re.compile(rf"^\s*(from|import)\s+{_IMPORTED_AS[framework]}\b", re.M)
|
|
180
|
+
return any(imports.search(text) for _, text in _texts(root))
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def shadows_agents(root: Path) -> bool:
|
|
184
|
+
"""Whether the project has a top-level `agents` package of its own, which the OpenAI
|
|
185
|
+
Agents SDK - imported as `agents` too - would clash with."""
|
|
186
|
+
return any((root / where).exists() for where in ("agents", "agents.py", "src/agents"))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
_FROM_AGENTS = re.compile(r"^\s*from\s+agents\b", re.M)
|
|
190
|
+
_BUILDS_AGENT = re.compile(r"\bAgent\s*[\[(]")
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def agent_dirs(root: Path) -> list[Path]:
|
|
194
|
+
"""The directories of the project's files that import from the OpenAI Agents SDK and build
|
|
195
|
+
an `Agent`, in order. A memory file this CLI wrote is not one, though its docstring shows
|
|
196
|
+
an `Agent(...)`."""
|
|
197
|
+
found = {
|
|
198
|
+
path.parent
|
|
199
|
+
for path, text in _texts(root)
|
|
200
|
+
if "Generated by `alexein-cli init" not in text[:2000]
|
|
201
|
+
and _FROM_AGENTS.search(text)
|
|
202
|
+
and _BUILDS_AGENT.search(text)
|
|
203
|
+
}
|
|
204
|
+
return sorted(found)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _texts(root: Path, limit: int = 2000) -> Iterator[tuple[Path, str]]:
|
|
208
|
+
"""The project's own Python files and their text, the first `limit` of them."""
|
|
209
|
+
for n, path in enumerate(sources(root)):
|
|
210
|
+
if n >= limit:
|
|
211
|
+
return
|
|
212
|
+
try:
|
|
213
|
+
yield path, path.read_text(errors="ignore")
|
|
214
|
+
except OSError:
|
|
215
|
+
continue
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def sources(root: Path) -> Iterator[Path]:
|
|
219
|
+
"""The project's own Python files, never walking into an environment or a dependency."""
|
|
220
|
+
for directory, subdirectories, files in os.walk(root):
|
|
221
|
+
subdirectories[:] = [d for d in subdirectories if d not in _SKIPPED]
|
|
222
|
+
yield from (Path(directory, f) for f in files if f.endswith(".py"))
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def environment(root: Path) -> Path | None:
|
|
226
|
+
"""The project's own virtual environment's site-packages, if it has one beside it."""
|
|
227
|
+
for name in (".venv", "venv"):
|
|
228
|
+
found = sorted((root / name).glob("lib/python*/site-packages"))
|
|
229
|
+
found += [root / name / "Lib" / "site-packages"]
|
|
230
|
+
for packages in found:
|
|
231
|
+
if packages.is_dir():
|
|
232
|
+
return packages
|
|
233
|
+
return None
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def interpreter(root: Path) -> Path | None:
|
|
237
|
+
"""The Python of the project's own virtual environment, if it has one beside it."""
|
|
238
|
+
for name in (".venv", "venv"):
|
|
239
|
+
for python in ("bin/python", "Scripts/python.exe"):
|
|
240
|
+
if (root / name / python).exists():
|
|
241
|
+
return root / name / python
|
|
242
|
+
return None
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def installed(site: Path, package: str) -> str | None:
|
|
246
|
+
"""The version of `package` installed in `site`, from its dist-info directory."""
|
|
247
|
+
wanted = re.sub(r"[-_.]+", "_", package).lower()
|
|
248
|
+
for info in site.glob("*.dist-info"):
|
|
249
|
+
name, _, version = info.name.removesuffix(".dist-info").rpartition("-")
|
|
250
|
+
if re.sub(r"[-_.]+", "_", name).lower() == wanted:
|
|
251
|
+
return version
|
|
252
|
+
return None
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def older(version: str, minimum: str) -> bool:
|
|
256
|
+
"""Whether `version` is below `minimum`, by their leading numbers (`0.6.2` < `1.0`)."""
|
|
257
|
+
|
|
258
|
+
def numbers(text: str) -> tuple[int, ...]:
|
|
259
|
+
return tuple(int(n) for n in re.findall(r"\d+", text.split("+")[0])[:3])
|
|
260
|
+
|
|
261
|
+
return numbers(version) < numbers(minimum)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def installer(root: Path, packages: list[str]) -> Installer:
|
|
265
|
+
"""How to add `packages` here: the project's own tool where its lock file or config says
|
|
266
|
+
which, else uv where it is installed, else pip."""
|
|
267
|
+
tools = pyproject(root).tools
|
|
268
|
+
has_pyproject = (root / "pyproject.toml").exists()
|
|
269
|
+
for tool, lock in (("uv", "uv.lock"), ("poetry", "poetry.lock"), ("pdm", "pdm.lock")):
|
|
270
|
+
if (root / lock).exists() or tool in tools:
|
|
271
|
+
command = [tool, "add", *packages]
|
|
272
|
+
return Installer(tool, command, shlex.join(command))
|
|
273
|
+
pip = shlex.join(["pip", "install", *packages])
|
|
274
|
+
if not has_pyproject and (root / "requirements.txt").exists():
|
|
275
|
+
return Installer("pip", None, pip, root / "requirements.txt")
|
|
276
|
+
if has_pyproject and shutil.which("uv"):
|
|
277
|
+
command = ["uv", "add", *packages]
|
|
278
|
+
return Installer("uv", command, shlex.join(command))
|
|
279
|
+
return Installer("pip", None, pip)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def declares(root: Path, package: str, extra: str | None = None) -> bool:
|
|
283
|
+
"""Whether the project already depends on `package` by that exact name (`langchain`, not
|
|
284
|
+
`langchain-core`) - and, given `extra`, with that extra (`alexein[langgraph]`): a quoted
|
|
285
|
+
requirement in `pyproject.toml` (`"langchain>=1"`), a Poetry table line (`langchain =
|
|
286
|
+
"^1"`, or `{extras = ["langgraph"], ...}`), or a line of a `requirements*.txt`. Never
|
|
287
|
+
prose: a description that says "LangGraph" is not a dependency on it."""
|
|
288
|
+
name = re.escape(package)
|
|
289
|
+
extras = r"(\[[^\]]*\])?"
|
|
290
|
+
quoted = re.compile(rf"""["']\s*{name}\s*{extras}\s*[<>=!~;@"']""", re.IGNORECASE)
|
|
291
|
+
table = re.compile(rf"^\s*{name}\s*=(.*)$", re.IGNORECASE | re.M)
|
|
292
|
+
listed = re.compile(rf"^\s*{name}\s*{extras}\s*(?:[<>=!~;@]|$)", re.IGNORECASE | re.M)
|
|
293
|
+
found: list[str] = []
|
|
294
|
+
try:
|
|
295
|
+
text = (root / "pyproject.toml").read_text()
|
|
296
|
+
found += [m.group(1) or "" for m in quoted.finditer(text)]
|
|
297
|
+
found += [m.group(1) for m in table.finditer(text)]
|
|
298
|
+
except OSError:
|
|
299
|
+
pass
|
|
300
|
+
for path in root.glob("requirements*.txt"):
|
|
301
|
+
try:
|
|
302
|
+
found += [m.group(1) or "" for m in listed.finditer(path.read_text())]
|
|
303
|
+
except OSError:
|
|
304
|
+
continue
|
|
305
|
+
if extra is None:
|
|
306
|
+
return bool(found)
|
|
307
|
+
wanted = _normal(extra)
|
|
308
|
+
return any(wanted in {_normal(e) for e in re.findall(r"[\w.-]+", f)} for f in found)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _normal(name: str) -> str:
|
|
312
|
+
return re.sub(r"[-_.]+", "-", name).lower()
|
alexein_cli/render.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Templates into source: the parts a preset leaves out cut away, and the values filled in.
|
|
2
|
+
|
|
3
|
+
A template marks each optional part with a pair of comment lines, `# >>> nodes` and
|
|
4
|
+
`# <<< nodes`, around whole lines; what is not asked for is dropped with its markers, and the
|
|
5
|
+
markers of what is kept are dropped too, so the written file carries none of this. Values are
|
|
6
|
+
`{name}` placeholders, filled by plain replacement - not `str.format`, since the code around
|
|
7
|
+
them is full of braces of its own.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
from collections.abc import Iterable, Mapping
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
TEMPLATES = Path(__file__).parent / "templates"
|
|
17
|
+
|
|
18
|
+
_MARK = re.compile(r"^\s*# (>>>|<<<) ([a-z_]+)\s*$")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def render(template: str, parts: Iterable[str], values: Mapping[str, str]) -> str:
|
|
22
|
+
"""The template at `templates/<template>`, keeping only `parts`, with `values` filled."""
|
|
23
|
+
kept = set(parts)
|
|
24
|
+
out: list[str] = []
|
|
25
|
+
skipping: list[str] = []
|
|
26
|
+
for line in (TEMPLATES / template).read_text().splitlines():
|
|
27
|
+
mark = _MARK.match(line)
|
|
28
|
+
if mark:
|
|
29
|
+
opens, part = mark.group(1) == ">>>", mark.group(2)
|
|
30
|
+
if opens and part not in kept:
|
|
31
|
+
skipping.append(part)
|
|
32
|
+
elif not opens and skipping and skipping[-1] == part:
|
|
33
|
+
skipping.pop()
|
|
34
|
+
continue
|
|
35
|
+
if not skipping:
|
|
36
|
+
out.append(line)
|
|
37
|
+
text = "\n".join(out) + "\n"
|
|
38
|
+
for name, value in values.items():
|
|
39
|
+
text = text.replace("{" + name + "}", value)
|
|
40
|
+
return text
|