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