@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.
@@ -39,9 +39,17 @@ import { CustomTypeFormat } from "./types";
39
39
  import { existsSync } from "node:fs";
40
40
  import { tmpdir } from "node:os";
41
41
  import { randomUUID } from "node:crypto";
42
- import { join as joinPath } from "node:path";
43
- import { mkdtemp, rename, rm, writeFile, readFile } from "node:fs/promises";
42
+ import { join as joinPath, relative as relativePath } from "node:path";
43
+ import {
44
+ mkdtemp,
45
+ rename,
46
+ rm,
47
+ writeFile,
48
+ readFile,
49
+ readdir,
50
+ } from "node:fs/promises";
44
51
  import { query as queryClaude } from "@anthropic-ai/claude-agent-sdk";
52
+ import { APPLICATION_MODE } from "../../constants/APPLICATION_MODE";
45
53
 
46
54
  type SliceMachineManagerReadCustomTypeLibraryReturnType = {
47
55
  ids: string[];
@@ -567,6 +575,8 @@ export class CustomTypesManager extends BaseManager {
567
575
  .object({ llmProxyUrl: z.string().url() })
568
576
  .parse(exp.payload);
569
577
 
578
+ console.info({ llmProxyUrl });
579
+
570
580
  let tmpDir: string | undefined;
571
581
  try {
572
582
  const config = await this.project.getSliceMachineConfig();
@@ -608,11 +618,12 @@ export class CustomTypesManager extends BaseManager {
608
618
 
609
619
  const projectRoot = await this.project.getRoot();
610
620
  const libraryAbsPath = joinPath(projectRoot, libraryID);
621
+ const cwd = libraryAbsPath;
611
622
 
612
623
  tmpDir = await mkdtemp(
613
624
  joinPath(tmpdir(), "slice-machine-infer-slice-tmp-"),
614
625
  );
615
- const tmpImagePath = joinPath(tmpDir, `${randomUUID()}.png`);
626
+ const tmpImageAbsPath = joinPath(tmpDir, `${randomUUID()}.png`);
616
627
  const response = await fetch(imageUrl);
617
628
  if (!response.ok) {
618
629
  throw new Error(
@@ -620,163 +631,207 @@ export class CustomTypesManager extends BaseManager {
620
631
  );
621
632
  }
622
633
  await writeFile(
623
- tmpImagePath,
634
+ tmpImageAbsPath,
624
635
  Buffer.from(await response.arrayBuffer()),
625
636
  );
626
637
 
638
+ const otherSlices = (
639
+ await Promise.all(
640
+ (await readdir(libraryAbsPath, { withFileTypes: true })).flatMap(
641
+ async (path) => {
642
+ try {
643
+ if (!path.isDirectory()) {
644
+ throw new Error("Not a directory");
645
+ }
646
+
647
+ const absPath = joinPath(libraryAbsPath, path.name);
648
+ const modelAbsPath = joinPath(absPath, "model.json");
649
+ if (!existsSync(modelAbsPath)) {
650
+ throw new Error("Model file not found");
651
+ }
652
+
653
+ const decoded = SharedSlice.decode(
654
+ JSON.parse(await readFile(modelAbsPath, "utf-8")),
655
+ );
656
+ if (decoded._tag === "Left") {
657
+ throw new Error("Invalid model file");
658
+ }
659
+
660
+ return [
661
+ {
662
+ absPath,
663
+ relPath: relativePath(cwd, absPath),
664
+ name: decoded.right.name,
665
+ },
666
+ ];
667
+ } catch {
668
+ return [];
669
+ }
670
+ },
671
+ ),
672
+ )
673
+ ).flat();
674
+
675
+ const prompt = `CRITICAL INSTRUCTIONS - READ FIRST:
676
+ - You MUST start immediately with Step 1.1. DO NOT read, analyze, or explore any project files first.
677
+ - Work step-by-step through the numbered tasks below.
678
+ - DO NOT present any summary, explanation, or completion message after finishing.
679
+ - DO NOT create TODO lists while performing tasks.
680
+ - Keep responses minimal - only show necessary tool calls and brief progress notes.
681
+
682
+ # CONTEXT
683
+
684
+ The user wants to build a new Prismic Slice based on a design image they provided.
685
+ Your goal is to analyze the design image and generate the JSON model data and boilerplate code for the slice following Prismic requirements.
686
+
687
+ You will work under the slice library at <slice_library_directory_path>, where all the slices are stored.
688
+
689
+ # AVAILABLE RESOURCES
690
+
691
+ <framework>
692
+ ${framework.label}
693
+ </framework>
694
+
695
+ <design_image_path>
696
+ ${tmpImageAbsPath}
697
+ </design_image_path>
698
+
699
+ <slice_library_directory_path>
700
+ ${libraryAbsPath}
701
+ </slice_library_directory_path>
702
+
703
+ <disallowed_slice_names>
704
+ ${otherSlices.map((slice) => `- ${slice.name}`).join("\n")}
705
+ </disallowed_slice_names>
706
+
707
+ # AVAILABLE TOOLS
708
+
709
+ You have access to specialized Prismic MCP tools for this task:
710
+
711
+ <tool name="mcp__prismic__how_to_model_slice">
712
+ <description>
713
+ Provides detailed guidance on creating Prismic slice models, including field types, naming conventions, and best practices.
714
+ </description>
715
+ <when_to_use>
716
+ Call this tool in Step 2.1 to learn how to structure the slice model data for the design you analysed.
717
+ </when_to_use>
718
+ </tool>
719
+
720
+ <tool name="mcp__prismic__how_to_code_slice">
721
+ <description>
722
+ Provides guidance on implementing Prismic slice components, including how to use Prismic field components, props structure, and best practices.
723
+ </description>
724
+ <when_to_use>
725
+ Call this tool in Step 2.1 to learn how to properly structure the slice component with Prismic fields.
726
+ </when_to_use>
727
+ </tool>
728
+
729
+ <tool name="mcp__prismic__save_slice_data">
730
+ <description>
731
+ Validates and saves the slice model data to model.json. This is the ONLY way to create the model file.
732
+ </description>
733
+ <when_to_use>
734
+ Call this tool in Step 2.3 after you have built the complete slice model structure in memory.
735
+ </when_to_use>
736
+ </tool>
737
+
738
+ # TASK REQUIREMENTS
739
+
740
+ ## Step 1: Gather information from the design image
741
+ 1.1. Analyse the design image at <design_image_path>.
742
+ 1.2. Identify all elements in the image that should be dynamically editable (e.g., headings, paragraphs, images, links, buttons, etc.).
743
+ 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>.
744
+
745
+ ## Step 2: Model the Prismic slice
746
+ 2.1. Call mcp__prismic__how_to_model_slice to learn how to structure the model for this design.
747
+ 2.2. Build the complete slice JSON model data in memory based on the guidance received and the information extracted from the image.
748
+ 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>.
749
+
750
+ ## Step 3: Code a boilerplate slice component based on the model
751
+ 3.1. Call mcp__prismic__how_to_code_slice to learn how to properly structure the slice component with Prismic fields.
752
+ 3.2. Update the slice component code at <slice_library_directory_path>/index.*, replacing the placeholder code with boilerplate code with the following requirements:
753
+ - Must NOT be based on existing slices or components from the codebase.
754
+ - Must render all the Prismic components to display the fields of the slice model created at <slice_model_path>.
755
+ - Must be a valid ${framework.label} component.
756
+ - Must NOT have any styling/CSS. No inlines styles or classNames. Just the skeleton component structure.
757
+ - Must NOT use any other custom component or functions from the user's codebase.
758
+ - Avoid creating unnecessary wrapper elements, like if they only wrap a single component (e.g., <div><PrismicRichText /></div>).
759
+
760
+ ## Step 4: Present the newly created slice path
761
+ 4.1. Present the path to the newly created slice in the following format: <new_slice_path>${libraryAbsPath}/MyNewSlice</new_slice_path>.
762
+ - "MyNewSlice" must be the name of the directory of the newly created slice.
763
+
764
+ # EXAMPLE OF CORRECT EXECUTION
765
+
766
+ <example>
767
+ Assistant: Step 1.1: Analysing design image...
768
+ [reads <design_image_path>]
769
+
770
+ Step 1.2: Identifying editable content elements...
771
+ [identifies: title field, description field, buttonText field, buttonLink field, backgroundImage field]
772
+
773
+ Step 1.3: Listing slice directories under <slice_library_directory_path>...
774
+ [lists slice directories: Hero, Hero2, Hero3]
775
+
776
+ Step 1.4: Coming up with a unique name for the new slice...
777
+ [comes up with a unique name for the new slice: Hero4]
778
+
779
+ Step 2.1: Getting Prismic modeling guidance...
780
+ [calls mcp__prismic__how_to_model_slice]
781
+
782
+ Step 2.2: Building slice model based on guidance and the information extracted...
783
+ [creates model with title field, description field, buttonText field, buttonLink field, backgroundImage field]
784
+
785
+ Step 2.3: Saving slice model...
786
+ [calls mcp__prismic__save_slice_data]
787
+
788
+ Step 3.1: Learning Prismic slice coding requirements...
789
+ [calls mcp__prismic__how_to_code_slice]
790
+
791
+ Step 3.2: Coding boilerplate slice component based on the model...
792
+ [updates component with Prismic field components, no styling, no other components]
793
+
794
+ Step 4.1: Presenting the path to the newly created slice...
795
+ [presents <new_slice_path>${joinPath(
796
+ libraryAbsPath,
797
+ "MyNewSlice",
798
+ )}</new_slice_path>]
799
+
800
+ # DELIVERABLES
801
+ - Slice model saved to <slice_library_directory_path>/model.json using mcp__prismic__save_slice_data
802
+ - Slice component at <slice_library_directory_path>/index.* updated with boilerplate code
803
+ - New slice path presented in the format mentioned in Step 3.1
804
+
805
+ YOU ARE NOT FINISHED UNTIL YOU HAVE THESE DELIVERABLES.
806
+
807
+ ---
808
+
809
+ FINAL REMINDERS:
810
+ - You MUST use mcp__prismic__save_slice_data to save the model
811
+ - You MUST call mcp__prismic__how_to_code_slice in Step 3.1
812
+ - DO NOT ATTEMPT TO BUILD THE APPLICATION
813
+ - START IMMEDIATELY WITH STEP 1.1 - NO PRELIMINARY ANALYSIS`;
814
+
627
815
  const queries = queryClaude({
628
- prompt: `CRITICAL INSTRUCTIONS - READ FIRST:
629
- - You MUST start immediately with Step 1.1. DO NOT read, analyze, or explore any project files first.
630
- - Work step-by-step through the numbered tasks below.
631
- - DO NOT present any summary, explanation, or completion message after finishing.
632
- - DO NOT create TODO lists while performing tasks.
633
- - Keep responses minimal - only show necessary tool calls and brief progress notes.
634
-
635
- # CONTEXT
636
-
637
- The user wants to build a new Prismic Slice based on a design image they provided.
638
- Your goal is to analyze the design image and generate the JSON model data and boilerplate code for the slice following Prismic requirements.
639
-
640
- You will work under the slice library at <slice_library_path>, where all the slices are stored.
641
-
642
- # AVAILABLE RESOURCES
643
-
644
- <design_image_path>
645
- ${tmpImagePath}
646
- </design_image_path>
647
-
648
- <slice_library_path>
649
- ${libraryAbsPath}
650
- </slice_library_path>
651
-
652
- <framework>
653
- ${framework.label}
654
- </framework>
655
-
656
- # AVAILABLE TOOLS
657
-
658
- You have access to specialized Prismic MCP tools for this task:
659
-
660
- <tool name="mcp__prismic__how_to_model_slice">
661
- <description>
662
- Provides detailed guidance on creating Prismic slice models, including field types, naming conventions, and best practices.
663
- </description>
664
- <when_to_use>
665
- Call this tool in Step 2.1 to learn how to structure the slice model data for the design you analysed.
666
- </when_to_use>
667
- </tool>
668
-
669
- <tool name="mcp__prismic__how_to_code_slice">
670
- <description>
671
- Provides guidance on implementing Prismic slice components, including how to use Prismic field components, props structure, and best practices.
672
- </description>
673
- <when_to_use>
674
- Call this tool in Step 2.1 to learn how to properly structure the slice component with Prismic fields.
675
- </when_to_use>
676
- </tool>
677
-
678
- <tool name="mcp__prismic__save_slice_data">
679
- <description>
680
- Validates and saves the slice model data to model.json. This is the ONLY way to create the model file.
681
- </description>
682
- <when_to_use>
683
- Call this tool in Step 2.3 after you have built the complete slice model structure in memory.
684
- </when_to_use>
685
- </tool>
686
-
687
- # TASK REQUIREMENTS
688
-
689
- ## Step 1: Gather information from the design image
690
- 1.1. Analyse the design image at <design_image_path>.
691
- 1.2. Identify all elements in the image that should be dynamically editable (e.g., headings, paragraphs, images, links, buttons, etc.).
692
- 1.3. List the slice directories under <slice_library_path>.
693
- 1.4. Come up with a unique name for the new slice based on the content of the image and the slice directories.
694
-
695
- ## Step 2: Model the Prismic slice
696
- 2.1. Call mcp__prismic__how_to_model_slice to learn how to structure the model for this design.
697
- - 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.
698
- 2.2. Build the complete slice JSON model data in memory based on the guidance received and the information extracted from the image.
699
- 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>.
700
-
701
- ## Step 3: Code a boilerplate slice component based on the model
702
- 3.1. Call mcp__prismic__how_to_code_slice to learn how to properly structure the slice component with Prismic fields.
703
- 3.2. Update the slice component code at <slice_library_path>/index.${frameworkFileExtension}, replacing the placeholder code with boilerplate code with the following requirements:
704
- - Must NOT be based on existing slices or components from the codebase.
705
- - Must render all the Prismic components to display the fields of the slice model created at <slice_model_path>.
706
- - Must be a valid ${framework.label} component.
707
- - Must NOT have any styling/CSS. No inlines styles or classNames. Just the skeleton component structure.
708
- - Must NOT use any other custom component or functions from the user's codebase.
709
- - Avoid creating unnecessary wrapper elements, like if they only wrap a single component (e.g., <div><PrismicRichText /></div>).
710
-
711
- ## Step 4: Present the newly created slice path
712
- 4.1. Present the path to the newly created slice in the following format: <new_slice_path>${libraryAbsPath}/MyNewSlice</new_slice_path>.
713
- - "MyNewSlice" must be the name of the directory of the newly created slice.
714
-
715
- # EXAMPLE OF CORRECT EXECUTION
716
-
717
- <example>
718
- Assistant: Step 1.1: Analysing design image...
719
- [reads <design_image_path>]
720
-
721
- Step 1.2: Identifying editable content elements...
722
- [identifies: title field, description field, buttonText field, buttonLink field, backgroundImage field]
723
-
724
- Step 1.3: Listing slice directories under <slice_library_path>...
725
- [lists slice directories: Hero, Hero2, Hero3]
726
-
727
- Step 1.4: Coming up with a unique name for the new slice...
728
- [comes up with a unique name for the new slice: Hero4]
729
-
730
- Step 2.1: Getting Prismic modeling guidance...
731
- [calls mcp__prismic__how_to_model_slice]
732
-
733
- Step 2.2: Building slice model based on guidance and the information extracted...
734
- [creates model with title field, description field, buttonText field, buttonLink field, backgroundImage field]
735
-
736
- Step 2.3: Saving slice model...
737
- [calls mcp__prismic__save_slice_data]
738
-
739
- Step 3.1: Learning Prismic slice coding requirements...
740
- [calls mcp__prismic__how_to_code_slice]
741
-
742
- Step 3.2: Coding boilerplate slice component based on the model...
743
- [updates component with Prismic field components, no styling, no other components]
744
-
745
- Step 4.1: Presenting the path to the newly created slice...
746
- [presents <new_slice_path>${joinPath(
747
- libraryAbsPath,
748
- "MyNewSlice",
749
- )}</new_slice_path>]
750
-
751
- # DELIVERABLES
752
- - Slice model saved to <slice_library_path>/model.json using mcp__prismic__save_slice_data
753
- - Slice component at <slice_library_path>/index.${frameworkFileExtension} updated with boilerplate code
754
- - New slice path presented in the format mentioned in Step 3.1
755
-
756
- YOU ARE NOT FINISHED UNTIL YOU HAVE THESE DELIVERABLES.
757
-
758
- ---
759
-
760
- FINAL REMINDERS:
761
- - You MUST use mcp__prismic__save_slice_data to save the model
762
- - You MUST call mcp__prismic__how_to_code_slice in Step 3.1
763
- - DO NOT ATTEMPT TO BUILD THE APPLICATION
764
- - START IMMEDIATELY WITH STEP 1.1 - NO PRELIMINARY ANALYSIS;`,
816
+ prompt,
765
817
  options: {
766
- cwd: libraryAbsPath,
767
- stderr: (data) => console.error("inferSlice error:" + data),
818
+ cwd,
819
+ stderr: (data) => {
820
+ if (!data.startsWith("Spawning Claude Code process")) {
821
+ console.error("inferSlice error:" + data);
822
+ }
823
+ },
768
824
  model: "claude-haiku-4-5",
769
- permissionMode: "bypassPermissions",
825
+ permissionMode: "acceptEdits",
770
826
  allowedTools: [
771
- "Bash",
827
+ `Bash(${cwd})`,
772
828
  "Read",
773
- "FileSearch",
774
829
  "Grep",
775
830
  "Glob",
776
- "Task",
777
- "Edit",
778
831
  "Write",
832
+ "Edit",
779
833
  "MultiEdit",
834
+ "FileSearch",
780
835
  "mcp__prismic__how_to_model_slice",
781
836
  "mcp__prismic__how_to_code_slice",
782
837
  "mcp__prismic__save_slice_data",
@@ -786,6 +841,11 @@ export class CustomTypesManager extends BaseManager {
786
841
  `Write(**/model.json)`,
787
842
  "Edit(**/mocks.json)",
788
843
  "Write(**/mocks.json)",
844
+ ...otherSlices.flatMap((slice) => [
845
+ `Write(${slice.relPath})`,
846
+ `Edit(${slice.relPath})`,
847
+ `MultiEdit(${slice.relPath})`,
848
+ ]),
789
849
  ],
790
850
  env: {
791
851
  ...process.env,
@@ -807,6 +867,9 @@ export class CustomTypesManager extends BaseManager {
807
867
  let newSliceAbsPath: string | undefined;
808
868
 
809
869
  for await (const query of queries) {
870
+ if (process.env.SM_ENV !== APPLICATION_MODE.Production) {
871
+ console.info(JSON.stringify(query, null, 2));
872
+ }
810
873
  switch (query.type) {
811
874
  case "result":
812
875
  if (query.subtype === "success") {
@@ -835,7 +898,7 @@ export class CustomTypesManager extends BaseManager {
835
898
 
836
899
  // move the screenshot image to the new slice directory
837
900
  await rename(
838
- tmpImagePath,
901
+ tmpImageAbsPath,
839
902
  joinPath(newSliceAbsPath, "screenshot-default.png"),
840
903
  );
841
904