-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcli_init.py
More file actions
182 lines (147 loc) · 5.89 KB
/
cli_init.py
File metadata and controls
182 lines (147 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import asyncio
import importlib.resources
import os
import shutil
from collections.abc import Generator
from enum import Enum
from typing import Any
import click
from uipath._cli._utils._console import ConsoleLogger
from uipath._cli.middlewares import MiddlewareResult
from uipath_langchain.runtime.config import LangGraphConfig
console = ConsoleLogger()
class FileOperationStatus(str, Enum):
"""Status of a file operation."""
CREATED = "created"
UPDATED = "updated"
SKIPPED = "skipped"
def generate_agent_md_file(
target_directory: str,
file_name: str,
resource_name: str,
no_agents_md_override: bool,
) -> tuple[str, FileOperationStatus] | None:
"""Generate an agent-specific file from the packaged resource.
Args:
target_directory: The directory where the file should be created.
file_name: The name of the file should be created.
resource_name: The name of the resource folder where should be the file.
no_agents_md_override: When True, do not overwrite an existing file.
Returns:
A tuple of (file_name, status) where status is a FileOperationStatus,
or None if an error occurred.
"""
target_path = os.path.join(target_directory, file_name)
will_override = os.path.exists(target_path)
if will_override and no_agents_md_override:
return file_name, FileOperationStatus.SKIPPED
try:
source_path = importlib.resources.files(resource_name).joinpath(file_name)
with importlib.resources.as_file(source_path) as s_path:
shutil.copy(s_path, target_path)
return (
file_name,
FileOperationStatus.UPDATED
if will_override
else FileOperationStatus.CREATED,
)
except Exception as e:
console.warning(f"Could not create {file_name}: {e}")
return None
def generate_specific_agents_md_files(
target_directory: str,
no_agents_md_override: bool,
with_offline_docs: bool = False,
) -> Generator[tuple[str, FileOperationStatus], None, None]:
"""Generate AGENTS.md (and a CLAUDE.md shim), optionally bundling the offline docs.
Args:
target_directory: The directory where the files should be created.
no_agents_md_override: When True, do not overwrite existing AGENTS.md/CLAUDE.md.
with_offline_docs: When True, copy llms-full.txt to .uipath/ as an offline fallback.
Yields:
Tuple of (file_name, status) for each file operation.
"""
result = generate_agent_md_file(
target_directory,
"AGENTS.md",
"uipath_langchain._resources",
no_agents_md_override,
)
if result:
yield result
claude_result = generate_agent_md_file(
target_directory,
"CLAUDE.md",
"uipath_langchain._resources",
no_agents_md_override,
)
if claude_result:
yield claude_result
if with_offline_docs:
uipath_dir = os.path.join(target_directory, ".uipath")
os.makedirs(uipath_dir, exist_ok=True)
try:
source = importlib.resources.files("uipath._resources").joinpath(
"llms-full.txt"
)
with importlib.resources.as_file(source) as s_path:
shutil.copy(s_path, os.path.join(uipath_dir, "llms-full.txt"))
except (FileNotFoundError, ModuleNotFoundError):
pass
else:
agents_path = os.path.join(target_directory, "AGENTS.md")
with open(agents_path, "a", encoding="utf-8") as f:
f.write(
"\n3. If neither of the above is reachable, read "
"`.uipath/llms-full.txt` (offline fallback bundled with this project).\n"
)
def generate_agents_md_files(options: dict[str, Any]) -> None:
"""Generate AGENTS.md and log a summary."""
current_directory = os.getcwd()
no_agents_md_override = options.get("no_agents_md_override", False)
with_offline_docs = options.get("with_offline_docs", False)
created_files = []
updated_files = []
skipped_files = []
for file_name, status in generate_specific_agents_md_files(
current_directory, no_agents_md_override, with_offline_docs
):
if status == FileOperationStatus.CREATED:
created_files.append(file_name)
elif status == FileOperationStatus.UPDATED:
updated_files.append(file_name)
elif status == FileOperationStatus.SKIPPED:
skipped_files.append(file_name)
if created_files:
files_str = ", ".join(click.style(f, fg="cyan") for f in created_files)
console.success(f"Created: {files_str}")
if updated_files:
files_str = ", ".join(click.style(f, fg="cyan") for f in updated_files)
console.success(f"Updated: {files_str}")
if skipped_files:
files_str = ", ".join(click.style(f, fg="yellow") for f in skipped_files)
console.info(f"Skipped (already exist): {files_str}")
async def langgraph_init_middleware_async(
options: dict[str, Any] | None = None,
) -> MiddlewareResult:
"""Middleware to check for langgraph.json and create uipath.json with schemas"""
options = options or {}
config = LangGraphConfig()
if not config.exists:
return MiddlewareResult(
should_continue=True
) # Continue with normal flow if no langgraph.json
try:
generate_agents_md_files(options)
return MiddlewareResult(should_continue=False)
except Exception as e:
console.error(f"Error processing langgraph configuration: {str(e)}")
return MiddlewareResult(
should_continue=False,
should_include_stacktrace=True,
)
def langgraph_init_middleware(
options: dict[str, Any] | None = None,
) -> MiddlewareResult:
"""Middleware to check for langgraph.json and create uipath.json with schemas"""
return asyncio.run(langgraph_init_middleware_async(options))