@slicemachine/manager 0.25.7-alpha.jp-figma-to-slice-1.2 → 0.25.7-alpha.jp-figma-to-slice-1.3

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.
@@ -17,6 +17,7 @@ const crypto = require("node:crypto");
17
17
  const path = require("node:path");
18
18
  const fs = require("node:fs/promises");
19
19
  const claudeAgentSdk = require("@anthropic-ai/claude-agent-sdk");
20
+ const APPLICATION_MODE = require("../../constants/APPLICATION_MODE.cjs");
20
21
  function _interopNamespaceDefault(e) {
21
22
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
22
23
  if (e) {
@@ -312,6 +313,7 @@ class CustomTypesManager extends BaseManager.BaseManager {
312
313
  throw new Error("User does not have access to the LLM proxy.");
313
314
  }
314
315
  const { llmProxyUrl } = index.default.object({ llmProxyUrl: index.default.string().url() }).parse(exp.payload);
316
+ console.info({ llmProxyUrl });
315
317
  let tmpDir;
316
318
  try {
317
319
  const config = await this.project.getSliceMachineConfig();
@@ -339,163 +341,195 @@ class CustomTypesManager extends BaseManager.BaseManager {
339
341
  }
340
342
  const projectRoot = await this.project.getRoot();
341
343
  const libraryAbsPath = path.join(projectRoot, libraryID);
344
+ const cwd = libraryAbsPath;
342
345
  tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "slice-machine-infer-slice-tmp-"));
343
- const tmpImagePath = path.join(tmpDir, `${crypto.randomUUID()}.png`);
346
+ const tmpImageAbsPath = path.join(tmpDir, `${crypto.randomUUID()}.png`);
344
347
  const response = await fetch.default(imageUrl);
345
348
  if (!response.ok) {
346
349
  throw new Error(`Failed to download image: ${response.status} ${response.statusText}`);
347
350
  }
348
- await fs.writeFile(tmpImagePath, Buffer.from(await response.arrayBuffer()));
351
+ await fs.writeFile(tmpImageAbsPath, Buffer.from(await response.arrayBuffer()));
352
+ const otherSlices = (await Promise.all((await fs.readdir(libraryAbsPath, { withFileTypes: true })).flatMap(async (path$1) => {
353
+ try {
354
+ if (!path$1.isDirectory()) {
355
+ throw new Error("Not a directory");
356
+ }
357
+ const absPath = path.join(libraryAbsPath, path$1.name);
358
+ const modelAbsPath = path.join(absPath, "model.json");
359
+ if (!node_fs.existsSync(modelAbsPath)) {
360
+ throw new Error("Model file not found");
361
+ }
362
+ const decoded = customtypes.SharedSlice.decode(JSON.parse(await fs.readFile(modelAbsPath, "utf-8")));
363
+ if (decoded._tag === "Left") {
364
+ throw new Error("Invalid model file");
365
+ }
366
+ return [
367
+ {
368
+ absPath,
369
+ relPath: path.relative(cwd, absPath),
370
+ name: decoded.right.name
371
+ }
372
+ ];
373
+ } catch {
374
+ return [];
375
+ }
376
+ }))).flat();
377
+ const prompt = `CRITICAL INSTRUCTIONS - READ FIRST:
378
+ - You MUST start immediately with Step 1.1. DO NOT read, analyze, or explore any project files first.
379
+ - Work step-by-step through the numbered tasks below.
380
+ - DO NOT present any summary, explanation, or completion message after finishing.
381
+ - DO NOT create TODO lists while performing tasks.
382
+ - Keep responses minimal - only show necessary tool calls and brief progress notes.
383
+
384
+ # CONTEXT
385
+
386
+ The user wants to build a new Prismic Slice based on a design image they provided.
387
+ Your goal is to analyze the design image and generate the JSON model data and boilerplate code for the slice following Prismic requirements.
388
+
389
+ You will work under the slice library at <slice_library_directory_path>, where all the slices are stored.
390
+
391
+ # AVAILABLE RESOURCES
392
+
393
+ <framework>
394
+ ${framework.label}
395
+ </framework>
396
+
397
+ <design_image_path>
398
+ ${tmpImageAbsPath}
399
+ </design_image_path>
400
+
401
+ <slice_library_directory_path>
402
+ ${libraryAbsPath}
403
+ </slice_library_directory_path>
404
+
405
+ <disallowed_slice_names>
406
+ ${otherSlices.map((slice) => `- ${slice.name}`).join("\n")}
407
+ </disallowed_slice_names>
408
+
409
+ # AVAILABLE TOOLS
410
+
411
+ You have access to specialized Prismic MCP tools for this task:
412
+
413
+ <tool name="mcp__prismic__how_to_model_slice">
414
+ <description>
415
+ Provides detailed guidance on creating Prismic slice models, including field types, naming conventions, and best practices.
416
+ </description>
417
+ <when_to_use>
418
+ Call this tool in Step 2.1 to learn how to structure the slice model data for the design you analysed.
419
+ </when_to_use>
420
+ </tool>
421
+
422
+ <tool name="mcp__prismic__how_to_code_slice">
423
+ <description>
424
+ Provides guidance on implementing Prismic slice components, including how to use Prismic field components, props structure, and best practices.
425
+ </description>
426
+ <when_to_use>
427
+ Call this tool in Step 2.1 to learn how to properly structure the slice component with Prismic fields.
428
+ </when_to_use>
429
+ </tool>
430
+
431
+ <tool name="mcp__prismic__save_slice_data">
432
+ <description>
433
+ Validates and saves the slice model data to model.json. This is the ONLY way to create the model file.
434
+ </description>
435
+ <when_to_use>
436
+ Call this tool in Step 2.3 after you have built the complete slice model structure in memory.
437
+ </when_to_use>
438
+ </tool>
439
+
440
+ # TASK REQUIREMENTS
441
+
442
+ ## Step 1: Gather information from the design image
443
+ 1.1. Analyse the design image at <design_image_path>.
444
+ 1.2. Identify all elements in the image that should be dynamically editable (e.g., headings, paragraphs, images, links, buttons, etc.).
445
+ 1.3. Come up with a UNIQUE name for the new slice based on the content of the image, DO NOT use any of the names in <disallowed_slice_names>.
446
+
447
+ ## Step 2: Model the Prismic slice
448
+ 2.1. Call mcp__prismic__how_to_model_slice to learn how to structure the model for this design.
449
+ 2.2. Build the complete slice JSON model data in memory based on the guidance received and the information extracted from the image.
450
+ 2.3. Call mcp__prismic__save_slice_data to save the model (DO NOT manually write model.json) in the slice library at <slice_library_directory_path>.
451
+
452
+ ## Step 3: Code a boilerplate slice component based on the model
453
+ 3.1. Call mcp__prismic__how_to_code_slice to learn how to properly structure the slice component with Prismic fields.
454
+ 3.2. Update the slice component code at <slice_library_directory_path>/index.*, replacing the placeholder code with boilerplate code with the following requirements:
455
+ - Must NOT be based on existing slices or components from the codebase.
456
+ - Must render all the Prismic components to display the fields of the slice model created at <slice_model_path>.
457
+ - Must be a valid ${framework.label} component.
458
+ - Must NOT have any styling/CSS. No inlines styles or classNames. Just the skeleton component structure.
459
+ - Must NOT use any other custom component or functions from the user's codebase.
460
+ - Avoid creating unnecessary wrapper elements, like if they only wrap a single component (e.g., <div><PrismicRichText /></div>).
461
+
462
+ ## Step 4: Present the newly created slice path
463
+ 4.1. Present the path to the newly created slice in the following format: <new_slice_path>${libraryAbsPath}/MyNewSlice</new_slice_path>.
464
+ - "MyNewSlice" must be the name of the directory of the newly created slice.
465
+
466
+ # EXAMPLE OF CORRECT EXECUTION
467
+
468
+ <example>
469
+ Assistant: Step 1.1: Analysing design image...
470
+ [reads <design_image_path>]
471
+
472
+ Step 1.2: Identifying editable content elements...
473
+ [identifies: title field, description field, buttonText field, buttonLink field, backgroundImage field]
474
+
475
+ Step 1.3: Listing slice directories under <slice_library_directory_path>...
476
+ [lists slice directories: Hero, Hero2, Hero3]
477
+
478
+ Step 1.4: Coming up with a unique name for the new slice...
479
+ [comes up with a unique name for the new slice: Hero4]
480
+
481
+ Step 2.1: Getting Prismic modeling guidance...
482
+ [calls mcp__prismic__how_to_model_slice]
483
+
484
+ Step 2.2: Building slice model based on guidance and the information extracted...
485
+ [creates model with title field, description field, buttonText field, buttonLink field, backgroundImage field]
486
+
487
+ Step 2.3: Saving slice model...
488
+ [calls mcp__prismic__save_slice_data]
489
+
490
+ Step 3.1: Learning Prismic slice coding requirements...
491
+ [calls mcp__prismic__how_to_code_slice]
492
+
493
+ Step 3.2: Coding boilerplate slice component based on the model...
494
+ [updates component with Prismic field components, no styling, no other components]
495
+
496
+ Step 4.1: Presenting the path to the newly created slice...
497
+ [presents <new_slice_path>${path.join(libraryAbsPath, "MyNewSlice")}</new_slice_path>]
498
+
499
+ # DELIVERABLES
500
+ - Slice model saved to <slice_library_directory_path>/model.json using mcp__prismic__save_slice_data
501
+ - Slice component at <slice_library_directory_path>/index.* updated with boilerplate code
502
+ - New slice path presented in the format mentioned in Step 3.1
503
+
504
+ YOU ARE NOT FINISHED UNTIL YOU HAVE THESE DELIVERABLES.
505
+
506
+ ---
507
+
508
+ FINAL REMINDERS:
509
+ - You MUST use mcp__prismic__save_slice_data to save the model
510
+ - You MUST call mcp__prismic__how_to_code_slice in Step 3.1
511
+ - DO NOT ATTEMPT TO BUILD THE APPLICATION
512
+ - START IMMEDIATELY WITH STEP 1.1 - NO PRELIMINARY ANALYSIS`;
349
513
  const queries = claudeAgentSdk.query({
350
- prompt: `CRITICAL INSTRUCTIONS - READ FIRST:
351
- - You MUST start immediately with Step 1.1. DO NOT read, analyze, or explore any project files first.
352
- - Work step-by-step through the numbered tasks below.
353
- - DO NOT present any summary, explanation, or completion message after finishing.
354
- - DO NOT create TODO lists while performing tasks.
355
- - Keep responses minimal - only show necessary tool calls and brief progress notes.
356
-
357
- # CONTEXT
358
-
359
- The user wants to build a new Prismic Slice based on a design image they provided.
360
- Your goal is to analyze the design image and generate the JSON model data and boilerplate code for the slice following Prismic requirements.
361
-
362
- You will work under the slice library at <slice_library_path>, where all the slices are stored.
363
-
364
- # AVAILABLE RESOURCES
365
-
366
- <design_image_path>
367
- ${tmpImagePath}
368
- </design_image_path>
369
-
370
- <slice_library_path>
371
- ${libraryAbsPath}
372
- </slice_library_path>
373
-
374
- <framework>
375
- ${framework.label}
376
- </framework>
377
-
378
- # AVAILABLE TOOLS
379
-
380
- You have access to specialized Prismic MCP tools for this task:
381
-
382
- <tool name="mcp__prismic__how_to_model_slice">
383
- <description>
384
- Provides detailed guidance on creating Prismic slice models, including field types, naming conventions, and best practices.
385
- </description>
386
- <when_to_use>
387
- Call this tool in Step 2.1 to learn how to structure the slice model data for the design you analysed.
388
- </when_to_use>
389
- </tool>
390
-
391
- <tool name="mcp__prismic__how_to_code_slice">
392
- <description>
393
- Provides guidance on implementing Prismic slice components, including how to use Prismic field components, props structure, and best practices.
394
- </description>
395
- <when_to_use>
396
- Call this tool in Step 2.1 to learn how to properly structure the slice component with Prismic fields.
397
- </when_to_use>
398
- </tool>
399
-
400
- <tool name="mcp__prismic__save_slice_data">
401
- <description>
402
- Validates and saves the slice model data to model.json. This is the ONLY way to create the model file.
403
- </description>
404
- <when_to_use>
405
- Call this tool in Step 2.3 after you have built the complete slice model structure in memory.
406
- </when_to_use>
407
- </tool>
408
-
409
- # TASK REQUIREMENTS
410
-
411
- ## Step 1: Gather information from the design image
412
- 1.1. Analyse the design image at <design_image_path>.
413
- 1.2. Identify all elements in the image that should be dynamically editable (e.g., headings, paragraphs, images, links, buttons, etc.).
414
- 1.3. List the slice directories under <slice_library_path>.
415
- 1.4. Come up with a unique name for the new slice based on the content of the image and the slice directories.
416
-
417
- ## Step 2: Model the Prismic slice
418
- 2.1. Call mcp__prismic__how_to_model_slice to learn how to structure the model for this design.
419
- - Make sure the name you use for the new slice does not yet exist in the slice library at <slice_library_path>. If it does, use a different name.
420
- 2.2. Build the complete slice JSON model data in memory based on the guidance received and the information extracted from the image.
421
- 2.3. Call mcp__prismic__save_slice_data to save the model (DO NOT manually write model.json) in the slice library at <slice_library_path>.
422
-
423
- ## Step 3: Code a boilerplate slice component based on the model
424
- 3.1. Call mcp__prismic__how_to_code_slice to learn how to properly structure the slice component with Prismic fields.
425
- 3.2. Update the slice component code at <slice_library_path>/index.${frameworkFileExtension}, replacing the placeholder code with boilerplate code with the following requirements:
426
- - Must NOT be based on existing slices or components from the codebase.
427
- - Must render all the Prismic components to display the fields of the slice model created at <slice_model_path>.
428
- - Must be a valid ${framework.label} component.
429
- - Must NOT have any styling/CSS. No inlines styles or classNames. Just the skeleton component structure.
430
- - Must NOT use any other custom component or functions from the user's codebase.
431
- - Avoid creating unnecessary wrapper elements, like if they only wrap a single component (e.g., <div><PrismicRichText /></div>).
432
-
433
- ## Step 4: Present the newly created slice path
434
- 4.1. Present the path to the newly created slice in the following format: <new_slice_path>${libraryAbsPath}/MyNewSlice</new_slice_path>.
435
- - "MyNewSlice" must be the name of the directory of the newly created slice.
436
-
437
- # EXAMPLE OF CORRECT EXECUTION
438
-
439
- <example>
440
- Assistant: Step 1.1: Analysing design image...
441
- [reads <design_image_path>]
442
-
443
- Step 1.2: Identifying editable content elements...
444
- [identifies: title field, description field, buttonText field, buttonLink field, backgroundImage field]
445
-
446
- Step 1.3: Listing slice directories under <slice_library_path>...
447
- [lists slice directories: Hero, Hero2, Hero3]
448
-
449
- Step 1.4: Coming up with a unique name for the new slice...
450
- [comes up with a unique name for the new slice: Hero4]
451
-
452
- Step 2.1: Getting Prismic modeling guidance...
453
- [calls mcp__prismic__how_to_model_slice]
454
-
455
- Step 2.2: Building slice model based on guidance and the information extracted...
456
- [creates model with title field, description field, buttonText field, buttonLink field, backgroundImage field]
457
-
458
- Step 2.3: Saving slice model...
459
- [calls mcp__prismic__save_slice_data]
460
-
461
- Step 3.1: Learning Prismic slice coding requirements...
462
- [calls mcp__prismic__how_to_code_slice]
463
-
464
- Step 3.2: Coding boilerplate slice component based on the model...
465
- [updates component with Prismic field components, no styling, no other components]
466
-
467
- Step 4.1: Presenting the path to the newly created slice...
468
- [presents <new_slice_path>${path.join(libraryAbsPath, "MyNewSlice")}</new_slice_path>]
469
-
470
- # DELIVERABLES
471
- - Slice model saved to <slice_library_path>/model.json using mcp__prismic__save_slice_data
472
- - Slice component at <slice_library_path>/index.${frameworkFileExtension} updated with boilerplate code
473
- - New slice path presented in the format mentioned in Step 3.1
474
-
475
- YOU ARE NOT FINISHED UNTIL YOU HAVE THESE DELIVERABLES.
476
-
477
- ---
478
-
479
- FINAL REMINDERS:
480
- - You MUST use mcp__prismic__save_slice_data to save the model
481
- - You MUST call mcp__prismic__how_to_code_slice in Step 3.1
482
- - DO NOT ATTEMPT TO BUILD THE APPLICATION
483
- - START IMMEDIATELY WITH STEP 1.1 - NO PRELIMINARY ANALYSIS;`,
514
+ prompt,
484
515
  options: {
485
- cwd: libraryAbsPath,
486
- stderr: (data) => console.error("inferSlice error:" + data),
516
+ cwd,
517
+ stderr: (data) => {
518
+ if (!data.startsWith("Spawning Claude Code process")) {
519
+ console.error("inferSlice error:" + data);
520
+ }
521
+ },
487
522
  model: "claude-haiku-4-5",
488
- permissionMode: "bypassPermissions",
523
+ permissionMode: "acceptEdits",
489
524
  allowedTools: [
490
- "Bash",
525
+ `Bash(${cwd})`,
491
526
  "Read",
492
- "FileSearch",
493
527
  "Grep",
494
528
  "Glob",
495
- "Task",
496
- "Edit",
497
529
  "Write",
530
+ "Edit",
498
531
  "MultiEdit",
532
+ "FileSearch",
499
533
  "mcp__prismic__how_to_model_slice",
500
534
  "mcp__prismic__how_to_code_slice",
501
535
  "mcp__prismic__save_slice_data"
@@ -504,7 +538,12 @@ class CustomTypesManager extends BaseManager.BaseManager {
504
538
  `Edit(**/model.json)`,
505
539
  `Write(**/model.json)`,
506
540
  "Edit(**/mocks.json)",
507
- "Write(**/mocks.json)"
541
+ "Write(**/mocks.json)",
542
+ ...otherSlices.flatMap((slice) => [
543
+ `Write(${slice.relPath})`,
544
+ `Edit(${slice.relPath})`,
545
+ `MultiEdit(${slice.relPath})`
546
+ ])
508
547
  ],
509
548
  env: {
510
549
  ...process.env,
@@ -524,6 +563,9 @@ x-prismic-repository: ${repository}
524
563
  });
525
564
  let newSliceAbsPath;
526
565
  for await (const query of queries) {
566
+ if (process.env.SM_ENV !== APPLICATION_MODE.APPLICATION_MODE.Production) {
567
+ console.info(JSON.stringify(query, null, 2));
568
+ }
527
569
  switch (query.type) {
528
570
  case "result":
529
571
  if (query.subtype === "success") {
@@ -539,7 +581,7 @@ x-prismic-repository: ${repository}
539
581
  if (!model) {
540
582
  throw new Error("Could not find model for the newly created slice.");
541
583
  }
542
- await fs.rename(tmpImagePath, path.join(newSliceAbsPath, "screenshot-default.png"));
584
+ await fs.rename(tmpImageAbsPath, path.join(newSliceAbsPath, "screenshot-default.png"));
543
585
  return InferSliceResponse.parse({ slice: JSON.parse(model) });
544
586
  } finally {
545
587
  if (tmpDir && node_fs.existsSync(tmpDir)) {