-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathbuild.js
More file actions
288 lines (260 loc) · 6.86 KB
/
Copy pathbuild.js
File metadata and controls
288 lines (260 loc) · 6.86 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env bun
/**
* Build script for Letta Code CLI
* Bundles TypeScript source into a single JavaScript file
*/
import {
cpSync,
existsSync,
readdirSync,
readFileSync,
rmSync,
statSync,
writeFileSync,
} from "node:fs";
import { dirname, join, relative } from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function walkFiles(root) {
const entries = readdirSync(root);
const files = [];
for (const entry of entries) {
const path = join(root, entry);
const stat = statSync(path);
if (stat.isDirectory()) {
files.push(...walkFiles(path));
continue;
}
files.push(path);
}
return files;
}
function toDeclarationSpecifier(fromFile, targetRoot, aliasPath) {
const targetPath = join(targetRoot, aliasPath);
const relativePath = relative(dirname(fromFile), targetPath).replaceAll(
"\\",
"/",
);
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
}
function rewriteDeclarationAliases(typesRoot) {
for (const file of walkFiles(typesRoot)) {
if (!file.endsWith(".d.ts")) {
continue;
}
const source = readFileSync(file, "utf-8");
const rewritten = source.replace(
/(["'])@\/([^"']+)\1/g,
(_match, quote, aliasPath) =>
`${quote}${toDeclarationSpecifier(file, typesRoot, aliasPath)}${quote}`,
);
if (rewritten !== source) {
writeFileSync(file, rewritten);
}
}
}
// Read version from package.json
const pkg = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8"));
const version = pkg.version;
const useMagick = Bun.env.USE_MAGICK;
const features = [];
console.log(`📦 Building Letta Code v${version}...`);
if (useMagick) {
console.log(`🪄 Using magick variant of imageResize...`);
features.push("USE_MAGICK");
}
await Bun.build({
entrypoints: ["./src/standalone-entry.ts"],
outdir: ".",
target: "node",
format: "esm",
minify: false, // Keep readable for debugging
sourcemap: "external",
naming: {
entry: "letta.js",
},
define: {
LETTA_VERSION: JSON.stringify(version),
BUILD_TIME: JSON.stringify(new Date().toISOString()),
__USE_MAGICK__: useMagick ? "true" : "false",
},
// Load text files as strings (for markdown, etc.)
loader: {
".md": "text",
".mdx": "text",
".txt": "text",
},
// Keep most native Node.js modules external to avoid bundling issues.
// grammY must stay external too: bundling its node-fetch/abort-controller
// stack into letta.js breaks Telegram startup because node-fetch rejects the
// bundled AbortSignal class during bot.init().
// But don't make `sharp` external, causes issues with global Bun-based installs
// ref: #745, #1200
external: ["ws", "@vscode/ripgrep", "node-pty", "grammy"],
features: features,
});
await Bun.build({
entrypoints: ["./src/utils/image-resize-worker.ts"],
outdir: ".",
target: "node",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "image-resize-worker.js",
},
});
// Add shebang to output file
const outputPath = join(__dirname, "letta.js");
let content = readFileSync(outputPath, "utf-8");
// Remove any existing shebang first
if (content.startsWith("#!")) {
content = content.slice(content.indexOf("\n") + 1);
}
// Patch secrets requirement back in for node build
content = content.replace(
`(()=>{throw new Error("Cannot require module "+"bun");})().secrets`,
`globalThis.Bun.secrets`,
);
const withShebang = `#!/usr/bin/env node
${content}`;
await Bun.write(outputPath, withShebang);
// Make executable
if (process.platform !== "win32") {
await Bun.$`chmod +x letta.js`;
}
await Bun.build({
entrypoints: ["./src/app-server-client.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "app-server-client.js",
},
});
await Bun.build({
entrypoints: ["./src/mcp-client.ts"],
outdir: "./dist",
target: "node",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "mcp-client.js",
},
define: {
LETTA_VERSION: JSON.stringify(version),
},
});
await Bun.build({
entrypoints: ["./src/memory-confinement.ts"],
outdir: "./dist",
target: "node",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "memory-confinement.js",
},
});
await Bun.build({
entrypoints: ["./src/app-server-client.ts"],
outdir: "./dist",
target: "node",
format: "cjs",
minify: false,
sourcemap: "external",
naming: {
entry: "app-server-client.cjs",
},
});
// Browser-safe agent creation presets (personalities, prompts, tags) for
// surfaces that create Letta Code agents through Core (e.g. the chat web app).
await Bun.build({
entrypoints: ["./src/agent-presets.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "agent-presets.js",
},
loader: {
".md": "text",
".mdx": "text",
".txt": "text",
},
});
// Pure scheduled-turn envelope contract shared by scheduler producers and
// transcript consumers.
await Bun.build({
entrypoints: ["./src/schedules.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "schedules.js",
},
});
await Bun.build({
entrypoints: ["./src/channels-public.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "channels-public.js",
},
});
await Bun.build({
entrypoints: ["./src/gateway-core.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "gateway-core.js",
},
loader: {
".md": "text",
},
});
await Bun.build({
entrypoints: ["./src/channels-slack.ts"],
outdir: "./dist",
target: "browser",
format: "esm",
minify: false,
sourcemap: "external",
naming: {
entry: "channels-slack.js",
},
});
// Copy bundled skills to skills/ directory for shipping
const bundledSkillsSrc = join(__dirname, "src/skills/builtin");
const bundledSkillsDst = join(__dirname, "skills");
if (existsSync(bundledSkillsSrc)) {
// Clean and copy
if (existsSync(bundledSkillsDst)) {
rmSync(bundledSkillsDst, { recursive: true });
}
cpSync(bundledSkillsSrc, bundledSkillsDst, { recursive: true });
console.log("📂 Copied bundled skills to skills/");
}
// Generate type declarations for wire types export
console.log("📝 Generating type declarations...");
await Bun.$`bunx tsc -p tsconfig.types.json`;
rewriteDeclarationAliases(join(__dirname, "dist/types"));
console.log(" Output: dist/types/protocol.d.ts");
console.log("✅ Build complete!");
console.log(` Output: letta.js`);
console.log(" Output: dist/app-server-client.js and .cjs");
console.log(` Size: ${(Bun.file(outputPath).size / 1024).toFixed(0)}KB`);