@sanity/assist 6.1.21 → 6.2.0

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/README.md CHANGED
@@ -180,6 +180,7 @@ assist({
180
180
  assist: {
181
181
  localeSettings: () => Intl.DateTimeFormat().resolvedOptions(),
182
182
  maxPathDepth: 4,
183
+ maxFieldSelectionDepth: 6,
183
184
  temperature: 0.3,
184
185
  },
185
186
  translate: {/* see sections about document and field translation */},
@@ -188,6 +189,7 @@ assist({
188
189
 
189
190
  - `localeSettings`: See section on [date and datetime](#date-and-datetime)
190
191
  - `maxPathDepth`: The max depth for document paths AI Assist will write to.
192
+ - `maxFieldSelectionDepth`: The max depth for fields that can be selected for AI Assist. Fields nested deeper than this get no AI Assist field action, are not listed in the field picker when editing instructions, and cannot be referenced from an instruction. Depth is counted in path segments from the document root (`title` has depth 1, `array[_key="no"].title` has depth 3). Be careful not to set this too high in studios with recursive document schemas, as it could have negative impact on performance.
191
193
  - `temperature`: Influences how much the output of an instruction will vary between runs.
192
194
 
193
195
  For more details, please review the TSDocs of the individual config parameters in [assistTypes.ts](./src/assistTypes.ts).
@@ -658,6 +660,8 @@ assist({
658
660
  Be careful not to set this too high in studios with recursive document schemas, as it could have negative impact on performance.
659
661
  maxPathDepth is hard-capped to 50.
660
662
 
663
+ `translate.field.maxPathDepth` only affects the "Translate fields" action. Which fields get an AI Assist field action at all is controlled by `assist.maxFieldSelectionDepth` (see [Assist configuration](#assist-configuration-optional)).
664
+
661
665
  ### Loading field languages
662
666
 
663
667
  Languages must be an array of objects with an id and title.
package/dist/index.d.ts CHANGED
@@ -37,6 +37,23 @@ interface AssistConfig {
37
37
  * Default: 4
38
38
  */
39
39
  maxPathDepth?: number;
40
+ /**
41
+ * The max depth for fields that can be selected for AI Assist in the Studio: fields deeper than this
42
+ * get no AI Assist field action (the sparkle button), are not listed in the field picker when editing
43
+ * instructions, and cannot be referenced from an instruction prompt.
44
+ *
45
+ * Depth is based on field path segments, counted from the document root:
46
+ * - `title` has depth 1
47
+ * - `array[_key="no"].title` has depth 3
48
+ *
49
+ * Unlike `maxPathDepth`, this does not affect what an instruction can write to once it runs.
50
+ *
51
+ * Be careful not to set this too high in studios with recursive document schemas, as it could have
52
+ * negative impact on performance.
53
+ *
54
+ * Default: 6
55
+ */
56
+ maxFieldSelectionDepth?: number;
40
57
  /**
41
58
  * Influences how much the output of an instruction will vary.
42
59
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/assistTypes.ts","../src/fieldActions/customFieldActions.tsx","../src/translate/types.ts","../src/types.ts","../src/plugin.tsx","../src/schemas/serialize/SchemTypeTool.tsx","../src/schemas/typeDefExtensions.ts","../src/translate/paths.ts","../src/fieldActions/useUserInput.ts","../src/helpers/typeUtils.ts"],"mappings":";;;;UAEiB;;;;;;;;;;;;;;;;;;EAkBf,kBAAkB,SAAS,0BAA0B;;;;;;;;;;;;;;;;EAiBrD;;;;;;;;;;;;;EAcA;;UAGe;EACf,MAAM;EACN,iBAAiB;;UAGF;;;;;;;;;;EAUf;;;;;;;;;;;EAYA;;UCvDe;EACf,MAAM;EACN;EACA;;UAGe;;;;;EAKf;;;;;;;;;;;;;;;;;;EAkBA;;;;;EAMA,oBAAoB;;;;;;;EAQpB,wBAAwB;;;;;;EAOxB,2BAA2B;;;;;;;;;;;;;;;;;;;;;;EAuB3B;;;;;;;;;;;;;;;;;;;;;EAsBA,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;EA0BN,YAAY;;;;;;;;;EAUZ,mBAAmB;;KAGT,wBACR,wBACA,yBACA;KAEQ,wBAAwB,KAClC;EAGA,uBAAuB;;KAGb,yBAAyB,KACnC;;;;;EAOA,WAAW;;iBAKG,wBACd,QAAQ,KAAK,iCACZ;iBAOa,4BAA4B;iBAM5B,6BACd,OAAO,KAAK,kCACX;UCxMc;EACf;EACA;;UAGe;EACf,YAAY;EACZ,MAAM;EACN;EACA;EACA;;UAGe;;EAEf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;;;;;;;;;;;;;;;;;;;;;;;;MAmCT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAgCA;QAGM;;;;;;;QAOA;;;;YAIA,sBAAsB;YACtB,sBAAsB;YACtB;IACR;;MAEE;;;;;MAMA;;;YAGM,oBAAoB;YACpB,sBAAsB;YACtB,oBAAoB;YACpB,mBAAmB;YACnB,qBAAqB;;cCUpB,wBAAwB;KCrIzB,gBAAgB;;;;EAI1B;;;;EAIA,QAAQ;MACJ,QAAQ;;;;UAKG;;;;EAIf;;;;EAIA;;;;EAIA;;KAGU;;;;EAIV,OAAO;;;;EAKP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+Dc,gBAAgB;;;;;;;iBC3FhB,OAAO,YAAY,YAAY"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/assistTypes.ts","../src/fieldActions/customFieldActions.tsx","../src/translate/types.ts","../src/types.ts","../src/plugin.tsx","../src/schemas/serialize/SchemTypeTool.tsx","../src/schemas/typeDefExtensions.ts","../src/translate/paths.ts","../src/fieldActions/useUserInput.ts","../src/helpers/typeUtils.ts"],"mappings":";;;;UAEiB;;;;;;;;;;;;;;;;;;EAkBf,kBAAkB,SAAS,0BAA0B;;;;;;;;;;;;;;;;EAiBrD;;;;;;;;;;;;;;;;;EAkBA;;;;;;;;;;;;;EAcA;;UAGe;EACf,MAAM;EACN,iBAAiB;;UAGF;;;;;;;;;;EAUf;;;;;;;;;;;EAYA;;UCzEe;EACf,MAAM;EACN;EACA;;UAGe;;;;;EAKf;;;;;;;;;;;;;;;;;;EAkBA;;;;;EAMA,oBAAoB;;;;;;;EAQpB,wBAAwB;;;;;;EAOxB,2BAA2B;;;;;;;;;;;;;;;;;;;;;;EAuB3B;;;;;;;;;;;;;;;;;;;;;EAsBA,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;EA0BN,YAAY;;;;;;;;;EAUZ,mBAAmB;;KAGT,wBACR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AY,eAAe;;cAGhB,yBAAM,cAAA;iBCvCH,kCAAc,IAAA;UCXb;EACf;;IAEE;;;;;IAMA;;;;YAKQ,qBAAqB;YACrB,qBAAqB;YACrB,uBAAuB;YACvB,qCAAqC;YACrC,oBAAoB;YACpB,wBAAwB;YACxB,wBAAwB;YACxB,oBAAoB;YACpB,wBAAwB;YACxB;IACR,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAmCT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAgCA;QAGM;;;;;;;QAOA;;;;YAIA,sBAAsB;YACtB,sBAAsB;YACtB;IACR;;MAEE;;;;;MAMA;;;YAGM,oBAAoB;YACpB,sBAAsB;YACtB,oBAAoB;YACpB,mBAAmB;YACnB,qBAAqB;;cCUpB,wBAAwB;KCrIzB,gBAAgB;;;;EAI1B;;;;EAIA,QAAQ;MACJ,QAAQ;;;;UAKG;;;;EAIf;;;;EAIA;;;;EAIA;;KAGU;;;;EAIV,OAAO;;;;EAKP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+Dc,gBAAgB;;;;;;;iBC3FhB,OAAO,YAAY,YAAY"}
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ import { ErrorOutlineIcon } from "@sanity/icons/ErrorOutline";
36
36
  import { SyncIcon } from "@sanity/icons/Sync";
37
37
  import { Popover } from "@sanity/ui/popover";
38
38
  import { formatDistanceToNow } from "date-fns/formatDistanceToNow";
39
- import isEqual from "react-fast-compare";
39
+ import { dequal } from "dequal/lite";
40
40
  import get from "lodash-es/get.js";
41
41
  import { Icon, icons } from "@sanity/icons";
42
42
  import { ControlsIcon } from "@sanity/icons/Controls";
@@ -291,7 +291,7 @@ function mapConnectorToLine(options, connector) {
291
291
  to
292
292
  };
293
293
  }
294
- const assistDocumentIdPrefix = "sanity.assist.schemaType.", assistDocumentStatusIdPrefix = "sanity.assist.status.", assistDocumentTypeName = "sanity.assist.schemaType.annotations", assistFieldTypeName = "sanity.assist.schemaType.field", userInputTypeName = "sanity.assist.instruction.userInput", contextDocumentTypeName = "assist.instruction.context", assistTasksStatusTypeName = "sanity.assist.task.status", instructionTaskTypeName = "sanity.assist.instructionTask", fieldPresenceTypeName = "sanity.assist.instructionTask.presence", assistSerializedTypeName = "sanity.assist.serialized.type", assistSerializedFieldTypeName = "sanity.assist.serialized.field", fieldPathParam = "pathKey", instructionParam = "instruction", documentRootKey = "<document>";
294
+ const assistDocumentStatusIdPrefix = "sanity.assist.status.", assistDocumentTypeName = "sanity.assist.schemaType.annotations", assistFieldTypeName = "sanity.assist.schemaType.field", userInputTypeName = "sanity.assist.instruction.userInput", fieldReferenceTypeName = "sanity.assist.instruction.fieldRef", contextDocumentTypeName = "assist.instruction.context", assistTasksStatusTypeName = "sanity.assist.task.status", instructionTaskTypeName = "sanity.assist.instructionTask", fieldPresenceTypeName = "sanity.assist.instructionTask.presence", assistSerializedTypeName = "sanity.assist.serialized.type", assistSerializedFieldTypeName = "sanity.assist.serialized.field", fieldPathParam = "pathKey", instructionParam = "instruction", documentRootKey = "<document>";
295
295
  function usePathKey(path) {
296
296
  let $ = c(2), t0;
297
297
  return $[0] === path ? t0 = $[1] : (t0 = getPathKey(path), $[0] = path, $[1] = t0), t0;
@@ -364,7 +364,7 @@ function _temp$16(member) {
364
364
  }
365
365
  const releaseAnnouncementUrl = "https://www.sanity.io/blog/sanity-ai-assist-announcement?utm_source=sanity-assist-plugin&utm_medium=organic_social&utm_campaign=ai-assist&utm_content=", packageName = "@sanity/assist", pluginTitle = "Sanity AI Assist", pluginTitleShort = "AI Assist", maxHistoryVisibilityMs = minutesToMilliseconds(30), illegalIdChars = /[^a-zA-Z0-9._-]/g;
366
366
  function assistDocumentId(documentType) {
367
- return `${assistDocumentIdPrefix}${documentType}`.replace(illegalIdChars, "_");
367
+ return `sanity.assist.schemaType.${documentType}`.replace(illegalIdChars, "_");
368
368
  }
369
369
  function assistTasksStatusId(documentId) {
370
370
  return isVersionId(documentId) ? `${assistDocumentStatusIdPrefix}${getVersionFromId(documentId)}.${getPublishedId(documentId)}` : `${assistDocumentStatusIdPrefix}${getPublishedId(documentId)}`;
@@ -588,15 +588,25 @@ function getDocumentFieldRef(schemaType) {
588
588
  schemaType
589
589
  };
590
590
  }
591
- function getFieldRefs(schemaType, parent, depth = 0) {
592
- return depth >= 6 ? [] : schemaType.fields.filter((f) => !f.name.startsWith("_")).flatMap((field) => {
591
+ /**
592
+ * Collects every field (and synthetic array item) in `schemaType` that AI Assist can attach
593
+ * instructions to.
594
+ *
595
+ * @param maxDepth - max number of path segments a collected field can have; deeper fields are
596
+ * skipped. Arrays contribute one segment for the item type (`array[_key="type"]`).
597
+ */
598
+ function getFieldRefs(schemaType, maxDepth = 6) {
599
+ return getObjectFieldRefs(schemaType, void 0, 0, maxDepth);
600
+ }
601
+ function getObjectFieldRefs(schemaType, parent, depth, maxDepth) {
602
+ return depth >= maxDepth ? [] : schemaType.fields.filter((f) => !f.name.startsWith("_")).flatMap((field) => {
593
603
  let path = parent ? [...parent.path, field.name] : [field.name], title = field.type.title ?? field.name, fieldRef = {
594
604
  key: patchableKey(pathToString(path)),
595
605
  path,
596
606
  title: parent ? [parent.title, title].join(" / ") : title,
597
607
  schemaType: field.type,
598
608
  icon: getTypeIcon(field.type)
599
- }, fields = field.type.jsonType === "object" ? getFieldRefs(field.type, fieldRef, depth + 1) : [], syntheticFields = field.type.jsonType === "array" ? getSyntheticFields(field.type, fieldRef, depth + 1) : [];
609
+ }, fields = field.type.jsonType === "object" ? getObjectFieldRefs(field.type, fieldRef, depth + 1, maxDepth) : [], syntheticFields = field.type.jsonType === "array" ? getSyntheticFields(field.type, fieldRef, depth + 1, maxDepth) : [];
600
610
  return isAssistSupported(field.type) ? [
601
611
  fieldRef,
602
612
  ...fields,
@@ -604,8 +614,8 @@ function getFieldRefs(schemaType, parent, depth = 0) {
604
614
  ] : [...fields, ...syntheticFields];
605
615
  });
606
616
  }
607
- function getSyntheticFields(schemaType, parent, depth = 0) {
608
- return depth >= 6 ? [] : schemaType.of.filter((itemType) => !isType(itemType, "block")).flatMap((itemType) => {
617
+ function getSyntheticFields(schemaType, parent, depth, maxDepth) {
618
+ return depth >= maxDepth ? [] : schemaType.of.filter((itemType) => !isType(itemType, "block")).flatMap((itemType) => {
609
619
  let segment = { _key: itemType.name }, title = itemType.title ?? itemType.name, path = parent ? [...parent.path, segment] : [segment], fieldRef = {
610
620
  key: patchableKey(pathToString(path)),
611
621
  path,
@@ -613,7 +623,7 @@ function getSyntheticFields(schemaType, parent, depth = 0) {
613
623
  schemaType: itemType,
614
624
  icon: getTypeIcon(itemType),
615
625
  synthetic: !0
616
- }, fields = itemType.jsonType === "object" ? getFieldRefs(itemType, fieldRef, depth + 1) : [];
626
+ }, fields = itemType.jsonType === "object" ? getObjectFieldRefs(itemType, fieldRef, depth + 1, maxDepth) : [];
617
627
  return isAssistSupported(itemType) ? [fieldRef, ...fields] : fields;
618
628
  });
619
629
  }
@@ -2163,7 +2173,7 @@ function TaskItem(props) {
2163
2173
  */
2164
2174
  function isolatePathParams(nextParams, hostParams) {
2165
2175
  let { path: nextLocalPath, ...restNext } = nextParams, { path: hostPath, ...restHost } = hostParams;
2166
- return isEqual(restNext, restHost) ? {
2176
+ return dequal(restNext, restHost) ? {
2167
2177
  nextLocalPath,
2168
2178
  forwardParams: null
2169
2179
  } : {
@@ -3177,12 +3187,16 @@ function refToTypeNames(type) {
3177
3187
  function removeUndef(obj) {
3178
3188
  return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
3179
3189
  }
3180
- function createFieldRefCache() {
3190
+ /**
3191
+ * @param maxFieldSelectionDepth - see `AssistConfig.maxFieldSelectionDepth`; uses the plugin
3192
+ * default when omitted
3193
+ */
3194
+ function createFieldRefCache(maxFieldSelectionDepth) {
3181
3195
  let byType = {};
3182
3196
  function getRefsForType(schemaType) {
3183
3197
  let documentType = schemaType.name, cached = byType[documentType];
3184
3198
  if (cached) return cached;
3185
- let fieldRefs = getFieldRefs(schemaType), refs = {
3199
+ let fieldRefs = getFieldRefs(schemaType, maxFieldSelectionDepth), refs = {
3186
3200
  fieldRefs,
3187
3201
  fieldRefsByTypePath: asFieldRefsByTypePath(fieldRefs)
3188
3202
  };
@@ -3193,7 +3207,7 @@ function createFieldRefCache() {
3193
3207
  function AiAssistanceConfigProvider(props) {
3194
3208
  let $ = c(21), [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient), schema = useSchema(), t0;
3195
3209
  $[0] === schema ? t0 = $[1] : (t0 = serializeSchema(schema, { leanFormat: !0 }), $[0] = schema, $[1] = t0);
3196
- let serializedTypes = t0, { getFieldRefs, getFieldRefsByTypePath } = useFieldRefGetters(schema), t1, t2;
3210
+ let serializedTypes = t0, { getFieldRefs, getFieldRefsByTypePath } = useFieldRefGetters(schema, props.config.assist?.maxFieldSelectionDepth), t1, t2;
3197
3211
  $[2] === getInstructStatus ? (t1 = $[3], t2 = $[4]) : (t1 = () => {
3198
3212
  getInstructStatus().then((s) => setStatus(s)).catch((e) => {
3199
3213
  console.error(e), setError(e);
@@ -3229,20 +3243,20 @@ function AiAssistanceConfigProvider(props) {
3229
3243
  children
3230
3244
  }), $[18] = children, $[19] = context, $[20] = t5) : t5 = $[20], t5;
3231
3245
  }
3232
- function useFieldRefGetters(schema) {
3233
- let $ = c(5), t0;
3234
- $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = createFieldRefCache(), $[0] = t0) : t0 = $[0];
3246
+ function useFieldRefGetters(schema, maxFieldSelectionDepth) {
3247
+ let $ = c(7), t0;
3248
+ $[0] === maxFieldSelectionDepth ? t0 = $[1] : (t0 = createFieldRefCache(maxFieldSelectionDepth), $[0] = maxFieldSelectionDepth, $[1] = t0);
3235
3249
  let getForSchemaType = t0, t1;
3236
- $[1] === schema ? t1 = $[2] : (t1 = function getRefsForType(documentType) {
3250
+ $[2] !== getForSchemaType || $[3] !== schema ? (t1 = function getRefsForType(documentType) {
3237
3251
  let schemaType = schema.get(documentType);
3238
3252
  if (!schemaType) throw Error(`Schema type "${documentType}" not found`);
3239
3253
  return getForSchemaType(schemaType);
3240
- }, $[1] = schema, $[2] = t1);
3254
+ }, $[2] = getForSchemaType, $[3] = schema, $[4] = t1) : t1 = $[4];
3241
3255
  let getRefsForType = t1, t2;
3242
- return $[3] === getRefsForType ? t2 = $[4] : (t2 = {
3256
+ return $[5] === getRefsForType ? t2 = $[6] : (t2 = {
3243
3257
  getFieldRefs: (documentType_0) => getRefsForType(documentType_0).fieldRefs,
3244
3258
  getFieldRefsByTypePath: (documentType_1) => getRefsForType(documentType_1).fieldRefsByTypePath
3245
- }, $[3] = getRefsForType, $[4] = t2), t2;
3259
+ }, $[5] = getRefsForType, $[6] = t2), t2;
3246
3260
  }
3247
3261
  function AssistLayout(props) {
3248
3262
  let $ = c(11), t0;
@@ -4697,46 +4711,55 @@ const contextDocumentSchema = defineType({
4697
4711
  };
4698
4712
  }
4699
4713
  }
4700
- }), fieldReference = defineType({
4701
- type: "object",
4702
- name: "sanity.assist.instruction.fieldRef",
4703
- title: "Field",
4704
- icon: ThListIcon,
4705
- fields: [defineField({
4706
- type: "string",
4707
- name: "path",
4714
+ });
4715
+ /**
4716
+ * The validation rule resolves the selected path against the same field-ref tree the field picker
4717
+ * offers, so it has to be built with the same `assist.maxFieldSelectionDepth`.
4718
+ */
4719
+ function createFieldReferenceType(maxFieldSelectionDepth) {
4720
+ return defineType({
4721
+ type: "object",
4722
+ name: fieldReferenceTypeName,
4708
4723
  title: "Field",
4709
- components: { input: FieldRefPathInput },
4710
- validation: (rule) => {
4711
- let getForSchemaType = createFieldRefCache();
4712
- return rule.custom((value, context) => {
4713
- if (!value) return "Please select a field";
4714
- try {
4715
- let docId = context.document?._id;
4716
- if (!docId) return "Field reference cannot be used outside document inspector context. Could not resolve document id.";
4717
- let targetDocType = docId.replace(RegExp(`^${assistDocumentIdPrefix}`), ""), schema = context.schema.get(targetDocType);
4718
- if (!schema) return `Field reference cannot be used outside document inspector context. Could not resolve schema: ${targetDocType}`;
4719
- let { fieldRefs } = getForSchemaType(schema);
4720
- return fieldRefs.find((r) => r.key === value) ? !0 : `Field with path "${value}" does not exist in the schema.`;
4721
- } catch (e) {
4722
- return console.error("Failed to resolve field reference", e), "Invalid field reference.";
4723
- }
4724
- });
4725
- }
4726
- })],
4727
- preview: {
4728
- select: { path: "path" },
4729
- prepare({ path }) {
4730
- return {
4731
- title: path,
4732
- path,
4733
- icon: CodeIcon
4734
- };
4735
- }
4736
- },
4737
- components: { preview: FieldRefPreview },
4738
- options: { modal: { type: "popover" } }
4739
- }), userInput = defineType({
4724
+ icon: ThListIcon,
4725
+ fields: [defineField({
4726
+ type: "string",
4727
+ name: "path",
4728
+ title: "Field",
4729
+ components: { input: FieldRefPathInput },
4730
+ validation: (rule) => {
4731
+ let getForSchemaType = createFieldRefCache(maxFieldSelectionDepth);
4732
+ return rule.custom((value, context) => {
4733
+ if (!value) return "Please select a field";
4734
+ try {
4735
+ let docId = context.document?._id;
4736
+ if (!docId) return "Field reference cannot be used outside document inspector context. Could not resolve document id.";
4737
+ if (!docId.startsWith("sanity.assist.schemaType.")) return `Field reference cannot be used outside document inspector context. Could not resolve document id: ${docId}`;
4738
+ let targetDocType = docId.slice(25), schema = context.schema.get(targetDocType);
4739
+ if (!schema) return `Field reference cannot be used outside document inspector context. Could not resolve schema: ${targetDocType}`;
4740
+ let { fieldRefs } = getForSchemaType(schema);
4741
+ return fieldRefs.find((r) => r.key === value) ? !0 : `Field with path "${value}" does not exist in the schema.`;
4742
+ } catch (e) {
4743
+ return console.error("Failed to resolve field reference", e), "Invalid field reference.";
4744
+ }
4745
+ });
4746
+ }
4747
+ })],
4748
+ preview: {
4749
+ select: { path: "path" },
4750
+ prepare({ path }) {
4751
+ return {
4752
+ title: path,
4753
+ path,
4754
+ icon: CodeIcon
4755
+ };
4756
+ }
4757
+ },
4758
+ components: { preview: FieldRefPreview },
4759
+ options: { modal: { type: "popover" } }
4760
+ });
4761
+ }
4762
+ const userInput = defineType({
4740
4763
  type: "object",
4741
4764
  name: userInputTypeName,
4742
4765
  title: "User input",
@@ -4812,7 +4835,7 @@ const contextDocumentSchema = defineType({
4812
4835
  annotations: []
4813
4836
  },
4814
4837
  of: [
4815
- defineArrayMember({ type: fieldReference.name }),
4838
+ defineArrayMember({ type: fieldReferenceTypeName }),
4816
4839
  defineArrayMember({ type: promptContext.name }),
4817
4840
  defineArrayMember({ type: userInput.name })
4818
4841
  ]
@@ -5062,29 +5085,37 @@ function excludeComments(type) {
5062
5085
  ..."of" in type ? { of: type.of?.map((arrayItemType) => excludeComments(arrayItemType)) } : {}
5063
5086
  };
5064
5087
  }
5065
- const schemaTypes = [
5066
- ...[
5067
- fieldInstructions,
5068
- instruction,
5069
- fieldReference,
5070
- prompt,
5071
- userInput,
5072
- promptContext
5073
- ].map(excludeComments),
5074
- outputFieldType,
5075
- outputTypeType,
5076
- assistDocumentSchema,
5077
- documentInstructionStatus,
5078
- instructionTask,
5079
- contextDocumentSchema
5080
- ], assist = definePlugin((config) => {
5081
- let configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
5088
+ /**
5089
+ * @param maxFieldSelectionDepth - see `AssistConfig.maxFieldSelectionDepth`; the field reference
5090
+ * type validates picked paths against the field-ref tree, which must be built with the same depth
5091
+ */
5092
+ function createSchemaTypes(maxFieldSelectionDepth) {
5093
+ return [
5094
+ ...[
5095
+ fieldInstructions,
5096
+ instruction,
5097
+ createFieldReferenceType(maxFieldSelectionDepth),
5098
+ prompt,
5099
+ userInput,
5100
+ promptContext
5101
+ ].map(excludeComments),
5102
+ outputFieldType,
5103
+ outputTypeType,
5104
+ assistDocumentSchema,
5105
+ documentInstructionStatus,
5106
+ instructionTask,
5107
+ contextDocumentSchema
5108
+ ];
5109
+ }
5110
+ const assist = definePlugin((config) => {
5111
+ let configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, maxFieldSelectionDepth = configWithDefaults.assist?.maxFieldSelectionDepth, temperature = configWithDefaults.assist?.temperature;
5082
5112
  if (typeof styleguide == "string" && validateStyleguide(styleguide), maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12)) throw Error(`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`);
5113
+ if (maxFieldSelectionDepth !== void 0 && (!Number.isInteger(maxFieldSelectionDepth) || maxFieldSelectionDepth < 1 || maxFieldSelectionDepth > 12)) throw Error(`[${packageName}]: \`assist.maxFieldSelectionDepth\` must be an integer in the range [1,12] inclusive, but was ${maxFieldSelectionDepth}`);
5083
5114
  if (temperature !== void 0 && (temperature < 0 || temperature > 1)) throw Error(`[${packageName}]: \`assist.temperature\` must be in the range [0,1] inclusive, but was ${temperature}`);
5084
5115
  return {
5085
5116
  name: packageName,
5086
5117
  handlesGDR: !0,
5087
- schema: { types: schemaTypes },
5118
+ schema: { types: createSchemaTypes(maxFieldSelectionDepth) },
5088
5119
  i18n: { bundles: [{}] },
5089
5120
  document: {
5090
5121
  inspectors: (prev, context) => {
@@ -5152,8 +5183,8 @@ function useListeningQuery(query, t0, t1) {
5152
5183
  let $ = c(11), params = t0 === void 0 ? DEFAULT_PARAMS : t0, options = t1 === void 0 ? DEFAULT_OPTIONS : t1, [loading, setLoading] = useState(!0), [error, setError] = useState(!1), [data, setData] = useState(null), subscription = useRef(null), t2;
5153
5184
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t2 = { apiVersion: "v2022-05-09" }, $[0] = t2) : t2 = $[0];
5154
5185
  let client = useClient(t2), t3, t4;
5155
- $[1] !== client || $[2] !== options || $[3] !== params || $[4] !== query ? (t3 = () => (query && (subscription.current = listenQuery(client, query, params, options).pipe(distinctUntilChanged(isEqual), catchError((err) => (console.error(err), setError(!0), setLoading(!1), setData(null), of(null)))).subscribe((documents) => {
5156
- setData((current) => isEqual(current, documents) ? current : documents), setLoading(!1), setError(!1);
5186
+ $[1] !== client || $[2] !== options || $[3] !== params || $[4] !== query ? (t3 = () => (query && (subscription.current = listenQuery(client, query, params, options).pipe(distinctUntilChanged(dequal), catchError((err) => (console.error(err), setError(!0), setLoading(!1), setData(null), of(null)))).subscribe((documents) => {
5187
+ setData((current) => dequal(current, documents) ? current : documents), setLoading(!1), setError(!1);
5157
5188
  })), () => subscription.current ? subscription.current.unsubscribe() : void 0), t4 = [
5158
5189
  query,
5159
5190
  params,