assistant-ui 0.0.80 → 0.0.82

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.
@@ -1,426 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import { spawn } from "node:child_process";
4
- import { sync as globSync } from "glob";
5
- import { detect } from "detect-package-manager";
6
- import { logger } from "./utils/logger";
7
-
8
- export interface CreateFromExampleOptions {
9
- skipInstall?: boolean;
10
- useNpm?: boolean;
11
- usePnpm?: boolean;
12
- useYarn?: boolean;
13
- useBun?: boolean;
14
- }
15
-
16
- const VALID_EXAMPLES = [
17
- "with-ag-ui",
18
- "with-ai-sdk-v6",
19
- "with-artifacts",
20
- "with-assistant-transport",
21
- "with-chain-of-thought",
22
- "with-cloud",
23
- "with-custom-thread-list",
24
- "with-elevenlabs-scribe",
25
- "with-external-store",
26
- "with-ffmpeg",
27
- "with-langgraph",
28
- "with-parent-id-grouping",
29
- "with-react-hook-form",
30
- "with-react-router",
31
- "with-tanstack",
32
- ];
33
-
34
- export async function createFromExample(
35
- projectDir: string,
36
- exampleName: string,
37
- opts: CreateFromExampleOptions,
38
- ): Promise<void> {
39
- // 1. Validate example name
40
- if (!VALID_EXAMPLES.includes(exampleName)) {
41
- logger.error(`Unknown example: ${exampleName}`);
42
- logger.info(`Available examples: ${VALID_EXAMPLES.join(", ")}`);
43
- process.exit(1);
44
- }
45
-
46
- const absoluteProjectDir = path.resolve(projectDir);
47
-
48
- // Check if directory already exists
49
- if (fs.existsSync(absoluteProjectDir)) {
50
- const files = fs.readdirSync(absoluteProjectDir);
51
- if (files.length > 0) {
52
- logger.error(`Directory ${projectDir} already exists and is not empty`);
53
- process.exit(1);
54
- }
55
- }
56
-
57
- logger.info(`Creating project from example: ${exampleName}`);
58
- logger.break();
59
-
60
- // 2. Download example using degit
61
- logger.step("Downloading example...");
62
- await downloadExample(exampleName, absoluteProjectDir);
63
-
64
- // 3. Transform package.json
65
- logger.step("Transforming package.json...");
66
- await transformPackageJson(absoluteProjectDir);
67
-
68
- // 4. Transform tsconfig.json
69
- logger.step("Transforming tsconfig.json...");
70
- await transformTsConfig(absoluteProjectDir);
71
-
72
- // 5. Transform CSS files (remove monorepo-specific @source directives)
73
- logger.step("Transforming CSS files...");
74
- await transformCssFiles(absoluteProjectDir);
75
-
76
- // 6. Scan for required components
77
- logger.step("Scanning for required components...");
78
- const { assistantUI, shadcnUI } =
79
- await scanRequiredComponents(absoluteProjectDir);
80
-
81
- // 7. Remove workspace components directory
82
- logger.step("Cleaning up workspace components...");
83
- await removeWorkspaceComponents(absoluteProjectDir);
84
-
85
- // 8. Install dependencies first (needed for shadcn)
86
- if (!opts.skipInstall) {
87
- logger.step("Installing dependencies...");
88
- await installDependencies(absoluteProjectDir, opts);
89
- }
90
-
91
- // 9. Install shadcn UI components (standard shadcn components like button, tooltip, etc.)
92
- // Always include "utils" since assistant-ui components import cn from @/lib/utils
93
- // and shadcn does not declare it as a registryDependency of button/tooltip/etc.
94
- if (!shadcnUI.includes("utils")) {
95
- shadcnUI.push("utils");
96
- }
97
- logger.step(`Installing shadcn UI components: ${shadcnUI.join(", ")}...`);
98
- await installShadcnComponents(absoluteProjectDir, shadcnUI);
99
-
100
- // 10. Install assistant-ui components
101
- if (assistantUI.length > 0) {
102
- logger.step(
103
- `Installing assistant-ui components: ${assistantUI.join(", ")}...`,
104
- );
105
- await installComponents(absoluteProjectDir, assistantUI, opts);
106
- }
107
-
108
- logger.break();
109
- logger.success("Project created successfully!");
110
- logger.break();
111
- logger.info("Next steps:");
112
- logger.info(` cd ${projectDir}`);
113
- if (opts.skipInstall) {
114
- logger.info(" npm install");
115
- }
116
- logger.info(" # Set up your environment variables in .env.local");
117
- logger.info(" npm run dev");
118
- }
119
-
120
- async function downloadExample(
121
- exampleName: string,
122
- destDir: string,
123
- ): Promise<void> {
124
- const degitPath = `assistant-ui/assistant-ui/examples/${exampleName}`;
125
-
126
- return new Promise((resolve, reject) => {
127
- const child = spawn("npx", ["degit", degitPath, destDir, "--force"], {
128
- stdio: "inherit",
129
- shell: true,
130
- });
131
-
132
- child.on("error", (error) => {
133
- reject(new Error(`Failed to download example: ${error.message}`));
134
- });
135
-
136
- child.on("close", (code) => {
137
- if (code !== 0) {
138
- reject(new Error(`degit exited with code ${code}`));
139
- } else {
140
- resolve();
141
- }
142
- });
143
- });
144
- }
145
-
146
- async function transformPackageJson(projectDir: string): Promise<void> {
147
- const pkgPath = path.join(projectDir, "package.json");
148
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
149
-
150
- // Remove @assistant-ui/ui (workspace-only package)
151
- if (pkg.dependencies?.["@assistant-ui/ui"]) {
152
- delete pkg.dependencies["@assistant-ui/ui"];
153
- }
154
-
155
- // Transform workspace dependencies to latest
156
- for (const depType of ["dependencies", "devDependencies"] as const) {
157
- const deps = pkg[depType];
158
- if (!deps) continue;
159
-
160
- for (const [name, version] of Object.entries(deps)) {
161
- if (String(version).includes("workspace:")) {
162
- deps[name] = "latest";
163
- }
164
- }
165
- }
166
-
167
- // Remove devDependencies that are workspace-only
168
- if (pkg.devDependencies?.["@assistant-ui/x-buildutils"]) {
169
- delete pkg.devDependencies["@assistant-ui/x-buildutils"];
170
- }
171
-
172
- // Update package name to be unique
173
- const dirName = path.basename(projectDir);
174
- pkg.name = dirName;
175
-
176
- fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
177
- }
178
-
179
- async function transformCssFiles(projectDir: string): Promise<void> {
180
- // Find all CSS files
181
- const cssFiles = globSync("**/*.css", {
182
- cwd: projectDir,
183
- ignore: ["**/node_modules/**", "**/dist/**", "**/.next/**"],
184
- });
185
-
186
- for (const file of cssFiles) {
187
- const fullPath = path.join(projectDir, file);
188
- try {
189
- let content = fs.readFileSync(fullPath, "utf-8");
190
-
191
- // Remove @source lines that point to monorepo packages directory
192
- // These are only needed in the monorepo context
193
- content = content.replace(
194
- /@source\s+["'][^"']*packages\/ui\/src[^"']*["'];\s*\n?/g,
195
- "",
196
- );
197
-
198
- fs.writeFileSync(fullPath, content);
199
- } catch {
200
- // Ignore files that cannot be read/written
201
- }
202
- }
203
- }
204
-
205
- async function transformTsConfig(projectDir: string): Promise<void> {
206
- const tsconfigPath = path.join(projectDir, "tsconfig.json");
207
-
208
- if (!fs.existsSync(tsconfigPath)) {
209
- return;
210
- }
211
-
212
- const content = fs.readFileSync(tsconfigPath, "utf-8");
213
- const tsconfig = JSON.parse(content);
214
-
215
- // Remove workspace paths
216
- if (tsconfig.compilerOptions?.paths) {
217
- delete tsconfig.compilerOptions.paths["@/components/assistant-ui/*"];
218
- delete tsconfig.compilerOptions.paths["@/components/ui/*"];
219
- delete tsconfig.compilerOptions.paths["@/lib/utils"];
220
- delete tsconfig.compilerOptions.paths["@assistant-ui/ui/*"];
221
-
222
- // If paths is empty, remove it
223
- if (Object.keys(tsconfig.compilerOptions.paths).length === 0) {
224
- delete tsconfig.compilerOptions.paths;
225
- }
226
- }
227
-
228
- // If extends uses @assistant-ui/x-buildutils, replace with inline config
229
- if (tsconfig.extends?.includes("@assistant-ui/x-buildutils")) {
230
- delete tsconfig.extends;
231
-
232
- // Add necessary compiler options that were in the extended config
233
- tsconfig.compilerOptions = {
234
- ...{
235
- target: "ES2017",
236
- lib: ["dom", "dom.iterable", "esnext"],
237
- allowJs: true,
238
- skipLibCheck: true,
239
- strict: true,
240
- noEmit: true,
241
- esModuleInterop: true,
242
- module: "esnext",
243
- moduleResolution: "bundler",
244
- resolveJsonModule: true,
245
- isolatedModules: true,
246
- jsx: "preserve",
247
- incremental: true,
248
- plugins: [{ name: "next" }],
249
- },
250
- ...tsconfig.compilerOptions,
251
- paths: {
252
- "@/*": ["./*"],
253
- ...(tsconfig.compilerOptions?.paths || {}),
254
- },
255
- };
256
- }
257
-
258
- fs.writeFileSync(tsconfigPath, `${JSON.stringify(tsconfig, null, 2)}\n`);
259
- }
260
-
261
- interface RequiredComponents {
262
- assistantUI: string[];
263
- shadcnUI: string[];
264
- }
265
-
266
- async function scanRequiredComponents(
267
- projectDir: string,
268
- ): Promise<RequiredComponents> {
269
- const files = globSync("**/*.{ts,tsx}", {
270
- cwd: projectDir,
271
- ignore: ["**/node_modules/**", "**/dist/**", "**/.next/**"],
272
- });
273
-
274
- const assistantUIComponents = new Set<string>();
275
- const shadcnUIComponents = new Set<string>();
276
-
277
- for (const file of files) {
278
- const fullPath = path.join(projectDir, file);
279
- try {
280
- const content = fs.readFileSync(fullPath, "utf-8");
281
-
282
- // Match imports from "@/components/assistant-ui/xxx"
283
- const assistantUIRegex =
284
- /from\s+["']@\/components\/assistant-ui\/([^"']+)["']/g;
285
- let match;
286
- while ((match = assistantUIRegex.exec(content)) !== null) {
287
- assistantUIComponents.add(match[1]!);
288
- }
289
-
290
- // Match imports from "@/components/ui/xxx"
291
- const uiRegex = /from\s+["']@\/components\/ui\/([^"']+)["']/g;
292
- while ((match = uiRegex.exec(content)) !== null) {
293
- shadcnUIComponents.add(match[1]!);
294
- }
295
- } catch {
296
- // Ignore files that cannot be read
297
- }
298
- }
299
-
300
- return {
301
- assistantUI: Array.from(assistantUIComponents),
302
- shadcnUI: Array.from(shadcnUIComponents),
303
- };
304
- }
305
-
306
- async function removeWorkspaceComponents(projectDir: string): Promise<void> {
307
- const componentsDir = path.join(projectDir, "components", "assistant-ui");
308
-
309
- if (fs.existsSync(componentsDir)) {
310
- fs.rmSync(componentsDir, { recursive: true });
311
- }
312
- }
313
-
314
- async function installShadcnComponents(
315
- projectDir: string,
316
- components: string[],
317
- ): Promise<void> {
318
- return new Promise((resolve, reject) => {
319
- const child = spawn(
320
- "npx",
321
- ["shadcn@latest", "add", ...components, "--yes"],
322
- {
323
- cwd: projectDir,
324
- stdio: "inherit",
325
- shell: true,
326
- },
327
- );
328
-
329
- child.on("error", (error) => {
330
- reject(
331
- new Error(`Failed to install shadcn components: ${error.message}`),
332
- );
333
- });
334
-
335
- child.on("close", (code) => {
336
- if (code !== 0) {
337
- // Don't fail if shadcn has issues, just warn
338
- logger.warn(
339
- `shadcn exited with code ${code}, components may need manual installation`,
340
- );
341
- }
342
- resolve();
343
- });
344
- });
345
- }
346
-
347
- async function installComponents(
348
- projectDir: string,
349
- components: string[],
350
- _opts: CreateFromExampleOptions,
351
- ): Promise<void> {
352
- // Format component names for shadcn registry
353
- const componentArgs = components.map((c) => `@assistant-ui/${c}`);
354
-
355
- return new Promise((resolve, reject) => {
356
- const child = spawn(
357
- "npx",
358
- ["shadcn@latest", "add", ...componentArgs, "--yes"],
359
- {
360
- cwd: projectDir,
361
- stdio: "inherit",
362
- shell: true,
363
- },
364
- );
365
-
366
- child.on("error", (error) => {
367
- reject(new Error(`Failed to install components: ${error.message}`));
368
- });
369
-
370
- child.on("close", (code) => {
371
- if (code !== 0) {
372
- // Don't fail if shadcn has issues, just warn
373
- logger.warn(
374
- `shadcn exited with code ${code}, components may need manual installation`,
375
- );
376
- }
377
- resolve();
378
- });
379
- });
380
- }
381
-
382
- async function installDependencies(
383
- projectDir: string,
384
- opts: CreateFromExampleOptions,
385
- ): Promise<void> {
386
- let pm: string;
387
-
388
- if (opts.useNpm) {
389
- pm = "npm";
390
- } else if (opts.usePnpm) {
391
- pm = "pnpm";
392
- } else if (opts.useYarn) {
393
- pm = "yarn";
394
- } else if (opts.useBun) {
395
- pm = "bun";
396
- } else {
397
- // Detect from parent directory or default to npm
398
- try {
399
- pm = await detect({ cwd: path.dirname(projectDir) });
400
- } catch {
401
- pm = "npm";
402
- }
403
- }
404
-
405
- const args = pm === "yarn" ? [] : ["install"];
406
-
407
- return new Promise((resolve, reject) => {
408
- const child = spawn(pm, args, {
409
- cwd: projectDir,
410
- stdio: "inherit",
411
- shell: true,
412
- });
413
-
414
- child.on("error", (error) => {
415
- reject(new Error(`Failed to install dependencies: ${error.message}`));
416
- });
417
-
418
- child.on("close", (code) => {
419
- if (code !== 0) {
420
- reject(new Error(`${pm} install exited with code ${code}`));
421
- } else {
422
- resolve();
423
- }
424
- });
425
- });
426
- }