-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_binary.py
More file actions
230 lines (198 loc) · 5.99 KB
/
make_binary.py
File metadata and controls
230 lines (198 loc) · 5.99 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Build script for compiling ciri-copilot CLI to a standalone binary.
Usage:
python build.py [--target TARGET]
Options:
--target TARGET Target platform (auto-detected if not specified)
Examples: x86_64-unknown-linux-gnu, x86_64-pc-windows-msvc, aarch64-apple-darwin
"""
import argparse
import os
import platform
import shutil
import subprocess
import sys
from pathlib import Path
def get_target_triple() -> str:
"""Get the Rust-style target triple for the current platform."""
system = platform.system().lower()
machine = platform.machine().lower()
# Map machine architectures
arch_map = {
"x86_64": "x86_64",
"amd64": "x86_64",
"aarch64": "aarch64",
"arm64": "aarch64",
"i686": "i686",
"i386": "i686",
}
arch = arch_map.get(machine, machine)
# Map OS to target triple
if system == "linux":
return f"{arch}-unknown-linux-gnu"
elif system == "darwin":
return f"{arch}-apple-darwin"
elif system == "windows":
return f"{arch}-pc-windows-msvc"
else:
raise ValueError(f"Unsupported platform: {system}")
def get_binary_name(target: str) -> str:
"""Get the output binary name based on target platform."""
base_name = "ciri"
if "windows" in target:
return f"{base_name}-{target}.exe"
return f"{base_name}-{target}"
def build(target: str | None = None) -> Path:
"""Build the ciri CLI binary using PyInstaller."""
src_dir = Path(__file__).parent
src_code_dir = src_dir / "ciri"
dist_dir = src_dir / "dist"
build_dir = src_dir / "build"
# Get target triple
if target is None:
target = get_target_triple()
print(f"Building ciri-copilot for target: {target}")
# Clean previous builds
if dist_dir.exists():
shutil.rmtree(dist_dir)
if build_dir.exists():
shutil.rmtree(build_dir)
# Determine binary name (Tauri expects: name-target[.exe])
binary_name = get_binary_name(target)
# Hidden imports for the langchain/langgraph ecosystem and dependencies
hidden_imports = [
# Core langchain modules
"langchain",
"langchain_core",
"langchain_core.tools",
"langchain_core.messages",
"langchain_core.runnables",
"langchain_openai",
"langchain_community",
"langchain_community.tools",
"langchain_unstructured",
"langchain_mcp_adapters",
"langchain_mcp_adapters.client",
# Langgraph
"langgraph",
"langgraph.types",
"langgraph.store",
"langgraph.store.base",
"langgraph.store.sqlite",
"langgraph.checkpoint.sqlite",
"sqlite_vec",
"langgraph.cache",
"langgraph.cache.base",
"langgraph_swarm",
# Deep agents
"deepagents",
"crawl4ai",
# CLI and UI
"typer",
"rich",
"anyio",
"httpx",
"tenacity",
"orjson",
# Other dependencies
"pydantic",
"pydantic._internal",
"pydantic._internal._model_construction",
"pydantic._internal._generate_schema",
"pydantic._internal._schema_generation_shared",
"pydantic.fields",
"pydantic_core",
"dotenv",
"python_dotenv",
"yaml",
"openai",
"duckduckgo_search",
"ddgs",
"sqlalchemy",
"structlog",
"opentelemetry",
"packaging",
"platformdirs",
# Local ciri modules
"ciri",
"ciri.db",
"ciri.subagents",
"ciri.serializers",
"ciri.utils",
"ciri.toolkit",
"ciri.middlewares",
"ciri.skills",
"ciri.prompts",
]
# PyInstaller command
cmd = [
sys.executable,
"-m",
"PyInstaller",
"--onefile",
"--name",
binary_name.replace(".exe", "").replace(
f"-{target}", ""
), # Base name without target
"--distpath",
str(dist_dir),
"--workpath",
str(build_dir),
"--specpath",
str(build_dir),
# Add all source modules
"--paths",
str(src_code_dir),
# Collect all submodules from key packages
"--collect-all",
"langchain",
"--collect-all",
"langchain_core",
"--collect-all",
"langchain_openai",
"--collect-all",
"langgraph",
"--collect-all",
"pydantic",
"--collect-all",
"pydantic_core",
"--collect-all",
"crawl4ai",
"--collect-all",
"playwright",
]
# Add all hidden imports
for hidden_import in hidden_imports:
cmd.extend(["--hidden-import", hidden_import])
# Add the main entry point
cmd.append(str(src_code_dir / "__main__.py"))
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, cwd=src_dir)
if result.returncode != 0:
print("PyInstaller build failed!")
sys.exit(1)
# Rename the output to include target triple for Tauri sidecar
output_binary = dist_dir / ("ciri.exe" if "windows" in target else "ciri")
final_binary = dist_dir / binary_name
if output_binary.exists() and output_binary != final_binary:
output_binary.rename(final_binary)
# Copy to src-tauri/binaries for Tauri sidecar
tauri_binaries_dir = src_dir.parent / "src-tauri" / "binaries"
tauri_binaries_dir.mkdir(parents=True, exist_ok=True)
dest_binary = tauri_binaries_dir / binary_name
shutil.copy2(final_binary, dest_binary)
print(f"Build complete: {final_binary}")
print(f"Copied to: {dest_binary}")
return final_binary
def main():
parser = argparse.ArgumentParser(description="Build ciri-copilot CLI binary")
parser.add_argument(
"--target",
type=str,
help="Target platform triple (auto-detected if not specified)",
)
args = parser.parse_args()
build(target=args.target)
if __name__ == "__main__":
main()