@slicemachine/manager 0.24.4-alpha.xru-poc-ai.1 → 0.24.4-alpha.xru-slice-generation-ai-poc.1
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.
- package/dist/_node_modules/@amplitude/experiment-node-server/dist/src/local/client.cjs +1 -1
- package/dist/_node_modules/@amplitude/experiment-node-server/dist/src/local/client.js +1 -1
- package/dist/_node_modules/cross-spawn/index.cjs +1 -1
- package/dist/_node_modules/cross-spawn/index.js +1 -1
- package/dist/_node_modules/execa/lib/pipe.cjs +2 -2
- package/dist/_node_modules/execa/lib/pipe.cjs.map +1 -1
- package/dist/_node_modules/execa/lib/stream.cjs +3 -3
- package/dist/_node_modules/execa/lib/stream.cjs.map +1 -1
- package/dist/_virtual/index2.cjs +3 -4
- package/dist/_virtual/index2.cjs.map +1 -1
- package/dist/_virtual/index2.js +2 -4
- package/dist/_virtual/index2.js.map +1 -1
- package/dist/_virtual/index3.cjs +4 -3
- package/dist/_virtual/index3.cjs.map +1 -1
- package/dist/_virtual/index3.js +4 -2
- package/dist/_virtual/index3.js.map +1 -1
- package/dist/managers/project/ProjectManager.cjs +8 -8
- package/dist/managers/project/ProjectManager.cjs.map +1 -1
- package/dist/managers/slices/SlicesManager.cjs +1304 -625
- package/dist/managers/slices/SlicesManager.cjs.map +1 -1
- package/dist/managers/slices/SlicesManager.d.ts +8 -3
- package/dist/managers/slices/SlicesManager.js +1304 -625
- package/dist/managers/slices/SlicesManager.js.map +1 -1
- package/package.json +7 -4
- package/src/managers/slices/SlicesManager.ts +1604 -665
@@ -1,10 +1,13 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
|
+
const fs = require("node:fs");
|
3
4
|
const t = require("io-ts");
|
5
|
+
const OpenAI = require("openai");
|
4
6
|
const prismicCustomTypesClient = require("@prismicio/custom-types-client");
|
5
7
|
const content = require("@prismicio/types-internal/lib/content");
|
6
8
|
const diff = require("@prismicio/types-internal/lib/customtypes/diff");
|
7
9
|
const customtypes = require("@prismicio/types-internal/lib/customtypes");
|
10
|
+
const path = require("node:path");
|
8
11
|
const assertPluginsInitialized = require("../../lib/assertPluginsInitialized.cjs");
|
9
12
|
const bufferCodec = require("../../lib/bufferCodec.cjs");
|
10
13
|
const decodeHookResult = require("../../lib/decodeHookResult.cjs");
|
@@ -117,6 +120,7 @@ class SlicesManager extends BaseManager.BaseManager {
|
|
117
120
|
}
|
118
121
|
async createSlice(args) {
|
119
122
|
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
123
|
+
console.log(`component code to create NOW for ${args.model.name}:`, JSON.stringify(args));
|
120
124
|
const hookResult = await this.sliceMachinePluginRunner.callHook("slice:create", args);
|
121
125
|
const updateSliceMocksArgs = {
|
122
126
|
libraryID: args.libraryID,
|
@@ -657,681 +661,1356 @@ class SlicesManager extends BaseManager.BaseManager {
|
|
657
661
|
return { errors: customTypeReadErrors };
|
658
662
|
}
|
659
663
|
async generateSlice(args) {
|
660
|
-
var _a, _b, _c, _d, _e;
|
661
664
|
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
665
|
+
const INPUT_TOKEN_PRICE = 3e-6;
|
666
|
+
const OUTPUT_TOKEN_PRICE = 15e-6;
|
667
|
+
let totalTokens = {
|
668
|
+
modelGeneration: {
|
669
|
+
input: 0,
|
670
|
+
output: 0,
|
671
|
+
total: 0,
|
672
|
+
price: 0
|
673
|
+
},
|
674
|
+
mocksGeneration: {
|
675
|
+
input: 0,
|
676
|
+
output: 0,
|
677
|
+
total: 0,
|
678
|
+
price: 0
|
679
|
+
},
|
680
|
+
codeGeneration: {
|
681
|
+
input: 0,
|
682
|
+
output: 0,
|
683
|
+
total: 0,
|
684
|
+
price: 0
|
685
|
+
}
|
686
|
+
};
|
662
687
|
const AWS_REGION = "us-east-1";
|
663
|
-
const AWS_ACCESS_KEY_ID = process.env
|
664
|
-
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
|
688
|
+
const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY } = process.env;
|
665
689
|
if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY) {
|
666
690
|
throw new Error("AWS credentials are not set.");
|
667
691
|
}
|
668
|
-
const
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
692
|
+
const SHARED_SLICE_SCHEMA = `
|
693
|
+
/**
|
694
|
+
* Represents a Prismic Slice.
|
695
|
+
* @property {string} type - Should always be "SharedSlice".
|
696
|
+
* @property {string} id - Unique identifier for the slice in snake_case.
|
697
|
+
* @property {string} name - Human-readable name in PascalCase.
|
698
|
+
* @property {string} description - Brief explanation of the slice's purpose.
|
699
|
+
* @property {SliceVariation[]} variations - Array of variations for the slice.
|
700
|
+
*/
|
701
|
+
type PrismicSlice = {
|
702
|
+
type: "SharedSlice";
|
703
|
+
id: string;
|
704
|
+
name: string;
|
705
|
+
description: string;
|
706
|
+
variations: SliceVariation[];
|
707
|
+
};
|
677
708
|
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
name: string;
|
690
|
-
description: string;
|
691
|
-
variations: SliceVariation[];
|
692
|
-
};
|
693
|
-
|
694
|
-
/**
|
695
|
-
* Represents a variation of a Prismic Slice.
|
696
|
-
* @property {string} id - Unique identifier for the variation in snake_case.
|
697
|
-
* @property {string} name - Human-readable name in PascalCase.
|
698
|
-
* @property {string} description - Brief explanation of the variation's purpose.
|
699
|
-
* @property {string} docURL - Legacy property, required with an empty string.
|
700
|
-
* @property {string} version - Legacy property, required with the name property value.
|
701
|
-
* @property {Record<string, PrismicField>} [primary] - Primary fields defining the main content of the slice.
|
702
|
-
*/
|
703
|
-
type SliceVariation = {
|
704
|
-
id: string;
|
705
|
-
name: string;
|
706
|
-
description: string;
|
707
|
-
primary: Record<string, PrismicField>;
|
708
|
-
docURL: string;
|
709
|
-
version: string;
|
710
|
-
};
|
711
|
-
|
712
|
-
/**
|
713
|
-
* Union type representing all possible Prismic fields.
|
714
|
-
*/
|
715
|
-
type PrismicField =
|
716
|
-
| UIDField
|
717
|
-
| BooleanField
|
718
|
-
| ColorField
|
719
|
-
| DateField
|
720
|
-
| TimestampField
|
721
|
-
| NumberField
|
722
|
-
| KeyTextField
|
723
|
-
| SelectField
|
724
|
-
| StructuredTextField
|
725
|
-
| ImageField
|
726
|
-
| LinkField
|
727
|
-
| GeoPointField
|
728
|
-
| EmbedField
|
729
|
-
| GroupField;
|
730
|
-
|
731
|
-
/**
|
732
|
-
* Represents a UID Field in Prismic.
|
733
|
-
* @property {"UID"} type - Field type.
|
734
|
-
* @property {Object} config - Configuration object.
|
735
|
-
* @property {string} config.label - Label displayed in the editor.
|
736
|
-
* @property {string} [config.placeholder] - Placeholder text.
|
737
|
-
* @property {string} [config.customregex] - Custom regex for validation.
|
738
|
-
* @property {string} [config.errorMessage] - Error message for invalid input.
|
739
|
-
*/
|
740
|
-
type UIDField = {
|
741
|
-
type: "UID";
|
742
|
-
config: {
|
743
|
-
label: string;
|
744
|
-
placeholder?: string;
|
745
|
-
customregex?: string;
|
746
|
-
errorMessage?: string;
|
747
|
-
};
|
748
|
-
};
|
749
|
-
|
750
|
-
/**
|
751
|
-
* Represents a Boolean Field in Prismic.
|
752
|
-
* @property {"Boolean"} type - Field type.
|
753
|
-
* @property {Object} config - Configuration object.
|
754
|
-
* @property {string} config.label - Label displayed in the editor.
|
755
|
-
* @property {boolean} [config.default_value] - Default value (true or false).
|
756
|
-
*/
|
757
|
-
type BooleanField = {
|
758
|
-
type: "Boolean";
|
759
|
-
config: {
|
760
|
-
label: string;
|
761
|
-
default_value?: boolean;
|
762
|
-
};
|
763
|
-
};
|
764
|
-
|
765
|
-
/**
|
766
|
-
* Represents a Color Field in Prismic.
|
767
|
-
* @property {"Color"} type - Field type.
|
768
|
-
* @property {Object} config - Configuration object.
|
769
|
-
* @property {string} config.label - Label displayed in the editor.
|
770
|
-
*/
|
771
|
-
type ColorField = {
|
772
|
-
type: "Color";
|
773
|
-
config: {
|
774
|
-
label: string;
|
775
|
-
};
|
776
|
-
};
|
777
|
-
|
778
|
-
/**
|
779
|
-
* Represents a Date Field in Prismic.
|
780
|
-
* @property {"Date"} type - Field type.
|
781
|
-
* @property {Object} config - Configuration object.
|
782
|
-
* @property {string} config.label - Label displayed in the editor.
|
783
|
-
*/
|
784
|
-
type DateField = {
|
785
|
-
type: "Date";
|
786
|
-
config: {
|
787
|
-
label: string;
|
788
|
-
};
|
789
|
-
};
|
790
|
-
|
791
|
-
/**
|
792
|
-
* Represents a Timestamp Field in Prismic.
|
793
|
-
* @property {"Timestamp"} type - Field type.
|
794
|
-
* @property {Object} config - Configuration object.
|
795
|
-
* @property {string} config.label - Label displayed in the editor.
|
796
|
-
*/
|
797
|
-
type TimestampField = {
|
798
|
-
type: "Timestamp";
|
799
|
-
config: {
|
800
|
-
label: string;
|
801
|
-
};
|
802
|
-
};
|
803
|
-
|
804
|
-
/**
|
805
|
-
* Represents a Number Field in Prismic.
|
806
|
-
* @property {"Number"} type - Field type.
|
807
|
-
* @property {Object} config - Configuration object.
|
808
|
-
* @property {string} config.label - Label displayed in the editor.
|
809
|
-
* @property {string} [config.placeholder] - Placeholder text.
|
810
|
-
* @property {number} [config.min] - Minimum allowable value.
|
811
|
-
* @property {number} [config.max] - Maximum allowable value.
|
812
|
-
*/
|
813
|
-
type NumberField = {
|
814
|
-
type: "Number";
|
815
|
-
config: {
|
816
|
-
label: string;
|
817
|
-
placeholder?: string;
|
818
|
-
min?: number;
|
819
|
-
max?: number;
|
820
|
-
};
|
821
|
-
};
|
822
|
-
|
823
|
-
/**
|
824
|
-
* Represents a Key Text Field in Prismic.
|
825
|
-
* @property {"Text"} type - Field type.
|
826
|
-
* @property {Object} config - Configuration object.
|
827
|
-
* @property {string} config.label - Label displayed in the editor.
|
828
|
-
* @property {string} [config.placeholder] - Placeholder text.
|
829
|
-
*/
|
830
|
-
type KeyTextField = {
|
831
|
-
type: "Text";
|
832
|
-
config: {
|
833
|
-
label: string;
|
834
|
-
placeholder?: string;
|
835
|
-
};
|
836
|
-
};
|
837
|
-
|
838
|
-
/**
|
839
|
-
* Represents a Select Field in Prismic.
|
840
|
-
* @property {"Select"} type - Field type.
|
841
|
-
* @property {Object} config - Configuration object.
|
842
|
-
* @property {string} config.label - Label displayed in the editor.
|
843
|
-
* @property {string[]} config.options - Array of options for the select dropdown.
|
844
|
-
*/
|
845
|
-
type SelectField = {
|
846
|
-
type: "Select";
|
847
|
-
config: {
|
848
|
-
label: string;
|
849
|
-
options: string[];
|
850
|
-
};
|
851
|
-
};
|
852
|
-
|
853
|
-
/**
|
854
|
-
* Represents a Structured Text Field in Prismic.
|
855
|
-
* @property {"StructuredText"} type - Field type.
|
856
|
-
* @property {Object} config - Configuration object.
|
857
|
-
* @property {string} config.label - Label displayed in the editor.
|
858
|
-
* @property {string} [config.placeholder] - Placeholder text.
|
859
|
-
* @property {string} [config.single] - A comma-separated list of formatting options that does not allow line breaks. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
860
|
-
* @property {string} [config.multi] - A comma-separated list of formatting options, with paragraph breaks allowed. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
861
|
-
* @property {boolean} [config.allowTargetBlank] - Allows links to open in a new tab.
|
862
|
-
* @property {string[]} [config.labels] - An array of strings to define labels for custom formatting.
|
863
|
-
* @property {ImageConstraint} [config.imageConstraint] - Constraints for images within the rich text field.
|
864
|
-
*/
|
865
|
-
type StructuredTextField = {
|
866
|
-
type: "StructuredText";
|
867
|
-
config: {
|
868
|
-
label: string;
|
869
|
-
placeholder?: string;
|
870
|
-
single?: string;
|
871
|
-
multi?: string;
|
872
|
-
allowTargetBlank?: boolean;
|
873
|
-
labels?: string[];
|
874
|
-
imageConstraint?: ImageConstraint;
|
875
|
-
};
|
876
|
-
};
|
877
|
-
|
878
|
-
/**
|
879
|
-
* Represents constraints for images within a rich text field.
|
880
|
-
* @property {number} [width] - Width constraint in pixels.
|
881
|
-
* @property {number
|
882
|
-
* @property {number} [height] - Height constraint in pixels.
|
883
|
-
*/
|
884
|
-
type ImageConstraint = {
|
885
|
-
width?: number;
|
886
|
-
height?: number;
|
887
|
-
};
|
888
|
-
|
889
|
-
/**
|
890
|
-
* Represents an Image Field in Prismic.
|
891
|
-
* @property {"Image"} type - Field type.
|
892
|
-
* @property {Object} config - Configuration object.
|
893
|
-
* @property {string} config.label - Label displayed in the editor.
|
894
|
-
* @property {Object} [config.constraint] - Constraints for the image dimensions.
|
895
|
-
* @property {number} [config.constraint.width] - Width constraint.
|
896
|
-
* @property {number} [config.constraint.height] - Height constraint.
|
897
|
-
* @property {Thumbnail[]} [config.thumbnails] - Array of thumbnail configurations.
|
898
|
-
*/
|
899
|
-
type ImageField = {
|
900
|
-
type: "Image";
|
901
|
-
config: {
|
902
|
-
label: string;
|
903
|
-
constraint?: {
|
904
|
-
width?: number;
|
905
|
-
height?: number;
|
906
|
-
};
|
907
|
-
thumbnails?: Thumbnail[];
|
908
|
-
};
|
909
|
-
};
|
910
|
-
|
911
|
-
/**
|
912
|
-
* Represents a Thumbnail configuration for an Image field.
|
913
|
-
* @property {string} name - Name of the thumbnail.
|
914
|
-
* @property {number} [width] - Width of the thumbnail in pixels.
|
915
|
-
* @property {number} [height] - Height of the thumbnail in pixels.
|
916
|
-
*/
|
917
|
-
type Thumbnail = {
|
918
|
-
name: string;
|
919
|
-
width?: number;
|
920
|
-
height?: number;
|
921
|
-
};
|
922
|
-
|
923
|
-
/**
|
924
|
-
* Represents a Link Field in Prismic.
|
925
|
-
* @property {"Link"} type - Field type.
|
926
|
-
* @property {Object} config - Configuration object.
|
927
|
-
* @property {string} config.label - Label displayed in the editor.
|
928
|
-
* @property {"web" | "document" | "media"} config.select - Defines the type of link allowed.
|
929
|
-
* @property {string[]} [config.customtypes] - Defines which Prismic document types are allowed if select is "document".
|
930
|
-
*/
|
931
|
-
type LinkField = {
|
932
|
-
type: "Link";
|
933
|
-
config: {
|
934
|
-
label: string;
|
935
|
-
select: "web" | "document" | "media";
|
936
|
-
customtypes?: string[];
|
937
|
-
allowText: boolean;
|
938
|
-
};
|
939
|
-
};
|
940
|
-
|
941
|
-
/**
|
942
|
-
* Represents an Embed Field in Prismic.
|
943
|
-
* @property {"Embed"} type - Field type.
|
944
|
-
* @property {Object} config - Configuration object.
|
945
|
-
* @property {string} config.label - Label displayed in the editor.
|
946
|
-
*/
|
947
|
-
type EmbedField = {
|
948
|
-
type: "Embed";
|
949
|
-
config: {
|
950
|
-
label: string;
|
951
|
-
};
|
952
|
-
};
|
953
|
-
|
954
|
-
/**
|
955
|
-
* Represents a GeoPoint Field in Prismic.
|
956
|
-
* @property {"GeoPoint"} type - Field type.
|
957
|
-
* @property {Object} config - Configuration object.
|
958
|
-
* @property {string} config.label - Label displayed in the editor.
|
959
|
-
*/
|
960
|
-
type GeoPointField = {
|
961
|
-
type: "GeoPoint";
|
962
|
-
config: {
|
963
|
-
label: string;
|
964
|
-
};
|
965
|
-
};
|
966
|
-
|
967
|
-
/**
|
968
|
-
* Represents a Group Field (Repeatable Fields) in Prismic.
|
969
|
-
* @property {"Group"} type - Field type.
|
970
|
-
* @property {Object} config - Configuration object.
|
971
|
-
* @property {string} config.label - Label displayed in the editor.
|
972
|
-
* @property {Record<string, PrismicField>} config.fields - Defines the fields inside the group.
|
973
|
-
*/
|
974
|
-
type GroupField = {
|
975
|
-
type: "Group";
|
976
|
-
config: {
|
977
|
-
label: string;
|
978
|
-
fields: Record<string, PrismicField>;
|
979
|
-
};
|
980
|
-
};
|
981
|
-
`;
|
982
|
-
};
|
983
|
-
const buildSystemMessage = (schema, existingSlice) => {
|
984
|
-
return `
|
985
|
-
You are a world-class expert in creating Prismic Custom Type models for slices.
|
986
|
-
Your task is to generate a valid Prismic JSON model based solely on the provided description and the TypeScript schema below.
|
987
|
-
The JSON model must adhere strictly to these rules:
|
988
|
-
1. Include all necessary fields as described.
|
989
|
-
2. Use the "primary" object for all main content fields.
|
990
|
-
3. Do NOT use an "items" field. If a field represents a collection, it must be defined inside a Group field within the "primary" object.
|
991
|
-
4. Ensure that each field has appropriate placeholders, labels, and configurations.
|
992
|
-
5. If the existing slices contain fields, ensure to understand the user prompt to update the existing slice instead of creating something from scratch.
|
993
|
-
6. If you detect the user wants to add a variation, always ensure the new variation is not exactly the same as another existing one
|
994
|
-
7. Always return the full valid slice, as given in existing it should be returned in a valid format.
|
995
|
-
8. If you detect the user prompt is about creating a totally different slice, do it and discard what was given in the existing slice, BUT only if you detect if nothing related.
|
996
|
-
9. NEVER generate a Link / Button text field, ONLY the Link / Button field itself is enough. So ONE field just for the Link / Button. ENABLE "allowText" when doing that.
|
997
|
-
10. AND THE MOST ULTRA IMPORTANT THING! Output ONLY a valid JSON object without any additional commentary or formatting! ONLY JSON, I need to parse it after with JSON.parse!
|
709
|
+
/**
|
710
|
+
* Represents a variation of a Prismic Slice.
|
711
|
+
*/
|
712
|
+
type SliceVariation = {
|
713
|
+
id: string;
|
714
|
+
name: string;
|
715
|
+
description: string;
|
716
|
+
primary: Record<string, PrismicField>;
|
717
|
+
docURL: string;
|
718
|
+
version: string;
|
719
|
+
};
|
998
720
|
|
999
|
-
|
1000
|
-
|
721
|
+
/**
|
722
|
+
* Union type representing all possible Prismic fields.
|
723
|
+
*/
|
724
|
+
type PrismicField =
|
725
|
+
| UIDField
|
726
|
+
| BooleanField
|
727
|
+
| ColorField
|
728
|
+
| DateField
|
729
|
+
| TimestampField
|
730
|
+
| NumberField
|
731
|
+
| KeyTextField
|
732
|
+
| SelectField
|
733
|
+
| StructuredTextField
|
734
|
+
| ImageField
|
735
|
+
| LinkField
|
736
|
+
| GeoPointField
|
737
|
+
| EmbedField
|
738
|
+
| GroupField;
|
1001
739
|
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
740
|
+
/* Definitions for each field type follow... */
|
741
|
+
|
742
|
+
/**
|
743
|
+
* Represents a UID Field in Prismic.
|
744
|
+
* @property {"UID"} type - Field type.
|
745
|
+
* @property {Object} config - Configuration object.
|
746
|
+
* @property {string} config.label - Label displayed in the editor.
|
747
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
748
|
+
* @property {string} [config.customregex] - Custom regex for validation.
|
749
|
+
* @property {string} [config.errorMessage] - Error message for invalid input.
|
750
|
+
*/
|
751
|
+
type UIDField = {
|
752
|
+
type: "UID";
|
753
|
+
config: {
|
754
|
+
label: string;
|
755
|
+
placeholder?: string;
|
756
|
+
customregex?: string;
|
757
|
+
errorMessage?: string;
|
758
|
+
};
|
759
|
+
};
|
760
|
+
|
761
|
+
/**
|
762
|
+
* Represents a Boolean Field in Prismic.
|
763
|
+
* @property {"Boolean"} type - Field type.
|
764
|
+
* @property {Object} config - Configuration object.
|
765
|
+
* @property {string} config.label - Label displayed in the editor.
|
766
|
+
* @property {boolean} [config.default_value] - Default value (true or false).
|
767
|
+
*/
|
768
|
+
type BooleanField = {
|
769
|
+
type: "Boolean";
|
770
|
+
config: {
|
771
|
+
label: string;
|
772
|
+
default_value?: boolean;
|
773
|
+
};
|
774
|
+
};
|
775
|
+
|
776
|
+
/**
|
777
|
+
* Represents a Color Field in Prismic.
|
778
|
+
* @property {"Color"} type - Field type.
|
779
|
+
* @property {Object} config - Configuration object.
|
780
|
+
* @property {string} config.label - Label displayed in the editor.
|
781
|
+
*/
|
782
|
+
type ColorField = {
|
783
|
+
type: "Color";
|
784
|
+
config: {
|
785
|
+
label: string;
|
786
|
+
};
|
787
|
+
};
|
788
|
+
|
789
|
+
/**
|
790
|
+
* Represents a Date Field in Prismic.
|
791
|
+
* @property {"Date"} type - Field type.
|
792
|
+
* @property {Object} config - Configuration object.
|
793
|
+
* @property {string} config.label - Label displayed in the editor.
|
794
|
+
*/
|
795
|
+
type DateField = {
|
796
|
+
type: "Date";
|
797
|
+
config: {
|
798
|
+
label: string;
|
799
|
+
};
|
800
|
+
};
|
801
|
+
|
802
|
+
/**
|
803
|
+
* Represents a Timestamp Field in Prismic.
|
804
|
+
* @property {"Timestamp"} type - Field type.
|
805
|
+
* @property {Object} config - Configuration object.
|
806
|
+
* @property {string} config.label - Label displayed in the editor.
|
807
|
+
*/
|
808
|
+
type TimestampField = {
|
809
|
+
type: "Timestamp";
|
810
|
+
config: {
|
811
|
+
label: string;
|
812
|
+
};
|
813
|
+
};
|
814
|
+
|
815
|
+
/**
|
816
|
+
* Represents a Number Field in Prismic.
|
817
|
+
* @property {"Number"} type - Field type.
|
818
|
+
* @property {Object} config - Configuration object.
|
819
|
+
* @property {string} config.label - Label displayed in the editor.
|
820
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
821
|
+
* @property {number} [config.min] - Minimum allowable value.
|
822
|
+
* @property {number} [config.max] - Maximum allowable value.
|
823
|
+
*/
|
824
|
+
type NumberField = {
|
825
|
+
type: "Number";
|
826
|
+
config: {
|
827
|
+
label: string;
|
828
|
+
placeholder?: string;
|
829
|
+
min?: number;
|
830
|
+
max?: number;
|
831
|
+
};
|
832
|
+
};
|
833
|
+
|
834
|
+
/**
|
835
|
+
* Represents a Key Text Field in Prismic.
|
836
|
+
* @property {"Text"} type - Field type.
|
837
|
+
* @property {Object} config - Configuration object.
|
838
|
+
* @property {string} config.label - Label displayed in the editor.
|
839
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
840
|
+
*/
|
841
|
+
type KeyTextField = {
|
842
|
+
type: "Text";
|
843
|
+
config: {
|
844
|
+
label: string;
|
845
|
+
placeholder?: string;
|
846
|
+
};
|
847
|
+
};
|
848
|
+
|
849
|
+
/**
|
850
|
+
* Represents a Select Field in Prismic.
|
851
|
+
* @property {"Select"} type - Field type.
|
852
|
+
* @property {Object} config - Configuration object.
|
853
|
+
* @property {string} config.label - Label displayed in the editor.
|
854
|
+
* @property {string[]} config.options - Array of options for the select dropdown.
|
855
|
+
*/
|
856
|
+
type SelectField = {
|
857
|
+
type: "Select";
|
858
|
+
config: {
|
859
|
+
label: string;
|
860
|
+
options: string[];
|
861
|
+
};
|
862
|
+
};
|
863
|
+
|
864
|
+
/**
|
865
|
+
* Represents a Structured Text Field in Prismic.
|
866
|
+
* @property {"StructuredText"} type - Field type.
|
867
|
+
* @property {Object} config - Configuration object.
|
868
|
+
* @property {string} config.label - Label displayed in the editor.
|
869
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
870
|
+
* @property {string} [config.single] - A comma-separated list of formatting options that does not allow line breaks. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
871
|
+
* @property {string} [config.multi] - A comma-separated list of formatting options, with paragraph breaks allowed. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
872
|
+
* @property {boolean} [config.allowTargetBlank] - Allows links to open in a new tab.
|
873
|
+
* @property {string[]} [config.labels] - An array of strings to define labels for custom formatting.
|
874
|
+
* @property {ImageConstraint} [config.imageConstraint] - Constraints for images within the rich text field.
|
875
|
+
*/
|
876
|
+
type StructuredTextField = {
|
877
|
+
type: "StructuredText";
|
878
|
+
config: {
|
879
|
+
label: string;
|
880
|
+
placeholder?: string;
|
881
|
+
single?: string;
|
882
|
+
multi?: string;
|
883
|
+
allowTargetBlank?: boolean;
|
884
|
+
labels?: string[];
|
885
|
+
imageConstraint?: ImageConstraint;
|
886
|
+
};
|
887
|
+
};
|
888
|
+
|
889
|
+
/**
|
890
|
+
* Represents constraints for images within a rich text field.
|
891
|
+
* @property {number} [width] - Width constraint in pixels.
|
892
|
+
* @property {number
|
893
|
+
* @property {number} [height] - Height constraint in pixels.
|
894
|
+
*/
|
895
|
+
type ImageConstraint = {
|
896
|
+
width?: number;
|
897
|
+
height?: number;
|
898
|
+
};
|
899
|
+
|
900
|
+
/**
|
901
|
+
* Represents an Image Field in Prismic.
|
902
|
+
* @property {"Image"} type - Field type.
|
903
|
+
* @property {Object} config - Configuration object.
|
904
|
+
* @property {string} config.label - Label displayed in the editor.
|
905
|
+
* @property {Object} [config.constraint] - Constraints for the image dimensions.
|
906
|
+
* @property {number} [config.constraint.width] - Width constraint.
|
907
|
+
* @property {number} [config.constraint.height] - Height constraint.
|
908
|
+
* @property {Thumbnail[]} [config.thumbnails] - Array of thumbnail configurations.
|
909
|
+
*/
|
910
|
+
type ImageField = {
|
911
|
+
type: "Image";
|
912
|
+
config: {
|
913
|
+
label: string;
|
914
|
+
constraint?: {
|
915
|
+
width?: number;
|
916
|
+
height?: number;
|
917
|
+
};
|
918
|
+
thumbnails?: Thumbnail[];
|
919
|
+
};
|
920
|
+
};
|
921
|
+
|
922
|
+
/**
|
923
|
+
* Represents a Thumbnail configuration for an Image field.
|
924
|
+
* @property {string} name - Name of the thumbnail.
|
925
|
+
* @property {number} [width] - Width of the thumbnail in pixels.
|
926
|
+
* @property {number} [height] - Height of the thumbnail in pixels.
|
927
|
+
*/
|
928
|
+
type Thumbnail = {
|
929
|
+
name: string;
|
930
|
+
width?: number;
|
931
|
+
height?: number;
|
932
|
+
};
|
933
|
+
|
934
|
+
/**
|
935
|
+
* Represents a Link Field in Prismic.
|
936
|
+
* @property {"Link"} type - Field type.
|
937
|
+
* @property {Object} config - Configuration object.
|
938
|
+
* @property {string} config.label - Label displayed in the editor.
|
939
|
+
* @property {boolean} config.allowText - Enable the text field for the link.
|
940
|
+
*/
|
941
|
+
type LinkField = {
|
942
|
+
type: "Link";
|
943
|
+
config: {
|
944
|
+
label: string;
|
945
|
+
allowText: boolean;
|
946
|
+
};
|
947
|
+
};
|
948
|
+
|
949
|
+
/**
|
950
|
+
* Represents an Embed Field in Prismic.
|
951
|
+
* @property {"Embed"} type - Field type.
|
952
|
+
* @property {Object} config - Configuration object.
|
953
|
+
* @property {string} config.label - Label displayed in the editor.
|
954
|
+
*/
|
955
|
+
type EmbedField = {
|
956
|
+
type: "Embed";
|
957
|
+
config: {
|
958
|
+
label: string;
|
959
|
+
};
|
960
|
+
};
|
961
|
+
|
962
|
+
/**
|
963
|
+
* Represents a GeoPoint Field in Prismic.
|
964
|
+
* @property {"GeoPoint"} type - Field type.
|
965
|
+
* @property {Object} config - Configuration object.
|
966
|
+
* @property {string} config.label - Label displayed in the editor.
|
967
|
+
*/
|
968
|
+
type GeoPointField = {
|
969
|
+
type: "GeoPoint";
|
970
|
+
config: {
|
971
|
+
label: string;
|
972
|
+
};
|
973
|
+
};
|
974
|
+
|
975
|
+
/**
|
976
|
+
* Represents a Group Field (Repeatable Fields) in Prismic.
|
977
|
+
* @property {"Group"} type - Field type.
|
978
|
+
* @property {Object} config - Configuration object.
|
979
|
+
* @property {string} config.label - Label displayed in the editor.
|
980
|
+
* @property {Record<string, PrismicField>} config.fields - Defines the fields inside the group.
|
981
|
+
*/
|
982
|
+
type GroupField = {
|
983
|
+
type: "Group";
|
984
|
+
config: {
|
985
|
+
label: string;
|
986
|
+
fields: Record<string, PrismicField>;
|
987
|
+
};
|
988
|
+
};
|
989
|
+
`;
|
990
|
+
async function generateSliceModel(client, existingSlice, imageFile) {
|
991
|
+
var _a, _b, _c, _d, _e;
|
992
|
+
const systemPrompt = `
|
993
|
+
You are an expert in Prismic content modeling. Using the image provided, generate a valid Prismic JSON model for the slice described below. Follow these rules precisely:
|
994
|
+
- Use the TypeScript schema provided as your reference.
|
995
|
+
- Place all main content fields under the "primary" object.
|
996
|
+
- Do not create any collections or groups for single-image content (background images should be a single image field).
|
997
|
+
- Ensure that each field has appropriate placeholders, labels, and configurations.
|
998
|
+
- Never generate a Link / Button text field, only the Link / Button field itself is enough. Just enable "allowText" when doing that.
|
999
|
+
- Do not forget any field visible from the image provide in the user prompt.
|
1000
|
+
- Ensure to differentiate Prismic fields from just an image with visual inside the image. When that's the case, just add a Prismic image field.
|
1001
|
+
- Do not include any decorative fields.
|
1002
|
+
- Do not include any extra commentary or formatting.
|
1003
|
+
|
1004
|
+
!IMPORTANT!:
|
1005
|
+
- Only return a valid JSON object representing the full slice model, nothing else before. JSON.parse on your response should not throw an error.
|
1006
|
+
- All your response should fit in a single return response.
|
1007
|
+
|
1008
|
+
Reference Schema:
|
1009
|
+
${SHARED_SLICE_SCHEMA}
|
1010
|
+
|
1011
|
+
Existing Slice:
|
1012
|
+
${JSON.stringify(existingSlice)}
|
1013
|
+
`.trim();
|
1010
1014
|
const command = new clientBedrockRuntime.ConverseCommand({
|
1011
|
-
modelId: "anthropic.claude-3-5-sonnet-
|
1012
|
-
system: [
|
1013
|
-
{
|
1014
|
-
text: systemMessage + (args.imageFile ? `
|
1015
|
-
### Additional important rules
|
1016
|
-
A. SUPER IMPORTANT, you have to check and read the image and generate the model according to what is visible.
|
1017
|
-
B. You MUST understand that background image are just a simple image field. Don't try to do anything fancy with it, just a simple image field.
|
1018
|
-
C. Even if the background image looks like two image, be smart and detect if it's actually just one simple background that don't need a GROUP!
|
1019
|
-
D. NEVER EVER ADD decorative elements as detailled fields
|
1020
|
-
` : ``)
|
1021
|
-
}
|
1022
|
-
],
|
1015
|
+
modelId: "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
|
1016
|
+
system: [{ text: systemPrompt }],
|
1023
1017
|
messages: [
|
1024
|
-
|
1018
|
+
{
|
1025
1019
|
role: "user",
|
1026
1020
|
content: [
|
1027
1021
|
{
|
1028
|
-
image: {
|
1029
|
-
format: "png",
|
1030
|
-
source: { bytes: args.imageFile }
|
1031
|
-
}
|
1022
|
+
image: { format: "png", source: { bytes: imageFile } }
|
1032
1023
|
}
|
1033
1024
|
]
|
1034
|
-
}
|
1025
|
+
}
|
1026
|
+
]
|
1027
|
+
});
|
1028
|
+
const response = await client.send(command);
|
1029
|
+
console.log("Generated model response:", JSON.stringify(response));
|
1030
|
+
if (!response.usage || !response.usage.inputTokens || !response.usage.outputTokens || !response.usage.totalTokens) {
|
1031
|
+
throw new Error("No usage data was returned.");
|
1032
|
+
}
|
1033
|
+
totalTokens.modelGeneration = {
|
1034
|
+
input: response.usage.inputTokens,
|
1035
|
+
output: response.usage.outputTokens,
|
1036
|
+
total: response.usage.totalTokens,
|
1037
|
+
price: response.usage.inputTokens * INPUT_TOKEN_PRICE + response.usage.outputTokens * OUTPUT_TOKEN_PRICE
|
1038
|
+
};
|
1039
|
+
const resultText = (_e = (_d = (_c = (_b = (_a = response.output) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) == null ? void 0 : _e.trim();
|
1040
|
+
if (!resultText) {
|
1041
|
+
throw new Error("No valid slice model was generated.");
|
1042
|
+
}
|
1043
|
+
try {
|
1044
|
+
const generatedModel = JSON.parse(resultText);
|
1045
|
+
const updatedSlice = {
|
1046
|
+
...args.slice,
|
1047
|
+
variations: generatedModel.variations
|
1048
|
+
};
|
1049
|
+
return updatedSlice;
|
1050
|
+
} catch (error) {
|
1051
|
+
throw new Error("Failed to parse AI response for model: " + error);
|
1052
|
+
}
|
1053
|
+
}
|
1054
|
+
async function generateSliceMocks(client, imageFile, existingMocks) {
|
1055
|
+
var _a, _b, _c, _d, _e;
|
1056
|
+
const systemPrompt = `
|
1057
|
+
You are a seasoned frontend engineer with deep expertise in Prismic slices.
|
1058
|
+
Your task is to update the provided mocks template based solely on the visible content in the image.
|
1059
|
+
Follow these guidelines strictly:
|
1060
|
+
- Do not modify the overall structure of the mocks template.
|
1061
|
+
- Strictly only update text content.
|
1062
|
+
- Do not touch images.
|
1063
|
+
- If you see a repetition with a group, you must create the same number of group items that are visible on the image.
|
1064
|
+
|
1065
|
+
!IMPORTANT!:
|
1066
|
+
- Only return a valid JSON object for mocks, nothing else before. JSON.parse on your response should not throw an error.
|
1067
|
+
- All your response should fit in a single return response.
|
1068
|
+
|
1069
|
+
Existing Mocks Template:
|
1070
|
+
${JSON.stringify(existingMocks, null, 2)}
|
1071
|
+
`.trim();
|
1072
|
+
const command = new clientBedrockRuntime.ConverseCommand({
|
1073
|
+
modelId: "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
|
1074
|
+
system: [{ text: systemPrompt }],
|
1075
|
+
messages: [
|
1076
|
+
{
|
1035
1077
|
role: "user",
|
1036
1078
|
content: [
|
1037
|
-
{
|
1038
|
-
|
1079
|
+
{ image: { format: "png", source: { bytes: imageFile } } }
|
1080
|
+
]
|
1081
|
+
}
|
1082
|
+
]
|
1083
|
+
});
|
1084
|
+
const response = await client.send(command);
|
1085
|
+
console.log("Generated mocks response:", JSON.stringify(response));
|
1086
|
+
if (!response.usage || !response.usage.inputTokens || !response.usage.outputTokens || !response.usage.totalTokens) {
|
1087
|
+
throw new Error("No usage data was returned.");
|
1088
|
+
}
|
1089
|
+
totalTokens.mocksGeneration = {
|
1090
|
+
input: response.usage.inputTokens,
|
1091
|
+
output: response.usage.outputTokens,
|
1092
|
+
total: response.usage.totalTokens,
|
1093
|
+
price: response.usage.inputTokens * INPUT_TOKEN_PRICE + response.usage.outputTokens * OUTPUT_TOKEN_PRICE
|
1094
|
+
};
|
1095
|
+
const resultText = (_e = (_d = (_c = (_b = (_a = response.output) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) == null ? void 0 : _e.trim();
|
1096
|
+
if (!resultText) {
|
1097
|
+
throw new Error("No valid mocks were generated.");
|
1098
|
+
}
|
1099
|
+
try {
|
1100
|
+
const updatedMocks = JSON.parse(resultText);
|
1101
|
+
return updatedMocks;
|
1102
|
+
} catch (error) {
|
1103
|
+
throw new Error("Failed to parse AI response for mocks: " + error);
|
1104
|
+
}
|
1105
|
+
}
|
1106
|
+
async function generateSliceComponentCode(client, imageFile, existingMocks) {
|
1107
|
+
var _a, _b, _c, _d, _e;
|
1108
|
+
const systemPrompt = `
|
1109
|
+
You are a seasoned frontend engineer with deep expertise in Prismic slices.
|
1110
|
+
Your task is to generate a fully isolated React component code for a slice based on the provided image input.
|
1111
|
+
Follow these guidelines strictly:
|
1112
|
+
- Be self-contained.
|
1113
|
+
- Your goal is to make the code visually looks as close as possible to the image from the user input.
|
1114
|
+
- Ensure that the color used for the background is the same as the image provide in the user prompt! It's better no background color than a wrong one.
|
1115
|
+
- For links, you must use PrismicNextLink and you must just pass the field, PrismicNextLink will handle the display of the link text, don't do it manually.
|
1116
|
+
- Respect the padding and margin visible in the image provide in the user prompt.
|
1117
|
+
- Respect the fonts size, color, type visible in the image provide in the user prompt.
|
1118
|
+
- Respect the colors visible in the image provide in the user prompt.
|
1119
|
+
- Respect the position of elements visible in the image provide in the user prompt.
|
1120
|
+
- Respect the size of each elements visible in the image provide in the user prompt.
|
1121
|
+
- Respect the overall proportions of the slice from the image provide in the user prompt.
|
1122
|
+
- Ensure to strictly respect what is defined on the mocks for each fields ID, do not invent or use something not in the mocks.
|
1123
|
+
- Ensure to use all fields provided in the mocks.
|
1124
|
+
- Use inline <style> (do not use <style jsx>).
|
1125
|
+
- Follow the structure provided in the code example below.
|
1039
1126
|
|
1040
|
-
|
1041
|
-
|
1127
|
+
!IMPORTANT!:
|
1128
|
+
- Only return a valid JSON object with two keys: "mocks" and "componentCode", nothing else before. JSON.parse on your response should not throw an error.
|
1129
|
+
- All your response should fit in a single return response.
|
1130
|
+
|
1131
|
+
## Example of a Fully Isolated Slice Component:
|
1132
|
+
-----------------------------------------------------------
|
1133
|
+
import { type Content } from "@prismicio/client";
|
1134
|
+
import { PrismicNextLink, PrismicNextImage } from "@prismicio/next";
|
1135
|
+
import { SliceComponentProps, PrismicRichText } from "@prismicio/react";
|
1136
|
+
|
1137
|
+
export type HeroProps = SliceComponentProps<Content.HeroSlice>;
|
1138
|
+
|
1139
|
+
const Hero = ({ slice }: HeroProps): JSX.Element => {
|
1140
|
+
return (
|
1141
|
+
<section
|
1142
|
+
data-slice-type={slice.slice_type}
|
1143
|
+
data-slice-variation={slice.variation}
|
1144
|
+
className="hero"
|
1145
|
+
>
|
1146
|
+
<div className="hero__content">
|
1147
|
+
<div className="hero__image-wrapper">
|
1148
|
+
<PrismicNextImage field={slice.primary.image} className="hero__image" />
|
1149
|
+
</div>
|
1150
|
+
<div className="hero__text">
|
1151
|
+
<PrismicRichText field={slice.primary.title} />
|
1152
|
+
<PrismicRichText field={slice.primary.description} />
|
1153
|
+
<PrismicNextLink field={slice.primary.link} />
|
1154
|
+
</div>
|
1155
|
+
</div>
|
1156
|
+
<style>
|
1157
|
+
{\`
|
1158
|
+
.hero { display: flex; flex-direction: row; padding: 20px; }
|
1159
|
+
.hero__content { width: 100%; }
|
1160
|
+
.hero__image-wrapper { flex: 1; }
|
1161
|
+
.hero__text { flex: 1; padding-left: 20px; }
|
1162
|
+
\`}
|
1163
|
+
</style>
|
1164
|
+
</section>
|
1165
|
+
);
|
1166
|
+
};
|
1167
|
+
|
1168
|
+
export default Hero;
|
1169
|
+
-----------------------------------------------------------
|
1170
|
+
Existing Mocks Template:
|
1171
|
+
${JSON.stringify(existingMocks, null, 2)}
|
1172
|
+
`.trim();
|
1173
|
+
const command = new clientBedrockRuntime.ConverseCommand({
|
1174
|
+
modelId: "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
|
1175
|
+
system: [{ text: systemPrompt }],
|
1176
|
+
messages: [
|
1177
|
+
{
|
1178
|
+
role: "user",
|
1179
|
+
content: [
|
1180
|
+
{ image: { format: "png", source: { bytes: imageFile } } }
|
1042
1181
|
]
|
1043
1182
|
}
|
1044
1183
|
]
|
1045
1184
|
});
|
1185
|
+
const response = await client.send(command);
|
1186
|
+
console.log("Generated component code response:", JSON.stringify(response));
|
1187
|
+
if (!response.usage || !response.usage.inputTokens || !response.usage.outputTokens || !response.usage.totalTokens) {
|
1188
|
+
throw new Error("No usage data was returned.");
|
1189
|
+
}
|
1190
|
+
totalTokens.codeGeneration = {
|
1191
|
+
input: response.usage.inputTokens,
|
1192
|
+
output: response.usage.outputTokens,
|
1193
|
+
total: response.usage.totalTokens,
|
1194
|
+
price: response.usage.inputTokens * INPUT_TOKEN_PRICE + response.usage.outputTokens * OUTPUT_TOKEN_PRICE
|
1195
|
+
};
|
1196
|
+
const resultText = (_e = (_d = (_c = (_b = (_a = response.output) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) == null ? void 0 : _e.trim();
|
1197
|
+
if (!resultText) {
|
1198
|
+
throw new Error("No valid slice component code was generated.");
|
1199
|
+
}
|
1046
1200
|
try {
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
const result = (_e2 = (_d2 = (_c2 = (_b2 = (_a2 = response2.output) == null ? void 0 : _a2.message) == null ? void 0 : _b2.content) == null ? void 0 : _c2[0]) == null ? void 0 : _d2.text) == null ? void 0 : _e2.trim();
|
1051
|
-
console.log("After sending command");
|
1052
|
-
if (!result) {
|
1053
|
-
throw new Error("No valid response received.");
|
1201
|
+
const parsed = JSON.parse(resultText);
|
1202
|
+
if (!parsed.componentCode) {
|
1203
|
+
throw new Error("Missing key 'componentCode' in AI response.");
|
1054
1204
|
}
|
1055
|
-
return
|
1205
|
+
return parsed.componentCode;
|
1056
1206
|
} catch (error) {
|
1057
|
-
|
1058
|
-
throw error;
|
1207
|
+
throw new Error("Failed to parse AI response for component code: " + error);
|
1059
1208
|
}
|
1060
|
-
};
|
1061
|
-
const prismicModel = await generatePrismicModel(args.userPrompt);
|
1062
|
-
console.log("After saving slice", prismicModel);
|
1063
|
-
const newSlice = {
|
1064
|
-
...args.slice,
|
1065
|
-
variations: JSON.parse(prismicModel).variations
|
1066
|
-
};
|
1067
|
-
const response = await this.updateSlice({
|
1068
|
-
libraryID: args.libraryID,
|
1069
|
-
model: newSlice
|
1070
|
-
});
|
1071
|
-
if (response.errors.length) {
|
1072
|
-
return {
|
1073
|
-
errors: response.errors
|
1074
|
-
};
|
1075
1209
|
}
|
1210
|
+
const bedrockClient = new clientBedrockRuntime.BedrockRuntimeClient({
|
1211
|
+
region: AWS_REGION,
|
1212
|
+
credentials: {
|
1213
|
+
accessKeyId: AWS_ACCESS_KEY_ID,
|
1214
|
+
secretAccessKey: AWS_SECRET_ACCESS_KEY
|
1215
|
+
}
|
1216
|
+
});
|
1076
1217
|
try {
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1218
|
+
console.log("STEP 1: Generate the slice model using the image.");
|
1219
|
+
const updatedSlice = await generateSliceModel(bedrockClient, args.slice, args.imageFile);
|
1220
|
+
console.log("STEP 2: Persist the updated slice model.");
|
1221
|
+
await this.updateSlice({
|
1222
|
+
libraryID: args.libraryID,
|
1223
|
+
model: updatedSlice
|
1224
|
+
});
|
1225
|
+
console.log("STEP 3: Update the slice screenshot.");
|
1226
|
+
await this.updateSliceScreenshot({
|
1227
|
+
libraryID: args.libraryID,
|
1228
|
+
sliceID: updatedSlice.id,
|
1229
|
+
variationID: updatedSlice.variations[0].id,
|
1230
|
+
data: Buffer.from(args.imageFile)
|
1231
|
+
});
|
1232
|
+
console.log("STEP 4: Generate updated mocks.");
|
1233
|
+
const existingMocks = mockSlice.mockSlice({ model: updatedSlice });
|
1234
|
+
const updatedMocks = await generateSliceMocks(bedrockClient, args.imageFile, existingMocks);
|
1235
|
+
console.log("STEP 5: Generate updated component code.");
|
1236
|
+
const componentCode = await generateSliceComponentCode(bedrockClient, args.imageFile, existingMocks);
|
1237
|
+
console.log("STEP 6: Update the slice code.");
|
1238
|
+
await this.createSlice({
|
1239
|
+
libraryID: args.libraryID,
|
1240
|
+
model: updatedSlice,
|
1241
|
+
componentContents: componentCode
|
1242
|
+
});
|
1243
|
+
console.log("STEP 7: Persist the generated mocks.");
|
1244
|
+
await this.updateSliceMocks({
|
1245
|
+
libraryID: args.libraryID,
|
1246
|
+
sliceID: args.slice.id,
|
1247
|
+
mocks: updatedMocks
|
1248
|
+
});
|
1249
|
+
console.log("Tokens used:", totalTokens);
|
1250
|
+
const totalPrice = Object.values(totalTokens).reduce((acc, { price }) => acc + price, 0);
|
1251
|
+
console.log("Total price:", totalPrice);
|
1252
|
+
return { slice: updatedSlice };
|
1253
|
+
} catch (error) {
|
1254
|
+
console.error("Failed to generate slice:", error);
|
1255
|
+
throw new Error("Failed to generate slice: " + error);
|
1256
|
+
}
|
1257
|
+
}
|
1258
|
+
async generateSlicesFromUrl(args) {
|
1259
|
+
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
1260
|
+
const { OPENAI_API_KEY } = process.env;
|
1261
|
+
if (!OPENAI_API_KEY) {
|
1262
|
+
throw new Error("OPENAI_API_KEY is not set.");
|
1263
|
+
}
|
1264
|
+
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
1265
|
+
const sliceMachineConfig = await this.project.getSliceMachineConfig();
|
1266
|
+
const libraryIDs = sliceMachineConfig.libraries || [];
|
1267
|
+
const DEFAULT_LIBRARY_ID = libraryIDs[0];
|
1268
|
+
const KNOWNED_WEBSITE_URLS = {
|
1269
|
+
"https://www.criteo.com/": "./resources/criteo/homepage",
|
1270
|
+
"https://www.brevo.com/landing/email-marketing-service/": "./resources/brevo/mail"
|
1271
|
+
};
|
1272
|
+
async function readImagesFromFolder(folderPath) {
|
1273
|
+
console.log(process.cwd());
|
1274
|
+
const files = await fs.promises.readdir(folderPath);
|
1275
|
+
const images = await Promise.all(files.map(async (file) => {
|
1276
|
+
const buffer = await fs.promises.readFile(path.join(folderPath, file));
|
1277
|
+
return new Uint8Array(buffer);
|
1278
|
+
}));
|
1279
|
+
return images;
|
1280
|
+
}
|
1281
|
+
async function readCodeFromFolder(folderPath) {
|
1282
|
+
const files = await fs.promises.readdir(folderPath);
|
1283
|
+
const codes = await Promise.all(files.map(async (file) => {
|
1284
|
+
const buffer = await fs.promises.readFile(path.join(folderPath, file), "utf-8");
|
1285
|
+
return buffer.toString();
|
1286
|
+
}));
|
1287
|
+
return codes;
|
1288
|
+
}
|
1289
|
+
async function getGlobalStyle(filePath) {
|
1290
|
+
const buffer = await fs.promises.readFile(filePath).catch(() => Buffer.from(""));
|
1291
|
+
return buffer.toString();
|
1292
|
+
}
|
1293
|
+
const SHARED_SLICE_SCHEMA = `
|
1294
|
+
/**
|
1295
|
+
* Represents a Prismic Slice.
|
1296
|
+
* @property {string} type - Should always be "SharedSlice".
|
1297
|
+
* @property {string} id - Unique identifier for the slice in snake_case.
|
1298
|
+
* @property {string} name - Human-readable name in PascalCase.
|
1299
|
+
* @property {string} description - Brief explanation of the slice's purpose.
|
1300
|
+
* @property {SliceVariation[]} variations - Array of variations for the slice.
|
1301
|
+
*/
|
1302
|
+
type PrismicSlice = {
|
1303
|
+
type: "SharedSlice";
|
1304
|
+
id: string;
|
1305
|
+
name: string;
|
1306
|
+
description: string;
|
1307
|
+
variations: SliceVariation[];
|
1308
|
+
};
|
1100
1309
|
|
1101
|
-
|
1102
|
-
|
1310
|
+
/**
|
1311
|
+
* Represents a variation of a Prismic Slice.
|
1312
|
+
* TIPS: Never use "items" property, you can see it doesn't exist here!
|
1313
|
+
*/
|
1314
|
+
type SliceVariation = {
|
1315
|
+
id: string;
|
1316
|
+
name: string;
|
1317
|
+
description: string;
|
1318
|
+
primary: Record<string, PrismicField>;
|
1319
|
+
docURL: string;
|
1320
|
+
version: string;
|
1321
|
+
};
|
1103
1322
|
|
1104
|
-
|
1105
|
-
|
1323
|
+
/**
|
1324
|
+
* Union type representing all possible Prismic fields.
|
1325
|
+
*/
|
1326
|
+
type PrismicField =
|
1327
|
+
| UIDField
|
1328
|
+
| BooleanField
|
1329
|
+
| ColorField
|
1330
|
+
| DateField
|
1331
|
+
| TimestampField
|
1332
|
+
| NumberField
|
1333
|
+
| TextField
|
1334
|
+
| SelectField
|
1335
|
+
| StructuredTextField
|
1336
|
+
| ImageField
|
1337
|
+
| LinkField
|
1338
|
+
| GeoPointField
|
1339
|
+
| EmbedField
|
1340
|
+
| GroupField;
|
1106
1341
|
|
1107
|
-
|
1108
|
-
Generate fully isolated slice mock that don't require any package. The code is in React.
|
1109
|
-
You have to ensure the mocks data will be the input of your code.
|
1110
|
-
Use the example of a slice fully isolated from bellow to help you know how to code a slice. But use the mocks data as input and the slice name etc.
|
1111
|
-
NEVER USE <style jsx> in the final code, just <style> is enough! It's important to not use <style jsx>!
|
1112
|
-
Ensure that the color used for the background is the same as the image provide in the user prompt! It's better no background color than a wrong one.
|
1113
|
-
Your goal is to make the code visually looks as close as possible to the image from the user input.
|
1114
|
-
AND ULTRA IMPORTANT! Output ONLY a valid React code without any additional commentary or formatting! Give me the code so I can just copy paste it into a React component file.
|
1342
|
+
/* Definitions for each field type follow... */
|
1115
1343
|
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1344
|
+
/**
|
1345
|
+
* Represents a UID Field in Prismic.
|
1346
|
+
* @property {"UID"} type - Field type.
|
1347
|
+
* @property {Object} config - Configuration object.
|
1348
|
+
* @property {string} config.label - Label displayed in the editor.
|
1349
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
1350
|
+
* @property {string} [config.customregex] - Custom regex for validation.
|
1351
|
+
* @property {string} [config.errorMessage] - Error message for invalid input.
|
1352
|
+
*/
|
1353
|
+
type UIDField = {
|
1354
|
+
type: "UID";
|
1355
|
+
config: {
|
1356
|
+
label: string;
|
1357
|
+
placeholder?: string;
|
1358
|
+
customregex?: string;
|
1359
|
+
errorMessage?: string;
|
1360
|
+
};
|
1361
|
+
};
|
1119
1362
|
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1363
|
+
/**
|
1364
|
+
* Represents a Boolean Field in Prismic.
|
1365
|
+
* @property {"Boolean"} type - Field type.
|
1366
|
+
* @property {Object} config - Configuration object.
|
1367
|
+
* @property {string} config.label - Label displayed in the editor.
|
1368
|
+
* @property {boolean} [config.default_value] - Default value (true or false).
|
1369
|
+
*/
|
1370
|
+
type BooleanField = {
|
1371
|
+
type: "Boolean";
|
1372
|
+
config: {
|
1373
|
+
label: string;
|
1374
|
+
default_value?: boolean;
|
1375
|
+
};
|
1376
|
+
};
|
1124
1377
|
|
1125
|
-
|
1378
|
+
/**
|
1379
|
+
* Represents a Color Field in Prismic.
|
1380
|
+
* @property {"Color"} type - Field type.
|
1381
|
+
* @property {Object} config - Configuration object.
|
1382
|
+
* @property {string} config.label - Label displayed in the editor.
|
1383
|
+
*/
|
1384
|
+
type ColorField = {
|
1385
|
+
type: "Color";
|
1386
|
+
config: {
|
1387
|
+
label: string;
|
1388
|
+
};
|
1389
|
+
};
|
1126
1390
|
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
field={slice.primary.image}
|
1140
|
-
className="es-fullpage-hero__image"
|
1141
|
-
/>
|
1142
|
-
</div>
|
1391
|
+
/**
|
1392
|
+
* Represents a Date Field in Prismic.
|
1393
|
+
* @property {"Date"} type - Field type.
|
1394
|
+
* @property {Object} config - Configuration object.
|
1395
|
+
* @property {string} config.label - Label displayed in the editor.
|
1396
|
+
*/
|
1397
|
+
type DateField = {
|
1398
|
+
type: "Date";
|
1399
|
+
config: {
|
1400
|
+
label: string;
|
1401
|
+
};
|
1402
|
+
};
|
1143
1403
|
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1404
|
+
/**
|
1405
|
+
* Represents a Timestamp Field in Prismic.
|
1406
|
+
* @property {"Timestamp"} type - Field type.
|
1407
|
+
* @property {Object} config - Configuration object.
|
1408
|
+
* @property {string} config.label - Label displayed in the editor.
|
1409
|
+
*/
|
1410
|
+
type TimestampField = {
|
1411
|
+
type: "Timestamp";
|
1412
|
+
config: {
|
1413
|
+
label: string;
|
1414
|
+
};
|
1415
|
+
};
|
1153
1416
|
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
}
|
1173
|
-
|
1174
|
-
.es-fullpage-hero {
|
1175
|
-
font-family: system-ui, sans-serif;
|
1176
|
-
background-color: #fff;
|
1177
|
-
color: #333;
|
1178
|
-
}
|
1179
|
-
|
1180
|
-
.es-fullpage-hero__image {
|
1181
|
-
max-width: 100%;
|
1182
|
-
height: auto;
|
1183
|
-
align-self: center;
|
1184
|
-
}
|
1185
|
-
|
1186
|
-
.es-fullpage-hero__content {
|
1187
|
-
display: flex;
|
1188
|
-
flex-direction: column;
|
1189
|
-
gap: 2rem;
|
1190
|
-
}
|
1191
|
-
|
1192
|
-
.es-fullpage-hero__content-right {
|
1193
|
-
display: flex;
|
1194
|
-
flex-direction: column;
|
1195
|
-
justify-content: space-around;
|
1196
|
-
padding: 1.5rem;
|
1197
|
-
}
|
1198
|
-
|
1199
|
-
@media (min-width: 1080px) {
|
1200
|
-
.es-fullpage-hero__content {
|
1201
|
-
flex-direction: row;
|
1202
|
-
}
|
1203
|
-
|
1204
|
-
.es-fullpage-hero__content > div {
|
1205
|
-
width: 50%;
|
1206
|
-
}
|
1207
|
-
}
|
1208
|
-
|
1209
|
-
.es-fullpage-hero__content__intro {
|
1210
|
-
display: grid;
|
1211
|
-
gap: 1rem;
|
1212
|
-
}
|
1213
|
-
|
1214
|
-
.es-fullpage-hero__content__intro__eyebrow {
|
1215
|
-
color: #47C1AF;
|
1216
|
-
font-size: 1.15rem;
|
1217
|
-
font-weight: 500;
|
1218
|
-
margin: 0;
|
1219
|
-
}
|
1220
|
-
|
1221
|
-
.es-fullpage-hero__content__intro__headline {
|
1222
|
-
font-size: 1.625rem;
|
1223
|
-
font-weight: 700;
|
1224
|
-
}
|
1225
|
-
|
1226
|
-
.es-fullpage-hero__content__intro__headline * {
|
1227
|
-
margin: 0;
|
1228
|
-
}
|
1229
|
-
|
1230
|
-
@media (min-width: 640px) {
|
1231
|
-
.es-fullpage-hero__content__intro__headline {
|
1232
|
-
font-size: 2rem;
|
1233
|
-
}
|
1234
|
-
}
|
1235
|
-
|
1236
|
-
@media (min-width: 1024px) {
|
1237
|
-
.es-fullpage-hero__content__intro__headline {
|
1238
|
-
font-size: 2.5rem;
|
1239
|
-
}
|
1240
|
-
}
|
1241
|
-
|
1242
|
-
@media (min-width: 1200px) {
|
1243
|
-
.es-fullpage-hero__content__intro__headline {
|
1244
|
-
font-size: 2.75rem;
|
1245
|
-
}
|
1246
|
-
}
|
1247
|
-
|
1248
|
-
.es-fullpage-hero__content__intro__description {
|
1249
|
-
font-size: 1.15rem;
|
1250
|
-
max-width: 38rem;
|
1251
|
-
}
|
1252
|
-
|
1253
|
-
.es-fullpage-hero__content__intro__description > p {
|
1254
|
-
margin: 0;
|
1255
|
-
}
|
1256
|
-
|
1257
|
-
@media (min-width: 1200px) {
|
1258
|
-
.es-fullpage-hero__content__intro__description {
|
1259
|
-
font-size: 1.4rem;
|
1260
|
-
}
|
1261
|
-
}
|
1417
|
+
/**
|
1418
|
+
* Represents a Number Field in Prismic.
|
1419
|
+
* @property {"Number"} type - Field type.
|
1420
|
+
* @property {Object} config - Configuration object.
|
1421
|
+
* @property {string} config.label - Label displayed in the editor.
|
1422
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
1423
|
+
* @property {number} [config.min] - Minimum allowable value.
|
1424
|
+
* @property {number} [config.max] - Maximum allowable value.
|
1425
|
+
*/
|
1426
|
+
type NumberField = {
|
1427
|
+
type: "Number";
|
1428
|
+
config: {
|
1429
|
+
label: string;
|
1430
|
+
placeholder?: string;
|
1431
|
+
min?: number;
|
1432
|
+
max?: number;
|
1433
|
+
};
|
1434
|
+
};
|
1262
1435
|
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
}
|
1277
|
-
\`}
|
1278
|
-
</style>
|
1279
|
-
</section>
|
1280
|
-
);
|
1436
|
+
/**
|
1437
|
+
* Represents a Text Field in Prismic.
|
1438
|
+
* @property {"Text"} type - Field type.
|
1439
|
+
* @property {Object} config - Configuration object.
|
1440
|
+
* @property {string} config.label - Label displayed in the editor.
|
1441
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
1442
|
+
*/
|
1443
|
+
type TextField = {
|
1444
|
+
type: "Text";
|
1445
|
+
config: {
|
1446
|
+
label: string;
|
1447
|
+
placeholder?: string;
|
1448
|
+
};
|
1281
1449
|
};
|
1282
1450
|
|
1283
|
-
|
1451
|
+
/**
|
1452
|
+
* Represents a Select Field in Prismic.
|
1453
|
+
* @property {"Select"} type - Field type.
|
1454
|
+
* @property {Object} config - Configuration object.
|
1455
|
+
* @property {string} config.label - Label displayed in the editor.
|
1456
|
+
* @property {string[]} config.options - Array of options for the select dropdown.
|
1457
|
+
*/
|
1458
|
+
type SelectField = {
|
1459
|
+
type: "Select";
|
1460
|
+
config: {
|
1461
|
+
label: string;
|
1462
|
+
options: string[];
|
1463
|
+
};
|
1464
|
+
};
|
1284
1465
|
|
1466
|
+
/**
|
1467
|
+
* Represents a Structured Text Field in Prismic.
|
1468
|
+
* @property {"StructuredText"} type - Field type.
|
1469
|
+
* @property {Object} config - Configuration object.
|
1470
|
+
* @property {string} config.label - Label displayed in the editor.
|
1471
|
+
* @property {string} [config.placeholder] - Placeholder text.
|
1472
|
+
* @property {string} [config.single] - A comma-separated list of formatting options that does not allow line breaks. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
1473
|
+
* @property {string} [config.multi] - A comma-separated list of formatting options, with paragraph breaks allowed. Options: paragraph | preformatted | heading1 | heading2 | heading3 | heading4 | heading5 | heading6 | strong | em | hyperlink | image | embed | list-item | o-list-item | rtl.
|
1474
|
+
* @property {boolean} [config.allowTargetBlank] - Allows links to open in a new tab.
|
1475
|
+
* @property {string[]} [config.labels] - An array of strings to define labels for custom formatting.
|
1476
|
+
* @property {ImageConstraint} [config.imageConstraint] - Constraints for images within the rich text field.
|
1477
|
+
*/
|
1478
|
+
type StructuredTextField = {
|
1479
|
+
type: "StructuredText";
|
1480
|
+
config: {
|
1481
|
+
label: string;
|
1482
|
+
placeholder?: string;
|
1483
|
+
single?: string;
|
1484
|
+
multi?: string;
|
1485
|
+
allowTargetBlank?: boolean;
|
1486
|
+
labels?: string[];
|
1487
|
+
imageConstraint?: ImageConstraint;
|
1488
|
+
};
|
1489
|
+
};
|
1285
1490
|
|
1286
|
-
|
1287
|
-
|
1288
|
-
{
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1491
|
+
/**
|
1492
|
+
* Represents constraints for images within a rich text field.
|
1493
|
+
* @property {number} [width] - Width constraint in pixels.
|
1494
|
+
* @property {number
|
1495
|
+
* @property {number} [height] - Height constraint in pixels.
|
1496
|
+
*/
|
1497
|
+
type ImageConstraint = {
|
1498
|
+
width?: number;
|
1499
|
+
height?: number;
|
1500
|
+
};
|
1501
|
+
|
1502
|
+
/**
|
1503
|
+
* Represents an Image Field in Prismic.
|
1504
|
+
* @property {"Image"} type - Field type.
|
1505
|
+
* @property {Object} config - Configuration object.
|
1506
|
+
* @property {string} config.label - Label displayed in the editor.
|
1507
|
+
* @property {Object} [config.constraint] - Constraints for the image dimensions.
|
1508
|
+
* @property {number} [config.constraint.width] - Width constraint.
|
1509
|
+
* @property {number} [config.constraint.height] - Height constraint.
|
1510
|
+
* @property {Thumbnail[]} [config.thumbnails] - Array of thumbnail configurations.
|
1511
|
+
*/
|
1512
|
+
type ImageField = {
|
1513
|
+
type: "Image";
|
1514
|
+
config: {
|
1515
|
+
label: string;
|
1516
|
+
constraint?: {
|
1517
|
+
width?: number;
|
1518
|
+
height?: number;
|
1519
|
+
};
|
1520
|
+
thumbnails?: Thumbnail[];
|
1521
|
+
};
|
1522
|
+
};
|
1523
|
+
|
1524
|
+
/**
|
1525
|
+
* Represents a Thumbnail configuration for an Image field.
|
1526
|
+
* @property {string} name - Name of the thumbnail.
|
1527
|
+
* @property {number} [width] - Width of the thumbnail in pixels.
|
1528
|
+
* @property {number} [height] - Height of the thumbnail in pixels.
|
1529
|
+
*/
|
1530
|
+
type Thumbnail = {
|
1531
|
+
name: string;
|
1532
|
+
width?: number;
|
1533
|
+
height?: number;
|
1534
|
+
};
|
1535
|
+
|
1536
|
+
/**
|
1537
|
+
* Represents a Link Field in Prismic.
|
1538
|
+
* @property {"Link"} type - Field type.
|
1539
|
+
* @property {Object} config - Configuration object.
|
1540
|
+
* @property {string} config.label - Label displayed in the editor.
|
1541
|
+
* @property {boolean} config.allowText - Enable the text field for the link.
|
1542
|
+
*/
|
1543
|
+
type LinkField = {
|
1544
|
+
type: "Link";
|
1545
|
+
config: {
|
1546
|
+
label: string;
|
1547
|
+
allowText: boolean;
|
1548
|
+
};
|
1549
|
+
};
|
1550
|
+
|
1551
|
+
/**
|
1552
|
+
* Represents an Embed Field in Prismic.
|
1553
|
+
* @property {"Embed"} type - Field type.
|
1554
|
+
* @property {Object} config - Configuration object.
|
1555
|
+
* @property {string} config.label - Label displayed in the editor.
|
1556
|
+
*/
|
1557
|
+
type EmbedField = {
|
1558
|
+
type: "Embed";
|
1559
|
+
config: {
|
1560
|
+
label: string;
|
1561
|
+
};
|
1562
|
+
};
|
1563
|
+
|
1564
|
+
/**
|
1565
|
+
* Represents a GeoPoint Field in Prismic.
|
1566
|
+
* @property {"GeoPoint"} type - Field type.
|
1567
|
+
* @property {Object} config - Configuration object.
|
1568
|
+
* @property {string} config.label - Label displayed in the editor.
|
1569
|
+
*/
|
1570
|
+
type GeoPointField = {
|
1571
|
+
type: "GeoPoint";
|
1572
|
+
config: {
|
1573
|
+
label: string;
|
1574
|
+
};
|
1575
|
+
};
|
1576
|
+
|
1577
|
+
/**
|
1578
|
+
* Represents a Group Field (Repeatable Fields) in Prismic.
|
1579
|
+
* It CAN NEVER BE PUT INSIDE ANOTHER FIELD.
|
1580
|
+
* @property {"Group"} type - Field type.
|
1581
|
+
* @property {Object} config - Configuration object.
|
1582
|
+
* @property {string} config.label - Label displayed in the editor.
|
1583
|
+
* @property {Record<string, PrismicField>} config.fields - Defines the fields inside the group.
|
1584
|
+
*/
|
1585
|
+
type GroupField = {
|
1586
|
+
type: "Group";
|
1587
|
+
config: {
|
1588
|
+
label: string;
|
1589
|
+
fields: Record<string, PrismicField>;
|
1590
|
+
};
|
1591
|
+
};
|
1592
|
+
`;
|
1593
|
+
const DEFAULT_SLICE_MODEL = {
|
1594
|
+
id: "<ID_TO_CHANGE>",
|
1595
|
+
type: "SharedSlice",
|
1596
|
+
name: "<NAME_TO_CHANGE>",
|
1597
|
+
description: "<DESCRIPTION_TO_CHANGE>",
|
1598
|
+
variations: [
|
1599
|
+
{
|
1600
|
+
id: "<VARIATION_ID_TO_CHANGE>",
|
1601
|
+
name: "<NAME_TO_CHANGE>",
|
1602
|
+
docURL: "...",
|
1603
|
+
version: "initial",
|
1604
|
+
description: "<DESCRIPTION_TO_CHANGE>",
|
1605
|
+
imageUrl: ""
|
1606
|
+
}
|
1607
|
+
]
|
1608
|
+
};
|
1609
|
+
async function generateSliceModel(imageFile, codeFile) {
|
1610
|
+
var _a, _b, _c;
|
1611
|
+
const systemPrompt = `
|
1612
|
+
You are an expert in Prismic content modeling. Using the image and the code provided, generate a valid Prismic JSON model for the slice described below. Follow these rules precisely:
|
1613
|
+
- Use the TypeScript schema provided as your reference.
|
1614
|
+
- Place all main content fields under the "primary" object.
|
1615
|
+
- Do not create any collections or groups for single-image content (background images should be a single image field).
|
1616
|
+
- Ensure that each field has appropriate placeholders, labels, and configurations.
|
1617
|
+
- Never generate a Link / Button text field, only the Link / Button field itself is enough. Just enable "allowText" when doing that.
|
1618
|
+
- Do not forget any field visible from the image provide in the user prompt.
|
1619
|
+
- Ensure to differentiate Prismic fields from just an image with visual inside the image. When that's the case, just add a Prismic image field.
|
1620
|
+
- Use the code to know exactly what is a real field and not an image. If in the code it's an image, then the field should also be an image, do a 1-1 mapping thanks to the code.
|
1621
|
+
- Do not include any decorative fields. When an element is purely visual, decorative, don't include it att all in the slice model.
|
1622
|
+
- Do not include any extra commentary or formatting.
|
1623
|
+
- When you see a repetition of an image, a text, a link, etc, NEVER create one field per repeated item, you HAVE to use a group for that.
|
1624
|
+
- When you see multiple fields repeated, you MUST use a group for that.
|
1625
|
+
- NEVER put a group inside another group field, this is not allowed. In the final JSON a group CANNOT be within another group field. YOU CANNOT NEST GROUP FIELDS! Not for any reason you are allowed to do that! Even for navigation, in header or footer you cannot nest group fields.
|
1626
|
+
- The "items" field must not be used under any circumstances. All repeatable fields should be defined using a Group field inside the primary object. If a field represents a collection of items, it must be part of a Group field, and items must never appear in the JSON output.
|
1627
|
+
- Don't forget to replace the temporary text in the "Existing Slice to update", like <ID_TO_CHANGE>, <NAME_TO_CHANGE>, <DESCRIPTION_TO_CHANGE>, <VARIATION_ID_TO_CHANGE>, etc.
|
1628
|
+
- Field placeholder should be super short, do not put the content from the image inside the placeholder.
|
1629
|
+
- Field label and id should define the field's purpose, not its content.
|
1630
|
+
- Slice name and id should define the slice's purpose, not its content.
|
1631
|
+
- Slice description should be a brief explanation of the slice's purpose not its content.
|
1632
|
+
|
1633
|
+
!IMPORTANT!:
|
1634
|
+
- Only return a valid JSON object representing the full slice model, nothing else before. JSON.parse on your response should not throw an error.
|
1635
|
+
- All your response should fit in a single return response.
|
1636
|
+
- Never stop the response until you totally finish the full JSON response you wanted.
|
1637
|
+
|
1638
|
+
Reference Schema:
|
1639
|
+
${SHARED_SLICE_SCHEMA}
|
1640
|
+
|
1641
|
+
Existing Slice to update:
|
1642
|
+
${JSON.stringify(DEFAULT_SLICE_MODEL)}
|
1643
|
+
`.trim();
|
1644
|
+
const messages = [
|
1645
|
+
{ role: "system", content: systemPrompt },
|
1646
|
+
{
|
1647
|
+
role: "user",
|
1648
|
+
content: [
|
1297
1649
|
{
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1650
|
+
type: "image_url",
|
1651
|
+
image_url: {
|
1652
|
+
url: "data:image/png;base64," + Buffer.from(imageFile).toString("base64")
|
1653
|
+
}
|
1654
|
+
},
|
1655
|
+
{ type: "text", text: codeFile }
|
1656
|
+
]
|
1657
|
+
}
|
1658
|
+
];
|
1659
|
+
const response = await openai.chat.completions.create({
|
1660
|
+
model: "gpt-4o",
|
1661
|
+
messages,
|
1662
|
+
response_format: {
|
1663
|
+
type: "json_object"
|
1664
|
+
}
|
1665
|
+
});
|
1666
|
+
console.log("Generated model response:", JSON.stringify(response));
|
1667
|
+
const resultText = (_c = (_b = (_a = response.choices[0]) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c.trim();
|
1668
|
+
if (!resultText) {
|
1669
|
+
throw new Error("No valid slice model was generated.");
|
1670
|
+
}
|
1671
|
+
try {
|
1672
|
+
const generatedModel = JSON.parse(resultText);
|
1673
|
+
return generatedModel;
|
1674
|
+
} catch (error) {
|
1675
|
+
throw new Error("Failed to parse AI response for model: " + error);
|
1676
|
+
}
|
1677
|
+
}
|
1678
|
+
async function generateSliceMocks(imageFile, existingMocks) {
|
1679
|
+
var _a, _b, _c;
|
1680
|
+
const systemPrompt = `
|
1681
|
+
You are a seasoned frontend engineer with deep expertise in Prismic slices.
|
1682
|
+
Your task is to update the provided mocks template based solely on the visible content in the image.
|
1683
|
+
Follow these guidelines strictly:
|
1684
|
+
- Do not modify the overall structure of the mocks template.
|
1685
|
+
- Strictly only update text content.
|
1686
|
+
- Do not touch images.
|
1687
|
+
- If you see a repetition with a group, you must create the same number of group items that are visible on the image.
|
1688
|
+
- Absolutely do not touch what is not necessary to be changed, like link "key" property, or the StructureText "direction", spans", etc or the structure, this is really important that you just do a replace of the text.
|
1689
|
+
- For structure text content you must alway keep the same structure and properties, even if empty, ONLY replace text content.
|
1690
|
+
- Only and strictly update the text content of the fields, nothing else. You should only and strictly update the text that is visible in the image.
|
1691
|
+
- Never touch the image fields, nothing should be changed for image fields.
|
1692
|
+
|
1693
|
+
!IMPORTANT!:
|
1694
|
+
- Only return a valid JSON object for mocks, nothing else before. JSON.parse on your response should not throw an error.
|
1695
|
+
- All your response should fit in a single return response.
|
1696
|
+
- Never stop the response until you totally finish the full JSON response you wanted.
|
1697
|
+
|
1698
|
+
Existing Mocks Template:
|
1699
|
+
${JSON.stringify(existingMocks)}
|
1700
|
+
`.trim();
|
1701
|
+
const messages = [
|
1702
|
+
{ role: "system", content: systemPrompt },
|
1703
|
+
{
|
1704
|
+
role: "user",
|
1705
|
+
content: [
|
1706
|
+
{
|
1707
|
+
type: "image_url",
|
1708
|
+
image_url: {
|
1709
|
+
url: "data:image/png;base64," + Buffer.from(imageFile).toString("base64")
|
1710
|
+
}
|
1307
1711
|
}
|
1308
1712
|
]
|
1713
|
+
}
|
1714
|
+
];
|
1715
|
+
const response = await openai.chat.completions.create({
|
1716
|
+
model: "gpt-4o",
|
1717
|
+
messages,
|
1718
|
+
response_format: {
|
1719
|
+
type: "json_object"
|
1720
|
+
}
|
1721
|
+
});
|
1722
|
+
console.log("Generated mocks response:", JSON.stringify(response));
|
1723
|
+
const resultText = (_c = (_b = (_a = response.choices[0]) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c.trim();
|
1724
|
+
if (!resultText) {
|
1725
|
+
throw new Error("No valid mocks were generated.");
|
1726
|
+
}
|
1727
|
+
try {
|
1728
|
+
const updatedMock = JSON.parse(resultText);
|
1729
|
+
return [updatedMock];
|
1730
|
+
} catch (error) {
|
1731
|
+
throw new Error("Failed to parse AI response for mocks: " + error);
|
1732
|
+
}
|
1733
|
+
}
|
1734
|
+
async function generateSliceComponentCode(imageFile, codeFile, updatedSlice) {
|
1735
|
+
var _a, _b, _c;
|
1736
|
+
const systemPrompt = `
|
1737
|
+
You are a seasoned frontend engineer with deep expertise in Prismic slices.
|
1738
|
+
Your task is to generate a fully isolated React component code for a slice based on the provided image and code input.
|
1739
|
+
The goal is to create the React (HTML) structure of the slice, NO STYLING! Concentrate 100% on the perfect structure of each component.
|
1740
|
+
|
1741
|
+
Follow these guidelines strictly:
|
1742
|
+
- Be self-contained.
|
1743
|
+
- For links, you must use PrismicNextLink and you must just pass the field, PrismicNextLink will handle the display of the link text, don't do it manually.
|
1744
|
+
- PrismicNextLink should never be open, just passing the field is enough like in the code example below. You can use className or inline style directly on the PrismicNextLink component.
|
1745
|
+
- Ensure to strictly respect what is defined on the model for each fields ID, do not invent or use something not in the model.
|
1746
|
+
- Ensure to use all fields provided in the model.
|
1747
|
+
- Follow the structure provided in the code example below.
|
1748
|
+
- Use the provided code to help yourself to create the structure.
|
1749
|
+
- As you can see in the example of the code you MUST never access the data with "<field>.value".
|
1750
|
+
- You need to really inspire yourself from the code example bellow in order to understand how to access field, write field etc. Do not try to invent something that you didn't see.
|
1751
|
+
- You cannot add a style prop to "PrismicRichText" component, it's not allowed.
|
1752
|
+
- It's important to respect the same imports as done in the code example bellow, import exactly from the same package.
|
1753
|
+
- Never do wrong W3C HTML structure, always respect a correct HTML structure, for example you cannot put a PrismicRichText component inside a <h1>, or a <p>, etc.
|
1754
|
+
- Ensure to map the field type to the correct Prismic component, for example, a StructuredText field should be mapped to PrismicRichText, an image field should be mapped to PrismicNextImage, a Text field should just be map to a classic <p> component
|
1755
|
+
|
1756
|
+
!IMPORTANT!:
|
1757
|
+
- Return a valid JSON object containing only one key: "componentCode". No additional keys, text, or formatting are allowed before, after, or within the JSON object.
|
1758
|
+
- Return a valid JSON, meaning you should NEVER start with a sentence, directly the JSON so that I can JSON.parse your response.
|
1759
|
+
- All strings must be enclosed in double quotes ("). Do not use single quotes or template literals.
|
1760
|
+
- Within the string value for "componentCode", every embedded double quote must be escaped as ". Similarly, every backslash must be escaped as \\.
|
1761
|
+
- Ensure that the string value does not contain any raw control characters (such as literal newline, tab, or carriage return characters). Instead, use their escape sequences.
|
1762
|
+
- Before finalizing the output, validate that JSON.parse(output) works without throwing an error. No unescaped characters should cause the parser to crash.
|
1763
|
+
- The output must not include any markdown formatting, code block fences, or extra text. It should be a single, clean JSON object.
|
1764
|
+
|
1765
|
+
## Example of a Fully Isolated Slice Component:
|
1766
|
+
-----------------------------------------------------------
|
1767
|
+
import { FC } from "react";
|
1768
|
+
import { Content } from "@prismicio/client";
|
1769
|
+
import { SliceComponentProps, PrismicRichText } from "@prismicio/react";
|
1770
|
+
import { PrismicNextImage, PrismicNextLink } from "@prismicio/next";
|
1771
|
+
|
1772
|
+
export type PascalNameToReplaceProps =
|
1773
|
+
SliceComponentProps<Content.PascalNameToReplaceSlice>;
|
1774
|
+
|
1775
|
+
const PascalNameToReplace: FC<PascalNameToReplaceProps> = ({ slice }) => {
|
1776
|
+
return (
|
1777
|
+
<section
|
1778
|
+
data-slice-type={slice.slice_type}
|
1779
|
+
data-slice-variation={slice.variation}
|
1780
|
+
className="es-bounded es-alternate-grid"
|
1781
|
+
>
|
1782
|
+
<PrismicNextLink
|
1783
|
+
className="es-alternate-grid__button"
|
1784
|
+
field={slice.primary.buttonLink}
|
1785
|
+
/>
|
1786
|
+
<div className="es-alternate-grid__content">
|
1787
|
+
<PrismicNextImage
|
1788
|
+
field={slice.primary.image}
|
1789
|
+
className="es-alternate-grid__image"
|
1790
|
+
/>
|
1791
|
+
<div className="es-alternate-grid__primary-content">
|
1792
|
+
<div className="es-alternate-grid__primary-content__intro">
|
1793
|
+
<p className="es-alternate-grid__primary-content__intro__eyebrow">
|
1794
|
+
{slice.primary.eyebrowHeadline}
|
1795
|
+
</p>
|
1796
|
+
<div className="es-alternate-grid__primary-content__intro__headline">
|
1797
|
+
<PrismicRichText field={slice.primary.title} />
|
1798
|
+
</div>
|
1799
|
+
<div className="es-alternate-grid__primary-content__intro__description">
|
1800
|
+
<PrismicRichText field={slice.primary.description} />
|
1801
|
+
</div>
|
1802
|
+
</div>
|
1803
|
+
|
1804
|
+
<div className="es-alternate-grid__primary-content__stats">
|
1805
|
+
{slice.primary.stats.map((stat, i) => (
|
1806
|
+
<div key={\`stat-\${i + 1}\`} className="es-alternate-grid__stat">
|
1807
|
+
<div className="es-alternate-grid__stat__heading">
|
1808
|
+
<PrismicRichText field={stat.title} />
|
1809
|
+
</div>
|
1810
|
+
<div className="es-alternate-grid__stat__description">
|
1811
|
+
<PrismicRichText field={stat.description} />
|
1812
|
+
</div>
|
1813
|
+
</div>
|
1814
|
+
))}
|
1815
|
+
</div>
|
1816
|
+
</div>
|
1817
|
+
</div>
|
1818
|
+
</section>
|
1819
|
+
);
|
1820
|
+
};
|
1821
|
+
|
1822
|
+
export default PascalNameToReplace;
|
1823
|
+
-----------------------------------------------------------
|
1824
|
+
|
1825
|
+
Model of the slice:
|
1826
|
+
${JSON.stringify(updatedSlice)}
|
1827
|
+
`.trim();
|
1828
|
+
const messages = [
|
1829
|
+
{ role: "system", content: systemPrompt },
|
1830
|
+
{
|
1831
|
+
role: "user",
|
1832
|
+
content: [
|
1833
|
+
{
|
1834
|
+
type: "image_url",
|
1835
|
+
image_url: {
|
1836
|
+
url: "data:image/png;base64," + Buffer.from(imageFile).toString("base64")
|
1837
|
+
}
|
1838
|
+
},
|
1839
|
+
{ type: "text", text: codeFile }
|
1840
|
+
]
|
1841
|
+
}
|
1842
|
+
];
|
1843
|
+
const response = await openai.chat.completions.create({
|
1844
|
+
model: "gpt-4o",
|
1845
|
+
messages,
|
1846
|
+
response_format: {
|
1847
|
+
type: "json_object"
|
1848
|
+
}
|
1849
|
+
});
|
1850
|
+
console.log("Generated component code response:", JSON.stringify(response));
|
1851
|
+
const resultText = (_c = (_b = (_a = response.choices[0]) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c.trim();
|
1852
|
+
if (!resultText) {
|
1853
|
+
throw new Error("No valid slice component code was generated.");
|
1854
|
+
}
|
1855
|
+
try {
|
1856
|
+
const parsed = JSON.parse(resultText);
|
1857
|
+
if (!parsed.componentCode) {
|
1858
|
+
throw new Error("Missing key 'componentCode' in AI response.");
|
1859
|
+
}
|
1860
|
+
return parsed.componentCode;
|
1861
|
+
} catch (error) {
|
1862
|
+
throw new Error("Failed to parse AI response for component code: " + error);
|
1863
|
+
}
|
1864
|
+
}
|
1865
|
+
async function generateSliceComponentCodeAppearance(imageFile, codeFile, globalStyle, componentCode) {
|
1866
|
+
var _a, _b, _c;
|
1867
|
+
const systemPrompt = `
|
1868
|
+
You are a seasoned frontend engineer with deep expertise in Prismic slices.
|
1869
|
+
Your task is to apply the branding (appearance) based on the provided image and code input.
|
1870
|
+
The branding is SUPER important, and the slice you create should PERFECTLY match the branding (appearance) of the provided slice image and code.
|
1871
|
+
|
1872
|
+
Follow these guidelines strictly:
|
1873
|
+
- Don't change anything related to the structure of the code, ONLY apply styling, PURELY styling is your ONLY task.
|
1874
|
+
- Be self-contained, no dependency should be use to do the styling, do inline style.
|
1875
|
+
- Your goal is to make the code visually looks as close as possible to the image from the user input.
|
1876
|
+
- Ensure that the color used for the background is the same as the image provide in the user prompt! It's better no background color than a wrong one.
|
1877
|
+
- Strictly respect the padding and margin visible in the image provide in the user prompt.
|
1878
|
+
- Strictly respect the fonts size, color, type visible in the image provide in the user prompt.
|
1879
|
+
- Strictly respect the colors visible in the image provide in the user prompt.
|
1880
|
+
- Strictly respect the position of elements visible in the image provide in the user prompt.
|
1881
|
+
- Strictly respect the size of each elements visible in the image provide in the user prompt.
|
1882
|
+
- Strictly respect the overall proportions of the slice from the image provide in the user prompt.
|
1883
|
+
- Ensure image are always displayed with the same aspect ratio as the image provide in the user prompt, put constraints on the image with / height to make sure it's the same.
|
1884
|
+
- Handle animations, but never make them too long, it should be fast enough to be nice to read.
|
1885
|
+
- Use inline <style> (do not use <style jsx>).
|
1886
|
+
- Items repetitions should be styled in the same way as the image provided, the direction of the flex should be the same so that the items are vertical or horizontal as in the image.
|
1887
|
+
|
1888
|
+
!IMPORTANT!:
|
1889
|
+
- DO NOT CHANGE anything else than the style BUT return everything as before for the rest, like from the import to the last export line, everything should stay the same BUT you add the styling on top.
|
1890
|
+
- Return a valid JSON object containing only one key: "componentCode". No additional keys, text, or formatting are allowed before, after, or within the JSON object.
|
1891
|
+
- Return a valid JSON, meaning you should NEVER start with a sentence, directly the JSON so that I can JSON.parse your response.
|
1892
|
+
- All strings must be enclosed in double quotes ("). Do not use single quotes or template literals.
|
1893
|
+
- Within the string value for "componentCode", every embedded double quote must be escaped as ". Similarly, every backslash must be escaped as \\.
|
1894
|
+
- Ensure that the string value does not contain any raw control characters (such as literal newline, tab, or carriage return characters). Instead, use their escape sequences.
|
1895
|
+
- Before finalizing the output, validate that JSON.parse(output) works without throwing an error. No unescaped characters should cause the parser to crash.
|
1896
|
+
- The output must not include any markdown formatting, code block fences, or extra text. It should be a single, clean JSON object.
|
1897
|
+
|
1898
|
+
Existing code to apply branding on it:
|
1899
|
+
${componentCode}
|
1900
|
+
`.trim();
|
1901
|
+
const messages = [
|
1902
|
+
{ role: "system", content: systemPrompt },
|
1903
|
+
{
|
1904
|
+
role: "user",
|
1905
|
+
content: [
|
1906
|
+
{
|
1907
|
+
type: "image_url",
|
1908
|
+
image_url: {
|
1909
|
+
url: "data:image/png;base64," + Buffer.from(imageFile).toString("base64")
|
1910
|
+
}
|
1911
|
+
},
|
1912
|
+
{ type: "text", text: codeFile }
|
1913
|
+
]
|
1914
|
+
}
|
1915
|
+
];
|
1916
|
+
const response = await openai.chat.completions.create({
|
1917
|
+
model: "gpt-4o",
|
1918
|
+
messages,
|
1919
|
+
response_format: {
|
1920
|
+
type: "json_object"
|
1921
|
+
}
|
1922
|
+
});
|
1923
|
+
console.log("Generated component code appearance response:", JSON.stringify(response));
|
1924
|
+
const resultText = (_c = (_b = (_a = response.choices[0]) == null ? void 0 : _a.message) == null ? void 0 : _b.content) == null ? void 0 : _c.trim();
|
1925
|
+
if (!resultText) {
|
1926
|
+
throw new Error("No valid slice component code was generated.");
|
1927
|
+
}
|
1928
|
+
try {
|
1929
|
+
const parsed = JSON.parse(resultText);
|
1930
|
+
if (!parsed.componentCode) {
|
1931
|
+
throw new Error("Missing key 'componentCode' in AI response.");
|
1932
|
+
}
|
1933
|
+
return parsed.componentCode;
|
1934
|
+
} catch (error) {
|
1935
|
+
throw new Error("Failed to parse AI response for component code appearance: " + error);
|
1936
|
+
}
|
1937
|
+
}
|
1938
|
+
try {
|
1939
|
+
let slices = [];
|
1940
|
+
const folderPath = KNOWNED_WEBSITE_URLS[args.websiteUrl];
|
1941
|
+
console.log("STEP 1: Get the slices images from the folder.");
|
1942
|
+
const sliceImages = await readImagesFromFolder(`${folderPath}/images`);
|
1943
|
+
console.log("STEP 2: Get the slices codes from the folder.");
|
1944
|
+
const sliceCodes = await readCodeFromFolder(`${folderPath}/code`);
|
1945
|
+
slices = sliceImages.map((sliceImage, index) => ({
|
1946
|
+
sliceImage,
|
1947
|
+
codeFile: sliceCodes[index]
|
1948
|
+
}));
|
1949
|
+
const updatedSlices = await Promise.all(slices.map(async ({ sliceImage, codeFile }, index) => {
|
1950
|
+
console.log("STEP 3: Generate the slice model using the image for slice:", index);
|
1951
|
+
const updatedSlice = await generateSliceModel(sliceImage, codeFile);
|
1952
|
+
console.log("STEP 4: Persist the updated slice model for:", `${index} - ${updatedSlice.name}`);
|
1953
|
+
await this.updateSlice({
|
1954
|
+
libraryID: DEFAULT_LIBRARY_ID,
|
1955
|
+
model: updatedSlice
|
1309
1956
|
});
|
1310
|
-
console.log("
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1957
|
+
console.log("STEP 5: Update the slice screenshot for:", `${index} - ${updatedSlice.name}`);
|
1958
|
+
await this.updateSliceScreenshot({
|
1959
|
+
libraryID: DEFAULT_LIBRARY_ID,
|
1960
|
+
sliceID: updatedSlice.id,
|
1961
|
+
variationID: updatedSlice.variations[0].id,
|
1962
|
+
data: Buffer.from(sliceImage)
|
1963
|
+
});
|
1964
|
+
let updatedMock;
|
1965
|
+
try {
|
1966
|
+
console.log("STEP 6: Generate updated mocks for:", `${index} - ${updatedSlice.name}`);
|
1967
|
+
const existingMocks = mockSlice.mockSlice({ model: updatedSlice });
|
1968
|
+
updatedMock = await generateSliceMocks(sliceImage, existingMocks);
|
1969
|
+
} catch (error) {
|
1970
|
+
console.error(`Failed to generate mocks for ${index} - ${updatedSlice.name}:`, error);
|
1971
|
+
updatedMock = mockSlice.mockSlice({ model: updatedSlice });
|
1972
|
+
}
|
1973
|
+
let componentCode;
|
1974
|
+
try {
|
1975
|
+
console.log("STEP 7: Generate the isolated slice component code for:", `${index} - ${updatedSlice.name}`);
|
1976
|
+
const globalStyle = await getGlobalStyle(`${folderPath}/globalStyle.css`);
|
1977
|
+
const initialCode = await generateSliceComponentCode(sliceImage, codeFile, updatedSlice);
|
1978
|
+
console.log("STEP 8: Generate the branding on the code:", `${index} - ${updatedSlice.name}`);
|
1979
|
+
componentCode = await generateSliceComponentCodeAppearance(sliceImage, codeFile, globalStyle, initialCode);
|
1980
|
+
} catch (error) {
|
1981
|
+
console.error(`Failed to generate code for ${index} - ${updatedSlice.name}:`, error);
|
1982
|
+
}
|
1983
|
+
return { updatedSlice, componentCode, updatedMock };
|
1984
|
+
}));
|
1985
|
+
await Promise.all(updatedSlices.map(async ({ updatedSlice, componentCode, updatedMock }, index) => {
|
1986
|
+
console.log("STEP 9: Update the slice code for:", `${index} - ${updatedSlice.name}`);
|
1987
|
+
if (componentCode) {
|
1988
|
+
await this.createSlice({
|
1989
|
+
libraryID: DEFAULT_LIBRARY_ID,
|
1990
|
+
model: updatedSlice,
|
1991
|
+
componentContents: componentCode
|
1320
1992
|
});
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1993
|
+
} else {
|
1994
|
+
await this.createSlice({
|
1995
|
+
libraryID: DEFAULT_LIBRARY_ID,
|
1996
|
+
model: updatedSlice
|
1325
1997
|
});
|
1326
1998
|
}
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1999
|
+
console.log("STEP 10: Persist the generated mocks for:", `${index} - ${updatedSlice.name}`);
|
2000
|
+
await this.updateSliceMocks({
|
2001
|
+
libraryID: DEFAULT_LIBRARY_ID,
|
2002
|
+
sliceID: updatedSlice.id,
|
2003
|
+
mocks: updatedMock
|
2004
|
+
});
|
2005
|
+
}));
|
2006
|
+
console.log("STEP 11: THE END");
|
2007
|
+
return {
|
2008
|
+
slices: updatedSlices.map(({ updatedSlice }) => updatedSlice)
|
2009
|
+
};
|
2010
|
+
} catch (error) {
|
2011
|
+
console.error("Failed to generate slice:", error);
|
2012
|
+
throw new Error("Failed to generate slice: " + error);
|
1330
2013
|
}
|
1331
|
-
return {
|
1332
|
-
errors: [],
|
1333
|
-
slice: newSlice
|
1334
|
-
};
|
1335
2014
|
}
|
1336
2015
|
}
|
1337
2016
|
exports.SlicesManager = SlicesManager;
|